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_truncate_file(ip, vap->va_size);
431 hammer2_extend_file(ip, vap->va_size);
433 hammer2_inode_modify(ip);
434 ip->meta.mtime = ctime;
442 /* atime not supported */
443 if (vap->va_atime.tv_sec != VNOVAL) {
444 hammer2_inode_modify(ip);
445 ip->meta.atime = hammer2_timespec_to_time(&vap->va_atime);
446 kflags |= NOTE_ATTRIB;
449 if (vap->va_mode != (mode_t)VNOVAL) {
450 mode_t cur_mode = ip->meta.mode;
451 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
452 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
454 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
455 cur_uid, cur_gid, &cur_mode);
456 if (error == 0 && ip->meta.mode != cur_mode) {
457 hammer2_inode_modify(ip);
458 ip->meta.mode = cur_mode;
459 ip->meta.ctime = ctime;
460 kflags |= NOTE_ATTRIB;
464 if (vap->va_mtime.tv_sec != VNOVAL) {
465 hammer2_inode_modify(ip);
466 ip->meta.mtime = hammer2_timespec_to_time(&vap->va_mtime);
467 kflags |= NOTE_ATTRIB;
472 * If a truncation occurred we must call inode_fsync() now in order
473 * to trim the related data chains, otherwise a later expansion can
476 * If an extend occured that changed the DIRECTDATA state, we must
477 * call inode_fsync now in order to prepare the inode's indirect
480 if (ip->flags & HAMMER2_INODE_RESIZED)
481 hammer2_inode_chain_sync(ip);
486 hammer2_inode_unlock(ip);
487 hammer2_trans_done(ip->pmp);
488 hammer2_knote(ip->vp, kflags);
496 hammer2_vop_readdir(struct vop_readdir_args *ap)
498 hammer2_xop_readdir_t *xop;
499 hammer2_blockref_t bref;
516 saveoff = uio->uio_offset;
521 * Setup cookies directory entry cookies if requested
523 if (ap->a_ncookies) {
524 ncookies = uio->uio_resid / 16 + 1;
527 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
534 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
537 * Handle artificial entries. To ensure that only positive 64 bit
538 * quantities are returned to userland we always strip off bit 63.
539 * The hash code is designed such that codes 0x0000-0x7FFF are not
540 * used, allowing us to use these codes for articial entries.
542 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
543 * allow '..' to cross the mount point into (e.g.) the super-root.
546 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
547 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
551 cookies[cookie_index] = saveoff;
554 if (cookie_index == ncookies)
560 * Be careful with lockorder when accessing ".."
562 * (ip is the current dir. xip is the parent dir).
564 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
565 if (ip->pip && ip != ip->pmp->iroot)
566 inum = ip->pip->meta.inum & HAMMER2_DIRHASH_USERMSK;
567 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
571 cookies[cookie_index] = saveoff;
574 if (cookie_index == ncookies)
578 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
579 if (hammer2_debug & 0x0020)
580 kprintf("readdir: lkey %016jx\n", lkey);
585 * Use XOP for cluster scan.
587 * parent is the inode cluster, already locked for us. Don't
588 * double lock shared locks as this will screw up upgrades.
590 xop = hammer2_xop_alloc(ip, 0);
592 hammer2_xop_start(&xop->head, hammer2_xop_readdir);
595 const hammer2_inode_data_t *ripdata;
597 error = hammer2_xop_collect(&xop->head, 0);
600 if (cookie_index == ncookies)
602 if (hammer2_debug & 0x0020)
603 kprintf("cluster chain %p %p\n",
604 xop->head.cluster.focus,
605 (xop->head.cluster.focus ?
606 xop->head.cluster.focus->data : (void *)-1));
607 ripdata = &hammer2_cluster_rdata(&xop->head.cluster)->ipdata;
608 hammer2_cluster_bref(&xop->head.cluster, &bref);
609 if (bref.type == HAMMER2_BREF_TYPE_INODE) {
610 dtype = hammer2_get_dtype(ripdata);
611 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
612 r = vop_write_dirent(&error, uio,
614 HAMMER2_DIRHASH_USERMSK,
616 ripdata->meta.name_len,
621 cookies[cookie_index] = saveoff;
624 /* XXX chain error */
625 kprintf("bad chain type readdir %d\n", bref.type);
628 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
629 if (error == ENOENT) {
632 saveoff = (hammer2_key_t)-1;
634 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
637 hammer2_inode_unlock(ip);
639 *ap->a_eofflag = eofflag;
640 if (hammer2_debug & 0x0020)
641 kprintf("readdir: done at %016jx\n", saveoff);
642 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
643 if (error && cookie_index == 0) {
645 kfree(cookies, M_TEMP);
647 *ap->a_cookies = NULL;
651 *ap->a_ncookies = cookie_index;
652 *ap->a_cookies = cookies;
660 * hammer2_vop_readlink { vp, uio, cred }
664 hammer2_vop_readlink(struct vop_readlink_args *ap)
671 if (vp->v_type != VLNK)
675 error = hammer2_read_file(ip, ap->a_uio, 0);
681 hammer2_vop_read(struct vop_read_args *ap)
691 * Read operations supported on this vnode?
694 if (vp->v_type != VREG)
704 seqcount = ap->a_ioflag >> 16;
705 bigread = (uio->uio_resid > 100 * 1024 * 1024);
707 error = hammer2_read_file(ip, uio, seqcount);
713 hammer2_vop_write(struct vop_write_args *ap)
723 * Read operations supported on this vnode?
726 if (vp->v_type != VREG)
735 if (ip->pmp->ronly) {
739 seqcount = ap->a_ioflag >> 16;
742 * Check resource limit
744 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
745 uio->uio_offset + uio->uio_resid >
746 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
747 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
752 * The transaction interlocks against flushes initiations
753 * (note: but will run concurrently with the actual flush).
755 hammer2_trans_init(ip->pmp, 0);
756 error = hammer2_write_file(ip, uio, ap->a_ioflag, seqcount);
757 hammer2_trans_done(ip->pmp);
763 * Perform read operations on a file or symlink given an UNLOCKED
766 * The passed ip is not locked.
770 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
781 * WARNING! Assumes that the kernel interlocks size changes at the
784 hammer2_mtx_sh(&ip->lock);
785 size = ip->meta.size;
786 hammer2_mtx_unlock(&ip->lock);
788 while (uio->uio_resid > 0 && uio->uio_offset < size) {
795 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
798 error = cluster_read(ip->vp, leof, lbase, lblksize,
799 uio->uio_resid, seqcount * BKVASIZE,
804 loff = (int)(uio->uio_offset - lbase);
806 if (n > uio->uio_resid)
808 if (n > size - uio->uio_offset)
809 n = (int)(size - uio->uio_offset);
810 bp->b_flags |= B_AGE;
811 uiomove((char *)bp->b_data + loff, n, uio);
818 * Write to the file represented by the inode via the logical buffer cache.
819 * The inode may represent a regular file or a symlink.
821 * The inode must not be locked.
825 hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
826 int ioflag, int seqcount)
828 hammer2_key_t old_eof;
829 hammer2_key_t new_eof;
838 * WARNING! Assumes that the kernel interlocks size changes at the
841 hammer2_mtx_ex(&ip->lock);
842 if (ioflag & IO_APPEND)
843 uio->uio_offset = ip->meta.size;
844 old_eof = ip->meta.size;
847 * Extend the file if necessary. If the write fails at some point
848 * we will truncate it back down to cover as much as we were able
851 * Doing this now makes it easier to calculate buffer sizes in
858 if (uio->uio_offset + uio->uio_resid > old_eof) {
859 new_eof = uio->uio_offset + uio->uio_resid;
861 hammer2_extend_file(ip, new_eof);
862 kflags |= NOTE_EXTEND;
866 hammer2_mtx_unlock(&ip->lock);
871 while (uio->uio_resid > 0) {
880 * Don't allow the buffer build to blow out the buffer
883 if ((ioflag & IO_RECURSE) == 0)
884 bwillwrite(HAMMER2_PBUFSIZE);
887 * This nominally tells us how much we can cluster and
888 * what the logical buffer size needs to be. Currently
889 * we don't try to cluster the write and just handle one
892 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
894 loff = (int)(uio->uio_offset - lbase);
896 KKASSERT(lblksize <= 65536);
899 * Calculate bytes to copy this transfer and whether the
900 * copy completely covers the buffer or not.
904 if (n > uio->uio_resid) {
906 if (loff == lbase && uio->uio_offset + n == new_eof)
918 if (uio->uio_segflg == UIO_NOCOPY) {
920 * Issuing a write with the same data backing the
921 * buffer. Instantiate the buffer to collect the
922 * backing vm pages, then read-in any missing bits.
924 * This case is used by vop_stdputpages().
926 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
927 if ((bp->b_flags & B_CACHE) == 0) {
929 error = bread(ip->vp, lbase, lblksize, &bp);
931 } else if (trivial) {
933 * Even though we are entirely overwriting the buffer
934 * we may still have to zero it out to avoid a
935 * mmap/write visibility issue.
937 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
938 if ((bp->b_flags & B_CACHE) == 0)
942 * Partial overwrite, read in any missing bits then
943 * replace the portion being written.
945 * (The strategy code will detect zero-fill physical
946 * blocks for this case).
948 error = bread(ip->vp, lbase, lblksize, &bp);
959 * Ok, copy the data in
961 error = uiomove(bp->b_data + loff, n, uio);
962 kflags |= NOTE_WRITE;
970 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
971 * with IO_SYNC or IO_ASYNC set. These writes
972 * must be handled as the pageout daemon expects.
974 if (ioflag & IO_SYNC) {
976 } else if ((ioflag & IO_DIRECT) && endofblk) {
978 } else if (ioflag & IO_ASYNC) {
986 * Cleanup. If we extended the file EOF but failed to write through
987 * the entire write is a failure and we have to back-up.
989 if (error && new_eof != old_eof) {
990 hammer2_mtx_ex(&ip->lock);
991 hammer2_truncate_file(ip, old_eof);
992 if (ip->flags & HAMMER2_INODE_MODIFIED)
993 hammer2_inode_chain_sync(ip);
994 hammer2_mtx_unlock(&ip->lock);
995 } else if (modified) {
996 hammer2_mtx_ex(&ip->lock);
997 hammer2_inode_modify(ip);
998 hammer2_update_time(&ip->meta.mtime);
999 if (ip->flags & HAMMER2_INODE_MODIFIED)
1000 hammer2_inode_chain_sync(ip);
1001 hammer2_mtx_unlock(&ip->lock);
1002 hammer2_knote(ip->vp, kflags);
1004 hammer2_trans_assert_strategy(ip->pmp);
1010 * Truncate the size of a file. The inode must not be locked.
1012 * We must unconditionally set HAMMER2_INODE_RESIZED to properly
1013 * ensure that any on-media data beyond the new file EOF has been destroyed.
1015 * WARNING: nvtruncbuf() can only be safely called without the inode lock
1016 * held due to the way our write thread works. If the truncation
1017 * occurs in the middle of a buffer, nvtruncbuf() is responsible
1018 * for dirtying that buffer and zeroing out trailing bytes.
1020 * WARNING! Assumes that the kernel interlocks size changes at the
1023 * WARNING! Caller assumes responsibility for removing dead blocks
1024 * if INODE_RESIZED is set.
1028 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1030 hammer2_key_t lbase;
1034 hammer2_mtx_unlock(&ip->lock);
1036 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1037 nvtruncbuf(ip->vp, nsize,
1038 nblksize, (int)nsize & (nblksize - 1),
1041 hammer2_mtx_ex(&ip->lock);
1042 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1043 ip->osize = ip->meta.size;
1044 ip->meta.size = nsize;
1045 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1046 hammer2_inode_modify(ip);
1051 * Extend the size of a file. The inode must not be locked.
1053 * Even though the file size is changing, we do not have to set the
1054 * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES
1055 * boundary. When this occurs a hammer2_inode_chain_sync() is required
1056 * to prepare the inode cluster's indirect block table, otherwise
1057 * async execution of the strategy code will implode on us.
1059 * WARNING! Assumes that the kernel interlocks size changes at the
1062 * WARNING! Caller assumes responsibility for transitioning out
1063 * of the inode DIRECTDATA mode if INODE_RESIZED is set.
1067 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1069 hammer2_key_t lbase;
1070 hammer2_key_t osize;
1076 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1077 hammer2_inode_modify(ip);
1078 osize = ip->meta.size;
1080 ip->meta.size = nsize;
1082 if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES) {
1083 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1084 hammer2_inode_chain_sync(ip);
1087 hammer2_mtx_unlock(&ip->lock);
1089 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1090 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1096 hammer2_mtx_ex(&ip->lock);
1103 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1105 hammer2_xop_nresolve_t *xop;
1106 hammer2_inode_t *ip;
1107 hammer2_inode_t *dip;
1108 struct namecache *ncp;
1113 dip = VTOI(ap->a_dvp);
1114 xop = hammer2_xop_alloc(dip, 0);
1116 ncp = ap->a_nch->ncp;
1117 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1120 * Note: In DragonFly the kernel handles '.' and '..'.
1122 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1123 hammer2_xop_start(&xop->head, hammer2_xop_nresolve);
1125 error = hammer2_xop_collect(&xop->head, 0);
1129 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1131 hammer2_inode_unlock(dip);
1134 * Acquire the related vnode
1136 * NOTE: For error processing, only ENOENT resolves the namecache
1137 * entry to NULL, otherwise we just return the error and
1138 * leave the namecache unresolved.
1140 * NOTE: multiple hammer2_inode structures can be aliased to the
1141 * same chain element, for example for hardlinks. This
1142 * use case does not 'reattach' inode associations that
1143 * might already exist, but always allocates a new one.
1145 * WARNING: inode structure is locked exclusively via inode_get
1146 * but chain was locked shared. inode_unlock()
1147 * will handle it properly.
1150 vp = hammer2_igetv(ip, &error);
1153 cache_setvp(ap->a_nch, vp);
1154 } else if (error == ENOENT) {
1155 cache_setvp(ap->a_nch, NULL);
1157 hammer2_inode_unlock(ip);
1160 * The vp should not be released until after we've disposed
1161 * of our locks, because it might cause vop_inactive() to
1168 cache_setvp(ap->a_nch, NULL);
1170 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1171 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1172 ("resolve error %d/%p ap %p\n",
1173 error, ap->a_nch->ncp->nc_vp, ap));
1181 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1183 hammer2_inode_t *dip;
1184 hammer2_inode_t *ip;
1188 dip = VTOI(ap->a_dvp);
1190 if ((ip = dip->pip) == NULL) {
1195 hammer2_inode_lock(ip, 0);
1196 *ap->a_vpp = hammer2_igetv(ip, &error);
1197 hammer2_inode_unlock(ip);
1205 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1207 hammer2_inode_t *dip;
1208 hammer2_inode_t *nip;
1209 struct namecache *ncp;
1210 const uint8_t *name;
1215 dip = VTOI(ap->a_dvp);
1216 if (dip->pmp->ronly) {
1221 ncp = ap->a_nch->ncp;
1222 name = ncp->nc_name;
1223 name_len = ncp->nc_nlen;
1225 hammer2_pfs_memory_wait(dip->pmp);
1226 hammer2_trans_init(dip->pmp, 0);
1227 nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1229 hammer2_trans_newinum(dip->pmp), 0, 0,
1232 KKASSERT(nip == NULL);
1235 *ap->a_vpp = hammer2_igetv(nip, &error);
1236 hammer2_inode_unlock(nip);
1238 hammer2_trans_done(dip->pmp);
1241 cache_setunresolved(ap->a_nch);
1242 cache_setvp(ap->a_nch, *ap->a_vpp);
1250 hammer2_vop_open(struct vop_open_args *ap)
1252 return vop_stdopen(ap);
1256 * hammer2_vop_advlock { vp, id, op, fl, flags }
1260 hammer2_vop_advlock(struct vop_advlock_args *ap)
1262 hammer2_inode_t *ip = VTOI(ap->a_vp);
1265 size = ip->meta.size;
1266 return (lf_advlock(ap, &ip->advlock, size));
1271 hammer2_vop_close(struct vop_close_args *ap)
1273 return vop_stdclose(ap);
1277 * hammer2_vop_nlink { nch, dvp, vp, cred }
1279 * Create a hardlink from (vp) to {dvp, nch}.
1283 hammer2_vop_nlink(struct vop_nlink_args *ap)
1285 hammer2_xop_nlink_t *xop1;
1286 hammer2_inode_t *fdip; /* target directory to create link in */
1287 hammer2_inode_t *tdip; /* target directory to create link in */
1288 hammer2_inode_t *cdip; /* common parent directory */
1289 hammer2_inode_t *ip; /* inode we are hardlinking to */
1290 struct namecache *ncp;
1291 const uint8_t *name;
1297 tdip = VTOI(ap->a_dvp);
1298 if (tdip->pmp->ronly) {
1303 ncp = ap->a_nch->ncp;
1304 name = ncp->nc_name;
1305 name_len = ncp->nc_nlen;
1308 * ip represents the file being hardlinked. The file could be a
1309 * normal file or a hardlink target if it has already been hardlinked.
1310 * If ip is a hardlinked target then ip->pip represents the location
1311 * of the hardlinked target, NOT the location of the hardlink pointer.
1313 * Bump nlinks and potentially also create or move the hardlink
1314 * target in the parent directory common to (ip) and (tdip). The
1315 * consolidation code can modify ip->cluster and ip->pip. The
1316 * returned cluster is locked.
1318 ip = VTOI(ap->a_vp);
1319 hammer2_pfs_memory_wait(ip->pmp);
1320 hammer2_trans_init(ip->pmp, 0);
1323 * The common parent directory must be locked first to avoid deadlocks.
1324 * Also note that fdip and/or tdip might match cdip.
1326 * WARNING! The kernel's namecache locks are insufficient for
1327 * protecting us from hardlink shifts, since unrelated
1328 * rename() or link() calls on parent directories might
1329 * cause a shift. A PFS-wide lock is required for this
1332 if (ip->meta.type == HAMMER2_OBJTYPE_DIRECTORY ||
1333 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1334 lockmgr(&ip->pmp->lock_nlink, LK_EXCLUSIVE);
1343 * Can return NULL and error == EXDEV if the common parent
1344 * crosses a directory with the xlink flag set.
1346 cdip = hammer2_inode_common_parent(fdip, tdip, &error, 1);
1348 hammer2_inode_lock(cdip, 0);
1349 hammer2_inode_lock(fdip, 0);
1350 hammer2_inode_lock(tdip, 0);
1351 hammer2_inode_lock(ip, 0);
1354 * Dispatch xop_nlink unconditionally since we have to update nlinks.
1356 * Otherwise we'd be able to avoid the XOP if the ip does not have
1357 * to be converted or moved.
1358 * If ip is not a hardlink target we must convert it to a hardlink.
1359 * If fdip != cdip we must shift the inode to cdip.
1361 * XXX this and other nlink update usage should be passed top-down
1362 * and not updated with a delta bottom-up.
1365 if (fdip != cdip || (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE))
1368 xop1 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
1369 hammer2_xop_setip2(&xop1->head, ip);
1370 hammer2_xop_setip3(&xop1->head, cdip);
1371 xop1->nlinks_delta = 1;
1373 hammer2_xop_start(&xop1->head, hammer2_xop_nlink);
1374 error = hammer2_xop_collect(&xop1->head, 0);
1375 hammer2_xop_retire(&xop1->head, HAMMER2_XOPMASK_VOP);
1376 if (error == ENOENT)
1381 * Must synchronize original inode whos chains are now a hardlink
1382 * target. We must match what the backend XOP did to the
1385 if (error == 0 && (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1386 hammer2_inode_modify(ip);
1387 ip->meta.name_key = ip->meta.inum;
1388 ip->meta.name_len = 18; /* "0x%016jx" */
1392 * Create the hardlink target and bump nlinks.
1395 hammer2_inode_create(tdip, NULL, NULL,
1398 HAMMER2_OBJTYPE_HARDLINK, ip->meta.type,
1400 hammer2_inode_modify(ip);
1404 cache_setunresolved(ap->a_nch);
1405 cache_setvp(ap->a_nch, ap->a_vp);
1407 hammer2_inode_unlock(ip);
1408 hammer2_inode_unlock(tdip);
1409 hammer2_inode_unlock(fdip);
1411 hammer2_inode_unlock(cdip);
1412 hammer2_inode_drop(cdip);
1416 lockmgr(&ip->pmp->lock_nlink, LK_RELEASE);
1417 hammer2_trans_done(ip->pmp);
1424 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1426 * The operating system has already ensured that the directory entry
1427 * does not exist and done all appropriate namespace locking.
1431 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1433 hammer2_inode_t *dip;
1434 hammer2_inode_t *nip;
1435 struct namecache *ncp;
1436 const uint8_t *name;
1441 dip = VTOI(ap->a_dvp);
1442 if (dip->pmp->ronly) {
1447 ncp = ap->a_nch->ncp;
1448 name = ncp->nc_name;
1449 name_len = ncp->nc_nlen;
1450 hammer2_pfs_memory_wait(dip->pmp);
1451 hammer2_trans_init(dip->pmp, 0);
1453 nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1455 hammer2_trans_newinum(dip->pmp), 0, 0,
1458 KKASSERT(nip == NULL);
1461 *ap->a_vpp = hammer2_igetv(nip, &error);
1462 hammer2_inode_unlock(nip);
1464 hammer2_trans_done(dip->pmp);
1467 cache_setunresolved(ap->a_nch);
1468 cache_setvp(ap->a_nch, *ap->a_vpp);
1475 * Make a device node (typically a fifo)
1479 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1481 hammer2_inode_t *dip;
1482 hammer2_inode_t *nip;
1483 struct namecache *ncp;
1484 const uint8_t *name;
1489 dip = VTOI(ap->a_dvp);
1490 if (dip->pmp->ronly) {
1495 ncp = ap->a_nch->ncp;
1496 name = ncp->nc_name;
1497 name_len = ncp->nc_nlen;
1498 hammer2_pfs_memory_wait(dip->pmp);
1499 hammer2_trans_init(dip->pmp, 0);
1501 nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1503 hammer2_trans_newinum(dip->pmp), 0, 0,
1506 KKASSERT(nip == NULL);
1509 *ap->a_vpp = hammer2_igetv(nip, &error);
1510 hammer2_inode_unlock(nip);
1512 hammer2_trans_done(dip->pmp);
1515 cache_setunresolved(ap->a_nch);
1516 cache_setvp(ap->a_nch, *ap->a_vpp);
1523 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1527 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1529 hammer2_inode_t *dip;
1530 hammer2_inode_t *nip;
1531 struct namecache *ncp;
1532 const uint8_t *name;
1536 dip = VTOI(ap->a_dvp);
1537 if (dip->pmp->ronly)
1540 ncp = ap->a_nch->ncp;
1541 name = ncp->nc_name;
1542 name_len = ncp->nc_nlen;
1543 hammer2_pfs_memory_wait(dip->pmp);
1544 hammer2_trans_init(dip->pmp, 0);
1546 ap->a_vap->va_type = VLNK; /* enforce type */
1548 nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1550 hammer2_trans_newinum(dip->pmp), 0, 0,
1553 KKASSERT(nip == NULL);
1555 hammer2_trans_done(dip->pmp);
1558 *ap->a_vpp = hammer2_igetv(nip, &error);
1561 * Build the softlink (~like file data) and finalize the namecache.
1568 bytes = strlen(ap->a_target);
1570 hammer2_inode_unlock(nip);
1571 bzero(&auio, sizeof(auio));
1572 bzero(&aiov, sizeof(aiov));
1573 auio.uio_iov = &aiov;
1574 auio.uio_segflg = UIO_SYSSPACE;
1575 auio.uio_rw = UIO_WRITE;
1576 auio.uio_resid = bytes;
1577 auio.uio_iovcnt = 1;
1578 auio.uio_td = curthread;
1579 aiov.iov_base = ap->a_target;
1580 aiov.iov_len = bytes;
1581 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1582 /* XXX handle error */
1585 hammer2_inode_unlock(nip);
1587 hammer2_trans_done(dip->pmp);
1590 * Finalize namecache
1593 cache_setunresolved(ap->a_nch);
1594 cache_setvp(ap->a_nch, *ap->a_vpp);
1595 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1601 * hammer2_vop_nremove { nch, dvp, cred }
1605 hammer2_vop_nremove(struct vop_nremove_args *ap)
1607 hammer2_xop_unlink_t *xop;
1608 hammer2_inode_t *dip;
1609 hammer2_inode_t *ip;
1610 struct namecache *ncp;
1615 dip = VTOI(ap->a_dvp);
1616 if (dip->pmp->ronly) {
1621 ncp = ap->a_nch->ncp;
1623 hammer2_pfs_memory_wait(dip->pmp);
1624 hammer2_trans_init(dip->pmp, 0);
1625 hammer2_inode_lock(dip, 0);
1628 * The unlink XOP unlinks the path from the directory and
1629 * locates and returns the cluster associated with the real inode.
1630 * We have to handle nlinks here on the frontend.
1632 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1633 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1634 isopen = cache_isopen(ap->a_nch);
1636 xop->dopermanent = isopen ? 0 : HAMMER2_DELETE_PERMANENT;
1637 hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1640 * Collect the real inode and adjust nlinks, destroy the real
1641 * inode if nlinks transitions to 0 and it was the real inode
1642 * (else it has already been removed).
1644 error = hammer2_xop_collect(&xop->head, 0);
1645 hammer2_inode_unlock(dip);
1648 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1649 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1651 hammer2_inode_unlink_finisher(ip, isopen);
1652 hammer2_inode_unlock(ip);
1655 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1658 hammer2_inode_run_sideq(dip->pmp);
1659 hammer2_trans_done(dip->pmp);
1661 cache_unlink(ap->a_nch);
1667 * hammer2_vop_nrmdir { nch, dvp, cred }
1671 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1673 hammer2_xop_unlink_t *xop;
1674 hammer2_inode_t *dip;
1675 hammer2_inode_t *ip;
1676 struct namecache *ncp;
1681 dip = VTOI(ap->a_dvp);
1682 if (dip->pmp->ronly) {
1687 hammer2_pfs_memory_wait(dip->pmp);
1688 hammer2_trans_init(dip->pmp, 0);
1689 hammer2_inode_lock(dip, 0);
1691 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1693 ncp = ap->a_nch->ncp;
1694 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1695 isopen = cache_isopen(ap->a_nch);
1697 xop->dopermanent = isopen ? 0 : HAMMER2_DELETE_PERMANENT;
1698 hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1701 * Collect the real inode and adjust nlinks, destroy the real
1702 * inode if nlinks transitions to 0 and it was the real inode
1703 * (else it has already been removed).
1705 error = hammer2_xop_collect(&xop->head, 0);
1706 hammer2_inode_unlock(dip);
1709 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1710 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1712 hammer2_inode_unlink_finisher(ip, isopen);
1713 hammer2_inode_unlock(ip);
1716 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1718 hammer2_inode_run_sideq(dip->pmp);
1719 hammer2_trans_done(dip->pmp);
1721 cache_unlink(ap->a_nch);
1727 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1731 hammer2_vop_nrename(struct vop_nrename_args *ap)
1733 struct namecache *fncp;
1734 struct namecache *tncp;
1735 hammer2_inode_t *cdip;
1736 hammer2_inode_t *fdip;
1737 hammer2_inode_t *tdip;
1738 hammer2_inode_t *ip;
1739 const uint8_t *fname;
1741 const uint8_t *tname;
1748 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1750 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1753 fdip = VTOI(ap->a_fdvp); /* source directory */
1754 tdip = VTOI(ap->a_tdvp); /* target directory */
1756 if (fdip->pmp->ronly)
1760 fncp = ap->a_fnch->ncp; /* entry name in source */
1761 fname = fncp->nc_name;
1762 fname_len = fncp->nc_nlen;
1764 tncp = ap->a_tnch->ncp; /* entry name in target */
1765 tname = tncp->nc_name;
1766 tname_len = tncp->nc_nlen;
1768 hammer2_pfs_memory_wait(tdip->pmp);
1769 hammer2_trans_init(tdip->pmp, 0);
1772 * ip is the inode being renamed. If this is a hardlink then
1773 * ip represents the actual file and not the hardlink marker.
1775 ip = VTOI(fncp->nc_vp);
1778 * The common parent directory must be locked first to avoid deadlocks.
1779 * Also note that fdip and/or tdip might match cdip.
1781 * WARNING! The kernel's namecache locks are insufficient for
1782 * protecting us from hardlink shifts, since unrelated
1783 * rename() or link() calls on parent directories might
1784 * cause a shift. A PFS-wide lock is required for this
1787 if (ip->meta.type == HAMMER2_OBJTYPE_DIRECTORY ||
1788 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1789 lockmgr(&ip->pmp->lock_nlink, LK_EXCLUSIVE);
1796 * Can return NULL and error == EXDEV if the common parent
1797 * crosses a directory with the xlink flag set.
1800 cdip = hammer2_inode_common_parent(ip->pip, tdip, &error, 0);
1805 hammer2_inode_lock(cdip, 0);
1806 hammer2_inode_lock(fdip, 0);
1807 hammer2_inode_lock(tdip, 0);
1808 hammer2_inode_ref(ip); /* extra ref */
1811 * If ip is a hardlink target and fdip != cdip we must shift the
1814 hammer2_inode_lock(ip, 0);
1817 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1818 hammer2_xop_nlink_t *xop1;
1820 xop1 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
1821 hammer2_xop_setip2(&xop1->head, ip);
1822 hammer2_xop_setip3(&xop1->head, cdip);
1823 xop1->nlinks_delta = 0;
1825 hammer2_xop_start(&xop1->head, hammer2_xop_nlink);
1826 error = hammer2_xop_collect(&xop1->head, 0);
1827 hammer2_xop_retire(&xop1->head, HAMMER2_XOPMASK_VOP);
1829 /* hammer2_inode_unlock(ip); */
1832 * Delete the target namespace.
1835 hammer2_xop_unlink_t *xop2;
1836 hammer2_inode_t *tip;
1840 * The unlink XOP unlinks the path from the directory and
1841 * locates and returns the cluster associated with the real
1842 * inode. We have to handle nlinks here on the frontend.
1844 xop2 = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
1845 hammer2_xop_setname(&xop2->head, tname, tname_len);
1846 isopen = cache_isopen(ap->a_tnch);
1848 xop2->dopermanent = isopen ? 0 : HAMMER2_DELETE_PERMANENT;
1849 hammer2_xop_start(&xop2->head, hammer2_xop_unlink);
1852 * Collect the real inode and adjust nlinks, destroy the real
1853 * inode if nlinks transitions to 0 and it was the real inode
1854 * (else it has already been removed).
1856 tnch_error = hammer2_xop_collect(&xop2->head, 0);
1857 /* hammer2_inode_unlock(tdip); */
1859 if (tnch_error == 0) {
1860 tip = hammer2_inode_get(tdip->pmp, NULL,
1861 &xop2->head.cluster, -1);
1862 hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
1864 hammer2_inode_unlink_finisher(tip, isopen);
1865 hammer2_inode_unlock(tip);
1868 hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
1870 /* hammer2_inode_lock(tdip, 0); */
1872 if (tnch_error && tnch_error != ENOENT) {
1879 * Resolve the collision space for (tdip, tname, tname_len)
1881 * tdip must be held exclusively locked to prevent races.
1884 hammer2_xop_scanlhc_t *sxop;
1885 hammer2_tid_t lhcbase;
1887 tlhc = hammer2_dirhash(tname, tname_len);
1889 sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
1891 hammer2_xop_start(&sxop->head, hammer2_xop_scanlhc);
1892 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
1893 if (tlhc != sxop->head.cluster.focus->bref.key)
1897 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
1900 if (error != ENOENT)
1905 if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) {
1912 * Everything is setup, do the rename.
1914 * We have to synchronize ip->meta to the underlying operation.
1916 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
1917 * unlinking elements from their directories. Locking
1918 * the nlinks field does not lock the whole inode.
1920 /* hammer2_inode_lock(ip, 0); */
1922 hammer2_xop_nrename_t *xop4;
1924 xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
1926 xop4->ip_key = ip->meta.name_key;
1927 hammer2_xop_setip2(&xop4->head, ip);
1928 hammer2_xop_setip3(&xop4->head, tdip);
1929 hammer2_xop_setname(&xop4->head, fname, fname_len);
1930 hammer2_xop_setname2(&xop4->head, tname, tname_len);
1931 hammer2_xop_start(&xop4->head, hammer2_xop_nrename);
1933 error = hammer2_xop_collect(&xop4->head, 0);
1934 hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP);
1936 if (error == ENOENT)
1939 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1940 hammer2_inode_modify(ip);
1941 ip->meta.name_len = tname_len;
1942 ip->meta.name_key = tlhc;
1948 * Fixup ip->pip if we were renaming the actual file and not a
1951 if (error == 0 && (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1952 hammer2_inode_t *opip;
1954 if (ip->pip != tdip) {
1955 hammer2_inode_ref(tdip);
1959 hammer2_inode_drop(opip);
1963 hammer2_inode_unlock(ip);
1964 hammer2_inode_unlock(tdip);
1965 hammer2_inode_unlock(fdip);
1966 hammer2_inode_unlock(cdip);
1967 hammer2_inode_drop(ip);
1968 hammer2_inode_drop(cdip);
1970 hammer2_inode_run_sideq(fdip->pmp);
1973 lockmgr(&ip->pmp->lock_nlink, LK_RELEASE);
1974 hammer2_trans_done(tdip->pmp);
1977 * Issue the namecache update after unlocking all the internal
1978 * hammer structures, otherwise we might deadlock.
1980 if (tnch_error == 0) {
1981 cache_unlink(ap->a_tnch);
1982 cache_setunresolved(ap->a_tnch);
1985 cache_rename(ap->a_fnch, ap->a_tnch);
1992 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
1996 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
1998 hammer2_inode_t *ip;
2002 ip = VTOI(ap->a_vp);
2004 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2005 ap->a_fflag, ap->a_cred);
2012 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2020 case (MOUNTCTL_SET_EXPORT):
2021 mp = ap->a_head.a_ops->head.vv_mount;
2024 if (ap->a_ctllen != sizeof(struct export_args))
2027 rc = vfs_export(mp, &pmp->export,
2028 (const struct export_args *)ap->a_ctl);
2031 rc = vop_stdmountctl(ap);
2041 static void filt_hammer2detach(struct knote *kn);
2042 static int filt_hammer2read(struct knote *kn, long hint);
2043 static int filt_hammer2write(struct knote *kn, long hint);
2044 static int filt_hammer2vnode(struct knote *kn, long hint);
2046 static struct filterops hammer2read_filtops =
2047 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2048 NULL, filt_hammer2detach, filt_hammer2read };
2049 static struct filterops hammer2write_filtops =
2050 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2051 NULL, filt_hammer2detach, filt_hammer2write };
2052 static struct filterops hammer2vnode_filtops =
2053 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2054 NULL, filt_hammer2detach, filt_hammer2vnode };
2058 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
2060 struct vnode *vp = ap->a_vp;
2061 struct knote *kn = ap->a_kn;
2063 switch (kn->kn_filter) {
2065 kn->kn_fop = &hammer2read_filtops;
2068 kn->kn_fop = &hammer2write_filtops;
2071 kn->kn_fop = &hammer2vnode_filtops;
2074 return (EOPNOTSUPP);
2077 kn->kn_hook = (caddr_t)vp;
2079 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2085 filt_hammer2detach(struct knote *kn)
2087 struct vnode *vp = (void *)kn->kn_hook;
2089 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2093 filt_hammer2read(struct knote *kn, long hint)
2095 struct vnode *vp = (void *)kn->kn_hook;
2096 hammer2_inode_t *ip = VTOI(vp);
2099 if (hint == NOTE_REVOKE) {
2100 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2103 off = ip->meta.size - kn->kn_fp->f_offset;
2104 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2105 if (kn->kn_sfflags & NOTE_OLDAPI)
2107 return (kn->kn_data != 0);
2112 filt_hammer2write(struct knote *kn, long hint)
2114 if (hint == NOTE_REVOKE)
2115 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2121 filt_hammer2vnode(struct knote *kn, long hint)
2123 if (kn->kn_sfflags & hint)
2124 kn->kn_fflags |= hint;
2125 if (hint == NOTE_REVOKE) {
2126 kn->kn_flags |= (EV_EOF | EV_NODATA);
2129 return (kn->kn_fflags != 0);
2137 hammer2_vop_markatime(struct vop_markatime_args *ap)
2139 hammer2_inode_t *ip;
2152 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2156 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2158 error = hammer2_vop_kqfilter(ap);
2165 struct vop_ops hammer2_vnode_vops = {
2166 .vop_default = vop_defaultop,
2167 .vop_fsync = hammer2_vop_fsync,
2168 .vop_getpages = vop_stdgetpages,
2169 .vop_putpages = vop_stdputpages,
2170 .vop_access = hammer2_vop_access,
2171 .vop_advlock = hammer2_vop_advlock,
2172 .vop_close = hammer2_vop_close,
2173 .vop_nlink = hammer2_vop_nlink,
2174 .vop_ncreate = hammer2_vop_ncreate,
2175 .vop_nsymlink = hammer2_vop_nsymlink,
2176 .vop_nremove = hammer2_vop_nremove,
2177 .vop_nrmdir = hammer2_vop_nrmdir,
2178 .vop_nrename = hammer2_vop_nrename,
2179 .vop_getattr = hammer2_vop_getattr,
2180 .vop_setattr = hammer2_vop_setattr,
2181 .vop_readdir = hammer2_vop_readdir,
2182 .vop_readlink = hammer2_vop_readlink,
2183 .vop_getpages = vop_stdgetpages,
2184 .vop_putpages = vop_stdputpages,
2185 .vop_read = hammer2_vop_read,
2186 .vop_write = hammer2_vop_write,
2187 .vop_open = hammer2_vop_open,
2188 .vop_inactive = hammer2_vop_inactive,
2189 .vop_reclaim = hammer2_vop_reclaim,
2190 .vop_nresolve = hammer2_vop_nresolve,
2191 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2192 .vop_nmkdir = hammer2_vop_nmkdir,
2193 .vop_nmknod = hammer2_vop_nmknod,
2194 .vop_ioctl = hammer2_vop_ioctl,
2195 .vop_mountctl = hammer2_vop_mountctl,
2196 .vop_bmap = hammer2_vop_bmap,
2197 .vop_strategy = hammer2_vop_strategy,
2198 .vop_kqfilter = hammer2_vop_kqfilter
2201 struct vop_ops hammer2_spec_vops = {
2202 .vop_default = vop_defaultop,
2203 .vop_fsync = hammer2_vop_fsync,
2204 .vop_read = vop_stdnoread,
2205 .vop_write = vop_stdnowrite,
2206 .vop_access = hammer2_vop_access,
2207 .vop_close = hammer2_vop_close,
2208 .vop_markatime = hammer2_vop_markatime,
2209 .vop_getattr = hammer2_vop_getattr,
2210 .vop_inactive = hammer2_vop_inactive,
2211 .vop_reclaim = hammer2_vop_reclaim,
2212 .vop_setattr = hammer2_vop_setattr
2215 struct vop_ops hammer2_fifo_vops = {
2216 .vop_default = fifo_vnoperate,
2217 .vop_fsync = hammer2_vop_fsync,
2219 .vop_read = hammer2_vop_fiforead,
2220 .vop_write = hammer2_vop_fifowrite,
2222 .vop_access = hammer2_vop_access,
2224 .vop_close = hammer2_vop_fifoclose,
2226 .vop_markatime = hammer2_vop_markatime,
2227 .vop_getattr = hammer2_vop_getattr,
2228 .vop_inactive = hammer2_vop_inactive,
2229 .vop_reclaim = hammer2_vop_reclaim,
2230 .vop_setattr = hammer2_vop_setattr,
2231 .vop_kqfilter = hammer2_vop_fifokqfilter