~ubuntu-branches/debian/wheezy/linux-2.6/wheezy

« back to all changes in this revision

Viewing changes to fs/namei.c

  • Committer: Bazaar Package Importer
  • Author(s): Ben Hutchings, Ben Hutchings, Aurelien Jarno
  • Date: 2011-06-07 12:14:05 UTC
  • mfrom: (43.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20110607121405-i3h1rd7nrnd2b73h
Tags: 2.6.39-2
[ Ben Hutchings ]
* [x86] Enable BACKLIGHT_APPLE, replacing BACKLIGHT_MBP_NVIDIA
  (Closes: #627492)
* cgroups: Disable memory resource controller by default. Allow it
  to be enabled using kernel parameter 'cgroup_enable=memory'.
* rt2800usb: Enable support for more USB devices including
  Linksys WUSB600N (Closes: #596626) (this change was accidentally
  omitted from 2.6.39-1)
* [x86] Remove Celeron from list of processors supporting PAE. Most
  'Celeron M' models do not.
* Update debconf template translations:
  - Swedish (Martin Bagge) (Closes: #628932)
  - French (David Prévot) (Closes: #628191)
* aufs: Update for 2.6.39 (Closes: #627837)
* Add stable 2.6.39.1, including:
  - ext4: dont set PageUptodate in ext4_end_bio()
  - pata_cmd64x: fix boot crash on parisc (Closes: #622997, #622745)
  - ext3: Fix fs corruption when make_indexed_dir() fails
  - netfilter: nf_ct_sip: validate Content-Length in TCP SIP messages
  - sctp: fix race between sctp_bind_addr_free() and
    sctp_bind_addr_conflict()
  - sctp: fix memory leak of the ASCONF queue when free asoc
  - md/bitmap: fix saving of events_cleared and other state
  - cdc_acm: Fix oops when Droids MuIn LCD is connected
  - cx88: Fix conversion from BKL to fine-grained locks (Closes: #619827)
  - keys: Set cred->user_ns in key_replace_session_keyring (CVE-2011-2184)
  - tmpfs: fix race between truncate and writepage
  - nfs41: Correct offset for LAYOUTCOMMIT
  - xen/mmu: fix a race window causing leave_mm BUG()
  - ext4: fix possible use-after-free in ext4_remove_li_request()
  For the complete list of changes, see:
   http://www.kernel.org/pub/linux/kernel/v2.6/ChangeLog-2.6.39.1
* Bump ABI to 2
* netfilter: Enable IP_SET, IP_SET_BITMAP_IP, IP_SET_BITMAP_IPMAC,
  IP_SET_BITMAP_PORT, IP_SET_HASH_IP, IP_SET_HASH_IPPORT,
  IP_SET_HASH_IPPORTIP, IP_SET_HASH_IPPORTNET, IP_SET_HASH_NET,
  IP_SET_HASH_NETPORT, IP_SET_LIST_SET, NETFILTER_XT_SET as modules
  (Closes: #629401)

[ Aurelien Jarno ]
* [mipsel/loongson-2f] Disable_SCSI_LPFC to workaround GCC ICE.

Show diffs side-by-side

added added

removed removed

Lines of Context:
70
70
 * name indicated by the symlink. The old code always complained that the
71
71
 * name already exists, due to not following the symlink even if its target
72
72
 * is nonexistent.  The new semantics affects also mknod() and link() when
73
 
 * the name is a symlink pointing to a non-existant name.
 
73
 * the name is a symlink pointing to a non-existent name.
74
74
 *
75
75
 * I don't know which semantics is the right one, since I have no access
76
76
 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
136
136
        return retval;
137
137
}
138
138
 
139
 
char * getname(const char __user * filename)
 
139
static char *getname_flags(const char __user * filename, int flags)
140
140
{
141
141
        char *tmp, *result;
142
142
 
147
147
 
148
148
                result = tmp;
149
149
                if (retval < 0) {
150
 
                        __putname(tmp);
151
 
                        result = ERR_PTR(retval);
 
150
                        if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
 
151
                                __putname(tmp);
 
152
                                result = ERR_PTR(retval);
 
153
                        }
152
154
                }
153
155
        }
154
156
        audit_getname(result);
155
157
        return result;
156
158
}
157
159
 
 
160
char *getname(const char __user * filename)
 
161
{
 
162
        return getname_flags(filename, 0);
 
163
}
 
164
 
158
165
#ifdef CONFIG_AUDITSYSCALL
159
166
void putname(const char *name)
160
167
{
172
179
static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
173
180
                int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
174
181
{
175
 
        umode_t                 mode = inode->i_mode;
 
182
        unsigned int mode = inode->i_mode;
176
183
 
177
184
        mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
178
185
 
 
186
        if (current_user_ns() != inode_userns(inode))
 
187
                goto other_perms;
 
188
 
179
189
        if (current_fsuid() == inode->i_uid)
180
190
                mode >>= 6;
181
191
        else {
189
199
                        mode >>= 3;
190
200
        }
191
201
 
 
202
other_perms:
192
203
        /*
193
204
         * If the DACs are ok we don't need any capability check.
194
205
         */
230
241
         * Executable DACs are overridable if at least one exec bit is set.
231
242
         */
232
243
        if (!(mask & MAY_EXEC) || execute_ok(inode))
233
 
                if (capable(CAP_DAC_OVERRIDE))
 
244
                if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
234
245
                        return 0;
235
246
 
236
247
        /*
238
249
         */
239
250
        mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
240
251
        if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
241
 
                if (capable(CAP_DAC_READ_SEARCH))
 
252
                if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
242
253
                        return 0;
243
254
 
244
255
        return -EACCES;
401
412
{
402
413
        struct fs_struct *fs = current->fs;
403
414
        struct dentry *dentry = nd->path.dentry;
 
415
        int want_root = 0;
404
416
 
405
417
        BUG_ON(!(nd->flags & LOOKUP_RCU));
406
 
        if (nd->root.mnt) {
 
418
        if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
 
419
                want_root = 1;
407
420
                spin_lock(&fs->lock);
408
421
                if (nd->root.mnt != fs->root.mnt ||
409
422
                                nd->root.dentry != fs->root.dentry)
414
427
                goto err;
415
428
        BUG_ON(nd->inode != dentry->d_inode);
416
429
        spin_unlock(&dentry->d_lock);
417
 
        if (nd->root.mnt) {
 
430
        if (want_root) {
418
431
                path_get(&nd->root);
419
432
                spin_unlock(&fs->lock);
420
433
        }
427
440
err:
428
441
        spin_unlock(&dentry->d_lock);
429
442
err_root:
430
 
        if (nd->root.mnt)
 
443
        if (want_root)
431
444
                spin_unlock(&fs->lock);
432
445
        return -ECHILD;
433
446
}
454
467
{
455
468
        struct fs_struct *fs = current->fs;
456
469
        struct dentry *parent = nd->path.dentry;
 
470
        int want_root = 0;
457
471
 
458
472
        BUG_ON(!(nd->flags & LOOKUP_RCU));
459
 
        if (nd->root.mnt) {
 
473
        if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
 
474
                want_root = 1;
460
475
                spin_lock(&fs->lock);
461
476
                if (nd->root.mnt != fs->root.mnt ||
462
477
                                nd->root.dentry != fs->root.dentry)
476
491
        parent->d_count++;
477
492
        spin_unlock(&dentry->d_lock);
478
493
        spin_unlock(&parent->d_lock);
479
 
        if (nd->root.mnt) {
 
494
        if (want_root) {
480
495
                path_get(&nd->root);
481
496
                spin_unlock(&fs->lock);
482
497
        }
490
505
        spin_unlock(&dentry->d_lock);
491
506
        spin_unlock(&parent->d_lock);
492
507
err_root:
493
 
        if (nd->root.mnt)
 
508
        if (want_root)
494
509
                spin_unlock(&fs->lock);
495
510
        return -ECHILD;
496
511
}
498
513
/* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
499
514
static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
500
515
{
501
 
        if (nd->flags & LOOKUP_RCU)
502
 
                return nameidata_dentry_drop_rcu(nd, dentry);
 
516
        if (nd->flags & LOOKUP_RCU) {
 
517
                if (unlikely(nameidata_dentry_drop_rcu(nd, dentry))) {
 
518
                        nd->flags &= ~LOOKUP_RCU;
 
519
                        if (!(nd->flags & LOOKUP_ROOT))
 
520
                                nd->root.mnt = NULL;
 
521
                        rcu_read_unlock();
 
522
                        br_read_unlock(vfsmount_lock);
 
523
                        return -ECHILD;
 
524
                }
 
525
        }
503
526
        return 0;
504
527
}
505
528
 
518
541
 
519
542
        BUG_ON(!(nd->flags & LOOKUP_RCU));
520
543
        nd->flags &= ~LOOKUP_RCU;
521
 
        nd->root.mnt = NULL;
 
544
        if (!(nd->flags & LOOKUP_ROOT))
 
545
                nd->root.mnt = NULL;
522
546
        spin_lock(&dentry->d_lock);
523
547
        if (!__d_rcu_to_refcount(dentry, nd->seq))
524
548
                goto err_unlock;
539
563
        return -ECHILD;
540
564
}
541
565
 
542
 
/* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
543
 
static 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
566
/**
551
567
 * release_open_intent - free up open intent resources
552
568
 * @nd: pointer to nameidata
590
606
        return dentry;
591
607
}
592
608
 
593
 
static inline struct dentry *
594
 
do_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
 
 
616
 
static 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
609
/*
628
 
 * force_reval_path - force revalidation of a dentry
 
610
 * handle_reval_path - force revalidation of a dentry
629
611
 *
630
612
 * In some situations the path walking code will trust dentries without
631
613
 * revalidating them. This causes problems for filesystems that depend on
639
621
 * invalidate the dentry. It's up to the caller to handle putting references
640
622
 * to the path if necessary.
641
623
 */
642
 
static int
643
 
force_reval_path(struct path *path, struct nameidata *nd)
 
624
static inline int handle_reval_path(struct nameidata *nd)
644
625
{
 
626
        struct dentry *dentry = nd->path.dentry;
645
627
        int status;
646
 
        struct dentry *dentry = path->dentry;
647
 
 
648
 
        /*
649
 
         * only check on filesystems where it's possible for the dentry to
650
 
         * become stale.
651
 
         */
652
 
        if (!need_reval_dot(dentry))
653
 
                return 0;
654
 
 
 
628
 
 
629
        if (likely(!(nd->flags & LOOKUP_JUMPED)))
 
630
                return 0;
 
631
 
 
632
        if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
 
633
                return 0;
 
634
 
 
635
        if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
 
636
                return 0;
 
637
 
 
638
        /* Note: we do not d_invalidate() */
655
639
        status = d_revalidate(dentry, nd);
656
640
        if (status > 0)
657
641
                return 0;
658
642
 
659
 
        if (!status) {
660
 
                d_invalidate(dentry);
 
643
        if (!status)
661
644
                status = -ESTALE;
662
 
        }
 
645
 
663
646
        return status;
664
647
}
665
648
 
675
658
static inline int exec_permission(struct inode *inode, unsigned int flags)
676
659
{
677
660
        int ret;
 
661
        struct user_namespace *ns = inode_userns(inode);
678
662
 
679
663
        if (inode->i_op->permission) {
680
664
                ret = inode->i_op->permission(inode, MAY_EXEC, flags);
687
671
        if (ret == -ECHILD)
688
672
                return ret;
689
673
 
690
 
        if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
 
674
        if (ns_capable(ns, CAP_DAC_OVERRIDE) ||
 
675
                        ns_capable(ns, CAP_DAC_READ_SEARCH))
691
676
                goto ok;
692
677
 
693
678
        return ret;
712
697
                do {
713
698
                        seq = read_seqcount_begin(&fs->seq);
714
699
                        nd->root = fs->root;
 
700
                        nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
715
701
                } while (read_seqcount_retry(&fs->seq, seq));
716
702
        }
717
703
}
728
714
                path_put(&nd->path);
729
715
                nd->path = nd->root;
730
716
                path_get(&nd->root);
 
717
                nd->flags |= LOOKUP_JUMPED;
731
718
        }
732
719
        nd->inode = nd->path.dentry->d_inode;
733
720
 
757
744
        nd->path.dentry = path->dentry;
758
745
}
759
746
 
 
747
static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
 
748
{
 
749
        struct inode *inode = link->dentry->d_inode;
 
750
        if (!IS_ERR(cookie) && inode->i_op->put_link)
 
751
                inode->i_op->put_link(link->dentry, nd, cookie);
 
752
        path_put(link);
 
753
}
 
754
 
760
755
static __always_inline int
761
 
__do_follow_link(const struct path *link, struct nameidata *nd, void **p)
 
756
follow_link(struct path *link, struct nameidata *nd, void **p)
762
757
{
763
758
        int error;
764
759
        struct dentry *dentry = link->dentry;
765
760
 
766
761
        BUG_ON(nd->flags & LOOKUP_RCU);
767
762
 
 
763
        if (link->mnt == nd->path.mnt)
 
764
                mntget(link->mnt);
 
765
 
 
766
        if (unlikely(current->total_link_count >= 40)) {
 
767
                *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
 
768
                path_put(&nd->path);
 
769
                return -ELOOP;
 
770
        }
 
771
        cond_resched();
 
772
        current->total_link_count++;
 
773
 
768
774
        touch_atime(link->mnt, dentry);
769
775
        nd_set_link(nd, NULL);
770
776
 
771
 
        if (link->mnt == nd->path.mnt)
772
 
                mntget(link->mnt);
 
777
        error = security_inode_follow_link(link->dentry, nd);
 
778
        if (error) {
 
779
                *p = ERR_PTR(error); /* no ->put_link(), please */
 
780
                path_put(&nd->path);
 
781
                return error;
 
782
        }
773
783
 
774
784
        nd->last_type = LAST_BIND;
775
785
        *p = dentry->d_inode->i_op->follow_link(dentry, nd);
780
790
                if (s)
781
791
                        error = __vfs_follow_link(nd, s);
782
792
                else if (nd->last_type == LAST_BIND) {
783
 
                        error = force_reval_path(&nd->path, nd);
784
 
                        if (error)
 
793
                        nd->flags |= LOOKUP_JUMPED;
 
794
                        nd->inode = nd->path.dentry->d_inode;
 
795
                        if (nd->inode->i_op->follow_link) {
 
796
                                /* stepped on a _really_ weird one */
785
797
                                path_put(&nd->path);
 
798
                                error = -ELOOP;
 
799
                        }
786
800
                }
787
801
        }
788
802
        return error;
789
803
}
790
804
 
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
 
 */
798
 
static 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;
827
 
loop:
828
 
        path_put_conditional(path, nd);
829
 
        path_put(&nd->path);
830
 
        return err;
831
 
}
832
 
 
833
805
static int follow_up_rcu(struct path *path)
834
806
{
835
807
        struct vfsmount *parent;
968
940
                if (managed & DCACHE_MANAGE_TRANSIT) {
969
941
                        BUG_ON(!path->dentry->d_op);
970
942
                        BUG_ON(!path->dentry->d_op->d_manage);
971
 
                        ret = path->dentry->d_op->d_manage(path->dentry,
972
 
                                                           false, false);
 
943
                        ret = path->dentry->d_op->d_manage(path->dentry, false);
973
944
                        if (ret < 0)
974
945
                                return ret == -EISDIR ? 0 : ret;
975
946
                }
1022
993
        return 0;
1023
994
}
1024
995
 
 
996
static inline bool managed_dentry_might_block(struct dentry *dentry)
 
997
{
 
998
        return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
 
999
                dentry->d_op->d_manage(dentry, true) < 0);
 
1000
}
 
1001
 
1025
1002
/*
1026
1003
 * Skip to top of mountpoint pile in rcuwalk mode.  We abort the rcu-walk if we
1027
1004
 * meet a managed dentry and we're not walking to "..".  True is returned to
1030
1007
static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1031
1008
                               struct inode **inode, bool reverse_transit)
1032
1009
{
1033
 
        while (d_mountpoint(path->dentry)) {
 
1010
        for (;;) {
1034
1011
                struct vfsmount *mounted;
1035
 
                if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
1036
 
                    !reverse_transit &&
1037
 
                    path->dentry->d_op->d_manage(path->dentry, false, true) < 0)
 
1012
                /*
 
1013
                 * Don't forget we might have a non-mountpoint managed dentry
 
1014
                 * that wants to block transit.
 
1015
                 */
 
1016
                *inode = path->dentry->d_inode;
 
1017
                if (!reverse_transit &&
 
1018
                     unlikely(managed_dentry_might_block(path->dentry)))
1038
1019
                        return false;
 
1020
 
 
1021
                if (!d_mountpoint(path->dentry))
 
1022
                        break;
 
1023
 
1039
1024
                mounted = __lookup_mnt(path->mnt, path->dentry, 1);
1040
1025
                if (!mounted)
1041
1026
                        break;
1042
1027
                path->mnt = mounted;
1043
1028
                path->dentry = mounted->mnt_root;
1044
1029
                nd->seq = read_seqcount_begin(&path->dentry->d_seq);
1045
 
                *inode = path->dentry->d_inode;
1046
1030
        }
1047
1031
 
1048
1032
        if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1068
1052
 
1069
1053
                        seq = read_seqcount_begin(&parent->d_seq);
1070
1054
                        if (read_seqcount_retry(&old->d_seq, nd->seq))
1071
 
                                return -ECHILD;
 
1055
                                goto failed;
1072
1056
                        inode = parent->d_inode;
1073
1057
                        nd->path.dentry = parent;
1074
1058
                        nd->seq = seq;
1081
1065
        }
1082
1066
        __follow_mount_rcu(nd, &nd->path, &inode, true);
1083
1067
        nd->inode = inode;
1084
 
 
1085
1068
        return 0;
 
1069
 
 
1070
failed:
 
1071
        nd->flags &= ~LOOKUP_RCU;
 
1072
        if (!(nd->flags & LOOKUP_ROOT))
 
1073
                nd->root.mnt = NULL;
 
1074
        rcu_read_unlock();
 
1075
        br_read_unlock(vfsmount_lock);
 
1076
        return -ECHILD;
1086
1077
}
1087
1078
 
1088
1079
/*
1093
1084
 * Care must be taken as namespace_sem may be held (indicated by mounting_here
1094
1085
 * being true).
1095
1086
 */
1096
 
int follow_down(struct path *path, bool mounting_here)
 
1087
int follow_down(struct path *path)
1097
1088
{
1098
1089
        unsigned managed;
1099
1090
        int ret;
1114
1105
                        BUG_ON(!path->dentry->d_op);
1115
1106
                        BUG_ON(!path->dentry->d_op->d_manage);
1116
1107
                        ret = path->dentry->d_op->d_manage(
1117
 
                                path->dentry, mounting_here, false);
 
1108
                                path->dentry, false);
1118
1109
                        if (ret < 0)
1119
1110
                                return ret == -EISDIR ? 0 : ret;
1120
1111
                }
1216
1207
{
1217
1208
        struct vfsmount *mnt = nd->path.mnt;
1218
1209
        struct dentry *dentry, *parent = nd->path.dentry;
1219
 
        struct inode *dir;
 
1210
        int need_reval = 1;
 
1211
        int status = 1;
1220
1212
        int err;
1221
1213
 
1222
1214
        /*
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
1215
         * Rename seqlock is not required here because in the off chance
1234
1216
         * of a false negative due to a concurrent rename, we're going to
1235
1217
         * do the non-racy lookup, below.
1236
1218
         */
1237
1219
        if (nd->flags & LOOKUP_RCU) {
1238
1220
                unsigned seq;
1239
 
 
1240
1221
                *inode = nd->inode;
1241
1222
                dentry = __d_lookup_rcu(parent, name, &seq, inode);
1242
 
                if (!dentry) {
1243
 
                        if (nameidata_drop_rcu(nd))
1244
 
                                return -ECHILD;
1245
 
                        goto need_lookup;
1246
 
                }
 
1223
                if (!dentry)
 
1224
                        goto unlazy;
 
1225
 
1247
1226
                /* Memory barrier in read_seqcount_begin of child is enough */
1248
1227
                if (__read_seqcount_retry(&parent->d_seq, nd->seq))
1249
1228
                        return -ECHILD;
1250
 
 
1251
1229
                nd->seq = seq;
 
1230
 
1252
1231
                if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1253
 
                        dentry = do_revalidate_rcu(dentry, nd);
1254
 
                        if (!dentry)
1255
 
                                goto need_lookup;
1256
 
                        if (IS_ERR(dentry))
1257
 
                                goto fail;
1258
 
                        if (!(nd->flags & LOOKUP_RCU))
1259
 
                                goto done;
 
1232
                        status = d_revalidate(dentry, nd);
 
1233
                        if (unlikely(status <= 0)) {
 
1234
                                if (status != -ECHILD)
 
1235
                                        need_reval = 0;
 
1236
                                goto unlazy;
 
1237
                        }
1260
1238
                }
1261
1239
                path->mnt = mnt;
1262
1240
                path->dentry = dentry;
1263
1241
                if (likely(__follow_mount_rcu(nd, path, inode, false)))
1264
1242
                        return 0;
1265
 
                if (nameidata_drop_rcu(nd))
1266
 
                        return -ECHILD;
1267
 
                /* fallthru */
1268
 
        }
1269
 
        dentry = __d_lookup(parent, name);
1270
 
        if (!dentry)
1271
 
                goto need_lookup;
1272
 
found:
1273
 
        if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1274
 
                dentry = do_revalidate(dentry, nd);
1275
 
                if (!dentry)
1276
 
                        goto need_lookup;
1277
 
                if (IS_ERR(dentry))
1278
 
                        goto fail;
1279
 
        }
1280
 
done:
 
1243
unlazy:
 
1244
                if (dentry) {
 
1245
                        if (nameidata_dentry_drop_rcu(nd, dentry))
 
1246
                                return -ECHILD;
 
1247
                } else {
 
1248
                        if (nameidata_drop_rcu(nd))
 
1249
                                return -ECHILD;
 
1250
                }
 
1251
        } else {
 
1252
                dentry = __d_lookup(parent, name);
 
1253
        }
 
