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