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