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