get rid of s_files and files_lock
[deliverable/linux.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
NP
484
485/**
19660af7
AV
486 * unlazy_walk - try to switch to ref-walk mode.
487 * @nd: nameidata pathwalk data
488 * @dentry: child of nd->path.dentry or NULL
39191628 489 * Returns: 0 on success, -ECHILD on failure
31e6b01f 490 *
19660af7
AV
491 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
492 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
493 * @nd or NULL. Must be called from rcu-walk context.
31e6b01f 494 */
19660af7 495static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
31e6b01f
NP
496{
497 struct fs_struct *fs = current->fs;
498 struct dentry *parent = nd->path.dentry;
499
500 BUG_ON(!(nd->flags & LOOKUP_RCU));
e5c832d5
LT
501
502 /*
48a066e7
AV
503 * After legitimizing the bastards, terminate_walk()
504 * will do the right thing for non-RCU mode, and all our
505 * subsequent exit cases should rcu_read_unlock()
506 * before returning. Do vfsmount first; if dentry
507 * can't be legitimized, just set nd->path.dentry to NULL
508 * and rely on dput(NULL) being a no-op.
e5c832d5 509 */
48a066e7 510 if (!legitimize_mnt(nd->path.mnt, nd->m_seq))
e5c832d5 511 return -ECHILD;
e5c832d5 512 nd->flags &= ~LOOKUP_RCU;
15570086 513
48a066e7
AV
514 if (!lockref_get_not_dead(&parent->d_lockref)) {
515 nd->path.dentry = NULL;
8b61e74f 516 rcu_read_unlock();
48a066e7
AV
517 return -ECHILD;
518 }
519
15570086
LT
520 /*
521 * For a negative lookup, the lookup sequence point is the parents
522 * sequence point, and it only needs to revalidate the parent dentry.
523 *
524 * For a positive lookup, we need to move both the parent and the
525 * dentry from the RCU domain to be properly refcounted. And the
526 * sequence number in the dentry validates *both* dentry counters,
527 * since we checked the sequence number of the parent after we got
528 * the child sequence number. So we know the parent must still
529 * be valid if the child sequence number is still valid.
530 */
19660af7 531 if (!dentry) {
e5c832d5
LT
532 if (read_seqcount_retry(&parent->d_seq, nd->seq))
533 goto out;
19660af7
AV
534 BUG_ON(nd->inode != parent->d_inode);
535 } else {
e5c832d5
LT
536 if (!lockref_get_not_dead(&dentry->d_lockref))
537 goto out;
538 if (read_seqcount_retry(&dentry->d_seq, nd->seq))
539 goto drop_dentry;
19660af7 540 }
e5c832d5
LT
541
542 /*
543 * Sequence counts matched. Now make sure that the root is
544 * still valid and get it if required.
545 */
546 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
547 spin_lock(&fs->lock);
548 if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
549 goto unlock_and_drop_dentry;
31e6b01f
NP
550 path_get(&nd->root);
551 spin_unlock(&fs->lock);
552 }
31e6b01f 553
8b61e74f 554 rcu_read_unlock();
31e6b01f 555 return 0;
19660af7 556
e5c832d5
LT
557unlock_and_drop_dentry:
558 spin_unlock(&fs->lock);
559drop_dentry:
8b61e74f 560 rcu_read_unlock();
15570086 561 dput(dentry);
d0d27277 562 goto drop_root_mnt;
e5c832d5 563out:
8b61e74f 564 rcu_read_unlock();
d0d27277
LT
565drop_root_mnt:
566 if (!(nd->flags & LOOKUP_ROOT))
567 nd->root.mnt = NULL;
31e6b01f
NP
568 return -ECHILD;
569}
570
4ce16ef3 571static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
34286d66 572{
4ce16ef3 573 return dentry->d_op->d_revalidate(dentry, flags);
34286d66
NP
574}
575
9f1fafee
AV
576/**
577 * complete_walk - successful completion of path walk
578 * @nd: pointer nameidata
39159de2 579 *
9f1fafee
AV
580 * If we had been in RCU mode, drop out of it and legitimize nd->path.
581 * Revalidate the final result, unless we'd already done that during
582 * the path walk or the filesystem doesn't ask for it. Return 0 on
583 * success, -error on failure. In case of failure caller does not
584 * need to drop nd->path.
39159de2 585 */
9f1fafee 586static int complete_walk(struct nameidata *nd)
39159de2 587{
16c2cd71 588 struct dentry *dentry = nd->path.dentry;
39159de2 589 int status;
39159de2 590
9f1fafee
AV
591 if (nd->flags & LOOKUP_RCU) {
592 nd->flags &= ~LOOKUP_RCU;
593 if (!(nd->flags & LOOKUP_ROOT))
594 nd->root.mnt = NULL;
15570086 595
48a066e7 596 if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) {
8b61e74f 597 rcu_read_unlock();
48a066e7
AV
598 return -ECHILD;
599 }
e5c832d5 600 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
8b61e74f 601 rcu_read_unlock();
48a066e7 602 mntput(nd->path.mnt);
e5c832d5
LT
603 return -ECHILD;
604 }
605 if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
8b61e74f 606 rcu_read_unlock();
e5c832d5 607 dput(dentry);
48a066e7 608 mntput(nd->path.mnt);
9f1fafee
AV
609 return -ECHILD;
610 }
8b61e74f 611 rcu_read_unlock();
9f1fafee
AV
612 }
613
16c2cd71
AV
614 if (likely(!(nd->flags & LOOKUP_JUMPED)))
615 return 0;
616
ecf3d1f1 617 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
39159de2
JL
618 return 0;
619
ecf3d1f1 620 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
39159de2
JL
621 if (status > 0)
622 return 0;
623
16c2cd71 624 if (!status)
39159de2 625 status = -ESTALE;
16c2cd71 626
9f1fafee 627 path_put(&nd->path);
39159de2
JL
628 return status;
629}
630
2a737871
AV
631static __always_inline void set_root(struct nameidata *nd)
632{
f7ad3c6b
MS
633 if (!nd->root.mnt)
634 get_fs_root(current->fs, &nd->root);
2a737871
AV
635}
636
6de88d72
AV
637static int link_path_walk(const char *, struct nameidata *);
638
31e6b01f
NP
639static __always_inline void set_root_rcu(struct nameidata *nd)
640{
641 if (!nd->root.mnt) {
642 struct fs_struct *fs = current->fs;
c28cc364
NP
643 unsigned seq;
644
645 do {
646 seq = read_seqcount_begin(&fs->seq);
647 nd->root = fs->root;
c1530019 648 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
c28cc364 649 } while (read_seqcount_retry(&fs->seq, seq));
31e6b01f
NP
650 }
651}
652
1d957f9b 653static void path_put_conditional(struct path *path, struct nameidata *nd)
051d3812
IK
654{
655 dput(path->dentry);
4ac91378 656 if (path->mnt != nd->path.mnt)
051d3812
IK
657 mntput(path->mnt);
658}
659
7b9337aa
NP
660static inline void path_to_nameidata(const struct path *path,
661 struct nameidata *nd)
051d3812 662{
31e6b01f
NP
663 if (!(nd->flags & LOOKUP_RCU)) {
664 dput(nd->path.dentry);
665 if (nd->path.mnt != path->mnt)
666 mntput(nd->path.mnt);
9a229683 667 }
31e6b01f 668 nd->path.mnt = path->mnt;
4ac91378 669 nd->path.dentry = path->dentry;
051d3812
IK
670}
671
b5fb63c1
CH
672/*
673 * Helper to directly jump to a known parsed path from ->follow_link,
674 * caller must have taken a reference to path beforehand.
675 */
676void nd_jump_link(struct nameidata *nd, struct path *path)
677{
678 path_put(&nd->path);
679
680 nd->path = *path;
681 nd->inode = nd->path.dentry->d_inode;
682 nd->flags |= LOOKUP_JUMPED;
b5fb63c1
CH
683}
684
574197e0
AV
685static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
686{
687 struct inode *inode = link->dentry->d_inode;
6d7b5aae 688 if (inode->i_op->put_link)
574197e0
AV
689 inode->i_op->put_link(link->dentry, nd, cookie);
690 path_put(link);
691}
692
561ec64a
LT
693int sysctl_protected_symlinks __read_mostly = 0;
694int sysctl_protected_hardlinks __read_mostly = 0;
800179c9
KC
695
696/**
697 * may_follow_link - Check symlink following for unsafe situations
698 * @link: The path of the symlink
55852635 699 * @nd: nameidata pathwalk data
800179c9
KC
700 *
701 * In the case of the sysctl_protected_symlinks sysctl being enabled,
702 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
703 * in a sticky world-writable directory. This is to protect privileged
704 * processes from failing races against path names that may change out
705 * from under them by way of other users creating malicious symlinks.
706 * It will permit symlinks to be followed only when outside a sticky
707 * world-writable directory, or when the uid of the symlink and follower
708 * match, or when the directory owner matches the symlink's owner.
709 *
710 * Returns 0 if following the symlink is allowed, -ve on error.
711 */
712static inline int may_follow_link(struct path *link, struct nameidata *nd)
713{
714 const struct inode *inode;
715 const struct inode *parent;
716
717 if (!sysctl_protected_symlinks)
718 return 0;
719
720 /* Allowed if owner and follower match. */
721 inode = link->dentry->d_inode;
81abe27b 722 if (uid_eq(current_cred()->fsuid, inode->i_uid))
800179c9
KC
723 return 0;
724
725 /* Allowed if parent directory not sticky and world-writable. */
726 parent = nd->path.dentry->d_inode;
727 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
728 return 0;
729
730 /* Allowed if parent directory and link owner match. */
81abe27b 731 if (uid_eq(parent->i_uid, inode->i_uid))
800179c9
KC
732 return 0;
733
ffd8d101 734 audit_log_link_denied("follow_link", link);
800179c9
KC
735 path_put_conditional(link, nd);
736 path_put(&nd->path);
737 return -EACCES;
738}
739
740/**
741 * safe_hardlink_source - Check for safe hardlink conditions
742 * @inode: the source inode to hardlink from
743 *
744 * Return false if at least one of the following conditions:
745 * - inode is not a regular file
746 * - inode is setuid
747 * - inode is setgid and group-exec
748 * - access failure for read and write
749 *
750 * Otherwise returns true.
751 */
752static bool safe_hardlink_source(struct inode *inode)
753{
754 umode_t mode = inode->i_mode;
755
756 /* Special files should not get pinned to the filesystem. */
757 if (!S_ISREG(mode))
758 return false;
759
760 /* Setuid files should not get pinned to the filesystem. */
761 if (mode & S_ISUID)
762 return false;
763
764 /* Executable setgid files should not get pinned to the filesystem. */
765 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
766 return false;
767
768 /* Hardlinking to unreadable or unwritable sources is dangerous. */
769 if (inode_permission(inode, MAY_READ | MAY_WRITE))
770 return false;
771
772 return true;
773}
774
775/**
776 * may_linkat - Check permissions for creating a hardlink
777 * @link: the source to hardlink from
778 *
779 * Block hardlink when all of:
780 * - sysctl_protected_hardlinks enabled
781 * - fsuid does not match inode
782 * - hardlink source is unsafe (see safe_hardlink_source() above)
783 * - not CAP_FOWNER
784 *
785 * Returns 0 if successful, -ve on error.
786 */
787static int may_linkat(struct path *link)
788{
789 const struct cred *cred;
790 struct inode *inode;
791
792 if (!sysctl_protected_hardlinks)
793 return 0;
794
795 cred = current_cred();
796 inode = link->dentry->d_inode;
797
798 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
799 * otherwise, it must be a safe source.
800 */
81abe27b 801 if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
800179c9
KC
802 capable(CAP_FOWNER))
803 return 0;
804
a51d9eaa 805 audit_log_link_denied("linkat", link);
800179c9
KC
806 return -EPERM;
807}
808
def4af30 809static __always_inline int
574197e0 810follow_link(struct path *link, struct nameidata *nd, void **p)
1da177e4 811{
7b9337aa 812 struct dentry *dentry = link->dentry;
6d7b5aae
AV
813 int error;
814 char *s;
1da177e4 815
844a3917
AV
816 BUG_ON(nd->flags & LOOKUP_RCU);
817
0e794589
AV
818 if (link->mnt == nd->path.mnt)
819 mntget(link->mnt);
820
6d7b5aae
AV
821 error = -ELOOP;
822 if (unlikely(current->total_link_count >= 40))
823 goto out_put_nd_path;
824
574197e0
AV
825 cond_resched();
826 current->total_link_count++;
827
68ac1234 828 touch_atime(link);
1da177e4 829 nd_set_link(nd, NULL);
cd4e91d3 830
36f3b4f6 831 error = security_inode_follow_link(link->dentry, nd);
6d7b5aae
AV
832 if (error)
833 goto out_put_nd_path;
36f3b4f6 834
86acdca1 835 nd->last_type = LAST_BIND;
def4af30
AV
836 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
837 error = PTR_ERR(*p);
6d7b5aae 838 if (IS_ERR(*p))
408ef013 839 goto out_put_nd_path;
6d7b5aae
AV
840
841 error = 0;
842 s = nd_get_link(nd);
843 if (s) {
443ed254
AV
844 if (unlikely(IS_ERR(s))) {
845 path_put(&nd->path);
846 put_link(nd, link, *p);
847 return PTR_ERR(s);
848 }
849 if (*s == '/') {
850 set_root(nd);
851 path_put(&nd->path);
852 nd->path = nd->root;
853 path_get(&nd->root);
854 nd->flags |= LOOKUP_JUMPED;
855 }
856 nd->inode = nd->path.dentry->d_inode;
857 error = link_path_walk(s, nd);
b5fb63c1
CH
858 if (unlikely(error))
859 put_link(nd, link, *p);
1da177e4 860 }
6d7b5aae
AV
861
862 return error;
863
864out_put_nd_path:
98f6ef64 865 *p = NULL;
6d7b5aae 866 path_put(&nd->path);
6d7b5aae 867 path_put(link);
1da177e4
LT
868 return error;
869}
870
31e6b01f
NP
871static int follow_up_rcu(struct path *path)
872{
0714a533
AV
873 struct mount *mnt = real_mount(path->mnt);
874 struct mount *parent;
31e6b01f
NP
875 struct dentry *mountpoint;
876
0714a533
AV
877 parent = mnt->mnt_parent;
878 if (&parent->mnt == path->mnt)
31e6b01f 879 return 0;
a73324da 880 mountpoint = mnt->mnt_mountpoint;
31e6b01f 881 path->dentry = mountpoint;
0714a533 882 path->mnt = &parent->mnt;
31e6b01f
NP
883 return 1;
884}
885
f015f126
DH
886/*
887 * follow_up - Find the mountpoint of path's vfsmount
888 *
889 * Given a path, find the mountpoint of its source file system.
890 * Replace @path with the path of the mountpoint in the parent mount.
891 * Up is towards /.
892 *
893 * Return 1 if we went up a level and 0 if we were already at the
894 * root.
895 */
bab77ebf 896int follow_up(struct path *path)
1da177e4 897{
0714a533
AV
898 struct mount *mnt = real_mount(path->mnt);
899 struct mount *parent;
1da177e4 900 struct dentry *mountpoint;
99b7db7b 901
48a066e7 902 read_seqlock_excl(&mount_lock);
0714a533 903 parent = mnt->mnt_parent;
3c0a6163 904 if (parent == mnt) {
48a066e7 905 read_sequnlock_excl(&mount_lock);
1da177e4
LT
906 return 0;
907 }
0714a533 908 mntget(&parent->mnt);
a73324da 909 mountpoint = dget(mnt->mnt_mountpoint);
48a066e7 910 read_sequnlock_excl(&mount_lock);
bab77ebf
AV
911 dput(path->dentry);
912 path->dentry = mountpoint;
913 mntput(path->mnt);
0714a533 914 path->mnt = &parent->mnt;
1da177e4
LT
915 return 1;
916}
917
b5c84bf6 918/*
9875cf80
DH
919 * Perform an automount
920 * - return -EISDIR to tell follow_managed() to stop and return the path we
921 * were called with.
1da177e4 922 */
9875cf80
DH
923static int follow_automount(struct path *path, unsigned flags,
924 bool *need_mntput)
31e6b01f 925{
9875cf80 926 struct vfsmount *mnt;
ea5b778a 927 int err;
9875cf80
DH
928
929 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
930 return -EREMOTE;
931
0ec26fd0
MS
932 /* We don't want to mount if someone's just doing a stat -
933 * unless they're stat'ing a directory and appended a '/' to
934 * the name.
935 *
936 * We do, however, want to mount if someone wants to open or
937 * create a file of any type under the mountpoint, wants to
938 * traverse through the mountpoint or wants to open the
939 * mounted directory. Also, autofs may mark negative dentries
940 * as being automount points. These will need the attentions
941 * of the daemon to instantiate them before they can be used.
9875cf80 942 */
0ec26fd0 943 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
d94c177b 944 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
0ec26fd0
MS
945 path->dentry->d_inode)
946 return -EISDIR;
947
9875cf80
DH
948 current->total_link_count++;
949 if (current->total_link_count >= 40)
950 return -ELOOP;
951
952 mnt = path->dentry->d_op->d_automount(path);
953 if (IS_ERR(mnt)) {
954 /*
955 * The filesystem is allowed to return -EISDIR here to indicate
956 * it doesn't want to automount. For instance, autofs would do
957 * this so that its userspace daemon can mount on this dentry.
958 *
959 * However, we can only permit this if it's a terminal point in
960 * the path being looked up; if it wasn't then the remainder of
961 * the path is inaccessible and we should say so.
962 */
49084c3b 963 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
9875cf80
DH
964 return -EREMOTE;
965 return PTR_ERR(mnt);
31e6b01f 966 }
ea5b778a 967
9875cf80
DH
968 if (!mnt) /* mount collision */
969 return 0;
31e6b01f 970
8aef1884
AV
971 if (!*need_mntput) {
972 /* lock_mount() may release path->mnt on error */
973 mntget(path->mnt);
974 *need_mntput = true;
975 }
19a167af 976 err = finish_automount(mnt, path);
9875cf80 977
ea5b778a
DH
978 switch (err) {
979 case -EBUSY:
980 /* Someone else made a mount here whilst we were busy */
19a167af 981 return 0;
ea5b778a 982 case 0:
8aef1884 983 path_put(path);
ea5b778a
DH
984 path->mnt = mnt;
985 path->dentry = dget(mnt->mnt_root);
ea5b778a 986 return 0;
19a167af
AV
987 default:
988 return err;
ea5b778a 989 }
19a167af 990
463ffb2e
AV
991}
992
9875cf80
DH
993/*
994 * Handle a dentry that is managed in some way.
cc53ce53 995 * - Flagged for transit management (autofs)
9875cf80
DH
996 * - Flagged as mountpoint
997 * - Flagged as automount point
998 *
999 * This may only be called in refwalk mode.
1000 *
1001 * Serialization is taken care of in namespace.c
1002 */
1003static int follow_managed(struct path *path, unsigned flags)
1da177e4 1004{
8aef1884 1005 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
9875cf80
DH
1006 unsigned managed;
1007 bool need_mntput = false;
8aef1884 1008 int ret = 0;
9875cf80
DH
1009
1010 /* Given that we're not holding a lock here, we retain the value in a
1011 * local variable for each dentry as we look at it so that we don't see
1012 * the components of that value change under us */
1013 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1014 managed &= DCACHE_MANAGED_DENTRY,
1015 unlikely(managed != 0)) {
cc53ce53
DH
1016 /* Allow the filesystem to manage the transit without i_mutex
1017 * being held. */
1018 if (managed & DCACHE_MANAGE_TRANSIT) {
1019 BUG_ON(!path->dentry->d_op);
1020 BUG_ON(!path->dentry->d_op->d_manage);
1aed3e42 1021 ret = path->dentry->d_op->d_manage(path->dentry, false);
cc53ce53 1022 if (ret < 0)
8aef1884 1023 break;
cc53ce53
DH
1024 }
1025
9875cf80
DH
1026 /* Transit to a mounted filesystem. */
1027 if (managed & DCACHE_MOUNTED) {
1028 struct vfsmount *mounted = lookup_mnt(path);
1029 if (mounted) {
1030 dput(path->dentry);
1031 if (need_mntput)
1032 mntput(path->mnt);
1033 path->mnt = mounted;
1034 path->dentry = dget(mounted->mnt_root);
1035 need_mntput = true;
1036 continue;
1037 }
1038
1039 /* Something is mounted on this dentry in another
1040 * namespace and/or whatever was mounted there in this
48a066e7
AV
1041 * namespace got unmounted before lookup_mnt() could
1042 * get it */
9875cf80
DH
1043 }
1044
1045 /* Handle an automount point */
1046 if (managed & DCACHE_NEED_AUTOMOUNT) {
1047 ret = follow_automount(path, flags, &need_mntput);
1048 if (ret < 0)
8aef1884 1049 break;
9875cf80
DH
1050 continue;
1051 }
1052
1053 /* We didn't change the current path point */
1054 break;
1da177e4 1055 }
8aef1884
AV
1056
1057 if (need_mntput && path->mnt == mnt)
1058 mntput(path->mnt);
1059 if (ret == -EISDIR)
1060 ret = 0;
a3fbbde7 1061 return ret < 0 ? ret : need_mntput;
1da177e4
LT
1062}
1063
cc53ce53 1064int follow_down_one(struct path *path)
1da177e4
LT
1065{
1066 struct vfsmount *mounted;
1067
1c755af4 1068 mounted = lookup_mnt(path);
1da177e4 1069 if (mounted) {
9393bd07
AV
1070 dput(path->dentry);
1071 mntput(path->mnt);
1072 path->mnt = mounted;
1073 path->dentry = dget(mounted->mnt_root);
1da177e4
LT
1074 return 1;
1075 }
1076 return 0;
1077}
1078
62a7375e
IK
1079static inline bool managed_dentry_might_block(struct dentry *dentry)
1080{
1081 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1082 dentry->d_op->d_manage(dentry, true) < 0);
1083}
1084
9875cf80 1085/*
287548e4
AV
1086 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1087 * we meet a managed dentry that would need blocking.
9875cf80
DH
1088 */
1089static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
287548e4 1090 struct inode **inode)
9875cf80 1091{
62a7375e 1092 for (;;) {
c7105365 1093 struct mount *mounted;
62a7375e
IK
1094 /*
1095 * Don't forget we might have a non-mountpoint managed dentry
1096 * that wants to block transit.
1097 */
287548e4 1098 if (unlikely(managed_dentry_might_block(path->dentry)))
ab90911f 1099 return false;
62a7375e
IK
1100
1101 if (!d_mountpoint(path->dentry))
1102 break;
1103
474279dc 1104 mounted = __lookup_mnt(path->mnt, path->dentry);
9875cf80
DH
1105 if (!mounted)
1106 break;
c7105365
AV
1107 path->mnt = &mounted->mnt;
1108 path->dentry = mounted->mnt.mnt_root;
a3fbbde7 1109 nd->flags |= LOOKUP_JUMPED;
9875cf80 1110 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
59430262
LT
1111 /*
1112 * Update the inode too. We don't need to re-check the
1113 * dentry sequence number here after this d_inode read,
1114 * because a mount-point is always pinned.
1115 */
1116 *inode = path->dentry->d_inode;
9875cf80 1117 }
9875cf80
DH
1118 return true;
1119}
1120
dea39376 1121static void follow_mount_rcu(struct nameidata *nd)
287548e4 1122{
dea39376 1123 while (d_mountpoint(nd->path.dentry)) {
c7105365 1124 struct mount *mounted;
474279dc 1125 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
287548e4
AV
1126 if (!mounted)
1127 break;
c7105365
AV
1128 nd->path.mnt = &mounted->mnt;
1129 nd->path.dentry = mounted->mnt.mnt_root;
dea39376 1130 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
287548e4
AV
1131 }
1132}
1133
31e6b01f
NP
1134static int follow_dotdot_rcu(struct nameidata *nd)
1135{
31e6b01f
NP
1136 set_root_rcu(nd);
1137
9875cf80 1138 while (1) {
31e6b01f
NP
1139 if (nd->path.dentry == nd->root.dentry &&
1140 nd->path.mnt == nd->root.mnt) {
1141 break;
1142 }
1143 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1144 struct dentry *old = nd->path.dentry;
1145 struct dentry *parent = old->d_parent;
1146 unsigned seq;
1147
1148 seq = read_seqcount_begin(&parent->d_seq);
1149 if (read_seqcount_retry(&old->d_seq, nd->seq))
ef7562d5 1150 goto failed;
31e6b01f
NP
1151 nd->path.dentry = parent;
1152 nd->seq = seq;
1153 break;
1154 }
1155 if (!follow_up_rcu(&nd->path))
1156 break;
1157 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
31e6b01f 1158 }
dea39376
AV
1159 follow_mount_rcu(nd);
1160 nd->inode = nd->path.dentry->d_inode;
31e6b01f 1161 return 0;
ef7562d5
AV
1162
1163failed:
1164 nd->flags &= ~LOOKUP_RCU;
5b6ca027
AV
1165 if (!(nd->flags & LOOKUP_ROOT))
1166 nd->root.mnt = NULL;
8b61e74f 1167 rcu_read_unlock();
ef7562d5 1168 return -ECHILD;
31e6b01f
NP
1169}
1170
cc53ce53
DH
1171/*
1172 * Follow down to the covering mount currently visible to userspace. At each
1173 * point, the filesystem owning that dentry may be queried as to whether the
1174 * caller is permitted to proceed or not.
cc53ce53 1175 */
7cc90cc3 1176int follow_down(struct path *path)
cc53ce53
DH
1177{
1178 unsigned managed;
1179 int ret;
1180
1181 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1182 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1183 /* Allow the filesystem to manage the transit without i_mutex
1184 * being held.
1185 *
1186 * We indicate to the filesystem if someone is trying to mount
1187 * something here. This gives autofs the chance to deny anyone
1188 * other than its daemon the right to mount on its
1189 * superstructure.
1190 *
1191 * The filesystem may sleep at this point.
1192 */
1193 if (managed & DCACHE_MANAGE_TRANSIT) {
1194 BUG_ON(!path->dentry->d_op);
1195 BUG_ON(!path->dentry->d_op->d_manage);
ab90911f 1196 ret = path->dentry->d_op->d_manage(
1aed3e42 1197 path->dentry, false);
cc53ce53
DH
1198 if (ret < 0)
1199 return ret == -EISDIR ? 0 : ret;
1200 }
1201
1202 /* Transit to a mounted filesystem. */
1203 if (managed & DCACHE_MOUNTED) {
1204 struct vfsmount *mounted = lookup_mnt(path);
1205 if (!mounted)
1206 break;
1207 dput(path->dentry);
1208 mntput(path->mnt);
1209 path->mnt = mounted;
1210 path->dentry = dget(mounted->mnt_root);
1211 continue;
1212 }
1213
1214 /* Don't handle automount points here */
1215 break;
1216 }
1217 return 0;
1218}
1219
9875cf80
DH
1220/*
1221 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1222 */
1223static void follow_mount(struct path *path)
1224{
1225 while (d_mountpoint(path->dentry)) {
1226 struct vfsmount *mounted = lookup_mnt(path);
1227 if (!mounted)
1228 break;
1229 dput(path->dentry);
1230 mntput(path->mnt);
1231 path->mnt = mounted;
1232 path->dentry = dget(mounted->mnt_root);
1233 }
1234}
1235
31e6b01f 1236static void follow_dotdot(struct nameidata *nd)
1da177e4 1237{
2a737871 1238 set_root(nd);
e518ddb7 1239
1da177e4 1240 while(1) {
4ac91378 1241 struct dentry *old = nd->path.dentry;
1da177e4 1242
2a737871
AV
1243 if (nd->path.dentry == nd->root.dentry &&
1244 nd->path.mnt == nd->root.mnt) {
1da177e4
LT
1245 break;
1246 }
4ac91378 1247 if (nd->path.dentry != nd->path.mnt->mnt_root) {
3088dd70
AV
1248 /* rare case of legitimate dget_parent()... */
1249 nd->path.dentry = dget_parent(nd->path.dentry);
1da177e4
LT
1250 dput(old);
1251 break;
1252 }
3088dd70 1253 if (!follow_up(&nd->path))
1da177e4 1254 break;
1da177e4 1255 }
79ed0226 1256 follow_mount(&nd->path);
31e6b01f 1257 nd->inode = nd->path.dentry->d_inode;
1da177e4
LT
1258}
1259
baa03890 1260/*
bad61189
MS
1261 * This looks up the name in dcache, possibly revalidates the old dentry and
1262 * allocates a new one if not found or not valid. In the need_lookup argument
1263 * returns whether i_op->lookup is necessary.
1264 *
1265 * dir->d_inode->i_mutex must be held
baa03890 1266 */
bad61189 1267static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
201f956e 1268 unsigned int flags, bool *need_lookup)
baa03890 1269{
baa03890 1270 struct dentry *dentry;
bad61189 1271 int error;
baa03890 1272
bad61189
MS
1273 *need_lookup = false;
1274 dentry = d_lookup(dir, name);
1275 if (dentry) {
39e3c955 1276 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
201f956e 1277 error = d_revalidate(dentry, flags);
bad61189
MS
1278 if (unlikely(error <= 0)) {
1279 if (error < 0) {
1280 dput(dentry);
1281 return ERR_PTR(error);
1282 } else if (!d_invalidate(dentry)) {
1283 dput(dentry);
1284 dentry = NULL;
1285 }
1286 }
1287 }
1288 }
baa03890 1289
bad61189
MS
1290 if (!dentry) {
1291 dentry = d_alloc(dir, name);
1292 if (unlikely(!dentry))
1293 return ERR_PTR(-ENOMEM);
baa03890 1294
bad61189 1295 *need_lookup = true;
baa03890
NP
1296 }
1297 return dentry;
1298}
1299
44396f4b 1300/*
bad61189
MS
1301 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1302 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1303 *
1304 * dir->d_inode->i_mutex must be held
44396f4b 1305 */
bad61189 1306static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
72bd866a 1307 unsigned int flags)
44396f4b 1308{
44396f4b
JB
1309 struct dentry *old;
1310
1311 /* Don't create child dentry for a dead directory. */
bad61189 1312 if (unlikely(IS_DEADDIR(dir))) {
e188dc02 1313 dput(dentry);
44396f4b 1314 return ERR_PTR(-ENOENT);
e188dc02 1315 }
44396f4b 1316
72bd866a 1317 old = dir->i_op->lookup(dir, dentry, flags);
44396f4b
JB
1318 if (unlikely(old)) {
1319 dput(dentry);
1320 dentry = old;
1321 }
1322 return dentry;
1323}
1324
a3255546 1325static struct dentry *__lookup_hash(struct qstr *name,
72bd866a 1326 struct dentry *base, unsigned int flags)
a3255546 1327{
bad61189 1328 bool need_lookup;
a3255546
AV
1329 struct dentry *dentry;
1330
72bd866a 1331 dentry = lookup_dcache(name, base, flags, &need_lookup);
bad61189
MS
1332 if (!need_lookup)
1333 return dentry;
a3255546 1334
72bd866a 1335 return lookup_real(base->d_inode, dentry, flags);
a3255546
AV
1336}
1337
1da177e4
LT
1338/*
1339 * It's more convoluted than I'd like it to be, but... it's still fairly
1340 * small and for now I'd prefer to have fast path as straight as possible.
1341 * It _is_ time-critical.
1342 */
e97cdc87 1343static int lookup_fast(struct nameidata *nd,
697f514d 1344 struct path *path, struct inode **inode)
1da177e4 1345{
4ac91378 1346 struct vfsmount *mnt = nd->path.mnt;
31e6b01f 1347 struct dentry *dentry, *parent = nd->path.dentry;
5a18fff2
AV
1348 int need_reval = 1;
1349 int status = 1;
9875cf80
DH
1350 int err;
1351
b04f784e
NP
1352 /*
1353 * Rename seqlock is not required here because in the off chance
1354 * of a false negative due to a concurrent rename, we're going to
1355 * do the non-racy lookup, below.
1356 */
31e6b01f
NP
1357 if (nd->flags & LOOKUP_RCU) {
1358 unsigned seq;
da53be12 1359 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
5a18fff2
AV
1360 if (!dentry)
1361 goto unlazy;
1362
12f8ad4b
LT
1363 /*
1364 * This sequence count validates that the inode matches
1365 * the dentry name information from lookup.
1366 */
1367 *inode = dentry->d_inode;
1368 if (read_seqcount_retry(&dentry->d_seq, seq))
1369 return -ECHILD;
1370
1371 /*
1372 * This sequence count validates that the parent had no
1373 * changes while we did the lookup of the dentry above.
1374 *
1375 * The memory barrier in read_seqcount_begin of child is
1376 * enough, we can use __read_seqcount_retry here.
1377 */
31e6b01f
NP
1378 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1379 return -ECHILD;
31e6b01f 1380 nd->seq = seq;
5a18fff2 1381
24643087 1382 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
4ce16ef3 1383 status = d_revalidate(dentry, nd->flags);
5a18fff2
AV
1384 if (unlikely(status <= 0)) {
1385 if (status != -ECHILD)
1386 need_reval = 0;
1387 goto unlazy;
1388 }
24643087 1389 }
31e6b01f
NP
1390 path->mnt = mnt;
1391 path->dentry = dentry;
d6e9bd25
AV
1392 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1393 goto unlazy;
1394 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1395 goto unlazy;
1396 return 0;
5a18fff2 1397unlazy:
19660af7
AV
1398 if (unlazy_walk(nd, dentry))
1399 return -ECHILD;
5a18fff2 1400 } else {
e97cdc87 1401 dentry = __d_lookup(parent, &nd->last);
9875cf80 1402 }
5a18fff2 1403
81e6f520
AV
1404 if (unlikely(!dentry))
1405 goto need_lookup;
1406
5a18fff2 1407 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
4ce16ef3 1408 status = d_revalidate(dentry, nd->flags);
5a18fff2
AV
1409 if (unlikely(status <= 0)) {
1410 if (status < 0) {
1411 dput(dentry);
1412 return status;
1413 }
1414 if (!d_invalidate(dentry)) {
1415 dput(dentry);
81e6f520 1416 goto need_lookup;
5a18fff2 1417 }
24643087 1418 }
697f514d 1419
9875cf80
DH
1420 path->mnt = mnt;
1421 path->dentry = dentry;
1422 err = follow_managed(path, nd->flags);
89312214
IK
1423 if (unlikely(err < 0)) {
1424 path_put_conditional(path, nd);
9875cf80 1425 return err;
89312214 1426 }
a3fbbde7
AV
1427 if (err)
1428 nd->flags |= LOOKUP_JUMPED;
9875cf80 1429 *inode = path->dentry->d_inode;
1da177e4 1430 return 0;
81e6f520
AV
1431
1432need_lookup:
697f514d
MS
1433 return 1;
1434}
1435
1436/* Fast lookup failed, do it the slow way */
cc2a5271 1437static int lookup_slow(struct nameidata *nd, struct path *path)
697f514d
MS
1438{
1439 struct dentry *dentry, *parent;
1440 int err;
1441
1442 parent = nd->path.dentry;
81e6f520
AV
1443 BUG_ON(nd->inode != parent->d_inode);
1444
1445 mutex_lock(&parent->d_inode->i_mutex);
cc2a5271 1446 dentry = __lookup_hash(&nd->last, parent, nd->flags);
81e6f520
AV
1447 mutex_unlock(&parent->d_inode->i_mutex);
1448 if (IS_ERR(dentry))
1449 return PTR_ERR(dentry);
697f514d
MS
1450 path->mnt = nd->path.mnt;
1451 path->dentry = dentry;
1452 err = follow_managed(path, nd->flags);
1453 if (unlikely(err < 0)) {
1454 path_put_conditional(path, nd);
1455 return err;
1456 }
1457 if (err)
1458 nd->flags |= LOOKUP_JUMPED;
1459 return 0;
1da177e4
LT
1460}
1461
52094c8a
AV
1462static inline int may_lookup(struct nameidata *nd)
1463{
1464 if (nd->flags & LOOKUP_RCU) {
4ad5abb3 1465 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
52094c8a
AV
1466 if (err != -ECHILD)
1467 return err;
19660af7 1468 if (unlazy_walk(nd, NULL))
52094c8a
AV
1469 return -ECHILD;
1470 }
4ad5abb3 1471 return inode_permission(nd->inode, MAY_EXEC);
52094c8a
AV
1472}
1473
9856fa1b
AV
1474static inline int handle_dots(struct nameidata *nd, int type)
1475{
1476 if (type == LAST_DOTDOT) {
1477 if (nd->flags & LOOKUP_RCU) {
1478 if (follow_dotdot_rcu(nd))
1479 return -ECHILD;
1480 } else
1481 follow_dotdot(nd);
1482 }
1483 return 0;
1484}
1485
951361f9
AV
1486static void terminate_walk(struct nameidata *nd)
1487{
1488 if (!(nd->flags & LOOKUP_RCU)) {
1489 path_put(&nd->path);
1490 } else {
1491 nd->flags &= ~LOOKUP_RCU;
5b6ca027
AV
1492 if (!(nd->flags & LOOKUP_ROOT))
1493 nd->root.mnt = NULL;
8b61e74f 1494 rcu_read_unlock();
951361f9
AV
1495 }
1496}
1497
3ddcd056
LT
1498/*
1499 * Do we need to follow links? We _really_ want to be able
1500 * to do this check without having to look at inode->i_op,
1501 * so we keep a cache of "no, this doesn't need follow_link"
1502 * for the common case.
1503 */
7813b94a 1504static inline int should_follow_link(struct inode *inode, int follow)
3ddcd056
LT
1505{
1506 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1507 if (likely(inode->i_op->follow_link))
1508 return follow;
1509
1510 /* This gets set once for the inode lifetime */
1511 spin_lock(&inode->i_lock);
1512 inode->i_opflags |= IOP_NOFOLLOW;
1513 spin_unlock(&inode->i_lock);
1514 }
1515 return 0;
1516}
1517
ce57dfc1 1518static inline int walk_component(struct nameidata *nd, struct path *path,
21b9b073 1519 int follow)
ce57dfc1
AV
1520{
1521 struct inode *inode;
1522 int err;
1523 /*
1524 * "." and ".." are special - ".." especially so because it has
1525 * to be able to know about the current root directory and
1526 * parent relationships.
1527 */
21b9b073
AV
1528 if (unlikely(nd->last_type != LAST_NORM))
1529 return handle_dots(nd, nd->last_type);
e97cdc87 1530 err = lookup_fast(nd, path, &inode);
ce57dfc1 1531 if (unlikely(err)) {
697f514d
MS
1532 if (err < 0)
1533 goto out_err;
1534
cc2a5271 1535 err = lookup_slow(nd, path);
697f514d
MS
1536 if (err < 0)
1537 goto out_err;
1538
1539 inode = path->dentry->d_inode;
ce57dfc1 1540 }
697f514d
MS
1541 err = -ENOENT;
1542 if (!inode)
1543 goto out_path_put;
1544
7813b94a 1545 if (should_follow_link(inode, follow)) {
19660af7
AV
1546 if (nd->flags & LOOKUP_RCU) {
1547 if (unlikely(unlazy_walk(nd, path->dentry))) {
697f514d
MS
1548 err = -ECHILD;
1549 goto out_err;
19660af7
AV
1550 }
1551 }
ce57dfc1
AV
1552 BUG_ON(inode != path->dentry->d_inode);
1553 return 1;
1554 }
1555 path_to_nameidata(path, nd);
1556 nd->inode = inode;
1557 return 0;
697f514d
MS
1558
1559out_path_put:
1560 path_to_nameidata(path, nd);
1561out_err:
1562 terminate_walk(nd);
1563 return err;
ce57dfc1
AV
1564}
1565
b356379a
AV
1566/*
1567 * This limits recursive symlink follows to 8, while
1568 * limiting consecutive symlinks to 40.
1569 *
1570 * Without that kind of total limit, nasty chains of consecutive
1571 * symlinks can cause almost arbitrarily long lookups.
1572 */
1573static inline int nested_symlink(struct path *path, struct nameidata *nd)
1574{
1575 int res;
1576
b356379a
AV
1577 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1578 path_put_conditional(path, nd);
1579 path_put(&nd->path);
1580 return -ELOOP;
1581 }
1a4022f8 1582 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
b356379a
AV
1583
1584 nd->depth++;
1585 current->link_count++;
1586
1587 do {
1588 struct path link = *path;
1589 void *cookie;
574197e0
AV
1590
1591 res = follow_link(&link, nd, &cookie);
6d7b5aae
AV
1592 if (res)
1593 break;
21b9b073 1594 res = walk_component(nd, path, LOOKUP_FOLLOW);
574197e0 1595 put_link(nd, &link, cookie);
b356379a
AV
1596 } while (res > 0);
1597
1598 current->link_count--;
1599 nd->depth--;
1600 return res;
1601}
1602
3ddcd056
LT
1603/*
1604 * We really don't want to look at inode->i_op->lookup
1605 * when we don't have to. So we keep a cache bit in
1606 * the inode ->i_opflags field that says "yes, we can
1607 * do lookup on this inode".
1608 */
1609static inline int can_lookup(struct inode *inode)
1610{
1611 if (likely(inode->i_opflags & IOP_LOOKUP))
1612 return 1;
1613 if (likely(!inode->i_op->lookup))
1614 return 0;
1615
1616 /* We do this once for the lifetime of the inode */
1617 spin_lock(&inode->i_lock);
1618 inode->i_opflags |= IOP_LOOKUP;
1619 spin_unlock(&inode->i_lock);
1620 return 1;
1621}
1622
bfcfaa77
LT
1623/*
1624 * We can do the critical dentry name comparison and hashing
1625 * operations one word at a time, but we are limited to:
1626 *
1627 * - Architectures with fast unaligned word accesses. We could
1628 * do a "get_unaligned()" if this helps and is sufficiently
1629 * fast.
1630 *
1631 * - Little-endian machines (so that we can generate the mask
1632 * of low bytes efficiently). Again, we *could* do a byte
1633 * swapping load on big-endian architectures if that is not
1634 * expensive enough to make the optimization worthless.
1635 *
1636 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1637 * do not trap on the (extremely unlikely) case of a page
1638 * crossing operation.
1639 *
1640 * - Furthermore, we need an efficient 64-bit compile for the
1641 * 64-bit case in order to generate the "number of bytes in
1642 * the final mask". Again, that could be replaced with a
1643 * efficient population count instruction or similar.
1644 */
1645#ifdef CONFIG_DCACHE_WORD_ACCESS
1646
f68e556e 1647#include <asm/word-at-a-time.h>
bfcfaa77 1648
f68e556e 1649#ifdef CONFIG_64BIT
bfcfaa77
LT
1650
1651static inline unsigned int fold_hash(unsigned long hash)
1652{
1653 hash += hash >> (8*sizeof(int));
1654 return hash;
1655}
1656
1657#else /* 32-bit case */
1658
bfcfaa77
LT
1659#define fold_hash(x) (x)
1660
1661#endif
1662
1663unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1664{
1665 unsigned long a, mask;
1666 unsigned long hash = 0;
1667
1668 for (;;) {
e419b4cc 1669 a = load_unaligned_zeropad(name);
bfcfaa77
LT
1670 if (len < sizeof(unsigned long))
1671 break;
1672 hash += a;
f132c5be 1673 hash *= 9;
bfcfaa77
LT
1674 name += sizeof(unsigned long);
1675 len -= sizeof(unsigned long);
1676 if (!len)
1677 goto done;
1678 }
1679 mask = ~(~0ul << len*8);
1680 hash += mask & a;
1681done:
1682 return fold_hash(hash);
1683}
1684EXPORT_SYMBOL(full_name_hash);
1685
bfcfaa77
LT
1686/*
1687 * Calculate the length and hash of the path component, and
1688 * return the length of the component;
1689 */
1690static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1691{
36126f8f
LT
1692 unsigned long a, b, adata, bdata, mask, hash, len;
1693 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
bfcfaa77
LT
1694
1695 hash = a = 0;
1696 len = -sizeof(unsigned long);
1697 do {
1698 hash = (hash + a) * 9;
1699 len += sizeof(unsigned long);
e419b4cc 1700 a = load_unaligned_zeropad(name+len);
36126f8f
LT
1701 b = a ^ REPEAT_BYTE('/');
1702 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1703
1704 adata = prep_zero_mask(a, adata, &constants);
1705 bdata = prep_zero_mask(b, bdata, &constants);
1706
1707 mask = create_zero_mask(adata | bdata);
1708
1709 hash += a & zero_bytemask(mask);
bfcfaa77
LT
1710 *hashp = fold_hash(hash);
1711
36126f8f 1712 return len + find_zero(mask);
bfcfaa77
LT
1713}
1714
1715#else
1716
0145acc2
LT
1717unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1718{
1719 unsigned long hash = init_name_hash();
1720 while (len--)
1721 hash = partial_name_hash(*name++, hash);
1722 return end_name_hash(hash);
1723}
ae942ae7 1724EXPORT_SYMBOL(full_name_hash);
0145acc2 1725
200e9ef7
LT
1726/*
1727 * We know there's a real path component here of at least
1728 * one character.
1729 */
1730static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1731{
1732 unsigned long hash = init_name_hash();
1733 unsigned long len = 0, c;
1734
1735 c = (unsigned char)*name;
1736 do {
1737 len++;
1738 hash = partial_name_hash(c, hash);
1739 c = (unsigned char)name[len];
1740 } while (c && c != '/');
1741 *hashp = end_name_hash(hash);
1742 return len;
1743}
1744
bfcfaa77
LT
1745#endif
1746
1da177e4
LT
1747/*
1748 * Name resolution.
ea3834d9
PM
1749 * This is the basic name resolution function, turning a pathname into
1750 * the final dentry. We expect 'base' to be positive and a directory.
1da177e4 1751 *
ea3834d9
PM
1752 * Returns 0 and nd will have valid dentry and mnt on success.
1753 * Returns error and drops reference to input namei data on failure.
1da177e4 1754 */
6de88d72 1755static int link_path_walk(const char *name, struct nameidata *nd)
1da177e4
LT
1756{
1757 struct path next;
1da177e4 1758 int err;
1da177e4
LT
1759
1760 while (*name=='/')
1761 name++;
1762 if (!*name)
086e183a 1763 return 0;
1da177e4 1764
1da177e4
LT
1765 /* At this point we know we have a real path component. */
1766 for(;;) {
1da177e4 1767 struct qstr this;
200e9ef7 1768 long len;
fe479a58 1769 int type;
1da177e4 1770
52094c8a 1771 err = may_lookup(nd);
1da177e4
LT
1772 if (err)
1773 break;
1774
200e9ef7 1775 len = hash_name(name, &this.hash);
1da177e4 1776 this.name = name;
200e9ef7 1777 this.len = len;
1da177e4 1778
fe479a58 1779 type = LAST_NORM;
200e9ef7 1780 if (name[0] == '.') switch (len) {
fe479a58 1781 case 2:
200e9ef7 1782 if (name[1] == '.') {
fe479a58 1783 type = LAST_DOTDOT;
16c2cd71
AV
1784 nd->flags |= LOOKUP_JUMPED;
1785 }
fe479a58
AV
1786 break;
1787 case 1:
1788 type = LAST_DOT;
1789 }
5a202bcd
AV
1790 if (likely(type == LAST_NORM)) {
1791 struct dentry *parent = nd->path.dentry;
16c2cd71 1792 nd->flags &= ~LOOKUP_JUMPED;
5a202bcd 1793 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
da53be12 1794 err = parent->d_op->d_hash(parent, &this);
5a202bcd
AV
1795 if (err < 0)
1796 break;
1797 }
1798 }
fe479a58 1799
5f4a6a69
AV
1800 nd->last = this;
1801 nd->last_type = type;
1802
200e9ef7 1803 if (!name[len])
5f4a6a69 1804 return 0;
200e9ef7
LT
1805 /*
1806 * If it wasn't NUL, we know it was '/'. Skip that
1807 * slash, and continue until no more slashes.
1808 */
1809 do {
1810 len++;
1811 } while (unlikely(name[len] == '/'));
1812 if (!name[len])
5f4a6a69
AV
1813 return 0;
1814
200e9ef7 1815 name += len;
1da177e4 1816
21b9b073 1817 err = walk_component(nd, &next, LOOKUP_FOLLOW);
ce57dfc1
AV
1818 if (err < 0)
1819 return err;
1da177e4 1820
ce57dfc1 1821 if (err) {
b356379a 1822 err = nested_symlink(&next, nd);
1da177e4 1823 if (err)
a7472bab 1824 return err;
31e6b01f 1825 }
5f4a6a69
AV
1826 if (!can_lookup(nd->inode)) {
1827 err = -ENOTDIR;
1828 break;
1829 }
1da177e4 1830 }
951361f9 1831 terminate_walk(nd);
1da177e4
LT
1832 return err;
1833}
1834
70e9b357
AV
1835static int path_init(int dfd, const char *name, unsigned int flags,
1836 struct nameidata *nd, struct file **fp)
31e6b01f
NP
1837{
1838 int retval = 0;
31e6b01f
NP
1839
1840 nd->last_type = LAST_ROOT; /* if there are only slashes... */
16c2cd71 1841 nd->flags = flags | LOOKUP_JUMPED;
31e6b01f 1842 nd->depth = 0;
5b6ca027
AV
1843 if (flags & LOOKUP_ROOT) {
1844 struct inode *inode = nd->root.dentry->d_inode;
73d049a4 1845 if (*name) {
741b7c3f 1846 if (!can_lookup(inode))
73d049a4
AV
1847 return -ENOTDIR;
1848 retval = inode_permission(inode, MAY_EXEC);
1849 if (retval)
1850 return retval;
1851 }
5b6ca027
AV
1852 nd->path = nd->root;
1853 nd->inode = inode;
1854 if (flags & LOOKUP_RCU) {
8b61e74f 1855 rcu_read_lock();
5b6ca027 1856 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
48a066e7 1857 nd->m_seq = read_seqbegin(&mount_lock);
5b6ca027
AV
1858 } else {
1859 path_get(&nd->path);
1860 }
1861 return 0;
1862 }
1863
31e6b01f 1864 nd->root.mnt = NULL;
31e6b01f 1865
48a066e7 1866 nd->m_seq = read_seqbegin(&mount_lock);
31e6b01f 1867 if (*name=='/') {
e41f7d4e 1868 if (flags & LOOKUP_RCU) {
8b61e74f 1869 rcu_read_lock();
e41f7d4e
AV
1870 set_root_rcu(nd);
1871 } else {
1872 set_root(nd);
1873 path_get(&nd->root);
1874 }
1875 nd->path = nd->root;
31e6b01f 1876 } else if (dfd == AT_FDCWD) {
e41f7d4e
AV
1877 if (flags & LOOKUP_RCU) {
1878 struct fs_struct *fs = current->fs;
1879 unsigned seq;
31e6b01f 1880
8b61e74f 1881 rcu_read_lock();
c28cc364 1882
e41f7d4e
AV
1883 do {
1884 seq = read_seqcount_begin(&fs->seq);
1885 nd->path = fs->pwd;
1886 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1887 } while (read_seqcount_retry(&fs->seq, seq));
1888 } else {
1889 get_fs_pwd(current->fs, &nd->path);
1890 }
31e6b01f 1891 } else {
582aa64a 1892 /* Caller must check execute permissions on the starting path component */
2903ff01 1893 struct fd f = fdget_raw(dfd);
31e6b01f
NP
1894 struct dentry *dentry;
1895
2903ff01
AV
1896 if (!f.file)
1897 return -EBADF;
31e6b01f 1898
2903ff01 1899 dentry = f.file->f_path.dentry;
31e6b01f 1900
f52e0c11 1901 if (*name) {
741b7c3f 1902 if (!can_lookup(dentry->d_inode)) {
2903ff01
AV
1903 fdput(f);
1904 return -ENOTDIR;
1905 }
f52e0c11 1906 }
31e6b01f 1907
2903ff01 1908 nd->path = f.file->f_path;
e41f7d4e 1909 if (flags & LOOKUP_RCU) {
2903ff01
AV
1910 if (f.need_put)
1911 *fp = f.file;
e41f7d4e 1912 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
8b61e74f 1913 rcu_read_lock();
e41f7d4e 1914 } else {
2903ff01
AV
1915 path_get(&nd->path);
1916 fdput(f);
e41f7d4e 1917 }
31e6b01f 1918 }
31e6b01f 1919
31e6b01f 1920 nd->inode = nd->path.dentry->d_inode;
9b4a9b14 1921 return 0;
9b4a9b14
AV
1922}
1923
bd92d7fe
AV
1924static inline int lookup_last(struct nameidata *nd, struct path *path)
1925{
1926 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1927 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1928
1929 nd->flags &= ~LOOKUP_PARENT;
21b9b073 1930 return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
bd92d7fe
AV
1931}
1932
9b4a9b14 1933/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
ee0827cd 1934static int path_lookupat(int dfd, const char *name,
9b4a9b14
AV
1935 unsigned int flags, struct nameidata *nd)
1936{
70e9b357 1937 struct file *base = NULL;
bd92d7fe
AV
1938 struct path path;
1939 int err;
31e6b01f
NP
1940
1941 /*
1942 * Path walking is largely split up into 2 different synchronisation
1943 * schemes, rcu-walk and ref-walk (explained in
1944 * Documentation/filesystems/path-lookup.txt). These share much of the
1945 * path walk code, but some things particularly setup, cleanup, and
1946 * following mounts are sufficiently divergent that functions are
1947 * duplicated. Typically there is a function foo(), and its RCU
1948 * analogue, foo_rcu().
1949 *
1950 * -ECHILD is the error number of choice (just to avoid clashes) that
1951 * is returned if some aspect of an rcu-walk fails. Such an error must
1952 * be handled by restarting a traditional ref-walk (which will always
1953 * be able to complete).
1954 */
bd92d7fe 1955 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
ee0827cd 1956
bd92d7fe
AV
1957 if (unlikely(err))
1958 return err;
ee0827cd
AV
1959
1960 current->total_link_count = 0;
bd92d7fe
AV
1961 err = link_path_walk(name, nd);
1962
1963 if (!err && !(flags & LOOKUP_PARENT)) {
bd92d7fe
AV
1964 err = lookup_last(nd, &path);
1965 while (err > 0) {
1966 void *cookie;
1967 struct path link = path;
800179c9
KC
1968 err = may_follow_link(&link, nd);
1969 if (unlikely(err))
1970 break;
bd92d7fe 1971 nd->flags |= LOOKUP_PARENT;
574197e0 1972 err = follow_link(&link, nd, &cookie);
6d7b5aae
AV
1973 if (err)
1974 break;
1975 err = lookup_last(nd, &path);
574197e0 1976 put_link(nd, &link, cookie);
bd92d7fe
AV
1977 }
1978 }
ee0827cd 1979
9f1fafee
AV
1980 if (!err)
1981 err = complete_walk(nd);
bd92d7fe
AV
1982
1983 if (!err && nd->flags & LOOKUP_DIRECTORY) {
05252901 1984 if (!can_lookup(nd->inode)) {
bd92d7fe 1985 path_put(&nd->path);
bd23a539 1986 err = -ENOTDIR;
bd92d7fe
AV
1987 }
1988 }
16c2cd71 1989
70e9b357
AV
1990 if (base)
1991 fput(base);
ee0827cd 1992
5b6ca027 1993 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
2a737871
AV
1994 path_put(&nd->root);
1995 nd->root.mnt = NULL;
1996 }
bd92d7fe 1997 return err;
ee0827cd 1998}
31e6b01f 1999
873f1eed 2000static int filename_lookup(int dfd, struct filename *name,
ee0827cd
AV
2001 unsigned int flags, struct nameidata *nd)
2002{
873f1eed 2003 int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
ee0827cd 2004 if (unlikely(retval == -ECHILD))
873f1eed 2005 retval = path_lookupat(dfd, name->name, flags, nd);
ee0827cd 2006 if (unlikely(retval == -ESTALE))
873f1eed
JL
2007 retval = path_lookupat(dfd, name->name,
2008 flags | LOOKUP_REVAL, nd);
31e6b01f 2009
f78570dd 2010 if (likely(!retval))
adb5c247 2011 audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
170aa3d0 2012 return retval;
1da177e4
LT
2013}
2014
873f1eed
JL
2015static int do_path_lookup(int dfd, const char *name,
2016 unsigned int flags, struct nameidata *nd)
2017{
2018 struct filename filename = { .name = name };
2019
2020 return filename_lookup(dfd, &filename, flags, nd);
2021}
2022
79714f72
AV
2023/* does lookup, returns the object with parent locked */
2024struct dentry *kern_path_locked(const char *name, struct path *path)
5590ff0d 2025{
79714f72
AV
2026 struct nameidata nd;
2027 struct dentry *d;
2028 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
2029 if (err)
2030 return ERR_PTR(err);
2031 if (nd.last_type != LAST_NORM) {
2032 path_put(&nd.path);
2033 return ERR_PTR(-EINVAL);
2034 }
2035 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
1e0ea001 2036 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
79714f72
AV
2037 if (IS_ERR(d)) {
2038 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2039 path_put(&nd.path);
2040 return d;
2041 }
2042 *path = nd.path;
2043 return d;
5590ff0d
UD
2044}
2045
d1811465
AV
2046int kern_path(const char *name, unsigned int flags, struct path *path)
2047{
2048 struct nameidata nd;
2049 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2050 if (!res)
2051 *path = nd.path;
2052 return res;
2053}
2054
16f18200
JJS
2055/**
2056 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2057 * @dentry: pointer to dentry of the base directory
2058 * @mnt: pointer to vfs mount of the base directory
2059 * @name: pointer to file name
2060 * @flags: lookup flags
e0a01249 2061 * @path: pointer to struct path to fill
16f18200
JJS
2062 */
2063int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2064 const char *name, unsigned int flags,
e0a01249 2065 struct path *path)
16f18200 2066{
e0a01249
AV
2067 struct nameidata nd;
2068 int err;
2069 nd.root.dentry = dentry;
2070 nd.root.mnt = mnt;
2071 BUG_ON(flags & LOOKUP_PARENT);
5b6ca027 2072 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
e0a01249
AV
2073 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2074 if (!err)
2075 *path = nd.path;
2076 return err;
16f18200
JJS
2077}
2078
057f6c01
JM
2079/*
2080 * Restricted form of lookup. Doesn't follow links, single-component only,
2081 * needs parent already locked. Doesn't follow mounts.
2082 * SMP-safe.
2083 */
eead1911 2084static struct dentry *lookup_hash(struct nameidata *nd)
057f6c01 2085{
72bd866a 2086 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
1da177e4
LT
2087}
2088
eead1911 2089/**
a6b91919 2090 * lookup_one_len - filesystem helper to lookup single pathname component
eead1911
CH
2091 * @name: pathname component to lookup
2092 * @base: base directory to lookup from
2093 * @len: maximum length @len should be interpreted to
2094 *
a6b91919
RD
2095 * Note that this routine is purely a helper for filesystem usage and should
2096 * not be called by generic code. Also note that by using this function the
eead1911
CH
2097 * nameidata argument is passed to the filesystem methods and a filesystem
2098 * using this helper needs to be prepared for that.
2099 */
057f6c01
JM
2100struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2101{
057f6c01 2102 struct qstr this;
6a96ba54 2103 unsigned int c;
cda309de 2104 int err;
057f6c01 2105
2f9092e1
DW
2106 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2107
6a96ba54
AV
2108 this.name = name;
2109 this.len = len;
0145acc2 2110 this.hash = full_name_hash(name, len);
6a96ba54
AV
2111 if (!len)
2112 return ERR_PTR(-EACCES);
2113
21d8a15a
AV
2114 if (unlikely(name[0] == '.')) {
2115 if (len < 2 || (len == 2 && name[1] == '.'))
2116 return ERR_PTR(-EACCES);
2117 }
2118
6a96ba54
AV
2119 while (len--) {
2120 c = *(const unsigned char *)name++;
2121 if (c == '/' || c == '\0')
2122 return ERR_PTR(-EACCES);
6a96ba54 2123 }
5a202bcd
AV
2124 /*
2125 * See if the low-level filesystem might want
2126 * to use its own hash..
2127 */
2128 if (base->d_flags & DCACHE_OP_HASH) {
da53be12 2129 int err = base->d_op->d_hash(base, &this);
5a202bcd
AV
2130 if (err < 0)
2131 return ERR_PTR(err);
2132 }
eead1911 2133
cda309de
MS
2134 err = inode_permission(base->d_inode, MAY_EXEC);
2135 if (err)
2136 return ERR_PTR(err);
2137
72bd866a 2138 return __lookup_hash(&this, base, 0);
057f6c01
JM
2139}
2140
1fa1e7f6
AW
2141int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2142 struct path *path, int *empty)
1da177e4 2143{
2d8f3038 2144 struct nameidata nd;
91a27b2a 2145 struct filename *tmp = getname_flags(name, flags, empty);
1da177e4 2146 int err = PTR_ERR(tmp);
1da177e4 2147 if (!IS_ERR(tmp)) {
2d8f3038
AV
2148
2149 BUG_ON(flags & LOOKUP_PARENT);
2150
873f1eed 2151 err = filename_lookup(dfd, tmp, flags, &nd);
1da177e4 2152 putname(tmp);
2d8f3038
AV
2153 if (!err)
2154 *path = nd.path;
1da177e4
LT
2155 }
2156 return err;
2157}
2158
1fa1e7f6
AW
2159int user_path_at(int dfd, const char __user *name, unsigned flags,
2160 struct path *path)
2161{
f7493e5d 2162 return user_path_at_empty(dfd, name, flags, path, NULL);
1fa1e7f6
AW
2163}
2164
873f1eed
JL
2165/*
2166 * NB: most callers don't do anything directly with the reference to the
2167 * to struct filename, but the nd->last pointer points into the name string
2168 * allocated by getname. So we must hold the reference to it until all
2169 * path-walking is complete.
2170 */
91a27b2a 2171static struct filename *
9e790bd6
JL
2172user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
2173 unsigned int flags)
2ad94ae6 2174{
91a27b2a 2175 struct filename *s = getname(path);
2ad94ae6
AV
2176 int error;
2177
9e790bd6
JL
2178 /* only LOOKUP_REVAL is allowed in extra flags */
2179 flags &= LOOKUP_REVAL;
2180
2ad94ae6 2181 if (IS_ERR(s))
91a27b2a 2182 return s;
2ad94ae6 2183
9e790bd6 2184 error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
91a27b2a 2185 if (error) {
2ad94ae6 2186 putname(s);
91a27b2a
JL
2187 return ERR_PTR(error);
2188 }
2ad94ae6 2189
91a27b2a 2190 return s;
2ad94ae6
AV
2191}
2192
8033426e 2193/**
197df04c 2194 * mountpoint_last - look up last component for umount
8033426e
JL
2195 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2196 * @path: pointer to container for result
2197 *
2198 * This is a special lookup_last function just for umount. In this case, we
2199 * need to resolve the path without doing any revalidation.
2200 *
2201 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2202 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2203 * in almost all cases, this lookup will be served out of the dcache. The only
2204 * cases where it won't are if nd->last refers to a symlink or the path is
2205 * bogus and it doesn't exist.
2206 *
2207 * Returns:
2208 * -error: if there was an error during lookup. This includes -ENOENT if the
2209 * lookup found a negative dentry. The nd->path reference will also be
2210 * put in this case.
2211 *
2212 * 0: if we successfully resolved nd->path and found it to not to be a
2213 * symlink that needs to be followed. "path" will also be populated.
2214 * The nd->path reference will also be put.
2215 *
2216 * 1: if we successfully resolved nd->last and found it to be a symlink
2217 * that needs to be followed. "path" will be populated with the path
2218 * to the link, and nd->path will *not* be put.
2219 */
2220static int
197df04c 2221mountpoint_last(struct nameidata *nd, struct path *path)
8033426e
JL
2222{
2223 int error = 0;
2224 struct dentry *dentry;
2225 struct dentry *dir = nd->path.dentry;
2226
35759521
AV
2227 /* If we're in rcuwalk, drop out of it to handle last component */
2228 if (nd->flags & LOOKUP_RCU) {
2229 if (unlazy_walk(nd, NULL)) {
2230 error = -ECHILD;
2231 goto out;
2232 }
8033426e
JL
2233 }
2234
2235 nd->flags &= ~LOOKUP_PARENT;
2236
2237 if (unlikely(nd->last_type != LAST_NORM)) {
2238 error = handle_dots(nd, nd->last_type);
35759521
AV
2239 if (error)
2240 goto out;
2241 dentry = dget(nd->path.dentry);
2242 goto done;
8033426e
JL
2243 }
2244
2245 mutex_lock(&dir->d_inode->i_mutex);
2246 dentry = d_lookup(dir, &nd->last);
2247 if (!dentry) {
2248 /*
2249 * No cached dentry. Mounted dentries are pinned in the cache,
2250 * so that means that this dentry is probably a symlink or the
2251 * path doesn't actually point to a mounted dentry.
2252 */
2253 dentry = d_alloc(dir, &nd->last);
2254 if (!dentry) {
2255 error = -ENOMEM;
bcceeeba 2256 mutex_unlock(&dir->d_inode->i_mutex);
35759521 2257 goto out;
8033426e 2258 }
35759521
AV
2259 dentry = lookup_real(dir->d_inode, dentry, nd->flags);
2260 error = PTR_ERR(dentry);
bcceeeba
DJ
2261 if (IS_ERR(dentry)) {
2262 mutex_unlock(&dir->d_inode->i_mutex);
35759521 2263 goto out;
bcceeeba 2264 }
8033426e
JL
2265 }
2266 mutex_unlock(&dir->d_inode->i_mutex);
2267
35759521
AV
2268done:
2269 if (!dentry->d_inode) {
2270 error = -ENOENT;
2271 dput(dentry);
2272 goto out;
8033426e 2273 }
35759521
AV
2274 path->dentry = dentry;
2275 path->mnt = mntget(nd->path.mnt);
2276 if (should_follow_link(dentry->d_inode, nd->flags & LOOKUP_FOLLOW))
2277 return 1;
2278 follow_mount(path);
2279 error = 0;
2280out:
8033426e
JL
2281 terminate_walk(nd);
2282 return error;
2283}
2284
2285/**
197df04c 2286 * path_mountpoint - look up a path to be umounted
8033426e
JL
2287 * @dfd: directory file descriptor to start walk from
2288 * @name: full pathname to walk
2289 * @flags: lookup flags
8033426e
JL
2290 *
2291 * Look up the given name, but don't attempt to revalidate the last component.
2292 * Returns 0 and "path" will be valid on success; Retuns error otherwise.
2293 */
2294static int
197df04c 2295path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
8033426e
JL
2296{
2297 struct file *base = NULL;
2298 struct nameidata nd;
2299 int err;
2300
2301 err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
2302 if (unlikely(err))
2303 return err;
2304
2305 current->total_link_count = 0;
2306 err = link_path_walk(name, &nd);
2307 if (err)
2308 goto out;
2309
197df04c 2310 err = mountpoint_last(&nd, path);
8033426e
JL
2311 while (err > 0) {
2312 void *cookie;
2313 struct path link = *path;
2314 err = may_follow_link(&link, &nd);
2315 if (unlikely(err))
2316 break;
2317 nd.flags |= LOOKUP_PARENT;
2318 err = follow_link(&link, &nd, &cookie);
2319 if (err)
2320 break;
197df04c 2321 err = mountpoint_last(&nd, path);
8033426e
JL
2322 put_link(&nd, &link, cookie);
2323 }
2324out:
2325 if (base)
2326 fput(base);
2327
2328 if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
2329 path_put(&nd.root);
2330
2331 return err;
2332}
2333
2d864651
AV
2334static int
2335filename_mountpoint(int dfd, struct filename *s, struct path *path,
2336 unsigned int flags)
2337{
2338 int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
2339 if (unlikely(error == -ECHILD))
2340 error = path_mountpoint(dfd, s->name, path, flags);
2341 if (unlikely(error == -ESTALE))
2342 error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
2343 if (likely(!error))
2344 audit_inode(s, path->dentry, 0);
2345 return error;
2346}
2347
8033426e 2348/**
197df04c 2349 * user_path_mountpoint_at - lookup a path from userland in order to umount it
8033426e
JL
2350 * @dfd: directory file descriptor
2351 * @name: pathname from userland
2352 * @flags: lookup flags
2353 * @path: pointer to container to hold result
2354 *
2355 * A umount is a special case for path walking. We're not actually interested
2356 * in the inode in this situation, and ESTALE errors can be a problem. We
2357 * simply want track down the dentry and vfsmount attached at the mountpoint
2358 * and avoid revalidating the last component.
2359 *
2360 * Returns 0 and populates "path" on success.
2361 */
2362int
197df04c 2363user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
8033426e
JL
2364 struct path *path)
2365{
2366 struct filename *s = getname(name);
2367 int error;
8033426e
JL
2368 if (IS_ERR(s))
2369 return PTR_ERR(s);
2d864651 2370 error = filename_mountpoint(dfd, s, path, flags);
8033426e
JL
2371 putname(s);
2372 return error;
2373}
2374
2d864651
AV
2375int
2376kern_path_mountpoint(int dfd, const char *name, struct path *path,
2377 unsigned int flags)
2378{
2379 struct filename s = {.name = name};
2380 return filename_mountpoint(dfd, &s, path, flags);
2381}
2382EXPORT_SYMBOL(kern_path_mountpoint);
2383
1da177e4
LT
2384/*
2385 * It's inline, so penalty for filesystems that don't use sticky bit is
2386 * minimal.
2387 */
2388static inline int check_sticky(struct inode *dir, struct inode *inode)
2389{
8e96e3b7 2390 kuid_t fsuid = current_fsuid();
da9592ed 2391
1da177e4
LT
2392 if (!(dir->i_mode & S_ISVTX))
2393 return 0;
8e96e3b7 2394 if (uid_eq(inode->i_uid, fsuid))
1da177e4 2395 return 0;
8e96e3b7 2396 if (uid_eq(dir->i_uid, fsuid))
1da177e4 2397 return 0;
1a48e2ac 2398 return !inode_capable(inode, CAP_FOWNER);
1da177e4
LT
2399}
2400
2401/*
2402 * Check whether we can remove a link victim from directory dir, check
2403 * whether the type of victim is right.
2404 * 1. We can't do it if dir is read-only (done in permission())
2405 * 2. We should have write and exec permissions on dir
2406 * 3. We can't remove anything from append-only dir
2407 * 4. We can't do anything with immutable dir (done in permission())
2408 * 5. If the sticky bit on dir is set we should either
2409 * a. be owner of dir, or
2410 * b. be owner of victim, or
2411 * c. have CAP_FOWNER capability
2412 * 6. If the victim is append-only or immutable we can't do antyhing with
2413 * links pointing to it.
2414 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2415 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2416 * 9. We can't remove a root or mountpoint.
2417 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2418 * nfs_async_unlink().
2419 */
858119e1 2420static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1da177e4
LT
2421{
2422 int error;
2423
2424 if (!victim->d_inode)
2425 return -ENOENT;
2426
2427 BUG_ON(victim->d_parent->d_inode != dir);
4fa6b5ec 2428 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
1da177e4 2429
f419a2e3 2430 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
2431 if (error)
2432 return error;
2433 if (IS_APPEND(dir))
2434 return -EPERM;
2435 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
f9454548 2436 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1da177e4
LT
2437 return -EPERM;
2438 if (isdir) {
2439 if (!S_ISDIR(victim->d_inode->i_mode))
2440 return -ENOTDIR;
2441 if (IS_ROOT(victim))
2442 return -EBUSY;
2443 } else if (S_ISDIR(victim->d_inode->i_mode))
2444 return -EISDIR;
2445 if (IS_DEADDIR(dir))
2446 return -ENOENT;
2447 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2448 return -EBUSY;
2449 return 0;
2450}
2451
2452/* Check whether we can create an object with dentry child in directory
2453 * dir.
2454 * 1. We can't do it if child already exists (open has special treatment for
2455 * this case, but since we are inlined it's OK)
2456 * 2. We can't do it if dir is read-only (done in permission())
2457 * 3. We should have write and exec permissions on dir
2458 * 4. We can't do it if dir is immutable (done in permission())
2459 */
a95164d9 2460static inline int may_create(struct inode *dir, struct dentry *child)
1da177e4
LT
2461{
2462 if (child->d_inode)
2463 return -EEXIST;
2464 if (IS_DEADDIR(dir))
2465 return -ENOENT;
f419a2e3 2466 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
2467}
2468
1da177e4
LT
2469/*
2470 * p1 and p2 should be directories on the same fs.
2471 */
2472struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2473{
2474 struct dentry *p;
2475
2476 if (p1 == p2) {
f2eace23 2477 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1da177e4
LT
2478 return NULL;
2479 }
2480
a11f3a05 2481 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4 2482
e2761a11
OH
2483 p = d_ancestor(p2, p1);
2484 if (p) {
2485 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2486 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2487 return p;
1da177e4
LT
2488 }
2489
e2761a11
OH
2490 p = d_ancestor(p1, p2);
2491 if (p) {
2492 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2493 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2494 return p;
1da177e4
LT
2495 }
2496
f2eace23
IM
2497 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2498 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1da177e4
LT
2499 return NULL;
2500}
2501
2502void unlock_rename(struct dentry *p1, struct dentry *p2)
2503{
1b1dcc1b 2504 mutex_unlock(&p1->d_inode->i_mutex);
1da177e4 2505 if (p1 != p2) {
1b1dcc1b 2506 mutex_unlock(&p2->d_inode->i_mutex);
a11f3a05 2507 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4
LT
2508 }
2509}
2510
4acdaf27 2511int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
312b63fb 2512 bool want_excl)
1da177e4 2513{
a95164d9 2514 int error = may_create(dir, dentry);
1da177e4
LT
2515 if (error)
2516 return error;
2517
acfa4380 2518 if (!dir->i_op->create)
1da177e4
LT
2519 return -EACCES; /* shouldn't it be ENOSYS? */
2520 mode &= S_IALLUGO;
2521 mode |= S_IFREG;
2522 error = security_inode_create(dir, dentry, mode);
2523 if (error)
2524 return error;
312b63fb 2525 error = dir->i_op->create(dir, dentry, mode, want_excl);
a74574aa 2526 if (!error)
f38aa942 2527 fsnotify_create(dir, dentry);
1da177e4
LT
2528 return error;
2529}
2530
73d049a4 2531static int may_open(struct path *path, int acc_mode, int flag)
1da177e4 2532{
3fb64190 2533 struct dentry *dentry = path->dentry;
1da177e4
LT
2534 struct inode *inode = dentry->d_inode;
2535 int error;
2536
bcda7652
AV
2537 /* O_PATH? */
2538 if (!acc_mode)
2539 return 0;
2540
1da177e4
LT
2541 if (!inode)
2542 return -ENOENT;
2543
c8fe8f30
CH
2544 switch (inode->i_mode & S_IFMT) {
2545 case S_IFLNK:
1da177e4 2546 return -ELOOP;
c8fe8f30
CH
2547 case S_IFDIR:
2548 if (acc_mode & MAY_WRITE)
2549 return -EISDIR;
2550 break;
2551 case S_IFBLK:
2552 case S_IFCHR:
3fb64190 2553 if (path->mnt->mnt_flags & MNT_NODEV)
1da177e4 2554 return -EACCES;
c8fe8f30
CH
2555 /*FALLTHRU*/
2556 case S_IFIFO:
2557 case S_IFSOCK:
1da177e4 2558 flag &= ~O_TRUNC;
c8fe8f30 2559 break;
4a3fd211 2560 }
b41572e9 2561
3fb64190 2562 error = inode_permission(inode, acc_mode);
b41572e9
DH
2563 if (error)
2564 return error;
6146f0d5 2565
1da177e4
LT
2566 /*
2567 * An append-only file must be opened in append mode for writing.
2568 */
2569 if (IS_APPEND(inode)) {
8737c930 2570 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
7715b521 2571 return -EPERM;
1da177e4 2572 if (flag & O_TRUNC)
7715b521 2573 return -EPERM;
1da177e4
LT
2574 }
2575
2576 /* O_NOATIME can only be set by the owner or superuser */
2e149670 2577 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
7715b521 2578 return -EPERM;
1da177e4 2579
f3c7691e 2580 return 0;
7715b521 2581}
1da177e4 2582
e1181ee6 2583static int handle_truncate(struct file *filp)
7715b521 2584{
e1181ee6 2585 struct path *path = &filp->f_path;
7715b521
AV
2586 struct inode *inode = path->dentry->d_inode;
2587 int error = get_write_access(inode);
2588 if (error)
2589 return error;
2590 /*
2591 * Refuse to truncate files with mandatory locks held on them.
2592 */
2593 error = locks_verify_locked(inode);
2594 if (!error)
ea0d3ab2 2595 error = security_path_truncate(path);
7715b521
AV
2596 if (!error) {
2597 error = do_truncate(path->dentry, 0,
2598 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
e1181ee6 2599 filp);
7715b521
AV
2600 }
2601 put_write_access(inode);
acd0c935 2602 return error;
1da177e4
LT
2603}
2604
d57999e1
DH
2605static inline int open_to_namei_flags(int flag)
2606{
8a5e929d
AV
2607 if ((flag & O_ACCMODE) == 3)
2608 flag--;
d57999e1
DH
2609 return flag;
2610}
2611
d18e9008
MS
2612static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2613{
2614 int error = security_path_mknod(dir, dentry, mode, 0);
2615 if (error)
2616 return error;
2617
2618 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2619 if (error)
2620 return error;
2621
2622 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2623}
2624
1acf0af9
DH
2625/*
2626 * Attempt to atomically look up, create and open a file from a negative
2627 * dentry.
2628 *
2629 * Returns 0 if successful. The file will have been created and attached to
2630 * @file by the filesystem calling finish_open().
2631 *
2632 * Returns 1 if the file was looked up only or didn't need creating. The
2633 * caller will need to perform the open themselves. @path will have been
2634 * updated to point to the new dentry. This may be negative.
2635 *
2636 * Returns an error code otherwise.
2637 */
2675a4eb
AV
2638static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2639 struct path *path, struct file *file,
2640 const struct open_flags *op,
64894cf8 2641 bool got_write, bool need_lookup,
2675a4eb 2642 int *opened)
d18e9008
MS
2643{
2644 struct inode *dir = nd->path.dentry->d_inode;
2645 unsigned open_flag = open_to_namei_flags(op->open_flag);
2646 umode_t mode;
2647 int error;
2648 int acc_mode;
d18e9008
MS
2649 int create_error = 0;
2650 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
116cc022 2651 bool excl;
d18e9008
MS
2652
2653 BUG_ON(dentry->d_inode);
2654
2655 /* Don't create child dentry for a dead directory. */
2656 if (unlikely(IS_DEADDIR(dir))) {
2675a4eb 2657 error = -ENOENT;
d18e9008
MS
2658 goto out;
2659 }
2660
62b259d8 2661 mode = op->mode;
d18e9008
MS
2662 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2663 mode &= ~current_umask();
2664
116cc022
MS
2665 excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2666 if (excl)
d18e9008 2667 open_flag &= ~O_TRUNC;
d18e9008
MS
2668
2669 /*
2670 * Checking write permission is tricky, bacuse we don't know if we are
2671 * going to actually need it: O_CREAT opens should work as long as the
2672 * file exists. But checking existence breaks atomicity. The trick is
2673 * to check access and if not granted clear O_CREAT from the flags.
2674 *
2675 * Another problem is returing the "right" error value (e.g. for an
2676 * O_EXCL open we want to return EEXIST not EROFS).
2677 */
64894cf8
AV
2678 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2679 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2680 if (!(open_flag & O_CREAT)) {
d18e9008
MS
2681 /*
2682 * No O_CREATE -> atomicity not a requirement -> fall
2683 * back to lookup + open
2684 */
2685 goto no_open;
2686 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2687 /* Fall back and fail with the right error */
64894cf8 2688 create_error = -EROFS;
d18e9008
MS
2689 goto no_open;
2690 } else {
2691 /* No side effects, safe to clear O_CREAT */
64894cf8 2692 create_error = -EROFS;
d18e9008
MS
2693 open_flag &= ~O_CREAT;
2694 }
2695 }
2696
2697 if (open_flag & O_CREAT) {
38227f78 2698 error = may_o_create(&nd->path, dentry, mode);
d18e9008
MS
2699 if (error) {
2700 create_error = error;
2701 if (open_flag & O_EXCL)
2702 goto no_open;
2703 open_flag &= ~O_CREAT;
2704 }
2705 }
2706
2707 if (nd->flags & LOOKUP_DIRECTORY)
2708 open_flag |= O_DIRECTORY;
2709
30d90494
AV
2710 file->f_path.dentry = DENTRY_NOT_SET;
2711 file->f_path.mnt = nd->path.mnt;
2712 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
47237687 2713 opened);
d9585277 2714 if (error < 0) {
d9585277
AV
2715 if (create_error && error == -ENOENT)
2716 error = create_error;
d18e9008
MS
2717 goto out;
2718 }
2719
d9585277 2720 if (error) { /* returned 1, that is */
30d90494 2721 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2675a4eb 2722 error = -EIO;
d18e9008
MS
2723 goto out;
2724 }
30d90494 2725 if (file->f_path.dentry) {
d18e9008 2726 dput(dentry);
30d90494 2727 dentry = file->f_path.dentry;
d18e9008 2728 }
03da633a
AV
2729 if (*opened & FILE_CREATED)
2730 fsnotify_create(dir, dentry);
2731 if (!dentry->d_inode) {
2732 WARN_ON(*opened & FILE_CREATED);
2733 if (create_error) {
2734 error = create_error;
2735 goto out;
2736 }
2737 } else {
2738 if (excl && !(*opened & FILE_CREATED)) {
2739 error = -EEXIST;
2740 goto out;
2741 }
62b2ce96 2742 }
d18e9008
MS
2743 goto looked_up;
2744 }
2745
2746 /*
2747 * We didn't have the inode before the open, so check open permission
2748 * here.
2749 */
03da633a
AV
2750 acc_mode = op->acc_mode;
2751 if (*opened & FILE_CREATED) {
2752 WARN_ON(!(open_flag & O_CREAT));
2753 fsnotify_create(dir, dentry);
2754 acc_mode = MAY_OPEN;
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
1da177e4
LT
4234/* get the link contents into pagecache */
4235static char *page_getlink(struct dentry * dentry, struct page **ppage)
4236{
ebd09abb
DG
4237 char *kaddr;
4238 struct page *page;
1da177e4 4239 struct address_space *mapping = dentry->d_inode->i_mapping;
090d2b18 4240 page = read_mapping_page(mapping, 0, NULL);
1da177e4 4241 if (IS_ERR(page))
6fe6900e 4242 return (char*)page;
1da177e4 4243 *ppage = page;
ebd09abb
DG
4244 kaddr = kmap(page);
4245 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4246 return kaddr;
1da177e4
LT
4247}
4248
4249int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4250{
4251 struct page *page = NULL;
4252 char *s = page_getlink(dentry, &page);
4253 int res = vfs_readlink(dentry,buffer,buflen,s);
4254 if (page) {
4255 kunmap(page);
4256 page_cache_release(page);
4257 }
4258 return res;
4259}
4260
cc314eef 4261void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
1da177e4 4262{
cc314eef 4263 struct page *page = NULL;
1da177e4 4264 nd_set_link(nd, page_getlink(dentry, &page));
cc314eef 4265 return page;
1da177e4
LT
4266}
4267
cc314eef 4268void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1da177e4 4269{
cc314eef
LT
4270 struct page *page = cookie;
4271
4272 if (page) {
1da177e4
LT
4273 kunmap(page);
4274 page_cache_release(page);
1da177e4
LT
4275 }
4276}
4277
54566b2c
NP
4278/*
4279 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4280 */
4281int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
1da177e4
LT
4282{
4283 struct address_space *mapping = inode->i_mapping;
0adb25d2 4284 struct page *page;
afddba49 4285 void *fsdata;
beb497ab 4286 int err;
1da177e4 4287 char *kaddr;
54566b2c
NP
4288 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4289 if (nofs)
4290 flags |= AOP_FLAG_NOFS;
1da177e4 4291
7e53cac4 4292retry:
afddba49 4293 err = pagecache_write_begin(NULL, mapping, 0, len-1,
54566b2c 4294 flags, &page, &fsdata);
1da177e4 4295 if (err)
afddba49
NP
4296 goto fail;
4297
e8e3c3d6 4298 kaddr = kmap_atomic(page);
1da177e4 4299 memcpy(kaddr, symname, len-1);
e8e3c3d6 4300 kunmap_atomic(kaddr);
afddba49
NP
4301
4302 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4303 page, fsdata);
1da177e4
LT
4304 if (err < 0)
4305 goto fail;
afddba49
NP
4306 if (err < len-1)
4307 goto retry;
4308
1da177e4
LT
4309 mark_inode_dirty(inode);
4310 return 0;
1da177e4
LT
4311fail:
4312 return err;
4313}
4314
0adb25d2
KK
4315int page_symlink(struct inode *inode, const char *symname, int len)
4316{
4317 return __page_symlink(inode, symname, len,
54566b2c 4318 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
0adb25d2
KK
4319}
4320
92e1d5be 4321const struct inode_operations page_symlink_inode_operations = {
1da177e4
LT
4322 .readlink = generic_readlink,
4323 .follow_link = page_follow_link_light,
4324 .put_link = page_put_link,
4325};
4326
2d8f3038 4327EXPORT_SYMBOL(user_path_at);
cc53ce53 4328EXPORT_SYMBOL(follow_down_one);
1da177e4
LT
4329EXPORT_SYMBOL(follow_down);
4330EXPORT_SYMBOL(follow_up);
f6d2ac5c 4331EXPORT_SYMBOL(get_write_access); /* nfsd */
1da177e4 4332EXPORT_SYMBOL(lock_rename);
1da177e4
LT
4333EXPORT_SYMBOL(lookup_one_len);
4334EXPORT_SYMBOL(page_follow_link_light);
4335EXPORT_SYMBOL(page_put_link);
4336EXPORT_SYMBOL(page_readlink);
0adb25d2 4337EXPORT_SYMBOL(__page_symlink);
1da177e4
LT
4338EXPORT_SYMBOL(page_symlink);
4339EXPORT_SYMBOL(page_symlink_inode_operations);
d1811465 4340EXPORT_SYMBOL(kern_path);
16f18200 4341EXPORT_SYMBOL(vfs_path_lookup);
f419a2e3 4342EXPORT_SYMBOL(inode_permission);
1da177e4
LT
4343EXPORT_SYMBOL(unlock_rename);
4344EXPORT_SYMBOL(vfs_create);
1da177e4
LT
4345EXPORT_SYMBOL(vfs_link);
4346EXPORT_SYMBOL(vfs_mkdir);
4347EXPORT_SYMBOL(vfs_mknod);
4348EXPORT_SYMBOL(generic_permission);
4349EXPORT_SYMBOL(vfs_readlink);
4350EXPORT_SYMBOL(vfs_rename);
4351EXPORT_SYMBOL(vfs_rmdir);
4352EXPORT_SYMBOL(vfs_symlink);
4353EXPORT_SYMBOL(vfs_unlink);
4354EXPORT_SYMBOL(dentry_unhash);
4355EXPORT_SYMBOL(generic_readlink);
This page took 1.05924 seconds and 5 git commands to generate.