]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/namei.c
[PATCH] namei fixes (6/19)
[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>
24#include <linux/dnotify.h>
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);
317 _mntput(nd->mnt);
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
1da177e4
LT
501static inline int __do_follow_link(struct dentry *dentry, struct nameidata *nd)
502{
503 int error;
504
505 touch_atime(nd->mnt, dentry);
506 nd_set_link(nd, NULL);
507 error = dentry->d_inode->i_op->follow_link(dentry, nd);
508 if (!error) {
509 char *s = nd_get_link(nd);
510 if (s)
511 error = __vfs_follow_link(nd, s);
512 if (dentry->d_inode->i_op->put_link)
513 dentry->d_inode->i_op->put_link(dentry, nd);
514 }
515
516 return error;
517}
518
519/*
520 * This limits recursive symlink follows to 8, while
521 * limiting consecutive symlinks to 40.
522 *
523 * Without that kind of total limit, nasty chains of consecutive
524 * symlinks can cause almost arbitrarily long lookups.
525 */
90ebe565 526static inline int do_follow_link(struct path *path, struct nameidata *nd)
1da177e4
LT
527{
528 int err = -ELOOP;
529 if (current->link_count >= MAX_NESTED_LINKS)
530 goto loop;
531 if (current->total_link_count >= 40)
532 goto loop;
533 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
534 cond_resched();
90ebe565 535 err = security_inode_follow_link(path->dentry, nd);
1da177e4
LT
536 if (err)
537 goto loop;
538 current->link_count++;
539 current->total_link_count++;
540 nd->depth++;
839d9f93 541 mntget(path->mnt);
90ebe565 542 err = __do_follow_link(path->dentry, nd);
5f92b3bc
AV
543 dput(path->dentry);
544 mntput(path->mnt);
839d9f93
AV
545 current->link_count--;
546 nd->depth--;
1da177e4
LT
547 return err;
548loop:
5f92b3bc 549 dput(path->dentry);
839d9f93 550 path_release(nd);
1da177e4
LT
551 return err;
552}
553
554int follow_up(struct vfsmount **mnt, struct dentry **dentry)
555{
556 struct vfsmount *parent;
557 struct dentry *mountpoint;
558 spin_lock(&vfsmount_lock);
559 parent=(*mnt)->mnt_parent;
560 if (parent == *mnt) {
561 spin_unlock(&vfsmount_lock);
562 return 0;
563 }
564 mntget(parent);
565 mountpoint=dget((*mnt)->mnt_mountpoint);
566 spin_unlock(&vfsmount_lock);
567 dput(*dentry);
568 *dentry = mountpoint;
569 mntput(*mnt);
570 *mnt = parent;
571 return 1;
572}
573
574/* no need for dcache_lock, as serialization is taken care in
575 * namespace.c
576 */
577static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
578{
579 int res = 0;
580 while (d_mountpoint(*dentry)) {
581 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
582 if (!mounted)
583 break;
584 mntput(*mnt);
585 *mnt = mounted;
586 dput(*dentry);
587 *dentry = dget(mounted->mnt_root);
588 res = 1;
589 }
590 return res;
591}
592
593/* no need for dcache_lock, as serialization is taken care in
594 * namespace.c
595 */
596static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
597{
598 struct vfsmount *mounted;
599
600 mounted = lookup_mnt(*mnt, *dentry);
601 if (mounted) {
602 mntput(*mnt);
603 *mnt = mounted;
604 dput(*dentry);
605 *dentry = dget(mounted->mnt_root);
606 return 1;
607 }
608 return 0;
609}
610
611int follow_down(struct vfsmount **mnt, struct dentry **dentry)
612{
613 return __follow_down(mnt,dentry);
614}
615
616static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
617{
618 while(1) {
619 struct vfsmount *parent;
620 struct dentry *old = *dentry;
621
622 read_lock(&current->fs->lock);
623 if (*dentry == current->fs->root &&
624 *mnt == current->fs->rootmnt) {
625 read_unlock(&current->fs->lock);
626 break;
627 }
628 read_unlock(&current->fs->lock);
629 spin_lock(&dcache_lock);
630 if (*dentry != (*mnt)->mnt_root) {
631 *dentry = dget((*dentry)->d_parent);
632 spin_unlock(&dcache_lock);
633 dput(old);
634 break;
635 }
636 spin_unlock(&dcache_lock);
637 spin_lock(&vfsmount_lock);
638 parent = (*mnt)->mnt_parent;
639 if (parent == *mnt) {
640 spin_unlock(&vfsmount_lock);
641 break;
642 }
643 mntget(parent);
644 *dentry = dget((*mnt)->mnt_mountpoint);
645 spin_unlock(&vfsmount_lock);
646 dput(old);
647 mntput(*mnt);
648 *mnt = parent;
649 }
650 follow_mount(mnt, dentry);
651}
652
1da177e4
LT
653/*
654 * It's more convoluted than I'd like it to be, but... it's still fairly
655 * small and for now I'd prefer to have fast path as straight as possible.
656 * It _is_ time-critical.
657 */
658static int do_lookup(struct nameidata *nd, struct qstr *name,
659 struct path *path)
660{
661 struct vfsmount *mnt = nd->mnt;
662 struct dentry *dentry = __d_lookup(nd->dentry, name);
663
664 if (!dentry)
665 goto need_lookup;
666 if (dentry->d_op && dentry->d_op->d_revalidate)
667 goto need_revalidate;
668done:
669 path->mnt = mnt;
670 path->dentry = dentry;
671 return 0;
672
673need_lookup:
674 dentry = real_lookup(nd->dentry, name, nd);
675 if (IS_ERR(dentry))
676 goto fail;
677 goto done;
678
679need_revalidate:
680 if (dentry->d_op->d_revalidate(dentry, nd))
681 goto done;
682 if (d_invalidate(dentry))
683 goto done;
684 dput(dentry);
685 goto need_lookup;
686
687fail:
688 return PTR_ERR(dentry);
689}
690
691/*
692 * Name resolution.
ea3834d9
PM
693 * This is the basic name resolution function, turning a pathname into
694 * the final dentry. We expect 'base' to be positive and a directory.
1da177e4 695 *
ea3834d9
PM
696 * Returns 0 and nd will have valid dentry and mnt on success.
697 * Returns error and drops reference to input namei data on failure.
1da177e4
LT
698 */
699static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
700{
701 struct path next;
702 struct inode *inode;
703 int err;
704 unsigned int lookup_flags = nd->flags;
705
706 while (*name=='/')
707 name++;
708 if (!*name)
709 goto return_reval;
710
711 inode = nd->dentry->d_inode;
712 if (nd->depth)
713 lookup_flags = LOOKUP_FOLLOW;
714
715 /* At this point we know we have a real path component. */
716 for(;;) {
717 unsigned long hash;
718 struct qstr this;
719 unsigned int c;
720
721 err = exec_permission_lite(inode, nd);
722 if (err == -EAGAIN) {
723 err = permission(inode, MAY_EXEC, nd);
724 }
725 if (err)
726 break;
727
728 this.name = name;
729 c = *(const unsigned char *)name;
730
731 hash = init_name_hash();
732 do {
733 name++;
734 hash = partial_name_hash(c, hash);
735 c = *(const unsigned char *)name;
736 } while (c && (c != '/'));
737 this.len = name - (const char *) this.name;
738 this.hash = end_name_hash(hash);
739
740 /* remove trailing slashes? */
741 if (!c)
742 goto last_component;
743 while (*++name == '/');
744 if (!*name)
745 goto last_with_slashes;
746
747 /*
748 * "." and ".." are special - ".." especially so because it has
749 * to be able to know about the current root directory and
750 * parent relationships.
751 */
752 if (this.name[0] == '.') switch (this.len) {
753 default:
754 break;
755 case 2:
756 if (this.name[1] != '.')
757 break;
758 follow_dotdot(&nd->mnt, &nd->dentry);
759 inode = nd->dentry->d_inode;
760 /* fallthrough */
761 case 1:
762 continue;
763 }
764 /*
765 * See if the low-level filesystem might want
766 * to use its own hash..
767 */
768 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
769 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
770 if (err < 0)
771 break;
772 }
773 nd->flags |= LOOKUP_CONTINUE;
774 /* This does the actual lookups.. */
775 err = do_lookup(nd, &this, &next);
776 if (err)
777 break;
778 /* Check mountpoints.. */
779 follow_mount(&next.mnt, &next.dentry);
780
781 err = -ENOENT;
782 inode = next.dentry->d_inode;
783 if (!inode)
784 goto out_dput;
785 err = -ENOTDIR;
786 if (!inode->i_op)
787 goto out_dput;
788
789 if (inode->i_op->follow_link) {
90ebe565 790 err = do_follow_link(&next, nd);
1da177e4
LT
791 if (err)
792 goto return_err;
793 err = -ENOENT;
794 inode = nd->dentry->d_inode;
795 if (!inode)
796 break;
797 err = -ENOTDIR;
798 if (!inode->i_op)
799 break;
800 } else {
801 dput(nd->dentry);
802 nd->mnt = next.mnt;
803 nd->dentry = next.dentry;
804 }
805 err = -ENOTDIR;
806 if (!inode->i_op->lookup)
807 break;
808 continue;
809 /* here ends the main loop */
810
811last_with_slashes:
812 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
813last_component:
814 nd->flags &= ~LOOKUP_CONTINUE;
815 if (lookup_flags & LOOKUP_PARENT)
816 goto lookup_parent;
817 if (this.name[0] == '.') switch (this.len) {
818 default:
819 break;
820 case 2:
821 if (this.name[1] != '.')
822 break;
823 follow_dotdot(&nd->mnt, &nd->dentry);
824 inode = nd->dentry->d_inode;
825 /* fallthrough */
826 case 1:
827 goto return_reval;
828 }
829 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
830 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
831 if (err < 0)
832 break;
833 }
834 err = do_lookup(nd, &this, &next);
835 if (err)
836 break;
837 follow_mount(&next.mnt, &next.dentry);
838 inode = next.dentry->d_inode;
839 if ((lookup_flags & LOOKUP_FOLLOW)
840 && inode && inode->i_op && inode->i_op->follow_link) {
90ebe565 841 err = do_follow_link(&next, nd);
1da177e4
LT
842 if (err)
843 goto return_err;
844 inode = nd->dentry->d_inode;
845 } else {
846 dput(nd->dentry);
847 nd->mnt = next.mnt;
848 nd->dentry = next.dentry;
849 }
850 err = -ENOENT;
851 if (!inode)
852 break;
853 if (lookup_flags & LOOKUP_DIRECTORY) {
854 err = -ENOTDIR;
855 if (!inode->i_op || !inode->i_op->lookup)
856 break;
857 }
858 goto return_base;
859lookup_parent:
860 nd->last = this;
861 nd->last_type = LAST_NORM;
862 if (this.name[0] != '.')
863 goto return_base;
864 if (this.len == 1)
865 nd->last_type = LAST_DOT;
866 else if (this.len == 2 && this.name[1] == '.')
867 nd->last_type = LAST_DOTDOT;
868 else
869 goto return_base;
870return_reval:
871 /*
872 * We bypassed the ordinary revalidation routines.
873 * We may need to check the cached dentry for staleness.
874 */
875 if (nd->dentry && nd->dentry->d_sb &&
876 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
877 err = -ESTALE;
878 /* Note: we do not d_invalidate() */
879 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
880 break;
881 }
882return_base:
883 return 0;
884out_dput:
885 dput(next.dentry);
886 break;
887 }
888 path_release(nd);
889return_err:
890 return err;
891}
892
893/*
894 * Wrapper to retry pathname resolution whenever the underlying
895 * file system returns an ESTALE.
896 *
897 * Retry the whole path once, forcing real lookup requests
898 * instead of relying on the dcache.
899 */
900int fastcall link_path_walk(const char *name, struct nameidata *nd)
901{
902 struct nameidata save = *nd;
903 int result;
904
905 /* make sure the stuff we saved doesn't go away */
906 dget(save.dentry);
907 mntget(save.mnt);
908
909 result = __link_path_walk(name, nd);
910 if (result == -ESTALE) {
911 *nd = save;
912 dget(nd->dentry);
913 mntget(nd->mnt);
914 nd->flags |= LOOKUP_REVAL;
915 result = __link_path_walk(name, nd);
916 }
917
918 dput(save.dentry);
919 mntput(save.mnt);
920
921 return result;
922}
923
924int fastcall path_walk(const char * name, struct nameidata *nd)
925{
926 current->total_link_count = 0;
927 return link_path_walk(name, nd);
928}
929
ea3834d9
PM
930/*
931 * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
932 * everything is done. Returns 0 and drops input nd, if lookup failed;
933 */
1da177e4
LT
934static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
935{
936 if (path_walk(name, nd))
937 return 0; /* something went wrong... */
938
939 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
940 struct dentry *old_dentry = nd->dentry;
941 struct vfsmount *old_mnt = nd->mnt;
942 struct qstr last = nd->last;
943 int last_type = nd->last_type;
944 /*
945 * NAME was not found in alternate root or it's a directory. Try to find
946 * it in the normal root:
947 */
948 nd->last_type = LAST_ROOT;
949 read_lock(&current->fs->lock);
950 nd->mnt = mntget(current->fs->rootmnt);
951 nd->dentry = dget(current->fs->root);
952 read_unlock(&current->fs->lock);
953 if (path_walk(name, nd) == 0) {
954 if (nd->dentry->d_inode) {
955 dput(old_dentry);
956 mntput(old_mnt);
957 return 1;
958 }
959 path_release(nd);
960 }
961 nd->dentry = old_dentry;
962 nd->mnt = old_mnt;
963 nd->last = last;
964 nd->last_type = last_type;
965 }
966 return 1;
967}
968
969void set_fs_altroot(void)
970{
971 char *emul = __emul_prefix();
972 struct nameidata nd;
973 struct vfsmount *mnt = NULL, *oldmnt;
974 struct dentry *dentry = NULL, *olddentry;
975 int err;
976
977 if (!emul)
978 goto set_it;
979 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
980 if (!err) {
981 mnt = nd.mnt;
982 dentry = nd.dentry;
983 }
984set_it:
985 write_lock(&current->fs->lock);
986 oldmnt = current->fs->altrootmnt;
987 olddentry = current->fs->altroot;
988 current->fs->altrootmnt = mnt;
989 current->fs->altroot = dentry;
990 write_unlock(&current->fs->lock);
991 if (olddentry) {
992 dput(olddentry);
993 mntput(oldmnt);
994 }
995}
996
ea3834d9 997/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1da177e4
LT
998int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
999{
ea3834d9 1000 int retval = 0;
1da177e4
LT
1001
1002 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1003 nd->flags = flags;
1004 nd->depth = 0;
1005
1006 read_lock(&current->fs->lock);
1007 if (*name=='/') {
1008 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
1009 nd->mnt = mntget(current->fs->altrootmnt);
1010 nd->dentry = dget(current->fs->altroot);
1011 read_unlock(&current->fs->lock);
1012 if (__emul_lookup_dentry(name,nd))
ea3834d9 1013 goto out; /* found in altroot */
1da177e4
LT
1014 read_lock(&current->fs->lock);
1015 }
1016 nd->mnt = mntget(current->fs->rootmnt);
1017 nd->dentry = dget(current->fs->root);
1018 } else {
1019 nd->mnt = mntget(current->fs->pwdmnt);
1020 nd->dentry = dget(current->fs->pwd);
1021 }
1022 read_unlock(&current->fs->lock);
1023 current->total_link_count = 0;
1024 retval = link_path_walk(name, nd);
ea3834d9 1025out:
1da177e4
LT
1026 if (unlikely(current->audit_context
1027 && nd && nd->dentry && nd->dentry->d_inode))
1028 audit_inode(name, nd->dentry->d_inode);
1029 return retval;
1030}
1031
1032/*
1033 * Restricted form of lookup. Doesn't follow links, single-component only,
1034 * needs parent already locked. Doesn't follow mounts.
1035 * SMP-safe.
1036 */
1037static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
1038{
1039 struct dentry * dentry;
1040 struct inode *inode;
1041 int err;
1042
1043 inode = base->d_inode;
1044 err = permission(inode, MAY_EXEC, nd);
1045 dentry = ERR_PTR(err);
1046 if (err)
1047 goto out;
1048
1049 /*
1050 * See if the low-level filesystem might want
1051 * to use its own hash..
1052 */
1053 if (base->d_op && base->d_op->d_hash) {
1054 err = base->d_op->d_hash(base, name);
1055 dentry = ERR_PTR(err);
1056 if (err < 0)
1057 goto out;
1058 }
1059
1060 dentry = cached_lookup(base, name, nd);
1061 if (!dentry) {
1062 struct dentry *new = d_alloc(base, name);
1063 dentry = ERR_PTR(-ENOMEM);
1064 if (!new)
1065 goto out;
1066 dentry = inode->i_op->lookup(inode, new, nd);
1067 if (!dentry)
1068 dentry = new;
1069 else
1070 dput(new);
1071 }
1072out:
1073 return dentry;
1074}
1075
1076struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
1077{
1078 return __lookup_hash(name, base, NULL);
1079}
1080
1081/* SMP-safe */
1082struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1083{
1084 unsigned long hash;
1085 struct qstr this;
1086 unsigned int c;
1087
1088 this.name = name;
1089 this.len = len;
1090 if (!len)
1091 goto access;
1092
1093 hash = init_name_hash();
1094 while (len--) {
1095 c = *(const unsigned char *)name++;
1096 if (c == '/' || c == '\0')
1097 goto access;
1098 hash = partial_name_hash(c, hash);
1099 }
1100 this.hash = end_name_hash(hash);
1101
1102 return lookup_hash(&this, base);
1103access:
1104 return ERR_PTR(-EACCES);
1105}
1106
1107/*
1108 * namei()
1109 *
1110 * is used by most simple commands to get the inode of a specified name.
1111 * Open, link etc use their own routines, but this is enough for things
1112 * like 'chmod' etc.
1113 *
1114 * namei exists in two versions: namei/lnamei. The only difference is
1115 * that namei follows links, while lnamei does not.
1116 * SMP-safe
1117 */
1118int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1119{
1120 char *tmp = getname(name);
1121 int err = PTR_ERR(tmp);
1122
1123 if (!IS_ERR(tmp)) {
1124 err = path_lookup(tmp, flags, nd);
1125 putname(tmp);
1126 }
1127 return err;
1128}
1129
1130/*
1131 * It's inline, so penalty for filesystems that don't use sticky bit is
1132 * minimal.
1133 */
1134static inline int check_sticky(struct inode *dir, struct inode *inode)
1135{
1136 if (!(dir->i_mode & S_ISVTX))
1137 return 0;
1138 if (inode->i_uid == current->fsuid)
1139 return 0;
1140 if (dir->i_uid == current->fsuid)
1141 return 0;
1142 return !capable(CAP_FOWNER);
1143}
1144
1145/*
1146 * Check whether we can remove a link victim from directory dir, check
1147 * whether the type of victim is right.
1148 * 1. We can't do it if dir is read-only (done in permission())
1149 * 2. We should have write and exec permissions on dir
1150 * 3. We can't remove anything from append-only dir
1151 * 4. We can't do anything with immutable dir (done in permission())
1152 * 5. If the sticky bit on dir is set we should either
1153 * a. be owner of dir, or
1154 * b. be owner of victim, or
1155 * c. have CAP_FOWNER capability
1156 * 6. If the victim is append-only or immutable we can't do antyhing with
1157 * links pointing to it.
1158 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1159 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1160 * 9. We can't remove a root or mountpoint.
1161 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1162 * nfs_async_unlink().
1163 */
1164static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1165{
1166 int error;
1167
1168 if (!victim->d_inode)
1169 return -ENOENT;
1170
1171 BUG_ON(victim->d_parent->d_inode != dir);
1172
1173 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1174 if (error)
1175 return error;
1176 if (IS_APPEND(dir))
1177 return -EPERM;
1178 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1179 IS_IMMUTABLE(victim->d_inode))
1180 return -EPERM;
1181 if (isdir) {
1182 if (!S_ISDIR(victim->d_inode->i_mode))
1183 return -ENOTDIR;
1184 if (IS_ROOT(victim))
1185 return -EBUSY;
1186 } else if (S_ISDIR(victim->d_inode->i_mode))
1187 return -EISDIR;
1188 if (IS_DEADDIR(dir))
1189 return -ENOENT;
1190 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1191 return -EBUSY;
1192 return 0;
1193}
1194
1195/* Check whether we can create an object with dentry child in directory
1196 * dir.
1197 * 1. We can't do it if child already exists (open has special treatment for
1198 * this case, but since we are inlined it's OK)
1199 * 2. We can't do it if dir is read-only (done in permission())
1200 * 3. We should have write and exec permissions on dir
1201 * 4. We can't do it if dir is immutable (done in permission())
1202 */
1203static inline int may_create(struct inode *dir, struct dentry *child,
1204 struct nameidata *nd)
1205{
1206 if (child->d_inode)
1207 return -EEXIST;
1208 if (IS_DEADDIR(dir))
1209 return -ENOENT;
1210 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1211}
1212
1213/*
1214 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1215 * reasons.
1216 *
1217 * O_DIRECTORY translates into forcing a directory lookup.
1218 */
1219static inline int lookup_flags(unsigned int f)
1220{
1221 unsigned long retval = LOOKUP_FOLLOW;
1222
1223 if (f & O_NOFOLLOW)
1224 retval &= ~LOOKUP_FOLLOW;
1225
1226 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1227 retval &= ~LOOKUP_FOLLOW;
1228
1229 if (f & O_DIRECTORY)
1230 retval |= LOOKUP_DIRECTORY;
1231
1232 return retval;
1233}
1234
1235/*
1236 * p1 and p2 should be directories on the same fs.
1237 */
1238struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1239{
1240 struct dentry *p;
1241
1242 if (p1 == p2) {
1243 down(&p1->d_inode->i_sem);
1244 return NULL;
1245 }
1246
1247 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1248
1249 for (p = p1; p->d_parent != p; p = p->d_parent) {
1250 if (p->d_parent == p2) {
1251 down(&p2->d_inode->i_sem);
1252 down(&p1->d_inode->i_sem);
1253 return p;
1254 }
1255 }
1256
1257 for (p = p2; p->d_parent != p; p = p->d_parent) {
1258 if (p->d_parent == p1) {
1259 down(&p1->d_inode->i_sem);
1260 down(&p2->d_inode->i_sem);
1261 return p;
1262 }
1263 }
1264
1265 down(&p1->d_inode->i_sem);
1266 down(&p2->d_inode->i_sem);
1267 return NULL;
1268}
1269
1270void unlock_rename(struct dentry *p1, struct dentry *p2)
1271{
1272 up(&p1->d_inode->i_sem);
1273 if (p1 != p2) {
1274 up(&p2->d_inode->i_sem);
1275 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1276 }
1277}
1278
1279int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1280 struct nameidata *nd)
1281{
1282 int error = may_create(dir, dentry, nd);
1283
1284 if (error)
1285 return error;
1286
1287 if (!dir->i_op || !dir->i_op->create)
1288 return -EACCES; /* shouldn't it be ENOSYS? */
1289 mode &= S_IALLUGO;
1290 mode |= S_IFREG;
1291 error = security_inode_create(dir, dentry, mode);
1292 if (error)
1293 return error;
1294 DQUOT_INIT(dir);
1295 error = dir->i_op->create(dir, dentry, mode, nd);
1296 if (!error) {
1297 inode_dir_notify(dir, DN_CREATE);
1298 security_inode_post_create(dir, dentry, mode);
1299 }
1300 return error;
1301}
1302
1303int may_open(struct nameidata *nd, int acc_mode, int flag)
1304{
1305 struct dentry *dentry = nd->dentry;
1306 struct inode *inode = dentry->d_inode;
1307 int error;
1308
1309 if (!inode)
1310 return -ENOENT;
1311
1312 if (S_ISLNK(inode->i_mode))
1313 return -ELOOP;
1314
1315 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1316 return -EISDIR;
1317
1318 error = permission(inode, acc_mode, nd);
1319 if (error)
1320 return error;
1321
1322 /*
1323 * FIFO's, sockets and device files are special: they don't
1324 * actually live on the filesystem itself, and as such you
1325 * can write to them even if the filesystem is read-only.
1326 */
1327 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1328 flag &= ~O_TRUNC;
1329 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1330 if (nd->mnt->mnt_flags & MNT_NODEV)
1331 return -EACCES;
1332
1333 flag &= ~O_TRUNC;
1334 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1335 return -EROFS;
1336 /*
1337 * An append-only file must be opened in append mode for writing.
1338 */
1339 if (IS_APPEND(inode)) {
1340 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1341 return -EPERM;
1342 if (flag & O_TRUNC)
1343 return -EPERM;
1344 }
1345
1346 /* O_NOATIME can only be set by the owner or superuser */
1347 if (flag & O_NOATIME)
1348 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1349 return -EPERM;
1350
1351 /*
1352 * Ensure there are no outstanding leases on the file.
1353 */
1354 error = break_lease(inode, flag);
1355 if (error)
1356 return error;
1357
1358 if (flag & O_TRUNC) {
1359 error = get_write_access(inode);
1360 if (error)
1361 return error;
1362
1363 /*
1364 * Refuse to truncate files with mandatory locks held on them.
1365 */
1366 error = locks_verify_locked(inode);
1367 if (!error) {
1368 DQUOT_INIT(inode);
1369
1370 error = do_truncate(dentry, 0);
1371 }
1372 put_write_access(inode);
1373 if (error)
1374 return error;
1375 } else
1376 if (flag & FMODE_WRITE)
1377 DQUOT_INIT(inode);
1378
1379 return 0;
1380}
1381
1382/*
1383 * open_namei()
1384 *
1385 * namei for open - this is in fact almost the whole open-routine.
1386 *
1387 * Note that the low bits of "flag" aren't the same as in the open
1388 * system call - they are 00 - no permissions needed
1389 * 01 - read permission needed
1390 * 10 - write permission needed
1391 * 11 - read/write permissions needed
1392 * which is a lot more logical, and also allows the "no perm" needed
1393 * for symlinks (where the permissions are checked later).
1394 * SMP-safe
1395 */
1396int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1397{
1398 int acc_mode, error = 0;
4e7506e4 1399 struct path path;
1da177e4
LT
1400 struct dentry *dir;
1401 int count = 0;
1402
1403 acc_mode = ACC_MODE(flag);
1404
1405 /* Allow the LSM permission hook to distinguish append
1406 access from general write access. */
1407 if (flag & O_APPEND)
1408 acc_mode |= MAY_APPEND;
1409
1410 /* Fill in the open() intent data */
1411 nd->intent.open.flags = flag;
1412 nd->intent.open.create_mode = mode;
1413
1414 /*
1415 * The simplest case - just a plain lookup.
1416 */
1417 if (!(flag & O_CREAT)) {
1418 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1419 if (error)
1420 return error;
1421 goto ok;
1422 }
1423
1424 /*
1425 * Create - we need to know the parent.
1426 */
1427 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1428 if (error)
1429 return error;
1430
1431 /*
1432 * We have the parent and last component. First of all, check
1433 * that we are not asked to creat(2) an obvious directory - that
1434 * will not do.
1435 */
1436 error = -EISDIR;
1437 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1438 goto exit;
1439
1440 dir = nd->dentry;
1441 nd->flags &= ~LOOKUP_PARENT;
1442 down(&dir->d_inode->i_sem);
4e7506e4 1443 path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
d73ffe16 1444 path.mnt = nd->mnt;
1da177e4
LT
1445
1446do_last:
4e7506e4
AV
1447 error = PTR_ERR(path.dentry);
1448 if (IS_ERR(path.dentry)) {
1da177e4
LT
1449 up(&dir->d_inode->i_sem);
1450 goto exit;
1451 }
1452
1453 /* Negative dentry, just create the file */
4e7506e4 1454 if (!path.dentry->d_inode) {
1da177e4
LT
1455 if (!IS_POSIXACL(dir->d_inode))
1456 mode &= ~current->fs->umask;
4e7506e4 1457 error = vfs_create(dir->d_inode, path.dentry, mode, nd);
1da177e4
LT
1458 up(&dir->d_inode->i_sem);
1459 dput(nd->dentry);
4e7506e4 1460 nd->dentry = path.dentry;
1da177e4
LT
1461 if (error)
1462 goto exit;
1463 /* Don't check for write permission, don't truncate */
1464 acc_mode = 0;
1465 flag &= ~O_TRUNC;
1466 goto ok;
1467 }
1468
1469 /*
1470 * It already exists.
1471 */
1472 up(&dir->d_inode->i_sem);
1473
1474 error = -EEXIST;
1475 if (flag & O_EXCL)
1476 goto exit_dput;
1477
4e7506e4 1478 if (d_mountpoint(path.dentry)) {
1da177e4
LT
1479 error = -ELOOP;
1480 if (flag & O_NOFOLLOW)
1481 goto exit_dput;
d73ffe16
AV
1482 while (__follow_down(&path.mnt,&path.dentry) && d_mountpoint(path.dentry));
1483 nd->mnt = path.mnt;
1da177e4
LT
1484 }
1485 error = -ENOENT;
4e7506e4 1486 if (!path.dentry->d_inode)
1da177e4 1487 goto exit_dput;
4e7506e4 1488 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
1da177e4
LT
1489 goto do_link;
1490
1491 dput(nd->dentry);
4e7506e4 1492 nd->dentry = path.dentry;
1da177e4 1493 error = -EISDIR;
4e7506e4 1494 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
1da177e4
LT
1495 goto exit;
1496ok:
1497 error = may_open(nd, acc_mode, flag);
1498 if (error)
1499 goto exit;
1500 return 0;
1501
1502exit_dput:
4e7506e4 1503 dput(path.dentry);
1da177e4
LT
1504exit:
1505 path_release(nd);
1506 return error;
1507
1508do_link:
1509 error = -ELOOP;
1510 if (flag & O_NOFOLLOW)
1511 goto exit_dput;
1512 /*
1513 * This is subtle. Instead of calling do_follow_link() we do the
1514 * thing by hands. The reason is that this way we have zero link_count
1515 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1516 * After that we have the parent and last component, i.e.
1517 * we are in the same situation as after the first path_walk().
1518 * Well, almost - if the last component is normal we get its copy
1519 * stored in nd->last.name and we will have to putname() it when we
1520 * are done. Procfs-like symlinks just set LAST_BIND.
1521 */
1522 nd->flags |= LOOKUP_PARENT;
4e7506e4 1523 error = security_inode_follow_link(path.dentry, nd);
1da177e4
LT
1524 if (error)
1525 goto exit_dput;
1be4a090 1526 mntget(path.mnt);
4e7506e4
AV
1527 error = __do_follow_link(path.dentry, nd);
1528 dput(path.dentry);
1be4a090 1529 mntput(path.mnt);
d73ffe16 1530 path.mnt = nd->mnt;
1da177e4
LT
1531 if (error)
1532 return error;
1533 nd->flags &= ~LOOKUP_PARENT;
1534 if (nd->last_type == LAST_BIND) {
4e7506e4 1535 path.dentry = nd->dentry;
1da177e4
LT
1536 goto ok;
1537 }
1538 error = -EISDIR;
1539 if (nd->last_type != LAST_NORM)
1540 goto exit;
1541 if (nd->last.name[nd->last.len]) {
1542 putname(nd->last.name);
1543 goto exit;
1544 }
1545 error = -ELOOP;
1546 if (count++==32) {
1547 putname(nd->last.name);
1548 goto exit;
1549 }
1550 dir = nd->dentry;
1551 down(&dir->d_inode->i_sem);
4e7506e4 1552 path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1da177e4
LT
1553 putname(nd->last.name);
1554 goto do_last;
1555}
1556
1557/**
1558 * lookup_create - lookup a dentry, creating it if it doesn't exist
1559 * @nd: nameidata info
1560 * @is_dir: directory flag
1561 *
1562 * Simple function to lookup and return a dentry and create it
1563 * if it doesn't exist. Is SMP-safe.
1564 */
1565struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1566{
1567 struct dentry *dentry;
1568
1569 down(&nd->dentry->d_inode->i_sem);
1570 dentry = ERR_PTR(-EEXIST);
1571 if (nd->last_type != LAST_NORM)
1572 goto fail;
1573 nd->flags &= ~LOOKUP_PARENT;
1574 dentry = lookup_hash(&nd->last, nd->dentry);
1575 if (IS_ERR(dentry))
1576 goto fail;
1577 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1578 goto enoent;
1579 return dentry;
1580enoent:
1581 dput(dentry);
1582 dentry = ERR_PTR(-ENOENT);
1583fail:
1584 return dentry;
1585}
f81a0bff 1586EXPORT_SYMBOL_GPL(lookup_create);
1da177e4
LT
1587
1588int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1589{
1590 int error = may_create(dir, dentry, NULL);
1591
1592 if (error)
1593 return error;
1594
1595 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1596 return -EPERM;
1597
1598 if (!dir->i_op || !dir->i_op->mknod)
1599 return -EPERM;
1600
1601 error = security_inode_mknod(dir, dentry, mode, dev);
1602 if (error)
1603 return error;
1604
1605 DQUOT_INIT(dir);
1606 error = dir->i_op->mknod(dir, dentry, mode, dev);
1607 if (!error) {
1608 inode_dir_notify(dir, DN_CREATE);
1609 security_inode_post_mknod(dir, dentry, mode, dev);
1610 }
1611 return error;
1612}
1613
1614asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1615{
1616 int error = 0;
1617 char * tmp;
1618 struct dentry * dentry;
1619 struct nameidata nd;
1620
1621 if (S_ISDIR(mode))
1622 return -EPERM;
1623 tmp = getname(filename);
1624 if (IS_ERR(tmp))
1625 return PTR_ERR(tmp);
1626
1627 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1628 if (error)
1629 goto out;
1630 dentry = lookup_create(&nd, 0);
1631 error = PTR_ERR(dentry);
1632
1633 if (!IS_POSIXACL(nd.dentry->d_inode))
1634 mode &= ~current->fs->umask;
1635 if (!IS_ERR(dentry)) {
1636 switch (mode & S_IFMT) {
1637 case 0: case S_IFREG:
1638 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1639 break;
1640 case S_IFCHR: case S_IFBLK:
1641 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1642 new_decode_dev(dev));
1643 break;
1644 case S_IFIFO: case S_IFSOCK:
1645 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1646 break;
1647 case S_IFDIR:
1648 error = -EPERM;
1649 break;
1650 default:
1651 error = -EINVAL;
1652 }
1653 dput(dentry);
1654 }
1655 up(&nd.dentry->d_inode->i_sem);
1656 path_release(&nd);
1657out:
1658 putname(tmp);
1659
1660 return error;
1661}
1662
1663int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1664{
1665 int error = may_create(dir, dentry, NULL);
1666
1667 if (error)
1668 return error;
1669
1670 if (!dir->i_op || !dir->i_op->mkdir)
1671 return -EPERM;
1672
1673 mode &= (S_IRWXUGO|S_ISVTX);
1674 error = security_inode_mkdir(dir, dentry, mode);
1675 if (error)
1676 return error;
1677
1678 DQUOT_INIT(dir);
1679 error = dir->i_op->mkdir(dir, dentry, mode);
1680 if (!error) {
1681 inode_dir_notify(dir, DN_CREATE);
1682 security_inode_post_mkdir(dir,dentry, mode);
1683 }
1684 return error;
1685}
1686
1687asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1688{
1689 int error = 0;
1690 char * tmp;
1691
1692 tmp = getname(pathname);
1693 error = PTR_ERR(tmp);
1694 if (!IS_ERR(tmp)) {
1695 struct dentry *dentry;
1696 struct nameidata nd;
1697
1698 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1699 if (error)
1700 goto out;
1701 dentry = lookup_create(&nd, 1);
1702 error = PTR_ERR(dentry);
1703 if (!IS_ERR(dentry)) {
1704 if (!IS_POSIXACL(nd.dentry->d_inode))
1705 mode &= ~current->fs->umask;
1706 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1707 dput(dentry);
1708 }
1709 up(&nd.dentry->d_inode->i_sem);
1710 path_release(&nd);
1711out:
1712 putname(tmp);
1713 }
1714
1715 return error;
1716}
1717
1718/*
1719 * We try to drop the dentry early: we should have
1720 * a usage count of 2 if we're the only user of this
1721 * dentry, and if that is true (possibly after pruning
1722 * the dcache), then we drop the dentry now.
1723 *
1724 * A low-level filesystem can, if it choses, legally
1725 * do a
1726 *
1727 * if (!d_unhashed(dentry))
1728 * return -EBUSY;
1729 *
1730 * if it cannot handle the case of removing a directory
1731 * that is still in use by something else..
1732 */
1733void dentry_unhash(struct dentry *dentry)
1734{
1735 dget(dentry);
1736 if (atomic_read(&dentry->d_count))
1737 shrink_dcache_parent(dentry);
1738 spin_lock(&dcache_lock);
1739 spin_lock(&dentry->d_lock);
1740 if (atomic_read(&dentry->d_count) == 2)
1741 __d_drop(dentry);
1742 spin_unlock(&dentry->d_lock);
1743 spin_unlock(&dcache_lock);
1744}
1745
1746int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1747{
1748 int error = may_delete(dir, dentry, 1);
1749
1750 if (error)
1751 return error;
1752
1753 if (!dir->i_op || !dir->i_op->rmdir)
1754 return -EPERM;
1755
1756 DQUOT_INIT(dir);
1757
1758 down(&dentry->d_inode->i_sem);
1759 dentry_unhash(dentry);
1760 if (d_mountpoint(dentry))
1761 error = -EBUSY;
1762 else {
1763 error = security_inode_rmdir(dir, dentry);
1764 if (!error) {
1765 error = dir->i_op->rmdir(dir, dentry);
1766 if (!error)
1767 dentry->d_inode->i_flags |= S_DEAD;
1768 }
1769 }
1770 up(&dentry->d_inode->i_sem);
1771 if (!error) {
1772 inode_dir_notify(dir, DN_DELETE);
1773 d_delete(dentry);
1774 }
1775 dput(dentry);
1776
1777 return error;
1778}
1779
1780asmlinkage long sys_rmdir(const char __user * pathname)
1781{
1782 int error = 0;
1783 char * name;
1784 struct dentry *dentry;
1785 struct nameidata nd;
1786
1787 name = getname(pathname);
1788 if(IS_ERR(name))
1789 return PTR_ERR(name);
1790
1791 error = path_lookup(name, LOOKUP_PARENT, &nd);
1792 if (error)
1793 goto exit;
1794
1795 switch(nd.last_type) {
1796 case LAST_DOTDOT:
1797 error = -ENOTEMPTY;
1798 goto exit1;
1799 case LAST_DOT:
1800 error = -EINVAL;
1801 goto exit1;
1802 case LAST_ROOT:
1803 error = -EBUSY;
1804 goto exit1;
1805 }
1806 down(&nd.dentry->d_inode->i_sem);
1807 dentry = lookup_hash(&nd.last, nd.dentry);
1808 error = PTR_ERR(dentry);
1809 if (!IS_ERR(dentry)) {
1810 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1811 dput(dentry);
1812 }
1813 up(&nd.dentry->d_inode->i_sem);
1814exit1:
1815 path_release(&nd);
1816exit:
1817 putname(name);
1818 return error;
1819}
1820
1821int vfs_unlink(struct inode *dir, struct dentry *dentry)
1822{
1823 int error = may_delete(dir, dentry, 0);
1824
1825 if (error)
1826 return error;
1827
1828 if (!dir->i_op || !dir->i_op->unlink)
1829 return -EPERM;
1830
1831 DQUOT_INIT(dir);
1832
1833 down(&dentry->d_inode->i_sem);
1834 if (d_mountpoint(dentry))
1835 error = -EBUSY;
1836 else {
1837 error = security_inode_unlink(dir, dentry);
1838 if (!error)
1839 error = dir->i_op->unlink(dir, dentry);
1840 }
1841 up(&dentry->d_inode->i_sem);
1842
1843 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1844 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1845 d_delete(dentry);
1846 inode_dir_notify(dir, DN_DELETE);
1847 }
1848 return error;
1849}
1850
1851/*
1852 * Make sure that the actual truncation of the file will occur outside its
1853 * directory's i_sem. Truncate can take a long time if there is a lot of
1854 * writeout happening, and we don't want to prevent access to the directory
1855 * while waiting on the I/O.
1856 */
1857asmlinkage long sys_unlink(const char __user * pathname)
1858{
1859 int error = 0;
1860 char * name;
1861 struct dentry *dentry;
1862 struct nameidata nd;
1863 struct inode *inode = NULL;
1864
1865 name = getname(pathname);
1866 if(IS_ERR(name))
1867 return PTR_ERR(name);
1868
1869 error = path_lookup(name, LOOKUP_PARENT, &nd);
1870 if (error)
1871 goto exit;
1872 error = -EISDIR;
1873 if (nd.last_type != LAST_NORM)
1874 goto exit1;
1875 down(&nd.dentry->d_inode->i_sem);
1876 dentry = lookup_hash(&nd.last, nd.dentry);
1877 error = PTR_ERR(dentry);
1878 if (!IS_ERR(dentry)) {
1879 /* Why not before? Because we want correct error value */
1880 if (nd.last.name[nd.last.len])
1881 goto slashes;
1882 inode = dentry->d_inode;
1883 if (inode)
1884 atomic_inc(&inode->i_count);
1885 error = vfs_unlink(nd.dentry->d_inode, dentry);
1886 exit2:
1887 dput(dentry);
1888 }
1889 up(&nd.dentry->d_inode->i_sem);
1890 if (inode)
1891 iput(inode); /* truncate the inode here */
1892exit1:
1893 path_release(&nd);
1894exit:
1895 putname(name);
1896 return error;
1897
1898slashes:
1899 error = !dentry->d_inode ? -ENOENT :
1900 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1901 goto exit2;
1902}
1903
1904int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
1905{
1906 int error = may_create(dir, dentry, NULL);
1907
1908 if (error)
1909 return error;
1910
1911 if (!dir->i_op || !dir->i_op->symlink)
1912 return -EPERM;
1913
1914 error = security_inode_symlink(dir, dentry, oldname);
1915 if (error)
1916 return error;
1917
1918 DQUOT_INIT(dir);
1919 error = dir->i_op->symlink(dir, dentry, oldname);
1920 if (!error) {
1921 inode_dir_notify(dir, DN_CREATE);
1922 security_inode_post_symlink(dir, dentry, oldname);
1923 }
1924 return error;
1925}
1926
1927asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1928{
1929 int error = 0;
1930 char * from;
1931 char * to;
1932
1933 from = getname(oldname);
1934 if(IS_ERR(from))
1935 return PTR_ERR(from);
1936 to = getname(newname);
1937 error = PTR_ERR(to);
1938 if (!IS_ERR(to)) {
1939 struct dentry *dentry;
1940 struct nameidata nd;
1941
1942 error = path_lookup(to, LOOKUP_PARENT, &nd);
1943 if (error)
1944 goto out;
1945 dentry = lookup_create(&nd, 0);
1946 error = PTR_ERR(dentry);
1947 if (!IS_ERR(dentry)) {
1948 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
1949 dput(dentry);
1950 }
1951 up(&nd.dentry->d_inode->i_sem);
1952 path_release(&nd);
1953out:
1954 putname(to);
1955 }
1956 putname(from);
1957 return error;
1958}
1959
1960int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1961{
1962 struct inode *inode = old_dentry->d_inode;
1963 int error;
1964
1965 if (!inode)
1966 return -ENOENT;
1967
1968 error = may_create(dir, new_dentry, NULL);
1969 if (error)
1970 return error;
1971
1972 if (dir->i_sb != inode->i_sb)
1973 return -EXDEV;
1974
1975 /*
1976 * A link to an append-only or immutable file cannot be created.
1977 */
1978 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1979 return -EPERM;
1980 if (!dir->i_op || !dir->i_op->link)
1981 return -EPERM;
1982 if (S_ISDIR(old_dentry->d_inode->i_mode))
1983 return -EPERM;
1984
1985 error = security_inode_link(old_dentry, dir, new_dentry);
1986 if (error)
1987 return error;
1988
1989 down(&old_dentry->d_inode->i_sem);
1990 DQUOT_INIT(dir);
1991 error = dir->i_op->link(old_dentry, dir, new_dentry);
1992 up(&old_dentry->d_inode->i_sem);
1993 if (!error) {
1994 inode_dir_notify(dir, DN_CREATE);
1995 security_inode_post_link(old_dentry, dir, new_dentry);
1996 }
1997 return error;
1998}
1999
2000/*
2001 * Hardlinks are often used in delicate situations. We avoid
2002 * security-related surprises by not following symlinks on the
2003 * newname. --KAB
2004 *
2005 * We don't follow them on the oldname either to be compatible
2006 * with linux 2.0, and to avoid hard-linking to directories
2007 * and other special files. --ADM
2008 */
2009asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
2010{
2011 struct dentry *new_dentry;
2012 struct nameidata nd, old_nd;
2013 int error;
2014 char * to;
2015
2016 to = getname(newname);
2017 if (IS_ERR(to))
2018 return PTR_ERR(to);
2019
2020 error = __user_walk(oldname, 0, &old_nd);
2021 if (error)
2022 goto exit;
2023 error = path_lookup(to, LOOKUP_PARENT, &nd);
2024 if (error)
2025 goto out;
2026 error = -EXDEV;
2027 if (old_nd.mnt != nd.mnt)
2028 goto out_release;
2029 new_dentry = lookup_create(&nd, 0);
2030 error = PTR_ERR(new_dentry);
2031 if (!IS_ERR(new_dentry)) {
2032 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2033 dput(new_dentry);
2034 }
2035 up(&nd.dentry->d_inode->i_sem);
2036out_release:
2037 path_release(&nd);
2038out:
2039 path_release(&old_nd);
2040exit:
2041 putname(to);
2042
2043 return error;
2044}
2045
2046/*
2047 * The worst of all namespace operations - renaming directory. "Perverted"
2048 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2049 * Problems:
2050 * a) we can get into loop creation. Check is done in is_subdir().
2051 * b) race potential - two innocent renames can create a loop together.
2052 * That's where 4.4 screws up. Current fix: serialization on
2053 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
2054 * story.
2055 * c) we have to lock _three_ objects - parents and victim (if it exists).
2056 * And that - after we got ->i_sem on parents (until then we don't know
2057 * whether the target exists). Solution: try to be smart with locking
2058 * order for inodes. We rely on the fact that tree topology may change
2059 * only under ->s_vfs_rename_sem _and_ that parent of the object we
2060 * move will be locked. Thus we can rank directories by the tree
2061 * (ancestors first) and rank all non-directories after them.
2062 * That works since everybody except rename does "lock parent, lookup,
2063 * lock child" and rename is under ->s_vfs_rename_sem.
2064 * HOWEVER, it relies on the assumption that any object with ->lookup()
2065 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2066 * we'd better make sure that there's no link(2) for them.
2067 * d) some filesystems don't support opened-but-unlinked directories,
2068 * either because of layout or because they are not ready to deal with
2069 * all cases correctly. The latter will be fixed (taking this sort of
2070 * stuff into VFS), but the former is not going away. Solution: the same
2071 * trick as in rmdir().
2072 * e) conversion from fhandle to dentry may come in the wrong moment - when
2073 * we are removing the target. Solution: we will have to grab ->i_sem
2074 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2075 * ->i_sem on parents, which works but leads to some truely excessive
2076 * locking].
2077 */
75c96f85
AB
2078static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2079 struct inode *new_dir, struct dentry *new_dentry)
1da177e4
LT
2080{
2081 int error = 0;
2082 struct inode *target;
2083
2084 /*
2085 * If we are going to change the parent - check write permissions,
2086 * we'll need to flip '..'.
2087 */
2088 if (new_dir != old_dir) {
2089 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2090 if (error)
2091 return error;
2092 }
2093
2094 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2095 if (error)
2096 return error;
2097
2098 target = new_dentry->d_inode;
2099 if (target) {
2100 down(&target->i_sem);
2101 dentry_unhash(new_dentry);
2102 }
2103 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2104 error = -EBUSY;
2105 else
2106 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2107 if (target) {
2108 if (!error)
2109 target->i_flags |= S_DEAD;
2110 up(&target->i_sem);
2111 if (d_unhashed(new_dentry))
2112 d_rehash(new_dentry);
2113 dput(new_dentry);
2114 }
2115 if (!error) {
2116 d_move(old_dentry,new_dentry);
2117 security_inode_post_rename(old_dir, old_dentry,
2118 new_dir, new_dentry);
2119 }
2120 return error;
2121}
2122
75c96f85
AB
2123static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2124 struct inode *new_dir, struct dentry *new_dentry)
1da177e4
LT
2125{
2126 struct inode *target;
2127 int error;
2128
2129 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2130 if (error)
2131 return error;
2132
2133 dget(new_dentry);
2134 target = new_dentry->d_inode;
2135 if (target)
2136 down(&target->i_sem);
2137 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2138 error = -EBUSY;
2139 else
2140 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2141 if (!error) {
2142 /* The following d_move() should become unconditional */
2143 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2144 d_move(old_dentry, new_dentry);
2145 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
2146 }
2147 if (target)
2148 up(&target->i_sem);
2149 dput(new_dentry);
2150 return error;
2151}
2152
2153int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2154 struct inode *new_dir, struct dentry *new_dentry)
2155{
2156 int error;
2157 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2158
2159 if (old_dentry->d_inode == new_dentry->d_inode)
2160 return 0;
2161
2162 error = may_delete(old_dir, old_dentry, is_dir);
2163 if (error)
2164 return error;
2165
2166 if (!new_dentry->d_inode)
2167 error = may_create(new_dir, new_dentry, NULL);
2168 else
2169 error = may_delete(new_dir, new_dentry, is_dir);
2170 if (error)
2171 return error;
2172
2173 if (!old_dir->i_op || !old_dir->i_op->rename)
2174 return -EPERM;
2175
2176 DQUOT_INIT(old_dir);
2177 DQUOT_INIT(new_dir);
2178
2179 if (is_dir)
2180 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2181 else
2182 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2183 if (!error) {
2184 if (old_dir == new_dir)
2185 inode_dir_notify(old_dir, DN_RENAME);
2186 else {
2187 inode_dir_notify(old_dir, DN_DELETE);
2188 inode_dir_notify(new_dir, DN_CREATE);
2189 }
2190 }
2191 return error;
2192}
2193
2194static inline int do_rename(const char * oldname, const char * newname)
2195{
2196 int error = 0;
2197 struct dentry * old_dir, * new_dir;
2198 struct dentry * old_dentry, *new_dentry;
2199 struct dentry * trap;
2200 struct nameidata oldnd, newnd;
2201
2202 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2203 if (error)
2204 goto exit;
2205
2206 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2207 if (error)
2208 goto exit1;
2209
2210 error = -EXDEV;
2211 if (oldnd.mnt != newnd.mnt)
2212 goto exit2;
2213
2214 old_dir = oldnd.dentry;
2215 error = -EBUSY;
2216 if (oldnd.last_type != LAST_NORM)
2217 goto exit2;
2218
2219 new_dir = newnd.dentry;
2220 if (newnd.last_type != LAST_NORM)
2221 goto exit2;
2222
2223 trap = lock_rename(new_dir, old_dir);
2224
2225 old_dentry = lookup_hash(&oldnd.last, old_dir);
2226 error = PTR_ERR(old_dentry);
2227 if (IS_ERR(old_dentry))
2228 goto exit3;
2229 /* source must exist */
2230 error = -ENOENT;
2231 if (!old_dentry->d_inode)
2232 goto exit4;
2233 /* unless the source is a directory trailing slashes give -ENOTDIR */
2234 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2235 error = -ENOTDIR;
2236 if (oldnd.last.name[oldnd.last.len])
2237 goto exit4;
2238 if (newnd.last.name[newnd.last.len])
2239 goto exit4;
2240 }
2241 /* source should not be ancestor of target */
2242 error = -EINVAL;
2243 if (old_dentry == trap)
2244 goto exit4;
2245 new_dentry = lookup_hash(&newnd.last, new_dir);
2246 error = PTR_ERR(new_dentry);
2247 if (IS_ERR(new_dentry))
2248 goto exit4;
2249 /* target should not be an ancestor of source */
2250 error = -ENOTEMPTY;
2251 if (new_dentry == trap)
2252 goto exit5;
2253
2254 error = vfs_rename(old_dir->d_inode, old_dentry,
2255 new_dir->d_inode, new_dentry);
2256exit5:
2257 dput(new_dentry);
2258exit4:
2259 dput(old_dentry);
2260exit3:
2261 unlock_rename(new_dir, old_dir);
2262exit2:
2263 path_release(&newnd);
2264exit1:
2265 path_release(&oldnd);
2266exit:
2267 return error;
2268}
2269
2270asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2271{
2272 int error;
2273 char * from;
2274 char * to;
2275
2276 from = getname(oldname);
2277 if(IS_ERR(from))
2278 return PTR_ERR(from);
2279 to = getname(newname);
2280 error = PTR_ERR(to);
2281 if (!IS_ERR(to)) {
2282 error = do_rename(from,to);
2283 putname(to);
2284 }
2285 putname(from);
2286 return error;
2287}
2288
2289int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2290{
2291 int len;
2292
2293 len = PTR_ERR(link);
2294 if (IS_ERR(link))
2295 goto out;
2296
2297 len = strlen(link);
2298 if (len > (unsigned) buflen)
2299 len = buflen;
2300 if (copy_to_user(buffer, link, len))
2301 len = -EFAULT;
2302out:
2303 return len;
2304}
2305
2306/*
2307 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2308 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2309 * using) it for any given inode is up to filesystem.
2310 */
2311int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2312{
2313 struct nameidata nd;
2314 int res;
2315 nd.depth = 0;
2316 res = dentry->d_inode->i_op->follow_link(dentry, &nd);
2317 if (!res) {
2318 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2319 if (dentry->d_inode->i_op->put_link)
2320 dentry->d_inode->i_op->put_link(dentry, &nd);
2321 }
2322 return res;
2323}
2324
2325int vfs_follow_link(struct nameidata *nd, const char *link)
2326{
2327 return __vfs_follow_link(nd, link);
2328}
2329
2330/* get the link contents into pagecache */
2331static char *page_getlink(struct dentry * dentry, struct page **ppage)
2332{
2333 struct page * page;
2334 struct address_space *mapping = dentry->d_inode->i_mapping;
2335 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2336 NULL);
2337 if (IS_ERR(page))
2338 goto sync_fail;
2339 wait_on_page_locked(page);
2340 if (!PageUptodate(page))
2341 goto async_fail;
2342 *ppage = page;
2343 return kmap(page);
2344
2345async_fail:
2346 page_cache_release(page);
2347 return ERR_PTR(-EIO);
2348
2349sync_fail:
2350 return (char*)page;
2351}
2352
2353int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2354{
2355 struct page *page = NULL;
2356 char *s = page_getlink(dentry, &page);
2357 int res = vfs_readlink(dentry,buffer,buflen,s);
2358 if (page) {
2359 kunmap(page);
2360 page_cache_release(page);
2361 }
2362 return res;
2363}
2364
2365int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2366{
2367 struct page *page;
2368 nd_set_link(nd, page_getlink(dentry, &page));
2369 return 0;
2370}
2371
2372void page_put_link(struct dentry *dentry, struct nameidata *nd)
2373{
2374 if (!IS_ERR(nd_get_link(nd))) {
2375 struct page *page;
2376 page = find_get_page(dentry->d_inode->i_mapping, 0);
2377 if (!page)
2378 BUG();
2379 kunmap(page);
2380 page_cache_release(page);
2381 page_cache_release(page);
2382 }
2383}
2384
2385int page_symlink(struct inode *inode, const char *symname, int len)
2386{
2387 struct address_space *mapping = inode->i_mapping;
2388 struct page *page = grab_cache_page(mapping, 0);
2389 int err = -ENOMEM;
2390 char *kaddr;
2391
2392 if (!page)
2393 goto fail;
2394 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2395 if (err)
2396 goto fail_map;
2397 kaddr = kmap_atomic(page, KM_USER0);
2398 memcpy(kaddr, symname, len-1);
2399 kunmap_atomic(kaddr, KM_USER0);
2400 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2401 /*
2402 * Notice that we are _not_ going to block here - end of page is
2403 * unmapped, so this will only try to map the rest of page, see
2404 * that it is unmapped (typically even will not look into inode -
2405 * ->i_size will be enough for everything) and zero it out.
2406 * OTOH it's obviously correct and should make the page up-to-date.
2407 */
2408 if (!PageUptodate(page)) {
2409 err = mapping->a_ops->readpage(NULL, page);
2410 wait_on_page_locked(page);
2411 } else {
2412 unlock_page(page);
2413 }
2414 page_cache_release(page);
2415 if (err < 0)
2416 goto fail;
2417 mark_inode_dirty(inode);
2418 return 0;
2419fail_map:
2420 unlock_page(page);
2421 page_cache_release(page);
2422fail:
2423 return err;
2424}
2425
2426struct inode_operations page_symlink_inode_operations = {
2427 .readlink = generic_readlink,
2428 .follow_link = page_follow_link_light,
2429 .put_link = page_put_link,
2430};
2431
2432EXPORT_SYMBOL(__user_walk);
2433EXPORT_SYMBOL(follow_down);
2434EXPORT_SYMBOL(follow_up);
2435EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2436EXPORT_SYMBOL(getname);
2437EXPORT_SYMBOL(lock_rename);
2438EXPORT_SYMBOL(lookup_hash);
2439EXPORT_SYMBOL(lookup_one_len);
2440EXPORT_SYMBOL(page_follow_link_light);
2441EXPORT_SYMBOL(page_put_link);
2442EXPORT_SYMBOL(page_readlink);
2443EXPORT_SYMBOL(page_symlink);
2444EXPORT_SYMBOL(page_symlink_inode_operations);
2445EXPORT_SYMBOL(path_lookup);
2446EXPORT_SYMBOL(path_release);
2447EXPORT_SYMBOL(path_walk);
2448EXPORT_SYMBOL(permission);
2449EXPORT_SYMBOL(unlock_rename);
2450EXPORT_SYMBOL(vfs_create);
2451EXPORT_SYMBOL(vfs_follow_link);
2452EXPORT_SYMBOL(vfs_link);
2453EXPORT_SYMBOL(vfs_mkdir);
2454EXPORT_SYMBOL(vfs_mknod);
2455EXPORT_SYMBOL(generic_permission);
2456EXPORT_SYMBOL(vfs_readlink);
2457EXPORT_SYMBOL(vfs_rename);
2458EXPORT_SYMBOL(vfs_rmdir);
2459EXPORT_SYMBOL(vfs_symlink);
2460EXPORT_SYMBOL(vfs_unlink);
2461EXPORT_SYMBOL(dentry_unhash);
2462EXPORT_SYMBOL(generic_readlink);