1
2
3
4
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
37int 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
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
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
63 ret = notify_change(dentry, &newattrs, NULL);
64 mutex_unlock(&dentry->d_inode->i_mutex);
65 return ret;
66}
67
68long vfs_truncate(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
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
96
97
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
110
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
122put_write_and_out:
123 put_write_access(upperdentry->d_inode);
124mnt_drop_write_and_out:
125 mnt_drop_write(path->mnt);
126out:
127 return error;
128}
129EXPORT_SYMBOL_GPL(vfs_truncate);
130
131static 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)
138 return -EINVAL;
139
140retry:
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
153SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
154{
155 return do_sys_truncate(path, length);
156}
157
158#ifdef CONFIG_COMPAT
159COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length)
160{
161 return do_sys_truncate(path, length);
162}
163#endif
164
165static 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
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
192 if (small && length > MAX_NON_LFS)
193 goto out_putf;
194
195 error = -EPERM;
196
197 if (IS_APPEND(file_inode(f.file)))
198 goto out_putf;
199
200 sb_start_write(inode->i_sb);
201 error = locks_verify_truncate(inode, f.file, length);
202 if (!error)
203 error = security_path_truncate(&f.file->f_path);
204 if (!error)
205 error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file);
206 sb_end_write(inode->i_sb);
207out_putf:
208 fdput(f);
209out:
210 return error;
211}
212
213SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
214{
215 return do_sys_ftruncate(fd, length, 1);
216}
217
218#ifdef CONFIG_COMPAT
219COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length)
220{
221 return do_sys_ftruncate(fd, length, 1);
222}
223#endif
224
225
226#if BITS_PER_LONG == 32
227SYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length)
228{
229 return do_sys_truncate(path, length);
230}
231
232SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length)
233{
234 return do_sys_ftruncate(fd, length, 0);
235}
236#endif
237
238
239int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
240{
241 struct inode *inode = file_inode(file);
242 long ret;
243
244 if (offset < 0 || len <= 0)
245 return -EINVAL;
246
247
248 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
249 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE))
250 return -EOPNOTSUPP;
251
252
253 if ((mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) ==
254 (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
255 return -EOPNOTSUPP;
256
257
258 if ((mode & FALLOC_FL_PUNCH_HOLE) &&
259 !(mode & FALLOC_FL_KEEP_SIZE))
260 return -EOPNOTSUPP;
261
262
263 if ((mode & FALLOC_FL_COLLAPSE_RANGE) &&
264 (mode & ~FALLOC_FL_COLLAPSE_RANGE))
265 return -EINVAL;
266
267 if (!(file->f_mode & FMODE_WRITE))
268 return -EBADF;
269
270
271
272
273
274 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE)
275 && IS_APPEND(inode))
276 return -EPERM;
277
278 if (IS_IMMUTABLE(inode))
279 return -EPERM;
280
281
282
283
284
285 if (IS_SWAPFILE(inode))
286 ret = -ETXTBSY;
287
288
289
290
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
301
302
303 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
304 return -ENODEV;
305
306
307 if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
308 return -EFBIG;
309
310 if (!file->f_op->fallocate)
311 return -EOPNOTSUPP;
312
313 sb_start_write(inode->i_sb);
314 ret = file->f_op->fallocate(file, mode, offset, len);
315 sb_end_write(inode->i_sb);
316 return ret;
317}
318EXPORT_SYMBOL_GPL(vfs_fallocate);
319
320SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
321{
322 struct fd f = fdget(fd);
323 int error = -EBADF;
324
325 if (f.file) {
326 error = vfs_fallocate(f.file, mode, offset, len);
327 fdput(f);
328 }
329 return error;
330}
331
332
333
334
335
336
337SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
338{
339 const struct cred *old_cred;
340 struct cred *override_cred;
341 struct path path;
342 struct inode *inode;
343 int res;
344 unsigned int lookup_flags = LOOKUP_FOLLOW;
345
346 if (mode & ~S_IRWXO)
347 return -EINVAL;
348
349 override_cred = prepare_creds();
350 if (!override_cred)
351 return -ENOMEM;
352
353 override_cred->fsuid = override_cred->uid;
354 override_cred->fsgid = override_cred->gid;
355
356 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
357
358 kuid_t root_uid = make_kuid(override_cred->user_ns, 0);
359 if (!uid_eq(override_cred->uid, root_uid))
360 cap_clear(override_cred->cap_effective);
361 else
362 override_cred->cap_effective =
363 override_cred->cap_permitted;
364 }
365
366 old_cred = override_creds(override_cred);
367retry:
368 res = user_path_at(dfd, filename, lookup_flags, &path);
369 if (res)
370 goto out;
371
372 inode = path.dentry->d_inode;
373
374 if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
375
376
377
378
379 res = -EACCES;
380 if (path_noexec(&path))
381 goto out_path_release;
382 }
383
384 res = inode_permission(inode, mode | MAY_ACCESS);
385
386 if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
387 goto out_path_release;
388
389
390
391
392
393
394
395
396
397
398 if (__mnt_is_readonly(path.mnt))
399 res = -EROFS;
400
401out_path_release:
402 path_put(&path);
403 if (retry_estale(res, lookup_flags)) {
404 lookup_flags |= LOOKUP_REVAL;
405 goto retry;
406 }
407out:
408 revert_creds(old_cred);
409 put_cred(override_cred);
410 return res;
411}
412
413SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
414{
415 return sys_faccessat(AT_FDCWD, filename, mode);
416}
417
418SYSCALL_DEFINE1(chdir, const char __user *, filename)
419{
420 struct path path;
421 int error;
422 unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
423retry:
424 error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
425 if (error)
426 goto out;
427
428 error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
429 if (error)
430 goto dput_and_out;
431
432 set_fs_pwd(current->fs, &path);
433
434dput_and_out:
435 path_put(&path);
436 if (retry_estale(error, lookup_flags)) {
437 lookup_flags |= LOOKUP_REVAL;
438 goto retry;
439 }
440out:
441 return error;
442}
443
444SYSCALL_DEFINE1(fchdir, unsigned int, fd)
445{
446 struct fd f = fdget_raw(fd);
447 int error;
448
449 error = -EBADF;
450 if (!f.file)
451 goto out;
452
453 error = -ENOTDIR;
454 if (!d_can_lookup(f.file->f_path.dentry))
455 goto out_putf;
456
457 error = inode_permission(file_inode(f.file), MAY_EXEC | MAY_CHDIR);
458 if (!error)
459 set_fs_pwd(current->fs, &f.file->f_path);
460out_putf:
461 fdput(f);
462out:
463 return error;
464}
465
466SYSCALL_DEFINE1(chroot, const char __user *, filename)
467{
468 struct path path;
469 int error;
470 unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
471retry:
472 error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
473 if (error)
474 goto out;
475
476 error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
477 if (error)
478 goto dput_and_out;
479
480 error = -EPERM;
481 if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
482 goto dput_and_out;
483 error = security_path_chroot(&path);
484 if (error)
485 goto dput_and_out;
486
487 set_fs_root(current->fs, &path);
488 error = 0;
489dput_and_out:
490 path_put(&path);
491 if (retry_estale(error, lookup_flags)) {
492 lookup_flags |= LOOKUP_REVAL;
493 goto retry;
494 }
495out:
496 return error;
497}
498
499static int chmod_common(struct path *path, umode_t mode)
500{
501 struct inode *inode = path->dentry->d_inode;
502 struct inode *delegated_inode = NULL;
503 struct iattr newattrs;
504 int error;
505
506 error = mnt_want_write(path->mnt);
507 if (error)
508 return error;
509retry_deleg:
510 mutex_lock(&inode->i_mutex);
511 error = security_path_chmod(path, mode);
512 if (error)
513 goto out_unlock;
514 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
515 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
516 error = notify_change(path->dentry, &newattrs, &delegated_inode);
517out_unlock:
518 mutex_unlock(&inode->i_mutex);
519 if (delegated_inode) {
520 error = break_deleg_wait(&delegated_inode);
521 if (!error)
522 goto retry_deleg;
523 }
524 mnt_drop_write(path->mnt);
525 return error;
526}
527
528SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
529{
530 struct file * file;
531 int err = -EBADF;
532
533 file = fget(fd);
534 if (file) {
535 audit_inode(NULL, file->f_path.dentry, 0);
536 err = chmod_common(&file->f_path, mode);
537 fput(file);
538 }
539 return err;
540}
541
542SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode)
543{
544 struct path path;
545 int error;
546 unsigned int lookup_flags = LOOKUP_FOLLOW;
547retry:
548 error = user_path_at(dfd, filename, lookup_flags, &path);
549 if (!error) {
550 error = chmod_common(&path, mode);
551 path_put(&path);
552 if (retry_estale(error, lookup_flags)) {
553 lookup_flags |= LOOKUP_REVAL;
554 goto retry;
555 }
556 }
557 return error;
558}
559
560SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
561{
562 return sys_fchmodat(AT_FDCWD, filename, mode);
563}
564
565static int chown_common(struct path *path, uid_t user, gid_t group)
566{
567 struct inode *inode = path->dentry->d_inode;
568 struct inode *delegated_inode = NULL;
569 int error;
570 struct iattr newattrs;
571 kuid_t uid;
572 kgid_t gid;
573
574 uid = make_kuid(current_user_ns(), user);
575 gid = make_kgid(current_user_ns(), group);
576
577retry_deleg:
578 newattrs.ia_valid = ATTR_CTIME;
579 if (user != (uid_t) -1) {
580 if (!uid_valid(uid))
581 return -EINVAL;
582 newattrs.ia_valid |= ATTR_UID;
583 newattrs.ia_uid = uid;
584 }
585 if (group != (gid_t) -1) {
586 if (!gid_valid(gid))
587 return -EINVAL;
588 newattrs.ia_valid |= ATTR_GID;
589 newattrs.ia_gid = gid;
590 }
591 if (!S_ISDIR(inode->i_mode))
592 newattrs.ia_valid |=
593 ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
594 mutex_lock(&inode->i_mutex);
595 error = security_path_chown(path, uid, gid);
596 if (!error)
597 error = notify_change(path->dentry, &newattrs, &delegated_inode);
598 mutex_unlock(&inode->i_mutex);
599 if (delegated_inode) {
600 error = break_deleg_wait(&delegated_inode);
601 if (!error)
602 goto retry_deleg;
603 }
604 return error;
605}
606
607SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
608 gid_t, group, int, flag)
609{
610 struct path path;
611 int error = -EINVAL;
612 int lookup_flags;
613
614 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
615 goto out;
616
617 lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
618 if (flag & AT_EMPTY_PATH)
619 lookup_flags |= LOOKUP_EMPTY;
620retry:
621 error = user_path_at(dfd, filename, lookup_flags, &path);
622 if (error)
623 goto out;
624 error = mnt_want_write(path.mnt);
625 if (error)
626 goto out_release;
627 error = chown_common(&path, user, group);
628 mnt_drop_write(path.mnt);
629out_release:
630 path_put(&path);
631 if (retry_estale(error, lookup_flags)) {
632 lookup_flags |= LOOKUP_REVAL;
633 goto retry;
634 }
635out:
636 return error;
637}
638
639SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
640{
641 return sys_fchownat(AT_FDCWD, filename, user, group, 0);
642}
643
644SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
645{
646 return sys_fchownat(AT_FDCWD, filename, user, group,
647 AT_SYMLINK_NOFOLLOW);
648}
649
650SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
651{
652 struct fd f = fdget(fd);
653 int error = -EBADF;
654
655 if (!f.file)
656 goto out;
657
658 error = mnt_want_write_file(f.file);
659 if (error)
660 goto out_fput;
661 audit_inode(NULL, f.file->f_path.dentry, 0);
662 error = chown_common(&f.file->f_path, user, group);
663 mnt_drop_write_file(f.file);
664out_fput:
665 fdput(f);
666out:
667 return error;
668}
669
670
671
672
673
674
675
676static inline int __get_file_write_access(struct inode *inode,
677 struct vfsmount *mnt)
678{
679 int error;
680 error = get_write_access(inode);
681 if (error)
682 return error;
683
684
685
686
687
688 if (!special_file(inode->i_mode)) {
689
690
691
692 error = __mnt_want_write(mnt);
693 if (error)
694 put_write_access(inode);
695 }
696 return error;
697}
698
699int open_check_o_direct(struct file *f)
700{
701
702 if (f->f_flags & O_DIRECT) {
703 if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO)
704 return -EINVAL;
705 }
706 return 0;
707}
708
709static int do_dentry_open(struct file *f,
710 struct inode *inode,
711 int (*open)(struct inode *, struct file *),
712 const struct cred *cred)
713{
714 static const struct file_operations empty_fops = {};
715 int error;
716
717 f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
718 FMODE_PREAD | FMODE_PWRITE;
719
720 if (unlikely(f->f_flags & O_PATH))
721 f->f_mode = FMODE_PATH;
722
723 path_get(&f->f_path);
724 f->f_inode = inode;
725 if (f->f_mode & FMODE_WRITE) {
726 error = __get_file_write_access(inode, f->f_path.mnt);
727 if (error)
728 goto cleanup_file;
729 if (!special_file(inode->i_mode))
730 file_take_write(f);
731 }
732
733 f->f_mapping = inode->i_mapping;
734
735 if (unlikely(f->f_mode & FMODE_PATH)) {
736 f->f_op = &empty_fops;
737 return 0;
738 }
739
740
741 if (S_ISREG(inode->i_mode))
742 f->f_mode |= FMODE_ATOMIC_POS;
743
744 f->f_op = fops_get(inode->i_fop);
745
746 error = security_file_open(f, cred);
747 if (error)
748 goto cleanup_all;
749
750 error = break_lease(locks_inode(f), f->f_flags);
751 if (error)
752 goto cleanup_all;
753
754 if (!open && f->f_op)
755 open = f->f_op->open;
756 if (open) {
757 error = open(inode, f);
758 if (error)
759 goto cleanup_all;
760 }
761 if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
762 i_readcount_inc(inode);
763
764 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
765
766 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
767
768 return 0;
769
770cleanup_all:
771 fops_put(f->f_op);
772 if (f->f_mode & FMODE_WRITE) {
773 put_write_access(inode);
774 if (!special_file(inode->i_mode)) {
775
776
777
778
779
780
781 file_reset_write(f);
782 __mnt_drop_write(f->f_path.mnt);
783 }
784 }
785cleanup_file:
786 path_put(&f->f_path);
787 f->f_path.mnt = NULL;
788 f->f_path.dentry = NULL;
789 f->f_inode = NULL;
790 return error;
791}
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814int finish_open(struct file *file, struct dentry *dentry,
815 int (*open)(struct inode *, struct file *),
816 int *opened)
817{
818 int error;
819 BUG_ON(*opened & FILE_OPENED);
820
821 file->f_path.dentry = dentry;
822 error = do_dentry_open(file, dentry->d_inode, open,
823 current_cred());
824 if (!error)
825 *opened |= FILE_OPENED;
826
827 return error;
828}
829EXPORT_SYMBOL(finish_open);
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845int finish_no_open(struct file *file, struct dentry *dentry)
846{
847 file->f_path.dentry = dentry;
848 return 1;
849}
850EXPORT_SYMBOL(finish_no_open);
851
852struct file *dentry_open(const struct path *path, int flags,
853 const struct cred *cred)
854{
855 int error;
856 struct file *f;
857
858 validate_creds(cred);
859
860
861 BUG_ON(!path->mnt);
862
863 f = get_empty_filp();
864 if (!IS_ERR(f)) {
865 f->f_flags = flags;
866 error = vfs_open(path, f, cred);
867 if (!error) {
868
869 error = open_check_o_direct(f);
870 if (error) {
871 fput(f);
872 f = ERR_PTR(error);
873 }
874 } else {
875 put_filp(f);
876 f = ERR_PTR(error);
877 }
878 }
879 return f;
880}
881EXPORT_SYMBOL(dentry_open);
882
883
884
885
886
887
888
889int vfs_open(const struct path *path, struct file *filp,
890 const struct cred *cred)
891{
892 struct inode *inode = path->dentry->d_inode;
893 iop_dentry_open_t dentry_open = get_dentry_open_iop(inode);
894
895 if (dentry_open)
896 return dentry_open(path->dentry, filp, cred);
897 else {
898 struct dentry *dentry = d_real(path->dentry, NULL, filp->f_flags);
899
900 if (IS_ERR(dentry))
901 return PTR_ERR(dentry);
902
903 filp->f_path = *path;
904 return do_dentry_open(filp, dentry->d_inode, NULL, cred);
905 }
906}
907EXPORT_SYMBOL(vfs_open);
908
909static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
910{
911 int lookup_flags = 0;
912 int acc_mode;
913
914 if (flags & (O_CREAT | __O_TMPFILE))
915 op->mode = (mode & S_IALLUGO) | S_IFREG;
916 else
917 op->mode = 0;
918
919
920 flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC;
921
922
923
924
925
926
927
928 if (flags & __O_SYNC)
929 flags |= O_DSYNC;
930
931 if (flags & __O_TMPFILE) {
932 if ((flags & O_TMPFILE_MASK) != O_TMPFILE)
933 return -EINVAL;
934 acc_mode = MAY_OPEN | ACC_MODE(flags);
935 if (!(acc_mode & MAY_WRITE))
936 return -EINVAL;
937 } else if (flags & O_PATH) {
938
939
940
941
942 flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
943 acc_mode = 0;
944 } else {
945 acc_mode = MAY_OPEN | ACC_MODE(flags);
946 }
947
948 op->open_flag = flags;
949
950
951 if (flags & O_TRUNC)
952 acc_mode |= MAY_WRITE;
953
954
955
956 if (flags & O_APPEND)
957 acc_mode |= MAY_APPEND;
958
959 op->acc_mode = acc_mode;
960
961 op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
962
963 if (flags & O_CREAT) {
964 op->intent |= LOOKUP_CREATE;
965 if (flags & O_EXCL)
966 op->intent |= LOOKUP_EXCL;
967 }
968
969 if (flags & O_DIRECTORY)
970 lookup_flags |= LOOKUP_DIRECTORY;
971 if (!(flags & O_NOFOLLOW))
972 lookup_flags |= LOOKUP_FOLLOW;
973 op->lookup_flags = lookup_flags;
974 return 0;
975}
976
977
978
979
980
981
982
983
984
985
986
987
988struct file *file_open_name(struct filename *name, int flags, umode_t mode)
989{
990 struct open_flags op;
991 int err = build_open_flags(flags, mode, &op);
992 return err ? ERR_PTR(err) : do_filp_open(AT_FDCWD, name, &op);
993}
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006struct file *filp_open(const char *filename, int flags, umode_t mode)
1007{
1008 struct filename *name = getname_kernel(filename);
1009 struct file *file = ERR_CAST(name);
1010
1011 if (!IS_ERR(name)) {
1012 file = file_open_name(name, flags, mode);
1013 putname(name);
1014 }
1015 return file;
1016}
1017EXPORT_SYMBOL(filp_open);
1018
1019struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
1020 const char *filename, int flags)
1021{
1022 struct open_flags op;
1023 int err = build_open_flags(flags, 0, &op);
1024 if (err)
1025 return ERR_PTR(err);
1026 if (flags & O_CREAT)
1027 return ERR_PTR(-EINVAL);
1028 if (!filename && (flags & O_DIRECTORY))
1029 if (!dentry->d_inode->i_op->lookup)
1030 return ERR_PTR(-ENOTDIR);
1031 return do_file_open_root(dentry, mnt, filename, &op);
1032}
1033EXPORT_SYMBOL(file_open_root);
1034
1035long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
1036{
1037 struct open_flags op;
1038 int fd = build_open_flags(flags, mode, &op);
1039 struct filename *tmp;
1040
1041 if (fd)
1042 return fd;
1043
1044 tmp = getname(filename);
1045 if (IS_ERR(tmp))
1046 return PTR_ERR(tmp);
1047
1048 fd = get_unused_fd_flags(flags);
1049 if (fd >= 0) {
1050 struct file *f = do_filp_open(dfd, tmp, &op);
1051 if (IS_ERR(f)) {
1052 put_unused_fd(fd);
1053 fd = PTR_ERR(f);
1054 } else {
1055 fsnotify_open(f);
1056 fd_install(fd, f);
1057 }
1058 }
1059 putname(tmp);
1060 return fd;
1061}
1062
1063SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
1064{
1065 if (force_o_largefile())
1066 flags |= O_LARGEFILE;
1067
1068 return do_sys_open(AT_FDCWD, filename, flags, mode);
1069}
1070
1071SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
1072 umode_t, mode)
1073{
1074 if (force_o_largefile())
1075 flags |= O_LARGEFILE;
1076
1077 return do_sys_open(dfd, filename, flags, mode);
1078}
1079
1080#ifndef __alpha__
1081
1082
1083
1084
1085
1086SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
1087{
1088 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1089}
1090
1091#endif
1092
1093
1094
1095
1096
1097int filp_close(struct file *filp, fl_owner_t id)
1098{
1099 int retval = 0;
1100
1101 if (!file_count(filp)) {
1102 printk(KERN_ERR "VFS: Close: file count is 0\n");
1103 return 0;
1104 }
1105
1106 if (filp->f_op && filp->f_op->flush)
1107 retval = filp->f_op->flush(filp, id);
1108
1109 if (likely(!(filp->f_mode & FMODE_PATH))) {
1110 dnotify_flush(filp, id);
1111 locks_remove_posix(filp, id);
1112 }
1113 fput(filp);
1114 return retval;
1115}
1116
1117EXPORT_SYMBOL(filp_close);
1118
1119
1120
1121
1122
1123
1124SYSCALL_DEFINE1(close, unsigned int, fd)
1125{
1126 int retval = __close_fd(current->files, fd);
1127
1128
1129 if (unlikely(retval == -ERESTARTSYS ||
1130 retval == -ERESTARTNOINTR ||
1131 retval == -ERESTARTNOHAND ||
1132 retval == -ERESTART_RESTARTBLOCK))
1133 retval = -EINTR;
1134
1135 return retval;
1136}
1137EXPORT_SYMBOL(sys_close);
1138
1139
1140
1141
1142
1143SYSCALL_DEFINE0(vhangup)
1144{
1145 if (capable(CAP_SYS_TTY_CONFIG)) {
1146 tty_vhangup_self();
1147 return 0;
1148 }
1149 return -EPERM;
1150}
1151
1152
1153
1154
1155
1156
1157
1158int generic_file_open(struct inode * inode, struct file * filp)
1159{
1160 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1161 return -EOVERFLOW;
1162 return 0;
1163}
1164
1165EXPORT_SYMBOL(generic_file_open);
1166
1167
1168
1169
1170
1171
1172
1173int nonseekable_open(struct inode *inode, struct file *filp)
1174{
1175 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1176 return 0;
1177}
1178
1179EXPORT_SYMBOL(nonseekable_open);
1180