1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * This file contains vfs inode ops for the 9P2000 protocol. 4 * 5 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com> 6 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov> 7 */ 8 9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 11 #include <linux/module.h> 12 #include <linux/errno.h> 13 #include <linux/fs.h> 14 #include <linux/file.h> 15 #include <linux/pagemap.h> 16 #include <linux/stat.h> 17 #include <linux/string.h> 18 #include <linux/namei.h> 19 #include <linux/sched.h> 20 #include <linux/slab.h> 21 #include <linux/xattr.h> 22 #include <linux/posix_acl.h> 23 #include <net/9p/9p.h> 24 #include <net/9p/client.h> 25 26 #include "v9fs.h" 27 #include "v9fs_vfs.h" 28 #include "fid.h" 29 #include "cache.h" 30 #include "xattr.h" 31 #include "acl.h" 32 33 static const struct inode_operations v9fs_dir_inode_operations; 34 static const struct inode_operations v9fs_dir_inode_operations_dotu; 35 static const struct inode_operations v9fs_file_inode_operations; 36 static const struct inode_operations v9fs_symlink_inode_operations; 37 38 /** 39 * unixmode2p9mode - convert unix mode bits to plan 9 40 * @v9ses: v9fs session information 41 * @mode: mode to convert 42 * 43 */ 44 45 static u32 unixmode2p9mode(struct v9fs_session_info *v9ses, umode_t mode) 46 { 47 int res; 48 49 res = mode & 0777; 50 if (S_ISDIR(mode)) 51 res |= P9_DMDIR; 52 if (v9fs_proto_dotu(v9ses)) { 53 if (v9ses->nodev == 0) { 54 if (S_ISSOCK(mode)) 55 res |= P9_DMSOCKET; 56 if (S_ISFIFO(mode)) 57 res |= P9_DMNAMEDPIPE; 58 if (S_ISBLK(mode)) 59 res |= P9_DMDEVICE; 60 if (S_ISCHR(mode)) 61 res |= P9_DMDEVICE; 62 } 63 64 if ((mode & S_ISUID) == S_ISUID) 65 res |= P9_DMSETUID; 66 if ((mode & S_ISGID) == S_ISGID) 67 res |= P9_DMSETGID; 68 if ((mode & S_ISVTX) == S_ISVTX) 69 res |= P9_DMSETVTX; 70 } 71 return res; 72 } 73 74 /** 75 * p9mode2perm- convert plan9 mode bits to unix permission bits 76 * @v9ses: v9fs session information 77 * @stat: p9_wstat from which mode need to be derived 78 * 79 */ 80 static int p9mode2perm(struct v9fs_session_info *v9ses, 81 struct p9_wstat *stat) 82 { 83 int res; 84 int mode = stat->mode; 85 86 res = mode & S_IALLUGO; 87 if (v9fs_proto_dotu(v9ses)) { 88 if ((mode & P9_DMSETUID) == P9_DMSETUID) 89 res |= S_ISUID; 90 91 if ((mode & P9_DMSETGID) == P9_DMSETGID) 92 res |= S_ISGID; 93 94 if ((mode & P9_DMSETVTX) == P9_DMSETVTX) 95 res |= S_ISVTX; 96 } 97 return res; 98 } 99 100 /** 101 * p9mode2unixmode- convert plan9 mode bits to unix mode bits 102 * @v9ses: v9fs session information 103 * @stat: p9_wstat from which mode need to be derived 104 * @rdev: major number, minor number in case of device files. 105 * 106 */ 107 static umode_t p9mode2unixmode(struct v9fs_session_info *v9ses, 108 struct p9_wstat *stat, dev_t *rdev) 109 { 110 int res, r; 111 u32 mode = stat->mode; 112 113 *rdev = 0; 114 res = p9mode2perm(v9ses, stat); 115 116 if ((mode & P9_DMDIR) == P9_DMDIR) 117 res |= S_IFDIR; 118 else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses))) 119 res |= S_IFLNK; 120 else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses)) 121 && (v9ses->nodev == 0)) 122 res |= S_IFSOCK; 123 else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses)) 124 && (v9ses->nodev == 0)) 125 res |= S_IFIFO; 126 else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses)) 127 && (v9ses->nodev == 0)) { 128 char type = 0; 129 int major = -1, minor = -1; 130 131 r = sscanf(stat->extension, "%c %i %i", &type, &major, &minor); 132 if (r != 3) { 133 p9_debug(P9_DEBUG_ERROR, 134 "invalid device string, umode will be bogus: %s\n", 135 stat->extension); 136 return res; 137 } 138 switch (type) { 139 case 'c': 140 res |= S_IFCHR; 141 break; 142 case 'b': 143 res |= S_IFBLK; 144 break; 145 default: 146 p9_debug(P9_DEBUG_ERROR, "Unknown special type %c %s\n", 147 type, stat->extension); 148 } 149 *rdev = MKDEV(major, minor); 150 } else 151 res |= S_IFREG; 152 153 return res; 154 } 155 156 /** 157 * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits 158 * @uflags: flags to convert 159 * @extended: if .u extensions are active 160 */ 161 162 int v9fs_uflags2omode(int uflags, int extended) 163 { 164 int ret; 165 166 switch (uflags&3) { 167 default: 168 case O_RDONLY: 169 ret = P9_OREAD; 170 break; 171 172 case O_WRONLY: 173 ret = P9_OWRITE; 174 break; 175 176 case O_RDWR: 177 ret = P9_ORDWR; 178 break; 179 } 180 181 if (extended) { 182 if (uflags & O_EXCL) 183 ret |= P9_OEXCL; 184 185 if (uflags & O_APPEND) 186 ret |= P9_OAPPEND; 187 } 188 189 return ret; 190 } 191 192 /** 193 * v9fs_blank_wstat - helper function to setup a 9P stat structure 194 * @wstat: structure to initialize 195 * 196 */ 197 198 void 199 v9fs_blank_wstat(struct p9_wstat *wstat) 200 { 201 wstat->type = ~0; 202 wstat->dev = ~0; 203 wstat->qid.type = ~0; 204 wstat->qid.version = ~0; 205 *((long long *)&wstat->qid.path) = ~0; 206 wstat->mode = ~0; 207 wstat->atime = ~0; 208 wstat->mtime = ~0; 209 wstat->length = ~0; 210 wstat->name = NULL; 211 wstat->uid = NULL; 212 wstat->gid = NULL; 213 wstat->muid = NULL; 214 wstat->n_uid = INVALID_UID; 215 wstat->n_gid = INVALID_GID; 216 wstat->n_muid = INVALID_UID; 217 wstat->extension = NULL; 218 } 219 220 /** 221 * v9fs_alloc_inode - helper function to allocate an inode 222 * @sb: The superblock to allocate the inode from 223 */ 224 struct inode *v9fs_alloc_inode(struct super_block *sb) 225 { 226 struct v9fs_inode *v9inode; 227 228 v9inode = alloc_inode_sb(sb, v9fs_inode_cache, GFP_KERNEL); 229 if (!v9inode) 230 return NULL; 231 v9inode->cache_validity = 0; 232 mutex_init(&v9inode->v_mutex); 233 return &v9inode->netfs.inode; 234 } 235 236 /** 237 * v9fs_free_inode - destroy an inode 238 * @inode: The inode to be freed 239 */ 240 241 void v9fs_free_inode(struct inode *inode) 242 { 243 kmem_cache_free(v9fs_inode_cache, V9FS_I(inode)); 244 } 245 246 /* 247 * Set parameters for the netfs library 248 */ 249 void v9fs_set_netfs_context(struct inode *inode) 250 { 251 struct v9fs_inode *v9inode = V9FS_I(inode); 252 netfs_inode_init(&v9inode->netfs, &v9fs_req_ops, true); 253 } 254 255 int v9fs_init_inode(struct v9fs_session_info *v9ses, 256 struct inode *inode, struct p9_qid *qid, umode_t mode, dev_t rdev) 257 { 258 int err = 0; 259 struct v9fs_inode *v9inode = V9FS_I(inode); 260 261 memcpy(&v9inode->qid, qid, sizeof(struct p9_qid)); 262 263 inode_init_owner(&nop_mnt_idmap, inode, NULL, mode); 264 inode->i_blocks = 0; 265 inode->i_rdev = rdev; 266 simple_inode_init_ts(inode); 267 inode->i_mapping->a_ops = &v9fs_addr_operations; 268 inode->i_private = NULL; 269 270 switch (mode & S_IFMT) { 271 case S_IFIFO: 272 case S_IFBLK: 273 case S_IFCHR: 274 case S_IFSOCK: 275 if (v9fs_proto_dotl(v9ses)) { 276 inode->i_op = &v9fs_file_inode_operations_dotl; 277 } else if (v9fs_proto_dotu(v9ses)) { 278 inode->i_op = &v9fs_file_inode_operations; 279 } else { 280 p9_debug(P9_DEBUG_ERROR, 281 "special files without extended mode\n"); 282 err = -EINVAL; 283 goto error; 284 } 285 init_special_inode(inode, inode->i_mode, inode->i_rdev); 286 break; 287 case S_IFREG: 288 if (v9fs_proto_dotl(v9ses)) { 289 inode->i_op = &v9fs_file_inode_operations_dotl; 290 inode->i_fop = &v9fs_file_operations_dotl; 291 } else { 292 inode->i_op = &v9fs_file_inode_operations; 293 inode->i_fop = &v9fs_file_operations; 294 } 295 296 break; 297 case S_IFLNK: 298 if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) { 299 p9_debug(P9_DEBUG_ERROR, 300 "extended modes used with legacy protocol\n"); 301 err = -EINVAL; 302 goto error; 303 } 304 305 if (v9fs_proto_dotl(v9ses)) 306 inode->i_op = &v9fs_symlink_inode_operations_dotl; 307 else 308 inode->i_op = &v9fs_symlink_inode_operations; 309 310 break; 311 case S_IFDIR: 312 inc_nlink(inode); 313 if (v9fs_proto_dotl(v9ses)) 314 inode->i_op = &v9fs_dir_inode_operations_dotl; 315 else if (v9fs_proto_dotu(v9ses)) 316 inode->i_op = &v9fs_dir_inode_operations_dotu; 317 else 318 inode->i_op = &v9fs_dir_inode_operations; 319 320 if (v9fs_proto_dotl(v9ses)) 321 inode->i_fop = &v9fs_dir_operations_dotl; 322 else 323 inode->i_fop = &v9fs_dir_operations; 324 325 break; 326 default: 327 p9_debug(P9_DEBUG_ERROR, "BAD mode 0x%hx S_IFMT 0x%x\n", 328 mode, mode & S_IFMT); 329 err = -EINVAL; 330 goto error; 331 } 332 error: 333 return err; 334 335 } 336 337 /** 338 * v9fs_evict_inode - Remove an inode from the inode cache 339 * @inode: inode to release 340 * 341 */ 342 void v9fs_evict_inode(struct inode *inode) 343 { 344 struct v9fs_inode __maybe_unused *v9inode = V9FS_I(inode); 345 __le32 __maybe_unused version; 346 347 if (!is_bad_inode(inode)) { 348 truncate_inode_pages_final(&inode->i_data); 349 350 version = cpu_to_le32(v9inode->qid.version); 351 netfs_clear_inode_writeback(inode, &version); 352 353 clear_inode(inode); 354 filemap_fdatawrite(&inode->i_data); 355 356 #ifdef CONFIG_9P_FSCACHE 357 if (v9fs_inode_cookie(v9inode)) 358 fscache_relinquish_cookie(v9fs_inode_cookie(v9inode), false); 359 #endif 360 } else 361 clear_inode(inode); 362 } 363 364 struct inode *v9fs_fid_iget(struct super_block *sb, struct p9_fid *fid) 365 { 366 dev_t rdev; 367 int retval; 368 umode_t umode; 369 struct inode *inode; 370 struct p9_wstat *st; 371 struct v9fs_session_info *v9ses = sb->s_fs_info; 372 373 inode = iget_locked(sb, QID2INO(&fid->qid)); 374 if (unlikely(!inode)) 375 return ERR_PTR(-ENOMEM); 376 if (!(inode->i_state & I_NEW)) 377 return inode; 378 379 /* 380 * initialize the inode with the stat info 381 * FIXME!! we may need support for stale inodes 382 * later. 383 */ 384 st = p9_client_stat(fid); 385 if (IS_ERR(st)) { 386 retval = PTR_ERR(st); 387 goto error; 388 } 389 390 umode = p9mode2unixmode(v9ses, st, &rdev); 391 retval = v9fs_init_inode(v9ses, inode, &fid->qid, umode, rdev); 392 v9fs_stat2inode(st, inode, sb, 0); 393 p9stat_free(st); 394 kfree(st); 395 if (retval) 396 goto error; 397 398 v9fs_set_netfs_context(inode); 399 v9fs_cache_inode_get_cookie(inode); 400 unlock_new_inode(inode); 401 return inode; 402 error: 403 iget_failed(inode); 404 return ERR_PTR(retval); 405 406 } 407 408 /** 409 * v9fs_at_to_dotl_flags- convert Linux specific AT flags to 410 * plan 9 AT flag. 411 * @flags: flags to convert 412 */ 413 static int v9fs_at_to_dotl_flags(int flags) 414 { 415 int rflags = 0; 416 417 if (flags & AT_REMOVEDIR) 418 rflags |= P9_DOTL_AT_REMOVEDIR; 419 420 return rflags; 421 } 422 423 /** 424 * v9fs_dec_count - helper functon to drop i_nlink. 425 * 426 * If a directory had nlink <= 2 (including . and ..), then we should not drop 427 * the link count, which indicates the underlying exported fs doesn't maintain 428 * nlink accurately. e.g. 429 * - overlayfs sets nlink to 1 for merged dir 430 * - ext4 (with dir_nlink feature enabled) sets nlink to 1 if a dir has more 431 * than EXT4_LINK_MAX (65000) links. 432 * 433 * @inode: inode whose nlink is being dropped 434 */ 435 static void v9fs_dec_count(struct inode *inode) 436 { 437 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2) 438 drop_nlink(inode); 439 } 440 441 /** 442 * v9fs_remove - helper function to remove files and directories 443 * @dir: directory inode that is being deleted 444 * @dentry: dentry that is being deleted 445 * @flags: removing a directory 446 * 447 */ 448 449 static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags) 450 { 451 struct inode *inode; 452 int retval = -EOPNOTSUPP; 453 struct p9_fid *v9fid, *dfid; 454 struct v9fs_session_info *v9ses; 455 456 p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n", 457 dir, dentry, flags); 458 459 v9ses = v9fs_inode2v9ses(dir); 460 inode = d_inode(dentry); 461 dfid = v9fs_parent_fid(dentry); 462 if (IS_ERR(dfid)) { 463 retval = PTR_ERR(dfid); 464 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval); 465 return retval; 466 } 467 if (v9fs_proto_dotl(v9ses)) 468 retval = p9_client_unlinkat(dfid, dentry->d_name.name, 469 v9fs_at_to_dotl_flags(flags)); 470 p9_fid_put(dfid); 471 if (retval == -EOPNOTSUPP) { 472 /* Try the one based on path */ 473 v9fid = v9fs_fid_clone(dentry); 474 if (IS_ERR(v9fid)) 475 return PTR_ERR(v9fid); 476 retval = p9_client_remove(v9fid); 477 } 478 if (!retval) { 479 /* 480 * directories on unlink should have zero 481 * link count 482 */ 483 if (flags & AT_REMOVEDIR) { 484 clear_nlink(inode); 485 v9fs_dec_count(dir); 486 } else 487 v9fs_dec_count(inode); 488 489 v9fs_invalidate_inode_attr(inode); 490 v9fs_invalidate_inode_attr(dir); 491 492 /* invalidate all fids associated with dentry */ 493 /* NOTE: This will not include open fids */ 494 dentry->d_op->d_release(dentry); 495 } 496 return retval; 497 } 498 499 /** 500 * v9fs_create - Create a file 501 * @v9ses: session information 502 * @dir: directory that dentry is being created in 503 * @dentry: dentry that is being created 504 * @extension: 9p2000.u extension string to support devices, etc. 505 * @perm: create permissions 506 * @mode: open mode 507 * 508 */ 509 static struct p9_fid * 510 v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir, 511 struct dentry *dentry, char *extension, u32 perm, u8 mode) 512 { 513 int err; 514 const unsigned char *name; 515 struct p9_fid *dfid, *ofid = NULL, *fid = NULL; 516 struct inode *inode; 517 518 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry); 519 520 name = dentry->d_name.name; 521 dfid = v9fs_parent_fid(dentry); 522 if (IS_ERR(dfid)) { 523 err = PTR_ERR(dfid); 524 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err); 525 return ERR_PTR(err); 526 } 527 528 /* clone a fid to use for creation */ 529 ofid = clone_fid(dfid); 530 if (IS_ERR(ofid)) { 531 err = PTR_ERR(ofid); 532 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err); 533 goto error; 534 } 535 536 err = p9_client_fcreate(ofid, name, perm, mode, extension); 537 if (err < 0) { 538 p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err); 539 goto error; 540 } 541 542 if (!(perm & P9_DMLINK)) { 543 /* now walk from the parent so we can get unopened fid */ 544 fid = p9_client_walk(dfid, 1, &name, 1); 545 if (IS_ERR(fid)) { 546 err = PTR_ERR(fid); 547 p9_debug(P9_DEBUG_VFS, 548 "p9_client_walk failed %d\n", err); 549 goto error; 550 } 551 /* 552 * instantiate inode and assign the unopened fid to the dentry 553 */ 554 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb); 555 if (IS_ERR(inode)) { 556 err = PTR_ERR(inode); 557 p9_debug(P9_DEBUG_VFS, 558 "inode creation failed %d\n", err); 559 goto error; 560 } 561 v9fs_fid_add(dentry, &fid); 562 d_instantiate(dentry, inode); 563 } 564 p9_fid_put(dfid); 565 return ofid; 566 error: 567 p9_fid_put(dfid); 568 p9_fid_put(ofid); 569 p9_fid_put(fid); 570 return ERR_PTR(err); 571 } 572 573 /** 574 * v9fs_vfs_create - VFS hook to create a regular file 575 * @idmap: idmap of the mount 576 * @dir: The parent directory 577 * @dentry: The name of file to be created 578 * @mode: The UNIX file mode to set 579 * @excl: True if the file must not yet exist 580 * 581 * open(.., O_CREAT) is handled in v9fs_vfs_atomic_open(). This is only called 582 * for mknod(2). 583 * 584 */ 585 586 static int 587 v9fs_vfs_create(struct mnt_idmap *idmap, struct inode *dir, 588 struct dentry *dentry, umode_t mode, bool excl) 589 { 590 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir); 591 u32 perm = unixmode2p9mode(v9ses, mode); 592 struct p9_fid *fid; 593 594 /* P9_OEXCL? */ 595 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_ORDWR); 596 if (IS_ERR(fid)) 597 return PTR_ERR(fid); 598 599 v9fs_invalidate_inode_attr(dir); 600 p9_fid_put(fid); 601 602 return 0; 603 } 604 605 /** 606 * v9fs_vfs_mkdir - VFS mkdir hook to create a directory 607 * @idmap: idmap of the mount 608 * @dir: inode that is being unlinked 609 * @dentry: dentry that is being unlinked 610 * @mode: mode for new directory 611 * 612 */ 613 614 static int v9fs_vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir, 615 struct dentry *dentry, umode_t mode) 616 { 617 int err; 618 u32 perm; 619 struct p9_fid *fid; 620 struct v9fs_session_info *v9ses; 621 622 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry); 623 err = 0; 624 v9ses = v9fs_inode2v9ses(dir); 625 perm = unixmode2p9mode(v9ses, mode | S_IFDIR); 626 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD); 627 if (IS_ERR(fid)) { 628 err = PTR_ERR(fid); 629 fid = NULL; 630 } else { 631 inc_nlink(dir); 632 v9fs_invalidate_inode_attr(dir); 633 } 634 635 if (fid) 636 p9_fid_put(fid); 637 638 return err; 639 } 640 641 /** 642 * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode 643 * @dir: inode that is being walked from 644 * @dentry: dentry that is being walked to? 645 * @flags: lookup flags (unused) 646 * 647 */ 648 649 struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry, 650 unsigned int flags) 651 { 652 struct dentry *res; 653 struct v9fs_session_info *v9ses; 654 struct p9_fid *dfid, *fid; 655 struct inode *inode; 656 const unsigned char *name; 657 658 p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%pd) %p flags: %x\n", 659 dir, dentry, dentry, flags); 660 661 if (dentry->d_name.len > NAME_MAX) 662 return ERR_PTR(-ENAMETOOLONG); 663 664 v9ses = v9fs_inode2v9ses(dir); 665 /* We can walk d_parent because we hold the dir->i_mutex */ 666 dfid = v9fs_parent_fid(dentry); 667 if (IS_ERR(dfid)) 668 return ERR_CAST(dfid); 669 670 /* 671 * Make sure we don't use a wrong inode due to parallel 672 * unlink. For cached mode create calls request for new 673 * inode. But with cache disabled, lookup should do this. 674 */ 675 name = dentry->d_name.name; 676 fid = p9_client_walk(dfid, 1, &name, 1); 677 p9_fid_put(dfid); 678 if (fid == ERR_PTR(-ENOENT)) 679 inode = NULL; 680 else if (IS_ERR(fid)) 681 inode = ERR_CAST(fid); 682 else 683 inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb); 684 /* 685 * If we had a rename on the server and a parallel lookup 686 * for the new name, then make sure we instantiate with 687 * the new name. ie look up for a/b, while on server somebody 688 * moved b under k and client parallely did a lookup for 689 * k/b. 690 */ 691 res = d_splice_alias(inode, dentry); 692 if (!IS_ERR(fid)) { 693 if (!res) 694 v9fs_fid_add(dentry, &fid); 695 else if (!IS_ERR(res)) 696 v9fs_fid_add(res, &fid); 697 else 698 p9_fid_put(fid); 699 } 700 return res; 701 } 702 703 static int 704 v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry, 705 struct file *file, unsigned int flags, umode_t mode) 706 { 707 int err; 708 u32 perm; 709 struct v9fs_inode __maybe_unused *v9inode; 710 struct v9fs_session_info *v9ses; 711 struct p9_fid *fid; 712 struct dentry *res = NULL; 713 struct inode *inode; 714 int p9_omode; 715 716 if (d_in_lookup(dentry)) { 717 res = v9fs_vfs_lookup(dir, dentry, 0); 718 if (IS_ERR(res)) 719 return PTR_ERR(res); 720 721 if (res) 722 dentry = res; 723 } 724 725 /* Only creates */ 726 if (!(flags & O_CREAT) || d_really_is_positive(dentry)) 727 return finish_no_open(file, res); 728 729 v9ses = v9fs_inode2v9ses(dir); 730 perm = unixmode2p9mode(v9ses, mode); 731 p9_omode = v9fs_uflags2omode(flags, v9fs_proto_dotu(v9ses)); 732 733 if ((v9ses->cache & CACHE_WRITEBACK) && (p9_omode & P9_OWRITE)) { 734 p9_omode = (p9_omode & ~P9_OWRITE) | P9_ORDWR; 735 p9_debug(P9_DEBUG_CACHE, 736 "write-only file with writeback enabled, creating w/ O_RDWR\n"); 737 } 738 fid = v9fs_create(v9ses, dir, dentry, NULL, perm, p9_omode); 739 if (IS_ERR(fid)) { 740 err = PTR_ERR(fid); 741 goto error; 742 } 743 744 v9fs_invalidate_inode_attr(dir); 745 inode = d_inode(dentry); 746 v9inode = V9FS_I(inode); 747 err = finish_open(file, dentry, generic_file_open); 748 if (err) 749 goto error; 750 751 file->private_data = fid; 752 #ifdef CONFIG_9P_FSCACHE 753 if (v9ses->cache & CACHE_FSCACHE) 754 fscache_use_cookie(v9fs_inode_cookie(v9inode), 755 file->f_mode & FMODE_WRITE); 756 #endif 757 758 v9fs_fid_add_modes(fid, v9ses->flags, v9ses->cache, file->f_flags); 759 v9fs_open_fid_add(inode, &fid); 760 761 file->f_mode |= FMODE_CREATED; 762 out: 763 dput(res); 764 return err; 765 766 error: 767 p9_fid_put(fid); 768 goto out; 769 } 770 771 /** 772 * v9fs_vfs_unlink - VFS unlink hook to delete an inode 773 * @i: inode that is being unlinked 774 * @d: dentry that is being unlinked 775 * 776 */ 777 778 int v9fs_vfs_unlink(struct inode *i, struct dentry *d) 779 { 780 return v9fs_remove(i, d, 0); 781 } 782 783 /** 784 * v9fs_vfs_rmdir - VFS unlink hook to delete a directory 785 * @i: inode that is being unlinked 786 * @d: dentry that is being unlinked 787 * 788 */ 789 790 int v9fs_vfs_rmdir(struct inode *i, struct dentry *d) 791 { 792 return v9fs_remove(i, d, AT_REMOVEDIR); 793 } 794 795 /** 796 * v9fs_vfs_rename - VFS hook to rename an inode 797 * @idmap: The idmap of the mount 798 * @old_dir: old dir inode 799 * @old_dentry: old dentry 800 * @new_dir: new dir inode 801 * @new_dentry: new dentry 802 * @flags: RENAME_* flags 803 * 804 */ 805 806 int 807 v9fs_vfs_rename(struct mnt_idmap *idmap, struct inode *old_dir, 808 struct dentry *old_dentry, struct inode *new_dir, 809 struct dentry *new_dentry, unsigned int flags) 810 { 811 int retval; 812 struct inode *old_inode; 813 struct inode *new_inode; 814 struct v9fs_session_info *v9ses; 815 struct p9_fid *oldfid = NULL, *dfid = NULL; 816 struct p9_fid *olddirfid = NULL; 817 struct p9_fid *newdirfid = NULL; 818 struct p9_wstat wstat; 819 820 if (flags) 821 return -EINVAL; 822 823 p9_debug(P9_DEBUG_VFS, "\n"); 824 old_inode = d_inode(old_dentry); 825 new_inode = d_inode(new_dentry); 826 v9ses = v9fs_inode2v9ses(old_inode); 827 oldfid = v9fs_fid_lookup(old_dentry); 828 if (IS_ERR(oldfid)) 829 return PTR_ERR(oldfid); 830 831 dfid = v9fs_parent_fid(old_dentry); 832 olddirfid = clone_fid(dfid); 833 p9_fid_put(dfid); 834 dfid = NULL; 835 836 if (IS_ERR(olddirfid)) { 837 retval = PTR_ERR(olddirfid); 838 goto error; 839 } 840 841 dfid = v9fs_parent_fid(new_dentry); 842 newdirfid = clone_fid(dfid); 843 p9_fid_put(dfid); 844 dfid = NULL; 845 846 if (IS_ERR(newdirfid)) { 847 retval = PTR_ERR(newdirfid); 848 goto error; 849 } 850 851 down_write(&v9ses->rename_sem); 852 if (v9fs_proto_dotl(v9ses)) { 853 retval = p9_client_renameat(olddirfid, old_dentry->d_name.name, 854 newdirfid, new_dentry->d_name.name); 855 if (retval == -EOPNOTSUPP) 856 retval = p9_client_rename(oldfid, newdirfid, 857 new_dentry->d_name.name); 858 if (retval != -EOPNOTSUPP) 859 goto error_locked; 860 } 861 if (old_dentry->d_parent != new_dentry->d_parent) { 862 /* 863 * 9P .u can only handle file rename in the same directory 864 */ 865 866 p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n"); 867 retval = -EXDEV; 868 goto error_locked; 869 } 870 v9fs_blank_wstat(&wstat); 871 wstat.muid = v9ses->uname; 872 wstat.name = new_dentry->d_name.name; 873 retval = p9_client_wstat(oldfid, &wstat); 874 875 error_locked: 876 if (!retval) { 877 if (new_inode) { 878 if (S_ISDIR(new_inode->i_mode)) 879 clear_nlink(new_inode); 880 else 881 v9fs_dec_count(new_inode); 882 } 883 if (S_ISDIR(old_inode->i_mode)) { 884 if (!new_inode) 885 inc_nlink(new_dir); 886 v9fs_dec_count(old_dir); 887 } 888 v9fs_invalidate_inode_attr(old_inode); 889 v9fs_invalidate_inode_attr(old_dir); 890 v9fs_invalidate_inode_attr(new_dir); 891 892 /* successful rename */ 893 d_move(old_dentry, new_dentry); 894 } 895 up_write(&v9ses->rename_sem); 896 897 error: 898 p9_fid_put(newdirfid); 899 p9_fid_put(olddirfid); 900 p9_fid_put(oldfid); 901 return retval; 902 } 903 904 /** 905 * v9fs_vfs_getattr - retrieve file metadata 906 * @idmap: idmap of the mount 907 * @path: Object to query 908 * @stat: metadata structure to populate 909 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests 910 * @flags: AT_STATX_xxx setting 911 * 912 */ 913 914 static int 915 v9fs_vfs_getattr(struct mnt_idmap *idmap, const struct path *path, 916 struct kstat *stat, u32 request_mask, unsigned int flags) 917 { 918 struct dentry *dentry = path->dentry; 919 struct inode *inode = d_inode(dentry); 920 struct v9fs_session_info *v9ses; 921 struct p9_fid *fid; 922 struct p9_wstat *st; 923 924 p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry); 925 v9ses = v9fs_dentry2v9ses(dentry); 926 if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) { 927 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat); 928 return 0; 929 } else if (v9ses->cache & CACHE_WRITEBACK) { 930 if (S_ISREG(inode->i_mode)) { 931 int retval = filemap_fdatawrite(inode->i_mapping); 932 933 if (retval) 934 p9_debug(P9_DEBUG_ERROR, 935 "flushing writeback during getattr returned %d\n", retval); 936 } 937 } 938 fid = v9fs_fid_lookup(dentry); 939 if (IS_ERR(fid)) 940 return PTR_ERR(fid); 941 942 st = p9_client_stat(fid); 943 p9_fid_put(fid); 944 if (IS_ERR(st)) 945 return PTR_ERR(st); 946 947 v9fs_stat2inode(st, d_inode(dentry), dentry->d_sb, 0); 948 generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(dentry), stat); 949 950 p9stat_free(st); 951 kfree(st); 952 return 0; 953 } 954 955 /** 956 * v9fs_vfs_setattr - set file metadata 957 * @idmap: idmap of the mount 958 * @dentry: file whose metadata to set 959 * @iattr: metadata assignment structure 960 * 961 */ 962 963 static int v9fs_vfs_setattr(struct mnt_idmap *idmap, 964 struct dentry *dentry, struct iattr *iattr) 965 { 966 int retval, use_dentry = 0; 967 struct inode *inode = d_inode(dentry); 968 struct v9fs_session_info *v9ses; 969 struct p9_fid *fid = NULL; 970 struct p9_wstat wstat; 971 972 p9_debug(P9_DEBUG_VFS, "\n"); 973 retval = setattr_prepare(&nop_mnt_idmap, dentry, iattr); 974 if (retval) 975 return retval; 976 977 v9ses = v9fs_dentry2v9ses(dentry); 978 if (iattr->ia_valid & ATTR_FILE) { 979 fid = iattr->ia_file->private_data; 980 WARN_ON(!fid); 981 } 982 if (!fid) { 983 fid = v9fs_fid_lookup(dentry); 984 use_dentry = 1; 985 } 986 if (IS_ERR(fid)) 987 return PTR_ERR(fid); 988 989 v9fs_blank_wstat(&wstat); 990 if (iattr->ia_valid & ATTR_MODE) 991 wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode); 992 993 if (iattr->ia_valid & ATTR_MTIME) 994 wstat.mtime = iattr->ia_mtime.tv_sec; 995 996 if (iattr->ia_valid & ATTR_ATIME) 997 wstat.atime = iattr->ia_atime.tv_sec; 998 999 if (iattr->ia_valid & ATTR_SIZE) 1000 wstat.length = iattr->ia_size; 1001 1002 if (v9fs_proto_dotu(v9ses)) { 1003 if (iattr->ia_valid & ATTR_UID) 1004 wstat.n_uid = iattr->ia_uid; 1005 1006 if (iattr->ia_valid & ATTR_GID) 1007 wstat.n_gid = iattr->ia_gid; 1008 } 1009 1010 /* Write all dirty data */ 1011 if (d_is_reg(dentry)) { 1012 retval = filemap_fdatawrite(inode->i_mapping); 1013 if (retval) 1014 p9_debug(P9_DEBUG_ERROR, 1015 "flushing writeback during setattr returned %d\n", retval); 1016 } 1017 1018 retval = p9_client_wstat(fid, &wstat); 1019 1020 if (use_dentry) 1021 p9_fid_put(fid); 1022 1023 if (retval < 0) 1024 return retval; 1025 1026 if ((iattr->ia_valid & ATTR_SIZE) && 1027 iattr->ia_size != i_size_read(inode)) { 1028 truncate_setsize(inode, iattr->ia_size); 1029 netfs_resize_file(netfs_inode(inode), iattr->ia_size, true); 1030 1031 #ifdef CONFIG_9P_FSCACHE 1032 if (v9ses->cache & CACHE_FSCACHE) { 1033 struct v9fs_inode *v9inode = V9FS_I(inode); 1034 1035 fscache_resize_cookie(v9fs_inode_cookie(v9inode), iattr->ia_size); 1036 } 1037 #endif 1038 } 1039 1040 v9fs_invalidate_inode_attr(inode); 1041 1042 setattr_copy(&nop_mnt_idmap, inode, iattr); 1043 mark_inode_dirty(inode); 1044 return 0; 1045 } 1046 1047 /** 1048 * v9fs_stat2inode - populate an inode structure with mistat info 1049 * @stat: Plan 9 metadata (mistat) structure 1050 * @inode: inode to populate 1051 * @sb: superblock of filesystem 1052 * @flags: control flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE) 1053 * 1054 */ 1055 1056 void 1057 v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode, 1058 struct super_block *sb, unsigned int flags) 1059 { 1060 umode_t mode; 1061 struct v9fs_session_info *v9ses = sb->s_fs_info; 1062 struct v9fs_inode *v9inode = V9FS_I(inode); 1063 1064 set_nlink(inode, 1); 1065 1066 inode_set_atime(inode, stat->atime, 0); 1067 inode_set_mtime(inode, stat->mtime, 0); 1068 inode_set_ctime(inode, stat->mtime, 0); 1069 1070 inode->i_uid = v9ses->dfltuid; 1071 inode->i_gid = v9ses->dfltgid; 1072 1073 if (v9fs_proto_dotu(v9ses)) { 1074 inode->i_uid = stat->n_uid; 1075 inode->i_gid = stat->n_gid; 1076 } 1077 if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) { 1078 if (v9fs_proto_dotu(v9ses)) { 1079 unsigned int i_nlink; 1080 /* 1081 * Hadlink support got added later to the .u extension. 1082 * So there can be a server out there that doesn't 1083 * support this even with .u extension. That would 1084 * just leave us with stat->extension being an empty 1085 * string, though. 1086 */ 1087 /* HARDLINKCOUNT %u */ 1088 if (sscanf(stat->extension, 1089 " HARDLINKCOUNT %u", &i_nlink) == 1) 1090 set_nlink(inode, i_nlink); 1091 } 1092 } 1093 mode = p9mode2perm(v9ses, stat); 1094 mode |= inode->i_mode & ~S_IALLUGO; 1095 inode->i_mode = mode; 1096 1097 v9inode->netfs.remote_i_size = stat->length; 1098 if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE)) 1099 v9fs_i_size_write(inode, stat->length); 1100 /* not real number of blocks, but 512 byte ones ... */ 1101 inode->i_blocks = (stat->length + 512 - 1) >> 9; 1102 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR; 1103 } 1104 1105 /** 1106 * v9fs_vfs_get_link - follow a symlink path 1107 * @dentry: dentry for symlink 1108 * @inode: inode for symlink 1109 * @done: delayed call for when we are done with the return value 1110 */ 1111 1112 static const char *v9fs_vfs_get_link(struct dentry *dentry, 1113 struct inode *inode, 1114 struct delayed_call *done) 1115 { 1116 struct v9fs_session_info *v9ses; 1117 struct p9_fid *fid; 1118 struct p9_wstat *st; 1119 char *res; 1120 1121 if (!dentry) 1122 return ERR_PTR(-ECHILD); 1123 1124 v9ses = v9fs_dentry2v9ses(dentry); 1125 if (!v9fs_proto_dotu(v9ses)) 1126 return ERR_PTR(-EBADF); 1127 1128 p9_debug(P9_DEBUG_VFS, "%pd\n", dentry); 1129 fid = v9fs_fid_lookup(dentry); 1130 1131 if (IS_ERR(fid)) 1132 return ERR_CAST(fid); 1133 1134 st = p9_client_stat(fid); 1135 p9_fid_put(fid); 1136 if (IS_ERR(st)) 1137 return ERR_CAST(st); 1138 1139 if (!(st->mode & P9_DMSYMLINK)) { 1140 p9stat_free(st); 1141 kfree(st); 1142 return ERR_PTR(-EINVAL); 1143 } 1144 res = st->extension; 1145 st->extension = NULL; 1146 if (strlen(res) >= PATH_MAX) 1147 res[PATH_MAX - 1] = '\0'; 1148 1149 p9stat_free(st); 1150 kfree(st); 1151 set_delayed_call(done, kfree_link, res); 1152 return res; 1153 } 1154 1155 /** 1156 * v9fs_vfs_mkspecial - create a special file 1157 * @dir: inode to create special file in 1158 * @dentry: dentry to create 1159 * @perm: mode to create special file 1160 * @extension: 9p2000.u format extension string representing special file 1161 * 1162 */ 1163 1164 static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry, 1165 u32 perm, const char *extension) 1166 { 1167 struct p9_fid *fid; 1168 struct v9fs_session_info *v9ses; 1169 1170 v9ses = v9fs_inode2v9ses(dir); 1171 if (!v9fs_proto_dotu(v9ses)) { 1172 p9_debug(P9_DEBUG_ERROR, "not extended\n"); 1173 return -EPERM; 1174 } 1175 1176 fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm, 1177 P9_OREAD); 1178 if (IS_ERR(fid)) 1179 return PTR_ERR(fid); 1180 1181 v9fs_invalidate_inode_attr(dir); 1182 p9_fid_put(fid); 1183 return 0; 1184 } 1185 1186 /** 1187 * v9fs_vfs_symlink - helper function to create symlinks 1188 * @idmap: idmap of the mount 1189 * @dir: directory inode containing symlink 1190 * @dentry: dentry for symlink 1191 * @symname: symlink data 1192 * 1193 * See Also: 9P2000.u RFC for more information 1194 * 1195 */ 1196 1197 static int 1198 v9fs_vfs_symlink(struct mnt_idmap *idmap, struct inode *dir, 1199 struct dentry *dentry, const char *symname) 1200 { 1201 p9_debug(P9_DEBUG_VFS, " %lu,%pd,%s\n", 1202 dir->i_ino, dentry, symname); 1203 1204 return v9fs_vfs_mkspecial(dir, dentry, P9_DMSYMLINK, symname); 1205 } 1206 1207 #define U32_MAX_DIGITS 10 1208 1209 /** 1210 * v9fs_vfs_link - create a hardlink 1211 * @old_dentry: dentry for file to link to 1212 * @dir: inode destination for new link 1213 * @dentry: dentry for link 1214 * 1215 */ 1216 1217 static int 1218 v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir, 1219 struct dentry *dentry) 1220 { 1221 int retval; 1222 char name[1 + U32_MAX_DIGITS + 2]; /* sign + number + \n + \0 */ 1223 struct p9_fid *oldfid; 1224 1225 p9_debug(P9_DEBUG_VFS, " %lu,%pd,%pd\n", 1226 dir->i_ino, dentry, old_dentry); 1227 1228 oldfid = v9fs_fid_clone(old_dentry); 1229 if (IS_ERR(oldfid)) 1230 return PTR_ERR(oldfid); 1231 1232 sprintf(name, "%d\n", oldfid->fid); 1233 retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name); 1234 if (!retval) { 1235 v9fs_refresh_inode(oldfid, d_inode(old_dentry)); 1236 v9fs_invalidate_inode_attr(dir); 1237 } 1238 p9_fid_put(oldfid); 1239 return retval; 1240 } 1241 1242 /** 1243 * v9fs_vfs_mknod - create a special file 1244 * @idmap: idmap of the mount 1245 * @dir: inode destination for new link 1246 * @dentry: dentry for file 1247 * @mode: mode for creation 1248 * @rdev: device associated with special file 1249 * 1250 */ 1251 1252 static int 1253 v9fs_vfs_mknod(struct mnt_idmap *idmap, struct inode *dir, 1254 struct dentry *dentry, umode_t mode, dev_t rdev) 1255 { 1256 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir); 1257 int retval; 1258 char name[2 + U32_MAX_DIGITS + 1 + U32_MAX_DIGITS + 1]; 1259 u32 perm; 1260 1261 p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %x MAJOR: %u MINOR: %u\n", 1262 dir->i_ino, dentry, mode, 1263 MAJOR(rdev), MINOR(rdev)); 1264 1265 /* build extension */ 1266 if (S_ISBLK(mode)) 1267 sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev)); 1268 else if (S_ISCHR(mode)) 1269 sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev)); 1270 else 1271 *name = 0; 1272 1273 perm = unixmode2p9mode(v9ses, mode); 1274 retval = v9fs_vfs_mkspecial(dir, dentry, perm, name); 1275 1276 return retval; 1277 } 1278 1279 int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode) 1280 { 1281 int umode; 1282 dev_t rdev; 1283 struct p9_wstat *st; 1284 struct v9fs_session_info *v9ses; 1285 unsigned int flags; 1286 1287 v9ses = v9fs_inode2v9ses(inode); 1288 st = p9_client_stat(fid); 1289 if (IS_ERR(st)) 1290 return PTR_ERR(st); 1291 /* 1292 * Don't update inode if the file type is different 1293 */ 1294 umode = p9mode2unixmode(v9ses, st, &rdev); 1295 if (inode_wrong_type(inode, umode)) 1296 goto out; 1297 1298 /* 1299 * We don't want to refresh inode->i_size, 1300 * because we may have cached data 1301 */ 1302 flags = (v9ses->cache & CACHE_LOOSE) ? 1303 V9FS_STAT2INODE_KEEP_ISIZE : 0; 1304 v9fs_stat2inode(st, inode, inode->i_sb, flags); 1305 out: 1306 p9stat_free(st); 1307 kfree(st); 1308 return 0; 1309 } 1310 1311 static const struct inode_operations v9fs_dir_inode_operations_dotu = { 1312 .create = v9fs_vfs_create, 1313 .lookup = v9fs_vfs_lookup, 1314 .atomic_open = v9fs_vfs_atomic_open, 1315 .symlink = v9fs_vfs_symlink, 1316 .link = v9fs_vfs_link, 1317 .unlink = v9fs_vfs_unlink, 1318 .mkdir = v9fs_vfs_mkdir, 1319 .rmdir = v9fs_vfs_rmdir, 1320 .mknod = v9fs_vfs_mknod, 1321 .rename = v9fs_vfs_rename, 1322 .getattr = v9fs_vfs_getattr, 1323 .setattr = v9fs_vfs_setattr, 1324 }; 1325 1326 static const struct inode_operations v9fs_dir_inode_operations = { 1327 .create = v9fs_vfs_create, 1328 .lookup = v9fs_vfs_lookup, 1329 .atomic_open = v9fs_vfs_atomic_open, 1330 .unlink = v9fs_vfs_unlink, 1331 .mkdir = v9fs_vfs_mkdir, 1332 .rmdir = v9fs_vfs_rmdir, 1333 .mknod = v9fs_vfs_mknod, 1334 .rename = v9fs_vfs_rename, 1335 .getattr = v9fs_vfs_getattr, 1336 .setattr = v9fs_vfs_setattr, 1337 }; 1338 1339 static const struct inode_operations v9fs_file_inode_operations = { 1340 .getattr = v9fs_vfs_getattr, 1341 .setattr = v9fs_vfs_setattr, 1342 }; 1343 1344 static const struct inode_operations v9fs_symlink_inode_operations = { 1345 .get_link = v9fs_vfs_get_link, 1346 .getattr = v9fs_vfs_getattr, 1347 .setattr = v9fs_vfs_setattr, 1348 }; 1349 1350