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)
101 * Check for deleted inodes and recycle immediately on the last
102 * release. Be sure to destroy any left-over buffer cache buffers
103 * so we do not waste time trying to flush them.
105 * Note that deleting the file block chains under the inode chain
106 * would just be a waste of energy, so don't do it.
108 * WARNING: nvtruncbuf() can only be safely called without the inode
109 * lock held due to the way our write thread works.
111 if (ip->flags & HAMMER2_INODE_ISUNLINKED) {
116 * Detect updates to the embedded data which may be
117 * synchronized by the strategy code. Simply mark the
118 * inode modified so it gets picked up by our normal flush.
120 nblksize = hammer2_calc_logical(ip, 0, &lbase, NULL);
121 nvtruncbuf(vp, 0, nblksize, 0, 0);
128 * Reclaim a vnode so that it can be reused; after the inode is
129 * disassociated, the filesystem must manage it alone.
133 hammer2_vop_reclaim(struct vop_reclaim_args *ap)
147 * The final close of a deleted file or directory marks it for
148 * destruction. The DELETED flag allows the flusher to shortcut
149 * any modified blocks still unflushed (that is, just ignore them).
151 * HAMMER2 usually does not try to optimize the freemap by returning
152 * deleted blocks to it as it does not usually know how many snapshots
153 * might be referencing portions of the file/dir.
159 * NOTE! We do not attempt to flush chains here, flushing is
160 * really fragile and could also deadlock.
165 * A modified inode may require chain synchronization. This
166 * synchronization is usually handled by VOP_SNYC / VOP_FSYNC
167 * when vfsync() is called. However, that requires a vnode.
169 * When the vnode is disassociated we must keep track of any modified
170 * inode via the sideq so that it is properly flushed. We cannot
171 * safely synchronize the inode from inside the reclaim due to
172 * potentially deep locks held as-of when the reclaim occurs.
173 * Interactions and potential deadlocks abound.
175 if ((ip->flags & (HAMMER2_INODE_ISUNLINKED |
176 HAMMER2_INODE_MODIFIED |
177 HAMMER2_INODE_RESIZED)) &&
178 (ip->flags & HAMMER2_INODE_ISDELETED) == 0) {
179 hammer2_inode_sideq_t *ipul;
181 ipul = kmalloc(sizeof(*ipul), pmp->minode, M_WAITOK | M_ZERO);
184 hammer2_spin_ex(&pmp->list_spin);
185 if ((ip->flags & HAMMER2_INODE_ONSIDEQ) == 0) {
187 atomic_set_int(&ip->flags, HAMMER2_INODE_ONSIDEQ);
188 TAILQ_INSERT_TAIL(&pmp->sideq, ipul, entry);
190 hammer2_spin_unex(&pmp->list_spin);
192 hammer2_spin_unex(&pmp->list_spin);
193 kfree(ipul, pmp->minode);
194 hammer2_inode_drop(ip); /* vp ref */
196 /* retain ref from vp for ipul */
198 hammer2_inode_drop(ip); /* vp ref */
202 * XXX handle background sync when ip dirty, kernel will no longer
203 * notify us regarding this inode because there is no longer a
204 * vnode attached to it.
212 hammer2_vop_fsync(struct vop_fsync_args *ap)
221 /* XXX can't do this yet */
222 hammer2_trans_init(ip->pmp, HAMMER2_TRANS_ISFLUSH);
223 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
225 hammer2_trans_init(ip->pmp, 0);
226 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
229 * Calling chain_flush here creates a lot of duplicative
230 * COW operations due to non-optimal vnode ordering.
232 * Only do it for an actual fsync() syscall. The other forms
233 * which call this function will eventually call chain_flush
234 * on the volume root as a catch-all, which is far more optimal.
236 hammer2_inode_lock(ip, 0);
237 if (ip->flags & HAMMER2_INODE_MODIFIED)
238 hammer2_inode_chain_sync(ip);
239 hammer2_inode_unlock(ip);
240 hammer2_trans_done(ip->pmp);
247 hammer2_vop_access(struct vop_access_args *ap)
249 hammer2_inode_t *ip = VTOI(ap->a_vp);
254 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
255 uid = hammer2_to_unix_xid(&ip->meta.uid);
256 gid = hammer2_to_unix_xid(&ip->meta.gid);
257 error = vop_helper_access(ap, uid, gid, ip->meta.mode, ip->meta.uflags);
258 hammer2_inode_unlock(ip);
265 hammer2_vop_getattr(struct vop_getattr_args *ap)
271 hammer2_chain_t *chain;
280 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
282 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
283 vap->va_fileid = ip->meta.inum;
284 vap->va_mode = ip->meta.mode;
285 vap->va_nlink = ip->meta.nlinks;
286 vap->va_uid = hammer2_to_unix_xid(&ip->meta.uid);
287 vap->va_gid = hammer2_to_unix_xid(&ip->meta.gid);
290 vap->va_size = ip->meta.size; /* protected by shared lock */
291 vap->va_blocksize = HAMMER2_PBUFSIZE;
292 vap->va_flags = ip->meta.uflags;
293 hammer2_time_to_timespec(ip->meta.ctime, &vap->va_ctime);
294 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_mtime);
295 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_atime);
298 if (ip->meta.type == HAMMER2_OBJTYPE_DIRECTORY) {
300 * Can't really calculate directory use sans the files under
301 * it, just assume one block for now.
303 vap->va_bytes += HAMMER2_INODE_BYTES;
305 for (i = 0; i < ip->cluster.nchains; ++i) {
306 if ((chain = ip->cluster.array[i].chain) != NULL) {
308 chain->bref.embed.stats.data_count) {
310 chain->bref.embed.stats.data_count;
315 vap->va_type = hammer2_get_vtype(ip->meta.type);
317 vap->va_uid_uuid = ip->meta.uid;
318 vap->va_gid_uuid = ip->meta.gid;
319 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
322 hammer2_inode_unlock(ip);
329 hammer2_vop_setattr(struct vop_setattr_args *ap)
340 hammer2_update_time(&ctime);
346 if (hammer2_vfs_enospace(ip, 0, ap->a_cred) > 1)
349 hammer2_pfs_memory_wait(ip->pmp);
350 hammer2_trans_init(ip->pmp, 0);
351 hammer2_inode_lock(ip, 0);
354 if (vap->va_flags != VNOVAL) {
357 flags = ip->meta.uflags;
358 error = vop_helper_setattr_flags(&flags, vap->va_flags,
359 hammer2_to_unix_xid(&ip->meta.uid),
362 if (ip->meta.uflags != flags) {
363 hammer2_inode_modify(ip);
364 ip->meta.uflags = flags;
365 ip->meta.ctime = ctime;
366 kflags |= NOTE_ATTRIB;
368 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
375 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
379 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
380 mode_t cur_mode = ip->meta.mode;
381 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
382 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
386 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
388 &cur_uid, &cur_gid, &cur_mode);
390 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
391 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
392 if (bcmp(&uuid_uid, &ip->meta.uid, sizeof(uuid_uid)) ||
393 bcmp(&uuid_gid, &ip->meta.gid, sizeof(uuid_gid)) ||
394 ip->meta.mode != cur_mode
396 hammer2_inode_modify(ip);
397 ip->meta.uid = uuid_uid;
398 ip->meta.gid = uuid_gid;
399 ip->meta.mode = cur_mode;
400 ip->meta.ctime = ctime;
402 kflags |= NOTE_ATTRIB;
409 if (vap->va_size != VNOVAL && ip->meta.size != vap->va_size) {
412 if (vap->va_size == ip->meta.size)
414 if (vap->va_size < ip->meta.size) {
415 hammer2_mtx_ex(&ip->truncate_lock);
416 hammer2_truncate_file(ip, vap->va_size);
417 hammer2_mtx_unlock(&ip->truncate_lock);
418 kflags |= NOTE_WRITE;
420 hammer2_extend_file(ip, vap->va_size);
421 kflags |= NOTE_WRITE | NOTE_EXTEND;
423 hammer2_inode_modify(ip);
424 ip->meta.mtime = ctime;
432 /* atime not supported */
433 if (vap->va_atime.tv_sec != VNOVAL) {
434 hammer2_inode_modify(ip);
435 ip->meta.atime = hammer2_timespec_to_time(&vap->va_atime);
436 kflags |= NOTE_ATTRIB;
439 if (vap->va_mode != (mode_t)VNOVAL) {
440 mode_t cur_mode = ip->meta.mode;
441 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
442 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
444 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
445 cur_uid, cur_gid, &cur_mode);
446 if (error == 0 && ip->meta.mode != cur_mode) {
447 hammer2_inode_modify(ip);
448 ip->meta.mode = cur_mode;
449 ip->meta.ctime = ctime;
450 kflags |= NOTE_ATTRIB;
454 if (vap->va_mtime.tv_sec != VNOVAL) {
455 hammer2_inode_modify(ip);
456 ip->meta.mtime = hammer2_timespec_to_time(&vap->va_mtime);
457 kflags |= NOTE_ATTRIB;
462 * If a truncation occurred we must call inode_fsync() now in order
463 * to trim the related data chains, otherwise a later expansion can
466 * If an extend occured that changed the DIRECTDATA state, we must
467 * call inode_fsync now in order to prepare the inode's indirect
470 if (ip->flags & HAMMER2_INODE_RESIZED)
471 hammer2_inode_chain_sync(ip);
476 hammer2_inode_unlock(ip);
477 hammer2_trans_done(ip->pmp);
478 hammer2_knote(ip->vp, kflags);
485 hammer2_vop_readdir(struct vop_readdir_args *ap)
487 hammer2_xop_readdir_t *xop;
488 hammer2_blockref_t bref;
503 saveoff = uio->uio_offset;
508 * Setup cookies directory entry cookies if requested
510 if (ap->a_ncookies) {
511 ncookies = uio->uio_resid / 16 + 1;
514 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
521 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
524 * Handle artificial entries. To ensure that only positive 64 bit
525 * quantities are returned to userland we always strip off bit 63.
526 * The hash code is designed such that codes 0x0000-0x7FFF are not
527 * used, allowing us to use these codes for articial entries.
529 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
530 * allow '..' to cross the mount point into (e.g.) the super-root.
533 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
534 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
538 cookies[cookie_index] = saveoff;
541 if (cookie_index == ncookies)
547 * Be careful with lockorder when accessing ".."
549 * (ip is the current dir. xip is the parent dir).
551 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
552 if (ip != ip->pmp->iroot)
553 inum = ip->meta.iparent & HAMMER2_DIRHASH_USERMSK;
554 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
558 cookies[cookie_index] = saveoff;
561 if (cookie_index == ncookies)
565 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
566 if (hammer2_debug & 0x0020)
567 kprintf("readdir: lkey %016jx\n", lkey);
572 * Use XOP for cluster scan.
574 * parent is the inode cluster, already locked for us. Don't
575 * double lock shared locks as this will screw up upgrades.
577 xop = hammer2_xop_alloc(ip, 0);
579 hammer2_xop_start(&xop->head, hammer2_xop_readdir);
582 const hammer2_inode_data_t *ripdata;
586 error = hammer2_xop_collect(&xop->head, 0);
587 error = hammer2_error_to_errno(error);
591 if (cookie_index == ncookies)
593 if (hammer2_debug & 0x0020)
594 kprintf("cluster chain %p %p\n",
595 xop->head.cluster.focus,
596 (xop->head.cluster.focus ?
597 xop->head.cluster.focus->data : (void *)-1));
598 hammer2_cluster_bref(&xop->head.cluster, &bref);
600 if (bref.type == HAMMER2_BREF_TYPE_INODE) {
602 &hammer2_cluster_rdata(&xop->head.cluster)->ipdata;
603 dtype = hammer2_get_dtype(ripdata->meta.type);
604 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
605 r = vop_write_dirent(&error, uio,
607 HAMMER2_DIRHASH_USERMSK,
609 ripdata->meta.name_len,
614 cookies[cookie_index] = saveoff;
616 } else if (bref.type == HAMMER2_BREF_TYPE_DIRENT) {
617 dtype = hammer2_get_dtype(bref.embed.dirent.type);
618 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
619 if (bref.embed.dirent.namlen <=
620 sizeof(bref.check.buf)) {
621 dname = bref.check.buf;
624 hammer2_cluster_rdata(&xop->head.cluster)->buf;
626 r = vop_write_dirent(&error, uio,
627 bref.embed.dirent.inum,
629 bref.embed.dirent.namlen,
634 cookies[cookie_index] = saveoff;
637 /* XXX chain error */
638 kprintf("bad chain type readdir %d\n", bref.type);
641 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
642 if (error == ENOENT) {
645 saveoff = (hammer2_key_t)-1;
647 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
650 hammer2_inode_unlock(ip);
652 *ap->a_eofflag = eofflag;
653 if (hammer2_debug & 0x0020)
654 kprintf("readdir: done at %016jx\n", saveoff);
655 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
656 if (error && cookie_index == 0) {
658 kfree(cookies, M_TEMP);
660 *ap->a_cookies = NULL;
664 *ap->a_ncookies = cookie_index;
665 *ap->a_cookies = cookies;
672 * hammer2_vop_readlink { vp, uio, cred }
676 hammer2_vop_readlink(struct vop_readlink_args *ap)
683 if (vp->v_type != VLNK)
687 error = hammer2_read_file(ip, ap->a_uio, 0);
693 hammer2_vop_read(struct vop_read_args *ap)
703 * Read operations supported on this vnode?
706 if (vp->v_type != VREG)
716 seqcount = ap->a_ioflag >> 16;
717 bigread = (uio->uio_resid > 100 * 1024 * 1024);
719 error = hammer2_read_file(ip, uio, seqcount);
725 hammer2_vop_write(struct vop_write_args *ap)
736 * Read operations supported on this vnode?
739 if (vp->v_type != VREG)
746 ioflag = ap->a_ioflag;
751 switch (hammer2_vfs_enospace(ip, uio->uio_resid, ap->a_cred)) {
755 ioflag |= IO_DIRECT; /* semi-synchronous */
761 seqcount = ioflag >> 16;
764 * Check resource limit
766 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
767 uio->uio_offset + uio->uio_resid >
768 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
769 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
774 * The transaction interlocks against flush initiations
775 * (note: but will run concurrently with the actual flush).
777 * To avoid deadlocking against the VM system, we must flag any
778 * transaction related to the buffer cache or other direct
779 * VM page manipulation.
781 if (uio->uio_segflg == UIO_NOCOPY)
782 hammer2_trans_init(ip->pmp, HAMMER2_TRANS_BUFCACHE);
784 hammer2_trans_init(ip->pmp, 0);
785 error = hammer2_write_file(ip, uio, ioflag, seqcount);
786 hammer2_trans_done(ip->pmp);
792 * Perform read operations on a file or symlink given an UNLOCKED
795 * The passed ip is not locked.
799 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
810 * WARNING! Assumes that the kernel interlocks size changes at the
813 hammer2_mtx_sh(&ip->lock);
814 hammer2_mtx_sh(&ip->truncate_lock);
815 size = ip->meta.size;
816 hammer2_mtx_unlock(&ip->lock);
818 while (uio->uio_resid > 0 && uio->uio_offset < size) {
825 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
829 error = cluster_read(ip->vp, leof, lbase, lblksize,
830 uio->uio_resid, seqcount * MAXBSIZE,
833 if (uio->uio_segflg == UIO_NOCOPY) {
834 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
835 if (bp->b_flags & B_CACHE) {
838 if (bp->b_xio.xio_npages != 16)
839 kprintf("NPAGES BAD\n");
840 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
842 m = bp->b_xio.xio_pages[i];
843 if (m == NULL || m->valid == 0) {
844 kprintf("bp %016jx %016jx pg %d inv",
847 kprintf("m->object %p/%p", m->object, ip->vp->v_object);
853 kprintf("b_flags %08x, b_error %d\n", bp->b_flags, bp->b_error);
857 error = bread(ip->vp, lbase, lblksize, &bp);
863 loff = (int)(uio->uio_offset - lbase);
865 if (n > uio->uio_resid)
867 if (n > size - uio->uio_offset)
868 n = (int)(size - uio->uio_offset);
869 bp->b_flags |= B_AGE;
870 uiomovebp(bp, (char *)bp->b_data + loff, n, uio);
873 hammer2_mtx_unlock(&ip->truncate_lock);
879 * Write to the file represented by the inode via the logical buffer cache.
880 * The inode may represent a regular file or a symlink.
882 * The inode must not be locked.
886 hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
887 int ioflag, int seqcount)
889 hammer2_key_t old_eof;
890 hammer2_key_t new_eof;
899 * WARNING! Assumes that the kernel interlocks size changes at the
902 hammer2_mtx_ex(&ip->lock);
903 hammer2_mtx_sh(&ip->truncate_lock);
904 if (ioflag & IO_APPEND)
905 uio->uio_offset = ip->meta.size;
906 old_eof = ip->meta.size;
909 * Extend the file if necessary. If the write fails at some point
910 * we will truncate it back down to cover as much as we were able
913 * Doing this now makes it easier to calculate buffer sizes in
920 if (uio->uio_offset + uio->uio_resid > old_eof) {
921 new_eof = uio->uio_offset + uio->uio_resid;
923 hammer2_extend_file(ip, new_eof);
924 kflags |= NOTE_EXTEND;
928 hammer2_mtx_unlock(&ip->lock);
933 while (uio->uio_resid > 0) {
942 * Don't allow the buffer build to blow out the buffer
945 if ((ioflag & IO_RECURSE) == 0)
946 bwillwrite(HAMMER2_PBUFSIZE);
949 * This nominally tells us how much we can cluster and
950 * what the logical buffer size needs to be. Currently
951 * we don't try to cluster the write and just handle one
954 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
956 loff = (int)(uio->uio_offset - lbase);
958 KKASSERT(lblksize <= 65536);
961 * Calculate bytes to copy this transfer and whether the
962 * copy completely covers the buffer or not.
966 if (n > uio->uio_resid) {
968 if (loff == lbase && uio->uio_offset + n == new_eof)
976 if (lbase >= new_eof)
982 if (uio->uio_segflg == UIO_NOCOPY) {
984 * Issuing a write with the same data backing the
985 * buffer. Instantiate the buffer to collect the
986 * backing vm pages, then read-in any missing bits.
988 * This case is used by vop_stdputpages().
990 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
991 if ((bp->b_flags & B_CACHE) == 0) {
993 error = bread(ip->vp, lbase, lblksize, &bp);
995 } else if (trivial) {
997 * Even though we are entirely overwriting the buffer
998 * we may still have to zero it out to avoid a
999 * mmap/write visibility issue.
1001 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
1002 if ((bp->b_flags & B_CACHE) == 0)
1006 * Partial overwrite, read in any missing bits then
1007 * replace the portion being written.
1009 * (The strategy code will detect zero-fill physical
1010 * blocks for this case).
1012 error = bread(ip->vp, lbase, lblksize, &bp);
1023 * Ok, copy the data in
1025 error = uiomovebp(bp, bp->b_data + loff, n, uio);
1026 kflags |= NOTE_WRITE;
1034 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
1035 * with IO_SYNC or IO_ASYNC set. These writes
1036 * must be handled as the pageout daemon expects.
1038 * NOTE! H2 relies on cluster_write() here because it
1039 * cannot preallocate disk blocks at the logical
1040 * level due to not knowing what the compression
1041 * size will be at this time.
1043 * We must use cluster_write() here and we depend
1044 * on the write-behind feature to flush buffers
1045 * appropriately. If we let the buffer daemons do
1046 * it the block allocations will be all over the
1049 if (ioflag & IO_SYNC) {
1051 } else if ((ioflag & IO_DIRECT) && endofblk) {
1053 } else if (ioflag & IO_ASYNC) {
1055 } else if (ip->vp->v_mount->mnt_flag & MNT_NOCLUSTERW) {
1059 bp->b_flags |= B_CLUSTEROK;
1060 cluster_write(bp, new_eof, lblksize, seqcount);
1062 bp->b_flags |= B_CLUSTEROK;
1069 * Cleanup. If we extended the file EOF but failed to write through
1070 * the entire write is a failure and we have to back-up.
1072 if (error && new_eof != old_eof) {
1073 hammer2_mtx_unlock(&ip->truncate_lock);
1074 hammer2_mtx_ex(&ip->lock);
1075 hammer2_mtx_ex(&ip->truncate_lock);
1076 hammer2_truncate_file(ip, old_eof);
1077 if (ip->flags & HAMMER2_INODE_MODIFIED)
1078 hammer2_inode_chain_sync(ip);
1079 hammer2_mtx_unlock(&ip->lock);
1080 } else if (modified) {
1081 hammer2_mtx_ex(&ip->lock);
1082 hammer2_inode_modify(ip);
1083 hammer2_update_time(&ip->meta.mtime);
1084 if (ip->flags & HAMMER2_INODE_MODIFIED)
1085 hammer2_inode_chain_sync(ip);
1086 hammer2_mtx_unlock(&ip->lock);
1087 hammer2_knote(ip->vp, kflags);
1089 hammer2_trans_assert_strategy(ip->pmp);
1090 hammer2_mtx_unlock(&ip->truncate_lock);
1096 * Truncate the size of a file. The inode must not be locked.
1098 * We must unconditionally set HAMMER2_INODE_RESIZED to properly
1099 * ensure that any on-media data beyond the new file EOF has been destroyed.
1101 * WARNING: nvtruncbuf() can only be safely called without the inode lock
1102 * held due to the way our write thread works. If the truncation
1103 * occurs in the middle of a buffer, nvtruncbuf() is responsible
1104 * for dirtying that buffer and zeroing out trailing bytes.
1106 * WARNING! Assumes that the kernel interlocks size changes at the
1109 * WARNING! Caller assumes responsibility for removing dead blocks
1110 * if INODE_RESIZED is set.
1114 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1116 hammer2_key_t lbase;
1119 hammer2_mtx_unlock(&ip->lock);
1121 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1122 nvtruncbuf(ip->vp, nsize,
1123 nblksize, (int)nsize & (nblksize - 1),
1126 hammer2_mtx_ex(&ip->lock);
1127 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1128 ip->osize = ip->meta.size;
1129 ip->meta.size = nsize;
1130 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1131 hammer2_inode_modify(ip);
1135 * Extend the size of a file. The inode must not be locked.
1137 * Even though the file size is changing, we do not have to set the
1138 * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES
1139 * boundary. When this occurs a hammer2_inode_chain_sync() is required
1140 * to prepare the inode cluster's indirect block table, otherwise
1141 * async execution of the strategy code will implode on us.
1143 * WARNING! Assumes that the kernel interlocks size changes at the
1146 * WARNING! Caller assumes responsibility for transitioning out
1147 * of the inode DIRECTDATA mode if INODE_RESIZED is set.
1151 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1153 hammer2_key_t lbase;
1154 hammer2_key_t osize;
1158 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1159 hammer2_inode_modify(ip);
1160 osize = ip->meta.size;
1162 ip->meta.size = nsize;
1164 if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES) {
1165 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1166 hammer2_inode_chain_sync(ip);
1169 hammer2_mtx_unlock(&ip->lock);
1171 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1172 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1178 hammer2_mtx_ex(&ip->lock);
1183 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1185 hammer2_xop_nresolve_t *xop;
1186 hammer2_inode_t *ip;
1187 hammer2_inode_t *dip;
1188 struct namecache *ncp;
1192 dip = VTOI(ap->a_dvp);
1193 xop = hammer2_xop_alloc(dip, 0);
1195 ncp = ap->a_nch->ncp;
1196 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1199 * Note: In DragonFly the kernel handles '.' and '..'.
1201 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1202 hammer2_xop_start(&xop->head, hammer2_xop_nresolve);
1204 error = hammer2_xop_collect(&xop->head, 0);
1205 error = hammer2_error_to_errno(error);
1209 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1211 hammer2_inode_unlock(dip);
1214 * Acquire the related vnode
1216 * NOTE: For error processing, only ENOENT resolves the namecache
1217 * entry to NULL, otherwise we just return the error and
1218 * leave the namecache unresolved.
1220 * NOTE: multiple hammer2_inode structures can be aliased to the
1221 * same chain element, for example for hardlinks. This
1222 * use case does not 'reattach' inode associations that
1223 * might already exist, but always allocates a new one.
1225 * WARNING: inode structure is locked exclusively via inode_get
1226 * but chain was locked shared. inode_unlock()
1227 * will handle it properly.
1230 vp = hammer2_igetv(ip, &error); /* error set to UNIX error */
1233 cache_setvp(ap->a_nch, vp);
1234 } else if (error == ENOENT) {
1235 cache_setvp(ap->a_nch, NULL);
1237 hammer2_inode_unlock(ip);
1240 * The vp should not be released until after we've disposed
1241 * of our locks, because it might cause vop_inactive() to
1248 cache_setvp(ap->a_nch, NULL);
1250 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1251 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1252 ("resolve error %d/%p ap %p\n",
1253 error, ap->a_nch->ncp->nc_vp, ap));
1260 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1262 hammer2_inode_t *dip;
1266 dip = VTOI(ap->a_dvp);
1267 inum = dip->meta.iparent;
1271 error = hammer2_vfs_vget(ap->a_dvp->v_mount, NULL,
1281 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1283 hammer2_inode_t *dip;
1284 hammer2_inode_t *nip;
1285 struct namecache *ncp;
1286 const uint8_t *name;
1291 dip = VTOI(ap->a_dvp);
1292 if (dip->pmp->ronly)
1294 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1297 ncp = ap->a_nch->ncp;
1298 name = ncp->nc_name;
1299 name_len = ncp->nc_nlen;
1301 hammer2_pfs_memory_wait(dip->pmp);
1302 hammer2_trans_init(dip->pmp, 0);
1304 inum = hammer2_trans_newinum(dip->pmp);
1307 * Create the actual inode as a hidden file in the iroot, then
1308 * create the directory entry. The creation of the actual inode
1309 * sets its nlinks to 1 which is the value we desire.
1311 nip = hammer2_inode_create(dip->pmp->iroot, dip, ap->a_vap, ap->a_cred,
1316 error = hammer2_error_to_errno(error);
1318 error = hammer2_dirent_create(dip, name, name_len,
1319 nip->meta.inum, nip->meta.type);
1320 /* returns UNIX error code */
1324 hammer2_inode_unlink_finisher(nip, 0);
1325 hammer2_inode_unlock(nip);
1330 *ap->a_vpp = hammer2_igetv(nip, &error);
1331 hammer2_inode_unlock(nip);
1335 * Update dip's mtime
1337 * We can use a shared inode lock and allow the meta.mtime update
1338 * SMP race. hammer2_inode_modify() is MPSAFE w/a shared lock.
1343 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1344 hammer2_update_time(&mtime);
1345 hammer2_inode_modify(dip);
1346 dip->meta.mtime = mtime;
1347 hammer2_inode_unlock(dip);
1350 hammer2_trans_done(dip->pmp);
1353 cache_setunresolved(ap->a_nch);
1354 cache_setvp(ap->a_nch, *ap->a_vpp);
1355 hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1362 hammer2_vop_open(struct vop_open_args *ap)
1364 return vop_stdopen(ap);
1368 * hammer2_vop_advlock { vp, id, op, fl, flags }
1372 hammer2_vop_advlock(struct vop_advlock_args *ap)
1374 hammer2_inode_t *ip = VTOI(ap->a_vp);
1377 size = ip->meta.size;
1378 return (lf_advlock(ap, &ip->advlock, size));
1383 hammer2_vop_close(struct vop_close_args *ap)
1385 return vop_stdclose(ap);
1389 * hammer2_vop_nlink { nch, dvp, vp, cred }
1391 * Create a hardlink from (vp) to {dvp, nch}.
1395 hammer2_vop_nlink(struct vop_nlink_args *ap)
1397 hammer2_inode_t *tdip; /* target directory to create link in */
1398 hammer2_inode_t *ip; /* inode we are hardlinking to */
1399 struct namecache *ncp;
1400 const uint8_t *name;
1404 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1407 tdip = VTOI(ap->a_dvp);
1408 if (tdip->pmp->ronly)
1410 if (hammer2_vfs_enospace(tdip, 0, ap->a_cred) > 1)
1413 ncp = ap->a_nch->ncp;
1414 name = ncp->nc_name;
1415 name_len = ncp->nc_nlen;
1418 * ip represents the file being hardlinked. The file could be a
1419 * normal file or a hardlink target if it has already been hardlinked.
1420 * (with the new semantics, it will almost always be a hardlink
1423 * Bump nlinks and potentially also create or move the hardlink
1424 * target in the parent directory common to (ip) and (tdip). The
1425 * consolidation code can modify ip->cluster. The returned cluster
1428 ip = VTOI(ap->a_vp);
1429 KASSERT(ip->pmp, ("ip->pmp is NULL %p %p", ip, ip->pmp));
1430 hammer2_pfs_memory_wait(ip->pmp);
1431 hammer2_trans_init(ip->pmp, 0);
1434 * Target should be an indexed inode or there's no way we will ever
1435 * be able to find it!
1437 KKASSERT((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0);
1442 * Can return NULL and error == EXDEV if the common parent
1443 * crosses a directory with the xlink flag set.
1445 hammer2_inode_lock(tdip, 0);
1446 hammer2_inode_lock(ip, 0);
1449 * Create the directory entry and bump nlinks.
1452 error = hammer2_dirent_create(tdip, name, name_len,
1453 ip->meta.inum, ip->meta.type);
1454 hammer2_inode_modify(ip);
1459 * Update dip's mtime
1463 hammer2_update_time(&mtime);
1464 hammer2_inode_modify(tdip);
1465 tdip->meta.mtime = mtime;
1467 cache_setunresolved(ap->a_nch);
1468 cache_setvp(ap->a_nch, ap->a_vp);
1470 hammer2_inode_unlock(ip);
1471 hammer2_inode_unlock(tdip);
1473 hammer2_trans_done(ip->pmp);
1474 hammer2_knote(ap->a_vp, NOTE_LINK);
1475 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1481 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1483 * The operating system has already ensured that the directory entry
1484 * does not exist and done all appropriate namespace locking.
1488 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1490 hammer2_inode_t *dip;
1491 hammer2_inode_t *nip;
1492 struct namecache *ncp;
1493 const uint8_t *name;
1498 dip = VTOI(ap->a_dvp);
1499 if (dip->pmp->ronly)
1501 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1504 ncp = ap->a_nch->ncp;
1505 name = ncp->nc_name;
1506 name_len = ncp->nc_nlen;
1507 hammer2_pfs_memory_wait(dip->pmp);
1508 hammer2_trans_init(dip->pmp, 0);
1510 inum = hammer2_trans_newinum(dip->pmp);
1513 * Create the actual inode as a hidden file in the iroot, then
1514 * create the directory entry. The creation of the actual inode
1515 * sets its nlinks to 1 which is the value we desire.
1517 nip = hammer2_inode_create(dip->pmp->iroot, dip, ap->a_vap, ap->a_cred,
1523 error = hammer2_error_to_errno(error);
1525 error = hammer2_dirent_create(dip, name, name_len,
1526 nip->meta.inum, nip->meta.type);
1530 hammer2_inode_unlink_finisher(nip, 0);
1531 hammer2_inode_unlock(nip);
1536 *ap->a_vpp = hammer2_igetv(nip, &error);
1537 hammer2_inode_unlock(nip);
1541 * Update dip's mtime
1546 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1547 hammer2_update_time(&mtime);
1548 hammer2_inode_modify(dip);
1549 dip->meta.mtime = mtime;
1550 hammer2_inode_unlock(dip);
1553 hammer2_trans_done(dip->pmp);
1556 cache_setunresolved(ap->a_nch);
1557 cache_setvp(ap->a_nch, *ap->a_vpp);
1558 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1564 * Make a device node (typically a fifo)
1568 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1570 hammer2_inode_t *dip;
1571 hammer2_inode_t *nip;
1572 struct namecache *ncp;
1573 const uint8_t *name;
1578 dip = VTOI(ap->a_dvp);
1579 if (dip->pmp->ronly)
1581 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1584 ncp = ap->a_nch->ncp;
1585 name = ncp->nc_name;
1586 name_len = ncp->nc_nlen;
1587 hammer2_pfs_memory_wait(dip->pmp);
1588 hammer2_trans_init(dip->pmp, 0);
1591 * Create the device inode and then create the directory entry.
1593 inum = hammer2_trans_newinum(dip->pmp);
1594 nip = hammer2_inode_create(dip->pmp->iroot, dip, ap->a_vap, ap->a_cred,
1599 error = hammer2_dirent_create(dip, name, name_len,
1600 nip->meta.inum, nip->meta.type);
1604 hammer2_inode_unlink_finisher(nip, 0);
1605 hammer2_inode_unlock(nip);
1610 *ap->a_vpp = hammer2_igetv(nip, &error);
1611 hammer2_inode_unlock(nip);
1615 * Update dip's mtime
1620 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1621 hammer2_update_time(&mtime);
1622 hammer2_inode_modify(dip);
1623 dip->meta.mtime = mtime;
1624 hammer2_inode_unlock(dip);
1627 hammer2_trans_done(dip->pmp);
1630 cache_setunresolved(ap->a_nch);
1631 cache_setvp(ap->a_nch, *ap->a_vpp);
1632 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1638 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1642 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1644 hammer2_inode_t *dip;
1645 hammer2_inode_t *nip;
1646 struct namecache *ncp;
1647 const uint8_t *name;
1652 dip = VTOI(ap->a_dvp);
1653 if (dip->pmp->ronly)
1655 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1658 ncp = ap->a_nch->ncp;
1659 name = ncp->nc_name;
1660 name_len = ncp->nc_nlen;
1661 hammer2_pfs_memory_wait(dip->pmp);
1662 hammer2_trans_init(dip->pmp, 0);
1664 ap->a_vap->va_type = VLNK; /* enforce type */
1667 * Create the softlink as an inode and then create the directory
1670 inum = hammer2_trans_newinum(dip->pmp);
1672 nip = hammer2_inode_create(dip->pmp->iroot, dip, ap->a_vap, ap->a_cred,
1677 error = hammer2_dirent_create(dip, name, name_len,
1678 nip->meta.inum, nip->meta.type);
1682 hammer2_inode_unlink_finisher(nip, 0);
1683 hammer2_inode_unlock(nip);
1687 hammer2_trans_done(dip->pmp);
1690 *ap->a_vpp = hammer2_igetv(nip, &error);
1693 * Build the softlink (~like file data) and finalize the namecache.
1700 bytes = strlen(ap->a_target);
1702 hammer2_inode_unlock(nip);
1703 bzero(&auio, sizeof(auio));
1704 bzero(&aiov, sizeof(aiov));
1705 auio.uio_iov = &aiov;
1706 auio.uio_segflg = UIO_SYSSPACE;
1707 auio.uio_rw = UIO_WRITE;
1708 auio.uio_resid = bytes;
1709 auio.uio_iovcnt = 1;
1710 auio.uio_td = curthread;
1711 aiov.iov_base = ap->a_target;
1712 aiov.iov_len = bytes;
1713 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1714 /* XXX handle error */
1717 hammer2_inode_unlock(nip);
1721 * Update dip's mtime
1726 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1727 hammer2_update_time(&mtime);
1728 hammer2_inode_modify(dip);
1729 dip->meta.mtime = mtime;
1730 hammer2_inode_unlock(dip);
1733 hammer2_trans_done(dip->pmp);
1736 * Finalize namecache
1739 cache_setunresolved(ap->a_nch);
1740 cache_setvp(ap->a_nch, *ap->a_vpp);
1741 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1747 * hammer2_vop_nremove { nch, dvp, cred }
1751 hammer2_vop_nremove(struct vop_nremove_args *ap)
1753 hammer2_xop_unlink_t *xop;
1754 hammer2_inode_t *dip;
1755 hammer2_inode_t *ip;
1756 struct namecache *ncp;
1760 dip = VTOI(ap->a_dvp);
1761 if (dip->pmp->ronly)
1764 /* allow removals, except user to also bulkfree */
1765 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1769 ncp = ap->a_nch->ncp;
1771 hammer2_pfs_memory_wait(dip->pmp);
1772 hammer2_trans_init(dip->pmp, 0);
1773 hammer2_inode_lock(dip, 0);
1776 * The unlink XOP unlinks the path from the directory and
1777 * locates and returns the cluster associated with the real inode.
1778 * We have to handle nlinks here on the frontend.
1780 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1781 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1784 * The namecache entry is locked so nobody can use this namespace.
1785 * Calculate isopen to determine if this namespace has an open vp
1786 * associated with it and resolve the vp only if it does.
1788 * We try to avoid resolving the vnode if nobody has it open, but
1789 * note that the test is via this namespace only.
1791 isopen = cache_isopen(ap->a_nch);
1793 xop->dopermanent = 0;
1794 hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1797 * Collect the real inode and adjust nlinks, destroy the real
1798 * inode if nlinks transitions to 0 and it was the real inode
1799 * (else it has already been removed).
1801 error = hammer2_xop_collect(&xop->head, 0);
1802 error = hammer2_error_to_errno(error);
1803 hammer2_inode_unlock(dip);
1806 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1807 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1809 hammer2_inode_unlink_finisher(ip, isopen);
1810 hammer2_inode_unlock(ip);
1813 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1817 * Update dip's mtime
1822 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1823 hammer2_update_time(&mtime);
1824 hammer2_inode_modify(dip);
1825 dip->meta.mtime = mtime;
1826 hammer2_inode_unlock(dip);
1829 hammer2_inode_run_sideq(dip->pmp, 0);
1830 hammer2_trans_done(dip->pmp);
1832 cache_unlink(ap->a_nch);
1833 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1839 * hammer2_vop_nrmdir { nch, dvp, cred }
1843 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1845 hammer2_xop_unlink_t *xop;
1846 hammer2_inode_t *dip;
1847 hammer2_inode_t *ip;
1848 struct namecache *ncp;
1852 dip = VTOI(ap->a_dvp);
1853 if (dip->pmp->ronly)
1856 /* allow removals, except user to also bulkfree */
1857 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1861 hammer2_pfs_memory_wait(dip->pmp);
1862 hammer2_trans_init(dip->pmp, 0);
1863 hammer2_inode_lock(dip, 0);
1865 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1867 ncp = ap->a_nch->ncp;
1868 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1869 isopen = cache_isopen(ap->a_nch);
1871 xop->dopermanent = 0;
1872 hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1875 * Collect the real inode and adjust nlinks, destroy the real
1876 * inode if nlinks transitions to 0 and it was the real inode
1877 * (else it has already been removed).
1879 error = hammer2_xop_collect(&xop->head, 0);
1880 error = hammer2_error_to_errno(error);
1881 hammer2_inode_unlock(dip);
1884 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1885 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1887 hammer2_inode_unlink_finisher(ip, isopen);
1888 hammer2_inode_unlock(ip);
1891 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1895 * Update dip's mtime
1900 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1901 hammer2_update_time(&mtime);
1902 hammer2_inode_modify(dip);
1903 dip->meta.mtime = mtime;
1904 hammer2_inode_unlock(dip);
1907 hammer2_inode_run_sideq(dip->pmp, 0);
1908 hammer2_trans_done(dip->pmp);
1910 cache_unlink(ap->a_nch);
1911 hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1917 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1921 hammer2_vop_nrename(struct vop_nrename_args *ap)
1923 struct namecache *fncp;
1924 struct namecache *tncp;
1925 hammer2_inode_t *fdip; /* source directory */
1926 hammer2_inode_t *tdip; /* target directory */
1927 hammer2_inode_t *ip; /* file being renamed */
1928 hammer2_inode_t *tip; /* replaced target during rename or NULL */
1929 const uint8_t *fname;
1931 const uint8_t *tname;
1938 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1940 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1943 fdip = VTOI(ap->a_fdvp); /* source directory */
1944 tdip = VTOI(ap->a_tdvp); /* target directory */
1946 if (fdip->pmp->ronly)
1948 if (hammer2_vfs_enospace(fdip, 0, ap->a_cred) > 1)
1951 fncp = ap->a_fnch->ncp; /* entry name in source */
1952 fname = fncp->nc_name;
1953 fname_len = fncp->nc_nlen;
1955 tncp = ap->a_tnch->ncp; /* entry name in target */
1956 tname = tncp->nc_name;
1957 tname_len = tncp->nc_nlen;
1959 hammer2_pfs_memory_wait(tdip->pmp);
1960 hammer2_trans_init(tdip->pmp, 0);
1965 ip = VTOI(fncp->nc_vp);
1966 hammer2_inode_ref(ip); /* extra ref */
1969 * Lookup the target name to determine if a directory entry
1970 * is being overwritten. We only hold related inode locks
1971 * temporarily, the operating system is expected to protect
1972 * against rename races.
1974 tip = tncp->nc_vp ? VTOI(tncp->nc_vp) : NULL;
1976 hammer2_inode_ref(tip); /* extra ref */
1979 * Can return NULL and error == EXDEV if the common parent
1980 * crosses a directory with the xlink flag set.
1982 * For now try to avoid deadlocks with a simple pointer address
1983 * test. (tip) can be NULL.
1987 hammer2_inode_lock(fdip, 0);
1988 hammer2_inode_lock(tdip, 0);
1990 hammer2_inode_lock(tdip, 0);
1991 hammer2_inode_lock(fdip, 0);
1995 hammer2_inode_lock(ip, 0);
1996 hammer2_inode_lock(tip, 0);
1998 hammer2_inode_lock(tip, 0);
1999 hammer2_inode_lock(ip, 0);
2002 hammer2_inode_lock(ip, 0);
2007 * Delete the target namespace.
2009 * REMOVED - NOW FOLDED INTO XOP_NRENAME OPERATION
2012 hammer2_xop_unlink_t *xop2;
2013 hammer2_inode_t *tip;
2017 * The unlink XOP unlinks the path from the directory and
2018 * locates and returns the cluster associated with the real
2019 * inode. We have to handle nlinks here on the frontend.
2021 xop2 = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
2022 hammer2_xop_setname(&xop2->head, tname, tname_len);
2023 isopen = cache_isopen(ap->a_tnch);
2025 xop2->dopermanent = 0;
2026 hammer2_xop_start(&xop2->head, hammer2_xop_unlink);
2029 * Collect the real inode and adjust nlinks, destroy the real
2030 * inode if nlinks transitions to 0 and it was the real inode
2031 * (else it has already been removed).
2033 tnch_error = hammer2_xop_collect(&xop2->head, 0);
2034 tnch_error = hammer2_error_to_errno(tnch_error);
2035 /* hammer2_inode_unlock(tdip); */
2037 if (tnch_error == 0) {
2038 tip = hammer2_inode_get(tdip->pmp, NULL,
2039 &xop2->head.cluster, -1);
2040 hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
2042 hammer2_inode_unlink_finisher(tip, isopen);
2043 hammer2_inode_unlock(tip);
2046 hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
2048 /* hammer2_inode_lock(tdip, 0); */
2050 if (tnch_error && tnch_error != ENOENT) {
2059 * Resolve the collision space for (tdip, tname, tname_len)
2061 * tdip must be held exclusively locked to prevent races since
2062 * multiple filenames can end up in the same collision space.
2065 hammer2_xop_scanlhc_t *sxop;
2066 hammer2_tid_t lhcbase;
2068 tlhc = hammer2_dirhash(tname, tname_len);
2070 sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
2072 hammer2_xop_start(&sxop->head, hammer2_xop_scanlhc);
2073 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
2074 if (tlhc != sxop->head.cluster.focus->bref.key)
2078 error = hammer2_error_to_errno(error);
2079 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
2082 if (error != ENOENT)
2087 if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) {
2094 * Ready to go, issue the rename to the backend. Note that meta-data
2095 * updates to the related inodes occur separately from the rename
2098 * NOTE: While it is not necessary to update ip->meta.name*, doing
2099 * so aids catastrophic recovery and debugging.
2102 hammer2_xop_nrename_t *xop4;
2104 xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
2106 xop4->ip_key = ip->meta.name_key;
2107 hammer2_xop_setip2(&xop4->head, ip);
2108 hammer2_xop_setip3(&xop4->head, tdip);
2109 hammer2_xop_setname(&xop4->head, fname, fname_len);
2110 hammer2_xop_setname2(&xop4->head, tname, tname_len);
2111 hammer2_xop_start(&xop4->head, hammer2_xop_nrename);
2113 error = hammer2_xop_collect(&xop4->head, 0);
2114 error = hammer2_error_to_errno(error);
2115 hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP);
2117 if (error == ENOENT)
2121 * Update inode meta-data.
2123 * WARNING! The in-memory inode (ip) structure does not
2124 * maintain a copy of the inode's filename buffer.
2127 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
2128 hammer2_inode_modify(ip);
2129 ip->meta.name_len = tname_len;
2130 ip->meta.name_key = tlhc;
2133 hammer2_inode_modify(ip);
2134 ip->meta.iparent = tdip->meta.inum;
2142 * If no error, the backend has replaced the target directory entry.
2143 * We must adjust nlinks on the original replace target if it exists.
2145 if (error == 0 && tip) {
2148 isopen = cache_isopen(ap->a_tnch);
2149 hammer2_inode_unlink_finisher(tip, isopen);
2153 * Update directory mtimes to represent the something changed.
2155 if (update_fdip || update_tdip) {
2158 hammer2_update_time(&mtime);
2160 hammer2_inode_modify(fdip);
2161 fdip->meta.mtime = mtime;
2164 hammer2_inode_modify(tdip);
2165 tdip->meta.mtime = mtime;
2169 hammer2_inode_unlock(tip);
2170 hammer2_inode_drop(tip);
2172 hammer2_inode_unlock(ip);
2173 hammer2_inode_unlock(tdip);
2174 hammer2_inode_unlock(fdip);
2175 hammer2_inode_drop(ip);
2176 hammer2_inode_run_sideq(fdip->pmp, 0);
2178 hammer2_trans_done(tdip->pmp);
2181 * Issue the namecache update after unlocking all the internal
2182 * hammer2 structures, otherwise we might deadlock.
2184 * WARNING! The target namespace must be updated atomically,
2185 * and we depend on cache_rename() to handle that for
2186 * us. Do not do a separate cache_unlink() because
2187 * that leaves a small window of opportunity for other
2188 * threads to allocate the target namespace before we
2189 * manage to complete our rename.
2191 * WARNING! cache_rename() (and cache_unlink()) will properly
2192 * set VREF_FINALIZE on any attached vnode. Do not
2193 * call cache_setunresolved() manually before-hand as
2194 * this will prevent the flag from being set later via
2195 * cache_rename(). If VREF_FINALIZE is not properly set
2196 * and the inode is no longer in the topology, related
2197 * chains can remain dirty indefinitely.
2199 if (error == 0 && tip) {
2200 /*cache_unlink(ap->a_tnch); see above */
2201 /*cache_setunresolved(ap->a_tnch); see above */
2204 cache_rename(ap->a_fnch, ap->a_tnch);
2205 hammer2_knote(ap->a_fdvp, NOTE_WRITE);
2206 hammer2_knote(ap->a_tdvp, NOTE_WRITE);
2207 hammer2_knote(fncp->nc_vp, NOTE_RENAME);
2214 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2218 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2220 hammer2_inode_t *ip;
2223 ip = VTOI(ap->a_vp);
2225 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2226 ap->a_fflag, ap->a_cred);
2232 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2239 case (MOUNTCTL_SET_EXPORT):
2240 mp = ap->a_head.a_ops->head.vv_mount;
2243 if (ap->a_ctllen != sizeof(struct export_args))
2246 rc = vfs_export(mp, &pmp->export,
2247 (const struct export_args *)ap->a_ctl);
2250 rc = vop_stdmountctl(ap);
2259 static void filt_hammer2detach(struct knote *kn);
2260 static int filt_hammer2read(struct knote *kn, long hint);
2261 static int filt_hammer2write(struct knote *kn, long hint);
2262 static int filt_hammer2vnode(struct knote *kn, long hint);
2264 static struct filterops hammer2read_filtops =
2265 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2266 NULL, filt_hammer2detach, filt_hammer2read };
2267 static struct filterops hammer2write_filtops =
2268 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2269 NULL, filt_hammer2detach, filt_hammer2write };
2270 static struct filterops hammer2vnode_filtops =
2271 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2272 NULL, filt_hammer2detach, filt_hammer2vnode };
2276 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
2278 struct vnode *vp = ap->a_vp;
2279 struct knote *kn = ap->a_kn;
2281 switch (kn->kn_filter) {
2283 kn->kn_fop = &hammer2read_filtops;
2286 kn->kn_fop = &hammer2write_filtops;
2289 kn->kn_fop = &hammer2vnode_filtops;
2292 return (EOPNOTSUPP);
2295 kn->kn_hook = (caddr_t)vp;
2297 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2303 filt_hammer2detach(struct knote *kn)
2305 struct vnode *vp = (void *)kn->kn_hook;
2307 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2311 filt_hammer2read(struct knote *kn, long hint)
2313 struct vnode *vp = (void *)kn->kn_hook;
2314 hammer2_inode_t *ip = VTOI(vp);
2317 if (hint == NOTE_REVOKE) {
2318 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2321 off = ip->meta.size - kn->kn_fp->f_offset;
2322 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2323 if (kn->kn_sfflags & NOTE_OLDAPI)
2325 return (kn->kn_data != 0);
2330 filt_hammer2write(struct knote *kn, long hint)
2332 if (hint == NOTE_REVOKE)
2333 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2339 filt_hammer2vnode(struct knote *kn, long hint)
2341 if (kn->kn_sfflags & hint)
2342 kn->kn_fflags |= hint;
2343 if (hint == NOTE_REVOKE) {
2344 kn->kn_flags |= (EV_EOF | EV_NODATA);
2347 return (kn->kn_fflags != 0);
2355 hammer2_vop_markatime(struct vop_markatime_args *ap)
2357 hammer2_inode_t *ip;
2370 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2374 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2376 error = hammer2_vop_kqfilter(ap);
2383 struct vop_ops hammer2_vnode_vops = {
2384 .vop_default = vop_defaultop,
2385 .vop_fsync = hammer2_vop_fsync,
2386 .vop_getpages = vop_stdgetpages,
2387 .vop_putpages = vop_stdputpages,
2388 .vop_access = hammer2_vop_access,
2389 .vop_advlock = hammer2_vop_advlock,
2390 .vop_close = hammer2_vop_close,
2391 .vop_nlink = hammer2_vop_nlink,
2392 .vop_ncreate = hammer2_vop_ncreate,
2393 .vop_nsymlink = hammer2_vop_nsymlink,
2394 .vop_nremove = hammer2_vop_nremove,
2395 .vop_nrmdir = hammer2_vop_nrmdir,
2396 .vop_nrename = hammer2_vop_nrename,
2397 .vop_getattr = hammer2_vop_getattr,
2398 .vop_setattr = hammer2_vop_setattr,
2399 .vop_readdir = hammer2_vop_readdir,
2400 .vop_readlink = hammer2_vop_readlink,
2401 .vop_getpages = vop_stdgetpages,
2402 .vop_putpages = vop_stdputpages,
2403 .vop_read = hammer2_vop_read,
2404 .vop_write = hammer2_vop_write,
2405 .vop_open = hammer2_vop_open,
2406 .vop_inactive = hammer2_vop_inactive,
2407 .vop_reclaim = hammer2_vop_reclaim,
2408 .vop_nresolve = hammer2_vop_nresolve,
2409 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2410 .vop_nmkdir = hammer2_vop_nmkdir,
2411 .vop_nmknod = hammer2_vop_nmknod,
2412 .vop_ioctl = hammer2_vop_ioctl,
2413 .vop_mountctl = hammer2_vop_mountctl,
2414 .vop_bmap = hammer2_vop_bmap,
2415 .vop_strategy = hammer2_vop_strategy,
2416 .vop_kqfilter = hammer2_vop_kqfilter
2419 struct vop_ops hammer2_spec_vops = {
2420 .vop_default = vop_defaultop,
2421 .vop_fsync = hammer2_vop_fsync,
2422 .vop_read = vop_stdnoread,
2423 .vop_write = vop_stdnowrite,
2424 .vop_access = hammer2_vop_access,
2425 .vop_close = hammer2_vop_close,
2426 .vop_markatime = hammer2_vop_markatime,
2427 .vop_getattr = hammer2_vop_getattr,
2428 .vop_inactive = hammer2_vop_inactive,
2429 .vop_reclaim = hammer2_vop_reclaim,
2430 .vop_setattr = hammer2_vop_setattr
2433 struct vop_ops hammer2_fifo_vops = {
2434 .vop_default = fifo_vnoperate,
2435 .vop_fsync = hammer2_vop_fsync,
2437 .vop_read = hammer2_vop_fiforead,
2438 .vop_write = hammer2_vop_fifowrite,
2440 .vop_access = hammer2_vop_access,
2442 .vop_close = hammer2_vop_fifoclose,
2444 .vop_markatime = hammer2_vop_markatime,
2445 .vop_getattr = hammer2_vop_getattr,
2446 .vop_inactive = hammer2_vop_inactive,
2447 .vop_reclaim = hammer2_vop_reclaim,
2448 .vop_setattr = hammer2_vop_setattr,
2449 .vop_kqfilter = hammer2_vop_fifokqfilter