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