1254
 
 
1255
retry:
 
1256
        if (unlikely(!dentry)) {
 
1257
                struct inode *dir = parent->d_inode;
 
1258
                BUG_ON(nd->inode != dir);
 
1259
 
 
1260
                mutex_lock(&dir->i_mutex);
 
1261
                dentry = d_lookup(parent, name);
 
1262
                if (likely(!dentry)) {
 
1263
                        dentry = d_alloc_and_lookup(parent, name, nd);
 
1264
                        if (IS_ERR(dentry)) {
 
1265
                                mutex_unlock(&dir->i_mutex);
 
1266
                                return PTR_ERR(dentry);
 
1267
                        }
 
1268
                        /* known good */
 
1269
                        need_reval = 0;
 
1270
                        status = 1;
 
1271
                }
 
1272
                mutex_unlock(&dir->i_mutex);
 
1273
        }
 
1274
        if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
 
1275
                status = d_revalidate(dentry, nd);
 
1276
        if (unlikely(status <= 0)) {
 
1277
                if (status < 0) {
 
1278
                        dput(dentry);
 
1279
                        return status;
 
1280
                }
 
1281
                if (!d_invalidate(dentry)) {
 
1282
                        dput(dentry);
 
1283
                        dentry = NULL;
 
1284
                        need_reval = 1;
 
1285
                        goto retry;
 
1286
                }
 
1287
        }
 
