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