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