UBUNTU: Ubuntu-2.6.38-12.51
[linux-flexiantxendom0-natty.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 SYSCALL_DEFINE2(fchmod, unsigned int, fd, mode_t, mode)
454 {
455         struct inode * inode;
456         struct dentry * dentry;
457         struct file * file;
458         int err = -EBADF;
459         struct iattr newattrs;
460
461         file = fget(fd);
462         if (!file)
463                 goto out;
464
465         dentry = file->f_path.dentry;
466         inode = dentry->d_inode;
467
468         audit_inode(NULL, dentry);
469
470         err = mnt_want_write_file(file);
471         if (err)
472                 goto out_putf;
473         mutex_lock(&inode->i_mutex);
474         err = security_path_chmod(dentry, file->f_vfsmnt, mode);
475         if (err)
476                 goto out_unlock;
477         if (mode == (mode_t) -1)
478                 mode = inode->i_mode;
479         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
480         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
481         err = notify_change(dentry, &newattrs);
482 out_unlock:
483         mutex_unlock(&inode->i_mutex);
484         mnt_drop_write(file->f_path.mnt);
485 out_putf:
486         fput(file);
487 out:
488         return err;
489 }
490
491 SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, mode_t, mode)
492 {
493         struct path path;
494         struct inode *inode;
495         int error;
496         struct iattr newattrs;
497
498         error = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
499         if (error)
500                 goto out;
501         inode = path.dentry->d_inode;
502
503         error = mnt_want_write(path.mnt);
504         if (error)
505                 goto dput_and_out;
506         mutex_lock(&inode->i_mutex);
507         error = security_path_chmod(path.dentry, path.mnt, mode);
508         if (error)
509                 goto out_unlock;
510         if (mode == (mode_t) -1)
511                 mode = inode->i_mode;
512         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
513         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
514         error = notify_change(path.dentry, &newattrs);
515 out_unlock:
516         mutex_unlock(&inode->i_mutex);
517         mnt_drop_write(path.mnt);
518 dput_and_out:
519         path_put(&path);
520 out:
521         return error;
522 }
523
524 SYSCALL_DEFINE2(chmod, const char __user *, filename, mode_t, mode)
525 {
526         return sys_fchmodat(AT_FDCWD, filename, mode);
527 }
528
529 static int chown_common(struct path *path, uid_t user, gid_t group)
530 {
531         struct inode *inode = path->dentry->d_inode;
532         int error;
533         struct iattr newattrs;
534
535         newattrs.ia_valid =  ATTR_CTIME;
536         if (user != (uid_t) -1) {
537                 newattrs.ia_valid |= ATTR_UID;
538                 newattrs.ia_uid = user;
539         }
540         if (group != (gid_t) -1) {
541                 newattrs.ia_valid |= ATTR_GID;
542                 newattrs.ia_gid = group;
543         }
544         if (!S_ISDIR(inode->i_mode))
545                 newattrs.ia_valid |=
546                         ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
547         mutex_lock(&inode->i_mutex);
548         error = security_path_chown(path, user, group);
549         if (!error)
550                 error = notify_change(path->dentry, &newattrs);
551         mutex_unlock(&inode->i_mutex);
552
553         return error;
554 }
555
556 SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
557 {
558         struct path path;
559         int error;
560
561         error = user_path(filename, &path);
562         if (error)
563                 goto out;
564         error = mnt_want_write(path.mnt);
565         if (error)
566                 goto out_release;
567         error = chown_common(&path, user, group);
568         mnt_drop_write(path.mnt);
569 out_release:
570         path_put(&path);
571 out:
572         return error;
573 }
574
575 SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
576                 gid_t, group, int, flag)
577 {
578         struct path path;
579         int error = -EINVAL;
580         int follow;
581
582         if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
583                 goto out;
584
585         follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
586         error = user_path_at(dfd, filename, follow, &path);
587         if (error)
588                 goto out;
589         error = mnt_want_write(path.mnt);
590         if (error)
591                 goto out_release;
592         error = chown_common(&path, user, group);
593         mnt_drop_write(path.mnt);
594 out_release:
595         path_put(&path);
596 out:
597         return error;
598 }
599
600 SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
601 {
602         struct path path;
603         int error;
604
605         error = user_lpath(filename, &path);
606         if (error)
607                 goto out;
608         error = mnt_want_write(path.mnt);
609         if (error)
610                 goto out_release;
611         error = chown_common(&path, user, group);
612         mnt_drop_write(path.mnt);
613 out_release:
614         path_put(&path);
615 out:
616         return error;
617 }
618
619 SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
620 {
621         struct file * file;
622         int error = -EBADF;
623         struct dentry * dentry;
624
625         file = fget(fd);
626         if (!file)
627                 goto out;
628
629         error = mnt_want_write_file(file);
630         if (error)
631                 goto out_fput;
632         dentry = file->f_path.dentry;
633         audit_inode(NULL, dentry);
634         error = chown_common(&file->f_path, user, group);
635         mnt_drop_write(file->f_path.mnt);
636 out_fput:
637         fput(file);
638 out:
639         return error;
640 }
641
642 /*
643  * You have to be very careful that these write
644  * counts get cleaned up in error cases and
645  * upon __fput().  This should probably never
646  * be called outside of __dentry_open().
647  */
648 static inline int __get_file_write_access(struct inode *inode,
649                                           struct vfsmount *mnt)
650 {
651         int error;
652         error = get_write_access(inode);
653         if (error)
654                 return error;
655         /*
656          * Do not take mount writer counts on
657          * special files since no writes to
658          * the mount itself will occur.
659          */
660         if (!special_file(inode->i_mode)) {
661                 /*
662                  * Balanced in __fput()
663                  */
664                 error = mnt_want_write(mnt);
665                 if (error)
666                         put_write_access(inode);
667         }
668         return error;
669 }
670
671 static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
672                                         struct file *f,
673                                         int (*open)(struct inode *, struct file *),
674                                         const struct cred *cred)
675 {
676         struct inode *inode;
677         int error;
678
679         f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
680                                 FMODE_PREAD | FMODE_PWRITE;
681         inode = dentry->d_inode;
682         if (f->f_mode & FMODE_WRITE) {
683                 error = __get_file_write_access(inode, mnt);
684                 if (error)
685                         goto cleanup_file;
686                 if (!special_file(inode->i_mode))
687                         file_take_write(f);
688         }
689
690         f->f_mapping = inode->i_mapping;
691         f->f_path.dentry = dentry;
692         f->f_path.mnt = mnt;
693         f->f_pos = 0;
694         f->f_op = fops_get(inode->i_fop);
695         file_sb_list_add(f, inode->i_sb);
696
697         error = security_dentry_open(f, cred);
698         if (error)
699                 goto cleanup_all;
700
701         if (!open && f->f_op)
702                 open = f->f_op->open;
703         if (open) {
704                 error = open(inode, f);
705                 if (error)
706                         goto cleanup_all;
707         }
708         ima_counts_get(f);
709
710         f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
711
712         file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
713
714         /* NB: we're sure to have correct a_ops only after f_op->open */
715         if (f->f_flags & O_DIRECT) {
716                 if (!f->f_mapping->a_ops ||
717                     ((!f->f_mapping->a_ops->direct_IO) &&
718                     (!f->f_mapping->a_ops->get_xip_mem))) {
719                         fput(f);
720                         f = ERR_PTR(-EINVAL);
721                 }
722         }
723
724         return f;
725
726 cleanup_all:
727         fops_put(f->f_op);
728         if (f->f_mode & FMODE_WRITE) {
729                 put_write_access(inode);
730                 if (!special_file(inode->i_mode)) {
731                         /*
732                          * We don't consider this a real
733                          * mnt_want/drop_write() pair
734                          * because it all happenend right
735                          * here, so just reset the state.
736                          */
737                         file_reset_write(f);
738                         mnt_drop_write(mnt);
739                 }
740         }
741         file_sb_list_del(f);
742         f->f_path.dentry = NULL;
743         f->f_path.mnt = NULL;
744 cleanup_file:
745         put_filp(f);
746         dput(dentry);
747         mntput(mnt);
748         return ERR_PTR(error);
749 }
750
751 /**
752  * lookup_instantiate_filp - instantiates the open intent filp
753  * @nd: pointer to nameidata
754  * @dentry: pointer to dentry
755  * @open: open callback
756  *
757  * Helper for filesystems that want to use lookup open intents and pass back
758  * a fully instantiated struct file to the caller.
759  * This function is meant to be called from within a filesystem's
760  * lookup method.
761  * Beware of calling it for non-regular files! Those ->open methods might block
762  * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
763  * leading to a deadlock, as nobody can open that fifo anymore, because
764  * another process to open fifo will block on locked parent when doing lookup).
765  * Note that in case of error, nd->intent.open.file is destroyed, but the
766  * path information remains valid.
767  * If the open callback is set to NULL, then the standard f_op->open()
768  * filesystem callback is substituted.
769  */
770 struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
771                 int (*open)(struct inode *, struct file *))
772 {
773         const struct cred *cred = current_cred();
774
775         if (IS_ERR(nd->intent.open.file))
776                 goto out;
777         if (IS_ERR(dentry))
778                 goto out_err;
779         nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
780                                              nd->intent.open.file,
781                                              open, cred);
782 out:
783         return nd->intent.open.file;
784 out_err:
785         release_open_intent(nd);
786         nd->intent.open.file = (struct file *)dentry;
787         goto out;
788 }
789 EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
790
791 /**
792  * nameidata_to_filp - convert a nameidata to an open filp.
793  * @nd: pointer to nameidata
794  * @flags: open flags
795  *
796  * Note that this function destroys the original nameidata
797  */
798 struct file *nameidata_to_filp(struct nameidata *nd)
799 {
800         const struct cred *cred = current_cred();
801         struct file *filp;
802
803         /* Pick up the filp from the open intent */
804         filp = nd->intent.open.file;
805         nd->intent.open.file = NULL;
806
807         /* Has the filesystem initialised the file for us? */
808         if (filp->f_path.dentry == NULL) {
809                 path_get(&nd->path);
810                 filp = __dentry_open(nd->path.dentry, nd->path.mnt, filp,
811                                      NULL, cred);
812         }
813         return filp;
814 }
815
816 /*
817  * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
818  * error.
819  */
820 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags,
821                          const struct cred *cred)
822 {
823         int error;
824         struct file *f;
825
826         validate_creds(cred);
827
828         /*
829          * We must always pass in a valid mount pointer.   Historically
830          * callers got away with not passing it, but we must enforce this at
831          * the earliest possible point now to avoid strange problems deep in the
832          * filesystem stack.
833          */
834         if (!mnt) {
835                 printk(KERN_WARNING "%s called with NULL vfsmount\n", __func__);
836                 dump_stack();
837                 return ERR_PTR(-EINVAL);
838         }
839
840         error = -ENFILE;
841         f = get_empty_filp();
842         if (f == NULL) {
843                 dput(dentry);
844                 mntput(mnt);
845                 return ERR_PTR(error);
846         }
847
848         f->f_flags = flags;
849         return __dentry_open(dentry, mnt, f, NULL, cred);
850 }
851 EXPORT_SYMBOL(dentry_open);
852
853 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
854 {
855         struct fdtable *fdt = files_fdtable(files);
856         __FD_CLR(fd, fdt->open_fds);
857         if (fd < files->next_fd)
858                 files->next_fd = fd;
859 }
860
861 void put_unused_fd(unsigned int fd)
862 {
863         struct files_struct *files = current->files;
864         spin_lock(&files->file_lock);
865         __put_unused_fd(files, fd);
866         spin_unlock(&files->file_lock);
867 }
868
869 EXPORT_SYMBOL(put_unused_fd);
870
871 /*
872  * Install a file pointer in the fd array.
873  *
874  * The VFS is full of places where we drop the files lock between
875  * setting the open_fds bitmap and installing the file in the file
876  * array.  At any such point, we are vulnerable to a dup2() race
877  * installing a file in the array before us.  We need to detect this and
878  * fput() the struct file we are about to overwrite in this case.
879  *
880  * It should never happen - if we allow dup2() do it, _really_ bad things
881  * will follow.
882  */
883
884 void fd_install(unsigned int fd, struct file *file)
885 {
886         struct files_struct *files = current->files;
887         struct fdtable *fdt;
888         spin_lock(&files->file_lock);
889         fdt = files_fdtable(files);
890         BUG_ON(fdt->fd[fd] != NULL);
891         rcu_assign_pointer(fdt->fd[fd], file);
892         spin_unlock(&files->file_lock);
893 }
894
895 EXPORT_SYMBOL(fd_install);
896
897 long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
898 {
899         char *tmp = getname(filename);
900         int fd = PTR_ERR(tmp);
901
902         if (!IS_ERR(tmp)) {
903                 fd = get_unused_fd_flags(flags);
904                 if (fd >= 0) {
905                         struct file *f = do_filp_open(dfd, tmp, flags, mode, 0);
906                         if (IS_ERR(f)) {
907                                 put_unused_fd(fd);
908                                 fd = PTR_ERR(f);
909                         } else {
910                                 fsnotify_open(f);
911                                 fd_install(fd, f);
912                                 trace_do_sys_open(tmp, flags, mode);
913                         }
914                 }
915                 putname(tmp);
916         }
917         return fd;
918 }
919
920 SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode)
921 {
922         long ret;
923
924         if (force_o_largefile())
925                 flags |= O_LARGEFILE;
926
927         ret = do_sys_open(AT_FDCWD, filename, flags, mode);
928         /* avoid REGPARM breakage on x86: */
929         asmlinkage_protect(3, ret, filename, flags, mode);
930         return ret;
931 }
932
933 SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
934                 int, mode)
935 {
936         long ret;
937
938         if (force_o_largefile())
939                 flags |= O_LARGEFILE;
940
941         ret = do_sys_open(dfd, filename, flags, mode);
942         /* avoid REGPARM breakage on x86: */
943         asmlinkage_protect(4, ret, dfd, filename, flags, mode);
944         return ret;
945 }
946
947 #ifndef __alpha__
948
949 /*
950  * For backward compatibility?  Maybe this should be moved
951  * into arch/i386 instead?
952  */
953 SYSCALL_DEFINE2(creat, const char __user *, pathname, int, mode)
954 {
955         return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
956 }
957
958 #endif
959
960 /*
961  * "id" is the POSIX thread ID. We use the
962  * files pointer for this..
963  */
964 int filp_close(struct file *filp, fl_owner_t id)
965 {
966         int retval = 0;
967
968         if (!file_count(filp)) {
969                 printk(KERN_ERR "VFS: Close: file count is 0\n");
970                 return 0;
971         }
972
973         if (filp->f_op && filp->f_op->flush)
974                 retval = filp->f_op->flush(filp, id);
975
976         dnotify_flush(filp, id);
977         locks_remove_posix(filp, id);
978         fput(filp);
979         return retval;
980 }
981
982 EXPORT_SYMBOL(filp_close);
983
984 /*
985  * Careful here! We test whether the file pointer is NULL before
986  * releasing the fd. This ensures that one clone task can't release
987  * an fd while another clone is opening it.
988  */
989 SYSCALL_DEFINE1(close, unsigned int, fd)
990 {
991         struct file * filp;
992         struct files_struct *files = current->files;
993         struct fdtable *fdt;
994         int retval;
995
996         spin_lock(&files->file_lock);
997         fdt = files_fdtable(files);
998         if (fd >= fdt->max_fds)
999                 goto out_unlock;
1000         filp = fdt->fd[fd];
1001         if (!filp)
1002                 goto out_unlock;
1003         rcu_assign_pointer(fdt->fd[fd], NULL);
1004         FD_CLR(fd, fdt->close_on_exec);
1005         __put_unused_fd(files, fd);
1006         spin_unlock(&files->file_lock);
1007         retval = filp_close(filp, files);
1008
1009         /* can't restart close syscall because file table entry was cleared */
1010         if (unlikely(retval == -ERESTARTSYS ||
1011                      retval == -ERESTARTNOINTR ||
1012                      retval == -ERESTARTNOHAND ||
1013                      retval == -ERESTART_RESTARTBLOCK))
1014                 retval = -EINTR;
1015
1016         return retval;
1017
1018 out_unlock:
1019         spin_unlock(&files->file_lock);
1020         return -EBADF;
1021 }
1022 EXPORT_SYMBOL(sys_close);
1023
1024 /*
1025  * This routine simulates a hangup on the tty, to arrange that users
1026  * are given clean terminals at login time.
1027  */
1028 SYSCALL_DEFINE0(vhangup)
1029 {
1030         if (capable(CAP_SYS_TTY_CONFIG)) {
1031                 tty_vhangup_self();
1032                 return 0;
1033         }
1034         return -EPERM;
1035 }
1036
1037 /*
1038  * Called when an inode is about to be open.
1039  * We use this to disallow opening large files on 32bit systems if
1040  * the caller didn't specify O_LARGEFILE.  On 64bit systems we force
1041  * on this flag in sys_open.
1042  */
1043 int generic_file_open(struct inode * inode, struct file * filp)
1044 {
1045         if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1046                 return -EOVERFLOW;
1047         return 0;
1048 }
1049
1050 EXPORT_SYMBOL(generic_file_open);
1051
1052 /*
1053  * This is used by subsystems that don't want seekable
1054  * file descriptors. The function is not supposed to ever fail, the only
1055  * reason it returns an 'int' and not 'void' is so that it can be plugged
1056  * directly into file_operations structure.
1057  */
1058 int nonseekable_open(struct inode *inode, struct file *filp)
1059 {
1060         filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1061         return 0;
1062 }
1063
1064 EXPORT_SYMBOL(nonseekable_open);