2 * Copyright (c) 2011-2015 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>
62 static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio,
64 static int hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
65 int ioflag, int seqcount);
66 static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize);
67 static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize);
69 struct objcache *cache_xops;
73 hammer2_knote(struct vnode *vp, int flags)
76 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
80 * Last reference to a vnode is going away but it is still cached.
84 hammer2_vop_inactive(struct vop_inactive_args *ap)
103 * Check for deleted inodes and recycle immediately on the last
104 * release. Be sure to destroy any left-over buffer cache buffers
105 * so we do not waste time trying to flush them.
107 * WARNING: nvtruncbuf() can only be safely called without the inode
108 * lock held due to the way our write thread works.
110 if (ip->flags & HAMMER2_INODE_ISUNLINKED) {
115 * Detect updates to the embedded data which may be
116 * synchronized by the strategy code. Simply mark the
117 * inode modified so it gets picked up by our normal flush.
119 nblksize = hammer2_calc_logical(ip, 0, &lbase, NULL);
120 nvtruncbuf(vp, 0, nblksize, 0, 0);
128 * Reclaim a vnode so that it can be reused; after the inode is
129 * disassociated, the filesystem must manage it alone.
133 hammer2_vop_reclaim(struct vop_reclaim_args *ap)
149 * The final close of a deleted file or directory marks it for
150 * destruction. The DELETED flag allows the flusher to shortcut
151 * any modified blocks still unflushed (that is, just ignore them).
153 * HAMMER2 usually does not try to optimize the freemap by returning
154 * deleted blocks to it as it does not usually know how many snapshots
155 * might be referencing portions of the file/dir.
161 * NOTE! We do not attempt to flush chains here, flushing is
162 * really fragile and could also deadlock.
167 * Once reclaimed the inode is disconnected from the normal flush
168 * mechanism and must be tracked
170 * A reclaim can occur at any time so we cannot safely start a
171 * transaction to handle reclamation of unlinked files. Instead,
172 * the ip is left with a reference and placed on a linked list and
175 if (ip->flags & HAMMER2_INODE_ISUNLINKED) {
176 hammer2_inode_unlink_t *ipul;
178 ipul = kmalloc(sizeof(*ipul), pmp->minode, M_WAITOK | M_ZERO);
181 hammer2_spin_ex(&pmp->list_spin);
182 TAILQ_INSERT_TAIL(&pmp->unlinkq, ipul, entry);
183 hammer2_spin_unex(&pmp->list_spin);
184 /* retain ref from vp for ipul */
186 hammer2_inode_drop(ip); /* vp ref */
190 * XXX handle background sync when ip dirty, kernel will no longer
191 * notify us regarding this inode because there is no longer a
192 * vnode attached to it.
201 hammer2_vop_fsync(struct vop_fsync_args *ap)
211 /* XXX can't do this yet */
212 hammer2_trans_init(ip->pmp, HAMMER2_TRANS_ISFLUSH);
213 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
215 hammer2_trans_init(ip->pmp, 0);
216 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
219 * Calling chain_flush here creates a lot of duplicative
220 * COW operations due to non-optimal vnode ordering.
222 * Only do it for an actual fsync() syscall. The other forms
223 * which call this function will eventually call chain_flush
224 * on the volume root as a catch-all, which is far more optimal.
226 hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
227 if (ip->flags & HAMMER2_INODE_MODIFIED)
228 hammer2_inode_fsync(ip, NULL);
229 hammer2_inode_unlock(ip, NULL);
230 hammer2_trans_done(ip->pmp);
238 hammer2_vop_access(struct vop_access_args *ap)
240 hammer2_inode_t *ip = VTOI(ap->a_vp);
246 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
247 uid = hammer2_to_unix_xid(&ip->meta.uid);
248 gid = hammer2_to_unix_xid(&ip->meta.gid);
249 error = vop_helper_access(ap, uid, gid, ip->meta.mode, ip->meta.uflags);
250 hammer2_inode_unlock(ip, NULL);
258 hammer2_vop_getattr(struct vop_getattr_args *ap)
272 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
274 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
275 vap->va_fileid = ip->meta.inum;
276 vap->va_mode = ip->meta.mode;
277 vap->va_nlink = ip->meta.nlinks;
278 vap->va_uid = hammer2_to_unix_xid(&ip->meta.uid);
279 vap->va_gid = hammer2_to_unix_xid(&ip->meta.gid);
282 vap->va_size = ip->meta.size; /* protected by shared lock */
283 vap->va_blocksize = HAMMER2_PBUFSIZE;
284 vap->va_flags = ip->meta.uflags;
285 hammer2_time_to_timespec(ip->meta.ctime, &vap->va_ctime);
286 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_mtime);
287 hammer2_time_to_timespec(ip->meta.mtime, &vap->va_atime);
289 vap->va_bytes = ip->bref.data_count;
290 vap->va_type = hammer2_get_vtype(ip->meta.type);
292 vap->va_uid_uuid = ip->meta.uid;
293 vap->va_gid_uuid = ip->meta.gid;
294 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
297 hammer2_inode_unlock(ip, NULL);
305 hammer2_vop_setattr(struct vop_setattr_args *ap)
317 hammer2_update_time(&ctime);
321 if (ip->pmp->ronly) {
326 hammer2_pfs_memory_wait(ip->pmp);
327 hammer2_trans_init(ip->pmp, 0);
328 hammer2_inode_lock(ip, 0);
331 if (vap->va_flags != VNOVAL) {
334 flags = ip->meta.uflags;
335 error = vop_helper_setattr_flags(&flags, vap->va_flags,
336 hammer2_to_unix_xid(&ip->meta.uid),
339 if (ip->meta.uflags != flags) {
340 hammer2_inode_modify(ip);
341 ip->meta.uflags = flags;
342 ip->meta.ctime = ctime;
343 kflags |= NOTE_ATTRIB;
345 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
352 if (ip->meta.uflags & (IMMUTABLE | APPEND)) {
356 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
357 mode_t cur_mode = ip->meta.mode;
358 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
359 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
363 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
365 &cur_uid, &cur_gid, &cur_mode);
367 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
368 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
369 if (bcmp(&uuid_uid, &ip->meta.uid, sizeof(uuid_uid)) ||
370 bcmp(&uuid_gid, &ip->meta.gid, sizeof(uuid_gid)) ||
371 ip->meta.mode != cur_mode
373 hammer2_inode_modify(ip);
374 ip->meta.uid = uuid_uid;
375 ip->meta.gid = uuid_gid;
376 ip->meta.mode = cur_mode;
377 ip->meta.ctime = ctime;
379 kflags |= NOTE_ATTRIB;
386 if (vap->va_size != VNOVAL && ip->meta.size != vap->va_size) {
389 if (vap->va_size == ip->meta.size)
391 if (vap->va_size < ip->meta.size) {
392 hammer2_truncate_file(ip, vap->va_size);
394 hammer2_extend_file(ip, vap->va_size);
396 hammer2_inode_modify(ip);
397 ip->meta.mtime = ctime;
405 /* atime not supported */
406 if (vap->va_atime.tv_sec != VNOVAL) {
407 hammer2_inode_modify(ip);
408 ip->meta.atime = hammer2_timespec_to_time(&vap->va_atime);
409 kflags |= NOTE_ATTRIB;
412 if (vap->va_mode != (mode_t)VNOVAL) {
413 mode_t cur_mode = ip->meta.mode;
414 uid_t cur_uid = hammer2_to_unix_xid(&ip->meta.uid);
415 gid_t cur_gid = hammer2_to_unix_xid(&ip->meta.gid);
417 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
418 cur_uid, cur_gid, &cur_mode);
419 if (error == 0 && ip->meta.mode != cur_mode) {
420 hammer2_inode_modify(ip);
421 ip->meta.mode = cur_mode;
422 ip->meta.ctime = ctime;
423 kflags |= NOTE_ATTRIB;
427 if (vap->va_mtime.tv_sec != VNOVAL) {
428 hammer2_inode_modify(ip);
429 ip->meta.mtime = hammer2_timespec_to_time(&vap->va_mtime);
430 kflags |= NOTE_ATTRIB;
435 * If a truncation occurred we must call inode_fsync() now in order
436 * to trim the related data chains, otherwise a later expansion can
439 * If an extend occured that changed the DIRECTDATA state, we must
440 * call inode_fsync now in order to prepare the inode's indirect
443 if (ip->flags & HAMMER2_INODE_RESIZED)
444 hammer2_inode_fsync(ip, NULL);
449 hammer2_inode_unlock(ip, NULL);
450 hammer2_trans_done(ip->pmp);
451 hammer2_knote(ip->vp, kflags);
459 hammer2_vop_readdir(struct vop_readdir_args *ap)
461 hammer2_xop_readdir_t *xop;
462 hammer2_blockref_t bref;
479 saveoff = uio->uio_offset;
484 * Setup cookies directory entry cookies if requested
486 if (ap->a_ncookies) {
487 ncookies = uio->uio_resid / 16 + 1;
490 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
497 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
500 * Handle artificial entries. To ensure that only positive 64 bit
501 * quantities are returned to userland we always strip off bit 63.
502 * The hash code is designed such that codes 0x0000-0x7FFF are not
503 * used, allowing us to use these codes for articial entries.
505 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
506 * allow '..' to cross the mount point into (e.g.) the super-root.
509 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
510 r = vop_write_dirent(&error, uio, inum, DT_DIR, 1, ".");
514 cookies[cookie_index] = saveoff;
517 if (cookie_index == ncookies)
523 * Be careful with lockorder when accessing ".."
525 * (ip is the current dir. xip is the parent dir).
527 inum = ip->meta.inum & HAMMER2_DIRHASH_USERMSK;
528 if (ip->pip && ip != ip->pmp->iroot)
529 inum = ip->pip->meta.inum & HAMMER2_DIRHASH_USERMSK;
530 r = vop_write_dirent(&error, uio, inum, DT_DIR, 2, "..");
534 cookies[cookie_index] = saveoff;
537 if (cookie_index == ncookies)
541 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
542 if (hammer2_debug & 0x0020)
543 kprintf("readdir: lkey %016jx\n", lkey);
548 * Use XOP for cluster scan.
550 * parent is the inode cluster, already locked for us. Don't
551 * double lock shared locks as this will screw up upgrades.
553 xop = &hammer2_xop_alloc(ip)->xop_readdir;
554 xop->head.lkey = lkey;
555 hammer2_xop_start(&xop->head, hammer2_xop_readdir);
558 const hammer2_inode_data_t *ripdata;
560 error = hammer2_xop_collect(&xop->head, 0);
563 if (cookie_index == ncookies)
565 if (hammer2_debug & 0x0020)
566 kprintf("cluster chain %p %p\n",
567 xop->head.cluster.focus,
568 (xop->head.cluster.focus ?
569 xop->head.cluster.focus->data : (void *)-1));
570 ripdata = &hammer2_cluster_rdata(&xop->head.cluster)->ipdata;
571 hammer2_cluster_bref(&xop->head.cluster, &bref);
572 if (bref.type == HAMMER2_BREF_TYPE_INODE) {
573 dtype = hammer2_get_dtype(ripdata);
574 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
575 r = vop_write_dirent(&error, uio,
577 HAMMER2_DIRHASH_USERMSK,
579 ripdata->meta.name_len,
584 cookies[cookie_index] = saveoff;
587 /* XXX chain error */
588 kprintf("bad chain type readdir %d\n", bref.type);
591 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
592 if (error == ENOENT) {
595 saveoff = (hammer2_key_t)-1;
597 saveoff = bref.key & HAMMER2_DIRHASH_USERMSK;
600 hammer2_inode_unlock(ip, NULL);
602 *ap->a_eofflag = eofflag;
603 if (hammer2_debug & 0x0020)
604 kprintf("readdir: done at %016jx\n", saveoff);
605 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
606 if (error && cookie_index == 0) {
608 kfree(cookies, M_TEMP);
610 *ap->a_cookies = NULL;
614 *ap->a_ncookies = cookie_index;
615 *ap->a_cookies = cookies;
623 * hammer2_vop_readlink { vp, uio, cred }
627 hammer2_vop_readlink(struct vop_readlink_args *ap)
634 if (vp->v_type != VLNK)
638 error = hammer2_read_file(ip, ap->a_uio, 0);
644 hammer2_vop_read(struct vop_read_args *ap)
654 * Read operations supported on this vnode?
657 if (vp->v_type != VREG)
667 seqcount = ap->a_ioflag >> 16;
668 bigread = (uio->uio_resid > 100 * 1024 * 1024);
670 error = hammer2_read_file(ip, uio, seqcount);
676 hammer2_vop_write(struct vop_write_args *ap)
686 * Read operations supported on this vnode?
689 if (vp->v_type != VREG)
698 if (ip->pmp->ronly) {
702 seqcount = ap->a_ioflag >> 16;
705 * Check resource limit
707 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
708 uio->uio_offset + uio->uio_resid >
709 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
710 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
715 * The transaction interlocks against flushes initiations
716 * (note: but will run concurrently with the actual flush).
718 hammer2_trans_init(ip->pmp, 0);
719 error = hammer2_write_file(ip, uio, ap->a_ioflag, seqcount);
720 hammer2_trans_done(ip->pmp);
726 * Perform read operations on a file or symlink given an UNLOCKED
729 * The passed ip is not locked.
733 hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
744 * WARNING! Assumes that the kernel interlocks size changes at the
747 hammer2_mtx_sh(&ip->lock);
748 size = ip->meta.size;
749 hammer2_mtx_unlock(&ip->lock);
751 while (uio->uio_resid > 0 && uio->uio_offset < size) {
758 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
761 error = cluster_read(ip->vp, leof, lbase, lblksize,
762 uio->uio_resid, seqcount * BKVASIZE,
767 loff = (int)(uio->uio_offset - lbase);
769 if (n > uio->uio_resid)
771 if (n > size - uio->uio_offset)
772 n = (int)(size - uio->uio_offset);
773 bp->b_flags |= B_AGE;
774 uiomove((char *)bp->b_data + loff, n, uio);
781 * Write to the file represented by the inode via the logical buffer cache.
782 * The inode may represent a regular file or a symlink.
784 * The inode must not be locked.
788 hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
789 int ioflag, int seqcount)
791 hammer2_key_t old_eof;
792 hammer2_key_t new_eof;
801 * WARNING! Assumes that the kernel interlocks size changes at the
804 hammer2_mtx_ex(&ip->lock);
805 if (ioflag & IO_APPEND)
806 uio->uio_offset = ip->meta.size;
807 old_eof = ip->meta.size;
810 * Extend the file if necessary. If the write fails at some point
811 * we will truncate it back down to cover as much as we were able
814 * Doing this now makes it easier to calculate buffer sizes in
821 if (uio->uio_offset + uio->uio_resid > old_eof) {
822 new_eof = uio->uio_offset + uio->uio_resid;
824 hammer2_extend_file(ip, new_eof);
825 kflags |= NOTE_EXTEND;
829 hammer2_mtx_unlock(&ip->lock);
834 while (uio->uio_resid > 0) {
843 * Don't allow the buffer build to blow out the buffer
846 if ((ioflag & IO_RECURSE) == 0)
847 bwillwrite(HAMMER2_PBUFSIZE);
850 * This nominally tells us how much we can cluster and
851 * what the logical buffer size needs to be. Currently
852 * we don't try to cluster the write and just handle one
855 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
857 loff = (int)(uio->uio_offset - lbase);
859 KKASSERT(lblksize <= 65536);
862 * Calculate bytes to copy this transfer and whether the
863 * copy completely covers the buffer or not.
867 if (n > uio->uio_resid) {
869 if (loff == lbase && uio->uio_offset + n == new_eof)
881 if (uio->uio_segflg == UIO_NOCOPY) {
883 * Issuing a write with the same data backing the
884 * buffer. Instantiate the buffer to collect the
885 * backing vm pages, then read-in any missing bits.
887 * This case is used by vop_stdputpages().
889 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
890 if ((bp->b_flags & B_CACHE) == 0) {
892 error = bread(ip->vp, lbase, lblksize, &bp);
894 } else if (trivial) {
896 * Even though we are entirely overwriting the buffer
897 * we may still have to zero it out to avoid a
898 * mmap/write visibility issue.
900 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
901 if ((bp->b_flags & B_CACHE) == 0)
905 * Partial overwrite, read in any missing bits then
906 * replace the portion being written.
908 * (The strategy code will detect zero-fill physical
909 * blocks for this case).
911 error = bread(ip->vp, lbase, lblksize, &bp);
922 * Ok, copy the data in
924 error = uiomove(bp->b_data + loff, n, uio);
925 kflags |= NOTE_WRITE;
933 * WARNING: Pageout daemon will issue UIO_NOCOPY writes
934 * with IO_SYNC or IO_ASYNC set. These writes
935 * must be handled as the pageout daemon expects.
937 if (ioflag & IO_SYNC) {
939 } else if ((ioflag & IO_DIRECT) && endofblk) {
941 } else if (ioflag & IO_ASYNC) {
949 * Cleanup. If we extended the file EOF but failed to write through
950 * the entire write is a failure and we have to back-up.
952 if (error && new_eof != old_eof) {
953 hammer2_mtx_ex(&ip->lock);
954 hammer2_truncate_file(ip, old_eof);
955 if (ip->flags & HAMMER2_INODE_MODIFIED)
956 hammer2_inode_fsync(ip, NULL);
957 hammer2_mtx_unlock(&ip->lock);
958 } else if (modified) {
959 hammer2_mtx_ex(&ip->lock);
960 hammer2_inode_modify(ip);
961 hammer2_update_time(&ip->meta.mtime);
962 if (ip->flags & HAMMER2_INODE_MODIFIED)
963 hammer2_inode_fsync(ip, NULL);
964 hammer2_mtx_unlock(&ip->lock);
965 hammer2_knote(ip->vp, kflags);
967 hammer2_trans_assert_strategy(ip->pmp);
973 * Truncate the size of a file. The inode must not be locked.
975 * We must unconditionally set HAMMER2_INODE_RESIZED to properly
976 * ensure that any on-media data beyond the new file EOF has been destroyed.
978 * WARNING: nvtruncbuf() can only be safely called without the inode lock
979 * held due to the way our write thread works. If the truncation
980 * occurs in the middle of a buffer, nvtruncbuf() is responsible
981 * for dirtying that buffer and zeroing out trailing bytes.
983 * WARNING! Assumes that the kernel interlocks size changes at the
986 * WARNING! Caller assumes responsibility for removing dead blocks
987 * if INODE_RESIZED is set.
991 hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
997 hammer2_mtx_unlock(&ip->lock);
999 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1000 nvtruncbuf(ip->vp, nsize,
1001 nblksize, (int)nsize & (nblksize - 1),
1004 hammer2_mtx_ex(&ip->lock);
1005 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1006 ip->osize = ip->meta.size;
1007 ip->meta.size = nsize;
1008 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED |
1009 HAMMER2_INODE_RESIZED);
1014 * Extend the size of a file. The inode must not be locked.
1016 * Even though the file size is changing, we do not have to set the
1017 * INODE_RESIZED bit unless the file size crosses the EMBEDDED_BYTES
1018 * boundary. When this occurs a hammer2_inode_fsync() is required
1019 * to prepare the inode cluster's indirect block table.
1021 * WARNING! Assumes that the kernel interlocks size changes at the
1024 * WARNING! Caller assumes responsibility for transitioning out
1025 * of the inode DIRECTDATA mode if INODE_RESIZED is set.
1029 hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1031 hammer2_key_t lbase;
1032 hammer2_key_t osize;
1038 KKASSERT((ip->flags & HAMMER2_INODE_RESIZED) == 0);
1039 osize = ip->meta.size;
1041 ip->meta.size = nsize;
1042 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1044 if (osize <= HAMMER2_EMBEDDED_BYTES && nsize > HAMMER2_EMBEDDED_BYTES)
1045 atomic_set_int(&ip->flags, HAMMER2_INODE_RESIZED);
1047 hammer2_mtx_unlock(&ip->lock);
1049 oblksize = hammer2_calc_logical(ip, osize, &lbase, NULL);
1050 nblksize = hammer2_calc_logical(ip, nsize, &lbase, NULL);
1056 hammer2_mtx_ex(&ip->lock);
1063 hammer2_vop_nresolve(struct vop_nresolve_args *ap)
1065 hammer2_xop_nresolve_t *xop;
1066 hammer2_inode_t *ip;
1067 hammer2_inode_t *dip;
1068 struct namecache *ncp;
1073 dip = VTOI(ap->a_dvp);
1074 xop = &hammer2_xop_alloc(dip)->xop_nresolve;
1076 ncp = ap->a_nch->ncp;
1077 xop->name = ncp->nc_name;
1078 xop->name_len = ncp->nc_nlen;
1081 * Note: In DragonFly the kernel handles '.' and '..'.
1083 hammer2_inode_lock(dip, HAMMER2_RESOLVE_ALWAYS |
1084 HAMMER2_RESOLVE_SHARED);
1085 hammer2_xop_start(&xop->head, hammer2_xop_nresolve);
1087 error = hammer2_xop_collect(&xop->head, 0);
1091 ip = hammer2_inode_get(dip->pmp, dip, &xop->head.cluster);
1093 hammer2_inode_unlock(dip, NULL);
1096 * Acquire the related vnode
1098 * NOTE: For error processing, only ENOENT resolves the namecache
1099 * entry to NULL, otherwise we just return the error and
1100 * leave the namecache unresolved.
1102 * NOTE: multiple hammer2_inode structures can be aliased to the
1103 * same chain element, for example for hardlinks. This
1104 * use case does not 'reattach' inode associations that
1105 * might already exist, but always allocates a new one.
1107 * WARNING: inode structure is locked exclusively via inode_get
1108 * but chain was locked shared. inode_unlock()
1109 * will handle it properly.
1112 vp = hammer2_igetv(ip, &error);
1115 cache_setvp(ap->a_nch, vp);
1116 } else if (error == ENOENT) {
1117 cache_setvp(ap->a_nch, NULL);
1119 hammer2_inode_unlock(ip, NULL);
1122 * The vp should not be released until after we've disposed
1123 * of our locks, because it might cause vop_inactive() to
1130 cache_setvp(ap->a_nch, NULL);
1132 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1133 KASSERT(error || ap->a_nch->ncp->nc_vp != NULL,
1134 ("resolve error %d/%p ap %p\n",
1135 error, ap->a_nch->ncp->nc_vp, ap));
1143 hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1145 hammer2_inode_t *dip;
1146 hammer2_inode_t *ip;
1150 dip = VTOI(ap->a_dvp);
1152 if ((ip = dip->pip) == NULL) {
1157 hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
1158 *ap->a_vpp = hammer2_igetv(ip, &error);
1159 hammer2_inode_unlock(ip, NULL);
1167 hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1169 hammer2_inode_t *dip;
1170 hammer2_inode_t *nip;
1171 struct namecache *ncp;
1172 const uint8_t *name;
1177 dip = VTOI(ap->a_dvp);
1178 if (dip->pmp->ronly) {
1183 ncp = ap->a_nch->ncp;
1184 name = ncp->nc_name;
1185 name_len = ncp->nc_nlen;
1187 hammer2_pfs_memory_wait(dip->pmp);
1188 hammer2_trans_init(dip->pmp, 0);
1189 nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1190 name, name_len, 0, &error);
1192 KKASSERT(nip == NULL);
1195 *ap->a_vpp = hammer2_igetv(nip, &error);
1196 hammer2_inode_unlock(nip, NULL);
1198 hammer2_trans_done(dip->pmp);
1201 cache_setunresolved(ap->a_nch);
1202 cache_setvp(ap->a_nch, *ap->a_vpp);
1210 hammer2_vop_open(struct vop_open_args *ap)
1212 return vop_stdopen(ap);
1216 * hammer2_vop_advlock { vp, id, op, fl, flags }
1220 hammer2_vop_advlock(struct vop_advlock_args *ap)
1222 hammer2_inode_t *ip = VTOI(ap->a_vp);
1223 const hammer2_inode_data_t *ripdata;
1224 hammer2_cluster_t *cparent;
1227 hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS |
1228 HAMMER2_RESOLVE_SHARED);
1229 cparent = hammer2_inode_cluster(ip, HAMMER2_RESOLVE_ALWAYS |
1230 HAMMER2_RESOLVE_SHARED);
1231 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
1232 size = ripdata->meta.size;
1233 hammer2_inode_unlock(ip, cparent);
1234 return (lf_advlock(ap, &ip->advlock, size));
1240 hammer2_vop_close(struct vop_close_args *ap)
1242 return vop_stdclose(ap);
1246 * hammer2_vop_nlink { nch, dvp, vp, cred }
1248 * Create a hardlink from (vp) to {dvp, nch}.
1252 hammer2_vop_nlink(struct vop_nlink_args *ap)
1254 hammer2_inode_t *fdip; /* target directory to create link in */
1255 hammer2_inode_t *tdip; /* target directory to create link in */
1256 hammer2_inode_t *cdip; /* common parent directory */
1257 hammer2_inode_t *ip; /* inode we are hardlinking to */
1258 hammer2_cluster_t *cluster;
1259 hammer2_cluster_t *fdcluster;
1260 hammer2_cluster_t *tdcluster;
1261 hammer2_cluster_t *cdcluster;
1262 struct namecache *ncp;
1263 const uint8_t *name;
1268 tdip = VTOI(ap->a_dvp);
1269 if (tdip->pmp->ronly) {
1274 ncp = ap->a_nch->ncp;
1275 name = ncp->nc_name;
1276 name_len = ncp->nc_nlen;
1279 * ip represents the file being hardlinked. The file could be a
1280 * normal file or a hardlink target if it has already been hardlinked.
1281 * If ip is a hardlinked target then ip->pip represents the location
1282 * of the hardlinked target, NOT the location of the hardlink pointer.
1284 * Bump nlinks and potentially also create or move the hardlink
1285 * target in the parent directory common to (ip) and (tdip). The
1286 * consolidation code can modify ip->cluster and ip->pip. The
1287 * returned cluster is locked.
1289 ip = VTOI(ap->a_vp);
1290 hammer2_pfs_memory_wait(ip->pmp);
1291 hammer2_trans_init(ip->pmp, 0);
1294 * The common parent directory must be locked first to avoid deadlocks.
1295 * Also note that fdip and/or tdip might match cdip.
1298 cdip = hammer2_inode_common_parent(fdip, tdip);
1299 hammer2_inode_lock(cdip, HAMMER2_RESOLVE_ALWAYS);
1300 hammer2_inode_lock(fdip, HAMMER2_RESOLVE_ALWAYS);
1301 hammer2_inode_lock(tdip, HAMMER2_RESOLVE_ALWAYS);
1302 cdcluster = hammer2_inode_cluster(cdip, HAMMER2_RESOLVE_ALWAYS);
1303 fdcluster = hammer2_inode_cluster(fdip, HAMMER2_RESOLVE_ALWAYS);
1304 tdcluster = hammer2_inode_cluster(tdip, HAMMER2_RESOLVE_ALWAYS);
1306 hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
1307 cluster = hammer2_inode_cluster(ip, HAMMER2_RESOLVE_ALWAYS);
1309 error = hammer2_cluster_hardlink_consolidate(ip, &cluster,
1310 cdip, cdcluster, 1);
1315 * Create a directory entry connected to the specified cluster.
1317 * WARNING! chain can get moved by the connect (indirectly due to
1318 * potential indirect block creation).
1320 error = hammer2_inode_connect(ip, &cluster, 1,
1324 cache_setunresolved(ap->a_nch);
1325 cache_setvp(ap->a_nch, ap->a_vp);
1328 hammer2_inode_unlock(ip, cluster);
1329 hammer2_inode_unlock(tdip, tdcluster);
1330 hammer2_inode_unlock(fdip, fdcluster);
1331 hammer2_inode_unlock(cdip, cdcluster);
1332 hammer2_inode_drop(cdip);
1333 hammer2_trans_done(ip->pmp);
1340 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
1342 * The operating system has already ensured that the directory entry
1343 * does not exist and done all appropriate namespace locking.
1347 hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1349 hammer2_inode_t *dip;
1350 hammer2_inode_t *nip;
1351 struct namecache *ncp;
1352 const uint8_t *name;
1357 dip = VTOI(ap->a_dvp);
1358 if (dip->pmp->ronly) {
1363 ncp = ap->a_nch->ncp;
1364 name = ncp->nc_name;
1365 name_len = ncp->nc_nlen;
1366 hammer2_pfs_memory_wait(dip->pmp);
1367 hammer2_trans_init(dip->pmp, 0);
1369 nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1370 name, name_len, 0, &error);
1372 KKASSERT(nip == NULL);
1375 *ap->a_vpp = hammer2_igetv(nip, &error);
1376 hammer2_inode_unlock(nip, NULL);
1378 hammer2_trans_done(dip->pmp);
1381 cache_setunresolved(ap->a_nch);
1382 cache_setvp(ap->a_nch, *ap->a_vpp);
1389 * Make a device node (typically a fifo)
1393 hammer2_vop_nmknod(struct vop_nmknod_args *ap)
1395 hammer2_inode_t *dip;
1396 hammer2_inode_t *nip;
1397 struct namecache *ncp;
1398 const uint8_t *name;
1403 dip = VTOI(ap->a_dvp);
1404 if (dip->pmp->ronly) {
1409 ncp = ap->a_nch->ncp;
1410 name = ncp->nc_name;
1411 name_len = ncp->nc_nlen;
1412 hammer2_pfs_memory_wait(dip->pmp);
1413 hammer2_trans_init(dip->pmp, 0);
1415 nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1416 name, name_len, 0, &error);
1418 KKASSERT(nip == NULL);
1421 *ap->a_vpp = hammer2_igetv(nip, &error);
1422 hammer2_inode_unlock(nip, NULL);
1424 hammer2_trans_done(dip->pmp);
1427 cache_setunresolved(ap->a_nch);
1428 cache_setvp(ap->a_nch, *ap->a_vpp);
1435 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1439 hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1441 hammer2_inode_t *dip;
1442 hammer2_inode_t *nip;
1443 struct namecache *ncp;
1444 const uint8_t *name;
1448 dip = VTOI(ap->a_dvp);
1449 if (dip->pmp->ronly)
1452 ncp = ap->a_nch->ncp;
1453 name = ncp->nc_name;
1454 name_len = ncp->nc_nlen;
1455 hammer2_pfs_memory_wait(dip->pmp);
1456 hammer2_trans_init(dip->pmp, 0);
1458 ap->a_vap->va_type = VLNK; /* enforce type */
1460 nip = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1461 name, name_len, 0, &error);
1463 KKASSERT(nip == NULL);
1465 hammer2_trans_done(dip->pmp);
1468 *ap->a_vpp = hammer2_igetv(nip, &error);
1471 * Build the softlink (~like file data) and finalize the namecache.
1478 bytes = strlen(ap->a_target);
1481 if (bytes <= HAMMER2_EMBEDDED_BYTES) {
1482 KKASSERT(nipdata->meta.op_flags &
1483 HAMMER2_OPFLAG_DIRECTDATA);
1484 bcopy(ap->a_target, nipdata->u.data, bytes);
1485 nipdata->meta.size = bytes;
1486 nip->meta.size = bytes;
1487 hammer2_cluster_modsync(ncparent);
1488 hammer2_inode_unlock(nip, ncparent);
1489 /* nipdata = NULL; not needed */
1493 hammer2_inode_unlock(nip, NULL);
1494 bzero(&auio, sizeof(auio));
1495 bzero(&aiov, sizeof(aiov));
1496 auio.uio_iov = &aiov;
1497 auio.uio_segflg = UIO_SYSSPACE;
1498 auio.uio_rw = UIO_WRITE;
1499 auio.uio_resid = bytes;
1500 auio.uio_iovcnt = 1;
1501 auio.uio_td = curthread;
1502 aiov.iov_base = ap->a_target;
1503 aiov.iov_len = bytes;
1504 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
1505 /* XXX handle error */
1509 hammer2_inode_unlock(nip, NULL);
1511 hammer2_trans_done(dip->pmp);
1514 * Finalize namecache
1517 cache_setunresolved(ap->a_nch);
1518 cache_setvp(ap->a_nch, *ap->a_vpp);
1519 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1525 * hammer2_vop_nremove { nch, dvp, cred }
1529 hammer2_vop_nremove(struct vop_nremove_args *ap)
1531 hammer2_inode_t *dip;
1532 struct namecache *ncp;
1533 const uint8_t *name;
1538 dip = VTOI(ap->a_dvp);
1539 if (dip->pmp->ronly) {
1544 ncp = ap->a_nch->ncp;
1545 name = ncp->nc_name;
1546 name_len = ncp->nc_nlen;
1548 hammer2_pfs_memory_wait(dip->pmp);
1549 hammer2_trans_init(dip->pmp, 0);
1550 error = hammer2_unlink_file(dip, NULL, name, name_len,
1551 0, NULL, ap->a_nch, -1);
1552 hammer2_run_unlinkq(dip->pmp);
1553 hammer2_trans_done(dip->pmp);
1555 cache_unlink(ap->a_nch);
1561 * hammer2_vop_nrmdir { nch, dvp, cred }
1565 hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1567 hammer2_inode_t *dip;
1568 struct namecache *ncp;
1569 const uint8_t *name;
1574 dip = VTOI(ap->a_dvp);
1575 if (dip->pmp->ronly) {
1580 ncp = ap->a_nch->ncp;
1581 name = ncp->nc_name;
1582 name_len = ncp->nc_nlen;
1584 hammer2_pfs_memory_wait(dip->pmp);
1585 hammer2_trans_init(dip->pmp, 0);
1586 hammer2_run_unlinkq(dip->pmp);
1587 error = hammer2_unlink_file(dip, NULL, name, name_len,
1588 1, NULL, ap->a_nch, -1);
1589 hammer2_trans_done(dip->pmp);
1591 cache_unlink(ap->a_nch);
1597 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1601 hammer2_vop_nrename(struct vop_nrename_args *ap)
1603 struct namecache *fncp;
1604 struct namecache *tncp;
1605 hammer2_inode_t *cdip;
1606 hammer2_inode_t *fdip;
1607 hammer2_inode_t *tdip;
1608 hammer2_inode_t *ip;
1609 hammer2_cluster_t *cluster;
1610 hammer2_cluster_t *fdcluster;
1611 hammer2_cluster_t *tdcluster;
1612 hammer2_cluster_t *cdcluster;
1613 const uint8_t *fname;
1615 const uint8_t *tname;
1621 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1623 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1626 fdip = VTOI(ap->a_fdvp); /* source directory */
1627 tdip = VTOI(ap->a_tdvp); /* target directory */
1629 if (fdip->pmp->ronly)
1633 fncp = ap->a_fnch->ncp; /* entry name in source */
1634 fname = fncp->nc_name;
1635 fname_len = fncp->nc_nlen;
1637 tncp = ap->a_tnch->ncp; /* entry name in target */
1638 tname = tncp->nc_name;
1639 tname_len = tncp->nc_nlen;
1641 hammer2_pfs_memory_wait(tdip->pmp);
1642 hammer2_trans_init(tdip->pmp, 0);
1645 * ip is the inode being renamed. If this is a hardlink then
1646 * ip represents the actual file and not the hardlink marker.
1648 ip = VTOI(fncp->nc_vp);
1653 * The common parent directory must be locked first to avoid deadlocks.
1654 * Also note that fdip and/or tdip might match cdip.
1656 * WARNING! fdip may not match ip->pip. That is, if the source file
1657 * is already a hardlink then what we are renaming is the
1658 * hardlink pointer, not the hardlink itself. The hardlink
1659 * directory (ip->pip) will already be at a common parent
1662 * Be sure to use ip->pip when finding the common parent
1663 * against tdip or we might accidently move the hardlink
1664 * target into a subdirectory that makes it inaccessible to
1667 cdip = hammer2_inode_common_parent(ip->pip, tdip);
1668 hammer2_inode_lock(cdip, HAMMER2_RESOLVE_ALWAYS);
1669 hammer2_inode_lock(fdip, HAMMER2_RESOLVE_ALWAYS);
1670 hammer2_inode_lock(tdip, HAMMER2_RESOLVE_ALWAYS);
1671 cdcluster = hammer2_inode_cluster(cdip, HAMMER2_RESOLVE_ALWAYS);
1672 fdcluster = hammer2_inode_cluster(fdip, HAMMER2_RESOLVE_ALWAYS);
1673 tdcluster = hammer2_inode_cluster(tdip, HAMMER2_RESOLVE_ALWAYS);
1676 * Keep a tight grip on the inode so the temporary unlinking from
1677 * the source location prior to linking to the target location
1678 * does not cause the cluster to be destroyed.
1680 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
1681 * unlinking elements from their directories. Locking
1682 * the nlinks field does not lock the whole inode.
1684 hammer2_inode_ref(ip);
1687 * Remove target if it exists.
1689 error = hammer2_unlink_file(tdip, NULL, tname, tname_len,
1690 -1, NULL, ap->a_tnch, -1);
1692 if (error && error != ENOENT)
1696 * When renaming a hardlinked file we may have to re-consolidate
1697 * the location of the hardlink target.
1699 * If ip represents a regular file the consolidation code essentially
1700 * does nothing other than return the same locked cluster that was
1703 * The returned cluster will be locked.
1705 * WARNING! We do not currently have a local copy of ipdata but
1706 * we do use one later remember that it must be reloaded
1707 * on any modification to the inode, including connects.
1709 hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
1710 cluster = hammer2_inode_cluster(ip, HAMMER2_RESOLVE_ALWAYS);
1711 error = hammer2_cluster_hardlink_consolidate(ip, &cluster,
1712 cdip, cdcluster, 0);
1717 * Disconnect (fdip, fname) from the source directory. This will
1718 * disconnect (ip) if it represents a direct file. If (ip) represents
1719 * a hardlink the HARDLINK pointer object will be removed but the
1720 * hardlink will stay intact.
1722 * Always pass nch as NULL because we intend to reconnect the inode,
1723 * so we don't want hammer2_unlink_file() to rename it to the hidden
1724 * open-but-unlinked directory.
1726 * The target cluster may be marked DELETED but will not be destroyed
1727 * since we retain our hold on ip and cluster.
1729 * NOTE: We pass nlinks as 0 (not -1) in order to retain the file's
1732 error = hammer2_unlink_file(fdip, ip, fname, fname_len,
1733 -1, &hlink, NULL, 0);
1734 KKASSERT(error != EAGAIN);
1739 * Reconnect ip to target directory using cluster. Chains cannot
1740 * actually be moved, so this will duplicate the cluster in the new
1741 * spot and assign it to the ip, replacing the old cluster.
1743 * WARNING: Because recursive locks are allowed and we unlinked the
1744 * file that we have a cluster-in-hand for just above, the
1745 * cluster might have been delete-duplicated. We must
1746 * refactor the cluster.
1748 * WARNING: Chain locks can lock buffer cache buffers, to avoid
1749 * deadlocks we want to unlock before issuing a cache_*()
1750 * op (that might have to lock a vnode).
1752 * NOTE: Pass nlinks as 0 because we retained the link count from
1753 * the unlink, so we do not have to modify it.
1755 error = hammer2_inode_connect(ip, &cluster, hlink,
1757 tname, tname_len, 0);
1759 KKASSERT(cluster != NULL);
1760 hammer2_inode_repoint(ip, (hlink ? ip->pip : tdip), cluster);
1763 hammer2_inode_unlock(ip, cluster);
1765 hammer2_inode_unlock(tdip, tdcluster);
1766 hammer2_inode_unlock(fdip, fdcluster);
1767 hammer2_inode_unlock(cdip, cdcluster);
1768 hammer2_inode_drop(ip);
1769 hammer2_inode_drop(cdip);
1770 hammer2_run_unlinkq(fdip->pmp);
1771 hammer2_trans_done(tdip->pmp);
1774 * Issue the namecache update after unlocking all the internal
1775 * hammer structures, otherwise we might deadlock.
1777 if (tnch_error == 0) {
1778 cache_unlink(ap->a_tnch);
1779 cache_setunresolved(ap->a_tnch);
1782 cache_rename(ap->a_fnch, ap->a_tnch);
1789 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
1793 hammer2_vop_ioctl(struct vop_ioctl_args *ap)
1795 hammer2_inode_t *ip;
1799 ip = VTOI(ap->a_vp);
1801 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
1802 ap->a_fflag, ap->a_cred);
1809 hammer2_vop_mountctl(struct vop_mountctl_args *ap)
1817 case (MOUNTCTL_SET_EXPORT):
1818 mp = ap->a_head.a_ops->head.vv_mount;
1821 if (ap->a_ctllen != sizeof(struct export_args))
1824 rc = vfs_export(mp, &pmp->export,
1825 (const struct export_args *)ap->a_ctl);
1828 rc = vop_stdmountctl(ap);
1836 * This handles unlinked open files after the vnode is finally dereferenced.
1837 * To avoid deadlocks it cannot be called from the normal vnode recycling
1838 * path, so we call it (1) after a unlink, rmdir, or rename, (2) on every
1839 * flush, and (3) on umount.
1842 hammer2_run_unlinkq(hammer2_pfs_t *pmp)
1844 const hammer2_inode_data_t *ripdata;
1845 hammer2_inode_unlink_t *ipul;
1846 hammer2_inode_t *ip;
1847 hammer2_cluster_t *cluster;
1848 hammer2_cluster_t *cparent;
1850 if (TAILQ_EMPTY(&pmp->unlinkq))
1854 hammer2_spin_ex(&pmp->list_spin);
1855 while ((ipul = TAILQ_FIRST(&pmp->unlinkq)) != NULL) {
1856 TAILQ_REMOVE(&pmp->unlinkq, ipul, entry);
1857 hammer2_spin_unex(&pmp->list_spin);
1859 kfree(ipul, pmp->minode);
1861 hammer2_inode_lock(ip, HAMMER2_RESOLVE_ALWAYS);
1862 cluster = hammer2_inode_cluster(ip, HAMMER2_RESOLVE_ALWAYS);
1863 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1864 if (hammer2_debug & 0x400) {
1865 kprintf("hammer2: unlink on reclaim: %s refs=%d\n",
1866 ripdata->filename, ip->refs);
1870 * NOTE: Due to optimizations to avoid I/O on the inode for
1871 * the last unlink, ripdata->nlinks is not necessarily
1874 /* KKASSERT(ripdata->nlinks == 0); (see NOTE) */
1875 cparent = hammer2_cluster_parent(cluster);
1876 hammer2_cluster_delete(cparent, cluster,
1877 HAMMER2_DELETE_PERMANENT);
1878 hammer2_cluster_unlock(cparent);
1879 hammer2_cluster_drop(cparent);
1880 hammer2_inode_unlock(ip, cluster); /* inode lock */
1881 hammer2_inode_drop(ip); /* ipul ref */
1883 hammer2_spin_ex(&pmp->list_spin);
1885 hammer2_spin_unex(&pmp->list_spin);
1893 static void filt_hammer2detach(struct knote *kn);
1894 static int filt_hammer2read(struct knote *kn, long hint);
1895 static int filt_hammer2write(struct knote *kn, long hint);
1896 static int filt_hammer2vnode(struct knote *kn, long hint);
1898 static struct filterops hammer2read_filtops =
1899 { FILTEROP_ISFD | FILTEROP_MPSAFE,
1900 NULL, filt_hammer2detach, filt_hammer2read };
1901 static struct filterops hammer2write_filtops =
1902 { FILTEROP_ISFD | FILTEROP_MPSAFE,
1903 NULL, filt_hammer2detach, filt_hammer2write };
1904 static struct filterops hammer2vnode_filtops =
1905 { FILTEROP_ISFD | FILTEROP_MPSAFE,
1906 NULL, filt_hammer2detach, filt_hammer2vnode };
1910 hammer2_vop_kqfilter(struct vop_kqfilter_args *ap)
1912 struct vnode *vp = ap->a_vp;
1913 struct knote *kn = ap->a_kn;
1915 switch (kn->kn_filter) {
1917 kn->kn_fop = &hammer2read_filtops;
1920 kn->kn_fop = &hammer2write_filtops;
1923 kn->kn_fop = &hammer2vnode_filtops;
1926 return (EOPNOTSUPP);
1929 kn->kn_hook = (caddr_t)vp;
1931 knote_insert(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1937 filt_hammer2detach(struct knote *kn)
1939 struct vnode *vp = (void *)kn->kn_hook;
1941 knote_remove(&vp->v_pollinfo.vpi_kqinfo.ki_note, kn);
1945 filt_hammer2read(struct knote *kn, long hint)
1947 struct vnode *vp = (void *)kn->kn_hook;
1948 hammer2_inode_t *ip = VTOI(vp);
1951 if (hint == NOTE_REVOKE) {
1952 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
1955 off = ip->meta.size - kn->kn_fp->f_offset;
1956 kn->kn_data = (off < INTPTR_MAX) ? off : INTPTR_MAX;
1957 if (kn->kn_sfflags & NOTE_OLDAPI)
1959 return (kn->kn_data != 0);
1964 filt_hammer2write(struct knote *kn, long hint)
1966 if (hint == NOTE_REVOKE)
1967 kn->kn_flags |= (EV_EOF | EV_NODATA | EV_ONESHOT);
1973 filt_hammer2vnode(struct knote *kn, long hint)
1975 if (kn->kn_sfflags & hint)
1976 kn->kn_fflags |= hint;
1977 if (hint == NOTE_REVOKE) {
1978 kn->kn_flags |= (EV_EOF | EV_NODATA);
1981 return (kn->kn_fflags != 0);
1989 hammer2_vop_markatime(struct vop_markatime_args *ap)
1991 hammer2_inode_t *ip;
2004 hammer2_vop_fifokqfilter(struct vop_kqfilter_args *ap)
2008 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2010 error = hammer2_vop_kqfilter(ap);
2017 struct vop_ops hammer2_vnode_vops = {
2018 .vop_default = vop_defaultop,
2019 .vop_fsync = hammer2_vop_fsync,
2020 .vop_getpages = vop_stdgetpages,
2021 .vop_putpages = vop_stdputpages,
2022 .vop_access = hammer2_vop_access,
2023 .vop_advlock = hammer2_vop_advlock,
2024 .vop_close = hammer2_vop_close,
2025 .vop_nlink = hammer2_vop_nlink,
2026 .vop_ncreate = hammer2_vop_ncreate,
2027 .vop_nsymlink = hammer2_vop_nsymlink,
2028 .vop_nremove = hammer2_vop_nremove,
2029 .vop_nrmdir = hammer2_vop_nrmdir,
2030 .vop_nrename = hammer2_vop_nrename,
2031 .vop_getattr = hammer2_vop_getattr,
2032 .vop_setattr = hammer2_vop_setattr,
2033 .vop_readdir = hammer2_vop_readdir,
2034 .vop_readlink = hammer2_vop_readlink,
2035 .vop_getpages = vop_stdgetpages,
2036 .vop_putpages = vop_stdputpages,
2037 .vop_read = hammer2_vop_read,
2038 .vop_write = hammer2_vop_write,
2039 .vop_open = hammer2_vop_open,
2040 .vop_inactive = hammer2_vop_inactive,
2041 .vop_reclaim = hammer2_vop_reclaim,
2042 .vop_nresolve = hammer2_vop_nresolve,
2043 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2044 .vop_nmkdir = hammer2_vop_nmkdir,
2045 .vop_nmknod = hammer2_vop_nmknod,
2046 .vop_ioctl = hammer2_vop_ioctl,
2047 .vop_mountctl = hammer2_vop_mountctl,
2048 .vop_bmap = hammer2_vop_bmap,
2049 .vop_strategy = hammer2_vop_strategy,
2050 .vop_kqfilter = hammer2_vop_kqfilter
2053 struct vop_ops hammer2_spec_vops = {
2054 .vop_default = vop_defaultop,
2055 .vop_fsync = hammer2_vop_fsync,
2056 .vop_read = vop_stdnoread,
2057 .vop_write = vop_stdnowrite,
2058 .vop_access = hammer2_vop_access,
2059 .vop_close = hammer2_vop_close,
2060 .vop_markatime = hammer2_vop_markatime,
2061 .vop_getattr = hammer2_vop_getattr,
2062 .vop_inactive = hammer2_vop_inactive,
2063 .vop_reclaim = hammer2_vop_reclaim,
2064 .vop_setattr = hammer2_vop_setattr
2067 struct vop_ops hammer2_fifo_vops = {
2068 .vop_default = fifo_vnoperate,
2069 .vop_fsync = hammer2_vop_fsync,
2071 .vop_read = hammer2_vop_fiforead,
2072 .vop_write = hammer2_vop_fifowrite,
2074 .vop_access = hammer2_vop_access,
2076 .vop_close = hammer2_vop_fifoclose,
2078 .vop_markatime = hammer2_vop_markatime,
2079 .vop_getattr = hammer2_vop_getattr,
2080 .vop_inactive = hammer2_vop_inactive,
2081 .vop_reclaim = hammer2_vop_reclaim,
2082 .vop_setattr = hammer2_vop_setattr,
2083 .vop_kqfilter = hammer2_vop_fifokqfilter