1288
 
1281
1289
        path->mnt = mnt;
1282
1290
        path->dentry = dentry;
1283
1291
        err = follow_managed(path, nd->flags);
1287
1295
        }
1288
1296
        *inode = path->dentry->d_inode;
1289
1297
        return 0;
1290
 
 
1291
 
need_lookup:
1292
 
        dir = parent->d_inode;
1293
 
        BUG_ON(nd->inode != dir);
1294
 
 
1295
 
        mutex_lock(&dir->i_mutex);
1296
 
        /*
1297
 
         * First re-do the cached lookup just in case it was created
1298
 
         * while we waited for the directory semaphore, or the first
1299
 
         * lookup failed due to an unrelated rename.
1300
 
         *
1301
 
         * This could use version numbering or similar to avoid unnecessary
1302
 
         * cache lookups, but then we'd have to do the first lookup in the
1303
 
         * non-racy way. However in the common case here, everything should
1304
 
         * be hot in cache, so would it be a big win?
1305
 
         */
1306
 
        dentry = d_lookup(parent, name);
1307
 
        if (likely(!dentry)) {
1308
 
                dentry = d_alloc_and_lookup(parent, name, nd);
1309
 
                mutex_unlock(&dir->i_mutex);
1310
 
                if (IS_ERR(dentry))
1311
 
                        goto fail;
1312
 
                goto done;
1313
 
        }
1314
 
        /*
1315
 
         * Uhhuh! Nasty case: the cache was re-populated while
1316
 
         * we waited on the semaphore. Need to revalidate.
1317
 
         */
1318
 
        mutex_unlock(&dir->i_mutex);
1319
 
        goto found;
1320
 
 
1321
 
fail:
1322
 
        return PTR_ERR(dentry);
 
1298
}
 
1299
 
 
1300
static inline int may_lookup(struct nameidata *nd)
 
1301
{
 
1302
        if (nd->flags & LOOKUP_RCU) {
 
1303
                int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
 
1304
                if (err != -ECHILD)
 
1305
                        return err;
 
1306
                if (nameidata_drop_rcu(nd))
 
1307
                        return -ECHILD;
 
1308
        }
 
1309
        return exec_permission(nd->inode, 0);
 
1310
}
 
1311
 
 
1312
static inline int handle_dots(struct nameidata *nd, int type)
 
1313
{
 
1314
        if (type == LAST_DOTDOT) {
 
1315
                if (nd->flags & LOOKUP_RCU) {
 
1316
                        if (follow_dotdot_rcu(nd))
 
1317
                                return -ECHILD;
 
1318
                } else
 
1319
                        follow_dotdot(nd);
 
1320
        }
 
1321
        return 0;
 
1322
}
 
1323
 
 
1324
static void terminate_walk(struct nameidata *nd)
 
1325
{
 
1326
        if (!(nd->flags & LOOKUP_RCU)) {
 
1327
                path_put(&nd->path);
 
1328
        } else {
 
1329
                nd->flags &= ~LOOKUP_RCU;
 
1330
                if (!(nd->flags & LOOKUP_ROOT))
 
1331
                        nd->root.mnt = NULL;
 
1332
                rcu_read_unlock();
 
1333
                br_read_unlock(vfsmount_lock);
 
1334
        }
 
1335
}
 
1336
 
 
1337
static inline int walk_component(struct nameidata *nd, struct path *path,
 
1338
                struct qstr *name, int type, int follow)
 
1339
{
 
1340
        struct inode *inode;
 
1341
        int err;
 
1342
        /*
 
1343
         * "." and ".." are special - ".." especially so because it has
 
1344
         * to be able to know about the current root directory and
 
1345
         * parent relationships.
 
1346
         */
 
1347
        if (unlikely(type != LAST_NORM))
 
1348
                return handle_dots(nd, type);
 
1349
        err = do_lookup(nd, name, path, &inode);
 
1350
        if (unlikely(err)) {
 
1351
                terminate_walk(nd);
 
1352
                return err;
 
1353
        }
 
1354
        if (!inode) {
 
1355
                path_to_nameidata(path, nd);
 
1356
                terminate_walk(nd);
 
1357
                return -ENOENT;
 
1358
        }
 
1359
        if (unlikely(inode->i_op->follow_link) && follow) {
 
1360
                if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
 
1361
                        return -ECHILD;
 
1362
                BUG_ON(inode != path->dentry->d_inode);
 
1363
                return 1;
 
1364
        }
 
1365
        path_to_nameidata(path, nd);
 
1366
        nd->inode = inode;
 
1367
        return 0;
 
1368
}
 
1369
 
 
1370
/*
 
1371
 * This limits recursive symlink follows to 8, while
 
1372
 * limiting consecutive symlinks to 40.
 
1373
 *
 
1374
 * Without that kind of total limit, nasty chains of consecutive
 
1375
 * symlinks can cause almost arbitrarily long lookups.
 
1376
 */
 
1377
static inline int nested_symlink(struct path *path, struct nameidata *nd)
 
1378
{
 
1379
        int res;
 
1380
 
 
1381
        BUG_ON(nd->depth >= MAX_NESTED_LINKS);
 
1382
        if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
 
1383
                path_put_conditional(path, nd);
 
1384
                path_put(&nd->path);
 
1385
                return -ELOOP;
 
1386
        }
 
1387
 
 
1388
        nd->depth++;
 
1389
        current->link_count++;
 
1390
 
 
1391
        do {
 
1392
                struct path link = *path;
 
1393
                void *cookie;
 
1394
 
 
1395
                res = follow_link(&link, nd, &cookie);
 
1396
                if (!res)
 
1397
                        res = walk_component(nd, path, &nd->last,
 
1398
                                             nd->last_type, LOOKUP_FOLLOW);
 
1399
                put_link(nd, &link, cookie);
 
1400
        } while (res > 0);
 
1401
 
 
1402
        current->link_count--;
 
1403
        nd->depth--;
 
1404
        return res;
1323
1405
}
1324
1406
 
1325
1407
/*
1339
1421
        while (*name=='/')
1340
1422
                name++;
1341
1423
        if (!*name)
1342
 
                goto return_reval;
1343
 
 
1344
 
        if (nd->depth)
1345
 
                lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
 
1424
                return 0;
1346
1425
 
1347
1426
        /* At this point we know we have a real path component. */
1348
1427
        for(;;) {
1349
 
                struct inode *inode;
1350
1428
                unsigned long hash;
1351
1429
                struct qstr this;
1352
1430
                unsigned int c;
 
1431
                int type;
1353
1432
 
1354
1433
                nd->flags |= LOOKUP_CONTINUE;
1355
 
                if (nd->flags & LOOKUP_RCU) {
1356
 
                        err = exec_permission(nd->inode, IPERM_FLAG_RCU);
1357
 
                        if (err == -ECHILD) {
1358
 
                                if (nameidata_drop_rcu(nd))
1359
 
                                        return -ECHILD;
1360
 
                                goto exec_again;
1361
 
                        }
1362
 
                } else {
1363
 
exec_again:
1364
 
                        err = exec_permission(nd->inode, 0);
1365
 
                }
 
1434
 
 
1435
                err = may_lookup(nd);
1366
1436
                if (err)
1367
1437
                        break;
1368
1438
 
1378
1448
                this.len = name - (const char *) this.name;
1379
1449
                this.hash = end_name_hash(hash);
1380
1450
 
 
1451
                type = LAST_NORM;
 
1452
                if (this.name[0] == '.') switch (this.len) {
 
1453
                        case 2:
 
1454
                                if (this.name[1] == '.') {
 
1455
                                        type = LAST_DOTDOT;
 
1456
                                        nd->flags |= LOOKUP_JUMPED;
 
1457
                                }
 
1458
                                break;
 
1459
                        case 1:
 
1460
                                type = LAST_DOT;
 
1461
                }
 
1462
                if (likely(type == LAST_NORM)) {
 
1463
                        struct dentry *parent = nd->path.dentry;
 
1464
                        nd->flags &= ~LOOKUP_JUMPED;
 
1465
                        if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
 
1466
                                err = parent->d_op->d_hash(parent, nd->inode,
 
1467
                                                           &this);
 
1468
                                if (err < 0)
 
1469
                                        break;
 
1470
                        }
 
1471
                }
 
