2 * Copyright (c) 2011-2018 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7 * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Kernel Filesystem interface
39 * NOTE! local ipdata pointers must be reloaded on any modifying operation
40 * to the inode as its underlying chain may have changed.
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/fcntl.h>
49 #include <sys/namei.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/mountctl.h>
53 #include <sys/dirent.h>
55 #include <sys/objcache.h>
56 #include <sys/event.h>
58 #include <vfs/fifofs/fifo.h>
62 static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio,
64 static int hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
65 int ioflag, int seqcount);
66 static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize);
67 static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize);
69 struct objcache *cache_xops;
73 hammer2_knote(struct vnode *vp, int flags)
76 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
80 * Last reference to a vnode is going away but it is still cached.
84 hammer2_vop_inactive(struct vop_inactive_args *ap)
101 * Check for deleted inodes and recycle immediately on the last
102 * release. Be sure to destroy any left-over buffer cache buffers
103 * so we do not waste time trying to flush them.
105 * Note that deleting the file block chains under the inode chain
106 * would just be a waste of energy, so don't do it.
108 * WARNING: nvtruncbuf() can only be safely called without the inode
109 * lock held due to the way our write thread works.
111 if (ip->flags & HAMMER2_INODE_ISUNLINKED) {
116 * Detect updates to the embedded data which may be
117 * synchronized by the strategy code. Simply mark the
118 * inode modified so it gets picked up by our normal flush.
120 nblksize = hammer2_calc_logical(ip, 0, &lbase, NULL);
121 nvtruncbuf(vp, 0, nblksize, 0, 0);
128 * Reclaim a vnode so that it can be reused; after the inode is
129 * disassociated, the filesystem must manage it alone.
133 hammer2_vop_reclaim(struct vop_reclaim_args *ap)
147 * The final close of a deleted file or directory marks it for
148 * destruction. The DELETED flag allows the flusher to shortcut
149 * any modified blocks still unflushed (that is, just ignore them).
151 * HAMMER2 usually does not try to optimize the freemap by returning
152 * deleted blocks to it as it does not usually know how many snapshots
153 * might be referencing portions of the file/dir.
159 * NOTE! We do not attempt to flush chains here, flushing is
160 * really fragile and could also deadlock.
165 * Modified inodes will already be on SIDEQ or SYNCQ, no further
168 * We cannot safely synchronize the inode from inside the reclaim
169 * due to potentially deep locks held as-of when the reclaim occurs.
170 * Interactions and potential deadlocks abound. We also can't do it
171 * here without desynchronizing from the related directory entrie(s).
173 hammer2_inode_drop(ip); /* vp ref */
175 if ((ip->flags & (HAMMER2_INODE_ISUNLINKED |
176 HAMMER2_INODE_MODIFIED |
177 HAMMER2_INODE_RESIZED |
178 HAMMER2_INODE_DIRTYDATA |
179 HAMMER2_INODE_CREATING |
180 HAMMER2_INODE_DELETING)) &&
181 (ip->flags & HAMMER2_INODE_ISDELETED) == 0) {
182 hammer2_spin_ex(&pmp->list_spin);
183 if ((ip->flags & (HAMMER2_INODE_SYNCQ |
184 HAMMER2_INODE_SIDEQ)) == 0) {
186 atomic_set_int(&ip->flags, HAMMER2_INODE_SIDEQ);
187 TAILQ_INSERT_TAIL(&pmp->sideq, ip, entry);
189 hammer2_spin_unex(&pmp->list_spin);
190 /* retain ip ref for SIDEQ linkage */
192 hammer2_spin_unex(&pmp->list_spin);
193 hammer2_inode_drop(ip); /* vp ref */
196 hammer2_inode_drop(ip); /* vp ref */
201 * XXX handle background sync when ip dirty, kernel will no longer
202 * notify us regarding this inode because there is no longer a
203 * vnode attached to it.
210 * Currently this function synchronizes the front-end inode state to the
211 * backend chain topology, then flushes the inode's chain and sub-topology
212 * to backend media. This function does not flush the root topology down to
217 hammer2_vop_fsync(struct vop_fsync_args *ap)
228 hammer2_trans_init(ip->pmp, 0);
231 * Flush dirty buffers in the file's logical buffer cache.
232 * It is best to wait for the strategy code to commit the
233 * buffers to the device's backing buffer cache before
234 * then trying to flush the inode.
236 * This should be quick, but certain inode modifications cached
237 * entirely in the hammer2_inode structure may not trigger a
238 * buffer read until the flush so the fsync can wind up also
239 * doing scattered reads.
241 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
242 bio_track_wait(&vp->v_track_write, 0, 0);
245 * Flush any inode changes
247 hammer2_inode_lock(ip, 0);
248 if (ip->flags & (HAMMER2_INODE_RESIZED|HAMMER2_INODE_MODIFIED))
249 error1 = hammer2_inode_chain_sync(ip);
252 * Flush dirty chains related to the inode.
254 * NOTE! We are not in a flush transaction. The inode remains on
255 * the sideq so the filesystem syncer can synchronize it to
258 error2 = hammer2_inode_chain_flush(ip, HAMMER2_XOP_INODE_STOP);
263 * We may be able to clear the vnode dirty flag. The
264 * hammer2_pfs_moderate() code depends on this usually working.
266 if ((ip->flags & (HAMMER2_INODE_MODIFIED |
267 HAMMER2_INODE_RESIZED |
268 HAMMER2_INODE_DIRTYDATA)) == 0 &&
269 RB_EMPTY(&vp->v_rbdirty_tree) &&
270 !bio_track_active(&vp->v_track_write)) {
273 hammer2_inode_unlock(ip);
274 hammer2_trans_done(ip->pmp, 0);
281 hammer2_vop_access(struct vop_access_args *ap)
283 hammer2_inode_t *ip = VTOI(ap->a_vp);
288 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
289 uid = hammer2_to_unix_xid(&ip->meta.uid);
290 gid = hammer2_to_unix_xid(&ip->meta.gid);
291 error = vop_helper_access(ap, uid, gid, ip->meta.mode, ip->meta.uflags);
292 hammer2_inode_unlock(ip);
299 hammer2_vop_getattr(struct vop_getattr_args *ap)
305 hammer2_chain_t *chain;
314 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
316 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
317 vap->va_fileid = ip->meta.inum;
318 vap->va_mode = ip->meta.mode;
319 vap->va_nlink = ip->meta.nlinks;
320 vap->va_uid = hammer2_to_unix_xid(&ip->meta.uid);
321 vap->va_gid = hammer2_to_unix_xid(&ip->meta.gid);
324 vap->va_size = ip->meta.size; /* protected by shared lock */
325 vap->va_blocksize = HAMMER2_PBUFSIZE;
326 vap->va_flags = ip->meta.uflags;
327 hammer2_time_to_timespec(ip->meta.ctime, &vap->va_ctime);
328 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_mtime);
329 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_atime);
332 if (ip->meta.type == HAMMER2_OBJTYPE_DIRECTORY) {
334 * Can't really calculate directory use sans the files under
335 * it, just assume one block for now.
337 vap->va_bytes += HAMMER2_INODE_BYTES;
339 for (i = 0; i < ip->cluster.nchains; ++i) {
340 if ((chain = ip->cluster.array[i].chain) != NULL) {
342 chain->bref.embed.stats.data_count) {
344 chain->bref.embed.stats.data_count;
349 vap->va_type = hammer2_get_vtype(ip->meta.type);
351 vap->va_uid_uuid = ip->meta.uid;
352 vap->va_gid_uuid = ip->meta.gid;
353 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
356 hammer2_inode_unlock(ip);
363 hammer2_vop_setattr(struct vop_setattr_args *ap)
374 hammer2_update_time(&ctime);
380 if (hammer2_vfs_enospace(ip, 0, ap->a_cred) > 1)
383 /*hammer2_pfs_memory_wait(ip->pmp);*/
384 hammer2_trans_init(ip->pmp, 0);
385 hammer2_inode_lock(ip, 0);
388 if (vap->va_flags != VNOVAL) {
391 flags = ip->meta.uflags;
392 error = vop_helper_setattr_flags(&flags, vap->va_flags,
393 hammer2_to_unix_xid(&ip->meta.uid),
396 if (ip->meta.uflags != flags) {
397 hammer2_inode_modify(ip);
398 ip->meta.uflags = flags;
399 ip->meta.ctime = ctime;
400 kflags |= NOTE_ATTRIB;
402 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
409 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
413 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
414 mode_t cur_mode = ip->meta.mode;
415 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
416 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
420 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
422 &cur_uid, &cur_gid, &cur_mode);
424 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
425 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
426 if (bcmp(&uuid_uid, &ip->meta.uid, sizeof(uuid_uid)) ||
427 bcmp(&uuid_gid, &ip->meta.gid, sizeof(uuid_gid)) ||
428 ip->meta.mode != cur_mode
430 hammer2_inode_modify(ip);
431 ip->meta.uid = uuid_uid;
432 ip->meta.gid = uuid_gid;
433 ip->meta.mode = cur_mode;
434 ip->meta.ctime = ctime;
436 kflags |= NOTE_ATTRIB;
443 if (vap->va_size != VNOVAL && ip->meta.size != vap->va_size) {
446 if (vap->va_size == ip->meta.size)
448 if (vap->va_size < ip->meta.size) {
449 hammer2_mtx_ex(&ip->truncate_lock);
450 hammer2_truncate_file(ip, vap->va_size);
451 hammer2_mtx_unlock(&ip->truncate_lock);
452 kflags |= NOTE_WRITE;
454 hammer2_extend_file(ip, vap->va_size);
455 kflags |= NOTE_WRITE | NOTE_EXTEND;
457 hammer2_inode_modify(ip);
458 ip->meta.mtime = ctime;
459 vclrflags(vp, VLASTWRITETS);
467 /* atime not supported */
468 if (vap->va_atime.tv_sec != VNOVAL) {
469 hammer2_inode_modify(ip);
470 ip->meta.atime = hammer2_timespec_to_time(&vap->va_atime);
471 kflags |= NOTE_ATTRIB;
474 if (vap->va_mode != (mode_t)VNOVAL) {
475 mode_t cur_mode = ip->meta.mode;
476 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
477 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
479 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
480 cur_uid, cur_gid, &cur_mode);
481 if (error == 0 && ip->meta.mode != cur_mode) {
482 hammer2_inode_modify(ip);
483 ip->meta.mode = cur_mode;
484 ip->meta.ctime = ctime;
485 kflags |= NOTE_ATTRIB;
489 if (vap->va_mtime.tv_sec != VNOVAL) {
490 hammer2_inode_modify(ip);
491 ip->meta.mtime = hammer2_timespec_to_time(&vap->va_mtime);
492 kflags |= NOTE_ATTRIB;
493 vclrflags(vp, VLASTWRITETS);
498 * If a truncation occurred we must call chain_sync() now in order
499 * to trim the related data chains, otherwise a later expansion can
502 * If an extend occured that changed the DIRECTDATA state, we must
503 * call inode_fsync now in order to prepare the inode's indirect
506 * WARNING! This means we are making an adjustment to the inode's
507 * chain outside of sync/fsync, and not just to inode->meta, which
508 * may result in some consistency issues if a crash were to occur
509 * at just the wrong time.
511 if (ip->flags & HAMMER2_INODE_RESIZED)
512 hammer2_inode_chain_sync(ip);
517 hammer2_inode_unlock(ip);
518 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ);
519 hammer2_knote(ip->vp, kflags);
526 hammer2_vop_readdir(struct vop_readdir_args *ap)
528 hammer2_xop_readdir_t *xop;
529 hammer2_blockref_t bref;
544 saveoff = uio->uio_offset;
549 * Setup cookies directory entry cookies if requested
551 if (ap->a_ncookies) {
552 ncookies = uio->uio_resid / 16 + 1;
555 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
562 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
565 * Handle artificial entries. To ensure that only positive 64 bit
566 * quantities are returned to userland we always strip off bit 63.
567 * The hash code is designed such that codes 0x0000-0x7FFF are not
568 * used, allowing us to use these codes for articial entries.
570 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
571 * allow '..' to cross the mount point into (e.g.) the super-root.
574 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
575 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
579 cookies[cookie_index] = saveoff;
582 if (cookie_index == ncookies)
588 * Be careful with lockorder when accessing ".."
590 * (ip is the current dir. xip is the parent dir).
592 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
593 if (ip != ip->pmp->iroot)
594 inum = ip->meta.iparent & HAMMER2_DIRHASH_USERMSK;
595 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
599 cookies[cookie_index] = saveoff;
602 if (cookie_index == ncookies)
606 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
607 if (hammer2_debug & 0x0020)
608 kprintf("readdir: lkey %016jx\n", lkey);
613 * Use XOP for cluster scan.
615 * parent is the inode cluster, already locked for us. Don't
616 * double lock shared locks as this will screw up upgrades.
618 xop = hammer2_xop_alloc(ip, 0);
620 hammer2_xop_start(&xop->head, &hammer2_readdir_desc);
623 const hammer2_inode_data_t *ripdata;
627 error = hammer2_xop_collect(&xop->head, 0);
628 error = hammer2_error_to_errno(error);
632 if (cookie_index == ncookies)
634 if (hammer2_debug & 0x0020)
635 kprintf("cluster chain %p %p\n",
636 xop->head.cluster.focus,
637 (xop->head.cluster.focus ?
638 xop->head.cluster.focus->data : (void *)-1));
639 hammer2_cluster_bref(&xop->head.cluster, &bref);
641 if (bref.type == HAMMER2_BREF_TYPE_INODE) {
642 ripdata = &hammer2_xop_gdata(&xop->head)->ipdata;
643 dtype = hammer2_get_dtype(ripdata->meta.type);
644 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
645 r = vop_write_dirent(&error, uio,
647 HAMMER2_DIRHASH_USERMSK,
649 ripdata->meta.name_len,
651 hammer2_xop_pdata(&xop->head);
655 cookies[cookie_index] = saveoff;
657 } else if (bref.type == HAMMER2_BREF_TYPE_DIRENT) {
660 dtype = hammer2_get_dtype(bref.embed.dirent.type);
661 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
662 namlen = bref.embed.dirent.namlen;
663 if (namlen <= sizeof(bref.check.buf)) {
664 dname = bref.check.buf;
666 dname = hammer2_xop_gdata(&xop->head)->buf;
668 r = vop_write_dirent(&error, uio,
669 bref.embed.dirent.inum, dtype,
671 if (namlen > sizeof(bref.check.buf))
672 hammer2_xop_pdata(&xop->head);
676 cookies[cookie_index] = saveoff;
679 /* XXX chain error */
680 kprintf("bad chain type readdir %d\n", bref.type);
683 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
684 if (error == ENOENT) {
687 saveoff = (hammer2_key_t)-1;
689 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
692 hammer2_inode_unlock(ip);
694 *ap->a_eofflag = eofflag;
695 if (hammer2_debug & 0x0020)
696 kprintf("readdir: done at %016jx\n", saveoff);
697 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
698 if (error && cookie_index == 0) {
700 kfree(cookies, M_TEMP);
702 *ap->a_cookies = NULL;
706 *ap->a_ncookies = cookie_index;
707 *ap->a_cookies = cookies;
714 * hammer2_vop_readlink { vp, uio, cred }
718 hammer2_vop_readlink(struct vop_readlink_args *ap)
725 if (vp->v_type != VLNK)
729 error = hammer2_read_file(ip, ap->a_uio, 0);
735 hammer2_vop_read(struct vop_read_args *ap)
745 * Read operations supported on this vnode?
748 if (vp->v_type != VREG)
758 seqcount = ap->a_ioflag >> 16;
759 bigread = (uio->uio_resid > 100 * 1024 * 1024);
761 error = hammer2_read_file(ip, uio, seqcount);
767 hammer2_vop_write(struct vop_write_args *ap)
778 * Read operations supported on this vnode?
781 if (vp->v_type != VREG)
788 ioflag = ap->a_ioflag;
793 switch (hammer2_vfs_enospace(ip, uio->uio_resid, ap->a_cred)) {
797 ioflag |= IO_DIRECT; /* semi-synchronous */
803 seqcount = ioflag >> 16;
806 * Check resource limit
808 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
809 uio->uio_offset + uio->uio_resid >
810 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
811 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
816 * The transaction interlocks against flush initiations
817 * (note: but will run concurrently with the actual flush).
819 * To avoid deadlocking against the VM system, we must flag any
820 * transaction related to the buffer cache or other direct
821 * VM page manipulation.
823 if (uio->uio_segflg == UIO_NOCOPY) {
824 hammer2_trans_init(ip->pmp, HAMMER2_TRANS_BUFCACHE);
826 /*hammer2_pfs_memory_wait(ip->pmp);*/
827 hammer2_trans_init(ip->pmp, 0);
829 error = hammer2_write_file(ip, uio, ioflag, seqcount);
830 if (uio->uio_segflg == UIO_NOCOPY)
831 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_BUFCACHE |
832 HAMMER2_TRANS_SIDEQ);
834 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ);
840 * Perform read operations on a file or symlink given an UNLOCKED
843 * The passed ip is not locked.
847 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
858 * WARNING! Assumes that the kernel interlocks size changes at the
861 hammer2_mtx_sh(&ip->lock);
862 hammer2_mtx_sh(&ip->truncate_lock);
863 size = ip->meta.size;
864 hammer2_mtx_unlock(&ip->lock);
866 while (uio->uio_resid > 0 && uio->uio_offset < size) {
873 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
878 error = cluster_readx(ip->vp, leof, lbase, lblksize,
879 B_NOTMETA | B_KVABIO,
884 if (uio->uio_segflg == UIO_NOCOPY) {
885 bp = getblk(ip->vp, lbase, lblksize,
886 GETBLK_BHEAVY | GETBLK_KVABIO, 0);
887 if (bp->b_flags & B_CACHE) {
890 if (bp->b_xio.xio_npages != 16)
891 kprintf("NPAGES BAD\n");
892 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
894 m = bp->b_xio.xio_pages[i];
895 if (m == NULL || m->valid == 0) {
896 kprintf("bp %016jx %016jx pg %d inv",
899 kprintf("m->object %p/%p", m->object, ip->vp->v_object);
905 kprintf("b_flags %08x, b_error %d\n", bp->b_flags, bp->b_error);
909 error = bread_kvabio(ip->vp, lbase, lblksize, &bp);
916 loff = (int)(uio->uio_offset - lbase);
918 if (n > uio->uio_resid)
920 if (n > size - uio->uio_offset)
921 n = (int)(size - uio->uio_offset);
922 bp->b_flags |= B_AGE;
923 uiomovebp(bp, (char *)bp->b_data + loff, n, uio);
926 hammer2_mtx_unlock(&ip->truncate_lock);
932 * Write to the file represented by the inode via the logical buffer cache.
933 * The inode may represent a regular file or a symlink.
935 * The inode must not be locked.
939 hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
940 int ioflag, int seqcount)
942 hammer2_key_t old_eof;
943 hammer2_key_t new_eof;
952 * WARNING! Assumes that the kernel interlocks size changes at the
955 hammer2_mtx_ex(&ip->lock);
956 hammer2_mtx_sh(&ip->truncate_lock);
957 if (ioflag & IO_APPEND)
958 uio->uio_offset = ip->meta.size;
959 old_eof = ip->meta.size;
962 * Extend the file if necessary. If the write fails at some point
963 * we will truncate it back down to cover as much as we were able
966 * Doing this now makes it easier to calculate buffer sizes in
973 if (uio->uio_offset + uio->uio_resid > old_eof) {
974 new_eof = uio->uio_offset + uio->uio_resid;
976 hammer2_extend_file(ip, new_eof);
977 kflags |= NOTE_EXTEND;
981 hammer2_mtx_unlock(&ip->lock);
986 while (uio->uio_resid > 0) {
995 * Don't allow the buffer build to blow out the buffer
998 if ((ioflag & IO_RECURSE) == 0)
999 bwillwrite(HAMMER2_PBUFSIZE);
1002 * This nominally tells us how much we can cluster and
1003 * what the logical buffer size needs to be. Currently
1004 * we don't try to cluster the write and just handle one
1007 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
1009 loff = (int)(uio->uio_offset - lbase);
1011 KKASSERT(lblksize <= 65536);
1014 * Calculate bytes to copy this transfer and whether the
1015 * copy completely covers the buffer or not.
1018 n = lblksize - loff;
1019 if (n > uio->uio_resid) {
1021 if (loff == lbase && uio->uio_offset + n == new_eof)
1029 if (lbase >= new_eof)
1035 if (uio->uio_segflg == UIO_NOCOPY) {
1037 * Issuing a write with the same data backing the
1038 * buffer. Instantiate the buffer to collect the
1039 * backing vm pages, then read-in any missing bits.
1041 * This case is used by vop_stdputpages().
1043 bp = getblk(ip->vp, lbase, lblksize,
1044 GETBLK_BHEAVY | GETBLK_KVABIO, 0);
1045 if ((bp->b_flags & B_CACHE) == 0) {
1047 error = bread_kvabio(ip->vp, lbase,
1050 } else if (trivial) {
1052 * Even though we are entirely overwriting the buffer
1053 * we may still have to zero it out to avoid a
1054 * mmap/write visibility issue.
1056 bp = getblk(ip->vp, lbase, lblksize,
1057 GETBLK_BHEAVY | GETBLK_KVABIO, 0);
1058 if ((bp->b_flags & B_CACHE) == 0)
1062 * Partial overwrite, read in any missing bits then
1063 * replace the portion being written.
1065 * (The strategy code will detect zero-fill physical
1066 * blocks for this case).
1068 error = bread_kvabio(ip->vp, lbase, lblksize, &bp);
1079 * Ok, copy the data in
1082 error = uiomovebp(bp, bp->b_data + loff, n, uio);
1083 kflags |= NOTE_WRITE;
1091 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
1092 * with IO_SYNC or IO_ASYNC set. These writes
1093 * must be handled as the pageout daemon expects.
1095 * NOTE! H2 relies on cluster_write() here because it
1096 * cannot preallocate disk blocks at the logical
1097 * level due to not knowing what the compression
1098 * size will be at this time.
1100 * We must use cluster_write() here and we depend
1101 * on the write-behind feature to flush buffers
1102 * appropriately. If we let the buffer daemons do
1103 * it the block allocations will be all over the
1106 if (ioflag & IO_SYNC) {
1108 } else if ((ioflag & IO_DIRECT) && endofblk) {
1110 } else if (ioflag & IO_ASYNC) {
1112 } else if (ip->vp->v_mount->mnt_flag & MNT_NOCLUSTERW) {
1116 bp->b_flags |= B_CLUSTEROK;
1117 cluster_write(bp, new_eof, lblksize, seqcount);
1119 bp->b_flags |= B_CLUSTEROK;
1126 * Cleanup. If we extended the file EOF but failed to write through
1127 * the entire write is a failure and we have to back-up.
1129 if (error && new_eof != old_eof) {
1130 hammer2_mtx_unlock(&ip->truncate_lock);
1131 hammer2_mtx_ex(&ip->lock);
1132 hammer2_mtx_ex(&ip->truncate_lock);
1133 hammer2_truncate_file(ip, old_eof);
1134 if (ip->flags & HAMMER2_INODE_MODIFIED)
1135 hammer2_inode_chain_sync(ip);
1136 hammer2_mtx_unlock(&ip->lock);
1137 } else if (modified) {
1138 struct vnode *vp = ip->vp;
1140 hammer2_mtx_ex(&ip->lock);
1141 hammer2_inode_modify(ip);
1142 if (uio->uio_segflg == UIO_NOCOPY) {
1143 if (vp->v_flag & VLASTWRITETS) {
1145 (unsigned long)vp->v_lastwrite_ts.tv_sec *
1147 vp->v_lastwrite_ts.tv_nsec / 1000;
1150 hammer2_update_time(&ip->meta.mtime);
1151 vclrflags(vp, VLASTWRITETS);
1156 * REMOVED - handled by hammer2_extend_file(). Do not issue
1157 * a chain_sync() outside of a sync/fsync except for DIRECTDATA
1160 * Under normal conditions we only issue a chain_sync if
1161 * the inode's DIRECTDATA state changed.
1163 if (ip->flags & HAMMER2_INODE_RESIZED)
1164 hammer2_inode_chain_sync(ip);
1166 hammer2_mtx_unlock(&ip->lock);
1167 hammer2_knote(ip->vp, kflags);
1169 hammer2_trans_assert_strategy(ip->pmp);
1170 hammer2_mtx_unlock(&ip->truncate_lock);
1176 * Truncate the size of a file. The inode must not be locked.
1178 * We must unconditionally set HAMMER2_INODE_RESIZED to properly
1179 * ensure that any on-media data beyond the new file EOF has been destroyed.
1181 * WARNING: nvtruncbuf() can only be safely called without the inode lock
1182 * held due to the way our write thread works. If the truncation
1183 * occurs in the middle of a buffer, nvtruncbuf() is responsible
1184 * for dirtying that buffer and zeroing out trailing bytes.
1186 * WARNING! Assumes that the kernel interlocks size changes at the
1189 * WARNING! Caller assumes responsibility for removing dead blocks
1190 * if INODE_RESIZED is set.
1194 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1196 hammer2_key_t lbase;
1199 hammer2_mtx_unlock(&ip->lock);
1201 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1202 nvtruncbuf(ip->vp, nsize,
1203 nblksize, (int)nsize & (nblksize - 1),
1206 hammer2_mtx_ex(&ip->lock);
1207 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1208 ip->osize = ip->meta.size;
1209 ip->meta.size = nsize;
1210 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1211 hammer2_inode_modify(ip);
1215 * Extend the size of a file. The inode must not be locked.
1217 * Even though the file size is changing, we do not have to set the
1218 * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES
1219 * boundary. When this occurs a hammer2_inode_chain_sync() is required
1220 * to prepare the inode cluster's indirect block table, otherwise
1221 * async execution of the strategy code will implode on us.
1223 * WARNING! Assumes that the kernel interlocks size changes at the
1226 * WARNING! Caller assumes responsibility for transitioning out
1227 * of the inode DIRECTDATA mode if INODE_RESIZED is set.
1231 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1233 hammer2_key_t lbase;
1234 hammer2_key_t osize;
1238 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1239 hammer2_inode_modify(ip);
1240 osize = ip->meta.size;
1242 ip->meta.size = nsize;
1245 * We must issue a chain_sync() when the DIRECTDATA state changes
1246 * to prevent confusion between the flush code and the in-memory
1247 * state. This is not perfect because we are doing it outside of
1248 * a sync/fsync operation, so it might not be fully synchronized
1249 * with the meta-data topology flush.
1251 if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES) {
1252 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1253 hammer2_inode_chain_sync(ip);
1256 hammer2_mtx_unlock(&ip->lock);
1258 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1259 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1265 hammer2_mtx_ex(&ip->lock);
1270 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1272 hammer2_xop_nresolve_t *xop;
1273 hammer2_inode_t *ip;
1274 hammer2_inode_t *dip;
1275 struct namecache *ncp;
1279 dip = VTOI(ap->a_dvp);
1280 xop = hammer2_xop_alloc(dip, 0);
1282 ncp = ap->a_nch->ncp;
1283 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1286 * Note: In DragonFly the kernel handles '.' and '..'.
1288 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1289 hammer2_xop_start(&xop->head, &hammer2_nresolve_desc);
1291 error = hammer2_xop_collect(&xop->head, 0);
1292 error = hammer2_error_to_errno(error);
1296 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1);
1298 hammer2_inode_unlock(dip);
1301 * Acquire the related vnode
1303 * NOTE: For error processing, only ENOENT resolves the namecache
1304 * entry to NULL, otherwise we just return the error and
1305 * leave the namecache unresolved.
1307 * NOTE: multiple hammer2_inode structures can be aliased to the
1308 * same chain element, for example for hardlinks. This
1309 * use case does not 'reattach' inode associations that
1310 * might already exist, but always allocates a new one.
1312 * WARNING: inode structure is locked exclusively via inode_get
1313 * but chain was locked shared. inode_unlock()
1314 * will handle it properly.
1317 vp = hammer2_igetv(ip, &error); /* error set to UNIX error */
1320 cache_setvp(ap->a_nch, vp);
1321 } else if (error == ENOENT) {
1322 cache_setvp(ap->a_nch, NULL);
1324 hammer2_inode_unlock(ip);
1327 * The vp should not be released until after we've disposed
1328 * of our locks, because it might cause vop_inactive() to
1335 cache_setvp(ap->a_nch, NULL);
1337 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1338 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1339 ("resolve error %d/%p ap %p\n",
1340 error, ap->a_nch->ncp->nc_vp, ap));
1347 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1349 hammer2_inode_t *dip;
1353 dip = VTOI(ap->a_dvp);
1354 inum = dip->meta.iparent;
1358 error = hammer2_vfs_vget(ap->a_dvp->v_mount, NULL,
1368 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1370 hammer2_inode_t *dip;
1371 hammer2_inode_t *nip;
1372 struct namecache *ncp;
1373 const uint8_t *name;
1378 dip = VTOI(ap->a_dvp);
1379 if (dip->pmp->ronly)
1381 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1384 ncp = ap->a_nch->ncp;
1385 name = ncp->nc_name;
1386 name_len = ncp->nc_nlen;
1388 /*hammer2_pfs_memory_wait(dip->pmp);*/
1389 hammer2_trans_init(dip->pmp, 0);
1391 inum = hammer2_trans_newinum(dip->pmp);
1394 * Create the actual inode as a hidden file in the iroot, then
1395 * create the directory entry. The creation of the actual inode
1396 * sets its nlinks to 1 which is the value we desire.
1398 * dip must be locked before nip to avoid deadlock.
1400 hammer2_inode_lock(dip, 0);
1401 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
1404 error = hammer2_error_to_errno(error);
1406 error = hammer2_dirent_create(dip, name, name_len,
1407 nip->meta.inum, nip->meta.type);
1408 /* returns UNIX error code */
1412 hammer2_inode_unlink_finisher(nip, 0);
1413 hammer2_inode_unlock(nip);
1419 * inode_depend() must occur before the igetv() because
1420 * the igetv() can temporarily release the inode lock.
1422 hammer2_inode_depend(dip, nip); /* before igetv */
1423 *ap->a_vpp = hammer2_igetv(nip, &error);
1424 hammer2_inode_unlock(nip);
1428 * Update dip's mtime
1430 * We can use a shared inode lock and allow the meta.mtime update
1431 * SMP race. hammer2_inode_modify() is MPSAFE w/a shared lock.
1436 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1437 hammer2_update_time(&mtime);
1438 hammer2_inode_modify(dip);
1439 dip->meta.mtime = mtime;
1440 /*hammer2_inode_unlock(dip);*/
1442 hammer2_inode_unlock(dip);
1444 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1447 cache_setunresolved(ap->a_nch);
1448 cache_setvp(ap->a_nch, *ap->a_vpp);
1449 hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1456 hammer2_vop_open(struct vop_open_args *ap)
1458 return vop_stdopen(ap);
1462 * hammer2_vop_advlock { vp, id, op, fl, flags }
1466 hammer2_vop_advlock(struct vop_advlock_args *ap)
1468 hammer2_inode_t *ip = VTOI(ap->a_vp);
1471 size = ip->meta.size;
1472 return (lf_advlock(ap, &ip->advlock, size));
1477 hammer2_vop_close(struct vop_close_args *ap)
1479 return vop_stdclose(ap);
1483 * hammer2_vop_nlink { nch, dvp, vp, cred }
1485 * Create a hardlink from (vp) to {dvp, nch}.
1489 hammer2_vop_nlink(struct vop_nlink_args *ap)
1491 hammer2_inode_t *tdip; /* target directory to create link in */
1492 hammer2_inode_t *ip; /* inode we are hardlinking to */
1493 struct namecache *ncp;
1494 const uint8_t *name;
1498 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1501 tdip = VTOI(ap->a_dvp);
1502 if (tdip->pmp->ronly)
1504 if (hammer2_vfs_enospace(tdip, 0, ap->a_cred) > 1)
1507 ncp = ap->a_nch->ncp;
1508 name = ncp->nc_name;
1509 name_len = ncp->nc_nlen;
1512 * ip represents the file being hardlinked. The file could be a
1513 * normal file or a hardlink target if it has already been hardlinked.
1514 * (with the new semantics, it will almost always be a hardlink
1517 * Bump nlinks and potentially also create or move the hardlink
1518 * target in the parent directory common to (ip) and (tdip). The
1519 * consolidation code can modify ip->cluster. The returned cluster
1522 ip = VTOI(ap->a_vp);
1523 KASSERT(ip->pmp, ("ip->pmp is NULL %p %p", ip, ip->pmp));
1524 /*hammer2_pfs_memory_wait(ip->pmp);*/
1525 hammer2_trans_init(ip->pmp, 0);
1528 * Target should be an indexed inode or there's no way we will ever
1529 * be able to find it!
1531 KKASSERT((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0);
1536 * Can return NULL and error == EXDEV if the common parent
1537 * crosses a directory with the xlink flag set.
1539 hammer2_inode_lock4(tdip, ip, NULL, NULL);
1542 * Create the directory entry and bump nlinks.
1545 error = hammer2_dirent_create(tdip, name, name_len,
1546 ip->meta.inum, ip->meta.type);
1547 hammer2_inode_modify(ip);
1552 * Update dip's mtime
1556 hammer2_update_time(&mtime);
1557 hammer2_inode_modify(tdip);
1558 tdip->meta.mtime = mtime;
1560 cache_setunresolved(ap->a_nch);
1561 cache_setvp(ap->a_nch, ap->a_vp);
1563 hammer2_inode_unlock(ip);
1564 hammer2_inode_unlock(tdip);
1566 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ);
1567 hammer2_knote(ap->a_vp, NOTE_LINK);
1568 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1574 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1576 * The operating system has already ensured that the directory entry
1577 * does not exist and done all appropriate namespace locking.
1581 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1583 hammer2_inode_t *dip;
1584 hammer2_inode_t *nip;
1585 struct namecache *ncp;
1586 const uint8_t *name;
1591 dip = VTOI(ap->a_dvp);
1592 if (dip->pmp->ronly)
1594 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1597 ncp = ap->a_nch->ncp;
1598 name = ncp->nc_name;
1599 name_len = ncp->nc_nlen;
1600 /*hammer2_pfs_memory_wait(dip->pmp);*/
1601 hammer2_trans_init(dip->pmp, 0);
1603 inum = hammer2_trans_newinum(dip->pmp);
1606 * Create the actual inode as a hidden file in the iroot, then
1607 * create the directory entry. The creation of the actual inode
1608 * sets its nlinks to 1 which is the value we desire.
1610 * dip must be locked before nip to avoid deadlock.
1612 hammer2_inode_lock(dip, 0);
1613 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
1617 error = hammer2_error_to_errno(error);
1619 error = hammer2_dirent_create(dip, name, name_len,
1620 nip->meta.inum, nip->meta.type);
1624 hammer2_inode_unlink_finisher(nip, 0);
1625 hammer2_inode_unlock(nip);
1630 hammer2_inode_depend(dip, nip); /* before igetv */
1631 *ap->a_vpp = hammer2_igetv(nip, &error);
1632 hammer2_inode_unlock(nip);
1636 * Update dip's mtime
1641 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1642 hammer2_update_time(&mtime);
1643 hammer2_inode_modify(dip);
1644 dip->meta.mtime = mtime;
1645 /*hammer2_inode_unlock(dip);*/
1647 hammer2_inode_unlock(dip);
1649 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1652 cache_setunresolved(ap->a_nch);
1653 cache_setvp(ap->a_nch, *ap->a_vpp);
1654 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1660 * Make a device node (typically a fifo)
1664 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1666 hammer2_inode_t *dip;
1667 hammer2_inode_t *nip;
1668 struct namecache *ncp;
1669 const uint8_t *name;
1674 dip = VTOI(ap->a_dvp);
1675 if (dip->pmp->ronly)
1677 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1680 ncp = ap->a_nch->ncp;
1681 name = ncp->nc_name;
1682 name_len = ncp->nc_nlen;
1683 /*hammer2_pfs_memory_wait(dip->pmp);*/
1684 hammer2_trans_init(dip->pmp, 0);
1687 * Create the device inode and then create the directory entry.
1689 * dip must be locked before nip to avoid deadlock.
1691 inum = hammer2_trans_newinum(dip->pmp);
1693 hammer2_inode_lock(dip, 0);
1694 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
1697 error = hammer2_dirent_create(dip, name, name_len,
1698 nip->meta.inum, nip->meta.type);
1702 hammer2_inode_unlink_finisher(nip, 0);
1703 hammer2_inode_unlock(nip);
1708 hammer2_inode_depend(dip, nip); /* before igetv */
1709 *ap->a_vpp = hammer2_igetv(nip, &error);
1710 hammer2_inode_unlock(nip);
1714 * Update dip's mtime
1719 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1720 hammer2_update_time(&mtime);
1721 hammer2_inode_modify(dip);
1722 dip->meta.mtime = mtime;
1723 /*hammer2_inode_unlock(dip);*/
1725 hammer2_inode_unlock(dip);
1727 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1730 cache_setunresolved(ap->a_nch);
1731 cache_setvp(ap->a_nch, *ap->a_vpp);
1732 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1738 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1742 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1744 hammer2_inode_t *dip;
1745 hammer2_inode_t *nip;
1746 struct namecache *ncp;
1747 const uint8_t *name;
1752 dip = VTOI(ap->a_dvp);
1753 if (dip->pmp->ronly)
1755 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1758 ncp = ap->a_nch->ncp;
1759 name = ncp->nc_name;
1760 name_len = ncp->nc_nlen;
1761 /*hammer2_pfs_memory_wait(dip->pmp);*/
1762 hammer2_trans_init(dip->pmp, 0);
1764 ap->a_vap->va_type = VLNK; /* enforce type */
1767 * Create the softlink as an inode and then create the directory
1770 * dip must be locked before nip to avoid deadlock.
1772 inum = hammer2_trans_newinum(dip->pmp);
1774 hammer2_inode_lock(dip, 0);
1775 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
1778 error = hammer2_dirent_create(dip, name, name_len,
1779 nip->meta.inum, nip->meta.type);
1783 hammer2_inode_unlink_finisher(nip, 0);
1784 hammer2_inode_unlock(nip);
1788 hammer2_inode_unlock(dip);
1789 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1792 hammer2_inode_depend(dip, nip); /* before igetv */
1793 *ap->a_vpp = hammer2_igetv(nip, &error);
1796 * Build the softlink (~like file data) and finalize the namecache.
1803 bytes = strlen(ap->a_target);
1805 hammer2_inode_unlock(nip);
1806 bzero(&auio, sizeof(auio));
1807 bzero(&aiov, sizeof(aiov));
1808 auio.uio_iov = &aiov;
1809 auio.uio_segflg = UIO_SYSSPACE;
1810 auio.uio_rw = UIO_WRITE;
1811 auio.uio_resid = bytes;
1812 auio.uio_iovcnt = 1;
1813 auio.uio_td = curthread;
1814 aiov.iov_base = ap->a_target;
1815 aiov.iov_len = bytes;
1816 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1817 /* XXX handle error */
1820 hammer2_inode_unlock(nip);
1824 * Update dip's mtime
1829 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1830 hammer2_update_time(&mtime);
1831 hammer2_inode_modify(dip);
1832 dip->meta.mtime = mtime;
1833 /*hammer2_inode_unlock(dip);*/
1835 hammer2_inode_unlock(dip);
1837 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1840 * Finalize namecache
1843 cache_setunresolved(ap->a_nch);
1844 cache_setvp(ap->a_nch, *ap->a_vpp);
1845 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1851 * hammer2_vop_nremove { nch, dvp, cred }
1855 hammer2_vop_nremove(struct vop_nremove_args *ap)
1857 hammer2_xop_unlink_t *xop;
1858 hammer2_inode_t *dip;
1859 hammer2_inode_t *ip;
1860 struct namecache *ncp;
1864 dip = VTOI(ap->a_dvp);
1865 if (dip->pmp->ronly)
1868 /* allow removals, except user to also bulkfree */
1869 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1873 ncp = ap->a_nch->ncp;
1875 if (hammer2_debug_inode && dip->meta.inum == hammer2_debug_inode) {
1876 kprintf("hammer2: attempt to delete inside debug inode: %s\n",
1878 while (hammer2_debug_inode &&
1879 dip->meta.inum == hammer2_debug_inode) {
1880 tsleep(&hammer2_debug_inode, 0, "h2debug", hz*5);
1884 /*hammer2_pfs_memory_wait(dip->pmp);*/
1885 hammer2_trans_init(dip->pmp, 0);
1886 hammer2_inode_lock(dip, 0);
1889 * The unlink XOP unlinks the path from the directory and
1890 * locates and returns the cluster associated with the real inode.
1891 * We have to handle nlinks here on the frontend.
1893 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1894 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1897 * The namecache entry is locked so nobody can use this namespace.
1898 * Calculate isopen to determine if this namespace has an open vp
1899 * associated with it and resolve the vp only if it does.
1901 * We try to avoid resolving the vnode if nobody has it open, but
1902 * note that the test is via this namespace only.
1904 isopen = cache_isopen(ap->a_nch);
1906 xop->dopermanent = 0;
1907 hammer2_xop_start(&xop->head, &hammer2_unlink_desc);
1910 * Collect the real inode and adjust nlinks, destroy the real
1911 * inode if nlinks transitions to 0 and it was the real inode
1912 * (else it has already been removed).
1914 error = hammer2_xop_collect(&xop->head, 0);
1915 error = hammer2_error_to_errno(error);
1918 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1);
1919 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1921 if (hammer2_debug_inode &&
1922 ip->meta.inum == hammer2_debug_inode) {
1923 kprintf("hammer2: attempt to delete debug "
1925 while (hammer2_debug_inode &&
1926 ip->meta.inum == hammer2_debug_inode) {
1927 tsleep(&hammer2_debug_inode, 0,
1931 hammer2_inode_unlink_finisher(ip, isopen);
1932 hammer2_inode_depend(dip, ip); /* after modified */
1933 hammer2_inode_unlock(ip);
1936 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1940 * Update dip's mtime
1945 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1946 hammer2_update_time(&mtime);
1947 hammer2_inode_modify(dip);
1948 dip->meta.mtime = mtime;
1949 /*hammer2_inode_unlock(dip);*/
1951 hammer2_inode_unlock(dip);
1953 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1955 cache_unlink(ap->a_nch);
1956 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1962 * hammer2_vop_nrmdir { nch, dvp, cred }
1966 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1968 hammer2_xop_unlink_t *xop;
1969 hammer2_inode_t *dip;
1970 hammer2_inode_t *ip;
1971 struct namecache *ncp;
1975 dip = VTOI(ap->a_dvp);
1976 if (dip->pmp->ronly)
1979 /* allow removals, except user to also bulkfree */
1980 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1984 /*hammer2_pfs_memory_wait(dip->pmp);*/
1985 hammer2_trans_init(dip->pmp, 0);
1986 hammer2_inode_lock(dip, 0);
1988 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1990 ncp = ap->a_nch->ncp;
1991 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1992 isopen = cache_isopen(ap->a_nch);
1994 xop->dopermanent = 0;
1995 hammer2_xop_start(&xop->head, &hammer2_unlink_desc);
1998 * Collect the real inode and adjust nlinks, destroy the real
1999 * inode if nlinks transitions to 0 and it was the real inode
2000 * (else it has already been removed).
2002 error = hammer2_xop_collect(&xop->head, 0);
2003 error = hammer2_error_to_errno(error);
2006 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1);
2007 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2009 hammer2_inode_unlink_finisher(ip, isopen);
2010 hammer2_inode_depend(dip, ip); /* after modified */
2011 hammer2_inode_unlock(ip);
2014 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2018 * Update dip's mtime
2023 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
2024 hammer2_update_time(&mtime);
2025 hammer2_inode_modify(dip);
2026 dip->meta.mtime = mtime;
2027 /*hammer2_inode_unlock(dip);*/
2029 hammer2_inode_unlock(dip);
2031 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
2033 cache_unlink(ap->a_nch);
2034 hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2040 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
2044 hammer2_vop_nrename(struct vop_nrename_args *ap)
2046 struct namecache *fncp;
2047 struct namecache *tncp;
2048 hammer2_inode_t *fdip; /* source directory */
2049 hammer2_inode_t *tdip; /* target directory */
2050 hammer2_inode_t *ip; /* file being renamed */
2051 hammer2_inode_t *tip; /* replaced target during rename or NULL */
2052 const uint8_t *fname;
2054 const uint8_t *tname;
2061 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
2063 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
2066 fdip = VTOI(ap->a_fdvp); /* source directory */
2067 tdip = VTOI(ap->a_tdvp); /* target directory */
2069 if (fdip->pmp->ronly)
2071 if (hammer2_vfs_enospace(fdip, 0, ap->a_cred) > 1)
2074 fncp = ap->a_fnch->ncp; /* entry name in source */
2075 fname = fncp->nc_name;
2076 fname_len = fncp->nc_nlen;
2078 tncp = ap->a_tnch->ncp; /* entry name in target */
2079 tname = tncp->nc_name;
2080 tname_len = tncp->nc_nlen;
2082 /*hammer2_pfs_memory_wait(tdip->pmp);*/
2083 hammer2_trans_init(tdip->pmp, 0);
2088 ip = VTOI(fncp->nc_vp);
2089 hammer2_inode_ref(ip); /* extra ref */
2092 * Lookup the target name to determine if a directory entry
2093 * is being overwritten. We only hold related inode locks
2094 * temporarily, the operating system is expected to protect
2095 * against rename races.
2097 tip = tncp->nc_vp ? VTOI(tncp->nc_vp) : NULL;
2099 hammer2_inode_ref(tip); /* extra ref */
2102 * Can return NULL and error == EXDEV if the common parent
2103 * crosses a directory with the xlink flag set.
2105 * For now try to avoid deadlocks with a simple pointer address
2106 * test. (tip) can be NULL.
2110 hammer2_inode_t *ip1 = fdip;
2111 hammer2_inode_t *ip2 = tdip;
2112 hammer2_inode_t *ip3 = ip;
2113 hammer2_inode_t *ip4 = tip; /* may be NULL */
2119 if (tip && ip > tip) {
2123 hammer2_inode_lock4(ip1, ip2, ip3, ip4);
2127 * Resolve the collision space for (tdip, tname, tname_len)
2129 * tdip must be held exclusively locked to prevent races since
2130 * multiple filenames can end up in the same collision space.
2133 hammer2_xop_scanlhc_t *sxop;
2134 hammer2_tid_t lhcbase;
2136 tlhc = hammer2_dirhash(tname, tname_len);
2138 sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
2140 hammer2_xop_start(&sxop->head, &hammer2_scanlhc_desc);
2141 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
2142 if (tlhc != sxop->head.cluster.focus->bref.key)
2146 error = hammer2_error_to_errno(error);
2147 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
2150 if (error != ENOENT)
2155 if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) {
2162 * Ready to go, issue the rename to the backend. Note that meta-data
2163 * updates to the related inodes occur separately from the rename
2166 * NOTE: While it is not necessary to update ip->meta.name*, doing
2167 * so aids catastrophic recovery and debugging.
2170 hammer2_xop_nrename_t *xop4;
2172 xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
2174 xop4->ip_key = ip->meta.name_key;
2175 hammer2_xop_setip2(&xop4->head, ip);
2176 hammer2_xop_setip3(&xop4->head, tdip);
2177 hammer2_xop_setname(&xop4->head, fname, fname_len);
2178 hammer2_xop_setname2(&xop4->head, tname, tname_len);
2179 hammer2_xop_start(&xop4->head, &hammer2_nrename_desc);
2181 error = hammer2_xop_collect(&xop4->head, 0);
2182 error = hammer2_error_to_errno(error);
2183 hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP);
2185 if (error == ENOENT)
2189 * Update inode meta-data.
2191 * WARNING! The in-memory inode (ip) structure does not
2192 * maintain a copy of the inode's filename buffer.
2195 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
2196 hammer2_inode_modify(ip);
2197 ip->meta.name_len = tname_len;
2198 ip->meta.name_key = tlhc;
2201 hammer2_inode_modify(ip);
2202 ip->meta.iparent = tdip->meta.inum;
2210 * If no error, the backend has replaced the target directory entry.
2211 * We must adjust nlinks on the original replace target if it exists.
2213 if (error == 0 && tip) {
2216 isopen = cache_isopen(ap->a_tnch);
2217 hammer2_inode_unlink_finisher(tip, isopen);
2221 * Update directory mtimes to represent the something changed.
2223 if (update_fdip || update_tdip) {
2226 hammer2_update_time(&mtime);
2228 hammer2_inode_modify(fdip);
2229 fdip->meta.mtime = mtime;
2232 hammer2_inode_modify(tdip);
2233 tdip->meta.mtime = mtime;
2237 hammer2_inode_unlock(tip);
2238 hammer2_inode_drop(tip);
2240 hammer2_inode_unlock(ip);
2241 hammer2_inode_unlock(tdip);
2242 hammer2_inode_unlock(fdip);
2243 hammer2_inode_drop(ip);
2244 hammer2_trans_done(tdip->pmp, HAMMER2_TRANS_SIDEQ);
2247 * Issue the namecache update after unlocking all the internal
2248 * hammer2 structures, otherwise we might deadlock.
2250 * WARNING! The target namespace must be updated atomically,
2251 * and we depend on cache_rename() to handle that for
2252 * us. Do not do a separate cache_unlink() because
2253 * that leaves a small window of opportunity for other
2254 * threads to allocate the target namespace before we
2255 * manage to complete our rename.
2257 * WARNING! cache_rename() (and cache_unlink()) will properly
2258 * set VREF_FINALIZE on any attached vnode. Do not
2259 * call cache_setunresolved() manually before-hand as
2260 * this will prevent the flag from being set later via
2261 * cache_rename(). If VREF_FINALIZE is not properly set
2262 * and the inode is no longer in the topology, related
2263 * chains can remain dirty indefinitely.
2265 if (error == 0 && tip) {
2266 /*cache_unlink(ap->a_tnch); see above */
2267 /*cache_setunresolved(ap->a_tnch); see above */
2270 cache_rename(ap->a_fnch, ap->a_tnch);
2271 hammer2_knote(ap->a_fdvp, NOTE_WRITE);
2272 hammer2_knote(ap->a_tdvp, NOTE_WRITE);
2273 hammer2_knote(fncp->nc_vp, NOTE_RENAME);
2280 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2284 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2286 hammer2_inode_t *ip;
2289 ip = VTOI(ap->a_vp);
2291 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2292 ap->a_fflag, ap->a_cred);
2298 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2305 case (MOUNTCTL_SET_EXPORT):
2306 mp = ap->a_head.a_ops->head.vv_mount;
2309 if (ap->a_ctllen != sizeof(struct export_args))
2312 rc = vfs_export(mp, &pmp->export,
2313 (const struct export_args *)ap->a_ctl);
2316 rc = vop_stdmountctl(ap);
2325 static void filt_hammer2detach(struct knote *kn);
2326 static int filt_hammer2read(struct knote *kn, long hint);
2327 static int filt_hammer2write(struct knote *kn, long hint);
2328 static int filt_hammer2vnode(struct knote *kn, long hint);
2330 static struct filterops hammer2read_filtops =
2331 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2332 NULL, filt_hammer2detach, filt_hammer2read };
2333 static struct filterops hammer2write_filtops =
2334 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2335 NULL, filt_hammer2detach, filt_hammer2write };
2336 static struct filterops hammer2vnode_filtops =
2337 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2338 NULL, filt_hammer2detach, filt_hammer2vnode };
2342 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
2344 struct vnode *vp = ap->a_vp;
2345 struct knote *kn = ap->a_kn;
2347 switch (kn->kn_filter) {
2349 kn->kn_fop = &hammer2read_filtops;
2352 kn->kn_fop = &hammer2write_filtops;
2355 kn->kn_fop = &hammer2vnode_filtops;
2358 return (EOPNOTSUPP);
2361 kn->kn_hook = (caddr_t)vp;
2363 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2369 filt_hammer2detach(struct knote *kn)
2371 struct vnode *vp = (void *)kn->kn_hook;
2373 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2377 filt_hammer2read(struct knote *kn, long hint)
2379 struct vnode *vp = (void *)kn->kn_hook;
2380 hammer2_inode_t *ip = VTOI(vp);
2383 if (hint == NOTE_REVOKE) {
2384 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2387 off = ip->meta.size - kn->kn_fp->f_offset;
2388 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2389 if (kn->kn_sfflags & NOTE_OLDAPI)
2391 return (kn->kn_data != 0);
2396 filt_hammer2write(struct knote *kn, long hint)
2398 if (hint == NOTE_REVOKE)
2399 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2405 filt_hammer2vnode(struct knote *kn, long hint)
2407 if (kn->kn_sfflags & hint)
2408 kn->kn_fflags |= hint;
2409 if (hint == NOTE_REVOKE) {
2410 kn->kn_flags |= (EV_EOF | EV_NODATA);
2413 return (kn->kn_fflags != 0);
2421 hammer2_vop_markatime(struct vop_markatime_args *ap)
2423 hammer2_inode_t *ip;
2436 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2440 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2442 error = hammer2_vop_kqfilter(ap);
2449 struct vop_ops hammer2_vnode_vops = {
2450 .vop_default = vop_defaultop,
2451 .vop_fsync = hammer2_vop_fsync,
2452 .vop_getpages = vop_stdgetpages,
2453 .vop_putpages = vop_stdputpages,
2454 .vop_access = hammer2_vop_access,
2455 .vop_advlock = hammer2_vop_advlock,
2456 .vop_close = hammer2_vop_close,
2457 .vop_nlink = hammer2_vop_nlink,
2458 .vop_ncreate = hammer2_vop_ncreate,
2459 .vop_nsymlink = hammer2_vop_nsymlink,
2460 .vop_nremove = hammer2_vop_nremove,
2461 .vop_nrmdir = hammer2_vop_nrmdir,
2462 .vop_nrename = hammer2_vop_nrename,
2463 .vop_getattr = hammer2_vop_getattr,
2464 .vop_setattr = hammer2_vop_setattr,
2465 .vop_readdir = hammer2_vop_readdir,
2466 .vop_readlink = hammer2_vop_readlink,
2467 .vop_read = hammer2_vop_read,
2468 .vop_write = hammer2_vop_write,
2469 .vop_open = hammer2_vop_open,
2470 .vop_inactive = hammer2_vop_inactive,
2471 .vop_reclaim = hammer2_vop_reclaim,
2472 .vop_nresolve = hammer2_vop_nresolve,
2473 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2474 .vop_nmkdir = hammer2_vop_nmkdir,
2475 .vop_nmknod = hammer2_vop_nmknod,
2476 .vop_ioctl = hammer2_vop_ioctl,
2477 .vop_mountctl = hammer2_vop_mountctl,
2478 .vop_bmap = hammer2_vop_bmap,
2479 .vop_strategy = hammer2_vop_strategy,
2480 .vop_kqfilter = hammer2_vop_kqfilter
2483 struct vop_ops hammer2_spec_vops = {
2484 .vop_default = vop_defaultop,
2485 .vop_fsync = hammer2_vop_fsync,
2486 .vop_read = vop_stdnoread,
2487 .vop_write = vop_stdnowrite,
2488 .vop_access = hammer2_vop_access,
2489 .vop_close = hammer2_vop_close,
2490 .vop_markatime = hammer2_vop_markatime,
2491 .vop_getattr = hammer2_vop_getattr,
2492 .vop_inactive = hammer2_vop_inactive,
2493 .vop_reclaim = hammer2_vop_reclaim,
2494 .vop_setattr = hammer2_vop_setattr
2497 struct vop_ops hammer2_fifo_vops = {
2498 .vop_default = fifo_vnoperate,
2499 .vop_fsync = hammer2_vop_fsync,
2501 .vop_read = hammer2_vop_fiforead,
2502 .vop_write = hammer2_vop_fifowrite,
2504 .vop_access = hammer2_vop_access,
2506 .vop_close = hammer2_vop_fifoclose,
2508 .vop_markatime = hammer2_vop_markatime,
2509 .vop_getattr = hammer2_vop_getattr,
2510 .vop_inactive = hammer2_vop_inactive,
2511 .vop_reclaim = hammer2_vop_reclaim,
2512 .vop_setattr = hammer2_vop_setattr,
2513 .vop_kqfilter = hammer2_vop_fifokqfilter