2 * Copyright (c) 2011-2015 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7 * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Kernel Filesystem interface
39 * NOTE! local ipdata pointers must be reloaded on any modifying operation
40 * to the inode as its underlying chain may have changed.
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/fcntl.h>
49 #include <sys/namei.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/mountctl.h>
53 #include <sys/dirent.h>
55 #include <sys/objcache.h>
56 #include <sys/event.h>
58 #include <vfs/fifofs/fifo.h>
62 static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio,
64 static int hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
65 int ioflag, int seqcount);
66 static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize);
67 static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize);
69 struct objcache *cache_xops;
73 hammer2_knote(struct vnode *vp, int flags)
76 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
80 * Last reference to a vnode is going away but it is still cached.
84 hammer2_vop_inactive(struct vop_inactive_args *ap)
103 * Check for deleted inodes and recycle immediately on the last
104 * release. Be sure to destroy any left-over buffer cache buffers
105 * so we do not waste time trying to flush them.
107 * Note that deleting the file block chains under the inode chain
108 * would just be a waste of energy, so don't do it.
110 * WARNING: nvtruncbuf() can only be safely called without the inode
111 * lock held due to the way our write thread works.
113 if (ip->flags & HAMMER2_INODE_ISUNLINKED) {
118 * Detect updates to the embedded data which may be
119 * synchronized by the strategy code. Simply mark the
120 * inode modified so it gets picked up by our normal flush.
122 nblksize = hammer2_calc_logical(ip, 0, &lbase, NULL);
123 nvtruncbuf(vp, 0, nblksize, 0, 0);
131 * Reclaim a vnode so that it can be reused; after the inode is
132 * disassociated, the filesystem must manage it alone.
136 hammer2_vop_reclaim(struct vop_reclaim_args *ap)
152 * The final close of a deleted file or directory marks it for
153 * destruction. The DELETED flag allows the flusher to shortcut
154 * any modified blocks still unflushed (that is, just ignore them).
156 * HAMMER2 usually does not try to optimize the freemap by returning
157 * deleted blocks to it as it does not usually know how many snapshots
158 * might be referencing portions of the file/dir.
164 * NOTE! We do not attempt to flush chains here, flushing is
165 * really fragile and could also deadlock.
170 * An unlinked inode may have been relinked to the ihidden directory.
171 * This occurs if the inode was unlinked while open. Reclamation of
172 * these inodes requires processing we cannot safely do here so add
173 * the inode to the sideq in that situation.
175 * A modified inode may require chain synchronization which will no
176 * longer be driven by a sync or fsync without the vnode, also use
177 * the sideq for that.
179 * A reclaim can occur at any time so we cannot safely start a
180 * transaction to handle reclamation of unlinked files. Instead,
181 * the ip is left with a reference and placed on a linked list and
184 if ((ip->flags & (HAMMER2_INODE_ISUNLINKED |
185 HAMMER2_INODE_MODIFIED |
186 HAMMER2_INODE_RESIZED)) &&
187 (ip->flags & HAMMER2_INODE_ISDELETED) == 0) {
188 hammer2_inode_sideq_t *ipul;
190 ipul = kmalloc(sizeof(*ipul), pmp->minode, M_WAITOK | M_ZERO);
193 hammer2_spin_ex(&pmp->list_spin);
194 if ((ip->flags & HAMMER2_INODE_ONSIDEQ) == 0) {
196 atomic_set_int(&ip->flags, HAMMER2_INODE_ONSIDEQ);
197 TAILQ_INSERT_TAIL(&pmp->sideq, ipul, entry);
198 hammer2_spin_unex(&pmp->list_spin);
200 hammer2_spin_unex(&pmp->list_spin);
201 kfree(ipul, pmp->minode);
202 hammer2_inode_drop(ip); /* vp ref */
204 /* retain ref from vp for ipul */
206 hammer2_inode_drop(ip); /* vp ref */
210 * XXX handle background sync when ip dirty, kernel will no longer
211 * notify us regarding this inode because there is no longer a
212 * vnode attached to it.
221 hammer2_vop_fsync(struct vop_fsync_args *ap)
231 /* XXX can't do this yet */
232 hammer2_trans_init(ip->pmp, HAMMER2_TRANS_ISFLUSH);
233 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
235 hammer2_trans_init(ip->pmp, 0);
236 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
239 * Calling chain_flush here creates a lot of duplicative
240 * COW operations due to non-optimal vnode ordering.
242 * Only do it for an actual fsync() syscall. The other forms
243 * which call this function will eventually call chain_flush
244 * on the volume root as a catch-all, which is far more optimal.
246 hammer2_inode_lock(ip, 0);
247 if (ip->flags & HAMMER2_INODE_MODIFIED)
248 hammer2_inode_chain_sync(ip);
249 hammer2_inode_unlock(ip);
250 hammer2_trans_done(ip->pmp);
258 hammer2_vop_access(struct vop_access_args *ap)
260 hammer2_inode_t *ip = VTOI(ap->a_vp);
266 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
267 uid = hammer2_to_unix_xid(&ip->meta.uid);
268 gid = hammer2_to_unix_xid(&ip->meta.gid);
269 error = vop_helper_access(ap, uid, gid, ip->meta.mode, ip->meta.uflags);
270 hammer2_inode_unlock(ip);
278 hammer2_vop_getattr(struct vop_getattr_args *ap)
284 hammer2_chain_t *chain;
294 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
296 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
297 vap->va_fileid = ip->meta.inum;
298 vap->va_mode = ip->meta.mode;
299 vap->va_nlink = ip->meta.nlinks;
300 vap->va_uid = hammer2_to_unix_xid(&ip->meta.uid);
301 vap->va_gid = hammer2_to_unix_xid(&ip->meta.gid);
304 vap->va_size = ip->meta.size; /* protected by shared lock */
305 vap->va_blocksize = HAMMER2_PBUFSIZE;
306 vap->va_flags = ip->meta.uflags;
307 hammer2_time_to_timespec(ip->meta.ctime, &vap->va_ctime);
308 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_mtime);
309 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_atime);
312 if (ip->meta.type == HAMMER2_OBJTYPE_DIRECTORY) {
314 * Can't really calculate directory use sans the files under
315 * it, just assume one block for now.
317 vap->va_bytes += HAMMER2_INODE_BYTES;
319 for (i = 0; i < ip->cluster.nchains; ++i) {
320 if ((chain = ip->cluster.array[i].chain) != NULL) {
321 if (vap->va_bytes < chain->bref.data_count)
322 vap->va_bytes = chain->bref.data_count;
326 vap->va_type = hammer2_get_vtype(ip->meta.type);
328 vap->va_uid_uuid = ip->meta.uid;
329 vap->va_gid_uuid = ip->meta.gid;
330 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
333 hammer2_inode_unlock(ip);
341 hammer2_vop_setattr(struct vop_setattr_args *ap)
353 hammer2_update_time(&ctime);
357 if (ip->pmp->ronly) {
362 hammer2_pfs_memory_wait(ip->pmp);
363 hammer2_trans_init(ip->pmp, 0);
364 hammer2_inode_lock(ip, 0);
367 if (vap->va_flags != VNOVAL) {
370 flags = ip->meta.uflags;
371 error = vop_helper_setattr_flags(&flags, vap->va_flags,
372 hammer2_to_unix_xid(&ip->meta.uid),
375 if (ip->meta.uflags != flags) {
376 hammer2_inode_modify(ip);
377 ip->meta.uflags = flags;
378 ip->meta.ctime = ctime;
379 kflags |= NOTE_ATTRIB;
381 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
388 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
392 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
393 mode_t cur_mode = ip->meta.mode;
394 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
395 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
399 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
401 &cur_uid, &cur_gid, &cur_mode);
403 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
404 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
405 if (bcmp(&uuid_uid, &ip->meta.uid, sizeof(uuid_uid)) ||
406 bcmp(&uuid_gid, &ip->meta.gid, sizeof(uuid_gid)) ||
407 ip->meta.mode != cur_mode
409 hammer2_inode_modify(ip);
410 ip->meta.uid = uuid_uid;
411 ip->meta.gid = uuid_gid;
412 ip->meta.mode = cur_mode;
413 ip->meta.ctime = ctime;
415 kflags |= NOTE_ATTRIB;
422 if (vap->va_size != VNOVAL && ip->meta.size != vap->va_size) {
425 if (vap->va_size == ip->meta.size)
427 if (vap->va_size < ip->meta.size) {
428 hammer2_truncate_file(ip, vap->va_size);
430 hammer2_extend_file(ip, vap->va_size);
432 hammer2_inode_modify(ip);
433 ip->meta.mtime = ctime;
441 /* atime not supported */
442 if (vap->va_atime.tv_sec != VNOVAL) {
443 hammer2_inode_modify(ip);
444 ip->meta.atime = hammer2_timespec_to_time(&vap->va_atime);
445 kflags |= NOTE_ATTRIB;
448 if (vap->va_mode != (mode_t)VNOVAL) {
449 mode_t cur_mode = ip->meta.mode;
450 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
451 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
453 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
454 cur_uid, cur_gid, &cur_mode);
455 if (error == 0 && ip->meta.mode != cur_mode) {
456 hammer2_inode_modify(ip);
457 ip->meta.mode = cur_mode;
458 ip->meta.ctime = ctime;
459 kflags |= NOTE_ATTRIB;
463 if (vap->va_mtime.tv_sec != VNOVAL) {
464 hammer2_inode_modify(ip);
465 ip->meta.mtime = hammer2_timespec_to_time(&vap->va_mtime);
466 kflags |= NOTE_ATTRIB;
471 * If a truncation occurred we must call inode_fsync() now in order
472 * to trim the related data chains, otherwise a later expansion can
475 * If an extend occured that changed the DIRECTDATA state, we must
476 * call inode_fsync now in order to prepare the inode's indirect
479 if (ip->flags & HAMMER2_INODE_RESIZED)
480 hammer2_inode_chain_sync(ip);
485 hammer2_inode_unlock(ip);
486 hammer2_trans_done(ip->pmp);
487 hammer2_knote(ip->vp, kflags);
495 hammer2_vop_readdir(struct vop_readdir_args *ap)
497 hammer2_xop_readdir_t *xop;
498 hammer2_blockref_t bref;
515 saveoff = uio->uio_offset;
520 * Setup cookies directory entry cookies if requested
522 if (ap->a_ncookies) {
523 ncookies = uio->uio_resid / 16 + 1;
526 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
533 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
536 * Handle artificial entries. To ensure that only positive 64 bit
537 * quantities are returned to userland we always strip off bit 63.
538 * The hash code is designed such that codes 0x0000-0x7FFF are not
539 * used, allowing us to use these codes for articial entries.
541 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
542 * allow '..' to cross the mount point into (e.g.) the super-root.
545 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
546 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
550 cookies[cookie_index] = saveoff;
553 if (cookie_index == ncookies)
559 * Be careful with lockorder when accessing ".."
561 * (ip is the current dir. xip is the parent dir).
563 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
564 if (ip->pip && ip != ip->pmp->iroot)
565 inum = ip->pip->meta.inum & HAMMER2_DIRHASH_USERMSK;
566 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
570 cookies[cookie_index] = saveoff;
573 if (cookie_index == ncookies)
577 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
578 if (hammer2_debug & 0x0020)
579 kprintf("readdir: lkey %016jx\n", lkey);
584 * Use XOP for cluster scan.
586 * parent is the inode cluster, already locked for us. Don't
587 * double lock shared locks as this will screw up upgrades.
589 xop = hammer2_xop_alloc(ip, 0);
591 hammer2_xop_start(&xop->head, hammer2_xop_readdir);
594 const hammer2_inode_data_t *ripdata;
596 error = hammer2_xop_collect(&xop->head, 0);
599 if (cookie_index == ncookies)
601 if (hammer2_debug & 0x0020)
602 kprintf("cluster chain %p %p\n",
603 xop->head.cluster.focus,
604 (xop->head.cluster.focus ?
605 xop->head.cluster.focus->data : (void *)-1));
606 ripdata = &hammer2_cluster_rdata(&xop->head.cluster)->ipdata;
607 hammer2_cluster_bref(&xop->head.cluster, &bref);
608 if (bref.type == HAMMER2_BREF_TYPE_INODE) {
609 dtype = hammer2_get_dtype(ripdata);
610 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
611 r = vop_write_dirent(&error, uio,
613 HAMMER2_DIRHASH_USERMSK,
615 ripdata->meta.name_len,
620 cookies[cookie_index] = saveoff;
623 /* XXX chain error */
624 kprintf("bad chain type readdir %d\n", bref.type);
627 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
628 if (error == ENOENT) {
631 saveoff = (hammer2_key_t)-1;
633 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
636 hammer2_inode_unlock(ip);
638 *ap->a_eofflag = eofflag;
639 if (hammer2_debug & 0x0020)
640 kprintf("readdir: done at %016jx\n", saveoff);
641 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
642 if (error && cookie_index == 0) {
644 kfree(cookies, M_TEMP);
646 *ap->a_cookies = NULL;
650 *ap->a_ncookies = cookie_index;
651 *ap->a_cookies = cookies;
659 * hammer2_vop_readlink { vp, uio, cred }
663 hammer2_vop_readlink(struct vop_readlink_args *ap)
670 if (vp->v_type != VLNK)
674 error = hammer2_read_file(ip, ap->a_uio, 0);
680 hammer2_vop_read(struct vop_read_args *ap)
690 * Read operations supported on this vnode?
693 if (vp->v_type != VREG)
703 seqcount = ap->a_ioflag >> 16;
704 bigread = (uio->uio_resid > 100 * 1024 * 1024);
706 error = hammer2_read_file(ip, uio, seqcount);
712 hammer2_vop_write(struct vop_write_args *ap)
722 * Read operations supported on this vnode?
725 if (vp->v_type != VREG)
734 if (ip->pmp->ronly) {
738 seqcount = ap->a_ioflag >> 16;
741 * Check resource limit
743 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
744 uio->uio_offset + uio->uio_resid >
745 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
746 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
751 * The transaction interlocks against flushes initiations
752 * (note: but will run concurrently with the actual flush).
754 hammer2_trans_init(ip->pmp, 0);
755 error = hammer2_write_file(ip, uio, ap->a_ioflag, seqcount);
756 hammer2_trans_done(ip->pmp);
762 * Perform read operations on a file or symlink given an UNLOCKED
765 * The passed ip is not locked.
769 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
780 * WARNING! Assumes that the kernel interlocks size changes at the
783 hammer2_mtx_sh(&ip->lock);
784 size = ip->meta.size;
785 hammer2_mtx_unlock(&ip->lock);
787 while (uio->uio_resid > 0 && uio->uio_offset < size) {
794 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
797 error = cluster_read(ip->vp, leof, lbase, lblksize,
798 uio->uio_resid, seqcount * BKVASIZE,
803 loff = (int)(uio->uio_offset - lbase);
805 if (n > uio->uio_resid)
807 if (n > size - uio->uio_offset)
808 n = (int)(size - uio->uio_offset);
809 bp->b_flags |= B_AGE;
810 uiomove((char *)bp->b_data + loff, n, uio);
817 * Write to the file represented by the inode via the logical buffer cache.
818 * The inode may represent a regular file or a symlink.
820 * The inode must not be locked.
824 hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
825 int ioflag, int seqcount)
827 hammer2_key_t old_eof;
828 hammer2_key_t new_eof;
837 * WARNING! Assumes that the kernel interlocks size changes at the
840 hammer2_mtx_ex(&ip->lock);
841 if (ioflag & IO_APPEND)
842 uio->uio_offset = ip->meta.size;
843 old_eof = ip->meta.size;
846 * Extend the file if necessary. If the write fails at some point
847 * we will truncate it back down to cover as much as we were able
850 * Doing this now makes it easier to calculate buffer sizes in
857 if (uio->uio_offset + uio->uio_resid > old_eof) {
858 new_eof = uio->uio_offset + uio->uio_resid;
860 hammer2_extend_file(ip, new_eof);
861 kflags |= NOTE_EXTEND;
865 hammer2_mtx_unlock(&ip->lock);
870 while (uio->uio_resid > 0) {
879 * Don't allow the buffer build to blow out the buffer
882 if ((ioflag & IO_RECURSE) == 0)
883 bwillwrite(HAMMER2_PBUFSIZE);
886 * This nominally tells us how much we can cluster and
887 * what the logical buffer size needs to be. Currently
888 * we don't try to cluster the write and just handle one
891 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
893 loff = (int)(uio->uio_offset - lbase);
895 KKASSERT(lblksize <= 65536);
898 * Calculate bytes to copy this transfer and whether the
899 * copy completely covers the buffer or not.
903 if (n > uio->uio_resid) {
905 if (loff == lbase && uio->uio_offset + n == new_eof)
917 if (uio->uio_segflg == UIO_NOCOPY) {
919 * Issuing a write with the same data backing the
920 * buffer. Instantiate the buffer to collect the
921 * backing vm pages, then read-in any missing bits.
923 * This case is used by vop_stdputpages().
925 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
926 if ((bp->b_flags & B_CACHE) == 0) {
928 error = bread(ip->vp, lbase, lblksize, &bp);
930 } else if (trivial) {
932 * Even though we are entirely overwriting the buffer
933 * we may still have to zero it out to avoid a
934 * mmap/write visibility issue.
936 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
937 if ((bp->b_flags & B_CACHE) == 0)
941 * Partial overwrite, read in any missing bits then
942 * replace the portion being written.
944 * (The strategy code will detect zero-fill physical
945 * blocks for this case).
947 error = bread(ip->vp, lbase, lblksize, &bp);
958 * Ok, copy the data in
960 error = uiomove(bp->b_data + loff, n, uio);
961 kflags |= NOTE_WRITE;
969 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
970 * with IO_SYNC or IO_ASYNC set. These writes
971 * must be handled as the pageout daemon expects.
973 if (ioflag & IO_SYNC) {
975 } else if ((ioflag & IO_DIRECT) && endofblk) {
977 } else if (ioflag & IO_ASYNC) {
985 * Cleanup. If we extended the file EOF but failed to write through
986 * the entire write is a failure and we have to back-up.
988 if (error && new_eof != old_eof) {
989 hammer2_mtx_ex(&ip->lock);
990 hammer2_truncate_file(ip, old_eof);
991 if (ip->flags & HAMMER2_INODE_MODIFIED)
992 hammer2_inode_chain_sync(ip);
993 hammer2_mtx_unlock(&ip->lock);
994 } else if (modified) {
995 hammer2_mtx_ex(&ip->lock);
996 hammer2_inode_modify(ip);
997 hammer2_update_time(&ip->meta.mtime);
998 if (ip->flags & HAMMER2_INODE_MODIFIED)
999 hammer2_inode_chain_sync(ip);
1000 hammer2_mtx_unlock(&ip->lock);
1001 hammer2_knote(ip->vp, kflags);
1003 hammer2_trans_assert_strategy(ip->pmp);
1009 * Truncate the size of a file. The inode must not be locked.
1011 * We must unconditionally set HAMMER2_INODE_RESIZED to properly
1012 * ensure that any on-media data beyond the new file EOF has been destroyed.
1014 * WARNING: nvtruncbuf() can only be safely called without the inode lock
1015 * held due to the way our write thread works. If the truncation
1016 * occurs in the middle of a buffer, nvtruncbuf() is responsible
1017 * for dirtying that buffer and zeroing out trailing bytes.
1019 * WARNING! Assumes that the kernel interlocks size changes at the
1022 * WARNING! Caller assumes responsibility for removing dead blocks
1023 * if INODE_RESIZED is set.
1027 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1029 hammer2_key_t lbase;
1033 hammer2_mtx_unlock(&ip->lock);
1035 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1036 nvtruncbuf(ip->vp, nsize,
1037 nblksize, (int)nsize & (nblksize - 1),
1040 hammer2_mtx_ex(&ip->lock);
1041 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1042 ip->osize = ip->meta.size;
1043 ip->meta.size = nsize;
1044 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED |
1045 HAMMER2_INODE_RESIZED);
1050 * Extend the size of a file. The inode must not be locked.
1052 * Even though the file size is changing, we do not have to set the
1053 * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES
1054 * boundary. When this occurs a hammer2_inode_chain_sync() is required
1055 * to prepare the inode cluster's indirect block table, otherwise
1056 * async execution of the strategy code will implode on us.
1058 * WARNING! Assumes that the kernel interlocks size changes at the
1061 * WARNING! Caller assumes responsibility for transitioning out
1062 * of the inode DIRECTDATA mode if INODE_RESIZED is set.
1066 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1068 hammer2_key_t lbase;
1069 hammer2_key_t osize;
1075 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1076 osize = ip->meta.size;
1078 ip->meta.size = nsize;
1079 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1081 if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES) {
1082 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1083 hammer2_inode_chain_sync(ip);
1086 hammer2_mtx_unlock(&ip->lock);
1088 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1089 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1095 hammer2_mtx_ex(&ip->lock);
1102 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1104 hammer2_xop_nresolve_t *xop;
1105 hammer2_inode_t *ip;
1106 hammer2_inode_t *dip;
1107 struct namecache *ncp;
1112 dip = VTOI(ap->a_dvp);
1113 xop = hammer2_xop_alloc(dip, 0);
1115 ncp = ap->a_nch->ncp;
1116 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1119 * Note: In DragonFly the kernel handles '.' and '..'.
1121 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1122 hammer2_xop_start(&xop->head, hammer2_xop_nresolve);
1124 error = hammer2_xop_collect(&xop->head, 0);
1128 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1130 hammer2_inode_unlock(dip);
1133 * Acquire the related vnode
1135 * NOTE: For error processing, only ENOENT resolves the namecache
1136 * entry to NULL, otherwise we just return the error and
1137 * leave the namecache unresolved.
1139 * NOTE: multiple hammer2_inode structures can be aliased to the
1140 * same chain element, for example for hardlinks. This
1141 * use case does not 'reattach' inode associations that
1142 * might already exist, but always allocates a new one.
1144 * WARNING: inode structure is locked exclusively via inode_get
1145 * but chain was locked shared. inode_unlock()
1146 * will handle it properly.
1149 vp = hammer2_igetv(ip, &error);
1152 cache_setvp(ap->a_nch, vp);
1153 } else if (error == ENOENT) {
1154 cache_setvp(ap->a_nch, NULL);
1156 hammer2_inode_unlock(ip);
1159 * The vp should not be released until after we've disposed
1160 * of our locks, because it might cause vop_inactive() to
1167 cache_setvp(ap->a_nch, NULL);
1169 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1170 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1171 ("resolve error %d/%p ap %p\n",
1172 error, ap->a_nch->ncp->nc_vp, ap));
1180 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1182 hammer2_inode_t *dip;
1183 hammer2_inode_t *ip;
1187 dip = VTOI(ap->a_dvp);
1189 if ((ip = dip->pip) == NULL) {
1194 hammer2_inode_lock(ip, 0);
1195 *ap->a_vpp = hammer2_igetv(ip, &error);
1196 hammer2_inode_unlock(ip);
1204 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1206 hammer2_inode_t *dip;
1207 hammer2_inode_t *nip;
1208 struct namecache *ncp;
1209 const uint8_t *name;
1214 dip = VTOI(ap->a_dvp);
1215 if (dip->pmp->ronly) {
1220 ncp = ap->a_nch->ncp;
1221 name = ncp->nc_name;
1222 name_len = ncp->nc_nlen;
1224 hammer2_pfs_memory_wait(dip->pmp);
1225 hammer2_trans_init(dip->pmp, 0);
1226 nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1228 hammer2_trans_newinum(dip->pmp), 0, 0,
1231 KKASSERT(nip == NULL);
1234 *ap->a_vpp = hammer2_igetv(nip, &error);
1235 hammer2_inode_unlock(nip);
1237 hammer2_trans_done(dip->pmp);
1240 cache_setunresolved(ap->a_nch);
1241 cache_setvp(ap->a_nch, *ap->a_vpp);
1249 hammer2_vop_open(struct vop_open_args *ap)
1251 return vop_stdopen(ap);
1255 * hammer2_vop_advlock { vp, id, op, fl, flags }
1259 hammer2_vop_advlock(struct vop_advlock_args *ap)
1261 hammer2_inode_t *ip = VTOI(ap->a_vp);
1264 size = ip->meta.size;
1265 return (lf_advlock(ap, &ip->advlock, size));
1270 hammer2_vop_close(struct vop_close_args *ap)
1272 return vop_stdclose(ap);
1276 * hammer2_vop_nlink { nch, dvp, vp, cred }
1278 * Create a hardlink from (vp) to {dvp, nch}.
1282 hammer2_vop_nlink(struct vop_nlink_args *ap)
1284 hammer2_xop_nlink_t *xop1;
1285 hammer2_inode_t *fdip; /* target directory to create link in */
1286 hammer2_inode_t *tdip; /* target directory to create link in */
1287 hammer2_inode_t *cdip; /* common parent directory */
1288 hammer2_inode_t *ip; /* inode we are hardlinking to */
1289 struct namecache *ncp;
1290 const uint8_t *name;
1296 tdip = VTOI(ap->a_dvp);
1297 if (tdip->pmp->ronly) {
1302 ncp = ap->a_nch->ncp;
1303 name = ncp->nc_name;
1304 name_len = ncp->nc_nlen;
1307 * ip represents the file being hardlinked. The file could be a
1308 * normal file or a hardlink target if it has already been hardlinked.
1309 * If ip is a hardlinked target then ip->pip represents the location
1310 * of the hardlinked target, NOT the location of the hardlink pointer.
1312 * Bump nlinks and potentially also create or move the hardlink
1313 * target in the parent directory common to (ip) and (tdip). The
1314 * consolidation code can modify ip->cluster and ip->pip. The
1315 * returned cluster is locked.
1317 ip = VTOI(ap->a_vp);
1318 hammer2_pfs_memory_wait(ip->pmp);
1319 hammer2_trans_init(ip->pmp, 0);
1322 * The common parent directory must be locked first to avoid deadlocks.
1323 * Also note that fdip and/or tdip might match cdip.
1325 * WARNING! The kernel's namecache locks are insufficient for
1326 * protecting us from hardlink shifts, since unrelated
1327 * rename() or link() calls on parent directories might
1328 * cause a shift. A PFS-wide lock is required for this
1331 if (ip->meta.type == HAMMER2_OBJTYPE_DIRECTORY ||
1332 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1333 lockmgr(&ip->pmp->lock_nlink, LK_EXCLUSIVE);
1339 cdip = hammer2_inode_common_parent(fdip, tdip);
1340 hammer2_inode_lock(cdip, 0);
1341 hammer2_inode_lock(fdip, 0);
1342 hammer2_inode_lock(tdip, 0);
1343 hammer2_inode_lock(ip, 0);
1347 * Dispatch xop_nlink unconditionally since we have to update nlinks.
1349 * Otherwise we'd be able to avoid the XOP if the ip does not have
1350 * to be converted or moved.
1351 * If ip is not a hardlink target we must convert it to a hardlink.
1352 * If fdip != cdip we must shift the inode to cdip.
1354 * XXX this and other nlink update usage should be passed top-down
1355 * and not updated with a delta bottom-up.
1358 if (fdip != cdip || (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE))
1361 xop1 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
1362 hammer2_xop_setip2(&xop1->head, ip);
1363 hammer2_xop_setip3(&xop1->head, cdip);
1364 xop1->nlinks_delta = 1;
1366 hammer2_xop_start(&xop1->head, hammer2_xop_nlink);
1367 error = hammer2_xop_collect(&xop1->head, 0);
1368 hammer2_xop_retire(&xop1->head, HAMMER2_XOPMASK_VOP);
1369 if (error == ENOENT)
1374 * Must synchronize original inode whos chains are now a hardlink
1375 * target. We must match what the backend XOP did to the
1378 if (error == 0 && (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1379 hammer2_inode_modify(ip);
1380 ip->meta.name_key = ip->meta.inum;
1381 ip->meta.name_len = 18; /* "0x%016jx" */
1385 * Create the hardlink target and bump nlinks.
1388 hammer2_inode_create(tdip, NULL, NULL,
1391 HAMMER2_OBJTYPE_HARDLINK, ip->meta.type,
1393 hammer2_inode_modify(ip);
1397 cache_setunresolved(ap->a_nch);
1398 cache_setvp(ap->a_nch, ap->a_vp);
1400 hammer2_inode_unlock(ip);
1401 hammer2_inode_unlock(tdip);
1402 hammer2_inode_unlock(fdip);
1403 hammer2_inode_unlock(cdip);
1404 hammer2_inode_drop(cdip);
1407 lockmgr(&ip->pmp->lock_nlink, LK_RELEASE);
1408 hammer2_trans_done(ip->pmp);
1415 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1417 * The operating system has already ensured that the directory entry
1418 * does not exist and done all appropriate namespace locking.
1422 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1424 hammer2_inode_t *dip;
1425 hammer2_inode_t *nip;
1426 struct namecache *ncp;
1427 const uint8_t *name;
1432 dip = VTOI(ap->a_dvp);
1433 if (dip->pmp->ronly) {
1438 ncp = ap->a_nch->ncp;
1439 name = ncp->nc_name;
1440 name_len = ncp->nc_nlen;
1441 hammer2_pfs_memory_wait(dip->pmp);
1442 hammer2_trans_init(dip->pmp, 0);
1444 nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1446 hammer2_trans_newinum(dip->pmp), 0, 0,
1449 KKASSERT(nip == NULL);
1452 *ap->a_vpp = hammer2_igetv(nip, &error);
1453 hammer2_inode_unlock(nip);
1455 hammer2_trans_done(dip->pmp);
1458 cache_setunresolved(ap->a_nch);
1459 cache_setvp(ap->a_nch, *ap->a_vpp);
1466 * Make a device node (typically a fifo)
1470 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1472 hammer2_inode_t *dip;
1473 hammer2_inode_t *nip;
1474 struct namecache *ncp;
1475 const uint8_t *name;
1480 dip = VTOI(ap->a_dvp);
1481 if (dip->pmp->ronly) {
1486 ncp = ap->a_nch->ncp;
1487 name = ncp->nc_name;
1488 name_len = ncp->nc_nlen;
1489 hammer2_pfs_memory_wait(dip->pmp);
1490 hammer2_trans_init(dip->pmp, 0);
1492 nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1494 hammer2_trans_newinum(dip->pmp), 0, 0,
1497 KKASSERT(nip == NULL);
1500 *ap->a_vpp = hammer2_igetv(nip, &error);
1501 hammer2_inode_unlock(nip);
1503 hammer2_trans_done(dip->pmp);
1506 cache_setunresolved(ap->a_nch);
1507 cache_setvp(ap->a_nch, *ap->a_vpp);
1514 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1518 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1520 hammer2_inode_t *dip;
1521 hammer2_inode_t *nip;
1522 struct namecache *ncp;
1523 const uint8_t *name;
1527 dip = VTOI(ap->a_dvp);
1528 if (dip->pmp->ronly)
1531 ncp = ap->a_nch->ncp;
1532 name = ncp->nc_name;
1533 name_len = ncp->nc_nlen;
1534 hammer2_pfs_memory_wait(dip->pmp);
1535 hammer2_trans_init(dip->pmp, 0);
1537 ap->a_vap->va_type = VLNK; /* enforce type */
1539 nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1541 hammer2_trans_newinum(dip->pmp), 0, 0,
1544 KKASSERT(nip == NULL);
1546 hammer2_trans_done(dip->pmp);
1549 *ap->a_vpp = hammer2_igetv(nip, &error);
1552 * Build the softlink (~like file data) and finalize the namecache.
1559 bytes = strlen(ap->a_target);
1561 hammer2_inode_unlock(nip);
1562 bzero(&auio, sizeof(auio));
1563 bzero(&aiov, sizeof(aiov));
1564 auio.uio_iov = &aiov;
1565 auio.uio_segflg = UIO_SYSSPACE;
1566 auio.uio_rw = UIO_WRITE;
1567 auio.uio_resid = bytes;
1568 auio.uio_iovcnt = 1;
1569 auio.uio_td = curthread;
1570 aiov.iov_base = ap->a_target;
1571 aiov.iov_len = bytes;
1572 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1573 /* XXX handle error */
1576 hammer2_inode_unlock(nip);
1578 hammer2_trans_done(dip->pmp);
1581 * Finalize namecache
1584 cache_setunresolved(ap->a_nch);
1585 cache_setvp(ap->a_nch, *ap->a_vpp);
1586 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1592 * hammer2_vop_nremove { nch, dvp, cred }
1596 hammer2_vop_nremove(struct vop_nremove_args *ap)
1598 hammer2_xop_unlink_t *xop;
1599 hammer2_inode_t *dip;
1600 hammer2_inode_t *ip;
1601 struct namecache *ncp;
1606 dip = VTOI(ap->a_dvp);
1607 if (dip->pmp->ronly) {
1612 ncp = ap->a_nch->ncp;
1614 hammer2_pfs_memory_wait(dip->pmp);
1615 hammer2_trans_init(dip->pmp, 0);
1616 hammer2_inode_lock(dip, 0);
1619 * The unlink XOP unlinks the path from the directory and
1620 * locates and returns the cluster associated with the real inode.
1621 * We have to handle nlinks here on the frontend.
1623 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1624 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1625 isopen = cache_isopen(ap->a_nch);
1627 xop->dopermanent = isopen ? 0 : HAMMER2_DELETE_PERMANENT;
1628 hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1631 * Collect the real inode and adjust nlinks, destroy the real
1632 * inode if nlinks transitions to 0 and it was the real inode
1633 * (else it has already been removed).
1635 error = hammer2_xop_collect(&xop->head, 0);
1636 hammer2_inode_unlock(dip);
1639 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1640 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1642 hammer2_inode_unlink_finisher(ip, isopen);
1643 hammer2_inode_unlock(ip);
1646 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1649 hammer2_inode_run_sideq(dip->pmp);
1650 hammer2_trans_done(dip->pmp);
1652 cache_unlink(ap->a_nch);
1658 * hammer2_vop_nrmdir { nch, dvp, cred }
1662 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1664 hammer2_xop_unlink_t *xop;
1665 hammer2_inode_t *dip;
1666 hammer2_inode_t *ip;
1667 struct namecache *ncp;
1672 dip = VTOI(ap->a_dvp);
1673 if (dip->pmp->ronly) {
1678 hammer2_pfs_memory_wait(dip->pmp);
1679 hammer2_trans_init(dip->pmp, 0);
1680 hammer2_inode_lock(dip, 0);
1682 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1684 ncp = ap->a_nch->ncp;
1685 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1686 isopen = cache_isopen(ap->a_nch);
1688 xop->dopermanent = isopen ? 0 : HAMMER2_DELETE_PERMANENT;
1689 hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1692 * Collect the real inode and adjust nlinks, destroy the real
1693 * inode if nlinks transitions to 0 and it was the real inode
1694 * (else it has already been removed).
1696 error = hammer2_xop_collect(&xop->head, 0);
1697 hammer2_inode_unlock(dip);
1700 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1701 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1703 hammer2_inode_unlink_finisher(ip, isopen);
1704 hammer2_inode_unlock(ip);
1707 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1709 hammer2_inode_run_sideq(dip->pmp);
1710 hammer2_trans_done(dip->pmp);
1712 cache_unlink(ap->a_nch);
1718 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1722 hammer2_vop_nrename(struct vop_nrename_args *ap)
1724 struct namecache *fncp;
1725 struct namecache *tncp;
1726 hammer2_inode_t *cdip;
1727 hammer2_inode_t *fdip;
1728 hammer2_inode_t *tdip;
1729 hammer2_inode_t *ip;
1730 const uint8_t *fname;
1732 const uint8_t *tname;
1739 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1741 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1744 fdip = VTOI(ap->a_fdvp); /* source directory */
1745 tdip = VTOI(ap->a_tdvp); /* target directory */
1747 if (fdip->pmp->ronly)
1751 fncp = ap->a_fnch->ncp; /* entry name in source */
1752 fname = fncp->nc_name;
1753 fname_len = fncp->nc_nlen;
1755 tncp = ap->a_tnch->ncp; /* entry name in target */
1756 tname = tncp->nc_name;
1757 tname_len = tncp->nc_nlen;
1759 hammer2_pfs_memory_wait(tdip->pmp);
1760 hammer2_trans_init(tdip->pmp, 0);
1763 * ip is the inode being renamed. If this is a hardlink then
1764 * ip represents the actual file and not the hardlink marker.
1766 ip = VTOI(fncp->nc_vp);
1769 * The common parent directory must be locked first to avoid deadlocks.
1770 * Also note that fdip and/or tdip might match cdip.
1772 * WARNING! The kernel's namecache locks are insufficient for
1773 * protecting us from hardlink shifts, since unrelated
1774 * rename() or link() calls on parent directories might
1775 * cause a shift. A PFS-wide lock is required for this
1778 if (ip->meta.type == HAMMER2_OBJTYPE_DIRECTORY ||
1779 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1780 lockmgr(&ip->pmp->lock_nlink, LK_EXCLUSIVE);
1786 cdip = hammer2_inode_common_parent(ip->pip, tdip);
1787 hammer2_inode_lock(cdip, 0);
1788 hammer2_inode_lock(fdip, 0);
1789 hammer2_inode_lock(tdip, 0);
1790 hammer2_inode_ref(ip); /* extra ref */
1794 * If ip is a hardlink target and fdip != cdip we must shift the
1797 hammer2_inode_lock(ip, 0);
1799 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1800 hammer2_xop_nlink_t *xop1;
1802 xop1 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
1803 hammer2_xop_setip2(&xop1->head, ip);
1804 hammer2_xop_setip3(&xop1->head, cdip);
1805 xop1->nlinks_delta = 0;
1807 hammer2_xop_start(&xop1->head, hammer2_xop_nlink);
1808 error = hammer2_xop_collect(&xop1->head, 0);
1809 hammer2_xop_retire(&xop1->head, HAMMER2_XOPMASK_VOP);
1811 /* hammer2_inode_unlock(ip); */
1814 * Delete the target namespace.
1817 hammer2_xop_unlink_t *xop2;
1818 hammer2_inode_t *tip;
1822 * The unlink XOP unlinks the path from the directory and
1823 * locates and returns the cluster associated with the real
1824 * inode. We have to handle nlinks here on the frontend.
1826 xop2 = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
1827 hammer2_xop_setname(&xop2->head, tname, tname_len);
1828 isopen = cache_isopen(ap->a_tnch);
1830 xop2->dopermanent = isopen ? 0 : HAMMER2_DELETE_PERMANENT;
1831 hammer2_xop_start(&xop2->head, hammer2_xop_unlink);
1834 * Collect the real inode and adjust nlinks, destroy the real
1835 * inode if nlinks transitions to 0 and it was the real inode
1836 * (else it has already been removed).
1838 tnch_error = hammer2_xop_collect(&xop2->head, 0);
1839 /* hammer2_inode_unlock(tdip); */
1841 if (tnch_error == 0) {
1842 tip = hammer2_inode_get(tdip->pmp, NULL,
1843 &xop2->head.cluster, -1);
1844 hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
1846 hammer2_inode_unlink_finisher(tip, isopen);
1847 hammer2_inode_unlock(tip);
1850 hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
1852 /* hammer2_inode_lock(tdip, 0); */
1854 if (tnch_error && tnch_error != ENOENT) {
1861 * Resolve the collision space for (tdip, tname, tname_len)
1863 * tdip must be held exclusively locked to prevent races.
1866 hammer2_xop_scanlhc_t *sxop;
1867 hammer2_tid_t lhcbase;
1869 tlhc = hammer2_dirhash(tname, tname_len);
1871 sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
1873 hammer2_xop_start(&sxop->head, hammer2_xop_scanlhc);
1874 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
1875 if (tlhc != sxop->head.cluster.focus->bref.key)
1879 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
1882 if (error != ENOENT)
1887 if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) {
1894 * Everything is setup, do the rename.
1896 * We have to synchronize ip->meta to the underlying operation.
1898 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
1899 * unlinking elements from their directories. Locking
1900 * the nlinks field does not lock the whole inode.
1902 /* hammer2_inode_lock(ip, 0); */
1904 hammer2_xop_nrename_t *xop4;
1906 xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
1908 xop4->ip_key = ip->meta.name_key;
1909 hammer2_xop_setip2(&xop4->head, ip);
1910 hammer2_xop_setip3(&xop4->head, tdip);
1911 hammer2_xop_setname(&xop4->head, fname, fname_len);
1912 hammer2_xop_setname2(&xop4->head, tname, tname_len);
1913 hammer2_xop_start(&xop4->head, hammer2_xop_nrename);
1915 error = hammer2_xop_collect(&xop4->head, 0);
1916 hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP);
1918 if (error == ENOENT)
1921 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1922 hammer2_inode_modify(ip);
1923 ip->meta.name_len = tname_len;
1924 ip->meta.name_key = tlhc;
1930 * Fixup ip->pip if we were renaming the actual file and not a
1933 if (error == 0 && (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1934 hammer2_inode_t *opip;
1936 if (ip->pip != tdip) {
1937 hammer2_inode_ref(tdip);
1941 hammer2_inode_drop(opip);
1945 hammer2_inode_unlock(ip);
1946 hammer2_inode_unlock(tdip);
1947 hammer2_inode_unlock(fdip);
1948 hammer2_inode_unlock(cdip);
1949 hammer2_inode_drop(ip);
1950 hammer2_inode_drop(cdip);
1951 hammer2_inode_run_sideq(fdip->pmp);
1954 lockmgr(&ip->pmp->lock_nlink, LK_RELEASE);
1955 hammer2_trans_done(tdip->pmp);
1958 * Issue the namecache update after unlocking all the internal
1959 * hammer structures, otherwise we might deadlock.
1961 if (tnch_error == 0) {
1962 cache_unlink(ap->a_tnch);
1963 cache_setunresolved(ap->a_tnch);
1966 cache_rename(ap->a_fnch, ap->a_tnch);
1973 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
1977 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
1979 hammer2_inode_t *ip;
1983 ip = VTOI(ap->a_vp);
1985 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
1986 ap->a_fflag, ap->a_cred);
1993 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2001 case (MOUNTCTL_SET_EXPORT):
2002 mp = ap->a_head.a_ops->head.vv_mount;
2005 if (ap->a_ctllen != sizeof(struct export_args))
2008 rc = vfs_export(mp, &pmp->export,
2009 (const struct export_args *)ap->a_ctl);
2012 rc = vop_stdmountctl(ap);
2022 static void filt_hammer2detach(struct knote *kn);
2023 static int filt_hammer2read(struct knote *kn, long hint);
2024 static int filt_hammer2write(struct knote *kn, long hint);
2025 static int filt_hammer2vnode(struct knote *kn, long hint);
2027 static struct filterops hammer2read_filtops =
2028 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2029 NULL, filt_hammer2detach, filt_hammer2read };
2030 static struct filterops hammer2write_filtops =
2031 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2032 NULL, filt_hammer2detach, filt_hammer2write };
2033 static struct filterops hammer2vnode_filtops =
2034 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2035 NULL, filt_hammer2detach, filt_hammer2vnode };
2039 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
2041 struct vnode *vp = ap->a_vp;
2042 struct knote *kn = ap->a_kn;
2044 switch (kn->kn_filter) {
2046 kn->kn_fop = &hammer2read_filtops;
2049 kn->kn_fop = &hammer2write_filtops;
2052 kn->kn_fop = &hammer2vnode_filtops;
2055 return (EOPNOTSUPP);
2058 kn->kn_hook = (caddr_t)vp;
2060 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2066 filt_hammer2detach(struct knote *kn)
2068 struct vnode *vp = (void *)kn->kn_hook;
2070 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2074 filt_hammer2read(struct knote *kn, long hint)
2076 struct vnode *vp = (void *)kn->kn_hook;
2077 hammer2_inode_t *ip = VTOI(vp);
2080 if (hint == NOTE_REVOKE) {
2081 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2084 off = ip->meta.size - kn->kn_fp->f_offset;
2085 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2086 if (kn->kn_sfflags & NOTE_OLDAPI)
2088 return (kn->kn_data != 0);
2093 filt_hammer2write(struct knote *kn, long hint)
2095 if (hint == NOTE_REVOKE)
2096 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2102 filt_hammer2vnode(struct knote *kn, long hint)
2104 if (kn->kn_sfflags & hint)
2105 kn->kn_fflags |= hint;
2106 if (hint == NOTE_REVOKE) {
2107 kn->kn_flags |= (EV_EOF | EV_NODATA);
2110 return (kn->kn_fflags != 0);
2118 hammer2_vop_markatime(struct vop_markatime_args *ap)
2120 hammer2_inode_t *ip;
2133 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2137 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2139 error = hammer2_vop_kqfilter(ap);
2146 struct vop_ops hammer2_vnode_vops = {
2147 .vop_default = vop_defaultop,
2148 .vop_fsync = hammer2_vop_fsync,
2149 .vop_getpages = vop_stdgetpages,
2150 .vop_putpages = vop_stdputpages,
2151 .vop_access = hammer2_vop_access,
2152 .vop_advlock = hammer2_vop_advlock,
2153 .vop_close = hammer2_vop_close,
2154 .vop_nlink = hammer2_vop_nlink,
2155 .vop_ncreate = hammer2_vop_ncreate,
2156 .vop_nsymlink = hammer2_vop_nsymlink,
2157 .vop_nremove = hammer2_vop_nremove,
2158 .vop_nrmdir = hammer2_vop_nrmdir,
2159 .vop_nrename = hammer2_vop_nrename,
2160 .vop_getattr = hammer2_vop_getattr,
2161 .vop_setattr = hammer2_vop_setattr,
2162 .vop_readdir = hammer2_vop_readdir,
2163 .vop_readlink = hammer2_vop_readlink,
2164 .vop_getpages = vop_stdgetpages,
2165 .vop_putpages = vop_stdputpages,
2166 .vop_read = hammer2_vop_read,
2167 .vop_write = hammer2_vop_write,
2168 .vop_open = hammer2_vop_open,
2169 .vop_inactive = hammer2_vop_inactive,
2170 .vop_reclaim = hammer2_vop_reclaim,
2171 .vop_nresolve = hammer2_vop_nresolve,
2172 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2173 .vop_nmkdir = hammer2_vop_nmkdir,
2174 .vop_nmknod = hammer2_vop_nmknod,
2175 .vop_ioctl = hammer2_vop_ioctl,
2176 .vop_mountctl = hammer2_vop_mountctl,
2177 .vop_bmap = hammer2_vop_bmap,
2178 .vop_strategy = hammer2_vop_strategy,
2179 .vop_kqfilter = hammer2_vop_kqfilter
2182 struct vop_ops hammer2_spec_vops = {
2183 .vop_default = vop_defaultop,
2184 .vop_fsync = hammer2_vop_fsync,
2185 .vop_read = vop_stdnoread,
2186 .vop_write = vop_stdnowrite,
2187 .vop_access = hammer2_vop_access,
2188 .vop_close = hammer2_vop_close,
2189 .vop_markatime = hammer2_vop_markatime,
2190 .vop_getattr = hammer2_vop_getattr,
2191 .vop_inactive = hammer2_vop_inactive,
2192 .vop_reclaim = hammer2_vop_reclaim,
2193 .vop_setattr = hammer2_vop_setattr
2196 struct vop_ops hammer2_fifo_vops = {
2197 .vop_default = fifo_vnoperate,
2198 .vop_fsync = hammer2_vop_fsync,
2200 .vop_read = hammer2_vop_fiforead,
2201 .vop_write = hammer2_vop_fifowrite,
2203 .vop_access = hammer2_vop_access,
2205 .vop_close = hammer2_vop_fifoclose,
2207 .vop_markatime = hammer2_vop_markatime,
2208 .vop_getattr = hammer2_vop_getattr,
2209 .vop_inactive = hammer2_vop_inactive,
2210 .vop_reclaim = hammer2_vop_reclaim,
2211 .vop_setattr = hammer2_vop_setattr,
2212 .vop_kqfilter = hammer2_vop_fifokqfilter