1472
 
1381
1473
                /* remove trailing slashes? */
1382
1474
                if (!c)
1383
1475
                        goto last_component;
1384
1476
                while (*++name == '/');
1385
1477
                if (!*name)
1386
 
                        goto last_with_slashes;
1387
 
 
1388
 
                /*
1389
 
                 * "." and ".." are special - ".." especially so because it has
1390
 
                 * to be able to know about the current root directory and
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
 
 
1416
 
                if (inode->i_op->follow_link) {
1417
 
                        err = do_follow_link(inode, &next, nd);
 
1478
                        goto last_component;
 
1479
 
 
1480
                err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
 
1481
                if (err < 0)
 
1482
                        return err;
 
1483
 
 
1484
                if (err) {
 
1485
                        err = nested_symlink(&next, nd);
1418
1486
                        if (err)
1419
 
                                goto 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;
 
1487
                                return err;
1427
1488
                }
1428
1489
                err = -ENOTDIR; 
1429
1490
                if (!nd->inode->i_op->lookup)
1431
1492
                continue;
1432
1493
                /* here ends the main loop */
1433
1494
 
1434
 
last_with_slashes:
1435
 
                lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1436
1495
last_component:
1437
1496
                /* Clear LOOKUP_CONTINUE iff it was previously unset */
1438
1497
                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;
1478
 
lookup_parent:
1479
1498
                nd->last = this;
1480
 
                nd->last_type = LAST_NORM;
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;
1489
 
return_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
 
                }
1505
 
return_base:
1506
 
                if (nameidata_drop_rcu_last_maybe(nd))
1507
 
                        return -ECHILD;
 
1499
                nd->last_type = type;
1508
1500
                return 0;
1509
 
out_dput:
1510
 
                if (!(nd->flags & LOOKUP_RCU))
1511
 
                        path_put_conditional(&next, nd);
1512
 
                break;
1513
1501
        }
1514
 
        if (!(nd->flags & LOOKUP_RCU))
1515
 
                path_put(&nd->path);
1516
 
return_err:
 
1502
        terminate_walk(nd);
1517
1503
        return err;
1518
1504
}
1519
1505
 
1520
 
static inline int path_walk_rcu(const char *name, struct nameidata *nd)
1521
 
{
1522
 
        current->total_link_count = 0;
1523
 
 
1524
 
        return link_path_walk(name, nd);
1525
 
}
1526
 
 
1527
 
static 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
 
 
1534
 
static 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
 
                nd->inode = save.dentry->d_inode;
1550
 
                path_get(&nd->path);
1551
 
                nd->flags |= LOOKUP_REVAL;
1552
 
                result = link_path_walk(name, nd);
1553
 
        }
1554
 
 
1555
 
        path_put(&save);
1556
 
 
1557
 
        return result;
1558
 
}
1559
 
 
1560
 
static void path_finish_rcu(struct nameidata *nd)
1561
 
{
1562
 
        if (nd->flags & LOOKUP_RCU) {
1563
 
                /* RCU dangling. Cancel it. */
1564
 
                nd->flags &= ~LOOKUP_RCU;
1565
 
                nd->root.mnt = NULL;
1566
 
                rcu_read_unlock();
1567
 
                br_read_unlock(vfsmount_lock);
1568
 
        }
1569
 
        if (nd->file)
1570
 
                fput(nd->file);
1571
 
}
1572
 
 
1573
 
static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
1574
 
{
1575
 
        int retval = 0;
1576
 
        int fput_needed;
1577
 
        struct file *file;
1578
 
 
1579
 
        nd->last_type = LAST_ROOT; /* if there are only slashes... */
1580
 
        nd->flags = flags | LOOKUP_RCU;
1581
 
        nd->depth = 0;
1582
 
        nd->root.mnt = NULL;
1583
 
        nd->file = NULL;
1584
 
 
1585
 
        if (*name=='/') {
1586
 
                struct fs_struct *fs = current->fs;
1587
 
                unsigned seq;
1588
 
 
1589
 
                br_read_lock(vfsmount_lock);
1590
 
                rcu_read_lock();
1591
 
 
1592
 
                do {
1593
 
                        seq = read_seqcount_begin(&fs->seq);
1594
 
                        nd->root = fs->root;
1595
 
                        nd->path = nd->root;
1596
 
                        nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1597
 
                } while (read_seqcount_retry(&fs->seq, seq));
1598
 
 
1599
 
        } else if (dfd == AT_FDCWD) {
1600
 
                struct fs_struct *fs = current->fs;
1601
 
                unsigned seq;
1602
 
 
1603
 
                br_read_lock(vfsmount_lock);
1604
 
                rcu_read_lock();
1605
 
 
1606
 
                do {
1607
 
                        seq = read_seqcount_begin(&fs->seq);
1608
 
                        nd->path = fs->pwd;
1609
 
                        nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1610
 
                } while (read_seqcount_retry(&fs->seq, seq));
1611
 
 
1612
 
        } else {
1613
 
                struct dentry *dentry;
1614
 
 
1615
 
                file = fget_light(dfd, &fput_needed);
1616
 
                retval = -EBADF;
1617
 
                if (!file)
1618
 
                        goto out_fail;
1619
 
 
1620
 
                dentry = file->f_path.dentry;
1621
 
 
1622
 
                retval = -ENOTDIR;
1623
 
                if (!S_ISDIR(dentry->d_inode->i_mode))
1624
 
                        goto fput_fail;
1625
 
 
1626
 
                retval = file_permission(file, MAY_EXEC);
1627
 
                if (retval)
1628
 
                        goto fput_fail;
1629
 
 
1630
 
                nd->path = file->f_path;
1631
 
                if (fput_needed)
1632
 
                        nd->file = file;
1633
 
 
1634
 
                nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1635
 
                br_read_lock(vfsmount_lock);
1636
 
                rcu_read_lock();
1637
 
        }
1638
 
        nd->inode = nd->path.dentry->d_inode;
1639
 
        return 0;
1640
 
 
1641
 
fput_fail:
1642
 
        fput_light(file, fput_needed);
1643
 
out_fail:
1644
 
        return retval;
1645
 
}
1646
 
 
1647
 
static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
1648
 
