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_vop_info;
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;
205 hammer2_cluster_t *cluster;
213 /* XXX can't do this yet */
214 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_ISFLUSH);
215 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
217 hammer2_trans_init(&trans, ip->pmp, 0);
218 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
221 * Calling chain_flush here creates a lot of duplicative
222 * COW operations due to non-optimal vnode ordering.
224 * Only do it for an actual fsync() syscall. The other forms
225 * which call this function will eventually call chain_flush
226 * on the volume root as a catch-all, which is far more optimal.
228 cluster = hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
229 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
231 if (ip->flags & (HAMMER2_INODE_RESIZED|HAMMER2_INODE_MTIME))
232 hammer2_inode_fsync(&trans, ip, cluster);
234 hammer2_inode_unlock(ip, cluster);
235 hammer2_trans_done(&trans);
243 hammer2_vop_access(struct vop_access_args *ap)
245 hammer2_inode_t *ip = VTOI(ap->a_vp);
246 const hammer2_inode_data_t *ripdata;
247 hammer2_cluster_t *cluster;
253 cluster = hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS |
254 HAMMER2_RESOLVE_SHARED);
255 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
256 uid = hammer2_to_unix_xid(&ripdata->meta.uid);
257 gid = hammer2_to_unix_xid(&ripdata->meta.gid);
258 error = vop_helper_access(ap, uid, gid,
259 ripdata->meta.mode, ripdata->meta.uflags);
260 hammer2_inode_unlock(ip, cluster);
268 hammer2_vop_getattr(struct vop_getattr_args *ap)
270 const hammer2_inode_data_t *ripdata;
271 hammer2_cluster_t *cluster;
274 hammer2_blockref_t bref;
285 cluster = hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS |
286 HAMMER2_RESOLVE_SHARED);
287 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
288 KKASSERT(hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE);
289 hammer2_cluster_bref(cluster, &bref);
291 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
292 vap->va_fileid = ripdata->meta.inum;
293 vap->va_mode = ripdata->meta.mode;
294 vap->va_nlink = ripdata->meta.nlinks;
295 vap->va_uid = hammer2_to_unix_xid(&ripdata->meta.uid);
296 vap->va_gid = hammer2_to_unix_xid(&ripdata->meta.gid);
299 vap->va_size = ip->meta.size; /* protected by shared lock */
300 vap->va_blocksize = HAMMER2_PBUFSIZE;
301 vap->va_flags = ripdata->meta.uflags;
302 hammer2_time_to_timespec(ripdata->meta.ctime, &vap->va_ctime);
303 hammer2_time_to_timespec(ripdata->meta.mtime, &vap->va_mtime);
304 hammer2_time_to_timespec(ripdata->meta.mtime, &vap->va_atime);
306 vap->va_bytes = bref.data_count;
307 vap->va_type = hammer2_get_vtype(ripdata);
309 vap->va_uid_uuid = ripdata->meta.uid;
310 vap->va_gid_uuid = ripdata->meta.gid;
311 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
314 hammer2_inode_unlock(ip, cluster);
322 hammer2_vop_setattr(struct vop_setattr_args *ap)
324 const hammer2_inode_data_t *ripdata;
325 hammer2_inode_data_t *wipdata;
327 hammer2_cluster_t *cluster;
328 hammer2_trans_t trans;
340 hammer2_update_time(&ctime);
344 if (ip->pmp->ronly) {
349 hammer2_pfs_memory_wait(ip->pmp);
350 hammer2_trans_init(&trans, ip->pmp, 0);
351 cluster = hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
352 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
355 if (vap->va_flags != VNOVAL) {
358 flags = ripdata->meta.uflags;
359 error = vop_helper_setattr_flags(&flags, vap->va_flags,
360 hammer2_to_unix_xid(&ripdata->meta.uid),
363 if (ripdata->meta.uflags != flags) {
364 wipdata = hammer2_cluster_modify_ip(&trans, ip,
366 wipdata->meta.uflags = flags;
367 wipdata->meta.ctime = ctime;
368 kflags |= NOTE_ATTRIB;
372 if (ripdata->meta.uflags & (IMMUTABLE | APPEND)) {
379 if (ripdata->meta.uflags & (IMMUTABLE | APPEND)) {
383 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
384 mode_t cur_mode = ripdata->meta.mode;
385 uid_t cur_uid = hammer2_to_unix_xid(&ripdata->meta.uid);
386 gid_t cur_gid = hammer2_to_unix_xid(&ripdata->meta.gid);
390 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
392 &cur_uid, &cur_gid, &cur_mode);
394 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
395 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
396 if (bcmp(&uuid_uid, &ripdata->meta.uid,
398 bcmp(&uuid_gid, &ripdata->meta.gid,
400 ripdata->meta.mode != cur_mode
402 wipdata = hammer2_cluster_modify_ip(&trans, ip,
404 wipdata->meta.uid = uuid_uid;
405 wipdata->meta.gid = uuid_gid;
406 wipdata->meta.mode = cur_mode;
407 wipdata->meta.ctime = ctime;
411 kflags |= NOTE_ATTRIB;
418 if (vap->va_size != VNOVAL && ip->meta.size != vap->va_size) {
421 if (vap->va_size == ip->meta.size)
423 hammer2_inode_unlock(ip, cluster);
424 if (vap->va_size < ip->meta.size) {
425 hammer2_truncate_file(ip, vap->va_size);
427 hammer2_extend_file(ip, vap->va_size);
429 cluster = hammer2_inode_lock(ip,
430 HAMMER2_RESOLVE_ALWAYS);
432 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
441 /* atime not supported */
442 if (vap->va_atime.tv_sec != VNOVAL) {
443 wipdata = hammer2_cluster_modify_ip(&trans, ip, cluster, 0);
444 wipdata->meta.atime = hammer2_timespec_to_time(&vap->va_atime);
445 kflags |= NOTE_ATTRIB;
450 if (vap->va_mtime.tv_sec != VNOVAL) {
451 wipdata = hammer2_cluster_modify_ip(&trans, ip, cluster, 0);
452 wipdata->meta.mtime = hammer2_timespec_to_time(&vap->va_mtime);
453 kflags |= NOTE_ATTRIB;
458 if (vap->va_mode != (mode_t)VNOVAL) {
459 mode_t cur_mode = ripdata->meta.mode;
460 uid_t cur_uid = hammer2_to_unix_xid(&ripdata->meta.uid);
461 gid_t cur_gid = hammer2_to_unix_xid(&ripdata->meta.gid);
463 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
464 cur_uid, cur_gid, &cur_mode);
465 if (error == 0 && ripdata->meta.mode != cur_mode) {
466 wipdata = hammer2_cluster_modify_ip(&trans, ip,
468 wipdata->meta.mode = cur_mode;
469 wipdata->meta.ctime = ctime;
470 kflags |= NOTE_ATTRIB;
477 * If a truncation occurred we must call inode_fsync() now in order
478 * to trim the related data chains, otherwise a later expansion can
482 hammer2_cluster_modsync(cluster);
485 hammer2_inode_fsync(&trans, ip, cluster);
488 * Cleanup. If domtime is set an additional inode modification
489 * must be flagged. All other modifications will have already
490 * set INODE_MODIFIED and called vsetisdirty().
494 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED |
495 HAMMER2_INODE_MTIME);
499 hammer2_cluster_modsync(cluster);
500 hammer2_inode_unlock(ip, cluster);
501 hammer2_trans_done(&trans);
502 hammer2_knote(ip->vp, kflags);
510 hammer2_vop_readdir(struct vop_readdir_args *ap)
512 const hammer2_inode_data_t *ripdata;
514 hammer2_inode_t *xip;
515 hammer2_cluster_t *cparent;
516 hammer2_cluster_t *cluster;
517 hammer2_cluster_t *xcluster;
518 hammer2_blockref_t bref;
520 hammer2_key_t key_next;
534 saveoff = uio->uio_offset;
537 * Setup cookies directory entry cookies if requested
539 if (ap->a_ncookies) {
540 ncookies = uio->uio_resid / 16 + 1;
543 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
550 cparent = hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS |
551 HAMMER2_RESOLVE_SHARED);
553 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
556 * Handle artificial entries. To ensure that only positive 64 bit
557 * quantities are returned to userland we always strip off bit 63.
558 * The hash code is designed such that codes 0x0000-0x7FFF are not
559 * used, allowing us to use these codes for articial entries.
561 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
562 * allow '..' to cross the mount point into (e.g.) the super-root.
565 cluster = (void *)(intptr_t)-1; /* non-NULL for early goto done case */
568 inum = ripdata->meta.inum & HAMMER2_DIRHASH_USERMSK;
569 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
573 cookies[cookie_index] = saveoff;
576 if (cookie_index == ncookies)
582 * Be careful with lockorder when accessing ".."
584 * (ip is the current dir. xip is the parent dir).
586 inum = ripdata->meta.inum & HAMMER2_DIRHASH_USERMSK;
587 while (ip->pip != NULL && ip != ip->pmp->iroot) {
589 hammer2_inode_ref(xip);
590 hammer2_inode_unlock(ip, cparent);
591 xcluster = hammer2_inode_lock(xip,
592 HAMMER2_RESOLVE_ALWAYS |
593 HAMMER2_RESOLVE_SHARED);
595 cparent = hammer2_inode_lock(ip,
596 HAMMER2_RESOLVE_ALWAYS |
597 HAMMER2_RESOLVE_SHARED);
598 hammer2_inode_drop(xip);
599 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
600 if (xip == ip->pip) {
601 inum = hammer2_cluster_rdata(xcluster)->
603 HAMMER2_DIRHASH_USERMSK;
604 hammer2_inode_unlock(xip, xcluster);
607 hammer2_inode_unlock(xip, xcluster);
609 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
613 cookies[cookie_index] = saveoff;
616 if (cookie_index == ncookies)
620 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
621 if (hammer2_debug & 0x0020)
622 kprintf("readdir: lkey %016jx\n", lkey);
625 * parent is the inode cluster, already locked for us. Don't
626 * double lock shared locks as this will screw up upgrades.
631 cluster = hammer2_cluster_lookup(cparent, &key_next, lkey, lkey,
632 HAMMER2_LOOKUP_SHARED);
633 if (cluster == NULL) {
634 cluster = hammer2_cluster_lookup(cparent, &key_next,
635 lkey, (hammer2_key_t)-1,
636 HAMMER2_LOOKUP_SHARED);
639 hammer2_cluster_bref(cluster, &bref);
641 if (hammer2_debug & 0x0020)
642 kprintf("readdir: p=%p chain=%p %016jx (next %016jx)\n",
643 cparent->focus, cluster->focus,
646 if (bref.type == HAMMER2_BREF_TYPE_INODE) {
647 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
648 dtype = hammer2_get_dtype(ripdata);
649 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
650 r = vop_write_dirent(&error, uio,
652 HAMMER2_DIRHASH_USERMSK,
654 ripdata->meta.name_len,
659 cookies[cookie_index] = saveoff;
662 /* XXX chain error */
663 kprintf("bad chain type readdir %d\n", bref.type);
667 * Keys may not be returned in order so once we have a
668 * placemarker (cluster) the scan must allow the full range
669 * or some entries will be missed.
671 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
672 key_next, (hammer2_key_t)-1,
673 HAMMER2_LOOKUP_SHARED);
675 hammer2_cluster_bref(cluster, &bref);
676 saveoff = (bref.key & HAMMER2_DIRHASH_USERMSK) + 1;
678 saveoff = (hammer2_key_t)-1;
680 if (cookie_index == ncookies)
684 hammer2_cluster_unlock(cluster);
685 hammer2_cluster_drop(cluster);
688 hammer2_inode_unlock(ip, cparent);
690 *ap->a_eofflag = (cluster == NULL);
691 if (hammer2_debug & 0x0020)
692 kprintf("readdir: done at %016jx\n", saveoff);
693 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
694 if (error && cookie_index == 0) {
696 kfree(cookies, M_TEMP);
698 *ap->a_cookies = NULL;
702 *ap->a_ncookies = cookie_index;
703 *ap->a_cookies = cookies;
711 * hammer2_vop_readlink { vp, uio, cred }
715 hammer2_vop_readlink(struct vop_readlink_args *ap)
722 if (vp->v_type != VLNK)
726 error = hammer2_read_file(ip, ap->a_uio, 0);
732 hammer2_vop_read(struct vop_read_args *ap)
742 * Read operations supported on this vnode?
745 if (vp->v_type != VREG)
755 seqcount = ap->a_ioflag >> 16;
756 bigread = (uio->uio_resid > 100 * 1024 * 1024);
758 error = hammer2_read_file(ip, uio, seqcount);
764 hammer2_vop_write(struct vop_write_args *ap)
767 hammer2_trans_t trans;
776 * Read operations supported on this vnode?
779 if (vp->v_type != VREG)
788 if (ip->pmp->ronly) {
792 seqcount = ap->a_ioflag >> 16;
793 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
796 * Check resource limit
798 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
799 uio->uio_offset + uio->uio_resid >
800 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
801 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
805 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
808 * The transaction interlocks against flushes initiations
809 * (note: but will run concurrently with the actual flush).
811 hammer2_trans_init(&trans, ip->pmp, 0);
812 error = hammer2_write_file(ip, uio, ap->a_ioflag, seqcount);
813 hammer2_trans_done(&trans);
819 * Perform read operations on a file or symlink given an UNLOCKED
822 * The passed ip is not locked.
826 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
837 * WARNING! Assumes that the kernel interlocks size changes at the
840 hammer2_mtx_sh(&ip->lock);
841 size = ip->meta.size;
842 hammer2_mtx_unlock(&ip->lock);
844 while (uio->uio_resid > 0 && uio->uio_offset < size) {
851 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
854 error = cluster_read(ip->vp, leof, lbase, lblksize,
855 uio->uio_resid, seqcount * BKVASIZE,
860 loff = (int)(uio->uio_offset - lbase);
862 if (n > uio->uio_resid)
864 if (n > size - uio->uio_offset)
865 n = (int)(size - uio->uio_offset);
866 bp->b_flags |= B_AGE;
867 uiomove((char *)bp->b_data + loff, n, uio);
874 * Write to the file represented by the inode via the logical buffer cache.
875 * The inode may represent a regular file or a symlink.
877 * The inode must not be locked.
881 hammer2_write_file(hammer2_inode_t *ip,
882 struct uio *uio, int ioflag, int seqcount)
884 hammer2_key_t old_eof;
885 hammer2_key_t new_eof;
894 * WARNING! Assumes that the kernel interlocks size changes at the
897 hammer2_mtx_ex(&ip->lock);
898 if (ioflag & IO_APPEND)
899 uio->uio_offset = ip->meta.size;
900 old_eof = ip->meta.size;
901 hammer2_mtx_unlock(&ip->lock);
904 * Extend the file if necessary. If the write fails at some point
905 * we will truncate it back down to cover as much as we were able
908 * Doing this now makes it easier to calculate buffer sizes in
915 if (uio->uio_offset + uio->uio_resid > old_eof) {
916 new_eof = uio->uio_offset + uio->uio_resid;
918 hammer2_extend_file(ip, new_eof);
919 kflags |= NOTE_EXTEND;
927 while (uio->uio_resid > 0) {
936 * Don't allow the buffer build to blow out the buffer
939 if ((ioflag & IO_RECURSE) == 0)
940 bwillwrite(HAMMER2_PBUFSIZE);
943 * This nominally tells us how much we can cluster and
944 * what the logical buffer size needs to be. Currently
945 * we don't try to cluster the write and just handle one
948 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
950 loff = (int)(uio->uio_offset - lbase);
952 KKASSERT(lblksize <= 65536);
955 * Calculate bytes to copy this transfer and whether the
956 * copy completely covers the buffer or not.
960 if (n > uio->uio_resid) {
962 if (loff == lbase && uio->uio_offset + n == new_eof)
974 if (uio->uio_segflg == UIO_NOCOPY) {
976 * Issuing a write with the same data backing the
977 * buffer. Instantiate the buffer to collect the
978 * backing vm pages, then read-in any missing bits.
980 * This case is used by vop_stdputpages().
982 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
983 if ((bp->b_flags & B_CACHE) == 0) {
985 error = bread(ip->vp, lbase, lblksize, &bp);
987 } else if (trivial) {
989 * Even though we are entirely overwriting the buffer
990 * we may still have to zero it out to avoid a
991 * mmap/write visibility issue.
993 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
994 if ((bp->b_flags & B_CACHE) == 0)
998 * Partial overwrite, read in any missing bits then
999 * replace the portion being written.
1001 * (The strategy code will detect zero-fill physical
1002 * blocks for this case).
1004 error = bread(ip->vp, lbase, lblksize, &bp);
1015 * Ok, copy the data in
1017 error = uiomove(bp->b_data + loff, n, uio);
1018 kflags |= NOTE_WRITE;
1026 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
1027 * with IO_SYNC or IO_ASYNC set. These writes
1028 * must be handled as the pageout daemon expects.
1030 if (ioflag & IO_SYNC) {
1032 } else if ((ioflag & IO_DIRECT) && endofblk) {
1034 } else if (ioflag & IO_ASYNC) {
1042 * Cleanup. If we extended the file EOF but failed to write through
1043 * the entire write is a failure and we have to back-up.
1045 if (error && new_eof != old_eof) {
1046 hammer2_truncate_file(ip, old_eof);
1047 } else if (modified) {
1048 hammer2_mtx_ex(&ip->lock);
1049 hammer2_update_time(&ip->meta.mtime);
1050 atomic_set_int(&ip->flags, HAMMER2_INODE_MTIME);
1051 hammer2_mtx_unlock(&ip->lock);
1053 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1054 hammer2_knote(ip->vp, kflags);
1055 vsetisdirty(ip->vp);
1056 hammer2_trans_assert_strategy(ip->pmp);
1062 * Truncate the size of a file. The inode must not be locked.
1064 * NOTE: Caller handles setting HAMMER2_INODE_MODIFIED
1066 * WARNING: nvtruncbuf() can only be safely called without the inode lock
1067 * held due to the way our write thread works.
1069 * WARNING! Assumes that the kernel interlocks size changes at the
1074 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1076 hammer2_key_t lbase;
1081 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1082 nvtruncbuf(ip->vp, nsize,
1083 nblksize, (int)nsize & (nblksize - 1),
1086 hammer2_mtx_ex(&ip->lock);
1087 ip->meta.size = nsize;
1088 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1089 hammer2_mtx_unlock(&ip->lock);
1094 * Extend the size of a file. The inode must not be locked.
1096 * WARNING! Assumes that the kernel interlocks size changes at the
1099 * NOTE: Caller handles setting HAMMER2_INODE_MODIFIED
1103 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1105 hammer2_key_t lbase;
1106 hammer2_key_t osize;
1111 hammer2_mtx_ex(&ip->lock);
1112 osize = ip->meta.size;
1113 ip->meta.size = nsize;
1114 hammer2_mtx_unlock(&ip->lock);
1117 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1118 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1124 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1130 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1132 hammer2_inode_t *ip;
1133 hammer2_inode_t *dip;
1134 hammer2_cluster_t *cparent;
1135 hammer2_cluster_t *cluster;
1136 const hammer2_inode_data_t *ripdata;
1137 hammer2_key_t key_next;
1139 struct namecache *ncp;
1140 const uint8_t *name;
1146 dip = VTOI(ap->a_dvp);
1147 ncp = ap->a_nch->ncp;
1148 name = ncp->nc_name;
1149 name_len = ncp->nc_nlen;
1150 lhc = hammer2_dirhash(name, name_len);
1153 * Note: In DragonFly the kernel handles '.' and '..'.
1155 cparent = hammer2_inode_lock(dip, HAMMER2_RESOLVE_ALWAYS |
1156 HAMMER2_RESOLVE_SHARED);
1158 cluster = hammer2_cluster_lookup(cparent, &key_next,
1159 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1160 HAMMER2_LOOKUP_SHARED);
1162 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE) {
1163 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1164 if (ripdata->meta.name_len == name_len &&
1165 bcmp(ripdata->filename, name, name_len) == 0) {
1169 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
1171 lhc + HAMMER2_DIRHASH_LOMASK,
1172 HAMMER2_LOOKUP_SHARED);
1174 hammer2_inode_unlock(dip, cparent);
1177 * Resolve hardlink entries before acquiring the inode.
1180 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1181 if (ripdata->meta.type == HAMMER2_OBJTYPE_HARDLINK) {
1182 hammer2_tid_t inum = ripdata->meta.inum;
1183 error = hammer2_hardlink_find(dip, NULL, &cluster);
1185 kprintf("hammer2: unable to find hardlink "
1186 "0x%016jx\n", inum);
1195 * nresolve needs to resolve hardlinks, the original cluster is not
1199 ip = hammer2_inode_get(dip->pmp, dip, cluster);
1200 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1201 if (ripdata->meta.type == HAMMER2_OBJTYPE_HARDLINK) {
1202 kprintf("nresolve: fixup hardlink\n");
1203 hammer2_inode_ref(ip);
1204 hammer2_inode_unlock(ip, NULL);
1205 hammer2_cluster_unlock(cluster);
1206 hammer2_cluster_drop(cluster);
1207 cluster = hammer2_inode_lock(ip,
1208 HAMMER2_RESOLVE_ALWAYS);
1209 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1210 hammer2_inode_drop(ip);
1211 kprintf("nresolve: fixup to type %02x\n",
1212 ripdata->meta.type);
1220 * Deconsolidate any hardlink whos nlinks == 1. Ignore errors.
1221 * If an error occurs chain and ip are left alone.
1223 * XXX upgrade shared lock?
1225 if (ochain && chain &&
1226 chain->data->ipdata.meta.nlinks == 1 && !dip->pmp->ronly) {
1227 kprintf("hammer2: need to unconsolidate hardlink for %s\n",
1228 chain->data->ipdata.filename);
1229 /* XXX retain shared lock on dip? (currently not held) */
1230 hammer2_trans_init(&trans, dip->pmp, 0);
1231 hammer2_hardlink_deconsolidate(&trans, dip, &chain, &ochain);
1232 hammer2_trans_done(&trans);
1237 * Acquire the related vnode
1239 * NOTE: For error processing, only ENOENT resolves the namecache
1240 * entry to NULL, otherwise we just return the error and
1241 * leave the namecache unresolved.
1243 * NOTE: multiple hammer2_inode structures can be aliased to the
1244 * same chain element, for example for hardlinks. This
1245 * use case does not 'reattach' inode associations that
1246 * might already exist, but always allocates a new one.
1248 * WARNING: inode structure is locked exclusively via inode_get
1249 * but chain was locked shared. inode_unlock()
1250 * will handle it properly.
1253 vp = hammer2_igetv(ip, cluster, &error);
1256 cache_setvp(ap->a_nch, vp);
1257 } else if (error == ENOENT) {
1258 cache_setvp(ap->a_nch, NULL);
1260 hammer2_inode_unlock(ip, cluster);
1263 * The vp should not be released until after we've disposed
1264 * of our locks, because it might cause vop_inactive() to
1271 cache_setvp(ap->a_nch, NULL);
1273 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1274 ("resolve error %d/%p ap %p\n",
1275 error, ap->a_nch->ncp->nc_vp, ap));
1282 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1284 hammer2_inode_t *dip;
1285 hammer2_inode_t *ip;
1286 hammer2_cluster_t *cparent;
1290 dip = VTOI(ap->a_dvp);
1292 if ((ip = dip->pip) == NULL) {
1297 cparent = hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
1298 *ap->a_vpp = hammer2_igetv(ip, cparent, &error);
1299 hammer2_inode_unlock(ip, cparent);
1307 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1309 hammer2_inode_t *dip;
1310 hammer2_inode_t *nip;
1311 hammer2_trans_t trans;
1312 hammer2_cluster_t *cluster;
1313 struct namecache *ncp;
1314 const uint8_t *name;
1319 dip = VTOI(ap->a_dvp);
1320 if (dip->pmp->ronly) {
1325 ncp = ap->a_nch->ncp;
1326 name = ncp->nc_name;
1327 name_len = ncp->nc_nlen;
1330 hammer2_pfs_memory_wait(dip->pmp);
1331 hammer2_trans_init(&trans, dip->pmp, HAMMER2_TRANS_NEWINODE);
1332 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1334 &cluster, 0, &error);
1336 KKASSERT(nip == NULL);
1339 *ap->a_vpp = hammer2_igetv(nip, cluster, &error);
1340 hammer2_inode_unlock(nip, cluster);
1342 hammer2_trans_done(&trans);
1345 cache_setunresolved(ap->a_nch);
1346 cache_setvp(ap->a_nch, *ap->a_vpp);
1354 hammer2_vop_open(struct vop_open_args *ap)
1356 return vop_stdopen(ap);
1360 * hammer2_vop_advlock { vp, id, op, fl, flags }
1364 hammer2_vop_advlock(struct vop_advlock_args *ap)
1366 hammer2_inode_t *ip = VTOI(ap->a_vp);
1367 const hammer2_inode_data_t *ripdata;
1368 hammer2_cluster_t *cparent;
1371 cparent = hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS |
1372 HAMMER2_RESOLVE_SHARED);
1373 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
1374 size = ripdata->meta.size;
1375 hammer2_inode_unlock(ip, cparent);
1376 return (lf_advlock(ap, &ip->advlock, size));
1382 hammer2_vop_close(struct vop_close_args *ap)
1384 return vop_stdclose(ap);
1388 * hammer2_vop_nlink { nch, dvp, vp, cred }
1390 * Create a hardlink from (vp) to {dvp, nch}.
1394 hammer2_vop_nlink(struct vop_nlink_args *ap)
1396 hammer2_inode_t *fdip; /* target directory to create link in */
1397 hammer2_inode_t *tdip; /* target directory to create link in */
1398 hammer2_inode_t *cdip; /* common parent directory */
1399 hammer2_inode_t *ip; /* inode we are hardlinking to */
1400 hammer2_cluster_t *cluster;
1401 hammer2_cluster_t *fdcluster;
1402 hammer2_cluster_t *tdcluster;
1403 hammer2_cluster_t *cdcluster;
1404 hammer2_trans_t trans;
1405 struct namecache *ncp;
1406 const uint8_t *name;
1411 tdip = VTOI(ap->a_dvp);
1412 if (tdip->pmp->ronly) {
1417 ncp = ap->a_nch->ncp;
1418 name = ncp->nc_name;
1419 name_len = ncp->nc_nlen;
1422 * ip represents the file being hardlinked. The file could be a
1423 * normal file or a hardlink target if it has already been hardlinked.
1424 * If ip is a hardlinked target then ip->pip represents the location
1425 * of the hardlinked target, NOT the location of the hardlink pointer.
1427 * Bump nlinks and potentially also create or move the hardlink
1428 * target in the parent directory common to (ip) and (tdip). The
1429 * consolidation code can modify ip->cluster and ip->pip. The
1430 * returned cluster is locked.
1432 ip = VTOI(ap->a_vp);
1433 hammer2_pfs_memory_wait(ip->pmp);
1434 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_NEWINODE);
1437 * The common parent directory must be locked first to avoid deadlocks.
1438 * Also note that fdip and/or tdip might match cdip.
1441 cdip = hammer2_inode_common_parent(fdip, tdip);
1442 cdcluster = hammer2_inode_lock(cdip, HAMMER2_RESOLVE_ALWAYS);
1443 fdcluster = hammer2_inode_lock(fdip, HAMMER2_RESOLVE_ALWAYS);
1444 tdcluster = hammer2_inode_lock(tdip, HAMMER2_RESOLVE_ALWAYS);
1445 cluster = hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
1446 error = hammer2_hardlink_consolidate(&trans, ip, &cluster,
1447 cdip, cdcluster, 1);
1452 * Create a directory entry connected to the specified cluster.
1454 * WARNING! chain can get moved by the connect (indirectly due to
1455 * potential indirect block creation).
1457 error = hammer2_inode_connect(&trans,
1462 cache_setunresolved(ap->a_nch);
1463 cache_setvp(ap->a_nch, ap->a_vp);
1466 hammer2_inode_unlock(ip, cluster);
1467 hammer2_inode_unlock(tdip, tdcluster);
1468 hammer2_inode_unlock(fdip, fdcluster);
1469 hammer2_inode_unlock(cdip, cdcluster);
1470 hammer2_inode_drop(cdip);
1471 hammer2_trans_done(&trans);
1478 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1480 * The operating system has already ensured that the directory entry
1481 * does not exist and done all appropriate namespace locking.
1485 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1487 hammer2_inode_t *dip;
1488 hammer2_inode_t *nip;
1489 hammer2_trans_t trans;
1490 hammer2_cluster_t *ncluster;
1491 struct namecache *ncp;
1492 const uint8_t *name;
1497 dip = VTOI(ap->a_dvp);
1498 if (dip->pmp->ronly) {
1503 ncp = ap->a_nch->ncp;
1504 name = ncp->nc_name;
1505 name_len = ncp->nc_nlen;
1506 hammer2_pfs_memory_wait(dip->pmp);
1507 hammer2_trans_init(&trans, dip->pmp, HAMMER2_TRANS_NEWINODE);
1510 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1512 &ncluster, 0, &error);
1514 KKASSERT(nip == NULL);
1517 *ap->a_vpp = hammer2_igetv(nip, ncluster, &error);
1518 hammer2_inode_unlock(nip, ncluster);
1520 hammer2_trans_done(&trans);
1523 cache_setunresolved(ap->a_nch);
1524 cache_setvp(ap->a_nch, *ap->a_vpp);
1531 * Make a device node (typically a fifo)
1535 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1537 hammer2_inode_t *dip;
1538 hammer2_inode_t *nip;
1539 hammer2_trans_t trans;
1540 hammer2_cluster_t *ncluster;
1541 struct namecache *ncp;
1542 const uint8_t *name;
1547 dip = VTOI(ap->a_dvp);
1548 if (dip->pmp->ronly) {
1553 ncp = ap->a_nch->ncp;
1554 name = ncp->nc_name;
1555 name_len = ncp->nc_nlen;
1556 hammer2_pfs_memory_wait(dip->pmp);
1557 hammer2_trans_init(&trans, dip->pmp, HAMMER2_TRANS_NEWINODE);
1560 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1562 &ncluster, 0, &error);
1564 KKASSERT(nip == NULL);
1567 *ap->a_vpp = hammer2_igetv(nip, ncluster, &error);
1568 hammer2_inode_unlock(nip, ncluster);
1570 hammer2_trans_done(&trans);
1573 cache_setunresolved(ap->a_nch);
1574 cache_setvp(ap->a_nch, *ap->a_vpp);
1581 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1585 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1587 hammer2_inode_t *dip;
1588 hammer2_inode_t *nip;
1589 hammer2_cluster_t *ncparent;
1590 hammer2_trans_t trans;
1591 struct namecache *ncp;
1592 const uint8_t *name;
1596 dip = VTOI(ap->a_dvp);
1597 if (dip->pmp->ronly)
1600 ncp = ap->a_nch->ncp;
1601 name = ncp->nc_name;
1602 name_len = ncp->nc_nlen;
1603 hammer2_pfs_memory_wait(dip->pmp);
1604 hammer2_trans_init(&trans, dip->pmp, HAMMER2_TRANS_NEWINODE);
1607 ap->a_vap->va_type = VLNK; /* enforce type */
1609 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1611 &ncparent, 0, &error);
1613 KKASSERT(nip == NULL);
1615 hammer2_trans_done(&trans);
1618 *ap->a_vpp = hammer2_igetv(nip, ncparent, &error);
1621 * Build the softlink (~like file data) and finalize the namecache.
1627 hammer2_inode_data_t *nipdata;
1629 nipdata = &hammer2_cluster_wdata(ncparent)->ipdata;
1630 /* nipdata = &nip->chain->data->ipdata;XXX */
1631 bytes = strlen(ap->a_target);
1633 if (bytes <= HAMMER2_EMBEDDED_BYTES) {
1634 KKASSERT(nipdata->meta.op_flags &
1635 HAMMER2_OPFLAG_DIRECTDATA);
1636 bcopy(ap->a_target, nipdata->u.data, bytes);
1637 nipdata->meta.size = bytes;
1638 nip->meta.size = bytes;
1639 hammer2_cluster_modsync(ncparent);
1640 hammer2_inode_unlock(nip, ncparent);
1641 /* nipdata = NULL; not needed */
1643 hammer2_inode_unlock(nip, ncparent);
1644 /* nipdata = NULL; not needed */
1645 bzero(&auio, sizeof(auio));
1646 bzero(&aiov, sizeof(aiov));
1647 auio.uio_iov = &aiov;
1648 auio.uio_segflg = UIO_SYSSPACE;
1649 auio.uio_rw = UIO_WRITE;
1650 auio.uio_resid = bytes;
1651 auio.uio_iovcnt = 1;
1652 auio.uio_td = curthread;
1653 aiov.iov_base = ap->a_target;
1654 aiov.iov_len = bytes;
1655 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1656 /* XXX handle error */
1660 hammer2_inode_unlock(nip, ncparent);
1662 hammer2_trans_done(&trans);
1665 * Finalize namecache
1668 cache_setunresolved(ap->a_nch);
1669 cache_setvp(ap->a_nch, *ap->a_vpp);
1670 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1676 * hammer2_vop_nremove { nch, dvp, cred }
1680 hammer2_vop_nremove(struct vop_nremove_args *ap)
1682 hammer2_inode_t *dip;
1683 hammer2_trans_t trans;
1684 struct namecache *ncp;
1685 const uint8_t *name;
1690 dip = VTOI(ap->a_dvp);
1691 if (dip->pmp->ronly) {
1696 ncp = ap->a_nch->ncp;
1697 name = ncp->nc_name;
1698 name_len = ncp->nc_nlen;
1700 hammer2_pfs_memory_wait(dip->pmp);
1701 hammer2_trans_init(&trans, dip->pmp, 0);
1702 error = hammer2_unlink_file(&trans, dip, name, name_len,
1703 0, NULL, ap->a_nch, -1);
1704 hammer2_run_unlinkq(&trans, dip->pmp);
1705 hammer2_trans_done(&trans);
1707 cache_unlink(ap->a_nch);
1713 * hammer2_vop_nrmdir { nch, dvp, cred }
1717 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1719 hammer2_inode_t *dip;
1720 hammer2_trans_t trans;
1721 struct namecache *ncp;
1722 const uint8_t *name;
1727 dip = VTOI(ap->a_dvp);
1728 if (dip->pmp->ronly) {
1733 ncp = ap->a_nch->ncp;
1734 name = ncp->nc_name;
1735 name_len = ncp->nc_nlen;
1737 hammer2_pfs_memory_wait(dip->pmp);
1738 hammer2_trans_init(&trans, dip->pmp, 0);
1739 hammer2_run_unlinkq(&trans, dip->pmp);
1740 error = hammer2_unlink_file(&trans, dip, name, name_len,
1741 1, NULL, ap->a_nch, -1);
1742 hammer2_trans_done(&trans);
1744 cache_unlink(ap->a_nch);
1750 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1754 hammer2_vop_nrename(struct vop_nrename_args *ap)
1756 struct namecache *fncp;
1757 struct namecache *tncp;
1758 hammer2_inode_t *cdip;
1759 hammer2_inode_t *fdip;
1760 hammer2_inode_t *tdip;
1761 hammer2_inode_t *ip;
1762 hammer2_cluster_t *cluster;
1763 hammer2_cluster_t *fdcluster;
1764 hammer2_cluster_t *tdcluster;
1765 hammer2_cluster_t *cdcluster;
1766 hammer2_trans_t trans;
1767 const uint8_t *fname;
1769 const uint8_t *tname;
1775 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1777 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1780 fdip = VTOI(ap->a_fdvp); /* source directory */
1781 tdip = VTOI(ap->a_tdvp); /* target directory */
1783 if (fdip->pmp->ronly)
1787 fncp = ap->a_fnch->ncp; /* entry name in source */
1788 fname = fncp->nc_name;
1789 fname_len = fncp->nc_nlen;
1791 tncp = ap->a_tnch->ncp; /* entry name in target */
1792 tname = tncp->nc_name;
1793 tname_len = tncp->nc_nlen;
1795 hammer2_pfs_memory_wait(tdip->pmp);
1796 hammer2_trans_init(&trans, tdip->pmp, 0);
1799 * ip is the inode being renamed. If this is a hardlink then
1800 * ip represents the actual file and not the hardlink marker.
1802 ip = VTOI(fncp->nc_vp);
1807 * The common parent directory must be locked first to avoid deadlocks.
1808 * Also note that fdip and/or tdip might match cdip.
1810 * WARNING! fdip may not match ip->pip. That is, if the source file
1811 * is already a hardlink then what we are renaming is the
1812 * hardlink pointer, not the hardlink itself. The hardlink
1813 * directory (ip->pip) will already be at a common parent
1816 * Be sure to use ip->pip when finding the common parent
1817 * against tdip or we might accidently move the hardlink
1818 * target into a subdirectory that makes it inaccessible to
1821 cdip = hammer2_inode_common_parent(ip->pip, tdip);
1822 cdcluster = hammer2_inode_lock(cdip, HAMMER2_RESOLVE_ALWAYS);
1823 fdcluster = hammer2_inode_lock(fdip, HAMMER2_RESOLVE_ALWAYS);
1824 tdcluster = hammer2_inode_lock(tdip, HAMMER2_RESOLVE_ALWAYS);
1827 * Keep a tight grip on the inode so the temporary unlinking from
1828 * the source location prior to linking to the target location
1829 * does not cause the cluster to be destroyed.
1831 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
1832 * unlinking elements from their directories. Locking
1833 * the nlinks field does not lock the whole inode.
1835 hammer2_inode_ref(ip);
1838 * Remove target if it exists.
1840 error = hammer2_unlink_file(&trans, tdip, tname, tname_len,
1841 -1, NULL, ap->a_tnch, -1);
1843 if (error && error != ENOENT)
1847 * When renaming a hardlinked file we may have to re-consolidate
1848 * the location of the hardlink target.
1850 * If ip represents a regular file the consolidation code essentially
1851 * does nothing other than return the same locked cluster that was
1854 * The returned cluster will be locked.
1856 * WARNING! We do not currently have a local copy of ipdata but
1857 * we do use one later remember that it must be reloaded
1858 * on any modification to the inode, including connects.
1860 cluster = hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
1861 error = hammer2_hardlink_consolidate(&trans, ip, &cluster,
1862 cdip, cdcluster, 0);
1867 * Disconnect (fdip, fname) from the source directory. This will
1868 * disconnect (ip) if it represents a direct file. If (ip) represents
1869 * a hardlink the HARDLINK pointer object will be removed but the
1870 * hardlink will stay intact.
1872 * Always pass nch as NULL because we intend to reconnect the inode,
1873 * so we don't want hammer2_unlink_file() to rename it to the hidden
1874 * open-but-unlinked directory.
1876 * The target cluster may be marked DELETED but will not be destroyed
1877 * since we retain our hold on ip and cluster.
1879 * NOTE: We pass nlinks as 0 (not -1) in order to retain the file's
1882 error = hammer2_unlink_file(&trans, fdip, fname, fname_len,
1883 -1, &hlink, NULL, 0);
1884 KKASSERT(error != EAGAIN);
1889 * Reconnect ip to target directory using cluster. Chains cannot
1890 * actually be moved, so this will duplicate the cluster in the new
1891 * spot and assign it to the ip, replacing the old cluster.
1893 * WARNING: Because recursive locks are allowed and we unlinked the
1894 * file that we have a cluster-in-hand for just above, the
1895 * cluster might have been delete-duplicated. We must
1896 * refactor the cluster.
1898 * WARNING: Chain locks can lock buffer cache buffers, to avoid
1899 * deadlocks we want to unlock before issuing a cache_*()
1900 * op (that might have to lock a vnode).
1902 * NOTE: Pass nlinks as 0 because we retained the link count from
1903 * the unlink, so we do not have to modify it.
1905 error = hammer2_inode_connect(&trans,
1906 ip, &cluster, hlink,
1908 tname, tname_len, 0);
1910 KKASSERT(cluster != NULL);
1911 hammer2_inode_repoint(ip, (hlink ? ip->pip : tdip), cluster);
1914 hammer2_inode_unlock(ip, cluster);
1915 hammer2_inode_unlock(tdip, tdcluster);
1916 hammer2_inode_unlock(fdip, fdcluster);
1917 hammer2_inode_unlock(cdip, cdcluster);
1918 hammer2_inode_drop(ip);
1919 hammer2_inode_drop(cdip);
1920 hammer2_run_unlinkq(&trans, fdip->pmp);
1921 hammer2_trans_done(&trans);
1924 * Issue the namecache update after unlocking all the internal
1925 * hammer structures, otherwise we might deadlock.
1927 if (tnch_error == 0) {
1928 cache_unlink(ap->a_tnch);
1929 cache_setunresolved(ap->a_tnch);
1932 cache_rename(ap->a_fnch, ap->a_tnch);
1939 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
1943 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
1945 hammer2_inode_t *ip;
1949 ip = VTOI(ap->a_vp);
1951 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
1952 ap->a_fflag, ap->a_cred);
1959 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
1967 case (MOUNTCTL_SET_EXPORT):
1968 mp = ap->a_head.a_ops->head.vv_mount;
1971 if (ap->a_ctllen != sizeof(struct export_args))
1974 rc = vfs_export(mp, &pmp->export,
1975 (const struct export_args *)ap->a_ctl);
1978 rc = vop_stdmountctl(ap);
1986 * This handles unlinked open files after the vnode is finally dereferenced.
1987 * To avoid deadlocks it cannot be called from the normal vnode recycling
1988 * path, so we call it (1) after a unlink, rmdir, or rename, (2) on every
1989 * flush, and (3) on umount.
1992 hammer2_run_unlinkq(hammer2_trans_t *trans, hammer2_pfs_t *pmp)
1994 const hammer2_inode_data_t *ripdata;
1995 hammer2_inode_unlink_t *ipul;
1996 hammer2_inode_t *ip;
1997 hammer2_cluster_t *cluster;
1998 hammer2_cluster_t *cparent;
2000 if (TAILQ_EMPTY(&pmp->unlinkq))
2004 hammer2_spin_ex(&pmp->list_spin);
2005 while ((ipul = TAILQ_FIRST(&pmp->unlinkq)) != NULL) {
2006 TAILQ_REMOVE(&pmp->unlinkq, ipul, entry);
2007 hammer2_spin_unex(&pmp->list_spin);
2009 kfree(ipul, pmp->minode);
2011 cluster = hammer2_inode_lock(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