HAMMER VFS - Do a better job with IO_DIRECT
[dragonfly.git] / sys / vfs / hammer / hammer_vnops.c
blob77691a5d49661e3bb02ecf788c3050630fd2e385
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>
51 #include <sys/mplock2.h>
53 #include "hammer.h"
56 * USERFS VNOPS
58 /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
59 static int hammer_vop_fsync(struct vop_fsync_args *);
60 static int hammer_vop_read(struct vop_read_args *);
61 static int hammer_vop_write(struct vop_write_args *);
62 static int hammer_vop_access(struct vop_access_args *);
63 static int hammer_vop_advlock(struct vop_advlock_args *);
64 static int hammer_vop_close(struct vop_close_args *);
65 static int hammer_vop_ncreate(struct vop_ncreate_args *);
66 static int hammer_vop_getattr(struct vop_getattr_args *);
67 static int hammer_vop_nresolve(struct vop_nresolve_args *);
68 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
69 static int hammer_vop_nlink(struct vop_nlink_args *);
70 static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
71 static int hammer_vop_nmknod(struct vop_nmknod_args *);
72 static int hammer_vop_open(struct vop_open_args *);
73 static int hammer_vop_print(struct vop_print_args *);
74 static int hammer_vop_readdir(struct vop_readdir_args *);
75 static int hammer_vop_readlink(struct vop_readlink_args *);
76 static int hammer_vop_nremove(struct vop_nremove_args *);
77 static int hammer_vop_nrename(struct vop_nrename_args *);
78 static int hammer_vop_nrmdir(struct vop_nrmdir_args *);
79 static int hammer_vop_markatime(struct vop_markatime_args *);
80 static int hammer_vop_setattr(struct vop_setattr_args *);
81 static int hammer_vop_strategy(struct vop_strategy_args *);
82 static int hammer_vop_bmap(struct vop_bmap_args *ap);
83 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
84 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
85 static int hammer_vop_ioctl(struct vop_ioctl_args *);
86 static int hammer_vop_mountctl(struct vop_mountctl_args *);
87 static int hammer_vop_kqfilter (struct vop_kqfilter_args *);
89 static int hammer_vop_fifoclose (struct vop_close_args *);
90 static int hammer_vop_fiforead (struct vop_read_args *);
91 static int hammer_vop_fifowrite (struct vop_write_args *);
92 static int hammer_vop_fifokqfilter (struct vop_kqfilter_args *);
94 struct vop_ops hammer_vnode_vops = {
95 .vop_default = vop_defaultop,
96 .vop_fsync = hammer_vop_fsync,
97 .vop_getpages = vop_stdgetpages,
98 .vop_putpages = vop_stdputpages,
99 .vop_read = hammer_vop_read,
100 .vop_write = hammer_vop_write,
101 .vop_access = hammer_vop_access,
102 .vop_advlock = hammer_vop_advlock,
103 .vop_close = hammer_vop_close,
104 .vop_ncreate = hammer_vop_ncreate,
105 .vop_getattr = hammer_vop_getattr,
106 .vop_inactive = hammer_vop_inactive,
107 .vop_reclaim = hammer_vop_reclaim,
108 .vop_nresolve = hammer_vop_nresolve,
109 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
110 .vop_nlink = hammer_vop_nlink,
111 .vop_nmkdir = hammer_vop_nmkdir,
112 .vop_nmknod = hammer_vop_nmknod,
113 .vop_open = hammer_vop_open,
114 .vop_pathconf = vop_stdpathconf,
115 .vop_print = hammer_vop_print,
116 .vop_readdir = hammer_vop_readdir,
117 .vop_readlink = hammer_vop_readlink,
118 .vop_nremove = hammer_vop_nremove,
119 .vop_nrename = hammer_vop_nrename,
120 .vop_nrmdir = hammer_vop_nrmdir,
121 .vop_markatime = hammer_vop_markatime,
122 .vop_setattr = hammer_vop_setattr,
123 .vop_bmap = hammer_vop_bmap,
124 .vop_strategy = hammer_vop_strategy,
125 .vop_nsymlink = hammer_vop_nsymlink,
126 .vop_nwhiteout = hammer_vop_nwhiteout,
127 .vop_ioctl = hammer_vop_ioctl,
128 .vop_mountctl = hammer_vop_mountctl,
129 .vop_kqfilter = hammer_vop_kqfilter
132 struct vop_ops hammer_spec_vops = {
133 .vop_default = vop_defaultop,
134 .vop_fsync = hammer_vop_fsync,
135 .vop_read = vop_stdnoread,
136 .vop_write = vop_stdnowrite,
137 .vop_access = hammer_vop_access,
138 .vop_close = hammer_vop_close,
139 .vop_markatime = hammer_vop_markatime,
140 .vop_getattr = hammer_vop_getattr,
141 .vop_inactive = hammer_vop_inactive,
142 .vop_reclaim = hammer_vop_reclaim,
143 .vop_setattr = hammer_vop_setattr
146 struct vop_ops hammer_fifo_vops = {
147 .vop_default = fifo_vnoperate,
148 .vop_fsync = hammer_vop_fsync,
149 .vop_read = hammer_vop_fiforead,
150 .vop_write = hammer_vop_fifowrite,
151 .vop_access = hammer_vop_access,
152 .vop_close = hammer_vop_fifoclose,
153 .vop_markatime = hammer_vop_markatime,
154 .vop_getattr = hammer_vop_getattr,
155 .vop_inactive = hammer_vop_inactive,
156 .vop_reclaim = hammer_vop_reclaim,
157 .vop_setattr = hammer_vop_setattr,
158 .vop_kqfilter = hammer_vop_fifokqfilter
161 static __inline
162 void
163 hammer_knote(struct vnode *vp, int flags)
165 if (flags)
166 KNOTE(&vp->v_pollinfo.vpi_selinfo.si_note, flags);
169 #ifdef DEBUG_TRUNCATE
170 struct hammer_inode *HammerTruncIp;
171 #endif
173 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
174 struct vnode *dvp, struct ucred *cred,
175 int flags, int isdir);
176 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
177 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
179 #if 0
180 static
182 hammer_vop_vnoperate(struct vop_generic_args *)
184 return (VOCALL(&hammer_vnode_vops, ap));
186 #endif
189 * hammer_vop_fsync { vp, waitfor }
191 * fsync() an inode to disk and wait for it to be completely committed
192 * such that the information would not be undone if a crash occured after
193 * return.
195 * NOTE: HAMMER's fsync()'s are going to remain expensive until we implement
196 * a REDO log. A sysctl is provided to relax HAMMER's fsync()
197 * operation.
199 * Ultimately the combination of a REDO log and use of fast storage
200 * to front-end cluster caches will make fsync fast, but it aint
201 * here yet. And, in anycase, we need real transactional
202 * all-or-nothing features which are not restricted to a single file.
204 static
206 hammer_vop_fsync(struct vop_fsync_args *ap)
208 hammer_inode_t ip = VTOI(ap->a_vp);
209 hammer_mount_t hmp = ip->hmp;
210 int waitfor = ap->a_waitfor;
211 int mode;
214 * Fsync rule relaxation (default is either full synchronous flush
215 * or REDO semantics with synchronous flush).
217 if (ap->a_flags & VOP_FSYNC_SYSCALL) {
218 switch(hammer_fsync_mode) {
219 case 0:
220 mode0:
221 /* no REDO, full synchronous flush */
222 goto skip;
223 case 1:
224 mode1:
225 /* no REDO, full asynchronous flush */
226 if (waitfor == MNT_WAIT)
227 waitfor = MNT_NOWAIT;
228 goto skip;
229 case 2:
230 /* REDO semantics, synchronous flush */
231 if (hmp->version < HAMMER_VOL_VERSION_FOUR)
232 goto mode0;
233 mode = HAMMER_FLUSH_UNDOS_AUTO;
234 break;
235 case 3:
236 /* REDO semantics, relaxed asynchronous flush */
237 if (hmp->version < HAMMER_VOL_VERSION_FOUR)
238 goto mode1;
239 mode = HAMMER_FLUSH_UNDOS_RELAXED;
240 if (waitfor == MNT_WAIT)
241 waitfor = MNT_NOWAIT;
242 break;
243 case 4:
244 /* ignore the fsync() system call */
245 return(0);
246 default:
247 /* we have to do something */
248 mode = HAMMER_FLUSH_UNDOS_RELAXED;
249 if (waitfor == MNT_WAIT)
250 waitfor = MNT_NOWAIT;
251 break;
255 * Fast fsync only needs to flush the UNDO/REDO fifo if
256 * HAMMER_INODE_REDO is non-zero and the only modifications
257 * made to the file are write or write-extends.
259 if ((ip->flags & HAMMER_INODE_REDO) &&
260 (ip->flags & HAMMER_INODE_MODMASK_NOREDO) == 0
262 ++hammer_count_fsyncs;
263 hammer_flusher_flush_undos(hmp, mode);
264 ip->redo_count = 0;
265 return(0);
269 * REDO is enabled by fsync(), the idea being we really only
270 * want to lay down REDO records when programs are using
271 * fsync() heavily. The first fsync() on the file starts
272 * the gravy train going and later fsync()s keep it hot by
273 * resetting the redo_count.
275 * We weren't running REDOs before now so we have to fall
276 * through and do a full fsync of what we have.
278 if (hmp->version >= HAMMER_VOL_VERSION_FOUR) {
279 ip->flags |= HAMMER_INODE_REDO;
280 ip->redo_count = 0;
283 skip:
286 * Do a full flush sequence.
288 ++hammer_count_fsyncs;
289 vfsync(ap->a_vp, waitfor, 1, NULL, NULL);
290 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
291 if (waitfor == MNT_WAIT) {
292 vn_unlock(ap->a_vp);
293 hammer_wait_inode(ip);
294 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
296 return (ip->error);
300 * hammer_vop_read { vp, uio, ioflag, cred }
302 * MPALMOSTSAFE
304 static
306 hammer_vop_read(struct vop_read_args *ap)
308 struct hammer_transaction trans;
309 hammer_inode_t ip;
310 off_t offset;
311 struct buf *bp;
312 struct uio *uio;
313 int error;
314 int n;
315 int seqcount;
316 int ioseqcount;
317 int blksize;
318 int got_mplock;
319 int bigread;
321 if (ap->a_vp->v_type != VREG)
322 return (EINVAL);
323 ip = VTOI(ap->a_vp);
324 error = 0;
325 uio = ap->a_uio;
328 * Allow the UIO's size to override the sequential heuristic.
330 blksize = hammer_blocksize(uio->uio_offset);
331 seqcount = (uio->uio_resid + (blksize - 1)) / blksize;
332 ioseqcount = ap->a_ioflag >> 16;
333 if (seqcount < ioseqcount)
334 seqcount = ioseqcount;
337 * Temporary hack until more of HAMMER can be made MPSAFE.
339 #ifdef SMP
340 if (curthread->td_mpcount) {
341 got_mplock = -1;
342 hammer_start_transaction(&trans, ip->hmp);
343 } else {
344 got_mplock = 0;
346 #else
347 hammer_start_transaction(&trans, ip->hmp);
348 got_mplock = -1;
349 #endif
352 * If reading or writing a huge amount of data we have to break
353 * atomicy and allow the operation to be interrupted by a signal
354 * or it can DOS the machine.
356 bigread = (uio->uio_resid > 100 * 1024 * 1024);
359 * Access the data typically in HAMMER_BUFSIZE blocks via the
360 * buffer cache, but HAMMER may use a variable block size based
361 * on the offset.
363 * XXX Temporary hack, delay the start transaction while we remain
364 * MPSAFE. NOTE: ino_data.size cannot change while vnode is
365 * locked-shared.
367 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
368 int64_t base_offset;
369 int64_t file_limit;
371 blksize = hammer_blocksize(uio->uio_offset);
372 offset = (int)uio->uio_offset & (blksize - 1);
373 base_offset = uio->uio_offset - offset;
375 if (bigread && (error = hammer_signal_check(ip->hmp)) != 0)
376 break;
379 * MPSAFE
381 bp = getcacheblk(ap->a_vp, base_offset);
382 if (bp) {
383 error = 0;
384 goto skip;
388 * MPUNSAFE
390 if (got_mplock == 0) {
391 got_mplock = 1;
392 get_mplock();
393 hammer_start_transaction(&trans, ip->hmp);
396 if (hammer_cluster_enable) {
398 * Use file_limit to prevent cluster_read() from
399 * creating buffers of the wrong block size past
400 * the demarc.
402 file_limit = ip->ino_data.size;
403 if (base_offset < HAMMER_XDEMARC &&
404 file_limit > HAMMER_XDEMARC) {
405 file_limit = HAMMER_XDEMARC;
407 error = cluster_read(ap->a_vp,
408 file_limit, base_offset,
409 blksize, MAXPHYS,
410 seqcount, &bp);
411 } else {
412 error = bread(ap->a_vp, base_offset, blksize, &bp);
414 if (error) {
415 brelse(bp);
416 break;
418 skip:
420 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
421 n = blksize - offset;
422 if (n > uio->uio_resid)
423 n = uio->uio_resid;
424 if (n > ip->ino_data.size - uio->uio_offset)
425 n = (int)(ip->ino_data.size - uio->uio_offset);
426 error = uiomove((char *)bp->b_data + offset, n, uio);
428 /* data has a lower priority then meta-data */
429 bp->b_flags |= B_AGE;
430 bqrelse(bp);
431 if (error)
432 break;
433 hammer_stats_file_read += n;
437 * XXX only update the atime if we had to get the MP lock.
438 * XXX hack hack hack, fixme.
440 if (got_mplock) {
441 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
442 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
443 ip->ino_data.atime = trans.time;
444 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
446 hammer_done_transaction(&trans);
447 if (got_mplock > 0)
448 rel_mplock();
450 return (error);
454 * hammer_vop_write { vp, uio, ioflag, cred }
456 static
458 hammer_vop_write(struct vop_write_args *ap)
460 struct hammer_transaction trans;
461 struct hammer_inode *ip;
462 hammer_mount_t hmp;
463 struct uio *uio;
464 int offset;
465 off_t base_offset;
466 struct buf *bp;
467 int kflags;
468 int error;
469 int n;
470 int flags;
471 int seqcount;
472 int bigwrite;
474 if (ap->a_vp->v_type != VREG)
475 return (EINVAL);
476 ip = VTOI(ap->a_vp);
477 hmp = ip->hmp;
478 error = 0;
479 kflags = 0;
480 seqcount = ap->a_ioflag >> 16;
482 if (ip->flags & HAMMER_INODE_RO)
483 return (EROFS);
486 * Create a transaction to cover the operations we perform.
488 hammer_start_transaction(&trans, hmp);
489 uio = ap->a_uio;
492 * Check append mode
494 if (ap->a_ioflag & IO_APPEND)
495 uio->uio_offset = ip->ino_data.size;
498 * Check for illegal write offsets. Valid range is 0...2^63-1.
500 * NOTE: the base_off assignment is required to work around what
501 * I consider to be a GCC-4 optimization bug.
503 if (uio->uio_offset < 0) {
504 hammer_done_transaction(&trans);
505 return (EFBIG);
507 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
508 if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) {
509 hammer_done_transaction(&trans);
510 return (EFBIG);
514 * If reading or writing a huge amount of data we have to break
515 * atomicy and allow the operation to be interrupted by a signal
516 * or it can DOS the machine.
518 * Preset redo_count so we stop generating REDOs earlier if the
519 * limit is exceeded.
521 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
522 if ((ip->flags & HAMMER_INODE_REDO) &&
523 ip->redo_count < hammer_limit_redo) {
524 ip->redo_count += uio->uio_resid;
528 * Access the data typically in HAMMER_BUFSIZE blocks via the
529 * buffer cache, but HAMMER may use a variable block size based
530 * on the offset.
532 while (uio->uio_resid > 0) {
533 int fixsize = 0;
534 int blksize;
535 int blkmask;
536 int trivial;
537 int endofblk;
538 off_t nsize;
540 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
541 break;
542 if (bigwrite && (error = hammer_signal_check(hmp)) != 0)
543 break;
545 blksize = hammer_blocksize(uio->uio_offset);
548 * Do not allow HAMMER to blow out the buffer cache. Very
549 * large UIOs can lockout other processes due to bwillwrite()
550 * mechanics.
552 * The hammer inode is not locked during these operations.
553 * The vnode is locked which can interfere with the pageout
554 * daemon for non-UIO_NOCOPY writes but should not interfere
555 * with the buffer cache. Even so, we cannot afford to
556 * allow the pageout daemon to build up too many dirty buffer
557 * cache buffers.
559 * Only call this if we aren't being recursively called from
560 * a virtual disk device (vn), else we may deadlock.
562 if ((ap->a_ioflag & IO_RECURSE) == 0)
563 bwillwrite(blksize);
566 * Control the number of pending records associated with
567 * this inode. If too many have accumulated start a
568 * flush. Try to maintain a pipeline with the flusher.
570 if (ip->rsv_recs >= hammer_limit_inode_recs) {
571 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
573 if (ip->rsv_recs >= hammer_limit_inode_recs * 2) {
574 while (ip->rsv_recs >= hammer_limit_inode_recs) {
575 tsleep(&ip->rsv_recs, 0, "hmrwww", hz);
577 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
580 #if 0
582 * Do not allow HAMMER to blow out system memory by
583 * accumulating too many records. Records are so well
584 * decoupled from the buffer cache that it is possible
585 * for userland to push data out to the media via
586 * direct-write, but build up the records queued to the
587 * backend faster then the backend can flush them out.
588 * HAMMER has hit its write limit but the frontend has
589 * no pushback to slow it down.
591 if (hmp->rsv_recs > hammer_limit_recs / 2) {
593 * Get the inode on the flush list
595 if (ip->rsv_recs >= 64)
596 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
597 else if (ip->rsv_recs >= 16)
598 hammer_flush_inode(ip, 0);
601 * Keep the flusher going if the system keeps
602 * queueing records.
604 delta = hmp->count_newrecords -
605 hmp->last_newrecords;
606 if (delta < 0 || delta > hammer_limit_recs / 2) {
607 hmp->last_newrecords = hmp->count_newrecords;
608 hammer_sync_hmp(hmp, MNT_NOWAIT);
612 * If we have gotten behind start slowing
613 * down the writers.
615 delta = (hmp->rsv_recs - hammer_limit_recs) *
616 hz / hammer_limit_recs;
617 if (delta > 0)
618 tsleep(&trans, 0, "hmrslo", delta);
620 #endif
623 * Calculate the blocksize at the current offset and figure
624 * out how much we can actually write.
626 blkmask = blksize - 1;
627 offset = (int)uio->uio_offset & blkmask;
628 base_offset = uio->uio_offset & ~(int64_t)blkmask;
629 n = blksize - offset;
630 if (n > uio->uio_resid) {
631 n = uio->uio_resid;
632 endofblk = 0;
633 } else {
634 endofblk = 1;
636 nsize = uio->uio_offset + n;
637 if (nsize > ip->ino_data.size) {
638 if (uio->uio_offset > ip->ino_data.size)
639 trivial = 0;
640 else
641 trivial = 1;
642 nvextendbuf(ap->a_vp,
643 ip->ino_data.size,
644 nsize,
645 hammer_blocksize(ip->ino_data.size),
646 hammer_blocksize(nsize),
647 hammer_blockoff(ip->ino_data.size),
648 hammer_blockoff(nsize),
649 trivial);
650 fixsize = 1;
651 kflags |= NOTE_EXTEND;
654 if (uio->uio_segflg == UIO_NOCOPY) {
656 * Issuing a write with the same data backing the
657 * buffer. Instantiate the buffer to collect the
658 * backing vm pages, then read-in any missing bits.
660 * This case is used by vop_stdputpages().
662 bp = getblk(ap->a_vp, base_offset,
663 blksize, GETBLK_BHEAVY, 0);
664 if ((bp->b_flags & B_CACHE) == 0) {
665 bqrelse(bp);
666 error = bread(ap->a_vp, base_offset,
667 blksize, &bp);
669 } else if (offset == 0 && uio->uio_resid >= blksize) {
671 * Even though we are entirely overwriting the buffer
672 * we may still have to zero it out to avoid a
673 * mmap/write visibility issue.
675 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
676 if ((bp->b_flags & B_CACHE) == 0)
677 vfs_bio_clrbuf(bp);
678 } else if (base_offset >= ip->ino_data.size) {
680 * If the base offset of the buffer is beyond the
681 * file EOF, we don't have to issue a read.
683 bp = getblk(ap->a_vp, base_offset,
684 blksize, GETBLK_BHEAVY, 0);
685 vfs_bio_clrbuf(bp);
686 } else {
688 * Partial overwrite, read in any missing bits then
689 * replace the portion being written.
691 error = bread(ap->a_vp, base_offset, blksize, &bp);
692 if (error == 0)
693 bheavy(bp);
695 if (error == 0)
696 error = uiomove(bp->b_data + offset, n, uio);
699 * Generate REDO records if enabled and redo_count will not
700 * exceeded the limit.
702 * If redo_count exceeds the limit we stop generating records
703 * and clear HAMMER_INODE_REDO. This will cause the next
704 * fsync() to do a full meta-data sync instead of just an
705 * UNDO/REDO fifo update.
707 * When clearing HAMMER_INODE_REDO any pre-existing REDOs
708 * will still be tracked. The tracks will be terminated
709 * when the related meta-data (including possible data
710 * modifications which are not tracked via REDO) is
711 * flushed.
713 if ((ip->flags & HAMMER_INODE_REDO) && error == 0) {
714 if (ip->redo_count < hammer_limit_redo) {
715 bp->b_flags |= B_VFSFLAG1;
716 error = hammer_generate_redo(&trans, ip,
717 base_offset + offset,
718 HAMMER_REDO_WRITE,
719 bp->b_data + offset,
720 (size_t)n);
721 } else {
722 ip->flags &= ~HAMMER_INODE_REDO;
727 * If we screwed up we have to undo any VM size changes we
728 * made.
730 if (error) {
731 brelse(bp);
732 if (fixsize) {
733 nvtruncbuf(ap->a_vp, ip->ino_data.size,
734 hammer_blocksize(ip->ino_data.size),
735 hammer_blockoff(ip->ino_data.size));
737 break;
739 kflags |= NOTE_WRITE;
740 hammer_stats_file_write += n;
741 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
742 if (ip->ino_data.size < uio->uio_offset) {
743 ip->ino_data.size = uio->uio_offset;
744 flags = HAMMER_INODE_SDIRTY;
745 } else {
746 flags = 0;
748 ip->ino_data.mtime = trans.time;
749 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
750 hammer_modify_inode(&trans, ip, flags);
753 * Once we dirty the buffer any cached zone-X offset
754 * becomes invalid. HAMMER NOTE: no-history mode cannot
755 * allow overwriting over the same data sector unless
756 * we provide UNDOs for the old data, which we don't.
758 bp->b_bio2.bio_offset = NOOFFSET;
761 * Final buffer disposition.
763 * Because meta-data updates are deferred, HAMMER is
764 * especially sensitive to excessive bdwrite()s because
765 * the I/O stream is not broken up by disk reads. So the
766 * buffer cache simply cannot keep up.
768 * WARNING! blksize is variable. cluster_write() is
769 * expected to not blow up if it encounters
770 * buffers that do not match the passed blksize.
772 * NOTE! Hammer shouldn't need to bawrite()/cluster_write().
773 * The ip->rsv_recs check should burst-flush the data.
774 * If we queue it immediately the buf could be left
775 * locked on the device queue for a very long time.
777 * NOTE! To avoid degenerate stalls due to mismatched block
778 * sizes we only honor IO_DIRECT on the write which
779 * abuts the end of the buffer. However, we must
780 * honor IO_SYNC in case someone is silly enough to
781 * configure a HAMMER file as swap, or when HAMMER
782 * is serving NFS (for commits). Ick ick.
784 bp->b_flags |= B_AGE;
785 if (ap->a_ioflag & IO_SYNC) {
786 bwrite(bp);
787 } else if ((ap->a_ioflag & IO_DIRECT) && endofblk) {
788 bawrite(bp);
789 } else {
790 #if 0
791 if (offset + n == blksize) {
792 if (hammer_cluster_enable == 0 ||
793 (ap->a_vp->v_mount->mnt_flag & MNT_NOCLUSTERW)) {
794 bawrite(bp);
795 } else {
796 cluster_write(bp, ip->ino_data.size,
797 blksize, seqcount);
799 } else {
800 #endif
801 bdwrite(bp);
804 hammer_done_transaction(&trans);
805 hammer_knote(ap->a_vp, kflags);
806 return (error);
810 * hammer_vop_access { vp, mode, cred }
812 static
814 hammer_vop_access(struct vop_access_args *ap)
816 struct hammer_inode *ip = VTOI(ap->a_vp);
817 uid_t uid;
818 gid_t gid;
819 int error;
821 ++hammer_stats_file_iopsr;
822 uid = hammer_to_unix_xid(&ip->ino_data.uid);
823 gid = hammer_to_unix_xid(&ip->ino_data.gid);
825 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
826 ip->ino_data.uflags);
827 return (error);
831 * hammer_vop_advlock { vp, id, op, fl, flags }
833 static
835 hammer_vop_advlock(struct vop_advlock_args *ap)
837 hammer_inode_t ip = VTOI(ap->a_vp);
839 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
843 * hammer_vop_close { vp, fflag }
845 * We can only sync-on-close for normal closes.
847 static
849 hammer_vop_close(struct vop_close_args *ap)
851 #if 0
852 struct vnode *vp = ap->a_vp;
853 hammer_inode_t ip = VTOI(vp);
854 int waitfor;
855 if (ip->flags & (HAMMER_INODE_CLOSESYNC|HAMMER_INODE_CLOSEASYNC)) {
856 if (vn_islocked(vp) == LK_EXCLUSIVE &&
857 (vp->v_flag & (VINACTIVE|VRECLAIMED)) == 0) {
858 if (ip->flags & HAMMER_INODE_CLOSESYNC)
859 waitfor = MNT_WAIT;
860 else
861 waitfor = MNT_NOWAIT;
862 ip->flags &= ~(HAMMER_INODE_CLOSESYNC |
863 HAMMER_INODE_CLOSEASYNC);
864 VOP_FSYNC(vp, MNT_NOWAIT, waitfor);
867 #endif
868 return (vop_stdclose(ap));
872 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
874 * The operating system has already ensured that the directory entry
875 * does not exist and done all appropriate namespace locking.
877 static
879 hammer_vop_ncreate(struct vop_ncreate_args *ap)
881 struct hammer_transaction trans;
882 struct hammer_inode *dip;
883 struct hammer_inode *nip;
884 struct nchandle *nch;
885 int error;
887 nch = ap->a_nch;
888 dip = VTOI(ap->a_dvp);
890 if (dip->flags & HAMMER_INODE_RO)
891 return (EROFS);
892 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
893 return (error);
896 * Create a transaction to cover the operations we perform.
898 hammer_start_transaction(&trans, dip->hmp);
899 ++hammer_stats_file_iopsw;
902 * Create a new filesystem object of the requested type. The
903 * returned inode will be referenced and shared-locked to prevent
904 * it from being moved to the flusher.
906 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
907 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
908 NULL, &nip);
909 if (error) {
910 hkprintf("hammer_create_inode error %d\n", error);
911 hammer_done_transaction(&trans);
912 *ap->a_vpp = NULL;
913 return (error);
917 * Add the new filesystem object to the directory. This will also
918 * bump the inode's link count.
920 error = hammer_ip_add_directory(&trans, dip,
921 nch->ncp->nc_name, nch->ncp->nc_nlen,
922 nip);
923 if (error)
924 hkprintf("hammer_ip_add_directory error %d\n", error);
927 * Finish up.
929 if (error) {
930 hammer_rel_inode(nip, 0);
931 hammer_done_transaction(&trans);
932 *ap->a_vpp = NULL;
933 } else {
934 error = hammer_get_vnode(nip, ap->a_vpp);
935 hammer_done_transaction(&trans);
936 hammer_rel_inode(nip, 0);
937 if (error == 0) {
938 cache_setunresolved(ap->a_nch);
939 cache_setvp(ap->a_nch, *ap->a_vpp);
941 hammer_knote(ap->a_dvp, NOTE_WRITE);
943 return (error);
947 * hammer_vop_getattr { vp, vap }
949 * Retrieve an inode's attribute information. When accessing inodes
950 * historically we fake the atime field to ensure consistent results.
951 * The atime field is stored in the B-Tree element and allowed to be
952 * updated without cycling the element.
954 * MPSAFE
956 static
958 hammer_vop_getattr(struct vop_getattr_args *ap)
960 struct hammer_inode *ip = VTOI(ap->a_vp);
961 struct vattr *vap = ap->a_vap;
964 * We want the fsid to be different when accessing a filesystem
965 * with different as-of's so programs like diff don't think
966 * the files are the same.
968 * We also want the fsid to be the same when comparing snapshots,
969 * or when comparing mirrors (which might be backed by different
970 * physical devices). HAMMER fsids are based on the PFS's
971 * shared_uuid field.
973 * XXX there is a chance of collision here. The va_fsid reported
974 * by stat is different from the more involved fsid used in the
975 * mount structure.
977 ++hammer_stats_file_iopsr;
978 hammer_lock_sh(&ip->lock);
979 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
980 (u_int32_t)(ip->obj_asof >> 32);
982 vap->va_fileid = ip->ino_leaf.base.obj_id;
983 vap->va_mode = ip->ino_data.mode;
984 vap->va_nlink = ip->ino_data.nlinks;
985 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
986 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
987 vap->va_rmajor = 0;
988 vap->va_rminor = 0;
989 vap->va_size = ip->ino_data.size;
992 * Special case for @@PFS softlinks. The actual size of the
993 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
994 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
996 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
997 ip->ino_data.size == 10 &&
998 ip->obj_asof == HAMMER_MAX_TID &&
999 ip->obj_localization == 0 &&
1000 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
1001 if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE)
1002 vap->va_size = 26;
1003 else
1004 vap->va_size = 10;
1008 * We must provide a consistent atime and mtime for snapshots
1009 * so people can do a 'tar cf - ... | md5' on them and get
1010 * consistent results.
1012 if (ip->flags & HAMMER_INODE_RO) {
1013 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
1014 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
1015 } else {
1016 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
1017 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
1019 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
1020 vap->va_flags = ip->ino_data.uflags;
1021 vap->va_gen = 1; /* hammer inums are unique for all time */
1022 vap->va_blocksize = HAMMER_BUFSIZE;
1023 if (ip->ino_data.size >= HAMMER_XDEMARC) {
1024 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
1025 ~HAMMER_XBUFMASK64;
1026 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
1027 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
1028 ~HAMMER_BUFMASK64;
1029 } else {
1030 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
1033 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
1034 vap->va_filerev = 0; /* XXX */
1035 vap->va_uid_uuid = ip->ino_data.uid;
1036 vap->va_gid_uuid = ip->ino_data.gid;
1037 vap->va_fsid_uuid = ip->hmp->fsid;
1038 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
1039 VA_FSID_UUID_VALID;
1041 switch (ip->ino_data.obj_type) {
1042 case HAMMER_OBJTYPE_CDEV:
1043 case HAMMER_OBJTYPE_BDEV:
1044 vap->va_rmajor = ip->ino_data.rmajor;
1045 vap->va_rminor = ip->ino_data.rminor;
1046 break;
1047 default:
1048 break;
1050 hammer_unlock(&ip->lock);
1051 return(0);
1055 * hammer_vop_nresolve { nch, dvp, cred }
1057 * Locate the requested directory entry.
1059 static
1061 hammer_vop_nresolve(struct vop_nresolve_args *ap)
1063 struct hammer_transaction trans;
1064 struct namecache *ncp;
1065 hammer_inode_t dip;
1066 hammer_inode_t ip;
1067 hammer_tid_t asof;
1068 struct hammer_cursor cursor;
1069 struct vnode *vp;
1070 int64_t namekey;
1071 int error;
1072 int i;
1073 int nlen;
1074 int flags;
1075 int ispfs;
1076 int64_t obj_id;
1077 u_int32_t localization;
1078 u_int32_t max_iterations;
1081 * Misc initialization, plus handle as-of name extensions. Look for
1082 * the '@@' extension. Note that as-of files and directories cannot
1083 * be modified.
1085 dip = VTOI(ap->a_dvp);
1086 ncp = ap->a_nch->ncp;
1087 asof = dip->obj_asof;
1088 localization = dip->obj_localization; /* for code consistency */
1089 nlen = ncp->nc_nlen;
1090 flags = dip->flags & HAMMER_INODE_RO;
1091 ispfs = 0;
1093 hammer_simple_transaction(&trans, dip->hmp);
1094 ++hammer_stats_file_iopsr;
1096 for (i = 0; i < nlen; ++i) {
1097 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
1098 error = hammer_str_to_tid(ncp->nc_name + i + 2,
1099 &ispfs, &asof, &localization);
1100 if (error != 0) {
1101 i = nlen;
1102 break;
1104 if (asof != HAMMER_MAX_TID)
1105 flags |= HAMMER_INODE_RO;
1106 break;
1109 nlen = i;
1112 * If this is a PFS softlink we dive into the PFS
1114 if (ispfs && nlen == 0) {
1115 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
1116 asof, localization,
1117 flags, &error);
1118 if (error == 0) {
1119 error = hammer_get_vnode(ip, &vp);
1120 hammer_rel_inode(ip, 0);
1121 } else {
1122 vp = NULL;
1124 if (error == 0) {
1125 vn_unlock(vp);
1126 cache_setvp(ap->a_nch, vp);
1127 vrele(vp);
1129 goto done;
1133 * If there is no path component the time extension is relative to dip.
1134 * e.g. "fubar/@@<snapshot>"
1136 * "." is handled by the kernel, but ".@@<snapshot>" is not.
1137 * e.g. "fubar/.@@<snapshot>"
1139 * ".." is handled by the kernel. We do not currently handle
1140 * "..@<snapshot>".
1142 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
1143 ip = hammer_get_inode(&trans, dip, dip->obj_id,
1144 asof, dip->obj_localization,
1145 flags, &error);
1146 if (error == 0) {
1147 error = hammer_get_vnode(ip, &vp);
1148 hammer_rel_inode(ip, 0);
1149 } else {
1150 vp = NULL;
1152 if (error == 0) {
1153 vn_unlock(vp);
1154 cache_setvp(ap->a_nch, vp);
1155 vrele(vp);
1157 goto done;
1161 * Calculate the namekey and setup the key range for the scan. This
1162 * works kinda like a chained hash table where the lower 32 bits
1163 * of the namekey synthesize the chain.
1165 * The key range is inclusive of both key_beg and key_end.
1167 namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen,
1168 &max_iterations);
1170 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
1171 cursor.key_beg.localization = dip->obj_localization +
1172 hammer_dir_localization(dip);
1173 cursor.key_beg.obj_id = dip->obj_id;
1174 cursor.key_beg.key = namekey;
1175 cursor.key_beg.create_tid = 0;
1176 cursor.key_beg.delete_tid = 0;
1177 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1178 cursor.key_beg.obj_type = 0;
1180 cursor.key_end = cursor.key_beg;
1181 cursor.key_end.key += max_iterations;
1182 cursor.asof = asof;
1183 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1186 * Scan all matching records (the chain), locate the one matching
1187 * the requested path component.
1189 * The hammer_ip_*() functions merge in-memory records with on-disk
1190 * records for the purposes of the search.
1192 obj_id = 0;
1193 localization = HAMMER_DEF_LOCALIZATION;
1195 if (error == 0) {
1196 error = hammer_ip_first(&cursor);
1197 while (error == 0) {
1198 error = hammer_ip_resolve_data(&cursor);
1199 if (error)
1200 break;
1201 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
1202 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1203 obj_id = cursor.data->entry.obj_id;
1204 localization = cursor.data->entry.localization;
1205 break;
1207 error = hammer_ip_next(&cursor);
1210 hammer_done_cursor(&cursor);
1213 * Lookup the obj_id. This should always succeed. If it does not
1214 * the filesystem may be damaged and we return a dummy inode.
1216 if (error == 0) {
1217 ip = hammer_get_inode(&trans, dip, obj_id,
1218 asof, localization,
1219 flags, &error);
1220 if (error == ENOENT) {
1221 kprintf("HAMMER: WARNING: Missing "
1222 "inode for dirent \"%s\"\n"
1223 "\tobj_id = %016llx, asof=%016llx, lo=%08x\n",
1224 ncp->nc_name,
1225 (long long)obj_id, (long long)asof,
1226 localization);
1227 error = 0;
1228 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
1229 asof, localization,
1230 flags, &error);
1232 if (error == 0) {
1233 error = hammer_get_vnode(ip, &vp);
1234 hammer_rel_inode(ip, 0);
1235 } else {
1236 vp = NULL;
1238 if (error == 0) {
1239 vn_unlock(vp);
1240 cache_setvp(ap->a_nch, vp);
1241 vrele(vp);
1243 } else if (error == ENOENT) {
1244 cache_setvp(ap->a_nch, NULL);
1246 done:
1247 hammer_done_transaction(&trans);
1248 return (error);
1252 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
1254 * Locate the parent directory of a directory vnode.
1256 * dvp is referenced but not locked. *vpp must be returned referenced and
1257 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
1258 * at the root, instead it could indicate that the directory we were in was
1259 * removed.
1261 * NOTE: as-of sequences are not linked into the directory structure. If
1262 * we are at the root with a different asof then the mount point, reload
1263 * the same directory with the mount point's asof. I'm not sure what this
1264 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
1265 * get confused, but it hasn't been tested.
1267 static
1269 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1271 struct hammer_transaction trans;
1272 struct hammer_inode *dip;
1273 struct hammer_inode *ip;
1274 int64_t parent_obj_id;
1275 u_int32_t parent_obj_localization;
1276 hammer_tid_t asof;
1277 int error;
1279 dip = VTOI(ap->a_dvp);
1280 asof = dip->obj_asof;
1283 * Whos are parent? This could be the root of a pseudo-filesystem
1284 * whos parent is in another localization domain.
1286 parent_obj_id = dip->ino_data.parent_obj_id;
1287 if (dip->obj_id == HAMMER_OBJID_ROOT)
1288 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
1289 else
1290 parent_obj_localization = dip->obj_localization;
1292 if (parent_obj_id == 0) {
1293 if (dip->obj_id == HAMMER_OBJID_ROOT &&
1294 asof != dip->hmp->asof) {
1295 parent_obj_id = dip->obj_id;
1296 asof = dip->hmp->asof;
1297 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1298 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
1299 (long long)dip->obj_asof);
1300 } else {
1301 *ap->a_vpp = NULL;
1302 return ENOENT;
1306 hammer_simple_transaction(&trans, dip->hmp);
1307 ++hammer_stats_file_iopsr;
1309 ip = hammer_get_inode(&trans, dip, parent_obj_id,
1310 asof, parent_obj_localization,
1311 dip->flags, &error);
1312 if (ip) {
1313 error = hammer_get_vnode(ip, ap->a_vpp);
1314 hammer_rel_inode(ip, 0);
1315 } else {
1316 *ap->a_vpp = NULL;
1318 hammer_done_transaction(&trans);
1319 return (error);
1323 * hammer_vop_nlink { nch, dvp, vp, cred }
1325 static
1327 hammer_vop_nlink(struct vop_nlink_args *ap)
1329 struct hammer_transaction trans;
1330 struct hammer_inode *dip;
1331 struct hammer_inode *ip;
1332 struct nchandle *nch;
1333 int error;
1335 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1336 return(EXDEV);
1338 nch = ap->a_nch;
1339 dip = VTOI(ap->a_dvp);
1340 ip = VTOI(ap->a_vp);
1342 if (dip->obj_localization != ip->obj_localization)
1343 return(EXDEV);
1345 if (dip->flags & HAMMER_INODE_RO)
1346 return (EROFS);
1347 if (ip->flags & HAMMER_INODE_RO)
1348 return (EROFS);
1349 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1350 return (error);
1353 * Create a transaction to cover the operations we perform.
1355 hammer_start_transaction(&trans, dip->hmp);
1356 ++hammer_stats_file_iopsw;
1359 * Add the filesystem object to the directory. Note that neither
1360 * dip nor ip are referenced or locked, but their vnodes are
1361 * referenced. This function will bump the inode's link count.
1363 error = hammer_ip_add_directory(&trans, dip,
1364 nch->ncp->nc_name, nch->ncp->nc_nlen,
1365 ip);
1368 * Finish up.
1370 if (error == 0) {
1371 cache_setunresolved(nch);
1372 cache_setvp(nch, ap->a_vp);
1374 hammer_done_transaction(&trans);
1375 hammer_knote(ap->a_vp, NOTE_LINK);
1376 hammer_knote(ap->a_dvp, NOTE_WRITE);
1377 return (error);
1381 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1383 * The operating system has already ensured that the directory entry
1384 * does not exist and done all appropriate namespace locking.
1386 static
1388 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1390 struct hammer_transaction trans;
1391 struct hammer_inode *dip;
1392 struct hammer_inode *nip;
1393 struct nchandle *nch;
1394 int error;
1396 nch = ap->a_nch;
1397 dip = VTOI(ap->a_dvp);
1399 if (dip->flags & HAMMER_INODE_RO)
1400 return (EROFS);
1401 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1402 return (error);
1405 * Create a transaction to cover the operations we perform.
1407 hammer_start_transaction(&trans, dip->hmp);
1408 ++hammer_stats_file_iopsw;
1411 * Create a new filesystem object of the requested type. The
1412 * returned inode will be referenced but not locked.
1414 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1415 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1416 NULL, &nip);
1417 if (error) {
1418 hkprintf("hammer_mkdir error %d\n", error);
1419 hammer_done_transaction(&trans);
1420 *ap->a_vpp = NULL;
1421 return (error);
1424 * Add the new filesystem object to the directory. This will also
1425 * bump the inode's link count.
1427 error = hammer_ip_add_directory(&trans, dip,
1428 nch->ncp->nc_name, nch->ncp->nc_nlen,
1429 nip);
1430 if (error)
1431 hkprintf("hammer_mkdir (add) error %d\n", error);
1434 * Finish up.
1436 if (error) {
1437 hammer_rel_inode(nip, 0);
1438 *ap->a_vpp = NULL;
1439 } else {
1440 error = hammer_get_vnode(nip, ap->a_vpp);
1441 hammer_rel_inode(nip, 0);
1442 if (error == 0) {
1443 cache_setunresolved(ap->a_nch);
1444 cache_setvp(ap->a_nch, *ap->a_vpp);
1447 hammer_done_transaction(&trans);
1448 if (error == 0)
1449 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
1450 return (error);
1454 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1456 * The operating system has already ensured that the directory entry
1457 * does not exist and done all appropriate namespace locking.
1459 static
1461 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1463 struct hammer_transaction trans;
1464 struct hammer_inode *dip;
1465 struct hammer_inode *nip;
1466 struct nchandle *nch;
1467 int error;
1469 nch = ap->a_nch;
1470 dip = VTOI(ap->a_dvp);
1472 if (dip->flags & HAMMER_INODE_RO)
1473 return (EROFS);
1474 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1475 return (error);
1478 * Create a transaction to cover the operations we perform.
1480 hammer_start_transaction(&trans, dip->hmp);
1481 ++hammer_stats_file_iopsw;
1484 * Create a new filesystem object of the requested type. The
1485 * returned inode will be referenced but not locked.
1487 * If mknod specifies a directory a pseudo-fs is created.
1489 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1490 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1491 NULL, &nip);
1492 if (error) {
1493 hammer_done_transaction(&trans);
1494 *ap->a_vpp = NULL;
1495 return (error);
1499 * Add the new filesystem object to the directory. This will also
1500 * bump the inode's link count.
1502 error = hammer_ip_add_directory(&trans, dip,
1503 nch->ncp->nc_name, nch->ncp->nc_nlen,
1504 nip);
1507 * Finish up.
1509 if (error) {
1510 hammer_rel_inode(nip, 0);
1511 *ap->a_vpp = NULL;
1512 } else {
1513 error = hammer_get_vnode(nip, ap->a_vpp);
1514 hammer_rel_inode(nip, 0);
1515 if (error == 0) {
1516 cache_setunresolved(ap->a_nch);
1517 cache_setvp(ap->a_nch, *ap->a_vpp);
1520 hammer_done_transaction(&trans);
1521 if (error == 0)
1522 hammer_knote(ap->a_dvp, NOTE_WRITE);
1523 return (error);
1527 * hammer_vop_open { vp, mode, cred, fp }
1529 static
1531 hammer_vop_open(struct vop_open_args *ap)
1533 hammer_inode_t ip;
1535 ++hammer_stats_file_iopsr;
1536 ip = VTOI(ap->a_vp);
1538 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1539 return (EROFS);
1540 return(vop_stdopen(ap));
1544 * hammer_vop_print { vp }
1546 static
1548 hammer_vop_print(struct vop_print_args *ap)
1550 return EOPNOTSUPP;
1554 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1556 static
1558 hammer_vop_readdir(struct vop_readdir_args *ap)
1560 struct hammer_transaction trans;
1561 struct hammer_cursor cursor;
1562 struct hammer_inode *ip;
1563 struct uio *uio;
1564 hammer_base_elm_t base;
1565 int error;
1566 int cookie_index;
1567 int ncookies;
1568 off_t *cookies;
1569 off_t saveoff;
1570 int r;
1571 int dtype;
1573 ++hammer_stats_file_iopsr;
1574 ip = VTOI(ap->a_vp);
1575 uio = ap->a_uio;
1576 saveoff = uio->uio_offset;
1578 if (ap->a_ncookies) {
1579 ncookies = uio->uio_resid / 16 + 1;
1580 if (ncookies > 1024)
1581 ncookies = 1024;
1582 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1583 cookie_index = 0;
1584 } else {
1585 ncookies = -1;
1586 cookies = NULL;
1587 cookie_index = 0;
1590 hammer_simple_transaction(&trans, ip->hmp);
1593 * Handle artificial entries
1595 * It should be noted that the minimum value for a directory
1596 * hash key on-media is 0x0000000100000000, so we can use anything
1597 * less then that to represent our 'special' key space.
1599 error = 0;
1600 if (saveoff == 0) {
1601 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1602 if (r)
1603 goto done;
1604 if (cookies)
1605 cookies[cookie_index] = saveoff;
1606 ++saveoff;
1607 ++cookie_index;
1608 if (cookie_index == ncookies)
1609 goto done;
1611 if (saveoff == 1) {
1612 if (ip->ino_data.parent_obj_id) {
1613 r = vop_write_dirent(&error, uio,
1614 ip->ino_data.parent_obj_id,
1615 DT_DIR, 2, "..");
1616 } else {
1617 r = vop_write_dirent(&error, uio,
1618 ip->obj_id, DT_DIR, 2, "..");
1620 if (r)
1621 goto done;
1622 if (cookies)
1623 cookies[cookie_index] = saveoff;
1624 ++saveoff;
1625 ++cookie_index;
1626 if (cookie_index == ncookies)
1627 goto done;
1631 * Key range (begin and end inclusive) to scan. Directory keys
1632 * directly translate to a 64 bit 'seek' position.
1634 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1635 cursor.key_beg.localization = ip->obj_localization +
1636 hammer_dir_localization(ip);
1637 cursor.key_beg.obj_id = ip->obj_id;
1638 cursor.key_beg.create_tid = 0;
1639 cursor.key_beg.delete_tid = 0;
1640 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1641 cursor.key_beg.obj_type = 0;
1642 cursor.key_beg.key = saveoff;
1644 cursor.key_end = cursor.key_beg;
1645 cursor.key_end.key = HAMMER_MAX_KEY;
1646 cursor.asof = ip->obj_asof;
1647 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1649 error = hammer_ip_first(&cursor);
1651 while (error == 0) {
1652 error = hammer_ip_resolve_data(&cursor);
1653 if (error)
1654 break;
1655 base = &cursor.leaf->base;
1656 saveoff = base->key;
1657 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1659 if (base->obj_id != ip->obj_id)
1660 panic("readdir: bad record at %p", cursor.node);
1663 * Convert pseudo-filesystems into softlinks
1665 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1666 r = vop_write_dirent(
1667 &error, uio, cursor.data->entry.obj_id,
1668 dtype,
1669 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1670 (void *)cursor.data->entry.name);
1671 if (r)
1672 break;
1673 ++saveoff;
1674 if (cookies)
1675 cookies[cookie_index] = base->key;
1676 ++cookie_index;
1677 if (cookie_index == ncookies)
1678 break;
1679 error = hammer_ip_next(&cursor);
1681 hammer_done_cursor(&cursor);
1683 done:
1684 hammer_done_transaction(&trans);
1686 if (ap->a_eofflag)
1687 *ap->a_eofflag = (error == ENOENT);
1688 uio->uio_offset = saveoff;
1689 if (error && cookie_index == 0) {
1690 if (error == ENOENT)
1691 error = 0;
1692 if (cookies) {
1693 kfree(cookies, M_TEMP);
1694 *ap->a_ncookies = 0;
1695 *ap->a_cookies = NULL;
1697 } else {
1698 if (error == ENOENT)
1699 error = 0;
1700 if (cookies) {
1701 *ap->a_ncookies = cookie_index;
1702 *ap->a_cookies = cookies;
1705 return(error);
1709 * hammer_vop_readlink { vp, uio, cred }
1711 static
1713 hammer_vop_readlink(struct vop_readlink_args *ap)
1715 struct hammer_transaction trans;
1716 struct hammer_cursor cursor;
1717 struct hammer_inode *ip;
1718 char buf[32];
1719 u_int32_t localization;
1720 hammer_pseudofs_inmem_t pfsm;
1721 int error;
1723 ip = VTOI(ap->a_vp);
1726 * Shortcut if the symlink data was stuffed into ino_data.
1728 * Also expand special "@@PFS%05d" softlinks (expansion only
1729 * occurs for non-historical (current) accesses made from the
1730 * primary filesystem).
1732 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1733 char *ptr;
1734 int bytes;
1736 ptr = ip->ino_data.ext.symlink;
1737 bytes = (int)ip->ino_data.size;
1738 if (bytes == 10 &&
1739 ip->obj_asof == HAMMER_MAX_TID &&
1740 ip->obj_localization == 0 &&
1741 strncmp(ptr, "@@PFS", 5) == 0) {
1742 hammer_simple_transaction(&trans, ip->hmp);
1743 bcopy(ptr + 5, buf, 5);
1744 buf[5] = 0;
1745 localization = strtoul(buf, NULL, 10) << 16;
1746 pfsm = hammer_load_pseudofs(&trans, localization,
1747 &error);
1748 if (error == 0) {
1749 if (pfsm->pfsd.mirror_flags &
1750 HAMMER_PFSD_SLAVE) {
1751 /* vap->va_size == 26 */
1752 ksnprintf(buf, sizeof(buf),
1753 "@@0x%016llx:%05d",
1754 (long long)pfsm->pfsd.sync_end_tid,
1755 localization >> 16);
1756 } else {
1757 /* vap->va_size == 10 */
1758 ksnprintf(buf, sizeof(buf),
1759 "@@-1:%05d",
1760 localization >> 16);
1761 #if 0
1762 ksnprintf(buf, sizeof(buf),
1763 "@@0x%016llx:%05d",
1764 (long long)HAMMER_MAX_TID,
1765 localization >> 16);
1766 #endif
1768 ptr = buf;
1769 bytes = strlen(buf);
1771 if (pfsm)
1772 hammer_rel_pseudofs(trans.hmp, pfsm);
1773 hammer_done_transaction(&trans);
1775 error = uiomove(ptr, bytes, ap->a_uio);
1776 return(error);
1780 * Long version
1782 hammer_simple_transaction(&trans, ip->hmp);
1783 ++hammer_stats_file_iopsr;
1784 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1787 * Key range (begin and end inclusive) to scan. Directory keys
1788 * directly translate to a 64 bit 'seek' position.
1790 cursor.key_beg.localization = ip->obj_localization +
1791 HAMMER_LOCALIZE_MISC;
1792 cursor.key_beg.obj_id = ip->obj_id;
1793 cursor.key_beg.create_tid = 0;
1794 cursor.key_beg.delete_tid = 0;
1795 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1796 cursor.key_beg.obj_type = 0;
1797 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1798 cursor.asof = ip->obj_asof;
1799 cursor.flags |= HAMMER_CURSOR_ASOF;
1801 error = hammer_ip_lookup(&cursor);
1802 if (error == 0) {
1803 error = hammer_ip_resolve_data(&cursor);
1804 if (error == 0) {
1805 KKASSERT(cursor.leaf->data_len >=
1806 HAMMER_SYMLINK_NAME_OFF);
1807 error = uiomove(cursor.data->symlink.name,
1808 cursor.leaf->data_len -
1809 HAMMER_SYMLINK_NAME_OFF,
1810 ap->a_uio);
1813 hammer_done_cursor(&cursor);
1814 hammer_done_transaction(&trans);
1815 return(error);
1819 * hammer_vop_nremove { nch, dvp, cred }
1821 static
1823 hammer_vop_nremove(struct vop_nremove_args *ap)
1825 struct hammer_transaction trans;
1826 struct hammer_inode *dip;
1827 int error;
1829 dip = VTOI(ap->a_dvp);
1831 if (hammer_nohistory(dip) == 0 &&
1832 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1833 return (error);
1836 hammer_start_transaction(&trans, dip->hmp);
1837 ++hammer_stats_file_iopsw;
1838 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
1839 hammer_done_transaction(&trans);
1840 if (error == 0)
1841 hammer_knote(ap->a_dvp, NOTE_WRITE);
1842 return (error);
1846 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1848 static
1850 hammer_vop_nrename(struct vop_nrename_args *ap)
1852 struct hammer_transaction trans;
1853 struct namecache *fncp;
1854 struct namecache *tncp;
1855 struct hammer_inode *fdip;
1856 struct hammer_inode *tdip;
1857 struct hammer_inode *ip;
1858 struct hammer_cursor cursor;
1859 int64_t namekey;
1860 u_int32_t max_iterations;
1861 int nlen, error;
1863 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1864 return(EXDEV);
1865 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1866 return(EXDEV);
1868 fdip = VTOI(ap->a_fdvp);
1869 tdip = VTOI(ap->a_tdvp);
1870 fncp = ap->a_fnch->ncp;
1871 tncp = ap->a_tnch->ncp;
1872 ip = VTOI(fncp->nc_vp);
1873 KKASSERT(ip != NULL);
1875 if (fdip->obj_localization != tdip->obj_localization)
1876 return(EXDEV);
1877 if (fdip->obj_localization != ip->obj_localization)
1878 return(EXDEV);
1880 if (fdip->flags & HAMMER_INODE_RO)
1881 return (EROFS);
1882 if (tdip->flags & HAMMER_INODE_RO)
1883 return (EROFS);
1884 if (ip->flags & HAMMER_INODE_RO)
1885 return (EROFS);
1886 if ((error = hammer_checkspace(fdip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1887 return (error);
1889 hammer_start_transaction(&trans, fdip->hmp);
1890 ++hammer_stats_file_iopsw;
1893 * Remove tncp from the target directory and then link ip as
1894 * tncp. XXX pass trans to dounlink
1896 * Force the inode sync-time to match the transaction so it is
1897 * in-sync with the creation of the target directory entry.
1899 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
1900 ap->a_cred, 0, -1);
1901 if (error == 0 || error == ENOENT) {
1902 error = hammer_ip_add_directory(&trans, tdip,
1903 tncp->nc_name, tncp->nc_nlen,
1904 ip);
1905 if (error == 0) {
1906 ip->ino_data.parent_obj_id = tdip->obj_id;
1907 ip->ino_data.ctime = trans.time;
1908 hammer_modify_inode(&trans, ip, HAMMER_INODE_DDIRTY);
1911 if (error)
1912 goto failed; /* XXX */
1915 * Locate the record in the originating directory and remove it.
1917 * Calculate the namekey and setup the key range for the scan. This
1918 * works kinda like a chained hash table where the lower 32 bits
1919 * of the namekey synthesize the chain.
1921 * The key range is inclusive of both key_beg and key_end.
1923 namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
1924 &max_iterations);
1925 retry:
1926 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
1927 cursor.key_beg.localization = fdip->obj_localization +
1928 hammer_dir_localization(fdip);
1929 cursor.key_beg.obj_id = fdip->obj_id;
1930 cursor.key_beg.key = namekey;
1931 cursor.key_beg.create_tid = 0;
1932 cursor.key_beg.delete_tid = 0;
1933 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1934 cursor.key_beg.obj_type = 0;
1936 cursor.key_end = cursor.key_beg;
1937 cursor.key_end.key += max_iterations;
1938 cursor.asof = fdip->obj_asof;
1939 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1942 * Scan all matching records (the chain), locate the one matching
1943 * the requested path component.
1945 * The hammer_ip_*() functions merge in-memory records with on-disk
1946 * records for the purposes of the search.
1948 error = hammer_ip_first(&cursor);
1949 while (error == 0) {
1950 if (hammer_ip_resolve_data(&cursor) != 0)
1951 break;
1952 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
1953 KKASSERT(nlen > 0);
1954 if (fncp->nc_nlen == nlen &&
1955 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1956 break;
1958 error = hammer_ip_next(&cursor);
1962 * If all is ok we have to get the inode so we can adjust nlinks.
1964 * WARNING: hammer_ip_del_directory() may have to terminate the
1965 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1966 * twice.
1968 if (error == 0)
1969 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1972 * XXX A deadlock here will break rename's atomicy for the purposes
1973 * of crash recovery.
1975 if (error == EDEADLK) {
1976 hammer_done_cursor(&cursor);
1977 goto retry;
1981 * Cleanup and tell the kernel that the rename succeeded.
1983 hammer_done_cursor(&cursor);
1984 if (error == 0) {
1985 cache_rename(ap->a_fnch, ap->a_tnch);
1986 hammer_knote(ap->a_fdvp, NOTE_WRITE);
1987 hammer_knote(ap->a_tdvp, NOTE_WRITE);
1988 if (ip->vp)
1989 hammer_knote(ip->vp, NOTE_RENAME);
1992 failed:
1993 hammer_done_transaction(&trans);
1994 return (error);
1998 * hammer_vop_nrmdir { nch, dvp, cred }
2000 static
2002 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
2004 struct hammer_transaction trans;
2005 struct hammer_inode *dip;
2006 int error;
2008 dip = VTOI(ap->a_dvp);
2010 if (hammer_nohistory(dip) == 0 &&
2011 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2012 return (error);
2015 hammer_start_transaction(&trans, dip->hmp);
2016 ++hammer_stats_file_iopsw;
2017 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
2018 hammer_done_transaction(&trans);
2019 if (error == 0)
2020 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
2021 return (error);
2025 * hammer_vop_markatime { vp, cred }
2027 static
2029 hammer_vop_markatime(struct vop_markatime_args *ap)
2031 struct hammer_transaction trans;
2032 struct hammer_inode *ip;
2034 ip = VTOI(ap->a_vp);
2035 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2036 return (EROFS);
2037 if (ip->flags & HAMMER_INODE_RO)
2038 return (EROFS);
2039 if (ip->hmp->mp->mnt_flag & MNT_NOATIME)
2040 return (0);
2041 hammer_start_transaction(&trans, ip->hmp);
2042 ++hammer_stats_file_iopsw;
2044 ip->ino_data.atime = trans.time;
2045 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
2046 hammer_done_transaction(&trans);
2047 hammer_knote(ap->a_vp, NOTE_ATTRIB);
2048 return (0);
2052 * hammer_vop_setattr { vp, vap, cred }
2054 static
2056 hammer_vop_setattr(struct vop_setattr_args *ap)
2058 struct hammer_transaction trans;
2059 struct vattr *vap;
2060 struct hammer_inode *ip;
2061 int modflags;
2062 int error;
2063 int truncating;
2064 int blksize;
2065 int kflags;
2066 #if 0
2067 int64_t aligned_size;
2068 #endif
2069 u_int32_t flags;
2071 vap = ap->a_vap;
2072 ip = ap->a_vp->v_data;
2073 modflags = 0;
2074 kflags = 0;
2076 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2077 return(EROFS);
2078 if (ip->flags & HAMMER_INODE_RO)
2079 return (EROFS);
2080 if (hammer_nohistory(ip) == 0 &&
2081 (error = hammer_checkspace(ip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
2082 return (error);
2085 hammer_start_transaction(&trans, ip->hmp);
2086 ++hammer_stats_file_iopsw;
2087 error = 0;
2089 if (vap->va_flags != VNOVAL) {
2090 flags = ip->ino_data.uflags;
2091 error = vop_helper_setattr_flags(&flags, vap->va_flags,
2092 hammer_to_unix_xid(&ip->ino_data.uid),
2093 ap->a_cred);
2094 if (error == 0) {
2095 if (ip->ino_data.uflags != flags) {
2096 ip->ino_data.uflags = flags;
2097 ip->ino_data.ctime = trans.time;
2098 modflags |= HAMMER_INODE_DDIRTY;
2099 kflags |= NOTE_ATTRIB;
2101 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2102 error = 0;
2103 goto done;
2106 goto done;
2108 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2109 error = EPERM;
2110 goto done;
2112 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
2113 mode_t cur_mode = ip->ino_data.mode;
2114 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2115 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2116 uuid_t uuid_uid;
2117 uuid_t uuid_gid;
2119 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
2120 ap->a_cred,
2121 &cur_uid, &cur_gid, &cur_mode);
2122 if (error == 0) {
2123 hammer_guid_to_uuid(&uuid_uid, cur_uid);
2124 hammer_guid_to_uuid(&uuid_gid, cur_gid);
2125 if (bcmp(&uuid_uid, &ip->ino_data.uid,
2126 sizeof(uuid_uid)) ||
2127 bcmp(&uuid_gid, &ip->ino_data.gid,
2128 sizeof(uuid_gid)) ||
2129 ip->ino_data.mode != cur_mode
2131 ip->ino_data.uid = uuid_uid;
2132 ip->ino_data.gid = uuid_gid;
2133 ip->ino_data.mode = cur_mode;
2134 ip->ino_data.ctime = trans.time;
2135 modflags |= HAMMER_INODE_DDIRTY;
2137 kflags |= NOTE_ATTRIB;
2140 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
2141 switch(ap->a_vp->v_type) {
2142 case VREG:
2143 if (vap->va_size == ip->ino_data.size)
2144 break;
2147 * Log the operation if in fast-fsync mode.
2149 if (ip->flags & HAMMER_INODE_REDO) {
2150 error = hammer_generate_redo(&trans, ip,
2151 vap->va_size,
2152 HAMMER_REDO_TRUNC,
2153 NULL, 0);
2155 blksize = hammer_blocksize(vap->va_size);
2158 * XXX break atomicy, we can deadlock the backend
2159 * if we do not release the lock. Probably not a
2160 * big deal here.
2162 if (vap->va_size < ip->ino_data.size) {
2163 nvtruncbuf(ap->a_vp, vap->va_size,
2164 blksize,
2165 hammer_blockoff(vap->va_size));
2166 truncating = 1;
2167 kflags |= NOTE_WRITE;
2168 } else {
2169 nvextendbuf(ap->a_vp,
2170 ip->ino_data.size,
2171 vap->va_size,
2172 hammer_blocksize(ip->ino_data.size),
2173 hammer_blocksize(vap->va_size),
2174 hammer_blockoff(ip->ino_data.size),
2175 hammer_blockoff(vap->va_size),
2177 truncating = 0;
2178 kflags |= NOTE_WRITE | NOTE_EXTEND;
2180 ip->ino_data.size = vap->va_size;
2181 ip->ino_data.mtime = trans.time;
2182 /* XXX safe to use SDIRTY instead of DDIRTY here? */
2183 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2186 * On-media truncation is cached in the inode until
2187 * the inode is synchronized. We must immediately
2188 * handle any frontend records.
2190 if (truncating) {
2191 hammer_ip_frontend_trunc(ip, vap->va_size);
2192 #ifdef DEBUG_TRUNCATE
2193 if (HammerTruncIp == NULL)
2194 HammerTruncIp = ip;
2195 #endif
2196 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2197 ip->flags |= HAMMER_INODE_TRUNCATED;
2198 ip->trunc_off = vap->va_size;
2199 #ifdef DEBUG_TRUNCATE
2200 if (ip == HammerTruncIp)
2201 kprintf("truncate1 %016llx\n",
2202 (long long)ip->trunc_off);
2203 #endif
2204 } else if (ip->trunc_off > vap->va_size) {
2205 ip->trunc_off = vap->va_size;
2206 #ifdef DEBUG_TRUNCATE
2207 if (ip == HammerTruncIp)
2208 kprintf("truncate2 %016llx\n",
2209 (long long)ip->trunc_off);
2210 #endif
2211 } else {
2212 #ifdef DEBUG_TRUNCATE
2213 if (ip == HammerTruncIp)
2214 kprintf("truncate3 %016llx (ignored)\n",
2215 (long long)vap->va_size);
2216 #endif
2220 #if 0
2222 * When truncating, nvtruncbuf() may have cleaned out
2223 * a portion of the last block on-disk in the buffer
2224 * cache. We must clean out any frontend records
2225 * for blocks beyond the new last block.
2227 aligned_size = (vap->va_size + (blksize - 1)) &
2228 ~(int64_t)(blksize - 1);
2229 if (truncating && vap->va_size < aligned_size) {
2230 aligned_size -= blksize;
2231 hammer_ip_frontend_trunc(ip, aligned_size);
2233 #endif
2234 break;
2235 case VDATABASE:
2236 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2237 ip->flags |= HAMMER_INODE_TRUNCATED;
2238 ip->trunc_off = vap->va_size;
2239 } else if (ip->trunc_off > vap->va_size) {
2240 ip->trunc_off = vap->va_size;
2242 hammer_ip_frontend_trunc(ip, vap->va_size);
2243 ip->ino_data.size = vap->va_size;
2244 ip->ino_data.mtime = trans.time;
2245 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
2246 kflags |= NOTE_ATTRIB;
2247 break;
2248 default:
2249 error = EINVAL;
2250 goto done;
2252 break;
2254 if (vap->va_atime.tv_sec != VNOVAL) {
2255 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
2256 modflags |= HAMMER_INODE_ATIME;
2257 kflags |= NOTE_ATTRIB;
2259 if (vap->va_mtime.tv_sec != VNOVAL) {
2260 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
2261 modflags |= HAMMER_INODE_MTIME;
2262 kflags |= NOTE_ATTRIB;
2264 if (vap->va_mode != (mode_t)VNOVAL) {
2265 mode_t cur_mode = ip->ino_data.mode;
2266 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2267 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2269 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
2270 cur_uid, cur_gid, &cur_mode);
2271 if (error == 0 && ip->ino_data.mode != cur_mode) {
2272 ip->ino_data.mode = cur_mode;
2273 ip->ino_data.ctime = trans.time;
2274 modflags |= HAMMER_INODE_DDIRTY;
2275 kflags |= NOTE_ATTRIB;
2278 done:
2279 if (error == 0)
2280 hammer_modify_inode(&trans, ip, modflags);
2281 hammer_done_transaction(&trans);
2282 hammer_knote(ap->a_vp, kflags);
2283 return (error);
2287 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2289 static
2291 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
2293 struct hammer_transaction trans;
2294 struct hammer_inode *dip;
2295 struct hammer_inode *nip;
2296 struct nchandle *nch;
2297 hammer_record_t record;
2298 int error;
2299 int bytes;
2301 ap->a_vap->va_type = VLNK;
2303 nch = ap->a_nch;
2304 dip = VTOI(ap->a_dvp);
2306 if (dip->flags & HAMMER_INODE_RO)
2307 return (EROFS);
2308 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
2309 return (error);
2312 * Create a transaction to cover the operations we perform.
2314 hammer_start_transaction(&trans, dip->hmp);
2315 ++hammer_stats_file_iopsw;
2318 * Create a new filesystem object of the requested type. The
2319 * returned inode will be referenced but not locked.
2322 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
2323 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2324 NULL, &nip);
2325 if (error) {
2326 hammer_done_transaction(&trans);
2327 *ap->a_vpp = NULL;
2328 return (error);
2332 * Add a record representing the symlink. symlink stores the link
2333 * as pure data, not a string, and is no \0 terminated.
2335 if (error == 0) {
2336 bytes = strlen(ap->a_target);
2338 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2339 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2340 } else {
2341 record = hammer_alloc_mem_record(nip, bytes);
2342 record->type = HAMMER_MEM_RECORD_GENERAL;
2344 record->leaf.base.localization = nip->obj_localization +
2345 HAMMER_LOCALIZE_MISC;
2346 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2347 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2348 record->leaf.data_len = bytes;
2349 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2350 bcopy(ap->a_target, record->data->symlink.name, bytes);
2351 error = hammer_ip_add_record(&trans, record);
2355 * Set the file size to the length of the link.
2357 if (error == 0) {
2358 nip->ino_data.size = bytes;
2359 hammer_modify_inode(&trans, nip, HAMMER_INODE_DDIRTY);
2362 if (error == 0)
2363 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
2364 nch->ncp->nc_nlen, nip);
2367 * Finish up.
2369 if (error) {
2370 hammer_rel_inode(nip, 0);
2371 *ap->a_vpp = NULL;
2372 } else {
2373 error = hammer_get_vnode(nip, ap->a_vpp);
2374 hammer_rel_inode(nip, 0);
2375 if (error == 0) {
2376 cache_setunresolved(ap->a_nch);
2377 cache_setvp(ap->a_nch, *ap->a_vpp);
2378 hammer_knote(ap->a_dvp, NOTE_WRITE);
2381 hammer_done_transaction(&trans);
2382 return (error);
2386 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2388 static
2390 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
2392 struct hammer_transaction trans;
2393 struct hammer_inode *dip;
2394 int error;
2396 dip = VTOI(ap->a_dvp);
2398 if (hammer_nohistory(dip) == 0 &&
2399 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0) {
2400 return (error);
2403 hammer_start_transaction(&trans, dip->hmp);
2404 ++hammer_stats_file_iopsw;
2405 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
2406 ap->a_cred, ap->a_flags, -1);
2407 hammer_done_transaction(&trans);
2409 return (error);
2413 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2415 static
2417 hammer_vop_ioctl(struct vop_ioctl_args *ap)
2419 struct hammer_inode *ip = ap->a_vp->v_data;
2421 ++hammer_stats_file_iopsr;
2422 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
2423 ap->a_fflag, ap->a_cred));
2426 static
2428 hammer_vop_mountctl(struct vop_mountctl_args *ap)
2430 static const struct mountctl_opt extraopt[] = {
2431 { HMNT_NOHISTORY, "nohistory" },
2432 { HMNT_MASTERID, "master" },
2433 { 0, NULL}
2436 struct hammer_mount *hmp;
2437 struct mount *mp;
2438 int usedbytes;
2439 int error;
2441 error = 0;
2442 usedbytes = 0;
2443 mp = ap->a_head.a_ops->head.vv_mount;
2444 KKASSERT(mp->mnt_data != NULL);
2445 hmp = (struct hammer_mount *)mp->mnt_data;
2447 switch(ap->a_op) {
2449 case MOUNTCTL_SET_EXPORT:
2450 if (ap->a_ctllen != sizeof(struct export_args))
2451 error = EINVAL;
2452 else
2453 error = hammer_vfs_export(mp, ap->a_op,
2454 (const struct export_args *)ap->a_ctl);
2455 break;
2456 case MOUNTCTL_MOUNTFLAGS:
2459 * Call standard mountctl VOP function
2460 * so we get user mount flags.
2462 error = vop_stdmountctl(ap);
2463 if (error)
2464 break;
2466 usedbytes = *ap->a_res;
2468 if (usedbytes > 0 && usedbytes < ap->a_buflen) {
2469 usedbytes += vfs_flagstostr(hmp->hflags, extraopt, ap->a_buf,
2470 ap->a_buflen - usedbytes,
2471 &error);
2474 *ap->a_res += usedbytes;
2475 break;
2477 default:
2478 error = vop_stdmountctl(ap);
2479 break;
2481 return(error);
2485 * hammer_vop_strategy { vp, bio }
2487 * Strategy call, used for regular file read & write only. Note that the
2488 * bp may represent a cluster.
2490 * To simplify operation and allow better optimizations in the future,
2491 * this code does not make any assumptions with regards to buffer alignment
2492 * or size.
2494 static
2496 hammer_vop_strategy(struct vop_strategy_args *ap)
2498 struct buf *bp;
2499 int error;
2501 bp = ap->a_bio->bio_buf;
2503 switch(bp->b_cmd) {
2504 case BUF_CMD_READ:
2505 error = hammer_vop_strategy_read(ap);
2506 break;
2507 case BUF_CMD_WRITE:
2508 error = hammer_vop_strategy_write(ap);
2509 break;
2510 default:
2511 bp->b_error = error = EINVAL;
2512 bp->b_flags |= B_ERROR;
2513 biodone(ap->a_bio);
2514 break;
2516 return (error);
2520 * Read from a regular file. Iterate the related records and fill in the
2521 * BIO/BUF. Gaps are zero-filled.
2523 * The support code in hammer_object.c should be used to deal with mixed
2524 * in-memory and on-disk records.
2526 * NOTE: Can be called from the cluster code with an oversized buf.
2528 * XXX atime update
2530 static
2532 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2534 struct hammer_transaction trans;
2535 struct hammer_inode *ip;
2536 struct hammer_inode *dip;
2537 struct hammer_cursor cursor;
2538 hammer_base_elm_t base;
2539 hammer_off_t disk_offset;
2540 struct bio *bio;
2541 struct bio *nbio;
2542 struct buf *bp;
2543 int64_t rec_offset;
2544 int64_t ran_end;
2545 int64_t tmp64;
2546 int error;
2547 int boff;
2548 int roff;
2549 int n;
2551 bio = ap->a_bio;
2552 bp = bio->bio_buf;
2553 ip = ap->a_vp->v_data;
2556 * The zone-2 disk offset may have been set by the cluster code via
2557 * a BMAP operation, or else should be NOOFFSET.
2559 * Checking the high bits for a match against zone-2 should suffice.
2561 nbio = push_bio(bio);
2562 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2563 HAMMER_ZONE_LARGE_DATA) {
2564 error = hammer_io_direct_read(ip->hmp, nbio, NULL);
2565 return (error);
2569 * Well, that sucked. Do it the hard way. If all the stars are
2570 * aligned we may still be able to issue a direct-read.
2572 hammer_simple_transaction(&trans, ip->hmp);
2573 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2576 * Key range (begin and end inclusive) to scan. Note that the key's
2577 * stored in the actual records represent BASE+LEN, not BASE. The
2578 * first record containing bio_offset will have a key > bio_offset.
2580 cursor.key_beg.localization = ip->obj_localization +
2581 HAMMER_LOCALIZE_MISC;
2582 cursor.key_beg.obj_id = ip->obj_id;
2583 cursor.key_beg.create_tid = 0;
2584 cursor.key_beg.delete_tid = 0;
2585 cursor.key_beg.obj_type = 0;
2586 cursor.key_beg.key = bio->bio_offset + 1;
2587 cursor.asof = ip->obj_asof;
2588 cursor.flags |= HAMMER_CURSOR_ASOF;
2590 cursor.key_end = cursor.key_beg;
2591 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2592 #if 0
2593 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2594 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2595 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2596 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2597 } else
2598 #endif
2600 ran_end = bio->bio_offset + bp->b_bufsize;
2601 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2602 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2603 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2604 if (tmp64 < ran_end)
2605 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2606 else
2607 cursor.key_end.key = ran_end + MAXPHYS + 1;
2609 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2611 error = hammer_ip_first(&cursor);
2612 boff = 0;
2614 while (error == 0) {
2616 * Get the base file offset of the record. The key for
2617 * data records is (base + bytes) rather then (base).
2619 base = &cursor.leaf->base;
2620 rec_offset = base->key - cursor.leaf->data_len;
2623 * Calculate the gap, if any, and zero-fill it.
2625 * n is the offset of the start of the record verses our
2626 * current seek offset in the bio.
2628 n = (int)(rec_offset - (bio->bio_offset + boff));
2629 if (n > 0) {
2630 if (n > bp->b_bufsize - boff)
2631 n = bp->b_bufsize - boff;
2632 bzero((char *)bp->b_data + boff, n);
2633 boff += n;
2634 n = 0;
2638 * Calculate the data offset in the record and the number
2639 * of bytes we can copy.
2641 * There are two degenerate cases. First, boff may already
2642 * be at bp->b_bufsize. Secondly, the data offset within
2643 * the record may exceed the record's size.
2645 roff = -n;
2646 rec_offset += roff;
2647 n = cursor.leaf->data_len - roff;
2648 if (n <= 0) {
2649 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2650 n = 0;
2651 } else if (n > bp->b_bufsize - boff) {
2652 n = bp->b_bufsize - boff;
2656 * Deal with cached truncations. This cool bit of code
2657 * allows truncate()/ftruncate() to avoid having to sync
2658 * the file.
2660 * If the frontend is truncated then all backend records are
2661 * subject to the frontend's truncation.
2663 * If the backend is truncated then backend records on-disk
2664 * (but not in-memory) are subject to the backend's
2665 * truncation. In-memory records owned by the backend
2666 * represent data written after the truncation point on the
2667 * backend and must not be truncated.
2669 * Truncate operations deal with frontend buffer cache
2670 * buffers and frontend-owned in-memory records synchronously.
2672 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2673 if (hammer_cursor_ondisk(&cursor)/* ||
2674 cursor.iprec->flush_state == HAMMER_FST_FLUSH*/) {
2675 if (ip->trunc_off <= rec_offset)
2676 n = 0;
2677 else if (ip->trunc_off < rec_offset + n)
2678 n = (int)(ip->trunc_off - rec_offset);
2681 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2682 if (hammer_cursor_ondisk(&cursor)) {
2683 if (ip->sync_trunc_off <= rec_offset)
2684 n = 0;
2685 else if (ip->sync_trunc_off < rec_offset + n)
2686 n = (int)(ip->sync_trunc_off - rec_offset);
2691 * Try to issue a direct read into our bio if possible,
2692 * otherwise resolve the element data into a hammer_buffer
2693 * and copy.
2695 * The buffer on-disk should be zerod past any real
2696 * truncation point, but may not be for any synthesized
2697 * truncation point from above.
2699 disk_offset = cursor.leaf->data_offset + roff;
2700 if (boff == 0 && n == bp->b_bufsize &&
2701 hammer_cursor_ondisk(&cursor) &&
2702 (disk_offset & HAMMER_BUFMASK) == 0) {
2703 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2704 HAMMER_ZONE_LARGE_DATA);
2705 nbio->bio_offset = disk_offset;
2706 error = hammer_io_direct_read(trans.hmp, nbio,
2707 cursor.leaf);
2708 goto done;
2709 } else if (n) {
2710 error = hammer_ip_resolve_data(&cursor);
2711 if (error == 0) {
2712 bcopy((char *)cursor.data + roff,
2713 (char *)bp->b_data + boff, n);
2716 if (error)
2717 break;
2720 * Iterate until we have filled the request.
2722 boff += n;
2723 if (boff == bp->b_bufsize)
2724 break;
2725 error = hammer_ip_next(&cursor);
2729 * There may have been a gap after the last record
2731 if (error == ENOENT)
2732 error = 0;
2733 if (error == 0 && boff != bp->b_bufsize) {
2734 KKASSERT(boff < bp->b_bufsize);
2735 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2736 /* boff = bp->b_bufsize; */
2738 bp->b_resid = 0;
2739 bp->b_error = error;
2740 if (error)
2741 bp->b_flags |= B_ERROR;
2742 biodone(ap->a_bio);
2744 done:
2746 * Cache the b-tree node for the last data read in cache[1].
2748 * If we hit the file EOF then also cache the node in the
2749 * governing director's cache[3], it will be used to initialize
2750 * the inode's cache[1] for any inodes looked up via the directory.
2752 * This doesn't reduce disk accesses since the B-Tree chain is
2753 * likely cached, but it does reduce cpu overhead when looking
2754 * up file offsets for cpdup/tar/cpio style iterations.
2756 if (cursor.node)
2757 hammer_cache_node(&ip->cache[1], cursor.node);
2758 if (ran_end >= ip->ino_data.size) {
2759 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
2760 ip->obj_asof, ip->obj_localization);
2761 if (dip) {
2762 hammer_cache_node(&dip->cache[3], cursor.node);
2763 hammer_rel_inode(dip, 0);
2766 hammer_done_cursor(&cursor);
2767 hammer_done_transaction(&trans);
2768 return(error);
2772 * BMAP operation - used to support cluster_read() only.
2774 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2776 * This routine may return EOPNOTSUPP if the opration is not supported for
2777 * the specified offset. The contents of the pointer arguments do not
2778 * need to be initialized in that case.
2780 * If a disk address is available and properly aligned return 0 with
2781 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2782 * to the run-length relative to that offset. Callers may assume that
2783 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2784 * large, so return EOPNOTSUPP if it is not sufficiently large.
2786 static
2788 hammer_vop_bmap(struct vop_bmap_args *ap)
2790 struct hammer_transaction trans;
2791 struct hammer_inode *ip;
2792 struct hammer_cursor cursor;
2793 hammer_base_elm_t base;
2794 int64_t rec_offset;
2795 int64_t ran_end;
2796 int64_t tmp64;
2797 int64_t base_offset;
2798 int64_t base_disk_offset;
2799 int64_t last_offset;
2800 hammer_off_t last_disk_offset;
2801 hammer_off_t disk_offset;
2802 int rec_len;
2803 int error;
2804 int blksize;
2806 ++hammer_stats_file_iopsr;
2807 ip = ap->a_vp->v_data;
2810 * We can only BMAP regular files. We can't BMAP database files,
2811 * directories, etc.
2813 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2814 return(EOPNOTSUPP);
2817 * bmap is typically called with runp/runb both NULL when used
2818 * for writing. We do not support BMAP for writing atm.
2820 if (ap->a_cmd != BUF_CMD_READ)
2821 return(EOPNOTSUPP);
2824 * Scan the B-Tree to acquire blockmap addresses, then translate
2825 * to raw addresses.
2827 hammer_simple_transaction(&trans, ip->hmp);
2828 #if 0
2829 kprintf("bmap_beg %016llx ip->cache %p\n",
2830 (long long)ap->a_loffset, ip->cache[1]);
2831 #endif
2832 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2835 * Key range (begin and end inclusive) to scan. Note that the key's
2836 * stored in the actual records represent BASE+LEN, not BASE. The
2837 * first record containing bio_offset will have a key > bio_offset.
2839 cursor.key_beg.localization = ip->obj_localization +
2840 HAMMER_LOCALIZE_MISC;
2841 cursor.key_beg.obj_id = ip->obj_id;
2842 cursor.key_beg.create_tid = 0;
2843 cursor.key_beg.delete_tid = 0;
2844 cursor.key_beg.obj_type = 0;
2845 if (ap->a_runb)
2846 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
2847 else
2848 cursor.key_beg.key = ap->a_loffset + 1;
2849 if (cursor.key_beg.key < 0)
2850 cursor.key_beg.key = 0;
2851 cursor.asof = ip->obj_asof;
2852 cursor.flags |= HAMMER_CURSOR_ASOF;
2854 cursor.key_end = cursor.key_beg;
2855 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2857 ran_end = ap->a_loffset + MAXPHYS;
2858 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2859 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2860 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2861 if (tmp64 < ran_end)
2862 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2863 else
2864 cursor.key_end.key = ran_end + MAXPHYS + 1;
2866 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2868 error = hammer_ip_first(&cursor);
2869 base_offset = last_offset = 0;
2870 base_disk_offset = last_disk_offset = 0;
2872 while (error == 0) {
2874 * Get the base file offset of the record. The key for
2875 * data records is (base + bytes) rather then (base).
2877 * NOTE: rec_offset + rec_len may exceed the end-of-file.
2878 * The extra bytes should be zero on-disk and the BMAP op
2879 * should still be ok.
2881 base = &cursor.leaf->base;
2882 rec_offset = base->key - cursor.leaf->data_len;
2883 rec_len = cursor.leaf->data_len;
2886 * Incorporate any cached truncation.
2888 * NOTE: Modifications to rec_len based on synthesized
2889 * truncation points remove the guarantee that any extended
2890 * data on disk is zero (since the truncations may not have
2891 * taken place on-media yet).
2893 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2894 if (hammer_cursor_ondisk(&cursor) ||
2895 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2896 if (ip->trunc_off <= rec_offset)
2897 rec_len = 0;
2898 else if (ip->trunc_off < rec_offset + rec_len)
2899 rec_len = (int)(ip->trunc_off - rec_offset);
2902 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2903 if (hammer_cursor_ondisk(&cursor)) {
2904 if (ip->sync_trunc_off <= rec_offset)
2905 rec_len = 0;
2906 else if (ip->sync_trunc_off < rec_offset + rec_len)
2907 rec_len = (int)(ip->sync_trunc_off - rec_offset);
2912 * Accumulate information. If we have hit a discontiguous
2913 * block reset base_offset unless we are already beyond the
2914 * requested offset. If we are, that's it, we stop.
2916 if (error)
2917 break;
2918 if (hammer_cursor_ondisk(&cursor)) {
2919 disk_offset = cursor.leaf->data_offset;
2920 if (rec_offset != last_offset ||
2921 disk_offset != last_disk_offset) {
2922 if (rec_offset > ap->a_loffset)
2923 break;
2924 base_offset = rec_offset;
2925 base_disk_offset = disk_offset;
2927 last_offset = rec_offset + rec_len;
2928 last_disk_offset = disk_offset + rec_len;
2930 error = hammer_ip_next(&cursor);
2933 #if 0
2934 kprintf("BMAP %016llx: %016llx - %016llx\n",
2935 (long long)ap->a_loffset,
2936 (long long)base_offset,
2937 (long long)last_offset);
2938 kprintf("BMAP %16s: %016llx - %016llx\n", "",
2939 (long long)base_disk_offset,
2940 (long long)last_disk_offset);
2941 #endif
2943 if (cursor.node) {
2944 hammer_cache_node(&ip->cache[1], cursor.node);
2945 #if 0
2946 kprintf("bmap_end2 %016llx ip->cache %p\n",
2947 (long long)ap->a_loffset, ip->cache[1]);
2948 #endif
2950 hammer_done_cursor(&cursor);
2951 hammer_done_transaction(&trans);
2954 * If we couldn't find any records or the records we did find were
2955 * all behind the requested offset, return failure. A forward
2956 * truncation can leave a hole w/ no on-disk records.
2958 if (last_offset == 0 || last_offset < ap->a_loffset)
2959 return (EOPNOTSUPP);
2962 * Figure out the block size at the requested offset and adjust
2963 * our limits so the cluster_read() does not create inappropriately
2964 * sized buffer cache buffers.
2966 blksize = hammer_blocksize(ap->a_loffset);
2967 if (hammer_blocksize(base_offset) != blksize) {
2968 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
2970 if (last_offset != ap->a_loffset &&
2971 hammer_blocksize(last_offset - 1) != blksize) {
2972 last_offset = hammer_blockdemarc(ap->a_loffset,
2973 last_offset - 1);
2977 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
2978 * from occuring.
2980 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
2982 if ((disk_offset & HAMMER_OFF_ZONE_MASK) != HAMMER_ZONE_LARGE_DATA) {
2984 * Only large-data zones can be direct-IOd
2986 error = EOPNOTSUPP;
2987 } else if ((disk_offset & HAMMER_BUFMASK) ||
2988 (last_offset - ap->a_loffset) < blksize) {
2990 * doffsetp is not aligned or the forward run size does
2991 * not cover a whole buffer, disallow the direct I/O.
2993 error = EOPNOTSUPP;
2994 } else {
2996 * We're good.
2998 *ap->a_doffsetp = disk_offset;
2999 if (ap->a_runb) {
3000 *ap->a_runb = ap->a_loffset - base_offset;
3001 KKASSERT(*ap->a_runb >= 0);
3003 if (ap->a_runp) {
3004 *ap->a_runp = last_offset - ap->a_loffset;
3005 KKASSERT(*ap->a_runp >= 0);
3007 error = 0;
3009 return(error);
3013 * Write to a regular file. Because this is a strategy call the OS is
3014 * trying to actually get data onto the media.
3016 static
3018 hammer_vop_strategy_write(struct vop_strategy_args *ap)
3020 hammer_record_t record;
3021 hammer_mount_t hmp;
3022 hammer_inode_t ip;
3023 struct bio *bio;
3024 struct buf *bp;
3025 int blksize;
3026 int bytes;
3027 int error;
3029 bio = ap->a_bio;
3030 bp = bio->bio_buf;
3031 ip = ap->a_vp->v_data;
3032 hmp = ip->hmp;
3034 blksize = hammer_blocksize(bio->bio_offset);
3035 KKASSERT(bp->b_bufsize == blksize);
3037 if (ip->flags & HAMMER_INODE_RO) {
3038 bp->b_error = EROFS;
3039 bp->b_flags |= B_ERROR;
3040 biodone(ap->a_bio);
3041 return(EROFS);
3045 * Interlock with inode destruction (no in-kernel or directory
3046 * topology visibility). If we queue new IO while trying to
3047 * destroy the inode we can deadlock the vtrunc call in
3048 * hammer_inode_unloadable_check().
3050 * Besides, there's no point flushing a bp associated with an
3051 * inode that is being destroyed on-media and has no kernel
3052 * references.
3054 if ((ip->flags | ip->sync_flags) &
3055 (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
3056 bp->b_resid = 0;
3057 biodone(ap->a_bio);
3058 return(0);
3062 * Reserve space and issue a direct-write from the front-end.
3063 * NOTE: The direct_io code will hammer_bread/bcopy smaller
3064 * allocations.
3066 * An in-memory record will be installed to reference the storage
3067 * until the flusher can get to it.
3069 * Since we own the high level bio the front-end will not try to
3070 * do a direct-read until the write completes.
3072 * NOTE: The only time we do not reserve a full-sized buffers
3073 * worth of data is if the file is small. We do not try to
3074 * allocate a fragment (from the small-data zone) at the end of
3075 * an otherwise large file as this can lead to wildly separated
3076 * data.
3078 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
3079 KKASSERT(bio->bio_offset < ip->ino_data.size);
3080 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
3081 bytes = bp->b_bufsize;
3082 else
3083 bytes = ((int)ip->ino_data.size + 15) & ~15;
3085 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
3086 bytes, &error);
3089 * B_VFSFLAG1 indicates that a REDO_WRITE entry was generated
3090 * in hammer_vop_write(). We must flag the record so the proper
3091 * REDO_TERM_WRITE entry is generated during the flush.
3093 if (record) {
3094 if (bp->b_flags & B_VFSFLAG1) {
3095 record->flags |= HAMMER_RECF_REDO;
3096 bp->b_flags &= ~B_VFSFLAG1;
3098 hammer_io_direct_write(hmp, bio, record);
3099 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
3100 hammer_flush_inode(ip, 0);
3101 } else {
3102 bp->b_bio2.bio_offset = NOOFFSET;
3103 bp->b_error = error;
3104 bp->b_flags |= B_ERROR;
3105 biodone(ap->a_bio);
3107 return(error);
3111 * dounlink - disconnect a directory entry
3113 * XXX whiteout support not really in yet
3115 static int
3116 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
3117 struct vnode *dvp, struct ucred *cred,
3118 int flags, int isdir)
3120 struct namecache *ncp;
3121 hammer_inode_t dip;
3122 hammer_inode_t ip;
3123 struct hammer_cursor cursor;
3124 int64_t namekey;
3125 u_int32_t max_iterations;
3126 int nlen, error;
3129 * Calculate the namekey and setup the key range for the scan. This
3130 * works kinda like a chained hash table where the lower 32 bits
3131 * of the namekey synthesize the chain.
3133 * The key range is inclusive of both key_beg and key_end.
3135 dip = VTOI(dvp);
3136 ncp = nch->ncp;
3138 if (dip->flags & HAMMER_INODE_RO)
3139 return (EROFS);
3141 namekey = hammer_directory_namekey(dip, ncp->nc_name, ncp->nc_nlen,
3142 &max_iterations);
3143 retry:
3144 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
3145 cursor.key_beg.localization = dip->obj_localization +
3146 hammer_dir_localization(dip);
3147 cursor.key_beg.obj_id = dip->obj_id;
3148 cursor.key_beg.key = namekey;
3149 cursor.key_beg.create_tid = 0;
3150 cursor.key_beg.delete_tid = 0;
3151 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
3152 cursor.key_beg.obj_type = 0;
3154 cursor.key_end = cursor.key_beg;
3155 cursor.key_end.key += max_iterations;
3156 cursor.asof = dip->obj_asof;
3157 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
3160 * Scan all matching records (the chain), locate the one matching
3161 * the requested path component. info->last_error contains the
3162 * error code on search termination and could be 0, ENOENT, or
3163 * something else.
3165 * The hammer_ip_*() functions merge in-memory records with on-disk
3166 * records for the purposes of the search.
3168 error = hammer_ip_first(&cursor);
3170 while (error == 0) {
3171 error = hammer_ip_resolve_data(&cursor);
3172 if (error)
3173 break;
3174 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
3175 KKASSERT(nlen > 0);
3176 if (ncp->nc_nlen == nlen &&
3177 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
3178 break;
3180 error = hammer_ip_next(&cursor);
3184 * If all is ok we have to get the inode so we can adjust nlinks.
3185 * To avoid a deadlock with the flusher we must release the inode
3186 * lock on the directory when acquiring the inode for the entry.
3188 * If the target is a directory, it must be empty.
3190 if (error == 0) {
3191 hammer_unlock(&cursor.ip->lock);
3192 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
3193 dip->hmp->asof,
3194 cursor.data->entry.localization,
3195 0, &error);
3196 hammer_lock_sh(&cursor.ip->lock);
3197 if (error == ENOENT) {
3198 kprintf("HAMMER: WARNING: Removing "
3199 "dirent w/missing inode \"%s\"\n"
3200 "\tobj_id = %016llx\n",
3201 ncp->nc_name,
3202 (long long)cursor.data->entry.obj_id);
3203 error = 0;
3207 * If isdir >= 0 we validate that the entry is or is not a
3208 * directory. If isdir < 0 we don't care.
3210 if (error == 0 && isdir >= 0 && ip) {
3211 if (isdir &&
3212 ip->ino_data.obj_type != HAMMER_OBJTYPE_DIRECTORY) {
3213 error = ENOTDIR;
3214 } else if (isdir == 0 &&
3215 ip->ino_data.obj_type == HAMMER_OBJTYPE_DIRECTORY) {
3216 error = EISDIR;
3221 * If we are trying to remove a directory the directory must
3222 * be empty.
3224 * The check directory code can loop and deadlock/retry. Our
3225 * own cursor's node locks must be released to avoid a 3-way
3226 * deadlock with the flusher if the check directory code
3227 * blocks.
3229 * If any changes whatsoever have been made to the cursor
3230 * set EDEADLK and retry.
3232 * WARNING: See warnings in hammer_unlock_cursor()
3233 * function.
3235 if (error == 0 && ip && ip->ino_data.obj_type ==
3236 HAMMER_OBJTYPE_DIRECTORY) {
3237 hammer_unlock_cursor(&cursor);
3238 error = hammer_ip_check_directory_empty(trans, ip);
3239 hammer_lock_cursor(&cursor);
3240 if (cursor.flags & HAMMER_CURSOR_RETEST) {
3241 kprintf("HAMMER: Warning: avoided deadlock "
3242 "on rmdir '%s'\n",
3243 ncp->nc_name);
3244 error = EDEADLK;
3249 * Delete the directory entry.
3251 * WARNING: hammer_ip_del_directory() may have to terminate
3252 * the cursor to avoid a deadlock. It is ok to call
3253 * hammer_done_cursor() twice.
3255 if (error == 0) {
3256 error = hammer_ip_del_directory(trans, &cursor,
3257 dip, ip);
3259 hammer_done_cursor(&cursor);
3260 if (error == 0) {
3261 cache_setunresolved(nch);
3262 cache_setvp(nch, NULL);
3265 * XXX locking. Note: ip->vp might get ripped out
3266 * when we setunresolved() the nch since we had
3267 * no other reference to it. In that case ip->vp
3268 * will be NULL.
3270 if (ip && ip->vp) {
3271 hammer_knote(ip->vp, NOTE_DELETE);
3272 cache_inval_vp(ip->vp, CINV_DESTROY);
3275 if (ip)
3276 hammer_rel_inode(ip, 0);
3277 } else {
3278 hammer_done_cursor(&cursor);
3280 if (error == EDEADLK)
3281 goto retry;
3283 return (error);
3286 /************************************************************************
3287 * FIFO AND SPECFS OPS *
3288 ************************************************************************
3292 static int
3293 hammer_vop_fifoclose (struct vop_close_args *ap)
3295 /* XXX update itimes */
3296 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
3299 static int
3300 hammer_vop_fiforead (struct vop_read_args *ap)
3302 int error;
3304 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3305 /* XXX update access time */
3306 return (error);
3309 static int
3310 hammer_vop_fifowrite (struct vop_write_args *ap)
3312 int error;
3314 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3315 /* XXX update access time */
3316 return (error);
3319 static
3321 hammer_vop_fifokqfilter(struct vop_kqfilter_args *ap)
3323 int error;
3325 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
3326 if (error)
3327 error = hammer_vop_kqfilter(ap);
3328 return(error);
3331 /************************************************************************
3332 * KQFILTER OPS *
3333 ************************************************************************
3336 static void filt_hammerdetach(struct knote *kn);
3337 static int filt_hammerread(struct knote *kn, long hint);
3338 static int filt_hammerwrite(struct knote *kn, long hint);
3339 static int filt_hammervnode(struct knote *kn, long hint);
3341 static struct filterops hammerread_filtops =
3342 { 1, NULL, filt_hammerdetach, filt_hammerread };
3343 static struct filterops hammerwrite_filtops =
3344 { 1, NULL, filt_hammerdetach, filt_hammerwrite };
3345 static struct filterops hammervnode_filtops =
3346 { 1, NULL, filt_hammerdetach, filt_hammervnode };
3348 static
3350 hammer_vop_kqfilter(struct vop_kqfilter_args *ap)
3352 struct vnode *vp = ap->a_vp;
3353 struct knote *kn = ap->a_kn;
3354 lwkt_tokref vlock;
3356 switch (kn->kn_filter) {
3357 case EVFILT_READ:
3358 kn->kn_fop = &hammerread_filtops;
3359 break;
3360 case EVFILT_WRITE:
3361 kn->kn_fop = &hammerwrite_filtops;
3362 break;
3363 case EVFILT_VNODE:
3364 kn->kn_fop = &hammervnode_filtops;
3365 break;
3366 default:
3367 return (1);
3370 kn->kn_hook = (caddr_t)vp;
3372 lwkt_gettoken(&vlock, &vp->v_token);
3373 SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext);
3374 lwkt_reltoken(&vlock);
3376 return(0);
3379 static void
3380 filt_hammerdetach(struct knote *kn)
3382 struct vnode *vp = (void *)kn->kn_hook;
3383 lwkt_tokref vlock;
3385 lwkt_gettoken(&vlock, &vp->v_token);
3386 SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note,
3387 kn, knote, kn_selnext);
3388 lwkt_reltoken(&vlock);
3391 static int
3392 filt_hammerread(struct knote *kn, long hint)
3394 struct vnode *vp = (void *)kn->kn_hook;
3395 hammer_inode_t ip = VTOI(vp);
3397 if (hint == NOTE_REVOKE) {
3398 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3399 return(1);
3401 kn->kn_data = ip->ino_data.size - kn->kn_fp->f_offset;
3402 return (kn->kn_data != 0);
3405 static int
3406 filt_hammerwrite(struct knote *kn, long hint)
3408 if (hint == NOTE_REVOKE)
3409 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3410 kn->kn_data = 0;
3411 return (1);
3414 static int
3415 filt_hammervnode(struct knote *kn, long hint)
3417 if (kn->kn_sfflags & hint)
3418 kn->kn_fflags |= hint;
3419 if (hint == NOTE_REVOKE) {
3420 kn->kn_flags |= EV_EOF;
3421 return (1);
3423 return (kn->kn_fflags != 0);