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