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