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