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