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