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