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