usr.sbin/makefs: Add HAMMER2 offline read / VOP_READ support
[dragonfly.git] / usr.sbin / makefs / hammer2 / hammer2_vnops.c
blobf7add0346296c7773d6a993e8a424ed85bd26362
1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2022 Tomohiro Kusumi <tkusumi@netbsd.org>
5 * Copyright (c) 2011-2022 The DragonFly Project. All rights reserved.
7 * This code is derived from software contributed to The DragonFly Project
8 * by Matthew Dillon <dillon@dragonflybsd.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
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.
38 * Kernel Filesystem interface
40 * NOTE! local ipdata pointers must be reloaded on any modifying operation
41 * to the inode as its underlying chain may have changed.
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>
62 #include "hammer2.h"
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);
72 * Last reference to a vnode is going away but it is still cached.
74 static
75 int
76 hammer2_vop_inactive(struct vop_inactive_args *ap)
78 #if 0
79 hammer2_inode_t *ip;
80 struct m_vnode *vp;
82 vp = ap->a_vp;
83 ip = VTOI(vp);
86 * Degenerate case
88 if (ip == NULL) {
89 vrecycle(vp);
90 return (0);
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).
98 hammer2_inode_lock(ip, 0);
99 if (ip->flags & HAMMER2_INODE_ISUNLINKED) {
100 hammer2_key_t lbase;
101 int nblksize;
104 * If the inode has been unlinked we can throw away all
105 * buffers (dirty or not) and clean the file out.
107 * Because vrecycle() calls are not guaranteed, try to
108 * dispose of the inode as much as possible right here.
110 nblksize = hammer2_calc_logical(ip, 0, &lbase, NULL);
111 nvtruncbuf(vp, 0, nblksize, 0, 0);
114 * Delete the file on-media.
116 if ((ip->flags & HAMMER2_INODE_DELETING) == 0) {
117 atomic_set_int(&ip->flags, HAMMER2_INODE_DELETING);
118 hammer2_inode_delayed_sideq(ip);
120 hammer2_inode_unlock(ip);
123 * Recycle immediately if possible
125 vrecycle(vp);
126 } else {
127 hammer2_inode_unlock(ip);
129 return (0);
130 #endif
131 return (EOPNOTSUPP);
135 * Reclaim a vnode so that it can be reused; after the inode is
136 * disassociated, the filesystem must manage it alone.
138 static
140 hammer2_vop_reclaim(struct vop_reclaim_args *ap)
142 hammer2_inode_t *ip;
143 struct m_vnode *vp;
145 vp = ap->a_vp;
146 ip = VTOI(vp);
147 if (ip == NULL)
148 return(0);
151 * NOTE! We do not attempt to flush chains here, flushing is
152 * really fragile and could also deadlock.
154 vclrisdirty(vp);
157 * The inode lock is required to disconnect it.
159 hammer2_inode_lock(ip, 0);
160 vp->v_data = NULL;
161 ip->vp = NULL;
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.
168 if ((ip->flags & (HAMMER2_INODE_ISUNLINKED | HAMMER2_INODE_DELETING)) ==
169 HAMMER2_INODE_ISUNLINKED)
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);
177 hammer2_inode_unlock(ip);
180 * Modified inodes will already be on SIDEQ or SYNCQ, no further
181 * action is needed.
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).
188 hammer2_inode_drop(ip); /* vp ref */
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.
196 return (0);
200 hammer2_reclaim(struct m_vnode *vp)
202 struct vop_reclaim_args ap = {
203 .a_vp = vp,
206 return hammer2_vop_reclaim(&ap);
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.
215 static
217 hammer2_vop_fsync(struct vop_fsync_args *ap)
219 #if 0
220 hammer2_inode_t *ip;
221 struct m_vnode *vp;
222 int error1;
223 int error2;
225 vp = ap->a_vp;
226 ip = VTOI(vp);
227 error1 = 0;
229 hammer2_trans_init(ip->pmp, 0);
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.
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.
242 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
243 bio_track_wait(&vp->v_track_write, 0, 0);
246 * Flush any inode changes
248 hammer2_inode_lock(ip, 0);
249 if (ip->flags & (HAMMER2_INODE_RESIZED|HAMMER2_INODE_MODIFIED))
250 error1 = hammer2_inode_chain_sync(ip);
253 * Flush dirty chains related to the inode.
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.
259 error2 = hammer2_inode_chain_flush(ip, HAMMER2_XOP_INODE_STOP);
260 if (error2)
261 error1 = error2;
264 * We may be able to clear the vnode dirty flag.
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);
273 hammer2_inode_unlock(ip);
274 hammer2_trans_done(ip->pmp, 0);
276 return (error1);
277 #endif
278 return (EOPNOTSUPP);
282 * No lock needed, just handle ip->update
284 static
286 hammer2_vop_access(struct vop_access_args *ap)
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;
297 retry:
298 update = spin_access_start(&ip->cluster_spin);
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);*/
307 if (__predict_false(spin_access_end(&ip->cluster_spin, update)))
308 goto retry;
310 error = vop_helper_access(ap, uid, gid, mode, uflags);
312 return (error);
313 #endif
314 return (EOPNOTSUPP);
317 static
319 hammer2_vop_getattr(struct vop_getattr_args *ap)
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;
328 vp = ap->a_vp;
329 vap = ap->a_vap;
331 ip = VTOI(vp);
332 pmp = ip->pmp;
334 retry:
335 update = spin_access_start(&ip->cluster_spin);
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) {
355 * Can't really calculate directory use sans the files under
356 * it, just assume one block for now.
358 vap->va_bytes += HAMMER2_INODE_BYTES;
359 } else {
360 vap->va_bytes = hammer2_inode_data_count(ip);
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;
369 if (__predict_false(spin_access_end(&ip->cluster_spin, update)))
370 goto retry;
372 return (0);
373 #endif
374 return (EOPNOTSUPP);
377 static
379 hammer2_vop_getattr_lite(struct vop_getattr_lite_args *ap)
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;
388 vp = ap->a_vp;
389 lvap = ap->a_lvap;
391 ip = VTOI(vp);
392 pmp = ip->pmp;
394 retry:
395 update = spin_access_start(&ip->cluster_spin);
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
423 if (__predict_false(spin_access_end(&ip->cluster_spin, update)))
424 goto retry;
426 return (0);
427 #endif
428 return (EOPNOTSUPP);
431 static
433 hammer2_vop_setattr(struct vop_setattr_args *ap)
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;
443 vp = ap->a_vp;
444 vap = ap->a_vap;
445 hammer2_update_time(&ctime);
447 ip = VTOI(vp);
449 if (ip->pmp->ronly)
450 return (EROFS);
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).
457 if ((ip->pmp->flags & HAMMER2_PMPF_EMERG) == 0 &&
458 hammer2_vfs_enospace(ip, 0, ap->a_cred) > 1) {
459 return (ENOSPC);
462 hammer2_trans_init(ip->pmp, 0);
463 hammer2_inode_lock(ip, 0);
464 error = 0;
466 if (vap->va_flags != VNOVAL) {
467 uint32_t flags;
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;
482 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
483 error = 0;
484 goto done;
487 goto done;
489 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
490 error = EPERM;
491 goto done;
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;
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
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);
518 kflags |= NOTE_ATTRIB;
523 * Resize the file
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;
539 hammer2_inode_modify(ip);
540 ip->meta.mtime = ctime;
541 vclrflags(vp, VLASTWRITETS);
542 break;
543 default:
544 error = EINVAL;
545 goto done;
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;
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);
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;
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);
580 done:
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.
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.
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.
595 if (ip->flags & HAMMER2_INODE_RESIZED)
596 hammer2_inode_chain_sync(ip);
599 * Cleanup.
601 hammer2_inode_unlock(ip);
602 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ);
603 hammer2_knote(ip->vp, kflags);
605 return (error);
606 #endif
607 return (EOPNOTSUPP);
610 static
612 hammer2_vop_readdir(struct vop_readdir_args *ap)
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;
629 ip = VTOI(ap->a_vp);
630 uio = ap->a_uio;
631 saveoff = uio->uio_offset;
632 eofflag = 0;
633 error = 0;
636 * Setup cookies directory entry cookies if requested
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;
647 cookie_index = 0;
649 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
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.
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.
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;
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;
688 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
689 if (hammer2_debug & 0x0020)
690 kprintf("readdir: lkey %016jx\n", lkey);
691 if (error)
692 goto done;
694 xop = hammer2_xop_alloc(ip, 0);
695 xop->lkey = lkey;
696 hammer2_xop_start(&xop->head, &hammer2_readdir_desc);
698 for (;;) {
699 const hammer2_inode_data_t *ripdata;
700 const char *dname;
701 int dtype;
703 error = hammer2_xop_collect(&xop->head, 0);
704 error = hammer2_error_to_errno(error);
705 if (error) {
706 break;
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);
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;
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;
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);
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;
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;
780 } else {
781 if (cookies) {
782 *ap->a_ncookies = cookie_index;
783 *ap->a_cookies = cookies;
786 return (error);
787 #endif
788 return (EOPNOTSUPP);
792 * hammer2_vop_readlink { vp, uio, cred }
794 static
796 hammer2_vop_readlink(struct vop_readlink_args *ap)
798 struct m_vnode *vp;
799 hammer2_inode_t *ip;
800 int error;
802 vp = ap->a_vp;
803 if (vp->v_type != VLNK)
804 return (EINVAL);
805 ip = VTOI(vp);
807 error = hammer2_read_file(ip, ap->a_uio, 0);
808 return (error);
812 hammer2_readlink(struct m_vnode *vp, void *buf, size_t size)
814 assert(buf);
815 assert(size > 0);
816 assert(size <= HAMMER2_PBUFSIZE);
818 struct iovec iov = {
819 .iov_base = buf,
820 .iov_len = size,
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,
831 struct vop_readlink_args ap = {
832 .a_vp = vp,
833 .a_uio = &uio,
834 .a_cred = NULL,
837 return hammer2_vop_readlink(&ap);
840 static
842 hammer2_vop_read(struct vop_read_args *ap)
844 struct m_vnode *vp;
845 hammer2_inode_t *ip;
846 struct uio *uio;
847 int error;
848 int seqcount;
851 * Read operations supported on this vnode?
853 vp = ap->a_vp;
854 if (vp->v_type == VDIR)
855 return (EISDIR);
856 if (vp->v_type != VREG)
857 return (EINVAL);
860 * Misc
862 ip = VTOI(vp);
863 uio = ap->a_uio;
864 error = 0;
866 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
868 error = hammer2_read_file(ip, uio, seqcount);
869 return (error);
873 hammer2_read(struct m_vnode *vp, void *buf, size_t size, off_t offset)
875 assert(buf);
876 assert(size > 0);
877 assert(size <= HAMMER2_PBUFSIZE);
879 struct iovec iov = {
880 .iov_base = buf,
881 .iov_len = size,
883 struct uio uio = {
884 .uio_iov = &iov,
885 .uio_iovcnt = 1,
886 .uio_offset = offset,
887 .uio_resid = size,
888 .uio_segflg = UIO_USERSPACE,
889 .uio_rw = UIO_READ,
890 .uio_td = NULL,
892 struct vop_read_args ap = {
893 .a_vp = vp,
894 .a_uio = &uio,
895 .a_ioflag = 0,
896 .a_cred = NULL,
899 return hammer2_vop_read(&ap);
902 static
904 hammer2_vop_write(struct vop_write_args *ap)
906 hammer2_inode_t *ip;
907 //thread_t td;
908 struct m_vnode *vp;
909 struct uio *uio;
910 int error;
911 int seqcount;
912 int ioflag;
915 * Read operations supported on this vnode?
917 vp = ap->a_vp;
918 if (vp->v_type != VREG)
919 return (EINVAL);
922 * Misc
924 ip = VTOI(vp);
925 ioflag = ap->a_ioflag;
926 uio = ap->a_uio;
927 error = 0;
928 if (ip->pmp->ronly || (ip->pmp->flags & HAMMER2_PMPF_EMERG))
929 return (EROFS);
930 switch (hammer2_vfs_enospace(ip, uio->uio_resid, ap->a_cred)) {
931 case 2:
932 return (ENOSPC);
933 case 1:
934 ioflag |= IO_DIRECT; /* semi-synchronous */
935 /* fall through */
936 default:
937 break;
940 seqcount = ioflag >> IO_SEQSHIFT;
943 * Check resource limit
946 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
947 uio->uio_offset + uio->uio_resid >
948 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
949 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
950 return (EFBIG);
955 * The transaction interlocks against flush initiations
956 * (note: but will run concurrently with the actual flush).
958 * To avoid deadlocking against the VM system, we must flag any
959 * transaction related to the buffer cache or other direct
960 * VM page manipulation.
962 if (uio->uio_segflg == UIO_NOCOPY) {
963 assert(0); /* no UIO_NOCOPY in makefs */
964 hammer2_trans_init(ip->pmp, HAMMER2_TRANS_BUFCACHE);
965 } else {
966 hammer2_trans_init(ip->pmp, 0);
968 error = hammer2_write_file(ip, uio, ioflag, seqcount);
969 if (uio->uio_segflg == UIO_NOCOPY) {
970 assert(0); /* no UIO_NOCOPY in makefs */
971 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_BUFCACHE |
972 HAMMER2_TRANS_SIDEQ);
973 } else
974 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ);
976 return (error);
980 hammer2_write(struct m_vnode *vp, void *buf, size_t size, off_t offset)
982 assert(buf);
983 assert(size > 0);
984 assert(size <= HAMMER2_PBUFSIZE);
986 struct iovec iov = {
987 .iov_base = buf,
988 .iov_len = size,
990 struct uio uio = {
991 .uio_iov = &iov,
992 .uio_iovcnt = 1,
993 .uio_offset = offset,
994 .uio_resid = size,
995 .uio_segflg = UIO_USERSPACE,
996 .uio_rw = UIO_WRITE,
997 .uio_td = NULL,
999 struct vop_write_args ap = {
1000 .a_vp = vp,
1001 .a_uio = &uio,
1002 .a_ioflag = 0,
1003 .a_cred = NULL,
1006 return hammer2_vop_write(&ap);
1010 * Perform read operations on a file or symlink given an UNLOCKED
1011 * inode and uio.
1013 * The passed ip is not locked.
1015 static
1017 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
1019 hammer2_off_t size;
1020 struct m_buf *bp;
1021 int error;
1023 error = 0;
1026 * UIO read loop.
1028 * WARNING! Assumes that the kernel interlocks size changes at the
1029 * vnode level.
1031 hammer2_mtx_sh(&ip->lock);
1032 hammer2_mtx_sh(&ip->truncate_lock);
1033 size = ip->meta.size;
1034 hammer2_mtx_unlock(&ip->lock);
1036 while (uio->uio_resid > 0 && uio->uio_offset < size) {
1037 hammer2_key_t lbase;
1038 hammer2_key_t leof;
1039 int lblksize;
1040 int loff;
1041 int n;
1043 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
1044 &lbase, &leof);
1045 #if 0
1046 #if 1
1047 bp = NULL;
1048 error = cluster_readx(ip->vp, leof, lbase, lblksize,
1049 B_NOTMETA | B_KVABIO,
1050 uio->uio_resid,
1051 seqcount * MAXBSIZE,
1052 &bp);
1053 #else
1054 if (uio->uio_segflg == UIO_NOCOPY) {
1055 bp = getblk(ip->vp, lbase, lblksize,
1056 GETBLK_BHEAVY | GETBLK_KVABIO, 0);
1057 if (bp->b_flags & B_CACHE) {
1058 int i;
1059 int j = 0;
1060 if (bp->b_xio.xio_npages != 16)
1061 kprintf("NPAGES BAD\n");
1062 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
1063 vm_page_t m;
1064 m = bp->b_xio.xio_pages[i];
1065 if (m == NULL || m->valid == 0) {
1066 kprintf("bp %016jx %016jx pg %d inv",
1067 lbase, leof, i);
1068 if (m)
1069 kprintf("m->object %p/%p", m->object, ip->vp->v_object);
1070 kprintf("\n");
1071 j = 1;
1074 if (j)
1075 kprintf("b_flags %08x, b_error %d\n", bp->b_flags, bp->b_error);
1077 bqrelse(bp);
1079 error = bread_kvabio(ip->vp, lbase, lblksize, &bp);
1080 #endif
1081 #else
1082 bp = getblkx(ip->vp, lbase, lblksize,
1083 GETBLK_BHEAVY | GETBLK_KVABIO, 0);
1084 bp->b_cmd = BUF_CMD_READ;
1086 struct bio bio;
1087 bio.bio_buf = bp;
1088 bio.bio_offset = lbase;
1090 struct vop_strategy_args ap;
1091 ap.a_vp = ip->vp;
1092 ap.a_bio = &bio;
1094 error = hammer2_vop_strategy(&ap);
1095 assert(!error);
1096 #endif
1097 if (error) {
1098 brelse(bp);
1099 break;
1101 bkvasync(bp);
1102 loff = (int)(uio->uio_offset - lbase);
1103 n = lblksize - loff;
1104 if (n > uio->uio_resid)
1105 n = uio->uio_resid;
1106 if (n > size - uio->uio_offset)
1107 n = (int)(size - uio->uio_offset);
1108 //bp->b_flags |= B_AGE;
1109 uiomovebp(bp, bp->b_data + loff, n, uio);
1110 bqrelse(bp);
1112 hammer2_mtx_unlock(&ip->truncate_lock);
1114 return (error);
1118 * Write to the file represented by the inode via the logical buffer cache.
1119 * The inode may represent a regular file or a symlink.
1121 * The inode must not be locked.
1123 static
1125 hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
1126 int ioflag, int seqcount)
1128 hammer2_key_t old_eof;
1129 hammer2_key_t new_eof;
1130 struct m_buf *bp;
1131 int kflags;
1132 int error;
1133 int modified;
1136 * Setup if append
1138 * WARNING! Assumes that the kernel interlocks size changes at the
1139 * vnode level.
1141 hammer2_mtx_ex(&ip->lock);
1142 hammer2_mtx_sh(&ip->truncate_lock);
1143 if (ioflag & IO_APPEND)
1144 uio->uio_offset = ip->meta.size;
1145 old_eof = ip->meta.size;
1148 * Extend the file if necessary. If the write fails at some point
1149 * we will truncate it back down to cover as much as we were able
1150 * to write.
1152 * Doing this now makes it easier to calculate buffer sizes in
1153 * the loop.
1155 kflags = 0;
1156 error = 0;
1157 modified = 0;
1159 if (uio->uio_offset + uio->uio_resid > old_eof) {
1160 new_eof = uio->uio_offset + uio->uio_resid;
1161 modified = 1;
1162 hammer2_extend_file(ip, new_eof);
1163 kflags |= NOTE_EXTEND;
1164 } else {
1165 new_eof = old_eof;
1167 hammer2_mtx_unlock(&ip->lock);
1170 * UIO write loop
1172 while (uio->uio_resid > 0) {
1173 hammer2_key_t lbase;
1174 int trivial;
1175 int endofblk;
1176 int lblksize;
1177 int loff;
1178 int n;
1181 * Don't allow the buffer build to blow out the buffer
1182 * cache.
1184 if ((ioflag & IO_RECURSE) == 0)
1185 bwillwrite(HAMMER2_PBUFSIZE);
1188 * This nominally tells us how much we can cluster and
1189 * what the logical buffer size needs to be. Currently
1190 * we don't try to cluster the write and just handle one
1191 * block at a time.
1193 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
1194 &lbase, NULL);
1195 loff = (int)(uio->uio_offset - lbase);
1197 KKASSERT(lblksize <= MAXBSIZE);
1200 * Calculate bytes to copy this transfer and whether the
1201 * copy completely covers the buffer or not.
1203 trivial = 0;
1204 n = lblksize - loff;
1205 if (n > uio->uio_resid) {
1206 n = uio->uio_resid;
1207 if (loff == lbase && uio->uio_offset + n == new_eof)
1208 trivial = 1;
1209 endofblk = 0;
1210 } else {
1211 if (loff == 0)
1212 trivial = 1;
1213 endofblk = 1;
1215 if (lbase >= new_eof)
1216 trivial = 1;
1217 trivial = 1; /* force trivial for makefs */
1220 * Get the buffer
1222 if (uio->uio_segflg == UIO_NOCOPY) {
1223 assert(0); /* no UIO_NOCOPY in makefs */
1225 * Issuing a write with the same data backing the
1226 * buffer. Instantiate the buffer to collect the
1227 * backing vm pages, then read-in any missing bits.
1229 * This case is used by vop_stdputpages().
1231 bp = getblkx(ip->vp, lbase, lblksize,
1232 GETBLK_BHEAVY | GETBLK_KVABIO, 0);
1234 if ((bp->b_flags & B_CACHE) == 0) {
1235 bqrelse(bp);
1236 error = bread_kvabio(ip->vp, lbase,
1237 lblksize, &bp);
1240 } else if (trivial) {
1242 * Even though we are entirely overwriting the buffer
1243 * we may still have to zero it out to avoid a
1244 * mmap/write visibility issue.
1246 bp = getblkx(ip->vp, lbase, lblksize,
1247 GETBLK_BHEAVY | GETBLK_KVABIO, 0);
1249 if ((bp->b_flags & B_CACHE) == 0)
1250 vfs_bio_clrbuf(bp);
1252 } else {
1253 assert(0); /* no partial write in makefs */
1255 * Partial overwrite, read in any missing bits then
1256 * replace the portion being written.
1258 * (The strategy code will detect zero-fill physical
1259 * blocks for this case).
1261 error = bread_kvabio(ip->vp, lbase, lblksize, &bp);
1262 if (error == 0)
1263 bheavy(bp);
1266 if (error) {
1267 brelse(bp);
1268 break;
1272 * Ok, copy the data in
1274 bkvasync(bp);
1275 error = uiomovebp(bp, bp->b_data + loff, n, uio);
1276 kflags |= NOTE_WRITE;
1277 modified = 1;
1278 if (error) {
1279 brelse(bp);
1280 break;
1284 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
1285 * with IO_SYNC or IO_ASYNC set. These writes
1286 * must be handled as the pageout daemon expects.
1288 * NOTE! H2 relies on cluster_write() here because it
1289 * cannot preallocate disk blocks at the logical
1290 * level due to not knowing what the compression
1291 * size will be at this time.
1293 * We must use cluster_write() here and we depend
1294 * on the write-behind feature to flush buffers
1295 * appropriately. If we let the buffer daemons do
1296 * it the block allocations will be all over the
1297 * map.
1299 if (1) {
1300 bp->b_cmd = BUF_CMD_WRITE;
1302 struct bio bio;
1303 bio.bio_buf = bp;
1304 bio.bio_offset = lbase;
1306 struct vop_strategy_args ap;
1307 ap.a_vp = ip->vp;
1308 ap.a_bio = &bio;
1310 error = hammer2_vop_strategy(&ap);
1311 assert(!error);
1313 brelse(bp);
1314 } else if (ioflag & IO_SYNC) {
1315 assert(0);
1316 bwrite(bp);
1317 } else if ((ioflag & IO_DIRECT) && endofblk) {
1318 assert(0);
1319 bawrite(bp);
1320 } else if (ioflag & IO_ASYNC) {
1321 assert(0);
1322 bawrite(bp);
1323 } else if (0 /*ip->vp->v_mount->mnt_flag & MNT_NOCLUSTERW*/) {
1324 assert(0);
1325 bdwrite(bp);
1326 } else {
1327 assert(0);
1328 #if 0
1329 #if 1
1330 bp->b_flags |= B_CLUSTEROK;
1331 cluster_write(bp, new_eof, lblksize, seqcount);
1332 #else
1333 bp->b_flags |= B_CLUSTEROK;
1334 bdwrite(bp);
1335 #endif
1336 #endif
1341 * Cleanup. If we extended the file EOF but failed to write through
1342 * the entire write is a failure and we have to back-up.
1344 if (error && new_eof != old_eof) {
1345 hammer2_mtx_unlock(&ip->truncate_lock);
1346 hammer2_mtx_ex(&ip->lock); /* note lock order */
1347 hammer2_mtx_ex(&ip->truncate_lock); /* note lock order */
1348 hammer2_truncate_file(ip, old_eof);
1349 if (ip->flags & HAMMER2_INODE_MODIFIED)
1350 hammer2_inode_chain_sync(ip);
1351 hammer2_mtx_unlock(&ip->lock);
1352 } else if (modified) {
1353 struct m_vnode *vp = ip->vp;
1355 hammer2_mtx_ex(&ip->lock);
1356 hammer2_inode_modify(ip);
1357 if (uio->uio_segflg == UIO_NOCOPY) {
1358 assert(0); /* no UIO_NOCOPY in makefs */
1360 if (vp->v_flag & VLASTWRITETS) {
1361 ip->meta.mtime =
1362 (unsigned long)vp->v_lastwrite_ts.tv_sec *
1363 1000000 +
1364 vp->v_lastwrite_ts.tv_nsec / 1000;
1367 } else {
1368 hammer2_update_time(&ip->meta.mtime, true);
1369 vclrflags(vp, VLASTWRITETS);
1372 #if 0
1374 * REMOVED - handled by hammer2_extend_file(). Do not issue
1375 * a chain_sync() outside of a sync/fsync except for DIRECTDATA
1376 * state changes.
1378 * Under normal conditions we only issue a chain_sync if
1379 * the inode's DIRECTDATA state changed.
1381 if (ip->flags & HAMMER2_INODE_RESIZED)
1382 hammer2_inode_chain_sync(ip);
1383 #endif
1384 hammer2_mtx_unlock(&ip->lock);
1385 hammer2_knote(ip->vp, kflags);
1387 hammer2_trans_assert_strategy(ip->pmp);
1388 hammer2_mtx_unlock(&ip->truncate_lock);
1390 return error;
1394 * Truncate the size of a file. The inode must be locked.
1396 * We must unconditionally set HAMMER2_INODE_RESIZED to properly
1397 * ensure that any on-media data beyond the new file EOF has been destroyed.
1399 * WARNING: nvtruncbuf() can only be safely called without the inode lock
1400 * held due to the way our write thread works. If the truncation
1401 * occurs in the middle of a buffer, nvtruncbuf() is responsible
1402 * for dirtying that buffer and zeroing out trailing bytes.
1404 * WARNING! Assumes that the kernel interlocks size changes at the
1405 * vnode level.
1407 * WARNING! Caller assumes responsibility for removing dead blocks
1408 * if INODE_RESIZED is set.
1410 static
1411 void
1412 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1414 hammer2_key_t lbase;
1415 int nblksize;
1417 hammer2_mtx_unlock(&ip->lock);
1418 if (ip->vp) {
1419 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1420 nvtruncbuf(ip->vp, nsize,
1421 nblksize, (int)nsize & (nblksize - 1),
1424 hammer2_mtx_ex(&ip->lock);
1425 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1426 ip->osize = ip->meta.size;
1427 ip->meta.size = nsize;
1428 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1429 hammer2_inode_modify(ip);
1433 * Extend the size of a file. The inode must be locked.
1435 * Even though the file size is changing, we do not have to set the
1436 * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES
1437 * boundary. When this occurs a hammer2_inode_chain_sync() is required
1438 * to prepare the inode cluster's indirect block table, otherwise
1439 * async execution of the strategy code will implode on us.
1441 * WARNING! Assumes that the kernel interlocks size changes at the
1442 * vnode level.
1444 * WARNING! Caller assumes responsibility for transitioning out
1445 * of the inode DIRECTDATA mode if INODE_RESIZED is set.
1447 static
1448 void
1449 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1451 hammer2_key_t lbase;
1452 hammer2_key_t osize;
1453 int oblksize;
1454 int nblksize;
1455 int error;
1457 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1458 hammer2_inode_modify(ip);
1459 osize = ip->meta.size;
1460 ip->osize = osize;
1461 ip->meta.size = nsize;
1464 * We must issue a chain_sync() when the DIRECTDATA state changes
1465 * to prevent confusion between the flush code and the in-memory
1466 * state. This is not perfect because we are doing it outside of
1467 * a sync/fsync operation, so it might not be fully synchronized
1468 * with the meta-data topology flush.
1470 * We must retain and re-dirty the buffer cache buffer containing
1471 * the direct data so it can be written to a real block. It should
1472 * not be possible for a bread error to occur since the original data
1473 * is extracted from the inode structure directly.
1475 if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES) {
1476 if (osize) {
1477 assert(0); /* no such transition in makefs */
1478 struct m_buf *bp;
1480 oblksize = hammer2_calc_logical(ip, 0, NULL, NULL);
1481 error = bread_kvabio(ip->vp, 0, oblksize, &bp);
1482 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1483 hammer2_inode_chain_sync(ip);
1484 if (error == 0) {
1485 bheavy(bp);
1486 bdwrite(bp);
1487 } else {
1488 brelse(bp);
1490 } else {
1491 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1492 hammer2_inode_chain_sync(ip);
1495 hammer2_mtx_unlock(&ip->lock);
1496 if (ip->vp) {
1497 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1498 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1499 nvextendbuf(ip->vp,
1500 osize, nsize,
1501 oblksize, nblksize,
1502 -1, -1, 0);
1504 hammer2_mtx_ex(&ip->lock);
1507 static
1509 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1511 hammer2_xop_nresolve_t *xop;
1512 hammer2_inode_t *ip;
1513 hammer2_inode_t *dip;
1514 struct namecache *ncp;
1515 struct m_vnode *vp;
1516 int error;
1518 dip = VTOI(ap->a_dvp);
1519 xop = hammer2_xop_alloc(dip, 0);
1521 ncp = ap->a_nch->ncp;
1522 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1525 * Note: In DragonFly the kernel handles '.' and '..'.
1527 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1528 hammer2_xop_start(&xop->head, &hammer2_nresolve_desc);
1530 error = hammer2_xop_collect(&xop->head, 0);
1531 error = hammer2_error_to_errno(error);
1532 if (error) {
1533 ip = NULL;
1534 } else {
1535 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1);
1537 hammer2_inode_unlock(dip);
1540 * Acquire the related vnode
1542 * NOTE: For error processing, only ENOENT resolves the namecache
1543 * entry to NULL, otherwise we just return the error and
1544 * leave the namecache unresolved.
1546 * WARNING: inode structure is locked exclusively via inode_get
1547 * but chain was locked shared. inode_unlock()
1548 * will handle it properly.
1550 if (ip) {
1551 vp = hammer2_igetv(ip, &error); /* error set to UNIX error */
1552 if (error == 0) {
1553 vn_unlock(vp);
1554 cache_setvp(ap->a_nch, vp);
1555 *ap->a_vpp = vp;
1556 } else if (error == ENOENT) {
1557 cache_setvp(ap->a_nch, NULL);
1559 hammer2_inode_unlock(ip);
1562 * The vp should not be released until after we've disposed
1563 * of our locks, because it might cause vop_inactive() to
1564 * be called.
1566 if (vp)
1567 vrele(vp);
1568 } else {
1569 error = ENOENT;
1570 cache_setvp(ap->a_nch, NULL);
1572 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1574 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1575 ("resolve error %d/%p ap %p\n",
1576 error, ap->a_nch->ncp->nc_vp, ap));
1579 return error;
1583 hammer2_nresolve(struct m_vnode *dvp, struct m_vnode **vpp, char *name, int nlen)
1585 *vpp = NULL;
1586 struct namecache nc = {
1587 .nc_name = name,
1588 .nc_nlen = nlen,
1590 struct nchandle nch = {
1591 .ncp = &nc,
1593 struct vop_nresolve_args ap = {
1594 .a_nch = &nch,
1595 .a_dvp = dvp,
1596 .a_vpp = vpp,
1599 return hammer2_vop_nresolve(&ap);
1602 static
1604 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1606 #if 0
1607 hammer2_inode_t *dip;
1608 hammer2_tid_t inum;
1609 int error;
1611 dip = VTOI(ap->a_dvp);
1612 inum = dip->meta.iparent;
1613 *ap->a_vpp = NULL;
1615 if (inum) {
1616 error = hammer2_vfs_vget(ap->a_dvp->v_mount, NULL,
1617 inum, ap->a_vpp);
1618 } else {
1619 error = ENOENT;
1621 return error;
1622 #endif
1623 return (EOPNOTSUPP);
1626 static
1628 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1630 hammer2_inode_t *dip;
1631 hammer2_inode_t *nip;
1632 struct namecache *ncp;
1633 const char *name;
1634 size_t name_len;
1635 hammer2_tid_t inum;
1636 int error;
1638 dip = VTOI(ap->a_dvp);
1639 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG))
1640 return (EROFS);
1641 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1642 return (ENOSPC);
1644 ncp = ap->a_nch->ncp;
1645 name = ncp->nc_name;
1646 name_len = ncp->nc_nlen;
1648 hammer2_trans_init(dip->pmp, 0);
1650 inum = hammer2_trans_newinum(dip->pmp);
1653 * Create the actual inode as a hidden file in the iroot, then
1654 * create the directory entry. The creation of the actual inode
1655 * sets its nlinks to 1 which is the value we desire.
1657 * dip must be locked before nip to avoid deadlock.
1659 hammer2_inode_lock(dip, 0);
1660 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
1661 inum, &error);
1662 if (error) {
1663 error = hammer2_error_to_errno(error);
1664 } else {
1665 error = hammer2_dirent_create(dip, name, name_len,
1666 nip->meta.inum, nip->meta.type);
1667 /* returns UNIX error code */
1669 if (error) {
1670 if (nip) {
1671 hammer2_inode_unlink_finisher(nip, NULL);
1672 hammer2_inode_unlock(nip);
1673 nip = NULL;
1675 *ap->a_vpp = NULL;
1676 } else {
1678 * inode_depend() must occur before the igetv() because
1679 * the igetv() can temporarily release the inode lock.
1681 hammer2_inode_depend(dip, nip); /* before igetv */
1682 *ap->a_vpp = hammer2_igetv(nip, &error);
1683 hammer2_inode_unlock(nip);
1687 * Update dip's mtime
1689 * We can use a shared inode lock and allow the meta.mtime update
1690 * SMP race. hammer2_inode_modify() is MPSAFE w/a shared lock.
1692 if (error == 0) {
1693 uint64_t mtime;
1695 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1696 hammer2_update_time(&mtime, true);
1697 hammer2_inode_modify(dip);
1698 dip->meta.mtime = mtime;
1699 /*hammer2_inode_unlock(dip);*/
1701 hammer2_inode_unlock(dip);
1703 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1705 if (error == 0) {
1706 cache_setunresolved(ap->a_nch);
1707 cache_setvp(ap->a_nch, *ap->a_vpp);
1708 hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1710 return error;
1714 hammer2_nmkdir(struct m_vnode *dvp, struct m_vnode **vpp, char *name, int nlen,
1715 mode_t mode)
1717 struct namecache nc = {
1718 .nc_name = name,
1719 .nc_nlen = nlen,
1721 struct nchandle nch = {
1722 .ncp = &nc,
1724 uid_t va_uid = VNOVAL; //getuid();
1725 uid_t va_gid = VNOVAL; //getgid();
1726 struct vattr va = {
1727 .va_type = VDIR,
1728 .va_mode = mode & ~S_IFMT,
1729 .va_uid = va_uid,
1730 .va_gid = va_gid,
1732 struct vop_nmkdir_args ap = {
1733 .a_nch = &nch,
1734 .a_dvp = dvp,
1735 .a_vpp = vpp,
1736 .a_vap = &va,
1739 return hammer2_vop_nmkdir(&ap);
1742 static
1744 hammer2_vop_open(struct vop_open_args *ap)
1746 #if 0
1747 return vop_stdopen(ap);
1748 #endif
1749 return (EOPNOTSUPP);
1753 * hammer2_vop_advlock { vp, id, op, fl, flags }
1755 static
1757 hammer2_vop_advlock(struct vop_advlock_args *ap)
1759 #if 0
1760 hammer2_inode_t *ip = VTOI(ap->a_vp);
1761 hammer2_off_t size;
1763 size = ip->meta.size;
1764 return (lf_advlock(ap, &ip->advlock, size));
1765 #endif
1766 return (EOPNOTSUPP);
1769 static
1771 hammer2_vop_close(struct vop_close_args *ap)
1773 #if 0
1774 return vop_stdclose(ap);
1775 #endif
1776 return (EOPNOTSUPP);
1780 * hammer2_vop_nlink { nch, dvp, vp, cred }
1782 * Create a hardlink from (vp) to {dvp, nch}.
1784 static
1786 hammer2_vop_nlink(struct vop_nlink_args *ap)
1788 hammer2_inode_t *tdip; /* target directory to create link in */
1789 hammer2_inode_t *ip; /* inode we are hardlinking to */
1790 struct namecache *ncp;
1791 const char *name;
1792 size_t name_len;
1793 int error;
1794 uint64_t cmtime;
1796 /* We know it's the same in makefs */
1798 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1799 return(EXDEV);
1802 tdip = VTOI(ap->a_dvp);
1803 if (tdip->pmp->ronly || (tdip->pmp->flags & HAMMER2_PMPF_EMERG))
1804 return (EROFS);
1805 if (hammer2_vfs_enospace(tdip, 0, ap->a_cred) > 1)
1806 return (ENOSPC);
1808 ncp = ap->a_nch->ncp;
1809 name = ncp->nc_name;
1810 name_len = ncp->nc_nlen;
1813 * ip represents the file being hardlinked. The file could be a
1814 * normal file or a hardlink target if it has already been hardlinked.
1815 * (with the new semantics, it will almost always be a hardlink
1816 * target).
1818 * Bump nlinks and potentially also create or move the hardlink
1819 * target in the parent directory common to (ip) and (tdip). The
1820 * consolidation code can modify ip->cluster. The returned cluster
1821 * is locked.
1823 ip = VTOI(ap->a_vp);
1824 KASSERT(ip->pmp, ("ip->pmp is NULL %p %p", ip, ip->pmp));
1825 hammer2_trans_init(ip->pmp, 0);
1828 * Target should be an indexed inode or there's no way we will ever
1829 * be able to find it!
1831 KKASSERT((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0);
1833 error = 0;
1836 * Can return NULL and error == EXDEV if the common parent
1837 * crosses a directory with the xlink flag set.
1839 hammer2_inode_lock4(tdip, ip, NULL, NULL);
1841 hammer2_update_time(&cmtime, true);
1844 * Create the directory entry and bump nlinks.
1845 * Also update ip's ctime.
1847 if (error == 0) {
1848 error = hammer2_dirent_create(tdip, name, name_len,
1849 ip->meta.inum, ip->meta.type);
1850 hammer2_inode_modify(ip);
1851 ++ip->meta.nlinks;
1852 ip->meta.ctime = cmtime;
1854 if (error == 0) {
1856 * Update dip's [cm]time
1858 hammer2_inode_modify(tdip);
1859 tdip->meta.mtime = cmtime;
1860 tdip->meta.ctime = cmtime;
1862 cache_setunresolved(ap->a_nch);
1863 cache_setvp(ap->a_nch, ap->a_vp);
1865 hammer2_inode_unlock(ip);
1866 hammer2_inode_unlock(tdip);
1868 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ);
1869 hammer2_knote(ap->a_vp, NOTE_LINK);
1870 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1872 return error;
1876 hammer2_nlink(struct m_vnode *dvp, struct m_vnode *vp, char *name, int nlen)
1878 struct namecache nc = {
1879 .nc_name = name,
1880 .nc_nlen = nlen,
1882 struct nchandle nch = {
1883 .ncp = &nc,
1885 struct vop_nlink_args ap = {
1886 .a_nch = &nch,
1887 .a_dvp = dvp,
1888 .a_vp = vp,
1891 return hammer2_vop_nlink(&ap);
1895 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1897 * The operating system has already ensured that the directory entry
1898 * does not exist and done all appropriate namespace locking.
1900 static
1902 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1904 hammer2_inode_t *dip;
1905 hammer2_inode_t *nip;
1906 struct namecache *ncp;
1907 const char *name;
1908 size_t name_len;
1909 hammer2_tid_t inum;
1910 int error;
1912 dip = VTOI(ap->a_dvp);
1913 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG))
1914 return (EROFS);
1915 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1916 return (ENOSPC);
1918 ncp = ap->a_nch->ncp;
1919 name = ncp->nc_name;
1920 name_len = ncp->nc_nlen;
1921 hammer2_trans_init(dip->pmp, 0);
1923 inum = hammer2_trans_newinum(dip->pmp);
1926 * Create the actual inode as a hidden file in the iroot, then
1927 * create the directory entry. The creation of the actual inode
1928 * sets its nlinks to 1 which is the value we desire.
1930 * dip must be locked before nip to avoid deadlock.
1932 hammer2_inode_lock(dip, 0);
1933 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
1934 inum, &error);
1936 if (error) {
1937 error = hammer2_error_to_errno(error);
1938 } else {
1939 error = hammer2_dirent_create(dip, name, name_len,
1940 nip->meta.inum, nip->meta.type);
1942 if (error) {
1943 if (nip) {
1944 hammer2_inode_unlink_finisher(nip, NULL);
1945 hammer2_inode_unlock(nip);
1946 nip = NULL;
1948 *ap->a_vpp = NULL;
1949 } else {
1950 hammer2_inode_depend(dip, nip); /* before igetv */
1951 *ap->a_vpp = hammer2_igetv(nip, &error);
1952 hammer2_inode_unlock(nip);
1956 * Update dip's mtime
1958 if (error == 0) {
1959 uint64_t mtime;
1961 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1962 hammer2_update_time(&mtime, true);
1963 hammer2_inode_modify(dip);
1964 dip->meta.mtime = mtime;
1965 /*hammer2_inode_unlock(dip);*/
1967 hammer2_inode_unlock(dip);
1969 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1971 if (error == 0) {
1972 cache_setunresolved(ap->a_nch);
1973 cache_setvp(ap->a_nch, *ap->a_vpp);
1974 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1976 return error;
1980 hammer2_ncreate(struct m_vnode *dvp, struct m_vnode **vpp, char *name, int nlen,
1981 mode_t mode)
1983 struct namecache nc = {
1984 .nc_name = name,
1985 .nc_nlen = nlen,
1987 struct nchandle nch = {
1988 .ncp = &nc,
1990 uid_t va_uid = VNOVAL; //getuid();
1991 uid_t va_gid = VNOVAL; //getgid();
1992 struct vattr va = {
1993 .va_type = VREG,
1994 .va_mode = mode & ~S_IFMT,
1995 .va_uid = va_uid,
1996 .va_gid = va_gid,
1998 struct vop_ncreate_args ap = {
1999 .a_nch = &nch,
2000 .a_dvp = dvp,
2001 .a_vpp = vpp,
2002 .a_vap = &va,
2005 return hammer2_vop_ncreate(&ap);
2009 * Make a device node (typically a fifo)
2011 static
2013 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
2015 hammer2_inode_t *dip;
2016 hammer2_inode_t *nip;
2017 struct namecache *ncp;
2018 const char *name;
2019 size_t name_len;
2020 hammer2_tid_t inum;
2021 int error;
2023 dip = VTOI(ap->a_dvp);
2024 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG))
2025 return (EROFS);
2026 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
2027 return (ENOSPC);
2029 ncp = ap->a_nch->ncp;
2030 name = ncp->nc_name;
2031 name_len = ncp->nc_nlen;
2032 hammer2_trans_init(dip->pmp, 0);
2035 * Create the device inode and then create the directory entry.
2037 * dip must be locked before nip to avoid deadlock.
2039 inum = hammer2_trans_newinum(dip->pmp);
2041 hammer2_inode_lock(dip, 0);
2042 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
2043 inum, &error);
2044 if (error == 0) {
2045 error = hammer2_dirent_create(dip, name, name_len,
2046 nip->meta.inum, nip->meta.type);
2048 if (error) {
2049 if (nip) {
2050 hammer2_inode_unlink_finisher(nip, NULL);
2051 hammer2_inode_unlock(nip);
2052 nip = NULL;
2054 *ap->a_vpp = NULL;
2055 } else {
2056 hammer2_inode_depend(dip, nip); /* before igetv */
2057 *ap->a_vpp = hammer2_igetv(nip, &error);
2058 hammer2_inode_unlock(nip);
2062 * Update dip's mtime
2064 if (error == 0) {
2065 uint64_t mtime;
2067 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
2068 hammer2_update_time(&mtime, true);
2069 hammer2_inode_modify(dip);
2070 dip->meta.mtime = mtime;
2071 /*hammer2_inode_unlock(dip);*/
2073 hammer2_inode_unlock(dip);
2075 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
2077 if (error == 0) {
2078 cache_setunresolved(ap->a_nch);
2079 cache_setvp(ap->a_nch, *ap->a_vpp);
2080 hammer2_knote(ap->a_dvp, NOTE_WRITE);
2082 return error;
2086 hammer2_nmknod(struct m_vnode *dvp, struct m_vnode **vpp, char *name, int nlen,
2087 int type, mode_t mode)
2089 struct namecache nc = {
2090 .nc_name = name,
2091 .nc_nlen = nlen,
2093 struct nchandle nch = {
2094 .ncp = &nc,
2096 uid_t va_uid = VNOVAL; //getuid();
2097 uid_t va_gid = VNOVAL; //getgid();
2098 struct vattr va = {
2099 .va_type = type,
2100 .va_mode = mode & ~S_IFMT,
2101 .va_uid = va_uid,
2102 .va_gid = va_gid,
2104 struct vop_nmknod_args ap = {
2105 .a_nch = &nch,
2106 .a_dvp = dvp,
2107 .a_vpp = vpp,
2108 .a_vap = &va,
2111 return hammer2_vop_nmknod(&ap);
2115 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2117 static
2119 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
2121 hammer2_inode_t *dip;
2122 hammer2_inode_t *nip;
2123 struct namecache *ncp;
2124 const char *name;
2125 size_t name_len;
2126 hammer2_tid_t inum;
2127 int error;
2129 dip = VTOI(ap->a_dvp);
2130 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG))
2131 return (EROFS);
2132 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
2133 return (ENOSPC);
2135 ncp = ap->a_nch->ncp;
2136 name = ncp->nc_name;
2137 name_len = ncp->nc_nlen;
2138 hammer2_trans_init(dip->pmp, 0);
2140 ap->a_vap->va_type = VLNK; /* enforce type */
2143 * Create the softlink as an inode and then create the directory
2144 * entry.
2146 * dip must be locked before nip to avoid deadlock.
2148 inum = hammer2_trans_newinum(dip->pmp);
2150 hammer2_inode_lock(dip, 0);
2151 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
2152 inum, &error);
2153 if (error == 0) {
2154 error = hammer2_dirent_create(dip, name, name_len,
2155 nip->meta.inum, nip->meta.type);
2157 if (error) {
2158 if (nip) {
2159 hammer2_inode_unlink_finisher(nip, NULL);
2160 hammer2_inode_unlock(nip);
2161 nip = NULL;
2163 *ap->a_vpp = NULL;
2164 hammer2_inode_unlock(dip);
2165 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
2166 return error;
2168 hammer2_inode_depend(dip, nip); /* before igetv */
2169 *ap->a_vpp = hammer2_igetv(nip, &error);
2172 * Build the softlink (~like file data) and finalize the namecache.
2174 if (error == 0) {
2175 size_t bytes;
2176 struct uio auio;
2177 struct iovec aiov;
2179 bytes = strlen(ap->a_target);
2181 hammer2_inode_unlock(nip);
2182 bzero(&auio, sizeof(auio));
2183 bzero(&aiov, sizeof(aiov));
2184 auio.uio_iov = &aiov;
2185 auio.uio_segflg = UIO_SYSSPACE;
2186 auio.uio_rw = UIO_WRITE;
2187 auio.uio_resid = bytes;
2188 auio.uio_iovcnt = 1;
2189 auio.uio_td = curthread;
2190 aiov.iov_base = ap->a_target;
2191 aiov.iov_len = bytes;
2192 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
2193 /* XXX handle error */
2194 error = 0;
2195 } else {
2196 hammer2_inode_unlock(nip);
2200 * Update dip's mtime
2202 if (error == 0) {
2203 uint64_t mtime;
2205 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
2206 hammer2_update_time(&mtime, true);
2207 hammer2_inode_modify(dip);
2208 dip->meta.mtime = mtime;
2209 /*hammer2_inode_unlock(dip);*/
2211 hammer2_inode_unlock(dip);
2213 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
2216 * Finalize namecache
2218 if (error == 0) {
2219 cache_setunresolved(ap->a_nch);
2220 cache_setvp(ap->a_nch, *ap->a_vpp);
2221 hammer2_knote(ap->a_dvp, NOTE_WRITE);
2223 return error;
2227 hammer2_nsymlink(struct m_vnode *dvp, struct m_vnode **vpp, char *name, int nlen,
2228 char *target, mode_t mode)
2230 struct namecache nc = {
2231 .nc_name = name,
2232 .nc_nlen = nlen,
2234 struct nchandle nch = {
2235 .ncp = &nc,
2237 uid_t va_uid = VNOVAL; //getuid();
2238 uid_t va_gid = VNOVAL; //getgid();
2239 struct vattr va = {
2240 .va_type = VDIR,
2241 .va_mode = mode & ~S_IFMT,
2242 .va_uid = va_uid,
2243 .va_gid = va_gid,
2245 struct vop_nsymlink_args ap = {
2246 .a_nch = &nch,
2247 .a_dvp = dvp,
2248 .a_vpp = vpp,
2249 .a_vap = &va,
2250 .a_target = target,
2253 return hammer2_vop_nsymlink(&ap);
2257 * hammer2_vop_nremove { nch, dvp, cred }
2259 static
2261 hammer2_vop_nremove(struct vop_nremove_args *ap)
2263 #if 0
2264 hammer2_xop_unlink_t *xop;
2265 hammer2_inode_t *dip;
2266 hammer2_inode_t *ip;
2267 struct m_vnode *vprecycle;
2268 struct namecache *ncp;
2269 int error;
2271 dip = VTOI(ap->a_dvp);
2272 if (dip->pmp->ronly)
2273 return (EROFS);
2274 #if 0
2275 /* allow removals, except user to also bulkfree */
2276 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
2277 return (ENOSPC);
2278 #endif
2280 ncp = ap->a_nch->ncp;
2282 if (hammer2_debug_inode && dip->meta.inum == hammer2_debug_inode) {
2283 kprintf("hammer2: attempt to delete inside debug inode: %s\n",
2284 ncp->nc_name);
2285 while (hammer2_debug_inode &&
2286 dip->meta.inum == hammer2_debug_inode) {
2287 tsleep(&hammer2_debug_inode, 0, "h2debug", hz*5);
2291 hammer2_trans_init(dip->pmp, 0);
2292 hammer2_inode_lock(dip, 0);
2295 * The unlink XOP unlinks the path from the directory and
2296 * locates and returns the cluster associated with the real inode.
2297 * We have to handle nlinks here on the frontend.
2299 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
2300 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
2302 xop->isdir = 0;
2303 xop->dopermanent = 0;
2304 hammer2_xop_start(&xop->head, &hammer2_unlink_desc);
2307 * Collect the real inode and adjust nlinks, destroy the real
2308 * inode if nlinks transitions to 0 and it was the real inode
2309 * (else it has already been removed).
2311 error = hammer2_xop_collect(&xop->head, 0);
2312 error = hammer2_error_to_errno(error);
2313 vprecycle = NULL;
2315 if (error == 0) {
2316 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1);
2317 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2318 if (ip) {
2319 if (hammer2_debug_inode &&
2320 ip->meta.inum == hammer2_debug_inode) {
2321 kprintf("hammer2: attempt to delete debug "
2322 "inode!\n");
2323 while (hammer2_debug_inode &&
2324 ip->meta.inum == hammer2_debug_inode) {
2325 tsleep(&hammer2_debug_inode, 0,
2326 "h2debug", hz*5);
2329 hammer2_inode_unlink_finisher(ip, &vprecycle);
2330 hammer2_inode_depend(dip, ip); /* after modified */
2331 hammer2_inode_unlock(ip);
2333 } else {
2334 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2338 * Update dip's mtime
2340 if (error == 0) {
2341 uint64_t mtime;
2343 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
2344 hammer2_update_time(&mtime);
2345 hammer2_inode_modify(dip);
2346 dip->meta.mtime = mtime;
2347 /*hammer2_inode_unlock(dip);*/
2349 hammer2_inode_unlock(dip);
2351 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
2352 if (error == 0) {
2353 cache_unlink(ap->a_nch);
2354 hammer2_knote(ap->a_dvp, NOTE_WRITE);
2356 if (vprecycle)
2357 hammer2_inode_vprecycle(vprecycle);
2359 return (error);
2360 #endif
2361 return (EOPNOTSUPP);
2365 * hammer2_vop_nrmdir { nch, dvp, cred }
2367 static
2369 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
2371 #if 0
2372 hammer2_xop_unlink_t *xop;
2373 hammer2_inode_t *dip;
2374 hammer2_inode_t *ip;
2375 struct namecache *ncp;
2376 struct m_vnode *vprecycle;
2377 int error;
2379 dip = VTOI(ap->a_dvp);
2380 if (dip->pmp->ronly)
2381 return (EROFS);
2382 #if 0
2383 /* allow removals, except user to also bulkfree */
2384 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
2385 return (ENOSPC);
2386 #endif
2388 hammer2_trans_init(dip->pmp, 0);
2389 hammer2_inode_lock(dip, 0);
2391 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
2393 ncp = ap->a_nch->ncp;
2394 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
2395 xop->isdir = 1;
2396 xop->dopermanent = 0;
2397 hammer2_xop_start(&xop->head, &hammer2_unlink_desc);
2400 * Collect the real inode and adjust nlinks, destroy the real
2401 * inode if nlinks transitions to 0 and it was the real inode
2402 * (else it has already been removed).
2404 error = hammer2_xop_collect(&xop->head, 0);
2405 error = hammer2_error_to_errno(error);
2406 vprecycle = NULL;
2408 if (error == 0) {
2409 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1);
2410 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2411 if (ip) {
2412 hammer2_inode_unlink_finisher(ip, &vprecycle);
2413 hammer2_inode_depend(dip, ip); /* after modified */
2414 hammer2_inode_unlock(ip);
2416 } else {
2417 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2421 * Update dip's mtime
2423 if (error == 0) {
2424 uint64_t mtime;
2426 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
2427 hammer2_update_time(&mtime);
2428 hammer2_inode_modify(dip);
2429 dip->meta.mtime = mtime;
2430 /*hammer2_inode_unlock(dip);*/
2432 hammer2_inode_unlock(dip);
2434 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
2435 if (error == 0) {
2436 cache_unlink(ap->a_nch);
2437 hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2439 if (vprecycle)
2440 hammer2_inode_vprecycle(vprecycle);
2441 return (error);
2442 #endif
2443 return (EOPNOTSUPP);
2447 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
2449 static
2451 hammer2_vop_nrename(struct vop_nrename_args *ap)
2453 #if 0
2454 struct namecache *fncp;
2455 struct namecache *tncp;
2456 hammer2_inode_t *fdip; /* source directory */
2457 hammer2_inode_t *tdip; /* target directory */
2458 hammer2_inode_t *ip; /* file being renamed */
2459 hammer2_inode_t *tip; /* replaced target during rename or NULL */
2460 struct m_vnode *vprecycle;
2461 const char *fname;
2462 size_t fname_len;
2463 const char *tname;
2464 size_t tname_len;
2465 int error;
2466 int update_tdip;
2467 int update_fdip;
2468 hammer2_key_t tlhc;
2470 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
2471 return(EXDEV);
2472 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
2473 return(EXDEV);
2475 fdip = VTOI(ap->a_fdvp); /* source directory */
2476 tdip = VTOI(ap->a_tdvp); /* target directory */
2478 if (fdip->pmp->ronly || (fdip->pmp->flags & HAMMER2_PMPF_EMERG))
2479 return (EROFS);
2480 if (hammer2_vfs_enospace(fdip, 0, ap->a_cred) > 1)
2481 return (ENOSPC);
2483 fncp = ap->a_fnch->ncp; /* entry name in source */
2484 fname = fncp->nc_name;
2485 fname_len = fncp->nc_nlen;
2487 tncp = ap->a_tnch->ncp; /* entry name in target */
2488 tname = tncp->nc_name;
2489 tname_len = tncp->nc_nlen;
2491 hammer2_trans_init(tdip->pmp, 0);
2493 update_tdip = 0;
2494 update_fdip = 0;
2496 ip = VTOI(fncp->nc_vp);
2497 hammer2_inode_ref(ip); /* extra ref */
2500 * Lookup the target name to determine if a directory entry
2501 * is being overwritten. We only hold related inode locks
2502 * temporarily, the operating system is expected to protect
2503 * against rename races.
2505 tip = tncp->nc_vp ? VTOI(tncp->nc_vp) : NULL;
2506 if (tip)
2507 hammer2_inode_ref(tip); /* extra ref */
2510 * Can return NULL and error == EXDEV if the common parent
2511 * crosses a directory with the xlink flag set.
2513 * For now try to avoid deadlocks with a simple pointer address
2514 * test. (tip) can be NULL.
2516 error = 0;
2518 hammer2_inode_t *ip1 = fdip;
2519 hammer2_inode_t *ip2 = tdip;
2520 hammer2_inode_t *ip3 = ip;
2521 hammer2_inode_t *ip4 = tip; /* may be NULL */
2523 if (fdip > tdip) {
2524 ip1 = tdip;
2525 ip2 = fdip;
2527 if (tip && ip > tip) {
2528 ip3 = tip;
2529 ip4 = ip;
2531 hammer2_inode_lock4(ip1, ip2, ip3, ip4);
2535 * Resolve the collision space for (tdip, tname, tname_len)
2537 * tdip must be held exclusively locked to prevent races since
2538 * multiple filenames can end up in the same collision space.
2541 hammer2_xop_scanlhc_t *sxop;
2542 hammer2_tid_t lhcbase;
2544 tlhc = hammer2_dirhash(tname, tname_len);
2545 lhcbase = tlhc;
2546 sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
2547 sxop->lhc = tlhc;
2548 hammer2_xop_start(&sxop->head, &hammer2_scanlhc_desc);
2549 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
2550 if (tlhc != sxop->head.cluster.focus->bref.key)
2551 break;
2552 ++tlhc;
2554 error = hammer2_error_to_errno(error);
2555 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
2557 if (error) {
2558 if (error != ENOENT)
2559 goto done2;
2560 ++tlhc;
2561 error = 0;
2563 if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) {
2564 error = ENOSPC;
2565 goto done2;
2570 * Ready to go, issue the rename to the backend. Note that meta-data
2571 * updates to the related inodes occur separately from the rename
2572 * operation.
2574 * NOTE: While it is not necessary to update ip->meta.name*, doing
2575 * so aids catastrophic recovery and debugging.
2577 if (error == 0) {
2578 hammer2_xop_nrename_t *xop4;
2580 xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
2581 xop4->lhc = tlhc;
2582 xop4->ip_key = ip->meta.name_key;
2583 hammer2_xop_setip2(&xop4->head, ip);
2584 hammer2_xop_setip3(&xop4->head, tdip);
2585 if (tip && tip->meta.type == HAMMER2_OBJTYPE_DIRECTORY)
2586 hammer2_xop_setip4(&xop4->head, tip);
2587 hammer2_xop_setname(&xop4->head, fname, fname_len);
2588 hammer2_xop_setname2(&xop4->head, tname, tname_len);
2589 hammer2_xop_start(&xop4->head, &hammer2_nrename_desc);
2591 error = hammer2_xop_collect(&xop4->head, 0);
2592 error = hammer2_error_to_errno(error);
2593 hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP);
2595 if (error == ENOENT)
2596 error = 0;
2599 * Update inode meta-data.
2601 * WARNING! The in-memory inode (ip) structure does not
2602 * maintain a copy of the inode's filename buffer.
2604 if (error == 0 &&
2605 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
2606 hammer2_inode_modify(ip);
2607 ip->meta.name_len = tname_len;
2608 ip->meta.name_key = tlhc;
2610 if (error == 0) {
2611 hammer2_inode_modify(ip);
2612 ip->meta.iparent = tdip->meta.inum;
2614 update_fdip = 1;
2615 update_tdip = 1;
2618 done2:
2620 * If no error, the backend has replaced the target directory entry.
2621 * We must adjust nlinks on the original replace target if it exists.
2623 vprecycle = NULL;
2624 if (error == 0 && tip) {
2625 hammer2_inode_unlink_finisher(tip, &vprecycle);
2629 * Update directory mtimes to represent the something changed.
2631 if (update_fdip || update_tdip) {
2632 uint64_t mtime;
2634 hammer2_update_time(&mtime);
2635 if (update_fdip) {
2636 hammer2_inode_modify(fdip);
2637 fdip->meta.mtime = mtime;
2639 if (update_tdip) {
2640 hammer2_inode_modify(tdip);
2641 tdip->meta.mtime = mtime;
2644 if (tip) {
2645 hammer2_inode_unlock(tip);
2646 hammer2_inode_drop(tip);
2648 hammer2_inode_unlock(ip);
2649 hammer2_inode_unlock(tdip);
2650 hammer2_inode_unlock(fdip);
2651 hammer2_inode_drop(ip);
2652 hammer2_trans_done(tdip->pmp, HAMMER2_TRANS_SIDEQ);
2655 * Issue the namecache update after unlocking all the internal
2656 * hammer2 structures, otherwise we might deadlock.
2658 * WARNING! The target namespace must be updated atomically,
2659 * and we depend on cache_rename() to handle that for
2660 * us. Do not do a separate cache_unlink() because
2661 * that leaves a small window of opportunity for other
2662 * threads to allocate the target namespace before we
2663 * manage to complete our rename.
2665 * WARNING! cache_rename() (and cache_unlink()) will properly
2666 * set VREF_FINALIZE on any attached vnode. Do not
2667 * call cache_setunresolved() manually before-hand as
2668 * this will prevent the flag from being set later via
2669 * cache_rename(). If VREF_FINALIZE is not properly set
2670 * and the inode is no longer in the topology, related
2671 * chains can remain dirty indefinitely.
2673 if (error == 0 && tip) {
2674 /*cache_unlink(ap->a_tnch); see above */
2675 /*cache_setunresolved(ap->a_tnch); see above */
2677 if (error == 0) {
2678 cache_rename(ap->a_fnch, ap->a_tnch);
2679 hammer2_knote(ap->a_fdvp, NOTE_WRITE);
2680 hammer2_knote(ap->a_tdvp, NOTE_WRITE);
2681 hammer2_knote(fncp->nc_vp, NOTE_RENAME);
2683 if (vprecycle)
2684 hammer2_inode_vprecycle(vprecycle);
2686 return (error);
2687 #endif
2688 return (EOPNOTSUPP);
2692 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2694 static
2696 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2698 #if 0
2699 hammer2_inode_t *ip;
2700 int error;
2702 ip = VTOI(ap->a_vp);
2704 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2705 ap->a_fflag, ap->a_cred);
2706 return (error);
2707 #endif
2708 return (EOPNOTSUPP);
2711 static
2713 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2715 #if 0
2716 struct mount *mp;
2717 hammer2_pfs_t *pmp;
2718 int rc;
2720 switch (ap->a_op) {
2721 case (MOUNTCTL_SET_EXPORT):
2722 mp = ap->a_head.a_ops->head.vv_mount;
2723 pmp = MPTOPMP(mp);
2725 if (ap->a_ctllen != sizeof(struct export_args))
2726 rc = (EINVAL);
2727 else
2728 rc = vfs_export(mp, &pmp->export,
2729 (const struct export_args *)ap->a_ctl);
2730 break;
2731 default:
2732 rc = vop_stdmountctl(ap);
2733 break;
2735 return (rc);
2736 #endif
2737 return (EOPNOTSUPP);
2741 * KQFILTER
2744 static void filt_hammer2detach(struct knote *kn);
2745 static int filt_hammer2read(struct knote *kn, long hint);
2746 static int filt_hammer2write(struct knote *kn, long hint);
2747 static int filt_hammer2vnode(struct knote *kn, long hint);
2749 static struct filterops hammer2read_filtops =
2750 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2751 NULL, filt_hammer2detach, filt_hammer2read };
2752 static struct filterops hammer2write_filtops =
2753 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2754 NULL, filt_hammer2detach, filt_hammer2write };
2755 static struct filterops hammer2vnode_filtops =
2756 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2757 NULL, filt_hammer2detach, filt_hammer2vnode };
2760 static
2762 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
2764 #if 0
2765 struct m_vnode *vp = ap->a_vp;
2766 struct knote *kn = ap->a_kn;
2768 switch (kn->kn_filter) {
2769 case EVFILT_READ:
2770 kn->kn_fop = &hammer2read_filtops;
2771 break;
2772 case EVFILT_WRITE:
2773 kn->kn_fop = &hammer2write_filtops;
2774 break;
2775 case EVFILT_VNODE:
2776 kn->kn_fop = &hammer2vnode_filtops;
2777 break;
2778 default:
2779 return (EOPNOTSUPP);
2782 kn->kn_hook = (caddr_t)vp;
2784 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2786 return(0);
2787 #endif
2788 return (EOPNOTSUPP);
2791 #if 0
2792 static void
2793 filt_hammer2detach(struct knote *kn)
2795 struct m_vnode *vp = (void *)kn->kn_hook;
2797 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2800 static int
2801 filt_hammer2read(struct knote *kn, long hint)
2803 struct m_vnode *vp = (void *)kn->kn_hook;
2804 hammer2_inode_t *ip = VTOI(vp);
2805 off_t off;
2807 if (hint == NOTE_REVOKE) {
2808 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2809 return(1);
2811 off = ip->meta.size - kn->kn_fp->f_offset;
2812 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2813 if (kn->kn_sfflags & NOTE_OLDAPI)
2814 return(1);
2815 return (kn->kn_data != 0);
2819 static int
2820 filt_hammer2write(struct knote *kn, long hint)
2822 if (hint == NOTE_REVOKE)
2823 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2824 kn->kn_data = 0;
2825 return (1);
2828 static int
2829 filt_hammer2vnode(struct knote *kn, long hint)
2831 if (kn->kn_sfflags & hint)
2832 kn->kn_fflags |= hint;
2833 if (hint == NOTE_REVOKE) {
2834 kn->kn_flags |= (EV_EOF | EV_NODATA);
2835 return (1);
2837 return (kn->kn_fflags != 0);
2839 #endif
2842 * FIFO VOPS
2844 static
2846 hammer2_vop_markatime(struct vop_markatime_args *ap)
2848 #if 0
2849 hammer2_inode_t *ip;
2850 struct m_vnode *vp;
2852 vp = ap->a_vp;
2853 ip = VTOI(vp);
2855 if (ip->pmp->ronly || (ip->pmp->flags & HAMMER2_PMPF_EMERG))
2856 return (EROFS);
2857 return(0);
2858 #endif
2859 return (EOPNOTSUPP);
2862 static
2864 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2866 #if 0
2867 int error;
2869 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2870 if (error)
2871 error = hammer2_vop_kqfilter(ap);
2872 return(error);
2873 #endif
2874 return (EOPNOTSUPP);
2878 * VOPS vector
2880 struct vop_ops hammer2_vnode_vops = {
2881 .vop_default = vop_defaultop,
2882 .vop_fsync = hammer2_vop_fsync,
2883 .vop_getpages = vop_stdgetpages,
2884 .vop_putpages = vop_stdputpages,
2885 .vop_access = hammer2_vop_access,
2886 .vop_advlock = hammer2_vop_advlock,
2887 .vop_close = hammer2_vop_close,
2888 .vop_nlink = hammer2_vop_nlink,
2889 .vop_ncreate = hammer2_vop_ncreate,
2890 .vop_nsymlink = hammer2_vop_nsymlink,
2891 .vop_nremove = hammer2_vop_nremove,
2892 .vop_nrmdir = hammer2_vop_nrmdir,
2893 .vop_nrename = hammer2_vop_nrename,
2894 .vop_getattr = hammer2_vop_getattr,
2895 .vop_getattr_lite = hammer2_vop_getattr_lite,
2896 .vop_setattr = hammer2_vop_setattr,
2897 .vop_readdir = hammer2_vop_readdir,
2898 .vop_readlink = hammer2_vop_readlink,
2899 .vop_read = hammer2_vop_read,
2900 .vop_write = hammer2_vop_write,
2901 .vop_open = hammer2_vop_open,
2902 .vop_inactive = hammer2_vop_inactive,
2903 .vop_reclaim = hammer2_vop_reclaim,
2904 .vop_nresolve = hammer2_vop_nresolve,
2905 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2906 .vop_nmkdir = hammer2_vop_nmkdir,
2907 .vop_nmknod = hammer2_vop_nmknod,
2908 .vop_ioctl = hammer2_vop_ioctl,
2909 .vop_mountctl = hammer2_vop_mountctl,
2910 .vop_bmap = hammer2_vop_bmap,
2911 .vop_strategy = hammer2_vop_strategy,
2912 .vop_kqfilter = hammer2_vop_kqfilter
2915 struct vop_ops hammer2_spec_vops = {
2916 .vop_default = vop_defaultop,
2917 .vop_fsync = hammer2_vop_fsync,
2918 .vop_read = vop_stdnoread,
2919 .vop_write = vop_stdnowrite,
2920 .vop_access = hammer2_vop_access,
2921 .vop_close = hammer2_vop_close,
2922 .vop_markatime = hammer2_vop_markatime,
2923 .vop_getattr = hammer2_vop_getattr,
2924 .vop_inactive = hammer2_vop_inactive,
2925 .vop_reclaim = hammer2_vop_reclaim,
2926 .vop_setattr = hammer2_vop_setattr
2929 struct vop_ops hammer2_fifo_vops = {
2930 .vop_default = fifo_vnoperate,
2931 .vop_fsync = hammer2_vop_fsync,
2932 #if 0
2933 .vop_read = hammer2_vop_fiforead,
2934 .vop_write = hammer2_vop_fifowrite,
2935 #endif
2936 .vop_access = hammer2_vop_access,
2937 #if 0
2938 .vop_close = hammer2_vop_fifoclose,
2939 #endif
2940 .vop_markatime = hammer2_vop_markatime,
2941 .vop_getattr = hammer2_vop_getattr,
2942 .vop_inactive = hammer2_vop_inactive,
2943 .vop_reclaim = hammer2_vop_reclaim,
2944 .vop_setattr = hammer2_vop_setattr,
2945 .vop_kqfilter = hammer2_vop_fifokqfilter