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