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>
58 #include "hammer2_lz4.h"
60 #include "zlib/hammer2_zlib.h"
62 #define ZFOFFSET (-2LL)
64 static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio,
66 static int hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
67 int ioflag, int seqcount);
68 static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize);
69 static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize);
70 static void hammer2_decompress_LZ4_callback(struct bio *bio);
71 static void hammer2_decompress_ZLIB_callback(struct bio *bio);
73 struct objcache *cache_buffer_read;
74 struct objcache *cache_buffer_write;
77 * Callback used in read path in case that a block is compressed with LZ4.
81 hammer2_decompress_LZ4_callback(struct bio *bio)
83 struct buf *bp = bio->bio_buf;
89 * If BIO_DONE is already set the device buffer was already
90 * fully valid (B_CACHE). If it is not set then I/O was issued
91 * and we have to run I/O completion as the last bio.
93 * Nobody is waiting for our device I/O to complete, we are
94 * responsible for bqrelse()ing it which means we also have to do
95 * the equivalent of biowait() and clear BIO_DONE (which breadcb()
98 * Any preexisting device buffer should match the requested size,
99 * but due to bigblock recycling and other factors there is some
100 * fragility there, so we assert that the device buffer covers
103 if ((bio->bio_flags & BIO_DONE) == 0)
105 bio->bio_flags &= ~(BIO_DONE | BIO_SYNC);
107 obio = bio->bio_caller_info1.ptr;
109 loff = obio->bio_caller_info3.value;
111 if (bp->b_flags & B_ERROR) {
112 obp->b_flags |= B_ERROR;
113 obp->b_error = bp->b_error;
114 } else if (obio->bio_caller_info2.index &&
115 obio->bio_caller_info1.uvalue32 !=
116 crc32(bp->b_data, bp->b_bufsize)) {
117 obp->b_flags |= B_ERROR;
120 KKASSERT(obp->b_bufsize <= 65536);
123 char *compressed_buffer;
124 int *compressed_size;
126 buffer = bp->b_data + loff;
127 compressed_size = (int*)buffer;
128 compressed_buffer = objcache_get(cache_buffer_read, M_INTWAIT);
129 KKASSERT((unsigned int)*compressed_size <= 65536);
130 int result = LZ4_decompress_safe(&buffer[sizeof(int)],
131 compressed_buffer, *compressed_size, obp->b_bufsize);
133 kprintf("READ PATH: Error during decompression."
134 "bio %016jx/%d loff=%d\n",
135 (intmax_t)bio->bio_offset, bio->bio_buf->b_bufsize, loff);
136 /* make sure it isn't random garbage */
137 bzero(compressed_buffer, obp->b_bufsize);
139 KKASSERT(result <= obp->b_bufsize);
140 bcopy(compressed_buffer, obp->b_data, obp->b_bufsize);
141 if (result < obp->b_bufsize)
142 bzero(obp->b_data + result, obp->b_bufsize - result);
143 objcache_put(cache_buffer_read, compressed_buffer);
145 obp->b_flags |= B_AGE;
152 * Callback used in read path in case that a block is compressed with ZLIB.
153 * It is almost identical to LZ4 callback, so in theory they can be unified,
154 * but we didn't want to make changes in bio structure for that.
158 hammer2_decompress_ZLIB_callback(struct bio *bio)
160 struct buf *bp = bio->bio_buf;
166 * If BIO_DONE is already set the device buffer was already
167 * fully valid (B_CACHE). If it is not set then I/O was issued
168 * and we have to run I/O completion as the last bio.
170 * Nobody is waiting for our device I/O to complete, we are
171 * responsible for bqrelse()ing it which means we also have to do
172 * the equivalent of biowait() and clear BIO_DONE (which breadcb()
175 * Any preexisting device buffer should match the requested size,
176 * but due to bigblock recycling and other factors there is some
177 * fragility there, so we assert that the device buffer covers
180 if ((bio->bio_flags & BIO_DONE) == 0)
182 bio->bio_flags &= ~(BIO_DONE | BIO_SYNC);
184 obio = bio->bio_caller_info1.ptr;
186 loff = obio->bio_caller_info3.value;
188 if (bp->b_flags & B_ERROR) {
189 obp->b_flags |= B_ERROR;
190 obp->b_error = bp->b_error;
191 } else if (obio->bio_caller_info2.index &&
192 obio->bio_caller_info1.uvalue32 !=
193 crc32(bp->b_data, bp->b_bufsize)) {
194 obp->b_flags |= B_ERROR;
197 KKASSERT(obp->b_bufsize <= 65536);
200 char *compressed_buffer;
203 z_stream strm_decompress;
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 buffer = bp->b_data + loff;
214 compressed_buffer = objcache_get(cache_buffer_read, M_INTWAIT);
215 strm_decompress.next_in = buffer;
217 /* XXX supply proper size, subset of device bp */
218 strm_decompress.avail_in = bp->b_bufsize - loff;
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 int 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);
242 hammer2_knote(struct vnode *vp, int flags)
245 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
249 * Last reference to a vnode is going away but it is still cached.
253 hammer2_vop_inactive(struct vop_inactive_args *ap)
256 hammer2_chain_t *parent;
271 * Detect updates to the embedded data which may be synchronized by
272 * the strategy code. Simply mark the inode modified so it gets
273 * picked up by our normal flush.
275 parent = hammer2_inode_lock_ex(ip);
279 * Check for deleted inodes and recycle immediately.
281 if (parent->flags & HAMMER2_CHAIN_DELETED) {
282 hammer2_inode_unlock_ex(ip, parent);
285 hammer2_inode_unlock_ex(ip, parent);
291 * Reclaim a vnode so that it can be reused; after the inode is
292 * disassociated, the filesystem must manage it alone.
296 hammer2_vop_reclaim(struct vop_reclaim_args *ap)
298 hammer2_chain_t *chain;
301 hammer2_trans_t trans;
311 * Set SUBMODIFIED so we can detect and propagate the DESTROYED
312 * bit in the flush code.
314 * ip->chain might be stale, correct it before checking as older
315 * versions of the chain are likely marked deleted even if the
316 * file hasn't been. XXX ip->chain should never be stale on
319 chain = hammer2_inode_lock_ex(ip);
321 if (chain->next_parent)
322 kprintf("RECLAIM DUPLINKED IP: %p ip->ch=%p ch=%p np=%p\n",
323 ip, ip->chain, chain, chain->next_parent);
327 * The final close of a deleted file or directory marks it for
328 * destruction. The DESTROYED flag allows the flusher to shortcut
329 * any modified blocks still unflushed (that is, just ignore them).
331 * HAMMER2 usually does not try to optimize the freemap by returning
332 * deleted blocks to it as it does not usually know how many snapshots
333 * might be referencing portions of the file/dir. XXX TODO.
335 * XXX TODO - However, any modified file as-of when a snapshot is made
336 * cannot use this optimization as some of the modifications
337 * may wind up being part of the snapshot.
341 if (chain->flags & HAMMER2_CHAIN_DELETED) {
342 KKASSERT(chain->flags & HAMMER2_CHAIN_DELETED);
343 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROYED |
344 HAMMER2_CHAIN_SUBMODIFIED);
348 * XXX chains will be flushed on sync, no need to do it here.
350 if (chain->flags & (HAMMER2_CHAIN_MODIFIED |
351 HAMMER2_CHAIN_DELETED |
352 HAMMER2_CHAIN_SUBMODIFIED)) {
353 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_ISFLUSH);
354 hammer2_chain_flush(&trans, chain);
355 hammer2_trans_done(&trans);
358 hammer2_inode_unlock_ex(ip, chain); /* unlock */
359 hammer2_inode_drop(ip); /* vp ref */
360 /* chain no longer referenced */
361 /* chain = NULL; not needed */
364 * XXX handle background sync when ip dirty, kernel will no longer
365 * notify us regarding this inode because there is no longer a
366 * vnode attached to it.
374 hammer2_vop_fsync(struct vop_fsync_args *ap)
377 hammer2_trans_t trans;
378 hammer2_chain_t *chain;
385 * WARNING: The vfsync interacts with the buffer cache and might
386 * block, we can't hold the inode lock and we can't
387 * have a flush transaction pending.
389 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_ISFLUSH);
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);
402 if (ip->flags & (HAMMER2_INODE_RESIZED|HAMMER2_INODE_MTIME))
403 hammer2_inode_fsync(&trans, ip, &chain);
405 if (ap->a_flags & VOP_FSYNC_SYSCALL) {
406 hammer2_chain_flush(&trans, chain);
408 hammer2_inode_unlock_ex(ip, chain);
409 hammer2_trans_done(&trans);
416 hammer2_vop_access(struct vop_access_args *ap)
418 hammer2_inode_t *ip = VTOI(ap->a_vp);
419 hammer2_inode_data_t *ipdata;
420 hammer2_chain_t *chain;
425 chain = hammer2_inode_lock_sh(ip);
426 ipdata = &chain->data->ipdata;
427 uid = hammer2_to_unix_xid(&ipdata->uid);
428 gid = hammer2_to_unix_xid(&ipdata->gid);
429 error = vop_helper_access(ap, uid, gid, ipdata->mode, ipdata->uflags);
430 hammer2_inode_unlock_sh(ip, chain);
437 hammer2_vop_getattr(struct vop_getattr_args *ap)
439 hammer2_inode_data_t *ipdata;
440 hammer2_chain_t *chain;
441 hammer2_pfsmount_t *pmp;
452 chain = hammer2_inode_lock_sh(ip);
453 ipdata = &chain->data->ipdata;
455 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
456 vap->va_fileid = ipdata->inum;
457 vap->va_mode = ipdata->mode;
458 vap->va_nlink = ipdata->nlinks;
459 vap->va_uid = hammer2_to_unix_xid(&ipdata->uid);
460 vap->va_gid = hammer2_to_unix_xid(&ipdata->gid);
463 vap->va_size = ip->size; /* protected by shared lock */
464 vap->va_blocksize = HAMMER2_PBUFSIZE;
465 vap->va_flags = ipdata->uflags;
466 hammer2_time_to_timespec(ipdata->ctime, &vap->va_ctime);
467 hammer2_time_to_timespec(ipdata->mtime, &vap->va_mtime);
468 hammer2_time_to_timespec(ipdata->mtime, &vap->va_atime);
470 vap->va_bytes = vap->va_size; /* XXX */
471 vap->va_type = hammer2_get_vtype(chain);
473 vap->va_uid_uuid = ipdata->uid;
474 vap->va_gid_uuid = ipdata->gid;
475 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
478 hammer2_inode_unlock_sh(ip, chain);
485 hammer2_vop_setattr(struct vop_setattr_args *ap)
487 hammer2_inode_data_t *ipdata;
489 hammer2_chain_t *chain;
490 hammer2_trans_t trans;
500 hammer2_update_time(&ctime);
507 hammer2_chain_memory_wait(ip->pmp);
508 hammer2_trans_init(&trans, ip->pmp, 0);
509 chain = hammer2_inode_lock_ex(ip);
510 ipdata = &chain->data->ipdata;
513 if (vap->va_flags != VNOVAL) {
516 flags = ipdata->uflags;
517 error = vop_helper_setattr_flags(&flags, vap->va_flags,
518 hammer2_to_unix_xid(&ipdata->uid),
521 if (ipdata->uflags != flags) {
522 ipdata = hammer2_chain_modify_ip(&trans, ip,
524 ipdata->uflags = flags;
525 ipdata->ctime = ctime;
526 kflags |= NOTE_ATTRIB;
528 if (ipdata->uflags & (IMMUTABLE | APPEND)) {
535 if (ipdata->uflags & (IMMUTABLE | APPEND)) {
539 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
540 mode_t cur_mode = ipdata->mode;
541 uid_t cur_uid = hammer2_to_unix_xid(&ipdata->uid);
542 gid_t cur_gid = hammer2_to_unix_xid(&ipdata->gid);
546 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
548 &cur_uid, &cur_gid, &cur_mode);
550 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
551 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
552 if (bcmp(&uuid_uid, &ipdata->uid, sizeof(uuid_uid)) ||
553 bcmp(&uuid_gid, &ipdata->gid, sizeof(uuid_gid)) ||
554 ipdata->mode != cur_mode
556 ipdata = hammer2_chain_modify_ip(&trans, ip,
558 ipdata->uid = uuid_uid;
559 ipdata->gid = uuid_gid;
560 ipdata->mode = cur_mode;
561 ipdata->ctime = ctime;
563 kflags |= NOTE_ATTRIB;
570 if (vap->va_size != VNOVAL && ip->size != vap->va_size) {
573 if (vap->va_size == ip->size)
575 hammer2_inode_unlock_ex(ip, chain);
576 if (vap->va_size < ip->size) {
577 hammer2_truncate_file(ip, vap->va_size);
579 hammer2_extend_file(ip, vap->va_size);
581 chain = hammer2_inode_lock_ex(ip);
582 ipdata = &chain->data->ipdata; /* RELOAD */
591 /* atime not supported */
592 if (vap->va_atime.tv_sec != VNOVAL) {
593 ipdata = hammer2_chain_modify_ip(&trans, ip, &chain, 0);
594 ipdata->atime = hammer2_timespec_to_time(&vap->va_atime);
595 kflags |= NOTE_ATTRIB;
598 if (vap->va_mtime.tv_sec != VNOVAL) {
599 ipdata = hammer2_chain_modify_ip(&trans, ip, &chain, 0);
600 ipdata->mtime = hammer2_timespec_to_time(&vap->va_mtime);
601 kflags |= NOTE_ATTRIB;
603 if (vap->va_mode != (mode_t)VNOVAL) {
604 mode_t cur_mode = ipdata->mode;
605 uid_t cur_uid = hammer2_to_unix_xid(&ipdata->uid);
606 gid_t cur_gid = hammer2_to_unix_xid(&ipdata->gid);
608 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
609 cur_uid, cur_gid, &cur_mode);
610 if (error == 0 && ipdata->mode != cur_mode) {
611 ipdata = hammer2_chain_modify_ip(&trans, ip, &chain, 0);
612 ipdata->mode = cur_mode;
613 ipdata->ctime = ctime;
614 kflags |= NOTE_ATTRIB;
619 * If a truncation occurred we must call inode_fsync() now in order
620 * to trim the related data chains, otherwise a later expansion can
623 hammer2_inode_fsync(&trans, ip, &chain);
625 hammer2_inode_unlock_ex(ip, chain);
626 hammer2_trans_done(&trans);
632 hammer2_vop_readdir(struct vop_readdir_args *ap)
634 hammer2_inode_data_t *ipdata;
636 hammer2_inode_t *xip;
637 hammer2_chain_t *parent;
638 hammer2_chain_t *chain;
639 hammer2_chain_t *xchain;
641 hammer2_key_t key_next;
647 int cache_index = -1;
655 saveoff = uio->uio_offset;
658 * Setup cookies directory entry cookies if requested
660 if (ap->a_ncookies) {
661 ncookies = uio->uio_resid / 16 + 1;
664 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
671 parent = hammer2_inode_lock_sh(ip);
672 ipdata = &parent->data->ipdata;
675 * Handle artificial entries. To ensure that only positive 64 bit
676 * quantities are returned to userland we always strip off bit 63.
677 * The hash code is designed such that codes 0x0000-0x7FFF are not
678 * used, allowing us to use these codes for articial entries.
680 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
681 * allow '..' to cross the mount point into (e.g.) the super-root.
684 chain = (void *)(intptr_t)-1; /* non-NULL for early goto done case */
687 inum = ipdata->inum & HAMMER2_DIRHASH_USERMSK;
688 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
692 cookies[cookie_index] = saveoff;
695 if (cookie_index == ncookies)
701 * Be careful with lockorder when accessing ".."
703 * (ip is the current dir. xip is the parent dir).
705 inum = ipdata->inum & HAMMER2_DIRHASH_USERMSK;
706 while (ip->pip != NULL && ip != ip->pmp->iroot) {
708 hammer2_inode_ref(xip);
709 hammer2_inode_unlock_sh(ip, parent);
710 xchain = hammer2_inode_lock_sh(xip);
711 parent = hammer2_inode_lock_sh(ip);
712 hammer2_inode_drop(xip);
713 if (xip == ip->pip) {
714 inum = xchain->data->ipdata.inum &
715 HAMMER2_DIRHASH_USERMSK;
716 hammer2_inode_unlock_sh(xip, xchain);
719 hammer2_inode_unlock_sh(xip, xchain);
721 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
725 cookies[cookie_index] = saveoff;
728 if (cookie_index == ncookies)
732 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
735 * parent is the inode chain, already locked for us. Don't
736 * double lock shared locks as this will screw up upgrades.
741 chain = hammer2_chain_lookup(&parent, &key_next, lkey, lkey,
742 &cache_index, HAMMER2_LOOKUP_SHARED);
744 chain = hammer2_chain_lookup(&parent, &key_next,
745 lkey, (hammer2_key_t)-1,
747 HAMMER2_LOOKUP_SHARED);
750 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
751 dtype = hammer2_get_dtype(chain);
752 saveoff = chain->bref.key & HAMMER2_DIRHASH_USERMSK;
753 r = vop_write_dirent(&error, uio,
754 chain->data->ipdata.inum &
755 HAMMER2_DIRHASH_USERMSK,
757 chain->data->ipdata.name_len,
758 chain->data->ipdata.filename);
762 cookies[cookie_index] = saveoff;
765 /* XXX chain error */
766 kprintf("bad chain type readdir %d\n",
771 * Keys may not be returned in order so once we have a
772 * placemarker (chain) the scan must allow the full range
773 * or some entries will be missed.
775 chain = hammer2_chain_next(&parent, chain, &key_next,
776 key_next, (hammer2_key_t)-1,
777 &cache_index, HAMMER2_LOOKUP_SHARED);
779 saveoff = (chain->bref.key &
780 HAMMER2_DIRHASH_USERMSK) + 1;
782 saveoff = (hammer2_key_t)-1;
784 if (cookie_index == ncookies)
788 hammer2_chain_unlock(chain);
790 hammer2_inode_unlock_sh(ip, parent);
792 *ap->a_eofflag = (chain == NULL);
793 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
794 if (error && cookie_index == 0) {
796 kfree(cookies, M_TEMP);
798 *ap->a_cookies = NULL;
802 *ap->a_ncookies = cookie_index;
803 *ap->a_cookies = cookies;
810 * hammer2_vop_readlink { vp, uio, cred }
814 hammer2_vop_readlink(struct vop_readlink_args *ap)
821 if (vp->v_type != VLNK)
825 error = hammer2_read_file(ip, ap->a_uio, 0);
831 hammer2_vop_read(struct vop_read_args *ap)
841 * Read operations supported on this vnode?
844 if (vp->v_type != VREG)
854 seqcount = ap->a_ioflag >> 16;
855 bigread = (uio->uio_resid > 100 * 1024 * 1024);
857 error = hammer2_read_file(ip, uio, seqcount);
863 hammer2_vop_write(struct vop_write_args *ap)
866 hammer2_trans_t trans;
875 * Read operations supported on this vnode?
878 if (vp->v_type != VREG)
890 seqcount = ap->a_ioflag >> 16;
891 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
894 * Check resource limit
896 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
897 uio->uio_offset + uio->uio_resid >
898 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
899 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
903 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
906 * The transaction interlocks against flushes initiations
907 * (note: but will run concurrently with the actual flush).
909 hammer2_trans_init(&trans, ip->pmp, 0);
910 error = hammer2_write_file(ip, uio, ap->a_ioflag, seqcount);
911 hammer2_trans_done(&trans);
917 * Perform read operations on a file or symlink given an UNLOCKED
920 * The passed ip is not locked.
924 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
935 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
937 ccms_thread_unlock(&ip->topo_cst);
939 while (uio->uio_resid > 0 && uio->uio_offset < size) {
946 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
949 error = cluster_read(ip->vp, leof, lbase, lblksize,
950 uio->uio_resid, seqcount * BKVASIZE,
955 loff = (int)(uio->uio_offset - lbase);
957 if (n > uio->uio_resid)
959 if (n > size - uio->uio_offset)
960 n = (int)(size - uio->uio_offset);
961 bp->b_flags |= B_AGE;
962 uiomove((char *)bp->b_data + loff, n, uio);
969 * Write to the file represented by the inode via the logical buffer cache.
970 * The inode may represent a regular file or a symlink.
972 * The inode must not be locked.
976 hammer2_write_file(hammer2_inode_t *ip,
977 struct uio *uio, int ioflag, int seqcount)
979 hammer2_key_t old_eof;
980 hammer2_key_t new_eof;
989 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
990 if (ioflag & IO_APPEND)
991 uio->uio_offset = ip->size;
993 ccms_thread_unlock(&ip->topo_cst);
996 * Extend the file if necessary. If the write fails at some point
997 * we will truncate it back down to cover as much as we were able
1000 * Doing this now makes it easier to calculate buffer sizes in
1007 if (uio->uio_offset + uio->uio_resid > old_eof) {
1008 new_eof = uio->uio_offset + uio->uio_resid;
1010 hammer2_extend_file(ip, new_eof);
1011 kflags |= NOTE_EXTEND;
1019 while (uio->uio_resid > 0) {
1020 hammer2_key_t lbase;
1028 * Don't allow the buffer build to blow out the buffer
1031 if ((ioflag & IO_RECURSE) == 0)
1032 bwillwrite(HAMMER2_PBUFSIZE);
1035 * This nominally tells us how much we can cluster and
1036 * what the logical buffer size needs to be. Currently
1037 * we don't try to cluster the write and just handle one
1040 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
1042 loff = (int)(uio->uio_offset - lbase);
1044 KKASSERT(lblksize <= 65536);
1047 * Calculate bytes to copy this transfer and whether the
1048 * copy completely covers the buffer or not.
1051 n = lblksize - loff;
1052 if (n > uio->uio_resid) {
1054 if (loff == lbase && uio->uio_offset + n == new_eof)
1066 if (uio->uio_segflg == UIO_NOCOPY) {
1068 * Issuing a write with the same data backing the
1069 * buffer. Instantiate the buffer to collect the
1070 * backing vm pages, then read-in any missing bits.
1072 * This case is used by vop_stdputpages().
1074 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
1075 if ((bp->b_flags & B_CACHE) == 0) {
1077 error = bread(ip->vp, lbase, lblksize, &bp);
1079 } else if (trivial) {
1081 * Even though we are entirely overwriting the buffer
1082 * we may still have to zero it out to avoid a
1083 * mmap/write visibility issue.
1085 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
1086 if ((bp->b_flags & B_CACHE) == 0)
1090 * Partial overwrite, read in any missing bits then
1091 * replace the portion being written.
1093 * (The strategy code will detect zero-fill physical
1094 * blocks for this case).
1096 error = bread(ip->vp, lbase, lblksize, &bp);
1107 * Ok, copy the data in
1109 error = uiomove(bp->b_data + loff, n, uio);
1110 kflags |= NOTE_WRITE;
1118 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
1119 * with IO_SYNC or IO_ASYNC set. These writes
1120 * must be handled as the pageout daemon expects.
1122 if (ioflag & IO_SYNC) {
1124 } else if ((ioflag & IO_DIRECT) && endofblk) {
1126 } else if (ioflag & IO_ASYNC) {
1134 * Cleanup. If we extended the file EOF but failed to write through
1135 * the entire write is a failure and we have to back-up.
1137 if (error && new_eof != old_eof) {
1138 hammer2_truncate_file(ip, old_eof);
1139 } else if (modified) {
1140 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1141 hammer2_update_time(&ip->mtime);
1142 atomic_set_int(&ip->flags, HAMMER2_INODE_MTIME);
1143 ccms_thread_unlock(&ip->topo_cst);
1145 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1146 hammer2_knote(ip->vp, kflags);
1152 * Truncate the size of a file. The inode must not be locked.
1156 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1158 hammer2_key_t lbase;
1162 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1163 nvtruncbuf(ip->vp, nsize,
1164 nblksize, (int)nsize & (nblksize - 1),
1167 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1169 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1170 ccms_thread_unlock(&ip->topo_cst);
1174 * Extend the size of a file. The inode must not be locked.
1178 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1180 hammer2_key_t lbase;
1181 hammer2_key_t osize;
1185 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1188 ccms_thread_unlock(&ip->topo_cst);
1191 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1192 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1198 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1203 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1205 hammer2_inode_t *ip;
1206 hammer2_inode_t *dip;
1207 hammer2_chain_t *parent;
1208 hammer2_chain_t *chain;
1209 hammer2_chain_t *ochain;
1210 hammer2_trans_t trans;
1211 hammer2_key_t key_next;
1213 struct namecache *ncp;
1214 const uint8_t *name;
1217 int cache_index = -1;
1220 dip = VTOI(ap->a_dvp);
1221 ncp = ap->a_nch->ncp;
1222 name = ncp->nc_name;
1223 name_len = ncp->nc_nlen;
1224 lhc = hammer2_dirhash(name, name_len);
1227 * Note: In DragonFly the kernel handles '.' and '..'.
1229 parent = hammer2_inode_lock_sh(dip);
1230 chain = hammer2_chain_lookup(&parent, &key_next,
1231 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1232 &cache_index, HAMMER2_LOOKUP_SHARED);
1234 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1235 name_len == chain->data->ipdata.name_len &&
1236 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1239 chain = hammer2_chain_next(&parent, chain, &key_next,
1241 lhc + HAMMER2_DIRHASH_LOMASK,
1242 &cache_index, HAMMER2_LOOKUP_SHARED);
1244 hammer2_inode_unlock_sh(dip, parent);
1247 * If the inode represents a forwarding entry for a hardlink we have
1248 * to locate the actual inode. The original ip is saved for possible
1249 * deconsolidation. (ip) will only be set to non-NULL when we have
1250 * to locate the real file via a hardlink. ip will be referenced but
1251 * not locked in that situation. chain is passed in locked and
1254 * XXX what kind of chain lock?
1257 if (chain && chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
1258 error = hammer2_hardlink_find(dip, &chain, &ochain);
1260 kprintf("hammer2: unable to find hardlink\n");
1262 hammer2_chain_unlock(chain);
1270 * Deconsolidate any hardlink whos nlinks == 1. Ignore errors.
1271 * If an error occurs chain and ip are left alone.
1273 * XXX upgrade shared lock?
1275 if (ochain && chain &&
1276 chain->data->ipdata.nlinks == 1 && !dip->pmp->ronly) {
1277 kprintf("hammer2: need to unconsolidate hardlink for %s\n",
1278 chain->data->ipdata.filename);
1279 /* XXX retain shared lock on dip? (currently not held) */
1280 hammer2_trans_init(&trans, dip->pmp, 0);
1281 hammer2_hardlink_deconsolidate(&trans, dip, &chain, &ochain);
1282 hammer2_trans_done(&trans);
1286 * Acquire the related vnode
1288 * NOTE: For error processing, only ENOENT resolves the namecache
1289 * entry to NULL, otherwise we just return the error and
1290 * leave the namecache unresolved.
1292 * NOTE: multiple hammer2_inode structures can be aliased to the
1293 * same chain element, for example for hardlinks. This
1294 * use case does not 'reattach' inode associations that
1295 * might already exist, but always allocates a new one.
1297 * WARNING: inode structure is locked exclusively via inode_get
1298 * but chain was locked shared. inode_unlock_ex()
1299 * will handle it properly.
1302 ip = hammer2_inode_get(dip->pmp, dip, chain);
1303 vp = hammer2_igetv(ip, &error);
1306 cache_setvp(ap->a_nch, vp);
1307 } else if (error == ENOENT) {
1308 cache_setvp(ap->a_nch, NULL);
1310 hammer2_inode_unlock_ex(ip, chain);
1313 * The vp should not be released until after we've disposed
1314 * of our locks, because it might cause vop_inactive() to
1321 cache_setvp(ap->a_nch, NULL);
1324 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1325 ("resolve error %d/%p chain %p ap %p\n",
1326 error, ap->a_nch->ncp->nc_vp, chain, ap));
1328 hammer2_chain_drop(ochain);
1334 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1336 hammer2_inode_t *dip;
1337 hammer2_inode_t *ip;
1338 hammer2_chain_t *parent;
1341 dip = VTOI(ap->a_dvp);
1343 if ((ip = dip->pip) == NULL) {
1347 parent = hammer2_inode_lock_ex(ip);
1348 *ap->a_vpp = hammer2_igetv(ip, &error);
1349 hammer2_inode_unlock_ex(ip, parent);
1356 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1358 hammer2_inode_t *dip;
1359 hammer2_inode_t *nip;
1360 hammer2_trans_t trans;
1361 hammer2_chain_t *chain;
1362 struct namecache *ncp;
1363 const uint8_t *name;
1367 dip = VTOI(ap->a_dvp);
1368 if (dip->pmp->ronly)
1371 ncp = ap->a_nch->ncp;
1372 name = ncp->nc_name;
1373 name_len = ncp->nc_nlen;
1375 hammer2_chain_memory_wait(dip->pmp);
1376 hammer2_trans_init(&trans, dip->pmp, 0);
1377 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1378 name, name_len, &chain, &error);
1380 KKASSERT(nip == NULL);
1383 *ap->a_vpp = hammer2_igetv(nip, &error);
1384 hammer2_inode_unlock_ex(nip, chain);
1386 hammer2_trans_done(&trans);
1389 cache_setunresolved(ap->a_nch);
1390 cache_setvp(ap->a_nch, *ap->a_vpp);
1396 * Return the largest contiguous physical disk range for the logical
1397 * request, in bytes.
1399 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
1401 * Basically disabled, the logical buffer write thread has to deal with
1402 * buffers one-at-a-time.
1406 hammer2_vop_bmap(struct vop_bmap_args *ap)
1408 *ap->a_doffsetp = NOOFFSET;
1413 return (EOPNOTSUPP);
1418 hammer2_vop_open(struct vop_open_args *ap)
1420 return vop_stdopen(ap);
1424 * hammer2_vop_advlock { vp, id, op, fl, flags }
1428 hammer2_vop_advlock(struct vop_advlock_args *ap)
1430 hammer2_inode_t *ip = VTOI(ap->a_vp);
1431 hammer2_chain_t *parent;
1434 parent = hammer2_inode_lock_sh(ip);
1435 size = parent->data->ipdata.size;
1436 hammer2_inode_unlock_sh(ip, parent);
1437 return (lf_advlock(ap, &ip->advlock, size));
1443 hammer2_vop_close(struct vop_close_args *ap)
1445 return vop_stdclose(ap);
1449 * hammer2_vop_nlink { nch, dvp, vp, cred }
1451 * Create a hardlink from (vp) to {dvp, nch}.
1455 hammer2_vop_nlink(struct vop_nlink_args *ap)
1457 hammer2_inode_t *dip; /* target directory to create link in */
1458 hammer2_inode_t *ip; /* inode we are hardlinking to */
1459 hammer2_chain_t *chain;
1460 hammer2_trans_t trans;
1461 struct namecache *ncp;
1462 const uint8_t *name;
1466 dip = VTOI(ap->a_dvp);
1467 if (dip->pmp->ronly)
1470 ncp = ap->a_nch->ncp;
1471 name = ncp->nc_name;
1472 name_len = ncp->nc_nlen;
1475 * ip represents the file being hardlinked. The file could be a
1476 * normal file or a hardlink target if it has already been hardlinked.
1477 * If ip is a hardlinked target then ip->pip represents the location
1478 * of the hardlinked target, NOT the location of the hardlink pointer.
1480 * Bump nlinks and potentially also create or move the hardlink
1481 * target in the parent directory common to (ip) and (dip). The
1482 * consolidation code can modify ip->chain and ip->pip. The
1483 * returned chain is locked.
1485 ip = VTOI(ap->a_vp);
1486 hammer2_chain_memory_wait(ip->pmp);
1487 hammer2_trans_init(&trans, ip->pmp, 0);
1489 chain = hammer2_inode_lock_ex(ip);
1490 error = hammer2_hardlink_consolidate(&trans, ip, &chain, dip, 1);
1495 * Create a directory entry connected to the specified chain.
1496 * The hardlink consolidation code has already adjusted ip->pip
1497 * to the common parent directory containing the actual hardlink
1499 * (which may be different from dip where we created our hardlink
1500 * entry. ip->chain always represents the actual hardlink and not
1501 * any of the pointers to the actual hardlink).
1503 error = hammer2_inode_connect(&trans, 1,
1507 cache_setunresolved(ap->a_nch);
1508 cache_setvp(ap->a_nch, ap->a_vp);
1511 hammer2_inode_unlock_ex(ip, chain);
1512 hammer2_trans_done(&trans);
1518 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1520 * The operating system has already ensured that the directory entry
1521 * does not exist and done all appropriate namespace locking.
1525 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1527 hammer2_inode_t *dip;
1528 hammer2_inode_t *nip;
1529 hammer2_trans_t trans;
1530 hammer2_chain_t *nchain;
1531 struct namecache *ncp;
1532 const uint8_t *name;
1536 dip = VTOI(ap->a_dvp);
1537 if (dip->pmp->ronly)
1540 ncp = ap->a_nch->ncp;
1541 name = ncp->nc_name;
1542 name_len = ncp->nc_nlen;
1543 hammer2_chain_memory_wait(dip->pmp);
1544 hammer2_trans_init(&trans, dip->pmp, 0);
1546 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1547 name, name_len, &nchain, &error);
1549 KKASSERT(nip == NULL);
1552 *ap->a_vpp = hammer2_igetv(nip, &error);
1553 hammer2_inode_unlock_ex(nip, nchain);
1555 hammer2_trans_done(&trans);
1558 cache_setunresolved(ap->a_nch);
1559 cache_setvp(ap->a_nch, *ap->a_vpp);
1565 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1569 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1571 hammer2_inode_t *dip;
1572 hammer2_inode_t *nip;
1573 hammer2_chain_t *nparent;
1574 hammer2_trans_t trans;
1575 struct namecache *ncp;
1576 const uint8_t *name;
1580 dip = VTOI(ap->a_dvp);
1581 if (dip->pmp->ronly)
1584 ncp = ap->a_nch->ncp;
1585 name = ncp->nc_name;
1586 name_len = ncp->nc_nlen;
1587 hammer2_chain_memory_wait(dip->pmp);
1588 hammer2_trans_init(&trans, dip->pmp, 0);
1590 ap->a_vap->va_type = VLNK; /* enforce type */
1592 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1593 name, name_len, &nparent, &error);
1595 KKASSERT(nip == NULL);
1597 hammer2_trans_done(&trans);
1600 *ap->a_vpp = hammer2_igetv(nip, &error);
1603 * Build the softlink (~like file data) and finalize the namecache.
1609 hammer2_inode_data_t *nipdata;
1611 nipdata = &nip->chain->data->ipdata;
1612 bytes = strlen(ap->a_target);
1614 if (bytes <= HAMMER2_EMBEDDED_BYTES) {
1615 KKASSERT(nipdata->op_flags &
1616 HAMMER2_OPFLAG_DIRECTDATA);
1617 bcopy(ap->a_target, nipdata->u.data, bytes);
1618 nipdata->size = bytes;
1620 hammer2_inode_unlock_ex(nip, nparent);
1622 hammer2_inode_unlock_ex(nip, nparent);
1623 bzero(&auio, sizeof(auio));
1624 bzero(&aiov, sizeof(aiov));
1625 auio.uio_iov = &aiov;
1626 auio.uio_segflg = UIO_SYSSPACE;
1627 auio.uio_rw = UIO_WRITE;
1628 auio.uio_resid = bytes;
1629 auio.uio_iovcnt = 1;
1630 auio.uio_td = curthread;
1631 aiov.iov_base = ap->a_target;
1632 aiov.iov_len = bytes;
1633 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1634 nipdata = &nip->chain->data->ipdata; /* RELOAD */
1635 /* XXX handle error */
1639 hammer2_inode_unlock_ex(nip, nparent);
1641 hammer2_trans_done(&trans);
1644 * Finalize namecache
1647 cache_setunresolved(ap->a_nch);
1648 cache_setvp(ap->a_nch, *ap->a_vpp);
1649 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1655 * hammer2_vop_nremove { nch, dvp, cred }
1659 hammer2_vop_nremove(struct vop_nremove_args *ap)
1661 hammer2_inode_t *dip;
1662 hammer2_trans_t trans;
1663 struct namecache *ncp;
1664 const uint8_t *name;
1668 dip = VTOI(ap->a_dvp);
1669 if (dip->pmp->ronly)
1672 ncp = ap->a_nch->ncp;
1673 name = ncp->nc_name;
1674 name_len = ncp->nc_nlen;
1675 hammer2_chain_memory_wait(dip->pmp);
1676 hammer2_trans_init(&trans, dip->pmp, 0);
1677 error = hammer2_unlink_file(&trans, dip, name, name_len, 0, NULL);
1678 hammer2_trans_done(&trans);
1680 cache_unlink(ap->a_nch);
1686 * hammer2_vop_nrmdir { nch, dvp, cred }
1690 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1692 hammer2_inode_t *dip;
1693 hammer2_trans_t trans;
1694 struct namecache *ncp;
1695 const uint8_t *name;
1699 dip = VTOI(ap->a_dvp);
1700 if (dip->pmp->ronly)
1703 ncp = ap->a_nch->ncp;
1704 name = ncp->nc_name;
1705 name_len = ncp->nc_nlen;
1707 hammer2_chain_memory_wait(dip->pmp);
1708 hammer2_trans_init(&trans, dip->pmp, 0);
1709 error = hammer2_unlink_file(&trans, dip, name, name_len, 1, NULL);
1710 hammer2_trans_done(&trans);
1712 cache_unlink(ap->a_nch);
1718 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1722 hammer2_vop_nrename(struct vop_nrename_args *ap)
1724 struct namecache *fncp;
1725 struct namecache *tncp;
1726 hammer2_inode_t *fdip;
1727 hammer2_inode_t *tdip;
1728 hammer2_inode_t *ip;
1729 hammer2_chain_t *chain;
1730 hammer2_trans_t trans;
1731 const uint8_t *fname;
1733 const uint8_t *tname;
1738 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1740 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1743 fdip = VTOI(ap->a_fdvp); /* source directory */
1744 tdip = VTOI(ap->a_tdvp); /* target directory */
1746 if (fdip->pmp->ronly)
1749 fncp = ap->a_fnch->ncp; /* entry name in source */
1750 fname = fncp->nc_name;
1751 fname_len = fncp->nc_nlen;
1753 tncp = ap->a_tnch->ncp; /* entry name in target */
1754 tname = tncp->nc_name;
1755 tname_len = tncp->nc_nlen;
1757 hammer2_chain_memory_wait(tdip->pmp);
1758 hammer2_trans_init(&trans, tdip->pmp, 0);
1761 * ip is the inode being renamed. If this is a hardlink then
1762 * ip represents the actual file and not the hardlink marker.
1764 ip = VTOI(fncp->nc_vp);
1768 * Keep a tight grip on the inode so the temporary unlinking from
1769 * the source location prior to linking to the target location
1770 * does not cause the chain to be destroyed.
1772 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
1773 * unlinking elements from their directories. Locking
1774 * the nlinks field does not lock the whole inode.
1776 hammer2_inode_ref(ip);
1779 * Remove target if it exists
1781 error = hammer2_unlink_file(&trans, tdip, tname, tname_len, -1, NULL);
1782 if (error && error != ENOENT)
1784 cache_setunresolved(ap->a_tnch);
1787 * When renaming a hardlinked file we may have to re-consolidate
1788 * the location of the hardlink target. Since the element is simply
1789 * being moved, nlinks is not modified in this case.
1791 * If ip represents a regular file the consolidation code essentially
1792 * does nothing other than return the same locked chain that was
1795 * The returned chain will be locked.
1797 * WARNING! We do not currently have a local copy of ipdata but
1798 * we do use one later remember that it must be reloaded
1799 * on any modification to the inode, including connects.
1801 chain = hammer2_inode_lock_ex(ip);
1802 error = hammer2_hardlink_consolidate(&trans, ip, &chain, tdip, 0);
1807 * Disconnect (fdip, fname) from the source directory. This will
1808 * disconnect (ip) if it represents a direct file. If (ip) represents
1809 * a hardlink the HARDLINK pointer object will be removed but the
1810 * hardlink will stay intact.
1812 * The target chain may be marked DELETED but will not be destroyed
1813 * since we retain our hold on ip and chain.
1815 error = hammer2_unlink_file(&trans, fdip, fname, fname_len, -1, &hlink);
1816 KKASSERT(error != EAGAIN);
1821 * Reconnect ip to target directory using chain. Chains cannot
1822 * actually be moved, so this will duplicate the chain in the new
1823 * spot and assign it to the ip, replacing the old chain.
1825 * WARNING: chain locks can lock buffer cache buffers, to avoid
1826 * deadlocks we want to unlock before issuing a cache_*()
1827 * op (that might have to lock a vnode).
1829 error = hammer2_inode_connect(&trans, hlink,
1833 KKASSERT(chain != NULL);
1834 hammer2_inode_repoint(ip, (hlink ? ip->pip : tdip), chain);
1835 cache_rename(ap->a_fnch, ap->a_tnch);
1838 hammer2_inode_unlock_ex(ip, chain);
1839 hammer2_inode_drop(ip);
1840 hammer2_trans_done(&trans);
1848 * WARNING: The strategy code cannot safely use hammer2 transactions
1849 * as this can deadlock against vfs_sync's vfsync() call
1850 * if multiple flushes are queued.
1852 static int hammer2_strategy_read(struct vop_strategy_args *ap);
1853 static int hammer2_strategy_write(struct vop_strategy_args *ap);
1854 static void hammer2_strategy_read_callback(hammer2_chain_t *chain,
1855 struct buf *dbp, char *data, void *arg);
1859 hammer2_vop_strategy(struct vop_strategy_args *ap)
1870 error = hammer2_strategy_read(ap);
1871 ++hammer2_iod_file_read;
1874 error = hammer2_strategy_write(ap);
1875 ++hammer2_iod_file_write;
1878 bp->b_error = error = EINVAL;
1879 bp->b_flags |= B_ERROR;
1889 hammer2_strategy_read(struct vop_strategy_args *ap)
1894 hammer2_inode_t *ip;
1895 hammer2_chain_t *parent;
1896 hammer2_chain_t *chain;
1897 hammer2_key_t key_dummy;
1898 hammer2_key_t lbase;
1900 int cache_index = -1;
1904 ip = VTOI(ap->a_vp);
1905 nbio = push_bio(bio);
1907 lbase = bio->bio_offset;
1909 KKASSERT(((int)lbase & HAMMER2_PBUFMASK) == 0);
1911 parent = hammer2_inode_lock_sh(ip);
1912 chain = hammer2_chain_lookup(&parent, &key_dummy,
1915 HAMMER2_LOOKUP_NODATA |
1916 HAMMER2_LOOKUP_SHARED);
1918 if (chain == NULL) {
1924 bzero(bp->b_data, bp->b_bcount);
1926 } else if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
1928 * Data is embedded in the inode (copy from inode).
1930 hammer2_chain_load_async(chain, hammer2_strategy_read_callback,
1932 } else if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
1934 * Data is on-media, issue device I/O and copy.
1936 * XXX direct-IO shortcut could go here XXX.
1938 if (HAMMER2_DEC_COMP(chain->bref.methods) == HAMMER2_COMP_LZ4) {
1940 * Block compression is determined by bref.methods value.
1942 hammer2_blockref_t *bref;
1943 hammer2_off_t pbase;
1944 hammer2_off_t pmask;
1947 bref = &chain->bref;
1948 psize = hammer2_devblksize(chain->bytes);
1949 pmask = (hammer2_off_t)psize - 1;
1950 pbase = bref->data_off & ~pmask;
1951 loff = (int)((bref->data_off &
1952 ~HAMMER2_OFF_MASK_RADIX) - pbase);
1953 nbio->bio_caller_info3.value = loff;
1954 breadcb(chain->hmp->devvp, pbase, psize,
1955 hammer2_decompress_LZ4_callback, nbio);
1956 /* XXX async read dev blk not protected by chain lk */
1957 hammer2_chain_unlock(chain);
1958 } else if (HAMMER2_DEC_COMP(chain->bref.methods) == HAMMER2_COMP_ZLIB) {
1959 hammer2_blockref_t *bref;
1960 hammer2_off_t pbase;
1961 hammer2_off_t pmask;
1964 bref = &chain->bref;
1965 psize = hammer2_devblksize(chain->bytes);
1966 pmask = (hammer2_off_t)psize - 1;
1967 pbase = bref->data_off & ~pmask;
1968 loff = (int)((bref->data_off &
1969 ~HAMMER2_OFF_MASK_RADIX) - pbase);
1970 nbio->bio_caller_info3.value = loff;
1971 breadcb(chain->hmp->devvp, pbase, psize,
1972 hammer2_decompress_ZLIB_callback, nbio);
1973 /* XXX async read dev blk not protected by chain lk */
1974 hammer2_chain_unlock(chain);
1977 hammer2_chain_load_async(chain,
1978 hammer2_strategy_read_callback,
1982 panic("READ PATH: hammer2_strategy_read: unknown bref type");
1985 hammer2_inode_unlock_sh(ip, parent);
1990 * Read callback for block that is not compressed.
1994 hammer2_strategy_read_callback(hammer2_chain_t *chain, struct buf *dbp,
1995 char *data, void *arg)
1997 struct bio *nbio = arg;
1998 struct buf *bp = nbio->bio_buf;
2000 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
2002 * Data is embedded in the inode (copy from inode).
2004 bcopy(((hammer2_inode_data_t *)data)->u.data,
2005 bp->b_data, HAMMER2_EMBEDDED_BYTES);
2006 bzero(bp->b_data + HAMMER2_EMBEDDED_BYTES,
2007 bp->b_bcount - HAMMER2_EMBEDDED_BYTES);
2010 hammer2_chain_unlock(chain);
2012 } else if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2014 * Data is on-media, issue device I/O and copy.
2016 * XXX direct-IO shortcut could go here XXX.
2018 KKASSERT(chain->bytes <= bp->b_bcount);
2019 bcopy(data, bp->b_data, chain->bytes);
2020 if (chain->bytes < bp->b_bcount); {
2021 bzero(bp->b_data + chain->bytes,
2022 bp->b_bcount - chain->bytes);
2024 bp->b_flags |= B_NOTMETA;
2027 hammer2_chain_unlock(chain);
2032 panic("hammer2_strategy_read: unknown bref type");
2033 /*hammer2_chain_unlock(chain);*/
2040 hammer2_strategy_write(struct vop_strategy_args *ap)
2042 hammer2_pfsmount_t *pmp;
2045 hammer2_inode_t *ip;
2049 ip = VTOI(ap->a_vp);
2052 mtx_lock(&pmp->wthread_mtx);
2053 bioq_insert_tail(&pmp->wthread_bioq, ap->a_bio);
2054 wakeup(&pmp->wthread_bioq);
2055 mtx_unlock(&pmp->wthread_mtx);
2061 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2065 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2067 hammer2_inode_t *ip;
2070 ip = VTOI(ap->a_vp);
2072 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2073 ap->a_fflag, ap->a_cred);
2079 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2082 hammer2_pfsmount_t *pmp;
2086 case (MOUNTCTL_SET_EXPORT):
2087 mp = ap->a_head.a_ops->head.vv_mount;
2090 if (ap->a_ctllen != sizeof(struct export_args))
2093 rc = vfs_export(mp, &pmp->export,
2094 (const struct export_args *)ap->a_ctl);
2097 rc = vop_stdmountctl(ap);
2103 struct vop_ops hammer2_vnode_vops = {
2104 .vop_default = vop_defaultop,
2105 .vop_fsync = hammer2_vop_fsync,
2106 .vop_getpages = vop_stdgetpages,
2107 .vop_putpages = vop_stdputpages,
2108 .vop_access = hammer2_vop_access,
2109 .vop_advlock = hammer2_vop_advlock,
2110 .vop_close = hammer2_vop_close,
2111 .vop_nlink = hammer2_vop_nlink,
2112 .vop_ncreate = hammer2_vop_ncreate,
2113 .vop_nsymlink = hammer2_vop_nsymlink,
2114 .vop_nremove = hammer2_vop_nremove,
2115 .vop_nrmdir = hammer2_vop_nrmdir,
2116 .vop_nrename = hammer2_vop_nrename,
2117 .vop_getattr = hammer2_vop_getattr,
2118 .vop_setattr = hammer2_vop_setattr,
2119 .vop_readdir = hammer2_vop_readdir,
2120 .vop_readlink = hammer2_vop_readlink,
2121 .vop_getpages = vop_stdgetpages,
2122 .vop_putpages = vop_stdputpages,
2123 .vop_read = hammer2_vop_read,
2124 .vop_write = hammer2_vop_write,
2125 .vop_open = hammer2_vop_open,
2126 .vop_inactive = hammer2_vop_inactive,
2127 .vop_reclaim = hammer2_vop_reclaim,
2128 .vop_nresolve = hammer2_vop_nresolve,
2129 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2130 .vop_nmkdir = hammer2_vop_nmkdir,
2131 .vop_ioctl = hammer2_vop_ioctl,
2132 .vop_mountctl = hammer2_vop_mountctl,
2133 .vop_bmap = hammer2_vop_bmap,
2134 .vop_strategy = hammer2_vop_strategy,
2137 struct vop_ops hammer2_spec_vops = {
2141 struct vop_ops hammer2_fifo_vops = {