ANDROID: vfs: Add permission2 for filesystems with per mount permissions
[firefly-linux-kernel-4.4.55.git] / 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/fdtable.h>
11 #include <linux/fsnotify.h>
12 #include <linux/module.h>
13 #include <linux/tty.h>
14 #include <linux/namei.h>
15 #include <linux/backing-dev.h>
16 #include <linux/capability.h>
17 #include <linux/securebits.h>
18 #include <linux/security.h>
19 #include <linux/mount.h>
20 #include <linux/fcntl.h>
21 #include <linux/slab.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 #include <linux/fs_struct.h>
31 #include <linux/ima.h>
32 #include <linux/dnotify.h>
33 #include <linux/compat.h>
34
35 #include "internal.h"
36
37 int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
38         struct file *filp)
39 {
40         int ret;
41         struct iattr newattrs;
42
43         /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
44         if (length < 0)
45                 return -EINVAL;
46
47         newattrs.ia_size = length;
48         newattrs.ia_valid = ATTR_SIZE | time_attrs;
49         if (filp) {
50                 newattrs.ia_file = filp;
51                 newattrs.ia_valid |= ATTR_FILE;
52         }
53
54         /* Remove suid, sgid, and file capabilities on truncate too */
55         ret = dentry_needs_remove_privs(dentry);
56         if (ret < 0)
57                 return ret;
58         if (ret)
59                 newattrs.ia_valid |= ret | ATTR_FORCE;
60
61         mutex_lock(&dentry->d_inode->i_mutex);
62         /* Note any delegations or leases have already been broken: */
63         ret = notify_change(dentry, &newattrs, NULL);
64         mutex_unlock(&dentry->d_inode->i_mutex);
65         return ret;
66 }
67
68 long vfs_truncate(struct path *path, loff_t length)
69 {
70         struct inode *inode;
71         struct vfsmount *mnt;
72         long error;
73
74         inode = path->dentry->d_inode;
75         mnt = path->mnt;
76
77         /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
78         if (S_ISDIR(inode->i_mode))
79                 return -EISDIR;
80         if (!S_ISREG(inode->i_mode))
81                 return -EINVAL;
82
83         error = mnt_want_write(path->mnt);
84         if (error)
85                 goto out;
86
87         error = inode_permission2(mnt, inode, MAY_WRITE);
88         if (error)
89                 goto mnt_drop_write_and_out;
90
91         error = -EPERM;
92         if (IS_APPEND(inode))
93                 goto mnt_drop_write_and_out;
94
95         error = get_write_access(inode);
96         if (error)
97                 goto mnt_drop_write_and_out;
98
99         /*
100          * Make sure that there are no leases.  get_write_access() protects
101          * against the truncate racing with a lease-granting setlease().
102          */
103         error = break_lease(inode, O_WRONLY);
104         if (error)
105                 goto put_write_and_out;
106
107         error = locks_verify_truncate(inode, NULL, length);
108         if (!error)
109                 error = security_path_truncate(path);
110         if (!error)
111                 error = do_truncate(path->dentry, length, 0, NULL);
112
113 put_write_and_out:
114         put_write_access(inode);
115 mnt_drop_write_and_out:
116         mnt_drop_write(path->mnt);
117 out:
118         return error;
119 }
120 EXPORT_SYMBOL_GPL(vfs_truncate);
121
122 static long do_sys_truncate(const char __user *pathname, loff_t length)
123 {
124         unsigned int lookup_flags = LOOKUP_FOLLOW;
125         struct path path;
126         int error;
127
128         if (length < 0) /* sorry, but loff_t says... */
129                 return -EINVAL;
130
131 retry:
132         error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
133         if (!error) {
134                 error = vfs_truncate(&path, length);
135                 path_put(&path);
136         }
137         if (retry_estale(error, lookup_flags)) {
138                 lookup_flags |= LOOKUP_REVAL;
139                 goto retry;
140         }
141         return error;
142 }
143
144 SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
145 {
146         return do_sys_truncate(path, length);
147 }
148
149 #ifdef CONFIG_COMPAT
150 COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length)
151 {
152         return do_sys_truncate(path, length);
153 }
154 #endif
155
156 static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
157 {
158         struct inode *inode;
159         struct dentry *dentry;
160         struct fd f;
161         int error;
162
163         error = -EINVAL;
164         if (length < 0)
165                 goto out;
166         error = -EBADF;
167         f = fdget(fd);
168         if (!f.file)
169                 goto out;
170
171         /* explicitly opened as large or we are on 64-bit box */
172         if (f.file->f_flags & O_LARGEFILE)
173                 small = 0;
174
175         dentry = f.file->f_path.dentry;
176         inode = dentry->d_inode;
177         error = -EINVAL;
178         if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
179                 goto out_putf;
180
181         error = -EINVAL;
182         /* Cannot ftruncate over 2^31 bytes without large file support */
183         if (small && length > MAX_NON_LFS)
184                 goto out_putf;
185
186         error = -EPERM;
187         if (IS_APPEND(inode))
188                 goto out_putf;
189
190         sb_start_write(inode->i_sb);
191         error = locks_verify_truncate(inode, f.file, length);
192         if (!error)
193                 error = security_path_truncate(&f.file->f_path);
194         if (!error)
195                 error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file);
196         sb_end_write(inode->i_sb);
197 out_putf:
198         fdput(f);
199 out:
200         return error;
201 }
202
203 SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
204 {
205         return do_sys_ftruncate(fd, length, 1);
206 }
207
208 #ifdef CONFIG_COMPAT
209 COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length)
210 {
211         return do_sys_ftruncate(fd, length, 1);
212 }
213 #endif
214
215 /* LFS versions of truncate are only needed on 32 bit machines */
216 #if BITS_PER_LONG == 32
217 SYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length)
218 {
219         return do_sys_truncate(path, length);
220 }
221
222 SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length)
223 {
224         return do_sys_ftruncate(fd, length, 0);
225 }
226 #endif /* BITS_PER_LONG == 32 */
227
228
229 int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
230 {
231         struct inode *inode = file_inode(file);
232         long ret;
233
234         if (offset < 0 || len <= 0)
235                 return -EINVAL;
236
237         /* Return error if mode is not supported */
238         if (mode & ~FALLOC_FL_SUPPORTED_MASK)
239                 return -EOPNOTSUPP;
240
241         /* Punch hole and zero range are mutually exclusive */
242         if ((mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) ==
243             (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
244                 return -EOPNOTSUPP;
245
246         /* Punch hole must have keep size set */
247         if ((mode & FALLOC_FL_PUNCH_HOLE) &&
248             !(mode & FALLOC_FL_KEEP_SIZE))
249                 return -EOPNOTSUPP;
250
251         /* Collapse range should only be used exclusively. */
252         if ((mode & FALLOC_FL_COLLAPSE_RANGE) &&
253             (mode & ~FALLOC_FL_COLLAPSE_RANGE))
254                 return -EINVAL;
255
256         /* Insert range should only be used exclusively. */
257         if ((mode & FALLOC_FL_INSERT_RANGE) &&
258             (mode & ~FALLOC_FL_INSERT_RANGE))
259                 return -EINVAL;
260
261         if (!(file->f_mode & FMODE_WRITE))
262                 return -EBADF;
263
264         /*
265          * We can only allow pure fallocate on append only files
266          */
267         if ((mode & ~FALLOC_FL_KEEP_SIZE) && IS_APPEND(inode))
268                 return -EPERM;
269
270         if (IS_IMMUTABLE(inode))
271                 return -EPERM;
272
273         /*
274          * We cannot allow any fallocate operation on an active swapfile
275          */
276         if (IS_SWAPFILE(inode))
277                 return -ETXTBSY;
278
279         /*
280          * Revalidate the write permissions, in case security policy has
281          * changed since the files were opened.
282          */
283         ret = security_file_permission(file, MAY_WRITE);
284         if (ret)
285                 return ret;
286
287         if (S_ISFIFO(inode->i_mode))
288                 return -ESPIPE;
289
290         /*
291          * Let individual file system decide if it supports preallocation
292          * for directories or not.
293          */
294         if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
295                 return -ENODEV;
296
297         /* Check for wrap through zero too */
298         if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
299                 return -EFBIG;
300
301         if (!file->f_op->fallocate)
302                 return -EOPNOTSUPP;
303
304         sb_start_write(inode->i_sb);
305         ret = file->f_op->fallocate(file, mode, offset, len);
306
307         /*
308          * Create inotify and fanotify events.
309          *
310          * To keep the logic simple always create events if fallocate succeeds.
311          * This implies that events are even created if the file size remains
312          * unchanged, e.g. when using flag FALLOC_FL_KEEP_SIZE.
313          */
314         if (ret == 0)
315                 fsnotify_modify(file);
316
317         sb_end_write(inode->i_sb);
318         return ret;
319 }
320 EXPORT_SYMBOL_GPL(vfs_fallocate);
321
322 SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
323 {
324         struct fd f = fdget(fd);
325         int error = -EBADF;
326
327         if (f.file) {
328                 error = vfs_fallocate(f.file, mode, offset, len);
329                 fdput(f);
330         }
331         return error;
332 }
333
334 /*
335  * access() needs to use the real uid/gid, not the effective uid/gid.
336  * We do this by temporarily clearing all FS-related capabilities and
337  * switching the fsuid/fsgid around to the real ones.
338  */
339 SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
340 {
341         const struct cred *old_cred;
342         struct cred *override_cred;
343         struct path path;
344         struct inode *inode;
345         struct vfsmount *mnt;
346         int res;
347         unsigned int lookup_flags = LOOKUP_FOLLOW;
348
349         if (mode & ~S_IRWXO)    /* where's F_OK, X_OK, W_OK, R_OK? */
350                 return -EINVAL;
351
352         override_cred = prepare_creds();
353         if (!override_cred)
354                 return -ENOMEM;
355
356         override_cred->fsuid = override_cred->uid;
357         override_cred->fsgid = override_cred->gid;
358
359         if (!issecure(SECURE_NO_SETUID_FIXUP)) {
360                 /* Clear the capabilities if we switch to a non-root user */
361                 kuid_t root_uid = make_kuid(override_cred->user_ns, 0);
362                 if (!uid_eq(override_cred->uid, root_uid))
363                         cap_clear(override_cred->cap_effective);
364                 else
365                         override_cred->cap_effective =
366                                 override_cred->cap_permitted;
367         }
368
369         old_cred = override_creds(override_cred);
370 retry:
371         res = user_path_at(dfd, filename, lookup_flags, &path);
372         if (res)
373                 goto out;
374
375         inode = d_backing_inode(path.dentry);
376         mnt = path.mnt;
377
378         if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
379                 /*
380                  * MAY_EXEC on regular files is denied if the fs is mounted
381                  * with the "noexec" flag.
382                  */
383                 res = -EACCES;
384                 if (path_noexec(&path))
385                         goto out_path_release;
386         }
387
388         res = inode_permission2(mnt, inode, mode | MAY_ACCESS);
389         /* SuS v2 requires we report a read only fs too */
390         if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
391                 goto out_path_release;
392         /*
393          * This is a rare case where using __mnt_is_readonly()
394          * is OK without a mnt_want/drop_write() pair.  Since
395          * no actual write to the fs is performed here, we do
396          * not need to telegraph to that to anyone.
397          *
398          * By doing this, we accept that this access is
399          * inherently racy and know that the fs may change
400          * state before we even see this result.
401          */
402         if (__mnt_is_readonly(path.mnt))
403                 res = -EROFS;
404
405 out_path_release:
406         path_put(&path);
407         if (retry_estale(res, lookup_flags)) {
408                 lookup_flags |= LOOKUP_REVAL;
409                 goto retry;
410         }
411 out:
412         revert_creds(old_cred);
413         put_cred(override_cred);
414         return res;
415 }
416
417 SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
418 {
419         return sys_faccessat(AT_FDCWD, filename, mode);
420 }
421
422 SYSCALL_DEFINE1(chdir, const char __user *, filename)
423 {
424         struct path path;
425         int error;
426         unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
427 retry:
428         error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
429         if (error)
430                 goto out;
431
432         error = inode_permission2(path.mnt, path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
433         if (error)
434                 goto dput_and_out;
435
436         set_fs_pwd(current->fs, &path);
437
438 dput_and_out:
439         path_put(&path);
440         if (retry_estale(error, lookup_flags)) {
441                 lookup_flags |= LOOKUP_REVAL;
442                 goto retry;
443         }
444 out:
445         return error;
446 }
447
448 SYSCALL_DEFINE1(fchdir, unsigned int, fd)
449 {
450         struct fd f = fdget_raw(fd);
451         struct inode *inode;
452         struct vfsmount *mnt;
453         int error = -EBADF;
454
455         error = -EBADF;
456         if (!f.file)
457                 goto out;
458
459         inode = file_inode(f.file);
460         mnt = f.file->f_path.mnt;
461
462         error = -ENOTDIR;
463         if (!S_ISDIR(inode->i_mode))
464                 goto out_putf;
465
466         error = inode_permission2(mnt, inode, MAY_EXEC | MAY_CHDIR);
467         if (!error)
468                 set_fs_pwd(current->fs, &f.file->f_path);
469 out_putf:
470         fdput(f);
471 out:
472         return error;
473 }
474
475 SYSCALL_DEFINE1(chroot, const char __user *, filename)
476 {
477         struct path path;
478         int error;
479         unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
480 retry:
481         error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
482         if (error)
483                 goto out;
484
485         error = inode_permission2(path.mnt, path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
486         if (error)
487                 goto dput_and_out;
488
489         error = -EPERM;
490         if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
491                 goto dput_and_out;
492         error = security_path_chroot(&path);
493         if (error)
494                 goto dput_and_out;
495
496         set_fs_root(current->fs, &path);
497         error = 0;
498 dput_and_out:
499         path_put(&path);
500         if (retry_estale(error, lookup_flags)) {
501                 lookup_flags |= LOOKUP_REVAL;
502                 goto retry;
503         }
504 out:
505         return error;
506 }
507
508 static int chmod_common(struct path *path, umode_t mode)
509 {
510         struct inode *inode = path->dentry->d_inode;
511         struct inode *delegated_inode = NULL;
512         struct iattr newattrs;
513         int error;
514
515         error = mnt_want_write(path->mnt);
516         if (error)
517                 return error;
518 retry_deleg:
519         mutex_lock(&inode->i_mutex);
520         error = security_path_chmod(path, mode);
521         if (error)
522                 goto out_unlock;
523         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
524         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
525         error = notify_change(path->dentry, &newattrs, &delegated_inode);
526 out_unlock:
527         mutex_unlock(&inode->i_mutex);
528         if (delegated_inode) {
529                 error = break_deleg_wait(&delegated_inode);
530                 if (!error)
531                         goto retry_deleg;
532         }
533         mnt_drop_write(path->mnt);
534         return error;
535 }
536
537 SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
538 {
539         struct fd f = fdget(fd);
540         int err = -EBADF;
541
542         if (f.file) {
543                 audit_file(f.file);
544                 err = chmod_common(&f.file->f_path, mode);
545                 fdput(f);
546         }
547         return err;
548 }
549
550 SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode)
551 {
552         struct path path;
553         int error;
554         unsigned int lookup_flags = LOOKUP_FOLLOW;
555 retry:
556         error = user_path_at(dfd, filename, lookup_flags, &path);
557         if (!error) {
558                 error = chmod_common(&path, mode);
559                 path_put(&path);
560                 if (retry_estale(error, lookup_flags)) {
561                         lookup_flags |= LOOKUP_REVAL;
562                         goto retry;
563                 }
564         }
565         return error;
566 }
567
568 SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
569 {
570         return sys_fchmodat(AT_FDCWD, filename, mode);
571 }
572
573 static int chown_common(struct path *path, uid_t user, gid_t group)
574 {
575         struct inode *inode = path->dentry->d_inode;
576         struct inode *delegated_inode = NULL;
577         int error;
578         struct iattr newattrs;
579         kuid_t uid;
580         kgid_t gid;
581
582         uid = make_kuid(current_user_ns(), user);
583         gid = make_kgid(current_user_ns(), group);
584
585 retry_deleg:
586         newattrs.ia_valid =  ATTR_CTIME;
587         if (user != (uid_t) -1) {
588                 if (!uid_valid(uid))
589                         return -EINVAL;
590                 newattrs.ia_valid |= ATTR_UID;
591                 newattrs.ia_uid = uid;
592         }
593         if (group != (gid_t) -1) {
594                 if (!gid_valid(gid))
595                         return -EINVAL;
596                 newattrs.ia_valid |= ATTR_GID;
597                 newattrs.ia_gid = gid;
598         }
599         if (!S_ISDIR(inode->i_mode))
600                 newattrs.ia_valid |=
601                         ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
602         mutex_lock(&inode->i_mutex);
603         error = security_path_chown(path, uid, gid);
604         if (!error)
605                 error = notify_change(path->dentry, &newattrs, &delegated_inode);
606         mutex_unlock(&inode->i_mutex);
607         if (delegated_inode) {
608                 error = break_deleg_wait(&delegated_inode);
609                 if (!error)
610                         goto retry_deleg;
611         }
612         return error;
613 }
614
615 SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
616                 gid_t, group, int, flag)
617 {
618         struct path path;
619         int error = -EINVAL;
620         int lookup_flags;
621
622         if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
623                 goto out;
624
625         lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
626         if (flag & AT_EMPTY_PATH)
627                 lookup_flags |= LOOKUP_EMPTY;
628 retry:
629         error = user_path_at(dfd, filename, lookup_flags, &path);
630         if (error)
631                 goto out;
632         error = mnt_want_write(path.mnt);
633         if (error)
634                 goto out_release;
635         error = chown_common(&path, user, group);
636         mnt_drop_write(path.mnt);
637 out_release:
638         path_put(&path);
639         if (retry_estale(error, lookup_flags)) {
640                 lookup_flags |= LOOKUP_REVAL;
641                 goto retry;
642         }
643 out:
644         return error;
645 }
646
647 SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
648 {
649         return sys_fchownat(AT_FDCWD, filename, user, group, 0);
650 }
651
652 SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
653 {
654         return sys_fchownat(AT_FDCWD, filename, user, group,
655                             AT_SYMLINK_NOFOLLOW);
656 }
657
658 SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
659 {
660         struct fd f = fdget(fd);
661         int error = -EBADF;
662
663         if (!f.file)
664                 goto out;
665
666         error = mnt_want_write_file(f.file);
667         if (error)
668                 goto out_fput;
669         audit_file(f.file);
670         error = chown_common(&f.file->f_path, user, group);
671         mnt_drop_write_file(f.file);
672 out_fput:
673         fdput(f);
674 out:
675         return error;
676 }
677
678 int open_check_o_direct(struct file *f)
679 {
680         /* NB: we're sure to have correct a_ops only after f_op->open */
681         if (f->f_flags & O_DIRECT) {
682                 if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO)
683                         return -EINVAL;
684         }
685         return 0;
686 }
687
688 static int do_dentry_open(struct file *f,
689                           struct inode *inode,
690                           int (*open)(struct inode *, struct file *),
691                           const struct cred *cred)
692 {
693         static const struct file_operations empty_fops = {};
694         int error;
695
696         f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
697                                 FMODE_PREAD | FMODE_PWRITE;
698
699         path_get(&f->f_path);
700         f->f_inode = inode;
701         f->f_mapping = inode->i_mapping;
702
703         if (unlikely(f->f_flags & O_PATH)) {
704                 f->f_mode = FMODE_PATH;
705                 f->f_op = &empty_fops;
706                 return 0;
707         }
708
709         if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
710                 error = get_write_access(inode);
711                 if (unlikely(error))
712                         goto cleanup_file;
713                 error = __mnt_want_write(f->f_path.mnt);
714                 if (unlikely(error)) {
715                         put_write_access(inode);
716                         goto cleanup_file;
717                 }
718                 f->f_mode |= FMODE_WRITER;
719         }
720
721         /* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
722         if (S_ISREG(inode->i_mode))
723                 f->f_mode |= FMODE_ATOMIC_POS;
724
725         f->f_op = fops_get(inode->i_fop);
726         if (unlikely(WARN_ON(!f->f_op))) {
727                 error = -ENODEV;
728                 goto cleanup_all;
729         }
730
731         error = security_file_open(f, cred);
732         if (error)
733                 goto cleanup_all;
734
735         error = break_lease(inode, f->f_flags);
736         if (error)
737                 goto cleanup_all;
738
739         if (!open)
740                 open = f->f_op->open;
741         if (open) {
742                 error = open(inode, f);
743                 if (error)
744                         goto cleanup_all;
745         }
746         if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
747                 i_readcount_inc(inode);
748         if ((f->f_mode & FMODE_READ) &&
749              likely(f->f_op->read || f->f_op->read_iter))
750                 f->f_mode |= FMODE_CAN_READ;
751         if ((f->f_mode & FMODE_WRITE) &&
752              likely(f->f_op->write || f->f_op->write_iter))
753                 f->f_mode |= FMODE_CAN_WRITE;
754
755         f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
756
757         file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
758
759         return 0;
760
761 cleanup_all:
762         fops_put(f->f_op);
763         if (f->f_mode & FMODE_WRITER) {
764                 put_write_access(inode);
765                 __mnt_drop_write(f->f_path.mnt);
766         }
767 cleanup_file:
768         path_put(&f->f_path);
769         f->f_path.mnt = NULL;
770         f->f_path.dentry = NULL;
771         f->f_inode = NULL;
772         return error;
773 }
774
775 /**
776  * finish_open - finish opening a file
777  * @file: file pointer
778  * @dentry: pointer to dentry
779  * @open: open callback
780  * @opened: state of open
781  *
782  * This can be used to finish opening a file passed to i_op->atomic_open().
783  *
784  * If the open callback is set to NULL, then the standard f_op->open()
785  * filesystem callback is substituted.
786  *
787  * NB: the dentry reference is _not_ consumed.  If, for example, the dentry is
788  * the return value of d_splice_alias(), then the caller needs to perform dput()
789  * on it after finish_open().
790  *
791  * On successful return @file is a fully instantiated open file.  After this, if
792  * an error occurs in ->atomic_open(), it needs to clean up with fput().
793  *
794  * Returns zero on success or -errno if the open failed.
795  */
796 int finish_open(struct file *file, struct dentry *dentry,
797                 int (*open)(struct inode *, struct file *),
798                 int *opened)
799 {
800         int error;
801         BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
802
803         file->f_path.dentry = dentry;
804         error = do_dentry_open(file, d_backing_inode(dentry), open,
805                                current_cred());
806         if (!error)
807                 *opened |= FILE_OPENED;
808
809         return error;
810 }
811 EXPORT_SYMBOL(finish_open);
812
813 /**
814  * finish_no_open - finish ->atomic_open() without opening the file
815  *
816  * @file: file pointer
817  * @dentry: dentry or NULL (as returned from ->lookup())
818  *
819  * This can be used to set the result of a successful lookup in ->atomic_open().
820  *
821  * NB: unlike finish_open() this function does consume the dentry reference and
822  * the caller need not dput() it.
823  *
824  * Returns "1" which must be the return value of ->atomic_open() after having
825  * called this function.
826  */
827 int finish_no_open(struct file *file, struct dentry *dentry)
828 {
829         file->f_path.dentry = dentry;
830         return 1;
831 }
832 EXPORT_SYMBOL(finish_no_open);
833
834 char *file_path(struct file *filp, char *buf, int buflen)
835 {
836         return d_path(&filp->f_path, buf, buflen);
837 }
838 EXPORT_SYMBOL(file_path);
839
840 /**
841  * vfs_open - open the file at the given path
842  * @path: path to open
843  * @file: newly allocated file with f_flag initialized
844  * @cred: credentials to use
845  */
846 int vfs_open(const struct path *path, struct file *file,
847              const struct cred *cred)
848 {
849         struct inode *inode = vfs_select_inode(path->dentry, file->f_flags);
850
851         if (IS_ERR(inode))
852                 return PTR_ERR(inode);
853
854         file->f_path = *path;
855         return do_dentry_open(file, inode, NULL, cred);
856 }
857
858 struct file *dentry_open(const struct path *path, int flags,
859                          const struct cred *cred)
860 {
861         int error;
862         struct file *f;
863
864         validate_creds(cred);
865
866         /* We must always pass in a valid mount pointer. */
867         BUG_ON(!path->mnt);
868
869         f = get_empty_filp();
870         if (!IS_ERR(f)) {
871                 f->f_flags = flags;
872                 error = vfs_open(path, f, cred);
873                 if (!error) {
874                         /* from now on we need fput() to dispose of f */
875                         error = open_check_o_direct(f);
876                         if (error) {
877                                 fput(f);
878                                 f = ERR_PTR(error);
879                         }
880                 } else { 
881                         put_filp(f);
882                         f = ERR_PTR(error);
883                 }
884         }
885         return f;
886 }
887 EXPORT_SYMBOL(dentry_open);
888
889 static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
890 {
891         int lookup_flags = 0;
892         int acc_mode;
893
894         if (flags & (O_CREAT | __O_TMPFILE))
895                 op->mode = (mode & S_IALLUGO) | S_IFREG;
896         else
897                 op->mode = 0;
898
899         /* Must never be set by userspace */
900         flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC;
901
902         /*
903          * O_SYNC is implemented as __O_SYNC|O_DSYNC.  As many places only
904          * check for O_DSYNC if the need any syncing at all we enforce it's
905          * always set instead of having to deal with possibly weird behaviour
906          * for malicious applications setting only __O_SYNC.
907          */
908         if (flags & __O_SYNC)
909                 flags |= O_DSYNC;
910
911         if (flags & __O_TMPFILE) {
912                 if ((flags & O_TMPFILE_MASK) != O_TMPFILE)
913                         return -EINVAL;
914                 acc_mode = MAY_OPEN | ACC_MODE(flags);
915                 if (!(acc_mode & MAY_WRITE))
916                         return -EINVAL;
917         } else if (flags & O_PATH) {
918                 /*
919                  * If we have O_PATH in the open flag. Then we
920                  * cannot have anything other than the below set of flags
921                  */
922                 flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
923                 acc_mode = 0;
924         } else {
925                 acc_mode = MAY_OPEN | ACC_MODE(flags);
926         }
927
928         op->open_flag = flags;
929
930         /* O_TRUNC implies we need access checks for write permissions */
931         if (flags & O_TRUNC)
932                 acc_mode |= MAY_WRITE;
933
934         /* Allow the LSM permission hook to distinguish append
935            access from general write access. */
936         if (flags & O_APPEND)
937                 acc_mode |= MAY_APPEND;
938
939         op->acc_mode = acc_mode;
940
941         op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
942
943         if (flags & O_CREAT) {
944                 op->intent |= LOOKUP_CREATE;
945                 if (flags & O_EXCL)
946                         op->intent |= LOOKUP_EXCL;
947         }
948
949         if (flags & O_DIRECTORY)
950                 lookup_flags |= LOOKUP_DIRECTORY;
951         if (!(flags & O_NOFOLLOW))
952                 lookup_flags |= LOOKUP_FOLLOW;
953         op->lookup_flags = lookup_flags;
954         return 0;
955 }
956
957 /**
958  * file_open_name - open file and return file pointer
959  *
960  * @name:       struct filename containing path to open
961  * @flags:      open flags as per the open(2) second argument
962  * @mode:       mode for the new file if O_CREAT is set, else ignored
963  *
964  * This is the helper to open a file from kernelspace if you really
965  * have to.  But in generally you should not do this, so please move
966  * along, nothing to see here..
967  */
968 struct file *file_open_name(struct filename *name, int flags, umode_t mode)
969 {
970         struct open_flags op;
971         int err = build_open_flags(flags, mode, &op);
972         return err ? ERR_PTR(err) : do_filp_open(AT_FDCWD, name, &op);
973 }
974
975 /**
976  * filp_open - open file and return file pointer
977  *
978  * @filename:   path to open
979  * @flags:      open flags as per the open(2) second argument
980  * @mode:       mode for the new file if O_CREAT is set, else ignored
981  *
982  * This is the helper to open a file from kernelspace if you really
983  * have to.  But in generally you should not do this, so please move
984  * along, nothing to see here..
985  */
986 struct file *filp_open(const char *filename, int flags, umode_t mode)
987 {
988         struct filename *name = getname_kernel(filename);
989         struct file *file = ERR_CAST(name);
990         
991         if (!IS_ERR(name)) {
992                 file = file_open_name(name, flags, mode);
993                 putname(name);
994         }
995         return file;
996 }
997 EXPORT_SYMBOL(filp_open);
998
999 struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
1000                             const char *filename, int flags, umode_t mode)
1001 {
1002         struct open_flags op;
1003         int err = build_open_flags(flags, mode, &op);
1004         if (err)
1005                 return ERR_PTR(err);
1006         return do_file_open_root(dentry, mnt, filename, &op);
1007 }
1008 EXPORT_SYMBOL(file_open_root);
1009
1010 long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
1011 {
1012         struct open_flags op;
1013         int fd = build_open_flags(flags, mode, &op);
1014         struct filename *tmp;
1015
1016         if (fd)
1017                 return fd;
1018
1019         tmp = getname(filename);
1020         if (IS_ERR(tmp))
1021                 return PTR_ERR(tmp);
1022
1023         fd = get_unused_fd_flags(flags);
1024         if (fd >= 0) {
1025                 struct file *f = do_filp_open(dfd, tmp, &op);
1026                 if (IS_ERR(f)) {
1027                         put_unused_fd(fd);
1028                         fd = PTR_ERR(f);
1029                 } else {
1030                         fsnotify_open(f);
1031                         fd_install(fd, f);
1032                 }
1033         }
1034         putname(tmp);
1035         return fd;
1036 }
1037
1038 SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
1039 {
1040         if (force_o_largefile())
1041                 flags |= O_LARGEFILE;
1042
1043         return do_sys_open(AT_FDCWD, filename, flags, mode);
1044 }
1045
1046 SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
1047                 umode_t, mode)
1048 {
1049         if (force_o_largefile())
1050                 flags |= O_LARGEFILE;
1051
1052         return do_sys_open(dfd, filename, flags, mode);
1053 }
1054
1055 #ifndef __alpha__
1056
1057 /*
1058  * For backward compatibility?  Maybe this should be moved
1059  * into arch/i386 instead?
1060  */
1061 SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
1062 {
1063         return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1064 }
1065
1066 #endif
1067
1068 /*
1069  * "id" is the POSIX thread ID. We use the
1070  * files pointer for this..
1071  */
1072 int filp_close(struct file *filp, fl_owner_t id)
1073 {
1074         int retval = 0;
1075
1076         if (!file_count(filp)) {
1077                 printk(KERN_ERR "VFS: Close: file count is 0\n");
1078                 return 0;
1079         }
1080
1081         if (filp->f_op->flush)
1082                 retval = filp->f_op->flush(filp, id);
1083
1084         if (likely(!(filp->f_mode & FMODE_PATH))) {
1085                 dnotify_flush(filp, id);
1086                 locks_remove_posix(filp, id);
1087         }
1088         fput(filp);
1089         return retval;
1090 }
1091
1092 EXPORT_SYMBOL(filp_close);
1093
1094 /*
1095  * Careful here! We test whether the file pointer is NULL before
1096  * releasing the fd. This ensures that one clone task can't release
1097  * an fd while another clone is opening it.
1098  */
1099 SYSCALL_DEFINE1(close, unsigned int, fd)
1100 {
1101         int retval = __close_fd(current->files, fd);
1102
1103         /* can't restart close syscall because file table entry was cleared */
1104         if (unlikely(retval == -ERESTARTSYS ||
1105                      retval == -ERESTARTNOINTR ||
1106                      retval == -ERESTARTNOHAND ||
1107                      retval == -ERESTART_RESTARTBLOCK))
1108                 retval = -EINTR;
1109
1110         return retval;
1111 }
1112 EXPORT_SYMBOL(sys_close);
1113
1114 /*
1115  * This routine simulates a hangup on the tty, to arrange that users
1116  * are given clean terminals at login time.
1117  */
1118 SYSCALL_DEFINE0(vhangup)
1119 {
1120         if (capable(CAP_SYS_TTY_CONFIG)) {
1121                 tty_vhangup_self();
1122                 return 0;
1123         }
1124         return -EPERM;
1125 }
1126
1127 /*
1128  * Called when an inode is about to be open.
1129  * We use this to disallow opening large files on 32bit systems if
1130  * the caller didn't specify O_LARGEFILE.  On 64bit systems we force
1131  * on this flag in sys_open.
1132  */
1133 int generic_file_open(struct inode * inode, struct file * filp)
1134 {
1135         if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1136                 return -EOVERFLOW;
1137         return 0;
1138 }
1139
1140 EXPORT_SYMBOL(generic_file_open);
1141
1142 /*
1143  * This is used by subsystems that don't want seekable
1144  * file descriptors. The function is not supposed to ever fail, the only
1145  * reason it returns an 'int' and not 'void' is so that it can be plugged
1146  * directly into file_operations structure.
1147  */
1148 int nonseekable_open(struct inode *inode, struct file *filp)
1149 {
1150         filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1151         return 0;
1152 }
1153
1154 EXPORT_SYMBOL(nonseekable_open);