{
1649
 
        int retval = 0;
1650
 
        int fput_needed;
1651
 
        struct file *file;
1652
 
 
1653
 
        nd->last_type = LAST_ROOT; /* if there are only slashes... */
1654
 
        nd->flags = flags;
1655
 
        nd->depth = 0;
1656
 
        nd->root.mnt = NULL;
1657
 
 
1658
 
        if (*name=='/') {
1659
 
                set_root(nd);
1660
 
                nd->path = nd->root;
1661
 
                path_get(&nd->root);
1662
 
        } else if (dfd == AT_FDCWD) {
1663
 
                get_fs_pwd(current->fs, &nd->path);
1664
 
        } else {
1665
 
                struct dentry *dentry;
1666
 
 
1667
 
                file = fget_light(dfd, &fput_needed);
1668
 
                retval = -EBADF;
1669
 
                if (!file)
1670
 
                        goto out_fail;
1671
 
 
1672
 
                dentry = file->f_path.dentry;
1673
 
 
1674
 
                retval = -ENOTDIR;
1675
 
                if (!S_ISDIR(dentry->d_inode->i_mode))
1676
 
                        goto fput_fail;
1677
 
 
1678
 
                retval = file_permission(file, MAY_EXEC);
1679
 
                if (retval)
1680
 
                        goto fput_fail;
1681
 
 
1682
 
                nd->path = file->f_path;
1683
 
                path_get(&file->f_path);
1684
 
 
1685
 
                fput_light(file, fput_needed);
1686
 
        }
1687
 
        nd->inode = nd->path.dentry->d_inode;
1688
 
        return 0;
1689
 
 
1690
 
fput_fail:
1691
 
        fput_light(file, fput_needed);
1692
 
out_fail:
1693
 
        return retval;
 
1506
static int path_init(int dfd, const char *name, unsigned int flags,
 
1507
                     struct nameidata *nd, struct file **fp)
 
1508
{
 
1509
        int retval = 0;
 
1510
        int fput_needed;
 
1511
        struct file *file;
 
1512
 
 
1513
        nd->last_type = LAST_ROOT; /* if there are only slashes... */
 
1514
        nd->flags = flags | LOOKUP_JUMPED;
 
1515
        nd->depth = 0;
 
1516
        if (flags & LOOKUP_ROOT) {
 
1517
                struct inode *inode = nd->root.dentry->d_inode;
 
1518
                if (*name) {
 
1519
                        if (!inode->i_op->lookup)
 
1520
                                return -ENOTDIR;
 
1521
                        retval = inode_permission(inode, MAY_EXEC);
 
1522
                        if (retval)
 
1523
                                return retval;
 
1524
                }
 
1525
                nd->path = nd->root;
 
1526
                nd->inode = inode;
 
1527
                if (flags & LOOKUP_RCU) {
 
1528
                        br_read_lock(vfsmount_lock);
 
1529
                        rcu_read_lock();
 
1530
                        nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
 
1531
                } else {
 
1532
                        path_get(&nd->path);
 
1533
                }
 
1534
                return 0;
 
1535
        }
 
1536
 
 
1537
        nd->root.mnt = NULL;
 
1538
 
 
1539
        if (*name=='/') {
 
1540
                if (flags & LOOKUP_RCU) {
 
1541
                        br_read_lock(vfsmount_lock);
 
1542
                        rcu_read_lock();
 
1543
                        set_root_rcu(nd);
 
1544
                } else {
 
1545
                        set_root(nd);
 
1546
                        path_get(&nd->root);
 
1547
                }
 
1548
                nd->path = nd->root;
 
1549
        } else if (dfd == AT_FDCWD) {
 
1550
                if (flags & LOOKUP_RCU) {
 
1551
                        struct fs_struct *fs = current->fs;
 
1552
                        unsigned seq;
 
1553
 
 
1554
                        br_read_lock(vfsmount_lock);
 
1555
                        rcu_read_lock();
 
1556
 
 
1557
                        do {
 
1558
                                seq = read_seqcount_begin(&fs->seq);
 
1559
                                nd->path = fs->pwd;
 
1560
                                nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
 
1561
                        } while (read_seqcount_retry(&fs->seq, seq));
 
1562
                } else {
 
1563
                        get_fs_pwd(current->fs, &nd->path);
 
1564
                }
 
1565
        } else {
 
1566
                struct dentry *dentry;
 
1567
 
 
1568
                file = fget_raw_light(dfd, &fput_needed);
 
1569
                retval = -EBADF;
 
1570
                if (!file)
 
1571
                        goto out_fail;
 
1572
 
 
1573
                dentry = file->f_path.dentry;
 
1574
 
 
1575
                if (*name) {
 
1576
                        retval = -ENOTDIR;
 
1577
                        if (!S_ISDIR(dentry->d_inode->i_mode))
 
1578
                                goto fput_fail;
 
1579
 
 
1580
                        retval = file_permission(file, MAY_EXEC);
 
1581
                        if (retval)
 
1582
                                goto fput_fail;
 
1583
                }
 
1584
 
 
1585
                nd->path = file->f_path;
 
1586
                if (flags & LOOKUP_RCU) {
 
1587
                        if (fput_needed)
 
1588
                                *fp = file;
 
1589
                        nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
 
1590
                        br_read_lock(vfsmount_lock);
 
1591
                        rcu_read_lock();
 
1592
                } else {
 
1593
                        path_get(&file->f_path);
 
1594
                        fput_light(file, fput_needed);
 
1595
                }
 
1596
        }
 
1597
 
 
1598
        nd->inode = nd->path.dentry->d_inode;
 
1599
        return 0;
 
1600
 
 
1601
fput_fail:
 
1602
        fput_light(file, fput_needed);
 
1603
out_fail:
 
1604
        return retval;
 
1605
}
 
1606
 
 
1607
static inline int lookup_last(struct nameidata *nd, struct path *path)
 
1608
{
 
1609
        if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
 
1610
                nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
 
1611
 
 
1612
        nd->flags &= ~LOOKUP_PARENT;
 
1613
        return walk_component(nd, path, &nd->last, nd->last_type,
 
1614
                                        nd->flags & LOOKUP_FOLLOW);
1694
1615
}
1695
1616
 
1696
1617
/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1697
 
static int do_path_lookup(int dfd, const char *name,
 
1618
static int path_lookupat(int dfd, const char *name,
1698
1619
                                unsigned int flags, struct nameidata *nd)
1699
1620
{
1700
 
        int retval;
 
1621
        struct file *base = NULL;
 
1622
        struct path path;
 
1623
        int err;
1701
1624
 
1702
1625
        /*
1703
1626
         * Path walking is largely split up into 2 different synchronisation
1713
1636
         * be handled by restarting a traditional ref-walk (which will always
1714
1637
         * be able to complete).
1715
1638
         */
1716
 
        retval = path_init_rcu(dfd, name, flags, nd);
1717
 
        if (unlikely(retval))
1718
 
                return retval;
1719
 
        retval = path_walk_rcu(name, nd);
1720
 
        path_finish_rcu(nd);
1721
 
        if (nd->root.mnt) {
 
1639
        err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
 
1640
 
 
1641
        if (unlikely(err))
 
1642
                return err;
 
1643
 
 
1644
        current->total_link_count = 0;
 
1645
        err = link_path_walk(name, nd);
 
1646
 
 
1647
        if (!err && !(flags & LOOKUP_PARENT)) {
 
1648
                err = lookup_last(nd, &path);
 
1649
                while (err > 0) {
 
1650
                        void *cookie;
 
1651
                        struct path link = path;
 
1652
                        nd->flags |= LOOKUP_PARENT;
 
1653
                        err = follow_link(&link, nd, &cookie);
 
1654
                        if (!err)
 
1655
                                err = lookup_last(nd, &path);
 
1656
                        put_link(nd, &link, cookie);
 
1657
                }
 
1658
        }
 
1659
 
 
1660
        if (nd->flags & LOOKUP_RCU) {
 
1661
                /* went all way through without dropping RCU */
 
1662
                BUG_ON(err);
 
1663
                if (nameidata_drop_rcu_last(nd))
 
1664
                        err = -ECHILD;
 
1665
        }
 
1666
 
 
1667
        if (!err) {
 
1668
                err = handle_reval_path(nd);
 
1669
                if (err)
 
1670
                        path_put(&nd->path);
 
1671
        }
 
1672
 
 
1673
        if (!err && nd->flags & LOOKUP_DIRECTORY) {
 
1674
                if (!nd->inode->i_op->lookup) {
 
1675
                        path_put(&nd->path);
 
1676
                        err = -ENOTDIR;
 
1677
                }
 
1678
        }
 
1679
 
 
1680
        if (base)
 
1681
                fput(base);
 
1682
 
 
1683
        if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1722
1684
                path_put(&nd->root);
1723
1685
                nd->root.mnt = NULL;
1724
1686
        }
 
1687
        return err;
 
1688
}
1725
1689
 
1726
 
        if (unlikely(retval == -ECHILD || retval == -ESTALE)) {
1727
 
                /* slower, locked walk */
1728
 
                if (retval == -ESTALE)
1729
 
                        flags |= LOOKUP_REVAL;
1730
 
                retval = path_init(dfd, name, flags, nd);
1731
 
                if (unlikely(retval))
1732
 
                        return retval;
1733
 
                retval = path_walk(name, nd);
1734
 
                if (nd->root.mnt) {
1735
 
                        path_put(&nd->root);
1736
 
                        nd->root.mnt = NULL;
1737
 
                }
1738
 
        }
 
1690
static int do_path_lookup(int dfd, const char *name,
 
1691
                                unsigned int flags, struct nameidata *nd)
 
1692
{
 
1693
        int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
 
1694
        if (unlikely(retval == -ECHILD))
 
1695
                retval = path_lookupat(dfd, name, flags, nd);
 
1696
        if (unlikely(retval == -ESTALE))
 
1697
                retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1739
1698
 
1740
1699
        if (likely(!retval)) {
1741
1700
                if (unlikely(!audit_dummy_context())) {
1743
1702
                                audit_inode(name, nd->path.dentry);
1744
1703
                }
1745
1704
        }
1746
 
 
1747
1705
        return retval;
1748
1706
}
1749
1707
 
1750
 
int path_lookup(const char *name, unsigned int flags,
1751
 
                        struct nameidata *nd)
 
1708
int kern_path_parent(const char *name, struct nameidata *nd)
1752
1709
{
1753
 
        return do_path_lookup(AT_FDCWD, name, flags, nd);
 
1710
        return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
1754
1711
}
1755
1712
 
1756
1713
int kern_path(const char *name, unsigned int flags, struct path *path)
1774
1731
                    const char *name, unsigned int flags,
1775
1732
                    struct nameidata *nd)
1776
1733
{
1777
 
        int retval;
1778
 
 
1779
 
        /* same as do_path_lookup */
1780
 
        nd->last_type = LAST_ROOT;
1781
 
        nd->flags = flags;
1782
 
        nd->depth = 0;
1783
 
 
1784
 
        nd->path.dentry = dentry;
1785
 
        nd->path.mnt = mnt;
1786
 
        path_get(&nd->path);
1787
 
        nd->root = nd->path;
1788
 
        path_get(&nd->root);
1789
 
        nd->inode = nd->path.dentry->d_inode;
1790
 
 
1791
 
        retval = path_walk(name, nd);
1792
 
        if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1793
 
                                nd->inode))
1794
 
                audit_inode(name, nd->path.dentry);
1795
 
 
1796
 
        path_put(&nd->root);
1797
 
        nd->root.mnt = NULL;
1798
 
 
1799
 
        return retval;
 
1734
        nd->root.dentry = dentry;
 
1735
        nd->root.mnt = mnt;
 
1736
        /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
 
1737
        return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
1800
1738
}
1801
1739
 
1802
1740
static struct dentry *__lookup_hash(struct qstr *name,
1811
1749
                return ERR_PTR(err);
1812
1750
 
1813
1751
        /*
1814
 
         * See if the low-level filesystem might want
1815
 
         * to use its own hash..
1816
 
         */
1817
 
        if (base->d_flags & DCACHE_OP_HASH) {
1818
 
                err = base->d_op->d_hash(base, inode, name);
1819
 
                dentry = ERR_PTR(err);
1820
 
                if (err < 0)
1821
 
                        goto out;
1822
 
        }
1823
 
 
1824
 
        /*
1825
1752
         * Don't bother with __d_lookup: callers are for creat as
1826
1753
         * well as unlink, so a lot of the time it would cost
1827
1754
         * a double lookup.
1833
1760
 
1834
1761
        if (!dentry)
1835
1762
                dentry = d_alloc_and_lookup(base, name, nd);
1836
 
out:
 
1763
 
1837
1764
        return dentry;
1838
1765
}
1839
1766
 
1847
1774
        return __lookup_hash(&nd->last, nd->path.dentry, nd);
1848
1775
}
1849
1776
 
1850
 
static int __lookup_one_len(const char *name, struct qstr *this,
1851
 
                struct dentry *base, int len)
1852
 
{
1853
 
        unsigned long hash;
1854
 
        unsigned int c;
1855
 
 
1856
 
        this->name = name;
1857
 
        this->len = len;
1858
 
        if (!len)
1859
 
                return -EACCES;
1860
 
 
1861
 
        hash = init_name_hash();
1862
 
        while (len--) {
1863
 
                c = *(const unsigned char *)name++;
1864
 
                if (c == '/' || c == '\0')
1865
 
                        return -EACCES;
1866
 
                hash = partial_name_hash(c, hash);
1867
 
        }
1868
 
        this->hash = end_name_hash(hash);
1869
 
        return 0;
1870
 
}
1871
 
 
1872
1777
/**
1873
1778
 * lookup_one_len - filesystem helper to lookup single pathname component
1874
1779
 * @name:       pathname component to lookup
1882
1787
 */
1883
1788
struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1884
1789
{
1885
 
        int err;
1886
1790
        struct qstr this;
 
1791
        unsigned long hash;
 
1792
        unsigned int c;
1887
1793
 
1888
1794
        WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
1889
1795
 
1890
 
        err = __lookup_one_len(name, &this, base, len);
1891
 
        if (err)
1892
 
                return ERR_PTR(err);
 
1796
        this.name = name;
 
1797
        this.len = len;
 
1798
        if (!len)
 
1799
                return ERR_PTR(-EACCES);
 
1800
 
 
1801
        hash = init_name_hash();
 
1802
        while (len--) {
 
1803
                c = *(const unsigned char *)name++;
 
1804
                if (c == '/' || c == '\0')
 
1805
                        return ERR_PTR(-EACCES);
 
1806
                hash = partial_name_hash(c, hash);
 
1807
        }
 
1808
        this.hash = end_name_hash(hash);
 
1809
        /*
 
1810
         * See if the low-level filesystem might want
 
1811
         * to use its own hash..
 
1812
         */
 
1813
        if (base->d_flags & DCACHE_OP_HASH) {
 
1814
                int err = base->d_op->d_hash(base, base->d_inode, &this);
 
1815
                if (err < 0)
 
1816
                        return ERR_PTR(err);
 
1817
        }
1893
1818
 
1894
1819
        return __lookup_hash(&this, base, NULL);
1895
1820
}
1898
1823
                 struct path *path)
