2 * Copyright (c) 2011-2013 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>
61 #include "hammer2_lz4.h"
63 #include "zlib/hammer2_zlib.h"
65 #define ZFOFFSET (-2LL)
67 static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio,
69 static int hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
70 int ioflag, int seqcount);
71 static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize);
72 static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize);
73 static void hammer2_decompress_LZ4_callback(hammer2_io_t *dio,
74 hammer2_chain_t *arg_c,
75 void *arg_p, off_t arg_o);
76 static void hammer2_decompress_ZLIB_callback(hammer2_io_t *dio,
77 hammer2_chain_t *arg_c,
78 void *arg_p, off_t arg_o);
80 struct objcache *cache_buffer_read;
81 struct objcache *cache_buffer_write;
84 * Callback used in read path in case that a block is compressed with LZ4.
88 hammer2_decompress_LZ4_callback(hammer2_io_t *dio, hammer2_chain_t *arg_c,
89 void *arg_p, off_t arg_o)
92 struct bio *obio = arg_p;
94 int bytes = 1 << (int)(arg_o & HAMMER2_OFF_MASK_RADIX);
97 * If BIO_DONE is already set the device buffer was already
98 * fully valid (B_CACHE). If it is not set then I/O was issued
99 * and we have to run I/O completion as the last bio.
101 * Nobody is waiting for our device I/O to complete, we are
102 * responsible for bqrelse()ing it which means we also have to do
103 * the equivalent of biowait() and clear BIO_DONE (which breadcb()
106 * Any preexisting device buffer should match the requested size,
107 * but due to bigblock recycling and other factors there is some
108 * fragility there, so we assert that the device buffer covers
113 if (dio->bp->b_flags & B_ERROR) {
114 obp->b_flags |= B_ERROR;
115 obp->b_error = dio->bp->b_error;
117 } else if (obio->bio_caller_info2.index &&
118 obio->bio_caller_info1.uvalue32 !=
119 crc32(bp->b_data, bp->b_bufsize)) {
120 obp->b_flags |= B_ERROR;
124 char *compressed_buffer;
125 int *compressed_size;
128 KKASSERT(obp->b_bufsize <= HAMMER2_PBUFSIZE);
129 bdata = hammer2_io_data(dio, arg_o);
130 compressed_size = (int *)bdata;
131 compressed_buffer = objcache_get(cache_buffer_read, M_INTWAIT);
132 KKASSERT((unsigned int)*compressed_size <= HAMMER2_PBUFSIZE);
133 result = LZ4_decompress_safe(&bdata[sizeof(int)],
138 kprintf("READ PATH: Error during decompression."
139 "bio %016jx/%d log %016jx/%d\n",
140 (intmax_t)dio->pbase, dio->psize,
141 (intmax_t)arg_o, bytes);
142 /* make sure it isn't random garbage */
143 bzero(compressed_buffer, obp->b_bufsize);
145 KKASSERT(result <= obp->b_bufsize);
146 bcopy(compressed_buffer, obp->b_data, obp->b_bufsize);
147 if (result < obp->b_bufsize)
148 bzero(obp->b_data + result, obp->b_bufsize - result);
149 objcache_put(cache_buffer_read, compressed_buffer);
151 obp->b_flags |= B_AGE;
157 * Callback used in read path in case that a block is compressed with ZLIB.
158 * It is almost identical to LZ4 callback, so in theory they can be unified,
159 * but we didn't want to make changes in bio structure for that.
163 hammer2_decompress_ZLIB_callback(hammer2_io_t *dio, hammer2_chain_t *arg_c,
164 void *arg_p, off_t arg_o)
167 struct bio *obio = arg_p;
169 int bytes = 1 << (int)(arg_o & HAMMER2_OFF_MASK_RADIX);
172 * If BIO_DONE is already set the device buffer was already
173 * fully valid (B_CACHE). If it is not set then I/O was issued
174 * and we have to run I/O completion as the last bio.
176 * Nobody is waiting for our device I/O to complete, we are
177 * responsible for bqrelse()ing it which means we also have to do
178 * the equivalent of biowait() and clear BIO_DONE (which breadcb()
181 * Any preexisting device buffer should match the requested size,
182 * but due to bigblock recycling and other factors there is some
183 * fragility there, so we assert that the device buffer covers
188 if (dio->bp->b_flags & B_ERROR) {
189 obp->b_flags |= B_ERROR;
190 obp->b_error = dio->bp->b_error;
192 } else if (obio->bio_caller_info2.index &&
193 obio->bio_caller_info1.uvalue32 !=
194 crc32(bp->b_data, bp->b_bufsize)) {
195 obp->b_flags |= B_ERROR;
199 char *compressed_buffer;
200 z_stream strm_decompress;
204 KKASSERT(obp->b_bufsize <= HAMMER2_PBUFSIZE);
205 strm_decompress.avail_in = 0;
206 strm_decompress.next_in = Z_NULL;
208 ret = inflateInit(&strm_decompress);
211 kprintf("HAMMER2 ZLIB: Fatal error in inflateInit.\n");
213 bdata = hammer2_io_data(dio, arg_o);
214 compressed_buffer = objcache_get(cache_buffer_read, M_INTWAIT);
215 strm_decompress.next_in = bdata;
217 /* XXX supply proper size, subset of device bp */
218 strm_decompress.avail_in = bytes;
219 strm_decompress.next_out = compressed_buffer;
220 strm_decompress.avail_out = obp->b_bufsize;
222 ret = inflate(&strm_decompress, Z_FINISH);
223 if (ret != Z_STREAM_END) {
224 kprintf("HAMMER2 ZLIB: Fatar error during decompression.\n");
225 bzero(compressed_buffer, obp->b_bufsize);
227 bcopy(compressed_buffer, obp->b_data, obp->b_bufsize);
228 result = obp->b_bufsize - strm_decompress.avail_out;
229 if (result < obp->b_bufsize)
230 bzero(obp->b_data + result, strm_decompress.avail_out);
231 objcache_put(cache_buffer_read, compressed_buffer);
233 obp->b_flags |= B_AGE;
234 ret = inflateEnd(&strm_decompress);
241 hammer2_knote(struct vnode *vp, int flags)
244 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
248 * Last reference to a vnode is going away but it is still cached.
252 hammer2_vop_inactive(struct vop_inactive_args *ap)
255 hammer2_chain_t *parent;
270 * Detect updates to the embedded data which may be synchronized by
271 * the strategy code. Simply mark the inode modified so it gets
272 * picked up by our normal flush.
274 parent = hammer2_inode_lock_ex(ip);
278 * Check for deleted inodes and recycle immediately.
280 if (parent->flags & HAMMER2_CHAIN_UNLINKED) {
281 hammer2_inode_unlock_ex(ip, parent);
284 hammer2_inode_unlock_ex(ip, parent);
290 * Reclaim a vnode so that it can be reused; after the inode is
291 * disassociated, the filesystem must manage it alone.
295 hammer2_vop_reclaim(struct vop_reclaim_args *ap)
297 hammer2_chain_t *chain;
299 hammer2_trans_t trans;
308 * Set update_hi so we can detect and propagate the DELETED
309 * bit in the flush code.
311 * ip->chain might be stale, correct it before checking as older
312 * versions of the chain are likely marked deleted even if the
313 * file hasn't been. XXX ip->chain should never be stale on
316 chain = hammer2_inode_lock_ex(ip);
318 if (chain->next_parent)
319 kprintf("RECLAIM DUPLINKED IP: %p ip->ch=%p ch=%p np=%p\n",
320 ip, ip->chain, chain, chain->next_parent);
324 * The final close of a deleted file or directory marks it for
325 * destruction. The DELETED flag allows the flusher to shortcut
326 * any modified blocks still unflushed (that is, just ignore them).
328 * HAMMER2 usually does not try to optimize the freemap by returning
329 * deleted blocks to it as it does not usually know how many snapshots
330 * might be referencing portions of the file/dir. XXX TODO.
332 * XXX TODO - However, any modified file as-of when a snapshot is made
333 * cannot use this optimization as some of the modifications
334 * may wind up being part of the snapshot.
338 if (chain->flags & HAMMER2_CHAIN_UNLINKED) {
339 kprintf("unlink on reclaim: %s\n",
340 chain->data->ipdata.filename);
341 hammer2_trans_init(&trans, ip->pmp, NULL,
342 HAMMER2_TRANS_BUFCACHE);
343 hammer2_chain_delete(&trans, chain, 0);
344 hammer2_chain_setsubmod(&trans, chain);
345 spin_lock(&chain->core->cst.spin);
346 if (chain->core->update_hi < trans.sync_tid)
347 chain->core->update_hi = trans.sync_tid; /* needed? */
348 spin_unlock(&chain->core->cst.spin);
349 hammer2_trans_done(&trans);
353 * NOTE! We do not attempt to flush chains here, flushing is
354 * really fragile and could also deadlock.
357 hammer2_inode_unlock_ex(ip, chain); /* unlock */
358 hammer2_inode_drop(ip); /* vp ref */
359 /* chain no longer referenced */
360 /* chain = NULL; not needed */
363 * XXX handle background sync when ip dirty, kernel will no longer
364 * notify us regarding this inode because there is no longer a
365 * vnode attached to it.
373 hammer2_vop_fsync(struct vop_fsync_args *ap)
376 hammer2_trans_t trans;
377 hammer2_chain_t *chain;
384 /* XXX can't do this yet */
385 hammer2_trans_init(&trans, ip->pmp, NULL, HAMMER2_TRANS_ISFLUSH);
386 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
387 hammer2_trans_clear_invfsync(&trans);
389 hammer2_trans_init(&trans, ip->pmp, NULL, 0);
390 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
393 * Calling chain_flush here creates a lot of duplicative
394 * COW operations due to non-optimal vnode ordering.
396 * Only do it for an actual fsync() syscall. The other forms
397 * which call this function will eventually call chain_flush
398 * on the volume root as a catch-all, which is far more optimal.
400 chain = hammer2_inode_lock_ex(ip);
401 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
403 if (ip->flags & (HAMMER2_INODE_RESIZED|HAMMER2_INODE_MTIME))
404 hammer2_inode_fsync(&trans, ip, &chain);
408 * XXX creates discontinuity w/modify_tid
410 if (ap->a_flags & VOP_FSYNC_SYSCALL) {
411 hammer2_chain_flush(&trans, &chain);
414 hammer2_inode_unlock_ex(ip, chain);
415 hammer2_trans_done(&trans);
422 hammer2_vop_access(struct vop_access_args *ap)
424 hammer2_inode_t *ip = VTOI(ap->a_vp);
425 hammer2_inode_data_t *ipdata;
426 hammer2_chain_t *chain;
431 chain = hammer2_inode_lock_sh(ip);
432 ipdata = &chain->data->ipdata;
433 uid = hammer2_to_unix_xid(&ipdata->uid);
434 gid = hammer2_to_unix_xid(&ipdata->gid);
435 error = vop_helper_access(ap, uid, gid, ipdata->mode, ipdata->uflags);
436 hammer2_inode_unlock_sh(ip, chain);
443 hammer2_vop_getattr(struct vop_getattr_args *ap)
445 hammer2_inode_data_t *ipdata;
446 hammer2_chain_t *chain;
447 hammer2_pfsmount_t *pmp;
458 chain = hammer2_inode_lock_sh(ip);
459 ipdata = &chain->data->ipdata;
461 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
462 vap->va_fileid = ipdata->inum;
463 vap->va_mode = ipdata->mode;
464 vap->va_nlink = ipdata->nlinks;
465 vap->va_uid = hammer2_to_unix_xid(&ipdata->uid);
466 vap->va_gid = hammer2_to_unix_xid(&ipdata->gid);
469 vap->va_size = ip->size; /* protected by shared lock */
470 vap->va_blocksize = HAMMER2_PBUFSIZE;
471 vap->va_flags = ipdata->uflags;
472 hammer2_time_to_timespec(ipdata->ctime, &vap->va_ctime);
473 hammer2_time_to_timespec(ipdata->mtime, &vap->va_mtime);
474 hammer2_time_to_timespec(ipdata->mtime, &vap->va_atime);
476 vap->va_bytes = vap->va_size; /* XXX */
477 vap->va_type = hammer2_get_vtype(chain);
479 vap->va_uid_uuid = ipdata->uid;
480 vap->va_gid_uuid = ipdata->gid;
481 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
484 hammer2_inode_unlock_sh(ip, chain);
491 hammer2_vop_setattr(struct vop_setattr_args *ap)
493 hammer2_inode_data_t *ipdata;
495 hammer2_chain_t *chain;
496 hammer2_trans_t trans;
506 hammer2_update_time(&ctime);
513 hammer2_chain_memory_wait(ip->pmp);
514 hammer2_trans_init(&trans, ip->pmp, NULL, 0);
515 chain = hammer2_inode_lock_ex(ip);
516 ipdata = &chain->data->ipdata;
519 if (vap->va_flags != VNOVAL) {
522 flags = ipdata->uflags;
523 error = vop_helper_setattr_flags(&flags, vap->va_flags,
524 hammer2_to_unix_xid(&ipdata->uid),
527 if (ipdata->uflags != flags) {
528 ipdata = hammer2_chain_modify_ip(&trans, ip,
530 ipdata->uflags = flags;
531 ipdata->ctime = ctime;
532 kflags |= NOTE_ATTRIB;
534 if (ipdata->uflags & (IMMUTABLE | APPEND)) {
541 if (ipdata->uflags & (IMMUTABLE | APPEND)) {
545 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
546 mode_t cur_mode = ipdata->mode;
547 uid_t cur_uid = hammer2_to_unix_xid(&ipdata->uid);
548 gid_t cur_gid = hammer2_to_unix_xid(&ipdata->gid);
552 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
554 &cur_uid, &cur_gid, &cur_mode);
556 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
557 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
558 if (bcmp(&uuid_uid, &ipdata->uid, sizeof(uuid_uid)) ||
559 bcmp(&uuid_gid, &ipdata->gid, sizeof(uuid_gid)) ||
560 ipdata->mode != cur_mode
562 ipdata = hammer2_chain_modify_ip(&trans, ip,
564 ipdata->uid = uuid_uid;
565 ipdata->gid = uuid_gid;
566 ipdata->mode = cur_mode;
567 ipdata->ctime = ctime;
569 kflags |= NOTE_ATTRIB;
576 if (vap->va_size != VNOVAL && ip->size != vap->va_size) {
579 if (vap->va_size == ip->size)
581 hammer2_inode_unlock_ex(ip, chain);
582 if (vap->va_size < ip->size) {
583 hammer2_truncate_file(ip, vap->va_size);
585 hammer2_extend_file(ip, vap->va_size);
587 chain = hammer2_inode_lock_ex(ip);
588 ipdata = &chain->data->ipdata; /* RELOAD */
597 /* atime not supported */
598 if (vap->va_atime.tv_sec != VNOVAL) {
599 ipdata = hammer2_chain_modify_ip(&trans, ip, &chain, 0);
600 ipdata->atime = hammer2_timespec_to_time(&vap->va_atime);
601 kflags |= NOTE_ATTRIB;
604 if (vap->va_mtime.tv_sec != VNOVAL) {
605 ipdata = hammer2_chain_modify_ip(&trans, ip, &chain, 0);
606 ipdata->mtime = hammer2_timespec_to_time(&vap->va_mtime);
607 kflags |= NOTE_ATTRIB;
610 if (vap->va_mode != (mode_t)VNOVAL) {
611 mode_t cur_mode = ipdata->mode;
612 uid_t cur_uid = hammer2_to_unix_xid(&ipdata->uid);
613 gid_t cur_gid = hammer2_to_unix_xid(&ipdata->gid);
615 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
616 cur_uid, cur_gid, &cur_mode);
617 if (error == 0 && ipdata->mode != cur_mode) {
618 ipdata = hammer2_chain_modify_ip(&trans, ip, &chain, 0);
619 ipdata->mode = cur_mode;
620 ipdata->ctime = ctime;
621 kflags |= NOTE_ATTRIB;
626 * If a truncation occurred we must call inode_fsync() now in order
627 * to trim the related data chains, otherwise a later expansion can
630 hammer2_inode_fsync(&trans, ip, &chain);
633 * Cleanup. If domtime is set an additional inode modification
634 * must be flagged. All other modifications will have already
635 * set INODE_MODIFIED and called vsetisdirty().
639 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED |
640 HAMMER2_INODE_MTIME);
643 hammer2_inode_unlock_ex(ip, chain);
644 hammer2_trans_done(&trans);
645 hammer2_knote(ip->vp, kflags);
652 hammer2_vop_readdir(struct vop_readdir_args *ap)
654 hammer2_inode_data_t *ipdata;
656 hammer2_inode_t *xip;
657 hammer2_chain_t *parent;
658 hammer2_chain_t *chain;
659 hammer2_chain_t *xchain;
661 hammer2_key_t key_next;
667 int cache_index = -1;
675 saveoff = uio->uio_offset;
678 * Setup cookies directory entry cookies if requested
680 if (ap->a_ncookies) {
681 ncookies = uio->uio_resid / 16 + 1;
684 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
691 parent = hammer2_inode_lock_sh(ip);
692 ipdata = &parent->data->ipdata;
695 * Handle artificial entries. To ensure that only positive 64 bit
696 * quantities are returned to userland we always strip off bit 63.
697 * The hash code is designed such that codes 0x0000-0x7FFF are not
698 * used, allowing us to use these codes for articial entries.
700 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
701 * allow '..' to cross the mount point into (e.g.) the super-root.
704 chain = (void *)(intptr_t)-1; /* non-NULL for early goto done case */
707 inum = ipdata->inum & HAMMER2_DIRHASH_USERMSK;
708 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
712 cookies[cookie_index] = saveoff;
715 if (cookie_index == ncookies)
721 * Be careful with lockorder when accessing ".."
723 * (ip is the current dir. xip is the parent dir).
725 inum = ipdata->inum & HAMMER2_DIRHASH_USERMSK;
726 while (ip->pip != NULL && ip != ip->pmp->iroot) {
728 hammer2_inode_ref(xip);
729 hammer2_inode_unlock_sh(ip, parent);
730 xchain = hammer2_inode_lock_sh(xip);
731 parent = hammer2_inode_lock_sh(ip);
732 hammer2_inode_drop(xip);
733 if (xip == ip->pip) {
734 inum = xchain->data->ipdata.inum &
735 HAMMER2_DIRHASH_USERMSK;
736 hammer2_inode_unlock_sh(xip, xchain);
739 hammer2_inode_unlock_sh(xip, xchain);
741 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
745 cookies[cookie_index] = saveoff;
748 if (cookie_index == ncookies)
752 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
753 if (hammer2_debug & 0x0020)
754 kprintf("readdir: lkey %016jx\n", lkey);
757 * parent is the inode chain, already locked for us. Don't
758 * double lock shared locks as this will screw up upgrades.
763 chain = hammer2_chain_lookup(&parent, &key_next, lkey, lkey,
764 &cache_index, HAMMER2_LOOKUP_SHARED);
766 chain = hammer2_chain_lookup(&parent, &key_next,
767 lkey, (hammer2_key_t)-1,
769 HAMMER2_LOOKUP_SHARED);
772 if (hammer2_debug & 0x0020)
773 kprintf("readdir: p=%p chain=%p %016jx (next %016jx)\n",
774 parent, chain, chain->bref.key, key_next);
775 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
776 dtype = hammer2_get_dtype(chain);
777 saveoff = chain->bref.key & HAMMER2_DIRHASH_USERMSK;
778 r = vop_write_dirent(&error, uio,
779 chain->data->ipdata.inum &
780 HAMMER2_DIRHASH_USERMSK,
782 chain->data->ipdata.name_len,
783 chain->data->ipdata.filename);
787 cookies[cookie_index] = saveoff;
790 /* XXX chain error */
791 kprintf("bad chain type readdir %d\n",
796 * Keys may not be returned in order so once we have a
797 * placemarker (chain) the scan must allow the full range
798 * or some entries will be missed.
800 chain = hammer2_chain_next(&parent, chain, &key_next,
801 key_next, (hammer2_key_t)-1,
802 &cache_index, HAMMER2_LOOKUP_SHARED);
804 saveoff = (chain->bref.key &
805 HAMMER2_DIRHASH_USERMSK) + 1;
807 saveoff = (hammer2_key_t)-1;
809 if (cookie_index == ncookies)
813 hammer2_chain_unlock(chain);
815 hammer2_inode_unlock_sh(ip, parent);
817 *ap->a_eofflag = (chain == NULL);
818 if (hammer2_debug & 0x0020)
819 kprintf("readdir: done at %016jx\n", saveoff);
820 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
821 if (error && cookie_index == 0) {
823 kfree(cookies, M_TEMP);
825 *ap->a_cookies = NULL;
829 *ap->a_ncookies = cookie_index;
830 *ap->a_cookies = cookies;
837 * hammer2_vop_readlink { vp, uio, cred }
841 hammer2_vop_readlink(struct vop_readlink_args *ap)
848 if (vp->v_type != VLNK)
852 error = hammer2_read_file(ip, ap->a_uio, 0);
858 hammer2_vop_read(struct vop_read_args *ap)
868 * Read operations supported on this vnode?
871 if (vp->v_type != VREG)
881 seqcount = ap->a_ioflag >> 16;
882 bigread = (uio->uio_resid > 100 * 1024 * 1024);
884 error = hammer2_read_file(ip, uio, seqcount);
890 hammer2_vop_write(struct vop_write_args *ap)
893 hammer2_trans_t trans;
902 * Read operations supported on this vnode?
905 if (vp->v_type != VREG)
917 seqcount = ap->a_ioflag >> 16;
918 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
921 * Check resource limit
923 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
924 uio->uio_offset + uio->uio_resid >
925 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
926 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
930 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
933 * The transaction interlocks against flushes initiations
934 * (note: but will run concurrently with the actual flush).
936 hammer2_trans_init(&trans, ip->pmp, NULL, 0);
937 error = hammer2_write_file(ip, uio, ap->a_ioflag, seqcount);
938 hammer2_trans_done(&trans);
944 * Perform read operations on a file or symlink given an UNLOCKED
947 * The passed ip is not locked.
951 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
962 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
964 ccms_thread_unlock(&ip->topo_cst);
966 while (uio->uio_resid > 0 && uio->uio_offset < size) {
973 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
976 error = cluster_read(ip->vp, leof, lbase, lblksize,
977 uio->uio_resid, seqcount * BKVASIZE,
982 loff = (int)(uio->uio_offset - lbase);
984 if (n > uio->uio_resid)
986 if (n > size - uio->uio_offset)
987 n = (int)(size - uio->uio_offset);
988 bp->b_flags |= B_AGE;
989 uiomove((char *)bp->b_data + loff, n, uio);
996 * Write to the file represented by the inode via the logical buffer cache.
997 * The inode may represent a regular file or a symlink.
999 * The inode must not be locked.
1003 hammer2_write_file(hammer2_inode_t *ip,
1004 struct uio *uio, int ioflag, int seqcount)
1006 hammer2_key_t old_eof;
1007 hammer2_key_t new_eof;
1016 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1017 if (ioflag & IO_APPEND)
1018 uio->uio_offset = ip->size;
1020 ccms_thread_unlock(&ip->topo_cst);
1023 * Extend the file if necessary. If the write fails at some point
1024 * we will truncate it back down to cover as much as we were able
1027 * Doing this now makes it easier to calculate buffer sizes in
1034 if (uio->uio_offset + uio->uio_resid > old_eof) {
1035 new_eof = uio->uio_offset + uio->uio_resid;
1037 hammer2_extend_file(ip, new_eof);
1038 kflags |= NOTE_EXTEND;
1046 while (uio->uio_resid > 0) {
1047 hammer2_key_t lbase;
1055 * Don't allow the buffer build to blow out the buffer
1058 if ((ioflag & IO_RECURSE) == 0)
1059 bwillwrite(HAMMER2_PBUFSIZE);
1062 * This nominally tells us how much we can cluster and
1063 * what the logical buffer size needs to be. Currently
1064 * we don't try to cluster the write and just handle one
1067 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
1069 loff = (int)(uio->uio_offset - lbase);
1071 KKASSERT(lblksize <= 65536);
1074 * Calculate bytes to copy this transfer and whether the
1075 * copy completely covers the buffer or not.
1078 n = lblksize - loff;
1079 if (n > uio->uio_resid) {
1081 if (loff == lbase && uio->uio_offset + n == new_eof)
1093 if (uio->uio_segflg == UIO_NOCOPY) {
1095 * Issuing a write with the same data backing the
1096 * buffer. Instantiate the buffer to collect the
1097 * backing vm pages, then read-in any missing bits.
1099 * This case is used by vop_stdputpages().
1101 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
1102 if ((bp->b_flags & B_CACHE) == 0) {
1104 error = bread(ip->vp, lbase, lblksize, &bp);
1106 } else if (trivial) {
1108 * Even though we are entirely overwriting the buffer
1109 * we may still have to zero it out to avoid a
1110 * mmap/write visibility issue.
1112 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
1113 if ((bp->b_flags & B_CACHE) == 0)
1117 * Partial overwrite, read in any missing bits then
1118 * replace the portion being written.
1120 * (The strategy code will detect zero-fill physical
1121 * blocks for this case).
1123 error = bread(ip->vp, lbase, lblksize, &bp);
1134 * Ok, copy the data in
1136 error = uiomove(bp->b_data + loff, n, uio);
1137 kflags |= NOTE_WRITE;
1145 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
1146 * with IO_SYNC or IO_ASYNC set. These writes
1147 * must be handled as the pageout daemon expects.
1149 if (ioflag & IO_SYNC) {
1151 } else if ((ioflag & IO_DIRECT) && endofblk) {
1153 } else if (ioflag & IO_ASYNC) {
1161 * Cleanup. If we extended the file EOF but failed to write through
1162 * the entire write is a failure and we have to back-up.
1164 if (error && new_eof != old_eof) {
1165 hammer2_truncate_file(ip, old_eof);
1166 } else if (modified) {
1167 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1168 hammer2_update_time(&ip->mtime);
1169 atomic_set_int(&ip->flags, HAMMER2_INODE_MTIME);
1170 ccms_thread_unlock(&ip->topo_cst);
1172 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1173 hammer2_knote(ip->vp, kflags);
1174 vsetisdirty(ip->vp);
1180 * Truncate the size of a file. The inode must not be locked.
1182 * NOTE: Caller handles setting HAMMER2_INODE_MODIFIED
1186 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1188 hammer2_key_t lbase;
1192 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1193 nvtruncbuf(ip->vp, nsize,
1194 nblksize, (int)nsize & (nblksize - 1),
1197 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1199 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1200 ccms_thread_unlock(&ip->topo_cst);
1204 * Extend the size of a file. The inode must not be locked.
1206 * NOTE: Caller handles setting HAMMER2_INODE_MODIFIED
1210 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1212 hammer2_key_t lbase;
1213 hammer2_key_t osize;
1217 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1220 ccms_thread_unlock(&ip->topo_cst);
1223 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1224 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1230 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1235 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1237 hammer2_inode_t *ip;
1238 hammer2_inode_t *dip;
1239 hammer2_chain_t *parent;
1240 hammer2_chain_t *chain;
1241 hammer2_chain_t *ochain;
1242 hammer2_trans_t trans;
1243 hammer2_key_t key_next;
1245 struct namecache *ncp;
1246 const uint8_t *name;
1249 int cache_index = -1;
1252 dip = VTOI(ap->a_dvp);
1253 ncp = ap->a_nch->ncp;
1254 name = ncp->nc_name;
1255 name_len = ncp->nc_nlen;
1256 lhc = hammer2_dirhash(name, name_len);
1259 * Note: In DragonFly the kernel handles '.' and '..'.
1261 parent = hammer2_inode_lock_sh(dip);
1262 chain = hammer2_chain_lookup(&parent, &key_next,
1263 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1264 &cache_index, HAMMER2_LOOKUP_SHARED);
1266 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1267 name_len == chain->data->ipdata.name_len &&
1268 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1271 chain = hammer2_chain_next(&parent, chain, &key_next,
1273 lhc + HAMMER2_DIRHASH_LOMASK,
1274 &cache_index, HAMMER2_LOOKUP_SHARED);
1276 hammer2_inode_unlock_sh(dip, parent);
1279 * If the inode represents a forwarding entry for a hardlink we have
1280 * to locate the actual inode. The original ip is saved for possible
1281 * deconsolidation. (ip) will only be set to non-NULL when we have
1282 * to locate the real file via a hardlink. ip will be referenced but
1283 * not locked in that situation. chain is passed in locked and
1286 * XXX what kind of chain lock?
1289 if (chain && chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
1290 error = hammer2_hardlink_find(dip, &chain, &ochain);
1292 kprintf("hammer2: unable to find hardlink\n");
1294 hammer2_chain_unlock(chain);
1302 * Deconsolidate any hardlink whos nlinks == 1. Ignore errors.
1303 * If an error occurs chain and ip are left alone.
1305 * XXX upgrade shared lock?
1307 if (ochain && chain &&
1308 chain->data->ipdata.nlinks == 1 && !dip->pmp->ronly) {
1309 kprintf("hammer2: need to unconsolidate hardlink for %s\n",
1310 chain->data->ipdata.filename);
1311 /* XXX retain shared lock on dip? (currently not held) */
1312 hammer2_trans_init(&trans, dip->pmp, NULL, 0);
1313 hammer2_hardlink_deconsolidate(&trans, dip, &chain, &ochain);
1314 hammer2_trans_done(&trans);
1318 * Acquire the related vnode
1320 * NOTE: For error processing, only ENOENT resolves the namecache
1321 * entry to NULL, otherwise we just return the error and
1322 * leave the namecache unresolved.
1324 * NOTE: multiple hammer2_inode structures can be aliased to the
1325 * same chain element, for example for hardlinks. This
1326 * use case does not 'reattach' inode associations that
1327 * might already exist, but always allocates a new one.
1329 * WARNING: inode structure is locked exclusively via inode_get
1330 * but chain was locked shared. inode_unlock_ex()
1331 * will handle it properly.
1334 ip = hammer2_inode_get(dip->pmp, dip, chain);
1335 vp = hammer2_igetv(ip, &error);
1338 cache_setvp(ap->a_nch, vp);
1339 } else if (error == ENOENT) {
1340 cache_setvp(ap->a_nch, NULL);
1342 hammer2_inode_unlock_ex(ip, chain);
1345 * The vp should not be released until after we've disposed
1346 * of our locks, because it might cause vop_inactive() to
1353 cache_setvp(ap->a_nch, NULL);
1356 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1357 ("resolve error %d/%p chain %p ap %p\n",
1358 error, ap->a_nch->ncp->nc_vp, chain, ap));
1360 hammer2_chain_drop(ochain);
1366 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1368 hammer2_inode_t *dip;
1369 hammer2_inode_t *ip;
1370 hammer2_chain_t *parent;
1373 dip = VTOI(ap->a_dvp);
1375 if ((ip = dip->pip) == NULL) {
1379 parent = hammer2_inode_lock_ex(ip);
1380 *ap->a_vpp = hammer2_igetv(ip, &error);
1381 hammer2_inode_unlock_ex(ip, parent);
1388 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1390 hammer2_inode_t *dip;
1391 hammer2_inode_t *nip;
1392 hammer2_trans_t trans;
1393 hammer2_chain_t *chain;
1394 struct namecache *ncp;
1395 const uint8_t *name;
1399 dip = VTOI(ap->a_dvp);
1400 if (dip->pmp->ronly)
1403 ncp = ap->a_nch->ncp;
1404 name = ncp->nc_name;
1405 name_len = ncp->nc_nlen;
1407 hammer2_chain_memory_wait(dip->pmp);
1408 hammer2_trans_init(&trans, dip->pmp, NULL, HAMMER2_TRANS_NEWINODE);
1409 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1410 name, name_len, &chain, &error);
1411 chain->inode_reason = 1;
1413 KKASSERT(nip == NULL);
1416 *ap->a_vpp = hammer2_igetv(nip, &error);
1417 hammer2_inode_unlock_ex(nip, chain);
1419 hammer2_trans_done(&trans);
1422 cache_setunresolved(ap->a_nch);
1423 cache_setvp(ap->a_nch, *ap->a_vpp);
1429 * Return the largest contiguous physical disk range for the logical
1430 * request, in bytes.
1432 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
1434 * Basically disabled, the logical buffer write thread has to deal with
1435 * buffers one-at-a-time.
1439 hammer2_vop_bmap(struct vop_bmap_args *ap)
1441 *ap->a_doffsetp = NOOFFSET;
1446 return (EOPNOTSUPP);
1451 hammer2_vop_open(struct vop_open_args *ap)
1453 return vop_stdopen(ap);
1457 * hammer2_vop_advlock { vp, id, op, fl, flags }
1461 hammer2_vop_advlock(struct vop_advlock_args *ap)
1463 hammer2_inode_t *ip = VTOI(ap->a_vp);
1464 hammer2_chain_t *parent;
1467 parent = hammer2_inode_lock_sh(ip);
1468 size = parent->data->ipdata.size;
1469 hammer2_inode_unlock_sh(ip, parent);
1470 return (lf_advlock(ap, &ip->advlock, size));
1476 hammer2_vop_close(struct vop_close_args *ap)
1478 return vop_stdclose(ap);
1482 * hammer2_vop_nlink { nch, dvp, vp, cred }
1484 * Create a hardlink from (vp) to {dvp, nch}.
1488 hammer2_vop_nlink(struct vop_nlink_args *ap)
1490 hammer2_inode_t *fdip; /* target directory to create link in */
1491 hammer2_inode_t *tdip; /* target directory to create link in */
1492 hammer2_inode_t *cdip; /* common parent directory */
1493 hammer2_inode_t *ip; /* inode we are hardlinking to */
1494 hammer2_chain_t *chain;
1495 hammer2_chain_t *fdchain;
1496 hammer2_chain_t *tdchain;
1497 hammer2_chain_t *cdchain;
1498 hammer2_trans_t trans;
1499 struct namecache *ncp;
1500 const uint8_t *name;
1504 tdip = VTOI(ap->a_dvp);
1505 if (tdip->pmp->ronly)
1508 ncp = ap->a_nch->ncp;
1509 name = ncp->nc_name;
1510 name_len = ncp->nc_nlen;
1513 * ip represents the file being hardlinked. The file could be a
1514 * normal file or a hardlink target if it has already been hardlinked.
1515 * If ip is a hardlinked target then ip->pip represents the location
1516 * of the hardlinked target, NOT the location of the hardlink pointer.
1518 * Bump nlinks and potentially also create or move the hardlink
1519 * target in the parent directory common to (ip) and (tdip). The
1520 * consolidation code can modify ip->chain and ip->pip. The
1521 * returned chain is locked.
1523 ip = VTOI(ap->a_vp);
1524 hammer2_chain_memory_wait(ip->pmp);
1525 hammer2_trans_init(&trans, ip->pmp, NULL, HAMMER2_TRANS_NEWINODE);
1528 * The common parent directory must be locked first to avoid deadlocks.
1529 * Also note that fdip and/or tdip might match cdip.
1532 cdip = hammer2_inode_common_parent(fdip, tdip);
1533 cdchain = hammer2_inode_lock_ex(cdip);
1534 fdchain = hammer2_inode_lock_ex(fdip);
1535 tdchain = hammer2_inode_lock_ex(tdip);
1536 chain = hammer2_inode_lock_ex(ip);
1537 error = hammer2_hardlink_consolidate(&trans, ip, &chain,
1543 * Create a directory entry connected to the specified chain.
1544 * The hardlink consolidation code has already adjusted ip->pip
1545 * to the common parent directory containing the actual hardlink
1547 * (which may be different from dip where we created our hardlink
1548 * entry. ip->chain always represents the actual hardlink and not
1549 * any of the pointers to the actual hardlink).
1551 * WARNING! chain can get moved by the connect (indirectly due to
1552 * potential indirect block creation).
1554 error = hammer2_inode_connect(&trans, &chain, 1,
1558 cache_setunresolved(ap->a_nch);
1559 cache_setvp(ap->a_nch, ap->a_vp);
1562 hammer2_inode_unlock_ex(ip, chain);
1563 hammer2_inode_unlock_ex(tdip, tdchain);
1564 hammer2_inode_unlock_ex(fdip, fdchain);
1565 hammer2_inode_unlock_ex(cdip, cdchain);
1566 hammer2_trans_done(&trans);
1572 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1574 * The operating system has already ensured that the directory entry
1575 * does not exist and done all appropriate namespace locking.
1579 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1581 hammer2_inode_t *dip;
1582 hammer2_inode_t *nip;
1583 hammer2_trans_t trans;
1584 hammer2_chain_t *nchain;
1585 struct namecache *ncp;
1586 const uint8_t *name;
1590 dip = VTOI(ap->a_dvp);
1591 if (dip->pmp->ronly)
1594 ncp = ap->a_nch->ncp;
1595 name = ncp->nc_name;
1596 name_len = ncp->nc_nlen;
1597 hammer2_chain_memory_wait(dip->pmp);
1598 hammer2_trans_init(&trans, dip->pmp, NULL, HAMMER2_TRANS_NEWINODE);
1600 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1601 name, name_len, &nchain, &error);
1602 nchain->inode_reason = 2;
1604 KKASSERT(nip == NULL);
1607 *ap->a_vpp = hammer2_igetv(nip, &error);
1608 hammer2_inode_unlock_ex(nip, nchain);
1610 hammer2_trans_done(&trans);
1613 cache_setunresolved(ap->a_nch);
1614 cache_setvp(ap->a_nch, *ap->a_vpp);
1624 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1626 hammer2_inode_t *dip;
1627 hammer2_inode_t *nip;
1628 hammer2_trans_t trans;
1629 hammer2_chain_t *nchain;
1630 struct namecache *ncp;
1631 const uint8_t *name;
1635 dip = VTOI(ap->a_dvp);
1636 if (dip->pmp->ronly)
1639 ncp = ap->a_nch->ncp;
1640 name = ncp->nc_name;
1641 name_len = ncp->nc_nlen;
1642 hammer2_chain_memory_wait(dip->pmp);
1643 hammer2_trans_init(&trans, dip->pmp, NULL, HAMMER2_TRANS_NEWINODE);
1645 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1646 name, name_len, &nchain, &error);
1647 nchain->inode_reason = 3;
1649 KKASSERT(nip == NULL);
1652 *ap->a_vpp = hammer2_igetv(nip, &error);
1653 hammer2_inode_unlock_ex(nip, nchain);
1655 hammer2_trans_done(&trans);
1658 cache_setunresolved(ap->a_nch);
1659 cache_setvp(ap->a_nch, *ap->a_vpp);
1665 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1669 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1671 hammer2_inode_t *dip;
1672 hammer2_inode_t *nip;
1673 hammer2_chain_t *nparent;
1674 hammer2_trans_t trans;
1675 struct namecache *ncp;
1676 const uint8_t *name;
1680 dip = VTOI(ap->a_dvp);
1681 if (dip->pmp->ronly)
1684 ncp = ap->a_nch->ncp;
1685 name = ncp->nc_name;
1686 name_len = ncp->nc_nlen;
1687 hammer2_chain_memory_wait(dip->pmp);
1688 hammer2_trans_init(&trans, dip->pmp, NULL, HAMMER2_TRANS_NEWINODE);
1690 ap->a_vap->va_type = VLNK; /* enforce type */
1692 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1693 name, name_len, &nparent, &error);
1694 nparent->inode_reason = 4;
1696 KKASSERT(nip == NULL);
1698 hammer2_trans_done(&trans);
1701 *ap->a_vpp = hammer2_igetv(nip, &error);
1704 * Build the softlink (~like file data) and finalize the namecache.
1710 hammer2_inode_data_t *nipdata;
1712 nipdata = &nip->chain->data->ipdata;
1713 bytes = strlen(ap->a_target);
1715 if (bytes <= HAMMER2_EMBEDDED_BYTES) {
1716 KKASSERT(nipdata->op_flags &
1717 HAMMER2_OPFLAG_DIRECTDATA);
1718 bcopy(ap->a_target, nipdata->u.data, bytes);
1719 nipdata->size = bytes;
1721 hammer2_inode_unlock_ex(nip, nparent);
1723 hammer2_inode_unlock_ex(nip, nparent);
1724 bzero(&auio, sizeof(auio));
1725 bzero(&aiov, sizeof(aiov));
1726 auio.uio_iov = &aiov;
1727 auio.uio_segflg = UIO_SYSSPACE;
1728 auio.uio_rw = UIO_WRITE;
1729 auio.uio_resid = bytes;
1730 auio.uio_iovcnt = 1;
1731 auio.uio_td = curthread;
1732 aiov.iov_base = ap->a_target;
1733 aiov.iov_len = bytes;
1734 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1735 nipdata = &nip->chain->data->ipdata; /* RELOAD */
1736 /* XXX handle error */
1740 hammer2_inode_unlock_ex(nip, nparent);
1742 hammer2_trans_done(&trans);
1745 * Finalize namecache
1748 cache_setunresolved(ap->a_nch);
1749 cache_setvp(ap->a_nch, *ap->a_vpp);
1750 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1756 * hammer2_vop_nremove { nch, dvp, cred }
1760 hammer2_vop_nremove(struct vop_nremove_args *ap)
1762 hammer2_inode_t *dip;
1763 hammer2_trans_t trans;
1764 struct namecache *ncp;
1765 const uint8_t *name;
1769 dip = VTOI(ap->a_dvp);
1770 if (dip->pmp->ronly)
1773 ncp = ap->a_nch->ncp;
1774 name = ncp->nc_name;
1775 name_len = ncp->nc_nlen;
1777 hammer2_chain_memory_wait(dip->pmp);
1778 hammer2_trans_init(&trans, dip->pmp, NULL, 0);
1779 error = hammer2_unlink_file(&trans, dip, name, name_len,
1780 0, NULL, ap->a_nch);
1781 hammer2_trans_done(&trans);
1783 cache_unlink(ap->a_nch);
1788 * hammer2_vop_nrmdir { nch, dvp, cred }
1792 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1794 hammer2_inode_t *dip;
1795 hammer2_trans_t trans;
1796 struct namecache *ncp;
1797 const uint8_t *name;
1801 dip = VTOI(ap->a_dvp);
1802 if (dip->pmp->ronly)
1805 ncp = ap->a_nch->ncp;
1806 name = ncp->nc_name;
1807 name_len = ncp->nc_nlen;
1809 hammer2_chain_memory_wait(dip->pmp);
1810 hammer2_trans_init(&trans, dip->pmp, NULL, 0);
1811 error = hammer2_unlink_file(&trans, dip, name, name_len,
1812 1, NULL, ap->a_nch);
1813 hammer2_trans_done(&trans);
1815 cache_unlink(ap->a_nch);
1820 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1824 hammer2_vop_nrename(struct vop_nrename_args *ap)
1826 struct namecache *fncp;
1827 struct namecache *tncp;
1828 hammer2_inode_t *cdip;
1829 hammer2_inode_t *fdip;
1830 hammer2_inode_t *tdip;
1831 hammer2_inode_t *ip;
1832 hammer2_chain_t *chain;
1833 hammer2_chain_t *fdchain;
1834 hammer2_chain_t *tdchain;
1835 hammer2_chain_t *cdchain;
1836 hammer2_trans_t trans;
1837 const uint8_t *fname;
1839 const uint8_t *tname;
1844 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1846 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1849 fdip = VTOI(ap->a_fdvp); /* source directory */
1850 tdip = VTOI(ap->a_tdvp); /* target directory */
1852 if (fdip->pmp->ronly)
1855 fncp = ap->a_fnch->ncp; /* entry name in source */
1856 fname = fncp->nc_name;
1857 fname_len = fncp->nc_nlen;
1859 tncp = ap->a_tnch->ncp; /* entry name in target */
1860 tname = tncp->nc_name;
1861 tname_len = tncp->nc_nlen;
1863 hammer2_chain_memory_wait(tdip->pmp);
1864 hammer2_trans_init(&trans, tdip->pmp, NULL, 0);
1867 * ip is the inode being renamed. If this is a hardlink then
1868 * ip represents the actual file and not the hardlink marker.
1870 ip = VTOI(fncp->nc_vp);
1875 * The common parent directory must be locked first to avoid deadlocks.
1876 * Also note that fdip and/or tdip might match cdip.
1878 * WARNING! fdip may not match ip->pip. That is, if the source file
1879 * is already a hardlink then what we are renaming is the
1880 * hardlink pointer, not the hardlink itself. The hardlink
1881 * directory (ip->pip) will already be at a common parent
1884 * Be sure to use ip->pip when finding the common parent
1885 * against tdip or we might accidently move the hardlink
1886 * target into a subdirectory that makes it inaccessible to
1889 cdip = hammer2_inode_common_parent(ip->pip, tdip);
1890 cdchain = hammer2_inode_lock_ex(cdip);
1891 fdchain = hammer2_inode_lock_ex(fdip);
1892 tdchain = hammer2_inode_lock_ex(tdip);
1895 * Keep a tight grip on the inode so the temporary unlinking from
1896 * the source location prior to linking to the target location
1897 * does not cause the chain to be destroyed.
1899 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
1900 * unlinking elements from their directories. Locking
1901 * the nlinks field does not lock the whole inode.
1903 hammer2_inode_ref(ip);
1906 * Remove target if it exists
1908 error = hammer2_unlink_file(&trans, tdip, tname, tname_len,
1909 -1, NULL, ap->a_tnch);
1910 if (error && error != ENOENT)
1912 cache_setunresolved(ap->a_tnch);
1915 * When renaming a hardlinked file we may have to re-consolidate
1916 * the location of the hardlink target. Also adjust nlinks by +1
1917 * to counter-act the unlink below.
1919 * If ip represents a regular file the consolidation code essentially
1920 * does nothing other than return the same locked chain that was
1923 * The returned chain will be locked.
1925 * WARNING! We do not currently have a local copy of ipdata but
1926 * we do use one later remember that it must be reloaded
1927 * on any modification to the inode, including connects.
1929 chain = hammer2_inode_lock_ex(ip);
1930 error = hammer2_hardlink_consolidate(&trans, ip, &chain,
1936 * Disconnect (fdip, fname) from the source directory. This will
1937 * disconnect (ip) if it represents a direct file. If (ip) represents
1938 * a hardlink the HARDLINK pointer object will be removed but the
1939 * hardlink will stay intact.
1941 * Always pass nch as NULL because we intend to reconnect the inode,
1942 * so we don't want hammer2_unlink_file() to rename it to the hidden
1943 * open-but-unlinked directory.
1945 * The target chain may be marked DELETED but will not be destroyed
1946 * since we retain our hold on ip and chain.
1948 error = hammer2_unlink_file(&trans, fdip, fname, fname_len,
1950 KKASSERT(error != EAGAIN);
1955 * Reconnect ip to target directory using chain. Chains cannot
1956 * actually be moved, so this will duplicate the chain in the new
1957 * spot and assign it to the ip, replacing the old chain.
1959 * WARNING: Because recursive locks are allowed and we unlinked the
1960 * file that we have a chain-in-hand for just above, the
1961 * chain might have been delete-duplicated. We must refactor
1964 * WARNING: Chain locks can lock buffer cache buffers, to avoid
1965 * deadlocks we want to unlock before issuing a cache_*()
1966 * op (that might have to lock a vnode).
1968 hammer2_chain_refactor(&chain);
1969 error = hammer2_inode_connect(&trans, &chain, hlink,
1971 tname, tname_len, 0);
1972 chain->inode_reason = 5;
1974 KKASSERT(chain != NULL);
1975 hammer2_inode_repoint(ip, (hlink ? ip->pip : tdip), chain);
1978 hammer2_inode_unlock_ex(ip, chain);
1979 hammer2_inode_unlock_ex(tdip, tdchain);
1980 hammer2_inode_unlock_ex(fdip, fdchain);
1981 hammer2_inode_unlock_ex(cdip, cdchain);
1982 hammer2_inode_drop(ip);
1983 hammer2_trans_done(&trans);
1986 * Issue the namecache update after unlocking all the internal
1987 * hammer structures, otherwise we might deadlock.
1990 cache_rename(ap->a_fnch, ap->a_tnch);
1998 * WARNING: The strategy code cannot safely use hammer2 transactions
1999 * as this can deadlock against vfs_sync's vfsync() call
2000 * if multiple flushes are queued.
2002 static int hammer2_strategy_read(struct vop_strategy_args *ap);
2003 static int hammer2_strategy_write(struct vop_strategy_args *ap);
2004 static void hammer2_strategy_read_callback(hammer2_io_t *dio,
2005 hammer2_chain_t *chain,
2006 void *arg_p, off_t arg_o);
2010 hammer2_vop_strategy(struct vop_strategy_args *ap)
2021 error = hammer2_strategy_read(ap);
2022 ++hammer2_iod_file_read;
2025 error = hammer2_strategy_write(ap);
2026 ++hammer2_iod_file_write;
2029 bp->b_error = error = EINVAL;
2030 bp->b_flags |= B_ERROR;
2040 hammer2_strategy_read(struct vop_strategy_args *ap)
2045 hammer2_inode_t *ip;
2046 hammer2_chain_t *parent;
2047 hammer2_chain_t *chain;
2048 hammer2_key_t key_dummy;
2049 hammer2_key_t lbase;
2050 int cache_index = -1;
2054 ip = VTOI(ap->a_vp);
2055 nbio = push_bio(bio);
2057 lbase = bio->bio_offset;
2059 KKASSERT(((int)lbase & HAMMER2_PBUFMASK) == 0);
2061 parent = hammer2_inode_lock_sh(ip);
2062 chain = hammer2_chain_lookup(&parent, &key_dummy,
2065 HAMMER2_LOOKUP_NODATA |
2066 HAMMER2_LOOKUP_SHARED);
2068 if (chain == NULL) {
2074 bzero(bp->b_data, bp->b_bcount);
2076 } else if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
2078 * Data is embedded in the inode (copy from inode).
2080 hammer2_chain_load_async(chain,
2081 hammer2_strategy_read_callback,
2083 } else if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2085 * Data is on-media, issue device I/O and copy.
2087 * XXX direct-IO shortcut could go here XXX.
2089 if (HAMMER2_DEC_COMP(chain->bref.methods) == HAMMER2_COMP_LZ4) {
2091 * Block compression is determined by bref.methods
2093 hammer2_blockref_t *bref;
2095 bref = &chain->bref;
2096 hammer2_io_breadcb(chain->hmp, bref->data_off,
2098 hammer2_decompress_LZ4_callback,
2099 NULL, nbio, bref->data_off);
2100 /* XXX async read dev blk not protected by chain lk */
2101 hammer2_chain_unlock(chain);
2102 } else if (HAMMER2_DEC_COMP(chain->bref.methods) ==
2103 HAMMER2_COMP_ZLIB) {
2104 hammer2_blockref_t *bref;
2106 bref = &chain->bref;
2107 hammer2_io_breadcb(chain->hmp, bref->data_off,
2109 hammer2_decompress_ZLIB_callback,
2110 NULL, nbio, bref->data_off);
2111 /* XXX async read dev blk not protected by chain lk */
2112 hammer2_chain_unlock(chain);
2114 hammer2_chain_load_async(chain,
2115 hammer2_strategy_read_callback,
2119 panic("READ PATH: hammer2_strategy_read: unknown bref type");
2122 hammer2_inode_unlock_sh(ip, parent);
2127 * Read callback for block that is not compressed.
2131 hammer2_strategy_read_callback(hammer2_io_t *dio, hammer2_chain_t *chain,
2132 void *arg_p, off_t arg_o __unused)
2134 struct bio *nbio = arg_p;
2135 struct buf *bp = nbio->bio_buf;
2139 data = hammer2_io_data(dio, chain->bref.data_off);
2141 data = (void *)chain->data;
2143 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
2145 * Data is embedded in the inode (copy from inode).
2147 bcopy(((hammer2_inode_data_t *)data)->u.data,
2148 bp->b_data, HAMMER2_EMBEDDED_BYTES);
2149 bzero(bp->b_data + HAMMER2_EMBEDDED_BYTES,
2150 bp->b_bcount - HAMMER2_EMBEDDED_BYTES);
2153 hammer2_chain_unlock(chain);
2155 } else if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2157 * Data is on-media, issue device I/O and copy.
2159 * XXX direct-IO shortcut could go here XXX.
2161 KKASSERT(chain->bytes <= bp->b_bcount);
2162 bcopy(data, bp->b_data, chain->bytes);
2163 if (chain->bytes < bp->b_bcount); {
2164 bzero(bp->b_data + chain->bytes,
2165 bp->b_bcount - chain->bytes);
2167 bp->b_flags |= B_NOTMETA;
2170 hammer2_chain_unlock(chain);
2173 /* bqrelse the dio to help stabilize the call to panic() */
2175 hammer2_io_bqrelse(&dio);
2176 panic("hammer2_strategy_read: unknown bref type");
2177 /*hammer2_chain_unlock(chain);*/
2184 hammer2_strategy_write(struct vop_strategy_args *ap)
2186 hammer2_pfsmount_t *pmp;
2189 hammer2_inode_t *ip;
2193 ip = VTOI(ap->a_vp);
2196 hammer2_lwinprog_ref(pmp);
2197 mtx_lock(&pmp->wthread_mtx);
2198 if (TAILQ_EMPTY(&pmp->wthread_bioq.queue)) {
2199 bioq_insert_tail(&pmp->wthread_bioq, ap->a_bio);
2200 mtx_unlock(&pmp->wthread_mtx);
2201 wakeup(&pmp->wthread_bioq);
2203 bioq_insert_tail(&pmp->wthread_bioq, ap->a_bio);
2204 mtx_unlock(&pmp->wthread_mtx);
2206 hammer2_lwinprog_wait(pmp);
2212 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2216 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2218 hammer2_inode_t *ip;
2221 ip = VTOI(ap->a_vp);
2223 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2224 ap->a_fflag, ap->a_cred);
2230 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2233 hammer2_pfsmount_t *pmp;
2237 case (MOUNTCTL_SET_EXPORT):
2238 mp = ap->a_head.a_ops->head.vv_mount;
2241 if (ap->a_ctllen != sizeof(struct export_args))
2244 rc = vfs_export(mp, &pmp->export,
2245 (const struct export_args *)ap->a_ctl);
2248 rc = vop_stdmountctl(ap);
2257 static void filt_hammer2detach(struct knote *kn);
2258 static int filt_hammer2read(struct knote *kn, long hint);
2259 static int filt_hammer2write(struct knote *kn, long hint);
2260 static int filt_hammer2vnode(struct knote *kn, long hint);
2262 static struct filterops hammer2read_filtops =
2263 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2264 NULL, filt_hammer2detach, filt_hammer2read };
2265 static struct filterops hammer2write_filtops =
2266 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2267 NULL, filt_hammer2detach, filt_hammer2write };
2268 static struct filterops hammer2vnode_filtops =
2269 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2270 NULL, filt_hammer2detach, filt_hammer2vnode };
2274 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
2276 struct vnode *vp = ap->a_vp;
2277 struct knote *kn = ap->a_kn;
2279 switch (kn->kn_filter) {
2281 kn->kn_fop = &hammer2read_filtops;
2284 kn->kn_fop = &hammer2write_filtops;
2287 kn->kn_fop = &hammer2vnode_filtops;
2290 return (EOPNOTSUPP);
2293 kn->kn_hook = (caddr_t)vp;
2295 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2301 filt_hammer2detach(struct knote *kn)
2303 struct vnode *vp = (void *)kn->kn_hook;
2305 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2309 filt_hammer2read(struct knote *kn, long hint)
2311 struct vnode *vp = (void *)kn->kn_hook;
2312 hammer2_inode_t *ip = VTOI(vp);
2315 if (hint == NOTE_REVOKE) {
2316 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2319 off = ip->size - kn->kn_fp->f_offset;
2320 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2321 if (kn->kn_sfflags & NOTE_OLDAPI)
2323 return (kn->kn_data != 0);
2328 filt_hammer2write(struct knote *kn, long hint)
2330 if (hint == NOTE_REVOKE)
2331 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2337 filt_hammer2vnode(struct knote *kn, long hint)
2339 if (kn->kn_sfflags & hint)
2340 kn->kn_fflags |= hint;
2341 if (hint == NOTE_REVOKE) {
2342 kn->kn_flags |= (EV_EOF | EV_NODATA);
2345 return (kn->kn_fflags != 0);
2353 hammer2_vop_markatime(struct vop_markatime_args *ap)
2355 hammer2_inode_t *ip;
2368 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2372 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2374 error = hammer2_vop_kqfilter(ap);
2381 struct vop_ops hammer2_vnode_vops = {
2382 .vop_default = vop_defaultop,
2383 .vop_fsync = hammer2_vop_fsync,
2384 .vop_getpages = vop_stdgetpages,
2385 .vop_putpages = vop_stdputpages,
2386 .vop_access = hammer2_vop_access,
2387 .vop_advlock = hammer2_vop_advlock,
2388 .vop_close = hammer2_vop_close,
2389 .vop_nlink = hammer2_vop_nlink,
2390 .vop_ncreate = hammer2_vop_ncreate,
2391 .vop_nsymlink = hammer2_vop_nsymlink,
2392 .vop_nremove = hammer2_vop_nremove,
2393 .vop_nrmdir = hammer2_vop_nrmdir,
2394 .vop_nrename = hammer2_vop_nrename,
2395 .vop_getattr = hammer2_vop_getattr,
2396 .vop_setattr = hammer2_vop_setattr,
2397 .vop_readdir = hammer2_vop_readdir,
2398 .vop_readlink = hammer2_vop_readlink,
2399 .vop_getpages = vop_stdgetpages,
2400 .vop_putpages = vop_stdputpages,
2401 .vop_read = hammer2_vop_read,
2402 .vop_write = hammer2_vop_write,
2403 .vop_open = hammer2_vop_open,
2404 .vop_inactive = hammer2_vop_inactive,
2405 .vop_reclaim = hammer2_vop_reclaim,
2406 .vop_nresolve = hammer2_vop_nresolve,
2407 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2408 .vop_nmkdir = hammer2_vop_nmkdir,
2409 .vop_nmknod = hammer2_vop_nmknod,
2410 .vop_ioctl = hammer2_vop_ioctl,
2411 .vop_mountctl = hammer2_vop_mountctl,
2412 .vop_bmap = hammer2_vop_bmap,
2413 .vop_strategy = hammer2_vop_strategy,
2414 .vop_kqfilter = hammer2_vop_kqfilter
2417 struct vop_ops hammer2_spec_vops = {
2418 .vop_default = vop_defaultop,
2419 .vop_fsync = hammer2_vop_fsync,
2420 .vop_read = vop_stdnoread,
2421 .vop_write = vop_stdnowrite,
2422 .vop_access = hammer2_vop_access,
2423 .vop_close = hammer2_vop_close,
2424 .vop_markatime = hammer2_vop_markatime,
2425 .vop_getattr = hammer2_vop_getattr,
2426 .vop_inactive = hammer2_vop_inactive,
2427 .vop_reclaim = hammer2_vop_reclaim,
2428 .vop_setattr = hammer2_vop_setattr
2431 struct vop_ops hammer2_fifo_vops = {
2432 .vop_default = fifo_vnoperate,
2433 .vop_fsync = hammer2_vop_fsync,
2435 .vop_read = hammer2_vop_fiforead,
2436 .vop_write = hammer2_vop_fifowrite,
2438 .vop_access = hammer2_vop_access,
2440 .vop_close = hammer2_vop_fifoclose,
2442 .vop_markatime = hammer2_vop_markatime,
2443 .vop_getattr = hammer2_vop_getattr,
2444 .vop_inactive = hammer2_vop_inactive,
2445 .vop_reclaim = hammer2_vop_reclaim,
2446 .vop_setattr = hammer2_vop_setattr,
2447 .vop_kqfilter = hammer2_vop_fifokqfilter