PageRenderTime 67ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/namei.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t
C | 3411 lines | 2426 code | 405 blank | 580 comment | 587 complexity | fb2fd2e2ce1cc24bb0eb80bc14eb23bf MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. /*
  2. * linux/fs/namei.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * Some corrections by tytso.
  8. */
  9. /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
  10. * lookup logic.
  11. */
  12. /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/fs.h>
  18. #include <linux/namei.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/fsnotify.h>
  21. #include <linux/personality.h>
  22. #include <linux/security.h>
  23. #include <linux/ima.h>
  24. #include <linux/syscalls.h>
  25. #include <linux/mount.h>
  26. #include <linux/audit.h>
  27. #include <linux/capability.h>
  28. #include <linux/file.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/device_cgroup.h>
  31. #include <linux/fs_struct.h>
  32. #include <linux/posix_acl.h>
  33. #include <asm/uaccess.h>
  34. #include "internal.h"
  35. /* [Feb-1997 T. Schoebel-Theuer]
  36. * Fundamental changes in the pathname lookup mechanisms (namei)
  37. * were necessary because of omirr. The reason is that omirr needs
  38. * to know the _real_ pathname, not the user-supplied one, in case
  39. * of symlinks (and also when transname replacements occur).
  40. *
  41. * The new code replaces the old recursive symlink resolution with
  42. * an iterative one (in case of non-nested symlink chains). It does
  43. * this with calls to <fs>_follow_link().
  44. * As a side effect, dir_namei(), _namei() and follow_link() are now
  45. * replaced with a single function lookup_dentry() that can handle all
  46. * the special cases of the former code.
  47. *
  48. * With the new dcache, the pathname is stored at each inode, at least as
  49. * long as the refcount of the inode is positive. As a side effect, the
  50. * size of the dcache depends on the inode cache and thus is dynamic.
  51. *
  52. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  53. * resolution to correspond with current state of the code.
  54. *
  55. * Note that the symlink resolution is not *completely* iterative.
  56. * There is still a significant amount of tail- and mid- recursion in
  57. * the algorithm. Also, note that <fs>_readlink() is not used in
  58. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  59. * may return different results than <fs>_follow_link(). Many virtual
  60. * filesystems (including /proc) exhibit this behavior.
  61. */
  62. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  63. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  64. * and the name already exists in form of a symlink, try to create the new
  65. * name indicated by the symlink. The old code always complained that the
  66. * name already exists, due to not following the symlink even if its target
  67. * is nonexistent. The new semantics affects also mknod() and link() when
  68. * the name is a symlink pointing to a non-existent name.
  69. *
  70. * I don't know which semantics is the right one, since I have no access
  71. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  72. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  73. * "old" one. Personally, I think the new semantics is much more logical.
  74. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  75. * file does succeed in both HP-UX and SunOs, but not in Solaris
  76. * and in the old Linux semantics.
  77. */
  78. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  79. * semantics. See the comments in "open_namei" and "do_link" below.
  80. *
  81. * [10-Sep-98 Alan Modra] Another symlink change.
  82. */
  83. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  84. * inside the path - always follow.
  85. * in the last component in creation/removal/renaming - never follow.
  86. * if LOOKUP_FOLLOW passed - follow.
  87. * if the pathname has trailing slashes - follow.
  88. * otherwise - don't follow.
  89. * (applied in that order).
  90. *
  91. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  92. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  93. * During the 2.4 we need to fix the userland stuff depending on it -
  94. * hopefully we will be able to get rid of that wart in 2.5. So far only
  95. * XEmacs seems to be relying on it...
  96. */
  97. /*
  98. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  99. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  100. * any extra contention...
  101. */
  102. /* In order to reduce some races, while at the same time doing additional
  103. * checking and hopefully speeding things up, we copy filenames to the
  104. * kernel data space before using them..
  105. *
  106. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  107. * PATH_MAX includes the nul terminator --RR.
  108. */
  109. static int do_getname(const char __user *filename, char *page)
  110. {
  111. int retval;
  112. unsigned long len = PATH_MAX;
  113. if (!segment_eq(get_fs(), KERNEL_DS)) {
  114. if ((unsigned long) filename >= TASK_SIZE)
  115. return -EFAULT;
  116. if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
  117. len = TASK_SIZE - (unsigned long) filename;
  118. }
  119. retval = strncpy_from_user(page, filename, len);
  120. if (retval > 0) {
  121. if (retval < len)
  122. return 0;
  123. return -ENAMETOOLONG;
  124. } else if (!retval)
  125. retval = -ENOENT;
  126. return retval;
  127. }
  128. static char *getname_flags(const char __user *filename, int flags, int *empty)
  129. {
  130. char *tmp, *result;
  131. result = ERR_PTR(-ENOMEM);
  132. tmp = __getname();
  133. if (tmp) {
  134. int retval = do_getname(filename, tmp);
  135. result = tmp;
  136. if (retval < 0) {
  137. if (retval == -ENOENT && empty)
  138. *empty = 1;
  139. if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
  140. __putname(tmp);
  141. result = ERR_PTR(retval);
  142. }
  143. }
  144. }
  145. audit_getname(result);
  146. return result;
  147. }
  148. char *getname(const char __user * filename)
  149. {
  150. return getname_flags(filename, 0, 0);
  151. }
  152. #ifdef CONFIG_AUDITSYSCALL
  153. void putname(const char *name)
  154. {
  155. if (unlikely(!audit_dummy_context()))
  156. audit_putname(name);
  157. else
  158. __putname(name);
  159. }
  160. EXPORT_SYMBOL(putname);
  161. #endif
  162. static int check_acl(struct inode *inode, int mask)
  163. {
  164. #ifdef CONFIG_FS_POSIX_ACL
  165. struct posix_acl *acl;
  166. if (mask & MAY_NOT_BLOCK) {
  167. acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
  168. if (!acl)
  169. return -EAGAIN;
  170. /* no ->get_acl() calls in RCU mode... */
  171. if (acl == ACL_NOT_CACHED)
  172. return -ECHILD;
  173. return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
  174. }
  175. acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
  176. /*
  177. * A filesystem can force a ACL callback by just never filling the
  178. * ACL cache. But normally you'd fill the cache either at inode
  179. * instantiation time, or on the first ->get_acl call.
  180. *
  181. * If the filesystem doesn't have a get_acl() function at all, we'll
  182. * just create the negative cache entry.
  183. */
  184. if (acl == ACL_NOT_CACHED) {
  185. if (inode->i_op->get_acl) {
  186. acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
  187. if (IS_ERR(acl))
  188. return PTR_ERR(acl);
  189. } else {
  190. set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
  191. return -EAGAIN;
  192. }
  193. }
  194. if (acl) {
  195. int error = posix_acl_permission(inode, acl, mask);
  196. posix_acl_release(acl);
  197. return error;
  198. }
  199. #endif
  200. return -EAGAIN;
  201. }
  202. /*
  203. * This does basic POSIX ACL permission checking
  204. */
  205. static int acl_permission_check(struct inode *inode, int mask)
  206. {
  207. unsigned int mode = inode->i_mode;
  208. mask &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
  209. if (current_user_ns() != inode_userns(inode))
  210. goto other_perms;
  211. if (likely(current_fsuid() == inode->i_uid))
  212. mode >>= 6;
  213. else {
  214. if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
  215. int error = check_acl(inode, mask);
  216. if (error != -EAGAIN)
  217. return error;
  218. }
  219. if (in_group_p(inode->i_gid))
  220. mode >>= 3;
  221. }
  222. other_perms:
  223. /*
  224. * If the DACs are ok we don't need any capability check.
  225. */
  226. if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  227. return 0;
  228. return -EACCES;
  229. }
  230. /**
  231. * generic_permission - check for access rights on a Posix-like filesystem
  232. * @inode: inode to check access rights for
  233. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  234. *
  235. * Used to check for read/write/execute permissions on a file.
  236. * We use "fsuid" for this, letting us set arbitrary permissions
  237. * for filesystem access without changing the "normal" uids which
  238. * are used for other things.
  239. *
  240. * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
  241. * request cannot be satisfied (eg. requires blocking or too much complexity).
  242. * It would then be called again in ref-walk mode.
  243. */
  244. int generic_permission(struct inode *inode, int mask)
  245. {
  246. int ret;
  247. /*
  248. * Do the basic POSIX ACL permission checks.
  249. */
  250. ret = acl_permission_check(inode, mask);
  251. if (ret != -EACCES)
  252. return ret;
  253. if (S_ISDIR(inode->i_mode)) {
  254. /* DACs are overridable for directories */
  255. if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
  256. return 0;
  257. if (!(mask & MAY_WRITE))
  258. if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
  259. return 0;
  260. return -EACCES;
  261. }
  262. /*
  263. * Read/write DACs are always overridable.
  264. * Executable DACs are overridable when there is
  265. * at least one exec bit set.
  266. */
  267. if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
  268. if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
  269. return 0;
  270. /*
  271. * Searching includes executable on directories, else just read.
  272. */
  273. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  274. if (mask == MAY_READ)
  275. if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
  276. return 0;
  277. return -EACCES;
  278. }
  279. /*
  280. * We _really_ want to just do "generic_permission()" without
  281. * even looking at the inode->i_op values. So we keep a cache
  282. * flag in inode->i_opflags, that says "this has not special
  283. * permission function, use the fast case".
  284. */
  285. static inline int do_inode_permission(struct inode *inode, int mask)
  286. {
  287. if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
  288. if (likely(inode->i_op->permission))
  289. return inode->i_op->permission(inode, mask);
  290. /* This gets set once for the inode lifetime */
  291. spin_lock(&inode->i_lock);
  292. inode->i_opflags |= IOP_FASTPERM;
  293. spin_unlock(&inode->i_lock);
  294. }
  295. return generic_permission(inode, mask);
  296. }
  297. /**
  298. * inode_permission - check for access rights to a given inode
  299. * @inode: inode to check permission on
  300. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  301. *
  302. * Used to check for read/write/execute permissions on an inode.
  303. * We use "fsuid" for this, letting us set arbitrary permissions
  304. * for filesystem access without changing the "normal" uids which
  305. * are used for other things.
  306. */
  307. int inode_permission(struct inode *inode, int mask)
  308. {
  309. int retval;
  310. if (unlikely(mask & MAY_WRITE)) {
  311. umode_t mode = inode->i_mode;
  312. /*
  313. * Nobody gets write access to a read-only fs.
  314. */
  315. if (IS_RDONLY(inode) &&
  316. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  317. return -EROFS;
  318. /*
  319. * Nobody gets write access to an immutable file.
  320. */
  321. if (IS_IMMUTABLE(inode))
  322. return -EACCES;
  323. }
  324. retval = do_inode_permission(inode, mask);
  325. if (retval)
  326. return retval;
  327. retval = devcgroup_inode_permission(inode, mask);
  328. if (retval)
  329. return retval;
  330. return security_inode_permission(inode, mask);
  331. }
  332. /**
  333. * path_get - get a reference to a path
  334. * @path: path to get the reference to
  335. *
  336. * Given a path increment the reference count to the dentry and the vfsmount.
  337. */
  338. void path_get(struct path *path)
  339. {
  340. mntget(path->mnt);
  341. dget(path->dentry);
  342. }
  343. EXPORT_SYMBOL(path_get);
  344. /**
  345. * path_put - put a reference to a path
  346. * @path: path to put the reference to
  347. *
  348. * Given a path decrement the reference count to the dentry and the vfsmount.
  349. */
  350. void path_put(struct path *path)
  351. {
  352. dput(path->dentry);
  353. mntput(path->mnt);
  354. }
  355. EXPORT_SYMBOL(path_put);
  356. /*
  357. * Path walking has 2 modes, rcu-walk and ref-walk (see
  358. * Documentation/filesystems/path-lookup.txt). In situations when we can't
  359. * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
  360. * normal reference counts on dentries and vfsmounts to transition to rcu-walk
  361. * mode. Refcounts are grabbed at the last known good point before rcu-walk
  362. * got stuck, so ref-walk may continue from there. If this is not successful
  363. * (eg. a seqcount has changed), then failure is returned and it's up to caller
  364. * to restart the path walk from the beginning in ref-walk mode.
  365. */
  366. /**
  367. * unlazy_walk - try to switch to ref-walk mode.
  368. * @nd: nameidata pathwalk data
  369. * @dentry: child of nd->path.dentry or NULL
  370. * Returns: 0 on success, -ECHILD on failure
  371. *
  372. * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
  373. * for ref-walk mode. @dentry must be a path found by a do_lookup call on
  374. * @nd or NULL. Must be called from rcu-walk context.
  375. */
  376. static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
  377. {
  378. struct fs_struct *fs = current->fs;
  379. struct dentry *parent = nd->path.dentry;
  380. int want_root = 0;
  381. BUG_ON(!(nd->flags & LOOKUP_RCU));
  382. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  383. want_root = 1;
  384. spin_lock(&fs->lock);
  385. if (nd->root.mnt != fs->root.mnt ||
  386. nd->root.dentry != fs->root.dentry)
  387. goto err_root;
  388. }
  389. spin_lock(&parent->d_lock);
  390. if (!dentry) {
  391. if (!__d_rcu_to_refcount(parent, nd->seq))
  392. goto err_parent;
  393. BUG_ON(nd->inode != parent->d_inode);
  394. } else {
  395. if (dentry->d_parent != parent)
  396. goto err_parent;
  397. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  398. if (!__d_rcu_to_refcount(dentry, nd->seq))
  399. goto err_child;
  400. /*
  401. * If the sequence check on the child dentry passed, then
  402. * the child has not been removed from its parent. This
  403. * means the parent dentry must be valid and able to take
  404. * a reference at this point.
  405. */
  406. BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
  407. BUG_ON(!parent->d_count);
  408. parent->d_count++;
  409. spin_unlock(&dentry->d_lock);
  410. }
  411. spin_unlock(&parent->d_lock);
  412. if (want_root) {
  413. path_get(&nd->root);
  414. spin_unlock(&fs->lock);
  415. }
  416. mntget(nd->path.mnt);
  417. rcu_read_unlock();
  418. br_read_unlock(vfsmount_lock);
  419. nd->flags &= ~LOOKUP_RCU;
  420. return 0;
  421. err_child:
  422. spin_unlock(&dentry->d_lock);
  423. err_parent:
  424. spin_unlock(&parent->d_lock);
  425. err_root:
  426. if (want_root)
  427. spin_unlock(&fs->lock);
  428. return -ECHILD;
  429. }
  430. /**
  431. * release_open_intent - free up open intent resources
  432. * @nd: pointer to nameidata
  433. */
  434. void release_open_intent(struct nameidata *nd)
  435. {
  436. struct file *file = nd->intent.open.file;
  437. if (file && !IS_ERR(file)) {
  438. if (file->f_path.dentry == NULL)
  439. put_filp(file);
  440. else
  441. fput(file);
  442. }
  443. }
  444. static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
  445. {
  446. return dentry->d_op->d_revalidate(dentry, nd);
  447. }
  448. /**
  449. * complete_walk - successful completion of path walk
  450. * @nd: pointer nameidata
  451. *
  452. * If we had been in RCU mode, drop out of it and legitimize nd->path.
  453. * Revalidate the final result, unless we'd already done that during
  454. * the path walk or the filesystem doesn't ask for it. Return 0 on
  455. * success, -error on failure. In case of failure caller does not
  456. * need to drop nd->path.
  457. */
  458. static int complete_walk(struct nameidata *nd)
  459. {
  460. struct dentry *dentry = nd->path.dentry;
  461. int status;
  462. if (nd->flags & LOOKUP_RCU) {
  463. nd->flags &= ~LOOKUP_RCU;
  464. if (!(nd->flags & LOOKUP_ROOT))
  465. nd->root.mnt = NULL;
  466. spin_lock(&dentry->d_lock);
  467. if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
  468. spin_unlock(&dentry->d_lock);
  469. rcu_read_unlock();
  470. br_read_unlock(vfsmount_lock);
  471. return -ECHILD;
  472. }
  473. BUG_ON(nd->inode != dentry->d_inode);
  474. spin_unlock(&dentry->d_lock);
  475. mntget(nd->path.mnt);
  476. rcu_read_unlock();
  477. br_read_unlock(vfsmount_lock);
  478. }
  479. if (likely(!(nd->flags & LOOKUP_JUMPED)))
  480. return 0;
  481. if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
  482. return 0;
  483. if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
  484. return 0;
  485. /* Note: we do not d_invalidate() */
  486. status = d_revalidate(dentry, nd);
  487. if (status > 0)
  488. return 0;
  489. if (!status)
  490. status = -ESTALE;
  491. path_put(&nd->path);
  492. return status;
  493. }
  494. static __always_inline void set_root(struct nameidata *nd)
  495. {
  496. if (!nd->root.mnt)
  497. get_fs_root(current->fs, &nd->root);
  498. }
  499. static int link_path_walk(const char *, struct nameidata *);
  500. static __always_inline void set_root_rcu(struct nameidata *nd)
  501. {
  502. if (!nd->root.mnt) {
  503. struct fs_struct *fs = current->fs;
  504. unsigned seq;
  505. do {
  506. seq = read_seqcount_begin(&fs->seq);
  507. nd->root = fs->root;
  508. nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
  509. } while (read_seqcount_retry(&fs->seq, seq));
  510. }
  511. }
  512. static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  513. {
  514. int ret;
  515. if (IS_ERR(link))
  516. goto fail;
  517. if (*link == '/') {
  518. set_root(nd);
  519. path_put(&nd->path);
  520. nd->path = nd->root;
  521. path_get(&nd->root);
  522. nd->flags |= LOOKUP_JUMPED;
  523. }
  524. nd->inode = nd->path.dentry->d_inode;
  525. ret = link_path_walk(link, nd);
  526. return ret;
  527. fail:
  528. path_put(&nd->path);
  529. return PTR_ERR(link);
  530. }
  531. static void path_put_conditional(struct path *path, struct nameidata *nd)
  532. {
  533. dput(path->dentry);
  534. if (path->mnt != nd->path.mnt)
  535. mntput(path->mnt);
  536. }
  537. static inline void path_to_nameidata(const struct path *path,
  538. struct nameidata *nd)
  539. {
  540. if (!(nd->flags & LOOKUP_RCU)) {
  541. dput(nd->path.dentry);
  542. if (nd->path.mnt != path->mnt)
  543. mntput(nd->path.mnt);
  544. }
  545. nd->path.mnt = path->mnt;
  546. nd->path.dentry = path->dentry;
  547. }
  548. static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
  549. {
  550. struct inode *inode = link->dentry->d_inode;
  551. if (!IS_ERR(cookie) && inode->i_op->put_link)
  552. inode->i_op->put_link(link->dentry, nd, cookie);
  553. path_put(link);
  554. }
  555. static __always_inline int
  556. follow_link(struct path *link, struct nameidata *nd, void **p)
  557. {
  558. int error;
  559. struct dentry *dentry = link->dentry;
  560. BUG_ON(nd->flags & LOOKUP_RCU);
  561. if (link->mnt == nd->path.mnt)
  562. mntget(link->mnt);
  563. if (unlikely(current->total_link_count >= 40)) {
  564. *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
  565. path_put(&nd->path);
  566. return -ELOOP;
  567. }
  568. cond_resched();
  569. current->total_link_count++;
  570. touch_atime(link->mnt, dentry);
  571. nd_set_link(nd, NULL);
  572. error = security_inode_follow_link(link->dentry, nd);
  573. if (error) {
  574. *p = ERR_PTR(error); /* no ->put_link(), please */
  575. path_put(&nd->path);
  576. return error;
  577. }
  578. nd->last_type = LAST_BIND;
  579. *p = dentry->d_inode->i_op->follow_link(dentry, nd);
  580. error = PTR_ERR(*p);
  581. if (!IS_ERR(*p)) {
  582. char *s = nd_get_link(nd);
  583. error = 0;
  584. if (s)
  585. error = __vfs_follow_link(nd, s);
  586. else if (nd->last_type == LAST_BIND) {
  587. nd->flags |= LOOKUP_JUMPED;
  588. nd->inode = nd->path.dentry->d_inode;
  589. if (nd->inode->i_op->follow_link) {
  590. /* stepped on a _really_ weird one */
  591. path_put(&nd->path);
  592. error = -ELOOP;
  593. }
  594. }
  595. }
  596. return error;
  597. }
  598. static int follow_up_rcu(struct path *path)
  599. {
  600. struct vfsmount *parent;
  601. struct dentry *mountpoint;
  602. parent = path->mnt->mnt_parent;
  603. if (parent == path->mnt)
  604. return 0;
  605. mountpoint = path->mnt->mnt_mountpoint;
  606. path->dentry = mountpoint;
  607. path->mnt = parent;
  608. return 1;
  609. }
  610. int follow_up(struct path *path)
  611. {
  612. struct vfsmount *parent;
  613. struct dentry *mountpoint;
  614. br_read_lock(vfsmount_lock);
  615. parent = path->mnt->mnt_parent;
  616. if (parent == path->mnt) {
  617. br_read_unlock(vfsmount_lock);
  618. return 0;
  619. }
  620. mntget(parent);
  621. mountpoint = dget(path->mnt->mnt_mountpoint);
  622. br_read_unlock(vfsmount_lock);
  623. dput(path->dentry);
  624. path->dentry = mountpoint;
  625. mntput(path->mnt);
  626. path->mnt = parent;
  627. return 1;
  628. }
  629. /*
  630. * Perform an automount
  631. * - return -EISDIR to tell follow_managed() to stop and return the path we
  632. * were called with.
  633. */
  634. static int follow_automount(struct path *path, unsigned flags,
  635. bool *need_mntput)
  636. {
  637. struct vfsmount *mnt;
  638. int err;
  639. if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  640. return -EREMOTE;
  641. /* We don't want to mount if someone's just doing a stat -
  642. * unless they're stat'ing a directory and appended a '/' to
  643. * the name.
  644. *
  645. * We do, however, want to mount if someone wants to open or
  646. * create a file of any type under the mountpoint, wants to
  647. * traverse through the mountpoint or wants to open the
  648. * mounted directory. Also, autofs may mark negative dentries
  649. * as being automount points. These will need the attentions
  650. * of the daemon to instantiate them before they can be used.
  651. */
  652. if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
  653. LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
  654. path->dentry->d_inode)
  655. return -EISDIR;
  656. current->total_link_count++;
  657. if (current->total_link_count >= 40)
  658. return -ELOOP;
  659. mnt = path->dentry->d_op->d_automount(path);
  660. if (IS_ERR(mnt)) {
  661. /*
  662. * The filesystem is allowed to return -EISDIR here to indicate
  663. * it doesn't want to automount. For instance, autofs would do
  664. * this so that its userspace daemon can mount on this dentry.
  665. *
  666. * However, we can only permit this if it's a terminal point in
  667. * the path being looked up; if it wasn't then the remainder of
  668. * the path is inaccessible and we should say so.
  669. */
  670. if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
  671. return -EREMOTE;
  672. return PTR_ERR(mnt);
  673. }
  674. if (!mnt) /* mount collision */
  675. return 0;
  676. if (!*need_mntput) {
  677. /* lock_mount() may release path->mnt on error */
  678. mntget(path->mnt);
  679. *need_mntput = true;
  680. }
  681. err = finish_automount(mnt, path);
  682. switch (err) {
  683. case -EBUSY:
  684. /* Someone else made a mount here whilst we were busy */
  685. return 0;
  686. case 0:
  687. path_put(path);
  688. path->mnt = mnt;
  689. path->dentry = dget(mnt->mnt_root);
  690. return 0;
  691. default:
  692. return err;
  693. }
  694. }
  695. /*
  696. * Handle a dentry that is managed in some way.
  697. * - Flagged for transit management (autofs)
  698. * - Flagged as mountpoint
  699. * - Flagged as automount point
  700. *
  701. * This may only be called in refwalk mode.
  702. *
  703. * Serialization is taken care of in namespace.c
  704. */
  705. static int follow_managed(struct path *path, unsigned flags)
  706. {
  707. struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
  708. unsigned managed;
  709. bool need_mntput = false;
  710. int ret = 0;
  711. /* Given that we're not holding a lock here, we retain the value in a
  712. * local variable for each dentry as we look at it so that we don't see
  713. * the components of that value change under us */
  714. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  715. managed &= DCACHE_MANAGED_DENTRY,
  716. unlikely(managed != 0)) {
  717. /* Allow the filesystem to manage the transit without i_mutex
  718. * being held. */
  719. if (managed & DCACHE_MANAGE_TRANSIT) {
  720. BUG_ON(!path->dentry->d_op);
  721. BUG_ON(!path->dentry->d_op->d_manage);
  722. ret = path->dentry->d_op->d_manage(path->dentry, false);
  723. if (ret < 0)
  724. break;
  725. }
  726. /* Transit to a mounted filesystem. */
  727. if (managed & DCACHE_MOUNTED) {
  728. struct vfsmount *mounted = lookup_mnt(path);
  729. if (mounted) {
  730. dput(path->dentry);
  731. if (need_mntput)
  732. mntput(path->mnt);
  733. path->mnt = mounted;
  734. path->dentry = dget(mounted->mnt_root);
  735. need_mntput = true;
  736. continue;
  737. }
  738. /* Something is mounted on this dentry in another
  739. * namespace and/or whatever was mounted there in this
  740. * namespace got unmounted before we managed to get the
  741. * vfsmount_lock */
  742. }
  743. /* Handle an automount point */
  744. if (managed & DCACHE_NEED_AUTOMOUNT) {
  745. ret = follow_automount(path, flags, &need_mntput);
  746. if (ret < 0)
  747. break;
  748. continue;
  749. }
  750. /* We didn't change the current path point */
  751. break;
  752. }
  753. if (need_mntput && path->mnt == mnt)
  754. mntput(path->mnt);
  755. if (ret == -EISDIR)
  756. ret = 0;
  757. return ret < 0 ? ret : need_mntput;
  758. }
  759. int follow_down_one(struct path *path)
  760. {
  761. struct vfsmount *mounted;
  762. mounted = lookup_mnt(path);
  763. if (mounted) {
  764. dput(path->dentry);
  765. mntput(path->mnt);
  766. path->mnt = mounted;
  767. path->dentry = dget(mounted->mnt_root);
  768. return 1;
  769. }
  770. return 0;
  771. }
  772. static inline bool managed_dentry_might_block(struct dentry *dentry)
  773. {
  774. return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
  775. dentry->d_op->d_manage(dentry, true) < 0);
  776. }
  777. /*
  778. * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
  779. * we meet a managed dentry that would need blocking.
  780. */
  781. static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
  782. struct inode **inode)
  783. {
  784. for (;;) {
  785. struct vfsmount *mounted;
  786. /*
  787. * Don't forget we might have a non-mountpoint managed dentry
  788. * that wants to block transit.
  789. */
  790. if (unlikely(managed_dentry_might_block(path->dentry)))
  791. return false;
  792. if (!d_mountpoint(path->dentry))
  793. break;
  794. mounted = __lookup_mnt(path->mnt, path->dentry, 1);
  795. if (!mounted)
  796. break;
  797. path->mnt = mounted;
  798. path->dentry = mounted->mnt_root;
  799. nd->flags |= LOOKUP_JUMPED;
  800. nd->seq = read_seqcount_begin(&path->dentry->d_seq);
  801. /*
  802. * Update the inode too. We don't need to re-check the
  803. * dentry sequence number here after this d_inode read,
  804. * because a mount-point is always pinned.
  805. */
  806. *inode = path->dentry->d_inode;
  807. }
  808. return true;
  809. }
  810. static void follow_mount_rcu(struct nameidata *nd)
  811. {
  812. while (d_mountpoint(nd->path.dentry)) {
  813. struct vfsmount *mounted;
  814. mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
  815. if (!mounted)
  816. break;
  817. nd->path.mnt = mounted;
  818. nd->path.dentry = mounted->mnt_root;
  819. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  820. }
  821. }
  822. static int follow_dotdot_rcu(struct nameidata *nd)
  823. {
  824. set_root_rcu(nd);
  825. while (1) {
  826. if (nd->path.dentry == nd->root.dentry &&
  827. nd->path.mnt == nd->root.mnt) {
  828. break;
  829. }
  830. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  831. struct dentry *old = nd->path.dentry;
  832. struct dentry *parent = old->d_parent;
  833. unsigned seq;
  834. seq = read_seqcount_begin(&parent->d_seq);
  835. if (read_seqcount_retry(&old->d_seq, nd->seq))
  836. goto failed;
  837. nd->path.dentry = parent;
  838. nd->seq = seq;
  839. break;
  840. }
  841. if (!follow_up_rcu(&nd->path))
  842. break;
  843. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  844. }
  845. follow_mount_rcu(nd);
  846. nd->inode = nd->path.dentry->d_inode;
  847. return 0;
  848. failed:
  849. nd->flags &= ~LOOKUP_RCU;
  850. if (!(nd->flags & LOOKUP_ROOT))
  851. nd->root.mnt = NULL;
  852. rcu_read_unlock();
  853. br_read_unlock(vfsmount_lock);
  854. return -ECHILD;
  855. }
  856. /*
  857. * Follow down to the covering mount currently visible to userspace. At each
  858. * point, the filesystem owning that dentry may be queried as to whether the
  859. * caller is permitted to proceed or not.
  860. */
  861. int follow_down(struct path *path)
  862. {
  863. unsigned managed;
  864. int ret;
  865. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  866. unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  867. /* Allow the filesystem to manage the transit without i_mutex
  868. * being held.
  869. *
  870. * We indicate to the filesystem if someone is trying to mount
  871. * something here. This gives autofs the chance to deny anyone
  872. * other than its daemon the right to mount on its
  873. * superstructure.
  874. *
  875. * The filesystem may sleep at this point.
  876. */
  877. if (managed & DCACHE_MANAGE_TRANSIT) {
  878. BUG_ON(!path->dentry->d_op);
  879. BUG_ON(!path->dentry->d_op->d_manage);
  880. ret = path->dentry->d_op->d_manage(
  881. path->dentry, false);
  882. if (ret < 0)
  883. return ret == -EISDIR ? 0 : ret;
  884. }
  885. /* Transit to a mounted filesystem. */
  886. if (managed & DCACHE_MOUNTED) {
  887. struct vfsmount *mounted = lookup_mnt(path);
  888. if (!mounted)
  889. break;
  890. dput(path->dentry);
  891. mntput(path->mnt);
  892. path->mnt = mounted;
  893. path->dentry = dget(mounted->mnt_root);
  894. continue;
  895. }
  896. /* Don't handle automount points here */
  897. break;
  898. }
  899. return 0;
  900. }
  901. /*
  902. * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
  903. */
  904. static void follow_mount(struct path *path)
  905. {
  906. while (d_mountpoint(path->dentry)) {
  907. struct vfsmount *mounted = lookup_mnt(path);
  908. if (!mounted)
  909. break;
  910. dput(path->dentry);
  911. mntput(path->mnt);
  912. path->mnt = mounted;
  913. path->dentry = dget(mounted->mnt_root);
  914. }
  915. }
  916. static void follow_dotdot(struct nameidata *nd)
  917. {
  918. set_root(nd);
  919. while(1) {
  920. struct dentry *old = nd->path.dentry;
  921. if (nd->path.dentry == nd->root.dentry &&
  922. nd->path.mnt == nd->root.mnt) {
  923. break;
  924. }
  925. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  926. /* rare case of legitimate dget_parent()... */
  927. nd->path.dentry = dget_parent(nd->path.dentry);
  928. dput(old);
  929. break;
  930. }
  931. if (!follow_up(&nd->path))
  932. break;
  933. }
  934. follow_mount(&nd->path);
  935. nd->inode = nd->path.dentry->d_inode;
  936. }
  937. /*
  938. * Allocate a dentry with name and parent, and perform a parent
  939. * directory ->lookup on it. Returns the new dentry, or ERR_PTR
  940. * on error. parent->d_inode->i_mutex must be held. d_lookup must
  941. * have verified that no child exists while under i_mutex.
  942. */
  943. static struct dentry *d_alloc_and_lookup(struct dentry *parent,
  944. struct qstr *name, struct nameidata *nd)
  945. {
  946. struct inode *inode = parent->d_inode;
  947. struct dentry *dentry;
  948. struct dentry *old;
  949. /* Don't create child dentry for a dead directory. */
  950. if (unlikely(IS_DEADDIR(inode)))
  951. return ERR_PTR(-ENOENT);
  952. dentry = d_alloc(parent, name);
  953. if (unlikely(!dentry))
  954. return ERR_PTR(-ENOMEM);
  955. old = inode->i_op->lookup(inode, dentry, nd);
  956. if (unlikely(old)) {
  957. dput(dentry);
  958. dentry = old;
  959. }
  960. return dentry;
  961. }
  962. /*
  963. * We already have a dentry, but require a lookup to be performed on the parent
  964. * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
  965. * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
  966. * child exists while under i_mutex.
  967. */
  968. static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
  969. struct nameidata *nd)
  970. {
  971. struct inode *inode = parent->d_inode;
  972. struct dentry *old;
  973. /* Don't create child dentry for a dead directory. */
  974. if (unlikely(IS_DEADDIR(inode)))
  975. return ERR_PTR(-ENOENT);
  976. old = inode->i_op->lookup(inode, dentry, nd);
  977. if (unlikely(old)) {
  978. dput(dentry);
  979. dentry = old;
  980. }
  981. return dentry;
  982. }
  983. /*
  984. * It's more convoluted than I'd like it to be, but... it's still fairly
  985. * small and for now I'd prefer to have fast path as straight as possible.
  986. * It _is_ time-critical.
  987. */
  988. static int do_lookup(struct nameidata *nd, struct qstr *name,
  989. struct path *path, struct inode **inode)
  990. {
  991. struct vfsmount *mnt = nd->path.mnt;
  992. struct dentry *dentry, *parent = nd->path.dentry;
  993. int need_reval = 1;
  994. int status = 1;
  995. int err;
  996. /*
  997. * Rename seqlock is not required here because in the off chance
  998. * of a false negative due to a concurrent rename, we're going to
  999. * do the non-racy lookup, below.
  1000. */
  1001. if (nd->flags & LOOKUP_RCU) {
  1002. unsigned seq;
  1003. *inode = nd->inode;
  1004. dentry = __d_lookup_rcu(parent, name, &seq, inode);
  1005. if (!dentry)
  1006. goto unlazy;
  1007. /* Memory barrier in read_seqcount_begin of child is enough */
  1008. if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  1009. return -ECHILD;
  1010. nd->seq = seq;
  1011. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1012. status = d_revalidate(dentry, nd);
  1013. if (unlikely(status <= 0)) {
  1014. if (status != -ECHILD)
  1015. need_reval = 0;
  1016. goto unlazy;
  1017. }
  1018. }
  1019. if (unlikely(d_need_lookup(dentry)))
  1020. goto unlazy;
  1021. path->mnt = mnt;
  1022. path->dentry = dentry;
  1023. if (unlikely(!__follow_mount_rcu(nd, path, inode)))
  1024. goto unlazy;
  1025. if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  1026. goto unlazy;
  1027. return 0;
  1028. unlazy:
  1029. if (unlazy_walk(nd, dentry))
  1030. return -ECHILD;
  1031. } else {
  1032. dentry = __d_lookup(parent, name);
  1033. }
  1034. if (dentry && unlikely(d_need_lookup(dentry))) {
  1035. dput(dentry);
  1036. dentry = NULL;
  1037. }
  1038. retry:
  1039. if (unlikely(!dentry)) {
  1040. struct inode *dir = parent->d_inode;
  1041. BUG_ON(nd->inode != dir);
  1042. mutex_lock(&dir->i_mutex);
  1043. dentry = d_lookup(parent, name);
  1044. if (likely(!dentry)) {
  1045. dentry = d_alloc_and_lookup(parent, name, nd);
  1046. if (IS_ERR(dentry)) {
  1047. mutex_unlock(&dir->i_mutex);
  1048. return PTR_ERR(dentry);
  1049. }
  1050. /* known good */
  1051. need_reval = 0;
  1052. status = 1;
  1053. } else if (unlikely(d_need_lookup(dentry))) {
  1054. dentry = d_inode_lookup(parent, dentry, nd);
  1055. if (IS_ERR(dentry)) {
  1056. mutex_unlock(&dir->i_mutex);
  1057. return PTR_ERR(dentry);
  1058. }
  1059. /* known good */
  1060. need_reval = 0;
  1061. status = 1;
  1062. }
  1063. mutex_unlock(&dir->i_mutex);
  1064. }
  1065. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
  1066. status = d_revalidate(dentry, nd);
  1067. if (unlikely(status <= 0)) {
  1068. if (status < 0) {
  1069. dput(dentry);
  1070. return status;
  1071. }
  1072. if (!d_invalidate(dentry)) {
  1073. dput(dentry);
  1074. dentry = NULL;
  1075. need_reval = 1;
  1076. goto retry;
  1077. }
  1078. }
  1079. path->mnt = mnt;
  1080. path->dentry = dentry;
  1081. err = follow_managed(path, nd->flags);
  1082. if (unlikely(err < 0)) {
  1083. path_put_conditional(path, nd);
  1084. return err;
  1085. }
  1086. if (err)
  1087. nd->flags |= LOOKUP_JUMPED;
  1088. *inode = path->dentry->d_inode;
  1089. return 0;
  1090. }
  1091. static inline int may_lookup(struct nameidata *nd)
  1092. {
  1093. if (nd->flags & LOOKUP_RCU) {
  1094. int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
  1095. if (err != -ECHILD)
  1096. return err;
  1097. if (unlazy_walk(nd, NULL))
  1098. return -ECHILD;
  1099. }
  1100. return inode_permission(nd->inode, MAY_EXEC);
  1101. }
  1102. static inline int handle_dots(struct nameidata *nd, int type)
  1103. {
  1104. if (type == LAST_DOTDOT) {
  1105. if (nd->flags & LOOKUP_RCU) {
  1106. if (follow_dotdot_rcu(nd))
  1107. return -ECHILD;
  1108. } else
  1109. follow_dotdot(nd);
  1110. }
  1111. return 0;
  1112. }
  1113. static void terminate_walk(struct nameidata *nd)
  1114. {
  1115. if (!(nd->flags & LOOKUP_RCU)) {
  1116. path_put(&nd->path);
  1117. } else {
  1118. nd->flags &= ~LOOKUP_RCU;
  1119. if (!(nd->flags & LOOKUP_ROOT))
  1120. nd->root.mnt = NULL;
  1121. rcu_read_unlock();
  1122. br_read_unlock(vfsmount_lock);
  1123. }
  1124. }
  1125. /*
  1126. * Do we need to follow links? We _really_ want to be able
  1127. * to do this check without having to look at inode->i_op,
  1128. * so we keep a cache of "no, this doesn't need follow_link"
  1129. * for the common case.
  1130. */
  1131. static inline int should_follow_link(struct inode *inode, int follow)
  1132. {
  1133. if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
  1134. if (likely(inode->i_op->follow_link))
  1135. return follow;
  1136. /* This gets set once for the inode lifetime */
  1137. spin_lock(&inode->i_lock);
  1138. inode->i_opflags |= IOP_NOFOLLOW;
  1139. spin_unlock(&inode->i_lock);
  1140. }
  1141. return 0;
  1142. }
  1143. static inline int walk_component(struct nameidata *nd, struct path *path,
  1144. struct qstr *name, int type, int follow)
  1145. {
  1146. struct inode *inode;
  1147. int err;
  1148. /*
  1149. * "." and ".." are special - ".." especially so because it has
  1150. * to be able to know about the current root directory and
  1151. * parent relationships.
  1152. */
  1153. if (unlikely(type != LAST_NORM))
  1154. return handle_dots(nd, type);
  1155. err = do_lookup(nd, name, path, &inode);
  1156. if (unlikely(err)) {
  1157. terminate_walk(nd);
  1158. return err;
  1159. }
  1160. if (!inode) {
  1161. path_to_nameidata(path, nd);
  1162. terminate_walk(nd);
  1163. return -ENOENT;
  1164. }
  1165. if (should_follow_link(inode, follow)) {
  1166. if (nd->flags & LOOKUP_RCU) {
  1167. if (unlikely(unlazy_walk(nd, path->dentry))) {
  1168. terminate_walk(nd);
  1169. return -ECHILD;
  1170. }
  1171. }
  1172. BUG_ON(inode != path->dentry->d_inode);
  1173. return 1;
  1174. }
  1175. path_to_nameidata(path, nd);
  1176. nd->inode = inode;
  1177. return 0;
  1178. }
  1179. /*
  1180. * This limits recursive symlink follows to 8, while
  1181. * limiting consecutive symlinks to 40.
  1182. *
  1183. * Without that kind of total limit, nasty chains of consecutive
  1184. * symlinks can cause almost arbitrarily long lookups.
  1185. */
  1186. static inline int nested_symlink(struct path *path, struct nameidata *nd)
  1187. {
  1188. int res;
  1189. if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
  1190. path_put_conditional(path, nd);
  1191. path_put(&nd->path);
  1192. return -ELOOP;
  1193. }
  1194. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  1195. nd->depth++;
  1196. current->link_count++;
  1197. do {
  1198. struct path link = *path;
  1199. void *cookie;
  1200. res = follow_link(&link, nd, &cookie);
  1201. if (!res)
  1202. res = walk_component(nd, path, &nd->last,
  1203. nd->last_type, LOOKUP_FOLLOW);
  1204. put_link(nd, &link, cookie);
  1205. } while (res > 0);
  1206. current->link_count--;
  1207. nd->depth--;
  1208. return res;
  1209. }
  1210. /*
  1211. * We really don't want to look at inode->i_op->lookup
  1212. * when we don't have to. So we keep a cache bit in
  1213. * the inode ->i_opflags field that says "yes, we can
  1214. * do lookup on this inode".
  1215. */
  1216. static inline int can_lookup(struct inode *inode)
  1217. {
  1218. if (likely(inode->i_opflags & IOP_LOOKUP))
  1219. return 1;
  1220. if (likely(!inode->i_op->lookup))
  1221. return 0;
  1222. /* We do this once for the lifetime of the inode */
  1223. spin_lock(&inode->i_lock);
  1224. inode->i_opflags |= IOP_LOOKUP;
  1225. spin_unlock(&inode->i_lock);
  1226. return 1;
  1227. }
  1228. /*
  1229. * Name resolution.
  1230. * This is the basic name resolution function, turning a pathname into
  1231. * the final dentry. We expect 'base' to be positive and a directory.
  1232. *
  1233. * Returns 0 and nd will have valid dentry and mnt on success.
  1234. * Returns error and drops reference to input namei data on failure.
  1235. */
  1236. static int link_path_walk(const char *name, struct nameidata *nd)
  1237. {
  1238. struct path next;
  1239. int err;
  1240. while (*name=='/')
  1241. name++;
  1242. if (!*name)
  1243. return 0;
  1244. /* At this point we know we have a real path component. */
  1245. for(;;) {
  1246. unsigned long hash;
  1247. struct qstr this;
  1248. unsigned int c;
  1249. int type;
  1250. err = may_lookup(nd);
  1251. if (err)
  1252. break;
  1253. this.name = name;
  1254. c = *(const unsigned char *)name;
  1255. hash = init_name_hash();
  1256. do {
  1257. name++;
  1258. hash = partial_name_hash(c, hash);
  1259. c = *(const unsigned char *)name;
  1260. } while (c && (c != '/'));
  1261. this.len = name - (const char *) this.name;
  1262. this.hash = end_name_hash(hash);
  1263. type = LAST_NORM;
  1264. if (this.name[0] == '.') switch (this.len) {
  1265. case 2:
  1266. if (this.name[1] == '.') {
  1267. type = LAST_DOTDOT;
  1268. nd->flags |= LOOKUP_JUMPED;
  1269. }
  1270. break;
  1271. case 1:
  1272. type = LAST_DOT;
  1273. }
  1274. if (likely(type == LAST_NORM)) {
  1275. struct dentry *parent = nd->path.dentry;
  1276. nd->flags &= ~LOOKUP_JUMPED;
  1277. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1278. err = parent->d_op->d_hash(parent, nd->inode,
  1279. &this);
  1280. if (err < 0)
  1281. break;
  1282. }
  1283. }
  1284. /* remove trailing slashes? */
  1285. if (!c)
  1286. goto last_component;
  1287. while (*++name == '/');
  1288. if (!*name)
  1289. goto last_component;
  1290. err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
  1291. if (err < 0)
  1292. return err;
  1293. if (err) {
  1294. err = nested_symlink(&next, nd);
  1295. if (err)
  1296. return err;
  1297. }
  1298. if (can_lookup(nd->inode))
  1299. continue;
  1300. err = -ENOTDIR;
  1301. break;
  1302. /* here ends the main loop */
  1303. last_component:
  1304. nd->last = this;
  1305. nd->last_type = type;
  1306. return 0;
  1307. }
  1308. terminate_walk(nd);
  1309. return err;
  1310. }
  1311. static int path_init(int dfd, const char *name, unsigned int flags,
  1312. struct nameidata *nd, struct file **fp)
  1313. {
  1314. int retval = 0;
  1315. int fput_needed;
  1316. struct file *file;
  1317. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1318. nd->flags = flags | LOOKUP_JUMPED;
  1319. nd->depth = 0;
  1320. if (flags & LOOKUP_ROOT) {
  1321. struct inode *inode = nd->root.dentry->d_inode;
  1322. if (*name) {
  1323. if (!inode->i_op->lookup)
  1324. return -ENOTDIR;
  1325. retval = inode_permission(inode, MAY_EXEC);
  1326. if (retval)
  1327. return retval;
  1328. }
  1329. nd->path = nd->root;
  1330. nd->inode = inode;
  1331. if (flags & LOOKUP_RCU) {
  1332. br_read_lock(vfsmount_lock);
  1333. rcu_read_lock();
  1334. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1335. } else {
  1336. path_get(&nd->path);
  1337. }
  1338. return 0;
  1339. }
  1340. nd->root.mnt = NULL;
  1341. if (*name=='/') {
  1342. if (flags & LOOKUP_RCU) {
  1343. br_read_lock(vfsmount_lock);
  1344. rcu_read_lock();
  1345. set_root_rcu(nd);
  1346. } else {
  1347. set_root(nd);
  1348. path_get(&nd->root);
  1349. }
  1350. nd->path = nd->root;
  1351. } else if (dfd == AT_FDCWD) {
  1352. if (flags & LOOKUP_RCU) {
  1353. struct fs_struct *fs = current->fs;
  1354. unsigned seq;
  1355. br_read_lock(vfsmount_lock);
  1356. rcu_read_lock();
  1357. do {
  1358. seq = read_seqcount_begin(&fs->seq);
  1359. nd->path = fs->pwd;
  1360. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1361. } while (read_seqcount_retry(&fs->seq, seq));
  1362. } else {
  1363. get_fs_pwd(current->fs, &nd->path);
  1364. }
  1365. } else {
  1366. struct dentry *dentry;
  1367. file = fget_raw_light(dfd, &fput_needed);
  1368. retval = -EBADF;
  1369. if (!file)
  1370. goto out_fail;
  1371. dentry = file->f_path.dentry;
  1372. if (*name) {
  1373. retval = -ENOTDIR;
  1374. if (!S_ISDIR(dentry->d_inode->i_mode))
  1375. goto fput_fail;
  1376. retval = inode_permission(dentry->d_inode, MAY_EXEC);
  1377. if (retval)
  1378. goto fput_fail;
  1379. }
  1380. nd->path = file->f_path;
  1381. if (flags & LOOKUP_RCU) {
  1382. if (fput_needed)
  1383. *fp = file;
  1384. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1385. br_read_lock(vfsmount_lock);
  1386. rcu_read_lock();
  1387. } else {
  1388. path_get(&file->f_path);
  1389. fput_light(file, fput_needed);
  1390. }
  1391. }
  1392. nd->inode = nd->path.dentry->d_inode;
  1393. return 0;
  1394. fput_fail:
  1395. fput_light(file, fput_needed);
  1396. out_fail:
  1397. return retval;
  1398. }
  1399. static inline int lookup_last(struct nameidata *nd, struct path *path)
  1400. {
  1401. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  1402. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1403. nd->flags &= ~LOOKUP_PARENT;
  1404. return walk_component(nd, path, &nd->last, nd->last_type,
  1405. nd->flags & LOOKUP_FOLLOW);
  1406. }
  1407. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1408. static int path_lookupat(int dfd, const char *name,
  1409. unsigned int flags, struct nameidata *nd)
  1410. {
  1411. struct file *base = NULL;
  1412. struct path path;
  1413. int err;
  1414. /*
  1415. * Path walking is largely split up into 2 different synchronisation
  1416. * schemes, rcu-walk and ref-walk (explained in
  1417. * Documentation/filesystems/path-lookup.txt). These share much of the
  1418. * path walk code, but some things particularly setup, cleanup, and
  1419. * following mounts are sufficiently divergent that functions are
  1420. * duplicated. Typically there is a function foo(), and its RCU
  1421. * analogue, foo_rcu().
  1422. *
  1423. * -ECHILD is the error number of choice (just to avoid clashes) that
  1424. * is returned if some aspect of an rcu-walk fails. Such an error must
  1425. * be handled by restarting a traditional ref-walk (which will always
  1426. * be able to complete).
  1427. */
  1428. err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
  1429. if (unlikely(err))
  1430. return err;
  1431. current->total_link_count = 0;
  1432. err = link_path_walk(name, nd);
  1433. if (!err && !(flags & LOOKUP_PARENT)) {
  1434. err = lookup_last(nd, &path);
  1435. while (err > 0) {
  1436. void *cookie;
  1437. struct path link = path;
  1438. nd->flags |= LOOKUP_PARENT;
  1439. err = follow_link(&link, nd, &cookie);
  1440. if (!err)
  1441. err = lookup_last(nd, &path);
  1442. put_link(nd, &link, cookie);
  1443. }
  1444. }
  1445. if (!err)
  1446. err = complete_walk(nd);
  1447. if (!err && nd->flags & LOOKUP_DIRECTORY) {
  1448. if (!nd->inode->i_op->lookup) {
  1449. path_put(&nd->path);
  1450. err = -ENOTDIR;
  1451. }
  1452. }
  1453. if (base)
  1454. fput(base);
  1455. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  1456. path_put(&nd->root);
  1457. nd->root.mnt = NULL;
  1458. }
  1459. return err;
  1460. }
  1461. static int do_path_lookup(int dfd, const char *name,
  1462. unsigned int flags, struct nameidata *nd)
  1463. {
  1464. int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
  1465. if (unlikely(retval == -ECHILD))
  1466. retval = path_lookupat(dfd, name, flags, nd);
  1467. if (unlikely(retval == -ESTALE))
  1468. retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
  1469. if (likely(!retval)) {
  1470. if (unlikely(!audit_dummy_context())) {
  1471. if (nd->path.dentry && nd->inode)
  1472. audit_inode(name, nd->path.dentry);
  1473. }
  1474. }
  1475. return retval;
  1476. }
  1477. int kern_path_parent(const char *name, struct nameidata *nd)
  1478. {
  1479. return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
  1480. }
  1481. int kern_path(const char *name, unsigned int flags, struct path *path)
  1482. {
  1483. struct nameidata nd;
  1484. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  1485. if (!res)
  1486. *path = nd.path;
  1487. return res;
  1488. }
  1489. /**
  1490. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1491. * @dentry: pointer to dentry of the base directory
  1492. * @mnt: pointer to vfs mount of the base directory
  1493. * @name: pointer to file name
  1494. * @flags: lookup flags
  1495. * @path: pointer to struct path to fill
  1496. */
  1497. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1498. const char *name, unsigned int flags,
  1499. struct path *path)
  1500. {
  1501. struct nameidata nd;
  1502. int err;
  1503. nd.root.dentry = dentry;
  1504. nd.root.mnt = mnt;
  1505. BUG_ON(flags & LOOKUP_PARENT);
  1506. /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
  1507. err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
  1508. if (!err)
  1509. *path = nd.path;
  1510. return err;
  1511. }
  1512. static struct dentry *__lookup_hash(struct qstr *name,
  1513. struct dentry *base, struct nameidata *nd)
  1514. {
  1515. struct inode *inode = base->d_inode;
  1516. struct dentry *dentry;
  1517. int err;
  1518. err = inode_permission(inode, MAY_EXEC);
  1519. if (err)
  1520. return ERR_PTR(err);
  1521. /*
  1522. * Don't bother with __d_lookup: callers are for creat as
  1523. * well as unlink, so a lot of the time it would cost
  1524. * a double lookup.
  1525. */
  1526. dentry = d_lookup(base, name);
  1527. if (dentry && d_need_lookup(dentry)) {
  1528. /*
  1529. * __lookup_hash is called with the parent dir's i_mutex already
  1530. * held, so we are good to go here.
  1531. */
  1532. dentry = d_inode_lookup(base, dentry, nd);
  1533. if (IS_ERR(dentry))
  1534. return dentry;
  1535. }
  1536. if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1537. int status = d_revalidate(dentry, nd);
  1538. if (unlikely(status <= 0)) {
  1539. /*
  1540. * The dentry failed validation.
  1541. * If d_revalidate returned 0 attempt to invalidate
  1542. * the dentry otherwise d_revalidate is asking us
  1543. * to return a fail status.
  1544. */
  1545. if (status < 0) {
  1546. dput(dentry);
  1547. return ERR_PTR(status);
  1548. } else if (!d_invalidate(dentry)) {
  1549. dput(dentry);
  1550. dentry = NULL;
  1551. }
  1552. }
  1553. }
  1554. if (!dentry)
  1555. dentry = d_alloc_and_lookup(base, name, nd);
  1556. return dentry;
  1557. }
  1558. /*
  1559. * Restricted form of lookup. Doesn't follow links, single-component only,
  1560. * needs parent already locked. Doesn't follow mounts.
  1561. * SMP-safe.
  1562. */
  1563. static struct dentry *lookup_hash(struct nameidata *nd)
  1564. {
  1565. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1566. }
  1567. /**
  1568. * lookup_one_len - filesystem helper to lookup single pathname component
  1569. * @name: pathname component to lookup
  1570. * @base: base directory to lookup from
  1571. * @len: maximum length @len should be interpreted to
  1572. *
  1573. * Note that this routine is purely a helper for filesystem usage and should
  1574. * not be called by generic code. Also note that by using this function the
  1575. * nameidata argument is passed to the filesystem methods and a filesystem
  1576. * using this helper needs to be prepared for that.
  1577. */
  1578. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1579. {
  1580. struct qstr this;
  1581. unsigned long hash;
  1582. unsigned int c;
  1583. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1584. this.name = name;
  1585. this.len = len;
  1586. if (!len)
  1587. return ERR_PTR(-EACCES);
  1588. hash = init_name_hash();
  1589. while (len--) {
  1590. c = *(const unsigned char *)name++;
  1591. if (c == '/' || c == '\0')
  1592. return ERR_PTR(-EACCES);
  1593. hash = partial_name_hash(c, hash);
  1594. }
  1595. this.hash = end_name_hash(hash);
  1596. /*
  1597. * See if the low-level filesystem might want
  1598. * to use its own hash..
  1599. */
  1600. if (base->d_flags & DCACHE_OP_HASH) {
  1601. int err = base->d_op->d_hash(base, base->d_inode, &this);
  1602. if (err < 0)
  1603. return ERR_PTR(err);
  1604. }
  1605. return __lookup_hash(&this, base, NULL);
  1606. }
  1607. int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
  1608. struct path *path, int *empty)
  1609. {
  1610. struct nameidata nd;
  1611. char *tmp = getname_flags(name, flags, empty);
  1612. int err = PTR_ERR(tmp);
  1613. if (!IS_ERR(tmp)) {
  1614. BUG_ON(flags & LOOKUP_PARENT);
  1615. err = do_path_lookup(dfd, tmp, flags, &nd);
  1616. putname(tmp);
  1617. if (!err)
  1618. *path = nd.path;
  1619. }
  1620. return err;
  1621. }
  1622. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1623. struct path *path)
  1624. {
  1625. return user_path_at_empty(dfd, name, flags, path, 0);
  1626. }
  1627. static int user_path_parent(int dfd, const char __user *path,
  1628. struct nameidata *nd, char **name)
  1629. {
  1630. char *s = getname(path);
  1631. int error;
  1632. if (IS_ERR(s))
  1633. return PTR_ERR(s);
  1634. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1635. if (error)
  1636. putname(s);
  1637. else
  1638. *name = s;
  1639. return error;
  1640. }
  1641. /*
  1642. * It's inline, so penalty for filesystems that don't use sticky bit is
  1643. * minimal.
  1644. */
  1645. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1646. {
  1647. uid_t fsuid = current_fsuid();
  1648. if (!(dir->i_mode & S_ISVTX))
  1649. return 0;
  1650. if (current_user_ns() != inode_userns(inode))
  1651. goto other_userns;
  1652. if (inode->i_uid == fsuid)
  1653. return 0;
  1654. if (dir->i_uid == fsuid)
  1655. return 0;
  1656. other_userns:
  1657. return !ns_capable(inode_userns(inode), CAP_FOWNER);
  1658. }
  1659. /*
  1660. * Check whether we can remove a link victim from directory dir, check
  1661. * whether the type of victim is right.
  1662. * 1. We can't do it if dir is read-only (done in permission())
  1663. * 2. We should have write and exec permissions on dir
  1664. * 3. We can't remove anything from append-only dir
  1665. * 4. We can't do anything with immutable dir (done in permission())
  1666. * 5. If the sticky bit on dir is set we should either
  1667. * a. be owner of dir, or
  1668. * b. be owner of victim, or
  1669. * c. have CAP_FOWNER capability
  1670. * 6. If the victim is append-only or immutable we can't do antyhing with
  1671. * links pointing to it.
  1672. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1673. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1674. * 9. We can't remove a root or mountpoint.
  1675. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1676. * nfs_async_unlink().
  1677. */
  1678. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1679. {
  1680. int error;
  1681. if (!victim->d_inode)
  1682. return -ENOENT;
  1683. BUG_ON(victim->d_parent->d_inode != dir);
  1684. audit_inode_child(victim, dir);
  1685. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1686. if (error)
  1687. return error;
  1688. if (IS_APPEND(dir))
  1689. return -EPERM;
  1690. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1691. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1692. return -EPERM;
  1693. if (isdir) {
  1694. if (!S_ISDIR(victim->d_inode->i_mode))
  1695. return -ENOTDIR;
  1696. if (IS_ROOT(victim))
  1697. return -EBUSY;
  1698. } else if (S_ISDIR(victim->d_inode->i_mode))
  1699. return -EISDIR;
  1700. if (IS_DEADDIR(dir))
  1701. return -ENOENT;
  1702. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1703. return -EBUSY;
  1704. return 0;
  1705. }
  1706. /* Check whether we can create an object with dentry child in directory
  1707. * dir.
  1708. * 1. We can't do it if child already exists (open has special treatment for
  1709. * this case, but since we are inlined it's OK)
  1710. * 2. We can't do it if dir is read-only (done in permission())
  1711. * 3. We should have write and exec permissions on dir
  1712. * 4. We can't do it if dir is immutable (done in permission())
  1713. */
  1714. static inline int may_create(struct inode *dir, struct dentry *child)
  1715. {
  1716. if (child->d_inode)
  1717. return -EEXIST;
  1718. if (IS_DEADDIR(dir))
  1719. return -ENOENT;
  1720. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1721. }
  1722. /*
  1723. * p1 and p2 should be directories on the same fs.
  1724. */
  1725. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1726. {
  1727. struct dentry *p;
  1728. if (p1 == p2) {
  1729. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1730. return NULL;
  1731. }
  1732. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1733. p = d_ancestor(p2, p1);
  1734. if (p) {
  1735. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1736. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1737. return p;
  1738. }
  1739. p = d_ancestor(p1, p2);
  1740. if (p) {
  1741. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1742. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1743. return p;
  1744. }
  1745. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1746. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1747. return NULL;
  1748. }
  1749. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1750. {
  1751. mutex_unlock(&p1->d_inode->i_mutex);
  1752. if (p1 != p2) {
  1753. mutex_unlock(&p2->d_inode->i_mutex);
  1754. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1755. }
  1756. }
  1757. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1758. struct nameidata *nd)
  1759. {
  1760. int error = may_create(dir, dentry);
  1761. if (error)
  1762. return error;
  1763. if (!dir->i_op->create)
  1764. return -EACCES; /* shouldn't it be ENOSYS? */
  1765. mode &= S_IALLUGO;
  1766. mode |= S_IFREG;
  1767. error = security_inode_create(dir, dentry, mode);
  1768. if (error)
  1769. return error;
  1770. error = dir->i_op->create(dir, dentry, mode, nd);
  1771. if (!error)
  1772. fsnotify_create(dir, dentry);
  1773. return error;
  1774. }
  1775. static int may_open(struct path *path, int acc_mode, int flag)
  1776. {
  1777. struct dentry *dentry = path->dentry;
  1778. struct inode *inode = dentry->d_inode;
  1779. int error;
  1780. /* O_PATH? */
  1781. if (!acc_mode)
  1782. return 0;
  1783. if (!inode)
  1784. return -ENOENT;
  1785. switch (inode->i_mode & S_IFMT) {
  1786. case S_IFLNK:
  1787. return -ELOOP;
  1788. case S_IFDIR:
  1789. if (acc_mode & MAY_WRITE)
  1790. return -EISDIR;
  1791. break;
  1792. case S_IFBLK:
  1793. case S_IFCHR:
  1794. if (path->mnt->mnt_flags & MNT_NODEV)
  1795. return -EACCES;
  1796. /*FALLTHRU*/
  1797. case S_IFIFO:
  1798. case S_IFSOCK:
  1799. flag &= ~O_TRUNC;
  1800. break;
  1801. }
  1802. error = inode_permission(inode, acc_mode);
  1803. if (error)
  1804. return error;
  1805. /*
  1806. * An append-only file must be opened in append mode for writing.
  1807. */
  1808. if (IS_APPEND(inode)) {
  1809. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  1810. return -EPERM;
  1811. if (flag & O_TRUNC)
  1812. return -EPERM;
  1813. }
  1814. /* O_NOATIME can only be set by the owner or superuser */
  1815. if (flag & O_NOATIME && !inode_owner_or_capable(inode))
  1816. return -EPERM;
  1817. /*
  1818. * Ensure there are no outstanding leases on the file.
  1819. */
  1820. return break_lease(inode, flag);
  1821. }
  1822. static int handle_truncate(struct file *filp)
  1823. {
  1824. struct path *path = &filp->f_path;
  1825. struct inode *inode = path->dentry->d_inode;
  1826. int error = get_write_access(inode);
  1827. if (error)
  1828. return error;
  1829. /*
  1830. * Refuse to truncate files with mandatory locks held on them.
  1831. */
  1832. error = locks_verify_locked(inode);
  1833. if (!error)
  1834. error = security_path_truncate(path);
  1835. if (!error) {
  1836. error = do_truncate(path->dentry, 0,
  1837. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1838. filp);
  1839. }
  1840. put_write_access(inode);
  1841. return error;
  1842. }
  1843. static inline int open_to_namei_flags(int flag)
  1844. {
  1845. if ((flag & O_ACCMODE) == 3)
  1846. flag--;
  1847. return flag;
  1848. }
  1849. /*
  1850. * Handle the last step of open()
  1851. */
  1852. static struct file *do_last(struct nameidata *nd, struct path *path,
  1853. const struct open_flags *op, const char *pathname)
  1854. {
  1855. struct dentry *dir = nd->path.dentry;
  1856. struct dentry *dentry;
  1857. int open_flag = op->open_flag;
  1858. int will_truncate = open_flag & O_TRUNC;
  1859. int want_write = 0;
  1860. int acc_mode = op->acc_mode;
  1861. struct file *filp;
  1862. int error;
  1863. nd->flags &= ~LOOKUP_PARENT;
  1864. nd->flags |= op->intent;
  1865. switch (nd->last_type) {
  1866. case LAST_DOTDOT:
  1867. case LAST_DOT:
  1868. error = handle_dots(nd, nd->last_type);
  1869. if (error)
  1870. return ERR_PTR(error);
  1871. /* fallthrough */
  1872. case LAST_ROOT:
  1873. error = complete_walk(nd);
  1874. if (error)
  1875. return ERR_PTR(error);
  1876. audit_inode(pathname, nd->path.dentry);
  1877. if (open_flag & O_CREAT) {
  1878. error = -EISDIR;
  1879. goto exit;
  1880. }
  1881. goto ok;
  1882. case LAST_BIND:
  1883. error = complete_walk(nd);
  1884. if (error)
  1885. return ERR_PTR(error);
  1886. audit_inode(pathname, dir);
  1887. goto ok;
  1888. }
  1889. if (!(open_flag & O_CREAT)) {
  1890. int symlink_ok = 0;
  1891. if (nd->last.name[nd->last.len])
  1892. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1893. if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
  1894. symlink_ok = 1;
  1895. /* we _can_ be in RCU mode here */
  1896. error = walk_component(nd, path, &nd->last, LAST_NORM,
  1897. !symlink_ok);
  1898. if (error < 0)
  1899. return ERR_PTR(error);
  1900. if (error) /* symlink */
  1901. return NULL;
  1902. /* sayonara */
  1903. error = complete_walk(nd);
  1904. if (error)
  1905. return ERR_PTR(-ECHILD);
  1906. error = -ENOTDIR;
  1907. if (nd->flags & LOOKUP_DIRECTORY) {
  1908. if (!nd->inode->i_op->lookup)
  1909. goto exit;
  1910. }
  1911. audit_inode(pathname, nd->path.dentry);
  1912. goto ok;
  1913. }
  1914. /* create side of things */
  1915. /*
  1916. * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been
  1917. * cleared when we got to the last component we are about to look up
  1918. */
  1919. error = complete_walk(nd);
  1920. if (error)
  1921. return ERR_PTR(error);
  1922. audit_inode(pathname, dir);
  1923. error = -EISDIR;
  1924. /* trailing slashes? */
  1925. if (nd->last.name[nd->last.len])
  1926. goto exit;
  1927. mutex_lock(&dir->d_inode->i_mutex);
  1928. dentry = lookup_hash(nd);
  1929. error = PTR_ERR(dentry);
  1930. if (IS_ERR(dentry)) {
  1931. mutex_unlock(&dir->d_inode->i_mutex);
  1932. goto exit;
  1933. }
  1934. path->dentry = dentry;
  1935. path->mnt = nd->path.mnt;
  1936. /* Negative dentry, just create the file */
  1937. if (!dentry->d_inode) {
  1938. int mode = op->mode;
  1939. if (!IS_POSIXACL(dir->d_inode))
  1940. mode &= ~current_umask();
  1941. /*
  1942. * This write is needed to ensure that a
  1943. * rw->ro transition does not occur between
  1944. * the time when the file is created and when
  1945. * a permanent write count is taken through
  1946. * the 'struct file' in nameidata_to_filp().
  1947. */
  1948. error = mnt_want_write(nd->path.mnt);
  1949. if (error)
  1950. goto exit_mutex_unlock;
  1951. want_write = 1;
  1952. /* Don't check for write permission, don't truncate */
  1953. open_flag &= ~O_TRUNC;
  1954. will_truncate = 0;
  1955. acc_mode = MAY_OPEN;
  1956. error = security_path_mknod(&nd->path, dentry, mode, 0);
  1957. if (error)
  1958. goto exit_mutex_unlock;
  1959. error = vfs_create(dir->d_inode, dentry, mode, nd);
  1960. if (error)
  1961. goto exit_mutex_unlock;
  1962. mutex_unlock(&dir->d_inode->i_mutex);
  1963. dput(nd->path.dentry);
  1964. nd->path.dentry = dentry;
  1965. goto common;
  1966. }
  1967. /*
  1968. * It already exists.
  1969. */
  1970. mutex_unlock(&dir->d_inode->i_mutex);
  1971. audit_inode(pathname, path->dentry);
  1972. error = -EEXIST;
  1973. if (open_flag & O_EXCL)
  1974. goto exit_dput;
  1975. error = follow_managed(path, nd->flags);
  1976. if (error < 0)
  1977. goto exit_dput;
  1978. if (error)
  1979. nd->flags |= LOOKUP_JUMPED;
  1980. error = -ENOENT;
  1981. if (!path->dentry->d_inode)
  1982. goto exit_dput;
  1983. if (path->dentry->d_inode->i_op->follow_link)
  1984. return NULL;
  1985. path_to_nameidata(path, nd);
  1986. nd->inode = path->dentry->d_inode;
  1987. /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
  1988. error = complete_walk(nd);
  1989. if (error)
  1990. goto exit;
  1991. error = -EISDIR;
  1992. if (S_ISDIR(nd->inode->i_mode))
  1993. goto exit;
  1994. ok:
  1995. if (!S_ISREG(nd->inode->i_mode))
  1996. will_truncate = 0;
  1997. if (will_truncate) {
  1998. error = mnt_want_write(nd->path.mnt);
  1999. if (error)
  2000. goto exit;
  2001. want_write = 1;
  2002. }
  2003. common:
  2004. error = may_open(&nd->path, acc_mode, open_flag);
  2005. if (error)
  2006. goto exit;
  2007. filp = nameidata_to_filp(nd);
  2008. if (!IS_ERR(filp)) {
  2009. error = ima_file_check(filp, op->acc_mode);
  2010. if (error) {
  2011. fput(filp);
  2012. filp = ERR_PTR(error);
  2013. }
  2014. }
  2015. if (!IS_ERR(filp)) {
  2016. if (will_truncate) {
  2017. error = handle_truncate(filp);
  2018. if (error) {
  2019. fput(filp);
  2020. filp = ERR_PTR(error);
  2021. }
  2022. }
  2023. }
  2024. out:
  2025. if (want_write)
  2026. mnt_drop_write(nd->path.mnt);
  2027. path_put(&nd->path);
  2028. return filp;
  2029. exit_mutex_unlock:
  2030. mutex_unlock(&dir->d_inode->i_mutex);
  2031. exit_dput:
  2032. path_put_conditional(path, nd);
  2033. exit:
  2034. filp = ERR_PTR(error);
  2035. goto out;
  2036. }
  2037. static struct file *path_openat(int dfd, const char *pathname,
  2038. struct nameidata *nd, const struct open_flags *op, int flags)
  2039. {
  2040. struct file *base = NULL;
  2041. struct file *filp;
  2042. struct path path;
  2043. int error;
  2044. filp = get_empty_filp();
  2045. if (!filp)
  2046. return ERR_PTR(-ENFILE);
  2047. filp->f_flags = op->open_flag;
  2048. nd->intent.open.file = filp;
  2049. nd->intent.open.flags = open_to_namei_flags(op->open_flag);
  2050. nd->intent.open.create_mode = op->mode;
  2051. error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
  2052. if (unlikely(error))
  2053. goto out_filp;
  2054. current->total_link_count = 0;
  2055. error = link_path_walk(pathname, nd);
  2056. if (unlikely(error))
  2057. goto out_filp;
  2058. filp = do_last(nd, &path, op, pathname);
  2059. while (unlikely(!filp)) { /* trailing symlink */
  2060. struct path link = path;
  2061. void *cookie;
  2062. if (!(nd->flags & LOOKUP_FOLLOW)) {
  2063. path_put_conditional(&path, nd);
  2064. path_put(&nd->path);
  2065. filp = ERR_PTR(-ELOOP);
  2066. break;
  2067. }
  2068. nd->flags |= LOOKUP_PARENT;
  2069. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  2070. error = follow_link(&link, nd, &cookie);
  2071. if (unlikely(error))
  2072. filp = ERR_PTR(error);
  2073. else
  2074. filp = do_last(nd, &path, op, pathname);
  2075. put_link(nd, &link, cookie);
  2076. }
  2077. out:
  2078. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  2079. path_put(&nd->root);
  2080. if (base)
  2081. fput(base);
  2082. release_open_intent(nd);
  2083. return filp;
  2084. out_filp:
  2085. filp = ERR_PTR(error);
  2086. goto out;
  2087. }
  2088. struct file *do_filp_open(int dfd, const char *pathname,
  2089. const struct open_flags *op, int flags)
  2090. {
  2091. struct nameidata nd;
  2092. struct file *filp;
  2093. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
  2094. if (unlikely(filp == ERR_PTR(-ECHILD)))
  2095. filp = path_openat(dfd, pathname, &nd, op, flags);
  2096. if (unlikely(filp == ERR_PTR(-ESTALE)))
  2097. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
  2098. return filp;
  2099. }
  2100. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  2101. const char *name, const struct open_flags *op, int flags)
  2102. {
  2103. struct nameidata nd;
  2104. struct file *file;
  2105. nd.root.mnt = mnt;
  2106. nd.root.dentry = dentry;
  2107. flags |= LOOKUP_ROOT;
  2108. if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
  2109. return ERR_PTR(-ELOOP);
  2110. file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
  2111. if (unlikely(file == ERR_PTR(-ECHILD)))
  2112. file = path_openat(-1, name, &nd, op, flags);
  2113. if (unlikely(file == ERR_PTR(-ESTALE)))
  2114. file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
  2115. return file;
  2116. }
  2117. struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
  2118. {
  2119. struct dentry *dentry = ERR_PTR(-EEXIST);
  2120. struct nameidata nd;
  2121. int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
  2122. if (error)
  2123. return ERR_PTR(error);
  2124. /*
  2125. * Yucky last component or no last component at all?
  2126. * (foo/., foo/.., /////)
  2127. */
  2128. if (nd.last_type != LAST_NORM)
  2129. goto out;
  2130. nd.flags &= ~LOOKUP_PARENT;
  2131. nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  2132. nd.intent.open.flags = O_EXCL;
  2133. /*
  2134. * Do the final lookup.
  2135. */
  2136. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2137. dentry = lookup_hash(&nd);
  2138. if (IS_ERR(dentry))
  2139. goto fail;
  2140. if (dentry->d_inode)
  2141. goto eexist;
  2142. /*
  2143. * Special case - lookup gave negative, but... we had foo/bar/
  2144. * From the vfs_mknod() POV we just have a negative dentry -
  2145. * all is fine. Let's be bastards - you had / on the end, you've
  2146. * been asking for (non-existent) directory. -ENOENT for you.
  2147. */
  2148. if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
  2149. dput(dentry);
  2150. dentry = ERR_PTR(-ENOENT);
  2151. goto fail;
  2152. }
  2153. *path = nd.path;
  2154. return dentry;
  2155. eexist:
  2156. dput(dentry);
  2157. dentry = ERR_PTR(-EEXIST);
  2158. fail:
  2159. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2160. out:
  2161. path_put(&nd.path);
  2162. return dentry;
  2163. }
  2164. EXPORT_SYMBOL(kern_path_create);
  2165. struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
  2166. {
  2167. char *tmp = getname(pathname);
  2168. struct dentry *res;
  2169. if (IS_ERR(tmp))
  2170. return ERR_CAST(tmp);
  2171. res = kern_path_create(dfd, tmp, path, is_dir);
  2172. putname(tmp);
  2173. return res;
  2174. }
  2175. EXPORT_SYMBOL(user_path_create);
  2176. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  2177. {
  2178. int error = may_create(dir, dentry);
  2179. if (error)
  2180. return error;
  2181. if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
  2182. !ns_capable(inode_userns(dir), CAP_MKNOD))
  2183. return -EPERM;
  2184. if (!dir->i_op->mknod)
  2185. return -EPERM;
  2186. error = devcgroup_inode_mknod(mode, dev);
  2187. if (error)
  2188. return error;
  2189. error = security_inode_mknod(dir, dentry, mode, dev);
  2190. if (error)
  2191. return error;
  2192. error = dir->i_op->mknod(dir, dentry, mode, dev);
  2193. if (!error)
  2194. fsnotify_create(dir, dentry);
  2195. return error;
  2196. }
  2197. static int may_mknod(mode_t mode)
  2198. {
  2199. switch (mode & S_IFMT) {
  2200. case S_IFREG:
  2201. case S_IFCHR:
  2202. case S_IFBLK:
  2203. case S_IFIFO:
  2204. case S_IFSOCK:
  2205. case 0: /* zero mode translates to S_IFREG */
  2206. return 0;
  2207. case S_IFDIR:
  2208. return -EPERM;
  2209. default:
  2210. return -EINVAL;
  2211. }
  2212. }
  2213. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
  2214. unsigned, dev)
  2215. {
  2216. struct dentry *dentry;
  2217. struct path path;
  2218. int error;
  2219. if (S_ISDIR(mode))
  2220. return -EPERM;
  2221. dentry = user_path_create(dfd, filename, &path, 0);
  2222. if (IS_ERR(dentry))
  2223. return PTR_ERR(dentry);
  2224. if (!IS_POSIXACL(path.dentry->d_inode))
  2225. mode &= ~current_umask();
  2226. error = may_mknod(mode);
  2227. if (error)
  2228. goto out_dput;
  2229. error = mnt_want_write(path.mnt);
  2230. if (error)
  2231. goto out_dput;
  2232. error = security_path_mknod(&path, dentry, mode, dev);
  2233. if (error)
  2234. goto out_drop_write;
  2235. switch (mode & S_IFMT) {
  2236. case 0: case S_IFREG:
  2237. error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
  2238. break;
  2239. case S_IFCHR: case S_IFBLK:
  2240. error = vfs_mknod(path.dentry->d_inode,dentry,mode,
  2241. new_decode_dev(dev));
  2242. break;
  2243. case S_IFIFO: case S_IFSOCK:
  2244. error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
  2245. break;
  2246. }
  2247. out_drop_write:
  2248. mnt_drop_write(path.mnt);
  2249. out_dput:
  2250. dput(dentry);
  2251. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2252. path_put(&path);
  2253. return error;
  2254. }
  2255. SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
  2256. {
  2257. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  2258. }
  2259. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2260. {
  2261. int error = may_create(dir, dentry);
  2262. if (error)
  2263. return error;
  2264. if (!dir->i_op->mkdir)
  2265. return -EPERM;
  2266. mode &= (S_IRWXUGO|S_ISVTX);
  2267. error = security_inode_mkdir(dir, dentry, mode);
  2268. if (error)
  2269. return error;
  2270. error = dir->i_op->mkdir(dir, dentry, mode);
  2271. if (!error)
  2272. fsnotify_mkdir(dir, dentry);
  2273. return error;
  2274. }
  2275. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
  2276. {
  2277. struct dentry *dentry;
  2278. struct path path;
  2279. int error;
  2280. dentry = user_path_create(dfd, pathname, &path, 1);
  2281. if (IS_ERR(dentry))
  2282. return PTR_ERR(dentry);
  2283. if (!IS_POSIXACL(path.dentry->d_inode))
  2284. mode &= ~current_umask();
  2285. error = mnt_want_write(path.mnt);
  2286. if (error)
  2287. goto out_dput;
  2288. error = security_path_mkdir(&path, dentry, mode);
  2289. if (error)
  2290. goto out_drop_write;
  2291. error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
  2292. out_drop_write:
  2293. mnt_drop_write(path.mnt);
  2294. out_dput:
  2295. dput(dentry);
  2296. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2297. path_put(&path);
  2298. return error;
  2299. }
  2300. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
  2301. {
  2302. return sys_mkdirat(AT_FDCWD, pathname, mode);
  2303. }
  2304. /*
  2305. * The dentry_unhash() helper will try to drop the dentry early: we
  2306. * should have a usage count of 2 if we're the only user of this
  2307. * dentry, and if that is true (possibly after pruning the dcache),
  2308. * then we drop the dentry now.
  2309. *
  2310. * A low-level filesystem can, if it choses, legally
  2311. * do a
  2312. *
  2313. * if (!d_unhashed(dentry))
  2314. * return -EBUSY;
  2315. *
  2316. * if it cannot handle the case of removing a directory
  2317. * that is still in use by something else..
  2318. */
  2319. void dentry_unhash(struct dentry *dentry)
  2320. {
  2321. shrink_dcache_parent(dentry);
  2322. spin_lock(&dentry->d_lock);
  2323. if (dentry->d_count == 1)
  2324. __d_drop(dentry);
  2325. spin_unlock(&dentry->d_lock);
  2326. }
  2327. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  2328. {
  2329. int error = may_delete(dir, dentry, 1);
  2330. if (error)
  2331. return error;
  2332. if (!dir->i_op->rmdir)
  2333. return -EPERM;
  2334. dget(dentry);
  2335. mutex_lock(&dentry->d_inode->i_mutex);
  2336. error = -EBUSY;
  2337. if (d_mountpoint(dentry))
  2338. goto out;
  2339. error = security_inode_rmdir(dir, dentry);
  2340. if (error)
  2341. goto out;
  2342. shrink_dcache_parent(dentry);
  2343. error = dir->i_op->rmdir(dir, dentry);
  2344. if (error)
  2345. goto out;
  2346. dentry->d_inode->i_flags |= S_DEAD;
  2347. dont_mount(dentry);
  2348. out:
  2349. mutex_unlock(&dentry->d_inode->i_mutex);
  2350. dput(dentry);
  2351. if (!error)
  2352. d_delete(dentry);
  2353. return error;
  2354. }
  2355. static long do_rmdir(int dfd, const char __user *pathname)
  2356. {
  2357. int error = 0;
  2358. char * name;
  2359. struct dentry *dentry;
  2360. struct nameidata nd;
  2361. error = user_path_parent(dfd, pathname, &nd, &name);
  2362. if (error)
  2363. return error;
  2364. switch(nd.last_type) {
  2365. case LAST_DOTDOT:
  2366. error = -ENOTEMPTY;
  2367. goto exit1;
  2368. case LAST_DOT:
  2369. error = -EINVAL;
  2370. goto exit1;
  2371. case LAST_ROOT:
  2372. error = -EBUSY;
  2373. goto exit1;
  2374. }
  2375. nd.flags &= ~LOOKUP_PARENT;
  2376. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2377. dentry = lookup_hash(&nd);
  2378. error = PTR_ERR(dentry);
  2379. if (IS_ERR(dentry))
  2380. goto exit2;
  2381. if (!dentry->d_inode) {
  2382. error = -ENOENT;
  2383. goto exit3;
  2384. }
  2385. error = mnt_want_write(nd.path.mnt);
  2386. if (error)
  2387. goto exit3;
  2388. error = security_path_rmdir(&nd.path, dentry);
  2389. if (error)
  2390. goto exit4;
  2391. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  2392. exit4:
  2393. mnt_drop_write(nd.path.mnt);
  2394. exit3:
  2395. dput(dentry);
  2396. exit2:
  2397. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2398. exit1:
  2399. path_put(&nd.path);
  2400. putname(name);
  2401. return error;
  2402. }
  2403. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  2404. {
  2405. return do_rmdir(AT_FDCWD, pathname);
  2406. }
  2407. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  2408. {
  2409. int error = may_delete(dir, dentry, 0);
  2410. if (error)
  2411. return error;
  2412. if (!dir->i_op->unlink)
  2413. return -EPERM;
  2414. mutex_lock(&dentry->d_inode->i_mutex);
  2415. if (d_mountpoint(dentry))
  2416. error = -EBUSY;
  2417. else {
  2418. error = security_inode_unlink(dir, dentry);
  2419. if (!error) {
  2420. error = dir->i_op->unlink(dir, dentry);
  2421. if (!error)
  2422. dont_mount(dentry);
  2423. }
  2424. }
  2425. mutex_unlock(&dentry->d_inode->i_mutex);
  2426. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  2427. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  2428. fsnotify_link_count(dentry->d_inode);
  2429. d_delete(dentry);
  2430. }
  2431. return error;
  2432. }
  2433. /*
  2434. * Make sure that the actual truncation of the file will occur outside its
  2435. * directory's i_mutex. Truncate can take a long time if there is a lot of
  2436. * writeout happening, and we don't want to prevent access to the directory
  2437. * while waiting on the I/O.
  2438. */
  2439. static long do_unlinkat(int dfd, const char __user *pathname)
  2440. {
  2441. int error;
  2442. char *name;
  2443. struct dentry *dentry;
  2444. struct nameidata nd;
  2445. struct inode *inode = NULL;
  2446. error = user_path_parent(dfd, pathname, &nd, &name);
  2447. if (error)
  2448. return error;
  2449. error = -EISDIR;
  2450. if (nd.last_type != LAST_NORM)
  2451. goto exit1;
  2452. nd.flags &= ~LOOKUP_PARENT;
  2453. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2454. dentry = lookup_hash(&nd);
  2455. error = PTR_ERR(dentry);
  2456. if (!IS_ERR(dentry)) {
  2457. /* Why not before? Because we want correct error value */
  2458. if (nd.last.name[nd.last.len])
  2459. goto slashes;
  2460. inode = dentry->d_inode;
  2461. if (!inode)
  2462. goto slashes;
  2463. ihold(inode);
  2464. error = mnt_want_write(nd.path.mnt);
  2465. if (error)
  2466. goto exit2;
  2467. error = security_path_unlink(&nd.path, dentry);
  2468. if (error)
  2469. goto exit3;
  2470. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2471. exit3:
  2472. mnt_drop_write(nd.path.mnt);
  2473. exit2:
  2474. dput(dentry);
  2475. }
  2476. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2477. if (inode)
  2478. iput(inode); /* truncate the inode here */
  2479. exit1:
  2480. path_put(&nd.path);
  2481. putname(name);
  2482. return error;
  2483. slashes:
  2484. error = !dentry->d_inode ? -ENOENT :
  2485. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2486. goto exit2;
  2487. }
  2488. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  2489. {
  2490. if ((flag & ~AT_REMOVEDIR) != 0)
  2491. return -EINVAL;
  2492. if (flag & AT_REMOVEDIR)
  2493. return do_rmdir(dfd, pathname);
  2494. return do_unlinkat(dfd, pathname);
  2495. }
  2496. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  2497. {
  2498. return do_unlinkat(AT_FDCWD, pathname);
  2499. }
  2500. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2501. {
  2502. int error = may_create(dir, dentry);
  2503. if (error)
  2504. return error;
  2505. if (!dir->i_op->symlink)
  2506. return -EPERM;
  2507. error = security_inode_symlink(dir, dentry, oldname);
  2508. if (error)
  2509. return error;
  2510. error = dir->i_op->symlink(dir, dentry, oldname);
  2511. if (!error)
  2512. fsnotify_create(dir, dentry);
  2513. return error;
  2514. }
  2515. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  2516. int, newdfd, const char __user *, newname)
  2517. {
  2518. int error;
  2519. char *from;
  2520. struct dentry *dentry;
  2521. struct path path;
  2522. from = getname(oldname);
  2523. if (IS_ERR(from))
  2524. return PTR_ERR(from);
  2525. dentry = user_path_create(newdfd, newname, &path, 0);
  2526. error = PTR_ERR(dentry);
  2527. if (IS_ERR(dentry))
  2528. goto out_putname;
  2529. error = mnt_want_write(path.mnt);
  2530. if (error)
  2531. goto out_dput;
  2532. error = security_path_symlink(&path, dentry, from);
  2533. if (error)
  2534. goto out_drop_write;
  2535. error = vfs_symlink(path.dentry->d_inode, dentry, from);
  2536. out_drop_write:
  2537. mnt_drop_write(path.mnt);
  2538. out_dput:
  2539. dput(dentry);
  2540. mutex_unlock(&path.dentry->d_inode->i_mutex);
  2541. path_put(&path);
  2542. out_putname:
  2543. putname(from);
  2544. return error;
  2545. }
  2546. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  2547. {
  2548. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2549. }
  2550. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2551. {
  2552. struct inode *inode = old_dentry->d_inode;
  2553. int error;
  2554. if (!inode)
  2555. return -ENOENT;
  2556. error = may_create(dir, new_dentry);
  2557. if (error)
  2558. return error;
  2559. if (dir->i_sb != inode->i_sb)
  2560. return -EXDEV;
  2561. /*
  2562. * A link to an append-only or immutable file cannot be created.
  2563. */
  2564. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2565. return -EPERM;
  2566. if (!dir->i_op->link)
  2567. return -EPERM;
  2568. if (S_ISDIR(inode->i_mode))
  2569. return -EPERM;
  2570. error = security_inode_link(old_dentry, dir, new_dentry);
  2571. if (error)
  2572. return error;
  2573. mutex_lock(&inode->i_mutex);
  2574. /* Make sure we don't allow creating hardlink to an unlinked file */
  2575. if (inode->i_nlink == 0)
  2576. error = -ENOENT;
  2577. else
  2578. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2579. mutex_unlock(&inode->i_mutex);
  2580. if (!error)
  2581. fsnotify_link(dir, inode, new_dentry);
  2582. return error;
  2583. }
  2584. /*
  2585. * Hardlinks are often used in delicate situations. We avoid
  2586. * security-related surprises by not following symlinks on the
  2587. * newname. --KAB
  2588. *
  2589. * We don't follow them on the oldname either to be compatible
  2590. * with linux 2.0, and to avoid hard-linking to directories
  2591. * and other special files. --ADM
  2592. */
  2593. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  2594. int, newdfd, const char __user *, newname, int, flags)
  2595. {
  2596. struct dentry *new_dentry;
  2597. struct path old_path, new_path;
  2598. int how = 0;
  2599. int error;
  2600. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  2601. return -EINVAL;
  2602. /*
  2603. * To use null names we require CAP_DAC_READ_SEARCH
  2604. * This ensures that not everyone will be able to create
  2605. * handlink using the passed filedescriptor.
  2606. */
  2607. if (flags & AT_EMPTY_PATH) {
  2608. if (!capable(CAP_DAC_READ_SEARCH))
  2609. return -ENOENT;
  2610. how = LOOKUP_EMPTY;
  2611. }
  2612. if (flags & AT_SYMLINK_FOLLOW)
  2613. how |= LOOKUP_FOLLOW;
  2614. error = user_path_at(olddfd, oldname, how, &old_path);
  2615. if (error)
  2616. return error;
  2617. new_dentry = user_path_create(newdfd, newname, &new_path, 0);
  2618. error = PTR_ERR(new_dentry);
  2619. if (IS_ERR(new_dentry))
  2620. goto out;
  2621. error = -EXDEV;
  2622. if (old_path.mnt != new_path.mnt)
  2623. goto out_dput;
  2624. error = mnt_want_write(new_path.mnt);
  2625. if (error)
  2626. goto out_dput;
  2627. error = security_path_link(old_path.dentry, &new_path, new_dentry);
  2628. if (error)
  2629. goto out_drop_write;
  2630. error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
  2631. out_drop_write:
  2632. mnt_drop_write(new_path.mnt);
  2633. out_dput:
  2634. dput(new_dentry);
  2635. mutex_unlock(&new_path.dentry->d_inode->i_mutex);
  2636. path_put(&new_path);
  2637. out:
  2638. path_put(&old_path);
  2639. return error;
  2640. }
  2641. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  2642. {
  2643. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2644. }
  2645. /*
  2646. * The worst of all namespace operations - renaming directory. "Perverted"
  2647. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2648. * Problems:
  2649. * a) we can get into loop creation. Check is done in is_subdir().
  2650. * b) race potential - two innocent renames can create a loop together.
  2651. * That's where 4.4 screws up. Current fix: serialization on
  2652. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2653. * story.
  2654. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2655. * And that - after we got ->i_mutex on parents (until then we don't know
  2656. * whether the target exists). Solution: try to be smart with locking
  2657. * order for inodes. We rely on the fact that tree topology may change
  2658. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2659. * move will be locked. Thus we can rank directories by the tree
  2660. * (ancestors first) and rank all non-directories after them.
  2661. * That works since everybody except rename does "lock parent, lookup,
  2662. * lock child" and rename is under ->s_vfs_rename_mutex.
  2663. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2664. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2665. * we'd better make sure that there's no link(2) for them.
  2666. * d) conversion from fhandle to dentry may come in the wrong moment - when
  2667. * we are removing the target. Solution: we will have to grab ->i_mutex
  2668. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2669. * ->i_mutex on parents, which works but leads to some truly excessive
  2670. * locking].
  2671. */
  2672. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2673. struct inode *new_dir, struct dentry *new_dentry)
  2674. {
  2675. int error = 0;
  2676. struct inode *target = new_dentry->d_inode;
  2677. /*
  2678. * If we are going to change the parent - check write permissions,
  2679. * we'll need to flip '..'.
  2680. */
  2681. if (new_dir != old_dir) {
  2682. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  2683. if (error)
  2684. return error;
  2685. }
  2686. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2687. if (error)
  2688. return error;
  2689. dget(new_dentry);
  2690. if (target)
  2691. mutex_lock(&target->i_mutex);
  2692. error = -EBUSY;
  2693. if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
  2694. goto out;
  2695. if (target)
  2696. shrink_dcache_parent(new_dentry);
  2697. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2698. if (error)
  2699. goto out;
  2700. if (target) {
  2701. target->i_flags |= S_DEAD;
  2702. dont_mount(new_dentry);
  2703. }
  2704. out:
  2705. if (target)
  2706. mutex_unlock(&target->i_mutex);
  2707. dput(new_dentry);
  2708. if (!error)
  2709. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2710. d_move(old_dentry,new_dentry);
  2711. return error;
  2712. }
  2713. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2714. struct inode *new_dir, struct dentry *new_dentry)
  2715. {
  2716. struct inode *target = new_dentry->d_inode;
  2717. int error;
  2718. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2719. if (error)
  2720. return error;
  2721. dget(new_dentry);
  2722. if (target)
  2723. mutex_lock(&target->i_mutex);
  2724. error = -EBUSY;
  2725. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2726. goto out;
  2727. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2728. if (error)
  2729. goto out;
  2730. if (target)
  2731. dont_mount(new_dentry);
  2732. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2733. d_move(old_dentry, new_dentry);
  2734. out:
  2735. if (target)
  2736. mutex_unlock(&target->i_mutex);
  2737. dput(new_dentry);
  2738. return error;
  2739. }
  2740. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2741. struct inode *new_dir, struct dentry *new_dentry)
  2742. {
  2743. int error;
  2744. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2745. const unsigned char *old_name;
  2746. if (old_dentry->d_inode == new_dentry->d_inode)
  2747. return 0;
  2748. error = may_delete(old_dir, old_dentry, is_dir);
  2749. if (error)
  2750. return error;
  2751. if (!new_dentry->d_inode)
  2752. error = may_create(new_dir, new_dentry);
  2753. else
  2754. error = may_delete(new_dir, new_dentry, is_dir);
  2755. if (error)
  2756. return error;
  2757. if (!old_dir->i_op->rename)
  2758. return -EPERM;
  2759. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2760. if (is_dir)
  2761. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2762. else
  2763. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2764. if (!error)
  2765. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  2766. new_dentry->d_inode, old_dentry);
  2767. fsnotify_oldname_free(old_name);
  2768. return error;
  2769. }
  2770. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  2771. int, newdfd, const char __user *, newname)
  2772. {
  2773. struct dentry *old_dir, *new_dir;
  2774. struct dentry *old_dentry, *new_dentry;
  2775. struct dentry *trap;
  2776. struct nameidata oldnd, newnd;
  2777. char *from;
  2778. char *to;
  2779. int error;
  2780. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  2781. if (error)
  2782. goto exit;
  2783. error = user_path_parent(newdfd, newname, &newnd, &to);
  2784. if (error)
  2785. goto exit1;
  2786. error = -EXDEV;
  2787. if (oldnd.path.mnt != newnd.path.mnt)
  2788. goto exit2;
  2789. old_dir = oldnd.path.dentry;
  2790. error = -EBUSY;
  2791. if (oldnd.last_type != LAST_NORM)
  2792. goto exit2;
  2793. new_dir = newnd.path.dentry;
  2794. if (newnd.last_type != LAST_NORM)
  2795. goto exit2;
  2796. oldnd.flags &= ~LOOKUP_PARENT;
  2797. newnd.flags &= ~LOOKUP_PARENT;
  2798. newnd.flags |= LOOKUP_RENAME_TARGET;
  2799. trap = lock_rename(new_dir, old_dir);
  2800. old_dentry = lookup_hash(&oldnd);
  2801. error = PTR_ERR(old_dentry);
  2802. if (IS_ERR(old_dentry))
  2803. goto exit3;
  2804. /* source must exist */
  2805. error = -ENOENT;
  2806. if (!old_dentry->d_inode)
  2807. goto exit4;
  2808. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2809. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2810. error = -ENOTDIR;
  2811. if (oldnd.last.name[oldnd.last.len])
  2812. goto exit4;
  2813. if (newnd.last.name[newnd.last.len])
  2814. goto exit4;
  2815. }
  2816. /* source should not be ancestor of target */
  2817. error = -EINVAL;
  2818. if (old_dentry == trap)
  2819. goto exit4;
  2820. new_dentry = lookup_hash(&newnd);
  2821. error = PTR_ERR(new_dentry);
  2822. if (IS_ERR(new_dentry))
  2823. goto exit4;
  2824. /* target should not be an ancestor of source */
  2825. error = -ENOTEMPTY;
  2826. if (new_dentry == trap)
  2827. goto exit5;
  2828. error = mnt_want_write(oldnd.path.mnt);
  2829. if (error)
  2830. goto exit5;
  2831. error = security_path_rename(&oldnd.path, old_dentry,
  2832. &newnd.path, new_dentry);
  2833. if (error)
  2834. goto exit6;
  2835. error = vfs_rename(old_dir->d_inode, old_dentry,
  2836. new_dir->d_inode, new_dentry);
  2837. exit6:
  2838. mnt_drop_write(oldnd.path.mnt);
  2839. exit5:
  2840. dput(new_dentry);
  2841. exit4:
  2842. dput(old_dentry);
  2843. exit3:
  2844. unlock_rename(new_dir, old_dir);
  2845. exit2:
  2846. path_put(&newnd.path);
  2847. putname(to);
  2848. exit1:
  2849. path_put(&oldnd.path);
  2850. putname(from);
  2851. exit:
  2852. return error;
  2853. }
  2854. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  2855. {
  2856. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2857. }
  2858. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2859. {
  2860. int len;
  2861. len = PTR_ERR(link);
  2862. if (IS_ERR(link))
  2863. goto out;
  2864. len = strlen(link);
  2865. if (len > (unsigned) buflen)
  2866. len = buflen;
  2867. if (copy_to_user(buffer, link, len))
  2868. len = -EFAULT;
  2869. out:
  2870. return len;
  2871. }
  2872. /*
  2873. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2874. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2875. * using) it for any given inode is up to filesystem.
  2876. */
  2877. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2878. {
  2879. struct nameidata nd;
  2880. void *cookie;
  2881. int res;
  2882. nd.depth = 0;
  2883. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2884. if (IS_ERR(cookie))
  2885. return PTR_ERR(cookie);
  2886. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2887. if (dentry->d_inode->i_op->put_link)
  2888. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2889. return res;
  2890. }
  2891. int vfs_follow_link(struct nameidata *nd, const char *link)
  2892. {
  2893. return __vfs_follow_link(nd, link);
  2894. }
  2895. /* get the link contents into pagecache */
  2896. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2897. {
  2898. char *kaddr;
  2899. struct page *page;
  2900. struct address_space *mapping = dentry->d_inode->i_mapping;
  2901. page = read_mapping_page(mapping, 0, NULL);
  2902. if (IS_ERR(page))
  2903. return (char*)page;
  2904. *ppage = page;
  2905. kaddr = kmap(page);
  2906. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  2907. return kaddr;
  2908. }
  2909. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2910. {
  2911. struct page *page = NULL;
  2912. char *s = page_getlink(dentry, &page);
  2913. int res = vfs_readlink(dentry,buffer,buflen,s);
  2914. if (page) {
  2915. kunmap(page);
  2916. page_cache_release(page);
  2917. }
  2918. return res;
  2919. }
  2920. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2921. {
  2922. struct page *page = NULL;
  2923. nd_set_link(nd, page_getlink(dentry, &page));
  2924. return page;
  2925. }
  2926. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2927. {
  2928. struct page *page = cookie;
  2929. if (page) {
  2930. kunmap(page);
  2931. page_cache_release(page);
  2932. }
  2933. }
  2934. /*
  2935. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  2936. */
  2937. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  2938. {
  2939. struct address_space *mapping = inode->i_mapping;
  2940. struct page *page;
  2941. void *fsdata;
  2942. int err;
  2943. char *kaddr;
  2944. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  2945. if (nofs)
  2946. flags |= AOP_FLAG_NOFS;
  2947. retry:
  2948. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  2949. flags, &page, &fsdata);
  2950. if (err)
  2951. goto fail;
  2952. kaddr = kmap_atomic(page, KM_USER0);
  2953. memcpy(kaddr, symname, len-1);
  2954. kunmap_atomic(kaddr, KM_USER0);
  2955. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  2956. page, fsdata);
  2957. if (err < 0)
  2958. goto fail;
  2959. if (err < len-1)
  2960. goto retry;
  2961. mark_inode_dirty(inode);
  2962. return 0;
  2963. fail:
  2964. return err;
  2965. }
  2966. int page_symlink(struct inode *inode, const char *symname, int len)
  2967. {
  2968. return __page_symlink(inode, symname, len,
  2969. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  2970. }
  2971. const struct inode_operations page_symlink_inode_operations = {
  2972. .readlink = generic_readlink,
  2973. .follow_link = page_follow_link_light,
  2974. .put_link = page_put_link,
  2975. };
  2976. EXPORT_SYMBOL(user_path_at);
  2977. EXPORT_SYMBOL(follow_down_one);
  2978. EXPORT_SYMBOL(follow_down);
  2979. EXPORT_SYMBOL(follow_up);
  2980. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2981. EXPORT_SYMBOL(getname);
  2982. EXPORT_SYMBOL(lock_rename);
  2983. EXPORT_SYMBOL(lookup_one_len);
  2984. EXPORT_SYMBOL(page_follow_link_light);
  2985. EXPORT_SYMBOL(page_put_link);
  2986. EXPORT_SYMBOL(page_readlink);
  2987. EXPORT_SYMBOL(__page_symlink);
  2988. EXPORT_SYMBOL(page_symlink);
  2989. EXPORT_SYMBOL(page_symlink_inode_operations);
  2990. EXPORT_SYMBOL(kern_path);
  2991. EXPORT_SYMBOL(vfs_path_lookup);
  2992. EXPORT_SYMBOL(inode_permission);
  2993. EXPORT_SYMBOL(unlock_rename);
  2994. EXPORT_SYMBOL(vfs_create);
  2995. EXPORT_SYMBOL(vfs_follow_link);
  2996. EXPORT_SYMBOL(vfs_link);
  2997. EXPORT_SYMBOL(vfs_mkdir);
  2998. EXPORT_SYMBOL(vfs_mknod);
  2999. EXPORT_SYMBOL(generic_permission);
  3000. EXPORT_SYMBOL(vfs_readlink);
  3001. EXPORT_SYMBOL(vfs_rename);
  3002. EXPORT_SYMBOL(vfs_rmdir);
  3003. EXPORT_SYMBOL(vfs_symlink);
  3004. EXPORT_SYMBOL(vfs_unlink);
  3005. EXPORT_SYMBOL(dentry_unhash);
  3006. EXPORT_SYMBOL(generic_readlink);