1899
1824
{
1900
1825
        struct nameidata nd;
1901
 
        char *tmp = getname(name);
 
1826
        char *tmp = getname_flags(name, flags);
1902
1827
        int err = PTR_ERR(tmp);
1903
1828
        if (!IS_ERR(tmp)) {
1904
1829
 
1940
1865
 
1941
1866
        if (!(dir->i_mode & S_ISVTX))
1942
1867
                return 0;
 
1868
        if (current_user_ns() != inode_userns(inode))
 
1869
                goto other_userns;
1943
1870
        if (inode->i_uid == fsuid)
1944
1871
                return 0;
1945
1872
        if (dir->i_uid == fsuid)
1946
1873
                return 0;
1947
 
        return !capable(CAP_FOWNER);
 
1874
 
 
1875
other_userns:
 
1876
        return !ns_capable(inode_userns(inode), CAP_FOWNER);
1948
1877
}
1949
1878
 
1950
1879
/*
2078
2007
        return error;
2079
2008
}
2080
2009
 
2081
 
int may_open(struct path *path, int acc_mode, int flag)
 
2010
static int may_open(struct path *path, int acc_mode, int flag)
2082
2011
{
2083
2012
        struct dentry *dentry = path->dentry;
2084
2013
        struct inode *inode = dentry->d_inode;
2085
2014
        int error;
2086
2015
 
 
2016
        /* O_PATH? */
 
2017
        if (!acc_mode)
 
2018
                return 0;
 
2019
 
2087
2020
        if (!inode)
2088
2021
                return -ENOENT;
2089
2022
 
2120
2053
        }
2121
2054
 
2122
2055
        /* O_NOATIME can only be set by the owner or superuser */
2123
 
        if (flag & O_NOATIME && !is_owner_or_cap(inode))
 
2056
        if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2124
2057
                return -EPERM;
2125
2058
 
2126
2059
        /*
2152
2085
}
2153
2086
 
2154
2087
/*
2155
 
 * Be careful about ever adding any more callers of this
2156
 
 * function.  Its flags must be in the namei format, not
2157
 
 * what get passed to sys_open().
2158
 
 */
2159
 
static int __open_namei_create(struct nameidata *nd, struct path *path,
2160
 
                                int open_flag, int mode)
2161
 
{
2162
 
        int error;
2163
 
        struct dentry *dir = nd->path.dentry;
2164
 
 
2165
 
        if (!IS_POSIXACL(dir->d_inode))
2166
 
                mode &= ~current_umask();
2167
 
        error = security_path_mknod(&nd->path, path->dentry, mode, 0);
2168
 
        if (error)
2169
 
                goto out_unlock;
2170
 
        error = vfs_create(dir->d_inode, path->dentry, mode, nd);
2171
 
out_unlock:
2172
 
        mutex_unlock(&dir->d_inode->i_mutex);
2173
 
        dput(nd->path.dentry);
2174
 
        nd->path.dentry = path->dentry;
2175
 
 
2176
 
        if (error)
2177
 
                return error;
2178
 
        /* Don't check for write permission, don't truncate */
2179
 
        return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
2180
 
}
2181
 
 
2182
 
/*
2183
2088
 * Note that while the flag value (low two bits) for sys_open means:
2184
2089
 *      00 - read-only
2185
2090
 *      01 - write-only
2203
2108
        return flag;
2204
2109
}
2205
2110
 
2206
 
static int open_will_truncate(int flag, struct inode *inode)
2207
 
{
2208
 
        /*
2209
 
         * We'll never write to the fs underlying
2210
 
         * a device file.
2211
 
         */
2212
 
        if (special_file(inode->i_mode))
2213
 
                return 0;
2214
 
        return (flag & O_TRUNC);
2215
 
}
2216
 
 
2217
 
static struct file *finish_open(struct nameidata *nd,
2218
 
                                int open_flag, int acc_mode)
2219
 
{
2220
 
        struct file *filp;
2221
 
        int will_truncate;
2222
 
        int error;
2223
 
 
2224
 
        will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
2225
 
        if (will_truncate) {
2226
 
                error = mnt_want_write(nd->path.mnt);
2227
 
                if (error)
2228
 
                        goto exit;
2229
 
        }
2230
 
        error = may_open(&nd->path, acc_mode, open_flag);
2231
 
        if (error) {
2232
 
                if (will_truncate)
2233
 
                        mnt_drop_write(nd->path.mnt);
2234
 
                goto exit;
2235
 
        }
2236
 
        filp = nameidata_to_filp(nd);
2237
 
        if (!IS_ERR(filp)) {
2238
 
                error = ima_file_check(filp, acc_mode);
2239
 
                if (error) {
2240
 
                        fput(filp);
2241
 
                        filp = ERR_PTR(error);
2242
 
                }
2243
 
        }
2244
 
        if (!IS_ERR(filp)) {
2245
 
                if (will_truncate) {
2246
 
                        error = handle_truncate(filp);
2247
 
                        if (error) {
2248
 
                                fput(filp);
2249
 
                                filp = ERR_PTR(error);
2250
 
                        }
2251
 
                }
2252
 
        }
2253
 
        /*
2254
 
         * It is now safe to drop the mnt write
2255
 
         * because the filp has had a write taken
2256
 
         * on its behalf.
2257
 
         */
2258
 
        if (will_truncate)
2259
 
                mnt_drop_write(nd->path.mnt);
2260
 
        path_put(&nd->path);
2261
 
        return filp;
2262
 
 
2263
 
exit:
2264
 
        path_put(&nd->path);
2265
 
        return ERR_PTR(error);
2266
 
}
2267
 
 
2268
2111
/*
2269
 
 * Handle O_CREAT case for do_filp_open
 
2112
 * Handle the last step of open()
2270
2113
 */
2271
2114
static struct file *do_last(struct nameidata *nd, struct path *path,
2272
 
                            int open_flag, int acc_mode,
2273
 
                            int mode, const char *pathname)
 
2115
                            const struct open_flags *op, const char *pathname)
