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