massage generic_permission() to treat directories on a separate path
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/fs.h>
21#include <linux/namei.h>
1da177e4 22#include <linux/pagemap.h>
0eeca283 23#include <linux/fsnotify.h>
1da177e4
LT
24#include <linux/personality.h>
25#include <linux/security.h>
6146f0d5 26#include <linux/ima.h>
1da177e4
LT
27#include <linux/syscalls.h>
28#include <linux/mount.h>
29#include <linux/audit.h>
16f7e0fe 30#include <linux/capability.h>
834f2a4a 31#include <linux/file.h>
5590ff0d 32#include <linux/fcntl.h>
08ce5f16 33#include <linux/device_cgroup.h>
5ad4e53b 34#include <linux/fs_struct.h>
1da177e4
LT
35#include <asm/uaccess.h>
36
e81e3f4d
EP
37#include "internal.h"
38
1da177e4
LT
39/* [Feb-1997 T. Schoebel-Theuer]
40 * Fundamental changes in the pathname lookup mechanisms (namei)
41 * were necessary because of omirr. The reason is that omirr needs
42 * to know the _real_ pathname, not the user-supplied one, in case
43 * of symlinks (and also when transname replacements occur).
44 *
45 * The new code replaces the old recursive symlink resolution with
46 * an iterative one (in case of non-nested symlink chains). It does
47 * this with calls to <fs>_follow_link().
48 * As a side effect, dir_namei(), _namei() and follow_link() are now
49 * replaced with a single function lookup_dentry() that can handle all
50 * the special cases of the former code.
51 *
52 * With the new dcache, the pathname is stored at each inode, at least as
53 * long as the refcount of the inode is positive. As a side effect, the
54 * size of the dcache depends on the inode cache and thus is dynamic.
55 *
56 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
57 * resolution to correspond with current state of the code.
58 *
59 * Note that the symlink resolution is not *completely* iterative.
60 * There is still a significant amount of tail- and mid- recursion in
61 * the algorithm. Also, note that <fs>_readlink() is not used in
62 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
63 * may return different results than <fs>_follow_link(). Many virtual
64 * filesystems (including /proc) exhibit this behavior.
65 */
66
67/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
68 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
69 * and the name already exists in form of a symlink, try to create the new
70 * name indicated by the symlink. The old code always complained that the
71 * name already exists, due to not following the symlink even if its target
72 * is nonexistent. The new semantics affects also mknod() and link() when
25985edc 73 * the name is a symlink pointing to a non-existent name.
1da177e4
LT
74 *
75 * I don't know which semantics is the right one, since I have no access
76 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
77 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
78 * "old" one. Personally, I think the new semantics is much more logical.
79 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
80 * file does succeed in both HP-UX and SunOs, but not in Solaris
81 * and in the old Linux semantics.
82 */
83
84/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
85 * semantics. See the comments in "open_namei" and "do_link" below.
86 *
87 * [10-Sep-98 Alan Modra] Another symlink change.
88 */
89
90/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
91 * inside the path - always follow.
92 * in the last component in creation/removal/renaming - never follow.
93 * if LOOKUP_FOLLOW passed - follow.
94 * if the pathname has trailing slashes - follow.
95 * otherwise - don't follow.
96 * (applied in that order).
97 *
98 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
99 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
100 * During the 2.4 we need to fix the userland stuff depending on it -
101 * hopefully we will be able to get rid of that wart in 2.5. So far only
102 * XEmacs seems to be relying on it...
103 */
104/*
105 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
a11f3a05 106 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
1da177e4
LT
107 * any extra contention...
108 */
109
110/* In order to reduce some races, while at the same time doing additional
111 * checking and hopefully speeding things up, we copy filenames to the
112 * kernel data space before using them..
113 *
114 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
115 * PATH_MAX includes the nul terminator --RR.
116 */
858119e1 117static int do_getname(const char __user *filename, char *page)
1da177e4
LT
118{
119 int retval;
120 unsigned long len = PATH_MAX;
121
122 if (!segment_eq(get_fs(), KERNEL_DS)) {
123 if ((unsigned long) filename >= TASK_SIZE)
124 return -EFAULT;
125 if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
126 len = TASK_SIZE - (unsigned long) filename;
127 }
128
129 retval = strncpy_from_user(page, filename, len);
130 if (retval > 0) {
131 if (retval < len)
132 return 0;
133 return -ENAMETOOLONG;
134 } else if (!retval)
135 retval = -ENOENT;
136 return retval;
137}
138
f52e0c11 139static char *getname_flags(const char __user * filename, int flags)
1da177e4
LT
140{
141 char *tmp, *result;
142
143 result = ERR_PTR(-ENOMEM);
144 tmp = __getname();
145 if (tmp) {
146 int retval = do_getname(filename, tmp);
147
148 result = tmp;
149 if (retval < 0) {
f52e0c11
AV
150 if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
151 __putname(tmp);
152 result = ERR_PTR(retval);
153 }
1da177e4
LT
154 }
155 }
156 audit_getname(result);
157 return result;
158}
159
f52e0c11
AV
160char *getname(const char __user * filename)
161{
162 return getname_flags(filename, 0);
163}
164
1da177e4
LT
165#ifdef CONFIG_AUDITSYSCALL
166void putname(const char *name)
167{
5ac3a9c2 168 if (unlikely(!audit_dummy_context()))
1da177e4
LT
169 audit_putname(name);
170 else
171 __putname(name);
172}
173EXPORT_SYMBOL(putname);
174#endif
175
5909ccaa
LT
176/*
177 * This does basic POSIX ACL permission checking
1da177e4 178 */
7e40145e 179static int acl_permission_check(struct inode *inode, int mask)
1da177e4 180{
7e40145e 181 int (*check_acl)(struct inode *inode, int mask);
26cf46be 182 unsigned int mode = inode->i_mode;
1da177e4 183
9c2c7039 184 mask &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
e6305c43 185
e795b717
SH
186 if (current_user_ns() != inode_userns(inode))
187 goto other_perms;
188
da9592ed 189 if (current_fsuid() == inode->i_uid)
1da177e4
LT
190 mode >>= 6;
191 else {
178ea735 192 check_acl = inode->i_op->check_acl;
1da177e4 193 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
7e40145e 194 int error = check_acl(inode, mask);
b74c79e9
NP
195 if (error != -EAGAIN)
196 return error;
1da177e4
LT
197 }
198
199 if (in_group_p(inode->i_gid))
200 mode >>= 3;
201 }
202
e795b717 203other_perms:
1da177e4
LT
204 /*
205 * If the DACs are ok we don't need any capability check.
206 */
9c2c7039 207 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
1da177e4 208 return 0;
5909ccaa
LT
209 return -EACCES;
210}
211
212/**
b74c79e9 213 * generic_permission - check for access rights on a Posix-like filesystem
5909ccaa
LT
214 * @inode: inode to check access rights for
215 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
39191628 216 * @flags: IPERM_FLAG_ flags.
5909ccaa
LT
217 *
218 * Used to check for read/write/execute permissions on a file.
219 * We use "fsuid" for this, letting us set arbitrary permissions
220 * for filesystem access without changing the "normal" uids which
b74c79e9
NP
221 * are used for other things.
222 *
223 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
224 * request cannot be satisfied (eg. requires blocking or too much complexity).
225 * It would then be called again in ref-walk mode.
5909ccaa 226 */
2830ba7f 227int generic_permission(struct inode *inode, int mask)
5909ccaa
LT
228{
229 int ret;
230
231 /*
232 * Do the basic POSIX ACL permission checks.
233 */
7e40145e 234 ret = acl_permission_check(inode, mask);
5909ccaa
LT
235 if (ret != -EACCES)
236 return ret;
1da177e4 237
d594e7ec
AV
238 if (S_ISDIR(inode->i_mode)) {
239 /* DACs are overridable for directories */
240 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
241 return 0;
242 if (!(mask & MAY_WRITE))
243 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
244 return 0;
245 return -EACCES;
246 }
1da177e4
LT
247 /*
248 * Read/write DACs are always overridable.
d594e7ec
AV
249 * Executable DACs are overridable when there is
250 * at least one exec bit set.
1da177e4 251 */
d594e7ec 252 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
e795b717 253 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
1da177e4
LT
254 return 0;
255
256 /*
257 * Searching includes executable on directories, else just read.
258 */
7ea66001 259 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
d594e7ec 260 if (mask == MAY_READ)
e795b717 261 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
1da177e4
LT
262 return 0;
263
264 return -EACCES;
265}
266
cb23beb5
CH
267/**
268 * inode_permission - check for access rights to a given inode
269 * @inode: inode to check permission on
270 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
271 *
272 * Used to check for read/write/execute permissions on an inode.
273 * We use "fsuid" for this, letting us set arbitrary permissions
274 * for filesystem access without changing the "normal" uids which
275 * are used for other things.
276 */
f419a2e3 277int inode_permission(struct inode *inode, int mask)
1da177e4 278{
e6305c43 279 int retval;
1da177e4
LT
280
281 if (mask & MAY_WRITE) {
22590e41 282 umode_t mode = inode->i_mode;
1da177e4
LT
283
284 /*
285 * Nobody gets write access to a read-only fs.
286 */
287 if (IS_RDONLY(inode) &&
288 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
289 return -EROFS;
290
291 /*
292 * Nobody gets write access to an immutable file.
293 */
294 if (IS_IMMUTABLE(inode))
295 return -EACCES;
296 }
297
acfa4380 298 if (inode->i_op->permission)
10556cb2 299 retval = inode->i_op->permission(inode, mask);
f696a365 300 else
2830ba7f 301 retval = generic_permission(inode, mask);
f696a365 302
1da177e4
LT
303 if (retval)
304 return retval;
305
08ce5f16
SH
306 retval = devcgroup_inode_permission(inode, mask);
307 if (retval)
308 return retval;
309
d09ca739 310 return security_inode_permission(inode, mask);
1da177e4
LT
311}
312
f4d6ff89
AV
313/**
314 * exec_permission - check for right to do lookups in a given directory
315 * @inode: inode to check permission on
eecdd358 316 * @mask: MAY_EXEC and possibly MAY_NOT_BLOCK flags.
f4d6ff89
AV
317 *
318 * Short-cut version of inode_permission(), for calling on directories
319 * during pathname resolution. Combines parts of inode_permission()
320 * and generic_permission(), and tests ONLY for MAY_EXEC permission.
321 *
322 * If appropriate, check DAC only. If not appropriate, or
323 * short-cut DAC fails, then call ->permission() to do more
324 * complete permission check.
325 */
eecdd358 326static inline int exec_permission(struct inode *inode, int mask)
f4d6ff89
AV
327{
328 int ret;
329 struct user_namespace *ns = inode_userns(inode);
330
331 if (inode->i_op->permission) {
10556cb2 332 ret = inode->i_op->permission(inode, mask);
f4d6ff89
AV
333 if (likely(!ret))
334 goto ok;
335 } else {
7e40145e 336 ret = acl_permission_check(inode, mask);
f4d6ff89
AV
337 if (likely(!ret))
338 goto ok;
339 if (ret != -EACCES)
340 return ret;
341 if (ns_capable(ns, CAP_DAC_OVERRIDE) ||
342 ns_capable(ns, CAP_DAC_READ_SEARCH))
343 goto ok;
344 }
345 return ret;
346ok:
eecdd358 347 return security_inode_permission(inode, mask);
f4d6ff89
AV
348}
349
5dd784d0
JB
350/**
351 * path_get - get a reference to a path
352 * @path: path to get the reference to
353 *
354 * Given a path increment the reference count to the dentry and the vfsmount.
355 */
356void path_get(struct path *path)
357{
358 mntget(path->mnt);
359 dget(path->dentry);
360}
361EXPORT_SYMBOL(path_get);
362
1d957f9b
JB
363/**
364 * path_put - put a reference to a path
365 * @path: path to put the reference to
366 *
367 * Given a path decrement the reference count to the dentry and the vfsmount.
368 */
369void path_put(struct path *path)
1da177e4 370{
1d957f9b
JB
371 dput(path->dentry);
372 mntput(path->mnt);
1da177e4 373}
1d957f9b 374EXPORT_SYMBOL(path_put);
1da177e4 375
19660af7 376/*
31e6b01f 377 * Path walking has 2 modes, rcu-walk and ref-walk (see
19660af7
AV
378 * Documentation/filesystems/path-lookup.txt). In situations when we can't
379 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
380 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
381 * mode. Refcounts are grabbed at the last known good point before rcu-walk
382 * got stuck, so ref-walk may continue from there. If this is not successful
383 * (eg. a seqcount has changed), then failure is returned and it's up to caller
384 * to restart the path walk from the beginning in ref-walk mode.
31e6b01f 385 */
31e6b01f
NP
386
387/**
19660af7
AV
388 * unlazy_walk - try to switch to ref-walk mode.
389 * @nd: nameidata pathwalk data
390 * @dentry: child of nd->path.dentry or NULL
39191628 391 * Returns: 0 on success, -ECHILD on failure
31e6b01f 392 *
19660af7
AV
393 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
394 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
395 * @nd or NULL. Must be called from rcu-walk context.
31e6b01f 396 */
19660af7 397static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
31e6b01f
NP
398{
399 struct fs_struct *fs = current->fs;
400 struct dentry *parent = nd->path.dentry;
5b6ca027 401 int want_root = 0;
31e6b01f
NP
402
403 BUG_ON(!(nd->flags & LOOKUP_RCU));
5b6ca027
AV
404 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
405 want_root = 1;
31e6b01f
NP
406 spin_lock(&fs->lock);
407 if (nd->root.mnt != fs->root.mnt ||
408 nd->root.dentry != fs->root.dentry)
409 goto err_root;
410 }
411 spin_lock(&parent->d_lock);
19660af7
AV
412 if (!dentry) {
413 if (!__d_rcu_to_refcount(parent, nd->seq))
414 goto err_parent;
415 BUG_ON(nd->inode != parent->d_inode);
416 } else {
94c0d4ec
AV
417 if (dentry->d_parent != parent)
418 goto err_parent;
19660af7
AV
419 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
420 if (!__d_rcu_to_refcount(dentry, nd->seq))
421 goto err_child;
422 /*
423 * If the sequence check on the child dentry passed, then
424 * the child has not been removed from its parent. This
425 * means the parent dentry must be valid and able to take
426 * a reference at this point.
427 */
428 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
429 BUG_ON(!parent->d_count);
430 parent->d_count++;
431 spin_unlock(&dentry->d_lock);
432 }
31e6b01f 433 spin_unlock(&parent->d_lock);
5b6ca027 434 if (want_root) {
31e6b01f
NP
435 path_get(&nd->root);
436 spin_unlock(&fs->lock);
437 }
438 mntget(nd->path.mnt);
439
440 rcu_read_unlock();
441 br_read_unlock(vfsmount_lock);
442 nd->flags &= ~LOOKUP_RCU;
443 return 0;
19660af7
AV
444
445err_child:
31e6b01f 446 spin_unlock(&dentry->d_lock);
19660af7 447err_parent:
31e6b01f
NP
448 spin_unlock(&parent->d_lock);
449err_root:
5b6ca027 450 if (want_root)
31e6b01f
NP
451 spin_unlock(&fs->lock);
452 return -ECHILD;
453}
454
834f2a4a
TM
455/**
456 * release_open_intent - free up open intent resources
457 * @nd: pointer to nameidata
458 */
459void release_open_intent(struct nameidata *nd)
460{
2dab5974
LT
461 struct file *file = nd->intent.open.file;
462
463 if (file && !IS_ERR(file)) {
464 if (file->f_path.dentry == NULL)
465 put_filp(file);
466 else
467 fput(file);
468 }
834f2a4a
TM
469}
470
f60aef7e 471static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
34286d66 472{
f60aef7e 473 return dentry->d_op->d_revalidate(dentry, nd);
34286d66
NP
474}
475
f5e1c1c1 476static struct dentry *
bcdc5e01
IK
477do_revalidate(struct dentry *dentry, struct nameidata *nd)
478{
f5e1c1c1 479 int status = d_revalidate(dentry, nd);
bcdc5e01
IK
480 if (unlikely(status <= 0)) {
481 /*
482 * The dentry failed validation.
483 * If d_revalidate returned 0 attempt to invalidate
484 * the dentry otherwise d_revalidate is asking us
485 * to return a fail status.
486 */
34286d66 487 if (status < 0) {
f5e1c1c1 488 dput(dentry);
34286d66 489 dentry = ERR_PTR(status);
f5e1c1c1
AV
490 } else if (!d_invalidate(dentry)) {
491 dput(dentry);
492 dentry = NULL;
bcdc5e01
IK
493 }
494 }
495 return dentry;
496}
497
9f1fafee
AV
498/**
499 * complete_walk - successful completion of path walk
500 * @nd: pointer nameidata
39159de2 501 *
9f1fafee
AV
502 * If we had been in RCU mode, drop out of it and legitimize nd->path.
503 * Revalidate the final result, unless we'd already done that during
504 * the path walk or the filesystem doesn't ask for it. Return 0 on
505 * success, -error on failure. In case of failure caller does not
506 * need to drop nd->path.
39159de2 507 */
9f1fafee 508static int complete_walk(struct nameidata *nd)
39159de2 509{
16c2cd71 510 struct dentry *dentry = nd->path.dentry;
39159de2 511 int status;
39159de2 512
9f1fafee
AV
513 if (nd->flags & LOOKUP_RCU) {
514 nd->flags &= ~LOOKUP_RCU;
515 if (!(nd->flags & LOOKUP_ROOT))
516 nd->root.mnt = NULL;
517 spin_lock(&dentry->d_lock);
518 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
519 spin_unlock(&dentry->d_lock);
520 rcu_read_unlock();
521 br_read_unlock(vfsmount_lock);
522 return -ECHILD;
523 }
524 BUG_ON(nd->inode != dentry->d_inode);
525 spin_unlock(&dentry->d_lock);
526 mntget(nd->path.mnt);
527 rcu_read_unlock();
528 br_read_unlock(vfsmount_lock);
529 }
530
16c2cd71
AV
531 if (likely(!(nd->flags & LOOKUP_JUMPED)))
532 return 0;
533
534 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
39159de2
JL
535 return 0;
536
16c2cd71
AV
537 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
538 return 0;
539
540 /* Note: we do not d_invalidate() */
34286d66 541 status = d_revalidate(dentry, nd);
39159de2
JL
542 if (status > 0)
543 return 0;
544
16c2cd71 545 if (!status)
39159de2 546 status = -ESTALE;
16c2cd71 547
9f1fafee 548 path_put(&nd->path);
39159de2
JL
549 return status;
550}
551
2a737871
AV
552static __always_inline void set_root(struct nameidata *nd)
553{
f7ad3c6b
MS
554 if (!nd->root.mnt)
555 get_fs_root(current->fs, &nd->root);
2a737871
AV
556}
557
6de88d72
AV
558static int link_path_walk(const char *, struct nameidata *);
559
31e6b01f
NP
560static __always_inline void set_root_rcu(struct nameidata *nd)
561{
562 if (!nd->root.mnt) {
563 struct fs_struct *fs = current->fs;
c28cc364
NP
564 unsigned seq;
565
566 do {
567 seq = read_seqcount_begin(&fs->seq);
568 nd->root = fs->root;
c1530019 569 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
c28cc364 570 } while (read_seqcount_retry(&fs->seq, seq));
31e6b01f
NP
571 }
572}
573
f1662356 574static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
1da177e4 575{
31e6b01f
NP
576 int ret;
577
1da177e4
LT
578 if (IS_ERR(link))
579 goto fail;
580
581 if (*link == '/') {
2a737871 582 set_root(nd);
1d957f9b 583 path_put(&nd->path);
2a737871
AV
584 nd->path = nd->root;
585 path_get(&nd->root);
16c2cd71 586 nd->flags |= LOOKUP_JUMPED;
1da177e4 587 }
31e6b01f 588 nd->inode = nd->path.dentry->d_inode;
b4091d5f 589
31e6b01f
NP
590 ret = link_path_walk(link, nd);
591 return ret;
1da177e4 592fail:
1d957f9b 593 path_put(&nd->path);
1da177e4
LT
594 return PTR_ERR(link);
595}
596
1d957f9b 597static void path_put_conditional(struct path *path, struct nameidata *nd)
051d3812
IK
598{
599 dput(path->dentry);
4ac91378 600 if (path->mnt != nd->path.mnt)
051d3812
IK
601 mntput(path->mnt);
602}
603
7b9337aa
NP
604static inline void path_to_nameidata(const struct path *path,
605 struct nameidata *nd)
051d3812 606{
31e6b01f
NP
607 if (!(nd->flags & LOOKUP_RCU)) {
608 dput(nd->path.dentry);
609 if (nd->path.mnt != path->mnt)
610 mntput(nd->path.mnt);
9a229683 611 }
31e6b01f 612 nd->path.mnt = path->mnt;
4ac91378 613 nd->path.dentry = path->dentry;
051d3812
IK
614}
615
574197e0
AV
616static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
617{
618 struct inode *inode = link->dentry->d_inode;
619 if (!IS_ERR(cookie) && inode->i_op->put_link)
620 inode->i_op->put_link(link->dentry, nd, cookie);
621 path_put(link);
622}
623
def4af30 624static __always_inline int
574197e0 625follow_link(struct path *link, struct nameidata *nd, void **p)
1da177e4
LT
626{
627 int error;
7b9337aa 628 struct dentry *dentry = link->dentry;
1da177e4 629
844a3917
AV
630 BUG_ON(nd->flags & LOOKUP_RCU);
631
0e794589
AV
632 if (link->mnt == nd->path.mnt)
633 mntget(link->mnt);
634
574197e0
AV
635 if (unlikely(current->total_link_count >= 40)) {
636 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
574197e0
AV
637 path_put(&nd->path);
638 return -ELOOP;
639 }
640 cond_resched();
641 current->total_link_count++;
642
7b9337aa 643 touch_atime(link->mnt, dentry);
1da177e4 644 nd_set_link(nd, NULL);
cd4e91d3 645
36f3b4f6
AV
646 error = security_inode_follow_link(link->dentry, nd);
647 if (error) {
648 *p = ERR_PTR(error); /* no ->put_link(), please */
649 path_put(&nd->path);
650 return error;
651 }
652
86acdca1 653 nd->last_type = LAST_BIND;
def4af30
AV
654 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
655 error = PTR_ERR(*p);
656 if (!IS_ERR(*p)) {
1da177e4 657 char *s = nd_get_link(nd);
cc314eef 658 error = 0;
1da177e4
LT
659 if (s)
660 error = __vfs_follow_link(nd, s);
bcda7652 661 else if (nd->last_type == LAST_BIND) {
16c2cd71 662 nd->flags |= LOOKUP_JUMPED;
b21041d0
AV
663 nd->inode = nd->path.dentry->d_inode;
664 if (nd->inode->i_op->follow_link) {
bcda7652
AV
665 /* stepped on a _really_ weird one */
666 path_put(&nd->path);
667 error = -ELOOP;
668 }
669 }
1da177e4 670 }
1da177e4
LT
671 return error;
672}
673
31e6b01f
NP
674static int follow_up_rcu(struct path *path)
675{
676 struct vfsmount *parent;
677 struct dentry *mountpoint;
678
679 parent = path->mnt->mnt_parent;
680 if (parent == path->mnt)
681 return 0;
682 mountpoint = path->mnt->mnt_mountpoint;
683 path->dentry = mountpoint;
684 path->mnt = parent;
685 return 1;
686}
687
bab77ebf 688int follow_up(struct path *path)
1da177e4
LT
689{
690 struct vfsmount *parent;
691 struct dentry *mountpoint;
99b7db7b
NP
692
693 br_read_lock(vfsmount_lock);
bab77ebf
AV
694 parent = path->mnt->mnt_parent;
695 if (parent == path->mnt) {
99b7db7b 696 br_read_unlock(vfsmount_lock);
1da177e4
LT
697 return 0;
698 }
699 mntget(parent);
bab77ebf 700 mountpoint = dget(path->mnt->mnt_mountpoint);
99b7db7b 701 br_read_unlock(vfsmount_lock);
bab77ebf
AV
702 dput(path->dentry);
703 path->dentry = mountpoint;
704 mntput(path->mnt);
705 path->mnt = parent;
1da177e4
LT
706 return 1;
707}
708
b5c84bf6 709/*
9875cf80
DH
710 * Perform an automount
711 * - return -EISDIR to tell follow_managed() to stop and return the path we
712 * were called with.
1da177e4 713 */
9875cf80
DH
714static int follow_automount(struct path *path, unsigned flags,
715 bool *need_mntput)
31e6b01f 716{
9875cf80 717 struct vfsmount *mnt;
ea5b778a 718 int err;
9875cf80
DH
719
720 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
721 return -EREMOTE;
722
6f45b656
DH
723 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
724 * and this is the terminal part of the path.
725 */
726 if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
727 return -EISDIR; /* we actually want to stop here */
728
9875cf80
DH
729 /* We want to mount if someone is trying to open/create a file of any
730 * type under the mountpoint, wants to traverse through the mountpoint
731 * or wants to open the mounted directory.
732 *
733 * We don't want to mount if someone's just doing a stat and they've
734 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
735 * appended a '/' to the name.
736 */
737 if (!(flags & LOOKUP_FOLLOW) &&
738 !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
739 LOOKUP_OPEN | LOOKUP_CREATE)))
740 return -EISDIR;
741
742 current->total_link_count++;
743 if (current->total_link_count >= 40)
744 return -ELOOP;
745
746 mnt = path->dentry->d_op->d_automount(path);
747 if (IS_ERR(mnt)) {
748 /*
749 * The filesystem is allowed to return -EISDIR here to indicate
750 * it doesn't want to automount. For instance, autofs would do
751 * this so that its userspace daemon can mount on this dentry.
752 *
753 * However, we can only permit this if it's a terminal point in
754 * the path being looked up; if it wasn't then the remainder of
755 * the path is inaccessible and we should say so.
756 */
757 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
758 return -EREMOTE;
759 return PTR_ERR(mnt);
31e6b01f 760 }
ea5b778a 761
9875cf80
DH
762 if (!mnt) /* mount collision */
763 return 0;
31e6b01f 764
8aef1884
AV
765 if (!*need_mntput) {
766 /* lock_mount() may release path->mnt on error */
767 mntget(path->mnt);
768 *need_mntput = true;
769 }
19a167af 770 err = finish_automount(mnt, path);
9875cf80 771
ea5b778a
DH
772 switch (err) {
773 case -EBUSY:
774 /* Someone else made a mount here whilst we were busy */
19a167af 775 return 0;
ea5b778a 776 case 0:
8aef1884 777 path_put(path);
ea5b778a
DH
778 path->mnt = mnt;
779 path->dentry = dget(mnt->mnt_root);
ea5b778a 780 return 0;
19a167af
AV
781 default:
782 return err;
ea5b778a 783 }
19a167af 784
463ffb2e
AV
785}
786
9875cf80
DH
787/*
788 * Handle a dentry that is managed in some way.
cc53ce53 789 * - Flagged for transit management (autofs)
9875cf80
DH
790 * - Flagged as mountpoint
791 * - Flagged as automount point
792 *
793 * This may only be called in refwalk mode.
794 *
795 * Serialization is taken care of in namespace.c
796 */
797static int follow_managed(struct path *path, unsigned flags)
1da177e4 798{
8aef1884 799 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
9875cf80
DH
800 unsigned managed;
801 bool need_mntput = false;
8aef1884 802 int ret = 0;
9875cf80
DH
803
804 /* Given that we're not holding a lock here, we retain the value in a
805 * local variable for each dentry as we look at it so that we don't see
806 * the components of that value change under us */
807 while (managed = ACCESS_ONCE(path->dentry->d_flags),
808 managed &= DCACHE_MANAGED_DENTRY,
809 unlikely(managed != 0)) {
cc53ce53
DH
810 /* Allow the filesystem to manage the transit without i_mutex
811 * being held. */
812 if (managed & DCACHE_MANAGE_TRANSIT) {
813 BUG_ON(!path->dentry->d_op);
814 BUG_ON(!path->dentry->d_op->d_manage);
1aed3e42 815 ret = path->dentry->d_op->d_manage(path->dentry, false);
cc53ce53 816 if (ret < 0)
8aef1884 817 break;
cc53ce53
DH
818 }
819
9875cf80
DH
820 /* Transit to a mounted filesystem. */
821 if (managed & DCACHE_MOUNTED) {
822 struct vfsmount *mounted = lookup_mnt(path);
823 if (mounted) {
824 dput(path->dentry);
825 if (need_mntput)
826 mntput(path->mnt);
827 path->mnt = mounted;
828 path->dentry = dget(mounted->mnt_root);
829 need_mntput = true;
830 continue;
831 }
832
833 /* Something is mounted on this dentry in another
834 * namespace and/or whatever was mounted there in this
835 * namespace got unmounted before we managed to get the
836 * vfsmount_lock */
837 }
838
839 /* Handle an automount point */
840 if (managed & DCACHE_NEED_AUTOMOUNT) {
841 ret = follow_automount(path, flags, &need_mntput);
842 if (ret < 0)
8aef1884 843 break;
9875cf80
DH
844 continue;
845 }
846
847 /* We didn't change the current path point */
848 break;
1da177e4 849 }
8aef1884
AV
850
851 if (need_mntput && path->mnt == mnt)
852 mntput(path->mnt);
853 if (ret == -EISDIR)
854 ret = 0;
855 return ret;
1da177e4
LT
856}
857
cc53ce53 858int follow_down_one(struct path *path)
1da177e4
LT
859{
860 struct vfsmount *mounted;
861
1c755af4 862 mounted = lookup_mnt(path);
1da177e4 863 if (mounted) {
9393bd07
AV
864 dput(path->dentry);
865 mntput(path->mnt);
866 path->mnt = mounted;
867 path->dentry = dget(mounted->mnt_root);
1da177e4
LT
868 return 1;
869 }
870 return 0;
871}
872
62a7375e
IK
873static inline bool managed_dentry_might_block(struct dentry *dentry)
874{
875 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
876 dentry->d_op->d_manage(dentry, true) < 0);
877}
878
9875cf80 879/*
287548e4
AV
880 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
881 * we meet a managed dentry that would need blocking.
9875cf80
DH
882 */
883static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
287548e4 884 struct inode **inode)
9875cf80 885{
62a7375e 886 for (;;) {
9875cf80 887 struct vfsmount *mounted;
62a7375e
IK
888 /*
889 * Don't forget we might have a non-mountpoint managed dentry
890 * that wants to block transit.
891 */
287548e4 892 if (unlikely(managed_dentry_might_block(path->dentry)))
ab90911f 893 return false;
62a7375e
IK
894
895 if (!d_mountpoint(path->dentry))
896 break;
897
9875cf80
DH
898 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
899 if (!mounted)
900 break;
901 path->mnt = mounted;
902 path->dentry = mounted->mnt_root;
903 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
59430262
LT
904 /*
905 * Update the inode too. We don't need to re-check the
906 * dentry sequence number here after this d_inode read,
907 * because a mount-point is always pinned.
908 */
909 *inode = path->dentry->d_inode;
9875cf80 910 }
9875cf80
DH
911 return true;
912}
913
dea39376 914static void follow_mount_rcu(struct nameidata *nd)
287548e4 915{
dea39376 916 while (d_mountpoint(nd->path.dentry)) {
287548e4 917 struct vfsmount *mounted;
dea39376 918 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
287548e4
AV
919 if (!mounted)
920 break;
dea39376
AV
921 nd->path.mnt = mounted;
922 nd->path.dentry = mounted->mnt_root;
923 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
287548e4
AV
924 }
925}
926
31e6b01f
NP
927static int follow_dotdot_rcu(struct nameidata *nd)
928{
31e6b01f
NP
929 set_root_rcu(nd);
930
9875cf80 931 while (1) {
31e6b01f
NP
932 if (nd->path.dentry == nd->root.dentry &&
933 nd->path.mnt == nd->root.mnt) {
934 break;
935 }
936 if (nd->path.dentry != nd->path.mnt->mnt_root) {
937 struct dentry *old = nd->path.dentry;
938 struct dentry *parent = old->d_parent;
939 unsigned seq;
940
941 seq = read_seqcount_begin(&parent->d_seq);
942 if (read_seqcount_retry(&old->d_seq, nd->seq))
ef7562d5 943 goto failed;
31e6b01f
NP
944 nd->path.dentry = parent;
945 nd->seq = seq;
946 break;
947 }
948 if (!follow_up_rcu(&nd->path))
949 break;
950 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
31e6b01f 951 }
dea39376
AV
952 follow_mount_rcu(nd);
953 nd->inode = nd->path.dentry->d_inode;
31e6b01f 954 return 0;
ef7562d5
AV
955
956failed:
957 nd->flags &= ~LOOKUP_RCU;
5b6ca027
AV
958 if (!(nd->flags & LOOKUP_ROOT))
959 nd->root.mnt = NULL;
ef7562d5
AV
960 rcu_read_unlock();
961 br_read_unlock(vfsmount_lock);
962 return -ECHILD;
31e6b01f
NP
963}
964
cc53ce53
DH
965/*
966 * Follow down to the covering mount currently visible to userspace. At each
967 * point, the filesystem owning that dentry may be queried as to whether the
968 * caller is permitted to proceed or not.
cc53ce53 969 */
7cc90cc3 970int follow_down(struct path *path)
cc53ce53
DH
971{
972 unsigned managed;
973 int ret;
974
975 while (managed = ACCESS_ONCE(path->dentry->d_flags),
976 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
977 /* Allow the filesystem to manage the transit without i_mutex
978 * being held.
979 *
980 * We indicate to the filesystem if someone is trying to mount
981 * something here. This gives autofs the chance to deny anyone
982 * other than its daemon the right to mount on its
983 * superstructure.
984 *
985 * The filesystem may sleep at this point.
986 */
987 if (managed & DCACHE_MANAGE_TRANSIT) {
988 BUG_ON(!path->dentry->d_op);
989 BUG_ON(!path->dentry->d_op->d_manage);
ab90911f 990 ret = path->dentry->d_op->d_manage(
1aed3e42 991 path->dentry, false);
cc53ce53
DH
992 if (ret < 0)
993 return ret == -EISDIR ? 0 : ret;
994 }
995
996 /* Transit to a mounted filesystem. */
997 if (managed & DCACHE_MOUNTED) {
998 struct vfsmount *mounted = lookup_mnt(path);
999 if (!mounted)
1000 break;
1001 dput(path->dentry);
1002 mntput(path->mnt);
1003 path->mnt = mounted;
1004 path->dentry = dget(mounted->mnt_root);
1005 continue;
1006 }
1007
1008 /* Don't handle automount points here */
1009 break;
1010 }
1011 return 0;
1012}
1013
9875cf80
DH
1014/*
1015 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1016 */
1017static void follow_mount(struct path *path)
1018{
1019 while (d_mountpoint(path->dentry)) {
1020 struct vfsmount *mounted = lookup_mnt(path);
1021 if (!mounted)
1022 break;
1023 dput(path->dentry);
1024 mntput(path->mnt);
1025 path->mnt = mounted;
1026 path->dentry = dget(mounted->mnt_root);
1027 }
1028}
1029
31e6b01f 1030static void follow_dotdot(struct nameidata *nd)
1da177e4 1031{
2a737871 1032 set_root(nd);
e518ddb7 1033
1da177e4 1034 while(1) {
4ac91378 1035 struct dentry *old = nd->path.dentry;
1da177e4 1036
2a737871
AV
1037 if (nd->path.dentry == nd->root.dentry &&
1038 nd->path.mnt == nd->root.mnt) {
1da177e4
LT
1039 break;
1040 }
4ac91378 1041 if (nd->path.dentry != nd->path.mnt->mnt_root) {
3088dd70
AV
1042 /* rare case of legitimate dget_parent()... */
1043 nd->path.dentry = dget_parent(nd->path.dentry);
1da177e4
LT
1044 dput(old);
1045 break;
1046 }
3088dd70 1047 if (!follow_up(&nd->path))
1da177e4 1048 break;
1da177e4 1049 }
79ed0226 1050 follow_mount(&nd->path);
31e6b01f 1051 nd->inode = nd->path.dentry->d_inode;
1da177e4
LT
1052}
1053
baa03890
NP
1054/*
1055 * Allocate a dentry with name and parent, and perform a parent
1056 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1057 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1058 * have verified that no child exists while under i_mutex.
1059 */
1060static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1061 struct qstr *name, struct nameidata *nd)
1062{
1063 struct inode *inode = parent->d_inode;
1064 struct dentry *dentry;
1065 struct dentry *old;
1066
1067 /* Don't create child dentry for a dead directory. */
1068 if (unlikely(IS_DEADDIR(inode)))
1069 return ERR_PTR(-ENOENT);
1070
1071 dentry = d_alloc(parent, name);
1072 if (unlikely(!dentry))
1073 return ERR_PTR(-ENOMEM);
1074
1075 old = inode->i_op->lookup(inode, dentry, nd);
1076 if (unlikely(old)) {
1077 dput(dentry);
1078 dentry = old;
1079 }
1080 return dentry;
1081}
1082
44396f4b
JB
1083/*
1084 * We already have a dentry, but require a lookup to be performed on the parent
1085 * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
1086 * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
1087 * child exists while under i_mutex.
1088 */
1089static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
1090 struct nameidata *nd)
1091{
1092 struct inode *inode = parent->d_inode;
1093 struct dentry *old;
1094
1095 /* Don't create child dentry for a dead directory. */
1096 if (unlikely(IS_DEADDIR(inode)))
1097 return ERR_PTR(-ENOENT);
1098
1099 old = inode->i_op->lookup(inode, dentry, nd);
1100 if (unlikely(old)) {
1101 dput(dentry);
1102 dentry = old;
1103 }
1104 return dentry;
1105}
1106
1da177e4
LT
1107/*
1108 * It's more convoluted than I'd like it to be, but... it's still fairly
1109 * small and for now I'd prefer to have fast path as straight as possible.
1110 * It _is_ time-critical.
1111 */
1112static int do_lookup(struct nameidata *nd, struct qstr *name,
31e6b01f 1113 struct path *path, struct inode **inode)
1da177e4 1114{
4ac91378 1115 struct vfsmount *mnt = nd->path.mnt;
31e6b01f 1116 struct dentry *dentry, *parent = nd->path.dentry;
5a18fff2
AV
1117 int need_reval = 1;
1118 int status = 1;
9875cf80
DH
1119 int err;
1120
b04f784e
NP
1121 /*
1122 * Rename seqlock is not required here because in the off chance
1123 * of a false negative due to a concurrent rename, we're going to
1124 * do the non-racy lookup, below.
1125 */
31e6b01f
NP
1126 if (nd->flags & LOOKUP_RCU) {
1127 unsigned seq;
31e6b01f
NP
1128 *inode = nd->inode;
1129 dentry = __d_lookup_rcu(parent, name, &seq, inode);
5a18fff2
AV
1130 if (!dentry)
1131 goto unlazy;
1132
31e6b01f
NP
1133 /* Memory barrier in read_seqcount_begin of child is enough */
1134 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1135 return -ECHILD;
31e6b01f 1136 nd->seq = seq;
5a18fff2 1137
24643087 1138 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
5a18fff2
AV
1139 status = d_revalidate(dentry, nd);
1140 if (unlikely(status <= 0)) {
1141 if (status != -ECHILD)
1142 need_reval = 0;
1143 goto unlazy;
1144 }
24643087 1145 }
44396f4b
JB
1146 if (unlikely(d_need_lookup(dentry)))
1147 goto unlazy;
31e6b01f
NP
1148 path->mnt = mnt;
1149 path->dentry = dentry;
d6e9bd25
AV
1150 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1151 goto unlazy;
1152 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1153 goto unlazy;
1154 return 0;
5a18fff2 1155unlazy:
19660af7
AV
1156 if (unlazy_walk(nd, dentry))
1157 return -ECHILD;
5a18fff2
AV
1158 } else {
1159 dentry = __d_lookup(parent, name);
9875cf80 1160 }
5a18fff2 1161
44396f4b
JB
1162 if (dentry && unlikely(d_need_lookup(dentry))) {
1163 dput(dentry);
1164 dentry = NULL;
1165 }
5a18fff2
AV
1166retry:
1167 if (unlikely(!dentry)) {
1168 struct inode *dir = parent->d_inode;
1169 BUG_ON(nd->inode != dir);
1170
1171 mutex_lock(&dir->i_mutex);
1172 dentry = d_lookup(parent, name);
1173 if (likely(!dentry)) {
1174 dentry = d_alloc_and_lookup(parent, name, nd);
1175 if (IS_ERR(dentry)) {
1176 mutex_unlock(&dir->i_mutex);
1177 return PTR_ERR(dentry);
1178 }
1179 /* known good */
1180 need_reval = 0;
1181 status = 1;
44396f4b
JB
1182 } else if (unlikely(d_need_lookup(dentry))) {
1183 dentry = d_inode_lookup(parent, dentry, nd);
1184 if (IS_ERR(dentry)) {
1185 mutex_unlock(&dir->i_mutex);
1186 return PTR_ERR(dentry);
1187 }
1188 /* known good */
1189 need_reval = 0;
1190 status = 1;
5a18fff2
AV
1191 }
1192 mutex_unlock(&dir->i_mutex);
1193 }
1194 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1195 status = d_revalidate(dentry, nd);
1196 if (unlikely(status <= 0)) {
1197 if (status < 0) {
1198 dput(dentry);
1199 return status;
1200 }
1201 if (!d_invalidate(dentry)) {
1202 dput(dentry);
1203 dentry = NULL;
1204 need_reval = 1;
1205 goto retry;
1206 }
24643087 1207 }
5a18fff2 1208
9875cf80
DH
1209 path->mnt = mnt;
1210 path->dentry = dentry;
1211 err = follow_managed(path, nd->flags);
89312214
IK
1212 if (unlikely(err < 0)) {
1213 path_put_conditional(path, nd);
9875cf80 1214 return err;
89312214 1215 }
9875cf80 1216 *inode = path->dentry->d_inode;
1da177e4 1217 return 0;
1da177e4
LT
1218}
1219
52094c8a
AV
1220static inline int may_lookup(struct nameidata *nd)
1221{
1222 if (nd->flags & LOOKUP_RCU) {
eecdd358 1223 int err = exec_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
52094c8a
AV
1224 if (err != -ECHILD)
1225 return err;
19660af7 1226 if (unlazy_walk(nd, NULL))
52094c8a
AV
1227 return -ECHILD;
1228 }
eecdd358 1229 return exec_permission(nd->inode, MAY_EXEC);
52094c8a
AV
1230}
1231
9856fa1b
AV
1232static inline int handle_dots(struct nameidata *nd, int type)
1233{
1234 if (type == LAST_DOTDOT) {
1235 if (nd->flags & LOOKUP_RCU) {
1236 if (follow_dotdot_rcu(nd))
1237 return -ECHILD;
1238 } else
1239 follow_dotdot(nd);
1240 }
1241 return 0;
1242}
1243
951361f9
AV
1244static void terminate_walk(struct nameidata *nd)
1245{
1246 if (!(nd->flags & LOOKUP_RCU)) {
1247 path_put(&nd->path);
1248 } else {
1249 nd->flags &= ~LOOKUP_RCU;
5b6ca027
AV
1250 if (!(nd->flags & LOOKUP_ROOT))
1251 nd->root.mnt = NULL;
951361f9
AV
1252 rcu_read_unlock();
1253 br_read_unlock(vfsmount_lock);
1254 }
1255}
1256
ce57dfc1
AV
1257static inline int walk_component(struct nameidata *nd, struct path *path,
1258 struct qstr *name, int type, int follow)
1259{
1260 struct inode *inode;
1261 int err;
1262 /*
1263 * "." and ".." are special - ".." especially so because it has
1264 * to be able to know about the current root directory and
1265 * parent relationships.
1266 */
1267 if (unlikely(type != LAST_NORM))
1268 return handle_dots(nd, type);
1269 err = do_lookup(nd, name, path, &inode);
1270 if (unlikely(err)) {
1271 terminate_walk(nd);
1272 return err;
1273 }
1274 if (!inode) {
1275 path_to_nameidata(path, nd);
1276 terminate_walk(nd);
1277 return -ENOENT;
1278 }
1279 if (unlikely(inode->i_op->follow_link) && follow) {
19660af7
AV
1280 if (nd->flags & LOOKUP_RCU) {
1281 if (unlikely(unlazy_walk(nd, path->dentry))) {
1282 terminate_walk(nd);
1283 return -ECHILD;
1284 }
1285 }
ce57dfc1
AV
1286 BUG_ON(inode != path->dentry->d_inode);
1287 return 1;
1288 }
1289 path_to_nameidata(path, nd);
1290 nd->inode = inode;
1291 return 0;
1292}
1293
b356379a
AV
1294/*
1295 * This limits recursive symlink follows to 8, while
1296 * limiting consecutive symlinks to 40.
1297 *
1298 * Without that kind of total limit, nasty chains of consecutive
1299 * symlinks can cause almost arbitrarily long lookups.
1300 */
1301static inline int nested_symlink(struct path *path, struct nameidata *nd)
1302{
1303 int res;
1304
b356379a
AV
1305 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1306 path_put_conditional(path, nd);
1307 path_put(&nd->path);
1308 return -ELOOP;
1309 }
1a4022f8 1310 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
b356379a
AV
1311
1312 nd->depth++;
1313 current->link_count++;
1314
1315 do {
1316 struct path link = *path;
1317 void *cookie;
574197e0
AV
1318
1319 res = follow_link(&link, nd, &cookie);
b356379a
AV
1320 if (!res)
1321 res = walk_component(nd, path, &nd->last,
1322 nd->last_type, LOOKUP_FOLLOW);
574197e0 1323 put_link(nd, &link, cookie);
b356379a
AV
1324 } while (res > 0);
1325
1326 current->link_count--;
1327 nd->depth--;
1328 return res;
1329}
1330
1da177e4
LT
1331/*
1332 * Name resolution.
ea3834d9
PM
1333 * This is the basic name resolution function, turning a pathname into
1334 * the final dentry. We expect 'base' to be positive and a directory.
1da177e4 1335 *
ea3834d9
PM
1336 * Returns 0 and nd will have valid dentry and mnt on success.
1337 * Returns error and drops reference to input namei data on failure.
1da177e4 1338 */
6de88d72 1339static int link_path_walk(const char *name, struct nameidata *nd)
1da177e4
LT
1340{
1341 struct path next;
1da177e4
LT
1342 int err;
1343 unsigned int lookup_flags = nd->flags;
1344
1345 while (*name=='/')
1346 name++;
1347 if (!*name)
086e183a 1348 return 0;
1da177e4 1349
1da177e4
LT
1350 /* At this point we know we have a real path component. */
1351 for(;;) {
1352 unsigned long hash;
1353 struct qstr this;
1354 unsigned int c;
fe479a58 1355 int type;
1da177e4 1356
cdce5d6b 1357 nd->flags |= LOOKUP_CONTINUE;
52094c8a
AV
1358
1359 err = may_lookup(nd);
1da177e4
LT
1360 if (err)
1361 break;
1362
1363 this.name = name;
1364 c = *(const unsigned char *)name;
1365
1366 hash = init_name_hash();
1367 do {
1368 name++;
1369 hash = partial_name_hash(c, hash);
1370 c = *(const unsigned char *)name;
1371 } while (c && (c != '/'));
1372 this.len = name - (const char *) this.name;
1373 this.hash = end_name_hash(hash);
1374
fe479a58
AV
1375 type = LAST_NORM;
1376 if (this.name[0] == '.') switch (this.len) {
1377 case 2:
16c2cd71 1378 if (this.name[1] == '.') {
fe479a58 1379 type = LAST_DOTDOT;
16c2cd71
AV
1380 nd->flags |= LOOKUP_JUMPED;
1381 }
fe479a58
AV
1382 break;
1383 case 1:
1384 type = LAST_DOT;
1385 }
5a202bcd
AV
1386 if (likely(type == LAST_NORM)) {
1387 struct dentry *parent = nd->path.dentry;
16c2cd71 1388 nd->flags &= ~LOOKUP_JUMPED;
5a202bcd
AV
1389 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1390 err = parent->d_op->d_hash(parent, nd->inode,
1391 &this);
1392 if (err < 0)
1393 break;
1394 }
1395 }
fe479a58 1396
1da177e4
LT
1397 /* remove trailing slashes? */
1398 if (!c)
1399 goto last_component;
1400 while (*++name == '/');
1401 if (!*name)
b356379a 1402 goto last_component;
1da177e4 1403
ce57dfc1
AV
1404 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1405 if (err < 0)
1406 return err;
1da177e4 1407
ce57dfc1 1408 if (err) {
b356379a 1409 err = nested_symlink(&next, nd);
1da177e4 1410 if (err)
a7472bab 1411 return err;
31e6b01f 1412 }
1da177e4 1413 err = -ENOTDIR;
31e6b01f 1414 if (!nd->inode->i_op->lookup)
1da177e4
LT
1415 break;
1416 continue;
1417 /* here ends the main loop */
1418
1da177e4 1419last_component:
f55eab82
TM
1420 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1421 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
b356379a
AV
1422 nd->last = this;
1423 nd->last_type = type;
086e183a 1424 return 0;
1da177e4 1425 }
951361f9 1426 terminate_walk(nd);
1da177e4
LT
1427 return err;
1428}
1429
70e9b357
AV
1430static int path_init(int dfd, const char *name, unsigned int flags,
1431 struct nameidata *nd, struct file **fp)
31e6b01f
NP
1432{
1433 int retval = 0;
1434 int fput_needed;
1435 struct file *file;
1436
1437 nd->last_type = LAST_ROOT; /* if there are only slashes... */
16c2cd71 1438 nd->flags = flags | LOOKUP_JUMPED;
31e6b01f 1439 nd->depth = 0;
5b6ca027
AV
1440 if (flags & LOOKUP_ROOT) {
1441 struct inode *inode = nd->root.dentry->d_inode;
73d049a4
AV
1442 if (*name) {
1443 if (!inode->i_op->lookup)
1444 return -ENOTDIR;
1445 retval = inode_permission(inode, MAY_EXEC);
1446 if (retval)
1447 return retval;
1448 }
5b6ca027
AV
1449 nd->path = nd->root;
1450 nd->inode = inode;
1451 if (flags & LOOKUP_RCU) {
1452 br_read_lock(vfsmount_lock);
1453 rcu_read_lock();
1454 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1455 } else {
1456 path_get(&nd->path);
1457 }
1458 return 0;
1459 }
1460
31e6b01f 1461 nd->root.mnt = NULL;
31e6b01f
NP
1462
1463 if (*name=='/') {
e41f7d4e
AV
1464 if (flags & LOOKUP_RCU) {
1465 br_read_lock(vfsmount_lock);
1466 rcu_read_lock();
1467 set_root_rcu(nd);
1468 } else {
1469 set_root(nd);
1470 path_get(&nd->root);
1471 }
1472 nd->path = nd->root;
31e6b01f 1473 } else if (dfd == AT_FDCWD) {
e41f7d4e
AV
1474 if (flags & LOOKUP_RCU) {
1475 struct fs_struct *fs = current->fs;
1476 unsigned seq;
31e6b01f 1477
e41f7d4e
AV
1478 br_read_lock(vfsmount_lock);
1479 rcu_read_lock();
c28cc364 1480
e41f7d4e
AV
1481 do {
1482 seq = read_seqcount_begin(&fs->seq);
1483 nd->path = fs->pwd;
1484 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1485 } while (read_seqcount_retry(&fs->seq, seq));
1486 } else {
1487 get_fs_pwd(current->fs, &nd->path);
1488 }
31e6b01f
NP
1489 } else {
1490 struct dentry *dentry;
1491
1abf0c71 1492 file = fget_raw_light(dfd, &fput_needed);
31e6b01f
NP
1493 retval = -EBADF;
1494 if (!file)
1495 goto out_fail;
1496
1497 dentry = file->f_path.dentry;
1498
f52e0c11
AV
1499 if (*name) {
1500 retval = -ENOTDIR;
1501 if (!S_ISDIR(dentry->d_inode->i_mode))
1502 goto fput_fail;
31e6b01f 1503
eecdd358 1504 retval = exec_permission(dentry->d_inode, MAY_EXEC);
f52e0c11
AV
1505 if (retval)
1506 goto fput_fail;
1507 }
31e6b01f
NP
1508
1509 nd->path = file->f_path;
e41f7d4e
AV
1510 if (flags & LOOKUP_RCU) {
1511 if (fput_needed)
70e9b357 1512 *fp = file;
e41f7d4e
AV
1513 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1514 br_read_lock(vfsmount_lock);
1515 rcu_read_lock();
1516 } else {
1517 path_get(&file->f_path);
1518 fput_light(file, fput_needed);
1519 }
31e6b01f 1520 }
31e6b01f 1521
31e6b01f 1522 nd->inode = nd->path.dentry->d_inode;
9b4a9b14 1523 return 0;
2dfdd266 1524
9b4a9b14
AV
1525fput_fail:
1526 fput_light(file, fput_needed);
1527out_fail:
1528 return retval;
1529}
1530
bd92d7fe
AV
1531static inline int lookup_last(struct nameidata *nd, struct path *path)
1532{
1533 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1534 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1535
1536 nd->flags &= ~LOOKUP_PARENT;
1537 return walk_component(nd, path, &nd->last, nd->last_type,
1538 nd->flags & LOOKUP_FOLLOW);
1539}
1540
9b4a9b14 1541/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
ee0827cd 1542static int path_lookupat(int dfd, const char *name,
9b4a9b14
AV
1543 unsigned int flags, struct nameidata *nd)
1544{
70e9b357 1545 struct file *base = NULL;
bd92d7fe
AV
1546 struct path path;
1547 int err;
31e6b01f
NP
1548
1549 /*
1550 * Path walking is largely split up into 2 different synchronisation
1551 * schemes, rcu-walk and ref-walk (explained in
1552 * Documentation/filesystems/path-lookup.txt). These share much of the
1553 * path walk code, but some things particularly setup, cleanup, and
1554 * following mounts are sufficiently divergent that functions are
1555 * duplicated. Typically there is a function foo(), and its RCU
1556 * analogue, foo_rcu().
1557 *
1558 * -ECHILD is the error number of choice (just to avoid clashes) that
1559 * is returned if some aspect of an rcu-walk fails. Such an error must
1560 * be handled by restarting a traditional ref-walk (which will always
1561 * be able to complete).
1562 */
bd92d7fe 1563 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
ee0827cd 1564
bd92d7fe
AV
1565 if (unlikely(err))
1566 return err;
ee0827cd
AV
1567
1568 current->total_link_count = 0;
bd92d7fe
AV
1569 err = link_path_walk(name, nd);
1570
1571 if (!err && !(flags & LOOKUP_PARENT)) {
bd92d7fe
AV
1572 err = lookup_last(nd, &path);
1573 while (err > 0) {
1574 void *cookie;
1575 struct path link = path;
bd92d7fe 1576 nd->flags |= LOOKUP_PARENT;
574197e0 1577 err = follow_link(&link, nd, &cookie);
bd92d7fe
AV
1578 if (!err)
1579 err = lookup_last(nd, &path);
574197e0 1580 put_link(nd, &link, cookie);
bd92d7fe
AV
1581 }
1582 }
ee0827cd 1583
9f1fafee
AV
1584 if (!err)
1585 err = complete_walk(nd);
bd92d7fe
AV
1586
1587 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1588 if (!nd->inode->i_op->lookup) {
1589 path_put(&nd->path);
bd23a539 1590 err = -ENOTDIR;
bd92d7fe
AV
1591 }
1592 }
16c2cd71 1593
70e9b357
AV
1594 if (base)
1595 fput(base);
ee0827cd 1596
5b6ca027 1597 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
2a737871
AV
1598 path_put(&nd->root);
1599 nd->root.mnt = NULL;
1600 }
bd92d7fe 1601 return err;
ee0827cd 1602}
31e6b01f 1603
ee0827cd
AV
1604static int do_path_lookup(int dfd, const char *name,
1605 unsigned int flags, struct nameidata *nd)
1606{
1607 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1608 if (unlikely(retval == -ECHILD))
1609 retval = path_lookupat(dfd, name, flags, nd);
1610 if (unlikely(retval == -ESTALE))
1611 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
31e6b01f
NP
1612
1613 if (likely(!retval)) {
1614 if (unlikely(!audit_dummy_context())) {
1615 if (nd->path.dentry && nd->inode)
1616 audit_inode(name, nd->path.dentry);
1617 }
1618 }
170aa3d0 1619 return retval;
1da177e4
LT
1620}
1621
c9c6cac0 1622int kern_path_parent(const char *name, struct nameidata *nd)
5590ff0d 1623{
c9c6cac0 1624 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
5590ff0d
UD
1625}
1626
d1811465
AV
1627int kern_path(const char *name, unsigned int flags, struct path *path)
1628{
1629 struct nameidata nd;
1630 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1631 if (!res)
1632 *path = nd.path;
1633 return res;
1634}
1635
16f18200
JJS
1636/**
1637 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1638 * @dentry: pointer to dentry of the base directory
1639 * @mnt: pointer to vfs mount of the base directory
1640 * @name: pointer to file name
1641 * @flags: lookup flags
1642 * @nd: pointer to nameidata
1643 */
1644int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1645 const char *name, unsigned int flags,
1646 struct nameidata *nd)
1647{
5b6ca027
AV
1648 nd->root.dentry = dentry;
1649 nd->root.mnt = mnt;
1650 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1651 return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
16f18200
JJS
1652}
1653
eead1911
CH
1654static struct dentry *__lookup_hash(struct qstr *name,
1655 struct dentry *base, struct nameidata *nd)
1da177e4 1656{
81fca444 1657 struct inode *inode = base->d_inode;
057f6c01 1658 struct dentry *dentry;
1da177e4
LT
1659 int err;
1660
eecdd358 1661 err = exec_permission(inode, MAY_EXEC);
81fca444
CH
1662 if (err)
1663 return ERR_PTR(err);
1da177e4 1664
b04f784e
NP
1665 /*
1666 * Don't bother with __d_lookup: callers are for creat as
1667 * well as unlink, so a lot of the time it would cost
1668 * a double lookup.
6e6b1bd1 1669 */
b04f784e 1670 dentry = d_lookup(base, name);
6e6b1bd1 1671
44396f4b
JB
1672 if (dentry && d_need_lookup(dentry)) {
1673 /*
1674 * __lookup_hash is called with the parent dir's i_mutex already
1675 * held, so we are good to go here.
1676 */
1677 dentry = d_inode_lookup(base, dentry, nd);
1678 if (IS_ERR(dentry))
1679 return dentry;
1680 }
1681
fb045adb 1682 if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
6e6b1bd1
AV
1683 dentry = do_revalidate(dentry, nd);
1684
baa03890
NP
1685 if (!dentry)
1686 dentry = d_alloc_and_lookup(base, name, nd);
5a202bcd 1687
1da177e4
LT
1688 return dentry;
1689}
1690
057f6c01
JM
1691/*
1692 * Restricted form of lookup. Doesn't follow links, single-component only,
1693 * needs parent already locked. Doesn't follow mounts.
1694 * SMP-safe.
1695 */
eead1911 1696static struct dentry *lookup_hash(struct nameidata *nd)
057f6c01 1697{
4ac91378 1698 return __lookup_hash(&nd->last, nd->path.dentry, nd);
1da177e4
LT
1699}
1700
eead1911 1701/**
a6b91919 1702 * lookup_one_len - filesystem helper to lookup single pathname component
eead1911
CH
1703 * @name: pathname component to lookup
1704 * @base: base directory to lookup from
1705 * @len: maximum length @len should be interpreted to
1706 *
a6b91919
RD
1707 * Note that this routine is purely a helper for filesystem usage and should
1708 * not be called by generic code. Also note that by using this function the
eead1911
CH
1709 * nameidata argument is passed to the filesystem methods and a filesystem
1710 * using this helper needs to be prepared for that.
1711 */
057f6c01
JM
1712struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1713{
057f6c01 1714 struct qstr this;
6a96ba54
AV
1715 unsigned long hash;
1716 unsigned int c;
057f6c01 1717
2f9092e1
DW
1718 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1719
6a96ba54
AV
1720 this.name = name;
1721 this.len = len;
1722 if (!len)
1723 return ERR_PTR(-EACCES);
1724
1725 hash = init_name_hash();
1726 while (len--) {
1727 c = *(const unsigned char *)name++;
1728 if (c == '/' || c == '\0')
1729 return ERR_PTR(-EACCES);
1730 hash = partial_name_hash(c, hash);
1731 }
1732 this.hash = end_name_hash(hash);
5a202bcd
AV
1733 /*
1734 * See if the low-level filesystem might want
1735 * to use its own hash..
1736 */
1737 if (base->d_flags & DCACHE_OP_HASH) {
1738 int err = base->d_op->d_hash(base, base->d_inode, &this);
1739 if (err < 0)
1740 return ERR_PTR(err);
1741 }
eead1911 1742
49705b77 1743 return __lookup_hash(&this, base, NULL);
057f6c01
JM
1744}
1745
2d8f3038
AV
1746int user_path_at(int dfd, const char __user *name, unsigned flags,
1747 struct path *path)
1da177e4 1748{
2d8f3038 1749 struct nameidata nd;
f52e0c11 1750 char *tmp = getname_flags(name, flags);
1da177e4 1751 int err = PTR_ERR(tmp);
1da177e4 1752 if (!IS_ERR(tmp)) {
2d8f3038
AV
1753
1754 BUG_ON(flags & LOOKUP_PARENT);
1755
1756 err = do_path_lookup(dfd, tmp, flags, &nd);
1da177e4 1757 putname(tmp);
2d8f3038
AV
1758 if (!err)
1759 *path = nd.path;
1da177e4
LT
1760 }
1761 return err;
1762}
1763
2ad94ae6
AV
1764static int user_path_parent(int dfd, const char __user *path,
1765 struct nameidata *nd, char **name)
1766{
1767 char *s = getname(path);
1768 int error;
1769
1770 if (IS_ERR(s))
1771 return PTR_ERR(s);
1772
1773 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
1774 if (error)
1775 putname(s);
1776 else
1777 *name = s;
1778
1779 return error;
1780}
1781
1da177e4
LT
1782/*
1783 * It's inline, so penalty for filesystems that don't use sticky bit is
1784 * minimal.
1785 */
1786static inline int check_sticky(struct inode *dir, struct inode *inode)
1787{
da9592ed
DH
1788 uid_t fsuid = current_fsuid();
1789
1da177e4
LT
1790 if (!(dir->i_mode & S_ISVTX))
1791 return 0;
e795b717
SH
1792 if (current_user_ns() != inode_userns(inode))
1793 goto other_userns;
da9592ed 1794 if (inode->i_uid == fsuid)
1da177e4 1795 return 0;
da9592ed 1796 if (dir->i_uid == fsuid)
1da177e4 1797 return 0;
e795b717
SH
1798
1799other_userns:
1800 return !ns_capable(inode_userns(inode), CAP_FOWNER);
1da177e4
LT
1801}
1802
1803/*
1804 * Check whether we can remove a link victim from directory dir, check
1805 * whether the type of victim is right.
1806 * 1. We can't do it if dir is read-only (done in permission())
1807 * 2. We should have write and exec permissions on dir
1808 * 3. We can't remove anything from append-only dir
1809 * 4. We can't do anything with immutable dir (done in permission())
1810 * 5. If the sticky bit on dir is set we should either
1811 * a. be owner of dir, or
1812 * b. be owner of victim, or
1813 * c. have CAP_FOWNER capability
1814 * 6. If the victim is append-only or immutable we can't do antyhing with
1815 * links pointing to it.
1816 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1817 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1818 * 9. We can't remove a root or mountpoint.
1819 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1820 * nfs_async_unlink().
1821 */
858119e1 1822static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1da177e4
LT
1823{
1824 int error;
1825
1826 if (!victim->d_inode)
1827 return -ENOENT;
1828
1829 BUG_ON(victim->d_parent->d_inode != dir);
cccc6bba 1830 audit_inode_child(victim, dir);
1da177e4 1831
f419a2e3 1832 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
1833 if (error)
1834 return error;
1835 if (IS_APPEND(dir))
1836 return -EPERM;
1837 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
f9454548 1838 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
1da177e4
LT
1839 return -EPERM;
1840 if (isdir) {
1841 if (!S_ISDIR(victim->d_inode->i_mode))
1842 return -ENOTDIR;
1843 if (IS_ROOT(victim))
1844 return -EBUSY;
1845 } else if (S_ISDIR(victim->d_inode->i_mode))
1846 return -EISDIR;
1847 if (IS_DEADDIR(dir))
1848 return -ENOENT;
1849 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1850 return -EBUSY;
1851 return 0;
1852}
1853
1854/* Check whether we can create an object with dentry child in directory
1855 * dir.
1856 * 1. We can't do it if child already exists (open has special treatment for
1857 * this case, but since we are inlined it's OK)
1858 * 2. We can't do it if dir is read-only (done in permission())
1859 * 3. We should have write and exec permissions on dir
1860 * 4. We can't do it if dir is immutable (done in permission())
1861 */
a95164d9 1862static inline int may_create(struct inode *dir, struct dentry *child)
1da177e4
LT
1863{
1864 if (child->d_inode)
1865 return -EEXIST;
1866 if (IS_DEADDIR(dir))
1867 return -ENOENT;
f419a2e3 1868 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
1da177e4
LT
1869}
1870
1da177e4
LT
1871/*
1872 * p1 and p2 should be directories on the same fs.
1873 */
1874struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1875{
1876 struct dentry *p;
1877
1878 if (p1 == p2) {
f2eace23 1879 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1da177e4
LT
1880 return NULL;
1881 }
1882
a11f3a05 1883 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4 1884
e2761a11
OH
1885 p = d_ancestor(p2, p1);
1886 if (p) {
1887 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1888 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
1889 return p;
1da177e4
LT
1890 }
1891
e2761a11
OH
1892 p = d_ancestor(p1, p2);
1893 if (p) {
1894 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1895 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1896 return p;
1da177e4
LT
1897 }
1898
f2eace23
IM
1899 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1900 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
1da177e4
LT
1901 return NULL;
1902}
1903
1904void unlock_rename(struct dentry *p1, struct dentry *p2)
1905{
1b1dcc1b 1906 mutex_unlock(&p1->d_inode->i_mutex);
1da177e4 1907 if (p1 != p2) {
1b1dcc1b 1908 mutex_unlock(&p2->d_inode->i_mutex);
a11f3a05 1909 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
1da177e4
LT
1910 }
1911}
1912
1913int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1914 struct nameidata *nd)
1915{
a95164d9 1916 int error = may_create(dir, dentry);
1da177e4
LT
1917
1918 if (error)
1919 return error;
1920
acfa4380 1921 if (!dir->i_op->create)
1da177e4
LT
1922 return -EACCES; /* shouldn't it be ENOSYS? */
1923 mode &= S_IALLUGO;
1924 mode |= S_IFREG;
1925 error = security_inode_create(dir, dentry, mode);
1926 if (error)
1927 return error;
1da177e4 1928 error = dir->i_op->create(dir, dentry, mode, nd);
a74574aa 1929 if (!error)
f38aa942 1930 fsnotify_create(dir, dentry);
1da177e4
LT
1931 return error;
1932}
1933
73d049a4 1934static int may_open(struct path *path, int acc_mode, int flag)
1da177e4 1935{
3fb64190 1936 struct dentry *dentry = path->dentry;
1da177e4
LT
1937 struct inode *inode = dentry->d_inode;
1938 int error;
1939
bcda7652
AV
1940 /* O_PATH? */
1941 if (!acc_mode)
1942 return 0;
1943
1da177e4
LT
1944 if (!inode)
1945 return -ENOENT;
1946
c8fe8f30
CH
1947 switch (inode->i_mode & S_IFMT) {
1948 case S_IFLNK:
1da177e4 1949 return -ELOOP;
c8fe8f30
CH
1950 case S_IFDIR:
1951 if (acc_mode & MAY_WRITE)
1952 return -EISDIR;
1953 break;
1954 case S_IFBLK:
1955 case S_IFCHR:
3fb64190 1956 if (path->mnt->mnt_flags & MNT_NODEV)
1da177e4 1957 return -EACCES;
c8fe8f30
CH
1958 /*FALLTHRU*/
1959 case S_IFIFO:
1960 case S_IFSOCK:
1da177e4 1961 flag &= ~O_TRUNC;
c8fe8f30 1962 break;
4a3fd211 1963 }
b41572e9 1964
3fb64190 1965 error = inode_permission(inode, acc_mode);
b41572e9
DH
1966 if (error)
1967 return error;
6146f0d5 1968
1da177e4
LT
1969 /*
1970 * An append-only file must be opened in append mode for writing.
1971 */
1972 if (IS_APPEND(inode)) {
8737c930 1973 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
7715b521 1974 return -EPERM;
1da177e4 1975 if (flag & O_TRUNC)
7715b521 1976 return -EPERM;
1da177e4
LT
1977 }
1978
1979 /* O_NOATIME can only be set by the owner or superuser */
2e149670 1980 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
7715b521 1981 return -EPERM;
1da177e4
LT
1982
1983 /*
1984 * Ensure there are no outstanding leases on the file.
1985 */
b65a9cfc 1986 return break_lease(inode, flag);
7715b521 1987}
1da177e4 1988
e1181ee6 1989static int handle_truncate(struct file *filp)
7715b521 1990{
e1181ee6 1991 struct path *path = &filp->f_path;
7715b521
AV
1992 struct inode *inode = path->dentry->d_inode;
1993 int error = get_write_access(inode);
1994 if (error)
1995 return error;
1996 /*
1997 * Refuse to truncate files with mandatory locks held on them.
1998 */
1999 error = locks_verify_locked(inode);
2000 if (!error)
ea0d3ab2 2001 error = security_path_truncate(path);
7715b521
AV
2002 if (!error) {
2003 error = do_truncate(path->dentry, 0,
2004 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
e1181ee6 2005 filp);
7715b521
AV
2006 }
2007 put_write_access(inode);
acd0c935 2008 return error;
1da177e4
LT
2009}
2010
d57999e1
DH
2011/*
2012 * Note that while the flag value (low two bits) for sys_open means:
2013 * 00 - read-only
2014 * 01 - write-only
2015 * 10 - read-write
2016 * 11 - special
2017 * it is changed into
2018 * 00 - no permissions needed
2019 * 01 - read-permission
2020 * 10 - write-permission
2021 * 11 - read-write
2022 * for the internal routines (ie open_namei()/follow_link() etc)
2023 * This is more logical, and also allows the 00 "no perm needed"
2024 * to be used for symlinks (where the permissions are checked
2025 * later).
2026 *
2027*/
2028static inline int open_to_namei_flags(int flag)
2029{
2030 if ((flag+1) & O_ACCMODE)
2031 flag++;
2032 return flag;
2033}
2034
31e6b01f 2035/*
fe2d35ff 2036 * Handle the last step of open()
31e6b01f 2037 */
fb1cc555 2038static struct file *do_last(struct nameidata *nd, struct path *path,
c3e380b0 2039 const struct open_flags *op, const char *pathname)
fb1cc555 2040{
a1e28038 2041 struct dentry *dir = nd->path.dentry;
6c0d46c4 2042 struct dentry *dentry;
ca344a89 2043 int open_flag = op->open_flag;
6c0d46c4 2044 int will_truncate = open_flag & O_TRUNC;
ca344a89 2045 int want_write = 0;
bcda7652 2046 int acc_mode = op->acc_mode;
fb1cc555 2047 struct file *filp;
16c2cd71 2048 int error;
1f36f774 2049
c3e380b0
AV
2050 nd->flags &= ~LOOKUP_PARENT;
2051 nd->flags |= op->intent;
2052
1f36f774
AV
2053 switch (nd->last_type) {
2054 case LAST_DOTDOT:
176306f5 2055 case LAST_DOT:
fe2d35ff
AV
2056 error = handle_dots(nd, nd->last_type);
2057 if (error)
2058 return ERR_PTR(error);
1f36f774 2059 /* fallthrough */
1f36f774 2060 case LAST_ROOT:
9f1fafee 2061 error = complete_walk(nd);
16c2cd71 2062 if (error)
9f1fafee 2063 return ERR_PTR(error);
fe2d35ff 2064 audit_inode(pathname, nd->path.dentry);
ca344a89 2065 if (open_flag & O_CREAT) {
fe2d35ff
AV
2066 error = -EISDIR;
2067 goto exit;
2068 }
2069 goto ok;
1f36f774 2070 case LAST_BIND:
9f1fafee 2071 error = complete_walk(nd);
16c2cd71 2072 if (error)
9f1fafee 2073 return ERR_PTR(error);
1f36f774 2074 audit_inode(pathname, dir);
67ee3ad2 2075 goto ok;
1f36f774 2076 }
67ee3ad2 2077
ca344a89 2078 if (!(open_flag & O_CREAT)) {
bcda7652 2079 int symlink_ok = 0;
fe2d35ff
AV
2080 if (nd->last.name[nd->last.len])
2081 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
bcda7652
AV
2082 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2083 symlink_ok = 1;
fe2d35ff 2084 /* we _can_ be in RCU mode here */
ce57dfc1
AV
2085 error = walk_component(nd, path, &nd->last, LAST_NORM,
2086 !symlink_ok);
2087 if (error < 0)
fe2d35ff 2088 return ERR_PTR(error);
ce57dfc1 2089 if (error) /* symlink */
fe2d35ff 2090 return NULL;
fe2d35ff 2091 /* sayonara */
9f1fafee
AV
2092 error = complete_walk(nd);
2093 if (error)
2094 return ERR_PTR(-ECHILD);
fe2d35ff
AV
2095
2096 error = -ENOTDIR;
2097 if (nd->flags & LOOKUP_DIRECTORY) {
ce57dfc1 2098 if (!nd->inode->i_op->lookup)
fe2d35ff
AV
2099 goto exit;
2100 }
2101 audit_inode(pathname, nd->path.dentry);
2102 goto ok;
2103 }
2104
2105 /* create side of things */
9f1fafee
AV
2106 error = complete_walk(nd);
2107 if (error)
2108 return ERR_PTR(error);
fe2d35ff
AV
2109
2110 audit_inode(pathname, dir);
16c2cd71 2111 error = -EISDIR;
1f36f774 2112 /* trailing slashes? */
31e6b01f
NP
2113 if (nd->last.name[nd->last.len])
2114 goto exit;
a2c36b45 2115
a1e28038
AV
2116 mutex_lock(&dir->d_inode->i_mutex);
2117
6c0d46c4
AV
2118 dentry = lookup_hash(nd);
2119 error = PTR_ERR(dentry);
2120 if (IS_ERR(dentry)) {
fb1cc555
AV
2121 mutex_unlock(&dir->d_inode->i_mutex);
2122 goto exit;
2123 }
2124
6c0d46c4
AV
2125 path->dentry = dentry;
2126 path->mnt = nd->path.mnt;
2127
fb1cc555 2128 /* Negative dentry, just create the file */
6c0d46c4
AV
2129 if (!dentry->d_inode) {
2130 int mode = op->mode;
2131 if (!IS_POSIXACL(dir->d_inode))
2132 mode &= ~current_umask();
fb1cc555
AV
2133 /*
2134 * This write is needed to ensure that a
6c0d46c4 2135 * rw->ro transition does not occur between
fb1cc555
AV
2136 * the time when the file is created and when
2137 * a permanent write count is taken through
2138 * the 'struct file' in nameidata_to_filp().
2139 */
2140 error = mnt_want_write(nd->path.mnt);
2141 if (error)
2142 goto exit_mutex_unlock;
ca344a89 2143 want_write = 1;
9b44f1b3 2144 /* Don't check for write permission, don't truncate */
ca344a89 2145 open_flag &= ~O_TRUNC;
6c0d46c4 2146 will_truncate = 0;
bcda7652 2147 acc_mode = MAY_OPEN;
6c0d46c4
AV
2148 error = security_path_mknod(&nd->path, dentry, mode, 0);
2149 if (error)
2150 goto exit_mutex_unlock;
2151 error = vfs_create(dir->d_inode, dentry, mode, nd);
2152 if (error)
2153 goto exit_mutex_unlock;
2154 mutex_unlock(&dir->d_inode->i_mutex);
2155 dput(nd->path.dentry);
2156 nd->path.dentry = dentry;
ca344a89 2157 goto common;
fb1cc555
AV
2158 }
2159
2160 /*
2161 * It already exists.
2162 */
2163 mutex_unlock(&dir->d_inode->i_mutex);
2164 audit_inode(pathname, path->dentry);
2165
2166 error = -EEXIST;
ca344a89 2167 if (open_flag & O_EXCL)
fb1cc555
AV
2168 goto exit_dput;
2169
9875cf80
DH
2170 error = follow_managed(path, nd->flags);
2171 if (error < 0)
2172 goto exit_dput;
fb1cc555
AV
2173
2174 error = -ENOENT;
2175 if (!path->dentry->d_inode)
2176 goto exit_dput;
9e67f361
AV
2177
2178 if (path->dentry->d_inode->i_op->follow_link)
fb1cc555 2179 return NULL;
fb1cc555
AV
2180
2181 path_to_nameidata(path, nd);
31e6b01f 2182 nd->inode = path->dentry->d_inode;
fb1cc555 2183 error = -EISDIR;
31e6b01f 2184 if (S_ISDIR(nd->inode->i_mode))
fb1cc555 2185 goto exit;
67ee3ad2 2186ok:
6c0d46c4
AV
2187 if (!S_ISREG(nd->inode->i_mode))
2188 will_truncate = 0;
2189
0f9d1a10
AV
2190 if (will_truncate) {
2191 error = mnt_want_write(nd->path.mnt);
2192 if (error)
2193 goto exit;
ca344a89 2194 want_write = 1;
0f9d1a10 2195 }
ca344a89 2196common:
bcda7652 2197 error = may_open(&nd->path, acc_mode, open_flag);
ca344a89 2198 if (error)
0f9d1a10 2199 goto exit;
0f9d1a10
AV
2200 filp = nameidata_to_filp(nd);
2201 if (!IS_ERR(filp)) {
2202 error = ima_file_check(filp, op->acc_mode);
2203 if (error) {
2204 fput(filp);
2205 filp = ERR_PTR(error);
2206 }
2207 }
2208 if (!IS_ERR(filp)) {
2209 if (will_truncate) {
2210 error = handle_truncate(filp);
2211 if (error) {
2212 fput(filp);
2213 filp = ERR_PTR(error);
2214 }
2215 }
2216 }
ca344a89
AV
2217out:
2218 if (want_write)
0f9d1a10
AV
2219 mnt_drop_write(nd->path.mnt);
2220 path_put(&nd->path);
fb1cc555
AV
2221 return filp;
2222
2223exit_mutex_unlock:
2224 mutex_unlock(&dir->d_inode->i_mutex);
2225exit_dput:
2226 path_put_conditional(path, nd);
2227exit:
ca344a89
AV
2228 filp = ERR_PTR(error);
2229 goto out;
fb1cc555
AV
2230}
2231
13aab428 2232static struct file *path_openat(int dfd, const char *pathname,
73d049a4 2233 struct nameidata *nd, const struct open_flags *op, int flags)
1da177e4 2234{
fe2d35ff 2235 struct file *base = NULL;
4a3fd211 2236 struct file *filp;
9850c056 2237 struct path path;
13aab428 2238 int error;
31e6b01f
NP
2239
2240 filp = get_empty_filp();
2241 if (!filp)
2242 return ERR_PTR(-ENFILE);
2243
47c805dc 2244 filp->f_flags = op->open_flag;
73d049a4
AV
2245 nd->intent.open.file = filp;
2246 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2247 nd->intent.open.create_mode = op->mode;
31e6b01f 2248
73d049a4 2249 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
31e6b01f 2250 if (unlikely(error))
13aab428 2251 goto out_filp;
31e6b01f 2252
fe2d35ff 2253 current->total_link_count = 0;
73d049a4 2254 error = link_path_walk(pathname, nd);
31e6b01f
NP
2255 if (unlikely(error))
2256 goto out_filp;
1da177e4 2257
73d049a4 2258 filp = do_last(nd, &path, op, pathname);
806b681c 2259 while (unlikely(!filp)) { /* trailing symlink */
7b9337aa 2260 struct path link = path;
def4af30 2261 void *cookie;
574197e0 2262 if (!(nd->flags & LOOKUP_FOLLOW)) {
73d049a4
AV
2263 path_put_conditional(&path, nd);
2264 path_put(&nd->path);
40b39136
AV
2265 filp = ERR_PTR(-ELOOP);
2266 break;
2267 }
73d049a4
AV
2268 nd->flags |= LOOKUP_PARENT;
2269 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
574197e0 2270 error = follow_link(&link, nd, &cookie);
c3e380b0 2271 if (unlikely(error))
f1afe9ef 2272 filp = ERR_PTR(error);
c3e380b0 2273 else
73d049a4 2274 filp = do_last(nd, &path, op, pathname);
574197e0 2275 put_link(nd, &link, cookie);
806b681c 2276 }
10fa8e62 2277out:
73d049a4
AV
2278 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2279 path_put(&nd->root);
fe2d35ff
AV
2280 if (base)
2281 fput(base);
73d049a4 2282 release_open_intent(nd);
10fa8e62 2283 return filp;
1da177e4 2284
31e6b01f 2285out_filp:
806b681c 2286 filp = ERR_PTR(error);
10fa8e62 2287 goto out;
1da177e4
LT
2288}
2289
13aab428
AV
2290struct file *do_filp_open(int dfd, const char *pathname,
2291 const struct open_flags *op, int flags)
2292{
73d049a4 2293 struct nameidata nd;
13aab428
AV
2294 struct file *filp;
2295
73d049a4 2296 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
13aab428 2297 if (unlikely(filp == ERR_PTR(-ECHILD)))
73d049a4 2298 filp = path_openat(dfd, pathname, &nd, op, flags);
13aab428 2299 if (unlikely(filp == ERR_PTR(-ESTALE)))
73d049a4 2300 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
13aab428
AV
2301 return filp;
2302}
2303
73d049a4
AV
2304struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2305 const char *name, const struct open_flags *op, int flags)
2306{
2307 struct nameidata nd;
2308 struct file *file;
2309
2310 nd.root.mnt = mnt;
2311 nd.root.dentry = dentry;
2312
2313 flags |= LOOKUP_ROOT;
2314
bcda7652 2315 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
73d049a4
AV
2316 return ERR_PTR(-ELOOP);
2317
2318 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2319 if (unlikely(file == ERR_PTR(-ECHILD)))
2320 file = path_openat(-1, name, &nd, op, flags);
2321 if (unlikely(file == ERR_PTR(-ESTALE)))
2322 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2323 return file;
2324}
2325
1da177e4
LT
2326/**
2327 * lookup_create - lookup a dentry, creating it if it doesn't exist
2328 * @nd: nameidata info
2329 * @is_dir: directory flag
2330 *
2331 * Simple function to lookup and return a dentry and create it
2332 * if it doesn't exist. Is SMP-safe.
c663e5d8 2333 *
4ac91378 2334 * Returns with nd->path.dentry->d_inode->i_mutex locked.
1da177e4
LT
2335 */
2336struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2337{
c663e5d8 2338 struct dentry *dentry = ERR_PTR(-EEXIST);
1da177e4 2339
4ac91378 2340 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
c663e5d8
CH
2341 /*
2342 * Yucky last component or no last component at all?
2343 * (foo/., foo/.., /////)
2344 */
1da177e4
LT
2345 if (nd->last_type != LAST_NORM)
2346 goto fail;
2347 nd->flags &= ~LOOKUP_PARENT;
3516586a 2348 nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
a634904a 2349 nd->intent.open.flags = O_EXCL;
c663e5d8
CH
2350
2351 /*
2352 * Do the final lookup.
2353 */
49705b77 2354 dentry = lookup_hash(nd);
1da177e4
LT
2355 if (IS_ERR(dentry))
2356 goto fail;
c663e5d8 2357
e9baf6e5
AV
2358 if (dentry->d_inode)
2359 goto eexist;
c663e5d8
CH
2360 /*
2361 * Special case - lookup gave negative, but... we had foo/bar/
2362 * From the vfs_mknod() POV we just have a negative dentry -
2363 * all is fine. Let's be bastards - you had / on the end, you've
2364 * been asking for (non-existent) directory. -ENOENT for you.
2365 */
e9baf6e5
AV
2366 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2367 dput(dentry);
2368 dentry = ERR_PTR(-ENOENT);
2369 }
1da177e4 2370 return dentry;
e9baf6e5 2371eexist:
1da177e4 2372 dput(dentry);
e9baf6e5 2373 dentry = ERR_PTR(-EEXIST);
1da177e4
LT
2374fail:
2375 return dentry;
2376}
f81a0bff 2377EXPORT_SYMBOL_GPL(lookup_create);
1da177e4
LT
2378
2379int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2380{
a95164d9 2381 int error = may_create(dir, dentry);
1da177e4
LT
2382
2383 if (error)
2384 return error;
2385
e795b717
SH
2386 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2387 !ns_capable(inode_userns(dir), CAP_MKNOD))
1da177e4
LT
2388 return -EPERM;
2389
acfa4380 2390 if (!dir->i_op->mknod)
1da177e4
LT
2391 return -EPERM;
2392
08ce5f16
SH
2393 error = devcgroup_inode_mknod(mode, dev);
2394 if (error)
2395 return error;
2396
1da177e4
LT
2397 error = security_inode_mknod(dir, dentry, mode, dev);
2398 if (error)
2399 return error;
2400
1da177e4 2401 error = dir->i_op->mknod(dir, dentry, mode, dev);
a74574aa 2402 if (!error)
f38aa942 2403 fsnotify_create(dir, dentry);
1da177e4
LT
2404 return error;
2405}
2406
463c3197
DH
2407static int may_mknod(mode_t mode)
2408{
2409 switch (mode & S_IFMT) {
2410 case S_IFREG:
2411 case S_IFCHR:
2412 case S_IFBLK:
2413 case S_IFIFO:
2414 case S_IFSOCK:
2415 case 0: /* zero mode translates to S_IFREG */
2416 return 0;
2417 case S_IFDIR:
2418 return -EPERM;
2419 default:
2420 return -EINVAL;
2421 }
2422}
2423
2e4d0924
HC
2424SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
2425 unsigned, dev)
1da177e4 2426{
2ad94ae6
AV
2427 int error;
2428 char *tmp;
2429 struct dentry *dentry;
1da177e4
LT
2430 struct nameidata nd;
2431
2432 if (S_ISDIR(mode))
2433 return -EPERM;
1da177e4 2434
2ad94ae6 2435 error = user_path_parent(dfd, filename, &nd, &tmp);
1da177e4 2436 if (error)
2ad94ae6
AV
2437 return error;
2438
1da177e4 2439 dentry = lookup_create(&nd, 0);
463c3197
DH
2440 if (IS_ERR(dentry)) {
2441 error = PTR_ERR(dentry);
2442 goto out_unlock;
2443 }
4ac91378 2444 if (!IS_POSIXACL(nd.path.dentry->d_inode))
ce3b0f8d 2445 mode &= ~current_umask();
463c3197
DH
2446 error = may_mknod(mode);
2447 if (error)
2448 goto out_dput;
2449 error = mnt_want_write(nd.path.mnt);
2450 if (error)
2451 goto out_dput;
be6d3e56
KT
2452 error = security_path_mknod(&nd.path, dentry, mode, dev);
2453 if (error)
2454 goto out_drop_write;
463c3197 2455 switch (mode & S_IFMT) {
1da177e4 2456 case 0: case S_IFREG:
4ac91378 2457 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
1da177e4
LT
2458 break;
2459 case S_IFCHR: case S_IFBLK:
4ac91378 2460 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
1da177e4
LT
2461 new_decode_dev(dev));
2462 break;
2463 case S_IFIFO: case S_IFSOCK:
4ac91378 2464 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
1da177e4 2465 break;
1da177e4 2466 }
be6d3e56 2467out_drop_write:
463c3197
DH
2468 mnt_drop_write(nd.path.mnt);
2469out_dput:
2470 dput(dentry);
2471out_unlock:
4ac91378 2472 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1d957f9b 2473 path_put(&nd.path);
1da177e4
LT
2474 putname(tmp);
2475
2476 return error;
2477}
2478
3480b257 2479SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
5590ff0d
UD
2480{
2481 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2482}
2483
1da177e4
LT
2484int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2485{
a95164d9 2486 int error = may_create(dir, dentry);
1da177e4
LT
2487
2488 if (error)
2489 return error;
2490
acfa4380 2491 if (!dir->i_op->mkdir)
1da177e4
LT
2492 return -EPERM;
2493
2494 mode &= (S_IRWXUGO|S_ISVTX);
2495 error = security_inode_mkdir(dir, dentry, mode);
2496 if (error)
2497 return error;
2498
1da177e4 2499 error = dir->i_op->mkdir(dir, dentry, mode);
a74574aa 2500 if (!error)
f38aa942 2501 fsnotify_mkdir(dir, dentry);
1da177e4
LT
2502 return error;
2503}
2504
2e4d0924 2505SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
1da177e4
LT
2506{
2507 int error = 0;
2508 char * tmp;
6902d925
DH
2509 struct dentry *dentry;
2510 struct nameidata nd;
1da177e4 2511
2ad94ae6
AV
2512 error = user_path_parent(dfd, pathname, &nd, &tmp);
2513 if (error)
6902d925 2514 goto out_err;
1da177e4 2515
6902d925
DH
2516 dentry = lookup_create(&nd, 1);
2517 error = PTR_ERR(dentry);
2518 if (IS_ERR(dentry))
2519 goto out_unlock;
1da177e4 2520
4ac91378 2521 if (!IS_POSIXACL(nd.path.dentry->d_inode))
ce3b0f8d 2522 mode &= ~current_umask();
463c3197
DH
2523 error = mnt_want_write(nd.path.mnt);
2524 if (error)
2525 goto out_dput;
be6d3e56
KT
2526 error = security_path_mkdir(&nd.path, dentry, mode);
2527 if (error)
2528 goto out_drop_write;
4ac91378 2529 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
be6d3e56 2530out_drop_write:
463c3197
DH
2531 mnt_drop_write(nd.path.mnt);
2532out_dput:
6902d925
DH
2533 dput(dentry);
2534out_unlock:
4ac91378 2535 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1d957f9b 2536 path_put(&nd.path);
6902d925
DH
2537 putname(tmp);
2538out_err:
1da177e4
LT
2539 return error;
2540}
2541
3cdad428 2542SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
5590ff0d
UD
2543{
2544 return sys_mkdirat(AT_FDCWD, pathname, mode);
2545}
2546
1da177e4 2547/*
a71905f0
SW
2548 * The dentry_unhash() helper will try to drop the dentry early: we
2549 * should have a usage count of 2 if we're the only user of this
2550 * dentry, and if that is true (possibly after pruning the dcache),
2551 * then we drop the dentry now.
1da177e4
LT
2552 *
2553 * A low-level filesystem can, if it choses, legally
2554 * do a
2555 *
2556 * if (!d_unhashed(dentry))
2557 * return -EBUSY;
2558 *
2559 * if it cannot handle the case of removing a directory
2560 * that is still in use by something else..
2561 */
2562void dentry_unhash(struct dentry *dentry)
2563{
dc168427 2564 shrink_dcache_parent(dentry);
1da177e4 2565 spin_lock(&dentry->d_lock);
64252c75 2566 if (dentry->d_count == 1)
1da177e4
LT
2567 __d_drop(dentry);
2568 spin_unlock(&dentry->d_lock);
1da177e4
LT
2569}
2570
2571int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2572{
2573 int error = may_delete(dir, dentry, 1);
2574
2575 if (error)
2576 return error;
2577
acfa4380 2578 if (!dir->i_op->rmdir)
1da177e4
LT
2579 return -EPERM;
2580
1b1dcc1b 2581 mutex_lock(&dentry->d_inode->i_mutex);
912dbc15
SW
2582
2583 error = -EBUSY;
1da177e4 2584 if (d_mountpoint(dentry))
912dbc15
SW
2585 goto out;
2586
2587 error = security_inode_rmdir(dir, dentry);
2588 if (error)
2589 goto out;
2590
3cebde24 2591 shrink_dcache_parent(dentry);
912dbc15
SW
2592 error = dir->i_op->rmdir(dir, dentry);
2593 if (error)
2594 goto out;
2595
2596 dentry->d_inode->i_flags |= S_DEAD;
2597 dont_mount(dentry);
2598
2599out:
1b1dcc1b 2600 mutex_unlock(&dentry->d_inode->i_mutex);
912dbc15 2601 if (!error)
1da177e4 2602 d_delete(dentry);
1da177e4
LT
2603 return error;
2604}
2605
5590ff0d 2606static long do_rmdir(int dfd, const char __user *pathname)
1da177e4
LT
2607{
2608 int error = 0;
2609 char * name;
2610 struct dentry *dentry;
2611 struct nameidata nd;
2612
2ad94ae6 2613 error = user_path_parent(dfd, pathname, &nd, &name);
1da177e4 2614 if (error)
2ad94ae6 2615 return error;
1da177e4
LT
2616
2617 switch(nd.last_type) {
0612d9fb
OH
2618 case LAST_DOTDOT:
2619 error = -ENOTEMPTY;
2620 goto exit1;
2621 case LAST_DOT:
2622 error = -EINVAL;
2623 goto exit1;
2624 case LAST_ROOT:
2625 error = -EBUSY;
2626 goto exit1;
1da177e4 2627 }
0612d9fb
OH
2628
2629 nd.flags &= ~LOOKUP_PARENT;
2630
4ac91378 2631 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
49705b77 2632 dentry = lookup_hash(&nd);
1da177e4 2633 error = PTR_ERR(dentry);
6902d925
DH
2634 if (IS_ERR(dentry))
2635 goto exit2;
e6bc45d6
TT
2636 if (!dentry->d_inode) {
2637 error = -ENOENT;
2638 goto exit3;
2639 }
0622753b
DH
2640 error = mnt_want_write(nd.path.mnt);
2641 if (error)
2642 goto exit3;
be6d3e56
KT
2643 error = security_path_rmdir(&nd.path, dentry);
2644 if (error)
2645 goto exit4;
4ac91378 2646 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
be6d3e56 2647exit4:
0622753b
DH
2648 mnt_drop_write(nd.path.mnt);
2649exit3:
6902d925
DH
2650 dput(dentry);
2651exit2:
4ac91378 2652 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1da177e4 2653exit1:
1d957f9b 2654 path_put(&nd.path);
1da177e4
LT
2655 putname(name);
2656 return error;
2657}
2658
3cdad428 2659SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
5590ff0d
UD
2660{
2661 return do_rmdir(AT_FDCWD, pathname);
2662}
2663
1da177e4
LT
2664int vfs_unlink(struct inode *dir, struct dentry *dentry)
2665{
2666 int error = may_delete(dir, dentry, 0);
2667
2668 if (error)
2669 return error;
2670
acfa4380 2671 if (!dir->i_op->unlink)
1da177e4
LT
2672 return -EPERM;
2673
1b1dcc1b 2674 mutex_lock(&dentry->d_inode->i_mutex);
1da177e4
LT
2675 if (d_mountpoint(dentry))
2676 error = -EBUSY;
2677 else {
2678 error = security_inode_unlink(dir, dentry);
bec1052e 2679 if (!error) {
1da177e4 2680 error = dir->i_op->unlink(dir, dentry);
bec1052e 2681 if (!error)
d83c49f3 2682 dont_mount(dentry);
bec1052e 2683 }
1da177e4 2684 }
1b1dcc1b 2685 mutex_unlock(&dentry->d_inode->i_mutex);
1da177e4
LT
2686
2687 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2688 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
ece95912 2689 fsnotify_link_count(dentry->d_inode);
e234f35c 2690 d_delete(dentry);
1da177e4 2691 }
0eeca283 2692
1da177e4
LT
2693 return error;
2694}
2695
2696/*
2697 * Make sure that the actual truncation of the file will occur outside its
1b1dcc1b 2698 * directory's i_mutex. Truncate can take a long time if there is a lot of
1da177e4
LT
2699 * writeout happening, and we don't want to prevent access to the directory
2700 * while waiting on the I/O.
2701 */
5590ff0d 2702static long do_unlinkat(int dfd, const char __user *pathname)
1da177e4 2703{
2ad94ae6
AV
2704 int error;
2705 char *name;
1da177e4
LT
2706 struct dentry *dentry;
2707 struct nameidata nd;
2708 struct inode *inode = NULL;
2709
2ad94ae6 2710 error = user_path_parent(dfd, pathname, &nd, &name);
1da177e4 2711 if (error)
2ad94ae6
AV
2712 return error;
2713
1da177e4
LT
2714 error = -EISDIR;
2715 if (nd.last_type != LAST_NORM)
2716 goto exit1;
0612d9fb
OH
2717
2718 nd.flags &= ~LOOKUP_PARENT;
2719
4ac91378 2720 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
49705b77 2721 dentry = lookup_hash(&nd);
1da177e4
LT
2722 error = PTR_ERR(dentry);
2723 if (!IS_ERR(dentry)) {
2724 /* Why not before? Because we want correct error value */
50338b88
TE
2725 if (nd.last.name[nd.last.len])
2726 goto slashes;
1da177e4 2727 inode = dentry->d_inode;
50338b88 2728 if (!inode)
e6bc45d6
TT
2729 goto slashes;
2730 ihold(inode);
0622753b
DH
2731 error = mnt_want_write(nd.path.mnt);
2732 if (error)
2733 goto exit2;
be6d3e56
KT
2734 error = security_path_unlink(&nd.path, dentry);
2735 if (error)
2736 goto exit3;
4ac91378 2737 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
be6d3e56 2738exit3:
0622753b 2739 mnt_drop_write(nd.path.mnt);
1da177e4
LT
2740 exit2:
2741 dput(dentry);
2742 }
4ac91378 2743 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1da177e4
LT
2744 if (inode)
2745 iput(inode); /* truncate the inode here */
2746exit1:
1d957f9b 2747 path_put(&nd.path);
1da177e4
LT
2748 putname(name);
2749 return error;
2750
2751slashes:
2752 error = !dentry->d_inode ? -ENOENT :
2753 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2754 goto exit2;
2755}
2756
2e4d0924 2757SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
5590ff0d
UD
2758{
2759 if ((flag & ~AT_REMOVEDIR) != 0)
2760 return -EINVAL;
2761
2762 if (flag & AT_REMOVEDIR)
2763 return do_rmdir(dfd, pathname);
2764
2765 return do_unlinkat(dfd, pathname);
2766}
2767
3480b257 2768SYSCALL_DEFINE1(unlink, const char __user *, pathname)
5590ff0d
UD
2769{
2770 return do_unlinkat(AT_FDCWD, pathname);
2771}
2772
db2e747b 2773int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
1da177e4 2774{
a95164d9 2775 int error = may_create(dir, dentry);
1da177e4
LT
2776
2777 if (error)
2778 return error;
2779
acfa4380 2780 if (!dir->i_op->symlink)
1da177e4
LT
2781 return -EPERM;
2782
2783 error = security_inode_symlink(dir, dentry, oldname);
2784 if (error)
2785 return error;
2786
1da177e4 2787 error = dir->i_op->symlink(dir, dentry, oldname);
a74574aa 2788 if (!error)
f38aa942 2789 fsnotify_create(dir, dentry);
1da177e4
LT
2790 return error;
2791}
2792
2e4d0924
HC
2793SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
2794 int, newdfd, const char __user *, newname)
1da177e4 2795{
2ad94ae6
AV
2796 int error;
2797 char *from;
2798 char *to;
6902d925
DH
2799 struct dentry *dentry;
2800 struct nameidata nd;
1da177e4
LT
2801
2802 from = getname(oldname);
2ad94ae6 2803 if (IS_ERR(from))
1da177e4 2804 return PTR_ERR(from);
1da177e4 2805
2ad94ae6 2806 error = user_path_parent(newdfd, newname, &nd, &to);
6902d925 2807 if (error)
2ad94ae6
AV
2808 goto out_putname;
2809
6902d925
DH
2810 dentry = lookup_create(&nd, 0);
2811 error = PTR_ERR(dentry);
2812 if (IS_ERR(dentry))
2813 goto out_unlock;
2814
75c3f29d
DH
2815 error = mnt_want_write(nd.path.mnt);
2816 if (error)
2817 goto out_dput;
be6d3e56
KT
2818 error = security_path_symlink(&nd.path, dentry, from);
2819 if (error)
2820 goto out_drop_write;
db2e747b 2821 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
be6d3e56 2822out_drop_write:
75c3f29d
DH
2823 mnt_drop_write(nd.path.mnt);
2824out_dput:
6902d925
DH
2825 dput(dentry);
2826out_unlock:
4ac91378 2827 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1d957f9b 2828 path_put(&nd.path);
6902d925
DH
2829 putname(to);
2830out_putname:
1da177e4
LT
2831 putname(from);
2832 return error;
2833}
2834
3480b257 2835SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
5590ff0d
UD
2836{
2837 return sys_symlinkat(oldname, AT_FDCWD, newname);
2838}
2839
1da177e4
LT
2840int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2841{
2842 struct inode *inode = old_dentry->d_inode;
2843 int error;
2844
2845 if (!inode)
2846 return -ENOENT;
2847
a95164d9 2848 error = may_create(dir, new_dentry);
1da177e4
LT
2849 if (error)
2850 return error;
2851
2852 if (dir->i_sb != inode->i_sb)
2853 return -EXDEV;
2854
2855 /*
2856 * A link to an append-only or immutable file cannot be created.
2857 */
2858 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2859 return -EPERM;
acfa4380 2860 if (!dir->i_op->link)
1da177e4 2861 return -EPERM;
7e79eedb 2862 if (S_ISDIR(inode->i_mode))
1da177e4
LT
2863 return -EPERM;
2864
2865 error = security_inode_link(old_dentry, dir, new_dentry);
2866 if (error)
2867 return error;
2868
7e79eedb 2869 mutex_lock(&inode->i_mutex);
aae8a97d
AK
2870 /* Make sure we don't allow creating hardlink to an unlinked file */
2871 if (inode->i_nlink == 0)
2872 error = -ENOENT;
2873 else
2874 error = dir->i_op->link(old_dentry, dir, new_dentry);
7e79eedb 2875 mutex_unlock(&inode->i_mutex);
e31e14ec 2876 if (!error)
7e79eedb 2877 fsnotify_link(dir, inode, new_dentry);
1da177e4
LT
2878 return error;
2879}
2880
2881/*
2882 * Hardlinks are often used in delicate situations. We avoid
2883 * security-related surprises by not following symlinks on the
2884 * newname. --KAB
2885 *
2886 * We don't follow them on the oldname either to be compatible
2887 * with linux 2.0, and to avoid hard-linking to directories
2888 * and other special files. --ADM
2889 */
2e4d0924
HC
2890SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
2891 int, newdfd, const char __user *, newname, int, flags)
1da177e4
LT
2892{
2893 struct dentry *new_dentry;
2d8f3038
AV
2894 struct nameidata nd;
2895 struct path old_path;
11a7b371 2896 int how = 0;
1da177e4 2897 int error;
2ad94ae6 2898 char *to;
1da177e4 2899
11a7b371 2900 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
c04030e1 2901 return -EINVAL;
11a7b371
AK
2902 /*
2903 * To use null names we require CAP_DAC_READ_SEARCH
2904 * This ensures that not everyone will be able to create
2905 * handlink using the passed filedescriptor.
2906 */
2907 if (flags & AT_EMPTY_PATH) {
2908 if (!capable(CAP_DAC_READ_SEARCH))
2909 return -ENOENT;
2910 how = LOOKUP_EMPTY;
2911 }
2912
2913 if (flags & AT_SYMLINK_FOLLOW)
2914 how |= LOOKUP_FOLLOW;
c04030e1 2915
11a7b371 2916 error = user_path_at(olddfd, oldname, how, &old_path);
1da177e4 2917 if (error)
2ad94ae6
AV
2918 return error;
2919
2920 error = user_path_parent(newdfd, newname, &nd, &to);
1da177e4
LT
2921 if (error)
2922 goto out;
2923 error = -EXDEV;
2d8f3038 2924 if (old_path.mnt != nd.path.mnt)
1da177e4
LT
2925 goto out_release;
2926 new_dentry = lookup_create(&nd, 0);
2927 error = PTR_ERR(new_dentry);
6902d925
DH
2928 if (IS_ERR(new_dentry))
2929 goto out_unlock;
75c3f29d
DH
2930 error = mnt_want_write(nd.path.mnt);
2931 if (error)
2932 goto out_dput;
be6d3e56
KT
2933 error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2934 if (error)
2935 goto out_drop_write;
2d8f3038 2936 error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
be6d3e56 2937out_drop_write:
75c3f29d
DH
2938 mnt_drop_write(nd.path.mnt);
2939out_dput:
6902d925
DH
2940 dput(new_dentry);
2941out_unlock:
4ac91378 2942 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
1da177e4 2943out_release:
1d957f9b 2944 path_put(&nd.path);
2ad94ae6 2945 putname(to);
1da177e4 2946out:
2d8f3038 2947 path_put(&old_path);
1da177e4
LT
2948
2949 return error;
2950}
2951
3480b257 2952SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
5590ff0d 2953{
c04030e1 2954 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
5590ff0d
UD
2955}
2956
1da177e4
LT
2957/*
2958 * The worst of all namespace operations - renaming directory. "Perverted"
2959 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2960 * Problems:
2961 * a) we can get into loop creation. Check is done in is_subdir().
2962 * b) race potential - two innocent renames can create a loop together.
2963 * That's where 4.4 screws up. Current fix: serialization on
a11f3a05 2964 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
1da177e4
LT
2965 * story.
2966 * c) we have to lock _three_ objects - parents and victim (if it exists).
1b1dcc1b 2967 * And that - after we got ->i_mutex on parents (until then we don't know
1da177e4
LT
2968 * whether the target exists). Solution: try to be smart with locking
2969 * order for inodes. We rely on the fact that tree topology may change
a11f3a05 2970 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
1da177e4
LT
2971 * move will be locked. Thus we can rank directories by the tree
2972 * (ancestors first) and rank all non-directories after them.
2973 * That works since everybody except rename does "lock parent, lookup,
a11f3a05 2974 * lock child" and rename is under ->s_vfs_rename_mutex.
1da177e4
LT
2975 * HOWEVER, it relies on the assumption that any object with ->lookup()
2976 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2977 * we'd better make sure that there's no link(2) for them.
e4eaac06 2978 * d) conversion from fhandle to dentry may come in the wrong moment - when
1b1dcc1b 2979 * we are removing the target. Solution: we will have to grab ->i_mutex
1da177e4 2980 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
c41b20e7 2981 * ->i_mutex on parents, which works but leads to some truly excessive
1da177e4
LT
2982 * locking].
2983 */
75c96f85
AB
2984static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2985 struct inode *new_dir, struct dentry *new_dentry)
1da177e4
LT
2986{
2987 int error = 0;
9055cba7 2988 struct inode *target = new_dentry->d_inode;
1da177e4
LT
2989
2990 /*
2991 * If we are going to change the parent - check write permissions,
2992 * we'll need to flip '..'.
2993 */
2994 if (new_dir != old_dir) {
f419a2e3 2995 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
1da177e4
LT
2996 if (error)
2997 return error;
2998 }
2999
3000 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3001 if (error)
3002 return error;
3003
d83c49f3 3004 if (target)
1b1dcc1b 3005 mutex_lock(&target->i_mutex);
9055cba7
SW
3006
3007 error = -EBUSY;
3008 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3009 goto out;
3010
3cebde24
SW
3011 if (target)
3012 shrink_dcache_parent(new_dentry);
9055cba7
SW
3013 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3014 if (error)
3015 goto out;
3016
1da177e4 3017 if (target) {
9055cba7
SW
3018 target->i_flags |= S_DEAD;
3019 dont_mount(new_dentry);
1da177e4 3020 }
9055cba7
SW
3021out:
3022 if (target)
3023 mutex_unlock(&target->i_mutex);
e31e14ec 3024 if (!error)
349457cc
MF
3025 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3026 d_move(old_dentry,new_dentry);
1da177e4
LT
3027 return error;
3028}
3029
75c96f85
AB
3030static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3031 struct inode *new_dir, struct dentry *new_dentry)
1da177e4 3032{
51892bbb 3033 struct inode *target = new_dentry->d_inode;
1da177e4
LT
3034 int error;
3035
3036 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3037 if (error)
3038 return error;
3039
3040 dget(new_dentry);
1da177e4 3041 if (target)
1b1dcc1b 3042 mutex_lock(&target->i_mutex);
51892bbb
SW
3043
3044 error = -EBUSY;
1da177e4 3045 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
51892bbb
SW
3046 goto out;
3047
3048 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3049 if (error)
3050 goto out;
3051
3052 if (target)
3053 dont_mount(new_dentry);
3054 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3055 d_move(old_dentry, new_dentry);
3056out:
1da177e4 3057 if (target)
1b1dcc1b 3058 mutex_unlock(&target->i_mutex);
1da177e4
LT
3059 dput(new_dentry);
3060 return error;
3061}
3062
3063int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3064 struct inode *new_dir, struct dentry *new_dentry)
3065{
3066 int error;
3067 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
59b0df21 3068 const unsigned char *old_name;
1da177e4
LT
3069
3070 if (old_dentry->d_inode == new_dentry->d_inode)
3071 return 0;
3072
3073 error = may_delete(old_dir, old_dentry, is_dir);
3074 if (error)
3075 return error;
3076
3077 if (!new_dentry->d_inode)
a95164d9 3078 error = may_create(new_dir, new_dentry);
1da177e4
LT
3079 else
3080 error = may_delete(new_dir, new_dentry, is_dir);
3081 if (error)
3082 return error;
3083
acfa4380 3084 if (!old_dir->i_op->rename)
1da177e4
LT
3085 return -EPERM;
3086
0eeca283
RL
3087 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3088
1da177e4
LT
3089 if (is_dir)
3090 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3091 else
3092 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
123df294
AV
3093 if (!error)
3094 fsnotify_move(old_dir, new_dir, old_name, is_dir,
5a190ae6 3095 new_dentry->d_inode, old_dentry);
0eeca283
RL
3096 fsnotify_oldname_free(old_name);
3097
1da177e4
LT
3098 return error;
3099}
3100
2e4d0924
HC
3101SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3102 int, newdfd, const char __user *, newname)
1da177e4 3103{
2ad94ae6
AV
3104 struct dentry *old_dir, *new_dir;
3105 struct dentry *old_dentry, *new_dentry;
3106 struct dentry *trap;
1da177e4 3107 struct nameidata oldnd, newnd;
2ad94ae6
AV
3108 char *from;
3109 char *to;
3110 int error;
1da177e4 3111
2ad94ae6 3112 error = user_path_parent(olddfd, oldname, &oldnd, &from);
1da177e4
LT
3113 if (error)
3114 goto exit;
3115
2ad94ae6 3116 error = user_path_parent(newdfd, newname, &newnd, &to);
1da177e4
LT
3117 if (error)
3118 goto exit1;
3119
3120 error = -EXDEV;
4ac91378 3121 if (oldnd.path.mnt != newnd.path.mnt)
1da177e4
LT
3122 goto exit2;
3123
4ac91378 3124 old_dir = oldnd.path.dentry;
1da177e4
LT
3125 error = -EBUSY;
3126 if (oldnd.last_type != LAST_NORM)
3127 goto exit2;
3128
4ac91378 3129 new_dir = newnd.path.dentry;
1da177e4
LT
3130 if (newnd.last_type != LAST_NORM)
3131 goto exit2;
3132
0612d9fb
OH
3133 oldnd.flags &= ~LOOKUP_PARENT;
3134 newnd.flags &= ~LOOKUP_PARENT;
4e9ed2f8 3135 newnd.flags |= LOOKUP_RENAME_TARGET;
0612d9fb 3136
1da177e4
LT
3137 trap = lock_rename(new_dir, old_dir);
3138
49705b77 3139 old_dentry = lookup_hash(&oldnd);
1da177e4
LT
3140 error = PTR_ERR(old_dentry);
3141 if (IS_ERR(old_dentry))
3142 goto exit3;
3143 /* source must exist */
3144 error = -ENOENT;
3145 if (!old_dentry->d_inode)
3146 goto exit4;
3147 /* unless the source is a directory trailing slashes give -ENOTDIR */
3148 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3149 error = -ENOTDIR;
3150 if (oldnd.last.name[oldnd.last.len])
3151 goto exit4;
3152 if (newnd.last.name[newnd.last.len])
3153 goto exit4;
3154 }
3155 /* source should not be ancestor of target */
3156 error = -EINVAL;
3157 if (old_dentry == trap)
3158 goto exit4;
49705b77 3159 new_dentry = lookup_hash(&newnd);
1da177e4
LT
3160 error = PTR_ERR(new_dentry);
3161 if (IS_ERR(new_dentry))
3162 goto exit4;
3163 /* target should not be an ancestor of source */
3164 error = -ENOTEMPTY;
3165 if (new_dentry == trap)
3166 goto exit5;
3167
9079b1eb
DH
3168 error = mnt_want_write(oldnd.path.mnt);
3169 if (error)
3170 goto exit5;
be6d3e56
KT
3171 error = security_path_rename(&oldnd.path, old_dentry,
3172 &newnd.path, new_dentry);
3173 if (error)
3174 goto exit6;
1da177e4
LT
3175 error = vfs_rename(old_dir->d_inode, old_dentry,
3176 new_dir->d_inode, new_dentry);
be6d3e56 3177exit6:
9079b1eb 3178 mnt_drop_write(oldnd.path.mnt);
1da177e4
LT
3179exit5:
3180 dput(new_dentry);
3181exit4:
3182 dput(old_dentry);
3183exit3:
3184 unlock_rename(new_dir, old_dir);
3185exit2:
1d957f9b 3186 path_put(&newnd.path);
2ad94ae6 3187 putname(to);
1da177e4 3188exit1:
1d957f9b 3189 path_put(&oldnd.path);
1da177e4 3190 putname(from);
2ad94ae6 3191exit:
1da177e4
LT
3192 return error;
3193}
3194
a26eab24 3195SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
5590ff0d
UD
3196{
3197 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3198}
3199
1da177e4
LT
3200int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3201{
3202 int len;
3203
3204 len = PTR_ERR(link);
3205 if (IS_ERR(link))
3206 goto out;
3207
3208 len = strlen(link);
3209 if (len > (unsigned) buflen)
3210 len = buflen;
3211 if (copy_to_user(buffer, link, len))
3212 len = -EFAULT;
3213out:
3214 return len;
3215}
3216
3217/*
3218 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3219 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3220 * using) it for any given inode is up to filesystem.
3221 */
3222int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3223{
3224 struct nameidata nd;
cc314eef 3225 void *cookie;
694a1764 3226 int res;
cc314eef 3227
1da177e4 3228 nd.depth = 0;
cc314eef 3229 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
694a1764
MS
3230 if (IS_ERR(cookie))
3231 return PTR_ERR(cookie);
3232
3233 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3234 if (dentry->d_inode->i_op->put_link)
3235 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3236 return res;
1da177e4
LT
3237}
3238
3239int vfs_follow_link(struct nameidata *nd, const char *link)
3240{
3241 return __vfs_follow_link(nd, link);
3242}
3243
3244/* get the link contents into pagecache */
3245static char *page_getlink(struct dentry * dentry, struct page **ppage)
3246{
ebd09abb
DG
3247 char *kaddr;
3248 struct page *page;
1da177e4 3249 struct address_space *mapping = dentry->d_inode->i_mapping;
090d2b18 3250 page = read_mapping_page(mapping, 0, NULL);
1da177e4 3251 if (IS_ERR(page))
6fe6900e 3252 return (char*)page;
1da177e4 3253 *ppage = page;
ebd09abb
DG
3254 kaddr = kmap(page);
3255 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3256 return kaddr;
1da177e4
LT
3257}
3258
3259int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3260{
3261 struct page *page = NULL;
3262 char *s = page_getlink(dentry, &page);
3263 int res = vfs_readlink(dentry,buffer,buflen,s);
3264 if (page) {
3265 kunmap(page);
3266 page_cache_release(page);
3267 }
3268 return res;
3269}
3270
cc314eef 3271void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
1da177e4 3272{
cc314eef 3273 struct page *page = NULL;
1da177e4 3274 nd_set_link(nd, page_getlink(dentry, &page));
cc314eef 3275 return page;
1da177e4
LT
3276}
3277
cc314eef 3278void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1da177e4 3279{
cc314eef
LT
3280 struct page *page = cookie;
3281
3282 if (page) {
1da177e4
LT
3283 kunmap(page);
3284 page_cache_release(page);
1da177e4
LT
3285 }
3286}
3287
54566b2c
NP
3288/*
3289 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3290 */
3291int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
1da177e4
LT
3292{
3293 struct address_space *mapping = inode->i_mapping;
0adb25d2 3294 struct page *page;
afddba49 3295 void *fsdata;
beb497ab 3296 int err;
1da177e4 3297 char *kaddr;
54566b2c
NP
3298 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3299 if (nofs)
3300 flags |= AOP_FLAG_NOFS;
1da177e4 3301
7e53cac4 3302retry:
afddba49 3303 err = pagecache_write_begin(NULL, mapping, 0, len-1,
54566b2c 3304 flags, &page, &fsdata);
1da177e4 3305 if (err)
afddba49
NP
3306 goto fail;
3307
1da177e4
LT
3308 kaddr = kmap_atomic(page, KM_USER0);
3309 memcpy(kaddr, symname, len-1);
3310 kunmap_atomic(kaddr, KM_USER0);
afddba49
NP
3311
3312 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3313 page, fsdata);
1da177e4
LT
3314 if (err < 0)
3315 goto fail;
afddba49
NP
3316 if (err < len-1)
3317 goto retry;
3318
1da177e4
LT
3319 mark_inode_dirty(inode);
3320 return 0;
1da177e4
LT
3321fail:
3322 return err;
3323}
3324
0adb25d2
KK
3325int page_symlink(struct inode *inode, const char *symname, int len)
3326{
3327 return __page_symlink(inode, symname, len,
54566b2c 3328 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
0adb25d2
KK
3329}
3330
92e1d5be 3331const struct inode_operations page_symlink_inode_operations = {
1da177e4
LT
3332 .readlink = generic_readlink,
3333 .follow_link = page_follow_link_light,
3334 .put_link = page_put_link,
3335};
3336
2d8f3038 3337EXPORT_SYMBOL(user_path_at);
cc53ce53 3338EXPORT_SYMBOL(follow_down_one);
1da177e4
LT
3339EXPORT_SYMBOL(follow_down);
3340EXPORT_SYMBOL(follow_up);
3341EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
3342EXPORT_SYMBOL(getname);
3343EXPORT_SYMBOL(lock_rename);
1da177e4
LT
3344EXPORT_SYMBOL(lookup_one_len);
3345EXPORT_SYMBOL(page_follow_link_light);
3346EXPORT_SYMBOL(page_put_link);
3347EXPORT_SYMBOL(page_readlink);
0adb25d2 3348EXPORT_SYMBOL(__page_symlink);
1da177e4
LT
3349EXPORT_SYMBOL(page_symlink);
3350EXPORT_SYMBOL(page_symlink_inode_operations);
c9c6cac0 3351EXPORT_SYMBOL(kern_path_parent);
d1811465 3352EXPORT_SYMBOL(kern_path);
16f18200 3353EXPORT_SYMBOL(vfs_path_lookup);
f419a2e3 3354EXPORT_SYMBOL(inode_permission);
1da177e4
LT
3355EXPORT_SYMBOL(unlock_rename);
3356EXPORT_SYMBOL(vfs_create);
3357EXPORT_SYMBOL(vfs_follow_link);
3358EXPORT_SYMBOL(vfs_link);
3359EXPORT_SYMBOL(vfs_mkdir);
3360EXPORT_SYMBOL(vfs_mknod);
3361EXPORT_SYMBOL(generic_permission);
3362EXPORT_SYMBOL(vfs_readlink);
3363EXPORT_SYMBOL(vfs_rename);
3364EXPORT_SYMBOL(vfs_rmdir);
3365EXPORT_SYMBOL(vfs_symlink);
3366EXPORT_SYMBOL(vfs_unlink);
3367EXPORT_SYMBOL(dentry_unhash);
3368EXPORT_SYMBOL(generic_readlink);