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