[PATCH] namei fixes (16/19)
[linux-2.6/zen-sources.git] / fs / namei.c
blob444086d441e12376b721e58b70ed32d23911167b
1 /*
2 * linux/fs/namei.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * Some corrections by tytso.
9 */
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
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>
34 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
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).
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.
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.
53 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
54 * resolution to correspond with current state of the code.
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.
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.
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.
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.
84 * [10-Sep-98 Alan Modra] Another symlink change.
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).
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...
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...
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..
111 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
112 * PATH_MAX includes the nul terminator --RR.
114 static inline int do_getname(const char __user *filename, char *page)
116 int retval;
117 unsigned long len = PATH_MAX;
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;
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;
136 char * getname(const char __user * filename)
138 char *tmp, *result;
140 result = ERR_PTR(-ENOMEM);
141 tmp = __getname();
142 if (tmp) {
143 int retval = do_getname(filename, tmp);
145 result = tmp;
146 if (retval < 0) {
147 __putname(tmp);
148 result = ERR_PTR(retval);
151 audit_getname(result);
152 return result;
155 #ifdef CONFIG_AUDITSYSCALL
156 void putname(const char *name)
158 if (unlikely(current->audit_context))
159 audit_putname(name);
160 else
161 __putname(name);
163 EXPORT_SYMBOL(putname);
164 #endif
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
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..
178 int generic_permission(struct inode *inode, int mask,
179 int (*check_acl)(struct inode *inode, int mask))
181 umode_t mode = inode->i_mode;
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;
194 if (in_group_p(inode->i_gid))
195 mode >>= 3;
199 * If the DACs are ok we don't need any capability check.
201 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
202 return 0;
204 check_capabilities:
206 * Read/write DACs are always overridable.
207 * Executable DACs are overridable if at least one exec bit is set.
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;
215 * Searching includes executable on directories, else just read.
217 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
218 if (capable(CAP_DAC_READ_SEARCH))
219 return 0;
221 return -EACCES;
224 int permission(struct inode *inode, int mask, struct nameidata *nd)
226 int retval, submask;
228 if (mask & MAY_WRITE) {
229 umode_t mode = inode->i_mode;
232 * Nobody gets write access to a read-only fs.
234 if (IS_RDONLY(inode) &&
235 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
236 return -EROFS;
239 * Nobody gets write access to an immutable file.
241 if (IS_IMMUTABLE(inode))
242 return -EACCES;
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;
255 return security_inode_permission(inode, mask, nd);
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.
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.
276 int get_write_access(struct inode * inode)
278 spin_lock(&inode->i_lock);
279 if (atomic_read(&inode->i_writecount) < 0) {
280 spin_unlock(&inode->i_lock);
281 return -ETXTBSY;
283 atomic_inc(&inode->i_writecount);
284 spin_unlock(&inode->i_lock);
286 return 0;
289 int deny_write_access(struct file * file)
291 struct inode *inode = file->f_dentry->d_inode;
293 spin_lock(&inode->i_lock);
294 if (atomic_read(&inode->i_writecount) > 0) {
295 spin_unlock(&inode->i_lock);
296 return -ETXTBSY;
298 atomic_dec(&inode->i_writecount);
299 spin_unlock(&inode->i_lock);
301 return 0;
304 void path_release(struct nameidata *nd)
306 dput(nd->dentry);
307 mntput(nd->mnt);
311 * umount() mustn't call path_release()/mntput() as that would clear
312 * mnt_expiry_mark
314 void path_release_on_umount(struct nameidata *nd)
316 dput(nd->dentry);
317 _mntput(nd->mnt);
321 * Internal lookup() using the new generic dcache.
322 * SMP-safe
324 static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
326 struct dentry * dentry = __d_lookup(parent, name);
328 /* lockess __d_lookup may fail due to concurrent d_move()
329 * in some unrelated directory, so try with d_lookup
331 if (!dentry)
332 dentry = d_lookup(parent, name);
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;
340 return dentry;
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.
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.
353 static inline int exec_permission_lite(struct inode *inode,
354 struct nameidata *nd)
356 umode_t mode = inode->i_mode;
358 if (inode->i_op && inode->i_op->permission)
359 return -EAGAIN;
361 if (current->fsuid == inode->i_uid)
362 mode >>= 6;
363 else if (in_group_p(inode->i_gid))
364 mode >>= 3;
366 if (mode & MAY_EXEC)
367 goto ok;
369 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
370 goto ok;
372 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
373 goto ok;
375 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
376 goto ok;
378 return -EACCES;
380 return security_inode_permission(inode, MAY_EXEC, nd);
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..
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
391 static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
393 struct dentry * result;
394 struct inode *dir = parent->d_inode;
396 down(&dir->i_sem);
398 * First re-do the cached lookup just in case it was created
399 * while we waited for the directory semaphore..
401 * FIXME! This could use version numbering or similar to
402 * avoid unnecessary cache lookups.
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).
409 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
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;
422 up(&dir->i_sem);
423 return result;
427 * Uhhuh! Nasty case: the cache was re-populated while
428 * we waited on the semaphore. Need to revalidate.
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);
437 return result;
440 static int __emul_lookup_dentry(const char *, struct nameidata *);
442 /* SMP-safe */
443 static inline int
444 walk_init_root(const char *name, struct nameidata *nd)
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);
455 nd->mnt = mntget(current->fs->rootmnt);
456 nd->dentry = dget(current->fs->root);
457 read_unlock(&current->fs->lock);
458 return 1;
461 static inline int __vfs_follow_link(struct nameidata *nd, const char *link)
463 int res = 0;
464 char *name;
465 if (IS_ERR(link))
466 goto fail;
468 if (*link == '/') {
469 path_release(nd);
470 if (!walk_init_root(link, nd))
471 /* weird __emul_prefix() stuff did it */
472 goto out;
474 res = link_path_walk(link, nd);
475 out:
476 if (nd->depth || res || nd->last_type!=LAST_NORM)
477 return res;
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...
483 name = __getname();
484 if (unlikely(!name)) {
485 path_release(nd);
486 return -ENOMEM;
488 strcpy(name, nd->last.name);
489 nd->last.name = name;
490 return 0;
491 fail:
492 path_release(nd);
493 return PTR_ERR(link);
496 struct path {
497 struct vfsmount *mnt;
498 struct dentry *dentry;
501 static inline int __do_follow_link(struct path *path, struct nameidata *nd)
503 int error;
504 struct dentry *dentry = path->dentry;
506 touch_atime(nd->mnt, dentry);
507 nd_set_link(nd, NULL);
509 if (path->mnt == nd->mnt)
510 mntget(path->mnt);
511 error = dentry->d_inode->i_op->follow_link(dentry, nd);
512 if (!error) {
513 char *s = nd_get_link(nd);
514 if (s)
515 error = __vfs_follow_link(nd, s);
516 if (dentry->d_inode->i_op->put_link)
517 dentry->d_inode->i_op->put_link(dentry, nd);
519 dput(dentry);
520 mntput(path->mnt);
522 return error;
526 * This limits recursive symlink follows to 8, while
527 * limiting consecutive symlinks to 40.
529 * Without that kind of total limit, nasty chains of consecutive
530 * symlinks can cause almost arbitrarily long lookups.
532 static inline int do_follow_link(struct path *path, struct nameidata *nd)
534 int err = -ELOOP;
535 if (current->link_count >= MAX_NESTED_LINKS)
536 goto loop;
537 if (current->total_link_count >= 40)
538 goto loop;
539 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
540 cond_resched();
541 err = security_inode_follow_link(path->dentry, nd);
542 if (err)
543 goto loop;
544 current->link_count++;
545 current->total_link_count++;
546 nd->depth++;
547 err = __do_follow_link(path, nd);
548 current->link_count--;
549 nd->depth--;
550 return err;
551 loop:
552 dput(path->dentry);
553 if (path->mnt != nd->mnt)
554 mntput(path->mnt);
555 path_release(nd);
556 return err;
559 int follow_up(struct vfsmount **mnt, struct dentry **dentry)
561 struct vfsmount *parent;
562 struct dentry *mountpoint;
563 spin_lock(&vfsmount_lock);
564 parent=(*mnt)->mnt_parent;
565 if (parent == *mnt) {
566 spin_unlock(&vfsmount_lock);
567 return 0;
569 mntget(parent);
570 mountpoint=dget((*mnt)->mnt_mountpoint);
571 spin_unlock(&vfsmount_lock);
572 dput(*dentry);
573 *dentry = mountpoint;
574 mntput(*mnt);
575 *mnt = parent;
576 return 1;
579 /* no need for dcache_lock, as serialization is taken care in
580 * namespace.c
582 static int __follow_mount(struct path *path)
584 int res = 0;
585 while (d_mountpoint(path->dentry)) {
586 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
587 if (!mounted)
588 break;
589 dput(path->dentry);
590 if (res)
591 mntput(path->mnt);
592 path->mnt = mounted;
593 path->dentry = dget(mounted->mnt_root);
594 res = 1;
596 return res;
599 static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
601 int res = 0;
602 while (d_mountpoint(*dentry)) {
603 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
604 if (!mounted)
605 break;
606 mntput(*mnt);
607 *mnt = mounted;
608 dput(*dentry);
609 *dentry = dget(mounted->mnt_root);
610 res = 1;
612 return res;
615 /* no need for dcache_lock, as serialization is taken care in
616 * namespace.c
618 int follow_down(struct vfsmount **mnt, struct dentry **dentry)
620 struct vfsmount *mounted;
622 mounted = lookup_mnt(*mnt, *dentry);
623 if (mounted) {
624 dput(*dentry);
625 mntput(*mnt);
626 *mnt = mounted;
627 *dentry = dget(mounted->mnt_root);
628 return 1;
630 return 0;
633 static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
635 while(1) {
636 struct vfsmount *parent;
637 struct dentry *old = *dentry;
639 read_lock(&current->fs->lock);
640 if (*dentry == current->fs->root &&
641 *mnt == current->fs->rootmnt) {
642 read_unlock(&current->fs->lock);
643 break;
645 read_unlock(&current->fs->lock);
646 spin_lock(&dcache_lock);
647 if (*dentry != (*mnt)->mnt_root) {
648 *dentry = dget((*dentry)->d_parent);
649 spin_unlock(&dcache_lock);
650 dput(old);
651 break;
653 spin_unlock(&dcache_lock);
654 spin_lock(&vfsmount_lock);
655 parent = (*mnt)->mnt_parent;
656 if (parent == *mnt) {
657 spin_unlock(&vfsmount_lock);
658 break;
660 mntget(parent);
661 *dentry = dget((*mnt)->mnt_mountpoint);
662 spin_unlock(&vfsmount_lock);
663 dput(old);
664 mntput(*mnt);
665 *mnt = parent;
667 follow_mount(mnt, dentry);
671 * It's more convoluted than I'd like it to be, but... it's still fairly
672 * small and for now I'd prefer to have fast path as straight as possible.
673 * It _is_ time-critical.
675 static int do_lookup(struct nameidata *nd, struct qstr *name,
676 struct path *path)
678 struct vfsmount *mnt = nd->mnt;
679 struct dentry *dentry = __d_lookup(nd->dentry, name);
681 if (!dentry)
682 goto need_lookup;
683 if (dentry->d_op && dentry->d_op->d_revalidate)
684 goto need_revalidate;
685 done:
686 path->mnt = mnt;
687 path->dentry = dentry;
688 return 0;
690 need_lookup:
691 dentry = real_lookup(nd->dentry, name, nd);
692 if (IS_ERR(dentry))
693 goto fail;
694 goto done;
696 need_revalidate:
697 if (dentry->d_op->d_revalidate(dentry, nd))
698 goto done;
699 if (d_invalidate(dentry))
700 goto done;
701 dput(dentry);
702 goto need_lookup;
704 fail:
705 return PTR_ERR(dentry);
709 * Name resolution.
710 * This is the basic name resolution function, turning a pathname into
711 * the final dentry. We expect 'base' to be positive and a directory.
713 * Returns 0 and nd will have valid dentry and mnt on success.
714 * Returns error and drops reference to input namei data on failure.
716 static fastcall int __link_path_walk(const char * name, struct nameidata *nd)
718 struct path next;
719 struct inode *inode;
720 int err;
721 unsigned int lookup_flags = nd->flags;
723 while (*name=='/')
724 name++;
725 if (!*name)
726 goto return_reval;
728 inode = nd->dentry->d_inode;
729 if (nd->depth)
730 lookup_flags = LOOKUP_FOLLOW;
732 /* At this point we know we have a real path component. */
733 for(;;) {
734 unsigned long hash;
735 struct qstr this;
736 unsigned int c;
738 err = exec_permission_lite(inode, nd);
739 if (err == -EAGAIN) {
740 err = permission(inode, MAY_EXEC, nd);
742 if (err)
743 break;
745 this.name = name;
746 c = *(const unsigned char *)name;
748 hash = init_name_hash();
749 do {
750 name++;
751 hash = partial_name_hash(c, hash);
752 c = *(const unsigned char *)name;
753 } while (c && (c != '/'));
754 this.len = name - (const char *) this.name;
755 this.hash = end_name_hash(hash);
757 /* remove trailing slashes? */
758 if (!c)
759 goto last_component;
760 while (*++name == '/');
761 if (!*name)
762 goto last_with_slashes;
765 * "." and ".." are special - ".." especially so because it has
766 * to be able to know about the current root directory and
767 * parent relationships.
769 if (this.name[0] == '.') switch (this.len) {
770 default:
771 break;
772 case 2:
773 if (this.name[1] != '.')
774 break;
775 follow_dotdot(&nd->mnt, &nd->dentry);
776 inode = nd->dentry->d_inode;
777 /* fallthrough */
778 case 1:
779 continue;
782 * See if the low-level filesystem might want
783 * to use its own hash..
785 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
786 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
787 if (err < 0)
788 break;
790 nd->flags |= LOOKUP_CONTINUE;
791 /* This does the actual lookups.. */
792 err = do_lookup(nd, &this, &next);
793 if (err)
794 break;
795 /* Check mountpoints.. */
796 __follow_mount(&next);
798 err = -ENOENT;
799 inode = next.dentry->d_inode;
800 if (!inode)
801 goto out_dput;
802 err = -ENOTDIR;
803 if (!inode->i_op)
804 goto out_dput;
806 if (inode->i_op->follow_link) {
807 err = do_follow_link(&next, nd);
808 if (err)
809 goto return_err;
810 err = -ENOENT;
811 inode = nd->dentry->d_inode;
812 if (!inode)
813 break;
814 err = -ENOTDIR;
815 if (!inode->i_op)
816 break;
817 } else {
818 dput(nd->dentry);
819 if (nd->mnt != next.mnt)
820 mntput(nd->mnt);
821 nd->mnt = next.mnt;
822 nd->dentry = next.dentry;
824 err = -ENOTDIR;
825 if (!inode->i_op->lookup)
826 break;
827 continue;
828 /* here ends the main loop */
830 last_with_slashes:
831 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
832 last_component:
833 nd->flags &= ~LOOKUP_CONTINUE;
834 if (lookup_flags & LOOKUP_PARENT)
835 goto lookup_parent;
836 if (this.name[0] == '.') switch (this.len) {
837 default:
838 break;
839 case 2:
840 if (this.name[1] != '.')
841 break;
842 follow_dotdot(&nd->mnt, &nd->dentry);
843 inode = nd->dentry->d_inode;
844 /* fallthrough */
845 case 1:
846 goto return_reval;
848 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
849 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
850 if (err < 0)
851 break;
853 err = do_lookup(nd, &this, &next);
854 if (err)
855 break;
856 __follow_mount(&next);
857 inode = next.dentry->d_inode;
858 if ((lookup_flags & LOOKUP_FOLLOW)
859 && inode && inode->i_op && inode->i_op->follow_link) {
860 err = do_follow_link(&next, nd);
861 if (err)
862 goto return_err;
863 inode = nd->dentry->d_inode;
864 } else {
865 dput(nd->dentry);
866 if (nd->mnt != next.mnt)
867 mntput(nd->mnt);
868 nd->mnt = next.mnt;
869 nd->dentry = next.dentry;
871 err = -ENOENT;
872 if (!inode)
873 break;
874 if (lookup_flags & LOOKUP_DIRECTORY) {
875 err = -ENOTDIR;
876 if (!inode->i_op || !inode->i_op->lookup)
877 break;
879 goto return_base;
880 lookup_parent:
881 nd->last = this;
882 nd->last_type = LAST_NORM;
883 if (this.name[0] != '.')
884 goto return_base;
885 if (this.len == 1)
886 nd->last_type = LAST_DOT;
887 else if (this.len == 2 && this.name[1] == '.')
888 nd->last_type = LAST_DOTDOT;
889 else
890 goto return_base;
891 return_reval:
893 * We bypassed the ordinary revalidation routines.
894 * We may need to check the cached dentry for staleness.
896 if (nd->dentry && nd->dentry->d_sb &&
897 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
898 err = -ESTALE;
899 /* Note: we do not d_invalidate() */
900 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
901 break;
903 return_base:
904 return 0;
905 out_dput:
906 dput(next.dentry);
907 if (nd->mnt != next.mnt)
908 mntput(next.mnt);
909 break;
911 path_release(nd);
912 return_err:
913 return err;
917 * Wrapper to retry pathname resolution whenever the underlying
918 * file system returns an ESTALE.
920 * Retry the whole path once, forcing real lookup requests
921 * instead of relying on the dcache.
923 int fastcall link_path_walk(const char *name, struct nameidata *nd)
925 struct nameidata save = *nd;
926 int result;
928 /* make sure the stuff we saved doesn't go away */
929 dget(save.dentry);
930 mntget(save.mnt);
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);
941 dput(save.dentry);
942 mntput(save.mnt);
944 return result;
947 int fastcall path_walk(const char * name, struct nameidata *nd)
949 current->total_link_count = 0;
950 return link_path_walk(name, nd);
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;
957 static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
959 if (path_walk(name, nd))
960 return 0; /* something went wrong... */
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;
968 * NAME was not found in alternate root or it's a directory. Try to find
969 * it in the normal root:
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;
982 path_release(nd);
984 nd->dentry = old_dentry;
985 nd->mnt = old_mnt;
986 nd->last = last;
987 nd->last_type = last_type;
989 return 1;
992 void set_fs_altroot(void)
994 char *emul = __emul_prefix();
995 struct nameidata nd;
996 struct vfsmount *mnt = NULL, *oldmnt;
997 struct dentry *dentry = NULL, *olddentry;
998 int err;
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;
1007 set_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);
1020 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1021 int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
1023 int retval = 0;
1025 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1026 nd->flags = flags;
1027 nd->depth = 0;
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))
1036 goto out; /* found in altroot */
1037 read_lock(&current->fs->lock);
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);
1045 read_unlock(&current->fs->lock);
1046 current->total_link_count = 0;
1047 retval = link_path_walk(name, nd);
1048 out:
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;
1056 * Restricted form of lookup. Doesn't follow links, single-component only,
1057 * needs parent already locked. Doesn't follow mounts.
1058 * SMP-safe.
1060 static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
1062 struct dentry * dentry;
1063 struct inode *inode;
1064 int err;
1066 inode = base->d_inode;
1067 err = permission(inode, MAY_EXEC, nd);
1068 dentry = ERR_PTR(err);
1069 if (err)
1070 goto out;
1073 * See if the low-level filesystem might want
1074 * to use its own hash..
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;
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);
1095 out:
1096 return dentry;
1099 struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
1101 return __lookup_hash(name, base, NULL);
1104 /* SMP-safe */
1105 struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1107 unsigned long hash;
1108 struct qstr this;
1109 unsigned int c;
1111 this.name = name;
1112 this.len = len;
1113 if (!len)
1114 goto access;
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);
1123 this.hash = end_name_hash(hash);
1125 return lookup_hash(&this, base);
1126 access:
1127 return ERR_PTR(-EACCES);
1131 * namei()
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.
1137 * namei exists in two versions: namei/lnamei. The only difference is
1138 * that namei follows links, while lnamei does not.
1139 * SMP-safe
1141 int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1143 char *tmp = getname(name);
1144 int err = PTR_ERR(tmp);
1146 if (!IS_ERR(tmp)) {
1147 err = path_lookup(tmp, flags, nd);
1148 putname(tmp);
1150 return err;
1154 * It's inline, so penalty for filesystems that don't use sticky bit is
1155 * minimal.
1157 static inline int check_sticky(struct inode *dir, struct inode *inode)
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);
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().
1187 static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1189 int error;
1191 if (!victim->d_inode)
1192 return -ENOENT;
1194 BUG_ON(victim->d_parent->d_inode != dir);
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;
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())
1226 static inline int may_create(struct inode *dir, struct dentry *child,
1227 struct nameidata *nd)
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);
1237 * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security
1238 * reasons.
1240 * O_DIRECTORY translates into forcing a directory lookup.
1242 static inline int lookup_flags(unsigned int f)
1244 unsigned long retval = LOOKUP_FOLLOW;
1246 if (f & O_NOFOLLOW)
1247 retval &= ~LOOKUP_FOLLOW;
1249 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1250 retval &= ~LOOKUP_FOLLOW;
1252 if (f & O_DIRECTORY)
1253 retval |= LOOKUP_DIRECTORY;
1255 return retval;
1259 * p1 and p2 should be directories on the same fs.
1261 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1263 struct dentry *p;
1265 if (p1 == p2) {
1266 down(&p1->d_inode->i_sem);
1267 return NULL;
1270 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
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;
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;
1288 down(&p1->d_inode->i_sem);
1289 down(&p2->d_inode->i_sem);
1290 return NULL;
1293 void unlock_rename(struct dentry *p1, struct dentry *p2)
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);
1302 int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1303 struct nameidata *nd)
1305 int error = may_create(dir, dentry, nd);
1307 if (error)
1308 return error;
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);
1319 if (!error) {
1320 inode_dir_notify(dir, DN_CREATE);
1321 security_inode_post_create(dir, dentry, mode);
1323 return error;
1326 int may_open(struct nameidata *nd, int acc_mode, int flag)
1328 struct dentry *dentry = nd->dentry;
1329 struct inode *inode = dentry->d_inode;
1330 int error;
1332 if (!inode)
1333 return -ENOENT;
1335 if (S_ISLNK(inode->i_mode))
1336 return -ELOOP;
1338 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1339 return -EISDIR;
1341 error = permission(inode, acc_mode, nd);
1342 if (error)
1343 return error;
1346 * FIFO's, sockets and device files are special: they don't
1347 * actually live on the filesystem itself, and as such you
1348 * can write to them even if the filesystem is read-only.
1350 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1351 flag &= ~O_TRUNC;
1352 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1353 if (nd->mnt->mnt_flags & MNT_NODEV)
1354 return -EACCES;
1356 flag &= ~O_TRUNC;
1357 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1358 return -EROFS;
1360 * An append-only file must be opened in append mode for writing.
1362 if (IS_APPEND(inode)) {
1363 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1364 return -EPERM;
1365 if (flag & O_TRUNC)
1366 return -EPERM;
1369 /* O_NOATIME can only be set by the owner or superuser */
1370 if (flag & O_NOATIME)
1371 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1372 return -EPERM;
1375 * Ensure there are no outstanding leases on the file.
1377 error = break_lease(inode, flag);
1378 if (error)
1379 return error;
1381 if (flag & O_TRUNC) {
1382 error = get_write_access(inode);
1383 if (error)
1384 return error;
1387 * Refuse to truncate files with mandatory locks held on them.
1389 error = locks_verify_locked(inode);
1390 if (!error) {
1391 DQUOT_INIT(inode);
1393 error = do_truncate(dentry, 0);
1395 put_write_access(inode);
1396 if (error)
1397 return error;
1398 } else
1399 if (flag & FMODE_WRITE)
1400 DQUOT_INIT(inode);
1402 return 0;
1406 * open_namei()
1408 * namei for open - this is in fact almost the whole open-routine.
1410 * Note that the low bits of "flag" aren't the same as in the open
1411 * system call - they are 00 - no permissions needed
1412 * 01 - read permission needed
1413 * 10 - write permission needed
1414 * 11 - read/write permissions needed
1415 * which is a lot more logical, and also allows the "no perm" needed
1416 * for symlinks (where the permissions are checked later).
1417 * SMP-safe
1419 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1421 int acc_mode, error = 0;
1422 struct path path;
1423 struct dentry *dir;
1424 int count = 0;
1426 acc_mode = ACC_MODE(flag);
1428 /* Allow the LSM permission hook to distinguish append
1429 access from general write access. */
1430 if (flag & O_APPEND)
1431 acc_mode |= MAY_APPEND;
1433 /* Fill in the open() intent data */
1434 nd->intent.open.flags = flag;
1435 nd->intent.open.create_mode = mode;
1438 * The simplest case - just a plain lookup.
1440 if (!(flag & O_CREAT)) {
1441 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1442 if (error)
1443 return error;
1444 goto ok;
1448 * Create - we need to know the parent.
1450 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1451 if (error)
1452 return error;
1455 * We have the parent and last component. First of all, check
1456 * that we are not asked to creat(2) an obvious directory - that
1457 * will not do.
1459 error = -EISDIR;
1460 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1461 goto exit;
1463 dir = nd->dentry;
1464 nd->flags &= ~LOOKUP_PARENT;
1465 down(&dir->d_inode->i_sem);
1466 path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1467 path.mnt = nd->mnt;
1469 do_last:
1470 error = PTR_ERR(path.dentry);
1471 if (IS_ERR(path.dentry)) {
1472 up(&dir->d_inode->i_sem);
1473 goto exit;
1476 /* Negative dentry, just create the file */
1477 if (!path.dentry->d_inode) {
1478 if (!IS_POSIXACL(dir->d_inode))
1479 mode &= ~current->fs->umask;
1480 error = vfs_create(dir->d_inode, path.dentry, mode, nd);
1481 up(&dir->d_inode->i_sem);
1482 dput(nd->dentry);
1483 nd->dentry = path.dentry;
1484 if (error)
1485 goto exit;
1486 /* Don't check for write permission, don't truncate */
1487 acc_mode = 0;
1488 flag &= ~O_TRUNC;
1489 goto ok;
1493 * It already exists.
1495 up(&dir->d_inode->i_sem);
1497 error = -EEXIST;
1498 if (flag & O_EXCL)
1499 goto exit_dput;
1501 if (__follow_mount(&path)) {
1502 error = -ELOOP;
1503 if (flag & O_NOFOLLOW)
1504 goto exit_dput;
1506 error = -ENOENT;
1507 if (!path.dentry->d_inode)
1508 goto exit_dput;
1509 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
1510 goto do_link;
1512 dput(nd->dentry);
1513 nd->dentry = path.dentry;
1514 if (nd->mnt != path.mnt)
1515 mntput(nd->mnt);
1516 nd->mnt = path.mnt;
1517 error = -EISDIR;
1518 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
1519 goto exit;
1521 error = may_open(nd, acc_mode, flag);
1522 if (error)
1523 goto exit;
1524 return 0;
1526 exit_dput:
1527 dput(path.dentry);
1528 if (nd->mnt != path.mnt)
1529 mntput(path.mnt);
1530 exit:
1531 path_release(nd);
1532 return error;
1534 do_link:
1535 error = -ELOOP;
1536 if (flag & O_NOFOLLOW)
1537 goto exit_dput;
1539 * This is subtle. Instead of calling do_follow_link() we do the
1540 * thing by hands. The reason is that this way we have zero link_count
1541 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1542 * After that we have the parent and last component, i.e.
1543 * we are in the same situation as after the first path_walk().
1544 * Well, almost - if the last component is normal we get its copy
1545 * stored in nd->last.name and we will have to putname() it when we
1546 * are done. Procfs-like symlinks just set LAST_BIND.
1548 nd->flags |= LOOKUP_PARENT;
1549 error = security_inode_follow_link(path.dentry, nd);
1550 if (error)
1551 goto exit_dput;
1552 error = __do_follow_link(&path, nd);
1553 if (error)
1554 return error;
1555 nd->flags &= ~LOOKUP_PARENT;
1556 if (nd->last_type == LAST_BIND)
1557 goto ok;
1558 error = -EISDIR;
1559 if (nd->last_type != LAST_NORM)
1560 goto exit;
1561 if (nd->last.name[nd->last.len]) {
1562 putname(nd->last.name);
1563 goto exit;
1565 error = -ELOOP;
1566 if (count++==32) {
1567 putname(nd->last.name);
1568 goto exit;
1570 dir = nd->dentry;
1571 down(&dir->d_inode->i_sem);
1572 path.dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1573 path.mnt = nd->mnt;
1574 putname(nd->last.name);
1575 goto do_last;
1579 * lookup_create - lookup a dentry, creating it if it doesn't exist
1580 * @nd: nameidata info
1581 * @is_dir: directory flag
1583 * Simple function to lookup and return a dentry and create it
1584 * if it doesn't exist. Is SMP-safe.
1586 struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1588 struct dentry *dentry;
1590 down(&nd->dentry->d_inode->i_sem);
1591 dentry = ERR_PTR(-EEXIST);
1592 if (nd->last_type != LAST_NORM)
1593 goto fail;
1594 nd->flags &= ~LOOKUP_PARENT;
1595 dentry = lookup_hash(&nd->last, nd->dentry);
1596 if (IS_ERR(dentry))
1597 goto fail;
1598 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1599 goto enoent;
1600 return dentry;
1601 enoent:
1602 dput(dentry);
1603 dentry = ERR_PTR(-ENOENT);
1604 fail:
1605 return dentry;
1607 EXPORT_SYMBOL_GPL(lookup_create);
1609 int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1611 int error = may_create(dir, dentry, NULL);
1613 if (error)
1614 return error;
1616 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1617 return -EPERM;
1619 if (!dir->i_op || !dir->i_op->mknod)
1620 return -EPERM;
1622 error = security_inode_mknod(dir, dentry, mode, dev);
1623 if (error)
1624 return error;
1626 DQUOT_INIT(dir);
1627 error = dir->i_op->mknod(dir, dentry, mode, dev);
1628 if (!error) {
1629 inode_dir_notify(dir, DN_CREATE);
1630 security_inode_post_mknod(dir, dentry, mode, dev);
1632 return error;
1635 asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1637 int error = 0;
1638 char * tmp;
1639 struct dentry * dentry;
1640 struct nameidata nd;
1642 if (S_ISDIR(mode))
1643 return -EPERM;
1644 tmp = getname(filename);
1645 if (IS_ERR(tmp))
1646 return PTR_ERR(tmp);
1648 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1649 if (error)
1650 goto out;
1651 dentry = lookup_create(&nd, 0);
1652 error = PTR_ERR(dentry);
1654 if (!IS_POSIXACL(nd.dentry->d_inode))
1655 mode &= ~current->fs->umask;
1656 if (!IS_ERR(dentry)) {
1657 switch (mode & S_IFMT) {
1658 case 0: case S_IFREG:
1659 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1660 break;
1661 case S_IFCHR: case S_IFBLK:
1662 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1663 new_decode_dev(dev));
1664 break;
1665 case S_IFIFO: case S_IFSOCK:
1666 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1667 break;
1668 case S_IFDIR:
1669 error = -EPERM;
1670 break;
1671 default:
1672 error = -EINVAL;
1674 dput(dentry);
1676 up(&nd.dentry->d_inode->i_sem);
1677 path_release(&nd);
1678 out:
1679 putname(tmp);
1681 return error;
1684 int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1686 int error = may_create(dir, dentry, NULL);
1688 if (error)
1689 return error;
1691 if (!dir->i_op || !dir->i_op->mkdir)
1692 return -EPERM;
1694 mode &= (S_IRWXUGO|S_ISVTX);
1695 error = security_inode_mkdir(dir, dentry, mode);
1696 if (error)
1697 return error;
1699 DQUOT_INIT(dir);
1700 error = dir->i_op->mkdir(dir, dentry, mode);
1701 if (!error) {
1702 inode_dir_notify(dir, DN_CREATE);
1703 security_inode_post_mkdir(dir,dentry, mode);
1705 return error;
1708 asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1710 int error = 0;
1711 char * tmp;
1713 tmp = getname(pathname);
1714 error = PTR_ERR(tmp);
1715 if (!IS_ERR(tmp)) {
1716 struct dentry *dentry;
1717 struct nameidata nd;
1719 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1720 if (error)
1721 goto out;
1722 dentry = lookup_create(&nd, 1);
1723 error = PTR_ERR(dentry);
1724 if (!IS_ERR(dentry)) {
1725 if (!IS_POSIXACL(nd.dentry->d_inode))
1726 mode &= ~current->fs->umask;
1727 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1728 dput(dentry);
1730 up(&nd.dentry->d_inode->i_sem);
1731 path_release(&nd);
1732 out:
1733 putname(tmp);
1736 return error;
1740 * We try to drop the dentry early: we should have
1741 * a usage count of 2 if we're the only user of this
1742 * dentry, and if that is true (possibly after pruning
1743 * the dcache), then we drop the dentry now.
1745 * A low-level filesystem can, if it choses, legally
1746 * do a
1748 * if (!d_unhashed(dentry))
1749 * return -EBUSY;
1751 * if it cannot handle the case of removing a directory
1752 * that is still in use by something else..
1754 void dentry_unhash(struct dentry *dentry)
1756 dget(dentry);
1757 if (atomic_read(&dentry->d_count))
1758 shrink_dcache_parent(dentry);
1759 spin_lock(&dcache_lock);
1760 spin_lock(&dentry->d_lock);
1761 if (atomic_read(&dentry->d_count) == 2)
1762 __d_drop(dentry);
1763 spin_unlock(&dentry->d_lock);
1764 spin_unlock(&dcache_lock);
1767 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1769 int error = may_delete(dir, dentry, 1);
1771 if (error)
1772 return error;
1774 if (!dir->i_op || !dir->i_op->rmdir)
1775 return -EPERM;
1777 DQUOT_INIT(dir);
1779 down(&dentry->d_inode->i_sem);
1780 dentry_unhash(dentry);
1781 if (d_mountpoint(dentry))
1782 error = -EBUSY;
1783 else {
1784 error = security_inode_rmdir(dir, dentry);
1785 if (!error) {
1786 error = dir->i_op->rmdir(dir, dentry);
1787 if (!error)
1788 dentry->d_inode->i_flags |= S_DEAD;
1791 up(&dentry->d_inode->i_sem);
1792 if (!error) {
1793 inode_dir_notify(dir, DN_DELETE);
1794 d_delete(dentry);
1796 dput(dentry);
1798 return error;
1801 asmlinkage long sys_rmdir(const char __user * pathname)
1803 int error = 0;
1804 char * name;
1805 struct dentry *dentry;
1806 struct nameidata nd;
1808 name = getname(pathname);
1809 if(IS_ERR(name))
1810 return PTR_ERR(name);
1812 error = path_lookup(name, LOOKUP_PARENT, &nd);
1813 if (error)
1814 goto exit;
1816 switch(nd.last_type) {
1817 case LAST_DOTDOT:
1818 error = -ENOTEMPTY;
1819 goto exit1;
1820 case LAST_DOT:
1821 error = -EINVAL;
1822 goto exit1;
1823 case LAST_ROOT:
1824 error = -EBUSY;
1825 goto exit1;
1827 down(&nd.dentry->d_inode->i_sem);
1828 dentry = lookup_hash(&nd.last, nd.dentry);
1829 error = PTR_ERR(dentry);
1830 if (!IS_ERR(dentry)) {
1831 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1832 dput(dentry);
1834 up(&nd.dentry->d_inode->i_sem);
1835 exit1:
1836 path_release(&nd);
1837 exit:
1838 putname(name);
1839 return error;
1842 int vfs_unlink(struct inode *dir, struct dentry *dentry)
1844 int error = may_delete(dir, dentry, 0);
1846 if (error)
1847 return error;
1849 if (!dir->i_op || !dir->i_op->unlink)
1850 return -EPERM;
1852 DQUOT_INIT(dir);
1854 down(&dentry->d_inode->i_sem);
1855 if (d_mountpoint(dentry))
1856 error = -EBUSY;
1857 else {
1858 error = security_inode_unlink(dir, dentry);
1859 if (!error)
1860 error = dir->i_op->unlink(dir, dentry);
1862 up(&dentry->d_inode->i_sem);
1864 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
1865 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1866 d_delete(dentry);
1867 inode_dir_notify(dir, DN_DELETE);
1869 return error;
1873 * Make sure that the actual truncation of the file will occur outside its
1874 * directory's i_sem. Truncate can take a long time if there is a lot of
1875 * writeout happening, and we don't want to prevent access to the directory
1876 * while waiting on the I/O.
1878 asmlinkage long sys_unlink(const char __user * pathname)
1880 int error = 0;
1881 char * name;
1882 struct dentry *dentry;
1883 struct nameidata nd;
1884 struct inode *inode = NULL;
1886 name = getname(pathname);
1887 if(IS_ERR(name))
1888 return PTR_ERR(name);
1890 error = path_lookup(name, LOOKUP_PARENT, &nd);
1891 if (error)
1892 goto exit;
1893 error = -EISDIR;
1894 if (nd.last_type != LAST_NORM)
1895 goto exit1;
1896 down(&nd.dentry->d_inode->i_sem);
1897 dentry = lookup_hash(&nd.last, nd.dentry);
1898 error = PTR_ERR(dentry);
1899 if (!IS_ERR(dentry)) {
1900 /* Why not before? Because we want correct error value */
1901 if (nd.last.name[nd.last.len])
1902 goto slashes;
1903 inode = dentry->d_inode;
1904 if (inode)
1905 atomic_inc(&inode->i_count);
1906 error = vfs_unlink(nd.dentry->d_inode, dentry);
1907 exit2:
1908 dput(dentry);
1910 up(&nd.dentry->d_inode->i_sem);
1911 if (inode)
1912 iput(inode); /* truncate the inode here */
1913 exit1:
1914 path_release(&nd);
1915 exit:
1916 putname(name);
1917 return error;
1919 slashes:
1920 error = !dentry->d_inode ? -ENOENT :
1921 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1922 goto exit2;
1925 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
1927 int error = may_create(dir, dentry, NULL);
1929 if (error)
1930 return error;
1932 if (!dir->i_op || !dir->i_op->symlink)
1933 return -EPERM;
1935 error = security_inode_symlink(dir, dentry, oldname);
1936 if (error)
1937 return error;
1939 DQUOT_INIT(dir);
1940 error = dir->i_op->symlink(dir, dentry, oldname);
1941 if (!error) {
1942 inode_dir_notify(dir, DN_CREATE);
1943 security_inode_post_symlink(dir, dentry, oldname);
1945 return error;
1948 asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1950 int error = 0;
1951 char * from;
1952 char * to;
1954 from = getname(oldname);
1955 if(IS_ERR(from))
1956 return PTR_ERR(from);
1957 to = getname(newname);
1958 error = PTR_ERR(to);
1959 if (!IS_ERR(to)) {
1960 struct dentry *dentry;
1961 struct nameidata nd;
1963 error = path_lookup(to, LOOKUP_PARENT, &nd);
1964 if (error)
1965 goto out;
1966 dentry = lookup_create(&nd, 0);
1967 error = PTR_ERR(dentry);
1968 if (!IS_ERR(dentry)) {
1969 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
1970 dput(dentry);
1972 up(&nd.dentry->d_inode->i_sem);
1973 path_release(&nd);
1974 out:
1975 putname(to);
1977 putname(from);
1978 return error;
1981 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1983 struct inode *inode = old_dentry->d_inode;
1984 int error;
1986 if (!inode)
1987 return -ENOENT;
1989 error = may_create(dir, new_dentry, NULL);
1990 if (error)
1991 return error;
1993 if (dir->i_sb != inode->i_sb)
1994 return -EXDEV;
1997 * A link to an append-only or immutable file cannot be created.
1999 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2000 return -EPERM;
2001 if (!dir->i_op || !dir->i_op->link)
2002 return -EPERM;
2003 if (S_ISDIR(old_dentry->d_inode->i_mode))
2004 return -EPERM;
2006 error = security_inode_link(old_dentry, dir, new_dentry);
2007 if (error)
2008 return error;
2010 down(&old_dentry->d_inode->i_sem);
2011 DQUOT_INIT(dir);
2012 error = dir->i_op->link(old_dentry, dir, new_dentry);
2013 up(&old_dentry->d_inode->i_sem);
2014 if (!error) {
2015 inode_dir_notify(dir, DN_CREATE);
2016 security_inode_post_link(old_dentry, dir, new_dentry);
2018 return error;
2022 * Hardlinks are often used in delicate situations. We avoid
2023 * security-related surprises by not following symlinks on the
2024 * newname. --KAB
2026 * We don't follow them on the oldname either to be compatible
2027 * with linux 2.0, and to avoid hard-linking to directories
2028 * and other special files. --ADM
2030 asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
2032 struct dentry *new_dentry;
2033 struct nameidata nd, old_nd;
2034 int error;
2035 char * to;
2037 to = getname(newname);
2038 if (IS_ERR(to))
2039 return PTR_ERR(to);
2041 error = __user_walk(oldname, 0, &old_nd);
2042 if (error)
2043 goto exit;
2044 error = path_lookup(to, LOOKUP_PARENT, &nd);
2045 if (error)
2046 goto out;
2047 error = -EXDEV;
2048 if (old_nd.mnt != nd.mnt)
2049 goto out_release;
2050 new_dentry = lookup_create(&nd, 0);
2051 error = PTR_ERR(new_dentry);
2052 if (!IS_ERR(new_dentry)) {
2053 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
2054 dput(new_dentry);
2056 up(&nd.dentry->d_inode->i_sem);
2057 out_release:
2058 path_release(&nd);
2059 out:
2060 path_release(&old_nd);
2061 exit:
2062 putname(to);
2064 return error;
2068 * The worst of all namespace operations - renaming directory. "Perverted"
2069 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2070 * Problems:
2071 * a) we can get into loop creation. Check is done in is_subdir().
2072 * b) race potential - two innocent renames can create a loop together.
2073 * That's where 4.4 screws up. Current fix: serialization on
2074 * sb->s_vfs_rename_sem. We might be more accurate, but that's another
2075 * story.
2076 * c) we have to lock _three_ objects - parents and victim (if it exists).
2077 * And that - after we got ->i_sem on parents (until then we don't know
2078 * whether the target exists). Solution: try to be smart with locking
2079 * order for inodes. We rely on the fact that tree topology may change
2080 * only under ->s_vfs_rename_sem _and_ that parent of the object we
2081 * move will be locked. Thus we can rank directories by the tree
2082 * (ancestors first) and rank all non-directories after them.
2083 * That works since everybody except rename does "lock parent, lookup,
2084 * lock child" and rename is under ->s_vfs_rename_sem.
2085 * HOWEVER, it relies on the assumption that any object with ->lookup()
2086 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2087 * we'd better make sure that there's no link(2) for them.
2088 * d) some filesystems don't support opened-but-unlinked directories,
2089 * either because of layout or because they are not ready to deal with
2090 * all cases correctly. The latter will be fixed (taking this sort of
2091 * stuff into VFS), but the former is not going away. Solution: the same
2092 * trick as in rmdir().
2093 * e) conversion from fhandle to dentry may come in the wrong moment - when
2094 * we are removing the target. Solution: we will have to grab ->i_sem
2095 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2096 * ->i_sem on parents, which works but leads to some truely excessive
2097 * locking].
2099 static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2100 struct inode *new_dir, struct dentry *new_dentry)
2102 int error = 0;
2103 struct inode *target;
2106 * If we are going to change the parent - check write permissions,
2107 * we'll need to flip '..'.
2109 if (new_dir != old_dir) {
2110 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2111 if (error)
2112 return error;
2115 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2116 if (error)
2117 return error;
2119 target = new_dentry->d_inode;
2120 if (target) {
2121 down(&target->i_sem);
2122 dentry_unhash(new_dentry);
2124 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2125 error = -EBUSY;
2126 else
2127 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2128 if (target) {
2129 if (!error)
2130 target->i_flags |= S_DEAD;
2131 up(&target->i_sem);
2132 if (d_unhashed(new_dentry))
2133 d_rehash(new_dentry);
2134 dput(new_dentry);
2136 if (!error) {
2137 d_move(old_dentry,new_dentry);
2138 security_inode_post_rename(old_dir, old_dentry,
2139 new_dir, new_dentry);
2141 return error;
2144 static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2145 struct inode *new_dir, struct dentry *new_dentry)
2147 struct inode *target;
2148 int error;
2150 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2151 if (error)
2152 return error;
2154 dget(new_dentry);
2155 target = new_dentry->d_inode;
2156 if (target)
2157 down(&target->i_sem);
2158 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2159 error = -EBUSY;
2160 else
2161 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2162 if (!error) {
2163 /* The following d_move() should become unconditional */
2164 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2165 d_move(old_dentry, new_dentry);
2166 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
2168 if (target)
2169 up(&target->i_sem);
2170 dput(new_dentry);
2171 return error;
2174 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2175 struct inode *new_dir, struct dentry *new_dentry)
2177 int error;
2178 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2180 if (old_dentry->d_inode == new_dentry->d_inode)
2181 return 0;
2183 error = may_delete(old_dir, old_dentry, is_dir);
2184 if (error)
2185 return error;
2187 if (!new_dentry->d_inode)
2188 error = may_create(new_dir, new_dentry, NULL);
2189 else
2190 error = may_delete(new_dir, new_dentry, is_dir);
2191 if (error)
2192 return error;
2194 if (!old_dir->i_op || !old_dir->i_op->rename)
2195 return -EPERM;
2197 DQUOT_INIT(old_dir);
2198 DQUOT_INIT(new_dir);
2200 if (is_dir)
2201 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2202 else
2203 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2204 if (!error) {
2205 if (old_dir == new_dir)
2206 inode_dir_notify(old_dir, DN_RENAME);
2207 else {
2208 inode_dir_notify(old_dir, DN_DELETE);
2209 inode_dir_notify(new_dir, DN_CREATE);
2212 return error;
2215 static inline int do_rename(const char * oldname, const char * newname)
2217 int error = 0;
2218 struct dentry * old_dir, * new_dir;
2219 struct dentry * old_dentry, *new_dentry;
2220 struct dentry * trap;
2221 struct nameidata oldnd, newnd;
2223 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2224 if (error)
2225 goto exit;
2227 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2228 if (error)
2229 goto exit1;
2231 error = -EXDEV;
2232 if (oldnd.mnt != newnd.mnt)
2233 goto exit2;
2235 old_dir = oldnd.dentry;
2236 error = -EBUSY;
2237 if (oldnd.last_type != LAST_NORM)
2238 goto exit2;
2240 new_dir = newnd.dentry;
2241 if (newnd.last_type != LAST_NORM)
2242 goto exit2;
2244 trap = lock_rename(new_dir, old_dir);
2246 old_dentry = lookup_hash(&oldnd.last, old_dir);
2247 error = PTR_ERR(old_dentry);
2248 if (IS_ERR(old_dentry))
2249 goto exit3;
2250 /* source must exist */
2251 error = -ENOENT;
2252 if (!old_dentry->d_inode)
2253 goto exit4;
2254 /* unless the source is a directory trailing slashes give -ENOTDIR */
2255 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2256 error = -ENOTDIR;
2257 if (oldnd.last.name[oldnd.last.len])
2258 goto exit4;
2259 if (newnd.last.name[newnd.last.len])
2260 goto exit4;
2262 /* source should not be ancestor of target */
2263 error = -EINVAL;
2264 if (old_dentry == trap)
2265 goto exit4;
2266 new_dentry = lookup_hash(&newnd.last, new_dir);
2267 error = PTR_ERR(new_dentry);
2268 if (IS_ERR(new_dentry))
2269 goto exit4;
2270 /* target should not be an ancestor of source */
2271 error = -ENOTEMPTY;
2272 if (new_dentry == trap)
2273 goto exit5;
2275 error = vfs_rename(old_dir->d_inode, old_dentry,
2276 new_dir->d_inode, new_dentry);
2277 exit5:
2278 dput(new_dentry);
2279 exit4:
2280 dput(old_dentry);
2281 exit3:
2282 unlock_rename(new_dir, old_dir);
2283 exit2:
2284 path_release(&newnd);
2285 exit1:
2286 path_release(&oldnd);
2287 exit:
2288 return error;
2291 asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2293 int error;
2294 char * from;
2295 char * to;
2297 from = getname(oldname);
2298 if(IS_ERR(from))
2299 return PTR_ERR(from);
2300 to = getname(newname);
2301 error = PTR_ERR(to);
2302 if (!IS_ERR(to)) {
2303 error = do_rename(from,to);
2304 putname(to);
2306 putname(from);
2307 return error;
2310 int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2312 int len;
2314 len = PTR_ERR(link);
2315 if (IS_ERR(link))
2316 goto out;
2318 len = strlen(link);
2319 if (len > (unsigned) buflen)
2320 len = buflen;
2321 if (copy_to_user(buffer, link, len))
2322 len = -EFAULT;
2323 out:
2324 return len;
2328 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2329 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2330 * using) it for any given inode is up to filesystem.
2332 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2334 struct nameidata nd;
2335 int res;
2336 nd.depth = 0;
2337 res = dentry->d_inode->i_op->follow_link(dentry, &nd);
2338 if (!res) {
2339 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2340 if (dentry->d_inode->i_op->put_link)
2341 dentry->d_inode->i_op->put_link(dentry, &nd);
2343 return res;
2346 int vfs_follow_link(struct nameidata *nd, const char *link)
2348 return __vfs_follow_link(nd, link);
2351 /* get the link contents into pagecache */
2352 static char *page_getlink(struct dentry * dentry, struct page **ppage)
2354 struct page * page;
2355 struct address_space *mapping = dentry->d_inode->i_mapping;
2356 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2357 NULL);
2358 if (IS_ERR(page))
2359 goto sync_fail;
2360 wait_on_page_locked(page);
2361 if (!PageUptodate(page))
2362 goto async_fail;
2363 *ppage = page;
2364 return kmap(page);
2366 async_fail:
2367 page_cache_release(page);
2368 return ERR_PTR(-EIO);
2370 sync_fail:
2371 return (char*)page;
2374 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2376 struct page *page = NULL;
2377 char *s = page_getlink(dentry, &page);
2378 int res = vfs_readlink(dentry,buffer,buflen,s);
2379 if (page) {
2380 kunmap(page);
2381 page_cache_release(page);
2383 return res;
2386 int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2388 struct page *page;
2389 nd_set_link(nd, page_getlink(dentry, &page));
2390 return 0;
2393 void page_put_link(struct dentry *dentry, struct nameidata *nd)
2395 if (!IS_ERR(nd_get_link(nd))) {
2396 struct page *page;
2397 page = find_get_page(dentry->d_inode->i_mapping, 0);
2398 if (!page)
2399 BUG();
2400 kunmap(page);
2401 page_cache_release(page);
2402 page_cache_release(page);
2406 int page_symlink(struct inode *inode, const char *symname, int len)
2408 struct address_space *mapping = inode->i_mapping;
2409 struct page *page = grab_cache_page(mapping, 0);
2410 int err = -ENOMEM;
2411 char *kaddr;
2413 if (!page)
2414 goto fail;
2415 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2416 if (err)
2417 goto fail_map;
2418 kaddr = kmap_atomic(page, KM_USER0);
2419 memcpy(kaddr, symname, len-1);
2420 kunmap_atomic(kaddr, KM_USER0);
2421 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2423 * Notice that we are _not_ going to block here - end of page is
2424 * unmapped, so this will only try to map the rest of page, see
2425 * that it is unmapped (typically even will not look into inode -
2426 * ->i_size will be enough for everything) and zero it out.
2427 * OTOH it's obviously correct and should make the page up-to-date.
2429 if (!PageUptodate(page)) {
2430 err = mapping->a_ops->readpage(NULL, page);
2431 wait_on_page_locked(page);
2432 } else {
2433 unlock_page(page);
2435 page_cache_release(page);
2436 if (err < 0)
2437 goto fail;
2438 mark_inode_dirty(inode);
2439 return 0;
2440 fail_map:
2441 unlock_page(page);
2442 page_cache_release(page);
2443 fail:
2444 return err;
2447 struct inode_operations page_symlink_inode_operations = {
2448 .readlink = generic_readlink,
2449 .follow_link = page_follow_link_light,
2450 .put_link = page_put_link,
2453 EXPORT_SYMBOL(__user_walk);
2454 EXPORT_SYMBOL(follow_down);
2455 EXPORT_SYMBOL(follow_up);
2456 EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2457 EXPORT_SYMBOL(getname);
2458 EXPORT_SYMBOL(lock_rename);
2459 EXPORT_SYMBOL(lookup_hash);
2460 EXPORT_SYMBOL(lookup_one_len);
2461 EXPORT_SYMBOL(page_follow_link_light);
2462 EXPORT_SYMBOL(page_put_link);
2463 EXPORT_SYMBOL(page_readlink);
2464 EXPORT_SYMBOL(page_symlink);
2465 EXPORT_SYMBOL(page_symlink_inode_operations);
2466 EXPORT_SYMBOL(path_lookup);
2467 EXPORT_SYMBOL(path_release);
2468 EXPORT_SYMBOL(path_walk);
2469 EXPORT_SYMBOL(permission);
2470 EXPORT_SYMBOL(unlock_rename);
2471 EXPORT_SYMBOL(vfs_create);
2472 EXPORT_SYMBOL(vfs_follow_link);
2473 EXPORT_SYMBOL(vfs_link);
2474 EXPORT_SYMBOL(vfs_mkdir);
2475 EXPORT_SYMBOL(vfs_mknod);
2476 EXPORT_SYMBOL(generic_permission);
2477 EXPORT_SYMBOL(vfs_readlink);
2478 EXPORT_SYMBOL(vfs_rename);
2479 EXPORT_SYMBOL(vfs_rmdir);
2480 EXPORT_SYMBOL(vfs_symlink);
2481 EXPORT_SYMBOL(vfs_unlink);
2482 EXPORT_SYMBOL(dentry_unhash);
2483 EXPORT_SYMBOL(generic_readlink);