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.77 2008/06/26 04:06:23 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
);
162 static void hammer_cleanup_write_io(hammer_inode_t ip
);
163 static void hammer_update_rsv_databufs(hammer_inode_t ip
);
168 hammer_vop_vnoperate(struct vop_generic_args
*)
170 return (VOCALL(&hammer_vnode_vops
, ap
));
175 * hammer_vop_fsync { vp, waitfor }
177 * fsync() an inode to disk and wait for it to be completely committed
178 * such that the information would not be undone if a crash occured after
183 hammer_vop_fsync(struct vop_fsync_args
*ap
)
185 hammer_inode_t ip
= VTOI(ap
->a_vp
);
187 vfsync(ap
->a_vp
, ap
->a_waitfor
, 1, NULL
, NULL
);
188 hammer_flush_inode(ip
, HAMMER_FLUSH_SIGNAL
);
189 if (ap
->a_waitfor
== MNT_WAIT
)
190 hammer_wait_inode(ip
);
195 * hammer_vop_read { vp, uio, ioflag, cred }
199 hammer_vop_read(struct vop_read_args
*ap
)
201 struct hammer_transaction trans
;
212 if (ap
->a_vp
->v_type
!= VREG
)
219 * Allow the UIO's size to override the sequential heuristic.
221 blksize
= hammer_blocksize(uio
->uio_offset
);
222 seqcount
= (uio
->uio_resid
+ (blksize
- 1)) / blksize
;
223 ioseqcount
= ap
->a_ioflag
>> 16;
224 if (seqcount
< ioseqcount
)
225 seqcount
= ioseqcount
;
227 hammer_start_transaction(&trans
, ip
->hmp
);
230 * Access the data typically in HAMMER_BUFSIZE blocks via the
231 * buffer cache, but HAMMER may use a variable block size based
234 while (uio
->uio_resid
> 0 && uio
->uio_offset
< ip
->ino_data
.size
) {
238 blksize
= hammer_blocksize(uio
->uio_offset
);
239 offset
= (int)uio
->uio_offset
& (blksize
- 1);
240 base_offset
= uio
->uio_offset
- offset
;
242 if (hammer_debug_cluster_enable
) {
244 * Use file_limit to prevent cluster_read() from
245 * creating buffers of the wrong block size past
248 file_limit
= ip
->ino_data
.size
;
249 if (base_offset
< HAMMER_XDEMARC
&&
250 file_limit
> HAMMER_XDEMARC
) {
251 file_limit
= HAMMER_XDEMARC
;
253 error
= cluster_read(ap
->a_vp
,
254 file_limit
, base_offset
,
258 error
= bread(ap
->a_vp
, base_offset
, blksize
, &bp
);
261 kprintf("error %d\n", error
);
266 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
267 n
= blksize
- offset
;
268 if (n
> uio
->uio_resid
)
270 if (n
> ip
->ino_data
.size
- uio
->uio_offset
)
271 n
= (int)(ip
->ino_data
.size
- uio
->uio_offset
);
272 error
= uiomove((char *)bp
->b_data
+ offset
, n
, uio
);
274 /* data has a lower priority then meta-data */
275 bp
->b_flags
|= B_AGE
;
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
;
310 if (ap
->a_vp
->v_type
!= VREG
)
315 seqcount
= ap
->a_ioflag
>> 16;
317 if (ip
->flags
& HAMMER_INODE_RO
)
321 * Create a transaction to cover the operations we perform.
323 hammer_start_transaction(&trans
, hmp
);
329 if (ap
->a_ioflag
& IO_APPEND
)
330 uio
->uio_offset
= ip
->ino_data
.size
;
333 * Check for illegal write offsets. Valid range is 0...2^63-1.
335 * NOTE: the base_off assignment is required to work around what
336 * I consider to be a GCC-4 optimization bug.
338 if (uio
->uio_offset
< 0) {
339 hammer_done_transaction(&trans
);
342 base_offset
= uio
->uio_offset
+ uio
->uio_resid
; /* work around gcc-4 */
343 if (uio
->uio_resid
> 0 && base_offset
<= 0) {
344 hammer_done_transaction(&trans
);
349 * Access the data typically in HAMMER_BUFSIZE blocks via the
350 * buffer cache, but HAMMER may use a variable block size based
354 while (uio
->uio_resid
> 0) {
359 if ((error
= hammer_checkspace(hmp
)) != 0)
363 * Do not allow HAMMER to blow out the buffer cache. Very
364 * large UIOs can lockout other processes due to bwillwrite()
367 * Do not allow HAMMER to blow out system memory by
368 * accumulating too many records. Records are so well
369 * decoupled from the buffer cache that it is possible
370 * for userland to push data out to the media via
371 * direct-write, but build up the records queued to the
372 * backend faster then the backend can flush them out.
373 * HAMMER has hit its write limit but the frontend has
374 * no pushback to slow it down.
376 * Always check at the beginning so separate writes are
377 * not able to bypass this code (count++).
379 * WARNING: Cannot unlock vp when doing a NOCOPY write as
380 * part of a putpages operation. Doing so could cause us
381 * to deadlock against the VM system when we try to re-lock.
383 if ((count
++ & 15) == 0 || count
> 64) {
387 if (uio
->uio_segflg
!= UIO_NOCOPY
) {
389 if ((ap
->a_ioflag
& IO_NOBWILL
) == 0)
394 * Pending record flush check.
396 if (hmp
->rsv_recs
> hammer_limit_recs
/ 2) {
398 * Get the inode on the flush list
400 if (ip
->rsv_recs
>= 64)
401 hammer_flush_inode(ip
, HAMMER_FLUSH_SIGNAL
);
402 else if (ip
->rsv_recs
>= 16)
403 hammer_flush_inode(ip
, 0);
406 * Keep the flusher going if the system keeps
409 delta
= hmp
->count_newrecords
-
410 hmp
->last_newrecords
;
411 if (delta
< 0 || delta
> hammer_limit_recs
/ 2) {
412 hmp
->last_newrecords
= hmp
->count_newrecords
;
413 hammer_sync_hmp(hmp
, MNT_NOWAIT
);
417 * If we have gotten behind start slowing
420 delta
= (hmp
->rsv_recs
- hammer_limit_recs
) *
421 hz
/ hammer_limit_recs
;
423 tsleep(&trans
, 0, "hmrslo", delta
);
426 if (uio
->uio_segflg
!= UIO_NOCOPY
)
427 vn_lock(ap
->a_vp
, LK_EXCLUSIVE
|LK_RETRY
);
431 * Calculate the blocksize at the current offset and figure
432 * out how much we can actually write.
434 blksize
= hammer_blocksize(uio
->uio_offset
);
435 blkmask
= blksize
- 1;
436 offset
= (int)uio
->uio_offset
& blkmask
;
437 base_offset
= uio
->uio_offset
& ~(int64_t)blkmask
;
438 n
= blksize
- offset
;
439 if (n
> uio
->uio_resid
)
441 if (uio
->uio_offset
+ n
> ip
->ino_data
.size
) {
442 vnode_pager_setsize(ap
->a_vp
, uio
->uio_offset
+ n
);
446 if (uio
->uio_segflg
== UIO_NOCOPY
) {
448 * Issuing a write with the same data backing the
449 * buffer. Instantiate the buffer to collect the
450 * backing vm pages, then read-in any missing bits.
452 * This case is used by vop_stdputpages().
454 bp
= getblk(ap
->a_vp
, base_offset
,
455 blksize
, GETBLK_BHEAVY
, 0);
456 if ((bp
->b_flags
& B_CACHE
) == 0) {
458 error
= bread(ap
->a_vp
, base_offset
,
461 } else if (offset
== 0 && uio
->uio_resid
>= blksize
) {
463 * Even though we are entirely overwriting the buffer
464 * we may still have to zero it out to avoid a
465 * mmap/write visibility issue.
467 bp
= getblk(ap
->a_vp
, base_offset
, blksize
, GETBLK_BHEAVY
, 0);
468 if ((bp
->b_flags
& B_CACHE
) == 0)
470 } else if (base_offset
>= ip
->ino_data
.size
) {
472 * If the base offset of the buffer is beyond the
473 * file EOF, we don't have to issue a read.
475 bp
= getblk(ap
->a_vp
, base_offset
,
476 blksize
, GETBLK_BHEAVY
, 0);
480 * Partial overwrite, read in any missing bits then
481 * replace the portion being written.
483 error
= bread(ap
->a_vp
, base_offset
, blksize
, &bp
);
488 error
= uiomove((char *)bp
->b_data
+ offset
,
493 * If we screwed up we have to undo any VM size changes we
499 vtruncbuf(ap
->a_vp
, ip
->ino_data
.size
,
500 hammer_blocksize(ip
->ino_data
.size
));
504 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
505 if (ip
->ino_data
.size
< uio
->uio_offset
) {
506 ip
->ino_data
.size
= uio
->uio_offset
;
507 flags
= HAMMER_INODE_DDIRTY
;
508 vnode_pager_setsize(ap
->a_vp
, ip
->ino_data
.size
);
512 ip
->ino_data
.mtime
= trans
.time
;
513 flags
|= HAMMER_INODE_MTIME
| HAMMER_INODE_BUFS
;
514 hammer_modify_inode(ip
, flags
);
517 * Try to keep track of cached dirty data.
519 if ((bp
->b_flags
& B_DIRTY
) == 0) {
525 * Final buffer disposition.
527 * If write_mode is non-zero we call bawrite()
528 * unconditionally. Otherwise we only use bawrite()
529 * if the writes are clearly sequential.
531 bp
->b_flags
|= B_AGE
;
532 if (ap
->a_ioflag
& IO_SYNC
) {
534 } else if (ap
->a_ioflag
& IO_DIRECT
) {
540 else if (hammer_write_mode
&&
541 ((int)uio
->uio_offset
& blkmask
) == 0) {
543 bp
->b_flags
|= B_CLUSTEROK
;
544 cluster_write(bp
, ip
->ino_data
.size
, XXX seqcount
);
548 } else if ((ap
->a_ioflag
>> 16) == IO_SEQMAX
&&
549 ((int)uio
->uio_offset
& blkmask
) == 0) {
551 * If seqcount indicates sequential operation and
552 * we just finished filling a buffer, push it out
553 * now to prevent the buffer cache from becoming
554 * too full, which would trigger non-optimal
563 hammer_done_transaction(&trans
);
568 * hammer_vop_access { vp, mode, cred }
572 hammer_vop_access(struct vop_access_args
*ap
)
574 struct hammer_inode
*ip
= VTOI(ap
->a_vp
);
579 uid
= hammer_to_unix_xid(&ip
->ino_data
.uid
);
580 gid
= hammer_to_unix_xid(&ip
->ino_data
.gid
);
582 error
= vop_helper_access(ap
, uid
, gid
, ip
->ino_data
.mode
,
583 ip
->ino_data
.uflags
);
588 * hammer_vop_advlock { vp, id, op, fl, flags }
592 hammer_vop_advlock(struct vop_advlock_args
*ap
)
594 hammer_inode_t ip
= VTOI(ap
->a_vp
);
596 return (lf_advlock(ap
, &ip
->advlock
, ip
->ino_data
.size
));
600 * hammer_vop_close { vp, fflag }
604 hammer_vop_close(struct vop_close_args
*ap
)
606 hammer_inode_t ip
= VTOI(ap
->a_vp
);
608 if ((ip
->flags
| ip
->sync_flags
) & HAMMER_INODE_MODMASK
)
609 hammer_inode_waitreclaims(ip
->hmp
);
610 return (vop_stdclose(ap
));
614 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
616 * The operating system has already ensured that the directory entry
617 * does not exist and done all appropriate namespace locking.
621 hammer_vop_ncreate(struct vop_ncreate_args
*ap
)
623 struct hammer_transaction trans
;
624 struct hammer_inode
*dip
;
625 struct hammer_inode
*nip
;
626 struct nchandle
*nch
;
630 dip
= VTOI(ap
->a_dvp
);
632 if (dip
->flags
& HAMMER_INODE_RO
)
634 if ((error
= hammer_checkspace(dip
->hmp
)) != 0)
638 * Create a transaction to cover the operations we perform.
640 hammer_start_transaction(&trans
, dip
->hmp
);
643 * Create a new filesystem object of the requested type. The
644 * returned inode will be referenced and shared-locked to prevent
645 * it from being moved to the flusher.
648 error
= hammer_create_inode(&trans
, ap
->a_vap
, ap
->a_cred
,
651 hkprintf("hammer_create_inode error %d\n", error
);
652 hammer_done_transaction(&trans
);
658 * Add the new filesystem object to the directory. This will also
659 * bump the inode's link count.
661 error
= hammer_ip_add_directory(&trans
, dip
,
662 nch
->ncp
->nc_name
, nch
->ncp
->nc_nlen
,
665 hkprintf("hammer_ip_add_directory error %d\n", error
);
671 hammer_rel_inode(nip
, 0);
672 hammer_done_transaction(&trans
);
675 error
= hammer_get_vnode(nip
, ap
->a_vpp
);
676 hammer_done_transaction(&trans
);
677 hammer_rel_inode(nip
, 0);
679 cache_setunresolved(ap
->a_nch
);
680 cache_setvp(ap
->a_nch
, *ap
->a_vpp
);
687 * hammer_vop_getattr { vp, vap }
689 * Retrieve an inode's attribute information. When accessing inodes
690 * historically we fake the atime field to ensure consistent results.
691 * The atime field is stored in the B-Tree element and allowed to be
692 * updated without cycling the element.
696 hammer_vop_getattr(struct vop_getattr_args
*ap
)
698 struct hammer_inode
*ip
= VTOI(ap
->a_vp
);
699 struct vattr
*vap
= ap
->a_vap
;
701 vap
->va_fsid
= ip
->hmp
->fsid_udev
;
703 * XXX munge the device if we are in a pseudo-fs, so user utilities
704 * do not think its the same 'filesystem'.
706 if (ip
->obj_localization
)
707 vap
->va_fsid
+= ip
->obj_localization
;
708 vap
->va_fileid
= ip
->ino_leaf
.base
.obj_id
;
709 vap
->va_mode
= ip
->ino_data
.mode
;
710 vap
->va_nlink
= ip
->ino_data
.nlinks
;
711 vap
->va_uid
= hammer_to_unix_xid(&ip
->ino_data
.uid
);
712 vap
->va_gid
= hammer_to_unix_xid(&ip
->ino_data
.gid
);
715 vap
->va_size
= ip
->ino_data
.size
;
718 * We must provide a consistent atime and mtime for snapshots
719 * so people can do a 'tar cf - ... | md5' on them and get
720 * consistent results.
722 if (ip
->flags
& HAMMER_INODE_RO
) {
723 hammer_time_to_timespec(ip
->ino_data
.ctime
, &vap
->va_atime
);
724 hammer_time_to_timespec(ip
->ino_data
.ctime
, &vap
->va_mtime
);
726 hammer_time_to_timespec(ip
->ino_data
.atime
, &vap
->va_atime
);
727 hammer_time_to_timespec(ip
->ino_data
.mtime
, &vap
->va_mtime
);
729 hammer_time_to_timespec(ip
->ino_data
.ctime
, &vap
->va_ctime
);
730 vap
->va_flags
= ip
->ino_data
.uflags
;
731 vap
->va_gen
= 1; /* hammer inums are unique for all time */
732 vap
->va_blocksize
= HAMMER_BUFSIZE
;
733 if (ip
->ino_data
.size
>= HAMMER_XDEMARC
) {
734 vap
->va_bytes
= (ip
->ino_data
.size
+ HAMMER_XBUFMASK64
) &
736 } else if (ip
->ino_data
.size
> HAMMER_BUFSIZE
/ 2) {
737 vap
->va_bytes
= (ip
->ino_data
.size
+ HAMMER_BUFMASK64
) &
740 vap
->va_bytes
= (ip
->ino_data
.size
+ 15) & ~15;
742 vap
->va_type
= hammer_get_vnode_type(ip
->ino_data
.obj_type
);
743 vap
->va_filerev
= 0; /* XXX */
744 /* mtime uniquely identifies any adjustments made to the file XXX */
745 vap
->va_fsmid
= ip
->ino_data
.mtime
;
746 vap
->va_uid_uuid
= ip
->ino_data
.uid
;
747 vap
->va_gid_uuid
= ip
->ino_data
.gid
;
748 vap
->va_fsid_uuid
= ip
->hmp
->fsid
;
749 vap
->va_vaflags
= VA_UID_UUID_VALID
| VA_GID_UUID_VALID
|
752 switch (ip
->ino_data
.obj_type
) {
753 case HAMMER_OBJTYPE_CDEV
:
754 case HAMMER_OBJTYPE_BDEV
:
755 vap
->va_rmajor
= ip
->ino_data
.rmajor
;
756 vap
->va_rminor
= ip
->ino_data
.rminor
;
766 * hammer_vop_nresolve { nch, dvp, cred }
768 * Locate the requested directory entry.
772 hammer_vop_nresolve(struct vop_nresolve_args
*ap
)
774 struct hammer_transaction trans
;
775 struct namecache
*ncp
;
779 struct hammer_cursor cursor
;
787 u_int32_t localization
;
790 * Misc initialization, plus handle as-of name extensions. Look for
791 * the '@@' extension. Note that as-of files and directories cannot
794 dip
= VTOI(ap
->a_dvp
);
795 ncp
= ap
->a_nch
->ncp
;
796 asof
= dip
->obj_asof
;
800 hammer_simple_transaction(&trans
, dip
->hmp
);
802 for (i
= 0; i
< nlen
; ++i
) {
803 if (ncp
->nc_name
[i
] == '@' && ncp
->nc_name
[i
+1] == '@') {
804 asof
= hammer_str_to_tid(ncp
->nc_name
+ i
+ 2);
805 flags
|= HAMMER_INODE_RO
;
812 * If there is no path component the time extension is relative to
816 ip
= hammer_get_inode(&trans
, dip
, dip
->obj_id
,
817 asof
, dip
->obj_localization
,
820 error
= hammer_get_vnode(ip
, &vp
);
821 hammer_rel_inode(ip
, 0);
827 cache_setvp(ap
->a_nch
, vp
);
834 * Calculate the namekey and setup the key range for the scan. This
835 * works kinda like a chained hash table where the lower 32 bits
836 * of the namekey synthesize the chain.
838 * The key range is inclusive of both key_beg and key_end.
840 namekey
= hammer_directory_namekey(ncp
->nc_name
, nlen
);
842 error
= hammer_init_cursor(&trans
, &cursor
, &dip
->cache
[1], dip
);
843 cursor
.key_beg
.localization
= dip
->obj_localization
+
844 HAMMER_LOCALIZE_MISC
;
845 cursor
.key_beg
.obj_id
= dip
->obj_id
;
846 cursor
.key_beg
.key
= namekey
;
847 cursor
.key_beg
.create_tid
= 0;
848 cursor
.key_beg
.delete_tid
= 0;
849 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DIRENTRY
;
850 cursor
.key_beg
.obj_type
= 0;
852 cursor
.key_end
= cursor
.key_beg
;
853 cursor
.key_end
.key
|= 0xFFFFFFFFULL
;
855 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
| HAMMER_CURSOR_ASOF
;
858 * Scan all matching records (the chain), locate the one matching
859 * the requested path component.
861 * The hammer_ip_*() functions merge in-memory records with on-disk
862 * records for the purposes of the search.
865 localization
= HAMMER_DEF_LOCALIZATION
;
868 error
= hammer_ip_first(&cursor
);
870 error
= hammer_ip_resolve_data(&cursor
);
873 if (nlen
== cursor
.leaf
->data_len
- HAMMER_ENTRY_NAME_OFF
&&
874 bcmp(ncp
->nc_name
, cursor
.data
->entry
.name
, nlen
) == 0) {
875 obj_id
= cursor
.data
->entry
.obj_id
;
876 localization
= cursor
.data
->entry
.localization
;
879 error
= hammer_ip_next(&cursor
);
882 hammer_done_cursor(&cursor
);
884 ip
= hammer_get_inode(&trans
, dip
, obj_id
,
888 error
= hammer_get_vnode(ip
, &vp
);
889 hammer_rel_inode(ip
, 0);
895 cache_setvp(ap
->a_nch
, vp
);
898 } else if (error
== ENOENT
) {
899 cache_setvp(ap
->a_nch
, NULL
);
902 hammer_done_transaction(&trans
);
907 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
909 * Locate the parent directory of a directory vnode.
911 * dvp is referenced but not locked. *vpp must be returned referenced and
912 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
913 * at the root, instead it could indicate that the directory we were in was
916 * NOTE: as-of sequences are not linked into the directory structure. If
917 * we are at the root with a different asof then the mount point, reload
918 * the same directory with the mount point's asof. I'm not sure what this
919 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
920 * get confused, but it hasn't been tested.
924 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args
*ap
)
926 struct hammer_transaction trans
;
927 struct hammer_inode
*dip
;
928 struct hammer_inode
*ip
;
929 int64_t parent_obj_id
;
930 u_int32_t parent_obj_localization
;
934 dip
= VTOI(ap
->a_dvp
);
935 asof
= dip
->obj_asof
;
938 * Whos are parent? This could be the root of a pseudo-filesystem
939 * whos parent is in another localization domain.
941 parent_obj_id
= dip
->ino_data
.parent_obj_id
;
942 if (dip
->obj_id
== HAMMER_OBJID_ROOT
)
943 parent_obj_localization
= dip
->ino_data
.ext
.obj
.parent_obj_localization
;
945 parent_obj_localization
= dip
->obj_localization
;
947 if (parent_obj_id
== 0) {
948 if (dip
->obj_id
== HAMMER_OBJID_ROOT
&&
949 asof
!= dip
->hmp
->asof
) {
950 parent_obj_id
= dip
->obj_id
;
951 asof
= dip
->hmp
->asof
;
952 *ap
->a_fakename
= kmalloc(19, M_TEMP
, M_WAITOK
);
953 ksnprintf(*ap
->a_fakename
, 19, "0x%016llx",
961 hammer_simple_transaction(&trans
, dip
->hmp
);
963 ip
= hammer_get_inode(&trans
, dip
, parent_obj_id
,
964 asof
, parent_obj_localization
,
967 error
= hammer_get_vnode(ip
, ap
->a_vpp
);
968 hammer_rel_inode(ip
, 0);
972 hammer_done_transaction(&trans
);
977 * hammer_vop_nlink { nch, dvp, vp, cred }
981 hammer_vop_nlink(struct vop_nlink_args
*ap
)
983 struct hammer_transaction trans
;
984 struct hammer_inode
*dip
;
985 struct hammer_inode
*ip
;
986 struct nchandle
*nch
;
990 dip
= VTOI(ap
->a_dvp
);
993 if (dip
->flags
& HAMMER_INODE_RO
)
995 if (ip
->flags
& HAMMER_INODE_RO
)
997 if ((error
= hammer_checkspace(dip
->hmp
)) != 0)
1001 * Create a transaction to cover the operations we perform.
1003 hammer_start_transaction(&trans
, dip
->hmp
);
1006 * Add the filesystem object to the directory. Note that neither
1007 * dip nor ip are referenced or locked, but their vnodes are
1008 * referenced. This function will bump the inode's link count.
1010 error
= hammer_ip_add_directory(&trans
, dip
,
1011 nch
->ncp
->nc_name
, nch
->ncp
->nc_nlen
,
1018 cache_setunresolved(nch
);
1019 cache_setvp(nch
, ap
->a_vp
);
1021 hammer_done_transaction(&trans
);
1026 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1028 * The operating system has already ensured that the directory entry
1029 * does not exist and done all appropriate namespace locking.
1033 hammer_vop_nmkdir(struct vop_nmkdir_args
*ap
)
1035 struct hammer_transaction trans
;
1036 struct hammer_inode
*dip
;
1037 struct hammer_inode
*nip
;
1038 struct nchandle
*nch
;
1042 dip
= VTOI(ap
->a_dvp
);
1044 if (dip
->flags
& HAMMER_INODE_RO
)
1046 if ((error
= hammer_checkspace(dip
->hmp
)) != 0)
1050 * Create a transaction to cover the operations we perform.
1052 hammer_start_transaction(&trans
, dip
->hmp
);
1055 * Create a new filesystem object of the requested type. The
1056 * returned inode will be referenced but not locked.
1058 error
= hammer_create_inode(&trans
, ap
->a_vap
, ap
->a_cred
,
1061 hkprintf("hammer_mkdir error %d\n", error
);
1062 hammer_done_transaction(&trans
);
1067 * Add the new filesystem object to the directory. This will also
1068 * bump the inode's link count.
1070 error
= hammer_ip_add_directory(&trans
, dip
,
1071 nch
->ncp
->nc_name
, nch
->ncp
->nc_nlen
,
1074 hkprintf("hammer_mkdir (add) error %d\n", error
);
1080 hammer_rel_inode(nip
, 0);
1083 error
= hammer_get_vnode(nip
, ap
->a_vpp
);
1084 hammer_rel_inode(nip
, 0);
1086 cache_setunresolved(ap
->a_nch
);
1087 cache_setvp(ap
->a_nch
, *ap
->a_vpp
);
1090 hammer_done_transaction(&trans
);
1095 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1097 * The operating system has already ensured that the directory entry
1098 * does not exist and done all appropriate namespace locking.
1102 hammer_vop_nmknod(struct vop_nmknod_args
*ap
)
1104 struct hammer_transaction trans
;
1105 struct hammer_inode
*dip
;
1106 struct hammer_inode
*nip
;
1107 struct nchandle
*nch
;
1112 dip
= VTOI(ap
->a_dvp
);
1114 if (dip
->flags
& HAMMER_INODE_RO
)
1116 if ((error
= hammer_checkspace(dip
->hmp
)) != 0)
1120 * Create a transaction to cover the operations we perform.
1122 hammer_start_transaction(&trans
, dip
->hmp
);
1125 * Create a new filesystem object of the requested type. The
1126 * returned inode will be referenced but not locked.
1128 * If mknod specifies a directory a pseudo-fs is created.
1130 pseudofs
= (ap
->a_vap
->va_type
== VDIR
);
1131 error
= hammer_create_inode(&trans
, ap
->a_vap
, ap
->a_cred
,
1132 dip
, pseudofs
, &nip
);
1134 hammer_done_transaction(&trans
);
1140 * Add the new filesystem object to the directory. This will also
1141 * bump the inode's link count.
1143 error
= hammer_ip_add_directory(&trans
, dip
,
1144 nch
->ncp
->nc_name
, nch
->ncp
->nc_nlen
,
1151 hammer_rel_inode(nip
, 0);
1154 error
= hammer_get_vnode(nip
, ap
->a_vpp
);
1155 hammer_rel_inode(nip
, 0);
1157 cache_setunresolved(ap
->a_nch
);
1158 cache_setvp(ap
->a_nch
, *ap
->a_vpp
);
1161 hammer_done_transaction(&trans
);
1166 * hammer_vop_open { vp, mode, cred, fp }
1170 hammer_vop_open(struct vop_open_args
*ap
)
1174 ip
= VTOI(ap
->a_vp
);
1176 if ((ap
->a_mode
& FWRITE
) && (ip
->flags
& HAMMER_INODE_RO
))
1178 return(vop_stdopen(ap
));
1182 * hammer_vop_pathconf { vp, name, retval }
1186 hammer_vop_pathconf(struct vop_pathconf_args
*ap
)
1192 * hammer_vop_print { vp }
1196 hammer_vop_print(struct vop_print_args
*ap
)
1202 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1206 hammer_vop_readdir(struct vop_readdir_args
*ap
)
1208 struct hammer_transaction trans
;
1209 struct hammer_cursor cursor
;
1210 struct hammer_inode
*ip
;
1212 hammer_base_elm_t base
;
1220 ip
= VTOI(ap
->a_vp
);
1222 saveoff
= uio
->uio_offset
;
1224 if (ap
->a_ncookies
) {
1225 ncookies
= uio
->uio_resid
/ 16 + 1;
1226 if (ncookies
> 1024)
1228 cookies
= kmalloc(ncookies
* sizeof(off_t
), M_TEMP
, M_WAITOK
);
1236 hammer_simple_transaction(&trans
, ip
->hmp
);
1239 * Handle artificial entries
1243 r
= vop_write_dirent(&error
, uio
, ip
->obj_id
, DT_DIR
, 1, ".");
1247 cookies
[cookie_index
] = saveoff
;
1250 if (cookie_index
== ncookies
)
1254 if (ip
->ino_data
.parent_obj_id
) {
1255 r
= vop_write_dirent(&error
, uio
,
1256 ip
->ino_data
.parent_obj_id
,
1259 r
= vop_write_dirent(&error
, uio
,
1260 ip
->obj_id
, DT_DIR
, 2, "..");
1265 cookies
[cookie_index
] = saveoff
;
1268 if (cookie_index
== ncookies
)
1273 * Key range (begin and end inclusive) to scan. Directory keys
1274 * directly translate to a 64 bit 'seek' position.
1276 hammer_init_cursor(&trans
, &cursor
, &ip
->cache
[1], ip
);
1277 cursor
.key_beg
.localization
= ip
->obj_localization
+
1278 HAMMER_LOCALIZE_MISC
;
1279 cursor
.key_beg
.obj_id
= ip
->obj_id
;
1280 cursor
.key_beg
.create_tid
= 0;
1281 cursor
.key_beg
.delete_tid
= 0;
1282 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DIRENTRY
;
1283 cursor
.key_beg
.obj_type
= 0;
1284 cursor
.key_beg
.key
= saveoff
;
1286 cursor
.key_end
= cursor
.key_beg
;
1287 cursor
.key_end
.key
= HAMMER_MAX_KEY
;
1288 cursor
.asof
= ip
->obj_asof
;
1289 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
| HAMMER_CURSOR_ASOF
;
1291 error
= hammer_ip_first(&cursor
);
1293 while (error
== 0) {
1294 error
= hammer_ip_resolve_data(&cursor
);
1297 base
= &cursor
.leaf
->base
;
1298 saveoff
= base
->key
;
1299 KKASSERT(cursor
.leaf
->data_len
> HAMMER_ENTRY_NAME_OFF
);
1301 if (base
->obj_id
!= ip
->obj_id
)
1302 panic("readdir: bad record at %p", cursor
.node
);
1304 r
= vop_write_dirent(
1305 &error
, uio
, cursor
.data
->entry
.obj_id
,
1306 hammer_get_dtype(cursor
.leaf
->base
.obj_type
),
1307 cursor
.leaf
->data_len
- HAMMER_ENTRY_NAME_OFF
,
1308 (void *)cursor
.data
->entry
.name
);
1313 cookies
[cookie_index
] = base
->key
;
1315 if (cookie_index
== ncookies
)
1317 error
= hammer_ip_next(&cursor
);
1319 hammer_done_cursor(&cursor
);
1322 hammer_done_transaction(&trans
);
1325 *ap
->a_eofflag
= (error
== ENOENT
);
1326 uio
->uio_offset
= saveoff
;
1327 if (error
&& cookie_index
== 0) {
1328 if (error
== ENOENT
)
1331 kfree(cookies
, M_TEMP
);
1332 *ap
->a_ncookies
= 0;
1333 *ap
->a_cookies
= NULL
;
1336 if (error
== ENOENT
)
1339 *ap
->a_ncookies
= cookie_index
;
1340 *ap
->a_cookies
= cookies
;
1347 * hammer_vop_readlink { vp, uio, cred }
1351 hammer_vop_readlink(struct vop_readlink_args
*ap
)
1353 struct hammer_transaction trans
;
1354 struct hammer_cursor cursor
;
1355 struct hammer_inode
*ip
;
1358 ip
= VTOI(ap
->a_vp
);
1361 * Shortcut if the symlink data was stuffed into ino_data.
1363 if (ip
->ino_data
.size
<= HAMMER_INODE_BASESYMLEN
) {
1364 error
= uiomove(ip
->ino_data
.ext
.symlink
,
1365 ip
->ino_data
.size
, ap
->a_uio
);
1372 hammer_simple_transaction(&trans
, ip
->hmp
);
1373 hammer_init_cursor(&trans
, &cursor
, &ip
->cache
[1], ip
);
1376 * Key range (begin and end inclusive) to scan. Directory keys
1377 * directly translate to a 64 bit 'seek' position.
1379 cursor
.key_beg
.localization
= ip
->obj_localization
+
1380 HAMMER_LOCALIZE_MISC
;
1381 cursor
.key_beg
.obj_id
= ip
->obj_id
;
1382 cursor
.key_beg
.create_tid
= 0;
1383 cursor
.key_beg
.delete_tid
= 0;
1384 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_FIX
;
1385 cursor
.key_beg
.obj_type
= 0;
1386 cursor
.key_beg
.key
= HAMMER_FIXKEY_SYMLINK
;
1387 cursor
.asof
= ip
->obj_asof
;
1388 cursor
.flags
|= HAMMER_CURSOR_ASOF
;
1390 error
= hammer_ip_lookup(&cursor
);
1392 error
= hammer_ip_resolve_data(&cursor
);
1394 KKASSERT(cursor
.leaf
->data_len
>=
1395 HAMMER_SYMLINK_NAME_OFF
);
1396 error
= uiomove(cursor
.data
->symlink
.name
,
1397 cursor
.leaf
->data_len
-
1398 HAMMER_SYMLINK_NAME_OFF
,
1402 hammer_done_cursor(&cursor
);
1403 hammer_done_transaction(&trans
);
1408 * hammer_vop_nremove { nch, dvp, cred }
1412 hammer_vop_nremove(struct vop_nremove_args
*ap
)
1414 struct hammer_transaction trans
;
1415 struct hammer_inode
*dip
;
1418 dip
= VTOI(ap
->a_dvp
);
1420 if (hammer_nohistory(dip
) == 0 &&
1421 (error
= hammer_checkspace(dip
->hmp
)) != 0) {
1425 hammer_start_transaction(&trans
, dip
->hmp
);
1426 error
= hammer_dounlink(&trans
, ap
->a_nch
, ap
->a_dvp
, ap
->a_cred
, 0);
1427 hammer_done_transaction(&trans
);
1433 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1437 hammer_vop_nrename(struct vop_nrename_args
*ap
)
1439 struct hammer_transaction trans
;
1440 struct namecache
*fncp
;
1441 struct namecache
*tncp
;
1442 struct hammer_inode
*fdip
;
1443 struct hammer_inode
*tdip
;
1444 struct hammer_inode
*ip
;
1445 struct hammer_cursor cursor
;
1449 fdip
= VTOI(ap
->a_fdvp
);
1450 tdip
= VTOI(ap
->a_tdvp
);
1451 fncp
= ap
->a_fnch
->ncp
;
1452 tncp
= ap
->a_tnch
->ncp
;
1453 ip
= VTOI(fncp
->nc_vp
);
1454 KKASSERT(ip
!= NULL
);
1456 if (fdip
->flags
& HAMMER_INODE_RO
)
1458 if (tdip
->flags
& HAMMER_INODE_RO
)
1460 if (ip
->flags
& HAMMER_INODE_RO
)
1462 if ((error
= hammer_checkspace(fdip
->hmp
)) != 0)
1465 hammer_start_transaction(&trans
, fdip
->hmp
);
1468 * Remove tncp from the target directory and then link ip as
1469 * tncp. XXX pass trans to dounlink
1471 * Force the inode sync-time to match the transaction so it is
1472 * in-sync with the creation of the target directory entry.
1474 error
= hammer_dounlink(&trans
, ap
->a_tnch
, ap
->a_tdvp
, ap
->a_cred
, 0);
1475 if (error
== 0 || error
== ENOENT
) {
1476 error
= hammer_ip_add_directory(&trans
, tdip
,
1477 tncp
->nc_name
, tncp
->nc_nlen
,
1480 ip
->ino_data
.parent_obj_id
= tdip
->obj_id
;
1481 hammer_modify_inode(ip
, HAMMER_INODE_DDIRTY
);
1485 goto failed
; /* XXX */
1488 * Locate the record in the originating directory and remove it.
1490 * Calculate the namekey and setup the key range for the scan. This
1491 * works kinda like a chained hash table where the lower 32 bits
1492 * of the namekey synthesize the chain.
1494 * The key range is inclusive of both key_beg and key_end.
1496 namekey
= hammer_directory_namekey(fncp
->nc_name
, fncp
->nc_nlen
);
1498 hammer_init_cursor(&trans
, &cursor
, &fdip
->cache
[1], fdip
);
1499 cursor
.key_beg
.localization
= fdip
->obj_localization
+
1500 HAMMER_LOCALIZE_MISC
;
1501 cursor
.key_beg
.obj_id
= fdip
->obj_id
;
1502 cursor
.key_beg
.key
= namekey
;
1503 cursor
.key_beg
.create_tid
= 0;
1504 cursor
.key_beg
.delete_tid
= 0;
1505 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DIRENTRY
;
1506 cursor
.key_beg
.obj_type
= 0;
1508 cursor
.key_end
= cursor
.key_beg
;
1509 cursor
.key_end
.key
|= 0xFFFFFFFFULL
;
1510 cursor
.asof
= fdip
->obj_asof
;
1511 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
| HAMMER_CURSOR_ASOF
;
1514 * Scan all matching records (the chain), locate the one matching
1515 * the requested path component.
1517 * The hammer_ip_*() functions merge in-memory records with on-disk
1518 * records for the purposes of the search.
1520 error
= hammer_ip_first(&cursor
);
1521 while (error
== 0) {
1522 if (hammer_ip_resolve_data(&cursor
) != 0)
1524 nlen
= cursor
.leaf
->data_len
- HAMMER_ENTRY_NAME_OFF
;
1526 if (fncp
->nc_nlen
== nlen
&&
1527 bcmp(fncp
->nc_name
, cursor
.data
->entry
.name
, nlen
) == 0) {
1530 error
= hammer_ip_next(&cursor
);
1534 * If all is ok we have to get the inode so we can adjust nlinks.
1536 * WARNING: hammer_ip_del_directory() may have to terminate the
1537 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1541 error
= hammer_ip_del_directory(&trans
, &cursor
, fdip
, ip
);
1544 * XXX A deadlock here will break rename's atomicy for the purposes
1545 * of crash recovery.
1547 if (error
== EDEADLK
) {
1548 hammer_done_cursor(&cursor
);
1553 * Cleanup and tell the kernel that the rename succeeded.
1555 hammer_done_cursor(&cursor
);
1557 cache_rename(ap
->a_fnch
, ap
->a_tnch
);
1560 hammer_done_transaction(&trans
);
1565 * hammer_vop_nrmdir { nch, dvp, cred }
1569 hammer_vop_nrmdir(struct vop_nrmdir_args
*ap
)
1571 struct hammer_transaction trans
;
1572 struct hammer_inode
*dip
;
1575 dip
= VTOI(ap
->a_dvp
);
1577 if (hammer_nohistory(dip
) == 0 &&
1578 (error
= hammer_checkspace(dip
->hmp
)) != 0) {
1582 hammer_start_transaction(&trans
, dip
->hmp
);
1583 error
= hammer_dounlink(&trans
, ap
->a_nch
, ap
->a_dvp
, ap
->a_cred
, 0);
1584 hammer_done_transaction(&trans
);
1590 * hammer_vop_setattr { vp, vap, cred }
1594 hammer_vop_setattr(struct vop_setattr_args
*ap
)
1596 struct hammer_transaction trans
;
1598 struct hammer_inode
*ip
;
1603 int64_t aligned_size
;
1607 ip
= ap
->a_vp
->v_data
;
1610 if (ap
->a_vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
1612 if (ip
->flags
& HAMMER_INODE_RO
)
1614 if (hammer_nohistory(ip
) == 0 &&
1615 (error
= hammer_checkspace(ip
->hmp
)) != 0) {
1619 hammer_start_transaction(&trans
, ip
->hmp
);
1622 if (vap
->va_flags
!= VNOVAL
) {
1623 flags
= ip
->ino_data
.uflags
;
1624 error
= vop_helper_setattr_flags(&flags
, vap
->va_flags
,
1625 hammer_to_unix_xid(&ip
->ino_data
.uid
),
1628 if (ip
->ino_data
.uflags
!= flags
) {
1629 ip
->ino_data
.uflags
= flags
;
1630 modflags
|= HAMMER_INODE_DDIRTY
;
1632 if (ip
->ino_data
.uflags
& (IMMUTABLE
| APPEND
)) {
1639 if (ip
->ino_data
.uflags
& (IMMUTABLE
| APPEND
)) {
1643 if (vap
->va_uid
!= (uid_t
)VNOVAL
|| vap
->va_gid
!= (gid_t
)VNOVAL
) {
1644 mode_t cur_mode
= ip
->ino_data
.mode
;
1645 uid_t cur_uid
= hammer_to_unix_xid(&ip
->ino_data
.uid
);
1646 gid_t cur_gid
= hammer_to_unix_xid(&ip
->ino_data
.gid
);
1650 error
= vop_helper_chown(ap
->a_vp
, vap
->va_uid
, vap
->va_gid
,
1652 &cur_uid
, &cur_gid
, &cur_mode
);
1654 hammer_guid_to_uuid(&uuid_uid
, cur_uid
);
1655 hammer_guid_to_uuid(&uuid_gid
, cur_gid
);
1656 if (bcmp(&uuid_uid
, &ip
->ino_data
.uid
,
1657 sizeof(uuid_uid
)) ||
1658 bcmp(&uuid_gid
, &ip
->ino_data
.gid
,
1659 sizeof(uuid_gid
)) ||
1660 ip
->ino_data
.mode
!= cur_mode
1662 ip
->ino_data
.uid
= uuid_uid
;
1663 ip
->ino_data
.gid
= uuid_gid
;
1664 ip
->ino_data
.mode
= cur_mode
;
1666 modflags
|= HAMMER_INODE_DDIRTY
;
1669 while (vap
->va_size
!= VNOVAL
&& ip
->ino_data
.size
!= vap
->va_size
) {
1670 switch(ap
->a_vp
->v_type
) {
1672 if (vap
->va_size
== ip
->ino_data
.size
)
1675 * XXX break atomicy, we can deadlock the backend
1676 * if we do not release the lock. Probably not a
1679 blksize
= hammer_blocksize(vap
->va_size
);
1680 if (vap
->va_size
< ip
->ino_data
.size
) {
1681 vtruncbuf(ap
->a_vp
, vap
->va_size
, blksize
);
1684 vnode_pager_setsize(ap
->a_vp
, vap
->va_size
);
1687 ip
->ino_data
.size
= vap
->va_size
;
1688 modflags
|= HAMMER_INODE_DDIRTY
;
1691 * on-media truncation is cached in the inode until
1692 * the inode is synchronized.
1695 hammer_ip_frontend_trunc(ip
, vap
->va_size
);
1696 hammer_update_rsv_databufs(ip
);
1697 #ifdef DEBUG_TRUNCATE
1698 if (HammerTruncIp
== NULL
)
1701 if ((ip
->flags
& HAMMER_INODE_TRUNCATED
) == 0) {
1702 ip
->flags
|= HAMMER_INODE_TRUNCATED
;
1703 ip
->trunc_off
= vap
->va_size
;
1704 #ifdef DEBUG_TRUNCATE
1705 if (ip
== HammerTruncIp
)
1706 kprintf("truncate1 %016llx\n", ip
->trunc_off
);
1708 } else if (ip
->trunc_off
> vap
->va_size
) {
1709 ip
->trunc_off
= vap
->va_size
;
1710 #ifdef DEBUG_TRUNCATE
1711 if (ip
== HammerTruncIp
)
1712 kprintf("truncate2 %016llx\n", ip
->trunc_off
);
1715 #ifdef DEBUG_TRUNCATE
1716 if (ip
== HammerTruncIp
)
1717 kprintf("truncate3 %016llx (ignored)\n", vap
->va_size
);
1723 * If truncating we have to clean out a portion of
1724 * the last block on-disk. We do this in the
1725 * front-end buffer cache.
1727 aligned_size
= (vap
->va_size
+ (blksize
- 1)) &
1728 ~(int64_t)(blksize
- 1);
1729 if (truncating
&& vap
->va_size
< aligned_size
) {
1733 aligned_size
-= blksize
;
1735 offset
= (int)vap
->va_size
& (blksize
- 1);
1736 error
= bread(ap
->a_vp
, aligned_size
,
1738 hammer_ip_frontend_trunc(ip
, aligned_size
);
1740 bzero(bp
->b_data
+ offset
,
1744 kprintf("ERROR %d\n", error
);
1750 if ((ip
->flags
& HAMMER_INODE_TRUNCATED
) == 0) {
1751 ip
->flags
|= HAMMER_INODE_TRUNCATED
;
1752 ip
->trunc_off
= vap
->va_size
;
1753 } else if (ip
->trunc_off
> vap
->va_size
) {
1754 ip
->trunc_off
= vap
->va_size
;
1756 hammer_ip_frontend_trunc(ip
, vap
->va_size
);
1757 ip
->ino_data
.size
= vap
->va_size
;
1758 modflags
|= HAMMER_INODE_DDIRTY
;
1766 if (vap
->va_atime
.tv_sec
!= VNOVAL
) {
1767 ip
->ino_data
.atime
=
1768 hammer_timespec_to_time(&vap
->va_atime
);
1769 modflags
|= HAMMER_INODE_ATIME
;
1771 if (vap
->va_mtime
.tv_sec
!= VNOVAL
) {
1772 ip
->ino_data
.mtime
=
1773 hammer_timespec_to_time(&vap
->va_mtime
);
1774 modflags
|= HAMMER_INODE_MTIME
;
1776 if (vap
->va_mode
!= (mode_t
)VNOVAL
) {
1777 mode_t cur_mode
= ip
->ino_data
.mode
;
1778 uid_t cur_uid
= hammer_to_unix_xid(&ip
->ino_data
.uid
);
1779 gid_t cur_gid
= hammer_to_unix_xid(&ip
->ino_data
.gid
);
1781 error
= vop_helper_chmod(ap
->a_vp
, vap
->va_mode
, ap
->a_cred
,
1782 cur_uid
, cur_gid
, &cur_mode
);
1783 if (error
== 0 && ip
->ino_data
.mode
!= cur_mode
) {
1784 ip
->ino_data
.mode
= cur_mode
;
1785 modflags
|= HAMMER_INODE_DDIRTY
;
1790 hammer_modify_inode(ip
, modflags
);
1791 hammer_done_transaction(&trans
);
1796 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1800 hammer_vop_nsymlink(struct vop_nsymlink_args
*ap
)
1802 struct hammer_transaction trans
;
1803 struct hammer_inode
*dip
;
1804 struct hammer_inode
*nip
;
1805 struct nchandle
*nch
;
1806 hammer_record_t record
;
1810 ap
->a_vap
->va_type
= VLNK
;
1813 dip
= VTOI(ap
->a_dvp
);
1815 if (dip
->flags
& HAMMER_INODE_RO
)
1817 if ((error
= hammer_checkspace(dip
->hmp
)) != 0)
1821 * Create a transaction to cover the operations we perform.
1823 hammer_start_transaction(&trans
, dip
->hmp
);
1826 * Create a new filesystem object of the requested type. The
1827 * returned inode will be referenced but not locked.
1830 error
= hammer_create_inode(&trans
, ap
->a_vap
, ap
->a_cred
,
1833 hammer_done_transaction(&trans
);
1839 * Add a record representing the symlink. symlink stores the link
1840 * as pure data, not a string, and is no \0 terminated.
1843 bytes
= strlen(ap
->a_target
);
1845 if (bytes
<= HAMMER_INODE_BASESYMLEN
) {
1846 bcopy(ap
->a_target
, nip
->ino_data
.ext
.symlink
, bytes
);
1848 record
= hammer_alloc_mem_record(nip
, bytes
);
1849 record
->type
= HAMMER_MEM_RECORD_GENERAL
;
1851 record
->leaf
.base
.localization
= nip
->obj_localization
+
1852 HAMMER_LOCALIZE_MISC
;
1853 record
->leaf
.base
.key
= HAMMER_FIXKEY_SYMLINK
;
1854 record
->leaf
.base
.rec_type
= HAMMER_RECTYPE_FIX
;
1855 record
->leaf
.data_len
= bytes
;
1856 KKASSERT(HAMMER_SYMLINK_NAME_OFF
== 0);
1857 bcopy(ap
->a_target
, record
->data
->symlink
.name
, bytes
);
1858 error
= hammer_ip_add_record(&trans
, record
);
1862 * Set the file size to the length of the link.
1865 nip
->ino_data
.size
= bytes
;
1866 hammer_modify_inode(nip
, HAMMER_INODE_DDIRTY
);
1870 error
= hammer_ip_add_directory(&trans
, dip
, nch
->ncp
->nc_name
,
1871 nch
->ncp
->nc_nlen
, nip
);
1877 hammer_rel_inode(nip
, 0);
1880 error
= hammer_get_vnode(nip
, ap
->a_vpp
);
1881 hammer_rel_inode(nip
, 0);
1883 cache_setunresolved(ap
->a_nch
);
1884 cache_setvp(ap
->a_nch
, *ap
->a_vpp
);
1887 hammer_done_transaction(&trans
);
1892 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1896 hammer_vop_nwhiteout(struct vop_nwhiteout_args
*ap
)
1898 struct hammer_transaction trans
;
1899 struct hammer_inode
*dip
;
1902 dip
= VTOI(ap
->a_dvp
);
1904 if (hammer_nohistory(dip
) == 0 &&
1905 (error
= hammer_checkspace(dip
->hmp
)) != 0) {
1909 hammer_start_transaction(&trans
, dip
->hmp
);
1910 error
= hammer_dounlink(&trans
, ap
->a_nch
, ap
->a_dvp
,
1911 ap
->a_cred
, ap
->a_flags
);
1912 hammer_done_transaction(&trans
);
1918 * hammer_vop_ioctl { vp, command, data, fflag, cred }
1922 hammer_vop_ioctl(struct vop_ioctl_args
*ap
)
1924 struct hammer_inode
*ip
= ap
->a_vp
->v_data
;
1926 return(hammer_ioctl(ip
, ap
->a_command
, ap
->a_data
,
1927 ap
->a_fflag
, ap
->a_cred
));
1932 hammer_vop_mountctl(struct vop_mountctl_args
*ap
)
1937 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
1940 case MOUNTCTL_SET_EXPORT
:
1941 if (ap
->a_ctllen
!= sizeof(struct export_args
))
1943 error
= hammer_vfs_export(mp
, ap
->a_op
,
1944 (const struct export_args
*)ap
->a_ctl
);
1947 error
= journal_mountctl(ap
);
1954 * hammer_vop_strategy { vp, bio }
1956 * Strategy call, used for regular file read & write only. Note that the
1957 * bp may represent a cluster.
1959 * To simplify operation and allow better optimizations in the future,
1960 * this code does not make any assumptions with regards to buffer alignment
1965 hammer_vop_strategy(struct vop_strategy_args
*ap
)
1970 bp
= ap
->a_bio
->bio_buf
;
1974 error
= hammer_vop_strategy_read(ap
);
1977 error
= hammer_vop_strategy_write(ap
);
1980 bp
->b_error
= error
= EINVAL
;
1981 bp
->b_flags
|= B_ERROR
;
1989 * Read from a regular file. Iterate the related records and fill in the
1990 * BIO/BUF. Gaps are zero-filled.
1992 * The support code in hammer_object.c should be used to deal with mixed
1993 * in-memory and on-disk records.
1995 * NOTE: Can be called from the cluster code with an oversized buf.
2001 hammer_vop_strategy_read(struct vop_strategy_args
*ap
)
2003 struct hammer_transaction trans
;
2004 struct hammer_inode
*ip
;
2005 struct hammer_cursor cursor
;
2006 hammer_base_elm_t base
;
2007 hammer_off_t disk_offset
;
2021 ip
= ap
->a_vp
->v_data
;
2024 * The zone-2 disk offset may have been set by the cluster code via
2025 * a BMAP operation, or else should be NOOFFSET.
2027 * Checking the high bits for a match against zone-2 should suffice.
2029 nbio
= push_bio(bio
);
2030 if ((nbio
->bio_offset
& HAMMER_OFF_ZONE_MASK
) ==
2031 HAMMER_ZONE_RAW_BUFFER
) {
2032 error
= hammer_io_direct_read(ip
->hmp
, nbio
);
2037 * Well, that sucked. Do it the hard way. If all the stars are
2038 * aligned we may still be able to issue a direct-read.
2040 hammer_simple_transaction(&trans
, ip
->hmp
);
2041 hammer_init_cursor(&trans
, &cursor
, &ip
->cache
[1], ip
);
2044 * Key range (begin and end inclusive) to scan. Note that the key's
2045 * stored in the actual records represent BASE+LEN, not BASE. The
2046 * first record containing bio_offset will have a key > bio_offset.
2048 cursor
.key_beg
.localization
= ip
->obj_localization
+
2049 HAMMER_LOCALIZE_MISC
;
2050 cursor
.key_beg
.obj_id
= ip
->obj_id
;
2051 cursor
.key_beg
.create_tid
= 0;
2052 cursor
.key_beg
.delete_tid
= 0;
2053 cursor
.key_beg
.obj_type
= 0;
2054 cursor
.key_beg
.key
= bio
->bio_offset
+ 1;
2055 cursor
.asof
= ip
->obj_asof
;
2056 cursor
.flags
|= HAMMER_CURSOR_ASOF
;
2058 cursor
.key_end
= cursor
.key_beg
;
2059 KKASSERT(ip
->ino_data
.obj_type
== HAMMER_OBJTYPE_REGFILE
);
2061 if (ip
->ino_data
.obj_type
== HAMMER_OBJTYPE_DBFILE
) {
2062 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DB
;
2063 cursor
.key_end
.rec_type
= HAMMER_RECTYPE_DB
;
2064 cursor
.key_end
.key
= 0x7FFFFFFFFFFFFFFFLL
;
2068 ran_end
= bio
->bio_offset
+ bp
->b_bufsize
;
2069 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DATA
;
2070 cursor
.key_end
.rec_type
= HAMMER_RECTYPE_DATA
;
2071 tmp64
= ran_end
+ MAXPHYS
+ 1; /* work-around GCC-4 bug */
2072 if (tmp64
< ran_end
)
2073 cursor
.key_end
.key
= 0x7FFFFFFFFFFFFFFFLL
;
2075 cursor
.key_end
.key
= ran_end
+ MAXPHYS
+ 1;
2077 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
;
2079 error
= hammer_ip_first(&cursor
);
2082 while (error
== 0) {
2084 * Get the base file offset of the record. The key for
2085 * data records is (base + bytes) rather then (base).
2087 base
= &cursor
.leaf
->base
;
2088 rec_offset
= base
->key
- cursor
.leaf
->data_len
;
2091 * Calculate the gap, if any, and zero-fill it.
2093 * n is the offset of the start of the record verses our
2094 * current seek offset in the bio.
2096 n
= (int)(rec_offset
- (bio
->bio_offset
+ boff
));
2098 if (n
> bp
->b_bufsize
- boff
)
2099 n
= bp
->b_bufsize
- boff
;
2100 bzero((char *)bp
->b_data
+ boff
, n
);
2106 * Calculate the data offset in the record and the number
2107 * of bytes we can copy.
2109 * There are two degenerate cases. First, boff may already
2110 * be at bp->b_bufsize. Secondly, the data offset within
2111 * the record may exceed the record's size.
2115 n
= cursor
.leaf
->data_len
- roff
;
2117 kprintf("strategy_read: bad n=%d roff=%d\n", n
, roff
);
2119 } else if (n
> bp
->b_bufsize
- boff
) {
2120 n
= bp
->b_bufsize
- boff
;
2124 * Deal with cached truncations. This cool bit of code
2125 * allows truncate()/ftruncate() to avoid having to sync
2128 * If the frontend is truncated then all backend records are
2129 * subject to the frontend's truncation.
2131 * If the backend is truncated then backend records on-disk
2132 * (but not in-memory) are subject to the backend's
2133 * truncation. In-memory records owned by the backend
2134 * represent data written after the truncation point on the
2135 * backend and must not be truncated.
2137 * Truncate operations deal with frontend buffer cache
2138 * buffers and frontend-owned in-memory records synchronously.
2140 if (ip
->flags
& HAMMER_INODE_TRUNCATED
) {
2141 if (hammer_cursor_ondisk(&cursor
) ||
2142 cursor
.iprec
->flush_state
== HAMMER_FST_FLUSH
) {
2143 if (ip
->trunc_off
<= rec_offset
)
2145 else if (ip
->trunc_off
< rec_offset
+ n
)
2146 n
= (int)(ip
->trunc_off
- rec_offset
);
2149 if (ip
->sync_flags
& HAMMER_INODE_TRUNCATED
) {
2150 if (hammer_cursor_ondisk(&cursor
)) {
2151 if (ip
->sync_trunc_off
<= rec_offset
)
2153 else if (ip
->sync_trunc_off
< rec_offset
+ n
)
2154 n
= (int)(ip
->sync_trunc_off
- rec_offset
);
2159 * Try to issue a direct read into our bio if possible,
2160 * otherwise resolve the element data into a hammer_buffer
2163 * The buffer on-disk should be zerod past any real
2164 * truncation point, but may not be for any synthesized
2165 * truncation point from above.
2167 if (boff
== 0 && n
== bp
->b_bufsize
&&
2168 ((cursor
.leaf
->data_offset
+ roff
) & HAMMER_BUFMASK
) == 0) {
2169 disk_offset
= hammer_blockmap_lookup(
2171 cursor
.leaf
->data_offset
+ roff
,
2175 nbio
->bio_offset
= disk_offset
;
2176 error
= hammer_io_direct_read(trans
.hmp
, nbio
);
2179 error
= hammer_ip_resolve_data(&cursor
);
2181 bcopy((char *)cursor
.data
+ roff
,
2182 (char *)bp
->b_data
+ boff
, n
);
2189 * Iterate until we have filled the request.
2192 if (boff
== bp
->b_bufsize
)
2194 error
= hammer_ip_next(&cursor
);
2198 * There may have been a gap after the last record
2200 if (error
== ENOENT
)
2202 if (error
== 0 && boff
!= bp
->b_bufsize
) {
2203 KKASSERT(boff
< bp
->b_bufsize
);
2204 bzero((char *)bp
->b_data
+ boff
, bp
->b_bufsize
- boff
);
2205 /* boff = bp->b_bufsize; */
2208 bp
->b_error
= error
;
2210 bp
->b_flags
|= B_ERROR
;
2215 hammer_cache_node(&ip
->cache
[1], cursor
.node
);
2216 hammer_done_cursor(&cursor
);
2217 hammer_done_transaction(&trans
);
2222 * BMAP operation - used to support cluster_read() only.
2224 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2226 * This routine may return EOPNOTSUPP if the opration is not supported for
2227 * the specified offset. The contents of the pointer arguments do not
2228 * need to be initialized in that case.
2230 * If a disk address is available and properly aligned return 0 with
2231 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2232 * to the run-length relative to that offset. Callers may assume that
2233 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2234 * large, so return EOPNOTSUPP if it is not sufficiently large.
2238 hammer_vop_bmap(struct vop_bmap_args
*ap
)
2240 struct hammer_transaction trans
;
2241 struct hammer_inode
*ip
;
2242 struct hammer_cursor cursor
;
2243 hammer_base_elm_t base
;
2247 int64_t base_offset
;
2248 int64_t base_disk_offset
;
2249 int64_t last_offset
;
2250 hammer_off_t last_disk_offset
;
2251 hammer_off_t disk_offset
;
2256 ip
= ap
->a_vp
->v_data
;
2259 * We can only BMAP regular files. We can't BMAP database files,
2262 if (ip
->ino_data
.obj_type
!= HAMMER_OBJTYPE_REGFILE
)
2266 * bmap is typically called with runp/runb both NULL when used
2267 * for writing. We do not support BMAP for writing atm.
2269 if (ap
->a_cmd
!= BUF_CMD_READ
)
2273 * Scan the B-Tree to acquire blockmap addresses, then translate
2276 hammer_simple_transaction(&trans
, ip
->hmp
);
2278 kprintf("bmap_beg %016llx ip->cache %p\n", ap
->a_loffset
, ip
->cache
[1]);
2280 hammer_init_cursor(&trans
, &cursor
, &ip
->cache
[1], ip
);
2283 * Key range (begin and end inclusive) to scan. Note that the key's
2284 * stored in the actual records represent BASE+LEN, not BASE. The
2285 * first record containing bio_offset will have a key > bio_offset.
2287 cursor
.key_beg
.localization
= ip
->obj_localization
+
2288 HAMMER_LOCALIZE_MISC
;
2289 cursor
.key_beg
.obj_id
= ip
->obj_id
;
2290 cursor
.key_beg
.create_tid
= 0;
2291 cursor
.key_beg
.delete_tid
= 0;
2292 cursor
.key_beg
.obj_type
= 0;
2294 cursor
.key_beg
.key
= ap
->a_loffset
- MAXPHYS
+ 1;
2296 cursor
.key_beg
.key
= ap
->a_loffset
+ 1;
2297 if (cursor
.key_beg
.key
< 0)
2298 cursor
.key_beg
.key
= 0;
2299 cursor
.asof
= ip
->obj_asof
;
2300 cursor
.flags
|= HAMMER_CURSOR_ASOF
;
2302 cursor
.key_end
= cursor
.key_beg
;
2303 KKASSERT(ip
->ino_data
.obj_type
== HAMMER_OBJTYPE_REGFILE
);
2305 ran_end
= ap
->a_loffset
+ MAXPHYS
;
2306 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DATA
;
2307 cursor
.key_end
.rec_type
= HAMMER_RECTYPE_DATA
;
2308 tmp64
= ran_end
+ MAXPHYS
+ 1; /* work-around GCC-4 bug */
2309 if (tmp64
< ran_end
)
2310 cursor
.key_end
.key
= 0x7FFFFFFFFFFFFFFFLL
;
2312 cursor
.key_end
.key
= ran_end
+ MAXPHYS
+ 1;
2314 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
;
2316 error
= hammer_ip_first(&cursor
);
2317 base_offset
= last_offset
= 0;
2318 base_disk_offset
= last_disk_offset
= 0;
2320 while (error
== 0) {
2322 * Get the base file offset of the record. The key for
2323 * data records is (base + bytes) rather then (base).
2325 * NOTE: rec_offset + rec_len may exceed the end-of-file.
2326 * The extra bytes should be zero on-disk and the BMAP op
2327 * should still be ok.
2329 base
= &cursor
.leaf
->base
;
2330 rec_offset
= base
->key
- cursor
.leaf
->data_len
;
2331 rec_len
= cursor
.leaf
->data_len
;
2334 * Incorporate any cached truncation.
2336 * NOTE: Modifications to rec_len based on synthesized
2337 * truncation points remove the guarantee that any extended
2338 * data on disk is zero (since the truncations may not have
2339 * taken place on-media yet).
2341 if (ip
->flags
& HAMMER_INODE_TRUNCATED
) {
2342 if (hammer_cursor_ondisk(&cursor
) ||
2343 cursor
.iprec
->flush_state
== HAMMER_FST_FLUSH
) {
2344 if (ip
->trunc_off
<= rec_offset
)
2346 else if (ip
->trunc_off
< rec_offset
+ rec_len
)
2347 rec_len
= (int)(ip
->trunc_off
- rec_offset
);
2350 if (ip
->sync_flags
& HAMMER_INODE_TRUNCATED
) {
2351 if (hammer_cursor_ondisk(&cursor
)) {
2352 if (ip
->sync_trunc_off
<= rec_offset
)
2354 else if (ip
->sync_trunc_off
< rec_offset
+ rec_len
)
2355 rec_len
= (int)(ip
->sync_trunc_off
- rec_offset
);
2360 * Accumulate information. If we have hit a discontiguous
2361 * block reset base_offset unless we are already beyond the
2362 * requested offset. If we are, that's it, we stop.
2364 disk_offset
= hammer_blockmap_lookup(trans
.hmp
,
2365 cursor
.leaf
->data_offset
,
2369 if (rec_offset
!= last_offset
||
2370 disk_offset
!= last_disk_offset
) {
2371 if (rec_offset
> ap
->a_loffset
)
2373 base_offset
= rec_offset
;
2374 base_disk_offset
= disk_offset
;
2376 last_offset
= rec_offset
+ rec_len
;
2377 last_disk_offset
= disk_offset
+ rec_len
;
2379 error
= hammer_ip_next(&cursor
);
2383 kprintf("BMAP %016llx: %016llx - %016llx\n",
2384 ap
->a_loffset
, base_offset
, last_offset
);
2385 kprintf("BMAP %16s: %016llx - %016llx\n",
2386 "", base_disk_offset
, last_disk_offset
);
2390 hammer_cache_node(&ip
->cache
[1], cursor
.node
);
2392 kprintf("bmap_end2 %016llx ip->cache %p\n", ap
->a_loffset
, ip
->cache
[1]);
2395 hammer_done_cursor(&cursor
);
2396 hammer_done_transaction(&trans
);
2399 * If we couldn't find any records or the records we did find were
2400 * all behind the requested offset, return failure. A forward
2401 * truncation can leave a hole w/ no on-disk records.
2403 if (last_offset
== 0 || last_offset
< ap
->a_loffset
)
2404 return (EOPNOTSUPP
);
2407 * Figure out the block size at the requested offset and adjust
2408 * our limits so the cluster_read() does not create inappropriately
2409 * sized buffer cache buffers.
2411 blksize
= hammer_blocksize(ap
->a_loffset
);
2412 if (hammer_blocksize(base_offset
) != blksize
) {
2413 base_offset
= hammer_blockdemarc(base_offset
, ap
->a_loffset
);
2415 if (last_offset
!= ap
->a_loffset
&&
2416 hammer_blocksize(last_offset
- 1) != blksize
) {
2417 last_offset
= hammer_blockdemarc(ap
->a_loffset
,
2422 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
2425 disk_offset
= base_disk_offset
+ (ap
->a_loffset
- base_offset
);
2428 * If doffsetp is not aligned or the forward run size does
2429 * not cover a whole buffer, disallow the direct I/O.
2431 if ((disk_offset
& HAMMER_BUFMASK
) ||
2432 (last_offset
- ap
->a_loffset
) < blksize
) {
2435 *ap
->a_doffsetp
= disk_offset
;
2437 *ap
->a_runb
= ap
->a_loffset
- base_offset
;
2438 KKASSERT(*ap
->a_runb
>= 0);
2441 *ap
->a_runp
= last_offset
- ap
->a_loffset
;
2442 KKASSERT(*ap
->a_runp
>= 0);
2450 * Write to a regular file. Because this is a strategy call the OS is
2451 * trying to actually get data onto the media.
2455 hammer_vop_strategy_write(struct vop_strategy_args
*ap
)
2457 hammer_record_t record
;
2467 ip
= ap
->a_vp
->v_data
;
2470 KKASSERT(bp
->b_bufsize
== hammer_blocksize(bio
->bio_offset
));
2472 if (ip
->flags
& HAMMER_INODE_RO
) {
2473 bp
->b_error
= EROFS
;
2474 bp
->b_flags
|= B_ERROR
;
2476 hammer_cleanup_write_io(ip
);
2481 * Interlock with inode destruction (no in-kernel or directory
2482 * topology visibility). If we queue new IO while trying to
2483 * destroy the inode we can deadlock the vtrunc call in
2484 * hammer_inode_unloadable_check().
2486 if (ip
->flags
& (HAMMER_INODE_DELETING
|HAMMER_INODE_DELETED
)) {
2489 hammer_cleanup_write_io(ip
);
2494 * Reserve space and issue a direct-write from the front-end.
2495 * NOTE: The direct_io code will hammer_bread/bcopy smaller
2498 * An in-memory record will be installed to reference the storage
2499 * until the flusher can get to it.
2501 * Since we own the high level bio the front-end will not try to
2502 * do a direct-read until the write completes.
2504 * NOTE: The only time we do not reserve a full-sized buffers
2505 * worth of data is if the file is small. We do not try to
2506 * allocate a fragment (from the small-data zone) at the end of
2507 * an otherwise large file as this can lead to wildly separated
2510 KKASSERT((bio
->bio_offset
& HAMMER_BUFMASK
) == 0);
2511 KKASSERT(bio
->bio_offset
< ip
->ino_data
.size
);
2512 if (bio
->bio_offset
|| ip
->ino_data
.size
> HAMMER_BUFSIZE
/ 2)
2513 bytes
= bp
->b_bufsize
;
2515 bytes
= ((int)ip
->ino_data
.size
+ 15) & ~15;
2517 record
= hammer_ip_add_bulk(ip
, bio
->bio_offset
, bp
->b_data
,
2520 hammer_io_direct_write(hmp
, &record
->leaf
, bio
);
2521 hammer_rel_mem_record(record
);
2522 if (ip
->rsv_recs
> 1 && hmp
->rsv_recs
> hammer_limit_recs
)
2523 hammer_flush_inode(ip
, 0);
2525 bp
->b_bio2
.bio_offset
= NOOFFSET
;
2526 bp
->b_error
= error
;
2527 bp
->b_flags
|= B_ERROR
;
2530 hammer_cleanup_write_io(ip
);
2535 * Clean-up after disposing of a dirty frontend buffer's data.
2536 * This is somewhat heuristical so try to be robust.
2539 hammer_cleanup_write_io(hammer_inode_t ip
)
2541 if (ip
->rsv_databufs
) {
2543 --ip
->hmp
->rsv_databufs
;
2548 * We can lose track of dirty buffer cache buffers if we truncate, this
2549 * routine will resynchronize the count.
2553 hammer_update_rsv_databufs(hammer_inode_t ip
)
2561 RB_FOREACH(bp
, buf_rb_tree
, &ip
->vp
->v_rbdirty_tree
) {
2567 delta
= n
- ip
->rsv_databufs
;
2568 ip
->rsv_databufs
+= delta
;
2569 ip
->hmp
->rsv_databufs
+= delta
;
2573 * dounlink - disconnect a directory entry
2575 * XXX whiteout support not really in yet
2578 hammer_dounlink(hammer_transaction_t trans
, struct nchandle
*nch
,
2579 struct vnode
*dvp
, struct ucred
*cred
, int flags
)
2581 struct namecache
*ncp
;
2584 struct hammer_cursor cursor
;
2589 * Calculate the namekey and setup the key range for the scan. This
2590 * works kinda like a chained hash table where the lower 32 bits
2591 * of the namekey synthesize the chain.
2593 * The key range is inclusive of both key_beg and key_end.
2598 if (dip
->flags
& HAMMER_INODE_RO
)
2601 namekey
= hammer_directory_namekey(ncp
->nc_name
, ncp
->nc_nlen
);
2603 hammer_init_cursor(trans
, &cursor
, &dip
->cache
[1], dip
);
2604 cursor
.key_beg
.localization
= dip
->obj_localization
+
2605 HAMMER_LOCALIZE_MISC
;
2606 cursor
.key_beg
.obj_id
= dip
->obj_id
;
2607 cursor
.key_beg
.key
= namekey
;
2608 cursor
.key_beg
.create_tid
= 0;
2609 cursor
.key_beg
.delete_tid
= 0;
2610 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DIRENTRY
;
2611 cursor
.key_beg
.obj_type
= 0;
2613 cursor
.key_end
= cursor
.key_beg
;
2614 cursor
.key_end
.key
|= 0xFFFFFFFFULL
;
2615 cursor
.asof
= dip
->obj_asof
;
2616 cursor
.flags
|= HAMMER_CURSOR_END_INCLUSIVE
| HAMMER_CURSOR_ASOF
;
2619 * Scan all matching records (the chain), locate the one matching
2620 * the requested path component. info->last_error contains the
2621 * error code on search termination and could be 0, ENOENT, or
2624 * The hammer_ip_*() functions merge in-memory records with on-disk
2625 * records for the purposes of the search.
2627 error
= hammer_ip_first(&cursor
);
2629 while (error
== 0) {
2630 error
= hammer_ip_resolve_data(&cursor
);
2633 nlen
= cursor
.leaf
->data_len
- HAMMER_ENTRY_NAME_OFF
;
2635 if (ncp
->nc_nlen
== nlen
&&
2636 bcmp(ncp
->nc_name
, cursor
.data
->entry
.name
, nlen
) == 0) {
2639 error
= hammer_ip_next(&cursor
);
2643 * If all is ok we have to get the inode so we can adjust nlinks.
2644 * To avoid a deadlock with the flusher we must release the inode
2645 * lock on the directory when acquiring the inode for the entry.
2647 * If the target is a directory, it must be empty.
2650 hammer_unlock(&cursor
.ip
->lock
);
2651 ip
= hammer_get_inode(trans
, dip
, cursor
.data
->entry
.obj_id
,
2653 cursor
.data
->entry
.localization
,
2655 hammer_lock_sh(&cursor
.ip
->lock
);
2656 if (error
== ENOENT
) {
2657 kprintf("obj_id %016llx\n", cursor
.data
->entry
.obj_id
);
2658 Debugger("ENOENT unlinking object that should exist");
2662 * If we are trying to remove a directory the directory must
2665 * WARNING: hammer_ip_check_directory_empty() may have to
2666 * terminate the cursor to avoid a deadlock. It is ok to
2667 * call hammer_done_cursor() twice.
2669 if (error
== 0 && ip
->ino_data
.obj_type
==
2670 HAMMER_OBJTYPE_DIRECTORY
) {
2671 error
= hammer_ip_check_directory_empty(trans
, ip
);
2675 * Delete the directory entry.
2677 * WARNING: hammer_ip_del_directory() may have to terminate
2678 * the cursor to avoid a deadlock. It is ok to call
2679 * hammer_done_cursor() twice.
2682 error
= hammer_ip_del_directory(trans
, &cursor
,
2685 hammer_done_cursor(&cursor
);
2687 cache_setunresolved(nch
);
2688 cache_setvp(nch
, NULL
);
2691 cache_inval_vp(ip
->vp
, CINV_DESTROY
);
2694 hammer_rel_inode(ip
, 0);
2696 hammer_done_cursor(&cursor
);
2698 if (error
== EDEADLK
)
2704 /************************************************************************
2705 * FIFO AND SPECFS OPS *
2706 ************************************************************************
2711 hammer_vop_fifoclose (struct vop_close_args
*ap
)
2713 /* XXX update itimes */
2714 return (VOCALL(&fifo_vnode_vops
, &ap
->a_head
));
2718 hammer_vop_fiforead (struct vop_read_args
*ap
)
2722 error
= VOCALL(&fifo_vnode_vops
, &ap
->a_head
);
2723 /* XXX update access time */
2728 hammer_vop_fifowrite (struct vop_write_args
*ap
)
2732 error
= VOCALL(&fifo_vnode_vops
, &ap
->a_head
);
2733 /* XXX update access time */
2738 hammer_vop_specclose (struct vop_close_args
*ap
)
2740 /* XXX update itimes */
2741 return (VOCALL(&spec_vnode_vops
, &ap
->a_head
));
2745 hammer_vop_specread (struct vop_read_args
*ap
)
2747 /* XXX update access time */
2748 return (VOCALL(&spec_vnode_vops
, &ap
->a_head
));
2752 hammer_vop_specwrite (struct vop_write_args
*ap
)
2754 /* XXX update last change time */
2755 return (VOCALL(&spec_vnode_vops
, &ap
->a_head
));