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_trans_t *trans, hammer2_inode_t *ip,
65 struct uio *uio, 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 * Once reclaimed the inode is disconnected from the normal flush
168 * mechanism and must be tracked
170 * A reclaim can occur at any time so we cannot safely start a
171 * transaction to handle reclamation of unlinked files. Instead,
172 * the ip is left with a reference and placed on a linked list and
175 if (ip->flags & HAMMER2_INODE_ISUNLINKED) {
176 hammer2_inode_unlink_t *ipul;
178 ipul = kmalloc(sizeof(*ipul), pmp->minode, M_WAITOK | M_ZERO);
181 hammer2_spin_ex(&pmp->list_spin);
182 TAILQ_INSERT_TAIL(&pmp->unlinkq, ipul, entry);
183 hammer2_spin_unex(&pmp->list_spin);
184 /* retain ref from vp for ipul */
186 hammer2_inode_drop(ip); /* vp ref */
190 * XXX handle background sync when ip dirty, kernel will no longer
191 * notify us regarding this inode because there is no longer a
192 * vnode attached to it.
201 hammer2_vop_fsync(struct vop_fsync_args *ap)
204 hammer2_trans_t trans;
212 /* XXX can't do this yet */
213 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_ISFLUSH);
214 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
216 hammer2_trans_init(&trans, ip->pmp, 0);
217 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
220 * Calling chain_flush here creates a lot of duplicative
221 * COW operations due to non-optimal vnode ordering.
223 * Only do it for an actual fsync() syscall. The other forms
224 * which call this function will eventually call chain_flush
225 * on the volume root as a catch-all, which is far more optimal.
227 hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
228 if (ip->flags & HAMMER2_INODE_MODIFIED)
229 hammer2_inode_fsync(&trans, ip, NULL);
230 hammer2_inode_unlock(ip, NULL);
232 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
234 if (ip->flags & (HAMMER2_INODE_RESIZED|HAMMER2_INODE_MTIME))
235 hammer2_inode_fsync(&trans, ip, cluster);
238 hammer2_trans_done(&trans);
246 hammer2_vop_access(struct vop_access_args *ap)
248 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, NULL);
266 hammer2_vop_getattr(struct vop_getattr_args *ap)
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);
297 vap->va_bytes = ip->bref.data_count;
298 vap->va_type = hammer2_get_vtype(ip->meta.type);
300 vap->va_uid_uuid = ip->meta.uid;
301 vap->va_gid_uuid = ip->meta.gid;
302 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
305 hammer2_inode_unlock(ip, NULL);
313 hammer2_vop_setattr(struct vop_setattr_args *ap)
316 hammer2_trans_t trans;
326 hammer2_update_time(&ctime);
330 if (ip->pmp->ronly) {
335 hammer2_pfs_memory_wait(ip->pmp);
336 hammer2_trans_init(&trans, ip->pmp, 0);
337 hammer2_inode_lock(ip, 0);
340 if (vap->va_flags != VNOVAL) {
343 flags = ip->meta.uflags;
344 error = vop_helper_setattr_flags(&flags, vap->va_flags,
345 hammer2_to_unix_xid(&ip->meta.uid),
348 if (ip->meta.uflags != flags) {
349 hammer2_inode_modify(&trans, ip);
350 ip->meta.uflags = flags;
351 ip->meta.ctime = ctime;
352 kflags |= NOTE_ATTRIB;
354 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
361 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
365 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
366 mode_t cur_mode = ip->meta.mode;
367 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
368 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
372 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
374 &cur_uid, &cur_gid, &cur_mode);
376 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
377 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
378 if (bcmp(&uuid_uid, &ip->meta.uid, sizeof(uuid_uid)) ||
379 bcmp(&uuid_gid, &ip->meta.gid, sizeof(uuid_gid)) ||
380 ip->meta.mode != cur_mode
382 hammer2_inode_modify(&trans, ip);
383 ip->meta.uid = uuid_uid;
384 ip->meta.gid = uuid_gid;
385 ip->meta.mode = cur_mode;
386 ip->meta.ctime = ctime;
388 kflags |= NOTE_ATTRIB;
395 if (vap->va_size != VNOVAL && ip->meta.size != vap->va_size) {
398 if (vap->va_size == ip->meta.size)
400 if (vap->va_size < ip->meta.size) {
401 hammer2_truncate_file(ip, vap->va_size);
403 hammer2_extend_file(ip, vap->va_size);
405 hammer2_inode_modify(&trans, ip);
406 ip->meta.mtime = ctime;
414 /* atime not supported */
415 if (vap->va_atime.tv_sec != VNOVAL) {
416 hammer2_inode_modify(&trans, ip);
417 ip->meta.atime = hammer2_timespec_to_time(&vap->va_atime);
418 kflags |= NOTE_ATTRIB;
421 if (vap->va_mode != (mode_t)VNOVAL) {
422 mode_t cur_mode = ip->meta.mode;
423 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
424 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
426 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
427 cur_uid, cur_gid, &cur_mode);
428 if (error == 0 && ip->meta.mode != cur_mode) {
429 hammer2_inode_modify(&trans, ip);
430 ip->meta.mode = cur_mode;
431 ip->meta.ctime = ctime;
432 kflags |= NOTE_ATTRIB;
436 if (vap->va_mtime.tv_sec != VNOVAL) {
437 hammer2_inode_modify(&trans, ip);
438 ip->meta.mtime = hammer2_timespec_to_time(&vap->va_mtime);
439 kflags |= NOTE_ATTRIB;
444 * If a truncation occurred we must call inode_fsync() now in order
445 * to trim the related data chains, otherwise a later expansion can
448 * If an extend occured that changed the DIRECTDATA state, we must
449 * call inode_fsync now in order to prepare the inode's indirect
452 if (ip->flags & HAMMER2_INODE_RESIZED)
453 hammer2_inode_fsync(&trans, ip, NULL);
458 hammer2_inode_unlock(ip, NULL);
459 hammer2_trans_done(&trans);
460 hammer2_knote(ip->vp, kflags);
468 hammer2_vop_readdir(struct vop_readdir_args *ap)
470 const hammer2_inode_data_t *ripdata;
472 hammer2_inode_t *xip;
473 hammer2_cluster_t *cparent;
474 hammer2_cluster_t *cluster;
475 hammer2_cluster_t *xcluster;
476 hammer2_blockref_t bref;
478 hammer2_key_t key_next;
492 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_ALWAYS | HAMMER2_RESOLVE_SHARED);
509 cparent = hammer2_inode_cluster(ip, HAMMER2_RESOLVE_ALWAYS |
510 HAMMER2_RESOLVE_SHARED);
512 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
515 * Handle artificial entries. To ensure that only positive 64 bit
516 * quantities are returned to userland we always strip off bit 63.
517 * The hash code is designed such that codes 0x0000-0x7FFF are not
518 * used, allowing us to use these codes for articial entries.
520 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
521 * allow '..' to cross the mount point into (e.g.) the super-root.
524 cluster = (void *)(intptr_t)-1; /* non-NULL for early goto done case */
527 inum = ripdata->meta.inum & HAMMER2_DIRHASH_USERMSK;
528 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
532 cookies[cookie_index] = saveoff;
535 if (cookie_index == ncookies)
541 * Be careful with lockorder when accessing ".."
543 * (ip is the current dir. xip is the parent dir).
545 inum = ripdata->meta.inum & HAMMER2_DIRHASH_USERMSK;
546 while (ip->pip != NULL && ip != ip->pmp->iroot) {
548 hammer2_inode_ref(xip);
549 hammer2_inode_unlock(ip, cparent);
550 hammer2_inode_lock(xip, HAMMER2_RESOLVE_ALWAYS |
551 HAMMER2_RESOLVE_SHARED);
552 xcluster = hammer2_inode_cluster(xip,
553 HAMMER2_RESOLVE_ALWAYS |
554 HAMMER2_RESOLVE_SHARED);
556 hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS |
557 HAMMER2_RESOLVE_SHARED);
558 cparent = hammer2_inode_cluster(ip,
559 HAMMER2_RESOLVE_ALWAYS |
560 HAMMER2_RESOLVE_SHARED);
561 hammer2_inode_drop(xip);
562 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
563 if (xip == ip->pip) {
564 inum = hammer2_cluster_rdata(xcluster)->
566 HAMMER2_DIRHASH_USERMSK;
567 hammer2_inode_unlock(xip, xcluster);
570 hammer2_inode_unlock(xip, xcluster);
572 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
576 cookies[cookie_index] = saveoff;
579 if (cookie_index == ncookies)
583 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
584 if (hammer2_debug & 0x0020)
585 kprintf("readdir: lkey %016jx\n", lkey);
588 * parent is the inode cluster, already locked for us. Don't
589 * double lock shared locks as this will screw up upgrades.
594 cluster = hammer2_cluster_lookup(cparent, &key_next, lkey, lkey,
595 HAMMER2_LOOKUP_SHARED);
596 if (cluster == NULL) {
597 cluster = hammer2_cluster_lookup(cparent, &key_next,
598 lkey, (hammer2_key_t)-1,
599 HAMMER2_LOOKUP_SHARED);
602 hammer2_cluster_bref(cluster, &bref);
604 if (hammer2_debug & 0x0020)
605 kprintf("readdir: p=%p chain=%p %016jx (next %016jx)\n",
606 cparent->focus, cluster->focus,
609 if (bref.type == HAMMER2_BREF_TYPE_INODE) {
610 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
611 dtype = hammer2_get_dtype(ripdata);
612 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
613 r = vop_write_dirent(&error, uio,
615 HAMMER2_DIRHASH_USERMSK,
617 ripdata->meta.name_len,
622 cookies[cookie_index] = saveoff;
625 /* XXX chain error */
626 kprintf("bad chain type readdir %d\n", bref.type);
630 * Keys may not be returned in order so once we have a
631 * placemarker (cluster) the scan must allow the full range
632 * or some entries will be missed.
634 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
635 key_next, (hammer2_key_t)-1,
636 HAMMER2_LOOKUP_SHARED);
638 hammer2_cluster_bref(cluster, &bref);
639 saveoff = (bref.key & HAMMER2_DIRHASH_USERMSK) + 1;
641 saveoff = (hammer2_key_t)-1;
643 if (cookie_index == ncookies)
647 hammer2_cluster_unlock(cluster);
648 hammer2_cluster_drop(cluster);
651 hammer2_inode_unlock(ip, cparent);
653 *ap->a_eofflag = (cluster == NULL);
654 if (hammer2_debug & 0x0020)
655 kprintf("readdir: done at %016jx\n", saveoff);
656 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
657 if (error && cookie_index == 0) {
659 kfree(cookies, M_TEMP);
661 *ap->a_cookies = NULL;
665 *ap->a_ncookies = cookie_index;
666 *ap->a_cookies = cookies;
674 * hammer2_vop_readlink { vp, uio, cred }
678 hammer2_vop_readlink(struct vop_readlink_args *ap)
685 if (vp->v_type != VLNK)
689 error = hammer2_read_file(ip, ap->a_uio, 0);
695 hammer2_vop_read(struct vop_read_args *ap)
705 * Read operations supported on this vnode?
708 if (vp->v_type != VREG)
718 seqcount = ap->a_ioflag >> 16;
719 bigread = (uio->uio_resid > 100 * 1024 * 1024);
721 error = hammer2_read_file(ip, uio, seqcount);
727 hammer2_vop_write(struct vop_write_args *ap)
730 hammer2_trans_t trans;
739 * Read operations supported on this vnode?
742 if (vp->v_type != VREG)
751 if (ip->pmp->ronly) {
755 seqcount = ap->a_ioflag >> 16;
756 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
759 * Check resource limit
761 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
762 uio->uio_offset + uio->uio_resid >
763 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
764 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
768 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
771 * The transaction interlocks against flushes initiations
772 * (note: but will run concurrently with the actual flush).
774 hammer2_trans_init(&trans, ip->pmp, 0);
775 error = hammer2_write_file(&trans, ip, uio, ap->a_ioflag, seqcount);
776 hammer2_trans_done(&trans);
782 * Perform read operations on a file or symlink given an UNLOCKED
785 * The passed ip is not locked.
789 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
800 * WARNING! Assumes that the kernel interlocks size changes at the
803 hammer2_mtx_sh(&ip->lock);
804 size = ip->meta.size;
805 hammer2_mtx_unlock(&ip->lock);
807 while (uio->uio_resid > 0 && uio->uio_offset < size) {
814 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
817 error = cluster_read(ip->vp, leof, lbase, lblksize,
818 uio->uio_resid, seqcount * BKVASIZE,
823 loff = (int)(uio->uio_offset - lbase);
825 if (n > uio->uio_resid)
827 if (n > size - uio->uio_offset)
828 n = (int)(size - uio->uio_offset);
829 bp->b_flags |= B_AGE;
830 uiomove((char *)bp->b_data + loff, n, uio);
837 * Write to the file represented by the inode via the logical buffer cache.
838 * The inode may represent a regular file or a symlink.
840 * The inode must not be locked.
844 hammer2_write_file(hammer2_trans_t *trans, hammer2_inode_t *ip,
845 struct uio *uio, int ioflag, int seqcount)
847 hammer2_key_t old_eof;
848 hammer2_key_t new_eof;
857 * WARNING! Assumes that the kernel interlocks size changes at the
860 hammer2_mtx_ex(&ip->lock);
861 if (ioflag & IO_APPEND)
862 uio->uio_offset = ip->meta.size;
863 old_eof = ip->meta.size;
866 * Extend the file if necessary. If the write fails at some point
867 * we will truncate it back down to cover as much as we were able
870 * Doing this now makes it easier to calculate buffer sizes in
877 if (uio->uio_offset + uio->uio_resid > old_eof) {
878 new_eof = uio->uio_offset + uio->uio_resid;
880 hammer2_extend_file(ip, new_eof);
881 kflags |= NOTE_EXTEND;
885 hammer2_mtx_unlock(&ip->lock);
890 while (uio->uio_resid > 0) {
899 * Don't allow the buffer build to blow out the buffer
902 if ((ioflag & IO_RECURSE) == 0)
903 bwillwrite(HAMMER2_PBUFSIZE);
906 * This nominally tells us how much we can cluster and
907 * what the logical buffer size needs to be. Currently
908 * we don't try to cluster the write and just handle one
911 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
913 loff = (int)(uio->uio_offset - lbase);
915 KKASSERT(lblksize <= 65536);
918 * Calculate bytes to copy this transfer and whether the
919 * copy completely covers the buffer or not.
923 if (n > uio->uio_resid) {
925 if (loff == lbase && uio->uio_offset + n == new_eof)
937 if (uio->uio_segflg == UIO_NOCOPY) {
939 * Issuing a write with the same data backing the
940 * buffer. Instantiate the buffer to collect the
941 * backing vm pages, then read-in any missing bits.
943 * This case is used by vop_stdputpages().
945 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
946 if ((bp->b_flags & B_CACHE) == 0) {
948 error = bread(ip->vp, lbase, lblksize, &bp);
950 } else if (trivial) {
952 * Even though we are entirely overwriting the buffer
953 * we may still have to zero it out to avoid a
954 * mmap/write visibility issue.
956 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
957 if ((bp->b_flags & B_CACHE) == 0)
961 * Partial overwrite, read in any missing bits then
962 * replace the portion being written.
964 * (The strategy code will detect zero-fill physical
965 * blocks for this case).
967 error = bread(ip->vp, lbase, lblksize, &bp);
978 * Ok, copy the data in
980 error = uiomove(bp->b_data + loff, n, uio);
981 kflags |= NOTE_WRITE;
989 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
990 * with IO_SYNC or IO_ASYNC set. These writes
991 * must be handled as the pageout daemon expects.
993 if (ioflag & IO_SYNC) {
995 } else if ((ioflag & IO_DIRECT) && endofblk) {
997 } else if (ioflag & IO_ASYNC) {
1005 * Cleanup. If we extended the file EOF but failed to write through
1006 * the entire write is a failure and we have to back-up.
1008 if (error && new_eof != old_eof) {
1009 hammer2_mtx_ex(&ip->lock);
1010 hammer2_truncate_file(ip, old_eof);
1011 if (ip->flags & HAMMER2_INODE_MODIFIED)
1012 hammer2_inode_fsync(trans, ip, NULL);
1013 hammer2_mtx_unlock(&ip->lock);
1014 } else if (modified) {
1015 hammer2_mtx_ex(&ip->lock);
1016 hammer2_inode_modify(NULL, ip);
1017 hammer2_update_time(&ip->meta.mtime);
1018 if (ip->flags & HAMMER2_INODE_MODIFIED)
1019 hammer2_inode_fsync(trans, ip, NULL);
1020 hammer2_mtx_unlock(&ip->lock);
1021 hammer2_knote(ip->vp, kflags);
1023 hammer2_trans_assert_strategy(ip->pmp);
1029 * Truncate the size of a file. The inode must not be locked.
1031 * We must unconditionally set HAMMER2_INODE_RESIZED to properly
1032 * ensure that any on-media data beyond the new file EOF has been destroyed.
1034 * WARNING: nvtruncbuf() can only be safely called without the inode lock
1035 * held due to the way our write thread works.
1037 * WARNING! Assumes that the kernel interlocks size changes at the
1042 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1044 hammer2_key_t lbase;
1048 hammer2_mtx_unlock(&ip->lock);
1050 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1051 nvtruncbuf(ip->vp, nsize,
1052 nblksize, (int)nsize & (nblksize - 1),
1055 hammer2_mtx_ex(&ip->lock);
1056 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1057 ip->osize = ip->meta.size;
1058 ip->meta.size = nsize;
1059 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED |
1060 HAMMER2_INODE_RESIZED);
1065 * Extend the size of a file. The inode must not be locked.
1067 * Even though the file size is changing, we do not have to set the
1068 * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES
1069 * boundary. When this occurs a hammer2_inode_fsync() is required
1070 * to prepare the inode cluster's indirect block table.
1072 * WARNING! Assumes that the kernel interlocks size changes at the
1077 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1079 hammer2_key_t lbase;
1080 hammer2_key_t osize;
1086 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1087 osize = ip->meta.size;
1089 ip->meta.size = nsize;
1090 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1092 if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES)
1093 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1095 hammer2_mtx_unlock(&ip->lock);
1097 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1098 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1104 hammer2_mtx_ex(&ip->lock);
1111 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1113 hammer2_inode_t *ip;
1114 hammer2_inode_t *dip;
1115 hammer2_cluster_t *cparent;
1116 hammer2_cluster_t *cluster;
1117 const hammer2_inode_data_t *ripdata;
1118 hammer2_key_t key_next;
1120 struct namecache *ncp;
1121 const uint8_t *name;
1127 dip = VTOI(ap->a_dvp);
1128 ncp = ap->a_nch->ncp;
1129 name = ncp->nc_name;
1130 name_len = ncp->nc_nlen;
1131 lhc = hammer2_dirhash(name, name_len);
1134 * Note: In DragonFly the kernel handles '.' and '..'.
1136 hammer2_inode_lock(dip, HAMMER2_RESOLVE_ALWAYS |
1137 HAMMER2_RESOLVE_SHARED);
1138 cparent = hammer2_inode_cluster(dip, HAMMER2_RESOLVE_ALWAYS |
1139 HAMMER2_RESOLVE_SHARED);
1141 cluster = hammer2_cluster_lookup(cparent, &key_next,
1142 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1143 HAMMER2_LOOKUP_SHARED);
1145 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE) {
1146 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1147 if (ripdata->meta.name_len == name_len &&
1148 bcmp(ripdata->filename, name, name_len) == 0) {
1152 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
1154 lhc + HAMMER2_DIRHASH_LOMASK,
1155 HAMMER2_LOOKUP_SHARED);
1157 hammer2_inode_unlock(dip, cparent);
1160 * Resolve hardlink entries before acquiring the inode.
1163 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1164 if (ripdata->meta.type == HAMMER2_OBJTYPE_HARDLINK) {
1165 hammer2_tid_t inum = ripdata->meta.inum;
1166 error = hammer2_hardlink_find(dip, NULL, &cluster);
1168 kprintf("hammer2: unable to find hardlink "
1169 "0x%016jx\n", inum);
1178 * nresolve needs to resolve hardlinks, the original cluster is not
1182 ip = hammer2_inode_get(dip->pmp, dip, cluster);
1183 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1184 if (ripdata->meta.type == HAMMER2_OBJTYPE_HARDLINK) {
1185 kprintf("nresolve: fixup hardlink\n");
1186 hammer2_inode_ref(ip);
1187 hammer2_inode_unlock(ip, NULL);
1188 hammer2_cluster_unlock(cluster);
1189 hammer2_cluster_drop(cluster);
1190 hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
1191 cluster = hammer2_inode_cluster(ip,
1192 HAMMER2_RESOLVE_ALWAYS);
1193 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1194 hammer2_inode_drop(ip);
1195 kprintf("nresolve: fixup to type %02x\n",
1196 ripdata->meta.type);
1204 * Deconsolidate any hardlink whos nlinks == 1. Ignore errors.
1205 * If an error occurs chain and ip are left alone.
1207 * XXX upgrade shared lock?
1209 if (ochain && chain &&
1210 chain->data->ipdata.meta.nlinks == 1 && !dip->pmp->ronly) {
1211 kprintf("hammer2: need to unconsolidate hardlink for %s\n",
1212 chain->data->ipdata.filename);
1213 /* XXX retain shared lock on dip? (currently not held) */
1214 hammer2_trans_init(&trans, dip->pmp, 0);
1215 hammer2_hardlink_deconsolidate(&trans, dip, &chain, &ochain);
1216 hammer2_trans_done(&trans);
1221 * Acquire the related vnode
1223 * NOTE: For error processing, only ENOENT resolves the namecache
1224 * entry to NULL, otherwise we just return the error and
1225 * leave the namecache unresolved.
1227 * NOTE: multiple hammer2_inode structures can be aliased to the
1228 * same chain element, for example for hardlinks. This
1229 * use case does not 'reattach' inode associations that
1230 * might already exist, but always allocates a new one.
1232 * WARNING: inode structure is locked exclusively via inode_get
1233 * but chain was locked shared. inode_unlock()
1234 * will handle it properly.
1237 vp = hammer2_igetv(ip, cluster, &error);
1240 cache_setvp(ap->a_nch, vp);
1241 } else if (error == ENOENT) {
1242 cache_setvp(ap->a_nch, NULL);
1244 hammer2_inode_unlock(ip, cluster);
1247 * The vp should not be released until after we've disposed
1248 * of our locks, because it might cause vop_inactive() to
1255 cache_setvp(ap->a_nch, NULL);
1257 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1258 ("resolve error %d/%p ap %p\n",
1259 error, ap->a_nch->ncp->nc_vp, ap));
1266 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1268 hammer2_inode_t *dip;
1269 hammer2_inode_t *ip;
1270 hammer2_cluster_t *cparent;
1274 dip = VTOI(ap->a_dvp);
1276 if ((ip = dip->pip) == NULL) {
1281 hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
1282 cparent = hammer2_inode_cluster(ip, HAMMER2_RESOLVE_ALWAYS);
1283 *ap->a_vpp = hammer2_igetv(ip, cparent, &error);
1284 hammer2_inode_unlock(ip, cparent);
1292 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1294 hammer2_inode_t *dip;
1295 hammer2_inode_t *nip;
1296 hammer2_trans_t trans;
1297 hammer2_cluster_t *cluster;
1298 struct namecache *ncp;
1299 const uint8_t *name;
1304 dip = VTOI(ap->a_dvp);
1305 if (dip->pmp->ronly) {
1310 ncp = ap->a_nch->ncp;
1311 name = ncp->nc_name;
1312 name_len = ncp->nc_nlen;
1315 hammer2_pfs_memory_wait(dip->pmp);
1316 hammer2_trans_init(&trans, dip->pmp, HAMMER2_TRANS_NEWINODE);
1317 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1319 &cluster, 0, &error);
1321 KKASSERT(nip == NULL);
1324 *ap->a_vpp = hammer2_igetv(nip, cluster, &error);
1325 hammer2_inode_unlock(nip, cluster);
1327 hammer2_trans_done(&trans);
1330 cache_setunresolved(ap->a_nch);
1331 cache_setvp(ap->a_nch, *ap->a_vpp);
1339 hammer2_vop_open(struct vop_open_args *ap)
1341 return vop_stdopen(ap);
1345 * hammer2_vop_advlock { vp, id, op, fl, flags }
1349 hammer2_vop_advlock(struct vop_advlock_args *ap)
1351 hammer2_inode_t *ip = VTOI(ap->a_vp);
1352 const hammer2_inode_data_t *ripdata;
1353 hammer2_cluster_t *cparent;
1356 hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS |
1357 HAMMER2_RESOLVE_SHARED);
1358 cparent = hammer2_inode_cluster(ip, HAMMER2_RESOLVE_ALWAYS |
1359 HAMMER2_RESOLVE_SHARED);
1360 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
1361 size = ripdata->meta.size;
1362 hammer2_inode_unlock(ip, cparent);
1363 return (lf_advlock(ap, &ip->advlock, size));
1369 hammer2_vop_close(struct vop_close_args *ap)
1371 return vop_stdclose(ap);
1375 * hammer2_vop_nlink { nch, dvp, vp, cred }
1377 * Create a hardlink from (vp) to {dvp, nch}.
1381 hammer2_vop_nlink(struct vop_nlink_args *ap)
1383 hammer2_inode_t *fdip; /* target directory to create link in */
1384 hammer2_inode_t *tdip; /* target directory to create link in */
1385 hammer2_inode_t *cdip; /* common parent directory */
1386 hammer2_inode_t *ip; /* inode we are hardlinking to */
1387 hammer2_cluster_t *cluster;
1388 hammer2_cluster_t *fdcluster;
1389 hammer2_cluster_t *tdcluster;
1390 hammer2_cluster_t *cdcluster;
1391 hammer2_trans_t trans;
1392 struct namecache *ncp;
1393 const uint8_t *name;
1398 tdip = VTOI(ap->a_dvp);
1399 if (tdip->pmp->ronly) {
1404 ncp = ap->a_nch->ncp;
1405 name = ncp->nc_name;
1406 name_len = ncp->nc_nlen;
1409 * ip represents the file being hardlinked. The file could be a
1410 * normal file or a hardlink target if it has already been hardlinked.
1411 * If ip is a hardlinked target then ip->pip represents the location
1412 * of the hardlinked target, NOT the location of the hardlink pointer.
1414 * Bump nlinks and potentially also create or move the hardlink
1415 * target in the parent directory common to (ip) and (tdip). The
1416 * consolidation code can modify ip->cluster and ip->pip. The
1417 * returned cluster is locked.
1419 ip = VTOI(ap->a_vp);
1420 hammer2_pfs_memory_wait(ip->pmp);
1421 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_NEWINODE);
1424 * The common parent directory must be locked first to avoid deadlocks.
1425 * Also note that fdip and/or tdip might match cdip.
1428 cdip = hammer2_inode_common_parent(fdip, tdip);
1429 hammer2_inode_lock(cdip, HAMMER2_RESOLVE_ALWAYS);
1430 hammer2_inode_lock(fdip, HAMMER2_RESOLVE_ALWAYS);
1431 hammer2_inode_lock(tdip, HAMMER2_RESOLVE_ALWAYS);
1432 cdcluster = hammer2_inode_cluster(cdip, HAMMER2_RESOLVE_ALWAYS);
1433 fdcluster = hammer2_inode_cluster(fdip, HAMMER2_RESOLVE_ALWAYS);
1434 tdcluster = hammer2_inode_cluster(tdip, HAMMER2_RESOLVE_ALWAYS);
1436 hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
1437 cluster = hammer2_inode_cluster(ip, HAMMER2_RESOLVE_ALWAYS);
1439 error = hammer2_hardlink_consolidate(&trans, ip, &cluster,
1440 cdip, cdcluster, 1);
1445 * Create a directory entry connected to the specified cluster.
1447 * WARNING! chain can get moved by the connect (indirectly due to
1448 * potential indirect block creation).
1450 error = hammer2_inode_connect(&trans,
1455 cache_setunresolved(ap->a_nch);
1456 cache_setvp(ap->a_nch, ap->a_vp);
1459 hammer2_inode_unlock(ip, cluster);
1460 hammer2_inode_unlock(tdip, tdcluster);
1461 hammer2_inode_unlock(fdip, fdcluster);
1462 hammer2_inode_unlock(cdip, cdcluster);
1463 hammer2_inode_drop(cdip);
1464 hammer2_trans_done(&trans);
1471 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1473 * The operating system has already ensured that the directory entry
1474 * does not exist and done all appropriate namespace locking.
1478 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1480 hammer2_inode_t *dip;
1481 hammer2_inode_t *nip;
1482 hammer2_trans_t trans;
1483 hammer2_cluster_t *ncluster;
1484 struct namecache *ncp;
1485 const uint8_t *name;
1490 dip = VTOI(ap->a_dvp);
1491 if (dip->pmp->ronly) {
1496 ncp = ap->a_nch->ncp;
1497 name = ncp->nc_name;
1498 name_len = ncp->nc_nlen;
1499 hammer2_pfs_memory_wait(dip->pmp);
1500 hammer2_trans_init(&trans, dip->pmp, HAMMER2_TRANS_NEWINODE);
1503 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1505 &ncluster, 0, &error);
1507 KKASSERT(nip == NULL);
1510 *ap->a_vpp = hammer2_igetv(nip, ncluster, &error);
1511 hammer2_inode_unlock(nip, ncluster);
1513 hammer2_trans_done(&trans);
1516 cache_setunresolved(ap->a_nch);
1517 cache_setvp(ap->a_nch, *ap->a_vpp);
1524 * Make a device node (typically a fifo)
1528 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1530 hammer2_inode_t *dip;
1531 hammer2_inode_t *nip;
1532 hammer2_trans_t trans;
1533 hammer2_cluster_t *ncluster;
1534 struct namecache *ncp;
1535 const uint8_t *name;
1540 dip = VTOI(ap->a_dvp);
1541 if (dip->pmp->ronly) {
1546 ncp = ap->a_nch->ncp;
1547 name = ncp->nc_name;
1548 name_len = ncp->nc_nlen;
1549 hammer2_pfs_memory_wait(dip->pmp);
1550 hammer2_trans_init(&trans, dip->pmp, HAMMER2_TRANS_NEWINODE);
1553 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1555 &ncluster, 0, &error);
1557 KKASSERT(nip == NULL);
1560 *ap->a_vpp = hammer2_igetv(nip, ncluster, &error);
1561 hammer2_inode_unlock(nip, ncluster);
1563 hammer2_trans_done(&trans);
1566 cache_setunresolved(ap->a_nch);
1567 cache_setvp(ap->a_nch, *ap->a_vpp);
1574 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1578 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1580 hammer2_inode_t *dip;
1581 hammer2_inode_t *nip;
1582 hammer2_cluster_t *ncparent;
1583 hammer2_trans_t trans;
1584 struct namecache *ncp;
1585 const uint8_t *name;
1589 dip = VTOI(ap->a_dvp);
1590 if (dip->pmp->ronly)
1593 ncp = ap->a_nch->ncp;
1594 name = ncp->nc_name;
1595 name_len = ncp->nc_nlen;
1596 hammer2_pfs_memory_wait(dip->pmp);
1597 hammer2_trans_init(&trans, dip->pmp, HAMMER2_TRANS_NEWINODE);
1600 ap->a_vap->va_type = VLNK; /* enforce type */
1602 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1604 &ncparent, 0, &error);
1606 KKASSERT(nip == NULL);
1608 hammer2_trans_done(&trans);
1611 *ap->a_vpp = hammer2_igetv(nip, ncparent, &error);
1614 * Build the softlink (~like file data) and finalize the namecache.
1620 hammer2_inode_data_t *nipdata;
1622 nipdata = &hammer2_cluster_wdata(ncparent)->ipdata;
1623 /* nipdata = &nip->chain->data->ipdata;XXX */
1624 bytes = strlen(ap->a_target);
1626 if (bytes <= HAMMER2_EMBEDDED_BYTES) {
1627 KKASSERT(nipdata->meta.op_flags &
1628 HAMMER2_OPFLAG_DIRECTDATA);
1629 bcopy(ap->a_target, nipdata->u.data, bytes);
1630 nipdata->meta.size = bytes;
1631 nip->meta.size = bytes;
1632 hammer2_cluster_modsync(ncparent);
1633 hammer2_inode_unlock(nip, ncparent);
1634 /* nipdata = NULL; not needed */
1636 hammer2_inode_unlock(nip, ncparent);
1637 /* nipdata = NULL; not needed */
1638 bzero(&auio, sizeof(auio));
1639 bzero(&aiov, sizeof(aiov));
1640 auio.uio_iov = &aiov;
1641 auio.uio_segflg = UIO_SYSSPACE;
1642 auio.uio_rw = UIO_WRITE;
1643 auio.uio_resid = bytes;
1644 auio.uio_iovcnt = 1;
1645 auio.uio_td = curthread;
1646 aiov.iov_base = ap->a_target;
1647 aiov.iov_len = bytes;
1648 error = hammer2_write_file(&trans, nip,
1649 &auio, IO_APPEND, 0);
1650 /* XXX handle error */
1654 hammer2_inode_unlock(nip, ncparent);
1656 hammer2_trans_done(&trans);
1659 * Finalize namecache
1662 cache_setunresolved(ap->a_nch);
1663 cache_setvp(ap->a_nch, *ap->a_vpp);
1664 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1670 * hammer2_vop_nremove { nch, dvp, cred }
1674 hammer2_vop_nremove(struct vop_nremove_args *ap)
1676 hammer2_inode_t *dip;
1677 hammer2_trans_t trans;
1678 struct namecache *ncp;
1679 const uint8_t *name;
1684 dip = VTOI(ap->a_dvp);
1685 if (dip->pmp->ronly) {
1690 ncp = ap->a_nch->ncp;
1691 name = ncp->nc_name;
1692 name_len = ncp->nc_nlen;
1694 hammer2_pfs_memory_wait(dip->pmp);
1695 hammer2_trans_init(&trans, dip->pmp, 0);
1696 error = hammer2_unlink_file(&trans, dip, NULL, name, name_len,
1697 0, NULL, ap->a_nch, -1);
1698 hammer2_run_unlinkq(&trans, dip->pmp);
1699 hammer2_trans_done(&trans);
1701 cache_unlink(ap->a_nch);
1707 * hammer2_vop_nrmdir { nch, dvp, cred }
1711 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1713 hammer2_inode_t *dip;
1714 hammer2_trans_t trans;
1715 struct namecache *ncp;
1716 const uint8_t *name;
1721 dip = VTOI(ap->a_dvp);
1722 if (dip->pmp->ronly) {
1727 ncp = ap->a_nch->ncp;
1728 name = ncp->nc_name;
1729 name_len = ncp->nc_nlen;
1731 hammer2_pfs_memory_wait(dip->pmp);
1732 hammer2_trans_init(&trans, dip->pmp, 0);
1733 hammer2_run_unlinkq(&trans, dip->pmp);
1734 error = hammer2_unlink_file(&trans, dip, NULL, name, name_len,
1735 1, NULL, ap->a_nch, -1);
1736 hammer2_trans_done(&trans);
1738 cache_unlink(ap->a_nch);
1744 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1748 hammer2_vop_nrename(struct vop_nrename_args *ap)
1750 struct namecache *fncp;
1751 struct namecache *tncp;
1752 hammer2_inode_t *cdip;
1753 hammer2_inode_t *fdip;
1754 hammer2_inode_t *tdip;
1755 hammer2_inode_t *ip;
1756 hammer2_cluster_t *cluster;
1757 hammer2_cluster_t *fdcluster;
1758 hammer2_cluster_t *tdcluster;
1759 hammer2_cluster_t *cdcluster;
1760 hammer2_trans_t trans;
1761 const uint8_t *fname;
1763 const uint8_t *tname;
1769 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1771 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1774 fdip = VTOI(ap->a_fdvp); /* source directory */
1775 tdip = VTOI(ap->a_tdvp); /* target directory */
1777 if (fdip->pmp->ronly)
1781 fncp = ap->a_fnch->ncp; /* entry name in source */
1782 fname = fncp->nc_name;
1783 fname_len = fncp->nc_nlen;
1785 tncp = ap->a_tnch->ncp; /* entry name in target */
1786 tname = tncp->nc_name;
1787 tname_len = tncp->nc_nlen;
1789 hammer2_pfs_memory_wait(tdip->pmp);
1790 hammer2_trans_init(&trans, tdip->pmp, 0);
1793 * ip is the inode being renamed. If this is a hardlink then
1794 * ip represents the actual file and not the hardlink marker.
1796 ip = VTOI(fncp->nc_vp);
1801 * The common parent directory must be locked first to avoid deadlocks.
1802 * Also note that fdip and/or tdip might match cdip.
1804 * WARNING! fdip may not match ip->pip. That is, if the source file
1805 * is already a hardlink then what we are renaming is the
1806 * hardlink pointer, not the hardlink itself. The hardlink
1807 * directory (ip->pip) will already be at a common parent
1810 * Be sure to use ip->pip when finding the common parent
1811 * against tdip or we might accidently move the hardlink
1812 * target into a subdirectory that makes it inaccessible to
1815 cdip = hammer2_inode_common_parent(ip->pip, tdip);
1816 hammer2_inode_lock(cdip, HAMMER2_RESOLVE_ALWAYS);
1817 hammer2_inode_lock(fdip, HAMMER2_RESOLVE_ALWAYS);
1818 hammer2_inode_lock(tdip, HAMMER2_RESOLVE_ALWAYS);
1819 cdcluster = hammer2_inode_cluster(cdip, HAMMER2_RESOLVE_ALWAYS);
1820 fdcluster = hammer2_inode_cluster(fdip, HAMMER2_RESOLVE_ALWAYS);
1821 tdcluster = hammer2_inode_cluster(tdip, HAMMER2_RESOLVE_ALWAYS);
1824 * Keep a tight grip on the inode so the temporary unlinking from
1825 * the source location prior to linking to the target location
1826 * does not cause the cluster to be destroyed.
1828 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
1829 * unlinking elements from their directories. Locking
1830 * the nlinks field does not lock the whole inode.
1832 hammer2_inode_ref(ip);
1835 * Remove target if it exists.
1837 error = hammer2_unlink_file(&trans, tdip, NULL, tname, tname_len,
1838 -1, NULL, ap->a_tnch, -1);
1840 if (error && error != ENOENT)
1844 * When renaming a hardlinked file we may have to re-consolidate
1845 * the location of the hardlink target.
1847 * If ip represents a regular file the consolidation code essentially
1848 * does nothing other than return the same locked cluster that was
1851 * The returned cluster will be locked.
1853 * WARNING! We do not currently have a local copy of ipdata but
1854 * we do use one later remember that it must be reloaded
1855 * on any modification to the inode, including connects.
1857 hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
1858 cluster = hammer2_inode_cluster(ip, HAMMER2_RESOLVE_ALWAYS);
1859 error = hammer2_hardlink_consolidate(&trans, ip, &cluster,
1860 cdip, cdcluster, 0);
1865 * Disconnect (fdip, fname) from the source directory. This will
1866 * disconnect (ip) if it represents a direct file. If (ip) represents
1867 * a hardlink the HARDLINK pointer object will be removed but the
1868 * hardlink will stay intact.
1870 * Always pass nch as NULL because we intend to reconnect the inode,
1871 * so we don't want hammer2_unlink_file() to rename it to the hidden
1872 * open-but-unlinked directory.
1874 * The target cluster may be marked DELETED but will not be destroyed
1875 * since we retain our hold on ip and cluster.
1877 * NOTE: We pass nlinks as 0 (not -1) in order to retain the file's
1880 error = hammer2_unlink_file(&trans, fdip, ip, fname, fname_len,
1881 -1, &hlink, NULL, 0);
1882 KKASSERT(error != EAGAIN);
1887 * Reconnect ip to target directory using cluster. Chains cannot
1888 * actually be moved, so this will duplicate the cluster in the new
1889 * spot and assign it to the ip, replacing the old cluster.
1891 * WARNING: Because recursive locks are allowed and we unlinked the
1892 * file that we have a cluster-in-hand for just above, the
1893 * cluster might have been delete-duplicated. We must
1894 * refactor the cluster.
1896 * WARNING: Chain locks can lock buffer cache buffers, to avoid
1897 * deadlocks we want to unlock before issuing a cache_*()
1898 * op (that might have to lock a vnode).
1900 * NOTE: Pass nlinks as 0 because we retained the link count from
1901 * the unlink, so we do not have to modify it.
1903 error = hammer2_inode_connect(&trans,
1904 ip, &cluster, hlink,
1906 tname, tname_len, 0);
1908 KKASSERT(cluster != NULL);
1909 hammer2_inode_repoint(ip, (hlink ? ip->pip : tdip), cluster);
1912 hammer2_inode_unlock(ip, cluster);
1914 hammer2_inode_unlock(tdip, tdcluster);
1915 hammer2_inode_unlock(fdip, fdcluster);
1916 hammer2_inode_unlock(cdip, cdcluster);
1917 hammer2_inode_drop(ip);
1918 hammer2_inode_drop(cdip);
1919 hammer2_run_unlinkq(&trans, fdip->pmp);
1920 hammer2_trans_done(&trans);
1923 * Issue the namecache update after unlocking all the internal
1924 * hammer structures, otherwise we might deadlock.
1926 if (tnch_error == 0) {
1927 cache_unlink(ap->a_tnch);
1928 cache_setunresolved(ap->a_tnch);
1931 cache_rename(ap->a_fnch, ap->a_tnch);
1938 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
1942 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
1944 hammer2_inode_t *ip;
1948 ip = VTOI(ap->a_vp);
1950 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
1951 ap->a_fflag, ap->a_cred);
1958 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
1966 case (MOUNTCTL_SET_EXPORT):
1967 mp = ap->a_head.a_ops->head.vv_mount;
1970 if (ap->a_ctllen != sizeof(struct export_args))
1973 rc = vfs_export(mp, &pmp->export,
1974 (const struct export_args *)ap->a_ctl);
1977 rc = vop_stdmountctl(ap);
1985 * This handles unlinked open files after the vnode is finally dereferenced.
1986 * To avoid deadlocks it cannot be called from the normal vnode recycling
1987 * path, so we call it (1) after a unlink, rmdir, or rename, (2) on every
1988 * flush, and (3) on umount.
1991 hammer2_run_unlinkq(hammer2_trans_t *trans, hammer2_pfs_t *pmp)
1993 const hammer2_inode_data_t *ripdata;
1994 hammer2_inode_unlink_t *ipul;
1995 hammer2_inode_t *ip;
1996 hammer2_cluster_t *cluster;
1997 hammer2_cluster_t *cparent;
1999 if (TAILQ_EMPTY(&pmp->unlinkq))
2003 hammer2_spin_ex(&pmp->list_spin);
2004 while ((ipul = TAILQ_FIRST(&pmp->unlinkq)) != NULL) {
2005 TAILQ_REMOVE(&pmp->unlinkq, ipul, entry);
2006 hammer2_spin_unex(&pmp->list_spin);
2008 kfree(ipul, pmp->minode);
2010 hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
2011 cluster = hammer2_inode_cluster(ip, HAMMER2_RESOLVE_ALWAYS);
2012 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
2013 if (hammer2_debug & 0x400) {
2014 kprintf("hammer2: unlink on reclaim: %s refs=%d\n",
2015 ripdata->filename, ip->refs);
2019 * NOTE: Due to optimizations to avoid I/O on the inode for
2020 * the last unlink, ripdata->nlinks is not necessarily
2023 /* KKASSERT(ripdata->nlinks == 0); (see NOTE) */
2024 cparent = hammer2_cluster_parent(cluster);
2025 hammer2_cluster_delete(trans, cparent, cluster,
2026 HAMMER2_DELETE_PERMANENT);
2027 hammer2_cluster_unlock(cparent);
2028 hammer2_cluster_drop(cparent);
2029 hammer2_inode_unlock(ip, cluster); /* inode lock */
2030 hammer2_inode_drop(ip); /* ipul ref */
2032 hammer2_spin_ex(&pmp->list_spin);
2034 hammer2_spin_unex(&pmp->list_spin);
2042 static void filt_hammer2detach(struct knote *kn);
2043 static int filt_hammer2read(struct knote *kn, long hint);
2044 static int filt_hammer2write(struct knote *kn, long hint);
2045 static int filt_hammer2vnode(struct knote *kn, long hint);
2047 static struct filterops hammer2read_filtops =
2048 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2049 NULL, filt_hammer2detach, filt_hammer2read };
2050 static struct filterops hammer2write_filtops =
2051 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2052 NULL, filt_hammer2detach, filt_hammer2write };
2053 static struct filterops hammer2vnode_filtops =
2054 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2055 NULL, filt_hammer2detach, filt_hammer2vnode };
2059 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
2061 struct vnode *vp = ap->a_vp;
2062 struct knote *kn = ap->a_kn;
2064 switch (kn->kn_filter) {
2066 kn->kn_fop = &hammer2read_filtops;
2069 kn->kn_fop = &hammer2write_filtops;
2072 kn->kn_fop = &hammer2vnode_filtops;
2075 return (EOPNOTSUPP);
2078 kn->kn_hook = (caddr_t)vp;
2080 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2086 filt_hammer2detach(struct knote *kn)
2088 struct vnode *vp = (void *)kn->kn_hook;
2090 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2094 filt_hammer2read(struct knote *kn, long hint)
2096 struct vnode *vp = (void *)kn->kn_hook;
2097 hammer2_inode_t *ip = VTOI(vp);
2100 if (hint == NOTE_REVOKE) {
2101 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2104 off = ip->meta.size - kn->kn_fp->f_offset;
2105 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2106 if (kn->kn_sfflags & NOTE_OLDAPI)
2108 return (kn->kn_data != 0);
2113 filt_hammer2write(struct knote *kn, long hint)
2115 if (hint == NOTE_REVOKE)
2116 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2122 filt_hammer2vnode(struct knote *kn, long hint)
2124 if (kn->kn_sfflags & hint)
2125 kn->kn_fflags |= hint;
2126 if (hint == NOTE_REVOKE) {
2127 kn->kn_flags |= (EV_EOF | EV_NODATA);
2130 return (kn->kn_fflags != 0);
2138 hammer2_vop_markatime(struct vop_markatime_args *ap)
2140 hammer2_inode_t *ip;
2153 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2157 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2159 error = hammer2_vop_kqfilter(ap);
2166 struct vop_ops hammer2_vnode_vops = {
2167 .vop_default = vop_defaultop,
2168 .vop_fsync = hammer2_vop_fsync,
2169 .vop_getpages = vop_stdgetpages,
2170 .vop_putpages = vop_stdputpages,
2171 .vop_access = hammer2_vop_access,
2172 .vop_advlock = hammer2_vop_advlock,
2173 .vop_close = hammer2_vop_close,
2174 .vop_nlink = hammer2_vop_nlink,
2175 .vop_ncreate = hammer2_vop_ncreate,
2176 .vop_nsymlink = hammer2_vop_nsymlink,
2177 .vop_nremove = hammer2_vop_nremove,
2178 .vop_nrmdir = hammer2_vop_nrmdir,
2179 .vop_nrename = hammer2_vop_nrename,
2180 .vop_getattr = hammer2_vop_getattr,
2181 .vop_setattr = hammer2_vop_setattr,
2182 .vop_readdir = hammer2_vop_readdir,
2183 .vop_readlink = hammer2_vop_readlink,
2184 .vop_getpages = vop_stdgetpages,
2185 .vop_putpages = vop_stdputpages,
2186 .vop_read = hammer2_vop_read,
2187 .vop_write = hammer2_vop_write,
2188 .vop_open = hammer2_vop_open,
2189 .vop_inactive = hammer2_vop_inactive,
2190 .vop_reclaim = hammer2_vop_reclaim,
2191 .vop_nresolve = hammer2_vop_nresolve,
2192 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2193 .vop_nmkdir = hammer2_vop_nmkdir,
2194 .vop_nmknod = hammer2_vop_nmknod,
2195 .vop_ioctl = hammer2_vop_ioctl,
2196 .vop_mountctl = hammer2_vop_mountctl,
2197 .vop_bmap = hammer2_vop_bmap,
2198 .vop_strategy = hammer2_vop_strategy,
2199 .vop_kqfilter = hammer2_vop_kqfilter
2202 struct vop_ops hammer2_spec_vops = {
2203 .vop_default = vop_defaultop,
2204 .vop_fsync = hammer2_vop_fsync,
2205 .vop_read = vop_stdnoread,
2206 .vop_write = vop_stdnowrite,
2207 .vop_access = hammer2_vop_access,
2208 .vop_close = hammer2_vop_close,
2209 .vop_markatime = hammer2_vop_markatime,
2210 .vop_getattr = hammer2_vop_getattr,
2211 .vop_inactive = hammer2_vop_inactive,
2212 .vop_reclaim = hammer2_vop_reclaim,
2213 .vop_setattr = hammer2_vop_setattr
2216 struct vop_ops hammer2_fifo_vops = {
2217 .vop_default = fifo_vnoperate,
2218 .vop_fsync = hammer2_vop_fsync,
2220 .vop_read = hammer2_vop_fiforead,
2221 .vop_write = hammer2_vop_fifowrite,
2223 .vop_access = hammer2_vop_access,
2225 .vop_close = hammer2_vop_fifoclose,
2227 .vop_markatime = hammer2_vop_markatime,
2228 .vop_getattr = hammer2_vop_getattr,
2229 .vop_inactive = hammer2_vop_inactive,
2230 .vop_reclaim = hammer2_vop_reclaim,
2231 .vop_setattr = hammer2_vop_setattr,
2232 .vop_kqfilter = hammer2_vop_fifokqfilter