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