chfs_vnops.c revision 1.26 1 /* $NetBSD: chfs_vnops.c,v 1.26 2015/03/28 17:23:42 maxv Exp $ */
2
3 /*-
4 * Copyright (c) 2010 Department of Software Engineering,
5 * University of Szeged, Hungary
6 * Copyright (C) 2010 Tamas Toth <ttoth (at) inf.u-szeged.hu>
7 * Copyright (C) 2010 Adam Hoka <ahoka (at) NetBSD.org>
8 * All rights reserved.
9 *
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by the Department of Software Engineering, University of Szeged, Hungary
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/param.h>
36 #include <miscfs/specfs/specdev.h>
37 #include <miscfs/fifofs/fifo.h>
38 #include <miscfs/genfs/genfs.h>
39 #include <ufs/ufs/dir.h>
40 #include <ufs/ufs/ufs_extern.h>
41 #include <uvm/uvm.h>
42 #include <sys/namei.h>
43 #include <sys/stat.h>
44 #include <sys/fcntl.h>
45 #include <sys/buf.h>
46 #include <sys/fstrans.h>
47 #include <sys/vnode.h>
48
49 #include "chfs.h"
50
51 #define READ_S "chfs_read"
52
53 int
54 chfs_lookup(void *v)
55 {
56 struct vnode *dvp = ((struct vop_lookup_v2_args *) v)->a_dvp;
57 struct vnode **vpp = ((struct vop_lookup_v2_args *) v)->a_vpp;
58 struct componentname *cnp = ((struct vop_lookup_v2_args *) v)->a_cnp;
59
60 int error;
61 struct chfs_inode* ip;
62 struct ufsmount* ump;
63 struct chfs_mount* chmp;
64 struct chfs_vnode_cache* chvc;
65 struct chfs_dirent* fd;
66
67 dbg("lookup(): %s\n", cnp->cn_nameptr);
68
69 KASSERT(VOP_ISLOCKED(dvp));
70
71 *vpp = NULL;
72
73 /* Check accessibility of requested node as a first step. */
74 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
75 if (error != 0) {
76 goto out;
77 }
78
79 /* If requesting the last path component on a read-only file system
80 * with a write operation, deny it. */
81 if ((cnp->cn_flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY)
82 && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
83 error = EROFS;
84 goto out;
85 }
86
87 /* Avoid doing a linear scan of the directory if the requested
88 * directory/name couple is already in the cache. */
89 if (cache_lookup(dvp, cnp->cn_nameptr, cnp->cn_namelen,
90 cnp->cn_nameiop, cnp->cn_flags, NULL, vpp)) {
91 return (*vpp == NULLVP ? ENOENT : 0);
92 }
93
94 ip = VTOI(dvp);
95 ump = VFSTOUFS(dvp->v_mount);
96 chmp = ump->um_chfs;
97 if (ip->ino == 0) {
98 ip->ino = ++chmp->chm_max_vno;
99 }
100 mutex_enter(&chmp->chm_lock_vnocache);
101 chvc = chfs_vnode_cache_get(chmp, ip->ino);
102 mutex_exit(&chmp->chm_lock_vnocache);
103
104 /* We cannot be requesting the parent directory of the root node. */
105 KASSERT(IMPLIES(ip->ch_type == CHT_DIR && chvc->pvno == chvc->vno,
106 !(cnp->cn_flags & ISDOTDOT)));
107
108 if (cnp->cn_flags & ISDOTDOT) {
109 VOP_UNLOCK(dvp);
110 error = VFS_VGET(dvp->v_mount, ip->chvc->pvno, vpp);
111 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
112 } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
113 vref(dvp);
114 *vpp = dvp;
115 error = 0;
116 } else {
117 fd = chfs_dir_lookup(ip, cnp);
118
119 if (fd == NULL) {
120 dbg("fd null\n");
121 /* The entry was not found in the directory.
122 * This is OK if we are creating or renaming an
123 * entry and are working on the last component of
124 * the path name. */
125 if ((cnp->cn_flags & ISLASTCN) && (cnp->cn_nameiop == CREATE
126 || cnp->cn_nameiop == RENAME)) {
127 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
128 if (error) {
129 dbg("after the entry was not found in dir\n");
130 goto out;
131 }
132
133 dbg("return EJUSTRETURN\n");
134 error = EJUSTRETURN;
135 } else {
136 error = ENOENT;
137 }
138 } else {
139 /* If we are not at the last path component and
140 * found a non-directory or non-link entry (which
141 * may itself be pointing to a directory), raise
142 * an error. */
143 if ((fd->type != CHT_DIR && fd->type != CHT_LNK) && !(cnp->cn_flags
144 & ISLASTCN)) {
145 error = ENOTDIR;
146 goto out;
147 }
148
149 dbg("vno@allocating new vnode: %llu\n",
150 (unsigned long long)fd->vno);
151 error = VFS_VGET(dvp->v_mount, fd->vno, vpp);
152 }
153 }
154 /* Store the result of this lookup in the cache. Avoid this if the
155 * request was for creation, as it does not improve timings on
156 * emprical tests. */
157 if (cnp->cn_nameiop != CREATE && (cnp->cn_flags & ISDOTDOT) == 0) {
158 cache_enter(dvp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
159 cnp->cn_flags);
160 }
161
162 out:
163 /* If there were no errors, *vpp cannot be NULL. */
164 KASSERT(IFF(error == 0, *vpp != NULL));
165 KASSERT(VOP_ISLOCKED(dvp));
166
167 if (error)
168 return error;
169 if (*vpp != dvp)
170 VOP_UNLOCK(*vpp);
171 return 0;
172 }
173
174 /* --------------------------------------------------------------------- */
175
176 int
177 chfs_create(void *v)
178 {
179 struct vop_create_v3_args /* {
180 struct vnode *a_dvp;
181 struct vnode **a_vpp;
182 struct componentname *a_cnp;
183 struct vattr *a_vap;
184 } */*ap = v;
185 int error, mode;
186 dbg("create()\n");
187
188 mode = MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode);
189
190 if ((mode & IFMT) == 0) {
191 if (ap->a_vap->va_type == VREG)
192 mode |= IFREG;
193 if (ap->a_vap->va_type == VSOCK)
194 mode |= IFSOCK;
195 }
196
197 error = chfs_makeinode(mode, ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap->va_type);
198
199 if (error) {
200 dbg("error: %d\n", error);
201 return error;
202 }
203
204 VN_KNOTE(ap->a_dvp, NOTE_WRITE);
205 return 0;
206 }
207 /* --------------------------------------------------------------------- */
208
209 int
210 chfs_mknod(void *v)
211 {
212 struct vnode *dvp = ((struct vop_mknod_v3_args *) v)->a_dvp;
213 struct vnode **vpp = ((struct vop_mknod_v3_args *) v)->a_vpp;
214 struct componentname *cnp = ((struct vop_mknod_v3_args *) v)->a_cnp;
215 struct vattr *vap = ((struct vop_mknod_v3_args *) v)->a_vap;
216 int mode, err = 0;
217 struct chfs_inode *ip;
218 struct vnode *vp;
219
220 struct ufsmount *ump;
221 struct chfs_mount *chmp;
222
223 struct chfs_full_dnode *fd;
224 struct buf *bp;
225 int len;
226 dbg("mknod()\n");
227
228 ump = VFSTOUFS(dvp->v_mount);
229 chmp = ump->um_chfs;
230
231 /* Check type of node. */
232 if (vap->va_type != VBLK && vap->va_type != VCHR && vap->va_type != VFIFO)
233 return EINVAL;
234
235 vp = *vpp;
236
237 mode = MAKEIMODE(vap->va_type, vap->va_mode);
238
239 if ((mode & IFMT) == 0) {
240 switch (vap->va_type) {
241 case VBLK:
242 mode |= IFBLK;
243 break;
244 case VCHR:
245 mode |= IFCHR;
246 break;
247 case VFIFO:
248 mode |= IFIFO;
249 break;
250 default:
251 break;
252 }
253 }
254
255 /* Create a new node. */
256 err = chfs_makeinode(mode, dvp, &vp, cnp, vap->va_type);
257
258 ip = VTOI(vp);
259 if (vap->va_rdev != VNOVAL)
260 ip->rdev = vap->va_rdev;
261
262 if (vap->va_type == VFIFO)
263 vp->v_op = chfs_fifoop_p;
264 else {
265 vp->v_op = chfs_specop_p;
266 spec_node_init(vp, ip->rdev);
267 }
268
269 if (err)
270 return err;
271
272 /* Device is written out as a data node. */
273 len = sizeof(dev_t);
274 chfs_set_vnode_size(vp, len);
275 bp = getiobuf(vp, true);
276 bp->b_bufsize = bp->b_resid = len;
277 bp->b_data = kmem_alloc(len, KM_SLEEP);
278 memcpy(bp->b_data, &ip->rdev, len);
279 bp->b_blkno = 0;
280
281 fd = chfs_alloc_full_dnode();
282
283 mutex_enter(&chmp->chm_lock_mountfields);
284
285 err = chfs_write_flash_dnode(chmp, vp, bp, fd);
286 if (err) {
287 mutex_exit(&chmp->chm_lock_mountfields);
288 kmem_free(bp->b_data, len);
289 return err;
290 }
291
292 /* Add data node to the inode. */
293 err = chfs_add_full_dnode_to_inode(chmp, ip, fd);
294 if (err) {
295 mutex_exit(&chmp->chm_lock_mountfields);
296 kmem_free(bp->b_data, len);
297 return err;
298 }
299
300 mutex_exit(&chmp->chm_lock_mountfields);
301
302 *vpp = vp;
303 kmem_free(bp->b_data, len);
304 putiobuf(bp);
305
306 return 0;
307 }
308
309 /* --------------------------------------------------------------------- */
310
311 int
312 chfs_open(void *v)
313 {
314 struct vnode *vp = ((struct vop_open_args *) v)->a_vp;
315 int mode = ((struct vop_open_args *) v)->a_mode;
316 dbg("open()\n");
317
318 int error;
319 struct chfs_inode *ip;
320
321 KASSERT(VOP_ISLOCKED(vp));
322
323 ip = VTOI(vp);
324
325 KASSERT(vp->v_size == ip->size);
326 if (ip->chvc->nlink < 1) {
327 error = ENOENT;
328 goto out;
329 }
330
331 /* If the file is marked append-only, deny write requests. */
332 if (ip->flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
333 error = EPERM;
334 else
335 error = 0;
336
337 out:
338 KASSERT(VOP_ISLOCKED(vp));
339 return error;
340 }
341
342 /* --------------------------------------------------------------------- */
343
344 int
345 chfs_close(void *v)
346 {
347 struct vnode *vp = ((struct vop_close_args *) v)->a_vp;
348 dbg("close()\n");
349
350 struct chfs_inode *ip;
351
352 KASSERT(VOP_ISLOCKED(vp));
353
354 ip = VTOI(vp);
355
356 if (ip->chvc->nlink > 0) {
357 chfs_update(vp, NULL, NULL, UPDATE_CLOSE);
358 }
359
360 return 0;
361 }
362
363 /* --------------------------------------------------------------------- */
364
365 int
366 chfs_access(void *v)
367 {
368 struct vnode *vp = ((struct vop_access_args *) v)->a_vp;
369 int mode = ((struct vop_access_args *) v)->a_mode;
370 kauth_cred_t cred = ((struct vop_access_args *) v)->a_cred;
371
372 dbg("access()\n");
373 struct chfs_inode *ip = VTOI(vp);
374
375 if (mode & VWRITE) {
376 switch (vp->v_type) {
377 case VLNK:
378 case VDIR:
379 case VREG:
380 if (vp->v_mount->mnt_flag & MNT_RDONLY)
381 return (EROFS);
382 break;
383 case VBLK:
384 case VCHR:
385 case VSOCK:
386 case VFIFO:
387 break;
388 default:
389 break;
390 }
391 }
392
393 if (mode & VWRITE && ip->flags & IMMUTABLE)
394 return (EPERM);
395
396 return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode, vp->v_type,
397 ip->mode & ALLPERMS), vp, NULL, genfs_can_access(vp->v_type,
398 ip->mode & ALLPERMS, ip->uid, ip->gid, mode, cred));
399 }
400
401 /* --------------------------------------------------------------------- */
402
403 int
404 chfs_getattr(void *v)
405 {
406 struct vnode *vp = ((struct vop_getattr_args *) v)->a_vp;
407 struct vattr *vap = ((struct vop_getattr_args *) v)->a_vap;
408
409 struct chfs_inode *ip = VTOI(vp);
410 dbg("getattr()\n");
411
412 KASSERT(vp->v_size == ip->size);
413
414 vattr_null(vap);
415 CHFS_ITIMES(ip, NULL, NULL, NULL);
416
417 vap->va_type = CHTTOVT(ip->ch_type);
418 vap->va_mode = ip->mode & ALLPERMS;
419 vap->va_nlink = ip->chvc->nlink;
420 vap->va_uid = ip->uid;
421 vap->va_gid = ip->gid;
422 vap->va_fsid = ip->dev;
423 vap->va_fileid = ip->ino;
424 vap->va_size = ip->size;
425 vap->va_blocksize = PAGE_SIZE;
426 vap->va_atime.tv_sec = ip->atime;
427 vap->va_atime.tv_nsec = 0;
428 vap->va_mtime.tv_sec = ip->mtime;
429 vap->va_mtime.tv_nsec = 0;
430 vap->va_ctime.tv_sec = ip->ctime;
431 vap->va_ctime.tv_nsec = 0;
432 vap->va_gen = ip->version;
433 vap->va_flags = ip->flags;
434 vap->va_rdev = ip->rdev;
435 vap->va_bytes = round_page(ip->size);
436 vap->va_filerev = VNOVAL;
437 vap->va_vaflags = 0;
438 vap->va_spare = VNOVAL;
439
440 return 0;
441 }
442
443 /* --------------------------------------------------------------------- */
444
445 /* Note: modelled after tmpfs's same function */
446
447 int
448 chfs_setattr(void *v)
449 {
450 struct vnode *vp = ((struct vop_setattr_args *) v)->a_vp;
451 struct vattr *vap = ((struct vop_setattr_args *) v)->a_vap;
452 kauth_cred_t cred = ((struct vop_setattr_args *) v)->a_cred;
453
454 struct chfs_inode *ip;
455 struct ufsmount *ump = VFSTOUFS(vp->v_mount);
456 struct chfs_mount *chmp = ump->um_chfs;
457 int error = 0;
458
459 dbg("setattr()\n");
460
461 KASSERT(VOP_ISLOCKED(vp));
462 ip = VTOI(vp);
463
464 /* Abort if any unsettable attribute is given. */
465 if (vap->va_type != VNON || vap->va_nlink != VNOVAL ||
466 vap->va_fsid != VNOVAL || vap->va_fileid != VNOVAL ||
467 vap->va_blocksize != VNOVAL /*|| GOODTIME(&vap->va_ctime)*/ ||
468 vap->va_gen != VNOVAL || vap->va_rdev != VNOVAL ||
469 vap->va_bytes != VNOVAL) {
470 return EINVAL;
471 }
472
473 /* set flags */
474 if (error == 0 && (vap->va_flags != VNOVAL)) {
475 error = chfs_chflags(vp, vap->va_flags, cred);
476 return error;
477 }
478
479 if (ip->flags & (IMMUTABLE | APPEND)) {
480 error = EPERM;
481 return error;
482 }
483
484 /* set size */
485 if (error == 0 && (vap->va_size != VNOVAL)) {
486 error = chfs_chsize(vp, vap->va_size, cred);
487 if (error)
488 return error;
489 }
490
491 /* set owner */
492 if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL)) {
493 error = chfs_chown(vp, vap->va_uid, vap->va_gid, cred);
494 if (error)
495 return error;
496 }
497
498 /* set mode */
499 if (error == 0 && (vap->va_mode != VNOVAL)) {
500 error = chfs_chmod(vp, vap->va_mode, cred);
501 if (error)
502 return error;
503 }
504
505 /* set time */
506 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
507 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp,
508 NULL, genfs_can_chtimes(vp, vap->va_vaflags, ip->uid, cred));
509 if (error)
510 return error;
511 if (vap->va_atime.tv_sec != VNOVAL)
512 ip->iflag |= IN_ACCESS;
513 if (vap->va_mtime.tv_sec != VNOVAL)
514 ip->iflag |= IN_CHANGE | IN_UPDATE;
515 error = chfs_update(vp,
516 &vap->va_atime, &vap->va_mtime, UPDATE_WAIT);
517 if (error)
518 return error;
519 }
520
521 /* Write it out. */
522 mutex_enter(&chmp->chm_lock_mountfields);
523 error = chfs_write_flash_vnode(chmp, ip, ALLOC_NORMAL);
524 mutex_exit(&chmp->chm_lock_mountfields);
525
526 return error;
527 }
528
529 int
530 chfs_chmod(struct vnode *vp, int mode, kauth_cred_t cred)
531 {
532 struct chfs_inode *ip = VTOI(vp);
533 int error;
534 dbg("chmod\n");
535
536 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp,
537 NULL, genfs_can_chmod(vp->v_type, cred, ip->uid, ip->gid, mode));
538 if (error)
539 return error;
540 ip->mode &= ~ALLPERMS;
541 ip->mode |= (mode & ALLPERMS);
542 ip->iflag |= IN_CHANGE;
543
544 error = chfs_update(vp, NULL, NULL, UPDATE_WAIT);
545 if (error)
546 return error;
547
548 return 0;
549 }
550
551 int
552 chfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred)
553 {
554 struct chfs_inode *ip = VTOI(vp);
555 int error;
556 dbg("chown\n");
557
558 if (uid == (uid_t)VNOVAL)
559 uid = ip->uid;
560 if (gid == (gid_t)VNOVAL)
561 gid = ip->gid;
562
563 error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp,
564 NULL, genfs_can_chown(cred, ip->uid, ip->gid, uid, gid));
565 if (error)
566 return error;
567
568 ip->gid = gid;
569 ip->uid = uid;
570 ip->iflag |= IN_CHANGE;
571
572 error = chfs_update(vp, NULL, NULL, UPDATE_WAIT);
573 if (error)
574 return error;
575
576 return 0;
577 }
578
579
580 /* --------------------------------------------------------------------- */
581 /* calculates ((off_t)blk * chmp->chm_chm_fs_bsize) */
582 #define chfs_lblktosize(chmp, blk) \
583 (((off_t)(blk)) << (chmp)->chm_fs_bshift)
584
585 /* calculates (loc % chmp->chm_chm_fs_bsize) */
586 #define chfs_blkoff(chmp, loc) \
587 ((loc) & (chmp)->chm_fs_qbmask)
588
589 /* calculates (loc / chmp->chm_chm_fs_bsize) */
590 #define chfs_lblkno(chmp, loc) \
591 ((loc) >> (chmp)->chm_fs_bshift)
592
593 /* calculates roundup(size, chmp->chm_chm_fs_fsize) */
594 #define chfs_fragroundup(chmp, size) \
595 (((size) + (chmp)->chm_fs_qfmask) & (chmp)->chm_fs_fmask)
596
597 #define chfs_blksize(chmp, ip, lbn) \
598 (((lbn) >= UFS_NDADDR || (ip)->size >= chfs_lblktosize(chmp, (lbn) + 1)) \
599 ? (chmp)->chm_fs_bsize \
600 : (chfs_fragroundup(chmp, chfs_blkoff(chmp, (ip)->size))))
601
602 /* calculates roundup(size, chmp->chm_chm_fs_bsize) */
603 #define chfs_blkroundup(chmp, size) \
604 (((size) + (chmp)->chm_fs_qbmask) & (chmp)->chm_fs_bmask)
605
606 /* from ffs read */
607 int
608 chfs_read(void *v)
609 {
610 struct vop_read_args /* {
611 struct vnode *a_vp;
612 struct uio *a_uio;
613 int a_ioflag;
614 kauth_cred_t a_cred;
615 } */ *ap = v;
616 struct vnode *vp;
617 struct chfs_inode *ip;
618 struct uio *uio;
619 struct ufsmount *ump;
620 struct buf *bp;
621 struct chfs_mount *chmp;
622 daddr_t lbn, nextlbn;
623 off_t bytesinfile;
624 long size, xfersize, blkoffset;
625 int error, ioflag;
626 vsize_t bytelen;
627 bool usepc = false;
628
629 dbg("chfs_read\n");
630
631 vp = ap->a_vp;
632 ip = VTOI(vp);
633 ump = ip->ump;
634 uio = ap->a_uio;
635 ioflag = ap->a_ioflag;
636 error = 0;
637
638 dbg("ip->size:%llu\n", (unsigned long long)ip->size);
639
640 #ifdef DIAGNOSTIC
641 if (uio->uio_rw != UIO_READ)
642 panic("%s: mode", READ_S);
643
644 if (vp->v_type == VLNK) {
645 if (ip->size < ump->um_maxsymlinklen)
646 panic("%s: short symlink", READ_S);
647 } else if (vp->v_type != VREG && vp->v_type != VDIR)
648 panic("%s: type %d", READ_S, vp->v_type);
649 #endif
650 chmp = ip->chmp;
651 if ((u_int64_t)uio->uio_offset > ump->um_maxfilesize)
652 return (EFBIG);
653 if (uio->uio_resid == 0)
654 return (0);
655
656 fstrans_start(vp->v_mount, FSTRANS_SHARED);
657
658 if (uio->uio_offset >= ip->size)
659 goto out;
660
661 usepc = vp->v_type == VREG;
662 bytelen = 0;
663 if (usepc) {
664 const int advice = IO_ADV_DECODE(ap->a_ioflag);
665
666 while (uio->uio_resid > 0) {
667 if (ioflag & IO_DIRECT) {
668 genfs_directio(vp, uio, ioflag);
669 }
670 bytelen = MIN(ip->size - uio->uio_offset,
671 uio->uio_resid);
672 if (bytelen == 0)
673 break;
674 error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
675 UBC_READ | UBC_PARTIALOK |
676 (UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0));
677 if (error)
678 break;
679
680 }
681 goto out;
682 }
683
684 dbg("start reading\n");
685 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
686 bytesinfile = ip->size - uio->uio_offset;
687 if (bytesinfile <= 0)
688 break;
689 lbn = chfs_lblkno(chmp, uio->uio_offset);
690 nextlbn = lbn + 1;
691 size = chfs_blksize(chmp, ip, lbn);
692 blkoffset = chfs_blkoff(chmp, uio->uio_offset);
693 xfersize = MIN(MIN(chmp->chm_fs_bsize - blkoffset, uio->uio_resid),
694 bytesinfile);
695
696 if (chfs_lblktosize(chmp, nextlbn) >= ip->size) {
697 error = bread(vp, lbn, size, NOCRED, 0, &bp);
698 dbg("after bread\n");
699 } else {
700 int nextsize = chfs_blksize(chmp, ip, nextlbn);
701 dbg("size: %ld\n", size);
702 error = breadn(vp, lbn,
703 size, &nextlbn, &nextsize, 1, 0, &bp);
704 dbg("after breadN\n");
705 }
706 if (error)
707 break;
708
709 /*
710 * We should only get non-zero b_resid when an I/O error
711 * has occurred, which should cause us to break above.
712 * However, if the short read did not cause an error,
713 * then we want to ensure that we do not uiomove bad
714 * or uninitialized data.
715 */
716 size -= bp->b_resid;
717 if (size < xfersize) {
718 if (size == 0)
719 break;
720 xfersize = size;
721 }
722 dbg("uiomove\n");
723 error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
724 if (error)
725 break;
726 brelse(bp, 0);
727 }
728
729 if (bp != NULL)
730 brelse(bp, 0);
731
732 out:
733 // FIXME HACK
734 ip->ino = ip->chvc->vno;
735
736 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
737 ip->iflag |= IN_ACCESS;
738 if ((ap->a_ioflag & IO_SYNC) == IO_SYNC) {
739 if (error) {
740 fstrans_done(vp->v_mount);
741 return error;
742 }
743 error = chfs_update(vp, NULL, NULL, UPDATE_WAIT);
744 }
745 }
746
747 dbg("[END]\n");
748 fstrans_done(vp->v_mount);
749
750 return (error);
751 }
752
753
754 /* --------------------------------------------------------------------- */
755
756 /* from ffs write */
757 int
758 chfs_write(void *v)
759 {
760 struct vop_write_args /* {
761 struct vnode *a_vp;
762 struct uio *a_uio;
763 int a_ioflag;
764 kauth_cred_t a_cred;
765 } */ *ap = v;
766 struct vnode *vp ;
767 struct uio *uio;
768 struct chfs_inode *ip;
769 struct chfs_mount *chmp;
770 struct lwp *l;
771 kauth_cred_t cred;
772 off_t osize, origoff, oldoff, preallocoff, endallocoff, nsize;
773 int blkoffset, error, flags, ioflag, resid;
774 int aflag;
775 int extended=0;
776 vsize_t bytelen;
777 bool async;
778 struct ufsmount *ump;
779
780
781 cred = ap->a_cred;
782 ioflag = ap->a_ioflag;
783 uio = ap->a_uio;
784 vp = ap->a_vp;
785 ip = VTOI(vp);
786 ump = ip->ump;
787
788 dbg("write\n");
789
790 KASSERT(vp->v_size == ip->size);
791
792 switch (vp->v_type) {
793 case VREG:
794 if (ioflag & IO_APPEND)
795 uio->uio_offset = ip->size;
796 if ((ip->flags & APPEND) && uio->uio_offset != ip->size)
797 return (EPERM);
798 /* FALLTHROUGH */
799 case VLNK:
800 break;
801 case VDIR:
802 if ((ioflag & IO_SYNC) == 0)
803 panic("chfs_write: nonsync dir write");
804 break;
805 default:
806 panic("chfs_write: type");
807 }
808
809 chmp = ip->chmp;
810 if (uio->uio_offset < 0 ||
811 (u_int64_t)uio->uio_offset +
812 uio->uio_resid > ump->um_maxfilesize) {
813 dbg("uio->uio_offset = %lld | uio->uio_offset + "
814 "uio->uio_resid (%llu) > ump->um_maxfilesize (%lld)\n",
815 (long long)uio->uio_offset,
816 (uint64_t)uio->uio_offset + uio->uio_resid,
817 (long long)ump->um_maxfilesize);
818 return (EFBIG);
819 }
820 /*
821 * Maybe this should be above the vnode op call, but so long as
822 * file servers have no limits, I don't think it matters.
823 */
824 l = curlwp;
825 if (vp->v_type == VREG && l &&
826 uio->uio_offset + uio->uio_resid >
827 l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
828 mutex_enter(proc_lock);
829 psignal(l->l_proc, SIGXFSZ);
830 mutex_exit(proc_lock);
831 return (EFBIG);
832 }
833 if (uio->uio_resid == 0)
834 return (0);
835
836 fstrans_start(vp->v_mount, FSTRANS_SHARED);
837
838 flags = ioflag & IO_SYNC ? B_SYNC : 0;
839 async = vp->v_mount->mnt_flag & MNT_ASYNC;
840 origoff = uio->uio_offset;
841 resid = uio->uio_resid;
842 osize = ip->size;
843 error = 0;
844
845 preallocoff = round_page(chfs_blkroundup(chmp,
846 MAX(osize, uio->uio_offset)));
847 aflag = ioflag & IO_SYNC ? B_SYNC : 0;
848 nsize = MAX(osize, uio->uio_offset + uio->uio_resid);
849 endallocoff = nsize - chfs_blkoff(chmp, nsize);
850
851 /*
852 * if we're increasing the file size, deal with expanding
853 * the fragment if there is one.
854 */
855
856 if (nsize > osize && chfs_lblkno(chmp, osize) < UFS_NDADDR &&
857 chfs_lblkno(chmp, osize) != chfs_lblkno(chmp, nsize) &&
858 chfs_blkroundup(chmp, osize) != osize) {
859 off_t eob;
860
861 eob = chfs_blkroundup(chmp, osize);
862 uvm_vnp_setwritesize(vp, eob);
863 error = ufs_balloc_range(vp, osize, eob - osize, cred, aflag);
864 if (error)
865 goto out;
866 if (flags & B_SYNC) {
867 mutex_enter(vp->v_interlock);
868 VOP_PUTPAGES(vp,
869 trunc_page(osize & chmp->chm_fs_bmask),
870 round_page(eob),
871 PGO_CLEANIT | PGO_SYNCIO | PGO_JOURNALLOCKED);
872 }
873 }
874
875 while (uio->uio_resid > 0) {
876 int ubc_flags = UBC_WRITE;
877 bool overwrite; /* if we're overwrite a whole block */
878 off_t newoff;
879
880 if (ioflag & IO_DIRECT) {
881 genfs_directio(vp, uio, ioflag | IO_JOURNALLOCKED);
882 }
883
884 oldoff = uio->uio_offset;
885 blkoffset = chfs_blkoff(chmp, uio->uio_offset);
886 bytelen = MIN(chmp->chm_fs_bsize - blkoffset, uio->uio_resid);
887 if (bytelen == 0) {
888 break;
889 }
890
891 /*
892 * if we're filling in a hole, allocate the blocks now and
893 * initialize the pages first. if we're extending the file,
894 * we can safely allocate blocks without initializing pages
895 * since the new blocks will be inaccessible until the write
896 * is complete.
897 */
898 overwrite = uio->uio_offset >= preallocoff &&
899 uio->uio_offset < endallocoff;
900 if (!overwrite && (vp->v_vflag & VV_MAPPED) == 0 &&
901 chfs_blkoff(chmp, uio->uio_offset) == 0 &&
902 (uio->uio_offset & PAGE_MASK) == 0) {
903 vsize_t len;
904
905 len = trunc_page(bytelen);
906 len -= chfs_blkoff(chmp, len);
907 if (len > 0) {
908 overwrite = true;
909 bytelen = len;
910 }
911 }
912
913 newoff = oldoff + bytelen;
914 if (vp->v_size < newoff) {
915 uvm_vnp_setwritesize(vp, newoff);
916 }
917
918 if (!overwrite) {
919 error = ufs_balloc_range(vp, uio->uio_offset, bytelen,
920 cred, aflag);
921 if (error)
922 break;
923 } else {
924 genfs_node_wrlock(vp);
925 error = GOP_ALLOC(vp, uio->uio_offset, bytelen,
926 aflag, cred);
927 genfs_node_unlock(vp);
928 if (error)
929 break;
930 ubc_flags |= UBC_FAULTBUSY;
931 }
932
933 /*
934 * copy the data.
935 */
936
937 ubc_flags |= UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
938 error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
939 IO_ADV_DECODE(ioflag), ubc_flags);
940
941 /*
942 * update UVM's notion of the size now that we've
943 * copied the data into the vnode's pages.
944 *
945 * we should update the size even when uiomove failed.
946 */
947
948 if (vp->v_size < newoff) {
949 uvm_vnp_setsize(vp, newoff);
950 extended = 1;
951 }
952
953 if (error)
954 break;
955
956 /*
957 * flush what we just wrote if necessary.
958 * XXXUBC simplistic async flushing.
959 */
960
961 if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
962 mutex_enter(vp->v_interlock);
963 error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
964 (uio->uio_offset >> 16) << 16,
965 PGO_CLEANIT | PGO_JOURNALLOCKED);
966 if (error)
967 break;
968 }
969 }
970 out:
971 if (error == 0 && ioflag & IO_SYNC) {
972 mutex_enter(vp->v_interlock);
973 error = VOP_PUTPAGES(vp,
974 trunc_page(origoff & chmp->chm_fs_bmask),
975 round_page(chfs_blkroundup(chmp, uio->uio_offset)),
976 PGO_CLEANIT | PGO_SYNCIO | PGO_JOURNALLOCKED);
977 }
978 ip->iflag |= IN_CHANGE | IN_UPDATE;
979 if (resid > uio->uio_resid && ap->a_cred) {
980 if (ip->mode & ISUID) {
981 if (kauth_authorize_vnode(ap->a_cred,
982 KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0)
983 ip->mode &= ~ISUID;
984 }
985
986 if (ip->mode & ISGID) {
987 if (kauth_authorize_vnode(ap->a_cred,
988 KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0)
989 ip->mode &= ~ISGID;
990 }
991 }
992 if (resid > uio->uio_resid)
993 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
994 if (error) {
995 (void) UFS_TRUNCATE(vp, osize, ioflag & IO_SYNC, ap->a_cred);
996 uio->uio_offset -= resid - uio->uio_resid;
997 uio->uio_resid = resid;
998 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
999 error = UFS_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
1000
1001 //FIXME HACK
1002 chfs_set_vnode_size(vp, vp->v_size);
1003
1004
1005 KASSERT(vp->v_size == ip->size);
1006 fstrans_done(vp->v_mount);
1007
1008 mutex_enter(&chmp->chm_lock_mountfields);
1009 error = chfs_write_flash_vnode(chmp, ip, ALLOC_NORMAL);
1010 mutex_exit(&chmp->chm_lock_mountfields);
1011
1012 return (error);
1013 }
1014
1015
1016 /* --------------------------------------------------------------------- */
1017
1018 int
1019 chfs_fsync(void *v)
1020 {
1021 struct vop_fsync_args /* {
1022 struct vnode *a_vp;
1023 kauth_cred_t a_cred;
1024 int a_flags;
1025 off_t offlo;
1026 off_t offhi;
1027 } */ *ap = v;
1028 struct vnode *vp = ap->a_vp;
1029
1030 if (ap->a_flags & FSYNC_CACHE) {
1031 return ENODEV;
1032 }
1033 vflushbuf(vp, ap->a_flags);
1034
1035 return 0;
1036 }
1037
1038 /* --------------------------------------------------------------------- */
1039
1040 int
1041 chfs_remove(void *v)
1042 {
1043 struct vnode *dvp = ((struct vop_remove_args *) v)->a_dvp;
1044 struct vnode *vp = ((struct vop_remove_args *) v)->a_vp;
1045 struct componentname *cnp = (((struct vop_remove_args *) v)->a_cnp);
1046 dbg("remove\n");
1047
1048 KASSERT(VOP_ISLOCKED(dvp));
1049 KASSERT(VOP_ISLOCKED(vp));
1050
1051 struct chfs_inode *ip = VTOI(vp);
1052 struct chfs_inode *parent = VTOI(dvp);
1053 int error = 0;
1054
1055 if (vp->v_type == VDIR || (ip->flags & (IMMUTABLE | APPEND)) ||
1056 (parent->flags & APPEND)) {
1057 error = EPERM;
1058 goto out;
1059 }
1060
1061 KASSERT(ip->chvc->vno != ip->chvc->pvno);
1062
1063 error = chfs_do_unlink(ip,
1064 parent, cnp->cn_nameptr, cnp->cn_namelen);
1065
1066 out:
1067 vput(dvp);
1068 vput(vp);
1069
1070 return error;
1071 }
1072
1073 /* --------------------------------------------------------------------- */
1074
1075 int
1076 chfs_link(void *v)
1077 {
1078 struct vnode *dvp = ((struct vop_link_args *) v)->a_dvp;
1079 struct vnode *vp = ((struct vop_link_args *) v)->a_vp;
1080 struct componentname *cnp = ((struct vop_link_args *) v)->a_cnp;
1081
1082 struct chfs_inode *ip, *parent;
1083 int error = 0;
1084
1085 if (vp->v_type == VDIR) {
1086 VOP_ABORTOP(dvp, cnp);
1087 error = EISDIR;
1088 goto out;
1089 }
1090 if (dvp->v_mount != vp->v_mount) {
1091 VOP_ABORTOP(dvp, cnp);
1092 error = EXDEV;
1093 goto out;
1094 }
1095 if (dvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE))) {
1096 VOP_ABORTOP(dvp, cnp);
1097 goto out;
1098 }
1099
1100 parent = VTOI(dvp);
1101 ip = VTOI(vp);
1102
1103 error = chfs_do_link(ip,
1104 parent, cnp->cn_nameptr, cnp->cn_namelen, ip->ch_type);
1105
1106 if (dvp != vp)
1107 VOP_UNLOCK(vp);
1108 out:
1109 vput(dvp);
1110 return error;
1111 }
1112
1113 /* --------------------------------------------------------------------- */
1114
1115 int
1116 chfs_rename(void *v)
1117 {
1118 struct vnode *fdvp = ((struct vop_rename_args *) v)->a_fdvp;
1119 struct vnode *fvp = ((struct vop_rename_args *) v)->a_fvp;
1120 struct componentname *fcnp = ((struct vop_rename_args *) v)->a_fcnp;
1121 struct vnode *tdvp = ((struct vop_rename_args *) v)->a_tdvp;
1122 struct vnode *tvp = ((struct vop_rename_args *) v)->a_tvp;
1123 struct componentname *tcnp = ((struct vop_rename_args *) v)->a_tcnp;
1124
1125 struct chfs_inode *oldparent, *old;
1126 struct chfs_inode *newparent;
1127 struct chfs_dirent *fd;
1128 struct chfs_inode *ip;
1129 int error = 0;
1130 dbg("rename\n");
1131
1132 KASSERT(VOP_ISLOCKED(tdvp));
1133 KASSERT(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
1134
1135 oldparent = VTOI(fdvp);
1136 old = VTOI(fvp);
1137 newparent = VTOI(tdvp);
1138 if (tvp) {
1139 dbg("tvp not null\n");
1140 ip = VTOI(tvp);
1141 if (tvp->v_type == VDIR) {
1142 TAILQ_FOREACH(fd, &ip->dents, fds) {
1143 if (fd->vno) {
1144 error = ENOTEMPTY;
1145 goto out_unlocked;
1146 }
1147 }
1148 }
1149 error = chfs_do_unlink(ip,
1150 newparent, tcnp->cn_nameptr, tcnp->cn_namelen);
1151 vput(tvp);
1152 }
1153 VFS_VGET(tdvp->v_mount, old->ino, &tvp);
1154 ip = VTOI(tvp);
1155
1156 /* link new */
1157 error = chfs_do_link(ip,
1158 newparent, tcnp->cn_nameptr, tcnp->cn_namelen, ip->ch_type);
1159 /* remove old */
1160 error = chfs_do_unlink(old,
1161 oldparent, fcnp->cn_nameptr, fcnp->cn_namelen);
1162
1163 out_unlocked:
1164 /* Release target nodes. */
1165 if (tdvp == tvp)
1166 vrele(tdvp);
1167 else
1168 vput(tdvp);
1169 if (tvp != NULL)
1170 vput(tvp);
1171
1172 /* Release source nodes. */
1173 vrele(fdvp);
1174 vrele(fvp);
1175
1176 return error;
1177 }
1178
1179 /* --------------------------------------------------------------------- */
1180
1181 int
1182 chfs_mkdir(void *v)
1183 {
1184 struct vnode *dvp = ((struct vop_mkdir_v3_args *) v)->a_dvp;
1185 struct vnode **vpp = ((struct vop_mkdir_v3_args *)v)->a_vpp;
1186 struct componentname *cnp = ((struct vop_mkdir_v3_args *) v)->a_cnp;
1187 struct vattr *vap = ((struct vop_mkdir_v3_args *) v)->a_vap;
1188 dbg("mkdir()\n");
1189
1190 int mode;
1191
1192 mode = vap->va_mode & ACCESSPERMS;
1193 if ((mode & IFMT) == 0) {
1194 mode |= IFDIR;
1195 }
1196
1197 KASSERT(vap->va_type == VDIR);
1198
1199 return chfs_makeinode(mode, dvp, vpp, cnp, VDIR);
1200 }
1201
1202 /* --------------------------------------------------------------------- */
1203
1204 int
1205 chfs_rmdir(void *v)
1206 {
1207 struct vnode *dvp = ((struct vop_rmdir_args *) v)->a_dvp;
1208 struct vnode *vp = ((struct vop_rmdir_args *) v)->a_vp;
1209 struct componentname *cnp = ((struct vop_rmdir_args *) v)->a_cnp;
1210 dbg("rmdir()\n");
1211
1212 KASSERT(VOP_ISLOCKED(dvp));
1213 KASSERT(VOP_ISLOCKED(vp));
1214
1215 struct chfs_inode *ip = VTOI(vp);
1216 struct chfs_inode *parent = VTOI(dvp);
1217 struct chfs_dirent *fd;
1218 int error = 0;
1219
1220 if (vp->v_type != VDIR) {
1221 error = ENOTDIR;
1222 goto out;
1223 }
1224
1225 KASSERT(ip->chvc->vno != ip->chvc->pvno);
1226
1227 TAILQ_FOREACH(fd, &ip->dents, fds) {
1228 if (fd->vno) {
1229 error = ENOTEMPTY;
1230 goto out;
1231 }
1232 }
1233
1234 error = chfs_do_unlink(ip,
1235 parent, cnp->cn_nameptr, cnp->cn_namelen);
1236
1237 out:
1238 vput(dvp);
1239 vput(vp);
1240
1241 return error;
1242 }
1243
1244 /* --------------------------------------------------------------------- */
1245
1246 int
1247 chfs_symlink(void *v)
1248 {
1249 struct vnode *dvp = ((struct vop_symlink_v3_args *) v)->a_dvp;
1250 struct vnode **vpp = ((struct vop_symlink_v3_args *) v)->a_vpp;
1251 struct componentname *cnp = ((struct vop_symlink_v3_args *) v)->a_cnp;
1252 struct vattr *vap = ((struct vop_symlink_v3_args *) v)->a_vap;
1253 char *target = ((struct vop_symlink_v3_args *) v)->a_target;
1254
1255 struct ufsmount *ump;
1256 struct chfs_mount *chmp;
1257 struct vnode *vp;
1258 struct chfs_inode *ip;
1259 int len, err;
1260 struct chfs_full_dnode *fd;
1261 struct buf *bp;
1262 dbg("symlink()\n");
1263
1264 ump = VFSTOUFS(dvp->v_mount);
1265 chmp = ump->um_chfs;
1266
1267 err = chfs_makeinode(IFLNK | vap->va_mode, dvp, vpp, cnp, VLNK);
1268 if (err)
1269 return (err);
1270 VN_KNOTE(dvp, NOTE_WRITE);
1271 vp = *vpp;
1272 len = strlen(target);
1273 ip = VTOI(vp);
1274 /* TODO max symlink len instead of "100" */
1275 if (len < 100) {
1276 /* symlink path stored as a data node */
1277 ip->target = kmem_alloc(len, KM_SLEEP);
1278 memcpy(ip->target, target, len);
1279 chfs_set_vnode_size(vp, len);
1280 ip->iflag |= IN_CHANGE | IN_UPDATE;
1281
1282 bp = getiobuf(vp, true);
1283 bp->b_bufsize = bp->b_resid = len;
1284 bp->b_data = kmem_alloc(len, KM_SLEEP);
1285 memcpy(bp->b_data, target, len);
1286 bp->b_blkno = 0;
1287
1288 fd = chfs_alloc_full_dnode();
1289
1290 mutex_enter(&chmp->chm_lock_mountfields);
1291
1292 /* write out the data node */
1293 err = chfs_write_flash_dnode(chmp, vp, bp, fd);
1294 if (err) {
1295 mutex_exit(&chmp->chm_lock_mountfields);
1296 goto out;
1297 }
1298
1299 /* add it to the inode */
1300 err = chfs_add_full_dnode_to_inode(chmp, ip, fd);
1301 if (err) {
1302 mutex_exit(&chmp->chm_lock_mountfields);
1303 goto out;
1304 }
1305
1306 mutex_exit(&chmp->chm_lock_mountfields);
1307
1308 kmem_free(bp->b_data, len);
1309 putiobuf(bp);
1310
1311 uvm_vnp_setsize(vp, len);
1312 } else {
1313 err = ufs_bufio(UIO_WRITE, vp, target, len, (off_t)0,
1314 IO_NODELOCKED, cnp->cn_cred, (size_t *)0, NULL);
1315 }
1316
1317 out:
1318 if (err)
1319 vrele(vp);
1320
1321 return (err);
1322 }
1323
1324 /* --------------------------------------------------------------------- */
1325
1326 int
1327 chfs_readdir(void *v)
1328 {
1329 struct vnode *vp = ((struct vop_readdir_args *) v)->a_vp;
1330 struct uio *uio = ((struct vop_readdir_args *) v)->a_uio;
1331 int *eofflag = ((struct vop_readdir_args *) v)->a_eofflag;
1332
1333 int error = 0;
1334 off_t skip, offset;
1335 struct chfs_inode *ip;
1336 struct chfs_dirent *fd;
1337
1338 struct ufsmount *ump;
1339 struct chfs_mount *chmp;
1340 struct chfs_vnode_cache *chvc;
1341
1342 KASSERT(VOP_ISLOCKED(vp));
1343
1344 /* This operation only makes sense on directory nodes. */
1345 if (vp->v_type != VDIR) {
1346 error = ENOTDIR;
1347 goto out;
1348 }
1349
1350 ip = VTOI(vp);
1351
1352 /* uiomove in chfs_filldir automatically increments the
1353 * uio_offset by an arbitrary size, so we discard any change
1354 * to uio_offset and set it to our own value on return
1355 */
1356 offset = uio->uio_offset;
1357
1358 /* Add this entry. */
1359 if (offset == CHFS_OFFSET_DOT) {
1360 error = chfs_filldir(uio, ip->ino, ".", 1, CHT_DIR);
1361 if (error == -1) {
1362 error = 0;
1363 goto outok;
1364 } else if (error != 0)
1365 goto outok;
1366
1367 offset = CHFS_OFFSET_DOTDOT;
1368 }
1369
1370 /* Add parent entry. */
1371 if (offset == CHFS_OFFSET_DOTDOT) {
1372 ump = VFSTOUFS(vp->v_mount);
1373 chmp = ump->um_chfs;
1374 mutex_enter(&chmp->chm_lock_vnocache);
1375 chvc = chfs_vnode_cache_get(chmp, ip->ino);
1376 mutex_exit(&chmp->chm_lock_vnocache);
1377
1378 error = chfs_filldir(uio, chvc->pvno, "..", 2, CHT_DIR);
1379 if (error == -1) {
1380 error = 0;
1381 goto outok;
1382 } else if (error != 0) {
1383 goto outok;
1384 }
1385
1386 /* Has child or not? */
1387 if (TAILQ_EMPTY(&ip->dents)) {
1388 offset = CHFS_OFFSET_EOF;
1389 } else {
1390 offset = CHFS_OFFSET_FIRST;
1391 }
1392 }
1393
1394 if (offset != CHFS_OFFSET_EOF) {
1395 /* Child entries. */
1396 skip = offset - CHFS_OFFSET_FIRST;
1397
1398 TAILQ_FOREACH(fd, &ip->dents, fds) {
1399 /* seek to offset by skipping items */
1400 /* XXX race conditions by changed dirent? */
1401 if (skip > 0) {
1402 skip--;
1403 continue;
1404 }
1405
1406 if (fd->vno != 0) {
1407 error = chfs_filldir(uio, fd->vno,
1408 fd->name, fd->nsize, fd->type);
1409 if (error == -1) {
1410 error = 0;
1411 goto outok;
1412 } else if (error != 0) {
1413 dbg("err %d\n", error);
1414 goto outok;
1415 }
1416 }
1417 offset++;
1418 }
1419 }
1420 offset = CHFS_OFFSET_EOF;
1421
1422 outok:
1423 uio->uio_offset = offset;
1424
1425 if (eofflag != NULL) {
1426 *eofflag = (error == 0 &&
1427 uio->uio_offset == CHFS_OFFSET_EOF);
1428 }
1429
1430 out:
1431 KASSERT(VOP_ISLOCKED(vp));
1432
1433 return error;
1434 }
1435
1436 /* --------------------------------------------------------------------- */
1437
1438 int
1439 chfs_readlink(void *v)
1440 {
1441
1442 struct vnode *vp = ((struct vop_readlink_args *) v)->a_vp;
1443 struct uio *uio = ((struct vop_readlink_args *) v)->a_uio;
1444 kauth_cred_t cred = ((struct vop_readlink_args *) v)->a_cred;
1445
1446 struct chfs_inode *ip = VTOI(vp);
1447
1448 dbg("readlink()\n");
1449
1450 /* TODO max symlink len instead of "100" */
1451 if (ip->size < 100) {
1452 uiomove(ip->target, ip->size, uio);
1453 return (0);
1454 }
1455
1456 return (UFS_BUFRD(vp, uio, 0, cred));
1457 }
1458
1459 /* --------------------------------------------------------------------- */
1460
1461 int
1462 chfs_inactive(void *v)
1463 {
1464 struct vnode *vp = ((struct vop_inactive_args *) v)->a_vp;
1465 struct chfs_inode *ip = VTOI(vp);
1466 struct chfs_vnode_cache *chvc;
1467 dbg("inactive | vno: %llu\n", (unsigned long long)ip->ino);
1468
1469 KASSERT(VOP_ISLOCKED(vp));
1470
1471 /* Reclaim only if there is no link to the node. */
1472 if (ip->ino) {
1473 chvc = ip->chvc;
1474 if (chvc->nlink)
1475 *((struct vop_inactive_args *) v)->a_recycle = 0;
1476 } else {
1477 *((struct vop_inactive_args *) v)->a_recycle = 1;
1478 }
1479
1480 VOP_UNLOCK(vp);
1481
1482 return 0;
1483 }
1484
1485 /* --------------------------------------------------------------------- */
1486
1487 int
1488 chfs_reclaim(void *v)
1489 {
1490 struct vop_reclaim_args *ap = v;
1491 struct vnode *vp = ap->a_vp;
1492 struct chfs_inode *ip = VTOI(vp);
1493 struct chfs_mount *chmp = ip->chmp;
1494 struct chfs_dirent *fd;
1495
1496 mutex_enter(&chmp->chm_lock_mountfields);
1497
1498 mutex_enter(&chmp->chm_lock_vnocache);
1499 ip->chvc->state = VNO_STATE_CHECKEDABSENT;
1500 mutex_exit(&chmp->chm_lock_vnocache);
1501
1502 chfs_update(vp, NULL, NULL, UPDATE_CLOSE);
1503
1504 /* Clean fragments. */
1505 chfs_kill_fragtree(chmp, &ip->fragtree);
1506
1507 /* Clean dirents. */
1508 fd = TAILQ_FIRST(&ip->dents);
1509 while (fd) {
1510 TAILQ_REMOVE(&ip->dents, fd, fds);
1511 chfs_free_dirent(fd);
1512 fd = TAILQ_FIRST(&ip->dents);
1513 }
1514
1515 cache_purge(vp);
1516 vcache_remove(vp->v_mount, &ip->ino, sizeof(ip->ino));
1517 if (ip->devvp) {
1518 vrele(ip->devvp);
1519 ip->devvp = 0;
1520 }
1521
1522 genfs_node_destroy(vp);
1523 pool_put(&chfs_inode_pool, vp->v_data);
1524 vp->v_data = NULL;
1525
1526 mutex_exit(&chmp->chm_lock_mountfields);
1527
1528 return (0);
1529 }
1530
1531 /* --------------------------------------------------------------------- */
1532
1533 int
1534 chfs_advlock(void *v)
1535 {
1536 return 0;
1537 }
1538
1539 /* --------------------------------------------------------------------- */
1540 int
1541 chfs_strategy(void *v)
1542 {
1543 struct vop_strategy_args /* {
1544 const struct vnodeop_desc *a_desc;
1545 struct vnode *a_vp;
1546 struct buf *a_bp;
1547 } */ *ap = v;
1548 struct chfs_full_dnode *fd;
1549 struct buf *bp = ap->a_bp;
1550 struct vnode *vp = ap->a_vp;
1551 struct chfs_inode *ip = VTOI(vp);
1552 struct chfs_mount *chmp = ip->chmp;
1553 int read = (bp->b_flags & B_READ) ? 1 : 0;
1554 int err = 0;
1555
1556 if (read) {
1557 err = chfs_read_data(chmp, vp, bp);
1558 } else {
1559 mutex_enter(&chmp->chm_lock_mountfields);
1560
1561 fd = chfs_alloc_full_dnode();
1562
1563 err = chfs_write_flash_dnode(chmp, vp, bp, fd);
1564 if (err) {
1565 mutex_exit(&chmp->chm_lock_mountfields);
1566 goto out;
1567 }
1568
1569 ip = VTOI(vp);
1570 err = chfs_add_full_dnode_to_inode(chmp, ip, fd);
1571
1572 mutex_exit(&chmp->chm_lock_mountfields);
1573 }
1574 out:
1575 biodone(bp);
1576 return err;
1577 }
1578
1579 int
1580 chfs_bmap(void *v)
1581 {
1582 struct vop_bmap_args /* {
1583 struct vnode *a_vp;
1584 daddr_t a_bn;
1585 struct vnode **a_vpp;
1586 daddr_t *a_bnp;
1587 int *a_runp;
1588 int *a_runb;
1589 } */ *ap = v;
1590 if (ap->a_vpp != NULL)
1591 *ap->a_vpp = ap->a_vp;
1592 if (ap->a_bnp != NULL)
1593 *ap->a_bnp = ap->a_bn;
1594 if (ap->a_runp != NULL)
1595 *ap->a_runp = 0;
1596 return (0);
1597 }
1598
1599 /*
1600 * vnode operations vector used for files stored in a chfs file system.
1601 */
1602 int
1603 (**chfs_vnodeop_p)(void *);
1604 const struct vnodeopv_entry_desc chfs_vnodeop_entries[] =
1605 {
1606 { &vop_default_desc, vn_default_error },
1607 { &vop_lookup_desc, chfs_lookup },
1608 { &vop_create_desc, chfs_create },
1609 { &vop_mknod_desc, chfs_mknod },
1610 { &vop_open_desc, chfs_open },
1611 { &vop_close_desc, chfs_close },
1612 { &vop_access_desc, chfs_access },
1613 { &vop_getattr_desc, chfs_getattr },
1614 { &vop_setattr_desc, chfs_setattr },
1615 { &vop_read_desc, chfs_read },
1616 { &vop_write_desc, chfs_write },
1617 { &vop_fallocate_desc, genfs_eopnotsupp },
1618 { &vop_fdiscard_desc, genfs_eopnotsupp },
1619 { &vop_ioctl_desc, genfs_enoioctl },
1620 { &vop_fcntl_desc, genfs_fcntl },
1621 { &vop_poll_desc, genfs_poll },
1622 { &vop_kqfilter_desc, genfs_kqfilter },
1623 { &vop_revoke_desc, genfs_revoke },
1624 { &vop_mmap_desc, genfs_mmap },
1625 { &vop_fsync_desc, chfs_fsync },
1626 { &vop_seek_desc, genfs_seek },
1627 { &vop_remove_desc, chfs_remove },
1628 { &vop_link_desc, chfs_link },
1629 { &vop_rename_desc, chfs_rename },
1630 { &vop_mkdir_desc, chfs_mkdir },
1631 { &vop_rmdir_desc, chfs_rmdir },
1632 { &vop_symlink_desc, chfs_symlink },
1633 { &vop_readdir_desc, chfs_readdir },
1634 { &vop_readlink_desc, chfs_readlink },
1635 { &vop_abortop_desc, genfs_abortop },
1636 { &vop_inactive_desc, chfs_inactive },
1637 { &vop_reclaim_desc, chfs_reclaim },
1638 { &vop_lock_desc, genfs_lock },
1639 { &vop_unlock_desc, genfs_unlock },
1640 { &vop_bmap_desc, chfs_bmap },
1641 { &vop_strategy_desc, chfs_strategy },
1642 { &vop_print_desc, ufs_print },
1643 { &vop_pathconf_desc, ufs_pathconf },
1644 { &vop_islocked_desc, genfs_islocked },
1645 { &vop_advlock_desc, chfs_advlock },
1646 { &vop_bwrite_desc, vn_bwrite },
1647 { &vop_getpages_desc, genfs_getpages },
1648 { &vop_putpages_desc, genfs_putpages },
1649 { NULL, NULL } };
1650
1651 const struct vnodeopv_desc chfs_vnodeop_opv_desc =
1652 { &chfs_vnodeop_p, chfs_vnodeop_entries };
1653
1654 /* --------------------------------------------------------------------- */
1655
1656 /*
1657 * vnode operations vector used for special devices stored in a chfs
1658 * file system.
1659 */
1660 int
1661 (**chfs_specop_p)(void *);
1662 const struct vnodeopv_entry_desc chfs_specop_entries[] =
1663 {
1664 { &vop_default_desc, vn_default_error },
1665 { &vop_lookup_desc, spec_lookup },
1666 { &vop_create_desc, spec_create },
1667 { &vop_mknod_desc, spec_mknod },
1668 { &vop_open_desc, spec_open },
1669 { &vop_close_desc, ufsspec_close },
1670 { &vop_access_desc, chfs_access },
1671 { &vop_getattr_desc, chfs_getattr },
1672 { &vop_setattr_desc, chfs_setattr },
1673 { &vop_read_desc, chfs_read },
1674 { &vop_write_desc, chfs_write },
1675 { &vop_fallocate_desc, spec_fallocate },
1676 { &vop_fdiscard_desc, spec_fdiscard },
1677 { &vop_ioctl_desc, spec_ioctl },
1678 { &vop_fcntl_desc, genfs_fcntl },
1679 { &vop_poll_desc, spec_poll },
1680 { &vop_kqfilter_desc, spec_kqfilter },
1681 { &vop_revoke_desc, spec_revoke },
1682 { &vop_mmap_desc, spec_mmap },
1683 { &vop_fsync_desc, spec_fsync },
1684 { &vop_seek_desc, spec_seek },
1685 { &vop_remove_desc, spec_remove },
1686 { &vop_link_desc, spec_link },
1687 { &vop_rename_desc, spec_rename },
1688 { &vop_mkdir_desc, spec_mkdir },
1689 { &vop_rmdir_desc, spec_rmdir },
1690 { &vop_symlink_desc, spec_symlink },
1691 { &vop_readdir_desc, spec_readdir },
1692 { &vop_readlink_desc, spec_readlink },
1693 { &vop_abortop_desc, spec_abortop },
1694 { &vop_inactive_desc, chfs_inactive },
1695 { &vop_reclaim_desc, chfs_reclaim },
1696 { &vop_lock_desc, genfs_lock },
1697 { &vop_unlock_desc, genfs_unlock },
1698 { &vop_bmap_desc, spec_bmap },
1699 { &vop_strategy_desc, spec_strategy },
1700 { &vop_print_desc, ufs_print },
1701 { &vop_pathconf_desc, spec_pathconf },
1702 { &vop_islocked_desc, genfs_islocked },
1703 { &vop_advlock_desc, spec_advlock },
1704 { &vop_bwrite_desc, vn_bwrite },
1705 { &vop_getpages_desc, spec_getpages },
1706 { &vop_putpages_desc, spec_putpages },
1707 { NULL, NULL } };
1708
1709 const struct vnodeopv_desc chfs_specop_opv_desc =
1710 { &chfs_specop_p, chfs_specop_entries };
1711
1712 /* --------------------------------------------------------------------- */
1713 /*
1714 * vnode operations vector used for fifos stored in a chfs file system.
1715 */
1716 int
1717 (**chfs_fifoop_p)(void *);
1718 const struct vnodeopv_entry_desc chfs_fifoop_entries[] =
1719 {
1720 { &vop_default_desc, vn_default_error },
1721 { &vop_lookup_desc, vn_fifo_bypass },
1722 { &vop_create_desc, vn_fifo_bypass },
1723 { &vop_mknod_desc, vn_fifo_bypass },
1724 { &vop_open_desc, vn_fifo_bypass },
1725 { &vop_close_desc, ufsfifo_close },
1726 { &vop_access_desc, chfs_access },
1727 { &vop_getattr_desc, chfs_getattr },
1728 { &vop_setattr_desc, chfs_setattr },
1729 { &vop_read_desc, ufsfifo_read },
1730 { &vop_write_desc, ufsfifo_write },
1731 { &vop_fallocate_desc, vn_fifo_bypass },
1732 { &vop_fdiscard_desc, vn_fifo_bypass },
1733 { &vop_ioctl_desc, vn_fifo_bypass },
1734 { &vop_fcntl_desc, genfs_fcntl },
1735 { &vop_poll_desc, vn_fifo_bypass },
1736 { &vop_kqfilter_desc, vn_fifo_bypass },
1737 { &vop_revoke_desc, vn_fifo_bypass },
1738 { &vop_mmap_desc, vn_fifo_bypass },
1739 { &vop_fsync_desc, vn_fifo_bypass },
1740 { &vop_seek_desc, vn_fifo_bypass },
1741 { &vop_remove_desc, vn_fifo_bypass },
1742 { &vop_link_desc, vn_fifo_bypass },
1743 { &vop_rename_desc, vn_fifo_bypass },
1744 { &vop_mkdir_desc, vn_fifo_bypass },
1745 { &vop_rmdir_desc, vn_fifo_bypass },
1746 { &vop_symlink_desc, vn_fifo_bypass },
1747 { &vop_readdir_desc, vn_fifo_bypass },
1748 { &vop_readlink_desc, vn_fifo_bypass },
1749 { &vop_abortop_desc, vn_fifo_bypass },
1750 { &vop_inactive_desc, chfs_inactive },
1751 { &vop_reclaim_desc, chfs_reclaim },
1752 { &vop_lock_desc, genfs_lock },
1753 { &vop_unlock_desc, genfs_unlock },
1754 { &vop_bmap_desc, vn_fifo_bypass },
1755 { &vop_strategy_desc, vn_fifo_bypass },
1756 { &vop_print_desc, ufs_print },
1757 { &vop_pathconf_desc, vn_fifo_bypass },
1758 { &vop_islocked_desc, genfs_islocked },
1759 { &vop_advlock_desc, vn_fifo_bypass },
1760 { &vop_bwrite_desc, genfs_nullop },
1761 { &vop_getpages_desc, genfs_badop },
1762 { &vop_putpages_desc, vn_fifo_bypass },
1763 { NULL, NULL } };
1764
1765 const struct vnodeopv_desc chfs_fifoop_opv_desc =
1766 { &chfs_fifoop_p, chfs_fifoop_entries };
1767