2274
2116
{
2275
2117
        struct dentry *dir = nd->path.dentry;
 
2118
        struct dentry *dentry;
 
2119
        int open_flag = op->open_flag;
 
2120
        int will_truncate = open_flag & O_TRUNC;
 
2121
        int want_write = 0;
 
2122
        int acc_mode = op->acc_mode;
2276
2123
        struct file *filp;
2277
 
        int error = -EISDIR;
 
2124
        int error;
 
2125
 
 
2126
        nd->flags &= ~LOOKUP_PARENT;
 
2127
        nd->flags |= op->intent;
2278
2128
 
2279
2129
        switch (nd->last_type) {
2280
2130
        case LAST_DOTDOT:
2281
 
                follow_dotdot(nd);
2282
 
                dir = nd->path.dentry;
2283
2131
        case LAST_DOT:
2284
 
                if (need_reval_dot(dir)) {
2285
 
                        int status = d_revalidate(nd->path.dentry, nd);
2286
 
                        if (!status)
2287
 
                                status = -ESTALE;
2288
 
                        if (status < 0) {
2289
 
                                error = status;
2290
 
                                goto exit;
2291
 
                        }
2292
 
                }
 
2132
                error = handle_dots(nd, nd->last_type);
 
2133
                if (error)
 
2134
                        return ERR_PTR(error);
2293
2135
                /* fallthrough */
2294
2136
        case LAST_ROOT:
2295
 
                goto exit;
 
2137
                if (nd->flags & LOOKUP_RCU) {
 
2138
                        if (nameidata_drop_rcu_last(nd))
 
2139
                                return ERR_PTR(-ECHILD);
 
2140
                }
 
2141
                error = handle_reval_path(nd);
 
2142
                if (error)
 
2143
                        goto exit;
 
2144
                audit_inode(pathname, nd->path.dentry);
 
2145
                if (open_flag & O_CREAT) {
 
2146
                        error = -EISDIR;
 
2147
                        goto exit;
 
2148
                }
 
2149
                goto ok;
2296
2150
        case LAST_BIND:
 
2151
                /* can't be RCU mode here */
 
2152
                error = handle_reval_path(nd);
 
2153
                if (error)
 
2154
                        goto exit;
2297
2155
                audit_inode(pathname, dir);
2298
2156
                goto ok;
2299
2157
        }
2300
2158
 
 
2159
        if (!(open_flag & O_CREAT)) {
 
2160
                int symlink_ok = 0;
 
2161
                if (nd->last.name[nd->last.len])
 
2162
                        nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
 
2163
                if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
 
2164
                        symlink_ok = 1;
 
2165
                /* we _can_ be in RCU mode here */
 
2166
                error = walk_component(nd, path, &nd->last, LAST_NORM,
 
2167
                                        !symlink_ok);
 
2168
                if (error < 0)
 
2169
                        return ERR_PTR(error);
 
2170
                if (error) /* symlink */
 
2171
                        return NULL;
 
2172
                /* sayonara */
 
2173
                if (nd->flags & LOOKUP_RCU) {
 
2174
                        if (nameidata_drop_rcu_last(nd))
 
2175
                                return ERR_PTR(-ECHILD);
 
2176
                }
 
2177
 
 
2178
                error = -ENOTDIR;
 
2179
                if (nd->flags & LOOKUP_DIRECTORY) {
 
2180
                        if (!nd->inode->i_op->lookup)
 
2181
                                goto exit;
 
2182
                }
 
2183
                audit_inode(pathname, nd->path.dentry);
 
2184
                goto ok;
 
2185
        }
 
2186
 
 
2187
        /* create side of things */
 
2188
 
 
2189
        if (nd->flags & LOOKUP_RCU) {
 
2190
                if (nameidata_drop_rcu_last(nd))
 
2191
                        return ERR_PTR(-ECHILD);
 
2192
        }
 
2193
 
 
2194
        audit_inode(pathname, dir);
 
2195
        error = -EISDIR;
2301
2196
        /* trailing slashes? */
2302
2197
        if (nd->last.name[nd->last.len])
2303
2198
                goto exit;
2304
2199
 
2305
2200
        mutex_lock(&dir->d_inode->i_mutex);
2306
2201
 
2307
 
        path->dentry = lookup_hash(nd);
 
2202
        dentry = lookup_hash(nd);
 
2203
        error = PTR_ERR(dentry);
 
2204
        if (IS_ERR(dentry)) {
 
2205
                mutex_unlock(&dir->d_inode->i_mutex);
 
2206
                goto exit;
 
2207
        }
 
2208
 
 
2209
        path->dentry = dentry;
2308
2210
        path->mnt = nd->path.mnt;
2309
2211
 
2310
 
        error = PTR_ERR(path->dentry);
2311
 
        if (IS_ERR(path->dentry)) {
2312
 
                mutex_unlock(&dir->d_inode->i_mutex);
2313
 
                goto exit;
2314
 
        }
2315
 
 
2316
 
        if (IS_ERR(nd->intent.open.file)) {
2317
 
                error = PTR_ERR(nd->intent.open.file);
2318
 
                goto exit_mutex_unlock;
2319
 
        }
2320
 
 
2321
2212
        /* Negative dentry, just create the file */
2322
 
        if (!path->dentry->d_inode) {
 
2213
        if (!dentry->d_inode) {
 
2214
                int mode = op->mode;
 
2215
                if (!IS_POSIXACL(dir->d_inode))
 
2216
                        mode &= ~current_umask();
2323
2217
                /*
2324
2218
                 * This write is needed to ensure that a
2325
 
                 * ro->rw transition does not occur between
 
2219
                 * rw->ro transition does not occur between
2326
2220
                 * the time when the file is created and when
2327
2221
                 * a permanent write count is taken through
2328
2222
                 * the 'struct file' in nameidata_to_filp().
2330
2224
                error = mnt_want_write(nd->path.mnt);
2331
2225
                if (error)
2332
2226
                        goto exit_mutex_unlock;
2333
 
                error = __open_namei_create(nd, path, open_flag, mode);
2334
 
                if (error) {
2335
 
                        mnt_drop_write(nd->path.mnt);
2336
 
                        goto exit;
2337
 
                }
2338
 
                filp = nameidata_to_filp(nd);
2339
 
                mnt_drop_write(nd->path.mnt);
2340
 
                path_put(&nd->path);
2341
 
                if (!IS_ERR(filp)) {
2342
 
                        error = ima_file_check(filp, acc_mode);
2343
 
                        if (error) {
2344
 
                                fput(filp);
2345
 
                                filp = ERR_PTR(error);
2346
 
                        }
2347
 
                }
2348
 
                return filp;
 
2227
                want_write = 1;
 
2228
                /* Don't check for write permission, don't truncate */
 
2229
                open_flag &= ~O_TRUNC;
 
2230
                will_truncate = 0;
 
2231
                acc_mode = MAY_OPEN;
 
2232
                error = security_path_mknod(&nd->path, dentry, mode, 0);
 
2233
                if (error)
 
2234
                        goto exit_mutex_unlock;
 
2235
                error = vfs_create(dir->d_inode, dentry, mode, nd);
 
2236
                if (error)
 
2237
                        goto exit_mutex_unlock;
 
2238
                mutex_unlock(&dir->d_inode->i_mutex);
 
2239
                dput(nd->path.dentry);
 
2240
                nd->path.dentry = dentry;
 
2241
                goto common;
2349
2242
        }
2350
2243
 
2351
2244
        /*
2375
2268
        if (S_ISDIR(nd->inode->i_mode))
2376
2269
                goto exit;
2377
2270
ok:
2378
 
        filp = finish_open(nd, open_flag, acc_mode);
 
2271
        if (!S_ISREG(nd->inode->i_mode))
 
2272
                will_truncate = 0;
 
2273
 
 
2274
        if (will_truncate) {
 
2275
                error = mnt_want_write(nd->path.mnt);
 
2276
                if (error)
 
2277
                        goto exit;
 
2278
                want_write = 1;
 
2279
        }
 
2280
common:
 
2281
        error = may_open(&nd->path, acc_mode, open_flag);
 
2282
        if (error)
 
2283
                goto exit;
 
2284
        filp = nameidata_to_filp(nd);
 
2285
        if (!IS_ERR(filp)) {
 
2286
                error = ima_file_check(filp, op->acc_mode);
 
2287
                if (error) {
 
2288
                        fput(filp);
 
2289
                        filp = ERR_PTR(error);
 
2290
                }
 
2291
        }
 
2292
        if (!IS_ERR(filp)) {
 
2293
                if (will_truncate) {
 
2294
                        error = handle_truncate(filp);
 
2295
                        if (error) {
 
2296
                                fput(filp);
 
2297
                                filp = ERR_PTR(error);
 
2298
                        }
 
2299
                }
 
2300
        }
 
2301
out:
 
2302
        if (want_write)
 
2303
                mnt_drop_write(nd->path.mnt);
 
2304
        path_put(&nd->path);
2379
2305
        return filp;
2380
2306
 
2381
2307
exit_mutex_unlock:
2383
2309
exit_dput:
2384
2310
        path_put_conditional(path, nd);
2385
2311
exit:
2386
 
        path_put(&nd->path);
2387
 
        return ERR_PTR(error);
 
2312
        filp = ERR_PTR(error);
 
2313
        goto out;
2388
2314
}
2389
2315
 
2390
 
/*
2391
 
 * Note that the low bits of the passed in "open_flag"
2392
 
 * are not the same as in the local variable "flag". See
2393
 
 * open_to_namei_flags() for more details.
2394
 
 */
2395
 
struct file *do_filp_open(int dfd, const char *pathname,
2396
 
                int open_flag, int mode, int acc_mode)
 
2316
static struct file *path_openat(int dfd, const char *pathname,
 
2317
                struct nameidata *nd, const struct open_flags *op, int flags)
2397
2318
{
 
2319
        struct file *base = NULL;
2398
2320
        struct file *filp;
2399
 
        struct nameidata nd;
 
2321
        struct path path;
2400
2322
        int error;
2401
 
        struct path path;
2402
 
        int count = 0;
2403
 
        int flag = open_to_namei_flags(open_flag);
2404
 
        int flags;
2405
 
 
2406
 
        if (!(open_flag & O_CREAT))
2407
 
                mode = 0;
2408
 
 
2409
 
        /* Must never be set by userspace */
2410
 
        open_flag &= ~FMODE_NONOTIFY;
2411
 
 
2412
 
        /*
2413
 
         * O_SYNC is implemented as __O_SYNC|O_DSYNC.  As many places only
2414
 
         * check for O_DSYNC if the need any syncing at all we enforce it's
2415
 
         * always set instead of having to deal with possibly weird behaviour
2416
 
         * for malicious applications setting only __O_SYNC.
2417
 
         */
2418
 
        if (open_flag & __O_SYNC)
2419
 
                open_flag |= O_DSYNC;
2420
 
 
2421
 
        if (!acc_mode)
2422
 
                acc_mode = MAY_OPEN | ACC_MODE(open_flag);
2423
 
 
2424
 
        /* O_TRUNC implies we need access checks for write permissions */
2425
 
        if (open_flag & O_TRUNC)
2426
 
                acc_mode |= MAY_WRITE;
2427
 
 
2428
 
        /* Allow the LSM permission hook to distinguish append 
2429
 
           access from general write access. */
2430
 
        if (open_flag & O_APPEND)
2431
 
                acc_mode |= MAY_APPEND;
2432
 
 
2433
 
        flags = LOOKUP_OPEN;
2434
 
        if (open_flag & O_CREAT) {
2435
 
                flags |= LOOKUP_CREATE;
2436
 
                if (open_flag & O_EXCL)
2437
 
                        flags |= LOOKUP_EXCL;
2438
 
        }
2439
 
        if (open_flag & O_DIRECTORY)
2440
 
                flags |= LOOKUP_DIRECTORY;
2441
 
        if (!(open_flag & O_NOFOLLOW))
2442
 
                flags |= LOOKUP_FOLLOW;
2443
2323
 
2444
2324
        filp = get_empty_filp();
2445
2325
        if (!filp)
2446
2326
                return ERR_PTR(-ENFILE);
2447
2327
 
2448
 
        filp->f_flags = open_flag;
2449
 
        nd.intent.open.file = filp;
2450
 
        nd.intent.open.flags = flag;
2451
 
        nd.intent.open.create_mode = mode;
2452
 
 
2453
 
        if (open_flag & O_CREAT)
2454
 
                goto creat;
2455
 
 
2456
 
        /* !O_CREAT, simple open */
2457
 
        error = do_path_lookup(dfd, pathname, flags, &nd);
2458
 
        if (unlikely(error))
2459
 
                goto out_filp2;
2460
 
        error = -ELOOP;
2461
 
        if (!(nd.flags & LOOKUP_FOLLOW)) {
2462
 
                if (nd.inode->i_op->follow_link)
2463
 
                        goto out_path2;
2464
 
        }
2465
 
        error = -ENOTDIR;
2466
 
        if (nd.flags & LOOKUP_DIRECTORY) {
2467
 
                if (!nd.inode->i_op->lookup)
2468
 
                        goto out_path2;
2469
 
        }
2470
 
        audit_inode(pathname, nd.path.dentry);
2471
 
        filp = finish_open(&nd, open_flag, acc_mode);
2472
 
out2:
2473
 
        release_open_intent(&nd);
2474
 
        return filp;
2475
 
 
2476
 
out_path2:
2477
 
        path_put(&nd.path);
2478
 
out_filp2:
2479
 
        filp = ERR_PTR(error);
2480
 
        goto out2;
2481
 
 
2482
 
creat:
2483
 
        /* OK, have to create the file. Find the parent. */
2484
 
        error = path_init_rcu(dfd, pathname,
2485
 
                        LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
2486
 
        if (error)
2487
 
                goto out_filp;
2488
 
        error = path_walk_rcu(pathname, &nd);
2489
 
        path_finish_rcu(&nd);
2490
 
        if (unlikely(error == -ECHILD || error == -ESTALE)) {
2491
 
                /* slower, locked walk */
2492
 
                if (error == -ESTALE) {
2493
 
reval:
2494
 
                        flags |= LOOKUP_REVAL;
2495
 
                }
2496
 
                error = path_init(dfd, pathname,
2497
 
                                LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
2498
 
                if (error)
2499
 
                        goto out_filp;
2500
 
 
2501
 
                error = path_walk_simple(pathname, &nd);
2502
 
        }
2503
 
        if (unlikely(error))
2504
 
                goto out_filp;
2505
 
        if (unlikely(!audit_dummy_context()))
2506
 
                audit_inode(pathname, nd.path.dentry);
2507
 
 
2508
 
        /*
2509
 
         * We have the parent and last component.
2510
 
         */
2511
 
        nd.flags = flags;
2512
 
        filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
 
2328
        filp->f_flags = op->open_flag;
 
2329
        nd->intent.open.file = filp;
 
2330
        nd->intent.open.flags = open_to_namei_flags(op->open_flag);
 
2331
        nd->intent.open.create_mode = op->mode;
 
2332
 
 
2333
        error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
 
2334
        if (unlikely(error))
 
2335
                goto out_filp;
 
2336
 
 
2337
        current->total_link_count = 0;
 
2338
        error = link_path_walk(pathname, nd);
 
2339
        if (unlikely(error))
 
2340
                goto out_filp;
 
2341
 
 
2342
        filp = do_last(nd, &path, op, pathname);
2513
2343
        while (unlikely(!filp)) { /* trailing symlink */
2514
2344
                struct path link = path;
2515
 
                struct inode *linki = link.dentry->d_inode;
2516
2345
                void *cookie;
2517
 
                error = -ELOOP;
2518
 
                if (!(nd.flags & LOOKUP_FOLLOW))
2519
 
                        goto exit_dput;
2520
 
                if (count++ == 32)
2521
 
                        goto exit_dput;
2522
 
                /*
2523
 
                 * This is subtle. Instead of calling do_follow_link() we do
2524
 
                 * the thing by hands. The reason is that this way we have zero
2525
 
                 * link_count and path_walk() (called from ->follow_link)
2526
 
                 * honoring LOOKUP_PARENT.  After that we have the parent and
2527
 
                 * last component, i.e. we are in the same situation as after
2528
 
                 * the first path_walk().  Well, almost - if the last component
2529
 
                 * is normal we get its copy stored in nd->last.name and we will
2530
 
                 * have to putname() it when we are done. Procfs-like symlinks
2531
 
                 * just set LAST_BIND.
2532
 
                 */
2533
 
                nd.flags |= LOOKUP_PARENT;
2534
 
                error = security_inode_follow_link(link.dentry, &nd);
2535
 
                if (error)
2536
 
                        goto exit_dput;
2537
 
                error = __do_follow_link(&link, &nd, &cookie);
2538
 
                if (unlikely(error)) {
2539
 
                        if (!IS_ERR(cookie) && linki->i_op->put_link)
2540
 
                                linki->i_op->put_link(link.dentry, &nd, cookie);
2541
 
                        /* nd.path had been dropped */
2542
 
                        nd.path = link;
2543
 
                        goto out_path;
 
2346
                if (!(nd->flags & LOOKUP_FOLLOW)) {
 
2347
                        path_put_conditional(&path, nd);
 
2348
                        path_put(&nd->path);
 
2349
                        filp = ERR_PTR(-ELOOP);
 
2350
                        break;
2544
2351
                }
2545
 
                nd.flags &= ~LOOKUP_PARENT;
2546
 
                filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
2547
 
                if (linki->i_op->put_link)
2548
 
                        linki->i_op->put_link(link.dentry, &nd, cookie);
2549
 
                path_put(&link);
 
2352
                nd->flags |= LOOKUP_PARENT;
 
2353
                nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
 
2354
                error = follow_link(&link, nd, &cookie);
 
2355
                if (unlikely(error))
 
2356
                        filp = ERR_PTR(error);
 
2357
                else
 
2358
                        filp = do_last(nd, &path, op, pathname);
 
2359
                put_link(nd, &link, cookie);
2550
2360
        }
2551
2361
out:
2552
 
        if (nd.root.mnt)
2553
 
                path_put(&nd.root);
2554
 
        if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL))
2555
 
                goto reval;
2556
 
        release_open_intent(&nd);
 
2362
        if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
 
2363
                path_put(&nd->root);
 
2364
        if (base)
 
2365
                fput(base);
 
2366
        release_open_intent(nd);
2557
2367
        return filp;
2558
2368
 
2559
 
exit_dput:
2560
 
        path_put_conditional(&path, &nd);
2561
 
out_path:
2562
 
        path_put(&nd.path);
2563
2369
out_filp:
2564
2370
        filp = ERR_PTR(error);
2565
2371
        goto out;
2566
2372
}
2567
2373
 
2568
 
/**
2569
 
 * filp_open - open file and return file pointer
2570
 
 *
2571
 
 * @filename:   path to open
2572
 
 * @flags:      open flags as per the open(2) second argument
2573
 
 * @mode:       mode for the new file if O_CREAT is set, else ignored
2574
 
 *
2575
 
 * This is the helper to open a file from kernelspace if you really
2576
 
 * have to.  But in generally you should not do this, so please move
2577
 
 * along, nothing to see here..
2578
 
 */
2579
 
struct file *filp_open(const char *filename, int flags, int mode)
2580
 
{
2581
 
        return do_filp_open(AT_FDCWD, filename, flags, mode, 0);
2582
 
}
2583
 
EXPORT_SYMBOL(filp_open);
 
2374
struct file *do_filp_open(int dfd, const char *pathname,
 
2375
                const struct open_flags *op, int flags)
 
2376
{
 
2377
        struct nameidata nd;
 
2378
        struct file *filp;
 
2379
 
 
2380
        filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
 
2381
        if (unlikely(filp == ERR_PTR(-ECHILD)))
 
2382
                filp = path_openat(dfd, pathname, &nd, op, flags);
 
2383
        if (unlikely(filp == ERR_PTR(-ESTALE)))
 
2384
                filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
 
2385
        return filp;
 
2386
}
 
2387
 
 
2388
struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
 
2389
                const char *name, const struct open_flags *op, int flags)
 
2390
{
 
2391
        struct nameidata nd;
 
2392
        struct file *file;
 
2393
 
 
2394
        nd.root.mnt = mnt;
 
2395
        nd.root.dentry = dentry;
 
2396
 
 
2397
        flags |= LOOKUP_ROOT;
 
2398
 
 
2399
        if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
 
2400
                return ERR_PTR(-ELOOP);
 
2401
 
 
2402
        file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
 
2403
        if (unlikely(file == ERR_PTR(-ECHILD)))
 
2404
                file = path_openat(-1, name, &nd, op, flags);
 
2405
        if (unlikely(file == ERR_PTR(-ESTALE)))
 
2406
                file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
 
2407
        return file;
 
2408
}
2584
2409
 
2585
2410
/**
2586
2411
 * lookup_create - lookup a dentry, creating it if it doesn't exist
2642
2467
        if (error)
2643
2468
                return error;
2644
2469
 
2645
 
        if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
 
2470
        if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
 
2471
            !ns_capable(inode_userns(dir), CAP_MKNOD))
2646
2472
                return -EPERM;
2647
2473
 
2648
2474
        if (!dir->i_op->mknod)
3119
2945
                return error;
3120
2946
 
3121
2947
        mutex_lock(&inode->i_mutex);
3122
 
        error = dir->i_op->link(old_dentry, dir, new_dentry);
 
2948
        /* Make sure we don't allow creating hardlink to an unlinked file */
 
2949
        if (inode->i_nlink == 0)
 
2950
                error =  -ENOENT;
 
2951
        else
 
2952
                error = dir->i_op->link(old_dentry, dir, new_dentry);
3123
2953
        mutex_unlock(&inode->i_mutex);
3124
2954
        if (!error)
3125
2955
                fsnotify_link(dir, inode, new_dentry);
3141
2971
        struct dentry *new_dentry;
3142
2972
        struct nameidata nd;
3143
2973
        struct path old_path;
 
2974
        int how = 0;
3144
2975
        int error;
3145
2976
        char *to;
3146
2977
 
3147
 
        if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
 
2978
        if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3148
2979
                return -EINVAL;
3149
 
 
3150
 
        error = user_path_at(olddfd, oldname,
3151
 
                             flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
3152
 
                             &old_path);
 
2980
        /*
 
2981
         * To use null names we require CAP_DAC_READ_SEARCH
 
2982
         * This ensures that not everyone will be able to create
 
2983
         * handlink using the passed filedescriptor.
 
2984
         */
 
2985
        if (flags & AT_EMPTY_PATH) {
 
2986
                if (!capable(CAP_DAC_READ_SEARCH))
 
2987
                        return -ENOENT;
 
2988
                how = LOOKUP_EMPTY;
 
2989
        }
 
2990
 
 
2991
        if (flags & AT_SYMLINK_FOLLOW)
 
2992
                how |= LOOKUP_FOLLOW;
 
2993
 
 
2994
        error = user_path_at(olddfd, oldname, how, &old_path);
3153
2995
        if (error)
3154
2996
                return error;
3155
2997
 
3586
3428
EXPORT_SYMBOL(__page_symlink);
3587
3429
EXPORT_SYMBOL(page_symlink);
3588
3430
EXPORT_SYMBOL(page_symlink_inode_operations);
3589
 
EXPORT_SYMBOL(path_lookup);
 
3431
EXPORT_SYMBOL(kern_path_parent);
3590
3432
EXPORT_SYMBOL(kern_path);
3591
3433
EXPORT_SYMBOL(vfs_path_lookup);
3592
3434
EXPORT_SYMBOL(inode_permission);