2 * Copyright (c) 2011-2012 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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/fcntl.h>
41 #include <sys/namei.h>
42 #include <sys/mount.h>
43 #include <sys/vnode.h>
44 #include <sys/mountctl.h>
45 #include <sys/dirent.h>
50 #define ZFOFFSET (-2LL)
52 static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio,
54 static int hammer2_write_file(hammer2_inode_t *ip, hammer2_chain_t **chainp,
55 struct uio *uio, int ioflag, int seqcount);
56 static hammer2_off_t hammer2_assign_physical(hammer2_inode_t *ip,
57 hammer2_key_t lbase, int lblksize, int *errorp);
58 static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize);
59 static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize);
63 hammer2_knote(struct vnode *vp, int flags)
66 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
70 * Last reference to a vnode is going away but it is still cached.
74 hammer2_vop_inactive(struct vop_inactive_args *ap)
76 hammer2_chain_t *chain;
80 struct hammer2_mount *hmp;
95 * Detect updates to the embedded data which may be synchronized by
96 * the strategy code. Simply mark the inode modified so it gets
97 * picked up by our normal flush.
99 chain = hammer2_inode_lock_ex(ip);
100 if (ip->flags & HAMMER2_INODE_DIRTYEMBED) {
101 atomic_clear_int(&ip->flags, HAMMER2_INODE_DIRTYEMBED);
102 hammer2_chain_modify(ip->hmp, chain, 0);
106 * Check for deleted inodes and recycle immediately.
108 if (chain && (chain->flags & HAMMER2_CHAIN_DELETED)) {
109 hammer2_inode_unlock_ex(ip, chain);
112 hammer2_inode_unlock_ex(ip, chain);
118 * Reclaim a vnode so that it can be reused; after the inode is
119 * disassociated, the filesystem must manage it alone.
123 hammer2_vop_reclaim(struct vop_reclaim_args *ap)
125 hammer2_chain_t *chain;
127 hammer2_mount_t *hmp;
137 * Set SUBMODIFIED so we can detect and propagate the DESTROYED
138 * bit in the flush code.
140 chain = hammer2_inode_lock_ex(ip);
143 if (chain->flags & HAMMER2_CHAIN_DELETED) {
144 KKASSERT(chain->flags & HAMMER2_CHAIN_DELETED);
145 atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROYED |
146 HAMMER2_CHAIN_SUBMODIFIED);
148 hammer2_chain_flush(hmp, chain, 0);
149 kprintf("vop_reclaim vp %p ip %p refs %d\n",
151 if (ip->refs > 2) /* (our lock + vp ref) */
152 hammer2_inode_unlock_ex(ip, chain); /* unlock */
154 hammer2_inode_put(ip, chain); /* unlock & disconnect */
155 hammer2_inode_drop(ip); /* vp ref */
158 * XXX handle background sync when ip dirty, kernel will no longer
159 * notify us regarding this inode because there is no longer a
160 * vnode attached to it.
168 hammer2_vop_fsync(struct vop_fsync_args *ap)
170 hammer2_chain_t *chain;
172 hammer2_mount_t *hmp;
179 chain = hammer2_inode_lock_ex(ip);
180 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
183 * Detect updates to the embedded data which may be synchronized by
184 * the strategy code. Simply mark the inode modified so it gets
185 * picked up by our normal flush.
187 if (ip->flags & HAMMER2_INODE_DIRTYEMBED) {
188 atomic_clear_int(&ip->flags, HAMMER2_INODE_DIRTYEMBED);
189 hammer2_chain_modify(hmp, chain, 0);
193 * Calling chain_flush here creates a lot of duplicative
194 * COW operations due to non-optimal vnode ordering.
196 * Only do it for an actual fsync() syscall. The other forms
197 * which call this function will eventually call chain_flush
198 * on the volume root as a catch-all, which is far more optimal.
200 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
201 if (ap->a_flags & VOP_FSYNC_SYSCALL)
202 hammer2_chain_flush(hmp, chain, 0);
203 hammer2_inode_unlock_ex(ip, chain);
209 hammer2_vop_access(struct vop_access_args *ap)
211 hammer2_inode_t *ip = VTOI(ap->a_vp);
212 hammer2_chain_t *chain;
213 hammer2_inode_data_t *ipdata;
218 chain = hammer2_inode_lock_sh(ip);
219 ipdata = &chain->data->ipdata;
220 uid = hammer2_to_unix_xid(&ipdata->uid);
221 gid = hammer2_to_unix_xid(&ipdata->gid);
222 error = vop_helper_access(ap, uid, gid, ipdata->mode, ipdata->uflags);
223 hammer2_inode_unlock_sh(ip, chain);
230 hammer2_vop_getattr(struct vop_getattr_args *ap)
232 hammer2_inode_data_t *ipdata;
233 hammer2_pfsmount_t *pmp;
235 hammer2_chain_t *chain;
245 chain = hammer2_inode_lock_sh(ip);
246 ipdata = &chain->data->ipdata;
248 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
249 vap->va_fileid = ipdata->inum;
250 vap->va_mode = ipdata->mode;
251 vap->va_nlink = ipdata->nlinks;
252 vap->va_uid = hammer2_to_unix_xid(&ipdata->uid);
253 vap->va_gid = hammer2_to_unix_xid(&ipdata->gid);
256 vap->va_size = ipdata->size;
257 vap->va_blocksize = HAMMER2_PBUFSIZE;
258 vap->va_flags = ipdata->uflags;
259 hammer2_time_to_timespec(ipdata->ctime, &vap->va_ctime);
260 hammer2_time_to_timespec(ipdata->mtime, &vap->va_mtime);
261 hammer2_time_to_timespec(ipdata->mtime, &vap->va_atime);
263 vap->va_bytes = vap->va_size; /* XXX */
264 vap->va_type = hammer2_get_vtype(chain);
266 vap->va_uid_uuid = ipdata->uid;
267 vap->va_gid_uuid = ipdata->gid;
268 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
271 hammer2_inode_unlock_sh(ip, chain);
278 hammer2_vop_setattr(struct vop_setattr_args *ap)
280 hammer2_inode_data_t *ipdata;
281 hammer2_chain_t *chain;
283 hammer2_mount_t *hmp;
293 hammer2_update_time(&ctime);
301 chain = hammer2_inode_lock_ex(ip);
302 ipdata = &chain->data->ipdata;
305 if (vap->va_flags != VNOVAL) {
308 flags = ipdata->uflags;
309 error = vop_helper_setattr_flags(&flags, vap->va_flags,
310 hammer2_to_unix_xid(&ipdata->uid),
313 if (ipdata->uflags != flags) {
314 hammer2_chain_modify(hmp, chain, 0);
315 ipdata->uflags = flags;
316 ipdata->ctime = ctime;
317 kflags |= NOTE_ATTRIB;
319 if (ipdata->uflags & (IMMUTABLE | APPEND)) {
326 if (ipdata->uflags & (IMMUTABLE | APPEND)) {
330 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
331 mode_t cur_mode = ipdata->mode;
332 uid_t cur_uid = hammer2_to_unix_xid(&ipdata->uid);
333 gid_t cur_gid = hammer2_to_unix_xid(&ipdata->gid);
337 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
339 &cur_uid, &cur_gid, &cur_mode);
341 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
342 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
343 if (bcmp(&uuid_uid, &ipdata->uid, sizeof(uuid_uid)) ||
344 bcmp(&uuid_gid, &ipdata->gid, sizeof(uuid_gid)) ||
345 ipdata->mode != cur_mode
347 hammer2_chain_modify(hmp, chain, 0);
348 ipdata->uid = uuid_uid;
349 ipdata->gid = uuid_gid;
350 ipdata->mode = cur_mode;
351 ipdata->ctime = ctime;
353 kflags |= NOTE_ATTRIB;
360 if (vap->va_size != VNOVAL && ipdata->size != vap->va_size) {
363 if (vap->va_size == ipdata->size)
365 if (vap->va_size < ipdata->size) {
366 hammer2_truncate_file(ip, vap->va_size);
368 hammer2_extend_file(ip, vap->va_size);
378 /* atime not supported */
379 if (vap->va_atime.tv_sec != VNOVAL) {
380 hammer2_chain_modify(hmp, chain, 0);
381 ipdata->atime = hammer2_timespec_to_time(&vap->va_atime);
382 kflags |= NOTE_ATTRIB;
385 if (vap->va_mtime.tv_sec != VNOVAL) {
386 hammer2_chain_modify(hmp, chain, 0);
387 ipdata->mtime = hammer2_timespec_to_time(&vap->va_mtime);
388 kflags |= NOTE_ATTRIB;
390 if (vap->va_mode != (mode_t)VNOVAL) {
391 mode_t cur_mode = ipdata->mode;
392 uid_t cur_uid = hammer2_to_unix_xid(&ipdata->uid);
393 gid_t cur_gid = hammer2_to_unix_xid(&ipdata->gid);
395 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
396 cur_uid, cur_gid, &cur_mode);
397 if (error == 0 && ipdata->mode != cur_mode) {
398 hammer2_chain_modify(hmp, chain, 0);
399 ipdata->mode = cur_mode;
400 ipdata->ctime = ctime;
401 kflags |= NOTE_ATTRIB;
405 hammer2_inode_unlock_ex(ip, chain);
411 hammer2_vop_readdir(struct vop_readdir_args *ap)
413 hammer2_inode_data_t *ipdata;
414 hammer2_mount_t *hmp;
416 hammer2_inode_t *xip;
417 hammer2_chain_t *parent;
418 hammer2_chain_t *xparent;
419 hammer2_chain_t *chain;
434 saveoff = uio->uio_offset;
437 * Setup cookies directory entry cookies if requested
439 if (ap->a_ncookies) {
440 ncookies = uio->uio_resid / 16 + 1;
443 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
450 parent = hammer2_inode_lock_sh(ip);
451 ipdata = &parent->data->ipdata;
454 * Handle artificial entries. To ensure that only positive 64 bit
455 * quantities are returned to userland we always strip off bit 63.
456 * The hash code is designed such that codes 0x0000-0x7FFF are not
457 * used, allowing us to use these codes for articial entries.
459 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
460 * allow '..' to cross the mount point into (e.g.) the super-root.
463 chain = (void *)(intptr_t)-1; /* non-NULL for early goto done case */
466 inum = ipdata->inum & HAMMER2_DIRHASH_USERMSK;
467 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
471 cookies[cookie_index] = saveoff;
474 if (cookie_index == ncookies)
480 * Be careful with lockorder when accessing ".."
482 * (parent is the current dir. xip is the parent dir).
484 inum = parent->data->ipdata.inum & HAMMER2_DIRHASH_USERMSK;
485 while (ip->pip != NULL && ip != ip->pmp->iroot) {
487 hammer2_inode_ref(xip);
488 hammer2_inode_unlock_sh(ip, parent);
489 xparent = hammer2_inode_lock_sh(xip);
490 parent = hammer2_inode_lock_sh(ip);
491 hammer2_inode_drop(xip);
492 if (xip == ip->pip) {
493 inum = xparent->data->ipdata.inum &
494 HAMMER2_DIRHASH_USERMSK;
495 hammer2_inode_unlock_sh(xip, xparent);
498 hammer2_inode_unlock_sh(xip, xparent);
500 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
504 cookies[cookie_index] = saveoff;
507 if (cookie_index == ncookies)
511 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
514 * parent is the inode chain, already locked for us. Don't
515 * double lock shared locks as this will screw up upgrades.
520 chain = hammer2_chain_lookup(hmp, &parent, lkey, lkey,
521 HAMMER2_LOOKUP_SHARED);
523 chain = hammer2_chain_lookup(hmp, &parent,
524 lkey, (hammer2_key_t)-1,
525 HAMMER2_LOOKUP_SHARED);
528 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
529 dtype = hammer2_get_dtype(chain);
530 saveoff = chain->bref.key & HAMMER2_DIRHASH_USERMSK;
531 r = vop_write_dirent(&error, uio,
532 chain->data->ipdata.inum &
533 HAMMER2_DIRHASH_USERMSK,
535 chain->data->ipdata.name_len,
536 chain->data->ipdata.filename);
540 cookies[cookie_index] = saveoff;
543 /* XXX chain error */
544 kprintf("bad chain type readdir %d\n",
549 * Keys may not be returned in order so once we have a
550 * placemarker (chain) the scan must allow the full range
551 * or some entries will be missed.
553 chain = hammer2_chain_next(hmp, &parent, chain,
554 HAMMER2_DIRHASH_VISIBLE,
556 HAMMER2_LOOKUP_SHARED);
558 saveoff = (chain->bref.key &
559 HAMMER2_DIRHASH_USERMSK) + 1;
561 saveoff = (hammer2_key_t)-1;
563 if (cookie_index == ncookies)
567 hammer2_chain_unlock(hmp, chain);
569 hammer2_inode_unlock_sh(ip, parent);
571 *ap->a_eofflag = (chain == NULL);
572 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
573 if (error && cookie_index == 0) {
575 kfree(cookies, M_TEMP);
577 *ap->a_cookies = NULL;
581 *ap->a_ncookies = cookie_index;
582 *ap->a_cookies = cookies;
589 * hammer2_vop_readlink { vp, uio, cred }
593 hammer2_vop_readlink(struct vop_readlink_args *ap)
596 hammer2_mount_t *hmp;
601 if (vp->v_type != VLNK)
606 error = hammer2_read_file(ip, ap->a_uio, 0);
612 hammer2_vop_read(struct vop_read_args *ap)
615 hammer2_mount_t *hmp;
623 * Read operations supported on this vnode?
626 if (vp->v_type != VREG)
637 seqcount = ap->a_ioflag >> 16;
638 bigread = (uio->uio_resid > 100 * 1024 * 1024);
640 error = hammer2_read_file(ip, uio, seqcount);
646 hammer2_vop_write(struct vop_write_args *ap)
648 hammer2_chain_t *chain;
649 hammer2_mount_t *hmp;
659 * Read operations supported on this vnode?
662 if (vp->v_type != VREG)
675 seqcount = ap->a_ioflag >> 16;
676 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
679 * Check resource limit
681 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
682 uio->uio_offset + uio->uio_resid >
683 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
684 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
688 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
691 * ip must be locked if extending the file.
692 * ip must be locked to avoid racing a truncation.
694 * ip must be marked modified, particularly because the write
695 * might wind up being copied into the embedded data area.
697 chain = hammer2_inode_lock_ex(ip);
698 error = hammer2_write_file(ip, &chain, uio, ap->a_ioflag, seqcount);
699 hammer2_inode_unlock_ex(ip, chain);
704 * Perform read operations on a file or symlink given an UNLOCKED
707 * The passed ip is not locked.
711 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
713 hammer2_chain_t *chain;
723 * We can't hold a shared lock on ip's chain across file bread's
724 * because the bread operation will itself obtain a shared lock,
725 * resulting in one thread holding 2 shared refs. This will deadlock
726 * against temporary lock upgrades. Temporary lock upgrades are
727 * needed to insert new chain structures into a parent's RB tree.
729 * We should be able to safely retain the shared lock on ip itself.
731 chain = hammer2_inode_lock_sh(ip);
732 size = chain->data->ipdata.size;
733 hammer2_chain_unlock(ip->hmp, chain);
736 while (uio->uio_resid > 0 && uio->uio_offset < size) {
743 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
746 error = cluster_read(ip->vp, leof, lbase, lblksize,
747 uio->uio_resid, seqcount * BKVASIZE,
752 loff = (int)(uio->uio_offset - lbase);
754 if (n > uio->uio_resid)
756 if (n > size - uio->uio_offset)
757 n = (int)(size - uio->uio_offset);
758 bp->b_flags |= B_AGE;
759 uiomove((char *)bp->b_data + loff, n, uio);
762 hammer2_inode_unlock_sh(ip, chain);
767 * Called with a locked (ip) to do the underlying write to a file or
768 * to build the symlink target.
772 hammer2_write_file(hammer2_inode_t *ip, hammer2_chain_t **chainp,
774 int ioflag, int seqcount)
776 hammer2_inode_data_t *ipdata;
777 hammer2_key_t old_eof;
786 ipdata = &ip->chain->data->ipdata;
787 if (ioflag & IO_APPEND)
788 uio->uio_offset = ipdata->size;
793 * Extend the file if necessary. If the write fails at some point
794 * we will truncate it back down to cover as much as we were able
797 * Doing this now makes it easier to calculate buffer sizes in
800 old_eof = ipdata->size;
801 if (uio->uio_offset + uio->uio_resid > ipdata->size) {
803 hammer2_extend_file(ip, uio->uio_offset + uio->uio_resid);
804 kflags |= NOTE_EXTEND;
810 while (uio->uio_resid > 0) {
819 * Don't allow the buffer build to blow out the buffer
822 if ((ioflag & IO_RECURSE) == 0) {
824 * XXX should try to leave this unlocked through
827 hammer2_inode_unlock_ex(ip, *chainp);
828 bwillwrite(HAMMER2_PBUFSIZE);
829 *chainp = hammer2_inode_lock_ex(ip);
830 ipdata = &(*chainp)->data->ipdata; /* reload */
833 /* XXX bigwrite & signal check test */
836 * This nominally tells us how much we can cluster and
837 * what the logical buffer size needs to be. Currently
838 * we don't try to cluster the write and just handle one
841 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
843 loff = (int)(uio->uio_offset - lbase);
846 * Calculate bytes to copy this transfer and whether the
847 * copy completely covers the buffer or not.
851 if (n > uio->uio_resid) {
853 if (uio->uio_offset + n == ipdata->size)
855 } else if (loff == 0) {
862 if (uio->uio_segflg == UIO_NOCOPY) {
864 * Issuing a write with the same data backing the
865 * buffer. Instantiate the buffer to collect the
866 * backing vm pages, then read-in any missing bits.
868 * This case is used by vop_stdputpages().
870 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
871 if ((bp->b_flags & B_CACHE) == 0) {
873 error = bread(ip->vp, lbase, lblksize, &bp);
875 } else if (trivial) {
877 * Even though we are entirely overwriting the buffer
878 * we may still have to zero it out to avoid a
879 * mmap/write visibility issue.
881 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
882 if ((bp->b_flags & B_CACHE) == 0)
886 * Partial overwrite, read in any missing bits then
887 * replace the portion being written.
889 * (The strategy code will detect zero-fill physical
890 * blocks for this case).
892 error = bread(ip->vp, lbase, lblksize, &bp);
903 * We have to assign physical storage to the buffer we intend
904 * to dirty or write now to avoid deadlocks in the strategy
907 * This can return NOOFFSET for inode-embedded data. The
908 * strategy code will take care of it in that case.
910 bp->b_bio2.bio_offset =
911 hammer2_assign_physical(ip, lbase, lblksize, &error);
918 * Ok, copy the data in
920 hammer2_inode_unlock_ex(ip, *chainp);
921 error = uiomove(bp->b_data + loff, n, uio);
922 *chainp = hammer2_inode_lock_ex(ip);
923 ipdata = &(*chainp)->data->ipdata; /* reload */
924 kflags |= NOTE_WRITE;
932 /* XXX update ip_data.mtime */
935 * Once we dirty a buffer any cached offset becomes invalid.
937 * NOTE: For cluster_write() always use the trailing block
938 * size, which is HAMMER2_PBUFSIZE. lblksize is the
939 * eof-straddling blocksize and is incorrect.
941 bp->b_flags |= B_AGE;
942 if (ioflag & IO_SYNC) {
944 } else if ((ioflag & IO_DIRECT) && loff + n == lblksize) {
945 if (bp->b_bcount == HAMMER2_PBUFSIZE)
946 bp->b_flags |= B_CLUSTEROK;
948 } else if (ioflag & IO_ASYNC) {
950 } else if (hammer2_cluster_enable) {
951 if (bp->b_bcount == HAMMER2_PBUFSIZE)
952 bp->b_flags |= B_CLUSTEROK;
953 cluster_write(bp, leof, HAMMER2_PBUFSIZE, seqcount);
955 if (bp->b_bcount == HAMMER2_PBUFSIZE)
956 bp->b_flags |= B_CLUSTEROK;
962 * Cleanup. If we extended the file EOF but failed to write through
963 * the entire write is a failure and we have to back-up.
965 if (error && ipdata->size != old_eof) {
966 hammer2_truncate_file(ip, old_eof);
967 } else if (modified) {
968 KKASSERT(ip->chain == *chainp);
969 hammer2_chain_modify(ip->hmp, *chainp, 0);
970 hammer2_update_time(&ipdata->mtime);
972 hammer2_knote(ip->vp, kflags);
977 * Assign physical storage to a logical block.
979 * NOOFFSET is returned if the data is inode-embedded. In this case the
980 * strategy code will simply bcopy() the data into the inode.
982 * The inode's delta_dcount is adjusted.
986 hammer2_assign_physical(hammer2_inode_t *ip, hammer2_key_t lbase,
987 int lblksize, int *errorp)
989 hammer2_mount_t *hmp;
990 hammer2_chain_t *parent;
991 hammer2_chain_t *chain;
995 * Locate the chain associated with lbase, return a locked chain.
996 * However, do not instantiate any data reference (which utilizes a
997 * device buffer) because we will be using direct IO via the
998 * logical buffer cache buffer.
1003 parent = hammer2_inode_lock_ex(ip);
1004 chain = hammer2_chain_lookup(hmp, &parent,
1006 HAMMER2_LOOKUP_NODATA);
1008 if (chain == NULL) {
1010 * We found a hole, create a new chain entry.
1012 * NOTE: DATA chains are created without device backing
1013 * store (nor do we want any).
1015 chain = hammer2_chain_create(hmp, parent, NULL,
1016 lbase, HAMMER2_PBUFRADIX,
1017 HAMMER2_BREF_TYPE_DATA,
1019 if (chain == NULL) {
1020 KKASSERT(*errorp == EAGAIN); /* XXX */
1021 hammer2_inode_unlock_ex(ip, parent);
1025 pbase = chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX;
1026 /*ip->delta_dcount += lblksize;*/
1028 switch (chain->bref.type) {
1029 case HAMMER2_BREF_TYPE_INODE:
1031 * The data is embedded in the inode. The
1032 * caller is responsible for marking the inode
1033 * modified and copying the data to the embedded
1038 case HAMMER2_BREF_TYPE_DATA:
1039 if (chain->bytes != lblksize) {
1040 panic("hammer2_assign_physical: "
1041 "size mismatch %d/%d\n",
1042 lblksize, chain->bytes);
1044 hammer2_chain_modify(hmp, chain,
1045 HAMMER2_MODIFY_OPTDATA);
1046 pbase = chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX;
1049 panic("hammer2_assign_physical: bad type");
1057 hammer2_chain_unlock(hmp, chain);
1058 hammer2_inode_unlock_ex(ip, parent);
1064 * Truncate the size of a file.
1066 * This routine adjusts ipdata->size smaller, destroying any related
1067 * data beyond the new EOF and potentially resizing the block straddling
1070 * The inode must be locked.
1074 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1076 hammer2_inode_data_t *ipdata;
1077 hammer2_chain_t *parent;
1078 hammer2_chain_t *chain;
1079 hammer2_mount_t *hmp = ip->hmp;
1080 hammer2_key_t lbase;
1088 hammer2_chain_modify(hmp, ip->chain, 0);
1090 ipdata = &ip->chain->data->ipdata;
1093 * Destroy any logical buffer cache buffers beyond the file EOF.
1095 * We call nvtruncbuf() w/ trivial == 1 to prevent it from messing
1096 * around with the buffer straddling EOF, because we need to assign
1097 * a new physical offset to it.
1100 nvtruncbuf(ip->vp, nsize,
1101 HAMMER2_PBUFSIZE, (int)nsize & HAMMER2_PBUFMASK,
1106 * Setup for lookup/search
1109 error = hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
1111 hammer2_chain_unlock(hmp, parent);
1112 /* XXX error reporting */
1117 * Handle the case where a chain/logical-buffer straddles the new
1118 * EOF. We told nvtruncbuf() above not to mess with the logical
1119 * buffer straddling the EOF because we need to reassign its storage
1120 * and can't let the strategy code do it for us.
1122 loff = (int)nsize & HAMMER2_PBUFMASK;
1123 if (loff && ip->vp) {
1124 oblksize = hammer2_calc_logical(ip, nsize, &lbase, &leof);
1125 error = bread(ip->vp, lbase, oblksize, &bp);
1126 KKASSERT(error == 0);
1128 ipdata->size = nsize;
1129 nblksize = hammer2_calc_logical(ip, nsize, &lbase, &leof);
1132 * Fixup the chain element. If we have a logical buffer in-hand
1133 * we don't want to create a conflicting device buffer.
1136 chain = hammer2_chain_lookup(hmp, &parent, lbase, lbase,
1137 HAMMER2_LOOKUP_NODATA);
1139 allocbuf(bp, nblksize);
1140 switch(chain->bref.type) {
1141 case HAMMER2_BREF_TYPE_DATA:
1142 hammer2_chain_resize(ip, chain,
1143 hammer2_allocsize(nblksize),
1144 HAMMER2_MODIFY_OPTDATA);
1145 bzero(bp->b_data + loff, nblksize - loff);
1146 bp->b_bio2.bio_offset = chain->bref.data_off &
1149 case HAMMER2_BREF_TYPE_INODE:
1150 bzero(bp->b_data + loff, nblksize - loff);
1151 bp->b_bio2.bio_offset = NOOFFSET;
1154 panic("hammer2_truncate_file: bad type");
1157 hammer2_chain_unlock(hmp, chain);
1158 if (bp->b_bcount == HAMMER2_PBUFSIZE)
1159 bp->b_flags |= B_CLUSTEROK;
1163 * Destroy clean buffer w/ wrong buffer size. Retain
1166 bp->b_flags |= B_RELBUF;
1167 KKASSERT(bp->b_bio2.bio_offset == NOOFFSET);
1168 KKASSERT((bp->b_flags & B_DIRTY) == 0);
1173 * WARNING: This utilizes a device buffer for the data.
1175 * This case should not occur because file truncations without
1176 * a vnode (and hence no logical buffer cache) should only
1177 * always truncate to 0-length.
1179 panic("hammer2_truncate_file: non-zero truncation, no-vnode");
1181 chain = hammer2_chain_lookup(hmp, &parent, lbase, lbase, 0);
1183 switch(chain->bref.type) {
1184 case HAMMER2_BREF_TYPE_DATA:
1185 hammer2_chain_resize(ip, chain,
1186 hammer2_allocsize(nblksize),
1188 hammer2_chain_modify(hmp, chain, 0);
1189 bzero(chain->data->buf + loff, nblksize - loff);
1191 case HAMMER2_BREF_TYPE_INODE:
1192 if (loff < HAMMER2_EMBEDDED_BYTES) {
1193 hammer2_chain_modify(hmp, chain, 0);
1194 bzero(chain->data->ipdata.u.data + loff,
1195 HAMMER2_EMBEDDED_BYTES - loff);
1199 hammer2_chain_unlock(hmp, chain);
1205 * Clean up any fragmentory VM pages now that we have properly
1206 * resized the straddling buffer. These pages are no longer
1207 * part of the buffer.
1210 nvtruncbuf(ip->vp, nsize,
1211 nblksize, (int)nsize & (nblksize - 1),
1216 * Destroy any physical blocks after the new EOF point.
1218 lbase = (nsize + HAMMER2_PBUFMASK64) & ~HAMMER2_PBUFMASK64;
1219 chain = hammer2_chain_lookup(hmp, &parent,
1220 lbase, (hammer2_key_t)-1,
1221 HAMMER2_LOOKUP_NODATA);
1224 * Degenerate embedded data case, nothing to loop on.
1226 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
1227 hammer2_chain_unlock(hmp, chain);
1232 * Delete physical data blocks past the file EOF.
1234 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
1235 /*ip->delta_dcount -= chain->bytes;*/
1236 hammer2_chain_delete(hmp, parent, chain, 0);
1238 /* XXX check parent if empty indirect block & delete */
1239 chain = hammer2_chain_next(hmp, &parent, chain,
1240 lbase, (hammer2_key_t)-1,
1241 HAMMER2_LOOKUP_NODATA);
1243 hammer2_chain_unlock(hmp, parent);
1247 * Extend the size of a file. The inode must be locked.
1249 * We may have to resize the block straddling the old EOF.
1253 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1255 hammer2_inode_data_t *ipdata;
1256 hammer2_mount_t *hmp;
1257 hammer2_chain_t *parent;
1258 hammer2_chain_t *chain;
1260 hammer2_key_t osize;
1261 hammer2_key_t obase;
1262 hammer2_key_t nbase;
1272 hammer2_chain_modify(hmp, ip->chain, 0);
1273 ipdata = &ip->chain->data->ipdata;
1276 * Nothing to do if the direct-data case is still intact
1278 if ((ipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
1279 nsize <= HAMMER2_EMBEDDED_BYTES) {
1280 ipdata->size = nsize;
1282 ipdata->size, nsize,
1283 0, HAMMER2_EMBEDDED_BYTES,
1290 * Calculate the blocksize at the original EOF and resize the block
1291 * if necessary. Adjust the file size in the inode.
1293 osize = ipdata->size;
1294 oblksize = hammer2_calc_logical(ip, osize, &obase, &leof);
1295 ipdata->size = nsize;
1296 nblksize = hammer2_calc_logical(ip, osize, &nbase, &leof);
1299 * Do all required vnode operations, but do not mess with the
1300 * buffer straddling the orignal EOF.
1303 ipdata->size, nsize,
1305 0, (int)nsize & HAMMER2_PBUFMASK,
1309 * Early return if we have no more work to do.
1311 if (obase == nbase && oblksize == nblksize &&
1312 (ipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) == 0) {
1317 * We have work to do, including possibly resizing the buffer
1318 * at the previous EOF point and turning off DIRECTDATA mode.
1321 if (((int)osize & HAMMER2_PBUFMASK)) {
1322 error = bread(ip->vp, obase, oblksize, &bp);
1323 KKASSERT(error == 0);
1325 if (obase != nbase) {
1326 if (oblksize != HAMMER2_PBUFSIZE)
1327 allocbuf(bp, HAMMER2_PBUFSIZE);
1329 if (oblksize != nblksize)
1330 allocbuf(bp, nblksize);
1335 * Disable direct-data mode by loading up a buffer cache buffer
1336 * with the data, then converting the inode data area into the
1337 * inode indirect block array area.
1339 if (ipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
1340 ipdata->op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
1341 bzero(&ipdata->u.blockset, sizeof(ipdata->u.blockset));
1345 * Resize the chain element at the old EOF.
1347 if (((int)osize & HAMMER2_PBUFMASK)) {
1350 error = hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
1351 KKASSERT(error == 0);
1353 nradix = hammer2_allocsize(nblksize);
1355 chain = hammer2_chain_lookup(hmp, &parent,
1357 HAMMER2_LOOKUP_NODATA);
1358 if (chain == NULL) {
1359 chain = hammer2_chain_create(hmp, parent, NULL,
1361 HAMMER2_BREF_TYPE_DATA,
1363 if (chain == NULL) {
1364 KKASSERT(error == EAGAIN);
1365 hammer2_chain_unlock(hmp, parent);
1368 /*ip->delta_dcount += nblksize;*/
1370 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA);
1371 hammer2_chain_resize(ip, chain, nradix,
1372 HAMMER2_MODIFY_OPTDATA);
1374 bp->b_bio2.bio_offset = chain->bref.data_off &
1376 hammer2_chain_unlock(hmp, chain);
1377 if (bp->b_bcount == HAMMER2_PBUFSIZE)
1378 bp->b_flags |= B_CLUSTEROK;
1380 hammer2_chain_unlock(hmp, parent);
1386 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1388 hammer2_inode_t *ip;
1389 hammer2_inode_t *dip;
1390 hammer2_mount_t *hmp;
1391 hammer2_chain_t *parent;
1392 hammer2_chain_t *chain;
1393 hammer2_chain_t *ochain;
1394 struct namecache *ncp;
1395 const uint8_t *name;
1401 dip = VTOI(ap->a_dvp);
1403 ncp = ap->a_nch->ncp;
1404 name = ncp->nc_name;
1405 name_len = ncp->nc_nlen;
1406 lhc = hammer2_dirhash(name, name_len);
1409 * Note: In DragonFly the kernel handles '.' and '..'.
1411 parent = hammer2_inode_lock_sh(dip);
1412 chain = hammer2_chain_lookup(hmp, &parent,
1413 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1414 HAMMER2_LOOKUP_SHARED);
1416 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1417 name_len == chain->data->ipdata.name_len &&
1418 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1421 chain = hammer2_chain_next(hmp, &parent, chain,
1422 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1423 HAMMER2_LOOKUP_SHARED);
1425 hammer2_inode_unlock_sh(dip, parent);
1428 * If the inode represents a forwarding entry for a hardlink we have
1429 * to locate the actual inode. The original ip is saved for possible
1430 * deconsolidation. (ip) will only be set to non-NULL when we have
1431 * to locate the real file via a hardlink. ip will be referenced but
1432 * not locked in that situation. chain is passed in locked and
1435 * XXX what kind of chain lock?
1438 if (chain && chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
1439 error = hammer2_hardlink_find(dip, &chain, &ochain);
1441 kprintf("hammer2: unable to find hardlink\n");
1443 hammer2_chain_unlock(hmp, chain);
1451 * Deconsolidate any hardlink whos nlinks == 1. Ignore errors.
1452 * If an error occurs chain and ip are left alone.
1454 * XXX upgrade shared lock?
1456 if (ochain && chain && chain->data->ipdata.nlinks == 1 && !hmp->ronly) {
1457 kprintf("hammer2: need to unconsolidate hardlink for %s\n",
1458 chain->data->ipdata.filename);
1459 /* XXX retain shared lock on dip? (currently not held) */
1460 hammer2_hardlink_deconsolidate(dip, &chain, &ochain);
1464 * Acquire the related vnode
1466 * NOTE: For error processing, only ENOENT resolves the namecache
1467 * entry to NULL, otherwise we just return the error and
1468 * leave the namecache unresolved.
1470 * NOTE: multiple hammer2_inode structures can be aliased to the
1471 * same chain element, for example for hardlinks. This
1472 * use case does not 'reattach' inode associations that
1473 * might already exist, but always allocates a new one.
1476 ip = hammer2_inode_get(dip->hmp, dip->pmp, dip, chain);
1477 vp = hammer2_igetv(ip, &error);
1480 cache_setvp(ap->a_nch, vp);
1481 } else if (error == ENOENT) {
1482 cache_setvp(ap->a_nch, NULL);
1485 * don't break the API, chain is locked shared so unlock
1486 * it separately even though unlock_ex() currently doesn't
1489 hammer2_inode_unlock_ex(ip, NULL);
1490 hammer2_chain_unlock(hmp, chain);
1493 * The vp should not be released until after we've disposed
1494 * of our locks, because it might cause vop_inactive() to
1501 cache_setvp(ap->a_nch, NULL);
1504 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1505 ("resolve error %d/%p chain %p ap %p\n",
1506 error, ap->a_nch->ncp->nc_vp, chain, ap));
1508 hammer2_chain_drop(hmp, ochain);
1514 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1516 hammer2_chain_t *chain;
1517 hammer2_inode_t *dip;
1518 hammer2_inode_t *ip;
1519 hammer2_mount_t *hmp;
1522 dip = VTOI(ap->a_dvp);
1525 if ((ip = dip->pip) == NULL) {
1529 chain = hammer2_inode_lock_ex(ip);
1530 *ap->a_vpp = hammer2_igetv(ip, &error);
1531 hammer2_inode_unlock_ex(ip, chain);
1538 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1540 hammer2_chain_t *nchain;
1541 hammer2_mount_t *hmp;
1542 hammer2_inode_t *dip;
1543 hammer2_inode_t *nip;
1544 struct namecache *ncp;
1545 const uint8_t *name;
1549 dip = VTOI(ap->a_dvp);
1554 ncp = ap->a_nch->ncp;
1555 name = ncp->nc_name;
1556 name_len = ncp->nc_nlen;
1558 error = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1559 name, name_len, &nip, &nchain);
1561 KKASSERT(nip == NULL);
1565 *ap->a_vpp = hammer2_igetv(nip, &error);
1566 hammer2_inode_unlock_ex(nip, nchain);
1569 cache_setunresolved(ap->a_nch);
1570 cache_setvp(ap->a_nch, *ap->a_vpp);
1576 * Return the largest contiguous physical disk range for the logical
1579 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
1583 hammer2_vop_bmap(struct vop_bmap_args *ap)
1586 hammer2_mount_t *hmp;
1587 hammer2_inode_t *ip;
1588 hammer2_chain_t *parent;
1589 hammer2_chain_t *chain;
1593 hammer2_off_t pbytes;
1594 hammer2_off_t array[HAMMER2_BMAP_COUNT][2];
1599 * Only supported on regular files
1601 * Only supported for read operations (required for cluster_read).
1602 * The block allocation is delayed for write operations.
1605 if (vp->v_type != VREG)
1606 return (EOPNOTSUPP);
1607 if (ap->a_cmd != BUF_CMD_READ)
1608 return (EOPNOTSUPP);
1612 bzero(array, sizeof(array));
1615 * Calculate logical range
1617 KKASSERT((ap->a_loffset & HAMMER2_LBUFMASK64) == 0);
1618 lbeg = ap->a_loffset & HAMMER2_OFF_MASK_HI;
1619 lend = lbeg + HAMMER2_BMAP_COUNT * HAMMER2_PBUFSIZE - 1;
1622 loff = ap->a_loffset & HAMMER2_OFF_MASK_LO;
1624 parent = hammer2_inode_lock_sh(ip);
1625 chain = hammer2_chain_lookup(hmp, &parent,
1627 HAMMER2_LOOKUP_NODATA |
1628 HAMMER2_LOOKUP_SHARED);
1629 if (chain == NULL) {
1630 *ap->a_doffsetp = ZFOFFSET;
1631 hammer2_inode_unlock_sh(ip, parent);
1636 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
1637 ai = (chain->bref.key - lbeg) / HAMMER2_PBUFSIZE;
1638 KKASSERT(ai >= 0 && ai < HAMMER2_BMAP_COUNT);
1639 array[ai][0] = chain->bref.data_off & HAMMER2_OFF_MASK;
1640 array[ai][1] = chain->bytes;
1642 chain = hammer2_chain_next(hmp, &parent, chain,
1644 HAMMER2_LOOKUP_NODATA |
1645 HAMMER2_LOOKUP_SHARED);
1647 hammer2_inode_unlock_sh(ip, parent);
1650 * If the requested loffset is not mappable physically we can't
1651 * bmap. The caller will have to access the file data via a
1654 if (array[0][0] == 0 || array[0][1] < loff + HAMMER2_LBUFSIZE) {
1655 *ap->a_doffsetp = NOOFFSET;
1660 * Calculate the physical disk offset range for array[0]
1662 pbeg = array[0][0] + loff;
1663 pbytes = array[0][1] - loff;
1665 for (ai = 1; ai < HAMMER2_BMAP_COUNT; ++ai) {
1666 if (array[ai][0] != pbeg + pbytes)
1668 pbytes += array[ai][1];
1671 *ap->a_doffsetp = pbeg;
1673 *ap->a_runp = pbytes;
1679 hammer2_vop_open(struct vop_open_args *ap)
1681 return vop_stdopen(ap);
1685 * hammer2_vop_advlock { vp, id, op, fl, flags }
1689 hammer2_vop_advlock(struct vop_advlock_args *ap)
1691 hammer2_inode_t *ip = VTOI(ap->a_vp);
1692 hammer2_chain_t *chain;
1695 chain = hammer2_inode_lock_sh(ip);
1696 size = chain->data->ipdata.size;
1697 hammer2_inode_unlock_sh(ip, chain);
1698 return (lf_advlock(ap, &ip->advlock, size));
1704 hammer2_vop_close(struct vop_close_args *ap)
1706 return vop_stdclose(ap);
1710 * hammer2_vop_nlink { nch, dvp, vp, cred }
1712 * Create a hardlink from (vp) to {dvp, nch}.
1716 hammer2_vop_nlink(struct vop_nlink_args *ap)
1718 hammer2_inode_t *dip; /* target directory to create link in */
1719 hammer2_inode_t *ip; /* inode we are hardlinking to */
1720 hammer2_mount_t *hmp;
1721 hammer2_chain_t *chain;
1722 struct namecache *ncp;
1723 const uint8_t *name;
1727 dip = VTOI(ap->a_dvp);
1732 ncp = ap->a_nch->ncp;
1733 name = ncp->nc_name;
1734 name_len = ncp->nc_nlen;
1737 * ip represents the file being hardlinked. The file could be a
1738 * normal file or a hardlink target if it has already been hardlinked.
1739 * If ip is a hardlinked target then ip->pip represents the location
1740 * of the hardlinked target, NOT the location of the hardlink pointer.
1742 * Bump nlinks and potentially also create or move the hardlink
1743 * target in the parent directory common to (ip) and (dip). The
1744 * consolidation code can modify ip->chain and ip->pip. The
1745 * returned chain is locked.
1747 ip = VTOI(ap->a_vp);
1748 hammer2_inode_ref(ip);
1749 error = hammer2_hardlink_consolidate(ip, &chain, dip, 1);
1754 * Create a directory entry connected to the specified chain.
1755 * This function unlocks and NULL's chain on return.
1757 error = hammer2_inode_connect(dip, &chain, name, name_len);
1759 cache_setunresolved(ap->a_nch);
1760 cache_setvp(ap->a_nch, ap->a_vp);
1763 hammer2_inode_drop(ip);
1768 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1770 * The operating system has already ensured that the directory entry
1771 * does not exist and done all appropriate namespace locking.
1775 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1777 hammer2_mount_t *hmp;
1778 hammer2_inode_t *dip;
1779 hammer2_inode_t *nip;
1780 hammer2_chain_t *nchain;
1781 struct namecache *ncp;
1782 const uint8_t *name;
1786 dip = VTOI(ap->a_dvp);
1791 ncp = ap->a_nch->ncp;
1792 name = ncp->nc_name;
1793 name_len = ncp->nc_nlen;
1795 error = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1796 name, name_len, &nip, &nchain);
1798 KKASSERT(nip == NULL);
1802 *ap->a_vpp = hammer2_igetv(nip, &error);
1803 hammer2_inode_unlock_ex(nip, nchain);
1806 cache_setunresolved(ap->a_nch);
1807 cache_setvp(ap->a_nch, *ap->a_vpp);
1813 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1817 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1819 hammer2_mount_t *hmp;
1820 hammer2_inode_t *dip;
1821 hammer2_inode_t *nip;
1822 hammer2_chain_t *nchain;
1823 struct namecache *ncp;
1824 const uint8_t *name;
1828 dip = VTOI(ap->a_dvp);
1833 ncp = ap->a_nch->ncp;
1834 name = ncp->nc_name;
1835 name_len = ncp->nc_nlen;
1837 ap->a_vap->va_type = VLNK; /* enforce type */
1839 error = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1840 name, name_len, &nip, &nchain);
1842 KKASSERT(nip == NULL);
1846 *ap->a_vpp = hammer2_igetv(nip, &error);
1849 * Build the softlink (~like file data) and finalize the namecache.
1855 hammer2_inode_data_t *nipdata;
1857 nipdata = &nchain->data->ipdata;
1858 bytes = strlen(ap->a_target);
1860 if (bytes <= HAMMER2_EMBEDDED_BYTES) {
1861 KKASSERT(nipdata->op_flags &
1862 HAMMER2_OPFLAG_DIRECTDATA);
1863 bcopy(ap->a_target, nipdata->u.data, bytes);
1864 nipdata->size = bytes;
1866 bzero(&auio, sizeof(auio));
1867 bzero(&aiov, sizeof(aiov));
1868 auio.uio_iov = &aiov;
1869 auio.uio_segflg = UIO_SYSSPACE;
1870 auio.uio_rw = UIO_WRITE;
1871 auio.uio_resid = bytes;
1872 auio.uio_iovcnt = 1;
1873 auio.uio_td = curthread;
1874 aiov.iov_base = ap->a_target;
1875 aiov.iov_len = bytes;
1876 error = hammer2_write_file(nip, &nchain,
1877 &auio, IO_APPEND, 0);
1878 /* XXX handle error */
1882 hammer2_inode_unlock_ex(nip, nchain);
1885 * Finalize namecache
1888 cache_setunresolved(ap->a_nch);
1889 cache_setvp(ap->a_nch, *ap->a_vpp);
1890 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1896 * hammer2_vop_nremove { nch, dvp, cred }
1900 hammer2_vop_nremove(struct vop_nremove_args *ap)
1902 hammer2_inode_t *dip;
1903 hammer2_mount_t *hmp;
1904 struct namecache *ncp;
1905 const uint8_t *name;
1909 dip = VTOI(ap->a_dvp);
1914 ncp = ap->a_nch->ncp;
1915 name = ncp->nc_name;
1916 name_len = ncp->nc_nlen;
1918 error = hammer2_unlink_file(dip, name, name_len, 0, NULL);
1920 cache_unlink(ap->a_nch);
1926 * hammer2_vop_nrmdir { nch, dvp, cred }
1930 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1932 hammer2_inode_t *dip;
1933 hammer2_mount_t *hmp;
1934 struct namecache *ncp;
1935 const uint8_t *name;
1939 dip = VTOI(ap->a_dvp);
1944 ncp = ap->a_nch->ncp;
1945 name = ncp->nc_name;
1946 name_len = ncp->nc_nlen;
1948 error = hammer2_unlink_file(dip, name, name_len, 1, NULL);
1950 cache_unlink(ap->a_nch);
1956 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1960 hammer2_vop_nrename(struct vop_nrename_args *ap)
1962 struct namecache *fncp;
1963 struct namecache *tncp;
1964 hammer2_inode_t *fdip;
1965 hammer2_inode_t *tdip;
1966 hammer2_inode_t *ip;
1967 hammer2_chain_t *chain;
1968 hammer2_mount_t *hmp;
1969 const uint8_t *fname;
1971 const uint8_t *tname;
1975 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1977 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1980 fdip = VTOI(ap->a_fdvp); /* source directory */
1981 tdip = VTOI(ap->a_tdvp); /* target directory */
1983 hmp = fdip->hmp; /* check read-only filesystem */
1987 fncp = ap->a_fnch->ncp; /* entry name in source */
1988 fname = fncp->nc_name;
1989 fname_len = fncp->nc_nlen;
1991 tncp = ap->a_tnch->ncp; /* entry name in target */
1992 tname = tncp->nc_name;
1993 tname_len = tncp->nc_nlen;
1996 * ip is the inode being removed. If this is a hardlink then
1997 * ip represents the actual file and not the hardlink marker.
1999 ip = VTOI(fncp->nc_vp);
2003 * Keep a tight grip on the inode so the temporary unlinking from
2004 * the source location prior to linking to the target location
2005 * does not cause the chain to be destroyed.
2007 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
2008 * unlinking elements from their directories. Locking
2009 * the nlinks field does not lock the whole inode.
2011 hammer2_inode_ref(ip);
2014 * Remove target if it exists
2016 error = hammer2_unlink_file(tdip, tname, tname_len, -1, NULL);
2017 if (error && error != ENOENT)
2019 cache_setunresolved(ap->a_tnch);
2022 * When renaming a hardlinked file we may have to re-consolidate
2023 * the location of the hardlink target. Since the element is simply
2024 * being moved, nlinks is not modified in this case.
2026 * If ip represents a regular file the consolidation code essentially
2027 * does nothing other than return the locked chain.
2029 * The returned chain will be locked.
2031 error = hammer2_hardlink_consolidate(ip, &chain, tdip, 0);
2036 * Disconnect (fdip, fname) from the source directory. This will
2037 * disconnect (ip) if it represents a direct file. If (ip) represents
2038 * a hardlink the HARDLINK pointer object will be removed but the
2039 * hardlink will stay intact.
2041 * NOTE! Because we are retaining (ip) the unlink can fail with
2045 error = hammer2_unlink_file(fdip, fname, fname_len, -1, chain);
2046 if (error != EAGAIN)
2048 kprintf("hammer2_vop_nrename: unlink race %s\n", fname);
2049 tsleep(fdip, 0, "h2renr", 1);
2055 * Reconnect ip to target directory.
2057 * WARNING: chain locks can lock buffer cache buffers, to avoid
2058 * deadlocks we want to unlock before issuing a cache_*()
2059 * op (that might have to lock a vnode). The *_connect()
2060 * function does this for us.
2062 error = hammer2_inode_connect(tdip, &chain, tname, tname_len);
2064 cache_rename(ap->a_fnch, ap->a_tnch);
2067 hammer2_chain_unlock(hmp, chain);
2068 hammer2_inode_drop(ip);
2073 static int hammer2_strategy_read(struct vop_strategy_args *ap);
2074 static int hammer2_strategy_write(struct vop_strategy_args *ap);
2078 hammer2_vop_strategy(struct vop_strategy_args *ap)
2089 error = hammer2_strategy_read(ap);
2090 ++hammer2_iod_file_read;
2093 error = hammer2_strategy_write(ap);
2094 ++hammer2_iod_file_write;
2097 bp->b_error = error = EINVAL;
2098 bp->b_flags |= B_ERROR;
2108 hammer2_strategy_read(struct vop_strategy_args *ap)
2113 hammer2_mount_t *hmp;
2114 hammer2_inode_t *ip;
2115 hammer2_chain_t *parent;
2116 hammer2_chain_t *chain;
2117 hammer2_key_t lbase;
2121 ip = VTOI(ap->a_vp);
2123 nbio = push_bio(bio);
2125 lbase = bio->bio_offset;
2127 KKASSERT(((int)lbase & HAMMER2_PBUFMASK) == 0);
2130 * We must characterize the logical->physical translation if it
2131 * has not already been cached.
2133 * Physical data references < LBUFSIZE are never cached. This
2134 * includes both small-block allocations and inode-embedded data.
2136 if (nbio->bio_offset == NOOFFSET) {
2137 parent = hammer2_inode_lock_sh(ip);
2139 chain = hammer2_chain_lookup(hmp, &parent, lbase, lbase,
2140 HAMMER2_LOOKUP_NODATA |
2141 HAMMER2_LOOKUP_SHARED);
2142 if (chain == NULL) {
2146 nbio->bio_offset = ZFOFFSET;
2147 } else if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
2149 * Data is embedded in the inode (do nothing)
2151 KKASSERT(chain == parent);
2152 hammer2_chain_unlock(hmp, chain);
2153 } else if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2157 KKASSERT(bp->b_bcount == chain->bytes);
2158 nbio->bio_offset = chain->bref.data_off &
2160 hammer2_chain_unlock(hmp, chain);
2161 KKASSERT(nbio->bio_offset != 0);
2163 panic("hammer2_strategy_read: unknown bref type");
2165 hammer2_inode_unlock_sh(ip, parent);
2168 if (hammer2_debug & 0x0020) {
2169 kprintf("read %016jx %016jx\n",
2170 bio->bio_offset, nbio->bio_offset);
2173 if (nbio->bio_offset == ZFOFFSET) {
2179 bzero(bp->b_data, bp->b_bcount);
2181 } else if (nbio->bio_offset != NOOFFSET) {
2183 * Forward direct IO to the device
2185 vn_strategy(hmp->devvp, nbio);
2188 * Data is embedded in inode.
2190 bcopy(chain->data->ipdata.u.data, bp->b_data,
2191 HAMMER2_EMBEDDED_BYTES);
2192 bzero(bp->b_data + HAMMER2_EMBEDDED_BYTES,
2193 bp->b_bcount - HAMMER2_EMBEDDED_BYTES);
2203 hammer2_strategy_write(struct vop_strategy_args *ap)
2208 hammer2_mount_t *hmp;
2209 hammer2_inode_t *ip;
2213 ip = VTOI(ap->a_vp);
2215 nbio = push_bio(bio);
2217 KKASSERT((bio->bio_offset & HAMMER2_PBUFMASK64) == 0);
2218 KKASSERT(nbio->bio_offset != 0 && nbio->bio_offset != ZFOFFSET);
2220 if (nbio->bio_offset == NOOFFSET) {
2222 * Must be embedded in the inode.
2224 * Because the inode is dirty, the chain must exist whether
2225 * the inode is locked or not. XXX
2227 KKASSERT(bio->bio_offset == 0);
2228 KKASSERT(ip->chain && ip->chain->data);
2229 bcopy(bp->b_data, ip->chain->data->ipdata.u.data,
2230 HAMMER2_EMBEDDED_BYTES);
2236 * This special flag does not follow the normal MODIFY rules
2237 * because we might deadlock on ip. Instead we depend on
2238 * VOP_FSYNC() to detect the case.
2240 atomic_set_int(&ip->flags, HAMMER2_INODE_DIRTYEMBED);
2243 * Forward direct IO to the device
2245 vn_strategy(hmp->devvp, nbio);
2251 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2255 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2257 hammer2_mount_t *hmp;
2258 hammer2_inode_t *ip;
2261 ip = VTOI(ap->a_vp);
2264 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2265 ap->a_fflag, ap->a_cred);
2271 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
2274 hammer2_pfsmount_t *pmp;
2278 case (MOUNTCTL_SET_EXPORT):
2279 mp = ap->a_head.a_ops->head.vv_mount;
2282 if (ap->a_ctllen != sizeof(struct export_args))
2285 rc = vfs_export(mp, &pmp->export,
2286 (const struct export_args *)ap->a_ctl);
2289 rc = vop_stdmountctl(ap);
2295 struct vop_ops hammer2_vnode_vops = {
2296 .vop_default = vop_defaultop,
2297 .vop_fsync = hammer2_vop_fsync,
2298 .vop_getpages = vop_stdgetpages,
2299 .vop_putpages = vop_stdputpages,
2300 .vop_access = hammer2_vop_access,
2301 .vop_advlock = hammer2_vop_advlock,
2302 .vop_close = hammer2_vop_close,
2303 .vop_nlink = hammer2_vop_nlink,
2304 .vop_ncreate = hammer2_vop_ncreate,
2305 .vop_nsymlink = hammer2_vop_nsymlink,
2306 .vop_nremove = hammer2_vop_nremove,
2307 .vop_nrmdir = hammer2_vop_nrmdir,
2308 .vop_nrename = hammer2_vop_nrename,
2309 .vop_getattr = hammer2_vop_getattr,
2310 .vop_setattr = hammer2_vop_setattr,
2311 .vop_readdir = hammer2_vop_readdir,
2312 .vop_readlink = hammer2_vop_readlink,
2313 .vop_getpages = vop_stdgetpages,
2314 .vop_putpages = vop_stdputpages,
2315 .vop_read = hammer2_vop_read,
2316 .vop_write = hammer2_vop_write,
2317 .vop_open = hammer2_vop_open,
2318 .vop_inactive = hammer2_vop_inactive,
2319 .vop_reclaim = hammer2_vop_reclaim,
2320 .vop_nresolve = hammer2_vop_nresolve,
2321 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2322 .vop_nmkdir = hammer2_vop_nmkdir,
2323 .vop_ioctl = hammer2_vop_ioctl,
2324 .vop_mountctl = hammer2_vop_mountctl,
2325 .vop_bmap = hammer2_vop_bmap,
2326 .vop_strategy = hammer2_vop_strategy,
2329 struct vop_ops hammer2_spec_vops = {
2333 struct vop_ops hammer2_fifo_vops = {