2 * Copyright (c) 2011-2014 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);
74 struct objcache *cache_buffer_read;
75 struct objcache *cache_buffer_write;
78 * Callback used in read path in case that a block is compressed with LZ4.
82 hammer2_decompress_LZ4_callback(const char *data, u_int bytes, struct bio *bio)
85 char *compressed_buffer;
92 if bio->bio_caller_info2.index &&
93 bio->bio_caller_info1.uvalue32 !=
94 crc32(bp->b_data, bp->b_bufsize) --- return error
97 KKASSERT(bp->b_bufsize <= HAMMER2_PBUFSIZE);
98 compressed_size = *(const int *)data;
99 KKASSERT(compressed_size <= bytes - sizeof(int));
101 compressed_buffer = objcache_get(cache_buffer_read, M_INTWAIT);
102 result = LZ4_decompress_safe(__DECONST(char *, &data[sizeof(int)]),
107 kprintf("READ PATH: Error during decompression."
109 (intmax_t)bio->bio_offset, bytes);
110 /* make sure it isn't random garbage */
111 bzero(compressed_buffer, bp->b_bufsize);
113 KKASSERT(result <= bp->b_bufsize);
114 bcopy(compressed_buffer, bp->b_data, bp->b_bufsize);
115 if (result < bp->b_bufsize)
116 bzero(bp->b_data + result, bp->b_bufsize - result);
117 objcache_put(cache_buffer_read, compressed_buffer);
119 bp->b_flags |= B_AGE;
123 * Callback used in read path in case that a block is compressed with ZLIB.
124 * It is almost identical to LZ4 callback, so in theory they can be unified,
125 * but we didn't want to make changes in bio structure for that.
129 hammer2_decompress_ZLIB_callback(const char *data, u_int bytes, struct bio *bio)
132 char *compressed_buffer;
133 z_stream strm_decompress;
139 KKASSERT(bp->b_bufsize <= HAMMER2_PBUFSIZE);
140 strm_decompress.avail_in = 0;
141 strm_decompress.next_in = Z_NULL;
143 ret = inflateInit(&strm_decompress);
146 kprintf("HAMMER2 ZLIB: Fatal error in inflateInit.\n");
148 compressed_buffer = objcache_get(cache_buffer_read, M_INTWAIT);
149 strm_decompress.next_in = __DECONST(char *, data);
151 /* XXX supply proper size, subset of device bp */
152 strm_decompress.avail_in = bytes;
153 strm_decompress.next_out = compressed_buffer;
154 strm_decompress.avail_out = bp->b_bufsize;
156 ret = inflate(&strm_decompress, Z_FINISH);
157 if (ret != Z_STREAM_END) {
158 kprintf("HAMMER2 ZLIB: Fatar error during decompression.\n");
159 bzero(compressed_buffer, bp->b_bufsize);
161 bcopy(compressed_buffer, bp->b_data, bp->b_bufsize);
162 result = bp->b_bufsize - strm_decompress.avail_out;
163 if (result < bp->b_bufsize)
164 bzero(bp->b_data + result, strm_decompress.avail_out);
165 objcache_put(cache_buffer_read, compressed_buffer);
166 ret = inflateEnd(&strm_decompress);
169 bp->b_flags |= B_AGE;
174 hammer2_knote(struct vnode *vp, int flags)
177 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
181 * Last reference to a vnode is going away but it is still cached.
185 hammer2_vop_inactive(struct vop_inactive_args *ap)
187 const hammer2_inode_data_t *ripdata;
189 hammer2_cluster_t *cluster;
206 * Detect updates to the embedded data which may be synchronized by
207 * the strategy code. Simply mark the inode modified so it gets
208 * picked up by our normal flush.
210 cluster = hammer2_inode_lock_ex(ip);
212 ripdata = &hammer2_cluster_data(cluster)->ipdata;
215 * Check for deleted inodes and recycle immediately.
217 * WARNING: nvtruncbuf() can only be safely called without the inode
218 * lock held due to the way our write thread works.
220 if (ripdata->nlinks == 0) {
224 nblksize = hammer2_calc_logical(ip, 0, &lbase, NULL);
225 hammer2_inode_unlock_ex(ip, cluster);
226 nvtruncbuf(vp, 0, nblksize, 0, 0);
229 hammer2_inode_unlock_ex(ip, cluster);
236 * Reclaim a vnode so that it can be reused; after the inode is
237 * disassociated, the filesystem must manage it alone.
241 hammer2_vop_reclaim(struct vop_reclaim_args *ap)
243 const hammer2_inode_data_t *ripdata;
244 hammer2_cluster_t *cluster;
246 hammer2_pfsmount_t *pmp;
258 * Inode must be locked for reclaim.
261 cluster = hammer2_inode_lock_ex(ip);
262 ripdata = &hammer2_cluster_data(cluster)->ipdata;
265 * The final close of a deleted file or directory marks it for
266 * destruction. The DELETED flag allows the flusher to shortcut
267 * any modified blocks still unflushed (that is, just ignore them).
269 * HAMMER2 usually does not try to optimize the freemap by returning
270 * deleted blocks to it as it does not usually know how many snapshots
271 * might be referencing portions of the file/dir.
277 * NOTE! We do not attempt to flush chains here, flushing is
278 * really fragile and could also deadlock.
283 * A reclaim can occur at any time so we cannot safely start a
284 * transaction to handle reclamation of unlinked files. Instead,
285 * the ip is left with a reference and placed on a linked list and
288 if (ripdata->nlinks == 0) {
289 hammer2_inode_unlink_t *ipul;
291 ipul = kmalloc(sizeof(*ipul), pmp->minode, M_WAITOK | M_ZERO);
294 spin_lock(&pmp->list_spin);
295 TAILQ_INSERT_TAIL(&pmp->unlinkq, ipul, entry);
296 spin_unlock(&pmp->list_spin);
297 hammer2_inode_unlock_ex(ip, cluster); /* unlock */
298 /* retain ref from vp for ipul */
300 hammer2_inode_unlock_ex(ip, cluster); /* unlock */
301 hammer2_inode_drop(ip); /* vp ref */
303 /* cluster no longer referenced */
304 /* cluster = NULL; not needed */
307 * XXX handle background sync when ip dirty, kernel will no longer
308 * notify us regarding this inode because there is no longer a
309 * vnode attached to it.
318 hammer2_vop_fsync(struct vop_fsync_args *ap)
321 hammer2_trans_t trans;
322 hammer2_cluster_t *cluster;
330 /* XXX can't do this yet */
331 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_ISFLUSH);
332 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
334 hammer2_trans_init(&trans, ip->pmp, 0);
335 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
338 * Calling chain_flush here creates a lot of duplicative
339 * COW operations due to non-optimal vnode ordering.
341 * Only do it for an actual fsync() syscall. The other forms
342 * which call this function will eventually call chain_flush
343 * on the volume root as a catch-all, which is far more optimal.
345 cluster = hammer2_inode_lock_ex(ip);
346 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
348 if (ip->flags & (HAMMER2_INODE_RESIZED|HAMMER2_INODE_MTIME))
349 hammer2_inode_fsync(&trans, ip, cluster);
353 * XXX creates discontinuity w/modify_tid
355 if (ap->a_flags & VOP_FSYNC_SYSCALL) {
356 hammer2_flush(&trans, cluster);
359 hammer2_inode_unlock_ex(ip, cluster);
360 hammer2_trans_done(&trans);
368 hammer2_vop_access(struct vop_access_args *ap)
370 hammer2_inode_t *ip = VTOI(ap->a_vp);
371 const hammer2_inode_data_t *ipdata;
372 hammer2_cluster_t *cluster;
378 cluster = hammer2_inode_lock_sh(ip);
379 ipdata = &hammer2_cluster_data(cluster)->ipdata;
380 uid = hammer2_to_unix_xid(&ipdata->uid);
381 gid = hammer2_to_unix_xid(&ipdata->gid);
382 error = vop_helper_access(ap, uid, gid, ipdata->mode, ipdata->uflags);
383 hammer2_inode_unlock_sh(ip, cluster);
391 hammer2_vop_getattr(struct vop_getattr_args *ap)
393 const hammer2_inode_data_t *ipdata;
394 hammer2_cluster_t *cluster;
395 hammer2_pfsmount_t *pmp;
407 cluster = hammer2_inode_lock_sh(ip);
408 ipdata = &hammer2_cluster_data(cluster)->ipdata;
409 KKASSERT(hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE);
411 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
412 vap->va_fileid = ipdata->inum;
413 vap->va_mode = ipdata->mode;
414 vap->va_nlink = ipdata->nlinks;
415 vap->va_uid = hammer2_to_unix_xid(&ipdata->uid);
416 vap->va_gid = hammer2_to_unix_xid(&ipdata->gid);
419 vap->va_size = ip->size; /* protected by shared lock */
420 vap->va_blocksize = HAMMER2_PBUFSIZE;
421 vap->va_flags = ipdata->uflags;
422 hammer2_time_to_timespec(ipdata->ctime, &vap->va_ctime);
423 hammer2_time_to_timespec(ipdata->mtime, &vap->va_mtime);
424 hammer2_time_to_timespec(ipdata->mtime, &vap->va_atime);
426 vap->va_bytes = vap->va_size; /* XXX */
427 vap->va_type = hammer2_get_vtype(ipdata);
429 vap->va_uid_uuid = ipdata->uid;
430 vap->va_gid_uuid = ipdata->gid;
431 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
434 hammer2_inode_unlock_sh(ip, cluster);
442 hammer2_vop_setattr(struct vop_setattr_args *ap)
444 const hammer2_inode_data_t *ripdata;
445 hammer2_inode_data_t *wipdata;
447 hammer2_cluster_t *cluster;
448 hammer2_trans_t trans;
460 hammer2_update_time(&ctime);
464 if (ip->pmp->ronly) {
469 hammer2_pfs_memory_wait(ip->pmp);
470 hammer2_trans_init(&trans, ip->pmp, 0);
471 cluster = hammer2_inode_lock_ex(ip);
472 ripdata = &hammer2_cluster_data(cluster)->ipdata;
475 if (vap->va_flags != VNOVAL) {
478 flags = ripdata->uflags;
479 error = vop_helper_setattr_flags(&flags, vap->va_flags,
480 hammer2_to_unix_xid(&ripdata->uid),
483 if (ripdata->uflags != flags) {
484 wipdata = hammer2_cluster_modify_ip(&trans, ip,
486 wipdata->uflags = flags;
487 wipdata->ctime = ctime;
488 kflags |= NOTE_ATTRIB;
492 if (ripdata->uflags & (IMMUTABLE | APPEND)) {
499 if (ripdata->uflags & (IMMUTABLE | APPEND)) {
503 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
504 mode_t cur_mode = ripdata->mode;
505 uid_t cur_uid = hammer2_to_unix_xid(&ripdata->uid);
506 gid_t cur_gid = hammer2_to_unix_xid(&ripdata->gid);
510 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
512 &cur_uid, &cur_gid, &cur_mode);
514 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
515 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
516 if (bcmp(&uuid_uid, &ripdata->uid, sizeof(uuid_uid)) ||
517 bcmp(&uuid_gid, &ripdata->gid, sizeof(uuid_gid)) ||
518 ripdata->mode != cur_mode
520 wipdata = hammer2_cluster_modify_ip(&trans, ip,
522 wipdata->uid = uuid_uid;
523 wipdata->gid = uuid_gid;
524 wipdata->mode = cur_mode;
525 wipdata->ctime = ctime;
529 kflags |= NOTE_ATTRIB;
536 if (vap->va_size != VNOVAL && ip->size != vap->va_size) {
539 if (vap->va_size == ip->size)
541 hammer2_inode_unlock_ex(ip, cluster);
542 if (vap->va_size < ip->size) {
543 hammer2_truncate_file(ip, vap->va_size);
545 hammer2_extend_file(ip, vap->va_size);
547 cluster = hammer2_inode_lock_ex(ip);
549 ripdata = &hammer2_cluster_data(cluster)->ipdata;
558 /* atime not supported */
559 if (vap->va_atime.tv_sec != VNOVAL) {
560 wipdata = hammer2_cluster_modify_ip(&trans, ip, cluster, 0);
561 wipdata->atime = hammer2_timespec_to_time(&vap->va_atime);
562 kflags |= NOTE_ATTRIB;
567 if (vap->va_mtime.tv_sec != VNOVAL) {
568 wipdata = hammer2_cluster_modify_ip(&trans, ip, cluster, 0);
569 wipdata->mtime = hammer2_timespec_to_time(&vap->va_mtime);
570 kflags |= NOTE_ATTRIB;
575 if (vap->va_mode != (mode_t)VNOVAL) {
576 mode_t cur_mode = ripdata->mode;
577 uid_t cur_uid = hammer2_to_unix_xid(&ripdata->uid);
578 gid_t cur_gid = hammer2_to_unix_xid(&ripdata->gid);
580 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
581 cur_uid, cur_gid, &cur_mode);
582 if (error == 0 && ripdata->mode != cur_mode) {
583 wipdata = hammer2_cluster_modify_ip(&trans, ip,
585 wipdata->mode = cur_mode;
586 wipdata->ctime = ctime;
587 kflags |= NOTE_ATTRIB;
594 * If a truncation occurred we must call inode_fsync() now in order
595 * to trim the related data chains, otherwise a later expansion can
599 hammer2_cluster_modsync(cluster);
602 hammer2_inode_fsync(&trans, ip, cluster);
605 * Cleanup. If domtime is set an additional inode modification
606 * must be flagged. All other modifications will have already
607 * set INODE_MODIFIED and called vsetisdirty().
611 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED |
612 HAMMER2_INODE_MTIME);
616 hammer2_cluster_modsync(cluster);
617 hammer2_inode_unlock_ex(ip, cluster);
618 hammer2_trans_done(&trans);
619 hammer2_knote(ip->vp, kflags);
627 hammer2_vop_readdir(struct vop_readdir_args *ap)
629 const hammer2_inode_data_t *ipdata;
631 hammer2_inode_t *xip;
632 hammer2_cluster_t *cparent;
633 hammer2_cluster_t *cluster;
634 hammer2_cluster_t *xcluster;
635 hammer2_blockref_t bref;
637 hammer2_key_t key_next;
652 saveoff = uio->uio_offset;
655 * Setup cookies directory entry cookies if requested
657 if (ap->a_ncookies) {
658 ncookies = uio->uio_resid / 16 + 1;
661 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
668 cparent = hammer2_inode_lock_sh(ip);
669 ipdata = &hammer2_cluster_data(cparent)->ipdata;
672 * Handle artificial entries. To ensure that only positive 64 bit
673 * quantities are returned to userland we always strip off bit 63.
674 * The hash code is designed such that codes 0x0000-0x7FFF are not
675 * used, allowing us to use these codes for articial entries.
677 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
678 * allow '..' to cross the mount point into (e.g.) the super-root.
681 cluster = (void *)(intptr_t)-1; /* non-NULL for early goto done case */
684 inum = ipdata->inum & HAMMER2_DIRHASH_USERMSK;
685 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
689 cookies[cookie_index] = saveoff;
692 if (cookie_index == ncookies)
698 * Be careful with lockorder when accessing ".."
700 * (ip is the current dir. xip is the parent dir).
702 inum = ipdata->inum & HAMMER2_DIRHASH_USERMSK;
703 while (ip->pip != NULL && ip != ip->pmp->iroot) {
705 hammer2_inode_ref(xip);
706 hammer2_inode_unlock_sh(ip, cparent);
707 xcluster = hammer2_inode_lock_sh(xip);
708 cparent = hammer2_inode_lock_sh(ip);
709 hammer2_inode_drop(xip);
710 ipdata = &hammer2_cluster_data(cparent)->ipdata;
711 if (xip == ip->pip) {
712 inum = hammer2_cluster_data(xcluster)->
713 ipdata.inum & HAMMER2_DIRHASH_USERMSK;
714 hammer2_inode_unlock_sh(xip, xcluster);
717 hammer2_inode_unlock_sh(xip, xcluster);
719 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
723 cookies[cookie_index] = saveoff;
726 if (cookie_index == ncookies)
730 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
731 if (hammer2_debug & 0x0020)
732 kprintf("readdir: lkey %016jx\n", lkey);
735 * parent is the inode cluster, already locked for us. Don't
736 * double lock shared locks as this will screw up upgrades.
741 cluster = hammer2_cluster_lookup(cparent, &key_next, lkey, lkey,
742 HAMMER2_LOOKUP_SHARED, &ddflag);
743 if (cluster == NULL) {
744 cluster = hammer2_cluster_lookup(cparent, &key_next,
745 lkey, (hammer2_key_t)-1,
746 HAMMER2_LOOKUP_SHARED, &ddflag);
749 hammer2_cluster_bref(cluster, &bref);
751 if (hammer2_debug & 0x0020)
752 kprintf("readdir: p=%p chain=%p %016jx (next %016jx)\n",
753 cparent->focus, cluster->focus,
756 if (bref.type == HAMMER2_BREF_TYPE_INODE) {
757 ipdata = &hammer2_cluster_data(cluster)->ipdata;
758 dtype = hammer2_get_dtype(ipdata);
759 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
760 r = vop_write_dirent(&error, uio,
762 HAMMER2_DIRHASH_USERMSK,
769 cookies[cookie_index] = saveoff;
772 /* XXX chain error */
773 kprintf("bad chain type readdir %d\n", bref.type);
777 * Keys may not be returned in order so once we have a
778 * placemarker (cluster) the scan must allow the full range
779 * or some entries will be missed.
781 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
782 key_next, (hammer2_key_t)-1,
783 HAMMER2_LOOKUP_SHARED);
785 hammer2_cluster_bref(cluster, &bref);
786 saveoff = (bref.key & HAMMER2_DIRHASH_USERMSK) + 1;
788 saveoff = (hammer2_key_t)-1;
790 if (cookie_index == ncookies)
794 hammer2_cluster_unlock(cluster);
796 hammer2_inode_unlock_sh(ip, cparent);
798 *ap->a_eofflag = (cluster == NULL);
799 if (hammer2_debug & 0x0020)
800 kprintf("readdir: done at %016jx\n", saveoff);
801 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
802 if (error && cookie_index == 0) {
804 kfree(cookies, M_TEMP);
806 *ap->a_cookies = NULL;
810 *ap->a_ncookies = cookie_index;
811 *ap->a_cookies = cookies;
819 * hammer2_vop_readlink { vp, uio, cred }
823 hammer2_vop_readlink(struct vop_readlink_args *ap)
830 if (vp->v_type != VLNK)
834 error = hammer2_read_file(ip, ap->a_uio, 0);
840 hammer2_vop_read(struct vop_read_args *ap)
850 * Read operations supported on this vnode?
853 if (vp->v_type != VREG)
863 seqcount = ap->a_ioflag >> 16;
864 bigread = (uio->uio_resid > 100 * 1024 * 1024);
866 error = hammer2_read_file(ip, uio, seqcount);
872 hammer2_vop_write(struct vop_write_args *ap)
875 hammer2_trans_t trans;
884 * Read operations supported on this vnode?
887 if (vp->v_type != VREG)
896 if (ip->pmp->ronly) {
900 seqcount = ap->a_ioflag >> 16;
901 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
904 * Check resource limit
906 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
907 uio->uio_offset + uio->uio_resid >
908 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
909 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
913 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
916 * The transaction interlocks against flushes initiations
917 * (note: but will run concurrently with the actual flush).
919 hammer2_trans_init(&trans, ip->pmp, 0);
920 error = hammer2_write_file(ip, uio, ap->a_ioflag, seqcount);
921 hammer2_trans_done(&trans);
927 * Perform read operations on a file or symlink given an UNLOCKED
930 * The passed ip is not locked.
934 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
945 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
947 ccms_thread_unlock(&ip->topo_cst);
949 while (uio->uio_resid > 0 && uio->uio_offset < size) {
956 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
959 error = cluster_read(ip->vp, leof, lbase, lblksize,
960 uio->uio_resid, seqcount * BKVASIZE,
965 loff = (int)(uio->uio_offset - lbase);
967 if (n > uio->uio_resid)
969 if (n > size - uio->uio_offset)
970 n = (int)(size - uio->uio_offset);
971 bp->b_flags |= B_AGE;
972 uiomove((char *)bp->b_data + loff, n, uio);
979 * Write to the file represented by the inode via the logical buffer cache.
980 * The inode may represent a regular file or a symlink.
982 * The inode must not be locked.
986 hammer2_write_file(hammer2_inode_t *ip,
987 struct uio *uio, int ioflag, int seqcount)
989 hammer2_key_t old_eof;
990 hammer2_key_t new_eof;
999 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1000 if (ioflag & IO_APPEND)
1001 uio->uio_offset = ip->size;
1003 ccms_thread_unlock(&ip->topo_cst);
1006 * Extend the file if necessary. If the write fails at some point
1007 * we will truncate it back down to cover as much as we were able
1010 * Doing this now makes it easier to calculate buffer sizes in
1017 if (uio->uio_offset + uio->uio_resid > old_eof) {
1018 new_eof = uio->uio_offset + uio->uio_resid;
1020 hammer2_extend_file(ip, new_eof);
1021 kflags |= NOTE_EXTEND;
1029 while (uio->uio_resid > 0) {
1030 hammer2_key_t lbase;
1038 * Don't allow the buffer build to blow out the buffer
1041 if ((ioflag & IO_RECURSE) == 0)
1042 bwillwrite(HAMMER2_PBUFSIZE);
1045 * This nominally tells us how much we can cluster and
1046 * what the logical buffer size needs to be. Currently
1047 * we don't try to cluster the write and just handle one
1050 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
1052 loff = (int)(uio->uio_offset - lbase);
1054 KKASSERT(lblksize <= 65536);
1057 * Calculate bytes to copy this transfer and whether the
1058 * copy completely covers the buffer or not.
1061 n = lblksize - loff;
1062 if (n > uio->uio_resid) {
1064 if (loff == lbase && uio->uio_offset + n == new_eof)
1076 if (uio->uio_segflg == UIO_NOCOPY) {
1078 * Issuing a write with the same data backing the
1079 * buffer. Instantiate the buffer to collect the
1080 * backing vm pages, then read-in any missing bits.
1082 * This case is used by vop_stdputpages().
1084 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
1085 if ((bp->b_flags & B_CACHE) == 0) {
1087 error = bread(ip->vp, lbase, lblksize, &bp);
1089 } else if (trivial) {
1091 * Even though we are entirely overwriting the buffer
1092 * we may still have to zero it out to avoid a
1093 * mmap/write visibility issue.
1095 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
1096 if ((bp->b_flags & B_CACHE) == 0)
1100 * Partial overwrite, read in any missing bits then
1101 * replace the portion being written.
1103 * (The strategy code will detect zero-fill physical
1104 * blocks for this case).
1106 error = bread(ip->vp, lbase, lblksize, &bp);
1117 * Ok, copy the data in
1119 error = uiomove(bp->b_data + loff, n, uio);
1120 kflags |= NOTE_WRITE;
1128 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
1129 * with IO_SYNC or IO_ASYNC set. These writes
1130 * must be handled as the pageout daemon expects.
1132 if (ioflag & IO_SYNC) {
1134 } else if ((ioflag & IO_DIRECT) && endofblk) {
1136 } else if (ioflag & IO_ASYNC) {
1144 * Cleanup. If we extended the file EOF but failed to write through
1145 * the entire write is a failure and we have to back-up.
1147 if (error && new_eof != old_eof) {
1148 hammer2_truncate_file(ip, old_eof);
1149 } else if (modified) {
1150 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1151 hammer2_update_time(&ip->mtime);
1152 atomic_set_int(&ip->flags, HAMMER2_INODE_MTIME);
1153 ccms_thread_unlock(&ip->topo_cst);
1155 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1156 hammer2_knote(ip->vp, kflags);
1157 vsetisdirty(ip->vp);
1163 * Truncate the size of a file. The inode must not be locked.
1165 * NOTE: Caller handles setting HAMMER2_INODE_MODIFIED
1167 * WARNING: nvtruncbuf() can only be safely called without the inode lock
1168 * held due to the way our write thread works.
1172 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1174 hammer2_key_t lbase;
1179 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1180 nvtruncbuf(ip->vp, nsize,
1181 nblksize, (int)nsize & (nblksize - 1),
1184 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1186 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1187 ccms_thread_unlock(&ip->topo_cst);
1192 * Extend the size of a file. The inode must not be locked.
1194 * NOTE: Caller handles setting HAMMER2_INODE_MODIFIED
1198 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1200 hammer2_key_t lbase;
1201 hammer2_key_t osize;
1206 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
1209 ccms_thread_unlock(&ip->topo_cst);
1212 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1213 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1219 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1225 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1227 hammer2_inode_t *ip;
1228 hammer2_inode_t *dip;
1229 hammer2_cluster_t *cparent;
1230 hammer2_cluster_t *cluster;
1231 const hammer2_inode_data_t *ipdata;
1232 hammer2_key_t key_next;
1234 struct namecache *ncp;
1235 const uint8_t *name;
1242 dip = VTOI(ap->a_dvp);
1243 ncp = ap->a_nch->ncp;
1244 name = ncp->nc_name;
1245 name_len = ncp->nc_nlen;
1246 lhc = hammer2_dirhash(name, name_len);
1249 * Note: In DragonFly the kernel handles '.' and '..'.
1251 cparent = hammer2_inode_lock_sh(dip);
1252 cluster = hammer2_cluster_lookup(cparent, &key_next,
1253 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1254 HAMMER2_LOOKUP_SHARED, &ddflag);
1256 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE) {
1257 ipdata = &hammer2_cluster_data(cluster)->ipdata;
1258 if (ipdata->name_len == name_len &&
1259 bcmp(ipdata->filename, name, name_len) == 0) {
1263 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
1265 lhc + HAMMER2_DIRHASH_LOMASK,
1266 HAMMER2_LOOKUP_SHARED);
1268 hammer2_inode_unlock_sh(dip, cparent);
1271 * Resolve hardlink entries before acquiring the inode.
1274 ipdata = &hammer2_cluster_data(cluster)->ipdata;
1275 if (ipdata->type == HAMMER2_OBJTYPE_HARDLINK) {
1276 hammer2_tid_t inum = ipdata->inum;
1277 error = hammer2_hardlink_find(dip, NULL, cluster);
1279 kprintf("hammer2: unable to find hardlink "
1280 "0x%016jx\n", inum);
1281 hammer2_cluster_unlock(cluster);
1289 * nresolve needs to resolve hardlinks, the original cluster is not
1293 ip = hammer2_inode_get(dip->pmp, dip, cluster);
1294 ipdata = &hammer2_cluster_data(cluster)->ipdata;
1295 if (ipdata->type == HAMMER2_OBJTYPE_HARDLINK) {
1296 kprintf("nresolve: fixup hardlink\n");
1297 hammer2_inode_ref(ip);
1298 hammer2_inode_unlock_ex(ip, NULL);
1299 hammer2_cluster_unlock(cluster);
1300 cluster = hammer2_inode_lock_ex(ip);
1301 ipdata = &hammer2_cluster_data(cluster)->ipdata;
1302 hammer2_inode_drop(ip);
1303 kprintf("nresolve: fixup to type %02x\n", ipdata->type);
1311 * Deconsolidate any hardlink whos nlinks == 1. Ignore errors.
1312 * If an error occurs chain and ip are left alone.
1314 * XXX upgrade shared lock?
1316 if (ochain && chain &&
1317 chain->data->ipdata.nlinks == 1 && !dip->pmp->ronly) {
1318 kprintf("hammer2: need to unconsolidate hardlink for %s\n",
1319 chain->data->ipdata.filename);
1320 /* XXX retain shared lock on dip? (currently not held) */
1321 hammer2_trans_init(&trans, dip->pmp, 0);
1322 hammer2_hardlink_deconsolidate(&trans, dip, &chain, &ochain);
1323 hammer2_trans_done(&trans);
1328 * Acquire the related vnode
1330 * NOTE: For error processing, only ENOENT resolves the namecache
1331 * entry to NULL, otherwise we just return the error and
1332 * leave the namecache unresolved.
1334 * NOTE: multiple hammer2_inode structures can be aliased to the
1335 * same chain element, for example for hardlinks. This
1336 * use case does not 'reattach' inode associations that
1337 * might already exist, but always allocates a new one.
1339 * WARNING: inode structure is locked exclusively via inode_get
1340 * but chain was locked shared. inode_unlock_ex()
1341 * will handle it properly.
1344 vp = hammer2_igetv(ip, cluster, &error);
1347 cache_setvp(ap->a_nch, vp);
1348 } else if (error == ENOENT) {
1349 cache_setvp(ap->a_nch, NULL);
1351 hammer2_inode_unlock_ex(ip, cluster);
1354 * The vp should not be released until after we've disposed
1355 * of our locks, because it might cause vop_inactive() to
1362 cache_setvp(ap->a_nch, NULL);
1364 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1365 ("resolve error %d/%p ap %p\n",
1366 error, ap->a_nch->ncp->nc_vp, ap));
1373 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1375 hammer2_inode_t *dip;
1376 hammer2_inode_t *ip;
1377 hammer2_cluster_t *cparent;
1381 dip = VTOI(ap->a_dvp);
1383 if ((ip = dip->pip) == NULL) {
1388 cparent = hammer2_inode_lock_ex(ip);
1389 *ap->a_vpp = hammer2_igetv(ip, cparent, &error);
1390 hammer2_inode_unlock_ex(ip, cparent);
1398 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1400 hammer2_inode_t *dip;
1401 hammer2_inode_t *nip;
1402 hammer2_trans_t trans;
1403 hammer2_cluster_t *cluster;
1404 struct namecache *ncp;
1405 const uint8_t *name;
1410 dip = VTOI(ap->a_dvp);
1411 if (dip->pmp->ronly) {
1416 ncp = ap->a_nch->ncp;
1417 name = ncp->nc_name;
1418 name_len = ncp->nc_nlen;
1421 hammer2_pfs_memory_wait(dip->pmp);
1422 hammer2_trans_init(&trans, dip->pmp, HAMMER2_TRANS_NEWINODE);
1423 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1424 name, name_len, &cluster, &error);
1426 KKASSERT(nip == NULL);
1429 *ap->a_vpp = hammer2_igetv(nip, cluster, &error);
1430 hammer2_inode_unlock_ex(nip, cluster);
1432 hammer2_trans_done(&trans);
1435 cache_setunresolved(ap->a_nch);
1436 cache_setvp(ap->a_nch, *ap->a_vpp);
1443 * Return the largest contiguous physical disk range for the logical
1444 * request, in bytes.
1446 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
1448 * Basically disabled, the logical buffer write thread has to deal with
1449 * buffers one-at-a-time.
1453 hammer2_vop_bmap(struct vop_bmap_args *ap)
1455 *ap->a_doffsetp = NOOFFSET;
1460 return (EOPNOTSUPP);
1465 hammer2_vop_open(struct vop_open_args *ap)
1467 return vop_stdopen(ap);
1471 * hammer2_vop_advlock { vp, id, op, fl, flags }
1475 hammer2_vop_advlock(struct vop_advlock_args *ap)
1477 hammer2_inode_t *ip = VTOI(ap->a_vp);
1478 const hammer2_inode_data_t *ipdata;
1479 hammer2_cluster_t *cparent;
1482 cparent = hammer2_inode_lock_sh(ip);
1483 ipdata = &hammer2_cluster_data(cparent)->ipdata;
1484 size = ipdata->size;
1485 hammer2_inode_unlock_sh(ip, cparent);
1486 return (lf_advlock(ap, &ip->advlock, size));
1492 hammer2_vop_close(struct vop_close_args *ap)
1494 return vop_stdclose(ap);
1498 * hammer2_vop_nlink { nch, dvp, vp, cred }
1500 * Create a hardlink from (vp) to {dvp, nch}.
1504 hammer2_vop_nlink(struct vop_nlink_args *ap)
1506 hammer2_inode_t *fdip; /* target directory to create link in */
1507 hammer2_inode_t *tdip; /* target directory to create link in */
1508 hammer2_inode_t *cdip; /* common parent directory */
1509 hammer2_inode_t *ip; /* inode we are hardlinking to */
1510 hammer2_cluster_t *cluster;
1511 hammer2_cluster_t *fdcluster;
1512 hammer2_cluster_t *tdcluster;
1513 hammer2_cluster_t *cdcluster;
1514 hammer2_trans_t trans;
1515 struct namecache *ncp;
1516 const uint8_t *name;
1521 tdip = VTOI(ap->a_dvp);
1522 if (tdip->pmp->ronly) {
1527 ncp = ap->a_nch->ncp;
1528 name = ncp->nc_name;
1529 name_len = ncp->nc_nlen;
1532 * ip represents the file being hardlinked. The file could be a
1533 * normal file or a hardlink target if it has already been hardlinked.
1534 * If ip is a hardlinked target then ip->pip represents the location
1535 * of the hardlinked target, NOT the location of the hardlink pointer.
1537 * Bump nlinks and potentially also create or move the hardlink
1538 * target in the parent directory common to (ip) and (tdip). The
1539 * consolidation code can modify ip->cluster and ip->pip. The
1540 * returned cluster is locked.
1542 ip = VTOI(ap->a_vp);
1543 hammer2_pfs_memory_wait(ip->pmp);
1544 hammer2_trans_init(&trans, ip->pmp, HAMMER2_TRANS_NEWINODE);
1547 * The common parent directory must be locked first to avoid deadlocks.
1548 * Also note that fdip and/or tdip might match cdip.
1551 cdip = hammer2_inode_common_parent(fdip, tdip);
1552 cdcluster = hammer2_inode_lock_ex(cdip);
1553 fdcluster = hammer2_inode_lock_ex(fdip);
1554 tdcluster = hammer2_inode_lock_ex(tdip);
1555 cluster = hammer2_inode_lock_ex(ip);
1556 error = hammer2_hardlink_consolidate(&trans, ip, &cluster,
1557 cdip, cdcluster, 1);
1562 * Create a directory entry connected to the specified cluster.
1564 * WARNING! chain can get moved by the connect (indirectly due to
1565 * potential indirect block creation).
1567 error = hammer2_inode_connect(&trans, &cluster, 1,
1571 cache_setunresolved(ap->a_nch);
1572 cache_setvp(ap->a_nch, ap->a_vp);
1575 hammer2_inode_unlock_ex(ip, cluster);
1576 hammer2_inode_unlock_ex(tdip, tdcluster);
1577 hammer2_inode_unlock_ex(fdip, fdcluster);
1578 hammer2_inode_unlock_ex(cdip, cdcluster);
1579 hammer2_inode_drop(cdip);
1580 hammer2_trans_done(&trans);
1587 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1589 * The operating system has already ensured that the directory entry
1590 * does not exist and done all appropriate namespace locking.
1594 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1596 hammer2_inode_t *dip;
1597 hammer2_inode_t *nip;
1598 hammer2_trans_t trans;
1599 hammer2_cluster_t *ncluster;
1600 struct namecache *ncp;
1601 const uint8_t *name;
1606 dip = VTOI(ap->a_dvp);
1607 if (dip->pmp->ronly) {
1612 ncp = ap->a_nch->ncp;
1613 name = ncp->nc_name;
1614 name_len = ncp->nc_nlen;
1615 hammer2_pfs_memory_wait(dip->pmp);
1616 hammer2_trans_init(&trans, dip->pmp, HAMMER2_TRANS_NEWINODE);
1619 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1620 name, name_len, &ncluster, &error);
1622 KKASSERT(nip == NULL);
1625 *ap->a_vpp = hammer2_igetv(nip, ncluster, &error);
1626 hammer2_inode_unlock_ex(nip, ncluster);
1628 hammer2_trans_done(&trans);
1631 cache_setunresolved(ap->a_nch);
1632 cache_setvp(ap->a_nch, *ap->a_vpp);
1639 * Make a device node (typically a fifo)
1643 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1645 hammer2_inode_t *dip;
1646 hammer2_inode_t *nip;
1647 hammer2_trans_t trans;
1648 hammer2_cluster_t *ncluster;
1649 struct namecache *ncp;
1650 const uint8_t *name;
1655 dip = VTOI(ap->a_dvp);
1656 if (dip->pmp->ronly) {
1661 ncp = ap->a_nch->ncp;
1662 name = ncp->nc_name;
1663 name_len = ncp->nc_nlen;
1664 hammer2_pfs_memory_wait(dip->pmp);
1665 hammer2_trans_init(&trans, dip->pmp, HAMMER2_TRANS_NEWINODE);
1668 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1669 name, name_len, &ncluster, &error);
1671 KKASSERT(nip == NULL);
1674 *ap->a_vpp = hammer2_igetv(nip, ncluster, &error);
1675 hammer2_inode_unlock_ex(nip, ncluster);
1677 hammer2_trans_done(&trans);
1680 cache_setunresolved(ap->a_nch);
1681 cache_setvp(ap->a_nch, *ap->a_vpp);
1688 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1692 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1694 hammer2_inode_t *dip;
1695 hammer2_inode_t *nip;
1696 hammer2_cluster_t *ncparent;
1697 hammer2_trans_t trans;
1698 struct namecache *ncp;
1699 const uint8_t *name;
1703 dip = VTOI(ap->a_dvp);
1704 if (dip->pmp->ronly)
1707 ncp = ap->a_nch->ncp;
1708 name = ncp->nc_name;
1709 name_len = ncp->nc_nlen;
1710 hammer2_pfs_memory_wait(dip->pmp);
1711 hammer2_trans_init(&trans, dip->pmp, HAMMER2_TRANS_NEWINODE);
1714 ap->a_vap->va_type = VLNK; /* enforce type */
1716 nip = hammer2_inode_create(&trans, dip, ap->a_vap, ap->a_cred,
1717 name, name_len, &ncparent, &error);
1719 KKASSERT(nip == NULL);
1721 hammer2_trans_done(&trans);
1724 *ap->a_vpp = hammer2_igetv(nip, ncparent, &error);
1727 * Build the softlink (~like file data) and finalize the namecache.
1733 hammer2_inode_data_t *nipdata;
1735 nipdata = &hammer2_cluster_wdata(ncparent)->ipdata;
1736 /* nipdata = &nip->chain->data->ipdata;XXX */
1737 bytes = strlen(ap->a_target);
1739 if (bytes <= HAMMER2_EMBEDDED_BYTES) {
1740 KKASSERT(nipdata->op_flags &
1741 HAMMER2_OPFLAG_DIRECTDATA);
1742 bcopy(ap->a_target, nipdata->u.data, bytes);
1743 nipdata->size = bytes;
1745 hammer2_cluster_modsync(ncparent);
1746 hammer2_inode_unlock_ex(nip, ncparent);
1747 /* nipdata = NULL; not needed */
1749 hammer2_inode_unlock_ex(nip, ncparent);
1750 /* nipdata = NULL; not needed */
1751 bzero(&auio, sizeof(auio));
1752 bzero(&aiov, sizeof(aiov));
1753 auio.uio_iov = &aiov;
1754 auio.uio_segflg = UIO_SYSSPACE;
1755 auio.uio_rw = UIO_WRITE;
1756 auio.uio_resid = bytes;
1757 auio.uio_iovcnt = 1;
1758 auio.uio_td = curthread;
1759 aiov.iov_base = ap->a_target;
1760 aiov.iov_len = bytes;
1761 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1762 /* XXX handle error */
1766 hammer2_inode_unlock_ex(nip, ncparent);
1768 hammer2_trans_done(&trans);
1771 * Finalize namecache
1774 cache_setunresolved(ap->a_nch);
1775 cache_setvp(ap->a_nch, *ap->a_vpp);
1776 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1782 * hammer2_vop_nremove { nch, dvp, cred }
1786 hammer2_vop_nremove(struct vop_nremove_args *ap)
1788 hammer2_inode_t *dip;
1789 hammer2_trans_t trans;
1790 struct namecache *ncp;
1791 const uint8_t *name;
1796 dip = VTOI(ap->a_dvp);
1797 if (dip->pmp->ronly) {
1802 ncp = ap->a_nch->ncp;
1803 name = ncp->nc_name;
1804 name_len = ncp->nc_nlen;
1806 hammer2_pfs_memory_wait(dip->pmp);
1807 hammer2_trans_init(&trans, dip->pmp, 0);
1808 error = hammer2_unlink_file(&trans, dip, name, name_len,
1809 0, NULL, ap->a_nch, -1);
1810 hammer2_run_unlinkq(&trans, dip->pmp);
1811 hammer2_trans_done(&trans);
1813 cache_unlink(ap->a_nch);
1819 * hammer2_vop_nrmdir { nch, dvp, cred }
1823 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1825 hammer2_inode_t *dip;
1826 hammer2_trans_t trans;
1827 struct namecache *ncp;
1828 const uint8_t *name;
1833 dip = VTOI(ap->a_dvp);
1834 if (dip->pmp->ronly) {
1839 ncp = ap->a_nch->ncp;
1840 name = ncp->nc_name;
1841 name_len = ncp->nc_nlen;
1843 hammer2_pfs_memory_wait(dip->pmp);
1844 hammer2_trans_init(&trans, dip->pmp, 0);
1845 hammer2_run_unlinkq(&trans, dip->pmp);
1846 error = hammer2_unlink_file(&trans, dip, name, name_len,
1847 1, NULL, ap->a_nch, -1);
1848 hammer2_trans_done(&trans);
1850 cache_unlink(ap->a_nch);
1856 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1860 hammer2_vop_nrename(struct vop_nrename_args *ap)
1862 struct namecache *fncp;
1863 struct namecache *tncp;
1864 hammer2_inode_t *cdip;
1865 hammer2_inode_t *fdip;
1866 hammer2_inode_t *tdip;
1867 hammer2_inode_t *ip;
1868 hammer2_cluster_t *cluster;
1869 hammer2_cluster_t *fdcluster;
1870 hammer2_cluster_t *tdcluster;
1871 hammer2_cluster_t *cdcluster;
1872 hammer2_trans_t trans;
1873 const uint8_t *fname;
1875 const uint8_t *tname;
1881 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1883 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1886 fdip = VTOI(ap->a_fdvp); /* source directory */
1887 tdip = VTOI(ap->a_tdvp); /* target directory */
1889 if (fdip->pmp->ronly)
1893 fncp = ap->a_fnch->ncp; /* entry name in source */
1894 fname = fncp->nc_name;
1895 fname_len = fncp->nc_nlen;
1897 tncp = ap->a_tnch->ncp; /* entry name in target */
1898 tname = tncp->nc_name;
1899 tname_len = tncp->nc_nlen;
1901 hammer2_pfs_memory_wait(tdip->pmp);
1902 hammer2_trans_init(&trans, tdip->pmp, 0);
1905 * ip is the inode being renamed. If this is a hardlink then
1906 * ip represents the actual file and not the hardlink marker.
1908 ip = VTOI(fncp->nc_vp);
1913 * The common parent directory must be locked first to avoid deadlocks.
1914 * Also note that fdip and/or tdip might match cdip.
1916 * WARNING! fdip may not match ip->pip. That is, if the source file
1917 * is already a hardlink then what we are renaming is the
1918 * hardlink pointer, not the hardlink itself. The hardlink
1919 * directory (ip->pip) will already be at a common parent
1922 * Be sure to use ip->pip when finding the common parent
1923 * against tdip or we might accidently move the hardlink
1924 * target into a subdirectory that makes it inaccessible to
1927 cdip = hammer2_inode_common_parent(ip->pip, tdip);
1928 cdcluster = hammer2_inode_lock_ex(cdip);
1929 fdcluster = hammer2_inode_lock_ex(fdip);
1930 tdcluster = hammer2_inode_lock_ex(tdip);
1933 * Keep a tight grip on the inode so the temporary unlinking from
1934 * the source location prior to linking to the target location
1935 * does not cause the cluster to be destroyed.
1937 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
1938 * unlinking elements from their directories. Locking
1939 * the nlinks field does not lock the whole inode.
1941 hammer2_inode_ref(ip);
1944 * Remove target if it exists.
1946 error = hammer2_unlink_file(&trans, tdip, tname, tname_len,
1947 -1, NULL, ap->a_tnch, -1);
1949 if (error && error != ENOENT)
1953 * When renaming a hardlinked file we may have to re-consolidate
1954 * the location of the hardlink target.
1956 * If ip represents a regular file the consolidation code essentially
1957 * does nothing other than return the same locked cluster that was
1960 * The returned cluster will be locked.
1962 * WARNING! We do not currently have a local copy of ipdata but
1963 * we do use one later remember that it must be reloaded
1964 * on any modification to the inode, including connects.
1966 cluster = hammer2_inode_lock_ex(ip);
1967 error = hammer2_hardlink_consolidate(&trans, ip, &cluster,
1968 cdip, cdcluster, 0);
1973 * Disconnect (fdip, fname) from the source directory. This will
1974 * disconnect (ip) if it represents a direct file. If (ip) represents
1975 * a hardlink the HARDLINK pointer object will be removed but the
1976 * hardlink will stay intact.
1978 * Always pass nch as NULL because we intend to reconnect the inode,
1979 * so we don't want hammer2_unlink_file() to rename it to the hidden
1980 * open-but-unlinked directory.
1982 * The target cluster may be marked DELETED but will not be destroyed
1983 * since we retain our hold on ip and cluster.
1985 * NOTE: We pass nlinks as 0 (not -1) in order to retain the file's
1988 error = hammer2_unlink_file(&trans, fdip, fname, fname_len,
1989 -1, &hlink, NULL, 0);
1990 KKASSERT(error != EAGAIN);
1995 * Reconnect ip to target directory using cluster. Chains cannot
1996 * actually be moved, so this will duplicate the cluster in the new
1997 * spot and assign it to the ip, replacing the old cluster.
1999 * WARNING: Because recursive locks are allowed and we unlinked the
2000 * file that we have a cluster-in-hand for just above, the
2001 * cluster might have been delete-duplicated. We must
2002 * refactor the cluster.
2004 * WARNING: Chain locks can lock buffer cache buffers, to avoid
2005 * deadlocks we want to unlock before issuing a cache_*()
2006 * op (that might have to lock a vnode).
2008 * NOTE: Pass nlinks as 0 because we retained the link count from
2009 * the unlink, so we do not have to modify it.
2011 error = hammer2_inode_connect(&trans, &cluster, hlink,
2013 tname, tname_len, 0);
2015 KKASSERT(cluster != NULL);
2016 hammer2_inode_repoint(ip, (hlink ? ip->pip : tdip), cluster);
2019 hammer2_inode_unlock_ex(ip, cluster);
2020 hammer2_inode_unlock_ex(tdip, tdcluster);
2021 hammer2_inode_unlock_ex(fdip, fdcluster);
2022 hammer2_inode_unlock_ex(cdip, cdcluster);
2023 hammer2_inode_drop(ip);
2024 hammer2_inode_drop(cdip);
2025 hammer2_run_unlinkq(&trans, fdip->pmp);
2026 hammer2_trans_done(&trans);
2029 * Issue the namecache update after unlocking all the internal
2030 * hammer structures, otherwise we might deadlock.
2032 if (tnch_error == 0) {
2033 cache_unlink(ap->a_tnch);
2034 cache_setunresolved(ap->a_tnch);
2037 cache_rename(ap->a_fnch, ap->a_tnch);
2046 * WARNING: The strategy code cannot safely use hammer2 transactions
2047 * as this can deadlock against vfs_sync's vfsync() call
2048 * if multiple flushes are queued.
2050 static int hammer2_strategy_read(struct vop_strategy_args *ap);
2051 static int hammer2_strategy_write(struct vop_strategy_args *ap);
2052 static void hammer2_strategy_read_callback(hammer2_io_t *dio,
2053 hammer2_cluster_t *cluster,
2054 hammer2_chain_t *chain,
2055 void *arg_p, off_t arg_o);
2059 hammer2_vop_strategy(struct vop_strategy_args *ap)
2070 error = hammer2_strategy_read(ap);
2071 ++hammer2_iod_file_read;
2074 error = hammer2_strategy_write(ap);
2075 ++hammer2_iod_file_write;
2078 bp->b_error = error = EINVAL;
2079 bp->b_flags |= B_ERROR;
2088 hammer2_strategy_read(struct vop_strategy_args *ap)
2093 hammer2_inode_t *ip;
2094 hammer2_cluster_t *cparent;
2095 hammer2_cluster_t *cluster;
2096 hammer2_key_t key_dummy;
2097 hammer2_key_t lbase;
2103 ip = VTOI(ap->a_vp);
2104 nbio = push_bio(bio);
2106 lbase = bio->bio_offset;
2107 KKASSERT(((int)lbase & HAMMER2_PBUFMASK) == 0);
2109 cparent = hammer2_inode_lock_sh(ip);
2110 cluster = hammer2_cluster_lookup(cparent, &key_dummy,
2112 HAMMER2_LOOKUP_NODATA |
2113 HAMMER2_LOOKUP_SHARED,
2115 hammer2_inode_unlock_sh(ip, cparent);
2118 * Data is zero-fill if no cluster could be found
2119 * (XXX or EIO on a cluster failure).
2121 if (cluster == NULL) {
2124 bzero(bp->b_data, bp->b_bcount);
2130 * Cluster elements must be type INODE or type DATA, but the
2131 * compression mode (or not) for DATA chains can be different for
2132 * each chain. This will be handled by the callback.
2134 btype = hammer2_cluster_type(cluster);
2135 if (btype != HAMMER2_BREF_TYPE_INODE &&
2136 btype != HAMMER2_BREF_TYPE_DATA) {
2137 panic("READ PATH: hammer2_strategy_read: unknown bref type");
2139 hammer2_chain_load_async(cluster, hammer2_strategy_read_callback, nbio);
2144 * Read callback for block that is not compressed.
2148 hammer2_strategy_read_callback(hammer2_io_t *dio,
2149 hammer2_cluster_t *cluster,
2150 hammer2_chain_t *chain,
2151 void *arg_p, off_t arg_o)
2153 struct bio *bio = arg_p;
2154 struct buf *bp = bio->bio_buf;
2159 * Extract data and handle iteration on I/O failure. arg_o is the
2160 * cluster index for iteration.
2163 if (dio->bp->b_flags & B_ERROR) {
2165 if (i >= cluster->nchains) {
2166 bp->b_flags |= B_ERROR;
2167 bp->b_error = dio->bp->b_error;
2169 hammer2_cluster_unlock(cluster);
2171 chain = cluster->array[i];
2172 kprintf("hammer2: IO CHAIN-%d %p\n", i, chain);
2173 hammer2_adjreadcounter(&chain->bref,
2175 hammer2_io_breadcb(chain->hmp,
2176 chain->bref.data_off,
2178 hammer2_strategy_read_callback,
2184 data = hammer2_io_data(dio, chain->bref.data_off);
2186 data = (void *)chain->data;
2189 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
2191 * Data is embedded in the inode (copy from inode).
2193 bcopy(((hammer2_inode_data_t *)data)->u.data,
2194 bp->b_data, HAMMER2_EMBEDDED_BYTES);
2195 bzero(bp->b_data + HAMMER2_EMBEDDED_BYTES,
2196 bp->b_bcount - HAMMER2_EMBEDDED_BYTES);
2199 } else if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2201 * Data is on-media, issue device I/O and copy.
2203 * XXX direct-IO shortcut could go here XXX.
2205 switch (HAMMER2_DEC_COMP(chain->bref.methods)) {
2206 case HAMMER2_COMP_LZ4:
2207 hammer2_decompress_LZ4_callback(data, chain->bytes,
2210 case HAMMER2_COMP_ZLIB:
2211 hammer2_decompress_ZLIB_callback(data, chain->bytes,
2214 case HAMMER2_COMP_NONE:
2215 KKASSERT(chain->bytes <= bp->b_bcount);
2216 bcopy(data, bp->b_data, chain->bytes);
2217 if (chain->bytes < bp->b_bcount) {
2218 bzero(bp->b_data + chain->bytes,
2219 bp->b_bcount - chain->bytes);
2221 bp->b_flags |= B_NOTMETA;
2226 panic("hammer2_strategy_read: "
2227 "unknown compression type");
2230 /* bqrelse the dio to help stabilize the call to panic() */
2232 hammer2_io_bqrelse(&dio);
2233 panic("hammer2_strategy_read: unknown bref type");
2235 hammer2_cluster_unlock(cluster);
2241 hammer2_strategy_write(struct vop_strategy_args *ap)
2243 hammer2_pfsmount_t *pmp;
2246 hammer2_inode_t *ip;
2250 ip = VTOI(ap->a_vp);
2253 hammer2_lwinprog_ref(pmp);
2254 mtx_lock(&pmp->wthread_mtx);
2255 if (TAILQ_EMPTY(&pmp->wthread_bioq.queue)) {
2256 bioq_insert_tail(&pmp->wthread_bioq, ap->a_bio);
2257 mtx_unlock(&pmp->wthread_mtx);
2258 wakeup(&pmp->wthread_bioq);
2260 bioq_insert_tail(&pmp->wthread_bioq, ap->a_bio);
2261 mtx_unlock(&pmp->wthread_mtx);
2263 hammer2_lwinprog_wait(pmp);
2269 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2273 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2275 hammer2_inode_t *ip;
2279 ip = VTOI(ap->a_vp);
2281 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2282 ap->a_fflag, ap->a_cred);
2289 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2292 hammer2_pfsmount_t *pmp;
2297 case (MOUNTCTL_SET_EXPORT):
2298 mp = ap->a_head.a_ops->head.vv_mount;
2301 if (ap->a_ctllen != sizeof(struct export_args))
2304 rc = vfs_export(mp, &pmp->export,
2305 (const struct export_args *)ap->a_ctl);
2308 rc = vop_stdmountctl(ap);
2316 * This handles unlinked open files after the vnode is finally dereferenced.
2317 * To avoid deadlocks it cannot be called from the normal vnode recycling
2318 * path, so we call it (1) after a unlink, rmdir, or rename, (2) on every
2319 * flush, and (3) on umount.
2322 hammer2_run_unlinkq(hammer2_trans_t *trans, hammer2_pfsmount_t *pmp)
2324 const hammer2_inode_data_t *ripdata;
2325 hammer2_inode_unlink_t *ipul;
2326 hammer2_inode_t *ip;
2327 hammer2_cluster_t *cluster;
2328 hammer2_cluster_t *cparent;
2330 if (TAILQ_EMPTY(&pmp->unlinkq))
2334 spin_lock(&pmp->list_spin);
2335 while ((ipul = TAILQ_FIRST(&pmp->unlinkq)) != NULL) {
2336 TAILQ_REMOVE(&pmp->unlinkq, ipul, entry);
2337 spin_unlock(&pmp->list_spin);
2339 kfree(ipul, pmp->minode);
2341 cluster = hammer2_inode_lock_ex(ip);
2342 ripdata = &hammer2_cluster_data(cluster)->ipdata;
2343 if (hammer2_debug & 0x400) {
2344 kprintf("hammer2: unlink on reclaim: %s refs=%d\n",
2345 cluster->focus->data->ipdata.filename,
2348 KKASSERT(ripdata->nlinks == 0);
2350 cparent = hammer2_cluster_parent(cluster);
2351 hammer2_cluster_delete(trans, cparent, cluster,
2352 HAMMER2_DELETE_PERMANENT);
2353 hammer2_cluster_unlock(cparent);
2354 hammer2_inode_unlock_ex(ip, cluster); /* inode lock */
2355 hammer2_inode_drop(ip); /* ipul ref */
2357 spin_lock(&pmp->list_spin);
2359 spin_unlock(&pmp->list_spin);
2367 static void filt_hammer2detach(struct knote *kn);
2368 static int filt_hammer2read(struct knote *kn, long hint);
2369 static int filt_hammer2write(struct knote *kn, long hint);
2370 static int filt_hammer2vnode(struct knote *kn, long hint);
2372 static struct filterops hammer2read_filtops =
2373 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2374 NULL, filt_hammer2detach, filt_hammer2read };
2375 static struct filterops hammer2write_filtops =
2376 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2377 NULL, filt_hammer2detach, filt_hammer2write };
2378 static struct filterops hammer2vnode_filtops =
2379 { FILTEROP_ISFD | FILTEROP_MPSAFE,
2380 NULL, filt_hammer2detach, filt_hammer2vnode };
2384 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
2386 struct vnode *vp = ap->a_vp;
2387 struct knote *kn = ap->a_kn;
2389 switch (kn->kn_filter) {
2391 kn->kn_fop = &hammer2read_filtops;
2394 kn->kn_fop = &hammer2write_filtops;
2397 kn->kn_fop = &hammer2vnode_filtops;
2400 return (EOPNOTSUPP);
2403 kn->kn_hook = (caddr_t)vp;
2405 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2411 filt_hammer2detach(struct knote *kn)
2413 struct vnode *vp = (void *)kn->kn_hook;
2415 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
2419 filt_hammer2read(struct knote *kn, long hint)
2421 struct vnode *vp = (void *)kn->kn_hook;
2422 hammer2_inode_t *ip = VTOI(vp);
2425 if (hint == NOTE_REVOKE) {
2426 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2429 off = ip->size - kn->kn_fp->f_offset;
2430 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
2431 if (kn->kn_sfflags & NOTE_OLDAPI)
2433 return (kn->kn_data != 0);
2438 filt_hammer2write(struct knote *kn, long hint)
2440 if (hint == NOTE_REVOKE)
2441 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
2447 filt_hammer2vnode(struct knote *kn, long hint)
2449 if (kn->kn_sfflags & hint)
2450 kn->kn_fflags |= hint;
2451 if (hint == NOTE_REVOKE) {
2452 kn->kn_flags |= (EV_EOF | EV_NODATA);
2455 return (kn->kn_fflags != 0);
2463 hammer2_vop_markatime(struct vop_markatime_args *ap)
2465 hammer2_inode_t *ip;
2478 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2482 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2484 error = hammer2_vop_kqfilter(ap);
2491 struct vop_ops hammer2_vnode_vops = {
2492 .vop_default = vop_defaultop,
2493 .vop_fsync = hammer2_vop_fsync,
2494 .vop_getpages = vop_stdgetpages,
2495 .vop_putpages = vop_stdputpages,
2496 .vop_access = hammer2_vop_access,
2497 .vop_advlock = hammer2_vop_advlock,
2498 .vop_close = hammer2_vop_close,
2499 .vop_nlink = hammer2_vop_nlink,
2500 .vop_ncreate = hammer2_vop_ncreate,
2501 .vop_nsymlink = hammer2_vop_nsymlink,
2502 .vop_nremove = hammer2_vop_nremove,
2503 .vop_nrmdir = hammer2_vop_nrmdir,
2504 .vop_nrename = hammer2_vop_nrename,
2505 .vop_getattr = hammer2_vop_getattr,
2506 .vop_setattr = hammer2_vop_setattr,
2507 .vop_readdir = hammer2_vop_readdir,
2508 .vop_readlink = hammer2_vop_readlink,
2509 .vop_getpages = vop_stdgetpages,
2510 .vop_putpages = vop_stdputpages,
2511 .vop_read = hammer2_vop_read,
2512 .vop_write = hammer2_vop_write,
2513 .vop_open = hammer2_vop_open,
2514 .vop_inactive = hammer2_vop_inactive,
2515 .vop_reclaim = hammer2_vop_reclaim,
2516 .vop_nresolve = hammer2_vop_nresolve,
2517 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2518 .vop_nmkdir = hammer2_vop_nmkdir,
2519 .vop_nmknod = hammer2_vop_nmknod,
2520 .vop_ioctl = hammer2_vop_ioctl,
2521 .vop_mountctl = hammer2_vop_mountctl,
2522 .vop_bmap = hammer2_vop_bmap,
2523 .vop_strategy = hammer2_vop_strategy,
2524 .vop_kqfilter = hammer2_vop_kqfilter
2527 struct vop_ops hammer2_spec_vops = {
2528 .vop_default = vop_defaultop,
2529 .vop_fsync = hammer2_vop_fsync,
2530 .vop_read = vop_stdnoread,
2531 .vop_write = vop_stdnowrite,
2532 .vop_access = hammer2_vop_access,
2533 .vop_close = hammer2_vop_close,
2534 .vop_markatime = hammer2_vop_markatime,
2535 .vop_getattr = hammer2_vop_getattr,
2536 .vop_inactive = hammer2_vop_inactive,
2537 .vop_reclaim = hammer2_vop_reclaim,
2538 .vop_setattr = hammer2_vop_setattr
2541 struct vop_ops hammer2_fifo_vops = {
2542 .vop_default = fifo_vnoperate,
2543 .vop_fsync = hammer2_vop_fsync,
2545 .vop_read = hammer2_vop_fiforead,
2546 .vop_write = hammer2_vop_fifowrite,
2548 .vop_access = hammer2_vop_access,
2550 .vop_close = hammer2_vop_fifoclose,
2552 .vop_markatime = hammer2_vop_markatime,
2553 .vop_getattr = hammer2_vop_getattr,
2554 .vop_inactive = hammer2_vop_inactive,
2555 .vop_reclaim = hammer2_vop_reclaim,
2556 .vop_setattr = hammer2_vop_setattr,
2557 .vop_kqfilter = hammer2_vop_fifokqfilter