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

« back to all changes in this revision

Viewing changes to fs/open.c

  • Committer: Bazaar Package Importer
  • Author(s): Ben Hutchings, Ben Hutchings, Aurelien Jarno, Martin Michlmayr
  • Date: 2011-04-06 13:53:30 UTC
  • mfrom: (43.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110406135330-wjufxhd0tvn3zx4z
Tags: 2.6.38-3
[ Ben Hutchings ]
* [ppc64] Add to linux-tools package architectures (Closes: #620124)
* [amd64] Save cr4 to mmu_cr4_features at boot time (Closes: #620284)
* appletalk: Fix bugs introduced when removing use of BKL
* ALSA: Fix yet another race in disconnection
* cciss: Fix lost command issue
* ath9k: Fix kernel panic in AR2427
* ses: Avoid kernel panic when lun 0 is not mapped
* PCI/ACPI: Report ASPM support to BIOS if not disabled from command line

[ Aurelien Jarno ]
* rtlwifi: fix build when PCI is not enabled.

[ Martin Michlmayr ]
* rtlwifi: Eliminate udelay calls with too large values (Closes: #620204)

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
#include <linux/mm.h>
9
9
#include <linux/file.h>
10
10
#include <linux/fdtable.h>
11
 
#include <linux/quotaops.h>
12
11
#include <linux/fsnotify.h>
13
12
#include <linux/module.h>
14
 
#include <linux/slab.h>
15
13
#include <linux/tty.h>
16
14
#include <linux/namei.h>
17
15
#include <linux/backing-dev.h>
19
17
#include <linux/securebits.h>
20
18
#include <linux/security.h>
21
19
#include <linux/mount.h>
22
 
#include <linux/vfs.h>
23
20
#include <linux/fcntl.h>
 
21
#include <linux/slab.h>
24
22
#include <asm/uaccess.h>
25
23
#include <linux/fs.h>
26
24
#include <linux/personality.h>
30
28
#include <linux/audit.h>
31
29
#include <linux/falloc.h>
32
30
#include <linux/fs_struct.h>
33
 
 
34
 
int vfs_statfs(struct dentry *dentry, struct kstatfs *buf)
35
 
{
36
 
        int retval = -ENODEV;
37
 
 
38
 
        if (dentry) {
39
 
                retval = -ENOSYS;
40
 
                if (dentry->d_sb->s_op->statfs) {
41
 
                        memset(buf, 0, sizeof(*buf));
42
 
                        retval = security_sb_statfs(dentry);
43
 
                        if (retval)
44
 
                                return retval;
45
 
                        retval = dentry->d_sb->s_op->statfs(dentry, buf);
46
 
                        if (retval == 0 && buf->f_frsize == 0)
47
 
                                buf->f_frsize = buf->f_bsize;
48
 
                }
49
 
        }
50
 
        return retval;
51
 
}
52
 
 
53
 
EXPORT_SYMBOL(vfs_statfs);
54
 
 
55
 
static int vfs_statfs_native(struct dentry *dentry, struct statfs *buf)
56
 
{
57
 
        struct kstatfs st;
58
 
        int retval;
59
 
 
60
 
        retval = vfs_statfs(dentry, &st);
61
 
        if (retval)
62
 
                return retval;
63
 
 
64
 
        if (sizeof(*buf) == sizeof(st))
65
 
                memcpy(buf, &st, sizeof(st));
66
 
        else {
67
 
                if (sizeof buf->f_blocks == 4) {
68
 
                        if ((st.f_blocks | st.f_bfree | st.f_bavail |
69
 
                             st.f_bsize | st.f_frsize) &
70
 
                            0xffffffff00000000ULL)
71
 
                                return -EOVERFLOW;
72
 
                        /*
73
 
                         * f_files and f_ffree may be -1; it's okay to stuff
74
 
                         * that into 32 bits
75
 
                         */
76
 
                        if (st.f_files != -1 &&
77
 
                            (st.f_files & 0xffffffff00000000ULL))
78
 
                                return -EOVERFLOW;
79
 
                        if (st.f_ffree != -1 &&
80
 
                            (st.f_ffree & 0xffffffff00000000ULL))
81
 
                                return -EOVERFLOW;
82
 
                }
83
 
 
84
 
                buf->f_type = st.f_type;
85
 
                buf->f_bsize = st.f_bsize;
86
 
                buf->f_blocks = st.f_blocks;
87
 
                buf->f_bfree = st.f_bfree;
88
 
                buf->f_bavail = st.f_bavail;
89
 
                buf->f_files = st.f_files;
90
 
                buf->f_ffree = st.f_ffree;
91
 
                buf->f_fsid = st.f_fsid;
92
 
                buf->f_namelen = st.f_namelen;
93
 
                buf->f_frsize = st.f_frsize;
94
 
                memset(buf->f_spare, 0, sizeof(buf->f_spare));
95
 
        }
96
 
        return 0;
97
 
}
98
 
 
99
 
static int vfs_statfs64(struct dentry *dentry, struct statfs64 *buf)
100
 
{
101
 
        struct kstatfs st;
102
 
        int retval;
103
 
 
104
 
        retval = vfs_statfs(dentry, &st);
105
 
        if (retval)
106
 
                return retval;
107
 
 
108
 
        if (sizeof(*buf) == sizeof(st))
109
 
                memcpy(buf, &st, sizeof(st));
110
 
        else {
111
 
                buf->f_type = st.f_type;
112
 
                buf->f_bsize = st.f_bsize;
113
 
                buf->f_blocks = st.f_blocks;
114
 
                buf->f_bfree = st.f_bfree;
115
 
                buf->f_bavail = st.f_bavail;
116
 
                buf->f_files = st.f_files;
117
 
                buf->f_ffree = st.f_ffree;
118
 
                buf->f_fsid = st.f_fsid;
119
 
                buf->f_namelen = st.f_namelen;
120
 
                buf->f_frsize = st.f_frsize;
121
 
                memset(buf->f_spare, 0, sizeof(buf->f_spare));
122
 
        }
123
 
        return 0;
124
 
}
125
 
 
126
 
SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
127
 
{
128
 
        struct path path;
129
 
        int error;
130
 
 
131
 
        error = user_path(pathname, &path);
132
 
        if (!error) {
133
 
                struct statfs tmp;
134
 
                error = vfs_statfs_native(path.dentry, &tmp);
135
 
                if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
136
 
                        error = -EFAULT;
137
 
                path_put(&path);
138
 
        }
139
 
        return error;
140
 
}
141
 
 
142
 
SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
143
 
{
144
 
        struct path path;
145
 
        long error;
146
 
 
147
 
        if (sz != sizeof(*buf))
148
 
                return -EINVAL;
149
 
        error = user_path(pathname, &path);
150
 
        if (!error) {
151
 
                struct statfs64 tmp;
152
 
                error = vfs_statfs64(path.dentry, &tmp);
153
 
                if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
154
 
                        error = -EFAULT;
155
 
                path_put(&path);
156
 
        }
157
 
        return error;
158
 
}
159
 
 
160
 
SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
161
 
{
162
 
        struct file * file;
163
 
        struct statfs tmp;
164
 
        int error;
165
 
 
166
 
        error = -EBADF;
167
 
        file = fget(fd);
168
 
        if (!file)
169
 
                goto out;
170
 
        error = vfs_statfs_native(file->f_path.dentry, &tmp);
171
 
        if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
172
 
                error = -EFAULT;
173
 
        fput(file);
174
 
out:
175
 
        return error;
176
 
}
177
 
 
178
 
SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
179
 
{
180
 
        struct file * file;
181
 
        struct statfs64 tmp;
182
 
        int error;
183
 
 
184
 
        if (sz != sizeof(*buf))
185
 
                return -EINVAL;
186
 
 
187
 
        error = -EBADF;
188
 
        file = fget(fd);
189
 
        if (!file)
190
 
                goto out;
191
 
        error = vfs_statfs64(file->f_path.dentry, &tmp);
192
 
        if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
193
 
                error = -EFAULT;
194
 
        fput(file);
195
 
out:
196
 
        return error;
197
 
}
 
31
#include <linux/ima.h>
 
32
#include <linux/dnotify.h>
 
33
 
 
34
#include "internal.h"
198
35
 
199
36
int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
200
37
        struct file *filp)
268
105
         * Make sure that there are no leases.  get_write_access() protects
269
106
         * against the truncate racing with a lease-granting setlease().
270
107
         */
271
 
        error = break_lease(inode, FMODE_WRITE);
 
108
        error = break_lease(inode, O_WRONLY);
272
109
        if (error)
273
110
                goto put_write_and_out;
274
111
 
275
112
        error = locks_verify_truncate(inode, NULL, length);
276
113
        if (!error)
277
 
                error = security_path_truncate(&path, length, 0);
278
 
        if (!error) {
279
 
                vfs_dq_init(inode);
 
114
                error = security_path_truncate(&path);
 
115
        if (!error)
280
116
                error = do_truncate(path.dentry, length, 0, NULL);
281
 
        }
282
117
 
283
118
put_write_and_out:
284
119
        put_write_access(inode);
331
166
 
332
167
        error = locks_verify_truncate(inode, file, length);
333
168
        if (!error)
334
 
                error = security_path_truncate(&file->f_path, length,
335
 
                                               ATTR_MTIME|ATTR_CTIME);
 
169
                error = security_path_truncate(&file->f_path);
336
170
        if (!error)
337
171
                error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
338
172
out_putf:
389
223
                return -EINVAL;
390
224
 
391
225
        /* Return error if mode is not supported */
392
 
        if (mode && !(mode & FALLOC_FL_KEEP_SIZE))
 
226
        if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
 
227
                return -EOPNOTSUPP;
 
228
 
 
229
        /* Punch hole must have keep size set */
 
230
        if ((mode & FALLOC_FL_PUNCH_HOLE) &&
 
231
            !(mode & FALLOC_FL_KEEP_SIZE))
393
232
                return -EOPNOTSUPP;
394
233
 
395
234
        if (!(file->f_mode & FMODE_WRITE))
396
235
                return -EBADF;
 
236
 
 
237
        /* It's not possible punch hole on append only file */
 
238
        if (mode & FALLOC_FL_PUNCH_HOLE && IS_APPEND(inode))
 
239
                return -EPERM;
 
240
 
 
241
        if (IS_IMMUTABLE(inode))
 
242
                return -EPERM;
 
243
 
397
244
        /*
398
245
         * Revalidate the write permissions, in case security policy has
399
246
         * changed since the files were opened.
416
263
        if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
417
264
                return -EFBIG;
418
265
 
419
 
        if (!inode->i_op->fallocate)
 
266
        if (!file->f_op->fallocate)
420
267
                return -EOPNOTSUPP;
421
268
 
422
 
        return inode->i_op->fallocate(inode, mode, offset, len);
 
269
        return file->f_op->fallocate(file, mode, offset, len);
423
270
}
424
271
 
425
272
SYSCALL_DEFINE(fallocate)(int fd, int mode, loff_t offset, loff_t len)
533
380
        if (error)
534
381
                goto out;
535
382
 
536
 
        error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
 
383
        error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
537
384
        if (error)
538
385
                goto dput_and_out;
539
386
 
562
409
        if (!S_ISDIR(inode->i_mode))
563
410
                goto out_putf;
564
411
 
565
 
        error = inode_permission(inode, MAY_EXEC | MAY_ACCESS);
 
412
        error = inode_permission(inode, MAY_EXEC | MAY_CHDIR);
566
413
        if (!error)
567
414
                set_fs_pwd(current->fs, &file->f_path);
568
415
out_putf:
580
427
        if (error)
581
428
                goto out;
582
429
 
583
 
        error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
 
430
        error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
584
431
        if (error)
585
432
                goto dput_and_out;
586
433
 
587
434
        error = -EPERM;
588
435
        if (!capable(CAP_SYS_CHROOT))
589
436
                goto dput_and_out;
 
437
        error = security_path_chroot(&path);
 
438
        if (error)
 
439
                goto dput_and_out;
590
440
 
591
441
        set_fs_root(current->fs, &path);
592
442
        error = 0;
617
467
        if (err)
618
468
                goto out_putf;
619
469
        mutex_lock(&inode->i_mutex);
 
470
        err = security_path_chmod(dentry, file->f_vfsmnt, mode);
 
471
        if (err)
 
472
                goto out_unlock;
620
473
        if (mode == (mode_t) -1)
621
474
                mode = inode->i_mode;
622
475
        newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
623
476
        newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
624
477
        err = notify_change(dentry, &newattrs);
 
478
out_unlock:
625
479
        mutex_unlock(&inode->i_mutex);
626
480
        mnt_drop_write(file->f_path.mnt);
627
481
out_putf:
646
500
        if (error)
647
501
                goto dput_and_out;
648
502
        mutex_lock(&inode->i_mutex);
 
503
        error = security_path_chmod(path.dentry, path.mnt, mode);
 
504
        if (error)
 
505
                goto out_unlock;
649
506
        if (mode == (mode_t) -1)
650
507
                mode = inode->i_mode;
651
508
        newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
652
509
        newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
653
510
        error = notify_change(path.dentry, &newattrs);
 
511
out_unlock:
654
512
        mutex_unlock(&inode->i_mutex);
655
513
        mnt_drop_write(path.mnt);
656
514
dput_and_out:
664
522
        return sys_fchmodat(AT_FDCWD, filename, mode);
665
523
}
666
524
 
667
 
static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
 
525
static int chown_common(struct path *path, uid_t user, gid_t group)
668
526
{
669
 
        struct inode *inode = dentry->d_inode;
 
527
        struct inode *inode = path->dentry->d_inode;
670
528
        int error;
671
529
        struct iattr newattrs;
672
530
 
683
541
                newattrs.ia_valid |=
684
542
                        ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
685
543
        mutex_lock(&inode->i_mutex);
686
 
        error = notify_change(dentry, &newattrs);
 
544
        error = security_path_chown(path, user, group);
 
545
        if (!error)
 
546
                error = notify_change(path->dentry, &newattrs);
687
547
        mutex_unlock(&inode->i_mutex);
688
548
 
689
549
        return error;
700
560
        error = mnt_want_write(path.mnt);
701
561
        if (error)
702
562
                goto out_release;
703
 
        error = chown_common(path.dentry, user, group);
 
563
        error = chown_common(&path, user, group);
704
564
        mnt_drop_write(path.mnt);
705
565
out_release:
706
566
        path_put(&path);
725
585
        error = mnt_want_write(path.mnt);
726
586
        if (error)
727
587
                goto out_release;
728
 
        error = chown_common(path.dentry, user, group);
 
588
        error = chown_common(&path, user, group);
729
589
        mnt_drop_write(path.mnt);
730
590
out_release:
731
591
        path_put(&path);
744
604
        error = mnt_want_write(path.mnt);
745
605
        if (error)
746
606
                goto out_release;
747
 
        error = chown_common(path.dentry, user, group);
 
607
        error = chown_common(&path, user, group);
748
608
        mnt_drop_write(path.mnt);
749
609
out_release:
750
610
        path_put(&path);
767
627
                goto out_fput;
768
628
        dentry = file->f_path.dentry;
769
629
        audit_inode(NULL, dentry);
770
 
        error = chown_common(dentry, user, group);
 
630
        error = chown_common(&file->f_path, user, group);
771
631
        mnt_drop_write(file->f_path.mnt);
772
632
out_fput:
773
633
        fput(file);
805
665
}
806
666
 
807
667
static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
808
 
                                        int flags, struct file *f,
 
668
                                        struct file *f,
809
669
                                        int (*open)(struct inode *, struct file *),
810
670
                                        const struct cred *cred)
811
671
{
812
672
        struct inode *inode;
813
673
        int error;
814
674
 
815
 
        f->f_flags = flags;
816
 
        f->f_mode = (__force fmode_t)((flags+1) & O_ACCMODE) | FMODE_LSEEK |
 
675
        f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
817
676
                                FMODE_PREAD | FMODE_PWRITE;
818
677
        inode = dentry->d_inode;
819
678
        if (f->f_mode & FMODE_WRITE) {
829
688
        f->f_path.mnt = mnt;
830
689
        f->f_pos = 0;
831
690
        f->f_op = fops_get(inode->i_fop);
832
 
        file_move(f, &inode->i_sb->s_files);
 
691
        file_sb_list_add(f, inode->i_sb);
833
692
 
834
693
        error = security_dentry_open(f, cred);
835
694
        if (error)
842
701
                if (error)
843
702
                        goto cleanup_all;
844
703
        }
 
704
        ima_counts_get(f);
845
705
 
846
706
        f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
847
707
 
874
734
                        mnt_drop_write(mnt);
875
735
                }
876
736
        }
877
 
        file_kill(f);
 
737
        file_sb_list_del(f);
878
738
        f->f_path.dentry = NULL;
879
739
        f->f_path.mnt = NULL;
880
740
cleanup_file:
913
773
        if (IS_ERR(dentry))
914
774
                goto out_err;
915
775
        nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
916
 
                                             nd->intent.open.flags - 1,
917
776
                                             nd->intent.open.file,
918
777
                                             open, cred);
919
778
out:
932
791
 *
933
792
 * Note that this function destroys the original nameidata
934
793
 */
935
 
struct file *nameidata_to_filp(struct nameidata *nd, int flags)
 
794
struct file *nameidata_to_filp(struct nameidata *nd)
936
795
{
937
796
        const struct cred *cred = current_cred();
938
797
        struct file *filp;
939
798
 
940
799
        /* Pick up the filp from the open intent */
941
800
        filp = nd->intent.open.file;
 
801
        nd->intent.open.file = NULL;
 
802
 
942
803
        /* Has the filesystem initialised the file for us? */
943
 
        if (filp->f_path.dentry == NULL)
944
 
                filp = __dentry_open(nd->path.dentry, nd->path.mnt, flags, filp,
 
804
        if (filp->f_path.dentry == NULL) {
 
805
                path_get(&nd->path);
 
806
                filp = __dentry_open(nd->path.dentry, nd->path.mnt, filp,
945
807
                                     NULL, cred);
946
 
        else
947
 
                path_put(&nd->path);
 
808
        }
948
809
        return filp;
949
810
}
950
811
 
980
841
                return ERR_PTR(error);
981
842
        }
982
843
 
983
 
        return __dentry_open(dentry, mnt, flags, f, NULL, cred);
 
844
        f->f_flags = flags;
 
845
        return __dentry_open(dentry, mnt, f, NULL, cred);
984
846
}
985
847
EXPORT_SYMBOL(dentry_open);
986
848
 
1041
903
                                put_unused_fd(fd);
1042
904
                                fd = PTR_ERR(f);
1043
905
                        } else {
1044
 
                                fsnotify_open(f->f_path.dentry);
 
906
                                fsnotify_open(f);
1045
907
                                fd_install(fd, f);
1046
908
                        }
1047
909
                }
1184
1046
 
1185
1047
/*
1186
1048
 * This is used by subsystems that don't want seekable
1187
 
 * file descriptors
 
1049
 * file descriptors. The function is not supposed to ever fail, the only
 
1050
 * reason it returns an 'int' and not 'void' is so that it can be plugged
 
1051
 * directly into file_operations structure.
1188
1052
 */
1189
1053
int nonseekable_open(struct inode *inode, struct file *filp)
1190
1054
{