2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
34 * $DragonFly: src/sys/vfs/hammer/hammer_vnops.c,v 1.91.2.4 2008/08/06 15:41:56 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>
46 #include <sys/dirent.h>
47 #include <vm/vm_extern.h>
48 #include <vfs/fifofs/fifo.h>
54 /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
55 static int hammer_vop_fsync(struct vop_fsync_args
*);
56 static int hammer_vop_read(struct vop_read_args
*);
57 static int hammer_vop_write(struct vop_write_args
*);
58 static int hammer_vop_access(struct vop_access_args
*);
59 static int hammer_vop_advlock(struct vop_advlock_args
*);
60 static int hammer_vop_close(struct vop_close_args
*);
61 static int hammer_vop_ncreate(struct vop_ncreate_args
*);
62 static int hammer_vop_getattr(struct vop_getattr_args
*);
63 static int hammer_vop_nresolve(struct vop_nresolve_args
*);
64 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args
*);
65 static int hammer_vop_nlink(struct vop_nlink_args
*);
66 static int hammer_vop_nmkdir(struct vop_nmkdir_args
*);
67 static int hammer_vop_nmknod(struct vop_nmknod_args
*);
68 static int hammer_vop_open(struct vop_open_args
*);
69 static int hammer_vop_pathconf(struct vop_pathconf_args
*);
70 static int hammer_vop_print(struct vop_print_args
*);
71 static int hammer_vop_readdir(struct vop_readdir_args
*);
72 static int hammer_vop_readlink(struct vop_readlink_args
*);
73 static int hammer_vop_nremove(struct vop_nremove_args
*);
74 static int hammer_vop_nrename(struct vop_nrename_args
*);
75 static int hammer_vop_nrmdir(struct vop_nrmdir_args
*);
76 static int hammer_vop_setattr(struct vop_setattr_args
*);
77 static int hammer_vop_strategy(struct vop_strategy_args
*);
78 static int hammer_vop_bmap(struct vop_bmap_args
*ap
);
79 static int hammer_vop_nsymlink(struct vop_nsymlink_args
*);
80 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args
*);
81 static int hammer_vop_ioctl(struct vop_ioctl_args
*);
82 static int hammer_vop_mountctl(struct vop_mountctl_args
*);
84 static int hammer_vop_fifoclose (struct vop_close_args
*);
85 static int hammer_vop_fiforead (struct vop_read_args
*);
86 static int hammer_vop_fifowrite (struct vop_write_args
*);
88 static int hammer_vop_specclose (struct vop_close_args
*);
89 static int hammer_vop_specread (struct vop_read_args
*);
90 static int hammer_vop_specwrite (struct vop_write_args
*);
92 struct vop_ops hammer_vnode_vops
= {
93 .vop_default
= vop_defaultop
,
94 .vop_fsync
= hammer_vop_fsync
,
95 .vop_getpages
= vop_stdgetpages
,
96 .vop_putpages
= vop_stdputpages
,
97 .vop_read
= hammer_vop_read
,
98 .vop_write
= hammer_vop_write
,
99 .vop_access
= hammer_vop_access
,
100 .vop_advlock
= hammer_vop_advlock
,
101 .vop_close
= hammer_vop_close
,
102 .vop_ncreate
= hammer_vop_ncreate
,
103 .vop_getattr
= hammer_vop_getattr
,
104 .vop_inactive
= hammer_vop_inactive
,
105 .vop_reclaim
= hammer_vop_reclaim
,
106 .vop_nresolve
= hammer_vop_nresolve
,
107 .vop_nlookupdotdot
= hammer_vop_nlookupdotdot
,
108 .vop_nlink
= hammer_vop_nlink
,
109 .vop_nmkdir
= hammer_vop_nmkdir
,
110 .vop_nmknod
= hammer_vop_nmknod
,
111 .vop_open
= hammer_vop_open
,
112 .vop_pathconf
= hammer_vop_pathconf
,
113 .vop_print
= hammer_vop_print
,
114 .vop_readdir
= hammer_vop_readdir
,
115 .vop_readlink
= hammer_vop_readlink
,
116 .vop_nremove
= hammer_vop_nremove
,
117 .vop_nrename
= hammer_vop_nrename
,
118 .vop_nrmdir
= hammer_vop_nrmdir
,
119 .vop_setattr
= hammer_vop_setattr
,
120 .vop_bmap
= hammer_vop_bmap
,
121 .vop_strategy
= hammer_vop_strategy
,
122 .vop_nsymlink
= hammer_vop_nsymlink
,
123 .vop_nwhiteout
= hammer_vop_nwhiteout
,
124 .vop_ioctl
= hammer_vop_ioctl
,
125 .vop_mountctl
= hammer_vop_mountctl
128 struct vop_ops hammer_spec_vops
= {
129 .vop_default
= spec_vnoperate
,
130 .vop_fsync
= hammer_vop_fsync
,
131 .vop_read
= hammer_vop_specread
,
132 .vop_write
= hammer_vop_specwrite
,
133 .vop_access
= hammer_vop_access
,
134 .vop_close
= hammer_vop_specclose
,
135 .vop_getattr
= hammer_vop_getattr
,
136 .vop_inactive
= hammer_vop_inactive
,
137 .vop_reclaim
= hammer_vop_reclaim
,
138 .vop_setattr
= hammer_vop_setattr
141 struct vop_ops hammer_fifo_vops
= {
142 .vop_default
= fifo_vnoperate
,
143 .vop_fsync
= hammer_vop_fsync
,
144 .vop_read
= hammer_vop_fiforead
,
145 .vop_write
= hammer_vop_fifowrite
,
146 .vop_access
= hammer_vop_access
,
147 .vop_close
= hammer_vop_fifoclose
,
148 .vop_getattr
= hammer_vop_getattr
,
149 .vop_inactive
= hammer_vop_inactive
,
150 .vop_reclaim
= hammer_vop_reclaim
,
151 .vop_setattr
= hammer_vop_setattr
154 #ifdef DEBUG_TRUNCATE
155 struct hammer_inode
*HammerTruncIp
;
158 static int hammer_dounlink(hammer_transaction_t trans
, struct nchandle
*nch
,
159 struct vnode
*dvp
, struct ucred
*cred
, int flags
);
160 static int hammer_vop_strategy_read(struct vop_strategy_args
*ap
);
161 static int hammer_vop_strategy_write(struct vop_strategy_args
*ap
);
166 hammer_vop_vnoperate(struct vop_generic_args
*)
168 return (VOCALL(&hammer_vnode_vops
, ap
));
173 * hammer_vop_fsync { vp, waitfor }
175 * fsync() an inode to disk and wait for it to be completely committed
176 * such that the information would not be undone if a crash occured after
181 hammer_vop_fsync(struct vop_fsync_args
*ap
)
183 hammer_inode_t ip
= VTOI(ap
->a_vp
);
185 ++hammer_count_fsyncs
;
186 vfsync(ap
->a_vp
, ap
->a_waitfor
, 1, NULL
, NULL
);
187 hammer_flush_inode(ip
, HAMMER_FLUSH_SIGNAL
);
188 if (ap
->a_waitfor
== MNT_WAIT
)
189 hammer_wait_inode(ip
);
194 * hammer_vop_read { vp, uio, ioflag, cred }
198 hammer_vop_read(struct vop_read_args
*ap
)
200 struct hammer_transaction trans
;
211 if (ap
->a_vp
->v_type
!= VREG
)
218 * Allow the UIO's size to override the sequential heuristic.
220 blksize
= hammer_blocksize(uio
->uio_offset
);
221 seqcount
= (uio
->uio_resid
+ (blksize
- 1)) / blksize
;
222 ioseqcount
= ap
->a_ioflag
>> 16;
223 if (seqcount
< ioseqcount
)
224 seqcount
= ioseqcount
;
226 hammer_start_transaction(&trans
, ip
->hmp
);
229 * Access the data typically in HAMMER_BUFSIZE blocks via the
230 * buffer cache, but HAMMER may use a variable block size based
233 while (uio
->uio_resid
> 0 && uio
->uio_offset
< ip
->ino_data
.size
) {
237 blksize
= hammer_blocksize(uio
->uio_offset
);
238 offset
= (int)uio
->uio_offset
& (blksize
- 1);
239 base_offset
= uio
->uio_offset
- offset
;
241 if (hammer_cluster_enable
) {
243 * Use file_limit to prevent cluster_read() from
244 * creating buffers of the wrong block size past
247 file_limit
= ip
->ino_data
.size
;
248 if (base_offset
< HAMMER_XDEMARC
&&
249 file_limit
> HAMMER_XDEMARC
) {
250 file_limit
= HAMMER_XDEMARC
;
252 error
= cluster_read(ap
->a_vp
,
253 file_limit
, base_offset
,
257 error
= bread(ap
->a_vp
, base_offset
, blksize
, &bp
);
260 kprintf("error %d\n", error
);
265 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
266 n
= blksize
- offset
;
267 if (n
> uio
->uio_resid
)
269 if (n
> ip
->ino_data
.size
- uio
->uio_offset
)
270 n
= (int)(ip
->ino_data
.size
- uio
->uio_offset
);
271 error
= uiomove((char *)bp
->b_data
+ offset
, n
, uio
);
273 /* data has a lower priority then meta-data */
274 bp
->b_flags
|= B_AGE
;
278 hammer_stats_file_read
+= n
;
280 if ((ip
->flags
& HAMMER_INODE_RO
) == 0 &&
281 (ip
->hmp
->mp
->mnt_flag
& MNT_NOATIME
) == 0) {
282 ip
->ino_data
.atime
= trans
.time
;
283 hammer_modify_inode(ip
, HAMMER_INODE_ATIME
);
285 hammer_done_transaction(&trans
);
290 * hammer_vop_write { vp, uio, ioflag, cred }
294 hammer_vop_write(struct vop_write_args
*ap
)
296 struct hammer_transaction trans
;
297 struct hammer_inode
*ip
;
309 if (ap
->a_vp
->v_type
!= VREG
)
314 seqcount
= ap
->a_ioflag
>> 16;
316 if (ip
->flags
& HAMMER_INODE_RO
)
320 * Create a transaction to cover the operations we perform.
322 hammer_start_transaction(&trans
, hmp
);
328 if (ap
->a_ioflag
& IO_APPEND
)
329 uio
->uio_offset
= ip
->ino_data
.size
;
332 * Check for illegal write offsets. Valid range is 0...2^63-1.
334 * NOTE: the base_off assignment is required to work around what
335 * I consider to be a GCC-4 optimization bug.
337 if (uio
->uio_offset
< 0) {
338 hammer_done_transaction(&trans
);
341 base_offset
= uio
->uio_offset
+ uio
->uio_resid
; /* work around gcc-4 */
342 if (uio
->uio_resid
> 0 && base_offset
<= 0) {
343 hammer_done_transaction(&trans
);
348 * Access the data typically in HAMMER_BUFSIZE blocks via the
349 * buffer cache, but HAMMER may use a variable block size based
352 while (uio
->uio_resid
> 0) {
357 if ((error
= hammer_checkspace(hmp
, HAMMER_CHKSPC_WRITE
)) != 0)
360 blksize
= hammer_blocksize(uio
->uio_offset
);
363 * Do not allow HAMMER to blow out the buffer cache. Very
364 * large UIOs can lockout other processes due to bwillwrite()
367 * The hammer inode is not locked during these operations.
368 * The vnode is locked which can interfere with the pageout
369 * daemon for non-UIO_NOCOPY writes but should not interfere
370 * with the buffer cache. Even so, we cannot afford to
371 * allow the pageout daemon to build up too many dirty buffer
374 /*if (((int)uio->uio_offset & (blksize - 1)) == 0)*/
378 * Do not allow HAMMER to blow out system memory by
379 * accumulating too many records. Records are so well
380 * decoupled from the buffer cache that it is possible
381 * for userland to push data out to the media via
382 * direct-write, but build up the records queued to the
383 * backend faster then the backend can flush them out.
384 * HAMMER has hit its write limit but the frontend has
385 * no pushback to slow it down.
387 if (hmp
->rsv_recs
> hammer_limit_recs
/ 2) {
389 * Get the inode on the flush list
391 if (ip
->rsv_recs
>= 64)
392 hammer_flush_inode(ip
, HAMMER_FLUSH_SIGNAL
);
393 else if (ip
->rsv_recs
>= 16)
394 hammer_flush_inode(ip
, 0);
397 * Keep the flusher going if the system keeps
400 delta
= hmp
->count_newrecords
-
401 hmp
->last_newrecords
;
402 if (delta
< 0 || delta
> hammer_limit_recs
/ 2) {
403 hmp
->last_newrecords
= hmp
->count_newrecords
;
404 hammer_sync_hmp(hmp
, MNT_NOWAIT
);
408 * If we have gotten behind start slowing
411 delta
= (hmp
->rsv_recs
- hammer_limit_recs
) *
412 hz
/ hammer_limit_recs
;
414 tsleep(&trans
, 0, "hmrslo", delta
);
418 * Calculate the blocksize at the current offset and figure
419 * out how much we can actually write.
421 blkmask
= blksize
- 1;
422 offset
= (int)uio
->uio_offset
& blkmask
;
423 base_offset
= uio
->uio_offset
& ~(int64_t)blkmask
;
424 n
= blksize
- offset
;
425 if (n
> uio
->uio_resid
)
427 if (uio
->uio_offset
+ n
> ip
->ino_data
.size
) {
428 vnode_pager_setsize(ap
->a_vp
, uio
->uio_offset
+ n
);
432 if (uio
->uio_segflg
== UIO_NOCOPY
) {
434 * Issuing a write with the same data backing the
435 * buffer. Instantiate the buffer to collect the
436 * backing vm pages, then read-in any missing bits.
438 * This case is used by vop_stdputpages().
440 bp
= getblk(ap
->a_vp
, base_offset
,
441 blksize
, GETBLK_BHEAVY
, 0);
442 if ((bp
->b_flags
& B_CACHE
) == 0) {
444 error
= bread(ap
->a_vp
, base_offset
,
447 } else if (offset
== 0 && uio
->uio_resid
>= blksize
) {
449 * Even though we are entirely overwriting the buffer
450 * we may still have to zero it out to avoid a
451 * mmap/write visibility issue.
453 bp
= getblk(ap
->a_vp
, base_offset
, blksize
, GETBLK_BHEAVY
, 0);
454 if ((bp
->b_flags
& B_CACHE
) == 0)
456 } else if (base_offset
>= ip
->ino_data
.size
) {
458 * If the base offset of the buffer is beyond the
459 * file EOF, we don't have to issue a read.
461 bp
= getblk(ap
->a_vp
, base_offset
,
462 blksize
, GETBLK_BHEAVY
, 0);
466 * Partial overwrite, read in any missing bits then
467 * replace the portion being written.
469 error
= bread(ap
->a_vp
, base_offset
, blksize
, &bp
);
474 error
= uiomove((char *)bp
->b_data
+ offset
,
479 * If we screwed up we have to undo any VM size changes we
485 vtruncbuf(ap
->a_vp
, ip
->ino_data
.size
,
486 hammer_blocksize(ip
->ino_data
.size
));
490 hammer_stats_file_write
+= n
;
491 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
492 if (ip
->ino_data
.size
< uio
->uio_offset
) {
493 ip
->ino_data
.size
= uio
->uio_offset
;
494 flags
= HAMMER_INODE_DDIRTY
;
495 vnode_pager_setsize(ap
->a_vp
, ip
->ino_data
.size
);
499 ip
->ino_data
.mtime
= trans
.time
;
500 flags
|= HAMMER_INODE_MTIME
| HAMMER_INODE_BUFS
;
501 hammer_modify_inode(ip
, flags
);
504 * Once we dirty the buffer any cached zone-X offset
505 * becomes invalid. HAMMER NOTE: no-history mode cannot
506 * allow overwriting over the same data sector unless
507 * we provide UNDOs for the old data, which we don't.
509 bp
->b_bio2
.bio_offset
= NOOFFSET
;
512 * Final buffer disposition.
514 bp
->b_flags
|= B_AGE
;
515 if (ap
->a_ioflag
& IO_SYNC
) {
517 } else if (ap
->a_ioflag
& IO_DIRECT
) {
523 hammer_done_transaction(&trans
);
528 * hammer_vop_access { vp, mode, cred }
532 hammer_vop_access(struct vop_access_args
*ap
)
534 struct hammer_inode
*ip
= VTOI(ap
->a_vp
);
539 ++hammer_stats_file_iopsr
;
540 uid
= hammer_to_unix_xid(&ip
->ino_data
.uid
);
541 gid
= hammer_to_unix_xid(&ip
->ino_data
.gid
);
543 error
= vop_helper_access(ap
, uid
, gid
, ip
->ino_data
.mode
,
544 ip
->ino_data
.uflags
);
549 * hammer_vop_advlock { vp, id, op, fl, flags }
553 hammer_vop_advlock(struct vop_advlock_args
*ap
)
555 hammer_inode_t ip
= VTOI(ap
->a_vp
);
557 return (lf_advlock(ap
, &ip
->advlock
, ip
->ino_data
.size
));
561 * hammer_vop_close { vp, fflag }
565 hammer_vop_close(struct vop_close_args
*ap
)
567 hammer_inode_t ip
= VTOI(ap
->a_vp
);
569 if ((ip
->flags
| ip
->sync_flags
) & HAMMER_INODE_MODMASK
)
570 hammer_inode_waitreclaims(ip
->hmp
);
571 return (vop_stdclose(ap
));
575 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
577 * The operating system has already ensured that the directory entry
578 * does not exist and done all appropriate namespace locking.
582 hammer_vop_ncreate(struct vop_ncreate_args
*ap
)
584 struct hammer_transaction trans
;
585 struct hammer_inode
*dip
;
586 struct hammer_inode
*nip
;
587 struct nchandle
*nch
;
591 dip
= VTOI(ap
->a_dvp
);
593 if (dip
->flags
& HAMMER_INODE_RO
)
595 if ((error
= hammer_checkspace(dip
->hmp
, HAMMER_CHKSPC_CREATE
)) != 0)
599 * Create a transaction to cover the operations we perform.
601 hammer_start_transaction(&trans
, dip
->hmp
);
602 ++hammer_stats_file_iopsw
;
605 * Create a new filesystem object of the requested type. The
606 * returned inode will be referenced and shared-locked to prevent
607 * it from being moved to the flusher.
610 error
= hammer_create_inode(&trans
, ap
->a_vap
, ap
->a_cred
,
613 hkprintf("hammer_create_inode error %d\n", error
);
614 hammer_done_transaction(&trans
);
620 * Add the new filesystem object to the directory. This will also
621 * bump the inode's link count.
623 error
= hammer_ip_add_directory(&trans
, dip
,
624 nch
->ncp
->nc_name
, nch
->ncp
->nc_nlen
,
627 hkprintf("hammer_ip_add_directory error %d\n", error
);
633 hammer_rel_inode(nip
, 0);
634 hammer_done_transaction(&trans
);
637 error
= hammer_get_vnode(nip
, ap
->a_vpp
);
638 hammer_done_transaction(&trans
);
639 hammer_rel_inode(nip
, 0);
641 cache_setunresolved(ap
->a_nch
);
642 cache_setvp(ap
->a_nch
, *ap
->a_vpp
);
649 * hammer_vop_getattr { vp, vap }
651 * Retrieve an inode's attribute information. When accessing inodes
652 * historically we fake the atime field to ensure consistent results.
653 * The atime field is stored in the B-Tree element and allowed to be
654 * updated without cycling the element.
658 hammer_vop_getattr(struct vop_getattr_args
*ap
)
660 struct hammer_inode
*ip
= VTOI(ap
->a_vp
);
661 struct vattr
*vap
= ap
->a_vap
;
664 * We want the fsid to be different when accessing a filesystem
665 * with different as-of's so programs like diff don't think
666 * the files are the same.
668 * We also want the fsid to be the same when comparing snapshots,
669 * or when comparing mirrors (which might be backed by different
670 * physical devices). HAMMER fsids are based on the PFS's
673 * XXX there is a chance of collision here. The va_fsid reported
674 * by stat is different from the more involved fsid used in the
677 ++hammer_stats_file_iopsr
;
678 vap
->va_fsid
= ip
->pfsm
->fsid_udev
^ (u_int32_t
)ip
->obj_asof
^
679 (u_int32_t
)(ip
->obj_asof
>> 32);
681 vap
->va_fileid
= ip
->ino_leaf
.base
.obj_id
;
682 vap
->va_mode
= ip
->ino_data
.mode
;
683 vap
->va_nlink
= ip
->ino_data
.nlinks
;
684 vap
->va_uid
= hammer_to_unix_xid(&ip
->ino_data
.uid
);
685 vap
->va_gid
= hammer_to_unix_xid(&ip
->ino_data
.gid
);
688 vap
->va_size
= ip
->ino_data
.size
;
691 * Special case for @@PFS softlinks. The actual size of the
692 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
694 if (ip
->ino_data
.obj_type
== HAMMER_OBJTYPE_SOFTLINK
&&
695 ip
->ino_data
.size
== 10 &&
696 ip
->obj_asof
== HAMMER_MAX_TID
&&
697 ip
->obj_localization
== 0 &&
698 strncmp(ip
->ino_data
.ext
.symlink
, "@@PFS", 5) == 0) {
703 * We must provide a consistent atime and mtime for snapshots
704 * so people can do a 'tar cf - ... | md5' on them and get
705 * consistent results.
707 if (ip
->flags
& HAMMER_INODE_RO
) {
708 hammer_time_to_timespec(ip
->ino_data
.ctime
, &vap
->va_atime
);
709 hammer_time_to_timespec(ip
->ino_data
.ctime
, &vap
->va_mtime
);
711 hammer_time_to_timespec(ip
->ino_data
.atime
, &vap
->va_atime
);
712 hammer_time_to_timespec(ip
->ino_data
.mtime
, &vap
->va_mtime
);
714 hammer_time_to_timespec(ip
->ino_data
.ctime
, &vap
->va_ctime
);
715 vap
->va_flags
= ip
->ino_data
.uflags
;
716 vap
->va_gen
= 1; /* hammer inums are unique for all time */
717 vap
->va_blocksize
= HAMMER_BUFSIZE
;
718 if (ip
->ino_data
.size
>= HAMMER_XDEMARC
) {
719 vap
->va_bytes
= (ip
->ino_data
.size
+ HAMMER_XBUFMASK64
) &
721 } else if (ip
->ino_data
.size
> HAMMER_BUFSIZE
/ 2) {
722 vap
->va_bytes
= (ip
->ino_data
.size
+ HAMMER_BUFMASK64
) &
725 vap
->va_bytes
= (ip
->ino_data
.size
+ 15) & ~15;
727 vap
->va_type
= hammer_get_vnode_type(ip
->ino_data
.obj_type
);
728 vap
->va_filerev
= 0; /* XXX */
729 /* mtime uniquely identifies any adjustments made to the file XXX */
730 vap
->va_fsmid
= ip
->ino_data
.mtime
;
731 vap
->va_uid_uuid
= ip
->ino_data
.uid
;
732 vap
->va_gid_uuid
= ip
->ino_data
.gid
;
733 vap
->va_fsid_uuid
= ip
->hmp
->fsid
;
734 vap
->va_vaflags
= VA_UID_UUID_VALID
| VA_GID_UUID_VALID
|
737 switch (ip
->ino_data
.obj_type
) {
738 case HAMMER_OBJTYPE_CDEV
:
739 case HAMMER_OBJTYPE_BDEV
:
740 vap
->va_rmajor
= ip
->ino_data
.rmajor
;
741 vap
->va_rminor
= ip
->ino_data
.rminor
;
750 * hammer_vop_nresolve { nch, dvp, cred }
752 * Locate the requested directory entry.
756 hammer_vop_nresolve(struct vop_nresolve_args
*ap
)
758 struct hammer_transaction trans
;
759 struct namecache
*ncp
;
763 struct hammer_cursor cursor
;
772 u_int32_t localization
;
775 * Misc initialization, plus handle as-of name extensions. Look for
776 * the '@@' extension. Note that as-of files and directories cannot
779 dip
= VTOI(ap
->a_dvp
);
780 ncp
= ap
->a_nch
->ncp
;
781 asof
= dip
->obj_asof
;
783 flags
= dip
->flags
& HAMMER_INODE_RO
;
786 hammer_simple_transaction(&trans
, dip
->hmp
);
787 ++hammer_stats_file_iopsr
;
789 for (i
= 0; i
< nlen
; ++i
) {
790 if (ncp
->nc_name
[i
] == '@' && ncp
->nc_name
[i
+1] == '@') {
791 asof
= hammer_str_to_tid(ncp
->nc_name
+ i
+ 2,
792 &ispfs
, &localization
);
793 if (asof
!= HAMMER_MAX_TID
)
794 flags
|= HAMMER_INODE_RO
;
801 * If this is a PFS softlink we dive into the PFS
803 if (ispfs
&& nlen
== 0) {
804 ip
= hammer_get_inode(&trans
, dip
, HAMMER_OBJID_ROOT
,
808 error
= hammer_get_vnode(ip
, &vp
);
809 hammer_rel_inode(ip
, 0);
815 cache_setvp(ap
->a_nch
, vp
);
822 * If there is no path component the time extension is relative to
826 ip
= hammer_get_inode(&trans
, dip
, dip
->obj_id
,
827 asof
, dip
->obj_localization
,
830 error
= hammer_get_vnode(ip
, &vp
);
831 hammer_rel_inode(ip
, 0);
837 cache_setvp(ap
->a_nch
, vp
);
844 * Calculate the namekey and setup the key range for the scan. This
845 * works kinda like a chained hash table where the lower 32 bits
846 * of the namekey synthesize the chain.
848 * The key range is inclusive of both key_beg and key_end.
850 namekey
= hammer_directory_namekey(ncp
->nc_name
, nlen
);
852 error
= hammer_init_cursor(&trans
, &cursor
, &dip
->cache
[1], dip
);
853 cursor
.key_beg
.localization
= dip
->obj_localization
+
854 HAMMER_LOCALIZE_MISC
;
855 cursor
.key_beg
.obj_id
= dip
->obj_id
;
856 cursor
.key_beg
.key
= namekey
;
857 cursor
.key_beg
.create_tid
= 0;
858 cursor
.key_beg
.delete_tid
= 0;
859 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DIRENTRY
;
860 cursor
.key_beg
.obj_type
= 0;
862 cursor
.key_end
= cursor
.key_beg
;
863 cursor
.key_end
.key
|= 0xFFFFFFFFULL
;
865 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
| HAMMER_CURSOR_ASOF
;
868 * Scan all matching records (the chain), locate the one matching
869 * the requested path component.
871 * The hammer_ip_*() functions merge in-memory records with on-disk
872 * records for the purposes of the search.
875 localization
= HAMMER_DEF_LOCALIZATION
;
878 error
= hammer_ip_first(&cursor
);
880 error
= hammer_ip_resolve_data(&cursor
);
883 if (nlen
== cursor
.leaf
->data_len
- HAMMER_ENTRY_NAME_OFF
&&
884 bcmp(ncp
->nc_name
, cursor
.data
->entry
.name
, nlen
) == 0) {
885 obj_id
= cursor
.data
->entry
.obj_id
;
886 localization
= cursor
.data
->entry
.localization
;
889 error
= hammer_ip_next(&cursor
);
892 hammer_done_cursor(&cursor
);
894 ip
= hammer_get_inode(&trans
, dip
, obj_id
,
898 error
= hammer_get_vnode(ip
, &vp
);
899 hammer_rel_inode(ip
, 0);
905 cache_setvp(ap
->a_nch
, vp
);
908 } else if (error
== ENOENT
) {
909 cache_setvp(ap
->a_nch
, NULL
);
912 hammer_done_transaction(&trans
);
917 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
919 * Locate the parent directory of a directory vnode.
921 * dvp is referenced but not locked. *vpp must be returned referenced and
922 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
923 * at the root, instead it could indicate that the directory we were in was
926 * NOTE: as-of sequences are not linked into the directory structure. If
927 * we are at the root with a different asof then the mount point, reload
928 * the same directory with the mount point's asof. I'm not sure what this
929 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
930 * get confused, but it hasn't been tested.
934 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args
*ap
)
936 struct hammer_transaction trans
;
937 struct hammer_inode
*dip
;
938 struct hammer_inode
*ip
;
939 int64_t parent_obj_id
;
940 u_int32_t parent_obj_localization
;
944 dip
= VTOI(ap
->a_dvp
);
945 asof
= dip
->obj_asof
;
948 * Whos are parent? This could be the root of a pseudo-filesystem
949 * whos parent is in another localization domain.
951 parent_obj_id
= dip
->ino_data
.parent_obj_id
;
952 if (dip
->obj_id
== HAMMER_OBJID_ROOT
)
953 parent_obj_localization
= dip
->ino_data
.ext
.obj
.parent_obj_localization
;
955 parent_obj_localization
= dip
->obj_localization
;
957 if (parent_obj_id
== 0) {
958 if (dip
->obj_id
== HAMMER_OBJID_ROOT
&&
959 asof
!= dip
->hmp
->asof
) {
960 parent_obj_id
= dip
->obj_id
;
961 asof
= dip
->hmp
->asof
;
962 *ap
->a_fakename
= kmalloc(19, M_TEMP
, M_WAITOK
);
963 ksnprintf(*ap
->a_fakename
, 19, "0x%016llx",
971 hammer_simple_transaction(&trans
, dip
->hmp
);
972 ++hammer_stats_file_iopsr
;
974 ip
= hammer_get_inode(&trans
, dip
, parent_obj_id
,
975 asof
, parent_obj_localization
,
978 error
= hammer_get_vnode(ip
, ap
->a_vpp
);
979 hammer_rel_inode(ip
, 0);
983 hammer_done_transaction(&trans
);
988 * hammer_vop_nlink { nch, dvp, vp, cred }
992 hammer_vop_nlink(struct vop_nlink_args
*ap
)
994 struct hammer_transaction trans
;
995 struct hammer_inode
*dip
;
996 struct hammer_inode
*ip
;
997 struct nchandle
*nch
;
1000 if (ap
->a_dvp
->v_mount
!= ap
->a_vp
->v_mount
)
1004 dip
= VTOI(ap
->a_dvp
);
1005 ip
= VTOI(ap
->a_vp
);
1007 if (dip
->obj_localization
!= ip
->obj_localization
)
1010 if (dip
->flags
& HAMMER_INODE_RO
)
1012 if (ip
->flags
& HAMMER_INODE_RO
)
1014 if ((error
= hammer_checkspace(dip
->hmp
, HAMMER_CHKSPC_CREATE
)) != 0)
1018 * Create a transaction to cover the operations we perform.
1020 hammer_start_transaction(&trans
, dip
->hmp
);
1021 ++hammer_stats_file_iopsw
;
1024 * Add the filesystem object to the directory. Note that neither
1025 * dip nor ip are referenced or locked, but their vnodes are
1026 * referenced. This function will bump the inode's link count.
1028 error
= hammer_ip_add_directory(&trans
, dip
,
1029 nch
->ncp
->nc_name
, nch
->ncp
->nc_nlen
,
1036 cache_setunresolved(nch
);
1037 cache_setvp(nch
, ap
->a_vp
);
1039 hammer_done_transaction(&trans
);
1044 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1046 * The operating system has already ensured that the directory entry
1047 * does not exist and done all appropriate namespace locking.
1051 hammer_vop_nmkdir(struct vop_nmkdir_args
*ap
)
1053 struct hammer_transaction trans
;
1054 struct hammer_inode
*dip
;
1055 struct hammer_inode
*nip
;
1056 struct nchandle
*nch
;
1060 dip
= VTOI(ap
->a_dvp
);
1062 if (dip
->flags
& HAMMER_INODE_RO
)
1064 if ((error
= hammer_checkspace(dip
->hmp
, HAMMER_CHKSPC_CREATE
)) != 0)
1068 * Create a transaction to cover the operations we perform.
1070 hammer_start_transaction(&trans
, dip
->hmp
);
1071 ++hammer_stats_file_iopsw
;
1074 * Create a new filesystem object of the requested type. The
1075 * returned inode will be referenced but not locked.
1077 error
= hammer_create_inode(&trans
, ap
->a_vap
, ap
->a_cred
,
1080 hkprintf("hammer_mkdir error %d\n", error
);
1081 hammer_done_transaction(&trans
);
1086 * Add the new filesystem object to the directory. This will also
1087 * bump the inode's link count.
1089 error
= hammer_ip_add_directory(&trans
, dip
,
1090 nch
->ncp
->nc_name
, nch
->ncp
->nc_nlen
,
1093 hkprintf("hammer_mkdir (add) error %d\n", error
);
1099 hammer_rel_inode(nip
, 0);
1102 error
= hammer_get_vnode(nip
, ap
->a_vpp
);
1103 hammer_rel_inode(nip
, 0);
1105 cache_setunresolved(ap
->a_nch
);
1106 cache_setvp(ap
->a_nch
, *ap
->a_vpp
);
1109 hammer_done_transaction(&trans
);
1114 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1116 * The operating system has already ensured that the directory entry
1117 * does not exist and done all appropriate namespace locking.
1121 hammer_vop_nmknod(struct vop_nmknod_args
*ap
)
1123 struct hammer_transaction trans
;
1124 struct hammer_inode
*dip
;
1125 struct hammer_inode
*nip
;
1126 struct nchandle
*nch
;
1130 dip
= VTOI(ap
->a_dvp
);
1132 if (dip
->flags
& HAMMER_INODE_RO
)
1134 if ((error
= hammer_checkspace(dip
->hmp
, HAMMER_CHKSPC_CREATE
)) != 0)
1138 * Create a transaction to cover the operations we perform.
1140 hammer_start_transaction(&trans
, dip
->hmp
);
1141 ++hammer_stats_file_iopsw
;
1144 * Create a new filesystem object of the requested type. The
1145 * returned inode will be referenced but not locked.
1147 * If mknod specifies a directory a pseudo-fs is created.
1149 error
= hammer_create_inode(&trans
, ap
->a_vap
, ap
->a_cred
,
1152 hammer_done_transaction(&trans
);
1158 * Add the new filesystem object to the directory. This will also
1159 * bump the inode's link count.
1161 error
= hammer_ip_add_directory(&trans
, dip
,
1162 nch
->ncp
->nc_name
, nch
->ncp
->nc_nlen
,
1169 hammer_rel_inode(nip
, 0);
1172 error
= hammer_get_vnode(nip
, ap
->a_vpp
);
1173 hammer_rel_inode(nip
, 0);
1175 cache_setunresolved(ap
->a_nch
);
1176 cache_setvp(ap
->a_nch
, *ap
->a_vpp
);
1179 hammer_done_transaction(&trans
);
1184 * hammer_vop_open { vp, mode, cred, fp }
1188 hammer_vop_open(struct vop_open_args
*ap
)
1192 ++hammer_stats_file_iopsr
;
1193 ip
= VTOI(ap
->a_vp
);
1195 if ((ap
->a_mode
& FWRITE
) && (ip
->flags
& HAMMER_INODE_RO
))
1197 return(vop_stdopen(ap
));
1201 * hammer_vop_pathconf { vp, name, retval }
1205 hammer_vop_pathconf(struct vop_pathconf_args
*ap
)
1211 * hammer_vop_print { vp }
1215 hammer_vop_print(struct vop_print_args
*ap
)
1221 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1225 hammer_vop_readdir(struct vop_readdir_args
*ap
)
1227 struct hammer_transaction trans
;
1228 struct hammer_cursor cursor
;
1229 struct hammer_inode
*ip
;
1231 hammer_base_elm_t base
;
1240 ++hammer_stats_file_iopsr
;
1241 ip
= VTOI(ap
->a_vp
);
1243 saveoff
= uio
->uio_offset
;
1245 if (ap
->a_ncookies
) {
1246 ncookies
= uio
->uio_resid
/ 16 + 1;
1247 if (ncookies
> 1024)
1249 cookies
= kmalloc(ncookies
* sizeof(off_t
), M_TEMP
, M_WAITOK
);
1257 hammer_simple_transaction(&trans
, ip
->hmp
);
1260 * Handle artificial entries
1264 r
= vop_write_dirent(&error
, uio
, ip
->obj_id
, DT_DIR
, 1, ".");
1268 cookies
[cookie_index
] = saveoff
;
1271 if (cookie_index
== ncookies
)
1275 if (ip
->ino_data
.parent_obj_id
) {
1276 r
= vop_write_dirent(&error
, uio
,
1277 ip
->ino_data
.parent_obj_id
,
1280 r
= vop_write_dirent(&error
, uio
,
1281 ip
->obj_id
, DT_DIR
, 2, "..");
1286 cookies
[cookie_index
] = saveoff
;
1289 if (cookie_index
== ncookies
)
1294 * Key range (begin and end inclusive) to scan. Directory keys
1295 * directly translate to a 64 bit 'seek' position.
1297 hammer_init_cursor(&trans
, &cursor
, &ip
->cache
[1], ip
);
1298 cursor
.key_beg
.localization
= ip
->obj_localization
+
1299 HAMMER_LOCALIZE_MISC
;
1300 cursor
.key_beg
.obj_id
= ip
->obj_id
;
1301 cursor
.key_beg
.create_tid
= 0;
1302 cursor
.key_beg
.delete_tid
= 0;
1303 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DIRENTRY
;
1304 cursor
.key_beg
.obj_type
= 0;
1305 cursor
.key_beg
.key
= saveoff
;
1307 cursor
.key_end
= cursor
.key_beg
;
1308 cursor
.key_end
.key
= HAMMER_MAX_KEY
;
1309 cursor
.asof
= ip
->obj_asof
;
1310 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
| HAMMER_CURSOR_ASOF
;
1312 error
= hammer_ip_first(&cursor
);
1314 while (error
== 0) {
1315 error
= hammer_ip_resolve_data(&cursor
);
1318 base
= &cursor
.leaf
->base
;
1319 saveoff
= base
->key
;
1320 KKASSERT(cursor
.leaf
->data_len
> HAMMER_ENTRY_NAME_OFF
);
1322 if (base
->obj_id
!= ip
->obj_id
)
1323 panic("readdir: bad record at %p", cursor
.node
);
1326 * Convert pseudo-filesystems into softlinks
1328 dtype
= hammer_get_dtype(cursor
.leaf
->base
.obj_type
);
1329 r
= vop_write_dirent(
1330 &error
, uio
, cursor
.data
->entry
.obj_id
,
1332 cursor
.leaf
->data_len
- HAMMER_ENTRY_NAME_OFF
,
1333 (void *)cursor
.data
->entry
.name
);
1338 cookies
[cookie_index
] = base
->key
;
1340 if (cookie_index
== ncookies
)
1342 error
= hammer_ip_next(&cursor
);
1344 hammer_done_cursor(&cursor
);
1347 hammer_done_transaction(&trans
);
1350 *ap
->a_eofflag
= (error
== ENOENT
);
1351 uio
->uio_offset
= saveoff
;
1352 if (error
&& cookie_index
== 0) {
1353 if (error
== ENOENT
)
1356 kfree(cookies
, M_TEMP
);
1357 *ap
->a_ncookies
= 0;
1358 *ap
->a_cookies
= NULL
;
1361 if (error
== ENOENT
)
1364 *ap
->a_ncookies
= cookie_index
;
1365 *ap
->a_cookies
= cookies
;
1372 * hammer_vop_readlink { vp, uio, cred }
1376 hammer_vop_readlink(struct vop_readlink_args
*ap
)
1378 struct hammer_transaction trans
;
1379 struct hammer_cursor cursor
;
1380 struct hammer_inode
*ip
;
1382 u_int32_t localization
;
1383 hammer_pseudofs_inmem_t pfsm
;
1386 ip
= VTOI(ap
->a_vp
);
1389 * Shortcut if the symlink data was stuffed into ino_data.
1391 * Also expand special "@@PFS%05d" softlinks (expansion only
1392 * occurs for non-historical (current) accesses made from the
1393 * primary filesystem).
1395 if (ip
->ino_data
.size
<= HAMMER_INODE_BASESYMLEN
) {
1399 ptr
= ip
->ino_data
.ext
.symlink
;
1400 bytes
= (int)ip
->ino_data
.size
;
1402 ip
->obj_asof
== HAMMER_MAX_TID
&&
1403 ip
->obj_localization
== 0 &&
1404 strncmp(ptr
, "@@PFS", 5) == 0) {
1405 hammer_simple_transaction(&trans
, ip
->hmp
);
1406 bcopy(ptr
+ 5, buf
, 5);
1408 localization
= strtoul(buf
, NULL
, 10) << 16;
1409 pfsm
= hammer_load_pseudofs(&trans
, localization
,
1412 if (pfsm
->pfsd
.mirror_flags
&
1413 HAMMER_PFSD_SLAVE
) {
1414 ksnprintf(buf
, sizeof(buf
),
1416 pfsm
->pfsd
.sync_end_tid
,
1417 localization
>> 16);
1419 ksnprintf(buf
, sizeof(buf
),
1422 localization
>> 16);
1425 bytes
= strlen(buf
);
1428 hammer_rel_pseudofs(trans
.hmp
, pfsm
);
1429 hammer_done_transaction(&trans
);
1431 error
= uiomove(ptr
, bytes
, ap
->a_uio
);
1438 hammer_simple_transaction(&trans
, ip
->hmp
);
1439 ++hammer_stats_file_iopsr
;
1440 hammer_init_cursor(&trans
, &cursor
, &ip
->cache
[1], ip
);
1443 * Key range (begin and end inclusive) to scan. Directory keys
1444 * directly translate to a 64 bit 'seek' position.
1446 cursor
.key_beg
.localization
= ip
->obj_localization
+
1447 HAMMER_LOCALIZE_MISC
;
1448 cursor
.key_beg
.obj_id
= ip
->obj_id
;
1449 cursor
.key_beg
.create_tid
= 0;
1450 cursor
.key_beg
.delete_tid
= 0;
1451 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_FIX
;
1452 cursor
.key_beg
.obj_type
= 0;
1453 cursor
.key_beg
.key
= HAMMER_FIXKEY_SYMLINK
;
1454 cursor
.asof
= ip
->obj_asof
;
1455 cursor
.flags
|= HAMMER_CURSOR_ASOF
;
1457 error
= hammer_ip_lookup(&cursor
);
1459 error
= hammer_ip_resolve_data(&cursor
);
1461 KKASSERT(cursor
.leaf
->data_len
>=
1462 HAMMER_SYMLINK_NAME_OFF
);
1463 error
= uiomove(cursor
.data
->symlink
.name
,
1464 cursor
.leaf
->data_len
-
1465 HAMMER_SYMLINK_NAME_OFF
,
1469 hammer_done_cursor(&cursor
);
1470 hammer_done_transaction(&trans
);
1475 * hammer_vop_nremove { nch, dvp, cred }
1479 hammer_vop_nremove(struct vop_nremove_args
*ap
)
1481 struct hammer_transaction trans
;
1482 struct hammer_inode
*dip
;
1485 dip
= VTOI(ap
->a_dvp
);
1487 if (hammer_nohistory(dip
) == 0 &&
1488 (error
= hammer_checkspace(dip
->hmp
, HAMMER_CHKSPC_REMOVE
)) != 0) {
1492 hammer_start_transaction(&trans
, dip
->hmp
);
1493 ++hammer_stats_file_iopsw
;
1494 error
= hammer_dounlink(&trans
, ap
->a_nch
, ap
->a_dvp
, ap
->a_cred
, 0);
1495 hammer_done_transaction(&trans
);
1501 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1505 hammer_vop_nrename(struct vop_nrename_args
*ap
)
1507 struct hammer_transaction trans
;
1508 struct namecache
*fncp
;
1509 struct namecache
*tncp
;
1510 struct hammer_inode
*fdip
;
1511 struct hammer_inode
*tdip
;
1512 struct hammer_inode
*ip
;
1513 struct hammer_cursor cursor
;
1517 if (ap
->a_fdvp
->v_mount
!= ap
->a_tdvp
->v_mount
)
1519 if (ap
->a_fdvp
->v_mount
!= ap
->a_fnch
->ncp
->nc_vp
->v_mount
)
1522 fdip
= VTOI(ap
->a_fdvp
);
1523 tdip
= VTOI(ap
->a_tdvp
);
1524 fncp
= ap
->a_fnch
->ncp
;
1525 tncp
= ap
->a_tnch
->ncp
;
1526 ip
= VTOI(fncp
->nc_vp
);
1527 KKASSERT(ip
!= NULL
);
1529 if (fdip
->obj_localization
!= tdip
->obj_localization
)
1531 if (fdip
->obj_localization
!= ip
->obj_localization
)
1534 if (fdip
->flags
& HAMMER_INODE_RO
)
1536 if (tdip
->flags
& HAMMER_INODE_RO
)
1538 if (ip
->flags
& HAMMER_INODE_RO
)
1540 if ((error
= hammer_checkspace(fdip
->hmp
, HAMMER_CHKSPC_CREATE
)) != 0)
1543 hammer_start_transaction(&trans
, fdip
->hmp
);
1544 ++hammer_stats_file_iopsw
;
1547 * Remove tncp from the target directory and then link ip as
1548 * tncp. XXX pass trans to dounlink
1550 * Force the inode sync-time to match the transaction so it is
1551 * in-sync with the creation of the target directory entry.
1553 error
= hammer_dounlink(&trans
, ap
->a_tnch
, ap
->a_tdvp
, ap
->a_cred
, 0);
1554 if (error
== 0 || error
== ENOENT
) {
1555 error
= hammer_ip_add_directory(&trans
, tdip
,
1556 tncp
->nc_name
, tncp
->nc_nlen
,
1559 ip
->ino_data
.parent_obj_id
= tdip
->obj_id
;
1560 hammer_modify_inode(ip
, HAMMER_INODE_DDIRTY
);
1564 goto failed
; /* XXX */
1567 * Locate the record in the originating directory and remove it.
1569 * Calculate the namekey and setup the key range for the scan. This
1570 * works kinda like a chained hash table where the lower 32 bits
1571 * of the namekey synthesize the chain.
1573 * The key range is inclusive of both key_beg and key_end.
1575 namekey
= hammer_directory_namekey(fncp
->nc_name
, fncp
->nc_nlen
);
1577 hammer_init_cursor(&trans
, &cursor
, &fdip
->cache
[1], fdip
);
1578 cursor
.key_beg
.localization
= fdip
->obj_localization
+
1579 HAMMER_LOCALIZE_MISC
;
1580 cursor
.key_beg
.obj_id
= fdip
->obj_id
;
1581 cursor
.key_beg
.key
= namekey
;
1582 cursor
.key_beg
.create_tid
= 0;
1583 cursor
.key_beg
.delete_tid
= 0;
1584 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DIRENTRY
;
1585 cursor
.key_beg
.obj_type
= 0;
1587 cursor
.key_end
= cursor
.key_beg
;
1588 cursor
.key_end
.key
|= 0xFFFFFFFFULL
;
1589 cursor
.asof
= fdip
->obj_asof
;
1590 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
| HAMMER_CURSOR_ASOF
;
1593 * Scan all matching records (the chain), locate the one matching
1594 * the requested path component.
1596 * The hammer_ip_*() functions merge in-memory records with on-disk
1597 * records for the purposes of the search.
1599 error
= hammer_ip_first(&cursor
);
1600 while (error
== 0) {
1601 if (hammer_ip_resolve_data(&cursor
) != 0)
1603 nlen
= cursor
.leaf
->data_len
- HAMMER_ENTRY_NAME_OFF
;
1605 if (fncp
->nc_nlen
== nlen
&&
1606 bcmp(fncp
->nc_name
, cursor
.data
->entry
.name
, nlen
) == 0) {
1609 error
= hammer_ip_next(&cursor
);
1613 * If all is ok we have to get the inode so we can adjust nlinks.
1615 * WARNING: hammer_ip_del_directory() may have to terminate the
1616 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1620 error
= hammer_ip_del_directory(&trans
, &cursor
, fdip
, ip
);
1623 * XXX A deadlock here will break rename's atomicy for the purposes
1624 * of crash recovery.
1626 if (error
== EDEADLK
) {
1627 hammer_done_cursor(&cursor
);
1632 * Cleanup and tell the kernel that the rename succeeded.
1634 hammer_done_cursor(&cursor
);
1636 cache_rename(ap
->a_fnch
, ap
->a_tnch
);
1639 hammer_done_transaction(&trans
);
1644 * hammer_vop_nrmdir { nch, dvp, cred }
1648 hammer_vop_nrmdir(struct vop_nrmdir_args
*ap
)
1650 struct hammer_transaction trans
;
1651 struct hammer_inode
*dip
;
1654 dip
= VTOI(ap
->a_dvp
);
1655 if (dip
->ino_data
.obj_type
!= HAMMER_OBJTYPE_DIRECTORY
)
1658 if (hammer_nohistory(dip
) == 0 &&
1659 (error
= hammer_checkspace(dip
->hmp
, HAMMER_CHKSPC_REMOVE
)) != 0) {
1663 hammer_start_transaction(&trans
, dip
->hmp
);
1664 ++hammer_stats_file_iopsw
;
1665 error
= hammer_dounlink(&trans
, ap
->a_nch
, ap
->a_dvp
, ap
->a_cred
, 0);
1666 hammer_done_transaction(&trans
);
1672 * hammer_vop_setattr { vp, vap, cred }
1676 hammer_vop_setattr(struct vop_setattr_args
*ap
)
1678 struct hammer_transaction trans
;
1680 struct hammer_inode
*ip
;
1685 int64_t aligned_size
;
1689 ip
= ap
->a_vp
->v_data
;
1692 if (ap
->a_vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
1694 if (ip
->flags
& HAMMER_INODE_RO
)
1696 if (hammer_nohistory(ip
) == 0 &&
1697 (error
= hammer_checkspace(ip
->hmp
, HAMMER_CHKSPC_REMOVE
)) != 0) {
1701 hammer_start_transaction(&trans
, ip
->hmp
);
1702 ++hammer_stats_file_iopsw
;
1705 if (vap
->va_flags
!= VNOVAL
) {
1706 flags
= ip
->ino_data
.uflags
;
1707 error
= vop_helper_setattr_flags(&flags
, vap
->va_flags
,
1708 hammer_to_unix_xid(&ip
->ino_data
.uid
),
1711 if (ip
->ino_data
.uflags
!= flags
) {
1712 ip
->ino_data
.uflags
= flags
;
1713 modflags
|= HAMMER_INODE_DDIRTY
;
1715 if (ip
->ino_data
.uflags
& (IMMUTABLE
| APPEND
)) {
1722 if (ip
->ino_data
.uflags
& (IMMUTABLE
| APPEND
)) {
1726 if (vap
->va_uid
!= (uid_t
)VNOVAL
|| vap
->va_gid
!= (gid_t
)VNOVAL
) {
1727 mode_t cur_mode
= ip
->ino_data
.mode
;
1728 uid_t cur_uid
= hammer_to_unix_xid(&ip
->ino_data
.uid
);
1729 gid_t cur_gid
= hammer_to_unix_xid(&ip
->ino_data
.gid
);
1733 error
= vop_helper_chown(ap
->a_vp
, vap
->va_uid
, vap
->va_gid
,
1735 &cur_uid
, &cur_gid
, &cur_mode
);
1737 hammer_guid_to_uuid(&uuid_uid
, cur_uid
);
1738 hammer_guid_to_uuid(&uuid_gid
, cur_gid
);
1739 if (bcmp(&uuid_uid
, &ip
->ino_data
.uid
,
1740 sizeof(uuid_uid
)) ||
1741 bcmp(&uuid_gid
, &ip
->ino_data
.gid
,
1742 sizeof(uuid_gid
)) ||
1743 ip
->ino_data
.mode
!= cur_mode
1745 ip
->ino_data
.uid
= uuid_uid
;
1746 ip
->ino_data
.gid
= uuid_gid
;
1747 ip
->ino_data
.mode
= cur_mode
;
1749 modflags
|= HAMMER_INODE_DDIRTY
;
1752 while (vap
->va_size
!= VNOVAL
&& ip
->ino_data
.size
!= vap
->va_size
) {
1753 switch(ap
->a_vp
->v_type
) {
1755 if (vap
->va_size
== ip
->ino_data
.size
)
1758 * XXX break atomicy, we can deadlock the backend
1759 * if we do not release the lock. Probably not a
1762 blksize
= hammer_blocksize(vap
->va_size
);
1763 if (vap
->va_size
< ip
->ino_data
.size
) {
1764 vtruncbuf(ap
->a_vp
, vap
->va_size
, blksize
);
1767 vnode_pager_setsize(ap
->a_vp
, vap
->va_size
);
1770 ip
->ino_data
.size
= vap
->va_size
;
1771 modflags
|= HAMMER_INODE_DDIRTY
;
1774 * on-media truncation is cached in the inode until
1775 * the inode is synchronized.
1778 hammer_ip_frontend_trunc(ip
, vap
->va_size
);
1779 #ifdef DEBUG_TRUNCATE
1780 if (HammerTruncIp
== NULL
)
1783 if ((ip
->flags
& HAMMER_INODE_TRUNCATED
) == 0) {
1784 ip
->flags
|= HAMMER_INODE_TRUNCATED
;
1785 ip
->trunc_off
= vap
->va_size
;
1786 #ifdef DEBUG_TRUNCATE
1787 if (ip
== HammerTruncIp
)
1788 kprintf("truncate1 %016llx\n", ip
->trunc_off
);
1790 } else if (ip
->trunc_off
> vap
->va_size
) {
1791 ip
->trunc_off
= vap
->va_size
;
1792 #ifdef DEBUG_TRUNCATE
1793 if (ip
== HammerTruncIp
)
1794 kprintf("truncate2 %016llx\n", ip
->trunc_off
);
1797 #ifdef DEBUG_TRUNCATE
1798 if (ip
== HammerTruncIp
)
1799 kprintf("truncate3 %016llx (ignored)\n", vap
->va_size
);
1805 * If truncating we have to clean out a portion of
1806 * the last block on-disk. We do this in the
1807 * front-end buffer cache.
1809 aligned_size
= (vap
->va_size
+ (blksize
- 1)) &
1810 ~(int64_t)(blksize
- 1);
1811 if (truncating
&& vap
->va_size
< aligned_size
) {
1815 aligned_size
-= blksize
;
1817 offset
= (int)vap
->va_size
& (blksize
- 1);
1818 error
= bread(ap
->a_vp
, aligned_size
,
1820 hammer_ip_frontend_trunc(ip
, aligned_size
);
1822 bzero(bp
->b_data
+ offset
,
1824 /* must de-cache direct-io offset */
1825 bp
->b_bio2
.bio_offset
= NOOFFSET
;
1828 kprintf("ERROR %d\n", error
);
1834 if ((ip
->flags
& HAMMER_INODE_TRUNCATED
) == 0) {
1835 ip
->flags
|= HAMMER_INODE_TRUNCATED
;
1836 ip
->trunc_off
= vap
->va_size
;
1837 } else if (ip
->trunc_off
> vap
->va_size
) {
1838 ip
->trunc_off
= vap
->va_size
;
1840 hammer_ip_frontend_trunc(ip
, vap
->va_size
);
1841 ip
->ino_data
.size
= vap
->va_size
;
1842 modflags
|= HAMMER_INODE_DDIRTY
;
1850 if (vap
->va_atime
.tv_sec
!= VNOVAL
) {
1851 ip
->ino_data
.atime
=
1852 hammer_timespec_to_time(&vap
->va_atime
);
1853 modflags
|= HAMMER_INODE_ATIME
;
1855 if (vap
->va_mtime
.tv_sec
!= VNOVAL
) {
1856 ip
->ino_data
.mtime
=
1857 hammer_timespec_to_time(&vap
->va_mtime
);
1858 modflags
|= HAMMER_INODE_MTIME
;
1860 if (vap
->va_mode
!= (mode_t
)VNOVAL
) {
1861 mode_t cur_mode
= ip
->ino_data
.mode
;
1862 uid_t cur_uid
= hammer_to_unix_xid(&ip
->ino_data
.uid
);
1863 gid_t cur_gid
= hammer_to_unix_xid(&ip
->ino_data
.gid
);
1865 error
= vop_helper_chmod(ap
->a_vp
, vap
->va_mode
, ap
->a_cred
,
1866 cur_uid
, cur_gid
, &cur_mode
);
1867 if (error
== 0 && ip
->ino_data
.mode
!= cur_mode
) {
1868 ip
->ino_data
.mode
= cur_mode
;
1869 modflags
|= HAMMER_INODE_DDIRTY
;
1874 hammer_modify_inode(ip
, modflags
);
1875 hammer_done_transaction(&trans
);
1880 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1884 hammer_vop_nsymlink(struct vop_nsymlink_args
*ap
)
1886 struct hammer_transaction trans
;
1887 struct hammer_inode
*dip
;
1888 struct hammer_inode
*nip
;
1889 struct nchandle
*nch
;
1890 hammer_record_t record
;
1894 ap
->a_vap
->va_type
= VLNK
;
1897 dip
= VTOI(ap
->a_dvp
);
1899 if (dip
->flags
& HAMMER_INODE_RO
)
1901 if ((error
= hammer_checkspace(dip
->hmp
, HAMMER_CHKSPC_CREATE
)) != 0)
1905 * Create a transaction to cover the operations we perform.
1907 hammer_start_transaction(&trans
, dip
->hmp
);
1908 ++hammer_stats_file_iopsw
;
1911 * Create a new filesystem object of the requested type. The
1912 * returned inode will be referenced but not locked.
1915 error
= hammer_create_inode(&trans
, ap
->a_vap
, ap
->a_cred
,
1918 hammer_done_transaction(&trans
);
1924 * Add a record representing the symlink. symlink stores the link
1925 * as pure data, not a string, and is no \0 terminated.
1928 bytes
= strlen(ap
->a_target
);
1930 if (bytes
<= HAMMER_INODE_BASESYMLEN
) {
1931 bcopy(ap
->a_target
, nip
->ino_data
.ext
.symlink
, bytes
);
1933 record
= hammer_alloc_mem_record(nip
, bytes
);
1934 record
->type
= HAMMER_MEM_RECORD_GENERAL
;
1936 record
->leaf
.base
.localization
= nip
->obj_localization
+
1937 HAMMER_LOCALIZE_MISC
;
1938 record
->leaf
.base
.key
= HAMMER_FIXKEY_SYMLINK
;
1939 record
->leaf
.base
.rec_type
= HAMMER_RECTYPE_FIX
;
1940 record
->leaf
.data_len
= bytes
;
1941 KKASSERT(HAMMER_SYMLINK_NAME_OFF
== 0);
1942 bcopy(ap
->a_target
, record
->data
->symlink
.name
, bytes
);
1943 error
= hammer_ip_add_record(&trans
, record
);
1947 * Set the file size to the length of the link.
1950 nip
->ino_data
.size
= bytes
;
1951 hammer_modify_inode(nip
, HAMMER_INODE_DDIRTY
);
1955 error
= hammer_ip_add_directory(&trans
, dip
, nch
->ncp
->nc_name
,
1956 nch
->ncp
->nc_nlen
, nip
);
1962 hammer_rel_inode(nip
, 0);
1965 error
= hammer_get_vnode(nip
, ap
->a_vpp
);
1966 hammer_rel_inode(nip
, 0);
1968 cache_setunresolved(ap
->a_nch
);
1969 cache_setvp(ap
->a_nch
, *ap
->a_vpp
);
1972 hammer_done_transaction(&trans
);
1977 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1981 hammer_vop_nwhiteout(struct vop_nwhiteout_args
*ap
)
1983 struct hammer_transaction trans
;
1984 struct hammer_inode
*dip
;
1987 dip
= VTOI(ap
->a_dvp
);
1989 if (hammer_nohistory(dip
) == 0 &&
1990 (error
= hammer_checkspace(dip
->hmp
, HAMMER_CHKSPC_CREATE
)) != 0) {
1994 hammer_start_transaction(&trans
, dip
->hmp
);
1995 ++hammer_stats_file_iopsw
;
1996 error
= hammer_dounlink(&trans
, ap
->a_nch
, ap
->a_dvp
,
1997 ap
->a_cred
, ap
->a_flags
);
1998 hammer_done_transaction(&trans
);
2004 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2008 hammer_vop_ioctl(struct vop_ioctl_args
*ap
)
2010 struct hammer_inode
*ip
= ap
->a_vp
->v_data
;
2012 ++hammer_stats_file_iopsr
;
2013 return(hammer_ioctl(ip
, ap
->a_command
, ap
->a_data
,
2014 ap
->a_fflag
, ap
->a_cred
));
2019 hammer_vop_mountctl(struct vop_mountctl_args
*ap
)
2024 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
2027 case MOUNTCTL_SET_EXPORT
:
2028 if (ap
->a_ctllen
!= sizeof(struct export_args
))
2030 error
= hammer_vfs_export(mp
, ap
->a_op
,
2031 (const struct export_args
*)ap
->a_ctl
);
2034 error
= journal_mountctl(ap
);
2041 * hammer_vop_strategy { vp, bio }
2043 * Strategy call, used for regular file read & write only. Note that the
2044 * bp may represent a cluster.
2046 * To simplify operation and allow better optimizations in the future,
2047 * this code does not make any assumptions with regards to buffer alignment
2052 hammer_vop_strategy(struct vop_strategy_args
*ap
)
2057 bp
= ap
->a_bio
->bio_buf
;
2061 error
= hammer_vop_strategy_read(ap
);
2064 error
= hammer_vop_strategy_write(ap
);
2067 bp
->b_error
= error
= EINVAL
;
2068 bp
->b_flags
|= B_ERROR
;
2076 * Read from a regular file. Iterate the related records and fill in the
2077 * BIO/BUF. Gaps are zero-filled.
2079 * The support code in hammer_object.c should be used to deal with mixed
2080 * in-memory and on-disk records.
2082 * NOTE: Can be called from the cluster code with an oversized buf.
2088 hammer_vop_strategy_read(struct vop_strategy_args
*ap
)
2090 struct hammer_transaction trans
;
2091 struct hammer_inode
*ip
;
2092 struct hammer_cursor cursor
;
2093 hammer_base_elm_t base
;
2094 hammer_off_t disk_offset
;
2108 ip
= ap
->a_vp
->v_data
;
2111 * The zone-2 disk offset may have been set by the cluster code via
2112 * a BMAP operation, or else should be NOOFFSET.
2114 * Checking the high bits for a match against zone-2 should suffice.
2116 nbio
= push_bio(bio
);
2117 if ((nbio
->bio_offset
& HAMMER_OFF_ZONE_MASK
) ==
2118 HAMMER_ZONE_LARGE_DATA
) {
2119 error
= hammer_io_direct_read(ip
->hmp
, nbio
, NULL
);
2124 * Well, that sucked. Do it the hard way. If all the stars are
2125 * aligned we may still be able to issue a direct-read.
2127 hammer_simple_transaction(&trans
, ip
->hmp
);
2128 hammer_init_cursor(&trans
, &cursor
, &ip
->cache
[1], ip
);
2131 * Key range (begin and end inclusive) to scan. Note that the key's
2132 * stored in the actual records represent BASE+LEN, not BASE. The
2133 * first record containing bio_offset will have a key > bio_offset.
2135 cursor
.key_beg
.localization
= ip
->obj_localization
+
2136 HAMMER_LOCALIZE_MISC
;
2137 cursor
.key_beg
.obj_id
= ip
->obj_id
;
2138 cursor
.key_beg
.create_tid
= 0;
2139 cursor
.key_beg
.delete_tid
= 0;
2140 cursor
.key_beg
.obj_type
= 0;
2141 cursor
.key_beg
.key
= bio
->bio_offset
+ 1;
2142 cursor
.asof
= ip
->obj_asof
;
2143 cursor
.flags
|= HAMMER_CURSOR_ASOF
;
2145 cursor
.key_end
= cursor
.key_beg
;
2146 KKASSERT(ip
->ino_data
.obj_type
== HAMMER_OBJTYPE_REGFILE
);
2148 if (ip
->ino_data
.obj_type
== HAMMER_OBJTYPE_DBFILE
) {
2149 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DB
;
2150 cursor
.key_end
.rec_type
= HAMMER_RECTYPE_DB
;
2151 cursor
.key_end
.key
= 0x7FFFFFFFFFFFFFFFLL
;
2155 ran_end
= bio
->bio_offset
+ bp
->b_bufsize
;
2156 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DATA
;
2157 cursor
.key_end
.rec_type
= HAMMER_RECTYPE_DATA
;
2158 tmp64
= ran_end
+ MAXPHYS
+ 1; /* work-around GCC-4 bug */
2159 if (tmp64
< ran_end
)
2160 cursor
.key_end
.key
= 0x7FFFFFFFFFFFFFFFLL
;
2162 cursor
.key_end
.key
= ran_end
+ MAXPHYS
+ 1;
2164 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
;
2166 error
= hammer_ip_first(&cursor
);
2169 while (error
== 0) {
2171 * Get the base file offset of the record. The key for
2172 * data records is (base + bytes) rather then (base).
2174 base
= &cursor
.leaf
->base
;
2175 rec_offset
= base
->key
- cursor
.leaf
->data_len
;
2178 * Calculate the gap, if any, and zero-fill it.
2180 * n is the offset of the start of the record verses our
2181 * current seek offset in the bio.
2183 n
= (int)(rec_offset
- (bio
->bio_offset
+ boff
));
2185 if (n
> bp
->b_bufsize
- boff
)
2186 n
= bp
->b_bufsize
- boff
;
2187 bzero((char *)bp
->b_data
+ boff
, n
);
2193 * Calculate the data offset in the record and the number
2194 * of bytes we can copy.
2196 * There are two degenerate cases. First, boff may already
2197 * be at bp->b_bufsize. Secondly, the data offset within
2198 * the record may exceed the record's size.
2202 n
= cursor
.leaf
->data_len
- roff
;
2204 kprintf("strategy_read: bad n=%d roff=%d\n", n
, roff
);
2206 } else if (n
> bp
->b_bufsize
- boff
) {
2207 n
= bp
->b_bufsize
- boff
;
2211 * Deal with cached truncations. This cool bit of code
2212 * allows truncate()/ftruncate() to avoid having to sync
2215 * If the frontend is truncated then all backend records are
2216 * subject to the frontend's truncation.
2218 * If the backend is truncated then backend records on-disk
2219 * (but not in-memory) are subject to the backend's
2220 * truncation. In-memory records owned by the backend
2221 * represent data written after the truncation point on the
2222 * backend and must not be truncated.
2224 * Truncate operations deal with frontend buffer cache
2225 * buffers and frontend-owned in-memory records synchronously.
2227 if (ip
->flags
& HAMMER_INODE_TRUNCATED
) {
2228 if (hammer_cursor_ondisk(&cursor
) ||
2229 cursor
.iprec
->flush_state
== HAMMER_FST_FLUSH
) {
2230 if (ip
->trunc_off
<= rec_offset
)
2232 else if (ip
->trunc_off
< rec_offset
+ n
)
2233 n
= (int)(ip
->trunc_off
- rec_offset
);
2236 if (ip
->sync_flags
& HAMMER_INODE_TRUNCATED
) {
2237 if (hammer_cursor_ondisk(&cursor
)) {
2238 if (ip
->sync_trunc_off
<= rec_offset
)
2240 else if (ip
->sync_trunc_off
< rec_offset
+ n
)
2241 n
= (int)(ip
->sync_trunc_off
- rec_offset
);
2246 * Try to issue a direct read into our bio if possible,
2247 * otherwise resolve the element data into a hammer_buffer
2250 * The buffer on-disk should be zerod past any real
2251 * truncation point, but may not be for any synthesized
2252 * truncation point from above.
2254 disk_offset
= cursor
.leaf
->data_offset
+ roff
;
2255 if (boff
== 0 && n
== bp
->b_bufsize
&&
2256 hammer_cursor_ondisk(&cursor
) &&
2257 (disk_offset
& HAMMER_BUFMASK
) == 0) {
2258 KKASSERT((disk_offset
& HAMMER_OFF_ZONE_MASK
) ==
2259 HAMMER_ZONE_LARGE_DATA
);
2260 nbio
->bio_offset
= disk_offset
;
2261 error
= hammer_io_direct_read(trans
.hmp
, nbio
,
2265 error
= hammer_ip_resolve_data(&cursor
);
2267 bcopy((char *)cursor
.data
+ roff
,
2268 (char *)bp
->b_data
+ boff
, n
);
2275 * Iterate until we have filled the request.
2278 if (boff
== bp
->b_bufsize
)
2280 error
= hammer_ip_next(&cursor
);
2284 * There may have been a gap after the last record
2286 if (error
== ENOENT
)
2288 if (error
== 0 && boff
!= bp
->b_bufsize
) {
2289 KKASSERT(boff
< bp
->b_bufsize
);
2290 bzero((char *)bp
->b_data
+ boff
, bp
->b_bufsize
- boff
);
2291 /* boff = bp->b_bufsize; */
2294 bp
->b_error
= error
;
2296 bp
->b_flags
|= B_ERROR
;
2301 hammer_cache_node(&ip
->cache
[1], cursor
.node
);
2302 hammer_done_cursor(&cursor
);
2303 hammer_done_transaction(&trans
);
2308 * BMAP operation - used to support cluster_read() only.
2310 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2312 * This routine may return EOPNOTSUPP if the opration is not supported for
2313 * the specified offset. The contents of the pointer arguments do not
2314 * need to be initialized in that case.
2316 * If a disk address is available and properly aligned return 0 with
2317 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2318 * to the run-length relative to that offset. Callers may assume that
2319 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2320 * large, so return EOPNOTSUPP if it is not sufficiently large.
2324 hammer_vop_bmap(struct vop_bmap_args
*ap
)
2326 struct hammer_transaction trans
;
2327 struct hammer_inode
*ip
;
2328 struct hammer_cursor cursor
;
2329 hammer_base_elm_t base
;
2333 int64_t base_offset
;
2334 int64_t base_disk_offset
;
2335 int64_t last_offset
;
2336 hammer_off_t last_disk_offset
;
2337 hammer_off_t disk_offset
;
2342 ++hammer_stats_file_iopsr
;
2343 ip
= ap
->a_vp
->v_data
;
2346 * We can only BMAP regular files. We can't BMAP database files,
2349 if (ip
->ino_data
.obj_type
!= HAMMER_OBJTYPE_REGFILE
)
2353 * bmap is typically called with runp/runb both NULL when used
2354 * for writing. We do not support BMAP for writing atm.
2356 if (ap
->a_cmd
!= BUF_CMD_READ
)
2360 * Scan the B-Tree to acquire blockmap addresses, then translate
2363 hammer_simple_transaction(&trans
, ip
->hmp
);
2365 kprintf("bmap_beg %016llx ip->cache %p\n", ap
->a_loffset
, ip
->cache
[1]);
2367 hammer_init_cursor(&trans
, &cursor
, &ip
->cache
[1], ip
);
2370 * Key range (begin and end inclusive) to scan. Note that the key's
2371 * stored in the actual records represent BASE+LEN, not BASE. The
2372 * first record containing bio_offset will have a key > bio_offset.
2374 cursor
.key_beg
.localization
= ip
->obj_localization
+
2375 HAMMER_LOCALIZE_MISC
;
2376 cursor
.key_beg
.obj_id
= ip
->obj_id
;
2377 cursor
.key_beg
.create_tid
= 0;
2378 cursor
.key_beg
.delete_tid
= 0;
2379 cursor
.key_beg
.obj_type
= 0;
2381 cursor
.key_beg
.key
= ap
->a_loffset
- MAXPHYS
+ 1;
2383 cursor
.key_beg
.key
= ap
->a_loffset
+ 1;
2384 if (cursor
.key_beg
.key
< 0)
2385 cursor
.key_beg
.key
= 0;
2386 cursor
.asof
= ip
->obj_asof
;
2387 cursor
.flags
|= HAMMER_CURSOR_ASOF
;
2389 cursor
.key_end
= cursor
.key_beg
;
2390 KKASSERT(ip
->ino_data
.obj_type
== HAMMER_OBJTYPE_REGFILE
);
2392 ran_end
= ap
->a_loffset
+ MAXPHYS
;
2393 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DATA
;
2394 cursor
.key_end
.rec_type
= HAMMER_RECTYPE_DATA
;
2395 tmp64
= ran_end
+ MAXPHYS
+ 1; /* work-around GCC-4 bug */
2396 if (tmp64
< ran_end
)
2397 cursor
.key_end
.key
= 0x7FFFFFFFFFFFFFFFLL
;
2399 cursor
.key_end
.key
= ran_end
+ MAXPHYS
+ 1;
2401 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
;
2403 error
= hammer_ip_first(&cursor
);
2404 base_offset
= last_offset
= 0;
2405 base_disk_offset
= last_disk_offset
= 0;
2407 while (error
== 0) {
2409 * Get the base file offset of the record. The key for
2410 * data records is (base + bytes) rather then (base).
2412 * NOTE: rec_offset + rec_len may exceed the end-of-file.
2413 * The extra bytes should be zero on-disk and the BMAP op
2414 * should still be ok.
2416 base
= &cursor
.leaf
->base
;
2417 rec_offset
= base
->key
- cursor
.leaf
->data_len
;
2418 rec_len
= cursor
.leaf
->data_len
;
2421 * Incorporate any cached truncation.
2423 * NOTE: Modifications to rec_len based on synthesized
2424 * truncation points remove the guarantee that any extended
2425 * data on disk is zero (since the truncations may not have
2426 * taken place on-media yet).
2428 if (ip
->flags
& HAMMER_INODE_TRUNCATED
) {
2429 if (hammer_cursor_ondisk(&cursor
) ||
2430 cursor
.iprec
->flush_state
== HAMMER_FST_FLUSH
) {
2431 if (ip
->trunc_off
<= rec_offset
)
2433 else if (ip
->trunc_off
< rec_offset
+ rec_len
)
2434 rec_len
= (int)(ip
->trunc_off
- rec_offset
);
2437 if (ip
->sync_flags
& HAMMER_INODE_TRUNCATED
) {
2438 if (hammer_cursor_ondisk(&cursor
)) {
2439 if (ip
->sync_trunc_off
<= rec_offset
)
2441 else if (ip
->sync_trunc_off
< rec_offset
+ rec_len
)
2442 rec_len
= (int)(ip
->sync_trunc_off
- rec_offset
);
2447 * Accumulate information. If we have hit a discontiguous
2448 * block reset base_offset unless we are already beyond the
2449 * requested offset. If we are, that's it, we stop.
2453 if (hammer_cursor_ondisk(&cursor
)) {
2454 disk_offset
= cursor
.leaf
->data_offset
;
2455 if (rec_offset
!= last_offset
||
2456 disk_offset
!= last_disk_offset
) {
2457 if (rec_offset
> ap
->a_loffset
)
2459 base_offset
= rec_offset
;
2460 base_disk_offset
= disk_offset
;
2462 last_offset
= rec_offset
+ rec_len
;
2463 last_disk_offset
= disk_offset
+ rec_len
;
2465 error
= hammer_ip_next(&cursor
);
2469 kprintf("BMAP %016llx: %016llx - %016llx\n",
2470 ap
->a_loffset
, base_offset
, last_offset
);
2471 kprintf("BMAP %16s: %016llx - %016llx\n",
2472 "", base_disk_offset
, last_disk_offset
);
2476 hammer_cache_node(&ip
->cache
[1], cursor
.node
);
2478 kprintf("bmap_end2 %016llx ip->cache %p\n", ap
->a_loffset
, ip
->cache
[1]);
2481 hammer_done_cursor(&cursor
);
2482 hammer_done_transaction(&trans
);
2485 * If we couldn't find any records or the records we did find were
2486 * all behind the requested offset, return failure. A forward
2487 * truncation can leave a hole w/ no on-disk records.
2489 if (last_offset
== 0 || last_offset
< ap
->a_loffset
)
2490 return (EOPNOTSUPP
);
2493 * Figure out the block size at the requested offset and adjust
2494 * our limits so the cluster_read() does not create inappropriately
2495 * sized buffer cache buffers.
2497 blksize
= hammer_blocksize(ap
->a_loffset
);
2498 if (hammer_blocksize(base_offset
) != blksize
) {
2499 base_offset
= hammer_blockdemarc(base_offset
, ap
->a_loffset
);
2501 if (last_offset
!= ap
->a_loffset
&&
2502 hammer_blocksize(last_offset
- 1) != blksize
) {
2503 last_offset
= hammer_blockdemarc(ap
->a_loffset
,
2508 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
2511 disk_offset
= base_disk_offset
+ (ap
->a_loffset
- base_offset
);
2513 if ((disk_offset
& HAMMER_OFF_ZONE_MASK
) != HAMMER_ZONE_LARGE_DATA
) {
2515 * Only large-data zones can be direct-IOd
2518 } else if ((disk_offset
& HAMMER_BUFMASK
) ||
2519 (last_offset
- ap
->a_loffset
) < blksize
) {
2521 * doffsetp is not aligned or the forward run size does
2522 * not cover a whole buffer, disallow the direct I/O.
2529 *ap
->a_doffsetp
= disk_offset
;
2531 *ap
->a_runb
= ap
->a_loffset
- base_offset
;
2532 KKASSERT(*ap
->a_runb
>= 0);
2535 *ap
->a_runp
= last_offset
- ap
->a_loffset
;
2536 KKASSERT(*ap
->a_runp
>= 0);
2544 * Write to a regular file. Because this is a strategy call the OS is
2545 * trying to actually get data onto the media.
2549 hammer_vop_strategy_write(struct vop_strategy_args
*ap
)
2551 hammer_record_t record
;
2562 ip
= ap
->a_vp
->v_data
;
2565 blksize
= hammer_blocksize(bio
->bio_offset
);
2566 KKASSERT(bp
->b_bufsize
== blksize
);
2568 if (ip
->flags
& HAMMER_INODE_RO
) {
2569 bp
->b_error
= EROFS
;
2570 bp
->b_flags
|= B_ERROR
;
2576 * Interlock with inode destruction (no in-kernel or directory
2577 * topology visibility). If we queue new IO while trying to
2578 * destroy the inode we can deadlock the vtrunc call in
2579 * hammer_inode_unloadable_check().
2581 if (ip
->flags
& (HAMMER_INODE_DELETING
|HAMMER_INODE_DELETED
)) {
2588 * Reserve space and issue a direct-write from the front-end.
2589 * NOTE: The direct_io code will hammer_bread/bcopy smaller
2592 * An in-memory record will be installed to reference the storage
2593 * until the flusher can get to it.
2595 * Since we own the high level bio the front-end will not try to
2596 * do a direct-read until the write completes.
2598 * NOTE: The only time we do not reserve a full-sized buffers
2599 * worth of data is if the file is small. We do not try to
2600 * allocate a fragment (from the small-data zone) at the end of
2601 * an otherwise large file as this can lead to wildly separated
2604 KKASSERT((bio
->bio_offset
& HAMMER_BUFMASK
) == 0);
2605 KKASSERT(bio
->bio_offset
< ip
->ino_data
.size
);
2606 if (bio
->bio_offset
|| ip
->ino_data
.size
> HAMMER_BUFSIZE
/ 2)
2607 bytes
= bp
->b_bufsize
;
2609 bytes
= ((int)ip
->ino_data
.size
+ 15) & ~15;
2611 record
= hammer_ip_add_bulk(ip
, bio
->bio_offset
, bp
->b_data
,
2614 hammer_io_direct_write(hmp
, record
, bio
);
2615 if (ip
->rsv_recs
> 1 && hmp
->rsv_recs
> hammer_limit_recs
)
2616 hammer_flush_inode(ip
, 0);
2618 bp
->b_bio2
.bio_offset
= NOOFFSET
;
2619 bp
->b_error
= error
;
2620 bp
->b_flags
|= B_ERROR
;
2627 * dounlink - disconnect a directory entry
2629 * XXX whiteout support not really in yet
2632 hammer_dounlink(hammer_transaction_t trans
, struct nchandle
*nch
,
2633 struct vnode
*dvp
, struct ucred
*cred
, int flags
)
2635 struct namecache
*ncp
;
2638 struct hammer_cursor cursor
;
2643 * Calculate the namekey and setup the key range for the scan. This
2644 * works kinda like a chained hash table where the lower 32 bits
2645 * of the namekey synthesize the chain.
2647 * The key range is inclusive of both key_beg and key_end.
2652 if (dip
->flags
& HAMMER_INODE_RO
)
2655 namekey
= hammer_directory_namekey(ncp
->nc_name
, ncp
->nc_nlen
);
2657 hammer_init_cursor(trans
, &cursor
, &dip
->cache
[1], dip
);
2658 cursor
.key_beg
.localization
= dip
->obj_localization
+
2659 HAMMER_LOCALIZE_MISC
;
2660 cursor
.key_beg
.obj_id
= dip
->obj_id
;
2661 cursor
.key_beg
.key
= namekey
;
2662 cursor
.key_beg
.create_tid
= 0;
2663 cursor
.key_beg
.delete_tid
= 0;
2664 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DIRENTRY
;
2665 cursor
.key_beg
.obj_type
= 0;
2667 cursor
.key_end
= cursor
.key_beg
;
2668 cursor
.key_end
.key
|= 0xFFFFFFFFULL
;
2669 cursor
.asof
= dip
->obj_asof
;
2670 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
| HAMMER_CURSOR_ASOF
;
2673 * Scan all matching records (the chain), locate the one matching
2674 * the requested path component. info->last_error contains the
2675 * error code on search termination and could be 0, ENOENT, or
2678 * The hammer_ip_*() functions merge in-memory records with on-disk
2679 * records for the purposes of the search.
2681 error
= hammer_ip_first(&cursor
);
2683 while (error
== 0) {
2684 error
= hammer_ip_resolve_data(&cursor
);
2687 nlen
= cursor
.leaf
->data_len
- HAMMER_ENTRY_NAME_OFF
;
2689 if (ncp
->nc_nlen
== nlen
&&
2690 bcmp(ncp
->nc_name
, cursor
.data
->entry
.name
, nlen
) == 0) {
2693 error
= hammer_ip_next(&cursor
);
2697 * If all is ok we have to get the inode so we can adjust nlinks.
2698 * To avoid a deadlock with the flusher we must release the inode
2699 * lock on the directory when acquiring the inode for the entry.
2701 * If the target is a directory, it must be empty.
2704 hammer_unlock(&cursor
.ip
->lock
);
2705 ip
= hammer_get_inode(trans
, dip
, cursor
.data
->entry
.obj_id
,
2707 cursor
.data
->entry
.localization
,
2709 hammer_lock_sh(&cursor
.ip
->lock
);
2710 if (error
== ENOENT
) {
2711 kprintf("obj_id %016llx\n", cursor
.data
->entry
.obj_id
);
2712 Debugger("ENOENT unlinking object that should exist");
2716 * If we are trying to remove a directory the directory must
2719 * WARNING: hammer_ip_check_directory_empty() may have to
2720 * terminate the cursor to avoid a deadlock. It is ok to
2721 * call hammer_done_cursor() twice.
2723 if (error
== 0 && ip
->ino_data
.obj_type
==
2724 HAMMER_OBJTYPE_DIRECTORY
) {
2725 error
= hammer_ip_check_directory_empty(trans
, ip
);
2729 * Delete the directory entry.
2731 * WARNING: hammer_ip_del_directory() may have to terminate
2732 * the cursor to avoid a deadlock. It is ok to call
2733 * hammer_done_cursor() twice.
2736 error
= hammer_ip_del_directory(trans
, &cursor
,
2739 hammer_done_cursor(&cursor
);
2741 cache_setunresolved(nch
);
2742 cache_setvp(nch
, NULL
);
2745 cache_inval_vp(ip
->vp
, CINV_DESTROY
);
2748 hammer_rel_inode(ip
, 0);
2750 hammer_done_cursor(&cursor
);
2752 hammer_inode_waitreclaims(dip
->hmp
);
2753 if (error
== EDEADLK
)
2759 /************************************************************************
2760 * FIFO AND SPECFS OPS *
2761 ************************************************************************
2766 hammer_vop_fifoclose (struct vop_close_args
*ap
)
2768 /* XXX update itimes */
2769 return (VOCALL(&fifo_vnode_vops
, &ap
->a_head
));
2773 hammer_vop_fiforead (struct vop_read_args
*ap
)
2777 error
= VOCALL(&fifo_vnode_vops
, &ap
->a_head
);
2778 /* XXX update access time */
2783 hammer_vop_fifowrite (struct vop_write_args
*ap
)
2787 error
= VOCALL(&fifo_vnode_vops
, &ap
->a_head
);
2788 /* XXX update access time */
2793 hammer_vop_specclose (struct vop_close_args
*ap
)
2795 /* XXX update itimes */
2796 return (VOCALL(&spec_vnode_vops
, &ap
->a_head
));
2800 hammer_vop_specread (struct vop_read_args
*ap
)
2802 /* XXX update access time */
2803 return (VOCALL(&spec_vnode_vops
, &ap
->a_head
));
2807 hammer_vop_specwrite (struct vop_write_args
*ap
)
2809 /* XXX update last change time */
2810 return (VOCALL(&spec_vnode_vops
, &ap
->a_head
));