1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2022 Tomohiro Kusumi <tkusumi@netbsd.org>
5  * Copyright (c) 2011-2022 The DragonFly Project.  All rights reserved.
6  *
7  * This code is derived from software contributed to The DragonFly Project
8  * by Matthew Dillon <dillon@dragonflybsd.org>
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  * 3. Neither the name of The DragonFly Project nor the names of its
21  *    contributors may be used to endorse or promote products derived
22  *    from this software without specific, prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
28  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 /*
38  * Kernel Filesystem interface
39  *
40  * NOTE! local ipdata pointers must be reloaded on any modifying operation
41  *	 to the inode as its underlying chain may have changed.
42  */
43 
44 /*
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/fcntl.h>
49 #include <sys/buf.h>
50 #include <sys/proc.h>
51 #include <sys/mount.h>
52 #include <sys/vnode.h>
53 #include <sys/mountctl.h>
54 #include <sys/dirent.h>
55 #include <sys/uio.h>
56 #include <sys/objcache.h>
57 #include <sys/event.h>
58 #include <sys/file.h>
59 #include <vfs/fifofs/fifo.h>
60 */
61 
62 #include "hammer2.h"
63 
64 static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio,
65 				int seqcount);
66 static int hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
67 				int ioflag, int seqcount);
68 static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize);
69 static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize);
70 
71 /*
72  * Last reference to a vnode is going away but it is still cached.
73  */
74 static
75 int
76 hammer2_vop_inactive(struct vop_inactive_args *ap)
77 {
78 #if 0
79 	hammer2_inode_t *ip;
80 	struct m_vnode *vp;
81 
82 	vp = ap->a_vp;
83 	ip = VTOI(vp);
84 
85 	/*
86 	 * Degenerate case
87 	 */
88 	if (ip == NULL) {
89 		vrecycle(vp);
90 		return (0);
91 	}
92 
93 	/*
94 	 * Aquire the inode lock to interlock against vp updates via
95 	 * the inode path and file deletions and such (which can be
96 	 * namespace-only operations that might not hold the vnode).
97 	 */
98 	hammer2_inode_lock(ip, 0);
99 	if (ip->flags & HAMMER2_INODE_ISUNLINKED) {
100 		hammer2_key_t lbase;
101 		int nblksize;
102 
103 		/*
104 		 * If the inode has been unlinked we can throw away all
105 		 * buffers (dirty or not) and clean the file out.
106 		 *
107 		 * Because vrecycle() calls are not guaranteed, try to
108 		 * dispose of the inode as much as possible right here.
109 		 */
110 		nblksize = hammer2_calc_logical(ip, 0, &lbase, NULL);
111 		nvtruncbuf(vp, 0, nblksize, 0, 0);
112 
113 		/*
114 		 * Delete the file on-media.
115 		 */
116 		if ((ip->flags & HAMMER2_INODE_DELETING) == 0) {
117 			atomic_set_int(&ip->flags, HAMMER2_INODE_DELETING);
118 			hammer2_inode_delayed_sideq(ip);
119 		}
120 		hammer2_inode_unlock(ip);
121 
122 		/*
123 		 * Recycle immediately if possible
124 		 */
125 		vrecycle(vp);
126 	} else {
127 		hammer2_inode_unlock(ip);
128 	}
129 	return (0);
130 #endif
131 	return (EOPNOTSUPP);
132 }
133 
134 /*
135  * Reclaim a vnode so that it can be reused; after the inode is
136  * disassociated, the filesystem must manage it alone.
137  */
138 static
139 int
140 hammer2_vop_reclaim(struct vop_reclaim_args *ap)
141 {
142 	hammer2_inode_t *ip;
143 	struct m_vnode *vp;
144 
145 	vp = ap->a_vp;
146 	ip = VTOI(vp);
147 	if (ip == NULL)
148 		return(0);
149 
150 	/*
151 	 * NOTE! We do not attempt to flush chains here, flushing is
152 	 *	 really fragile and could also deadlock.
153 	 */
154 	vclrisdirty(vp);
155 
156 	/*
157 	 * The inode lock is required to disconnect it.
158 	 */
159 	hammer2_inode_lock(ip, 0);
160 	vp->v_data = NULL;
161 	ip->vp = NULL;
162 
163 	/*
164 	 * Delete the file on-media.  This should have been handled by the
165 	 * inactivation.  The operation is likely still queued on the inode
166 	 * though so only complain if the stars don't align.
167 	 */
168 	if ((ip->flags & (HAMMER2_INODE_ISUNLINKED | HAMMER2_INODE_DELETING)) ==
169 	    HAMMER2_INODE_ISUNLINKED)
170 	{
171 		assert(0);
172 		atomic_set_int(&ip->flags, HAMMER2_INODE_DELETING);
173 		hammer2_inode_delayed_sideq(ip);
174 		kprintf("hammer2: vp=%p ip=%p unlinked but not disposed\n",
175 			vp, ip);
176 	}
177 	hammer2_inode_unlock(ip);
178 
179 	/*
180 	 * Modified inodes will already be on SIDEQ or SYNCQ, no further
181 	 * action is needed.
182 	 *
183 	 * We cannot safely synchronize the inode from inside the reclaim
184 	 * due to potentially deep locks held as-of when the reclaim occurs.
185 	 * Interactions and potential deadlocks abound.  We also can't do it
186 	 * here without desynchronizing from the related directory entrie(s).
187 	 */
188 	hammer2_inode_drop(ip);			/* vp ref */
189 
190 	/*
191 	 * XXX handle background sync when ip dirty, kernel will no longer
192 	 * notify us regarding this inode because there is no longer a
193 	 * vnode attached to it.
194 	 */
195 
196 	return (0);
197 }
198 
199 int
200 hammer2_reclaim(struct m_vnode *vp)
201 {
202 	struct vop_reclaim_args ap = {
203 		.a_vp = vp,
204 	};
205 
206 	return hammer2_vop_reclaim(&ap);
207 }
208 
209 /*
210  * Currently this function synchronizes the front-end inode state to the
211  * backend chain topology, then flushes the inode's chain and sub-topology
212  * to backend media.  This function does not flush the root topology down to
213  * the inode.
214  */
215 static
216 int
217 hammer2_vop_fsync(struct vop_fsync_args *ap)
218 {
219 #if 0
220 	hammer2_inode_t *ip;
221 	struct m_vnode *vp;
222 	int error1;
223 	int error2;
224 
225 	vp = ap->a_vp;
226 	ip = VTOI(vp);
227 	error1 = 0;
228 
229 	hammer2_trans_init(ip->pmp, 0);
230 
231 	/*
232 	 * Flush dirty buffers in the file's logical buffer cache.
233 	 * It is best to wait for the strategy code to commit the
234 	 * buffers to the device's backing buffer cache before
235 	 * then trying to flush the inode.
236 	 *
237 	 * This should be quick, but certain inode modifications cached
238 	 * entirely in the hammer2_inode structure may not trigger a
239 	 * buffer read until the flush so the fsync can wind up also
240 	 * doing scattered reads.
241 	 */
242 	vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
243 	bio_track_wait(&vp->v_track_write, 0, 0);
244 
245 	/*
246 	 * Flush any inode changes
247 	 */
248 	hammer2_inode_lock(ip, 0);
249 	if (ip->flags & (HAMMER2_INODE_RESIZED|HAMMER2_INODE_MODIFIED))
250 		error1 = hammer2_inode_chain_sync(ip);
251 
252 	/*
253 	 * Flush dirty chains related to the inode.
254 	 *
255 	 * NOTE! We are not in a flush transaction.  The inode remains on
256 	 *	 the sideq so the filesystem syncer can synchronize it to
257 	 *	 the volume root.
258 	 */
259 	error2 = hammer2_inode_chain_flush(ip, HAMMER2_XOP_INODE_STOP);
260 	if (error2)
261 		error1 = error2;
262 
263 	/*
264 	 * We may be able to clear the vnode dirty flag.
265 	 */
266 	if ((ip->flags & (HAMMER2_INODE_MODIFIED |
267 			  HAMMER2_INODE_RESIZED |
268 			  HAMMER2_INODE_DIRTYDATA)) == 0 &&
269 	    RB_EMPTY(&vp->v_rbdirty_tree) &&
270 	    !bio_track_active(&vp->v_track_write)) {
271 		vclrisdirty(vp);
272 	}
273 	hammer2_inode_unlock(ip);
274 	hammer2_trans_done(ip->pmp, 0);
275 
276 	return (error1);
277 #endif
278 	return (EOPNOTSUPP);
279 }
280 
281 /*
282  * No lock needed, just handle ip->update
283  */
284 static
285 int
286 hammer2_vop_access(struct vop_access_args *ap)
287 {
288 #if 0
289 	hammer2_inode_t *ip = VTOI(ap->a_vp);
290 	uid_t uid;
291 	gid_t gid;
292 	mode_t mode;
293 	uint32_t uflags;
294 	int error;
295 	int update;
296 
297 retry:
298 	update = spin_access_start(&ip->cluster_spin);
299 
300 	/*hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);*/
301 	uid = hammer2_to_unix_xid(&ip->meta.uid);
302 	gid = hammer2_to_unix_xid(&ip->meta.gid);
303 	mode = ip->meta.mode;
304 	uflags = ip->meta.uflags;
305 	/*hammer2_inode_unlock(ip);*/
306 
307 	if (__predict_false(spin_access_end(&ip->cluster_spin, update)))
308 		goto retry;
309 
310 	error = vop_helper_access(ap, uid, gid, mode, uflags);
311 
312 	return (error);
313 #endif
314 	return (EOPNOTSUPP);
315 }
316 
317 static
318 int
319 hammer2_vop_getattr(struct vop_getattr_args *ap)
320 {
321 #if 0
322 	hammer2_pfs_t *pmp;
323 	hammer2_inode_t *ip;
324 	struct m_vnode *vp;
325 	struct vattr *vap;
326 	int update;
327 
328 	vp = ap->a_vp;
329 	vap = ap->a_vap;
330 
331 	ip = VTOI(vp);
332 	pmp = ip->pmp;
333 
334 retry:
335 	update = spin_access_start(&ip->cluster_spin);
336 
337 	vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
338 	vap->va_fileid = ip->meta.inum;
339 	vap->va_mode = ip->meta.mode;
340 	vap->va_nlink = ip->meta.nlinks;
341 	vap->va_uid = hammer2_to_unix_xid(&ip->meta.uid);
342 	vap->va_gid = hammer2_to_unix_xid(&ip->meta.gid);
343 	vap->va_rmajor = 0;
344 	vap->va_rminor = 0;
345 	vap->va_size = ip->meta.size;	/* protected by shared lock */
346 	vap->va_blocksize = HAMMER2_PBUFSIZE;
347 	vap->va_flags = ip->meta.uflags;
348 	hammer2_time_to_timespec(ip->meta.ctime, &vap->va_ctime);
349 	hammer2_time_to_timespec(ip->meta.mtime, &vap->va_mtime);
350 	hammer2_time_to_timespec(ip->meta.mtime, &vap->va_atime);
351 	vap->va_gen = 1;
352 	vap->va_bytes = 0;
353 	if (ip->meta.type == HAMMER2_OBJTYPE_DIRECTORY) {
354 		/*
355 		 * Can't really calculate directory use sans the files under
356 		 * it, just assume one block for now.
357 		 */
358 		vap->va_bytes += HAMMER2_INODE_BYTES;
359 	} else {
360 		vap->va_bytes = hammer2_inode_data_count(ip);
361 	}
362 	vap->va_type = hammer2_get_vtype(ip->meta.type);
363 	vap->va_filerev = 0;
364 	vap->va_uid_uuid = ip->meta.uid;
365 	vap->va_gid_uuid = ip->meta.gid;
366 	vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
367 			  VA_FSID_UUID_VALID;
368 
369 	if (__predict_false(spin_access_end(&ip->cluster_spin, update)))
370 		goto retry;
371 
372 	return (0);
373 #endif
374 	return (EOPNOTSUPP);
375 }
376 
377 static
378 int
379 hammer2_vop_getattr_lite(struct vop_getattr_lite_args *ap)
380 {
381 #if 0
382 	hammer2_pfs_t *pmp;
383 	hammer2_inode_t *ip;
384 	struct m_vnode *vp;
385 	struct vattr_lite *lvap;
386 	int update;
387 
388 	vp = ap->a_vp;
389 	lvap = ap->a_lvap;
390 
391 	ip = VTOI(vp);
392 	pmp = ip->pmp;
393 
394 retry:
395 	update = spin_access_start(&ip->cluster_spin);
396 
397 #if 0
398 	vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
399 	vap->va_fileid = ip->meta.inum;
400 #endif
401 	lvap->va_mode = ip->meta.mode;
402 	lvap->va_nlink = ip->meta.nlinks;
403 	lvap->va_uid = hammer2_to_unix_xid(&ip->meta.uid);
404 	lvap->va_gid = hammer2_to_unix_xid(&ip->meta.gid);
405 #if 0
406 	vap->va_rmajor = 0;
407 	vap->va_rminor = 0;
408 #endif
409 	lvap->va_size = ip->meta.size;
410 #if 0
411 	vap->va_blocksize = HAMMER2_PBUFSIZE;
412 #endif
413 	lvap->va_flags = ip->meta.uflags;
414 	lvap->va_type = hammer2_get_vtype(ip->meta.type);
415 #if 0
416 	vap->va_filerev = 0;
417 	vap->va_uid_uuid = ip->meta.uid;
418 	vap->va_gid_uuid = ip->meta.gid;
419 	vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
420 			  VA_FSID_UUID_VALID;
421 #endif
422 
423 	if (__predict_false(spin_access_end(&ip->cluster_spin, update)))
424 		goto retry;
425 
426 	return (0);
427 #endif
428 	return (EOPNOTSUPP);
429 }
430 
431 static
432 int
433 hammer2_vop_setattr(struct vop_setattr_args *ap)
434 {
435 #if 0
436 	hammer2_inode_t *ip;
437 	struct m_vnode *vp;
438 	struct vattr *vap;
439 	int error;
440 	int kflags = 0;
441 	uint64_t ctime;
442 
443 	vp = ap->a_vp;
444 	vap = ap->a_vap;
445 	hammer2_update_time(&ctime);
446 
447 	ip = VTOI(vp);
448 
449 	if (ip->pmp->ronly)
450 		return (EROFS);
451 
452 	/*
453 	 * Normally disallow setattr if there is no space, unless we
454 	 * are in emergency mode (might be needed to chflags -R noschg
455 	 * files prior to removal).
456 	 */
457 	if ((ip->pmp->flags & HAMMER2_PMPF_EMERG) == 0 &&
458 	    hammer2_vfs_enospace(ip, 0, ap->a_cred) > 1) {
459 		return (ENOSPC);
460 	}
461 
462 	hammer2_trans_init(ip->pmp, 0);
463 	hammer2_inode_lock(ip, 0);
464 	error = 0;
465 
466 	if (vap->va_flags != VNOVAL) {
467 		uint32_t flags;
468 
469 		flags = ip->meta.uflags;
470 		error = vop_helper_setattr_flags(&flags, vap->va_flags,
471 				     hammer2_to_unix_xid(&ip->meta.uid),
472 				     ap->a_cred);
473 		if (error == 0) {
474 			if (ip->meta.uflags != flags) {
475 				hammer2_inode_modify(ip);
476 				hammer2_spin_lock_update(&ip->cluster_spin);
477 				ip->meta.uflags = flags;
478 				ip->meta.ctime = ctime;
479 				hammer2_spin_unlock_update(&ip->cluster_spin);
480 				kflags |= NOTE_ATTRIB;
481 			}
482 			if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
483 				error = 0;
484 				goto done;
485 			}
486 		}
487 		goto done;
488 	}
489 	if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
490 		error = EPERM;
491 		goto done;
492 	}
493 	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
494 		mode_t cur_mode = ip->meta.mode;
495 		uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
496 		gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
497 		uuid_t uuid_uid;
498 		uuid_t uuid_gid;
499 
500 		error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
501 					 ap->a_cred,
502 					 &cur_uid, &cur_gid, &cur_mode);
503 		if (error == 0) {
504 			hammer2_guid_to_uuid(&uuid_uid, cur_uid);
505 			hammer2_guid_to_uuid(&uuid_gid, cur_gid);
506 			if (bcmp(&uuid_uid, &ip->meta.uid, sizeof(uuid_uid)) ||
507 			    bcmp(&uuid_gid, &ip->meta.gid, sizeof(uuid_gid)) ||
508 			    ip->meta.mode != cur_mode
509 			) {
510 				hammer2_inode_modify(ip);
511 				hammer2_spin_lock_update(&ip->cluster_spin);
512 				ip->meta.uid = uuid_uid;
513 				ip->meta.gid = uuid_gid;
514 				ip->meta.mode = cur_mode;
515 				ip->meta.ctime = ctime;
516 				hammer2_spin_unlock_update(&ip->cluster_spin);
517 			}
518 			kflags |= NOTE_ATTRIB;
519 		}
520 	}
521 
522 	/*
523 	 * Resize the file
524 	 */
525 	if (vap->va_size != VNOVAL && ip->meta.size != vap->va_size) {
526 		switch(vp->v_type) {
527 		case VREG:
528 			if (vap->va_size == ip->meta.size)
529 				break;
530 			if (vap->va_size < ip->meta.size) {
531 				hammer2_mtx_ex(&ip->truncate_lock);
532 				hammer2_truncate_file(ip, vap->va_size);
533 				hammer2_mtx_unlock(&ip->truncate_lock);
534 				kflags |= NOTE_WRITE;
535 			} else {
536 				hammer2_extend_file(ip, vap->va_size);
537 				kflags |= NOTE_WRITE | NOTE_EXTEND;
538 			}
539 			hammer2_inode_modify(ip);
540 			ip->meta.mtime = ctime;
541 			vclrflags(vp, VLASTWRITETS);
542 			break;
543 		default:
544 			error = EINVAL;
545 			goto done;
546 		}
547 	}
548 #if 0
549 	/* atime not supported */
550 	if (vap->va_atime.tv_sec != VNOVAL) {
551 		hammer2_inode_modify(ip);
552 		ip->meta.atime = hammer2_timespec_to_time(&vap->va_atime);
553 		kflags |= NOTE_ATTRIB;
554 	}
555 #endif
556 	if (vap->va_mode != (mode_t)VNOVAL) {
557 		mode_t cur_mode = ip->meta.mode;
558 		uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
559 		gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
560 
561 		error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
562 					 cur_uid, cur_gid, &cur_mode);
563 		if (error == 0) {
564 			hammer2_inode_modify(ip);
565 			hammer2_spin_lock_update(&ip->cluster_spin);
566 			ip->meta.mode = cur_mode;
567 			ip->meta.ctime = ctime;
568 			hammer2_spin_unlock_update(&ip->cluster_spin);
569 			kflags |= NOTE_ATTRIB;
570 		}
571 	}
572 
573 	if (vap->va_mtime.tv_sec != VNOVAL) {
574 		hammer2_inode_modify(ip);
575 		ip->meta.mtime = hammer2_timespec_to_time(&vap->va_mtime);
576 		kflags |= NOTE_ATTRIB;
577 		vclrflags(vp, VLASTWRITETS);
578 	}
579 
580 done:
581 	/*
582 	 * If a truncation occurred we must call chain_sync() now in order
583 	 * to trim the related data chains, otherwise a later expansion can
584 	 * cause havoc.
585 	 *
586 	 * If an extend occured that changed the DIRECTDATA state, we must
587 	 * call inode_chain_sync now in order to prepare the inode's indirect
588 	 * block table.
589 	 *
590 	 * WARNING! This means we are making an adjustment to the inode's
591 	 * chain outside of sync/fsync, and not just to inode->meta, which
592 	 * may result in some consistency issues if a crash were to occur
593 	 * at just the wrong time.
594 	 */
595 	if (ip->flags & HAMMER2_INODE_RESIZED)
596 		hammer2_inode_chain_sync(ip);
597 
598 	/*
599 	 * Cleanup.
600 	 */
601 	hammer2_inode_unlock(ip);
602 	hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ);
603 	hammer2_knote(ip->vp, kflags);
604 
605 	return (error);
606 #endif
607 	return (EOPNOTSUPP);
608 }
609 
610 static
611 int
612 hammer2_vop_readdir(struct vop_readdir_args *ap)
613 {
614 #if 0
615 	hammer2_xop_readdir_t *xop;
616 	hammer2_blockref_t bref;
617 	hammer2_inode_t *ip;
618 	hammer2_tid_t inum;
619 	hammer2_key_t lkey;
620 	struct uio *uio;
621 	off_t *cookies;
622 	off_t saveoff;
623 	int cookie_index;
624 	int ncookies;
625 	int error;
626 	int eofflag;
627 	int r;
628 
629 	ip = VTOI(ap->a_vp);
630 	uio = ap->a_uio;
631 	saveoff = uio->uio_offset;
632 	eofflag = 0;
633 	error = 0;
634 
635 	/*
636 	 * Setup cookies directory entry cookies if requested
637 	 */
638 	if (ap->a_ncookies) {
639 		ncookies = uio->uio_resid / 16 + 1;
640 		if (ncookies > 1024)
641 			ncookies = 1024;
642 		cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
643 	} else {
644 		ncookies = -1;
645 		cookies = NULL;
646 	}
647 	cookie_index = 0;
648 
649 	hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
650 
651 	/*
652 	 * Handle artificial entries.  To ensure that only positive 64 bit
653 	 * quantities are returned to userland we always strip off bit 63.
654 	 * The hash code is designed such that codes 0x0000-0x7FFF are not
655 	 * used, allowing us to use these codes for articial entries.
656 	 *
657 	 * Entry 0 is used for '.' and entry 1 is used for '..'.  Do not
658 	 * allow '..' to cross the mount point into (e.g.) the super-root.
659 	 */
660 	if (saveoff == 0) {
661 		inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
662 		r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
663 		if (r)
664 			goto done;
665 		if (cookies)
666 			cookies[cookie_index] = saveoff;
667 		++saveoff;
668 		++cookie_index;
669 		if (cookie_index == ncookies)
670 			goto done;
671 	}
672 
673 	if (saveoff == 1) {
674 		inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
675 		if (ip != ip->pmp->iroot)
676 			inum = ip->meta.iparent & HAMMER2_DIRHASH_USERMSK;
677 		r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
678 		if (r)
679 			goto done;
680 		if (cookies)
681 			cookies[cookie_index] = saveoff;
682 		++saveoff;
683 		++cookie_index;
684 		if (cookie_index == ncookies)
685 			goto done;
686 	}
687 
688 	lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
689 	if (hammer2_debug & 0x0020)
690 		kprintf("readdir: lkey %016jx\n", lkey);
691 	if (error)
692 		goto done;
693 
694 	xop = hammer2_xop_alloc(ip, 0);
695 	xop->lkey = lkey;
696 	hammer2_xop_start(&xop->head, &hammer2_readdir_desc);
697 
698 	for (;;) {
699 		const hammer2_inode_data_t *ripdata;
700 		const char *dname;
701 		int dtype;
702 
703 		error = hammer2_xop_collect(&xop->head, 0);
704 		error = hammer2_error_to_errno(error);
705 		if (error) {
706 			break;
707 		}
708 		if (cookie_index == ncookies)
709 			break;
710 		if (hammer2_debug & 0x0020)
711 			kprintf("cluster chain %p %p\n",
712 				xop->head.cluster.focus,
713 				(xop->head.cluster.focus ?
714 				 xop->head.cluster.focus->data : (void *)-1));
715 		hammer2_cluster_bref(&xop->head.cluster, &bref);
716 
717 		if (bref.type == HAMMER2_BREF_TYPE_INODE) {
718 			ripdata = &hammer2_xop_gdata(&xop->head)->ipdata;
719 			dtype = hammer2_get_dtype(ripdata->meta.type);
720 			saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
721 			r = vop_write_dirent(&error, uio,
722 					     ripdata->meta.inum &
723 					      HAMMER2_DIRHASH_USERMSK,
724 					     dtype,
725 					     ripdata->meta.name_len,
726 					     ripdata->filename);
727 			hammer2_xop_pdata(&xop->head);
728 			if (r)
729 				break;
730 			if (cookies)
731 				cookies[cookie_index] = saveoff;
732 			++cookie_index;
733 		} else if (bref.type == HAMMER2_BREF_TYPE_DIRENT) {
734 			uint16_t namlen;
735 
736 			dtype = hammer2_get_dtype(bref.embed.dirent.type);
737 			saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
738 			namlen = bref.embed.dirent.namlen;
739 			if (namlen <= sizeof(bref.check.buf)) {
740 				dname = bref.check.buf;
741 			} else {
742 				dname = hammer2_xop_gdata(&xop->head)->buf;
743 			}
744 			r = vop_write_dirent(&error, uio,
745 					     bref.embed.dirent.inum, dtype,
746 					     namlen, dname);
747 			if (namlen > sizeof(bref.check.buf))
748 				hammer2_xop_pdata(&xop->head);
749 			if (r)
750 				break;
751 			if (cookies)
752 				cookies[cookie_index] = saveoff;
753 			++cookie_index;
754 		} else {
755 			/* XXX chain error */
756 			kprintf("bad chain type readdir %d\n", bref.type);
757 		}
758 	}
759 	hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
760 	if (error == ENOENT) {
761 		error = 0;
762 		eofflag = 1;
763 		saveoff = (hammer2_key_t)-1;
764 	} else {
765 		saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
766 	}
767 done:
768 	hammer2_inode_unlock(ip);
769 	if (ap->a_eofflag)
770 		*ap->a_eofflag = eofflag;
771 	if (hammer2_debug & 0x0020)
772 		kprintf("readdir: done at %016jx\n", saveoff);
773 	uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
774 	if (error && cookie_index == 0) {
775 		if (cookies) {
776 			kfree(cookies, M_TEMP);
777 			*ap->a_ncookies = 0;
778 			*ap->a_cookies = NULL;
779 		}
780 	} else {
781 		if (cookies) {
782 			*ap->a_ncookies = cookie_index;
783 			*ap->a_cookies = cookies;
784 		}
785 	}
786 	return (error);
787 #endif
788 	return (EOPNOTSUPP);
789 }
790 
791 /*
792  * hammer2_vop_readlink { vp, uio, cred }
793  */
794 static
795 int
796 hammer2_vop_readlink(struct vop_readlink_args *ap)
797 {
798 	struct m_vnode *vp;
799 	hammer2_inode_t *ip;
800 	int error;
801 
802 	vp = ap->a_vp;
803 	if (vp->v_type != VLNK)
804 		return (EINVAL);
805 	ip = VTOI(vp);
806 
807 	error = hammer2_read_file(ip, ap->a_uio, 0);
808 	return (error);
809 }
810 
811 int
812 hammer2_readlink(struct m_vnode *vp, void *buf, size_t size)
813 {
814 	assert(buf);
815 	assert(size > 0);
816 	assert(size <= HAMMER2_PBUFSIZE);
817 
818 	struct iovec iov = {
819 		.iov_base = buf,
820 		.iov_len = size,
821 	};
822 	struct uio uio = {
823 		.uio_iov = &iov,
824 		.uio_iovcnt = 1,
825 		.uio_offset = 0,
826 		.uio_resid = size,
827 		.uio_segflg = UIO_USERSPACE,
828 		.uio_rw = UIO_READ,
829 		.uio_td = NULL,
830 	};
831 	struct vop_readlink_args ap = {
832 		.a_vp = vp,
833 		.a_uio = &uio,
834 		.a_cred = NULL,
835 	};
836 
837 	return hammer2_vop_readlink(&ap);
838 }
839 
840 static
841 int
842 hammer2_vop_read(struct vop_read_args *ap)
843 {
844 #if 0
845 	struct m_vnode *vp;
846 	hammer2_inode_t *ip;
847 	struct uio *uio;
848 	int error;
849 	int seqcount;
850 
851 	/*
852 	 * Read operations supported on this vnode?
853 	 */
854 	vp = ap->a_vp;
855 	if (vp->v_type == VDIR)
856 		return (EISDIR);
857 	if (vp->v_type != VREG)
858 		return (EINVAL);
859 
860 	/*
861 	 * Misc
862 	 */
863 	ip = VTOI(vp);
864 	uio = ap->a_uio;
865 	error = 0;
866 
867 	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
868 
869 	error = hammer2_read_file(ip, uio, seqcount);
870 	return (error);
871 #endif
872 	return (EOPNOTSUPP);
873 }
874 
875 static
876 int
877 hammer2_vop_write(struct vop_write_args *ap)
878 {
879 	hammer2_inode_t *ip;
880 	//thread_t td;
881 	struct m_vnode *vp;
882 	struct uio *uio;
883 	int error;
884 	int seqcount;
885 	int ioflag;
886 
887 	/*
888 	 * Read operations supported on this vnode?
889 	 */
890 	vp = ap->a_vp;
891 	if (vp->v_type != VREG)
892 		return (EINVAL);
893 
894 	/*
895 	 * Misc
896 	 */
897 	ip = VTOI(vp);
898 	ioflag = ap->a_ioflag;
899 	uio = ap->a_uio;
900 	error = 0;
901 	if (ip->pmp->ronly || (ip->pmp->flags & HAMMER2_PMPF_EMERG))
902 		return (EROFS);
903 	switch (hammer2_vfs_enospace(ip, uio->uio_resid, ap->a_cred)) {
904 	case 2:
905 		return (ENOSPC);
906 	case 1:
907 		ioflag |= IO_DIRECT;	/* semi-synchronous */
908 		/* fall through */
909 	default:
910 		break;
911 	}
912 
913 	seqcount = ioflag >> IO_SEQSHIFT;
914 
915 	/*
916 	 * Check resource limit
917 	 */
918 	/*
919 	if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
920 	    uio->uio_offset + uio->uio_resid >
921 	     td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
922 		lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
923 		return (EFBIG);
924 	}
925 	*/
926 
927 	/*
928 	 * The transaction interlocks against flush initiations
929 	 * (note: but will run concurrently with the actual flush).
930 	 *
931 	 * To avoid deadlocking against the VM system, we must flag any
932 	 * transaction related to the buffer cache or other direct
933 	 * VM page manipulation.
934 	 */
935 	if (uio->uio_segflg == UIO_NOCOPY) {
936 		assert(0); /* no UIO_NOCOPY in makefs */
937 		hammer2_trans_init(ip->pmp, HAMMER2_TRANS_BUFCACHE);
938 	} else {
939 		hammer2_trans_init(ip->pmp, 0);
940 	}
941 	error = hammer2_write_file(ip, uio, ioflag, seqcount);
942 	if (uio->uio_segflg == UIO_NOCOPY) {
943 		assert(0); /* no UIO_NOCOPY in makefs */
944 		hammer2_trans_done(ip->pmp, HAMMER2_TRANS_BUFCACHE |
945 					    HAMMER2_TRANS_SIDEQ);
946 	} else
947 		hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ);
948 
949 	return (error);
950 }
951 
952 int
953 hammer2_write(struct m_vnode *vp, void *buf, size_t size, off_t offset)
954 {
955 	assert(buf);
956 	assert(size > 0);
957 	assert(size <= HAMMER2_PBUFSIZE);
958 
959 	struct iovec iov = {
960 		.iov_base = buf,
961 		.iov_len = size,
962 	};
963 	struct uio uio = {
964 		.uio_iov = &iov,
965 		.uio_iovcnt = 1,
966 		.uio_offset = offset,
967 		.uio_resid = size,
968 		.uio_segflg = UIO_USERSPACE,
969 		.uio_rw = UIO_WRITE,
970 		.uio_td = NULL,
971 	};
972 	struct vop_write_args ap = {
973 		.a_vp = vp,
974 		.a_uio = &uio,
975 		.a_ioflag = 0,
976 		.a_cred = NULL,
977 	};
978 
979 	return hammer2_vop_write(&ap);
980 }
981 
982 /*
983  * Perform read operations on a file or symlink given an UNLOCKED
984  * inode and uio.
985  *
986  * The passed ip is not locked.
987  */
988 static
989 int
990 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
991 {
992 	hammer2_off_t size;
993 	struct m_buf *bp;
994 	int error;
995 
996 	error = 0;
997 
998 	/*
999 	 * UIO read loop.
1000 	 *
1001 	 * WARNING! Assumes that the kernel interlocks size changes at the
1002 	 *	    vnode level.
1003 	 */
1004 	hammer2_mtx_sh(&ip->lock);
1005 	hammer2_mtx_sh(&ip->truncate_lock);
1006 	size = ip->meta.size;
1007 	hammer2_mtx_unlock(&ip->lock);
1008 
1009 	while (uio->uio_resid > 0 && uio->uio_offset < size) {
1010 		hammer2_key_t lbase;
1011 		hammer2_key_t leof;
1012 		int lblksize;
1013 		int loff;
1014 		int n;
1015 
1016 		lblksize = hammer2_calc_logical(ip, uio->uio_offset,
1017 						&lbase, &leof);
1018 #if 0
1019 #if 1
1020 		bp = NULL;
1021 		error = cluster_readx(ip->vp, leof, lbase, lblksize,
1022 				      B_NOTMETA | B_KVABIO,
1023 				      uio->uio_resid,
1024 				      seqcount * MAXBSIZE,
1025 				      &bp);
1026 #else
1027 		if (uio->uio_segflg == UIO_NOCOPY) {
1028 			bp = getblk(ip->vp, lbase, lblksize,
1029 				    GETBLK_BHEAVY | GETBLK_KVABIO, 0);
1030 			if (bp->b_flags & B_CACHE) {
1031 				int i;
1032 				int j = 0;
1033 				if (bp->b_xio.xio_npages != 16)
1034 					kprintf("NPAGES BAD\n");
1035 				for (i = 0; i < bp->b_xio.xio_npages; ++i) {
1036 					vm_page_t m;
1037 					m = bp->b_xio.xio_pages[i];
1038 					if (m == NULL || m->valid == 0) {
1039 						kprintf("bp %016jx %016jx pg %d inv",
1040 							lbase, leof, i);
1041 						if (m)
1042 							kprintf("m->object %p/%p", m->object, ip->vp->v_object);
1043 						kprintf("\n");
1044 						j = 1;
1045 					}
1046 				}
1047 				if (j)
1048 					kprintf("b_flags %08x, b_error %d\n", bp->b_flags, bp->b_error);
1049 			}
1050 			bqrelse(bp);
1051 		}
1052 		error = bread_kvabio(ip->vp, lbase, lblksize, &bp);
1053 #endif
1054 #else
1055 		bp = getblkx(ip->vp, lbase, lblksize,
1056 			    GETBLK_BHEAVY | GETBLK_KVABIO, 0);
1057 		bp->b_cmd = BUF_CMD_READ;
1058 
1059 		struct bio bio;
1060 		bio.bio_buf = bp;
1061 		bio.bio_offset = lbase;
1062 
1063 		struct vop_strategy_args ap;
1064 		ap.a_vp = ip->vp;
1065 		ap.a_bio = &bio;
1066 
1067 		error = hammer2_vop_strategy(&ap);
1068 		assert(!error);
1069 #endif
1070 		if (error) {
1071 			brelse(bp);
1072 			break;
1073 		}
1074 		bkvasync(bp);
1075 		loff = (int)(uio->uio_offset - lbase);
1076 		n = lblksize - loff;
1077 		if (n > uio->uio_resid)
1078 			n = uio->uio_resid;
1079 		if (n > size - uio->uio_offset)
1080 			n = (int)(size - uio->uio_offset);
1081 		//bp->b_flags |= B_AGE;
1082 		uiomovebp(bp, bp->b_data + loff, n, uio);
1083 		bqrelse(bp);
1084 	}
1085 	hammer2_mtx_unlock(&ip->truncate_lock);
1086 
1087 	return (error);
1088 }
1089 
1090 /*
1091  * Write to the file represented by the inode via the logical buffer cache.
1092  * The inode may represent a regular file or a symlink.
1093  *
1094  * The inode must not be locked.
1095  */
1096 static
1097 int
1098 hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
1099 		   int ioflag, int seqcount)
1100 {
1101 	hammer2_key_t old_eof;
1102 	hammer2_key_t new_eof;
1103 	struct m_buf *bp;
1104 	int kflags;
1105 	int error;
1106 	int modified;
1107 
1108 	/*
1109 	 * Setup if append
1110 	 *
1111 	 * WARNING! Assumes that the kernel interlocks size changes at the
1112 	 *	    vnode level.
1113 	 */
1114 	hammer2_mtx_ex(&ip->lock);
1115 	hammer2_mtx_sh(&ip->truncate_lock);
1116 	if (ioflag & IO_APPEND)
1117 		uio->uio_offset = ip->meta.size;
1118 	old_eof = ip->meta.size;
1119 
1120 	/*
1121 	 * Extend the file if necessary.  If the write fails at some point
1122 	 * we will truncate it back down to cover as much as we were able
1123 	 * to write.
1124 	 *
1125 	 * Doing this now makes it easier to calculate buffer sizes in
1126 	 * the loop.
1127 	 */
1128 	kflags = 0;
1129 	error = 0;
1130 	modified = 0;
1131 
1132 	if (uio->uio_offset + uio->uio_resid > old_eof) {
1133 		new_eof = uio->uio_offset + uio->uio_resid;
1134 		modified = 1;
1135 		hammer2_extend_file(ip, new_eof);
1136 		kflags |= NOTE_EXTEND;
1137 	} else {
1138 		new_eof = old_eof;
1139 	}
1140 	hammer2_mtx_unlock(&ip->lock);
1141 
1142 	/*
1143 	 * UIO write loop
1144 	 */
1145 	while (uio->uio_resid > 0) {
1146 		hammer2_key_t lbase;
1147 		int trivial;
1148 		int endofblk;
1149 		int lblksize;
1150 		int loff;
1151 		int n;
1152 
1153 		/*
1154 		 * Don't allow the buffer build to blow out the buffer
1155 		 * cache.
1156 		 */
1157 		if ((ioflag & IO_RECURSE) == 0)
1158 			bwillwrite(HAMMER2_PBUFSIZE);
1159 
1160 		/*
1161 		 * This nominally tells us how much we can cluster and
1162 		 * what the logical buffer size needs to be.  Currently
1163 		 * we don't try to cluster the write and just handle one
1164 		 * block at a time.
1165 		 */
1166 		lblksize = hammer2_calc_logical(ip, uio->uio_offset,
1167 						&lbase, NULL);
1168 		loff = (int)(uio->uio_offset - lbase);
1169 
1170 		KKASSERT(lblksize <= MAXBSIZE);
1171 
1172 		/*
1173 		 * Calculate bytes to copy this transfer and whether the
1174 		 * copy completely covers the buffer or not.
1175 		 */
1176 		trivial = 0;
1177 		n = lblksize - loff;
1178 		if (n > uio->uio_resid) {
1179 			n = uio->uio_resid;
1180 			if (loff == lbase && uio->uio_offset + n == new_eof)
1181 				trivial = 1;
1182 			endofblk = 0;
1183 		} else {
1184 			if (loff == 0)
1185 				trivial = 1;
1186 			endofblk = 1;
1187 		}
1188 		if (lbase >= new_eof)
1189 			trivial = 1;
1190 		trivial = 1; /* force trivial for makefs */
1191 
1192 		/*
1193 		 * Get the buffer
1194 		 */
1195 		if (uio->uio_segflg == UIO_NOCOPY) {
1196 			assert(0); /* no UIO_NOCOPY in makefs */
1197 			/*
1198 			 * Issuing a write with the same data backing the
1199 			 * buffer.  Instantiate the buffer to collect the
1200 			 * backing vm pages, then read-in any missing bits.
1201 			 *
1202 			 * This case is used by vop_stdputpages().
1203 			 */
1204 			bp = getblkx(ip->vp, lbase, lblksize,
1205 				    GETBLK_BHEAVY | GETBLK_KVABIO, 0);
1206 			/*
1207 			if ((bp->b_flags & B_CACHE) == 0) {
1208 				bqrelse(bp);
1209 				error = bread_kvabio(ip->vp, lbase,
1210 						     lblksize, &bp);
1211 			}
1212 			*/
1213 		} else if (trivial) {
1214 			/*
1215 			 * Even though we are entirely overwriting the buffer
1216 			 * we may still have to zero it out to avoid a
1217 			 * mmap/write visibility issue.
1218 			 */
1219 			bp = getblkx(ip->vp, lbase, lblksize,
1220 				    GETBLK_BHEAVY | GETBLK_KVABIO, 0);
1221 			/*
1222 			if ((bp->b_flags & B_CACHE) == 0)
1223 				vfs_bio_clrbuf(bp);
1224 			*/
1225 		} else {
1226 			assert(0); /* no partial write in makefs */
1227 			/*
1228 			 * Partial overwrite, read in any missing bits then
1229 			 * replace the portion being written.
1230 			 *
1231 			 * (The strategy code will detect zero-fill physical
1232 			 * blocks for this case).
1233 			 */
1234 			error = bread_kvabio(ip->vp, lbase, lblksize, &bp);
1235 			if (error == 0)
1236 				bheavy(bp);
1237 		}
1238 
1239 		if (error) {
1240 			brelse(bp);
1241 			break;
1242 		}
1243 
1244 		/*
1245 		 * Ok, copy the data in
1246 		 */
1247 		bkvasync(bp);
1248 		error = uiomovebp(bp, bp->b_data + loff, n, uio);
1249 		kflags |= NOTE_WRITE;
1250 		modified = 1;
1251 		if (error) {
1252 			brelse(bp);
1253 			break;
1254 		}
1255 
1256 		/*
1257 		 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
1258 		 *	    with IO_SYNC or IO_ASYNC set.  These writes
1259 		 *	    must be handled as the pageout daemon expects.
1260 		 *
1261 		 * NOTE!    H2 relies on cluster_write() here because it
1262 		 *	    cannot preallocate disk blocks at the logical
1263 		 *	    level due to not knowing what the compression
1264 		 *	    size will be at this time.
1265 		 *
1266 		 *	    We must use cluster_write() here and we depend
1267 		 *	    on the write-behind feature to flush buffers
1268 		 *	    appropriately.  If we let the buffer daemons do
1269 		 *	    it the block allocations will be all over the
1270 		 *	    map.
1271 		 */
1272 		if (1) {
1273 			bp->b_cmd = BUF_CMD_WRITE;
1274 
1275 			struct bio bio;
1276 			bio.bio_buf = bp;
1277 			bio.bio_offset = lbase;
1278 
1279 			struct vop_strategy_args ap;
1280 			ap.a_vp = ip->vp;
1281 			ap.a_bio = &bio;
1282 
1283 			error = hammer2_vop_strategy(&ap);
1284 			assert(!error);
1285 
1286 			brelse(bp);
1287 		} else if (ioflag & IO_SYNC) {
1288 			assert(0);
1289 			bwrite(bp);
1290 		} else if ((ioflag & IO_DIRECT) && endofblk) {
1291 			assert(0);
1292 			bawrite(bp);
1293 		} else if (ioflag & IO_ASYNC) {
1294 			assert(0);
1295 			bawrite(bp);
1296 		} else if (0 /*ip->vp->v_mount->mnt_flag & MNT_NOCLUSTERW*/) {
1297 			assert(0);
1298 			bdwrite(bp);
1299 		} else {
1300 			assert(0);
1301 #if 0
1302 #if 1
1303 			bp->b_flags |= B_CLUSTEROK;
1304 			cluster_write(bp, new_eof, lblksize, seqcount);
1305 #else
1306 			bp->b_flags |= B_CLUSTEROK;
1307 			bdwrite(bp);
1308 #endif
1309 #endif
1310 		}
1311 	}
1312 
1313 	/*
1314 	 * Cleanup.  If we extended the file EOF but failed to write through
1315 	 * the entire write is a failure and we have to back-up.
1316 	 */
1317 	if (error && new_eof != old_eof) {
1318 		hammer2_mtx_unlock(&ip->truncate_lock);
1319 		hammer2_mtx_ex(&ip->lock);		/* note lock order */
1320 		hammer2_mtx_ex(&ip->truncate_lock);	/* note lock order */
1321 		hammer2_truncate_file(ip, old_eof);
1322 		if (ip->flags & HAMMER2_INODE_MODIFIED)
1323 			hammer2_inode_chain_sync(ip);
1324 		hammer2_mtx_unlock(&ip->lock);
1325 	} else if (modified) {
1326 		struct m_vnode *vp = ip->vp;
1327 
1328 		hammer2_mtx_ex(&ip->lock);
1329 		hammer2_inode_modify(ip);
1330 		if (uio->uio_segflg == UIO_NOCOPY) {
1331 			assert(0); /* no UIO_NOCOPY in makefs */
1332 			/*
1333 			if (vp->v_flag & VLASTWRITETS) {
1334 				ip->meta.mtime =
1335 				    (unsigned long)vp->v_lastwrite_ts.tv_sec *
1336 				    1000000 +
1337 				    vp->v_lastwrite_ts.tv_nsec / 1000;
1338 			}
1339 			*/
1340 		} else {
1341 			hammer2_update_time(&ip->meta.mtime, true);
1342 			vclrflags(vp, VLASTWRITETS);
1343 		}
1344 
1345 #if 0
1346 		/*
1347 		 * REMOVED - handled by hammer2_extend_file().  Do not issue
1348 		 * a chain_sync() outside of a sync/fsync except for DIRECTDATA
1349 		 * state changes.
1350 		 *
1351 		 * Under normal conditions we only issue a chain_sync if
1352 		 * the inode's DIRECTDATA state changed.
1353 		 */
1354 		if (ip->flags & HAMMER2_INODE_RESIZED)
1355 			hammer2_inode_chain_sync(ip);
1356 #endif
1357 		hammer2_mtx_unlock(&ip->lock);
1358 		hammer2_knote(ip->vp, kflags);
1359 	}
1360 	hammer2_trans_assert_strategy(ip->pmp);
1361 	hammer2_mtx_unlock(&ip->truncate_lock);
1362 
1363 	return error;
1364 }
1365 
1366 /*
1367  * Truncate the size of a file.  The inode must be locked.
1368  *
1369  * We must unconditionally set HAMMER2_INODE_RESIZED to properly
1370  * ensure that any on-media data beyond the new file EOF has been destroyed.
1371  *
1372  * WARNING: nvtruncbuf() can only be safely called without the inode lock
1373  *	    held due to the way our write thread works.  If the truncation
1374  *	    occurs in the middle of a buffer, nvtruncbuf() is responsible
1375  *	    for dirtying that buffer and zeroing out trailing bytes.
1376  *
1377  * WARNING! Assumes that the kernel interlocks size changes at the
1378  *	    vnode level.
1379  *
1380  * WARNING! Caller assumes responsibility for removing dead blocks
1381  *	    if INODE_RESIZED is set.
1382  */
1383 static
1384 void
1385 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1386 {
1387 	hammer2_key_t lbase;
1388 	int nblksize;
1389 
1390 	hammer2_mtx_unlock(&ip->lock);
1391 	if (ip->vp) {
1392 		nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1393 		nvtruncbuf(ip->vp, nsize,
1394 			   nblksize, (int)nsize & (nblksize - 1),
1395 			   0);
1396 	}
1397 	hammer2_mtx_ex(&ip->lock);
1398 	KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1399 	ip->osize = ip->meta.size;
1400 	ip->meta.size = nsize;
1401 	atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1402 	hammer2_inode_modify(ip);
1403 }
1404 
1405 /*
1406  * Extend the size of a file.  The inode must be locked.
1407  *
1408  * Even though the file size is changing, we do not have to set the
1409  * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES
1410  * boundary.  When this occurs a hammer2_inode_chain_sync() is required
1411  * to prepare the inode cluster's indirect block table, otherwise
1412  * async execution of the strategy code will implode on us.
1413  *
1414  * WARNING! Assumes that the kernel interlocks size changes at the
1415  *	    vnode level.
1416  *
1417  * WARNING! Caller assumes responsibility for transitioning out
1418  *	    of the inode DIRECTDATA mode if INODE_RESIZED is set.
1419  */
1420 static
1421 void
1422 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1423 {
1424 	hammer2_key_t lbase;
1425 	hammer2_key_t osize;
1426 	int oblksize;
1427 	int nblksize;
1428 	int error;
1429 
1430 	KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1431 	hammer2_inode_modify(ip);
1432 	osize = ip->meta.size;
1433 	ip->osize = osize;
1434 	ip->meta.size = nsize;
1435 
1436 	/*
1437 	 * We must issue a chain_sync() when the DIRECTDATA state changes
1438 	 * to prevent confusion between the flush code and the in-memory
1439 	 * state.  This is not perfect because we are doing it outside of
1440 	 * a sync/fsync operation, so it might not be fully synchronized
1441 	 * with the meta-data topology flush.
1442 	 *
1443 	 * We must retain and re-dirty the buffer cache buffer containing
1444 	 * the direct data so it can be written to a real block.  It should
1445 	 * not be possible for a bread error to occur since the original data
1446 	 * is extracted from the inode structure directly.
1447 	 */
1448 	if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES) {
1449 		if (osize) {
1450 			assert(0); /* no such transition in makefs */
1451 			struct m_buf *bp;
1452 
1453 			oblksize = hammer2_calc_logical(ip, 0, NULL, NULL);
1454 			error = bread_kvabio(ip->vp, 0, oblksize, &bp);
1455 			atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1456 			hammer2_inode_chain_sync(ip);
1457 			if (error == 0) {
1458 				bheavy(bp);
1459 				bdwrite(bp);
1460 			} else {
1461 				brelse(bp);
1462 			}
1463 		} else {
1464 			atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1465 			hammer2_inode_chain_sync(ip);
1466 		}
1467 	}
1468 	hammer2_mtx_unlock(&ip->lock);
1469 	if (ip->vp) {
1470 		oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1471 		nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1472 		nvextendbuf(ip->vp,
1473 			    osize, nsize,
1474 			    oblksize, nblksize,
1475 			    -1, -1, 0);
1476 	}
1477 	hammer2_mtx_ex(&ip->lock);
1478 }
1479 
1480 static
1481 int
1482 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1483 {
1484 	hammer2_xop_nresolve_t *xop;
1485 	hammer2_inode_t *ip;
1486 	hammer2_inode_t *dip;
1487 	struct namecache *ncp;
1488 	struct m_vnode *vp;
1489 	int error;
1490 
1491 	dip = VTOI(ap->a_dvp);
1492 	xop = hammer2_xop_alloc(dip, 0);
1493 
1494 	ncp = ap->a_nch->ncp;
1495 	hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1496 
1497 	/*
1498 	 * Note: In DragonFly the kernel handles '.' and '..'.
1499 	 */
1500 	hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1501 	hammer2_xop_start(&xop->head, &hammer2_nresolve_desc);
1502 
1503 	error = hammer2_xop_collect(&xop->head, 0);
1504 	error = hammer2_error_to_errno(error);
1505 	if (error) {
1506 		ip = NULL;
1507 	} else {
1508 		ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1);
1509 	}
1510 	hammer2_inode_unlock(dip);
1511 
1512 	/*
1513 	 * Acquire the related vnode
1514 	 *
1515 	 * NOTE: For error processing, only ENOENT resolves the namecache
1516 	 *	 entry to NULL, otherwise we just return the error and
1517 	 *	 leave the namecache unresolved.
1518 	 *
1519 	 * WARNING: inode structure is locked exclusively via inode_get
1520 	 *	    but chain was locked shared.  inode_unlock()
1521 	 *	    will handle it properly.
1522 	 */
1523 	if (ip) {
1524 		vp = hammer2_igetv(ip, &error);	/* error set to UNIX error */
1525 		if (error == 0) {
1526 			vn_unlock(vp);
1527 			cache_setvp(ap->a_nch, vp);
1528 			*ap->a_vpp = vp;
1529 		} else if (error == ENOENT) {
1530 			cache_setvp(ap->a_nch, NULL);
1531 		}
1532 		hammer2_inode_unlock(ip);
1533 
1534 		/*
1535 		 * The vp should not be released until after we've disposed
1536 		 * of our locks, because it might cause vop_inactive() to
1537 		 * be called.
1538 		 */
1539 		if (vp)
1540 			vrele(vp);
1541 	} else {
1542 		error = ENOENT;
1543 		cache_setvp(ap->a_nch, NULL);
1544 	}
1545 	hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1546 	/*
1547 	KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1548 		("resolve error %d/%p ap %p\n",
1549 		 error, ap->a_nch->ncp->nc_vp, ap));
1550 	*/
1551 
1552 	return error;
1553 }
1554 
1555 int
1556 hammer2_nresolve(struct m_vnode *dvp, struct m_vnode **vpp, char *name, int nlen)
1557 {
1558 	*vpp = NULL;
1559 	struct namecache nc = {
1560 		.nc_name = name,
1561 		.nc_nlen = nlen,
1562 	};
1563 	struct nchandle nch = {
1564 		.ncp = &nc,
1565 	};
1566 	struct vop_nresolve_args ap = {
1567 		.a_nch = &nch,
1568 		.a_dvp = dvp,
1569 		.a_vpp = vpp,
1570 	};
1571 
1572 	return hammer2_vop_nresolve(&ap);
1573 }
1574 
1575 static
1576 int
1577 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1578 {
1579 #if 0
1580 	hammer2_inode_t *dip;
1581 	hammer2_tid_t inum;
1582 	int error;
1583 
1584 	dip = VTOI(ap->a_dvp);
1585 	inum = dip->meta.iparent;
1586 	*ap->a_vpp = NULL;
1587 
1588 	if (inum) {
1589 		error = hammer2_vfs_vget(ap->a_dvp->v_mount, NULL,
1590 					 inum, ap->a_vpp);
1591 	} else {
1592 		error = ENOENT;
1593 	}
1594 	return error;
1595 #endif
1596 	return (EOPNOTSUPP);
1597 }
1598 
1599 static
1600 int
1601 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1602 {
1603 	hammer2_inode_t *dip;
1604 	hammer2_inode_t *nip;
1605 	struct namecache *ncp;
1606 	const char *name;
1607 	size_t name_len;
1608 	hammer2_tid_t inum;
1609 	int error;
1610 
1611 	dip = VTOI(ap->a_dvp);
1612 	if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG))
1613 		return (EROFS);
1614 	if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1615 		return (ENOSPC);
1616 
1617 	ncp = ap->a_nch->ncp;
1618 	name = ncp->nc_name;
1619 	name_len = ncp->nc_nlen;
1620 
1621 	hammer2_trans_init(dip->pmp, 0);
1622 
1623 	inum = hammer2_trans_newinum(dip->pmp);
1624 
1625 	/*
1626 	 * Create the actual inode as a hidden file in the iroot, then
1627 	 * create the directory entry.  The creation of the actual inode
1628 	 * sets its nlinks to 1 which is the value we desire.
1629 	 *
1630 	 * dip must be locked before nip to avoid deadlock.
1631 	 */
1632 	hammer2_inode_lock(dip, 0);
1633 	nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
1634 					  inum, &error);
1635 	if (error) {
1636 		error = hammer2_error_to_errno(error);
1637 	} else {
1638 		error = hammer2_dirent_create(dip, name, name_len,
1639 					      nip->meta.inum, nip->meta.type);
1640 		/* returns UNIX error code */
1641 	}
1642 	if (error) {
1643 		if (nip) {
1644 			hammer2_inode_unlink_finisher(nip, NULL);
1645 			hammer2_inode_unlock(nip);
1646 			nip = NULL;
1647 		}
1648 		*ap->a_vpp = NULL;
1649 	} else {
1650 		/*
1651 		 * inode_depend() must occur before the igetv() because
1652 		 * the igetv() can temporarily release the inode lock.
1653 		 */
1654 		hammer2_inode_depend(dip, nip);	/* before igetv */
1655 		*ap->a_vpp = hammer2_igetv(nip, &error);
1656 		hammer2_inode_unlock(nip);
1657 	}
1658 
1659 	/*
1660 	 * Update dip's mtime
1661 	 *
1662 	 * We can use a shared inode lock and allow the meta.mtime update
1663 	 * SMP race.  hammer2_inode_modify() is MPSAFE w/a shared lock.
1664 	 */
1665 	if (error == 0) {
1666 		uint64_t mtime;
1667 
1668 		/*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1669 		hammer2_update_time(&mtime, true);
1670 		hammer2_inode_modify(dip);
1671 		dip->meta.mtime = mtime;
1672 		/*hammer2_inode_unlock(dip);*/
1673 	}
1674 	hammer2_inode_unlock(dip);
1675 
1676 	hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1677 
1678 	if (error == 0) {
1679 		cache_setunresolved(ap->a_nch);
1680 		cache_setvp(ap->a_nch, *ap->a_vpp);
1681 		hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1682 	}
1683 	return error;
1684 }
1685 
1686 int
1687 hammer2_nmkdir(struct m_vnode *dvp, struct m_vnode **vpp, char *name, int nlen,
1688 		mode_t mode)
1689 {
1690 	struct namecache nc = {
1691 		.nc_name = name,
1692 		.nc_nlen = nlen,
1693 	};
1694 	struct nchandle nch = {
1695 		.ncp = &nc,
1696 	};
1697 	uid_t va_uid = VNOVAL; //getuid();
1698 	uid_t va_gid = VNOVAL; //getgid();
1699 	struct vattr va = {
1700 		.va_type = VDIR,
1701 		.va_mode = mode & ~S_IFMT,
1702 		.va_uid = va_uid,
1703 		.va_gid = va_gid,
1704 	};
1705 	struct vop_nmkdir_args ap = {
1706 		.a_nch = &nch,
1707 		.a_dvp = dvp,
1708 		.a_vpp = vpp,
1709 		.a_vap = &va,
1710 	};
1711 
1712 	return hammer2_vop_nmkdir(&ap);
1713 }
1714 
1715 static
1716 int
1717 hammer2_vop_open(struct vop_open_args *ap)
1718 {
1719 #if 0
1720 	return vop_stdopen(ap);
1721 #endif
1722 	return (EOPNOTSUPP);
1723 }
1724 
1725 /*
1726  * hammer2_vop_advlock { vp, id, op, fl, flags }
1727  */
1728 static
1729 int
1730 hammer2_vop_advlock(struct vop_advlock_args *ap)
1731 {
1732 #if 0
1733 	hammer2_inode_t *ip = VTOI(ap->a_vp);
1734 	hammer2_off_t size;
1735 
1736 	size = ip->meta.size;
1737 	return (lf_advlock(ap, &ip->advlock, size));
1738 #endif
1739 	return (EOPNOTSUPP);
1740 }
1741 
1742 static
1743 int
1744 hammer2_vop_close(struct vop_close_args *ap)
1745 {
1746 #if 0
1747 	return vop_stdclose(ap);
1748 #endif
1749 	return (EOPNOTSUPP);
1750 }
1751 
1752 /*
1753  * hammer2_vop_nlink { nch, dvp, vp, cred }
1754  *
1755  * Create a hardlink from (vp) to {dvp, nch}.
1756  */
1757 static
1758 int
1759 hammer2_vop_nlink(struct vop_nlink_args *ap)
1760 {
1761 	hammer2_inode_t *tdip;	/* target directory to create link in */
1762 	hammer2_inode_t *ip;	/* inode we are hardlinking to */
1763 	struct namecache *ncp;
1764 	const char *name;
1765 	size_t name_len;
1766 	int error;
1767 	uint64_t cmtime;
1768 
1769 	/* We know it's the same in makefs */
1770 	/*
1771 	if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1772 		return(EXDEV);
1773 	*/
1774 
1775 	tdip = VTOI(ap->a_dvp);
1776 	if (tdip->pmp->ronly || (tdip->pmp->flags & HAMMER2_PMPF_EMERG))
1777 		return (EROFS);
1778 	if (hammer2_vfs_enospace(tdip, 0, ap->a_cred) > 1)
1779 		return (ENOSPC);
1780 
1781 	ncp = ap->a_nch->ncp;
1782 	name = ncp->nc_name;
1783 	name_len = ncp->nc_nlen;
1784 
1785 	/*
1786 	 * ip represents the file being hardlinked.  The file could be a
1787 	 * normal file or a hardlink target if it has already been hardlinked.
1788 	 * (with the new semantics, it will almost always be a hardlink
1789 	 * target).
1790 	 *
1791 	 * Bump nlinks and potentially also create or move the hardlink
1792 	 * target in the parent directory common to (ip) and (tdip).  The
1793 	 * consolidation code can modify ip->cluster.  The returned cluster
1794 	 * is locked.
1795 	 */
1796 	ip = VTOI(ap->a_vp);
1797 	KASSERT(ip->pmp, ("ip->pmp is NULL %p %p", ip, ip->pmp));
1798 	hammer2_trans_init(ip->pmp, 0);
1799 
1800 	/*
1801 	 * Target should be an indexed inode or there's no way we will ever
1802 	 * be able to find it!
1803 	 */
1804 	KKASSERT((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0);
1805 
1806 	error = 0;
1807 
1808 	/*
1809 	 * Can return NULL and error == EXDEV if the common parent
1810 	 * crosses a directory with the xlink flag set.
1811 	 */
1812 	hammer2_inode_lock4(tdip, ip, NULL, NULL);
1813 
1814 	hammer2_update_time(&cmtime, true);
1815 
1816 	/*
1817 	 * Create the directory entry and bump nlinks.
1818 	 * Also update ip's ctime.
1819 	 */
1820 	if (error == 0) {
1821 		error = hammer2_dirent_create(tdip, name, name_len,
1822 					      ip->meta.inum, ip->meta.type);
1823 		hammer2_inode_modify(ip);
1824 		++ip->meta.nlinks;
1825 		ip->meta.ctime = cmtime;
1826 	}
1827 	if (error == 0) {
1828 		/*
1829 		 * Update dip's [cm]time
1830 		 */
1831 		hammer2_inode_modify(tdip);
1832 		tdip->meta.mtime = cmtime;
1833 		tdip->meta.ctime = cmtime;
1834 
1835 		cache_setunresolved(ap->a_nch);
1836 		cache_setvp(ap->a_nch, ap->a_vp);
1837 	}
1838 	hammer2_inode_unlock(ip);
1839 	hammer2_inode_unlock(tdip);
1840 
1841 	hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ);
1842 	hammer2_knote(ap->a_vp, NOTE_LINK);
1843 	hammer2_knote(ap->a_dvp, NOTE_WRITE);
1844 
1845 	return error;
1846 }
1847 
1848 int
1849 hammer2_nlink(struct m_vnode *dvp, struct m_vnode *vp, char *name, int nlen)
1850 {
1851 	struct namecache nc = {
1852 		.nc_name = name,
1853 		.nc_nlen = nlen,
1854 	};
1855 	struct nchandle nch = {
1856 		.ncp = &nc,
1857 	};
1858 	struct vop_nlink_args ap = {
1859 		.a_nch = &nch,
1860 		.a_dvp = dvp,
1861 		.a_vp = vp,
1862 	};
1863 
1864 	return hammer2_vop_nlink(&ap);
1865 }
1866 
1867 /*
1868  * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1869  *
1870  * The operating system has already ensured that the directory entry
1871  * does not exist and done all appropriate namespace locking.
1872  */
1873 static
1874 int
1875 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1876 {
1877 	hammer2_inode_t *dip;
1878 	hammer2_inode_t *nip;
1879 	struct namecache *ncp;
1880 	const char *name;
1881 	size_t name_len;
1882 	hammer2_tid_t inum;
1883 	int error;
1884 
1885 	dip = VTOI(ap->a_dvp);
1886 	if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG))
1887 		return (EROFS);
1888 	if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1889 		return (ENOSPC);
1890 
1891 	ncp = ap->a_nch->ncp;
1892 	name = ncp->nc_name;
1893 	name_len = ncp->nc_nlen;
1894 	hammer2_trans_init(dip->pmp, 0);
1895 
1896 	inum = hammer2_trans_newinum(dip->pmp);
1897 
1898 	/*
1899 	 * Create the actual inode as a hidden file in the iroot, then
1900 	 * create the directory entry.  The creation of the actual inode
1901 	 * sets its nlinks to 1 which is the value we desire.
1902 	 *
1903 	 * dip must be locked before nip to avoid deadlock.
1904 	 */
1905 	hammer2_inode_lock(dip, 0);
1906 	nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
1907 					  inum, &error);
1908 
1909 	if (error) {
1910 		error = hammer2_error_to_errno(error);
1911 	} else {
1912 		error = hammer2_dirent_create(dip, name, name_len,
1913 					      nip->meta.inum, nip->meta.type);
1914 	}
1915 	if (error) {
1916 		if (nip) {
1917 			hammer2_inode_unlink_finisher(nip, NULL);
1918 			hammer2_inode_unlock(nip);
1919 			nip = NULL;
1920 		}
1921 		*ap->a_vpp = NULL;
1922 	} else {
1923 		hammer2_inode_depend(dip, nip);	/* before igetv */
1924 		*ap->a_vpp = hammer2_igetv(nip, &error);
1925 		hammer2_inode_unlock(nip);
1926 	}
1927 
1928 	/*
1929 	 * Update dip's mtime
1930 	 */
1931 	if (error == 0) {
1932 		uint64_t mtime;
1933 
1934 		/*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1935 		hammer2_update_time(&mtime, true);
1936 		hammer2_inode_modify(dip);
1937 		dip->meta.mtime = mtime;
1938 		/*hammer2_inode_unlock(dip);*/
1939 	}
1940 	hammer2_inode_unlock(dip);
1941 
1942 	hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1943 
1944 	if (error == 0) {
1945 		cache_setunresolved(ap->a_nch);
1946 		cache_setvp(ap->a_nch, *ap->a_vpp);
1947 		hammer2_knote(ap->a_dvp, NOTE_WRITE);
1948 	}
1949 	return error;
1950 }
1951 
1952 int
1953 hammer2_ncreate(struct m_vnode *dvp, struct m_vnode **vpp, char *name, int nlen,
1954 		mode_t mode)
1955 {
1956 	struct namecache nc = {
1957 		.nc_name = name,
1958 		.nc_nlen = nlen,
1959 	};
1960 	struct nchandle nch = {
1961 		.ncp = &nc,
1962 	};
1963 	uid_t va_uid = VNOVAL; //getuid();
1964 	uid_t va_gid = VNOVAL; //getgid();
1965 	struct vattr va = {
1966 		.va_type = VREG,
1967 		.va_mode = mode & ~S_IFMT,
1968 		.va_uid = va_uid,
1969 		.va_gid = va_gid,
1970 	};
1971 	struct vop_ncreate_args ap = {
1972 		.a_nch = &nch,
1973 		.a_dvp = dvp,
1974 		.a_vpp = vpp,
1975 		.a_vap = &va,
1976 	};
1977 
1978 	return hammer2_vop_ncreate(&ap);
1979 }
1980 
1981 /*
1982  * Make a device node (typically a fifo)
1983  */
1984 static
1985 int
1986 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1987 {
1988 	hammer2_inode_t *dip;
1989 	hammer2_inode_t *nip;
1990 	struct namecache *ncp;
1991 	const char *name;
1992 	size_t name_len;
1993 	hammer2_tid_t inum;
1994 	int error;
1995 
1996 	dip = VTOI(ap->a_dvp);
1997 	if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG))
1998 		return (EROFS);
1999 	if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
2000 		return (ENOSPC);
2001 
2002 	ncp = ap->a_nch->ncp;
2003 	name = ncp->nc_name;
2004 	name_len = ncp->nc_nlen;
2005 	hammer2_trans_init(dip->pmp, 0);
2006 
2007 	/*
2008 	 * Create the device inode and then create the directory entry.
2009 	 *
2010 	 * dip must be locked before nip to avoid deadlock.
2011 	 */
2012 	inum = hammer2_trans_newinum(dip->pmp);
2013 
2014 	hammer2_inode_lock(dip, 0);
2015 	nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
2016 					  inum, &error);
2017 	if (error == 0) {
2018 		error = hammer2_dirent_create(dip, name, name_len,
2019 					      nip->meta.inum, nip->meta.type);
2020 	}
2021 	if (error) {
2022 		if (nip) {
2023 			hammer2_inode_unlink_finisher(nip, NULL);
2024 			hammer2_inode_unlock(nip);
2025 			nip = NULL;
2026 		}
2027 		*ap->a_vpp = NULL;
2028 	} else {
2029 		hammer2_inode_depend(dip, nip);	/* before igetv */
2030 		*ap->a_vpp = hammer2_igetv(nip, &error);
2031 		hammer2_inode_unlock(nip);
2032 	}
2033 
2034 	/*
2035 	 * Update dip's mtime
2036 	 */
2037 	if (error == 0) {
2038 		uint64_t mtime;
2039 
2040 		/*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
2041 		hammer2_update_time(&mtime, true);
2042 		hammer2_inode_modify(dip);
2043 		dip->meta.mtime = mtime;
2044 		/*hammer2_inode_unlock(dip);*/
2045 	}
2046 	hammer2_inode_unlock(dip);
2047 
2048 	hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
2049 
2050 	if (error == 0) {
2051 		cache_setunresolved(ap->a_nch);
2052 		cache_setvp(ap->a_nch, *ap->a_vpp);
2053 		hammer2_knote(ap->a_dvp, NOTE_WRITE);
2054 	}
2055 	return error;
2056 }
2057 
2058 int
2059 hammer2_nmknod(struct m_vnode *dvp, struct m_vnode **vpp, char *name, int nlen,
2060 		int type, mode_t mode)
2061 {
2062 	struct namecache nc = {
2063 		.nc_name = name,
2064 		.nc_nlen = nlen,
2065 	};
2066 	struct nchandle nch = {
2067 		.ncp = &nc,
2068 	};
2069 	uid_t va_uid = VNOVAL; //getuid();
2070 	uid_t va_gid = VNOVAL; //getgid();
2071 	struct vattr va = {
2072 		.va_type = type,
2073 		.va_mode = mode & ~S_IFMT,
2074 		.va_uid = va_uid,
2075 		.va_gid = va_gid,
2076 	};
2077 	struct vop_nmknod_args ap = {
2078 		.a_nch = &nch,
2079 		.a_dvp = dvp,
2080 		.a_vpp = vpp,
2081 		.a_vap = &va,
2082 	};
2083 
2084 	return hammer2_vop_nmknod(&ap);
2085 }
2086 
2087 /*
2088  * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2089  */
2090 static
2091 int
2092 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
2093 {
2094 	hammer2_inode_t *dip;
2095 	hammer2_inode_t *nip;
2096 	struct namecache *ncp;
2097 	const char *name;
2098 	size_t name_len;
2099 	hammer2_tid_t inum;
2100 	int error;
2101 
2102 	dip = VTOI(ap->a_dvp);
2103 	if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG))
2104 		return (EROFS);
2105 	if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
2106 		return (ENOSPC);
2107 
2108 	ncp = ap->a_nch->ncp;
2109 	name = ncp->nc_name;
2110 	name_len = ncp->nc_nlen;
2111 	hammer2_trans_init(dip->pmp, 0);
2112 
2113 	ap->a_vap->va_type = VLNK;	/* enforce type */
2114 
2115 	/*
2116 	 * Create the softlink as an inode and then create the directory
2117 	 * entry.
2118 	 *
2119 	 * dip must be locked before nip to avoid deadlock.
2120 	 */
2121 	inum = hammer2_trans_newinum(dip->pmp);
2122 
2123 	hammer2_inode_lock(dip, 0);
2124 	nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
2125 					  inum, &error);
2126 	if (error == 0) {
2127 		error = hammer2_dirent_create(dip, name, name_len,
2128 					      nip->meta.inum, nip->meta.type);
2129 	}
2130 	if (error) {
2131 		if (nip) {
2132 			hammer2_inode_unlink_finisher(nip, NULL);
2133 			hammer2_inode_unlock(nip);
2134 			nip = NULL;
2135 		}
2136 		*ap->a_vpp = NULL;
2137 		hammer2_inode_unlock(dip);
2138 		hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
2139 		return error;
2140 	}
2141 	hammer2_inode_depend(dip, nip);	/* before igetv */
2142 	*ap->a_vpp = hammer2_igetv(nip, &error);
2143 
2144 	/*
2145 	 * Build the softlink (~like file data) and finalize the namecache.
2146 	 */
2147 	if (error == 0) {
2148 		size_t bytes;
2149 		struct uio auio;
2150 		struct iovec aiov;
2151 
2152 		bytes = strlen(ap->a_target);
2153 
2154 		hammer2_inode_unlock(nip);
2155 		bzero(&auio, sizeof(auio));
2156 		bzero(&aiov, sizeof(aiov));
2157 		auio.uio_iov = &aiov;
2158 		auio.uio_segflg = UIO_SYSSPACE;
2159 		auio.uio_rw = UIO_WRITE;
2160 		auio.uio_resid = bytes;
2161 		auio.uio_iovcnt = 1;
2162 		auio.uio_td = curthread;
2163 		aiov.iov_base = ap->a_target;
2164 		aiov.iov_len = bytes;
2165 		error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
2166 		/* XXX handle error */
2167 		error = 0;
2168 	} else {
2169 		hammer2_inode_unlock(nip);
2170 	}
2171 
2172 	/*
2173 	 * Update dip's mtime
2174 	 */
2175 	if (error == 0) {
2176 		uint64_t mtime;
2177 
2178 		/*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
2179 		hammer2_update_time(&mtime, true);
2180 		hammer2_inode_modify(dip);
2181 		dip->meta.mtime = mtime;
2182 		/*hammer2_inode_unlock(dip);*/
2183 	}
2184 	hammer2_inode_unlock(dip);
2185 
2186 	hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
2187 
2188 	/*
2189 	 * Finalize namecache
2190 	 */
2191 	if (error == 0) {
2192 		cache_setunresolved(ap->a_nch);
2193 		cache_setvp(ap->a_nch, *ap->a_vpp);
2194 		hammer2_knote(ap->a_dvp, NOTE_WRITE);
2195 	}
2196 	return error;
2197 }
2198 
2199 int
2200 hammer2_nsymlink(struct m_vnode *dvp, struct m_vnode **vpp, char *name, int nlen,
2201 			char *target, mode_t mode)
2202 {
2203 	struct namecache nc = {
2204 		.nc_name = name,
2205 		.nc_nlen = nlen,
2206 	};
2207 	struct nchandle nch = {
2208 		.ncp = &nc,
2209 	};
2210 	uid_t va_uid = VNOVAL; //getuid();
2211 	uid_t va_gid = VNOVAL; //getgid();
2212 	struct vattr va = {
2213 		.va_type = VDIR,
2214 		.va_mode = mode & ~S_IFMT,
2215 		.va_uid = va_uid,
2216 		.va_gid = va_gid,
2217 	};
2218 	struct vop_nsymlink_args ap = {
2219 		.a_nch = &nch,
2220 		.a_dvp = dvp,
2221 		.a_vpp = vpp,
2222 		.a_vap = &va,
2223 		.a_target = target,
2224 	};
2225 
2226 	return hammer2_vop_nsymlink(&ap);
2227 }
2228 
2229 /*
2230  * hammer2_vop_nremove { nch, dvp, cred }
2231  */
2232 static
2233 int
2234 hammer2_vop_nremove(struct vop_nremove_args *ap)
2235 {
2236 #if 0
2237 	hammer2_xop_unlink_t *xop;
2238 	hammer2_inode_t *dip;
2239 	hammer2_inode_t *ip;
2240 	struct m_vnode *vprecycle;
2241 	struct namecache *ncp;
2242 	int error;
2243 
2244 	dip = VTOI(ap->a_dvp);
2245 	if (dip->pmp->ronly)
2246 		return (EROFS);
2247 #if 0
2248 	/* allow removals, except user to also bulkfree */
2249 	if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
2250 		return (ENOSPC);
2251 #endif
2252 
2253 	ncp = ap->a_nch->ncp;
2254 
2255 	if (hammer2_debug_inode && dip->meta.inum == hammer2_debug_inode) {
2256 		kprintf("hammer2: attempt to delete inside debug inode: %s\n",
2257 			ncp->nc_name);
2258 		while (hammer2_debug_inode &&
2259 		       dip->meta.inum == hammer2_debug_inode) {
2260 			tsleep(&hammer2_debug_inode, 0, "h2debug", hz*5);
2261 		}
2262 	}
2263 
2264 	hammer2_trans_init(dip->pmp, 0);
2265 	hammer2_inode_lock(dip, 0);
2266 
2267 	/*
2268 	 * The unlink XOP unlinks the path from the directory and
2269 	 * locates and returns the cluster associated with the real inode.
2270 	 * We have to handle nlinks here on the frontend.
2271 	 */
2272 	xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
2273 	hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
2274 
2275 	xop->isdir = 0;
2276 	xop->dopermanent = 0;
2277 	hammer2_xop_start(&xop->head, &hammer2_unlink_desc);
2278 
2279 	/*
2280 	 * Collect the real inode and adjust nlinks, destroy the real
2281 	 * inode if nlinks transitions to 0 and it was the real inode
2282 	 * (else it has already been removed).
2283 	 */
2284 	error = hammer2_xop_collect(&xop->head, 0);
2285 	error = hammer2_error_to_errno(error);
2286 	vprecycle = NULL;
2287 
2288 	if (error == 0) {
2289 		ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1);
2290 		hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2291 		if (ip) {
2292 			if (hammer2_debug_inode &&
2293 			    ip->meta.inum == hammer2_debug_inode) {
2294 				kprintf("hammer2: attempt to delete debug "
2295 					"inode!\n");
2296 				while (hammer2_debug_inode &&
2297 				       ip->meta.inum == hammer2_debug_inode) {
2298 					tsleep(&hammer2_debug_inode, 0,
2299 					       "h2debug", hz*5);
2300 				}
2301 			}
2302 			hammer2_inode_unlink_finisher(ip, &vprecycle);
2303 			hammer2_inode_depend(dip, ip); /* after modified */
2304 			hammer2_inode_unlock(ip);
2305 		}
2306 	} else {
2307 		hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2308 	}
2309 
2310 	/*
2311 	 * Update dip's mtime
2312 	 */
2313 	if (error == 0) {
2314 		uint64_t mtime;
2315 
2316 		/*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
2317 		hammer2_update_time(&mtime);
2318 		hammer2_inode_modify(dip);
2319 		dip->meta.mtime = mtime;
2320 		/*hammer2_inode_unlock(dip);*/
2321 	}
2322 	hammer2_inode_unlock(dip);
2323 
2324 	hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
2325 	if (error == 0) {
2326 		cache_unlink(ap->a_nch);
2327 		hammer2_knote(ap->a_dvp, NOTE_WRITE);
2328 	}
2329 	if (vprecycle)
2330 		hammer2_inode_vprecycle(vprecycle);
2331 
2332 	return (error);
2333 #endif
2334 	return (EOPNOTSUPP);
2335 }
2336 
2337 /*
2338  * hammer2_vop_nrmdir { nch, dvp, cred }
2339  */
2340 static
2341 int
2342 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
2343 {
2344 #if 0
2345 	hammer2_xop_unlink_t *xop;
2346 	hammer2_inode_t *dip;
2347 	hammer2_inode_t *ip;
2348 	struct namecache *ncp;
2349 	struct m_vnode *vprecycle;
2350 	int error;
2351 
2352 	dip = VTOI(ap->a_dvp);
2353 	if (dip->pmp->ronly)
2354 		return (EROFS);
2355 #if 0
2356 	/* allow removals, except user to also bulkfree */
2357 	if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
2358 		return (ENOSPC);
2359 #endif
2360 
2361 	hammer2_trans_init(dip->pmp, 0);
2362 	hammer2_inode_lock(dip, 0);
2363 
2364 	xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
2365 
2366 	ncp = ap->a_nch->ncp;
2367 	hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
2368 	xop->isdir = 1;
2369 	xop->dopermanent = 0;
2370 	hammer2_xop_start(&xop->head, &hammer2_unlink_desc);
2371 
2372 	/*
2373 	 * Collect the real inode and adjust nlinks, destroy the real
2374 	 * inode if nlinks transitions to 0 and it was the real inode
2375 	 * (else it has already been removed).
2376 	 */
2377 	error = hammer2_xop_collect(&xop->head, 0);
2378 	error = hammer2_error_to_errno(error);
2379 	vprecycle = NULL;
2380 
2381 	if (error == 0) {
2382 		ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1);
2383 		hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2384 		if (ip) {
2385 			hammer2_inode_unlink_finisher(ip, &vprecycle);
2386 			hammer2_inode_depend(dip, ip);	/* after modified */
2387 			hammer2_inode_unlock(ip);
2388 		}
2389 	} else {
2390 		hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2391 	}
2392 
2393 	/*
2394 	 * Update dip's mtime
2395 	 */
2396 	if (error == 0) {
2397 		uint64_t mtime;
2398 
2399 		/*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
2400 		hammer2_update_time(&mtime);
2401 		hammer2_inode_modify(dip);
2402 		dip->meta.mtime = mtime;
2403 		/*hammer2_inode_unlock(dip);*/
2404 	}
2405 	hammer2_inode_unlock(dip);
2406 
2407 	hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
2408 	if (error == 0) {
2409 		cache_unlink(ap->a_nch);
2410 		hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2411 	}
2412 	if (vprecycle)
2413 		hammer2_inode_vprecycle(vprecycle);
2414 	return (error);
2415 #endif
2416 	return (EOPNOTSUPP);
2417 }
2418 
2419 /*
2420  * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
2421  */
2422 static
2423 int
2424 hammer2_vop_nrename(struct vop_nrename_args *ap)
2425 {
2426 #if 0
2427 	struct namecache *fncp;
2428 	struct namecache *tncp;
2429 	hammer2_inode_t *fdip;	/* source directory */
2430 	hammer2_inode_t *tdip;	/* target directory */
2431 	hammer2_inode_t *ip;	/* file being renamed */
2432 	hammer2_inode_t *tip;	/* replaced target during rename or NULL */
2433 	struct m_vnode *vprecycle;
2434 	const char *fname;
2435 	size_t fname_len;
2436 	const char *tname;
2437 	size_t tname_len;
2438 	int error;
2439 	int update_tdip;
2440 	int update_fdip;
2441 	hammer2_key_t tlhc;
2442 
2443 	if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
2444 		return(EXDEV);
2445 	if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
2446 		return(EXDEV);
2447 
2448 	fdip = VTOI(ap->a_fdvp);	/* source directory */
2449 	tdip = VTOI(ap->a_tdvp);	/* target directory */
2450 
2451 	if (fdip->pmp->ronly || (fdip->pmp->flags & HAMMER2_PMPF_EMERG))
2452 		return (EROFS);
2453 	if (hammer2_vfs_enospace(fdip, 0, ap->a_cred) > 1)
2454 		return (ENOSPC);
2455 
2456 	fncp = ap->a_fnch->ncp;		/* entry name in source */
2457 	fname = fncp->nc_name;
2458 	fname_len = fncp->nc_nlen;
2459 
2460 	tncp = ap->a_tnch->ncp;		/* entry name in target */
2461 	tname = tncp->nc_name;
2462 	tname_len = tncp->nc_nlen;
2463 
2464 	hammer2_trans_init(tdip->pmp, 0);
2465 
2466 	update_tdip = 0;
2467 	update_fdip = 0;
2468 
2469 	ip = VTOI(fncp->nc_vp);
2470 	hammer2_inode_ref(ip);		/* extra ref */
2471 
2472 	/*
2473 	 * Lookup the target name to determine if a directory entry
2474 	 * is being overwritten.  We only hold related inode locks
2475 	 * temporarily, the operating system is expected to protect
2476 	 * against rename races.
2477 	 */
2478 	tip = tncp->nc_vp ? VTOI(tncp->nc_vp) : NULL;
2479 	if (tip)
2480 		hammer2_inode_ref(tip);	/* extra ref */
2481 
2482 	/*
2483 	 * Can return NULL and error == EXDEV if the common parent
2484 	 * crosses a directory with the xlink flag set.
2485 	 *
2486 	 * For now try to avoid deadlocks with a simple pointer address
2487 	 * test.  (tip) can be NULL.
2488 	 */
2489 	error = 0;
2490 	{
2491 		hammer2_inode_t *ip1 = fdip;
2492 		hammer2_inode_t *ip2 = tdip;
2493 		hammer2_inode_t *ip3 = ip;
2494 		hammer2_inode_t *ip4 = tip;	/* may be NULL */
2495 
2496 		if (fdip > tdip) {
2497 			ip1 = tdip;
2498 			ip2 = fdip;
2499 		}
2500 		if (tip && ip > tip) {
2501 			ip3 = tip;
2502 			ip4 = ip;
2503 		}
2504 		hammer2_inode_lock4(ip1, ip2, ip3, ip4);
2505 	}
2506 
2507 	/*
2508 	 * Resolve the collision space for (tdip, tname, tname_len)
2509 	 *
2510 	 * tdip must be held exclusively locked to prevent races since
2511 	 * multiple filenames can end up in the same collision space.
2512 	 */
2513 	{
2514 		hammer2_xop_scanlhc_t *sxop;
2515 		hammer2_tid_t lhcbase;
2516 
2517 		tlhc = hammer2_dirhash(tname, tname_len);
2518 		lhcbase = tlhc;
2519 		sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
2520 		sxop->lhc = tlhc;
2521 		hammer2_xop_start(&sxop->head, &hammer2_scanlhc_desc);
2522 		while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
2523 			if (tlhc != sxop->head.cluster.focus->bref.key)
2524 				break;
2525 			++tlhc;
2526 		}
2527 		error = hammer2_error_to_errno(error);
2528 		hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
2529 
2530 		if (error) {
2531 			if (error != ENOENT)
2532 				goto done2;
2533 			++tlhc;
2534 			error = 0;
2535 		}
2536 		if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) {
2537 			error = ENOSPC;
2538 			goto done2;
2539 		}
2540 	}
2541 
2542 	/*
2543 	 * Ready to go, issue the rename to the backend.  Note that meta-data
2544 	 * updates to the related inodes occur separately from the rename
2545 	 * operation.
2546 	 *
2547 	 * NOTE: While it is not necessary to update ip->meta.name*, doing
2548 	 *	 so aids catastrophic recovery and debugging.
2549 	 */
2550 	if (error == 0) {
2551 		hammer2_xop_nrename_t *xop4;
2552 
2553 		xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
2554 		xop4->lhc = tlhc;
2555 		xop4->ip_key = ip->meta.name_key;
2556 		hammer2_xop_setip2(&xop4->head, ip);
2557 		hammer2_xop_setip3(&xop4->head, tdip);
2558 		if (tip && tip->meta.type == HAMMER2_OBJTYPE_DIRECTORY)
2559 		    hammer2_xop_setip4(&xop4->head, tip);
2560 		hammer2_xop_setname(&xop4->head, fname, fname_len);
2561 		hammer2_xop_setname2(&xop4->head, tname, tname_len);
2562 		hammer2_xop_start(&xop4->head, &hammer2_nrename_desc);
2563 
2564 		error = hammer2_xop_collect(&xop4->head, 0);
2565 		error = hammer2_error_to_errno(error);
2566 		hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP);
2567 
2568 		if (error == ENOENT)
2569 			error = 0;
2570 
2571 		/*
2572 		 * Update inode meta-data.
2573 		 *
2574 		 * WARNING!  The in-memory inode (ip) structure does not
2575 		 *	     maintain a copy of the inode's filename buffer.
2576 		 */
2577 		if (error == 0 &&
2578 		    (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
2579 			hammer2_inode_modify(ip);
2580 			ip->meta.name_len = tname_len;
2581 			ip->meta.name_key = tlhc;
2582 		}
2583 		if (error == 0) {
2584 			hammer2_inode_modify(ip);
2585 			ip->meta.iparent = tdip->meta.inum;
2586 		}
2587 		update_fdip = 1;
2588 		update_tdip = 1;
2589 	}
2590 
2591 done2:
2592 	/*
2593 	 * If no error, the backend has replaced the target directory entry.
2594 	 * We must adjust nlinks on the original replace target if it exists.
2595 	 */
2596 	vprecycle = NULL;
2597 	if (error == 0 && tip) {
2598 		hammer2_inode_unlink_finisher(tip, &vprecycle);
2599 	}
2600 
2601 	/*
2602 	 * Update directory mtimes to represent the something changed.
2603 	 */
2604 	if (update_fdip || update_tdip) {
2605 		uint64_t mtime;
2606 
2607 		hammer2_update_time(&mtime);
2608 		if (update_fdip) {
2609 			hammer2_inode_modify(fdip);
2610 			fdip->meta.mtime = mtime;
2611 		}
2612 		if (update_tdip) {
2613 			hammer2_inode_modify(tdip);
2614 			tdip->meta.mtime = mtime;
2615 		}
2616 	}
2617 	if (tip) {
2618 		hammer2_inode_unlock(tip);
2619 		hammer2_inode_drop(tip);
2620 	}
2621 	hammer2_inode_unlock(ip);
2622 	hammer2_inode_unlock(tdip);
2623 	hammer2_inode_unlock(fdip);
2624 	hammer2_inode_drop(ip);
2625 	hammer2_trans_done(tdip->pmp, HAMMER2_TRANS_SIDEQ);
2626 
2627 	/*
2628 	 * Issue the namecache update after unlocking all the internal
2629 	 * hammer2 structures, otherwise we might deadlock.
2630 	 *
2631 	 * WARNING! The target namespace must be updated atomically,
2632 	 *	    and we depend on cache_rename() to handle that for
2633 	 *	    us.  Do not do a separate cache_unlink() because
2634 	 *	    that leaves a small window of opportunity for other
2635 	 *	    threads to allocate the target namespace before we
2636 	 *	    manage to complete our rename.
2637 	 *
2638 	 * WARNING! cache_rename() (and cache_unlink()) will properly
2639 	 *	    set VREF_FINALIZE on any attached vnode.  Do not
2640 	 *	    call cache_setunresolved() manually before-hand as
2641 	 *	    this will prevent the flag from being set later via
2642 	 *	    cache_rename().  If VREF_FINALIZE is not properly set
2643 	 *	    and the inode is no longer in the topology, related
2644 	 *	    chains can remain dirty indefinitely.
2645 	 */
2646 	if (error == 0 && tip) {
2647 		/*cache_unlink(ap->a_tnch); see above */
2648 		/*cache_setunresolved(ap->a_tnch); see above */
2649 	}
2650 	if (error == 0) {
2651 		cache_rename(ap->a_fnch, ap->a_tnch);
2652 		hammer2_knote(ap->a_fdvp, NOTE_WRITE);
2653 		hammer2_knote(ap->a_tdvp, NOTE_WRITE);
2654 		hammer2_knote(fncp->nc_vp, NOTE_RENAME);
2655 	}
2656 	if (vprecycle)
2657 		hammer2_inode_vprecycle(vprecycle);
2658 
2659 	return (error);
2660 #endif
2661 	return (EOPNOTSUPP);
2662 }
2663 
2664 /*
2665  * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2666  */
2667 static
2668 int
2669 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2670 {
2671 #if 0
2672 	hammer2_inode_t *ip;
2673 	int error;
2674 
2675 	ip = VTOI(ap->a_vp);
2676 
2677 	error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2678 			      ap->a_fflag, ap->a_cred);
2679 	return (error);
2680 #endif
2681 	return (EOPNOTSUPP);
2682 }
2683 
2684 static
2685 int
2686 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2687 {
2688 #if 0
2689 	struct mount *mp;
2690 	hammer2_pfs_t *pmp;
2691 	int rc;
2692 
2693 	switch (ap->a_op) {
2694 	case (MOUNTCTL_SET_EXPORT):
2695 		mp = ap->a_head.a_ops->head.vv_mount;
2696 		pmp = MPTOPMP(mp);
2697 
2698 		if (ap->a_ctllen != sizeof(struct export_args))
2699 			rc = (EINVAL);
2700 		else
2701 			rc = vfs_export(mp, &pmp->export,
2702 					(const struct export_args *)ap->a_ctl);
2703 		break;
2704 	default:
2705 		rc = vop_stdmountctl(ap);
2706 		break;
2707 	}
2708 	return (rc);
2709 #endif
2710 	return (EOPNOTSUPP);
2711 }
2712 
2713 /*
2714  * KQFILTER
2715  */
2716 /*
2717 static void filt_hammer2detach(struct knote *kn);
2718 static int filt_hammer2read(struct knote *kn, long hint);
2719 static int filt_hammer2write(struct knote *kn, long hint);
2720 static int filt_hammer2vnode(struct knote *kn, long hint);
2721 
2722 static struct filterops hammer2read_filtops =
2723 	{ FILTEROP_ISFD | FILTEROP_MPSAFE,
2724 	  NULL, filt_hammer2detach, filt_hammer2read };
2725 static struct filterops hammer2write_filtops =
2726 	{ FILTEROP_ISFD | FILTEROP_MPSAFE,
2727 	  NULL, filt_hammer2detach, filt_hammer2write };
2728 static struct filterops hammer2vnode_filtops =
2729 	{ FILTEROP_ISFD | FILTEROP_MPSAFE,
2730 	  NULL, filt_hammer2detach, filt_hammer2vnode };
2731 */
2732 
2733 static
2734 int
2735 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
2736 {
2737 #if 0
2738 	struct m_vnode *vp = ap->a_vp;
2739 	struct knote *kn = ap->a_kn;
2740 
2741 	switch (kn->kn_filter) {
2742 	case EVFILT_READ:
2743 		kn->kn_fop = &hammer2read_filtops;
2744 		break;
2745 	case EVFILT_WRITE:
2746 		kn->kn_fop = &hammer2write_filtops;
2747 		break;
2748 	case EVFILT_VNODE:
2749 		kn->kn_fop = &hammer2vnode_filtops;
2750 		break;
2751 	default:
2752 		return (EOPNOTSUPP);
2753 	}
2754 
2755 	kn->kn_hook = (caddr_t)vp;
2756 
2757 	knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2758 
2759 	return(0);
2760 #endif
2761 	return (EOPNOTSUPP);
2762 }
2763 
2764 #if 0
2765 static void
2766 filt_hammer2detach(struct knote *kn)
2767 {
2768 	struct m_vnode *vp = (void *)kn->kn_hook;
2769 
2770 	knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2771 }
2772 
2773 static int
2774 filt_hammer2read(struct knote *kn, long hint)
2775 {
2776 	struct m_vnode *vp = (void *)kn->kn_hook;
2777 	hammer2_inode_t *ip = VTOI(vp);
2778 	off_t off;
2779 
2780 	if (hint == NOTE_REVOKE) {
2781 		kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2782 		return(1);
2783 	}
2784 	off = ip->meta.size - kn->kn_fp->f_offset;
2785 	kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2786 	if (kn->kn_sfflags & NOTE_OLDAPI)
2787 		return(1);
2788 	return (kn->kn_data != 0);
2789 }
2790 
2791 
2792 static int
2793 filt_hammer2write(struct knote *kn, long hint)
2794 {
2795 	if (hint == NOTE_REVOKE)
2796 		kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2797 	kn->kn_data = 0;
2798 	return (1);
2799 }
2800 
2801 static int
2802 filt_hammer2vnode(struct knote *kn, long hint)
2803 {
2804 	if (kn->kn_sfflags & hint)
2805 		kn->kn_fflags |= hint;
2806 	if (hint == NOTE_REVOKE) {
2807 		kn->kn_flags |= (EV_EOF | EV_NODATA);
2808 		return (1);
2809 	}
2810 	return (kn->kn_fflags != 0);
2811 }
2812 #endif
2813 
2814 /*
2815  * FIFO VOPS
2816  */
2817 static
2818 int
2819 hammer2_vop_markatime(struct vop_markatime_args *ap)
2820 {
2821 #if 0
2822 	hammer2_inode_t *ip;
2823 	struct m_vnode *vp;
2824 
2825 	vp = ap->a_vp;
2826 	ip = VTOI(vp);
2827 
2828 	if (ip->pmp->ronly || (ip->pmp->flags & HAMMER2_PMPF_EMERG))
2829 		return (EROFS);
2830 	return(0);
2831 #endif
2832 	return (EOPNOTSUPP);
2833 }
2834 
2835 static
2836 int
2837 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2838 {
2839 #if 0
2840 	int error;
2841 
2842 	error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2843 	if (error)
2844 		error = hammer2_vop_kqfilter(ap);
2845 	return(error);
2846 #endif
2847 	return (EOPNOTSUPP);
2848 }
2849 
2850 /*
2851  * VOPS vector
2852  */
2853 struct vop_ops hammer2_vnode_vops = {
2854 	.vop_default	= vop_defaultop,
2855 	.vop_fsync	= hammer2_vop_fsync,
2856 	.vop_getpages	= vop_stdgetpages,
2857 	.vop_putpages	= vop_stdputpages,
2858 	.vop_access	= hammer2_vop_access,
2859 	.vop_advlock	= hammer2_vop_advlock,
2860 	.vop_close	= hammer2_vop_close,
2861 	.vop_nlink	= hammer2_vop_nlink,
2862 	.vop_ncreate	= hammer2_vop_ncreate,
2863 	.vop_nsymlink	= hammer2_vop_nsymlink,
2864 	.vop_nremove	= hammer2_vop_nremove,
2865 	.vop_nrmdir	= hammer2_vop_nrmdir,
2866 	.vop_nrename	= hammer2_vop_nrename,
2867 	.vop_getattr	= hammer2_vop_getattr,
2868 	.vop_getattr_lite = hammer2_vop_getattr_lite,
2869 	.vop_setattr	= hammer2_vop_setattr,
2870 	.vop_readdir	= hammer2_vop_readdir,
2871 	.vop_readlink	= hammer2_vop_readlink,
2872 	.vop_read	= hammer2_vop_read,
2873 	.vop_write	= hammer2_vop_write,
2874 	.vop_open	= hammer2_vop_open,
2875 	.vop_inactive	= hammer2_vop_inactive,
2876 	.vop_reclaim	= hammer2_vop_reclaim,
2877 	.vop_nresolve	= hammer2_vop_nresolve,
2878 	.vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2879 	.vop_nmkdir	= hammer2_vop_nmkdir,
2880 	.vop_nmknod	= hammer2_vop_nmknod,
2881 	.vop_ioctl	= hammer2_vop_ioctl,
2882 	.vop_mountctl	= hammer2_vop_mountctl,
2883 	.vop_bmap	= hammer2_vop_bmap,
2884 	.vop_strategy	= hammer2_vop_strategy,
2885 	.vop_kqfilter	= hammer2_vop_kqfilter
2886 };
2887 
2888 struct vop_ops hammer2_spec_vops = {
2889 	.vop_default =          vop_defaultop,
2890 	.vop_fsync =            hammer2_vop_fsync,
2891 	.vop_read =             vop_stdnoread,
2892 	.vop_write =            vop_stdnowrite,
2893 	.vop_access =           hammer2_vop_access,
2894 	.vop_close =            hammer2_vop_close,
2895 	.vop_markatime =        hammer2_vop_markatime,
2896 	.vop_getattr =          hammer2_vop_getattr,
2897 	.vop_inactive =         hammer2_vop_inactive,
2898 	.vop_reclaim =          hammer2_vop_reclaim,
2899 	.vop_setattr =          hammer2_vop_setattr
2900 };
2901 
2902 struct vop_ops hammer2_fifo_vops = {
2903 	.vop_default =          fifo_vnoperate,
2904 	.vop_fsync =            hammer2_vop_fsync,
2905 #if 0
2906 	.vop_read =             hammer2_vop_fiforead,
2907 	.vop_write =            hammer2_vop_fifowrite,
2908 #endif
2909 	.vop_access =           hammer2_vop_access,
2910 #if 0
2911 	.vop_close =            hammer2_vop_fifoclose,
2912 #endif
2913 	.vop_markatime =        hammer2_vop_markatime,
2914 	.vop_getattr =          hammer2_vop_getattr,
2915 	.vop_inactive =         hammer2_vop_inactive,
2916 	.vop_reclaim =          hammer2_vop_reclaim,
2917 	.vop_setattr =          hammer2_vop_setattr,
2918 	.vop_kqfilter =         hammer2_vop_fifokqfilter
2919 };
2920 
2921