chfs_vnops.c revision 1.4 1 /* $NetBSD: chfs_vnops.c,v 1.4 2012/04/12 15:31:01 ttoth 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_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE
158 && (cnp->cn_flags & ISDOTDOT) == 0)
159 cache_enter(dvp, *vpp, cnp);
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 error = kauth_authorize_vnode(ap->a_cred, KAUTH_VNODE_RETAIN_SUID, vp,
973 NULL, EPERM);
974 if (error)
975 ip->mode &= ~ISUID;
976 }
977
978 if (ip->mode & ISGID) {
979 error = kauth_authorize_vnode(ap->a_cred, KAUTH_VNODE_RETAIN_SGID, vp,
980 NULL, EPERM);
981 if (error)
982 ip->mode &= ~ISGID;
983 }
984 }
985 if (resid > uio->uio_resid)
986 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
987 if (error) {
988 (void) UFS_TRUNCATE(vp, osize, ioflag & IO_SYNC, ap->a_cred);
989 uio->uio_offset -= resid - uio->uio_resid;
990 uio->uio_resid = resid;
991 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
992 error = UFS_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
993
994 //XXX hack, i write the next line after i know ip->i_size and vp->v_size don't equal
995 chfs_set_vnode_size(vp, vp->v_size);
996
997
998 //dbg("end file size (vp): %llu\n", (unsigned long long)vp->v_size);
999 //dbg("end file size (ip): %llu\n", (unsigned long long)ip->i_size);
1000 KASSERT(vp->v_size == ip->size);
1001 fstrans_done(vp->v_mount);
1002
1003 mutex_enter(&chmp->chm_lock_mountfields);
1004 error = chfs_write_flash_vnode(chmp, ip, ALLOC_NORMAL);
1005 mutex_exit(&chmp->chm_lock_mountfields);
1006
1007 //mutex_exit(&ip->inode_lock);
1008 //dbg("end\n");
1009 return (error);
1010 }
1011
1012
1013 /* --------------------------------------------------------------------- */
1014
1015 int
1016 chfs_fsync(void *v)
1017 {
1018 //dbg("fsync\n");
1019 struct vop_fsync_args /* {
1020 struct vnode *a_vp;
1021 kauth_cred_t a_cred;
1022 int a_flags;
1023 off_t offlo;
1024 off_t offhi;
1025 } */ *ap = v;
1026 struct vnode *vp = ap->a_vp;
1027 int wait;
1028
1029 if (ap->a_flags & FSYNC_CACHE) {
1030 return ENODEV;
1031 }
1032 wait = (ap->a_flags & FSYNC_WAIT) != 0;
1033 vflushbuf(vp, wait);
1034 //struct chfs_inode *ip = VTOI(vp);
1035 //chfs_set_vnode_size(vp, ip->write_size);
1036
1037 return 0;
1038 }
1039
1040 /* --------------------------------------------------------------------- */
1041
1042 int
1043 chfs_remove(void *v)
1044 {
1045 struct vnode *dvp = ((struct vop_remove_args *) v)->a_dvp;
1046 struct vnode *vp = ((struct vop_remove_args *) v)->a_vp;
1047 struct componentname *cnp = (((struct vop_remove_args *) v)->a_cnp);
1048 dbg("remove\n");
1049
1050 KASSERT(VOP_ISLOCKED(dvp));
1051 KASSERT(VOP_ISLOCKED(vp));
1052
1053 struct chfs_inode *ip = VTOI(vp);
1054 struct chfs_inode *parent = VTOI(dvp);
1055 int error = 0;
1056
1057 KASSERT(ip->chvc->vno != ip->chvc->pvno);
1058
1059 error = chfs_do_unlink(ip,
1060 parent, cnp->cn_nameptr, cnp->cn_namelen);
1061
1062 vput(dvp);
1063 vput(vp);
1064
1065 return error;
1066 }
1067
1068 /* --------------------------------------------------------------------- */
1069
1070 int
1071 chfs_link(void *v)
1072 {
1073 struct vnode *dvp = ((struct vop_link_args *) v)->a_dvp;
1074 struct vnode *vp = ((struct vop_link_args *) v)->a_vp;
1075 struct componentname *cnp = ((struct vop_link_args *) v)->a_cnp;
1076
1077 struct chfs_inode *ip, *parent;
1078 int error = 0;
1079
1080 if (vp->v_type == VDIR) {
1081 VOP_ABORTOP(dvp, cnp);
1082 error = EISDIR;
1083 goto out;
1084 }
1085 if (dvp->v_mount != vp->v_mount) {
1086 VOP_ABORTOP(dvp, cnp);
1087 error = EXDEV;
1088 goto out;
1089 }
1090 if (dvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE))) {
1091 VOP_ABORTOP(dvp, cnp);
1092 goto out;
1093 }
1094
1095 parent = VTOI(dvp);
1096 ip = VTOI(vp);
1097
1098 error = chfs_do_link(ip,
1099 parent, cnp->cn_nameptr, cnp->cn_namelen, ip->ch_type);
1100
1101 if (dvp != vp)
1102 VOP_UNLOCK(vp);
1103 out:
1104 vput(dvp);
1105 return error;
1106 }
1107
1108 /* --------------------------------------------------------------------- */
1109
1110 int
1111 chfs_rename(void *v)
1112 {
1113 struct vnode *fdvp = ((struct vop_rename_args *) v)->a_fdvp;
1114 struct vnode *fvp = ((struct vop_rename_args *) v)->a_fvp;
1115 struct componentname *fcnp = ((struct vop_rename_args *) v)->a_fcnp;
1116 struct vnode *tdvp = ((struct vop_rename_args *) v)->a_tdvp;
1117 struct vnode *tvp = ((struct vop_rename_args *) v)->a_tvp;
1118 struct componentname *tcnp = ((struct vop_rename_args *) v)->a_tcnp;
1119
1120 struct chfs_inode *oldparent, *old;
1121 struct chfs_inode *newparent;
1122 struct chfs_dirent *fd;//, *oldfd;
1123 struct chfs_inode *ip;
1124 int error = 0;
1125 dbg("rename\n");
1126
1127 KASSERT(VOP_ISLOCKED(tdvp));
1128 KASSERT(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
1129
1130 oldparent = VTOI(fdvp);
1131 old = VTOI(fvp);
1132 newparent = VTOI(tdvp);
1133 if (tvp) {
1134 dbg("tvp not null\n");
1135 ip = VTOI(tvp);
1136 if (tvp->v_type == VDIR) {
1137 //TODO: lock
1138 // fd = ip->dents;
1139 // while (fd) {
1140 TAILQ_FOREACH(fd, &ip->dents, fds) {
1141 if (fd->vno) {
1142 //TODO: unlock
1143 error = ENOTEMPTY;
1144 goto out_unlocked;
1145 }
1146 // fd = fd->next;
1147 }
1148 //TODO: unlock
1149 }
1150 error = chfs_do_unlink(ip,
1151 newparent, tcnp->cn_nameptr, tcnp->cn_namelen);
1152 vput(tvp);
1153 }
1154 VFS_VGET(tdvp->v_mount, old->ino, &tvp);
1155 ip = VTOI(tvp);
1156
1157 // for (oldfd = oldparent->dents;
1158 // oldfd->vno != old->ino;
1159 // oldfd = oldfd->next);
1160
1161 error = chfs_do_link(ip,
1162 newparent, tcnp->cn_nameptr, tcnp->cn_namelen, ip->ch_type);
1163 error = chfs_do_unlink(old,
1164 oldparent, fcnp->cn_nameptr, fcnp->cn_namelen);
1165
1166 //out:
1167 // if (fchnode != tchnode)
1168 // VOP_UNLOCK(fdvp, 0);
1169
1170 out_unlocked:
1171 // Release target nodes.
1172 if (tdvp == tvp)
1173 vrele(tdvp);
1174 else
1175 vput(tdvp);
1176 if (tvp != NULL)
1177 vput(tvp);
1178
1179 // Release source nodes.
1180 vrele(fdvp);
1181 vrele(fvp);
1182
1183 return error;
1184 }
1185
1186 /* --------------------------------------------------------------------- */
1187
1188 int
1189 chfs_mkdir(void *v)
1190 {
1191 struct vnode *dvp = ((struct vop_mkdir_args *) v)->a_dvp;
1192 struct vnode **vpp = ((struct vop_mkdir_args *)v)->a_vpp;
1193 struct componentname *cnp = ((struct vop_mkdir_args *) v)->a_cnp;
1194 struct vattr *vap = ((struct vop_mkdir_args *) v)->a_vap;
1195 dbg("mkdir()\n");
1196
1197 int mode;
1198
1199 mode = vap->va_mode & ACCESSPERMS;
1200 if ((mode & IFMT) == 0) {
1201 mode |= IFDIR;
1202 }
1203
1204 KASSERT(vap->va_type == VDIR);
1205
1206 return chfs_makeinode(mode, dvp, vpp, cnp, VDIR);
1207 }
1208
1209 /* --------------------------------------------------------------------- */
1210
1211 int
1212 chfs_rmdir(void *v)
1213 {
1214 struct vnode *dvp = ((struct vop_rmdir_args *) v)->a_dvp;
1215 struct vnode *vp = ((struct vop_rmdir_args *) v)->a_vp;
1216 struct componentname *cnp = ((struct vop_rmdir_args *) v)->a_cnp;
1217 dbg("rmdir()\n");
1218
1219 KASSERT(VOP_ISLOCKED(dvp));
1220 KASSERT(VOP_ISLOCKED(vp));
1221
1222 struct chfs_inode *ip = VTOI(vp);
1223 struct chfs_inode *parent = VTOI(dvp);
1224 struct chfs_dirent *fd;
1225 int error = 0;
1226
1227 if (vp->v_type != VDIR) {
1228 error = ENOTDIR;
1229 goto out;
1230 }
1231
1232 KASSERT(ip->chvc->vno != ip->chvc->pvno);
1233
1234 // for (fd = ip->dents; fd; fd = fd->next) {
1235 TAILQ_FOREACH(fd, &ip->dents, fds) {
1236 if (fd->vno) {
1237 error = ENOTEMPTY;
1238 goto out;
1239 }
1240 }
1241
1242 error = chfs_do_unlink(ip,
1243 parent, cnp->cn_nameptr, cnp->cn_namelen);
1244
1245 out:
1246 vput(dvp);
1247 vput(vp);
1248
1249 return error;
1250 }
1251
1252 /* --------------------------------------------------------------------- */
1253
1254 int
1255 chfs_symlink(void *v)
1256 {
1257 struct vnode *dvp = ((struct vop_symlink_args *) v)->a_dvp;
1258 struct vnode **vpp = ((struct vop_symlink_args *) v)->a_vpp;
1259 struct componentname *cnp = ((struct vop_symlink_args *) v)->a_cnp;
1260 struct vattr *vap = ((struct vop_symlink_args *) v)->a_vap;
1261 char *target = ((struct vop_symlink_args *) v)->a_target;
1262
1263 struct ufsmount *ump;
1264 struct chfs_mount *chmp;
1265 struct vnode *vp;
1266 struct chfs_inode *ip;
1267 int len, err;
1268 struct chfs_full_dnode *fd;
1269 struct buf *bp;
1270 dbg("symlink()\n");
1271
1272 ump = VFSTOUFS(dvp->v_mount);
1273 chmp = ump->um_chfs;
1274
1275 err = chfs_makeinode(IFLNK | vap->va_mode, dvp, vpp, cnp, VLNK);
1276 if (err)
1277 return (err);
1278 VN_KNOTE(dvp, NOTE_WRITE);
1279 vp = *vpp;
1280 len = strlen(target);
1281 ip = VTOI(vp);
1282 /* TODO max symlink len instead of "100" */
1283 if (len < 100) {
1284 ip->target = kmem_alloc(len, KM_SLEEP);
1285 memcpy(ip->target, target, len);
1286 chfs_set_vnode_size(vp, len);
1287 ip->iflag |= IN_CHANGE | IN_UPDATE;
1288
1289 bp = getiobuf(vp, true);
1290 bp->b_bufsize = bp->b_resid = len;
1291 bp->b_data = kmem_alloc(len, KM_SLEEP);
1292 memcpy(bp->b_data, target, len);
1293 bp->b_blkno = 0;
1294
1295 fd = chfs_alloc_full_dnode();
1296
1297 mutex_enter(&chmp->chm_lock_mountfields);
1298
1299 err = chfs_write_flash_dnode(chmp, vp, bp, fd);
1300 if (err) {
1301 mutex_exit(&chmp->chm_lock_mountfields);
1302 goto out;
1303 }
1304
1305 err = chfs_add_full_dnode_to_inode(chmp, ip, fd);
1306 if (err) {
1307 mutex_exit(&chmp->chm_lock_mountfields);
1308 goto out;
1309 }
1310
1311 mutex_exit(&chmp->chm_lock_mountfields);
1312
1313 kmem_free(bp->b_data, len);
1314 putiobuf(bp);
1315
1316 uvm_vnp_setsize(vp, len);
1317 } else {
1318 err = vn_rdwr(UIO_WRITE, vp, target, len, (off_t)0,
1319 UIO_SYSSPACE, IO_NODELOCKED, cnp->cn_cred,
1320 (size_t *)0, NULL);
1321 }
1322
1323 out:
1324 if (err)
1325 vput(vp);
1326
1327 return (err);
1328 }
1329
1330 /* --------------------------------------------------------------------- */
1331
1332 int
1333 chfs_readdir(void *v)
1334 {
1335 struct vnode *vp = ((struct vop_readdir_args *) v)->a_vp;
1336 struct uio *uio = ((struct vop_readdir_args *) v)->a_uio;
1337 int *eofflag = ((struct vop_readdir_args *) v)->a_eofflag;
1338
1339 int error = 0;
1340 off_t skip, offset;
1341 struct chfs_inode *ip;
1342 struct chfs_dirent *fd;
1343
1344 struct ufsmount *ump;
1345 struct chfs_mount *chmp;
1346 struct chfs_vnode_cache *chvc;
1347
1348 KASSERT(VOP_ISLOCKED(vp));
1349
1350 /* This operation only makes sense on directory nodes. */
1351 if (vp->v_type != VDIR) {
1352 error = ENOTDIR;
1353 goto out;
1354 }
1355
1356 ip = VTOI(vp);
1357
1358 /* uiomove in chfs_filldir automatically increments the
1359 * uio_offset by an arbitrary size, so we discard any change
1360 * to uio_offset and set it to our own value on return
1361 */
1362 offset = uio->uio_offset;
1363
1364 if (offset == CHFS_OFFSET_DOT) {
1365 error = chfs_filldir(uio, ip->ino, ".", 1, VDIR);
1366 if (error == -1) {
1367 error = 0;
1368 goto outok;
1369 } else if (error != 0)
1370 goto outok;
1371
1372 offset = CHFS_OFFSET_DOTDOT;
1373 }
1374
1375 if (offset == CHFS_OFFSET_DOTDOT) {
1376 ump = VFSTOUFS(vp->v_mount);
1377 chmp = ump->um_chfs;
1378 mutex_enter(&chmp->chm_lock_vnocache);
1379 chvc = chfs_vnode_cache_get(chmp, ip->ino);
1380 mutex_exit(&chmp->chm_lock_vnocache);
1381
1382 error = chfs_filldir(uio, chvc->pvno, "..", 2, VDIR);
1383 if (error == -1) {
1384 error = 0;
1385 goto outok;
1386 } else if (error != 0) {
1387 goto outok;
1388 }
1389
1390 if (TAILQ_EMPTY(&ip->dents)) {
1391 offset = CHFS_OFFSET_EOF;
1392 } else {
1393 offset = CHFS_OFFSET_FIRST;
1394 }
1395 }
1396
1397 if (offset != CHFS_OFFSET_EOF) {
1398 skip = offset - CHFS_OFFSET_FIRST;
1399
1400 TAILQ_FOREACH(fd, &ip->dents, fds) {
1401 /* seek to offset by skipping items */
1402 /* XXX race conditions by changed dirent? */
1403 if (skip > 0) {
1404 skip--;
1405 continue;
1406 }
1407
1408 if (fd->vno != 0) {
1409 error = chfs_filldir(uio, fd->vno,
1410 fd->name, fd->nsize, fd->type);
1411 if (error == -1) {
1412 error = 0;
1413 goto outok;
1414 } else if (error != 0) {
1415 dbg("err %d\n", error);
1416 goto outok;
1417 }
1418 }
1419 offset++;
1420 }
1421 }
1422 offset = CHFS_OFFSET_EOF;
1423
1424 outok:
1425 uio->uio_offset = offset;
1426
1427 if (eofflag != NULL) {
1428 *eofflag = (error == 0 &&
1429 uio->uio_offset == CHFS_OFFSET_EOF);
1430 }
1431
1432 out:
1433 KASSERT(VOP_ISLOCKED(vp));
1434
1435 return error;
1436 }
1437
1438 /* --------------------------------------------------------------------- */
1439
1440 int
1441 chfs_readlink(void *v)
1442 {
1443
1444 struct vnode *vp = ((struct vop_readlink_args *) v)->a_vp;
1445 struct uio *uio = ((struct vop_readlink_args *) v)->a_uio;
1446 kauth_cred_t cred = ((struct vop_readlink_args *) v)->a_cred;
1447
1448 struct chfs_inode *ip = VTOI(vp);
1449
1450 dbg("readlink()\n");
1451
1452 /* TODO max symlink len instead of "100" */
1453 if (ip->size < 100) {
1454 uiomove(ip->target, ip->size, uio);
1455 return (0);
1456 }
1457
1458 return (VOP_READ(vp, uio, 0, cred));
1459 }
1460
1461 /* --------------------------------------------------------------------- */
1462
1463 int
1464 chfs_inactive(void *v)
1465 {
1466 struct vnode *vp = ((struct vop_inactive_args *) v)->a_vp;
1467 struct chfs_inode *ip = VTOI(vp);
1468 struct chfs_vnode_cache *chvc;
1469 dbg("inactive | vno: %llu\n", (unsigned long long)ip->ino);
1470
1471 KASSERT(VOP_ISLOCKED(vp));
1472
1473 if (ip->ino) {
1474 chvc = ip->chvc;
1475 if (chvc->nlink)
1476 *((struct vop_inactive_args *) v)->a_recycle = 0;
1477 } else {
1478 *((struct vop_inactive_args *) v)->a_recycle = 1;
1479 }
1480
1481 VOP_UNLOCK(vp);
1482
1483 return 0;
1484 }
1485
1486 /* --------------------------------------------------------------------- */
1487
1488 int
1489 chfs_reclaim(void *v)
1490 {
1491 struct vop_reclaim_args *ap = v;
1492 struct vnode *vp = ap->a_vp;
1493 struct chfs_inode *ip = VTOI(vp);
1494 struct chfs_mount *chmp = ip->chmp;
1495 struct chfs_dirent *fd;
1496
1497 //dbg("reclaim() | ino: %llu\n", (unsigned long long)ip->ino);
1498 //mutex_enter(&ip->inode_lock);
1499
1500 mutex_enter(&chmp->chm_lock_vnocache);
1501 chfs_vnode_cache_set_state(chmp,
1502 ip->chvc, VNO_STATE_CHECKEDABSENT);
1503 mutex_exit(&chmp->chm_lock_vnocache);
1504
1505 chfs_update(vp, NULL, NULL, UPDATE_CLOSE);
1506
1507 if (ip->ch_type == CHT_REG || ip->ch_type == CHT_LNK ||
1508 ip->ch_type == CHT_CHR || ip->ch_type == CHT_BLK ||
1509 ip->ch_type == CHT_FIFO || ip->ch_type == CHT_SOCK) {
1510 chfs_kill_fragtree(&ip->fragtree);
1511 }
1512
1513 fd = TAILQ_FIRST(&ip->dents);
1514 while(fd) {
1515 TAILQ_REMOVE(&ip->dents, fd, fds);
1516 chfs_free_dirent(fd);
1517 fd = TAILQ_FIRST(&ip->dents);
1518 }
1519 //mutex_exit(&ip->inode_lock);
1520 //mutex_destroy(&ip->inode_lock);
1521
1522 cache_purge(vp);
1523 if (ip->devvp) {
1524 vrele(ip->devvp);
1525 ip->devvp = 0;
1526 }
1527 chfs_ihashrem(ip);
1528
1529 genfs_node_destroy(vp);
1530 pool_put(&chfs_inode_pool, vp->v_data);
1531 vp->v_data = NULL;
1532 return (0);
1533 }
1534
1535 /* --------------------------------------------------------------------- */
1536
1537 int
1538 chfs_advlock(void *v)
1539 {
1540 //struct vnode *vp = ((struct vop_advlock_args *) v)->a_vp;
1541 dbg("advlock()\n");
1542 /*
1543 struct chfs_node *node;
1544
1545 node = VP_TO_CHFS_NODE(vp);
1546
1547 return lf_advlock(v, &node->chn_lockf, node->chn_size);
1548 */
1549 return 0;
1550 }
1551
1552 /* --------------------------------------------------------------------- */
1553 int
1554 chfs_strategy(void *v)
1555 {
1556 struct vop_strategy_args /* {
1557 const struct vnodeop_desc *a_desc;
1558 struct vnode *a_vp;
1559 struct buf *a_bp;
1560 } */ *ap = v;
1561 struct chfs_full_dnode *fd;
1562 struct buf *bp = ap->a_bp;
1563 struct vnode *vp = ap->a_vp;
1564 struct chfs_inode *ip = VTOI(vp);
1565 struct chfs_mount *chmp = ip->chmp;
1566 int read = (bp->b_flags & B_READ) ? 1 : 0;
1567 int err = 0;
1568
1569 /* dbg("bp dump:\n");
1570 dbg(" ->b_bcount: %d\n", bp->b_bcount);
1571 dbg(" ->b_resid: %d\n", bp->b_resid);
1572 dbg(" ->b_blkno: %llu\n", (unsigned long long)bp->b_blkno);
1573 dbg(" ->b_error: %d\n", bp->b_error);*/
1574 if (read) {
1575 err = chfs_read_data(chmp, vp, bp);
1576 } else {
1577 fd = chfs_alloc_full_dnode();
1578
1579 mutex_enter(&chmp->chm_lock_mountfields);
1580
1581 err = chfs_write_flash_dnode(chmp, vp, bp, fd);
1582 if (err) {
1583 mutex_exit(&chmp->chm_lock_mountfields);
1584 goto out;
1585 }
1586
1587 err = chfs_add_full_dnode_to_inode(chmp, ip, fd);
1588 /*if (err) {
1589 mutex_exit(&chmp->chm_lock_mountfields);
1590 goto out;
1591 }*/
1592
1593 mutex_exit(&chmp->chm_lock_mountfields);
1594 }
1595 out:
1596 biodone(bp);
1597 //dbg("end\n");
1598 return err;
1599 }
1600
1601 int
1602 chfs_bmap(void *v)
1603 {
1604 struct vop_bmap_args /* {
1605 struct vnode *a_vp;
1606 daddr_t a_bn;
1607 struct vnode **a_vpp;
1608 daddr_t *a_bnp;
1609 int *a_runp;
1610 int *a_runb;
1611 } */ *ap = v;
1612 if (ap->a_vpp != NULL)
1613 *ap->a_vpp = ap->a_vp;
1614 if (ap->a_bnp != NULL)
1615 *ap->a_bnp = ap->a_bn;
1616 if (ap->a_runp != NULL)
1617 *ap->a_runp = 0;
1618 return (0);
1619 }
1620
1621 /*
1622 * vnode operations vector used for files stored in a chfs file system.
1623 */
1624 int
1625 (**chfs_vnodeop_p)(void *);
1626 const struct vnodeopv_entry_desc chfs_vnodeop_entries[] =
1627 {
1628 { &vop_default_desc, vn_default_error },
1629 { &vop_lookup_desc, chfs_lookup },
1630 { &vop_create_desc, chfs_create },
1631 { &vop_mknod_desc, chfs_mknod },
1632 { &vop_open_desc, chfs_open },
1633 { &vop_close_desc, chfs_close },
1634 { &vop_access_desc, chfs_access },
1635 { &vop_getattr_desc, chfs_getattr },
1636 { &vop_setattr_desc, chfs_setattr },
1637 { &vop_read_desc, chfs_read },
1638 { &vop_write_desc, chfs_write },
1639 { &vop_ioctl_desc, genfs_enoioctl },
1640 { &vop_fcntl_desc, genfs_fcntl },
1641 { &vop_poll_desc, genfs_poll },
1642 { &vop_kqfilter_desc, genfs_kqfilter },
1643 { &vop_revoke_desc, genfs_revoke },
1644 { &vop_mmap_desc, genfs_mmap },
1645 { &vop_fsync_desc, chfs_fsync },
1646 { &vop_seek_desc, genfs_seek },
1647 { &vop_remove_desc, chfs_remove },
1648 { &vop_link_desc, chfs_link },
1649 { &vop_rename_desc, chfs_rename },
1650 { &vop_mkdir_desc, chfs_mkdir },
1651 { &vop_rmdir_desc, chfs_rmdir },
1652 { &vop_symlink_desc, chfs_symlink },
1653 { &vop_readdir_desc, chfs_readdir },
1654 { &vop_readlink_desc, chfs_readlink },
1655 { &vop_abortop_desc, genfs_abortop },
1656 { &vop_inactive_desc, chfs_inactive },
1657 { &vop_reclaim_desc, chfs_reclaim },
1658 { &vop_lock_desc, genfs_lock },
1659 { &vop_unlock_desc, genfs_unlock },
1660 { &vop_bmap_desc, chfs_bmap },
1661 { &vop_strategy_desc, chfs_strategy },
1662 { &vop_print_desc, ufs_print },
1663 { &vop_pathconf_desc, ufs_pathconf },
1664 { &vop_islocked_desc, genfs_islocked },
1665 { &vop_advlock_desc, chfs_advlock },
1666 { &vop_bwrite_desc, vn_bwrite },
1667 { &vop_getpages_desc, genfs_getpages },
1668 { &vop_putpages_desc, genfs_putpages },
1669 { NULL, NULL } };
1670
1671 const struct vnodeopv_desc chfs_vnodeop_opv_desc =
1672 { &chfs_vnodeop_p, chfs_vnodeop_entries };
1673
1674 /* --------------------------------------------------------------------- */
1675
1676 /*
1677 * vnode operations vector used for special devices stored in a chfs
1678 * file system.
1679 */
1680 int
1681 (**chfs_specop_p)(void *);
1682 const struct vnodeopv_entry_desc chfs_specop_entries[] =
1683 {
1684 { &vop_default_desc, vn_default_error },
1685 { &vop_lookup_desc, spec_lookup },
1686 { &vop_create_desc, spec_create },
1687 { &vop_mknod_desc, spec_mknod },
1688 { &vop_open_desc, spec_open },
1689 { &vop_close_desc, ufsspec_close },
1690 { &vop_access_desc, chfs_access },
1691 { &vop_getattr_desc, chfs_getattr },
1692 { &vop_setattr_desc, chfs_setattr },
1693 { &vop_read_desc, chfs_read },
1694 { &vop_write_desc, chfs_write },
1695 { &vop_ioctl_desc, spec_ioctl },
1696 { &vop_fcntl_desc, genfs_fcntl },
1697 { &vop_poll_desc, spec_poll },
1698 { &vop_kqfilter_desc, spec_kqfilter },
1699 { &vop_revoke_desc, spec_revoke },
1700 { &vop_mmap_desc, spec_mmap },
1701 { &vop_fsync_desc, spec_fsync },
1702 { &vop_seek_desc, spec_seek },
1703 { &vop_remove_desc, spec_remove },
1704 { &vop_link_desc, spec_link },
1705 { &vop_rename_desc, spec_rename },
1706 { &vop_mkdir_desc, spec_mkdir },
1707 { &vop_rmdir_desc, spec_rmdir },
1708 { &vop_symlink_desc, spec_symlink },
1709 { &vop_readdir_desc, spec_readdir },
1710 { &vop_readlink_desc, spec_readlink },
1711 { &vop_abortop_desc, spec_abortop },
1712 { &vop_inactive_desc, chfs_inactive },
1713 { &vop_reclaim_desc, chfs_reclaim },
1714 { &vop_lock_desc, genfs_lock },
1715 { &vop_unlock_desc, genfs_unlock },
1716 { &vop_bmap_desc, spec_bmap },
1717 { &vop_strategy_desc, spec_strategy },
1718 { &vop_print_desc, ufs_print },
1719 { &vop_pathconf_desc, spec_pathconf },
1720 { &vop_islocked_desc, genfs_islocked },
1721 { &vop_advlock_desc, spec_advlock },
1722 { &vop_bwrite_desc, vn_bwrite },
1723 { &vop_getpages_desc, spec_getpages },
1724 { &vop_putpages_desc, spec_putpages },
1725 { NULL, NULL } };
1726
1727 const struct vnodeopv_desc chfs_specop_opv_desc =
1728 { &chfs_specop_p, chfs_specop_entries };
1729
1730 /* --------------------------------------------------------------------- */
1731 /*
1732 * vnode operations vector used for fifos stored in a chfs file system.
1733 */
1734 int
1735 (**chfs_fifoop_p)(void *);
1736 const struct vnodeopv_entry_desc chfs_fifoop_entries[] =
1737 {
1738 { &vop_default_desc, vn_default_error },
1739 { &vop_lookup_desc, vn_fifo_bypass },
1740 { &vop_create_desc, vn_fifo_bypass },
1741 { &vop_mknod_desc, vn_fifo_bypass },
1742 { &vop_open_desc, vn_fifo_bypass },
1743 { &vop_close_desc, ufsfifo_close },
1744 { &vop_access_desc, chfs_access },
1745 { &vop_getattr_desc, chfs_getattr },
1746 { &vop_setattr_desc, chfs_setattr },
1747 { &vop_read_desc, ufsfifo_read },
1748 { &vop_write_desc, ufsfifo_write },
1749 { &vop_ioctl_desc, vn_fifo_bypass },
1750 { &vop_fcntl_desc, genfs_fcntl },
1751 { &vop_poll_desc, vn_fifo_bypass },
1752 { &vop_kqfilter_desc, vn_fifo_bypass },
1753 { &vop_revoke_desc, vn_fifo_bypass },
1754 { &vop_mmap_desc, vn_fifo_bypass },
1755 { &vop_fsync_desc, vn_fifo_bypass },
1756 { &vop_seek_desc, vn_fifo_bypass },
1757 { &vop_remove_desc, vn_fifo_bypass },
1758 { &vop_link_desc, vn_fifo_bypass },
1759 { &vop_rename_desc, vn_fifo_bypass },
1760 { &vop_mkdir_desc, vn_fifo_bypass },
1761 { &vop_rmdir_desc, vn_fifo_bypass },
1762 { &vop_symlink_desc, vn_fifo_bypass },
1763 { &vop_readdir_desc, vn_fifo_bypass },
1764 { &vop_readlink_desc, vn_fifo_bypass },
1765 { &vop_abortop_desc, vn_fifo_bypass },
1766 { &vop_inactive_desc, chfs_inactive },
1767 { &vop_reclaim_desc, chfs_reclaim },
1768 { &vop_lock_desc, genfs_lock },
1769 { &vop_unlock_desc, genfs_unlock },
1770 { &vop_bmap_desc, vn_fifo_bypass },
1771 { &vop_strategy_desc, vn_fifo_bypass },
1772 { &vop_print_desc, ufs_print },
1773 { &vop_pathconf_desc, vn_fifo_bypass },
1774 { &vop_islocked_desc, genfs_islocked },
1775 { &vop_advlock_desc, vn_fifo_bypass },
1776 { &vop_bwrite_desc, genfs_nullop },
1777 { &vop_getpages_desc, genfs_badop },
1778 { &vop_putpages_desc, vn_fifo_bypass },
1779 { NULL, NULL } };
1780
1781 const struct vnodeopv_desc chfs_fifoop_opv_desc =
1782 { &chfs_fifoop_p, chfs_fifoop_entries };
1783