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 * WARNING: nvtruncbuf() can only be safely called without the inode
108 * lock held due to the way our write thread works.
110 if (ip->flags & HAMMER2_INODE_ISUNLINKED) {
115 * Detect updates to the embedded data which may be
116 * synchronized by the strategy code. Simply mark the
117 * inode modified so it gets picked up by our normal flush.
119 nblksize = hammer2_calc_logical(ip, 0, &lbase, NULL);
120 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)
149 * The final close of a deleted file or directory marks it for
150 * destruction. The DELETED flag allows the flusher to shortcut
151 * any modified blocks still unflushed (that is, just ignore them).
153 * HAMMER2 usually does not try to optimize the freemap by returning
154 * deleted blocks to it as it does not usually know how many snapshots
155 * might be referencing portions of the file/dir.
161 * NOTE! We do not attempt to flush chains here, flushing is
162 * really fragile and could also deadlock.
167 * An unlinked inode may have been relinked to the ihidden directory.
168 * This occurs if the inode was unlinked while open. Reclamation of
169 * these inodes requires processing we cannot safely do here so add
170 * the inode to the unlinkq in that situation.
172 * A reclaim can occur at any time so we cannot safely start a
173 * transaction to handle reclamation of unlinked files. Instead,
174 * the ip is left with a reference and placed on a linked list and
177 if ((ip->flags & HAMMER2_INODE_ISUNLINKED) &&
178 (ip->flags & HAMMER2_INODE_ISDELETED) == 0) {
179 hammer2_inode_unlink_t *ipul;
181 ipul = kmalloc(sizeof(*ipul), pmp->minode, M_WAITOK | M_ZERO);
184 hammer2_spin_ex(&pmp->list_spin);
185 TAILQ_INSERT_TAIL(&pmp->unlinkq, ipul, entry);
186 hammer2_spin_unex(&pmp->list_spin);
187 /* retain ref from vp for ipul */
189 hammer2_inode_drop(ip); /* vp ref */
193 * XXX handle background sync when ip dirty, kernel will no longer
194 * notify us regarding this inode because there is no longer a
195 * vnode attached to it.
204 hammer2_vop_fsync(struct vop_fsync_args *ap)
214 /* XXX can't do this yet */
215 hammer2_trans_init(ip->pmp, HAMMER2_TRANS_ISFLUSH);
216 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
218 hammer2_trans_init(ip->pmp, 0);
219 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
222 * Calling chain_flush here creates a lot of duplicative
223 * COW operations due to non-optimal vnode ordering.
225 * Only do it for an actual fsync() syscall. The other forms
226 * which call this function will eventually call chain_flush
227 * on the volume root as a catch-all, which is far more optimal.
229 hammer2_inode_lock(ip, 0);
230 if (ip->flags & HAMMER2_INODE_MODIFIED)
231 hammer2_inode_fsync(ip);
232 hammer2_inode_unlock(ip);
233 hammer2_trans_done(ip->pmp);
241 hammer2_vop_access(struct vop_access_args *ap)
243 hammer2_inode_t *ip = VTOI(ap->a_vp);
249 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
250 uid = hammer2_to_unix_xid(&ip->meta.uid);
251 gid = hammer2_to_unix_xid(&ip->meta.gid);
252 error = vop_helper_access(ap, uid, gid, ip->meta.mode, ip->meta.uflags);
253 hammer2_inode_unlock(ip);
261 hammer2_vop_getattr(struct vop_getattr_args *ap)
267 hammer2_chain_t *chain;
277 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
279 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
280 vap->va_fileid = ip->meta.inum;
281 vap->va_mode = ip->meta.mode;
282 vap->va_nlink = ip->meta.nlinks;
283 vap->va_uid = hammer2_to_unix_xid(&ip->meta.uid);
284 vap->va_gid = hammer2_to_unix_xid(&ip->meta.gid);
287 vap->va_size = ip->meta.size; /* protected by shared lock */
288 vap->va_blocksize = HAMMER2_PBUFSIZE;
289 vap->va_flags = ip->meta.uflags;
290 hammer2_time_to_timespec(ip->meta.ctime, &vap->va_ctime);
291 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_mtime);
292 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_atime);
295 for (i = 0; i < ip->cluster.nchains; ++i) {
296 if ((chain = ip->cluster.array[i].chain) != NULL) {
297 if (vap->va_bytes < chain->bref.data_count)
298 vap->va_bytes = chain->bref.data_count;
301 vap->va_type = hammer2_get_vtype(ip->meta.type);
303 vap->va_uid_uuid = ip->meta.uid;
304 vap->va_gid_uuid = ip->meta.gid;
305 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
308 hammer2_inode_unlock(ip);
316 hammer2_vop_setattr(struct vop_setattr_args *ap)
328 hammer2_update_time(&ctime);
332 if (ip->pmp->ronly) {
337 hammer2_pfs_memory_wait(ip->pmp);
338 hammer2_trans_init(ip->pmp, 0);
339 hammer2_inode_lock(ip, 0);
342 if (vap->va_flags != VNOVAL) {
345 flags = ip->meta.uflags;
346 error = vop_helper_setattr_flags(&flags, vap->va_flags,
347 hammer2_to_unix_xid(&ip->meta.uid),
350 if (ip->meta.uflags != flags) {
351 hammer2_inode_modify(ip);
352 ip->meta.uflags = flags;
353 ip->meta.ctime = ctime;
354 kflags |= NOTE_ATTRIB;
356 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
363 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
367 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
368 mode_t cur_mode = ip->meta.mode;
369 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
370 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
374 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
376 &cur_uid, &cur_gid, &cur_mode);
378 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
379 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
380 if (bcmp(&uuid_uid, &ip->meta.uid, sizeof(uuid_uid)) ||
381 bcmp(&uuid_gid, &ip->meta.gid, sizeof(uuid_gid)) ||
382 ip->meta.mode != cur_mode
384 hammer2_inode_modify(ip);
385 ip->meta.uid = uuid_uid;
386 ip->meta.gid = uuid_gid;
387 ip->meta.mode = cur_mode;
388 ip->meta.ctime = ctime;
390 kflags |= NOTE_ATTRIB;
397 if (vap->va_size != VNOVAL && ip->meta.size != vap->va_size) {
400 if (vap->va_size == ip->meta.size)
402 if (vap->va_size < ip->meta.size) {
403 hammer2_truncate_file(ip, vap->va_size);
405 hammer2_extend_file(ip, vap->va_size);
407 hammer2_inode_modify(ip);
408 ip->meta.mtime = ctime;
416 /* atime not supported */
417 if (vap->va_atime.tv_sec != VNOVAL) {
418 hammer2_inode_modify(ip);
419 ip->meta.atime = hammer2_timespec_to_time(&vap->va_atime);
420 kflags |= NOTE_ATTRIB;
423 if (vap->va_mode != (mode_t)VNOVAL) {
424 mode_t cur_mode = ip->meta.mode;
425 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
426 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
428 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
429 cur_uid, cur_gid, &cur_mode);
430 if (error == 0 && ip->meta.mode != cur_mode) {
431 hammer2_inode_modify(ip);
432 ip->meta.mode = cur_mode;
433 ip->meta.ctime = ctime;
434 kflags |= NOTE_ATTRIB;
438 if (vap->va_mtime.tv_sec != VNOVAL) {
439 hammer2_inode_modify(ip);
440 ip->meta.mtime = hammer2_timespec_to_time(&vap->va_mtime);
441 kflags |= NOTE_ATTRIB;
446 * If a truncation occurred we must call inode_fsync() now in order
447 * to trim the related data chains, otherwise a later expansion can
450 * If an extend occured that changed the DIRECTDATA state, we must
451 * call inode_fsync now in order to prepare the inode's indirect
454 if (ip->flags & HAMMER2_INODE_RESIZED)
455 hammer2_inode_fsync(ip);
460 hammer2_inode_unlock(ip);
461 hammer2_trans_done(ip->pmp);
462 hammer2_knote(ip->vp, kflags);
470 hammer2_vop_readdir(struct vop_readdir_args *ap)
472 hammer2_xop_readdir_t *xop;
473 hammer2_blockref_t bref;
490 saveoff = uio->uio_offset;
495 * Setup cookies directory entry cookies if requested
497 if (ap->a_ncookies) {
498 ncookies = uio->uio_resid / 16 + 1;
501 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
508 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
511 * Handle artificial entries. To ensure that only positive 64 bit
512 * quantities are returned to userland we always strip off bit 63.
513 * The hash code is designed such that codes 0x0000-0x7FFF are not
514 * used, allowing us to use these codes for articial entries.
516 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
517 * allow '..' to cross the mount point into (e.g.) the super-root.
520 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
521 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
525 cookies[cookie_index] = saveoff;
528 if (cookie_index == ncookies)
534 * Be careful with lockorder when accessing ".."
536 * (ip is the current dir. xip is the parent dir).
538 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
539 if (ip->pip && ip != ip->pmp->iroot)
540 inum = ip->pip->meta.inum & HAMMER2_DIRHASH_USERMSK;
541 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
545 cookies[cookie_index] = saveoff;
548 if (cookie_index == ncookies)
552 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
553 if (hammer2_debug & 0x0020)
554 kprintf("readdir: lkey %016jx\n", lkey);
559 * Use XOP for cluster scan.
561 * parent is the inode cluster, already locked for us. Don't
562 * double lock shared locks as this will screw up upgrades.
564 xop = hammer2_xop_alloc(ip, 0);
566 hammer2_xop_start(&xop->head, hammer2_xop_readdir);
569 const hammer2_inode_data_t *ripdata;
571 error = hammer2_xop_collect(&xop->head, 0);
574 if (cookie_index == ncookies)
576 if (hammer2_debug & 0x0020)
577 kprintf("cluster chain %p %p\n",
578 xop->head.cluster.focus,
579 (xop->head.cluster.focus ?
580 xop->head.cluster.focus->data : (void *)-1));
581 ripdata = &hammer2_cluster_rdata(&xop->head.cluster)->ipdata;
582 hammer2_cluster_bref(&xop->head.cluster, &bref);
583 if (bref.type == HAMMER2_BREF_TYPE_INODE) {
584 dtype = hammer2_get_dtype(ripdata);
585 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
586 r = vop_write_dirent(&error, uio,
588 HAMMER2_DIRHASH_USERMSK,
590 ripdata->meta.name_len,
595 cookies[cookie_index] = saveoff;
598 /* XXX chain error */
599 kprintf("bad chain type readdir %d\n", bref.type);
602 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
603 if (error == ENOENT) {
606 saveoff = (hammer2_key_t)-1;
608 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
611 hammer2_inode_unlock(ip);
613 *ap->a_eofflag = eofflag;
614 if (hammer2_debug & 0x0020)
615 kprintf("readdir: done at %016jx\n", saveoff);
616 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
617 if (error && cookie_index == 0) {
619 kfree(cookies, M_TEMP);
621 *ap->a_cookies = NULL;
625 *ap->a_ncookies = cookie_index;
626 *ap->a_cookies = cookies;
634 * hammer2_vop_readlink { vp, uio, cred }
638 hammer2_vop_readlink(struct vop_readlink_args *ap)
645 if (vp->v_type != VLNK)
649 error = hammer2_read_file(ip, ap->a_uio, 0);
655 hammer2_vop_read(struct vop_read_args *ap)
665 * Read operations supported on this vnode?
668 if (vp->v_type != VREG)
678 seqcount = ap->a_ioflag >> 16;
679 bigread = (uio->uio_resid > 100 * 1024 * 1024);
681 error = hammer2_read_file(ip, uio, seqcount);
687 hammer2_vop_write(struct vop_write_args *ap)
697 * Read operations supported on this vnode?
700 if (vp->v_type != VREG)
709 if (ip->pmp->ronly) {
713 seqcount = ap->a_ioflag >> 16;
716 * Check resource limit
718 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
719 uio->uio_offset + uio->uio_resid >
720 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
721 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
726 * The transaction interlocks against flushes initiations
727 * (note: but will run concurrently with the actual flush).
729 hammer2_trans_init(ip->pmp, 0);
730 error = hammer2_write_file(ip, uio, ap->a_ioflag, seqcount);
731 hammer2_trans_done(ip->pmp);
737 * Perform read operations on a file or symlink given an UNLOCKED
740 * The passed ip is not locked.
744 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
755 * WARNING! Assumes that the kernel interlocks size changes at the
758 hammer2_mtx_sh(&ip->lock);
759 size = ip->meta.size;
760 hammer2_mtx_unlock(&ip->lock);
762 while (uio->uio_resid > 0 && uio->uio_offset < size) {
769 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
772 error = cluster_read(ip->vp, leof, lbase, lblksize,
773 uio->uio_resid, seqcount * BKVASIZE,
778 loff = (int)(uio->uio_offset - lbase);
780 if (n > uio->uio_resid)
782 if (n > size - uio->uio_offset)
783 n = (int)(size - uio->uio_offset);
784 bp->b_flags |= B_AGE;
785 uiomove((char *)bp->b_data + loff, n, uio);
792 * Write to the file represented by the inode via the logical buffer cache.
793 * The inode may represent a regular file or a symlink.
795 * The inode must not be locked.
799 hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
800 int ioflag, int seqcount)
802 hammer2_key_t old_eof;
803 hammer2_key_t new_eof;
812 * WARNING! Assumes that the kernel interlocks size changes at the
815 hammer2_mtx_ex(&ip->lock);
816 if (ioflag & IO_APPEND)
817 uio->uio_offset = ip->meta.size;
818 old_eof = ip->meta.size;
821 * Extend the file if necessary. If the write fails at some point
822 * we will truncate it back down to cover as much as we were able
825 * Doing this now makes it easier to calculate buffer sizes in
832 if (uio->uio_offset + uio->uio_resid > old_eof) {
833 new_eof = uio->uio_offset + uio->uio_resid;
835 hammer2_extend_file(ip, new_eof);
836 kflags |= NOTE_EXTEND;
840 hammer2_mtx_unlock(&ip->lock);
845 while (uio->uio_resid > 0) {
854 * Don't allow the buffer build to blow out the buffer
857 if ((ioflag & IO_RECURSE) == 0)
858 bwillwrite(HAMMER2_PBUFSIZE);
861 * This nominally tells us how much we can cluster and
862 * what the logical buffer size needs to be. Currently
863 * we don't try to cluster the write and just handle one
866 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
868 loff = (int)(uio->uio_offset - lbase);
870 KKASSERT(lblksize <= 65536);
873 * Calculate bytes to copy this transfer and whether the
874 * copy completely covers the buffer or not.
878 if (n > uio->uio_resid) {
880 if (loff == lbase && uio->uio_offset + n == new_eof)
892 if (uio->uio_segflg == UIO_NOCOPY) {
894 * Issuing a write with the same data backing the
895 * buffer. Instantiate the buffer to collect the
896 * backing vm pages, then read-in any missing bits.
898 * This case is used by vop_stdputpages().
900 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
901 if ((bp->b_flags & B_CACHE) == 0) {
903 error = bread(ip->vp, lbase, lblksize, &bp);
905 } else if (trivial) {
907 * Even though we are entirely overwriting the buffer
908 * we may still have to zero it out to avoid a
909 * mmap/write visibility issue.
911 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
912 if ((bp->b_flags & B_CACHE) == 0)
916 * Partial overwrite, read in any missing bits then
917 * replace the portion being written.
919 * (The strategy code will detect zero-fill physical
920 * blocks for this case).
922 error = bread(ip->vp, lbase, lblksize, &bp);
933 * Ok, copy the data in
935 error = uiomove(bp->b_data + loff, n, uio);
936 kflags |= NOTE_WRITE;
944 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
945 * with IO_SYNC or IO_ASYNC set. These writes
946 * must be handled as the pageout daemon expects.
948 if (ioflag & IO_SYNC) {
950 } else if ((ioflag & IO_DIRECT) && endofblk) {
952 } else if (ioflag & IO_ASYNC) {
960 * Cleanup. If we extended the file EOF but failed to write through
961 * the entire write is a failure and we have to back-up.
963 if (error && new_eof != old_eof) {
964 hammer2_mtx_ex(&ip->lock);
965 hammer2_truncate_file(ip, old_eof);
966 if (ip->flags & HAMMER2_INODE_MODIFIED)
967 hammer2_inode_fsync(ip);
968 hammer2_mtx_unlock(&ip->lock);
969 } else if (modified) {
970 hammer2_mtx_ex(&ip->lock);
971 hammer2_inode_modify(ip);
972 hammer2_update_time(&ip->meta.mtime);
973 if (ip->flags & HAMMER2_INODE_MODIFIED)
974 hammer2_inode_fsync(ip);
975 hammer2_mtx_unlock(&ip->lock);
976 hammer2_knote(ip->vp, kflags);
978 hammer2_trans_assert_strategy(ip->pmp);
984 * Truncate the size of a file. The inode must not be locked.
986 * We must unconditionally set HAMMER2_INODE_RESIZED to properly
987 * ensure that any on-media data beyond the new file EOF has been destroyed.
989 * WARNING: nvtruncbuf() can only be safely called without the inode lock
990 * held due to the way our write thread works. If the truncation
991 * occurs in the middle of a buffer, nvtruncbuf() is responsible
992 * for dirtying that buffer and zeroing out trailing bytes.
994 * WARNING! Assumes that the kernel interlocks size changes at the
997 * WARNING! Caller assumes responsibility for removing dead blocks
998 * if INODE_RESIZED is set.
1002 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1004 hammer2_key_t lbase;
1008 hammer2_mtx_unlock(&ip->lock);
1010 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1011 nvtruncbuf(ip->vp, nsize,
1012 nblksize, (int)nsize & (nblksize - 1),
1015 hammer2_mtx_ex(&ip->lock);
1016 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1017 ip->osize = ip->meta.size;
1018 ip->meta.size = nsize;
1019 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED |
1020 HAMMER2_INODE_RESIZED);
1025 * Extend the size of a file. The inode must not be locked.
1027 * Even though the file size is changing, we do not have to set the
1028 * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES
1029 * boundary. When this occurs a hammer2_inode_fsync() is required
1030 * to prepare the inode cluster's indirect block table.
1032 * WARNING! Assumes that the kernel interlocks size changes at the
1035 * WARNING! Caller assumes responsibility for transitioning out
1036 * of the inode DIRECTDATA mode if INODE_RESIZED is set.
1040 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1042 hammer2_key_t lbase;
1043 hammer2_key_t osize;
1049 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1050 osize = ip->meta.size;
1052 ip->meta.size = nsize;
1053 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1055 if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES)
1056 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1058 hammer2_mtx_unlock(&ip->lock);
1060 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1061 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1067 hammer2_mtx_ex(&ip->lock);
1074 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1076 hammer2_xop_nresolve_t *xop;
1077 hammer2_inode_t *ip;
1078 hammer2_inode_t *dip;
1079 struct namecache *ncp;
1084 dip = VTOI(ap->a_dvp);
1085 xop = hammer2_xop_alloc(dip, 0);
1087 ncp = ap->a_nch->ncp;
1088 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1091 * Note: In DragonFly the kernel handles '.' and '..'.
1093 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1094 hammer2_xop_start(&xop->head, hammer2_xop_nresolve);
1096 error = hammer2_xop_collect(&xop->head, 0);
1100 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1102 hammer2_inode_unlock(dip);
1105 * Acquire the related vnode
1107 * NOTE: For error processing, only ENOENT resolves the namecache
1108 * entry to NULL, otherwise we just return the error and
1109 * leave the namecache unresolved.
1111 * NOTE: multiple hammer2_inode structures can be aliased to the
1112 * same chain element, for example for hardlinks. This
1113 * use case does not 'reattach' inode associations that
1114 * might already exist, but always allocates a new one.
1116 * WARNING: inode structure is locked exclusively via inode_get
1117 * but chain was locked shared. inode_unlock()
1118 * will handle it properly.
1121 vp = hammer2_igetv(ip, &error);
1124 cache_setvp(ap->a_nch, vp);
1125 } else if (error == ENOENT) {
1126 cache_setvp(ap->a_nch, NULL);
1128 hammer2_inode_unlock(ip);
1131 * The vp should not be released until after we've disposed
1132 * of our locks, because it might cause vop_inactive() to
1139 cache_setvp(ap->a_nch, NULL);
1141 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1142 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1143 ("resolve error %d/%p ap %p\n",
1144 error, ap->a_nch->ncp->nc_vp, ap));
1152 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1154 hammer2_inode_t *dip;
1155 hammer2_inode_t *ip;
1159 dip = VTOI(ap->a_dvp);
1161 if ((ip = dip->pip) == NULL) {
1166 hammer2_inode_lock(ip, 0);
1167 *ap->a_vpp = hammer2_igetv(ip, &error);
1168 hammer2_inode_unlock(ip);
1176 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1178 hammer2_inode_t *dip;
1179 hammer2_inode_t *nip;
1180 struct namecache *ncp;
1181 const uint8_t *name;
1186 dip = VTOI(ap->a_dvp);
1187 if (dip->pmp->ronly) {
1192 ncp = ap->a_nch->ncp;
1193 name = ncp->nc_name;
1194 name_len = ncp->nc_nlen;
1196 hammer2_pfs_memory_wait(dip->pmp);
1197 hammer2_trans_init(dip->pmp, 0);
1198 nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1200 hammer2_trans_newinum(dip->pmp), 0, 0,
1203 KKASSERT(nip == NULL);
1206 *ap->a_vpp = hammer2_igetv(nip, &error);
1207 hammer2_inode_unlock(nip);
1209 hammer2_trans_done(dip->pmp);
1212 cache_setunresolved(ap->a_nch);
1213 cache_setvp(ap->a_nch, *ap->a_vpp);
1221 hammer2_vop_open(struct vop_open_args *ap)
1223 return vop_stdopen(ap);
1227 * hammer2_vop_advlock { vp, id, op, fl, flags }
1231 hammer2_vop_advlock(struct vop_advlock_args *ap)
1233 hammer2_inode_t *ip = VTOI(ap->a_vp);
1236 size = ip->meta.size;
1237 return (lf_advlock(ap, &ip->advlock, size));
1242 hammer2_vop_close(struct vop_close_args *ap)
1244 return vop_stdclose(ap);
1248 * hammer2_vop_nlink { nch, dvp, vp, cred }
1250 * Create a hardlink from (vp) to {dvp, nch}.
1254 hammer2_vop_nlink(struct vop_nlink_args *ap)
1256 hammer2_xop_nlink_t *xop1;
1257 hammer2_inode_t *fdip; /* target directory to create link in */
1258 hammer2_inode_t *tdip; /* target directory to create link in */
1259 hammer2_inode_t *cdip; /* common parent directory */
1260 hammer2_inode_t *ip; /* inode we are hardlinking to */
1261 struct namecache *ncp;
1262 const uint8_t *name;
1267 tdip = VTOI(ap->a_dvp);
1268 if (tdip->pmp->ronly) {
1273 ncp = ap->a_nch->ncp;
1274 name = ncp->nc_name;
1275 name_len = ncp->nc_nlen;
1278 * ip represents the file being hardlinked. The file could be a
1279 * normal file or a hardlink target if it has already been hardlinked.
1280 * If ip is a hardlinked target then ip->pip represents the location
1281 * of the hardlinked target, NOT the location of the hardlink pointer.
1283 * Bump nlinks and potentially also create or move the hardlink
1284 * target in the parent directory common to (ip) and (tdip). The
1285 * consolidation code can modify ip->cluster and ip->pip. The
1286 * returned cluster is locked.
1288 ip = VTOI(ap->a_vp);
1289 hammer2_pfs_memory_wait(ip->pmp);
1290 hammer2_trans_init(ip->pmp, 0);
1293 * The common parent directory must be locked first to avoid deadlocks.
1294 * Also note that fdip and/or tdip might match cdip.
1297 cdip = hammer2_inode_common_parent(fdip, tdip);
1298 hammer2_inode_lock(cdip, 0);
1299 hammer2_inode_lock(fdip, 0);
1300 hammer2_inode_lock(tdip, 0);
1301 hammer2_inode_lock(ip, 0);
1305 * If ip is not a hardlink target we must convert it to a hardlink.
1306 * If fdip != cdip we must shift the inode to cdip.
1308 if (fdip != cdip || (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1309 xop1 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
1310 hammer2_xop_setip2(&xop1->head, ip);
1311 hammer2_xop_setip3(&xop1->head, cdip);
1313 hammer2_xop_start(&xop1->head, hammer2_xop_nlink);
1314 error = hammer2_xop_collect(&xop1->head, 0);
1315 hammer2_xop_retire(&xop1->head, HAMMER2_XOPMASK_VOP);
1316 if (error == ENOENT)
1321 * Must synchronize original inode whos chains are now a hardlink
1322 * target. We must match what the backend XOP did to the
1325 if (error == 0 && (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1326 hammer2_inode_modify(ip);
1327 ip->meta.name_key = ip->meta.inum;
1328 ip->meta.name_len = 18; /* "0x%016jx" */
1332 * Create the hardlink target and bump nlinks.
1335 hammer2_inode_create(tdip, NULL, NULL,
1338 HAMMER2_OBJTYPE_HARDLINK, ip->meta.type,
1340 hammer2_inode_modify(ip);
1344 cache_setunresolved(ap->a_nch);
1345 cache_setvp(ap->a_nch, ap->a_vp);
1347 hammer2_inode_unlock(ip);
1348 hammer2_inode_unlock(tdip);
1349 hammer2_inode_unlock(fdip);
1350 hammer2_inode_unlock(cdip);
1351 hammer2_inode_drop(cdip);
1352 hammer2_trans_done(ip->pmp);
1359 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1361 * The operating system has already ensured that the directory entry
1362 * does not exist and done all appropriate namespace locking.
1366 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1368 hammer2_inode_t *dip;
1369 hammer2_inode_t *nip;
1370 struct namecache *ncp;
1371 const uint8_t *name;
1376 dip = VTOI(ap->a_dvp);
1377 if (dip->pmp->ronly) {
1382 ncp = ap->a_nch->ncp;
1383 name = ncp->nc_name;
1384 name_len = ncp->nc_nlen;
1385 hammer2_pfs_memory_wait(dip->pmp);
1386 hammer2_trans_init(dip->pmp, 0);
1388 nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1390 hammer2_trans_newinum(dip->pmp), 0, 0,
1393 KKASSERT(nip == NULL);
1396 *ap->a_vpp = hammer2_igetv(nip, &error);
1397 hammer2_inode_unlock(nip);
1399 hammer2_trans_done(dip->pmp);
1402 cache_setunresolved(ap->a_nch);
1403 cache_setvp(ap->a_nch, *ap->a_vpp);
1410 * Make a device node (typically a fifo)
1414 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1416 hammer2_inode_t *dip;
1417 hammer2_inode_t *nip;
1418 struct namecache *ncp;
1419 const uint8_t *name;
1424 dip = VTOI(ap->a_dvp);
1425 if (dip->pmp->ronly) {
1430 ncp = ap->a_nch->ncp;
1431 name = ncp->nc_name;
1432 name_len = ncp->nc_nlen;
1433 hammer2_pfs_memory_wait(dip->pmp);
1434 hammer2_trans_init(dip->pmp, 0);
1436 nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1438 hammer2_trans_newinum(dip->pmp), 0, 0,
1441 KKASSERT(nip == NULL);
1444 *ap->a_vpp = hammer2_igetv(nip, &error);
1445 hammer2_inode_unlock(nip);
1447 hammer2_trans_done(dip->pmp);
1450 cache_setunresolved(ap->a_nch);
1451 cache_setvp(ap->a_nch, *ap->a_vpp);
1458 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1462 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1464 hammer2_inode_t *dip;
1465 hammer2_inode_t *nip;
1466 struct namecache *ncp;
1467 const uint8_t *name;
1471 dip = VTOI(ap->a_dvp);
1472 if (dip->pmp->ronly)
1475 ncp = ap->a_nch->ncp;
1476 name = ncp->nc_name;
1477 name_len = ncp->nc_nlen;
1478 hammer2_pfs_memory_wait(dip->pmp);
1479 hammer2_trans_init(dip->pmp, 0);
1481 ap->a_vap->va_type = VLNK; /* enforce type */
1483 nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1485 hammer2_trans_newinum(dip->pmp), 0, 0,
1488 KKASSERT(nip == NULL);
1490 hammer2_trans_done(dip->pmp);
1493 *ap->a_vpp = hammer2_igetv(nip, &error);
1496 * Build the softlink (~like file data) and finalize the namecache.
1503 bytes = strlen(ap->a_target);
1505 hammer2_inode_unlock(nip);
1506 bzero(&auio, sizeof(auio));
1507 bzero(&aiov, sizeof(aiov));
1508 auio.uio_iov = &aiov;
1509 auio.uio_segflg = UIO_SYSSPACE;
1510 auio.uio_rw = UIO_WRITE;
1511 auio.uio_resid = bytes;
1512 auio.uio_iovcnt = 1;
1513 auio.uio_td = curthread;
1514 aiov.iov_base = ap->a_target;
1515 aiov.iov_len = bytes;
1516 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1517 /* XXX handle error */
1520 hammer2_inode_unlock(nip);
1522 hammer2_trans_done(dip->pmp);
1525 * Finalize namecache
1528 cache_setunresolved(ap->a_nch);
1529 cache_setvp(ap->a_nch, *ap->a_vpp);
1530 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1536 * hammer2_vop_nremove { nch, dvp, cred }
1540 hammer2_vop_nremove(struct vop_nremove_args *ap)
1542 hammer2_xop_unlink_t *xop;
1543 hammer2_inode_t *dip;
1544 hammer2_inode_t *ip;
1545 struct namecache *ncp;
1550 dip = VTOI(ap->a_dvp);
1551 if (dip->pmp->ronly) {
1556 ncp = ap->a_nch->ncp;
1558 hammer2_pfs_memory_wait(dip->pmp);
1559 hammer2_trans_init(dip->pmp, 0);
1560 hammer2_inode_lock(dip, 0);
1563 * The unlink XOP unlinks the path from the directory and
1564 * locates and returns the cluster associated with the real inode.
1565 * We have to handle nlinks here on the frontend.
1567 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1568 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1569 isopen = cache_isopen(ap->a_nch);
1571 xop->dopermanent = isopen ? 0 : HAMMER2_DELETE_PERMANENT;
1572 hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1575 * Collect the real inode and adjust nlinks, destroy the real
1576 * inode if nlinks transitions to 0 and it was the real inode
1577 * (else it has already been removed).
1579 error = hammer2_xop_collect(&xop->head, 0);
1580 hammer2_inode_unlock(dip);
1583 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1584 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1586 hammer2_inode_unlink_finisher(ip, isopen);
1587 hammer2_inode_unlock(ip);
1590 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1593 hammer2_inode_run_unlinkq(dip->pmp);
1594 hammer2_trans_done(dip->pmp);
1596 cache_unlink(ap->a_nch);
1602 * hammer2_vop_nrmdir { nch, dvp, cred }
1606 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1608 hammer2_xop_unlink_t *xop;
1609 hammer2_inode_t *dip;
1610 hammer2_inode_t *ip;
1611 struct namecache *ncp;
1616 dip = VTOI(ap->a_dvp);
1617 if (dip->pmp->ronly) {
1622 hammer2_pfs_memory_wait(dip->pmp);
1623 hammer2_trans_init(dip->pmp, 0);
1624 hammer2_inode_lock(dip, 0);
1626 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1628 ncp = ap->a_nch->ncp;
1629 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1630 isopen = cache_isopen(ap->a_nch);
1632 xop->dopermanent = isopen ? 0 : HAMMER2_DELETE_PERMANENT;
1633 hammer2_xop_start(&xop->head, hammer2_xop_unlink);
1636 * Collect the real inode and adjust nlinks, destroy the real
1637 * inode if nlinks transitions to 0 and it was the real inode
1638 * (else it has already been removed).
1640 error = hammer2_xop_collect(&xop->head, 0);
1641 hammer2_inode_unlock(dip);
1644 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster, -1);
1645 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1647 hammer2_inode_unlink_finisher(ip, isopen);
1648 hammer2_inode_unlock(ip);
1651 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1653 hammer2_inode_run_unlinkq(dip->pmp);
1654 hammer2_trans_done(dip->pmp);
1656 cache_unlink(ap->a_nch);
1662 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1666 hammer2_vop_nrename(struct vop_nrename_args *ap)
1668 struct namecache *fncp;
1669 struct namecache *tncp;
1670 hammer2_inode_t *cdip;
1671 hammer2_inode_t *fdip;
1672 hammer2_inode_t *tdip;
1673 hammer2_inode_t *ip;
1674 const uint8_t *fname;
1676 const uint8_t *tname;
1682 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1684 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1687 fdip = VTOI(ap->a_fdvp); /* source directory */
1688 tdip = VTOI(ap->a_tdvp); /* target directory */
1690 if (fdip->pmp->ronly)
1694 fncp = ap->a_fnch->ncp; /* entry name in source */
1695 fname = fncp->nc_name;
1696 fname_len = fncp->nc_nlen;
1698 tncp = ap->a_tnch->ncp; /* entry name in target */
1699 tname = tncp->nc_name;
1700 tname_len = tncp->nc_nlen;
1702 hammer2_pfs_memory_wait(tdip->pmp);
1703 hammer2_trans_init(tdip->pmp, 0);
1706 * ip is the inode being renamed. If this is a hardlink then
1707 * ip represents the actual file and not the hardlink marker.
1709 ip = VTOI(fncp->nc_vp);
1712 * The common parent directory must be locked first to avoid deadlocks.
1713 * Also note that fdip and/or tdip might match cdip.
1715 cdip = hammer2_inode_common_parent(ip->pip, tdip);
1716 hammer2_inode_lock(cdip, 0);
1717 hammer2_inode_lock(fdip, 0);
1718 hammer2_inode_lock(tdip, 0);
1719 hammer2_inode_ref(ip); /* extra ref */
1723 * If ip is a hardlink target and fdip != cdip we must shift the
1727 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
1728 hammer2_xop_nlink_t *xop1;
1730 xop1 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
1731 hammer2_xop_setip2(&xop1->head, ip);
1732 hammer2_xop_setip3(&xop1->head, cdip);
1734 hammer2_xop_start(&xop1->head, hammer2_xop_nlink);
1735 error = hammer2_xop_collect(&xop1->head, 0);
1736 hammer2_xop_retire(&xop1->head, HAMMER2_XOPMASK_VOP);
1740 * Delete the target namespace.
1743 hammer2_xop_unlink_t *xop2;
1744 hammer2_inode_t *tip;
1748 * The unlink XOP unlinks the path from the directory and
1749 * locates and returns the cluster associated with the real
1750 * inode. We have to handle nlinks here on the frontend.
1752 xop2 = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
1753 hammer2_xop_setname(&xop2->head, tname, tname_len);
1754 isopen = cache_isopen(ap->a_tnch);
1756 xop2->dopermanent = isopen ? 0 : HAMMER2_DELETE_PERMANENT;
1757 hammer2_xop_start(&xop2->head, hammer2_xop_unlink);
1760 * Collect the real inode and adjust nlinks, destroy the real
1761 * inode if nlinks transitions to 0 and it was the real inode
1762 * (else it has already been removed).
1764 tnch_error = hammer2_xop_collect(&xop2->head, 0);
1765 /* hammer2_inode_unlock(tdip); */
1767 if (tnch_error == 0) {
1768 tip = hammer2_inode_get(tdip->pmp, NULL,
1769 &xop2->head.cluster, -1);
1770 hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
1772 hammer2_inode_unlink_finisher(tip, isopen);
1773 hammer2_inode_unlock(tip);
1776 hammer2_xop_retire(&xop2->head, HAMMER2_XOPMASK_VOP);
1778 /* hammer2_inode_lock(tdip, 0); */
1780 if (tnch_error && tnch_error != ENOENT) {
1787 * Resolve the collision space for (tdip, tname, tname_len)
1789 * tdip must be held exclusively locked to prevent races.
1792 hammer2_xop_scanlhc_t *sxop;
1793 hammer2_tid_t lhcbase;
1795 tlhc = hammer2_dirhash(tname, tname_len);
1797 sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
1799 hammer2_xop_start(&sxop->head, hammer2_xop_scanlhc);
1800 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
1801 if (tlhc != sxop->head.cluster.focus->bref.key)
1805 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
1808 if (error != ENOENT)
1813 if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) {
1820 * Everything is setup, do the rename.
1822 * We have to synchronize ip->meta to the underlying operation.
1824 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
1825 * unlinking elements from their directories. Locking
1826 * the nlinks field does not lock the whole inode.
1828 hammer2_inode_lock(ip, 0);
1830 hammer2_xop_nrename_t *xop4;
1832 xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
1834 xop4->ip_key = ip->meta.name_key;
1835 hammer2_xop_setip2(&xop4->head, ip);
1836 hammer2_xop_setip3(&xop4->head, tdip);
1837 hammer2_xop_setname(&xop4->head, fname, fname_len);
1838 hammer2_xop_setname2(&xop4->head, tname, tname_len);
1839 hammer2_xop_start(&xop4->head, hammer2_xop_nrename);
1841 error = hammer2_xop_collect(&xop4->head, 0);
1842 hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP);
1844 if (error == ENOENT)
1847 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1848 hammer2_inode_modify(ip);
1849 ip->meta.name_len = tname_len;
1850 ip->meta.name_key = tlhc;
1856 * Fixup ip->pip if we were renaming the actual file and not a
1859 if (error == 0 && (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
1860 hammer2_inode_t *opip;
1862 if (ip->pip != tdip) {
1863 hammer2_inode_ref(tdip);
1867 hammer2_inode_drop(opip);
1870 hammer2_inode_unlock(ip);
1872 hammer2_inode_unlock(tdip);
1873 hammer2_inode_unlock(fdip);
1874 hammer2_inode_unlock(cdip);
1875 hammer2_inode_drop(ip);
1876 hammer2_inode_drop(cdip);
1877 hammer2_inode_run_unlinkq(fdip->pmp);
1878 hammer2_trans_done(tdip->pmp);
1881 * Issue the namecache update after unlocking all the internal
1882 * hammer structures, otherwise we might deadlock.
1884 if (tnch_error == 0) {
1885 cache_unlink(ap->a_tnch);
1886 cache_setunresolved(ap->a_tnch);
1889 cache_rename(ap->a_fnch, ap->a_tnch);
1896 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
1900 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
1902 hammer2_inode_t *ip;
1906 ip = VTOI(ap->a_vp);
1908 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
1909 ap->a_fflag, ap->a_cred);
1916 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
1924 case (MOUNTCTL_SET_EXPORT):
1925 mp = ap->a_head.a_ops->head.vv_mount;
1928 if (ap->a_ctllen != sizeof(struct export_args))
1931 rc = vfs_export(mp, &pmp->export,
1932 (const struct export_args *)ap->a_ctl);
1935 rc = vop_stdmountctl(ap);
1945 static void filt_hammer2detach(struct knote *kn);
1946 static int filt_hammer2read(struct knote *kn, long hint);
1947 static int filt_hammer2write(struct knote *kn, long hint);
1948 static int filt_hammer2vnode(struct knote *kn, long hint);
1950 static struct filterops hammer2read_filtops =
1951 { FILTEROP_ISFD | FILTEROP_MPSAFE,
1952 NULL, filt_hammer2detach, filt_hammer2read };
1953 static struct filterops hammer2write_filtops =
1954 { FILTEROP_ISFD | FILTEROP_MPSAFE,
1955 NULL, filt_hammer2detach, filt_hammer2write };
1956 static struct filterops hammer2vnode_filtops =
1957 { FILTEROP_ISFD | FILTEROP_MPSAFE,
1958 NULL, filt_hammer2detach, filt_hammer2vnode };
1962 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
1964 struct vnode *vp = ap->a_vp;
1965 struct knote *kn = ap->a_kn;
1967 switch (kn->kn_filter) {
1969 kn->kn_fop = &hammer2read_filtops;
1972 kn->kn_fop = &hammer2write_filtops;
1975 kn->kn_fop = &hammer2vnode_filtops;
1978 return (EOPNOTSUPP);
1981 kn->kn_hook = (caddr_t)vp;
1983 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1989 filt_hammer2detach(struct knote *kn)
1991 struct vnode *vp = (void *)kn->kn_hook;
1993 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1997 filt_hammer2read(struct knote *kn, long hint)
1999 struct vnode *vp = (void *)kn->kn_hook;
2000 hammer2_inode_t *ip = VTOI(vp);
2003 if (hint == NOTE_REVOKE) {
2004 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2007 off = ip->meta.size - kn->kn_fp->f_offset;
2008 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2009 if (kn->kn_sfflags & NOTE_OLDAPI)
2011 return (kn->kn_data != 0);
2016 filt_hammer2write(struct knote *kn, long hint)
2018 if (hint == NOTE_REVOKE)
2019 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2025 filt_hammer2vnode(struct knote *kn, long hint)
2027 if (kn->kn_sfflags & hint)
2028 kn->kn_fflags |= hint;
2029 if (hint == NOTE_REVOKE) {
2030 kn->kn_flags |= (EV_EOF | EV_NODATA);
2033 return (kn->kn_fflags != 0);
2041 hammer2_vop_markatime(struct vop_markatime_args *ap)
2043 hammer2_inode_t *ip;
2056 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2060 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2062 error = hammer2_vop_kqfilter(ap);
2069 struct vop_ops hammer2_vnode_vops = {
2070 .vop_default = vop_defaultop,
2071 .vop_fsync = hammer2_vop_fsync,
2072 .vop_getpages = vop_stdgetpages,
2073 .vop_putpages = vop_stdputpages,
2074 .vop_access = hammer2_vop_access,
2075 .vop_advlock = hammer2_vop_advlock,
2076 .vop_close = hammer2_vop_close,
2077 .vop_nlink = hammer2_vop_nlink,
2078 .vop_ncreate = hammer2_vop_ncreate,
2079 .vop_nsymlink = hammer2_vop_nsymlink,
2080 .vop_nremove = hammer2_vop_nremove,
2081 .vop_nrmdir = hammer2_vop_nrmdir,
2082 .vop_nrename = hammer2_vop_nrename,
2083 .vop_getattr = hammer2_vop_getattr,
2084 .vop_setattr = hammer2_vop_setattr,
2085 .vop_readdir = hammer2_vop_readdir,
2086 .vop_readlink = hammer2_vop_readlink,
2087 .vop_getpages = vop_stdgetpages,
2088 .vop_putpages = vop_stdputpages,
2089 .vop_read = hammer2_vop_read,
2090 .vop_write = hammer2_vop_write,
2091 .vop_open = hammer2_vop_open,
2092 .vop_inactive = hammer2_vop_inactive,
2093 .vop_reclaim = hammer2_vop_reclaim,
2094 .vop_nresolve = hammer2_vop_nresolve,
2095 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2096 .vop_nmkdir = hammer2_vop_nmkdir,
2097 .vop_nmknod = hammer2_vop_nmknod,
2098 .vop_ioctl = hammer2_vop_ioctl,
2099 .vop_mountctl = hammer2_vop_mountctl,
2100 .vop_bmap = hammer2_vop_bmap,
2101 .vop_strategy = hammer2_vop_strategy,
2102 .vop_kqfilter = hammer2_vop_kqfilter
2105 struct vop_ops hammer2_spec_vops = {
2106 .vop_default = vop_defaultop,
2107 .vop_fsync = hammer2_vop_fsync,
2108 .vop_read = vop_stdnoread,
2109 .vop_write = vop_stdnowrite,
2110 .vop_access = hammer2_vop_access,
2111 .vop_close = hammer2_vop_close,
2112 .vop_markatime = hammer2_vop_markatime,
2113 .vop_getattr = hammer2_vop_getattr,
2114 .vop_inactive = hammer2_vop_inactive,
2115 .vop_reclaim = hammer2_vop_reclaim,
2116 .vop_setattr = hammer2_vop_setattr
2119 struct vop_ops hammer2_fifo_vops = {
2120 .vop_default = fifo_vnoperate,
2121 .vop_fsync = hammer2_vop_fsync,
2123 .vop_read = hammer2_vop_fiforead,
2124 .vop_write = hammer2_vop_fifowrite,
2126 .vop_access = hammer2_vop_access,
2128 .vop_close = hammer2_vop_fifoclose,
2130 .vop_markatime = hammer2_vop_markatime,
2131 .vop_getattr = hammer2_vop_getattr,
2132 .vop_inactive = hammer2_vop_inactive,
2133 .vop_reclaim = hammer2_vop_reclaim,
2134 .vop_setattr = hammer2_vop_setattr,
2135 .vop_kqfilter = hammer2_vop_fifokqfilter