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.90 2008/07/12 23:55:22 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_debug_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
;
279 if ((ip
->flags
& HAMMER_INODE_RO
) == 0 &&
280 (ip
->hmp
->mp
->mnt_flag
& MNT_NOATIME
) == 0) {
281 ip
->ino_data
.atime
= trans
.time
;
282 hammer_modify_inode(ip
, HAMMER_INODE_ATIME
);
284 hammer_done_transaction(&trans
);
289 * hammer_vop_write { vp, uio, ioflag, cred }
293 hammer_vop_write(struct vop_write_args
*ap
)
295 struct hammer_transaction trans
;
296 struct hammer_inode
*ip
;
308 if (ap
->a_vp
->v_type
!= VREG
)
313 seqcount
= ap
->a_ioflag
>> 16;
315 if (ip
->flags
& HAMMER_INODE_RO
)
319 * Create a transaction to cover the operations we perform.
321 hammer_start_transaction(&trans
, hmp
);
327 if (ap
->a_ioflag
& IO_APPEND
)
328 uio
->uio_offset
= ip
->ino_data
.size
;
331 * Check for illegal write offsets. Valid range is 0...2^63-1.
333 * NOTE: the base_off assignment is required to work around what
334 * I consider to be a GCC-4 optimization bug.
336 if (uio
->uio_offset
< 0) {
337 hammer_done_transaction(&trans
);
340 base_offset
= uio
->uio_offset
+ uio
->uio_resid
; /* work around gcc-4 */
341 if (uio
->uio_resid
> 0 && base_offset
<= 0) {
342 hammer_done_transaction(&trans
);
347 * Access the data typically in HAMMER_BUFSIZE blocks via the
348 * buffer cache, but HAMMER may use a variable block size based
351 while (uio
->uio_resid
> 0) {
356 if ((error
= hammer_checkspace(hmp
, HAMMER_CHKSPC_WRITE
)) != 0)
359 blksize
= hammer_blocksize(uio
->uio_offset
);
362 * Do not allow HAMMER to blow out the buffer cache. Very
363 * large UIOs can lockout other processes due to bwillwrite()
366 * The hammer inode is not locked during these operations.
367 * The vnode is locked which can interfere with the pageout
368 * daemon for non-UIO_NOCOPY writes but should not interfere
369 * with the buffer cache. Even so, we cannot afford to
370 * allow the pageout daemon to build up too many dirty buffer
373 /*if (((int)uio->uio_offset & (blksize - 1)) == 0)*/
377 * Do not allow HAMMER to blow out system memory by
378 * accumulating too many records. Records are so well
379 * decoupled from the buffer cache that it is possible
380 * for userland to push data out to the media via
381 * direct-write, but build up the records queued to the
382 * backend faster then the backend can flush them out.
383 * HAMMER has hit its write limit but the frontend has
384 * no pushback to slow it down.
386 if (hmp
->rsv_recs
> hammer_limit_recs
/ 2) {
388 * Get the inode on the flush list
390 if (ip
->rsv_recs
>= 64)
391 hammer_flush_inode(ip
, HAMMER_FLUSH_SIGNAL
);
392 else if (ip
->rsv_recs
>= 16)
393 hammer_flush_inode(ip
, 0);
396 * Keep the flusher going if the system keeps
399 delta
= hmp
->count_newrecords
-
400 hmp
->last_newrecords
;
401 if (delta
< 0 || delta
> hammer_limit_recs
/ 2) {
402 hmp
->last_newrecords
= hmp
->count_newrecords
;
403 hammer_sync_hmp(hmp
, MNT_NOWAIT
);
407 * If we have gotten behind start slowing
410 delta
= (hmp
->rsv_recs
- hammer_limit_recs
) *
411 hz
/ hammer_limit_recs
;
413 tsleep(&trans
, 0, "hmrslo", delta
);
417 * Calculate the blocksize at the current offset and figure
418 * out how much we can actually write.
420 blkmask
= blksize
- 1;
421 offset
= (int)uio
->uio_offset
& blkmask
;
422 base_offset
= uio
->uio_offset
& ~(int64_t)blkmask
;
423 n
= blksize
- offset
;
424 if (n
> uio
->uio_resid
)
426 if (uio
->uio_offset
+ n
> ip
->ino_data
.size
) {
427 vnode_pager_setsize(ap
->a_vp
, uio
->uio_offset
+ n
);
431 if (uio
->uio_segflg
== UIO_NOCOPY
) {
433 * Issuing a write with the same data backing the
434 * buffer. Instantiate the buffer to collect the
435 * backing vm pages, then read-in any missing bits.
437 * This case is used by vop_stdputpages().
439 bp
= getblk(ap
->a_vp
, base_offset
,
440 blksize
, GETBLK_BHEAVY
, 0);
441 if ((bp
->b_flags
& B_CACHE
) == 0) {
443 error
= bread(ap
->a_vp
, base_offset
,
446 } else if (offset
== 0 && uio
->uio_resid
>= blksize
) {
448 * Even though we are entirely overwriting the buffer
449 * we may still have to zero it out to avoid a
450 * mmap/write visibility issue.
452 bp
= getblk(ap
->a_vp
, base_offset
, blksize
, GETBLK_BHEAVY
, 0);
453 if ((bp
->b_flags
& B_CACHE
) == 0)
455 } else if (base_offset
>= ip
->ino_data
.size
) {
457 * If the base offset of the buffer is beyond the
458 * file EOF, we don't have to issue a read.
460 bp
= getblk(ap
->a_vp
, base_offset
,
461 blksize
, GETBLK_BHEAVY
, 0);
465 * Partial overwrite, read in any missing bits then
466 * replace the portion being written.
468 error
= bread(ap
->a_vp
, base_offset
, blksize
, &bp
);
473 error
= uiomove((char *)bp
->b_data
+ offset
,
478 * If we screwed up we have to undo any VM size changes we
484 vtruncbuf(ap
->a_vp
, ip
->ino_data
.size
,
485 hammer_blocksize(ip
->ino_data
.size
));
489 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
490 if (ip
->ino_data
.size
< uio
->uio_offset
) {
491 ip
->ino_data
.size
= uio
->uio_offset
;
492 flags
= HAMMER_INODE_DDIRTY
;
493 vnode_pager_setsize(ap
->a_vp
, ip
->ino_data
.size
);
497 ip
->ino_data
.mtime
= trans
.time
;
498 flags
|= HAMMER_INODE_MTIME
| HAMMER_INODE_BUFS
;
499 hammer_modify_inode(ip
, flags
);
502 * Final buffer disposition.
504 bp
->b_flags
|= B_AGE
;
505 if (ap
->a_ioflag
& IO_SYNC
) {
507 } else if (ap
->a_ioflag
& IO_DIRECT
) {
513 hammer_done_transaction(&trans
);
518 * hammer_vop_access { vp, mode, cred }
522 hammer_vop_access(struct vop_access_args
*ap
)
524 struct hammer_inode
*ip
= VTOI(ap
->a_vp
);
529 uid
= hammer_to_unix_xid(&ip
->ino_data
.uid
);
530 gid
= hammer_to_unix_xid(&ip
->ino_data
.gid
);
532 error
= vop_helper_access(ap
, uid
, gid
, ip
->ino_data
.mode
,
533 ip
->ino_data
.uflags
);
538 * hammer_vop_advlock { vp, id, op, fl, flags }
542 hammer_vop_advlock(struct vop_advlock_args
*ap
)
544 hammer_inode_t ip
= VTOI(ap
->a_vp
);
546 return (lf_advlock(ap
, &ip
->advlock
, ip
->ino_data
.size
));
550 * hammer_vop_close { vp, fflag }
554 hammer_vop_close(struct vop_close_args
*ap
)
556 hammer_inode_t ip
= VTOI(ap
->a_vp
);
558 if ((ip
->flags
| ip
->sync_flags
) & HAMMER_INODE_MODMASK
)
559 hammer_inode_waitreclaims(ip
->hmp
);
560 return (vop_stdclose(ap
));
564 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
566 * The operating system has already ensured that the directory entry
567 * does not exist and done all appropriate namespace locking.
571 hammer_vop_ncreate(struct vop_ncreate_args
*ap
)
573 struct hammer_transaction trans
;
574 struct hammer_inode
*dip
;
575 struct hammer_inode
*nip
;
576 struct nchandle
*nch
;
580 dip
= VTOI(ap
->a_dvp
);
582 if (dip
->flags
& HAMMER_INODE_RO
)
584 if ((error
= hammer_checkspace(dip
->hmp
, HAMMER_CHKSPC_CREATE
)) != 0)
588 * Create a transaction to cover the operations we perform.
590 hammer_start_transaction(&trans
, dip
->hmp
);
593 * Create a new filesystem object of the requested type. The
594 * returned inode will be referenced and shared-locked to prevent
595 * it from being moved to the flusher.
598 error
= hammer_create_inode(&trans
, ap
->a_vap
, ap
->a_cred
,
601 hkprintf("hammer_create_inode error %d\n", error
);
602 hammer_done_transaction(&trans
);
608 * Add the new filesystem object to the directory. This will also
609 * bump the inode's link count.
611 error
= hammer_ip_add_directory(&trans
, dip
,
612 nch
->ncp
->nc_name
, nch
->ncp
->nc_nlen
,
615 hkprintf("hammer_ip_add_directory error %d\n", error
);
621 hammer_rel_inode(nip
, 0);
622 hammer_done_transaction(&trans
);
625 error
= hammer_get_vnode(nip
, ap
->a_vpp
);
626 hammer_done_transaction(&trans
);
627 hammer_rel_inode(nip
, 0);
629 cache_setunresolved(ap
->a_nch
);
630 cache_setvp(ap
->a_nch
, *ap
->a_vpp
);
637 * hammer_vop_getattr { vp, vap }
639 * Retrieve an inode's attribute information. When accessing inodes
640 * historically we fake the atime field to ensure consistent results.
641 * The atime field is stored in the B-Tree element and allowed to be
642 * updated without cycling the element.
646 hammer_vop_getattr(struct vop_getattr_args
*ap
)
648 struct hammer_inode
*ip
= VTOI(ap
->a_vp
);
649 struct vattr
*vap
= ap
->a_vap
;
652 * We want the fsid to be different when accessing a filesystem
653 * with different as-of's so programs like diff don't think
654 * the files are the same.
656 * We also want the fsid to be the same when comparing snapshots,
657 * or when comparing mirrors (which might be backed by different
658 * physical devices). HAMMER fsids are based on the PFS's
661 * XXX there is a chance of collision here. The va_fsid reported
662 * by stat is different from the more involved fsid used in the
665 vap
->va_fsid
= ip
->pfsm
->fsid_udev
^ (u_int32_t
)ip
->obj_asof
^
666 (u_int32_t
)(ip
->obj_asof
>> 32);
668 vap
->va_fileid
= ip
->ino_leaf
.base
.obj_id
;
669 vap
->va_mode
= ip
->ino_data
.mode
;
670 vap
->va_nlink
= ip
->ino_data
.nlinks
;
671 vap
->va_uid
= hammer_to_unix_xid(&ip
->ino_data
.uid
);
672 vap
->va_gid
= hammer_to_unix_xid(&ip
->ino_data
.gid
);
675 vap
->va_size
= ip
->ino_data
.size
;
678 * We must provide a consistent atime and mtime for snapshots
679 * so people can do a 'tar cf - ... | md5' on them and get
680 * consistent results.
682 if (ip
->flags
& HAMMER_INODE_RO
) {
683 hammer_time_to_timespec(ip
->ino_data
.ctime
, &vap
->va_atime
);
684 hammer_time_to_timespec(ip
->ino_data
.ctime
, &vap
->va_mtime
);
686 hammer_time_to_timespec(ip
->ino_data
.atime
, &vap
->va_atime
);
687 hammer_time_to_timespec(ip
->ino_data
.mtime
, &vap
->va_mtime
);
689 hammer_time_to_timespec(ip
->ino_data
.ctime
, &vap
->va_ctime
);
690 vap
->va_flags
= ip
->ino_data
.uflags
;
691 vap
->va_gen
= 1; /* hammer inums are unique for all time */
692 vap
->va_blocksize
= HAMMER_BUFSIZE
;
693 if (ip
->ino_data
.size
>= HAMMER_XDEMARC
) {
694 vap
->va_bytes
= (ip
->ino_data
.size
+ HAMMER_XBUFMASK64
) &
696 } else if (ip
->ino_data
.size
> HAMMER_BUFSIZE
/ 2) {
697 vap
->va_bytes
= (ip
->ino_data
.size
+ HAMMER_BUFMASK64
) &
700 vap
->va_bytes
= (ip
->ino_data
.size
+ 15) & ~15;
702 vap
->va_type
= hammer_get_vnode_type(ip
->ino_data
.obj_type
);
703 vap
->va_filerev
= 0; /* XXX */
704 /* mtime uniquely identifies any adjustments made to the file XXX */
705 vap
->va_fsmid
= ip
->ino_data
.mtime
;
706 vap
->va_uid_uuid
= ip
->ino_data
.uid
;
707 vap
->va_gid_uuid
= ip
->ino_data
.gid
;
708 vap
->va_fsid_uuid
= ip
->hmp
->fsid
;
709 vap
->va_vaflags
= VA_UID_UUID_VALID
| VA_GID_UUID_VALID
|
712 switch (ip
->ino_data
.obj_type
) {
713 case HAMMER_OBJTYPE_CDEV
:
714 case HAMMER_OBJTYPE_BDEV
:
715 vap
->va_rmajor
= ip
->ino_data
.rmajor
;
716 vap
->va_rminor
= ip
->ino_data
.rminor
;
725 * hammer_vop_nresolve { nch, dvp, cred }
727 * Locate the requested directory entry.
731 hammer_vop_nresolve(struct vop_nresolve_args
*ap
)
733 struct hammer_transaction trans
;
734 struct namecache
*ncp
;
738 struct hammer_cursor cursor
;
747 u_int32_t localization
;
750 * Misc initialization, plus handle as-of name extensions. Look for
751 * the '@@' extension. Note that as-of files and directories cannot
754 dip
= VTOI(ap
->a_dvp
);
755 ncp
= ap
->a_nch
->ncp
;
756 asof
= dip
->obj_asof
;
758 flags
= dip
->flags
& HAMMER_INODE_RO
;
761 hammer_simple_transaction(&trans
, dip
->hmp
);
763 for (i
= 0; i
< nlen
; ++i
) {
764 if (ncp
->nc_name
[i
] == '@' && ncp
->nc_name
[i
+1] == '@') {
765 asof
= hammer_str_to_tid(ncp
->nc_name
+ i
+ 2,
766 &ispfs
, &localization
);
767 if (asof
!= HAMMER_MAX_TID
)
768 flags
|= HAMMER_INODE_RO
;
775 * If this is a PFS softlink we dive into the PFS
777 if (ispfs
&& nlen
== 0) {
778 ip
= hammer_get_inode(&trans
, dip
, HAMMER_OBJID_ROOT
,
782 error
= hammer_get_vnode(ip
, &vp
);
783 hammer_rel_inode(ip
, 0);
789 cache_setvp(ap
->a_nch
, vp
);
796 * If there is no path component the time extension is relative to
800 ip
= hammer_get_inode(&trans
, dip
, dip
->obj_id
,
801 asof
, dip
->obj_localization
,
804 error
= hammer_get_vnode(ip
, &vp
);
805 hammer_rel_inode(ip
, 0);
811 cache_setvp(ap
->a_nch
, vp
);
818 * Calculate the namekey and setup the key range for the scan. This
819 * works kinda like a chained hash table where the lower 32 bits
820 * of the namekey synthesize the chain.
822 * The key range is inclusive of both key_beg and key_end.
824 namekey
= hammer_directory_namekey(ncp
->nc_name
, nlen
);
826 error
= hammer_init_cursor(&trans
, &cursor
, &dip
->cache
[1], dip
);
827 cursor
.key_beg
.localization
= dip
->obj_localization
+
828 HAMMER_LOCALIZE_MISC
;
829 cursor
.key_beg
.obj_id
= dip
->obj_id
;
830 cursor
.key_beg
.key
= namekey
;
831 cursor
.key_beg
.create_tid
= 0;
832 cursor
.key_beg
.delete_tid
= 0;
833 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DIRENTRY
;
834 cursor
.key_beg
.obj_type
= 0;
836 cursor
.key_end
= cursor
.key_beg
;
837 cursor
.key_end
.key
|= 0xFFFFFFFFULL
;
839 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
| HAMMER_CURSOR_ASOF
;
842 * Scan all matching records (the chain), locate the one matching
843 * the requested path component.
845 * The hammer_ip_*() functions merge in-memory records with on-disk
846 * records for the purposes of the search.
849 localization
= HAMMER_DEF_LOCALIZATION
;
852 error
= hammer_ip_first(&cursor
);
854 error
= hammer_ip_resolve_data(&cursor
);
857 if (nlen
== cursor
.leaf
->data_len
- HAMMER_ENTRY_NAME_OFF
&&
858 bcmp(ncp
->nc_name
, cursor
.data
->entry
.name
, nlen
) == 0) {
859 obj_id
= cursor
.data
->entry
.obj_id
;
860 localization
= cursor
.data
->entry
.localization
;
863 error
= hammer_ip_next(&cursor
);
866 hammer_done_cursor(&cursor
);
868 ip
= hammer_get_inode(&trans
, dip
, obj_id
,
872 error
= hammer_get_vnode(ip
, &vp
);
873 hammer_rel_inode(ip
, 0);
879 cache_setvp(ap
->a_nch
, vp
);
882 } else if (error
== ENOENT
) {
883 cache_setvp(ap
->a_nch
, NULL
);
886 hammer_done_transaction(&trans
);
891 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
893 * Locate the parent directory of a directory vnode.
895 * dvp is referenced but not locked. *vpp must be returned referenced and
896 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
897 * at the root, instead it could indicate that the directory we were in was
900 * NOTE: as-of sequences are not linked into the directory structure. If
901 * we are at the root with a different asof then the mount point, reload
902 * the same directory with the mount point's asof. I'm not sure what this
903 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
904 * get confused, but it hasn't been tested.
908 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args
*ap
)
910 struct hammer_transaction trans
;
911 struct hammer_inode
*dip
;
912 struct hammer_inode
*ip
;
913 int64_t parent_obj_id
;
914 u_int32_t parent_obj_localization
;
918 dip
= VTOI(ap
->a_dvp
);
919 asof
= dip
->obj_asof
;
922 * Whos are parent? This could be the root of a pseudo-filesystem
923 * whos parent is in another localization domain.
925 parent_obj_id
= dip
->ino_data
.parent_obj_id
;
926 if (dip
->obj_id
== HAMMER_OBJID_ROOT
)
927 parent_obj_localization
= dip
->ino_data
.ext
.obj
.parent_obj_localization
;
929 parent_obj_localization
= dip
->obj_localization
;
931 if (parent_obj_id
== 0) {
932 if (dip
->obj_id
== HAMMER_OBJID_ROOT
&&
933 asof
!= dip
->hmp
->asof
) {
934 parent_obj_id
= dip
->obj_id
;
935 asof
= dip
->hmp
->asof
;
936 *ap
->a_fakename
= kmalloc(19, M_TEMP
, M_WAITOK
);
937 ksnprintf(*ap
->a_fakename
, 19, "0x%016llx",
945 hammer_simple_transaction(&trans
, dip
->hmp
);
947 ip
= hammer_get_inode(&trans
, dip
, parent_obj_id
,
948 asof
, parent_obj_localization
,
951 error
= hammer_get_vnode(ip
, ap
->a_vpp
);
952 hammer_rel_inode(ip
, 0);
956 hammer_done_transaction(&trans
);
961 * hammer_vop_nlink { nch, dvp, vp, cred }
965 hammer_vop_nlink(struct vop_nlink_args
*ap
)
967 struct hammer_transaction trans
;
968 struct hammer_inode
*dip
;
969 struct hammer_inode
*ip
;
970 struct nchandle
*nch
;
974 dip
= VTOI(ap
->a_dvp
);
977 if (dip
->flags
& HAMMER_INODE_RO
)
979 if (ip
->flags
& HAMMER_INODE_RO
)
981 if ((error
= hammer_checkspace(dip
->hmp
, HAMMER_CHKSPC_CREATE
)) != 0)
985 * Create a transaction to cover the operations we perform.
987 hammer_start_transaction(&trans
, dip
->hmp
);
990 * Add the filesystem object to the directory. Note that neither
991 * dip nor ip are referenced or locked, but their vnodes are
992 * referenced. This function will bump the inode's link count.
994 error
= hammer_ip_add_directory(&trans
, dip
,
995 nch
->ncp
->nc_name
, nch
->ncp
->nc_nlen
,
1002 cache_setunresolved(nch
);
1003 cache_setvp(nch
, ap
->a_vp
);
1005 hammer_done_transaction(&trans
);
1010 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1012 * The operating system has already ensured that the directory entry
1013 * does not exist and done all appropriate namespace locking.
1017 hammer_vop_nmkdir(struct vop_nmkdir_args
*ap
)
1019 struct hammer_transaction trans
;
1020 struct hammer_inode
*dip
;
1021 struct hammer_inode
*nip
;
1022 struct nchandle
*nch
;
1026 dip
= VTOI(ap
->a_dvp
);
1028 if (dip
->flags
& HAMMER_INODE_RO
)
1030 if ((error
= hammer_checkspace(dip
->hmp
, HAMMER_CHKSPC_CREATE
)) != 0)
1034 * Create a transaction to cover the operations we perform.
1036 hammer_start_transaction(&trans
, dip
->hmp
);
1039 * Create a new filesystem object of the requested type. The
1040 * returned inode will be referenced but not locked.
1042 error
= hammer_create_inode(&trans
, ap
->a_vap
, ap
->a_cred
,
1045 hkprintf("hammer_mkdir error %d\n", error
);
1046 hammer_done_transaction(&trans
);
1051 * Add the new filesystem object to the directory. This will also
1052 * bump the inode's link count.
1054 error
= hammer_ip_add_directory(&trans
, dip
,
1055 nch
->ncp
->nc_name
, nch
->ncp
->nc_nlen
,
1058 hkprintf("hammer_mkdir (add) error %d\n", error
);
1064 hammer_rel_inode(nip
, 0);
1067 error
= hammer_get_vnode(nip
, ap
->a_vpp
);
1068 hammer_rel_inode(nip
, 0);
1070 cache_setunresolved(ap
->a_nch
);
1071 cache_setvp(ap
->a_nch
, *ap
->a_vpp
);
1074 hammer_done_transaction(&trans
);
1079 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1081 * The operating system has already ensured that the directory entry
1082 * does not exist and done all appropriate namespace locking.
1086 hammer_vop_nmknod(struct vop_nmknod_args
*ap
)
1088 struct hammer_transaction trans
;
1089 struct hammer_inode
*dip
;
1090 struct hammer_inode
*nip
;
1091 struct nchandle
*nch
;
1095 dip
= VTOI(ap
->a_dvp
);
1097 if (dip
->flags
& HAMMER_INODE_RO
)
1099 if ((error
= hammer_checkspace(dip
->hmp
, HAMMER_CHKSPC_CREATE
)) != 0)
1103 * Create a transaction to cover the operations we perform.
1105 hammer_start_transaction(&trans
, dip
->hmp
);
1108 * Create a new filesystem object of the requested type. The
1109 * returned inode will be referenced but not locked.
1111 * If mknod specifies a directory a pseudo-fs is created.
1113 error
= hammer_create_inode(&trans
, ap
->a_vap
, ap
->a_cred
,
1116 hammer_done_transaction(&trans
);
1122 * Add the new filesystem object to the directory. This will also
1123 * bump the inode's link count.
1125 error
= hammer_ip_add_directory(&trans
, dip
,
1126 nch
->ncp
->nc_name
, nch
->ncp
->nc_nlen
,
1133 hammer_rel_inode(nip
, 0);
1136 error
= hammer_get_vnode(nip
, ap
->a_vpp
);
1137 hammer_rel_inode(nip
, 0);
1139 cache_setunresolved(ap
->a_nch
);
1140 cache_setvp(ap
->a_nch
, *ap
->a_vpp
);
1143 hammer_done_transaction(&trans
);
1148 * hammer_vop_open { vp, mode, cred, fp }
1152 hammer_vop_open(struct vop_open_args
*ap
)
1156 ip
= VTOI(ap
->a_vp
);
1158 if ((ap
->a_mode
& FWRITE
) && (ip
->flags
& HAMMER_INODE_RO
))
1160 return(vop_stdopen(ap
));
1164 * hammer_vop_pathconf { vp, name, retval }
1168 hammer_vop_pathconf(struct vop_pathconf_args
*ap
)
1174 * hammer_vop_print { vp }
1178 hammer_vop_print(struct vop_print_args
*ap
)
1184 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1188 hammer_vop_readdir(struct vop_readdir_args
*ap
)
1190 struct hammer_transaction trans
;
1191 struct hammer_cursor cursor
;
1192 struct hammer_inode
*ip
;
1194 hammer_base_elm_t base
;
1203 ip
= VTOI(ap
->a_vp
);
1205 saveoff
= uio
->uio_offset
;
1207 if (ap
->a_ncookies
) {
1208 ncookies
= uio
->uio_resid
/ 16 + 1;
1209 if (ncookies
> 1024)
1211 cookies
= kmalloc(ncookies
* sizeof(off_t
), M_TEMP
, M_WAITOK
);
1219 hammer_simple_transaction(&trans
, ip
->hmp
);
1222 * Handle artificial entries
1226 r
= vop_write_dirent(&error
, uio
, ip
->obj_id
, DT_DIR
, 1, ".");
1230 cookies
[cookie_index
] = saveoff
;
1233 if (cookie_index
== ncookies
)
1237 if (ip
->ino_data
.parent_obj_id
) {
1238 r
= vop_write_dirent(&error
, uio
,
1239 ip
->ino_data
.parent_obj_id
,
1242 r
= vop_write_dirent(&error
, uio
,
1243 ip
->obj_id
, DT_DIR
, 2, "..");
1248 cookies
[cookie_index
] = saveoff
;
1251 if (cookie_index
== ncookies
)
1256 * Key range (begin and end inclusive) to scan. Directory keys
1257 * directly translate to a 64 bit 'seek' position.
1259 hammer_init_cursor(&trans
, &cursor
, &ip
->cache
[1], ip
);
1260 cursor
.key_beg
.localization
= ip
->obj_localization
+
1261 HAMMER_LOCALIZE_MISC
;
1262 cursor
.key_beg
.obj_id
= ip
->obj_id
;
1263 cursor
.key_beg
.create_tid
= 0;
1264 cursor
.key_beg
.delete_tid
= 0;
1265 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DIRENTRY
;
1266 cursor
.key_beg
.obj_type
= 0;
1267 cursor
.key_beg
.key
= saveoff
;
1269 cursor
.key_end
= cursor
.key_beg
;
1270 cursor
.key_end
.key
= HAMMER_MAX_KEY
;
1271 cursor
.asof
= ip
->obj_asof
;
1272 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
| HAMMER_CURSOR_ASOF
;
1274 error
= hammer_ip_first(&cursor
);
1276 while (error
== 0) {
1277 error
= hammer_ip_resolve_data(&cursor
);
1280 base
= &cursor
.leaf
->base
;
1281 saveoff
= base
->key
;
1282 KKASSERT(cursor
.leaf
->data_len
> HAMMER_ENTRY_NAME_OFF
);
1284 if (base
->obj_id
!= ip
->obj_id
)
1285 panic("readdir: bad record at %p", cursor
.node
);
1288 * Convert pseudo-filesystems into softlinks
1290 dtype
= hammer_get_dtype(cursor
.leaf
->base
.obj_type
);
1291 r
= vop_write_dirent(
1292 &error
, uio
, cursor
.data
->entry
.obj_id
,
1294 cursor
.leaf
->data_len
- HAMMER_ENTRY_NAME_OFF
,
1295 (void *)cursor
.data
->entry
.name
);
1300 cookies
[cookie_index
] = base
->key
;
1302 if (cookie_index
== ncookies
)
1304 error
= hammer_ip_next(&cursor
);
1306 hammer_done_cursor(&cursor
);
1309 hammer_done_transaction(&trans
);
1312 *ap
->a_eofflag
= (error
== ENOENT
);
1313 uio
->uio_offset
= saveoff
;
1314 if (error
&& cookie_index
== 0) {
1315 if (error
== ENOENT
)
1318 kfree(cookies
, M_TEMP
);
1319 *ap
->a_ncookies
= 0;
1320 *ap
->a_cookies
= NULL
;
1323 if (error
== ENOENT
)
1326 *ap
->a_ncookies
= cookie_index
;
1327 *ap
->a_cookies
= cookies
;
1334 * hammer_vop_readlink { vp, uio, cred }
1338 hammer_vop_readlink(struct vop_readlink_args
*ap
)
1340 struct hammer_transaction trans
;
1341 struct hammer_cursor cursor
;
1342 struct hammer_inode
*ip
;
1344 u_int32_t localization
;
1345 hammer_pseudofs_inmem_t pfsm
;
1348 ip
= VTOI(ap
->a_vp
);
1351 * Shortcut if the symlink data was stuffed into ino_data.
1353 * Also expand special "@@PFS%05d" softlinks (expansion only
1354 * occurs for non-historical (current) accesses made from the
1355 * primary filesystem).
1357 if (ip
->ino_data
.size
<= HAMMER_INODE_BASESYMLEN
) {
1361 ptr
= ip
->ino_data
.ext
.symlink
;
1362 bytes
= (int)ip
->ino_data
.size
;
1364 ip
->obj_asof
== HAMMER_MAX_TID
&&
1365 ip
->obj_localization
== 0 &&
1366 strncmp(ptr
, "@@PFS", 5) == 0) {
1367 hammer_simple_transaction(&trans
, ip
->hmp
);
1368 bcopy(ptr
+ 5, buf
, 5);
1370 localization
= strtoul(buf
, NULL
, 10) << 16;
1371 pfsm
= hammer_load_pseudofs(&trans
, localization
,
1374 if (pfsm
->pfsd
.mirror_flags
&
1375 HAMMER_PFSD_SLAVE
) {
1376 ksnprintf(buf
, sizeof(buf
),
1378 pfsm
->pfsd
.sync_end_tid
,
1379 localization
>> 16);
1381 ksnprintf(buf
, sizeof(buf
),
1384 localization
>> 16);
1387 bytes
= strlen(buf
);
1390 hammer_rel_pseudofs(trans
.hmp
, pfsm
);
1391 hammer_done_transaction(&trans
);
1393 error
= uiomove(ptr
, bytes
, ap
->a_uio
);
1400 hammer_simple_transaction(&trans
, ip
->hmp
);
1401 hammer_init_cursor(&trans
, &cursor
, &ip
->cache
[1], ip
);
1404 * Key range (begin and end inclusive) to scan. Directory keys
1405 * directly translate to a 64 bit 'seek' position.
1407 cursor
.key_beg
.localization
= ip
->obj_localization
+
1408 HAMMER_LOCALIZE_MISC
;
1409 cursor
.key_beg
.obj_id
= ip
->obj_id
;
1410 cursor
.key_beg
.create_tid
= 0;
1411 cursor
.key_beg
.delete_tid
= 0;
1412 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_FIX
;
1413 cursor
.key_beg
.obj_type
= 0;
1414 cursor
.key_beg
.key
= HAMMER_FIXKEY_SYMLINK
;
1415 cursor
.asof
= ip
->obj_asof
;
1416 cursor
.flags
|= HAMMER_CURSOR_ASOF
;
1418 error
= hammer_ip_lookup(&cursor
);
1420 error
= hammer_ip_resolve_data(&cursor
);
1422 KKASSERT(cursor
.leaf
->data_len
>=
1423 HAMMER_SYMLINK_NAME_OFF
);
1424 error
= uiomove(cursor
.data
->symlink
.name
,
1425 cursor
.leaf
->data_len
-
1426 HAMMER_SYMLINK_NAME_OFF
,
1430 hammer_done_cursor(&cursor
);
1431 hammer_done_transaction(&trans
);
1436 * hammer_vop_nremove { nch, dvp, cred }
1440 hammer_vop_nremove(struct vop_nremove_args
*ap
)
1442 struct hammer_transaction trans
;
1443 struct hammer_inode
*dip
;
1446 dip
= VTOI(ap
->a_dvp
);
1448 if (hammer_nohistory(dip
) == 0 &&
1449 (error
= hammer_checkspace(dip
->hmp
, HAMMER_CHKSPC_REMOVE
)) != 0) {
1453 hammer_start_transaction(&trans
, dip
->hmp
);
1454 error
= hammer_dounlink(&trans
, ap
->a_nch
, ap
->a_dvp
, ap
->a_cred
, 0);
1455 hammer_done_transaction(&trans
);
1461 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1465 hammer_vop_nrename(struct vop_nrename_args
*ap
)
1467 struct hammer_transaction trans
;
1468 struct namecache
*fncp
;
1469 struct namecache
*tncp
;
1470 struct hammer_inode
*fdip
;
1471 struct hammer_inode
*tdip
;
1472 struct hammer_inode
*ip
;
1473 struct hammer_cursor cursor
;
1477 fdip
= VTOI(ap
->a_fdvp
);
1478 tdip
= VTOI(ap
->a_tdvp
);
1479 fncp
= ap
->a_fnch
->ncp
;
1480 tncp
= ap
->a_tnch
->ncp
;
1481 ip
= VTOI(fncp
->nc_vp
);
1482 KKASSERT(ip
!= NULL
);
1484 if (fdip
->flags
& HAMMER_INODE_RO
)
1486 if (tdip
->flags
& HAMMER_INODE_RO
)
1488 if (ip
->flags
& HAMMER_INODE_RO
)
1490 if ((error
= hammer_checkspace(fdip
->hmp
, HAMMER_CHKSPC_CREATE
)) != 0)
1493 hammer_start_transaction(&trans
, fdip
->hmp
);
1496 * Remove tncp from the target directory and then link ip as
1497 * tncp. XXX pass trans to dounlink
1499 * Force the inode sync-time to match the transaction so it is
1500 * in-sync with the creation of the target directory entry.
1502 error
= hammer_dounlink(&trans
, ap
->a_tnch
, ap
->a_tdvp
, ap
->a_cred
, 0);
1503 if (error
== 0 || error
== ENOENT
) {
1504 error
= hammer_ip_add_directory(&trans
, tdip
,
1505 tncp
->nc_name
, tncp
->nc_nlen
,
1508 ip
->ino_data
.parent_obj_id
= tdip
->obj_id
;
1509 hammer_modify_inode(ip
, HAMMER_INODE_DDIRTY
);
1513 goto failed
; /* XXX */
1516 * Locate the record in the originating directory and remove it.
1518 * Calculate the namekey and setup the key range for the scan. This
1519 * works kinda like a chained hash table where the lower 32 bits
1520 * of the namekey synthesize the chain.
1522 * The key range is inclusive of both key_beg and key_end.
1524 namekey
= hammer_directory_namekey(fncp
->nc_name
, fncp
->nc_nlen
);
1526 hammer_init_cursor(&trans
, &cursor
, &fdip
->cache
[1], fdip
);
1527 cursor
.key_beg
.localization
= fdip
->obj_localization
+
1528 HAMMER_LOCALIZE_MISC
;
1529 cursor
.key_beg
.obj_id
= fdip
->obj_id
;
1530 cursor
.key_beg
.key
= namekey
;
1531 cursor
.key_beg
.create_tid
= 0;
1532 cursor
.key_beg
.delete_tid
= 0;
1533 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DIRENTRY
;
1534 cursor
.key_beg
.obj_type
= 0;
1536 cursor
.key_end
= cursor
.key_beg
;
1537 cursor
.key_end
.key
|= 0xFFFFFFFFULL
;
1538 cursor
.asof
= fdip
->obj_asof
;
1539 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
| HAMMER_CURSOR_ASOF
;
1542 * Scan all matching records (the chain), locate the one matching
1543 * the requested path component.
1545 * The hammer_ip_*() functions merge in-memory records with on-disk
1546 * records for the purposes of the search.
1548 error
= hammer_ip_first(&cursor
);
1549 while (error
== 0) {
1550 if (hammer_ip_resolve_data(&cursor
) != 0)
1552 nlen
= cursor
.leaf
->data_len
- HAMMER_ENTRY_NAME_OFF
;
1554 if (fncp
->nc_nlen
== nlen
&&
1555 bcmp(fncp
->nc_name
, cursor
.data
->entry
.name
, nlen
) == 0) {
1558 error
= hammer_ip_next(&cursor
);
1562 * If all is ok we have to get the inode so we can adjust nlinks.
1564 * WARNING: hammer_ip_del_directory() may have to terminate the
1565 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1569 error
= hammer_ip_del_directory(&trans
, &cursor
, fdip
, ip
);
1572 * XXX A deadlock here will break rename's atomicy for the purposes
1573 * of crash recovery.
1575 if (error
== EDEADLK
) {
1576 hammer_done_cursor(&cursor
);
1581 * Cleanup and tell the kernel that the rename succeeded.
1583 hammer_done_cursor(&cursor
);
1585 cache_rename(ap
->a_fnch
, ap
->a_tnch
);
1588 hammer_done_transaction(&trans
);
1593 * hammer_vop_nrmdir { nch, dvp, cred }
1597 hammer_vop_nrmdir(struct vop_nrmdir_args
*ap
)
1599 struct hammer_transaction trans
;
1600 struct hammer_inode
*dip
;
1603 dip
= VTOI(ap
->a_dvp
);
1605 if (hammer_nohistory(dip
) == 0 &&
1606 (error
= hammer_checkspace(dip
->hmp
, HAMMER_CHKSPC_REMOVE
)) != 0) {
1610 hammer_start_transaction(&trans
, dip
->hmp
);
1611 error
= hammer_dounlink(&trans
, ap
->a_nch
, ap
->a_dvp
, ap
->a_cred
, 0);
1612 hammer_done_transaction(&trans
);
1618 * hammer_vop_setattr { vp, vap, cred }
1622 hammer_vop_setattr(struct vop_setattr_args
*ap
)
1624 struct hammer_transaction trans
;
1626 struct hammer_inode
*ip
;
1631 int64_t aligned_size
;
1635 ip
= ap
->a_vp
->v_data
;
1638 if (ap
->a_vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
1640 if (ip
->flags
& HAMMER_INODE_RO
)
1642 if (hammer_nohistory(ip
) == 0 &&
1643 (error
= hammer_checkspace(ip
->hmp
, HAMMER_CHKSPC_REMOVE
)) != 0) {
1647 hammer_start_transaction(&trans
, ip
->hmp
);
1650 if (vap
->va_flags
!= VNOVAL
) {
1651 flags
= ip
->ino_data
.uflags
;
1652 error
= vop_helper_setattr_flags(&flags
, vap
->va_flags
,
1653 hammer_to_unix_xid(&ip
->ino_data
.uid
),
1656 if (ip
->ino_data
.uflags
!= flags
) {
1657 ip
->ino_data
.uflags
= flags
;
1658 modflags
|= HAMMER_INODE_DDIRTY
;
1660 if (ip
->ino_data
.uflags
& (IMMUTABLE
| APPEND
)) {
1667 if (ip
->ino_data
.uflags
& (IMMUTABLE
| APPEND
)) {
1671 if (vap
->va_uid
!= (uid_t
)VNOVAL
|| vap
->va_gid
!= (gid_t
)VNOVAL
) {
1672 mode_t cur_mode
= ip
->ino_data
.mode
;
1673 uid_t cur_uid
= hammer_to_unix_xid(&ip
->ino_data
.uid
);
1674 gid_t cur_gid
= hammer_to_unix_xid(&ip
->ino_data
.gid
);
1678 error
= vop_helper_chown(ap
->a_vp
, vap
->va_uid
, vap
->va_gid
,
1680 &cur_uid
, &cur_gid
, &cur_mode
);
1682 hammer_guid_to_uuid(&uuid_uid
, cur_uid
);
1683 hammer_guid_to_uuid(&uuid_gid
, cur_gid
);
1684 if (bcmp(&uuid_uid
, &ip
->ino_data
.uid
,
1685 sizeof(uuid_uid
)) ||
1686 bcmp(&uuid_gid
, &ip
->ino_data
.gid
,
1687 sizeof(uuid_gid
)) ||
1688 ip
->ino_data
.mode
!= cur_mode
1690 ip
->ino_data
.uid
= uuid_uid
;
1691 ip
->ino_data
.gid
= uuid_gid
;
1692 ip
->ino_data
.mode
= cur_mode
;
1694 modflags
|= HAMMER_INODE_DDIRTY
;
1697 while (vap
->va_size
!= VNOVAL
&& ip
->ino_data
.size
!= vap
->va_size
) {
1698 switch(ap
->a_vp
->v_type
) {
1700 if (vap
->va_size
== ip
->ino_data
.size
)
1703 * XXX break atomicy, we can deadlock the backend
1704 * if we do not release the lock. Probably not a
1707 blksize
= hammer_blocksize(vap
->va_size
);
1708 if (vap
->va_size
< ip
->ino_data
.size
) {
1709 vtruncbuf(ap
->a_vp
, vap
->va_size
, blksize
);
1712 vnode_pager_setsize(ap
->a_vp
, vap
->va_size
);
1715 ip
->ino_data
.size
= vap
->va_size
;
1716 modflags
|= HAMMER_INODE_DDIRTY
;
1719 * on-media truncation is cached in the inode until
1720 * the inode is synchronized.
1723 hammer_ip_frontend_trunc(ip
, vap
->va_size
);
1724 #ifdef DEBUG_TRUNCATE
1725 if (HammerTruncIp
== NULL
)
1728 if ((ip
->flags
& HAMMER_INODE_TRUNCATED
) == 0) {
1729 ip
->flags
|= HAMMER_INODE_TRUNCATED
;
1730 ip
->trunc_off
= vap
->va_size
;
1731 #ifdef DEBUG_TRUNCATE
1732 if (ip
== HammerTruncIp
)
1733 kprintf("truncate1 %016llx\n", ip
->trunc_off
);
1735 } else if (ip
->trunc_off
> vap
->va_size
) {
1736 ip
->trunc_off
= vap
->va_size
;
1737 #ifdef DEBUG_TRUNCATE
1738 if (ip
== HammerTruncIp
)
1739 kprintf("truncate2 %016llx\n", ip
->trunc_off
);
1742 #ifdef DEBUG_TRUNCATE
1743 if (ip
== HammerTruncIp
)
1744 kprintf("truncate3 %016llx (ignored)\n", vap
->va_size
);
1750 * If truncating we have to clean out a portion of
1751 * the last block on-disk. We do this in the
1752 * front-end buffer cache.
1754 aligned_size
= (vap
->va_size
+ (blksize
- 1)) &
1755 ~(int64_t)(blksize
- 1);
1756 if (truncating
&& vap
->va_size
< aligned_size
) {
1760 aligned_size
-= blksize
;
1762 offset
= (int)vap
->va_size
& (blksize
- 1);
1763 error
= bread(ap
->a_vp
, aligned_size
,
1765 hammer_ip_frontend_trunc(ip
, aligned_size
);
1767 bzero(bp
->b_data
+ offset
,
1771 kprintf("ERROR %d\n", error
);
1777 if ((ip
->flags
& HAMMER_INODE_TRUNCATED
) == 0) {
1778 ip
->flags
|= HAMMER_INODE_TRUNCATED
;
1779 ip
->trunc_off
= vap
->va_size
;
1780 } else if (ip
->trunc_off
> vap
->va_size
) {
1781 ip
->trunc_off
= vap
->va_size
;
1783 hammer_ip_frontend_trunc(ip
, vap
->va_size
);
1784 ip
->ino_data
.size
= vap
->va_size
;
1785 modflags
|= HAMMER_INODE_DDIRTY
;
1793 if (vap
->va_atime
.tv_sec
!= VNOVAL
) {
1794 ip
->ino_data
.atime
=
1795 hammer_timespec_to_time(&vap
->va_atime
);
1796 modflags
|= HAMMER_INODE_ATIME
;
1798 if (vap
->va_mtime
.tv_sec
!= VNOVAL
) {
1799 ip
->ino_data
.mtime
=
1800 hammer_timespec_to_time(&vap
->va_mtime
);
1801 modflags
|= HAMMER_INODE_MTIME
;
1803 if (vap
->va_mode
!= (mode_t
)VNOVAL
) {
1804 mode_t cur_mode
= ip
->ino_data
.mode
;
1805 uid_t cur_uid
= hammer_to_unix_xid(&ip
->ino_data
.uid
);
1806 gid_t cur_gid
= hammer_to_unix_xid(&ip
->ino_data
.gid
);
1808 error
= vop_helper_chmod(ap
->a_vp
, vap
->va_mode
, ap
->a_cred
,
1809 cur_uid
, cur_gid
, &cur_mode
);
1810 if (error
== 0 && ip
->ino_data
.mode
!= cur_mode
) {
1811 ip
->ino_data
.mode
= cur_mode
;
1812 modflags
|= HAMMER_INODE_DDIRTY
;
1817 hammer_modify_inode(ip
, modflags
);
1818 hammer_done_transaction(&trans
);
1823 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1827 hammer_vop_nsymlink(struct vop_nsymlink_args
*ap
)
1829 struct hammer_transaction trans
;
1830 struct hammer_inode
*dip
;
1831 struct hammer_inode
*nip
;
1832 struct nchandle
*nch
;
1833 hammer_record_t record
;
1837 ap
->a_vap
->va_type
= VLNK
;
1840 dip
= VTOI(ap
->a_dvp
);
1842 if (dip
->flags
& HAMMER_INODE_RO
)
1844 if ((error
= hammer_checkspace(dip
->hmp
, HAMMER_CHKSPC_CREATE
)) != 0)
1848 * Create a transaction to cover the operations we perform.
1850 hammer_start_transaction(&trans
, dip
->hmp
);
1853 * Create a new filesystem object of the requested type. The
1854 * returned inode will be referenced but not locked.
1857 error
= hammer_create_inode(&trans
, ap
->a_vap
, ap
->a_cred
,
1860 hammer_done_transaction(&trans
);
1866 * Add a record representing the symlink. symlink stores the link
1867 * as pure data, not a string, and is no \0 terminated.
1870 bytes
= strlen(ap
->a_target
);
1872 if (bytes
<= HAMMER_INODE_BASESYMLEN
) {
1873 bcopy(ap
->a_target
, nip
->ino_data
.ext
.symlink
, bytes
);
1875 record
= hammer_alloc_mem_record(nip
, bytes
);
1876 record
->type
= HAMMER_MEM_RECORD_GENERAL
;
1878 record
->leaf
.base
.localization
= nip
->obj_localization
+
1879 HAMMER_LOCALIZE_MISC
;
1880 record
->leaf
.base
.key
= HAMMER_FIXKEY_SYMLINK
;
1881 record
->leaf
.base
.rec_type
= HAMMER_RECTYPE_FIX
;
1882 record
->leaf
.data_len
= bytes
;
1883 KKASSERT(HAMMER_SYMLINK_NAME_OFF
== 0);
1884 bcopy(ap
->a_target
, record
->data
->symlink
.name
, bytes
);
1885 error
= hammer_ip_add_record(&trans
, record
);
1889 * Set the file size to the length of the link.
1892 nip
->ino_data
.size
= bytes
;
1893 hammer_modify_inode(nip
, HAMMER_INODE_DDIRTY
);
1897 error
= hammer_ip_add_directory(&trans
, dip
, nch
->ncp
->nc_name
,
1898 nch
->ncp
->nc_nlen
, nip
);
1904 hammer_rel_inode(nip
, 0);
1907 error
= hammer_get_vnode(nip
, ap
->a_vpp
);
1908 hammer_rel_inode(nip
, 0);
1910 cache_setunresolved(ap
->a_nch
);
1911 cache_setvp(ap
->a_nch
, *ap
->a_vpp
);
1914 hammer_done_transaction(&trans
);
1919 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1923 hammer_vop_nwhiteout(struct vop_nwhiteout_args
*ap
)
1925 struct hammer_transaction trans
;
1926 struct hammer_inode
*dip
;
1929 dip
= VTOI(ap
->a_dvp
);
1931 if (hammer_nohistory(dip
) == 0 &&
1932 (error
= hammer_checkspace(dip
->hmp
, HAMMER_CHKSPC_CREATE
)) != 0) {
1936 hammer_start_transaction(&trans
, dip
->hmp
);
1937 error
= hammer_dounlink(&trans
, ap
->a_nch
, ap
->a_dvp
,
1938 ap
->a_cred
, ap
->a_flags
);
1939 hammer_done_transaction(&trans
);
1945 * hammer_vop_ioctl { vp, command, data, fflag, cred }
1949 hammer_vop_ioctl(struct vop_ioctl_args
*ap
)
1951 struct hammer_inode
*ip
= ap
->a_vp
->v_data
;
1953 return(hammer_ioctl(ip
, ap
->a_command
, ap
->a_data
,
1954 ap
->a_fflag
, ap
->a_cred
));
1959 hammer_vop_mountctl(struct vop_mountctl_args
*ap
)
1964 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
1967 case MOUNTCTL_SET_EXPORT
:
1968 if (ap
->a_ctllen
!= sizeof(struct export_args
))
1970 error
= hammer_vfs_export(mp
, ap
->a_op
,
1971 (const struct export_args
*)ap
->a_ctl
);
1974 error
= journal_mountctl(ap
);
1981 * hammer_vop_strategy { vp, bio }
1983 * Strategy call, used for regular file read & write only. Note that the
1984 * bp may represent a cluster.
1986 * To simplify operation and allow better optimizations in the future,
1987 * this code does not make any assumptions with regards to buffer alignment
1992 hammer_vop_strategy(struct vop_strategy_args
*ap
)
1997 bp
= ap
->a_bio
->bio_buf
;
2001 error
= hammer_vop_strategy_read(ap
);
2004 error
= hammer_vop_strategy_write(ap
);
2007 bp
->b_error
= error
= EINVAL
;
2008 bp
->b_flags
|= B_ERROR
;
2016 * Read from a regular file. Iterate the related records and fill in the
2017 * BIO/BUF. Gaps are zero-filled.
2019 * The support code in hammer_object.c should be used to deal with mixed
2020 * in-memory and on-disk records.
2022 * NOTE: Can be called from the cluster code with an oversized buf.
2028 hammer_vop_strategy_read(struct vop_strategy_args
*ap
)
2030 struct hammer_transaction trans
;
2031 struct hammer_inode
*ip
;
2032 struct hammer_cursor cursor
;
2033 hammer_base_elm_t base
;
2034 hammer_off_t disk_offset
;
2048 ip
= ap
->a_vp
->v_data
;
2051 * The zone-2 disk offset may have been set by the cluster code via
2052 * a BMAP operation, or else should be NOOFFSET.
2054 * Checking the high bits for a match against zone-2 should suffice.
2056 nbio
= push_bio(bio
);
2057 if ((nbio
->bio_offset
& HAMMER_OFF_ZONE_MASK
) ==
2058 HAMMER_ZONE_RAW_BUFFER
) {
2059 error
= hammer_io_direct_read(ip
->hmp
, nbio
);
2064 * Well, that sucked. Do it the hard way. If all the stars are
2065 * aligned we may still be able to issue a direct-read.
2067 hammer_simple_transaction(&trans
, ip
->hmp
);
2068 hammer_init_cursor(&trans
, &cursor
, &ip
->cache
[1], ip
);
2071 * Key range (begin and end inclusive) to scan. Note that the key's
2072 * stored in the actual records represent BASE+LEN, not BASE. The
2073 * first record containing bio_offset will have a key > bio_offset.
2075 cursor
.key_beg
.localization
= ip
->obj_localization
+
2076 HAMMER_LOCALIZE_MISC
;
2077 cursor
.key_beg
.obj_id
= ip
->obj_id
;
2078 cursor
.key_beg
.create_tid
= 0;
2079 cursor
.key_beg
.delete_tid
= 0;
2080 cursor
.key_beg
.obj_type
= 0;
2081 cursor
.key_beg
.key
= bio
->bio_offset
+ 1;
2082 cursor
.asof
= ip
->obj_asof
;
2083 cursor
.flags
|= HAMMER_CURSOR_ASOF
;
2085 cursor
.key_end
= cursor
.key_beg
;
2086 KKASSERT(ip
->ino_data
.obj_type
== HAMMER_OBJTYPE_REGFILE
);
2088 if (ip
->ino_data
.obj_type
== HAMMER_OBJTYPE_DBFILE
) {
2089 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DB
;
2090 cursor
.key_end
.rec_type
= HAMMER_RECTYPE_DB
;
2091 cursor
.key_end
.key
= 0x7FFFFFFFFFFFFFFFLL
;
2095 ran_end
= bio
->bio_offset
+ bp
->b_bufsize
;
2096 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DATA
;
2097 cursor
.key_end
.rec_type
= HAMMER_RECTYPE_DATA
;
2098 tmp64
= ran_end
+ MAXPHYS
+ 1; /* work-around GCC-4 bug */
2099 if (tmp64
< ran_end
)
2100 cursor
.key_end
.key
= 0x7FFFFFFFFFFFFFFFLL
;
2102 cursor
.key_end
.key
= ran_end
+ MAXPHYS
+ 1;
2104 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
;
2106 error
= hammer_ip_first(&cursor
);
2109 while (error
== 0) {
2111 * Get the base file offset of the record. The key for
2112 * data records is (base + bytes) rather then (base).
2114 base
= &cursor
.leaf
->base
;
2115 rec_offset
= base
->key
- cursor
.leaf
->data_len
;
2118 * Calculate the gap, if any, and zero-fill it.
2120 * n is the offset of the start of the record verses our
2121 * current seek offset in the bio.
2123 n
= (int)(rec_offset
- (bio
->bio_offset
+ boff
));
2125 if (n
> bp
->b_bufsize
- boff
)
2126 n
= bp
->b_bufsize
- boff
;
2127 bzero((char *)bp
->b_data
+ boff
, n
);
2133 * Calculate the data offset in the record and the number
2134 * of bytes we can copy.
2136 * There are two degenerate cases. First, boff may already
2137 * be at bp->b_bufsize. Secondly, the data offset within
2138 * the record may exceed the record's size.
2142 n
= cursor
.leaf
->data_len
- roff
;
2144 kprintf("strategy_read: bad n=%d roff=%d\n", n
, roff
);
2146 } else if (n
> bp
->b_bufsize
- boff
) {
2147 n
= bp
->b_bufsize
- boff
;
2151 * Deal with cached truncations. This cool bit of code
2152 * allows truncate()/ftruncate() to avoid having to sync
2155 * If the frontend is truncated then all backend records are
2156 * subject to the frontend's truncation.
2158 * If the backend is truncated then backend records on-disk
2159 * (but not in-memory) are subject to the backend's
2160 * truncation. In-memory records owned by the backend
2161 * represent data written after the truncation point on the
2162 * backend and must not be truncated.
2164 * Truncate operations deal with frontend buffer cache
2165 * buffers and frontend-owned in-memory records synchronously.
2167 if (ip
->flags
& HAMMER_INODE_TRUNCATED
) {
2168 if (hammer_cursor_ondisk(&cursor
) ||
2169 cursor
.iprec
->flush_state
== HAMMER_FST_FLUSH
) {
2170 if (ip
->trunc_off
<= rec_offset
)
2172 else if (ip
->trunc_off
< rec_offset
+ n
)
2173 n
= (int)(ip
->trunc_off
- rec_offset
);
2176 if (ip
->sync_flags
& HAMMER_INODE_TRUNCATED
) {
2177 if (hammer_cursor_ondisk(&cursor
)) {
2178 if (ip
->sync_trunc_off
<= rec_offset
)
2180 else if (ip
->sync_trunc_off
< rec_offset
+ n
)
2181 n
= (int)(ip
->sync_trunc_off
- rec_offset
);
2186 * Try to issue a direct read into our bio if possible,
2187 * otherwise resolve the element data into a hammer_buffer
2190 * The buffer on-disk should be zerod past any real
2191 * truncation point, but may not be for any synthesized
2192 * truncation point from above.
2194 if (boff
== 0 && n
== bp
->b_bufsize
&&
2195 ((cursor
.leaf
->data_offset
+ roff
) & HAMMER_BUFMASK
) == 0) {
2196 disk_offset
= hammer_blockmap_lookup(
2198 cursor
.leaf
->data_offset
+ roff
,
2202 nbio
->bio_offset
= disk_offset
;
2203 error
= hammer_io_direct_read(trans
.hmp
, nbio
);
2206 error
= hammer_ip_resolve_data(&cursor
);
2208 bcopy((char *)cursor
.data
+ roff
,
2209 (char *)bp
->b_data
+ boff
, n
);
2216 * Iterate until we have filled the request.
2219 if (boff
== bp
->b_bufsize
)
2221 error
= hammer_ip_next(&cursor
);
2225 * There may have been a gap after the last record
2227 if (error
== ENOENT
)
2229 if (error
== 0 && boff
!= bp
->b_bufsize
) {
2230 KKASSERT(boff
< bp
->b_bufsize
);
2231 bzero((char *)bp
->b_data
+ boff
, bp
->b_bufsize
- boff
);
2232 /* boff = bp->b_bufsize; */
2235 bp
->b_error
= error
;
2237 bp
->b_flags
|= B_ERROR
;
2242 hammer_cache_node(&ip
->cache
[1], cursor
.node
);
2243 hammer_done_cursor(&cursor
);
2244 hammer_done_transaction(&trans
);
2249 * BMAP operation - used to support cluster_read() only.
2251 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2253 * This routine may return EOPNOTSUPP if the opration is not supported for
2254 * the specified offset. The contents of the pointer arguments do not
2255 * need to be initialized in that case.
2257 * If a disk address is available and properly aligned return 0 with
2258 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2259 * to the run-length relative to that offset. Callers may assume that
2260 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2261 * large, so return EOPNOTSUPP if it is not sufficiently large.
2265 hammer_vop_bmap(struct vop_bmap_args
*ap
)
2267 struct hammer_transaction trans
;
2268 struct hammer_inode
*ip
;
2269 struct hammer_cursor cursor
;
2270 hammer_base_elm_t base
;
2274 int64_t base_offset
;
2275 int64_t base_disk_offset
;
2276 int64_t last_offset
;
2277 hammer_off_t last_disk_offset
;
2278 hammer_off_t disk_offset
;
2283 ip
= ap
->a_vp
->v_data
;
2286 * We can only BMAP regular files. We can't BMAP database files,
2289 if (ip
->ino_data
.obj_type
!= HAMMER_OBJTYPE_REGFILE
)
2293 * bmap is typically called with runp/runb both NULL when used
2294 * for writing. We do not support BMAP for writing atm.
2296 if (ap
->a_cmd
!= BUF_CMD_READ
)
2300 * Scan the B-Tree to acquire blockmap addresses, then translate
2303 hammer_simple_transaction(&trans
, ip
->hmp
);
2305 kprintf("bmap_beg %016llx ip->cache %p\n", ap
->a_loffset
, ip
->cache
[1]);
2307 hammer_init_cursor(&trans
, &cursor
, &ip
->cache
[1], ip
);
2310 * Key range (begin and end inclusive) to scan. Note that the key's
2311 * stored in the actual records represent BASE+LEN, not BASE. The
2312 * first record containing bio_offset will have a key > bio_offset.
2314 cursor
.key_beg
.localization
= ip
->obj_localization
+
2315 HAMMER_LOCALIZE_MISC
;
2316 cursor
.key_beg
.obj_id
= ip
->obj_id
;
2317 cursor
.key_beg
.create_tid
= 0;
2318 cursor
.key_beg
.delete_tid
= 0;
2319 cursor
.key_beg
.obj_type
= 0;
2321 cursor
.key_beg
.key
= ap
->a_loffset
- MAXPHYS
+ 1;
2323 cursor
.key_beg
.key
= ap
->a_loffset
+ 1;
2324 if (cursor
.key_beg
.key
< 0)
2325 cursor
.key_beg
.key
= 0;
2326 cursor
.asof
= ip
->obj_asof
;
2327 cursor
.flags
|= HAMMER_CURSOR_ASOF
;
2329 cursor
.key_end
= cursor
.key_beg
;
2330 KKASSERT(ip
->ino_data
.obj_type
== HAMMER_OBJTYPE_REGFILE
);
2332 ran_end
= ap
->a_loffset
+ MAXPHYS
;
2333 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DATA
;
2334 cursor
.key_end
.rec_type
= HAMMER_RECTYPE_DATA
;
2335 tmp64
= ran_end
+ MAXPHYS
+ 1; /* work-around GCC-4 bug */
2336 if (tmp64
< ran_end
)
2337 cursor
.key_end
.key
= 0x7FFFFFFFFFFFFFFFLL
;
2339 cursor
.key_end
.key
= ran_end
+ MAXPHYS
+ 1;
2341 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
;
2343 error
= hammer_ip_first(&cursor
);
2344 base_offset
= last_offset
= 0;
2345 base_disk_offset
= last_disk_offset
= 0;
2347 while (error
== 0) {
2349 * Get the base file offset of the record. The key for
2350 * data records is (base + bytes) rather then (base).
2352 * NOTE: rec_offset + rec_len may exceed the end-of-file.
2353 * The extra bytes should be zero on-disk and the BMAP op
2354 * should still be ok.
2356 base
= &cursor
.leaf
->base
;
2357 rec_offset
= base
->key
- cursor
.leaf
->data_len
;
2358 rec_len
= cursor
.leaf
->data_len
;
2361 * Incorporate any cached truncation.
2363 * NOTE: Modifications to rec_len based on synthesized
2364 * truncation points remove the guarantee that any extended
2365 * data on disk is zero (since the truncations may not have
2366 * taken place on-media yet).
2368 if (ip
->flags
& HAMMER_INODE_TRUNCATED
) {
2369 if (hammer_cursor_ondisk(&cursor
) ||
2370 cursor
.iprec
->flush_state
== HAMMER_FST_FLUSH
) {
2371 if (ip
->trunc_off
<= rec_offset
)
2373 else if (ip
->trunc_off
< rec_offset
+ rec_len
)
2374 rec_len
= (int)(ip
->trunc_off
- rec_offset
);
2377 if (ip
->sync_flags
& HAMMER_INODE_TRUNCATED
) {
2378 if (hammer_cursor_ondisk(&cursor
)) {
2379 if (ip
->sync_trunc_off
<= rec_offset
)
2381 else if (ip
->sync_trunc_off
< rec_offset
+ rec_len
)
2382 rec_len
= (int)(ip
->sync_trunc_off
- rec_offset
);
2387 * Accumulate information. If we have hit a discontiguous
2388 * block reset base_offset unless we are already beyond the
2389 * requested offset. If we are, that's it, we stop.
2391 disk_offset
= hammer_blockmap_lookup(trans
.hmp
,
2392 cursor
.leaf
->data_offset
,
2396 if (rec_offset
!= last_offset
||
2397 disk_offset
!= last_disk_offset
) {
2398 if (rec_offset
> ap
->a_loffset
)
2400 base_offset
= rec_offset
;
2401 base_disk_offset
= disk_offset
;
2403 last_offset
= rec_offset
+ rec_len
;
2404 last_disk_offset
= disk_offset
+ rec_len
;
2406 error
= hammer_ip_next(&cursor
);
2410 kprintf("BMAP %016llx: %016llx - %016llx\n",
2411 ap
->a_loffset
, base_offset
, last_offset
);
2412 kprintf("BMAP %16s: %016llx - %016llx\n",
2413 "", base_disk_offset
, last_disk_offset
);
2417 hammer_cache_node(&ip
->cache
[1], cursor
.node
);
2419 kprintf("bmap_end2 %016llx ip->cache %p\n", ap
->a_loffset
, ip
->cache
[1]);
2422 hammer_done_cursor(&cursor
);
2423 hammer_done_transaction(&trans
);
2426 * If we couldn't find any records or the records we did find were
2427 * all behind the requested offset, return failure. A forward
2428 * truncation can leave a hole w/ no on-disk records.
2430 if (last_offset
== 0 || last_offset
< ap
->a_loffset
)
2431 return (EOPNOTSUPP
);
2434 * Figure out the block size at the requested offset and adjust
2435 * our limits so the cluster_read() does not create inappropriately
2436 * sized buffer cache buffers.
2438 blksize
= hammer_blocksize(ap
->a_loffset
);
2439 if (hammer_blocksize(base_offset
) != blksize
) {
2440 base_offset
= hammer_blockdemarc(base_offset
, ap
->a_loffset
);
2442 if (last_offset
!= ap
->a_loffset
&&
2443 hammer_blocksize(last_offset
- 1) != blksize
) {
2444 last_offset
= hammer_blockdemarc(ap
->a_loffset
,
2449 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
2452 disk_offset
= base_disk_offset
+ (ap
->a_loffset
- base_offset
);
2455 * If doffsetp is not aligned or the forward run size does
2456 * not cover a whole buffer, disallow the direct I/O.
2458 if ((disk_offset
& HAMMER_BUFMASK
) ||
2459 (last_offset
- ap
->a_loffset
) < blksize
) {
2462 *ap
->a_doffsetp
= disk_offset
;
2464 *ap
->a_runb
= ap
->a_loffset
- base_offset
;
2465 KKASSERT(*ap
->a_runb
>= 0);
2468 *ap
->a_runp
= last_offset
- ap
->a_loffset
;
2469 KKASSERT(*ap
->a_runp
>= 0);
2477 * Write to a regular file. Because this is a strategy call the OS is
2478 * trying to actually get data onto the media.
2482 hammer_vop_strategy_write(struct vop_strategy_args
*ap
)
2484 hammer_record_t record
;
2495 ip
= ap
->a_vp
->v_data
;
2498 blksize
= hammer_blocksize(bio
->bio_offset
);
2499 KKASSERT(bp
->b_bufsize
== blksize
);
2501 if (ip
->flags
& HAMMER_INODE_RO
) {
2502 bp
->b_error
= EROFS
;
2503 bp
->b_flags
|= B_ERROR
;
2509 * Interlock with inode destruction (no in-kernel or directory
2510 * topology visibility). If we queue new IO while trying to
2511 * destroy the inode we can deadlock the vtrunc call in
2512 * hammer_inode_unloadable_check().
2514 if (ip
->flags
& (HAMMER_INODE_DELETING
|HAMMER_INODE_DELETED
)) {
2521 * Reserve space and issue a direct-write from the front-end.
2522 * NOTE: The direct_io code will hammer_bread/bcopy smaller
2525 * An in-memory record will be installed to reference the storage
2526 * until the flusher can get to it.
2528 * Since we own the high level bio the front-end will not try to
2529 * do a direct-read until the write completes.
2531 * NOTE: The only time we do not reserve a full-sized buffers
2532 * worth of data is if the file is small. We do not try to
2533 * allocate a fragment (from the small-data zone) at the end of
2534 * an otherwise large file as this can lead to wildly separated
2537 KKASSERT((bio
->bio_offset
& HAMMER_BUFMASK
) == 0);
2538 KKASSERT(bio
->bio_offset
< ip
->ino_data
.size
);
2539 if (bio
->bio_offset
|| ip
->ino_data
.size
> HAMMER_BUFSIZE
/ 2)
2540 bytes
= bp
->b_bufsize
;
2542 bytes
= ((int)ip
->ino_data
.size
+ 15) & ~15;
2544 record
= hammer_ip_add_bulk(ip
, bio
->bio_offset
, bp
->b_data
,
2547 hammer_io_direct_write(hmp
, &record
->leaf
, bio
);
2548 hammer_rel_mem_record(record
);
2549 if (ip
->rsv_recs
> 1 && hmp
->rsv_recs
> hammer_limit_recs
)
2550 hammer_flush_inode(ip
, 0);
2552 bp
->b_bio2
.bio_offset
= NOOFFSET
;
2553 bp
->b_error
= error
;
2554 bp
->b_flags
|= B_ERROR
;
2561 * dounlink - disconnect a directory entry
2563 * XXX whiteout support not really in yet
2566 hammer_dounlink(hammer_transaction_t trans
, struct nchandle
*nch
,
2567 struct vnode
*dvp
, struct ucred
*cred
, int flags
)
2569 struct namecache
*ncp
;
2572 struct hammer_cursor cursor
;
2577 * Calculate the namekey and setup the key range for the scan. This
2578 * works kinda like a chained hash table where the lower 32 bits
2579 * of the namekey synthesize the chain.
2581 * The key range is inclusive of both key_beg and key_end.
2586 if (dip
->flags
& HAMMER_INODE_RO
)
2589 namekey
= hammer_directory_namekey(ncp
->nc_name
, ncp
->nc_nlen
);
2591 hammer_init_cursor(trans
, &cursor
, &dip
->cache
[1], dip
);
2592 cursor
.key_beg
.localization
= dip
->obj_localization
+
2593 HAMMER_LOCALIZE_MISC
;
2594 cursor
.key_beg
.obj_id
= dip
->obj_id
;
2595 cursor
.key_beg
.key
= namekey
;
2596 cursor
.key_beg
.create_tid
= 0;
2597 cursor
.key_beg
.delete_tid
= 0;
2598 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DIRENTRY
;
2599 cursor
.key_beg
.obj_type
= 0;
2601 cursor
.key_end
= cursor
.key_beg
;
2602 cursor
.key_end
.key
|= 0xFFFFFFFFULL
;
2603 cursor
.asof
= dip
->obj_asof
;
2604 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
| HAMMER_CURSOR_ASOF
;
2607 * Scan all matching records (the chain), locate the one matching
2608 * the requested path component. info->last_error contains the
2609 * error code on search termination and could be 0, ENOENT, or
2612 * The hammer_ip_*() functions merge in-memory records with on-disk
2613 * records for the purposes of the search.
2615 error
= hammer_ip_first(&cursor
);
2617 while (error
== 0) {
2618 error
= hammer_ip_resolve_data(&cursor
);
2621 nlen
= cursor
.leaf
->data_len
- HAMMER_ENTRY_NAME_OFF
;
2623 if (ncp
->nc_nlen
== nlen
&&
2624 bcmp(ncp
->nc_name
, cursor
.data
->entry
.name
, nlen
) == 0) {
2627 error
= hammer_ip_next(&cursor
);
2631 * If all is ok we have to get the inode so we can adjust nlinks.
2632 * To avoid a deadlock with the flusher we must release the inode
2633 * lock on the directory when acquiring the inode for the entry.
2635 * If the target is a directory, it must be empty.
2638 hammer_unlock(&cursor
.ip
->lock
);
2639 ip
= hammer_get_inode(trans
, dip
, cursor
.data
->entry
.obj_id
,
2641 cursor
.data
->entry
.localization
,
2643 hammer_lock_sh(&cursor
.ip
->lock
);
2644 if (error
== ENOENT
) {
2645 kprintf("obj_id %016llx\n", cursor
.data
->entry
.obj_id
);
2646 Debugger("ENOENT unlinking object that should exist");
2650 * If we are trying to remove a directory the directory must
2653 * WARNING: hammer_ip_check_directory_empty() may have to
2654 * terminate the cursor to avoid a deadlock. It is ok to
2655 * call hammer_done_cursor() twice.
2657 if (error
== 0 && ip
->ino_data
.obj_type
==
2658 HAMMER_OBJTYPE_DIRECTORY
) {
2659 error
= hammer_ip_check_directory_empty(trans
, ip
);
2663 * Delete the directory entry.
2665 * WARNING: hammer_ip_del_directory() may have to terminate
2666 * the cursor to avoid a deadlock. It is ok to call
2667 * hammer_done_cursor() twice.
2670 error
= hammer_ip_del_directory(trans
, &cursor
,
2673 hammer_done_cursor(&cursor
);
2675 cache_setunresolved(nch
);
2676 cache_setvp(nch
, NULL
);
2679 cache_inval_vp(ip
->vp
, CINV_DESTROY
);
2682 hammer_rel_inode(ip
, 0);
2684 hammer_done_cursor(&cursor
);
2686 hammer_inode_waitreclaims(dip
->hmp
);
2687 if (error
== EDEADLK
)
2693 /************************************************************************
2694 * FIFO AND SPECFS OPS *
2695 ************************************************************************
2700 hammer_vop_fifoclose (struct vop_close_args
*ap
)
2702 /* XXX update itimes */
2703 return (VOCALL(&fifo_vnode_vops
, &ap
->a_head
));
2707 hammer_vop_fiforead (struct vop_read_args
*ap
)
2711 error
= VOCALL(&fifo_vnode_vops
, &ap
->a_head
);
2712 /* XXX update access time */
2717 hammer_vop_fifowrite (struct vop_write_args
*ap
)
2721 error
= VOCALL(&fifo_vnode_vops
, &ap
->a_head
);
2722 /* XXX update access time */
2727 hammer_vop_specclose (struct vop_close_args
*ap
)
2729 /* XXX update itimes */
2730 return (VOCALL(&spec_vnode_vops
, &ap
->a_head
));
2734 hammer_vop_specread (struct vop_read_args
*ap
)
2736 /* XXX update access time */
2737 return (VOCALL(&spec_vnode_vops
, &ap
->a_head
));
2741 hammer_vop_specwrite (struct vop_write_args
*ap
)
2743 /* XXX update last change time */
2744 return (VOCALL(&spec_vnode_vops
, &ap
->a_head
));