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;
653 saveoff = uio->uio_offset;
656 * Setup cookies directory entry cookies if requested
658 if (ap->a_ncookies) {
659 ncookies = uio->uio_resid / 16 + 1;
662 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
669 parent = hammer2_inode_lock_sh(ip);
670 ipdata = &parent->data->ipdata;
673 * Handle artificial entries. To ensure that only positive 64 bit
674 * quantities are returned to userland we always strip off bit 63.
675 * The hash code is designed such that codes 0x0000-0x7FFF are not
676 * used, allowing us to use these codes for articial entries.
678 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
679 * allow '..' to cross the mount point into (e.g.) the super-root.
682 chain = (void *)(intptr_t)-1; /* non-NULL for early goto done case */
685 inum = ipdata->inum & HAMMER2_DIRHASH_USERMSK;
686 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
690 cookies[cookie_index] = saveoff;
693 if (cookie_index == ncookies)
699 * Be careful with lockorder when accessing ".."
701 * (ip is the current dir. xip is the parent dir).
703 inum = ipdata->inum & HAMMER2_DIRHASH_USERMSK;
704 while (ip->pip != NULL && ip != ip->pmp->iroot) {
706 hammer2_inode_ref(xip);
707 hammer2_inode_unlock_sh(ip, parent);
708 xchain = hammer2_inode_lock_sh(xip);
709 parent = hammer2_inode_lock_sh(ip);
710 hammer2_inode_drop(xip);
711 if (xip == ip->pip) {
712 inum = xchain->data->ipdata.inum &
713 HAMMER2_DIRHASH_USERMSK;
714 hammer2_inode_unlock_sh(xip, xchain);
717 hammer2_inode_unlock_sh(xip, xchain);
719 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
723 cookies[cookie_index] = saveoff;
726 if (cookie_index == ncookies)
730 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
733 * parent is the inode chain, already locked for us. Don't
734 * double lock shared locks as this will screw up upgrades.
739 chain = hammer2_chain_lookup(&parent, lkey, lkey,
740 HAMMER2_LOOKUP_SHARED);
742 chain = hammer2_chain_lookup(&parent,
743 lkey, (hammer2_key_t)-1,
744 HAMMER2_LOOKUP_SHARED);
747 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
748 dtype = hammer2_get_dtype(chain);
749 saveoff = chain->bref.key & HAMMER2_DIRHASH_USERMSK;
750 r = vop_write_dirent(&error, uio,
751 chain->data->ipdata.inum &
752 HAMMER2_DIRHASH_USERMSK,
754 chain->data->ipdata.name_len,
755 chain->data->ipdata.filename);
759 cookies[cookie_index] = saveoff;
762 /* XXX chain error */
763 kprintf("bad chain type readdir %d\n",
768 * Keys may not be returned in order so once we have a
769 * placemarker (chain) the scan must allow the full range
770 * or some entries will be missed.
772 chain = hammer2_chain_next(&parent, chain,
773 HAMMER2_DIRHASH_VISIBLE,
775 HAMMER2_LOOKUP_SHARED);
777 saveoff = (chain->bref.key &
778 HAMMER2_DIRHASH_USERMSK) + 1;
780 saveoff = (hammer2_key_t)-1;
782 if (cookie_index == ncookies)
786 hammer2_chain_unlock(chain);
788 hammer2_inode_unlock_sh(ip, parent);
790 *ap->a_eofflag = (chain == NULL);
791 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
792 if (error && cookie_index == 0) {
794 kfree(cookies, M_TEMP);
796 *ap->a_cookies = NULL;
800 *ap->a_ncookies = cookie_index;
801 *ap->a_cookies = cookies;
808 * hammer2_vop_readlink { vp, uio, cred }
812 hammer2_vop_readlink(struct vop_readlink_args *ap)
819 if (vp->v_type != VLNK)
823 error = hammer2_read_file(ip, ap->a_uio, 0);
829 hammer2_vop_read(struct vop_read_args *ap)
839 * Read operations supported on this vnode?
842 if (vp->v_type != VREG)
852 seqcount = ap->a_ioflag >> 16;
853 bigread = (uio->uio_resid > 100 * 1024 * 1024);
855 error = hammer2_read_file(ip, uio, seqcount);
861 hammer2_vop_write(struct vop_write_args *ap)
864 hammer2_trans_t trans;
873 * Read operations supported on this vnode?
876 if (vp->v_type != VREG)
888 seqcount = ap->a_ioflag >> 16;
889 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
892 * Check resource limit
894 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
895 uio->uio_offset + uio->uio_resid >
896 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
897 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
901 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
904 * The transaction interlocks against flushes initiations
905 * (note: but will run concurrently with the actual flush).
907 hammer2_trans_init(&trans, ip->pmp, 0);
908 error = hammer2_write_file(ip, uio, ap->a_ioflag, seqcount);
909 hammer2_trans_done(&trans);
915 * Perform read operations on a file or symlink given an UNLOCKED
918 * The passed ip is not locked.
922 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
933 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
935 ccms_thread_unlock(&ip->topo_cst);
937 while (uio->uio_resid > 0 && uio->uio_offset < size) {
944 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
947 error = cluster_read(ip->vp, leof, lbase, lblksize,
948 uio->uio_resid, seqcount * BKVASIZE,
953 loff = (int)(uio->uio_offset - lbase);
955 if (n > uio->uio_resid)
957 if (n > size - uio->uio_offset)
958 n = (int)(size - uio->uio_offset);
959 bp->b_flags |= B_AGE;
960 uiomove((char *)bp->b_data + loff, n, uio);
967 * Write to the file represented by the inode via the logical buffer cache.
968 * The inode may represent a regular file or a symlink.
970 * The inode must not be locked.
974 hammer2_write_file(hammer2_inode_t *ip,
975 struct uio *uio, int ioflag, int seqcount)
977 hammer2_key_t old_eof;
978 hammer2_key_t new_eof;
987 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
988 if (ioflag & IO_APPEND)
989 uio->uio_offset = ip->size;
991 ccms_thread_unlock(&ip->topo_cst);
994 * Extend the file if necessary. If the write fails at some point
995 * we will truncate it back down to cover as much as we were able
998 * Doing this now makes it easier to calculate buffer sizes in
1005 if (uio->uio_offset + uio->uio_resid > old_eof) {
1006 new_eof = uio->uio_offset + uio->uio_resid;
1008 hammer2_extend_file(ip, new_eof);
1009 kflags |= NOTE_EXTEND;
1017 while (uio->uio_resid > 0) {
1018 hammer2_key_t lbase;
1026 * Don't allow the buffer build to blow out the buffer
1029 if ((ioflag & IO_RECURSE) == 0)
1030 bwillwrite(HAMMER2_PBUFSIZE);
1033 * This nominally tells us how much we can cluster and
1034 * what the logical buffer size needs to be. Currently
1035 * we don't try to cluster the write and just handle one
1038 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
1040 loff = (int)(uio->uio_offset - lbase);
1042 if (uio->uio_resid < lblksize) {
1043 rem_size = (int)uio->uio_resid;
1049 KKASSERT(lblksize <= 65536);
1052 * Calculate bytes to copy this transfer and whether the
1053 * copy completely covers the buffer or not.
1056 n = lblksize - loff;
1057 if (n > uio->uio_resid) {
1059 if (loff == lbase && uio->uio_offset + n == new_eof)
1061 } else if (loff == 0) {
1068 if (uio->uio_segflg == UIO_NOCOPY) {
1070 * Issuing a write with the same data backing the
1071 * buffer. Instantiate the buffer to collect the
1072 * backing vm pages, then read-in any missing bits.
1074 * This case is used by vop_stdputpages().
1076 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
1077 if ((bp->b_flags & B_CACHE) == 0) {
1079 error = bread(ip->vp, lbase, lblksize, &bp);
1081 } else if (trivial) {
1083 * Even though we are entirely overwriting the buffer
1084 * we may still have to zero it out to avoid a
1085 * mmap/write visibility issue.
1087 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
1088 if ((bp->b_flags & B_CACHE) == 0)
1092 * Partial overwrite, read in any missing bits then
1093 * replace the portion being written.
1095 * (The strategy code will detect zero-fill physical
1096 * blocks for this case).
1098 error = bread(ip->vp, lbase, lblksize, &bp);
1109 * Ok, copy the data in
1111 error = uiomove(bp->b_data + loff, n, uio);
1112 kflags |= NOTE_WRITE;
1124 * Cleanup. If we extended the file EOF but failed to write through
1125 * the entire write is a failure and we have to back-up.
1127 if (error && new_eof != old_eof) {
1128 hammer2_truncate_file(ip, old_eof);
1129 } else if (modified) {
1130 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1131 hammer2_update_time(&ip->mtime);
1132 atomic_set_int(&ip->flags, HAMMER2_INODE_MTIME);
1133 ccms_thread_unlock(&ip->topo_cst);
1135 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1136 hammer2_knote(ip->vp, kflags);
1142 * Truncate the size of a file. The inode must not be locked.
1146 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1148 hammer2_key_t lbase;
1152 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1153 nvtruncbuf(ip->vp, nsize,
1154 nblksize, (int)nsize & (nblksize - 1),
1157 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1159 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1160 ccms_thread_unlock(&ip->topo_cst);
1164 * Extend the size of a file. The inode must not be locked.
1168 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1170 hammer2_key_t lbase;
1171 hammer2_key_t osize;
1175 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1178 ccms_thread_unlock(&ip->topo_cst);
1181 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1182 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1188 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1193 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1195 hammer2_inode_t *ip;
1196 hammer2_inode_t *dip;
1197 hammer2_chain_t *parent;
1198 hammer2_chain_t *chain;
1199 hammer2_chain_t *ochain;
1200 hammer2_trans_t trans;
1201 struct namecache *ncp;
1202 const uint8_t *name;
1208 dip = VTOI(ap->a_dvp);
1209 ncp = ap->a_nch->ncp;
1210 name = ncp->nc_name;
1211 name_len = ncp->nc_nlen;
1212 lhc = hammer2_dirhash(name, name_len);
1215 * Note: In DragonFly the kernel handles '.' and '..'.
1217 parent = hammer2_inode_lock_sh(dip);
1218 chain = hammer2_chain_lookup(&parent,
1219 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1220 HAMMER2_LOOKUP_SHARED);
1222 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1223 name_len == chain->data->ipdata.name_len &&
1224 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1227 chain = hammer2_chain_next(&parent, chain,
1228 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1229 HAMMER2_LOOKUP_SHARED);
1231 hammer2_inode_unlock_sh(dip, parent);
1234 * If the inode represents a forwarding entry for a hardlink we have
1235 * to locate the actual inode. The original ip is saved for possible
1236 * deconsolidation. (ip) will only be set to non-NULL when we have
1237 * to locate the real file via a hardlink. ip will be referenced but
1238 * not locked in that situation. chain is passed in locked and
1241 * XXX what kind of chain lock?
1244 if (chain && chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
1245 error = hammer2_hardlink_find(dip, &chain, &ochain);
1247 kprintf("hammer2: unable to find hardlink\n");
1249 hammer2_chain_unlock(chain);
1257 * Deconsolidate any hardlink whos nlinks == 1. Ignore errors.
1258 * If an error occurs chain and ip are left alone.
1260 * XXX upgrade shared lock?
1262 if (ochain && chain &&
1263 chain->data->ipdata.nlinks == 1 && !dip->pmp->ronly) {
1264 kprintf("hammer2: need to unconsolidate hardlink for %s\n",
1265 chain->data->ipdata.filename);
1266 /* XXX retain shared lock on dip? (currently not held) */
1267 hammer2_trans_init(&trans, dip->pmp, 0);
1268 hammer2_hardlink_deconsolidate(&trans, dip, &chain, &ochain);
1269 hammer2_trans_done(&trans);
1273 * Acquire the related vnode
1275 * NOTE: For error processing, only ENOENT resolves the namecache
1276 * entry to NULL, otherwise we just return the error and
1277 * leave the namecache unresolved.
1279 * NOTE: multiple hammer2_inode structures can be aliased to the
1280 * same chain element, for example for hardlinks. This
1281 * use case does not 'reattach' inode associations that
1282 * might already exist, but always allocates a new one.
1284 * WARNING: inode structure is locked exclusively via inode_get
1285 * but chain was locked shared. inode_unlock_ex()
1286 * will handle it properly.
1289 ip = hammer2_inode_get(dip->pmp, dip, chain);
1290 vp = hammer2_igetv(ip, &error);
1293 cache_setvp(ap->a_nch, vp);
1294 } else if (error == ENOENT) {
1295 cache_setvp(ap->a_nch, NULL);
1297 hammer2_inode_unlock_ex(ip, chain);
1300 * The vp should not be released until after we've disposed
1301 * of our locks, because it might cause vop_inactive() to
1308 cache_setvp(ap->a_nch, NULL);
1311 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1312 ("resolve error %d/%p chain %p ap %p\n",
1313 error, ap->a_nch->ncp->nc_vp, chain, ap));
1315 hammer2_chain_drop(ochain);
1321 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1323 hammer2_inode_t *dip;
1324 hammer2_inode_t *ip;
1325 hammer2_chain_t *parent;
1328 dip = VTOI(ap->a_dvp);
1330 if ((ip = dip->pip) == NULL) {
1334 parent = hammer2_inode_lock_ex(ip);
1335 *ap->a_vpp = hammer2_igetv(ip, &error);
1336 hammer2_inode_unlock_ex(ip, parent);
1343 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1345 hammer2_inode_t *dip;
1346 hammer2_inode_t *nip;
1347 hammer2_trans_t trans;
1348 hammer2_chain_t *chain;
1349 struct namecache *ncp;
1350 const uint8_t *name;
1354 dip = VTOI(ap->a_dvp);
1355 if (dip->pmp->ronly)
1358 ncp = ap->a_nch->ncp;
1359 name = ncp->nc_name;
1360 name_len = ncp->nc_nlen;
1362 hammer2_chain_memory_wait(dip->pmp);
1363 hammer2_trans_init(&trans, dip->pmp, 0);
1364 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1365 name, name_len, &chain, &error);
1367 KKASSERT(nip == NULL);
1370 *ap->a_vpp = hammer2_igetv(nip, &error);
1371 hammer2_inode_unlock_ex(nip, chain);
1373 hammer2_trans_done(&trans);
1376 cache_setunresolved(ap->a_nch);
1377 cache_setvp(ap->a_nch, *ap->a_vpp);
1383 * Return the largest contiguous physical disk range for the logical
1384 * request, in bytes.
1386 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
1390 hammer2_vop_bmap(struct vop_bmap_args *ap)
1392 *ap->a_doffsetp = NOOFFSET;
1397 return (EOPNOTSUPP);
1400 hammer2_inode_t *ip;
1401 hammer2_chain_t *parent;
1402 hammer2_chain_t *chain;
1406 hammer2_off_t pbytes;
1407 hammer2_off_t array[HAMMER2_BMAP_COUNT][2];
1412 * Only supported on regular files
1414 * Only supported for read operations (required for cluster_read).
1415 * The block allocation is delayed for write operations.
1418 if (vp->v_type != VREG)
1419 return (EOPNOTSUPP);
1420 if (ap->a_cmd != BUF_CMD_READ)
1421 return (EOPNOTSUPP);
1424 bzero(array, sizeof(array));
1427 * Calculate logical range
1429 KKASSERT((ap->a_loffset & HAMMER2_LBUFMASK64) == 0);
1430 lbeg = ap->a_loffset & HAMMER2_OFF_MASK_HI;
1431 lend = lbeg + HAMMER2_BMAP_COUNT * HAMMER2_PBUFSIZE - 1;
1434 loff = ap->a_loffset & HAMMER2_OFF_MASK_LO;
1436 parent = hammer2_inode_lock_sh(ip);
1437 chain = hammer2_chain_lookup(&parent,
1439 HAMMER2_LOOKUP_NODATA |
1440 HAMMER2_LOOKUP_SHARED);
1441 if (chain == NULL) {
1442 *ap->a_doffsetp = ZFOFFSET;
1443 hammer2_inode_unlock_sh(ip, parent);
1448 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
1449 ai = (chain->bref.key - lbeg) / HAMMER2_PBUFSIZE;
1450 KKASSERT(ai >= 0 && ai < HAMMER2_BMAP_COUNT);
1451 array[ai][0] = chain->bref.data_off & HAMMER2_OFF_MASK;
1452 array[ai][1] = chain->bytes;
1454 chain = hammer2_chain_next(&parent, chain,
1456 HAMMER2_LOOKUP_NODATA |
1457 HAMMER2_LOOKUP_SHARED);
1459 hammer2_inode_unlock_sh(ip, parent);
1462 * If the requested loffset is not mappable physically we can't
1463 * bmap. The caller will have to access the file data via a
1466 if (array[0][0] == 0 || array[0][1] < loff + HAMMER2_MINIOSIZE) {
1467 *ap->a_doffsetp = NOOFFSET;
1472 * Calculate the physical disk offset range for array[0]
1474 pbeg = array[0][0] + loff;
1475 pbytes = array[0][1] - loff;
1477 for (ai = 1; ai < HAMMER2_BMAP_COUNT; ++ai) {
1478 if (array[ai][0] != pbeg + pbytes)
1480 pbytes += array[ai][1];
1483 *ap->a_doffsetp = pbeg;
1485 *ap->a_runp = pbytes;
1492 hammer2_vop_open(struct vop_open_args *ap)
1494 return vop_stdopen(ap);
1498 * hammer2_vop_advlock { vp, id, op, fl, flags }
1502 hammer2_vop_advlock(struct vop_advlock_args *ap)
1504 hammer2_inode_t *ip = VTOI(ap->a_vp);
1505 hammer2_chain_t *parent;
1508 parent = hammer2_inode_lock_sh(ip);
1509 size = parent->data->ipdata.size;
1510 hammer2_inode_unlock_sh(ip, parent);
1511 return (lf_advlock(ap, &ip->advlock, size));
1517 hammer2_vop_close(struct vop_close_args *ap)
1519 return vop_stdclose(ap);
1523 * hammer2_vop_nlink { nch, dvp, vp, cred }
1525 * Create a hardlink from (vp) to {dvp, nch}.
1529 hammer2_vop_nlink(struct vop_nlink_args *ap)
1531 hammer2_inode_t *dip; /* target directory to create link in */
1532 hammer2_inode_t *ip; /* inode we are hardlinking to */
1533 hammer2_chain_t *chain;
1534 hammer2_trans_t trans;
1535 struct namecache *ncp;
1536 const uint8_t *name;
1540 dip = VTOI(ap->a_dvp);
1541 if (dip->pmp->ronly)
1544 ncp = ap->a_nch->ncp;
1545 name = ncp->nc_name;
1546 name_len = ncp->nc_nlen;
1549 * ip represents the file being hardlinked. The file could be a
1550 * normal file or a hardlink target if it has already been hardlinked.
1551 * If ip is a hardlinked target then ip->pip represents the location
1552 * of the hardlinked target, NOT the location of the hardlink pointer.
1554 * Bump nlinks and potentially also create or move the hardlink
1555 * target in the parent directory common to (ip) and (dip). The
1556 * consolidation code can modify ip->chain and ip->pip. The
1557 * returned chain is locked.
1559 ip = VTOI(ap->a_vp);
1560 hammer2_chain_memory_wait(ip->pmp);
1561 hammer2_trans_init(&trans, ip->pmp, 0);
1563 chain = hammer2_inode_lock_ex(ip);
1564 error = hammer2_hardlink_consolidate(&trans, ip, &chain, dip, 1);
1569 * Create a directory entry connected to the specified chain.
1570 * The hardlink consolidation code has already adjusted ip->pip
1571 * to the common parent directory containing the actual hardlink
1573 * (which may be different from dip where we created our hardlink
1574 * entry. ip->chain always represents the actual hardlink and not
1575 * any of the pointers to the actual hardlink).
1577 error = hammer2_inode_connect(&trans, 1,
1581 cache_setunresolved(ap->a_nch);
1582 cache_setvp(ap->a_nch, ap->a_vp);
1585 hammer2_inode_unlock_ex(ip, chain);
1586 hammer2_trans_done(&trans);
1592 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1594 * The operating system has already ensured that the directory entry
1595 * does not exist and done all appropriate namespace locking.
1599 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1601 hammer2_inode_t *dip;
1602 hammer2_inode_t *nip;
1603 hammer2_trans_t trans;
1604 hammer2_chain_t *nchain;
1605 struct namecache *ncp;
1606 const uint8_t *name;
1610 dip = VTOI(ap->a_dvp);
1611 if (dip->pmp->ronly)
1614 ncp = ap->a_nch->ncp;
1615 name = ncp->nc_name;
1616 name_len = ncp->nc_nlen;
1617 hammer2_chain_memory_wait(dip->pmp);
1618 hammer2_trans_init(&trans, dip->pmp, 0);
1620 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1621 name, name_len, &nchain, &error);
1623 KKASSERT(nip == NULL);
1626 *ap->a_vpp = hammer2_igetv(nip, &error);
1627 hammer2_inode_unlock_ex(nip, nchain);
1629 hammer2_trans_done(&trans);
1632 cache_setunresolved(ap->a_nch);
1633 cache_setvp(ap->a_nch, *ap->a_vpp);
1639 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1643 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1645 hammer2_inode_t *dip;
1646 hammer2_inode_t *nip;
1647 hammer2_chain_t *nparent;
1648 hammer2_trans_t trans;
1649 struct namecache *ncp;
1650 const uint8_t *name;
1654 dip = VTOI(ap->a_dvp);
1655 if (dip->pmp->ronly)
1658 ncp = ap->a_nch->ncp;
1659 name = ncp->nc_name;
1660 name_len = ncp->nc_nlen;
1661 hammer2_chain_memory_wait(dip->pmp);
1662 hammer2_trans_init(&trans, dip->pmp, 0);
1664 ap->a_vap->va_type = VLNK; /* enforce type */
1666 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1667 name, name_len, &nparent, &error);
1669 KKASSERT(nip == NULL);
1671 hammer2_trans_done(&trans);
1674 *ap->a_vpp = hammer2_igetv(nip, &error);
1677 * Build the softlink (~like file data) and finalize the namecache.
1683 hammer2_inode_data_t *nipdata;
1685 nipdata = &nip->chain->data->ipdata;
1686 bytes = strlen(ap->a_target);
1688 if (bytes <= HAMMER2_EMBEDDED_BYTES) {
1689 KKASSERT(nipdata->op_flags &
1690 HAMMER2_OPFLAG_DIRECTDATA);
1691 bcopy(ap->a_target, nipdata->u.data, bytes);
1692 nipdata->size = bytes;
1694 hammer2_inode_unlock_ex(nip, nparent);
1696 hammer2_inode_unlock_ex(nip, nparent);
1697 bzero(&auio, sizeof(auio));
1698 bzero(&aiov, sizeof(aiov));
1699 auio.uio_iov = &aiov;
1700 auio.uio_segflg = UIO_SYSSPACE;
1701 auio.uio_rw = UIO_WRITE;
1702 auio.uio_resid = bytes;
1703 auio.uio_iovcnt = 1;
1704 auio.uio_td = curthread;
1705 aiov.iov_base = ap->a_target;
1706 aiov.iov_len = bytes;
1707 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1708 nipdata = &nip->chain->data->ipdata; /* RELOAD */
1709 /* XXX handle error */
1713 hammer2_inode_unlock_ex(nip, nparent);
1715 hammer2_trans_done(&trans);
1718 * Finalize namecache
1721 cache_setunresolved(ap->a_nch);
1722 cache_setvp(ap->a_nch, *ap->a_vpp);
1723 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1729 * hammer2_vop_nremove { nch, dvp, cred }
1733 hammer2_vop_nremove(struct vop_nremove_args *ap)
1735 hammer2_inode_t *dip;
1736 hammer2_trans_t trans;
1737 struct namecache *ncp;
1738 const uint8_t *name;
1742 dip = VTOI(ap->a_dvp);
1743 if (dip->pmp->ronly)
1746 ncp = ap->a_nch->ncp;
1747 name = ncp->nc_name;
1748 name_len = ncp->nc_nlen;
1749 hammer2_chain_memory_wait(dip->pmp);
1750 hammer2_trans_init(&trans, dip->pmp, 0);
1751 error = hammer2_unlink_file(&trans, dip, name, name_len, 0, NULL);
1752 hammer2_trans_done(&trans);
1754 cache_unlink(ap->a_nch);
1760 * hammer2_vop_nrmdir { nch, dvp, cred }
1764 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1766 hammer2_inode_t *dip;
1767 hammer2_trans_t trans;
1768 struct namecache *ncp;
1769 const uint8_t *name;
1773 dip = VTOI(ap->a_dvp);
1774 if (dip->pmp->ronly)
1777 ncp = ap->a_nch->ncp;
1778 name = ncp->nc_name;
1779 name_len = ncp->nc_nlen;
1781 hammer2_chain_memory_wait(dip->pmp);
1782 hammer2_trans_init(&trans, dip->pmp, 0);
1783 error = hammer2_unlink_file(&trans, dip, name, name_len, 1, NULL);
1784 hammer2_trans_done(&trans);
1786 cache_unlink(ap->a_nch);
1792 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1796 hammer2_vop_nrename(struct vop_nrename_args *ap)
1798 struct namecache *fncp;
1799 struct namecache *tncp;
1800 hammer2_inode_t *fdip;
1801 hammer2_inode_t *tdip;
1802 hammer2_inode_t *ip;
1803 hammer2_chain_t *chain;
1804 hammer2_trans_t trans;
1805 const uint8_t *fname;
1807 const uint8_t *tname;
1812 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1814 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1817 fdip = VTOI(ap->a_fdvp); /* source directory */
1818 tdip = VTOI(ap->a_tdvp); /* target directory */
1820 if (fdip->pmp->ronly)
1823 fncp = ap->a_fnch->ncp; /* entry name in source */
1824 fname = fncp->nc_name;
1825 fname_len = fncp->nc_nlen;
1827 tncp = ap->a_tnch->ncp; /* entry name in target */
1828 tname = tncp->nc_name;
1829 tname_len = tncp->nc_nlen;
1831 hammer2_chain_memory_wait(tdip->pmp);
1832 hammer2_trans_init(&trans, tdip->pmp, 0);
1835 * ip is the inode being renamed. If this is a hardlink then
1836 * ip represents the actual file and not the hardlink marker.
1838 ip = VTOI(fncp->nc_vp);
1842 * Keep a tight grip on the inode so the temporary unlinking from
1843 * the source location prior to linking to the target location
1844 * does not cause the chain to be destroyed.
1846 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
1847 * unlinking elements from their directories. Locking
1848 * the nlinks field does not lock the whole inode.
1850 hammer2_inode_ref(ip);
1853 * Remove target if it exists
1855 error = hammer2_unlink_file(&trans, tdip, tname, tname_len, -1, NULL);
1856 if (error && error != ENOENT)
1858 cache_setunresolved(ap->a_tnch);
1861 * When renaming a hardlinked file we may have to re-consolidate
1862 * the location of the hardlink target. Since the element is simply
1863 * being moved, nlinks is not modified in this case.
1865 * If ip represents a regular file the consolidation code essentially
1866 * does nothing other than return the same locked chain that was
1869 * The returned chain will be locked.
1871 * WARNING! We do not currently have a local copy of ipdata but
1872 * we do use one later remember that it must be reloaded
1873 * on any modification to the inode, including connects.
1875 chain = hammer2_inode_lock_ex(ip);
1876 error = hammer2_hardlink_consolidate(&trans, ip, &chain, tdip, 0);
1881 * Disconnect (fdip, fname) from the source directory. This will
1882 * disconnect (ip) if it represents a direct file. If (ip) represents
1883 * a hardlink the HARDLINK pointer object will be removed but the
1884 * hardlink will stay intact.
1886 * The target chain may be marked DELETED but will not be destroyed
1887 * since we retain our hold on ip and chain.
1889 error = hammer2_unlink_file(&trans, fdip, fname, fname_len, -1, &hlink);
1890 KKASSERT(error != EAGAIN);
1895 * Reconnect ip to target directory using chain. Chains cannot
1896 * actually be moved, so this will duplicate the chain in the new
1897 * spot and assign it to the ip, replacing the old chain.
1899 * WARNING: chain locks can lock buffer cache buffers, to avoid
1900 * deadlocks we want to unlock before issuing a cache_*()
1901 * op (that might have to lock a vnode).
1903 error = hammer2_inode_connect(&trans, hlink,
1907 KKASSERT(chain != NULL);
1908 hammer2_inode_repoint(ip, (hlink ? ip->pip : tdip), chain);
1909 cache_rename(ap->a_fnch, ap->a_tnch);
1912 hammer2_inode_unlock_ex(ip, chain);
1913 hammer2_inode_drop(ip);
1914 hammer2_trans_done(&trans);
1922 * WARNING: The strategy code cannot safely use hammer2 transactions
1923 * as this can deadlock against vfs_sync's vfsync() call
1924 * if multiple flushes are queued.
1926 static int hammer2_strategy_read(struct vop_strategy_args *ap);
1927 static int hammer2_strategy_write(struct vop_strategy_args *ap);
1928 static void hammer2_strategy_read_callback(hammer2_chain_t *chain,
1929 struct buf *dbp, char *data, void *arg);
1933 hammer2_vop_strategy(struct vop_strategy_args *ap)
1944 error = hammer2_strategy_read(ap);
1945 ++hammer2_iod_file_read;
1948 error = hammer2_strategy_write(ap);
1949 ++hammer2_iod_file_write;
1952 bp->b_error = error = EINVAL;
1953 bp->b_flags |= B_ERROR;
1963 hammer2_strategy_read(struct vop_strategy_args *ap)
1968 hammer2_inode_t *ip;
1969 hammer2_chain_t *parent;
1970 hammer2_chain_t *chain;
1971 hammer2_key_t lbase;
1976 ip = VTOI(ap->a_vp);
1977 nbio = push_bio(bio);
1979 lbase = bio->bio_offset;
1981 KKASSERT(((int)lbase & HAMMER2_PBUFMASK) == 0);
1984 kprintf("read lbase %jd cached %016jx\n",
1985 lbase, nbio->bio_offset);
1988 parent = hammer2_inode_lock_sh(ip);
1989 chain = hammer2_chain_lookup(&parent, lbase, lbase,
1990 HAMMER2_LOOKUP_NODATA |
1991 HAMMER2_LOOKUP_SHARED);
1993 if (chain == NULL) {
1999 bzero(bp->b_data, bp->b_bcount);
2001 } else if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
2003 * Data is embedded in the inode (copy from inode).
2005 hammer2_chain_load_async(chain, hammer2_strategy_read_callback,
2007 } else if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2009 * Data is on-media, issue device I/O and copy.
2011 * XXX direct-IO shortcut could go here XXX.
2013 if (HAMMER2_DEC_COMP(chain->bref.methods) == HAMMER2_COMP_LZ4) {
2015 * Block compression is determined by bref.methods value.
2017 hammer2_blockref_t *bref;
2018 hammer2_off_t pbase;
2019 hammer2_off_t pmask;
2022 bref = &chain->bref;
2023 psize = hammer2_devblksize(chain->bytes);
2024 pmask = (hammer2_off_t)psize - 1;
2025 pbase = bref->data_off & ~pmask;
2026 loff = (int)((bref->data_off &
2027 ~HAMMER2_OFF_MASK_RADIX) - pbase);
2028 nbio->bio_caller_info3.value = loff;
2029 breadcb(chain->hmp->devvp, pbase, psize,
2030 hammer2_decompress_LZ4_callback, nbio);
2031 /* XXX async read dev blk not protected by chain lk */
2032 hammer2_chain_unlock(chain);
2033 } else if (HAMMER2_DEC_COMP(chain->bref.methods) == HAMMER2_COMP_ZLIB) {
2034 hammer2_blockref_t *bref;
2035 hammer2_off_t pbase;
2036 hammer2_off_t pmask;
2039 bref = &chain->bref;
2040 psize = hammer2_devblksize(chain->bytes);
2041 pmask = (hammer2_off_t)psize - 1;
2042 pbase = bref->data_off & ~pmask;
2043 loff = (int)((bref->data_off &
2044 ~HAMMER2_OFF_MASK_RADIX) - pbase);
2045 nbio->bio_caller_info3.value = loff;
2046 breadcb(chain->hmp->devvp, pbase, psize,
2047 hammer2_decompress_ZLIB_callback, nbio);
2048 /* XXX async read dev blk not protected by chain lk */
2049 hammer2_chain_unlock(chain);
2052 hammer2_chain_load_async(chain,
2053 hammer2_strategy_read_callback,
2057 panic("READ PATH: hammer2_strategy_read: unknown bref type");
2060 hammer2_inode_unlock_sh(ip, parent);
2065 * Read callback for block that is not compressed.
2069 hammer2_strategy_read_callback(hammer2_chain_t *chain, struct buf *dbp,
2070 char *data, void *arg)
2072 struct bio *nbio = arg;
2073 struct buf *bp = nbio->bio_buf;
2075 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
2077 * Data is embedded in the inode (copy from inode).
2079 bcopy(((hammer2_inode_data_t *)data)->u.data,
2080 bp->b_data, HAMMER2_EMBEDDED_BYTES);
2081 bzero(bp->b_data + HAMMER2_EMBEDDED_BYTES,
2082 bp->b_bcount - HAMMER2_EMBEDDED_BYTES);
2085 hammer2_chain_unlock(chain);
2087 } else if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2089 * Data is on-media, issue device I/O and copy.
2091 * XXX direct-IO shortcut could go here XXX.
2093 KKASSERT(chain->bytes <= bp->b_bcount);
2094 bcopy(data, bp->b_data, chain->bytes);
2095 if (chain->bytes < bp->b_bcount); {
2096 bzero(bp->b_data + chain->bytes,
2097 bp->b_bcount - chain->bytes);
2099 bp->b_flags |= B_NOTMETA;
2102 hammer2_chain_unlock(chain);
2107 panic("hammer2_strategy_read: unknown bref type");
2108 /*hammer2_chain_unlock(chain);*/
2115 hammer2_strategy_write(struct vop_strategy_args *ap)
2118 * XXX temporary because all write handling is currently
2119 * in the vop_write path (which is incorrect and won't catch
2120 * certain file modifications via mmap()). What we need
2121 * to do is have the strategy_write code queue the bio to
2122 * one or more support threads which will do the complex
2123 * logical->physical work and have the vop_write path just do
2124 * normal operations on the logical buffer.
2126 hammer2_mount_t *hmp;
2129 hammer2_inode_t *ip;
2133 ip = VTOI(ap->a_vp);
2134 hmp = ip->pmp->mount_cluster->hmp;
2136 mtx_lock(&hmp->wthread_mtx);
2137 bioq_insert_tail(&hmp->wthread_bioq, ap->a_bio);
2138 wakeup(&hmp->wthread_bioq);
2139 mtx_unlock(&hmp->wthread_mtx);
2144 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2148 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2150 hammer2_inode_t *ip;
2153 ip = VTOI(ap->a_vp);
2155 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2156 ap->a_fflag, ap->a_cred);
2162 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2165 hammer2_pfsmount_t *pmp;
2169 case (MOUNTCTL_SET_EXPORT):
2170 mp = ap->a_head.a_ops->head.vv_mount;
2173 if (ap->a_ctllen != sizeof(struct export_args))
2176 rc = vfs_export(mp, &pmp->export,
2177 (const struct export_args *)ap->a_ctl);
2180 rc = vop_stdmountctl(ap);
2186 struct vop_ops hammer2_vnode_vops = {
2187 .vop_default = vop_defaultop,
2188 .vop_fsync = hammer2_vop_fsync,
2189 .vop_getpages = vop_stdgetpages,
2190 .vop_putpages = vop_stdputpages,
2191 .vop_access = hammer2_vop_access,
2192 .vop_advlock = hammer2_vop_advlock,
2193 .vop_close = hammer2_vop_close,
2194 .vop_nlink = hammer2_vop_nlink,
2195 .vop_ncreate = hammer2_vop_ncreate,
2196 .vop_nsymlink = hammer2_vop_nsymlink,
2197 .vop_nremove = hammer2_vop_nremove,
2198 .vop_nrmdir = hammer2_vop_nrmdir,
2199 .vop_nrename = hammer2_vop_nrename,
2200 .vop_getattr = hammer2_vop_getattr,
2201 .vop_setattr = hammer2_vop_setattr,
2202 .vop_readdir = hammer2_vop_readdir,
2203 .vop_readlink = hammer2_vop_readlink,
2204 .vop_getpages = vop_stdgetpages,
2205 .vop_putpages = vop_stdputpages,
2206 .vop_read = hammer2_vop_read,
2207 .vop_write = hammer2_vop_write,
2208 .vop_open = hammer2_vop_open,
2209 .vop_inactive = hammer2_vop_inactive,
2210 .vop_reclaim = hammer2_vop_reclaim,
2211 .vop_nresolve = hammer2_vop_nresolve,
2212 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2213 .vop_nmkdir = hammer2_vop_nmkdir,
2214 .vop_ioctl = hammer2_vop_ioctl,
2215 .vop_mountctl = hammer2_vop_mountctl,
2216 .vop_bmap = hammer2_vop_bmap,
2217 .vop_strategy = hammer2_vop_strategy,
2220 struct vop_ops hammer2_spec_vops = {
2224 struct vop_ops hammer2_fifo_vops = {