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