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