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
185 if ((ip->flags & (HAMMER2_INODE_ISUNLINKED |
186 HAMMER2_INODE_MODIFIED |
187 HAMMER2_INODE_RESIZED)) &&
188 (ip->flags & HAMMER2_INODE_ISDELETED) == 0) {
189 hammer2_inode_sideq_t *ipul;
191 ipul = kmalloc(sizeof(*ipul), pmp->minode, M_WAITOK | M_ZERO);
194 hammer2_spin_ex(&pmp->list_spin);
195 if ((ip->flags & HAMMER2_INODE_ONSIDEQ) == 0) {
197 atomic_set_int(&ip->flags, HAMMER2_INODE_ONSIDEQ);
198 TAILQ_INSERT_TAIL(&pmp->sideq, ipul, entry);
199 hammer2_spin_unex(&pmp->list_spin);
201 hammer2_spin_unex(&pmp->list_spin);
202 kfree(ipul, pmp->minode);
203 hammer2_inode_drop(ip); /* vp ref */
205 /* retain ref from vp for ipul */
207 hammer2_inode_drop(ip); /* vp ref */
211 * XXX handle background sync when ip dirty, kernel will no longer
212 * notify us regarding this inode because there is no longer a
213 * vnode attached to it.
222 hammer2_vop_fsync(struct vop_fsync_args *ap)
232 /* XXX can't do this yet */
233 hammer2_trans_init(ip->pmp, HAMMER2_TRANS_ISFLUSH);
234 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
236 hammer2_trans_init(ip->pmp, 0);
237 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
240 * Calling chain_flush here creates a lot of duplicative
241 * COW operations due to non-optimal vnode ordering.
243 * Only do it for an actual fsync() syscall. The other forms
244 * which call this function will eventually call chain_flush
245 * on the volume root as a catch-all, which is far more optimal.
247 hammer2_inode_lock(ip, 0);
248 if (ip->flags & HAMMER2_INODE_MODIFIED)
249 hammer2_inode_chain_sync(ip);
250 hammer2_inode_unlock(ip);
251 hammer2_trans_done(ip->pmp);
259 hammer2_vop_access(struct vop_access_args *ap)
261 hammer2_inode_t *ip = VTOI(ap->a_vp);
267 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
268 uid = hammer2_to_unix_xid(&ip->meta.uid);
269 gid = hammer2_to_unix_xid(&ip->meta.gid);
270 error = vop_helper_access(ap, uid, gid, ip->meta.mode, ip->meta.uflags);
271 hammer2_inode_unlock(ip);
279 hammer2_vop_getattr(struct vop_getattr_args *ap)
285 hammer2_chain_t *chain;
295 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
297 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
298 vap->va_fileid = ip->meta.inum;
299 vap->va_mode = ip->meta.mode;
300 vap->va_nlink = ip->meta.nlinks;
301 vap->va_uid = hammer2_to_unix_xid(&ip->meta.uid);
302 vap->va_gid = hammer2_to_unix_xid(&ip->meta.gid);
305 vap->va_size = ip->meta.size; /* protected by shared lock */
306 vap->va_blocksize = HAMMER2_PBUFSIZE;
307 vap->va_flags = ip->meta.uflags;
308 hammer2_time_to_timespec(ip->meta.ctime, &vap->va_ctime);
309 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_mtime);
310 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_atime);
313 if (ip->meta.type == HAMMER2_OBJTYPE_DIRECTORY) {
315 * Can't really calculate directory use sans the files under
316 * it, just assume one block for now.
318 vap->va_bytes += HAMMER2_INODE_BYTES;
320 for (i = 0; i < ip->cluster.nchains; ++i) {
321 if ((chain = ip->cluster.array[i].chain) != NULL) {
322 if (vap->va_bytes < chain->bref.data_count)
323 vap->va_bytes = chain->bref.data_count;
327 vap->va_type = hammer2_get_vtype(ip->meta.type);
329 vap->va_uid_uuid = ip->meta.uid;
330 vap->va_gid_uuid = ip->meta.gid;
331 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
334 hammer2_inode_unlock(ip);
342 hammer2_vop_setattr(struct vop_setattr_args *ap)
354 hammer2_update_time(&ctime);
358 if (ip->pmp->ronly) {
363 hammer2_pfs_memory_wait(ip->pmp);
364 hammer2_trans_init(ip->pmp, 0);
365 hammer2_inode_lock(ip, 0);
368 if (vap->va_flags != VNOVAL) {
371 flags = ip->meta.uflags;
372 error = vop_helper_setattr_flags(&flags, vap->va_flags,
373 hammer2_to_unix_xid(&ip->meta.uid),
376 if (ip->meta.uflags != flags) {
377 hammer2_inode_modify(ip);
378 ip->meta.uflags = flags;
379 ip->meta.ctime = ctime;
380 kflags |= NOTE_ATTRIB;
382 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
389 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
393 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
394 mode_t cur_mode = ip->meta.mode;
395 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
396 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
400 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
402 &cur_uid, &cur_gid, &cur_mode);
404 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
405 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
406 if (bcmp(&uuid_uid, &ip->meta.uid, sizeof(uuid_uid)) ||
407 bcmp(&uuid_gid, &ip->meta.gid, sizeof(uuid_gid)) ||
408 ip->meta.mode != cur_mode
410 hammer2_inode_modify(ip);
411 ip->meta.uid = uuid_uid;
412 ip->meta.gid = uuid_gid;
413 ip->meta.mode = cur_mode;
414 ip->meta.ctime = ctime;
416 kflags |= NOTE_ATTRIB;
423 if (vap->va_size != VNOVAL && ip->meta.size != vap->va_size) {
426 if (vap->va_size == ip->meta.size)
428 if (vap->va_size < ip->meta.size) {
429 hammer2_mtx_ex(&ip->truncate_lock);
430 hammer2_truncate_file(ip, vap->va_size);
431 hammer2_mtx_unlock(&ip->truncate_lock);
433 hammer2_extend_file(ip, vap->va_size);
435 hammer2_inode_modify(ip);
436 ip->meta.mtime = ctime;
444 /* atime not supported */
445 if (vap->va_atime.tv_sec != VNOVAL) {
446 hammer2_inode_modify(ip);
447 ip->meta.atime = hammer2_timespec_to_time(&vap->va_atime);
448 kflags |= NOTE_ATTRIB;
451 if (vap->va_mode != (mode_t)VNOVAL) {
452 mode_t cur_mode = ip->meta.mode;
453 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
454 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
456 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
457 cur_uid, cur_gid, &cur_mode);
458 if (error == 0 && ip->meta.mode != cur_mode) {
459 hammer2_inode_modify(ip);
460 ip->meta.mode = cur_mode;
461 ip->meta.ctime = ctime;
462 kflags |= NOTE_ATTRIB;
466 if (vap->va_mtime.tv_sec != VNOVAL) {
467 hammer2_inode_modify(ip);
468 ip->meta.mtime = hammer2_timespec_to_time(&vap->va_mtime);
469 kflags |= NOTE_ATTRIB;
474 * If a truncation occurred we must call inode_fsync() now in order
475 * to trim the related data chains, otherwise a later expansion can
478 * If an extend occured that changed the DIRECTDATA state, we must
479 * call inode_fsync now in order to prepare the inode's indirect
482 if (ip->flags & HAMMER2_INODE_RESIZED)
483 hammer2_inode_chain_sync(ip);
488 hammer2_inode_unlock(ip);
489 hammer2_trans_done(ip->pmp);
490 hammer2_knote(ip->vp, kflags);
498 hammer2_vop_readdir(struct vop_readdir_args *ap)
500 hammer2_xop_readdir_t *xop;
501 hammer2_blockref_t bref;
518 saveoff = uio->uio_offset;
523 * Setup cookies directory entry cookies if requested
525 if (ap->a_ncookies) {
526 ncookies = uio->uio_resid / 16 + 1;
529 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
536 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
539 * Handle artificial entries. To ensure that only positive 64 bit
540 * quantities are returned to userland we always strip off bit 63.
541 * The hash code is designed such that codes 0x0000-0x7FFF are not
542 * used, allowing us to use these codes for articial entries.
544 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
545 * allow '..' to cross the mount point into (e.g.) the super-root.
548 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
549 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
553 cookies[cookie_index] = saveoff;
556 if (cookie_index == ncookies)
562 * Be careful with lockorder when accessing ".."
564 * (ip is the current dir. xip is the parent dir).
566 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
567 if (ip != ip->pmp->iroot)
568 inum = ip->meta.iparent & HAMMER2_DIRHASH_USERMSK;
569 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
573 cookies[cookie_index] = saveoff;
576 if (cookie_index == ncookies)
580 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
581 if (hammer2_debug & 0x0020)
582 kprintf("readdir: lkey %016jx\n", lkey);
587 * Use XOP for cluster scan.
589 * parent is the inode cluster, already locked for us. Don't
590 * double lock shared locks as this will screw up upgrades.
592 xop = hammer2_xop_alloc(ip, 0);
594 hammer2_xop_start(&xop->head, hammer2_xop_readdir);
597 const hammer2_inode_data_t *ripdata;
599 error = hammer2_xop_collect(&xop->head, 0);
602 if (cookie_index == ncookies)
604 if (hammer2_debug & 0x0020)
605 kprintf("cluster chain %p %p\n",
606 xop->head.cluster.focus,
607 (xop->head.cluster.focus ?
608 xop->head.cluster.focus->data : (void *)-1));
609 ripdata = &hammer2_cluster_rdata(&xop->head.cluster)->ipdata;
610 hammer2_cluster_bref(&xop->head.cluster, &bref);
611 if (bref.type == HAMMER2_BREF_TYPE_INODE) {
612 dtype = hammer2_get_dtype(ripdata);
613 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
614 r = vop_write_dirent(&error, uio,
616 HAMMER2_DIRHASH_USERMSK,
618 ripdata->meta.name_len,
623 cookies[cookie_index] = saveoff;
626 /* XXX chain error */
627 kprintf("bad chain type readdir %d\n", bref.type);
630 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
631 if (error == ENOENT) {
634 saveoff = (hammer2_key_t)-1;
636 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
639 hammer2_inode_unlock(ip);
641 *ap->a_eofflag = eofflag;
642 if (hammer2_debug & 0x0020)
643 kprintf("readdir: done at %016jx\n", saveoff);
644 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
645 if (error && cookie_index == 0) {
647 kfree(cookies, M_TEMP);
649 *ap->a_cookies = NULL;
653 *ap->a_ncookies = cookie_index;
654 *ap->a_cookies = cookies;
662 * hammer2_vop_readlink { vp, uio, cred }
666 hammer2_vop_readlink(struct vop_readlink_args *ap)
673 if (vp->v_type != VLNK)
677 error = hammer2_read_file(ip, ap->a_uio, 0);
683 hammer2_vop_read(struct vop_read_args *ap)
693 * Read operations supported on this vnode?
696 if (vp->v_type != VREG)
706 seqcount = ap->a_ioflag >> 16;
707 bigread = (uio->uio_resid > 100 * 1024 * 1024);
709 error = hammer2_read_file(ip, uio, seqcount);
715 hammer2_vop_write(struct vop_write_args *ap)
725 * Read operations supported on this vnode?
728 if (vp->v_type != VREG)
737 if (ip->pmp->ronly) {
741 seqcount = ap->a_ioflag >> 16;
744 * Check resource limit
746 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
747 uio->uio_offset + uio->uio_resid >
748 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
749 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
754 * The transaction interlocks against flushes initiations
755 * (note: but will run concurrently with the actual flush).
757 hammer2_trans_init(ip->pmp, 0);
758 error = hammer2_write_file(ip, uio, ap->a_ioflag, seqcount);
759 hammer2_trans_done(ip->pmp);
765 * Perform read operations on a file or symlink given an UNLOCKED
768 * The passed ip is not locked.
772 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
783 * WARNING! Assumes that the kernel interlocks size changes at the
786 hammer2_mtx_sh(&ip->lock);
787 hammer2_mtx_sh(&ip->truncate_lock);
788 size = ip->meta.size;
789 hammer2_mtx_unlock(&ip->lock);
791 while (uio->uio_resid > 0 && uio->uio_offset < size) {
798 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
802 error = cluster_read(ip->vp, leof, lbase, lblksize,
803 uio->uio_resid, seqcount * BKVASIZE,
806 if (uio->uio_segflg == UIO_NOCOPY) {
807 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
808 if (bp->b_flags & B_CACHE) {
811 if (bp->b_xio.xio_npages != 16)
812 kprintf("NPAGES BAD\n");
813 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
815 m = bp->b_xio.xio_pages[i];
816 if (m == NULL || m->valid == 0) {
817 kprintf("bp %016jx %016jx pg %d inv",
820 kprintf("m->object %p/%p", m->object, ip->vp->v_object);
826 kprintf("b_flags %08x, b_error %d\n", bp->b_flags, bp->b_error);
830 error = bread(ip->vp, lbase, lblksize, &bp);
834 loff = (int)(uio->uio_offset - lbase);
836 if (n > uio->uio_resid)
838 if (n > size - uio->uio_offset)
839 n = (int)(size - uio->uio_offset);
840 bp->b_flags |= B_AGE;
841 uiomovebp(bp, (char *)bp->b_data + loff, n, uio);
844 hammer2_mtx_unlock(&ip->truncate_lock);
850 * Write to the file represented by the inode via the logical buffer cache.
851 * The inode may represent a regular file or a symlink.
853 * The inode must not be locked.
857 hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
858 int ioflag, int seqcount)
860 hammer2_key_t old_eof;
861 hammer2_key_t new_eof;
870 * WARNING! Assumes that the kernel interlocks size changes at the
873 hammer2_mtx_ex(&ip->lock);
874 hammer2_mtx_sh(&ip->truncate_lock);
875 if (ioflag & IO_APPEND)
876 uio->uio_offset = ip->meta.size;
877 old_eof = ip->meta.size;
880 * Extend the file if necessary. If the write fails at some point
881 * we will truncate it back down to cover as much as we were able
884 * Doing this now makes it easier to calculate buffer sizes in
891 if (uio->uio_offset + uio->uio_resid > old_eof) {
892 new_eof = uio->uio_offset + uio->uio_resid;
894 hammer2_extend_file(ip, new_eof);
895 kflags |= NOTE_EXTEND;
899 hammer2_mtx_unlock(&ip->lock);
904 while (uio->uio_resid > 0) {
913 * Don't allow the buffer build to blow out the buffer
916 if ((ioflag & IO_RECURSE) == 0)
917 bwillwrite(HAMMER2_PBUFSIZE);
920 * This nominally tells us how much we can cluster and
921 * what the logical buffer size needs to be. Currently
922 * we don't try to cluster the write and just handle one
925 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
927 loff = (int)(uio->uio_offset - lbase);
929 KKASSERT(lblksize <= 65536);
932 * Calculate bytes to copy this transfer and whether the
933 * copy completely covers the buffer or not.
937 if (n > uio->uio_resid) {
939 if (loff == lbase && uio->uio_offset + n == new_eof)
947 if (lbase >= new_eof)
953 if (uio->uio_segflg == UIO_NOCOPY) {
955 * Issuing a write with the same data backing the
956 * buffer. Instantiate the buffer to collect the
957 * backing vm pages, then read-in any missing bits.
959 * This case is used by vop_stdputpages().
961 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
962 if ((bp->b_flags & B_CACHE) == 0) {
964 error = bread(ip->vp, lbase, lblksize, &bp);
966 } else if (trivial) {
968 * Even though we are entirely overwriting the buffer
969 * we may still have to zero it out to avoid a
970 * mmap/write visibility issue.
972 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
973 if ((bp->b_flags & B_CACHE) == 0)
977 * Partial overwrite, read in any missing bits then
978 * replace the portion being written.
980 * (The strategy code will detect zero-fill physical
981 * blocks for this case).
983 error = bread(ip->vp, lbase, lblksize, &bp);
994 * Ok, copy the data in
996 error = uiomovebp(bp, bp->b_data + loff, n, uio);
997 kflags |= NOTE_WRITE;
1005 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
1006 * with IO_SYNC or IO_ASYNC set. These writes
1007 * must be handled as the pageout daemon expects.
1009 * NOTE! H2 relies on cluster_write() here because it
1010 * cannot preallocate disk blocks at the logical
1011 * level due to not knowing what the compression
1012 * size will be at this time.
1014 * We must use cluster_write() here and we depend
1015 * on the write-behind feature to flush buffers
1016 * appropriately. If we let the buffer daemons do
1017 * it the block allocations will be all over the
1020 if (ioflag & IO_SYNC) {
1022 } else if ((ioflag & IO_DIRECT) && endofblk) {
1024 } else if (ioflag & IO_ASYNC) {
1026 } else if (ip->vp->v_mount->mnt_flag & MNT_NOCLUSTERW) {
1030 bp->b_flags |= B_CLUSTEROK;
1031 cluster_write(bp, new_eof, lblksize, seqcount);
1039 * Cleanup. If we extended the file EOF but failed to write through
1040 * the entire write is a failure and we have to back-up.
1042 if (error && new_eof != old_eof) {
1043 hammer2_mtx_unlock(&ip->truncate_lock);
1044 hammer2_mtx_ex(&ip->lock);
1045 hammer2_mtx_ex(&ip->truncate_lock);
1046 hammer2_truncate_file(ip, old_eof);
1047 if (ip->flags & HAMMER2_INODE_MODIFIED)
1048 hammer2_inode_chain_sync(ip);
1049 hammer2_mtx_unlock(&ip->lock);
1050 } else if (modified) {
1051 hammer2_mtx_ex(&ip->lock);
1052 hammer2_inode_modify(ip);
1053 hammer2_update_time(&ip->meta.mtime);
1054 if (ip->flags & HAMMER2_INODE_MODIFIED)
1055 hammer2_inode_chain_sync(ip);
1056 hammer2_mtx_unlock(&ip->lock);
1057 hammer2_knote(ip->vp, kflags);
1059 hammer2_trans_assert_strategy(ip->pmp);
1060 hammer2_mtx_unlock(&ip->truncate_lock);
1066 * Truncate the size of a file. The inode must not be locked.
1068 * We must unconditionally set HAMMER2_INODE_RESIZED to properly
1069 * ensure that any on-media data beyond the new file EOF has been destroyed.
1071 * WARNING: nvtruncbuf() can only be safely called without the inode lock
1072 * held due to the way our write thread works. If the truncation
1073 * occurs in the middle of a buffer, nvtruncbuf() is responsible
1074 * for dirtying that buffer and zeroing out trailing bytes.
1076 * WARNING! Assumes that the kernel interlocks size changes at the
1079 * WARNING! Caller assumes responsibility for removing dead blocks
1080 * if INODE_RESIZED is set.
1084 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1086 hammer2_key_t lbase;
1090 hammer2_mtx_unlock(&ip->lock);
1092 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1093 nvtruncbuf(ip->vp, nsize,
1094 nblksize, (int)nsize & (nblksize - 1),
1097 hammer2_mtx_ex(&ip->lock);
1098 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1099 ip->osize = ip->meta.size;
1100 ip->meta.size = nsize;
1101 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1102 hammer2_inode_modify(ip);
1107 * Extend the size of a file. The inode must not be locked.
1109 * Even though the file size is changing, we do not have to set the
1110 * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES
1111 * boundary. When this occurs a hammer2_inode_chain_sync() is required
1112 * to prepare the inode cluster's indirect block table, otherwise
1113 * async execution of the strategy code will implode on us.
1115 * WARNING! Assumes that the kernel interlocks size changes at the
1118 * WARNING! Caller assumes responsibility for transitioning out
1119 * of the inode DIRECTDATA mode if INODE_RESIZED is set.
1123 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1125 hammer2_key_t lbase;
1126 hammer2_key_t osize;
1132 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1133 hammer2_inode_modify(ip);
1134 osize = ip->meta.size;
1136 ip->meta.size = nsize;
1138 if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES) {
1139 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1140 hammer2_inode_chain_sync(ip);
1143 hammer2_mtx_unlock(&ip->lock);
1145 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1146 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1152 hammer2_mtx_ex(&ip->lock);
1159 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1161 hammer2_xop_nresolve_t *xop;
1162 hammer2_inode_t *ip;
1163 hammer2_inode_t *dip;
1164 struct namecache *ncp;
1169 dip = VTOI(ap->a_dvp);
1170 xop = hammer2_xop_alloc(dip, 0);
1172 ncp = ap->a_nch->ncp;
1173 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1176 * Note: In DragonFly the kernel handles '.' and '..'.
1178 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1179 hammer2_xop_start(&xop->head, hammer2_xop_nresolve);
1181 error = hammer2_xop_collect(&xop->head, 0);
1185 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1187 hammer2_inode_unlock(dip);
1190 * Acquire the related vnode
1192 * NOTE: For error processing, only ENOENT resolves the namecache
1193 * entry to NULL, otherwise we just return the error and
1194 * leave the namecache unresolved.
1196 * NOTE: multiple hammer2_inode structures can be aliased to the
1197 * same chain element, for example for hardlinks. This
1198 * use case does not 'reattach' inode associations that
1199 * might already exist, but always allocates a new one.
1201 * WARNING: inode structure is locked exclusively via inode_get
1202 * but chain was locked shared. inode_unlock()
1203 * will handle it properly.
1206 vp = hammer2_igetv(ip, &error);
1209 cache_setvp(ap->a_nch, vp);
1210 } else if (error == ENOENT) {
1211 cache_setvp(ap->a_nch, NULL);
1213 hammer2_inode_unlock(ip);
1216 * The vp should not be released until after we've disposed
1217 * of our locks, because it might cause vop_inactive() to
1224 cache_setvp(ap->a_nch, NULL);
1226 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1227 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1228 ("resolve error %d/%p ap %p\n",
1229 error, ap->a_nch->ncp->nc_vp, ap));
1237 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1239 hammer2_inode_t *dip;
1244 dip = VTOI(ap->a_dvp);
1245 inum = dip->meta.iparent;
1249 error = hammer2_vfs_vget(ap->a_dvp->v_mount, NULL,
1260 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1262 hammer2_inode_t *dip;
1263 hammer2_inode_t *nip;
1264 struct namecache *ncp;
1265 const uint8_t *name;
1271 dip = VTOI(ap->a_dvp);
1272 if (dip->pmp->ronly) {
1277 ncp = ap->a_nch->ncp;
1278 name = ncp->nc_name;
1279 name_len = ncp->nc_nlen;
1281 hammer2_pfs_memory_wait(dip->pmp);
1282 hammer2_trans_init(dip->pmp, 0);
1284 inum = hammer2_trans_newinum(dip->pmp);
1287 * Create the actual inode as a hidden file in the iroot, then
1288 * create the directory entry as a hardlink to it. The creation
1289 * of the actual inode sets its nlinks to 1 which is the value
1292 nip = hammer2_inode_create(dip->pmp->iroot, dip, ap->a_vap, ap->a_cred,
1297 hammer2_inode_create(dip, dip, NULL, NULL,
1300 HAMMER2_OBJTYPE_HARDLINK, nip->meta.type,
1305 KKASSERT(nip == NULL);
1308 *ap->a_vpp = hammer2_igetv(nip, &error);
1309 hammer2_inode_unlock(nip);
1313 * Update dip's mtime
1318 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1319 hammer2_update_time(&mtime);
1320 hammer2_inode_modify(dip);
1321 dip->meta.mtime = mtime;
1322 hammer2_inode_unlock(dip);
1325 hammer2_trans_done(dip->pmp);
1328 cache_setunresolved(ap->a_nch);
1329 cache_setvp(ap->a_nch, *ap->a_vpp);
1337 hammer2_vop_open(struct vop_open_args *ap)
1339 return vop_stdopen(ap);
1343 * hammer2_vop_advlock { vp, id, op, fl, flags }
1347 hammer2_vop_advlock(struct vop_advlock_args *ap)
1349 hammer2_inode_t *ip = VTOI(ap->a_vp);
1352 size = ip->meta.size;
1353 return (lf_advlock(ap, &ip->advlock, size));
1358 hammer2_vop_close(struct vop_close_args *ap)
1360 return vop_stdclose(ap);
1364 * hammer2_vop_nlink { nch, dvp, vp, cred }
1366 * Create a hardlink from (vp) to {dvp, nch}.
1370 hammer2_vop_nlink(struct vop_nlink_args *ap)
1372 hammer2_inode_t *tdip; /* target directory to create link in */
1373 hammer2_inode_t *ip; /* inode we are hardlinking to */
1374 struct namecache *ncp;
1375 const uint8_t *name;
1380 tdip = VTOI(ap->a_dvp);
1381 if (tdip->pmp->ronly) {
1386 ncp = ap->a_nch->ncp;
1387 name = ncp->nc_name;
1388 name_len = ncp->nc_nlen;
1391 * ip represents the file being hardlinked. The file could be a
1392 * normal file or a hardlink target if it has already been hardlinked.
1393 * (with the new semantics, it will almost always be a hardlink
1396 * Bump nlinks and potentially also create or move the hardlink
1397 * target in the parent directory common to (ip) and (tdip). The
1398 * consolidation code can modify ip->cluster. The returned cluster
1401 ip = VTOI(ap->a_vp);
1402 hammer2_pfs_memory_wait(ip->pmp);
1403 hammer2_trans_init(ip->pmp, 0);
1406 * Target should be an indexed inode or there's no way we will ever
1407 * be able to find it!
1409 KKASSERT((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0);
1414 * Can return NULL and error == EXDEV if the common parent
1415 * crosses a directory with the xlink flag set.
1417 hammer2_inode_lock(tdip, 0);
1418 hammer2_inode_lock(ip, 0);
1421 * Create the hardlink target and bump nlinks.
1424 hammer2_inode_create(tdip, tdip, NULL, NULL,
1427 HAMMER2_OBJTYPE_HARDLINK, ip->meta.type,
1429 hammer2_inode_modify(ip);
1434 * Update dip's mtime
1438 hammer2_update_time(&mtime);
1439 hammer2_inode_modify(tdip);
1440 tdip->meta.mtime = mtime;
1442 cache_setunresolved(ap->a_nch);
1443 cache_setvp(ap->a_nch, ap->a_vp);
1445 hammer2_inode_unlock(ip);
1446 hammer2_inode_unlock(tdip);
1448 hammer2_trans_done(ip->pmp);
1455 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1457 * The operating system has already ensured that the directory entry
1458 * does not exist and done all appropriate namespace locking.
1462 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1464 hammer2_inode_t *dip;
1465 hammer2_inode_t *nip;
1466 struct namecache *ncp;
1467 const uint8_t *name;
1473 dip = VTOI(ap->a_dvp);
1474 if (dip->pmp->ronly) {
1479 ncp = ap->a_nch->ncp;
1480 name = ncp->nc_name;
1481 name_len = ncp->nc_nlen;
1482 hammer2_pfs_memory_wait(dip->pmp);
1483 hammer2_trans_init(dip->pmp, 0);
1485 inum = hammer2_trans_newinum(dip->pmp);
1488 * Create the actual inode as a hidden file in the iroot, then
1489 * create the directory entry as a hardlink to it. The creation
1490 * of the actual inode sets its nlinks to 1 which is the value
1493 nip = hammer2_inode_create(dip->pmp->iroot, dip, ap->a_vap, ap->a_cred,
1499 hammer2_inode_create(dip, dip, NULL, NULL,
1502 HAMMER2_OBJTYPE_HARDLINK, nip->meta.type,
1506 KKASSERT(nip == NULL);
1509 *ap->a_vpp = hammer2_igetv(nip, &error);
1510 hammer2_inode_unlock(nip);
1514 * Update dip's mtime
1519 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1520 hammer2_update_time(&mtime);
1521 hammer2_inode_modify(dip);
1522 dip->meta.mtime = mtime;
1523 hammer2_inode_unlock(dip);
1526 hammer2_trans_done(dip->pmp);
1529 cache_setunresolved(ap->a_nch);
1530 cache_setvp(ap->a_nch, *ap->a_vpp);
1537 * Make a device node (typically a fifo)
1541 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1543 hammer2_inode_t *dip;
1544 hammer2_inode_t *nip;
1545 struct namecache *ncp;
1546 const uint8_t *name;
1552 dip = VTOI(ap->a_dvp);
1553 if (dip->pmp->ronly) {
1558 ncp = ap->a_nch->ncp;
1559 name = ncp->nc_name;
1560 name_len = ncp->nc_nlen;
1561 hammer2_pfs_memory_wait(dip->pmp);
1562 hammer2_trans_init(dip->pmp, 0);
1565 * The device node is entered as the directory entry itself and not
1566 * as a hardlink to an inode. Since one cannot obtain a
1567 * file handle on the filesystem entry representing the device, we
1568 * do not have to worry about indexing its inode.
1570 inum = hammer2_trans_newinum(dip->pmp);
1571 nip = hammer2_inode_create(dip, dip, ap->a_vap, ap->a_cred,
1576 KKASSERT(nip == NULL);
1579 *ap->a_vpp = hammer2_igetv(nip, &error);
1580 hammer2_inode_unlock(nip);
1584 * Update dip's mtime
1589 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1590 hammer2_update_time(&mtime);
1591 hammer2_inode_modify(dip);
1592 dip->meta.mtime = mtime;
1593 hammer2_inode_unlock(dip);
1596 hammer2_trans_done(dip->pmp);
1599 cache_setunresolved(ap->a_nch);
1600 cache_setvp(ap->a_nch, *ap->a_vpp);
1607 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1611 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1613 hammer2_inode_t *dip;
1614 hammer2_inode_t *nip;
1615 struct namecache *ncp;
1616 const uint8_t *name;
1621 dip = VTOI(ap->a_dvp);
1622 if (dip->pmp->ronly)
1625 ncp = ap->a_nch->ncp;
1626 name = ncp->nc_name;
1627 name_len = ncp->nc_nlen;
1628 hammer2_pfs_memory_wait(dip->pmp);
1629 hammer2_trans_init(dip->pmp, 0);
1631 ap->a_vap->va_type = VLNK; /* enforce type */
1634 * The softlink is entered into the directory itself and not
1635 * as a hardlink to an inode. Since one cannot obtain a
1636 * file handle on the softlink itself we do not have to worry
1637 * about indexing its inode.
1639 inum = hammer2_trans_newinum(dip->pmp);
1640 nip = hammer2_inode_create(dip, dip, ap->a_vap, ap->a_cred,
1645 KKASSERT(nip == NULL);
1647 hammer2_trans_done(dip->pmp);
1650 *ap->a_vpp = hammer2_igetv(nip, &error);
1653 * Build the softlink (~like file data) and finalize the namecache.
1660 bytes = strlen(ap->a_target);
1662 hammer2_inode_unlock(nip);
1663 bzero(&auio, sizeof(auio));
1664 bzero(&aiov, sizeof(aiov));
1665 auio.uio_iov = &aiov;
1666 auio.uio_segflg = UIO_SYSSPACE;
1667 auio.uio_rw = UIO_WRITE;
1668 auio.uio_resid = bytes;
1669 auio.uio_iovcnt = 1;
1670 auio.uio_td = curthread;
1671 aiov.iov_base = ap->a_target;
1672 aiov.iov_len = bytes;
1673 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1674 /* XXX handle error */
1677 hammer2_inode_unlock(nip);
1681 * Update dip's mtime
1686 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1687 hammer2_update_time(&mtime);
1688 hammer2_inode_modify(dip);
1689 dip->meta.mtime = mtime;
1690 hammer2_inode_unlock(dip);
1693 hammer2_trans_done(dip->pmp);
1696 * Finalize namecache
1699 cache_setunresolved(ap->a_nch);
1700 cache_setvp(ap->a_nch, *ap->a_vpp);
1701 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1707 * hammer2_vop_nremove { nch, dvp, cred }
1711 hammer2_vop_nremove(struct vop_nremove_args *ap)
1713 hammer2_xop_unlink_t *xop;
1714 hammer2_inode_t *dip;
1715 hammer2_inode_t *ip;
1716 struct namecache *ncp;
1721 dip = VTOI(ap->a_dvp);
1722 if (dip->pmp->ronly) {
1727 ncp = ap->a_nch->ncp;
1729 hammer2_pfs_memory_wait(dip->pmp);
1730 hammer2_trans_init(dip->pmp, 0);
1731 hammer2_inode_lock(dip, 0);
1734 * The unlink XOP unlinks the path from the directory and
1735 * locates and returns the cluster associated with the real inode.
1736 * We have to handle nlinks here on the frontend.
1738 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1739 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1740 isopen = cache_isopen(ap->a_nch);
1742 xop->dopermanent = isopen ? 0 : HAMMER2_DELETE_PERMANENT;
1743 hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1746 * Collect the real inode and adjust nlinks, destroy the real
1747 * inode if nlinks transitions to 0 and it was the real inode
1748 * (else it has already been removed).
1750 error = hammer2_xop_collect(&xop->head, 0);
1751 hammer2_inode_unlock(dip);
1754 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1755 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1757 hammer2_inode_unlink_finisher(ip, isopen);
1758 hammer2_inode_unlock(ip);
1761 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1765 * Update dip's mtime
1770 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1771 hammer2_update_time(&mtime);
1772 hammer2_inode_modify(dip);
1773 dip->meta.mtime = mtime;
1774 hammer2_inode_unlock(dip);
1777 hammer2_inode_run_sideq(dip->pmp);
1778 hammer2_trans_done(dip->pmp);
1780 cache_unlink(ap->a_nch);
1786 * hammer2_vop_nrmdir { nch, dvp, cred }
1790 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1792 hammer2_xop_unlink_t *xop;
1793 hammer2_inode_t *dip;
1794 hammer2_inode_t *ip;
1795 struct namecache *ncp;
1800 dip = VTOI(ap->a_dvp);
1801 if (dip->pmp->ronly) {
1806 hammer2_pfs_memory_wait(dip->pmp);
1807 hammer2_trans_init(dip->pmp, 0);
1808 hammer2_inode_lock(dip, 0);
1810 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1812 ncp = ap->a_nch->ncp;
1813 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1814 isopen = cache_isopen(ap->a_nch);
1816 xop->dopermanent = isopen ? 0 : HAMMER2_DELETE_PERMANENT;
1817 hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1820 * Collect the real inode and adjust nlinks, destroy the real
1821 * inode if nlinks transitions to 0 and it was the real inode
1822 * (else it has already been removed).
1824 error = hammer2_xop_collect(&xop->head, 0);
1825 hammer2_inode_unlock(dip);
1828 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1829 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1831 hammer2_inode_unlink_finisher(ip, isopen);
1832 hammer2_inode_unlock(ip);
1835 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1839 * Update dip's mtime
1844 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1845 hammer2_update_time(&mtime);
1846 hammer2_inode_modify(dip);
1847 dip->meta.mtime = mtime;
1848 hammer2_inode_unlock(dip);
1851 hammer2_inode_run_sideq(dip->pmp);
1852 hammer2_trans_done(dip->pmp);
1854 cache_unlink(ap->a_nch);
1860 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1864 hammer2_vop_nrename(struct vop_nrename_args *ap)
1866 struct namecache *fncp;
1867 struct namecache *tncp;
1868 hammer2_inode_t *fdip;
1869 hammer2_inode_t *tdip;
1870 hammer2_inode_t *ip;
1871 const uint8_t *fname;
1873 const uint8_t *tname;
1881 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1883 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1886 fdip = VTOI(ap->a_fdvp); /* source directory */
1887 tdip = VTOI(ap->a_tdvp); /* target directory */
1889 if (fdip->pmp->ronly)
1893 fncp = ap->a_fnch->ncp; /* entry name in source */
1894 fname = fncp->nc_name;
1895 fname_len = fncp->nc_nlen;
1897 tncp = ap->a_tnch->ncp; /* entry name in target */
1898 tname = tncp->nc_name;
1899 tname_len = tncp->nc_nlen;
1901 hammer2_pfs_memory_wait(tdip->pmp);
1902 hammer2_trans_init(tdip->pmp, 0);
1908 * ip is the inode being renamed. If this is a hardlink then
1909 * ip represents the actual file and not the hardlink marker.
1911 ip = VTOI(fncp->nc_vp);
1913 KKASSERT((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0);
1916 * Can return NULL and error == EXDEV if the common parent
1917 * crosses a directory with the xlink flag set.
1920 hammer2_inode_lock(fdip, 0);
1921 hammer2_inode_lock(tdip, 0);
1922 hammer2_inode_ref(ip); /* extra ref */
1924 hammer2_inode_lock(ip, 0);
1927 * Delete the target namespace.
1930 hammer2_xop_unlink_t *xop2;
1931 hammer2_inode_t *tip;
1935 * The unlink XOP unlinks the path from the directory and
1936 * locates and returns the cluster associated with the real
1937 * inode. We have to handle nlinks here on the frontend.
1939 xop2 = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
1940 hammer2_xop_setname(&xop2->head, tname, tname_len);
1941 isopen = cache_isopen(ap->a_tnch);
1943 xop2->dopermanent = isopen ? 0 : HAMMER2_DELETE_PERMANENT;
1944 hammer2_xop_start(&xop2->head, hammer2_xop_unlink);
1947 * Collect the real inode and adjust nlinks, destroy the real
1948 * inode if nlinks transitions to 0 and it was the real inode
1949 * (else it has already been removed).
1951 tnch_error = hammer2_xop_collect(&xop2->head, 0);
1952 /* hammer2_inode_unlock(tdip); */
1954 if (tnch_error == 0) {
1955 tip = hammer2_inode_get(tdip->pmp, NULL,
1956 &xop2->head.cluster, -1);
1957 hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
1959 hammer2_inode_unlink_finisher(tip, isopen);
1960 hammer2_inode_unlock(tip);
1963 hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
1965 /* hammer2_inode_lock(tdip, 0); */
1967 if (tnch_error && tnch_error != ENOENT) {
1975 * Resolve the collision space for (tdip, tname, tname_len)
1977 * tdip must be held exclusively locked to prevent races.
1980 hammer2_xop_scanlhc_t *sxop;
1981 hammer2_tid_t lhcbase;
1983 tlhc = hammer2_dirhash(tname, tname_len);
1985 sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
1987 hammer2_xop_start(&sxop->head, hammer2_xop_scanlhc);
1988 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
1989 if (tlhc != sxop->head.cluster.focus->bref.key)
1993 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
1996 if (error != ENOENT)
2001 if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) {
2008 * Everything is setup, do the rename.
2010 * We have to synchronize ip->meta to the underlying operation.
2012 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
2013 * unlinking elements from their directories. Locking
2014 * the nlinks field does not lock the whole inode.
2016 /* hammer2_inode_lock(ip, 0); */
2018 hammer2_xop_nrename_t *xop4;
2020 xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
2022 xop4->ip_key = ip->meta.name_key;
2023 hammer2_xop_setip2(&xop4->head, ip);
2024 hammer2_xop_setip3(&xop4->head, tdip);
2025 hammer2_xop_setname(&xop4->head, fname, fname_len);
2026 hammer2_xop_setname2(&xop4->head, tname, tname_len);
2027 hammer2_xop_start(&xop4->head, hammer2_xop_nrename);
2029 error = hammer2_xop_collect(&xop4->head, 0);
2030 hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP);
2032 if (error == ENOENT)
2035 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
2036 hammer2_inode_modify(ip);
2037 ip->meta.name_len = tname_len;
2038 ip->meta.name_key = tlhc;
2047 * Update directory mtimes to represent the something changed.
2049 if (update_fdip || update_tdip) {
2052 hammer2_update_time(&mtime);
2054 hammer2_inode_modify(fdip);
2055 fdip->meta.mtime = mtime;
2058 hammer2_inode_modify(tdip);
2059 tdip->meta.mtime = mtime;
2062 hammer2_inode_unlock(ip);
2063 hammer2_inode_unlock(tdip);
2064 hammer2_inode_unlock(fdip);
2065 hammer2_inode_drop(ip);
2066 hammer2_inode_run_sideq(fdip->pmp);
2068 hammer2_trans_done(tdip->pmp);
2071 * Issue the namecache update after unlocking all the internal
2072 * hammer structures, otherwise we might deadlock.
2074 if (tnch_error == 0) {
2075 cache_unlink(ap->a_tnch);
2076 cache_setunresolved(ap->a_tnch);
2079 cache_rename(ap->a_fnch, ap->a_tnch);
2086 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2090 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2092 hammer2_inode_t *ip;
2096 ip = VTOI(ap->a_vp);
2098 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2099 ap->a_fflag, ap->a_cred);
2106 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2114 case (MOUNTCTL_SET_EXPORT):
2115 mp = ap->a_head.a_ops->head.vv_mount;
2118 if (ap->a_ctllen != sizeof(struct export_args))
2121 rc = vfs_export(mp, &pmp->export,
2122 (const struct export_args *)ap->a_ctl);
2125 rc = vop_stdmountctl(ap);
2135 static void filt_hammer2detach(struct knote *kn);
2136 static int filt_hammer2read(struct knote *kn, long hint);
2137 static int filt_hammer2write(struct knote *kn, long hint);
2138 static int filt_hammer2vnode(struct knote *kn, long hint);
2140 static struct filterops hammer2read_filtops =
2141 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2142 NULL, filt_hammer2detach, filt_hammer2read };
2143 static struct filterops hammer2write_filtops =
2144 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2145 NULL, filt_hammer2detach, filt_hammer2write };
2146 static struct filterops hammer2vnode_filtops =
2147 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2148 NULL, filt_hammer2detach, filt_hammer2vnode };
2152 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
2154 struct vnode *vp = ap->a_vp;
2155 struct knote *kn = ap->a_kn;
2157 switch (kn->kn_filter) {
2159 kn->kn_fop = &hammer2read_filtops;
2162 kn->kn_fop = &hammer2write_filtops;
2165 kn->kn_fop = &hammer2vnode_filtops;
2168 return (EOPNOTSUPP);
2171 kn->kn_hook = (caddr_t)vp;
2173 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2179 filt_hammer2detach(struct knote *kn)
2181 struct vnode *vp = (void *)kn->kn_hook;
2183 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2187 filt_hammer2read(struct knote *kn, long hint)
2189 struct vnode *vp = (void *)kn->kn_hook;
2190 hammer2_inode_t *ip = VTOI(vp);
2193 if (hint == NOTE_REVOKE) {
2194 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2197 off = ip->meta.size - kn->kn_fp->f_offset;
2198 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2199 if (kn->kn_sfflags & NOTE_OLDAPI)
2201 return (kn->kn_data != 0);
2206 filt_hammer2write(struct knote *kn, long hint)
2208 if (hint == NOTE_REVOKE)
2209 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2215 filt_hammer2vnode(struct knote *kn, long hint)
2217 if (kn->kn_sfflags & hint)
2218 kn->kn_fflags |= hint;
2219 if (hint == NOTE_REVOKE) {
2220 kn->kn_flags |= (EV_EOF | EV_NODATA);
2223 return (kn->kn_fflags != 0);
2231 hammer2_vop_markatime(struct vop_markatime_args *ap)
2233 hammer2_inode_t *ip;
2246 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2250 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2252 error = hammer2_vop_kqfilter(ap);
2259 struct vop_ops hammer2_vnode_vops = {
2260 .vop_default = vop_defaultop,
2261 .vop_fsync = hammer2_vop_fsync,
2262 .vop_getpages = vop_stdgetpages,
2263 .vop_putpages = vop_stdputpages,
2264 .vop_access = hammer2_vop_access,
2265 .vop_advlock = hammer2_vop_advlock,
2266 .vop_close = hammer2_vop_close,
2267 .vop_nlink = hammer2_vop_nlink,
2268 .vop_ncreate = hammer2_vop_ncreate,
2269 .vop_nsymlink = hammer2_vop_nsymlink,
2270 .vop_nremove = hammer2_vop_nremove,
2271 .vop_nrmdir = hammer2_vop_nrmdir,
2272 .vop_nrename = hammer2_vop_nrename,
2273 .vop_getattr = hammer2_vop_getattr,
2274 .vop_setattr = hammer2_vop_setattr,
2275 .vop_readdir = hammer2_vop_readdir,
2276 .vop_readlink = hammer2_vop_readlink,
2277 .vop_getpages = vop_stdgetpages,
2278 .vop_putpages = vop_stdputpages,
2279 .vop_read = hammer2_vop_read,
2280 .vop_write = hammer2_vop_write,
2281 .vop_open = hammer2_vop_open,
2282 .vop_inactive = hammer2_vop_inactive,
2283 .vop_reclaim = hammer2_vop_reclaim,
2284 .vop_nresolve = hammer2_vop_nresolve,
2285 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2286 .vop_nmkdir = hammer2_vop_nmkdir,
2287 .vop_nmknod = hammer2_vop_nmknod,
2288 .vop_ioctl = hammer2_vop_ioctl,
2289 .vop_mountctl = hammer2_vop_mountctl,
2290 .vop_bmap = hammer2_vop_bmap,
2291 .vop_strategy = hammer2_vop_strategy,
2292 .vop_kqfilter = hammer2_vop_kqfilter
2295 struct vop_ops hammer2_spec_vops = {
2296 .vop_default = vop_defaultop,
2297 .vop_fsync = hammer2_vop_fsync,
2298 .vop_read = vop_stdnoread,
2299 .vop_write = vop_stdnowrite,
2300 .vop_access = hammer2_vop_access,
2301 .vop_close = hammer2_vop_close,
2302 .vop_markatime = hammer2_vop_markatime,
2303 .vop_getattr = hammer2_vop_getattr,
2304 .vop_inactive = hammer2_vop_inactive,
2305 .vop_reclaim = hammer2_vop_reclaim,
2306 .vop_setattr = hammer2_vop_setattr
2309 struct vop_ops hammer2_fifo_vops = {
2310 .vop_default = fifo_vnoperate,
2311 .vop_fsync = hammer2_vop_fsync,
2313 .vop_read = hammer2_vop_fiforead,
2314 .vop_write = hammer2_vop_fifowrite,
2316 .vop_access = hammer2_vop_access,
2318 .vop_close = hammer2_vop_fifoclose,
2320 .vop_markatime = hammer2_vop_markatime,
2321 .vop_getattr = hammer2_vop_getattr,
2322 .vop_inactive = hammer2_vop_inactive,
2323 .vop_reclaim = hammer2_vop_reclaim,
2324 .vop_setattr = hammer2_vop_setattr,
2325 .vop_kqfilter = hammer2_vop_fifokqfilter