MOUNTCTL - Adjust the hammer and null filesystems to work with the new mountctl
[dragonfly.git] / sys / vfs / hammer / hammer_vnops.c
blob70648113a635071698701943f7e098db23e58ac2
1 /*
2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * $DragonFly: src/sys/vfs/hammer/hammer_vnops.c,v 1.102 2008/10/16 17:24:16 dillon Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/fcntl.h>
41 #include <sys/namecache.h>
42 #include <sys/vnode.h>
43 #include <sys/lockf.h>
44 #include <sys/event.h>
45 #include <sys/stat.h>
46 #include <sys/dirent.h>
47 #include <sys/file.h>
48 #include <vm/vm_extern.h>
49 #include <vfs/fifofs/fifo.h>
50 #include "hammer.h"
53 * USERFS VNOPS
55 /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
56 static int hammer_vop_fsync(struct vop_fsync_args *);
57 static int hammer_vop_read(struct vop_read_args *);
58 static int hammer_vop_write(struct vop_write_args *);
59 static int hammer_vop_access(struct vop_access_args *);
60 static int hammer_vop_advlock(struct vop_advlock_args *);
61 static int hammer_vop_close(struct vop_close_args *);
62 static int hammer_vop_ncreate(struct vop_ncreate_args *);
63 static int hammer_vop_getattr(struct vop_getattr_args *);
64 static int hammer_vop_nresolve(struct vop_nresolve_args *);
65 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
66 static int hammer_vop_nlink(struct vop_nlink_args *);
67 static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
68 static int hammer_vop_nmknod(struct vop_nmknod_args *);
69 static int hammer_vop_open(struct vop_open_args *);
70 static int hammer_vop_print(struct vop_print_args *);
71 static int hammer_vop_readdir(struct vop_readdir_args *);
72 static int hammer_vop_readlink(struct vop_readlink_args *);
73 static int hammer_vop_nremove(struct vop_nremove_args *);
74 static int hammer_vop_nrename(struct vop_nrename_args *);
75 static int hammer_vop_nrmdir(struct vop_nrmdir_args *);
76 static int hammer_vop_markatime(struct vop_markatime_args *);
77 static int hammer_vop_setattr(struct vop_setattr_args *);
78 static int hammer_vop_strategy(struct vop_strategy_args *);
79 static int hammer_vop_bmap(struct vop_bmap_args *ap);
80 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
81 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
82 static int hammer_vop_ioctl(struct vop_ioctl_args *);
83 static int hammer_vop_mountctl(struct vop_mountctl_args *);
84 static int hammer_vop_kqfilter (struct vop_kqfilter_args *);
86 static int hammer_vop_fifoclose (struct vop_close_args *);
87 static int hammer_vop_fiforead (struct vop_read_args *);
88 static int hammer_vop_fifowrite (struct vop_write_args *);
89 static int hammer_vop_fifokqfilter (struct vop_kqfilter_args *);
91 struct vop_ops hammer_vnode_vops = {
92 .vop_default = vop_defaultop,
93 .vop_fsync = hammer_vop_fsync,
94 .vop_getpages = vop_stdgetpages,
95 .vop_putpages = vop_stdputpages,
96 .vop_read = hammer_vop_read,
97 .vop_write = hammer_vop_write,
98 .vop_access = hammer_vop_access,
99 .vop_advlock = hammer_vop_advlock,
100 .vop_close = hammer_vop_close,
101 .vop_ncreate = hammer_vop_ncreate,
102 .vop_getattr = hammer_vop_getattr,
103 .vop_inactive = hammer_vop_inactive,
104 .vop_reclaim = hammer_vop_reclaim,
105 .vop_nresolve = hammer_vop_nresolve,
106 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
107 .vop_nlink = hammer_vop_nlink,
108 .vop_nmkdir = hammer_vop_nmkdir,
109 .vop_nmknod = hammer_vop_nmknod,
110 .vop_open = hammer_vop_open,
111 .vop_pathconf = vop_stdpathconf,
112 .vop_print = hammer_vop_print,
113 .vop_readdir = hammer_vop_readdir,
114 .vop_readlink = hammer_vop_readlink,
115 .vop_nremove = hammer_vop_nremove,
116 .vop_nrename = hammer_vop_nrename,
117 .vop_nrmdir = hammer_vop_nrmdir,
118 .vop_markatime = hammer_vop_markatime,
119 .vop_setattr = hammer_vop_setattr,
120 .vop_bmap = hammer_vop_bmap,
121 .vop_strategy = hammer_vop_strategy,
122 .vop_nsymlink = hammer_vop_nsymlink,
123 .vop_nwhiteout = hammer_vop_nwhiteout,
124 .vop_ioctl = hammer_vop_ioctl,
125 .vop_mountctl = hammer_vop_mountctl,
126 .vop_kqfilter = hammer_vop_kqfilter
129 struct vop_ops hammer_spec_vops = {
130 .vop_default = vop_defaultop,
131 .vop_fsync = hammer_vop_fsync,
132 .vop_read = vop_stdnoread,
133 .vop_write = vop_stdnowrite,
134 .vop_access = hammer_vop_access,
135 .vop_close = hammer_vop_close,
136 .vop_markatime = hammer_vop_markatime,
137 .vop_getattr = hammer_vop_getattr,
138 .vop_inactive = hammer_vop_inactive,
139 .vop_reclaim = hammer_vop_reclaim,
140 .vop_setattr = hammer_vop_setattr
143 struct vop_ops hammer_fifo_vops = {
144 .vop_default = fifo_vnoperate,
145 .vop_fsync = hammer_vop_fsync,
146 .vop_read = hammer_vop_fiforead,
147 .vop_write = hammer_vop_fifowrite,
148 .vop_access = hammer_vop_access,
149 .vop_close = hammer_vop_fifoclose,
150 .vop_markatime = hammer_vop_markatime,
151 .vop_getattr = hammer_vop_getattr,
152 .vop_inactive = hammer_vop_inactive,
153 .vop_reclaim = hammer_vop_reclaim,
154 .vop_setattr = hammer_vop_setattr,
155 .vop_kqfilter = hammer_vop_fifokqfilter
158 static __inline
159 void
160 hammer_knote(struct vnode *vp, int flags)
162 if (flags)
163 KNOTE(&vp->v_pollinfo.vpi_selinfo.si_note, flags);
166 #ifdef DEBUG_TRUNCATE
167 struct hammer_inode *HammerTruncIp;
168 #endif
170 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
171 struct vnode *dvp, struct ucred *cred,
172 int flags, int isdir);
173 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
174 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
176 #if 0
177 static
179 hammer_vop_vnoperate(struct vop_generic_args *)
181 return (VOCALL(&hammer_vnode_vops, ap));
183 #endif
186 * hammer_vop_fsync { vp, waitfor }
188 * fsync() an inode to disk and wait for it to be completely committed
189 * such that the information would not be undone if a crash occured after
190 * return.
192 static
194 hammer_vop_fsync(struct vop_fsync_args *ap)
196 hammer_inode_t ip = VTOI(ap->a_vp);
198 ++hammer_count_fsyncs;
199 vfsync(ap->a_vp, ap->a_waitfor, 1, NULL, NULL);
200 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
201 if (ap->a_waitfor == MNT_WAIT) {
202 vn_unlock(ap->a_vp);
203 hammer_wait_inode(ip);
204 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
206 return (ip->error);
210 * hammer_vop_read { vp, uio, ioflag, cred }
212 * MPALMOSTSAFE
214 static
216 hammer_vop_read(struct vop_read_args *ap)
218 struct hammer_transaction trans;
219 hammer_inode_t ip;
220 off_t offset;
221 struct buf *bp;
222 struct uio *uio;
223 int error;
224 int n;
225 int seqcount;
226 int ioseqcount;
227 int blksize;
228 int got_mplock;
230 if (ap->a_vp->v_type != VREG)
231 return (EINVAL);
232 ip = VTOI(ap->a_vp);
233 error = 0;
234 uio = ap->a_uio;
237 * Allow the UIO's size to override the sequential heuristic.
239 blksize = hammer_blocksize(uio->uio_offset);
240 seqcount = (uio->uio_resid + (blksize - 1)) / blksize;
241 ioseqcount = ap->a_ioflag >> 16;
242 if (seqcount < ioseqcount)
243 seqcount = ioseqcount;
246 * Temporary hack until more of HAMMER can be made MPSAFE.
248 #ifdef SMP
249 if (curthread->td_mpcount) {
250 got_mplock = -1;
251 hammer_start_transaction(&trans, ip->hmp);
252 } else {
253 got_mplock = 0;
255 #else
256 hammer_start_transaction(&trans, ip->hmp);
257 got_mplock = -1;
258 #endif
261 * Access the data typically in HAMMER_BUFSIZE blocks via the
262 * buffer cache, but HAMMER may use a variable block size based
263 * on the offset.
265 * XXX Temporary hack, delay the start transaction while we remain
266 * MPSAFE. NOTE: ino_data.size cannot change while vnode is
267 * locked-shared.
269 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
270 int64_t base_offset;
271 int64_t file_limit;
273 blksize = hammer_blocksize(uio->uio_offset);
274 offset = (int)uio->uio_offset & (blksize - 1);
275 base_offset = uio->uio_offset - offset;
278 * MPSAFE
280 bp = getcacheblk(ap->a_vp, base_offset);
281 if (bp) {
282 error = 0;
283 goto skip;
287 * MPUNSAFE
289 if (got_mplock == 0) {
290 got_mplock = 1;
291 get_mplock();
292 hammer_start_transaction(&trans, ip->hmp);
295 if (hammer_cluster_enable) {
297 * Use file_limit to prevent cluster_read() from
298 * creating buffers of the wrong block size past
299 * the demarc.
301 file_limit = ip->ino_data.size;
302 if (base_offset < HAMMER_XDEMARC &&
303 file_limit > HAMMER_XDEMARC) {
304 file_limit = HAMMER_XDEMARC;
306 error = cluster_read(ap->a_vp,
307 file_limit, base_offset,
308 blksize, MAXPHYS,
309 seqcount, &bp);
310 } else {
311 error = bread(ap->a_vp, base_offset, blksize, &bp);
313 if (error) {
314 kprintf("error %d\n", error);
315 brelse(bp);
316 break;
318 skip:
320 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
321 n = blksize - offset;
322 if (n > uio->uio_resid)
323 n = uio->uio_resid;
324 if (n > ip->ino_data.size - uio->uio_offset)
325 n = (int)(ip->ino_data.size - uio->uio_offset);
326 error = uiomove((char *)bp->b_data + offset, n, uio);
328 /* data has a lower priority then meta-data */
329 bp->b_flags |= B_AGE;
330 bqrelse(bp);
331 if (error)
332 break;
333 hammer_stats_file_read += n;
337 * XXX only update the atime if we had to get the MP lock.
338 * XXX hack hack hack, fixme.
340 if (got_mplock) {
341 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
342 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
343 ip->ino_data.atime = trans.time;
344 hammer_modify_inode(ip, HAMMER_INODE_ATIME);
346 hammer_done_transaction(&trans);
347 if (got_mplock > 0)
348 rel_mplock();
350 return (error);
354 * hammer_vop_write { vp, uio, ioflag, cred }
356 static
358 hammer_vop_write(struct vop_write_args *ap)
360 struct hammer_transaction trans;
361 struct hammer_inode *ip;
362 hammer_mount_t hmp;
363 struct uio *uio;
364 int offset;
365 off_t base_offset;
366 struct buf *bp;
367 int kflags;
368 int error;
369 int n;
370 int flags;
371 int delta;
372 int seqcount;
374 if (ap->a_vp->v_type != VREG)
375 return (EINVAL);
376 ip = VTOI(ap->a_vp);
377 hmp = ip->hmp;
378 error = 0;
379 kflags = 0;
380 seqcount = ap->a_ioflag >> 16;
382 if (ip->flags & HAMMER_INODE_RO)
383 return (EROFS);
386 * Create a transaction to cover the operations we perform.
388 hammer_start_transaction(&trans, hmp);
389 uio = ap->a_uio;
392 * Check append mode
394 if (ap->a_ioflag & IO_APPEND)
395 uio->uio_offset = ip->ino_data.size;
398 * Check for illegal write offsets. Valid range is 0...2^63-1.
400 * NOTE: the base_off assignment is required to work around what
401 * I consider to be a GCC-4 optimization bug.
403 if (uio->uio_offset < 0) {
404 hammer_done_transaction(&trans);
405 return (EFBIG);
407 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
408 if (uio->uio_resid > 0 && base_offset <= 0) {
409 hammer_done_transaction(&trans);
410 return (EFBIG);
414 * Access the data typically in HAMMER_BUFSIZE blocks via the
415 * buffer cache, but HAMMER may use a variable block size based
416 * on the offset.
418 while (uio->uio_resid > 0) {
419 int fixsize = 0;
420 int blksize;
421 int blkmask;
423 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
424 break;
426 blksize = hammer_blocksize(uio->uio_offset);
429 * Do not allow HAMMER to blow out the buffer cache. Very
430 * large UIOs can lockout other processes due to bwillwrite()
431 * mechanics.
433 * The hammer inode is not locked during these operations.
434 * The vnode is locked which can interfere with the pageout
435 * daemon for non-UIO_NOCOPY writes but should not interfere
436 * with the buffer cache. Even so, we cannot afford to
437 * allow the pageout daemon to build up too many dirty buffer
438 * cache buffers.
440 * Only call this if we aren't being recursively called from
441 * a virtual disk device (vn), else we may deadlock.
443 if ((ap->a_ioflag & IO_RECURSE) == 0)
444 bwillwrite(blksize);
447 * Do not allow HAMMER to blow out system memory by
448 * accumulating too many records. Records are so well
449 * decoupled from the buffer cache that it is possible
450 * for userland to push data out to the media via
451 * direct-write, but build up the records queued to the
452 * backend faster then the backend can flush them out.
453 * HAMMER has hit its write limit but the frontend has
454 * no pushback to slow it down.
456 if (hmp->rsv_recs > hammer_limit_recs / 2) {
458 * Get the inode on the flush list
460 if (ip->rsv_recs >= 64)
461 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
462 else if (ip->rsv_recs >= 16)
463 hammer_flush_inode(ip, 0);
466 * Keep the flusher going if the system keeps
467 * queueing records.
469 delta = hmp->count_newrecords -
470 hmp->last_newrecords;
471 if (delta < 0 || delta > hammer_limit_recs / 2) {
472 hmp->last_newrecords = hmp->count_newrecords;
473 hammer_sync_hmp(hmp, MNT_NOWAIT);
477 * If we have gotten behind start slowing
478 * down the writers.
480 delta = (hmp->rsv_recs - hammer_limit_recs) *
481 hz / hammer_limit_recs;
482 if (delta > 0)
483 tsleep(&trans, 0, "hmrslo", delta);
487 * Calculate the blocksize at the current offset and figure
488 * out how much we can actually write.
490 blkmask = blksize - 1;
491 offset = (int)uio->uio_offset & blkmask;
492 base_offset = uio->uio_offset & ~(int64_t)blkmask;
493 n = blksize - offset;
494 if (n > uio->uio_resid)
495 n = uio->uio_resid;
496 if (uio->uio_offset + n > ip->ino_data.size) {
497 vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
498 fixsize = 1;
499 kflags |= NOTE_EXTEND;
502 if (uio->uio_segflg == UIO_NOCOPY) {
504 * Issuing a write with the same data backing the
505 * buffer. Instantiate the buffer to collect the
506 * backing vm pages, then read-in any missing bits.
508 * This case is used by vop_stdputpages().
510 bp = getblk(ap->a_vp, base_offset,
511 blksize, GETBLK_BHEAVY, 0);
512 if ((bp->b_flags & B_CACHE) == 0) {
513 bqrelse(bp);
514 error = bread(ap->a_vp, base_offset,
515 blksize, &bp);
517 } else if (offset == 0 && uio->uio_resid >= blksize) {
519 * Even though we are entirely overwriting the buffer
520 * we may still have to zero it out to avoid a
521 * mmap/write visibility issue.
523 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
524 if ((bp->b_flags & B_CACHE) == 0)
525 vfs_bio_clrbuf(bp);
526 } else if (base_offset >= ip->ino_data.size) {
528 * If the base offset of the buffer is beyond the
529 * file EOF, we don't have to issue a read.
531 bp = getblk(ap->a_vp, base_offset,
532 blksize, GETBLK_BHEAVY, 0);
533 vfs_bio_clrbuf(bp);
534 } else {
536 * Partial overwrite, read in any missing bits then
537 * replace the portion being written.
539 error = bread(ap->a_vp, base_offset, blksize, &bp);
540 if (error == 0)
541 bheavy(bp);
543 if (error == 0) {
544 error = uiomove((char *)bp->b_data + offset,
545 n, uio);
549 * If we screwed up we have to undo any VM size changes we
550 * made.
552 if (error) {
553 brelse(bp);
554 if (fixsize) {
555 vtruncbuf(ap->a_vp, ip->ino_data.size,
556 hammer_blocksize(ip->ino_data.size));
558 break;
560 kflags |= NOTE_WRITE;
561 hammer_stats_file_write += n;
562 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
563 if (ip->ino_data.size < uio->uio_offset) {
564 ip->ino_data.size = uio->uio_offset;
565 flags = HAMMER_INODE_DDIRTY;
566 vnode_pager_setsize(ap->a_vp, ip->ino_data.size);
567 } else {
568 flags = 0;
570 ip->ino_data.mtime = trans.time;
571 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
572 hammer_modify_inode(ip, flags);
575 * Once we dirty the buffer any cached zone-X offset
576 * becomes invalid. HAMMER NOTE: no-history mode cannot
577 * allow overwriting over the same data sector unless
578 * we provide UNDOs for the old data, which we don't.
580 bp->b_bio2.bio_offset = NOOFFSET;
583 * Final buffer disposition.
585 bp->b_flags |= B_AGE;
586 if (ap->a_ioflag & IO_SYNC) {
587 bwrite(bp);
588 } else if (ap->a_ioflag & IO_DIRECT) {
589 bawrite(bp);
590 } else {
591 bdwrite(bp);
594 hammer_done_transaction(&trans);
595 hammer_knote(ap->a_vp, kflags);
596 return (error);
600 * hammer_vop_access { vp, mode, cred }
602 static
604 hammer_vop_access(struct vop_access_args *ap)
606 struct hammer_inode *ip = VTOI(ap->a_vp);
607 uid_t uid;
608 gid_t gid;
609 int error;
611 ++hammer_stats_file_iopsr;
612 uid = hammer_to_unix_xid(&ip->ino_data.uid);
613 gid = hammer_to_unix_xid(&ip->ino_data.gid);
615 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
616 ip->ino_data.uflags);
617 return (error);
621 * hammer_vop_advlock { vp, id, op, fl, flags }
623 static
625 hammer_vop_advlock(struct vop_advlock_args *ap)
627 hammer_inode_t ip = VTOI(ap->a_vp);
629 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
633 * hammer_vop_close { vp, fflag }
635 static
637 hammer_vop_close(struct vop_close_args *ap)
639 /*hammer_inode_t ip = VTOI(ap->a_vp);*/
640 return (vop_stdclose(ap));
644 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
646 * The operating system has already ensured that the directory entry
647 * does not exist and done all appropriate namespace locking.
649 static
651 hammer_vop_ncreate(struct vop_ncreate_args *ap)
653 struct hammer_transaction trans;
654 struct hammer_inode *dip;
655 struct hammer_inode *nip;
656 struct nchandle *nch;
657 int error;
659 nch = ap->a_nch;
660 dip = VTOI(ap->a_dvp);
662 if (dip->flags & HAMMER_INODE_RO)
663 return (EROFS);
664 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
665 return (error);
668 * Create a transaction to cover the operations we perform.
670 hammer_start_transaction(&trans, dip->hmp);
671 ++hammer_stats_file_iopsw;
674 * Create a new filesystem object of the requested type. The
675 * returned inode will be referenced and shared-locked to prevent
676 * it from being moved to the flusher.
678 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
679 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
680 NULL, &nip);
681 if (error) {
682 hkprintf("hammer_create_inode error %d\n", error);
683 hammer_done_transaction(&trans);
684 *ap->a_vpp = NULL;
685 return (error);
689 * Add the new filesystem object to the directory. This will also
690 * bump the inode's link count.
692 error = hammer_ip_add_directory(&trans, dip,
693 nch->ncp->nc_name, nch->ncp->nc_nlen,
694 nip);
695 if (error)
696 hkprintf("hammer_ip_add_directory error %d\n", error);
699 * Finish up.
701 if (error) {
702 hammer_rel_inode(nip, 0);
703 hammer_done_transaction(&trans);
704 *ap->a_vpp = NULL;
705 } else {
706 error = hammer_get_vnode(nip, ap->a_vpp);
707 hammer_done_transaction(&trans);
708 hammer_rel_inode(nip, 0);
709 if (error == 0) {
710 cache_setunresolved(ap->a_nch);
711 cache_setvp(ap->a_nch, *ap->a_vpp);
713 hammer_knote(ap->a_dvp, NOTE_WRITE);
715 return (error);
719 * hammer_vop_getattr { vp, vap }
721 * Retrieve an inode's attribute information. When accessing inodes
722 * historically we fake the atime field to ensure consistent results.
723 * The atime field is stored in the B-Tree element and allowed to be
724 * updated without cycling the element.
726 * MPSAFE
728 static
730 hammer_vop_getattr(struct vop_getattr_args *ap)
732 struct hammer_inode *ip = VTOI(ap->a_vp);
733 struct vattr *vap = ap->a_vap;
736 * We want the fsid to be different when accessing a filesystem
737 * with different as-of's so programs like diff don't think
738 * the files are the same.
740 * We also want the fsid to be the same when comparing snapshots,
741 * or when comparing mirrors (which might be backed by different
742 * physical devices). HAMMER fsids are based on the PFS's
743 * shared_uuid field.
745 * XXX there is a chance of collision here. The va_fsid reported
746 * by stat is different from the more involved fsid used in the
747 * mount structure.
749 ++hammer_stats_file_iopsr;
750 hammer_lock_sh(&ip->lock);
751 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
752 (u_int32_t)(ip->obj_asof >> 32);
754 vap->va_fileid = ip->ino_leaf.base.obj_id;
755 vap->va_mode = ip->ino_data.mode;
756 vap->va_nlink = ip->ino_data.nlinks;
757 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
758 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
759 vap->va_rmajor = 0;
760 vap->va_rminor = 0;
761 vap->va_size = ip->ino_data.size;
764 * Special case for @@PFS softlinks. The actual size of the
765 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
766 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
768 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
769 ip->ino_data.size == 10 &&
770 ip->obj_asof == HAMMER_MAX_TID &&
771 ip->obj_localization == 0 &&
772 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
773 if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE)
774 vap->va_size = 26;
775 else
776 vap->va_size = 10;
780 * We must provide a consistent atime and mtime for snapshots
781 * so people can do a 'tar cf - ... | md5' on them and get
782 * consistent results.
784 if (ip->flags & HAMMER_INODE_RO) {
785 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
786 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
787 } else {
788 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
789 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
791 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
792 vap->va_flags = ip->ino_data.uflags;
793 vap->va_gen = 1; /* hammer inums are unique for all time */
794 vap->va_blocksize = HAMMER_BUFSIZE;
795 if (ip->ino_data.size >= HAMMER_XDEMARC) {
796 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
797 ~HAMMER_XBUFMASK64;
798 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
799 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
800 ~HAMMER_BUFMASK64;
801 } else {
802 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
805 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
806 vap->va_filerev = 0; /* XXX */
807 /* mtime uniquely identifies any adjustments made to the file XXX */
808 vap->va_fsmid = ip->ino_data.mtime;
809 vap->va_uid_uuid = ip->ino_data.uid;
810 vap->va_gid_uuid = ip->ino_data.gid;
811 vap->va_fsid_uuid = ip->hmp->fsid;
812 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
813 VA_FSID_UUID_VALID;
815 switch (ip->ino_data.obj_type) {
816 case HAMMER_OBJTYPE_CDEV:
817 case HAMMER_OBJTYPE_BDEV:
818 vap->va_rmajor = ip->ino_data.rmajor;
819 vap->va_rminor = ip->ino_data.rminor;
820 break;
821 default:
822 break;
824 hammer_unlock(&ip->lock);
825 return(0);
829 * hammer_vop_nresolve { nch, dvp, cred }
831 * Locate the requested directory entry.
833 static
835 hammer_vop_nresolve(struct vop_nresolve_args *ap)
837 struct hammer_transaction trans;
838 struct namecache *ncp;
839 hammer_inode_t dip;
840 hammer_inode_t ip;
841 hammer_tid_t asof;
842 struct hammer_cursor cursor;
843 struct vnode *vp;
844 int64_t namekey;
845 int error;
846 int i;
847 int nlen;
848 int flags;
849 int ispfs;
850 int64_t obj_id;
851 u_int32_t localization;
852 u_int32_t max_iterations;
855 * Misc initialization, plus handle as-of name extensions. Look for
856 * the '@@' extension. Note that as-of files and directories cannot
857 * be modified.
859 dip = VTOI(ap->a_dvp);
860 ncp = ap->a_nch->ncp;
861 asof = dip->obj_asof;
862 localization = dip->obj_localization; /* for code consistency */
863 nlen = ncp->nc_nlen;
864 flags = dip->flags & HAMMER_INODE_RO;
865 ispfs = 0;
867 hammer_simple_transaction(&trans, dip->hmp);
868 ++hammer_stats_file_iopsr;
870 for (i = 0; i < nlen; ++i) {
871 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
872 error = hammer_str_to_tid(ncp->nc_name + i + 2,
873 &ispfs, &asof, &localization);
874 if (error != 0) {
875 i = nlen;
876 break;
878 if (asof != HAMMER_MAX_TID)
879 flags |= HAMMER_INODE_RO;
880 break;
883 nlen = i;
886 * If this is a PFS softlink we dive into the PFS
888 if (ispfs && nlen == 0) {
889 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
890 asof, localization,
891 flags, &error);
892 if (error == 0) {
893 error = hammer_get_vnode(ip, &vp);
894 hammer_rel_inode(ip, 0);
895 } else {
896 vp = NULL;
898 if (error == 0) {
899 vn_unlock(vp);
900 cache_setvp(ap->a_nch, vp);
901 vrele(vp);
903 goto done;
907 * If there is no path component the time extension is relative to dip.
908 * e.g. "fubar/@@<snapshot>"
910 * "." is handled by the kernel, but ".@@<snapshot>" is not.
911 * e.g. "fubar/.@@<snapshot>"
913 * ".." is handled by the kernel. We do not currently handle
914 * "..@<snapshot>".
916 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
917 ip = hammer_get_inode(&trans, dip, dip->obj_id,
918 asof, dip->obj_localization,
919 flags, &error);
920 if (error == 0) {
921 error = hammer_get_vnode(ip, &vp);
922 hammer_rel_inode(ip, 0);
923 } else {
924 vp = NULL;
926 if (error == 0) {
927 vn_unlock(vp);
928 cache_setvp(ap->a_nch, vp);
929 vrele(vp);
931 goto done;
935 * Calculate the namekey and setup the key range for the scan. This
936 * works kinda like a chained hash table where the lower 32 bits
937 * of the namekey synthesize the chain.
939 * The key range is inclusive of both key_beg and key_end.
941 namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen,
942 &max_iterations);
944 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
945 cursor.key_beg.localization = dip->obj_localization +
946 hammer_dir_localization(dip);
947 cursor.key_beg.obj_id = dip->obj_id;
948 cursor.key_beg.key = namekey;
949 cursor.key_beg.create_tid = 0;
950 cursor.key_beg.delete_tid = 0;
951 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
952 cursor.key_beg.obj_type = 0;
954 cursor.key_end = cursor.key_beg;
955 cursor.key_end.key += max_iterations;
956 cursor.asof = asof;
957 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
960 * Scan all matching records (the chain), locate the one matching
961 * the requested path component.
963 * The hammer_ip_*() functions merge in-memory records with on-disk
964 * records for the purposes of the search.
966 obj_id = 0;
967 localization = HAMMER_DEF_LOCALIZATION;
969 if (error == 0) {
970 error = hammer_ip_first(&cursor);
971 while (error == 0) {
972 error = hammer_ip_resolve_data(&cursor);
973 if (error)
974 break;
975 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
976 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
977 obj_id = cursor.data->entry.obj_id;
978 localization = cursor.data->entry.localization;
979 break;
981 error = hammer_ip_next(&cursor);
984 hammer_done_cursor(&cursor);
987 * Lookup the obj_id. This should always succeed. If it does not
988 * the filesystem may be damaged and we return a dummy inode.
990 if (error == 0) {
991 ip = hammer_get_inode(&trans, dip, obj_id,
992 asof, localization,
993 flags, &error);
994 if (error == ENOENT) {
995 kprintf("HAMMER: WARNING: Missing "
996 "inode for dirent \"%s\"\n"
997 "\tobj_id = %016llx, asof=%016llx, lo=%08x\n",
998 ncp->nc_name,
999 (long long)obj_id, (long long)asof,
1000 localization);
1001 error = 0;
1002 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
1003 asof, localization,
1004 flags, &error);
1006 if (error == 0) {
1007 error = hammer_get_vnode(ip, &vp);
1008 hammer_rel_inode(ip, 0);
1009 } else {
1010 vp = NULL;
1012 if (error == 0) {
1013 vn_unlock(vp);
1014 cache_setvp(ap->a_nch, vp);
1015 vrele(vp);
1017 } else if (error == ENOENT) {
1018 cache_setvp(ap->a_nch, NULL);
1020 done:
1021 hammer_done_transaction(&trans);
1022 return (error);
1026 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
1028 * Locate the parent directory of a directory vnode.
1030 * dvp is referenced but not locked. *vpp must be returned referenced and
1031 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
1032 * at the root, instead it could indicate that the directory we were in was
1033 * removed.
1035 * NOTE: as-of sequences are not linked into the directory structure. If
1036 * we are at the root with a different asof then the mount point, reload
1037 * the same directory with the mount point's asof. I'm not sure what this
1038 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
1039 * get confused, but it hasn't been tested.
1041 static
1043 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1045 struct hammer_transaction trans;
1046 struct hammer_inode *dip;
1047 struct hammer_inode *ip;
1048 int64_t parent_obj_id;
1049 u_int32_t parent_obj_localization;
1050 hammer_tid_t asof;
1051 int error;
1053 dip = VTOI(ap->a_dvp);
1054 asof = dip->obj_asof;
1057 * Whos are parent? This could be the root of a pseudo-filesystem
1058 * whos parent is in another localization domain.
1060 parent_obj_id = dip->ino_data.parent_obj_id;
1061 if (dip->obj_id == HAMMER_OBJID_ROOT)
1062 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
1063 else
1064 parent_obj_localization = dip->obj_localization;
1066 if (parent_obj_id == 0) {
1067 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1068 asof != dip->hmp->asof) {
1069 parent_obj_id = dip->obj_id;
1070 asof = dip->hmp->asof;
1071 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1072 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
1073 (long long)dip->obj_asof);
1074 } else {
1075 *ap->a_vpp = NULL;
1076 return ENOENT;
1080 hammer_simple_transaction(&trans, dip->hmp);
1081 ++hammer_stats_file_iopsr;
1083 ip = hammer_get_inode(&trans, dip, parent_obj_id,
1084 asof, parent_obj_localization,
1085 dip->flags, &error);
1086 if (ip) {
1087 error = hammer_get_vnode(ip, ap->a_vpp);
1088 hammer_rel_inode(ip, 0);
1089 } else {
1090 *ap->a_vpp = NULL;
1092 hammer_done_transaction(&trans);
1093 return (error);
1097 * hammer_vop_nlink { nch, dvp, vp, cred }
1099 static
1101 hammer_vop_nlink(struct vop_nlink_args *ap)
1103 struct hammer_transaction trans;
1104 struct hammer_inode *dip;
1105 struct hammer_inode *ip;
1106 struct nchandle *nch;
1107 int error;
1109 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1110 return(EXDEV);
1112 nch = ap->a_nch;
1113 dip = VTOI(ap->a_dvp);
1114 ip = VTOI(ap->a_vp);
1116 if (dip->obj_localization != ip->obj_localization)
1117 return(EXDEV);
1119 if (dip->flags & HAMMER_INODE_RO)
1120 return (EROFS);
1121 if (ip->flags & HAMMER_INODE_RO)
1122 return (EROFS);
1123 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1124 return (error);
1127 * Create a transaction to cover the operations we perform.
1129 hammer_start_transaction(&trans, dip->hmp);
1130 ++hammer_stats_file_iopsw;
1133 * Add the filesystem object to the directory. Note that neither
1134 * dip nor ip are referenced or locked, but their vnodes are
1135 * referenced. This function will bump the inode's link count.
1137 error = hammer_ip_add_directory(&trans, dip,
1138 nch->ncp->nc_name, nch->ncp->nc_nlen,
1139 ip);
1142 * Finish up.
1144 if (error == 0) {
1145 cache_setunresolved(nch);
1146 cache_setvp(nch, ap->a_vp);
1148 hammer_done_transaction(&trans);
1149 hammer_knote(ap->a_vp, NOTE_LINK);
1150 hammer_knote(ap->a_dvp, NOTE_WRITE);
1151 return (error);
1155 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1157 * The operating system has already ensured that the directory entry
1158 * does not exist and done all appropriate namespace locking.
1160 static
1162 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1164 struct hammer_transaction trans;
1165 struct hammer_inode *dip;
1166 struct hammer_inode *nip;
1167 struct nchandle *nch;
1168 int error;
1170 nch = ap->a_nch;
1171 dip = VTOI(ap->a_dvp);
1173 if (dip->flags & HAMMER_INODE_RO)
1174 return (EROFS);
1175 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1176 return (error);
1179 * Create a transaction to cover the operations we perform.
1181 hammer_start_transaction(&trans, dip->hmp);
1182 ++hammer_stats_file_iopsw;
1185 * Create a new filesystem object of the requested type. The
1186 * returned inode will be referenced but not locked.
1188 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1189 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1190 NULL, &nip);
1191 if (error) {
1192 hkprintf("hammer_mkdir error %d\n", error);
1193 hammer_done_transaction(&trans);
1194 *ap->a_vpp = NULL;
1195 return (error);
1198 * Add the new filesystem object to the directory. This will also
1199 * bump the inode's link count.
1201 error = hammer_ip_add_directory(&trans, dip,
1202 nch->ncp->nc_name, nch->ncp->nc_nlen,
1203 nip);
1204 if (error)
1205 hkprintf("hammer_mkdir (add) error %d\n", error);
1208 * Finish up.
1210 if (error) {
1211 hammer_rel_inode(nip, 0);
1212 *ap->a_vpp = NULL;
1213 } else {
1214 error = hammer_get_vnode(nip, ap->a_vpp);
1215 hammer_rel_inode(nip, 0);
1216 if (error == 0) {
1217 cache_setunresolved(ap->a_nch);
1218 cache_setvp(ap->a_nch, *ap->a_vpp);
1221 hammer_done_transaction(&trans);
1222 if (error == 0)
1223 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1224 return (error);
1228 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1230 * The operating system has already ensured that the directory entry
1231 * does not exist and done all appropriate namespace locking.
1233 static
1235 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1237 struct hammer_transaction trans;
1238 struct hammer_inode *dip;
1239 struct hammer_inode *nip;
1240 struct nchandle *nch;
1241 int error;
1243 nch = ap->a_nch;
1244 dip = VTOI(ap->a_dvp);
1246 if (dip->flags & HAMMER_INODE_RO)
1247 return (EROFS);
1248 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1249 return (error);
1252 * Create a transaction to cover the operations we perform.
1254 hammer_start_transaction(&trans, dip->hmp);
1255 ++hammer_stats_file_iopsw;
1258 * Create a new filesystem object of the requested type. The
1259 * returned inode will be referenced but not locked.
1261 * If mknod specifies a directory a pseudo-fs is created.
1263 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1264 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1265 NULL, &nip);
1266 if (error) {
1267 hammer_done_transaction(&trans);
1268 *ap->a_vpp = NULL;
1269 return (error);
1273 * Add the new filesystem object to the directory. This will also
1274 * bump the inode's link count.
1276 error = hammer_ip_add_directory(&trans, dip,
1277 nch->ncp->nc_name, nch->ncp->nc_nlen,
1278 nip);
1281 * Finish up.
1283 if (error) {
1284 hammer_rel_inode(nip, 0);
1285 *ap->a_vpp = NULL;
1286 } else {
1287 error = hammer_get_vnode(nip, ap->a_vpp);
1288 hammer_rel_inode(nip, 0);
1289 if (error == 0) {
1290 cache_setunresolved(ap->a_nch);
1291 cache_setvp(ap->a_nch, *ap->a_vpp);
1294 hammer_done_transaction(&trans);
1295 if (error == 0)
1296 hammer_knote(ap->a_dvp, NOTE_WRITE);
1297 return (error);
1301 * hammer_vop_open { vp, mode, cred, fp }
1303 static
1305 hammer_vop_open(struct vop_open_args *ap)
1307 hammer_inode_t ip;
1309 ++hammer_stats_file_iopsr;
1310 ip = VTOI(ap->a_vp);
1312 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1313 return (EROFS);
1314 return(vop_stdopen(ap));
1318 * hammer_vop_print { vp }
1320 static
1322 hammer_vop_print(struct vop_print_args *ap)
1324 return EOPNOTSUPP;
1328 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1330 static
1332 hammer_vop_readdir(struct vop_readdir_args *ap)
1334 struct hammer_transaction trans;
1335 struct hammer_cursor cursor;
1336 struct hammer_inode *ip;
1337 struct uio *uio;
1338 hammer_base_elm_t base;
1339 int error;
1340 int cookie_index;
1341 int ncookies;
1342 off_t *cookies;
1343 off_t saveoff;
1344 int r;
1345 int dtype;
1347 ++hammer_stats_file_iopsr;
1348 ip = VTOI(ap->a_vp);
1349 uio = ap->a_uio;
1350 saveoff = uio->uio_offset;
1352 if (ap->a_ncookies) {
1353 ncookies = uio->uio_resid / 16 + 1;
1354 if (ncookies > 1024)
1355 ncookies = 1024;
1356 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1357 cookie_index = 0;
1358 } else {
1359 ncookies = -1;
1360 cookies = NULL;
1361 cookie_index = 0;
1364 hammer_simple_transaction(&trans, ip->hmp);
1367 * Handle artificial entries
1369 * It should be noted that the minimum value for a directory
1370 * hash key on-media is 0x0000000100000000, so we can use anything
1371 * less then that to represent our 'special' key space.
1373 error = 0;
1374 if (saveoff == 0) {
1375 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1376 if (r)
1377 goto done;
1378 if (cookies)
1379 cookies[cookie_index] = saveoff;
1380 ++saveoff;
1381 ++cookie_index;
1382 if (cookie_index == ncookies)
1383 goto done;
1385 if (saveoff == 1) {
1386 if (ip->ino_data.parent_obj_id) {
1387 r = vop_write_dirent(&error, uio,
1388 ip->ino_data.parent_obj_id,
1389 DT_DIR, 2, "..");
1390 } else {
1391 r = vop_write_dirent(&error, uio,
1392 ip->obj_id, DT_DIR, 2, "..");
1394 if (r)
1395 goto done;
1396 if (cookies)
1397 cookies[cookie_index] = saveoff;
1398 ++saveoff;
1399 ++cookie_index;
1400 if (cookie_index == ncookies)
1401 goto done;
1405 * Key range (begin and end inclusive) to scan. Directory keys
1406 * directly translate to a 64 bit 'seek' position.
1408 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1409 cursor.key_beg.localization = ip->obj_localization +
1410 hammer_dir_localization(ip);
1411 cursor.key_beg.obj_id = ip->obj_id;
1412 cursor.key_beg.create_tid = 0;
1413 cursor.key_beg.delete_tid = 0;
1414 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1415 cursor.key_beg.obj_type = 0;
1416 cursor.key_beg.key = saveoff;
1418 cursor.key_end = cursor.key_beg;
1419 cursor.key_end.key = HAMMER_MAX_KEY;
1420 cursor.asof = ip->obj_asof;
1421 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1423 error = hammer_ip_first(&cursor);
1425 while (error == 0) {
1426 error = hammer_ip_resolve_data(&cursor);
1427 if (error)
1428 break;
1429 base = &cursor.leaf->base;
1430 saveoff = base->key;
1431 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1433 if (base->obj_id != ip->obj_id)
1434 panic("readdir: bad record at %p", cursor.node);
1437 * Convert pseudo-filesystems into softlinks
1439 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1440 r = vop_write_dirent(
1441 &error, uio, cursor.data->entry.obj_id,
1442 dtype,
1443 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1444 (void *)cursor.data->entry.name);
1445 if (r)
1446 break;
1447 ++saveoff;
1448 if (cookies)
1449 cookies[cookie_index] = base->key;
1450 ++cookie_index;
1451 if (cookie_index == ncookies)
1452 break;
1453 error = hammer_ip_next(&cursor);
1455 hammer_done_cursor(&cursor);
1457 done:
1458 hammer_done_transaction(&trans);
1460 if (ap->a_eofflag)
1461 *ap->a_eofflag = (error == ENOENT);
1462 uio->uio_offset = saveoff;
1463 if (error && cookie_index == 0) {
1464 if (error == ENOENT)
1465 error = 0;
1466 if (cookies) {
1467 kfree(cookies, M_TEMP);
1468 *ap->a_ncookies = 0;
1469 *ap->a_cookies = NULL;
1471 } else {
1472 if (error == ENOENT)
1473 error = 0;
1474 if (cookies) {
1475 *ap->a_ncookies = cookie_index;
1476 *ap->a_cookies = cookies;
1479 return(error);
1483 * hammer_vop_readlink { vp, uio, cred }
1485 static
1487 hammer_vop_readlink(struct vop_readlink_args *ap)
1489 struct hammer_transaction trans;
1490 struct hammer_cursor cursor;
1491 struct hammer_inode *ip;
1492 char buf[32];
1493 u_int32_t localization;
1494 hammer_pseudofs_inmem_t pfsm;
1495 int error;
1497 ip = VTOI(ap->a_vp);
1500 * Shortcut if the symlink data was stuffed into ino_data.
1502 * Also expand special "@@PFS%05d" softlinks (expansion only
1503 * occurs for non-historical (current) accesses made from the
1504 * primary filesystem).
1506 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1507 char *ptr;
1508 int bytes;
1510 ptr = ip->ino_data.ext.symlink;
1511 bytes = (int)ip->ino_data.size;
1512 if (bytes == 10 &&
1513 ip->obj_asof == HAMMER_MAX_TID &&
1514 ip->obj_localization == 0 &&
1515 strncmp(ptr, "@@PFS", 5) == 0) {
1516 hammer_simple_transaction(&trans, ip->hmp);
1517 bcopy(ptr + 5, buf, 5);
1518 buf[5] = 0;
1519 localization = strtoul(buf, NULL, 10) << 16;
1520 pfsm = hammer_load_pseudofs(&trans, localization,
1521 &error);
1522 if (error == 0) {
1523 if (pfsm->pfsd.mirror_flags &
1524 HAMMER_PFSD_SLAVE) {
1525 /* vap->va_size == 26 */
1526 ksnprintf(buf, sizeof(buf),
1527 "@@0x%016llx:%05d",
1528 (long long)pfsm->pfsd.sync_end_tid,
1529 localization >> 16);
1530 } else {
1531 /* vap->va_size == 10 */
1532 ksnprintf(buf, sizeof(buf),
1533 "@@-1:%05d",
1534 localization >> 16);
1535 #if 0
1536 ksnprintf(buf, sizeof(buf),
1537 "@@0x%016llx:%05d",
1538 (long long)HAMMER_MAX_TID,
1539 localization >> 16);
1540 #endif
1542 ptr = buf;
1543 bytes = strlen(buf);
1545 if (pfsm)
1546 hammer_rel_pseudofs(trans.hmp, pfsm);
1547 hammer_done_transaction(&trans);
1549 error = uiomove(ptr, bytes, ap->a_uio);
1550 return(error);
1554 * Long version
1556 hammer_simple_transaction(&trans, ip->hmp);
1557 ++hammer_stats_file_iopsr;
1558 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1561 * Key range (begin and end inclusive) to scan. Directory keys
1562 * directly translate to a 64 bit 'seek' position.
1564 cursor.key_beg.localization = ip->obj_localization +
1565 HAMMER_LOCALIZE_MISC;
1566 cursor.key_beg.obj_id = ip->obj_id;
1567 cursor.key_beg.create_tid = 0;
1568 cursor.key_beg.delete_tid = 0;
1569 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1570 cursor.key_beg.obj_type = 0;
1571 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1572 cursor.asof = ip->obj_asof;
1573 cursor.flags |= HAMMER_CURSOR_ASOF;
1575 error = hammer_ip_lookup(&cursor);
1576 if (error == 0) {
1577 error = hammer_ip_resolve_data(&cursor);
1578 if (error == 0) {
1579 KKASSERT(cursor.leaf->data_len >=
1580 HAMMER_SYMLINK_NAME_OFF);
1581 error = uiomove(cursor.data->symlink.name,
1582 cursor.leaf->data_len -
1583 HAMMER_SYMLINK_NAME_OFF,
1584 ap->a_uio);
1587 hammer_done_cursor(&cursor);
1588 hammer_done_transaction(&trans);
1589 return(error);
1593 * hammer_vop_nremove { nch, dvp, cred }
1595 static
1597 hammer_vop_nremove(struct vop_nremove_args *ap)
1599 struct hammer_transaction trans;
1600 struct hammer_inode *dip;
1601 int error;
1603 dip = VTOI(ap->a_dvp);
1605 if (hammer_nohistory(dip) == 0 &&
1606 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1607 return (error);
1610 hammer_start_transaction(&trans, dip->hmp);
1611 ++hammer_stats_file_iopsw;
1612 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1613 hammer_done_transaction(&trans);
1614 if (error == 0)
1615 hammer_knote(ap->a_dvp, NOTE_WRITE);
1616 return (error);
1620 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1622 static
1624 hammer_vop_nrename(struct vop_nrename_args *ap)
1626 struct hammer_transaction trans;
1627 struct namecache *fncp;
1628 struct namecache *tncp;
1629 struct hammer_inode *fdip;
1630 struct hammer_inode *tdip;
1631 struct hammer_inode *ip;
1632 struct hammer_cursor cursor;
1633 int64_t namekey;
1634 u_int32_t max_iterations;
1635 int nlen, error;
1637 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1638 return(EXDEV);
1639 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1640 return(EXDEV);
1642 fdip = VTOI(ap->a_fdvp);
1643 tdip = VTOI(ap->a_tdvp);
1644 fncp = ap->a_fnch->ncp;
1645 tncp = ap->a_tnch->ncp;
1646 ip = VTOI(fncp->nc_vp);
1647 KKASSERT(ip != NULL);
1649 if (fdip->obj_localization != tdip->obj_localization)
1650 return(EXDEV);
1651 if (fdip->obj_localization != ip->obj_localization)
1652 return(EXDEV);
1654 if (fdip->flags & HAMMER_INODE_RO)
1655 return (EROFS);
1656 if (tdip->flags & HAMMER_INODE_RO)
1657 return (EROFS);
1658 if (ip->flags & HAMMER_INODE_RO)
1659 return (EROFS);
1660 if ((error = hammer_checkspace(fdip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1661 return (error);
1663 hammer_start_transaction(&trans, fdip->hmp);
1664 ++hammer_stats_file_iopsw;
1667 * Remove tncp from the target directory and then link ip as
1668 * tncp. XXX pass trans to dounlink
1670 * Force the inode sync-time to match the transaction so it is
1671 * in-sync with the creation of the target directory entry.
1673 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
1674 ap->a_cred, 0, -1);
1675 if (error == 0 || error == ENOENT) {
1676 error = hammer_ip_add_directory(&trans, tdip,
1677 tncp->nc_name, tncp->nc_nlen,
1678 ip);
1679 if (error == 0) {
1680 ip->ino_data.parent_obj_id = tdip->obj_id;
1681 ip->ino_data.ctime = trans.time;
1682 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
1685 if (error)
1686 goto failed; /* XXX */
1689 * Locate the record in the originating directory and remove it.
1691 * Calculate the namekey and setup the key range for the scan. This
1692 * works kinda like a chained hash table where the lower 32 bits
1693 * of the namekey synthesize the chain.
1695 * The key range is inclusive of both key_beg and key_end.
1697 namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
1698 &max_iterations);
1699 retry:
1700 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
1701 cursor.key_beg.localization = fdip->obj_localization +
1702 hammer_dir_localization(fdip);
1703 cursor.key_beg.obj_id = fdip->obj_id;
1704 cursor.key_beg.key = namekey;
1705 cursor.key_beg.create_tid = 0;
1706 cursor.key_beg.delete_tid = 0;
1707 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1708 cursor.key_beg.obj_type = 0;
1710 cursor.key_end = cursor.key_beg;
1711 cursor.key_end.key += max_iterations;
1712 cursor.asof = fdip->obj_asof;
1713 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1716 * Scan all matching records (the chain), locate the one matching
1717 * the requested path component.
1719 * The hammer_ip_*() functions merge in-memory records with on-disk
1720 * records for the purposes of the search.
1722 error = hammer_ip_first(&cursor);
1723 while (error == 0) {
1724 if (hammer_ip_resolve_data(&cursor) != 0)
1725 break;
1726 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
1727 KKASSERT(nlen > 0);
1728 if (fncp->nc_nlen == nlen &&
1729 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1730 break;
1732 error = hammer_ip_next(&cursor);
1736 * If all is ok we have to get the inode so we can adjust nlinks.
1738 * WARNING: hammer_ip_del_directory() may have to terminate the
1739 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1740 * twice.
1742 if (error == 0)
1743 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1746 * XXX A deadlock here will break rename's atomicy for the purposes
1747 * of crash recovery.
1749 if (error == EDEADLK) {
1750 hammer_done_cursor(&cursor);
1751 goto retry;
1755 * Cleanup and tell the kernel that the rename succeeded.
1757 hammer_done_cursor(&cursor);
1758 if (error == 0) {
1759 cache_rename(ap->a_fnch, ap->a_tnch);
1760 hammer_knote(ap->a_fdvp, NOTE_WRITE);
1761 hammer_knote(ap->a_tdvp, NOTE_WRITE);
1762 if (ip->vp)
1763 hammer_knote(ip->vp, NOTE_RENAME);
1766 failed:
1767 hammer_done_transaction(&trans);
1768 return (error);
1772 * hammer_vop_nrmdir { nch, dvp, cred }
1774 static
1776 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1778 struct hammer_transaction trans;
1779 struct hammer_inode *dip;
1780 int error;
1782 dip = VTOI(ap->a_dvp);
1784 if (hammer_nohistory(dip) == 0 &&
1785 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1786 return (error);
1789 hammer_start_transaction(&trans, dip->hmp);
1790 ++hammer_stats_file_iopsw;
1791 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
1792 hammer_done_transaction(&trans);
1793 if (error == 0)
1794 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1795 return (error);
1799 * hammer_vop_markatime { vp, cred }
1801 static
1803 hammer_vop_markatime(struct vop_markatime_args *ap)
1805 struct hammer_transaction trans;
1806 struct hammer_inode *ip;
1808 ip = VTOI(ap->a_vp);
1809 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1810 return (EROFS);
1811 if (ip->flags & HAMMER_INODE_RO)
1812 return (EROFS);
1813 if (ip->hmp->mp->mnt_flag & MNT_NOATIME)
1814 return (0);
1815 hammer_start_transaction(&trans, ip->hmp);
1816 ++hammer_stats_file_iopsw;
1818 ip->ino_data.atime = trans.time;
1819 hammer_modify_inode(ip, HAMMER_INODE_ATIME);
1820 hammer_done_transaction(&trans);
1821 hammer_knote(ap->a_vp, NOTE_ATTRIB);
1822 return (0);
1826 * hammer_vop_setattr { vp, vap, cred }
1828 static
1830 hammer_vop_setattr(struct vop_setattr_args *ap)
1832 struct hammer_transaction trans;
1833 struct vattr *vap;
1834 struct hammer_inode *ip;
1835 int modflags;
1836 int error;
1837 int truncating;
1838 int blksize;
1839 int kflags;
1840 int64_t aligned_size;
1841 u_int32_t flags;
1843 vap = ap->a_vap;
1844 ip = ap->a_vp->v_data;
1845 modflags = 0;
1846 kflags = 0;
1848 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1849 return(EROFS);
1850 if (ip->flags & HAMMER_INODE_RO)
1851 return (EROFS);
1852 if (hammer_nohistory(ip) == 0 &&
1853 (error = hammer_checkspace(ip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1854 return (error);
1857 hammer_start_transaction(&trans, ip->hmp);
1858 ++hammer_stats_file_iopsw;
1859 error = 0;
1861 if (vap->va_flags != VNOVAL) {
1862 flags = ip->ino_data.uflags;
1863 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1864 hammer_to_unix_xid(&ip->ino_data.uid),
1865 ap->a_cred);
1866 if (error == 0) {
1867 if (ip->ino_data.uflags != flags) {
1868 ip->ino_data.uflags = flags;
1869 ip->ino_data.ctime = trans.time;
1870 modflags |= HAMMER_INODE_DDIRTY;
1871 kflags |= NOTE_ATTRIB;
1873 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1874 error = 0;
1875 goto done;
1878 goto done;
1880 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1881 error = EPERM;
1882 goto done;
1884 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1885 mode_t cur_mode = ip->ino_data.mode;
1886 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1887 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1888 uuid_t uuid_uid;
1889 uuid_t uuid_gid;
1891 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
1892 ap->a_cred,
1893 &cur_uid, &cur_gid, &cur_mode);
1894 if (error == 0) {
1895 hammer_guid_to_uuid(&uuid_uid, cur_uid);
1896 hammer_guid_to_uuid(&uuid_gid, cur_gid);
1897 if (bcmp(&uuid_uid, &ip->ino_data.uid,
1898 sizeof(uuid_uid)) ||
1899 bcmp(&uuid_gid, &ip->ino_data.gid,
1900 sizeof(uuid_gid)) ||
1901 ip->ino_data.mode != cur_mode
1903 ip->ino_data.uid = uuid_uid;
1904 ip->ino_data.gid = uuid_gid;
1905 ip->ino_data.mode = cur_mode;
1906 ip->ino_data.ctime = trans.time;
1907 modflags |= HAMMER_INODE_DDIRTY;
1909 kflags |= NOTE_ATTRIB;
1912 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
1913 switch(ap->a_vp->v_type) {
1914 case VREG:
1915 if (vap->va_size == ip->ino_data.size)
1916 break;
1918 * XXX break atomicy, we can deadlock the backend
1919 * if we do not release the lock. Probably not a
1920 * big deal here.
1922 blksize = hammer_blocksize(vap->va_size);
1923 if (vap->va_size < ip->ino_data.size) {
1924 vtruncbuf(ap->a_vp, vap->va_size, blksize);
1925 truncating = 1;
1926 kflags |= NOTE_WRITE;
1927 } else {
1928 vnode_pager_setsize(ap->a_vp, vap->va_size);
1929 truncating = 0;
1930 kflags |= NOTE_WRITE | NOTE_EXTEND;
1932 ip->ino_data.size = vap->va_size;
1933 ip->ino_data.mtime = trans.time;
1934 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
1937 * on-media truncation is cached in the inode until
1938 * the inode is synchronized.
1940 if (truncating) {
1941 hammer_ip_frontend_trunc(ip, vap->va_size);
1942 #ifdef DEBUG_TRUNCATE
1943 if (HammerTruncIp == NULL)
1944 HammerTruncIp = ip;
1945 #endif
1946 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1947 ip->flags |= HAMMER_INODE_TRUNCATED;
1948 ip->trunc_off = vap->va_size;
1949 #ifdef DEBUG_TRUNCATE
1950 if (ip == HammerTruncIp)
1951 kprintf("truncate1 %016llx\n",
1952 (long long)ip->trunc_off);
1953 #endif
1954 } else if (ip->trunc_off > vap->va_size) {
1955 ip->trunc_off = vap->va_size;
1956 #ifdef DEBUG_TRUNCATE
1957 if (ip == HammerTruncIp)
1958 kprintf("truncate2 %016llx\n",
1959 (long long)ip->trunc_off);
1960 #endif
1961 } else {
1962 #ifdef DEBUG_TRUNCATE
1963 if (ip == HammerTruncIp)
1964 kprintf("truncate3 %016llx (ignored)\n",
1965 (long long)vap->va_size);
1966 #endif
1971 * If truncating we have to clean out a portion of
1972 * the last block on-disk. We do this in the
1973 * front-end buffer cache.
1975 aligned_size = (vap->va_size + (blksize - 1)) &
1976 ~(int64_t)(blksize - 1);
1977 if (truncating && vap->va_size < aligned_size) {
1978 struct buf *bp;
1979 int offset;
1981 aligned_size -= blksize;
1983 offset = (int)vap->va_size & (blksize - 1);
1984 error = bread(ap->a_vp, aligned_size,
1985 blksize, &bp);
1986 hammer_ip_frontend_trunc(ip, aligned_size);
1987 if (error == 0) {
1988 bzero(bp->b_data + offset,
1989 blksize - offset);
1990 /* must de-cache direct-io offset */
1991 bp->b_bio2.bio_offset = NOOFFSET;
1992 bdwrite(bp);
1993 } else {
1994 kprintf("ERROR %d\n", error);
1995 brelse(bp);
1998 break;
1999 case VDATABASE:
2000 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2001 ip->flags |= HAMMER_INODE_TRUNCATED;
2002 ip->trunc_off = vap->va_size;
2003 } else if (ip->trunc_off > vap->va_size) {
2004 ip->trunc_off = vap->va_size;
2006 hammer_ip_frontend_trunc(ip, vap->va_size);
2007 ip->ino_data.size = vap->va_size;
2008 ip->ino_data.mtime = trans.time;
2009 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2010 kflags |= NOTE_ATTRIB;
2011 break;
2012 default:
2013 error = EINVAL;
2014 goto done;
2016 break;
2018 if (vap->va_atime.tv_sec != VNOVAL) {
2019 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
2020 modflags |= HAMMER_INODE_ATIME;
2021 kflags |= NOTE_ATTRIB;
2023 if (vap->va_mtime.tv_sec != VNOVAL) {
2024 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
2025 modflags |= HAMMER_INODE_MTIME;
2026 kflags |= NOTE_ATTRIB;
2028 if (vap->va_mode != (mode_t)VNOVAL) {
2029 mode_t cur_mode = ip->ino_data.mode;
2030 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2031 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2033 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
2034 cur_uid, cur_gid, &cur_mode);
2035 if (error == 0 && ip->ino_data.mode != cur_mode) {
2036 ip->ino_data.mode = cur_mode;
2037 ip->ino_data.ctime = trans.time;
2038 modflags |= HAMMER_INODE_DDIRTY;
2039 kflags |= NOTE_ATTRIB;
2042 done:
2043 if (error == 0)
2044 hammer_modify_inode(ip, modflags);
2045 hammer_done_transaction(&trans);
2046 hammer_knote(ap->a_vp, kflags);
2047 return (error);
2051 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2053 static
2055 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
2057 struct hammer_transaction trans;
2058 struct hammer_inode *dip;
2059 struct hammer_inode *nip;
2060 struct nchandle *nch;
2061 hammer_record_t record;
2062 int error;
2063 int bytes;
2065 ap->a_vap->va_type = VLNK;
2067 nch = ap->a_nch;
2068 dip = VTOI(ap->a_dvp);
2070 if (dip->flags & HAMMER_INODE_RO)
2071 return (EROFS);
2072 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
2073 return (error);
2076 * Create a transaction to cover the operations we perform.
2078 hammer_start_transaction(&trans, dip->hmp);
2079 ++hammer_stats_file_iopsw;
2082 * Create a new filesystem object of the requested type. The
2083 * returned inode will be referenced but not locked.
2086 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2087 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2088 NULL, &nip);
2089 if (error) {
2090 hammer_done_transaction(&trans);
2091 *ap->a_vpp = NULL;
2092 return (error);
2096 * Add a record representing the symlink. symlink stores the link
2097 * as pure data, not a string, and is no \0 terminated.
2099 if (error == 0) {
2100 bytes = strlen(ap->a_target);
2102 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2103 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2104 } else {
2105 record = hammer_alloc_mem_record(nip, bytes);
2106 record->type = HAMMER_MEM_RECORD_GENERAL;
2108 record->leaf.base.localization = nip->obj_localization +
2109 HAMMER_LOCALIZE_MISC;
2110 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2111 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2112 record->leaf.data_len = bytes;
2113 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2114 bcopy(ap->a_target, record->data->symlink.name, bytes);
2115 error = hammer_ip_add_record(&trans, record);
2119 * Set the file size to the length of the link.
2121 if (error == 0) {
2122 nip->ino_data.size = bytes;
2123 hammer_modify_inode(nip, HAMMER_INODE_DDIRTY);
2126 if (error == 0)
2127 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
2128 nch->ncp->nc_nlen, nip);
2131 * Finish up.
2133 if (error) {
2134 hammer_rel_inode(nip, 0);
2135 *ap->a_vpp = NULL;
2136 } else {
2137 error = hammer_get_vnode(nip, ap->a_vpp);
2138 hammer_rel_inode(nip, 0);
2139 if (error == 0) {
2140 cache_setunresolved(ap->a_nch);
2141 cache_setvp(ap->a_nch, *ap->a_vpp);
2142 hammer_knote(ap->a_dvp, NOTE_WRITE);
2145 hammer_done_transaction(&trans);
2146 return (error);
2150 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2152 static
2154 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2156 struct hammer_transaction trans;
2157 struct hammer_inode *dip;
2158 int error;
2160 dip = VTOI(ap->a_dvp);
2162 if (hammer_nohistory(dip) == 0 &&
2163 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2164 return (error);
2167 hammer_start_transaction(&trans, dip->hmp);
2168 ++hammer_stats_file_iopsw;
2169 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2170 ap->a_cred, ap->a_flags, -1);
2171 hammer_done_transaction(&trans);
2173 return (error);
2177 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2179 static
2181 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2183 struct hammer_inode *ip = ap->a_vp->v_data;
2185 ++hammer_stats_file_iopsr;
2186 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
2187 ap->a_fflag, ap->a_cred));
2190 static
2192 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2194 struct mount *mp;
2195 int error;
2197 mp = ap->a_head.a_ops->head.vv_mount;
2199 switch(ap->a_op) {
2200 case MOUNTCTL_SET_EXPORT:
2201 if (ap->a_ctllen != sizeof(struct export_args))
2202 error = EINVAL;
2203 else
2204 error = hammer_vfs_export(mp, ap->a_op,
2205 (const struct export_args *)ap->a_ctl);
2206 break;
2207 default:
2208 error = vop_stdmountctl(ap);
2209 break;
2211 return(error);
2215 * hammer_vop_strategy { vp, bio }
2217 * Strategy call, used for regular file read & write only. Note that the
2218 * bp may represent a cluster.
2220 * To simplify operation and allow better optimizations in the future,
2221 * this code does not make any assumptions with regards to buffer alignment
2222 * or size.
2224 static
2226 hammer_vop_strategy(struct vop_strategy_args *ap)
2228 struct buf *bp;
2229 int error;
2231 bp = ap->a_bio->bio_buf;
2233 switch(bp->b_cmd) {
2234 case BUF_CMD_READ:
2235 error = hammer_vop_strategy_read(ap);
2236 break;
2237 case BUF_CMD_WRITE:
2238 error = hammer_vop_strategy_write(ap);
2239 break;
2240 default:
2241 bp->b_error = error = EINVAL;
2242 bp->b_flags |= B_ERROR;
2243 biodone(ap->a_bio);
2244 break;
2246 return (error);
2250 * Read from a regular file. Iterate the related records and fill in the
2251 * BIO/BUF. Gaps are zero-filled.
2253 * The support code in hammer_object.c should be used to deal with mixed
2254 * in-memory and on-disk records.
2256 * NOTE: Can be called from the cluster code with an oversized buf.
2258 * XXX atime update
2260 static
2262 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2264 struct hammer_transaction trans;
2265 struct hammer_inode *ip;
2266 struct hammer_inode *dip;
2267 struct hammer_cursor cursor;
2268 hammer_base_elm_t base;
2269 hammer_off_t disk_offset;
2270 struct bio *bio;
2271 struct bio *nbio;
2272 struct buf *bp;
2273 int64_t rec_offset;
2274 int64_t ran_end;
2275 int64_t tmp64;
2276 int error;
2277 int boff;
2278 int roff;
2279 int n;
2281 bio = ap->a_bio;
2282 bp = bio->bio_buf;
2283 ip = ap->a_vp->v_data;
2286 * The zone-2 disk offset may have been set by the cluster code via
2287 * a BMAP operation, or else should be NOOFFSET.
2289 * Checking the high bits for a match against zone-2 should suffice.
2291 nbio = push_bio(bio);
2292 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2293 HAMMER_ZONE_LARGE_DATA) {
2294 error = hammer_io_direct_read(ip->hmp, nbio, NULL);
2295 return (error);
2299 * Well, that sucked. Do it the hard way. If all the stars are
2300 * aligned we may still be able to issue a direct-read.
2302 hammer_simple_transaction(&trans, ip->hmp);
2303 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2306 * Key range (begin and end inclusive) to scan. Note that the key's
2307 * stored in the actual records represent BASE+LEN, not BASE. The
2308 * first record containing bio_offset will have a key > bio_offset.
2310 cursor.key_beg.localization = ip->obj_localization +
2311 HAMMER_LOCALIZE_MISC;
2312 cursor.key_beg.obj_id = ip->obj_id;
2313 cursor.key_beg.create_tid = 0;
2314 cursor.key_beg.delete_tid = 0;
2315 cursor.key_beg.obj_type = 0;
2316 cursor.key_beg.key = bio->bio_offset + 1;
2317 cursor.asof = ip->obj_asof;
2318 cursor.flags |= HAMMER_CURSOR_ASOF;
2320 cursor.key_end = cursor.key_beg;
2321 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2322 #if 0
2323 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2324 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2325 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2326 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2327 } else
2328 #endif
2330 ran_end = bio->bio_offset + bp->b_bufsize;
2331 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2332 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2333 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2334 if (tmp64 < ran_end)
2335 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2336 else
2337 cursor.key_end.key = ran_end + MAXPHYS + 1;
2339 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2341 error = hammer_ip_first(&cursor);
2342 boff = 0;
2344 while (error == 0) {
2346 * Get the base file offset of the record. The key for
2347 * data records is (base + bytes) rather then (base).
2349 base = &cursor.leaf->base;
2350 rec_offset = base->key - cursor.leaf->data_len;
2353 * Calculate the gap, if any, and zero-fill it.
2355 * n is the offset of the start of the record verses our
2356 * current seek offset in the bio.
2358 n = (int)(rec_offset - (bio->bio_offset + boff));
2359 if (n > 0) {
2360 if (n > bp->b_bufsize - boff)
2361 n = bp->b_bufsize - boff;
2362 bzero((char *)bp->b_data + boff, n);
2363 boff += n;
2364 n = 0;
2368 * Calculate the data offset in the record and the number
2369 * of bytes we can copy.
2371 * There are two degenerate cases. First, boff may already
2372 * be at bp->b_bufsize. Secondly, the data offset within
2373 * the record may exceed the record's size.
2375 roff = -n;
2376 rec_offset += roff;
2377 n = cursor.leaf->data_len - roff;
2378 if (n <= 0) {
2379 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2380 n = 0;
2381 } else if (n > bp->b_bufsize - boff) {
2382 n = bp->b_bufsize - boff;
2386 * Deal with cached truncations. This cool bit of code
2387 * allows truncate()/ftruncate() to avoid having to sync
2388 * the file.
2390 * If the frontend is truncated then all backend records are
2391 * subject to the frontend's truncation.
2393 * If the backend is truncated then backend records on-disk
2394 * (but not in-memory) are subject to the backend's
2395 * truncation. In-memory records owned by the backend
2396 * represent data written after the truncation point on the
2397 * backend and must not be truncated.
2399 * Truncate operations deal with frontend buffer cache
2400 * buffers and frontend-owned in-memory records synchronously.
2402 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2403 if (hammer_cursor_ondisk(&cursor) ||
2404 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2405 if (ip->trunc_off <= rec_offset)
2406 n = 0;
2407 else if (ip->trunc_off < rec_offset + n)
2408 n = (int)(ip->trunc_off - rec_offset);
2411 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2412 if (hammer_cursor_ondisk(&cursor)) {
2413 if (ip->sync_trunc_off <= rec_offset)
2414 n = 0;
2415 else if (ip->sync_trunc_off < rec_offset + n)
2416 n = (int)(ip->sync_trunc_off - rec_offset);
2421 * Try to issue a direct read into our bio if possible,
2422 * otherwise resolve the element data into a hammer_buffer
2423 * and copy.
2425 * The buffer on-disk should be zerod past any real
2426 * truncation point, but may not be for any synthesized
2427 * truncation point from above.
2429 disk_offset = cursor.leaf->data_offset + roff;
2430 if (boff == 0 && n == bp->b_bufsize &&
2431 hammer_cursor_ondisk(&cursor) &&
2432 (disk_offset & HAMMER_BUFMASK) == 0) {
2433 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2434 HAMMER_ZONE_LARGE_DATA);
2435 nbio->bio_offset = disk_offset;
2436 error = hammer_io_direct_read(trans.hmp, nbio,
2437 cursor.leaf);
2438 goto done;
2439 } else if (n) {
2440 error = hammer_ip_resolve_data(&cursor);
2441 if (error == 0) {
2442 bcopy((char *)cursor.data + roff,
2443 (char *)bp->b_data + boff, n);
2446 if (error)
2447 break;
2450 * Iterate until we have filled the request.
2452 boff += n;
2453 if (boff == bp->b_bufsize)
2454 break;
2455 error = hammer_ip_next(&cursor);
2459 * There may have been a gap after the last record
2461 if (error == ENOENT)
2462 error = 0;
2463 if (error == 0 && boff != bp->b_bufsize) {
2464 KKASSERT(boff < bp->b_bufsize);
2465 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2466 /* boff = bp->b_bufsize; */
2468 bp->b_resid = 0;
2469 bp->b_error = error;
2470 if (error)
2471 bp->b_flags |= B_ERROR;
2472 biodone(ap->a_bio);
2474 done:
2476 * Cache the b-tree node for the last data read in cache[1].
2478 * If we hit the file EOF then also cache the node in the
2479 * governing director's cache[3], it will be used to initialize
2480 * the inode's cache[1] for any inodes looked up via the directory.
2482 * This doesn't reduce disk accesses since the B-Tree chain is
2483 * likely cached, but it does reduce cpu overhead when looking
2484 * up file offsets for cpdup/tar/cpio style iterations.
2486 if (cursor.node)
2487 hammer_cache_node(&ip->cache[1], cursor.node);
2488 if (ran_end >= ip->ino_data.size) {
2489 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
2490 ip->obj_asof, ip->obj_localization);
2491 if (dip) {
2492 hammer_cache_node(&dip->cache[3], cursor.node);
2493 hammer_rel_inode(dip, 0);
2496 hammer_done_cursor(&cursor);
2497 hammer_done_transaction(&trans);
2498 return(error);
2502 * BMAP operation - used to support cluster_read() only.
2504 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2506 * This routine may return EOPNOTSUPP if the opration is not supported for
2507 * the specified offset. The contents of the pointer arguments do not
2508 * need to be initialized in that case.
2510 * If a disk address is available and properly aligned return 0 with
2511 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2512 * to the run-length relative to that offset. Callers may assume that
2513 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2514 * large, so return EOPNOTSUPP if it is not sufficiently large.
2516 static
2518 hammer_vop_bmap(struct vop_bmap_args *ap)
2520 struct hammer_transaction trans;
2521 struct hammer_inode *ip;
2522 struct hammer_cursor cursor;
2523 hammer_base_elm_t base;
2524 int64_t rec_offset;
2525 int64_t ran_end;
2526 int64_t tmp64;
2527 int64_t base_offset;
2528 int64_t base_disk_offset;
2529 int64_t last_offset;
2530 hammer_off_t last_disk_offset;
2531 hammer_off_t disk_offset;
2532 int rec_len;
2533 int error;
2534 int blksize;
2536 ++hammer_stats_file_iopsr;
2537 ip = ap->a_vp->v_data;
2540 * We can only BMAP regular files. We can't BMAP database files,
2541 * directories, etc.
2543 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2544 return(EOPNOTSUPP);
2547 * bmap is typically called with runp/runb both NULL when used
2548 * for writing. We do not support BMAP for writing atm.
2550 if (ap->a_cmd != BUF_CMD_READ)
2551 return(EOPNOTSUPP);
2554 * Scan the B-Tree to acquire blockmap addresses, then translate
2555 * to raw addresses.
2557 hammer_simple_transaction(&trans, ip->hmp);
2558 #if 0
2559 kprintf("bmap_beg %016llx ip->cache %p\n",
2560 (long long)ap->a_loffset, ip->cache[1]);
2561 #endif
2562 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2565 * Key range (begin and end inclusive) to scan. Note that the key's
2566 * stored in the actual records represent BASE+LEN, not BASE. The
2567 * first record containing bio_offset will have a key > bio_offset.
2569 cursor.key_beg.localization = ip->obj_localization +
2570 HAMMER_LOCALIZE_MISC;
2571 cursor.key_beg.obj_id = ip->obj_id;
2572 cursor.key_beg.create_tid = 0;
2573 cursor.key_beg.delete_tid = 0;
2574 cursor.key_beg.obj_type = 0;
2575 if (ap->a_runb)
2576 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
2577 else
2578 cursor.key_beg.key = ap->a_loffset + 1;
2579 if (cursor.key_beg.key < 0)
2580 cursor.key_beg.key = 0;
2581 cursor.asof = ip->obj_asof;
2582 cursor.flags |= HAMMER_CURSOR_ASOF;
2584 cursor.key_end = cursor.key_beg;
2585 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2587 ran_end = ap->a_loffset + MAXPHYS;
2588 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2589 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2590 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2591 if (tmp64 < ran_end)
2592 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2593 else
2594 cursor.key_end.key = ran_end + MAXPHYS + 1;
2596 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2598 error = hammer_ip_first(&cursor);
2599 base_offset = last_offset = 0;
2600 base_disk_offset = last_disk_offset = 0;
2602 while (error == 0) {
2604 * Get the base file offset of the record. The key for
2605 * data records is (base + bytes) rather then (base).
2607 * NOTE: rec_offset + rec_len may exceed the end-of-file.
2608 * The extra bytes should be zero on-disk and the BMAP op
2609 * should still be ok.
2611 base = &cursor.leaf->base;
2612 rec_offset = base->key - cursor.leaf->data_len;
2613 rec_len = cursor.leaf->data_len;
2616 * Incorporate any cached truncation.
2618 * NOTE: Modifications to rec_len based on synthesized
2619 * truncation points remove the guarantee that any extended
2620 * data on disk is zero (since the truncations may not have
2621 * taken place on-media yet).
2623 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2624 if (hammer_cursor_ondisk(&cursor) ||
2625 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2626 if (ip->trunc_off <= rec_offset)
2627 rec_len = 0;
2628 else if (ip->trunc_off < rec_offset + rec_len)
2629 rec_len = (int)(ip->trunc_off - rec_offset);
2632 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2633 if (hammer_cursor_ondisk(&cursor)) {
2634 if (ip->sync_trunc_off <= rec_offset)
2635 rec_len = 0;
2636 else if (ip->sync_trunc_off < rec_offset + rec_len)
2637 rec_len = (int)(ip->sync_trunc_off - rec_offset);
2642 * Accumulate information. If we have hit a discontiguous
2643 * block reset base_offset unless we are already beyond the
2644 * requested offset. If we are, that's it, we stop.
2646 if (error)
2647 break;
2648 if (hammer_cursor_ondisk(&cursor)) {
2649 disk_offset = cursor.leaf->data_offset;
2650 if (rec_offset != last_offset ||
2651 disk_offset != last_disk_offset) {
2652 if (rec_offset > ap->a_loffset)
2653 break;
2654 base_offset = rec_offset;
2655 base_disk_offset = disk_offset;
2657 last_offset = rec_offset + rec_len;
2658 last_disk_offset = disk_offset + rec_len;
2660 error = hammer_ip_next(&cursor);
2663 #if 0
2664 kprintf("BMAP %016llx: %016llx - %016llx\n",
2665 (long long)ap->a_loffset,
2666 (long long)base_offset,
2667 (long long)last_offset);
2668 kprintf("BMAP %16s: %016llx - %016llx\n", "",
2669 (long long)base_disk_offset,
2670 (long long)last_disk_offset);
2671 #endif
2673 if (cursor.node) {
2674 hammer_cache_node(&ip->cache[1], cursor.node);
2675 #if 0
2676 kprintf("bmap_end2 %016llx ip->cache %p\n",
2677 (long long)ap->a_loffset, ip->cache[1]);
2678 #endif
2680 hammer_done_cursor(&cursor);
2681 hammer_done_transaction(&trans);
2684 * If we couldn't find any records or the records we did find were
2685 * all behind the requested offset, return failure. A forward
2686 * truncation can leave a hole w/ no on-disk records.
2688 if (last_offset == 0 || last_offset < ap->a_loffset)
2689 return (EOPNOTSUPP);
2692 * Figure out the block size at the requested offset and adjust
2693 * our limits so the cluster_read() does not create inappropriately
2694 * sized buffer cache buffers.
2696 blksize = hammer_blocksize(ap->a_loffset);
2697 if (hammer_blocksize(base_offset) != blksize) {
2698 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
2700 if (last_offset != ap->a_loffset &&
2701 hammer_blocksize(last_offset - 1) != blksize) {
2702 last_offset = hammer_blockdemarc(ap->a_loffset,
2703 last_offset - 1);
2707 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
2708 * from occuring.
2710 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
2712 if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
2714 * Only large-data zones can be direct-IOd
2716 error = EOPNOTSUPP;
2717 } else if ((disk_offset & HAMMER_BUFMASK) ||
2718 (last_offset - ap->a_loffset) < blksize) {
2720 * doffsetp is not aligned or the forward run size does
2721 * not cover a whole buffer, disallow the direct I/O.
2723 error = EOPNOTSUPP;
2724 } else {
2726 * We're good.
2728 *ap->a_doffsetp = disk_offset;
2729 if (ap->a_runb) {
2730 *ap->a_runb = ap->a_loffset - base_offset;
2731 KKASSERT(*ap->a_runb >= 0);
2733 if (ap->a_runp) {
2734 *ap->a_runp = last_offset - ap->a_loffset;
2735 KKASSERT(*ap->a_runp >= 0);
2737 error = 0;
2739 return(error);
2743 * Write to a regular file. Because this is a strategy call the OS is
2744 * trying to actually get data onto the media.
2746 static
2748 hammer_vop_strategy_write(struct vop_strategy_args *ap)
2750 hammer_record_t record;
2751 hammer_mount_t hmp;
2752 hammer_inode_t ip;
2753 struct bio *bio;
2754 struct buf *bp;
2755 int blksize;
2756 int bytes;
2757 int error;
2759 bio = ap->a_bio;
2760 bp = bio->bio_buf;
2761 ip = ap->a_vp->v_data;
2762 hmp = ip->hmp;
2764 blksize = hammer_blocksize(bio->bio_offset);
2765 KKASSERT(bp->b_bufsize == blksize);
2767 if (ip->flags & HAMMER_INODE_RO) {
2768 bp->b_error = EROFS;
2769 bp->b_flags |= B_ERROR;
2770 biodone(ap->a_bio);
2771 return(EROFS);
2775 * Interlock with inode destruction (no in-kernel or directory
2776 * topology visibility). If we queue new IO while trying to
2777 * destroy the inode we can deadlock the vtrunc call in
2778 * hammer_inode_unloadable_check().
2780 * Besides, there's no point flushing a bp associated with an
2781 * inode that is being destroyed on-media and has no kernel
2782 * references.
2784 if ((ip->flags | ip->sync_flags) &
2785 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
2786 bp->b_resid = 0;
2787 biodone(ap->a_bio);
2788 return(0);
2792 * Reserve space and issue a direct-write from the front-end.
2793 * NOTE: The direct_io code will hammer_bread/bcopy smaller
2794 * allocations.
2796 * An in-memory record will be installed to reference the storage
2797 * until the flusher can get to it.
2799 * Since we own the high level bio the front-end will not try to
2800 * do a direct-read until the write completes.
2802 * NOTE: The only time we do not reserve a full-sized buffers
2803 * worth of data is if the file is small. We do not try to
2804 * allocate a fragment (from the small-data zone) at the end of
2805 * an otherwise large file as this can lead to wildly separated
2806 * data.
2808 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
2809 KKASSERT(bio->bio_offset < ip->ino_data.size);
2810 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
2811 bytes = bp->b_bufsize;
2812 else
2813 bytes = ((int)ip->ino_data.size + 15) & ~15;
2815 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
2816 bytes, &error);
2817 if (record) {
2818 hammer_io_direct_write(hmp, record, bio);
2819 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
2820 hammer_flush_inode(ip, 0);
2821 } else {
2822 bp->b_bio2.bio_offset = NOOFFSET;
2823 bp->b_error = error;
2824 bp->b_flags |= B_ERROR;
2825 biodone(ap->a_bio);
2827 return(error);
2831 * dounlink - disconnect a directory entry
2833 * XXX whiteout support not really in yet
2835 static int
2836 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
2837 struct vnode *dvp, struct ucred *cred,
2838 int flags, int isdir)
2840 struct namecache *ncp;
2841 hammer_inode_t dip;
2842 hammer_inode_t ip;
2843 struct hammer_cursor cursor;
2844 int64_t namekey;
2845 u_int32_t max_iterations;
2846 int nlen, error;
2849 * Calculate the namekey and setup the key range for the scan. This
2850 * works kinda like a chained hash table where the lower 32 bits
2851 * of the namekey synthesize the chain.
2853 * The key range is inclusive of both key_beg and key_end.
2855 dip = VTOI(dvp);
2856 ncp = nch->ncp;
2858 if (dip->flags & HAMMER_INODE_RO)
2859 return (EROFS);
2861 namekey = hammer_directory_namekey(dip, ncp->nc_name, ncp->nc_nlen,
2862 &max_iterations);
2863 retry:
2864 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
2865 cursor.key_beg.localization = dip->obj_localization +
2866 hammer_dir_localization(dip);
2867 cursor.key_beg.obj_id = dip->obj_id;
2868 cursor.key_beg.key = namekey;
2869 cursor.key_beg.create_tid = 0;
2870 cursor.key_beg.delete_tid = 0;
2871 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2872 cursor.key_beg.obj_type = 0;
2874 cursor.key_end = cursor.key_beg;
2875 cursor.key_end.key += max_iterations;
2876 cursor.asof = dip->obj_asof;
2877 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2880 * Scan all matching records (the chain), locate the one matching
2881 * the requested path component. info->last_error contains the
2882 * error code on search termination and could be 0, ENOENT, or
2883 * something else.
2885 * The hammer_ip_*() functions merge in-memory records with on-disk
2886 * records for the purposes of the search.
2888 error = hammer_ip_first(&cursor);
2890 while (error == 0) {
2891 error = hammer_ip_resolve_data(&cursor);
2892 if (error)
2893 break;
2894 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2895 KKASSERT(nlen > 0);
2896 if (ncp->nc_nlen == nlen &&
2897 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2898 break;
2900 error = hammer_ip_next(&cursor);
2904 * If all is ok we have to get the inode so we can adjust nlinks.
2905 * To avoid a deadlock with the flusher we must release the inode
2906 * lock on the directory when acquiring the inode for the entry.
2908 * If the target is a directory, it must be empty.
2910 if (error == 0) {
2911 hammer_unlock(&cursor.ip->lock);
2912 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
2913 dip->hmp->asof,
2914 cursor.data->entry.localization,
2915 0, &error);
2916 hammer_lock_sh(&cursor.ip->lock);
2917 if (error == ENOENT) {
2918 kprintf("HAMMER: WARNING: Removing "
2919 "dirent w/missing inode \"%s\"\n"
2920 "\tobj_id = %016llx\n",
2921 ncp->nc_name,
2922 (long long)cursor.data->entry.obj_id);
2923 error = 0;
2927 * If isdir >= 0 we validate that the entry is or is not a
2928 * directory. If isdir < 0 we don't care.
2930 if (error == 0 && isdir >= 0 && ip) {
2931 if (isdir &&
2932 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
2933 error = ENOTDIR;
2934 } else if (isdir == 0 &&
2935 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
2936 error = EISDIR;
2941 * If we are trying to remove a directory the directory must
2942 * be empty.
2944 * The check directory code can loop and deadlock/retry. Our
2945 * own cursor's node locks must be released to avoid a 3-way
2946 * deadlock with the flusher if the check directory code
2947 * blocks.
2949 * If any changes whatsoever have been made to the cursor
2950 * set EDEADLK and retry.
2952 if (error == 0 && ip && ip->ino_data.obj_type ==
2953 HAMMER_OBJTYPE_DIRECTORY) {
2954 hammer_unlock_cursor(&cursor);
2955 error = hammer_ip_check_directory_empty(trans, ip);
2956 hammer_lock_cursor(&cursor);
2957 if (cursor.flags & HAMMER_CURSOR_RETEST) {
2958 kprintf("HAMMER: Warning: avoided deadlock "
2959 "on rmdir '%s'\n",
2960 ncp->nc_name);
2961 error = EDEADLK;
2966 * Delete the directory entry.
2968 * WARNING: hammer_ip_del_directory() may have to terminate
2969 * the cursor to avoid a deadlock. It is ok to call
2970 * hammer_done_cursor() twice.
2972 if (error == 0) {
2973 error = hammer_ip_del_directory(trans, &cursor,
2974 dip, ip);
2976 hammer_done_cursor(&cursor);
2977 if (error == 0) {
2978 cache_setunresolved(nch);
2979 cache_setvp(nch, NULL);
2980 /* XXX locking */
2981 if (ip && ip->vp) {
2982 hammer_knote(ip->vp, NOTE_DELETE);
2983 cache_inval_vp(ip->vp, CINV_DESTROY);
2986 if (ip)
2987 hammer_rel_inode(ip, 0);
2988 } else {
2989 hammer_done_cursor(&cursor);
2991 if (error == EDEADLK)
2992 goto retry;
2994 return (error);
2997 /************************************************************************
2998 * FIFO AND SPECFS OPS *
2999 ************************************************************************
3003 static int
3004 hammer_vop_fifoclose (struct vop_close_args *ap)
3006 /* XXX update itimes */
3007 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3010 static int
3011 hammer_vop_fiforead (struct vop_read_args *ap)
3013 int error;
3015 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3016 /* XXX update access time */
3017 return (error);
3020 static int
3021 hammer_vop_fifowrite (struct vop_write_args *ap)
3023 int error;
3025 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3026 /* XXX update access time */
3027 return (error);
3030 static
3032 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
3034 int error;
3036 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3037 if (error)
3038 error = hammer_vop_kqfilter(ap);
3039 return(error);
3042 /************************************************************************
3043 * KQFILTER OPS *
3044 ************************************************************************
3047 static void filt_hammerdetach(struct knote *kn);
3048 static int filt_hammerread(struct knote *kn, long hint);
3049 static int filt_hammerwrite(struct knote *kn, long hint);
3050 static int filt_hammervnode(struct knote *kn, long hint);
3052 static struct filterops hammerread_filtops =
3053 { 1, NULL, filt_hammerdetach, filt_hammerread };
3054 static struct filterops hammerwrite_filtops =
3055 { 1, NULL, filt_hammerdetach, filt_hammerwrite };
3056 static struct filterops hammervnode_filtops =
3057 { 1, NULL, filt_hammerdetach, filt_hammervnode };
3059 static
3061 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
3063 struct vnode *vp = ap->a_vp;
3064 struct knote *kn = ap->a_kn;
3065 lwkt_tokref vlock;
3067 switch (kn->kn_filter) {
3068 case EVFILT_READ:
3069 kn->kn_fop = &hammerread_filtops;
3070 break;
3071 case EVFILT_WRITE:
3072 kn->kn_fop = &hammerwrite_filtops;
3073 break;
3074 case EVFILT_VNODE:
3075 kn->kn_fop = &hammervnode_filtops;
3076 break;
3077 default:
3078 return (1);
3081 kn->kn_hook = (caddr_t)vp;
3083 lwkt_gettoken(&vlock, &vp->v_token);
3084 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
3085 lwkt_reltoken(&vlock);
3087 return(0);
3090 static void
3091 filt_hammerdetach(struct knote *kn)
3093 struct vnode *vp = (void *)kn->kn_hook;
3094 lwkt_tokref vlock;
3096 lwkt_gettoken(&vlock, &vp->v_token);
3097 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
3098 kn, knote, kn_selnext);
3099 lwkt_reltoken(&vlock);
3102 static int
3103 filt_hammerread(struct knote *kn, long hint)
3105 struct vnode *vp = (void *)kn->kn_hook;
3106 hammer_inode_t ip = VTOI(vp);
3108 if (hint == NOTE_REVOKE) {
3109 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3110 return(1);
3112 kn->kn_data = ip->ino_data.size - kn->kn_fp->f_offset;
3113 return (kn->kn_data != 0);
3116 static int
3117 filt_hammerwrite(struct knote *kn, long hint)
3119 if (hint == NOTE_REVOKE)
3120 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3121 kn->kn_data = 0;
3122 return (1);
3125 static int
3126 filt_hammervnode(struct knote *kn, long hint)
3128 if (kn->kn_sfflags & hint)
3129 kn->kn_fflags |= hint;
3130 if (hint == NOTE_REVOKE) {
3131 kn->kn_flags |= EV_EOF;
3132 return (1);
3134 return (kn->kn_fflags != 0);