aboutsummaryrefslogtreecommitdiffstats
path: root/fs/namei.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/namei.c')
-rw-r--r--fs/namei.c1490
1 files changed, 657 insertions, 833 deletions
diff --git a/fs/namei.c b/fs/namei.c
index 0087cf9c2c6b..0a601cae23de 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -136,7 +136,7 @@ static int do_getname(const char __user *filename, char *page)
136 return retval; 136 return retval;
137} 137}
138 138
139char * getname(const char __user * filename) 139static char *getname_flags(const char __user * filename, int flags)
140{ 140{
141 char *tmp, *result; 141 char *tmp, *result;
142 142
@@ -147,14 +147,21 @@ char * getname(const char __user * filename)
147 147
148 result = tmp; 148 result = tmp;
149 if (retval < 0) { 149 if (retval < 0) {
150 __putname(tmp); 150 if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
151 result = ERR_PTR(retval); 151 __putname(tmp);
152 result = ERR_PTR(retval);
153 }
152 } 154 }
153 } 155 }
154 audit_getname(result); 156 audit_getname(result);
155 return result; 157 return result;
156} 158}
157 159
160char *getname(const char __user * filename)
161{
162 return getname_flags(filename, 0);
163}
164
158#ifdef CONFIG_AUDITSYSCALL 165#ifdef CONFIG_AUDITSYSCALL
159void putname(const char *name) 166void putname(const char *name)
160{ 167{
@@ -401,9 +408,11 @@ static int nameidata_drop_rcu(struct nameidata *nd)
401{ 408{
402 struct fs_struct *fs = current->fs; 409 struct fs_struct *fs = current->fs;
403 struct dentry *dentry = nd->path.dentry; 410 struct dentry *dentry = nd->path.dentry;
411 int want_root = 0;
404 412
405 BUG_ON(!(nd->flags & LOOKUP_RCU)); 413 BUG_ON(!(nd->flags & LOOKUP_RCU));
406 if (nd->root.mnt) { 414 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
415 want_root = 1;
407 spin_lock(&fs->lock); 416 spin_lock(&fs->lock);
408 if (nd->root.mnt != fs->root.mnt || 417 if (nd->root.mnt != fs->root.mnt ||
409 nd->root.dentry != fs->root.dentry) 418 nd->root.dentry != fs->root.dentry)
@@ -414,7 +423,7 @@ static int nameidata_drop_rcu(struct nameidata *nd)
414 goto err; 423 goto err;
415 BUG_ON(nd->inode != dentry->d_inode); 424 BUG_ON(nd->inode != dentry->d_inode);
416 spin_unlock(&dentry->d_lock); 425 spin_unlock(&dentry->d_lock);
417 if (nd->root.mnt) { 426 if (want_root) {
418 path_get(&nd->root); 427 path_get(&nd->root);
419 spin_unlock(&fs->lock); 428 spin_unlock(&fs->lock);
420 } 429 }
@@ -427,7 +436,7 @@ static int nameidata_drop_rcu(struct nameidata *nd)
427err: 436err:
428 spin_unlock(&dentry->d_lock); 437 spin_unlock(&dentry->d_lock);
429err_root: 438err_root:
430 if (nd->root.mnt) 439 if (want_root)
431 spin_unlock(&fs->lock); 440 spin_unlock(&fs->lock);
432 return -ECHILD; 441 return -ECHILD;
433} 442}
@@ -454,9 +463,11 @@ static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry
454{ 463{
455 struct fs_struct *fs = current->fs; 464 struct fs_struct *fs = current->fs;
456 struct dentry *parent = nd->path.dentry; 465 struct dentry *parent = nd->path.dentry;
466 int want_root = 0;
457 467
458 BUG_ON(!(nd->flags & LOOKUP_RCU)); 468 BUG_ON(!(nd->flags & LOOKUP_RCU));
459 if (nd->root.mnt) { 469 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
470 want_root = 1;
460 spin_lock(&fs->lock); 471 spin_lock(&fs->lock);
461 if (nd->root.mnt != fs->root.mnt || 472 if (nd->root.mnt != fs->root.mnt ||
462 nd->root.dentry != fs->root.dentry) 473 nd->root.dentry != fs->root.dentry)
@@ -476,7 +487,7 @@ static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry
476 parent->d_count++; 487 parent->d_count++;
477 spin_unlock(&dentry->d_lock); 488 spin_unlock(&dentry->d_lock);
478 spin_unlock(&parent->d_lock); 489 spin_unlock(&parent->d_lock);
479 if (nd->root.mnt) { 490 if (want_root) {
480 path_get(&nd->root); 491 path_get(&nd->root);
481 spin_unlock(&fs->lock); 492 spin_unlock(&fs->lock);
482 } 493 }
@@ -490,7 +501,7 @@ err:
490 spin_unlock(&dentry->d_lock); 501 spin_unlock(&dentry->d_lock);
491 spin_unlock(&parent->d_lock); 502 spin_unlock(&parent->d_lock);
492err_root: 503err_root:
493 if (nd->root.mnt) 504 if (want_root)
494 spin_unlock(&fs->lock); 505 spin_unlock(&fs->lock);
495 return -ECHILD; 506 return -ECHILD;
496} 507}
@@ -498,8 +509,16 @@ err_root:
498/* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 509/* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
499static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry) 510static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
500{ 511{
501 if (nd->flags & LOOKUP_RCU) 512 if (nd->flags & LOOKUP_RCU) {
502 return nameidata_dentry_drop_rcu(nd, dentry); 513 if (unlikely(nameidata_dentry_drop_rcu(nd, dentry))) {
514 nd->flags &= ~LOOKUP_RCU;
515 if (!(nd->flags & LOOKUP_ROOT))
516 nd->root.mnt = NULL;
517 rcu_read_unlock();
518 br_read_unlock(vfsmount_lock);
519 return -ECHILD;
520 }
521 }
503 return 0; 522 return 0;
504} 523}
505 524
@@ -518,7 +537,8 @@ static int nameidata_drop_rcu_last(struct nameidata *nd)
518 537
519 BUG_ON(!(nd->flags & LOOKUP_RCU)); 538 BUG_ON(!(nd->flags & LOOKUP_RCU));
520 nd->flags &= ~LOOKUP_RCU; 539 nd->flags &= ~LOOKUP_RCU;
521 nd->root.mnt = NULL; 540 if (!(nd->flags & LOOKUP_ROOT))
541 nd->root.mnt = NULL;
522 spin_lock(&dentry->d_lock); 542 spin_lock(&dentry->d_lock);
523 if (!__d_rcu_to_refcount(dentry, nd->seq)) 543 if (!__d_rcu_to_refcount(dentry, nd->seq))
524 goto err_unlock; 544 goto err_unlock;
@@ -539,14 +559,6 @@ err_unlock:
539 return -ECHILD; 559 return -ECHILD;
540} 560}
541 561
542/* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
543static inline int nameidata_drop_rcu_last_maybe(struct nameidata *nd)
544{
545 if (likely(nd->flags & LOOKUP_RCU))
546 return nameidata_drop_rcu_last(nd);
547 return 0;
548}
549
550/** 562/**
551 * release_open_intent - free up open intent resources 563 * release_open_intent - free up open intent resources
552 * @nd: pointer to nameidata 564 * @nd: pointer to nameidata
@@ -590,42 +602,8 @@ do_revalidate(struct dentry *dentry, struct nameidata *nd)
590 return dentry; 602 return dentry;
591} 603}
592 604
593static inline struct dentry *
594do_revalidate_rcu(struct dentry *dentry, struct nameidata *nd)
595{
596 int status = d_revalidate(dentry, nd);
597 if (likely(status > 0))
598 return dentry;
599 if (status == -ECHILD) {
600 if (nameidata_dentry_drop_rcu(nd, dentry))
601 return ERR_PTR(-ECHILD);
602 return do_revalidate(dentry, nd);
603 }
604 if (status < 0)
605 return ERR_PTR(status);
606 /* Don't d_invalidate in rcu-walk mode */
607 if (nameidata_dentry_drop_rcu(nd, dentry))
608 return ERR_PTR(-ECHILD);
609 if (!d_invalidate(dentry)) {
610 dput(dentry);
611 dentry = NULL;
612 }
613 return dentry;
614}
615
616static inline int need_reval_dot(struct dentry *dentry)
617{
618 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
619 return 0;
620
621 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
622 return 0;
623
624 return 1;
625}
626
627/* 605/*
628 * force_reval_path - force revalidation of a dentry 606 * handle_reval_path - force revalidation of a dentry
629 * 607 *
630 * In some situations the path walking code will trust dentries without 608 * In some situations the path walking code will trust dentries without
631 * revalidating them. This causes problems for filesystems that depend on 609 * revalidating them. This causes problems for filesystems that depend on
@@ -639,27 +617,28 @@ static inline int need_reval_dot(struct dentry *dentry)
639 * invalidate the dentry. It's up to the caller to handle putting references 617 * invalidate the dentry. It's up to the caller to handle putting references
640 * to the path if necessary. 618 * to the path if necessary.
641 */ 619 */
642static int 620static inline int handle_reval_path(struct nameidata *nd)
643force_reval_path(struct path *path, struct nameidata *nd)
644{ 621{
622 struct dentry *dentry = nd->path.dentry;
645 int status; 623 int status;
646 struct dentry *dentry = path->dentry;
647 624
648 /* 625 if (likely(!(nd->flags & LOOKUP_JUMPED)))
649 * only check on filesystems where it's possible for the dentry to 626 return 0;
650 * become stale. 627
651 */ 628 if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
652 if (!need_reval_dot(dentry)) 629 return 0;
630
631 if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
653 return 0; 632 return 0;
654 633
634 /* Note: we do not d_invalidate() */
655 status = d_revalidate(dentry, nd); 635 status = d_revalidate(dentry, nd);
656 if (status > 0) 636 if (status > 0)
657 return 0; 637 return 0;
658 638
659 if (!status) { 639 if (!status)
660 d_invalidate(dentry);
661 status = -ESTALE; 640 status = -ESTALE;
662 } 641
663 return status; 642 return status;
664} 643}
665 644
@@ -728,6 +707,7 @@ static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *l
728 path_put(&nd->path); 707 path_put(&nd->path);
729 nd->path = nd->root; 708 nd->path = nd->root;
730 path_get(&nd->root); 709 path_get(&nd->root);
710 nd->flags |= LOOKUP_JUMPED;
731 } 711 }
732 nd->inode = nd->path.dentry->d_inode; 712 nd->inode = nd->path.dentry->d_inode;
733 713
@@ -757,20 +737,44 @@ static inline void path_to_nameidata(const struct path *path,
757 nd->path.dentry = path->dentry; 737 nd->path.dentry = path->dentry;
758} 738}
759 739
740static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
741{
742 struct inode *inode = link->dentry->d_inode;
743 if (!IS_ERR(cookie) && inode->i_op->put_link)
744 inode->i_op->put_link(link->dentry, nd, cookie);
745 path_put(link);
746}
747
760static __always_inline int 748static __always_inline int
761__do_follow_link(const struct path *link, struct nameidata *nd, void **p) 749follow_link(struct path *link, struct nameidata *nd, void **p)
762{ 750{
763 int error; 751 int error;
764 struct dentry *dentry = link->dentry; 752 struct dentry *dentry = link->dentry;
765 753
766 BUG_ON(nd->flags & LOOKUP_RCU); 754 BUG_ON(nd->flags & LOOKUP_RCU);
767 755
756 if (unlikely(current->total_link_count >= 40)) {
757 *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
758 path_put_conditional(link, nd);
759 path_put(&nd->path);
760 return -ELOOP;
761 }
762 cond_resched();
763 current->total_link_count++;
764
768 touch_atime(link->mnt, dentry); 765 touch_atime(link->mnt, dentry);
769 nd_set_link(nd, NULL); 766 nd_set_link(nd, NULL);
770 767
771 if (link->mnt == nd->path.mnt) 768 if (link->mnt == nd->path.mnt)
772 mntget(link->mnt); 769 mntget(link->mnt);
773 770
771 error = security_inode_follow_link(link->dentry, nd);
772 if (error) {
773 *p = ERR_PTR(error); /* no ->put_link(), please */
774 path_put(&nd->path);
775 return error;
776 }
777
774 nd->last_type = LAST_BIND; 778 nd->last_type = LAST_BIND;
775 *p = dentry->d_inode->i_op->follow_link(dentry, nd); 779 *p = dentry->d_inode->i_op->follow_link(dentry, nd);
776 error = PTR_ERR(*p); 780 error = PTR_ERR(*p);
@@ -780,56 +784,18 @@ __do_follow_link(const struct path *link, struct nameidata *nd, void **p)
780 if (s) 784 if (s)
781 error = __vfs_follow_link(nd, s); 785 error = __vfs_follow_link(nd, s);
782 else if (nd->last_type == LAST_BIND) { 786 else if (nd->last_type == LAST_BIND) {
783 error = force_reval_path(&nd->path, nd); 787 nd->flags |= LOOKUP_JUMPED;
784 if (error) 788 nd->inode = nd->path.dentry->d_inode;
789 if (nd->inode->i_op->follow_link) {
790 /* stepped on a _really_ weird one */
785 path_put(&nd->path); 791 path_put(&nd->path);
792 error = -ELOOP;
793 }
786 } 794 }
787 } 795 }
788 return error; 796 return error;
789} 797}
790 798
791/*
792 * This limits recursive symlink follows to 8, while
793 * limiting consecutive symlinks to 40.
794 *
795 * Without that kind of total limit, nasty chains of consecutive
796 * symlinks can cause almost arbitrarily long lookups.
797 */
798static inline int do_follow_link(struct inode *inode, struct path *path, struct nameidata *nd)
799{
800 void *cookie;
801 int err = -ELOOP;
802
803 /* We drop rcu-walk here */
804 if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
805 return -ECHILD;
806 BUG_ON(inode != path->dentry->d_inode);
807
808 if (current->link_count >= MAX_NESTED_LINKS)
809 goto loop;
810 if (current->total_link_count >= 40)
811 goto loop;
812 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
813 cond_resched();
814 err = security_inode_follow_link(path->dentry, nd);
815 if (err)
816 goto loop;
817 current->link_count++;
818 current->total_link_count++;
819 nd->depth++;
820 err = __do_follow_link(path, nd, &cookie);
821 if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
822 path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
823 path_put(path);
824 current->link_count--;
825 nd->depth--;
826 return err;
827loop:
828 path_put_conditional(path, nd);
829 path_put(&nd->path);
830 return err;
831}
832
833static int follow_up_rcu(struct path *path) 799static int follow_up_rcu(struct path *path)
834{ 800{
835 struct vfsmount *parent; 801 struct vfsmount *parent;
@@ -1068,7 +1034,7 @@ static int follow_dotdot_rcu(struct nameidata *nd)
1068 1034
1069 seq = read_seqcount_begin(&parent->d_seq); 1035 seq = read_seqcount_begin(&parent->d_seq);
1070 if (read_seqcount_retry(&old->d_seq, nd->seq)) 1036 if (read_seqcount_retry(&old->d_seq, nd->seq))
1071 return -ECHILD; 1037 goto failed;
1072 inode = parent->d_inode; 1038 inode = parent->d_inode;
1073 nd->path.dentry = parent; 1039 nd->path.dentry = parent;
1074 nd->seq = seq; 1040 nd->seq = seq;
@@ -1081,8 +1047,15 @@ static int follow_dotdot_rcu(struct nameidata *nd)
1081 } 1047 }
1082 __follow_mount_rcu(nd, &nd->path, &inode, true); 1048 __follow_mount_rcu(nd, &nd->path, &inode, true);
1083 nd->inode = inode; 1049 nd->inode = inode;
1084
1085 return 0; 1050 return 0;
1051
1052failed:
1053 nd->flags &= ~LOOKUP_RCU;
1054 if (!(nd->flags & LOOKUP_ROOT))
1055 nd->root.mnt = NULL;
1056 rcu_read_unlock();
1057 br_read_unlock(vfsmount_lock);
1058 return -ECHILD;
1086} 1059}
1087 1060
1088/* 1061/*
@@ -1216,68 +1189,85 @@ static int do_lookup(struct nameidata *nd, struct qstr *name,
1216{ 1189{
1217 struct vfsmount *mnt = nd->path.mnt; 1190 struct vfsmount *mnt = nd->path.mnt;
1218 struct dentry *dentry, *parent = nd->path.dentry; 1191 struct dentry *dentry, *parent = nd->path.dentry;
1219 struct inode *dir; 1192 int need_reval = 1;
1193 int status = 1;
1220 int err; 1194 int err;
1221 1195
1222 /* 1196 /*
1223 * See if the low-level filesystem might want
1224 * to use its own hash..
1225 */
1226 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1227 err = parent->d_op->d_hash(parent, nd->inode, name);
1228 if (err < 0)
1229 return err;
1230 }
1231
1232 /*
1233 * Rename seqlock is not required here because in the off chance 1197 * Rename seqlock is not required here because in the off chance
1234 * of a false negative due to a concurrent rename, we're going to 1198 * of a false negative due to a concurrent rename, we're going to
1235 * do the non-racy lookup, below. 1199 * do the non-racy lookup, below.
1236 */ 1200 */
1237 if (nd->flags & LOOKUP_RCU) { 1201 if (nd->flags & LOOKUP_RCU) {
1238 unsigned seq; 1202 unsigned seq;
1239
1240 *inode = nd->inode; 1203 *inode = nd->inode;
1241 dentry = __d_lookup_rcu(parent, name, &seq, inode); 1204 dentry = __d_lookup_rcu(parent, name, &seq, inode);
1242 if (!dentry) { 1205 if (!dentry)
1243 if (nameidata_drop_rcu(nd)) 1206 goto unlazy;
1244 return -ECHILD; 1207
1245 goto need_lookup;
1246 }
1247 /* Memory barrier in read_seqcount_begin of child is enough */ 1208 /* Memory barrier in read_seqcount_begin of child is enough */
1248 if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 1209 if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1249 return -ECHILD; 1210 return -ECHILD;
1250
1251 nd->seq = seq; 1211 nd->seq = seq;
1212
1252 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 1213 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1253 dentry = do_revalidate_rcu(dentry, nd); 1214 status = d_revalidate(dentry, nd);
1254 if (!dentry) 1215 if (unlikely(status <= 0)) {
1255 goto need_lookup; 1216 if (status != -ECHILD)
1256 if (IS_ERR(dentry)) 1217 need_reval = 0;
1257 goto fail; 1218 goto unlazy;
1258 if (!(nd->flags & LOOKUP_RCU)) 1219 }
1259 goto done;
1260 } 1220 }
1261 path->mnt = mnt; 1221 path->mnt = mnt;
1262 path->dentry = dentry; 1222 path->dentry = dentry;
1263 if (likely(__follow_mount_rcu(nd, path, inode, false))) 1223 if (likely(__follow_mount_rcu(nd, path, inode, false)))
1264 return 0; 1224 return 0;
1265 if (nameidata_drop_rcu(nd)) 1225unlazy:
1266 return -ECHILD; 1226 if (dentry) {
1267 /* fallthru */ 1227 if (nameidata_dentry_drop_rcu(nd, dentry))
1228 return -ECHILD;
1229 } else {
1230 if (nameidata_drop_rcu(nd))
1231 return -ECHILD;
1232 }
1233 } else {
1234 dentry = __d_lookup(parent, name);
1268 } 1235 }
1269 dentry = __d_lookup(parent, name); 1236
1270 if (!dentry) 1237retry:
1271 goto need_lookup; 1238 if (unlikely(!dentry)) {
1272found: 1239 struct inode *dir = parent->d_inode;
1273 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 1240 BUG_ON(nd->inode != dir);
1274 dentry = do_revalidate(dentry, nd); 1241
1275 if (!dentry) 1242 mutex_lock(&dir->i_mutex);
1276 goto need_lookup; 1243 dentry = d_lookup(parent, name);
1277 if (IS_ERR(dentry)) 1244 if (likely(!dentry)) {
1278 goto fail; 1245 dentry = d_alloc_and_lookup(parent, name, nd);
1246 if (IS_ERR(dentry)) {
1247 mutex_unlock(&dir->i_mutex);
1248 return PTR_ERR(dentry);
1249 }
1250 /* known good */
1251 need_reval = 0;
1252 status = 1;
1253 }
1254 mutex_unlock(&dir->i_mutex);
1279 } 1255 }
1280done: 1256 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
1257 status = d_revalidate(dentry, nd);
1258 if (unlikely(status <= 0)) {
1259 if (status < 0) {
1260 dput(dentry);
1261 return status;
1262 }
1263 if (!d_invalidate(dentry)) {
1264 dput(dentry);
1265 dentry = NULL;
1266 need_reval = 1;
1267 goto retry;
1268 }
1269 }
1270
1281 path->mnt = mnt; 1271 path->mnt = mnt;
1282 path->dentry = dentry; 1272 path->dentry = dentry;
1283 err = follow_managed(path, nd->flags); 1273 err = follow_managed(path, nd->flags);
@@ -1287,39 +1277,113 @@ done:
1287 } 1277 }
1288 *inode = path->dentry->d_inode; 1278 *inode = path->dentry->d_inode;
1289 return 0; 1279 return 0;
1280}
1290 1281
1291need_lookup: 1282static inline int may_lookup(struct nameidata *nd)
1292 dir = parent->d_inode; 1283{
1293 BUG_ON(nd->inode != dir); 1284 if (nd->flags & LOOKUP_RCU) {
1285 int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
1286 if (err != -ECHILD)
1287 return err;
1288 if (nameidata_drop_rcu(nd))
1289 return -ECHILD;
1290 }
1291 return exec_permission(nd->inode, 0);
1292}
1294 1293
1295 mutex_lock(&dir->i_mutex); 1294static inline int handle_dots(struct nameidata *nd, int type)
1296 /* 1295{
1297 * First re-do the cached lookup just in case it was created 1296 if (type == LAST_DOTDOT) {
1298 * while we waited for the directory semaphore, or the first 1297 if (nd->flags & LOOKUP_RCU) {
1299 * lookup failed due to an unrelated rename. 1298 if (follow_dotdot_rcu(nd))
1300 * 1299 return -ECHILD;
1301 * This could use version numbering or similar to avoid unnecessary 1300 } else
1302 * cache lookups, but then we'd have to do the first lookup in the 1301 follow_dotdot(nd);
1303 * non-racy way. However in the common case here, everything should 1302 }
1304 * be hot in cache, so would it be a big win? 1303 return 0;
1305 */ 1304}
1306 dentry = d_lookup(parent, name); 1305
1307 if (likely(!dentry)) { 1306static void terminate_walk(struct nameidata *nd)
1308 dentry = d_alloc_and_lookup(parent, name, nd); 1307{
1309 mutex_unlock(&dir->i_mutex); 1308 if (!(nd->flags & LOOKUP_RCU)) {
1310 if (IS_ERR(dentry)) 1309 path_put(&nd->path);
1311 goto fail; 1310 } else {
1312 goto done; 1311 nd->flags &= ~LOOKUP_RCU;
1312 if (!(nd->flags & LOOKUP_ROOT))
1313 nd->root.mnt = NULL;
1314 rcu_read_unlock();
1315 br_read_unlock(vfsmount_lock);
1313 } 1316 }
1317}
1318
1319static inline int walk_component(struct nameidata *nd, struct path *path,
1320 struct qstr *name, int type, int follow)
1321{
1322 struct inode *inode;
1323 int err;
1314 /* 1324 /*
1315 * Uhhuh! Nasty case: the cache was re-populated while 1325 * "." and ".." are special - ".." especially so because it has
1316 * we waited on the semaphore. Need to revalidate. 1326 * to be able to know about the current root directory and
1327 * parent relationships.
1317 */ 1328 */
1318 mutex_unlock(&dir->i_mutex); 1329 if (unlikely(type != LAST_NORM))
1319 goto found; 1330 return handle_dots(nd, type);
1331 err = do_lookup(nd, name, path, &inode);
1332 if (unlikely(err)) {
1333 terminate_walk(nd);
1334 return err;
1335 }
1336 if (!inode) {
1337 path_to_nameidata(path, nd);
1338 terminate_walk(nd);
1339 return -ENOENT;
1340 }
1341 if (unlikely(inode->i_op->follow_link) && follow) {
1342 if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
1343 return -ECHILD;
1344 BUG_ON(inode != path->dentry->d_inode);
1345 return 1;
1346 }
1347 path_to_nameidata(path, nd);
1348 nd->inode = inode;
1349 return 0;
1350}
1320 1351
1321fail: 1352/*
1322 return PTR_ERR(dentry); 1353 * This limits recursive symlink follows to 8, while
1354 * limiting consecutive symlinks to 40.
1355 *
1356 * Without that kind of total limit, nasty chains of consecutive
1357 * symlinks can cause almost arbitrarily long lookups.
1358 */
1359static inline int nested_symlink(struct path *path, struct nameidata *nd)
1360{
1361 int res;
1362
1363 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1364 if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1365 path_put_conditional(path, nd);
1366 path_put(&nd->path);
1367 return -ELOOP;
1368 }
1369
1370 nd->depth++;
1371 current->link_count++;
1372
1373 do {
1374 struct path link = *path;
1375 void *cookie;
1376
1377 res = follow_link(&link, nd, &cookie);
1378 if (!res)
1379 res = walk_component(nd, path, &nd->last,
1380 nd->last_type, LOOKUP_FOLLOW);
1381 put_link(nd, &link, cookie);
1382 } while (res > 0);
1383
1384 current->link_count--;
1385 nd->depth--;
1386 return res;
1323} 1387}
1324 1388
1325/* 1389/*
@@ -1339,30 +1403,18 @@ static int link_path_walk(const char *name, struct nameidata *nd)
1339 while (*name=='/') 1403 while (*name=='/')
1340 name++; 1404 name++;
1341 if (!*name) 1405 if (!*name)
1342 goto return_reval; 1406 return 0;
1343
1344 if (nd->depth)
1345 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
1346 1407
1347 /* At this point we know we have a real path component. */ 1408 /* At this point we know we have a real path component. */
1348 for(;;) { 1409 for(;;) {
1349 struct inode *inode;
1350 unsigned long hash; 1410 unsigned long hash;
1351 struct qstr this; 1411 struct qstr this;
1352 unsigned int c; 1412 unsigned int c;
1413 int type;
1353 1414
1354 nd->flags |= LOOKUP_CONTINUE; 1415 nd->flags |= LOOKUP_CONTINUE;
1355 if (nd->flags & LOOKUP_RCU) { 1416
1356 err = exec_permission(nd->inode, IPERM_FLAG_RCU); 1417 err = may_lookup(nd);
1357 if (err == -ECHILD) {
1358 if (nameidata_drop_rcu(nd))
1359 return -ECHILD;
1360 goto exec_again;
1361 }
1362 } else {
1363exec_again:
1364 err = exec_permission(nd->inode, 0);
1365 }
1366 if (err) 1418 if (err)
1367 break; 1419 break;
1368 1420
@@ -1378,52 +1430,43 @@ exec_again:
1378 this.len = name - (const char *) this.name; 1430 this.len = name - (const char *) this.name;
1379 this.hash = end_name_hash(hash); 1431 this.hash = end_name_hash(hash);
1380 1432
1433 type = LAST_NORM;
1434 if (this.name[0] == '.') switch (this.len) {
1435 case 2:
1436 if (this.name[1] == '.') {
1437 type = LAST_DOTDOT;
1438 nd->flags |= LOOKUP_JUMPED;
1439 }
1440 break;
1441 case 1:
1442 type = LAST_DOT;
1443 }
1444 if (likely(type == LAST_NORM)) {
1445 struct dentry *parent = nd->path.dentry;
1446 nd->flags &= ~LOOKUP_JUMPED;
1447 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1448 err = parent->d_op->d_hash(parent, nd->inode,
1449 &this);
1450 if (err < 0)
1451 break;
1452 }
1453 }
1454
1381 /* remove trailing slashes? */ 1455 /* remove trailing slashes? */
1382 if (!c) 1456 if (!c)
1383 goto last_component; 1457 goto last_component;
1384 while (*++name == '/'); 1458 while (*++name == '/');
1385 if (!*name) 1459 if (!*name)
1386 goto last_with_slashes; 1460 goto last_component;
1387 1461
1388 /* 1462 err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1389 * "." and ".." are special - ".." especially so because it has 1463 if (err < 0)
1390 * to be able to know about the current root directory and 1464 return err;
1391 * parent relationships.
1392 */
1393 if (this.name[0] == '.') switch (this.len) {
1394 default:
1395 break;
1396 case 2:
1397 if (this.name[1] != '.')
1398 break;
1399 if (nd->flags & LOOKUP_RCU) {
1400 if (follow_dotdot_rcu(nd))
1401 return -ECHILD;
1402 } else
1403 follow_dotdot(nd);
1404 /* fallthrough */
1405 case 1:
1406 continue;
1407 }
1408 /* This does the actual lookups.. */
1409 err = do_lookup(nd, &this, &next, &inode);
1410 if (err)
1411 break;
1412 err = -ENOENT;
1413 if (!inode)
1414 goto out_dput;
1415 1465
1416 if (inode->i_op->follow_link) { 1466 if (err) {
1417 err = do_follow_link(inode, &next, nd); 1467 err = nested_symlink(&next, nd);
1418 if (err) 1468 if (err)
1419 goto return_err; 1469 return err;
1420 nd->inode = nd->path.dentry->d_inode;
1421 err = -ENOENT;
1422 if (!nd->inode)
1423 break;
1424 } else {
1425 path_to_nameidata(&next, nd);
1426 nd->inode = inode;
1427 } 1470 }
1428 err = -ENOTDIR; 1471 err = -ENOTDIR;
1429 if (!nd->inode->i_op->lookup) 1472 if (!nd->inode->i_op->lookup)
@@ -1431,209 +1474,109 @@ exec_again:
1431 continue; 1474 continue;
1432 /* here ends the main loop */ 1475 /* here ends the main loop */
1433 1476
1434last_with_slashes:
1435 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1436last_component: 1477last_component:
1437 /* Clear LOOKUP_CONTINUE iff it was previously unset */ 1478 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1438 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; 1479 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
1439 if (lookup_flags & LOOKUP_PARENT)
1440 goto lookup_parent;
1441 if (this.name[0] == '.') switch (this.len) {
1442 default:
1443 break;
1444 case 2:
1445 if (this.name[1] != '.')
1446 break;
1447 if (nd->flags & LOOKUP_RCU) {
1448 if (follow_dotdot_rcu(nd))
1449 return -ECHILD;
1450 } else
1451 follow_dotdot(nd);
1452 /* fallthrough */
1453 case 1:
1454 goto return_reval;
1455 }
1456 err = do_lookup(nd, &this, &next, &inode);
1457 if (err)
1458 break;
1459 if (inode && unlikely(inode->i_op->follow_link) &&
1460 (lookup_flags & LOOKUP_FOLLOW)) {
1461 err = do_follow_link(inode, &next, nd);
1462 if (err)
1463 goto return_err;
1464 nd->inode = nd->path.dentry->d_inode;
1465 } else {
1466 path_to_nameidata(&next, nd);
1467 nd->inode = inode;
1468 }
1469 err = -ENOENT;
1470 if (!nd->inode)
1471 break;
1472 if (lookup_flags & LOOKUP_DIRECTORY) {
1473 err = -ENOTDIR;
1474 if (!nd->inode->i_op->lookup)
1475 break;
1476 }
1477 goto return_base;
1478lookup_parent:
1479 nd->last = this; 1480 nd->last = this;
1480 nd->last_type = LAST_NORM; 1481 nd->last_type = type;
1481 if (this.name[0] != '.')
1482 goto return_base;
1483 if (this.len == 1)
1484 nd->last_type = LAST_DOT;
1485 else if (this.len == 2 && this.name[1] == '.')
1486 nd->last_type = LAST_DOTDOT;
1487 else
1488 goto return_base;
1489return_reval:
1490 /*
1491 * We bypassed the ordinary revalidation routines.
1492 * We may need to check the cached dentry for staleness.
1493 */
1494 if (need_reval_dot(nd->path.dentry)) {
1495 if (nameidata_drop_rcu_last_maybe(nd))
1496 return -ECHILD;
1497 /* Note: we do not d_invalidate() */
1498 err = d_revalidate(nd->path.dentry, nd);
1499 if (!err)
1500 err = -ESTALE;
1501 if (err < 0)
1502 break;
1503 return 0;
1504 }
1505return_base:
1506 if (nameidata_drop_rcu_last_maybe(nd))
1507 return -ECHILD;
1508 return 0; 1482 return 0;
1509out_dput:
1510 if (!(nd->flags & LOOKUP_RCU))
1511 path_put_conditional(&next, nd);
1512 break;
1513 } 1483 }
1514 if (!(nd->flags & LOOKUP_RCU)) 1484 terminate_walk(nd);
1515 path_put(&nd->path);
1516return_err:
1517 return err; 1485 return err;
1518} 1486}
1519 1487
1520static inline int path_walk_rcu(const char *name, struct nameidata *nd) 1488static int path_init(int dfd, const char *name, unsigned int flags,
1521{ 1489 struct nameidata *nd, struct file **fp)
1522 current->total_link_count = 0;
1523
1524 return link_path_walk(name, nd);
1525}
1526
1527static inline int path_walk_simple(const char *name, struct nameidata *nd)
1528{
1529 current->total_link_count = 0;
1530
1531 return link_path_walk(name, nd);
1532}
1533
1534static int path_walk(const char *name, struct nameidata *nd)
1535{
1536 struct path save = nd->path;
1537 int result;
1538
1539 current->total_link_count = 0;
1540
1541 /* make sure the stuff we saved doesn't go away */
1542 path_get(&save);
1543
1544 result = link_path_walk(name, nd);
1545 if (result == -ESTALE) {
1546 /* nd->path had been dropped */
1547 current->total_link_count = 0;
1548 nd->path = save;
1549 path_get(&nd->path);
1550 nd->flags |= LOOKUP_REVAL;
1551 result = link_path_walk(name, nd);
1552 }
1553
1554 path_put(&save);
1555
1556 return result;
1557}
1558
1559static void path_finish_rcu(struct nameidata *nd)
1560{
1561 if (nd->flags & LOOKUP_RCU) {
1562 /* RCU dangling. Cancel it. */
1563 nd->flags &= ~LOOKUP_RCU;
1564 nd->root.mnt = NULL;
1565 rcu_read_unlock();
1566 br_read_unlock(vfsmount_lock);
1567 }
1568 if (nd->file)
1569 fput(nd->file);
1570}
1571
1572static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
1573{ 1490{
1574 int retval = 0; 1491 int retval = 0;
1575 int fput_needed; 1492 int fput_needed;
1576 struct file *file; 1493 struct file *file;
1577 1494
1578 nd->last_type = LAST_ROOT; /* if there are only slashes... */ 1495 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1579 nd->flags = flags | LOOKUP_RCU; 1496 nd->flags = flags | LOOKUP_JUMPED;
1580 nd->depth = 0; 1497 nd->depth = 0;
1498 if (flags & LOOKUP_ROOT) {
1499 struct inode *inode = nd->root.dentry->d_inode;
1500 if (*name) {
1501 if (!inode->i_op->lookup)
1502 return -ENOTDIR;
1503 retval = inode_permission(inode, MAY_EXEC);
1504 if (retval)
1505 return retval;
1506 }
1507 nd->path = nd->root;
1508 nd->inode = inode;
1509 if (flags & LOOKUP_RCU) {
1510 br_read_lock(vfsmount_lock);
1511 rcu_read_lock();
1512 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1513 } else {
1514 path_get(&nd->path);
1515 }
1516 return 0;
1517 }
1518
1581 nd->root.mnt = NULL; 1519 nd->root.mnt = NULL;
1582 nd->file = NULL;
1583 1520
1584 if (*name=='/') { 1521 if (*name=='/') {
1585 struct fs_struct *fs = current->fs; 1522 if (flags & LOOKUP_RCU) {
1586 unsigned seq; 1523 br_read_lock(vfsmount_lock);
1587 1524 rcu_read_lock();
1588 br_read_lock(vfsmount_lock); 1525 set_root_rcu(nd);
1589 rcu_read_lock(); 1526 } else {
1590 1527 set_root(nd);
1591 do { 1528 path_get(&nd->root);
1592 seq = read_seqcount_begin(&fs->seq); 1529 }
1593 nd->root = fs->root; 1530 nd->path = nd->root;
1594 nd->path = nd->root;
1595 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1596 } while (read_seqcount_retry(&fs->seq, seq));
1597
1598 } else if (dfd == AT_FDCWD) { 1531 } else if (dfd == AT_FDCWD) {
1599 struct fs_struct *fs = current->fs; 1532 if (flags & LOOKUP_RCU) {
1600 unsigned seq; 1533 struct fs_struct *fs = current->fs;
1601 1534 unsigned seq;
1602 br_read_lock(vfsmount_lock);
1603 rcu_read_lock();
1604 1535
1605 do { 1536 br_read_lock(vfsmount_lock);
1606 seq = read_seqcount_begin(&fs->seq); 1537 rcu_read_lock();
1607 nd->path = fs->pwd;
1608 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1609 } while (read_seqcount_retry(&fs->seq, seq));
1610 1538
1539 do {
1540 seq = read_seqcount_begin(&fs->seq);
1541 nd->path = fs->pwd;
1542 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1543 } while (read_seqcount_retry(&fs->seq, seq));
1544 } else {
1545 get_fs_pwd(current->fs, &nd->path);
1546 }
1611 } else { 1547 } else {
1612 struct dentry *dentry; 1548 struct dentry *dentry;
1613 1549
1614 file = fget_light(dfd, &fput_needed); 1550 file = fget_raw_light(dfd, &fput_needed);
1615 retval = -EBADF; 1551 retval = -EBADF;
1616 if (!file) 1552 if (!file)
1617 goto out_fail; 1553 goto out_fail;
1618 1554
1619 dentry = file->f_path.dentry; 1555 dentry = file->f_path.dentry;
1620 1556
1621 retval = -ENOTDIR; 1557 if (*name) {
1622 if (!S_ISDIR(dentry->d_inode->i_mode)) 1558 retval = -ENOTDIR;
1623 goto fput_fail; 1559 if (!S_ISDIR(dentry->d_inode->i_mode))
1560 goto fput_fail;
1624 1561
1625 retval = file_permission(file, MAY_EXEC); 1562 retval = file_permission(file, MAY_EXEC);
1626 if (retval) 1563 if (retval)
1627 goto fput_fail; 1564 goto fput_fail;
1565 }
1628 1566
1629 nd->path = file->f_path; 1567 nd->path = file->f_path;
1630 if (fput_needed) 1568 if (flags & LOOKUP_RCU) {
1631 nd->file = file; 1569 if (fput_needed)
1632 1570 *fp = file;
1633 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1571 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1634 br_read_lock(vfsmount_lock); 1572 br_read_lock(vfsmount_lock);
1635 rcu_read_lock(); 1573 rcu_read_lock();
1574 } else {
1575 path_get(&file->f_path);
1576 fput_light(file, fput_needed);
1577 }
1636 } 1578 }
1579
1637 nd->inode = nd->path.dentry->d_inode; 1580 nd->inode = nd->path.dentry->d_inode;
1638 return 0; 1581 return 0;
1639 1582
@@ -1643,60 +1586,23 @@ out_fail:
1643 return retval; 1586 return retval;
1644} 1587}
1645 1588
1646static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd) 1589static inline int lookup_last(struct nameidata *nd, struct path *path)
1647{ 1590{
1648 int retval = 0; 1591 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1649 int fput_needed; 1592 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1650 struct file *file;
1651 1593
1652 nd->last_type = LAST_ROOT; /* if there are only slashes... */ 1594 nd->flags &= ~LOOKUP_PARENT;
1653 nd->flags = flags; 1595 return walk_component(nd, path, &nd->last, nd->last_type,
1654 nd->depth = 0; 1596 nd->flags & LOOKUP_FOLLOW);
1655 nd->root.mnt = NULL;
1656
1657 if (*name=='/') {
1658 set_root(nd);
1659 nd->path = nd->root;
1660 path_get(&nd->root);
1661 } else if (dfd == AT_FDCWD) {
1662 get_fs_pwd(current->fs, &nd->path);
1663 } else {
1664 struct dentry *dentry;
1665
1666 file = fget_light(dfd, &fput_needed);
1667 retval = -EBADF;
1668 if (!file)
1669 goto out_fail;
1670
1671 dentry = file->f_path.dentry;
1672
1673 retval = -ENOTDIR;
1674 if (!S_ISDIR(dentry->d_inode->i_mode))
1675 goto fput_fail;
1676
1677 retval = file_permission(file, MAY_EXEC);
1678 if (retval)
1679 goto fput_fail;
1680
1681 nd->path = file->f_path;
1682 path_get(&file->f_path);
1683
1684 fput_light(file, fput_needed);
1685 }
1686 nd->inode = nd->path.dentry->d_inode;
1687 return 0;
1688
1689fput_fail:
1690 fput_light(file, fput_needed);
1691out_fail:
1692 return retval;
1693} 1597}
1694 1598
1695/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1599/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1696static int do_path_lookup(int dfd, const char *name, 1600static int path_lookupat(int dfd, const char *name,
1697 unsigned int flags, struct nameidata *nd) 1601 unsigned int flags, struct nameidata *nd)
1698{ 1602{
1699 int retval; 1603 struct file *base = NULL;
1604 struct path path;
1605 int err;
1700 1606
1701 /* 1607 /*
1702 * Path walking is largely split up into 2 different synchronisation 1608 * Path walking is largely split up into 2 different synchronisation
@@ -1712,44 +1618,75 @@ static int do_path_lookup(int dfd, const char *name,
1712 * be handled by restarting a traditional ref-walk (which will always 1618 * be handled by restarting a traditional ref-walk (which will always
1713 * be able to complete). 1619 * be able to complete).
1714 */ 1620 */
1715 retval = path_init_rcu(dfd, name, flags, nd); 1621 err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1716 if (unlikely(retval)) 1622
1717 return retval; 1623 if (unlikely(err))
1718 retval = path_walk_rcu(name, nd); 1624 return err;
1719 path_finish_rcu(nd); 1625
1720 if (nd->root.mnt) { 1626 current->total_link_count = 0;
1721 path_put(&nd->root); 1627 err = link_path_walk(name, nd);
1722 nd->root.mnt = NULL; 1628
1629 if (!err && !(flags & LOOKUP_PARENT)) {
1630 err = lookup_last(nd, &path);
1631 while (err > 0) {
1632 void *cookie;
1633 struct path link = path;
1634 nd->flags |= LOOKUP_PARENT;
1635 err = follow_link(&link, nd, &cookie);
1636 if (!err)
1637 err = lookup_last(nd, &path);
1638 put_link(nd, &link, cookie);
1639 }
1723 } 1640 }
1724 1641
1725 if (unlikely(retval == -ECHILD || retval == -ESTALE)) { 1642 if (nd->flags & LOOKUP_RCU) {
1726 /* slower, locked walk */ 1643 /* went all way through without dropping RCU */
1727 if (retval == -ESTALE) 1644 BUG_ON(err);
1728 flags |= LOOKUP_REVAL; 1645 if (nameidata_drop_rcu_last(nd))
1729 retval = path_init(dfd, name, flags, nd); 1646 err = -ECHILD;
1730 if (unlikely(retval)) 1647 }
1731 return retval; 1648
1732 retval = path_walk(name, nd); 1649 if (!err)
1733 if (nd->root.mnt) { 1650 err = handle_reval_path(nd);
1734 path_put(&nd->root); 1651
1735 nd->root.mnt = NULL; 1652 if (!err && nd->flags & LOOKUP_DIRECTORY) {
1653 if (!nd->inode->i_op->lookup) {
1654 path_put(&nd->path);
1655 return -ENOTDIR;
1736 } 1656 }
1737 } 1657 }
1738 1658
1659 if (base)
1660 fput(base);
1661
1662 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1663 path_put(&nd->root);
1664 nd->root.mnt = NULL;
1665 }
1666 return err;
1667}
1668
1669static int do_path_lookup(int dfd, const char *name,
1670 unsigned int flags, struct nameidata *nd)
1671{
1672 int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1673 if (unlikely(retval == -ECHILD))
1674 retval = path_lookupat(dfd, name, flags, nd);
1675 if (unlikely(retval == -ESTALE))
1676 retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1677
1739 if (likely(!retval)) { 1678 if (likely(!retval)) {
1740 if (unlikely(!audit_dummy_context())) { 1679 if (unlikely(!audit_dummy_context())) {
1741 if (nd->path.dentry && nd->inode) 1680 if (nd->path.dentry && nd->inode)
1742 audit_inode(name, nd->path.dentry); 1681 audit_inode(name, nd->path.dentry);
1743 } 1682 }
1744 } 1683 }
1745
1746 return retval; 1684 return retval;
1747} 1685}
1748 1686
1749int path_lookup(const char *name, unsigned int flags, 1687int kern_path_parent(const char *name, struct nameidata *nd)
1750 struct nameidata *nd)
1751{ 1688{
1752 return do_path_lookup(AT_FDCWD, name, flags, nd); 1689 return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
1753} 1690}
1754 1691
1755int kern_path(const char *name, unsigned int flags, struct path *path) 1692int kern_path(const char *name, unsigned int flags, struct path *path)
@@ -1773,29 +1710,10 @@ int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1773 const char *name, unsigned int flags, 1710 const char *name, unsigned int flags,
1774 struct nameidata *nd) 1711 struct nameidata *nd)
1775{ 1712{
1776 int retval; 1713 nd->root.dentry = dentry;
1777 1714 nd->root.mnt = mnt;
1778 /* same as do_path_lookup */ 1715 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1779 nd->last_type = LAST_ROOT; 1716 return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
1780 nd->flags = flags;
1781 nd->depth = 0;
1782
1783 nd->path.dentry = dentry;
1784 nd->path.mnt = mnt;
1785 path_get(&nd->path);
1786 nd->root = nd->path;
1787 path_get(&nd->root);
1788 nd->inode = nd->path.dentry->d_inode;
1789
1790 retval = path_walk(name, nd);
1791 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1792 nd->inode))
1793 audit_inode(name, nd->path.dentry);
1794
1795 path_put(&nd->root);
1796 nd->root.mnt = NULL;
1797
1798 return retval;
1799} 1717}
1800 1718
1801static struct dentry *__lookup_hash(struct qstr *name, 1719static struct dentry *__lookup_hash(struct qstr *name,
@@ -1810,17 +1728,6 @@ static struct dentry *__lookup_hash(struct qstr *name,
1810 return ERR_PTR(err); 1728 return ERR_PTR(err);
1811 1729
1812 /* 1730 /*
1813 * See if the low-level filesystem might want
1814 * to use its own hash..
1815 */
1816 if (base->d_flags & DCACHE_OP_HASH) {
1817 err = base->d_op->d_hash(base, inode, name);
1818 dentry = ERR_PTR(err);
1819 if (err < 0)
1820 goto out;
1821 }
1822
1823 /*
1824 * Don't bother with __d_lookup: callers are for creat as 1731 * Don't bother with __d_lookup: callers are for creat as
1825 * well as unlink, so a lot of the time it would cost 1732 * well as unlink, so a lot of the time it would cost
1826 * a double lookup. 1733 * a double lookup.
@@ -1832,7 +1739,7 @@ static struct dentry *__lookup_hash(struct qstr *name,
1832 1739
1833 if (!dentry) 1740 if (!dentry)
1834 dentry = d_alloc_and_lookup(base, name, nd); 1741 dentry = d_alloc_and_lookup(base, name, nd);
1835out: 1742
1836 return dentry; 1743 return dentry;
1837} 1744}
1838 1745
@@ -1846,28 +1753,6 @@ static struct dentry *lookup_hash(struct nameidata *nd)
1846 return __lookup_hash(&nd->last, nd->path.dentry, nd); 1753 return __lookup_hash(&nd->last, nd->path.dentry, nd);
1847} 1754}
1848 1755
1849static int __lookup_one_len(const char *name, struct qstr *this,
1850 struct dentry *base, int len)
1851{
1852 unsigned long hash;
1853 unsigned int c;
1854
1855 this->name = name;
1856 this->len = len;
1857 if (!len)
1858 return -EACCES;
1859
1860 hash = init_name_hash();
1861 while (len--) {
1862 c = *(const unsigned char *)name++;
1863 if (c == '/' || c == '\0')
1864 return -EACCES;
1865 hash = partial_name_hash(c, hash);
1866 }
1867 this->hash = end_name_hash(hash);
1868 return 0;
1869}
1870
1871/** 1756/**
1872 * lookup_one_len - filesystem helper to lookup single pathname component 1757 * lookup_one_len - filesystem helper to lookup single pathname component
1873 * @name: pathname component to lookup 1758 * @name: pathname component to lookup
@@ -1881,14 +1766,34 @@ static int __lookup_one_len(const char *name, struct qstr *this,
1881 */ 1766 */
1882struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1767struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1883{ 1768{
1884 int err;
1885 struct qstr this; 1769 struct qstr this;
1770 unsigned long hash;
1771 unsigned int c;
1886 1772
1887 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 1773 WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1888 1774
1889 err = __lookup_one_len(name, &this, base, len); 1775 this.name = name;
1890 if (err) 1776 this.len = len;
1891 return ERR_PTR(err); 1777 if (!len)
1778 return ERR_PTR(-EACCES);
1779
1780 hash = init_name_hash();
1781 while (len--) {
1782 c = *(const unsigned char *)name++;
1783 if (c == '/' || c == '\0')
1784 return ERR_PTR(-EACCES);
1785 hash = partial_name_hash(c, hash);
1786 }
1787 this.hash = end_name_hash(hash);
1788 /*
1789 * See if the low-level filesystem might want
1790 * to use its own hash..
1791 */
1792 if (base->d_flags & DCACHE_OP_HASH) {
1793 int err = base->d_op->d_hash(base, base->d_inode, &this);
1794 if (err < 0)
1795 return ERR_PTR(err);
1796 }
1892 1797
1893 return __lookup_hash(&this, base, NULL); 1798 return __lookup_hash(&this, base, NULL);
1894} 1799}
@@ -1897,7 +1802,7 @@ int user_path_at(int dfd, const char __user *name, unsigned flags,
1897 struct path *path) 1802 struct path *path)
1898{ 1803{
1899 struct nameidata nd; 1804 struct nameidata nd;
1900 char *tmp = getname(name); 1805 char *tmp = getname_flags(name, flags);
1901 int err = PTR_ERR(tmp); 1806 int err = PTR_ERR(tmp);
1902 if (!IS_ERR(tmp)) { 1807 if (!IS_ERR(tmp)) {
1903 1808
@@ -2077,12 +1982,16 @@ int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
2077 return error; 1982 return error;
2078} 1983}
2079 1984
2080int may_open(struct path *path, int acc_mode, int flag) 1985static int may_open(struct path *path, int acc_mode, int flag)
2081{ 1986{
2082 struct dentry *dentry = path->dentry; 1987 struct dentry *dentry = path->dentry;
2083 struct inode *inode = dentry->d_inode; 1988 struct inode *inode = dentry->d_inode;
2084 int error; 1989 int error;
2085 1990
1991 /* O_PATH? */
1992 if (!acc_mode)
1993 return 0;
1994
2086 if (!inode) 1995 if (!inode)
2087 return -ENOENT; 1996 return -ENOENT;
2088 1997
@@ -2151,34 +2060,6 @@ static int handle_truncate(struct file *filp)
2151} 2060}
2152 2061
2153/* 2062/*
2154 * Be careful about ever adding any more callers of this
2155 * function. Its flags must be in the namei format, not
2156 * what get passed to sys_open().
2157 */
2158static int __open_namei_create(struct nameidata *nd, struct path *path,
2159 int open_flag, int mode)
2160{
2161 int error;
2162 struct dentry *dir = nd->path.dentry;
2163
2164 if (!IS_POSIXACL(dir->d_inode))
2165 mode &= ~current_umask();
2166 error = security_path_mknod(&nd->path, path->dentry, mode, 0);
2167 if (error)
2168 goto out_unlock;
2169 error = vfs_create(dir->d_inode, path->dentry, mode, nd);
2170out_unlock:
2171 mutex_unlock(&dir->d_inode->i_mutex);
2172 dput(nd->path.dentry);
2173 nd->path.dentry = path->dentry;
2174
2175 if (error)
2176 return error;
2177 /* Don't check for write permission, don't truncate */
2178 return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
2179}
2180
2181/*
2182 * Note that while the flag value (low two bits) for sys_open means: 2063 * Note that while the flag value (low two bits) for sys_open means:
2183 * 00 - read-only 2064 * 00 - read-only
2184 * 01 - write-only 2065 * 01 - write-only
@@ -2202,126 +2083,115 @@ static inline int open_to_namei_flags(int flag)
2202 return flag; 2083 return flag;
2203} 2084}
2204 2085
2205static int open_will_truncate(int flag, struct inode *inode)
2206{
2207 /*
2208 * We'll never write to the fs underlying
2209 * a device file.
2210 */
2211 if (special_file(inode->i_mode))
2212 return 0;
2213 return (flag & O_TRUNC);
2214}
2215
2216static struct file *finish_open(struct nameidata *nd,
2217 int open_flag, int acc_mode)
2218{
2219 struct file *filp;
2220 int will_truncate;
2221 int error;
2222
2223 will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
2224 if (will_truncate) {
2225 error = mnt_want_write(nd->path.mnt);
2226 if (error)
2227 goto exit;
2228 }
2229 error = may_open(&nd->path, acc_mode, open_flag);
2230 if (error) {
2231 if (will_truncate)
2232 mnt_drop_write(nd->path.mnt);
2233 goto exit;
2234 }
2235 filp = nameidata_to_filp(nd);
2236 if (!IS_ERR(filp)) {
2237 error = ima_file_check(filp, acc_mode);
2238 if (error) {
2239 fput(filp);
2240 filp = ERR_PTR(error);
2241 }
2242 }
2243 if (!IS_ERR(filp)) {
2244 if (will_truncate) {
2245 error = handle_truncate(filp);
2246 if (error) {
2247 fput(filp);
2248 filp = ERR_PTR(error);
2249 }
2250 }
2251 }
2252 /*
2253 * It is now safe to drop the mnt write
2254 * because the filp has had a write taken
2255 * on its behalf.
2256 */
2257 if (will_truncate)
2258 mnt_drop_write(nd->path.mnt);
2259 path_put(&nd->path);
2260 return filp;
2261
2262exit:
2263 path_put(&nd->path);
2264 return ERR_PTR(error);
2265}
2266
2267/* 2086/*
2268 * Handle O_CREAT case for do_filp_open 2087 * Handle the last step of open()
2269 */ 2088 */
2270static struct file *do_last(struct nameidata *nd, struct path *path, 2089static struct file *do_last(struct nameidata *nd, struct path *path,
2271 int open_flag, int acc_mode, 2090 const struct open_flags *op, const char *pathname)
2272 int mode, const char *pathname)
2273{ 2091{
2274 struct dentry *dir = nd->path.dentry; 2092 struct dentry *dir = nd->path.dentry;
2093 struct dentry *dentry;
2094 int open_flag = op->open_flag;
2095 int will_truncate = open_flag & O_TRUNC;
2096 int want_write = 0;
2097 int acc_mode = op->acc_mode;
2275 struct file *filp; 2098 struct file *filp;
2276 int error = -EISDIR; 2099 int error;
2100
2101 nd->flags &= ~LOOKUP_PARENT;
2102 nd->flags |= op->intent;
2277 2103
2278 switch (nd->last_type) { 2104 switch (nd->last_type) {
2279 case LAST_DOTDOT: 2105 case LAST_DOTDOT:
2280 follow_dotdot(nd);
2281 dir = nd->path.dentry;
2282 case LAST_DOT: 2106 case LAST_DOT:
2283 if (need_reval_dot(dir)) { 2107 error = handle_dots(nd, nd->last_type);
2284 int status = d_revalidate(nd->path.dentry, nd); 2108 if (error)
2285 if (!status) 2109 return ERR_PTR(error);
2286 status = -ESTALE;
2287 if (status < 0) {
2288 error = status;
2289 goto exit;
2290 }
2291 }
2292 /* fallthrough */ 2110 /* fallthrough */
2293 case LAST_ROOT: 2111 case LAST_ROOT:
2294 goto exit; 2112 if (nd->flags & LOOKUP_RCU) {
2113 if (nameidata_drop_rcu_last(nd))
2114 return ERR_PTR(-ECHILD);
2115 }
2116 error = handle_reval_path(nd);
2117 if (error)
2118 goto exit;
2119 audit_inode(pathname, nd->path.dentry);
2120 if (open_flag & O_CREAT) {
2121 error = -EISDIR;
2122 goto exit;
2123 }
2124 goto ok;
2295 case LAST_BIND: 2125 case LAST_BIND:
2126 /* can't be RCU mode here */
2127 error = handle_reval_path(nd);
2128 if (error)
2129 goto exit;
2296 audit_inode(pathname, dir); 2130 audit_inode(pathname, dir);
2297 goto ok; 2131 goto ok;
2298 } 2132 }
2299 2133
2134 if (!(open_flag & O_CREAT)) {
2135 int symlink_ok = 0;
2136 if (nd->last.name[nd->last.len])
2137 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2138 if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2139 symlink_ok = 1;
2140 /* we _can_ be in RCU mode here */
2141 error = walk_component(nd, path, &nd->last, LAST_NORM,
2142 !symlink_ok);
2143 if (error < 0)
2144 return ERR_PTR(error);
2145 if (error) /* symlink */
2146 return NULL;
2147 /* sayonara */
2148 if (nd->flags & LOOKUP_RCU) {
2149 if (nameidata_drop_rcu_last(nd))
2150 return ERR_PTR(-ECHILD);
2151 }
2152
2153 error = -ENOTDIR;
2154 if (nd->flags & LOOKUP_DIRECTORY) {
2155 if (!nd->inode->i_op->lookup)
2156 goto exit;
2157 }
2158 audit_inode(pathname, nd->path.dentry);
2159 goto ok;
2160 }
2161
2162 /* create side of things */
2163
2164 if (nd->flags & LOOKUP_RCU) {
2165 if (nameidata_drop_rcu_last(nd))
2166 return ERR_PTR(-ECHILD);
2167 }
2168
2169 audit_inode(pathname, dir);
2170 error = -EISDIR;
2300 /* trailing slashes? */ 2171 /* trailing slashes? */
2301 if (nd->last.name[nd->last.len]) 2172 if (nd->last.name[nd->last.len])
2302 goto exit; 2173 goto exit;
2303 2174
2304 mutex_lock(&dir->d_inode->i_mutex); 2175 mutex_lock(&dir->d_inode->i_mutex);
2305 2176
2306 path->dentry = lookup_hash(nd); 2177 dentry = lookup_hash(nd);
2307 path->mnt = nd->path.mnt; 2178 error = PTR_ERR(dentry);
2308 2179 if (IS_ERR(dentry)) {
2309 error = PTR_ERR(path->dentry);
2310 if (IS_ERR(path->dentry)) {
2311 mutex_unlock(&dir->d_inode->i_mutex); 2180 mutex_unlock(&dir->d_inode->i_mutex);
2312 goto exit; 2181 goto exit;
2313 } 2182 }
2314 2183
2315 if (IS_ERR(nd->intent.open.file)) { 2184 path->dentry = dentry;
2316 error = PTR_ERR(nd->intent.open.file); 2185 path->mnt = nd->path.mnt;
2317 goto exit_mutex_unlock;
2318 }
2319 2186
2320 /* Negative dentry, just create the file */ 2187 /* Negative dentry, just create the file */
2321 if (!path->dentry->d_inode) { 2188 if (!dentry->d_inode) {
2189 int mode = op->mode;
2190 if (!IS_POSIXACL(dir->d_inode))
2191 mode &= ~current_umask();
2322 /* 2192 /*
2323 * This write is needed to ensure that a 2193 * This write is needed to ensure that a
2324 * ro->rw transition does not occur between 2194 * rw->ro transition does not occur between
2325 * the time when the file is created and when 2195 * the time when the file is created and when
2326 * a permanent write count is taken through 2196 * a permanent write count is taken through
2327 * the 'struct file' in nameidata_to_filp(). 2197 * the 'struct file' in nameidata_to_filp().
@@ -2329,22 +2199,21 @@ static struct file *do_last(struct nameidata *nd, struct path *path,
2329 error = mnt_want_write(nd->path.mnt); 2199 error = mnt_want_write(nd->path.mnt);
2330 if (error) 2200 if (error)
2331 goto exit_mutex_unlock; 2201 goto exit_mutex_unlock;
2332 error = __open_namei_create(nd, path, open_flag, mode); 2202 want_write = 1;
2333 if (error) { 2203 /* Don't check for write permission, don't truncate */
2334 mnt_drop_write(nd->path.mnt); 2204 open_flag &= ~O_TRUNC;
2335 goto exit; 2205 will_truncate = 0;
2336 } 2206 acc_mode = MAY_OPEN;
2337 filp = nameidata_to_filp(nd); 2207 error = security_path_mknod(&nd->path, dentry, mode, 0);
2338 mnt_drop_write(nd->path.mnt); 2208 if (error)
2339 path_put(&nd->path); 2209 goto exit_mutex_unlock;
2340 if (!IS_ERR(filp)) { 2210 error = vfs_create(dir->d_inode, dentry, mode, nd);
2341 error = ima_file_check(filp, acc_mode); 2211 if (error)
2342 if (error) { 2212 goto exit_mutex_unlock;
2343 fput(filp); 2213 mutex_unlock(&dir->d_inode->i_mutex);
2344 filp = ERR_PTR(error); 2214 dput(nd->path.dentry);
2345 } 2215 nd->path.dentry = dentry;
2346 } 2216 goto common;
2347 return filp;
2348 } 2217 }
2349 2218
2350 /* 2219 /*
@@ -2374,7 +2243,40 @@ static struct file *do_last(struct nameidata *nd, struct path *path,
2374 if (S_ISDIR(nd->inode->i_mode)) 2243 if (S_ISDIR(nd->inode->i_mode))
2375 goto exit; 2244 goto exit;
2376ok: 2245ok:
2377 filp = finish_open(nd, open_flag, acc_mode); 2246 if (!S_ISREG(nd->inode->i_mode))
2247 will_truncate = 0;
2248
2249 if (will_truncate) {
2250 error = mnt_want_write(nd->path.mnt);
2251 if (error)
2252 goto exit;
2253 want_write = 1;
2254 }
2255common:
2256 error = may_open(&nd->path, acc_mode, open_flag);
2257 if (error)
2258 goto exit;
2259 filp = nameidata_to_filp(nd);
2260 if (!IS_ERR(filp)) {
2261 error = ima_file_check(filp, op->acc_mode);
2262 if (error) {
2263 fput(filp);
2264 filp = ERR_PTR(error);
2265 }
2266 }
2267 if (!IS_ERR(filp)) {
2268 if (will_truncate) {
2269 error = handle_truncate(filp);
2270 if (error) {
2271 fput(filp);
2272 filp = ERR_PTR(error);
2273 }
2274 }
2275 }
2276out:
2277 if (want_write)
2278 mnt_drop_write(nd->path.mnt);
2279 path_put(&nd->path);
2378 return filp; 2280 return filp;
2379 2281
2380exit_mutex_unlock: 2282exit_mutex_unlock:
@@ -2382,197 +2284,103 @@ exit_mutex_unlock:
2382exit_dput: 2284exit_dput:
2383 path_put_conditional(path, nd); 2285 path_put_conditional(path, nd);
2384exit: 2286exit:
2385 path_put(&nd->path); 2287 filp = ERR_PTR(error);
2386 return ERR_PTR(error); 2288 goto out;
2387} 2289}
2388 2290
2389/* 2291static struct file *path_openat(int dfd, const char *pathname,
2390 * Note that the low bits of the passed in "open_flag" 2292 struct nameidata *nd, const struct open_flags *op, int flags)
2391 * are not the same as in the local variable "flag". See
2392 * open_to_namei_flags() for more details.
2393 */
2394struct file *do_filp_open(int dfd, const char *pathname,
2395 int open_flag, int mode, int acc_mode)
2396{ 2293{
2294 struct file *base = NULL;
2397 struct file *filp; 2295 struct file *filp;
2398 struct nameidata nd;
2399 int error;
2400 struct path path; 2296 struct path path;
2401 int count = 0; 2297 int error;
2402 int flag = open_to_namei_flags(open_flag);
2403 int flags;
2404
2405 if (!(open_flag & O_CREAT))
2406 mode = 0;
2407
2408 /* Must never be set by userspace */
2409 open_flag &= ~FMODE_NONOTIFY;
2410
2411 /*
2412 * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
2413 * check for O_DSYNC if the need any syncing at all we enforce it's
2414 * always set instead of having to deal with possibly weird behaviour
2415 * for malicious applications setting only __O_SYNC.
2416 */
2417 if (open_flag & __O_SYNC)
2418 open_flag |= O_DSYNC;
2419
2420 if (!acc_mode)
2421 acc_mode = MAY_OPEN | ACC_MODE(open_flag);
2422
2423 /* O_TRUNC implies we need access checks for write permissions */
2424 if (open_flag & O_TRUNC)
2425 acc_mode |= MAY_WRITE;
2426
2427 /* Allow the LSM permission hook to distinguish append
2428 access from general write access. */
2429 if (open_flag & O_APPEND)
2430 acc_mode |= MAY_APPEND;
2431
2432 flags = LOOKUP_OPEN;
2433 if (open_flag & O_CREAT) {
2434 flags |= LOOKUP_CREATE;
2435 if (open_flag & O_EXCL)
2436 flags |= LOOKUP_EXCL;
2437 }
2438 if (open_flag & O_DIRECTORY)
2439 flags |= LOOKUP_DIRECTORY;
2440 if (!(open_flag & O_NOFOLLOW))
2441 flags |= LOOKUP_FOLLOW;
2442 2298
2443 filp = get_empty_filp(); 2299 filp = get_empty_filp();
2444 if (!filp) 2300 if (!filp)
2445 return ERR_PTR(-ENFILE); 2301 return ERR_PTR(-ENFILE);
2446 2302
2447 filp->f_flags = open_flag; 2303 filp->f_flags = op->open_flag;
2448 nd.intent.open.file = filp; 2304 nd->intent.open.file = filp;
2449 nd.intent.open.flags = flag; 2305 nd->intent.open.flags = open_to_namei_flags(op->open_flag);
2450 nd.intent.open.create_mode = mode; 2306 nd->intent.open.create_mode = op->mode;
2451
2452 if (open_flag & O_CREAT)
2453 goto creat;
2454 2307
2455 /* !O_CREAT, simple open */ 2308 error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
2456 error = do_path_lookup(dfd, pathname, flags, &nd);
2457 if (unlikely(error)) 2309 if (unlikely(error))
2458 goto out_filp; 2310 goto out_filp;
2459 error = -ELOOP;
2460 if (!(nd.flags & LOOKUP_FOLLOW)) {
2461 if (nd.inode->i_op->follow_link)
2462 goto out_path;
2463 }
2464 error = -ENOTDIR;
2465 if (nd.flags & LOOKUP_DIRECTORY) {
2466 if (!nd.inode->i_op->lookup)
2467 goto out_path;
2468 }
2469 audit_inode(pathname, nd.path.dentry);
2470 filp = finish_open(&nd, open_flag, acc_mode);
2471 release_open_intent(&nd);
2472 return filp;
2473 2311
2474creat: 2312 current->total_link_count = 0;
2475 /* OK, have to create the file. Find the parent. */ 2313 error = link_path_walk(pathname, nd);
2476 error = path_init_rcu(dfd, pathname,
2477 LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
2478 if (error)
2479 goto out_filp;
2480 error = path_walk_rcu(pathname, &nd);
2481 path_finish_rcu(&nd);
2482 if (unlikely(error == -ECHILD || error == -ESTALE)) {
2483 /* slower, locked walk */
2484 if (error == -ESTALE) {
2485reval:
2486 flags |= LOOKUP_REVAL;
2487 }
2488 error = path_init(dfd, pathname,
2489 LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
2490 if (error)
2491 goto out_filp;
2492
2493 error = path_walk_simple(pathname, &nd);
2494 }
2495 if (unlikely(error)) 2314 if (unlikely(error))
2496 goto out_filp; 2315 goto out_filp;
2497 if (unlikely(!audit_dummy_context()))
2498 audit_inode(pathname, nd.path.dentry);
2499 2316
2500 /* 2317 filp = do_last(nd, &path, op, pathname);
2501 * We have the parent and last component.
2502 */
2503 nd.flags = flags;
2504 filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
2505 while (unlikely(!filp)) { /* trailing symlink */ 2318 while (unlikely(!filp)) { /* trailing symlink */
2506 struct path link = path; 2319 struct path link = path;
2507 struct inode *linki = link.dentry->d_inode;
2508 void *cookie; 2320 void *cookie;
2509 error = -ELOOP; 2321 if (!(nd->flags & LOOKUP_FOLLOW)) {
2510 if (!(nd.flags & LOOKUP_FOLLOW)) 2322 path_put_conditional(&path, nd);
2511 goto exit_dput; 2323 path_put(&nd->path);
2512 if (count++ == 32) 2324 filp = ERR_PTR(-ELOOP);
2513 goto exit_dput; 2325 break;
2514 /*
2515 * This is subtle. Instead of calling do_follow_link() we do
2516 * the thing by hands. The reason is that this way we have zero
2517 * link_count and path_walk() (called from ->follow_link)
2518 * honoring LOOKUP_PARENT. After that we have the parent and
2519 * last component, i.e. we are in the same situation as after
2520 * the first path_walk(). Well, almost - if the last component
2521 * is normal we get its copy stored in nd->last.name and we will
2522 * have to putname() it when we are done. Procfs-like symlinks
2523 * just set LAST_BIND.
2524 */
2525 nd.flags |= LOOKUP_PARENT;
2526 error = security_inode_follow_link(link.dentry, &nd);
2527 if (error)
2528 goto exit_dput;
2529 error = __do_follow_link(&link, &nd, &cookie);
2530 if (unlikely(error)) {
2531 if (!IS_ERR(cookie) && linki->i_op->put_link)
2532 linki->i_op->put_link(link.dentry, &nd, cookie);
2533 /* nd.path had been dropped */
2534 nd.path = link;
2535 goto out_path;
2536 } 2326 }
2537 nd.flags &= ~LOOKUP_PARENT; 2327 nd->flags |= LOOKUP_PARENT;
2538 filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname); 2328 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2539 if (linki->i_op->put_link) 2329 error = follow_link(&link, nd, &cookie);
2540 linki->i_op->put_link(link.dentry, &nd, cookie); 2330 if (unlikely(error))
2541 path_put(&link); 2331 filp = ERR_PTR(error);
2332 else
2333 filp = do_last(nd, &path, op, pathname);
2334 put_link(nd, &link, cookie);
2542 } 2335 }
2543out: 2336out:
2544 if (nd.root.mnt) 2337 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
2545 path_put(&nd.root); 2338 path_put(&nd->root);
2546 if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL)) 2339 if (base)
2547 goto reval; 2340 fput(base);
2548 release_open_intent(&nd); 2341 release_open_intent(nd);
2549 return filp; 2342 return filp;
2550 2343
2551exit_dput:
2552 path_put_conditional(&path, &nd);
2553out_path:
2554 path_put(&nd.path);
2555out_filp: 2344out_filp:
2556 filp = ERR_PTR(error); 2345 filp = ERR_PTR(error);
2557 goto out; 2346 goto out;
2558} 2347}
2559 2348
2560/** 2349struct file *do_filp_open(int dfd, const char *pathname,
2561 * filp_open - open file and return file pointer 2350 const struct open_flags *op, int flags)
2562 * 2351{
2563 * @filename: path to open 2352 struct nameidata nd;
2564 * @flags: open flags as per the open(2) second argument 2353 struct file *filp;
2565 * @mode: mode for the new file if O_CREAT is set, else ignored 2354
2566 * 2355 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
2567 * This is the helper to open a file from kernelspace if you really 2356 if (unlikely(filp == ERR_PTR(-ECHILD)))
2568 * have to. But in generally you should not do this, so please move 2357 filp = path_openat(dfd, pathname, &nd, op, flags);
2569 * along, nothing to see here.. 2358 if (unlikely(filp == ERR_PTR(-ESTALE)))
2570 */ 2359 filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
2571struct file *filp_open(const char *filename, int flags, int mode) 2360 return filp;
2361}
2362
2363struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
2364 const char *name, const struct open_flags *op, int flags)
2572{ 2365{
2573 return do_filp_open(AT_FDCWD, filename, flags, mode, 0); 2366 struct nameidata nd;
2367 struct file *file;
2368
2369 nd.root.mnt = mnt;
2370 nd.root.dentry = dentry;
2371
2372 flags |= LOOKUP_ROOT;
2373
2374 if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
2375 return ERR_PTR(-ELOOP);
2376
2377 file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
2378 if (unlikely(file == ERR_PTR(-ECHILD)))
2379 file = path_openat(-1, name, &nd, op, flags);
2380 if (unlikely(file == ERR_PTR(-ESTALE)))
2381 file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
2382 return file;
2574} 2383}
2575EXPORT_SYMBOL(filp_open);
2576 2384
2577/** 2385/**
2578 * lookup_create - lookup a dentry, creating it if it doesn't exist 2386 * lookup_create - lookup a dentry, creating it if it doesn't exist
@@ -3111,7 +2919,11 @@ int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_de
3111 return error; 2919 return error;
3112 2920
3113 mutex_lock(&inode->i_mutex); 2921 mutex_lock(&inode->i_mutex);
3114 error = dir->i_op->link(old_dentry, dir, new_dentry); 2922 /* Make sure we don't allow creating hardlink to an unlinked file */
2923 if (inode->i_nlink == 0)
2924 error = -ENOENT;
2925 else
2926 error = dir->i_op->link(old_dentry, dir, new_dentry);
3115 mutex_unlock(&inode->i_mutex); 2927 mutex_unlock(&inode->i_mutex);
3116 if (!error) 2928 if (!error)
3117 fsnotify_link(dir, inode, new_dentry); 2929 fsnotify_link(dir, inode, new_dentry);
@@ -3133,15 +2945,27 @@ SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
3133 struct dentry *new_dentry; 2945 struct dentry *new_dentry;
3134 struct nameidata nd; 2946 struct nameidata nd;
3135 struct path old_path; 2947 struct path old_path;
2948 int how = 0;
3136 int error; 2949 int error;
3137 char *to; 2950 char *to;
3138 2951
3139 if ((flags & ~AT_SYMLINK_FOLLOW) != 0) 2952 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3140 return -EINVAL; 2953 return -EINVAL;
2954 /*
2955 * To use null names we require CAP_DAC_READ_SEARCH
2956 * This ensures that not everyone will be able to create
2957 * handlink using the passed filedescriptor.
2958 */
2959 if (flags & AT_EMPTY_PATH) {
2960 if (!capable(CAP_DAC_READ_SEARCH))
2961 return -ENOENT;
2962 how = LOOKUP_EMPTY;
2963 }
2964
2965 if (flags & AT_SYMLINK_FOLLOW)
2966 how |= LOOKUP_FOLLOW;
3141 2967
3142 error = user_path_at(olddfd, oldname, 2968 error = user_path_at(olddfd, oldname, how, &old_path);
3143 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
3144 &old_path);
3145 if (error) 2969 if (error)
3146 return error; 2970 return error;
3147 2971
@@ -3578,7 +3402,7 @@ EXPORT_SYMBOL(page_readlink);
3578EXPORT_SYMBOL(__page_symlink); 3402EXPORT_SYMBOL(__page_symlink);
3579EXPORT_SYMBOL(page_symlink); 3403EXPORT_SYMBOL(page_symlink);
3580EXPORT_SYMBOL(page_symlink_inode_operations); 3404EXPORT_SYMBOL(page_symlink_inode_operations);
3581EXPORT_SYMBOL(path_lookup); 3405EXPORT_SYMBOL(kern_path_parent);
3582EXPORT_SYMBOL(kern_path); 3406EXPORT_SYMBOL(kern_path);
3583EXPORT_SYMBOL(vfs_path_lookup); 3407EXPORT_SYMBOL(vfs_path_lookup);
3584EXPORT_SYMBOL(inode_permission); 3408EXPORT_SYMBOL(inode_permission);