1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) Neil Brown 2002 4 * Copyright (C) Christoph Hellwig 2007 5 * 6 * This file contains the code mapping from inodes to NFS file handles, 7 * and for mapping back from file handles to dentries. 8 * 9 * For details on why we do all the strange and hairy things in here 10 * take a look at Documentation/filesystems/nfs/exporting.rst. 11 */ 12 #include <linux/exportfs.h> 13 #include <linux/fs.h> 14 #include <linux/file.h> 15 #include <linux/module.h> 16 #include <linux/mount.h> 17 #include <linux/namei.h> 18 #include <linux/sched.h> 19 #include <linux/cred.h> 20 21 #define dprintk(fmt, args...) pr_debug(fmt, ##args) 22 23 24 static int get_name(const struct path *path, char *name, struct dentry *child); 25 26 27 static int exportfs_get_name(struct vfsmount *mnt, struct dentry *dir, 28 char *name, struct dentry *child) 29 { 30 const struct export_operations *nop = dir->d_sb->s_export_op; 31 struct path path = {.mnt = mnt, .dentry = dir}; 32 33 if (nop->get_name) 34 return nop->get_name(dir, name, child); 35 else 36 return get_name(&path, name, child); 37 } 38 39 /* 40 * Check if the dentry or any of it's aliases is acceptable. 41 */ 42 static struct dentry * 43 find_acceptable_alias(struct dentry *result, 44 int (*acceptable)(void *context, struct dentry *dentry), 45 void *context) 46 { 47 struct dentry *dentry, *toput = NULL; 48 struct inode *inode; 49 50 if (acceptable(context, result)) 51 return result; 52 53 inode = result->d_inode; 54 spin_lock(&inode->i_lock); 55 for_each_alias(dentry, inode) { 56 if (!dget_alias_ilocked(dentry)) 57 continue; 58 spin_unlock(&inode->i_lock); 59 dput(toput); 60 if (dentry != result && acceptable(context, dentry)) { 61 dput(result); 62 return dentry; 63 } 64 spin_lock(&inode->i_lock); 65 toput = dentry; 66 } 67 spin_unlock(&inode->i_lock); 68 69 dput(toput); 70 return NULL; 71 } 72 73 static bool dentry_connected(struct dentry *dentry) 74 { 75 dget(dentry); 76 while (dentry->d_flags & DCACHE_DISCONNECTED) { 77 struct dentry *parent = dget_parent(dentry); 78 79 dput(dentry); 80 if (dentry == parent) { 81 dput(parent); 82 return false; 83 } 84 dentry = parent; 85 } 86 dput(dentry); 87 return true; 88 } 89 90 static void clear_disconnected(struct dentry *dentry) 91 { 92 dget(dentry); 93 while (dentry->d_flags & DCACHE_DISCONNECTED) { 94 struct dentry *parent = dget_parent(dentry); 95 96 WARN_ON_ONCE(IS_ROOT(dentry)); 97 98 spin_lock(&dentry->d_lock); 99 dentry->d_flags &= ~DCACHE_DISCONNECTED; 100 spin_unlock(&dentry->d_lock); 101 102 dput(dentry); 103 dentry = parent; 104 } 105 dput(dentry); 106 } 107 108 /* 109 * Reconnect a directory dentry with its parent. 110 * 111 * This can return a dentry, or NULL, or an error. 112 * 113 * In the first case the returned dentry is the parent of the given 114 * dentry, and may itself need to be reconnected to its parent. 115 * 116 * In the NULL case, a concurrent VFS operation has either renamed or 117 * removed this directory. The concurrent operation has reconnected our 118 * dentry, so we no longer need to. 119 */ 120 static struct dentry *reconnect_one(struct vfsmount *mnt, 121 struct dentry *dentry, char *nbuf) 122 { 123 struct dentry *parent; 124 struct dentry *tmp; 125 int err; 126 127 parent = ERR_PTR(-EACCES); 128 if (mnt->mnt_sb->s_export_op->get_parent) 129 parent = mnt->mnt_sb->s_export_op->get_parent(dentry); 130 131 if (IS_ERR(parent)) { 132 dprintk("get_parent of %llu failed, err %ld\n", 133 dentry->d_inode->i_ino, PTR_ERR(parent)); 134 return parent; 135 } 136 137 dprintk("%s: find name of %llu in %llu\n", __func__, 138 dentry->d_inode->i_ino, parent->d_inode->i_ino); 139 err = exportfs_get_name(mnt, parent, nbuf, dentry); 140 if (err == -ENOENT) 141 goto out_reconnected; 142 if (err) 143 goto out_err; 144 dprintk("%s: found name: %s\n", __func__, nbuf); 145 tmp = lookup_one_unlocked(mnt_idmap(mnt), &QSTR(nbuf), parent); 146 if (IS_ERR(tmp)) { 147 dprintk("lookup failed: %ld\n", PTR_ERR(tmp)); 148 err = PTR_ERR(tmp); 149 goto out_err; 150 } 151 if (tmp != dentry) { 152 /* 153 * Somebody has renamed it since exportfs_get_name(); 154 * great, since it could've only been renamed if it 155 * got looked up and thus connected, and it would 156 * remain connected afterwards. We are done. 157 */ 158 dput(tmp); 159 goto out_reconnected; 160 } 161 dput(tmp); 162 if (IS_ROOT(dentry)) { 163 err = -ESTALE; 164 goto out_err; 165 } 166 return parent; 167 168 out_err: 169 dput(parent); 170 return ERR_PTR(err); 171 out_reconnected: 172 dput(parent); 173 /* 174 * Someone must have renamed our entry into another parent, in 175 * which case it has been reconnected by the rename. 176 * 177 * Or someone removed it entirely, in which case filehandle 178 * lookup will succeed but the directory is now IS_DEAD and 179 * subsequent operations on it will fail. 180 * 181 * Alternatively, maybe there was no race at all, and the 182 * filesystem is just corrupt and gave us a parent that doesn't 183 * actually contain any entry pointing to this inode. So, 184 * double check that this worked and return -ESTALE if not: 185 */ 186 if (!dentry_connected(dentry)) 187 return ERR_PTR(-ESTALE); 188 return NULL; 189 } 190 191 /* 192 * Make sure target_dir is fully connected to the dentry tree. 193 * 194 * On successful return, DCACHE_DISCONNECTED will be cleared on 195 * target_dir, and target_dir->d_parent->...->d_parent will reach the 196 * root of the filesystem. 197 * 198 * Whenever DCACHE_DISCONNECTED is unset, target_dir is fully connected. 199 * But the converse is not true: target_dir may have DCACHE_DISCONNECTED 200 * set but already be connected. In that case we'll verify the 201 * connection to root and then clear the flag. 202 * 203 * Note that target_dir could be removed by a concurrent operation. In 204 * that case reconnect_path may still succeed with target_dir fully 205 * connected, but further operations using the filehandle will fail when 206 * necessary (due to S_DEAD being set on the directory). 207 */ 208 static int 209 reconnect_path(struct vfsmount *mnt, struct dentry *target_dir, char *nbuf) 210 { 211 struct dentry *dentry, *parent; 212 213 dentry = dget(target_dir); 214 215 while (dentry->d_flags & DCACHE_DISCONNECTED) { 216 BUG_ON(dentry == mnt->mnt_sb->s_root); 217 218 if (IS_ROOT(dentry)) 219 parent = reconnect_one(mnt, dentry, nbuf); 220 else 221 parent = dget_parent(dentry); 222 223 if (!parent) 224 break; 225 dput(dentry); 226 if (IS_ERR(parent)) 227 return PTR_ERR(parent); 228 dentry = parent; 229 } 230 dput(dentry); 231 clear_disconnected(target_dir); 232 return 0; 233 } 234 235 struct getdents_callback { 236 struct dir_context ctx; 237 char *name; /* name that was found. It already points to a 238 buffer NAME_MAX+1 is size */ 239 u64 ino; /* the inum we are looking for */ 240 int found; /* inode matched? */ 241 int sequence; /* sequence counter */ 242 }; 243 244 /* 245 * A rather strange filldir function to capture 246 * the name matching the specified inode number. 247 */ 248 static bool filldir_one(struct dir_context *ctx, const char *name, int len, 249 loff_t pos, u64 ino, unsigned int d_type) 250 { 251 struct getdents_callback *buf = 252 container_of(ctx, struct getdents_callback, ctx); 253 254 buf->sequence++; 255 if (buf->ino == ino && len <= NAME_MAX && 256 !name_is_dot_dotdot(name, len)) { 257 memcpy(buf->name, name, len); 258 buf->name[len] = '\0'; 259 buf->found = 1; 260 return false; // no more 261 } 262 return true; 263 } 264 265 /** 266 * get_name - default export_operations->get_name function 267 * @path: the directory in which to find a name 268 * @name: a pointer to a %NAME_MAX+1 char buffer to store the name 269 * @child: the dentry for the child directory. 270 * 271 * calls readdir on the parent until it finds an entry with 272 * the same inode number as the child, and returns that. 273 */ 274 static int get_name(const struct path *path, char *name, struct dentry *child) 275 { 276 const struct cred *cred = current_cred(); 277 struct inode *dir = path->dentry->d_inode; 278 int error; 279 struct file *file; 280 struct kstat stat; 281 struct path child_path = { 282 .mnt = path->mnt, 283 .dentry = child, 284 }; 285 struct getdents_callback buffer = { 286 .ctx.actor = filldir_one, 287 .ctx.count = INT_MAX, 288 .name = name, 289 }; 290 291 error = -ENOTDIR; 292 if (!dir || !S_ISDIR(dir->i_mode)) 293 goto out; 294 error = -EINVAL; 295 if (!dir->i_fop) 296 goto out; 297 /* 298 * inode->i_ino is unsigned long, kstat->ino is u64, so the 299 * former would be insufficient on 32-bit hosts when the 300 * filesystem supports 64-bit inode numbers. So we need to 301 * actually call ->getattr, not just read i_ino: 302 */ 303 error = vfs_getattr_nosec(&child_path, &stat, 304 STATX_INO, AT_STATX_SYNC_AS_STAT); 305 if (error) 306 return error; 307 buffer.ino = stat.ino; 308 /* 309 * Open the directory ... 310 */ 311 file = dentry_open(path, O_RDONLY, cred); 312 error = PTR_ERR(file); 313 if (IS_ERR(file)) 314 goto out; 315 316 error = -EINVAL; 317 if (!file->f_op->iterate_shared) 318 goto out_close; 319 320 buffer.sequence = 0; 321 while (1) { 322 int old_seq = buffer.sequence; 323 324 error = iterate_dir(file, &buffer.ctx); 325 if (buffer.found) { 326 error = 0; 327 break; 328 } 329 330 if (error < 0) 331 break; 332 333 error = -ENOENT; 334 if (old_seq == buffer.sequence) 335 break; 336 } 337 338 out_close: 339 /* 340 * @file is the read-only directory opened above, so __fput() 341 * cannot block on anything the caller holds. 342 */ 343 __fput_sync(file); 344 out: 345 return error; 346 } 347 348 #define FILEID_INO64_GEN_LEN 3 349 350 /** 351 * exportfs_encode_ino64_fid - encode non-decodeable 64bit ino file id 352 * @inode: the object to encode 353 * @fid: where to store the file handle fragment 354 * @max_len: maximum length to store there (in 4 byte units) 355 * 356 * This generic function is used to encode a non-decodeable file id for 357 * fanotify for filesystems that do not support NFS export. 358 */ 359 static int exportfs_encode_ino64_fid(struct inode *inode, struct fid *fid, 360 int *max_len) 361 { 362 if (*max_len < FILEID_INO64_GEN_LEN) { 363 *max_len = FILEID_INO64_GEN_LEN; 364 return FILEID_INVALID; 365 } 366 367 fid->i64.ino = inode->i_ino; 368 fid->i64.gen = inode->i_generation; 369 *max_len = FILEID_INO64_GEN_LEN; 370 371 return FILEID_INO64_GEN; 372 } 373 374 /** 375 * exportfs_encode_inode_fh - encode a file handle from inode 376 * @inode: the object to encode 377 * @fid: where to store the file handle fragment 378 * @max_len: maximum length to store there 379 * @parent: parent directory inode, if wanted 380 * @flags: properties of the requested file handle 381 * 382 * Returns an enum fid_type or a negative errno. 383 */ 384 int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid, 385 int *max_len, struct inode *parent, int flags) 386 { 387 const struct export_operations *nop = inode->i_sb->s_export_op; 388 enum fid_type type; 389 390 if (!exportfs_can_encode_fh(nop, flags)) 391 return -EOPNOTSUPP; 392 393 if (!nop && (flags & EXPORT_FH_FID)) 394 type = exportfs_encode_ino64_fid(inode, fid, max_len); 395 else 396 type = nop->encode_fh(inode, fid->raw, max_len, parent); 397 398 if (type > 0 && FILEID_USER_FLAGS(type)) { 399 pr_warn_once("%s: unexpected fh type value 0x%x from fstype %s.\n", 400 __func__, type, inode->i_sb->s_type->name); 401 return -EINVAL; 402 } 403 404 return type; 405 406 } 407 EXPORT_SYMBOL_GPL(exportfs_encode_inode_fh); 408 409 /** 410 * exportfs_encode_fh - encode a file handle from dentry 411 * @dentry: the object to encode 412 * @fid: where to store the file handle fragment 413 * @max_len: maximum length to store there 414 * @flags: properties of the requested file handle 415 * 416 * Returns an enum fid_type or a negative errno. 417 */ 418 int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len, 419 int flags) 420 { 421 int error; 422 struct dentry *p = NULL; 423 struct inode *inode = dentry->d_inode, *parent = NULL; 424 425 if ((flags & EXPORT_FH_CONNECTABLE) && !S_ISDIR(inode->i_mode)) { 426 p = dget_parent(dentry); 427 /* 428 * note that while p might've ceased to be our parent already, 429 * it's still pinned by and still positive. 430 */ 431 parent = p->d_inode; 432 } 433 434 error = exportfs_encode_inode_fh(inode, fid, max_len, parent, flags); 435 dput(p); 436 437 return error; 438 } 439 EXPORT_SYMBOL_GPL(exportfs_encode_fh); 440 441 struct dentry * 442 exportfs_decode_fh_raw(struct vfsmount *mnt, struct fid *fid, int fh_len, 443 int fileid_type, unsigned int flags, 444 int (*acceptable)(void *, struct dentry *), 445 void *context) 446 { 447 const struct export_operations *nop = mnt->mnt_sb->s_export_op; 448 struct dentry *result, *alias; 449 char nbuf[NAME_MAX+1]; 450 int err; 451 452 if (fileid_type < 0 || FILEID_USER_FLAGS(fileid_type)) 453 return ERR_PTR(-EINVAL); 454 455 /* 456 * Try to get any dentry for the given file handle from the filesystem. 457 */ 458 if (!exportfs_can_decode_fh(nop)) 459 return ERR_PTR(-ESTALE); 460 result = nop->fh_to_dentry(mnt->mnt_sb, fid, fh_len, fileid_type); 461 if (IS_ERR_OR_NULL(result)) 462 return result; 463 464 if ((flags & EXPORT_FH_DIR_ONLY) && !d_is_dir(result)) { 465 err = -ENOTDIR; 466 goto err_result; 467 } 468 469 /* 470 * If no acceptance criteria was specified by caller, a disconnected 471 * dentry is also accepatable. Callers may use this mode to query if 472 * file handle is stale or to get a reference to an inode without 473 * risking the high overhead caused by directory reconnect. 474 */ 475 if (!acceptable) 476 return result; 477 478 if (d_is_dir(result)) { 479 /* 480 * This request is for a directory. 481 * 482 * On the positive side there is only one dentry for each 483 * directory inode. On the negative side this implies that we 484 * to ensure our dentry is connected all the way up to the 485 * filesystem root. 486 */ 487 if (result->d_flags & DCACHE_DISCONNECTED) { 488 err = reconnect_path(mnt, result, nbuf); 489 if (err) 490 goto err_result; 491 } 492 493 if (!acceptable(context, result)) { 494 err = -EACCES; 495 goto err_result; 496 } 497 498 return result; 499 } else { 500 /* 501 * It's not a directory. Life is a little more complicated. 502 */ 503 struct dentry *target_dir, *nresult; 504 505 /* 506 * See if either the dentry we just got from the filesystem 507 * or any alias for it is acceptable. This is always true 508 * if this filesystem is exported without the subtreecheck 509 * option. If the filesystem is exported with the subtree 510 * check option there's a fair chance we need to look at 511 * the parent directory in the file handle and make sure 512 * it's connected to the filesystem root. 513 */ 514 alias = find_acceptable_alias(result, acceptable, context); 515 if (alias) 516 return alias; 517 518 /* 519 * Try to extract a dentry for the parent directory from the 520 * file handle. If this fails we'll have to give up. 521 */ 522 err = -ESTALE; 523 if (!nop->fh_to_parent) 524 goto err_result; 525 526 target_dir = nop->fh_to_parent(mnt->mnt_sb, fid, 527 fh_len, fileid_type); 528 if (!target_dir) 529 goto err_result; 530 err = PTR_ERR(target_dir); 531 if (IS_ERR(target_dir)) 532 goto err_result; 533 534 /* 535 * And as usual we need to make sure the parent directory is 536 * connected to the filesystem root. The VFS really doesn't 537 * like disconnected directories.. 538 */ 539 err = reconnect_path(mnt, target_dir, nbuf); 540 if (err) { 541 dput(target_dir); 542 goto err_result; 543 } 544 545 /* 546 * Now that we've got both a well-connected parent and a 547 * dentry for the inode we're after, make sure that our 548 * inode is actually connected to the parent. 549 */ 550 err = exportfs_get_name(mnt, target_dir, nbuf, result); 551 if (err) { 552 dput(target_dir); 553 goto err_result; 554 } 555 556 nresult = lookup_one_unlocked(mnt_idmap(mnt), &QSTR(nbuf), target_dir); 557 if (!IS_ERR(nresult)) { 558 if (unlikely(nresult->d_inode != result->d_inode)) { 559 dput(nresult); 560 nresult = ERR_PTR(-ESTALE); 561 } 562 } 563 /* 564 * At this point we are done with the parent, but it's pinned 565 * by the child dentry anyway. 566 */ 567 dput(target_dir); 568 569 if (IS_ERR(nresult)) { 570 err = PTR_ERR(nresult); 571 goto err_result; 572 } 573 dput(result); 574 result = nresult; 575 576 /* 577 * And finally make sure the dentry is actually acceptable 578 * to NFSD. 579 */ 580 alias = find_acceptable_alias(result, acceptable, context); 581 if (!alias) { 582 err = -EACCES; 583 goto err_result; 584 } 585 586 return alias; 587 } 588 589 err_result: 590 dput(result); 591 return ERR_PTR(err); 592 } 593 EXPORT_SYMBOL_GPL(exportfs_decode_fh_raw); 594 595 struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid, 596 int fh_len, int fileid_type, 597 int (*acceptable)(void *, struct dentry *), 598 void *context) 599 { 600 struct dentry *ret; 601 602 ret = exportfs_decode_fh_raw(mnt, fid, fh_len, fileid_type, 0, 603 acceptable, context); 604 if (IS_ERR_OR_NULL(ret)) { 605 if (ret == ERR_PTR(-ENOMEM)) 606 return ret; 607 return ERR_PTR(-ESTALE); 608 } 609 return ret; 610 } 611 EXPORT_SYMBOL_GPL(exportfs_decode_fh); 612 613 MODULE_DESCRIPTION("Code mapping from inodes to file handles"); 614 MODULE_LICENSE("GPL"); 615