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