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/mount.h>
50 #include <sys/vnode.h>
51 #include <sys/mountctl.h>
52 #include <sys/dirent.h>
54 #include <sys/objcache.h>
55 #include <sys/event.h>
57 #include <vfs/fifofs/fifo.h>
61 static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio,
63 static int hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
64 int ioflag, int seqcount);
65 static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize);
66 static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize);
69 * Last reference to a vnode is going away but it is still cached.
73 hammer2_vop_inactive(struct vop_inactive_args *ap)
90 * Aquire the inode lock to interlock against vp updates via
91 * the inode path and file deletions and such (which can be
92 * namespace-only operations that might not hold the vnode).
94 hammer2_inode_lock(ip, 0);
95 if (ip->flags & HAMMER2_INODE_ISUNLINKED) {
99 * If the inode has been unlinked we can throw away all
100 * buffers (dirty or not) and clean the file out.
102 * Because vrecycle() calls are not guaranteed, try to
103 * dispose of the inode as much as possible right here.
105 nblksize = hammer2_calc_logical(ip, 0, NULL, NULL);
106 nvtruncbuf(vp, 0, nblksize, 0, 0);
109 * Delete the file on-media.
111 if ((ip->flags & HAMMER2_INODE_DELETING) == 0) {
112 atomic_set_int(&ip->flags, HAMMER2_INODE_DELETING);
113 hammer2_inode_delayed_sideq(ip);
115 hammer2_inode_unlock(ip);
118 * Recycle immediately if possible
122 hammer2_inode_unlock(ip);
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)
144 * NOTE! We do not attempt to flush chains here, flushing is
145 * really fragile and could also deadlock.
150 * The inode lock is required to disconnect it.
152 hammer2_inode_lock(ip, 0);
157 * Delete the file on-media. This should have been handled by the
158 * inactivation. The operation is likely still queued on the inode
159 * though so only complain if the stars don't align.
161 if ((ip->flags & (HAMMER2_INODE_ISUNLINKED | HAMMER2_INODE_DELETING)) ==
162 HAMMER2_INODE_ISUNLINKED)
164 atomic_set_int(&ip->flags, HAMMER2_INODE_DELETING);
165 hammer2_inode_delayed_sideq(ip);
166 kprintf("hammer2: vp=%p ip=%p unlinked but not disposed\n",
169 hammer2_inode_unlock(ip);
172 * Modified inodes will already be on SIDEQ or SYNCQ, no further
175 * We cannot safely synchronize the inode from inside the reclaim
176 * due to potentially deep locks held as-of when the reclaim occurs.
177 * Interactions and potential deadlocks abound. We also can't do it
178 * here without desynchronizing from the related directory entrie(s).
180 hammer2_inode_drop(ip); /* vp ref */
183 * XXX handle background sync when ip dirty, kernel will no longer
184 * notify us regarding this inode because there is no longer a
185 * vnode attached to it.
192 * Currently this function synchronizes the front-end inode state to the
193 * backend chain topology, then flushes the inode's chain and sub-topology
194 * to backend media. This function does not flush the root topology down to
199 hammer2_vop_fsync(struct vop_fsync_args *ap)
210 hammer2_trans_init(ip->pmp, 0);
213 * Flush dirty buffers in the file's logical buffer cache.
214 * It is best to wait for the strategy code to commit the
215 * buffers to the device's backing buffer cache before
216 * then trying to flush the inode.
218 * This should be quick, but certain inode modifications cached
219 * entirely in the hammer2_inode structure may not trigger a
220 * buffer read until the flush so the fsync can wind up also
221 * doing scattered reads.
223 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
224 bio_track_wait(&vp->v_track_write, 0, 0);
227 * Flush any inode changes
229 hammer2_inode_lock(ip, 0);
230 if (ip->flags & (HAMMER2_INODE_RESIZED|HAMMER2_INODE_MODIFIED))
231 error1 = hammer2_inode_chain_sync(ip);
234 * Flush dirty chains related to the inode.
236 * NOTE! We are not in a flush transaction. The inode remains on
237 * the sideq so the filesystem syncer can synchronize it to
240 error2 = hammer2_inode_chain_flush(ip, HAMMER2_XOP_INODE_STOP);
245 * We may be able to clear the vnode dirty flag.
247 if ((ip->flags & (HAMMER2_INODE_MODIFIED |
248 HAMMER2_INODE_RESIZED |
249 HAMMER2_INODE_DIRTYDATA)) == 0 &&
250 RB_EMPTY(&vp->v_rbdirty_tree) &&
251 !bio_track_active(&vp->v_track_write)) {
254 hammer2_inode_unlock(ip);
255 hammer2_trans_done(ip->pmp, 0);
261 * No lock needed, just handle ip->update
265 hammer2_vop_access(struct vop_access_args *ap)
267 hammer2_inode_t *ip = VTOI(ap->a_vp);
276 update = spin_access_start(&ip->cluster_spin);
278 /*hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);*/
279 uid = hammer2_to_unix_xid(&ip->meta.uid);
280 gid = hammer2_to_unix_xid(&ip->meta.gid);
281 mode = ip->meta.mode;
282 uflags = ip->meta.uflags;
283 /*hammer2_inode_unlock(ip);*/
285 if (__predict_false(spin_access_end(&ip->cluster_spin, update)))
288 error = vop_helper_access(ap, uid, gid, mode, uflags);
295 hammer2_vop_getattr(struct vop_getattr_args *ap)
310 update = spin_access_start(&ip->cluster_spin);
312 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
313 vap->va_fileid = ip->meta.inum;
314 vap->va_mode = ip->meta.mode;
315 vap->va_nlink = ip->meta.nlinks;
316 vap->va_uid = hammer2_to_unix_xid(&ip->meta.uid);
317 vap->va_gid = hammer2_to_unix_xid(&ip->meta.gid);
320 vap->va_size = ip->meta.size; /* protected by shared lock */
321 vap->va_blocksize = HAMMER2_PBUFSIZE;
322 vap->va_flags = ip->meta.uflags;
323 hammer2_time_to_timespec(ip->meta.ctime, &vap->va_ctime);
324 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_mtime);
325 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_atime);
328 if (ip->meta.type == HAMMER2_OBJTYPE_DIRECTORY) {
330 * Can't really calculate directory use sans the files under
331 * it, just assume one block for now.
333 vap->va_bytes += HAMMER2_INODE_BYTES;
335 vap->va_bytes = hammer2_inode_data_count(ip);
337 vap->va_type = hammer2_get_vtype(ip->meta.type);
339 vap->va_uid_uuid = ip->meta.uid;
340 vap->va_gid_uuid = ip->meta.gid;
341 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
344 if (__predict_false(spin_access_end(&ip->cluster_spin, update)))
352 hammer2_vop_getattr_lite(struct vop_getattr_lite_args *ap)
357 struct vattr_lite *lvap;
367 update = spin_access_start(&ip->cluster_spin);
370 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
371 vap->va_fileid = ip->meta.inum;
373 lvap->va_mode = ip->meta.mode;
374 lvap->va_nlink = ip->meta.nlinks;
375 lvap->va_uid = hammer2_to_unix_xid(&ip->meta.uid);
376 lvap->va_gid = hammer2_to_unix_xid(&ip->meta.gid);
381 lvap->va_size = ip->meta.size;
383 vap->va_blocksize = HAMMER2_PBUFSIZE;
385 lvap->va_flags = ip->meta.uflags;
386 lvap->va_type = hammer2_get_vtype(ip->meta.type);
389 vap->va_uid_uuid = ip->meta.uid;
390 vap->va_gid_uuid = ip->meta.gid;
391 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
395 if (__predict_false(spin_access_end(&ip->cluster_spin, update)))
403 hammer2_vop_setattr(struct vop_setattr_args *ap)
414 hammer2_update_time(&ctime);
422 * Normally disallow setattr if there is no space, unless we
423 * are in emergency mode (might be needed to chflags -R noschg
424 * files prior to removal).
426 if ((ip->pmp->flags & HAMMER2_PMPF_EMERG) == 0 &&
427 hammer2_vfs_enospace(ip, 0, ap->a_cred) > 1) {
431 hammer2_trans_init(ip->pmp, 0);
432 hammer2_inode_lock(ip, 0);
435 if (vap->va_flags != VNOVAL) {
438 flags = ip->meta.uflags;
439 error = vop_helper_setattr_flags(&flags, vap->va_flags,
440 hammer2_to_unix_xid(&ip->meta.uid),
443 if (ip->meta.uflags != flags) {
444 hammer2_inode_modify(ip);
445 hammer2_spin_lock_update(&ip->cluster_spin);
446 ip->meta.uflags = flags;
447 ip->meta.ctime = ctime;
448 hammer2_spin_unlock_update(&ip->cluster_spin);
449 kflags |= NOTE_ATTRIB;
451 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
458 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
462 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
463 mode_t cur_mode = ip->meta.mode;
464 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
465 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
469 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
471 &cur_uid, &cur_gid, &cur_mode);
473 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
474 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
475 if (bcmp(&uuid_uid, &ip->meta.uid, sizeof(uuid_uid)) ||
476 bcmp(&uuid_gid, &ip->meta.gid, sizeof(uuid_gid)) ||
477 ip->meta.mode != cur_mode
479 hammer2_inode_modify(ip);
480 hammer2_spin_lock_update(&ip->cluster_spin);
481 ip->meta.uid = uuid_uid;
482 ip->meta.gid = uuid_gid;
483 ip->meta.mode = cur_mode;
484 ip->meta.ctime = ctime;
485 hammer2_spin_unlock_update(&ip->cluster_spin);
487 kflags |= NOTE_ATTRIB;
494 if (vap->va_size != VNOVAL && ip->meta.size != vap->va_size) {
497 if (vap->va_size == ip->meta.size)
499 if (vap->va_size < ip->meta.size) {
500 hammer2_mtx_ex(&ip->truncate_lock);
501 hammer2_truncate_file(ip, vap->va_size);
502 hammer2_mtx_unlock(&ip->truncate_lock);
503 kflags |= NOTE_WRITE;
505 hammer2_extend_file(ip, vap->va_size);
506 kflags |= NOTE_WRITE | NOTE_EXTEND;
508 hammer2_inode_modify(ip);
509 ip->meta.mtime = ctime;
510 vclrflags(vp, VLASTWRITETS);
518 /* atime not supported */
519 if (vap->va_atime.tv_sec != VNOVAL) {
520 hammer2_inode_modify(ip);
521 ip->meta.atime = hammer2_timespec_to_time(&vap->va_atime);
522 kflags |= NOTE_ATTRIB;
525 if (vap->va_mode != (mode_t)VNOVAL) {
526 mode_t cur_mode = ip->meta.mode;
527 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
528 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
530 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
531 cur_uid, cur_gid, &cur_mode);
533 hammer2_inode_modify(ip);
534 hammer2_spin_lock_update(&ip->cluster_spin);
535 ip->meta.mode = cur_mode;
536 ip->meta.ctime = ctime;
537 hammer2_spin_unlock_update(&ip->cluster_spin);
538 kflags |= NOTE_ATTRIB;
542 if (vap->va_mtime.tv_sec != VNOVAL) {
543 hammer2_inode_modify(ip);
544 ip->meta.mtime = hammer2_timespec_to_time(&vap->va_mtime);
545 kflags |= NOTE_ATTRIB;
546 vclrflags(vp, VLASTWRITETS);
551 * If a truncation occurred we must call chain_sync() now in order
552 * to trim the related data chains, otherwise a later expansion can
555 * If an extend occured that changed the DIRECTDATA state, we must
556 * call inode_chain_sync now in order to prepare the inode's indirect
559 * WARNING! This means we are making an adjustment to the inode's
560 * chain outside of sync/fsync, and not just to inode->meta, which
561 * may result in some consistency issues if a crash were to occur
562 * at just the wrong time.
564 if (ip->flags & HAMMER2_INODE_RESIZED)
565 hammer2_inode_chain_sync(ip);
570 hammer2_inode_unlock(ip);
571 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ);
572 hammer2_knote(ip->vp, kflags);
579 hammer2_vop_readdir(struct vop_readdir_args *ap)
581 hammer2_xop_readdir_t *xop;
582 hammer2_blockref_t bref;
597 saveoff = uio->uio_offset;
602 * Setup cookies directory entry cookies if requested
604 if (ap->a_ncookies) {
605 ncookies = uio->uio_resid / 16 + 1;
608 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
615 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
618 * Handle artificial entries. To ensure that only positive 64 bit
619 * quantities are returned to userland we always strip off bit 63.
620 * The hash code is designed such that codes 0x0000-0x7FFF are not
621 * used, allowing us to use these codes for articial entries.
623 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
624 * allow '..' to cross the mount point into (e.g.) the super-root.
627 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
628 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
632 cookies[cookie_index] = saveoff;
635 if (cookie_index == ncookies)
640 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
641 if (ip != ip->pmp->iroot)
642 inum = ip->meta.iparent & HAMMER2_DIRHASH_USERMSK;
643 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
647 cookies[cookie_index] = saveoff;
650 if (cookie_index == ncookies)
654 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
655 if (hammer2_debug & 0x0020)
656 kprintf("readdir: lkey %016jx\n", lkey);
660 xop = hammer2_xop_alloc(ip, 0);
662 hammer2_xop_start(&xop->head, &hammer2_readdir_desc);
665 const hammer2_inode_data_t *ripdata;
669 error = hammer2_xop_collect(&xop->head, 0);
670 error = hammer2_error_to_errno(error);
674 if (cookie_index == ncookies)
676 if (hammer2_debug & 0x0020)
677 kprintf("cluster chain %p %p\n",
678 xop->head.cluster.focus,
679 (xop->head.cluster.focus ?
680 xop->head.cluster.focus->data : (void *)-1));
681 hammer2_cluster_bref(&xop->head.cluster, &bref);
683 if (bref.type == HAMMER2_BREF_TYPE_INODE) {
684 ripdata = &hammer2_xop_gdata(&xop->head)->ipdata;
685 dtype = hammer2_get_dtype(ripdata->meta.type);
686 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
687 r = vop_write_dirent(&error, uio,
689 HAMMER2_DIRHASH_USERMSK,
691 ripdata->meta.name_len,
693 hammer2_xop_pdata(&xop->head);
697 cookies[cookie_index] = saveoff;
699 } else if (bref.type == HAMMER2_BREF_TYPE_DIRENT) {
702 dtype = hammer2_get_dtype(bref.embed.dirent.type);
703 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
704 namlen = bref.embed.dirent.namlen;
705 if (namlen <= sizeof(bref.check.buf)) {
706 dname = bref.check.buf;
708 dname = hammer2_xop_gdata(&xop->head)->buf;
710 r = vop_write_dirent(&error, uio,
711 bref.embed.dirent.inum, dtype,
713 if (namlen > sizeof(bref.check.buf))
714 hammer2_xop_pdata(&xop->head);
718 cookies[cookie_index] = saveoff;
721 /* XXX chain error */
722 kprintf("bad chain type readdir %d\n", bref.type);
725 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
726 if (error == ENOENT) {
729 saveoff = (hammer2_key_t)-1;
731 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
734 hammer2_inode_unlock(ip);
736 *ap->a_eofflag = eofflag;
737 if (hammer2_debug & 0x0020)
738 kprintf("readdir: done at %016jx\n", saveoff);
739 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
740 if (error && cookie_index == 0) {
742 kfree(cookies, M_TEMP);
744 *ap->a_cookies = NULL;
748 *ap->a_ncookies = cookie_index;
749 *ap->a_cookies = cookies;
756 * hammer2_vop_readlink { vp, uio, cred }
760 hammer2_vop_readlink(struct vop_readlink_args *ap)
767 if (vp->v_type != VLNK)
771 error = hammer2_read_file(ip, ap->a_uio, 0);
777 hammer2_vop_read(struct vop_read_args *ap)
786 * Read operations supported on this vnode?
789 if (vp->v_type == VDIR)
791 if (vp->v_type != VREG)
801 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
803 error = hammer2_read_file(ip, uio, seqcount);
809 hammer2_vop_write(struct vop_write_args *ap)
820 * Read operations supported on this vnode?
823 if (vp->v_type != VREG)
830 ioflag = ap->a_ioflag;
833 if (ip->pmp->ronly || (ip->pmp->flags & HAMMER2_PMPF_EMERG))
835 switch (hammer2_vfs_enospace(ip, uio->uio_resid, ap->a_cred)) {
839 ioflag |= IO_DIRECT; /* semi-synchronous */
845 seqcount = ioflag >> IO_SEQSHIFT;
848 * Check resource limit
850 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
851 uio->uio_offset + uio->uio_resid >
852 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
853 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
858 * The transaction interlocks against flush initiations
859 * (note: but will run concurrently with the actual flush).
861 * To avoid deadlocking against the VM system, we must flag any
862 * transaction related to the buffer cache or other direct
863 * VM page manipulation.
865 if (uio->uio_segflg == UIO_NOCOPY) {
866 hammer2_trans_init(ip->pmp, HAMMER2_TRANS_BUFCACHE);
868 hammer2_trans_init(ip->pmp, 0);
870 error = hammer2_write_file(ip, uio, ioflag, seqcount);
871 if (uio->uio_segflg == UIO_NOCOPY)
872 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_BUFCACHE |
873 HAMMER2_TRANS_SIDEQ);
875 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ);
881 * Perform read operations on a file or symlink given an UNLOCKED
884 * The passed ip is not locked.
888 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
899 * WARNING! Assumes that the kernel interlocks size changes at the
902 hammer2_mtx_sh(&ip->lock);
903 hammer2_mtx_sh(&ip->truncate_lock);
904 size = ip->meta.size;
905 hammer2_mtx_unlock(&ip->lock);
907 while (uio->uio_resid > 0 && uio->uio_offset < size) {
914 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
919 error = cluster_readx(ip->vp, leof, lbase, lblksize,
920 B_NOTMETA | B_KVABIO,
925 if (uio->uio_segflg == UIO_NOCOPY) {
926 bp = getblk(ip->vp, lbase, lblksize,
927 GETBLK_BHEAVY | GETBLK_KVABIO, 0);
928 if (bp->b_flags & B_CACHE) {
931 if (bp->b_xio.xio_npages != 16)
932 kprintf("NPAGES BAD\n");
933 for (i = 0; i < bp->b_xio.xio_npages; ++i) {
935 m = bp->b_xio.xio_pages[i];
936 if (m == NULL || m->valid == 0) {
937 kprintf("bp %016jx %016jx pg %d inv",
940 kprintf("m->object %p/%p", m->object, ip->vp->v_object);
946 kprintf("b_flags %08x, b_error %d\n", bp->b_flags, bp->b_error);
950 error = bread_kvabio(ip->vp, lbase, lblksize, &bp);
957 loff = (int)(uio->uio_offset - lbase);
959 if (n > uio->uio_resid)
961 if (n > size - uio->uio_offset)
962 n = (int)(size - uio->uio_offset);
963 bp->b_flags |= B_AGE;
964 uiomovebp(bp, bp->b_data + loff, n, uio);
967 hammer2_mtx_unlock(&ip->truncate_lock);
973 * Write to the file represented by the inode via the logical buffer cache.
974 * The inode may represent a regular file or a symlink.
976 * The inode must not be locked.
980 hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
981 int ioflag, int seqcount)
983 hammer2_key_t old_eof;
984 hammer2_key_t new_eof;
993 * WARNING! Assumes that the kernel interlocks size changes at the
996 hammer2_mtx_ex(&ip->lock);
997 hammer2_mtx_sh(&ip->truncate_lock);
998 if (ioflag & IO_APPEND)
999 uio->uio_offset = ip->meta.size;
1000 old_eof = ip->meta.size;
1003 * Extend the file if necessary. If the write fails at some point
1004 * we will truncate it back down to cover as much as we were able
1007 * Doing this now makes it easier to calculate buffer sizes in
1014 if (uio->uio_offset + uio->uio_resid > old_eof) {
1015 new_eof = uio->uio_offset + uio->uio_resid;
1017 hammer2_extend_file(ip, new_eof);
1018 kflags |= NOTE_EXTEND;
1022 hammer2_mtx_unlock(&ip->lock);
1027 while (uio->uio_resid > 0) {
1028 hammer2_key_t lbase;
1036 * Don't allow the buffer build to blow out the buffer
1039 if ((ioflag & IO_RECURSE) == 0)
1040 bwillwrite(HAMMER2_PBUFSIZE);
1043 * This nominally tells us how much we can cluster and
1044 * what the logical buffer size needs to be. Currently
1045 * we don't try to cluster the write and just handle one
1048 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
1050 loff = (int)(uio->uio_offset - lbase);
1052 KKASSERT(lblksize <= MAXBSIZE);
1055 * Calculate bytes to copy this transfer and whether the
1056 * copy completely covers the buffer or not.
1059 n = lblksize - loff;
1060 if (n > uio->uio_resid) {
1062 if (loff == lbase && uio->uio_offset + n == new_eof)
1070 if (lbase >= new_eof)
1076 if (uio->uio_segflg == UIO_NOCOPY) {
1078 * Issuing a write with the same data backing the
1079 * buffer. Instantiate the buffer to collect the
1080 * backing vm pages, then read-in any missing bits.
1082 * This case is used by vop_stdputpages().
1084 bp = getblk(ip->vp, lbase, lblksize,
1085 GETBLK_BHEAVY | GETBLK_KVABIO, 0);
1086 if ((bp->b_flags & B_CACHE) == 0) {
1088 error = bread_kvabio(ip->vp, lbase,
1091 } else if (trivial) {
1093 * Even though we are entirely overwriting the buffer
1094 * we may still have to zero it out to avoid a
1095 * mmap/write visibility issue.
1097 bp = getblk(ip->vp, lbase, lblksize,
1098 GETBLK_BHEAVY | GETBLK_KVABIO, 0);
1099 if ((bp->b_flags & B_CACHE) == 0)
1103 * Partial overwrite, read in any missing bits then
1104 * replace the portion being written.
1106 * (The strategy code will detect zero-fill physical
1107 * blocks for this case).
1109 error = bread_kvabio(ip->vp, lbase, lblksize, &bp);
1120 * Ok, copy the data in
1123 error = uiomovebp(bp, bp->b_data + loff, n, uio);
1124 kflags |= NOTE_WRITE;
1132 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
1133 * with IO_SYNC or IO_ASYNC set. These writes
1134 * must be handled as the pageout daemon expects.
1136 * NOTE! H2 relies on cluster_write() here because it
1137 * cannot preallocate disk blocks at the logical
1138 * level due to not knowing what the compression
1139 * size will be at this time.
1141 * We must use cluster_write() here and we depend
1142 * on the write-behind feature to flush buffers
1143 * appropriately. If we let the buffer daemons do
1144 * it the block allocations will be all over the
1147 if (ioflag & IO_SYNC) {
1149 } else if ((ioflag & IO_DIRECT) && endofblk) {
1151 } else if (ioflag & IO_ASYNC) {
1153 } else if (ip->vp->v_mount->mnt_flag & MNT_NOCLUSTERW) {
1157 bp->b_flags |= B_CLUSTEROK;
1158 cluster_write(bp, new_eof, lblksize, seqcount);
1160 bp->b_flags |= B_CLUSTEROK;
1167 * Cleanup. If we extended the file EOF but failed to write through
1168 * the entire write is a failure and we have to back-up.
1170 if (error && new_eof != old_eof) {
1171 hammer2_mtx_unlock(&ip->truncate_lock);
1172 hammer2_mtx_ex(&ip->lock); /* note lock order */
1173 hammer2_mtx_ex(&ip->truncate_lock); /* note lock order */
1174 hammer2_truncate_file(ip, old_eof);
1175 if (ip->flags & HAMMER2_INODE_MODIFIED)
1176 hammer2_inode_chain_sync(ip);
1177 hammer2_mtx_unlock(&ip->lock);
1178 } else if (modified) {
1179 struct vnode *vp = ip->vp;
1181 hammer2_mtx_ex(&ip->lock);
1182 hammer2_inode_modify(ip);
1183 if (uio->uio_segflg == UIO_NOCOPY) {
1184 if (vp->v_flag & VLASTWRITETS) {
1186 (unsigned long)vp->v_lastwrite_ts.tv_sec *
1188 vp->v_lastwrite_ts.tv_nsec / 1000;
1191 hammer2_update_time(&ip->meta.mtime);
1192 vclrflags(vp, VLASTWRITETS);
1197 * REMOVED - handled by hammer2_extend_file(). Do not issue
1198 * a chain_sync() outside of a sync/fsync except for DIRECTDATA
1201 * Under normal conditions we only issue a chain_sync if
1202 * the inode's DIRECTDATA state changed.
1204 if (ip->flags & HAMMER2_INODE_RESIZED)
1205 hammer2_inode_chain_sync(ip);
1207 hammer2_mtx_unlock(&ip->lock);
1208 hammer2_knote(ip->vp, kflags);
1210 hammer2_trans_assert_strategy(ip->pmp);
1211 hammer2_mtx_unlock(&ip->truncate_lock);
1217 * Truncate the size of a file. The inode must be locked.
1219 * We must unconditionally set HAMMER2_INODE_RESIZED to properly
1220 * ensure that any on-media data beyond the new file EOF has been destroyed.
1222 * WARNING: nvtruncbuf() can only be safely called without the inode lock
1223 * held due to the way our write thread works. If the truncation
1224 * occurs in the middle of a buffer, nvtruncbuf() is responsible
1225 * for dirtying that buffer and zeroing out trailing bytes.
1227 * WARNING! Assumes that the kernel interlocks size changes at the
1230 * WARNING! Caller assumes responsibility for removing dead blocks
1231 * if INODE_RESIZED is set.
1235 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1239 hammer2_mtx_unlock(&ip->lock);
1241 nblksize = hammer2_calc_logical(ip, 0, NULL, NULL);
1242 nvtruncbuf(ip->vp, nsize,
1243 nblksize, (int)nsize & (nblksize - 1),
1246 hammer2_mtx_ex(&ip->lock);
1247 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1248 ip->osize = ip->meta.size;
1249 ip->meta.size = nsize;
1250 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1251 hammer2_inode_modify(ip);
1255 * Extend the size of a file. The inode must be locked.
1257 * Even though the file size is changing, we do not have to set the
1258 * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES
1259 * boundary. When this occurs a hammer2_inode_chain_sync() is required
1260 * to prepare the inode cluster's indirect block table, otherwise
1261 * async execution of the strategy code will implode on us.
1263 * WARNING! Assumes that the kernel interlocks size changes at the
1266 * WARNING! Caller assumes responsibility for transitioning out
1267 * of the inode DIRECTDATA mode if INODE_RESIZED is set.
1271 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1273 hammer2_key_t osize;
1278 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1279 hammer2_inode_modify(ip);
1280 osize = ip->meta.size;
1282 ip->meta.size = nsize;
1285 * We must issue a chain_sync() when the DIRECTDATA state changes
1286 * to prevent confusion between the flush code and the in-memory
1287 * state. This is not perfect because we are doing it outside of
1288 * a sync/fsync operation, so it might not be fully synchronized
1289 * with the meta-data topology flush.
1291 * We must retain and re-dirty the buffer cache buffer containing
1292 * the direct data so it can be written to a real block. It should
1293 * not be possible for a bread error to occur since the original data
1294 * is extracted from the inode structure directly.
1296 if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES) {
1300 oblksize = hammer2_calc_logical(ip, 0, NULL, NULL);
1301 error = bread_kvabio(ip->vp, 0, oblksize, &bp);
1302 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1303 hammer2_inode_chain_sync(ip);
1311 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1312 hammer2_inode_chain_sync(ip);
1315 hammer2_mtx_unlock(&ip->lock);
1317 oblksize = hammer2_calc_logical(ip, 0, NULL, NULL);
1318 nblksize = hammer2_calc_logical(ip, 0, NULL, NULL);
1324 hammer2_mtx_ex(&ip->lock);
1329 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1331 hammer2_xop_nresolve_t *xop;
1332 hammer2_inode_t *ip;
1333 hammer2_inode_t *dip;
1334 struct namecache *ncp;
1338 dip = VTOI(ap->a_dvp);
1339 xop = hammer2_xop_alloc(dip, 0);
1341 ncp = ap->a_nch->ncp;
1342 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1345 * Note: In DragonFly the kernel handles '.' and '..'.
1347 hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);
1348 hammer2_xop_start(&xop->head, &hammer2_nresolve_desc);
1350 error = hammer2_xop_collect(&xop->head, 0);
1351 error = hammer2_error_to_errno(error);
1355 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1);
1357 hammer2_inode_unlock(dip);
1360 * Acquire the related vnode
1362 * NOTE: For error processing, only ENOENT resolves the namecache
1363 * entry to NULL, otherwise we just return the error and
1364 * leave the namecache unresolved.
1366 * WARNING: inode structure is locked exclusively via inode_get
1367 * but chain was locked shared. inode_unlock()
1368 * will handle it properly.
1371 vp = hammer2_igetv(ip, &error); /* error set to UNIX error */
1374 cache_setvp(ap->a_nch, vp);
1375 } else if (error == ENOENT) {
1376 cache_setvp(ap->a_nch, NULL);
1378 hammer2_inode_unlock(ip);
1381 * The vp should not be released until after we've disposed
1382 * of our locks, because it might cause vop_inactive() to
1389 cache_setvp(ap->a_nch, NULL);
1391 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1392 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1393 ("resolve error %d/%p ap %p\n",
1394 error, ap->a_nch->ncp->nc_vp, ap));
1401 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1403 hammer2_inode_t *dip;
1407 dip = VTOI(ap->a_dvp);
1408 inum = dip->meta.iparent;
1412 error = hammer2_vfs_vget(ap->a_dvp->v_mount, NULL,
1422 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1424 hammer2_inode_t *dip;
1425 hammer2_inode_t *nip;
1426 struct namecache *ncp;
1432 dip = VTOI(ap->a_dvp);
1433 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG))
1435 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1438 ncp = ap->a_nch->ncp;
1439 name = ncp->nc_name;
1440 name_len = ncp->nc_nlen;
1442 hammer2_trans_init(dip->pmp, 0);
1444 inum = hammer2_trans_newinum(dip->pmp);
1447 * Create the directory as an inode and then create the directory
1450 * dip must be locked before nip to avoid deadlock.
1452 hammer2_inode_lock(dip, 0);
1453 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
1456 error = hammer2_error_to_errno(error);
1458 error = hammer2_dirent_create(dip, name, name_len,
1459 nip->meta.inum, nip->meta.type);
1460 /* returns UNIX error code */
1464 hammer2_inode_unlink_finisher(nip, NULL);
1465 hammer2_inode_unlock(nip);
1471 * inode_depend() must occur before the igetv() because
1472 * the igetv() can temporarily release the inode lock.
1474 hammer2_inode_depend(dip, nip); /* before igetv */
1475 *ap->a_vpp = hammer2_igetv(nip, &error);
1476 hammer2_inode_unlock(nip);
1480 * Update dip's mtime
1482 * We can use a shared inode lock and allow the meta.mtime update
1483 * SMP race. hammer2_inode_modify() is MPSAFE w/a shared lock.
1488 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1489 hammer2_update_time(&mtime);
1490 hammer2_inode_modify(dip);
1491 dip->meta.mtime = mtime;
1492 /*hammer2_inode_unlock(dip);*/
1494 hammer2_inode_unlock(dip);
1496 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1499 cache_setunresolved(ap->a_nch);
1500 cache_setvp(ap->a_nch, *ap->a_vpp);
1501 hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1508 hammer2_vop_open(struct vop_open_args *ap)
1510 return vop_stdopen(ap);
1514 * hammer2_vop_advlock { vp, id, op, fl, flags }
1518 hammer2_vop_advlock(struct vop_advlock_args *ap)
1520 hammer2_inode_t *ip = VTOI(ap->a_vp);
1523 size = ip->meta.size;
1524 return (lf_advlock(ap, &ip->advlock, size));
1529 hammer2_vop_close(struct vop_close_args *ap)
1531 return vop_stdclose(ap);
1535 * hammer2_vop_nlink { nch, dvp, vp, cred }
1537 * Create a hardlink from (vp) to {dvp, nch}.
1541 hammer2_vop_nlink(struct vop_nlink_args *ap)
1543 hammer2_inode_t *tdip; /* target directory to create link in */
1544 hammer2_inode_t *ip; /* inode we are hardlinking to */
1545 struct namecache *ncp;
1551 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1554 tdip = VTOI(ap->a_dvp);
1555 if (tdip->pmp->ronly || (tdip->pmp->flags & HAMMER2_PMPF_EMERG))
1557 if (hammer2_vfs_enospace(tdip, 0, ap->a_cred) > 1)
1560 ncp = ap->a_nch->ncp;
1561 name = ncp->nc_name;
1562 name_len = ncp->nc_nlen;
1565 * ip represents the file being hardlinked. The file could be a
1566 * normal file or a hardlink target if it has already been hardlinked.
1567 * (with the new semantics, it will almost always be a hardlink
1570 * Bump nlinks and potentially also create or move the hardlink
1571 * target in the parent directory common to (ip) and (tdip). The
1572 * consolidation code can modify ip->cluster. The returned cluster
1575 ip = VTOI(ap->a_vp);
1576 KASSERT(ip->pmp, ("ip->pmp is NULL %p %p", ip, ip->pmp));
1577 hammer2_trans_init(ip->pmp, 0);
1580 * Target should be an indexed inode or there's no way we will ever
1581 * be able to find it!
1583 KKASSERT((ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE) == 0);
1585 hammer2_inode_lock4(tdip, ip, NULL, NULL);
1587 hammer2_update_time(&cmtime);
1590 * Create the directory entry and bump nlinks.
1591 * Also update ip's ctime.
1593 error = hammer2_dirent_create(tdip, name, name_len,
1594 ip->meta.inum, ip->meta.type);
1595 hammer2_inode_modify(ip);
1597 ip->meta.ctime = cmtime;
1600 * Update dip's [cm]time
1602 hammer2_inode_modify(tdip);
1603 tdip->meta.mtime = cmtime;
1604 tdip->meta.ctime = cmtime;
1606 cache_setunresolved(ap->a_nch);
1607 cache_setvp(ap->a_nch, ap->a_vp);
1609 hammer2_inode_unlock(ip);
1610 hammer2_inode_unlock(tdip);
1612 hammer2_trans_done(ip->pmp, HAMMER2_TRANS_SIDEQ);
1613 hammer2_knote(ap->a_vp, NOTE_LINK);
1614 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1620 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1622 * The operating system has already ensured that the directory entry
1623 * does not exist and done all appropriate namespace locking.
1627 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1629 hammer2_inode_t *dip;
1630 hammer2_inode_t *nip;
1631 struct namecache *ncp;
1637 dip = VTOI(ap->a_dvp);
1638 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG))
1640 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1643 ncp = ap->a_nch->ncp;
1644 name = ncp->nc_name;
1645 name_len = ncp->nc_nlen;
1646 hammer2_trans_init(dip->pmp, 0);
1648 inum = hammer2_trans_newinum(dip->pmp);
1651 * Create the regular file as an inode and then create the directory
1654 * dip must be locked before nip to avoid deadlock.
1656 hammer2_inode_lock(dip, 0);
1657 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
1660 error = hammer2_error_to_errno(error);
1662 error = hammer2_dirent_create(dip, name, name_len,
1663 nip->meta.inum, nip->meta.type);
1667 hammer2_inode_unlink_finisher(nip, NULL);
1668 hammer2_inode_unlock(nip);
1673 hammer2_inode_depend(dip, nip); /* before igetv */
1674 *ap->a_vpp = hammer2_igetv(nip, &error);
1675 hammer2_inode_unlock(nip);
1679 * Update dip's mtime
1684 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1685 hammer2_update_time(&mtime);
1686 hammer2_inode_modify(dip);
1687 dip->meta.mtime = mtime;
1688 /*hammer2_inode_unlock(dip);*/
1690 hammer2_inode_unlock(dip);
1692 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1695 cache_setunresolved(ap->a_nch);
1696 cache_setvp(ap->a_nch, *ap->a_vpp);
1697 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1703 * Make a device node (typically a fifo)
1707 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1709 hammer2_inode_t *dip;
1710 hammer2_inode_t *nip;
1711 struct namecache *ncp;
1717 dip = VTOI(ap->a_dvp);
1718 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG))
1720 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1723 ncp = ap->a_nch->ncp;
1724 name = ncp->nc_name;
1725 name_len = ncp->nc_nlen;
1726 hammer2_trans_init(dip->pmp, 0);
1729 * Create the device inode and then create the directory entry.
1731 * dip must be locked before nip to avoid deadlock.
1733 inum = hammer2_trans_newinum(dip->pmp);
1735 hammer2_inode_lock(dip, 0);
1736 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
1739 error = hammer2_error_to_errno(error);
1741 error = hammer2_dirent_create(dip, name, name_len,
1742 nip->meta.inum, nip->meta.type);
1746 hammer2_inode_unlink_finisher(nip, NULL);
1747 hammer2_inode_unlock(nip);
1752 hammer2_inode_depend(dip, nip); /* before igetv */
1753 *ap->a_vpp = hammer2_igetv(nip, &error);
1754 hammer2_inode_unlock(nip);
1758 * Update dip's mtime
1763 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1764 hammer2_update_time(&mtime);
1765 hammer2_inode_modify(dip);
1766 dip->meta.mtime = mtime;
1767 /*hammer2_inode_unlock(dip);*/
1769 hammer2_inode_unlock(dip);
1771 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1774 cache_setunresolved(ap->a_nch);
1775 cache_setvp(ap->a_nch, *ap->a_vpp);
1776 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1782 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1786 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1788 hammer2_inode_t *dip;
1789 hammer2_inode_t *nip;
1790 struct namecache *ncp;
1796 dip = VTOI(ap->a_dvp);
1797 if (dip->pmp->ronly || (dip->pmp->flags & HAMMER2_PMPF_EMERG))
1799 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1802 ncp = ap->a_nch->ncp;
1803 name = ncp->nc_name;
1804 name_len = ncp->nc_nlen;
1805 hammer2_trans_init(dip->pmp, 0);
1807 ap->a_vap->va_type = VLNK; /* enforce type */
1810 * Create the softlink as an inode and then create the directory
1813 * dip must be locked before nip to avoid deadlock.
1815 inum = hammer2_trans_newinum(dip->pmp);
1817 hammer2_inode_lock(dip, 0);
1818 nip = hammer2_inode_create_normal(dip, ap->a_vap, ap->a_cred,
1821 error = hammer2_error_to_errno(error);
1823 error = hammer2_dirent_create(dip, name, name_len,
1824 nip->meta.inum, nip->meta.type);
1828 hammer2_inode_unlink_finisher(nip, NULL);
1829 hammer2_inode_unlock(nip);
1833 hammer2_inode_unlock(dip);
1834 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1837 hammer2_inode_depend(dip, nip); /* before igetv */
1838 *ap->a_vpp = hammer2_igetv(nip, &error);
1841 * Build the softlink (~like file data) and finalize the namecache.
1848 bytes = strlen(ap->a_target);
1850 hammer2_inode_unlock(nip);
1851 bzero(&auio, sizeof(auio));
1852 bzero(&aiov, sizeof(aiov));
1853 auio.uio_iov = &aiov;
1854 auio.uio_segflg = UIO_SYSSPACE;
1855 auio.uio_rw = UIO_WRITE;
1856 auio.uio_resid = bytes;
1857 auio.uio_iovcnt = 1;
1858 auio.uio_td = curthread;
1859 aiov.iov_base = ap->a_target;
1860 aiov.iov_len = bytes;
1861 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1862 /* XXX handle error */
1865 hammer2_inode_unlock(nip);
1869 * Update dip's mtime
1874 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1875 hammer2_update_time(&mtime);
1876 hammer2_inode_modify(dip);
1877 dip->meta.mtime = mtime;
1878 /*hammer2_inode_unlock(dip);*/
1880 hammer2_inode_unlock(dip);
1882 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1885 * Finalize namecache
1888 cache_setunresolved(ap->a_nch);
1889 cache_setvp(ap->a_nch, *ap->a_vpp);
1890 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1896 * hammer2_vop_nremove { nch, dvp, cred }
1900 hammer2_vop_nremove(struct vop_nremove_args *ap)
1902 hammer2_xop_unlink_t *xop;
1903 hammer2_inode_t *dip;
1904 hammer2_inode_t *ip;
1905 struct vnode *vprecycle;
1906 struct namecache *ncp;
1909 dip = VTOI(ap->a_dvp);
1910 if (dip->pmp->ronly)
1913 /* allow removals, except user to also bulkfree */
1914 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
1918 ncp = ap->a_nch->ncp;
1920 if (hammer2_debug_inode && dip->meta.inum == hammer2_debug_inode) {
1921 kprintf("hammer2: attempt to delete inside debug inode: %s\n",
1923 while (hammer2_debug_inode &&
1924 dip->meta.inum == hammer2_debug_inode) {
1925 tsleep(&hammer2_debug_inode, 0, "h2debug", hz*5);
1929 hammer2_trans_init(dip->pmp, 0);
1930 hammer2_inode_lock(dip, 0);
1933 * The unlink XOP unlinks the path from the directory and
1934 * locates and returns the cluster associated with the real inode.
1935 * We have to handle nlinks here on the frontend.
1937 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
1938 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
1941 xop->dopermanent = 0;
1942 hammer2_xop_start(&xop->head, &hammer2_unlink_desc);
1945 * Collect the real inode and adjust nlinks, destroy the real
1946 * inode if nlinks transitions to 0 and it was the real inode
1947 * (else it has already been removed).
1949 error = hammer2_xop_collect(&xop->head, 0);
1950 error = hammer2_error_to_errno(error);
1954 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1);
1955 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1957 if (hammer2_debug_inode &&
1958 ip->meta.inum == hammer2_debug_inode) {
1959 kprintf("hammer2: attempt to delete debug "
1961 while (hammer2_debug_inode &&
1962 ip->meta.inum == hammer2_debug_inode) {
1963 tsleep(&hammer2_debug_inode, 0,
1967 hammer2_inode_unlink_finisher(ip, &vprecycle);
1968 hammer2_inode_depend(dip, ip); /* after modified */
1969 hammer2_inode_unlock(ip);
1972 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1976 * Update dip's mtime
1981 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
1982 hammer2_update_time(&mtime);
1983 hammer2_inode_modify(dip);
1984 dip->meta.mtime = mtime;
1985 /*hammer2_inode_unlock(dip);*/
1987 hammer2_inode_unlock(dip);
1989 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
1991 cache_unlink(ap->a_nch);
1992 hammer2_knote(ap->a_dvp, NOTE_WRITE);
1995 hammer2_inode_vprecycle(vprecycle);
2001 * hammer2_vop_nrmdir { nch, dvp, cred }
2005 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
2007 hammer2_xop_unlink_t *xop;
2008 hammer2_inode_t *dip;
2009 hammer2_inode_t *ip;
2010 struct namecache *ncp;
2011 struct vnode *vprecycle;
2014 dip = VTOI(ap->a_dvp);
2015 if (dip->pmp->ronly)
2018 /* allow removals, except user to also bulkfree */
2019 if (hammer2_vfs_enospace(dip, 0, ap->a_cred) > 1)
2023 hammer2_trans_init(dip->pmp, 0);
2024 hammer2_inode_lock(dip, 0);
2026 xop = hammer2_xop_alloc(dip, HAMMER2_XOP_MODIFYING);
2028 ncp = ap->a_nch->ncp;
2029 hammer2_xop_setname(&xop->head, ncp->nc_name, ncp->nc_nlen);
2031 xop->dopermanent = 0;
2032 hammer2_xop_start(&xop->head, &hammer2_unlink_desc);
2035 * Collect the real inode and adjust nlinks, destroy the real
2036 * inode if nlinks transitions to 0 and it was the real inode
2037 * (else it has already been removed).
2039 error = hammer2_xop_collect(&xop->head, 0);
2040 error = hammer2_error_to_errno(error);
2044 ip = hammer2_inode_get(dip->pmp, &xop->head, -1, -1);
2045 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2047 hammer2_inode_unlink_finisher(ip, &vprecycle);
2048 hammer2_inode_depend(dip, ip); /* after modified */
2049 hammer2_inode_unlock(ip);
2052 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2056 * Update dip's mtime
2061 /*hammer2_inode_lock(dip, HAMMER2_RESOLVE_SHARED);*/
2062 hammer2_update_time(&mtime);
2063 hammer2_inode_modify(dip);
2064 dip->meta.mtime = mtime;
2065 /*hammer2_inode_unlock(dip);*/
2067 hammer2_inode_unlock(dip);
2069 hammer2_trans_done(dip->pmp, HAMMER2_TRANS_SIDEQ);
2071 cache_unlink(ap->a_nch);
2072 hammer2_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2075 hammer2_inode_vprecycle(vprecycle);
2080 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
2084 hammer2_vop_nrename(struct vop_nrename_args *ap)
2086 struct namecache *fncp;
2087 struct namecache *tncp;
2088 hammer2_inode_t *fdip; /* source directory */
2089 hammer2_inode_t *tdip; /* target directory */
2090 hammer2_inode_t *ip; /* file being renamed */
2091 hammer2_inode_t *tip; /* replaced target during rename or NULL */
2092 struct vnode *vprecycle;
2102 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
2104 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
2107 fdip = VTOI(ap->a_fdvp); /* source directory */
2108 tdip = VTOI(ap->a_tdvp); /* target directory */
2110 if (fdip->pmp->ronly || (fdip->pmp->flags & HAMMER2_PMPF_EMERG))
2112 if (hammer2_vfs_enospace(fdip, 0, ap->a_cred) > 1)
2115 fncp = ap->a_fnch->ncp; /* entry name in source */
2116 fname = fncp->nc_name;
2117 fname_len = fncp->nc_nlen;
2119 tncp = ap->a_tnch->ncp; /* entry name in target */
2120 tname = tncp->nc_name;
2121 tname_len = tncp->nc_nlen;
2123 hammer2_trans_init(tdip->pmp, 0);
2128 ip = VTOI(fncp->nc_vp);
2129 hammer2_inode_ref(ip); /* extra ref */
2132 * Lookup the target name to determine if a directory entry
2133 * is being overwritten. We only hold related inode locks
2134 * temporarily, the operating system is expected to protect
2135 * against rename races.
2137 tip = tncp->nc_vp ? VTOI(tncp->nc_vp) : NULL;
2139 hammer2_inode_ref(tip); /* extra ref */
2142 * For now try to avoid deadlocks with a simple pointer address
2143 * test. (tip) can be NULL.
2147 hammer2_inode_t *ip1 = fdip;
2148 hammer2_inode_t *ip2 = tdip;
2149 hammer2_inode_t *ip3 = ip;
2150 hammer2_inode_t *ip4 = tip; /* may be NULL */
2156 if (tip && ip > tip) {
2160 hammer2_inode_lock4(ip1, ip2, ip3, ip4);
2164 * Resolve the collision space for (tdip, tname, tname_len)
2166 * tdip must be held exclusively locked to prevent races since
2167 * multiple filenames can end up in the same collision space.
2170 hammer2_xop_scanlhc_t *sxop;
2171 hammer2_key_t lhcbase;
2173 tlhc = hammer2_dirhash(tname, tname_len);
2175 sxop = hammer2_xop_alloc(tdip, HAMMER2_XOP_MODIFYING);
2177 hammer2_xop_start(&sxop->head, &hammer2_scanlhc_desc);
2178 while ((error = hammer2_xop_collect(&sxop->head, 0)) == 0) {
2179 if (tlhc != sxop->head.cluster.focus->bref.key)
2183 error = hammer2_error_to_errno(error);
2184 hammer2_xop_retire(&sxop->head, HAMMER2_XOPMASK_VOP);
2187 if (error != ENOENT)
2192 if ((lhcbase ^ tlhc) & ~HAMMER2_DIRHASH_LOMASK) {
2199 * Ready to go, issue the rename to the backend. Note that meta-data
2200 * updates to the related inodes occur separately from the rename
2203 * NOTE: While it is not necessary to update ip->meta.name*, doing
2204 * so aids catastrophic recovery and debugging.
2207 hammer2_xop_nrename_t *xop4;
2209 xop4 = hammer2_xop_alloc(fdip, HAMMER2_XOP_MODIFYING);
2211 xop4->ip_key = ip->meta.name_key;
2212 hammer2_xop_setip2(&xop4->head, ip);
2213 hammer2_xop_setip3(&xop4->head, tdip);
2214 if (tip && tip->meta.type == HAMMER2_OBJTYPE_DIRECTORY)
2215 hammer2_xop_setip4(&xop4->head, tip);
2216 hammer2_xop_setname(&xop4->head, fname, fname_len);
2217 hammer2_xop_setname2(&xop4->head, tname, tname_len);
2218 hammer2_xop_start(&xop4->head, &hammer2_nrename_desc);
2220 error = hammer2_xop_collect(&xop4->head, 0);
2221 error = hammer2_error_to_errno(error);
2222 hammer2_xop_retire(&xop4->head, HAMMER2_XOPMASK_VOP);
2224 if (error == ENOENT)
2228 * Update inode meta-data.
2230 * WARNING! The in-memory inode (ip) structure does not
2231 * maintain a copy of the inode's filename buffer.
2234 (ip->meta.name_key & HAMMER2_DIRHASH_VISIBLE)) {
2235 hammer2_inode_modify(ip);
2236 ip->meta.name_len = tname_len;
2237 ip->meta.name_key = tlhc;
2240 hammer2_inode_modify(ip);
2241 ip->meta.iparent = tdip->meta.inum;
2249 * If no error, the backend has replaced the target directory entry.
2250 * We must adjust nlinks on the original replace target if it exists.
2253 if (error == 0 && tip) {
2254 hammer2_inode_unlink_finisher(tip, &vprecycle);
2258 * Update directory mtimes to represent the something changed.
2260 if (update_fdip || update_tdip) {
2263 hammer2_update_time(&mtime);
2265 hammer2_inode_modify(fdip);
2266 fdip->meta.mtime = mtime;
2269 hammer2_inode_modify(tdip);
2270 tdip->meta.mtime = mtime;
2274 hammer2_inode_unlock(tip);
2275 hammer2_inode_drop(tip);
2277 hammer2_inode_unlock(ip);
2278 hammer2_inode_unlock(tdip);
2279 hammer2_inode_unlock(fdip);
2280 hammer2_inode_drop(ip);
2281 hammer2_trans_done(tdip->pmp, HAMMER2_TRANS_SIDEQ);
2284 * Issue the namecache update after unlocking all the internal
2285 * hammer2 structures, otherwise we might deadlock.
2287 * WARNING! The target namespace must be updated atomically,
2288 * and we depend on cache_rename() to handle that for
2289 * us. Do not do a separate cache_unlink() because
2290 * that leaves a small window of opportunity for other
2291 * threads to allocate the target namespace before we
2292 * manage to complete our rename.
2294 * WARNING! cache_rename() (and cache_unlink()) will properly
2295 * set VREF_FINALIZE on any attached vnode. Do not
2296 * call cache_setunresolved() manually before-hand as
2297 * this will prevent the flag from being set later via
2298 * cache_rename(). If VREF_FINALIZE is not properly set
2299 * and the inode is no longer in the topology, related
2300 * chains can remain dirty indefinitely.
2302 if (error == 0 && tip) {
2303 /*cache_unlink(ap->a_tnch); see above */
2304 /*cache_setunresolved(ap->a_tnch); see above */
2307 cache_rename(ap->a_fnch, ap->a_tnch);
2308 hammer2_knote(ap->a_fdvp, NOTE_WRITE);
2309 hammer2_knote(ap->a_tdvp, NOTE_WRITE);
2310 hammer2_knote(fncp->nc_vp, NOTE_RENAME);
2313 hammer2_inode_vprecycle(vprecycle);
2319 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2323 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2325 hammer2_inode_t *ip;
2328 ip = VTOI(ap->a_vp);
2330 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2331 ap->a_fflag, ap->a_cred);
2337 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2344 case (MOUNTCTL_SET_EXPORT):
2345 mp = ap->a_head.a_ops->head.vv_mount;
2348 if (ap->a_ctllen != sizeof(struct export_args))
2351 rc = vfs_export(mp, &pmp->export,
2352 (const struct export_args *)ap->a_ctl);
2355 rc = vop_stdmountctl(ap);
2364 static void filt_hammer2detach(struct knote *kn);
2365 static int filt_hammer2read(struct knote *kn, long hint);
2366 static int filt_hammer2write(struct knote *kn, long hint);
2367 static int filt_hammer2vnode(struct knote *kn, long hint);
2369 static struct filterops hammer2read_filtops =
2370 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2371 NULL, filt_hammer2detach, filt_hammer2read };
2372 static struct filterops hammer2write_filtops =
2373 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2374 NULL, filt_hammer2detach, filt_hammer2write };
2375 static struct filterops hammer2vnode_filtops =
2376 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2377 NULL, filt_hammer2detach, filt_hammer2vnode };
2381 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
2383 struct vnode *vp = ap->a_vp;
2384 struct knote *kn = ap->a_kn;
2386 switch (kn->kn_filter) {
2388 kn->kn_fop = &hammer2read_filtops;
2391 kn->kn_fop = &hammer2write_filtops;
2394 kn->kn_fop = &hammer2vnode_filtops;
2397 return (EOPNOTSUPP);
2400 kn->kn_hook = (caddr_t)vp;
2402 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2408 filt_hammer2detach(struct knote *kn)
2410 struct vnode *vp = (void *)kn->kn_hook;
2412 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2416 filt_hammer2read(struct knote *kn, long hint)
2418 struct vnode *vp = (void *)kn->kn_hook;
2419 hammer2_inode_t *ip = VTOI(vp);
2422 if (hint == NOTE_REVOKE) {
2423 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2426 off = ip->meta.size - kn->kn_fp->f_offset;
2427 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2428 if (kn->kn_sfflags & NOTE_OLDAPI)
2430 return (kn->kn_data != 0);
2435 filt_hammer2write(struct knote *kn, long hint)
2437 if (hint == NOTE_REVOKE)
2438 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2444 filt_hammer2vnode(struct knote *kn, long hint)
2446 if (kn->kn_sfflags & hint)
2447 kn->kn_fflags |= hint;
2448 if (hint == NOTE_REVOKE) {
2449 kn->kn_flags |= (EV_EOF | EV_NODATA);
2452 return (kn->kn_fflags != 0);
2460 hammer2_vop_markatime(struct vop_markatime_args *ap)
2462 hammer2_inode_t *ip;
2468 if (ip->pmp->ronly || (ip->pmp->flags & HAMMER2_PMPF_EMERG))
2475 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2479 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2481 error = hammer2_vop_kqfilter(ap);
2488 struct vop_ops hammer2_vnode_vops = {
2489 .vop_default = vop_defaultop,
2490 .vop_fsync = hammer2_vop_fsync,
2491 .vop_getpages = vop_stdgetpages,
2492 .vop_putpages = vop_stdputpages,
2493 .vop_access = hammer2_vop_access,
2494 .vop_advlock = hammer2_vop_advlock,
2495 .vop_close = hammer2_vop_close,
2496 .vop_nlink = hammer2_vop_nlink,
2497 .vop_ncreate = hammer2_vop_ncreate,
2498 .vop_nsymlink = hammer2_vop_nsymlink,
2499 .vop_nremove = hammer2_vop_nremove,
2500 .vop_nrmdir = hammer2_vop_nrmdir,
2501 .vop_nrename = hammer2_vop_nrename,
2502 .vop_getattr = hammer2_vop_getattr,
2503 .vop_getattr_lite = hammer2_vop_getattr_lite,
2504 .vop_setattr = hammer2_vop_setattr,
2505 .vop_readdir = hammer2_vop_readdir,
2506 .vop_readlink = hammer2_vop_readlink,
2507 .vop_read = hammer2_vop_read,
2508 .vop_write = hammer2_vop_write,
2509 .vop_open = hammer2_vop_open,
2510 .vop_inactive = hammer2_vop_inactive,
2511 .vop_reclaim = hammer2_vop_reclaim,
2512 .vop_nresolve = hammer2_vop_nresolve,
2513 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2514 .vop_nmkdir = hammer2_vop_nmkdir,
2515 .vop_nmknod = hammer2_vop_nmknod,
2516 .vop_ioctl = hammer2_vop_ioctl,
2517 .vop_mountctl = hammer2_vop_mountctl,
2518 .vop_bmap = hammer2_vop_bmap,
2519 .vop_strategy = hammer2_vop_strategy,
2520 .vop_kqfilter = hammer2_vop_kqfilter
2523 struct vop_ops hammer2_spec_vops = {
2524 .vop_default = vop_defaultop,
2525 .vop_fsync = hammer2_vop_fsync,
2526 .vop_read = vop_stdnoread,
2527 .vop_write = vop_stdnowrite,
2528 .vop_access = hammer2_vop_access,
2529 .vop_close = hammer2_vop_close,
2530 .vop_markatime = hammer2_vop_markatime,
2531 .vop_getattr = hammer2_vop_getattr,
2532 .vop_inactive = hammer2_vop_inactive,
2533 .vop_reclaim = hammer2_vop_reclaim,
2534 .vop_setattr = hammer2_vop_setattr
2537 struct vop_ops hammer2_fifo_vops = {
2538 .vop_default = fifo_vnoperate,
2539 .vop_fsync = hammer2_vop_fsync,
2541 .vop_read = hammer2_vop_fiforead,
2542 .vop_write = hammer2_vop_fifowrite,
2544 .vop_access = hammer2_vop_access,
2546 .vop_close = hammer2_vop_fifoclose,
2548 .vop_markatime = hammer2_vop_markatime,
2549 .vop_getattr = hammer2_vop_getattr,
2550 .vop_inactive = hammer2_vop_inactive,
2551 .vop_reclaim = hammer2_vop_reclaim,
2552 .vop_setattr = hammer2_vop_setattr,
2553 .vop_kqfilter = hammer2_vop_fifokqfilter