[PATCH] r/o bind mounts: write counts for truncate()
[safe/jmp/linux-2.6] / fs / open.c
1 /*
2  *  linux/fs/open.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6
7 #include <linux/string.h>
8 #include <linux/mm.h>
9 #include <linux/file.h>
10 #include <linux/quotaops.h>
11 #include <linux/fsnotify.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/tty.h>
15 #include <linux/namei.h>
16 #include <linux/backing-dev.h>
17 #include <linux/capability.h>
18 #include <linux/security.h>
19 #include <linux/mount.h>
20 #include <linux/vfs.h>
21 #include <linux/fcntl.h>
22 #include <asm/uaccess.h>
23 #include <linux/fs.h>
24 #include <linux/personality.h>
25 #include <linux/pagemap.h>
26 #include <linux/syscalls.h>
27 #include <linux/rcupdate.h>
28 #include <linux/audit.h>
29 #include <linux/falloc.h>
30
31 int vfs_statfs(struct dentry *dentry, struct kstatfs *buf)
32 {
33         int retval = -ENODEV;
34
35         if (dentry) {
36                 retval = -ENOSYS;
37                 if (dentry->d_sb->s_op->statfs) {
38                         memset(buf, 0, sizeof(*buf));
39                         retval = security_sb_statfs(dentry);
40                         if (retval)
41                                 return retval;
42                         retval = dentry->d_sb->s_op->statfs(dentry, buf);
43                         if (retval == 0 && buf->f_frsize == 0)
44                                 buf->f_frsize = buf->f_bsize;
45                 }
46         }
47         return retval;
48 }
49
50 EXPORT_SYMBOL(vfs_statfs);
51
52 static int vfs_statfs_native(struct dentry *dentry, struct statfs *buf)
53 {
54         struct kstatfs st;
55         int retval;
56
57         retval = vfs_statfs(dentry, &st);
58         if (retval)
59                 return retval;
60
61         if (sizeof(*buf) == sizeof(st))
62                 memcpy(buf, &st, sizeof(st));
63         else {
64                 if (sizeof buf->f_blocks == 4) {
65                         if ((st.f_blocks | st.f_bfree | st.f_bavail) &
66                             0xffffffff00000000ULL)
67                                 return -EOVERFLOW;
68                         /*
69                          * f_files and f_ffree may be -1; it's okay to stuff
70                          * that into 32 bits
71                          */
72                         if (st.f_files != -1 &&
73                             (st.f_files & 0xffffffff00000000ULL))
74                                 return -EOVERFLOW;
75                         if (st.f_ffree != -1 &&
76                             (st.f_ffree & 0xffffffff00000000ULL))
77                                 return -EOVERFLOW;
78                 }
79
80                 buf->f_type = st.f_type;
81                 buf->f_bsize = st.f_bsize;
82                 buf->f_blocks = st.f_blocks;
83                 buf->f_bfree = st.f_bfree;
84                 buf->f_bavail = st.f_bavail;
85                 buf->f_files = st.f_files;
86                 buf->f_ffree = st.f_ffree;
87                 buf->f_fsid = st.f_fsid;
88                 buf->f_namelen = st.f_namelen;
89                 buf->f_frsize = st.f_frsize;
90                 memset(buf->f_spare, 0, sizeof(buf->f_spare));
91         }
92         return 0;
93 }
94
95 static int vfs_statfs64(struct dentry *dentry, struct statfs64 *buf)
96 {
97         struct kstatfs st;
98         int retval;
99
100         retval = vfs_statfs(dentry, &st);
101         if (retval)
102                 return retval;
103
104         if (sizeof(*buf) == sizeof(st))
105                 memcpy(buf, &st, sizeof(st));
106         else {
107                 buf->f_type = st.f_type;
108                 buf->f_bsize = st.f_bsize;
109                 buf->f_blocks = st.f_blocks;
110                 buf->f_bfree = st.f_bfree;
111                 buf->f_bavail = st.f_bavail;
112                 buf->f_files = st.f_files;
113                 buf->f_ffree = st.f_ffree;
114                 buf->f_fsid = st.f_fsid;
115                 buf->f_namelen = st.f_namelen;
116                 buf->f_frsize = st.f_frsize;
117                 memset(buf->f_spare, 0, sizeof(buf->f_spare));
118         }
119         return 0;
120 }
121
122 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
123 {
124         struct nameidata nd;
125         int error;
126
127         error = user_path_walk(path, &nd);
128         if (!error) {
129                 struct statfs tmp;
130                 error = vfs_statfs_native(nd.path.dentry, &tmp);
131                 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
132                         error = -EFAULT;
133                 path_put(&nd.path);
134         }
135         return error;
136 }
137
138
139 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
140 {
141         struct nameidata nd;
142         long error;
143
144         if (sz != sizeof(*buf))
145                 return -EINVAL;
146         error = user_path_walk(path, &nd);
147         if (!error) {
148                 struct statfs64 tmp;
149                 error = vfs_statfs64(nd.path.dentry, &tmp);
150                 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
151                         error = -EFAULT;
152                 path_put(&nd.path);
153         }
154         return error;
155 }
156
157
158 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
159 {
160         struct file * file;
161         struct statfs tmp;
162         int error;
163
164         error = -EBADF;
165         file = fget(fd);
166         if (!file)
167                 goto out;
168         error = vfs_statfs_native(file->f_path.dentry, &tmp);
169         if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
170                 error = -EFAULT;
171         fput(file);
172 out:
173         return error;
174 }
175
176 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
177 {
178         struct file * file;
179         struct statfs64 tmp;
180         int error;
181
182         if (sz != sizeof(*buf))
183                 return -EINVAL;
184
185         error = -EBADF;
186         file = fget(fd);
187         if (!file)
188                 goto out;
189         error = vfs_statfs64(file->f_path.dentry, &tmp);
190         if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
191                 error = -EFAULT;
192         fput(file);
193 out:
194         return error;
195 }
196
197 int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
198         struct file *filp)
199 {
200         int err;
201         struct iattr newattrs;
202
203         /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
204         if (length < 0)
205                 return -EINVAL;
206
207         newattrs.ia_size = length;
208         newattrs.ia_valid = ATTR_SIZE | time_attrs;
209         if (filp) {
210                 newattrs.ia_file = filp;
211                 newattrs.ia_valid |= ATTR_FILE;
212         }
213
214         /* Remove suid/sgid on truncate too */
215         newattrs.ia_valid |= should_remove_suid(dentry);
216
217         mutex_lock(&dentry->d_inode->i_mutex);
218         err = notify_change(dentry, &newattrs);
219         mutex_unlock(&dentry->d_inode->i_mutex);
220         return err;
221 }
222
223 static long do_sys_truncate(const char __user * path, loff_t length)
224 {
225         struct nameidata nd;
226         struct inode * inode;
227         int error;
228
229         error = -EINVAL;
230         if (length < 0) /* sorry, but loff_t says... */
231                 goto out;
232
233         error = user_path_walk(path, &nd);
234         if (error)
235                 goto out;
236         inode = nd.path.dentry->d_inode;
237
238         /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
239         error = -EISDIR;
240         if (S_ISDIR(inode->i_mode))
241                 goto dput_and_out;
242
243         error = -EINVAL;
244         if (!S_ISREG(inode->i_mode))
245                 goto dput_and_out;
246
247         error = mnt_want_write(nd.path.mnt);
248         if (error)
249                 goto dput_and_out;
250
251         error = vfs_permission(&nd, MAY_WRITE);
252         if (error)
253                 goto mnt_drop_write_and_out;
254
255         error = -EPERM;
256         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
257                 goto mnt_drop_write_and_out;
258
259         error = get_write_access(inode);
260         if (error)
261                 goto mnt_drop_write_and_out;
262
263         /*
264          * Make sure that there are no leases.  get_write_access() protects
265          * against the truncate racing with a lease-granting setlease().
266          */
267         error = break_lease(inode, FMODE_WRITE);
268         if (error)
269                 goto put_write_and_out;
270
271         error = locks_verify_truncate(inode, NULL, length);
272         if (!error) {
273                 DQUOT_INIT(inode);
274                 error = do_truncate(nd.path.dentry, length, 0, NULL);
275         }
276
277 put_write_and_out:
278         put_write_access(inode);
279 mnt_drop_write_and_out:
280         mnt_drop_write(nd.path.mnt);
281 dput_and_out:
282         path_put(&nd.path);
283 out:
284         return error;
285 }
286
287 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
288 {
289         /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
290         return do_sys_truncate(path, (long)length);
291 }
292
293 static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
294 {
295         struct inode * inode;
296         struct dentry *dentry;
297         struct file * file;
298         int error;
299
300         error = -EINVAL;
301         if (length < 0)
302                 goto out;
303         error = -EBADF;
304         file = fget(fd);
305         if (!file)
306                 goto out;
307
308         /* explicitly opened as large or we are on 64-bit box */
309         if (file->f_flags & O_LARGEFILE)
310                 small = 0;
311
312         dentry = file->f_path.dentry;
313         inode = dentry->d_inode;
314         error = -EINVAL;
315         if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
316                 goto out_putf;
317
318         error = -EINVAL;
319         /* Cannot ftruncate over 2^31 bytes without large file support */
320         if (small && length > MAX_NON_LFS)
321                 goto out_putf;
322
323         error = -EPERM;
324         if (IS_APPEND(inode))
325                 goto out_putf;
326
327         error = locks_verify_truncate(inode, file, length);
328         if (!error)
329                 error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
330 out_putf:
331         fput(file);
332 out:
333         return error;
334 }
335
336 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
337 {
338         long ret = do_sys_ftruncate(fd, length, 1);
339         /* avoid REGPARM breakage on x86: */
340         asmlinkage_protect(2, ret, fd, length);
341         return ret;
342 }
343
344 /* LFS versions of truncate are only needed on 32 bit machines */
345 #if BITS_PER_LONG == 32
346 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
347 {
348         return do_sys_truncate(path, length);
349 }
350
351 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
352 {
353         long ret = do_sys_ftruncate(fd, length, 0);
354         /* avoid REGPARM breakage on x86: */
355         asmlinkage_protect(2, ret, fd, length);
356         return ret;
357 }
358 #endif
359
360 asmlinkage long sys_fallocate(int fd, int mode, loff_t offset, loff_t len)
361 {
362         struct file *file;
363         struct inode *inode;
364         long ret = -EINVAL;
365
366         if (offset < 0 || len <= 0)
367                 goto out;
368
369         /* Return error if mode is not supported */
370         ret = -EOPNOTSUPP;
371         if (mode && !(mode & FALLOC_FL_KEEP_SIZE))
372                 goto out;
373
374         ret = -EBADF;
375         file = fget(fd);
376         if (!file)
377                 goto out;
378         if (!(file->f_mode & FMODE_WRITE))
379                 goto out_fput;
380         /*
381          * Revalidate the write permissions, in case security policy has
382          * changed since the files were opened.
383          */
384         ret = security_file_permission(file, MAY_WRITE);
385         if (ret)
386                 goto out_fput;
387
388         inode = file->f_path.dentry->d_inode;
389
390         ret = -ESPIPE;
391         if (S_ISFIFO(inode->i_mode))
392                 goto out_fput;
393
394         ret = -ENODEV;
395         /*
396          * Let individual file system decide if it supports preallocation
397          * for directories or not.
398          */
399         if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
400                 goto out_fput;
401
402         ret = -EFBIG;
403         /* Check for wrap through zero too */
404         if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
405                 goto out_fput;
406
407         if (inode->i_op && inode->i_op->fallocate)
408                 ret = inode->i_op->fallocate(inode, mode, offset, len);
409         else
410                 ret = -EOPNOTSUPP;
411
412 out_fput:
413         fput(file);
414 out:
415         return ret;
416 }
417
418 /*
419  * access() needs to use the real uid/gid, not the effective uid/gid.
420  * We do this by temporarily clearing all FS-related capabilities and
421  * switching the fsuid/fsgid around to the real ones.
422  */
423 asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
424 {
425         struct nameidata nd;
426         int old_fsuid, old_fsgid;
427         kernel_cap_t old_cap;
428         int res;
429
430         if (mode & ~S_IRWXO)    /* where's F_OK, X_OK, W_OK, R_OK? */
431                 return -EINVAL;
432
433         old_fsuid = current->fsuid;
434         old_fsgid = current->fsgid;
435         old_cap = current->cap_effective;
436
437         current->fsuid = current->uid;
438         current->fsgid = current->gid;
439
440         /*
441          * Clear the capabilities if we switch to a non-root user
442          *
443          * FIXME: There is a race here against sys_capset.  The
444          * capabilities can change yet we will restore the old
445          * value below.  We should hold task_capabilities_lock,
446          * but we cannot because user_path_walk can sleep.
447          */
448         if (current->uid)
449                 cap_clear(current->cap_effective);
450         else
451                 current->cap_effective = current->cap_permitted;
452
453         res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
454         if (res)
455                 goto out;
456
457         res = vfs_permission(&nd, mode);
458         /* SuS v2 requires we report a read only fs too */
459         if(res || !(mode & S_IWOTH) ||
460            special_file(nd.path.dentry->d_inode->i_mode))
461                 goto out_path_release;
462
463         if(IS_RDONLY(nd.path.dentry->d_inode))
464                 res = -EROFS;
465
466 out_path_release:
467         path_put(&nd.path);
468 out:
469         current->fsuid = old_fsuid;
470         current->fsgid = old_fsgid;
471         current->cap_effective = old_cap;
472
473         return res;
474 }
475
476 asmlinkage long sys_access(const char __user *filename, int mode)
477 {
478         return sys_faccessat(AT_FDCWD, filename, mode);
479 }
480
481 asmlinkage long sys_chdir(const char __user * filename)
482 {
483         struct nameidata nd;
484         int error;
485
486         error = __user_walk(filename,
487                             LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
488         if (error)
489                 goto out;
490
491         error = vfs_permission(&nd, MAY_EXEC);
492         if (error)
493                 goto dput_and_out;
494
495         set_fs_pwd(current->fs, &nd.path);
496
497 dput_and_out:
498         path_put(&nd.path);
499 out:
500         return error;
501 }
502
503 asmlinkage long sys_fchdir(unsigned int fd)
504 {
505         struct file *file;
506         struct inode *inode;
507         int error;
508
509         error = -EBADF;
510         file = fget(fd);
511         if (!file)
512                 goto out;
513
514         inode = file->f_path.dentry->d_inode;
515
516         error = -ENOTDIR;
517         if (!S_ISDIR(inode->i_mode))
518                 goto out_putf;
519
520         error = file_permission(file, MAY_EXEC);
521         if (!error)
522                 set_fs_pwd(current->fs, &file->f_path);
523 out_putf:
524         fput(file);
525 out:
526         return error;
527 }
528
529 asmlinkage long sys_chroot(const char __user * filename)
530 {
531         struct nameidata nd;
532         int error;
533
534         error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
535         if (error)
536                 goto out;
537
538         error = vfs_permission(&nd, MAY_EXEC);
539         if (error)
540                 goto dput_and_out;
541
542         error = -EPERM;
543         if (!capable(CAP_SYS_CHROOT))
544                 goto dput_and_out;
545
546         set_fs_root(current->fs, &nd.path);
547         set_fs_altroot();
548         error = 0;
549 dput_and_out:
550         path_put(&nd.path);
551 out:
552         return error;
553 }
554
555 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
556 {
557         struct inode * inode;
558         struct dentry * dentry;
559         struct file * file;
560         int err = -EBADF;
561         struct iattr newattrs;
562
563         file = fget(fd);
564         if (!file)
565                 goto out;
566
567         dentry = file->f_path.dentry;
568         inode = dentry->d_inode;
569
570         audit_inode(NULL, dentry);
571
572         err = mnt_want_write(file->f_path.mnt);
573         if (err)
574                 goto out_putf;
575         err = -EPERM;
576         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
577                 goto out_drop_write;
578         mutex_lock(&inode->i_mutex);
579         if (mode == (mode_t) -1)
580                 mode = inode->i_mode;
581         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
582         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
583         err = notify_change(dentry, &newattrs);
584         mutex_unlock(&inode->i_mutex);
585
586 out_drop_write:
587         mnt_drop_write(file->f_path.mnt);
588 out_putf:
589         fput(file);
590 out:
591         return err;
592 }
593
594 asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
595                              mode_t mode)
596 {
597         struct nameidata nd;
598         struct inode * inode;
599         int error;
600         struct iattr newattrs;
601
602         error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
603         if (error)
604                 goto out;
605         inode = nd.path.dentry->d_inode;
606
607         error = mnt_want_write(nd.path.mnt);
608         if (error)
609                 goto dput_and_out;
610
611         error = -EPERM;
612         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
613                 goto out_drop_write;
614
615         mutex_lock(&inode->i_mutex);
616         if (mode == (mode_t) -1)
617                 mode = inode->i_mode;
618         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
619         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
620         error = notify_change(nd.path.dentry, &newattrs);
621         mutex_unlock(&inode->i_mutex);
622
623 out_drop_write:
624         mnt_drop_write(nd.path.mnt);
625 dput_and_out:
626         path_put(&nd.path);
627 out:
628         return error;
629 }
630
631 asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
632 {
633         return sys_fchmodat(AT_FDCWD, filename, mode);
634 }
635
636 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
637 {
638         struct inode * inode;
639         int error;
640         struct iattr newattrs;
641
642         error = -ENOENT;
643         if (!(inode = dentry->d_inode)) {
644                 printk(KERN_ERR "chown_common: NULL inode\n");
645                 goto out;
646         }
647         error = -EPERM;
648         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
649                 goto out;
650         newattrs.ia_valid =  ATTR_CTIME;
651         if (user != (uid_t) -1) {
652                 newattrs.ia_valid |= ATTR_UID;
653                 newattrs.ia_uid = user;
654         }
655         if (group != (gid_t) -1) {
656                 newattrs.ia_valid |= ATTR_GID;
657                 newattrs.ia_gid = group;
658         }
659         if (!S_ISDIR(inode->i_mode))
660                 newattrs.ia_valid |=
661                         ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
662         mutex_lock(&inode->i_mutex);
663         error = notify_change(dentry, &newattrs);
664         mutex_unlock(&inode->i_mutex);
665 out:
666         return error;
667 }
668
669 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
670 {
671         struct nameidata nd;
672         int error;
673
674         error = user_path_walk(filename, &nd);
675         if (error)
676                 goto out;
677         error = mnt_want_write(nd.path.mnt);
678         if (error)
679                 goto out_release;
680         error = chown_common(nd.path.dentry, user, group);
681         mnt_drop_write(nd.path.mnt);
682 out_release:
683         path_put(&nd.path);
684 out:
685         return error;
686 }
687
688 asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
689                              gid_t group, int flag)
690 {
691         struct nameidata nd;
692         int error = -EINVAL;
693         int follow;
694
695         if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
696                 goto out;
697
698         follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
699         error = __user_walk_fd(dfd, filename, follow, &nd);
700         if (error)
701                 goto out;
702         error = mnt_want_write(nd.path.mnt);
703         if (error)
704                 goto out_release;
705         error = chown_common(nd.path.dentry, user, group);
706         mnt_drop_write(nd.path.mnt);
707 out_release:
708         path_put(&nd.path);
709 out:
710         return error;
711 }
712
713 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
714 {
715         struct nameidata nd;
716         int error;
717
718         error = user_path_walk_link(filename, &nd);
719         if (error)
720                 goto out;
721         error = mnt_want_write(nd.path.mnt);
722         if (error)
723                 goto out_release;
724         error = chown_common(nd.path.dentry, user, group);
725         mnt_drop_write(nd.path.mnt);
726 out_release:
727         path_put(&nd.path);
728 out:
729         return error;
730 }
731
732
733 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
734 {
735         struct file * file;
736         int error = -EBADF;
737         struct dentry * dentry;
738
739         file = fget(fd);
740         if (!file)
741                 goto out;
742
743         error = mnt_want_write(file->f_path.mnt);
744         if (error)
745                 goto out_fput;
746         dentry = file->f_path.dentry;
747         audit_inode(NULL, dentry);
748         error = chown_common(dentry, user, group);
749         mnt_drop_write(file->f_path.mnt);
750 out_fput:
751         fput(file);
752 out:
753         return error;
754 }
755
756 /*
757  * You have to be very careful that these write
758  * counts get cleaned up in error cases and
759  * upon __fput().  This should probably never
760  * be called outside of __dentry_open().
761  */
762 static inline int __get_file_write_access(struct inode *inode,
763                                           struct vfsmount *mnt)
764 {
765         int error;
766         error = get_write_access(inode);
767         if (error)
768                 return error;
769         /*
770          * Do not take mount writer counts on
771          * special files since no writes to
772          * the mount itself will occur.
773          */
774         if (!special_file(inode->i_mode)) {
775                 /*
776                  * Balanced in __fput()
777                  */
778                 error = mnt_want_write(mnt);
779                 if (error)
780                         put_write_access(inode);
781         }
782         return error;
783 }
784
785 static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
786                                         int flags, struct file *f,
787                                         int (*open)(struct inode *, struct file *))
788 {
789         struct inode *inode;
790         int error;
791
792         f->f_flags = flags;
793         f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
794                                 FMODE_PREAD | FMODE_PWRITE;
795         inode = dentry->d_inode;
796         if (f->f_mode & FMODE_WRITE) {
797                 error = __get_file_write_access(inode, mnt);
798                 if (error)
799                         goto cleanup_file;
800         }
801
802         f->f_mapping = inode->i_mapping;
803         f->f_path.dentry = dentry;
804         f->f_path.mnt = mnt;
805         f->f_pos = 0;
806         f->f_op = fops_get(inode->i_fop);
807         file_move(f, &inode->i_sb->s_files);
808
809         error = security_dentry_open(f);
810         if (error)
811                 goto cleanup_all;
812
813         if (!open && f->f_op)
814                 open = f->f_op->open;
815         if (open) {
816                 error = open(inode, f);
817                 if (error)
818                         goto cleanup_all;
819         }
820
821         f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
822
823         file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
824
825         /* NB: we're sure to have correct a_ops only after f_op->open */
826         if (f->f_flags & O_DIRECT) {
827                 if (!f->f_mapping->a_ops ||
828                     ((!f->f_mapping->a_ops->direct_IO) &&
829                     (!f->f_mapping->a_ops->get_xip_page))) {
830                         fput(f);
831                         f = ERR_PTR(-EINVAL);
832                 }
833         }
834
835         return f;
836
837 cleanup_all:
838         fops_put(f->f_op);
839         if (f->f_mode & FMODE_WRITE) {
840                 put_write_access(inode);
841                 if (!special_file(inode->i_mode))
842                         mnt_drop_write(mnt);
843         }
844         file_kill(f);
845         f->f_path.dentry = NULL;
846         f->f_path.mnt = NULL;
847 cleanup_file:
848         put_filp(f);
849         dput(dentry);
850         mntput(mnt);
851         return ERR_PTR(error);
852 }
853
854 /**
855  * lookup_instantiate_filp - instantiates the open intent filp
856  * @nd: pointer to nameidata
857  * @dentry: pointer to dentry
858  * @open: open callback
859  *
860  * Helper for filesystems that want to use lookup open intents and pass back
861  * a fully instantiated struct file to the caller.
862  * This function is meant to be called from within a filesystem's
863  * lookup method.
864  * Beware of calling it for non-regular files! Those ->open methods might block
865  * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
866  * leading to a deadlock, as nobody can open that fifo anymore, because
867  * another process to open fifo will block on locked parent when doing lookup).
868  * Note that in case of error, nd->intent.open.file is destroyed, but the
869  * path information remains valid.
870  * If the open callback is set to NULL, then the standard f_op->open()
871  * filesystem callback is substituted.
872  */
873 struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
874                 int (*open)(struct inode *, struct file *))
875 {
876         if (IS_ERR(nd->intent.open.file))
877                 goto out;
878         if (IS_ERR(dentry))
879                 goto out_err;
880         nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
881                                              nd->intent.open.flags - 1,
882                                              nd->intent.open.file,
883                                              open);
884 out:
885         return nd->intent.open.file;
886 out_err:
887         release_open_intent(nd);
888         nd->intent.open.file = (struct file *)dentry;
889         goto out;
890 }
891 EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
892
893 /**
894  * nameidata_to_filp - convert a nameidata to an open filp.
895  * @nd: pointer to nameidata
896  * @flags: open flags
897  *
898  * Note that this function destroys the original nameidata
899  */
900 struct file *nameidata_to_filp(struct nameidata *nd, int flags)
901 {
902         struct file *filp;
903
904         /* Pick up the filp from the open intent */
905         filp = nd->intent.open.file;
906         /* Has the filesystem initialised the file for us? */
907         if (filp->f_path.dentry == NULL)
908                 filp = __dentry_open(nd->path.dentry, nd->path.mnt, flags, filp,
909                                      NULL);
910         else
911                 path_put(&nd->path);
912         return filp;
913 }
914
915 /*
916  * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
917  * error.
918  */
919 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
920 {
921         int error;
922         struct file *f;
923
924         /*
925          * We must always pass in a valid mount pointer.   Historically
926          * callers got away with not passing it, but we must enforce this at
927          * the earliest possible point now to avoid strange problems deep in the
928          * filesystem stack.
929          */
930         if (!mnt) {
931                 printk(KERN_WARNING "%s called with NULL vfsmount\n", __func__);
932                 dump_stack();
933                 return ERR_PTR(-EINVAL);
934         }
935
936         error = -ENFILE;
937         f = get_empty_filp();
938         if (f == NULL) {
939                 dput(dentry);
940                 mntput(mnt);
941                 return ERR_PTR(error);
942         }
943
944         return __dentry_open(dentry, mnt, flags, f, NULL);
945 }
946 EXPORT_SYMBOL(dentry_open);
947
948 /*
949  * Find an empty file descriptor entry, and mark it busy.
950  */
951 int get_unused_fd_flags(int flags)
952 {
953         struct files_struct * files = current->files;
954         int fd, error;
955         struct fdtable *fdt;
956
957         error = -EMFILE;
958         spin_lock(&files->file_lock);
959
960 repeat:
961         fdt = files_fdtable(files);
962         fd = find_next_zero_bit(fdt->open_fds->fds_bits, fdt->max_fds,
963                                 files->next_fd);
964
965         /*
966          * N.B. For clone tasks sharing a files structure, this test
967          * will limit the total number of files that can be opened.
968          */
969         if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
970                 goto out;
971
972         /* Do we need to expand the fd array or fd set?  */
973         error = expand_files(files, fd);
974         if (error < 0)
975                 goto out;
976
977         if (error) {
978                 /*
979                  * If we needed to expand the fs array we
980                  * might have blocked - try again.
981                  */
982                 error = -EMFILE;
983                 goto repeat;
984         }
985
986         FD_SET(fd, fdt->open_fds);
987         if (flags & O_CLOEXEC)
988                 FD_SET(fd, fdt->close_on_exec);
989         else
990                 FD_CLR(fd, fdt->close_on_exec);
991         files->next_fd = fd + 1;
992 #if 1
993         /* Sanity check */
994         if (fdt->fd[fd] != NULL) {
995                 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
996                 fdt->fd[fd] = NULL;
997         }
998 #endif
999         error = fd;
1000
1001 out:
1002         spin_unlock(&files->file_lock);
1003         return error;
1004 }
1005
1006 int get_unused_fd(void)
1007 {
1008         return get_unused_fd_flags(0);
1009 }
1010
1011 EXPORT_SYMBOL(get_unused_fd);
1012
1013 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
1014 {
1015         struct fdtable *fdt = files_fdtable(files);
1016         __FD_CLR(fd, fdt->open_fds);
1017         if (fd < files->next_fd)
1018                 files->next_fd = fd;
1019 }
1020
1021 void put_unused_fd(unsigned int fd)
1022 {
1023         struct files_struct *files = current->files;
1024         spin_lock(&files->file_lock);
1025         __put_unused_fd(files, fd);
1026         spin_unlock(&files->file_lock);
1027 }
1028
1029 EXPORT_SYMBOL(put_unused_fd);
1030
1031 /*
1032  * Install a file pointer in the fd array.
1033  *
1034  * The VFS is full of places where we drop the files lock between
1035  * setting the open_fds bitmap and installing the file in the file
1036  * array.  At any such point, we are vulnerable to a dup2() race
1037  * installing a file in the array before us.  We need to detect this and
1038  * fput() the struct file we are about to overwrite in this case.
1039  *
1040  * It should never happen - if we allow dup2() do it, _really_ bad things
1041  * will follow.
1042  */
1043
1044 void fd_install(unsigned int fd, struct file *file)
1045 {
1046         struct files_struct *files = current->files;
1047         struct fdtable *fdt;
1048         spin_lock(&files->file_lock);
1049         fdt = files_fdtable(files);
1050         BUG_ON(fdt->fd[fd] != NULL);
1051         rcu_assign_pointer(fdt->fd[fd], file);
1052         spin_unlock(&files->file_lock);
1053 }
1054
1055 EXPORT_SYMBOL(fd_install);
1056
1057 long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
1058 {
1059         char *tmp = getname(filename);
1060         int fd = PTR_ERR(tmp);
1061
1062         if (!IS_ERR(tmp)) {
1063                 fd = get_unused_fd_flags(flags);
1064                 if (fd >= 0) {
1065                         struct file *f = do_filp_open(dfd, tmp, flags, mode);
1066                         if (IS_ERR(f)) {
1067                                 put_unused_fd(fd);
1068                                 fd = PTR_ERR(f);
1069                         } else {
1070                                 fsnotify_open(f->f_path.dentry);
1071                                 fd_install(fd, f);
1072                         }
1073                 }
1074                 putname(tmp);
1075         }
1076         return fd;
1077 }
1078
1079 asmlinkage long sys_open(const char __user *filename, int flags, int mode)
1080 {
1081         long ret;
1082
1083         if (force_o_largefile())
1084                 flags |= O_LARGEFILE;
1085
1086         ret = do_sys_open(AT_FDCWD, filename, flags, mode);
1087         /* avoid REGPARM breakage on x86: */
1088         asmlinkage_protect(3, ret, filename, flags, mode);
1089         return ret;
1090 }
1091
1092 asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
1093                            int mode)
1094 {
1095         long ret;
1096
1097         if (force_o_largefile())
1098                 flags |= O_LARGEFILE;
1099
1100         ret = do_sys_open(dfd, filename, flags, mode);
1101         /* avoid REGPARM breakage on x86: */
1102         asmlinkage_protect(4, ret, dfd, filename, flags, mode);
1103         return ret;
1104 }
1105
1106 #ifndef __alpha__
1107
1108 /*
1109  * For backward compatibility?  Maybe this should be moved
1110  * into arch/i386 instead?
1111  */
1112 asmlinkage long sys_creat(const char __user * pathname, int mode)
1113 {
1114         return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1115 }
1116
1117 #endif
1118
1119 /*
1120  * "id" is the POSIX thread ID. We use the
1121  * files pointer for this..
1122  */
1123 int filp_close(struct file *filp, fl_owner_t id)
1124 {
1125         int retval = 0;
1126
1127         if (!file_count(filp)) {
1128                 printk(KERN_ERR "VFS: Close: file count is 0\n");
1129                 return 0;
1130         }
1131
1132         if (filp->f_op && filp->f_op->flush)
1133                 retval = filp->f_op->flush(filp, id);
1134
1135         dnotify_flush(filp, id);
1136         locks_remove_posix(filp, id);
1137         fput(filp);
1138         return retval;
1139 }
1140
1141 EXPORT_SYMBOL(filp_close);
1142
1143 /*
1144  * Careful here! We test whether the file pointer is NULL before
1145  * releasing the fd. This ensures that one clone task can't release
1146  * an fd while another clone is opening it.
1147  */
1148 asmlinkage long sys_close(unsigned int fd)
1149 {
1150         struct file * filp;
1151         struct files_struct *files = current->files;
1152         struct fdtable *fdt;
1153         int retval;
1154
1155         spin_lock(&files->file_lock);
1156         fdt = files_fdtable(files);
1157         if (fd >= fdt->max_fds)
1158                 goto out_unlock;
1159         filp = fdt->fd[fd];
1160         if (!filp)
1161                 goto out_unlock;
1162         rcu_assign_pointer(fdt->fd[fd], NULL);
1163         FD_CLR(fd, fdt->close_on_exec);
1164         __put_unused_fd(files, fd);
1165         spin_unlock(&files->file_lock);
1166         retval = filp_close(filp, files);
1167
1168         /* can't restart close syscall because file table entry was cleared */
1169         if (unlikely(retval == -ERESTARTSYS ||
1170                      retval == -ERESTARTNOINTR ||
1171                      retval == -ERESTARTNOHAND ||
1172                      retval == -ERESTART_RESTARTBLOCK))
1173                 retval = -EINTR;
1174
1175         return retval;
1176
1177 out_unlock:
1178         spin_unlock(&files->file_lock);
1179         return -EBADF;
1180 }
1181
1182 EXPORT_SYMBOL(sys_close);
1183
1184 /*
1185  * This routine simulates a hangup on the tty, to arrange that users
1186  * are given clean terminals at login time.
1187  */
1188 asmlinkage long sys_vhangup(void)
1189 {
1190         if (capable(CAP_SYS_TTY_CONFIG)) {
1191                 /* XXX: this needs locking */
1192                 tty_vhangup(current->signal->tty);
1193                 return 0;
1194         }
1195         return -EPERM;
1196 }
1197
1198 /*
1199  * Called when an inode is about to be open.
1200  * We use this to disallow opening large files on 32bit systems if
1201  * the caller didn't specify O_LARGEFILE.  On 64bit systems we force
1202  * on this flag in sys_open.
1203  */
1204 int generic_file_open(struct inode * inode, struct file * filp)
1205 {
1206         if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1207                 return -EOVERFLOW;
1208         return 0;
1209 }
1210
1211 EXPORT_SYMBOL(generic_file_open);
1212
1213 /*
1214  * This is used by subsystems that don't want seekable
1215  * file descriptors
1216  */
1217 int nonseekable_open(struct inode *inode, struct file *filp)
1218 {
1219         filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1220         return 0;
1221 }
1222
1223 EXPORT_SYMBOL(nonseekable_open);