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