1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * This file contains vfs inode ops for the 9P2000.L 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 #include <linux/module.h> 10 #include <linux/errno.h> 11 #include <linux/fs.h> 12 #include <linux/file.h> 13 #include <linux/pagemap.h> 14 #include <linux/stat.h> 15 #include <linux/string.h> 16 #include <linux/namei.h> 17 #include <linux/sched.h> 18 #include <linux/slab.h> 19 #include <linux/xattr.h> 20 #include <linux/posix_acl.h> 21 #include <net/9p/9p.h> 22 #include <net/9p/client.h> 23 24 #include "v9fs.h" 25 #include "v9fs_vfs.h" 26 #include "fid.h" 27 #include "cache.h" 28 #include "xattr.h" 29 #include "acl.h" 30 31 static int 32 v9fs_vfs_mknod_dotl(struct mnt_idmap *idmap, struct inode *dir, 33 struct dentry *dentry, umode_t omode, dev_t rdev); 34 35 /** 36 * v9fs_get_fsgid_for_create - Helper function to get the gid for a new object 37 * @dir_inode: The directory inode 38 * 39 * Helper function to get the gid for creating a 40 * new file system object. This checks the S_ISGID to determine the owning 41 * group of the new file system object. 42 */ 43 44 static kgid_t v9fs_get_fsgid_for_create(struct inode *dir_inode) 45 { 46 BUG_ON(dir_inode == NULL); 47 48 if (dir_inode->i_mode & S_ISGID) { 49 /* set_gid bit is set.*/ 50 return dir_inode->i_gid; 51 } 52 return current_fsgid(); 53 } 54 55 static int v9fs_test_inode_dotl(struct inode *inode, void *data) 56 { 57 struct v9fs_inode *v9inode = V9FS_I(inode); 58 struct p9_stat_dotl *st = (struct p9_stat_dotl *)data; 59 60 /* don't match inode of different type */ 61 if (inode_wrong_type(inode, st->st_mode)) 62 return 0; 63 64 if (inode->i_generation != st->st_gen) 65 return 0; 66 67 /* compare qid details */ 68 if (memcmp(&v9inode->qid.version, 69 &st->qid.version, sizeof(v9inode->qid.version))) 70 return 0; 71 72 if (v9inode->qid.type != st->qid.type) 73 return 0; 74 75 if (v9inode->qid.path != st->qid.path) 76 return 0; 77 return 1; 78 } 79 80 /* Always get a new inode */ 81 static int v9fs_test_new_inode_dotl(struct inode *inode, void *data) 82 { 83 return 0; 84 } 85 86 static int v9fs_set_inode_dotl(struct inode *inode, void *data) 87 { 88 struct v9fs_inode *v9inode = V9FS_I(inode); 89 struct p9_stat_dotl *st = (struct p9_stat_dotl *)data; 90 91 memcpy(&v9inode->qid, &st->qid, sizeof(st->qid)); 92 inode->i_generation = st->st_gen; 93 return 0; 94 } 95 96 static struct inode *v9fs_qid_iget_dotl(struct super_block *sb, 97 struct p9_qid *qid, 98 struct p9_fid *fid, 99 struct p9_stat_dotl *st, 100 int new) 101 { 102 int retval; 103 struct inode *inode; 104 struct v9fs_session_info *v9ses = sb->s_fs_info; 105 int (*test)(struct inode *inode, void *data); 106 107 if (new) 108 test = v9fs_test_new_inode_dotl; 109 else 110 test = v9fs_test_inode_dotl; 111 112 inode = iget5_locked(sb, QID2INO(qid), test, v9fs_set_inode_dotl, st); 113 if (!inode) 114 return ERR_PTR(-ENOMEM); 115 if (!(inode_state_read_once(inode) & I_NEW)) 116 return inode; 117 /* 118 * initialize the inode with the stat info 119 * FIXME!! we may need support for stale inodes 120 * later. 121 */ 122 inode->i_ino = QID2INO(qid); 123 retval = v9fs_init_inode(v9ses, inode, 124 st->st_mode, new_decode_dev(st->st_rdev)); 125 if (retval) 126 goto error; 127 128 v9fs_stat2inode_dotl(st, inode, 0); 129 v9fs_set_netfs_context(inode); 130 v9fs_cache_inode_get_cookie(inode); 131 retval = v9fs_get_acl(inode, fid); 132 if (retval) 133 goto error; 134 135 unlock_new_inode(inode); 136 return inode; 137 error: 138 iget_failed(inode); 139 return ERR_PTR(retval); 140 141 } 142 143 struct inode * 144 v9fs_inode_from_fid_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid, 145 struct super_block *sb, int new) 146 { 147 struct p9_stat_dotl *st; 148 struct inode *inode = NULL; 149 150 st = p9_client_getattr_dotl(fid, P9_STATS_BASIC | P9_STATS_GEN); 151 if (IS_ERR(st)) 152 return ERR_CAST(st); 153 154 inode = v9fs_qid_iget_dotl(sb, &st->qid, fid, st, new); 155 kfree(st); 156 return inode; 157 } 158 159 struct dotl_openflag_map { 160 int open_flag; 161 int dotl_flag; 162 }; 163 164 static int v9fs_mapped_dotl_flags(int flags) 165 { 166 int i; 167 int rflags = 0; 168 struct dotl_openflag_map dotl_oflag_map[] = { 169 { O_CREAT, P9_DOTL_CREATE }, 170 { O_EXCL, P9_DOTL_EXCL }, 171 { O_NOCTTY, P9_DOTL_NOCTTY }, 172 { O_APPEND, P9_DOTL_APPEND }, 173 { O_NONBLOCK, P9_DOTL_NONBLOCK }, 174 { O_DSYNC, P9_DOTL_DSYNC }, 175 { FASYNC, P9_DOTL_FASYNC }, 176 { O_DIRECT, P9_DOTL_DIRECT }, 177 { O_LARGEFILE, P9_DOTL_LARGEFILE }, 178 { O_DIRECTORY, P9_DOTL_DIRECTORY }, 179 { O_NOFOLLOW, P9_DOTL_NOFOLLOW }, 180 { O_NOATIME, P9_DOTL_NOATIME }, 181 { O_CLOEXEC, P9_DOTL_CLOEXEC }, 182 { O_SYNC, P9_DOTL_SYNC}, 183 }; 184 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) { 185 if (flags & dotl_oflag_map[i].open_flag) 186 rflags |= dotl_oflag_map[i].dotl_flag; 187 } 188 return rflags; 189 } 190 191 /** 192 * v9fs_open_to_dotl_flags- convert Linux specific open flags to 193 * plan 9 open flag. 194 * @flags: flags to convert 195 */ 196 int v9fs_open_to_dotl_flags(int flags) 197 { 198 int rflags = 0; 199 200 /* 201 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY 202 * and P9_DOTL_NOACCESS 203 */ 204 rflags |= flags & O_ACCMODE; 205 rflags |= v9fs_mapped_dotl_flags(flags); 206 207 return rflags; 208 } 209 210 /** 211 * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol. 212 * @idmap: The user namespace of the mount 213 * @dir: directory inode that is being created 214 * @dentry: dentry that is being deleted 215 * @omode: create permissions 216 * @excl: True if the file must not yet exist 217 * 218 */ 219 static int 220 v9fs_vfs_create_dotl(struct mnt_idmap *idmap, struct inode *dir, 221 struct dentry *dentry, umode_t omode, bool excl) 222 { 223 return v9fs_vfs_mknod_dotl(idmap, dir, dentry, omode, 0); 224 } 225 226 static int 227 v9fs_vfs_atomic_open_dotl(struct inode *dir, struct dentry *dentry, 228 struct file *file, unsigned int flags, umode_t omode) 229 { 230 int err = 0; 231 kgid_t gid; 232 umode_t mode; 233 int p9_omode = v9fs_open_to_dotl_flags(flags); 234 const unsigned char *name = NULL; 235 struct p9_qid qid; 236 struct inode *inode; 237 struct p9_fid *fid = NULL; 238 struct p9_fid *dfid = NULL, *ofid = NULL; 239 struct v9fs_session_info *v9ses; 240 struct posix_acl *pacl = NULL, *dacl = NULL; 241 242 if (d_in_lookup(dentry)) { 243 struct dentry *res = v9fs_vfs_lookup(dir, dentry, 0); 244 if (res || d_really_is_positive(dentry)) 245 return finish_no_open(file, res); 246 } 247 248 /* Only creates */ 249 if (!(flags & O_CREAT)) 250 return finish_no_open(file, NULL); 251 252 v9ses = v9fs_inode2v9ses(dir); 253 254 name = dentry->d_name.name; 255 p9_debug(P9_DEBUG_VFS, "name:%s flags:0x%x mode:0x%x\n", 256 name, flags, omode); 257 258 dfid = v9fs_parent_fid(dentry); 259 if (IS_ERR(dfid)) { 260 err = PTR_ERR(dfid); 261 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err); 262 goto out; 263 } 264 265 /* clone a fid to use for creation */ 266 ofid = clone_fid(dfid); 267 if (IS_ERR(ofid)) { 268 err = PTR_ERR(ofid); 269 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err); 270 goto out; 271 } 272 273 gid = v9fs_get_fsgid_for_create(dir); 274 275 mode = omode; 276 /* Update mode based on ACL value */ 277 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl); 278 if (err) { 279 p9_debug(P9_DEBUG_VFS, "Failed to get acl values in create %d\n", 280 err); 281 goto out; 282 } 283 284 if ((v9ses->cache & CACHE_WRITEBACK) && (p9_omode & P9_OWRITE)) { 285 p9_omode = (p9_omode & ~(P9_OWRITE | P9_DOTL_APPEND)) | P9_ORDWR; 286 p9_debug(P9_DEBUG_CACHE, 287 "write-only file with writeback enabled, creating w/ O_RDWR\n"); 288 } 289 err = p9_client_create_dotl(ofid, name, p9_omode, mode, gid, &qid); 290 if (err < 0) { 291 p9_debug(P9_DEBUG_VFS, "p9_client_open_dotl failed in create %d\n", 292 err); 293 goto out; 294 } 295 v9fs_invalidate_inode_attr(dir); 296 297 /* instantiate inode and assign the unopened fid to the dentry */ 298 fid = p9_client_walk(dfid, 1, &name, 1); 299 if (IS_ERR(fid)) { 300 err = PTR_ERR(fid); 301 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err); 302 goto out; 303 } 304 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb); 305 if (IS_ERR(inode)) { 306 err = PTR_ERR(inode); 307 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n", err); 308 goto out; 309 } 310 /* Now set the ACL based on the default value */ 311 v9fs_set_create_acl(inode, fid, dacl, pacl); 312 313 v9fs_fid_add(dentry, &fid); 314 d_instantiate(dentry, inode); 315 316 /* Since we are opening a file, assign the open fid to the file */ 317 err = finish_open(file, dentry, generic_file_open); 318 if (err) 319 goto out; 320 file->private_data = ofid; 321 #ifdef CONFIG_9P_FSCACHE 322 if (v9ses->cache & CACHE_FSCACHE) { 323 struct v9fs_inode *v9inode = V9FS_I(inode); 324 fscache_use_cookie(v9fs_inode_cookie(v9inode), 325 file->f_mode & FMODE_WRITE); 326 } 327 #endif 328 v9fs_fid_add_modes(ofid, v9ses->flags, v9ses->cache, flags); 329 v9fs_open_fid_add(inode, &ofid); 330 file->f_mode |= FMODE_CREATED; 331 out: 332 p9_fid_put(dfid); 333 p9_fid_put(ofid); 334 p9_fid_put(fid); 335 v9fs_put_acl(dacl, pacl); 336 return err; 337 } 338 339 /** 340 * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory 341 * @idmap: The idmap of the mount 342 * @dir: inode that is being unlinked 343 * @dentry: dentry that is being unlinked 344 * @omode: mode for new directory 345 * 346 */ 347 348 static struct dentry *v9fs_vfs_mkdir_dotl(struct mnt_idmap *idmap, 349 struct inode *dir, struct dentry *dentry, 350 umode_t omode) 351 { 352 int err = 0; 353 struct v9fs_session_info *v9ses; 354 struct p9_fid *fid = NULL, *dfid = NULL; 355 kgid_t gid; 356 const unsigned char *name; 357 umode_t mode; 358 struct inode *inode; 359 struct p9_qid qid; 360 struct posix_acl *dacl = NULL, *pacl = NULL; 361 362 p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry); 363 v9ses = v9fs_inode2v9ses(dir); 364 365 omode |= S_IFDIR; 366 if (dir->i_mode & S_ISGID) 367 omode |= S_ISGID; 368 369 dfid = v9fs_parent_fid(dentry); 370 if (IS_ERR(dfid)) { 371 err = PTR_ERR(dfid); 372 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err); 373 goto error; 374 } 375 376 gid = v9fs_get_fsgid_for_create(dir); 377 mode = omode; 378 /* Update mode based on ACL value */ 379 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl); 380 if (err) { 381 p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mkdir %d\n", 382 err); 383 goto error; 384 } 385 name = dentry->d_name.name; 386 err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid); 387 if (err < 0) 388 goto error; 389 fid = p9_client_walk(dfid, 1, &name, 1); 390 if (IS_ERR(fid)) { 391 err = PTR_ERR(fid); 392 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", 393 err); 394 goto error; 395 } 396 397 /* instantiate inode and assign the unopened fid to the dentry */ 398 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb); 399 if (IS_ERR(inode)) { 400 err = PTR_ERR(inode); 401 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n", 402 err); 403 goto error; 404 } 405 v9fs_set_create_acl(inode, fid, dacl, pacl); 406 v9fs_fid_add(dentry, &fid); 407 d_instantiate(dentry, inode); 408 err = 0; 409 inc_nlink(dir); 410 v9fs_invalidate_inode_attr(dir); 411 error: 412 p9_fid_put(fid); 413 v9fs_put_acl(dacl, pacl); 414 p9_fid_put(dfid); 415 return err ? ERR_PTR(err) : NULL; 416 } 417 418 static int 419 v9fs_vfs_getattr_dotl(struct mnt_idmap *idmap, 420 const struct path *path, struct kstat *stat, 421 u32 request_mask, unsigned int flags) 422 { 423 struct dentry *dentry = path->dentry; 424 struct v9fs_session_info *v9ses; 425 struct p9_fid *fid; 426 struct inode *inode = d_inode(dentry); 427 struct p9_stat_dotl *st; 428 429 p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry); 430 v9ses = v9fs_dentry2v9ses(dentry); 431 if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) { 432 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat); 433 return 0; 434 } else if (v9ses->cache) { 435 if (S_ISREG(inode->i_mode)) { 436 int retval = filemap_fdatawrite(inode->i_mapping); 437 438 if (retval) 439 p9_debug(P9_DEBUG_ERROR, 440 "flushing writeback during getattr returned %d\n", retval); 441 } 442 } 443 fid = v9fs_fid_lookup(dentry); 444 if (IS_ERR(fid)) 445 return PTR_ERR(fid); 446 447 /* Ask for all the fields in stat structure. Server will return 448 * whatever it supports 449 */ 450 451 st = p9_client_getattr_dotl(fid, P9_STATS_ALL); 452 p9_fid_put(fid); 453 if (IS_ERR(st)) 454 return PTR_ERR(st); 455 456 v9fs_stat2inode_dotl(st, d_inode(dentry), 0); 457 generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(dentry), stat); 458 /* Change block size to what the server returned */ 459 stat->blksize = st->st_blksize; 460 461 kfree(st); 462 return 0; 463 } 464 465 /* 466 * Attribute flags. 467 */ 468 #define P9_ATTR_MODE (1 << 0) 469 #define P9_ATTR_UID (1 << 1) 470 #define P9_ATTR_GID (1 << 2) 471 #define P9_ATTR_SIZE (1 << 3) 472 #define P9_ATTR_ATIME (1 << 4) 473 #define P9_ATTR_MTIME (1 << 5) 474 #define P9_ATTR_CTIME (1 << 6) 475 #define P9_ATTR_ATIME_SET (1 << 7) 476 #define P9_ATTR_MTIME_SET (1 << 8) 477 478 struct dotl_iattr_map { 479 int iattr_valid; 480 int p9_iattr_valid; 481 }; 482 483 static int v9fs_mapped_iattr_valid(int iattr_valid) 484 { 485 int i; 486 int p9_iattr_valid = 0; 487 struct dotl_iattr_map dotl_iattr_map[] = { 488 { ATTR_MODE, P9_ATTR_MODE }, 489 { ATTR_UID, P9_ATTR_UID }, 490 { ATTR_GID, P9_ATTR_GID }, 491 { ATTR_SIZE, P9_ATTR_SIZE }, 492 { ATTR_ATIME, P9_ATTR_ATIME }, 493 { ATTR_MTIME, P9_ATTR_MTIME }, 494 { ATTR_CTIME, P9_ATTR_CTIME }, 495 { ATTR_ATIME_SET, P9_ATTR_ATIME_SET }, 496 { ATTR_MTIME_SET, P9_ATTR_MTIME_SET }, 497 }; 498 for (i = 0; i < ARRAY_SIZE(dotl_iattr_map); i++) { 499 if (iattr_valid & dotl_iattr_map[i].iattr_valid) 500 p9_iattr_valid |= dotl_iattr_map[i].p9_iattr_valid; 501 } 502 return p9_iattr_valid; 503 } 504 505 /** 506 * v9fs_vfs_setattr_dotl - set file metadata 507 * @idmap: idmap of the mount 508 * @dentry: file whose metadata to set 509 * @iattr: metadata assignment structure 510 * 511 */ 512 513 int v9fs_vfs_setattr_dotl(struct mnt_idmap *idmap, 514 struct dentry *dentry, struct iattr *iattr) 515 { 516 int retval, use_dentry = 0; 517 struct inode *inode = d_inode(dentry); 518 struct v9fs_session_info __maybe_unused *v9ses; 519 struct p9_fid *fid = NULL; 520 struct p9_iattr_dotl p9attr = { 521 .uid = INVALID_UID, 522 .gid = INVALID_GID, 523 }; 524 525 p9_debug(P9_DEBUG_VFS, "\n"); 526 527 retval = setattr_prepare(&nop_mnt_idmap, dentry, iattr); 528 if (retval) 529 return retval; 530 531 v9ses = v9fs_dentry2v9ses(dentry); 532 533 p9attr.valid = v9fs_mapped_iattr_valid(iattr->ia_valid); 534 if (iattr->ia_valid & ATTR_MODE) 535 p9attr.mode = iattr->ia_mode; 536 if (iattr->ia_valid & ATTR_UID) 537 p9attr.uid = iattr->ia_uid; 538 if (iattr->ia_valid & ATTR_GID) 539 p9attr.gid = iattr->ia_gid; 540 if (iattr->ia_valid & ATTR_SIZE) 541 p9attr.size = iattr->ia_size; 542 if (iattr->ia_valid & ATTR_ATIME_SET) { 543 p9attr.atime_sec = iattr->ia_atime.tv_sec; 544 p9attr.atime_nsec = iattr->ia_atime.tv_nsec; 545 } 546 if (iattr->ia_valid & ATTR_MTIME_SET) { 547 p9attr.mtime_sec = iattr->ia_mtime.tv_sec; 548 p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec; 549 } 550 551 if (iattr->ia_valid & ATTR_FILE) { 552 fid = iattr->ia_file->private_data; 553 WARN_ON(!fid); 554 } 555 if (!fid) { 556 fid = v9fs_fid_lookup(dentry); 557 use_dentry = 1; 558 } 559 if (IS_ERR(fid)) 560 return PTR_ERR(fid); 561 562 /* Write all dirty data */ 563 if (S_ISREG(inode->i_mode)) { 564 retval = filemap_fdatawrite(inode->i_mapping); 565 if (retval < 0) 566 p9_debug(P9_DEBUG_ERROR, 567 "Flushing file prior to setattr failed: %d\n", retval); 568 } 569 570 retval = p9_client_setattr(fid, &p9attr); 571 if (retval < 0) { 572 if (use_dentry) 573 p9_fid_put(fid); 574 return retval; 575 } 576 577 if ((iattr->ia_valid & ATTR_SIZE) && iattr->ia_size != 578 i_size_read(inode)) { 579 truncate_setsize(inode, iattr->ia_size); 580 netfs_resize_file(netfs_inode(inode), iattr->ia_size, true); 581 582 #ifdef CONFIG_9P_FSCACHE 583 if (v9ses->cache & CACHE_FSCACHE) 584 fscache_resize_cookie(v9fs_inode_cookie(V9FS_I(inode)), 585 iattr->ia_size); 586 #endif 587 } 588 589 v9fs_invalidate_inode_attr(inode); 590 setattr_copy(&nop_mnt_idmap, inode, iattr); 591 mark_inode_dirty(inode); 592 if (iattr->ia_valid & ATTR_MODE) { 593 /* We also want to update ACL when we update mode bits */ 594 retval = v9fs_acl_chmod(inode, fid); 595 if (retval < 0) { 596 if (use_dentry) 597 p9_fid_put(fid); 598 return retval; 599 } 600 } 601 if (use_dentry) 602 p9_fid_put(fid); 603 604 return 0; 605 } 606 607 /** 608 * v9fs_stat2inode_dotl - populate an inode structure with stat info 609 * @stat: stat structure 610 * @inode: inode to populate 611 * @flags: ctrl flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE) 612 * 613 */ 614 615 void 616 v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode, 617 unsigned int flags) 618 { 619 umode_t mode; 620 struct v9fs_inode *v9inode = V9FS_I(inode); 621 622 if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) { 623 inode_set_atime(inode, stat->st_atime_sec, 624 stat->st_atime_nsec); 625 inode_set_mtime(inode, stat->st_mtime_sec, 626 stat->st_mtime_nsec); 627 inode_set_ctime(inode, stat->st_ctime_sec, 628 stat->st_ctime_nsec); 629 inode->i_uid = stat->st_uid; 630 inode->i_gid = stat->st_gid; 631 set_nlink(inode, stat->st_nlink); 632 633 mode = stat->st_mode & S_IALLUGO; 634 mode |= inode->i_mode & ~S_IALLUGO; 635 inode->i_mode = mode; 636 637 spin_lock(&inode->i_lock); 638 netfs_write_remote_i_size(inode, stat->st_size); 639 if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE)) 640 i_size_write(inode, stat->st_size); 641 inode->i_blocks = stat->st_blocks; 642 spin_unlock(&inode->i_lock); 643 } else { 644 if (stat->st_result_mask & P9_STATS_ATIME) { 645 inode_set_atime(inode, stat->st_atime_sec, 646 stat->st_atime_nsec); 647 } 648 if (stat->st_result_mask & P9_STATS_MTIME) { 649 inode_set_mtime(inode, stat->st_mtime_sec, 650 stat->st_mtime_nsec); 651 } 652 if (stat->st_result_mask & P9_STATS_CTIME) { 653 inode_set_ctime(inode, stat->st_ctime_sec, 654 stat->st_ctime_nsec); 655 } 656 if (stat->st_result_mask & P9_STATS_UID) 657 inode->i_uid = stat->st_uid; 658 if (stat->st_result_mask & P9_STATS_GID) 659 inode->i_gid = stat->st_gid; 660 if (stat->st_result_mask & P9_STATS_NLINK) 661 set_nlink(inode, stat->st_nlink); 662 if (stat->st_result_mask & P9_STATS_MODE) { 663 mode = stat->st_mode & S_IALLUGO; 664 mode |= inode->i_mode & ~S_IALLUGO; 665 inode->i_mode = mode; 666 } 667 spin_lock(&inode->i_lock); 668 if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE) && 669 stat->st_result_mask & P9_STATS_SIZE) { 670 netfs_write_remote_i_size(inode, stat->st_size); 671 i_size_write(inode, stat->st_size); 672 } 673 if (stat->st_result_mask & P9_STATS_BLOCKS) 674 inode->i_blocks = stat->st_blocks; 675 spin_unlock(&inode->i_lock); 676 } 677 if (stat->st_result_mask & P9_STATS_GEN) 678 inode->i_generation = stat->st_gen; 679 680 /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION 681 * because the inode structure does not have fields for them. 682 */ 683 v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR; 684 } 685 686 static int 687 v9fs_vfs_symlink_dotl(struct mnt_idmap *idmap, struct inode *dir, 688 struct dentry *dentry, const char *symname) 689 { 690 int err; 691 kgid_t gid; 692 const unsigned char *name; 693 struct v9fs_session_info *v9ses; 694 struct p9_qid qid; 695 struct p9_fid *dfid; 696 struct p9_fid *fid = NULL; 697 struct inode *inode; 698 699 name = dentry->d_name.name; 700 p9_debug(P9_DEBUG_VFS, "%llu,%s,%s\n", dir->i_ino, name, symname); 701 702 dfid = v9fs_parent_fid(dentry); 703 if (IS_ERR(dfid)) { 704 err = PTR_ERR(dfid); 705 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err); 706 return err; 707 } 708 709 gid = v9fs_get_fsgid_for_create(dir); 710 711 /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */ 712 err = p9_client_symlink(dfid, name, symname, gid, &qid); 713 714 if (err < 0) { 715 p9_debug(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err); 716 goto error; 717 } 718 719 v9fs_invalidate_inode_attr(dir); 720 721 /* instantiate inode and assign the unopened fid to the dentry */ 722 fid = p9_client_walk(dfid, 1, &name, 1); 723 if (IS_ERR(fid)) { 724 err = PTR_ERR(fid); 725 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", 726 err); 727 goto error; 728 } 729 730 v9ses = v9fs_inode2v9ses(dir); 731 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb); 732 if (IS_ERR(inode)) { 733 err = PTR_ERR(inode); 734 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n", 735 err); 736 goto error; 737 } 738 v9fs_fid_add(dentry, &fid); 739 d_instantiate(dentry, inode); 740 err = 0; 741 error: 742 p9_fid_put(fid); 743 p9_fid_put(dfid); 744 return err; 745 } 746 747 /** 748 * v9fs_vfs_link_dotl - create a hardlink for dotl 749 * @old_dentry: dentry for file to link to 750 * @dir: inode destination for new link 751 * @dentry: dentry for link 752 * 753 */ 754 755 static int 756 v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir, 757 struct dentry *dentry) 758 { 759 int err; 760 struct p9_fid *dfid, *oldfid; 761 struct v9fs_session_info *v9ses; 762 763 p9_debug(P9_DEBUG_VFS, "dir ino: %llu, old_name: %pd, new_name: %pd\n", 764 dir->i_ino, old_dentry, dentry); 765 766 v9ses = v9fs_inode2v9ses(dir); 767 dfid = v9fs_parent_fid(dentry); 768 if (IS_ERR(dfid)) 769 return PTR_ERR(dfid); 770 771 oldfid = v9fs_fid_lookup(old_dentry); 772 if (IS_ERR(oldfid)) { 773 p9_fid_put(dfid); 774 return PTR_ERR(oldfid); 775 } 776 777 err = p9_client_link(dfid, oldfid, dentry->d_name.name); 778 779 p9_fid_put(dfid); 780 p9_fid_put(oldfid); 781 if (err < 0) { 782 p9_debug(P9_DEBUG_VFS, "p9_client_link failed %d\n", err); 783 return err; 784 } 785 786 v9fs_invalidate_inode_attr(dir); 787 if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) { 788 /* Get the latest stat info from server. */ 789 struct p9_fid *fid; 790 791 fid = v9fs_fid_lookup(old_dentry); 792 if (IS_ERR(fid)) 793 return PTR_ERR(fid); 794 795 v9fs_refresh_inode_dotl(fid, d_inode(old_dentry)); 796 p9_fid_put(fid); 797 } 798 ihold(d_inode(old_dentry)); 799 d_instantiate(dentry, d_inode(old_dentry)); 800 801 return err; 802 } 803 804 /** 805 * v9fs_vfs_mknod_dotl - create a special file 806 * @idmap: The idmap of the mount 807 * @dir: inode destination for new link 808 * @dentry: dentry for file 809 * @omode: mode for creation 810 * @rdev: device associated with special file 811 * 812 */ 813 static int 814 v9fs_vfs_mknod_dotl(struct mnt_idmap *idmap, struct inode *dir, 815 struct dentry *dentry, umode_t omode, dev_t rdev) 816 { 817 int err; 818 kgid_t gid; 819 const unsigned char *name; 820 umode_t mode; 821 struct v9fs_session_info *v9ses; 822 struct p9_fid *fid = NULL, *dfid = NULL; 823 struct inode *inode; 824 struct p9_qid qid; 825 struct posix_acl *dacl = NULL, *pacl = NULL; 826 827 p9_debug(P9_DEBUG_VFS, " %llu,%pd mode: %x MAJOR: %u MINOR: %u\n", 828 dir->i_ino, dentry, omode, 829 MAJOR(rdev), MINOR(rdev)); 830 831 v9ses = v9fs_inode2v9ses(dir); 832 dfid = v9fs_parent_fid(dentry); 833 if (IS_ERR(dfid)) { 834 err = PTR_ERR(dfid); 835 p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err); 836 goto error; 837 } 838 839 gid = v9fs_get_fsgid_for_create(dir); 840 mode = omode; 841 /* Update mode based on ACL value */ 842 err = v9fs_acl_mode(dir, &mode, &dacl, &pacl); 843 if (err) { 844 p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mknod %d\n", 845 err); 846 goto error; 847 } 848 name = dentry->d_name.name; 849 850 err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid); 851 if (err < 0) 852 goto error; 853 854 v9fs_invalidate_inode_attr(dir); 855 fid = p9_client_walk(dfid, 1, &name, 1); 856 if (IS_ERR(fid)) { 857 err = PTR_ERR(fid); 858 p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", 859 err); 860 goto error; 861 } 862 inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb); 863 if (IS_ERR(inode)) { 864 err = PTR_ERR(inode); 865 p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n", 866 err); 867 goto error; 868 } 869 v9fs_set_create_acl(inode, fid, dacl, pacl); 870 v9fs_fid_add(dentry, &fid); 871 d_instantiate(dentry, inode); 872 err = 0; 873 error: 874 p9_fid_put(fid); 875 v9fs_put_acl(dacl, pacl); 876 p9_fid_put(dfid); 877 878 return err; 879 } 880 881 /** 882 * v9fs_vfs_get_link_nocache_dotl - Resolve a symlink directly. 883 * 884 * To be used when symlink caching is not enabled. 885 * 886 * @dentry: dentry for symlink 887 * @inode: inode for symlink 888 * @done: destructor for return value 889 */ 890 static const char * 891 v9fs_vfs_get_link_nocache_dotl(struct dentry *dentry, 892 struct inode *inode, 893 struct delayed_call *done) 894 { 895 struct p9_fid *fid; 896 char *target; 897 int retval; 898 899 if (!dentry) 900 return ERR_PTR(-ECHILD); 901 902 p9_debug(P9_DEBUG_VFS, "%pd\n", dentry); 903 904 fid = v9fs_fid_lookup(dentry); 905 if (IS_ERR(fid)) 906 return ERR_CAST(fid); 907 retval = p9_client_readlink(fid, &target); 908 p9_fid_put(fid); 909 if (retval) 910 return ERR_PTR(retval); 911 set_delayed_call(done, kfree_link, target); 912 return target; 913 } 914 915 /** 916 * v9fs_vfs_get_link_dotl - follow a symlink path 917 * @dentry: dentry for symlink 918 * @inode: inode for symlink 919 * @done: destructor for return value 920 */ 921 static const char * 922 v9fs_vfs_get_link_dotl(struct dentry *dentry, 923 struct inode *inode, 924 struct delayed_call *done) 925 { 926 struct v9fs_session_info *v9ses; 927 928 v9ses = v9fs_inode2v9ses(inode); 929 if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) 930 return page_get_link(dentry, inode, done); 931 932 return v9fs_vfs_get_link_nocache_dotl(dentry, inode, done); 933 } 934 935 int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode) 936 { 937 struct p9_stat_dotl *st; 938 struct v9fs_session_info *v9ses; 939 unsigned int flags; 940 941 v9ses = v9fs_inode2v9ses(inode); 942 st = p9_client_getattr_dotl(fid, P9_STATS_ALL); 943 if (IS_ERR(st)) 944 return PTR_ERR(st); 945 /* 946 * Don't update inode if the file type is different 947 */ 948 if (inode_wrong_type(inode, st->st_mode)) 949 goto out; 950 951 /* 952 * We don't want to refresh inode->i_size, 953 * because we may have cached data 954 */ 955 flags = (v9ses->cache & CACHE_LOOSE) ? 956 V9FS_STAT2INODE_KEEP_ISIZE : 0; 957 v9fs_stat2inode_dotl(st, inode, flags); 958 out: 959 kfree(st); 960 return 0; 961 } 962 963 const struct inode_operations v9fs_dir_inode_operations_dotl = { 964 .create = v9fs_vfs_create_dotl, 965 .atomic_open = v9fs_vfs_atomic_open_dotl, 966 .lookup = v9fs_vfs_lookup, 967 .link = v9fs_vfs_link_dotl, 968 .symlink = v9fs_vfs_symlink_dotl, 969 .unlink = v9fs_vfs_unlink, 970 .mkdir = v9fs_vfs_mkdir_dotl, 971 .rmdir = v9fs_vfs_rmdir, 972 .mknod = v9fs_vfs_mknod_dotl, 973 .rename = v9fs_vfs_rename, 974 .getattr = v9fs_vfs_getattr_dotl, 975 .setattr = v9fs_vfs_setattr_dotl, 976 .listxattr = v9fs_listxattr, 977 .get_inode_acl = v9fs_iop_get_inode_acl, 978 .get_acl = v9fs_iop_get_acl, 979 .set_acl = v9fs_iop_set_acl, 980 }; 981 982 const struct inode_operations v9fs_file_inode_operations_dotl = { 983 .getattr = v9fs_vfs_getattr_dotl, 984 .setattr = v9fs_vfs_setattr_dotl, 985 .listxattr = v9fs_listxattr, 986 .get_inode_acl = v9fs_iop_get_inode_acl, 987 .get_acl = v9fs_iop_get_acl, 988 .set_acl = v9fs_iop_set_acl, 989 }; 990 991 const struct inode_operations v9fs_symlink_inode_operations_dotl = { 992 .get_link = v9fs_vfs_get_link_dotl, 993 .getattr = v9fs_vfs_getattr_dotl, 994 .setattr = v9fs_vfs_setattr_dotl, 995 .listxattr = v9fs_listxattr, 996 }; 997