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