sysvbfs_vnops.c revision 1.22 1 /* $NetBSD: sysvbfs_vnops.c,v 1.22 2009/06/23 19:36:39 elad Exp $ */
2
3 /*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: sysvbfs_vnops.c,v 1.22 2009/06/23 19:36:39 elad Exp $");
34
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/resource.h>
38 #include <sys/vnode.h>
39 #include <sys/namei.h>
40 #include <sys/dirent.h>
41 #include <sys/malloc.h>
42 #include <sys/lockf.h>
43 #include <sys/unistd.h>
44 #include <sys/fcntl.h>
45 #include <sys/kauth.h>
46 #include <sys/buf.h>
47
48 #include <miscfs/genfs/genfs.h>
49
50 #include <fs/sysvbfs/sysvbfs.h>
51 #include <fs/sysvbfs/bfs.h>
52
53 #ifdef SYSVBFS_VNOPS_DEBUG
54 #define DPRINTF(fmt, args...) printf(fmt, ##args)
55 #else
56 #define DPRINTF(arg...) ((void)0)
57 #endif
58 #define ROUND_SECTOR(x) (((x) + 511) & ~511)
59
60 MALLOC_JUSTDEFINE(M_SYSVBFS_VNODE, "sysvbfs vnode", "sysvbfs vnode structures");
61 MALLOC_DECLARE(M_BFS);
62
63 int
64 sysvbfs_lookup(void *arg)
65 {
66 struct vop_lookup_args /* {
67 struct vnode *a_dvp;
68 struct vnode **a_vpp;
69 struct componentname *a_cnp;
70 } */ *a = arg;
71 struct vnode *v = a->a_dvp;
72 struct sysvbfs_node *bnode = v->v_data;
73 struct bfs *bfs = bnode->bmp->bfs; /* my filesystem */
74 struct vnode *vpp = NULL;
75 struct bfs_dirent *dirent = NULL;
76 struct componentname *cnp = a->a_cnp;
77 int nameiop = cnp->cn_nameiop;
78 const char *name = cnp->cn_nameptr;
79 int namelen = cnp->cn_namelen;
80 int error;
81 bool islastcn = cnp->cn_flags & ISLASTCN;
82
83 DPRINTF("%s: %s op=%d %d\n", __func__, name, nameiop,
84 cnp->cn_flags);
85
86 KASSERT((cnp->cn_flags & ISDOTDOT) == 0);
87 if ((error = VOP_ACCESS(a->a_dvp, VEXEC, cnp->cn_cred)) != 0) {
88 return error; /* directory permittion. */
89 }
90
91
92 if (namelen == 1 && name[0] == '.') { /* "." */
93 VREF(v);
94 *a->a_vpp = v;
95 } else { /* Regular file */
96 if (!bfs_dirent_lookup_by_name(bfs, cnp->cn_nameptr,
97 &dirent)) {
98 if (nameiop != CREATE && nameiop != RENAME) {
99 DPRINTF("%s: no such a file. (1)\n",
100 __func__);
101 return ENOENT;
102 }
103 if ((error = VOP_ACCESS(v, VWRITE, cnp->cn_cred)) != 0)
104 return error;
105 cnp->cn_flags |= SAVENAME;
106 return EJUSTRETURN;
107 }
108
109 /* Allocate v-node */
110 if ((error = sysvbfs_vget(v->v_mount, dirent->inode, &vpp)) != 0) {
111 DPRINTF("%s: can't get vnode.\n", __func__);
112 return error;
113 }
114 *a->a_vpp = vpp;
115 }
116
117 if (cnp->cn_nameiop != LOOKUP && islastcn)
118 cnp->cn_flags |= SAVENAME;
119
120 return 0;
121 }
122
123 int
124 sysvbfs_create(void *arg)
125 {
126 struct vop_create_args /* {
127 struct vnode *a_dvp;
128 struct vnode **a_vpp;
129 struct componentname *a_cnp;
130 struct vattr *a_vap;
131 } */ *a = arg;
132 struct sysvbfs_node *bnode = a->a_dvp->v_data;
133 struct sysvbfs_mount *bmp = bnode->bmp;
134 struct bfs *bfs = bmp->bfs;
135 struct mount *mp = bmp->mountp;
136 struct bfs_dirent *dirent;
137 struct bfs_fileattr attr;
138 struct vattr *va = a->a_vap;
139 kauth_cred_t cr = a->a_cnp->cn_cred;
140 int err = 0;
141
142 DPRINTF("%s: %s\n", __func__, a->a_cnp->cn_nameptr);
143 KDASSERT(a->a_vap->va_type == VREG);
144 attr.uid = kauth_cred_geteuid(cr);
145 attr.gid = kauth_cred_getegid(cr);
146 attr.mode = va->va_mode;
147
148 if ((err = bfs_file_create(bfs, a->a_cnp->cn_nameptr, 0, 0, &attr))
149 != 0) {
150 DPRINTF("%s: bfs_file_create failed.\n", __func__);
151 goto unlock_exit;
152 }
153
154 if (!bfs_dirent_lookup_by_name(bfs, a->a_cnp->cn_nameptr, &dirent))
155 panic("no dirent for created file.");
156
157 if ((err = sysvbfs_vget(mp, dirent->inode, a->a_vpp)) != 0) {
158 DPRINTF("%s: sysvbfs_vget failed.\n", __func__);
159 goto unlock_exit;
160 }
161 bnode = (*a->a_vpp)->v_data;
162 bnode->update_ctime = true;
163 bnode->update_mtime = true;
164 bnode->update_atime = true;
165
166 unlock_exit:
167 /* unlock parent directory */
168 vput(a->a_dvp); /* locked at sysvbfs_lookup(); */
169
170 return err;
171 }
172
173 int
174 sysvbfs_open(void *arg)
175 {
176 struct vop_open_args /* {
177 struct vnode *a_vp;
178 int a_mode;
179 kauth_cred_t a_cred;
180 } */ *a = arg;
181 struct vnode *v = a->a_vp;
182 struct sysvbfs_node *bnode = v->v_data;
183 struct bfs_inode *inode = bnode->inode;
184 struct bfs *bfs = bnode->bmp->bfs;
185 struct bfs_dirent *dirent;
186
187 DPRINTF("%s:\n", __func__);
188 KDASSERT(v->v_type == VREG || v->v_type == VDIR);
189
190 if (!bfs_dirent_lookup_by_inode(bfs, inode->number, &dirent))
191 return ENOENT;
192 bnode->update_atime = true;
193 if ((a->a_mode & FWRITE) && !(a->a_mode & O_APPEND)) {
194 bnode->size = 0;
195 } else {
196 bnode->size = bfs_file_size(inode);
197 }
198 bnode->data_block = inode->start_sector;
199
200 return 0;
201 }
202
203 int
204 sysvbfs_close(void *arg)
205 {
206 struct vop_close_args /* {
207 struct vnodeop_desc *a_desc;
208 struct vnode *a_vp;
209 int a_fflag;
210 kauth_cred_t a_cred;
211 } */ *a = arg;
212 struct vnode *v = a->a_vp;
213 struct sysvbfs_node *bnode = v->v_data;
214 struct bfs_fileattr attr;
215
216 DPRINTF("%s:\n", __func__);
217 uvm_vnp_setsize(v, bnode->size);
218
219 memset(&attr, 0xff, sizeof attr); /* Set VNOVAL all */
220 if (bnode->update_atime)
221 attr.atime = time_second;
222 if (bnode->update_ctime)
223 attr.ctime = time_second;
224 if (bnode->update_mtime)
225 attr.mtime = time_second;
226 bfs_inode_set_attr(bnode->bmp->bfs, bnode->inode, &attr);
227
228 VOP_FSYNC(a->a_vp, a->a_cred, FSYNC_WAIT, 0, 0);
229
230 return 0;
231 }
232
233 int
234 sysvbfs_access(void *arg)
235 {
236 struct vop_access_args /* {
237 struct vnode *a_vp;
238 int a_mode;
239 kauth_cred_t a_cred;
240 } */ *ap = arg;
241 struct vnode *vp = ap->a_vp;
242 struct sysvbfs_node *bnode = vp->v_data;
243 struct bfs_fileattr *attr = &bnode->inode->attr;
244
245 DPRINTF("%s:\n", __func__);
246 if ((ap->a_mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY))
247 return EROFS;
248
249 return genfs_can_access(vp->v_type, attr->mode, attr->uid, attr->gid,
250 ap->a_mode, ap->a_cred);
251 }
252
253 int
254 sysvbfs_getattr(void *v)
255 {
256 struct vop_getattr_args /* {
257 struct vnode *a_vp;
258 struct vattr *a_vap;
259 kauth_cred_t a_cred;
260 } */ *ap = v;
261 struct vnode *vp = ap->a_vp;
262 struct sysvbfs_node *bnode = vp->v_data;
263 struct bfs_inode *inode = bnode->inode;
264 struct bfs_fileattr *attr = &inode->attr;
265 struct sysvbfs_mount *bmp = bnode->bmp;
266 struct vattr *vap = ap->a_vap;
267
268 DPRINTF("%s:\n", __func__);
269
270 vap->va_type = vp->v_type;
271 vap->va_mode = attr->mode;
272 vap->va_nlink = attr->nlink;
273 vap->va_uid = attr->uid;
274 vap->va_gid = attr->gid;
275 vap->va_fsid = bmp->devvp->v_rdev;
276 vap->va_fileid = inode->number;
277 vap->va_size = bfs_file_size(inode);
278 vap->va_blocksize = BFS_BSIZE;
279 vap->va_atime.tv_sec = attr->atime;
280 vap->va_mtime.tv_sec = attr->mtime;
281 vap->va_ctime.tv_sec = attr->ctime;
282 vap->va_birthtime.tv_sec = 0;
283 vap->va_gen = 1;
284 vap->va_flags = 0;
285 vap->va_rdev = 0; /* No device file */
286 vap->va_bytes = vap->va_size;
287 vap->va_filerev = 0;
288 vap->va_vaflags = 0;
289
290 return 0;
291 }
292
293 int
294 sysvbfs_setattr(void *arg)
295 {
296 struct vop_setattr_args /* {
297 struct vnode *a_vp;
298 struct vattr *a_vap;
299 kauth_cred_t a_cred;
300 struct proc *p;
301 } */ *ap = arg;
302 struct vnode *vp = ap->a_vp;
303 struct vattr *vap = ap->a_vap;
304 struct sysvbfs_node *bnode = vp->v_data;
305 struct bfs_inode *inode = bnode->inode;
306 struct bfs_fileattr *attr = &inode->attr;
307 struct bfs *bfs = bnode->bmp->bfs;
308
309 DPRINTF("%s:\n", __func__);
310 if (vp->v_mount->mnt_flag & MNT_RDONLY)
311 return EROFS;
312
313 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
314 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
315 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
316 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL))
317 return EINVAL;
318
319 if (vap->va_uid != (uid_t)VNOVAL)
320 attr->uid = vap->va_uid;
321 if (vap->va_gid != (uid_t)VNOVAL)
322 attr->gid = vap->va_gid;
323 if (vap->va_mode != (mode_t)VNOVAL)
324 attr->mode = vap->va_mode;
325 if (vap->va_atime.tv_sec != VNOVAL)
326 attr->atime = vap->va_atime.tv_sec;
327 if (vap->va_mtime.tv_sec != VNOVAL)
328 attr->mtime = vap->va_mtime.tv_sec;
329 if (vap->va_ctime.tv_sec != VNOVAL)
330 attr->ctime = vap->va_ctime.tv_sec;
331
332 bfs_inode_set_attr(bfs, inode, attr);
333
334 return 0;
335 }
336
337 int
338 sysvbfs_read(void *arg)
339 {
340 struct vop_read_args /* {
341 struct vnode *a_vp;
342 struct uio *a_uio;
343 int a_ioflag;
344 kauth_cred_t a_cred;
345 } */ *a = arg;
346 struct vnode *v = a->a_vp;
347 struct uio *uio = a->a_uio;
348 struct sysvbfs_node *bnode = v->v_data;
349 struct bfs_inode *inode = bnode->inode;
350 vsize_t sz, filesz = bfs_file_size(inode);
351 int err;
352 const int advice = IO_ADV_DECODE(a->a_ioflag);
353
354 DPRINTF("%s: type=%d\n", __func__, v->v_type);
355 if (v->v_type != VREG)
356 return EINVAL;
357
358 while (uio->uio_resid > 0) {
359 if ((sz = MIN(filesz - uio->uio_offset, uio->uio_resid)) == 0)
360 break;
361
362 err = ubc_uiomove(&v->v_uobj, uio, sz, advice,
363 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(v));
364 if (err)
365 break;
366 DPRINTF("%s: read %ldbyte\n", __func__, sz);
367 }
368
369 return sysvbfs_update(v, NULL, NULL, UPDATE_WAIT);
370 }
371
372 int
373 sysvbfs_write(void *arg)
374 {
375 struct vop_write_args /* {
376 struct vnode *a_vp;
377 struct uio *a_uio;
378 int a_ioflag;
379 kauth_cred_t a_cred;
380 } */ *a = arg;
381 struct vnode *v = a->a_vp;
382 struct uio *uio = a->a_uio;
383 int advice = IO_ADV_DECODE(a->a_ioflag);
384 struct sysvbfs_node *bnode = v->v_data;
385 struct bfs_inode *inode = bnode->inode;
386 bool extended = false;
387 vsize_t sz;
388 int err = 0;
389
390 if (a->a_vp->v_type != VREG)
391 return EISDIR;
392
393 if (a->a_ioflag & IO_APPEND)
394 uio->uio_offset = bnode->size;
395
396 if (uio->uio_resid == 0)
397 return 0;
398
399 if (bnode->size < uio->uio_offset + uio->uio_resid) {
400 bnode->size = uio->uio_offset + uio->uio_resid;
401 uvm_vnp_setsize(v, bnode->size);
402 extended = true;
403 }
404
405 while (uio->uio_resid > 0) {
406 sz = uio->uio_resid;
407 err = ubc_uiomove(&v->v_uobj, uio, sz, advice,
408 UBC_WRITE | UBC_UNMAP_FLAG(v));
409 if (err)
410 break;
411 DPRINTF("%s: write %ldbyte\n", __func__, sz);
412 }
413 inode->end_sector = bnode->data_block +
414 (ROUND_SECTOR(bnode->size) >> DEV_BSHIFT) - 1;
415 inode->eof_offset_byte = bnode->data_block * DEV_BSIZE +
416 bnode->size - 1;
417 bnode->update_mtime = true;
418
419 VN_KNOTE(v, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
420
421 return err;
422 }
423
424 int
425 sysvbfs_remove(void *arg)
426 {
427 struct vop_remove_args /* {
428 struct vnodeop_desc *a_desc;
429 struct vnode * a_dvp;
430 struct vnode * a_vp;
431 struct componentname * a_cnp;
432 } */ *ap = arg;
433 struct vnode *vp = ap->a_vp;
434 struct vnode *dvp = ap->a_dvp;
435 struct sysvbfs_node *bnode = vp->v_data;
436 struct sysvbfs_mount *bmp = bnode->bmp;
437 struct bfs *bfs = bmp->bfs;
438 int err;
439
440 DPRINTF("%s: delete %s\n", __func__, ap->a_cnp->cn_nameptr);
441
442 if (vp->v_type == VDIR)
443 return EPERM;
444
445 if ((err = bfs_file_delete(bfs, ap->a_cnp->cn_nameptr)) != 0)
446 DPRINTF("%s: bfs_file_delete failed.\n", __func__);
447
448 VN_KNOTE(ap->a_vp, NOTE_DELETE);
449 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
450 if (dvp == vp)
451 vrele(vp);
452 else
453 vput(vp);
454 vput(dvp);
455
456 return err;
457 }
458
459 int
460 sysvbfs_rename(void *arg)
461 {
462 struct vop_rename_args /* {
463 struct vnode *a_fdvp; from parent-directory v-node
464 struct vnode *a_fvp; from file v-node
465 struct componentname *a_fcnp;
466 struct vnode *a_tdvp; to parent-directory
467 struct vnode *a_tvp; to file v-node
468 struct componentname *a_tcnp;
469 } */ *ap = arg;
470 struct vnode *fvp = ap->a_fvp;
471 struct vnode *tvp = ap->a_tvp;
472 struct sysvbfs_node *bnode = fvp->v_data;
473 struct bfs *bfs = bnode->bmp->bfs;
474 const char *from_name = ap->a_fcnp->cn_nameptr;
475 const char *to_name = ap->a_tcnp->cn_nameptr;
476 int error;
477
478 DPRINTF("%s: %s->%s\n", __func__, from_name, to_name);
479 if ((fvp->v_mount != ap->a_tdvp->v_mount) ||
480 (tvp && (fvp->v_mount != tvp->v_mount))) {
481 error = EXDEV;
482 printf("cross-device link\n");
483 goto out;
484 }
485
486 KDASSERT(fvp->v_type == VREG);
487 KDASSERT(tvp == NULL ? true : tvp->v_type == VREG);
488
489 error = bfs_file_rename(bfs, from_name, to_name);
490 out:
491 vput(ap->a_tdvp);
492 if (tvp)
493 vput(ap->a_tvp); /* locked on entry */
494 if (ap->a_tdvp != ap->a_fdvp)
495 vrele(ap->a_fdvp);
496 vrele(ap->a_fvp); /* unlocked and refcnt is incremented on entry. */
497
498 return 0;
499 }
500
501 int
502 sysvbfs_readdir(void *v)
503 {
504 struct vop_readdir_args /* {
505 struct vnode *a_vp;
506 struct uio *a_uio;
507 kauth_cred_t a_cred;
508 int *a_eofflag;
509 off_t **a_cookies;
510 int *a_ncookies;
511 } */ *ap = v;
512 struct uio *uio = ap->a_uio;
513 struct vnode *vp = ap->a_vp;
514 struct sysvbfs_node *bnode = vp->v_data;
515 struct bfs *bfs = bnode->bmp->bfs;
516 struct dirent *dp;
517 struct bfs_dirent *file;
518 int i, n, error;
519
520 DPRINTF("%s: offset=%lld residue=%d\n", __func__,
521 uio->uio_offset, uio->uio_resid);
522
523 KDASSERT(vp->v_type == VDIR);
524 KDASSERT(uio->uio_offset >= 0);
525
526 dp = malloc(sizeof(struct dirent), M_BFS, M_WAITOK | M_ZERO);
527
528 i = uio->uio_offset / sizeof(struct dirent);
529 n = uio->uio_resid / sizeof(struct dirent);
530 if ((i + n) > bfs->n_dirent)
531 n = bfs->n_dirent - i;
532
533 for (file = &bfs->dirent[i]; i < n; file++) {
534 if (file->inode == 0)
535 continue;
536 if (i == bfs->max_dirent) {
537 DPRINTF("%s: file system inconsistent.\n",
538 __func__);
539 break;
540 }
541 i++;
542 memset(dp, 0, sizeof(struct dirent));
543 dp->d_fileno = file->inode;
544 dp->d_type = file->inode == BFS_ROOT_INODE ? DT_DIR : DT_REG;
545 dp->d_namlen = strlen(file->name);
546 strncpy(dp->d_name, file->name, BFS_FILENAME_MAXLEN);
547 dp->d_reclen = sizeof(struct dirent);
548 if ((error = uiomove(dp, dp->d_reclen, uio)) != 0) {
549 DPRINTF("%s: uiomove failed.\n", __func__);
550 free(dp, M_BFS);
551 return error;
552 }
553 }
554 DPRINTF("%s: %d %d %d\n", __func__, i, n, bfs->n_dirent);
555 *ap->a_eofflag = (i == bfs->n_dirent);
556
557 free(dp, M_BFS);
558 return 0;
559 }
560
561 int
562 sysvbfs_inactive(void *arg)
563 {
564 struct vop_inactive_args /* {
565 struct vnode *a_vp;
566 bool *a_recycle;
567 } */ *a = arg;
568 struct vnode *v = a->a_vp;
569
570 DPRINTF("%s:\n", __func__);
571 *a->a_recycle = true;
572 VOP_UNLOCK(v, 0);
573
574 return 0;
575 }
576
577 int
578 sysvbfs_reclaim(void *v)
579 {
580 extern struct pool sysvbfs_node_pool;
581 struct vop_reclaim_args /* {
582 struct vnode *a_vp;
583 } */ *ap = v;
584 struct vnode *vp = ap->a_vp;
585 struct sysvbfs_node *bnode = vp->v_data;
586
587 DPRINTF("%s:\n", __func__);
588 mutex_enter(&mntvnode_lock);
589 LIST_REMOVE(bnode, link);
590 mutex_exit(&mntvnode_lock);
591 cache_purge(vp);
592 genfs_node_destroy(vp);
593 pool_put(&sysvbfs_node_pool, bnode);
594 vp->v_data = NULL;
595
596 return 0;
597 }
598
599 int
600 sysvbfs_bmap(void *arg)
601 {
602 struct vop_bmap_args /* {
603 struct vnode *a_vp;
604 daddr_t a_bn;
605 struct vnode **a_vpp;
606 daddr_t *a_bnp;
607 int *a_runp;
608 } */ *a = arg;
609 struct vnode *v = a->a_vp;
610 struct sysvbfs_node *bnode = v->v_data;
611 struct sysvbfs_mount *bmp = bnode->bmp;
612 struct bfs_inode *inode = bnode->inode;
613 daddr_t blk;
614
615 DPRINTF("%s:\n", __func__);
616 /* BFS algorithm is contiguous allocation */
617 blk = inode->start_sector + a->a_bn;
618
619 if (blk * BFS_BSIZE > bmp->bfs->data_end)
620 return ENOSPC;
621
622 *a->a_vpp = bmp->devvp;
623 *a->a_runp = 0;
624 DPRINTF("%s: %d + %lld\n", __func__, inode->start_sector, a->a_bn);
625
626 *a->a_bnp = blk;
627
628
629 return 0;
630 }
631
632 int
633 sysvbfs_strategy(void *arg)
634 {
635 struct vop_strategy_args /* {
636 struct vnode *a_vp;
637 struct buf *a_bp;
638 } */ *a = arg;
639 struct buf *b = a->a_bp;
640 struct vnode *v = a->a_vp;
641 struct sysvbfs_node *bnode = v->v_data;
642 struct sysvbfs_mount *bmp = bnode->bmp;
643 int error;
644
645 DPRINTF("%s:\n", __func__);
646 KDASSERT(v->v_type == VREG);
647 if (b->b_blkno == b->b_lblkno) {
648 error = VOP_BMAP(v, b->b_lblkno, NULL, &b->b_blkno, NULL);
649 if (error) {
650 b->b_error = error;
651 biodone(b);
652 return error;
653 }
654 if ((long)b->b_blkno == -1)
655 clrbuf(b);
656 }
657 if ((long)b->b_blkno == -1) {
658 biodone(b);
659 return 0;
660 }
661
662 return VOP_STRATEGY(bmp->devvp, b);
663 }
664
665 int
666 sysvbfs_print(void *v)
667 {
668 struct vop_print_args /* {
669 struct vnode *a_vp;
670 } */ *ap = v;
671 struct sysvbfs_node *bnode = ap->a_vp->v_data;
672
673 DPRINTF("%s:\n", __func__);
674 bfs_dump(bnode->bmp->bfs);
675
676 return 0;
677 }
678
679 int
680 sysvbfs_advlock(void *v)
681 {
682 struct vop_advlock_args /* {
683 struct vnode *a_vp;
684 void *a_id;
685 int a_op;
686 struct flock *a_fl;
687 int a_flags;
688 } */ *ap = v;
689 struct sysvbfs_node *bnode = ap->a_vp->v_data;
690
691 DPRINTF("%s: op=%d\n", __func__, ap->a_op);
692
693 return lf_advlock(ap, &bnode->lockf, bfs_file_size(bnode->inode));
694 }
695
696 int
697 sysvbfs_pathconf(void *v)
698 {
699 struct vop_pathconf_args /* {
700 struct vnode *a_vp;
701 int a_name;
702 register_t *a_retval;
703 } */ *ap = v;
704 int err = 0;
705
706 DPRINTF("%s:\n", __func__);
707
708 switch (ap->a_name) {
709 case _PC_LINK_MAX:
710 *ap->a_retval = 1;
711 break;;
712 case _PC_NAME_MAX:
713 *ap->a_retval = BFS_FILENAME_MAXLEN;
714 break;;
715 case _PC_PATH_MAX:
716 *ap->a_retval = BFS_FILENAME_MAXLEN;
717 break;;
718 case _PC_CHOWN_RESTRICTED:
719 *ap->a_retval = 1;
720 break;;
721 case _PC_NO_TRUNC:
722 *ap->a_retval = 0;
723 break;;
724 case _PC_SYNC_IO:
725 *ap->a_retval = 1;
726 break;;
727 case _PC_FILESIZEBITS:
728 *ap->a_retval = 32;
729 break;;
730 default:
731 err = EINVAL;
732 break;
733 }
734
735 return err;
736 }
737
738 int
739 sysvbfs_fsync(void *v)
740 {
741 struct vop_fsync_args /* {
742 struct vnode *a_vp;
743 kauth_cred_t a_cred;
744 int a_flags;
745 off_t offlo;
746 off_t offhi;
747 } */ *ap = v;
748 struct vnode *vp = ap->a_vp;
749 int error, wait;
750
751 if (ap->a_flags & FSYNC_CACHE) {
752 return EOPNOTSUPP;
753 }
754
755 wait = (ap->a_flags & FSYNC_WAIT) != 0;
756 vflushbuf(vp, wait);
757
758 if ((ap->a_flags & FSYNC_DATAONLY) != 0)
759 error = 0;
760 else
761 error = sysvbfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
762
763 return error;
764 }
765
766 int
767 sysvbfs_update(struct vnode *vp, const struct timespec *acc,
768 const struct timespec *mod, int flags)
769 {
770 struct sysvbfs_node *bnode = vp->v_data;
771 struct bfs_fileattr attr;
772
773 DPRINTF("%s:\n", __func__);
774 memset(&attr, 0xff, sizeof attr); /* Set VNOVAL all */
775 if (bnode->update_atime) {
776 attr.atime = acc ? acc->tv_sec : time_second;
777 bnode->update_atime = false;
778 }
779 if (bnode->update_ctime) {
780 attr.ctime = time_second;
781 bnode->update_ctime = false;
782 }
783 if (bnode->update_mtime) {
784 attr.mtime = mod ? mod->tv_sec : time_second;
785 bnode->update_mtime = false;
786 }
787 bfs_inode_set_attr(bnode->bmp->bfs, bnode->inode, &attr);
788
789 return 0;
790 }
791