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