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