2 * Copyright (c) 2011-2013 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7 * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Kernel Filesystem interface
39 * NOTE! local ipdata pointers must be reloaded on any modifying operation
40 * to the inode as its underlying chain may have changed.
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/fcntl.h>
49 #include <sys/namei.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/mountctl.h>
53 #include <sys/dirent.h>
55 #include <sys/objcache.h>
56 #include <sys/event.h>
58 #include <vfs/fifofs/fifo.h>
61 #include "hammer2_lz4.h"
63 #include "zlib/hammer2_zlib.h"
65 #define ZFOFFSET (-2LL)
67 static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio,
69 static int hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
70 int ioflag, int seqcount);
71 static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize);
72 static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize);
73 static void hammer2_decompress_LZ4_callback(struct bio *bio);
74 static void hammer2_decompress_ZLIB_callback(struct bio *bio);
76 struct objcache *cache_buffer_read;
77 struct objcache *cache_buffer_write;
80 * Callback used in read path in case that a block is compressed with LZ4.
84 hammer2_decompress_LZ4_callback(struct bio *bio)
86 struct buf *bp = bio->bio_buf;
92 * If BIO_DONE is already set the device buffer was already
93 * fully valid (B_CACHE). If it is not set then I/O was issued
94 * and we have to run I/O completion as the last bio.
96 * Nobody is waiting for our device I/O to complete, we are
97 * responsible for bqrelse()ing it which means we also have to do
98 * the equivalent of biowait() and clear BIO_DONE (which breadcb()
101 * Any preexisting device buffer should match the requested size,
102 * but due to bigblock recycling and other factors there is some
103 * fragility there, so we assert that the device buffer covers
106 if ((bio->bio_flags & BIO_DONE) == 0)
108 bio->bio_flags &= ~(BIO_DONE | BIO_SYNC);
110 obio = bio->bio_caller_info1.ptr;
112 loff = obio->bio_caller_info3.value;
114 if (bp->b_flags & B_ERROR) {
115 obp->b_flags |= B_ERROR;
116 obp->b_error = bp->b_error;
117 } else if (obio->bio_caller_info2.index &&
118 obio->bio_caller_info1.uvalue32 !=
119 crc32(bp->b_data, bp->b_bufsize)) {
120 obp->b_flags |= B_ERROR;
123 KKASSERT(obp->b_bufsize <= 65536);
126 char *compressed_buffer;
127 int *compressed_size;
129 buffer = bp->b_data + loff;
130 compressed_size = (int*)buffer;
131 compressed_buffer = objcache_get(cache_buffer_read, M_INTWAIT);
132 KKASSERT((unsigned int)*compressed_size <= 65536);
133 int result = LZ4_decompress_safe(&buffer[sizeof(int)],
134 compressed_buffer, *compressed_size, obp->b_bufsize);
136 kprintf("READ PATH: Error during decompression."
137 "bio %016jx/%d loff=%d\n",
138 (intmax_t)bio->bio_offset, bio->bio_buf->b_bufsize, loff);
139 /* make sure it isn't random garbage */
140 bzero(compressed_buffer, obp->b_bufsize);
142 KKASSERT(result <= obp->b_bufsize);
143 bcopy(compressed_buffer, obp->b_data, obp->b_bufsize);
144 if (result < obp->b_bufsize)
145 bzero(obp->b_data + result, obp->b_bufsize - result);
146 objcache_put(cache_buffer_read, compressed_buffer);
148 obp->b_flags |= B_AGE;
155 * Callback used in read path in case that a block is compressed with ZLIB.
156 * It is almost identical to LZ4 callback, so in theory they can be unified,
157 * but we didn't want to make changes in bio structure for that.
161 hammer2_decompress_ZLIB_callback(struct bio *bio)
163 struct buf *bp = bio->bio_buf;
169 * If BIO_DONE is already set the device buffer was already
170 * fully valid (B_CACHE). If it is not set then I/O was issued
171 * and we have to run I/O completion as the last bio.
173 * Nobody is waiting for our device I/O to complete, we are
174 * responsible for bqrelse()ing it which means we also have to do
175 * the equivalent of biowait() and clear BIO_DONE (which breadcb()
178 * Any preexisting device buffer should match the requested size,
179 * but due to bigblock recycling and other factors there is some
180 * fragility there, so we assert that the device buffer covers
183 if ((bio->bio_flags & BIO_DONE) == 0)
185 bio->bio_flags &= ~(BIO_DONE | BIO_SYNC);
187 obio = bio->bio_caller_info1.ptr;
189 loff = obio->bio_caller_info3.value;
191 if (bp->b_flags & B_ERROR) {
192 obp->b_flags |= B_ERROR;
193 obp->b_error = bp->b_error;
194 } else if (obio->bio_caller_info2.index &&
195 obio->bio_caller_info1.uvalue32 !=
196 crc32(bp->b_data, bp->b_bufsize)) {
197 obp->b_flags |= B_ERROR;
200 KKASSERT(obp->b_bufsize <= 65536);
203 char *compressed_buffer;
206 z_stream strm_decompress;
208 strm_decompress.avail_in = 0;
209 strm_decompress.next_in = Z_NULL;
211 ret = inflateInit(&strm_decompress);
214 kprintf("HAMMER2 ZLIB: Fatal error in inflateInit.\n");
216 buffer = bp->b_data + loff;
217 compressed_buffer = objcache_get(cache_buffer_read, M_INTWAIT);
218 strm_decompress.next_in = buffer;
220 /* XXX supply proper size, subset of device bp */
221 strm_decompress.avail_in = bp->b_bufsize - loff;
222 strm_decompress.next_out = compressed_buffer;
223 strm_decompress.avail_out = obp->b_bufsize;
225 ret = inflate(&strm_decompress, Z_FINISH);
226 if (ret != Z_STREAM_END) {
227 kprintf("HAMMER2 ZLIB: Fatar error during decompression.\n");
228 bzero(compressed_buffer, obp->b_bufsize);
230 bcopy(compressed_buffer, obp->b_data, obp->b_bufsize);
231 int result = obp->b_bufsize - strm_decompress.avail_out;
232 if (result < obp->b_bufsize)
233 bzero(obp->b_data + result, strm_decompress.avail_out);
234 objcache_put(cache_buffer_read, compressed_buffer);
236 obp->b_flags |= B_AGE;
237 ret = inflateEnd(&strm_decompress);
245 hammer2_knote(struct vnode *vp, int flags)
248 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
252 * Last reference to a vnode is going away but it is still cached.
256 hammer2_vop_inactive(struct vop_inactive_args *ap)
259 hammer2_chain_t *parent;
274 * Detect updates to the embedded data which may be synchronized by
275 * the strategy code. Simply mark the inode modified so it gets
276 * picked up by our normal flush.
278 parent = hammer2_inode_lock_ex(ip);
282 * Check for deleted inodes and recycle immediately.
284 if (parent->flags & HAMMER2_CHAIN_DELETED) {
285 hammer2_inode_unlock_ex(ip, parent);
288 hammer2_inode_unlock_ex(ip, parent);
294 * Reclaim a vnode so that it can be reused; after the inode is
295 * disassociated, the filesystem must manage it alone.
299 hammer2_vop_reclaim(struct vop_reclaim_args *ap)
301 hammer2_chain_t *chain;
304 hammer2_trans_t trans;
314 * Set update_tid so we can detect and propagate the DESTROYED
315 * bit in the flush code.
317 * ip->chain might be stale, correct it before checking as older
318 * versions of the chain are likely marked deleted even if the
319 * file hasn't been. XXX ip->chain should never be stale on
322 chain = hammer2_inode_lock_ex(ip);
324 if (chain->next_parent)
325 kprintf("RECLAIM DUPLINKED IP: %p ip->ch=%p ch=%p np=%p\n",
326 ip, ip->chain, chain, chain->next_parent);
330 * The final close of a deleted file or directory marks it for
331 * destruction. The DESTROYED flag allows the flusher to shortcut
332 * any modified blocks still unflushed (that is, just ignore them).
334 * HAMMER2 usually does not try to optimize the freemap by returning
335 * deleted blocks to it as it does not usually know how many snapshots
336 * might be referencing portions of the file/dir. XXX TODO.
338 * XXX TODO - However, any modified file as-of when a snapshot is made
339 * cannot use this optimization as some of the modifications
340 * may wind up being part of the snapshot.
344 if (chain->flags & HAMMER2_CHAIN_DELETED) {
345 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROYED);
346 spin_lock(&chain->core->cst.spin);
347 chain->core->update_tid = HAMMER2_MAX_TID; /* special case */
348 spin_unlock(&chain->core->cst.spin);
352 * NOTE! We do not attempt to flush chains here, flushing is
353 * really fragile and could also deadlock.
356 hammer2_inode_unlock_ex(ip, chain); /* unlock */
357 hammer2_inode_drop(ip); /* vp ref */
358 /* chain no longer referenced */
359 /* chain = NULL; not needed */
362 * XXX handle background sync when ip dirty, kernel will no longer
363 * notify us regarding this inode because there is no longer a
364 * vnode attached to it.
372 hammer2_vop_fsync(struct vop_fsync_args *ap)
375 hammer2_trans_t trans;
376 hammer2_chain_t *chain;
383 * TRANS_ISFLUSH allocates two transaction ids, one for concurrent
384 * buffer syncs, and one for our flush.
386 * WARNING: The vfsync interacts with the buffer cache and might
387 * block, we can't hold the inode lock and we can't
388 * have a flush transaction pending.
390 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_ISFLUSH |
391 HAMMER2_TRANS_INVFSYNC);
392 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
393 hammer2_trans_clear_invfsync(&trans);
396 * Calling chain_flush here creates a lot of duplicative
397 * COW operations due to non-optimal vnode ordering.
399 * Only do it for an actual fsync() syscall. The other forms
400 * which call this function will eventually call chain_flush
401 * on the volume root as a catch-all, which is far more optimal.
403 chain = hammer2_inode_lock_ex(ip);
404 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
406 if (ip->flags & (HAMMER2_INODE_RESIZED|HAMMER2_INODE_MTIME))
407 hammer2_inode_fsync(&trans, ip, &chain);
409 if (ap->a_flags & VOP_FSYNC_SYSCALL) {
410 hammer2_chain_flush(&trans, &chain);
412 hammer2_inode_unlock_ex(ip, chain);
413 hammer2_trans_done(&trans);
420 hammer2_vop_access(struct vop_access_args *ap)
422 hammer2_inode_t *ip = VTOI(ap->a_vp);
423 hammer2_inode_data_t *ipdata;
424 hammer2_chain_t *chain;
429 chain = hammer2_inode_lock_sh(ip);
430 ipdata = &chain->data->ipdata;
431 uid = hammer2_to_unix_xid(&ipdata->uid);
432 gid = hammer2_to_unix_xid(&ipdata->gid);
433 error = vop_helper_access(ap, uid, gid, ipdata->mode, ipdata->uflags);
434 hammer2_inode_unlock_sh(ip, chain);
441 hammer2_vop_getattr(struct vop_getattr_args *ap)
443 hammer2_inode_data_t *ipdata;
444 hammer2_chain_t *chain;
445 hammer2_pfsmount_t *pmp;
456 chain = hammer2_inode_lock_sh(ip);
457 ipdata = &chain->data->ipdata;
459 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
460 vap->va_fileid = ipdata->inum;
461 vap->va_mode = ipdata->mode;
462 vap->va_nlink = ipdata->nlinks;
463 vap->va_uid = hammer2_to_unix_xid(&ipdata->uid);
464 vap->va_gid = hammer2_to_unix_xid(&ipdata->gid);
467 vap->va_size = ip->size; /* protected by shared lock */
468 vap->va_blocksize = HAMMER2_PBUFSIZE;
469 vap->va_flags = ipdata->uflags;
470 hammer2_time_to_timespec(ipdata->ctime, &vap->va_ctime);
471 hammer2_time_to_timespec(ipdata->mtime, &vap->va_mtime);
472 hammer2_time_to_timespec(ipdata->mtime, &vap->va_atime);
474 vap->va_bytes = vap->va_size; /* XXX */
475 vap->va_type = hammer2_get_vtype(chain);
477 vap->va_uid_uuid = ipdata->uid;
478 vap->va_gid_uuid = ipdata->gid;
479 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
482 hammer2_inode_unlock_sh(ip, chain);
489 hammer2_vop_setattr(struct vop_setattr_args *ap)
491 hammer2_inode_data_t *ipdata;
493 hammer2_chain_t *chain;
494 hammer2_trans_t trans;
504 hammer2_update_time(&ctime);
511 hammer2_chain_memory_wait(ip->pmp);
512 hammer2_trans_init(&trans, ip->pmp, 0);
513 chain = hammer2_inode_lock_ex(ip);
514 ipdata = &chain->data->ipdata;
517 if (vap->va_flags != VNOVAL) {
520 flags = ipdata->uflags;
521 error = vop_helper_setattr_flags(&flags, vap->va_flags,
522 hammer2_to_unix_xid(&ipdata->uid),
525 if (ipdata->uflags != flags) {
526 ipdata = hammer2_chain_modify_ip(&trans, ip,
528 ipdata->uflags = flags;
529 ipdata->ctime = ctime;
530 kflags |= NOTE_ATTRIB;
532 if (ipdata->uflags & (IMMUTABLE | APPEND)) {
539 if (ipdata->uflags & (IMMUTABLE | APPEND)) {
543 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
544 mode_t cur_mode = ipdata->mode;
545 uid_t cur_uid = hammer2_to_unix_xid(&ipdata->uid);
546 gid_t cur_gid = hammer2_to_unix_xid(&ipdata->gid);
550 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
552 &cur_uid, &cur_gid, &cur_mode);
554 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
555 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
556 if (bcmp(&uuid_uid, &ipdata->uid, sizeof(uuid_uid)) ||
557 bcmp(&uuid_gid, &ipdata->gid, sizeof(uuid_gid)) ||
558 ipdata->mode != cur_mode
560 ipdata = hammer2_chain_modify_ip(&trans, ip,
562 ipdata->uid = uuid_uid;
563 ipdata->gid = uuid_gid;
564 ipdata->mode = cur_mode;
565 ipdata->ctime = ctime;
567 kflags |= NOTE_ATTRIB;
574 if (vap->va_size != VNOVAL && ip->size != vap->va_size) {
577 if (vap->va_size == ip->size)
579 hammer2_inode_unlock_ex(ip, chain);
580 if (vap->va_size < ip->size) {
581 hammer2_truncate_file(ip, vap->va_size);
583 hammer2_extend_file(ip, vap->va_size);
585 chain = hammer2_inode_lock_ex(ip);
586 ipdata = &chain->data->ipdata; /* RELOAD */
595 /* atime not supported */
596 if (vap->va_atime.tv_sec != VNOVAL) {
597 ipdata = hammer2_chain_modify_ip(&trans, ip, &chain, 0);
598 ipdata->atime = hammer2_timespec_to_time(&vap->va_atime);
599 kflags |= NOTE_ATTRIB;
602 if (vap->va_mtime.tv_sec != VNOVAL) {
603 ipdata = hammer2_chain_modify_ip(&trans, ip, &chain, 0);
604 ipdata->mtime = hammer2_timespec_to_time(&vap->va_mtime);
605 kflags |= NOTE_ATTRIB;
608 if (vap->va_mode != (mode_t)VNOVAL) {
609 mode_t cur_mode = ipdata->mode;
610 uid_t cur_uid = hammer2_to_unix_xid(&ipdata->uid);
611 gid_t cur_gid = hammer2_to_unix_xid(&ipdata->gid);
613 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
614 cur_uid, cur_gid, &cur_mode);
615 if (error == 0 && ipdata->mode != cur_mode) {
616 ipdata = hammer2_chain_modify_ip(&trans, ip, &chain, 0);
617 ipdata->mode = cur_mode;
618 ipdata->ctime = ctime;
619 kflags |= NOTE_ATTRIB;
624 * If a truncation occurred we must call inode_fsync() now in order
625 * to trim the related data chains, otherwise a later expansion can
628 hammer2_inode_fsync(&trans, ip, &chain);
631 * Cleanup. If domtime is set an additional inode modification
632 * must be flagged. All other modifications will have already
633 * set INODE_MODIFIED and called vsetisdirty().
637 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED |
638 HAMMER2_INODE_MTIME);
641 hammer2_inode_unlock_ex(ip, chain);
642 hammer2_trans_done(&trans);
643 hammer2_knote(ip->vp, kflags);
650 hammer2_vop_readdir(struct vop_readdir_args *ap)
652 hammer2_inode_data_t *ipdata;
654 hammer2_inode_t *xip;
655 hammer2_chain_t *parent;
656 hammer2_chain_t *chain;
657 hammer2_chain_t *xchain;
659 hammer2_key_t key_next;
665 int cache_index = -1;
673 saveoff = uio->uio_offset;
676 * Setup cookies directory entry cookies if requested
678 if (ap->a_ncookies) {
679 ncookies = uio->uio_resid / 16 + 1;
682 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
689 parent = hammer2_inode_lock_sh(ip);
690 ipdata = &parent->data->ipdata;
693 * Handle artificial entries. To ensure that only positive 64 bit
694 * quantities are returned to userland we always strip off bit 63.
695 * The hash code is designed such that codes 0x0000-0x7FFF are not
696 * used, allowing us to use these codes for articial entries.
698 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
699 * allow '..' to cross the mount point into (e.g.) the super-root.
702 chain = (void *)(intptr_t)-1; /* non-NULL for early goto done case */
705 inum = ipdata->inum & HAMMER2_DIRHASH_USERMSK;
706 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
710 cookies[cookie_index] = saveoff;
713 if (cookie_index == ncookies)
719 * Be careful with lockorder when accessing ".."
721 * (ip is the current dir. xip is the parent dir).
723 inum = ipdata->inum & HAMMER2_DIRHASH_USERMSK;
724 while (ip->pip != NULL && ip != ip->pmp->iroot) {
726 hammer2_inode_ref(xip);
727 hammer2_inode_unlock_sh(ip, parent);
728 xchain = hammer2_inode_lock_sh(xip);
729 parent = hammer2_inode_lock_sh(ip);
730 hammer2_inode_drop(xip);
731 if (xip == ip->pip) {
732 inum = xchain->data->ipdata.inum &
733 HAMMER2_DIRHASH_USERMSK;
734 hammer2_inode_unlock_sh(xip, xchain);
737 hammer2_inode_unlock_sh(xip, xchain);
739 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
743 cookies[cookie_index] = saveoff;
746 if (cookie_index == ncookies)
750 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
753 * parent is the inode chain, already locked for us. Don't
754 * double lock shared locks as this will screw up upgrades.
759 chain = hammer2_chain_lookup(&parent, &key_next, lkey, lkey,
760 &cache_index, HAMMER2_LOOKUP_SHARED);
762 chain = hammer2_chain_lookup(&parent, &key_next,
763 lkey, (hammer2_key_t)-1,
765 HAMMER2_LOOKUP_SHARED);
768 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
769 dtype = hammer2_get_dtype(chain);
770 saveoff = chain->bref.key & HAMMER2_DIRHASH_USERMSK;
771 r = vop_write_dirent(&error, uio,
772 chain->data->ipdata.inum &
773 HAMMER2_DIRHASH_USERMSK,
775 chain->data->ipdata.name_len,
776 chain->data->ipdata.filename);
780 cookies[cookie_index] = saveoff;
783 /* XXX chain error */
784 kprintf("bad chain type readdir %d\n",
789 * Keys may not be returned in order so once we have a
790 * placemarker (chain) the scan must allow the full range
791 * or some entries will be missed.
793 chain = hammer2_chain_next(&parent, chain, &key_next,
794 key_next, (hammer2_key_t)-1,
795 &cache_index, HAMMER2_LOOKUP_SHARED);
797 saveoff = (chain->bref.key &
798 HAMMER2_DIRHASH_USERMSK) + 1;
800 saveoff = (hammer2_key_t)-1;
802 if (cookie_index == ncookies)
806 hammer2_chain_unlock(chain);
808 hammer2_inode_unlock_sh(ip, parent);
810 *ap->a_eofflag = (chain == NULL);
811 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
812 if (error && cookie_index == 0) {
814 kfree(cookies, M_TEMP);
816 *ap->a_cookies = NULL;
820 *ap->a_ncookies = cookie_index;
821 *ap->a_cookies = cookies;
828 * hammer2_vop_readlink { vp, uio, cred }
832 hammer2_vop_readlink(struct vop_readlink_args *ap)
839 if (vp->v_type != VLNK)
843 error = hammer2_read_file(ip, ap->a_uio, 0);
849 hammer2_vop_read(struct vop_read_args *ap)
859 * Read operations supported on this vnode?
862 if (vp->v_type != VREG)
872 seqcount = ap->a_ioflag >> 16;
873 bigread = (uio->uio_resid > 100 * 1024 * 1024);
875 error = hammer2_read_file(ip, uio, seqcount);
881 hammer2_vop_write(struct vop_write_args *ap)
884 hammer2_trans_t trans;
893 * Read operations supported on this vnode?
896 if (vp->v_type != VREG)
908 seqcount = ap->a_ioflag >> 16;
909 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
912 * Check resource limit
914 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
915 uio->uio_offset + uio->uio_resid >
916 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
917 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
921 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
924 * The transaction interlocks against flushes initiations
925 * (note: but will run concurrently with the actual flush).
927 hammer2_trans_init(&trans, ip->pmp, 0);
928 error = hammer2_write_file(ip, uio, ap->a_ioflag, seqcount);
929 hammer2_trans_done(&trans);
935 * Perform read operations on a file or symlink given an UNLOCKED
938 * The passed ip is not locked.
942 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
953 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
955 ccms_thread_unlock(&ip->topo_cst);
957 while (uio->uio_resid > 0 && uio->uio_offset < size) {
964 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
967 error = cluster_read(ip->vp, leof, lbase, lblksize,
968 uio->uio_resid, seqcount * BKVASIZE,
973 loff = (int)(uio->uio_offset - lbase);
975 if (n > uio->uio_resid)
977 if (n > size - uio->uio_offset)
978 n = (int)(size - uio->uio_offset);
979 bp->b_flags |= B_AGE;
980 uiomove((char *)bp->b_data + loff, n, uio);
987 * Write to the file represented by the inode via the logical buffer cache.
988 * The inode may represent a regular file or a symlink.
990 * The inode must not be locked.
994 hammer2_write_file(hammer2_inode_t *ip,
995 struct uio *uio, int ioflag, int seqcount)
997 hammer2_key_t old_eof;
998 hammer2_key_t new_eof;
1007 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1008 if (ioflag & IO_APPEND)
1009 uio->uio_offset = ip->size;
1011 ccms_thread_unlock(&ip->topo_cst);
1014 * Extend the file if necessary. If the write fails at some point
1015 * we will truncate it back down to cover as much as we were able
1018 * Doing this now makes it easier to calculate buffer sizes in
1025 if (uio->uio_offset + uio->uio_resid > old_eof) {
1026 new_eof = uio->uio_offset + uio->uio_resid;
1028 hammer2_extend_file(ip, new_eof);
1029 kflags |= NOTE_EXTEND;
1037 while (uio->uio_resid > 0) {
1038 hammer2_key_t lbase;
1046 * Don't allow the buffer build to blow out the buffer
1049 if ((ioflag & IO_RECURSE) == 0)
1050 bwillwrite(HAMMER2_PBUFSIZE);
1053 * This nominally tells us how much we can cluster and
1054 * what the logical buffer size needs to be. Currently
1055 * we don't try to cluster the write and just handle one
1058 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
1060 loff = (int)(uio->uio_offset - lbase);
1062 KKASSERT(lblksize <= 65536);
1065 * Calculate bytes to copy this transfer and whether the
1066 * copy completely covers the buffer or not.
1069 n = lblksize - loff;
1070 if (n > uio->uio_resid) {
1072 if (loff == lbase && uio->uio_offset + n == new_eof)
1084 if (uio->uio_segflg == UIO_NOCOPY) {
1086 * Issuing a write with the same data backing the
1087 * buffer. Instantiate the buffer to collect the
1088 * backing vm pages, then read-in any missing bits.
1090 * This case is used by vop_stdputpages().
1092 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
1093 if ((bp->b_flags & B_CACHE) == 0) {
1095 error = bread(ip->vp, lbase, lblksize, &bp);
1097 } else if (trivial) {
1099 * Even though we are entirely overwriting the buffer
1100 * we may still have to zero it out to avoid a
1101 * mmap/write visibility issue.
1103 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
1104 if ((bp->b_flags & B_CACHE) == 0)
1108 * Partial overwrite, read in any missing bits then
1109 * replace the portion being written.
1111 * (The strategy code will detect zero-fill physical
1112 * blocks for this case).
1114 error = bread(ip->vp, lbase, lblksize, &bp);
1125 * Ok, copy the data in
1127 error = uiomove(bp->b_data + loff, n, uio);
1128 kflags |= NOTE_WRITE;
1136 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
1137 * with IO_SYNC or IO_ASYNC set. These writes
1138 * must be handled as the pageout daemon expects.
1140 if (ioflag & IO_SYNC) {
1142 } else if ((ioflag & IO_DIRECT) && endofblk) {
1144 } else if (ioflag & IO_ASYNC) {
1152 * Cleanup. If we extended the file EOF but failed to write through
1153 * the entire write is a failure and we have to back-up.
1155 if (error && new_eof != old_eof) {
1156 hammer2_truncate_file(ip, old_eof);
1157 } else if (modified) {
1158 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1159 hammer2_update_time(&ip->mtime);
1160 atomic_set_int(&ip->flags, HAMMER2_INODE_MTIME);
1161 ccms_thread_unlock(&ip->topo_cst);
1163 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1164 hammer2_knote(ip->vp, kflags);
1165 vsetisdirty(ip->vp);
1171 * Truncate the size of a file. The inode must not be locked.
1173 * NOTE: Caller handles setting HAMMER2_INODE_MODIFIED
1177 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1179 hammer2_key_t lbase;
1183 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1184 nvtruncbuf(ip->vp, nsize,
1185 nblksize, (int)nsize & (nblksize - 1),
1188 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1190 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1191 ccms_thread_unlock(&ip->topo_cst);
1195 * Extend the size of a file. The inode must not be locked.
1197 * NOTE: Caller handles setting HAMMER2_INODE_MODIFIED
1201 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1203 hammer2_key_t lbase;
1204 hammer2_key_t osize;
1208 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1211 ccms_thread_unlock(&ip->topo_cst);
1214 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1215 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1221 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1226 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1228 hammer2_inode_t *ip;
1229 hammer2_inode_t *dip;
1230 hammer2_chain_t *parent;
1231 hammer2_chain_t *chain;
1232 hammer2_chain_t *ochain;
1233 hammer2_trans_t trans;
1234 hammer2_key_t key_next;
1236 struct namecache *ncp;
1237 const uint8_t *name;
1240 int cache_index = -1;
1243 dip = VTOI(ap->a_dvp);
1244 ncp = ap->a_nch->ncp;
1245 name = ncp->nc_name;
1246 name_len = ncp->nc_nlen;
1247 lhc = hammer2_dirhash(name, name_len);
1250 * Note: In DragonFly the kernel handles '.' and '..'.
1252 parent = hammer2_inode_lock_sh(dip);
1253 chain = hammer2_chain_lookup(&parent, &key_next,
1254 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1255 &cache_index, HAMMER2_LOOKUP_SHARED);
1257 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1258 name_len == chain->data->ipdata.name_len &&
1259 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1262 chain = hammer2_chain_next(&parent, chain, &key_next,
1264 lhc + HAMMER2_DIRHASH_LOMASK,
1265 &cache_index, HAMMER2_LOOKUP_SHARED);
1267 hammer2_inode_unlock_sh(dip, parent);
1270 * If the inode represents a forwarding entry for a hardlink we have
1271 * to locate the actual inode. The original ip is saved for possible
1272 * deconsolidation. (ip) will only be set to non-NULL when we have
1273 * to locate the real file via a hardlink. ip will be referenced but
1274 * not locked in that situation. chain is passed in locked and
1277 * XXX what kind of chain lock?
1280 if (chain && chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
1281 error = hammer2_hardlink_find(dip, &chain, &ochain);
1283 kprintf("hammer2: unable to find hardlink\n");
1285 hammer2_chain_unlock(chain);
1293 * Deconsolidate any hardlink whos nlinks == 1. Ignore errors.
1294 * If an error occurs chain and ip are left alone.
1296 * XXX upgrade shared lock?
1298 if (ochain && chain &&
1299 chain->data->ipdata.nlinks == 1 && !dip->pmp->ronly) {
1300 kprintf("hammer2: need to unconsolidate hardlink for %s\n",
1301 chain->data->ipdata.filename);
1302 /* XXX retain shared lock on dip? (currently not held) */
1303 hammer2_trans_init(&trans, dip->pmp, 0);
1304 hammer2_hardlink_deconsolidate(&trans, dip, &chain, &ochain);
1305 hammer2_trans_done(&trans);
1309 * Acquire the related vnode
1311 * NOTE: For error processing, only ENOENT resolves the namecache
1312 * entry to NULL, otherwise we just return the error and
1313 * leave the namecache unresolved.
1315 * NOTE: multiple hammer2_inode structures can be aliased to the
1316 * same chain element, for example for hardlinks. This
1317 * use case does not 'reattach' inode associations that
1318 * might already exist, but always allocates a new one.
1320 * WARNING: inode structure is locked exclusively via inode_get
1321 * but chain was locked shared. inode_unlock_ex()
1322 * will handle it properly.
1325 ip = hammer2_inode_get(dip->pmp, dip, chain);
1326 vp = hammer2_igetv(ip, &error);
1329 cache_setvp(ap->a_nch, vp);
1330 } else if (error == ENOENT) {
1331 cache_setvp(ap->a_nch, NULL);
1333 hammer2_inode_unlock_ex(ip, chain);
1336 * The vp should not be released until after we've disposed
1337 * of our locks, because it might cause vop_inactive() to
1344 cache_setvp(ap->a_nch, NULL);
1347 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1348 ("resolve error %d/%p chain %p ap %p\n",
1349 error, ap->a_nch->ncp->nc_vp, chain, ap));
1351 hammer2_chain_drop(ochain);
1357 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1359 hammer2_inode_t *dip;
1360 hammer2_inode_t *ip;
1361 hammer2_chain_t *parent;
1364 dip = VTOI(ap->a_dvp);
1366 if ((ip = dip->pip) == NULL) {
1370 parent = hammer2_inode_lock_ex(ip);
1371 *ap->a_vpp = hammer2_igetv(ip, &error);
1372 hammer2_inode_unlock_ex(ip, parent);
1379 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1381 hammer2_inode_t *dip;
1382 hammer2_inode_t *nip;
1383 hammer2_trans_t trans;
1384 hammer2_chain_t *chain;
1385 struct namecache *ncp;
1386 const uint8_t *name;
1390 dip = VTOI(ap->a_dvp);
1391 if (dip->pmp->ronly)
1394 ncp = ap->a_nch->ncp;
1395 name = ncp->nc_name;
1396 name_len = ncp->nc_nlen;
1398 hammer2_chain_memory_wait(dip->pmp);
1399 hammer2_trans_init(&trans, dip->pmp, 0);
1400 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1401 name, name_len, &chain, &error);
1403 KKASSERT(nip == NULL);
1406 *ap->a_vpp = hammer2_igetv(nip, &error);
1407 hammer2_inode_unlock_ex(nip, chain);
1409 hammer2_trans_done(&trans);
1412 cache_setunresolved(ap->a_nch);
1413 cache_setvp(ap->a_nch, *ap->a_vpp);
1419 * Return the largest contiguous physical disk range for the logical
1420 * request, in bytes.
1422 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
1424 * Basically disabled, the logical buffer write thread has to deal with
1425 * buffers one-at-a-time.
1429 hammer2_vop_bmap(struct vop_bmap_args *ap)
1431 *ap->a_doffsetp = NOOFFSET;
1436 return (EOPNOTSUPP);
1441 hammer2_vop_open(struct vop_open_args *ap)
1443 return vop_stdopen(ap);
1447 * hammer2_vop_advlock { vp, id, op, fl, flags }
1451 hammer2_vop_advlock(struct vop_advlock_args *ap)
1453 hammer2_inode_t *ip = VTOI(ap->a_vp);
1454 hammer2_chain_t *parent;
1457 parent = hammer2_inode_lock_sh(ip);
1458 size = parent->data->ipdata.size;
1459 hammer2_inode_unlock_sh(ip, parent);
1460 return (lf_advlock(ap, &ip->advlock, size));
1466 hammer2_vop_close(struct vop_close_args *ap)
1468 return vop_stdclose(ap);
1472 * hammer2_vop_nlink { nch, dvp, vp, cred }
1474 * Create a hardlink from (vp) to {dvp, nch}.
1478 hammer2_vop_nlink(struct vop_nlink_args *ap)
1480 hammer2_inode_t *dip; /* target directory to create link in */
1481 hammer2_inode_t *ip; /* inode we are hardlinking to */
1482 hammer2_chain_t *chain;
1483 hammer2_trans_t trans;
1484 struct namecache *ncp;
1485 const uint8_t *name;
1489 dip = VTOI(ap->a_dvp);
1490 if (dip->pmp->ronly)
1493 ncp = ap->a_nch->ncp;
1494 name = ncp->nc_name;
1495 name_len = ncp->nc_nlen;
1498 * ip represents the file being hardlinked. The file could be a
1499 * normal file or a hardlink target if it has already been hardlinked.
1500 * If ip is a hardlinked target then ip->pip represents the location
1501 * of the hardlinked target, NOT the location of the hardlink pointer.
1503 * Bump nlinks and potentially also create or move the hardlink
1504 * target in the parent directory common to (ip) and (dip). The
1505 * consolidation code can modify ip->chain and ip->pip. The
1506 * returned chain is locked.
1508 ip = VTOI(ap->a_vp);
1509 hammer2_chain_memory_wait(ip->pmp);
1510 hammer2_trans_init(&trans, ip->pmp, 0);
1512 chain = hammer2_inode_lock_ex(ip);
1513 error = hammer2_hardlink_consolidate(&trans, ip, &chain, dip, 1);
1518 * Create a directory entry connected to the specified chain.
1519 * The hardlink consolidation code has already adjusted ip->pip
1520 * to the common parent directory containing the actual hardlink
1522 * (which may be different from dip where we created our hardlink
1523 * entry. ip->chain always represents the actual hardlink and not
1524 * any of the pointers to the actual hardlink).
1526 error = hammer2_inode_connect(&trans, 1,
1530 cache_setunresolved(ap->a_nch);
1531 cache_setvp(ap->a_nch, ap->a_vp);
1534 hammer2_inode_unlock_ex(ip, chain);
1535 hammer2_trans_done(&trans);
1541 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1543 * The operating system has already ensured that the directory entry
1544 * does not exist and done all appropriate namespace locking.
1548 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1550 hammer2_inode_t *dip;
1551 hammer2_inode_t *nip;
1552 hammer2_trans_t trans;
1553 hammer2_chain_t *nchain;
1554 struct namecache *ncp;
1555 const uint8_t *name;
1559 dip = VTOI(ap->a_dvp);
1560 if (dip->pmp->ronly)
1563 ncp = ap->a_nch->ncp;
1564 name = ncp->nc_name;
1565 name_len = ncp->nc_nlen;
1566 hammer2_chain_memory_wait(dip->pmp);
1567 hammer2_trans_init(&trans, dip->pmp, 0);
1569 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1570 name, name_len, &nchain, &error);
1572 KKASSERT(nip == NULL);
1575 *ap->a_vpp = hammer2_igetv(nip, &error);
1576 hammer2_inode_unlock_ex(nip, nchain);
1578 hammer2_trans_done(&trans);
1581 cache_setunresolved(ap->a_nch);
1582 cache_setvp(ap->a_nch, *ap->a_vpp);
1592 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1594 hammer2_inode_t *dip;
1595 hammer2_inode_t *nip;
1596 hammer2_trans_t trans;
1597 hammer2_chain_t *nchain;
1598 struct namecache *ncp;
1599 const uint8_t *name;
1603 dip = VTOI(ap->a_dvp);
1604 if (dip->pmp->ronly)
1607 ncp = ap->a_nch->ncp;
1608 name = ncp->nc_name;
1609 name_len = ncp->nc_nlen;
1610 hammer2_chain_memory_wait(dip->pmp);
1611 hammer2_trans_init(&trans, dip->pmp, 0);
1613 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1614 name, name_len, &nchain, &error);
1616 KKASSERT(nip == NULL);
1619 *ap->a_vpp = hammer2_igetv(nip, &error);
1620 hammer2_inode_unlock_ex(nip, nchain);
1622 hammer2_trans_done(&trans);
1625 cache_setunresolved(ap->a_nch);
1626 cache_setvp(ap->a_nch, *ap->a_vpp);
1632 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1636 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1638 hammer2_inode_t *dip;
1639 hammer2_inode_t *nip;
1640 hammer2_chain_t *nparent;
1641 hammer2_trans_t trans;
1642 struct namecache *ncp;
1643 const uint8_t *name;
1647 dip = VTOI(ap->a_dvp);
1648 if (dip->pmp->ronly)
1651 ncp = ap->a_nch->ncp;
1652 name = ncp->nc_name;
1653 name_len = ncp->nc_nlen;
1654 hammer2_chain_memory_wait(dip->pmp);
1655 hammer2_trans_init(&trans, dip->pmp, 0);
1657 ap->a_vap->va_type = VLNK; /* enforce type */
1659 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1660 name, name_len, &nparent, &error);
1662 KKASSERT(nip == NULL);
1664 hammer2_trans_done(&trans);
1667 *ap->a_vpp = hammer2_igetv(nip, &error);
1670 * Build the softlink (~like file data) and finalize the namecache.
1676 hammer2_inode_data_t *nipdata;
1678 nipdata = &nip->chain->data->ipdata;
1679 bytes = strlen(ap->a_target);
1681 if (bytes <= HAMMER2_EMBEDDED_BYTES) {
1682 KKASSERT(nipdata->op_flags &
1683 HAMMER2_OPFLAG_DIRECTDATA);
1684 bcopy(ap->a_target, nipdata->u.data, bytes);
1685 nipdata->size = bytes;
1687 hammer2_inode_unlock_ex(nip, nparent);
1689 hammer2_inode_unlock_ex(nip, nparent);
1690 bzero(&auio, sizeof(auio));
1691 bzero(&aiov, sizeof(aiov));
1692 auio.uio_iov = &aiov;
1693 auio.uio_segflg = UIO_SYSSPACE;
1694 auio.uio_rw = UIO_WRITE;
1695 auio.uio_resid = bytes;
1696 auio.uio_iovcnt = 1;
1697 auio.uio_td = curthread;
1698 aiov.iov_base = ap->a_target;
1699 aiov.iov_len = bytes;
1700 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1701 nipdata = &nip->chain->data->ipdata; /* RELOAD */
1702 /* XXX handle error */
1706 hammer2_inode_unlock_ex(nip, nparent);
1708 hammer2_trans_done(&trans);
1711 * Finalize namecache
1714 cache_setunresolved(ap->a_nch);
1715 cache_setvp(ap->a_nch, *ap->a_vpp);
1716 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1722 * hammer2_vop_nremove { nch, dvp, cred }
1726 hammer2_vop_nremove(struct vop_nremove_args *ap)
1728 hammer2_inode_t *dip;
1729 hammer2_trans_t trans;
1730 struct namecache *ncp;
1731 const uint8_t *name;
1735 dip = VTOI(ap->a_dvp);
1736 if (dip->pmp->ronly)
1739 ncp = ap->a_nch->ncp;
1740 name = ncp->nc_name;
1741 name_len = ncp->nc_nlen;
1742 hammer2_chain_memory_wait(dip->pmp);
1743 hammer2_trans_init(&trans, dip->pmp, 0);
1744 error = hammer2_unlink_file(&trans, dip, name, name_len, 0, NULL);
1745 hammer2_trans_done(&trans);
1747 cache_unlink(ap->a_nch);
1753 * hammer2_vop_nrmdir { nch, dvp, cred }
1757 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1759 hammer2_inode_t *dip;
1760 hammer2_trans_t trans;
1761 struct namecache *ncp;
1762 const uint8_t *name;
1766 dip = VTOI(ap->a_dvp);
1767 if (dip->pmp->ronly)
1770 ncp = ap->a_nch->ncp;
1771 name = ncp->nc_name;
1772 name_len = ncp->nc_nlen;
1774 hammer2_chain_memory_wait(dip->pmp);
1775 hammer2_trans_init(&trans, dip->pmp, 0);
1776 error = hammer2_unlink_file(&trans, dip, name, name_len, 1, NULL);
1777 hammer2_trans_done(&trans);
1779 cache_unlink(ap->a_nch);
1785 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1789 hammer2_vop_nrename(struct vop_nrename_args *ap)
1791 struct namecache *fncp;
1792 struct namecache *tncp;
1793 hammer2_inode_t *fdip;
1794 hammer2_inode_t *tdip;
1795 hammer2_inode_t *ip;
1796 hammer2_chain_t *chain;
1797 hammer2_trans_t trans;
1798 const uint8_t *fname;
1800 const uint8_t *tname;
1805 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1807 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1810 fdip = VTOI(ap->a_fdvp); /* source directory */
1811 tdip = VTOI(ap->a_tdvp); /* target directory */
1813 if (fdip->pmp->ronly)
1816 fncp = ap->a_fnch->ncp; /* entry name in source */
1817 fname = fncp->nc_name;
1818 fname_len = fncp->nc_nlen;
1820 tncp = ap->a_tnch->ncp; /* entry name in target */
1821 tname = tncp->nc_name;
1822 tname_len = tncp->nc_nlen;
1824 hammer2_chain_memory_wait(tdip->pmp);
1825 hammer2_trans_init(&trans, tdip->pmp, 0);
1828 * ip is the inode being renamed. If this is a hardlink then
1829 * ip represents the actual file and not the hardlink marker.
1831 ip = VTOI(fncp->nc_vp);
1835 * Keep a tight grip on the inode so the temporary unlinking from
1836 * the source location prior to linking to the target location
1837 * does not cause the chain to be destroyed.
1839 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
1840 * unlinking elements from their directories. Locking
1841 * the nlinks field does not lock the whole inode.
1843 hammer2_inode_ref(ip);
1846 * Remove target if it exists
1848 error = hammer2_unlink_file(&trans, tdip, tname, tname_len, -1, NULL);
1849 if (error && error != ENOENT)
1851 cache_setunresolved(ap->a_tnch);
1854 * When renaming a hardlinked file we may have to re-consolidate
1855 * the location of the hardlink target. Since the element is simply
1856 * being moved, nlinks is not modified in this case.
1858 * If ip represents a regular file the consolidation code essentially
1859 * does nothing other than return the same locked chain that was
1862 * The returned chain will be locked.
1864 * WARNING! We do not currently have a local copy of ipdata but
1865 * we do use one later remember that it must be reloaded
1866 * on any modification to the inode, including connects.
1868 chain = hammer2_inode_lock_ex(ip);
1869 error = hammer2_hardlink_consolidate(&trans, ip, &chain, tdip, 0);
1874 * Disconnect (fdip, fname) from the source directory. This will
1875 * disconnect (ip) if it represents a direct file. If (ip) represents
1876 * a hardlink the HARDLINK pointer object will be removed but the
1877 * hardlink will stay intact.
1879 * The target chain may be marked DELETED but will not be destroyed
1880 * since we retain our hold on ip and chain.
1882 error = hammer2_unlink_file(&trans, fdip, fname, fname_len, -1, &hlink);
1883 KKASSERT(error != EAGAIN);
1888 * Reconnect ip to target directory using chain. Chains cannot
1889 * actually be moved, so this will duplicate the chain in the new
1890 * spot and assign it to the ip, replacing the old chain.
1892 * WARNING: Because recursive locks are allowed and we unlinked the
1893 * file that we have a chain-in-hand for just above, the
1894 * chain might have been delete-duplicated. We must refactor
1897 * WARNING: Chain locks can lock buffer cache buffers, to avoid
1898 * deadlocks we want to unlock before issuing a cache_*()
1899 * op (that might have to lock a vnode).
1901 hammer2_chain_refactor(&chain);
1902 error = hammer2_inode_connect(&trans, hlink,
1906 KKASSERT(chain != NULL);
1907 hammer2_inode_repoint(ip, (hlink ? ip->pip : tdip), chain);
1908 cache_rename(ap->a_fnch, ap->a_tnch);
1911 hammer2_inode_unlock_ex(ip, chain);
1912 hammer2_inode_drop(ip);
1913 hammer2_trans_done(&trans);
1921 * WARNING: The strategy code cannot safely use hammer2 transactions
1922 * as this can deadlock against vfs_sync's vfsync() call
1923 * if multiple flushes are queued.
1925 static int hammer2_strategy_read(struct vop_strategy_args *ap);
1926 static int hammer2_strategy_write(struct vop_strategy_args *ap);
1927 static void hammer2_strategy_read_callback(hammer2_chain_t *chain,
1928 struct buf *dbp, char *data, void *arg);
1932 hammer2_vop_strategy(struct vop_strategy_args *ap)
1943 error = hammer2_strategy_read(ap);
1944 ++hammer2_iod_file_read;
1947 error = hammer2_strategy_write(ap);
1948 ++hammer2_iod_file_write;
1951 bp->b_error = error = EINVAL;
1952 bp->b_flags |= B_ERROR;
1962 hammer2_strategy_read(struct vop_strategy_args *ap)
1967 hammer2_inode_t *ip;
1968 hammer2_chain_t *parent;
1969 hammer2_chain_t *chain;
1970 hammer2_key_t key_dummy;
1971 hammer2_key_t lbase;
1973 int cache_index = -1;
1977 ip = VTOI(ap->a_vp);
1978 nbio = push_bio(bio);
1980 lbase = bio->bio_offset;
1982 KKASSERT(((int)lbase & HAMMER2_PBUFMASK) == 0);
1984 parent = hammer2_inode_lock_sh(ip);
1985 chain = hammer2_chain_lookup(&parent, &key_dummy,
1988 HAMMER2_LOOKUP_NODATA |
1989 HAMMER2_LOOKUP_SHARED);
1991 if (chain == NULL) {
1997 bzero(bp->b_data, bp->b_bcount);
1999 } else if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
2001 * Data is embedded in the inode (copy from inode).
2003 hammer2_chain_load_async(chain, hammer2_strategy_read_callback,
2005 } else if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2007 * Data is on-media, issue device I/O and copy.
2009 * XXX direct-IO shortcut could go here XXX.
2011 if (HAMMER2_DEC_COMP(chain->bref.methods) == HAMMER2_COMP_LZ4) {
2013 * Block compression is determined by bref.methods value.
2015 hammer2_blockref_t *bref;
2016 hammer2_off_t pbase;
2017 hammer2_off_t pmask;
2020 bref = &chain->bref;
2021 psize = hammer2_devblksize(chain->bytes);
2022 pmask = (hammer2_off_t)psize - 1;
2023 pbase = bref->data_off & ~pmask;
2024 loff = (int)((bref->data_off &
2025 ~HAMMER2_OFF_MASK_RADIX) - pbase);
2026 nbio->bio_caller_info3.value = loff;
2027 breadcb(chain->hmp->devvp, pbase, psize,
2028 hammer2_decompress_LZ4_callback, nbio);
2029 /* XXX async read dev blk not protected by chain lk */
2030 hammer2_chain_unlock(chain);
2031 } else if (HAMMER2_DEC_COMP(chain->bref.methods) == HAMMER2_COMP_ZLIB) {
2032 hammer2_blockref_t *bref;
2033 hammer2_off_t pbase;
2034 hammer2_off_t pmask;
2037 bref = &chain->bref;
2038 psize = hammer2_devblksize(chain->bytes);
2039 pmask = (hammer2_off_t)psize - 1;
2040 pbase = bref->data_off & ~pmask;
2041 loff = (int)((bref->data_off &
2042 ~HAMMER2_OFF_MASK_RADIX) - pbase);
2043 nbio->bio_caller_info3.value = loff;
2044 breadcb(chain->hmp->devvp, pbase, psize,
2045 hammer2_decompress_ZLIB_callback, nbio);
2046 /* XXX async read dev blk not protected by chain lk */
2047 hammer2_chain_unlock(chain);
2050 hammer2_chain_load_async(chain,
2051 hammer2_strategy_read_callback,
2055 panic("READ PATH: hammer2_strategy_read: unknown bref type");
2058 hammer2_inode_unlock_sh(ip, parent);
2063 * Read callback for block that is not compressed.
2067 hammer2_strategy_read_callback(hammer2_chain_t *chain, struct buf *dbp,
2068 char *data, void *arg)
2070 struct bio *nbio = arg;
2071 struct buf *bp = nbio->bio_buf;
2073 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
2075 * Data is embedded in the inode (copy from inode).
2077 bcopy(((hammer2_inode_data_t *)data)->u.data,
2078 bp->b_data, HAMMER2_EMBEDDED_BYTES);
2079 bzero(bp->b_data + HAMMER2_EMBEDDED_BYTES,
2080 bp->b_bcount - HAMMER2_EMBEDDED_BYTES);
2083 hammer2_chain_unlock(chain);
2085 } else if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2087 * Data is on-media, issue device I/O and copy.
2089 * XXX direct-IO shortcut could go here XXX.
2091 KKASSERT(chain->bytes <= bp->b_bcount);
2092 bcopy(data, bp->b_data, chain->bytes);
2093 if (chain->bytes < bp->b_bcount); {
2094 bzero(bp->b_data + chain->bytes,
2095 bp->b_bcount - chain->bytes);
2097 bp->b_flags |= B_NOTMETA;
2100 hammer2_chain_unlock(chain);
2105 panic("hammer2_strategy_read: unknown bref type");
2106 /*hammer2_chain_unlock(chain);*/
2113 hammer2_strategy_write(struct vop_strategy_args *ap)
2115 hammer2_pfsmount_t *pmp;
2118 hammer2_inode_t *ip;
2122 ip = VTOI(ap->a_vp);
2125 mtx_lock(&pmp->wthread_mtx);
2126 if (TAILQ_EMPTY(&pmp->wthread_bioq.queue)) {
2127 bioq_insert_tail(&pmp->wthread_bioq, ap->a_bio);
2128 wakeup(&pmp->wthread_bioq);
2130 bioq_insert_tail(&pmp->wthread_bioq, ap->a_bio);
2132 mtx_unlock(&pmp->wthread_mtx);
2138 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2142 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2144 hammer2_inode_t *ip;
2147 ip = VTOI(ap->a_vp);
2149 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2150 ap->a_fflag, ap->a_cred);
2156 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2159 hammer2_pfsmount_t *pmp;
2163 case (MOUNTCTL_SET_EXPORT):
2164 mp = ap->a_head.a_ops->head.vv_mount;
2167 if (ap->a_ctllen != sizeof(struct export_args))
2170 rc = vfs_export(mp, &pmp->export,
2171 (const struct export_args *)ap->a_ctl);
2174 rc = vop_stdmountctl(ap);
2183 static void filt_hammer2detach(struct knote *kn);
2184 static int filt_hammer2read(struct knote *kn, long hint);
2185 static int filt_hammer2write(struct knote *kn, long hint);
2186 static int filt_hammer2vnode(struct knote *kn, long hint);
2188 static struct filterops hammer2read_filtops =
2189 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2190 NULL, filt_hammer2detach, filt_hammer2read };
2191 static struct filterops hammer2write_filtops =
2192 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2193 NULL, filt_hammer2detach, filt_hammer2write };
2194 static struct filterops hammer2vnode_filtops =
2195 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2196 NULL, filt_hammer2detach, filt_hammer2vnode };
2200 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
2202 struct vnode *vp = ap->a_vp;
2203 struct knote *kn = ap->a_kn;
2205 switch (kn->kn_filter) {
2207 kn->kn_fop = &hammer2read_filtops;
2210 kn->kn_fop = &hammer2write_filtops;
2213 kn->kn_fop = &hammer2vnode_filtops;
2216 return (EOPNOTSUPP);
2219 kn->kn_hook = (caddr_t)vp;
2221 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2227 filt_hammer2detach(struct knote *kn)
2229 struct vnode *vp = (void *)kn->kn_hook;
2231 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2235 filt_hammer2read(struct knote *kn, long hint)
2237 struct vnode *vp = (void *)kn->kn_hook;
2238 hammer2_inode_t *ip = VTOI(vp);
2241 if (hint == NOTE_REVOKE) {
2242 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2245 off = ip->size - kn->kn_fp->f_offset;
2246 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2247 if (kn->kn_sfflags & NOTE_OLDAPI)
2249 return (kn->kn_data != 0);
2254 filt_hammer2write(struct knote *kn, long hint)
2256 if (hint == NOTE_REVOKE)
2257 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2263 filt_hammer2vnode(struct knote *kn, long hint)
2265 if (kn->kn_sfflags & hint)
2266 kn->kn_fflags |= hint;
2267 if (hint == NOTE_REVOKE) {
2268 kn->kn_flags |= (EV_EOF | EV_NODATA);
2271 return (kn->kn_fflags != 0);
2279 hammer2_vop_markatime(struct vop_markatime_args *ap)
2281 hammer2_inode_t *ip;
2294 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2298 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2300 error = hammer2_vop_kqfilter(ap);
2307 struct vop_ops hammer2_vnode_vops = {
2308 .vop_default = vop_defaultop,
2309 .vop_fsync = hammer2_vop_fsync,
2310 .vop_getpages = vop_stdgetpages,
2311 .vop_putpages = vop_stdputpages,
2312 .vop_access = hammer2_vop_access,
2313 .vop_advlock = hammer2_vop_advlock,
2314 .vop_close = hammer2_vop_close,
2315 .vop_nlink = hammer2_vop_nlink,
2316 .vop_ncreate = hammer2_vop_ncreate,
2317 .vop_nsymlink = hammer2_vop_nsymlink,
2318 .vop_nremove = hammer2_vop_nremove,
2319 .vop_nrmdir = hammer2_vop_nrmdir,
2320 .vop_nrename = hammer2_vop_nrename,
2321 .vop_getattr = hammer2_vop_getattr,
2322 .vop_setattr = hammer2_vop_setattr,
2323 .vop_readdir = hammer2_vop_readdir,
2324 .vop_readlink = hammer2_vop_readlink,
2325 .vop_getpages = vop_stdgetpages,
2326 .vop_putpages = vop_stdputpages,
2327 .vop_read = hammer2_vop_read,
2328 .vop_write = hammer2_vop_write,
2329 .vop_open = hammer2_vop_open,
2330 .vop_inactive = hammer2_vop_inactive,
2331 .vop_reclaim = hammer2_vop_reclaim,
2332 .vop_nresolve = hammer2_vop_nresolve,
2333 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2334 .vop_nmkdir = hammer2_vop_nmkdir,
2335 .vop_nmknod = hammer2_vop_nmknod,
2336 .vop_ioctl = hammer2_vop_ioctl,
2337 .vop_mountctl = hammer2_vop_mountctl,
2338 .vop_bmap = hammer2_vop_bmap,
2339 .vop_strategy = hammer2_vop_strategy,
2340 .vop_kqfilter = hammer2_vop_kqfilter
2343 struct vop_ops hammer2_spec_vops = {
2344 .vop_default = vop_defaultop,
2345 .vop_fsync = hammer2_vop_fsync,
2346 .vop_read = vop_stdnoread,
2347 .vop_write = vop_stdnowrite,
2348 .vop_access = hammer2_vop_access,
2349 .vop_close = hammer2_vop_close,
2350 .vop_markatime = hammer2_vop_markatime,
2351 .vop_getattr = hammer2_vop_getattr,
2352 .vop_inactive = hammer2_vop_inactive,
2353 .vop_reclaim = hammer2_vop_reclaim,
2354 .vop_setattr = hammer2_vop_setattr
2357 struct vop_ops hammer2_fifo_vops = {
2358 .vop_default = fifo_vnoperate,
2359 .vop_fsync = hammer2_vop_fsync,
2361 .vop_read = hammer2_vop_fiforead,
2362 .vop_write = hammer2_vop_fifowrite,
2364 .vop_access = hammer2_vop_access,
2366 .vop_close = hammer2_vop_fifoclose,
2368 .vop_markatime = hammer2_vop_markatime,
2369 .vop_getattr = hammer2_vop_getattr,
2370 .vop_inactive = hammer2_vop_inactive,
2371 .vop_reclaim = hammer2_vop_reclaim,
2372 .vop_setattr = hammer2_vop_setattr,
2373 .vop_kqfilter = hammer2_vop_fifokqfilter