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 * This occurs if the inode was unlinked while open. Reclamation of
171 * these inodes requires processing we cannot safely do here so add
172 * the inode to the sideq in that situation.
174 * A modified inode may require chain synchronization which will no
175 * longer be driven by a sync or fsync without the vnode, also use
176 * the sideq for that.
178 * A reclaim can occur at any time so we cannot safely start a
179 * transaction to handle reclamation of unlinked files. Instead,
180 * 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_mtx_ex(&ip->truncate_lock);
429 hammer2_truncate_file(ip, vap->va_size);
430 hammer2_mtx_unlock(&ip->truncate_lock);
432 hammer2_extend_file(ip, vap->va_size);
434 hammer2_inode_modify(ip);
435 ip->meta.mtime = ctime;
443 /* atime not supported */
444 if (vap->va_atime.tv_sec != VNOVAL) {
445 hammer2_inode_modify(ip);
446 ip->meta.atime = hammer2_timespec_to_time(&vap->va_atime);
447 kflags |= NOTE_ATTRIB;
450 if (vap->va_mode != (mode_t)VNOVAL) {
451 mode_t cur_mode = ip->meta.mode;
452 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
453 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
455 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
456 cur_uid, cur_gid, &cur_mode);
457 if (error == 0 && ip->meta.mode != cur_mode) {
458 hammer2_inode_modify(ip);
459 ip->meta.mode = cur_mode;
460 ip->meta.ctime = ctime;
461 kflags |= NOTE_ATTRIB;
465 if (vap->va_mtime.tv_sec != VNOVAL) {
466 hammer2_inode_modify(ip);
467 ip->meta.mtime = hammer2_timespec_to_time(&vap->va_mtime);
468 kflags |= NOTE_ATTRIB;
473 * If a truncation occurred we must call inode_fsync() now in order
474 * to trim the related data chains, otherwise a later expansion can
477 * If an extend occured that changed the DIRECTDATA state, we must
478 * call inode_fsync now in order to prepare the inode's indirect
481 if (ip->flags & HAMMER2_INODE_RESIZED)
482 hammer2_inode_chain_sync(ip);
487 hammer2_inode_unlock(ip);
488 hammer2_trans_done(ip->pmp);
489 hammer2_knote(ip->vp, kflags);
497 hammer2_vop_readdir(struct vop_readdir_args *ap)
499 hammer2_xop_readdir_t *xop;
500 hammer2_blockref_t bref;
517 saveoff = uio->uio_offset;
522 * Setup cookies directory entry cookies if requested
524 if (ap->a_ncookies) {
525 ncookies = uio->uio_resid / 16 + 1;
528 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
535 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
538 * Handle artificial entries. To ensure that only positive 64 bit
539 * quantities are returned to userland we always strip off bit 63.
540 * The hash code is designed such that codes 0x0000-0x7FFF are not
541 * used, allowing us to use these codes for articial entries.
543 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
544 * allow '..' to cross the mount point into (e.g.) the super-root.
547 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
548 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
552 cookies[cookie_index] = saveoff;
555 if (cookie_index == ncookies)
561 * Be careful with lockorder when accessing ".."
563 * (ip is the current dir. xip is the parent dir).
565 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
566 if (ip != ip->pmp->iroot)
567 inum = ip->meta.iparent & HAMMER2_DIRHASH_USERMSK;
568 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
572 cookies[cookie_index] = saveoff;
575 if (cookie_index == ncookies)
579 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
580 if (hammer2_debug & 0x0020)
581 kprintf("readdir: lkey %016jx\n", lkey);
586 * Use XOP for cluster scan.
588 * parent is the inode cluster, already locked for us. Don't
589 * double lock shared locks as this will screw up upgrades.
591 xop = hammer2_xop_alloc(ip, 0);
593 hammer2_xop_start(&xop->head, hammer2_xop_readdir);
596 const hammer2_inode_data_t *ripdata;
598 error = hammer2_xop_collect(&xop->head, 0);
601 if (cookie_index == ncookies)
603 if (hammer2_debug & 0x0020)
604 kprintf("cluster chain %p %p\n",
605 xop->head.cluster.focus,
606 (xop->head.cluster.focus ?
607 xop->head.cluster.focus->data : (void *)-1));
608 ripdata = &hammer2_cluster_rdata(&xop->head.cluster)->ipdata;
609 hammer2_cluster_bref(&xop->head.cluster, &bref);
610 if (bref.type == HAMMER2_BREF_TYPE_INODE) {
611 dtype = hammer2_get_dtype(ripdata);
612 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
613 r = vop_write_dirent(&error, uio,
615 HAMMER2_DIRHASH_USERMSK,
617 ripdata->meta.name_len,
622 cookies[cookie_index] = saveoff;
625 /* XXX chain error */
626 kprintf("bad chain type readdir %d\n", bref.type);
629 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
630 if (error == ENOENT) {
633 saveoff = (hammer2_key_t)-1;
635 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
638 hammer2_inode_unlock(ip);
640 *ap->a_eofflag = eofflag;
641 if (hammer2_debug & 0x0020)
642 kprintf("readdir: done at %016jx\n", saveoff);
643 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
644 if (error && cookie_index == 0) {
646 kfree(cookies, M_TEMP);
648 *ap->a_cookies = NULL;
652 *ap->a_ncookies = cookie_index;
653 *ap->a_cookies = cookies;
661 * hammer2_vop_readlink { vp, uio, cred }
665 hammer2_vop_readlink(struct vop_readlink_args *ap)
672 if (vp->v_type != VLNK)
676 error = hammer2_read_file(ip, ap->a_uio, 0);
682 hammer2_vop_read(struct vop_read_args *ap)
692 * Read operations supported on this vnode?
695 if (vp->v_type != VREG)
705 seqcount = ap->a_ioflag >> 16;
706 bigread = (uio->uio_resid > 100 * 1024 * 1024);
708 error = hammer2_read_file(ip, uio, seqcount);
714 hammer2_vop_write(struct vop_write_args *ap)
724 * Read operations supported on this vnode?
727 if (vp->v_type != VREG)
736 if (ip->pmp->ronly) {
740 seqcount = ap->a_ioflag >> 16;
743 * Check resource limit
745 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
746 uio->uio_offset + uio->uio_resid >
747 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
748 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
753 * The transaction interlocks against flushes initiations
754 * (note: but will run concurrently with the actual flush).
756 hammer2_trans_init(ip->pmp, 0);
757 error = hammer2_write_file(ip, uio, ap->a_ioflag, seqcount);
758 hammer2_trans_done(ip->pmp);
764 * Perform read operations on a file or symlink given an UNLOCKED
767 * The passed ip is not locked.
771 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
782 * WARNING! Assumes that the kernel interlocks size changes at the
785 hammer2_mtx_sh(&ip->lock);
786 hammer2_mtx_sh(&ip->truncate_lock);
787 size = ip->meta.size;
788 hammer2_mtx_unlock(&ip->lock);
790 while (uio->uio_resid > 0 && uio->uio_offset < size) {
797 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
801 error = cluster_read(ip->vp, leof, lbase, lblksize,
802 uio->uio_resid, seqcount * BKVASIZE,
805 if (uio->uio_segflg == UIO_NOCOPY) {
806 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
807 if (bp->b_flags & B_CACHE) {
810 if (bp->b_xio.xio_npages != 16)
811 kprintf("NPAGES BAD\n");
812 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
814 m = bp->b_xio.xio_pages[i];
815 if (m == NULL || m->valid == 0) {
816 kprintf("bp %016jx %016jx pg %d inv",
819 kprintf("m->object %p/%p", m->object, ip->vp->v_object);
825 kprintf("b_flags %08x, b_error %d\n", bp->b_flags, bp->b_error);
829 error = bread(ip->vp, lbase, lblksize, &bp);
835 loff = (int)(uio->uio_offset - lbase);
837 if (n > uio->uio_resid)
839 if (n > size - uio->uio_offset)
840 n = (int)(size - uio->uio_offset);
841 bp->b_flags |= B_AGE;
842 uiomovebp(bp, (char *)bp->b_data + loff, n, uio);
845 hammer2_mtx_unlock(&ip->truncate_lock);
851 * Write to the file represented by the inode via the logical buffer cache.
852 * The inode may represent a regular file or a symlink.
854 * The inode must not be locked.
858 hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
859 int ioflag, int seqcount)
861 hammer2_key_t old_eof;
862 hammer2_key_t new_eof;
871 * WARNING! Assumes that the kernel interlocks size changes at the
874 hammer2_mtx_ex(&ip->lock);
875 hammer2_mtx_sh(&ip->truncate_lock);
876 if (ioflag & IO_APPEND)
877 uio->uio_offset = ip->meta.size;
878 old_eof = ip->meta.size;
881 * Extend the file if necessary. If the write fails at some point
882 * we will truncate it back down to cover as much as we were able
885 * Doing this now makes it easier to calculate buffer sizes in
892 if (uio->uio_offset + uio->uio_resid > old_eof) {
893 new_eof = uio->uio_offset + uio->uio_resid;
895 hammer2_extend_file(ip, new_eof);
896 kflags |= NOTE_EXTEND;
900 hammer2_mtx_unlock(&ip->lock);
905 while (uio->uio_resid > 0) {
914 * Don't allow the buffer build to blow out the buffer
917 if ((ioflag & IO_RECURSE) == 0)
918 bwillwrite(HAMMER2_PBUFSIZE);
921 * This nominally tells us how much we can cluster and
922 * what the logical buffer size needs to be. Currently
923 * we don't try to cluster the write and just handle one
926 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
928 loff = (int)(uio->uio_offset - lbase);
930 KKASSERT(lblksize <= 65536);
933 * Calculate bytes to copy this transfer and whether the
934 * copy completely covers the buffer or not.
938 if (n > uio->uio_resid) {
940 if (loff == lbase && uio->uio_offset + n == new_eof)
948 if (lbase >= new_eof)
954 if (uio->uio_segflg == UIO_NOCOPY) {
956 * Issuing a write with the same data backing the
957 * buffer. Instantiate the buffer to collect the
958 * backing vm pages, then read-in any missing bits.
960 * This case is used by vop_stdputpages().
962 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
963 if ((bp->b_flags & B_CACHE) == 0) {
965 error = bread(ip->vp, lbase, lblksize, &bp);
967 } else if (trivial) {
969 * Even though we are entirely overwriting the buffer
970 * we may still have to zero it out to avoid a
971 * mmap/write visibility issue.
973 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
974 if ((bp->b_flags & B_CACHE) == 0)
978 * Partial overwrite, read in any missing bits then
979 * replace the portion being written.
981 * (The strategy code will detect zero-fill physical
982 * blocks for this case).
984 error = bread(ip->vp, lbase, lblksize, &bp);
995 * Ok, copy the data in
997 error = uiomovebp(bp, bp->b_data + loff, n, uio);
998 kflags |= NOTE_WRITE;
1006 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
1007 * with IO_SYNC or IO_ASYNC set. These writes
1008 * must be handled as the pageout daemon expects.
1010 * NOTE! H2 relies on cluster_write() here because it
1011 * cannot preallocate disk blocks at the logical
1012 * level due to not knowing what the compression
1013 * size will be at this time.
1015 * We must use cluster_write() here and we depend
1016 * on the write-behind feature to flush buffers
1017 * appropriately. If we let the buffer daemons do
1018 * it the block allocations will be all over the
1021 if (ioflag & IO_SYNC) {
1023 } else if ((ioflag & IO_DIRECT) && endofblk) {
1025 } else if (ioflag & IO_ASYNC) {
1027 } else if (ip->vp->v_mount->mnt_flag & MNT_NOCLUSTERW) {
1031 bp->b_flags |= B_CLUSTEROK;
1032 cluster_write(bp, new_eof, lblksize, seqcount);
1034 bp->b_flags |= B_CLUSTEROK;
1041 * Cleanup. If we extended the file EOF but failed to write through
1042 * the entire write is a failure and we have to back-up.
1044 if (error && new_eof != old_eof) {
1045 hammer2_mtx_unlock(&ip->truncate_lock);
1046 hammer2_mtx_ex(&ip->lock);
1047 hammer2_mtx_ex(&ip->truncate_lock);
1048 hammer2_truncate_file(ip, old_eof);
1049 if (ip->flags & HAMMER2_INODE_MODIFIED)
1050 hammer2_inode_chain_sync(ip);
1051 hammer2_mtx_unlock(&ip->lock);
1052 } else if (modified) {
1053 hammer2_mtx_ex(&ip->lock);
1054 hammer2_inode_modify(ip);
1055 hammer2_update_time(&ip->meta.mtime);
1056 if (ip->flags & HAMMER2_INODE_MODIFIED)
1057 hammer2_inode_chain_sync(ip);
1058 hammer2_mtx_unlock(&ip->lock);
1059 hammer2_knote(ip->vp, kflags);
1061 hammer2_trans_assert_strategy(ip->pmp);
1062 hammer2_mtx_unlock(&ip->truncate_lock);
1068 * Truncate the size of a file. The inode must not be locked.
1070 * We must unconditionally set HAMMER2_INODE_RESIZED to properly
1071 * ensure that any on-media data beyond the new file EOF has been destroyed.
1073 * WARNING: nvtruncbuf() can only be safely called without the inode lock
1074 * held due to the way our write thread works. If the truncation
1075 * occurs in the middle of a buffer, nvtruncbuf() is responsible
1076 * for dirtying that buffer and zeroing out trailing bytes.
1078 * WARNING! Assumes that the kernel interlocks size changes at the
1081 * WARNING! Caller assumes responsibility for removing dead blocks
1082 * if INODE_RESIZED is set.
1086 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1088 hammer2_key_t lbase;
1092 hammer2_mtx_unlock(&ip->lock);
1094 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1095 nvtruncbuf(ip->vp, nsize,
1096 nblksize, (int)nsize & (nblksize - 1),
1099 hammer2_mtx_ex(&ip->lock);
1100 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1101 ip->osize = ip->meta.size;
1102 ip->meta.size = nsize;
1103 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1104 hammer2_inode_modify(ip);
1109 * Extend the size of a file. The inode must not be locked.
1111 * Even though the file size is changing, we do not have to set the
1112 * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES
1113 * boundary. When this occurs a hammer2_inode_chain_sync() is required
1114 * to prepare the inode cluster's indirect block table, otherwise
1115 * async execution of the strategy code will implode on us.
1117 * WARNING! Assumes that the kernel interlocks size changes at the
1120 * WARNING! Caller assumes responsibility for transitioning out
1121 * of the inode DIRECTDATA mode if INODE_RESIZED is set.
1125 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1127 hammer2_key_t lbase;
1128 hammer2_key_t osize;
1134 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1135 hammer2_inode_modify(ip);
1136 osize = ip->meta.size;
1138 ip->meta.size = nsize;
1140 if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES) {
1141 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1142 hammer2_inode_chain_sync(ip);
1145 hammer2_mtx_unlock(&ip->lock);
1147 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1148 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1154 hammer2_mtx_ex(&ip->lock);
1161 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1163 hammer2_xop_nresolve_t *xop;
1164 hammer2_inode_t *ip;
1165 hammer2_inode_t *dip;
1166 struct namecache *ncp;
1171 dip = VTOI(ap->a_dvp);
1172 xop = hammer2_xop_alloc(dip, 0);
1174 ncp = ap->a_nch->ncp;
1175 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1178 * Note: In DragonFly the kernel handles '.' and '..'.
1180 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1181 hammer2_xop_start(&xop->head, hammer2_xop_nresolve);
1183 error = hammer2_xop_collect(&xop->head, 0);
1187 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1189 hammer2_inode_unlock(dip);
1192 * Acquire the related vnode
1194 * NOTE: For error processing, only ENOENT resolves the namecache
1195 * entry to NULL, otherwise we just return the error and
1196 * leave the namecache unresolved.
1198 * NOTE: multiple hammer2_inode structures can be aliased to the
1199 * same chain element, for example for hardlinks. This
1200 * use case does not 'reattach' inode associations that
1201 * might already exist, but always allocates a new one.
1203 * WARNING: inode structure is locked exclusively via inode_get
1204 * but chain was locked shared. inode_unlock()
1205 * will handle it properly.
1208 vp = hammer2_igetv(ip, &error);
1211 cache_setvp(ap->a_nch, vp);
1212 } else if (error == ENOENT) {
1213 cache_setvp(ap->a_nch, NULL);
1215 hammer2_inode_unlock(ip);
1218 * The vp should not be released until after we've disposed
1219 * of our locks, because it might cause vop_inactive() to
1226 cache_setvp(ap->a_nch, NULL);
1228 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1229 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1230 ("resolve error %d/%p ap %p\n",
1231 error, ap->a_nch->ncp->nc_vp, ap));
1239 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1241 hammer2_inode_t *dip;
1246 dip = VTOI(ap->a_dvp);
1247 inum = dip->meta.iparent;
1251 error = hammer2_vfs_vget(ap->a_dvp->v_mount, NULL,
1262 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1264 hammer2_inode_t *dip;
1265 hammer2_inode_t *nip;
1266 struct namecache *ncp;
1267 const uint8_t *name;
1273 dip = VTOI(ap->a_dvp);
1274 if (dip->pmp->ronly) {
1279 ncp = ap->a_nch->ncp;
1280 name = ncp->nc_name;
1281 name_len = ncp->nc_nlen;
1283 hammer2_pfs_memory_wait(dip->pmp);
1284 hammer2_trans_init(dip->pmp, 0);
1286 inum = hammer2_trans_newinum(dip->pmp);
1289 * Create the actual inode as a hidden file in the iroot, then
1290 * create the directory entry as a hardlink to it. The creation
1291 * of the actual inode sets its nlinks to 1 which is the value
1294 nip = hammer2_inode_create(dip->pmp->iroot, dip, ap->a_vap, ap->a_cred,
1299 hammer2_inode_create(dip, dip, NULL, NULL,
1302 HAMMER2_OBJTYPE_HARDLINK, nip->meta.type,
1307 KKASSERT(nip == NULL);
1310 *ap->a_vpp = hammer2_igetv(nip, &error);
1311 hammer2_inode_unlock(nip);
1315 * Update dip's mtime
1320 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1321 hammer2_update_time(&mtime);
1322 hammer2_inode_modify(dip);
1323 dip->meta.mtime = mtime;
1324 hammer2_inode_unlock(dip);
1327 hammer2_trans_done(dip->pmp);
1330 cache_setunresolved(ap->a_nch);
1331 cache_setvp(ap->a_nch, *ap->a_vpp);
1339 hammer2_vop_open(struct vop_open_args *ap)
1341 return vop_stdopen(ap);
1345 * hammer2_vop_advlock { vp, id, op, fl, flags }
1349 hammer2_vop_advlock(struct vop_advlock_args *ap)
1351 hammer2_inode_t *ip = VTOI(ap->a_vp);
1354 size = ip->meta.size;
1355 return (lf_advlock(ap, &ip->advlock, size));
1360 hammer2_vop_close(struct vop_close_args *ap)
1362 return vop_stdclose(ap);
1366 * hammer2_vop_nlink { nch, dvp, vp, cred }
1368 * Create a hardlink from (vp) to {dvp, nch}.
1372 hammer2_vop_nlink(struct vop_nlink_args *ap)
1374 hammer2_inode_t *tdip; /* target directory to create link in */
1375 hammer2_inode_t *ip; /* inode we are hardlinking to */
1376 struct namecache *ncp;
1377 const uint8_t *name;
1382 tdip = VTOI(ap->a_dvp);
1383 if (tdip->pmp->ronly) {
1388 ncp = ap->a_nch->ncp;
1389 name = ncp->nc_name;
1390 name_len = ncp->nc_nlen;
1393 * ip represents the file being hardlinked. The file could be a
1394 * normal file or a hardlink target if it has already been hardlinked.
1395 * (with the new semantics, it will almost always be a hardlink
1398 * Bump nlinks and potentially also create or move the hardlink
1399 * target in the parent directory common to (ip) and (tdip). The
1400 * consolidation code can modify ip->cluster. The returned cluster
1403 ip = VTOI(ap->a_vp);
1404 hammer2_pfs_memory_wait(ip->pmp);
1405 hammer2_trans_init(ip->pmp, 0);
1408 * Target should be an indexed inode or there's no way we will ever
1409 * be able to find it!
1411 KKASSERT((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0);
1416 * Can return NULL and error == EXDEV if the common parent
1417 * crosses a directory with the xlink flag set.
1419 hammer2_inode_lock(tdip, 0);
1420 hammer2_inode_lock(ip, 0);
1423 * Create the hardlink target and bump nlinks.
1426 hammer2_inode_create(tdip, tdip, NULL, NULL,
1429 HAMMER2_OBJTYPE_HARDLINK, ip->meta.type,
1431 hammer2_inode_modify(ip);
1436 * Update dip's mtime
1440 hammer2_update_time(&mtime);
1441 hammer2_inode_modify(tdip);
1442 tdip->meta.mtime = mtime;
1444 cache_setunresolved(ap->a_nch);
1445 cache_setvp(ap->a_nch, ap->a_vp);
1447 hammer2_inode_unlock(ip);
1448 hammer2_inode_unlock(tdip);
1450 hammer2_trans_done(ip->pmp);
1457 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1459 * The operating system has already ensured that the directory entry
1460 * does not exist and done all appropriate namespace locking.
1464 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1466 hammer2_inode_t *dip;
1467 hammer2_inode_t *nip;
1468 struct namecache *ncp;
1469 const uint8_t *name;
1475 dip = VTOI(ap->a_dvp);
1476 if (dip->pmp->ronly) {
1481 ncp = ap->a_nch->ncp;
1482 name = ncp->nc_name;
1483 name_len = ncp->nc_nlen;
1484 hammer2_pfs_memory_wait(dip->pmp);
1485 hammer2_trans_init(dip->pmp, 0);
1487 inum = hammer2_trans_newinum(dip->pmp);
1490 * Create the actual inode as a hidden file in the iroot, then
1491 * create the directory entry as a hardlink to it. The creation
1492 * of the actual inode sets its nlinks to 1 which is the value
1495 nip = hammer2_inode_create(dip->pmp->iroot, dip, ap->a_vap, ap->a_cred,
1501 hammer2_inode_create(dip, dip, NULL, NULL,
1504 HAMMER2_OBJTYPE_HARDLINK, nip->meta.type,
1508 KKASSERT(nip == NULL);
1511 *ap->a_vpp = hammer2_igetv(nip, &error);
1512 hammer2_inode_unlock(nip);
1516 * Update dip's mtime
1521 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1522 hammer2_update_time(&mtime);
1523 hammer2_inode_modify(dip);
1524 dip->meta.mtime = mtime;
1525 hammer2_inode_unlock(dip);
1528 hammer2_trans_done(dip->pmp);
1531 cache_setunresolved(ap->a_nch);
1532 cache_setvp(ap->a_nch, *ap->a_vpp);
1539 * Make a device node (typically a fifo)
1543 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1545 hammer2_inode_t *dip;
1546 hammer2_inode_t *nip;
1547 struct namecache *ncp;
1548 const uint8_t *name;
1554 dip = VTOI(ap->a_dvp);
1555 if (dip->pmp->ronly) {
1560 ncp = ap->a_nch->ncp;
1561 name = ncp->nc_name;
1562 name_len = ncp->nc_nlen;
1563 hammer2_pfs_memory_wait(dip->pmp);
1564 hammer2_trans_init(dip->pmp, 0);
1567 * The device node is entered as the directory entry itself and not
1568 * as a hardlink to an inode. Since one cannot obtain a
1569 * file handle on the filesystem entry representing the device, we
1570 * do not have to worry about indexing its inode.
1572 inum = hammer2_trans_newinum(dip->pmp);
1573 nip = hammer2_inode_create(dip->pmp->iroot, dip, ap->a_vap, ap->a_cred,
1578 hammer2_inode_create(dip, dip, NULL, NULL,
1581 HAMMER2_OBJTYPE_HARDLINK, nip->meta.type,
1587 KKASSERT(nip == NULL);
1590 *ap->a_vpp = hammer2_igetv(nip, &error);
1591 hammer2_inode_unlock(nip);
1595 * Update dip's mtime
1600 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1601 hammer2_update_time(&mtime);
1602 hammer2_inode_modify(dip);
1603 dip->meta.mtime = mtime;
1604 hammer2_inode_unlock(dip);
1607 hammer2_trans_done(dip->pmp);
1610 cache_setunresolved(ap->a_nch);
1611 cache_setvp(ap->a_nch, *ap->a_vpp);
1618 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1622 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1624 hammer2_inode_t *dip;
1625 hammer2_inode_t *nip;
1626 struct namecache *ncp;
1627 const uint8_t *name;
1632 dip = VTOI(ap->a_dvp);
1633 if (dip->pmp->ronly)
1636 ncp = ap->a_nch->ncp;
1637 name = ncp->nc_name;
1638 name_len = ncp->nc_nlen;
1639 hammer2_pfs_memory_wait(dip->pmp);
1640 hammer2_trans_init(dip->pmp, 0);
1642 ap->a_vap->va_type = VLNK; /* enforce type */
1645 * The softlink is entered into the directory itself and not
1646 * as a hardlink to an inode. Since one cannot obtain a
1647 * file handle on the softlink itself we do not have to worry
1648 * about indexing its inode.
1650 inum = hammer2_trans_newinum(dip->pmp);
1652 nip = hammer2_inode_create(dip->pmp->iroot, dip, ap->a_vap, ap->a_cred,
1657 hammer2_inode_create(dip, dip, NULL, NULL,
1660 HAMMER2_OBJTYPE_HARDLINK, nip->meta.type,
1666 KKASSERT(nip == NULL);
1668 hammer2_trans_done(dip->pmp);
1671 *ap->a_vpp = hammer2_igetv(nip, &error);
1674 * Build the softlink (~like file data) and finalize the namecache.
1681 bytes = strlen(ap->a_target);
1683 hammer2_inode_unlock(nip);
1684 bzero(&auio, sizeof(auio));
1685 bzero(&aiov, sizeof(aiov));
1686 auio.uio_iov = &aiov;
1687 auio.uio_segflg = UIO_SYSSPACE;
1688 auio.uio_rw = UIO_WRITE;
1689 auio.uio_resid = bytes;
1690 auio.uio_iovcnt = 1;
1691 auio.uio_td = curthread;
1692 aiov.iov_base = ap->a_target;
1693 aiov.iov_len = bytes;
1694 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1695 /* XXX handle error */
1698 hammer2_inode_unlock(nip);
1702 * Update dip's mtime
1707 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1708 hammer2_update_time(&mtime);
1709 hammer2_inode_modify(dip);
1710 dip->meta.mtime = mtime;
1711 hammer2_inode_unlock(dip);
1714 hammer2_trans_done(dip->pmp);
1717 * Finalize namecache
1720 cache_setunresolved(ap->a_nch);
1721 cache_setvp(ap->a_nch, *ap->a_vpp);
1722 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1728 * hammer2_vop_nremove { nch, dvp, cred }
1732 hammer2_vop_nremove(struct vop_nremove_args *ap)
1734 hammer2_xop_unlink_t *xop;
1735 hammer2_inode_t *dip;
1736 hammer2_inode_t *ip;
1737 struct namecache *ncp;
1742 dip = VTOI(ap->a_dvp);
1743 if (dip->pmp->ronly) {
1748 ncp = ap->a_nch->ncp;
1750 hammer2_pfs_memory_wait(dip->pmp);
1751 hammer2_trans_init(dip->pmp, 0);
1752 hammer2_inode_lock(dip, 0);
1755 * The unlink XOP unlinks the path from the directory and
1756 * locates and returns the cluster associated with the real inode.
1757 * We have to handle nlinks here on the frontend.
1759 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1760 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1761 isopen = cache_isopen(ap->a_nch);
1763 xop->dopermanent = 0;
1764 hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1767 * Collect the real inode and adjust nlinks, destroy the real
1768 * inode if nlinks transitions to 0 and it was the real inode
1769 * (else it has already been removed).
1771 error = hammer2_xop_collect(&xop->head, 0);
1772 hammer2_inode_unlock(dip);
1775 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1776 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1778 hammer2_inode_unlink_finisher(ip, isopen);
1779 hammer2_inode_unlock(ip);
1782 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1786 * Update dip's mtime
1791 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1792 hammer2_update_time(&mtime);
1793 hammer2_inode_modify(dip);
1794 dip->meta.mtime = mtime;
1795 hammer2_inode_unlock(dip);
1798 hammer2_inode_run_sideq(dip->pmp);
1799 hammer2_trans_done(dip->pmp);
1801 cache_unlink(ap->a_nch);
1807 * hammer2_vop_nrmdir { nch, dvp, cred }
1811 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1813 hammer2_xop_unlink_t *xop;
1814 hammer2_inode_t *dip;
1815 hammer2_inode_t *ip;
1816 struct namecache *ncp;
1821 dip = VTOI(ap->a_dvp);
1822 if (dip->pmp->ronly) {
1827 hammer2_pfs_memory_wait(dip->pmp);
1828 hammer2_trans_init(dip->pmp, 0);
1829 hammer2_inode_lock(dip, 0);
1831 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1833 ncp = ap->a_nch->ncp;
1834 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1835 isopen = cache_isopen(ap->a_nch);
1837 xop->dopermanent = 0;
1838 hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1841 * Collect the real inode and adjust nlinks, destroy the real
1842 * inode if nlinks transitions to 0 and it was the real inode
1843 * (else it has already been removed).
1845 error = hammer2_xop_collect(&xop->head, 0);
1846 hammer2_inode_unlock(dip);
1849 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1850 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1852 hammer2_inode_unlink_finisher(ip, isopen);
1853 hammer2_inode_unlock(ip);
1856 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1860 * Update dip's mtime
1865 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1866 hammer2_update_time(&mtime);
1867 hammer2_inode_modify(dip);
1868 dip->meta.mtime = mtime;
1869 hammer2_inode_unlock(dip);
1872 hammer2_inode_run_sideq(dip->pmp);
1873 hammer2_trans_done(dip->pmp);
1875 cache_unlink(ap->a_nch);
1881 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1885 hammer2_vop_nrename(struct vop_nrename_args *ap)
1887 struct namecache *fncp;
1888 struct namecache *tncp;
1889 hammer2_inode_t *fdip;
1890 hammer2_inode_t *tdip;
1891 hammer2_inode_t *ip;
1892 const uint8_t *fname;
1894 const uint8_t *tname;
1902 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1904 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1907 fdip = VTOI(ap->a_fdvp); /* source directory */
1908 tdip = VTOI(ap->a_tdvp); /* target directory */
1910 if (fdip->pmp->ronly)
1914 fncp = ap->a_fnch->ncp; /* entry name in source */
1915 fname = fncp->nc_name;
1916 fname_len = fncp->nc_nlen;
1918 tncp = ap->a_tnch->ncp; /* entry name in target */
1919 tname = tncp->nc_name;
1920 tname_len = tncp->nc_nlen;
1922 hammer2_pfs_memory_wait(tdip->pmp);
1923 hammer2_trans_init(tdip->pmp, 0);
1929 * ip is the inode being renamed. If this is a hardlink then
1930 * ip represents the actual file and not the hardlink marker.
1932 ip = VTOI(fncp->nc_vp);
1934 KKASSERT((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0);
1937 * Can return NULL and error == EXDEV if the common parent
1938 * crosses a directory with the xlink flag set.
1941 hammer2_inode_lock(fdip, 0);
1942 hammer2_inode_lock(tdip, 0);
1943 hammer2_inode_ref(ip); /* extra ref */
1945 hammer2_inode_lock(ip, 0);
1948 * Delete the target namespace.
1951 hammer2_xop_unlink_t *xop2;
1952 hammer2_inode_t *tip;
1956 * The unlink XOP unlinks the path from the directory and
1957 * locates and returns the cluster associated with the real
1958 * inode. We have to handle nlinks here on the frontend.
1960 xop2 = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
1961 hammer2_xop_setname(&xop2->head, tname, tname_len);
1962 isopen = cache_isopen(ap->a_tnch);
1964 xop2->dopermanent = 0;
1965 hammer2_xop_start(&xop2->head, hammer2_xop_unlink);
1968 * Collect the real inode and adjust nlinks, destroy the real
1969 * inode if nlinks transitions to 0 and it was the real inode
1970 * (else it has already been removed).
1972 tnch_error = hammer2_xop_collect(&xop2->head, 0);
1973 /* hammer2_inode_unlock(tdip); */
1975 if (tnch_error == 0) {
1976 tip = hammer2_inode_get(tdip->pmp, NULL,
1977 &xop2->head.cluster, -1);
1978 hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
1980 hammer2_inode_unlink_finisher(tip, isopen);
1981 hammer2_inode_unlock(tip);
1984 hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
1986 /* hammer2_inode_lock(tdip, 0); */
1988 if (tnch_error && tnch_error != ENOENT) {
1996 * Resolve the collision space for (tdip, tname, tname_len)
1998 * tdip must be held exclusively locked to prevent races.
2001 hammer2_xop_scanlhc_t *sxop;
2002 hammer2_tid_t lhcbase;
2004 tlhc = hammer2_dirhash(tname, tname_len);
2006 sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
2008 hammer2_xop_start(&sxop->head, hammer2_xop_scanlhc);
2009 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
2010 if (tlhc != sxop->head.cluster.focus->bref.key)
2014 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
2017 if (error != ENOENT)
2022 if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) {
2029 * Everything is setup, do the rename.
2031 * We have to synchronize ip->meta to the underlying operation.
2033 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
2034 * unlinking elements from their directories. Locking
2035 * the nlinks field does not lock the whole inode.
2037 /* hammer2_inode_lock(ip, 0); */
2039 hammer2_xop_nrename_t *xop4;
2041 xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
2043 xop4->ip_key = ip->meta.name_key;
2044 hammer2_xop_setip2(&xop4->head, ip);
2045 hammer2_xop_setip3(&xop4->head, tdip);
2046 hammer2_xop_setname(&xop4->head, fname, fname_len);
2047 hammer2_xop_setname2(&xop4->head, tname, tname_len);
2048 hammer2_xop_start(&xop4->head, hammer2_xop_nrename);
2050 error = hammer2_xop_collect(&xop4->head, 0);
2051 hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP);
2053 if (error == ENOENT)
2056 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
2057 hammer2_inode_modify(ip);
2058 ip->meta.name_len = tname_len;
2059 ip->meta.name_key = tlhc;
2068 * Update directory mtimes to represent the something changed.
2070 if (update_fdip || update_tdip) {
2073 hammer2_update_time(&mtime);
2075 hammer2_inode_modify(fdip);
2076 fdip->meta.mtime = mtime;
2079 hammer2_inode_modify(tdip);
2080 tdip->meta.mtime = mtime;
2083 hammer2_inode_unlock(ip);
2084 hammer2_inode_unlock(tdip);
2085 hammer2_inode_unlock(fdip);
2086 hammer2_inode_drop(ip);
2087 hammer2_inode_run_sideq(fdip->pmp);
2089 hammer2_trans_done(tdip->pmp);
2092 * Issue the namecache update after unlocking all the internal
2093 * hammer structures, otherwise we might deadlock.
2095 if (tnch_error == 0) {
2096 cache_unlink(ap->a_tnch);
2097 cache_setunresolved(ap->a_tnch);
2100 cache_rename(ap->a_fnch, ap->a_tnch);
2107 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2111 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2113 hammer2_inode_t *ip;
2117 ip = VTOI(ap->a_vp);
2119 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2120 ap->a_fflag, ap->a_cred);
2127 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2135 case (MOUNTCTL_SET_EXPORT):
2136 mp = ap->a_head.a_ops->head.vv_mount;
2139 if (ap->a_ctllen != sizeof(struct export_args))
2142 rc = vfs_export(mp, &pmp->export,
2143 (const struct export_args *)ap->a_ctl);
2146 rc = vop_stdmountctl(ap);
2156 static void filt_hammer2detach(struct knote *kn);
2157 static int filt_hammer2read(struct knote *kn, long hint);
2158 static int filt_hammer2write(struct knote *kn, long hint);
2159 static int filt_hammer2vnode(struct knote *kn, long hint);
2161 static struct filterops hammer2read_filtops =
2162 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2163 NULL, filt_hammer2detach, filt_hammer2read };
2164 static struct filterops hammer2write_filtops =
2165 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2166 NULL, filt_hammer2detach, filt_hammer2write };
2167 static struct filterops hammer2vnode_filtops =
2168 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2169 NULL, filt_hammer2detach, filt_hammer2vnode };
2173 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
2175 struct vnode *vp = ap->a_vp;
2176 struct knote *kn = ap->a_kn;
2178 switch (kn->kn_filter) {
2180 kn->kn_fop = &hammer2read_filtops;
2183 kn->kn_fop = &hammer2write_filtops;
2186 kn->kn_fop = &hammer2vnode_filtops;
2189 return (EOPNOTSUPP);
2192 kn->kn_hook = (caddr_t)vp;
2194 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2200 filt_hammer2detach(struct knote *kn)
2202 struct vnode *vp = (void *)kn->kn_hook;
2204 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2208 filt_hammer2read(struct knote *kn, long hint)
2210 struct vnode *vp = (void *)kn->kn_hook;
2211 hammer2_inode_t *ip = VTOI(vp);
2214 if (hint == NOTE_REVOKE) {
2215 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2218 off = ip->meta.size - kn->kn_fp->f_offset;
2219 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2220 if (kn->kn_sfflags & NOTE_OLDAPI)
2222 return (kn->kn_data != 0);
2227 filt_hammer2write(struct knote *kn, long hint)
2229 if (hint == NOTE_REVOKE)
2230 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2236 filt_hammer2vnode(struct knote *kn, long hint)
2238 if (kn->kn_sfflags & hint)
2239 kn->kn_fflags |= hint;
2240 if (hint == NOTE_REVOKE) {
2241 kn->kn_flags |= (EV_EOF | EV_NODATA);
2244 return (kn->kn_fflags != 0);
2252 hammer2_vop_markatime(struct vop_markatime_args *ap)
2254 hammer2_inode_t *ip;
2267 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2271 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2273 error = hammer2_vop_kqfilter(ap);
2280 struct vop_ops hammer2_vnode_vops = {
2281 .vop_default = vop_defaultop,
2282 .vop_fsync = hammer2_vop_fsync,
2283 .vop_getpages = vop_stdgetpages,
2284 .vop_putpages = vop_stdputpages,
2285 .vop_access = hammer2_vop_access,
2286 .vop_advlock = hammer2_vop_advlock,
2287 .vop_close = hammer2_vop_close,
2288 .vop_nlink = hammer2_vop_nlink,
2289 .vop_ncreate = hammer2_vop_ncreate,
2290 .vop_nsymlink = hammer2_vop_nsymlink,
2291 .vop_nremove = hammer2_vop_nremove,
2292 .vop_nrmdir = hammer2_vop_nrmdir,
2293 .vop_nrename = hammer2_vop_nrename,
2294 .vop_getattr = hammer2_vop_getattr,
2295 .vop_setattr = hammer2_vop_setattr,
2296 .vop_readdir = hammer2_vop_readdir,
2297 .vop_readlink = hammer2_vop_readlink,
2298 .vop_getpages = vop_stdgetpages,
2299 .vop_putpages = vop_stdputpages,
2300 .vop_read = hammer2_vop_read,
2301 .vop_write = hammer2_vop_write,
2302 .vop_open = hammer2_vop_open,
2303 .vop_inactive = hammer2_vop_inactive,
2304 .vop_reclaim = hammer2_vop_reclaim,
2305 .vop_nresolve = hammer2_vop_nresolve,
2306 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2307 .vop_nmkdir = hammer2_vop_nmkdir,
2308 .vop_nmknod = hammer2_vop_nmknod,
2309 .vop_ioctl = hammer2_vop_ioctl,
2310 .vop_mountctl = hammer2_vop_mountctl,
2311 .vop_bmap = hammer2_vop_bmap,
2312 .vop_strategy = hammer2_vop_strategy,
2313 .vop_kqfilter = hammer2_vop_kqfilter
2316 struct vop_ops hammer2_spec_vops = {
2317 .vop_default = vop_defaultop,
2318 .vop_fsync = hammer2_vop_fsync,
2319 .vop_read = vop_stdnoread,
2320 .vop_write = vop_stdnowrite,
2321 .vop_access = hammer2_vop_access,
2322 .vop_close = hammer2_vop_close,
2323 .vop_markatime = hammer2_vop_markatime,
2324 .vop_getattr = hammer2_vop_getattr,
2325 .vop_inactive = hammer2_vop_inactive,
2326 .vop_reclaim = hammer2_vop_reclaim,
2327 .vop_setattr = hammer2_vop_setattr
2330 struct vop_ops hammer2_fifo_vops = {
2331 .vop_default = fifo_vnoperate,
2332 .vop_fsync = hammer2_vop_fsync,
2334 .vop_read = hammer2_vop_fiforead,
2335 .vop_write = hammer2_vop_fifowrite,
2337 .vop_access = hammer2_vop_access,
2339 .vop_close = hammer2_vop_fifoclose,
2341 .vop_markatime = hammer2_vop_markatime,
2342 .vop_getattr = hammer2_vop_getattr,
2343 .vop_inactive = hammer2_vop_inactive,
2344 .vop_reclaim = hammer2_vop_reclaim,
2345 .vop_setattr = hammer2_vop_setattr,
2346 .vop_kqfilter = hammer2_vop_fifokqfilter