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