]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/nandfs/nandfs_vnops.c
MFV: file 5.33
[FreeBSD/FreeBSD.git] / sys / fs / nandfs / nandfs_vnops.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010-2012 Semihalf
5  * Copyright (c) 2008, 2009 Reinoud Zandijk
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * From: NetBSD: nilfs_vnops.c,v 1.2 2009/08/26 03:40:48 elad
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/conf.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/lockf.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
42 #include <sys/mutex.h>
43 #include <sys/namei.h>
44 #include <sys/sysctl.h>
45 #include <sys/unistd.h>
46 #include <sys/vnode.h>
47 #include <sys/buf.h>
48 #include <sys/bio.h>
49 #include <sys/fcntl.h>
50 #include <sys/dirent.h>
51 #include <sys/rwlock.h>
52 #include <sys/stat.h>
53 #include <sys/priv.h>
54
55 #include <vm/vm.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_object.h>
58 #include <vm/vnode_pager.h>
59
60 #include <machine/_inttypes.h>
61
62 #include <fs/nandfs/nandfs_mount.h>
63 #include <fs/nandfs/nandfs.h>
64 #include <fs/nandfs/nandfs_subr.h>
65
66 extern uma_zone_t nandfs_node_zone;
67 static void nandfs_read_filebuf(struct nandfs_node *, struct buf *);
68 static void nandfs_itimes_locked(struct vnode *);
69 static int nandfs_truncate(struct vnode *, uint64_t);
70
71 static vop_pathconf_t   nandfs_pathconf;
72
73 #define UPDATE_CLOSE 0
74 #define UPDATE_WAIT 0
75
76 static int
77 nandfs_inactive(struct vop_inactive_args *ap)
78 {
79         struct vnode *vp = ap->a_vp;
80         struct nandfs_node *node = VTON(vp);
81         int error = 0;
82
83         DPRINTF(VNCALL, ("%s: vp:%p node:%p\n", __func__, vp, node));
84
85         if (node == NULL) {
86                 DPRINTF(NODE, ("%s: inactive NULL node\n", __func__));
87                 return (0);
88         }
89
90         if (node->nn_inode.i_mode != 0 && !(node->nn_inode.i_links_count)) {
91                 nandfs_truncate(vp, 0);
92                 error = nandfs_node_destroy(node);
93                 if (error)
94                         nandfs_error("%s: destroy node: %p\n", __func__, node);
95                 node->nn_flags = 0;
96                 vrecycle(vp);
97         }
98
99         return (error);
100 }
101
102 static int
103 nandfs_reclaim(struct vop_reclaim_args *ap)
104 {
105         struct vnode *vp = ap->a_vp;
106         struct nandfs_node *nandfs_node = VTON(vp);
107         struct nandfs_device *fsdev = nandfs_node->nn_nandfsdev;
108         uint64_t ino = nandfs_node->nn_ino;
109
110         DPRINTF(VNCALL, ("%s: vp:%p node:%p\n", __func__, vp, nandfs_node));
111
112         /* Invalidate all entries to a particular vnode. */
113         cache_purge(vp);
114
115         /* Destroy the vm object and flush associated pages. */
116         vnode_destroy_vobject(vp);
117
118         /* Remove from vfs hash if not system vnode */
119         if (!NANDFS_SYS_NODE(nandfs_node->nn_ino))
120                 vfs_hash_remove(vp);
121
122         /* Dispose all node knowledge */
123         nandfs_dispose_node(&nandfs_node);
124
125         if (!NANDFS_SYS_NODE(ino))
126                 NANDFS_WRITEUNLOCK(fsdev);
127
128         return (0);
129 }
130
131 static int
132 nandfs_read(struct vop_read_args *ap)
133 {
134         struct vnode *vp = ap->a_vp;
135         struct nandfs_node *node = VTON(vp);
136         struct nandfs_device *nandfsdev = node->nn_nandfsdev;
137         struct uio *uio = ap->a_uio;
138         struct buf *bp;
139         uint64_t size;
140         uint32_t blocksize;
141         off_t bytesinfile;
142         ssize_t toread, off;
143         daddr_t lbn;
144         ssize_t resid;
145         int error = 0;
146
147         if (uio->uio_resid == 0)
148                 return (0);
149
150         size = node->nn_inode.i_size;
151         if (uio->uio_offset >= size)
152                 return (0);
153
154         blocksize = nandfsdev->nd_blocksize;
155         bytesinfile = size - uio->uio_offset;
156
157         resid = omin(uio->uio_resid, bytesinfile);
158
159         while (resid) {
160                 lbn = uio->uio_offset / blocksize;
161                 off = uio->uio_offset & (blocksize - 1);
162
163                 toread = omin(resid, blocksize - off);
164
165                 DPRINTF(READ, ("nandfs_read bn: 0x%jx toread: 0x%zx (0x%x)\n",
166                     (uintmax_t)lbn, toread, blocksize));
167
168                 error = nandfs_bread(node, lbn, NOCRED, 0, &bp);
169                 if (error) {
170                         brelse(bp);
171                         break;
172                 }
173
174                 error = uiomove(bp->b_data + off, toread, uio);
175                 if (error) {
176                         brelse(bp);
177                         break;
178                 }
179
180                 brelse(bp);
181                 resid -= toread;
182         }
183
184         return (error);
185 }
186
187 static int
188 nandfs_write(struct vop_write_args *ap)
189 {
190         struct nandfs_device *fsdev;
191         struct nandfs_node *node;
192         struct vnode *vp;
193         struct uio *uio;
194         struct buf *bp;
195         uint64_t file_size, vblk;
196         uint32_t blocksize;
197         ssize_t towrite, off;
198         daddr_t lbn;
199         ssize_t resid;
200         int error, ioflag, modified;
201
202         vp = ap->a_vp;
203         uio = ap->a_uio;
204         ioflag = ap->a_ioflag;
205         node = VTON(vp);
206         fsdev = node->nn_nandfsdev;
207
208         if (nandfs_fs_full(fsdev))
209                 return (ENOSPC);
210
211         DPRINTF(WRITE, ("nandfs_write called %#zx at %#jx\n",
212             uio->uio_resid, (uintmax_t)uio->uio_offset));
213
214         if (uio->uio_offset < 0)
215                 return (EINVAL);
216         if (uio->uio_resid == 0)
217                 return (0);
218
219         blocksize = fsdev->nd_blocksize;
220         file_size = node->nn_inode.i_size;
221
222         switch (vp->v_type) {
223         case VREG:
224                 if (ioflag & IO_APPEND)
225                         uio->uio_offset = file_size;
226                 break;
227         case VDIR:
228                 return (EISDIR);
229         case VLNK:
230                 break;
231         default:
232                 panic("%s: bad file type vp: %p", __func__, vp);
233         }
234
235         /* If explicitly asked to append, uio_offset can be wrong? */
236         if (ioflag & IO_APPEND)
237                 uio->uio_offset = file_size;
238
239         resid = uio->uio_resid;
240         modified = error = 0;
241
242         while (uio->uio_resid) {
243                 lbn = uio->uio_offset / blocksize;
244                 off = uio->uio_offset & (blocksize - 1);
245
246                 towrite = omin(uio->uio_resid, blocksize - off);
247
248                 DPRINTF(WRITE, ("%s: lbn: 0x%jd toread: 0x%zx (0x%x)\n",
249                     __func__, (uintmax_t)lbn, towrite, blocksize));
250
251                 error = nandfs_bmap_lookup(node, lbn, &vblk);
252                 if (error)
253                         break;
254
255                 DPRINTF(WRITE, ("%s: lbn: 0x%jd toread: 0x%zx (0x%x) "
256                     "vblk=%jx\n", __func__, (uintmax_t)lbn, towrite, blocksize,
257                     vblk));
258
259                 if (vblk != 0)
260                         error = nandfs_bread(node, lbn, NOCRED, 0, &bp);
261                 else
262                         error = nandfs_bcreate(node, lbn, NOCRED, 0, &bp);
263
264                 DPRINTF(WRITE, ("%s: vp %p bread bp %p lbn %#jx\n", __func__,
265                     vp, bp, (uintmax_t)lbn));
266                 if (error) {
267                         if (bp)
268                                 brelse(bp);
269                         break;
270                 }
271
272                 error = uiomove((char *)bp->b_data + off, (int)towrite, uio);
273                 if (error)
274                         break;
275
276                 error = nandfs_dirty_buf(bp, 0);
277                 if (error)
278                         break;
279
280                 modified++;
281         }
282
283         /* XXX proper handling when only part of file was properly written */
284         if (modified) {
285                 if (resid > uio->uio_resid && ap->a_cred &&
286                     ap->a_cred->cr_uid != 0)
287                         node->nn_inode.i_mode &= ~(ISUID | ISGID);
288
289                 if (file_size < uio->uio_offset + uio->uio_resid) {
290                         node->nn_inode.i_size = uio->uio_offset +
291                             uio->uio_resid;
292                         node->nn_flags |= IN_CHANGE | IN_UPDATE;
293                         vnode_pager_setsize(vp, uio->uio_offset +
294                             uio->uio_resid);
295                         nandfs_itimes(vp);
296                 }
297         }
298
299         DPRINTF(WRITE, ("%s: return:%d\n", __func__, error));
300
301         return (error);
302 }
303
304 static int
305 nandfs_lookup(struct vop_cachedlookup_args *ap)
306 {
307         struct vnode *dvp, **vpp;
308         struct componentname *cnp;
309         struct ucred *cred;
310         struct thread *td;
311         struct nandfs_node *dir_node, *node;
312         struct nandfsmount *nmp;
313         uint64_t ino, off;
314         const char *name;
315         int namelen, nameiop, islastcn, mounted_ro;
316         int error, found;
317
318         DPRINTF(VNCALL, ("%s\n", __func__));
319
320         dvp = ap->a_dvp;
321         vpp = ap->a_vpp;
322         *vpp = NULL;
323
324         cnp = ap->a_cnp;
325         cred = cnp->cn_cred;
326         td = cnp->cn_thread;
327
328         dir_node = VTON(dvp);
329         nmp = dir_node->nn_nmp;
330
331         /* Simplify/clarification flags */
332         nameiop = cnp->cn_nameiop;
333         islastcn = cnp->cn_flags & ISLASTCN;
334         mounted_ro = dvp->v_mount->mnt_flag & MNT_RDONLY;
335
336         /*
337          * If requesting a modify on the last path element on a read-only
338          * filingsystem, reject lookup;
339          */
340         if (islastcn && mounted_ro && (nameiop == DELETE || nameiop == RENAME))
341                 return (EROFS);
342
343         if (dir_node->nn_inode.i_links_count == 0)
344                 return (ENOENT);
345
346         /*
347          * Obviously, the file is not (anymore) in the namecache, we have to
348          * search for it. There are three basic cases: '.', '..' and others.
349          *
350          * Following the guidelines of VOP_LOOKUP manpage and tmpfs.
351          */
352         error = 0;
353         if ((cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.')) {
354                 DPRINTF(LOOKUP, ("\tlookup '.'\n"));
355                 /* Special case 1 '.' */
356                 VREF(dvp);
357                 *vpp = dvp;
358                 /* Done */
359         } else if (cnp->cn_flags & ISDOTDOT) {
360                 /* Special case 2 '..' */
361                 DPRINTF(LOOKUP, ("\tlookup '..'\n"));
362
363                 /* Get our node */
364                 name = "..";
365                 namelen = 2;
366                 error = nandfs_lookup_name_in_dir(dvp, name, namelen, &ino,
367                     &found, &off);
368                 if (error)
369                         goto out;
370                 if (!found)
371                         error = ENOENT;
372
373                 /* First unlock parent */
374                 VOP_UNLOCK(dvp, 0);
375
376                 if (error == 0) {
377                         DPRINTF(LOOKUP, ("\tfound '..'\n"));
378                         /* Try to create/reuse the node */
379                         error = nandfs_get_node(nmp, ino, &node);
380
381                         if (!error) {
382                                 DPRINTF(LOOKUP,
383                                     ("\tnode retrieved/created OK\n"));
384                                 *vpp = NTOV(node);
385                         }
386                 }
387
388                 /* Try to relock parent */
389                 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
390         } else {
391                 DPRINTF(LOOKUP, ("\tlookup file\n"));
392                 /* All other files */
393                 /* Look up filename in the directory returning its inode */
394                 name = cnp->cn_nameptr;
395                 namelen = cnp->cn_namelen;
396                 error = nandfs_lookup_name_in_dir(dvp, name, namelen,
397                     &ino, &found, &off);
398                 if (error)
399                         goto out;
400                 if (!found) {
401                         DPRINTF(LOOKUP, ("\tNOT found\n"));
402                         /*
403                          * UGH, didn't find name. If we're creating or
404                          * renaming on the last name this is OK and we ought
405                          * to return EJUSTRETURN if its allowed to be created.
406                          */
407                         error = ENOENT;
408                         if ((nameiop == CREATE || nameiop == RENAME) &&
409                             islastcn) {
410                                 error = VOP_ACCESS(dvp, VWRITE, cred, td);
411                                 if (!error) {
412                                         /* keep the component name */
413                                         cnp->cn_flags |= SAVENAME;
414                                         error = EJUSTRETURN;
415                                 }
416                         }
417                         /* Done */
418                 } else {
419                         if (ino == NANDFS_WHT_INO)
420                                 cnp->cn_flags |= ISWHITEOUT;
421
422                         if ((cnp->cn_flags & ISWHITEOUT) &&
423                             (nameiop == LOOKUP))
424                                 return (ENOENT);
425
426                         if ((nameiop == DELETE) && islastcn) {
427                                 if ((cnp->cn_flags & ISWHITEOUT) &&
428                                     (cnp->cn_flags & DOWHITEOUT)) {
429                                         cnp->cn_flags |= SAVENAME;
430                                         dir_node->nn_diroff = off;
431                                         return (EJUSTRETURN);
432                                 }
433
434                                 error = VOP_ACCESS(dvp, VWRITE, cred,
435                                     cnp->cn_thread);
436                                 if (error)
437                                         return (error);
438
439                                 /* Try to create/reuse the node */
440                                 error = nandfs_get_node(nmp, ino, &node);
441                                 if (!error) {
442                                         *vpp = NTOV(node);
443                                         node->nn_diroff = off;
444                                 }
445
446                                 if ((dir_node->nn_inode.i_mode & ISVTX) &&
447                                     cred->cr_uid != 0 &&
448                                     cred->cr_uid != dir_node->nn_inode.i_uid &&
449                                     node->nn_inode.i_uid != cred->cr_uid) {
450                                         vput(*vpp);
451                                         *vpp = NULL;
452                                         return (EPERM);
453                                 }
454                         } else if ((nameiop == RENAME) && islastcn) {
455                                 error = VOP_ACCESS(dvp, VWRITE, cred,
456                                     cnp->cn_thread);
457                                 if (error)
458                                         return (error);
459
460                                 /* Try to create/reuse the node */
461                                 error = nandfs_get_node(nmp, ino, &node);
462                                 if (!error) {
463                                         *vpp = NTOV(node);
464                                         node->nn_diroff = off;
465                                 }
466                         } else {
467                                 /* Try to create/reuse the node */
468                                 error = nandfs_get_node(nmp, ino, &node);
469                                 if (!error) {
470                                         *vpp = NTOV(node);
471                                         node->nn_diroff = off;
472                                 }
473                         }
474                 }
475         }
476
477 out:
478         /*
479          * Store result in the cache if requested. If we are creating a file,
480          * the file might not be found and thus putting it into the namecache
481          * might be seen as negative caching.
482          */
483         if ((cnp->cn_flags & MAKEENTRY) != 0)
484                 cache_enter(dvp, *vpp, cnp);
485
486         return (error);
487
488 }
489
490 static int
491 nandfs_getattr(struct vop_getattr_args *ap)
492 {
493         struct vnode *vp = ap->a_vp;
494         struct vattr *vap = ap->a_vap;
495         struct nandfs_node *node = VTON(vp);
496         struct nandfs_inode *inode = &node->nn_inode;
497
498         DPRINTF(VNCALL, ("%s: vp: %p\n", __func__, vp));
499         nandfs_itimes(vp);
500
501         /* Basic info */
502         VATTR_NULL(vap);
503         vap->va_atime.tv_sec = inode->i_mtime;
504         vap->va_atime.tv_nsec = inode->i_mtime_nsec;
505         vap->va_mtime.tv_sec = inode->i_mtime;
506         vap->va_mtime.tv_nsec = inode->i_mtime_nsec;
507         vap->va_ctime.tv_sec = inode->i_ctime;
508         vap->va_ctime.tv_nsec = inode->i_ctime_nsec;
509         vap->va_type = IFTOVT(inode->i_mode);
510         vap->va_mode = inode->i_mode & ~S_IFMT;
511         vap->va_nlink = inode->i_links_count;
512         vap->va_uid = inode->i_uid;
513         vap->va_gid = inode->i_gid;
514         vap->va_rdev = inode->i_special;
515         vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
516         vap->va_fileid = node->nn_ino;
517         vap->va_size = inode->i_size;
518         vap->va_blocksize = node->nn_nandfsdev->nd_blocksize;
519         vap->va_gen = 0;
520         vap->va_flags = inode->i_flags;
521         vap->va_bytes = inode->i_blocks * vap->va_blocksize;
522         vap->va_filerev = 0;
523         vap->va_vaflags = 0;
524
525         return (0);
526 }
527
528 static int
529 nandfs_vtruncbuf(struct vnode *vp, uint64_t nblks)
530 {
531         struct nandfs_device *nffsdev;
532         struct bufobj *bo;
533         struct buf *bp, *nbp;
534
535         bo = &vp->v_bufobj;
536         nffsdev = VTON(vp)->nn_nandfsdev;
537
538         ASSERT_VOP_LOCKED(vp, "nandfs_truncate");
539 restart:
540         BO_LOCK(bo);
541 restart_locked:
542         TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
543                 if (bp->b_lblkno < nblks)
544                         continue;
545                 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL))
546                         goto restart_locked;
547
548                 bremfree(bp);
549                 bp->b_flags |= (B_INVAL | B_RELBUF);
550                 bp->b_flags &= ~(B_ASYNC | B_MANAGED);
551                 BO_UNLOCK(bo);
552                 brelse(bp);
553                 BO_LOCK(bo);
554         }
555
556         TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
557                 if (bp->b_lblkno < nblks)
558                         continue;
559                 if (BUF_LOCK(bp,
560                     LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
561                     BO_LOCKPTR(bo)) == ENOLCK)
562                         goto restart;
563                 bp->b_flags |= (B_INVAL | B_RELBUF);
564                 bp->b_flags &= ~(B_ASYNC | B_MANAGED);
565                 brelse(bp);
566                 nandfs_dirty_bufs_decrement(nffsdev);
567                 BO_LOCK(bo);
568         }
569
570         BO_UNLOCK(bo);
571
572         return (0);
573 }
574
575 static int
576 nandfs_truncate(struct vnode *vp, uint64_t newsize)
577 {
578         struct nandfs_device *nffsdev;
579         struct nandfs_node *node;
580         struct nandfs_inode *inode;
581         struct buf *bp = NULL;
582         uint64_t oblks, nblks, vblk, size, rest;
583         int error;
584
585         node = VTON(vp);
586         nffsdev = node->nn_nandfsdev;
587         inode = &node->nn_inode;
588
589         /* Calculate end of file */
590         size = inode->i_size;
591
592         if (newsize == size) {
593                 node->nn_flags |= IN_CHANGE | IN_UPDATE;
594                 nandfs_itimes(vp);
595                 return (0);
596         }
597
598         if (newsize > size) {
599                 inode->i_size = newsize;
600                 vnode_pager_setsize(vp, newsize);
601                 node->nn_flags |= IN_CHANGE | IN_UPDATE;
602                 nandfs_itimes(vp);
603                 return (0);
604         }
605
606         nblks = howmany(newsize, nffsdev->nd_blocksize);
607         oblks = howmany(size, nffsdev->nd_blocksize);
608         rest = newsize % nffsdev->nd_blocksize;
609
610         if (rest) {
611                 error = nandfs_bmap_lookup(node, nblks - 1, &vblk);
612                 if (error)
613                         return (error);
614
615                 if (vblk != 0)
616                         error = nandfs_bread(node, nblks - 1, NOCRED, 0, &bp);
617                 else
618                         error = nandfs_bcreate(node, nblks - 1, NOCRED, 0, &bp);
619
620                 if (error) {
621                         if (bp)
622                                 brelse(bp);
623                         return (error);
624                 }
625
626                 bzero((char *)bp->b_data + rest,
627                     (u_int)(nffsdev->nd_blocksize - rest));
628                 error = nandfs_dirty_buf(bp, 0);
629                 if (error)
630                         return (error);
631         }
632
633         DPRINTF(VNCALL, ("%s: vp %p oblks %jx nblks %jx\n", __func__, vp, oblks,
634             nblks));
635
636         error = nandfs_bmap_truncate_mapping(node, oblks - 1, nblks - 1);
637         if (error) {
638                 if (bp)
639                         nandfs_undirty_buf(bp);
640                 return (error);
641         }
642
643         error = nandfs_vtruncbuf(vp, nblks);
644         if (error) {
645                 if (bp)
646                         nandfs_undirty_buf(bp);
647                 return (error);
648         }
649
650         inode->i_size = newsize;
651         vnode_pager_setsize(vp, newsize);
652         node->nn_flags |= IN_CHANGE | IN_UPDATE;
653         nandfs_itimes(vp);
654
655         return (error);
656 }
657
658 static void
659 nandfs_itimes_locked(struct vnode *vp)
660 {
661         struct nandfs_node *node;
662         struct nandfs_inode *inode;
663         struct timespec ts;
664
665         ASSERT_VI_LOCKED(vp, __func__);
666
667         node = VTON(vp);
668         inode = &node->nn_inode;
669
670         if ((node->nn_flags & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0)
671                 return;
672
673         if (((vp->v_mount->mnt_kern_flag &
674             (MNTK_SUSPENDED | MNTK_SUSPEND)) == 0) ||
675             (node->nn_flags & (IN_CHANGE | IN_UPDATE)))
676                 node->nn_flags |= IN_MODIFIED;
677
678         vfs_timestamp(&ts);
679         if (node->nn_flags & IN_UPDATE) {
680                 inode->i_mtime = ts.tv_sec;
681                 inode->i_mtime_nsec = ts.tv_nsec;
682         }
683         if (node->nn_flags & IN_CHANGE) {
684                 inode->i_ctime = ts.tv_sec;
685                 inode->i_ctime_nsec = ts.tv_nsec;
686         }
687
688         node->nn_flags &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE);
689 }
690
691 void
692 nandfs_itimes(struct vnode *vp)
693 {
694
695         VI_LOCK(vp);
696         nandfs_itimes_locked(vp);
697         VI_UNLOCK(vp);
698 }
699
700 static int
701 nandfs_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td)
702 {
703         struct nandfs_node *node = VTON(vp);
704         struct nandfs_inode *inode = &node->nn_inode;
705         uint16_t nmode;
706         int error = 0;
707
708         DPRINTF(VNCALL, ("%s: vp %p, mode %x, cred %p, td %p\n", __func__, vp,
709             mode, cred, td));
710         /*
711          * To modify the permissions on a file, must possess VADMIN
712          * for that file.
713          */
714         if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
715                 return (error);
716
717         /*
718          * Privileged processes may set the sticky bit on non-directories,
719          * as well as set the setgid bit on a file with a group that the
720          * process is not a member of. Both of these are allowed in
721          * jail(8).
722          */
723         if (vp->v_type != VDIR && (mode & S_ISTXT)) {
724                 if (priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0))
725                         return (EFTYPE);
726         }
727         if (!groupmember(inode->i_gid, cred) && (mode & ISGID)) {
728                 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0);
729                 if (error)
730                         return (error);
731         }
732
733         /*
734          * Deny setting setuid if we are not the file owner.
735          */
736         if ((mode & ISUID) && inode->i_uid != cred->cr_uid) {
737                 error = priv_check_cred(cred, PRIV_VFS_ADMIN, 0);
738                 if (error)
739                         return (error);
740         }
741
742         nmode = inode->i_mode;
743         nmode &= ~ALLPERMS;
744         nmode |= (mode & ALLPERMS);
745         inode->i_mode = nmode;
746         node->nn_flags |= IN_CHANGE;
747
748         DPRINTF(VNCALL, ("%s: to mode %x\n", __func__, nmode));
749
750         return (error);
751 }
752
753 static int
754 nandfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
755     struct thread *td)
756 {
757         struct nandfs_node *node = VTON(vp);
758         struct nandfs_inode *inode = &node->nn_inode;
759         uid_t ouid;
760         gid_t ogid;
761         int error = 0;
762
763         if (uid == (uid_t)VNOVAL)
764                 uid = inode->i_uid;
765         if (gid == (gid_t)VNOVAL)
766                 gid = inode->i_gid;
767         /*
768          * To modify the ownership of a file, must possess VADMIN for that
769          * file.
770          */
771         if ((error = VOP_ACCESSX(vp, VWRITE_OWNER, cred, td)))
772                 return (error);
773         /*
774          * To change the owner of a file, or change the group of a file to a
775          * group of which we are not a member, the caller must have
776          * privilege.
777          */
778         if (((uid != inode->i_uid && uid != cred->cr_uid) ||
779             (gid != inode->i_gid && !groupmember(gid, cred))) &&
780             (error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0)))
781                 return (error);
782         ogid = inode->i_gid;
783         ouid = inode->i_uid;
784
785         inode->i_gid = gid;
786         inode->i_uid = uid;
787
788         node->nn_flags |= IN_CHANGE;
789         if ((inode->i_mode & (ISUID | ISGID)) &&
790             (ouid != uid || ogid != gid)) {
791                 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0))
792                         inode->i_mode &= ~(ISUID | ISGID);
793         }
794         DPRINTF(VNCALL, ("%s: vp %p, cred %p, td %p - ret OK\n", __func__, vp,
795             cred, td));
796         return (0);
797 }
798
799 static int
800 nandfs_setattr(struct vop_setattr_args *ap)
801 {
802         struct vnode *vp = ap->a_vp;
803         struct nandfs_node *node = VTON(vp);
804         struct nandfs_inode *inode = &node->nn_inode;
805         struct vattr *vap = ap->a_vap;
806         struct ucred *cred = ap->a_cred;
807         struct thread *td = curthread;
808         uint32_t flags;
809         int error = 0;
810
811         if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
812             (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
813             (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
814             (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
815                 DPRINTF(VNCALL, ("%s: unsettable attribute\n", __func__));
816                 return (EINVAL);
817         }
818
819         if (vap->va_flags != VNOVAL) {
820                 DPRINTF(VNCALL, ("%s: vp:%p td:%p flags:%lx\n", __func__, vp,
821                     td, vap->va_flags));
822
823                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
824                         return (EROFS);
825                 /*
826                  * Callers may only modify the file flags on objects they
827                  * have VADMIN rights for.
828                  */
829                 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)))
830                         return (error);
831                 /*
832                  * Unprivileged processes are not permitted to unset system
833                  * flags, or modify flags if any system flags are set.
834                  * Privileged non-jail processes may not modify system flags
835                  * if securelevel > 0 and any existing system flags are set.
836                  * Privileged jail processes behave like privileged non-jail
837                  * processes if the security.jail.chflags_allowed sysctl is
838                  * is non-zero; otherwise, they behave like unprivileged
839                  * processes.
840                  */
841
842                 flags = inode->i_flags;
843                 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) {
844                         if (flags & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) {
845                                 error = securelevel_gt(cred, 0);
846                                 if (error)
847                                         return (error);
848                         }
849                         /* Snapshot flag cannot be set or cleared */
850                         if (((vap->va_flags & SF_SNAPSHOT) != 0 &&
851                             (flags & SF_SNAPSHOT) == 0) ||
852                             ((vap->va_flags & SF_SNAPSHOT) == 0 &&
853                             (flags & SF_SNAPSHOT) != 0))
854                                 return (EPERM);
855
856                         inode->i_flags = vap->va_flags;
857                 } else {
858                         if (flags & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) ||
859                             (vap->va_flags & UF_SETTABLE) != vap->va_flags)
860                                 return (EPERM);
861
862                         flags &= SF_SETTABLE;
863                         flags |= (vap->va_flags & UF_SETTABLE);
864                         inode->i_flags = flags;
865                 }
866                 node->nn_flags |= IN_CHANGE;
867                 if (vap->va_flags & (IMMUTABLE | APPEND))
868                         return (0);
869         }
870         if (inode->i_flags & (IMMUTABLE | APPEND))
871                 return (EPERM);
872
873         if (vap->va_size != (u_quad_t)VNOVAL) {
874                 DPRINTF(VNCALL, ("%s: vp:%p td:%p size:%jx\n", __func__, vp, td,
875                     (uintmax_t)vap->va_size));
876
877                 switch (vp->v_type) {
878                 case VDIR:
879                         return (EISDIR);
880                 case VLNK:
881                 case VREG:
882                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
883                                 return (EROFS);
884                         if ((inode->i_flags & SF_SNAPSHOT) != 0)
885                                 return (EPERM);
886                         break;
887                 default:
888                         return (0);
889                 }
890
891                 if (vap->va_size > node->nn_nandfsdev->nd_maxfilesize)
892                         return (EFBIG);
893
894                 KASSERT((vp->v_type == VREG), ("Set size %d", vp->v_type));
895                 nandfs_truncate(vp, vap->va_size);
896                 node->nn_flags |= IN_CHANGE;
897
898                 return (0);
899         }
900
901         if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
902                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
903                         return (EROFS);
904                 DPRINTF(VNCALL, ("%s: vp:%p td:%p uid/gid %x/%x\n", __func__,
905                     vp, td, vap->va_uid, vap->va_gid));
906                 error = nandfs_chown(vp, vap->va_uid, vap->va_gid, cred, td);
907                 if (error)
908                         return (error);
909         }
910
911         if (vap->va_mode != (mode_t)VNOVAL) {
912                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
913                         return (EROFS);
914                 DPRINTF(VNCALL, ("%s: vp:%p td:%p mode %x\n", __func__, vp, td,
915                     vap->va_mode));
916
917                 error = nandfs_chmod(vp, (int)vap->va_mode, cred, td);
918                 if (error)
919                         return (error);
920         }
921         if (vap->va_atime.tv_sec != VNOVAL ||
922             vap->va_mtime.tv_sec != VNOVAL ||
923             vap->va_birthtime.tv_sec != VNOVAL) {
924                 DPRINTF(VNCALL, ("%s: vp:%p td:%p time a/m/b %jx/%jx/%jx\n",
925                     __func__, vp, td, (uintmax_t)vap->va_atime.tv_sec,
926                     (uintmax_t)vap->va_mtime.tv_sec,
927                     (uintmax_t)vap->va_birthtime.tv_sec));
928
929                 if (vap->va_atime.tv_sec != VNOVAL)
930                         node->nn_flags |= IN_ACCESS;
931                 if (vap->va_mtime.tv_sec != VNOVAL)
932                         node->nn_flags |= IN_CHANGE | IN_UPDATE;
933                 if (vap->va_birthtime.tv_sec != VNOVAL)
934                         node->nn_flags |= IN_MODIFIED;
935                 nandfs_itimes(vp);
936                 return (0);
937         }
938
939         return (0);
940 }
941
942 static int
943 nandfs_open(struct vop_open_args *ap)
944 {
945         struct nandfs_node *node = VTON(ap->a_vp);
946         uint64_t filesize;
947
948         DPRINTF(VNCALL, ("nandfs_open called ap->a_mode %x\n", ap->a_mode));
949
950         if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
951                 return (EOPNOTSUPP);
952
953         if ((node->nn_inode.i_flags & APPEND) &&
954             (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE)
955                 return (EPERM);
956
957         filesize = node->nn_inode.i_size;
958         vnode_create_vobject(ap->a_vp, filesize, ap->a_td);
959
960         return (0);
961 }
962
963 static int
964 nandfs_close(struct vop_close_args *ap)
965 {
966         struct vnode *vp = ap->a_vp;
967         struct nandfs_node *node = VTON(vp);
968
969         DPRINTF(VNCALL, ("%s: vp %p node %p\n", __func__, vp, node));
970
971         mtx_lock(&vp->v_interlock);
972         if (vp->v_usecount > 1)
973                 nandfs_itimes_locked(vp);
974         mtx_unlock(&vp->v_interlock);
975
976         return (0);
977 }
978
979 static int
980 nandfs_check_possible(struct vnode *vp, struct vattr *vap, mode_t mode)
981 {
982
983         /* Check if we are allowed to write */
984         switch (vap->va_type) {
985         case VDIR:
986         case VLNK:
987         case VREG:
988                 /*
989                  * Normal nodes: check if we're on a read-only mounted
990                  * filingsystem and bomb out if we're trying to write.
991                  */
992                 if ((mode & VMODIFY_PERMS) && (vp->v_mount->mnt_flag & MNT_RDONLY))
993                         return (EROFS);
994                 break;
995         case VBLK:
996         case VCHR:
997         case VSOCK:
998         case VFIFO:
999                 /*
1000                  * Special nodes: even on read-only mounted filingsystems
1001                  * these are allowed to be written to if permissions allow.
1002                  */
1003                 break;
1004         default:
1005                 /* No idea what this is */
1006                 return (EINVAL);
1007         }
1008
1009         /* No one may write immutable files */
1010         if ((mode & VWRITE) && (VTON(vp)->nn_inode.i_flags & IMMUTABLE))
1011                 return (EPERM);
1012
1013         return (0);
1014 }
1015
1016 static int
1017 nandfs_check_permitted(struct vnode *vp, struct vattr *vap, mode_t mode,
1018     struct ucred *cred)
1019 {
1020
1021         return (vaccess(vp->v_type, vap->va_mode, vap->va_uid, vap->va_gid, mode,
1022             cred, NULL));
1023 }
1024
1025 static int
1026 nandfs_advlock(struct vop_advlock_args *ap)
1027 {
1028         struct nandfs_node *nvp;
1029         quad_t size;
1030
1031         nvp = VTON(ap->a_vp);
1032         size = nvp->nn_inode.i_size;
1033         return (lf_advlock(ap, &(nvp->nn_lockf), size));
1034 }
1035
1036 static int
1037 nandfs_access(struct vop_access_args *ap)
1038 {
1039         struct vnode *vp = ap->a_vp;
1040         accmode_t accmode = ap->a_accmode;
1041         struct ucred *cred = ap->a_cred;
1042         struct vattr vap;
1043         int error;
1044
1045         DPRINTF(VNCALL, ("%s: vp:%p mode: %x\n", __func__, vp, accmode));
1046
1047         error = VOP_GETATTR(vp, &vap, NULL);
1048         if (error)
1049                 return (error);
1050
1051         error = nandfs_check_possible(vp, &vap, accmode);
1052         if (error)
1053                 return (error);
1054
1055         error = nandfs_check_permitted(vp, &vap, accmode, cred);
1056
1057         return (error);
1058 }
1059
1060 static int
1061 nandfs_print(struct vop_print_args *ap)
1062 {
1063         struct vnode *vp = ap->a_vp;
1064         struct nandfs_node *nvp = VTON(vp);
1065
1066         printf("\tvp=%p, nandfs_node=%p\n", vp, nvp);
1067         printf("nandfs inode %#jx\n", (uintmax_t)nvp->nn_ino);
1068         printf("flags = 0x%b\n", (u_int)nvp->nn_flags, PRINT_NODE_FLAGS);
1069
1070         return (0);
1071 }
1072
1073 static void
1074 nandfs_read_filebuf(struct nandfs_node *node, struct buf *bp)
1075 {
1076         struct nandfs_device *nandfsdev = node->nn_nandfsdev;
1077         struct buf *nbp;
1078         nandfs_daddr_t vblk, pblk;
1079         nandfs_lbn_t from;
1080         uint32_t blocksize;
1081         int error = 0;
1082         int blk2dev = nandfsdev->nd_blocksize / DEV_BSIZE;
1083
1084         /*
1085          * Translate all the block sectors into a series of buffers to read
1086          * asynchronously from the nandfs device. Note that this lookup may
1087          * induce readin's too.
1088          */
1089
1090         blocksize = nandfsdev->nd_blocksize;
1091         if (bp->b_bcount / blocksize != 1)
1092                 panic("invalid b_count in bp %p\n", bp);
1093
1094         from = bp->b_blkno;
1095
1096         DPRINTF(READ, ("\tread in from inode %#jx blkno %#jx"
1097             " count %#lx\n", (uintmax_t)node->nn_ino, from,
1098             bp->b_bcount));
1099
1100         /* Get virtual block numbers for the vnode's buffer span */
1101         error = nandfs_bmap_lookup(node, from, &vblk);
1102         if (error) {
1103                 bp->b_error = EINVAL;
1104                 bp->b_ioflags |= BIO_ERROR;
1105                 bufdone(bp);
1106                 return;
1107         }
1108
1109         /* Translate virtual block numbers to physical block numbers */
1110         error = nandfs_vtop(node, vblk, &pblk);
1111         if (error) {
1112                 bp->b_error = EINVAL;
1113                 bp->b_ioflags |= BIO_ERROR;
1114                 bufdone(bp);
1115                 return;
1116         }
1117
1118         /* Issue translated blocks */
1119         bp->b_resid = bp->b_bcount;
1120
1121         /* Note virtual block 0 marks not mapped */
1122         if (vblk == 0) {
1123                 vfs_bio_clrbuf(bp);
1124                 bufdone(bp);
1125                 return;
1126         }
1127
1128         nbp = bp;
1129         nbp->b_blkno = pblk * blk2dev;
1130         bp->b_iooffset = dbtob(nbp->b_blkno);
1131         MPASS(bp->b_iooffset >= 0);
1132         BO_STRATEGY(&nandfsdev->nd_devvp->v_bufobj, nbp);
1133         nandfs_vblk_set(bp, vblk);
1134         DPRINTF(READ, ("read_filebuf : ino %#jx blk %#jx -> "
1135             "%#jx -> %#jx [bp %p]\n", (uintmax_t)node->nn_ino,
1136             (uintmax_t)(from), (uintmax_t)vblk,
1137             (uintmax_t)pblk, nbp));
1138 }
1139
1140 static void
1141 nandfs_write_filebuf(struct nandfs_node *node, struct buf *bp)
1142 {
1143         struct nandfs_device *nandfsdev = node->nn_nandfsdev;
1144
1145         bp->b_iooffset = dbtob(bp->b_blkno);
1146         MPASS(bp->b_iooffset >= 0);
1147         BO_STRATEGY(&nandfsdev->nd_devvp->v_bufobj, bp);
1148 }
1149
1150 static int
1151 nandfs_strategy(struct vop_strategy_args *ap)
1152 {
1153         struct vnode *vp = ap->a_vp;
1154         struct buf *bp = ap->a_bp;
1155         struct nandfs_node *node = VTON(vp);
1156
1157
1158         /* check if we ought to be here */
1159         KASSERT((vp->v_type != VBLK && vp->v_type != VCHR),
1160             ("nandfs_strategy on type %d", vp->v_type));
1161
1162         /* Translate if needed and pass on */
1163         if (bp->b_iocmd == BIO_READ) {
1164                 nandfs_read_filebuf(node, bp);
1165                 return (0);
1166         }
1167
1168         /* Send to segment collector */
1169         nandfs_write_filebuf(node, bp);
1170         return (0);
1171 }
1172
1173 static int
1174 nandfs_readdir(struct vop_readdir_args *ap)
1175 {
1176         struct uio *uio = ap->a_uio;
1177         struct vnode *vp = ap->a_vp;
1178         struct nandfs_node *node = VTON(vp);
1179         struct nandfs_dir_entry *ndirent;
1180         struct dirent dirent;
1181         struct buf *bp;
1182         uint64_t file_size, diroffset, transoffset, blkoff;
1183         uint64_t blocknr;
1184         uint32_t blocksize = node->nn_nandfsdev->nd_blocksize;
1185         uint8_t *pos, name_len;
1186         int error;
1187
1188         DPRINTF(READDIR, ("nandfs_readdir called\n"));
1189
1190         if (vp->v_type != VDIR)
1191                 return (ENOTDIR);
1192
1193         file_size = node->nn_inode.i_size;
1194         DPRINTF(READDIR, ("nandfs_readdir filesize %jd resid %zd\n",
1195             (uintmax_t)file_size, uio->uio_resid ));
1196
1197         /* We are called just as long as we keep on pushing data in */
1198         error = 0;
1199         if ((uio->uio_offset < file_size) &&
1200             (uio->uio_resid >= sizeof(struct dirent))) {
1201                 diroffset = uio->uio_offset;
1202                 transoffset = diroffset;
1203
1204                 blocknr = diroffset / blocksize;
1205                 blkoff = diroffset % blocksize;
1206                 error = nandfs_bread(node, blocknr, NOCRED, 0, &bp);
1207                 if (error) {
1208                         brelse(bp);
1209                         return (EIO);
1210                 }
1211                 while (diroffset < file_size) {
1212                         DPRINTF(READDIR, ("readdir : offset = %"PRIu64"\n",
1213                             diroffset));
1214                         if (blkoff >= blocksize) {
1215                                 blkoff = 0; blocknr++;
1216                                 brelse(bp);
1217                                 error = nandfs_bread(node, blocknr, NOCRED, 0,
1218                                     &bp);
1219                                 if (error) {
1220                                         brelse(bp);
1221                                         return (EIO);
1222                                 }
1223                         }
1224
1225                         /* Read in one dirent */
1226                         pos = (uint8_t *)bp->b_data + blkoff;
1227                         ndirent = (struct nandfs_dir_entry *)pos;
1228
1229                         name_len = ndirent->name_len;
1230                         memset(&dirent, 0, sizeof(struct dirent));
1231                         dirent.d_fileno = ndirent->inode;
1232                         if (dirent.d_fileno) {
1233                                 dirent.d_type = ndirent->file_type;
1234                                 dirent.d_namlen = name_len;
1235                                 strncpy(dirent.d_name, ndirent->name, name_len);
1236                                 dirent.d_reclen = GENERIC_DIRSIZ(&dirent);
1237                                 DPRINTF(READDIR, ("copying `%*.*s`\n", name_len,
1238                                     name_len, dirent.d_name));
1239                         }
1240
1241                         /*
1242                          * If there isn't enough space in the uio to return a
1243                          * whole dirent, break off read
1244                          */
1245                         if (uio->uio_resid < GENERIC_DIRSIZ(&dirent))
1246                                 break;
1247
1248                         /* Transfer */
1249                         if (dirent.d_fileno)
1250                                 uiomove(&dirent, GENERIC_DIRSIZ(&dirent), uio);
1251
1252                         /* Advance */
1253                         diroffset += ndirent->rec_len;
1254                         blkoff += ndirent->rec_len;
1255
1256                         /* Remember the last entry we transferred */
1257                         transoffset = diroffset;
1258                 }
1259                 brelse(bp);
1260
1261                 /* Pass on last transferred offset */
1262                 uio->uio_offset = transoffset;
1263         }
1264
1265         if (ap->a_eofflag)
1266                 *ap->a_eofflag = (uio->uio_offset >= file_size);
1267
1268         return (error);
1269 }
1270
1271 static int
1272 nandfs_dirempty(struct vnode *dvp, uint64_t parentino, struct ucred *cred)
1273 {
1274         struct nandfs_node *dnode = VTON(dvp);
1275         struct nandfs_dir_entry *dirent;
1276         uint64_t file_size = dnode->nn_inode.i_size;
1277         uint64_t blockcount = dnode->nn_inode.i_blocks;
1278         uint64_t blocknr;
1279         uint32_t blocksize = dnode->nn_nandfsdev->nd_blocksize;
1280         uint32_t limit;
1281         uint32_t off;
1282         uint8_t *pos;
1283         struct buf *bp;
1284         int error;
1285
1286         DPRINTF(LOOKUP, ("%s: dvp %p parentino %#jx cred %p\n", __func__, dvp,
1287             (uintmax_t)parentino, cred));
1288
1289         KASSERT((file_size != 0), ("nandfs_dirempty for NULL dir %p", dvp));
1290
1291         blocknr = 0;
1292         while (blocknr < blockcount) {
1293                 error = nandfs_bread(dnode, blocknr, NOCRED, 0, &bp);
1294                 if (error) {
1295                         brelse(bp);
1296                         return (0);
1297                 }
1298
1299                 pos = (uint8_t *)bp->b_data;
1300                 off = 0;
1301
1302                 if (blocknr == (blockcount - 1))
1303                         limit = file_size % blocksize;
1304                 else
1305                         limit = blocksize;
1306
1307                 while (off < limit) {
1308                         dirent = (struct nandfs_dir_entry *)(pos + off);
1309                         off += dirent->rec_len;
1310
1311                         if (dirent->inode == 0)
1312                                 continue;
1313
1314                         switch (dirent->name_len) {
1315                         case 0:
1316                                 break;
1317                         case 1:
1318                                 if (dirent->name[0] != '.')
1319                                         goto notempty;
1320
1321                                 KASSERT(dirent->inode == dnode->nn_ino,
1322                                     (".'s inode does not match dir"));
1323                                 break;
1324                         case 2:
1325                                 if (dirent->name[0] != '.' &&
1326                                     dirent->name[1] != '.')
1327                                         goto notempty;
1328
1329                                 KASSERT(dirent->inode == parentino,
1330                                     ("..'s inode does not match parent"));
1331                                 break;
1332                         default:
1333                                 goto notempty;
1334                         }
1335                 }
1336
1337                 brelse(bp);
1338                 blocknr++;
1339         }
1340
1341         return (1);
1342 notempty:
1343         brelse(bp);
1344         return (0);
1345 }
1346
1347 static int
1348 nandfs_link(struct vop_link_args *ap)
1349 {
1350         struct vnode *tdvp = ap->a_tdvp;
1351         struct vnode *vp = ap->a_vp;
1352         struct componentname *cnp = ap->a_cnp;
1353         struct nandfs_node *node = VTON(vp);
1354         struct nandfs_inode *inode = &node->nn_inode;
1355         int error;
1356
1357         if (inode->i_links_count >= NANDFS_LINK_MAX)
1358                 return (EMLINK);
1359
1360         if (inode->i_flags & (IMMUTABLE | APPEND))
1361                 return (EPERM);
1362
1363         /* Update link count */
1364         inode->i_links_count++;
1365
1366         /* Add dir entry */
1367         error = nandfs_add_dirent(tdvp, node->nn_ino, cnp->cn_nameptr,
1368             cnp->cn_namelen, IFTODT(inode->i_mode));
1369         if (error) {
1370                 inode->i_links_count--;
1371         }
1372
1373         node->nn_flags |= IN_CHANGE;
1374         nandfs_itimes(vp);
1375         DPRINTF(VNCALL, ("%s: tdvp %p vp %p cnp %p\n",
1376             __func__, tdvp, vp, cnp));
1377
1378         return (0);
1379 }
1380
1381 static int
1382 nandfs_create(struct vop_create_args *ap)
1383 {
1384         struct vnode *dvp = ap->a_dvp;
1385         struct vnode **vpp = ap->a_vpp;
1386         struct componentname *cnp = ap->a_cnp;
1387         uint16_t mode = MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode);
1388         struct nandfs_node *dir_node = VTON(dvp);
1389         struct nandfsmount *nmp = dir_node->nn_nmp;
1390         struct nandfs_node *node;
1391         int error;
1392
1393         DPRINTF(VNCALL, ("%s: dvp %p\n", __func__, dvp));
1394
1395         if (nandfs_fs_full(dir_node->nn_nandfsdev))
1396                 return (ENOSPC);
1397
1398         /* Create new vnode/inode */
1399         error = nandfs_node_create(nmp, &node, mode);
1400         if (error)
1401                 return (error);
1402         node->nn_inode.i_gid = dir_node->nn_inode.i_gid;
1403         node->nn_inode.i_uid = cnp->cn_cred->cr_uid;
1404
1405         /* Add new dir entry */
1406         error = nandfs_add_dirent(dvp, node->nn_ino, cnp->cn_nameptr,
1407             cnp->cn_namelen, IFTODT(mode));
1408         if (error) {
1409                 if (nandfs_node_destroy(node)) {
1410                         nandfs_error("%s: error destroying node %p\n",
1411                             __func__, node);
1412                 }
1413                 return (error);
1414         }
1415         *vpp = NTOV(node);
1416         if ((cnp->cn_flags & MAKEENTRY) != 0)
1417                 cache_enter(dvp, *vpp, cnp);
1418
1419         DPRINTF(VNCALL, ("created file vp %p nandnode %p ino %jx\n", *vpp, node,
1420             (uintmax_t)node->nn_ino));
1421         return (0);
1422 }
1423
1424 static int
1425 nandfs_remove(struct vop_remove_args *ap)
1426 {
1427         struct vnode *vp = ap->a_vp;
1428         struct vnode *dvp = ap->a_dvp;
1429         struct nandfs_node *node = VTON(vp);
1430         struct nandfs_node *dnode = VTON(dvp);
1431         struct componentname *cnp = ap->a_cnp;
1432
1433         DPRINTF(VNCALL, ("%s: dvp %p vp %p nandnode %p ino %#jx link %d\n",
1434             __func__, dvp, vp, node, (uintmax_t)node->nn_ino,
1435             node->nn_inode.i_links_count));
1436
1437         if (vp->v_type == VDIR)
1438                 return (EISDIR);
1439
1440         /* Files marked as immutable or append-only cannot be deleted. */
1441         if ((node->nn_inode.i_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
1442             (dnode->nn_inode.i_flags & APPEND))
1443                 return (EPERM);
1444
1445         nandfs_remove_dirent(dvp, node, cnp);
1446         node->nn_inode.i_links_count--;
1447         node->nn_flags |= IN_CHANGE;
1448
1449         return (0);
1450 }
1451
1452 /*
1453  * Check if source directory is in the path of the target directory.
1454  * Target is supplied locked, source is unlocked.
1455  * The target is always vput before returning.
1456  */
1457 static int
1458 nandfs_checkpath(struct nandfs_node *src, struct nandfs_node *dest,
1459     struct ucred *cred)
1460 {
1461         struct vnode *vp;
1462         int error, rootino;
1463         struct nandfs_dir_entry dirent;
1464
1465         vp = NTOV(dest);
1466         if (src->nn_ino == dest->nn_ino) {
1467                 error = EEXIST;
1468                 goto out;
1469         }
1470         rootino = NANDFS_ROOT_INO;
1471         error = 0;
1472         if (dest->nn_ino == rootino)
1473                 goto out;
1474
1475         for (;;) {
1476                 if (vp->v_type != VDIR) {
1477                         error = ENOTDIR;
1478                         break;
1479                 }
1480
1481                 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirent,
1482                     NANDFS_DIR_REC_LEN(2), (off_t)0, UIO_SYSSPACE,
1483                     IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED,
1484                     NULL, NULL);
1485                 if (error != 0)
1486                         break;
1487                 if (dirent.name_len != 2 ||
1488                     dirent.name[0] != '.' ||
1489                     dirent.name[1] != '.') {
1490                         error = ENOTDIR;
1491                         break;
1492                 }
1493                 if (dirent.inode == src->nn_ino) {
1494                         error = EINVAL;
1495                         break;
1496                 }
1497                 if (dirent.inode == rootino)
1498                         break;
1499                 vput(vp);
1500                 if ((error = VFS_VGET(vp->v_mount, dirent.inode,
1501                     LK_EXCLUSIVE, &vp)) != 0) {
1502                         vp = NULL;
1503                         break;
1504                 }
1505         }
1506
1507 out:
1508         if (error == ENOTDIR)
1509                 printf("checkpath: .. not a directory\n");
1510         if (vp != NULL)
1511                 vput(vp);
1512         return (error);
1513 }
1514
1515 static int
1516 nandfs_rename(struct vop_rename_args *ap)
1517 {
1518         struct vnode *tvp = ap->a_tvp;
1519         struct vnode *tdvp = ap->a_tdvp;
1520         struct vnode *fvp = ap->a_fvp;
1521         struct vnode *fdvp = ap->a_fdvp;
1522         struct componentname *tcnp = ap->a_tcnp;
1523         struct componentname *fcnp = ap->a_fcnp;
1524         int doingdirectory = 0, oldparent = 0, newparent = 0;
1525         int error = 0;
1526
1527         struct nandfs_node *fdnode, *fnode, *fnode1;
1528         struct nandfs_node *tdnode = VTON(tdvp);
1529         struct nandfs_node *tnode;
1530
1531         uint32_t tdflags, fflags, fdflags;
1532         uint16_t mode;
1533
1534         DPRINTF(VNCALL, ("%s: fdvp:%p fvp:%p tdvp:%p tdp:%p\n", __func__, fdvp,
1535             fvp, tdvp, tvp));
1536
1537         /*
1538          * Check for cross-device rename.
1539          */
1540         if ((fvp->v_mount != tdvp->v_mount) ||
1541             (tvp && (fvp->v_mount != tvp->v_mount))) {
1542                 error = EXDEV;
1543 abortit:
1544                 if (tdvp == tvp)
1545                         vrele(tdvp);
1546                 else
1547                         vput(tdvp);
1548                 if (tvp)
1549                         vput(tvp);
1550                 vrele(fdvp);
1551                 vrele(fvp);
1552                 return (error);
1553         }
1554
1555         tdflags = tdnode->nn_inode.i_flags;
1556         if (tvp &&
1557             ((VTON(tvp)->nn_inode.i_flags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1558             (tdflags & APPEND))) {
1559                 error = EPERM;
1560                 goto abortit;
1561         }
1562
1563         /*
1564          * Renaming a file to itself has no effect.  The upper layers should
1565          * not call us in that case.  Temporarily just warn if they do.
1566          */
1567         if (fvp == tvp) {
1568                 printf("nandfs_rename: fvp == tvp (can't happen)\n");
1569                 error = 0;
1570                 goto abortit;
1571         }
1572
1573         if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
1574                 goto abortit;
1575
1576         fdnode = VTON(fdvp);
1577         fnode = VTON(fvp);
1578
1579         if (fnode->nn_inode.i_links_count >= NANDFS_LINK_MAX) {
1580                 VOP_UNLOCK(fvp, 0);
1581                 error = EMLINK;
1582                 goto abortit;
1583         }
1584
1585         fflags = fnode->nn_inode.i_flags;
1586         fdflags = fdnode->nn_inode.i_flags;
1587
1588         if ((fflags & (NOUNLINK | IMMUTABLE | APPEND)) ||
1589             (fdflags & APPEND)) {
1590                 VOP_UNLOCK(fvp, 0);
1591                 error = EPERM;
1592                 goto abortit;
1593         }
1594
1595         mode = fnode->nn_inode.i_mode;
1596         if ((mode & S_IFMT) == S_IFDIR) {
1597                 /*
1598                  * Avoid ".", "..", and aliases of "." for obvious reasons.
1599                  */
1600
1601                 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1602                     (fdvp == fvp) ||
1603                     ((fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT) ||
1604                     (fnode->nn_flags & IN_RENAME)) {
1605                         VOP_UNLOCK(fvp, 0);
1606                         error = EINVAL;
1607                         goto abortit;
1608                 }
1609                 fnode->nn_flags |= IN_RENAME;
1610                 doingdirectory = 1;
1611                 DPRINTF(VNCALL, ("%s: doingdirectory dvp %p\n", __func__,
1612                     tdvp));
1613                 oldparent = fdnode->nn_ino;
1614         }
1615
1616         vrele(fdvp);
1617
1618         tnode = NULL;
1619         if (tvp)
1620                 tnode = VTON(tvp);
1621
1622         /*
1623          * Bump link count on fvp while we are moving stuff around. If we
1624          * crash before completing the work, the link count may be wrong
1625          * but correctable.
1626          */
1627         fnode->nn_inode.i_links_count++;
1628
1629         /* Check for in path moving XXX */
1630         error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
1631         VOP_UNLOCK(fvp, 0);
1632         if (oldparent != tdnode->nn_ino)
1633                 newparent = tdnode->nn_ino;
1634         if (doingdirectory && newparent) {
1635                 if (error)      /* write access check above */
1636                         goto bad;
1637                 if (tnode != NULL)
1638                         vput(tvp);
1639
1640                 error = nandfs_checkpath(fnode, tdnode, tcnp->cn_cred);
1641                 if (error)
1642                         goto out;
1643
1644                 VREF(tdvp);
1645                 error = relookup(tdvp, &tvp, tcnp);
1646                 if (error)
1647                         goto out;
1648                 vrele(tdvp);
1649                 tdnode = VTON(tdvp);
1650                 tnode = NULL;
1651                 if (tvp)
1652                         tnode = VTON(tvp);
1653         }
1654
1655         /*
1656          * If the target doesn't exist, link the target to the source and
1657          * unlink the source. Otherwise, rewrite the target directory to
1658          * reference the source and remove the original entry.
1659          */
1660
1661         if (tvp == NULL) {
1662                 /*
1663                  * Account for ".." in new directory.
1664                  */
1665                 if (doingdirectory && fdvp != tdvp)
1666                         tdnode->nn_inode.i_links_count++;
1667
1668                 DPRINTF(VNCALL, ("%s: new entry in dvp:%p\n", __func__, tdvp));
1669                 /*
1670                  * Add name in new directory.
1671                  */
1672                 error = nandfs_add_dirent(tdvp, fnode->nn_ino, tcnp->cn_nameptr,
1673                     tcnp->cn_namelen, IFTODT(fnode->nn_inode.i_mode));
1674                 if (error) {
1675                         if (doingdirectory && fdvp != tdvp)
1676                                 tdnode->nn_inode.i_links_count--;
1677                         goto bad;
1678                 }
1679
1680                 vput(tdvp);
1681         } else {
1682                 /*
1683                  * If the parent directory is "sticky", then the user must
1684                  * own the parent directory, or the destination of the rename,
1685                  * otherwise the destination may not be changed (except by
1686                  * root). This implements append-only directories.
1687                  */
1688                 if ((tdnode->nn_inode.i_mode & S_ISTXT) &&
1689                     tcnp->cn_cred->cr_uid != 0 &&
1690                     tcnp->cn_cred->cr_uid != tdnode->nn_inode.i_uid &&
1691                     tnode->nn_inode.i_uid != tcnp->cn_cred->cr_uid) {
1692                         error = EPERM;
1693                         goto bad;
1694                 }
1695                 /*
1696                  * Target must be empty if a directory and have no links
1697                  * to it. Also, ensure source and target are compatible
1698                  * (both directories, or both not directories).
1699                  */
1700                 mode = tnode->nn_inode.i_mode;
1701                 if ((mode & S_IFMT) == S_IFDIR) {
1702                         if (!nandfs_dirempty(tvp, tdnode->nn_ino,
1703                             tcnp->cn_cred)) {
1704                                 error = ENOTEMPTY;
1705                                 goto bad;
1706                         }
1707                         if (!doingdirectory) {
1708                                 error = ENOTDIR;
1709                                 goto bad;
1710                         }
1711                         /*
1712                          * Update name cache since directory is going away.
1713                          */
1714                         cache_purge(tdvp);
1715                 } else if (doingdirectory) {
1716                         error = EISDIR;
1717                         goto bad;
1718                 }
1719
1720                 DPRINTF(VNCALL, ("%s: update entry dvp:%p\n", __func__, tdvp));
1721                 /*
1722                  * Change name tcnp in tdvp to point at fvp.
1723                  */
1724                 error = nandfs_update_dirent(tdvp, fnode, tnode);
1725                 if (error)
1726                         goto bad;
1727
1728                 if (doingdirectory && !newparent)
1729                         tdnode->nn_inode.i_links_count--;
1730
1731                 vput(tdvp);
1732
1733                 tnode->nn_inode.i_links_count--;
1734                 vput(tvp);
1735                 tnode = NULL;
1736         }
1737
1738         /*
1739          * Unlink the source.
1740          */
1741         fcnp->cn_flags &= ~MODMASK;
1742         fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1743         VREF(fdvp);
1744         error = relookup(fdvp, &fvp, fcnp);
1745         if (error == 0)
1746                 vrele(fdvp);
1747         if (fvp != NULL) {
1748                 fnode1 = VTON(fvp);
1749                 fdnode = VTON(fdvp);
1750         } else {
1751                 /*
1752                  * From name has disappeared.
1753                  */
1754                 if (doingdirectory)
1755                         panic("nandfs_rename: lost dir entry");
1756                 vrele(ap->a_fvp);
1757                 return (0);
1758         }
1759
1760         DPRINTF(VNCALL, ("%s: unlink source fnode:%p\n", __func__, fnode));
1761
1762         /*
1763          * Ensure that the directory entry still exists and has not
1764          * changed while the new name has been entered. If the source is
1765          * a file then the entry may have been unlinked or renamed. In
1766          * either case there is no further work to be done. If the source
1767          * is a directory then it cannot have been rmdir'ed; its link
1768          * count of three would cause a rmdir to fail with ENOTEMPTY.
1769          * The IN_RENAME flag ensures that it cannot be moved by another
1770          * rename.
1771          */
1772         if (fnode != fnode1) {
1773                 if (doingdirectory)
1774                         panic("nandfs: lost dir entry");
1775         } else {
1776                 /*
1777                  * If the source is a directory with a
1778                  * new parent, the link count of the old
1779                  * parent directory must be decremented
1780                  * and ".." set to point to the new parent.
1781                  */
1782                 if (doingdirectory && newparent) {
1783                         DPRINTF(VNCALL, ("%s: new parent %#jx -> %#jx\n",
1784                             __func__, (uintmax_t) oldparent,
1785                             (uintmax_t) newparent));
1786                         error = nandfs_update_parent_dir(fvp, newparent);
1787                         if (!error) {
1788                                 fdnode->nn_inode.i_links_count--;
1789                                 fdnode->nn_flags |= IN_CHANGE;
1790                         }
1791                 }
1792                 error = nandfs_remove_dirent(fdvp, fnode, fcnp);
1793                 if (!error) {
1794                         fnode->nn_inode.i_links_count--;
1795                         fnode->nn_flags |= IN_CHANGE;
1796                 }
1797                 fnode->nn_flags &= ~IN_RENAME;
1798         }
1799         if (fdnode)
1800                 vput(fdvp);
1801         if (fnode)
1802                 vput(fvp);
1803         vrele(ap->a_fvp);
1804         return (error);
1805
1806 bad:
1807         DPRINTF(VNCALL, ("%s: error:%d\n", __func__, error));
1808         if (tnode)
1809                 vput(NTOV(tnode));
1810         vput(NTOV(tdnode));
1811 out:
1812         if (doingdirectory)
1813                 fnode->nn_flags &= ~IN_RENAME;
1814         if (vn_lock(fvp, LK_EXCLUSIVE) == 0) {
1815                 fnode->nn_inode.i_links_count--;
1816                 fnode->nn_flags |= IN_CHANGE;
1817                 fnode->nn_flags &= ~IN_RENAME;
1818                 vput(fvp);
1819         } else
1820                 vrele(fvp);
1821         return (error);
1822 }
1823
1824 static int
1825 nandfs_mkdir(struct vop_mkdir_args *ap)
1826 {
1827         struct vnode *dvp = ap->a_dvp;
1828         struct vnode **vpp = ap->a_vpp;
1829         struct componentname *cnp = ap->a_cnp;
1830         struct nandfs_node *dir_node = VTON(dvp);
1831         struct nandfs_inode *dir_inode = &dir_node->nn_inode;
1832         struct nandfs_node *node;
1833         struct nandfsmount *nmp = dir_node->nn_nmp;
1834         uint16_t mode = MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode);
1835         int error;
1836
1837         DPRINTF(VNCALL, ("%s: dvp %p\n", __func__, dvp));
1838
1839         if (nandfs_fs_full(dir_node->nn_nandfsdev))
1840                 return (ENOSPC);
1841
1842         if (dir_inode->i_links_count >= NANDFS_LINK_MAX)
1843                 return (EMLINK);
1844
1845         error = nandfs_node_create(nmp, &node, mode);
1846         if (error)
1847                 return (error);
1848
1849         node->nn_inode.i_gid = dir_node->nn_inode.i_gid;
1850         node->nn_inode.i_uid = cnp->cn_cred->cr_uid;
1851
1852         *vpp = NTOV(node);
1853
1854         error = nandfs_add_dirent(dvp, node->nn_ino, cnp->cn_nameptr,
1855             cnp->cn_namelen, IFTODT(mode));
1856         if (error) {
1857                 vput(*vpp);
1858                 return (error);
1859         }
1860
1861         dir_node->nn_inode.i_links_count++;
1862         dir_node->nn_flags |= IN_CHANGE;
1863
1864         error = nandfs_init_dir(NTOV(node), node->nn_ino, dir_node->nn_ino);
1865         if (error) {
1866                 vput(NTOV(node));
1867                 return (error);
1868         }
1869
1870         DPRINTF(VNCALL, ("created dir vp %p nandnode %p ino %jx\n", *vpp, node,
1871             (uintmax_t)node->nn_ino));
1872         return (0);
1873 }
1874
1875 static int
1876 nandfs_mknod(struct vop_mknod_args *ap)
1877 {
1878         struct vnode *dvp = ap->a_dvp;
1879         struct vnode **vpp = ap->a_vpp;
1880         struct vattr *vap = ap->a_vap;
1881         uint16_t mode = MAKEIMODE(vap->va_type, vap->va_mode);
1882         struct componentname *cnp = ap->a_cnp;
1883         struct nandfs_node *dir_node = VTON(dvp);
1884         struct nandfsmount *nmp = dir_node->nn_nmp;
1885         struct nandfs_node *node;
1886         int error;
1887
1888         if (nandfs_fs_full(dir_node->nn_nandfsdev))
1889                 return (ENOSPC);
1890
1891         error = nandfs_node_create(nmp, &node, mode);
1892         if (error)
1893                 return (error);
1894         node->nn_inode.i_gid = dir_node->nn_inode.i_gid;
1895         node->nn_inode.i_uid = cnp->cn_cred->cr_uid;
1896         if (vap->va_rdev != VNOVAL)
1897                 node->nn_inode.i_special = vap->va_rdev;
1898
1899         *vpp = NTOV(node);
1900
1901         if (nandfs_add_dirent(dvp, node->nn_ino, cnp->cn_nameptr,
1902             cnp->cn_namelen, IFTODT(mode))) {
1903                 vput(*vpp);
1904                 return (ENOTDIR);
1905         }
1906
1907         node->nn_flags |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
1908
1909         return (0);
1910 }
1911
1912 static int
1913 nandfs_symlink(struct vop_symlink_args *ap)
1914 {
1915         struct vnode **vpp = ap->a_vpp;
1916         struct vnode *dvp = ap->a_dvp;
1917         uint16_t mode = MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode);
1918         struct componentname *cnp = ap->a_cnp;
1919         struct nandfs_node *dir_node = VTON(dvp);
1920         struct nandfsmount *nmp = dir_node->nn_nmp;
1921         struct nandfs_node *node;
1922         int len, error;
1923
1924         if (nandfs_fs_full(dir_node->nn_nandfsdev))
1925                 return (ENOSPC);
1926
1927         error = nandfs_node_create(nmp, &node, S_IFLNK | mode);
1928         if (error)
1929                 return (error);
1930         node->nn_inode.i_gid = dir_node->nn_inode.i_gid;
1931         node->nn_inode.i_uid = cnp->cn_cred->cr_uid;
1932
1933         *vpp = NTOV(node);
1934
1935         if (nandfs_add_dirent(dvp, node->nn_ino, cnp->cn_nameptr,
1936             cnp->cn_namelen, IFTODT(mode))) {
1937                 vput(*vpp);
1938                 return (ENOTDIR);
1939         }
1940
1941
1942         len = strlen(ap->a_target);
1943         error = vn_rdwr(UIO_WRITE, *vpp, ap->a_target, len, (off_t)0,
1944             UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK,
1945             cnp->cn_cred, NOCRED, NULL, NULL);
1946         if (error)
1947                 vput(*vpp);
1948
1949         return (error);
1950 }
1951
1952 static int
1953 nandfs_readlink(struct vop_readlink_args *ap)
1954 {
1955         struct vnode *vp = ap->a_vp;
1956
1957         return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred));
1958 }
1959
1960 static int
1961 nandfs_rmdir(struct vop_rmdir_args *ap)
1962 {
1963         struct vnode *vp = ap->a_vp;
1964         struct vnode *dvp = ap->a_dvp;
1965         struct componentname *cnp = ap->a_cnp;
1966         struct nandfs_node *node, *dnode;
1967         uint32_t dflag, flag;
1968         int error = 0;
1969
1970         node = VTON(vp);
1971         dnode = VTON(dvp);
1972
1973         /* Files marked as immutable or append-only cannot be deleted. */
1974         if ((node->nn_inode.i_flags & (IMMUTABLE | APPEND | NOUNLINK)) ||
1975             (dnode->nn_inode.i_flags & APPEND))
1976                 return (EPERM);
1977
1978         DPRINTF(VNCALL, ("%s: dvp %p vp %p nandnode %p ino %#jx\n", __func__,
1979             dvp, vp, node, (uintmax_t)node->nn_ino));
1980
1981         if (node->nn_inode.i_links_count < 2)
1982                 return (EINVAL);
1983
1984         if (!nandfs_dirempty(vp, dnode->nn_ino, cnp->cn_cred))
1985                 return (ENOTEMPTY);
1986
1987         /* Files marked as immutable or append-only cannot be deleted. */
1988         dflag = dnode->nn_inode.i_flags;
1989         flag = node->nn_inode.i_flags;
1990         if ((dflag & APPEND) ||
1991             (flag & (NOUNLINK | IMMUTABLE | APPEND))) {
1992                 return (EPERM);
1993         }
1994
1995         if (vp->v_mountedhere != 0)
1996                 return (EINVAL);
1997
1998         nandfs_remove_dirent(dvp, node, cnp);
1999         dnode->nn_inode.i_links_count -= 1;
2000         dnode->nn_flags |= IN_CHANGE;
2001
2002         cache_purge(dvp);
2003
2004         error = nandfs_truncate(vp, (uint64_t)0);
2005         if (error)
2006                 return (error);
2007
2008         node->nn_inode.i_links_count -= 2;
2009         node->nn_flags |= IN_CHANGE;
2010
2011         cache_purge(vp);
2012
2013         return (error);
2014 }
2015
2016 static int
2017 nandfs_fsync(struct vop_fsync_args *ap)
2018 {
2019         struct vnode *vp = ap->a_vp;
2020         struct nandfs_node *node = VTON(vp);
2021         int locked;
2022
2023         DPRINTF(VNCALL, ("%s: vp %p nandnode %p ino %#jx\n", __func__, vp,
2024             node, (uintmax_t)node->nn_ino));
2025
2026         /*
2027          * Start syncing vnode only if inode was modified or
2028          * there are some dirty buffers
2029          */
2030         if (VTON(vp)->nn_flags & IN_MODIFIED ||
2031             vp->v_bufobj.bo_dirty.bv_cnt) {
2032                 locked = VOP_ISLOCKED(vp);
2033                 VOP_UNLOCK(vp, 0);
2034                 nandfs_wakeup_wait_sync(node->nn_nandfsdev, SYNCER_FSYNC);
2035                 VOP_LOCK(vp, locked | LK_RETRY);
2036         }
2037
2038         return (0);
2039 }
2040
2041 static int
2042 nandfs_bmap(struct vop_bmap_args *ap)
2043 {
2044         struct vnode *vp = ap->a_vp;
2045         struct nandfs_node *nnode = VTON(vp);
2046         struct nandfs_device *nandfsdev = nnode->nn_nandfsdev;
2047         nandfs_daddr_t l2vmap, v2pmap;
2048         int error;
2049         int blk2dev = nandfsdev->nd_blocksize / DEV_BSIZE;
2050
2051         DPRINTF(VNCALL, ("%s: vp %p nandnode %p ino %#jx\n", __func__, vp,
2052             nnode, (uintmax_t)nnode->nn_ino));
2053
2054         if (ap->a_bop != NULL)
2055                 *ap->a_bop = &nandfsdev->nd_devvp->v_bufobj;
2056         if (ap->a_bnp == NULL)
2057                 return (0);
2058         if (ap->a_runp != NULL)
2059                 *ap->a_runp = 0;
2060         if (ap->a_runb != NULL)
2061                 *ap->a_runb = 0;
2062
2063         /*
2064          * Translate all the block sectors into a series of buffers to read
2065          * asynchronously from the nandfs device. Note that this lookup may
2066          * induce readin's too.
2067          */
2068
2069         /* Get virtual block numbers for the vnode's buffer span */
2070         error = nandfs_bmap_lookup(nnode, ap->a_bn, &l2vmap);
2071         if (error)
2072                 return (-1);
2073
2074         /* Translate virtual block numbers to physical block numbers */
2075         error = nandfs_vtop(nnode, l2vmap, &v2pmap);
2076         if (error)
2077                 return (-1);
2078
2079         /* Note virtual block 0 marks not mapped */
2080         if (l2vmap == 0)
2081                 *ap->a_bnp = -1;
2082         else
2083                 *ap->a_bnp = v2pmap * blk2dev;  /* in DEV_BSIZE */
2084
2085         DPRINTF(VNCALL, ("%s: vp %p nandnode %p ino %#jx lblk %jx -> blk %jx\n",
2086             __func__, vp, nnode, (uintmax_t)nnode->nn_ino, (uintmax_t)ap->a_bn,
2087             (uintmax_t)*ap->a_bnp ));
2088
2089         return (0);
2090 }
2091
2092 static void
2093 nandfs_force_syncer(struct nandfsmount *nmp)
2094 {
2095
2096         nmp->nm_flags |= NANDFS_FORCE_SYNCER;
2097         nandfs_wakeup_wait_sync(nmp->nm_nandfsdev, SYNCER_FFORCE);
2098 }
2099
2100 static int
2101 nandfs_ioctl(struct vop_ioctl_args *ap)
2102 {
2103         struct vnode *vp = ap->a_vp;
2104         u_long command = ap->a_command;
2105         caddr_t data = ap->a_data;
2106         struct nandfs_node *node = VTON(vp);
2107         struct nandfs_device *nandfsdev = node->nn_nandfsdev;
2108         struct nandfsmount *nmp = node->nn_nmp;
2109         uint64_t *tab, *cno;
2110         struct nandfs_seg_stat *nss;
2111         struct nandfs_cpmode *ncpm;
2112         struct nandfs_argv *nargv;
2113         struct nandfs_cpstat *ncp;
2114         int error;
2115
2116         DPRINTF(VNCALL, ("%s: %x\n", __func__, (uint32_t)command));
2117
2118         error = priv_check(ap->a_td, PRIV_VFS_MOUNT);
2119         if (error)
2120                 return (error);
2121
2122         if (nmp->nm_ronly) {
2123                 switch (command) {
2124                 case NANDFS_IOCTL_GET_FSINFO:
2125                 case NANDFS_IOCTL_GET_SUSTAT:
2126                 case NANDFS_IOCTL_GET_CPINFO:
2127                 case NANDFS_IOCTL_GET_CPSTAT:
2128                 case NANDFS_IOCTL_GET_SUINFO:
2129                 case NANDFS_IOCTL_GET_VINFO:
2130                 case NANDFS_IOCTL_GET_BDESCS:
2131                         break;
2132                 default:
2133                         return (EROFS);
2134                 }
2135         }
2136
2137         switch (command) {
2138         case NANDFS_IOCTL_GET_FSINFO:
2139                 error = nandfs_get_fsinfo(nmp, (struct nandfs_fsinfo *)data);
2140                 break;
2141         case NANDFS_IOCTL_GET_SUSTAT:
2142                 nss = (struct nandfs_seg_stat *)data;
2143                 error = nandfs_get_seg_stat(nandfsdev, nss);
2144                 break;
2145         case NANDFS_IOCTL_CHANGE_CPMODE:
2146                 ncpm = (struct nandfs_cpmode *)data;
2147                 error = nandfs_chng_cpmode(nandfsdev->nd_cp_node, ncpm);
2148                 nandfs_force_syncer(nmp);
2149                 break;
2150         case NANDFS_IOCTL_GET_CPINFO:
2151                 nargv = (struct nandfs_argv *)data;
2152                 error = nandfs_get_cpinfo_ioctl(nandfsdev->nd_cp_node, nargv);
2153                 break;
2154         case NANDFS_IOCTL_DELETE_CP:
2155                 tab = (uint64_t *)data;
2156                 error = nandfs_delete_cp(nandfsdev->nd_cp_node, tab[0], tab[1]);
2157                 nandfs_force_syncer(nmp);
2158                 break;
2159         case NANDFS_IOCTL_GET_CPSTAT:
2160                 ncp = (struct nandfs_cpstat *)data;
2161                 error = nandfs_get_cpstat(nandfsdev->nd_cp_node, ncp);
2162                 break;
2163         case NANDFS_IOCTL_GET_SUINFO:
2164                 nargv = (struct nandfs_argv *)data;
2165                 error = nandfs_get_segment_info_ioctl(nandfsdev, nargv);
2166                 break;
2167         case NANDFS_IOCTL_GET_VINFO:
2168                 nargv = (struct nandfs_argv *)data;
2169                 error = nandfs_get_dat_vinfo_ioctl(nandfsdev, nargv);
2170                 break;
2171         case NANDFS_IOCTL_GET_BDESCS:
2172                 nargv = (struct nandfs_argv *)data;
2173                 error = nandfs_get_dat_bdescs_ioctl(nandfsdev, nargv);
2174                 break;
2175         case NANDFS_IOCTL_SYNC:
2176                 cno = (uint64_t *)data;
2177                 nandfs_force_syncer(nmp);
2178                 *cno = nandfsdev->nd_last_cno;
2179                 error = 0;
2180                 break;
2181         case NANDFS_IOCTL_MAKE_SNAP:
2182                 cno = (uint64_t *)data;
2183                 error = nandfs_make_snap(nandfsdev, cno);
2184                 nandfs_force_syncer(nmp);
2185                 break;
2186         case NANDFS_IOCTL_DELETE_SNAP:
2187                 cno = (uint64_t *)data;
2188                 error = nandfs_delete_snap(nandfsdev, *cno);
2189                 nandfs_force_syncer(nmp);
2190                 break;
2191         default:
2192                 error = ENOTTY;
2193                 break;
2194         }
2195
2196         return (error);
2197 }
2198
2199 /*
2200  * Whiteout vnode call
2201  */
2202 static int
2203 nandfs_whiteout(struct vop_whiteout_args *ap)
2204 {
2205         struct vnode *dvp = ap->a_dvp;
2206         struct componentname *cnp = ap->a_cnp;
2207         int error = 0;
2208
2209         switch (ap->a_flags) {
2210         case LOOKUP:
2211                 return (0);
2212         case CREATE:
2213                 /* Create a new directory whiteout */
2214 #ifdef INVARIANTS
2215                 if ((cnp->cn_flags & SAVENAME) == 0)
2216                         panic("nandfs_whiteout: missing name");
2217 #endif
2218                 error = nandfs_add_dirent(dvp, NANDFS_WHT_INO, cnp->cn_nameptr,
2219                     cnp->cn_namelen, DT_WHT);
2220                 break;
2221
2222         case DELETE:
2223                 /* Remove an existing directory whiteout */
2224                 cnp->cn_flags &= ~DOWHITEOUT;
2225                 error = nandfs_remove_dirent(dvp, NULL, cnp);
2226                 break;
2227         default:
2228                 panic("nandf_whiteout: unknown op: %d", ap->a_flags);
2229         }
2230
2231         return (error);
2232 }
2233
2234 static int
2235 nandfs_pathconf(struct vop_pathconf_args *ap)
2236 {
2237         int error;
2238
2239         error = 0;
2240         switch (ap->a_name) {
2241         case _PC_LINK_MAX:
2242                 *ap->a_retval = NANDFS_LINK_MAX;
2243                 break;
2244         case _PC_NAME_MAX:
2245                 *ap->a_retval = NANDFS_NAME_LEN;
2246                 break;
2247         case _PC_PIPE_BUF:
2248                 if (ap->a_vp->v_type == VDIR || ap->a_vp->v_type == VFIFO)
2249                         *ap->a_retval = PIPE_BUF;
2250                 else
2251                         error = EINVAL;
2252                 break;
2253         case _PC_CHOWN_RESTRICTED:
2254                 *ap->a_retval = 1;
2255                 break;
2256         case _PC_NO_TRUNC:
2257                 *ap->a_retval = 1;
2258                 break;
2259         case _PC_ACL_EXTENDED:
2260                 *ap->a_retval = 0;
2261                 break;
2262         case _PC_ALLOC_SIZE_MIN:
2263                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize;
2264                 break;
2265         case _PC_FILESIZEBITS:
2266                 *ap->a_retval = 64;
2267                 break;
2268         case _PC_REC_INCR_XFER_SIZE:
2269                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2270                 break;
2271         case _PC_REC_MAX_XFER_SIZE:
2272                 *ap->a_retval = -1; /* means ``unlimited'' */
2273                 break;
2274         case _PC_REC_MIN_XFER_SIZE:
2275                 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize;
2276                 break;
2277         default:
2278                 error = vop_stdpathconf(ap);
2279                 break;
2280         }
2281         return (error);
2282 }
2283
2284 static int
2285 nandfs_vnlock1(struct vop_lock1_args *ap)
2286 {
2287         struct vnode *vp = ap->a_vp;
2288         struct nandfs_node *node = VTON(vp);
2289         int error, vi_locked;
2290
2291         /*
2292          * XXX can vnode go away while we are sleeping?
2293          */
2294         vi_locked = mtx_owned(&vp->v_interlock);
2295         if (vi_locked)
2296                 VI_UNLOCK(vp);
2297         error = NANDFS_WRITELOCKFLAGS(node->nn_nandfsdev,
2298             ap->a_flags & LK_NOWAIT);
2299         if (vi_locked && !error)
2300                 VI_LOCK(vp);
2301         if (error)
2302                 return (error);
2303
2304         error = vop_stdlock(ap);
2305         if (error) {
2306                 NANDFS_WRITEUNLOCK(node->nn_nandfsdev);
2307                 return (error);
2308         }
2309
2310         return (0);
2311 }
2312
2313 static int
2314 nandfs_vnunlock(struct vop_unlock_args *ap)
2315 {
2316         struct vnode *vp = ap->a_vp;
2317         struct nandfs_node *node = VTON(vp);
2318         int error;
2319
2320         error = vop_stdunlock(ap);
2321         if (error)
2322                 return (error);
2323
2324         NANDFS_WRITEUNLOCK(node->nn_nandfsdev);
2325
2326         return (0);
2327 }
2328
2329 /*
2330  * Global vfs data structures
2331  */
2332 struct vop_vector nandfs_vnodeops = {
2333         .vop_default =          &default_vnodeops,
2334         .vop_access =           nandfs_access,
2335         .vop_advlock =          nandfs_advlock,
2336         .vop_bmap =             nandfs_bmap,
2337         .vop_close =            nandfs_close,
2338         .vop_create =           nandfs_create,
2339         .vop_fsync =            nandfs_fsync,
2340         .vop_getattr =          nandfs_getattr,
2341         .vop_inactive =         nandfs_inactive,
2342         .vop_cachedlookup =     nandfs_lookup,
2343         .vop_ioctl =            nandfs_ioctl,
2344         .vop_link =             nandfs_link,
2345         .vop_lookup =           vfs_cache_lookup,
2346         .vop_mkdir =            nandfs_mkdir,
2347         .vop_mknod =            nandfs_mknod,
2348         .vop_open =             nandfs_open,
2349         .vop_pathconf =         nandfs_pathconf,
2350         .vop_print =            nandfs_print,
2351         .vop_read =             nandfs_read,
2352         .vop_readdir =          nandfs_readdir,
2353         .vop_readlink =         nandfs_readlink,
2354         .vop_reclaim =          nandfs_reclaim,
2355         .vop_remove =           nandfs_remove,
2356         .vop_rename =           nandfs_rename,
2357         .vop_rmdir =            nandfs_rmdir,
2358         .vop_whiteout =         nandfs_whiteout,
2359         .vop_write =            nandfs_write,
2360         .vop_setattr =          nandfs_setattr,
2361         .vop_strategy =         nandfs_strategy,
2362         .vop_symlink =          nandfs_symlink,
2363         .vop_lock1 =            nandfs_vnlock1,
2364         .vop_unlock =           nandfs_vnunlock,
2365 };
2366
2367 struct vop_vector nandfs_system_vnodeops = {
2368         .vop_default =          &default_vnodeops,
2369         .vop_close =            nandfs_close,
2370         .vop_inactive =         nandfs_inactive,
2371         .vop_reclaim =          nandfs_reclaim,
2372         .vop_strategy =         nandfs_strategy,
2373         .vop_fsync =            nandfs_fsync,
2374         .vop_bmap =             nandfs_bmap,
2375         .vop_access =           VOP_PANIC,
2376         .vop_advlock =          VOP_PANIC,
2377         .vop_create =           VOP_PANIC,
2378         .vop_getattr =          VOP_PANIC,
2379         .vop_cachedlookup =     VOP_PANIC,
2380         .vop_ioctl =            VOP_PANIC,
2381         .vop_link =             VOP_PANIC,
2382         .vop_lookup =           VOP_PANIC,
2383         .vop_mkdir =            VOP_PANIC,
2384         .vop_mknod =            VOP_PANIC,
2385         .vop_open =             VOP_PANIC,
2386         .vop_pathconf =         VOP_PANIC,
2387         .vop_print =            VOP_PANIC,
2388         .vop_read =             VOP_PANIC,
2389         .vop_readdir =          VOP_PANIC,
2390         .vop_readlink =         VOP_PANIC,
2391         .vop_remove =           VOP_PANIC,
2392         .vop_rename =           VOP_PANIC,
2393         .vop_rmdir =            VOP_PANIC,
2394         .vop_whiteout =         VOP_PANIC,
2395         .vop_write =            VOP_PANIC,
2396         .vop_setattr =          VOP_PANIC,
2397         .vop_symlink =          VOP_PANIC,
2398 };
2399
2400 static int
2401 nandfsfifo_close(struct vop_close_args *ap)
2402 {
2403         struct vnode *vp = ap->a_vp;
2404         struct nandfs_node *node = VTON(vp);
2405
2406         DPRINTF(VNCALL, ("%s: vp %p node %p\n", __func__, vp, node));
2407
2408         mtx_lock(&vp->v_interlock);
2409         if (vp->v_usecount > 1)
2410                 nandfs_itimes_locked(vp);
2411         mtx_unlock(&vp->v_interlock);
2412
2413         return (fifo_specops.vop_close(ap));
2414 }
2415
2416 struct vop_vector nandfs_fifoops = {
2417         .vop_default =          &fifo_specops,
2418         .vop_fsync =            VOP_PANIC,
2419         .vop_access =           nandfs_access,
2420         .vop_close =            nandfsfifo_close,
2421         .vop_getattr =          nandfs_getattr,
2422         .vop_inactive =         nandfs_inactive,
2423         .vop_pathconf =         nandfs_pathconf,
2424         .vop_print =            nandfs_print,
2425         .vop_read =             VOP_PANIC,
2426         .vop_reclaim =          nandfs_reclaim,
2427         .vop_setattr =          nandfs_setattr,
2428         .vop_write =            VOP_PANIC,
2429         .vop_lock1 =            nandfs_vnlock1,
2430         .vop_unlock =           nandfs_vnunlock,
2431 };
2432
2433 int
2434 nandfs_vinit(struct vnode *vp, uint64_t ino)
2435 {
2436         struct nandfs_node *node;
2437
2438         ASSERT_VOP_LOCKED(vp, __func__);
2439
2440         node = VTON(vp);
2441
2442         /* Check if we're fetching the root */
2443         if (ino == NANDFS_ROOT_INO)
2444                 vp->v_vflag |= VV_ROOT;
2445
2446         if (ino != NANDFS_GC_INO)
2447                 vp->v_type = IFTOVT(node->nn_inode.i_mode);
2448         else
2449                 vp->v_type = VREG;
2450
2451         if (vp->v_type == VFIFO)
2452                 vp->v_op = &nandfs_fifoops;
2453
2454         return (0);
2455 }