2 * Copyright (c) 2011-2013 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7 * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Kernel Filesystem interface
39 * NOTE! local ipdata pointers must be reloaded on any modifying operation
40 * to the inode as its underlying chain may have changed.
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/fcntl.h>
49 #include <sys/namei.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/mountctl.h>
53 #include <sys/dirent.h>
55 #include <sys/objcache.h>
56 #include <sys/event.h>
58 #include <vfs/fifofs/fifo.h>
61 #include "hammer2_lz4.h"
63 #include "zlib/hammer2_zlib.h"
65 #define ZFOFFSET (-2LL)
67 static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio,
69 static int hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
70 int ioflag, int seqcount);
71 static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize);
72 static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize);
73 static void hammer2_decompress_LZ4_callback(hammer2_io_t *dio,
74 hammer2_chain_t *arg_c,
75 void *arg_p, off_t arg_o);
76 static void hammer2_decompress_ZLIB_callback(hammer2_io_t *dio,
77 hammer2_chain_t *arg_c,
78 void *arg_p, off_t arg_o);
80 struct objcache *cache_buffer_read;
81 struct objcache *cache_buffer_write;
84 * Callback used in read path in case that a block is compressed with LZ4.
88 hammer2_decompress_LZ4_callback(hammer2_io_t *dio, hammer2_chain_t *arg_c,
89 void *arg_p, off_t arg_o)
92 struct bio *obio = arg_p;
94 int bytes = 1 << (int)(arg_o & HAMMER2_OFF_MASK_RADIX);
97 * If BIO_DONE is already set the device buffer was already
98 * fully valid (B_CACHE). If it is not set then I/O was issued
99 * and we have to run I/O completion as the last bio.
101 * Nobody is waiting for our device I/O to complete, we are
102 * responsible for bqrelse()ing it which means we also have to do
103 * the equivalent of biowait() and clear BIO_DONE (which breadcb()
106 * Any preexisting device buffer should match the requested size,
107 * but due to bigblock recycling and other factors there is some
108 * fragility there, so we assert that the device buffer covers
113 if (dio->bp->b_flags & B_ERROR) {
114 obp->b_flags |= B_ERROR;
115 obp->b_error = dio->bp->b_error;
117 } else if (obio->bio_caller_info2.index &&
118 obio->bio_caller_info1.uvalue32 !=
119 crc32(bp->b_data, bp->b_bufsize)) {
120 obp->b_flags |= B_ERROR;
124 char *compressed_buffer;
125 int *compressed_size;
128 KKASSERT(obp->b_bufsize <= HAMMER2_PBUFSIZE);
129 bdata = hammer2_io_data(dio, arg_o);
130 compressed_size = (int *)bdata;
131 compressed_buffer = objcache_get(cache_buffer_read, M_INTWAIT);
132 KKASSERT((unsigned int)*compressed_size <= HAMMER2_PBUFSIZE);
133 result = LZ4_decompress_safe(&bdata[sizeof(int)],
138 kprintf("READ PATH: Error during decompression."
139 "bio %016jx/%d log %016jx/%d\n",
140 (intmax_t)dio->pbase, dio->psize,
141 (intmax_t)arg_o, bytes);
142 /* make sure it isn't random garbage */
143 bzero(compressed_buffer, obp->b_bufsize);
145 KKASSERT(result <= obp->b_bufsize);
146 bcopy(compressed_buffer, obp->b_data, obp->b_bufsize);
147 if (result < obp->b_bufsize)
148 bzero(obp->b_data + result, obp->b_bufsize - result);
149 objcache_put(cache_buffer_read, compressed_buffer);
151 obp->b_flags |= B_AGE;
157 * Callback used in read path in case that a block is compressed with ZLIB.
158 * It is almost identical to LZ4 callback, so in theory they can be unified,
159 * but we didn't want to make changes in bio structure for that.
163 hammer2_decompress_ZLIB_callback(hammer2_io_t *dio, hammer2_chain_t *arg_c,
164 void *arg_p, off_t arg_o)
167 struct bio *obio = arg_p;
169 int bytes = 1 << (int)(arg_o & HAMMER2_OFF_MASK_RADIX);
172 * If BIO_DONE is already set the device buffer was already
173 * fully valid (B_CACHE). If it is not set then I/O was issued
174 * and we have to run I/O completion as the last bio.
176 * Nobody is waiting for our device I/O to complete, we are
177 * responsible for bqrelse()ing it which means we also have to do
178 * the equivalent of biowait() and clear BIO_DONE (which breadcb()
181 * Any preexisting device buffer should match the requested size,
182 * but due to bigblock recycling and other factors there is some
183 * fragility there, so we assert that the device buffer covers
188 if (dio->bp->b_flags & B_ERROR) {
189 obp->b_flags |= B_ERROR;
190 obp->b_error = dio->bp->b_error;
192 } else if (obio->bio_caller_info2.index &&
193 obio->bio_caller_info1.uvalue32 !=
194 crc32(bp->b_data, bp->b_bufsize)) {
195 obp->b_flags |= B_ERROR;
199 char *compressed_buffer;
200 z_stream strm_decompress;
204 KKASSERT(obp->b_bufsize <= HAMMER2_PBUFSIZE);
205 strm_decompress.avail_in = 0;
206 strm_decompress.next_in = Z_NULL;
208 ret = inflateInit(&strm_decompress);
211 kprintf("HAMMER2 ZLIB: Fatal error in inflateInit.\n");
213 bdata = hammer2_io_data(dio, arg_o);
214 compressed_buffer = objcache_get(cache_buffer_read, M_INTWAIT);
215 strm_decompress.next_in = bdata;
217 /* XXX supply proper size, subset of device bp */
218 strm_decompress.avail_in = bytes;
219 strm_decompress.next_out = compressed_buffer;
220 strm_decompress.avail_out = obp->b_bufsize;
222 ret = inflate(&strm_decompress, Z_FINISH);
223 if (ret != Z_STREAM_END) {
224 kprintf("HAMMER2 ZLIB: Fatar error during decompression.\n");
225 bzero(compressed_buffer, obp->b_bufsize);
227 bcopy(compressed_buffer, obp->b_data, obp->b_bufsize);
228 result = obp->b_bufsize - strm_decompress.avail_out;
229 if (result < obp->b_bufsize)
230 bzero(obp->b_data + result, strm_decompress.avail_out);
231 objcache_put(cache_buffer_read, compressed_buffer);
233 obp->b_flags |= B_AGE;
234 ret = inflateEnd(&strm_decompress);
241 hammer2_knote(struct vnode *vp, int flags)
244 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
248 * Last reference to a vnode is going away but it is still cached.
252 hammer2_vop_inactive(struct vop_inactive_args *ap)
255 hammer2_chain_t *parent;
270 * Detect updates to the embedded data which may be synchronized by
271 * the strategy code. Simply mark the inode modified so it gets
272 * picked up by our normal flush.
274 parent = hammer2_inode_lock_ex(ip);
278 * Check for deleted inodes and recycle immediately.
280 if (parent->flags & HAMMER2_CHAIN_DELETED) {
281 hammer2_inode_unlock_ex(ip, parent);
284 hammer2_inode_unlock_ex(ip, parent);
290 * Reclaim a vnode so that it can be reused; after the inode is
291 * disassociated, the filesystem must manage it alone.
295 hammer2_vop_reclaim(struct vop_reclaim_args *ap)
297 hammer2_chain_t *chain;
299 hammer2_trans_t trans;
308 * Set update_hi so we can detect and propagate the DESTROYED
309 * bit in the flush code.
311 * ip->chain might be stale, correct it before checking as older
312 * versions of the chain are likely marked deleted even if the
313 * file hasn't been. XXX ip->chain should never be stale on
316 chain = hammer2_inode_lock_ex(ip);
318 if (chain->next_parent)
319 kprintf("RECLAIM DUPLINKED IP: %p ip->ch=%p ch=%p np=%p\n",
320 ip, ip->chain, chain, chain->next_parent);
324 * The final close of a deleted file or directory marks it for
325 * destruction. The DESTROYED flag allows the flusher to shortcut
326 * any modified blocks still unflushed (that is, just ignore them).
328 * HAMMER2 usually does not try to optimize the freemap by returning
329 * deleted blocks to it as it does not usually know how many snapshots
330 * might be referencing portions of the file/dir. XXX TODO.
332 * XXX TODO - However, any modified file as-of when a snapshot is made
333 * cannot use this optimization as some of the modifications
334 * may wind up being part of the snapshot.
338 if (chain->flags & HAMMER2_CHAIN_DELETED) {
339 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROYED);
340 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_BUFCACHE);
341 hammer2_chain_setsubmod(&trans, chain);
342 spin_lock(&chain->core->cst.spin);
343 if (chain->core->update_hi < trans.sync_tid)
344 chain->core->update_hi = trans.sync_tid; /* needed? */
345 spin_unlock(&chain->core->cst.spin);
346 hammer2_trans_done(&trans);
350 * NOTE! We do not attempt to flush chains here, flushing is
351 * really fragile and could also deadlock.
354 hammer2_inode_unlock_ex(ip, chain); /* unlock */
355 hammer2_inode_drop(ip); /* vp ref */
356 /* chain no longer referenced */
357 /* chain = NULL; not needed */
360 * XXX handle background sync when ip dirty, kernel will no longer
361 * notify us regarding this inode because there is no longer a
362 * vnode attached to it.
370 hammer2_vop_fsync(struct vop_fsync_args *ap)
373 hammer2_trans_t trans;
374 hammer2_chain_t *chain;
381 * WARNING: Cannot use TRANS_ISFLUSH for partial syncs.
384 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_ISFLUSH);
385 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
386 hammer2_trans_clear_invfsync(&trans);
388 hammer2_trans_init(&trans, ip->pmp, 0);
389 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
392 * Calling chain_flush here creates a lot of duplicative
393 * COW operations due to non-optimal vnode ordering.
395 * Only do it for an actual fsync() syscall. The other forms
396 * which call this function will eventually call chain_flush
397 * on the volume root as a catch-all, which is far more optimal.
399 chain = hammer2_inode_lock_ex(ip);
400 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);
407 * XXX creates discontinuity w/modify_tid
409 if (ap->a_flags & VOP_FSYNC_SYSCALL) {
410 hammer2_chain_flush(&trans, &chain);
413 hammer2_inode_unlock_ex(ip, chain);
414 hammer2_trans_done(&trans);
421 hammer2_vop_access(struct vop_access_args *ap)
423 hammer2_inode_t *ip = VTOI(ap->a_vp);
424 hammer2_inode_data_t *ipdata;
425 hammer2_chain_t *chain;
430 chain = hammer2_inode_lock_sh(ip);
431 ipdata = &chain->data->ipdata;
432 uid = hammer2_to_unix_xid(&ipdata->uid);
433 gid = hammer2_to_unix_xid(&ipdata->gid);
434 error = vop_helper_access(ap, uid, gid, ipdata->mode, ipdata->uflags);
435 hammer2_inode_unlock_sh(ip, chain);
442 hammer2_vop_getattr(struct vop_getattr_args *ap)
444 hammer2_inode_data_t *ipdata;
445 hammer2_chain_t *chain;
446 hammer2_pfsmount_t *pmp;
457 chain = hammer2_inode_lock_sh(ip);
458 ipdata = &chain->data->ipdata;
460 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
461 vap->va_fileid = ipdata->inum;
462 vap->va_mode = ipdata->mode;
463 vap->va_nlink = ipdata->nlinks;
464 vap->va_uid = hammer2_to_unix_xid(&ipdata->uid);
465 vap->va_gid = hammer2_to_unix_xid(&ipdata->gid);
468 vap->va_size = ip->size; /* protected by shared lock */
469 vap->va_blocksize = HAMMER2_PBUFSIZE;
470 vap->va_flags = ipdata->uflags;
471 hammer2_time_to_timespec(ipdata->ctime, &vap->va_ctime);
472 hammer2_time_to_timespec(ipdata->mtime, &vap->va_mtime);
473 hammer2_time_to_timespec(ipdata->mtime, &vap->va_atime);
475 vap->va_bytes = vap->va_size; /* XXX */
476 vap->va_type = hammer2_get_vtype(chain);
478 vap->va_uid_uuid = ipdata->uid;
479 vap->va_gid_uuid = ipdata->gid;
480 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
483 hammer2_inode_unlock_sh(ip, chain);
490 hammer2_vop_setattr(struct vop_setattr_args *ap)
492 hammer2_inode_data_t *ipdata;
494 hammer2_chain_t *chain;
495 hammer2_trans_t trans;
505 hammer2_update_time(&ctime);
512 hammer2_chain_memory_wait(ip->pmp);
513 hammer2_trans_init(&trans, ip->pmp, 0);
514 chain = hammer2_inode_lock_ex(ip);
515 ipdata = &chain->data->ipdata;
518 if (vap->va_flags != VNOVAL) {
521 flags = ipdata->uflags;
522 error = vop_helper_setattr_flags(&flags, vap->va_flags,
523 hammer2_to_unix_xid(&ipdata->uid),
526 if (ipdata->uflags != flags) {
527 ipdata = hammer2_chain_modify_ip(&trans, ip,
529 ipdata->uflags = flags;
530 ipdata->ctime = ctime;
531 kflags |= NOTE_ATTRIB;
533 if (ipdata->uflags & (IMMUTABLE | APPEND)) {
540 if (ipdata->uflags & (IMMUTABLE | APPEND)) {
544 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
545 mode_t cur_mode = ipdata->mode;
546 uid_t cur_uid = hammer2_to_unix_xid(&ipdata->uid);
547 gid_t cur_gid = hammer2_to_unix_xid(&ipdata->gid);
551 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
553 &cur_uid, &cur_gid, &cur_mode);
555 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
556 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
557 if (bcmp(&uuid_uid, &ipdata->uid, sizeof(uuid_uid)) ||
558 bcmp(&uuid_gid, &ipdata->gid, sizeof(uuid_gid)) ||
559 ipdata->mode != cur_mode
561 ipdata = hammer2_chain_modify_ip(&trans, ip,
563 ipdata->uid = uuid_uid;
564 ipdata->gid = uuid_gid;
565 ipdata->mode = cur_mode;
566 ipdata->ctime = ctime;
568 kflags |= NOTE_ATTRIB;
575 if (vap->va_size != VNOVAL && ip->size != vap->va_size) {
578 if (vap->va_size == ip->size)
580 hammer2_inode_unlock_ex(ip, chain);
581 if (vap->va_size < ip->size) {
582 hammer2_truncate_file(ip, vap->va_size);
584 hammer2_extend_file(ip, vap->va_size);
586 chain = hammer2_inode_lock_ex(ip);
587 ipdata = &chain->data->ipdata; /* RELOAD */
596 /* atime not supported */
597 if (vap->va_atime.tv_sec != VNOVAL) {
598 ipdata = hammer2_chain_modify_ip(&trans, ip, &chain, 0);
599 ipdata->atime = hammer2_timespec_to_time(&vap->va_atime);
600 kflags |= NOTE_ATTRIB;
603 if (vap->va_mtime.tv_sec != VNOVAL) {
604 ipdata = hammer2_chain_modify_ip(&trans, ip, &chain, 0);
605 ipdata->mtime = hammer2_timespec_to_time(&vap->va_mtime);
606 kflags |= NOTE_ATTRIB;
609 if (vap->va_mode != (mode_t)VNOVAL) {
610 mode_t cur_mode = ipdata->mode;
611 uid_t cur_uid = hammer2_to_unix_xid(&ipdata->uid);
612 gid_t cur_gid = hammer2_to_unix_xid(&ipdata->gid);
614 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
615 cur_uid, cur_gid, &cur_mode);
616 if (error == 0 && ipdata->mode != cur_mode) {
617 ipdata = hammer2_chain_modify_ip(&trans, ip, &chain, 0);
618 ipdata->mode = cur_mode;
619 ipdata->ctime = ctime;
620 kflags |= NOTE_ATTRIB;
625 * If a truncation occurred we must call inode_fsync() now in order
626 * to trim the related data chains, otherwise a later expansion can
629 hammer2_inode_fsync(&trans, ip, &chain);
632 * Cleanup. If domtime is set an additional inode modification
633 * must be flagged. All other modifications will have already
634 * set INODE_MODIFIED and called vsetisdirty().
638 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED |
639 HAMMER2_INODE_MTIME);
642 hammer2_inode_unlock_ex(ip, chain);
643 hammer2_trans_done(&trans);
644 hammer2_knote(ip->vp, kflags);
651 hammer2_vop_readdir(struct vop_readdir_args *ap)
653 hammer2_inode_data_t *ipdata;
655 hammer2_inode_t *xip;
656 hammer2_chain_t *parent;
657 hammer2_chain_t *chain;
658 hammer2_chain_t *xchain;
660 hammer2_key_t key_next;
666 int cache_index = -1;
674 saveoff = uio->uio_offset;
677 * Setup cookies directory entry cookies if requested
679 if (ap->a_ncookies) {
680 ncookies = uio->uio_resid / 16 + 1;
683 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
690 parent = hammer2_inode_lock_sh(ip);
691 ipdata = &parent->data->ipdata;
694 * Handle artificial entries. To ensure that only positive 64 bit
695 * quantities are returned to userland we always strip off bit 63.
696 * The hash code is designed such that codes 0x0000-0x7FFF are not
697 * used, allowing us to use these codes for articial entries.
699 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
700 * allow '..' to cross the mount point into (e.g.) the super-root.
703 chain = (void *)(intptr_t)-1; /* non-NULL for early goto done case */
706 inum = ipdata->inum & HAMMER2_DIRHASH_USERMSK;
707 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
711 cookies[cookie_index] = saveoff;
714 if (cookie_index == ncookies)
720 * Be careful with lockorder when accessing ".."
722 * (ip is the current dir. xip is the parent dir).
724 inum = ipdata->inum & HAMMER2_DIRHASH_USERMSK;
725 while (ip->pip != NULL && ip != ip->pmp->iroot) {
727 hammer2_inode_ref(xip);
728 hammer2_inode_unlock_sh(ip, parent);
729 xchain = hammer2_inode_lock_sh(xip);
730 parent = hammer2_inode_lock_sh(ip);
731 hammer2_inode_drop(xip);
732 if (xip == ip->pip) {
733 inum = xchain->data->ipdata.inum &
734 HAMMER2_DIRHASH_USERMSK;
735 hammer2_inode_unlock_sh(xip, xchain);
738 hammer2_inode_unlock_sh(xip, xchain);
740 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
744 cookies[cookie_index] = saveoff;
747 if (cookie_index == ncookies)
751 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
754 * parent is the inode chain, already locked for us. Don't
755 * double lock shared locks as this will screw up upgrades.
760 chain = hammer2_chain_lookup(&parent, &key_next, lkey, lkey,
761 &cache_index, HAMMER2_LOOKUP_SHARED);
763 chain = hammer2_chain_lookup(&parent, &key_next,
764 lkey, (hammer2_key_t)-1,
766 HAMMER2_LOOKUP_SHARED);
769 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
770 dtype = hammer2_get_dtype(chain);
771 saveoff = chain->bref.key & HAMMER2_DIRHASH_USERMSK;
772 r = vop_write_dirent(&error, uio,
773 chain->data->ipdata.inum &
774 HAMMER2_DIRHASH_USERMSK,
776 chain->data->ipdata.name_len,
777 chain->data->ipdata.filename);
781 cookies[cookie_index] = saveoff;
784 /* XXX chain error */
785 kprintf("bad chain type readdir %d\n",
790 * Keys may not be returned in order so once we have a
791 * placemarker (chain) the scan must allow the full range
792 * or some entries will be missed.
794 chain = hammer2_chain_next(&parent, chain, &key_next,
795 key_next, (hammer2_key_t)-1,
796 &cache_index, HAMMER2_LOOKUP_SHARED);
798 saveoff = (chain->bref.key &
799 HAMMER2_DIRHASH_USERMSK) + 1;
801 saveoff = (hammer2_key_t)-1;
803 if (cookie_index == ncookies)
807 hammer2_chain_unlock(chain);
809 hammer2_inode_unlock_sh(ip, parent);
811 *ap->a_eofflag = (chain == NULL);
812 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
813 if (error && cookie_index == 0) {
815 kfree(cookies, M_TEMP);
817 *ap->a_cookies = NULL;
821 *ap->a_ncookies = cookie_index;
822 *ap->a_cookies = cookies;
829 * hammer2_vop_readlink { vp, uio, cred }
833 hammer2_vop_readlink(struct vop_readlink_args *ap)
840 if (vp->v_type != VLNK)
844 error = hammer2_read_file(ip, ap->a_uio, 0);
850 hammer2_vop_read(struct vop_read_args *ap)
860 * Read operations supported on this vnode?
863 if (vp->v_type != VREG)
873 seqcount = ap->a_ioflag >> 16;
874 bigread = (uio->uio_resid > 100 * 1024 * 1024);
876 error = hammer2_read_file(ip, uio, seqcount);
882 hammer2_vop_write(struct vop_write_args *ap)
885 hammer2_trans_t trans;
894 * Read operations supported on this vnode?
897 if (vp->v_type != VREG)
909 seqcount = ap->a_ioflag >> 16;
910 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
913 * Check resource limit
915 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
916 uio->uio_offset + uio->uio_resid >
917 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
918 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
922 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
925 * The transaction interlocks against flushes initiations
926 * (note: but will run concurrently with the actual flush).
928 hammer2_trans_init(&trans, ip->pmp, 0);
929 error = hammer2_write_file(ip, uio, ap->a_ioflag, seqcount);
930 hammer2_trans_done(&trans);
936 * Perform read operations on a file or symlink given an UNLOCKED
939 * The passed ip is not locked.
943 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
954 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
956 ccms_thread_unlock(&ip->topo_cst);
958 while (uio->uio_resid > 0 && uio->uio_offset < size) {
965 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
968 error = cluster_read(ip->vp, leof, lbase, lblksize,
969 uio->uio_resid, seqcount * BKVASIZE,
974 loff = (int)(uio->uio_offset - lbase);
976 if (n > uio->uio_resid)
978 if (n > size - uio->uio_offset)
979 n = (int)(size - uio->uio_offset);
980 bp->b_flags |= B_AGE;
981 uiomove((char *)bp->b_data + loff, n, uio);
988 * Write to the file represented by the inode via the logical buffer cache.
989 * The inode may represent a regular file or a symlink.
991 * The inode must not be locked.
995 hammer2_write_file(hammer2_inode_t *ip,
996 struct uio *uio, int ioflag, int seqcount)
998 hammer2_key_t old_eof;
999 hammer2_key_t new_eof;
1008 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1009 if (ioflag & IO_APPEND)
1010 uio->uio_offset = ip->size;
1012 ccms_thread_unlock(&ip->topo_cst);
1015 * Extend the file if necessary. If the write fails at some point
1016 * we will truncate it back down to cover as much as we were able
1019 * Doing this now makes it easier to calculate buffer sizes in
1026 if (uio->uio_offset + uio->uio_resid > old_eof) {
1027 new_eof = uio->uio_offset + uio->uio_resid;
1029 hammer2_extend_file(ip, new_eof);
1030 kflags |= NOTE_EXTEND;
1038 while (uio->uio_resid > 0) {
1039 hammer2_key_t lbase;
1047 * Don't allow the buffer build to blow out the buffer
1050 if ((ioflag & IO_RECURSE) == 0)
1051 bwillwrite(HAMMER2_PBUFSIZE);
1054 * This nominally tells us how much we can cluster and
1055 * what the logical buffer size needs to be. Currently
1056 * we don't try to cluster the write and just handle one
1059 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
1061 loff = (int)(uio->uio_offset - lbase);
1063 KKASSERT(lblksize <= 65536);
1066 * Calculate bytes to copy this transfer and whether the
1067 * copy completely covers the buffer or not.
1070 n = lblksize - loff;
1071 if (n > uio->uio_resid) {
1073 if (loff == lbase && uio->uio_offset + n == new_eof)
1085 if (uio->uio_segflg == UIO_NOCOPY) {
1087 * Issuing a write with the same data backing the
1088 * buffer. Instantiate the buffer to collect the
1089 * backing vm pages, then read-in any missing bits.
1091 * This case is used by vop_stdputpages().
1093 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
1094 if ((bp->b_flags & B_CACHE) == 0) {
1096 error = bread(ip->vp, lbase, lblksize, &bp);
1098 } else if (trivial) {
1100 * Even though we are entirely overwriting the buffer
1101 * we may still have to zero it out to avoid a
1102 * mmap/write visibility issue.
1104 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
1105 if ((bp->b_flags & B_CACHE) == 0)
1109 * Partial overwrite, read in any missing bits then
1110 * replace the portion being written.
1112 * (The strategy code will detect zero-fill physical
1113 * blocks for this case).
1115 error = bread(ip->vp, lbase, lblksize, &bp);
1126 * Ok, copy the data in
1128 error = uiomove(bp->b_data + loff, n, uio);
1129 kflags |= NOTE_WRITE;
1137 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
1138 * with IO_SYNC or IO_ASYNC set. These writes
1139 * must be handled as the pageout daemon expects.
1141 if (ioflag & IO_SYNC) {
1143 } else if ((ioflag & IO_DIRECT) && endofblk) {
1145 } else if (ioflag & IO_ASYNC) {
1153 * Cleanup. If we extended the file EOF but failed to write through
1154 * the entire write is a failure and we have to back-up.
1156 if (error && new_eof != old_eof) {
1157 hammer2_truncate_file(ip, old_eof);
1158 } else if (modified) {
1159 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1160 hammer2_update_time(&ip->mtime);
1161 atomic_set_int(&ip->flags, HAMMER2_INODE_MTIME);
1162 ccms_thread_unlock(&ip->topo_cst);
1164 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1165 hammer2_knote(ip->vp, kflags);
1166 vsetisdirty(ip->vp);
1172 * Truncate the size of a file. The inode must not be locked.
1174 * NOTE: Caller handles setting HAMMER2_INODE_MODIFIED
1178 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1180 hammer2_key_t lbase;
1184 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1185 nvtruncbuf(ip->vp, nsize,
1186 nblksize, (int)nsize & (nblksize - 1),
1189 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1191 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1192 ccms_thread_unlock(&ip->topo_cst);
1196 * Extend the size of a file. The inode must not be locked.
1198 * NOTE: Caller handles setting HAMMER2_INODE_MODIFIED
1202 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1204 hammer2_key_t lbase;
1205 hammer2_key_t osize;
1209 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1212 ccms_thread_unlock(&ip->topo_cst);
1215 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1216 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1222 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1227 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1229 hammer2_inode_t *ip;
1230 hammer2_inode_t *dip;
1231 hammer2_chain_t *parent;
1232 hammer2_chain_t *chain;
1233 hammer2_chain_t *ochain;
1234 hammer2_trans_t trans;
1235 hammer2_key_t key_next;
1237 struct namecache *ncp;
1238 const uint8_t *name;
1241 int cache_index = -1;
1244 dip = VTOI(ap->a_dvp);
1245 ncp = ap->a_nch->ncp;
1246 name = ncp->nc_name;
1247 name_len = ncp->nc_nlen;
1248 lhc = hammer2_dirhash(name, name_len);
1251 * Note: In DragonFly the kernel handles '.' and '..'.
1253 parent = hammer2_inode_lock_sh(dip);
1254 chain = hammer2_chain_lookup(&parent, &key_next,
1255 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1256 &cache_index, HAMMER2_LOOKUP_SHARED);
1258 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1259 name_len == chain->data->ipdata.name_len &&
1260 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1263 chain = hammer2_chain_next(&parent, chain, &key_next,
1265 lhc + HAMMER2_DIRHASH_LOMASK,
1266 &cache_index, HAMMER2_LOOKUP_SHARED);
1268 hammer2_inode_unlock_sh(dip, parent);
1271 * If the inode represents a forwarding entry for a hardlink we have
1272 * to locate the actual inode. The original ip is saved for possible
1273 * deconsolidation. (ip) will only be set to non-NULL when we have
1274 * to locate the real file via a hardlink. ip will be referenced but
1275 * not locked in that situation. chain is passed in locked and
1278 * XXX what kind of chain lock?
1281 if (chain && chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
1282 error = hammer2_hardlink_find(dip, &chain, &ochain);
1284 kprintf("hammer2: unable to find hardlink\n");
1286 hammer2_chain_unlock(chain);
1294 * Deconsolidate any hardlink whos nlinks == 1. Ignore errors.
1295 * If an error occurs chain and ip are left alone.
1297 * XXX upgrade shared lock?
1299 if (ochain && chain &&
1300 chain->data->ipdata.nlinks == 1 && !dip->pmp->ronly) {
1301 kprintf("hammer2: need to unconsolidate hardlink for %s\n",
1302 chain->data->ipdata.filename);
1303 /* XXX retain shared lock on dip? (currently not held) */
1304 hammer2_trans_init(&trans, dip->pmp, 0);
1305 hammer2_hardlink_deconsolidate(&trans, dip, &chain, &ochain);
1306 hammer2_trans_done(&trans);
1310 * Acquire the related vnode
1312 * NOTE: For error processing, only ENOENT resolves the namecache
1313 * entry to NULL, otherwise we just return the error and
1314 * leave the namecache unresolved.
1316 * NOTE: multiple hammer2_inode structures can be aliased to the
1317 * same chain element, for example for hardlinks. This
1318 * use case does not 'reattach' inode associations that
1319 * might already exist, but always allocates a new one.
1321 * WARNING: inode structure is locked exclusively via inode_get
1322 * but chain was locked shared. inode_unlock_ex()
1323 * will handle it properly.
1326 ip = hammer2_inode_get(dip->pmp, dip, chain);
1327 vp = hammer2_igetv(ip, &error);
1330 cache_setvp(ap->a_nch, vp);
1331 } else if (error == ENOENT) {
1332 cache_setvp(ap->a_nch, NULL);
1334 hammer2_inode_unlock_ex(ip, chain);
1337 * The vp should not be released until after we've disposed
1338 * of our locks, because it might cause vop_inactive() to
1345 cache_setvp(ap->a_nch, NULL);
1348 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1349 ("resolve error %d/%p chain %p ap %p\n",
1350 error, ap->a_nch->ncp->nc_vp, chain, ap));
1352 hammer2_chain_drop(ochain);
1358 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1360 hammer2_inode_t *dip;
1361 hammer2_inode_t *ip;
1362 hammer2_chain_t *parent;
1365 dip = VTOI(ap->a_dvp);
1367 if ((ip = dip->pip) == NULL) {
1371 parent = hammer2_inode_lock_ex(ip);
1372 *ap->a_vpp = hammer2_igetv(ip, &error);
1373 hammer2_inode_unlock_ex(ip, parent);
1380 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1382 hammer2_inode_t *dip;
1383 hammer2_inode_t *nip;
1384 hammer2_trans_t trans;
1385 hammer2_chain_t *chain;
1386 struct namecache *ncp;
1387 const uint8_t *name;
1391 dip = VTOI(ap->a_dvp);
1392 if (dip->pmp->ronly)
1395 ncp = ap->a_nch->ncp;
1396 name = ncp->nc_name;
1397 name_len = ncp->nc_nlen;
1399 hammer2_chain_memory_wait(dip->pmp);
1400 hammer2_trans_init(&trans, dip->pmp, HAMMER2_TRANS_NEWINODE);
1401 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1402 name, name_len, &chain, &error);
1404 KKASSERT(nip == NULL);
1407 *ap->a_vpp = hammer2_igetv(nip, &error);
1408 hammer2_inode_unlock_ex(nip, chain);
1410 hammer2_trans_done(&trans);
1413 cache_setunresolved(ap->a_nch);
1414 cache_setvp(ap->a_nch, *ap->a_vpp);
1420 * Return the largest contiguous physical disk range for the logical
1421 * request, in bytes.
1423 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
1425 * Basically disabled, the logical buffer write thread has to deal with
1426 * buffers one-at-a-time.
1430 hammer2_vop_bmap(struct vop_bmap_args *ap)
1432 *ap->a_doffsetp = NOOFFSET;
1437 return (EOPNOTSUPP);
1442 hammer2_vop_open(struct vop_open_args *ap)
1444 return vop_stdopen(ap);
1448 * hammer2_vop_advlock { vp, id, op, fl, flags }
1452 hammer2_vop_advlock(struct vop_advlock_args *ap)
1454 hammer2_inode_t *ip = VTOI(ap->a_vp);
1455 hammer2_chain_t *parent;
1458 parent = hammer2_inode_lock_sh(ip);
1459 size = parent->data->ipdata.size;
1460 hammer2_inode_unlock_sh(ip, parent);
1461 return (lf_advlock(ap, &ip->advlock, size));
1467 hammer2_vop_close(struct vop_close_args *ap)
1469 return vop_stdclose(ap);
1473 * hammer2_vop_nlink { nch, dvp, vp, cred }
1475 * Create a hardlink from (vp) to {dvp, nch}.
1479 hammer2_vop_nlink(struct vop_nlink_args *ap)
1481 hammer2_inode_t *dip; /* target directory to create link in */
1482 hammer2_inode_t *ip; /* inode we are hardlinking to */
1483 hammer2_chain_t *chain;
1484 hammer2_trans_t trans;
1485 struct namecache *ncp;
1486 const uint8_t *name;
1490 dip = VTOI(ap->a_dvp);
1491 if (dip->pmp->ronly)
1494 ncp = ap->a_nch->ncp;
1495 name = ncp->nc_name;
1496 name_len = ncp->nc_nlen;
1499 * ip represents the file being hardlinked. The file could be a
1500 * normal file or a hardlink target if it has already been hardlinked.
1501 * If ip is a hardlinked target then ip->pip represents the location
1502 * of the hardlinked target, NOT the location of the hardlink pointer.
1504 * Bump nlinks and potentially also create or move the hardlink
1505 * target in the parent directory common to (ip) and (dip). The
1506 * consolidation code can modify ip->chain and ip->pip. The
1507 * returned chain is locked.
1509 ip = VTOI(ap->a_vp);
1510 hammer2_chain_memory_wait(ip->pmp);
1511 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_NEWINODE);
1513 chain = hammer2_inode_lock_ex(ip);
1514 error = hammer2_hardlink_consolidate(&trans, ip, &chain, dip, 1);
1519 * Create a directory entry connected to the specified chain.
1520 * The hardlink consolidation code has already adjusted ip->pip
1521 * to the common parent directory containing the actual hardlink
1523 * (which may be different from dip where we created our hardlink
1524 * entry. ip->chain always represents the actual hardlink and not
1525 * any of the pointers to the actual hardlink).
1527 error = hammer2_inode_connect(&trans, 1,
1531 cache_setunresolved(ap->a_nch);
1532 cache_setvp(ap->a_nch, ap->a_vp);
1535 hammer2_inode_unlock_ex(ip, chain);
1536 hammer2_trans_done(&trans);
1542 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1544 * The operating system has already ensured that the directory entry
1545 * does not exist and done all appropriate namespace locking.
1549 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1551 hammer2_inode_t *dip;
1552 hammer2_inode_t *nip;
1553 hammer2_trans_t trans;
1554 hammer2_chain_t *nchain;
1555 struct namecache *ncp;
1556 const uint8_t *name;
1560 dip = VTOI(ap->a_dvp);
1561 if (dip->pmp->ronly)
1564 ncp = ap->a_nch->ncp;
1565 name = ncp->nc_name;
1566 name_len = ncp->nc_nlen;
1567 hammer2_chain_memory_wait(dip->pmp);
1568 hammer2_trans_init(&trans, dip->pmp, HAMMER2_TRANS_NEWINODE);
1570 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1571 name, name_len, &nchain, &error);
1573 KKASSERT(nip == NULL);
1576 *ap->a_vpp = hammer2_igetv(nip, &error);
1577 hammer2_inode_unlock_ex(nip, nchain);
1579 hammer2_trans_done(&trans);
1582 cache_setunresolved(ap->a_nch);
1583 cache_setvp(ap->a_nch, *ap->a_vpp);
1593 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1595 hammer2_inode_t *dip;
1596 hammer2_inode_t *nip;
1597 hammer2_trans_t trans;
1598 hammer2_chain_t *nchain;
1599 struct namecache *ncp;
1600 const uint8_t *name;
1604 dip = VTOI(ap->a_dvp);
1605 if (dip->pmp->ronly)
1608 ncp = ap->a_nch->ncp;
1609 name = ncp->nc_name;
1610 name_len = ncp->nc_nlen;
1611 hammer2_chain_memory_wait(dip->pmp);
1612 hammer2_trans_init(&trans, dip->pmp, HAMMER2_TRANS_NEWINODE);
1614 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1615 name, name_len, &nchain, &error);
1617 KKASSERT(nip == NULL);
1620 *ap->a_vpp = hammer2_igetv(nip, &error);
1621 hammer2_inode_unlock_ex(nip, nchain);
1623 hammer2_trans_done(&trans);
1626 cache_setunresolved(ap->a_nch);
1627 cache_setvp(ap->a_nch, *ap->a_vpp);
1633 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1637 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1639 hammer2_inode_t *dip;
1640 hammer2_inode_t *nip;
1641 hammer2_chain_t *nparent;
1642 hammer2_trans_t trans;
1643 struct namecache *ncp;
1644 const uint8_t *name;
1648 dip = VTOI(ap->a_dvp);
1649 if (dip->pmp->ronly)
1652 ncp = ap->a_nch->ncp;
1653 name = ncp->nc_name;
1654 name_len = ncp->nc_nlen;
1655 hammer2_chain_memory_wait(dip->pmp);
1656 hammer2_trans_init(&trans, dip->pmp, HAMMER2_TRANS_NEWINODE);
1658 ap->a_vap->va_type = VLNK; /* enforce type */
1660 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1661 name, name_len, &nparent, &error);
1663 KKASSERT(nip == NULL);
1665 hammer2_trans_done(&trans);
1668 *ap->a_vpp = hammer2_igetv(nip, &error);
1671 * Build the softlink (~like file data) and finalize the namecache.
1677 hammer2_inode_data_t *nipdata;
1679 nipdata = &nip->chain->data->ipdata;
1680 bytes = strlen(ap->a_target);
1682 if (bytes <= HAMMER2_EMBEDDED_BYTES) {
1683 KKASSERT(nipdata->op_flags &
1684 HAMMER2_OPFLAG_DIRECTDATA);
1685 bcopy(ap->a_target, nipdata->u.data, bytes);
1686 nipdata->size = bytes;
1688 hammer2_inode_unlock_ex(nip, nparent);
1690 hammer2_inode_unlock_ex(nip, nparent);
1691 bzero(&auio, sizeof(auio));
1692 bzero(&aiov, sizeof(aiov));
1693 auio.uio_iov = &aiov;
1694 auio.uio_segflg = UIO_SYSSPACE;
1695 auio.uio_rw = UIO_WRITE;
1696 auio.uio_resid = bytes;
1697 auio.uio_iovcnt = 1;
1698 auio.uio_td = curthread;
1699 aiov.iov_base = ap->a_target;
1700 aiov.iov_len = bytes;
1701 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1702 nipdata = &nip->chain->data->ipdata; /* RELOAD */
1703 /* XXX handle error */
1707 hammer2_inode_unlock_ex(nip, nparent);
1709 hammer2_trans_done(&trans);
1712 * Finalize namecache
1715 cache_setunresolved(ap->a_nch);
1716 cache_setvp(ap->a_nch, *ap->a_vpp);
1717 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1723 * hammer2_vop_nremove { nch, dvp, cred }
1727 hammer2_vop_nremove(struct vop_nremove_args *ap)
1729 hammer2_inode_t *dip;
1730 hammer2_trans_t trans;
1731 struct namecache *ncp;
1732 const uint8_t *name;
1736 dip = VTOI(ap->a_dvp);
1737 if (dip->pmp->ronly)
1740 ncp = ap->a_nch->ncp;
1741 name = ncp->nc_name;
1742 name_len = ncp->nc_nlen;
1743 hammer2_chain_memory_wait(dip->pmp);
1744 hammer2_trans_init(&trans, dip->pmp, 0);
1745 error = hammer2_unlink_file(&trans, dip, name, name_len, 0, NULL);
1746 hammer2_trans_done(&trans);
1748 cache_unlink(ap->a_nch);
1754 * hammer2_vop_nrmdir { nch, dvp, cred }
1758 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1760 hammer2_inode_t *dip;
1761 hammer2_trans_t trans;
1762 struct namecache *ncp;
1763 const uint8_t *name;
1767 dip = VTOI(ap->a_dvp);
1768 if (dip->pmp->ronly)
1771 ncp = ap->a_nch->ncp;
1772 name = ncp->nc_name;
1773 name_len = ncp->nc_nlen;
1775 hammer2_chain_memory_wait(dip->pmp);
1776 hammer2_trans_init(&trans, dip->pmp, 0);
1777 error = hammer2_unlink_file(&trans, dip, name, name_len, 1, NULL);
1778 hammer2_trans_done(&trans);
1780 cache_unlink(ap->a_nch);
1786 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1790 hammer2_vop_nrename(struct vop_nrename_args *ap)
1792 struct namecache *fncp;
1793 struct namecache *tncp;
1794 hammer2_inode_t *fdip;
1795 hammer2_inode_t *tdip;
1796 hammer2_inode_t *ip;
1797 hammer2_chain_t *chain;
1798 hammer2_trans_t trans;
1799 const uint8_t *fname;
1801 const uint8_t *tname;
1806 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1808 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1811 fdip = VTOI(ap->a_fdvp); /* source directory */
1812 tdip = VTOI(ap->a_tdvp); /* target directory */
1814 if (fdip->pmp->ronly)
1817 fncp = ap->a_fnch->ncp; /* entry name in source */
1818 fname = fncp->nc_name;
1819 fname_len = fncp->nc_nlen;
1821 tncp = ap->a_tnch->ncp; /* entry name in target */
1822 tname = tncp->nc_name;
1823 tname_len = tncp->nc_nlen;
1825 hammer2_chain_memory_wait(tdip->pmp);
1826 hammer2_trans_init(&trans, tdip->pmp, 0);
1829 * ip is the inode being renamed. If this is a hardlink then
1830 * ip represents the actual file and not the hardlink marker.
1832 ip = VTOI(fncp->nc_vp);
1836 * Keep a tight grip on the inode so the temporary unlinking from
1837 * the source location prior to linking to the target location
1838 * does not cause the chain to be destroyed.
1840 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
1841 * unlinking elements from their directories. Locking
1842 * the nlinks field does not lock the whole inode.
1844 hammer2_inode_ref(ip);
1847 * Remove target if it exists
1849 error = hammer2_unlink_file(&trans, tdip, tname, tname_len, -1, NULL);
1850 if (error && error != ENOENT)
1852 cache_setunresolved(ap->a_tnch);
1855 * When renaming a hardlinked file we may have to re-consolidate
1856 * the location of the hardlink target. Since the element is simply
1857 * being moved, nlinks is not modified in this case.
1859 * If ip represents a regular file the consolidation code essentially
1860 * does nothing other than return the same locked chain that was
1863 * The returned chain will be locked.
1865 * WARNING! We do not currently have a local copy of ipdata but
1866 * we do use one later remember that it must be reloaded
1867 * on any modification to the inode, including connects.
1869 chain = hammer2_inode_lock_ex(ip);
1870 error = hammer2_hardlink_consolidate(&trans, ip, &chain, tdip, 0);
1875 * Disconnect (fdip, fname) from the source directory. This will
1876 * disconnect (ip) if it represents a direct file. If (ip) represents
1877 * a hardlink the HARDLINK pointer object will be removed but the
1878 * hardlink will stay intact.
1880 * The target chain may be marked DELETED but will not be destroyed
1881 * since we retain our hold on ip and chain.
1883 error = hammer2_unlink_file(&trans, fdip, fname, fname_len, -1, &hlink);
1884 KKASSERT(error != EAGAIN);
1889 * Reconnect ip to target directory using chain. Chains cannot
1890 * actually be moved, so this will duplicate the chain in the new
1891 * spot and assign it to the ip, replacing the old chain.
1893 * WARNING: Because recursive locks are allowed and we unlinked the
1894 * file that we have a chain-in-hand for just above, the
1895 * chain might have been delete-duplicated. We must refactor
1898 * WARNING: Chain locks can lock buffer cache buffers, to avoid
1899 * deadlocks we want to unlock before issuing a cache_*()
1900 * op (that might have to lock a vnode).
1902 hammer2_chain_refactor(&chain);
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_io_t *dio,
1929 hammer2_chain_t *chain,
1930 void *arg_p, off_t arg_o);
1934 hammer2_vop_strategy(struct vop_strategy_args *ap)
1945 error = hammer2_strategy_read(ap);
1946 ++hammer2_iod_file_read;
1949 error = hammer2_strategy_write(ap);
1950 ++hammer2_iod_file_write;
1953 bp->b_error = error = EINVAL;
1954 bp->b_flags |= B_ERROR;
1964 hammer2_strategy_read(struct vop_strategy_args *ap)
1969 hammer2_inode_t *ip;
1970 hammer2_chain_t *parent;
1971 hammer2_chain_t *chain;
1972 hammer2_key_t key_dummy;
1973 hammer2_key_t lbase;
1974 int cache_index = -1;
1978 ip = VTOI(ap->a_vp);
1979 nbio = push_bio(bio);
1981 lbase = bio->bio_offset;
1983 KKASSERT(((int)lbase & HAMMER2_PBUFMASK) == 0);
1985 parent = hammer2_inode_lock_sh(ip);
1986 chain = hammer2_chain_lookup(&parent, &key_dummy,
1989 HAMMER2_LOOKUP_NODATA |
1990 HAMMER2_LOOKUP_SHARED);
1992 if (chain == NULL) {
1998 bzero(bp->b_data, bp->b_bcount);
2000 } else if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
2002 * Data is embedded in the inode (copy from inode).
2004 hammer2_chain_load_async(chain,
2005 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
2017 hammer2_blockref_t *bref;
2019 bref = &chain->bref;
2020 hammer2_io_breadcb(chain->hmp, bref->data_off,
2022 hammer2_decompress_LZ4_callback,
2023 NULL, nbio, bref->data_off);
2024 /* XXX async read dev blk not protected by chain lk */
2025 hammer2_chain_unlock(chain);
2026 } else if (HAMMER2_DEC_COMP(chain->bref.methods) ==
2027 HAMMER2_COMP_ZLIB) {
2028 hammer2_blockref_t *bref;
2030 bref = &chain->bref;
2031 hammer2_io_breadcb(chain->hmp, bref->data_off,
2033 hammer2_decompress_ZLIB_callback,
2034 NULL, nbio, bref->data_off);
2035 /* XXX async read dev blk not protected by chain lk */
2036 hammer2_chain_unlock(chain);
2038 hammer2_chain_load_async(chain,
2039 hammer2_strategy_read_callback,
2043 panic("READ PATH: hammer2_strategy_read: unknown bref type");
2046 hammer2_inode_unlock_sh(ip, parent);
2051 * Read callback for block that is not compressed.
2055 hammer2_strategy_read_callback(hammer2_io_t *dio, hammer2_chain_t *chain,
2056 void *arg_p, off_t arg_o __unused)
2058 struct bio *nbio = arg_p;
2059 struct buf *bp = nbio->bio_buf;
2063 data = hammer2_io_data(dio, chain->bref.data_off);
2065 data = (void *)chain->data;
2067 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
2069 * Data is embedded in the inode (copy from inode).
2071 bcopy(((hammer2_inode_data_t *)data)->u.data,
2072 bp->b_data, HAMMER2_EMBEDDED_BYTES);
2073 bzero(bp->b_data + HAMMER2_EMBEDDED_BYTES,
2074 bp->b_bcount - HAMMER2_EMBEDDED_BYTES);
2077 hammer2_chain_unlock(chain);
2079 } else if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2081 * Data is on-media, issue device I/O and copy.
2083 * XXX direct-IO shortcut could go here XXX.
2085 KKASSERT(chain->bytes <= bp->b_bcount);
2086 bcopy(data, bp->b_data, chain->bytes);
2087 if (chain->bytes < bp->b_bcount); {
2088 bzero(bp->b_data + chain->bytes,
2089 bp->b_bcount - chain->bytes);
2091 bp->b_flags |= B_NOTMETA;
2094 hammer2_chain_unlock(chain);
2097 /* bqrelse the dio to help stabilize the call to panic() */
2099 hammer2_io_bqrelse(&dio);
2100 panic("hammer2_strategy_read: unknown bref type");
2101 /*hammer2_chain_unlock(chain);*/
2108 hammer2_strategy_write(struct vop_strategy_args *ap)
2110 hammer2_pfsmount_t *pmp;
2113 hammer2_inode_t *ip;
2117 ip = VTOI(ap->a_vp);
2120 hammer2_lwinprog_ref(pmp);
2121 mtx_lock(&pmp->wthread_mtx);
2122 if (TAILQ_EMPTY(&pmp->wthread_bioq.queue)) {
2123 bioq_insert_tail(&pmp->wthread_bioq, ap->a_bio);
2124 mtx_unlock(&pmp->wthread_mtx);
2125 wakeup(&pmp->wthread_bioq);
2127 bioq_insert_tail(&pmp->wthread_bioq, ap->a_bio);
2128 mtx_unlock(&pmp->wthread_mtx);
2130 hammer2_lwinprog_wait(pmp);
2136 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2140 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2142 hammer2_inode_t *ip;
2145 ip = VTOI(ap->a_vp);
2147 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2148 ap->a_fflag, ap->a_cred);
2154 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2157 hammer2_pfsmount_t *pmp;
2161 case (MOUNTCTL_SET_EXPORT):
2162 mp = ap->a_head.a_ops->head.vv_mount;
2165 if (ap->a_ctllen != sizeof(struct export_args))
2168 rc = vfs_export(mp, &pmp->export,
2169 (const struct export_args *)ap->a_ctl);
2172 rc = vop_stdmountctl(ap);
2181 static void filt_hammer2detach(struct knote *kn);
2182 static int filt_hammer2read(struct knote *kn, long hint);
2183 static int filt_hammer2write(struct knote *kn, long hint);
2184 static int filt_hammer2vnode(struct knote *kn, long hint);
2186 static struct filterops hammer2read_filtops =
2187 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2188 NULL, filt_hammer2detach, filt_hammer2read };
2189 static struct filterops hammer2write_filtops =
2190 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2191 NULL, filt_hammer2detach, filt_hammer2write };
2192 static struct filterops hammer2vnode_filtops =
2193 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2194 NULL, filt_hammer2detach, filt_hammer2vnode };
2198 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
2200 struct vnode *vp = ap->a_vp;
2201 struct knote *kn = ap->a_kn;
2203 switch (kn->kn_filter) {
2205 kn->kn_fop = &hammer2read_filtops;
2208 kn->kn_fop = &hammer2write_filtops;
2211 kn->kn_fop = &hammer2vnode_filtops;
2214 return (EOPNOTSUPP);
2217 kn->kn_hook = (caddr_t)vp;
2219 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2225 filt_hammer2detach(struct knote *kn)
2227 struct vnode *vp = (void *)kn->kn_hook;
2229 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2233 filt_hammer2read(struct knote *kn, long hint)
2235 struct vnode *vp = (void *)kn->kn_hook;
2236 hammer2_inode_t *ip = VTOI(vp);
2239 if (hint == NOTE_REVOKE) {
2240 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2243 off = ip->size - kn->kn_fp->f_offset;
2244 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2245 if (kn->kn_sfflags & NOTE_OLDAPI)
2247 return (kn->kn_data != 0);
2252 filt_hammer2write(struct knote *kn, long hint)
2254 if (hint == NOTE_REVOKE)
2255 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2261 filt_hammer2vnode(struct knote *kn, long hint)
2263 if (kn->kn_sfflags & hint)
2264 kn->kn_fflags |= hint;
2265 if (hint == NOTE_REVOKE) {
2266 kn->kn_flags |= (EV_EOF | EV_NODATA);
2269 return (kn->kn_fflags != 0);
2277 hammer2_vop_markatime(struct vop_markatime_args *ap)
2279 hammer2_inode_t *ip;
2292 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2296 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2298 error = hammer2_vop_kqfilter(ap);
2305 struct vop_ops hammer2_vnode_vops = {
2306 .vop_default = vop_defaultop,
2307 .vop_fsync = hammer2_vop_fsync,
2308 .vop_getpages = vop_stdgetpages,
2309 .vop_putpages = vop_stdputpages,
2310 .vop_access = hammer2_vop_access,
2311 .vop_advlock = hammer2_vop_advlock,
2312 .vop_close = hammer2_vop_close,
2313 .vop_nlink = hammer2_vop_nlink,
2314 .vop_ncreate = hammer2_vop_ncreate,
2315 .vop_nsymlink = hammer2_vop_nsymlink,
2316 .vop_nremove = hammer2_vop_nremove,
2317 .vop_nrmdir = hammer2_vop_nrmdir,
2318 .vop_nrename = hammer2_vop_nrename,
2319 .vop_getattr = hammer2_vop_getattr,
2320 .vop_setattr = hammer2_vop_setattr,
2321 .vop_readdir = hammer2_vop_readdir,
2322 .vop_readlink = hammer2_vop_readlink,
2323 .vop_getpages = vop_stdgetpages,
2324 .vop_putpages = vop_stdputpages,
2325 .vop_read = hammer2_vop_read,
2326 .vop_write = hammer2_vop_write,
2327 .vop_open = hammer2_vop_open,
2328 .vop_inactive = hammer2_vop_inactive,
2329 .vop_reclaim = hammer2_vop_reclaim,
2330 .vop_nresolve = hammer2_vop_nresolve,
2331 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2332 .vop_nmkdir = hammer2_vop_nmkdir,
2333 .vop_nmknod = hammer2_vop_nmknod,
2334 .vop_ioctl = hammer2_vop_ioctl,
2335 .vop_mountctl = hammer2_vop_mountctl,
2336 .vop_bmap = hammer2_vop_bmap,
2337 .vop_strategy = hammer2_vop_strategy,
2338 .vop_kqfilter = hammer2_vop_kqfilter
2341 struct vop_ops hammer2_spec_vops = {
2342 .vop_default = vop_defaultop,
2343 .vop_fsync = hammer2_vop_fsync,
2344 .vop_read = vop_stdnoread,
2345 .vop_write = vop_stdnowrite,
2346 .vop_access = hammer2_vop_access,
2347 .vop_close = hammer2_vop_close,
2348 .vop_markatime = hammer2_vop_markatime,
2349 .vop_getattr = hammer2_vop_getattr,
2350 .vop_inactive = hammer2_vop_inactive,
2351 .vop_reclaim = hammer2_vop_reclaim,
2352 .vop_setattr = hammer2_vop_setattr
2355 struct vop_ops hammer2_fifo_vops = {
2356 .vop_default = fifo_vnoperate,
2357 .vop_fsync = hammer2_vop_fsync,
2359 .vop_read = hammer2_vop_fiforead,
2360 .vop_write = hammer2_vop_fifowrite,
2362 .vop_access = hammer2_vop_access,
2364 .vop_close = hammer2_vop_fifoclose,
2366 .vop_markatime = hammer2_vop_markatime,
2367 .vop_getattr = hammer2_vop_getattr,
2368 .vop_inactive = hammer2_vop_inactive,
2369 .vop_reclaim = hammer2_vop_reclaim,
2370 .vop_setattr = hammer2_vop_setattr,
2371 .vop_kqfilter = hammer2_vop_fifokqfilter