xref: /dragonfly/sys/vfs/hammer2/hammer2_vnops.c (revision 279dd846)
1 /*
2  * Copyright (c) 2011-2015 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7  * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression)
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  * 3. Neither the name of The DragonFly Project nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific, prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
27  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 /*
37  * Kernel Filesystem interface
38  *
39  * NOTE! local ipdata pointers must be reloaded on any modifying operation
40  *	 to the inode as its underlying chain may have changed.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/fcntl.h>
47 #include <sys/buf.h>
48 #include <sys/proc.h>
49 #include <sys/namei.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/mountctl.h>
53 #include <sys/dirent.h>
54 #include <sys/uio.h>
55 #include <sys/objcache.h>
56 #include <sys/event.h>
57 #include <sys/file.h>
58 #include <vfs/fifofs/fifo.h>
59 
60 #include "hammer2.h"
61 
62 static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio,
63 				int seqcount);
64 static int hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
65 				int ioflag, int seqcount);
66 static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize);
67 static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize);
68 
69 struct objcache *cache_xops;
70 
71 static __inline
72 void
73 hammer2_knote(struct vnode *vp, int flags)
74 {
75 	if (flags)
76 		KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
77 }
78 
79 /*
80  * Last reference to a vnode is going away but it is still cached.
81  */
82 static
83 int
84 hammer2_vop_inactive(struct vop_inactive_args *ap)
85 {
86 	hammer2_inode_t *ip;
87 	struct vnode *vp;
88 
89 	LOCKSTART;
90 	vp = ap->a_vp;
91 	ip = VTOI(vp);
92 
93 	/*
94 	 * Degenerate case
95 	 */
96 	if (ip == NULL) {
97 		vrecycle(vp);
98 		LOCKSTOP;
99 		return (0);
100 	}
101 
102 	/*
103 	 * Check for deleted inodes and recycle immediately on the last
104 	 * release.  Be sure to destroy any left-over buffer cache buffers
105 	 * so we do not waste time trying to flush them.
106 	 *
107 	 * Note that deleting the file block chains under the inode chain
108 	 * would just be a waste of energy, so don't do it.
109 	 *
110 	 * WARNING: nvtruncbuf() can only be safely called without the inode
111 	 *	    lock held due to the way our write thread works.
112 	 */
113 	if (ip->flags & HAMMER2_INODE_ISUNLINKED) {
114 		hammer2_key_t lbase;
115 		int nblksize;
116 
117 		/*
118 		 * Detect updates to the embedded data which may be
119 		 * synchronized by the strategy code.  Simply mark the
120 		 * inode modified so it gets picked up by our normal flush.
121 		 */
122 		nblksize = hammer2_calc_logical(ip, 0, &lbase, NULL);
123 		nvtruncbuf(vp, 0, nblksize, 0, 0);
124 		vrecycle(vp);
125 	}
126 	LOCKSTOP;
127 	return (0);
128 }
129 
130 /*
131  * Reclaim a vnode so that it can be reused; after the inode is
132  * disassociated, the filesystem must manage it alone.
133  */
134 static
135 int
136 hammer2_vop_reclaim(struct vop_reclaim_args *ap)
137 {
138 	hammer2_inode_t *ip;
139 	hammer2_pfs_t *pmp;
140 	struct vnode *vp;
141 
142 	LOCKSTART;
143 	vp = ap->a_vp;
144 	ip = VTOI(vp);
145 	if (ip == NULL) {
146 		LOCKSTOP;
147 		return(0);
148 	}
149 	pmp = ip->pmp;
150 
151 	/*
152 	 * The final close of a deleted file or directory marks it for
153 	 * destruction.  The DELETED flag allows the flusher to shortcut
154 	 * any modified blocks still unflushed (that is, just ignore them).
155 	 *
156 	 * HAMMER2 usually does not try to optimize the freemap by returning
157 	 * deleted blocks to it as it does not usually know how many snapshots
158 	 * might be referencing portions of the file/dir.
159 	 */
160 	vp->v_data = NULL;
161 	ip->vp = NULL;
162 
163 	/*
164 	 * NOTE! We do not attempt to flush chains here, flushing is
165 	 *	 really fragile and could also deadlock.
166 	 */
167 	vclrisdirty(vp);
168 
169 	/*
170 	 * An unlinked inode may have been relinked to the ihidden directory.
171 	 * This occurs if the inode was unlinked while open.  Reclamation of
172 	 * these inodes requires processing we cannot safely do here so add
173 	 * the inode to the sideq in that situation.
174 	 *
175 	 * A modified inode may require chain synchronization which will no
176 	 * longer be driven by a sync or fsync without the vnode, also use
177 	 * the sideq for that.
178 	 *
179 	 * A reclaim can occur at any time so we cannot safely start a
180 	 * transaction to handle reclamation of unlinked files.  Instead,
181 	 * the ip is left with a reference and placed on a linked list and
182 	 * handled later on.
183 	 */
184 	if ((ip->flags & (HAMMER2_INODE_ISUNLINKED |
185 			  HAMMER2_INODE_MODIFIED |
186 			  HAMMER2_INODE_RESIZED)) &&
187 	    (ip->flags & HAMMER2_INODE_ISDELETED) == 0) {
188 		hammer2_inode_sideq_t *ipul;
189 
190 		ipul = kmalloc(sizeof(*ipul), pmp->minode, M_WAITOK | M_ZERO);
191 		ipul->ip = ip;
192 
193 		hammer2_spin_ex(&pmp->list_spin);
194 		if ((ip->flags & HAMMER2_INODE_ONSIDEQ) == 0) {
195 			/* ref -> sideq */
196 			atomic_set_int(&ip->flags, HAMMER2_INODE_ONSIDEQ);
197 			TAILQ_INSERT_TAIL(&pmp->sideq, ipul, entry);
198 			hammer2_spin_unex(&pmp->list_spin);
199 		} else {
200 			hammer2_spin_unex(&pmp->list_spin);
201 			kfree(ipul, pmp->minode);
202 			hammer2_inode_drop(ip);		/* vp ref */
203 		}
204 		/* retain ref from vp for ipul */
205 	} else {
206 		hammer2_inode_drop(ip);			/* vp ref */
207 	}
208 
209 	/*
210 	 * XXX handle background sync when ip dirty, kernel will no longer
211 	 * notify us regarding this inode because there is no longer a
212 	 * vnode attached to it.
213 	 */
214 
215 	LOCKSTOP;
216 	return (0);
217 }
218 
219 static
220 int
221 hammer2_vop_fsync(struct vop_fsync_args *ap)
222 {
223 	hammer2_inode_t *ip;
224 	struct vnode *vp;
225 
226 	LOCKSTART;
227 	vp = ap->a_vp;
228 	ip = VTOI(vp);
229 
230 #if 0
231 	/* XXX can't do this yet */
232 	hammer2_trans_init(ip->pmp, HAMMER2_TRANS_ISFLUSH);
233 	vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
234 #endif
235 	hammer2_trans_init(ip->pmp, 0);
236 	vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
237 
238 	/*
239 	 * Calling chain_flush here creates a lot of duplicative
240 	 * COW operations due to non-optimal vnode ordering.
241 	 *
242 	 * Only do it for an actual fsync() syscall.  The other forms
243 	 * which call this function will eventually call chain_flush
244 	 * on the volume root as a catch-all, which is far more optimal.
245 	 */
246 	hammer2_inode_lock(ip, 0);
247 	if (ip->flags & HAMMER2_INODE_MODIFIED)
248 		hammer2_inode_chain_sync(ip);
249 	hammer2_inode_unlock(ip);
250 	hammer2_trans_done(ip->pmp);
251 
252 	LOCKSTOP;
253 	return (0);
254 }
255 
256 static
257 int
258 hammer2_vop_access(struct vop_access_args *ap)
259 {
260 	hammer2_inode_t *ip = VTOI(ap->a_vp);
261 	uid_t uid;
262 	gid_t gid;
263 	int error;
264 
265 	LOCKSTART;
266 	hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
267 	uid = hammer2_to_unix_xid(&ip->meta.uid);
268 	gid = hammer2_to_unix_xid(&ip->meta.gid);
269 	error = vop_helper_access(ap, uid, gid, ip->meta.mode, ip->meta.uflags);
270 	hammer2_inode_unlock(ip);
271 
272 	LOCKSTOP;
273 	return (error);
274 }
275 
276 static
277 int
278 hammer2_vop_getattr(struct vop_getattr_args *ap)
279 {
280 	hammer2_pfs_t *pmp;
281 	hammer2_inode_t *ip;
282 	struct vnode *vp;
283 	struct vattr *vap;
284 	hammer2_chain_t *chain;
285 	int i;
286 
287 	LOCKSTART;
288 	vp = ap->a_vp;
289 	vap = ap->a_vap;
290 
291 	ip = VTOI(vp);
292 	pmp = ip->pmp;
293 
294 	hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
295 
296 	vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
297 	vap->va_fileid = ip->meta.inum;
298 	vap->va_mode = ip->meta.mode;
299 	vap->va_nlink = ip->meta.nlinks;
300 	vap->va_uid = hammer2_to_unix_xid(&ip->meta.uid);
301 	vap->va_gid = hammer2_to_unix_xid(&ip->meta.gid);
302 	vap->va_rmajor = 0;
303 	vap->va_rminor = 0;
304 	vap->va_size = ip->meta.size;	/* protected by shared lock */
305 	vap->va_blocksize = HAMMER2_PBUFSIZE;
306 	vap->va_flags = ip->meta.uflags;
307 	hammer2_time_to_timespec(ip->meta.ctime, &vap->va_ctime);
308 	hammer2_time_to_timespec(ip->meta.mtime, &vap->va_mtime);
309 	hammer2_time_to_timespec(ip->meta.mtime, &vap->va_atime);
310 	vap->va_gen = 1;
311 	vap->va_bytes = 0;
312 	if (ip->meta.type == HAMMER2_OBJTYPE_DIRECTORY) {
313 		/*
314 		 * Can't really calculate directory use sans the files under
315 		 * it, just assume one block for now.
316 		 */
317 		vap->va_bytes += HAMMER2_INODE_BYTES;
318 	} else {
319 		for (i = 0; i < ip->cluster.nchains; ++i) {
320 			if ((chain = ip->cluster.array[i].chain) != NULL) {
321 				if (vap->va_bytes < chain->bref.data_count)
322 					vap->va_bytes = chain->bref.data_count;
323 			}
324 		}
325 	}
326 	vap->va_type = hammer2_get_vtype(ip->meta.type);
327 	vap->va_filerev = 0;
328 	vap->va_uid_uuid = ip->meta.uid;
329 	vap->va_gid_uuid = ip->meta.gid;
330 	vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
331 			  VA_FSID_UUID_VALID;
332 
333 	hammer2_inode_unlock(ip);
334 
335 	LOCKSTOP;
336 	return (0);
337 }
338 
339 static
340 int
341 hammer2_vop_setattr(struct vop_setattr_args *ap)
342 {
343 	hammer2_inode_t *ip;
344 	struct vnode *vp;
345 	struct vattr *vap;
346 	int error;
347 	int kflags = 0;
348 	uint64_t ctime;
349 
350 	LOCKSTART;
351 	vp = ap->a_vp;
352 	vap = ap->a_vap;
353 	hammer2_update_time(&ctime);
354 
355 	ip = VTOI(vp);
356 
357 	if (ip->pmp->ronly) {
358 		LOCKSTOP;
359 		return(EROFS);
360 	}
361 
362 	hammer2_pfs_memory_wait(ip->pmp);
363 	hammer2_trans_init(ip->pmp, 0);
364 	hammer2_inode_lock(ip, 0);
365 	error = 0;
366 
367 	if (vap->va_flags != VNOVAL) {
368 		u_int32_t flags;
369 
370 		flags = ip->meta.uflags;
371 		error = vop_helper_setattr_flags(&flags, vap->va_flags,
372 				     hammer2_to_unix_xid(&ip->meta.uid),
373 				     ap->a_cred);
374 		if (error == 0) {
375 			if (ip->meta.uflags != flags) {
376 				hammer2_inode_modify(ip);
377 				ip->meta.uflags = flags;
378 				ip->meta.ctime = ctime;
379 				kflags |= NOTE_ATTRIB;
380 			}
381 			if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
382 				error = 0;
383 				goto done;
384 			}
385 		}
386 		goto done;
387 	}
388 	if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
389 		error = EPERM;
390 		goto done;
391 	}
392 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
393 		mode_t cur_mode = ip->meta.mode;
394 		uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
395 		gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
396 		uuid_t uuid_uid;
397 		uuid_t uuid_gid;
398 
399 		error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
400 					 ap->a_cred,
401 					 &cur_uid, &cur_gid, &cur_mode);
402 		if (error == 0) {
403 			hammer2_guid_to_uuid(&uuid_uid, cur_uid);
404 			hammer2_guid_to_uuid(&uuid_gid, cur_gid);
405 			if (bcmp(&uuid_uid, &ip->meta.uid, sizeof(uuid_uid)) ||
406 			    bcmp(&uuid_gid, &ip->meta.gid, sizeof(uuid_gid)) ||
407 			    ip->meta.mode != cur_mode
408 			) {
409 				hammer2_inode_modify(ip);
410 				ip->meta.uid = uuid_uid;
411 				ip->meta.gid = uuid_gid;
412 				ip->meta.mode = cur_mode;
413 				ip->meta.ctime = ctime;
414 			}
415 			kflags |= NOTE_ATTRIB;
416 		}
417 	}
418 
419 	/*
420 	 * Resize the file
421 	 */
422 	if (vap->va_size != VNOVAL && ip->meta.size != vap->va_size) {
423 		switch(vp->v_type) {
424 		case VREG:
425 			if (vap->va_size == ip->meta.size)
426 				break;
427 			if (vap->va_size < ip->meta.size) {
428 				hammer2_truncate_file(ip, vap->va_size);
429 			} else {
430 				hammer2_extend_file(ip, vap->va_size);
431 			}
432 			hammer2_inode_modify(ip);
433 			ip->meta.mtime = ctime;
434 			break;
435 		default:
436 			error = EINVAL;
437 			goto done;
438 		}
439 	}
440 #if 0
441 	/* atime not supported */
442 	if (vap->va_atime.tv_sec != VNOVAL) {
443 		hammer2_inode_modify(ip);
444 		ip->meta.atime = hammer2_timespec_to_time(&vap->va_atime);
445 		kflags |= NOTE_ATTRIB;
446 	}
447 #endif
448 	if (vap->va_mode != (mode_t)VNOVAL) {
449 		mode_t cur_mode = ip->meta.mode;
450 		uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
451 		gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
452 
453 		error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
454 					 cur_uid, cur_gid, &cur_mode);
455 		if (error == 0 && ip->meta.mode != cur_mode) {
456 			hammer2_inode_modify(ip);
457 			ip->meta.mode = cur_mode;
458 			ip->meta.ctime = ctime;
459 			kflags |= NOTE_ATTRIB;
460 		}
461 	}
462 
463 	if (vap->va_mtime.tv_sec != VNOVAL) {
464 		hammer2_inode_modify(ip);
465 		ip->meta.mtime = hammer2_timespec_to_time(&vap->va_mtime);
466 		kflags |= NOTE_ATTRIB;
467 	}
468 
469 done:
470 	/*
471 	 * If a truncation occurred we must call inode_fsync() now in order
472 	 * to trim the related data chains, otherwise a later expansion can
473 	 * cause havoc.
474 	 *
475 	 * If an extend occured that changed the DIRECTDATA state, we must
476 	 * call inode_fsync now in order to prepare the inode's indirect
477 	 * block table.
478 	 */
479 	if (ip->flags & HAMMER2_INODE_RESIZED)
480 		hammer2_inode_chain_sync(ip);
481 
482 	/*
483 	 * Cleanup.
484 	 */
485 	hammer2_inode_unlock(ip);
486 	hammer2_trans_done(ip->pmp);
487 	hammer2_knote(ip->vp, kflags);
488 
489 	LOCKSTOP;
490 	return (error);
491 }
492 
493 static
494 int
495 hammer2_vop_readdir(struct vop_readdir_args *ap)
496 {
497 	hammer2_xop_readdir_t *xop;
498 	hammer2_blockref_t bref;
499 	hammer2_inode_t *ip;
500 	hammer2_tid_t inum;
501 	hammer2_key_t lkey;
502 	struct uio *uio;
503 	off_t *cookies;
504 	off_t saveoff;
505 	int cookie_index;
506 	int ncookies;
507 	int error;
508 	int eofflag;
509 	int dtype;
510 	int r;
511 
512 	LOCKSTART;
513 	ip = VTOI(ap->a_vp);
514 	uio = ap->a_uio;
515 	saveoff = uio->uio_offset;
516 	eofflag = 0;
517 	error = 0;
518 
519 	/*
520 	 * Setup cookies directory entry cookies if requested
521 	 */
522 	if (ap->a_ncookies) {
523 		ncookies = uio->uio_resid / 16 + 1;
524 		if (ncookies > 1024)
525 			ncookies = 1024;
526 		cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
527 	} else {
528 		ncookies = -1;
529 		cookies = NULL;
530 	}
531 	cookie_index = 0;
532 
533 	hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
534 
535 	/*
536 	 * Handle artificial entries.  To ensure that only positive 64 bit
537 	 * quantities are returned to userland we always strip off bit 63.
538 	 * The hash code is designed such that codes 0x0000-0x7FFF are not
539 	 * used, allowing us to use these codes for articial entries.
540 	 *
541 	 * Entry 0 is used for '.' and entry 1 is used for '..'.  Do not
542 	 * allow '..' to cross the mount point into (e.g.) the super-root.
543 	 */
544 	if (saveoff == 0) {
545 		inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
546 		r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
547 		if (r)
548 			goto done;
549 		if (cookies)
550 			cookies[cookie_index] = saveoff;
551 		++saveoff;
552 		++cookie_index;
553 		if (cookie_index == ncookies)
554 			goto done;
555 	}
556 
557 	if (saveoff == 1) {
558 		/*
559 		 * Be careful with lockorder when accessing ".."
560 		 *
561 		 * (ip is the current dir. xip is the parent dir).
562 		 */
563 		inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
564 		if (ip->pip && ip != ip->pmp->iroot)
565 			inum = ip->pip->meta.inum & HAMMER2_DIRHASH_USERMSK;
566 		r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
567 		if (r)
568 			goto done;
569 		if (cookies)
570 			cookies[cookie_index] = saveoff;
571 		++saveoff;
572 		++cookie_index;
573 		if (cookie_index == ncookies)
574 			goto done;
575 	}
576 
577 	lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
578 	if (hammer2_debug & 0x0020)
579 		kprintf("readdir: lkey %016jx\n", lkey);
580 	if (error)
581 		goto done;
582 
583 	/*
584 	 * Use XOP for cluster scan.
585 	 *
586 	 * parent is the inode cluster, already locked for us.  Don't
587 	 * double lock shared locks as this will screw up upgrades.
588 	 */
589 	xop = hammer2_xop_alloc(ip, 0);
590 	xop->lkey = lkey;
591 	hammer2_xop_start(&xop->head, hammer2_xop_readdir);
592 
593 	for (;;) {
594 		const hammer2_inode_data_t *ripdata;
595 
596 		error = hammer2_xop_collect(&xop->head, 0);
597 		if (error)
598 			break;
599 		if (cookie_index == ncookies)
600 			break;
601 		if (hammer2_debug & 0x0020)
602 		kprintf("cluster chain %p %p\n",
603 			xop->head.cluster.focus,
604 			(xop->head.cluster.focus ?
605 			 xop->head.cluster.focus->data : (void *)-1));
606 		ripdata = &hammer2_cluster_rdata(&xop->head.cluster)->ipdata;
607 		hammer2_cluster_bref(&xop->head.cluster, &bref);
608 		if (bref.type == HAMMER2_BREF_TYPE_INODE) {
609 			dtype = hammer2_get_dtype(ripdata);
610 			saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
611 			r = vop_write_dirent(&error, uio,
612 					     ripdata->meta.inum &
613 					      HAMMER2_DIRHASH_USERMSK,
614 					     dtype,
615 					     ripdata->meta.name_len,
616 					     ripdata->filename);
617 			if (r)
618 				break;
619 			if (cookies)
620 				cookies[cookie_index] = saveoff;
621 			++cookie_index;
622 		} else {
623 			/* XXX chain error */
624 			kprintf("bad chain type readdir %d\n", bref.type);
625 		}
626 	}
627 	hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
628 	if (error == ENOENT) {
629 		error = 0;
630 		eofflag = 1;
631 		saveoff = (hammer2_key_t)-1;
632 	} else {
633 		saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
634 	}
635 done:
636 	hammer2_inode_unlock(ip);
637 	if (ap->a_eofflag)
638 		*ap->a_eofflag = eofflag;
639 	if (hammer2_debug & 0x0020)
640 		kprintf("readdir: done at %016jx\n", saveoff);
641 	uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
642 	if (error && cookie_index == 0) {
643 		if (cookies) {
644 			kfree(cookies, M_TEMP);
645 			*ap->a_ncookies = 0;
646 			*ap->a_cookies = NULL;
647 		}
648 	} else {
649 		if (cookies) {
650 			*ap->a_ncookies = cookie_index;
651 			*ap->a_cookies = cookies;
652 		}
653 	}
654 	LOCKSTOP;
655 	return (error);
656 }
657 
658 /*
659  * hammer2_vop_readlink { vp, uio, cred }
660  */
661 static
662 int
663 hammer2_vop_readlink(struct vop_readlink_args *ap)
664 {
665 	struct vnode *vp;
666 	hammer2_inode_t *ip;
667 	int error;
668 
669 	vp = ap->a_vp;
670 	if (vp->v_type != VLNK)
671 		return (EINVAL);
672 	ip = VTOI(vp);
673 
674 	error = hammer2_read_file(ip, ap->a_uio, 0);
675 	return (error);
676 }
677 
678 static
679 int
680 hammer2_vop_read(struct vop_read_args *ap)
681 {
682 	struct vnode *vp;
683 	hammer2_inode_t *ip;
684 	struct uio *uio;
685 	int error;
686 	int seqcount;
687 	int bigread;
688 
689 	/*
690 	 * Read operations supported on this vnode?
691 	 */
692 	vp = ap->a_vp;
693 	if (vp->v_type != VREG)
694 		return (EINVAL);
695 
696 	/*
697 	 * Misc
698 	 */
699 	ip = VTOI(vp);
700 	uio = ap->a_uio;
701 	error = 0;
702 
703 	seqcount = ap->a_ioflag >> 16;
704 	bigread = (uio->uio_resid > 100 * 1024 * 1024);
705 
706 	error = hammer2_read_file(ip, uio, seqcount);
707 	return (error);
708 }
709 
710 static
711 int
712 hammer2_vop_write(struct vop_write_args *ap)
713 {
714 	hammer2_inode_t *ip;
715 	thread_t td;
716 	struct vnode *vp;
717 	struct uio *uio;
718 	int error;
719 	int seqcount;
720 
721 	/*
722 	 * Read operations supported on this vnode?
723 	 */
724 	vp = ap->a_vp;
725 	if (vp->v_type != VREG)
726 		return (EINVAL);
727 
728 	/*
729 	 * Misc
730 	 */
731 	ip = VTOI(vp);
732 	uio = ap->a_uio;
733 	error = 0;
734 	if (ip->pmp->ronly) {
735 		return (EROFS);
736 	}
737 
738 	seqcount = ap->a_ioflag >> 16;
739 
740 	/*
741 	 * Check resource limit
742 	 */
743 	if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
744 	    uio->uio_offset + uio->uio_resid >
745 	     td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
746 		lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
747 		return (EFBIG);
748 	}
749 
750 	/*
751 	 * The transaction interlocks against flushes initiations
752 	 * (note: but will run concurrently with the actual flush).
753 	 */
754 	hammer2_trans_init(ip->pmp, 0);
755 	error = hammer2_write_file(ip, uio, ap->a_ioflag, seqcount);
756 	hammer2_trans_done(ip->pmp);
757 
758 	return (error);
759 }
760 
761 /*
762  * Perform read operations on a file or symlink given an UNLOCKED
763  * inode and uio.
764  *
765  * The passed ip is not locked.
766  */
767 static
768 int
769 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
770 {
771 	hammer2_off_t size;
772 	struct buf *bp;
773 	int error;
774 
775 	error = 0;
776 
777 	/*
778 	 * UIO read loop.
779 	 *
780 	 * WARNING! Assumes that the kernel interlocks size changes at the
781 	 *	    vnode level.
782 	 */
783 	hammer2_mtx_sh(&ip->lock);
784 	size = ip->meta.size;
785 	hammer2_mtx_unlock(&ip->lock);
786 
787 	while (uio->uio_resid > 0 && uio->uio_offset < size) {
788 		hammer2_key_t lbase;
789 		hammer2_key_t leof;
790 		int lblksize;
791 		int loff;
792 		int n;
793 
794 		lblksize = hammer2_calc_logical(ip, uio->uio_offset,
795 						&lbase, &leof);
796 
797 		error = cluster_read(ip->vp, leof, lbase, lblksize,
798 				     uio->uio_resid, seqcount * BKVASIZE,
799 				     &bp);
800 
801 		if (error)
802 			break;
803 		loff = (int)(uio->uio_offset - lbase);
804 		n = lblksize - loff;
805 		if (n > uio->uio_resid)
806 			n = uio->uio_resid;
807 		if (n > size - uio->uio_offset)
808 			n = (int)(size - uio->uio_offset);
809 		bp->b_flags |= B_AGE;
810 		uiomove((char *)bp->b_data + loff, n, uio);
811 		bqrelse(bp);
812 	}
813 	return (error);
814 }
815 
816 /*
817  * Write to the file represented by the inode via the logical buffer cache.
818  * The inode may represent a regular file or a symlink.
819  *
820  * The inode must not be locked.
821  */
822 static
823 int
824 hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
825 		   int ioflag, int seqcount)
826 {
827 	hammer2_key_t old_eof;
828 	hammer2_key_t new_eof;
829 	struct buf *bp;
830 	int kflags;
831 	int error;
832 	int modified;
833 
834 	/*
835 	 * Setup if append
836 	 *
837 	 * WARNING! Assumes that the kernel interlocks size changes at the
838 	 *	    vnode level.
839 	 */
840 	hammer2_mtx_ex(&ip->lock);
841 	if (ioflag & IO_APPEND)
842 		uio->uio_offset = ip->meta.size;
843 	old_eof = ip->meta.size;
844 
845 	/*
846 	 * Extend the file if necessary.  If the write fails at some point
847 	 * we will truncate it back down to cover as much as we were able
848 	 * to write.
849 	 *
850 	 * Doing this now makes it easier to calculate buffer sizes in
851 	 * the loop.
852 	 */
853 	kflags = 0;
854 	error = 0;
855 	modified = 0;
856 
857 	if (uio->uio_offset + uio->uio_resid > old_eof) {
858 		new_eof = uio->uio_offset + uio->uio_resid;
859 		modified = 1;
860 		hammer2_extend_file(ip, new_eof);
861 		kflags |= NOTE_EXTEND;
862 	} else {
863 		new_eof = old_eof;
864 	}
865 	hammer2_mtx_unlock(&ip->lock);
866 
867 	/*
868 	 * UIO write loop
869 	 */
870 	while (uio->uio_resid > 0) {
871 		hammer2_key_t lbase;
872 		int trivial;
873 		int endofblk;
874 		int lblksize;
875 		int loff;
876 		int n;
877 
878 		/*
879 		 * Don't allow the buffer build to blow out the buffer
880 		 * cache.
881 		 */
882 		if ((ioflag & IO_RECURSE) == 0)
883 			bwillwrite(HAMMER2_PBUFSIZE);
884 
885 		/*
886 		 * This nominally tells us how much we can cluster and
887 		 * what the logical buffer size needs to be.  Currently
888 		 * we don't try to cluster the write and just handle one
889 		 * block at a time.
890 		 */
891 		lblksize = hammer2_calc_logical(ip, uio->uio_offset,
892 						&lbase, NULL);
893 		loff = (int)(uio->uio_offset - lbase);
894 
895 		KKASSERT(lblksize <= 65536);
896 
897 		/*
898 		 * Calculate bytes to copy this transfer and whether the
899 		 * copy completely covers the buffer or not.
900 		 */
901 		trivial = 0;
902 		n = lblksize - loff;
903 		if (n > uio->uio_resid) {
904 			n = uio->uio_resid;
905 			if (loff == lbase && uio->uio_offset + n == new_eof)
906 				trivial = 1;
907 			endofblk = 0;
908 		} else {
909 			if (loff == 0)
910 				trivial = 1;
911 			endofblk = 1;
912 		}
913 
914 		/*
915 		 * Get the buffer
916 		 */
917 		if (uio->uio_segflg == UIO_NOCOPY) {
918 			/*
919 			 * Issuing a write with the same data backing the
920 			 * buffer.  Instantiate the buffer to collect the
921 			 * backing vm pages, then read-in any missing bits.
922 			 *
923 			 * This case is used by vop_stdputpages().
924 			 */
925 			bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
926 			if ((bp->b_flags & B_CACHE) == 0) {
927 				bqrelse(bp);
928 				error = bread(ip->vp, lbase, lblksize, &bp);
929 			}
930 		} else if (trivial) {
931 			/*
932 			 * Even though we are entirely overwriting the buffer
933 			 * we may still have to zero it out to avoid a
934 			 * mmap/write visibility issue.
935 			 */
936 			bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
937 			if ((bp->b_flags & B_CACHE) == 0)
938 				vfs_bio_clrbuf(bp);
939 		} else {
940 			/*
941 			 * Partial overwrite, read in any missing bits then
942 			 * replace the portion being written.
943 			 *
944 			 * (The strategy code will detect zero-fill physical
945 			 * blocks for this case).
946 			 */
947 			error = bread(ip->vp, lbase, lblksize, &bp);
948 			if (error == 0)
949 				bheavy(bp);
950 		}
951 
952 		if (error) {
953 			brelse(bp);
954 			break;
955 		}
956 
957 		/*
958 		 * Ok, copy the data in
959 		 */
960 		error = uiomove(bp->b_data + loff, n, uio);
961 		kflags |= NOTE_WRITE;
962 		modified = 1;
963 		if (error) {
964 			brelse(bp);
965 			break;
966 		}
967 
968 		/*
969 		 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
970 		 *	    with IO_SYNC or IO_ASYNC set.  These writes
971 		 *	    must be handled as the pageout daemon expects.
972 		 */
973 		if (ioflag & IO_SYNC) {
974 			bwrite(bp);
975 		} else if ((ioflag & IO_DIRECT) && endofblk) {
976 			bawrite(bp);
977 		} else if (ioflag & IO_ASYNC) {
978 			bawrite(bp);
979 		} else {
980 			bdwrite(bp);
981 		}
982 	}
983 
984 	/*
985 	 * Cleanup.  If we extended the file EOF but failed to write through
986 	 * the entire write is a failure and we have to back-up.
987 	 */
988 	if (error && new_eof != old_eof) {
989 		hammer2_mtx_ex(&ip->lock);
990 		hammer2_truncate_file(ip, old_eof);
991 		if (ip->flags & HAMMER2_INODE_MODIFIED)
992 			hammer2_inode_chain_sync(ip);
993 		hammer2_mtx_unlock(&ip->lock);
994 	} else if (modified) {
995 		hammer2_mtx_ex(&ip->lock);
996 		hammer2_inode_modify(ip);
997 		hammer2_update_time(&ip->meta.mtime);
998 		if (ip->flags & HAMMER2_INODE_MODIFIED)
999 			hammer2_inode_chain_sync(ip);
1000 		hammer2_mtx_unlock(&ip->lock);
1001 		hammer2_knote(ip->vp, kflags);
1002 	}
1003 	hammer2_trans_assert_strategy(ip->pmp);
1004 
1005 	return error;
1006 }
1007 
1008 /*
1009  * Truncate the size of a file.  The inode must not be locked.
1010  *
1011  * We must unconditionally set HAMMER2_INODE_RESIZED to properly
1012  * ensure that any on-media data beyond the new file EOF has been destroyed.
1013  *
1014  * WARNING: nvtruncbuf() can only be safely called without the inode lock
1015  *	    held due to the way our write thread works.  If the truncation
1016  *	    occurs in the middle of a buffer, nvtruncbuf() is responsible
1017  *	    for dirtying that buffer and zeroing out trailing bytes.
1018  *
1019  * WARNING! Assumes that the kernel interlocks size changes at the
1020  *	    vnode level.
1021  *
1022  * WARNING! Caller assumes responsibility for removing dead blocks
1023  *	    if INODE_RESIZED is set.
1024  */
1025 static
1026 void
1027 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1028 {
1029 	hammer2_key_t lbase;
1030 	int nblksize;
1031 
1032 	LOCKSTART;
1033 	hammer2_mtx_unlock(&ip->lock);
1034 	if (ip->vp) {
1035 		nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1036 		nvtruncbuf(ip->vp, nsize,
1037 			   nblksize, (int)nsize & (nblksize - 1),
1038 			   0);
1039 	}
1040 	hammer2_mtx_ex(&ip->lock);
1041 	KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1042 	ip->osize = ip->meta.size;
1043 	ip->meta.size = nsize;
1044 	atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED |
1045 				   HAMMER2_INODE_RESIZED);
1046 	LOCKSTOP;
1047 }
1048 
1049 /*
1050  * Extend the size of a file.  The inode must not be locked.
1051  *
1052  * Even though the file size is changing, we do not have to set the
1053  * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES
1054  * boundary.  When this occurs a hammer2_inode_chain_sync() is required
1055  * to prepare the inode cluster's indirect block table, otherwise
1056  * async execution of the strategy code will implode on us.
1057  *
1058  * WARNING! Assumes that the kernel interlocks size changes at the
1059  *	    vnode level.
1060  *
1061  * WARNING! Caller assumes responsibility for transitioning out
1062  *	    of the inode DIRECTDATA mode if INODE_RESIZED is set.
1063  */
1064 static
1065 void
1066 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1067 {
1068 	hammer2_key_t lbase;
1069 	hammer2_key_t osize;
1070 	int oblksize;
1071 	int nblksize;
1072 
1073 	LOCKSTART;
1074 
1075 	KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1076 	osize = ip->meta.size;
1077 	ip->osize = osize;
1078 	ip->meta.size = nsize;
1079 	atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1080 
1081 	if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES) {
1082 		atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1083 		hammer2_inode_chain_sync(ip);
1084 	}
1085 
1086 	hammer2_mtx_unlock(&ip->lock);
1087 	if (ip->vp) {
1088 		oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1089 		nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1090 		nvextendbuf(ip->vp,
1091 			    osize, nsize,
1092 			    oblksize, nblksize,
1093 			    -1, -1, 0);
1094 	}
1095 	hammer2_mtx_ex(&ip->lock);
1096 
1097 	LOCKSTOP;
1098 }
1099 
1100 static
1101 int
1102 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1103 {
1104 	hammer2_xop_nresolve_t *xop;
1105 	hammer2_inode_t *ip;
1106 	hammer2_inode_t *dip;
1107 	struct namecache *ncp;
1108 	struct vnode *vp;
1109 	int error;
1110 
1111 	LOCKSTART;
1112 	dip = VTOI(ap->a_dvp);
1113 	xop = hammer2_xop_alloc(dip, 0);
1114 
1115 	ncp = ap->a_nch->ncp;
1116 	hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1117 
1118 	/*
1119 	 * Note: In DragonFly the kernel handles '.' and '..'.
1120 	 */
1121 	hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1122 	hammer2_xop_start(&xop->head, hammer2_xop_nresolve);
1123 
1124 	error = hammer2_xop_collect(&xop->head, 0);
1125 	if (error) {
1126 		ip = NULL;
1127 	} else {
1128 		ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1129 	}
1130 	hammer2_inode_unlock(dip);
1131 
1132 	/*
1133 	 * Acquire the related vnode
1134 	 *
1135 	 * NOTE: For error processing, only ENOENT resolves the namecache
1136 	 *	 entry to NULL, otherwise we just return the error and
1137 	 *	 leave the namecache unresolved.
1138 	 *
1139 	 * NOTE: multiple hammer2_inode structures can be aliased to the
1140 	 *	 same chain element, for example for hardlinks.  This
1141 	 *	 use case does not 'reattach' inode associations that
1142 	 *	 might already exist, but always allocates a new one.
1143 	 *
1144 	 * WARNING: inode structure is locked exclusively via inode_get
1145 	 *	    but chain was locked shared.  inode_unlock()
1146 	 *	    will handle it properly.
1147 	 */
1148 	if (ip) {
1149 		vp = hammer2_igetv(ip, &error);
1150 		if (error == 0) {
1151 			vn_unlock(vp);
1152 			cache_setvp(ap->a_nch, vp);
1153 		} else if (error == ENOENT) {
1154 			cache_setvp(ap->a_nch, NULL);
1155 		}
1156 		hammer2_inode_unlock(ip);
1157 
1158 		/*
1159 		 * The vp should not be released until after we've disposed
1160 		 * of our locks, because it might cause vop_inactive() to
1161 		 * be called.
1162 		 */
1163 		if (vp)
1164 			vrele(vp);
1165 	} else {
1166 		error = ENOENT;
1167 		cache_setvp(ap->a_nch, NULL);
1168 	}
1169 	hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1170 	KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1171 		("resolve error %d/%p ap %p\n",
1172 		 error, ap->a_nch->ncp->nc_vp, ap));
1173 	LOCKSTOP;
1174 
1175 	return error;
1176 }
1177 
1178 static
1179 int
1180 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1181 {
1182 	hammer2_inode_t *dip;
1183 	hammer2_inode_t *ip;
1184 	int error;
1185 
1186 	LOCKSTART;
1187 	dip = VTOI(ap->a_dvp);
1188 
1189 	if ((ip = dip->pip) == NULL) {
1190 		*ap->a_vpp = NULL;
1191 		LOCKSTOP;
1192 		return ENOENT;
1193 	}
1194 	hammer2_inode_lock(ip, 0);
1195 	*ap->a_vpp = hammer2_igetv(ip, &error);
1196 	hammer2_inode_unlock(ip);
1197 
1198 	LOCKSTOP;
1199 	return error;
1200 }
1201 
1202 static
1203 int
1204 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1205 {
1206 	hammer2_inode_t *dip;
1207 	hammer2_inode_t *nip;
1208 	struct namecache *ncp;
1209 	const uint8_t *name;
1210 	size_t name_len;
1211 	int error;
1212 
1213 	LOCKSTART;
1214 	dip = VTOI(ap->a_dvp);
1215 	if (dip->pmp->ronly) {
1216 		LOCKSTOP;
1217 		return (EROFS);
1218 	}
1219 
1220 	ncp = ap->a_nch->ncp;
1221 	name = ncp->nc_name;
1222 	name_len = ncp->nc_nlen;
1223 
1224 	hammer2_pfs_memory_wait(dip->pmp);
1225 	hammer2_trans_init(dip->pmp, 0);
1226 	nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1227 				   name, name_len, 0,
1228 				   hammer2_trans_newinum(dip->pmp), 0, 0,
1229 				   0, &error);
1230 	if (error) {
1231 		KKASSERT(nip == NULL);
1232 		*ap->a_vpp = NULL;
1233 	} else {
1234 		*ap->a_vpp = hammer2_igetv(nip, &error);
1235 		hammer2_inode_unlock(nip);
1236 	}
1237 	hammer2_trans_done(dip->pmp);
1238 
1239 	if (error == 0) {
1240 		cache_setunresolved(ap->a_nch);
1241 		cache_setvp(ap->a_nch, *ap->a_vpp);
1242 	}
1243 	LOCKSTOP;
1244 	return error;
1245 }
1246 
1247 static
1248 int
1249 hammer2_vop_open(struct vop_open_args *ap)
1250 {
1251 	return vop_stdopen(ap);
1252 }
1253 
1254 /*
1255  * hammer2_vop_advlock { vp, id, op, fl, flags }
1256  */
1257 static
1258 int
1259 hammer2_vop_advlock(struct vop_advlock_args *ap)
1260 {
1261 	hammer2_inode_t *ip = VTOI(ap->a_vp);
1262 	hammer2_off_t size;
1263 
1264 	size = ip->meta.size;
1265 	return (lf_advlock(ap, &ip->advlock, size));
1266 }
1267 
1268 static
1269 int
1270 hammer2_vop_close(struct vop_close_args *ap)
1271 {
1272 	return vop_stdclose(ap);
1273 }
1274 
1275 /*
1276  * hammer2_vop_nlink { nch, dvp, vp, cred }
1277  *
1278  * Create a hardlink from (vp) to {dvp, nch}.
1279  */
1280 static
1281 int
1282 hammer2_vop_nlink(struct vop_nlink_args *ap)
1283 {
1284 	hammer2_xop_nlink_t *xop1;
1285 	hammer2_inode_t *fdip;	/* target directory to create link in */
1286 	hammer2_inode_t *tdip;	/* target directory to create link in */
1287 	hammer2_inode_t *cdip;	/* common parent directory */
1288 	hammer2_inode_t *ip;	/* inode we are hardlinking to */
1289 	struct namecache *ncp;
1290 	const uint8_t *name;
1291 	size_t name_len;
1292 	int error;
1293 
1294 	LOCKSTART;
1295 	tdip = VTOI(ap->a_dvp);
1296 	if (tdip->pmp->ronly) {
1297 		LOCKSTOP;
1298 		return (EROFS);
1299 	}
1300 
1301 	ncp = ap->a_nch->ncp;
1302 	name = ncp->nc_name;
1303 	name_len = ncp->nc_nlen;
1304 
1305 	/*
1306 	 * ip represents the file being hardlinked.  The file could be a
1307 	 * normal file or a hardlink target if it has already been hardlinked.
1308 	 * If ip is a hardlinked target then ip->pip represents the location
1309 	 * of the hardlinked target, NOT the location of the hardlink pointer.
1310 	 *
1311 	 * Bump nlinks and potentially also create or move the hardlink
1312 	 * target in the parent directory common to (ip) and (tdip).  The
1313 	 * consolidation code can modify ip->cluster and ip->pip.  The
1314 	 * returned cluster is locked.
1315 	 */
1316 	ip = VTOI(ap->a_vp);
1317 	hammer2_pfs_memory_wait(ip->pmp);
1318 	hammer2_trans_init(ip->pmp, 0);
1319 
1320 	/*
1321 	 * The common parent directory must be locked first to avoid deadlocks.
1322 	 * Also note that fdip and/or tdip might match cdip.
1323 	 */
1324 	fdip = ip->pip;
1325 	cdip = hammer2_inode_common_parent(fdip, tdip);
1326 	hammer2_inode_lock(cdip, 0);
1327 	hammer2_inode_lock(fdip, 0);
1328 	hammer2_inode_lock(tdip, 0);
1329 	hammer2_inode_lock(ip, 0);
1330 	error = 0;
1331 
1332 	/*
1333 	 * If ip is not a hardlink target we must convert it to a hardlink.
1334 	 * If fdip != cdip we must shift the inode to cdip.
1335 	 */
1336 	if (fdip != cdip || (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1337 		xop1 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
1338 		hammer2_xop_setip2(&xop1->head, ip);
1339 		hammer2_xop_setip3(&xop1->head, cdip);
1340 
1341 		hammer2_xop_start(&xop1->head, hammer2_xop_nlink);
1342 		error = hammer2_xop_collect(&xop1->head, 0);
1343 		hammer2_xop_retire(&xop1->head, HAMMER2_XOPMASK_VOP);
1344 		if (error == ENOENT)
1345 			error = 0;
1346 	}
1347 
1348 	/*
1349 	 * Must synchronize original inode whos chains are now a hardlink
1350 	 * target.  We must match what the backend XOP did to the
1351 	 * chains.
1352 	 */
1353 	if (error == 0 && (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1354 		hammer2_inode_modify(ip);
1355 		ip->meta.name_key = ip->meta.inum;
1356 		ip->meta.name_len = 18;	/* "0x%016jx" */
1357 	}
1358 
1359 	/*
1360 	 * Create the hardlink target and bump nlinks.
1361 	 */
1362 	if (error == 0) {
1363 		hammer2_inode_create(tdip, NULL, NULL,
1364 				     name, name_len, 0,
1365 				     ip->meta.inum,
1366 				     HAMMER2_OBJTYPE_HARDLINK, ip->meta.type,
1367 				     0, &error);
1368 		hammer2_inode_modify(ip);
1369 		++ip->meta.nlinks;
1370 	}
1371 	if (error == 0) {
1372 		cache_setunresolved(ap->a_nch);
1373 		cache_setvp(ap->a_nch, ap->a_vp);
1374 	}
1375 	hammer2_inode_unlock(ip);
1376 	hammer2_inode_unlock(tdip);
1377 	hammer2_inode_unlock(fdip);
1378 	hammer2_inode_unlock(cdip);
1379 	hammer2_inode_drop(cdip);
1380 	hammer2_trans_done(ip->pmp);
1381 
1382 	LOCKSTOP;
1383 	return error;
1384 }
1385 
1386 /*
1387  * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1388  *
1389  * The operating system has already ensured that the directory entry
1390  * does not exist and done all appropriate namespace locking.
1391  */
1392 static
1393 int
1394 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1395 {
1396 	hammer2_inode_t *dip;
1397 	hammer2_inode_t *nip;
1398 	struct namecache *ncp;
1399 	const uint8_t *name;
1400 	size_t name_len;
1401 	int error;
1402 
1403 	LOCKSTART;
1404 	dip = VTOI(ap->a_dvp);
1405 	if (dip->pmp->ronly) {
1406 		LOCKSTOP;
1407 		return (EROFS);
1408 	}
1409 
1410 	ncp = ap->a_nch->ncp;
1411 	name = ncp->nc_name;
1412 	name_len = ncp->nc_nlen;
1413 	hammer2_pfs_memory_wait(dip->pmp);
1414 	hammer2_trans_init(dip->pmp, 0);
1415 
1416 	nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1417 				   name, name_len, 0,
1418 				   hammer2_trans_newinum(dip->pmp), 0, 0,
1419 				   0, &error);
1420 	if (error) {
1421 		KKASSERT(nip == NULL);
1422 		*ap->a_vpp = NULL;
1423 	} else {
1424 		*ap->a_vpp = hammer2_igetv(nip, &error);
1425 		hammer2_inode_unlock(nip);
1426 	}
1427 	hammer2_trans_done(dip->pmp);
1428 
1429 	if (error == 0) {
1430 		cache_setunresolved(ap->a_nch);
1431 		cache_setvp(ap->a_nch, *ap->a_vpp);
1432 	}
1433 	LOCKSTOP;
1434 	return error;
1435 }
1436 
1437 /*
1438  * Make a device node (typically a fifo)
1439  */
1440 static
1441 int
1442 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1443 {
1444 	hammer2_inode_t *dip;
1445 	hammer2_inode_t *nip;
1446 	struct namecache *ncp;
1447 	const uint8_t *name;
1448 	size_t name_len;
1449 	int error;
1450 
1451 	LOCKSTART;
1452 	dip = VTOI(ap->a_dvp);
1453 	if (dip->pmp->ronly) {
1454 		LOCKSTOP;
1455 		return (EROFS);
1456 	}
1457 
1458 	ncp = ap->a_nch->ncp;
1459 	name = ncp->nc_name;
1460 	name_len = ncp->nc_nlen;
1461 	hammer2_pfs_memory_wait(dip->pmp);
1462 	hammer2_trans_init(dip->pmp, 0);
1463 
1464 	nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1465 				   name, name_len, 0,
1466 				   hammer2_trans_newinum(dip->pmp), 0, 0,
1467 				   0, &error);
1468 	if (error) {
1469 		KKASSERT(nip == NULL);
1470 		*ap->a_vpp = NULL;
1471 	} else {
1472 		*ap->a_vpp = hammer2_igetv(nip, &error);
1473 		hammer2_inode_unlock(nip);
1474 	}
1475 	hammer2_trans_done(dip->pmp);
1476 
1477 	if (error == 0) {
1478 		cache_setunresolved(ap->a_nch);
1479 		cache_setvp(ap->a_nch, *ap->a_vpp);
1480 	}
1481 	LOCKSTOP;
1482 	return error;
1483 }
1484 
1485 /*
1486  * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1487  */
1488 static
1489 int
1490 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1491 {
1492 	hammer2_inode_t *dip;
1493 	hammer2_inode_t *nip;
1494 	struct namecache *ncp;
1495 	const uint8_t *name;
1496 	size_t name_len;
1497 	int error;
1498 
1499 	dip = VTOI(ap->a_dvp);
1500 	if (dip->pmp->ronly)
1501 		return (EROFS);
1502 
1503 	ncp = ap->a_nch->ncp;
1504 	name = ncp->nc_name;
1505 	name_len = ncp->nc_nlen;
1506 	hammer2_pfs_memory_wait(dip->pmp);
1507 	hammer2_trans_init(dip->pmp, 0);
1508 
1509 	ap->a_vap->va_type = VLNK;	/* enforce type */
1510 
1511 	nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1512 				   name, name_len, 0,
1513 				   hammer2_trans_newinum(dip->pmp), 0, 0,
1514 				   0, &error);
1515 	if (error) {
1516 		KKASSERT(nip == NULL);
1517 		*ap->a_vpp = NULL;
1518 		hammer2_trans_done(dip->pmp);
1519 		return error;
1520 	}
1521 	*ap->a_vpp = hammer2_igetv(nip, &error);
1522 
1523 	/*
1524 	 * Build the softlink (~like file data) and finalize the namecache.
1525 	 */
1526 	if (error == 0) {
1527 		size_t bytes;
1528 		struct uio auio;
1529 		struct iovec aiov;
1530 
1531 		bytes = strlen(ap->a_target);
1532 
1533 		hammer2_inode_unlock(nip);
1534 		bzero(&auio, sizeof(auio));
1535 		bzero(&aiov, sizeof(aiov));
1536 		auio.uio_iov = &aiov;
1537 		auio.uio_segflg = UIO_SYSSPACE;
1538 		auio.uio_rw = UIO_WRITE;
1539 		auio.uio_resid = bytes;
1540 		auio.uio_iovcnt = 1;
1541 		auio.uio_td = curthread;
1542 		aiov.iov_base = ap->a_target;
1543 		aiov.iov_len = bytes;
1544 		error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1545 		/* XXX handle error */
1546 		error = 0;
1547 	} else {
1548 		hammer2_inode_unlock(nip);
1549 	}
1550 	hammer2_trans_done(dip->pmp);
1551 
1552 	/*
1553 	 * Finalize namecache
1554 	 */
1555 	if (error == 0) {
1556 		cache_setunresolved(ap->a_nch);
1557 		cache_setvp(ap->a_nch, *ap->a_vpp);
1558 		/* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1559 	}
1560 	return error;
1561 }
1562 
1563 /*
1564  * hammer2_vop_nremove { nch, dvp, cred }
1565  */
1566 static
1567 int
1568 hammer2_vop_nremove(struct vop_nremove_args *ap)
1569 {
1570 	hammer2_xop_unlink_t *xop;
1571 	hammer2_inode_t *dip;
1572 	hammer2_inode_t *ip;
1573 	struct namecache *ncp;
1574 	int error;
1575 	int isopen;
1576 
1577 	LOCKSTART;
1578 	dip = VTOI(ap->a_dvp);
1579 	if (dip->pmp->ronly) {
1580 		LOCKSTOP;
1581 		return(EROFS);
1582 	}
1583 
1584 	ncp = ap->a_nch->ncp;
1585 
1586 	hammer2_pfs_memory_wait(dip->pmp);
1587 	hammer2_trans_init(dip->pmp, 0);
1588 	hammer2_inode_lock(dip, 0);
1589 
1590 	/*
1591 	 * The unlink XOP unlinks the path from the directory and
1592 	 * locates and returns the cluster associated with the real inode.
1593 	 * We have to handle nlinks here on the frontend.
1594 	 */
1595 	xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1596 	hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1597 	isopen = cache_isopen(ap->a_nch);
1598 	xop->isdir = 0;
1599 	xop->dopermanent = isopen ?  0 : HAMMER2_DELETE_PERMANENT;
1600 	hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1601 
1602 	/*
1603 	 * Collect the real inode and adjust nlinks, destroy the real
1604 	 * inode if nlinks transitions to 0 and it was the real inode
1605 	 * (else it has already been removed).
1606 	 */
1607 	error = hammer2_xop_collect(&xop->head, 0);
1608 	hammer2_inode_unlock(dip);
1609 
1610 	if (error == 0) {
1611 		ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1612 		hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1613 		if (ip) {
1614 			hammer2_inode_unlink_finisher(ip, isopen);
1615 			hammer2_inode_unlock(ip);
1616 		}
1617 	} else {
1618 		hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1619 	}
1620 
1621 	hammer2_inode_run_sideq(dip->pmp);
1622 	hammer2_trans_done(dip->pmp);
1623 	if (error == 0)
1624 		cache_unlink(ap->a_nch);
1625 	LOCKSTOP;
1626 	return (error);
1627 }
1628 
1629 /*
1630  * hammer2_vop_nrmdir { nch, dvp, cred }
1631  */
1632 static
1633 int
1634 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1635 {
1636 	hammer2_xop_unlink_t *xop;
1637 	hammer2_inode_t *dip;
1638 	hammer2_inode_t *ip;
1639 	struct namecache *ncp;
1640 	int isopen;
1641 	int error;
1642 
1643 	LOCKSTART;
1644 	dip = VTOI(ap->a_dvp);
1645 	if (dip->pmp->ronly) {
1646 		LOCKSTOP;
1647 		return(EROFS);
1648 	}
1649 
1650 	hammer2_pfs_memory_wait(dip->pmp);
1651 	hammer2_trans_init(dip->pmp, 0);
1652 	hammer2_inode_lock(dip, 0);
1653 
1654 	xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1655 
1656 	ncp = ap->a_nch->ncp;
1657 	hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1658 	isopen = cache_isopen(ap->a_nch);
1659 	xop->isdir = 1;
1660 	xop->dopermanent = isopen ?  0 : HAMMER2_DELETE_PERMANENT;
1661 	hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1662 
1663 	/*
1664 	 * Collect the real inode and adjust nlinks, destroy the real
1665 	 * inode if nlinks transitions to 0 and it was the real inode
1666 	 * (else it has already been removed).
1667 	 */
1668 	error = hammer2_xop_collect(&xop->head, 0);
1669 	hammer2_inode_unlock(dip);
1670 
1671 	if (error == 0) {
1672 		ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1673 		hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1674 		if (ip) {
1675 			hammer2_inode_unlink_finisher(ip, isopen);
1676 			hammer2_inode_unlock(ip);
1677 		}
1678 	} else {
1679 		hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1680 	}
1681 	hammer2_inode_run_sideq(dip->pmp);
1682 	hammer2_trans_done(dip->pmp);
1683 	if (error == 0)
1684 		cache_unlink(ap->a_nch);
1685 	LOCKSTOP;
1686 	return (error);
1687 }
1688 
1689 /*
1690  * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1691  */
1692 static
1693 int
1694 hammer2_vop_nrename(struct vop_nrename_args *ap)
1695 {
1696 	struct namecache *fncp;
1697 	struct namecache *tncp;
1698 	hammer2_inode_t *cdip;
1699 	hammer2_inode_t *fdip;
1700 	hammer2_inode_t *tdip;
1701 	hammer2_inode_t *ip;
1702 	const uint8_t *fname;
1703 	size_t fname_len;
1704 	const uint8_t *tname;
1705 	size_t tname_len;
1706 	int error;
1707 	int tnch_error;
1708 	hammer2_key_t tlhc;
1709 
1710 	if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1711 		return(EXDEV);
1712 	if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1713 		return(EXDEV);
1714 
1715 	fdip = VTOI(ap->a_fdvp);	/* source directory */
1716 	tdip = VTOI(ap->a_tdvp);	/* target directory */
1717 
1718 	if (fdip->pmp->ronly)
1719 		return(EROFS);
1720 
1721 	LOCKSTART;
1722 	fncp = ap->a_fnch->ncp;		/* entry name in source */
1723 	fname = fncp->nc_name;
1724 	fname_len = fncp->nc_nlen;
1725 
1726 	tncp = ap->a_tnch->ncp;		/* entry name in target */
1727 	tname = tncp->nc_name;
1728 	tname_len = tncp->nc_nlen;
1729 
1730 	hammer2_pfs_memory_wait(tdip->pmp);
1731 	hammer2_trans_init(tdip->pmp, 0);
1732 
1733 	/*
1734 	 * ip is the inode being renamed.  If this is a hardlink then
1735 	 * ip represents the actual file and not the hardlink marker.
1736 	 */
1737 	ip = VTOI(fncp->nc_vp);
1738 
1739 	/*
1740 	 * The common parent directory must be locked first to avoid deadlocks.
1741 	 * Also note that fdip and/or tdip might match cdip.
1742 	 */
1743 	cdip = hammer2_inode_common_parent(ip->pip, tdip);
1744 	hammer2_inode_lock(cdip, 0);
1745 	hammer2_inode_lock(fdip, 0);
1746 	hammer2_inode_lock(tdip, 0);
1747 	hammer2_inode_ref(ip);		/* extra ref */
1748 	error = 0;
1749 
1750 	/*
1751 	 * If ip is a hardlink target and fdip != cdip we must shift the
1752 	 * inode to cdip.
1753 	 */
1754 	if (fdip != cdip &&
1755 	    (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1756 		hammer2_xop_nlink_t *xop1;
1757 
1758 		xop1 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
1759 		hammer2_xop_setip2(&xop1->head, ip);
1760 		hammer2_xop_setip3(&xop1->head, cdip);
1761 
1762 		hammer2_xop_start(&xop1->head, hammer2_xop_nlink);
1763 		error = hammer2_xop_collect(&xop1->head, 0);
1764 		hammer2_xop_retire(&xop1->head, HAMMER2_XOPMASK_VOP);
1765 	}
1766 
1767 	/*
1768 	 * Delete the target namespace.
1769 	 */
1770 	{
1771 		hammer2_xop_unlink_t *xop2;
1772 		hammer2_inode_t *tip;
1773 		int isopen;
1774 
1775 		/*
1776 		 * The unlink XOP unlinks the path from the directory and
1777 		 * locates and returns the cluster associated with the real
1778 		 * inode.  We have to handle nlinks here on the frontend.
1779 		 */
1780 		xop2 = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
1781 		hammer2_xop_setname(&xop2->head, tname, tname_len);
1782 		isopen = cache_isopen(ap->a_tnch);
1783 		xop2->isdir = -1;
1784 		xop2->dopermanent = isopen ?  0 : HAMMER2_DELETE_PERMANENT;
1785 		hammer2_xop_start(&xop2->head, hammer2_xop_unlink);
1786 
1787 		/*
1788 		 * Collect the real inode and adjust nlinks, destroy the real
1789 		 * inode if nlinks transitions to 0 and it was the real inode
1790 		 * (else it has already been removed).
1791 		 */
1792 		tnch_error = hammer2_xop_collect(&xop2->head, 0);
1793 		/* hammer2_inode_unlock(tdip); */
1794 
1795 		if (tnch_error == 0) {
1796 			tip = hammer2_inode_get(tdip->pmp, NULL,
1797 						&xop2->head.cluster, -1);
1798 			hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
1799 			if (tip) {
1800 				hammer2_inode_unlink_finisher(tip, isopen);
1801 				hammer2_inode_unlock(tip);
1802 			}
1803 		} else {
1804 			hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
1805 		}
1806 		/* hammer2_inode_lock(tdip, 0); */
1807 
1808 		if (tnch_error && tnch_error != ENOENT) {
1809 			error = tnch_error;
1810 			goto done2;
1811 		}
1812 	}
1813 
1814 	/*
1815 	 * Resolve the collision space for (tdip, tname, tname_len)
1816 	 *
1817 	 * tdip must be held exclusively locked to prevent races.
1818 	 */
1819 	{
1820 		hammer2_xop_scanlhc_t *sxop;
1821 		hammer2_tid_t lhcbase;
1822 
1823 		tlhc = hammer2_dirhash(tname, tname_len);
1824 		lhcbase = tlhc;
1825 		sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
1826 		sxop->lhc = tlhc;
1827 		hammer2_xop_start(&sxop->head, hammer2_xop_scanlhc);
1828 		while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
1829 			if (tlhc != sxop->head.cluster.focus->bref.key)
1830 				break;
1831 			++tlhc;
1832 		}
1833 		hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
1834 
1835 		if (error) {
1836 			if (error != ENOENT)
1837 				goto done2;
1838 			++tlhc;
1839 			error = 0;
1840 		}
1841 		if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) {
1842 			error = ENOSPC;
1843 			goto done2;
1844 		}
1845 	}
1846 
1847 	/*
1848 	 * Everything is setup, do the rename.
1849 	 *
1850 	 * We have to synchronize ip->meta to the underlying operation.
1851 	 *
1852 	 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
1853 	 *	 unlinking elements from their directories.  Locking
1854 	 *	 the nlinks field does not lock the whole inode.
1855 	 */
1856 	hammer2_inode_lock(ip, 0);
1857 	if (error == 0) {
1858 		hammer2_xop_nrename_t *xop4;
1859 
1860 		xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
1861 		xop4->lhc = tlhc;
1862 		xop4->ip_key = ip->meta.name_key;
1863 		hammer2_xop_setip2(&xop4->head, ip);
1864 		hammer2_xop_setip3(&xop4->head, tdip);
1865 		hammer2_xop_setname(&xop4->head, fname, fname_len);
1866 		hammer2_xop_setname2(&xop4->head, tname, tname_len);
1867 		hammer2_xop_start(&xop4->head, hammer2_xop_nrename);
1868 
1869 		error = hammer2_xop_collect(&xop4->head, 0);
1870 		hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP);
1871 
1872 		if (error == ENOENT)
1873 			error = 0;
1874 		if (error == 0 &&
1875 		    (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1876 			hammer2_inode_modify(ip);
1877 			ip->meta.name_len = tname_len;
1878 			ip->meta.name_key = tlhc;
1879 
1880 		}
1881 	}
1882 
1883 	/*
1884 	 * Fixup ip->pip if we were renaming the actual file and not a
1885 	 * hardlink pointer.
1886 	 */
1887 	if (error == 0 && (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1888 		hammer2_inode_t *opip;
1889 
1890 		if (ip->pip != tdip) {
1891 			hammer2_inode_ref(tdip);
1892 			opip = ip->pip;
1893 			ip->pip = tdip;
1894 			if (opip)
1895 				hammer2_inode_drop(opip);
1896 		}
1897 	}
1898 	hammer2_inode_unlock(ip);
1899 done2:
1900 	hammer2_inode_unlock(tdip);
1901 	hammer2_inode_unlock(fdip);
1902 	hammer2_inode_unlock(cdip);
1903 	hammer2_inode_drop(ip);
1904 	hammer2_inode_drop(cdip);
1905 	hammer2_inode_run_sideq(fdip->pmp);
1906 	hammer2_trans_done(tdip->pmp);
1907 
1908 	/*
1909 	 * Issue the namecache update after unlocking all the internal
1910 	 * hammer structures, otherwise we might deadlock.
1911 	 */
1912 	if (tnch_error == 0) {
1913 		cache_unlink(ap->a_tnch);
1914 		cache_setunresolved(ap->a_tnch);
1915 	}
1916 	if (error == 0)
1917 		cache_rename(ap->a_fnch, ap->a_tnch);
1918 
1919 	LOCKSTOP;
1920 	return (error);
1921 }
1922 
1923 /*
1924  * hammer2_vop_ioctl { vp, command, data, fflag, cred }
1925  */
1926 static
1927 int
1928 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
1929 {
1930 	hammer2_inode_t *ip;
1931 	int error;
1932 
1933 	LOCKSTART;
1934 	ip = VTOI(ap->a_vp);
1935 
1936 	error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
1937 			      ap->a_fflag, ap->a_cred);
1938 	LOCKSTOP;
1939 	return (error);
1940 }
1941 
1942 static
1943 int
1944 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
1945 {
1946 	struct mount *mp;
1947 	hammer2_pfs_t *pmp;
1948 	int rc;
1949 
1950 	LOCKSTART;
1951 	switch (ap->a_op) {
1952 	case (MOUNTCTL_SET_EXPORT):
1953 		mp = ap->a_head.a_ops->head.vv_mount;
1954 		pmp = MPTOPMP(mp);
1955 
1956 		if (ap->a_ctllen != sizeof(struct export_args))
1957 			rc = (EINVAL);
1958 		else
1959 			rc = vfs_export(mp, &pmp->export,
1960 					(const struct export_args *)ap->a_ctl);
1961 		break;
1962 	default:
1963 		rc = vop_stdmountctl(ap);
1964 		break;
1965 	}
1966 	LOCKSTOP;
1967 	return (rc);
1968 }
1969 
1970 /*
1971  * KQFILTER
1972  */
1973 static void filt_hammer2detach(struct knote *kn);
1974 static int filt_hammer2read(struct knote *kn, long hint);
1975 static int filt_hammer2write(struct knote *kn, long hint);
1976 static int filt_hammer2vnode(struct knote *kn, long hint);
1977 
1978 static struct filterops hammer2read_filtops =
1979 	{ FILTEROP_ISFD | FILTEROP_MPSAFE,
1980 	  NULL, filt_hammer2detach, filt_hammer2read };
1981 static struct filterops hammer2write_filtops =
1982 	{ FILTEROP_ISFD | FILTEROP_MPSAFE,
1983 	  NULL, filt_hammer2detach, filt_hammer2write };
1984 static struct filterops hammer2vnode_filtops =
1985 	{ FILTEROP_ISFD | FILTEROP_MPSAFE,
1986 	  NULL, filt_hammer2detach, filt_hammer2vnode };
1987 
1988 static
1989 int
1990 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
1991 {
1992 	struct vnode *vp = ap->a_vp;
1993 	struct knote *kn = ap->a_kn;
1994 
1995 	switch (kn->kn_filter) {
1996 	case EVFILT_READ:
1997 		kn->kn_fop = &hammer2read_filtops;
1998 		break;
1999 	case EVFILT_WRITE:
2000 		kn->kn_fop = &hammer2write_filtops;
2001 		break;
2002 	case EVFILT_VNODE:
2003 		kn->kn_fop = &hammer2vnode_filtops;
2004 		break;
2005 	default:
2006 		return (EOPNOTSUPP);
2007 	}
2008 
2009 	kn->kn_hook = (caddr_t)vp;
2010 
2011 	knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2012 
2013 	return(0);
2014 }
2015 
2016 static void
2017 filt_hammer2detach(struct knote *kn)
2018 {
2019 	struct vnode *vp = (void *)kn->kn_hook;
2020 
2021 	knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2022 }
2023 
2024 static int
2025 filt_hammer2read(struct knote *kn, long hint)
2026 {
2027 	struct vnode *vp = (void *)kn->kn_hook;
2028 	hammer2_inode_t *ip = VTOI(vp);
2029 	off_t off;
2030 
2031 	if (hint == NOTE_REVOKE) {
2032 		kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2033 		return(1);
2034 	}
2035 	off = ip->meta.size - kn->kn_fp->f_offset;
2036 	kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2037 	if (kn->kn_sfflags & NOTE_OLDAPI)
2038 		return(1);
2039 	return (kn->kn_data != 0);
2040 }
2041 
2042 
2043 static int
2044 filt_hammer2write(struct knote *kn, long hint)
2045 {
2046 	if (hint == NOTE_REVOKE)
2047 		kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2048 	kn->kn_data = 0;
2049 	return (1);
2050 }
2051 
2052 static int
2053 filt_hammer2vnode(struct knote *kn, long hint)
2054 {
2055 	if (kn->kn_sfflags & hint)
2056 		kn->kn_fflags |= hint;
2057 	if (hint == NOTE_REVOKE) {
2058 		kn->kn_flags |= (EV_EOF | EV_NODATA);
2059 		return (1);
2060 	}
2061 	return (kn->kn_fflags != 0);
2062 }
2063 
2064 /*
2065  * FIFO VOPS
2066  */
2067 static
2068 int
2069 hammer2_vop_markatime(struct vop_markatime_args *ap)
2070 {
2071 	hammer2_inode_t *ip;
2072 	struct vnode *vp;
2073 
2074 	vp = ap->a_vp;
2075 	ip = VTOI(vp);
2076 
2077 	if (ip->pmp->ronly)
2078 		return(EROFS);
2079 	return(0);
2080 }
2081 
2082 static
2083 int
2084 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2085 {
2086 	int error;
2087 
2088 	error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2089 	if (error)
2090 		error = hammer2_vop_kqfilter(ap);
2091 	return(error);
2092 }
2093 
2094 /*
2095  * VOPS vector
2096  */
2097 struct vop_ops hammer2_vnode_vops = {
2098 	.vop_default	= vop_defaultop,
2099 	.vop_fsync	= hammer2_vop_fsync,
2100 	.vop_getpages	= vop_stdgetpages,
2101 	.vop_putpages	= vop_stdputpages,
2102 	.vop_access	= hammer2_vop_access,
2103 	.vop_advlock	= hammer2_vop_advlock,
2104 	.vop_close	= hammer2_vop_close,
2105 	.vop_nlink	= hammer2_vop_nlink,
2106 	.vop_ncreate	= hammer2_vop_ncreate,
2107 	.vop_nsymlink	= hammer2_vop_nsymlink,
2108 	.vop_nremove	= hammer2_vop_nremove,
2109 	.vop_nrmdir	= hammer2_vop_nrmdir,
2110 	.vop_nrename	= hammer2_vop_nrename,
2111 	.vop_getattr	= hammer2_vop_getattr,
2112 	.vop_setattr	= hammer2_vop_setattr,
2113 	.vop_readdir	= hammer2_vop_readdir,
2114 	.vop_readlink	= hammer2_vop_readlink,
2115 	.vop_getpages	= vop_stdgetpages,
2116 	.vop_putpages	= vop_stdputpages,
2117 	.vop_read	= hammer2_vop_read,
2118 	.vop_write	= hammer2_vop_write,
2119 	.vop_open	= hammer2_vop_open,
2120 	.vop_inactive	= hammer2_vop_inactive,
2121 	.vop_reclaim 	= hammer2_vop_reclaim,
2122 	.vop_nresolve	= hammer2_vop_nresolve,
2123 	.vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2124 	.vop_nmkdir 	= hammer2_vop_nmkdir,
2125 	.vop_nmknod 	= hammer2_vop_nmknod,
2126 	.vop_ioctl	= hammer2_vop_ioctl,
2127 	.vop_mountctl	= hammer2_vop_mountctl,
2128 	.vop_bmap	= hammer2_vop_bmap,
2129 	.vop_strategy	= hammer2_vop_strategy,
2130         .vop_kqfilter	= hammer2_vop_kqfilter
2131 };
2132 
2133 struct vop_ops hammer2_spec_vops = {
2134         .vop_default =          vop_defaultop,
2135         .vop_fsync =            hammer2_vop_fsync,
2136         .vop_read =             vop_stdnoread,
2137         .vop_write =            vop_stdnowrite,
2138         .vop_access =           hammer2_vop_access,
2139         .vop_close =            hammer2_vop_close,
2140         .vop_markatime =        hammer2_vop_markatime,
2141         .vop_getattr =          hammer2_vop_getattr,
2142         .vop_inactive =         hammer2_vop_inactive,
2143         .vop_reclaim =          hammer2_vop_reclaim,
2144         .vop_setattr =          hammer2_vop_setattr
2145 };
2146 
2147 struct vop_ops hammer2_fifo_vops = {
2148         .vop_default =          fifo_vnoperate,
2149         .vop_fsync =            hammer2_vop_fsync,
2150 #if 0
2151         .vop_read =             hammer2_vop_fiforead,
2152         .vop_write =            hammer2_vop_fifowrite,
2153 #endif
2154         .vop_access =           hammer2_vop_access,
2155 #if 0
2156         .vop_close =            hammer2_vop_fifoclose,
2157 #endif
2158         .vop_markatime =        hammer2_vop_markatime,
2159         .vop_getattr =          hammer2_vop_getattr,
2160         .vop_inactive =         hammer2_vop_inactive,
2161         .vop_reclaim =          hammer2_vop_reclaim,
2162         .vop_setattr =          hammer2_vop_setattr,
2163         .vop_kqfilter =         hammer2_vop_fifokqfilter
2164 };
2165 
2166