1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/init.h>
18#include <linux/export.h>
19#include <linux/kernel.h>
20#include <linux/slab.h>
21#include <linux/fs.h>
22#include <linux/namei.h>
23#include <linux/pagemap.h>
24#include <linux/fsnotify.h>
25#include <linux/personality.h>
26#include <linux/security.h>
27#include <linux/ima.h>
28#include <linux/syscalls.h>
29#include <linux/mount.h>
30#include <linux/audit.h>
31#include <linux/capability.h>
32#include <linux/file.h>
33#include <linux/fcntl.h>
34#include <linux/device_cgroup.h>
35#include <linux/fs_struct.h>
36#include <linux/posix_acl.h>
37#include <asm/uaccess.h>
38
39#include "internal.h"
40#include "mount.h"
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
114
115
116
117
118
119
120static char *getname_flags(const char __user *filename, int flags, int *empty)
121{
122 char *result = __getname(), *err;
123 int len;
124
125 if (unlikely(!result))
126 return ERR_PTR(-ENOMEM);
127
128 len = strncpy_from_user(result, filename, PATH_MAX);
129 err = ERR_PTR(len);
130 if (unlikely(len < 0))
131 goto error;
132
133
134 if (unlikely(!len)) {
135 if (empty)
136 *empty = 1;
137 err = ERR_PTR(-ENOENT);
138 if (!(flags & LOOKUP_EMPTY))
139 goto error;
140 }
141
142 err = ERR_PTR(-ENAMETOOLONG);
143 if (likely(len < PATH_MAX)) {
144 audit_getname(result);
145 return result;
146 }
147
148error:
149 __putname(result);
150 return err;
151}
152
153char *getname(const char __user * filename)
154{
155 return getname_flags(filename, 0, NULL);
156}
157
158#ifdef CONFIG_AUDITSYSCALL
159void putname(const char *name)
160{
161 if (unlikely(!audit_dummy_context()))
162 audit_putname(name);
163 else
164 __putname(name);
165}
166EXPORT_SYMBOL(putname);
167#endif
168
169static int check_acl(struct inode *inode, int mask)
170{
171#ifdef CONFIG_FS_POSIX_ACL
172 struct posix_acl *acl;
173
174 if (mask & MAY_NOT_BLOCK) {
175 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
176 if (!acl)
177 return -EAGAIN;
178
179 if (acl == ACL_NOT_CACHED)
180 return -ECHILD;
181 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
182 }
183
184 acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
185
186
187
188
189
190
191
192
193
194 if (acl == ACL_NOT_CACHED) {
195 if (inode->i_op->get_acl) {
196 acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
197 if (IS_ERR(acl))
198 return PTR_ERR(acl);
199 } else {
200 set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
201 return -EAGAIN;
202 }
203 }
204
205 if (acl) {
206 int error = posix_acl_permission(inode, acl, mask);
207 posix_acl_release(acl);
208 return error;
209 }
210#endif
211
212 return -EAGAIN;
213}
214
215
216
217
218static int acl_permission_check(struct inode *inode, int mask)
219{
220 unsigned int mode = inode->i_mode;
221
222 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
223 mode >>= 6;
224 else {
225 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
226 int error = check_acl(inode, mask);
227 if (error != -EAGAIN)
228 return error;
229 }
230
231 if (in_group_p(inode->i_gid))
232 mode >>= 3;
233 }
234
235
236
237
238 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
239 return 0;
240 return -EACCES;
241}
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257int generic_permission(struct inode *inode, int mask)
258{
259 int ret;
260
261
262
263
264 ret = acl_permission_check(inode, mask);
265 if (ret != -EACCES)
266 return ret;
267
268 if (S_ISDIR(inode->i_mode)) {
269
270 if (inode_capable(inode, CAP_DAC_OVERRIDE))
271 return 0;
272 if (!(mask & MAY_WRITE))
273 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
274 return 0;
275 return -EACCES;
276 }
277
278
279
280
281
282 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
283 if (inode_capable(inode, CAP_DAC_OVERRIDE))
284 return 0;
285
286
287
288
289 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
290 if (mask == MAY_READ)
291 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
292 return 0;
293
294 return -EACCES;
295}
296
297
298
299
300
301
302
303static inline int do_inode_permission(struct inode *inode, int mask)
304{
305 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
306 if (likely(inode->i_op->permission))
307 return inode->i_op->permission(inode, mask);
308
309
310 spin_lock(&inode->i_lock);
311 inode->i_opflags |= IOP_FASTPERM;
312 spin_unlock(&inode->i_lock);
313 }
314 return generic_permission(inode, mask);
315}
316
317
318
319
320
321
322
323
324
325
326
327
328
329int __inode_permission(struct inode *inode, int mask)
330{
331 int retval;
332
333 if (unlikely(mask & MAY_WRITE)) {
334
335
336
337 if (IS_IMMUTABLE(inode))
338 return -EACCES;
339 }
340
341 retval = do_inode_permission(inode, mask);
342 if (retval)
343 return retval;
344
345 retval = devcgroup_inode_permission(inode, mask);
346 if (retval)
347 return retval;
348
349 return security_inode_permission(inode, mask);
350}
351
352
353
354
355
356
357
358
359
360static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
361{
362 if (unlikely(mask & MAY_WRITE)) {
363 umode_t mode = inode->i_mode;
364
365
366 if ((sb->s_flags & MS_RDONLY) &&
367 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
368 return -EROFS;
369 }
370 return 0;
371}
372
373
374
375
376
377
378
379
380
381
382
383
384int inode_permission(struct inode *inode, int mask)
385{
386 int retval;
387
388 retval = sb_permission(inode->i_sb, inode, mask);
389 if (retval)
390 return retval;
391 return __inode_permission(inode, mask);
392}
393
394
395
396
397
398
399
400void path_get(struct path *path)
401{
402 mntget(path->mnt);
403 dget(path->dentry);
404}
405EXPORT_SYMBOL(path_get);
406
407
408
409
410
411
412
413void path_put(struct path *path)
414{
415 dput(path->dentry);
416 mntput(path->mnt);
417}
418EXPORT_SYMBOL(path_put);
419
420
421
422
423
424
425
426
427
428
429
430
431static inline void lock_rcu_walk(void)
432{
433 br_read_lock(&vfsmount_lock);
434 rcu_read_lock();
435}
436
437static inline void unlock_rcu_walk(void)
438{
439 rcu_read_unlock();
440 br_read_unlock(&vfsmount_lock);
441}
442
443
444
445
446
447
448
449
450
451
452
453static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
454{
455 struct fs_struct *fs = current->fs;
456 struct dentry *parent = nd->path.dentry;
457 int want_root = 0;
458
459 BUG_ON(!(nd->flags & LOOKUP_RCU));
460 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
461 want_root = 1;
462 spin_lock(&fs->lock);
463 if (nd->root.mnt != fs->root.mnt ||
464 nd->root.dentry != fs->root.dentry)
465 goto err_root;
466 }
467 spin_lock(&parent->d_lock);
468 if (!dentry) {
469 if (!__d_rcu_to_refcount(parent, nd->seq))
470 goto err_parent;
471 BUG_ON(nd->inode != parent->d_inode);
472 } else {
473 if (dentry->d_parent != parent)
474 goto err_parent;
475 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
476 if (!__d_rcu_to_refcount(dentry, nd->seq))
477 goto err_child;
478
479
480
481
482
483
484 BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
485 BUG_ON(!parent->d_count);
486 parent->d_count++;
487 spin_unlock(&dentry->d_lock);
488 }
489 spin_unlock(&parent->d_lock);
490 if (want_root) {
491 path_get(&nd->root);
492 spin_unlock(&fs->lock);
493 }
494 mntget(nd->path.mnt);
495
496 unlock_rcu_walk();
497 nd->flags &= ~LOOKUP_RCU;
498 return 0;
499
500err_child:
501 spin_unlock(&dentry->d_lock);
502err_parent:
503 spin_unlock(&parent->d_lock);
504err_root:
505 if (want_root)
506 spin_unlock(&fs->lock);
507 return -ECHILD;
508}
509
510static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
511{
512 return dentry->d_op->d_revalidate(dentry, flags);
513}
514
515
516
517
518
519
520
521
522
523
524
525static int complete_walk(struct nameidata *nd)
526{
527 struct dentry *dentry = nd->path.dentry;
528 int status;
529
530 if (nd->flags & LOOKUP_RCU) {
531 nd->flags &= ~LOOKUP_RCU;
532 if (!(nd->flags & LOOKUP_ROOT))
533 nd->root.mnt = NULL;
534 spin_lock(&dentry->d_lock);
535 if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
536 spin_unlock(&dentry->d_lock);
537 unlock_rcu_walk();
538 return -ECHILD;
539 }
540 BUG_ON(nd->inode != dentry->d_inode);
541 spin_unlock(&dentry->d_lock);
542 mntget(nd->path.mnt);
543 unlock_rcu_walk();
544 }
545
546 if (likely(!(nd->flags & LOOKUP_JUMPED)))
547 return 0;
548
549 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
550 return 0;
551
552 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
553 return 0;
554
555
556 status = d_revalidate(dentry, nd->flags);
557 if (status > 0)
558 return 0;
559
560 if (!status)
561 status = -ESTALE;
562
563 path_put(&nd->path);
564 return status;
565}
566
567static __always_inline void set_root(struct nameidata *nd)
568{
569 if (!nd->root.mnt)
570 get_fs_root(current->fs, &nd->root);
571}
572
573static int link_path_walk(const char *, struct nameidata *);
574
575static __always_inline void set_root_rcu(struct nameidata *nd)
576{
577 if (!nd->root.mnt) {
578 struct fs_struct *fs = current->fs;
579 unsigned seq;
580
581 do {
582 seq = read_seqcount_begin(&fs->seq);
583 nd->root = fs->root;
584 nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
585 } while (read_seqcount_retry(&fs->seq, seq));
586 }
587}
588
589static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
590{
591 int ret;
592
593 if (IS_ERR(link))
594 goto fail;
595
596 if (*link == '/') {
597 set_root(nd);
598 path_put(&nd->path);
599 nd->path = nd->root;
600 path_get(&nd->root);
601 nd->flags |= LOOKUP_JUMPED;
602 }
603 nd->inode = nd->path.dentry->d_inode;
604
605 ret = link_path_walk(link, nd);
606 return ret;
607fail:
608 path_put(&nd->path);
609 return PTR_ERR(link);
610}
611
612static void path_put_conditional(struct path *path, struct nameidata *nd)
613{
614 dput(path->dentry);
615 if (path->mnt != nd->path.mnt)
616 mntput(path->mnt);
617}
618
619static inline void path_to_nameidata(const struct path *path,
620 struct nameidata *nd)
621{
622 if (!(nd->flags & LOOKUP_RCU)) {
623 dput(nd->path.dentry);
624 if (nd->path.mnt != path->mnt)
625 mntput(nd->path.mnt);
626 }
627 nd->path.mnt = path->mnt;
628 nd->path.dentry = path->dentry;
629}
630
631
632
633
634
635void nd_jump_link(struct nameidata *nd, struct path *path)
636{
637 path_put(&nd->path);
638
639 nd->path = *path;
640 nd->inode = nd->path.dentry->d_inode;
641 nd->flags |= LOOKUP_JUMPED;
642
643 BUG_ON(nd->inode->i_op->follow_link);
644}
645
646static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
647{
648 struct inode *inode = link->dentry->d_inode;
649 if (inode->i_op->put_link)
650 inode->i_op->put_link(link->dentry, nd, cookie);
651 path_put(link);
652}
653
654int sysctl_protected_symlinks __read_mostly = 1;
655int sysctl_protected_hardlinks __read_mostly = 1;
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673static inline int may_follow_link(struct path *link, struct nameidata *nd)
674{
675 const struct inode *inode;
676 const struct inode *parent;
677
678 if (!sysctl_protected_symlinks)
679 return 0;
680
681
682 inode = link->dentry->d_inode;
683 if (current_cred()->fsuid == inode->i_uid)
684 return 0;
685
686
687 parent = nd->path.dentry->d_inode;
688 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
689 return 0;
690
691
692 if (parent->i_uid == inode->i_uid)
693 return 0;
694
695 path_put_conditional(link, nd);
696 path_put(&nd->path);
697 audit_log_link_denied("follow_link", link);
698 return -EACCES;
699}
700
701
702
703
704
705
706
707
708
709
710
711
712
713static bool safe_hardlink_source(struct inode *inode)
714{
715 umode_t mode = inode->i_mode;
716
717
718 if (!S_ISREG(mode))
719 return false;
720
721
722 if (mode & S_ISUID)
723 return false;
724
725
726 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
727 return false;
728
729
730 if (inode_permission(inode, MAY_READ | MAY_WRITE))
731 return false;
732
733 return true;
734}
735
736
737
738
739
740
741
742
743
744
745
746
747
748static int may_linkat(struct path *link)
749{
750 const struct cred *cred;
751 struct inode *inode;
752
753 if (!sysctl_protected_hardlinks)
754 return 0;
755
756 cred = current_cred();
757 inode = link->dentry->d_inode;
758
759
760
761
762 if (cred->fsuid == inode->i_uid || safe_hardlink_source(inode) ||
763 capable(CAP_FOWNER))
764 return 0;
765
766 audit_log_link_denied("linkat", link);
767 return -EPERM;
768}
769
770static __always_inline int
771follow_link(struct path *link, struct nameidata *nd, void **p)
772{
773 struct dentry *dentry = link->dentry;
774 int error;
775 char *s;
776
777 BUG_ON(nd->flags & LOOKUP_RCU);
778
779 if (link->mnt == nd->path.mnt)
780 mntget(link->mnt);
781
782 error = -ELOOP;
783 if (unlikely(current->total_link_count >= 40))
784 goto out_put_nd_path;
785
786 cond_resched();
787 current->total_link_count++;
788
789 touch_atime(link);
790 nd_set_link(nd, NULL);
791
792 error = security_inode_follow_link(link->dentry, nd);
793 if (error)
794 goto out_put_nd_path;
795
796 nd->last_type = LAST_BIND;
797 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
798 error = PTR_ERR(*p);
799 if (IS_ERR(*p))
800 goto out_put_nd_path;
801
802 error = 0;
803 s = nd_get_link(nd);
804 if (s) {
805 error = __vfs_follow_link(nd, s);
806 if (unlikely(error))
807 put_link(nd, link, *p);
808 }
809
810 return error;
811
812out_put_nd_path:
813 path_put(&nd->path);
814 path_put(link);
815 return error;
816}
817
818static int follow_up_rcu(struct path *path)
819{
820 struct mount *mnt = real_mount(path->mnt);
821 struct mount *parent;
822 struct dentry *mountpoint;
823
824 parent = mnt->mnt_parent;
825 if (&parent->mnt == path->mnt)
826 return 0;
827 mountpoint = mnt->mnt_mountpoint;
828 path->dentry = mountpoint;
829 path->mnt = &parent->mnt;
830 return 1;
831}
832
833
834
835
836
837
838
839
840
841
842
843int follow_up(struct path *path)
844{
845 struct mount *mnt = real_mount(path->mnt);
846 struct mount *parent;
847 struct dentry *mountpoint;
848
849 br_read_lock(&vfsmount_lock);
850 parent = mnt->mnt_parent;
851 if (parent == mnt) {
852 br_read_unlock(&vfsmount_lock);
853 return 0;
854 }
855 mntget(&parent->mnt);
856 mountpoint = dget(mnt->mnt_mountpoint);
857 br_read_unlock(&vfsmount_lock);
858 dput(path->dentry);
859 path->dentry = mountpoint;
860 mntput(path->mnt);
861 path->mnt = &parent->mnt;
862 return 1;
863}
864
865
866
867
868
869
870static int follow_automount(struct path *path, unsigned flags,
871 bool *need_mntput)
872{
873 struct vfsmount *mnt;
874 int err;
875
876 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
877 return -EREMOTE;
878
879
880
881
882
883
884
885
886
887
888
889
890 if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
891 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
892 path->dentry->d_inode)
893 return -EISDIR;
894
895 current->total_link_count++;
896 if (current->total_link_count >= 40)
897 return -ELOOP;
898
899 mnt = path->dentry->d_op->d_automount(path);
900 if (IS_ERR(mnt)) {
901
902
903
904
905
906
907
908
909
910 if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
911 return -EREMOTE;
912 return PTR_ERR(mnt);
913 }
914
915 if (!mnt)
916 return 0;
917
918 if (!*need_mntput) {
919
920 mntget(path->mnt);
921 *need_mntput = true;
922 }
923 err = finish_automount(mnt, path);
924
925 switch (err) {
926 case -EBUSY:
927
928 return 0;
929 case 0:
930 path_put(path);
931 path->mnt = mnt;
932 path->dentry = dget(mnt->mnt_root);
933 return 0;
934 default:
935 return err;
936 }
937
938}
939
940
941
942
943
944
945
946
947
948
949
950static int follow_managed(struct path *path, unsigned flags)
951{
952 struct vfsmount *mnt = path->mnt;
953 unsigned managed;
954 bool need_mntput = false;
955 int ret = 0;
956
957
958
959
960 while (managed = ACCESS_ONCE(path->dentry->d_flags),
961 managed &= DCACHE_MANAGED_DENTRY,
962 unlikely(managed != 0)) {
963
964
965 if (managed & DCACHE_MANAGE_TRANSIT) {
966 BUG_ON(!path->dentry->d_op);
967 BUG_ON(!path->dentry->d_op->d_manage);
968 ret = path->dentry->d_op->d_manage(path->dentry, false);
969 if (ret < 0)
970 break;
971 }
972
973
974 if (managed & DCACHE_MOUNTED) {
975 struct vfsmount *mounted = lookup_mnt(path);
976 if (mounted) {
977 dput(path->dentry);
978 if (need_mntput)
979 mntput(path->mnt);
980 path->mnt = mounted;
981 path->dentry = dget(mounted->mnt_root);
982 need_mntput = true;
983 continue;
984 }
985
986
987
988
989
990 }
991
992
993 if (managed & DCACHE_NEED_AUTOMOUNT) {
994 ret = follow_automount(path, flags, &need_mntput);
995 if (ret < 0)
996 break;
997 continue;
998 }
999
1000
1001 break;
1002 }
1003
1004 if (need_mntput && path->mnt == mnt)
1005 mntput(path->mnt);
1006 if (ret == -EISDIR)
1007 ret = 0;
1008 return ret < 0 ? ret : need_mntput;
1009}
1010
1011int follow_down_one(struct path *path)
1012{
1013 struct vfsmount *mounted;
1014
1015 mounted = lookup_mnt(path);
1016 if (mounted) {
1017 dput(path->dentry);
1018 mntput(path->mnt);
1019 path->mnt = mounted;
1020 path->dentry = dget(mounted->mnt_root);
1021 return 1;
1022 }
1023 return 0;
1024}
1025
1026static inline bool managed_dentry_might_block(struct dentry *dentry)
1027{
1028 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
1029 dentry->d_op->d_manage(dentry, true) < 0);
1030}
1031
1032
1033
1034
1035
1036static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1037 struct inode **inode)
1038{
1039 for (;;) {
1040 struct mount *mounted;
1041
1042
1043
1044
1045 if (unlikely(managed_dentry_might_block(path->dentry)))
1046 return false;
1047
1048 if (!d_mountpoint(path->dentry))
1049 break;
1050
1051 mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1052 if (!mounted)
1053 break;
1054 path->mnt = &mounted->mnt;
1055 path->dentry = mounted->mnt.mnt_root;
1056 nd->flags |= LOOKUP_JUMPED;
1057 nd->seq = read_seqcount_begin(&path->dentry->d_seq);
1058
1059
1060
1061
1062
1063 *inode = path->dentry->d_inode;
1064 }
1065 return true;
1066}
1067
1068static void follow_mount_rcu(struct nameidata *nd)
1069{
1070 while (d_mountpoint(nd->path.dentry)) {
1071 struct mount *mounted;
1072 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
1073 if (!mounted)
1074 break;
1075 nd->path.mnt = &mounted->mnt;
1076 nd->path.dentry = mounted->mnt.mnt_root;
1077 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1078 }
1079}
1080
1081static int follow_dotdot_rcu(struct nameidata *nd)
1082{
1083 set_root_rcu(nd);
1084
1085 while (1) {
1086 if (nd->path.dentry == nd->root.dentry &&
1087 nd->path.mnt == nd->root.mnt) {
1088 break;
1089 }
1090 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1091 struct dentry *old = nd->path.dentry;
1092 struct dentry *parent = old->d_parent;
1093 unsigned seq;
1094
1095 seq = read_seqcount_begin(&parent->d_seq);
1096 if (read_seqcount_retry(&old->d_seq, nd->seq))
1097 goto failed;
1098 nd->path.dentry = parent;
1099 nd->seq = seq;
1100 break;
1101 }
1102 if (!follow_up_rcu(&nd->path))
1103 break;
1104 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1105 }
1106 follow_mount_rcu(nd);
1107 nd->inode = nd->path.dentry->d_inode;
1108 return 0;
1109
1110failed:
1111 nd->flags &= ~LOOKUP_RCU;
1112 if (!(nd->flags & LOOKUP_ROOT))
1113 nd->root.mnt = NULL;
1114 unlock_rcu_walk();
1115 return -ECHILD;
1116}
1117
1118
1119
1120
1121
1122
1123int follow_down(struct path *path)
1124{
1125 unsigned managed;
1126 int ret;
1127
1128 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1129 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140 if (managed & DCACHE_MANAGE_TRANSIT) {
1141 BUG_ON(!path->dentry->d_op);
1142 BUG_ON(!path->dentry->d_op->d_manage);
1143 ret = path->dentry->d_op->d_manage(
1144 path->dentry, false);
1145 if (ret < 0)
1146 return ret == -EISDIR ? 0 : ret;
1147 }
1148
1149
1150 if (managed & DCACHE_MOUNTED) {
1151 struct vfsmount *mounted = lookup_mnt(path);
1152 if (!mounted)
1153 break;
1154 dput(path->dentry);
1155 mntput(path->mnt);
1156 path->mnt = mounted;
1157 path->dentry = dget(mounted->mnt_root);
1158 continue;
1159 }
1160
1161
1162 break;
1163 }
1164 return 0;
1165}
1166
1167
1168
1169
1170static void follow_mount(struct path *path)
1171{
1172 while (d_mountpoint(path->dentry)) {
1173 struct vfsmount *mounted = lookup_mnt(path);
1174 if (!mounted)
1175 break;
1176 dput(path->dentry);
1177 mntput(path->mnt);
1178 path->mnt = mounted;
1179 path->dentry = dget(mounted->mnt_root);
1180 }
1181}
1182
1183static void follow_dotdot(struct nameidata *nd)
1184{
1185 set_root(nd);
1186
1187 while(1) {
1188 struct dentry *old = nd->path.dentry;
1189
1190 if (nd->path.dentry == nd->root.dentry &&
1191 nd->path.mnt == nd->root.mnt) {
1192 break;
1193 }
1194 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1195
1196 nd->path.dentry = dget_parent(nd->path.dentry);
1197 dput(old);
1198 break;
1199 }
1200 if (!follow_up(&nd->path))
1201 break;
1202 }
1203 follow_mount(&nd->path);
1204 nd->inode = nd->path.dentry->d_inode;
1205}
1206
1207
1208
1209
1210
1211
1212
1213
1214static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1215 unsigned int flags, bool *need_lookup)
1216{
1217 struct dentry *dentry;
1218 int error;
1219
1220 *need_lookup = false;
1221 dentry = d_lookup(dir, name);
1222 if (dentry) {
1223 if (d_need_lookup(dentry)) {
1224 *need_lookup = true;
1225 } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1226 error = d_revalidate(dentry, flags);
1227 if (unlikely(error <= 0)) {
1228 if (error < 0) {
1229 dput(dentry);
1230 return ERR_PTR(error);
1231 } else if (!d_invalidate(dentry)) {
1232 dput(dentry);
1233 dentry = NULL;
1234 }
1235 }
1236 }
1237 }
1238
1239 if (!dentry) {
1240 dentry = d_alloc(dir, name);
1241 if (unlikely(!dentry))
1242 return ERR_PTR(-ENOMEM);
1243
1244 *need_lookup = true;
1245 }
1246 return dentry;
1247}
1248
1249
1250
1251
1252
1253
1254
1255static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1256 unsigned int flags)
1257{
1258 struct dentry *old;
1259
1260
1261 if (unlikely(IS_DEADDIR(dir))) {
1262 dput(dentry);
1263 return ERR_PTR(-ENOENT);
1264 }
1265
1266 old = dir->i_op->lookup(dir, dentry, flags);
1267 if (unlikely(old)) {
1268 dput(dentry);
1269 dentry = old;
1270 }
1271 return dentry;
1272}
1273
1274static struct dentry *__lookup_hash(struct qstr *name,
1275 struct dentry *base, unsigned int flags)
1276{
1277 bool need_lookup;
1278 struct dentry *dentry;
1279
1280 dentry = lookup_dcache(name, base, flags, &need_lookup);
1281 if (!need_lookup)
1282 return dentry;
1283
1284 return lookup_real(base->d_inode, dentry, flags);
1285}
1286
1287
1288
1289
1290
1291
1292static int lookup_fast(struct nameidata *nd, struct qstr *name,
1293 struct path *path, struct inode **inode)
1294{
1295 struct vfsmount *mnt = nd->path.mnt;
1296 struct dentry *dentry, *parent = nd->path.dentry;
1297 int need_reval = 1;
1298 int status = 1;
1299 int err;
1300
1301
1302
1303
1304
1305
1306 if (nd->flags & LOOKUP_RCU) {
1307 unsigned seq;
1308 dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
1309 if (!dentry)
1310 goto unlazy;
1311
1312
1313
1314
1315
1316 *inode = dentry->d_inode;
1317 if (read_seqcount_retry(&dentry->d_seq, seq))
1318 return -ECHILD;
1319
1320
1321
1322
1323
1324
1325
1326
1327 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1328 return -ECHILD;
1329 nd->seq = seq;
1330
1331 if (unlikely(d_need_lookup(dentry)))
1332 goto unlazy;
1333 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1334 status = d_revalidate(dentry, nd->flags);
1335 if (unlikely(status <= 0)) {
1336 if (status != -ECHILD)
1337 need_reval = 0;
1338 goto unlazy;
1339 }
1340 }
1341 path->mnt = mnt;
1342 path->dentry = dentry;
1343 if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1344 goto unlazy;
1345 if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1346 goto unlazy;
1347 return 0;
1348unlazy:
1349 if (unlazy_walk(nd, dentry))
1350 return -ECHILD;
1351 } else {
1352 dentry = __d_lookup(parent, name);
1353 }
1354
1355 if (unlikely(!dentry))
1356 goto need_lookup;
1357
1358 if (unlikely(d_need_lookup(dentry))) {
1359 dput(dentry);
1360 goto need_lookup;
1361 }
1362
1363 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1364 status = d_revalidate(dentry, nd->flags);
1365 if (unlikely(status <= 0)) {
1366 if (status < 0) {
1367 dput(dentry);
1368 return status;
1369 }
1370 if (!d_invalidate(dentry)) {
1371 dput(dentry);
1372 goto need_lookup;
1373 }
1374 }
1375
1376 path->mnt = mnt;
1377 path->dentry = dentry;
1378 err = follow_managed(path, nd->flags);
1379 if (unlikely(err < 0)) {
1380 path_put_conditional(path, nd);
1381 return err;
1382 }
1383 if (err)
1384 nd->flags |= LOOKUP_JUMPED;
1385 *inode = path->dentry->d_inode;
1386 return 0;
1387
1388need_lookup:
1389 return 1;
1390}
1391
1392
1393static int lookup_slow(struct nameidata *nd, struct qstr *name,
1394 struct path *path)
1395{
1396 struct dentry *dentry, *parent;
1397 int err;
1398
1399 parent = nd->path.dentry;
1400 BUG_ON(nd->inode != parent->d_inode);
1401
1402 mutex_lock(&parent->d_inode->i_mutex);
1403 dentry = __lookup_hash(name, parent, nd->flags);
1404 mutex_unlock(&parent->d_inode->i_mutex);
1405 if (IS_ERR(dentry))
1406 return PTR_ERR(dentry);
1407 path->mnt = nd->path.mnt;
1408 path->dentry = dentry;
1409 err = follow_managed(path, nd->flags);
1410 if (unlikely(err < 0)) {
1411 path_put_conditional(path, nd);
1412 return err;
1413 }
1414 if (err)
1415 nd->flags |= LOOKUP_JUMPED;
1416 return 0;
1417}
1418
1419static inline int may_lookup(struct nameidata *nd)
1420{
1421 if (nd->flags & LOOKUP_RCU) {
1422 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1423 if (err != -ECHILD)
1424 return err;
1425 if (unlazy_walk(nd, NULL))
1426 return -ECHILD;
1427 }
1428 return inode_permission(nd->inode, MAY_EXEC);
1429}
1430
1431static inline int handle_dots(struct nameidata *nd, int type)
1432{
1433 if (type == LAST_DOTDOT) {
1434 if (nd->flags & LOOKUP_RCU) {
1435 if (follow_dotdot_rcu(nd))
1436 return -ECHILD;
1437 } else
1438 follow_dotdot(nd);
1439 }
1440 return 0;
1441}
1442
1443static void terminate_walk(struct nameidata *nd)
1444{
1445 if (!(nd->flags & LOOKUP_RCU)) {
1446 path_put(&nd->path);
1447 } else {
1448 nd->flags &= ~LOOKUP_RCU;
1449 if (!(nd->flags & LOOKUP_ROOT))
1450 nd->root.mnt = NULL;
1451 unlock_rcu_walk();
1452 }
1453}
1454
1455
1456
1457
1458
1459
1460
1461static inline int should_follow_link(struct inode *inode, int follow)
1462{
1463 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1464 if (likely(inode->i_op->follow_link))
1465 return follow;
1466
1467
1468 spin_lock(&inode->i_lock);
1469 inode->i_opflags |= IOP_NOFOLLOW;
1470 spin_unlock(&inode->i_lock);
1471 }
1472 return 0;
1473}
1474
1475static inline int walk_component(struct nameidata *nd, struct path *path,
1476 struct qstr *name, int type, int follow)
1477{
1478 struct inode *inode;
1479 int err;
1480
1481
1482
1483
1484
1485 if (unlikely(type != LAST_NORM))
1486 return handle_dots(nd, type);
1487 err = lookup_fast(nd, name, path, &inode);
1488 if (unlikely(err)) {
1489 if (err < 0)
1490 goto out_err;
1491
1492 err = lookup_slow(nd, name, path);
1493 if (err < 0)
1494 goto out_err;
1495
1496 inode = path->dentry->d_inode;
1497 }
1498 err = -ENOENT;
1499 if (!inode)
1500 goto out_path_put;
1501
1502 if (should_follow_link(inode, follow)) {
1503 if (nd->flags & LOOKUP_RCU) {
1504 if (unlikely(unlazy_walk(nd, path->dentry))) {
1505 err = -ECHILD;
1506 goto out_err;
1507 }
1508 }
1509 BUG_ON(inode != path->dentry->d_inode);
1510 return 1;
1511 }
1512 path_to_nameidata(path, nd);
1513 nd->inode = inode;
1514 return 0;
1515
1516out_path_put:
1517 path_to_nameidata(path, nd);
1518out_err:
1519 terminate_walk(nd);
1520 return err;
1521}
1522
1523
1524
1525
1526
1527
1528
1529
1530static inline int nested_symlink(struct path *path, struct nameidata *nd)
1531{
1532 int res;
1533
1534 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1535 path_put_conditional(path, nd);
1536 path_put(&nd->path);
1537 return -ELOOP;
1538 }
1539 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1540
1541 nd->depth++;
1542 current->link_count++;
1543
1544 do {
1545 struct path link = *path;
1546 void *cookie;
1547
1548 res = follow_link(&link, nd, &cookie);
1549 if (res)
1550 break;
1551 res = walk_component(nd, path, &nd->last,
1552 nd->last_type, LOOKUP_FOLLOW);
1553 put_link(nd, &link, cookie);
1554 } while (res > 0);
1555
1556 current->link_count--;
1557 nd->depth--;
1558 return res;
1559}
1560
1561
1562
1563
1564
1565
1566
1567static inline int can_lookup(struct inode *inode)
1568{
1569 if (likely(inode->i_opflags & IOP_LOOKUP))
1570 return 1;
1571 if (likely(!inode->i_op->lookup))
1572 return 0;
1573
1574
1575 spin_lock(&inode->i_lock);
1576 inode->i_opflags |= IOP_LOOKUP;
1577 spin_unlock(&inode->i_lock);
1578 return 1;
1579}
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603#ifdef CONFIG_DCACHE_WORD_ACCESS
1604
1605#include <asm/word-at-a-time.h>
1606
1607#ifdef CONFIG_64BIT
1608
1609static inline unsigned int fold_hash(unsigned long hash)
1610{
1611 hash += hash >> (8*sizeof(int));
1612 return hash;
1613}
1614
1615#else
1616
1617#define fold_hash(x) (x)
1618
1619#endif
1620
1621unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1622{
1623 unsigned long a, mask;
1624 unsigned long hash = 0;
1625
1626 for (;;) {
1627 a = load_unaligned_zeropad(name);
1628 if (len < sizeof(unsigned long))
1629 break;
1630 hash += a;
1631 hash *= 9;
1632 name += sizeof(unsigned long);
1633 len -= sizeof(unsigned long);
1634 if (!len)
1635 goto done;
1636 }
1637 mask = ~(~0ul << len*8);
1638 hash += mask & a;
1639done:
1640 return fold_hash(hash);
1641}
1642EXPORT_SYMBOL(full_name_hash);
1643
1644
1645
1646
1647
1648static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1649{
1650 unsigned long a, b, adata, bdata, mask, hash, len;
1651 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1652
1653 hash = a = 0;
1654 len = -sizeof(unsigned long);
1655 do {
1656 hash = (hash + a) * 9;
1657 len += sizeof(unsigned long);
1658 a = load_unaligned_zeropad(name+len);
1659 b = a ^ REPEAT_BYTE('/');
1660 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1661
1662 adata = prep_zero_mask(a, adata, &constants);
1663 bdata = prep_zero_mask(b, bdata, &constants);
1664
1665 mask = create_zero_mask(adata | bdata);
1666
1667 hash += a & zero_bytemask(mask);
1668 *hashp = fold_hash(hash);
1669
1670 return len + find_zero(mask);
1671}
1672
1673#else
1674
1675unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1676{
1677 unsigned long hash = init_name_hash();
1678 while (len--)
1679 hash = partial_name_hash(*name++, hash);
1680 return end_name_hash(hash);
1681}
1682EXPORT_SYMBOL(full_name_hash);
1683
1684
1685
1686
1687
1688static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1689{
1690 unsigned long hash = init_name_hash();
1691 unsigned long len = 0, c;
1692
1693 c = (unsigned char)*name;
1694 do {
1695 len++;
1696 hash = partial_name_hash(c, hash);
1697 c = (unsigned char)name[len];
1698 } while (c && c != '/');
1699 *hashp = end_name_hash(hash);
1700 return len;
1701}
1702
1703#endif
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713static int link_path_walk(const char *name, struct nameidata *nd)
1714{
1715 struct path next;
1716 int err;
1717
1718 while (*name=='/')
1719 name++;
1720 if (!*name)
1721 return 0;
1722
1723
1724 for(;;) {
1725 struct qstr this;
1726 long len;
1727 int type;
1728
1729 err = may_lookup(nd);
1730 if (err)
1731 break;
1732
1733 len = hash_name(name, &this.hash);
1734 this.name = name;
1735 this.len = len;
1736
1737 type = LAST_NORM;
1738 if (name[0] == '.') switch (len) {
1739 case 2:
1740 if (name[1] == '.') {
1741 type = LAST_DOTDOT;
1742 nd->flags |= LOOKUP_JUMPED;
1743 }
1744 break;
1745 case 1:
1746 type = LAST_DOT;
1747 }
1748 if (likely(type == LAST_NORM)) {
1749 struct dentry *parent = nd->path.dentry;
1750 nd->flags &= ~LOOKUP_JUMPED;
1751 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1752 err = parent->d_op->d_hash(parent, nd->inode,
1753 &this);
1754 if (err < 0)
1755 break;
1756 }
1757 }
1758
1759 if (!name[len])
1760 goto last_component;
1761
1762
1763
1764
1765 do {
1766 len++;
1767 } while (unlikely(name[len] == '/'));
1768 if (!name[len])
1769 goto last_component;
1770 name += len;
1771
1772 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1773 if (err < 0)
1774 return err;
1775
1776 if (err) {
1777 err = nested_symlink(&next, nd);
1778 if (err)
1779 return err;
1780 }
1781 if (can_lookup(nd->inode))
1782 continue;
1783 err = -ENOTDIR;
1784 break;
1785
1786
1787last_component:
1788 nd->last = this;
1789 nd->last_type = type;
1790 return 0;
1791 }
1792 terminate_walk(nd);
1793 return err;
1794}
1795
1796static int path_init(int dfd, const char *name, unsigned int flags,
1797 struct nameidata *nd, struct file **fp)
1798{
1799 int retval = 0;
1800 int fput_needed;
1801 struct file *file;
1802
1803 nd->last_type = LAST_ROOT;
1804 nd->flags = flags | LOOKUP_JUMPED;
1805 nd->depth = 0;
1806 if (flags & LOOKUP_ROOT) {
1807 struct inode *inode = nd->root.dentry->d_inode;
1808 if (*name) {
1809 if (!inode->i_op->lookup)
1810 return -ENOTDIR;
1811 retval = inode_permission(inode, MAY_EXEC);
1812 if (retval)
1813 return retval;
1814 }
1815 nd->path = nd->root;
1816 nd->inode = inode;
1817 if (flags & LOOKUP_RCU) {
1818 lock_rcu_walk();
1819 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1820 } else {
1821 path_get(&nd->path);
1822 }
1823 return 0;
1824 }
1825
1826 nd->root.mnt = NULL;
1827
1828 if (*name=='/') {
1829 if (flags & LOOKUP_RCU) {
1830 lock_rcu_walk();
1831 set_root_rcu(nd);
1832 } else {
1833 set_root(nd);
1834 path_get(&nd->root);
1835 }
1836 nd->path = nd->root;
1837 } else if (dfd == AT_FDCWD) {
1838 if (flags & LOOKUP_RCU) {
1839 struct fs_struct *fs = current->fs;
1840 unsigned seq;
1841
1842 lock_rcu_walk();
1843
1844 do {
1845 seq = read_seqcount_begin(&fs->seq);
1846 nd->path = fs->pwd;
1847 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1848 } while (read_seqcount_retry(&fs->seq, seq));
1849 } else {
1850 get_fs_pwd(current->fs, &nd->path);
1851 }
1852 } else {
1853 struct dentry *dentry;
1854
1855 file = fget_raw_light(dfd, &fput_needed);
1856 retval = -EBADF;
1857 if (!file)
1858 goto out_fail;
1859
1860 dentry = file->f_path.dentry;
1861
1862 if (*name) {
1863 retval = -ENOTDIR;
1864 if (!S_ISDIR(dentry->d_inode->i_mode))
1865 goto fput_fail;
1866
1867 retval = inode_permission(dentry->d_inode, MAY_EXEC);
1868 if (retval)
1869 goto fput_fail;
1870 }
1871
1872 nd->path = file->f_path;
1873 if (flags & LOOKUP_RCU) {
1874 if (fput_needed)
1875 *fp = file;
1876 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1877 lock_rcu_walk();
1878 } else {
1879 path_get(&file->f_path);
1880 fput_light(file, fput_needed);
1881 }
1882 }
1883
1884 nd->inode = nd->path.dentry->d_inode;
1885 return 0;
1886
1887fput_fail:
1888 fput_light(file, fput_needed);
1889out_fail:
1890 return retval;
1891}
1892
1893static inline int lookup_last(struct nameidata *nd, struct path *path)
1894{
1895 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1896 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1897
1898 nd->flags &= ~LOOKUP_PARENT;
1899 return walk_component(nd, path, &nd->last, nd->last_type,
1900 nd->flags & LOOKUP_FOLLOW);
1901}
1902
1903
1904static int path_lookupat(int dfd, const char *name,
1905 unsigned int flags, struct nameidata *nd)
1906{
1907 struct file *base = NULL;
1908 struct path path;
1909 int err;
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1926
1927 if (unlikely(err))
1928 return err;
1929
1930 current->total_link_count = 0;
1931 err = link_path_walk(name, nd);
1932
1933 if (!err && !(flags & LOOKUP_PARENT)) {
1934 err = lookup_last(nd, &path);
1935 while (err > 0) {
1936 void *cookie;
1937 struct path link = path;
1938 err = may_follow_link(&link, nd);
1939 if (unlikely(err))
1940 break;
1941 nd->flags |= LOOKUP_PARENT;
1942 err = follow_link(&link, nd, &cookie);
1943 if (err)
1944 break;
1945 err = lookup_last(nd, &path);
1946 put_link(nd, &link, cookie);
1947 }
1948 }
1949
1950 if (!err)
1951 err = complete_walk(nd);
1952
1953 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1954 if (!nd->inode->i_op->lookup) {
1955 path_put(&nd->path);
1956 err = -ENOTDIR;
1957 }
1958 }
1959
1960 if (base)
1961 fput(base);
1962
1963 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1964 path_put(&nd->root);
1965 nd->root.mnt = NULL;
1966 }
1967 return err;
1968}
1969
1970static int do_path_lookup(int dfd, const char *name,
1971 unsigned int flags, struct nameidata *nd)
1972{
1973 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1974 if (unlikely(retval == -ECHILD))
1975 retval = path_lookupat(dfd, name, flags, nd);
1976 if (unlikely(retval == -ESTALE))
1977 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1978
1979 if (likely(!retval)) {
1980 if (unlikely(!audit_dummy_context())) {
1981 if (nd->path.dentry && nd->inode)
1982 audit_inode(name, nd->path.dentry);
1983 }
1984 }
1985 return retval;
1986}
1987
1988
1989struct dentry *kern_path_locked(const char *name, struct path *path)
1990{
1991 struct nameidata nd;
1992 struct dentry *d;
1993 int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
1994 if (err)
1995 return ERR_PTR(err);
1996 if (nd.last_type != LAST_NORM) {
1997 path_put(&nd.path);
1998 return ERR_PTR(-EINVAL);
1999 }
2000 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2001 d = __lookup_hash(&nd.last, nd.path.dentry, 0);
2002 if (IS_ERR(d)) {
2003 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2004 path_put(&nd.path);
2005 return d;
2006 }
2007 *path = nd.path;
2008 return d;
2009}
2010
2011int kern_path(const char *name, unsigned int flags, struct path *path)
2012{
2013 struct nameidata nd;
2014 int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2015 if (!res)
2016 *path = nd.path;
2017 return res;
2018}
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2029 const char *name, unsigned int flags,
2030 struct path *path)
2031{
2032 struct nameidata nd;
2033 int err;
2034 nd.root.dentry = dentry;
2035 nd.root.mnt = mnt;
2036 BUG_ON(flags & LOOKUP_PARENT);
2037
2038 err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2039 if (!err)
2040 *path = nd.path;
2041 return err;
2042}
2043
2044
2045
2046
2047
2048
2049static struct dentry *lookup_hash(struct nameidata *nd)
2050{
2051 return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
2052}
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2066{
2067 struct qstr this;
2068 unsigned int c;
2069 int err;
2070
2071 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
2072
2073 this.name = name;
2074 this.len = len;
2075 this.hash = full_name_hash(name, len);
2076 if (!len)
2077 return ERR_PTR(-EACCES);
2078
2079 while (len--) {
2080 c = *(const unsigned char *)name++;
2081 if (c == '/' || c == '\0')
2082 return ERR_PTR(-EACCES);
2083 }
2084
2085
2086
2087
2088 if (base->d_flags & DCACHE_OP_HASH) {
2089 int err = base->d_op->d_hash(base, base->d_inode, &this);
2090 if (err < 0)
2091 return ERR_PTR(err);
2092 }
2093
2094 err = inode_permission(base->d_inode, MAY_EXEC);
2095 if (err)
2096 return ERR_PTR(err);
2097
2098 return __lookup_hash(&this, base, 0);
2099}
2100
2101int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2102 struct path *path, int *empty)
2103{
2104 struct nameidata nd;
2105 char *tmp = getname_flags(name, flags, empty);
2106 int err = PTR_ERR(tmp);
2107 if (!IS_ERR(tmp)) {
2108
2109 BUG_ON(flags & LOOKUP_PARENT);
2110
2111 err = do_path_lookup(dfd, tmp, flags, &nd);
2112 putname(tmp);
2113 if (!err)
2114 *path = nd.path;
2115 }
2116 return err;
2117}
2118
2119int user_path_at(int dfd, const char __user *name, unsigned flags,
2120 struct path *path)
2121{
2122 return user_path_at_empty(dfd, name, flags, path, NULL);
2123}
2124
2125static int user_path_parent(int dfd, const char __user *path,
2126 struct nameidata *nd, char **name)
2127{
2128 char *s = getname(path);
2129 int error;
2130
2131 if (IS_ERR(s))
2132 return PTR_ERR(s);
2133
2134 error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
2135 if (error)
2136 putname(s);
2137 else
2138 *name = s;
2139
2140 return error;
2141}
2142
2143
2144
2145
2146
2147static inline int check_sticky(struct inode *dir, struct inode *inode)
2148{
2149 kuid_t fsuid = current_fsuid();
2150
2151 if (!(dir->i_mode & S_ISVTX))
2152 return 0;
2153 if (uid_eq(inode->i_uid, fsuid))
2154 return 0;
2155 if (uid_eq(dir->i_uid, fsuid))
2156 return 0;
2157 return !inode_capable(inode, CAP_FOWNER);
2158}
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
2180{
2181 int error;
2182
2183 if (!victim->d_inode)
2184 return -ENOENT;
2185
2186 BUG_ON(victim->d_parent->d_inode != dir);
2187 audit_inode_child(victim, dir);
2188
2189 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
2190 if (error)
2191 return error;
2192 if (IS_APPEND(dir))
2193 return -EPERM;
2194 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
2195 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
2196 return -EPERM;
2197 if (isdir) {
2198 if (!S_ISDIR(victim->d_inode->i_mode))
2199 return -ENOTDIR;
2200 if (IS_ROOT(victim))
2201 return -EBUSY;
2202 } else if (S_ISDIR(victim->d_inode->i_mode))
2203 return -EISDIR;
2204 if (IS_DEADDIR(dir))
2205 return -ENOENT;
2206 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2207 return -EBUSY;
2208 return 0;
2209}
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219static inline int may_create(struct inode *dir, struct dentry *child)
2220{
2221 if (child->d_inode)
2222 return -EEXIST;
2223 if (IS_DEADDIR(dir))
2224 return -ENOENT;
2225 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
2226}
2227
2228
2229
2230
2231struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2232{
2233 struct dentry *p;
2234
2235 if (p1 == p2) {
2236 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2237 return NULL;
2238 }
2239
2240 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2241
2242 p = d_ancestor(p2, p1);
2243 if (p) {
2244 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2245 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
2246 return p;
2247 }
2248
2249 p = d_ancestor(p1, p2);
2250 if (p) {
2251 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2252 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2253 return p;
2254 }
2255
2256 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2257 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
2258 return NULL;
2259}
2260
2261void unlock_rename(struct dentry *p1, struct dentry *p2)
2262{
2263 mutex_unlock(&p1->d_inode->i_mutex);
2264 if (p1 != p2) {
2265 mutex_unlock(&p2->d_inode->i_mutex);
2266 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
2267 }
2268}
2269
2270int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2271 bool want_excl)
2272{
2273 int error = may_create(dir, dentry);
2274 if (error)
2275 return error;
2276
2277 if (!dir->i_op->create)
2278 return -EACCES;
2279 mode &= S_IALLUGO;
2280 mode |= S_IFREG;
2281 error = security_inode_create(dir, dentry, mode);
2282 if (error)
2283 return error;
2284 error = dir->i_op->create(dir, dentry, mode, want_excl);
2285 if (!error)
2286 fsnotify_create(dir, dentry);
2287 return error;
2288}
2289
2290static int may_open(struct path *path, int acc_mode, int flag)
2291{
2292 struct dentry *dentry = path->dentry;
2293 struct inode *inode = dentry->d_inode;
2294 int error;
2295
2296
2297 if (!acc_mode)
2298 return 0;
2299
2300 if (!inode)
2301 return -ENOENT;
2302
2303 switch (inode->i_mode & S_IFMT) {
2304 case S_IFLNK:
2305 return -ELOOP;
2306 case S_IFDIR:
2307 if (acc_mode & MAY_WRITE)
2308 return -EISDIR;
2309 break;
2310 case S_IFBLK:
2311 case S_IFCHR:
2312 if (path->mnt->mnt_flags & MNT_NODEV)
2313 return -EACCES;
2314
2315 case S_IFIFO:
2316 case S_IFSOCK:
2317 flag &= ~O_TRUNC;
2318 break;
2319 }
2320
2321 error = inode_permission(inode, acc_mode);
2322 if (error)
2323 return error;
2324
2325
2326
2327
2328 if (IS_APPEND(inode)) {
2329 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2330 return -EPERM;
2331 if (flag & O_TRUNC)
2332 return -EPERM;
2333 }
2334
2335
2336 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2337 return -EPERM;
2338
2339 return 0;
2340}
2341
2342static int handle_truncate(struct file *filp)
2343{
2344 struct path *path = &filp->f_path;
2345 struct inode *inode = path->dentry->d_inode;
2346 int error = get_write_access(inode);
2347 if (error)
2348 return error;
2349
2350
2351
2352 error = locks_verify_locked(inode);
2353 if (!error)
2354 error = security_path_truncate(path);
2355 if (!error) {
2356 error = do_truncate(path->dentry, 0,
2357 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2358 filp);
2359 }
2360 put_write_access(inode);
2361 return error;
2362}
2363
2364static inline int open_to_namei_flags(int flag)
2365{
2366 if ((flag & O_ACCMODE) == 3)
2367 flag--;
2368 return flag;
2369}
2370
2371static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2372{
2373 int error = security_path_mknod(dir, dentry, mode, 0);
2374 if (error)
2375 return error;
2376
2377 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2378 if (error)
2379 return error;
2380
2381 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2382}
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2398 struct path *path, struct file *file,
2399 const struct open_flags *op,
2400 bool got_write, bool need_lookup,
2401 int *opened)
2402{
2403 struct inode *dir = nd->path.dentry->d_inode;
2404 unsigned open_flag = open_to_namei_flags(op->open_flag);
2405 umode_t mode;
2406 int error;
2407 int acc_mode;
2408 int create_error = 0;
2409 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2410
2411 BUG_ON(dentry->d_inode);
2412
2413
2414 if (unlikely(IS_DEADDIR(dir))) {
2415 error = -ENOENT;
2416 goto out;
2417 }
2418
2419 mode = op->mode;
2420 if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2421 mode &= ~current_umask();
2422
2423 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) {
2424 open_flag &= ~O_TRUNC;
2425 *opened |= FILE_CREATED;
2426 }
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437 if (((open_flag & (O_CREAT | O_TRUNC)) ||
2438 (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
2439 if (!(open_flag & O_CREAT)) {
2440
2441
2442
2443
2444 goto no_open;
2445 } else if (open_flag & (O_EXCL | O_TRUNC)) {
2446
2447 create_error = -EROFS;
2448 goto no_open;
2449 } else {
2450
2451 create_error = -EROFS;
2452 open_flag &= ~O_CREAT;
2453 }
2454 }
2455
2456 if (open_flag & O_CREAT) {
2457 error = may_o_create(&nd->path, dentry, mode);
2458 if (error) {
2459 create_error = error;
2460 if (open_flag & O_EXCL)
2461 goto no_open;
2462 open_flag &= ~O_CREAT;
2463 }
2464 }
2465
2466 if (nd->flags & LOOKUP_DIRECTORY)
2467 open_flag |= O_DIRECTORY;
2468
2469 file->f_path.dentry = DENTRY_NOT_SET;
2470 file->f_path.mnt = nd->path.mnt;
2471 error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
2472 opened);
2473 if (error < 0) {
2474 if (create_error && error == -ENOENT)
2475 error = create_error;
2476 goto out;
2477 }
2478
2479 acc_mode = op->acc_mode;
2480 if (*opened & FILE_CREATED) {
2481 fsnotify_create(dir, dentry);
2482 acc_mode = MAY_OPEN;
2483 }
2484
2485 if (error) {
2486 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
2487 error = -EIO;
2488 goto out;
2489 }
2490 if (file->f_path.dentry) {
2491 dput(dentry);
2492 dentry = file->f_path.dentry;
2493 }
2494 if (create_error && dentry->d_inode == NULL) {
2495 error = create_error;
2496 goto out;
2497 }
2498 goto looked_up;
2499 }
2500
2501
2502
2503
2504
2505 error = may_open(&file->f_path, acc_mode, open_flag);
2506 if (error)
2507 fput(file);
2508
2509out:
2510 dput(dentry);
2511 return error;
2512
2513no_open:
2514 if (need_lookup) {
2515 dentry = lookup_real(dir, dentry, nd->flags);
2516 if (IS_ERR(dentry))
2517 return PTR_ERR(dentry);
2518
2519 if (create_error) {
2520 int open_flag = op->open_flag;
2521
2522 error = create_error;
2523 if ((open_flag & O_EXCL)) {
2524 if (!dentry->d_inode)
2525 goto out;
2526 } else if (!dentry->d_inode) {
2527 goto out;
2528 } else if ((open_flag & O_TRUNC) &&
2529 S_ISREG(dentry->d_inode->i_mode)) {
2530 goto out;
2531 }
2532
2533 }
2534 }
2535looked_up:
2536 path->dentry = dentry;
2537 path->mnt = nd->path.mnt;
2538 return 1;
2539}
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559static int lookup_open(struct nameidata *nd, struct path *path,
2560 struct file *file,
2561 const struct open_flags *op,
2562 bool got_write, int *opened)
2563{
2564 struct dentry *dir = nd->path.dentry;
2565 struct inode *dir_inode = dir->d_inode;
2566 struct dentry *dentry;
2567 int error;
2568 bool need_lookup;
2569
2570 *opened &= ~FILE_CREATED;
2571 dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2572 if (IS_ERR(dentry))
2573 return PTR_ERR(dentry);
2574
2575
2576 if (!need_lookup && dentry->d_inode)
2577 goto out_no_open;
2578
2579 if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
2580 return atomic_open(nd, dentry, path, file, op, got_write,
2581 need_lookup, opened);
2582 }
2583
2584 if (need_lookup) {
2585 BUG_ON(dentry->d_inode);
2586
2587 dentry = lookup_real(dir_inode, dentry, nd->flags);
2588 if (IS_ERR(dentry))
2589 return PTR_ERR(dentry);
2590 }
2591
2592
2593 if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2594 umode_t mode = op->mode;
2595 if (!IS_POSIXACL(dir->d_inode))
2596 mode &= ~current_umask();
2597
2598
2599
2600
2601
2602
2603
2604 if (!got_write) {
2605 error = -EROFS;
2606 goto out_dput;
2607 }
2608 *opened |= FILE_CREATED;
2609 error = security_path_mknod(&nd->path, dentry, mode, 0);
2610 if (error)
2611 goto out_dput;
2612 error = vfs_create(dir->d_inode, dentry, mode,
2613 nd->flags & LOOKUP_EXCL);
2614 if (error)
2615 goto out_dput;
2616 }
2617out_no_open:
2618 path->dentry = dentry;
2619 path->mnt = nd->path.mnt;
2620 return 1;
2621
2622out_dput:
2623 dput(dentry);
2624 return error;
2625}
2626
2627
2628
2629
2630static int do_last(struct nameidata *nd, struct path *path,
2631 struct file *file, const struct open_flags *op,
2632 int *opened, const char *pathname)
2633{
2634 struct dentry *dir = nd->path.dentry;
2635 int open_flag = op->open_flag;
2636 bool will_truncate = (open_flag & O_TRUNC) != 0;
2637 bool got_write = false;
2638 int acc_mode = op->acc_mode;
2639 struct inode *inode;
2640 bool symlink_ok = false;
2641 struct path save_parent = { .dentry = NULL, .mnt = NULL };
2642 bool retried = false;
2643 int error;
2644
2645 nd->flags &= ~LOOKUP_PARENT;
2646 nd->flags |= op->intent;
2647
2648 switch (nd->last_type) {
2649 case LAST_DOTDOT:
2650 case LAST_DOT:
2651 error = handle_dots(nd, nd->last_type);
2652 if (error)
2653 return error;
2654
2655 case LAST_ROOT:
2656 error = complete_walk(nd);
2657 if (error)
2658 return error;
2659 audit_inode(pathname, nd->path.dentry);
2660 if (open_flag & O_CREAT) {
2661 error = -EISDIR;
2662 goto out;
2663 }
2664 goto finish_open;
2665 case LAST_BIND:
2666 error = complete_walk(nd);
2667 if (error)
2668 return error;
2669 audit_inode(pathname, dir);
2670 goto finish_open;
2671 }
2672
2673 if (!(open_flag & O_CREAT)) {
2674 if (nd->last.name[nd->last.len])
2675 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2676 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2677 symlink_ok = true;
2678
2679 error = lookup_fast(nd, &nd->last, path, &inode);
2680 if (likely(!error))
2681 goto finish_lookup;
2682
2683 if (error < 0)
2684 goto out;
2685
2686 BUG_ON(nd->inode != dir->d_inode);
2687 } else {
2688
2689
2690
2691
2692
2693
2694 error = complete_walk(nd);
2695 if (error)
2696 return error;
2697
2698 audit_inode(pathname, dir);
2699 error = -EISDIR;
2700
2701 if (nd->last.name[nd->last.len])
2702 goto out;
2703 }
2704
2705retry_lookup:
2706 if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
2707 error = mnt_want_write(nd->path.mnt);
2708 if (!error)
2709 got_write = true;
2710
2711
2712
2713
2714
2715 }
2716 mutex_lock(&dir->d_inode->i_mutex);
2717 error = lookup_open(nd, path, file, op, got_write, opened);
2718 mutex_unlock(&dir->d_inode->i_mutex);
2719
2720 if (error <= 0) {
2721 if (error)
2722 goto out;
2723
2724 if ((*opened & FILE_CREATED) ||
2725 !S_ISREG(file->f_path.dentry->d_inode->i_mode))
2726 will_truncate = false;
2727
2728 audit_inode(pathname, file->f_path.dentry);
2729 goto opened;
2730 }
2731
2732 if (*opened & FILE_CREATED) {
2733
2734 open_flag &= ~O_TRUNC;
2735 will_truncate = false;
2736 acc_mode = MAY_OPEN;
2737 path_to_nameidata(path, nd);
2738 goto finish_open_created;
2739 }
2740
2741
2742
2743
2744 if (path->dentry->d_inode)
2745 audit_inode(pathname, path->dentry);
2746
2747
2748
2749
2750
2751
2752 if (got_write) {
2753 mnt_drop_write(nd->path.mnt);
2754 got_write = false;
2755 }
2756
2757 error = -EEXIST;
2758 if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
2759 goto exit_dput;
2760
2761 error = follow_managed(path, nd->flags);
2762 if (error < 0)
2763 goto exit_dput;
2764
2765 if (error)
2766 nd->flags |= LOOKUP_JUMPED;
2767
2768 BUG_ON(nd->flags & LOOKUP_RCU);
2769 inode = path->dentry->d_inode;
2770finish_lookup:
2771
2772 error = -ENOENT;
2773 if (!inode) {
2774 path_to_nameidata(path, nd);
2775 goto out;
2776 }
2777
2778 if (should_follow_link(inode, !symlink_ok)) {
2779 if (nd->flags & LOOKUP_RCU) {
2780 if (unlikely(unlazy_walk(nd, path->dentry))) {
2781 error = -ECHILD;
2782 goto out;
2783 }
2784 }
2785 BUG_ON(inode != path->dentry->d_inode);
2786 return 1;
2787 }
2788
2789 if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2790 path_to_nameidata(path, nd);
2791 } else {
2792 save_parent.dentry = nd->path.dentry;
2793 save_parent.mnt = mntget(path->mnt);
2794 nd->path.dentry = path->dentry;
2795
2796 }
2797 nd->inode = inode;
2798
2799 error = complete_walk(nd);
2800 if (error) {
2801 path_put(&save_parent);
2802 return error;
2803 }
2804 error = -EISDIR;
2805 if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
2806 goto out;
2807 error = -ENOTDIR;
2808 if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup)
2809 goto out;
2810 audit_inode(pathname, nd->path.dentry);
2811finish_open:
2812 if (!S_ISREG(nd->inode->i_mode))
2813 will_truncate = false;
2814
2815 if (will_truncate) {
2816 error = mnt_want_write(nd->path.mnt);
2817 if (error)
2818 goto out;
2819 got_write = true;
2820 }
2821finish_open_created:
2822 error = may_open(&nd->path, acc_mode, open_flag);
2823 if (error)
2824 goto out;
2825 file->f_path.mnt = nd->path.mnt;
2826 error = finish_open(file, nd->path.dentry, NULL, opened);
2827 if (error) {
2828 if (error == -EOPENSTALE)
2829 goto stale_open;
2830 goto out;
2831 }
2832opened:
2833 error = open_check_o_direct(file);
2834 if (error)
2835 goto exit_fput;
2836 error = ima_file_check(file, op->acc_mode);
2837 if (error)
2838 goto exit_fput;
2839
2840 if (will_truncate) {
2841 error = handle_truncate(file);
2842 if (error)
2843 goto exit_fput;
2844 }
2845out:
2846 if (got_write)
2847 mnt_drop_write(nd->path.mnt);
2848 path_put(&save_parent);
2849 terminate_walk(nd);
2850 return error;
2851
2852exit_dput:
2853 path_put_conditional(path, nd);
2854 goto out;
2855exit_fput:
2856 fput(file);
2857 goto out;
2858
2859stale_open:
2860
2861 if (!save_parent.dentry || retried)
2862 goto out;
2863
2864 BUG_ON(save_parent.dentry != dir);
2865 path_put(&nd->path);
2866 nd->path = save_parent;
2867 nd->inode = dir->d_inode;
2868 save_parent.mnt = NULL;
2869 save_parent.dentry = NULL;
2870 if (got_write) {
2871 mnt_drop_write(nd->path.mnt);
2872 got_write = false;
2873 }
2874 retried = true;
2875 goto retry_lookup;
2876}
2877
2878static struct file *path_openat(int dfd, const char *pathname,
2879 struct nameidata *nd, const struct open_flags *op, int flags)
2880{
2881 struct file *base = NULL;
2882 struct file *file;
2883 struct path path;
2884 int opened = 0;
2885 int error;
2886
2887 file = get_empty_filp();
2888 if (!file)
2889 return ERR_PTR(-ENFILE);
2890
2891 file->f_flags = op->open_flag;
2892
2893 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
2894 if (unlikely(error))
2895 goto out;
2896
2897 current->total_link_count = 0;
2898 error = link_path_walk(pathname, nd);
2899 if (unlikely(error))
2900 goto out;
2901
2902 error = do_last(nd, &path, file, op, &opened, pathname);
2903 while (unlikely(error > 0)) {
2904 struct path link = path;
2905 void *cookie;
2906 if (!(nd->flags & LOOKUP_FOLLOW)) {
2907 path_put_conditional(&path, nd);
2908 path_put(&nd->path);
2909 error = -ELOOP;
2910 break;
2911 }
2912 error = may_follow_link(&link, nd);
2913 if (unlikely(error))
2914 break;
2915 nd->flags |= LOOKUP_PARENT;
2916 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2917 error = follow_link(&link, nd, &cookie);
2918 if (unlikely(error))
2919 break;
2920 error = do_last(nd, &path, file, op, &opened, pathname);
2921 put_link(nd, &link, cookie);
2922 }
2923out:
2924 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2925 path_put(&nd->root);
2926 if (base)
2927 fput(base);
2928 if (!(opened & FILE_OPENED)) {
2929 BUG_ON(!error);
2930 put_filp(file);
2931 }
2932 if (unlikely(error)) {
2933 if (error == -EOPENSTALE) {
2934 if (flags & LOOKUP_RCU)
2935 error = -ECHILD;
2936 else
2937 error = -ESTALE;
2938 }
2939 file = ERR_PTR(error);
2940 }
2941 return file;
2942}
2943
2944struct file *do_filp_open(int dfd, const char *pathname,
2945 const struct open_flags *op, int flags)
2946{
2947 struct nameidata nd;
2948 struct file *filp;
2949
2950 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
2951 if (unlikely(filp == ERR_PTR(-ECHILD)))
2952 filp = path_openat(dfd, pathname, &nd, op, flags);
2953 if (unlikely(filp == ERR_PTR(-ESTALE)))
2954 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
2955 return filp;
2956}
2957
2958struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2959 const char *name, const struct open_flags *op, int flags)
2960{
2961 struct nameidata nd;
2962 struct file *file;
2963
2964 nd.root.mnt = mnt;
2965 nd.root.dentry = dentry;
2966
2967 flags |= LOOKUP_ROOT;
2968
2969 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
2970 return ERR_PTR(-ELOOP);
2971
2972 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2973 if (unlikely(file == ERR_PTR(-ECHILD)))
2974 file = path_openat(-1, name, &nd, op, flags);
2975 if (unlikely(file == ERR_PTR(-ESTALE)))
2976 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2977 return file;
2978}
2979
2980struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
2981{
2982 struct dentry *dentry = ERR_PTR(-EEXIST);
2983 struct nameidata nd;
2984 int err2;
2985 int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2986 if (error)
2987 return ERR_PTR(error);
2988
2989
2990
2991
2992
2993 if (nd.last_type != LAST_NORM)
2994 goto out;
2995 nd.flags &= ~LOOKUP_PARENT;
2996 nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2997
2998
2999 err2 = mnt_want_write(nd.path.mnt);
3000
3001
3002
3003 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3004 dentry = lookup_hash(&nd);
3005 if (IS_ERR(dentry))
3006 goto unlock;
3007
3008 error = -EEXIST;
3009 if (dentry->d_inode)
3010 goto fail;
3011
3012
3013
3014
3015
3016
3017 if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
3018 error = -ENOENT;
3019 goto fail;
3020 }
3021 if (unlikely(err2)) {
3022 error = err2;
3023 goto fail;
3024 }
3025 *path = nd.path;
3026 return dentry;
3027fail:
3028 dput(dentry);
3029 dentry = ERR_PTR(error);
3030unlock:
3031 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3032 if (!err2)
3033 mnt_drop_write(nd.path.mnt);
3034out:
3035 path_put(&nd.path);
3036 return dentry;
3037}
3038EXPORT_SYMBOL(kern_path_create);
3039
3040void done_path_create(struct path *path, struct dentry *dentry)
3041{
3042 dput(dentry);
3043 mutex_unlock(&path->dentry->d_inode->i_mutex);
3044 mnt_drop_write(path->mnt);
3045 path_put(path);
3046}
3047EXPORT_SYMBOL(done_path_create);
3048
3049struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
3050{
3051 char *tmp = getname(pathname);
3052 struct dentry *res;
3053 if (IS_ERR(tmp))
3054 return ERR_CAST(tmp);
3055 res = kern_path_create(dfd, tmp, path, is_dir);
3056 putname(tmp);
3057 return res;
3058}
3059EXPORT_SYMBOL(user_path_create);
3060
3061int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3062{
3063 int error = may_create(dir, dentry);
3064
3065 if (error)
3066 return error;
3067
3068 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
3069 return -EPERM;
3070
3071 if (!dir->i_op->mknod)
3072 return -EPERM;
3073
3074 error = devcgroup_inode_mknod(mode, dev);
3075 if (error)
3076 return error;
3077
3078 error = security_inode_mknod(dir, dentry, mode, dev);
3079 if (error)
3080 return error;
3081
3082 error = dir->i_op->mknod(dir, dentry, mode, dev);
3083 if (!error)
3084 fsnotify_create(dir, dentry);
3085 return error;
3086}
3087
3088static int may_mknod(umode_t mode)
3089{
3090 switch (mode & S_IFMT) {
3091 case S_IFREG:
3092 case S_IFCHR:
3093 case S_IFBLK:
3094 case S_IFIFO:
3095 case S_IFSOCK:
3096 case 0:
3097 return 0;
3098 case S_IFDIR:
3099 return -EPERM;
3100 default:
3101 return -EINVAL;
3102 }
3103}
3104
3105SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3106 unsigned, dev)
3107{
3108 struct dentry *dentry;
3109 struct path path;
3110 int error;
3111
3112 error = may_mknod(mode);
3113 if (error)
3114 return error;
3115
3116 dentry = user_path_create(dfd, filename, &path, 0);
3117 if (IS_ERR(dentry))
3118 return PTR_ERR(dentry);
3119
3120 if (!IS_POSIXACL(path.dentry->d_inode))
3121 mode &= ~current_umask();
3122 error = security_path_mknod(&path, dentry, mode, dev);
3123 if (error)
3124 goto out;
3125 switch (mode & S_IFMT) {
3126 case 0: case S_IFREG:
3127 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
3128 break;
3129 case S_IFCHR: case S_IFBLK:
3130 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
3131 new_decode_dev(dev));
3132 break;
3133 case S_IFIFO: case S_IFSOCK:
3134 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
3135 break;
3136 }
3137out:
3138 done_path_create(&path, dentry);
3139 return error;
3140}
3141
3142SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
3143{
3144 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3145}
3146
3147int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3148{
3149 int error = may_create(dir, dentry);
3150 unsigned max_links = dir->i_sb->s_max_links;
3151
3152 if (error)
3153 return error;
3154
3155 if (!dir->i_op->mkdir)
3156 return -EPERM;
3157
3158 mode &= (S_IRWXUGO|S_ISVTX);
3159 error = security_inode_mkdir(dir, dentry, mode);
3160 if (error)
3161 return error;
3162
3163 if (max_links && dir->i_nlink >= max_links)
3164 return -EMLINK;
3165
3166 error = dir->i_op->mkdir(dir, dentry, mode);
3167 if (!error)
3168 fsnotify_mkdir(dir, dentry);
3169 return error;
3170}
3171
3172SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3173{
3174 struct dentry *dentry;
3175 struct path path;
3176 int error;
3177
3178 dentry = user_path_create(dfd, pathname, &path, 1);
3179 if (IS_ERR(dentry))
3180 return PTR_ERR(dentry);
3181
3182 if (!IS_POSIXACL(path.dentry->d_inode))
3183 mode &= ~current_umask();
3184 error = security_path_mkdir(&path, dentry, mode);
3185 if (!error)
3186 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3187 done_path_create(&path, dentry);
3188 return error;
3189}
3190
3191SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
3192{
3193 return sys_mkdirat(AT_FDCWD, pathname, mode);
3194}
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211void dentry_unhash(struct dentry *dentry)
3212{
3213 shrink_dcache_parent(dentry);
3214 spin_lock(&dentry->d_lock);
3215 if (dentry->d_count == 1)
3216 __d_drop(dentry);
3217 spin_unlock(&dentry->d_lock);
3218}
3219
3220int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3221{
3222 int error = may_delete(dir, dentry, 1);
3223
3224 if (error)
3225 return error;
3226
3227 if (!dir->i_op->rmdir)
3228 return -EPERM;
3229
3230 dget(dentry);
3231 mutex_lock(&dentry->d_inode->i_mutex);
3232
3233 error = -EBUSY;
3234 if (d_mountpoint(dentry))
3235 goto out;
3236
3237 error = security_inode_rmdir(dir, dentry);
3238 if (error)
3239 goto out;
3240
3241 shrink_dcache_parent(dentry);
3242 error = dir->i_op->rmdir(dir, dentry);
3243 if (error)
3244 goto out;
3245
3246 dentry->d_inode->i_flags |= S_DEAD;
3247 dont_mount(dentry);
3248
3249out:
3250 mutex_unlock(&dentry->d_inode->i_mutex);
3251 dput(dentry);
3252 if (!error)
3253 d_delete(dentry);
3254 return error;
3255}
3256
3257static long do_rmdir(int dfd, const char __user *pathname)
3258{
3259 int error = 0;
3260 char * name;
3261 struct dentry *dentry;
3262 struct nameidata nd;
3263
3264 error = user_path_parent(dfd, pathname, &nd, &name);
3265 if (error)
3266 return error;
3267
3268 switch(nd.last_type) {
3269 case LAST_DOTDOT:
3270 error = -ENOTEMPTY;
3271 goto exit1;
3272 case LAST_DOT:
3273 error = -EINVAL;
3274 goto exit1;
3275 case LAST_ROOT:
3276 error = -EBUSY;
3277 goto exit1;
3278 }
3279
3280 nd.flags &= ~LOOKUP_PARENT;
3281 error = mnt_want_write(nd.path.mnt);
3282 if (error)
3283 goto exit1;
3284
3285 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3286 dentry = lookup_hash(&nd);
3287 error = PTR_ERR(dentry);
3288 if (IS_ERR(dentry))
3289 goto exit2;
3290 if (!dentry->d_inode) {
3291 error = -ENOENT;
3292 goto exit3;
3293 }
3294 error = security_path_rmdir(&nd.path, dentry);
3295 if (error)
3296 goto exit3;
3297 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
3298exit3:
3299 dput(dentry);
3300exit2:
3301 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3302 mnt_drop_write(nd.path.mnt);
3303exit1:
3304 path_put(&nd.path);
3305 putname(name);
3306 return error;
3307}
3308
3309SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
3310{
3311 return do_rmdir(AT_FDCWD, pathname);
3312}
3313
3314int vfs_unlink(struct inode *dir, struct dentry *dentry)
3315{
3316 int error = may_delete(dir, dentry, 0);
3317
3318 if (error)
3319 return error;
3320
3321 if (!dir->i_op->unlink)
3322 return -EPERM;
3323
3324 mutex_lock(&dentry->d_inode->i_mutex);
3325 if (d_mountpoint(dentry))
3326 error = -EBUSY;
3327 else {
3328 error = security_inode_unlink(dir, dentry);
3329 if (!error) {
3330 error = dir->i_op->unlink(dir, dentry);
3331 if (!error)
3332 dont_mount(dentry);
3333 }
3334 }
3335 mutex_unlock(&dentry->d_inode->i_mutex);
3336
3337
3338 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3339 fsnotify_link_count(dentry->d_inode);
3340 d_delete(dentry);
3341 }
3342
3343 return error;
3344}
3345
3346
3347
3348
3349
3350
3351
3352static long do_unlinkat(int dfd, const char __user *pathname)
3353{
3354 int error;
3355 char *name;
3356 struct dentry *dentry;
3357 struct nameidata nd;
3358 struct inode *inode = NULL;
3359
3360 error = user_path_parent(dfd, pathname, &nd, &name);
3361 if (error)
3362 return error;
3363
3364 error = -EISDIR;
3365 if (nd.last_type != LAST_NORM)
3366 goto exit1;
3367
3368 nd.flags &= ~LOOKUP_PARENT;
3369 error = mnt_want_write(nd.path.mnt);
3370 if (error)
3371 goto exit1;
3372
3373 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3374 dentry = lookup_hash(&nd);
3375 error = PTR_ERR(dentry);
3376 if (!IS_ERR(dentry)) {
3377
3378 if (nd.last.name[nd.last.len])
3379 goto slashes;
3380 inode = dentry->d_inode;
3381 if (!inode)
3382 goto slashes;
3383 ihold(inode);
3384 error = security_path_unlink(&nd.path, dentry);
3385 if (error)
3386 goto exit2;
3387 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
3388exit2:
3389 dput(dentry);
3390 }
3391 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3392 if (inode)
3393 iput(inode);
3394 mnt_drop_write(nd.path.mnt);
3395exit1:
3396 path_put(&nd.path);
3397 putname(name);
3398 return error;
3399
3400slashes:
3401 error = !dentry->d_inode ? -ENOENT :
3402 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
3403 goto exit2;
3404}
3405
3406SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
3407{
3408 if ((flag & ~AT_REMOVEDIR) != 0)
3409 return -EINVAL;
3410
3411 if (flag & AT_REMOVEDIR)
3412 return do_rmdir(dfd, pathname);
3413
3414 return do_unlinkat(dfd, pathname);
3415}
3416
3417SYSCALL_DEFINE1(unlink, const char __user *, pathname)
3418{
3419 return do_unlinkat(AT_FDCWD, pathname);
3420}
3421
3422int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
3423{
3424 int error = may_create(dir, dentry);
3425
3426 if (error)
3427 return error;
3428
3429 if (!dir->i_op->symlink)
3430 return -EPERM;
3431
3432 error = security_inode_symlink(dir, dentry, oldname);
3433 if (error)
3434 return error;
3435
3436 error = dir->i_op->symlink(dir, dentry, oldname);
3437 if (!error)
3438 fsnotify_create(dir, dentry);
3439 return error;
3440}
3441
3442SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
3443 int, newdfd, const char __user *, newname)
3444{
3445 int error;
3446 char *from;
3447 struct dentry *dentry;
3448 struct path path;
3449
3450 from = getname(oldname);
3451 if (IS_ERR(from))
3452 return PTR_ERR(from);
3453
3454 dentry = user_path_create(newdfd, newname, &path, 0);
3455 error = PTR_ERR(dentry);
3456 if (IS_ERR(dentry))
3457 goto out_putname;
3458
3459 error = security_path_symlink(&path, dentry, from);
3460 if (!error)
3461 error = vfs_symlink(path.dentry->d_inode, dentry, from);
3462 done_path_create(&path, dentry);
3463out_putname:
3464 putname(from);
3465 return error;
3466}
3467
3468SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
3469{
3470 return sys_symlinkat(oldname, AT_FDCWD, newname);
3471}
3472
3473int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3474{
3475 struct inode *inode = old_dentry->d_inode;
3476 unsigned max_links = dir->i_sb->s_max_links;
3477 int error;
3478
3479 if (!inode)
3480 return -ENOENT;
3481
3482 error = may_create(dir, new_dentry);
3483 if (error)
3484 return error;
3485
3486 if (dir->i_sb != inode->i_sb)
3487 return -EXDEV;
3488
3489
3490
3491
3492 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3493 return -EPERM;
3494 if (!dir->i_op->link)
3495 return -EPERM;
3496 if (S_ISDIR(inode->i_mode))
3497 return -EPERM;
3498
3499 error = security_inode_link(old_dentry, dir, new_dentry);
3500 if (error)
3501 return error;
3502
3503 mutex_lock(&inode->i_mutex);
3504
3505 if (inode->i_nlink == 0)
3506 error = -ENOENT;
3507 else if (max_links && inode->i_nlink >= max_links)
3508 error = -EMLINK;
3509 else
3510 error = dir->i_op->link(old_dentry, dir, new_dentry);
3511 mutex_unlock(&inode->i_mutex);
3512 if (!error)
3513 fsnotify_link(dir, inode, new_dentry);
3514 return error;
3515}
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3527 int, newdfd, const char __user *, newname, int, flags)
3528{
3529 struct dentry *new_dentry;
3530 struct path old_path, new_path;
3531 int how = 0;
3532 int error;
3533
3534 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3535 return -EINVAL;
3536
3537
3538
3539
3540
3541 if (flags & AT_EMPTY_PATH) {
3542 if (!capable(CAP_DAC_READ_SEARCH))
3543 return -ENOENT;
3544 how = LOOKUP_EMPTY;
3545 }
3546
3547 if (flags & AT_SYMLINK_FOLLOW)
3548 how |= LOOKUP_FOLLOW;
3549
3550 error = user_path_at(olddfd, oldname, how, &old_path);
3551 if (error)
3552 return error;
3553
3554 new_dentry = user_path_create(newdfd, newname, &new_path, 0);
3555 error = PTR_ERR(new_dentry);
3556 if (IS_ERR(new_dentry))
3557 goto out;
3558
3559 error = -EXDEV;
3560 if (old_path.mnt != new_path.mnt)
3561 goto out_dput;
3562 error = may_linkat(&old_path);
3563 if (unlikely(error))
3564 goto out_dput;
3565 error = security_path_link(old_path.dentry, &new_path, new_dentry);
3566 if (error)
3567 goto out_dput;
3568 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
3569out_dput:
3570 done_path_create(&new_path, new_dentry);
3571out:
3572 path_put(&old_path);
3573
3574 return error;
3575}
3576
3577SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
3578{
3579 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
3580}
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
3610 struct inode *new_dir, struct dentry *new_dentry)
3611{
3612 int error = 0;
3613 struct inode *target = new_dentry->d_inode;
3614 unsigned max_links = new_dir->i_sb->s_max_links;
3615
3616
3617
3618
3619
3620 if (new_dir != old_dir) {
3621 error = inode_permission(old_dentry->d_inode, MAY_WRITE);
3622 if (error)
3623 return error;
3624 }
3625
3626 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3627 if (error)
3628 return error;
3629
3630 dget(new_dentry);
3631 if (target)
3632 mutex_lock(&target->i_mutex);
3633
3634 error = -EBUSY;
3635 if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
3636 goto out;
3637
3638 error = -EMLINK;
3639 if (max_links && !target && new_dir != old_dir &&
3640 new_dir->i_nlink >= max_links)
3641 goto out;
3642
3643 if (target)
3644 shrink_dcache_parent(new_dentry);
3645 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3646 if (error)
3647 goto out;
3648
3649 if (target) {
3650 target->i_flags |= S_DEAD;
3651 dont_mount(new_dentry);
3652 }
3653out:
3654 if (target)
3655 mutex_unlock(&target->i_mutex);
3656 dput(new_dentry);
3657 if (!error)
3658 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3659 d_move(old_dentry,new_dentry);
3660 return error;
3661}
3662
3663static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
3664 struct inode *new_dir, struct dentry *new_dentry)
3665{
3666 struct inode *target = new_dentry->d_inode;
3667 int error;
3668
3669 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
3670 if (error)
3671 return error;
3672
3673 dget(new_dentry);
3674 if (target)
3675 mutex_lock(&target->i_mutex);
3676
3677 error = -EBUSY;
3678 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
3679 goto out;
3680
3681 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3682 if (error)
3683 goto out;
3684
3685 if (target)
3686 dont_mount(new_dentry);
3687 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
3688 d_move(old_dentry, new_dentry);
3689out:
3690 if (target)
3691 mutex_unlock(&target->i_mutex);
3692 dput(new_dentry);
3693 return error;
3694}
3695
3696int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
3697 struct inode *new_dir, struct dentry *new_dentry)
3698{
3699 int error;
3700 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
3701 const unsigned char *old_name;
3702
3703 if (old_dentry->d_inode == new_dentry->d_inode)
3704 return 0;
3705
3706 error = may_delete(old_dir, old_dentry, is_dir);
3707 if (error)
3708 return error;
3709
3710 if (!new_dentry->d_inode)
3711 error = may_create(new_dir, new_dentry);
3712 else
3713 error = may_delete(new_dir, new_dentry, is_dir);
3714 if (error)
3715 return error;
3716
3717 if (!old_dir->i_op->rename)
3718 return -EPERM;
3719
3720 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
3721
3722 if (is_dir)
3723 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
3724 else
3725 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3726 if (!error)
3727 fsnotify_move(old_dir, new_dir, old_name, is_dir,
3728 new_dentry->d_inode, old_dentry);
3729 fsnotify_oldname_free(old_name);
3730
3731 return error;
3732}
3733
3734SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
3735 int, newdfd, const char __user *, newname)
3736{
3737 struct dentry *old_dir, *new_dir;
3738 struct dentry *old_dentry, *new_dentry;
3739 struct dentry *trap;
3740 struct nameidata oldnd, newnd;
3741 char *from;
3742 char *to;
3743 int error;
3744
3745 error = user_path_parent(olddfd, oldname, &oldnd, &from);
3746 if (error)
3747 goto exit;
3748
3749 error = user_path_parent(newdfd, newname, &newnd, &to);
3750 if (error)
3751 goto exit1;
3752
3753 error = -EXDEV;
3754 if (oldnd.path.mnt != newnd.path.mnt)
3755 goto exit2;
3756
3757 old_dir = oldnd.path.dentry;
3758 error = -EBUSY;
3759 if (oldnd.last_type != LAST_NORM)
3760 goto exit2;
3761
3762 new_dir = newnd.path.dentry;
3763 if (newnd.last_type != LAST_NORM)
3764 goto exit2;
3765
3766 error = mnt_want_write(oldnd.path.mnt);
3767 if (error)
3768 goto exit2;
3769
3770 oldnd.flags &= ~LOOKUP_PARENT;
3771 newnd.flags &= ~LOOKUP_PARENT;
3772 newnd.flags |= LOOKUP_RENAME_TARGET;
3773
3774 trap = lock_rename(new_dir, old_dir);
3775
3776 old_dentry = lookup_hash(&oldnd);
3777 error = PTR_ERR(old_dentry);
3778 if (IS_ERR(old_dentry))
3779 goto exit3;
3780
3781 error = -ENOENT;
3782 if (!old_dentry->d_inode)
3783 goto exit4;
3784
3785 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
3786 error = -ENOTDIR;
3787 if (oldnd.last.name[oldnd.last.len])
3788 goto exit4;
3789 if (newnd.last.name[newnd.last.len])
3790 goto exit4;
3791 }
3792
3793 error = -EINVAL;
3794 if (old_dentry == trap)
3795 goto exit4;
3796 new_dentry = lookup_hash(&newnd);
3797 error = PTR_ERR(new_dentry);
3798 if (IS_ERR(new_dentry))
3799 goto exit4;
3800
3801 error = -ENOTEMPTY;
3802 if (new_dentry == trap)
3803 goto exit5;
3804
3805 error = security_path_rename(&oldnd.path, old_dentry,
3806 &newnd.path, new_dentry);
3807 if (error)
3808 goto exit5;
3809 error = vfs_rename(old_dir->d_inode, old_dentry,
3810 new_dir->d_inode, new_dentry);
3811exit5:
3812 dput(new_dentry);
3813exit4:
3814 dput(old_dentry);
3815exit3:
3816 unlock_rename(new_dir, old_dir);
3817 mnt_drop_write(oldnd.path.mnt);
3818exit2:
3819 path_put(&newnd.path);
3820 putname(to);
3821exit1:
3822 path_put(&oldnd.path);
3823 putname(from);
3824exit:
3825 return error;
3826}
3827
3828SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
3829{
3830 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
3831}
3832
3833int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
3834{
3835 int len;
3836
3837 len = PTR_ERR(link);
3838 if (IS_ERR(link))
3839 goto out;
3840
3841 len = strlen(link);
3842 if (len > (unsigned) buflen)
3843 len = buflen;
3844 if (copy_to_user(buffer, link, len))
3845 len = -EFAULT;
3846out:
3847 return len;
3848}
3849
3850
3851
3852
3853
3854
3855int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3856{
3857 struct nameidata nd;
3858 void *cookie;
3859 int res;
3860
3861 nd.depth = 0;
3862 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3863 if (IS_ERR(cookie))
3864 return PTR_ERR(cookie);
3865
3866 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
3867 if (dentry->d_inode->i_op->put_link)
3868 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3869 return res;
3870}
3871
3872int vfs_follow_link(struct nameidata *nd, const char *link)
3873{
3874 return __vfs_follow_link(nd, link);
3875}
3876
3877
3878static char *page_getlink(struct dentry * dentry, struct page **ppage)
3879{
3880 char *kaddr;
3881 struct page *page;
3882 struct address_space *mapping = dentry->d_inode->i_mapping;
3883 page = read_mapping_page(mapping, 0, NULL);
3884 if (IS_ERR(page))
3885 return (char*)page;
3886 *ppage = page;
3887 kaddr = kmap(page);
3888 nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3889 return kaddr;
3890}
3891
3892int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
3893{
3894 struct page *page = NULL;
3895 char *s = page_getlink(dentry, &page);
3896 int res = vfs_readlink(dentry,buffer,buflen,s);
3897 if (page) {
3898 kunmap(page);
3899 page_cache_release(page);
3900 }
3901 return res;
3902}
3903
3904void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
3905{
3906 struct page *page = NULL;
3907 nd_set_link(nd, page_getlink(dentry, &page));
3908 return page;
3909}
3910
3911void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
3912{
3913 struct page *page = cookie;
3914
3915 if (page) {
3916 kunmap(page);
3917 page_cache_release(page);
3918 }
3919}
3920
3921
3922
3923
3924int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
3925{
3926 struct address_space *mapping = inode->i_mapping;
3927 struct page *page;
3928 void *fsdata;
3929 int err;
3930 char *kaddr;
3931 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
3932 if (nofs)
3933 flags |= AOP_FLAG_NOFS;
3934
3935retry:
3936 err = pagecache_write_begin(NULL, mapping, 0, len-1,
3937 flags, &page, &fsdata);
3938 if (err)
3939 goto fail;
3940
3941 kaddr = kmap_atomic(page);
3942 memcpy(kaddr, symname, len-1);
3943 kunmap_atomic(kaddr);
3944
3945 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3946 page, fsdata);
3947 if (err < 0)
3948 goto fail;
3949 if (err < len-1)
3950 goto retry;
3951
3952 mark_inode_dirty(inode);
3953 return 0;
3954fail:
3955 return err;
3956}
3957
3958int page_symlink(struct inode *inode, const char *symname, int len)
3959{
3960 return __page_symlink(inode, symname, len,
3961 !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
3962}
3963
3964const struct inode_operations page_symlink_inode_operations = {
3965 .readlink = generic_readlink,
3966 .follow_link = page_follow_link_light,
3967 .put_link = page_put_link,
3968};
3969
3970EXPORT_SYMBOL(user_path_at);
3971EXPORT_SYMBOL(follow_down_one);
3972EXPORT_SYMBOL(follow_down);
3973EXPORT_SYMBOL(follow_up);
3974EXPORT_SYMBOL(get_write_access);
3975EXPORT_SYMBOL(getname);
3976EXPORT_SYMBOL(lock_rename);
3977EXPORT_SYMBOL(lookup_one_len);
3978EXPORT_SYMBOL(page_follow_link_light);
3979EXPORT_SYMBOL(page_put_link);
3980EXPORT_SYMBOL(page_readlink);
3981EXPORT_SYMBOL(__page_symlink);
3982EXPORT_SYMBOL(page_symlink);
3983EXPORT_SYMBOL(page_symlink_inode_operations);
3984EXPORT_SYMBOL(kern_path);
3985EXPORT_SYMBOL(vfs_path_lookup);
3986EXPORT_SYMBOL(inode_permission);
3987EXPORT_SYMBOL(unlock_rename);
3988EXPORT_SYMBOL(vfs_create);
3989EXPORT_SYMBOL(vfs_follow_link);
3990EXPORT_SYMBOL(vfs_link);
3991EXPORT_SYMBOL(vfs_mkdir);
3992EXPORT_SYMBOL(vfs_mknod);
3993EXPORT_SYMBOL(generic_permission);
3994EXPORT_SYMBOL(vfs_readlink);
3995EXPORT_SYMBOL(vfs_rename);
3996EXPORT_SYMBOL(vfs_rmdir);
3997EXPORT_SYMBOL(vfs_symlink);
3998EXPORT_SYMBOL(vfs_unlink);
3999EXPORT_SYMBOL(dentry_unhash);
4000EXPORT_SYMBOL(generic_readlink);
4001