1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/fs.h>
21#include <linux/namei.h>
22#include <linux/quotaops.h>
23#include <linux/pagemap.h>
24#include <linux/dnotify.h>
25#include <linux/smp_lock.h>
26#include <linux/personality.h>
27#include <linux/security.h>
28#include <linux/syscalls.h>
29#include <linux/mount.h>
30#include <linux/audit.h>
31#include <asm/namei.h>
32#include <asm/uaccess.h>
33
34#define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114static inline int do_getname(const char __user *filename, char *page)
115{
116 int retval;
117 unsigned long len = PATH_MAX;
118
119 if ((unsigned long) filename >= TASK_SIZE) {
120 if (!segment_eq(get_fs(), KERNEL_DS))
121 return -EFAULT;
122 } else if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
123 len = TASK_SIZE - (unsigned long) filename;
124
125 retval = strncpy_from_user((char *)page, filename, len);
126 if (retval > 0) {
127 if (retval < len)
128 return 0;
129 return -ENAMETOOLONG;
130 } else if (!retval)
131 retval = -ENOENT;
132 return retval;
133}
134
135char * getname(const char __user * filename)
136{
137 char *tmp, *result;
138
139 result = ERR_PTR(-ENOMEM);
140 tmp = __getname();
141 if (tmp) {
142 int retval = do_getname(filename, tmp);
143
144 result = tmp;
145 if (retval < 0) {
146 __putname(tmp);
147 result = ERR_PTR(retval);
148 }
149 }
150 if (unlikely(current->audit_context) && !IS_ERR(result) && result)
151 audit_getname(result);
152 return result;
153}
154
155
156
157
158
159
160
161
162
163
164
165
166int generic_permission(struct inode *inode, int mask,
167 int (*check_acl)(struct inode *inode, int mask))
168{
169 umode_t mode = inode->i_mode;
170
171 if (mask & MAY_WRITE) {
172
173
174
175 if (IS_RDONLY(inode) &&
176 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
177 return -EROFS;
178
179
180
181
182 if (IS_IMMUTABLE(inode))
183 return -EACCES;
184 }
185
186 if (current->fsuid == inode->i_uid)
187 mode >>= 6;
188 else {
189 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
190 int error = check_acl(inode, mask);
191 if (error == -EACCES)
192 goto check_capabilities;
193 else if (error != -EAGAIN)
194 return error;
195 }
196
197 if (in_group_p(inode->i_gid))
198 mode >>= 3;
199 }
200
201
202
203
204 if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
205 return 0;
206
207 check_capabilities:
208
209
210
211
212 if (!(mask & MAY_EXEC) ||
213 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
214 if (capable(CAP_DAC_OVERRIDE))
215 return 0;
216
217
218
219
220 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
221 if (capable(CAP_DAC_READ_SEARCH))
222 return 0;
223
224 return -EACCES;
225}
226
227int permission(struct inode * inode,int mask, struct nameidata *nd)
228{
229 int retval;
230 int submask;
231
232
233 submask = mask & ~MAY_APPEND;
234
235 if (inode->i_op && inode->i_op->permission)
236 retval = inode->i_op->permission(inode, submask, nd);
237 else
238 retval = generic_permission(inode, submask, NULL);
239 if (retval)
240 return retval;
241
242 return security_inode_permission(inode, mask, nd);
243}
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263int get_write_access(struct inode * inode)
264{
265 spin_lock(&inode->i_lock);
266 if (atomic_read(&inode->i_writecount) < 0) {
267 spin_unlock(&inode->i_lock);
268 return -ETXTBSY;
269 }
270 atomic_inc(&inode->i_writecount);
271 spin_unlock(&inode->i_lock);
272
273 return 0;
274}
275
276int deny_write_access(struct file * file)
277{
278 struct inode *inode = file->f_dentry->d_inode;
279
280 spin_lock(&inode->i_lock);
281 if (atomic_read(&inode->i_writecount) > 0) {
282 spin_unlock(&inode->i_lock);
283 return -ETXTBSY;
284 }
285 atomic_dec(&inode->i_writecount);
286 spin_unlock(&inode->i_lock);
287
288 return 0;
289}
290
291void path_release(struct nameidata *nd)
292{
293 dput(nd->dentry);
294 mntput(nd->mnt);
295}
296
297
298
299
300
301void path_release_on_umount(struct nameidata *nd)
302{
303 dput(nd->dentry);
304 _mntput(nd->mnt);
305}
306
307
308
309
310
311static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
312{
313 struct dentry * dentry = __d_lookup(parent, name);
314
315
316
317
318 if (!dentry)
319 dentry = d_lookup(parent, name);
320
321 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {
322 if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) {
323 dput(dentry);
324 dentry = NULL;
325 }
326 }
327 return dentry;
328}
329
330
331
332
333
334
335
336
337
338
339
340static inline int exec_permission_lite(struct inode *inode,
341 struct nameidata *nd)
342{
343 umode_t mode = inode->i_mode;
344
345 if (inode->i_op && inode->i_op->permission)
346 return -EAGAIN;
347
348 if (current->fsuid == inode->i_uid)
349 mode >>= 6;
350 else if (in_group_p(inode->i_gid))
351 mode >>= 3;
352
353 if (mode & MAY_EXEC)
354 goto ok;
355
356 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
357 goto ok;
358
359 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
360 goto ok;
361
362 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
363 goto ok;
364
365 return -EACCES;
366ok:
367 return security_inode_permission(inode, MAY_EXEC, nd);
368}
369
370
371
372
373
374
375
376
377
378static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
379{
380 struct dentry * result;
381 struct inode *dir = parent->d_inode;
382
383 down(&dir->i_sem);
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398 result = d_lookup(parent, name);
399 if (!result) {
400 struct dentry * dentry = d_alloc(parent, name);
401 result = ERR_PTR(-ENOMEM);
402 if (dentry) {
403 result = dir->i_op->lookup(dir, dentry, nd);
404 if (result)
405 dput(dentry);
406 else
407 result = dentry;
408 }
409 up(&dir->i_sem);
410 return result;
411 }
412
413
414
415
416
417 up(&dir->i_sem);
418 if (result->d_op && result->d_op->d_revalidate) {
419 if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) {
420 dput(result);
421 result = ERR_PTR(-ENOENT);
422 }
423 }
424 return result;
425}
426
427static int __emul_lookup_dentry(const char *, struct nameidata *);
428
429
430static inline int
431walk_init_root(const char *name, struct nameidata *nd)
432{
433 read_lock(¤t->fs->lock);
434 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
435 nd->mnt = mntget(current->fs->altrootmnt);
436 nd->dentry = dget(current->fs->altroot);
437 read_unlock(¤t->fs->lock);
438 if (__emul_lookup_dentry(name,nd))
439 return 0;
440 read_lock(¤t->fs->lock);
441 }
442 nd->mnt = mntget(current->fs->rootmnt);
443 nd->dentry = dget(current->fs->root);
444 read_unlock(¤t->fs->lock);
445 return 1;
446}
447
448static inline int __vfs_follow_link(struct nameidata *nd, const char *link)
449{
450 int res = 0;
451 char *name;
452 if (IS_ERR(link))
453 goto fail;
454
455 if (*link == '/') {
456 path_release(nd);
457 if (!walk_init_root(link, nd))
458
459 goto out;
460 }
461 res = link_path_walk(link, nd);
462out:
463 if (nd->depth || res || nd->last_type!=LAST_NORM)
464 return res;
465
466
467
468
469
470 name = __getname();
471 if (unlikely(!name)) {
472 path_release(nd);
473 return -ENOMEM;
474 }
475 strcpy(name, nd->last.name);
476 nd->last.name = name;
477 return 0;
478fail:
479 path_release(nd);
480 return PTR_ERR(link);
481}
482
483
484
485
486
487
488
489
490static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
491{
492 int err = -ELOOP;
493 if (current->link_count >= MAX_NESTED_LINKS)
494 goto loop;
495 if (current->total_link_count >= 40)
496 goto loop;
497 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
498 cond_resched();
499 err = security_inode_follow_link(dentry, nd);
500 if (err)
501 goto loop;
502 current->link_count++;
503 current->total_link_count++;
504 nd->depth++;
505 touch_atime(nd->mnt, dentry);
506 nd_set_link(nd, NULL);
507 err = dentry->d_inode->i_op->follow_link(dentry, nd);
508 if (!err) {
509 char *s = nd_get_link(nd);
510 if (s)
511 err = __vfs_follow_link(nd, s);
512 if (dentry->d_inode->i_op->put_link)
513 dentry->d_inode->i_op->put_link(dentry, nd);
514 }
515 current->link_count--;
516 nd->depth--;
517 return err;
518loop:
519 path_release(nd);
520 return err;
521}
522
523int follow_up(struct vfsmount **mnt, struct dentry **dentry)
524{
525 struct vfsmount *parent;
526 struct dentry *mountpoint;
527 spin_lock(&vfsmount_lock);
528 parent=(*mnt)->mnt_parent;
529 if (parent == *mnt) {
530 spin_unlock(&vfsmount_lock);
531 return 0;
532 }
533 mntget(parent);
534 mountpoint=dget((*mnt)->mnt_mountpoint);
535 spin_unlock(&vfsmount_lock);
536 dput(*dentry);
537 *dentry = mountpoint;
538 mntput(*mnt);
539 *mnt = parent;
540 return 1;
541}
542
543
544
545
546static int follow_mount(struct vfsmount **mnt, struct dentry **dentry)
547{
548 int res = 0;
549 while (d_mountpoint(*dentry)) {
550 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
551 if (!mounted)
552 break;
553 mntput(*mnt);
554 *mnt = mounted;
555 dput(*dentry);
556 *dentry = dget(mounted->mnt_root);
557 res = 1;
558 }
559 return res;
560}
561
562
563
564
565static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry)
566{
567 struct vfsmount *mounted;
568
569 mounted = lookup_mnt(*mnt, *dentry);
570 if (mounted) {
571 mntput(*mnt);
572 *mnt = mounted;
573 dput(*dentry);
574 *dentry = dget(mounted->mnt_root);
575 return 1;
576 }
577 return 0;
578}
579
580int follow_down(struct vfsmount **mnt, struct dentry **dentry)
581{
582 return __follow_down(mnt,dentry);
583}
584
585static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry)
586{
587 while(1) {
588 struct vfsmount *parent;
589 struct dentry *old = *dentry;
590
591 read_lock(¤t->fs->lock);
592 if (*dentry == current->fs->root &&
593 *mnt == current->fs->rootmnt) {
594 read_unlock(¤t->fs->lock);
595 break;
596 }
597 read_unlock(¤t->fs->lock);
598 spin_lock(&dcache_lock);
599 if (*dentry != (*mnt)->mnt_root) {
600 *dentry = dget((*dentry)->d_parent);
601 spin_unlock(&dcache_lock);
602 dput(old);
603 break;
604 }
605 spin_unlock(&dcache_lock);
606 spin_lock(&vfsmount_lock);
607 parent = (*mnt)->mnt_parent;
608 if (parent == *mnt) {
609 spin_unlock(&vfsmount_lock);
610 break;
611 }
612 mntget(parent);
613 *dentry = dget((*mnt)->mnt_mountpoint);
614 spin_unlock(&vfsmount_lock);
615 dput(old);
616 mntput(*mnt);
617 *mnt = parent;
618 }
619 follow_mount(mnt, dentry);
620}
621
622struct path {
623 struct vfsmount *mnt;
624 struct dentry *dentry;
625};
626
627
628
629
630
631
632static int do_lookup(struct nameidata *nd, struct qstr *name,
633 struct path *path)
634{
635 struct vfsmount *mnt = nd->mnt;
636 struct dentry *dentry = __d_lookup(nd->dentry, name);
637
638 if (!dentry)
639 goto need_lookup;
640 if (dentry->d_op && dentry->d_op->d_revalidate)
641 goto need_revalidate;
642done:
643 path->mnt = mnt;
644 path->dentry = dentry;
645 return 0;
646
647need_lookup:
648 dentry = real_lookup(nd->dentry, name, nd);
649 if (IS_ERR(dentry))
650 goto fail;
651 goto done;
652
653need_revalidate:
654 if (dentry->d_op->d_revalidate(dentry, nd))
655 goto done;
656 if (d_invalidate(dentry))
657 goto done;
658 dput(dentry);
659 goto need_lookup;
660
661fail:
662 return PTR_ERR(dentry);
663}
664
665
666
667
668
669
670
671
672
673int fastcall link_path_walk(const char * name, struct nameidata *nd)
674{
675 struct path next;
676 struct inode *inode;
677 int err;
678 unsigned int lookup_flags = nd->flags;
679
680 while (*name=='/')
681 name++;
682 if (!*name)
683 goto return_reval;
684
685 inode = nd->dentry->d_inode;
686 if (nd->depth)
687 lookup_flags = LOOKUP_FOLLOW;
688
689
690 for(;;) {
691 unsigned long hash;
692 struct qstr this;
693 unsigned int c;
694
695 err = exec_permission_lite(inode, nd);
696 if (err == -EAGAIN) {
697 err = permission(inode, MAY_EXEC, nd);
698 }
699 if (err)
700 break;
701
702 this.name = name;
703 c = *(const unsigned char *)name;
704
705 hash = init_name_hash();
706 do {
707 name++;
708 hash = partial_name_hash(c, hash);
709 c = *(const unsigned char *)name;
710 } while (c && (c != '/'));
711 this.len = name - (const char *) this.name;
712 this.hash = end_name_hash(hash);
713
714
715 if (!c)
716 goto last_component;
717 while (*++name == '/');
718 if (!*name)
719 goto last_with_slashes;
720
721
722
723
724
725
726 if (this.name[0] == '.') switch (this.len) {
727 default:
728 break;
729 case 2:
730 if (this.name[1] != '.')
731 break;
732 follow_dotdot(&nd->mnt, &nd->dentry);
733 inode = nd->dentry->d_inode;
734
735 case 1:
736 continue;
737 }
738
739
740
741
742 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
743 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
744 if (err < 0)
745 break;
746 }
747 nd->flags |= LOOKUP_CONTINUE;
748
749 err = do_lookup(nd, &this, &next);
750 if (err)
751 break;
752
753 follow_mount(&next.mnt, &next.dentry);
754
755 err = -ENOENT;
756 inode = next.dentry->d_inode;
757 if (!inode)
758 goto out_dput;
759 err = -ENOTDIR;
760 if (!inode->i_op)
761 goto out_dput;
762
763 if (inode->i_op->follow_link) {
764 mntget(next.mnt);
765 err = do_follow_link(next.dentry, nd);
766 dput(next.dentry);
767 mntput(next.mnt);
768 if (err)
769 goto return_err;
770 err = -ENOENT;
771 inode = nd->dentry->d_inode;
772 if (!inode)
773 break;
774 err = -ENOTDIR;
775 if (!inode->i_op)
776 break;
777 } else {
778 dput(nd->dentry);
779 nd->mnt = next.mnt;
780 nd->dentry = next.dentry;
781 }
782 err = -ENOTDIR;
783 if (!inode->i_op->lookup)
784 break;
785 continue;
786
787
788last_with_slashes:
789 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
790last_component:
791 nd->flags &= ~LOOKUP_CONTINUE;
792 if (lookup_flags & LOOKUP_PARENT)
793 goto lookup_parent;
794 if (this.name[0] == '.') switch (this.len) {
795 default:
796 break;
797 case 2:
798 if (this.name[1] != '.')
799 break;
800 follow_dotdot(&nd->mnt, &nd->dentry);
801 inode = nd->dentry->d_inode;
802
803 case 1:
804 goto return_reval;
805 }
806 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
807 err = nd->dentry->d_op->d_hash(nd->dentry, &this);
808 if (err < 0)
809 break;
810 }
811 err = do_lookup(nd, &this, &next);
812 if (err)
813 break;
814 follow_mount(&next.mnt, &next.dentry);
815 inode = next.dentry->d_inode;
816 if ((lookup_flags & LOOKUP_FOLLOW)
817 && inode && inode->i_op && inode->i_op->follow_link) {
818 mntget(next.mnt);
819 err = do_follow_link(next.dentry, nd);
820 dput(next.dentry);
821 mntput(next.mnt);
822 if (err)
823 goto return_err;
824 inode = nd->dentry->d_inode;
825 } else {
826 dput(nd->dentry);
827 nd->mnt = next.mnt;
828 nd->dentry = next.dentry;
829 }
830 err = -ENOENT;
831 if (!inode)
832 break;
833 if (lookup_flags & LOOKUP_DIRECTORY) {
834 err = -ENOTDIR;
835 if (!inode->i_op || !inode->i_op->lookup)
836 break;
837 }
838 goto return_base;
839lookup_parent:
840 nd->last = this;
841 nd->last_type = LAST_NORM;
842 if (this.name[0] != '.')
843 goto return_base;
844 if (this.len == 1)
845 nd->last_type = LAST_DOT;
846 else if (this.len == 2 && this.name[1] == '.')
847 nd->last_type = LAST_DOTDOT;
848 else
849 goto return_base;
850return_reval:
851
852
853
854
855 if (nd->dentry && nd->dentry->d_sb &&
856 (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
857 err = -ESTALE;
858
859 if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
860 break;
861 }
862return_base:
863 return 0;
864out_dput:
865 dput(next.dentry);
866 break;
867 }
868 path_release(nd);
869return_err:
870 return err;
871}
872
873int fastcall path_walk(const char * name, struct nameidata *nd)
874{
875 current->total_link_count = 0;
876 return link_path_walk(name, nd);
877}
878
879
880
881static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
882{
883 if (path_walk(name, nd))
884 return 0;
885
886 if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
887 struct dentry *old_dentry = nd->dentry;
888 struct vfsmount *old_mnt = nd->mnt;
889 struct qstr last = nd->last;
890 int last_type = nd->last_type;
891
892
893
894
895 nd->last_type = LAST_ROOT;
896 read_lock(¤t->fs->lock);
897 nd->mnt = mntget(current->fs->rootmnt);
898 nd->dentry = dget(current->fs->root);
899 read_unlock(¤t->fs->lock);
900 if (path_walk(name, nd) == 0) {
901 if (nd->dentry->d_inode) {
902 dput(old_dentry);
903 mntput(old_mnt);
904 return 1;
905 }
906 path_release(nd);
907 }
908 nd->dentry = old_dentry;
909 nd->mnt = old_mnt;
910 nd->last = last;
911 nd->last_type = last_type;
912 }
913 return 1;
914}
915
916void set_fs_altroot(void)
917{
918 char *emul = __emul_prefix();
919 struct nameidata nd;
920 struct vfsmount *mnt = NULL, *oldmnt;
921 struct dentry *dentry = NULL, *olddentry;
922 int err;
923
924 if (!emul)
925 goto set_it;
926 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
927 if (!err) {
928 mnt = nd.mnt;
929 dentry = nd.dentry;
930 }
931set_it:
932 write_lock(¤t->fs->lock);
933 oldmnt = current->fs->altrootmnt;
934 olddentry = current->fs->altroot;
935 current->fs->altrootmnt = mnt;
936 current->fs->altroot = dentry;
937 write_unlock(¤t->fs->lock);
938 if (olddentry) {
939 dput(olddentry);
940 mntput(oldmnt);
941 }
942}
943
944int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
945{
946 int retval;
947
948 nd->last_type = LAST_ROOT;
949 nd->flags = flags;
950 nd->depth = 0;
951
952 read_lock(¤t->fs->lock);
953 if (*name=='/') {
954 if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
955 nd->mnt = mntget(current->fs->altrootmnt);
956 nd->dentry = dget(current->fs->altroot);
957 read_unlock(¤t->fs->lock);
958 if (__emul_lookup_dentry(name,nd))
959 return 0;
960 read_lock(¤t->fs->lock);
961 }
962 nd->mnt = mntget(current->fs->rootmnt);
963 nd->dentry = dget(current->fs->root);
964 } else {
965 nd->mnt = mntget(current->fs->pwdmnt);
966 nd->dentry = dget(current->fs->pwd);
967 }
968 read_unlock(¤t->fs->lock);
969 current->total_link_count = 0;
970 retval = link_path_walk(name, nd);
971 if (unlikely(current->audit_context
972 && nd && nd->dentry && nd->dentry->d_inode))
973 audit_inode(name,
974 nd->dentry->d_inode->i_ino,
975 nd->dentry->d_inode->i_rdev);
976 return retval;
977}
978
979
980
981
982
983
984static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd)
985{
986 struct dentry * dentry;
987 struct inode *inode;
988 int err;
989
990 inode = base->d_inode;
991 err = permission(inode, MAY_EXEC, nd);
992 dentry = ERR_PTR(err);
993 if (err)
994 goto out;
995
996
997
998
999
1000 if (base->d_op && base->d_op->d_hash) {
1001 err = base->d_op->d_hash(base, name);
1002 dentry = ERR_PTR(err);
1003 if (err < 0)
1004 goto out;
1005 }
1006
1007 dentry = cached_lookup(base, name, nd);
1008 if (!dentry) {
1009 struct dentry *new = d_alloc(base, name);
1010 dentry = ERR_PTR(-ENOMEM);
1011 if (!new)
1012 goto out;
1013 dentry = inode->i_op->lookup(inode, new, nd);
1014 if (!dentry)
1015 dentry = new;
1016 else
1017 dput(new);
1018 }
1019out:
1020 return dentry;
1021}
1022
1023struct dentry * lookup_hash(struct qstr *name, struct dentry * base)
1024{
1025 return __lookup_hash(name, base, NULL);
1026}
1027
1028
1029struct dentry * lookup_one_len(const char * name, struct dentry * base, int len)
1030{
1031 unsigned long hash;
1032 struct qstr this;
1033 unsigned int c;
1034
1035 this.name = name;
1036 this.len = len;
1037 if (!len)
1038 goto access;
1039
1040 hash = init_name_hash();
1041 while (len--) {
1042 c = *(const unsigned char *)name++;
1043 if (c == '/' || c == '\0')
1044 goto access;
1045 hash = partial_name_hash(c, hash);
1046 }
1047 this.hash = end_name_hash(hash);
1048
1049 return lookup_hash(&this, base);
1050access:
1051 return ERR_PTR(-EACCES);
1052}
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
1066{
1067 char *tmp = getname(name);
1068 int err = PTR_ERR(tmp);
1069
1070 if (!IS_ERR(tmp)) {
1071 err = path_lookup(tmp, flags, nd);
1072 putname(tmp);
1073 }
1074 return err;
1075}
1076
1077
1078
1079
1080
1081static inline int check_sticky(struct inode *dir, struct inode *inode)
1082{
1083 if (!(dir->i_mode & S_ISVTX))
1084 return 0;
1085 if (inode->i_uid == current->fsuid)
1086 return 0;
1087 if (dir->i_uid == current->fsuid)
1088 return 0;
1089 return !capable(CAP_FOWNER);
1090}
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir)
1112{
1113 int error;
1114
1115 if (!victim->d_inode)
1116 return -ENOENT;
1117
1118 BUG_ON(victim->d_parent->d_inode != dir);
1119
1120 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1121 if (error)
1122 return error;
1123 if (IS_APPEND(dir))
1124 return -EPERM;
1125 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1126 IS_IMMUTABLE(victim->d_inode))
1127 return -EPERM;
1128 if (isdir) {
1129 if (!S_ISDIR(victim->d_inode->i_mode))
1130 return -ENOTDIR;
1131 if (IS_ROOT(victim))
1132 return -EBUSY;
1133 } else if (S_ISDIR(victim->d_inode->i_mode))
1134 return -EISDIR;
1135 if (IS_DEADDIR(dir))
1136 return -ENOENT;
1137 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1138 return -EBUSY;
1139 return 0;
1140}
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150static inline int may_create(struct inode *dir, struct dentry *child,
1151 struct nameidata *nd)
1152{
1153 if (child->d_inode)
1154 return -EEXIST;
1155 if (IS_DEADDIR(dir))
1156 return -ENOENT;
1157 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1158}
1159
1160
1161
1162
1163
1164
1165
1166static inline int lookup_flags(unsigned int f)
1167{
1168 unsigned long retval = LOOKUP_FOLLOW;
1169
1170 if (f & O_NOFOLLOW)
1171 retval &= ~LOOKUP_FOLLOW;
1172
1173 if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
1174 retval &= ~LOOKUP_FOLLOW;
1175
1176 if (f & O_DIRECTORY)
1177 retval |= LOOKUP_DIRECTORY;
1178
1179 return retval;
1180}
1181
1182
1183
1184
1185struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1186{
1187 struct dentry *p;
1188
1189 if (p1 == p2) {
1190 down(&p1->d_inode->i_sem);
1191 return NULL;
1192 }
1193
1194 down(&p1->d_inode->i_sb->s_vfs_rename_sem);
1195
1196 for (p = p1; p->d_parent != p; p = p->d_parent) {
1197 if (p->d_parent == p2) {
1198 down(&p2->d_inode->i_sem);
1199 down(&p1->d_inode->i_sem);
1200 return p;
1201 }
1202 }
1203
1204 for (p = p2; p->d_parent != p; p = p->d_parent) {
1205 if (p->d_parent == p1) {
1206 down(&p1->d_inode->i_sem);
1207 down(&p2->d_inode->i_sem);
1208 return p;
1209 }
1210 }
1211
1212 down(&p1->d_inode->i_sem);
1213 down(&p2->d_inode->i_sem);
1214 return NULL;
1215}
1216
1217void unlock_rename(struct dentry *p1, struct dentry *p2)
1218{
1219 up(&p1->d_inode->i_sem);
1220 if (p1 != p2) {
1221 up(&p2->d_inode->i_sem);
1222 up(&p1->d_inode->i_sb->s_vfs_rename_sem);
1223 }
1224}
1225
1226int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1227 struct nameidata *nd)
1228{
1229 int error = may_create(dir, dentry, nd);
1230
1231 if (error)
1232 return error;
1233
1234 if (!dir->i_op || !dir->i_op->create)
1235 return -EACCES;
1236 mode &= S_IALLUGO;
1237 mode |= S_IFREG;
1238 error = security_inode_create(dir, dentry, mode);
1239 if (error)
1240 return error;
1241 DQUOT_INIT(dir);
1242 error = dir->i_op->create(dir, dentry, mode, nd);
1243 if (!error) {
1244 inode_dir_notify(dir, DN_CREATE);
1245 security_inode_post_create(dir, dentry, mode);
1246 }
1247 return error;
1248}
1249
1250int may_open(struct nameidata *nd, int acc_mode, int flag)
1251{
1252 struct dentry *dentry = nd->dentry;
1253 struct inode *inode = dentry->d_inode;
1254 int error;
1255
1256 if (!inode)
1257 return -ENOENT;
1258
1259 if (S_ISLNK(inode->i_mode))
1260 return -ELOOP;
1261
1262 if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))
1263 return -EISDIR;
1264
1265 error = permission(inode, acc_mode, nd);
1266 if (error)
1267 return error;
1268
1269
1270
1271
1272
1273
1274 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1275 flag &= ~O_TRUNC;
1276 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
1277 if (nd->mnt->mnt_flags & MNT_NODEV)
1278 return -EACCES;
1279
1280 flag &= ~O_TRUNC;
1281 } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE))
1282 return -EROFS;
1283
1284
1285
1286 if (IS_APPEND(inode)) {
1287 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1288 return -EPERM;
1289 if (flag & O_TRUNC)
1290 return -EPERM;
1291 }
1292
1293
1294 if (flag & O_NOATIME)
1295 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
1296 return -EPERM;
1297
1298
1299
1300
1301 error = break_lease(inode, flag);
1302 if (error)
1303 return error;
1304
1305 if (flag & O_TRUNC) {
1306 error = get_write_access(inode);
1307 if (error)
1308 return error;
1309
1310
1311
1312
1313 error = locks_verify_locked(inode);
1314 if (!error) {
1315 DQUOT_INIT(inode);
1316
1317 error = do_truncate(dentry, 0);
1318 }
1319 put_write_access(inode);
1320 if (error)
1321 return error;
1322 } else
1323 if (flag & FMODE_WRITE)
1324 DQUOT_INIT(inode);
1325
1326 return 0;
1327}
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)
1344{
1345 int acc_mode, error = 0;
1346 struct dentry *dentry;
1347 struct dentry *dir;
1348 int count = 0;
1349
1350 acc_mode = ACC_MODE(flag);
1351
1352
1353
1354 if (flag & O_APPEND)
1355 acc_mode |= MAY_APPEND;
1356
1357
1358 nd->intent.open.flags = flag;
1359 nd->intent.open.create_mode = mode;
1360
1361
1362
1363
1364 if (!(flag & O_CREAT)) {
1365 error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd);
1366 if (error)
1367 return error;
1368 goto ok;
1369 }
1370
1371
1372
1373
1374 error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd);
1375 if (error)
1376 return error;
1377
1378
1379
1380
1381
1382
1383 error = -EISDIR;
1384 if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
1385 goto exit;
1386
1387 dir = nd->dentry;
1388 nd->flags &= ~LOOKUP_PARENT;
1389 down(&dir->d_inode->i_sem);
1390 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1391
1392do_last:
1393 error = PTR_ERR(dentry);
1394 if (IS_ERR(dentry)) {
1395 up(&dir->d_inode->i_sem);
1396 goto exit;
1397 }
1398
1399
1400 if (!dentry->d_inode) {
1401 if (!IS_POSIXACL(dir->d_inode))
1402 mode &= ~current->fs->umask;
1403 error = vfs_create(dir->d_inode, dentry, mode, nd);
1404 up(&dir->d_inode->i_sem);
1405 dput(nd->dentry);
1406 nd->dentry = dentry;
1407 if (error)
1408 goto exit;
1409
1410 acc_mode = 0;
1411 flag &= ~O_TRUNC;
1412 goto ok;
1413 }
1414
1415
1416
1417
1418 up(&dir->d_inode->i_sem);
1419
1420 error = -EEXIST;
1421 if (flag & O_EXCL)
1422 goto exit_dput;
1423
1424 if (d_mountpoint(dentry)) {
1425 error = -ELOOP;
1426 if (flag & O_NOFOLLOW)
1427 goto exit_dput;
1428 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));
1429 }
1430 error = -ENOENT;
1431 if (!dentry->d_inode)
1432 goto exit_dput;
1433 if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)
1434 goto do_link;
1435
1436 dput(nd->dentry);
1437 nd->dentry = dentry;
1438 error = -EISDIR;
1439 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
1440 goto exit;
1441ok:
1442 error = may_open(nd, acc_mode, flag);
1443 if (error)
1444 goto exit;
1445 return 0;
1446
1447exit_dput:
1448 dput(dentry);
1449exit:
1450 path_release(nd);
1451 return error;
1452
1453do_link:
1454 error = -ELOOP;
1455 if (flag & O_NOFOLLOW)
1456 goto exit_dput;
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467 nd->flags |= LOOKUP_PARENT;
1468 error = security_inode_follow_link(dentry, nd);
1469 if (error)
1470 goto exit_dput;
1471 touch_atime(nd->mnt, dentry);
1472 nd_set_link(nd, NULL);
1473 error = dentry->d_inode->i_op->follow_link(dentry, nd);
1474 if (!error) {
1475 char *s = nd_get_link(nd);
1476 if (s)
1477 error = __vfs_follow_link(nd, s);
1478 if (dentry->d_inode->i_op->put_link)
1479 dentry->d_inode->i_op->put_link(dentry, nd);
1480 }
1481 dput(dentry);
1482 if (error)
1483 return error;
1484 nd->flags &= ~LOOKUP_PARENT;
1485 if (nd->last_type == LAST_BIND) {
1486 dentry = nd->dentry;
1487 goto ok;
1488 }
1489 error = -EISDIR;
1490 if (nd->last_type != LAST_NORM)
1491 goto exit;
1492 if (nd->last.name[nd->last.len]) {
1493 putname(nd->last.name);
1494 goto exit;
1495 }
1496 error = -ELOOP;
1497 if (count++==32) {
1498 putname(nd->last.name);
1499 goto exit;
1500 }
1501 dir = nd->dentry;
1502 down(&dir->d_inode->i_sem);
1503 dentry = __lookup_hash(&nd->last, nd->dentry, nd);
1504 putname(nd->last.name);
1505 goto do_last;
1506}
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516struct dentry *lookup_create(struct nameidata *nd, int is_dir)
1517{
1518 struct dentry *dentry;
1519
1520 down(&nd->dentry->d_inode->i_sem);
1521 dentry = ERR_PTR(-EEXIST);
1522 if (nd->last_type != LAST_NORM)
1523 goto fail;
1524 nd->flags &= ~LOOKUP_PARENT;
1525 dentry = lookup_hash(&nd->last, nd->dentry);
1526 if (IS_ERR(dentry))
1527 goto fail;
1528 if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
1529 goto enoent;
1530 return dentry;
1531enoent:
1532 dput(dentry);
1533 dentry = ERR_PTR(-ENOENT);
1534fail:
1535 return dentry;
1536}
1537
1538int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1539{
1540 int error = may_create(dir, dentry, NULL);
1541
1542 if (error)
1543 return error;
1544
1545 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
1546 return -EPERM;
1547
1548 if (!dir->i_op || !dir->i_op->mknod)
1549 return -EPERM;
1550
1551 error = security_inode_mknod(dir, dentry, mode, dev);
1552 if (error)
1553 return error;
1554
1555 DQUOT_INIT(dir);
1556 error = dir->i_op->mknod(dir, dentry, mode, dev);
1557 if (!error) {
1558 inode_dir_notify(dir, DN_CREATE);
1559 security_inode_post_mknod(dir, dentry, mode, dev);
1560 }
1561 return error;
1562}
1563
1564asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev)
1565{
1566 int error = 0;
1567 char * tmp;
1568 struct dentry * dentry;
1569 struct nameidata nd;
1570
1571 if (S_ISDIR(mode))
1572 return -EPERM;
1573 tmp = getname(filename);
1574 if (IS_ERR(tmp))
1575 return PTR_ERR(tmp);
1576
1577 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1578 if (error)
1579 goto out;
1580 dentry = lookup_create(&nd, 0);
1581 error = PTR_ERR(dentry);
1582
1583 if (!IS_POSIXACL(nd.dentry->d_inode))
1584 mode &= ~current->fs->umask;
1585 if (!IS_ERR(dentry)) {
1586 switch (mode & S_IFMT) {
1587 case 0: case S_IFREG:
1588 error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
1589 break;
1590 case S_IFCHR: case S_IFBLK:
1591 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
1592 new_decode_dev(dev));
1593 break;
1594 case S_IFIFO: case S_IFSOCK:
1595 error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
1596 break;
1597 case S_IFDIR:
1598 error = -EPERM;
1599 break;
1600 default:
1601 error = -EINVAL;
1602 }
1603 dput(dentry);
1604 }
1605 up(&nd.dentry->d_inode->i_sem);
1606 path_release(&nd);
1607out:
1608 putname(tmp);
1609
1610 return error;
1611}
1612
1613int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1614{
1615 int error = may_create(dir, dentry, NULL);
1616
1617 if (error)
1618 return error;
1619
1620 if (!dir->i_op || !dir->i_op->mkdir)
1621 return -EPERM;
1622
1623 mode &= (S_IRWXUGO|S_ISVTX);
1624 error = security_inode_mkdir(dir, dentry, mode);
1625 if (error)
1626 return error;
1627
1628 DQUOT_INIT(dir);
1629 error = dir->i_op->mkdir(dir, dentry, mode);
1630 if (!error) {
1631 inode_dir_notify(dir, DN_CREATE);
1632 security_inode_post_mkdir(dir,dentry, mode);
1633 }
1634 return error;
1635}
1636
1637asmlinkage long sys_mkdir(const char __user * pathname, int mode)
1638{
1639 int error = 0;
1640 char * tmp;
1641
1642 tmp = getname(pathname);
1643 error = PTR_ERR(tmp);
1644 if (!IS_ERR(tmp)) {
1645 struct dentry *dentry;
1646 struct nameidata nd;
1647
1648 error = path_lookup(tmp, LOOKUP_PARENT, &nd);
1649 if (error)
1650 goto out;
1651 dentry = lookup_create(&nd, 1);
1652 error = PTR_ERR(dentry);
1653 if (!IS_ERR(dentry)) {
1654 if (!IS_POSIXACL(nd.dentry->d_inode))
1655 mode &= ~current->fs->umask;
1656 error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
1657 dput(dentry);
1658 }
1659 up(&nd.dentry->d_inode->i_sem);
1660 path_release(&nd);
1661out:
1662 putname(tmp);
1663 }
1664
1665 return error;
1666}
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683void dentry_unhash(struct dentry *dentry)
1684{
1685 dget(dentry);
1686 spin_lock(&dcache_lock);
1687 switch (atomic_read(&dentry->d_count)) {
1688 default:
1689 spin_unlock(&dcache_lock);
1690 shrink_dcache_parent(dentry);
1691 spin_lock(&dcache_lock);
1692 if (atomic_read(&dentry->d_count) != 2)
1693 break;
1694 case 2:
1695 __d_drop(dentry);
1696 }
1697 spin_unlock(&dcache_lock);
1698}
1699
1700int vfs_rmdir(struct inode *dir, struct dentry *dentry)
1701{
1702 int error = may_delete(dir, dentry, 1);
1703
1704 if (error)
1705 return error;
1706
1707 if (!dir->i_op || !dir->i_op->rmdir)
1708 return -EPERM;
1709
1710 DQUOT_INIT(dir);
1711
1712 down(&dentry->d_inode->i_sem);
1713 dentry_unhash(dentry);
1714 if (d_mountpoint(dentry))
1715 error = -EBUSY;
1716 else {
1717 error = security_inode_rmdir(dir, dentry);
1718 if (!error) {
1719 error = dir->i_op->rmdir(dir, dentry);
1720 if (!error)
1721 dentry->d_inode->i_flags |= S_DEAD;
1722 }
1723 }
1724 up(&dentry->d_inode->i_sem);
1725 if (!error) {
1726 inode_dir_notify(dir, DN_DELETE);
1727 d_delete(dentry);
1728 }
1729 dput(dentry);
1730
1731 return error;
1732}
1733
1734asmlinkage long sys_rmdir(const char __user * pathname)
1735{
1736 int error = 0;
1737 char * name;
1738 struct dentry *dentry;
1739 struct nameidata nd;
1740
1741 name = getname(pathname);
1742 if(IS_ERR(name))
1743 return PTR_ERR(name);
1744
1745 error = path_lookup(name, LOOKUP_PARENT, &nd);
1746 if (error)
1747 goto exit;
1748
1749 switch(nd.last_type) {
1750 case LAST_DOTDOT:
1751 error = -ENOTEMPTY;
1752 goto exit1;
1753 case LAST_DOT:
1754 error = -EINVAL;
1755 goto exit1;
1756 case LAST_ROOT:
1757 error = -EBUSY;
1758 goto exit1;
1759 }
1760 down(&nd.dentry->d_inode->i_sem);
1761 dentry = lookup_hash(&nd.last, nd.dentry);
1762 error = PTR_ERR(dentry);
1763 if (!IS_ERR(dentry)) {
1764 error = vfs_rmdir(nd.dentry->d_inode, dentry);
1765 dput(dentry);
1766 }
1767 up(&nd.dentry->d_inode->i_sem);
1768exit1:
1769 path_release(&nd);
1770exit:
1771 putname(name);
1772 return error;
1773}
1774
1775int vfs_unlink(struct inode *dir, struct dentry *dentry)
1776{
1777 int error = may_delete(dir, dentry, 0);
1778
1779 if (error)
1780 return error;
1781
1782 if (!dir->i_op || !dir->i_op->unlink)
1783 return -EPERM;
1784
1785 DQUOT_INIT(dir);
1786
1787 down(&dentry->d_inode->i_sem);
1788 if (d_mountpoint(dentry))
1789 error = -EBUSY;
1790 else {
1791 error = security_inode_unlink(dir, dentry);
1792 if (!error)
1793 error = dir->i_op->unlink(dir, dentry);
1794 }
1795 up(&dentry->d_inode->i_sem);
1796
1797
1798 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
1799 d_delete(dentry);
1800 inode_dir_notify(dir, DN_DELETE);
1801 }
1802 return error;
1803}
1804
1805
1806
1807
1808
1809
1810
1811asmlinkage long sys_unlink(const char __user * pathname)
1812{
1813 int error = 0;
1814 char * name;
1815 struct dentry *dentry;
1816 struct nameidata nd;
1817 struct inode *inode = NULL;
1818
1819 name = getname(pathname);
1820 if(IS_ERR(name))
1821 return PTR_ERR(name);
1822
1823 error = path_lookup(name, LOOKUP_PARENT, &nd);
1824 if (error)
1825 goto exit;
1826 error = -EISDIR;
1827 if (nd.last_type != LAST_NORM)
1828 goto exit1;
1829 down(&nd.dentry->d_inode->i_sem);
1830 dentry = lookup_hash(&nd.last, nd.dentry);
1831 error = PTR_ERR(dentry);
1832 if (!IS_ERR(dentry)) {
1833
1834 if (nd.last.name[nd.last.len])
1835 goto slashes;
1836 inode = dentry->d_inode;
1837 if (inode)
1838 atomic_inc(&inode->i_count);
1839 error = vfs_unlink(nd.dentry->d_inode, dentry);
1840 exit2:
1841 dput(dentry);
1842 }
1843 up(&nd.dentry->d_inode->i_sem);
1844 if (inode)
1845 iput(inode);
1846exit1:
1847 path_release(&nd);
1848exit:
1849 putname(name);
1850 return error;
1851
1852slashes:
1853 error = !dentry->d_inode ? -ENOENT :
1854 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
1855 goto exit2;
1856}
1857
1858int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
1859{
1860 int error = may_create(dir, dentry, NULL);
1861
1862 if (error)
1863 return error;
1864
1865 if (!dir->i_op || !dir->i_op->symlink)
1866 return -EPERM;
1867
1868 error = security_inode_symlink(dir, dentry, oldname);
1869 if (error)
1870 return error;
1871
1872 DQUOT_INIT(dir);
1873 error = dir->i_op->symlink(dir, dentry, oldname);
1874 if (!error) {
1875 inode_dir_notify(dir, DN_CREATE);
1876 security_inode_post_symlink(dir, dentry, oldname);
1877 }
1878 return error;
1879}
1880
1881asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname)
1882{
1883 int error = 0;
1884 char * from;
1885 char * to;
1886
1887 from = getname(oldname);
1888 if(IS_ERR(from))
1889 return PTR_ERR(from);
1890 to = getname(newname);
1891 error = PTR_ERR(to);
1892 if (!IS_ERR(to)) {
1893 struct dentry *dentry;
1894 struct nameidata nd;
1895
1896 error = path_lookup(to, LOOKUP_PARENT, &nd);
1897 if (error)
1898 goto out;
1899 dentry = lookup_create(&nd, 0);
1900 error = PTR_ERR(dentry);
1901 if (!IS_ERR(dentry)) {
1902 error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
1903 dput(dentry);
1904 }
1905 up(&nd.dentry->d_inode->i_sem);
1906 path_release(&nd);
1907out:
1908 putname(to);
1909 }
1910 putname(from);
1911 return error;
1912}
1913
1914int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
1915{
1916 struct inode *inode = old_dentry->d_inode;
1917 int error;
1918
1919 if (!inode)
1920 return -ENOENT;
1921
1922 error = may_create(dir, new_dentry, NULL);
1923 if (error)
1924 return error;
1925
1926 if (dir->i_sb != inode->i_sb)
1927 return -EXDEV;
1928
1929
1930
1931
1932 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1933 return -EPERM;
1934 if (!dir->i_op || !dir->i_op->link)
1935 return -EPERM;
1936 if (S_ISDIR(old_dentry->d_inode->i_mode))
1937 return -EPERM;
1938
1939 error = security_inode_link(old_dentry, dir, new_dentry);
1940 if (error)
1941 return error;
1942
1943 down(&old_dentry->d_inode->i_sem);
1944 DQUOT_INIT(dir);
1945 error = dir->i_op->link(old_dentry, dir, new_dentry);
1946 up(&old_dentry->d_inode->i_sem);
1947 if (!error) {
1948 inode_dir_notify(dir, DN_CREATE);
1949 security_inode_post_link(old_dentry, dir, new_dentry);
1950 }
1951 return error;
1952}
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963asmlinkage long sys_link(const char __user * oldname, const char __user * newname)
1964{
1965 struct dentry *new_dentry;
1966 struct nameidata nd, old_nd;
1967 int error;
1968 char * to;
1969
1970 to = getname(newname);
1971 if (IS_ERR(to))
1972 return PTR_ERR(to);
1973
1974 error = __user_walk(oldname, 0, &old_nd);
1975 if (error)
1976 goto exit;
1977 error = path_lookup(to, LOOKUP_PARENT, &nd);
1978 if (error)
1979 goto out;
1980 error = -EXDEV;
1981 if (old_nd.mnt != nd.mnt)
1982 goto out_release;
1983 new_dentry = lookup_create(&nd, 0);
1984 error = PTR_ERR(new_dentry);
1985 if (!IS_ERR(new_dentry)) {
1986 error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
1987 dput(new_dentry);
1988 }
1989 up(&nd.dentry->d_inode->i_sem);
1990out_release:
1991 path_release(&nd);
1992out:
1993 path_release(&old_nd);
1994exit:
1995 putname(to);
1996
1997 return error;
1998}
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2033 struct inode *new_dir, struct dentry *new_dentry)
2034{
2035 int error = 0;
2036 struct inode *target;
2037
2038
2039
2040
2041
2042 if (new_dir != old_dir) {
2043 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2044 if (error)
2045 return error;
2046 }
2047
2048 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2049 if (error)
2050 return error;
2051
2052 target = new_dentry->d_inode;
2053 if (target) {
2054 down(&target->i_sem);
2055 dentry_unhash(new_dentry);
2056 }
2057 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2058 error = -EBUSY;
2059 else
2060 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2061 if (target) {
2062 if (!error)
2063 target->i_flags |= S_DEAD;
2064 up(&target->i_sem);
2065 if (d_unhashed(new_dentry))
2066 d_rehash(new_dentry);
2067 dput(new_dentry);
2068 }
2069 if (!error) {
2070 d_move(old_dentry,new_dentry);
2071 security_inode_post_rename(old_dir, old_dentry,
2072 new_dir, new_dentry);
2073 }
2074 return error;
2075}
2076
2077int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2078 struct inode *new_dir, struct dentry *new_dentry)
2079{
2080 struct inode *target;
2081 int error;
2082
2083 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2084 if (error)
2085 return error;
2086
2087 dget(new_dentry);
2088 target = new_dentry->d_inode;
2089 if (target)
2090 down(&target->i_sem);
2091 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2092 error = -EBUSY;
2093 else
2094 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2095 if (!error) {
2096
2097 if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME))
2098 d_move(old_dentry, new_dentry);
2099 security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry);
2100 }
2101 if (target)
2102 up(&target->i_sem);
2103 dput(new_dentry);
2104 return error;
2105}
2106
2107int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2108 struct inode *new_dir, struct dentry *new_dentry)
2109{
2110 int error;
2111 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
2112
2113 if (old_dentry->d_inode == new_dentry->d_inode)
2114 return 0;
2115
2116 error = may_delete(old_dir, old_dentry, is_dir);
2117 if (error)
2118 return error;
2119
2120 if (!new_dentry->d_inode)
2121 error = may_create(new_dir, new_dentry, NULL);
2122 else
2123 error = may_delete(new_dir, new_dentry, is_dir);
2124 if (error)
2125 return error;
2126
2127 if (!old_dir->i_op || !old_dir->i_op->rename)
2128 return -EPERM;
2129
2130 DQUOT_INIT(old_dir);
2131 DQUOT_INIT(new_dir);
2132
2133 if (is_dir)
2134 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2135 else
2136 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2137 if (!error) {
2138 if (old_dir == new_dir)
2139 inode_dir_notify(old_dir, DN_RENAME);
2140 else {
2141 inode_dir_notify(old_dir, DN_DELETE);
2142 inode_dir_notify(new_dir, DN_CREATE);
2143 }
2144 }
2145 return error;
2146}
2147
2148static inline int do_rename(const char * oldname, const char * newname)
2149{
2150 int error = 0;
2151 struct dentry * old_dir, * new_dir;
2152 struct dentry * old_dentry, *new_dentry;
2153 struct dentry * trap;
2154 struct nameidata oldnd, newnd;
2155
2156 error = path_lookup(oldname, LOOKUP_PARENT, &oldnd);
2157 if (error)
2158 goto exit;
2159
2160 error = path_lookup(newname, LOOKUP_PARENT, &newnd);
2161 if (error)
2162 goto exit1;
2163
2164 error = -EXDEV;
2165 if (oldnd.mnt != newnd.mnt)
2166 goto exit2;
2167
2168 old_dir = oldnd.dentry;
2169 error = -EBUSY;
2170 if (oldnd.last_type != LAST_NORM)
2171 goto exit2;
2172
2173 new_dir = newnd.dentry;
2174 if (newnd.last_type != LAST_NORM)
2175 goto exit2;
2176
2177 trap = lock_rename(new_dir, old_dir);
2178
2179 old_dentry = lookup_hash(&oldnd.last, old_dir);
2180 error = PTR_ERR(old_dentry);
2181 if (IS_ERR(old_dentry))
2182 goto exit3;
2183
2184 error = -ENOENT;
2185 if (!old_dentry->d_inode)
2186 goto exit4;
2187
2188 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2189 error = -ENOTDIR;
2190 if (oldnd.last.name[oldnd.last.len])
2191 goto exit4;
2192 if (newnd.last.name[newnd.last.len])
2193 goto exit4;
2194 }
2195
2196 error = -EINVAL;
2197 if (old_dentry == trap)
2198 goto exit4;
2199 new_dentry = lookup_hash(&newnd.last, new_dir);
2200 error = PTR_ERR(new_dentry);
2201 if (IS_ERR(new_dentry))
2202 goto exit4;
2203
2204 error = -ENOTEMPTY;
2205 if (new_dentry == trap)
2206 goto exit5;
2207
2208 error = vfs_rename(old_dir->d_inode, old_dentry,
2209 new_dir->d_inode, new_dentry);
2210exit5:
2211 dput(new_dentry);
2212exit4:
2213 dput(old_dentry);
2214exit3:
2215 unlock_rename(new_dir, old_dir);
2216exit2:
2217 path_release(&newnd);
2218exit1:
2219 path_release(&oldnd);
2220exit:
2221 return error;
2222}
2223
2224asmlinkage long sys_rename(const char __user * oldname, const char __user * newname)
2225{
2226 int error;
2227 char * from;
2228 char * to;
2229
2230 from = getname(oldname);
2231 if(IS_ERR(from))
2232 return PTR_ERR(from);
2233 to = getname(newname);
2234 error = PTR_ERR(to);
2235 if (!IS_ERR(to)) {
2236 error = do_rename(from,to);
2237 putname(to);
2238 }
2239 putname(from);
2240 return error;
2241}
2242
2243int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2244{
2245 int len;
2246
2247 len = PTR_ERR(link);
2248 if (IS_ERR(link))
2249 goto out;
2250
2251 len = strlen(link);
2252 if (len > (unsigned) buflen)
2253 len = buflen;
2254 if (copy_to_user(buffer, link, len))
2255 len = -EFAULT;
2256out:
2257 return len;
2258}
2259
2260
2261
2262
2263
2264
2265int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2266{
2267 struct nameidata nd;
2268 int res;
2269 nd.depth = 0;
2270 res = dentry->d_inode->i_op->follow_link(dentry, &nd);
2271 if (!res) {
2272 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2273 if (dentry->d_inode->i_op->put_link)
2274 dentry->d_inode->i_op->put_link(dentry, &nd);
2275 }
2276 return res;
2277}
2278
2279int vfs_follow_link(struct nameidata *nd, const char *link)
2280{
2281 return __vfs_follow_link(nd, link);
2282}
2283
2284
2285static char *page_getlink(struct dentry * dentry, struct page **ppage)
2286{
2287 struct page * page;
2288 struct address_space *mapping = dentry->d_inode->i_mapping;
2289 page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage,
2290 NULL);
2291 if (IS_ERR(page))
2292 goto sync_fail;
2293 wait_on_page_locked(page);
2294 if (!PageUptodate(page))
2295 goto async_fail;
2296 *ppage = page;
2297 return kmap(page);
2298
2299async_fail:
2300 page_cache_release(page);
2301 return ERR_PTR(-EIO);
2302
2303sync_fail:
2304 return (char*)page;
2305}
2306
2307int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2308{
2309 struct page *page = NULL;
2310 char *s = page_getlink(dentry, &page);
2311 int res = vfs_readlink(dentry,buffer,buflen,s);
2312 if (page) {
2313 kunmap(page);
2314 page_cache_release(page);
2315 }
2316 return res;
2317}
2318
2319int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
2320{
2321 struct page *page;
2322 nd_set_link(nd, page_getlink(dentry, &page));
2323 return 0;
2324}
2325
2326void page_put_link(struct dentry *dentry, struct nameidata *nd)
2327{
2328 if (!IS_ERR(nd_get_link(nd))) {
2329 struct page *page;
2330 page = find_get_page(dentry->d_inode->i_mapping, 0);
2331 if (!page)
2332 BUG();
2333 kunmap(page);
2334 page_cache_release(page);
2335 page_cache_release(page);
2336 }
2337}
2338
2339int page_symlink(struct inode *inode, const char *symname, int len)
2340{
2341 struct address_space *mapping = inode->i_mapping;
2342 struct page *page = grab_cache_page(mapping, 0);
2343 int err = -ENOMEM;
2344 char *kaddr;
2345
2346 if (!page)
2347 goto fail;
2348 err = mapping->a_ops->prepare_write(NULL, page, 0, len-1);
2349 if (err)
2350 goto fail_map;
2351 kaddr = kmap_atomic(page, KM_USER0);
2352 memcpy(kaddr, symname, len-1);
2353 kunmap_atomic(kaddr, KM_USER0);
2354 mapping->a_ops->commit_write(NULL, page, 0, len-1);
2355
2356
2357
2358
2359
2360
2361
2362 if (!PageUptodate(page)) {
2363 err = mapping->a_ops->readpage(NULL, page);
2364 wait_on_page_locked(page);
2365 } else {
2366 unlock_page(page);
2367 }
2368 page_cache_release(page);
2369 if (err < 0)
2370 goto fail;
2371 mark_inode_dirty(inode);
2372 return 0;
2373fail_map:
2374 unlock_page(page);
2375 page_cache_release(page);
2376fail:
2377 return err;
2378}
2379
2380struct inode_operations page_symlink_inode_operations = {
2381 .readlink = generic_readlink,
2382 .follow_link = page_follow_link_light,
2383 .put_link = page_put_link,
2384};
2385
2386EXPORT_SYMBOL(__user_walk);
2387EXPORT_SYMBOL(follow_down);
2388EXPORT_SYMBOL(follow_up);
2389EXPORT_SYMBOL(get_write_access);
2390EXPORT_SYMBOL(getname);
2391EXPORT_SYMBOL(lock_rename);
2392EXPORT_SYMBOL(lookup_hash);
2393EXPORT_SYMBOL(lookup_one_len);
2394EXPORT_SYMBOL(page_follow_link_light);
2395EXPORT_SYMBOL(page_put_link);
2396EXPORT_SYMBOL(page_readlink);
2397EXPORT_SYMBOL(page_symlink);
2398EXPORT_SYMBOL(page_symlink_inode_operations);
2399EXPORT_SYMBOL(path_lookup);
2400EXPORT_SYMBOL(path_release);
2401EXPORT_SYMBOL(path_walk);
2402EXPORT_SYMBOL(permission);
2403EXPORT_SYMBOL(unlock_rename);
2404EXPORT_SYMBOL(vfs_create);
2405EXPORT_SYMBOL(vfs_follow_link);
2406EXPORT_SYMBOL(vfs_link);
2407EXPORT_SYMBOL(vfs_mkdir);
2408EXPORT_SYMBOL(vfs_mknod);
2409EXPORT_SYMBOL(generic_permission);
2410EXPORT_SYMBOL(vfs_readlink);
2411EXPORT_SYMBOL(vfs_rename);
2412EXPORT_SYMBOL(vfs_rmdir);
2413EXPORT_SYMBOL(vfs_symlink);
2414EXPORT_SYMBOL(vfs_unlink);
2415EXPORT_SYMBOL(dentry_unhash);
2416EXPORT_SYMBOL(generic_readlink);
2417