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