| Commit | Line | Data |
|---|---|---|
| 427e5fc6 | 1 | /* |
| b84de5af | 2 | * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved. |
| 427e5fc6 MD |
3 | * |
| 4 | * This code is derived from software contributed to The DragonFly Project | |
| 5 | * by Matthew Dillon <dillon@backplane.com> | |
| 6 | * | |
| 7 | * Redistribution and use in source and binary forms, with or without | |
| 8 | * modification, are permitted provided that the following conditions | |
| 9 | * are met: | |
| 10 | * | |
| 11 | * 1. Redistributions of source code must retain the above copyright | |
| 12 | * notice, this list of conditions and the following disclaimer. | |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 14 | * notice, this list of conditions and the following disclaimer in | |
| 15 | * the documentation and/or other materials provided with the | |
| 16 | * distribution. | |
| 17 | * 3. Neither the name of The DragonFly Project nor the names of its | |
| 18 | * contributors may be used to endorse or promote products derived | |
| 19 | * from this software without specific, prior written permission. | |
| 20 | * | |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 22 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 24 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 25 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 26 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 27 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 28 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
| 29 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| 30 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 31 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 32 | * SUCH DAMAGE. | |
| 33 | * | |
| fbb84158 | 34 | * $DragonFly: src/sys/vfs/hammer/hammer_vnops.c,v 1.102 2008/10/16 17:24:16 dillon Exp $ |
| 427e5fc6 MD |
35 | */ |
| 36 | ||
| 37 | #include <sys/param.h> | |
| 38 | #include <sys/systm.h> | |
| 39 | #include <sys/kernel.h> | |
| 40 | #include <sys/fcntl.h> | |
| 41 | #include <sys/namecache.h> | |
| 42 | #include <sys/vnode.h> | |
| 43 | #include <sys/lockf.h> | |
| 44 | #include <sys/event.h> | |
| 45 | #include <sys/stat.h> | |
| b3deaf57 | 46 | #include <sys/dirent.h> |
| fbb84158 | 47 | #include <sys/file.h> |
| c0ade690 | 48 | #include <vm/vm_extern.h> |
| 18bee4a2 | 49 | #include <vm/swap_pager.h> |
| 7a04d74f | 50 | #include <vfs/fifofs/fifo.h> |
| 684a93c4 | 51 | |
| 427e5fc6 MD |
52 | #include "hammer.h" |
| 53 | ||
| 54 | /* | |
| 55 | * USERFS VNOPS | |
| 56 | */ | |
| 57 | /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/ | |
| 66325755 MD |
58 | static int hammer_vop_fsync(struct vop_fsync_args *); |
| 59 | static int hammer_vop_read(struct vop_read_args *); | |
| 60 | static int hammer_vop_write(struct vop_write_args *); | |
| 61 | static int hammer_vop_access(struct vop_access_args *); | |
| 62 | static int hammer_vop_advlock(struct vop_advlock_args *); | |
| 63 | static int hammer_vop_close(struct vop_close_args *); | |
| 64 | static int hammer_vop_ncreate(struct vop_ncreate_args *); | |
| 65 | static int hammer_vop_getattr(struct vop_getattr_args *); | |
| 66 | static int hammer_vop_nresolve(struct vop_nresolve_args *); | |
| 67 | static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *); | |
| 68 | static int hammer_vop_nlink(struct vop_nlink_args *); | |
| 69 | static int hammer_vop_nmkdir(struct vop_nmkdir_args *); | |
| 70 | static int hammer_vop_nmknod(struct vop_nmknod_args *); | |
| 71 | static int hammer_vop_open(struct vop_open_args *); | |
| 66325755 MD |
72 | static int hammer_vop_print(struct vop_print_args *); |
| 73 | static int hammer_vop_readdir(struct vop_readdir_args *); | |
| 74 | static int hammer_vop_readlink(struct vop_readlink_args *); | |
| 75 | static int hammer_vop_nremove(struct vop_nremove_args *); | |
| 76 | static int hammer_vop_nrename(struct vop_nrename_args *); | |
| 77 | static int hammer_vop_nrmdir(struct vop_nrmdir_args *); | |
| 349433c9 | 78 | static int hammer_vop_markatime(struct vop_markatime_args *); |
| 66325755 MD |
79 | static int hammer_vop_setattr(struct vop_setattr_args *); |
| 80 | static int hammer_vop_strategy(struct vop_strategy_args *); | |
| a99b9ea2 | 81 | static int hammer_vop_bmap(struct vop_bmap_args *ap); |
| 66325755 MD |
82 | static int hammer_vop_nsymlink(struct vop_nsymlink_args *); |
| 83 | static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *); | |
| 7dc57964 | 84 | static int hammer_vop_ioctl(struct vop_ioctl_args *); |
| 513ca7d7 | 85 | static int hammer_vop_mountctl(struct vop_mountctl_args *); |
| fbb84158 | 86 | static int hammer_vop_kqfilter (struct vop_kqfilter_args *); |
| 427e5fc6 | 87 | |
| 7a04d74f MD |
88 | static int hammer_vop_fifoclose (struct vop_close_args *); |
| 89 | static int hammer_vop_fiforead (struct vop_read_args *); | |
| 90 | static int hammer_vop_fifowrite (struct vop_write_args *); | |
| fbb84158 | 91 | static int hammer_vop_fifokqfilter (struct vop_kqfilter_args *); |
| 7a04d74f | 92 | |
| 427e5fc6 MD |
93 | struct vop_ops hammer_vnode_vops = { |
| 94 | .vop_default = vop_defaultop, | |
| 95 | .vop_fsync = hammer_vop_fsync, | |
| c0ade690 MD |
96 | .vop_getpages = vop_stdgetpages, |
| 97 | .vop_putpages = vop_stdputpages, | |
| 427e5fc6 MD |
98 | .vop_read = hammer_vop_read, |
| 99 | .vop_write = hammer_vop_write, | |
| 100 | .vop_access = hammer_vop_access, | |
| 101 | .vop_advlock = hammer_vop_advlock, | |
| 102 | .vop_close = hammer_vop_close, | |
| 103 | .vop_ncreate = hammer_vop_ncreate, | |
| 104 | .vop_getattr = hammer_vop_getattr, | |
| 105 | .vop_inactive = hammer_vop_inactive, | |
| 106 | .vop_reclaim = hammer_vop_reclaim, | |
| 107 | .vop_nresolve = hammer_vop_nresolve, | |
| 108 | .vop_nlookupdotdot = hammer_vop_nlookupdotdot, | |
| 109 | .vop_nlink = hammer_vop_nlink, | |
| 110 | .vop_nmkdir = hammer_vop_nmkdir, | |
| 111 | .vop_nmknod = hammer_vop_nmknod, | |
| 112 | .vop_open = hammer_vop_open, | |
| 64950f31 | 113 | .vop_pathconf = vop_stdpathconf, |
| 427e5fc6 MD |
114 | .vop_print = hammer_vop_print, |
| 115 | .vop_readdir = hammer_vop_readdir, | |
| 116 | .vop_readlink = hammer_vop_readlink, | |
| 117 | .vop_nremove = hammer_vop_nremove, | |
| 118 | .vop_nrename = hammer_vop_nrename, | |
| 119 | .vop_nrmdir = hammer_vop_nrmdir, | |
| 349433c9 | 120 | .vop_markatime = hammer_vop_markatime, |
| 427e5fc6 | 121 | .vop_setattr = hammer_vop_setattr, |
| a99b9ea2 | 122 | .vop_bmap = hammer_vop_bmap, |
| 427e5fc6 MD |
123 | .vop_strategy = hammer_vop_strategy, |
| 124 | .vop_nsymlink = hammer_vop_nsymlink, | |
| 7dc57964 | 125 | .vop_nwhiteout = hammer_vop_nwhiteout, |
| 513ca7d7 | 126 | .vop_ioctl = hammer_vop_ioctl, |
| fbb84158 MD |
127 | .vop_mountctl = hammer_vop_mountctl, |
| 128 | .vop_kqfilter = hammer_vop_kqfilter | |
| 427e5fc6 MD |
129 | }; |
| 130 | ||
| 7a04d74f | 131 | struct vop_ops hammer_spec_vops = { |
| 8be7edad | 132 | .vop_default = vop_defaultop, |
| 7a04d74f | 133 | .vop_fsync = hammer_vop_fsync, |
| 8be7edad MD |
134 | .vop_read = vop_stdnoread, |
| 135 | .vop_write = vop_stdnowrite, | |
| 7a04d74f | 136 | .vop_access = hammer_vop_access, |
| 8be7edad | 137 | .vop_close = hammer_vop_close, |
| 349433c9 | 138 | .vop_markatime = hammer_vop_markatime, |
| 8be7edad | 139 | .vop_getattr = hammer_vop_getattr, |
| 7a04d74f MD |
140 | .vop_inactive = hammer_vop_inactive, |
| 141 | .vop_reclaim = hammer_vop_reclaim, | |
| 142 | .vop_setattr = hammer_vop_setattr | |
| 143 | }; | |
| 144 | ||
| 145 | struct vop_ops hammer_fifo_vops = { | |
| 146 | .vop_default = fifo_vnoperate, | |
| 147 | .vop_fsync = hammer_vop_fsync, | |
| 148 | .vop_read = hammer_vop_fiforead, | |
| 149 | .vop_write = hammer_vop_fifowrite, | |
| 150 | .vop_access = hammer_vop_access, | |
| 151 | .vop_close = hammer_vop_fifoclose, | |
| 349433c9 | 152 | .vop_markatime = hammer_vop_markatime, |
| 7a04d74f MD |
153 | .vop_getattr = hammer_vop_getattr, |
| 154 | .vop_inactive = hammer_vop_inactive, | |
| 155 | .vop_reclaim = hammer_vop_reclaim, | |
| fbb84158 MD |
156 | .vop_setattr = hammer_vop_setattr, |
| 157 | .vop_kqfilter = hammer_vop_fifokqfilter | |
| 7a04d74f MD |
158 | }; |
| 159 | ||
| fbb84158 MD |
160 | static __inline |
| 161 | void | |
| 162 | hammer_knote(struct vnode *vp, int flags) | |
| 163 | { | |
| 164 | if (flags) | |
| 5b22f1a7 | 165 | KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags); |
| fbb84158 MD |
166 | } |
| 167 | ||
| 0832c9bb MD |
168 | #ifdef DEBUG_TRUNCATE |
| 169 | struct hammer_inode *HammerTruncIp; | |
| 170 | #endif | |
| 171 | ||
| b84de5af | 172 | static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch, |
| d7e278bb MD |
173 | struct vnode *dvp, struct ucred *cred, |
| 174 | int flags, int isdir); | |
| 8cd0a023 MD |
175 | static int hammer_vop_strategy_read(struct vop_strategy_args *ap); |
| 176 | static int hammer_vop_strategy_write(struct vop_strategy_args *ap); | |
| 177 | ||
| 427e5fc6 MD |
178 | #if 0 |
| 179 | static | |
| 180 | int | |
| 181 | hammer_vop_vnoperate(struct vop_generic_args *) | |
| 182 | { | |
| 183 | return (VOCALL(&hammer_vnode_vops, ap)); | |
| 184 | } | |
| 185 | #endif | |
| 186 | ||
| 66325755 MD |
187 | /* |
| 188 | * hammer_vop_fsync { vp, waitfor } | |
| ddfdf542 MD |
189 | * |
| 190 | * fsync() an inode to disk and wait for it to be completely committed | |
| 191 | * such that the information would not be undone if a crash occured after | |
| 192 | * return. | |
| 6f3d87c0 MD |
193 | * |
| 194 | * NOTE: HAMMER's fsync()'s are going to remain expensive until we implement | |
| 195 | * a REDO log. A sysctl is provided to relax HAMMER's fsync() | |
| 196 | * operation. | |
| 197 | * | |
| 198 | * Ultimately the combination of a REDO log and use of fast storage | |
| 199 | * to front-end cluster caches will make fsync fast, but it aint | |
| 200 | * here yet. And, in anycase, we need real transactional | |
| 201 | * all-or-nothing features which are not restricted to a single file. | |
| 66325755 | 202 | */ |
| 427e5fc6 MD |
203 | static |
| 204 | int | |
| 66325755 | 205 | hammer_vop_fsync(struct vop_fsync_args *ap) |
| 427e5fc6 | 206 | { |
| b84de5af | 207 | hammer_inode_t ip = VTOI(ap->a_vp); |
| 9192654c | 208 | hammer_mount_t hmp = ip->hmp; |
| 6f3d87c0 | 209 | int waitfor = ap->a_waitfor; |
| 9192654c | 210 | int mode; |
| 6f3d87c0 | 211 | |
| b0aab9b9 MD |
212 | lwkt_gettoken(&hmp->fs_token); |
| 213 | ||
| 6f3d87c0 | 214 | /* |
| 9192654c MD |
215 | * Fsync rule relaxation (default is either full synchronous flush |
| 216 | * or REDO semantics with synchronous flush). | |
| 6f3d87c0 MD |
217 | */ |
| 218 | if (ap->a_flags & VOP_FSYNC_SYSCALL) { | |
| 219 | switch(hammer_fsync_mode) { | |
| 220 | case 0: | |
| 9192654c | 221 | mode0: |
| 47f363f1 | 222 | /* no REDO, full synchronous flush */ |
| 9192654c | 223 | goto skip; |
| 6f3d87c0 | 224 | case 1: |
| 9192654c | 225 | mode1: |
| 47f363f1 | 226 | /* no REDO, full asynchronous flush */ |
| 6f3d87c0 MD |
227 | if (waitfor == MNT_WAIT) |
| 228 | waitfor = MNT_NOWAIT; | |
| 9192654c | 229 | goto skip; |
| 6f3d87c0 | 230 | case 2: |
| 9192654c MD |
231 | /* REDO semantics, synchronous flush */ |
| 232 | if (hmp->version < HAMMER_VOL_VERSION_FOUR) | |
| 233 | goto mode0; | |
| 234 | mode = HAMMER_FLUSH_UNDOS_AUTO; | |
| 235 | break; | |
| 6f3d87c0 | 236 | case 3: |
| 9192654c MD |
237 | /* REDO semantics, relaxed asynchronous flush */ |
| 238 | if (hmp->version < HAMMER_VOL_VERSION_FOUR) | |
| 239 | goto mode1; | |
| 240 | mode = HAMMER_FLUSH_UNDOS_RELAXED; | |
| 241 | if (waitfor == MNT_WAIT) | |
| 242 | waitfor = MNT_NOWAIT; | |
| 243 | break; | |
| 244 | case 4: | |
| 245 | /* ignore the fsync() system call */ | |
| b0aab9b9 | 246 | lwkt_reltoken(&hmp->fs_token); |
| 6f3d87c0 MD |
247 | return(0); |
| 248 | default: | |
| 9192654c MD |
249 | /* we have to do something */ |
| 250 | mode = HAMMER_FLUSH_UNDOS_RELAXED; | |
| 251 | if (waitfor == MNT_WAIT) | |
| 252 | waitfor = MNT_NOWAIT; | |
| 253 | break; | |
| 254 | } | |
| 255 | ||
| 256 | /* | |
| 47f363f1 MD |
257 | * Fast fsync only needs to flush the UNDO/REDO fifo if |
| 258 | * HAMMER_INODE_REDO is non-zero and the only modifications | |
| 259 | * made to the file are write or write-extends. | |
| 9192654c | 260 | */ |
| 47f363f1 | 261 | if ((ip->flags & HAMMER_INODE_REDO) && |
| 9192654c MD |
262 | (ip->flags & HAMMER_INODE_MODMASK_NOREDO) == 0 |
| 263 | ) { | |
| 264 | ++hammer_count_fsyncs; | |
| 265 | hammer_flusher_flush_undos(hmp, mode); | |
| 266 | ip->redo_count = 0; | |
| b0aab9b9 | 267 | lwkt_reltoken(&hmp->fs_token); |
| 6f3d87c0 MD |
268 | return(0); |
| 269 | } | |
| 47f363f1 MD |
270 | |
| 271 | /* | |
| 272 | * REDO is enabled by fsync(), the idea being we really only | |
| 273 | * want to lay down REDO records when programs are using | |
| 274 | * fsync() heavily. The first fsync() on the file starts | |
| 275 | * the gravy train going and later fsync()s keep it hot by | |
| 276 | * resetting the redo_count. | |
| 277 | * | |
| 278 | * We weren't running REDOs before now so we have to fall | |
| 279 | * through and do a full fsync of what we have. | |
| 280 | */ | |
| c58123da MD |
281 | if (hmp->version >= HAMMER_VOL_VERSION_FOUR && |
| 282 | (hmp->flags & HAMMER_MOUNT_REDO_RECOVERY_RUN) == 0) { | |
| 47f363f1 MD |
283 | ip->flags |= HAMMER_INODE_REDO; |
| 284 | ip->redo_count = 0; | |
| 285 | } | |
| 6f3d87c0 | 286 | } |
| 9192654c | 287 | skip: |
| c0ade690 | 288 | |
| 6f3d87c0 | 289 | /* |
| 9192654c | 290 | * Do a full flush sequence. |
| aad00981 MD |
291 | * |
| 292 | * Attempt to release the vnode while waiting for the inode to | |
| 293 | * finish flushing. This can really mess up inactive->reclaim | |
| 294 | * sequences so only do it if the vnode is active. | |
| 6f3d87c0 | 295 | */ |
| 7a61b85d | 296 | ++hammer_count_fsyncs; |
| 6f3d87c0 | 297 | vfsync(ap->a_vp, waitfor, 1, NULL, NULL); |
| af209b0f | 298 | hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL); |
| 6f3d87c0 | 299 | if (waitfor == MNT_WAIT) { |
| aad00981 MD |
300 | if ((ap->a_vp->v_flag & VINACTIVE) == 0) |
| 301 | vn_unlock(ap->a_vp); | |
| b84de5af | 302 | hammer_wait_inode(ip); |
| aad00981 MD |
303 | if ((ap->a_vp->v_flag & VINACTIVE) == 0) |
| 304 | vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY); | |
| b424ca30 | 305 | } |
| b0aab9b9 | 306 | lwkt_reltoken(&hmp->fs_token); |
| 059819e3 | 307 | return (ip->error); |
| 427e5fc6 MD |
308 | } |
| 309 | ||
| 66325755 MD |
310 | /* |
| 311 | * hammer_vop_read { vp, uio, ioflag, cred } | |
| 42cd5131 | 312 | * |
| b0aab9b9 | 313 | * MPSAFE (for the cache safe does not require fs_token) |
| 66325755 | 314 | */ |
| 427e5fc6 MD |
315 | static |
| 316 | int | |
| 66325755 | 317 | hammer_vop_read(struct vop_read_args *ap) |
| 427e5fc6 | 318 | { |
| 66325755 | 319 | struct hammer_transaction trans; |
| c0ade690 | 320 | hammer_inode_t ip; |
| b0aab9b9 | 321 | hammer_mount_t hmp; |
| 66325755 MD |
322 | off_t offset; |
| 323 | struct buf *bp; | |
| 324 | struct uio *uio; | |
| 325 | int error; | |
| 326 | int n; | |
| 8cd0a023 | 327 | int seqcount; |
| 4a2796f3 MD |
328 | int ioseqcount; |
| 329 | int blksize; | |
| f864373f | 330 | int bigread; |
| b0aab9b9 | 331 | int got_fstoken; |
| 66325755 MD |
332 | |
| 333 | if (ap->a_vp->v_type != VREG) | |
| 334 | return (EINVAL); | |
| 335 | ip = VTOI(ap->a_vp); | |
| b0aab9b9 | 336 | hmp = ip->hmp; |
| 66325755 | 337 | error = 0; |
| 4a2796f3 MD |
338 | uio = ap->a_uio; |
| 339 | ||
| 340 | /* | |
| 341 | * Allow the UIO's size to override the sequential heuristic. | |
| 342 | */ | |
| 343 | blksize = hammer_blocksize(uio->uio_offset); | |
| 69adbed4 MD |
344 | seqcount = (uio->uio_resid + (BKVASIZE - 1)) / BKVASIZE; |
| 345 | ioseqcount = (ap->a_ioflag >> 16); | |
| 4a2796f3 MD |
346 | if (seqcount < ioseqcount) |
| 347 | seqcount = ioseqcount; | |
| 66325755 | 348 | |
| 7ff770b4 | 349 | /* |
| f864373f MD |
350 | * If reading or writing a huge amount of data we have to break |
| 351 | * atomicy and allow the operation to be interrupted by a signal | |
| 352 | * or it can DOS the machine. | |
| 353 | */ | |
| 354 | bigread = (uio->uio_resid > 100 * 1024 * 1024); | |
| b0aab9b9 | 355 | got_fstoken = 0; |
| f864373f MD |
356 | |
| 357 | /* | |
| 4a2796f3 MD |
358 | * Access the data typically in HAMMER_BUFSIZE blocks via the |
| 359 | * buffer cache, but HAMMER may use a variable block size based | |
| 360 | * on the offset. | |
| 42cd5131 MD |
361 | * |
| 362 | * XXX Temporary hack, delay the start transaction while we remain | |
| 363 | * MPSAFE. NOTE: ino_data.size cannot change while vnode is | |
| 364 | * locked-shared. | |
| 66325755 | 365 | */ |
| 11ad5ade | 366 | while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) { |
| 4a2796f3 MD |
367 | int64_t base_offset; |
| 368 | int64_t file_limit; | |
| 369 | ||
| 370 | blksize = hammer_blocksize(uio->uio_offset); | |
| 371 | offset = (int)uio->uio_offset & (blksize - 1); | |
| 372 | base_offset = uio->uio_offset - offset; | |
| 373 | ||
| f864373f MD |
374 | if (bigread && (error = hammer_signal_check(ip->hmp)) != 0) |
| 375 | break; | |
| 376 | ||
| 42cd5131 MD |
377 | /* |
| 378 | * MPSAFE | |
| 379 | */ | |
| 54341a3b MD |
380 | bp = getblk(ap->a_vp, base_offset, blksize, 0, 0); |
| 381 | if ((bp->b_flags & (B_INVAL | B_CACHE | B_RAM)) == B_CACHE) { | |
| 382 | bp->b_flags &= ~B_AGE; | |
| 42cd5131 MD |
383 | error = 0; |
| 384 | goto skip; | |
| 54341a3b MD |
385 | } |
| 386 | if (ap->a_ioflag & IO_NRDELAY) { | |
| 387 | bqrelse(bp); | |
| 388 | return (EWOULDBLOCK); | |
| 42cd5131 MD |
389 | } |
| 390 | ||
| 391 | /* | |
| 392 | * MPUNSAFE | |
| 393 | */ | |
| b0aab9b9 MD |
394 | if (got_fstoken == 0) { |
| 395 | lwkt_gettoken(&hmp->fs_token); | |
| 396 | got_fstoken = 1; | |
| 42cd5131 MD |
397 | hammer_start_transaction(&trans, ip->hmp); |
| 398 | } | |
| 399 | ||
| 54341a3b MD |
400 | /* |
| 401 | * NOTE: A valid bp has already been acquired, but was not | |
| 402 | * B_CACHE. | |
| 403 | */ | |
| 1b0ab2c3 | 404 | if (hammer_cluster_enable) { |
| 4a2796f3 MD |
405 | /* |
| 406 | * Use file_limit to prevent cluster_read() from | |
| 407 | * creating buffers of the wrong block size past | |
| 408 | * the demarc. | |
| 409 | */ | |
| 410 | file_limit = ip->ino_data.size; | |
| 411 | if (base_offset < HAMMER_XDEMARC && | |
| 412 | file_limit > HAMMER_XDEMARC) { | |
| 413 | file_limit = HAMMER_XDEMARC; | |
| 414 | } | |
| 54341a3b | 415 | error = cluster_readx(ap->a_vp, |
| 4a2796f3 | 416 | file_limit, base_offset, |
| 364c022c MD |
417 | blksize, uio->uio_resid, |
| 418 | seqcount * BKVASIZE, &bp); | |
| a99b9ea2 | 419 | } else { |
| 54341a3b MD |
420 | error = breadnx(ap->a_vp, base_offset, blksize, |
| 421 | NULL, NULL, 0, &bp); | |
| a99b9ea2 | 422 | } |
| 66325755 MD |
423 | if (error) { |
| 424 | brelse(bp); | |
| 425 | break; | |
| 426 | } | |
| 42cd5131 | 427 | skip: |
| 24c8374a MD |
428 | if ((hammer_debug_io & 0x0001) && (bp->b_flags & B_IODEBUG)) { |
| 429 | kprintf("doff %016jx read file %016jx@%016jx\n", | |
| 430 | (intmax_t)bp->b_bio2.bio_offset, | |
| 431 | (intmax_t)ip->obj_id, | |
| 432 | (intmax_t)bp->b_loffset); | |
| 433 | } | |
| 434 | bp->b_flags &= ~B_IODEBUG; | |
| 66030e2b MD |
435 | if (blksize == HAMMER_XBUFSIZE) |
| 436 | bp->b_flags |= B_CLUSTEROK; | |
| 7bc5b8c2 | 437 | |
| 4a2796f3 | 438 | n = blksize - offset; |
| 66325755 MD |
439 | if (n > uio->uio_resid) |
| 440 | n = uio->uio_resid; | |
| 11ad5ade MD |
441 | if (n > ip->ino_data.size - uio->uio_offset) |
| 442 | n = (int)(ip->ino_data.size - uio->uio_offset); | |
| df01a101 MD |
443 | if (got_fstoken) |
| 444 | lwkt_reltoken(&hmp->fs_token); | |
| 283b9448 MD |
445 | |
| 446 | /* | |
| 447 | * Set B_AGE, data has a lower priority than meta-data. | |
| 448 | * | |
| 449 | * Use a hold/unlock/drop sequence to run the uiomove | |
| 450 | * with the buffer unlocked, avoiding deadlocks against | |
| 451 | * read()s on mmap()'d spaces. | |
| 452 | */ | |
| 453 | bp->b_flags |= B_AGE; | |
| 454 | bqhold(bp); | |
| 455 | bqrelse(bp); | |
| 66325755 | 456 | error = uiomove((char *)bp->b_data + offset, n, uio); |
| 283b9448 MD |
457 | bqdrop(bp); |
| 458 | ||
| df01a101 MD |
459 | if (got_fstoken) |
| 460 | lwkt_gettoken(&hmp->fs_token); | |
| 7bc5b8c2 | 461 | |
| af209b0f MD |
462 | if (error) |
| 463 | break; | |
| ce0138a6 | 464 | hammer_stats_file_read += n; |
| 66325755 | 465 | } |
| 42cd5131 MD |
466 | |
| 467 | /* | |
| 70125e78 MD |
468 | * Try to update the atime with just the inode lock for maximum |
| 469 | * concurrency. If we can't shortcut it we have to get the full | |
| 470 | * blown transaction. | |
| 42cd5131 | 471 | */ |
| 70125e78 MD |
472 | if (got_fstoken == 0 && hammer_update_atime_quick(ip) < 0) { |
| 473 | lwkt_gettoken(&hmp->fs_token); | |
| 474 | got_fstoken = 1; | |
| 475 | hammer_start_transaction(&trans, ip->hmp); | |
| 476 | } | |
| 477 | ||
| b0aab9b9 | 478 | if (got_fstoken) { |
| 42cd5131 MD |
479 | if ((ip->flags & HAMMER_INODE_RO) == 0 && |
| 480 | (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) { | |
| 481 | ip->ino_data.atime = trans.time; | |
| e98f1b96 | 482 | hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME); |
| 42cd5131 MD |
483 | } |
| 484 | hammer_done_transaction(&trans); | |
| b0aab9b9 | 485 | lwkt_reltoken(&hmp->fs_token); |
| b84de5af | 486 | } |
| 66325755 | 487 | return (error); |
| 427e5fc6 MD |
488 | } |
| 489 | ||
| 66325755 MD |
490 | /* |
| 491 | * hammer_vop_write { vp, uio, ioflag, cred } | |
| 492 | */ | |
| 427e5fc6 MD |
493 | static |
| 494 | int | |
| 66325755 | 495 | hammer_vop_write(struct vop_write_args *ap) |
| 427e5fc6 | 496 | { |
| 66325755 MD |
497 | struct hammer_transaction trans; |
| 498 | struct hammer_inode *ip; | |
| 4a2796f3 | 499 | hammer_mount_t hmp; |
| 1589191a | 500 | thread_t td; |
| 66325755 | 501 | struct uio *uio; |
| 4a2796f3 | 502 | int offset; |
| 47637bff | 503 | off_t base_offset; |
| 9de13b88 | 504 | int64_t cluster_eof; |
| 66325755 | 505 | struct buf *bp; |
| fbb84158 | 506 | int kflags; |
| 66325755 MD |
507 | int error; |
| 508 | int n; | |
| c0ade690 | 509 | int flags; |
| cb51be26 | 510 | int seqcount; |
| f864373f | 511 | int bigwrite; |
| 66325755 MD |
512 | |
| 513 | if (ap->a_vp->v_type != VREG) | |
| 514 | return (EINVAL); | |
| 515 | ip = VTOI(ap->a_vp); | |
| 4a2796f3 | 516 | hmp = ip->hmp; |
| 66325755 | 517 | error = 0; |
| fbb84158 | 518 | kflags = 0; |
| cb51be26 | 519 | seqcount = ap->a_ioflag >> 16; |
| 66325755 | 520 | |
| d113fda1 MD |
521 | if (ip->flags & HAMMER_INODE_RO) |
| 522 | return (EROFS); | |
| 523 | ||
| 66325755 MD |
524 | /* |
| 525 | * Create a transaction to cover the operations we perform. | |
| 526 | */ | |
| b0aab9b9 | 527 | lwkt_gettoken(&hmp->fs_token); |
| 4a2796f3 | 528 | hammer_start_transaction(&trans, hmp); |
| 66325755 MD |
529 | uio = ap->a_uio; |
| 530 | ||
| 531 | /* | |
| 532 | * Check append mode | |
| 533 | */ | |
| 534 | if (ap->a_ioflag & IO_APPEND) | |
| 11ad5ade | 535 | uio->uio_offset = ip->ino_data.size; |
| 66325755 MD |
536 | |
| 537 | /* | |
| af209b0f MD |
538 | * Check for illegal write offsets. Valid range is 0...2^63-1. |
| 539 | * | |
| 540 | * NOTE: the base_off assignment is required to work around what | |
| 541 | * I consider to be a GCC-4 optimization bug. | |
| 66325755 | 542 | */ |
| af209b0f MD |
543 | if (uio->uio_offset < 0) { |
| 544 | hammer_done_transaction(&trans); | |
| b0aab9b9 | 545 | lwkt_reltoken(&hmp->fs_token); |
| af209b0f MD |
546 | return (EFBIG); |
| 547 | } | |
| 548 | base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */ | |
| e54488bb | 549 | if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) { |
| b84de5af | 550 | hammer_done_transaction(&trans); |
| b0aab9b9 | 551 | lwkt_reltoken(&hmp->fs_token); |
| 66325755 | 552 | return (EFBIG); |
| 9c448776 | 553 | } |
| 66325755 | 554 | |
| 1589191a MD |
555 | if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc && |
| 556 | base_offset > td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) { | |
| 557 | hammer_done_transaction(&trans); | |
| 558 | lwkt_reltoken(&hmp->fs_token); | |
| 559 | lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ); | |
| 560 | return (EFBIG); | |
| 561 | } | |
| 562 | ||
| 66325755 | 563 | /* |
| f864373f MD |
564 | * If reading or writing a huge amount of data we have to break |
| 565 | * atomicy and allow the operation to be interrupted by a signal | |
| 566 | * or it can DOS the machine. | |
| 9192654c | 567 | * |
| 47f363f1 MD |
568 | * Preset redo_count so we stop generating REDOs earlier if the |
| 569 | * limit is exceeded. | |
| f864373f MD |
570 | */ |
| 571 | bigwrite = (uio->uio_resid > 100 * 1024 * 1024); | |
| 47f363f1 MD |
572 | if ((ip->flags & HAMMER_INODE_REDO) && |
| 573 | ip->redo_count < hammer_limit_redo) { | |
| 9192654c | 574 | ip->redo_count += uio->uio_resid; |
| 47f363f1 | 575 | } |
| f864373f MD |
576 | |
| 577 | /* | |
| 4a2796f3 MD |
578 | * Access the data typically in HAMMER_BUFSIZE blocks via the |
| 579 | * buffer cache, but HAMMER may use a variable block size based | |
| 580 | * on the offset. | |
| 66325755 MD |
581 | */ |
| 582 | while (uio->uio_resid > 0) { | |
| d5ef456e | 583 | int fixsize = 0; |
| 4a2796f3 MD |
584 | int blksize; |
| 585 | int blkmask; | |
| 6362a262 | 586 | int trivial; |
| d1eff1f7 | 587 | int endofblk; |
| 6362a262 | 588 | off_t nsize; |
| d5ef456e | 589 | |
| 93291532 | 590 | if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0) |
| e63644f0 | 591 | break; |
| f864373f MD |
592 | if (bigwrite && (error = hammer_signal_check(hmp)) != 0) |
| 593 | break; | |
| e63644f0 | 594 | |
| a9d52b76 MD |
595 | blksize = hammer_blocksize(uio->uio_offset); |
| 596 | ||
| 059819e3 | 597 | /* |
| 4a2796f3 MD |
598 | * Do not allow HAMMER to blow out the buffer cache. Very |
| 599 | * large UIOs can lockout other processes due to bwillwrite() | |
| 600 | * mechanics. | |
| 47637bff | 601 | * |
| df301614 MD |
602 | * The hammer inode is not locked during these operations. |
| 603 | * The vnode is locked which can interfere with the pageout | |
| 604 | * daemon for non-UIO_NOCOPY writes but should not interfere | |
| 605 | * with the buffer cache. Even so, we cannot afford to | |
| 606 | * allow the pageout daemon to build up too many dirty buffer | |
| 607 | * cache buffers. | |
| cb63d1bc MD |
608 | * |
| 609 | * Only call this if we aren't being recursively called from | |
| 610 | * a virtual disk device (vn), else we may deadlock. | |
| df301614 | 611 | */ |
| cb63d1bc MD |
612 | if ((ap->a_ioflag & IO_RECURSE) == 0) |
| 613 | bwillwrite(blksize); | |
| df301614 MD |
614 | |
| 615 | /* | |
| de996e86 MD |
616 | * Control the number of pending records associated with |
| 617 | * this inode. If too many have accumulated start a | |
| 618 | * flush. Try to maintain a pipeline with the flusher. | |
| a117fbeb MD |
619 | * |
| 620 | * NOTE: It is possible for other sources to grow the | |
| 621 | * records but not necessarily issue another flush, | |
| 622 | * so use a timeout and ensure that a re-flush occurs. | |
| de996e86 MD |
623 | */ |
| 624 | if (ip->rsv_recs >= hammer_limit_inode_recs) { | |
| 625 | hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL); | |
| a117fbeb MD |
626 | while (ip->rsv_recs >= hammer_limit_inode_recs * 2) { |
| 627 | ip->flags |= HAMMER_INODE_RECSW; | |
| de996e86 | 628 | tsleep(&ip->rsv_recs, 0, "hmrwww", hz); |
| a117fbeb | 629 | hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL); |
| de996e86 | 630 | } |
| de996e86 MD |
631 | } |
| 632 | ||
| 633 | #if 0 | |
| 634 | /* | |
| e4a5ff06 MD |
635 | * Do not allow HAMMER to blow out system memory by |
| 636 | * accumulating too many records. Records are so well | |
| 637 | * decoupled from the buffer cache that it is possible | |
| 638 | * for userland to push data out to the media via | |
| 639 | * direct-write, but build up the records queued to the | |
| 640 | * backend faster then the backend can flush them out. | |
| 641 | * HAMMER has hit its write limit but the frontend has | |
| 642 | * no pushback to slow it down. | |
| 059819e3 | 643 | */ |
| df301614 | 644 | if (hmp->rsv_recs > hammer_limit_recs / 2) { |
| 4a2796f3 | 645 | /* |
| df301614 | 646 | * Get the inode on the flush list |
| 4a2796f3 | 647 | */ |
| df301614 MD |
648 | if (ip->rsv_recs >= 64) |
| 649 | hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL); | |
| 650 | else if (ip->rsv_recs >= 16) | |
| 651 | hammer_flush_inode(ip, 0); | |
| 4a2796f3 MD |
652 | |
| 653 | /* | |
| df301614 MD |
654 | * Keep the flusher going if the system keeps |
| 655 | * queueing records. | |
| 4a2796f3 | 656 | */ |
| df301614 MD |
657 | delta = hmp->count_newrecords - |
| 658 | hmp->last_newrecords; | |
| 659 | if (delta < 0 || delta > hammer_limit_recs / 2) { | |
| 660 | hmp->last_newrecords = hmp->count_newrecords; | |
| 661 | hammer_sync_hmp(hmp, MNT_NOWAIT); | |
| 4a2796f3 MD |
662 | } |
| 663 | ||
| df301614 MD |
664 | /* |
| 665 | * If we have gotten behind start slowing | |
| 666 | * down the writers. | |
| 667 | */ | |
| 668 | delta = (hmp->rsv_recs - hammer_limit_recs) * | |
| 669 | hz / hammer_limit_recs; | |
| 670 | if (delta > 0) | |
| 671 | tsleep(&trans, 0, "hmrslo", delta); | |
| 059819e3 | 672 | } |
| de996e86 | 673 | #endif |
| 059819e3 | 674 | |
| 4a2796f3 MD |
675 | /* |
| 676 | * Calculate the blocksize at the current offset and figure | |
| 677 | * out how much we can actually write. | |
| 678 | */ | |
| 4a2796f3 MD |
679 | blkmask = blksize - 1; |
| 680 | offset = (int)uio->uio_offset & blkmask; | |
| 681 | base_offset = uio->uio_offset & ~(int64_t)blkmask; | |
| 682 | n = blksize - offset; | |
| d1eff1f7 | 683 | if (n > uio->uio_resid) { |
| d5ef456e | 684 | n = uio->uio_resid; |
| d1eff1f7 MD |
685 | endofblk = 0; |
| 686 | } else { | |
| 687 | endofblk = 1; | |
| 688 | } | |
| 6362a262 MD |
689 | nsize = uio->uio_offset + n; |
| 690 | if (nsize > ip->ino_data.size) { | |
| 691 | if (uio->uio_offset > ip->ino_data.size) | |
| 692 | trivial = 0; | |
| 693 | else | |
| 694 | trivial = 1; | |
| 695 | nvextendbuf(ap->a_vp, | |
| 696 | ip->ino_data.size, | |
| 697 | nsize, | |
| 698 | hammer_blocksize(ip->ino_data.size), | |
| 699 | hammer_blocksize(nsize), | |
| 700 | hammer_blockoff(ip->ino_data.size), | |
| 701 | hammer_blockoff(nsize), | |
| 702 | trivial); | |
| d5ef456e | 703 | fixsize = 1; |
| fbb84158 | 704 | kflags |= NOTE_EXTEND; |
| d5ef456e MD |
705 | } |
| 706 | ||
| c0ade690 MD |
707 | if (uio->uio_segflg == UIO_NOCOPY) { |
| 708 | /* | |
| 709 | * Issuing a write with the same data backing the | |
| 710 | * buffer. Instantiate the buffer to collect the | |
| 711 | * backing vm pages, then read-in any missing bits. | |
| 712 | * | |
| 713 | * This case is used by vop_stdputpages(). | |
| 714 | */ | |
| 47637bff | 715 | bp = getblk(ap->a_vp, base_offset, |
| 4a2796f3 | 716 | blksize, GETBLK_BHEAVY, 0); |
| c0ade690 MD |
717 | if ((bp->b_flags & B_CACHE) == 0) { |
| 718 | bqrelse(bp); | |
| 47637bff | 719 | error = bread(ap->a_vp, base_offset, |
| 4a2796f3 | 720 | blksize, &bp); |
| c0ade690 | 721 | } |
| 4a2796f3 | 722 | } else if (offset == 0 && uio->uio_resid >= blksize) { |
| c0ade690 | 723 | /* |
| a5fddc16 MD |
724 | * Even though we are entirely overwriting the buffer |
| 725 | * we may still have to zero it out to avoid a | |
| 726 | * mmap/write visibility issue. | |
| c0ade690 | 727 | */ |
| 4a2796f3 | 728 | bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0); |
| a5fddc16 MD |
729 | if ((bp->b_flags & B_CACHE) == 0) |
| 730 | vfs_bio_clrbuf(bp); | |
| 47637bff | 731 | } else if (base_offset >= ip->ino_data.size) { |
| c0ade690 | 732 | /* |
| a5fddc16 MD |
733 | * If the base offset of the buffer is beyond the |
| 734 | * file EOF, we don't have to issue a read. | |
| c0ade690 | 735 | */ |
| 47637bff | 736 | bp = getblk(ap->a_vp, base_offset, |
| 4a2796f3 | 737 | blksize, GETBLK_BHEAVY, 0); |
| 66325755 MD |
738 | vfs_bio_clrbuf(bp); |
| 739 | } else { | |
| c0ade690 MD |
740 | /* |
| 741 | * Partial overwrite, read in any missing bits then | |
| 742 | * replace the portion being written. | |
| 743 | */ | |
| 4a2796f3 | 744 | error = bread(ap->a_vp, base_offset, blksize, &bp); |
| d5ef456e MD |
745 | if (error == 0) |
| 746 | bheavy(bp); | |
| 66325755 | 747 | } |
| df01a101 MD |
748 | if (error == 0) { |
| 749 | lwkt_reltoken(&hmp->fs_token); | |
| 9192654c | 750 | error = uiomove(bp->b_data + offset, n, uio); |
| df01a101 MD |
751 | lwkt_gettoken(&hmp->fs_token); |
| 752 | } | |
| 9192654c MD |
753 | |
| 754 | /* | |
| 47f363f1 MD |
755 | * Generate REDO records if enabled and redo_count will not |
| 756 | * exceeded the limit. | |
| 757 | * | |
| 758 | * If redo_count exceeds the limit we stop generating records | |
| 759 | * and clear HAMMER_INODE_REDO. This will cause the next | |
| 760 | * fsync() to do a full meta-data sync instead of just an | |
| 761 | * UNDO/REDO fifo update. | |
| 762 | * | |
| 763 | * When clearing HAMMER_INODE_REDO any pre-existing REDOs | |
| 764 | * will still be tracked. The tracks will be terminated | |
| 765 | * when the related meta-data (including possible data | |
| 766 | * modifications which are not tracked via REDO) is | |
| 767 | * flushed. | |
| 9192654c | 768 | */ |
| 47f363f1 MD |
769 | if ((ip->flags & HAMMER_INODE_REDO) && error == 0) { |
| 770 | if (ip->redo_count < hammer_limit_redo) { | |
| 771 | bp->b_flags |= B_VFSFLAG1; | |
| 772 | error = hammer_generate_redo(&trans, ip, | |
| 9192654c | 773 | base_offset + offset, |
| 47f363f1 | 774 | HAMMER_REDO_WRITE, |
| 9192654c MD |
775 | bp->b_data + offset, |
| 776 | (size_t)n); | |
| 47f363f1 MD |
777 | } else { |
| 778 | ip->flags &= ~HAMMER_INODE_REDO; | |
| 779 | } | |
| 47637bff | 780 | } |
| d5ef456e MD |
781 | |
| 782 | /* | |
| 783 | * If we screwed up we have to undo any VM size changes we | |
| 784 | * made. | |
| 785 | */ | |
| 66325755 MD |
786 | if (error) { |
| 787 | brelse(bp); | |
| d5ef456e | 788 | if (fixsize) { |
| 6362a262 MD |
789 | nvtruncbuf(ap->a_vp, ip->ino_data.size, |
| 790 | hammer_blocksize(ip->ino_data.size), | |
| 753df37e MD |
791 | hammer_blockoff(ip->ino_data.size), |
| 792 | 0); | |
| d5ef456e | 793 | } |
| 66325755 MD |
794 | break; |
| 795 | } | |
| fbb84158 | 796 | kflags |= NOTE_WRITE; |
| ce0138a6 | 797 | hammer_stats_file_write += n; |
| 66030e2b MD |
798 | if (blksize == HAMMER_XBUFSIZE) |
| 799 | bp->b_flags |= B_CLUSTEROK; | |
| 11ad5ade MD |
800 | if (ip->ino_data.size < uio->uio_offset) { |
| 801 | ip->ino_data.size = uio->uio_offset; | |
| 9192654c | 802 | flags = HAMMER_INODE_SDIRTY; |
| c0ade690 | 803 | } else { |
| d113fda1 | 804 | flags = 0; |
| 66325755 | 805 | } |
| 11ad5ade | 806 | ip->ino_data.mtime = trans.time; |
| ddfdf542 | 807 | flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS; |
| e98f1b96 | 808 | hammer_modify_inode(&trans, ip, flags); |
| 32c90105 | 809 | |
| 0832c9bb | 810 | /* |
| 1b0ab2c3 MD |
811 | * Once we dirty the buffer any cached zone-X offset |
| 812 | * becomes invalid. HAMMER NOTE: no-history mode cannot | |
| 813 | * allow overwriting over the same data sector unless | |
| 814 | * we provide UNDOs for the old data, which we don't. | |
| 815 | */ | |
| 816 | bp->b_bio2.bio_offset = NOOFFSET; | |
| 817 | ||
| 818 | /* | |
| 47637bff | 819 | * Final buffer disposition. |
| de996e86 MD |
820 | * |
| 821 | * Because meta-data updates are deferred, HAMMER is | |
| 822 | * especially sensitive to excessive bdwrite()s because | |
| 823 | * the I/O stream is not broken up by disk reads. So the | |
| 824 | * buffer cache simply cannot keep up. | |
| 825 | * | |
| 826 | * WARNING! blksize is variable. cluster_write() is | |
| d1eff1f7 MD |
827 | * expected to not blow up if it encounters |
| 828 | * buffers that do not match the passed blksize. | |
| 710733a6 MD |
829 | * |
| 830 | * NOTE! Hammer shouldn't need to bawrite()/cluster_write(). | |
| 831 | * The ip->rsv_recs check should burst-flush the data. | |
| 832 | * If we queue it immediately the buf could be left | |
| 833 | * locked on the device queue for a very long time. | |
| d1eff1f7 | 834 | * |
| 55b50bd5 MD |
835 | * However, failing to flush a dirty buffer out when |
| 836 | * issued from the pageout daemon can result in a low | |
| 837 | * memory deadlock against bio_page_alloc(), so we | |
| 838 | * have to bawrite() on IO_ASYNC as well. | |
| 839 | * | |
| d1eff1f7 MD |
840 | * NOTE! To avoid degenerate stalls due to mismatched block |
| 841 | * sizes we only honor IO_DIRECT on the write which | |
| 842 | * abuts the end of the buffer. However, we must | |
| 843 | * honor IO_SYNC in case someone is silly enough to | |
| 844 | * configure a HAMMER file as swap, or when HAMMER | |
| 845 | * is serving NFS (for commits). Ick ick. | |
| 47637bff | 846 | */ |
| 66030e2b MD |
847 | bp->b_flags |= B_AGE; |
| 848 | if (blksize == HAMMER_XBUFSIZE) | |
| 849 | bp->b_flags |= B_CLUSTEROK; | |
| 850 | ||
| 66325755 MD |
851 | if (ap->a_ioflag & IO_SYNC) { |
| 852 | bwrite(bp); | |
| d1eff1f7 | 853 | } else if ((ap->a_ioflag & IO_DIRECT) && endofblk) { |
| 66325755 | 854 | bawrite(bp); |
| 55b50bd5 MD |
855 | } else if (ap->a_ioflag & IO_ASYNC) { |
| 856 | bawrite(bp); | |
| 9de13b88 MD |
857 | } else if (hammer_cluster_enable && |
| 858 | !(ap->a_vp->v_mount->mnt_flag & MNT_NOCLUSTERW)) { | |
| 859 | if (base_offset < HAMMER_XDEMARC) | |
| 860 | cluster_eof = hammer_blockdemarc(base_offset, | |
| 861 | ip->ino_data.size); | |
| 862 | else | |
| 863 | cluster_eof = ip->ino_data.size; | |
| 864 | cluster_write(bp, cluster_eof, blksize, seqcount); | |
| 710733a6 | 865 | } else { |
| 4a2796f3 MD |
866 | bdwrite(bp); |
| 867 | } | |
| 66325755 | 868 | } |
| b84de5af | 869 | hammer_done_transaction(&trans); |
| fbb84158 | 870 | hammer_knote(ap->a_vp, kflags); |
| b0aab9b9 | 871 | lwkt_reltoken(&hmp->fs_token); |
| 66325755 | 872 | return (error); |
| 427e5fc6 MD |
873 | } |
| 874 | ||
| 66325755 MD |
875 | /* |
| 876 | * hammer_vop_access { vp, mode, cred } | |
| b0aab9b9 MD |
877 | * |
| 878 | * MPSAFE - does not require fs_token | |
| 66325755 | 879 | */ |
| 427e5fc6 MD |
880 | static |
| 881 | int | |
| 66325755 | 882 | hammer_vop_access(struct vop_access_args *ap) |
| 427e5fc6 | 883 | { |
| 66325755 MD |
884 | struct hammer_inode *ip = VTOI(ap->a_vp); |
| 885 | uid_t uid; | |
| 886 | gid_t gid; | |
| 887 | int error; | |
| 888 | ||
| ce0138a6 | 889 | ++hammer_stats_file_iopsr; |
| 66325755 MD |
890 | uid = hammer_to_unix_xid(&ip->ino_data.uid); |
| 891 | gid = hammer_to_unix_xid(&ip->ino_data.gid); | |
| 892 | ||
| 893 | error = vop_helper_access(ap, uid, gid, ip->ino_data.mode, | |
| 894 | ip->ino_data.uflags); | |
| 895 | return (error); | |
| 427e5fc6 MD |
896 | } |
| 897 | ||
| 66325755 MD |
898 | /* |
| 899 | * hammer_vop_advlock { vp, id, op, fl, flags } | |
| b0aab9b9 MD |
900 | * |
| 901 | * MPSAFE - does not require fs_token | |
| 66325755 | 902 | */ |
| 427e5fc6 MD |
903 | static |
| 904 | int | |
| 66325755 | 905 | hammer_vop_advlock(struct vop_advlock_args *ap) |
| 427e5fc6 | 906 | { |
| 4a2796f3 | 907 | hammer_inode_t ip = VTOI(ap->a_vp); |
| 66325755 | 908 | |
| 11ad5ade | 909 | return (lf_advlock(ap, &ip->advlock, ip->ino_data.size)); |
| 427e5fc6 MD |
910 | } |
| 911 | ||
| 66325755 MD |
912 | /* |
| 913 | * hammer_vop_close { vp, fflag } | |
| 6f3d87c0 | 914 | * |
| b0aab9b9 | 915 | * We can only sync-on-close for normal closes. XXX disabled for now. |
| 66325755 | 916 | */ |
| 427e5fc6 MD |
917 | static |
| 918 | int | |
| 66325755 | 919 | hammer_vop_close(struct vop_close_args *ap) |
| 427e5fc6 | 920 | { |
| 9192654c | 921 | #if 0 |
| 6f3d87c0 MD |
922 | struct vnode *vp = ap->a_vp; |
| 923 | hammer_inode_t ip = VTOI(vp); | |
| 924 | int waitfor; | |
| 6f3d87c0 MD |
925 | if (ip->flags & (HAMMER_INODE_CLOSESYNC|HAMMER_INODE_CLOSEASYNC)) { |
| 926 | if (vn_islocked(vp) == LK_EXCLUSIVE && | |
| 927 | (vp->v_flag & (VINACTIVE|VRECLAIMED)) == 0) { | |
| 928 | if (ip->flags & HAMMER_INODE_CLOSESYNC) | |
| 929 | waitfor = MNT_WAIT; | |
| 930 | else | |
| 931 | waitfor = MNT_NOWAIT; | |
| 932 | ip->flags &= ~(HAMMER_INODE_CLOSESYNC | | |
| 933 | HAMMER_INODE_CLOSEASYNC); | |
| 934 | VOP_FSYNC(vp, MNT_NOWAIT, waitfor); | |
| 935 | } | |
| 936 | } | |
| 9192654c | 937 | #endif |
| a89aec1b | 938 | return (vop_stdclose(ap)); |
| 427e5fc6 MD |
939 | } |
| 940 | ||
| 66325755 MD |
941 | /* |
| 942 | * hammer_vop_ncreate { nch, dvp, vpp, cred, vap } | |
| 943 | * | |
| 944 | * The operating system has already ensured that the directory entry | |
| 945 | * does not exist and done all appropriate namespace locking. | |
| 946 | */ | |
| 427e5fc6 MD |
947 | static |
| 948 | int | |
| 66325755 | 949 | hammer_vop_ncreate(struct vop_ncreate_args *ap) |
| 427e5fc6 | 950 | { |
| 66325755 MD |
951 | struct hammer_transaction trans; |
| 952 | struct hammer_inode *dip; | |
| 953 | struct hammer_inode *nip; | |
| 954 | struct nchandle *nch; | |
| b0aab9b9 | 955 | hammer_mount_t hmp; |
| 66325755 MD |
956 | int error; |
| 957 | ||
| 958 | nch = ap->a_nch; | |
| 959 | dip = VTOI(ap->a_dvp); | |
| b0aab9b9 | 960 | hmp = dip->hmp; |
| 66325755 | 961 | |
| d113fda1 MD |
962 | if (dip->flags & HAMMER_INODE_RO) |
| 963 | return (EROFS); | |
| b0aab9b9 | 964 | if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) |
| e63644f0 | 965 | return (error); |
| d113fda1 | 966 | |
| 66325755 MD |
967 | /* |
| 968 | * Create a transaction to cover the operations we perform. | |
| 969 | */ | |
| b0aab9b9 MD |
970 | lwkt_gettoken(&hmp->fs_token); |
| 971 | hammer_start_transaction(&trans, hmp); | |
| ce0138a6 | 972 | ++hammer_stats_file_iopsw; |
| 66325755 MD |
973 | |
| 974 | /* | |
| 975 | * Create a new filesystem object of the requested type. The | |
| b84de5af MD |
976 | * returned inode will be referenced and shared-locked to prevent |
| 977 | * it from being moved to the flusher. | |
| 66325755 | 978 | */ |
| 5a930e66 | 979 | error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, |
| 5a64efa1 MD |
980 | dip, nch->ncp->nc_name, nch->ncp->nc_nlen, |
| 981 | NULL, &nip); | |
| 66325755 | 982 | if (error) { |
| 77062c8a | 983 | hkprintf("hammer_create_inode error %d\n", error); |
| b84de5af | 984 | hammer_done_transaction(&trans); |
| 66325755 | 985 | *ap->a_vpp = NULL; |
| b0aab9b9 | 986 | lwkt_reltoken(&hmp->fs_token); |
| 66325755 MD |
987 | return (error); |
| 988 | } | |
| 66325755 MD |
989 | |
| 990 | /* | |
| 991 | * Add the new filesystem object to the directory. This will also | |
| 992 | * bump the inode's link count. | |
| 993 | */ | |
| 5a930e66 MD |
994 | error = hammer_ip_add_directory(&trans, dip, |
| 995 | nch->ncp->nc_name, nch->ncp->nc_nlen, | |
| 996 | nip); | |
| 0b075555 | 997 | if (error) |
| 77062c8a | 998 | hkprintf("hammer_ip_add_directory error %d\n", error); |
| 66325755 MD |
999 | |
| 1000 | /* | |
| 1001 | * Finish up. | |
| 1002 | */ | |
| 1003 | if (error) { | |
| a89aec1b | 1004 | hammer_rel_inode(nip, 0); |
| b84de5af | 1005 | hammer_done_transaction(&trans); |
| 66325755 MD |
1006 | *ap->a_vpp = NULL; |
| 1007 | } else { | |
| e8599db1 | 1008 | error = hammer_get_vnode(nip, ap->a_vpp); |
| b84de5af | 1009 | hammer_done_transaction(&trans); |
| a89aec1b MD |
1010 | hammer_rel_inode(nip, 0); |
| 1011 | if (error == 0) { | |
| 1012 | cache_setunresolved(ap->a_nch); | |
| 1013 | cache_setvp(ap->a_nch, *ap->a_vpp); | |
| 1014 | } | |
| fbb84158 | 1015 | hammer_knote(ap->a_dvp, NOTE_WRITE); |
| 66325755 | 1016 | } |
| b0aab9b9 | 1017 | lwkt_reltoken(&hmp->fs_token); |
| 66325755 | 1018 | return (error); |
| 427e5fc6 MD |
1019 | } |
| 1020 | ||
| 66325755 MD |
1021 | /* |
| 1022 | * hammer_vop_getattr { vp, vap } | |
| 98f7132d MD |
1023 | * |
| 1024 | * Retrieve an inode's attribute information. When accessing inodes | |
| 1025 | * historically we fake the atime field to ensure consistent results. | |
| 1026 | * The atime field is stored in the B-Tree element and allowed to be | |
| 1027 | * updated without cycling the element. | |
| 899eb297 | 1028 | * |
| b0aab9b9 | 1029 | * MPSAFE - does not require fs_token |
| 66325755 | 1030 | */ |
| 427e5fc6 MD |
1031 | static |
| 1032 | int | |
| 66325755 | 1033 | hammer_vop_getattr(struct vop_getattr_args *ap) |
| 427e5fc6 | 1034 | { |
| 66325755 MD |
1035 | struct hammer_inode *ip = VTOI(ap->a_vp); |
| 1036 | struct vattr *vap = ap->a_vap; | |
| 1037 | ||
| a56cb012 MD |
1038 | /* |
| 1039 | * We want the fsid to be different when accessing a filesystem | |
| 1040 | * with different as-of's so programs like diff don't think | |
| 1041 | * the files are the same. | |
| 1042 | * | |
| 1043 | * We also want the fsid to be the same when comparing snapshots, | |
| 1044 | * or when comparing mirrors (which might be backed by different | |
| 1045 | * physical devices). HAMMER fsids are based on the PFS's | |
| 1046 | * shared_uuid field. | |
| 1047 | * | |
| 1048 | * XXX there is a chance of collision here. The va_fsid reported | |
| 1049 | * by stat is different from the more involved fsid used in the | |
| 1050 | * mount structure. | |
| c82af904 | 1051 | */ |
| ce0138a6 | 1052 | ++hammer_stats_file_iopsr; |
| 899eb297 | 1053 | hammer_lock_sh(&ip->lock); |
| a56cb012 MD |
1054 | vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^ |
| 1055 | (u_int32_t)(ip->obj_asof >> 32); | |
| 1056 | ||
| 11ad5ade | 1057 | vap->va_fileid = ip->ino_leaf.base.obj_id; |
| 66325755 | 1058 | vap->va_mode = ip->ino_data.mode; |
| 11ad5ade | 1059 | vap->va_nlink = ip->ino_data.nlinks; |
| 66325755 MD |
1060 | vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid); |
| 1061 | vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid); | |
| 1062 | vap->va_rmajor = 0; | |
| 1063 | vap->va_rminor = 0; | |
| 11ad5ade | 1064 | vap->va_size = ip->ino_data.size; |
| bcac4bbb MD |
1065 | |
| 1066 | /* | |
| f437a2ab MD |
1067 | * Special case for @@PFS softlinks. The actual size of the |
| 1068 | * expanded softlink is "@@0x%016llx:%05d" == 26 bytes. | |
| cb3c760c | 1069 | * or for MAX_TID is "@@-1:%05d" == 10 bytes. |
| f437a2ab MD |
1070 | */ |
| 1071 | if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK && | |
| 1072 | ip->ino_data.size == 10 && | |
| 1073 | ip->obj_asof == HAMMER_MAX_TID && | |
| 1074 | ip->obj_localization == 0 && | |
| 1075 | strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) { | |
| cb3c760c MD |
1076 | if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE) |
| 1077 | vap->va_size = 26; | |
| 1078 | else | |
| 1079 | vap->va_size = 10; | |
| f437a2ab MD |
1080 | } |
| 1081 | ||
| 1082 | /* | |
| bcac4bbb MD |
1083 | * We must provide a consistent atime and mtime for snapshots |
| 1084 | * so people can do a 'tar cf - ... | md5' on them and get | |
| 1085 | * consistent results. | |
| 1086 | */ | |
| 1087 | if (ip->flags & HAMMER_INODE_RO) { | |
| ddfdf542 MD |
1088 | hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime); |
| 1089 | hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime); | |
| bcac4bbb | 1090 | } else { |
| ddfdf542 MD |
1091 | hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime); |
| 1092 | hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime); | |
| bcac4bbb | 1093 | } |
| ddfdf542 | 1094 | hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime); |
| 66325755 MD |
1095 | vap->va_flags = ip->ino_data.uflags; |
| 1096 | vap->va_gen = 1; /* hammer inums are unique for all time */ | |
| bf686dbe | 1097 | vap->va_blocksize = HAMMER_BUFSIZE; |
| 4a2796f3 MD |
1098 | if (ip->ino_data.size >= HAMMER_XDEMARC) { |
| 1099 | vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) & | |
| 1100 | ~HAMMER_XBUFMASK64; | |
| 1101 | } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) { | |
| 1102 | vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) & | |
| 1103 | ~HAMMER_BUFMASK64; | |
| 1104 | } else { | |
| 1105 | vap->va_bytes = (ip->ino_data.size + 15) & ~15; | |
| 1106 | } | |
| 64950f31 | 1107 | |
| 11ad5ade | 1108 | vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type); |
| 66325755 | 1109 | vap->va_filerev = 0; /* XXX */ |
| 66325755 MD |
1110 | vap->va_uid_uuid = ip->ino_data.uid; |
| 1111 | vap->va_gid_uuid = ip->ino_data.gid; | |
| 1112 | vap->va_fsid_uuid = ip->hmp->fsid; | |
| 1113 | vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID | | |
| 1114 | VA_FSID_UUID_VALID; | |
| 7a04d74f | 1115 | |
| 11ad5ade | 1116 | switch (ip->ino_data.obj_type) { |
| 7a04d74f MD |
1117 | case HAMMER_OBJTYPE_CDEV: |
| 1118 | case HAMMER_OBJTYPE_BDEV: | |
| 1119 | vap->va_rmajor = ip->ino_data.rmajor; | |
| 1120 | vap->va_rminor = ip->ino_data.rminor; | |
| 1121 | break; | |
| 1122 | default: | |
| 1123 | break; | |
| 1124 | } | |
| 899eb297 | 1125 | hammer_unlock(&ip->lock); |
| 66325755 | 1126 | return(0); |
| 427e5fc6 MD |
1127 | } |
| 1128 | ||
| 66325755 MD |
1129 | /* |
| 1130 | * hammer_vop_nresolve { nch, dvp, cred } | |
| 1131 | * | |
| 1132 | * Locate the requested directory entry. | |
| 1133 | */ | |
| 427e5fc6 MD |
1134 | static |
| 1135 | int | |
| 66325755 | 1136 | hammer_vop_nresolve(struct vop_nresolve_args *ap) |
| 427e5fc6 | 1137 | { |
| 36f82b23 | 1138 | struct hammer_transaction trans; |
| 66325755 | 1139 | struct namecache *ncp; |
| b0aab9b9 | 1140 | hammer_mount_t hmp; |
| 7f7c1f84 MD |
1141 | hammer_inode_t dip; |
| 1142 | hammer_inode_t ip; | |
| 1143 | hammer_tid_t asof; | |
| 8cd0a023 | 1144 | struct hammer_cursor cursor; |
| 66325755 MD |
1145 | struct vnode *vp; |
| 1146 | int64_t namekey; | |
| 1147 | int error; | |
| 7f7c1f84 MD |
1148 | int i; |
| 1149 | int nlen; | |
| d113fda1 | 1150 | int flags; |
| a56cb012 | 1151 | int ispfs; |
| adf01747 | 1152 | int64_t obj_id; |
| ddfdf542 | 1153 | u_int32_t localization; |
| 5e435c92 | 1154 | u_int32_t max_iterations; |
| 7f7c1f84 MD |
1155 | |
| 1156 | /* | |
| 1157 | * Misc initialization, plus handle as-of name extensions. Look for | |
| 1158 | * the '@@' extension. Note that as-of files and directories cannot | |
| 1159 | * be modified. | |
| 7f7c1f84 MD |
1160 | */ |
| 1161 | dip = VTOI(ap->a_dvp); | |
| 1162 | ncp = ap->a_nch->ncp; | |
| 1163 | asof = dip->obj_asof; | |
| bc6c1f13 | 1164 | localization = dip->obj_localization; /* for code consistency */ |
| 7f7c1f84 | 1165 | nlen = ncp->nc_nlen; |
| ea434b6f | 1166 | flags = dip->flags & HAMMER_INODE_RO; |
| a56cb012 | 1167 | ispfs = 0; |
| b0aab9b9 | 1168 | hmp = dip->hmp; |
| 7f7c1f84 | 1169 | |
| b0aab9b9 MD |
1170 | lwkt_gettoken(&hmp->fs_token); |
| 1171 | hammer_simple_transaction(&trans, hmp); | |
| ce0138a6 | 1172 | ++hammer_stats_file_iopsr; |
| 36f82b23 | 1173 | |
| 7f7c1f84 MD |
1174 | for (i = 0; i < nlen; ++i) { |
| 1175 | if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') { | |
| bc6c1f13 MD |
1176 | error = hammer_str_to_tid(ncp->nc_name + i + 2, |
| 1177 | &ispfs, &asof, &localization); | |
| 1178 | if (error != 0) { | |
| 1179 | i = nlen; | |
| 1180 | break; | |
| 1181 | } | |
| ea434b6f MD |
1182 | if (asof != HAMMER_MAX_TID) |
| 1183 | flags |= HAMMER_INODE_RO; | |
| 7f7c1f84 MD |
1184 | break; |
| 1185 | } | |
| 1186 | } | |
| 1187 | nlen = i; | |
| 66325755 | 1188 | |
| 8cd0a023 | 1189 | /* |
| ea434b6f MD |
1190 | * If this is a PFS softlink we dive into the PFS |
| 1191 | */ | |
| 1192 | if (ispfs && nlen == 0) { | |
| 1193 | ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT, | |
| 1194 | asof, localization, | |
| 1195 | flags, &error); | |
| 1196 | if (error == 0) { | |
| 1197 | error = hammer_get_vnode(ip, &vp); | |
| 1198 | hammer_rel_inode(ip, 0); | |
| 1199 | } else { | |
| 1200 | vp = NULL; | |
| 1201 | } | |
| 1202 | if (error == 0) { | |
| 1203 | vn_unlock(vp); | |
| 1204 | cache_setvp(ap->a_nch, vp); | |
| 1205 | vrele(vp); | |
| 1206 | } | |
| 1207 | goto done; | |
| 1208 | } | |
| 1209 | ||
| 1210 | /* | |
| 294aec9f MD |
1211 | * If there is no path component the time extension is relative to dip. |
| 1212 | * e.g. "fubar/@@<snapshot>" | |
| 1213 | * | |
| 1214 | * "." is handled by the kernel, but ".@@<snapshot>" is not. | |
| 1215 | * e.g. "fubar/.@@<snapshot>" | |
| 1216 | * | |
| 1217 | * ".." is handled by the kernel. We do not currently handle | |
| 1218 | * "..@<snapshot>". | |
| d113fda1 | 1219 | */ |
| 294aec9f | 1220 | if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) { |
| bcac4bbb | 1221 | ip = hammer_get_inode(&trans, dip, dip->obj_id, |
| ddfdf542 MD |
1222 | asof, dip->obj_localization, |
| 1223 | flags, &error); | |
| d113fda1 | 1224 | if (error == 0) { |
| e8599db1 | 1225 | error = hammer_get_vnode(ip, &vp); |
| d113fda1 MD |
1226 | hammer_rel_inode(ip, 0); |
| 1227 | } else { | |
| 1228 | vp = NULL; | |
| 1229 | } | |
| 1230 | if (error == 0) { | |
| 1231 | vn_unlock(vp); | |
| 1232 | cache_setvp(ap->a_nch, vp); | |
| 1233 | vrele(vp); | |
| 1234 | } | |
| 36f82b23 | 1235 | goto done; |
| d113fda1 MD |
1236 | } |
| 1237 | ||
| 1238 | /* | |
| 8cd0a023 MD |
1239 | * Calculate the namekey and setup the key range for the scan. This |
| 1240 | * works kinda like a chained hash table where the lower 32 bits | |
| 1241 | * of the namekey synthesize the chain. | |
| 1242 | * | |
| 1243 | * The key range is inclusive of both key_beg and key_end. | |
| 1244 | */ | |
| 5e435c92 MD |
1245 | namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen, |
| 1246 | &max_iterations); | |
| 66325755 | 1247 | |
| bcac4bbb | 1248 | error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip); |
| 5a930e66 | 1249 | cursor.key_beg.localization = dip->obj_localization + |
| beec5dc4 | 1250 | hammer_dir_localization(dip); |
| 8cd0a023 MD |
1251 | cursor.key_beg.obj_id = dip->obj_id; |
| 1252 | cursor.key_beg.key = namekey; | |
| d5530d22 | 1253 | cursor.key_beg.create_tid = 0; |
| 8cd0a023 MD |
1254 | cursor.key_beg.delete_tid = 0; |
| 1255 | cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY; | |
| 1256 | cursor.key_beg.obj_type = 0; | |
| 66325755 | 1257 | |
| 8cd0a023 | 1258 | cursor.key_end = cursor.key_beg; |
| 5e435c92 | 1259 | cursor.key_end.key += max_iterations; |
| d5530d22 MD |
1260 | cursor.asof = asof; |
| 1261 | cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF; | |
| 66325755 MD |
1262 | |
| 1263 | /* | |
| 8cd0a023 | 1264 | * Scan all matching records (the chain), locate the one matching |
| a89aec1b | 1265 | * the requested path component. |
| 8cd0a023 MD |
1266 | * |
| 1267 | * The hammer_ip_*() functions merge in-memory records with on-disk | |
| 1268 | * records for the purposes of the search. | |
| 66325755 | 1269 | */ |
| 6a37e7e4 | 1270 | obj_id = 0; |
| 43c665ae | 1271 | localization = HAMMER_DEF_LOCALIZATION; |
| 6a37e7e4 | 1272 | |
| 4e17f465 | 1273 | if (error == 0) { |
| 4e17f465 MD |
1274 | error = hammer_ip_first(&cursor); |
| 1275 | while (error == 0) { | |
| 1276 | error = hammer_ip_resolve_data(&cursor); | |
| 1277 | if (error) | |
| 1278 | break; | |
| 11ad5ade MD |
1279 | if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF && |
| 1280 | bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) { | |
| 1281 | obj_id = cursor.data->entry.obj_id; | |
| ddfdf542 | 1282 | localization = cursor.data->entry.localization; |
| 4e17f465 MD |
1283 | break; |
| 1284 | } | |
| 1285 | error = hammer_ip_next(&cursor); | |
| 66325755 MD |
1286 | } |
| 1287 | } | |
| 6a37e7e4 | 1288 | hammer_done_cursor(&cursor); |
| 4c286c36 MD |
1289 | |
| 1290 | /* | |
| 1291 | * Lookup the obj_id. This should always succeed. If it does not | |
| 1292 | * the filesystem may be damaged and we return a dummy inode. | |
| 1293 | */ | |
| 66325755 | 1294 | if (error == 0) { |
| bcac4bbb | 1295 | ip = hammer_get_inode(&trans, dip, obj_id, |
| ddfdf542 MD |
1296 | asof, localization, |
| 1297 | flags, &error); | |
| 4c286c36 MD |
1298 | if (error == ENOENT) { |
| 1299 | kprintf("HAMMER: WARNING: Missing " | |
| 1300 | "inode for dirent \"%s\"\n" | |
| 3d30bff3 MD |
1301 | "\tobj_id = %016llx, asof=%016llx, lo=%08x\n", |
| 1302 | ncp->nc_name, | |
| 1303 | (long long)obj_id, (long long)asof, | |
| 1304 | localization); | |
| 4c286c36 MD |
1305 | error = 0; |
| 1306 | ip = hammer_get_dummy_inode(&trans, dip, obj_id, | |
| 1307 | asof, localization, | |
| 1308 | flags, &error); | |
| 1309 | } | |
| 7f7c1f84 | 1310 | if (error == 0) { |
| e8599db1 | 1311 | error = hammer_get_vnode(ip, &vp); |
| 7f7c1f84 MD |
1312 | hammer_rel_inode(ip, 0); |
| 1313 | } else { | |
| 1314 | vp = NULL; | |
| 1315 | } | |
| 66325755 MD |
1316 | if (error == 0) { |
| 1317 | vn_unlock(vp); | |
| 1318 | cache_setvp(ap->a_nch, vp); | |
| 1319 | vrele(vp); | |
| 1320 | } | |
| 1321 | } else if (error == ENOENT) { | |
| 1322 | cache_setvp(ap->a_nch, NULL); | |
| 1323 | } | |
| 36f82b23 | 1324 | done: |
| b84de5af | 1325 | hammer_done_transaction(&trans); |
| b0aab9b9 | 1326 | lwkt_reltoken(&hmp->fs_token); |
| 66325755 | 1327 | return (error); |
| 427e5fc6 MD |
1328 | } |
| 1329 | ||
| 66325755 MD |
1330 | /* |
| 1331 | * hammer_vop_nlookupdotdot { dvp, vpp, cred } | |
| 1332 | * | |
| 1333 | * Locate the parent directory of a directory vnode. | |
| 1334 | * | |
| 1335 | * dvp is referenced but not locked. *vpp must be returned referenced and | |
| 1336 | * locked. A parent_obj_id of 0 does not necessarily indicate that we are | |
| 1337 | * at the root, instead it could indicate that the directory we were in was | |
| 1338 | * removed. | |
| 42c7d26b MD |
1339 | * |
| 1340 | * NOTE: as-of sequences are not linked into the directory structure. If | |
| 1341 | * we are at the root with a different asof then the mount point, reload | |
| 1342 | * the same directory with the mount point's asof. I'm not sure what this | |
| 1343 | * will do to NFS. We encode ASOF stamps in NFS file handles so it might not | |
| 1344 | * get confused, but it hasn't been tested. | |
| 66325755 | 1345 | */ |
| 427e5fc6 MD |
1346 | static |
| 1347 | int | |
| 66325755 | 1348 | hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap) |
| 427e5fc6 | 1349 | { |
| 36f82b23 | 1350 | struct hammer_transaction trans; |
| 66325755 | 1351 | struct hammer_inode *dip; |
| d113fda1 | 1352 | struct hammer_inode *ip; |
| b0aab9b9 | 1353 | hammer_mount_t hmp; |
| 42c7d26b | 1354 | int64_t parent_obj_id; |
| 5a930e66 | 1355 | u_int32_t parent_obj_localization; |
| 42c7d26b | 1356 | hammer_tid_t asof; |
| d113fda1 | 1357 | int error; |
| 66325755 MD |
1358 | |
| 1359 | dip = VTOI(ap->a_dvp); | |
| 42c7d26b | 1360 | asof = dip->obj_asof; |
| b0aab9b9 | 1361 | hmp = dip->hmp; |
| 5a930e66 MD |
1362 | |
| 1363 | /* | |
| 1364 | * Whos are parent? This could be the root of a pseudo-filesystem | |
| 1365 | * whos parent is in another localization domain. | |
| 1366 | */ | |
| b0aab9b9 | 1367 | lwkt_gettoken(&hmp->fs_token); |
| 42c7d26b | 1368 | parent_obj_id = dip->ino_data.parent_obj_id; |
| 5a930e66 MD |
1369 | if (dip->obj_id == HAMMER_OBJID_ROOT) |
| 1370 | parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization; | |
| 1371 | else | |
| 1372 | parent_obj_localization = dip->obj_localization; | |
| 42c7d26b MD |
1373 | |
| 1374 | if (parent_obj_id == 0) { | |
| 1375 | if (dip->obj_id == HAMMER_OBJID_ROOT && | |
| b0aab9b9 | 1376 | asof != hmp->asof) { |
| 42c7d26b | 1377 | parent_obj_id = dip->obj_id; |
| b0aab9b9 | 1378 | asof = hmp->asof; |
| 42c7d26b MD |
1379 | *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK); |
| 1380 | ksnprintf(*ap->a_fakename, 19, "0x%016llx", | |
| 973c11b9 | 1381 | (long long)dip->obj_asof); |
| 42c7d26b MD |
1382 | } else { |
| 1383 | *ap->a_vpp = NULL; | |
| b0aab9b9 | 1384 | lwkt_reltoken(&hmp->fs_token); |
| 42c7d26b MD |
1385 | return ENOENT; |
| 1386 | } | |
| 66325755 | 1387 | } |
| d113fda1 | 1388 | |
| b0aab9b9 | 1389 | hammer_simple_transaction(&trans, hmp); |
| ce0138a6 | 1390 | ++hammer_stats_file_iopsr; |
| 36f82b23 | 1391 | |
| bcac4bbb | 1392 | ip = hammer_get_inode(&trans, dip, parent_obj_id, |
| 5a930e66 | 1393 | asof, parent_obj_localization, |
| ddfdf542 | 1394 | dip->flags, &error); |
| 36f82b23 | 1395 | if (ip) { |
| e8599db1 | 1396 | error = hammer_get_vnode(ip, ap->a_vpp); |
| 36f82b23 MD |
1397 | hammer_rel_inode(ip, 0); |
| 1398 | } else { | |
| d113fda1 | 1399 | *ap->a_vpp = NULL; |
| d113fda1 | 1400 | } |
| b84de5af | 1401 | hammer_done_transaction(&trans); |
| b0aab9b9 | 1402 | lwkt_reltoken(&hmp->fs_token); |
| d113fda1 | 1403 | return (error); |
| 427e5fc6 MD |
1404 | } |
| 1405 | ||
| 66325755 MD |
1406 | /* |
| 1407 | * hammer_vop_nlink { nch, dvp, vp, cred } | |
| 1408 | */ | |
| 427e5fc6 MD |
1409 | static |
| 1410 | int | |
| 66325755 | 1411 | hammer_vop_nlink(struct vop_nlink_args *ap) |
| 427e5fc6 | 1412 | { |
| 66325755 MD |
1413 | struct hammer_transaction trans; |
| 1414 | struct hammer_inode *dip; | |
| 1415 | struct hammer_inode *ip; | |
| 1416 | struct nchandle *nch; | |
| b0aab9b9 | 1417 | hammer_mount_t hmp; |
| 66325755 MD |
1418 | int error; |
| 1419 | ||
| f437a2ab MD |
1420 | if (ap->a_dvp->v_mount != ap->a_vp->v_mount) |
| 1421 | return(EXDEV); | |
| 1422 | ||
| 66325755 MD |
1423 | nch = ap->a_nch; |
| 1424 | dip = VTOI(ap->a_dvp); | |
| 1425 | ip = VTOI(ap->a_vp); | |
| b0aab9b9 | 1426 | hmp = dip->hmp; |
| 66325755 | 1427 | |
| f437a2ab MD |
1428 | if (dip->obj_localization != ip->obj_localization) |
| 1429 | return(EXDEV); | |
| 1430 | ||
| d113fda1 MD |
1431 | if (dip->flags & HAMMER_INODE_RO) |
| 1432 | return (EROFS); | |
| 1433 | if (ip->flags & HAMMER_INODE_RO) | |
| 1434 | return (EROFS); | |
| b0aab9b9 | 1435 | if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) |
| e63644f0 | 1436 | return (error); |
| d113fda1 | 1437 | |
| 66325755 MD |
1438 | /* |
| 1439 | * Create a transaction to cover the operations we perform. | |
| 1440 | */ | |
| b0aab9b9 MD |
1441 | lwkt_gettoken(&hmp->fs_token); |
| 1442 | hammer_start_transaction(&trans, hmp); | |
| ce0138a6 | 1443 | ++hammer_stats_file_iopsw; |
| 66325755 MD |
1444 | |
| 1445 | /* | |
| 1446 | * Add the filesystem object to the directory. Note that neither | |
| 1447 | * dip nor ip are referenced or locked, but their vnodes are | |
| 1448 | * referenced. This function will bump the inode's link count. | |
| 1449 | */ | |
| 5a930e66 MD |
1450 | error = hammer_ip_add_directory(&trans, dip, |
| 1451 | nch->ncp->nc_name, nch->ncp->nc_nlen, | |
| 1452 | ip); | |
| 66325755 MD |
1453 | |
| 1454 | /* | |
| 1455 | * Finish up. | |
| 1456 | */ | |
| b84de5af | 1457 | if (error == 0) { |
| 6b4f890b MD |
1458 | cache_setunresolved(nch); |
| 1459 | cache_setvp(nch, ap->a_vp); | |
| 66325755 | 1460 | } |
| b84de5af | 1461 | hammer_done_transaction(&trans); |
| fbb84158 MD |
1462 | hammer_knote(ap->a_vp, NOTE_LINK); |
| 1463 | hammer_knote(ap->a_dvp, NOTE_WRITE); | |
| b0aab9b9 | 1464 | lwkt_reltoken(&hmp->fs_token); |
| 66325755 | 1465 | return (error); |
| 427e5fc6 MD |
1466 | } |
| 1467 | ||
| 66325755 MD |
1468 | /* |
| 1469 | * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap } | |
| 1470 | * | |
| 1471 | * The operating system has already ensured that the directory entry | |
| 1472 | * does not exist and done all appropriate namespace locking. | |
| 1473 | */ | |
| 427e5fc6 MD |
1474 | static |
| 1475 | int | |
| 66325755 | 1476 | hammer_vop_nmkdir(struct vop_nmkdir_args *ap) |
| 427e5fc6 | 1477 | { |
| 66325755 MD |
1478 | struct hammer_transaction trans; |
| 1479 | struct hammer_inode *dip; | |
| 1480 | struct hammer_inode *nip; | |
| 1481 | struct nchandle *nch; | |
| b0aab9b9 | 1482 | hammer_mount_t hmp; |
| 66325755 MD |
1483 | int error; |
| 1484 | ||
| 1485 | nch = ap->a_nch; | |
| 1486 | dip = VTOI(ap->a_dvp); | |
| b0aab9b9 | 1487 | hmp = dip->hmp; |
| 66325755 | 1488 | |
| d113fda1 MD |
1489 | if (dip->flags & HAMMER_INODE_RO) |
| 1490 | return (EROFS); | |
| b0aab9b9 | 1491 | if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) |
| e63644f0 | 1492 | return (error); |
| d113fda1 | 1493 | |
| 66325755 MD |
1494 | /* |
| 1495 | * Create a transaction to cover the operations we perform. | |
| 1496 | */ | |
| b0aab9b9 MD |
1497 | lwkt_gettoken(&hmp->fs_token); |
| 1498 | hammer_start_transaction(&trans, hmp); | |
| ce0138a6 | 1499 | ++hammer_stats_file_iopsw; |
| 66325755 MD |
1500 | |
| 1501 | /* | |
| 1502 | * Create a new filesystem object of the requested type. The | |
| 8cd0a023 | 1503 | * returned inode will be referenced but not locked. |
| 66325755 | 1504 | */ |
| 5a930e66 | 1505 | error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, |
| 5a64efa1 MD |
1506 | dip, nch->ncp->nc_name, nch->ncp->nc_nlen, |
| 1507 | NULL, &nip); | |
| 66325755 | 1508 | if (error) { |
| 77062c8a | 1509 | hkprintf("hammer_mkdir error %d\n", error); |
| b84de5af | 1510 | hammer_done_transaction(&trans); |
| 66325755 | 1511 | *ap->a_vpp = NULL; |
| b0aab9b9 | 1512 | lwkt_reltoken(&hmp->fs_token); |
| 66325755 MD |
1513 | return (error); |
| 1514 | } | |
| 66325755 MD |
1515 | /* |
| 1516 | * Add the new filesystem object to the directory. This will also | |
| 1517 | * bump the inode's link count. | |
| 1518 | */ | |
| 5a930e66 MD |
1519 | error = hammer_ip_add_directory(&trans, dip, |
| 1520 | nch->ncp->nc_name, nch->ncp->nc_nlen, | |
| 1521 | nip); | |
| 0b075555 | 1522 | if (error) |
| 77062c8a | 1523 | hkprintf("hammer_mkdir (add) error %d\n", error); |
| 66325755 MD |
1524 | |
| 1525 | /* | |
| 1526 | * Finish up. | |
| 1527 | */ | |
| 1528 | if (error) { | |
| a89aec1b | 1529 | hammer_rel_inode(nip, 0); |
| 66325755 MD |
1530 | *ap->a_vpp = NULL; |
| 1531 | } else { | |
| e8599db1 | 1532 | error = hammer_get_vnode(nip, ap->a_vpp); |
| a89aec1b MD |
1533 | hammer_rel_inode(nip, 0); |
| 1534 | if (error == 0) { | |
| 1535 | cache_setunresolved(ap->a_nch); | |
| 1536 | cache_setvp(ap->a_nch, *ap->a_vpp); | |
| 1537 | } | |
| 66325755 | 1538 | } |
| b84de5af | 1539 | hammer_done_transaction(&trans); |
| fbb84158 MD |
1540 | if (error == 0) |
| 1541 | hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK); | |
| b0aab9b9 | 1542 | lwkt_reltoken(&hmp->fs_token); |
| 66325755 | 1543 | return (error); |
| 427e5fc6 MD |
1544 | } |
| 1545 | ||
| 66325755 MD |
1546 | /* |
| 1547 | * hammer_vop_nmknod { nch, dvp, vpp, cred, vap } | |
| 1548 | * | |
| 1549 | * The operating system has already ensured that the directory entry | |
| 1550 | * does not exist and done all appropriate namespace locking. | |
| 1551 | */ | |
| 427e5fc6 MD |
1552 | static |
| 1553 | int | |
| 66325755 | 1554 | hammer_vop_nmknod(struct vop_nmknod_args *ap) |
| 427e5fc6 | 1555 | { |
| 66325755 MD |
1556 | struct hammer_transaction trans; |
| 1557 | struct hammer_inode *dip; | |
| 1558 | struct hammer_inode *nip; | |
| 1559 | struct nchandle *nch; | |
| b0aab9b9 | 1560 | hammer_mount_t hmp; |
| 66325755 MD |
1561 | int error; |
| 1562 | ||
| 1563 | nch = ap->a_nch; | |
| 1564 | dip = VTOI(ap->a_dvp); | |
| b0aab9b9 | 1565 | hmp = dip->hmp; |
| 66325755 | 1566 | |
| d113fda1 MD |
1567 | if (dip->flags & HAMMER_INODE_RO) |
| 1568 | return (EROFS); | |
| b0aab9b9 | 1569 | if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) |
| e63644f0 | 1570 | return (error); |
| d113fda1 | 1571 | |
| 66325755 MD |
1572 | /* |
| 1573 | * Create a transaction to cover the operations we perform. | |
| 1574 | */ | |
| b0aab9b9 MD |
1575 | lwkt_gettoken(&hmp->fs_token); |
| 1576 | hammer_start_transaction(&trans, hmp); | |
| ce0138a6 | 1577 | ++hammer_stats_file_iopsw; |
| 66325755 MD |
1578 | |
| 1579 | /* | |
| 1580 | * Create a new filesystem object of the requested type. The | |
| 8cd0a023 | 1581 | * returned inode will be referenced but not locked. |
| 5a930e66 MD |
1582 | * |
| 1583 | * If mknod specifies a directory a pseudo-fs is created. | |
| 66325755 | 1584 | */ |
| 5a930e66 | 1585 | error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, |
| 5a64efa1 MD |
1586 | dip, nch->ncp->nc_name, nch->ncp->nc_nlen, |
| 1587 | NULL, &nip); | |
| 66325755 | 1588 | if (error) { |
| b84de5af | 1589 | hammer_done_transaction(&trans); |
| 66325755 | 1590 | *ap->a_vpp = NULL; |
| b0aab9b9 | 1591 | lwkt_reltoken(&hmp->fs_token); |
| 66325755 MD |
1592 | return (error); |
| 1593 | } | |
| 66325755 MD |
1594 | |
| 1595 | /* | |
| 1596 | * Add the new filesystem object to the directory. This will also | |
| 1597 | * bump the inode's link count. | |
| 1598 | */ | |
| 5a930e66 MD |
1599 | error = hammer_ip_add_directory(&trans, dip, |
| 1600 | nch->ncp->nc_name, nch->ncp->nc_nlen, | |
| 1601 | nip); | |
| 66325755 MD |
1602 | |
| 1603 | /* | |
| 1604 | * Finish up. | |
| 1605 | */ | |
| 1606 | if (error) { | |
| a89aec1b | 1607 | hammer_rel_inode(nip, 0); |
| 66325755 MD |
1608 | *ap->a_vpp = NULL; |
| 1609 | } else { | |
| e8599db1 | 1610 | error = hammer_get_vnode(nip, ap->a_vpp); |
| a89aec1b MD |
1611 | hammer_rel_inode(nip, 0); |
| 1612 | if (error == 0) { | |
| 1613 | cache_setunresolved(ap->a_nch); | |
| 1614 | cache_setvp(ap->a_nch, *ap->a_vpp); | |
| 1615 | } | |
| 66325755 | 1616 | } |
| b84de5af | 1617 | hammer_done_transaction(&trans); |
| fbb84158 MD |
1618 | if (error == 0) |
| 1619 | hammer_knote(ap->a_dvp, NOTE_WRITE); | |
| b0aab9b9 | 1620 | lwkt_reltoken(&hmp->fs_token); |
| 66325755 | 1621 | return (error); |
| 427e5fc6 MD |
1622 | } |
| 1623 | ||
| 66325755 MD |
1624 | /* |
| 1625 | * hammer_vop_open { vp, mode, cred, fp } | |
| b0aab9b9 MD |
1626 | * |
| 1627 | * MPSAFE (does not require fs_token) | |
| 66325755 | 1628 | */ |
| 427e5fc6 MD |
1629 | static |
| 1630 | int | |
| 66325755 | 1631 | hammer_vop_open(struct vop_open_args *ap) |
| 427e5fc6 | 1632 | { |
| 9f5097dc MD |
1633 | hammer_inode_t ip; |
| 1634 | ||
| ce0138a6 | 1635 | ++hammer_stats_file_iopsr; |
| 9f5097dc MD |
1636 | ip = VTOI(ap->a_vp); |
| 1637 | ||
| 1638 | if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO)) | |
| d113fda1 | 1639 | return (EROFS); |
| a89aec1b | 1640 | return(vop_stdopen(ap)); |
| 427e5fc6 MD |
1641 | } |
| 1642 | ||
| 66325755 | 1643 | /* |
| 66325755 MD |
1644 | * hammer_vop_print { vp } |
| 1645 | */ | |
| 427e5fc6 MD |
1646 | static |
| 1647 | int | |
| 66325755 | 1648 | hammer_vop_print(struct vop_print_args *ap) |
| 427e5fc6 MD |
1649 | { |
| 1650 | return EOPNOTSUPP; | |
| 1651 | } | |
| 1652 | ||
| 66325755 | 1653 | /* |
| 6b4f890b | 1654 | * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies } |
| 66325755 | 1655 | */ |
| 427e5fc6 MD |
1656 | static |
| 1657 | int | |
| 66325755 | 1658 | hammer_vop_readdir(struct vop_readdir_args *ap) |
| 427e5fc6 | 1659 | { |
| 36f82b23 | 1660 | struct hammer_transaction trans; |
| 6b4f890b MD |
1661 | struct hammer_cursor cursor; |
| 1662 | struct hammer_inode *ip; | |
| b0aab9b9 | 1663 | hammer_mount_t hmp; |
| 6b4f890b | 1664 | struct uio *uio; |
| 6b4f890b MD |
1665 | hammer_base_elm_t base; |
| 1666 | int error; | |
| 1667 | int cookie_index; | |
| 1668 | int ncookies; | |
| 1669 | off_t *cookies; | |
| 1670 | off_t saveoff; | |
| 1671 | int r; | |
| ea434b6f | 1672 | int dtype; |
| 6b4f890b | 1673 | |
| ce0138a6 | 1674 | ++hammer_stats_file_iopsr; |
| 6b4f890b MD |
1675 | ip = VTOI(ap->a_vp); |
| 1676 | uio = ap->a_uio; | |
| b3deaf57 | 1677 | saveoff = uio->uio_offset; |
| b0aab9b9 | 1678 | hmp = ip->hmp; |
| b3deaf57 MD |
1679 | |
| 1680 | if (ap->a_ncookies) { | |
| 1681 | ncookies = uio->uio_resid / 16 + 1; | |
| 1682 | if (ncookies > 1024) | |
| 1683 | ncookies = 1024; | |
| 1684 | cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK); | |
| 1685 | cookie_index = 0; | |
| 1686 | } else { | |
| 1687 | ncookies = -1; | |
| 1688 | cookies = NULL; | |
| 1689 | cookie_index = 0; | |
| 1690 | } | |
| 1691 | ||
| b0aab9b9 MD |
1692 | lwkt_gettoken(&hmp->fs_token); |
| 1693 | hammer_simple_transaction(&trans, hmp); | |
| 36f82b23 | 1694 | |
| b3deaf57 MD |
1695 | /* |
| 1696 | * Handle artificial entries | |
| 4c286c36 MD |
1697 | * |
| 1698 | * It should be noted that the minimum value for a directory | |
| 1699 | * hash key on-media is 0x0000000100000000, so we can use anything | |
| 1700 | * less then that to represent our 'special' key space. | |
| b3deaf57 MD |
1701 | */ |
| 1702 | error = 0; | |
| 1703 | if (saveoff == 0) { | |
| 1704 | r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, "."); | |
| 1705 | if (r) | |
| 1706 | goto done; | |
| 1707 | if (cookies) | |
| 1708 | cookies[cookie_index] = saveoff; | |
| 1709 | ++saveoff; | |
| 1710 | ++cookie_index; | |
| 1711 | if (cookie_index == ncookies) | |
| 1712 | goto done; | |
| 1713 | } | |
| 1714 | if (saveoff == 1) { | |
| 1715 | if (ip->ino_data.parent_obj_id) { | |
| 1716 | r = vop_write_dirent(&error, uio, | |
| 1717 | ip->ino_data.parent_obj_id, | |
| 1718 | DT_DIR, 2, ".."); | |
| 1719 | } else { | |
| 1720 | r = vop_write_dirent(&error, uio, | |
| 1721 | ip->obj_id, DT_DIR, 2, ".."); | |
| 1722 | } | |
| 1723 | if (r) | |
| 1724 | goto done; | |
| 1725 | if (cookies) | |
| 1726 | cookies[cookie_index] = saveoff; | |
| 1727 | ++saveoff; | |
| 1728 | ++cookie_index; | |
| 1729 | if (cookie_index == ncookies) | |
| 1730 | goto done; | |
| 1731 | } | |
| 6b4f890b MD |
1732 | |
| 1733 | /* | |
| 1734 | * Key range (begin and end inclusive) to scan. Directory keys | |
| 1735 | * directly translate to a 64 bit 'seek' position. | |
| 1736 | */ | |
| bcac4bbb | 1737 | hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip); |
| 5a930e66 | 1738 | cursor.key_beg.localization = ip->obj_localization + |
| beec5dc4 | 1739 | hammer_dir_localization(ip); |
| 6b4f890b | 1740 | cursor.key_beg.obj_id = ip->obj_id; |
| d5530d22 | 1741 | cursor.key_beg.create_tid = 0; |
| 6b4f890b MD |
1742 | cursor.key_beg.delete_tid = 0; |
| 1743 | cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY; | |
| 1744 | cursor.key_beg.obj_type = 0; | |
| b3deaf57 | 1745 | cursor.key_beg.key = saveoff; |
| 6b4f890b MD |
1746 | |
| 1747 | cursor.key_end = cursor.key_beg; | |
| 1748 | cursor.key_end.key = HAMMER_MAX_KEY; | |
| d5530d22 MD |
1749 | cursor.asof = ip->obj_asof; |
| 1750 | cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF; | |
| 6b4f890b | 1751 | |
| 4e17f465 | 1752 | error = hammer_ip_first(&cursor); |
| 6b4f890b MD |
1753 | |
| 1754 | while (error == 0) { | |
| 11ad5ade | 1755 | error = hammer_ip_resolve_data(&cursor); |
| 6b4f890b MD |
1756 | if (error) |
| 1757 | break; | |
| 11ad5ade | 1758 | base = &cursor.leaf->base; |
| 6b4f890b | 1759 | saveoff = base->key; |
| 11ad5ade | 1760 | KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF); |
| 6b4f890b | 1761 | |
| 7a04d74f MD |
1762 | if (base->obj_id != ip->obj_id) |
| 1763 | panic("readdir: bad record at %p", cursor.node); | |
| 1764 | ||
| ea434b6f MD |
1765 | /* |
| 1766 | * Convert pseudo-filesystems into softlinks | |
| 1767 | */ | |
| 1768 | dtype = hammer_get_dtype(cursor.leaf->base.obj_type); | |
| 6b4f890b | 1769 | r = vop_write_dirent( |
| 11ad5ade | 1770 | &error, uio, cursor.data->entry.obj_id, |
| ea434b6f | 1771 | dtype, |
| 11ad5ade MD |
1772 | cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF , |
| 1773 | (void *)cursor.data->entry.name); | |
| 6b4f890b MD |
1774 | if (r) |
| 1775 | break; | |
| 1776 | ++saveoff; | |
| 1777 | if (cookies) | |
| 1778 | cookies[cookie_index] = base->key; | |
| 1779 | ++cookie_index; | |
| 1780 | if (cookie_index == ncookies) | |
| 1781 | break; | |
| 1782 | error = hammer_ip_next(&cursor); | |
| 1783 | } | |
| 1784 | hammer_done_cursor(&cursor); | |
| 1785 | ||
| b3deaf57 | 1786 | done: |
| b84de5af | 1787 | hammer_done_transaction(&trans); |
| 36f82b23 | 1788 | |
| 6b4f890b MD |
1789 | if (ap->a_eofflag) |
| 1790 | *ap->a_eofflag = (error == ENOENT); | |
| 6b4f890b MD |
1791 | uio->uio_offset = saveoff; |
| 1792 | if (error && cookie_index == 0) { | |
| b3deaf57 MD |
1793 | if (error == ENOENT) |
| 1794 | error = 0; | |
| 6b4f890b MD |
1795 | if (cookies) { |
| 1796 | kfree(cookies, M_TEMP); | |
| 1797 | *ap->a_ncookies = 0; | |
| 1798 | *ap->a_cookies = NULL; | |
| 1799 | } | |
| 1800 | } else { | |
| 7a04d74f MD |
1801 | if (error == ENOENT) |
| 1802 | error = 0; | |
| 6b4f890b MD |
1803 | if (cookies) { |
| 1804 | *ap->a_ncookies = cookie_index; | |
| 1805 | *ap->a_cookies = cookies; | |
| 1806 | } | |
| 1807 | } | |
| b0aab9b9 | 1808 | lwkt_reltoken(&hmp->fs_token); |
| 6b4f890b | 1809 | return(error); |
| 427e5fc6 MD |
1810 | } |
| 1811 | ||
| 66325755 MD |
1812 | /* |
| 1813 | * hammer_vop_readlink { vp, uio, cred } | |
| 1814 | */ | |
| 427e5fc6 MD |
1815 | static |
| 1816 | int | |
| 66325755 | 1817 | hammer_vop_readlink(struct vop_readlink_args *ap) |
| 427e5fc6 | 1818 | { |
| 36f82b23 | 1819 | struct hammer_transaction trans; |
| 7a04d74f MD |
1820 | struct hammer_cursor cursor; |
| 1821 | struct hammer_inode *ip; | |
| b0aab9b9 | 1822 | hammer_mount_t hmp; |
| ea434b6f MD |
1823 | char buf[32]; |
| 1824 | u_int32_t localization; | |
| 1825 | hammer_pseudofs_inmem_t pfsm; | |
| 7a04d74f MD |
1826 | int error; |
| 1827 | ||
| 1828 | ip = VTOI(ap->a_vp); | |
| b0aab9b9 MD |
1829 | hmp = ip->hmp; |
| 1830 | ||
| 1831 | lwkt_gettoken(&hmp->fs_token); | |
| 36f82b23 | 1832 | |
| 2f85fa4d MD |
1833 | /* |
| 1834 | * Shortcut if the symlink data was stuffed into ino_data. | |
| ea434b6f | 1835 | * |
| 842e7a70 MD |
1836 | * Also expand special "@@PFS%05d" softlinks (expansion only |
| 1837 | * occurs for non-historical (current) accesses made from the | |
| 1838 | * primary filesystem). | |
| 2f85fa4d MD |
1839 | */ |
| 1840 | if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) { | |
| ea434b6f MD |
1841 | char *ptr; |
| 1842 | int bytes; | |
| 1843 | ||
| 1844 | ptr = ip->ino_data.ext.symlink; | |
| 1845 | bytes = (int)ip->ino_data.size; | |
| 842e7a70 MD |
1846 | if (bytes == 10 && |
| 1847 | ip->obj_asof == HAMMER_MAX_TID && | |
| 1848 | ip->obj_localization == 0 && | |
| 1849 | strncmp(ptr, "@@PFS", 5) == 0) { | |
| b0aab9b9 | 1850 | hammer_simple_transaction(&trans, hmp); |
| ea434b6f MD |
1851 | bcopy(ptr + 5, buf, 5); |
| 1852 | buf[5] = 0; | |
| 1853 | localization = strtoul(buf, NULL, 10) << 16; | |
| 1854 | pfsm = hammer_load_pseudofs(&trans, localization, | |
| 1855 | &error); | |
| 1856 | if (error == 0) { | |
| 4c038e17 MD |
1857 | if (pfsm->pfsd.mirror_flags & |
| 1858 | HAMMER_PFSD_SLAVE) { | |
| cb3c760c | 1859 | /* vap->va_size == 26 */ |
| 4c038e17 MD |
1860 | ksnprintf(buf, sizeof(buf), |
| 1861 | "@@0x%016llx:%05d", | |
| 973c11b9 | 1862 | (long long)pfsm->pfsd.sync_end_tid, |
| 4c038e17 MD |
1863 | localization >> 16); |
| 1864 | } else { | |
| cb3c760c MD |
1865 | /* vap->va_size == 10 */ |
| 1866 | ksnprintf(buf, sizeof(buf), | |
| 1867 | "@@-1:%05d", | |
| 1868 | localization >> 16); | |
| 1869 | #if 0 | |
| 4c038e17 MD |
1870 | ksnprintf(buf, sizeof(buf), |
| 1871 | "@@0x%016llx:%05d", | |
| 973c11b9 | 1872 | (long long)HAMMER_MAX_TID, |
| 4c038e17 | 1873 | localization >> 16); |
| cb3c760c | 1874 | #endif |
| 4c038e17 | 1875 | } |
| ea434b6f MD |
1876 | ptr = buf; |
| 1877 | bytes = strlen(buf); | |
| 1878 | } | |
| 1879 | if (pfsm) | |
| b0aab9b9 | 1880 | hammer_rel_pseudofs(hmp, pfsm); |
| ea434b6f MD |
1881 | hammer_done_transaction(&trans); |
| 1882 | } | |
| 1883 | error = uiomove(ptr, bytes, ap->a_uio); | |
| b0aab9b9 | 1884 | lwkt_reltoken(&hmp->fs_token); |
| 2f85fa4d MD |
1885 | return(error); |
| 1886 | } | |
| 36f82b23 | 1887 | |
| 2f85fa4d MD |
1888 | /* |
| 1889 | * Long version | |
| 1890 | */ | |
| b0aab9b9 | 1891 | hammer_simple_transaction(&trans, hmp); |
| ce0138a6 | 1892 | ++hammer_stats_file_iopsr; |
| bcac4bbb | 1893 | hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip); |
| 7a04d74f MD |
1894 | |
| 1895 | /* | |
| 1896 | * Key range (begin and end inclusive) to scan. Directory keys | |
| 1897 | * directly translate to a 64 bit 'seek' position. | |
| 1898 | */ | |
| 5a930e66 MD |
1899 | cursor.key_beg.localization = ip->obj_localization + |
| 1900 | HAMMER_LOCALIZE_MISC; | |
| 7a04d74f | 1901 | cursor.key_beg.obj_id = ip->obj_id; |
| d5530d22 | 1902 | cursor.key_beg.create_tid = 0; |
| 7a04d74f MD |
1903 | cursor.key_beg.delete_tid = 0; |
| 1904 | cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX; | |
| 1905 | cursor.key_beg.obj_type = 0; | |
| 1906 | cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK; | |
| d5530d22 MD |
1907 | cursor.asof = ip->obj_asof; |
| 1908 | cursor.flags |= HAMMER_CURSOR_ASOF; | |
| 7a04d74f | 1909 | |
| 45a014dc | 1910 | error = hammer_ip_lookup(&cursor); |
| 7a04d74f MD |
1911 | if (error == 0) { |
| 1912 | error = hammer_ip_resolve_data(&cursor); | |
| 1913 | if (error == 0) { | |
| 11ad5ade MD |
1914 | KKASSERT(cursor.leaf->data_len >= |
| 1915 | HAMMER_SYMLINK_NAME_OFF); | |
| 1916 | error = uiomove(cursor.data->symlink.name, | |
| 1917 | cursor.leaf->data_len - | |
| 1918 | HAMMER_SYMLINK_NAME_OFF, | |
| 7a04d74f MD |
1919 | ap->a_uio); |
| 1920 | } | |
| 1921 | } | |
| 1922 | hammer_done_cursor(&cursor); | |
| b84de5af | 1923 | hammer_done_transaction(&trans); |
| b0aab9b9 | 1924 | lwkt_reltoken(&hmp->fs_token); |
| 7a04d74f | 1925 | return(error); |
| 427e5fc6 MD |
1926 | } |
| 1927 | ||
| 66325755 MD |
1928 | /* |
| 1929 | * hammer_vop_nremove { nch, dvp, cred } | |
| 1930 | */ | |
| 427e5fc6 MD |
1931 | static |
| 1932 | int | |
| 66325755 | 1933 | hammer_vop_nremove(struct vop_nremove_args *ap) |
| 427e5fc6 | 1934 | { |
| b84de5af | 1935 | struct hammer_transaction trans; |
| e63644f0 | 1936 | struct hammer_inode *dip; |
| b0aab9b9 | 1937 | hammer_mount_t hmp; |
| b84de5af MD |
1938 | int error; |
| 1939 | ||
| e63644f0 | 1940 | dip = VTOI(ap->a_dvp); |
| b0aab9b9 | 1941 | hmp = dip->hmp; |
| e63644f0 MD |
1942 | |
| 1943 | if (hammer_nohistory(dip) == 0 && | |
| b0aab9b9 | 1944 | (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) { |
| e63644f0 MD |
1945 | return (error); |
| 1946 | } | |
| 1947 | ||
| b0aab9b9 MD |
1948 | lwkt_gettoken(&hmp->fs_token); |
| 1949 | hammer_start_transaction(&trans, hmp); | |
| ce0138a6 | 1950 | ++hammer_stats_file_iopsw; |
| d7e278bb | 1951 | error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0); |
| b84de5af | 1952 | hammer_done_transaction(&trans); |
| fbb84158 MD |
1953 | if (error == 0) |
| 1954 | hammer_knote(ap->a_dvp, NOTE_WRITE); | |
| b0aab9b9 | 1955 | lwkt_reltoken(&hmp->fs_token); |
| b84de5af | 1956 | return (error); |
| 427e5fc6 MD |
1957 | } |
| 1958 | ||
| 66325755 MD |
1959 | /* |
| 1960 | * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred } | |
| 1961 | */ | |
| 427e5fc6 MD |
1962 | static |
| 1963 | int | |
| 66325755 | 1964 | hammer_vop_nrename(struct vop_nrename_args *ap) |
| 427e5fc6 | 1965 | { |
| 8cd0a023 MD |
1966 | struct hammer_transaction trans; |
| 1967 | struct namecache *fncp; | |
| 1968 | struct namecache *tncp; | |
| 1969 | struct hammer_inode *fdip; | |
| 1970 | struct hammer_inode *tdip; | |
| 1971 | struct hammer_inode *ip; | |
| b0aab9b9 | 1972 | hammer_mount_t hmp; |
| 8cd0a023 | 1973 | struct hammer_cursor cursor; |
| 8cd0a023 | 1974 | int64_t namekey; |
| 5e435c92 | 1975 | u_int32_t max_iterations; |
| 11ad5ade | 1976 | int nlen, error; |
| 8cd0a023 | 1977 | |
| f437a2ab MD |
1978 | if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount) |
| 1979 | return(EXDEV); | |
| 1980 | if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount) | |
| 1981 | return(EXDEV); | |
| 1982 | ||
| 8cd0a023 MD |
1983 | fdip = VTOI(ap->a_fdvp); |
| 1984 | tdip = VTOI(ap->a_tdvp); | |
| 1985 | fncp = ap->a_fnch->ncp; | |
| 1986 | tncp = ap->a_tnch->ncp; | |
| b3deaf57 MD |
1987 | ip = VTOI(fncp->nc_vp); |
| 1988 | KKASSERT(ip != NULL); | |
| d113fda1 | 1989 | |
| b0aab9b9 MD |
1990 | hmp = ip->hmp; |
| 1991 | ||
| f437a2ab MD |
1992 | if (fdip->obj_localization != tdip->obj_localization) |
| 1993 | return(EXDEV); | |
| 1994 | if (fdip->obj_localization != ip->obj_localization) | |
| 1995 | return(EXDEV); | |
| 1996 | ||
| d113fda1 MD |
1997 | if (fdip->flags & HAMMER_INODE_RO) |
| 1998 | return (EROFS); | |
| 1999 | if (tdip->flags & HAMMER_INODE_RO) | |
| 2000 | return (EROFS); | |
| 2001 | if (ip->flags & HAMMER_INODE_RO) | |
| 2002 | return (EROFS); | |
| b0aab9b9 | 2003 | if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) |
| e63644f0 | 2004 | return (error); |
| d113fda1 | 2005 | |
| b0aab9b9 MD |
2006 | lwkt_gettoken(&hmp->fs_token); |
| 2007 | hammer_start_transaction(&trans, hmp); | |
| ce0138a6 | 2008 | ++hammer_stats_file_iopsw; |
| 8cd0a023 MD |
2009 | |
| 2010 | /* | |
| b3deaf57 MD |
2011 | * Remove tncp from the target directory and then link ip as |
| 2012 | * tncp. XXX pass trans to dounlink | |
| 42c7d26b MD |
2013 | * |
| 2014 | * Force the inode sync-time to match the transaction so it is | |
| 2015 | * in-sync with the creation of the target directory entry. | |
| 8cd0a023 | 2016 | */ |
| d7e278bb MD |
2017 | error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp, |
| 2018 | ap->a_cred, 0, -1); | |
| 42c7d26b | 2019 | if (error == 0 || error == ENOENT) { |
| 5a930e66 MD |
2020 | error = hammer_ip_add_directory(&trans, tdip, |
| 2021 | tncp->nc_name, tncp->nc_nlen, | |
| 2022 | ip); | |
| 42c7d26b MD |
2023 | if (error == 0) { |
| 2024 | ip->ino_data.parent_obj_id = tdip->obj_id; | |
| cc0758d0 | 2025 | ip->ino_data.ctime = trans.time; |
| e98f1b96 | 2026 | hammer_modify_inode(&trans, ip, HAMMER_INODE_DDIRTY); |
| 42c7d26b MD |
2027 | } |
| 2028 | } | |
| b3deaf57 MD |
2029 | if (error) |
| 2030 | goto failed; /* XXX */ | |
| 8cd0a023 MD |
2031 | |
| 2032 | /* | |
| 2033 | * Locate the record in the originating directory and remove it. | |
| 2034 | * | |
| 2035 | * Calculate the namekey and setup the key range for the scan. This | |
| 2036 | * works kinda like a chained hash table where the lower 32 bits | |
| 2037 | * of the namekey synthesize the chain. | |
| 2038 | * | |
| 2039 | * The key range is inclusive of both key_beg and key_end. | |
| 2040 | */ | |
| 5e435c92 MD |
2041 | namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen, |
| 2042 | &max_iterations); | |
| 6a37e7e4 | 2043 | retry: |
| bcac4bbb | 2044 | hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip); |
| 5a930e66 | 2045 | cursor.key_beg.localization = fdip->obj_localization + |
| beec5dc4 | 2046 | hammer_dir_localization(fdip); |
| 8cd0a023 MD |
2047 | cursor.key_beg.obj_id = fdip->obj_id; |
| 2048 | cursor.key_beg.key = namekey; | |
| d5530d22 | 2049 | cursor.key_beg.create_tid = 0; |
| 8cd0a023 MD |
2050 | cursor.key_beg.delete_tid = 0; |
| 2051 | cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY; | |
| 2052 | cursor.key_beg.obj_type = 0; | |
| 2053 | ||
| 2054 | cursor.key_end = cursor.key_beg; | |
| 5e435c92 | 2055 | cursor.key_end.key += max_iterations; |
| d5530d22 MD |
2056 | cursor.asof = fdip->obj_asof; |
| 2057 | cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF; | |
| 8cd0a023 MD |
2058 | |
| 2059 | /* | |
| 2060 | * Scan all matching records (the chain), locate the one matching | |
| a89aec1b | 2061 | * the requested path component. |
| 8cd0a023 MD |
2062 | * |
| 2063 | * The hammer_ip_*() functions merge in-memory records with on-disk | |
| 2064 | * records for the purposes of the search. | |
| 2065 | */ | |
| 4e17f465 | 2066 | error = hammer_ip_first(&cursor); |
| a89aec1b | 2067 | while (error == 0) { |
| 8cd0a023 MD |
2068 | if (hammer_ip_resolve_data(&cursor) != 0) |
| 2069 | break; | |
| 11ad5ade MD |
2070 | nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF; |
| 2071 | KKASSERT(nlen > 0); | |
| 2072 | if (fncp->nc_nlen == nlen && | |
| 2073 | bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) { | |
| 8cd0a023 MD |
2074 | break; |
| 2075 | } | |
| a89aec1b | 2076 | error = hammer_ip_next(&cursor); |
| 8cd0a023 | 2077 | } |
| 8cd0a023 MD |
2078 | |
| 2079 | /* | |
| 2080 | * If all is ok we have to get the inode so we can adjust nlinks. | |
| 6a37e7e4 MD |
2081 | * |
| 2082 | * WARNING: hammer_ip_del_directory() may have to terminate the | |
| 2083 | * cursor to avoid a recursion. It's ok to call hammer_done_cursor() | |
| 2084 | * twice. | |
| 8cd0a023 | 2085 | */ |
| 9944ae54 | 2086 | if (error == 0) |
| 6a37e7e4 | 2087 | error = hammer_ip_del_directory(&trans, &cursor, fdip, ip); |
| b84de5af MD |
2088 | |
| 2089 | /* | |
| 2090 | * XXX A deadlock here will break rename's atomicy for the purposes | |
| 2091 | * of crash recovery. | |
| 2092 | */ | |
| 2093 | if (error == EDEADLK) { | |
| b84de5af | 2094 | hammer_done_cursor(&cursor); |
| b84de5af MD |
2095 | goto retry; |
| 2096 | } | |
| 2097 | ||
| 2098 | /* | |
| 2099 | * Cleanup and tell the kernel that the rename succeeded. | |
| 036ea0c3 MD |
2100 | * |
| 2101 | * NOTE: ip->vp, if non-NULL, cannot be directly referenced | |
| 2102 | * without formally acquiring the vp since the vp might | |
| 2103 | * have zero refs on it, or in the middle of a reclaim, | |
| 2104 | * etc. | |
| b84de5af | 2105 | */ |
| c0ade690 | 2106 | hammer_done_cursor(&cursor); |
| fbb84158 | 2107 | if (error == 0) { |
| 6a37e7e4 | 2108 | cache_rename(ap->a_fnch, ap->a_tnch); |
| fbb84158 MD |
2109 | hammer_knote(ap->a_fdvp, NOTE_WRITE); |
| 2110 | hammer_knote(ap->a_tdvp, NOTE_WRITE); | |
| 036ea0c3 MD |
2111 | while (ip->vp) { |
| 2112 | struct vnode *vp; | |
| 2113 | ||
| 2114 | error = hammer_get_vnode(ip, &vp); | |
| 2115 | if (error == 0 && vp) { | |
| 2116 | vn_unlock(vp); | |
| 2117 | hammer_knote(ip->vp, NOTE_RENAME); | |
| 2118 | vrele(vp); | |
| 2119 | break; | |
| 2120 | } | |
| 2121 | kprintf("Debug: HAMMER ip/vp race2 avoided\n"); | |
| 2122 | } | |
| fbb84158 | 2123 | } |
| b84de5af | 2124 | |
| b3deaf57 | 2125 | failed: |
| b84de5af | 2126 | hammer_done_transaction(&trans); |
| b0aab9b9 | 2127 | lwkt_reltoken(&hmp->fs_token); |
| 8cd0a023 | 2128 | return (error); |
| 427e5fc6 MD |
2129 | } |
| 2130 | ||
| 66325755 MD |
2131 | /* |
| 2132 | * hammer_vop_nrmdir { nch, dvp, cred } | |
| 2133 | */ | |
| 427e5fc6 MD |
2134 | static |
| 2135 | int | |
| 66325755 | 2136 | hammer_vop_nrmdir(struct vop_nrmdir_args *ap) |
| 427e5fc6 | 2137 | { |
| b84de5af | 2138 | struct hammer_transaction trans; |
| e63644f0 | 2139 | struct hammer_inode *dip; |
| b0aab9b9 | 2140 | hammer_mount_t hmp; |
| b84de5af MD |
2141 | int error; |
| 2142 | ||
| e63644f0 | 2143 | dip = VTOI(ap->a_dvp); |
| b0aab9b9 | 2144 | hmp = dip->hmp; |
| e63644f0 MD |
2145 | |
| 2146 | if (hammer_nohistory(dip) == 0 && | |
| b0aab9b9 | 2147 | (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) { |
| e63644f0 MD |
2148 | return (error); |
| 2149 | } | |
| 2150 | ||
| b0aab9b9 MD |
2151 | lwkt_gettoken(&hmp->fs_token); |
| 2152 | hammer_start_transaction(&trans, hmp); | |
| ce0138a6 | 2153 | ++hammer_stats_file_iopsw; |
| d7e278bb | 2154 | error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1); |
| b84de5af | 2155 | hammer_done_transaction(&trans); |
| fbb84158 MD |
2156 | if (error == 0) |
| 2157 | hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK); | |
| b0aab9b9 | 2158 | lwkt_reltoken(&hmp->fs_token); |
| b84de5af | 2159 | return (error); |
| 427e5fc6 MD |
2160 | } |
| 2161 | ||
| 66325755 | 2162 | /* |
| 349433c9 MD |
2163 | * hammer_vop_markatime { vp, cred } |
| 2164 | */ | |
| 2165 | static | |
| 2166 | int | |
| 2167 | hammer_vop_markatime(struct vop_markatime_args *ap) | |
| 2168 | { | |
| 2169 | struct hammer_transaction trans; | |
| 2170 | struct hammer_inode *ip; | |
| b0aab9b9 | 2171 | hammer_mount_t hmp; |
| 349433c9 MD |
2172 | |
| 2173 | ip = VTOI(ap->a_vp); | |
| 2174 | if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) | |
| 2175 | return (EROFS); | |
| 2176 | if (ip->flags & HAMMER_INODE_RO) | |
| 2177 | return (EROFS); | |
| b0aab9b9 MD |
2178 | hmp = ip->hmp; |
| 2179 | if (hmp->mp->mnt_flag & MNT_NOATIME) | |
| 349433c9 | 2180 | return (0); |
| b0aab9b9 MD |
2181 | lwkt_gettoken(&hmp->fs_token); |
| 2182 | hammer_start_transaction(&trans, hmp); | |
| 349433c9 MD |
2183 | ++hammer_stats_file_iopsw; |
| 2184 | ||
| 2185 | ip->ino_data.atime = trans.time; | |
| e98f1b96 | 2186 | hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME); |
| 349433c9 MD |
2187 | hammer_done_transaction(&trans); |
| 2188 | hammer_knote(ap->a_vp, NOTE_ATTRIB); | |
| b0aab9b9 | 2189 | lwkt_reltoken(&hmp->fs_token); |
| 349433c9 MD |
2190 | return (0); |
| 2191 | } | |
| 2192 | ||
| 2193 | /* | |
| 66325755 MD |
2194 | * hammer_vop_setattr { vp, vap, cred } |
| 2195 | */ | |
| 427e5fc6 MD |
2196 | static |
| 2197 | int | |
| 66325755 | 2198 | hammer_vop_setattr(struct vop_setattr_args *ap) |
| 427e5fc6 | 2199 | { |
| 8cd0a023 | 2200 | struct hammer_transaction trans; |
| 8cd0a023 | 2201 | struct hammer_inode *ip; |
| b0aab9b9 MD |
2202 | struct vattr *vap; |
| 2203 | hammer_mount_t hmp; | |
| 8cd0a023 MD |
2204 | int modflags; |
| 2205 | int error; | |
| d5ef456e | 2206 | int truncating; |
| 4a2796f3 | 2207 | int blksize; |
| fbb84158 | 2208 | int kflags; |
| 6362a262 | 2209 | #if 0 |
| 4a2796f3 | 2210 | int64_t aligned_size; |
| 6362a262 | 2211 | #endif |
| 8cd0a023 | 2212 | u_int32_t flags; |
| 8cd0a023 MD |
2213 | |
| 2214 | vap = ap->a_vap; | |
| 2215 | ip = ap->a_vp->v_data; | |
| 2216 | modflags = 0; | |
| fbb84158 | 2217 | kflags = 0; |
| b0aab9b9 | 2218 | hmp = ip->hmp; |
| 8cd0a023 MD |
2219 | |
| 2220 | if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) | |
| 2221 | return(EROFS); | |
| d113fda1 MD |
2222 | if (ip->flags & HAMMER_INODE_RO) |
| 2223 | return (EROFS); | |
| e63644f0 | 2224 | if (hammer_nohistory(ip) == 0 && |
| b0aab9b9 | 2225 | (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) { |
| e63644f0 MD |
2226 | return (error); |
| 2227 | } | |
| 8cd0a023 | 2228 | |
| b0aab9b9 MD |
2229 | lwkt_gettoken(&hmp->fs_token); |
| 2230 | hammer_start_transaction(&trans, hmp); | |
| ce0138a6 | 2231 | ++hammer_stats_file_iopsw; |
| 8cd0a023 MD |
2232 | error = 0; |
| 2233 | ||
| 2234 | if (vap->va_flags != VNOVAL) { | |
| 2235 | flags = ip->ino_data.uflags; | |
| 2236 | error = vop_helper_setattr_flags(&flags, vap->va_flags, | |
| 2237 | hammer_to_unix_xid(&ip->ino_data.uid), | |
| 2238 | ap->a_cred); | |
| 2239 | if (error == 0) { | |
| 2240 | if (ip->ino_data.uflags != flags) { | |
| 2241 | ip->ino_data.uflags = flags; | |
| cc0758d0 | 2242 | ip->ino_data.ctime = trans.time; |
| 8cd0a023 | 2243 | modflags |= HAMMER_INODE_DDIRTY; |
| fbb84158 | 2244 | kflags |= NOTE_ATTRIB; |
| 8cd0a023 MD |
2245 | } |
| 2246 | if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) { | |
| 2247 | error = 0; | |
| 2248 | goto done; | |
| 2249 | } | |
| 2250 | } | |
| 2251 | goto done; | |
| 2252 | } | |
| 2253 | if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) { | |
| 2254 | error = EPERM; | |
| 2255 | goto done; | |
| 2256 | } | |
| 7538695e MD |
2257 | if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) { |
| 2258 | mode_t cur_mode = ip->ino_data.mode; | |
| 2259 | uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid); | |
| 2260 | gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid); | |
| 2261 | uuid_t uuid_uid; | |
| 2262 | uuid_t uuid_gid; | |
| 2263 | ||
| 2264 | error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid, | |
| 2265 | ap->a_cred, | |
| 2266 | &cur_uid, &cur_gid, &cur_mode); | |
| 2267 | if (error == 0) { | |
| 2268 | hammer_guid_to_uuid(&uuid_uid, cur_uid); | |
| 2269 | hammer_guid_to_uuid(&uuid_gid, cur_gid); | |
| 2270 | if (bcmp(&uuid_uid, &ip->ino_data.uid, | |
| 2271 | sizeof(uuid_uid)) || | |
| 2272 | bcmp(&uuid_gid, &ip->ino_data.gid, | |
| 2273 | sizeof(uuid_gid)) || | |
| 2274 | ip->ino_data.mode != cur_mode | |
| 2275 | ) { | |
| 2276 | ip->ino_data.uid = uuid_uid; | |
| 2277 | ip->ino_data.gid = uuid_gid; | |
| 2278 | ip->ino_data.mode = cur_mode; | |
| cc0758d0 MD |
2279 | ip->ino_data.ctime = trans.time; |
| 2280 | modflags |= HAMMER_INODE_DDIRTY; | |
| 7538695e | 2281 | } |
| fbb84158 | 2282 | kflags |= NOTE_ATTRIB; |
| 8cd0a023 MD |
2283 | } |
| 2284 | } | |
| 11ad5ade | 2285 | while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) { |
| 8cd0a023 MD |
2286 | switch(ap->a_vp->v_type) { |
| 2287 | case VREG: | |
| 11ad5ade | 2288 | if (vap->va_size == ip->ino_data.size) |
| d5ef456e | 2289 | break; |
| 47f363f1 MD |
2290 | |
| 2291 | /* | |
| c58123da MD |
2292 | * Log the operation if in fast-fsync mode or if |
| 2293 | * there are unterminated redo write records present. | |
| 2294 | * | |
| 2295 | * The second check is needed so the recovery code | |
| 2296 | * properly truncates write redos even if nominal | |
| 2297 | * REDO operations is turned off due to excessive | |
| 2298 | * writes, because the related records might be | |
| 2299 | * destroyed and never lay down a TERM_WRITE. | |
| 47f363f1 | 2300 | */ |
| c58123da MD |
2301 | if ((ip->flags & HAMMER_INODE_REDO) || |
| 2302 | (ip->flags & HAMMER_INODE_RDIRTY)) { | |
| 47f363f1 MD |
2303 | error = hammer_generate_redo(&trans, ip, |
| 2304 | vap->va_size, | |
| 2305 | HAMMER_REDO_TRUNC, | |
| 2306 | NULL, 0); | |
| 2307 | } | |
| 2308 | blksize = hammer_blocksize(vap->va_size); | |
| 2309 | ||
| b84de5af MD |
2310 | /* |
| 2311 | * XXX break atomicy, we can deadlock the backend | |
| 2312 | * if we do not release the lock. Probably not a | |
| 2313 | * big deal here. | |
| 2314 | */ | |
| 11ad5ade | 2315 | if (vap->va_size < ip->ino_data.size) { |
| 6362a262 MD |
2316 | nvtruncbuf(ap->a_vp, vap->va_size, |
| 2317 | blksize, | |
| 753df37e MD |
2318 | hammer_blockoff(vap->va_size), |
| 2319 | 0); | |
| d5ef456e | 2320 | truncating = 1; |
| fbb84158 | 2321 | kflags |= NOTE_WRITE; |
| d5ef456e | 2322 | } else { |
| 6362a262 MD |
2323 | nvextendbuf(ap->a_vp, |
| 2324 | ip->ino_data.size, | |
| 2325 | vap->va_size, | |
| 2326 | hammer_blocksize(ip->ino_data.size), | |
| 2327 | hammer_blocksize(vap->va_size), | |
| 2328 | hammer_blockoff(ip->ino_data.size), | |
| 2329 | hammer_blockoff(vap->va_size), | |
| 2330 | 0); | |
| d5ef456e | 2331 | truncating = 0; |
| fbb84158 | 2332 | kflags |= NOTE_WRITE | NOTE_EXTEND; |
| c0ade690 | 2333 | } |
| 11ad5ade | 2334 | ip->ino_data.size = vap->va_size; |
| cc0758d0 | 2335 | ip->ino_data.mtime = trans.time; |
| 47f363f1 | 2336 | /* XXX safe to use SDIRTY instead of DDIRTY here? */ |
| cc0758d0 | 2337 | modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY; |
| d5ef456e | 2338 | |
| b84de5af | 2339 | /* |
| 6362a262 MD |
2340 | * On-media truncation is cached in the inode until |
| 2341 | * the inode is synchronized. We must immediately | |
| 2342 | * handle any frontend records. | |
| b84de5af | 2343 | */ |
| d5ef456e | 2344 | if (truncating) { |
| 47637bff | 2345 | hammer_ip_frontend_trunc(ip, vap->va_size); |
| 0832c9bb MD |
2346 | #ifdef DEBUG_TRUNCATE |
| 2347 | if (HammerTruncIp == NULL) | |
| 2348 | HammerTruncIp = ip; | |
| 2349 | #endif | |
| b84de5af MD |
2350 | if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) { |
| 2351 | ip->flags |= HAMMER_INODE_TRUNCATED; | |
| 2352 | ip->trunc_off = vap->va_size; | |
| 0832c9bb MD |
2353 | #ifdef DEBUG_TRUNCATE |
| 2354 | if (ip == HammerTruncIp) | |
| 973c11b9 MD |
2355 | kprintf("truncate1 %016llx\n", |
| 2356 | (long long)ip->trunc_off); | |
| 0832c9bb | 2357 | #endif |
| b84de5af MD |
2358 | } else if (ip->trunc_off > vap->va_size) { |
| 2359 | ip->trunc_off = vap->va_size; | |
| 0832c9bb MD |
2360 | #ifdef DEBUG_TRUNCATE |
| 2361 | if (ip == HammerTruncIp) | |
| 973c11b9 MD |
2362 | kprintf("truncate2 %016llx\n", |
| 2363 | (long long)ip->trunc_off); | |
| 0832c9bb MD |
2364 | #endif |
| 2365 | } else { | |
| 2366 | #ifdef DEBUG_TRUNCATE | |
| 2367 | if (ip == HammerTruncIp) | |
| 973c11b9 MD |
2368 | kprintf("truncate3 %016llx (ignored)\n", |
| 2369 | (long long)vap->va_size); | |
| 0832c9bb | 2370 | #endif |
| b84de5af | 2371 | } |
| d5ef456e | 2372 | } |
| b84de5af | 2373 | |
| 6362a262 | 2374 | #if 0 |
| d5ef456e | 2375 | /* |
| 6362a262 MD |
2376 | * When truncating, nvtruncbuf() may have cleaned out |
| 2377 | * a portion of the last block on-disk in the buffer | |
| 2378 | * cache. We must clean out any frontend records | |
| 2379 | * for blocks beyond the new last block. | |
| d5ef456e | 2380 | */ |
| 4a2796f3 MD |
2381 | aligned_size = (vap->va_size + (blksize - 1)) & |
| 2382 | ~(int64_t)(blksize - 1); | |
| b84de5af | 2383 | if (truncating && vap->va_size < aligned_size) { |
| 4a2796f3 | 2384 | aligned_size -= blksize; |
| 47637bff | 2385 | hammer_ip_frontend_trunc(ip, aligned_size); |
| d5ef456e | 2386 | } |
| 6362a262 | 2387 | #endif |
| 76376933 | 2388 | break; |
| 8cd0a023 | 2389 | case VDATABASE: |
| b84de5af MD |
2390 | if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) { |
| 2391 | ip->flags |= HAMMER_INODE_TRUNCATED; | |
| 2392 | ip->trunc_off = vap->va_size; | |
| 2393 | } else if (ip->trunc_off > vap->va_size) { | |
| 2394 | ip->trunc_off = vap->va_size; | |
| 2395 | } | |
| 47637bff | 2396 | hammer_ip_frontend_trunc(ip, vap->va_size); |
| 11ad5ade | 2397 | ip->ino_data.size = vap->va_size; |
| cc0758d0 MD |
2398 | ip->ino_data.mtime = trans.time; |
| 2399 | modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY; | |
| fbb84158 | 2400 | kflags |= NOTE_ATTRIB; |
| 8cd0a023 MD |
2401 | break; |
| 2402 | default: | |
| 2403 | error = EINVAL; | |
| 2404 | goto done; | |
| 2405 | } | |
| d26d0ae9 | 2406 | break; |
| 8cd0a023 MD |
2407 | } |
| 2408 | if (vap->va_atime.tv_sec != VNOVAL) { | |
| cc0758d0 | 2409 | ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime); |
| ddfdf542 | 2410 | modflags |= HAMMER_INODE_ATIME; |
| fbb84158 | 2411 | kflags |= NOTE_ATTRIB; |
| 8cd0a023 MD |
2412 | } |
| 2413 | if (vap->va_mtime.tv_sec != VNOVAL) { | |
| cc0758d0 | 2414 | ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime); |
| ddfdf542 | 2415 | modflags |= HAMMER_INODE_MTIME; |
| fbb84158 | 2416 | kflags |= NOTE_ATTRIB; |
| 8cd0a023 MD |
2417 | } |
| 2418 | if (vap->va_mode != (mode_t)VNOVAL) { | |
| 7538695e MD |
2419 | mode_t cur_mode = ip->ino_data.mode; |
| 2420 | uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid); | |
| 2421 | gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid); | |
| 2422 | ||
| 2423 | error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred, | |
| 2424 | cur_uid, cur_gid, &cur_mode); | |
| 2425 | if (error == 0 && ip->ino_data.mode != cur_mode) { | |
| 2426 | ip->ino_data.mode = cur_mode; | |
| cc0758d0 | 2427 | ip->ino_data.ctime = trans.time; |
| 8cd0a023 | 2428 | modflags |= HAMMER_INODE_DDIRTY; |
| fbb84158 | 2429 | kflags |= NOTE_ATTRIB; |
| 8cd0a023 MD |
2430 | } |
| 2431 | } | |
| 2432 | done: | |
| b84de5af | 2433 | if (error == 0) |
| e98f1b96 | 2434 | hammer_modify_inode(&trans, ip, modflags); |
| b84de5af | 2435 | hammer_done_transaction(&trans); |
| fbb84158 | 2436 | hammer_knote(ap->a_vp, kflags); |
| b0aab9b9 | 2437 | lwkt_reltoken(&hmp->fs_token); |
| 8cd0a023 | 2438 | return (error); |
| 427e5fc6 MD |
2439 | } |
| 2440 | ||
| 66325755 MD |
2441 | /* |
| 2442 | * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target } | |
| 2443 | */ | |
| 427e5fc6 MD |
2444 | static |
| 2445 | int | |
| 66325755 | 2446 | hammer_vop_nsymlink(struct vop_nsymlink_args *ap) |
| 427e5fc6 | 2447 | { |
| 7a04d74f MD |
2448 | struct hammer_transaction trans; |
| 2449 | struct hammer_inode *dip; | |
| 2450 | struct hammer_inode *nip; | |
| 7a04d74f | 2451 | hammer_record_t record; |
| b0aab9b9 MD |
2452 | struct nchandle *nch; |
| 2453 | hammer_mount_t hmp; | |
| 7a04d74f MD |
2454 | int error; |
| 2455 | int bytes; | |
| 2456 | ||
| 2457 | ap->a_vap->va_type = VLNK; | |
| 2458 | ||
| 2459 | nch = ap->a_nch; | |
| 2460 | dip = VTOI(ap->a_dvp); | |
| b0aab9b9 | 2461 | hmp = dip->hmp; |
| 7a04d74f | 2462 | |
| d113fda1 MD |
2463 | if (dip->flags & HAMMER_INODE_RO) |
| 2464 | return (EROFS); | |
| b0aab9b9 | 2465 | if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) |
| e63644f0 | 2466 | return (error); |
| d113fda1 | 2467 | |
| 7a04d74f MD |
2468 | /* |
| 2469 | * Create a transaction to cover the operations we perform. | |
| 2470 | */ | |
| b0aab9b9 MD |
2471 | lwkt_gettoken(&hmp->fs_token); |
| 2472 | hammer_start_transaction(&trans, hmp); | |
| ce0138a6 | 2473 | ++hammer_stats_file_iopsw; |
| 7a04d74f MD |
2474 | |
| 2475 | /* | |
| 2476 | * Create a new filesystem object of the requested type. The | |
| 2477 | * returned inode will be referenced but not locked. | |
| 2478 | */ | |
| 2479 | ||
| 5a930e66 | 2480 | error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, |
| 5a64efa1 MD |
2481 | dip, nch->ncp->nc_name, nch->ncp->nc_nlen, |
| 2482 | NULL, &nip); | |
| 7a04d74f | 2483 | if (error) { |
| b84de5af | 2484 | hammer_done_transaction(&trans); |
| 7a04d74f | 2485 | *ap->a_vpp = NULL; |
| b0aab9b9 | 2486 | lwkt_reltoken(&hmp->fs_token); |
| 7a04d74f MD |
2487 | return (error); |
| 2488 | } | |
| 2489 | ||
| 2490 | /* | |
| 7a04d74f MD |
2491 | * Add a record representing the symlink. symlink stores the link |
| 2492 | * as pure data, not a string, and is no \0 terminated. | |
| 2493 | */ | |
| 2494 | if (error == 0) { | |
| 7a04d74f MD |
2495 | bytes = strlen(ap->a_target); |
| 2496 | ||
| 2f85fa4d MD |
2497 | if (bytes <= HAMMER_INODE_BASESYMLEN) { |
| 2498 | bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes); | |
| 2499 | } else { | |
| 2500 | record = hammer_alloc_mem_record(nip, bytes); | |
| 2501 | record->type = HAMMER_MEM_RECORD_GENERAL; | |
| 2502 | ||
| 5a930e66 MD |
2503 | record->leaf.base.localization = nip->obj_localization + |
| 2504 | HAMMER_LOCALIZE_MISC; | |
| 2f85fa4d MD |
2505 | record->leaf.base.key = HAMMER_FIXKEY_SYMLINK; |
| 2506 | record->leaf.base.rec_type = HAMMER_RECTYPE_FIX; | |
| 2507 | record->leaf.data_len = bytes; | |
| 2508 | KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0); | |
| 2509 | bcopy(ap->a_target, record->data->symlink.name, bytes); | |
| 2510 | error = hammer_ip_add_record(&trans, record); | |
| 2511 | } | |
| 42c7d26b MD |
2512 | |
| 2513 | /* | |
| 2514 | * Set the file size to the length of the link. | |
| 2515 | */ | |
| 2516 | if (error == 0) { | |
| 11ad5ade | 2517 | nip->ino_data.size = bytes; |
| e98f1b96 | 2518 | hammer_modify_inode(&trans, nip, HAMMER_INODE_DDIRTY); |
| 42c7d26b | 2519 | } |
| 7a04d74f | 2520 | } |
| 1f07f686 | 2521 | if (error == 0) |
| 5a930e66 MD |
2522 | error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name, |
| 2523 | nch->ncp->nc_nlen, nip); | |
| 7a04d74f MD |
2524 | |
| 2525 | /* | |
| 2526 | * Finish up. | |
| 2527 | */ | |
| 2528 | if (error) { | |
| 2529 | hammer_rel_inode(nip, 0); | |
| 7a04d74f MD |
2530 | *ap->a_vpp = NULL; |
| 2531 | } else { | |
| e8599db1 | 2532 | error = hammer_get_vnode(nip, ap->a_vpp); |
| 7a04d74f MD |
2533 | hammer_rel_inode(nip, 0); |
| 2534 | if (error == 0) { | |
| 2535 | cache_setunresolved(ap->a_nch); | |
| 2536 | cache_setvp(ap->a_nch, *ap->a_vpp); | |
| fbb84158 | 2537 | hammer_knote(ap->a_dvp, NOTE_WRITE); |
| 7a04d74f MD |
2538 | } |
| 2539 | } | |
| b84de5af | 2540 | hammer_done_transaction(&trans); |
| b0aab9b9 | 2541 | lwkt_reltoken(&hmp->fs_token); |
| 7a04d74f | 2542 | return (error); |
| 427e5fc6 MD |
2543 | } |
| 2544 | ||
| 66325755 MD |
2545 | /* |
| 2546 | * hammer_vop_nwhiteout { nch, dvp, cred, flags } | |
| 2547 | */ | |
| 427e5fc6 MD |
2548 | static |
| 2549 | int | |
| 66325755 | 2550 | hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap) |
| 427e5fc6 | 2551 | { |
| b84de5af | 2552 | struct hammer_transaction trans; |
| e63644f0 | 2553 | struct hammer_inode *dip; |
| b0aab9b9 | 2554 | hammer_mount_t hmp; |
| b84de5af MD |
2555 | int error; |
| 2556 | ||
| e63644f0 | 2557 | dip = VTOI(ap->a_dvp); |
| b0aab9b9 | 2558 | hmp = dip->hmp; |
| e63644f0 MD |
2559 | |
| 2560 | if (hammer_nohistory(dip) == 0 && | |
| b0aab9b9 | 2561 | (error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) { |
| e63644f0 MD |
2562 | return (error); |
| 2563 | } | |
| 2564 | ||
| b0aab9b9 MD |
2565 | lwkt_gettoken(&hmp->fs_token); |
| 2566 | hammer_start_transaction(&trans, hmp); | |
| ce0138a6 | 2567 | ++hammer_stats_file_iopsw; |
| b84de5af | 2568 | error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, |
| d7e278bb | 2569 | ap->a_cred, ap->a_flags, -1); |
| b84de5af | 2570 | hammer_done_transaction(&trans); |
| b0aab9b9 | 2571 | lwkt_reltoken(&hmp->fs_token); |
| b84de5af MD |
2572 | |
| 2573 | return (error); | |
| 427e5fc6 MD |
2574 | } |
| 2575 | ||
| 66325755 | 2576 | /* |
| 7dc57964 MD |
2577 | * hammer_vop_ioctl { vp, command, data, fflag, cred } |
| 2578 | */ | |
| 2579 | static | |
| 2580 | int | |
| 2581 | hammer_vop_ioctl(struct vop_ioctl_args *ap) | |
| 2582 | { | |
| 2583 | struct hammer_inode *ip = ap->a_vp->v_data; | |
| b0aab9b9 MD |
2584 | hammer_mount_t hmp = ip->hmp; |
| 2585 | int error; | |
| 7dc57964 | 2586 | |
| ce0138a6 | 2587 | ++hammer_stats_file_iopsr; |
| b0aab9b9 MD |
2588 | lwkt_gettoken(&hmp->fs_token); |
| 2589 | error = hammer_ioctl(ip, ap->a_command, ap->a_data, | |
| 2590 | ap->a_fflag, ap->a_cred); | |
| 2591 | lwkt_reltoken(&hmp->fs_token); | |
| 2592 | return (error); | |
| 7dc57964 MD |
2593 | } |
| 2594 | ||
| 513ca7d7 MD |
2595 | static |
| 2596 | int | |
| 2597 | hammer_vop_mountctl(struct vop_mountctl_args *ap) | |
| 2598 | { | |
| dad088a5 MD |
2599 | static const struct mountctl_opt extraopt[] = { |
| 2600 | { HMNT_NOHISTORY, "nohistory" }, | |
| 2601 | { HMNT_MASTERID, "master" }, | |
| 2602 | { 0, NULL} | |
| 2603 | ||
| 2604 | }; | |
| 2605 | struct hammer_mount *hmp; | |
| 513ca7d7 | 2606 | struct mount *mp; |
| dad088a5 | 2607 | int usedbytes; |
| 513ca7d7 MD |
2608 | int error; |
| 2609 | ||
| dad088a5 MD |
2610 | error = 0; |
| 2611 | usedbytes = 0; | |
| 513ca7d7 | 2612 | mp = ap->a_head.a_ops->head.vv_mount; |
| dad088a5 MD |
2613 | KKASSERT(mp->mnt_data != NULL); |
| 2614 | hmp = (struct hammer_mount *)mp->mnt_data; | |
| 513ca7d7 | 2615 | |
| b0aab9b9 | 2616 | lwkt_gettoken(&hmp->fs_token); |
| dad088a5 | 2617 | |
| b0aab9b9 | 2618 | switch(ap->a_op) { |
| 513ca7d7 MD |
2619 | case MOUNTCTL_SET_EXPORT: |
| 2620 | if (ap->a_ctllen != sizeof(struct export_args)) | |
| 2621 | error = EINVAL; | |
| b424ca30 MD |
2622 | else |
| 2623 | error = hammer_vfs_export(mp, ap->a_op, | |
| 513ca7d7 MD |
2624 | (const struct export_args *)ap->a_ctl); |
| 2625 | break; | |
| dad088a5 MD |
2626 | case MOUNTCTL_MOUNTFLAGS: |
| 2627 | { | |
| 2628 | /* | |
| 2629 | * Call standard mountctl VOP function | |
| 2630 | * so we get user mount flags. | |
| 2631 | */ | |
| 2632 | error = vop_stdmountctl(ap); | |
| 2633 | if (error) | |
| 2634 | break; | |
| 2635 | ||
| 2636 | usedbytes = *ap->a_res; | |
| 2637 | ||
| eac446c5 | 2638 | if (usedbytes > 0 && usedbytes < ap->a_buflen) { |
| b0aab9b9 MD |
2639 | usedbytes += vfs_flagstostr(hmp->hflags, extraopt, |
| 2640 | ap->a_buf, | |
| dad088a5 MD |
2641 | ap->a_buflen - usedbytes, |
| 2642 | &error); | |
| dad088a5 MD |
2643 | } |
| 2644 | ||
| 2645 | *ap->a_res += usedbytes; | |
| 2646 | break; | |
| 2647 | } | |
| 513ca7d7 | 2648 | default: |
| 726e0641 | 2649 | error = vop_stdmountctl(ap); |
| 513ca7d7 MD |
2650 | break; |
| 2651 | } | |
| b0aab9b9 | 2652 | lwkt_reltoken(&hmp->fs_token); |
| 513ca7d7 MD |
2653 | return(error); |
| 2654 | } | |
| 2655 | ||
| 7dc57964 | 2656 | /* |
| 66325755 | 2657 | * hammer_vop_strategy { vp, bio } |
| 8cd0a023 MD |
2658 | * |
| 2659 | * Strategy call, used for regular file read & write only. Note that the | |
| 2660 | * bp may represent a cluster. | |
| 2661 | * | |
| 2662 | * To simplify operation and allow better optimizations in the future, | |
| 2663 | * this code does not make any assumptions with regards to buffer alignment | |
| 2664 | * or size. | |
| 66325755 | 2665 | */ |
| 427e5fc6 MD |
2666 | static |
| 2667 | int | |
| 66325755 | 2668 | hammer_vop_strategy(struct vop_strategy_args *ap) |
| 427e5fc6 | 2669 | { |
| 8cd0a023 MD |
2670 | struct buf *bp; |
| 2671 | int error; | |
| 2672 | ||
| 2673 | bp = ap->a_bio->bio_buf; | |
| 2674 | ||
| 2675 | switch(bp->b_cmd) { | |
| 2676 | case BUF_CMD_READ: | |
| 2677 | error = hammer_vop_strategy_read(ap); | |
| 2678 | break; | |
| 2679 | case BUF_CMD_WRITE: | |
| 2680 | error = hammer_vop_strategy_write(ap); | |
| 2681 | break; | |
| 2682 | default: | |
| 059819e3 MD |
2683 | bp->b_error = error = EINVAL; |
| 2684 | bp->b_flags |= B_ERROR; | |
| 2685 | biodone(ap->a_bio); | |
| 8cd0a023 MD |
2686 | break; |
| 2687 | } | |
| 507df98a ID |
2688 | |
| 2689 | /* hammer_dump_dedup_cache(((hammer_inode_t)ap->a_vp->v_data)->hmp); */ | |
| 2690 | ||
| 8cd0a023 | 2691 | return (error); |
| 427e5fc6 MD |
2692 | } |
| 2693 | ||
| 8cd0a023 MD |
2694 | /* |
| 2695 | * Read from a regular file. Iterate the related records and fill in the | |
| 2696 | * BIO/BUF. Gaps are zero-filled. | |
| 2697 | * | |
| 2698 | * The support code in hammer_object.c should be used to deal with mixed | |
| 2699 | * in-memory and on-disk records. | |
| 2700 | * | |
| 4a2796f3 MD |
2701 | * NOTE: Can be called from the cluster code with an oversized buf. |
| 2702 | * | |
| 8cd0a023 MD |
2703 | * XXX atime update |
| 2704 | */ | |
| 2705 | static | |
| 2706 | int | |
| 2707 | hammer_vop_strategy_read(struct vop_strategy_args *ap) | |
| 2708 | { | |
| 36f82b23 MD |
2709 | struct hammer_transaction trans; |
| 2710 | struct hammer_inode *ip; | |
| 39d8fd63 | 2711 | struct hammer_inode *dip; |
| b0aab9b9 | 2712 | hammer_mount_t hmp; |
| 8cd0a023 | 2713 | struct hammer_cursor cursor; |
| 8cd0a023 | 2714 | hammer_base_elm_t base; |
| 4a2796f3 | 2715 | hammer_off_t disk_offset; |
| 8cd0a023 | 2716 | struct bio *bio; |
| a99b9ea2 | 2717 | struct bio *nbio; |
| 8cd0a023 MD |
2718 | struct buf *bp; |
| 2719 | int64_t rec_offset; | |
| a89aec1b | 2720 | int64_t ran_end; |
| 195c19a1 | 2721 | int64_t tmp64; |
| 8cd0a023 MD |
2722 | int error; |
| 2723 | int boff; | |
| 2724 | int roff; | |
| 2725 | int n; | |
| b4f86ea3 | 2726 | int isdedupable; |
| 8cd0a023 MD |
2727 | |
| 2728 | bio = ap->a_bio; | |
| 2729 | bp = bio->bio_buf; | |
| 36f82b23 | 2730 | ip = ap->a_vp->v_data; |
| b0aab9b9 | 2731 | hmp = ip->hmp; |
| 8cd0a023 | 2732 | |
| a99b9ea2 MD |
2733 | /* |
| 2734 | * The zone-2 disk offset may have been set by the cluster code via | |
| 4a2796f3 | 2735 | * a BMAP operation, or else should be NOOFFSET. |
| a99b9ea2 | 2736 | * |
| 4a2796f3 | 2737 | * Checking the high bits for a match against zone-2 should suffice. |
| b4f86ea3 MD |
2738 | * |
| 2739 | * In cases where a lot of data duplication is present it may be | |
| 2740 | * more beneficial to drop through and doubule-buffer through the | |
| 2741 | * device. | |
| a99b9ea2 MD |
2742 | */ |
| 2743 | nbio = push_bio(bio); | |
| 9a98f3cc | 2744 | if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) == |
| 1b0ab2c3 | 2745 | HAMMER_ZONE_LARGE_DATA) { |
| 9a98f3cc MD |
2746 | if (hammer_double_buffer == 0) { |
| 2747 | lwkt_gettoken(&hmp->fs_token); | |
| 2748 | error = hammer_io_direct_read(hmp, nbio, NULL); | |
| 2749 | lwkt_reltoken(&hmp->fs_token); | |
| 2750 | return (error); | |
| 2751 | } | |
| 2752 | ||
| 2753 | /* | |
| 2754 | * Try to shortcut requests for double_buffer mode too. | |
| 2755 | * Since this mode runs through the device buffer cache | |
| 2756 | * only compatible buffer sizes (meaning those generated | |
| 2757 | * by normal filesystem buffers) are legal. | |
| 2758 | */ | |
| 2759 | if (hammer_live_dedup == 0 && (bp->b_flags & B_PAGING) == 0) { | |
| 2760 | error = hammer_io_indirect_read(hmp, nbio, NULL); | |
| 2761 | return (error); | |
| 2762 | } | |
| a99b9ea2 MD |
2763 | } |
| 2764 | ||
| 2765 | /* | |
| 4a2796f3 MD |
2766 | * Well, that sucked. Do it the hard way. If all the stars are |
| 2767 | * aligned we may still be able to issue a direct-read. | |
| a99b9ea2 | 2768 | */ |
| b0aab9b9 MD |
2769 | lwkt_gettoken(&hmp->fs_token); |
| 2770 | hammer_simple_transaction(&trans, hmp); | |
| 47637bff | 2771 | hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip); |
| 8cd0a023 MD |
2772 | |
| 2773 | /* | |
| 2774 | * Key range (begin and end inclusive) to scan. Note that the key's | |
| c0ade690 MD |
2775 | * stored in the actual records represent BASE+LEN, not BASE. The |
| 2776 | * first record containing bio_offset will have a key > bio_offset. | |
| 8cd0a023 | 2777 | */ |
| 5a930e66 MD |
2778 | cursor.key_beg.localization = ip->obj_localization + |
| 2779 | HAMMER_LOCALIZE_MISC; | |
| 8cd0a023 | 2780 | cursor.key_beg.obj_id = ip->obj_id; |
| d5530d22 | 2781 | cursor.key_beg.create_tid = 0; |
| 8cd0a023 | 2782 | cursor.key_beg.delete_tid = 0; |
| 8cd0a023 | 2783 | cursor.key_beg.obj_type = 0; |
| c0ade690 | 2784 | cursor.key_beg.key = bio->bio_offset + 1; |
| d5530d22 | 2785 | cursor.asof = ip->obj_asof; |
| bf3b416b | 2786 | cursor.flags |= HAMMER_CURSOR_ASOF; |
| 8cd0a023 MD |
2787 | |
| 2788 | cursor.key_end = cursor.key_beg; | |
| 11ad5ade | 2789 | KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE); |
| b84de5af | 2790 | #if 0 |
| 11ad5ade | 2791 | if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) { |
| a89aec1b MD |
2792 | cursor.key_beg.rec_type = HAMMER_RECTYPE_DB; |
| 2793 | cursor.key_end.rec_type = HAMMER_RECTYPE_DB; | |
| 2794 | cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL; | |
| b84de5af MD |
2795 | } else |
| 2796 | #endif | |
| 2797 | { | |
| c0ade690 | 2798 | ran_end = bio->bio_offset + bp->b_bufsize; |
| a89aec1b MD |
2799 | cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA; |
| 2800 | cursor.key_end.rec_type = HAMMER_RECTYPE_DATA; | |
| 195c19a1 MD |
2801 | tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */ |
| 2802 | if (tmp64 < ran_end) | |
| a89aec1b MD |
2803 | cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL; |
| 2804 | else | |
| 7f7c1f84 | 2805 | cursor.key_end.key = ran_end + MAXPHYS + 1; |
| a89aec1b | 2806 | } |
| d26d0ae9 | 2807 | cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE; |
| 8cd0a023 | 2808 | |
| 18bee4a2 MD |
2809 | /* |
| 2810 | * Set NOSWAPCACHE for cursor data extraction if double buffering | |
| 2811 | * is disabled or (if the file is not marked cacheable via chflags | |
| 2812 | * and vm.swapcache_use_chflags is enabled). | |
| 2813 | */ | |
| 2814 | if (hammer_double_buffer == 0 || | |
| 2815 | ((ap->a_vp->v_flag & VSWAPCACHE) == 0 && | |
| 2816 | vm_swapcache_use_chflags)) { | |
| 2817 | cursor.flags |= HAMMER_CURSOR_NOSWAPCACHE; | |
| 2818 | } | |
| 2819 | ||
| 4e17f465 | 2820 | error = hammer_ip_first(&cursor); |
| 8cd0a023 MD |
2821 | boff = 0; |
| 2822 | ||
| a89aec1b | 2823 | while (error == 0) { |
| 47637bff MD |
2824 | /* |
| 2825 | * Get the base file offset of the record. The key for | |
| 2826 | * data records is (base + bytes) rather then (base). | |
| 2827 | */ | |
| 11ad5ade | 2828 | base = &cursor.leaf->base; |
| 11ad5ade | 2829 | rec_offset = base->key - cursor.leaf->data_len; |
| 8cd0a023 | 2830 | |
| 66325755 | 2831 | /* |
| a89aec1b | 2832 | * Calculate the gap, if any, and zero-fill it. |
| 1fef775e MD |
2833 | * |
| 2834 | * n is the offset of the start of the record verses our | |
| 2835 | * current seek offset in the bio. | |
| 66325755 | 2836 | */ |
| 8cd0a023 MD |
2837 | n = (int)(rec_offset - (bio->bio_offset + boff)); |
| 2838 | if (n > 0) { | |
| a89aec1b MD |
2839 | if (n > bp->b_bufsize - boff) |
| 2840 | n = bp->b_bufsize - boff; | |
| 8cd0a023 MD |
2841 | bzero((char *)bp->b_data + boff, n); |
| 2842 | boff += n; | |
| 2843 | n = 0; | |
| 66325755 | 2844 | } |
| 8cd0a023 MD |
2845 | |
| 2846 | /* | |
| 2847 | * Calculate the data offset in the record and the number | |
| 2848 | * of bytes we can copy. | |
| a89aec1b | 2849 | * |
| 1fef775e MD |
2850 | * There are two degenerate cases. First, boff may already |
| 2851 | * be at bp->b_bufsize. Secondly, the data offset within | |
| 2852 | * the record may exceed the record's size. | |
| 8cd0a023 MD |
2853 | */ |
| 2854 | roff = -n; | |
| b84de5af | 2855 | rec_offset += roff; |
| 11ad5ade | 2856 | n = cursor.leaf->data_len - roff; |
| 1fef775e MD |
2857 | if (n <= 0) { |
| 2858 | kprintf("strategy_read: bad n=%d roff=%d\n", n, roff); | |
| 2859 | n = 0; | |
| 2860 | } else if (n > bp->b_bufsize - boff) { | |
| 8cd0a023 | 2861 | n = bp->b_bufsize - boff; |
| 1fef775e | 2862 | } |
| 059819e3 | 2863 | |
| b84de5af | 2864 | /* |
| 47637bff MD |
2865 | * Deal with cached truncations. This cool bit of code |
| 2866 | * allows truncate()/ftruncate() to avoid having to sync | |
| 2867 | * the file. | |
| 2868 | * | |
| 2869 | * If the frontend is truncated then all backend records are | |
| 2870 | * subject to the frontend's truncation. | |
| 2871 | * | |
| 2872 | * If the backend is truncated then backend records on-disk | |
| 2873 | * (but not in-memory) are subject to the backend's | |
| 2874 | * truncation. In-memory records owned by the backend | |
| 2875 | * represent data written after the truncation point on the | |
| 2876 | * backend and must not be truncated. | |
| 2877 | * | |
| 2878 | * Truncate operations deal with frontend buffer cache | |
| 2879 | * buffers and frontend-owned in-memory records synchronously. | |
| b84de5af | 2880 | */ |
| 47637bff | 2881 | if (ip->flags & HAMMER_INODE_TRUNCATED) { |
| 6362a262 MD |
2882 | if (hammer_cursor_ondisk(&cursor)/* || |
| 2883 | cursor.iprec->flush_state == HAMMER_FST_FLUSH*/) { | |
| 47637bff MD |
2884 | if (ip->trunc_off <= rec_offset) |
| 2885 | n = 0; | |
| 2886 | else if (ip->trunc_off < rec_offset + n) | |
| 2887 | n = (int)(ip->trunc_off - rec_offset); | |
| 2888 | } | |
| 2889 | } | |
| 2890 | if (ip->sync_flags & HAMMER_INODE_TRUNCATED) { | |
| 2891 | if (hammer_cursor_ondisk(&cursor)) { | |
| 2892 | if (ip->sync_trunc_off <= rec_offset) | |
| 2893 | n = 0; | |
| 2894 | else if (ip->sync_trunc_off < rec_offset + n) | |
| 2895 | n = (int)(ip->sync_trunc_off - rec_offset); | |
| 2896 | } | |
| 2897 | } | |
| b84de5af MD |
2898 | |
| 2899 | /* | |
| 47637bff MD |
2900 | * Try to issue a direct read into our bio if possible, |
| 2901 | * otherwise resolve the element data into a hammer_buffer | |
| 2902 | * and copy. | |
| 4a2796f3 MD |
2903 | * |
| 2904 | * The buffer on-disk should be zerod past any real | |
| 2905 | * truncation point, but may not be for any synthesized | |
| 2906 | * truncation point from above. | |
| 9a98f3cc MD |
2907 | * |
| 2908 | * NOTE: disk_offset is only valid if the cursor data is | |
| 2909 | * on-disk. | |
| b84de5af | 2910 | */ |
| 1b0ab2c3 | 2911 | disk_offset = cursor.leaf->data_offset + roff; |
| b4f86ea3 MD |
2912 | isdedupable = (boff == 0 && n == bp->b_bufsize && |
| 2913 | hammer_cursor_ondisk(&cursor) && | |
| 2914 | ((int)disk_offset & HAMMER_BUFMASK) == 0); | |
| 2915 | ||
| 2916 | if (isdedupable && hammer_double_buffer == 0) { | |
| 9a98f3cc MD |
2917 | /* |
| 2918 | * Direct read case | |
| 2919 | */ | |
| 1b0ab2c3 MD |
2920 | KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) == |
| 2921 | HAMMER_ZONE_LARGE_DATA); | |
| 4a2796f3 | 2922 | nbio->bio_offset = disk_offset; |
| b0aab9b9 | 2923 | error = hammer_io_direct_read(hmp, nbio, cursor.leaf); |
| b4f86ea3 | 2924 | if (hammer_live_dedup && error == 0) |
| 507df98a | 2925 | hammer_dedup_cache_add(ip, cursor.leaf); |
| 47637bff | 2926 | goto done; |
| 9a98f3cc MD |
2927 | } else if (isdedupable) { |
| 2928 | /* | |
| 2929 | * Async I/O case for reading from backing store | |
| 2930 | * and copying the data to the filesystem buffer. | |
| 2931 | * live-dedup has to verify the data anyway if it | |
| 2932 | * gets a hit later so we can just add the entry | |
| 2933 | * now. | |
| 2934 | */ | |
| 2935 | KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) == | |
| 2936 | HAMMER_ZONE_LARGE_DATA); | |
| 2937 | nbio->bio_offset = disk_offset; | |
| 2938 | if (hammer_live_dedup) | |
| 2939 | hammer_dedup_cache_add(ip, cursor.leaf); | |
| 2940 | error = hammer_io_indirect_read(hmp, nbio, cursor.leaf); | |
| 2941 | goto done; | |
| 47637bff MD |
2942 | } else if (n) { |
| 2943 | error = hammer_ip_resolve_data(&cursor); | |
| 2944 | if (error == 0) { | |
| b4f86ea3 MD |
2945 | if (hammer_live_dedup && isdedupable) |
| 2946 | hammer_dedup_cache_add(ip, cursor.leaf); | |
| 47637bff MD |
2947 | bcopy((char *)cursor.data + roff, |
| 2948 | (char *)bp->b_data + boff, n); | |
| 2949 | } | |
| b84de5af | 2950 | } |
| 47637bff MD |
2951 | if (error) |
| 2952 | break; | |
| 2953 | ||
| 2954 | /* | |
| 507df98a ID |
2955 | * We have to be sure that the only elements added to the |
| 2956 | * dedup cache are those which are already on-media. | |
| 2957 | */ | |
| 2958 | if (hammer_live_dedup && hammer_cursor_ondisk(&cursor)) | |
| 2959 | hammer_dedup_cache_add(ip, cursor.leaf); | |
| 2960 | ||
| 2961 | /* | |
| 47637bff MD |
2962 | * Iterate until we have filled the request. |
| 2963 | */ | |
| 2964 | boff += n; | |
| 8cd0a023 | 2965 | if (boff == bp->b_bufsize) |
| 66325755 | 2966 | break; |
| a89aec1b | 2967 | error = hammer_ip_next(&cursor); |
| 66325755 MD |
2968 | } |
| 2969 | ||
| 2970 | /* | |
| 8cd0a023 | 2971 | * There may have been a gap after the last record |
| 66325755 | 2972 | */ |
| 8cd0a023 MD |
2973 | if (error == ENOENT) |
| 2974 | error = 0; | |
| 2975 | if (error == 0 && boff != bp->b_bufsize) { | |
| 7f7c1f84 | 2976 | KKASSERT(boff < bp->b_bufsize); |
| 8cd0a023 MD |
2977 | bzero((char *)bp->b_data + boff, bp->b_bufsize - boff); |
| 2978 | /* boff = bp->b_bufsize; */ | |
| 2979 | } | |
| 18bee4a2 MD |
2980 | |
| 2981 | /* | |
| 2982 | * Disallow swapcache operation on the vnode buffer if double | |
| 2983 | * buffering is enabled, the swapcache will get the data via | |
| 2984 | * the block device buffer. | |
| 2985 | */ | |
| 2986 | if (hammer_double_buffer) | |
| 2987 | bp->b_flags |= B_NOTMETA; | |
| 2988 | ||
| 2989 | /* | |
| 2990 | * Cleanup | |
| 2991 | */ | |
| 8cd0a023 | 2992 | bp->b_resid = 0; |
| 059819e3 MD |
2993 | bp->b_error = error; |
| 2994 | if (error) | |
| 2995 | bp->b_flags |= B_ERROR; | |
| 2996 | biodone(ap->a_bio); | |
| 47637bff MD |
2997 | |
| 2998 | done: | |
| 39d8fd63 MD |
2999 | /* |
| 3000 | * Cache the b-tree node for the last data read in cache[1]. | |
| 3001 | * | |
| 3002 | * If we hit the file EOF then also cache the node in the | |
| 3003 | * governing director's cache[3], it will be used to initialize | |
| 3004 | * the inode's cache[1] for any inodes looked up via the directory. | |
| 3005 | * | |
| 3006 | * This doesn't reduce disk accesses since the B-Tree chain is | |
| 3007 | * likely cached, but it does reduce cpu overhead when looking | |
| 3008 | * up file offsets for cpdup/tar/cpio style iterations. | |
| 3009 | */ | |
| 47637bff | 3010 | if (cursor.node) |
| bcac4bbb | 3011 | hammer_cache_node(&ip->cache[1], cursor.node); |
| 39d8fd63 MD |
3012 | if (ran_end >= ip->ino_data.size) { |
| 3013 | dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id, | |
| 3014 | ip->obj_asof, ip->obj_localization); | |
| 3015 | if (dip) { | |
| 3016 | hammer_cache_node(&dip->cache[3], cursor.node); | |
| 3017 | hammer_rel_inode(dip, 0); | |
| 3018 | } | |
| 3019 | } | |
| 47637bff MD |
3020 | hammer_done_cursor(&cursor); |
| 3021 | hammer_done_transaction(&trans); | |
| b0aab9b9 | 3022 | lwkt_reltoken(&hmp->fs_token); |
| 8cd0a023 MD |
3023 | return(error); |
| 3024 | } | |
| 3025 | ||
| 3026 | /* | |
| a99b9ea2 MD |
3027 | * BMAP operation - used to support cluster_read() only. |
| 3028 | * | |
| 3029 | * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb) | |
| 3030 | * | |
| 3031 | * This routine may return EOPNOTSUPP if the opration is not supported for | |
| 3032 | * the specified offset. The contents of the pointer arguments do not | |
| 3033 | * need to be initialized in that case. | |
| 3034 | * | |
| 3035 | * If a disk address is available and properly aligned return 0 with | |
| 3036 | * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately | |
| 3037 | * to the run-length relative to that offset. Callers may assume that | |
| 3038 | * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently | |
| 3039 | * large, so return EOPNOTSUPP if it is not sufficiently large. | |
| 3040 | */ | |
| 3041 | static | |
| 3042 | int | |
| 3043 | hammer_vop_bmap(struct vop_bmap_args *ap) | |
| 3044 | { | |
| 3045 | struct hammer_transaction trans; | |
| 3046 | struct hammer_inode *ip; | |
| b0aab9b9 | 3047 | hammer_mount_t hmp; |
| a99b9ea2 MD |
3048 | struct hammer_cursor cursor; |
| 3049 | hammer_base_elm_t base; | |
| 3050 | int64_t rec_offset; | |
| 3051 | int64_t ran_end; | |
| 3052 | int64_t tmp64; | |
| 3053 | int64_t base_offset; | |
| 3054 | int64_t base_disk_offset; | |
| 3055 | int64_t last_offset; | |
| 3056 | hammer_off_t last_disk_offset; | |
| 3057 | hammer_off_t disk_offset; | |
| 3058 | int rec_len; | |
| 3059 | int error; | |
| 4a2796f3 | 3060 | int blksize; |
| a99b9ea2 | 3061 | |
| ce0138a6 | 3062 | ++hammer_stats_file_iopsr; |
| a99b9ea2 | 3063 | ip = ap->a_vp->v_data; |
| b0aab9b9 | 3064 | hmp = ip->hmp; |
| a99b9ea2 MD |
3065 | |
| 3066 | /* | |
| 3067 | * We can only BMAP regular files. We can't BMAP database files, | |
| 3068 | * directories, etc. | |
| 3069 | */ | |
| 3070 | if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE) | |
| 3071 | return(EOPNOTSUPP); | |
| 3072 | ||
| 3073 | /* | |
| 3074 | * bmap is typically called with runp/runb both NULL when used | |
| 3075 | * for writing. We do not support BMAP for writing atm. | |
| 3076 | */ | |
| 4a2796f3 | 3077 | if (ap->a_cmd != BUF_CMD_READ) |
| a99b9ea2 MD |
3078 | return(EOPNOTSUPP); |
| 3079 | ||
| 3080 | /* | |
| 3081 | * Scan the B-Tree to acquire blockmap addresses, then translate | |
| 3082 | * to raw addresses. | |
| 3083 | */ | |
| b0aab9b9 MD |
3084 | lwkt_gettoken(&hmp->fs_token); |
| 3085 | hammer_simple_transaction(&trans, hmp); | |
| cb51be26 | 3086 | #if 0 |
| 973c11b9 MD |
3087 | kprintf("bmap_beg %016llx ip->cache %p\n", |
| 3088 | (long long)ap->a_loffset, ip->cache[1]); | |
| cb51be26 | 3089 | #endif |
| a99b9ea2 MD |
3090 | hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip); |
| 3091 | ||
| 3092 | /* | |
| 3093 | * Key range (begin and end inclusive) to scan. Note that the key's | |
| 3094 | * stored in the actual records represent BASE+LEN, not BASE. The | |
| 3095 | * first record containing bio_offset will have a key > bio_offset. | |
| 3096 | */ | |
| 5a930e66 MD |
3097 | cursor.key_beg.localization = ip->obj_localization + |
| 3098 | HAMMER_LOCALIZE_MISC; | |
| a99b9ea2 MD |
3099 | cursor.key_beg.obj_id = ip->obj_id; |
| 3100 | cursor.key_beg.create_tid = 0; | |
| 3101 | cursor.key_beg.delete_tid = 0; | |
| 3102 | cursor.key_beg.obj_type = 0; | |
| 3103 | if (ap->a_runb) | |
| 3104 | cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1; | |
| 3105 | else | |
| 3106 | cursor.key_beg.key = ap->a_loffset + 1; | |
| 3107 | if (cursor.key_beg.key < 0) | |
| 3108 | cursor.key_beg.key = 0; | |
| 3109 | cursor.asof = ip->obj_asof; | |
| bf3b416b | 3110 | cursor.flags |= HAMMER_CURSOR_ASOF; |
| a99b9ea2 MD |
3111 | |
| 3112 | cursor.key_end = cursor.key_beg; | |
| 3113 | KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE); | |
| 3114 | ||
| 3115 | ran_end = ap->a_loffset + MAXPHYS; | |
| 3116 | cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA; | |
| 3117 | cursor.key_end.rec_type = HAMMER_RECTYPE_DATA; | |
| 3118 | tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */ | |
| 3119 | if (tmp64 < ran_end) | |
| 3120 | cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL; | |
| 3121 | else | |
| 3122 | cursor.key_end.key = ran_end + MAXPHYS + 1; | |
| 3123 | ||
| 3124 | cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE; | |
| 3125 | ||
| 3126 | error = hammer_ip_first(&cursor); | |
| 3127 | base_offset = last_offset = 0; | |
| 3128 | base_disk_offset = last_disk_offset = 0; | |
| 3129 | ||
| 3130 | while (error == 0) { | |
| 3131 | /* | |
| 3132 | * Get the base file offset of the record. The key for | |
| 3133 | * data records is (base + bytes) rather then (base). | |
| 4a2796f3 MD |
3134 | * |
| 3135 | * NOTE: rec_offset + rec_len may exceed the end-of-file. | |
| 3136 | * The extra bytes should be zero on-disk and the BMAP op | |
| 3137 | * should still be ok. | |
| a99b9ea2 MD |
3138 | */ |
| 3139 | base = &cursor.leaf->base; | |
| 3140 | rec_offset = base->key - cursor.leaf->data_len; | |
| 3141 | rec_len = cursor.leaf->data_len; | |
| 3142 | ||
| 3143 | /* | |
| 4a2796f3 MD |
3144 | * Incorporate any cached truncation. |
| 3145 | * | |
| 3146 | * NOTE: Modifications to rec_len based on synthesized | |
| 3147 | * truncation points remove the guarantee that any extended | |
| 3148 | * data on disk is zero (since the truncations may not have | |
| 3149 | * taken place on-media yet). | |
| a99b9ea2 MD |
3150 | */ |
| 3151 | if (ip->flags & HAMMER_INODE_TRUNCATED) { | |
| 3152 | if (hammer_cursor_ondisk(&cursor) || | |
| 3153 | cursor.iprec->flush_state == HAMMER_FST_FLUSH) { | |
| 3154 | if (ip->trunc_off <= rec_offset) | |
| 3155 | rec_len = 0; | |
| 3156 | else if (ip->trunc_off < rec_offset + rec_len) | |
| 3157 | rec_len = (int)(ip->trunc_off - rec_offset); | |
| 3158 | } | |
| 3159 | } | |
| 3160 | if (ip->sync_flags & HAMMER_INODE_TRUNCATED) { | |
| 3161 | if (hammer_cursor_ondisk(&cursor)) { | |
| 3162 | if (ip->sync_trunc_off <= rec_offset) | |
| 3163 | rec_len = 0; | |
| 3164 | else if (ip->sync_trunc_off < rec_offset + rec_len) | |
| 3165 | rec_len = (int)(ip->sync_trunc_off - rec_offset); | |
| 3166 | } | |
| 3167 | } | |
| 3168 | ||
| 3169 | /* | |
| 3170 | * Accumulate information. If we have hit a discontiguous | |
| 3171 | * block reset base_offset unless we are already beyond the | |
| 3172 | * requested offset. If we are, that's it, we stop. | |
| 3173 | */ | |
| a99b9ea2 MD |
3174 | if (error) |
| 3175 | break; | |
| 1b0ab2c3 MD |
3176 | if (hammer_cursor_ondisk(&cursor)) { |
| 3177 | disk_offset = cursor.leaf->data_offset; | |
| 3178 | if (rec_offset != last_offset || | |