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