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