1 /* $NetBSD: nilfs_vnops.c,v 1.10 2011/04/24 21:35:29 rmind Exp $ */ 2 3 /* 4 * Copyright (c) 2008, 2009 Reinoud Zandijk 5 * All rights reserved. 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 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 #ifndef lint 31 __KERNEL_RCSID(0, "$NetBSD: nilfs_vnops.c,v 1.10 2011/04/24 21:35:29 rmind Exp $"); 32 #endif /* not lint */ 33 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/namei.h> 38 #include <sys/resourcevar.h> /* defines plimit structure in proc struct */ 39 #include <sys/kernel.h> 40 #include <sys/file.h> /* define FWRITE ... */ 41 #include <sys/stat.h> 42 #include <sys/buf.h> 43 #include <sys/proc.h> 44 #include <sys/mount.h> 45 #include <sys/vnode.h> 46 #include <sys/signalvar.h> 47 #include <sys/malloc.h> 48 #include <sys/dirent.h> 49 #include <sys/lockf.h> 50 #include <sys/kauth.h> 51 52 #include <miscfs/genfs/genfs.h> 53 #include <uvm/uvm_extern.h> 54 55 #include <fs/nilfs/nilfs_mount.h> 56 #include "nilfs.h" 57 #include "nilfs_subr.h" 58 #include "nilfs_bswap.h" 59 60 61 #define VTOI(vnode) ((struct nilfs_node *) (vnode)->v_data) 62 63 64 /* externs */ 65 extern int prtactive; 66 67 /* implementations of vnode functions; table follows at end */ 68 /* --------------------------------------------------------------------- */ 69 70 int 71 nilfs_inactive(void *v) 72 { 73 struct vop_inactive_args /* { 74 struct vnode *a_vp; 75 bool *a_recycle; 76 } */ *ap = v; 77 struct vnode *vp = ap->a_vp; 78 struct nilfs_node *nilfs_node = VTOI(vp); 79 80 DPRINTF(NODE, ("nilfs_inactive called for nilfs_node %p\n", VTOI(vp))); 81 82 if (nilfs_node == NULL) { 83 DPRINTF(NODE, ("nilfs_inactive: inactive NULL NILFS node\n")); 84 VOP_UNLOCK(vp); 85 return 0; 86 } 87 88 /* 89 * Optionally flush metadata to disc. If the file has not been 90 * referenced anymore in a directory we ought to free up the resources 91 * on disc if applicable. 92 */ 93 VOP_UNLOCK(vp); 94 95 return 0; 96 } 97 98 /* --------------------------------------------------------------------- */ 99 100 int 101 nilfs_reclaim(void *v) 102 { 103 struct vop_reclaim_args /* { 104 struct vnode *a_vp; 105 } */ *ap = v; 106 struct vnode *vp = ap->a_vp; 107 struct nilfs_node *nilfs_node = VTOI(vp); 108 109 DPRINTF(NODE, ("nilfs_reclaim called for node %p\n", nilfs_node)); 110 if (prtactive && vp->v_usecount > 1) 111 vprint("nilfs_reclaim(): pushing active", vp); 112 113 if (nilfs_node == NULL) { 114 DPRINTF(NODE, ("nilfs_reclaim(): null nilfsnode\n")); 115 return 0; 116 } 117 118 /* update note for closure */ 119 nilfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE); 120 121 /* purge old data from namei */ 122 cache_purge(vp); 123 124 /* dispose all node knowledge */ 125 nilfs_dispose_node(&nilfs_node); 126 127 return 0; 128 } 129 130 /* --------------------------------------------------------------------- */ 131 132 int 133 nilfs_read(void *v) 134 { 135 struct vop_read_args /* { 136 struct vnode *a_vp; 137 struct uio *a_uio; 138 int a_ioflag; 139 kauth_cred_t a_cred; 140 } */ *ap = v; 141 struct vnode *vp = ap->a_vp; 142 struct uio *uio = ap->a_uio; 143 int ioflag = ap->a_ioflag; 144 int advice = IO_ADV_DECODE(ap->a_ioflag); 145 struct uvm_object *uobj; 146 struct nilfs_node *nilfs_node = VTOI(vp); 147 uint64_t file_size; 148 vsize_t len; 149 int error; 150 int flags; 151 152 DPRINTF(READ, ("nilfs_read called\n")); 153 154 /* can this happen? some filingsystems have this check */ 155 if (uio->uio_offset < 0) 156 return EINVAL; 157 if (uio->uio_resid == 0) 158 return 0; 159 160 /* protect against rogue programs reading raw directories and links */ 161 if ((ioflag & IO_ALTSEMANTICS) == 0) { 162 if (vp->v_type == VDIR) 163 return EISDIR; 164 /* all but regular files just give EINVAL */ 165 if (vp->v_type != VREG) 166 return EINVAL; 167 } 168 169 assert(nilfs_node); 170 file_size = nilfs_rw64(nilfs_node->inode.i_size); 171 172 /* read contents using buffercache */ 173 uobj = &vp->v_uobj; 174 flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0; 175 error = 0; 176 while (uio->uio_resid > 0) { 177 /* reached end? */ 178 if (file_size <= uio->uio_offset) 179 break; 180 181 /* maximise length to file extremity */ 182 len = MIN(file_size - uio->uio_offset, uio->uio_resid); 183 if (len == 0) 184 break; 185 186 /* ubc, here we come, prepare to trap */ 187 error = ubc_uiomove(uobj, uio, len, advice, 188 UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp)); 189 if (error) 190 break; 191 } 192 193 /* note access time unless not requested */ 194 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) { 195 nilfs_node->i_flags |= IN_ACCESS; 196 if ((ioflag & IO_SYNC) == IO_SYNC) 197 error = nilfs_update(vp, NULL, NULL, NULL, UPDATE_WAIT); 198 } 199 200 return error; 201 } 202 203 /* --------------------------------------------------------------------- */ 204 205 int 206 nilfs_write(void *v) 207 { 208 struct vop_write_args /* { 209 struct vnode *a_vp; 210 struct uio *a_uio; 211 int a_ioflag; 212 kauth_cred_t a_cred; 213 } */ *ap = v; 214 struct vnode *vp = ap->a_vp; 215 struct uio *uio = ap->a_uio; 216 int ioflag = ap->a_ioflag; 217 int advice = IO_ADV_DECODE(ap->a_ioflag); 218 struct uvm_object *uobj; 219 struct nilfs_node *nilfs_node = VTOI(vp); 220 uint64_t file_size, old_size; 221 vsize_t len; 222 int error; 223 int flags, resid, extended; 224 225 DPRINTF(WRITE, ("nilfs_write called\n")); 226 227 /* can this happen? some filingsystems have this check */ 228 if (uio->uio_offset < 0) 229 return EINVAL; 230 if (uio->uio_resid == 0) 231 return 0; 232 233 /* protect against rogue programs writing raw directories or links */ 234 if ((ioflag & IO_ALTSEMANTICS) == 0) { 235 if (vp->v_type == VDIR) 236 return EISDIR; 237 /* all but regular files just give EINVAL for now */ 238 if (vp->v_type != VREG) 239 return EINVAL; 240 } 241 242 assert(nilfs_node); 243 panic("nilfs_write() called\n"); 244 return EIO; 245 246 /* remember old file size */ 247 assert(nilfs_node); 248 file_size = nilfs_rw64(nilfs_node->inode.i_size); 249 old_size = file_size; 250 251 /* if explicitly asked to append, uio_offset can be wrong? */ 252 if (ioflag & IO_APPEND) 253 uio->uio_offset = file_size; 254 255 #if 0 256 extended = (uio->uio_offset + uio->uio_resid > file_size); 257 if (extended) { 258 DPRINTF(WRITE, ("extending file from %"PRIu64" to %"PRIu64"\n", 259 file_size, uio->uio_offset + uio->uio_resid)); 260 error = nilfs_grow_node(nilfs_node, uio->uio_offset + uio->uio_resid); 261 if (error) 262 return error; 263 file_size = uio->uio_offset + uio->uio_resid; 264 } 265 #endif 266 267 /* write contents using buffercache */ 268 uobj = &vp->v_uobj; 269 flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0; 270 resid = uio->uio_resid; 271 error = 0; 272 273 uvm_vnp_setwritesize(vp, file_size); 274 while (uio->uio_resid > 0) { 275 /* maximise length to file extremity */ 276 len = MIN(file_size - uio->uio_offset, uio->uio_resid); 277 if (len == 0) 278 break; 279 280 /* ubc, here we come, prepare to trap */ 281 error = ubc_uiomove(uobj, uio, len, advice, 282 UBC_WRITE | UBC_UNMAP_FLAG(vp)); 283 if (error) 284 break; 285 } 286 uvm_vnp_setsize(vp, file_size); 287 288 /* mark node changed and request update */ 289 nilfs_node->i_flags |= IN_CHANGE | IN_UPDATE; 290 291 /* 292 * XXX TODO FFS has code here to reset setuid & setgid when we're not 293 * the superuser as a precaution against tampering. 294 */ 295 296 /* if we wrote a thing, note write action on vnode */ 297 if (resid > uio->uio_resid) 298 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0)); 299 300 if (error) { 301 /* bring back file size to its former size */ 302 /* take notice of its errors? */ 303 // (void) nilfs_chsize(vp, (u_quad_t) old_size, NOCRED); 304 305 /* roll back uio */ 306 uio->uio_offset -= resid - uio->uio_resid; 307 uio->uio_resid = resid; 308 } else { 309 /* if we write and we're synchronous, update node */ 310 if ((resid > uio->uio_resid) && ((ioflag & IO_SYNC) == IO_SYNC)) 311 error = nilfs_update(vp, NULL, NULL, NULL, UPDATE_WAIT); 312 } 313 314 return error; 315 } 316 317 318 /* --------------------------------------------------------------------- */ 319 320 /* 321 * bmap functionality that translates logical block numbers to the virtual 322 * block numbers to be stored on the vnode itself. 323 */ 324 325 int 326 nilfs_trivial_bmap(void *v) 327 { 328 struct vop_bmap_args /* { 329 struct vnode *a_vp; 330 daddr_t a_bn; 331 struct vnode **a_vpp; 332 daddr_t *a_bnp; 333 int *a_runp; 334 } */ *ap = v; 335 struct vnode *vp = ap->a_vp; /* our node */ 336 struct vnode **vpp = ap->a_vpp; /* return node */ 337 daddr_t *bnp = ap->a_bnp; /* translated */ 338 daddr_t bn = ap->a_bn; /* origional */ 339 int *runp = ap->a_runp; 340 struct nilfs_node *node = VTOI(vp); 341 uint64_t *l2vmap; 342 uint32_t blocksize; 343 int blks, run, error; 344 345 DPRINTF(TRANSLATE, ("nilfs_bmap() called\n")); 346 /* XXX could return `-1' to indicate holes/zero's */ 347 348 blocksize = node->nilfsdev->blocksize; 349 blks = MAXPHYS / blocksize; 350 351 /* get mapping memory */ 352 l2vmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK); 353 354 /* get virtual block numbers for the vnode's buffer span */ 355 error = nilfs_btree_nlookup(node, bn, blks, l2vmap); 356 if (error) { 357 free(l2vmap, M_TEMP); 358 return error; 359 } 360 361 /* store virtual blocks on our own vp */ 362 if (vpp) 363 *vpp = vp; 364 365 /* start at virt[0] */ 366 *bnp = l2vmap[0]; 367 368 /* get runlength */ 369 run = 1; 370 while ((run < blks) && (l2vmap[run] == *bnp + run)) 371 run++; 372 373 /* set runlength */ 374 if (runp) 375 *runp = run; 376 377 DPRINTF(TRANSLATE, ("\tstart %"PRIu64" -> %"PRIu64" run %d\n", 378 bn, *bnp, run)); 379 380 /* mark not translated on virtual block number 0 */ 381 if (*bnp == 0) 382 *bnp = -1; 383 384 /* return success */ 385 free(l2vmap, M_TEMP); 386 return 0; 387 } 388 389 /* --------------------------------------------------------------------- */ 390 391 static void 392 nilfs_read_filebuf(struct nilfs_node *node, struct buf *bp) 393 { 394 struct nilfs_device *nilfsdev = node->nilfsdev; 395 struct buf *nbp; 396 uint64_t *l2vmap, *v2pmap; 397 uint64_t from, blks; 398 uint32_t blocksize, buf_offset; 399 uint8_t *buf_pos; 400 int blk2dev = nilfsdev->blocksize / DEV_BSIZE; 401 int i, error; 402 403 /* 404 * Translate all the block sectors into a series of buffers to read 405 * asynchronously from the nilfs device. Note that this lookup may 406 * induce readin's too. 407 */ 408 409 blocksize = nilfsdev->blocksize; 410 411 from = bp->b_blkno; 412 blks = bp->b_bcount / blocksize; 413 414 DPRINTF(READ, ("\tread in from inode %"PRIu64" blkno %"PRIu64" " 415 "+ %"PRIu64" blocks\n", node->ino, from, blks)); 416 417 DPRINTF(READ, ("\t\tblkno %"PRIu64" " 418 "+ %d bytes\n", bp->b_blkno, bp->b_bcount)); 419 420 /* get mapping memory */ 421 l2vmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK); 422 v2pmap = malloc(sizeof(uint64_t) * blks, M_TEMP, M_WAITOK); 423 424 /* get virtual block numbers for the vnode's buffer span */ 425 for (i = 0; i < blks; i++) 426 l2vmap[i] = from + i; 427 428 /* translate virtual block numbers to physical block numbers */ 429 error = nilfs_nvtop(node, blks, l2vmap, v2pmap); 430 if (error) 431 goto out; 432 433 /* issue translated blocks */ 434 bp->b_resid = bp->b_bcount; 435 for (i = 0; i < blks; i++) { 436 DPRINTF(READ, ("read_filebuf : ino %"PRIu64" blk %d -> " 437 "%"PRIu64" -> %"PRIu64"\n", 438 node->ino, i, l2vmap[i], v2pmap[i])); 439 440 buf_offset = i * blocksize; 441 buf_pos = (uint8_t *) bp->b_data + buf_offset; 442 443 /* note virtual block 0 marks not mapped */ 444 if (l2vmap[i] == 0) { 445 memset(buf_pos, 0, blocksize); 446 nestiobuf_done(bp, blocksize, 0); 447 continue; 448 } 449 450 /* nest iobuf */ 451 nbp = getiobuf(NULL, true); 452 nestiobuf_setup(bp, nbp, buf_offset, blocksize); 453 KASSERT(nbp->b_vp == node->vnode); 454 /* nbp is B_ASYNC */ 455 456 nbp->b_lblkno = i; 457 nbp->b_blkno = v2pmap[i] * blk2dev; /* in DEV_BSIZE */ 458 nbp->b_rawblkno = nbp->b_blkno; 459 460 VOP_STRATEGY(nilfsdev->devvp, nbp); 461 } 462 463 if ((bp->b_flags & B_ASYNC) == 0) 464 biowait(bp); 465 466 out: 467 free(l2vmap, M_TEMP); 468 free(v2pmap, M_TEMP); 469 if (error) { 470 bp->b_error = EIO; 471 biodone(bp); 472 } 473 } 474 475 476 static void 477 nilfs_write_filebuf(struct nilfs_node *node, struct buf *bp) 478 { 479 /* TODO pass on to segment collector */ 480 panic("nilfs_strategy writing called\n"); 481 } 482 483 484 int 485 nilfs_vfsstrategy(void *v) 486 { 487 struct vop_strategy_args /* { 488 struct vnode *a_vp; 489 struct buf *a_bp; 490 } */ *ap = v; 491 struct vnode *vp = ap->a_vp; 492 struct buf *bp = ap->a_bp; 493 struct nilfs_node *node = VTOI(vp); 494 495 DPRINTF(STRATEGY, ("nilfs_strategy called\n")); 496 497 /* check if we ought to be here */ 498 if (vp->v_type == VBLK || vp->v_type == VCHR) 499 panic("nilfs_strategy: spec"); 500 501 /* translate if needed and pass on */ 502 if (bp->b_flags & B_READ) { 503 nilfs_read_filebuf(node, bp); 504 return bp->b_error; 505 } 506 507 /* send to segment collector */ 508 nilfs_write_filebuf(node, bp); 509 return bp->b_error; 510 } 511 512 /* --------------------------------------------------------------------- */ 513 514 int 515 nilfs_readdir(void *v) 516 { 517 struct vop_readdir_args /* { 518 struct vnode *a_vp; 519 struct uio *a_uio; 520 kauth_cred_t a_cred; 521 int *a_eofflag; 522 off_t **a_cookies; 523 int *a_ncookies; 524 } */ *ap = v; 525 struct uio *uio = ap->a_uio; 526 struct vnode *vp = ap->a_vp; 527 struct nilfs_node *node = VTOI(vp); 528 struct nilfs_dir_entry *ndirent; 529 struct dirent dirent; 530 struct buf *bp; 531 uint64_t file_size, diroffset, transoffset, blkoff; 532 uint64_t blocknr; 533 uint32_t blocksize = node->nilfsdev->blocksize; 534 uint8_t *pos, name_len; 535 int error; 536 537 DPRINTF(READDIR, ("nilfs_readdir called\n")); 538 539 if (vp->v_type != VDIR) 540 return ENOTDIR; 541 542 file_size = nilfs_rw64(node->inode.i_size); 543 544 /* we are called just as long as we keep on pushing data in */ 545 error = 0; 546 if ((uio->uio_offset < file_size) && 547 (uio->uio_resid >= sizeof(struct dirent))) { 548 diroffset = uio->uio_offset; 549 transoffset = diroffset; 550 551 blocknr = diroffset / blocksize; 552 blkoff = diroffset % blocksize; 553 error = nilfs_bread(node, blocknr, NOCRED, 0, &bp); 554 if (error) 555 return EIO; 556 while (diroffset < file_size) { 557 DPRINTF(READDIR, ("readdir : offset = %"PRIu64"\n", 558 diroffset)); 559 if (blkoff >= blocksize) { 560 blkoff = 0; blocknr++; 561 brelse(bp, BC_AGE); 562 error = nilfs_bread(node, blocknr, NOCRED, 0, 563 &bp); 564 if (error) 565 return EIO; 566 } 567 568 /* read in one dirent */ 569 pos = (uint8_t *) bp->b_data + blkoff; 570 ndirent = (struct nilfs_dir_entry *) pos; 571 572 name_len = ndirent->name_len; 573 memset(&dirent, 0, sizeof(struct dirent)); 574 dirent.d_fileno = nilfs_rw64(ndirent->inode); 575 dirent.d_type = ndirent->file_type; /* 1:1 ? */ 576 dirent.d_namlen = name_len; 577 strncpy(dirent.d_name, ndirent->name, name_len); 578 dirent.d_reclen = _DIRENT_SIZE(&dirent); 579 DPRINTF(READDIR, ("copying `%*.*s`\n", name_len, 580 name_len, dirent.d_name)); 581 582 /* 583 * If there isn't enough space in the uio to return a 584 * whole dirent, break off read 585 */ 586 if (uio->uio_resid < _DIRENT_SIZE(&dirent)) 587 break; 588 589 /* transfer */ 590 if (name_len) 591 uiomove(&dirent, _DIRENT_SIZE(&dirent), uio); 592 593 /* advance */ 594 diroffset += nilfs_rw16(ndirent->rec_len); 595 blkoff += nilfs_rw16(ndirent->rec_len); 596 597 /* remember the last entry we transfered */ 598 transoffset = diroffset; 599 } 600 brelse(bp, BC_AGE); 601 602 /* pass on last transfered offset */ 603 uio->uio_offset = transoffset; 604 } 605 606 if (ap->a_eofflag) 607 *ap->a_eofflag = (uio->uio_offset >= file_size); 608 609 return error; 610 } 611 612 /* --------------------------------------------------------------------- */ 613 614 int 615 nilfs_lookup(void *v) 616 { 617 struct vop_lookup_args /* { 618 struct vnode *a_dvp; 619 struct vnode **a_vpp; 620 struct componentname *a_cnp; 621 } */ *ap = v; 622 struct vnode *dvp = ap->a_dvp; 623 struct vnode **vpp = ap->a_vpp; 624 struct componentname *cnp = ap->a_cnp; 625 struct nilfs_node *dir_node, *res_node; 626 struct nilfs_mount *ump; 627 uint64_t ino; 628 const char *name; 629 int namelen, nameiop, islastcn, mounted_ro; 630 int vnodetp; 631 int error, found; 632 633 dir_node = VTOI(dvp); 634 ump = dir_node->ump; 635 *vpp = NULL; 636 637 DPRINTF(LOOKUP, ("nilfs_lookup called\n")); 638 639 /* simplify/clarification flags */ 640 nameiop = cnp->cn_nameiop; 641 islastcn = cnp->cn_flags & ISLASTCN; 642 mounted_ro = dvp->v_mount->mnt_flag & MNT_RDONLY; 643 644 /* check exec/dirread permissions first */ 645 error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred); 646 if (error) 647 return error; 648 649 DPRINTF(LOOKUP, ("\taccess ok\n")); 650 651 /* 652 * If requesting a modify on the last path element on a read-only 653 * filingsystem, reject lookup; XXX why is this repeated in every FS ? 654 */ 655 if (islastcn && mounted_ro && (nameiop == DELETE || nameiop == RENAME)) 656 return EROFS; 657 658 DPRINTF(LOOKUP, ("\tlooking up cnp->cn_nameptr '%s'\n", 659 cnp->cn_nameptr)); 660 /* look in the nami cache; returns 0 on success!! */ 661 error = cache_lookup(dvp, vpp, cnp); 662 if (error >= 0) 663 return error; 664 665 DPRINTF(LOOKUP, ("\tNOT found in cache\n")); 666 667 /* 668 * Obviously, the file is not (anymore) in the namecache, we have to 669 * search for it. There are three basic cases: '.', '..' and others. 670 * 671 * Following the guidelines of VOP_LOOKUP manpage and tmpfs. 672 */ 673 error = 0; 674 if ((cnp->cn_namelen == 1) && (cnp->cn_nameptr[0] == '.')) { 675 DPRINTF(LOOKUP, ("\tlookup '.'\n")); 676 /* special case 1 '.' */ 677 vref(dvp); 678 *vpp = dvp; 679 /* done */ 680 } else if (cnp->cn_flags & ISDOTDOT) { 681 /* special case 2 '..' */ 682 DPRINTF(LOOKUP, ("\tlookup '..'\n")); 683 684 /* get our node */ 685 name = ".."; 686 namelen = 2; 687 error = nilfs_lookup_name_in_dir(dvp, name, namelen, 688 &ino, &found); 689 if (error) 690 goto out; 691 if (!found) 692 error = ENOENT; 693 694 /* first unlock parent */ 695 VOP_UNLOCK(dvp); 696 697 if (error == 0) { 698 DPRINTF(LOOKUP, ("\tfound '..'\n")); 699 /* try to create/reuse the node */ 700 error = nilfs_get_node(ump, ino, &res_node); 701 702 if (!error) { 703 DPRINTF(LOOKUP, 704 ("\tnode retrieved/created OK\n")); 705 *vpp = res_node->vnode; 706 } 707 } 708 709 /* try to relock parent */ 710 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 711 } else { 712 DPRINTF(LOOKUP, ("\tlookup file\n")); 713 /* all other files */ 714 /* lookup filename in the directory returning its inode */ 715 name = cnp->cn_nameptr; 716 namelen = cnp->cn_namelen; 717 error = nilfs_lookup_name_in_dir(dvp, name, namelen, 718 &ino, &found); 719 if (error) 720 goto out; 721 if (!found) { 722 DPRINTF(LOOKUP, ("\tNOT found\n")); 723 /* 724 * UGH, didn't find name. If we're creating or 725 * renaming on the last name this is OK and we ought 726 * to return EJUSTRETURN if its allowed to be created. 727 */ 728 error = ENOENT; 729 if (islastcn && 730 (nameiop == CREATE || nameiop == RENAME)) 731 error = 0; 732 if (!error) { 733 error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred); 734 if (!error) { 735 error = EJUSTRETURN; 736 } 737 } 738 /* done */ 739 } else { 740 /* try to create/reuse the node */ 741 error = nilfs_get_node(ump, ino, &res_node); 742 if (!error) { 743 /* 744 * If we are not at the last path component 745 * and found a non-directory or non-link entry 746 * (which may itself be pointing to a 747 * directory), raise an error. 748 */ 749 vnodetp = res_node->vnode->v_type; 750 if ((vnodetp != VDIR) && (vnodetp != VLNK)) { 751 if (!islastcn) 752 error = ENOTDIR; 753 } 754 755 } 756 if (!error) { 757 *vpp = res_node->vnode; 758 } 759 } 760 } 761 762 out: 763 /* 764 * Store result in the cache if requested. If we are creating a file, 765 * the file might not be found and thus putting it into the namecache 766 * might be seen as negative caching. 767 */ 768 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE) 769 cache_enter(dvp, *vpp, cnp); 770 771 DPRINTFIF(LOOKUP, error, ("nilfs_lookup returing error %d\n", error)); 772 773 return error; 774 } 775 776 /* --------------------------------------------------------------------- */ 777 778 static void 779 nilfs_ctime_to_timespec(struct timespec *ts, uint64_t ctime) 780 { 781 ts->tv_sec = ctime; 782 ts->tv_nsec = 0; 783 } 784 785 786 int 787 nilfs_getattr(void *v) 788 { 789 struct vop_getattr_args /* { 790 struct vnode *a_vp; 791 struct vattr *a_vap; 792 kauth_cred_t a_cred; 793 struct lwp *a_l; 794 } */ *ap = v; 795 struct vnode *vp = ap->a_vp; 796 struct vattr *vap = ap->a_vap; 797 struct nilfs_node *node = VTOI(vp); 798 struct nilfs_inode *inode = &node->inode; 799 800 DPRINTF(VFSCALL, ("nilfs_getattr called\n")); 801 802 /* basic info */ 803 vattr_null(vap); 804 vap->va_type = vp->v_type; 805 vap->va_mode = nilfs_rw16(inode->i_mode); /* XXX same? */ 806 vap->va_nlink = nilfs_rw16(inode->i_links_count); 807 vap->va_uid = nilfs_rw32(inode->i_uid); 808 vap->va_gid = nilfs_rw32(inode->i_gid); 809 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0]; 810 vap->va_fileid = node->ino; 811 vap->va_size = nilfs_rw64(inode->i_size); 812 vap->va_blocksize = node->nilfsdev->blocksize; 813 814 /* times */ 815 nilfs_ctime_to_timespec(&vap->va_atime, nilfs_rw64(inode->i_mtime)); 816 nilfs_ctime_to_timespec(&vap->va_mtime, nilfs_rw64(inode->i_mtime)); 817 nilfs_ctime_to_timespec(&vap->va_ctime, nilfs_rw64(inode->i_ctime)); 818 nilfs_ctime_to_timespec(&vap->va_birthtime, nilfs_rw64(inode->i_ctime)); 819 820 vap->va_gen = nilfs_rw32(inode->i_generation); 821 vap->va_flags = 0; /* vattr flags */ 822 vap->va_bytes = nilfs_rw64(inode->i_blocks) * vap->va_blocksize; 823 vap->va_filerev = vap->va_gen; /* XXX file revision? same as gen? */ 824 vap->va_vaflags = 0; /* XXX chflags flags */ 825 826 return 0; 827 } 828 829 /* --------------------------------------------------------------------- */ 830 831 #if 0 832 static int 833 nilfs_chown(struct vnode *vp, uid_t new_uid, gid_t new_gid, 834 kauth_cred_t cred) 835 { 836 return EINVAL; 837 } 838 839 840 static int 841 nilfs_chmod(struct vnode *vp, mode_t mode, kauth_cred_t cred) 842 { 843 844 return EINVAL; 845 } 846 847 848 /* exported */ 849 int 850 nilfs_chsize(struct vnode *vp, u_quad_t newsize, kauth_cred_t cred) 851 { 852 return EINVAL; 853 } 854 855 856 static int 857 nilfs_chflags(struct vnode *vp, mode_t mode, kauth_cred_t cred) 858 { 859 return EINVAL; 860 } 861 862 863 static int 864 nilfs_chtimes(struct vnode *vp, 865 struct timespec *atime, struct timespec *mtime, 866 struct timespec *birthtime, int setattrflags, 867 kauth_cred_t cred) 868 { 869 return EINVAL; 870 } 871 #endif 872 873 874 int 875 nilfs_setattr(void *v) 876 { 877 struct vop_setattr_args /* { 878 struct vnode *a_vp; 879 struct vattr *a_vap; 880 kauth_cred_t a_cred; 881 struct lwp *a_l; 882 } */ *ap = v; 883 struct vnode *vp = ap->a_vp; 884 885 vp = vp; 886 DPRINTF(VFSCALL, ("nilfs_setattr called\n")); 887 return EINVAL; 888 } 889 890 /* --------------------------------------------------------------------- */ 891 892 /* 893 * Return POSIX pathconf information for NILFS file systems. 894 */ 895 int 896 nilfs_pathconf(void *v) 897 { 898 struct vop_pathconf_args /* { 899 struct vnode *a_vp; 900 int a_name; 901 register_t *a_retval; 902 } */ *ap = v; 903 uint32_t bits; 904 905 DPRINTF(VFSCALL, ("nilfs_pathconf called\n")); 906 907 switch (ap->a_name) { 908 case _PC_LINK_MAX: 909 *ap->a_retval = (1<<16)-1; /* 16 bits */ 910 return 0; 911 case _PC_NAME_MAX: 912 *ap->a_retval = NAME_MAX; 913 return 0; 914 case _PC_PATH_MAX: 915 *ap->a_retval = PATH_MAX; 916 return 0; 917 case _PC_PIPE_BUF: 918 *ap->a_retval = PIPE_BUF; 919 return 0; 920 case _PC_CHOWN_RESTRICTED: 921 *ap->a_retval = 1; 922 return 0; 923 case _PC_NO_TRUNC: 924 *ap->a_retval = 1; 925 return 0; 926 case _PC_SYNC_IO: 927 *ap->a_retval = 0; /* synchronised is off for performance */ 928 return 0; 929 case _PC_FILESIZEBITS: 930 /* 64 bit file offsets -> 2+floor(2log(2^64-1)) = 2 + 63 = 65 */ 931 bits = 64; /* XXX ought to deliver 65 */ 932 #if 0 933 if (nilfs_node) 934 bits = 64 * vp->v_mount->mnt_dev_bshift; 935 #endif 936 *ap->a_retval = bits; 937 return 0; 938 } 939 940 return EINVAL; 941 } 942 943 944 /* --------------------------------------------------------------------- */ 945 946 int 947 nilfs_open(void *v) 948 { 949 struct vop_open_args /* { 950 struct vnode *a_vp; 951 int a_mode; 952 kauth_cred_t a_cred; 953 struct proc *a_p; 954 } */ *ap = v; 955 int flags; 956 957 DPRINTF(VFSCALL, ("nilfs_open called\n")); 958 959 /* 960 * Files marked append-only must be opened for appending. 961 */ 962 flags = 0; 963 if ((flags & APPEND) && (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) 964 return (EPERM); 965 966 return 0; 967 } 968 969 970 /* --------------------------------------------------------------------- */ 971 972 int 973 nilfs_close(void *v) 974 { 975 struct vop_close_args /* { 976 struct vnode *a_vp; 977 int a_fflag; 978 kauth_cred_t a_cred; 979 struct proc *a_p; 980 } */ *ap = v; 981 struct vnode *vp = ap->a_vp; 982 struct nilfs_node *nilfs_node = VTOI(vp); 983 984 DPRINTF(VFSCALL, ("nilfs_close called\n")); 985 nilfs_node = nilfs_node; /* shut up gcc */ 986 987 mutex_enter(&vp->v_interlock); 988 if (vp->v_usecount > 1) 989 nilfs_itimes(nilfs_node, NULL, NULL, NULL); 990 mutex_exit(&vp->v_interlock); 991 992 return 0; 993 } 994 995 996 /* --------------------------------------------------------------------- */ 997 998 static int 999 nilfs_check_possible(struct vnode *vp, struct vattr *vap, mode_t mode) 1000 { 1001 int flags; 1002 1003 /* check if we are allowed to write */ 1004 switch (vap->va_type) { 1005 case VDIR: 1006 case VLNK: 1007 case VREG: 1008 /* 1009 * normal nodes: check if we're on a read-only mounted 1010 * filingsystem and bomb out if we're trying to write. 1011 */ 1012 if ((mode & VWRITE) && (vp->v_mount->mnt_flag & MNT_RDONLY)) 1013 return EROFS; 1014 break; 1015 case VBLK: 1016 case VCHR: 1017 case VSOCK: 1018 case VFIFO: 1019 /* 1020 * special nodes: even on read-only mounted filingsystems 1021 * these are allowed to be written to if permissions allow. 1022 */ 1023 break; 1024 default: 1025 /* no idea what this is */ 1026 return EINVAL; 1027 } 1028 1029 /* noone may write immutable files */ 1030 /* TODO: get chflags(2) flags */ 1031 flags = 0; 1032 if ((mode & VWRITE) && (flags & IMMUTABLE)) 1033 return EPERM; 1034 1035 return 0; 1036 } 1037 1038 static int 1039 nilfs_check_permitted(struct vnode *vp, struct vattr *vap, mode_t mode, 1040 kauth_cred_t cred) 1041 { 1042 1043 /* ask the generic genfs_can_access to advice on security */ 1044 return genfs_can_access(vp->v_type, 1045 vap->va_mode, vap->va_uid, vap->va_gid, 1046 mode, cred); 1047 } 1048 1049 int 1050 nilfs_access(void *v) 1051 { 1052 struct vop_access_args /* { 1053 struct vnode *a_vp; 1054 int a_mode; 1055 kauth_cred_t a_cred; 1056 struct proc *a_p; 1057 } */ *ap = v; 1058 struct vnode *vp = ap->a_vp; 1059 mode_t mode = ap->a_mode; 1060 kauth_cred_t cred = ap->a_cred; 1061 /* struct nilfs_node *nilfs_node = VTOI(vp); */ 1062 struct vattr vap; 1063 int error; 1064 1065 DPRINTF(VFSCALL, ("nilfs_access called\n")); 1066 1067 error = VOP_GETATTR(vp, &vap, NULL); 1068 if (error) 1069 return error; 1070 1071 error = nilfs_check_possible(vp, &vap, mode); 1072 if (error) 1073 return error; 1074 1075 error = nilfs_check_permitted(vp, &vap, mode, cred); 1076 1077 return error; 1078 } 1079 1080 /* --------------------------------------------------------------------- */ 1081 1082 int 1083 nilfs_create(void *v) 1084 { 1085 struct vop_create_args /* { 1086 struct vnode *a_dvp; 1087 struct vnode **a_vpp; 1088 struct componentname *a_cnp; 1089 struct vattr *a_vap; 1090 } */ *ap = v; 1091 struct vnode *dvp = ap->a_dvp; 1092 struct vnode **vpp = ap->a_vpp; 1093 struct vattr *vap = ap->a_vap; 1094 struct componentname *cnp = ap->a_cnp; 1095 int error; 1096 1097 DPRINTF(VFSCALL, ("nilfs_create called\n")); 1098 error = nilfs_create_node(dvp, vpp, vap, cnp); 1099 1100 vput(dvp); 1101 return error; 1102 } 1103 1104 /* --------------------------------------------------------------------- */ 1105 1106 int 1107 nilfs_mknod(void *v) 1108 { 1109 struct vop_mknod_args /* { 1110 struct vnode *a_dvp; 1111 struct vnode **a_vpp; 1112 struct componentname *a_cnp; 1113 struct vattr *a_vap; 1114 } */ *ap = v; 1115 struct vnode *dvp = ap->a_dvp; 1116 struct vnode **vpp = ap->a_vpp; 1117 struct vattr *vap = ap->a_vap; 1118 struct componentname *cnp = ap->a_cnp; 1119 int error; 1120 1121 DPRINTF(VFSCALL, ("nilfs_mknod called\n")); 1122 error = nilfs_create_node(dvp, vpp, vap, cnp); 1123 1124 vput(dvp); 1125 return error; 1126 } 1127 1128 /* --------------------------------------------------------------------- */ 1129 1130 int 1131 nilfs_mkdir(void *v) 1132 { 1133 struct vop_mkdir_args /* { 1134 struct vnode *a_dvp; 1135 struct vnode **a_vpp; 1136 struct componentname *a_cnp; 1137 struct vattr *a_vap; 1138 } */ *ap = v; 1139 struct vnode *dvp = ap->a_dvp; 1140 struct vnode **vpp = ap->a_vpp; 1141 struct vattr *vap = ap->a_vap; 1142 struct componentname *cnp = ap->a_cnp; 1143 int error; 1144 1145 DPRINTF(VFSCALL, ("nilfs_mkdir called\n")); 1146 error = nilfs_create_node(dvp, vpp, vap, cnp); 1147 1148 vput(dvp); 1149 return error; 1150 } 1151 1152 /* --------------------------------------------------------------------- */ 1153 1154 static int 1155 nilfs_do_link(struct vnode *dvp, struct vnode *vp, struct componentname *cnp) 1156 { 1157 struct nilfs_node *nilfs_node, *dir_node; 1158 struct vattr vap; 1159 int error; 1160 1161 DPRINTF(VFSCALL, ("nilfs_link called\n")); 1162 KASSERT(dvp != vp); 1163 KASSERT(vp->v_type != VDIR); 1164 KASSERT(dvp->v_mount == vp->v_mount); 1165 1166 /* lock node */ 1167 error = vn_lock(vp, LK_EXCLUSIVE); 1168 if (error) 1169 return error; 1170 1171 /* get attributes */ 1172 dir_node = VTOI(dvp); 1173 nilfs_node = VTOI(vp); 1174 1175 error = VOP_GETATTR(vp, &vap, FSCRED); 1176 if (error) { 1177 VOP_UNLOCK(vp); 1178 return error; 1179 } 1180 1181 /* check link count overflow */ 1182 if (vap.va_nlink >= (1<<16)-1) { /* uint16_t */ 1183 VOP_UNLOCK(vp); 1184 return EMLINK; 1185 } 1186 1187 error = nilfs_dir_attach(dir_node->ump, dir_node, nilfs_node, 1188 &vap, cnp); 1189 if (error) 1190 VOP_UNLOCK(vp); 1191 return error; 1192 } 1193 1194 int 1195 nilfs_link(void *v) 1196 { 1197 struct vop_link_args /* { 1198 struct vnode *a_dvp; 1199 struct vnode *a_vp; 1200 struct componentname *a_cnp; 1201 } */ *ap = v; 1202 struct vnode *dvp = ap->a_dvp; 1203 struct vnode *vp = ap->a_vp; 1204 struct componentname *cnp = ap->a_cnp; 1205 int error; 1206 1207 error = nilfs_do_link(dvp, vp, cnp); 1208 if (error) 1209 VOP_ABORTOP(dvp, cnp); 1210 1211 VN_KNOTE(vp, NOTE_LINK); 1212 VN_KNOTE(dvp, NOTE_WRITE); 1213 vput(dvp); 1214 1215 return error; 1216 } 1217 1218 /* --------------------------------------------------------------------- */ 1219 1220 static int 1221 nilfs_do_symlink(struct nilfs_node *nilfs_node, char *target) 1222 { 1223 return EROFS; 1224 } 1225 1226 1227 int 1228 nilfs_symlink(void *v) 1229 { 1230 struct vop_symlink_args /* { 1231 struct vnode *a_dvp; 1232 struct vnode **a_vpp; 1233 struct componentname *a_cnp; 1234 struct vattr *a_vap; 1235 char *a_target; 1236 } */ *ap = v; 1237 struct vnode *dvp = ap->a_dvp; 1238 struct vnode **vpp = ap->a_vpp; 1239 struct vattr *vap = ap->a_vap; 1240 struct componentname *cnp = ap->a_cnp; 1241 struct nilfs_node *dir_node; 1242 struct nilfs_node *nilfs_node; 1243 int error; 1244 1245 DPRINTF(VFSCALL, ("nilfs_symlink called\n")); 1246 DPRINTF(VFSCALL, ("\tlinking to `%s`\n", ap->a_target)); 1247 error = nilfs_create_node(dvp, vpp, vap, cnp); 1248 KASSERT(((error == 0) && (*vpp != NULL)) || ((error && (*vpp == NULL)))); 1249 if (!error) { 1250 dir_node = VTOI(dvp); 1251 nilfs_node = VTOI(*vpp); 1252 KASSERT(nilfs_node); 1253 error = nilfs_do_symlink(nilfs_node, ap->a_target); 1254 if (error) { 1255 /* remove node */ 1256 nilfs_shrink_node(nilfs_node, 0); 1257 nilfs_dir_detach(nilfs_node->ump, dir_node, nilfs_node, cnp); 1258 } 1259 } 1260 vput(dvp); 1261 return error; 1262 } 1263 1264 /* --------------------------------------------------------------------- */ 1265 1266 int 1267 nilfs_readlink(void *v) 1268 { 1269 struct vop_readlink_args /* { 1270 struct vnode *a_vp; 1271 struct uio *a_uio; 1272 kauth_cred_t a_cred; 1273 } */ *ap = v; 1274 #if 0 1275 struct vnode *vp = ap->a_vp; 1276 struct uio *uio = ap->a_uio; 1277 kauth_cred_t cred = ap->a_cred; 1278 struct nilfs_node *nilfs_node; 1279 struct pathcomp pathcomp; 1280 struct vattr vattr; 1281 uint8_t *pathbuf, *targetbuf, *tmpname; 1282 uint8_t *pathpos, *targetpos; 1283 char *mntonname; 1284 int pathlen, targetlen, namelen, mntonnamelen, len, l_ci; 1285 int first, error; 1286 #endif 1287 ap = ap; 1288 1289 DPRINTF(VFSCALL, ("nilfs_readlink called\n")); 1290 1291 return EROFS; 1292 } 1293 1294 /* --------------------------------------------------------------------- */ 1295 1296 /* note: i tried to follow the logics of the tmpfs rename code */ 1297 int 1298 nilfs_rename(void *v) 1299 { 1300 struct vop_rename_args /* { 1301 struct vnode *a_fdvp; 1302 struct vnode *a_fvp; 1303 struct componentname *a_fcnp; 1304 struct vnode *a_tdvp; 1305 struct vnode *a_tvp; 1306 struct componentname *a_tcnp; 1307 } */ *ap = v; 1308 struct vnode *tvp = ap->a_tvp; 1309 struct vnode *tdvp = ap->a_tdvp; 1310 struct vnode *fvp = ap->a_fvp; 1311 struct vnode *fdvp = ap->a_fdvp; 1312 struct componentname *tcnp = ap->a_tcnp; 1313 struct componentname *fcnp = ap->a_fcnp; 1314 struct nilfs_node *fnode, *fdnode, *tnode, *tdnode; 1315 struct vattr fvap, tvap; 1316 int error; 1317 1318 DPRINTF(VFSCALL, ("nilfs_rename called\n")); 1319 1320 /* disallow cross-device renames */ 1321 if (fvp->v_mount != tdvp->v_mount || 1322 (tvp != NULL && fvp->v_mount != tvp->v_mount)) { 1323 error = EXDEV; 1324 goto out_unlocked; 1325 } 1326 1327 fnode = VTOI(fvp); 1328 fdnode = VTOI(fdvp); 1329 tnode = (tvp == NULL) ? NULL : VTOI(tvp); 1330 tdnode = VTOI(tdvp); 1331 1332 /* lock our source dir */ 1333 if (fdnode != tdnode) { 1334 error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY); 1335 if (error != 0) 1336 goto out_unlocked; 1337 } 1338 1339 /* get info about the node to be moved */ 1340 error = VOP_GETATTR(fvp, &fvap, FSCRED); 1341 KASSERT(error == 0); 1342 1343 /* check when to delete the old already existing entry */ 1344 if (tvp) { 1345 /* get info about the node to be moved to */ 1346 error = VOP_GETATTR(fvp, &tvap, FSCRED); 1347 KASSERT(error == 0); 1348 1349 /* if both dirs, make sure the destination is empty */ 1350 if (fvp->v_type == VDIR && tvp->v_type == VDIR) { 1351 if (tvap.va_nlink > 2) { 1352 error = ENOTEMPTY; 1353 goto out; 1354 } 1355 } 1356 /* if moving dir, make sure destination is dir too */ 1357 if (fvp->v_type == VDIR && tvp->v_type != VDIR) { 1358 error = ENOTDIR; 1359 goto out; 1360 } 1361 /* if we're moving a non-directory, make sure dest is no dir */ 1362 if (fvp->v_type != VDIR && tvp->v_type == VDIR) { 1363 error = EISDIR; 1364 goto out; 1365 } 1366 } 1367 1368 /* dont allow renaming directories acros directory for now */ 1369 if (fdnode != tdnode) { 1370 if (fvp->v_type == VDIR) { 1371 error = EINVAL; 1372 goto out; 1373 } 1374 } 1375 1376 /* remove existing entry if present */ 1377 if (tvp) 1378 nilfs_dir_detach(tdnode->ump, tdnode, tnode, tcnp); 1379 1380 /* create new directory entry for the node */ 1381 error = nilfs_dir_attach(tdnode->ump, tdnode, fnode, &fvap, tcnp); 1382 if (error) 1383 goto out; 1384 1385 /* unlink old directory entry for the node, if failing, unattach new */ 1386 error = nilfs_dir_detach(tdnode->ump, fdnode, fnode, fcnp); 1387 if (error) 1388 nilfs_dir_detach(tdnode->ump, tdnode, fnode, tcnp); 1389 1390 out: 1391 if (fdnode != tdnode) 1392 VOP_UNLOCK(fdvp); 1393 1394 out_unlocked: 1395 VOP_ABORTOP(tdvp, tcnp); 1396 if (tdvp == tvp) 1397 vrele(tdvp); 1398 else 1399 vput(tdvp); 1400 if (tvp) 1401 vput(tvp); 1402 VOP_ABORTOP(fdvp, fcnp); 1403 1404 /* release source nodes. */ 1405 vrele(fdvp); 1406 vrele(fvp); 1407 1408 return error; 1409 } 1410 1411 /* --------------------------------------------------------------------- */ 1412 1413 int 1414 nilfs_remove(void *v) 1415 { 1416 struct vop_remove_args /* { 1417 struct vnode *a_dvp; 1418 struct vnode *a_vp; 1419 struct componentname *a_cnp; 1420 } */ *ap = v; 1421 struct vnode *dvp = ap->a_dvp; 1422 struct vnode *vp = ap->a_vp; 1423 struct componentname *cnp = ap->a_cnp; 1424 struct nilfs_node *dir_node = VTOI(dvp); 1425 struct nilfs_node *nilfs_node = VTOI(vp); 1426 struct nilfs_mount *ump = dir_node->ump; 1427 int error; 1428 1429 DPRINTF(VFSCALL, ("nilfs_remove called\n")); 1430 if (vp->v_type != VDIR) { 1431 error = nilfs_dir_detach(ump, dir_node, nilfs_node, cnp); 1432 DPRINTFIF(NODE, error, ("\tgot error removing file\n")); 1433 } else { 1434 DPRINTF(NODE, ("\tis a directory: perm. denied\n")); 1435 error = EPERM; 1436 } 1437 1438 if (error == 0) { 1439 VN_KNOTE(vp, NOTE_DELETE); 1440 VN_KNOTE(dvp, NOTE_WRITE); 1441 } 1442 1443 if (dvp == vp) 1444 vrele(vp); 1445 else 1446 vput(vp); 1447 vput(dvp); 1448 1449 return error; 1450 } 1451 1452 /* --------------------------------------------------------------------- */ 1453 1454 int 1455 nilfs_rmdir(void *v) 1456 { 1457 struct vop_rmdir_args /* { 1458 struct vnode *a_dvp; 1459 struct vnode *a_vp; 1460 struct componentname *a_cnp; 1461 } */ *ap = v; 1462 struct vnode *vp = ap->a_vp; 1463 struct vnode *dvp = ap->a_dvp; 1464 struct componentname *cnp = ap->a_cnp; 1465 struct nilfs_node *dir_node = VTOI(dvp); 1466 struct nilfs_node *nilfs_node = VTOI(vp); 1467 struct nilfs_mount *ump = dir_node->ump; 1468 int refcnt, error; 1469 1470 DPRINTF(NOTIMPL, ("nilfs_rmdir called\n")); 1471 1472 /* don't allow '.' to be deleted */ 1473 if (dir_node == nilfs_node) { 1474 vrele(dvp); 1475 vput(vp); 1476 return EINVAL; 1477 } 1478 1479 /* check to see if the directory is empty */ 1480 error = 0; 1481 refcnt = 2; /* XXX */ 1482 if (refcnt > 1) { 1483 /* NOT empty */ 1484 vput(dvp); 1485 vput(vp); 1486 return ENOTEMPTY; 1487 } 1488 1489 /* detach the node from the directory */ 1490 error = nilfs_dir_detach(ump, dir_node, nilfs_node, cnp); 1491 if (error == 0) { 1492 cache_purge(vp); 1493 // cache_purge(dvp); /* XXX from msdosfs, why? */ 1494 VN_KNOTE(vp, NOTE_DELETE); 1495 } 1496 DPRINTFIF(NODE, error, ("\tgot error removing file\n")); 1497 1498 /* unput the nodes and exit */ 1499 vput(dvp); 1500 vput(vp); 1501 1502 return error; 1503 } 1504 1505 /* --------------------------------------------------------------------- */ 1506 1507 int 1508 nilfs_fsync(void *v) 1509 { 1510 struct vop_fsync_args /* { 1511 struct vnode *a_vp; 1512 kauth_cred_t a_cred; 1513 int a_flags; 1514 off_t offlo; 1515 off_t offhi; 1516 struct proc *a_p; 1517 } */ *ap = v; 1518 struct vnode *vp = ap->a_vp; 1519 // struct nilfs_node *nilfs_node = VTOI(vp); 1520 // int error, flags, wait; 1521 1522 DPRINTF(STRATEGY, ("nilfs_fsync called : %s, %s\n", 1523 (ap->a_flags & FSYNC_WAIT) ? "wait":"no wait", 1524 (ap->a_flags & FSYNC_DATAONLY) ? "data_only":"complete")); 1525 1526 vp = vp; 1527 return 0; 1528 } 1529 1530 /* --------------------------------------------------------------------- */ 1531 1532 int 1533 nilfs_advlock(void *v) 1534 { 1535 struct vop_advlock_args /* { 1536 struct vnode *a_vp; 1537 void *a_id; 1538 int a_op; 1539 struct flock *a_fl; 1540 int a_flags; 1541 } */ *ap = v; 1542 struct vnode *vp = ap->a_vp; 1543 struct nilfs_node *nilfs_node = VTOI(vp); 1544 uint64_t file_size; 1545 1546 DPRINTF(LOCKING, ("nilfs_advlock called\n")); 1547 1548 assert(nilfs_node); 1549 file_size = nilfs_rw64(nilfs_node->inode.i_size); 1550 1551 return lf_advlock(ap, &nilfs_node->lockf, file_size); 1552 } 1553 1554 /* --------------------------------------------------------------------- */ 1555 1556 1557 /* Global vfs vnode data structures for nilfss */ 1558 int (**nilfs_vnodeop_p) __P((void *)); 1559 1560 const struct vnodeopv_entry_desc nilfs_vnodeop_entries[] = { 1561 { &vop_default_desc, vn_default_error }, 1562 { &vop_lookup_desc, nilfs_lookup }, /* lookup */ 1563 { &vop_create_desc, nilfs_create }, /* create */ 1564 { &vop_mknod_desc, nilfs_mknod }, /* mknod */ /* TODO */ 1565 { &vop_open_desc, nilfs_open }, /* open */ 1566 { &vop_close_desc, nilfs_close }, /* close */ 1567 { &vop_access_desc, nilfs_access }, /* access */ 1568 { &vop_getattr_desc, nilfs_getattr }, /* getattr */ 1569 { &vop_setattr_desc, nilfs_setattr }, /* setattr */ /* TODO chflags */ 1570 { &vop_read_desc, nilfs_read }, /* read */ 1571 { &vop_write_desc, nilfs_write }, /* write */ /* WRITE */ 1572 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */ /* TODO? */ 1573 { &vop_ioctl_desc, genfs_enoioctl }, /* ioctl */ /* TODO? */ 1574 { &vop_poll_desc, genfs_poll }, /* poll */ /* TODO/OK? */ 1575 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */ /* ? */ 1576 { &vop_revoke_desc, genfs_revoke }, /* revoke */ /* TODO? */ 1577 { &vop_mmap_desc, genfs_mmap }, /* mmap */ /* OK? */ 1578 { &vop_fsync_desc, nilfs_fsync }, /* fsync */ 1579 { &vop_seek_desc, genfs_seek }, /* seek */ 1580 { &vop_remove_desc, nilfs_remove }, /* remove */ 1581 { &vop_link_desc, nilfs_link }, /* link */ /* TODO */ 1582 { &vop_rename_desc, nilfs_rename }, /* rename */ /* TODO */ 1583 { &vop_mkdir_desc, nilfs_mkdir }, /* mkdir */ 1584 { &vop_rmdir_desc, nilfs_rmdir }, /* rmdir */ 1585 { &vop_symlink_desc, nilfs_symlink }, /* symlink */ /* TODO */ 1586 { &vop_readdir_desc, nilfs_readdir }, /* readdir */ 1587 { &vop_readlink_desc, nilfs_readlink }, /* readlink */ /* TEST ME */ 1588 { &vop_abortop_desc, genfs_abortop }, /* abortop */ /* TODO/OK? */ 1589 { &vop_inactive_desc, nilfs_inactive }, /* inactive */ 1590 { &vop_reclaim_desc, nilfs_reclaim }, /* reclaim */ 1591 { &vop_lock_desc, genfs_lock }, /* lock */ 1592 { &vop_unlock_desc, genfs_unlock }, /* unlock */ 1593 { &vop_bmap_desc, nilfs_trivial_bmap }, /* bmap */ /* 1:1 bmap */ 1594 { &vop_strategy_desc, nilfs_vfsstrategy },/* strategy */ 1595 /* { &vop_print_desc, nilfs_print }, */ /* print */ 1596 { &vop_islocked_desc, genfs_islocked }, /* islocked */ 1597 { &vop_pathconf_desc, nilfs_pathconf }, /* pathconf */ 1598 { &vop_advlock_desc, nilfs_advlock }, /* advlock */ /* TEST ME */ 1599 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ /* ->strategy */ 1600 { &vop_getpages_desc, genfs_getpages }, /* getpages */ 1601 { &vop_putpages_desc, genfs_putpages }, /* putpages */ 1602 { NULL, NULL } 1603 }; 1604 1605 1606 const struct vnodeopv_desc nilfs_vnodeop_opv_desc = { 1607 &nilfs_vnodeop_p, nilfs_vnodeop_entries 1608 }; 1609 1610