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