introduce ->iterate(), ctx->pos, dir_emit()
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / namei.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Some corrections by tytso.
9 */
10
11/* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
13 */
14/* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15 */
16
17#include <linux/init.h>
630d9c47 18#include <linux/export.h>
44696908 19#include <linux/kernel.h>
1da177e4
LT
20#include <linux/slab.h>
21#include <linux/fs.h>
22#include <linux/namei.h>
1da177e4 23#include <linux/pagemap.h>
0eeca283 24#include <linux/fsnotify.h>
1da177e4
LT
25#include <linux/personality.h>
26#include <linux/security.h>
6146f0d5 27#include <linux/ima.h>
1da177e4
LT
28#include <linux/syscalls.h>
29#include <linux/mount.h>
30#include <linux/audit.h>
16f7e0fe 31#include <linux/capability.h>
834f2a4a 32#include <linux/file.h>
5590ff0d 33#include <linux/fcntl.h>
08ce5f16 34#include <linux/device_cgroup.h>
5ad4e53b 35#include <linux/fs_struct.h>
e77819e5 36#include <linux/posix_acl.h>
d4c96061 37#include <linux/hash.h>
1da177e4
LT
38#include <asm/uaccess.h>
39
e81e3f4d 40#include "internal.h"
c7105365 41#include "mount.h"
e81e3f4d 42
1da177e4
LT
43/* [Feb-1997 T. Schoebel-Theuer]
44 * Fundamental changes in the pathname lookup mechanisms (namei)
45 * were necessary because of omirr. The reason is that omirr needs
46 * to know the _real_ pathname, not the user-supplied one, in case
47 * of symlinks (and also when transname replacements occur).
48 *
49 * The new code replaces the old recursive symlink resolution with
50 * an iterative one (in case of non-nested symlink chains). It does
51 * this with calls to <fs>_follow_link().
52 * As a side effect, dir_namei(), _namei() and follow_link() are now
53 * replaced with a single function lookup_dentry() that can handle all
54 * the special cases of the former code.
55 *
56 * With the new dcache, the pathname is stored at each inode, at least as
57 * long as the refcount of the inode is positive. As a side effect, the
58 * size of the dcache depends on the inode cache and thus is dynamic.
59 *
60 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
61 * resolution to correspond with current state of the code.
62 *
63 * Note that the symlink resolution is not *completely* iterative.
64 * There is still a significant amount of tail- and mid- recursion in
65 * the algorithm. Also, note that <fs>_readlink() is not used in
66 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
67 * may return different results than <fs>_follow_link(). Many virtual
68 * filesystems (including /proc) exhibit this behavior.
69 */
70
71/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
72 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
73 * and the name already exists in form of a symlink, try to create the new
74 * name indicated by the symlink. The old code always complained that the
75 * name already exists, due to not following the symlink even if its target
76 * is nonexistent. The new semantics affects also mknod() and link() when
25985edc 77 * the name is a symlink pointing to a non-existent name.
1da177e4
LT
78 *
79 * I don't know which semantics is the right one, since I have no access
80 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
81 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
82 * "old" one. Personally, I think the new semantics is much more logical.
83 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
84 * file does succeed in both HP-UX and SunOs, but not in Solaris
85 * and in the old Linux semantics.
86 */
87
88/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
89 * semantics. See the comments in "open_namei" and "do_link" below.
90 *
91 * [10-Sep-98 Alan Modra] Another symlink change.
92 */
93
94/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
95 * inside the path - always follow.
96 * in the last component in creation/removal/renaming - never follow.
97 * if LOOKUP_FOLLOW passed - follow.
98 * if the pathname has trailing slashes - follow.
99 * otherwise - don't follow.
100 * (applied in that order).
101 *
102 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
103 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
104 * During the 2.4 we need to fix the userland stuff depending on it -
105 * hopefully we will be able to get rid of that wart in 2.5. So far only
106 * XEmacs seems to be relying on it...
107 */
108/*
109 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
a11f3a05 110 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
1da177e4
LT
111 * any extra contention...
112 */
113
114/* In order to reduce some races, while at the same time doing additional
115 * checking and hopefully speeding things up, we copy filenames to the
116 * kernel data space before using them..
117 *
118 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
119 * PATH_MAX includes the nul terminator --RR.
120 */
91a27b2a 121void final_putname(struct filename *name)
1da177e4 122{
7950e385
JL
123 if (name->separate) {
124 __putname(name->name);
125 kfree(name);
126 } else {
127 __putname(name);
128 }
91a27b2a
JL
129}
130
7950e385
JL
131#define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename))
132
91a27b2a
JL
133static struct filename *
134getname_flags(const char __user *filename, int flags, int *empty)
135{
136 struct filename *result, *err;
3f9f0aa6 137 int len;
7950e385
JL
138 long max;
139 char *kname;
4043cde8 140
7ac86265
JL
141 result = audit_reusename(filename);
142 if (result)
143 return result;
144
7950e385 145 result = __getname();
3f9f0aa6 146 if (unlikely(!result))
4043cde8
EP
147 return ERR_PTR(-ENOMEM);
148
7950e385
JL
149 /*
150 * First, try to embed the struct filename inside the names_cache
151 * allocation
152 */
153 kname = (char *)result + sizeof(*result);
91a27b2a 154 result->name = kname;
7950e385
JL
155 result->separate = false;
156 max = EMBEDDED_NAME_MAX;
157
158recopy:
159 len = strncpy_from_user(kname, filename, max);
91a27b2a
JL
160 if (unlikely(len < 0)) {
161 err = ERR_PTR(len);
3f9f0aa6 162 goto error;
91a27b2a 163 }
3f9f0aa6 164
7950e385
JL
165 /*
166 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
167 * separate struct filename so we can dedicate the entire
168 * names_cache allocation for the pathname, and re-do the copy from
169 * userland.
170 */
171 if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) {
172 kname = (char *)result;
173
174 result = kzalloc(sizeof(*result), GFP_KERNEL);
175 if (!result) {
176 err = ERR_PTR(-ENOMEM);
177 result = (struct filename *)kname;
178 goto error;
179 }
180 result->name = kname;
181 result->separate = true;
182 max = PATH_MAX;
183 goto recopy;
184 }
185
3f9f0aa6
LT
186 /* The empty path is special. */
187 if (unlikely(!len)) {
188 if (empty)
4043cde8 189 *empty = 1;
3f9f0aa6
LT
190 err = ERR_PTR(-ENOENT);
191 if (!(flags & LOOKUP_EMPTY))
192 goto error;
1da177e4 193 }
3f9f0aa6
LT
194
195 err = ERR_PTR(-ENAMETOOLONG);
7950e385
JL
196 if (unlikely(len >= PATH_MAX))
197 goto error;
198
199 result->uptr = filename;
200 audit_getname(result);
201 return result;
3f9f0aa6
LT
202
203error:
7950e385 204 final_putname(result);
3f9f0aa6 205 return err;
1da177e4
LT
206}
207
91a27b2a
JL
208struct filename *
209getname(const char __user * filename)
f52e0c11 210{
f7493e5d 211 return getname_flags(filename, 0, NULL);
f52e0c11 212}
91a27b2a 213EXPORT_SYMBOL(getname);
f52e0c11 214
1da177e4 215#ifdef CONFIG_AUDITSYSCALL
91a27b2a 216void putname(struct filename *name)
1da177e4 217{
5ac3a9c2 218 if (unlikely(!audit_dummy_context()))
91a27b2a
JL
219 return audit_putname(name);
220 final_putname(name);
1da177e4 221}
1da177e4
LT
222#endif
223
e77819e5
LT
224static int check_acl(struct inode *inode, int mask)
225{
84635d68 226#ifdef CONFIG_FS_POSIX_ACL
e77819e5
LT
227 struct posix_acl *acl;
228
e77819e5 229 if (mask & MAY_NOT_BLOCK) {
3567866b
AV
230 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
231 if (!acl)
e77819e5 232 return -EAGAIN;
3567866b
AV
233 /* no ->get_acl() calls in RCU mode... */
234 if (acl == ACL_NOT_CACHED)
235 return -ECHILD;
206b1d09 236 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
e77819e5
LT
237 }
238
239 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
240
241 /*
4e34e719
CH
242 * A filesystem can force a ACL callback by just never filling the
243 * ACL cache. But normally you'd fill the cache either at inode
244 * instantiation time, or on the first ->get_acl call.
e77819e5 245 *
4e34e719
CH
246 * If the filesystem doesn't have a get_acl() function at all, we'll
247 * just create the negative cache entry.
e77819e5
LT
248 */
249 if (acl == ACL_NOT_CACHED) {
4e34e719
CH
250 if (inode->i_op->get_acl) {
251 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
252 if (IS_ERR(acl))
253 return PTR_ERR(acl);
254 } else {
255 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
256 return -EAGAIN;
257 }
e77819e5
LT
258 }
259
260 if (acl) {
261 int error = posix_acl_permission(inode, acl, mask);
262 posix_acl_release(acl);
263 return error;
264 }
84635d68 265#endif
e77819e5
LT
266
267 return -EAGAIN;
268}
269
5909ccaa 270/*
948409c7 271 * This does the basic permission checking
1da177e4 272 */
7e40145e 273static int acl_permission_check(struct inode *inode, int mask)
1da177e4 274{
26cf46be 275 unsigned int mode = inode->i_mode;
1da177e4 276
8e96e3b7 277 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
1da177e4
LT
278 mode >>= 6;
279 else {
e77819e5 280 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
7e40145e 281 int error = check_acl(inode, mask);
b74c79e9
NP
282 if (error != -EAGAIN)
283 return error;
1da177e4
LT
284 }
285
286 if (in_group_p(inode->i_gid))
287 mode >>= 3;
288 }
289
290 /*
291 * If the DACs are ok we don't need any capability check.
292 */
9c2c7039 293 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
1da177e4 294 return 0;
5909ccaa
LT
295 return -EACCES;
296}
297
298/**
b74c79e9 299 * generic_permission - check for access rights on a Posix-like filesystem
5909ccaa 300 * @inode: inode to check access rights for
8fd90c8d 301 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
5909ccaa
LT
302 *
303 * Used to check for read/write/execute permissions on a file.
304 * We use "fsuid" for this, letting us set arbitrary permissions
305 * for filesystem access without changing the "normal" uids which
b74c79e9
NP
306 * are used for other things.
307 *
308 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
309 * request cannot be satisfied (eg. requires blocking or too much complexity).
310 * It would then be called again in ref-walk mode.
5909ccaa 311 */
2830ba7f 312int generic_permission(struct inode *inode, int mask)
5909ccaa
LT
313{
314 int ret;
315
316 /*
948409c7 317 * Do the basic permission checks.
5909ccaa 318 */
7e40145e 319 ret = acl_permission_check(inode, mask);
5909ccaa
LT
320 if (ret != -EACCES)
321 return ret;
1da177e4 322
d594e7ec
AV
323 if (S_ISDIR(inode->i_mode)) {
324 /* DACs are overridable for directories */
4f80c6c1 325 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
d594e7ec
AV
326 return 0;
327 if (!(mask & MAY_WRITE))
4f80c6c1
AL
328 if (capable_wrt_inode_uidgid(inode,
329 CAP_DAC_READ_SEARCH))
d594e7ec
AV
330 return 0;
331 return -EACCES;
332 }
1da177e4
LT
333 /*
334 * Read/write DACs are always overridable.
d594e7ec
AV
335 * Executable DACs are overridable when there is
336 * at least one exec bit set.
1da177e4 337 */
d594e7ec 338 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
4f80c6c1 339 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
1da177e4
LT
340 return 0;
341
342 /*
343 * Searching includes executable on directories, else just read.
344 */
7ea66001 345 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
d594e7ec 346 if (mask == MAY_READ)
4f80c6c1 347 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
1da177e4
LT
348 return 0;
349
350 return -EACCES;
351}
352
3ddcd056
LT
353/*
354 * We _really_ want to just do "generic_permission()" without
355 * even looking at the inode->i_op values. So we keep a cache
356 * flag in inode->i_opflags, that says "this has not special
357 * permission function, use the fast case".
358 */
359static inline int do_inode_permission(struct inode *inode, int mask)
360{
361 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
362 if (likely(inode->i_op->permission))
363 return inode->i_op->permission(inode, mask);
364
365 /* This gets set once for the inode lifetime */
366 spin_lock(&inode->i_lock);
367 inode->i_opflags |= IOP_FASTPERM;
368 spin_unlock(&inode->i_lock);
369 }
370 return generic_permission(inode, mask);
371}
372
cb23beb5 373/**
0bdaea90
DH
374 * __inode_permission - Check for access rights to a given inode
375 * @inode: Inode to check permission on
376 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
cb23beb5 377 *
0bdaea90 378 * Check for read/write/execute permissions on an inode.
948409c7
AG
379 *
380 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
0bdaea90
DH
381 *
382 * This does not check for a read-only file system. You probably want
383 * inode_permission().
cb23beb5 384 */
0bdaea90 385int __inode_permission(struct inode *inode, int mask)
1da177e4 386{
e6305c43 387 int retval;
1da177e4 388
3ddcd056 389 if (unlikely(mask & MAY_WRITE)) {
1da177e4
LT
390 /*
391 * Nobody gets write access to an immutable file.
392 */
393 if (IS_IMMUTABLE(inode))
394 return -EACCES;
395 }
396
3ddcd056 397 retval = do_inode_permission(inode, mask);
1da177e4
LT
398 if (retval)
399 return retval;
400
08ce5f16
SH
401 retval = devcgroup_inode_permission(inode, mask);
402 if (retval)
403 return retval;
404
d09ca739 405 return security_inode_permission(inode, mask);
1da177e4
LT
406}
407
0bdaea90
DH
408/**
409 * sb_permission - Check superblock-level permissions
410 * @sb: Superblock of inode to check permission on
55852635 411 * @inode: Inode to check permission on
0bdaea90
DH
412 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
413 *
414 * Separate out file-system wide checks from inode-specific permission checks.
415 */
416static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
417{
418 if (unlikely(mask & MAY_WRITE)) {
419 umode_t mode = inode->i_mode;
420
421 /* Nobody gets write access to a read-only fs. */
422 if ((sb->s_flags & MS_RDONLY) &&
423 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
424 return -EROFS;
425 }
426 return 0;
427}
428
429/**
430 * inode_permission - Check for access rights to a given inode
431 * @inode: Inode to check permission on
432 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
433 *
434 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
435 * this, letting us set arbitrary permissions for filesystem access without
436 * changing the "normal" UIDs which are used for other things.
437 *
438 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
439 */
440int inode_permission(struct inode *inode, int mask)
441{
442 int retval;
443
444 retval = sb_permission(inode->i_sb, inode, mask);
445 if (retval)
446 return retval;
447 return __inode_permission(inode, mask);
448}
449
5dd784d0
JB
450/**
451 * path_get - get a reference to a path
452 * @path: path to get the reference to
453 *
454 * Given a path increment the reference count to the dentry and the vfsmount.
455 */
dcf787f3 456void path_get(const struct path *path)
5dd784d0
JB
457{
458 mntget(path->mnt);
459 dget(path->dentry);
460}
461EXPORT_SYMBOL(path_get);
462
1d957f9b
JB
463/**
464 * path_put - put a reference to a path
465 * @path: path to put the reference to
466 *
467 * Given a path decrement the reference count to the dentry and the vfsmount.
468 */
dcf787f3 469void path_put(const struct path *path)
1da177e4 470{
1d957f9b
JB
471 dput(path->dentry);
472 mntput(path->mnt);
1da177e4 473}
1d957f9b 474EXPORT_SYMBOL(path_put);
1da177e4 475
fa6ef735
EB
476/**
477 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
478 * @path: nameidate to verify
479 *
480 * Rename can sometimes move a file or directory outside of a bind
481 * mount, path_connected allows those cases to be detected.
482 */
483static bool path_connected(const struct path *path)
484{
485 struct vfsmount *mnt = path->mnt;
486
487 /* Only bind mounts can have disconnected paths */
488 if (mnt->mnt_root == mnt->mnt_sb->s_root)
489 return true;
490
491 return is_subdir(path->dentry, mnt->mnt_root);
492}
493
19660af7 494/*
31e6b01f 495 * Path walking has 2 modes, rcu-walk and ref-walk (see
19660af7
AV
496 * Documentation/filesystems/path-lookup.txt). In situations when we can't
497 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
498 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
499 * mode. Refcounts are grabbed at the last known good point before rcu-walk
500 * got stuck, so ref-walk may continue from there. If this is not successful
501 * (eg. a seqcount has changed), then failure is returned and it's up to caller
502 * to restart the path walk from the beginning in ref-walk mode.
31e6b01f 503 */
31e6b01f 504
32a7991b
AV
505static inline void lock_rcu_walk(void)
506{
507 br_read_lock(&vfsmount_lock);
508 rcu_read_lock();
509}
510
511static inline void unlock_rcu_walk(void)
512{
513 rcu_read_unlock();
514 br_read_unlock(&vfsmount_lock);
515}
516
31e6b01f 517/**
19660af7
AV
518 * unlazy_walk - try to switch to ref-walk mode.
519 * @nd: nameidata pathwalk data
520 * @dentry: child of nd->path.dentry or NULL
39191628 521 * Returns: 0 on success, -ECHILD on failure
31e6b01f 522 *
19660af7
AV
523 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
524 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
525 * @nd or NULL. Must be called from rcu-walk context.
31e6b01f 526 */
19660af7 527static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
31e6b01f
NP
528{
529 struct fs_struct *fs = current->fs;
530 struct dentry *parent = nd->path.dentry;
5b6ca027 531 int want_root = 0;
31e6b01f
NP
532
533 BUG_ON(!(nd->flags & LOOKUP_RCU));
5b6ca027
AV
534 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
535 want_root = 1;
31e6b01f
NP
536 spin_lock(&fs->lock);
537 if (nd->root.mnt != fs->root.mnt ||
538 nd->root.dentry != fs->root.dentry)
539 goto err_root;
540 }
541 spin_lock(&parent->d_lock);
19660af7
AV
542 if (!dentry) {
543 if (!__d_rcu_to_refcount(parent, nd->seq))
544 goto err_parent;
545 BUG_ON(nd->inode != parent->d_inode);
546 } else {
94c0d4ec
AV
547 if (dentry->d_parent != parent)
548 goto err_parent;
19660af7
AV
549 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
550 if (!__d_rcu_to_refcount(dentry, nd->seq))
551 goto err_child;
552 /*
553 * If the sequence check on the child dentry passed, then
554 * the child has not been removed from its parent. This
555 * means the parent dentry must be valid and able to take
556 * a reference at this point.
557 */
558 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
559 BUG_ON(!parent->d_count);
560 parent->d_count++;
561 spin_unlock(&dentry->d_lock);
562 }
31e6b01f 563 spin_unlock(&parent->d_lock);
5b6ca027 564 if (want_root) {
31e6b01f
NP
565 path_get(&nd->root);
566 spin_unlock(&fs->lock);
567 }
568 mntget(nd->path.mnt);
569
32a7991b 570 unlock_rcu_walk();
31e6b01f
NP
571 nd->flags &= ~LOOKUP_RCU;
572 return 0;
19660af7
AV
573
574err_child:
31e6b01f 575 spin_unlock(&dentry->d_lock);
19660af7 576err_parent:
31e6b01f
NP
577 spin_unlock(&parent->d_lock);
578err_root:
5b6ca027 579 if (want_root)
31e6b01f
NP
580 spin_unlock(&fs->lock);
581 return -ECHILD;
582}
583
4ce16ef3 584static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
34286d66 585{
4ce16ef3 586 return dentry->d_op->d_revalidate(dentry, flags);
34286d66
NP
587}
588
9f1fafee
AV
589/**
590 * complete_walk - successful completion of path walk
591 * @nd: pointer nameidata
39159de2 592 *
9f1fafee
AV
593 * If we had been in RCU mode, drop out of it and legitimize nd->path.
594 * Revalidate the final result, unless we'd already done that during
595 * the path walk or the filesystem doesn't ask for it. Return 0 on
596 * success, -error on failure. In case of failure caller does not
597 * need to drop nd->path.
39159de2 598 */
9f1fafee 599static int complete_walk(struct nameidata *nd)
39159de2 600{
16c2cd71 601 struct dentry *dentry = nd->path.dentry;
39159de2 602 int status;
39159de2 603
9f1fafee
AV
604 if (nd->flags & LOOKUP_RCU) {
605 nd->flags &= ~LOOKUP_RCU;
606 if (!(nd->flags & LOOKUP_ROOT))
607 nd->root.mnt = NULL;
608 spin_lock(&dentry->d_lock);
609 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
610 spin_unlock(&dentry->d_lock);
32a7991b 611 unlock_rcu_walk();
9f1fafee
AV
612 return -ECHILD;
613 }
614 BUG_ON(nd->inode != dentry->d_inode);
615 spin_unlock(&dentry->d_lock);
616 mntget(nd->path.mnt);
32a7991b 617 unlock_rcu_walk();
9f1fafee
AV
618 }
619
16c2cd71
AV
620 if (likely(!(nd->flags & LOOKUP_JUMPED)))
621 return 0;
622
ecf3d1f1 623 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
39159de2
JL
624 return 0;
625
ecf3d1f1 626 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
39159de2
JL
627 if (status > 0)
628 return 0;
629
16c2cd71 630 if (!status)
39159de2 631 status = -ESTALE;
16c2cd71 632
9f1fafee 633 path_put(&nd->path);
39159de2
JL
634 return status;
635}
636
2a737871
AV
637static __always_inline void set_root(struct nameidata *nd)
638{
f7ad3c6b
MS
639 if (!nd->root.mnt)
640 get_fs_root(current->fs, &nd->root);
2a737871
AV
641}
642
6de88d72
AV
643static int link_path_walk(const char *, struct nameidata *);
644
31e6b01f
NP
645static __always_inline void set_root_rcu(struct nameidata *nd)
646{
647 if (!nd->root.mnt) {
648 struct fs_struct *fs = current->fs;
c28cc364
NP
649 unsigned seq;
650
651 do {
652 seq = read_seqcount_begin(&fs->seq);
653 nd->root = fs->root;
c1530019 654 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
c28cc364 655 } while (read_seqcount_retry(&fs->seq, seq));
31e6b01f
NP
656 }
657}
658
f1662356 659static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
1da177e4 660{
31e6b01f
NP
661 int ret;
662
1da177e4
LT
663 if (IS_ERR(link))
664 goto fail;
665
666 if (*link == '/') {
2a737871 667 set_root(nd);
1d957f9b 668 path_put(&nd->path);
2a737871
AV
669 nd->path = nd->root;
670 path_get(&nd->root);
16c2cd71 671 nd->flags |= LOOKUP_JUMPED;
1da177e4 672 }
31e6b01f 673 nd->inode = nd->path.dentry->d_inode;
b4091d5f 674
31e6b01f
NP
675 ret = link_path_walk(link, nd);
676 return ret;
1da177e4 677fail:
1d957f9b 678 path_put(&nd->path);
1da177e4
LT
679 return PTR_ERR(link);
680}
681
1d957f9b 682static void path_put_conditional(struct path *path, struct nameidata *nd)
051d3812
IK
683{
684 dput(path->dentry);
4ac91378 685 if (path->mnt != nd->path.mnt)
051d3812
IK
686 mntput(path->mnt);
687}
688
7b9337aa
NP
689static inline void path_to_nameidata(const struct path *path,
690 struct nameidata *nd)
051d3812 691{
31e6b01f
NP
692 if (!(nd->flags & LOOKUP_RCU)) {
693 dput(nd->path.dentry);
694 if (nd->path.mnt != path->mnt)
695 mntput(nd->path.mnt);
9a229683 696 }
31e6b01f 697 nd->path.mnt = path->mnt;
4ac91378 698 nd->path.dentry = path->dentry;
051d3812
IK
699}
700
b5fb63c1
CH
701/*
702 * Helper to directly jump to a known parsed path from ->follow_link,
703 * caller must have taken a reference to path beforehand.
704 */
705void nd_jump_link(struct nameidata *nd, struct path *path)
706{
707 path_put(&nd->path);
708
709 nd->path = *path;
710 nd->inode = nd->path.dentry->d_inode;
711 nd->flags |= LOOKUP_JUMPED;
b5fb63c1
CH
712}
713
574197e0
AV
714static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
715{
716 struct inode *inode = link->dentry->d_inode;
6d7b5aae 717 if (inode->i_op->put_link)
574197e0
AV
718 inode->i_op->put_link(link->dentry, nd, cookie);
719 path_put(link);
720}
721
561ec64a
LT
722int sysctl_protected_symlinks __read_mostly = 0;
723int sysctl_protected_hardlinks __read_mostly = 0;
800179c9
KC
724
725/**
726 * may_follow_link - Check symlink following for unsafe situations
727 * @link: The path of the symlink
55852635 728 * @nd: nameidata pathwalk data
800179c9
KC
729 *
730 * In the case of the sysctl_protected_symlinks sysctl being enabled,
731 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
732 * in a sticky world-writable directory. This is to protect privileged
733 * processes from failing races against path names that may change out
734 * from under them by way of other users creating malicious symlinks.
735 * It will permit symlinks to be followed only when outside a sticky
736 * world-writable directory, or when the uid of the symlink and follower
737 * match, or when the directory owner matches the symlink's owner.
738 *
739 * Returns 0 if following the symlink is allowed, -ve on error.
740 */
741static inline int may_follow_link(struct path *link, struct nameidata *nd)
742{
743 const struct inode *inode;
744 const struct inode *parent;
745
746 if (!sysctl_protected_symlinks)
747 return 0;
748
749 /* Allowed if owner and follower match. */
750 inode = link->dentry->d_inode;
81abe27b 751 if (uid_eq(current_cred()->fsuid, inode->i_uid))
800179c9
KC
752 return 0;
753
754 /* Allowed if parent directory not sticky and world-writable. */
755 parent = nd->path.dentry->d_inode;
756 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
757 return 0;
758
759 /* Allowed if parent directory and link owner match. */
81abe27b 760 if (uid_eq(parent->i_uid, inode->i_uid))
800179c9
KC
761 return 0;
762
ffd8d101 763 audit_log_link_denied("follow_link", link);
800179c9
KC
764 path_put_conditional(link, nd);
765 path_put(&nd->path);
766 return -EACCES;
767}
768
769/**
770 * safe_hardlink_source - Check for safe hardlink conditions
771 * @inode: the source inode to hardlink from
772 *
773 * Return false if at least one of the following conditions:
774 * - inode is not a regular file
775 * - inode is setuid
776 * - inode is setgid and group-exec
777 * - access failure for read and write
778 *
779 * Otherwise returns true.
780 */
781static bool safe_hardlink_source(struct inode *inode)
782{
783 umode_t mode = inode->i_mode;
784
785 /* Special files should not get pinned to the filesystem. */
786 if (!S_ISREG(mode))
787 return false;
788
789 /* Setuid files should not get pinned to the filesystem. */
790 if (mode & S_ISUID)
791 return false;
792
793 /* Executable setgid files should not get pinned to the filesystem. */
794 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
795 return false;
796
797 /* Hardlinking to unreadable or unwritable sources is dangerous. */
798 if (inode_permission(inode, MAY_READ | MAY_WRITE))
799 return false;
800
801 return true;
802}
803
804/**
805 * may_linkat - Check permissions for creating a hardlink
806 * @link: the source to hardlink from
807 *
808 * Block hardlink when all of:
809 * - sysctl_protected_hardlinks enabled
810 * - fsuid does not match inode
811 * - hardlink source is unsafe (see safe_hardlink_source() above)
812 * - not CAP_FOWNER
813 *
814 * Returns 0 if successful, -ve on error.
815 */
816static int may_linkat(struct path *link)
817{
818 const struct cred *cred;
819 struct inode *inode;
820
821 if (!sysctl_protected_hardlinks)
822 return 0;
823
824 cred = current_cred();
825 inode = link->dentry->d_inode;
826
827 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
828 * otherwise, it must be a safe source.
829 */
81abe27b 830 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
800179c9
KC
831 capable(CAP_FOWNER))
832 return 0;
833
a51d9eaa 834 audit_log_link_denied("linkat", link);
800179c9
KC
835 return -EPERM;
836}
837
def4af30 838static __always_inline int
574197e0 839follow_link(struct path *link, struct nameidata *nd, void **p)
1da177e4 840{
7b9337aa 841 struct dentry *dentry = link->dentry;
6d7b5aae
AV
842 int error;
843 char *s;
1da177e4 844
844a3917
AV
845 BUG_ON(nd->flags & LOOKUP_RCU);
846
0e794589
AV
847 if (link->mnt == nd->path.mnt)
848 mntget(link->mnt);
849
6d7b5aae
AV
850 error = -ELOOP;
851 if (unlikely(current->total_link_count >= 40))
852 goto out_put_nd_path;
853
574197e0
AV
854 cond_resched();
855 current->total_link_count++;
856
68ac1234 857 touch_atime(link);
1da177e4 858 nd_set_link(nd, NULL);
cd4e91d3 859
36f3b4f6 860 error = security_inode_follow_link(link->dentry, nd);
6d7b5aae
AV
861 if (error)
862 goto out_put_nd_path;
36f3b4f6 863
86acdca1 864 nd->last_type = LAST_BIND;
def4af30
AV
865 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
866 error = PTR_ERR(*p);
6d7b5aae 867 if (IS_ERR(*p))
408ef013 868 goto out_put_nd_path;
6d7b5aae
AV
869
870 error = 0;
871 s = nd_get_link(nd);
872 if (s) {
873 error = __vfs_follow_link(nd, s);
b5fb63c1
CH
874 if (unlikely(error))
875 put_link(nd, link, *p);
1da177e4 876 }
6d7b5aae
AV
877
878 return error;
879
880out_put_nd_path:
98f6ef64 881 *p = NULL;
6d7b5aae 882 path_put(&nd->path);
6d7b5aae 883 path_put(link);
1da177e4
LT
884 return error;
885}
886
31e6b01f
NP
887static int follow_up_rcu(struct path *path)
888{
0714a533
AV
889 struct mount *mnt = real_mount(path->mnt);
890 struct mount *parent;
31e6b01f
NP
891 struct dentry *mountpoint;
892
0714a533
AV
893 parent = mnt->mnt_parent;
894 if (&parent->mnt == path->mnt)
31e6b01f 895 return 0;
a73324da 896 mountpoint = mnt->mnt_mountpoint;
31e6b01f 897 path->dentry = mountpoint;
0714a533 898 path->mnt = &parent->mnt;
31e6b01f
NP
899 return 1;
900}
901
f015f126
DH
902/*
903 * follow_up - Find the mountpoint of path's vfsmount
904 *
905 * Given a path, find the mountpoint of its source file system.
906 * Replace @path with the path of the mountpoint in the parent mount.
907 * Up is towards /.
908 *
909 * Return 1 if we went up a level and 0 if we were already at the
910 * root.
911 */
bab77ebf 912int follow_up(struct path *path)
1da177e4 913{
0714a533
AV
914 struct mount *mnt = real_mount(path->mnt);
915 struct mount *parent;
1da177e4 916 struct dentry *mountpoint;
99b7db7b 917
962830df 918 br_read_lock(&vfsmount_lock);
0714a533 919 parent = mnt->mnt_parent;
3c0a6163 920 if (parent == mnt) {
962830df 921 br_read_unlock(&vfsmount_lock);
1da177e4
LT
922 return 0;
923 }
0714a533 924 mntget(&parent->mnt);
a73324da 925 mountpoint = dget(mnt->mnt_mountpoint);
962830df 926 br_read_unlock(&vfsmount_lock);
bab77ebf
AV
927 dput(path->dentry);
928 path->dentry = mountpoint;
929 mntput(path->mnt);
0714a533 930 path->mnt = &parent->mnt;
1da177e4
LT
931 return 1;
932}
933
b5c84bf6 934/*
9875cf80
DH
935 * Perform an automount
936 * - return -EISDIR to tell follow_managed() to stop and return the path we
937 * were called with.
1da177e4 938 */
9875cf80
DH
939static int follow_automount(struct path *path, unsigned flags,
940 bool *need_mntput)
31e6b01f 941{
9875cf80 942 struct vfsmount *mnt;
ea5b778a 943 int err;
9875cf80
DH
944
945 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
946 return -EREMOTE;
947
0ec26fd0
MS
948 /* We don't want to mount if someone's just doing a stat -
949 * unless they're stat'ing a directory and appended a '/' to
950 * the name.
951 *
952 * We do, however, want to mount if someone wants to open or
953 * create a file of any type under the mountpoint, wants to
954 * traverse through the mountpoint or wants to open the
955 * mounted directory. Also, autofs may mark negative dentries
956 * as being automount points. These will need the attentions
957 * of the daemon to instantiate them before they can be used.
9875cf80 958 */
0ec26fd0 959 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
d94c177b 960 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
0ec26fd0
MS
961 path->dentry->d_inode)
962 return -EISDIR;
963
9875cf80
DH
964 current->total_link_count++;
965 if (current->total_link_count >= 40)
966 return -ELOOP;
967
968 mnt = path->dentry->d_op->d_automount(path);
969 if (IS_ERR(mnt)) {
970 /*
971 * The filesystem is allowed to return -EISDIR here to indicate
972 * it doesn't want to automount. For instance, autofs would do
973 * this so that its userspace daemon can mount on this dentry.
974 *
975 * However, we can only permit this if it's a terminal point in
976 * the path being looked up; if it wasn't then the remainder of
977 * the path is inaccessible and we should say so.
978 */
49084c3b 979 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
9875cf80
DH
980 return -EREMOTE;
981 return PTR_ERR(mnt);
31e6b01f 982 }
ea5b778a 983
9875cf80
DH
984 if (!mnt) /* mount collision */
985 return 0;
31e6b01f 986
8aef1884
AV
987 if (!*need_mntput) {
988 /* lock_mount() may release path->mnt on error */
989 mntget(path->mnt);
990 *need_mntput = true;
991 }
19a167af 992 err = finish_automount(mnt, path);
9875cf80 993
ea5b778a
DH
994 switch (err) {
995 case -EBUSY:
996 /* Someone else made a mount here whilst we were busy */
19a167af 997 return 0;
ea5b778a 998 case 0:
8aef1884 999 path_put(path);
ea5b778a
DH
1000 path->mnt = mnt;
1001 path->dentry = dget(mnt->mnt_root);
ea5b778a 1002 return 0;
19a167af
AV
1003 default:
1004 return err;
ea5b778a 1005 }
19a167af 1006
463ffb2e
AV
1007}
1008
9875cf80
DH
1009/*
1010 * Handle a dentry that is managed in some way.
cc53ce53 1011 * - Flagged for transit management (autofs)
9875cf80
DH
1012 * - Flagged as mountpoint
1013 * - Flagged as automount point
1014 *
1015 * This may only be called in refwalk mode.
1016 *
1017 * Serialization is taken care of in namespace.c
1018 */
1019static int follow_managed(struct path *path, unsigned flags)
1da177e4 1020{
8aef1884 1021 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
9875cf80
DH
1022 unsigned managed;
1023 bool need_mntput = false;
8aef1884 1024 int ret = 0;
9875cf80
DH
1025
1026 /* Given that we're not holding a lock here, we retain the value in a
1027 * local variable for each dentry as we look at it so that we don't see
1028 * the components of that value change under us */
1029 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1030 managed &= DCACHE_MANAGED_DENTRY,
1031 unlikely(managed != 0)) {
cc53ce53
DH
1032 /* Allow the filesystem to manage the transit without i_mutex
1033 * being held. */
1034 if (managed & DCACHE_MANAGE_TRANSIT) {
1035 BUG_ON(!path->dentry->d_op);
1036 BUG_ON(!path->dentry->d_op->d_manage);
1aed3e42 1037 ret = path->dentry->d_op->d_manage(path->dentry, false);
cc53ce53 1038 if (ret < 0)
8aef1884 1039 break;
cc53ce53
DH
1040 }
1041
9875cf80
DH
1042 /* Transit to a mounted filesystem. */
1043 if (managed & DCACHE_MOUNTED) {
1044 struct vfsmount *mounted = lookup_mnt(path);
1045 if (mounted) {
1046 dput(path->dentry);
1047 if (need_mntput)
1048 mntput(path->mnt);
1049 path->mnt = mounted;
1050 path->dentry = dget(mounted->mnt_root);
1051 need_mntput = true;
1052 continue;
1053 }
1054
1055 /* Something is mounted on this dentry in another
1056 * namespace and/or whatever was mounted there in this
1057 * namespace got unmounted before we managed to get the
1058 * vfsmount_lock */
1059 }
1060
1061 /* Handle an automount point */
1062 if (managed & DCACHE_NEED_AUTOMOUNT) {
1063 ret = follow_automount(path, flags, &need_mntput);
1064 if (ret < 0)
8aef1884 1065 break;
9875cf80
DH
1066 continue;
1067 }
1068
1069 /* We didn't change the current path point */
1070 break;
1da177e4 1071 }
8aef1884
AV
1072
1073 if (need_mntput && path->mnt == mnt)
1074 mntput(path->mnt);
1075 if (ret == -EISDIR)
1076 ret = 0;
a3fbbde7 1077 return ret < 0 ? ret : need_mntput;
1da177e4
LT
1078}
1079
cc53ce53 1080int follow_down_one(struct path *path)
1da177e4
LT
1081{
1082 struct vfsmount *mounted;
1083
1c755af4 1084 mounted = lookup_mnt(path);
1da177e4 1085 if (mounted) {
9393bd07
AV
1086 dput(path->dentry);
1087 mntput(path->mnt);
1088 path->mnt = mounted;
1089 path->dentry = dget(mounted->mnt_root);
1da177e4
LT
1090 return 1;
1091 }
1092 return 0;
1093}
1094
62a7375e
IK
1095static inline bool managed_dentry_might_block(struct dentry *dentry)
1096{
1097 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1098 dentry->d_op->d_manage(dentry, true) < 0);
1099}
1100
9875cf80 1101/*
287548e4
AV
1102 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1103 * we meet a managed dentry that would need blocking.
9875cf80
DH
1104 */
1105static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
287548e4 1106 struct inode **inode)
9875cf80 1107{
62a7375e 1108 for (;;) {
c7105365 1109 struct mount *mounted;
62a7375e
IK
1110 /*
1111 * Don't forget we might have a non-mountpoint managed dentry
1112 * that wants to block transit.
1113 */
287548e4 1114 if (unlikely(managed_dentry_might_block(path->dentry)))
ab90911f 1115 return false;
62a7375e
IK
1116
1117 if (!d_mountpoint(path->dentry))
1118 break;
1119
9875cf80
DH
1120 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1121 if (!mounted)
1122 break;
c7105365
AV
1123 path->mnt = &mounted->mnt;
1124 path->dentry = mounted->mnt.mnt_root;
a3fbbde7 1125 nd->flags |= LOOKUP_JUMPED;
9875cf80 1126 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
59430262
LT
1127 /*
1128 * Update the inode too. We don't need to re-check the
1129 * dentry sequence number here after this d_inode read,
1130 * because a mount-point is always pinned.
1131 */
1132 *inode = path->dentry->d_inode;
9875cf80 1133 }
9875cf80
DH
1134 return true;
1135}
1136
dea39376 1137static void follow_mount_rcu(struct nameidata *nd)
287548e4 1138{
dea39376 1139 while (d_mountpoint(nd->path.dentry)) {
c7105365 1140 struct mount *mounted;
dea39376 1141 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
287548e4
AV
1142 if (!mounted)
1143 break;
c7105365
AV
1144 nd->path.mnt = &mounted->mnt;
1145 nd->path.dentry = mounted->mnt.mnt_root;
dea39376 1146 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
287548e4
AV
1147 }
1148}
1149
31e6b01f
NP
1150static int follow_dotdot_rcu(struct nameidata *nd)
1151{
31e6b01f
NP
1152 set_root_rcu(nd);
1153
9875cf80 1154 while (1) {
31e6b01f
NP
1155 if (nd->path.dentry == nd->root.dentry &&
1156 nd->path.mnt == nd->root.mnt) {
1157 break;
1158 }
1159 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1160 struct dentry *old = nd->path.dentry;
1161 struct dentry *parent = old->d_parent;
1162 unsigned seq;
1163
1164 seq = read_seqcount_begin(&parent->d_seq);
1165 if (read_seqcount_retry(&old->d_seq, nd->seq))
ef7562d5 1166 goto failed;
31e6b01f
NP
1167 nd->path.dentry = parent;
1168 nd->seq = seq;
fa6ef735
EB
1169 if (unlikely(!path_connected(&nd->path)))
1170 goto failed;
31e6b01f
NP
1171 break;
1172 }
1173 if (!follow_up_rcu(&nd->path))
1174 break;
1175 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
31e6b01f 1176 }
dea39376
AV
1177 follow_mount_rcu(nd);
1178 nd->inode = nd->path.dentry->d_inode;
31e6b01f 1179 return 0;
ef7562d5
AV
1180
1181failed:
1182 nd->flags &= ~LOOKUP_RCU;
5b6ca027
AV
1183 if (!(nd->flags & LOOKUP_ROOT))
1184 nd->root.mnt = NULL;
32a7991b 1185 unlock_rcu_walk();
ef7562d5 1186 return -ECHILD;
31e6b01f
NP
1187}
1188
cc53ce53
DH
1189/*
1190 * Follow down to the covering mount currently visible to userspace. At each
1191 * point, the filesystem owning that dentry may be queried as to whether the
1192 * caller is permitted to proceed or not.
cc53ce53 1193 */
7cc90cc3 1194int follow_down(struct path *path)
cc53ce53
DH
1195{
1196 unsigned managed;
1197 int ret;
1198
1199 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1200 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1201 /* Allow the filesystem to manage the transit without i_mutex
1202 * being held.
1203 *
1204 * We indicate to the filesystem if someone is trying to mount
1205 * something here. This gives autofs the chance to deny anyone
1206 * other than its daemon the right to mount on its
1207 * superstructure.
1208 *
1209 * The filesystem may sleep at this point.
1210 */
1211 if (managed & DCACHE_MANAGE_TRANSIT) {
1212 BUG_ON(!path->dentry->d_op);
1213 BUG_ON(!path->dentry->d_op->d_manage);
ab90911f 1214 ret = path->dentry->d_op->d_manage(
1aed3e42 1215 path->dentry, false);
cc53ce53
DH
1216 if (ret < 0)
1217 return ret == -EISDIR ? 0 : ret;
1218 }
1219
1220 /* Transit to a mounted filesystem. */
1221 if (managed & DCACHE_MOUNTED) {
1222 struct vfsmount *mounted = lookup_mnt(path);
1223 if (!mounted)
1224 break;
1225 dput(path->dentry);
1226 mntput(path->mnt);
1227 path->mnt = mounted;
1228 path->dentry = dget(mounted->mnt_root);
1229 continue;
1230 }
1231
1232 /* Don't handle automount points here */
1233 break;
1234 }
1235 return 0;
1236}
1237
9875cf80
DH
1238/*
1239 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1240 */
1241static void follow_mount(struct path *path)
1242{
1243 while (d_mountpoint(path->dentry)) {
1244 struct vfsmount *mounted = lookup_mnt(path);
1245 if (!mounted)
1246 break;
1247 dput(path->dentry);
1248 mntput(path->mnt);
1249 path->mnt = mounted;
1250 path->dentry = dget(mounted->mnt_root);
1251 }
1252}
1253
fa6ef735 1254static int follow_dotdot(struct nameidata *nd)
1da177e4 1255{
2a737871 1256 set_root(nd);
e518ddb7 1257
1da177e4 1258 while(1) {
4ac91378 1259 struct dentry *old = nd->path.dentry;
1da177e4 1260
2a737871
AV
1261 if (nd->path.dentry == nd->root.dentry &&
1262 nd->path.mnt == nd->root.mnt) {
1da177e4
LT
1263 break;
1264 }
4ac91378 1265 if (nd->path.dentry != nd->path.mnt->mnt_root) {
3088dd70
AV
1266 /* rare case of legitimate dget_parent()... */
1267 nd->path.dentry = dget_parent(nd->path.dentry);
1da177e4 1268 dput(old);
fa6ef735
EB
1269 if (unlikely(!path_connected(&nd->path))) {
1270 path_put(&nd->path);
1271 return -ENOENT;
1272 }
1da177e4
LT
1273 break;
1274 }
3088dd70 1275 if (!follow_up(&nd->path))
1da177e4 1276 break;
1da177e4 1277 }
79ed0226 1278 follow_mount(&nd->path);
31e6b01f 1279 nd->inode = nd->path.dentry->d_inode;
fa6ef735 1280 return 0;
1da177e4
LT
1281}
1282
baa03890 1283/*
bad61189
MS
1284 * This looks up the name in dcache, possibly revalidates the old dentry and
1285 * allocates a new one if not found or not valid. In the need_lookup argument
1286 * returns whether i_op->lookup is necessary.
1287 *
1288 * dir->d_inode->i_mutex must be held
baa03890 1289 */
bad61189 1290static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
201f956e 1291 unsigned int flags, bool *need_lookup)
baa03890 1292{
baa03890 1293 struct dentry *dentry;
bad61189 1294 int error;
baa03890 1295
bad61189
MS
1296 *need_lookup = false;
1297 dentry = d_lookup(dir, name);
1298 if (dentry) {
39e3c955 1299 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
201f956e 1300 error = d_revalidate(dentry, flags);
bad61189
MS
1301 if (unlikely(error <= 0)) {
1302 if (error < 0) {
1303 dput(dentry);
1304 return ERR_PTR(error);
1305 } else if (!d_invalidate(dentry)) {
1306 dput(dentry);
1307 dentry = NULL;
1308 }
1309 }
1310 }
1311 }
baa03890 1312
bad61189
MS
1313 if (!dentry) {
1314 dentry = d_alloc(dir, name);
1315 if (unlikely(!dentry))
1316 return ERR_PTR(-ENOMEM);
baa03890 1317
bad61189 1318 *need_lookup = true;
baa03890
NP
1319 }
1320 return dentry;
1321}
1322
44396f4b 1323/*
bad61189
MS
1324 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1325 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1326 *
1327 * dir->d_inode->i_mutex must be held
44396f4b 1328 */
bad61189 1329static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
72bd866a 1330 unsigned int flags)
44396f4b 1331{
44396f4b
JB
1332 struct dentry *old;
1333
1334 /* Don't create child dentry for a dead directory. */
bad61189 1335 if (unlikely(IS_DEADDIR(dir))) {
e188dc02 1336 dput(dentry);
44396f4b 1337 return ERR_PTR(-ENOENT);
e188dc02 1338 }
44396f4b 1339
72bd866a 1340 old = dir->i_op->lookup(dir, dentry, flags);
44396f4b
JB
1341 if (unlikely(old)) {
1342 dput(dentry);
1343 dentry = old;
1344 }
1345 return dentry;
1346}
1347
a3255546 1348static struct dentry *__lookup_hash(struct qstr *name,
72bd866a 1349 struct dentry *base, unsigned int flags)
a3255546 1350{
bad61189 1351 bool need_lookup;
a3255546
AV
1352 struct dentry *dentry;
1353
72bd866a 1354 dentry = lookup_dcache(name, base, flags, &need_lookup);
bad61189
MS
1355 if (!need_lookup)
1356 return dentry;
a3255546 1357
72bd866a 1358 return lookup_real(base->d_inode, dentry, flags);
a3255546
AV
1359}
1360
1da177e4
LT
1361/*
1362 * It's more convoluted than I'd like it to be, but... it's still fairly
1363 * small and for now I'd prefer to have fast path as straight as possible.
1364 * It _is_ time-critical.
1365 */
e97cdc87 1366static int lookup_fast(struct nameidata *nd,
697f514d 1367 struct path *path, struct inode **inode)
1da177e4 1368{
4ac91378 1369 struct vfsmount *mnt = nd->path.mnt;
31e6b01f 1370 struct dentry *dentry, *parent = nd->path.dentry;
5a18fff2
AV
1371 int need_reval = 1;
1372 int status = 1;
9875cf80
DH
1373 int err;
1374
b04f784e
NP
1375 /*
1376 * Rename seqlock is not required here because in the off chance
1377 * of a false negative due to a concurrent rename, we're going to
1378 * do the non-racy lookup, below.
1379 */
31e6b01f
NP
1380 if (nd->flags & LOOKUP_RCU) {
1381 unsigned seq;
e97cdc87 1382 dentry = __d_lookup_rcu(parent, &nd->last, &seq, nd->inode);
5a18fff2
AV
1383 if (!dentry)
1384 goto unlazy;
1385
12f8ad4b
LT
1386 /*
1387 * This sequence count validates that the inode matches
1388 * the dentry name information from lookup.
1389 */
1390 *inode = dentry->d_inode;
1391 if (read_seqcount_retry(&dentry->d_seq, seq))
1392 return -ECHILD;
1393
1394 /*
1395 * This sequence count validates that the parent had no
1396 * changes while we did the lookup of the dentry above.
1397 *
1398 * The memory barrier in read_seqcount_begin of child is
1399 * enough, we can use __read_seqcount_retry here.
1400 */
31e6b01f
NP
1401 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1402 return -ECHILD;
31e6b01f 1403 nd->seq = seq;
5a18fff2 1404
24643087 1405 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
4ce16ef3 1406 status = d_revalidate(dentry, nd->flags);
5a18fff2
AV
1407 if (unlikely(status <= 0)) {
1408 if (status != -ECHILD)
1409 need_reval = 0;
1410 goto unlazy;
1411 }
24643087 1412 }
31e6b01f
NP
1413 path->mnt = mnt;
1414 path->dentry = dentry;
d6e9bd25
AV
1415 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1416 goto unlazy;
1417 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1418 goto unlazy;
1419 return 0;
5a18fff2 1420unlazy:
19660af7
AV
1421 if (unlazy_walk(nd, dentry))
1422 return -ECHILD;
5a18fff2 1423 } else {
e97cdc87 1424 dentry = __d_lookup(parent, &nd->last);
9875cf80 1425 }
5a18fff2 1426
81e6f520
AV
1427 if (unlikely(!dentry))
1428 goto need_lookup;
1429
5a18fff2 1430 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
4ce16ef3 1431 status = d_revalidate(dentry, nd->flags);
5a18fff2
AV
1432 if (unlikely(status <= 0)) {
1433 if (status < 0) {
1434 dput(dentry);
1435 return status;
1436 }
1437 if (!d_invalidate(dentry)) {
1438 dput(dentry);
81e6f520 1439 goto need_lookup;
5a18fff2 1440 }
24643087 1441 }
697f514d 1442
9875cf80
DH
1443 path->mnt = mnt;
1444 path->dentry = dentry;
1445 err = follow_managed(path, nd->flags);
89312214
IK
1446 if (unlikely(err < 0)) {
1447 path_put_conditional(path, nd);
9875cf80 1448 return err;
89312214 1449 }
a3fbbde7
AV
1450 if (err)
1451 nd->flags |= LOOKUP_JUMPED;
9875cf80 1452 *inode = path->dentry->d_inode;
1da177e4 1453 return 0;
81e6f520
AV
1454
1455need_lookup:
697f514d
MS
1456 return 1;
1457}
1458
1459/* Fast lookup failed, do it the slow way */
cc2a5271 1460static int lookup_slow(struct nameidata *nd, struct path *path)
697f514d
MS
1461{
1462 struct dentry *dentry, *parent;
1463 int err;
1464
1465 parent = nd->path.dentry;
81e6f520
AV
1466 BUG_ON(nd->inode != parent->d_inode);
1467
1468 mutex_lock(&parent->d_inode->i_mutex);
cc2a5271 1469 dentry = __lookup_hash(&nd->last, parent, nd->flags);
81e6f520
AV
1470 mutex_unlock(&parent->d_inode->i_mutex);
1471 if (IS_ERR(dentry))
1472 return PTR_ERR(dentry);
697f514d
MS
1473 path->mnt = nd->path.mnt;
1474 path->dentry = dentry;
1475 err = follow_managed(path, nd->flags);
1476 if (unlikely(err < 0)) {
1477 path_put_conditional(path, nd);
1478 return err;
1479 }
1480 if (err)
1481 nd->flags |= LOOKUP_JUMPED;
1482 return 0;
1da177e4
LT
1483}
1484
52094c8a
AV
1485static inline int may_lookup(struct nameidata *nd)
1486{
1487 if (nd->flags & LOOKUP_RCU) {
4ad5abb3 1488 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
52094c8a
AV
1489 if (err != -ECHILD)
1490 return err;
19660af7 1491 if (unlazy_walk(nd, NULL))
52094c8a
AV
1492 return -ECHILD;
1493 }
4ad5abb3 1494 return inode_permission(nd->inode, MAY_EXEC);
52094c8a
AV
1495}
1496
9856fa1b
AV
1497static inline int handle_dots(struct nameidata *nd, int type)
1498{
1499 if (type == LAST_DOTDOT) {
1500 if (nd->flags & LOOKUP_RCU) {
1501 if (follow_dotdot_rcu(nd))
1502 return -ECHILD;
1503 } else
fa6ef735 1504 return follow_dotdot(nd);
9856fa1b
AV
1505 }
1506 return 0;
1507}
1508
951361f9
AV
1509static void terminate_walk(struct nameidata *nd)
1510{
1511 if (!(nd->flags & LOOKUP_RCU)) {
1512 path_put(&nd->path);
1513 } else {
1514 nd->flags &= ~LOOKUP_RCU;
5b6ca027
AV
1515 if (!(nd->flags & LOOKUP_ROOT))
1516 nd->root.mnt = NULL;
32a7991b 1517 unlock_rcu_walk();
951361f9
AV
1518 }
1519}
1520
3ddcd056
LT
1521/*
1522 * Do we need to follow links? We _really_ want to be able
1523 * to do this check without having to look at inode->i_op,
1524 * so we keep a cache of "no, this doesn't need follow_link"
1525 * for the common case.
1526 */
7813b94a 1527static inline int should_follow_link(struct inode *inode, int follow)
3ddcd056
LT
1528{
1529 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1530 if (likely(inode->i_op->follow_link))
1531 return follow;
1532
1533 /* This gets set once for the inode lifetime */
1534 spin_lock(&inode->i_lock);
1535 inode->i_opflags |= IOP_NOFOLLOW;
1536 spin_unlock(&inode->i_lock);
1537 }
1538 return 0;
1539}
1540
ce57dfc1 1541static inline int walk_component(struct nameidata *nd, struct path *path,
21b9b073 1542 int follow)
ce57dfc1
AV
1543{
1544 struct inode *inode;
1545 int err;
1546 /*
1547 * "." and ".." are special - ".." especially so because it has
1548 * to be able to know about the current root directory and
1549 * parent relationships.
1550 */
21b9b073
AV
1551 if (unlikely(nd->last_type != LAST_NORM))
1552 return handle_dots(nd, nd->last_type);
e97cdc87 1553 err = lookup_fast(nd, path, &inode);
ce57dfc1 1554 if (unlikely(err)) {
697f514d
MS
1555 if (err < 0)
1556 goto out_err;
1557
cc2a5271 1558 err = lookup_slow(nd, path);
697f514d
MS
1559 if (err < 0)
1560 goto out_err;
1561
1562 inode = path->dentry->d_inode;
ce57dfc1 1563 }
697f514d
MS
1564 err = -ENOENT;
1565 if (!inode)
1566 goto out_path_put;
1567
7813b94a 1568 if (should_follow_link(inode, follow)) {
19660af7 1569 if (nd->flags & LOOKUP_RCU) {
3b388f33
AV
1570 if (unlikely(nd->path.mnt != path->mnt ||
1571 unlazy_walk(nd, path->dentry))) {
697f514d
MS
1572 err = -ECHILD;
1573 goto out_err;
19660af7
AV
1574 }
1575 }
ce57dfc1
AV
1576 BUG_ON(inode != path->dentry->d_inode);
1577 return 1;
1578 }
1579 path_to_nameidata(path, nd);
1580 nd->inode = inode;
1581 return 0;
697f514d
MS
1582
1583out_path_put:
1584 path_to_nameidata(path, nd);
1585out_err:
1586 terminate_walk(nd);
1587 return err;
ce57dfc1
AV
1588}
1589
b356379a
AV
1590/*
1591 * This limits recursive symlink follows to 8, while
1592 * limiting consecutive symlinks to 40.
1593 *
1594 * Without that kind of total limit, nasty chains of consecutive
1595 * symlinks can cause almost arbitrarily long lookups.
1596 */
1597static inline int nested_symlink(struct path *path, struct nameidata *nd)
1598{
1599 int res;
1600
b356379a
AV
1601 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1602 path_put_conditional(path, nd);
1603 path_put(&nd->path);
1604 return -ELOOP;
1605 }
1a4022f8 1606 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
b356379a
AV
1607
1608 nd->depth++;
1609 current->link_count++;
1610
1611 do {
1612 struct path link = *path;
1613 void *cookie;
574197e0
AV
1614
1615 res = follow_link(&link, nd, &cookie);
6d7b5aae
AV
1616 if (res)
1617 break;
21b9b073 1618 res = walk_component(nd, path, LOOKUP_FOLLOW);
574197e0 1619 put_link(nd, &link, cookie);
b356379a
AV
1620 } while (res > 0);
1621
1622 current->link_count--;
1623 nd->depth--;
1624 return res;
1625}
1626
3ddcd056
LT
1627/*
1628 * We really don't want to look at inode->i_op->lookup
1629 * when we don't have to. So we keep a cache bit in
1630 * the inode ->i_opflags field that says "yes, we can
1631 * do lookup on this inode".
1632 */
1633static inline int can_lookup(struct inode *inode)
1634{
1635 if (likely(inode->i_opflags & IOP_LOOKUP))
1636 return 1;
1637 if (likely(!inode->i_op->lookup))
1638 return 0;
1639
1640 /* We do this once for the lifetime of the inode */
1641 spin_lock(&inode->i_lock);
1642 inode->i_opflags |= IOP_LOOKUP;
1643 spin_unlock(&inode->i_lock);
1644 return 1;
1645}
1646
bfcfaa77
LT
1647/*
1648 * We can do the critical dentry name comparison and hashing
1649 * operations one word at a time, but we are limited to:
1650 *
1651 * - Architectures with fast unaligned word accesses. We could
1652 * do a "get_unaligned()" if this helps and is sufficiently
1653 * fast.
1654 *
1655 * - Little-endian machines (so that we can generate the mask
1656 * of low bytes efficiently). Again, we *could* do a byte
1657 * swapping load on big-endian architectures if that is not
1658 * expensive enough to make the optimization worthless.
1659 *
1660 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1661 * do not trap on the (extremely unlikely) case of a page
1662 * crossing operation.
1663 *
1664 * - Furthermore, we need an efficient 64-bit compile for the
1665 * 64-bit case in order to generate the "number of bytes in
1666 * the final mask". Again, that could be replaced with a
1667 * efficient population count instruction or similar.
1668 */
1669#ifdef CONFIG_DCACHE_WORD_ACCESS
1670
f68e556e 1671#include <asm/word-at-a-time.h>
bfcfaa77 1672
f68e556e 1673#ifdef CONFIG_64BIT
bfcfaa77
LT
1674
1675static inline unsigned int fold_hash(unsigned long hash)
1676{
d4c96061 1677 return hash_64(hash, 32);
bfcfaa77
LT
1678}
1679
1680#else /* 32-bit case */
1681
bfcfaa77
LT
1682#define fold_hash(x) (x)
1683
1684#endif
1685
1686unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1687{
1688 unsigned long a, mask;
1689 unsigned long hash = 0;
1690
1691 for (;;) {
e419b4cc 1692 a = load_unaligned_zeropad(name);
bfcfaa77
LT
1693 if (len < sizeof(unsigned long))
1694 break;
1695 hash += a;
f132c5be 1696 hash *= 9;
bfcfaa77
LT
1697 name += sizeof(unsigned long);
1698 len -= sizeof(unsigned long);
1699 if (!len)
1700 goto done;
1701 }
1702 mask = ~(~0ul << len*8);
1703 hash += mask & a;
1704done:
1705 return fold_hash(hash);
1706}
1707EXPORT_SYMBOL(full_name_hash);
1708
bfcfaa77
LT
1709/*
1710 * Calculate the length and hash of the path component, and
1711 * return the length of the component;
1712 */
1713static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1714{
36126f8f
LT
1715 unsigned long a, b, adata, bdata, mask, hash, len;
1716 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
bfcfaa77
LT
1717
1718 hash = a = 0;
1719 len = -sizeof(unsigned long);
1720 do {
1721 hash = (hash + a) * 9;
1722 len += sizeof(unsigned long);
e419b4cc 1723 a = load_unaligned_zeropad(name+len);
36126f8f
LT
1724 b = a ^ REPEAT_BYTE('/');
1725 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1726
1727 adata = prep_zero_mask(a, adata, &constants);
1728 bdata = prep_zero_mask(b, bdata, &constants);
1729
1730 mask = create_zero_mask(adata | bdata);
1731
1732 hash += a & zero_bytemask(mask);
bfcfaa77
LT
1733 *hashp = fold_hash(hash);
1734
36126f8f 1735 return len + find_zero(mask);
bfcfaa77
LT
1736}
1737
1738#else
1739
0145acc2
LT
1740unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1741{
1742 unsigned long hash = init_name_hash();
1743 while (len--)
1744 hash = partial_name_hash(*name++, hash);
1745 return end_name_hash(hash);
1746}
ae942ae7 1747EXPORT_SYMBOL(full_name_hash);
0145acc2 1748
200e9ef7
LT
1749/*
1750 * We know there's a real path component here of at least
1751 * one character.
1752 */
1753static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1754{
1755 unsigned long hash = init_name_hash();
1756 unsigned long len = 0, c;
1757
1758 c = (unsigned char)*name;
1759 do {
1760 len++;
1761 hash = partial_name_hash(c, hash);
1762 c = (unsigned char)name[len];
1763 } while (c && c != '/');
1764 *hashp = end_name_hash(hash);
1765 return len;
1766}
1767
bfcfaa77
LT
1768#endif
1769
1da177e4
LT
1770/*
1771 * Name resolution.
ea3834d9
PM
1772 * This is the basic name resolution function, turning a pathname into
1773 * the final dentry. We expect 'base' to be positive and a directory.
1da177e4 1774 *
ea3834d9
PM
1775 * Returns 0 and nd will have valid dentry and mnt on success.
1776 * Returns error and drops reference to input namei data on failure.
1da177e4 1777 */
6de88d72 1778static int link_path_walk(const char *name, struct nameidata *nd)
1da177e4
LT
1779{
1780 struct path next;
1da177e4 1781 int err;
1da177e4
LT
1782
1783 while (*name=='/')
1784 name++;
1785 if (!*name)
086e183a 1786 return 0;
1da177e4 1787
1da177e4
LT
1788 /* At this point we know we have a real path component. */
1789 for(;;) {
1da177e4 1790 struct qstr this;
200e9ef7 1791 long len;
fe479a58 1792 int type;
1da177e4 1793
52094c8a 1794 err = may_lookup(nd);
1da177e4
LT
1795 if (err)
1796 break;
1797
200e9ef7 1798 len = hash_name(name, &this.hash);
1da177e4 1799 this.name = name;
200e9ef7 1800 this.len = len;
1da177e4 1801
fe479a58 1802 type = LAST_NORM;
200e9ef7 1803 if (name[0] == '.') switch (len) {
fe479a58 1804 case 2:
200e9ef7 1805 if (name[1] == '.') {
fe479a58 1806 type = LAST_DOTDOT;
16c2cd71
AV
1807 nd->flags |= LOOKUP_JUMPED;
1808 }
fe479a58
AV
1809 break;
1810 case 1:
1811 type = LAST_DOT;
1812 }
5a202bcd
AV
1813 if (likely(type == LAST_NORM)) {
1814 struct dentry *parent = nd->path.dentry;
16c2cd71 1815 nd->flags &= ~LOOKUP_JUMPED;
5a202bcd
AV
1816 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1817 err = parent->d_op->d_hash(parent, nd->inode,
1818 &this);
1819 if (err < 0)
1820 break;
1821 }
1822 }
fe479a58 1823
5f4a6a69
AV
1824 nd->last = this;
1825 nd->last_type = type;
1826
200e9ef7 1827 if (!name[len])
5f4a6a69 1828 return 0;
200e9ef7
LT
1829 /*
1830 * If it wasn't NUL, we know it was '/'. Skip that
1831 * slash, and continue until no more slashes.
1832 */
1833 do {
1834 len++;
1835 } while (unlikely(name[len] == '/'));
1836 if (!name[len])
5f4a6a69
AV
1837 return 0;
1838
200e9ef7 1839 name += len;
1da177e4 1840
21b9b073 1841 err = walk_component(nd, &next, LOOKUP_FOLLOW);
ce57dfc1
AV
1842 if (err < 0)
1843 return err;
1da177e4 1844
ce57dfc1 1845 if (err) {
b356379a 1846 err = nested_symlink(&next, nd);
1da177e4 1847 if (err)
a7472bab 1848 return err;
31e6b01f 1849 }
5f4a6a69
AV
1850 if (!can_lookup(nd->inode)) {
1851 err = -ENOTDIR;
1852 break;
1853 }
1da177e4 1854 }
951361f9 1855 terminate_walk(nd);
1da177e4
LT
1856 return err;
1857}
1858
70e9b357
AV
1859static int path_init(int dfd, const char *name, unsigned int flags,
1860 struct nameidata *nd, struct file **fp)
31e6b01f
NP
1861{
1862 int retval = 0;
31e6b01f
NP
1863
1864 nd->last_type = LAST_ROOT; /* if there are only slashes... */
16c2cd71 1865 nd->flags = flags | LOOKUP_JUMPED;
31e6b01f 1866 nd->depth = 0;
5b6ca027
AV
1867 if (flags & LOOKUP_ROOT) {
1868 struct inode *inode = nd->root.dentry->d_inode;
73d049a4 1869 if (*name) {
741b7c3f 1870 if (!can_lookup(inode))
73d049a4
AV
1871 return -ENOTDIR;
1872 retval = inode_permission(inode, MAY_EXEC);
1873 if (retval)
1874 return retval;
1875 }
5b6ca027
AV
1876 nd->path = nd->root;
1877 nd->inode = inode;
1878 if (flags & LOOKUP_RCU) {
32a7991b 1879 lock_rcu_walk();
5b6ca027
AV
1880 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1881 } else {
1882 path_get(&nd->path);
1883 }
1884 return 0;
1885 }
1886
31e6b01f 1887 nd->root.mnt = NULL;
31e6b01f
NP
1888
1889 if (*name=='/') {
e41f7d4e 1890 if (flags & LOOKUP_RCU) {
32a7991b 1891 lock_rcu_walk();
e41f7d4e
AV
1892 set_root_rcu(nd);
1893 } else {
1894 set_root(nd);
1895 path_get(&nd->root);
1896 }
1897 nd->path = nd->root;
31e6b01f 1898 } else if (dfd == AT_FDCWD) {
e41f7d4e
AV
1899 if (flags & LOOKUP_RCU) {
1900 struct fs_struct *fs = current->fs;
1901 unsigned seq;
31e6b01f 1902
32a7991b 1903 lock_rcu_walk();
c28cc364 1904
e41f7d4e
AV
1905 do {
1906 seq = read_seqcount_begin(&fs->seq);
1907 nd->path = fs->pwd;
1908 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1909 } while (read_seqcount_retry(&fs->seq, seq));
1910 } else {
1911 get_fs_pwd(current->fs, &nd->path);
1912 }
31e6b01f 1913 } else {
582aa64a 1914 /* Caller must check execute permissions on the starting path component */
2903ff01 1915 struct fd f = fdget_raw(dfd);
31e6b01f
NP
1916 struct dentry *dentry;
1917
2903ff01
AV
1918 if (!f.file)
1919 return -EBADF;
31e6b01f 1920
2903ff01 1921 dentry = f.file->f_path.dentry;
31e6b01f 1922
f52e0c11 1923 if (*name) {
741b7c3f 1924 if (!can_lookup(dentry->d_inode)) {
2903ff01
AV
1925 fdput(f);
1926 return -ENOTDIR;
1927 }
f52e0c11 1928 }
31e6b01f 1929
2903ff01 1930 nd->path = f.file->f_path;
e41f7d4e 1931 if (flags & LOOKUP_RCU) {
2903ff01
AV
1932 if (f.need_put)
1933 *fp = f.file;
e41f7d4e 1934 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
32a7991b 1935 lock_rcu_walk();
e41f7d4e 1936 } else {
2903ff01
AV
1937 path_get(&nd->path);
1938 fdput(f);
e41f7d4e 1939 }
31e6b01f 1940 }
31e6b01f 1941
31e6b01f 1942 nd->inode = nd->path.dentry->d_inode;
9b4a9b14 1943 return 0;
9b4a9b14
AV
1944}
1945
bd92d7fe
AV
1946static inline int lookup_last(struct nameidata *nd, struct path *path)
1947{
1948 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1949 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1950
1951 nd->flags &= ~LOOKUP_PARENT;
21b9b073 1952 return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
bd92d7fe
AV
1953}
1954
9b4a9b14 1955/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
ee0827cd 1956static int path_lookupat(int dfd, const char *name,
9b4a9b14
AV
1957 unsigned int flags, struct nameidata *nd)
1958{
70e9b357 1959 struct file *base = NULL;
bd92d7fe
AV
1960 struct path path;
1961 int err;
31e6b01f
NP
1962
1963 /*
1964 * Path walking is largely split up into 2 different synchronisation
1965 * schemes, rcu-walk and ref-walk (explained in
1966 * Documentation/filesystems/path-lookup.txt). These share much of the
1967 * path walk code, but some things particularly setup, cleanup, and
1968 * following mounts are sufficiently divergent that functions are
1969 * duplicated. Typically there is a function foo(), and its RCU
1970 * analogue, foo_rcu().
1971 *
1972 * -ECHILD is the error number of choice (just to avoid clashes) that
1973 * is returned if some aspect of an rcu-walk fails. Such an error must
1974 * be handled by restarting a traditional ref-walk (which will always
1975 * be able to complete).
1976 */
bd92d7fe 1977 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
ee0827cd 1978
bd92d7fe
AV
1979 if (unlikely(err))
1980 return err;
ee0827cd
AV
1981
1982 current->total_link_count = 0;
bd92d7fe
AV
1983 err = link_path_walk(name, nd);
1984
1985 if (!err && !(flags & LOOKUP_PARENT)) {
bd92d7fe
AV
1986 err = lookup_last(nd, &path);
1987 while (err > 0) {
1988 void *cookie;
1989 struct path link = path;
800179c9
KC
1990 err = may_follow_link(&link, nd);
1991 if (unlikely(err))
1992 break;
bd92d7fe 1993 nd->flags |= LOOKUP_PARENT;
574197e0 1994 err = follow_link(&link, nd, &cookie);
6d7b5aae
AV
1995 if (err)
1996 break;
1997 err = lookup_last(nd, &path);
574197e0 1998 put_link(nd, &link, cookie);
bd92d7fe
AV
1999 }
2000 }
ee0827cd 2001
9f1fafee
AV
2002 if (!err)
2003 err = complete_walk(nd);
bd92d7fe
AV
2004
2005 if (!err && nd->flags & LOOKUP_DIRECTORY) {
05252901 2006 if (!can_lookup(nd->inode)) {
bd92d7fe 2007 path_put(&nd->path);
bd23a539 2008 err = -ENOTDIR;
bd92d7fe
AV
2009 }
2010 }
16c2cd71 2011
70e9b357
AV
2012 if (base)
2013 fput(base);
ee0827cd 2014
5b6ca027 2015 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
2a737871
AV
2016 path_put(&nd->root);
2017 nd->root.mnt = NULL;
2018 }
bd92d7fe 2019 return err;
ee0827cd 2020}
31e6b01f 2021
873f1eed 2022static int filename_lookup(int dfd, struct filename *name,
ee0827cd
AV
2023 unsigned int flags, struct nameidata *nd)
2024{
873f1eed 2025 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
ee0827cd 2026 if (unlikely(retval == -ECHILD))
873f1eed 2027 retval = path_lookupat(dfd, name->name, flags, nd);
ee0827cd 2028 if (unlikely(retval == -ESTALE))
873f1eed
JL
2029 retval = path_lookupat(dfd, name->name,
2030 flags | LOOKUP_REVAL, nd);
31e6b01f 2031
f78570dd 2032 if (likely(!retval))
adb5c247 2033 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
170aa3d0 2034 return retval;
1da177e4
LT
2035}
2036
873f1eed
JL
2037static int do_path_lookup(int dfd, const char *name,
2038 unsigned int flags, struct nameidata *nd)
2039{
2040 struct filename filename = { .name = name };
2041
2042 return filename_lookup(dfd, &filename, flags, nd);
2043}
2044
79714f72
AV
2045/* does lookup, returns the object with parent locked */
2046struct dentry *kern_path_locked(const char *name, struct path *path)
5590ff0d 2047{
79714f72
AV
2048 struct nameidata nd;
2049 struct dentry *d;
2050 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2051 if (err)
2052 return ERR_PTR(err);
2053 if (nd.last_type != LAST_NORM) {
2054 path_put(&nd.path);
2055 return ERR_PTR(-EINVAL);
2056 }
2057 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
1e0ea001 2058 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
79714f72
AV
2059 if (IS_ERR(d)) {
2060 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2061 path_put(&nd.path);
2062 return d;
2063 }
2064 *path = nd.path;
2065 return d;
5590ff0d
UD
2066}
2067
d1811465
AV
2068int kern_path(const char *name, unsigned int flags, struct path *path)
2069{
2070 struct nameidata nd;
2071 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2072 if (!res)
2073 *path = nd.path;
2074 return res;
2075}
2076
16f18200
JJS
2077/**
2078 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2079 * @dentry: pointer to dentry of the base directory
2080 * @mnt: pointer to vfs mount of the base directory
2081 * @name: pointer to file name
2082 * @flags: lookup flags
e0a01249 2083 * @path: pointer to struct path to fill
16f18200
JJS
2084 */
2085int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2086 const char *name, unsigned int flags,
e0a01249 2087 struct path *path)
16f18200 2088{
e0a01249
AV
2089 struct nameidata nd;
2090 int err;
2091 nd.root.dentry = dentry;
2092 nd.root.mnt = mnt;
2093 BUG_ON(flags & LOOKUP_PARENT);
5b6ca027 2094 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
e0a01249
AV
2095 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2096 if (!err)
2097 *path = nd.path;
2098 return err;
16f18200
JJS
2099}
2100
057f6c01
JM
2101/*
2102 * Restricted form of lookup. Doesn't follow links, single-component only,
2103 * needs parent already locked. Doesn't follow mounts.
2104 * SMP-safe.
2105 */
eead1911 2106static struct dentry *lookup_hash(struct nameidata *nd)
057f6c01 2107{
72bd866a 2108 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
1da177e4
LT
2109}
2110
eead1911 2111/**
a6b91919 2112 * lookup_one_len - filesystem helper to lookup single pathname component
eead1911
CH
2113 * @name: pathname component to lookup
2114 * @base: base directory to lookup from
2115 * @len: maximum length @len should be interpreted to
2116 *
a6b91919
RD
2117 * Note that this routine is purely a helper for filesystem usage and should
2118 * not be called by generic code. Also note that by using this function the
eead1911
CH
2119 * nameidata argument is passed to the filesystem methods and a filesystem
2120 * using this helper needs to be prepared for that.
2121 */
057f6c01
JM
2122struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2123{
057f6c01 2124 struct qstr this;
6a96ba54 2125 unsigned int c;
cda309de 2126 int err;
057f6c01 2127
2f9092e1
DW
2128 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2129
6a96ba54
AV
2130 this.name = name;
2131 this.len = len;
0145acc2 2132 this.hash = full_name_hash(name, len);
6a96ba54
AV
2133 if (!len)
2134 return ERR_PTR(-EACCES);
2135
21d8a15a
AV
2136 if (unlikely(name[0] == '.')) {
2137 if (len < 2 || (len == 2 && name[1] == '.'))
2138 return ERR_PTR(-EACCES);
2139 }
2140
6a96ba54
AV
2141 while (len--) {
2142 c = *(const unsigned char *)name++;
2143 if (c == '/' || c == '\0')
2144 return ERR_PTR(-EACCES);
6a96ba54 2145 }
5a202bcd
AV
2146 /*
2147 * See if the low-level filesystem might want
2148 * to use its own hash..
2149 */
2150 if (base->d_flags & DCACHE_OP_HASH) {
2151 int err = base->d_op->d_hash(base, base->d_inode, &this);
2152 if (err < 0)
2153 return ERR_PTR(err);
2154 }
eead1911 2155
cda309de
MS
2156 err = inode_permission(base->d_inode, MAY_EXEC);
2157 if (err)
2158 return ERR_PTR(err);
2159
72bd866a 2160 return __lookup_hash(&this, base, 0);
057f6c01
JM
2161}
2162
1fa1e7f6
AW
2163int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2164 struct path *path, int *empty)
1da177e4 2165{
2d8f3038 2166 struct nameidata nd;
91a27b2a 2167 struct filename *tmp = getname_flags(name, flags, empty);
1da177e4 2168 int err = PTR_ERR(tmp);
1da177e4 2169 if (!IS_ERR(tmp)) {
2d8f3038
AV
2170
2171 BUG_ON(flags & LOOKUP_PARENT);
2172
873f1eed 2173 err = filename_lookup(dfd, tmp, flags, &nd);
1da177e4 2174 putname(tmp);
2d8f3038
AV
2175 if (!err)
2176 *path = nd.path;
1da177e4
LT
2177 }
2178 return err;
2179}
2180
1fa1e7f6
AW
2181int user_path_at(int dfd, const char __user *name, unsigned flags,
2182 struct path *path)
2183{
f7493e5d 2184 return user_path_at_empty(dfd, name, flags, path, NULL);
1fa1e7f6
AW
2185}
2186
873f1eed
JL
2187/*
2188 * NB: most callers don't do anything directly with the reference to the
2189 * to struct filename, but the nd->last pointer points into the name string
2190 * allocated by getname. So we must hold the reference to it until all
2191 * path-walking is complete.
2192 */
91a27b2a 2193static struct filename *
9e790bd6
JL
2194user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
2195 unsigned int flags)
2ad94ae6 2196{
91a27b2a 2197 struct filename *s = getname(path);
2ad94ae6
AV
2198 int error;
2199
9e790bd6
JL
2200 /* only LOOKUP_REVAL is allowed in extra flags */
2201 flags &= LOOKUP_REVAL;
2202
2ad94ae6 2203 if (IS_ERR(s))
91a27b2a 2204 return s;
2ad94ae6 2205
9e790bd6 2206 error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
91a27b2a 2207 if (error) {
2ad94ae6 2208 putname(s);
91a27b2a
JL
2209 return ERR_PTR(error);
2210 }
2ad94ae6 2211
91a27b2a 2212 return s;
2ad94ae6
AV
2213}
2214
1da177e4
LT
2215/*
2216 * It's inline, so penalty for filesystems that don't use sticky bit is
2217 * minimal.
2218 */
2219static inline int check_sticky(struct inode *dir, struct inode *inode)
2220{
8e96e3b7 2221 kuid_t fsuid = current_fsuid();
da9592ed 2222
1da177e4
LT
2223 if (!(dir->i_mode & S_ISVTX))
2224 return 0;
8e96e3b7 2225 if (uid_eq(inode->i_uid, fsuid))
1da177e4 2226 return 0;
8e96e3b7 2227 if (uid_eq(dir->i_uid, fsuid))
1da177e4 2228 return 0;
4f80c6c1 2229 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
1da177e4
LT
2230}
2231
2232/*
2233 * Check whether we can remove a link victim from directory dir, check
2234 * whether the type of victim is right.
2235 * 1. We can't do it if dir is read-only (done in permission())
2236 * 2. We should have write and exec permissions on dir
2237 * 3. We can't remove anything from append-only dir
2238 * 4. We can't do anything with immutable dir (done in permission())
2239 * 5. If the sticky bit on dir is set we should either
2240 * a. be owner of dir, or
2241 * b. be owner of victim, or
2242 * c. have CAP_FOWNER capability
2243 * 6. If the victim is append-only or immutable we can't do antyhing with
2244 * links pointing to it.
2245 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2246 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2247 * 9. We can't remove a root or mountpoint.
2248 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2249 * nfs_async_unlink().
2250 */
858119e1 2251static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1da177e4
LT
2252{
2253 int error;
2254
2255 if (!victim->d_inode)
2256 return -ENOENT;
2257
2258 BUG_ON(victim->d_parent->d_inode != dir);
4fa6b5ec 2259 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
1da177e4 2260
f419a2e3 2261 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
2262 if (error)
2263 return error;
2264 if (IS_APPEND(dir))
2265 return -EPERM;
2266 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
f9454548 2267 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1da177e4
LT
2268 return -EPERM;
2269 if (isdir) {
2270 if (!S_ISDIR(victim->d_inode->i_mode))
2271 return -ENOTDIR;
2272 if (IS_ROOT(victim))
2273 return -EBUSY;
2274 } else if (S_ISDIR(victim->d_inode->i_mode))
2275 return -EISDIR;
2276 if (IS_DEADDIR(dir))
2277 return -ENOENT;
2278 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2279 return -EBUSY;
2280 return 0;
2281}
2282
2283/* Check whether we can create an object with dentry child in directory
2284 * dir.
2285 * 1. We can't do it if child already exists (open has special treatment for
2286 * this case, but since we are inlined it's OK)
2287 * 2. We can't do it if dir is read-only (done in permission())
2288 * 3. We should have write and exec permissions on dir
2289 * 4. We can't do it if dir is immutable (done in permission())
2290 */
a95164d9 2291static inline int may_create(struct inode *dir, struct dentry *child)
1da177e4 2292{
1ec12a81 2293 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
1da177e4
LT
2294 if (child->d_inode)
2295 return -EEXIST;
2296 if (IS_DEADDIR(dir))
2297 return -ENOENT;
f419a2e3 2298 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
2299}
2300
1da177e4
LT
2301/*
2302 * p1 and p2 should be directories on the same fs.
2303 */
2304struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2305{
2306 struct dentry *p;
2307
2308 if (p1 == p2) {
f2eace23 2309 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1da177e4
LT
2310 return NULL;
2311 }
2312
a11f3a05 2313 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4 2314
e2761a11
OH
2315 p = d_ancestor(p2, p1);
2316 if (p) {
2317 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2318 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2319 return p;
1da177e4
LT
2320 }
2321
e2761a11
OH
2322 p = d_ancestor(p1, p2);
2323 if (p) {
2324 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2325 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2326 return p;
1da177e4
LT
2327 }
2328
f2eace23
IM
2329 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2330 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1da177e4
LT
2331 return NULL;
2332}
2333
2334void unlock_rename(struct dentry *p1, struct dentry *p2)
2335{
1b1dcc1b 2336 mutex_unlock(&p1->d_inode->i_mutex);
1da177e4 2337 if (p1 != p2) {
1b1dcc1b 2338 mutex_unlock(&p2->d_inode->i_mutex);
a11f3a05 2339 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4
LT
2340 }
2341}
2342
4acdaf27 2343int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
312b63fb 2344 bool want_excl)
1da177e4 2345{
a95164d9 2346 int error = may_create(dir, dentry);
1da177e4
LT
2347 if (error)
2348 return error;
2349
acfa4380 2350 if (!dir->i_op->create)
1da177e4
LT
2351 return -EACCES; /* shouldn't it be ENOSYS? */
2352 mode &= S_IALLUGO;
2353 mode |= S_IFREG;
2354 error = security_inode_create(dir, dentry, mode);
2355 if (error)
2356 return error;
312b63fb 2357 error = dir->i_op->create(dir, dentry, mode, want_excl);
a74574aa 2358 if (!error)
f38aa942 2359 fsnotify_create(dir, dentry);
1da177e4
LT
2360 return error;
2361}
2362
73d049a4 2363static int may_open(struct path *path, int acc_mode, int flag)
1da177e4 2364{
3fb64190 2365 struct dentry *dentry = path->dentry;
1da177e4
LT
2366 struct inode *inode = dentry->d_inode;
2367 int error;
2368
bcda7652
AV
2369 /* O_PATH? */
2370 if (!acc_mode)
2371 return 0;
2372
1da177e4
LT
2373 if (!inode)
2374 return -ENOENT;
2375
c8fe8f30
CH
2376 switch (inode->i_mode & S_IFMT) {
2377 case S_IFLNK:
1da177e4 2378 return -ELOOP;
c8fe8f30
CH
2379 case S_IFDIR:
2380 if (acc_mode & MAY_WRITE)
2381 return -EISDIR;
2382 break;
2383 case S_IFBLK:
2384 case S_IFCHR:
3fb64190 2385 if (path->mnt->mnt_flags & MNT_NODEV)
1da177e4 2386 return -EACCES;
c8fe8f30
CH
2387 /*FALLTHRU*/
2388 case S_IFIFO:
2389 case S_IFSOCK:
1da177e4 2390 flag &= ~O_TRUNC;
c8fe8f30 2391 break;
4a3fd211 2392 }
b41572e9 2393
3fb64190 2394 error = inode_permission(inode, acc_mode);
b41572e9
DH
2395 if (error)
2396 return error;
6146f0d5 2397
1da177e4
LT
2398 /*
2399 * An append-only file must be opened in append mode for writing.
2400 */
2401 if (IS_APPEND(inode)) {
8737c930 2402 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
7715b521 2403 return -EPERM;
1da177e4 2404 if (flag & O_TRUNC)
7715b521 2405 return -EPERM;
1da177e4
LT
2406 }
2407
2408 /* O_NOATIME can only be set by the owner or superuser */
2e149670 2409 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
7715b521 2410 return -EPERM;
1da177e4 2411
f3c7691e 2412 return 0;
7715b521 2413}
1da177e4 2414
e1181ee6 2415static int handle_truncate(struct file *filp)
7715b521 2416{
e1181ee6 2417 struct path *path = &filp->f_path;
7715b521
AV
2418 struct inode *inode = path->dentry->d_inode;
2419 int error = get_write_access(inode);
2420 if (error)
2421 return error;
2422 /*
2423 * Refuse to truncate files with mandatory locks held on them.
2424 */
2425 error = locks_verify_locked(inode);
2426 if (!error)
ea0d3ab2 2427 error = security_path_truncate(path);
7715b521
AV
2428 if (!error) {
2429 error = do_truncate(path->dentry, 0,
2430 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
e1181ee6 2431 filp);
7715b521
AV
2432 }
2433 put_write_access(inode);
acd0c935 2434 return error;
1da177e4
LT
2435}
2436
d57999e1
DH
2437static inline int open_to_namei_flags(int flag)
2438{
8a5e929d
AV
2439 if ((flag & O_ACCMODE) == 3)
2440 flag--;
d57999e1
DH
2441 return flag;
2442}
2443
d18e9008
MS
2444static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2445{
2446 int error = security_path_mknod(dir, dentry, mode, 0);
2447 if (error)
2448 return error;
2449
2450 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2451 if (error)
2452 return error;
2453
2454 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2455}
2456
1acf0af9
DH
2457/*
2458 * Attempt to atomically look up, create and open a file from a negative
2459 * dentry.
2460 *
2461 * Returns 0 if successful. The file will have been created and attached to
2462 * @file by the filesystem calling finish_open().
2463 *
2464 * Returns 1 if the file was looked up only or didn't need creating. The
2465 * caller will need to perform the open themselves. @path will have been
2466 * updated to point to the new dentry. This may be negative.
2467 *
2468 * Returns an error code otherwise.
2469 */
2675a4eb
AV
2470static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2471 struct path *path, struct file *file,
2472 const struct open_flags *op,
64894cf8 2473 bool got_write, bool need_lookup,
2675a4eb 2474 int *opened)
d18e9008
MS
2475{
2476 struct inode *dir = nd->path.dentry->d_inode;
2477 unsigned open_flag = open_to_namei_flags(op->open_flag);
2478 umode_t mode;
2479 int error;
2480 int acc_mode;
d18e9008
MS
2481 int create_error = 0;
2482 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2483
2484 BUG_ON(dentry->d_inode);
2485
2486 /* Don't create child dentry for a dead directory. */
2487 if (unlikely(IS_DEADDIR(dir))) {
2675a4eb 2488 error = -ENOENT;
d18e9008
MS
2489 goto out;
2490 }
2491
62b259d8 2492 mode = op->mode;
d18e9008
MS
2493 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2494 mode &= ~current_umask();
2495
f8310c59 2496 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) {
d18e9008 2497 open_flag &= ~O_TRUNC;
47237687 2498 *opened |= FILE_CREATED;
d18e9008
MS
2499 }
2500
2501 /*
2502 * Checking write permission is tricky, bacuse we don't know if we are
2503 * going to actually need it: O_CREAT opens should work as long as the
2504 * file exists. But checking existence breaks atomicity. The trick is
2505 * to check access and if not granted clear O_CREAT from the flags.
2506 *
2507 * Another problem is returing the "right" error value (e.g. for an
2508 * O_EXCL open we want to return EEXIST not EROFS).
2509 */
64894cf8
AV
2510 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2511 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2512 if (!(open_flag & O_CREAT)) {
d18e9008
MS
2513 /*
2514 * No O_CREATE -> atomicity not a requirement -> fall
2515 * back to lookup + open
2516 */
2517 goto no_open;
2518 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2519 /* Fall back and fail with the right error */
64894cf8 2520 create_error = -EROFS;
d18e9008
MS
2521 goto no_open;
2522 } else {
2523 /* No side effects, safe to clear O_CREAT */
64894cf8 2524 create_error = -EROFS;
d18e9008
MS
2525 open_flag &= ~O_CREAT;
2526 }
2527 }
2528
2529 if (open_flag & O_CREAT) {
38227f78 2530 error = may_o_create(&nd->path, dentry, mode);
d18e9008
MS
2531 if (error) {
2532 create_error = error;
2533 if (open_flag & O_EXCL)
2534 goto no_open;
2535 open_flag &= ~O_CREAT;
2536 }
2537 }
2538
2539 if (nd->flags & LOOKUP_DIRECTORY)
2540 open_flag |= O_DIRECTORY;
2541
30d90494
AV
2542 file->f_path.dentry = DENTRY_NOT_SET;
2543 file->f_path.mnt = nd->path.mnt;
2544 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
47237687 2545 opened);
d9585277 2546 if (error < 0) {
d9585277
AV
2547 if (create_error && error == -ENOENT)
2548 error = create_error;
d18e9008
MS
2549 goto out;
2550 }
2551
2552 acc_mode = op->acc_mode;
47237687 2553 if (*opened & FILE_CREATED) {
d18e9008
MS
2554 fsnotify_create(dir, dentry);
2555 acc_mode = MAY_OPEN;
2556 }
2557
d9585277 2558 if (error) { /* returned 1, that is */
30d90494 2559 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2675a4eb 2560 error = -EIO;
d18e9008
MS
2561 goto out;
2562 }
30d90494 2563 if (file->f_path.dentry) {
d18e9008 2564 dput(dentry);
30d90494 2565 dentry = file->f_path.dentry;
d18e9008 2566 }
62b2ce96
SW
2567 if (create_error && dentry->d_inode == NULL) {
2568 error = create_error;
2569 goto out;
2570 }
d18e9008
MS
2571 goto looked_up;
2572 }
2573
2574 /*
2575 * We didn't have the inode before the open, so check open permission
2576 * here.
2577 */
2675a4eb
AV
2578 error = may_open(&file->f_path, acc_mode, open_flag);
2579 if (error)
2580 fput(file);
d18e9008
MS
2581
2582out:
2583 dput(dentry);
2675a4eb 2584 return error;
d18e9008 2585
d18e9008
MS
2586no_open:
2587 if (need_lookup) {
72bd866a 2588 dentry = lookup_real(dir, dentry, nd->flags);
d18e9008 2589 if (IS_ERR(dentry))
2675a4eb 2590 return PTR_ERR(dentry);
d18e9008
MS
2591
2592 if (create_error) {
2593 int open_flag = op->open_flag;
2594
2675a4eb 2595 error = create_error;
d18e9008
MS
2596 if ((open_flag & O_EXCL)) {
2597 if (!dentry->d_inode)
2598 goto out;
2599 } else if (!dentry->d_inode) {
2600 goto out;
2601 } else if ((open_flag & O_TRUNC) &&
2602 S_ISREG(dentry->d_inode->i_mode)) {
2603 goto out;
2604 }
2605 /* will fail later, go on to get the right error */
2606 }
2607 }
2608looked_up:
2609 path->dentry = dentry;
2610 path->mnt = nd->path.mnt;
2675a4eb 2611 return 1;
d18e9008
MS
2612}
2613
d58ffd35 2614/*
1acf0af9 2615 * Look up and maybe create and open the last component.
d58ffd35
MS
2616 *
2617 * Must be called with i_mutex held on parent.
2618 *
1acf0af9
DH
2619 * Returns 0 if the file was successfully atomically created (if necessary) and
2620 * opened. In this case the file will be returned attached to @file.
2621 *
2622 * Returns 1 if the file was not completely opened at this time, though lookups
2623 * and creations will have been performed and the dentry returned in @path will
2624 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2625 * specified then a negative dentry may be returned.
2626 *
2627 * An error code is returned otherwise.
2628 *
2629 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2630 * cleared otherwise prior to returning.
d58ffd35 2631 */
2675a4eb
AV
2632static int lookup_open(struct nameidata *nd, struct path *path,
2633 struct file *file,
2634 const struct open_flags *op,
64894cf8 2635 bool got_write, int *opened)
d58ffd35
MS
2636{
2637 struct dentry *dir = nd->path.dentry;
54ef4872 2638 struct inode *dir_inode = dir->d_inode;
d58ffd35
MS
2639 struct dentry *dentry;
2640 int error;
54ef4872 2641 bool need_lookup;
d58ffd35 2642
47237687 2643 *opened &= ~FILE_CREATED;
201f956e 2644 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
d58ffd35 2645 if (IS_ERR(dentry))
2675a4eb 2646 return PTR_ERR(dentry);
d58ffd35 2647
d18e9008
MS
2648 /* Cached positive dentry: will open in f_op->open */
2649 if (!need_lookup && dentry->d_inode)
2650 goto out_no_open;
2651
2652 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
64894cf8 2653 return atomic_open(nd, dentry, path, file, op, got_write,
47237687 2654 need_lookup, opened);
d18e9008
MS
2655 }
2656
54ef4872
MS
2657 if (need_lookup) {
2658 BUG_ON(dentry->d_inode);
2659
72bd866a 2660 dentry = lookup_real(dir_inode, dentry, nd->flags);
54ef4872 2661 if (IS_ERR(dentry))
2675a4eb 2662 return PTR_ERR(dentry);
54ef4872
MS
2663 }
2664
d58ffd35
MS
2665 /* Negative dentry, just create the file */
2666 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2667 umode_t mode = op->mode;
2668 if (!IS_POSIXACL(dir->d_inode))
2669 mode &= ~current_umask();
2670 /*
2671 * This write is needed to ensure that a
2672 * rw->ro transition does not occur between
2673 * the time when the file is created and when
2674 * a permanent write count is taken through
015c3bbc 2675 * the 'struct file' in finish_open().
d58ffd35 2676 */
64894cf8
AV
2677 if (!got_write) {
2678 error = -EROFS;
d58ffd35 2679 goto out_dput;
64894cf8 2680 }
47237687 2681 *opened |= FILE_CREATED;
d58ffd35
MS
2682 error = security_path_mknod(&nd->path, dentry, mode, 0);
2683 if (error)
2684 goto out_dput;
312b63fb
AV
2685 error = vfs_create(dir->d_inode, dentry, mode,
2686 nd->flags & LOOKUP_EXCL);
d58ffd35
MS
2687 if (error)
2688 goto out_dput;
2689 }
d18e9008 2690out_no_open:
d58ffd35
MS
2691 path->dentry = dentry;
2692 path->mnt = nd->path.mnt;
2675a4eb 2693 return 1;
d58ffd35
MS
2694
2695out_dput:
2696 dput(dentry);
2675a4eb 2697 return error;
d58ffd35
MS
2698}
2699
31e6b01f 2700/*
fe2d35ff 2701 * Handle the last step of open()
31e6b01f 2702 */
2675a4eb
AV
2703static int do_last(struct nameidata *nd, struct path *path,
2704 struct file *file, const struct open_flags *op,
669abf4e 2705 int *opened, struct filename *name)
fb1cc555 2706{
a1e28038 2707 struct dentry *dir = nd->path.dentry;
ca344a89 2708 int open_flag = op->open_flag;
77d660a8 2709 bool will_truncate = (open_flag & O_TRUNC) != 0;
64894cf8 2710 bool got_write = false;
bcda7652 2711 int acc_mode = op->acc_mode;
a1eb3315 2712 struct inode *inode;
77d660a8 2713 bool symlink_ok = false;
16b1c1cd
MS
2714 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2715 bool retried = false;
16c2cd71 2716 int error;
1f36f774 2717
c3e380b0
AV
2718 nd->flags &= ~LOOKUP_PARENT;
2719 nd->flags |= op->intent;
2720
1f36f774
AV
2721 switch (nd->last_type) {
2722 case LAST_DOTDOT:
176306f5 2723 case LAST_DOT:
fe2d35ff
AV
2724 error = handle_dots(nd, nd->last_type);
2725 if (error)
2675a4eb 2726 return error;
1f36f774 2727 /* fallthrough */
1f36f774 2728 case LAST_ROOT:
9f1fafee 2729 error = complete_walk(nd);
16c2cd71 2730 if (error)
2675a4eb 2731 return error;
adb5c247 2732 audit_inode(name, nd->path.dentry, 0);
ca344a89 2733 if (open_flag & O_CREAT) {
fe2d35ff 2734 error = -EISDIR;
2675a4eb 2735 goto out;
fe2d35ff 2736 }
e83db167 2737 goto finish_open;
1f36f774 2738 case LAST_BIND:
9f1fafee 2739 error = complete_walk(nd);
16c2cd71 2740 if (error)
2675a4eb 2741 return error;
adb5c247 2742 audit_inode(name, dir, 0);
e83db167 2743 goto finish_open;
1f36f774 2744 }
67ee3ad2 2745
ca344a89 2746 if (!(open_flag & O_CREAT)) {
fe2d35ff
AV
2747 if (nd->last.name[nd->last.len])
2748 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
bcda7652 2749 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
77d660a8 2750 symlink_ok = true;
fe2d35ff 2751 /* we _can_ be in RCU mode here */
e97cdc87 2752 error = lookup_fast(nd, path, &inode);
71574865
MS
2753 if (likely(!error))
2754 goto finish_lookup;
2755
2756 if (error < 0)
2675a4eb 2757 goto out;
71574865
MS
2758
2759 BUG_ON(nd->inode != dir->d_inode);
b6183df7
MS
2760 } else {
2761 /* create side of things */
2762 /*
2763 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2764 * has been cleared when we got to the last component we are
2765 * about to look up
2766 */
2767 error = complete_walk(nd);
2768 if (error)
2675a4eb 2769 return error;
fe2d35ff 2770
33e2208a 2771 audit_inode(name, dir, LOOKUP_PARENT);
b6183df7
MS
2772 error = -EISDIR;
2773 /* trailing slashes? */
2774 if (nd->last.name[nd->last.len])
2675a4eb 2775 goto out;
b6183df7 2776 }
a2c36b45 2777
16b1c1cd 2778retry_lookup:
64894cf8
AV
2779 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2780 error = mnt_want_write(nd->path.mnt);
2781 if (!error)
2782 got_write = true;
2783 /*
2784 * do _not_ fail yet - we might not need that or fail with
2785 * a different error; let lookup_open() decide; we'll be
2786 * dropping this one anyway.
2787 */
2788 }
a1e28038 2789 mutex_lock(&dir->d_inode->i_mutex);
64894cf8 2790 error = lookup_open(nd, path, file, op, got_write, opened);
d58ffd35 2791 mutex_unlock(&dir->d_inode->i_mutex);
a1e28038 2792
2675a4eb
AV
2793 if (error <= 0) {
2794 if (error)
d18e9008
MS
2795 goto out;
2796
47237687 2797 if ((*opened & FILE_CREATED) ||
496ad9aa 2798 !S_ISREG(file_inode(file)->i_mode))
77d660a8 2799 will_truncate = false;
d18e9008 2800
adb5c247 2801 audit_inode(name, file->f_path.dentry, 0);
d18e9008
MS
2802 goto opened;
2803 }
fb1cc555 2804
47237687 2805 if (*opened & FILE_CREATED) {
9b44f1b3 2806 /* Don't check for write permission, don't truncate */
ca344a89 2807 open_flag &= ~O_TRUNC;
77d660a8 2808 will_truncate = false;
bcda7652 2809 acc_mode = MAY_OPEN;
d58ffd35 2810 path_to_nameidata(path, nd);
e83db167 2811 goto finish_open_created;
fb1cc555
AV
2812 }
2813
2814 /*
3134f37e 2815 * create/update audit record if it already exists.
fb1cc555 2816 */
3134f37e 2817 if (path->dentry->d_inode)
adb5c247 2818 audit_inode(name, path->dentry, 0);
fb1cc555 2819
d18e9008
MS
2820 /*
2821 * If atomic_open() acquired write access it is dropped now due to
2822 * possible mount and symlink following (this might be optimized away if
2823 * necessary...)
2824 */
64894cf8 2825 if (got_write) {
d18e9008 2826 mnt_drop_write(nd->path.mnt);
64894cf8 2827 got_write = false;
d18e9008
MS
2828 }
2829
fb1cc555 2830 error = -EEXIST;
f8310c59 2831 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
fb1cc555
AV
2832 goto exit_dput;
2833
9875cf80
DH
2834 error = follow_managed(path, nd->flags);
2835 if (error < 0)
2836 goto exit_dput;
fb1cc555 2837
a3fbbde7
AV
2838 if (error)
2839 nd->flags |= LOOKUP_JUMPED;
2840
decf3400
MS
2841 BUG_ON(nd->flags & LOOKUP_RCU);
2842 inode = path->dentry->d_inode;
5f5daac1
MS
2843finish_lookup:
2844 /* we _can_ be in RCU mode here */
fb1cc555 2845 error = -ENOENT;
54c33e7f
MS
2846 if (!inode) {
2847 path_to_nameidata(path, nd);
2675a4eb 2848 goto out;
54c33e7f 2849 }
9e67f361 2850
d45ea867
MS
2851 if (should_follow_link(inode, !symlink_ok)) {
2852 if (nd->flags & LOOKUP_RCU) {
3b388f33
AV
2853 if (unlikely(nd->path.mnt != path->mnt ||
2854 unlazy_walk(nd, path->dentry))) {
d45ea867 2855 error = -ECHILD;
2675a4eb 2856 goto out;
d45ea867
MS
2857 }
2858 }
2859 BUG_ON(inode != path->dentry->d_inode);
2675a4eb 2860 return 1;
d45ea867 2861 }
fb1cc555 2862
16b1c1cd
MS
2863 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2864 path_to_nameidata(path, nd);
2865 } else {
2866 save_parent.dentry = nd->path.dentry;
2867 save_parent.mnt = mntget(path->mnt);
2868 nd->path.dentry = path->dentry;
2869
2870 }
decf3400 2871 nd->inode = inode;
a3fbbde7
AV
2872 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2873 error = complete_walk(nd);
16b1c1cd
MS
2874 if (error) {
2875 path_put(&save_parent);
2675a4eb 2876 return error;
16b1c1cd 2877 }
fb1cc555 2878 error = -EISDIR;
050ac841 2879 if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
2675a4eb 2880 goto out;
af2f5542 2881 error = -ENOTDIR;
05252901 2882 if ((nd->flags & LOOKUP_DIRECTORY) && !can_lookup(nd->inode))
2675a4eb 2883 goto out;
adb5c247 2884 audit_inode(name, nd->path.dentry, 0);
e83db167 2885finish_open:
6c0d46c4 2886 if (!S_ISREG(nd->inode->i_mode))
77d660a8 2887 will_truncate = false;
6c0d46c4 2888
0f9d1a10
AV
2889 if (will_truncate) {
2890 error = mnt_want_write(nd->path.mnt);
2891 if (error)
2675a4eb 2892 goto out;
64894cf8 2893 got_write = true;
0f9d1a10 2894 }
e83db167 2895finish_open_created:
bcda7652 2896 error = may_open(&nd->path, acc_mode, open_flag);
ca344a89 2897 if (error)
2675a4eb 2898 goto out;
30d90494
AV
2899 file->f_path.mnt = nd->path.mnt;
2900 error = finish_open(file, nd->path.dentry, NULL, opened);
2901 if (error) {
30d90494 2902 if (error == -EOPENSTALE)
f60dc3db 2903 goto stale_open;
015c3bbc 2904 goto out;
f60dc3db 2905 }
a8277b9b 2906opened:
2675a4eb 2907 error = open_check_o_direct(file);
015c3bbc
MS
2908 if (error)
2909 goto exit_fput;
2675a4eb 2910 error = ima_file_check(file, op->acc_mode);
aa4caadb
MS
2911 if (error)
2912 goto exit_fput;
2913
2914 if (will_truncate) {
2675a4eb 2915 error = handle_truncate(file);
aa4caadb
MS
2916 if (error)
2917 goto exit_fput;
0f9d1a10 2918 }
ca344a89 2919out:
1b153db5
AV
2920 if (unlikely(error > 0)) {
2921 WARN_ON(1);
2922 error = -EINVAL;
2923 }
64894cf8 2924 if (got_write)
0f9d1a10 2925 mnt_drop_write(nd->path.mnt);
16b1c1cd 2926 path_put(&save_parent);
e276ae67 2927 terminate_walk(nd);
2675a4eb 2928 return error;
fb1cc555 2929
fb1cc555
AV
2930exit_dput:
2931 path_put_conditional(path, nd);
ca344a89 2932 goto out;
015c3bbc 2933exit_fput:
2675a4eb
AV
2934 fput(file);
2935 goto out;
015c3bbc 2936
f60dc3db
MS
2937stale_open:
2938 /* If no saved parent or already retried then can't retry */
2939 if (!save_parent.dentry || retried)
2940 goto out;
2941
2942 BUG_ON(save_parent.dentry != dir);
2943 path_put(&nd->path);
2944 nd->path = save_parent;
2945 nd->inode = dir->d_inode;
2946 save_parent.mnt = NULL;
2947 save_parent.dentry = NULL;
64894cf8 2948 if (got_write) {
f60dc3db 2949 mnt_drop_write(nd->path.mnt);
64894cf8 2950 got_write = false;
f60dc3db
MS
2951 }
2952 retried = true;
2953 goto retry_lookup;
fb1cc555
AV
2954}
2955
669abf4e 2956static struct file *path_openat(int dfd, struct filename *pathname,
73d049a4 2957 struct nameidata *nd, const struct open_flags *op, int flags)
1da177e4 2958{
fe2d35ff 2959 struct file *base = NULL;
30d90494 2960 struct file *file;
9850c056 2961 struct path path;
47237687 2962 int opened = 0;
13aab428 2963 int error;
31e6b01f 2964
30d90494 2965 file = get_empty_filp();
1afc99be
AV
2966 if (IS_ERR(file))
2967 return file;
31e6b01f 2968
30d90494 2969 file->f_flags = op->open_flag;
31e6b01f 2970
669abf4e 2971 error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
31e6b01f 2972 if (unlikely(error))
2675a4eb 2973 goto out;
31e6b01f 2974
fe2d35ff 2975 current->total_link_count = 0;
669abf4e 2976 error = link_path_walk(pathname->name, nd);
31e6b01f 2977 if (unlikely(error))
2675a4eb 2978 goto out;
1da177e4 2979
2675a4eb
AV
2980 error = do_last(nd, &path, file, op, &opened, pathname);
2981 while (unlikely(error > 0)) { /* trailing symlink */
7b9337aa 2982 struct path link = path;
def4af30 2983 void *cookie;
574197e0 2984 if (!(nd->flags & LOOKUP_FOLLOW)) {
73d049a4
AV
2985 path_put_conditional(&path, nd);
2986 path_put(&nd->path);
2675a4eb 2987 error = -ELOOP;
40b39136
AV
2988 break;
2989 }
800179c9
KC
2990 error = may_follow_link(&link, nd);
2991 if (unlikely(error))
2992 break;
73d049a4
AV
2993 nd->flags |= LOOKUP_PARENT;
2994 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
574197e0 2995 error = follow_link(&link, nd, &cookie);
c3e380b0 2996 if (unlikely(error))
2675a4eb
AV
2997 break;
2998 error = do_last(nd, &path, file, op, &opened, pathname);
574197e0 2999 put_link(nd, &link, cookie);
806b681c 3000 }
10fa8e62 3001out:
73d049a4
AV
3002 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
3003 path_put(&nd->root);
fe2d35ff
AV
3004 if (base)
3005 fput(base);
2675a4eb
AV
3006 if (!(opened & FILE_OPENED)) {
3007 BUG_ON(!error);
30d90494 3008 put_filp(file);
16b1c1cd 3009 }
2675a4eb
AV
3010 if (unlikely(error)) {
3011 if (error == -EOPENSTALE) {
3012 if (flags & LOOKUP_RCU)
3013 error = -ECHILD;
3014 else
3015 error = -ESTALE;
3016 }
3017 file = ERR_PTR(error);
3018 }
3019 return file;
1da177e4
LT
3020}
3021
669abf4e 3022struct file *do_filp_open(int dfd, struct filename *pathname,
13aab428
AV
3023 const struct open_flags *op, int flags)
3024{
73d049a4 3025 struct nameidata nd;
13aab428
AV
3026 struct file *filp;
3027
73d049a4 3028 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
13aab428 3029 if (unlikely(filp == ERR_PTR(-ECHILD)))
73d049a4 3030 filp = path_openat(dfd, pathname, &nd, op, flags);
13aab428 3031 if (unlikely(filp == ERR_PTR(-ESTALE)))
73d049a4 3032 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
13aab428
AV
3033 return filp;
3034}
3035
73d049a4
AV
3036struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3037 const char *name, const struct open_flags *op, int flags)
3038{
3039 struct nameidata nd;
3040 struct file *file;
669abf4e 3041 struct filename filename = { .name = name };
73d049a4
AV
3042
3043 nd.root.mnt = mnt;
3044 nd.root.dentry = dentry;
3045
3046 flags |= LOOKUP_ROOT;
3047
bcda7652 3048 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
73d049a4
AV
3049 return ERR_PTR(-ELOOP);
3050
669abf4e 3051 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
73d049a4 3052 if (unlikely(file == ERR_PTR(-ECHILD)))
669abf4e 3053 file = path_openat(-1, &filename, &nd, op, flags);
73d049a4 3054 if (unlikely(file == ERR_PTR(-ESTALE)))
669abf4e 3055 file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
73d049a4
AV
3056 return file;
3057}
3058
1ac12b4b
JL
3059struct dentry *kern_path_create(int dfd, const char *pathname,
3060 struct path *path, unsigned int lookup_flags)
1da177e4 3061{
c663e5d8 3062 struct dentry *dentry = ERR_PTR(-EEXIST);
ed75e95d 3063 struct nameidata nd;
c30dabfe 3064 int err2;
1ac12b4b
JL
3065 int error;
3066 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3067
3068 /*
3069 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3070 * other flags passed in are ignored!
3071 */
3072 lookup_flags &= LOOKUP_REVAL;
3073
3074 error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
ed75e95d
AV
3075 if (error)
3076 return ERR_PTR(error);
1da177e4 3077
c663e5d8
CH
3078 /*
3079 * Yucky last component or no last component at all?
3080 * (foo/., foo/.., /////)
3081 */
ed75e95d
AV
3082 if (nd.last_type != LAST_NORM)
3083 goto out;
3084 nd.flags &= ~LOOKUP_PARENT;
3085 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
c663e5d8 3086
c30dabfe
JK
3087 /* don't fail immediately if it's r/o, at least try to report other errors */
3088 err2 = mnt_want_write(nd.path.mnt);
c663e5d8
CH
3089 /*
3090 * Do the final lookup.
3091 */
ed75e95d
AV
3092 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3093 dentry = lookup_hash(&nd);
1da177e4 3094 if (IS_ERR(dentry))
a8104a9f 3095 goto unlock;
c663e5d8 3096
a8104a9f 3097 error = -EEXIST;
e9baf6e5 3098 if (dentry->d_inode)
a8104a9f 3099 goto fail;
c663e5d8
CH
3100 /*
3101 * Special case - lookup gave negative, but... we had foo/bar/
3102 * From the vfs_mknod() POV we just have a negative dentry -
3103 * all is fine. Let's be bastards - you had / on the end, you've
3104 * been asking for (non-existent) directory. -ENOENT for you.
3105 */
ed75e95d 3106 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
a8104a9f 3107 error = -ENOENT;
ed75e95d 3108 goto fail;
e9baf6e5 3109 }
c30dabfe
JK
3110 if (unlikely(err2)) {
3111 error = err2;
a8104a9f 3112 goto fail;
c30dabfe 3113 }
ed75e95d 3114 *path = nd.path;
1da177e4 3115 return dentry;
1da177e4 3116fail:
a8104a9f
AV
3117 dput(dentry);
3118 dentry = ERR_PTR(error);
3119unlock:
ed75e95d 3120 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
c30dabfe
JK
3121 if (!err2)
3122 mnt_drop_write(nd.path.mnt);
ed75e95d
AV
3123out:
3124 path_put(&nd.path);
1da177e4
LT
3125 return dentry;
3126}
dae6ad8f
AV
3127EXPORT_SYMBOL(kern_path_create);
3128
921a1650
AV
3129void done_path_create(struct path *path, struct dentry *dentry)
3130{
3131 dput(dentry);
3132 mutex_unlock(&path->dentry->d_inode->i_mutex);
a8104a9f 3133 mnt_drop_write(path->mnt);
921a1650
AV
3134 path_put(path);
3135}
3136EXPORT_SYMBOL(done_path_create);
3137
1ac12b4b
JL
3138struct dentry *user_path_create(int dfd, const char __user *pathname,
3139 struct path *path, unsigned int lookup_flags)
dae6ad8f 3140{
91a27b2a 3141 struct filename *tmp = getname(pathname);
dae6ad8f
AV
3142 struct dentry *res;
3143 if (IS_ERR(tmp))
3144 return ERR_CAST(tmp);
1ac12b4b 3145 res = kern_path_create(dfd, tmp->name, path, lookup_flags);
dae6ad8f
AV
3146 putname(tmp);
3147 return res;
3148}
3149EXPORT_SYMBOL(user_path_create);
3150
1a67aafb 3151int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
1da177e4 3152{
a95164d9 3153 int error = may_create(dir, dentry);
1da177e4
LT
3154
3155 if (error)
3156 return error;
3157
975d6b39 3158 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1da177e4
LT
3159 return -EPERM;
3160
acfa4380 3161 if (!dir->i_op->mknod)
1da177e4
LT
3162 return -EPERM;
3163
08ce5f16
SH
3164 error = devcgroup_inode_mknod(mode, dev);
3165 if (error)
3166 return error;
3167
1da177e4
LT
3168 error = security_inode_mknod(dir, dentry, mode, dev);
3169 if (error)
3170 return error;
3171
1da177e4 3172 error = dir->i_op->mknod(dir, dentry, mode, dev);
a74574aa 3173 if (!error)
f38aa942 3174 fsnotify_create(dir, dentry);
1da177e4
LT
3175 return error;
3176}
3177
f69aac00 3178static int may_mknod(umode_t mode)
463c3197
DH
3179{
3180 switch (mode & S_IFMT) {
3181 case S_IFREG:
3182 case S_IFCHR:
3183 case S_IFBLK:
3184 case S_IFIFO:
3185 case S_IFSOCK:
3186 case 0: /* zero mode translates to S_IFREG */
3187 return 0;
3188 case S_IFDIR:
3189 return -EPERM;
3190 default:
3191 return -EINVAL;
3192 }
3193}
3194
8208a22b 3195SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
2e4d0924 3196 unsigned, dev)
1da177e4 3197{
2ad94ae6 3198 struct dentry *dentry;
dae6ad8f
AV
3199 struct path path;
3200 int error;
972567f1 3201 unsigned int lookup_flags = 0;
1da177e4 3202
8e4bfca1
AV
3203 error = may_mknod(mode);
3204 if (error)
3205 return error;
972567f1
JL
3206retry:
3207 dentry = user_path_create(dfd, filename, &path, lookup_flags);
dae6ad8f
AV
3208 if (IS_ERR(dentry))
3209 return PTR_ERR(dentry);
2ad94ae6 3210
dae6ad8f 3211 if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d 3212 mode &= ~current_umask();
dae6ad8f 3213 error = security_path_mknod(&path, dentry, mode, dev);
be6d3e56 3214 if (error)
a8104a9f 3215 goto out;
463c3197 3216 switch (mode & S_IFMT) {
1da177e4 3217 case 0: case S_IFREG:
312b63fb 3218 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
1da177e4
LT
3219 break;
3220 case S_IFCHR: case S_IFBLK:
dae6ad8f 3221 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
1da177e4
LT
3222 new_decode_dev(dev));
3223 break;
3224 case S_IFIFO: case S_IFSOCK:
dae6ad8f 3225 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
1da177e4 3226 break;
1da177e4 3227 }
a8104a9f 3228out:
921a1650 3229 done_path_create(&path, dentry);
972567f1
JL
3230 if (retry_estale(error, lookup_flags)) {
3231 lookup_flags |= LOOKUP_REVAL;
3232 goto retry;
3233 }
1da177e4
LT
3234 return error;
3235}
3236
8208a22b 3237SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
5590ff0d
UD
3238{
3239 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3240}
3241
18bb1db3 3242int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1da177e4 3243{
a95164d9 3244 int error = may_create(dir, dentry);
8de52778 3245 unsigned max_links = dir->i_sb->s_max_links;
1da177e4
LT
3246
3247 if (error)
3248 return error;
3249
acfa4380 3250 if (!dir->i_op->mkdir)
1da177e4
LT
3251 return -EPERM;
3252
3253 mode &= (S_IRWXUGO|S_ISVTX);
3254 error = security_inode_mkdir(dir, dentry, mode);
3255 if (error)
3256 return error;
3257
8de52778
AV
3258 if (max_links && dir->i_nlink >= max_links)
3259 return -EMLINK;
3260
1da177e4 3261 error = dir->i_op->mkdir(dir, dentry, mode);
a74574aa 3262 if (!error)
f38aa942 3263 fsnotify_mkdir(dir, dentry);
1da177e4
LT
3264 return error;
3265}
3266
a218d0fd 3267SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
1da177e4 3268{
6902d925 3269 struct dentry *dentry;
dae6ad8f
AV
3270 struct path path;
3271 int error;
b76d8b82 3272 unsigned int lookup_flags = LOOKUP_DIRECTORY;
1da177e4 3273
b76d8b82
JL
3274retry:
3275 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
6902d925 3276 if (IS_ERR(dentry))
dae6ad8f 3277 return PTR_ERR(dentry);
1da177e4 3278
dae6ad8f 3279 if (!IS_POSIXACL(path.dentry->d_inode))
ce3b0f8d 3280 mode &= ~current_umask();
dae6ad8f 3281 error = security_path_mkdir(&path, dentry, mode);
a8104a9f
AV
3282 if (!error)
3283 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
921a1650 3284 done_path_create(&path, dentry);
b76d8b82
JL
3285 if (retry_estale(error, lookup_flags)) {
3286 lookup_flags |= LOOKUP_REVAL;
3287 goto retry;
3288 }
1da177e4
LT
3289 return error;
3290}
3291
a218d0fd 3292SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
5590ff0d
UD
3293{
3294 return sys_mkdirat(AT_FDCWD, pathname, mode);
3295}
3296
1da177e4 3297/*
a71905f0 3298 * The dentry_unhash() helper will try to drop the dentry early: we
c0d02594 3299 * should have a usage count of 1 if we're the only user of this
a71905f0
SW
3300 * dentry, and if that is true (possibly after pruning the dcache),
3301 * then we drop the dentry now.
1da177e4
LT
3302 *
3303 * A low-level filesystem can, if it choses, legally
3304 * do a
3305 *
3306 * if (!d_unhashed(dentry))
3307 * return -EBUSY;
3308 *
3309 * if it cannot handle the case of removing a directory
3310 * that is still in use by something else..
3311 */
3312void dentry_unhash(struct dentry *dentry)
3313{
dc168427 3314 shrink_dcache_parent(dentry);
1da177e4 3315 spin_lock(&dentry->d_lock);
64252c75 3316 if (dentry->d_count == 1)
1da177e4
LT
3317 __d_drop(dentry);
3318 spin_unlock(&dentry->d_lock);
1da177e4
LT
3319}
3320
3321int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3322{
3323 int error = may_delete(dir, dentry, 1);
3324
3325 if (error)
3326 return error;
3327
acfa4380 3328 if (!dir->i_op->rmdir)
1da177e4
LT
3329 return -EPERM;
3330
1d2ef590 3331 dget(dentry);
1b1dcc1b 3332 mutex_lock(&dentry->d_inode->i_mutex);
912dbc15
SW
3333
3334 error = -EBUSY;
1da177e4 3335 if (d_mountpoint(dentry))
912dbc15
SW
3336 goto out;
3337
3338 error = security_inode_rmdir(dir, dentry);
3339 if (error)
3340 goto out;
3341
3cebde24 3342 shrink_dcache_parent(dentry);
912dbc15
SW
3343 error = dir->i_op->rmdir(dir, dentry);
3344 if (error)
3345 goto out;
3346
3347 dentry->d_inode->i_flags |= S_DEAD;
3348 dont_mount(dentry);
3349
3350out:
1b1dcc1b 3351 mutex_unlock(&dentry->d_inode->i_mutex);
1d2ef590 3352 dput(dentry);
912dbc15 3353 if (!error)
1da177e4 3354 d_delete(dentry);
1da177e4
LT
3355 return error;
3356}
3357
5590ff0d 3358static long do_rmdir(int dfd, const char __user *pathname)
1da177e4
LT
3359{
3360 int error = 0;
91a27b2a 3361 struct filename *name;
1da177e4
LT
3362 struct dentry *dentry;
3363 struct nameidata nd;
c6ee9206
JL
3364 unsigned int lookup_flags = 0;
3365retry:
3366 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
91a27b2a
JL
3367 if (IS_ERR(name))
3368 return PTR_ERR(name);
1da177e4
LT
3369
3370 switch(nd.last_type) {
0612d9fb
OH
3371 case LAST_DOTDOT:
3372 error = -ENOTEMPTY;
3373 goto exit1;
3374 case LAST_DOT:
3375 error = -EINVAL;
3376 goto exit1;
3377 case LAST_ROOT:
3378 error = -EBUSY;
3379 goto exit1;
1da177e4 3380 }
0612d9fb
OH
3381
3382 nd.flags &= ~LOOKUP_PARENT;
c30dabfe
JK
3383 error = mnt_want_write(nd.path.mnt);
3384 if (error)
3385 goto exit1;
0612d9fb 3386
4ac91378 3387 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
49705b77 3388 dentry = lookup_hash(&nd);
1da177e4 3389 error = PTR_ERR(dentry);
6902d925
DH
3390 if (IS_ERR(dentry))
3391 goto exit2;
e6bc45d6
TT
3392 if (!dentry->d_inode) {
3393 error = -ENOENT;
3394 goto exit3;
3395 }
be6d3e56
KT
3396 error = security_path_rmdir(&nd.path, dentry);
3397 if (error)
c30dabfe 3398 goto exit3;
4ac91378 3399 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
0622753b 3400exit3:
6902d925
DH
3401 dput(dentry);
3402exit2:
4ac91378 3403 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
c30dabfe 3404 mnt_drop_write(nd.path.mnt);
1da177e4 3405exit1:
1d957f9b 3406 path_put(&nd.path);
1da177e4 3407 putname(name);
c6ee9206
JL
3408 if (retry_estale(error, lookup_flags)) {
3409 lookup_flags |= LOOKUP_REVAL;
3410 goto retry;
3411 }
1da177e4
LT
3412 return error;
3413}
3414
3cdad428 3415SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
5590ff0d
UD
3416{
3417 return do_rmdir(AT_FDCWD, pathname);
3418}
3419
1da177e4
LT
3420int vfs_unlink(struct inode *dir, struct dentry *dentry)
3421{
3422 int error = may_delete(dir, dentry, 0);
3423
3424 if (error)
3425 return error;
3426
acfa4380 3427 if (!dir->i_op->unlink)
1da177e4
LT
3428 return -EPERM;
3429
1b1dcc1b 3430 mutex_lock(&dentry->d_inode->i_mutex);
1da177e4
LT
3431 if (d_mountpoint(dentry))
3432 error = -EBUSY;
3433 else {
3434 error = security_inode_unlink(dir, dentry);
bec1052e 3435 if (!error) {
1da177e4 3436 error = dir->i_op->unlink(dir, dentry);
bec1052e 3437 if (!error)
d83c49f3 3438 dont_mount(dentry);
bec1052e 3439 }
1da177e4 3440 }
1b1dcc1b 3441 mutex_unlock(&dentry->d_inode->i_mutex);
1da177e4
LT
3442
3443 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3444 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
ece95912 3445 fsnotify_link_count(dentry->d_inode);
e234f35c 3446 d_delete(dentry);
1da177e4 3447 }
0eeca283 3448
1da177e4
LT
3449 return error;
3450}
3451
3452/*
3453 * Make sure that the actual truncation of the file will occur outside its
1b1dcc1b 3454 * directory's i_mutex. Truncate can take a long time if there is a lot of
1da177e4
LT
3455 * writeout happening, and we don't want to prevent access to the directory
3456 * while waiting on the I/O.
3457 */
5590ff0d 3458static long do_unlinkat(int dfd, const char __user *pathname)
1da177e4 3459{
2ad94ae6 3460 int error;
91a27b2a 3461 struct filename *name;
1da177e4
LT
3462 struct dentry *dentry;
3463 struct nameidata nd;
3464 struct inode *inode = NULL;
5d18f813
JL
3465 unsigned int lookup_flags = 0;
3466retry:
3467 name = user_path_parent(dfd, pathname, &nd, lookup_flags);
91a27b2a
JL
3468 if (IS_ERR(name))
3469 return PTR_ERR(name);
2ad94ae6 3470
1da177e4
LT
3471 error = -EISDIR;
3472 if (nd.last_type != LAST_NORM)
3473 goto exit1;
0612d9fb
OH
3474
3475 nd.flags &= ~LOOKUP_PARENT;
c30dabfe
JK
3476 error = mnt_want_write(nd.path.mnt);
3477 if (error)
3478 goto exit1;
0612d9fb 3479
4ac91378 3480 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
49705b77 3481 dentry = lookup_hash(&nd);
1da177e4
LT
3482 error = PTR_ERR(dentry);
3483 if (!IS_ERR(dentry)) {
3484 /* Why not before? Because we want correct error value */
50338b88
TE
3485 if (nd.last.name[nd.last.len])
3486 goto slashes;
1da177e4 3487 inode = dentry->d_inode;
50338b88 3488 if (!inode)
e6bc45d6
TT
3489 goto slashes;
3490 ihold(inode);
be6d3e56
KT
3491 error = security_path_unlink(&nd.path, dentry);
3492 if (error)
c30dabfe 3493 goto exit2;
4ac91378 3494 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
c30dabfe 3495exit2:
1da177e4
LT
3496 dput(dentry);
3497 }
4ac91378 3498 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1da177e4
LT
3499 if (inode)
3500 iput(inode); /* truncate the inode here */
c30dabfe 3501 mnt_drop_write(nd.path.mnt);
1da177e4 3502exit1:
1d957f9b 3503 path_put(&nd.path);
1da177e4 3504 putname(name);
5d18f813
JL
3505 if (retry_estale(error, lookup_flags)) {
3506 lookup_flags |= LOOKUP_REVAL;
3507 inode = NULL;
3508 goto retry;
3509 }
1da177e4
LT
3510 return error;
3511
3512slashes:
3513 error = !dentry->d_inode ? -ENOENT :
3514 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
3515 goto exit2;
3516}
3517
2e4d0924 3518SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
5590ff0d
UD
3519{
3520 if ((flag & ~AT_REMOVEDIR) != 0)
3521 return -EINVAL;
3522
3523 if (flag & AT_REMOVEDIR)
3524 return do_rmdir(dfd, pathname);
3525
3526 return do_unlinkat(dfd, pathname);
3527}
3528
3480b257 3529SYSCALL_DEFINE1(unlink, const char __user *, pathname)
5590ff0d
UD
3530{
3531 return do_unlinkat(AT_FDCWD, pathname);
3532}
3533
db2e747b 3534int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1da177e4 3535{
a95164d9 3536 int error = may_create(dir, dentry);
1da177e4
LT
3537
3538 if (error)
3539 return error;
3540
acfa4380 3541 if (!dir->i_op->symlink)
1da177e4
LT
3542 return -EPERM;
3543
3544 error = security_inode_symlink(dir, dentry, oldname);
3545 if (error)
3546 return error;
3547
1da177e4 3548 error = dir->i_op->symlink(dir, dentry, oldname);
a74574aa 3549 if (!error)
f38aa942 3550 fsnotify_create(dir, dentry);
1da177e4
LT
3551 return error;
3552}
3553
2e4d0924
HC
3554SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3555 int, newdfd, const char __user *, newname)
1da177e4 3556{
2ad94ae6 3557 int error;
91a27b2a 3558 struct filename *from;
6902d925 3559 struct dentry *dentry;
dae6ad8f 3560 struct path path;
f46d3567 3561 unsigned int lookup_flags = 0;
1da177e4
LT
3562
3563 from = getname(oldname);
2ad94ae6 3564 if (IS_ERR(from))
1da177e4 3565 return PTR_ERR(from);
f46d3567
JL
3566retry:
3567 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
6902d925
DH
3568 error = PTR_ERR(dentry);
3569 if (IS_ERR(dentry))
dae6ad8f 3570 goto out_putname;
6902d925 3571
91a27b2a 3572 error = security_path_symlink(&path, dentry, from->name);
a8104a9f 3573 if (!error)
91a27b2a 3574 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
921a1650 3575 done_path_create(&path, dentry);
f46d3567
JL
3576 if (retry_estale(error, lookup_flags)) {
3577 lookup_flags |= LOOKUP_REVAL;
3578 goto retry;
3579 }
6902d925 3580out_putname:
1da177e4
LT
3581 putname(from);
3582 return error;
3583}
3584
3480b257 3585SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
5590ff0d
UD
3586{
3587 return sys_symlinkat(oldname, AT_FDCWD, newname);
3588}
3589
1da177e4
LT
3590int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3591{
3592 struct inode *inode = old_dentry->d_inode;
8de52778 3593 unsigned max_links = dir->i_sb->s_max_links;
1da177e4
LT
3594 int error;
3595
3596 if (!inode)
3597 return -ENOENT;
3598
a95164d9 3599 error = may_create(dir, new_dentry);
1da177e4
LT
3600 if (error)
3601 return error;
3602
3603 if (dir->i_sb != inode->i_sb)
3604 return -EXDEV;
3605
3606 /*
3607 * A link to an append-only or immutable file cannot be created.
3608 */
3609 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3610 return -EPERM;
acfa4380 3611 if (!dir->i_op->link)
1da177e4 3612 return -EPERM;
7e79eedb 3613 if (S_ISDIR(inode->i_mode))
1da177e4
LT
3614 return -EPERM;
3615
3616 error = security_inode_link(old_dentry, dir, new_dentry);
3617 if (error)
3618 return error;
3619
7e79eedb 3620 mutex_lock(&inode->i_mutex);
aae8a97d
AK
3621 /* Make sure we don't allow creating hardlink to an unlinked file */
3622 if (inode->i_nlink == 0)
3623 error = -ENOENT;
8de52778
AV
3624 else if (max_links && inode->i_nlink >= max_links)
3625 error = -EMLINK;
aae8a97d
AK
3626 else
3627 error = dir->i_op->link(old_dentry, dir, new_dentry);
7e79eedb 3628 mutex_unlock(&inode->i_mutex);
e31e14ec 3629 if (!error)
7e79eedb 3630 fsnotify_link(dir, inode, new_dentry);
1da177e4
LT
3631 return error;
3632}
3633
3634/*
3635 * Hardlinks are often used in delicate situations. We avoid
3636 * security-related surprises by not following symlinks on the
3637 * newname. --KAB
3638 *
3639 * We don't follow them on the oldname either to be compatible
3640 * with linux 2.0, and to avoid hard-linking to directories
3641 * and other special files. --ADM
3642 */
2e4d0924
HC
3643SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3644 int, newdfd, const char __user *, newname, int, flags)
1da177e4
LT
3645{
3646 struct dentry *new_dentry;
dae6ad8f 3647 struct path old_path, new_path;
11a7b371 3648 int how = 0;
1da177e4 3649 int error;
1da177e4 3650
11a7b371 3651 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
c04030e1 3652 return -EINVAL;
11a7b371
AK
3653 /*
3654 * To use null names we require CAP_DAC_READ_SEARCH
3655 * This ensures that not everyone will be able to create
3656 * handlink using the passed filedescriptor.
3657 */
3658 if (flags & AT_EMPTY_PATH) {
3659 if (!capable(CAP_DAC_READ_SEARCH))
3660 return -ENOENT;
3661 how = LOOKUP_EMPTY;
3662 }
3663
3664 if (flags & AT_SYMLINK_FOLLOW)
3665 how |= LOOKUP_FOLLOW;
442e31ca 3666retry:
11a7b371 3667 error = user_path_at(olddfd, oldname, how, &old_path);
1da177e4 3668 if (error)
2ad94ae6
AV
3669 return error;
3670
442e31ca
JL
3671 new_dentry = user_path_create(newdfd, newname, &new_path,
3672 (how & LOOKUP_REVAL));
1da177e4 3673 error = PTR_ERR(new_dentry);
6902d925 3674 if (IS_ERR(new_dentry))
dae6ad8f
AV
3675 goto out;
3676
3677 error = -EXDEV;
3678 if (old_path.mnt != new_path.mnt)
3679 goto out_dput;
800179c9
KC
3680 error = may_linkat(&old_path);
3681 if (unlikely(error))
3682 goto out_dput;
dae6ad8f 3683 error = security_path_link(old_path.dentry, &new_path, new_dentry);
be6d3e56 3684 if (error)
a8104a9f 3685 goto out_dput;
dae6ad8f 3686 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
75c3f29d 3687out_dput:
921a1650 3688 done_path_create(&new_path, new_dentry);
442e31ca 3689 if (retry_estale(error, how)) {
f1352fb2 3690 path_put(&old_path);
442e31ca
JL
3691 how |= LOOKUP_REVAL;
3692 goto retry;
3693 }
1da177e4 3694out:
2d8f3038 3695 path_put(&old_path);
1da177e4
LT
3696
3697 return error;
3698}
3699
3480b257 3700SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
5590ff0d 3701{
c04030e1 3702 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
5590ff0d
UD
3703}
3704
1da177e4
LT
3705/*
3706 * The worst of all namespace operations - renaming directory. "Perverted"
3707 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3708 * Problems:
3709 * a) we can get into loop creation. Check is done in is_subdir().
3710 * b) race potential - two innocent renames can create a loop together.
3711 * That's where 4.4 screws up. Current fix: serialization on
a11f3a05 3712 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
1da177e4
LT
3713 * story.
3714 * c) we have to lock _three_ objects - parents and victim (if it exists).
1b1dcc1b 3715 * And that - after we got ->i_mutex on parents (until then we don't know
1da177e4
LT
3716 * whether the target exists). Solution: try to be smart with locking
3717 * order for inodes. We rely on the fact that tree topology may change
a11f3a05 3718 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
1da177e4
LT
3719 * move will be locked. Thus we can rank directories by the tree
3720 * (ancestors first) and rank all non-directories after them.
3721 * That works since everybody except rename does "lock parent, lookup,
a11f3a05 3722 * lock child" and rename is under ->s_vfs_rename_mutex.
1da177e4
LT
3723 * HOWEVER, it relies on the assumption that any object with ->lookup()
3724 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3725 * we'd better make sure that there's no link(2) for them.
e4eaac06 3726 * d) conversion from fhandle to dentry may come in the wrong moment - when
1b1dcc1b 3727 * we are removing the target. Solution: we will have to grab ->i_mutex
1da177e4 3728 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
c41b20e7 3729 * ->i_mutex on parents, which works but leads to some truly excessive
1da177e4
LT
3730 * locking].
3731 */
75c96f85
AB
3732static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3733 struct inode *new_dir, struct dentry *new_dentry)
1da177e4
LT
3734{
3735 int error = 0;
9055cba7 3736 struct inode *target = new_dentry->d_inode;
8de52778 3737 unsigned max_links = new_dir->i_sb->s_max_links;
1da177e4
LT
3738
3739 /*
3740 * If we are going to change the parent - check write permissions,
3741 * we'll need to flip '..'.
3742 */
3743 if (new_dir != old_dir) {
f419a2e3 3744 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
1da177e4
LT
3745 if (error)
3746 return error;
3747 }
3748
3749 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3750 if (error)
3751 return error;
3752
1d2ef590 3753 dget(new_dentry);
d83c49f3 3754 if (target)
1b1dcc1b 3755 mutex_lock(&target->i_mutex);
9055cba7
SW
3756
3757 error = -EBUSY;
3758 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3759 goto out;
3760
8de52778
AV
3761 error = -EMLINK;
3762 if (max_links && !target && new_dir != old_dir &&
3763 new_dir->i_nlink >= max_links)
3764 goto out;
3765
3cebde24
SW
3766 if (target)
3767 shrink_dcache_parent(new_dentry);
9055cba7
SW
3768 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3769 if (error)
3770 goto out;
3771
1da177e4 3772 if (target) {
9055cba7
SW
3773 target->i_flags |= S_DEAD;
3774 dont_mount(new_dentry);
1da177e4 3775 }
9055cba7
SW
3776out:
3777 if (target)
3778 mutex_unlock(&target->i_mutex);
1d2ef590 3779 dput(new_dentry);
e31e14ec 3780 if (!error)
349457cc
MF
3781 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3782 d_move(old_dentry,new_dentry);
1da177e4
LT
3783 return error;
3784}
3785
75c96f85
AB
3786static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3787 struct inode *new_dir, struct dentry *new_dentry)
1da177e4 3788{
51892bbb 3789 struct inode *target = new_dentry->d_inode;
1da177e4
LT
3790 int error;
3791
3792 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3793 if (error)
3794 return error;
3795
3796 dget(new_dentry);
1da177e4 3797 if (target)
1b1dcc1b 3798 mutex_lock(&target->i_mutex);
51892bbb
SW
3799
3800 error = -EBUSY;
1da177e4 3801 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
51892bbb
SW
3802 goto out;
3803
3804 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3805 if (error)
3806 goto out;
3807
3808 if (target)
3809 dont_mount(new_dentry);
3810 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3811 d_move(old_dentry, new_dentry);
3812out:
1da177e4 3813 if (target)
1b1dcc1b 3814 mutex_unlock(&target->i_mutex);
1da177e4
LT
3815 dput(new_dentry);
3816 return error;
3817}
3818
3819int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3820 struct inode *new_dir, struct dentry *new_dentry)
3821{
3822 int error;
3823 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
59b0df21 3824 const unsigned char *old_name;
1da177e4
LT
3825
3826 if (old_dentry->d_inode == new_dentry->d_inode)
3827 return 0;
3828
3829 error = may_delete(old_dir, old_dentry, is_dir);
3830 if (error)
3831 return error;
3832
3833 if (!new_dentry->d_inode)
a95164d9 3834 error = may_create(new_dir, new_dentry);
1da177e4
LT
3835 else
3836 error = may_delete(new_dir, new_dentry, is_dir);
3837 if (error)
3838 return error;
3839
acfa4380 3840 if (!old_dir->i_op->rename)
1da177e4
LT
3841 return -EPERM;
3842
0eeca283
RL
3843 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3844
1da177e4
LT
3845 if (is_dir)
3846 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3847 else
3848 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
123df294
AV
3849 if (!error)
3850 fsnotify_move(old_dir, new_dir, old_name, is_dir,
5a190ae6 3851 new_dentry->d_inode, old_dentry);
0eeca283
RL
3852 fsnotify_oldname_free(old_name);
3853
1da177e4
LT
3854 return error;
3855}
3856
2e4d0924
HC
3857SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3858 int, newdfd, const char __user *, newname)
1da177e4 3859{
2ad94ae6
AV
3860 struct dentry *old_dir, *new_dir;
3861 struct dentry *old_dentry, *new_dentry;
3862 struct dentry *trap;
1da177e4 3863 struct nameidata oldnd, newnd;
91a27b2a
JL
3864 struct filename *from;
3865 struct filename *to;
c6a94284
JL
3866 unsigned int lookup_flags = 0;
3867 bool should_retry = false;
2ad94ae6 3868 int error;
c6a94284
JL
3869retry:
3870 from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
91a27b2a
JL
3871 if (IS_ERR(from)) {
3872 error = PTR_ERR(from);
1da177e4 3873 goto exit;
91a27b2a 3874 }
1da177e4 3875
c6a94284 3876 to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
91a27b2a
JL
3877 if (IS_ERR(to)) {
3878 error = PTR_ERR(to);
1da177e4 3879 goto exit1;
91a27b2a 3880 }
1da177e4
LT
3881
3882 error = -EXDEV;
4ac91378 3883 if (oldnd.path.mnt != newnd.path.mnt)
1da177e4
LT
3884 goto exit2;
3885
4ac91378 3886 old_dir = oldnd.path.dentry;
1da177e4
LT
3887 error = -EBUSY;
3888 if (oldnd.last_type != LAST_NORM)
3889 goto exit2;
3890
4ac91378 3891 new_dir = newnd.path.dentry;
1da177e4
LT
3892 if (newnd.last_type != LAST_NORM)
3893 goto exit2;
3894
c30dabfe
JK
3895 error = mnt_want_write(oldnd.path.mnt);
3896 if (error)
3897 goto exit2;
3898
0612d9fb
OH
3899 oldnd.flags &= ~LOOKUP_PARENT;
3900 newnd.flags &= ~LOOKUP_PARENT;
4e9ed2f8 3901 newnd.flags |= LOOKUP_RENAME_TARGET;
0612d9fb 3902
1da177e4
LT
3903 trap = lock_rename(new_dir, old_dir);
3904
49705b77 3905 old_dentry = lookup_hash(&oldnd);
1da177e4
LT
3906 error = PTR_ERR(old_dentry);
3907 if (IS_ERR(old_dentry))
3908 goto exit3;
3909 /* source must exist */
3910 error = -ENOENT;
3911 if (!old_dentry->d_inode)
3912 goto exit4;
3913 /* unless the source is a directory trailing slashes give -ENOTDIR */
3914 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3915 error = -ENOTDIR;
3916 if (oldnd.last.name[oldnd.last.len])
3917 goto exit4;
3918 if (newnd.last.name[newnd.last.len])
3919 goto exit4;
3920 }
3921 /* source should not be ancestor of target */
3922 error = -EINVAL;
3923 if (old_dentry == trap)
3924 goto exit4;
49705b77 3925 new_dentry = lookup_hash(&newnd);
1da177e4
LT
3926 error = PTR_ERR(new_dentry);
3927 if (IS_ERR(new_dentry))
3928 goto exit4;
3929 /* target should not be an ancestor of source */
3930 error = -ENOTEMPTY;
3931 if (new_dentry == trap)
3932 goto exit5;
3933
be6d3e56
KT
3934 error = security_path_rename(&oldnd.path, old_dentry,
3935 &newnd.path, new_dentry);
3936 if (error)
c30dabfe 3937 goto exit5;
1da177e4
LT
3938 error = vfs_rename(old_dir->d_inode, old_dentry,
3939 new_dir->d_inode, new_dentry);
3940exit5:
3941 dput(new_dentry);
3942exit4:
3943 dput(old_dentry);
3944exit3:
3945 unlock_rename(new_dir, old_dir);
c30dabfe 3946 mnt_drop_write(oldnd.path.mnt);
1da177e4 3947exit2:
c6a94284
JL
3948 if (retry_estale(error, lookup_flags))
3949 should_retry = true;
1d957f9b 3950 path_put(&newnd.path);
2ad94ae6 3951 putname(to);
1da177e4 3952exit1:
1d957f9b 3953 path_put(&oldnd.path);
1da177e4 3954 putname(from);
c6a94284
JL
3955 if (should_retry) {
3956 should_retry = false;
3957 lookup_flags |= LOOKUP_REVAL;
3958 goto retry;
3959 }
2ad94ae6 3960exit:
1da177e4
LT
3961 return error;
3962}
3963
a26eab24 3964SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
5590ff0d
UD
3965{
3966 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3967}
3968
1da177e4
LT
3969int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3970{
3971 int len;
3972
3973 len = PTR_ERR(link);
3974 if (IS_ERR(link))
3975 goto out;
3976
3977 len = strlen(link);
3978 if (len > (unsigned) buflen)
3979 len = buflen;
3980 if (copy_to_user(buffer, link, len))
3981 len = -EFAULT;
3982out:
3983 return len;
3984}
3985
3986/*
3987 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3988 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3989 * using) it for any given inode is up to filesystem.
3990 */
3991int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3992{
3993 struct nameidata nd;
cc314eef 3994 void *cookie;
694a1764 3995 int res;
cc314eef 3996
1da177e4 3997 nd.depth = 0;
cc314eef 3998 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
694a1764
MS
3999 if (IS_ERR(cookie))
4000 return PTR_ERR(cookie);
4001
4002 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
4003 if (dentry->d_inode->i_op->put_link)
4004 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4005 return res;
1da177e4
LT
4006}
4007
4008int vfs_follow_link(struct nameidata *nd, const char *link)
4009{
4010 return __vfs_follow_link(nd, link);
4011}
4012
4013/* get the link contents into pagecache */
4014static char *page_getlink(struct dentry * dentry, struct page **ppage)
4015{
ebd09abb
DG
4016 char *kaddr;
4017 struct page *page;
1da177e4 4018 struct address_space *mapping = dentry->d_inode->i_mapping;
090d2b18 4019 page = read_mapping_page(mapping, 0, NULL);
1da177e4 4020 if (IS_ERR(page))
6fe6900e 4021 return (char*)page;
1da177e4 4022 *ppage = page;
ebd09abb
DG
4023 kaddr = kmap(page);
4024 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4025 return kaddr;
1da177e4
LT
4026}
4027
4028int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4029{
4030 struct page *page = NULL;
4031 char *s = page_getlink(dentry, &page);
4032 int res = vfs_readlink(dentry,buffer,buflen,s);
4033 if (page) {
4034 kunmap(page);
4035 page_cache_release(page);
4036 }
4037 return res;
4038}
4039
cc314eef 4040void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
1da177e4 4041{
cc314eef 4042 struct page *page = NULL;
1da177e4 4043 nd_set_link(nd, page_getlink(dentry, &page));
cc314eef 4044 return page;
1da177e4
LT
4045}
4046
cc314eef 4047void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1da177e4 4048{
cc314eef
LT
4049 struct page *page = cookie;
4050
4051 if (page) {
1da177e4
LT
4052 kunmap(page);
4053 page_cache_release(page);
1da177e4
LT
4054 }
4055}
4056
54566b2c
NP
4057/*
4058 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4059 */
4060int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
1da177e4
LT
4061{
4062 struct address_space *mapping = inode->i_mapping;
0adb25d2 4063 struct page *page;
afddba49 4064 void *fsdata;
beb497ab 4065 int err;
1da177e4 4066 char *kaddr;
54566b2c
NP
4067 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4068 if (nofs)
4069 flags |= AOP_FLAG_NOFS;
1da177e4 4070
7e53cac4 4071retry:
afddba49 4072 err = pagecache_write_begin(NULL, mapping, 0, len-1,
54566b2c 4073 flags, &page, &fsdata);
1da177e4 4074 if (err)
afddba49
NP
4075 goto fail;
4076
e8e3c3d6 4077 kaddr = kmap_atomic(page);
1da177e4 4078 memcpy(kaddr, symname, len-1);
e8e3c3d6 4079 kunmap_atomic(kaddr);
afddba49
NP
4080
4081 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4082 page, fsdata);
1da177e4
LT
4083 if (err < 0)
4084 goto fail;
afddba49
NP
4085 if (err < len-1)
4086 goto retry;
4087
1da177e4
LT
4088 mark_inode_dirty(inode);
4089 return 0;
1da177e4
LT
4090fail:
4091 return err;
4092}
4093
0adb25d2
KK
4094int page_symlink(struct inode *inode, const char *symname, int len)
4095{
4096 return __page_symlink(inode, symname, len,
54566b2c 4097 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
0adb25d2
KK
4098}
4099
92e1d5be 4100const struct inode_operations page_symlink_inode_operations = {
1da177e4
LT
4101 .readlink = generic_readlink,
4102 .follow_link = page_follow_link_light,
4103 .put_link = page_put_link,
4104};
4105
2d8f3038 4106EXPORT_SYMBOL(user_path_at);
cc53ce53 4107EXPORT_SYMBOL(follow_down_one);
1da177e4
LT
4108EXPORT_SYMBOL(follow_down);
4109EXPORT_SYMBOL(follow_up);
f6d2ac5c 4110EXPORT_SYMBOL(get_write_access); /* nfsd */
1da177e4 4111EXPORT_SYMBOL(lock_rename);
1da177e4
LT
4112EXPORT_SYMBOL(lookup_one_len);
4113EXPORT_SYMBOL(page_follow_link_light);
4114EXPORT_SYMBOL(page_put_link);
4115EXPORT_SYMBOL(page_readlink);
0adb25d2 4116EXPORT_SYMBOL(__page_symlink);
1da177e4
LT
4117EXPORT_SYMBOL(page_symlink);
4118EXPORT_SYMBOL(page_symlink_inode_operations);
d1811465 4119EXPORT_SYMBOL(kern_path);
16f18200 4120EXPORT_SYMBOL(vfs_path_lookup);
f419a2e3 4121EXPORT_SYMBOL(inode_permission);
1da177e4
LT
4122EXPORT_SYMBOL(unlock_rename);
4123EXPORT_SYMBOL(vfs_create);
4124EXPORT_SYMBOL(vfs_follow_link);
4125EXPORT_SYMBOL(vfs_link);
4126EXPORT_SYMBOL(vfs_mkdir);
4127EXPORT_SYMBOL(vfs_mknod);
4128EXPORT_SYMBOL(generic_permission);
4129EXPORT_SYMBOL(vfs_readlink);
4130EXPORT_SYMBOL(vfs_rename);
4131EXPORT_SYMBOL(vfs_rmdir);
4132EXPORT_SYMBOL(vfs_symlink);
4133EXPORT_SYMBOL(vfs_unlink);
4134EXPORT_SYMBOL(dentry_unhash);
4135EXPORT_SYMBOL(generic_readlink);