| 1 | /* |
| 2 | * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved. |
| 3 | * |
| 4 | * This code is derived from software contributed to The DragonFly Project |
| 5 | * by Matthew Dillon <dillon@backplane.com> |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 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 | * |
| 34 | * $DragonFly: src/sys/vfs/hammer/hammer_vnops.c,v 1.90 2008/07/12 23:55:22 dillon Exp $ |
| 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> |
| 46 | #include <sys/dirent.h> |
| 47 | #include <vm/vm_extern.h> |
| 48 | #include <vfs/fifofs/fifo.h> |
| 49 | #include "hammer.h" |
| 50 | |
| 51 | /* |
| 52 | * USERFS VNOPS |
| 53 | */ |
| 54 | /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/ |
| 55 | static int hammer_vop_fsync(struct vop_fsync_args *); |
| 56 | static int hammer_vop_read(struct vop_read_args *); |
| 57 | static int hammer_vop_write(struct vop_write_args *); |
| 58 | static int hammer_vop_access(struct vop_access_args *); |
| 59 | static int hammer_vop_advlock(struct vop_advlock_args *); |
| 60 | static int hammer_vop_close(struct vop_close_args *); |
| 61 | static int hammer_vop_ncreate(struct vop_ncreate_args *); |
| 62 | static int hammer_vop_getattr(struct vop_getattr_args *); |
| 63 | static int hammer_vop_nresolve(struct vop_nresolve_args *); |
| 64 | static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *); |
| 65 | static int hammer_vop_nlink(struct vop_nlink_args *); |
| 66 | static int hammer_vop_nmkdir(struct vop_nmkdir_args *); |
| 67 | static int hammer_vop_nmknod(struct vop_nmknod_args *); |
| 68 | static int hammer_vop_open(struct vop_open_args *); |
| 69 | static int hammer_vop_pathconf(struct vop_pathconf_args *); |
| 70 | static int hammer_vop_print(struct vop_print_args *); |
| 71 | static int hammer_vop_readdir(struct vop_readdir_args *); |
| 72 | static int hammer_vop_readlink(struct vop_readlink_args *); |
| 73 | static int hammer_vop_nremove(struct vop_nremove_args *); |
| 74 | static int hammer_vop_nrename(struct vop_nrename_args *); |
| 75 | static int hammer_vop_nrmdir(struct vop_nrmdir_args *); |
| 76 | static int hammer_vop_setattr(struct vop_setattr_args *); |
| 77 | static int hammer_vop_strategy(struct vop_strategy_args *); |
| 78 | static int hammer_vop_bmap(struct vop_bmap_args *ap); |
| 79 | static int hammer_vop_nsymlink(struct vop_nsymlink_args *); |
| 80 | static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *); |
| 81 | static int hammer_vop_ioctl(struct vop_ioctl_args *); |
| 82 | static int hammer_vop_mountctl(struct vop_mountctl_args *); |
| 83 | |
| 84 | static int hammer_vop_fifoclose (struct vop_close_args *); |
| 85 | static int hammer_vop_fiforead (struct vop_read_args *); |
| 86 | static int hammer_vop_fifowrite (struct vop_write_args *); |
| 87 | |
| 88 | static int hammer_vop_specclose (struct vop_close_args *); |
| 89 | static int hammer_vop_specread (struct vop_read_args *); |
| 90 | static int hammer_vop_specwrite (struct vop_write_args *); |
| 91 | |
| 92 | struct vop_ops hammer_vnode_vops = { |
| 93 | .vop_default = vop_defaultop, |
| 94 | .vop_fsync = hammer_vop_fsync, |
| 95 | .vop_getpages = vop_stdgetpages, |
| 96 | .vop_putpages = vop_stdputpages, |
| 97 | .vop_read = hammer_vop_read, |
| 98 | .vop_write = hammer_vop_write, |
| 99 | .vop_access = hammer_vop_access, |
| 100 | .vop_advlock = hammer_vop_advlock, |
| 101 | .vop_close = hammer_vop_close, |
| 102 | .vop_ncreate = hammer_vop_ncreate, |
| 103 | .vop_getattr = hammer_vop_getattr, |
| 104 | .vop_inactive = hammer_vop_inactive, |
| 105 | .vop_reclaim = hammer_vop_reclaim, |
| 106 | .vop_nresolve = hammer_vop_nresolve, |
| 107 | .vop_nlookupdotdot = hammer_vop_nlookupdotdot, |
| 108 | .vop_nlink = hammer_vop_nlink, |
| 109 | .vop_nmkdir = hammer_vop_nmkdir, |
| 110 | .vop_nmknod = hammer_vop_nmknod, |
| 111 | .vop_open = hammer_vop_open, |
| 112 | .vop_pathconf = hammer_vop_pathconf, |
| 113 | .vop_print = hammer_vop_print, |
| 114 | .vop_readdir = hammer_vop_readdir, |
| 115 | .vop_readlink = hammer_vop_readlink, |
| 116 | .vop_nremove = hammer_vop_nremove, |
| 117 | .vop_nrename = hammer_vop_nrename, |
| 118 | .vop_nrmdir = hammer_vop_nrmdir, |
| 119 | .vop_setattr = hammer_vop_setattr, |
| 120 | .vop_bmap = hammer_vop_bmap, |
| 121 | .vop_strategy = hammer_vop_strategy, |
| 122 | .vop_nsymlink = hammer_vop_nsymlink, |
| 123 | .vop_nwhiteout = hammer_vop_nwhiteout, |
| 124 | .vop_ioctl = hammer_vop_ioctl, |
| 125 | .vop_mountctl = hammer_vop_mountctl |
| 126 | }; |
| 127 | |
| 128 | struct vop_ops hammer_spec_vops = { |
| 129 | .vop_default = spec_vnoperate, |
| 130 | .vop_fsync = hammer_vop_fsync, |
| 131 | .vop_read = hammer_vop_specread, |
| 132 | .vop_write = hammer_vop_specwrite, |
| 133 | .vop_access = hammer_vop_access, |
| 134 | .vop_close = hammer_vop_specclose, |
| 135 | .vop_getattr = hammer_vop_getattr, |
| 136 | .vop_inactive = hammer_vop_inactive, |
| 137 | .vop_reclaim = hammer_vop_reclaim, |
| 138 | .vop_setattr = hammer_vop_setattr |
| 139 | }; |
| 140 | |
| 141 | struct vop_ops hammer_fifo_vops = { |
| 142 | .vop_default = fifo_vnoperate, |
| 143 | .vop_fsync = hammer_vop_fsync, |
| 144 | .vop_read = hammer_vop_fiforead, |
| 145 | .vop_write = hammer_vop_fifowrite, |
| 146 | .vop_access = hammer_vop_access, |
| 147 | .vop_close = hammer_vop_fifoclose, |
| 148 | .vop_getattr = hammer_vop_getattr, |
| 149 | .vop_inactive = hammer_vop_inactive, |
| 150 | .vop_reclaim = hammer_vop_reclaim, |
| 151 | .vop_setattr = hammer_vop_setattr |
| 152 | }; |
| 153 | |
| 154 | #ifdef DEBUG_TRUNCATE |
| 155 | struct hammer_inode *HammerTruncIp; |
| 156 | #endif |
| 157 | |
| 158 | static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch, |
| 159 | struct vnode *dvp, struct ucred *cred, int flags); |
| 160 | static int hammer_vop_strategy_read(struct vop_strategy_args *ap); |
| 161 | static int hammer_vop_strategy_write(struct vop_strategy_args *ap); |
| 162 | |
| 163 | #if 0 |
| 164 | static |
| 165 | int |
| 166 | hammer_vop_vnoperate(struct vop_generic_args *) |
| 167 | { |
| 168 | return (VOCALL(&hammer_vnode_vops, ap)); |
| 169 | } |
| 170 | #endif |
| 171 | |
| 172 | /* |
| 173 | * hammer_vop_fsync { vp, waitfor } |
| 174 | * |
| 175 | * fsync() an inode to disk and wait for it to be completely committed |
| 176 | * such that the information would not be undone if a crash occured after |
| 177 | * return. |
| 178 | */ |
| 179 | static |
| 180 | int |
| 181 | hammer_vop_fsync(struct vop_fsync_args *ap) |
| 182 | { |
| 183 | hammer_inode_t ip = VTOI(ap->a_vp); |
| 184 | |
| 185 | ++hammer_count_fsyncs; |
| 186 | vfsync(ap->a_vp, ap->a_waitfor, 1, NULL, NULL); |
| 187 | hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL); |
| 188 | if (ap->a_waitfor == MNT_WAIT) |
| 189 | hammer_wait_inode(ip); |
| 190 | return (ip->error); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * hammer_vop_read { vp, uio, ioflag, cred } |
| 195 | */ |
| 196 | static |
| 197 | int |
| 198 | hammer_vop_read(struct vop_read_args *ap) |
| 199 | { |
| 200 | struct hammer_transaction trans; |
| 201 | hammer_inode_t ip; |
| 202 | off_t offset; |
| 203 | struct buf *bp; |
| 204 | struct uio *uio; |
| 205 | int error; |
| 206 | int n; |
| 207 | int seqcount; |
| 208 | int ioseqcount; |
| 209 | int blksize; |
| 210 | |
| 211 | if (ap->a_vp->v_type != VREG) |
| 212 | return (EINVAL); |
| 213 | ip = VTOI(ap->a_vp); |
| 214 | error = 0; |
| 215 | uio = ap->a_uio; |
| 216 | |
| 217 | /* |
| 218 | * Allow the UIO's size to override the sequential heuristic. |
| 219 | */ |
| 220 | blksize = hammer_blocksize(uio->uio_offset); |
| 221 | seqcount = (uio->uio_resid + (blksize - 1)) / blksize; |
| 222 | ioseqcount = ap->a_ioflag >> 16; |
| 223 | if (seqcount < ioseqcount) |
| 224 | seqcount = ioseqcount; |
| 225 | |
| 226 | hammer_start_transaction(&trans, ip->hmp); |
| 227 | |
| 228 | /* |
| 229 | * Access the data typically in HAMMER_BUFSIZE blocks via the |
| 230 | * buffer cache, but HAMMER may use a variable block size based |
| 231 | * on the offset. |
| 232 | */ |
| 233 | while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) { |
| 234 | int64_t base_offset; |
| 235 | int64_t file_limit; |
| 236 | |
| 237 | blksize = hammer_blocksize(uio->uio_offset); |
| 238 | offset = (int)uio->uio_offset & (blksize - 1); |
| 239 | base_offset = uio->uio_offset - offset; |
| 240 | |
| 241 | if (hammer_debug_cluster_enable) { |
| 242 | /* |
| 243 | * Use file_limit to prevent cluster_read() from |
| 244 | * creating buffers of the wrong block size past |
| 245 | * the demarc. |
| 246 | */ |
| 247 | file_limit = ip->ino_data.size; |
| 248 | if (base_offset < HAMMER_XDEMARC && |
| 249 | file_limit > HAMMER_XDEMARC) { |
| 250 | file_limit = HAMMER_XDEMARC; |
| 251 | } |
| 252 | error = cluster_read(ap->a_vp, |
| 253 | file_limit, base_offset, |
| 254 | blksize, MAXPHYS, |
| 255 | seqcount, &bp); |
| 256 | } else { |
| 257 | error = bread(ap->a_vp, base_offset, blksize, &bp); |
| 258 | } |
| 259 | if (error) { |
| 260 | kprintf("error %d\n", error); |
| 261 | brelse(bp); |
| 262 | break; |
| 263 | } |
| 264 | |
| 265 | /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */ |
| 266 | n = blksize - offset; |
| 267 | if (n > uio->uio_resid) |
| 268 | n = uio->uio_resid; |
| 269 | if (n > ip->ino_data.size - uio->uio_offset) |
| 270 | n = (int)(ip->ino_data.size - uio->uio_offset); |
| 271 | error = uiomove((char *)bp->b_data + offset, n, uio); |
| 272 | |
| 273 | /* data has a lower priority then meta-data */ |
| 274 | bp->b_flags |= B_AGE; |
| 275 | bqrelse(bp); |
| 276 | if (error) |
| 277 | break; |
| 278 | } |
| 279 | if ((ip->flags & HAMMER_INODE_RO) == 0 && |
| 280 | (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) { |
| 281 | ip->ino_data.atime = trans.time; |
| 282 | hammer_modify_inode(ip, HAMMER_INODE_ATIME); |
| 283 | } |
| 284 | hammer_done_transaction(&trans); |
| 285 | return (error); |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * hammer_vop_write { vp, uio, ioflag, cred } |
| 290 | */ |
| 291 | static |
| 292 | int |
| 293 | hammer_vop_write(struct vop_write_args *ap) |
| 294 | { |
| 295 | struct hammer_transaction trans; |
| 296 | struct hammer_inode *ip; |
| 297 | hammer_mount_t hmp; |
| 298 | struct uio *uio; |
| 299 | int offset; |
| 300 | off_t base_offset; |
| 301 | struct buf *bp; |
| 302 | int error; |
| 303 | int n; |
| 304 | int flags; |
| 305 | int delta; |
| 306 | int seqcount; |
| 307 | |
| 308 | if (ap->a_vp->v_type != VREG) |
| 309 | return (EINVAL); |
| 310 | ip = VTOI(ap->a_vp); |
| 311 | hmp = ip->hmp; |
| 312 | error = 0; |
| 313 | seqcount = ap->a_ioflag >> 16; |
| 314 | |
| 315 | if (ip->flags & HAMMER_INODE_RO) |
| 316 | return (EROFS); |
| 317 | |
| 318 | /* |
| 319 | * Create a transaction to cover the operations we perform. |
| 320 | */ |
| 321 | hammer_start_transaction(&trans, hmp); |
| 322 | uio = ap->a_uio; |
| 323 | |
| 324 | /* |
| 325 | * Check append mode |
| 326 | */ |
| 327 | if (ap->a_ioflag & IO_APPEND) |
| 328 | uio->uio_offset = ip->ino_data.size; |
| 329 | |
| 330 | /* |
| 331 | * Check for illegal write offsets. Valid range is 0...2^63-1. |
| 332 | * |
| 333 | * NOTE: the base_off assignment is required to work around what |
| 334 | * I consider to be a GCC-4 optimization bug. |
| 335 | */ |
| 336 | if (uio->uio_offset < 0) { |
| 337 | hammer_done_transaction(&trans); |
| 338 | return (EFBIG); |
| 339 | } |
| 340 | base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */ |
| 341 | if (uio->uio_resid > 0 && base_offset <= 0) { |
| 342 | hammer_done_transaction(&trans); |
| 343 | return (EFBIG); |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * Access the data typically in HAMMER_BUFSIZE blocks via the |
| 348 | * buffer cache, but HAMMER may use a variable block size based |
| 349 | * on the offset. |
| 350 | */ |
| 351 | while (uio->uio_resid > 0) { |
| 352 | int fixsize = 0; |
| 353 | int blksize; |
| 354 | int blkmask; |
| 355 | |
| 356 | if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0) |
| 357 | break; |
| 358 | |
| 359 | blksize = hammer_blocksize(uio->uio_offset); |
| 360 | |
| 361 | /* |
| 362 | * Do not allow HAMMER to blow out the buffer cache. Very |
| 363 | * large UIOs can lockout other processes due to bwillwrite() |
| 364 | * mechanics. |
| 365 | * |
| 366 | * The hammer inode is not locked during these operations. |
| 367 | * The vnode is locked which can interfere with the pageout |
| 368 | * daemon for non-UIO_NOCOPY writes but should not interfere |
| 369 | * with the buffer cache. Even so, we cannot afford to |
| 370 | * allow the pageout daemon to build up too many dirty buffer |
| 371 | * cache buffers. |
| 372 | */ |
| 373 | /*if (((int)uio->uio_offset & (blksize - 1)) == 0)*/ |
| 374 | bwillwrite(blksize); |
| 375 | |
| 376 | /* |
| 377 | * Do not allow HAMMER to blow out system memory by |
| 378 | * accumulating too many records. Records are so well |
| 379 | * decoupled from the buffer cache that it is possible |
| 380 | * for userland to push data out to the media via |
| 381 | * direct-write, but build up the records queued to the |
| 382 | * backend faster then the backend can flush them out. |
| 383 | * HAMMER has hit its write limit but the frontend has |
| 384 | * no pushback to slow it down. |
| 385 | */ |
| 386 | if (hmp->rsv_recs > hammer_limit_recs / 2) { |
| 387 | /* |
| 388 | * Get the inode on the flush list |
| 389 | */ |
| 390 | if (ip->rsv_recs >= 64) |
| 391 | hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL); |
| 392 | else if (ip->rsv_recs >= 16) |
| 393 | hammer_flush_inode(ip, 0); |
| 394 | |
| 395 | /* |
| 396 | * Keep the flusher going if the system keeps |
| 397 | * queueing records. |
| 398 | */ |
| 399 | delta = hmp->count_newrecords - |
| 400 | hmp->last_newrecords; |
| 401 | if (delta < 0 || delta > hammer_limit_recs / 2) { |
| 402 | hmp->last_newrecords = hmp->count_newrecords; |
| 403 | hammer_sync_hmp(hmp, MNT_NOWAIT); |
| 404 | } |
| 405 | |
| 406 | /* |
| 407 | * If we have gotten behind start slowing |
| 408 | * down the writers. |
| 409 | */ |
| 410 | delta = (hmp->rsv_recs - hammer_limit_recs) * |
| 411 | hz / hammer_limit_recs; |
| 412 | if (delta > 0) |
| 413 | tsleep(&trans, 0, "hmrslo", delta); |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * Calculate the blocksize at the current offset and figure |
| 418 | * out how much we can actually write. |
| 419 | */ |
| 420 | blkmask = blksize - 1; |
| 421 | offset = (int)uio->uio_offset & blkmask; |
| 422 | base_offset = uio->uio_offset & ~(int64_t)blkmask; |
| 423 | n = blksize - offset; |
| 424 | if (n > uio->uio_resid) |
| 425 | n = uio->uio_resid; |
| 426 | if (uio->uio_offset + n > ip->ino_data.size) { |
| 427 | vnode_pager_setsize(ap->a_vp, uio->uio_offset + n); |
| 428 | fixsize = 1; |
| 429 | } |
| 430 | |
| 431 | if (uio->uio_segflg == UIO_NOCOPY) { |
| 432 | /* |
| 433 | * Issuing a write with the same data backing the |
| 434 | * buffer. Instantiate the buffer to collect the |
| 435 | * backing vm pages, then read-in any missing bits. |
| 436 | * |
| 437 | * This case is used by vop_stdputpages(). |
| 438 | */ |
| 439 | bp = getblk(ap->a_vp, base_offset, |
| 440 | blksize, GETBLK_BHEAVY, 0); |
| 441 | if ((bp->b_flags & B_CACHE) == 0) { |
| 442 | bqrelse(bp); |
| 443 | error = bread(ap->a_vp, base_offset, |
| 444 | blksize, &bp); |
| 445 | } |
| 446 | } else if (offset == 0 && uio->uio_resid >= blksize) { |
| 447 | /* |
| 448 | * Even though we are entirely overwriting the buffer |
| 449 | * we may still have to zero it out to avoid a |
| 450 | * mmap/write visibility issue. |
| 451 | */ |
| 452 | bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0); |
| 453 | if ((bp->b_flags & B_CACHE) == 0) |
| 454 | vfs_bio_clrbuf(bp); |
| 455 | } else if (base_offset >= ip->ino_data.size) { |
| 456 | /* |
| 457 | * If the base offset of the buffer is beyond the |
| 458 | * file EOF, we don't have to issue a read. |
| 459 | */ |
| 460 | bp = getblk(ap->a_vp, base_offset, |
| 461 | blksize, GETBLK_BHEAVY, 0); |
| 462 | vfs_bio_clrbuf(bp); |
| 463 | } else { |
| 464 | /* |
| 465 | * Partial overwrite, read in any missing bits then |
| 466 | * replace the portion being written. |
| 467 | */ |
| 468 | error = bread(ap->a_vp, base_offset, blksize, &bp); |
| 469 | if (error == 0) |
| 470 | bheavy(bp); |
| 471 | } |
| 472 | if (error == 0) { |
| 473 | error = uiomove((char *)bp->b_data + offset, |
| 474 | n, uio); |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * If we screwed up we have to undo any VM size changes we |
| 479 | * made. |
| 480 | */ |
| 481 | if (error) { |
| 482 | brelse(bp); |
| 483 | if (fixsize) { |
| 484 | vtruncbuf(ap->a_vp, ip->ino_data.size, |
| 485 | hammer_blocksize(ip->ino_data.size)); |
| 486 | } |
| 487 | break; |
| 488 | } |
| 489 | /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */ |
| 490 | if (ip->ino_data.size < uio->uio_offset) { |
| 491 | ip->ino_data.size = uio->uio_offset; |
| 492 | flags = HAMMER_INODE_DDIRTY; |
| 493 | vnode_pager_setsize(ap->a_vp, ip->ino_data.size); |
| 494 | } else { |
| 495 | flags = 0; |
| 496 | } |
| 497 | ip->ino_data.mtime = trans.time; |
| 498 | flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS; |
| 499 | hammer_modify_inode(ip, flags); |
| 500 | |
| 501 | /* |
| 502 | * Final buffer disposition. |
| 503 | */ |
| 504 | bp->b_flags |= B_AGE; |
| 505 | if (ap->a_ioflag & IO_SYNC) { |
| 506 | bwrite(bp); |
| 507 | } else if (ap->a_ioflag & IO_DIRECT) { |
| 508 | bawrite(bp); |
| 509 | } else { |
| 510 | bdwrite(bp); |
| 511 | } |
| 512 | } |
| 513 | hammer_done_transaction(&trans); |
| 514 | return (error); |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * hammer_vop_access { vp, mode, cred } |
| 519 | */ |
| 520 | static |
| 521 | int |
| 522 | hammer_vop_access(struct vop_access_args *ap) |
| 523 | { |
| 524 | struct hammer_inode *ip = VTOI(ap->a_vp); |
| 525 | uid_t uid; |
| 526 | gid_t gid; |
| 527 | int error; |
| 528 | |
| 529 | uid = hammer_to_unix_xid(&ip->ino_data.uid); |
| 530 | gid = hammer_to_unix_xid(&ip->ino_data.gid); |
| 531 | |
| 532 | error = vop_helper_access(ap, uid, gid, ip->ino_data.mode, |
| 533 | ip->ino_data.uflags); |
| 534 | return (error); |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | * hammer_vop_advlock { vp, id, op, fl, flags } |
| 539 | */ |
| 540 | static |
| 541 | int |
| 542 | hammer_vop_advlock(struct vop_advlock_args *ap) |
| 543 | { |
| 544 | hammer_inode_t ip = VTOI(ap->a_vp); |
| 545 | |
| 546 | return (lf_advlock(ap, &ip->advlock, ip->ino_data.size)); |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * hammer_vop_close { vp, fflag } |
| 551 | */ |
| 552 | static |
| 553 | int |
| 554 | hammer_vop_close(struct vop_close_args *ap) |
| 555 | { |
| 556 | hammer_inode_t ip = VTOI(ap->a_vp); |
| 557 | |
| 558 | if ((ip->flags | ip->sync_flags) & HAMMER_INODE_MODMASK) |
| 559 | hammer_inode_waitreclaims(ip->hmp); |
| 560 | return (vop_stdclose(ap)); |
| 561 | } |
| 562 | |
| 563 | /* |
| 564 | * hammer_vop_ncreate { nch, dvp, vpp, cred, vap } |
| 565 | * |
| 566 | * The operating system has already ensured that the directory entry |
| 567 | * does not exist and done all appropriate namespace locking. |
| 568 | */ |
| 569 | static |
| 570 | int |
| 571 | hammer_vop_ncreate(struct vop_ncreate_args *ap) |
| 572 | { |
| 573 | struct hammer_transaction trans; |
| 574 | struct hammer_inode *dip; |
| 575 | struct hammer_inode *nip; |
| 576 | struct nchandle *nch; |
| 577 | int error; |
| 578 | |
| 579 | nch = ap->a_nch; |
| 580 | dip = VTOI(ap->a_dvp); |
| 581 | |
| 582 | if (dip->flags & HAMMER_INODE_RO) |
| 583 | return (EROFS); |
| 584 | if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0) |
| 585 | return (error); |
| 586 | |
| 587 | /* |
| 588 | * Create a transaction to cover the operations we perform. |
| 589 | */ |
| 590 | hammer_start_transaction(&trans, dip->hmp); |
| 591 | |
| 592 | /* |
| 593 | * Create a new filesystem object of the requested type. The |
| 594 | * returned inode will be referenced and shared-locked to prevent |
| 595 | * it from being moved to the flusher. |
| 596 | */ |
| 597 | |
| 598 | error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, |
| 599 | dip, NULL, &nip); |
| 600 | if (error) { |
| 601 | hkprintf("hammer_create_inode error %d\n", error); |
| 602 | hammer_done_transaction(&trans); |
| 603 | *ap->a_vpp = NULL; |
| 604 | return (error); |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | * Add the new filesystem object to the directory. This will also |
| 609 | * bump the inode's link count. |
| 610 | */ |
| 611 | error = hammer_ip_add_directory(&trans, dip, |
| 612 | nch->ncp->nc_name, nch->ncp->nc_nlen, |
| 613 | nip); |
| 614 | if (error) |
| 615 | hkprintf("hammer_ip_add_directory error %d\n", error); |
| 616 | |
| 617 | /* |
| 618 | * Finish up. |
| 619 | */ |
| 620 | if (error) { |
| 621 | hammer_rel_inode(nip, 0); |
| 622 | hammer_done_transaction(&trans); |
| 623 | *ap->a_vpp = NULL; |
| 624 | } else { |
| 625 | error = hammer_get_vnode(nip, ap->a_vpp); |
| 626 | hammer_done_transaction(&trans); |
| 627 | hammer_rel_inode(nip, 0); |
| 628 | if (error == 0) { |
| 629 | cache_setunresolved(ap->a_nch); |
| 630 | cache_setvp(ap->a_nch, *ap->a_vpp); |
| 631 | } |
| 632 | } |
| 633 | return (error); |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | * hammer_vop_getattr { vp, vap } |
| 638 | * |
| 639 | * Retrieve an inode's attribute information. When accessing inodes |
| 640 | * historically we fake the atime field to ensure consistent results. |
| 641 | * The atime field is stored in the B-Tree element and allowed to be |
| 642 | * updated without cycling the element. |
| 643 | */ |
| 644 | static |
| 645 | int |
| 646 | hammer_vop_getattr(struct vop_getattr_args *ap) |
| 647 | { |
| 648 | struct hammer_inode *ip = VTOI(ap->a_vp); |
| 649 | struct vattr *vap = ap->a_vap; |
| 650 | |
| 651 | /* |
| 652 | * We want the fsid to be different when accessing a filesystem |
| 653 | * with different as-of's so programs like diff don't think |
| 654 | * the files are the same. |
| 655 | * |
| 656 | * We also want the fsid to be the same when comparing snapshots, |
| 657 | * or when comparing mirrors (which might be backed by different |
| 658 | * physical devices). HAMMER fsids are based on the PFS's |
| 659 | * shared_uuid field. |
| 660 | * |
| 661 | * XXX there is a chance of collision here. The va_fsid reported |
| 662 | * by stat is different from the more involved fsid used in the |
| 663 | * mount structure. |
| 664 | */ |
| 665 | vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^ |
| 666 | (u_int32_t)(ip->obj_asof >> 32); |
| 667 | |
| 668 | vap->va_fileid = ip->ino_leaf.base.obj_id; |
| 669 | vap->va_mode = ip->ino_data.mode; |
| 670 | vap->va_nlink = ip->ino_data.nlinks; |
| 671 | vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid); |
| 672 | vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid); |
| 673 | vap->va_rmajor = 0; |
| 674 | vap->va_rminor = 0; |
| 675 | vap->va_size = ip->ino_data.size; |
| 676 | |
| 677 | /* |
| 678 | * We must provide a consistent atime and mtime for snapshots |
| 679 | * so people can do a 'tar cf - ... | md5' on them and get |
| 680 | * consistent results. |
| 681 | */ |
| 682 | if (ip->flags & HAMMER_INODE_RO) { |
| 683 | hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime); |
| 684 | hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime); |
| 685 | } else { |
| 686 | hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime); |
| 687 | hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime); |
| 688 | } |
| 689 | hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime); |
| 690 | vap->va_flags = ip->ino_data.uflags; |
| 691 | vap->va_gen = 1; /* hammer inums are unique for all time */ |
| 692 | vap->va_blocksize = HAMMER_BUFSIZE; |
| 693 | if (ip->ino_data.size >= HAMMER_XDEMARC) { |
| 694 | vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) & |
| 695 | ~HAMMER_XBUFMASK64; |
| 696 | } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) { |
| 697 | vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) & |
| 698 | ~HAMMER_BUFMASK64; |
| 699 | } else { |
| 700 | vap->va_bytes = (ip->ino_data.size + 15) & ~15; |
| 701 | } |
| 702 | vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type); |
| 703 | vap->va_filerev = 0; /* XXX */ |
| 704 | /* mtime uniquely identifies any adjustments made to the file XXX */ |
| 705 | vap->va_fsmid = ip->ino_data.mtime; |
| 706 | vap->va_uid_uuid = ip->ino_data.uid; |
| 707 | vap->va_gid_uuid = ip->ino_data.gid; |
| 708 | vap->va_fsid_uuid = ip->hmp->fsid; |
| 709 | vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID | |
| 710 | VA_FSID_UUID_VALID; |
| 711 | |
| 712 | switch (ip->ino_data.obj_type) { |
| 713 | case HAMMER_OBJTYPE_CDEV: |
| 714 | case HAMMER_OBJTYPE_BDEV: |
| 715 | vap->va_rmajor = ip->ino_data.rmajor; |
| 716 | vap->va_rminor = ip->ino_data.rminor; |
| 717 | break; |
| 718 | default: |
| 719 | break; |
| 720 | } |
| 721 | return(0); |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | * hammer_vop_nresolve { nch, dvp, cred } |
| 726 | * |
| 727 | * Locate the requested directory entry. |
| 728 | */ |
| 729 | static |
| 730 | int |
| 731 | hammer_vop_nresolve(struct vop_nresolve_args *ap) |
| 732 | { |
| 733 | struct hammer_transaction trans; |
| 734 | struct namecache *ncp; |
| 735 | hammer_inode_t dip; |
| 736 | hammer_inode_t ip; |
| 737 | hammer_tid_t asof; |
| 738 | struct hammer_cursor cursor; |
| 739 | struct vnode *vp; |
| 740 | int64_t namekey; |
| 741 | int error; |
| 742 | int i; |
| 743 | int nlen; |
| 744 | int flags; |
| 745 | int ispfs; |
| 746 | int64_t obj_id; |
| 747 | u_int32_t localization; |
| 748 | |
| 749 | /* |
| 750 | * Misc initialization, plus handle as-of name extensions. Look for |
| 751 | * the '@@' extension. Note that as-of files and directories cannot |
| 752 | * be modified. |
| 753 | */ |
| 754 | dip = VTOI(ap->a_dvp); |
| 755 | ncp = ap->a_nch->ncp; |
| 756 | asof = dip->obj_asof; |
| 757 | nlen = ncp->nc_nlen; |
| 758 | flags = dip->flags & HAMMER_INODE_RO; |
| 759 | ispfs = 0; |
| 760 | |
| 761 | hammer_simple_transaction(&trans, dip->hmp); |
| 762 | |
| 763 | for (i = 0; i < nlen; ++i) { |
| 764 | if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') { |
| 765 | asof = hammer_str_to_tid(ncp->nc_name + i + 2, |
| 766 | &ispfs, &localization); |
| 767 | if (asof != HAMMER_MAX_TID) |
| 768 | flags |= HAMMER_INODE_RO; |
| 769 | break; |
| 770 | } |
| 771 | } |
| 772 | nlen = i; |
| 773 | |
| 774 | /* |
| 775 | * If this is a PFS softlink we dive into the PFS |
| 776 | */ |
| 777 | if (ispfs && nlen == 0) { |
| 778 | ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT, |
| 779 | asof, localization, |
| 780 | flags, &error); |
| 781 | if (error == 0) { |
| 782 | error = hammer_get_vnode(ip, &vp); |
| 783 | hammer_rel_inode(ip, 0); |
| 784 | } else { |
| 785 | vp = NULL; |
| 786 | } |
| 787 | if (error == 0) { |
| 788 | vn_unlock(vp); |
| 789 | cache_setvp(ap->a_nch, vp); |
| 790 | vrele(vp); |
| 791 | } |
| 792 | goto done; |
| 793 | } |
| 794 | |
| 795 | /* |
| 796 | * If there is no path component the time extension is relative to |
| 797 | * dip. |
| 798 | */ |
| 799 | if (nlen == 0) { |
| 800 | ip = hammer_get_inode(&trans, dip, dip->obj_id, |
| 801 | asof, dip->obj_localization, |
| 802 | flags, &error); |
| 803 | if (error == 0) { |
| 804 | error = hammer_get_vnode(ip, &vp); |
| 805 | hammer_rel_inode(ip, 0); |
| 806 | } else { |
| 807 | vp = NULL; |
| 808 | } |
| 809 | if (error == 0) { |
| 810 | vn_unlock(vp); |
| 811 | cache_setvp(ap->a_nch, vp); |
| 812 | vrele(vp); |
| 813 | } |
| 814 | goto done; |
| 815 | } |
| 816 | |
| 817 | /* |
| 818 | * Calculate the namekey and setup the key range for the scan. This |
| 819 | * works kinda like a chained hash table where the lower 32 bits |
| 820 | * of the namekey synthesize the chain. |
| 821 | * |
| 822 | * The key range is inclusive of both key_beg and key_end. |
| 823 | */ |
| 824 | namekey = hammer_directory_namekey(ncp->nc_name, nlen); |
| 825 | |
| 826 | error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip); |
| 827 | cursor.key_beg.localization = dip->obj_localization + |
| 828 | HAMMER_LOCALIZE_MISC; |
| 829 | cursor.key_beg.obj_id = dip->obj_id; |
| 830 | cursor.key_beg.key = namekey; |
| 831 | cursor.key_beg.create_tid = 0; |
| 832 | cursor.key_beg.delete_tid = 0; |
| 833 | cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY; |
| 834 | cursor.key_beg.obj_type = 0; |
| 835 | |
| 836 | cursor.key_end = cursor.key_beg; |
| 837 | cursor.key_end.key |= 0xFFFFFFFFULL; |
| 838 | cursor.asof = asof; |
| 839 | cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF; |
| 840 | |
| 841 | /* |
| 842 | * Scan all matching records (the chain), locate the one matching |
| 843 | * the requested path component. |
| 844 | * |
| 845 | * The hammer_ip_*() functions merge in-memory records with on-disk |
| 846 | * records for the purposes of the search. |
| 847 | */ |
| 848 | obj_id = 0; |
| 849 | localization = HAMMER_DEF_LOCALIZATION; |
| 850 | |
| 851 | if (error == 0) { |
| 852 | error = hammer_ip_first(&cursor); |
| 853 | while (error == 0) { |
| 854 | error = hammer_ip_resolve_data(&cursor); |
| 855 | if (error) |
| 856 | break; |
| 857 | if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF && |
| 858 | bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) { |
| 859 | obj_id = cursor.data->entry.obj_id; |
| 860 | localization = cursor.data->entry.localization; |
| 861 | break; |
| 862 | } |
| 863 | error = hammer_ip_next(&cursor); |
| 864 | } |
| 865 | } |
| 866 | hammer_done_cursor(&cursor); |
| 867 | if (error == 0) { |
| 868 | ip = hammer_get_inode(&trans, dip, obj_id, |
| 869 | asof, localization, |
| 870 | flags, &error); |
| 871 | if (error == 0) { |
| 872 | error = hammer_get_vnode(ip, &vp); |
| 873 | hammer_rel_inode(ip, 0); |
| 874 | } else { |
| 875 | vp = NULL; |
| 876 | } |
| 877 | if (error == 0) { |
| 878 | vn_unlock(vp); |
| 879 | cache_setvp(ap->a_nch, vp); |
| 880 | vrele(vp); |
| 881 | } |
| 882 | } else if (error == ENOENT) { |
| 883 | cache_setvp(ap->a_nch, NULL); |
| 884 | } |
| 885 | done: |
| 886 | hammer_done_transaction(&trans); |
| 887 | return (error); |
| 888 | } |
| 889 | |
| 890 | /* |
| 891 | * hammer_vop_nlookupdotdot { dvp, vpp, cred } |
| 892 | * |
| 893 | * Locate the parent directory of a directory vnode. |
| 894 | * |
| 895 | * dvp is referenced but not locked. *vpp must be returned referenced and |
| 896 | * locked. A parent_obj_id of 0 does not necessarily indicate that we are |
| 897 | * at the root, instead it could indicate that the directory we were in was |
| 898 | * removed. |
| 899 | * |
| 900 | * NOTE: as-of sequences are not linked into the directory structure. If |
| 901 | * we are at the root with a different asof then the mount point, reload |
| 902 | * the same directory with the mount point's asof. I'm not sure what this |
| 903 | * will do to NFS. We encode ASOF stamps in NFS file handles so it might not |
| 904 | * get confused, but it hasn't been tested. |
| 905 | */ |
| 906 | static |
| 907 | int |
| 908 | hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap) |
| 909 | { |
| 910 | struct hammer_transaction trans; |
| 911 | struct hammer_inode *dip; |
| 912 | struct hammer_inode *ip; |
| 913 | int64_t parent_obj_id; |
| 914 | u_int32_t parent_obj_localization; |
| 915 | hammer_tid_t asof; |
| 916 | int error; |
| 917 | |
| 918 | dip = VTOI(ap->a_dvp); |
| 919 | asof = dip->obj_asof; |
| 920 | |
| 921 | /* |
| 922 | * Whos are parent? This could be the root of a pseudo-filesystem |
| 923 | * whos parent is in another localization domain. |
| 924 | */ |
| 925 | parent_obj_id = dip->ino_data.parent_obj_id; |
| 926 | if (dip->obj_id == HAMMER_OBJID_ROOT) |
| 927 | parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization; |
| 928 | else |
| 929 | parent_obj_localization = dip->obj_localization; |
| 930 | |
| 931 | if (parent_obj_id == 0) { |
| 932 | if (dip->obj_id == HAMMER_OBJID_ROOT && |
| 933 | asof != dip->hmp->asof) { |
| 934 | parent_obj_id = dip->obj_id; |
| 935 | asof = dip->hmp->asof; |
| 936 | *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK); |
| 937 | ksnprintf(*ap->a_fakename, 19, "0x%016llx", |
| 938 | dip->obj_asof); |
| 939 | } else { |
| 940 | *ap->a_vpp = NULL; |
| 941 | return ENOENT; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | hammer_simple_transaction(&trans, dip->hmp); |
| 946 | |
| 947 | ip = hammer_get_inode(&trans, dip, parent_obj_id, |
| 948 | asof, parent_obj_localization, |
| 949 | dip->flags, &error); |
| 950 | if (ip) { |
| 951 | error = hammer_get_vnode(ip, ap->a_vpp); |
| 952 | hammer_rel_inode(ip, 0); |
| 953 | } else { |
| 954 | *ap->a_vpp = NULL; |
| 955 | } |
| 956 | hammer_done_transaction(&trans); |
| 957 | return (error); |
| 958 | } |
| 959 | |
| 960 | /* |
| 961 | * hammer_vop_nlink { nch, dvp, vp, cred } |
| 962 | */ |
| 963 | static |
| 964 | int |
| 965 | hammer_vop_nlink(struct vop_nlink_args *ap) |
| 966 | { |
| 967 | struct hammer_transaction trans; |
| 968 | struct hammer_inode *dip; |
| 969 | struct hammer_inode *ip; |
| 970 | struct nchandle *nch; |
| 971 | int error; |
| 972 | |
| 973 | nch = ap->a_nch; |
| 974 | dip = VTOI(ap->a_dvp); |
| 975 | ip = VTOI(ap->a_vp); |
| 976 | |
| 977 | if (dip->flags & HAMMER_INODE_RO) |
| 978 | return (EROFS); |
| 979 | if (ip->flags & HAMMER_INODE_RO) |
| 980 | return (EROFS); |
| 981 | if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0) |
| 982 | return (error); |
| 983 | |
| 984 | /* |
| 985 | * Create a transaction to cover the operations we perform. |
| 986 | */ |
| 987 | hammer_start_transaction(&trans, dip->hmp); |
| 988 | |
| 989 | /* |
| 990 | * Add the filesystem object to the directory. Note that neither |
| 991 | * dip nor ip are referenced or locked, but their vnodes are |
| 992 | * referenced. This function will bump the inode's link count. |
| 993 | */ |
| 994 | error = hammer_ip_add_directory(&trans, dip, |
| 995 | nch->ncp->nc_name, nch->ncp->nc_nlen, |
| 996 | ip); |
| 997 | |
| 998 | /* |
| 999 | * Finish up. |
| 1000 | */ |
| 1001 | if (error == 0) { |
| 1002 | cache_setunresolved(nch); |
| 1003 | cache_setvp(nch, ap->a_vp); |
| 1004 | } |
| 1005 | hammer_done_transaction(&trans); |
| 1006 | return (error); |
| 1007 | } |
| 1008 | |
| 1009 | /* |
| 1010 | * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap } |
| 1011 | * |
| 1012 | * The operating system has already ensured that the directory entry |
| 1013 | * does not exist and done all appropriate namespace locking. |
| 1014 | */ |
| 1015 | static |
| 1016 | int |
| 1017 | hammer_vop_nmkdir(struct vop_nmkdir_args *ap) |
| 1018 | { |
| 1019 | struct hammer_transaction trans; |
| 1020 | struct hammer_inode *dip; |
| 1021 | struct hammer_inode *nip; |
| 1022 | struct nchandle *nch; |
| 1023 | int error; |
| 1024 | |
| 1025 | nch = ap->a_nch; |
| 1026 | dip = VTOI(ap->a_dvp); |
| 1027 | |
| 1028 | if (dip->flags & HAMMER_INODE_RO) |
| 1029 | return (EROFS); |
| 1030 | if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0) |
| 1031 | return (error); |
| 1032 | |
| 1033 | /* |
| 1034 | * Create a transaction to cover the operations we perform. |
| 1035 | */ |
| 1036 | hammer_start_transaction(&trans, dip->hmp); |
| 1037 | |
| 1038 | /* |
| 1039 | * Create a new filesystem object of the requested type. The |
| 1040 | * returned inode will be referenced but not locked. |
| 1041 | */ |
| 1042 | error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, |
| 1043 | dip, NULL, &nip); |
| 1044 | if (error) { |
| 1045 | hkprintf("hammer_mkdir error %d\n", error); |
| 1046 | hammer_done_transaction(&trans); |
| 1047 | *ap->a_vpp = NULL; |
| 1048 | return (error); |
| 1049 | } |
| 1050 | /* |
| 1051 | * Add the new filesystem object to the directory. This will also |
| 1052 | * bump the inode's link count. |
| 1053 | */ |
| 1054 | error = hammer_ip_add_directory(&trans, dip, |
| 1055 | nch->ncp->nc_name, nch->ncp->nc_nlen, |
| 1056 | nip); |
| 1057 | if (error) |
| 1058 | hkprintf("hammer_mkdir (add) error %d\n", error); |
| 1059 | |
| 1060 | /* |
| 1061 | * Finish up. |
| 1062 | */ |
| 1063 | if (error) { |
| 1064 | hammer_rel_inode(nip, 0); |
| 1065 | *ap->a_vpp = NULL; |
| 1066 | } else { |
| 1067 | error = hammer_get_vnode(nip, ap->a_vpp); |
| 1068 | hammer_rel_inode(nip, 0); |
| 1069 | if (error == 0) { |
| 1070 | cache_setunresolved(ap->a_nch); |
| 1071 | cache_setvp(ap->a_nch, *ap->a_vpp); |
| 1072 | } |
| 1073 | } |
| 1074 | hammer_done_transaction(&trans); |
| 1075 | return (error); |
| 1076 | } |
| 1077 | |
| 1078 | /* |
| 1079 | * hammer_vop_nmknod { nch, dvp, vpp, cred, vap } |
| 1080 | * |
| 1081 | * The operating system has already ensured that the directory entry |
| 1082 | * does not exist and done all appropriate namespace locking. |
| 1083 | */ |
| 1084 | static |
| 1085 | int |
| 1086 | hammer_vop_nmknod(struct vop_nmknod_args *ap) |
| 1087 | { |
| 1088 | struct hammer_transaction trans; |
| 1089 | struct hammer_inode *dip; |
| 1090 | struct hammer_inode *nip; |
| 1091 | struct nchandle *nch; |
| 1092 | int error; |
| 1093 | |
| 1094 | nch = ap->a_nch; |
| 1095 | dip = VTOI(ap->a_dvp); |
| 1096 | |
| 1097 | if (dip->flags & HAMMER_INODE_RO) |
| 1098 | return (EROFS); |
| 1099 | if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0) |
| 1100 | return (error); |
| 1101 | |
| 1102 | /* |
| 1103 | * Create a transaction to cover the operations we perform. |
| 1104 | */ |
| 1105 | hammer_start_transaction(&trans, dip->hmp); |
| 1106 | |
| 1107 | /* |
| 1108 | * Create a new filesystem object of the requested type. The |
| 1109 | * returned inode will be referenced but not locked. |
| 1110 | * |
| 1111 | * If mknod specifies a directory a pseudo-fs is created. |
| 1112 | */ |
| 1113 | error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, |
| 1114 | dip, NULL, &nip); |
| 1115 | if (error) { |
| 1116 | hammer_done_transaction(&trans); |
| 1117 | *ap->a_vpp = NULL; |
| 1118 | return (error); |
| 1119 | } |
| 1120 | |
| 1121 | /* |
| 1122 | * Add the new filesystem object to the directory. This will also |
| 1123 | * bump the inode's link count. |
| 1124 | */ |
| 1125 | error = hammer_ip_add_directory(&trans, dip, |
| 1126 | nch->ncp->nc_name, nch->ncp->nc_nlen, |
| 1127 | nip); |
| 1128 | |
| 1129 | /* |
| 1130 | * Finish up. |
| 1131 | */ |
| 1132 | if (error) { |
| 1133 | hammer_rel_inode(nip, 0); |
| 1134 | *ap->a_vpp = NULL; |
| 1135 | } else { |
| 1136 | error = hammer_get_vnode(nip, ap->a_vpp); |
| 1137 | hammer_rel_inode(nip, 0); |
| 1138 | if (error == 0) { |
| 1139 | cache_setunresolved(ap->a_nch); |
| 1140 | cache_setvp(ap->a_nch, *ap->a_vpp); |
| 1141 | } |
| 1142 | } |
| 1143 | hammer_done_transaction(&trans); |
| 1144 | return (error); |
| 1145 | } |
| 1146 | |
| 1147 | /* |
| 1148 | * hammer_vop_open { vp, mode, cred, fp } |
| 1149 | */ |
| 1150 | static |
| 1151 | int |
| 1152 | hammer_vop_open(struct vop_open_args *ap) |
| 1153 | { |
| 1154 | hammer_inode_t ip; |
| 1155 | |
| 1156 | ip = VTOI(ap->a_vp); |
| 1157 | |
| 1158 | if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO)) |
| 1159 | return (EROFS); |
| 1160 | return(vop_stdopen(ap)); |
| 1161 | } |
| 1162 | |
| 1163 | /* |
| 1164 | * hammer_vop_pathconf { vp, name, retval } |
| 1165 | */ |
| 1166 | static |
| 1167 | int |
| 1168 | hammer_vop_pathconf(struct vop_pathconf_args *ap) |
| 1169 | { |
| 1170 | return EOPNOTSUPP; |
| 1171 | } |
| 1172 | |
| 1173 | /* |
| 1174 | * hammer_vop_print { vp } |
| 1175 | */ |
| 1176 | static |
| 1177 | int |
| 1178 | hammer_vop_print(struct vop_print_args *ap) |
| 1179 | { |
| 1180 | return EOPNOTSUPP; |
| 1181 | } |
| 1182 | |
| 1183 | /* |
| 1184 | * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies } |
| 1185 | */ |
| 1186 | static |
| 1187 | int |
| 1188 | hammer_vop_readdir(struct vop_readdir_args *ap) |
| 1189 | { |
| 1190 | struct hammer_transaction trans; |
| 1191 | struct hammer_cursor cursor; |
| 1192 | struct hammer_inode *ip; |
| 1193 | struct uio *uio; |
| 1194 | hammer_base_elm_t base; |
| 1195 | int error; |
| 1196 | int cookie_index; |
| 1197 | int ncookies; |
| 1198 | off_t *cookies; |
| 1199 | off_t saveoff; |
| 1200 | int r; |
| 1201 | int dtype; |
| 1202 | |
| 1203 | ip = VTOI(ap->a_vp); |
| 1204 | uio = ap->a_uio; |
| 1205 | saveoff = uio->uio_offset; |
| 1206 | |
| 1207 | if (ap->a_ncookies) { |
| 1208 | ncookies = uio->uio_resid / 16 + 1; |
| 1209 | if (ncookies > 1024) |
| 1210 | ncookies = 1024; |
| 1211 | cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK); |
| 1212 | cookie_index = 0; |
| 1213 | } else { |
| 1214 | ncookies = -1; |
| 1215 | cookies = NULL; |
| 1216 | cookie_index = 0; |
| 1217 | } |
| 1218 | |
| 1219 | hammer_simple_transaction(&trans, ip->hmp); |
| 1220 | |
| 1221 | /* |
| 1222 | * Handle artificial entries |
| 1223 | */ |
| 1224 | error = 0; |
| 1225 | if (saveoff == 0) { |
| 1226 | r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, "."); |
| 1227 | if (r) |
| 1228 | goto done; |
| 1229 | if (cookies) |
| 1230 | cookies[cookie_index] = saveoff; |
| 1231 | ++saveoff; |
| 1232 | ++cookie_index; |
| 1233 | if (cookie_index == ncookies) |
| 1234 | goto done; |
| 1235 | } |
| 1236 | if (saveoff == 1) { |
| 1237 | if (ip->ino_data.parent_obj_id) { |
| 1238 | r = vop_write_dirent(&error, uio, |
| 1239 | ip->ino_data.parent_obj_id, |
| 1240 | DT_DIR, 2, ".."); |
| 1241 | } else { |
| 1242 | r = vop_write_dirent(&error, uio, |
| 1243 | ip->obj_id, DT_DIR, 2, ".."); |
| 1244 | } |
| 1245 | if (r) |
| 1246 | goto done; |
| 1247 | if (cookies) |
| 1248 | cookies[cookie_index] = saveoff; |
| 1249 | ++saveoff; |
| 1250 | ++cookie_index; |
| 1251 | if (cookie_index == ncookies) |
| 1252 | goto done; |
| 1253 | } |
| 1254 | |
| 1255 | /* |
| 1256 | * Key range (begin and end inclusive) to scan. Directory keys |
| 1257 | * directly translate to a 64 bit 'seek' position. |
| 1258 | */ |
| 1259 | hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip); |
| 1260 | cursor.key_beg.localization = ip->obj_localization + |
| 1261 | HAMMER_LOCALIZE_MISC; |
| 1262 | cursor.key_beg.obj_id = ip->obj_id; |
| 1263 | cursor.key_beg.create_tid = 0; |
| 1264 | cursor.key_beg.delete_tid = 0; |
| 1265 | cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY; |
| 1266 | cursor.key_beg.obj_type = 0; |
| 1267 | cursor.key_beg.key = saveoff; |
| 1268 | |
| 1269 | cursor.key_end = cursor.key_beg; |
| 1270 | cursor.key_end.key = HAMMER_MAX_KEY; |
| 1271 | cursor.asof = ip->obj_asof; |
| 1272 | cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF; |
| 1273 | |
| 1274 | error = hammer_ip_first(&cursor); |
| 1275 | |
| 1276 | while (error == 0) { |
| 1277 | error = hammer_ip_resolve_data(&cursor); |
| 1278 | if (error) |
| 1279 | break; |
| 1280 | base = &cursor.leaf->base; |
| 1281 | saveoff = base->key; |
| 1282 | KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF); |
| 1283 | |
| 1284 | if (base->obj_id != ip->obj_id) |
| 1285 | panic("readdir: bad record at %p", cursor.node); |
| 1286 | |
| 1287 | /* |
| 1288 | * Convert pseudo-filesystems into softlinks |
| 1289 | */ |
| 1290 | dtype = hammer_get_dtype(cursor.leaf->base.obj_type); |
| 1291 | r = vop_write_dirent( |
| 1292 | &error, uio, cursor.data->entry.obj_id, |
| 1293 | dtype, |
| 1294 | cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF , |
| 1295 | (void *)cursor.data->entry.name); |
| 1296 | if (r) |
| 1297 | break; |
| 1298 | ++saveoff; |
| 1299 | if (cookies) |
| 1300 | cookies[cookie_index] = base->key; |
| 1301 | ++cookie_index; |
| 1302 | if (cookie_index == ncookies) |
| 1303 | break; |
| 1304 | error = hammer_ip_next(&cursor); |
| 1305 | } |
| 1306 | hammer_done_cursor(&cursor); |
| 1307 | |
| 1308 | done: |
| 1309 | hammer_done_transaction(&trans); |
| 1310 | |
| 1311 | if (ap->a_eofflag) |
| 1312 | *ap->a_eofflag = (error == ENOENT); |
| 1313 | uio->uio_offset = saveoff; |
| 1314 | if (error && cookie_index == 0) { |
| 1315 | if (error == ENOENT) |
| 1316 | error = 0; |
| 1317 | if (cookies) { |
| 1318 | kfree(cookies, M_TEMP); |
| 1319 | *ap->a_ncookies = 0; |
| 1320 | *ap->a_cookies = NULL; |
| 1321 | } |
| 1322 | } else { |
| 1323 | if (error == ENOENT) |
| 1324 | error = 0; |
| 1325 | if (cookies) { |
| 1326 | *ap->a_ncookies = cookie_index; |
| 1327 | *ap->a_cookies = cookies; |
| 1328 | } |
| 1329 | } |
| 1330 | return(error); |
| 1331 | } |
| 1332 | |
| 1333 | /* |
| 1334 | * hammer_vop_readlink { vp, uio, cred } |
| 1335 | */ |
| 1336 | static |
| 1337 | int |
| 1338 | hammer_vop_readlink(struct vop_readlink_args *ap) |
| 1339 | { |
| 1340 | struct hammer_transaction trans; |
| 1341 | struct hammer_cursor cursor; |
| 1342 | struct hammer_inode *ip; |
| 1343 | char buf[32]; |
| 1344 | u_int32_t localization; |
| 1345 | hammer_pseudofs_inmem_t pfsm; |
| 1346 | int error; |
| 1347 | |
| 1348 | ip = VTOI(ap->a_vp); |
| 1349 | |
| 1350 | /* |
| 1351 | * Shortcut if the symlink data was stuffed into ino_data. |
| 1352 | * |
| 1353 | * Also expand special "@@PFS%05d" softlinks (expansion only |
| 1354 | * occurs for non-historical (current) accesses made from the |
| 1355 | * primary filesystem). |
| 1356 | */ |
| 1357 | if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) { |
| 1358 | char *ptr; |
| 1359 | int bytes; |
| 1360 | |
| 1361 | ptr = ip->ino_data.ext.symlink; |
| 1362 | bytes = (int)ip->ino_data.size; |
| 1363 | if (bytes == 10 && |
| 1364 | ip->obj_asof == HAMMER_MAX_TID && |
| 1365 | ip->obj_localization == 0 && |
| 1366 | strncmp(ptr, "@@PFS", 5) == 0) { |
| 1367 | hammer_simple_transaction(&trans, ip->hmp); |
| 1368 | bcopy(ptr + 5, buf, 5); |
| 1369 | buf[5] = 0; |
| 1370 | localization = strtoul(buf, NULL, 10) << 16; |
| 1371 | pfsm = hammer_load_pseudofs(&trans, localization, |
| 1372 | &error); |
| 1373 | if (error == 0) { |
| 1374 | if (pfsm->pfsd.mirror_flags & |
| 1375 | HAMMER_PFSD_SLAVE) { |
| 1376 | ksnprintf(buf, sizeof(buf), |
| 1377 | "@@0x%016llx:%05d", |
| 1378 | pfsm->pfsd.sync_end_tid, |
| 1379 | localization >> 16); |
| 1380 | } else { |
| 1381 | ksnprintf(buf, sizeof(buf), |
| 1382 | "@@0x%016llx:%05d", |
| 1383 | HAMMER_MAX_TID, |
| 1384 | localization >> 16); |
| 1385 | } |
| 1386 | ptr = buf; |
| 1387 | bytes = strlen(buf); |
| 1388 | } |
| 1389 | if (pfsm) |
| 1390 | hammer_rel_pseudofs(trans.hmp, pfsm); |
| 1391 | hammer_done_transaction(&trans); |
| 1392 | } |
| 1393 | error = uiomove(ptr, bytes, ap->a_uio); |
| 1394 | return(error); |
| 1395 | } |
| 1396 | |
| 1397 | /* |
| 1398 | * Long version |
| 1399 | */ |
| 1400 | hammer_simple_transaction(&trans, ip->hmp); |
| 1401 | hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip); |
| 1402 | |
| 1403 | /* |
| 1404 | * Key range (begin and end inclusive) to scan. Directory keys |
| 1405 | * directly translate to a 64 bit 'seek' position. |
| 1406 | */ |
| 1407 | cursor.key_beg.localization = ip->obj_localization + |
| 1408 | HAMMER_LOCALIZE_MISC; |
| 1409 | cursor.key_beg.obj_id = ip->obj_id; |
| 1410 | cursor.key_beg.create_tid = 0; |
| 1411 | cursor.key_beg.delete_tid = 0; |
| 1412 | cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX; |
| 1413 | cursor.key_beg.obj_type = 0; |
| 1414 | cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK; |
| 1415 | cursor.asof = ip->obj_asof; |
| 1416 | cursor.flags |= HAMMER_CURSOR_ASOF; |
| 1417 | |
| 1418 | error = hammer_ip_lookup(&cursor); |
| 1419 | if (error == 0) { |
| 1420 | error = hammer_ip_resolve_data(&cursor); |
| 1421 | if (error == 0) { |
| 1422 | KKASSERT(cursor.leaf->data_len >= |
| 1423 | HAMMER_SYMLINK_NAME_OFF); |
| 1424 | error = uiomove(cursor.data->symlink.name, |
| 1425 | cursor.leaf->data_len - |
| 1426 | HAMMER_SYMLINK_NAME_OFF, |
| 1427 | ap->a_uio); |
| 1428 | } |
| 1429 | } |
| 1430 | hammer_done_cursor(&cursor); |
| 1431 | hammer_done_transaction(&trans); |
| 1432 | return(error); |
| 1433 | } |
| 1434 | |
| 1435 | /* |
| 1436 | * hammer_vop_nremove { nch, dvp, cred } |
| 1437 | */ |
| 1438 | static |
| 1439 | int |
| 1440 | hammer_vop_nremove(struct vop_nremove_args *ap) |
| 1441 | { |
| 1442 | struct hammer_transaction trans; |
| 1443 | struct hammer_inode *dip; |
| 1444 | int error; |
| 1445 | |
| 1446 | dip = VTOI(ap->a_dvp); |
| 1447 | |
| 1448 | if (hammer_nohistory(dip) == 0 && |
| 1449 | (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) { |
| 1450 | return (error); |
| 1451 | } |
| 1452 | |
| 1453 | hammer_start_transaction(&trans, dip->hmp); |
| 1454 | error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0); |
| 1455 | hammer_done_transaction(&trans); |
| 1456 | |
| 1457 | return (error); |
| 1458 | } |
| 1459 | |
| 1460 | /* |
| 1461 | * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred } |
| 1462 | */ |
| 1463 | static |
| 1464 | int |
| 1465 | hammer_vop_nrename(struct vop_nrename_args *ap) |
| 1466 | { |
| 1467 | struct hammer_transaction trans; |
| 1468 | struct namecache *fncp; |
| 1469 | struct namecache *tncp; |
| 1470 | struct hammer_inode *fdip; |
| 1471 | struct hammer_inode *tdip; |
| 1472 | struct hammer_inode *ip; |
| 1473 | struct hammer_cursor cursor; |
| 1474 | int64_t namekey; |
| 1475 | int nlen, error; |
| 1476 | |
| 1477 | fdip = VTOI(ap->a_fdvp); |
| 1478 | tdip = VTOI(ap->a_tdvp); |
| 1479 | fncp = ap->a_fnch->ncp; |
| 1480 | tncp = ap->a_tnch->ncp; |
| 1481 | ip = VTOI(fncp->nc_vp); |
| 1482 | KKASSERT(ip != NULL); |
| 1483 | |
| 1484 | if (fdip->flags & HAMMER_INODE_RO) |
| 1485 | return (EROFS); |
| 1486 | if (tdip->flags & HAMMER_INODE_RO) |
| 1487 | return (EROFS); |
| 1488 | if (ip->flags & HAMMER_INODE_RO) |
| 1489 | return (EROFS); |
| 1490 | if ((error = hammer_checkspace(fdip->hmp, HAMMER_CHKSPC_CREATE)) != 0) |
| 1491 | return (error); |
| 1492 | |
| 1493 | hammer_start_transaction(&trans, fdip->hmp); |
| 1494 | |
| 1495 | /* |
| 1496 | * Remove tncp from the target directory and then link ip as |
| 1497 | * tncp. XXX pass trans to dounlink |
| 1498 | * |
| 1499 | * Force the inode sync-time to match the transaction so it is |
| 1500 | * in-sync with the creation of the target directory entry. |
| 1501 | */ |
| 1502 | error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp, ap->a_cred, 0); |
| 1503 | if (error == 0 || error == ENOENT) { |
| 1504 | error = hammer_ip_add_directory(&trans, tdip, |
| 1505 | tncp->nc_name, tncp->nc_nlen, |
| 1506 | ip); |
| 1507 | if (error == 0) { |
| 1508 | ip->ino_data.parent_obj_id = tdip->obj_id; |
| 1509 | hammer_modify_inode(ip, HAMMER_INODE_DDIRTY); |
| 1510 | } |
| 1511 | } |
| 1512 | if (error) |
| 1513 | goto failed; /* XXX */ |
| 1514 | |
| 1515 | /* |
| 1516 | * Locate the record in the originating directory and remove it. |
| 1517 | * |
| 1518 | * Calculate the namekey and setup the key range for the scan. This |
| 1519 | * works kinda like a chained hash table where the lower 32 bits |
| 1520 | * of the namekey synthesize the chain. |
| 1521 | * |
| 1522 | * The key range is inclusive of both key_beg and key_end. |
| 1523 | */ |
| 1524 | namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen); |
| 1525 | retry: |
| 1526 | hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip); |
| 1527 | cursor.key_beg.localization = fdip->obj_localization + |
| 1528 | HAMMER_LOCALIZE_MISC; |
| 1529 | cursor.key_beg.obj_id = fdip->obj_id; |
| 1530 | cursor.key_beg.key = namekey; |
| 1531 | cursor.key_beg.create_tid = 0; |
| 1532 | cursor.key_beg.delete_tid = 0; |
| 1533 | cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY; |
| 1534 | cursor.key_beg.obj_type = 0; |
| 1535 | |
| 1536 | cursor.key_end = cursor.key_beg; |
| 1537 | cursor.key_end.key |= 0xFFFFFFFFULL; |
| 1538 | cursor.asof = fdip->obj_asof; |
| 1539 | cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF; |
| 1540 | |
| 1541 | /* |
| 1542 | * Scan all matching records (the chain), locate the one matching |
| 1543 | * the requested path component. |
| 1544 | * |
| 1545 | * The hammer_ip_*() functions merge in-memory records with on-disk |
| 1546 | * records for the purposes of the search. |
| 1547 | */ |
| 1548 | error = hammer_ip_first(&cursor); |
| 1549 | while (error == 0) { |
| 1550 | if (hammer_ip_resolve_data(&cursor) != 0) |
| 1551 | break; |
| 1552 | nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF; |
| 1553 | KKASSERT(nlen > 0); |
| 1554 | if (fncp->nc_nlen == nlen && |
| 1555 | bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) { |
| 1556 | break; |
| 1557 | } |
| 1558 | error = hammer_ip_next(&cursor); |
| 1559 | } |
| 1560 | |
| 1561 | /* |
| 1562 | * If all is ok we have to get the inode so we can adjust nlinks. |
| 1563 | * |
| 1564 | * WARNING: hammer_ip_del_directory() may have to terminate the |
| 1565 | * cursor to avoid a recursion. It's ok to call hammer_done_cursor() |
| 1566 | * twice. |
| 1567 | */ |
| 1568 | if (error == 0) |
| 1569 | error = hammer_ip_del_directory(&trans, &cursor, fdip, ip); |
| 1570 | |
| 1571 | /* |
| 1572 | * XXX A deadlock here will break rename's atomicy for the purposes |
| 1573 | * of crash recovery. |
| 1574 | */ |
| 1575 | if (error == EDEADLK) { |
| 1576 | hammer_done_cursor(&cursor); |
| 1577 | goto retry; |
| 1578 | } |
| 1579 | |
| 1580 | /* |
| 1581 | * Cleanup and tell the kernel that the rename succeeded. |
| 1582 | */ |
| 1583 | hammer_done_cursor(&cursor); |
| 1584 | if (error == 0) |
| 1585 | cache_rename(ap->a_fnch, ap->a_tnch); |
| 1586 | |
| 1587 | failed: |
| 1588 | hammer_done_transaction(&trans); |
| 1589 | return (error); |
| 1590 | } |
| 1591 | |
| 1592 | /* |
| 1593 | * hammer_vop_nrmdir { nch, dvp, cred } |
| 1594 | */ |
| 1595 | static |
| 1596 | int |
| 1597 | hammer_vop_nrmdir(struct vop_nrmdir_args *ap) |
| 1598 | { |
| 1599 | struct hammer_transaction trans; |
| 1600 | struct hammer_inode *dip; |
| 1601 | int error; |
| 1602 | |
| 1603 | dip = VTOI(ap->a_dvp); |
| 1604 | |
| 1605 | if (hammer_nohistory(dip) == 0 && |
| 1606 | (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) { |
| 1607 | return (error); |
| 1608 | } |
| 1609 | |
| 1610 | hammer_start_transaction(&trans, dip->hmp); |
| 1611 | error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0); |
| 1612 | hammer_done_transaction(&trans); |
| 1613 | |
| 1614 | return (error); |
| 1615 | } |
| 1616 | |
| 1617 | /* |
| 1618 | * hammer_vop_setattr { vp, vap, cred } |
| 1619 | */ |
| 1620 | static |
| 1621 | int |
| 1622 | hammer_vop_setattr(struct vop_setattr_args *ap) |
| 1623 | { |
| 1624 | struct hammer_transaction trans; |
| 1625 | struct vattr *vap; |
| 1626 | struct hammer_inode *ip; |
| 1627 | int modflags; |
| 1628 | int error; |
| 1629 | int truncating; |
| 1630 | int blksize; |
| 1631 | int64_t aligned_size; |
| 1632 | u_int32_t flags; |
| 1633 | |
| 1634 | vap = ap->a_vap; |
| 1635 | ip = ap->a_vp->v_data; |
| 1636 | modflags = 0; |
| 1637 | |
| 1638 | if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) |
| 1639 | return(EROFS); |
| 1640 | if (ip->flags & HAMMER_INODE_RO) |
| 1641 | return (EROFS); |
| 1642 | if (hammer_nohistory(ip) == 0 && |
| 1643 | (error = hammer_checkspace(ip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) { |
| 1644 | return (error); |
| 1645 | } |
| 1646 | |
| 1647 | hammer_start_transaction(&trans, ip->hmp); |
| 1648 | error = 0; |
| 1649 | |
| 1650 | if (vap->va_flags != VNOVAL) { |
| 1651 | flags = ip->ino_data.uflags; |
| 1652 | error = vop_helper_setattr_flags(&flags, vap->va_flags, |
| 1653 | hammer_to_unix_xid(&ip->ino_data.uid), |
| 1654 | ap->a_cred); |
| 1655 | if (error == 0) { |
| 1656 | if (ip->ino_data.uflags != flags) { |
| 1657 | ip->ino_data.uflags = flags; |
| 1658 | modflags |= HAMMER_INODE_DDIRTY; |
| 1659 | } |
| 1660 | if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) { |
| 1661 | error = 0; |
| 1662 | goto done; |
| 1663 | } |
| 1664 | } |
| 1665 | goto done; |
| 1666 | } |
| 1667 | if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) { |
| 1668 | error = EPERM; |
| 1669 | goto done; |
| 1670 | } |
| 1671 | if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) { |
| 1672 | mode_t cur_mode = ip->ino_data.mode; |
| 1673 | uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid); |
| 1674 | gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid); |
| 1675 | uuid_t uuid_uid; |
| 1676 | uuid_t uuid_gid; |
| 1677 | |
| 1678 | error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid, |
| 1679 | ap->a_cred, |
| 1680 | &cur_uid, &cur_gid, &cur_mode); |
| 1681 | if (error == 0) { |
| 1682 | hammer_guid_to_uuid(&uuid_uid, cur_uid); |
| 1683 | hammer_guid_to_uuid(&uuid_gid, cur_gid); |
| 1684 | if (bcmp(&uuid_uid, &ip->ino_data.uid, |
| 1685 | sizeof(uuid_uid)) || |
| 1686 | bcmp(&uuid_gid, &ip->ino_data.gid, |
| 1687 | sizeof(uuid_gid)) || |
| 1688 | ip->ino_data.mode != cur_mode |
| 1689 | ) { |
| 1690 | ip->ino_data.uid = uuid_uid; |
| 1691 | ip->ino_data.gid = uuid_gid; |
| 1692 | ip->ino_data.mode = cur_mode; |
| 1693 | } |
| 1694 | modflags |= HAMMER_INODE_DDIRTY; |
| 1695 | } |
| 1696 | } |
| 1697 | while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) { |
| 1698 | switch(ap->a_vp->v_type) { |
| 1699 | case VREG: |
| 1700 | if (vap->va_size == ip->ino_data.size) |
| 1701 | break; |
| 1702 | /* |
| 1703 | * XXX break atomicy, we can deadlock the backend |
| 1704 | * if we do not release the lock. Probably not a |
| 1705 | * big deal here. |
| 1706 | */ |
| 1707 | blksize = hammer_blocksize(vap->va_size); |
| 1708 | if (vap->va_size < ip->ino_data.size) { |
| 1709 | vtruncbuf(ap->a_vp, vap->va_size, blksize); |
| 1710 | truncating = 1; |
| 1711 | } else { |
| 1712 | vnode_pager_setsize(ap->a_vp, vap->va_size); |
| 1713 | truncating = 0; |
| 1714 | } |
| 1715 | ip->ino_data.size = vap->va_size; |
| 1716 | modflags |= HAMMER_INODE_DDIRTY; |
| 1717 | |
| 1718 | /* |
| 1719 | * on-media truncation is cached in the inode until |
| 1720 | * the inode is synchronized. |
| 1721 | */ |
| 1722 | if (truncating) { |
| 1723 | hammer_ip_frontend_trunc(ip, vap->va_size); |
| 1724 | #ifdef DEBUG_TRUNCATE |
| 1725 | if (HammerTruncIp == NULL) |
| 1726 | HammerTruncIp = ip; |
| 1727 | #endif |
| 1728 | if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) { |
| 1729 | ip->flags |= HAMMER_INODE_TRUNCATED; |
| 1730 | ip->trunc_off = vap->va_size; |
| 1731 | #ifdef DEBUG_TRUNCATE |
| 1732 | if (ip == HammerTruncIp) |
| 1733 | kprintf("truncate1 %016llx\n", ip->trunc_off); |
| 1734 | #endif |
| 1735 | } else if (ip->trunc_off > vap->va_size) { |
| 1736 | ip->trunc_off = vap->va_size; |
| 1737 | #ifdef DEBUG_TRUNCATE |
| 1738 | if (ip == HammerTruncIp) |
| 1739 | kprintf("truncate2 %016llx\n", ip->trunc_off); |
| 1740 | #endif |
| 1741 | } else { |
| 1742 | #ifdef DEBUG_TRUNCATE |
| 1743 | if (ip == HammerTruncIp) |
| 1744 | kprintf("truncate3 %016llx (ignored)\n", vap->va_size); |
| 1745 | #endif |
| 1746 | } |
| 1747 | } |
| 1748 | |
| 1749 | /* |
| 1750 | * If truncating we have to clean out a portion of |
| 1751 | * the last block on-disk. We do this in the |
| 1752 | * front-end buffer cache. |
| 1753 | */ |
| 1754 | aligned_size = (vap->va_size + (blksize - 1)) & |
| 1755 | ~(int64_t)(blksize - 1); |
| 1756 | if (truncating && vap->va_size < aligned_size) { |
| 1757 | struct buf *bp; |
| 1758 | int offset; |
| 1759 | |
| 1760 | aligned_size -= blksize; |
| 1761 | |
| 1762 | offset = (int)vap->va_size & (blksize - 1); |
| 1763 | error = bread(ap->a_vp, aligned_size, |
| 1764 | blksize, &bp); |
| 1765 | hammer_ip_frontend_trunc(ip, aligned_size); |
| 1766 | if (error == 0) { |
| 1767 | bzero(bp->b_data + offset, |
| 1768 | blksize - offset); |
| 1769 | bdwrite(bp); |
| 1770 | } else { |
| 1771 | kprintf("ERROR %d\n", error); |
| 1772 | brelse(bp); |
| 1773 | } |
| 1774 | } |
| 1775 | break; |
| 1776 | case VDATABASE: |
| 1777 | if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) { |
| 1778 | ip->flags |= HAMMER_INODE_TRUNCATED; |
| 1779 | ip->trunc_off = vap->va_size; |
| 1780 | } else if (ip->trunc_off > vap->va_size) { |
| 1781 | ip->trunc_off = vap->va_size; |
| 1782 | } |
| 1783 | hammer_ip_frontend_trunc(ip, vap->va_size); |
| 1784 | ip->ino_data.size = vap->va_size; |
| 1785 | modflags |= HAMMER_INODE_DDIRTY; |
| 1786 | break; |
| 1787 | default: |
| 1788 | error = EINVAL; |
| 1789 | goto done; |
| 1790 | } |
| 1791 | break; |
| 1792 | } |
| 1793 | if (vap->va_atime.tv_sec != VNOVAL) { |
| 1794 | ip->ino_data.atime = |
| 1795 | hammer_timespec_to_time(&vap->va_atime); |
| 1796 | modflags |= HAMMER_INODE_ATIME; |
| 1797 | } |
| 1798 | if (vap->va_mtime.tv_sec != VNOVAL) { |
| 1799 | ip->ino_data.mtime = |
| 1800 | hammer_timespec_to_time(&vap->va_mtime); |
| 1801 | modflags |= HAMMER_INODE_MTIME; |
| 1802 | } |
| 1803 | if (vap->va_mode != (mode_t)VNOVAL) { |
| 1804 | mode_t cur_mode = ip->ino_data.mode; |
| 1805 | uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid); |
| 1806 | gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid); |
| 1807 | |
| 1808 | error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred, |
| 1809 | cur_uid, cur_gid, &cur_mode); |
| 1810 | if (error == 0 && ip->ino_data.mode != cur_mode) { |
| 1811 | ip->ino_data.mode = cur_mode; |
| 1812 | modflags |= HAMMER_INODE_DDIRTY; |
| 1813 | } |
| 1814 | } |
| 1815 | done: |
| 1816 | if (error == 0) |
| 1817 | hammer_modify_inode(ip, modflags); |
| 1818 | hammer_done_transaction(&trans); |
| 1819 | return (error); |
| 1820 | } |
| 1821 | |
| 1822 | /* |
| 1823 | * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target } |
| 1824 | */ |
| 1825 | static |
| 1826 | int |
| 1827 | hammer_vop_nsymlink(struct vop_nsymlink_args *ap) |
| 1828 | { |
| 1829 | struct hammer_transaction trans; |
| 1830 | struct hammer_inode *dip; |
| 1831 | struct hammer_inode *nip; |
| 1832 | struct nchandle *nch; |
| 1833 | hammer_record_t record; |
| 1834 | int error; |
| 1835 | int bytes; |
| 1836 | |
| 1837 | ap->a_vap->va_type = VLNK; |
| 1838 | |
| 1839 | nch = ap->a_nch; |
| 1840 | dip = VTOI(ap->a_dvp); |
| 1841 | |
| 1842 | if (dip->flags & HAMMER_INODE_RO) |
| 1843 | return (EROFS); |
| 1844 | if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0) |
| 1845 | return (error); |
| 1846 | |
| 1847 | /* |
| 1848 | * Create a transaction to cover the operations we perform. |
| 1849 | */ |
| 1850 | hammer_start_transaction(&trans, dip->hmp); |
| 1851 | |
| 1852 | /* |
| 1853 | * Create a new filesystem object of the requested type. The |
| 1854 | * returned inode will be referenced but not locked. |
| 1855 | */ |
| 1856 | |
| 1857 | error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, |
| 1858 | dip, NULL, &nip); |
| 1859 | if (error) { |
| 1860 | hammer_done_transaction(&trans); |
| 1861 | *ap->a_vpp = NULL; |
| 1862 | return (error); |
| 1863 | } |
| 1864 | |
| 1865 | /* |
| 1866 | * Add a record representing the symlink. symlink stores the link |
| 1867 | * as pure data, not a string, and is no \0 terminated. |
| 1868 | */ |
| 1869 | if (error == 0) { |
| 1870 | bytes = strlen(ap->a_target); |
| 1871 | |
| 1872 | if (bytes <= HAMMER_INODE_BASESYMLEN) { |
| 1873 | bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes); |
| 1874 | } else { |
| 1875 | record = hammer_alloc_mem_record(nip, bytes); |
| 1876 | record->type = HAMMER_MEM_RECORD_GENERAL; |
| 1877 | |
| 1878 | record->leaf.base.localization = nip->obj_localization + |
| 1879 | HAMMER_LOCALIZE_MISC; |
| 1880 | record->leaf.base.key = HAMMER_FIXKEY_SYMLINK; |
| 1881 | record->leaf.base.rec_type = HAMMER_RECTYPE_FIX; |
| 1882 | record->leaf.data_len = bytes; |
| 1883 | KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0); |
| 1884 | bcopy(ap->a_target, record->data->symlink.name, bytes); |
| 1885 | error = hammer_ip_add_record(&trans, record); |
| 1886 | } |
| 1887 | |
| 1888 | /* |
| 1889 | * Set the file size to the length of the link. |
| 1890 | */ |
| 1891 | if (error == 0) { |
| 1892 | nip->ino_data.size = bytes; |
| 1893 | hammer_modify_inode(nip, HAMMER_INODE_DDIRTY); |
| 1894 | } |
| 1895 | } |
| 1896 | if (error == 0) |
| 1897 | error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name, |
| 1898 | nch->ncp->nc_nlen, nip); |
| 1899 | |
| 1900 | /* |
| 1901 | * Finish up. |
| 1902 | */ |
| 1903 | if (error) { |
| 1904 | hammer_rel_inode(nip, 0); |
| 1905 | *ap->a_vpp = NULL; |
| 1906 | } else { |
| 1907 | error = hammer_get_vnode(nip, ap->a_vpp); |
| 1908 | hammer_rel_inode(nip, 0); |
| 1909 | if (error == 0) { |
| 1910 | cache_setunresolved(ap->a_nch); |
| 1911 | cache_setvp(ap->a_nch, *ap->a_vpp); |
| 1912 | } |
| 1913 | } |
| 1914 | hammer_done_transaction(&trans); |
| 1915 | return (error); |
| 1916 | } |
| 1917 | |
| 1918 | /* |
| 1919 | * hammer_vop_nwhiteout { nch, dvp, cred, flags } |
| 1920 | */ |
| 1921 | static |
| 1922 | int |
| 1923 | hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap) |
| 1924 | { |
| 1925 | struct hammer_transaction trans; |
| 1926 | struct hammer_inode *dip; |
| 1927 | int error; |
| 1928 | |
| 1929 | dip = VTOI(ap->a_dvp); |
| 1930 | |
| 1931 | if (hammer_nohistory(dip) == 0 && |
| 1932 | (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0) { |
| 1933 | return (error); |
| 1934 | } |
| 1935 | |
| 1936 | hammer_start_transaction(&trans, dip->hmp); |
| 1937 | error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, |
| 1938 | ap->a_cred, ap->a_flags); |
| 1939 | hammer_done_transaction(&trans); |
| 1940 | |
| 1941 | return (error); |
| 1942 | } |
| 1943 | |
| 1944 | /* |
| 1945 | * hammer_vop_ioctl { vp, command, data, fflag, cred } |
| 1946 | */ |
| 1947 | static |
| 1948 | int |
| 1949 | hammer_vop_ioctl(struct vop_ioctl_args *ap) |
| 1950 | { |
| 1951 | struct hammer_inode *ip = ap->a_vp->v_data; |
| 1952 | |
| 1953 | return(hammer_ioctl(ip, ap->a_command, ap->a_data, |
| 1954 | ap->a_fflag, ap->a_cred)); |
| 1955 | } |
| 1956 | |
| 1957 | static |
| 1958 | int |
| 1959 | hammer_vop_mountctl(struct vop_mountctl_args *ap) |
| 1960 | { |
| 1961 | struct mount *mp; |
| 1962 | int error; |
| 1963 | |
| 1964 | mp = ap->a_head.a_ops->head.vv_mount; |
| 1965 | |
| 1966 | switch(ap->a_op) { |
| 1967 | case MOUNTCTL_SET_EXPORT: |
| 1968 | if (ap->a_ctllen != sizeof(struct export_args)) |
| 1969 | error = EINVAL; |
| 1970 | error = hammer_vfs_export(mp, ap->a_op, |
| 1971 | (const struct export_args *)ap->a_ctl); |
| 1972 | break; |
| 1973 | default: |
| 1974 | error = journal_mountctl(ap); |
| 1975 | break; |
| 1976 | } |
| 1977 | return(error); |
| 1978 | } |
| 1979 | |
| 1980 | /* |
| 1981 | * hammer_vop_strategy { vp, bio } |
| 1982 | * |
| 1983 | * Strategy call, used for regular file read & write only. Note that the |
| 1984 | * bp may represent a cluster. |
| 1985 | * |
| 1986 | * To simplify operation and allow better optimizations in the future, |
| 1987 | * this code does not make any assumptions with regards to buffer alignment |
| 1988 | * or size. |
| 1989 | */ |
| 1990 | static |
| 1991 | int |
| 1992 | hammer_vop_strategy(struct vop_strategy_args *ap) |
| 1993 | { |
| 1994 | struct buf *bp; |
| 1995 | int error; |
| 1996 | |
| 1997 | bp = ap->a_bio->bio_buf; |
| 1998 | |
| 1999 | switch(bp->b_cmd) { |
| 2000 | case BUF_CMD_READ: |
| 2001 | error = hammer_vop_strategy_read(ap); |
| 2002 | break; |
| 2003 | case BUF_CMD_WRITE: |
| 2004 | error = hammer_vop_strategy_write(ap); |
| 2005 | break; |
| 2006 | default: |
| 2007 | bp->b_error = error = EINVAL; |
| 2008 | bp->b_flags |= B_ERROR; |
| 2009 | biodone(ap->a_bio); |
| 2010 | break; |
| 2011 | } |
| 2012 | return (error); |
| 2013 | } |
| 2014 | |
| 2015 | /* |
| 2016 | * Read from a regular file. Iterate the related records and fill in the |
| 2017 | * BIO/BUF. Gaps are zero-filled. |
| 2018 | * |
| 2019 | * The support code in hammer_object.c should be used to deal with mixed |
| 2020 | * in-memory and on-disk records. |
| 2021 | * |
| 2022 | * NOTE: Can be called from the cluster code with an oversized buf. |
| 2023 | * |
| 2024 | * XXX atime update |
| 2025 | */ |
| 2026 | static |
| 2027 | int |
| 2028 | hammer_vop_strategy_read(struct vop_strategy_args *ap) |
| 2029 | { |
| 2030 | struct hammer_transaction trans; |
| 2031 | struct hammer_inode *ip; |
| 2032 | struct hammer_cursor cursor; |
| 2033 | hammer_base_elm_t base; |
| 2034 | hammer_off_t disk_offset; |
| 2035 | struct bio *bio; |
| 2036 | struct bio *nbio; |
| 2037 | struct buf *bp; |
| 2038 | int64_t rec_offset; |
| 2039 | int64_t ran_end; |
| 2040 | int64_t tmp64; |
| 2041 | int error; |
| 2042 | int boff; |
| 2043 | int roff; |
| 2044 | int n; |
| 2045 | |
| 2046 | bio = ap->a_bio; |
| 2047 | bp = bio->bio_buf; |
| 2048 | ip = ap->a_vp->v_data; |
| 2049 | |
| 2050 | /* |
| 2051 | * The zone-2 disk offset may have been set by the cluster code via |
| 2052 | * a BMAP operation, or else should be NOOFFSET. |
| 2053 | * |
| 2054 | * Checking the high bits for a match against zone-2 should suffice. |
| 2055 | */ |
| 2056 | nbio = push_bio(bio); |
| 2057 | if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) == |
| 2058 | HAMMER_ZONE_RAW_BUFFER) { |
| 2059 | error = hammer_io_direct_read(ip->hmp, nbio); |
| 2060 | return (error); |
| 2061 | } |
| 2062 | |
| 2063 | /* |
| 2064 | * Well, that sucked. Do it the hard way. If all the stars are |
| 2065 | * aligned we may still be able to issue a direct-read. |
| 2066 | */ |
| 2067 | hammer_simple_transaction(&trans, ip->hmp); |
| 2068 | hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip); |
| 2069 | |
| 2070 | /* |
| 2071 | * Key range (begin and end inclusive) to scan. Note that the key's |
| 2072 | * stored in the actual records represent BASE+LEN, not BASE. The |
| 2073 | * first record containing bio_offset will have a key > bio_offset. |
| 2074 | */ |
| 2075 | cursor.key_beg.localization = ip->obj_localization + |
| 2076 | HAMMER_LOCALIZE_MISC; |
| 2077 | cursor.key_beg.obj_id = ip->obj_id; |
| 2078 | cursor.key_beg.create_tid = 0; |
| 2079 | cursor.key_beg.delete_tid = 0; |
| 2080 | cursor.key_beg.obj_type = 0; |
| 2081 | cursor.key_beg.key = bio->bio_offset + 1; |
| 2082 | cursor.asof = ip->obj_asof; |
| 2083 | cursor.flags |= HAMMER_CURSOR_ASOF; |
| 2084 | |
| 2085 | cursor.key_end = cursor.key_beg; |
| 2086 | KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE); |
| 2087 | #if 0 |
| 2088 | if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) { |
| 2089 | cursor.key_beg.rec_type = HAMMER_RECTYPE_DB; |
| 2090 | cursor.key_end.rec_type = HAMMER_RECTYPE_DB; |
| 2091 | cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL; |
| 2092 | } else |
| 2093 | #endif |
| 2094 | { |
| 2095 | ran_end = bio->bio_offset + bp->b_bufsize; |
| 2096 | cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA; |
| 2097 | cursor.key_end.rec_type = HAMMER_RECTYPE_DATA; |
| 2098 | tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */ |
| 2099 | if (tmp64 < ran_end) |
| 2100 | cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL; |
| 2101 | else |
| 2102 | cursor.key_end.key = ran_end + MAXPHYS + 1; |
| 2103 | } |
| 2104 | cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE; |
| 2105 | |
| 2106 | error = hammer_ip_first(&cursor); |
| 2107 | boff = 0; |
| 2108 | |
| 2109 | while (error == 0) { |
| 2110 | /* |
| 2111 | * Get the base file offset of the record. The key for |
| 2112 | * data records is (base + bytes) rather then (base). |
| 2113 | */ |
| 2114 | base = &cursor.leaf->base; |
| 2115 | rec_offset = base->key - cursor.leaf->data_len; |
| 2116 | |
| 2117 | /* |
| 2118 | * Calculate the gap, if any, and zero-fill it. |
| 2119 | * |
| 2120 | * n is the offset of the start of the record verses our |
| 2121 | * current seek offset in the bio. |
| 2122 | */ |
| 2123 | n = (int)(rec_offset - (bio->bio_offset + boff)); |
| 2124 | if (n > 0) { |
| 2125 | if (n > bp->b_bufsize - boff) |
| 2126 | n = bp->b_bufsize - boff; |
| 2127 | bzero((char *)bp->b_data + boff, n); |
| 2128 | boff += n; |
| 2129 | n = 0; |
| 2130 | } |
| 2131 | |
| 2132 | /* |
| 2133 | * Calculate the data offset in the record and the number |
| 2134 | * of bytes we can copy. |
| 2135 | * |
| 2136 | * There are two degenerate cases. First, boff may already |
| 2137 | * be at bp->b_bufsize. Secondly, the data offset within |
| 2138 | * the record may exceed the record's size. |
| 2139 | */ |
| 2140 | roff = -n; |
| 2141 | rec_offset += roff; |
| 2142 | n = cursor.leaf->data_len - roff; |
| 2143 | if (n <= 0) { |
| 2144 | kprintf("strategy_read: bad n=%d roff=%d\n", n, roff); |
| 2145 | n = 0; |
| 2146 | } else if (n > bp->b_bufsize - boff) { |
| 2147 | n = bp->b_bufsize - boff; |
| 2148 | } |
| 2149 | |
| 2150 | /* |
| 2151 | * Deal with cached truncations. This cool bit of code |
| 2152 | * allows truncate()/ftruncate() to avoid having to sync |
| 2153 | * the file. |
| 2154 | * |
| 2155 | * If the frontend is truncated then all backend records are |
| 2156 | * subject to the frontend's truncation. |
| 2157 | * |
| 2158 | * If the backend is truncated then backend records on-disk |
| 2159 | * (but not in-memory) are subject to the backend's |
| 2160 | * truncation. In-memory records owned by the backend |
| 2161 | * represent data written after the truncation point on the |
| 2162 | * backend and must not be truncated. |
| 2163 | * |
| 2164 | * Truncate operations deal with frontend buffer cache |
| 2165 | * buffers and frontend-owned in-memory records synchronously. |
| 2166 | */ |
| 2167 | if (ip->flags & HAMMER_INODE_TRUNCATED) { |
| 2168 | if (hammer_cursor_ondisk(&cursor) || |
| 2169 | cursor.iprec->flush_state == HAMMER_FST_FLUSH) { |
| 2170 | if (ip->trunc_off <= rec_offset) |
| 2171 | n = 0; |
| 2172 | else if (ip->trunc_off < rec_offset + n) |
| 2173 | n = (int)(ip->trunc_off - rec_offset); |
| 2174 | } |
| 2175 | } |
| 2176 | if (ip->sync_flags & HAMMER_INODE_TRUNCATED) { |
| 2177 | if (hammer_cursor_ondisk(&cursor)) { |
| 2178 | if (ip->sync_trunc_off <= rec_offset) |
| 2179 | n = 0; |
| 2180 | else if (ip->sync_trunc_off < rec_offset + n) |
| 2181 | n = (int)(ip->sync_trunc_off - rec_offset); |
| 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | /* |
| 2186 | * Try to issue a direct read into our bio if possible, |
| 2187 | * otherwise resolve the element data into a hammer_buffer |
| 2188 | * and copy. |
| 2189 | * |
| 2190 | * The buffer on-disk should be zerod past any real |
| 2191 | * truncation point, but may not be for any synthesized |
| 2192 | * truncation point from above. |
| 2193 | */ |
| 2194 | if (boff == 0 && n == bp->b_bufsize && |
| 2195 | ((cursor.leaf->data_offset + roff) & HAMMER_BUFMASK) == 0) { |
| 2196 | disk_offset = hammer_blockmap_lookup( |
| 2197 | trans.hmp, |
| 2198 | cursor.leaf->data_offset + roff, |
| 2199 | &error); |
| 2200 | if (error) |
| 2201 | break; |
| 2202 | nbio->bio_offset = disk_offset; |
| 2203 | error = hammer_io_direct_read(trans.hmp, nbio); |
| 2204 | goto done; |
| 2205 | } else if (n) { |
| 2206 | error = hammer_ip_resolve_data(&cursor); |
| 2207 | if (error == 0) { |
| 2208 | bcopy((char *)cursor.data + roff, |
| 2209 | (char *)bp->b_data + boff, n); |
| 2210 | } |
| 2211 | } |
| 2212 | if (error) |
| 2213 | break; |
| 2214 | |
| 2215 | /* |
| 2216 | * Iterate until we have filled the request. |
| 2217 | */ |
| 2218 | boff += n; |
| 2219 | if (boff == bp->b_bufsize) |
| 2220 | break; |
| 2221 | error = hammer_ip_next(&cursor); |
| 2222 | } |
| 2223 | |
| 2224 | /* |
| 2225 | * There may have been a gap after the last record |
| 2226 | */ |
| 2227 | if (error == ENOENT) |
| 2228 | error = 0; |
| 2229 | if (error == 0 && boff != bp->b_bufsize) { |
| 2230 | KKASSERT(boff < bp->b_bufsize); |
| 2231 | bzero((char *)bp->b_data + boff, bp->b_bufsize - boff); |
| 2232 | /* boff = bp->b_bufsize; */ |
| 2233 | } |
| 2234 | bp->b_resid = 0; |
| 2235 | bp->b_error = error; |
| 2236 | if (error) |
| 2237 | bp->b_flags |= B_ERROR; |
| 2238 | biodone(ap->a_bio); |
| 2239 | |
| 2240 | done: |
| 2241 | if (cursor.node) |
| 2242 | hammer_cache_node(&ip->cache[1], cursor.node); |
| 2243 | hammer_done_cursor(&cursor); |
| 2244 | hammer_done_transaction(&trans); |
| 2245 | return(error); |
| 2246 | } |
| 2247 | |
| 2248 | /* |
| 2249 | * BMAP operation - used to support cluster_read() only. |
| 2250 | * |
| 2251 | * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb) |
| 2252 | * |
| 2253 | * This routine may return EOPNOTSUPP if the opration is not supported for |
| 2254 | * the specified offset. The contents of the pointer arguments do not |
| 2255 | * need to be initialized in that case. |
| 2256 | * |
| 2257 | * If a disk address is available and properly aligned return 0 with |
| 2258 | * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately |
| 2259 | * to the run-length relative to that offset. Callers may assume that |
| 2260 | * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently |
| 2261 | * large, so return EOPNOTSUPP if it is not sufficiently large. |
| 2262 | */ |
| 2263 | static |
| 2264 | int |
| 2265 | hammer_vop_bmap(struct vop_bmap_args *ap) |
| 2266 | { |
| 2267 | struct hammer_transaction trans; |
| 2268 | struct hammer_inode *ip; |
| 2269 | struct hammer_cursor cursor; |
| 2270 | hammer_base_elm_t base; |
| 2271 | int64_t rec_offset; |
| 2272 | int64_t ran_end; |
| 2273 | int64_t tmp64; |
| 2274 | int64_t base_offset; |
| 2275 | int64_t base_disk_offset; |
| 2276 | int64_t last_offset; |
| 2277 | hammer_off_t last_disk_offset; |
| 2278 | hammer_off_t disk_offset; |
| 2279 | int rec_len; |
| 2280 | int error; |
| 2281 | int blksize; |
| 2282 | |
| 2283 | ip = ap->a_vp->v_data; |
| 2284 | |
| 2285 | /* |
| 2286 | * We can only BMAP regular files. We can't BMAP database files, |
| 2287 | * directories, etc. |
| 2288 | */ |
| 2289 | if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE) |
| 2290 | return(EOPNOTSUPP); |
| 2291 | |
| 2292 | /* |
| 2293 | * bmap is typically called with runp/runb both NULL when used |
| 2294 | * for writing. We do not support BMAP for writing atm. |
| 2295 | */ |
| 2296 | if (ap->a_cmd != BUF_CMD_READ) |
| 2297 | return(EOPNOTSUPP); |
| 2298 | |
| 2299 | /* |
| 2300 | * Scan the B-Tree to acquire blockmap addresses, then translate |
| 2301 | * to raw addresses. |
| 2302 | */ |
| 2303 | hammer_simple_transaction(&trans, ip->hmp); |
| 2304 | #if 0 |
| 2305 | kprintf("bmap_beg %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]); |
| 2306 | #endif |
| 2307 | hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip); |
| 2308 | |
| 2309 | /* |
| 2310 | * Key range (begin and end inclusive) to scan. Note that the key's |
| 2311 | * stored in the actual records represent BASE+LEN, not BASE. The |
| 2312 | * first record containing bio_offset will have a key > bio_offset. |
| 2313 | */ |
| 2314 | cursor.key_beg.localization = ip->obj_localization + |
| 2315 | HAMMER_LOCALIZE_MISC; |
| 2316 | cursor.key_beg.obj_id = ip->obj_id; |
| 2317 | cursor.key_beg.create_tid = 0; |
| 2318 | cursor.key_beg.delete_tid = 0; |
| 2319 | cursor.key_beg.obj_type = 0; |
| 2320 | if (ap->a_runb) |
| 2321 | cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1; |
| 2322 | else |
| 2323 | cursor.key_beg.key = ap->a_loffset + 1; |
| 2324 | if (cursor.key_beg.key < 0) |
| 2325 | cursor.key_beg.key = 0; |
| 2326 | cursor.asof = ip->obj_asof; |
| 2327 | cursor.flags |= HAMMER_CURSOR_ASOF; |
| 2328 | |
| 2329 | cursor.key_end = cursor.key_beg; |
| 2330 | KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE); |
| 2331 | |
| 2332 | ran_end = ap->a_loffset + MAXPHYS; |
| 2333 | cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA; |
| 2334 | cursor.key_end.rec_type = HAMMER_RECTYPE_DATA; |
| 2335 | tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */ |
| 2336 | if (tmp64 < ran_end) |
| 2337 | cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL; |
| 2338 | else |
| 2339 | cursor.key_end.key = ran_end + MAXPHYS + 1; |
| 2340 | |
| 2341 | cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE; |
| 2342 | |
| 2343 | error = hammer_ip_first(&cursor); |
| 2344 | base_offset = last_offset = 0; |
| 2345 | base_disk_offset = last_disk_offset = 0; |
| 2346 | |
| 2347 | while (error == 0) { |
| 2348 | /* |
| 2349 | * Get the base file offset of the record. The key for |
| 2350 | * data records is (base + bytes) rather then (base). |
| 2351 | * |
| 2352 | * NOTE: rec_offset + rec_len may exceed the end-of-file. |
| 2353 | * The extra bytes should be zero on-disk and the BMAP op |
| 2354 | * should still be ok. |
| 2355 | */ |
| 2356 | base = &cursor.leaf->base; |
| 2357 | rec_offset = base->key - cursor.leaf->data_len; |
| 2358 | rec_len = cursor.leaf->data_len; |
| 2359 | |
| 2360 | /* |
| 2361 | * Incorporate any cached truncation. |
| 2362 | * |
| 2363 | * NOTE: Modifications to rec_len based on synthesized |
| 2364 | * truncation points remove the guarantee that any extended |
| 2365 | * data on disk is zero (since the truncations may not have |
| 2366 | * taken place on-media yet). |
| 2367 | */ |
| 2368 | if (ip->flags & HAMMER_INODE_TRUNCATED) { |
| 2369 | if (hammer_cursor_ondisk(&cursor) || |
| 2370 | cursor.iprec->flush_state == HAMMER_FST_FLUSH) { |
| 2371 | if (ip->trunc_off <= rec_offset) |
| 2372 | rec_len = 0; |
| 2373 | else if (ip->trunc_off < rec_offset + rec_len) |
| 2374 | rec_len = (int)(ip->trunc_off - rec_offset); |
| 2375 | } |
| 2376 | } |
| 2377 | if (ip->sync_flags & HAMMER_INODE_TRUNCATED) { |
| 2378 | if (hammer_cursor_ondisk(&cursor)) { |
| 2379 | if (ip->sync_trunc_off <= rec_offset) |
| 2380 | rec_len = 0; |
| 2381 | else if (ip->sync_trunc_off < rec_offset + rec_len) |
| 2382 | rec_len = (int)(ip->sync_trunc_off - rec_offset); |
| 2383 | } |
| 2384 | } |
| 2385 | |
| 2386 | /* |
| 2387 | * Accumulate information. If we have hit a discontiguous |
| 2388 | * block reset base_offset unless we are already beyond the |
| 2389 | * requested offset. If we are, that's it, we stop. |
| 2390 | */ |
| 2391 | disk_offset = hammer_blockmap_lookup(trans.hmp, |
| 2392 | cursor.leaf->data_offset, |
| 2393 | &error); |
| 2394 | if (error) |
| 2395 | break; |
| 2396 | if (rec_offset != last_offset || |
| 2397 | disk_offset != last_disk_offset) { |
| 2398 | if (rec_offset > ap->a_loffset) |
| 2399 | break; |
| 2400 | base_offset = rec_offset; |
| 2401 | base_disk_offset = disk_offset; |
| 2402 | } |
| 2403 | last_offset = rec_offset + rec_len; |
| 2404 | last_disk_offset = disk_offset + rec_len; |
| 2405 | |
| 2406 | error = hammer_ip_next(&cursor); |
| 2407 | } |
| 2408 | |
| 2409 | #if 0 |
| 2410 | kprintf("BMAP %016llx: %016llx - %016llx\n", |
| 2411 | ap->a_loffset, base_offset, last_offset); |
| 2412 | kprintf("BMAP %16s: %016llx - %016llx\n", |
| 2413 | "", base_disk_offset, last_disk_offset); |
| 2414 | #endif |
| 2415 | |
| 2416 | if (cursor.node) { |
| 2417 | hammer_cache_node(&ip->cache[1], cursor.node); |
| 2418 | #if 0 |
| 2419 | kprintf("bmap_end2 %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]); |
| 2420 | #endif |
| 2421 | } |
| 2422 | hammer_done_cursor(&cursor); |
| 2423 | hammer_done_transaction(&trans); |
| 2424 | |
| 2425 | /* |
| 2426 | * If we couldn't find any records or the records we did find were |
| 2427 | * all behind the requested offset, return failure. A forward |
| 2428 | * truncation can leave a hole w/ no on-disk records. |
| 2429 | */ |
| 2430 | if (last_offset == 0 || last_offset < ap->a_loffset) |
| 2431 | return (EOPNOTSUPP); |
| 2432 | |
| 2433 | /* |
| 2434 | * Figure out the block size at the requested offset and adjust |
| 2435 | * our limits so the cluster_read() does not create inappropriately |
| 2436 | * sized buffer cache buffers. |
| 2437 | */ |
| 2438 | blksize = hammer_blocksize(ap->a_loffset); |
| 2439 | if (hammer_blocksize(base_offset) != blksize) { |
| 2440 | base_offset = hammer_blockdemarc(base_offset, ap->a_loffset); |
| 2441 | } |
| 2442 | if (last_offset != ap->a_loffset && |
| 2443 | hammer_blocksize(last_offset - 1) != blksize) { |
| 2444 | last_offset = hammer_blockdemarc(ap->a_loffset, |
| 2445 | last_offset - 1); |
| 2446 | } |
| 2447 | |
| 2448 | /* |
| 2449 | * Returning EOPNOTSUPP simply prevents the direct-IO optimization |
| 2450 | * from occuring. |
| 2451 | */ |
| 2452 | disk_offset = base_disk_offset + (ap->a_loffset - base_offset); |
| 2453 | |
| 2454 | /* |
| 2455 | * If doffsetp is not aligned or the forward run size does |
| 2456 | * not cover a whole buffer, disallow the direct I/O. |
| 2457 | */ |
| 2458 | if ((disk_offset & HAMMER_BUFMASK) || |
| 2459 | (last_offset - ap->a_loffset) < blksize) { |
| 2460 | error = EOPNOTSUPP; |
| 2461 | } else { |
| 2462 | *ap->a_doffsetp = disk_offset; |
| 2463 | if (ap->a_runb) { |
| 2464 | *ap->a_runb = ap->a_loffset - base_offset; |
| 2465 | KKASSERT(*ap->a_runb >= 0); |
| 2466 | } |
| 2467 | if (ap->a_runp) { |
| 2468 | *ap->a_runp = last_offset - ap->a_loffset; |
| 2469 | KKASSERT(*ap->a_runp >= 0); |
| 2470 | } |
| 2471 | error = 0; |
| 2472 | } |
| 2473 | return(error); |
| 2474 | } |
| 2475 | |
| 2476 | /* |
| 2477 | * Write to a regular file. Because this is a strategy call the OS is |
| 2478 | * trying to actually get data onto the media. |
| 2479 | */ |
| 2480 | static |
| 2481 | int |
| 2482 | hammer_vop_strategy_write(struct vop_strategy_args *ap) |
| 2483 | { |
| 2484 | hammer_record_t record; |
| 2485 | hammer_mount_t hmp; |
| 2486 | hammer_inode_t ip; |
| 2487 | struct bio *bio; |
| 2488 | struct buf *bp; |
| 2489 | int blksize; |
| 2490 | int bytes; |
| 2491 | int error; |
| 2492 | |
| 2493 | bio = ap->a_bio; |
| 2494 | bp = bio->bio_buf; |
| 2495 | ip = ap->a_vp->v_data; |
| 2496 | hmp = ip->hmp; |
| 2497 | |
| 2498 | blksize = hammer_blocksize(bio->bio_offset); |
| 2499 | KKASSERT(bp->b_bufsize == blksize); |
| 2500 | |
| 2501 | if (ip->flags & HAMMER_INODE_RO) { |
| 2502 | bp->b_error = EROFS; |
| 2503 | bp->b_flags |= B_ERROR; |
| 2504 | biodone(ap->a_bio); |
| 2505 | return(EROFS); |
| 2506 | } |
| 2507 | |
| 2508 | /* |
| 2509 | * Interlock with inode destruction (no in-kernel or directory |
| 2510 | * topology visibility). If we queue new IO while trying to |
| 2511 | * destroy the inode we can deadlock the vtrunc call in |
| 2512 | * hammer_inode_unloadable_check(). |
| 2513 | */ |
| 2514 | if (ip->flags & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) { |
| 2515 | bp->b_resid = 0; |
| 2516 | biodone(ap->a_bio); |
| 2517 | return(0); |
| 2518 | } |
| 2519 | |
| 2520 | /* |
| 2521 | * Reserve space and issue a direct-write from the front-end. |
| 2522 | * NOTE: The direct_io code will hammer_bread/bcopy smaller |
| 2523 | * allocations. |
| 2524 | * |
| 2525 | * An in-memory record will be installed to reference the storage |
| 2526 | * until the flusher can get to it. |
| 2527 | * |
| 2528 | * Since we own the high level bio the front-end will not try to |
| 2529 | * do a direct-read until the write completes. |
| 2530 | * |
| 2531 | * NOTE: The only time we do not reserve a full-sized buffers |
| 2532 | * worth of data is if the file is small. We do not try to |
| 2533 | * allocate a fragment (from the small-data zone) at the end of |
| 2534 | * an otherwise large file as this can lead to wildly separated |
| 2535 | * data. |
| 2536 | */ |
| 2537 | KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0); |
| 2538 | KKASSERT(bio->bio_offset < ip->ino_data.size); |
| 2539 | if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2) |
| 2540 | bytes = bp->b_bufsize; |
| 2541 | else |
| 2542 | bytes = ((int)ip->ino_data.size + 15) & ~15; |
| 2543 | |
| 2544 | record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data, |
| 2545 | bytes, &error); |
| 2546 | if (record) { |
| 2547 | hammer_io_direct_write(hmp, &record->leaf, bio); |
| 2548 | hammer_rel_mem_record(record); |
| 2549 | if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs) |
| 2550 | hammer_flush_inode(ip, 0); |
| 2551 | } else { |
| 2552 | bp->b_bio2.bio_offset = NOOFFSET; |
| 2553 | bp->b_error = error; |
| 2554 | bp->b_flags |= B_ERROR; |
| 2555 | biodone(ap->a_bio); |
| 2556 | } |
| 2557 | return(error); |
| 2558 | } |
| 2559 | |
| 2560 | /* |
| 2561 | * dounlink - disconnect a directory entry |
| 2562 | * |
| 2563 | * XXX whiteout support not really in yet |
| 2564 | */ |
| 2565 | static int |
| 2566 | hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch, |
| 2567 | struct vnode *dvp, struct ucred *cred, int flags) |
| 2568 | { |
| 2569 | struct namecache *ncp; |
| 2570 | hammer_inode_t dip; |
| 2571 | hammer_inode_t ip; |
| 2572 | struct hammer_cursor cursor; |
| 2573 | int64_t namekey; |
| 2574 | int nlen, error; |
| 2575 | |
| 2576 | /* |
| 2577 | * Calculate the namekey and setup the key range for the scan. This |
| 2578 | * works kinda like a chained hash table where the lower 32 bits |
| 2579 | * of the namekey synthesize the chain. |
| 2580 | * |
| 2581 | * The key range is inclusive of both key_beg and key_end. |
| 2582 | */ |
| 2583 | dip = VTOI(dvp); |
| 2584 | ncp = nch->ncp; |
| 2585 | |
| 2586 | if (dip->flags & HAMMER_INODE_RO) |
| 2587 | return (EROFS); |
| 2588 | |
| 2589 | namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen); |
| 2590 | retry: |
| 2591 | hammer_init_cursor(trans, &cursor, &dip->cache[1], dip); |
| 2592 | cursor.key_beg.localization = dip->obj_localization + |
| 2593 | HAMMER_LOCALIZE_MISC; |
| 2594 | cursor.key_beg.obj_id = dip->obj_id; |
| 2595 | cursor.key_beg.key = namekey; |
| 2596 | cursor.key_beg.create_tid = 0; |
| 2597 | cursor.key_beg.delete_tid = 0; |
| 2598 | cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY; |
| 2599 | cursor.key_beg.obj_type = 0; |
| 2600 | |
| 2601 | cursor.key_end = cursor.key_beg; |
| 2602 | cursor.key_end.key |= 0xFFFFFFFFULL; |
| 2603 | cursor.asof = dip->obj_asof; |
| 2604 | cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF; |
| 2605 | |
| 2606 | /* |
| 2607 | * Scan all matching records (the chain), locate the one matching |
| 2608 | * the requested path component. info->last_error contains the |
| 2609 | * error code on search termination and could be 0, ENOENT, or |
| 2610 | * something else. |
| 2611 | * |
| 2612 | * The hammer_ip_*() functions merge in-memory records with on-disk |
| 2613 | * records for the purposes of the search. |
| 2614 | */ |
| 2615 | error = hammer_ip_first(&cursor); |
| 2616 | |
| 2617 | while (error == 0) { |
| 2618 | error = hammer_ip_resolve_data(&cursor); |
| 2619 | if (error) |
| 2620 | break; |
| 2621 | nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF; |
| 2622 | KKASSERT(nlen > 0); |
| 2623 | if (ncp->nc_nlen == nlen && |
| 2624 | bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) { |
| 2625 | break; |
| 2626 | } |
| 2627 | error = hammer_ip_next(&cursor); |
| 2628 | } |
| 2629 | |
| 2630 | /* |
| 2631 | * If all is ok we have to get the inode so we can adjust nlinks. |
| 2632 | * To avoid a deadlock with the flusher we must release the inode |
| 2633 | * lock on the directory when acquiring the inode for the entry. |
| 2634 | * |
| 2635 | * If the target is a directory, it must be empty. |
| 2636 | */ |
| 2637 | if (error == 0) { |
| 2638 | hammer_unlock(&cursor.ip->lock); |
| 2639 | ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id, |
| 2640 | dip->hmp->asof, |
| 2641 | cursor.data->entry.localization, |
| 2642 | 0, &error); |
| 2643 | hammer_lock_sh(&cursor.ip->lock); |
| 2644 | if (error == ENOENT) { |
| 2645 | kprintf("obj_id %016llx\n", cursor.data->entry.obj_id); |
| 2646 | Debugger("ENOENT unlinking object that should exist"); |
| 2647 | } |
| 2648 | |
| 2649 | /* |
| 2650 | * If we are trying to remove a directory the directory must |
| 2651 | * be empty. |
| 2652 | * |
| 2653 | * WARNING: hammer_ip_check_directory_empty() may have to |
| 2654 | * terminate the cursor to avoid a deadlock. It is ok to |
| 2655 | * call hammer_done_cursor() twice. |
| 2656 | */ |
| 2657 | if (error == 0 && ip->ino_data.obj_type == |
| 2658 | HAMMER_OBJTYPE_DIRECTORY) { |
| 2659 | error = hammer_ip_check_directory_empty(trans, ip); |
| 2660 | } |
| 2661 | |
| 2662 | /* |
| 2663 | * Delete the directory entry. |
| 2664 | * |
| 2665 | * WARNING: hammer_ip_del_directory() may have to terminate |
| 2666 | * the cursor to avoid a deadlock. It is ok to call |
| 2667 | * hammer_done_cursor() twice. |
| 2668 | */ |
| 2669 | if (error == 0) { |
| 2670 | error = hammer_ip_del_directory(trans, &cursor, |
| 2671 | dip, ip); |
| 2672 | } |
| 2673 | hammer_done_cursor(&cursor); |
| 2674 | if (error == 0) { |
| 2675 | cache_setunresolved(nch); |
| 2676 | cache_setvp(nch, NULL); |
| 2677 | /* XXX locking */ |
| 2678 | if (ip->vp) |
| 2679 | cache_inval_vp(ip->vp, CINV_DESTROY); |
| 2680 | } |
| 2681 | if (ip) |
| 2682 | hammer_rel_inode(ip, 0); |
| 2683 | } else { |
| 2684 | hammer_done_cursor(&cursor); |
| 2685 | } |
| 2686 | hammer_inode_waitreclaims(dip->hmp); |
| 2687 | if (error == EDEADLK) |
| 2688 | goto retry; |
| 2689 | |
| 2690 | return (error); |
| 2691 | } |
| 2692 | |
| 2693 | /************************************************************************ |
| 2694 | * FIFO AND SPECFS OPS * |
| 2695 | ************************************************************************ |
| 2696 | * |
| 2697 | */ |
| 2698 | |
| 2699 | static int |
| 2700 | hammer_vop_fifoclose (struct vop_close_args *ap) |
| 2701 | { |
| 2702 | /* XXX update itimes */ |
| 2703 | return (VOCALL(&fifo_vnode_vops, &ap->a_head)); |
| 2704 | } |
| 2705 | |
| 2706 | static int |
| 2707 | hammer_vop_fiforead (struct vop_read_args *ap) |
| 2708 | { |
| 2709 | int error; |
| 2710 | |
| 2711 | error = VOCALL(&fifo_vnode_vops, &ap->a_head); |
| 2712 | /* XXX update access time */ |
| 2713 | return (error); |
| 2714 | } |
| 2715 | |
| 2716 | static int |
| 2717 | hammer_vop_fifowrite (struct vop_write_args *ap) |
| 2718 | { |
| 2719 | int error; |
| 2720 | |
| 2721 | error = VOCALL(&fifo_vnode_vops, &ap->a_head); |
| 2722 | /* XXX update access time */ |
| 2723 | return (error); |
| 2724 | } |
| 2725 | |
| 2726 | static int |
| 2727 | hammer_vop_specclose (struct vop_close_args *ap) |
| 2728 | { |
| 2729 | /* XXX update itimes */ |
| 2730 | return (VOCALL(&spec_vnode_vops, &ap->a_head)); |
| 2731 | } |
| 2732 | |
| 2733 | static int |
| 2734 | hammer_vop_specread (struct vop_read_args *ap) |
| 2735 | { |
| 2736 | /* XXX update access time */ |
| 2737 | return (VOCALL(&spec_vnode_vops, &ap->a_head)); |
| 2738 | } |
| 2739 | |
| 2740 | static int |
| 2741 | hammer_vop_specwrite (struct vop_write_args *ap) |
| 2742 | { |
| 2743 | /* XXX update last change time */ |
| 2744 | return (VOCALL(&spec_vnode_vops, &ap->a_head)); |
| 2745 | } |
| 2746 | |