[PATCH] fix MAY_CHDIR/MAY_ACCESS/LOOKUP_ACCESS mess
[safe/jmp/linux-2.6] / 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/quotaops.h>
12 #include <linux/fsnotify.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/tty.h>
16 #include <linux/namei.h>
17 #include <linux/backing-dev.h>
18 #include <linux/capability.h>
19 #include <linux/securebits.h>
20 #include <linux/security.h>
21 #include <linux/mount.h>
22 #include <linux/vfs.h>
23 #include <linux/fcntl.h>
24 #include <asm/uaccess.h>
25 #include <linux/fs.h>
26 #include <linux/personality.h>
27 #include <linux/pagemap.h>
28 #include <linux/syscalls.h>
29 #include <linux/rcupdate.h>
30 #include <linux/audit.h>
31 #include <linux/falloc.h>
32
33 int vfs_statfs(struct dentry *dentry, struct kstatfs *buf)
34 {
35         int retval = -ENODEV;
36
37         if (dentry) {
38                 retval = -ENOSYS;
39                 if (dentry->d_sb->s_op->statfs) {
40                         memset(buf, 0, sizeof(*buf));
41                         retval = security_sb_statfs(dentry);
42                         if (retval)
43                                 return retval;
44                         retval = dentry->d_sb->s_op->statfs(dentry, buf);
45                         if (retval == 0 && buf->f_frsize == 0)
46                                 buf->f_frsize = buf->f_bsize;
47                 }
48         }
49         return retval;
50 }
51
52 EXPORT_SYMBOL(vfs_statfs);
53
54 static int vfs_statfs_native(struct dentry *dentry, struct statfs *buf)
55 {
56         struct kstatfs st;
57         int retval;
58
59         retval = vfs_statfs(dentry, &st);
60         if (retval)
61                 return retval;
62
63         if (sizeof(*buf) == sizeof(st))
64                 memcpy(buf, &st, sizeof(st));
65         else {
66                 if (sizeof buf->f_blocks == 4) {
67                         if ((st.f_blocks | st.f_bfree | st.f_bavail |
68                              st.f_bsize | st.f_frsize) &
69                             0xffffffff00000000ULL)
70                                 return -EOVERFLOW;
71                         /*
72                          * f_files and f_ffree may be -1; it's okay to stuff
73                          * that into 32 bits
74                          */
75                         if (st.f_files != -1 &&
76                             (st.f_files & 0xffffffff00000000ULL))
77                                 return -EOVERFLOW;
78                         if (st.f_ffree != -1 &&
79                             (st.f_ffree & 0xffffffff00000000ULL))
80                                 return -EOVERFLOW;
81                 }
82
83                 buf->f_type = st.f_type;
84                 buf->f_bsize = st.f_bsize;
85                 buf->f_blocks = st.f_blocks;
86                 buf->f_bfree = st.f_bfree;
87                 buf->f_bavail = st.f_bavail;
88                 buf->f_files = st.f_files;
89                 buf->f_ffree = st.f_ffree;
90                 buf->f_fsid = st.f_fsid;
91                 buf->f_namelen = st.f_namelen;
92                 buf->f_frsize = st.f_frsize;
93                 memset(buf->f_spare, 0, sizeof(buf->f_spare));
94         }
95         return 0;
96 }
97
98 static int vfs_statfs64(struct dentry *dentry, struct statfs64 *buf)
99 {
100         struct kstatfs st;
101         int retval;
102
103         retval = vfs_statfs(dentry, &st);
104         if (retval)
105                 return retval;
106
107         if (sizeof(*buf) == sizeof(st))
108                 memcpy(buf, &st, sizeof(st));
109         else {
110                 buf->f_type = st.f_type;
111                 buf->f_bsize = st.f_bsize;
112                 buf->f_blocks = st.f_blocks;
113                 buf->f_bfree = st.f_bfree;
114                 buf->f_bavail = st.f_bavail;
115                 buf->f_files = st.f_files;
116                 buf->f_ffree = st.f_ffree;
117                 buf->f_fsid = st.f_fsid;
118                 buf->f_namelen = st.f_namelen;
119                 buf->f_frsize = st.f_frsize;
120                 memset(buf->f_spare, 0, sizeof(buf->f_spare));
121         }
122         return 0;
123 }
124
125 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
126 {
127         struct nameidata nd;
128         int error;
129
130         error = user_path_walk(path, &nd);
131         if (!error) {
132                 struct statfs tmp;
133                 error = vfs_statfs_native(nd.path.dentry, &tmp);
134                 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
135                         error = -EFAULT;
136                 path_put(&nd.path);
137         }
138         return error;
139 }
140
141
142 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
143 {
144         struct nameidata nd;
145         long error;
146
147         if (sz != sizeof(*buf))
148                 return -EINVAL;
149         error = user_path_walk(path, &nd);
150         if (!error) {
151                 struct statfs64 tmp;
152                 error = vfs_statfs64(nd.path.dentry, &tmp);
153                 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
154                         error = -EFAULT;
155                 path_put(&nd.path);
156         }
157         return error;
158 }
159
160
161 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
162 {
163         struct file * file;
164         struct statfs tmp;
165         int error;
166
167         error = -EBADF;
168         file = fget(fd);
169         if (!file)
170                 goto out;
171         error = vfs_statfs_native(file->f_path.dentry, &tmp);
172         if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
173                 error = -EFAULT;
174         fput(file);
175 out:
176         return error;
177 }
178
179 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
180 {
181         struct file * file;
182         struct statfs64 tmp;
183         int error;
184
185         if (sz != sizeof(*buf))
186                 return -EINVAL;
187
188         error = -EBADF;
189         file = fget(fd);
190         if (!file)
191                 goto out;
192         error = vfs_statfs64(file->f_path.dentry, &tmp);
193         if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
194                 error = -EFAULT;
195         fput(file);
196 out:
197         return error;
198 }
199
200 int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
201         struct file *filp)
202 {
203         int err;
204         struct iattr newattrs;
205
206         /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
207         if (length < 0)
208                 return -EINVAL;
209
210         newattrs.ia_size = length;
211         newattrs.ia_valid = ATTR_SIZE | time_attrs;
212         if (filp) {
213                 newattrs.ia_file = filp;
214                 newattrs.ia_valid |= ATTR_FILE;
215         }
216
217         /* Remove suid/sgid on truncate too */
218         newattrs.ia_valid |= should_remove_suid(dentry);
219
220         mutex_lock(&dentry->d_inode->i_mutex);
221         err = notify_change(dentry, &newattrs);
222         mutex_unlock(&dentry->d_inode->i_mutex);
223         return err;
224 }
225
226 static long do_sys_truncate(const char __user * path, loff_t length)
227 {
228         struct nameidata nd;
229         struct inode * inode;
230         int error;
231
232         error = -EINVAL;
233         if (length < 0) /* sorry, but loff_t says... */
234                 goto out;
235
236         error = user_path_walk(path, &nd);
237         if (error)
238                 goto out;
239         inode = nd.path.dentry->d_inode;
240
241         /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
242         error = -EISDIR;
243         if (S_ISDIR(inode->i_mode))
244                 goto dput_and_out;
245
246         error = -EINVAL;
247         if (!S_ISREG(inode->i_mode))
248                 goto dput_and_out;
249
250         error = mnt_want_write(nd.path.mnt);
251         if (error)
252                 goto dput_and_out;
253
254         error = vfs_permission(&nd, MAY_WRITE);
255         if (error)
256                 goto mnt_drop_write_and_out;
257
258         error = -EPERM;
259         if (IS_APPEND(inode))
260                 goto mnt_drop_write_and_out;
261
262         error = get_write_access(inode);
263         if (error)
264                 goto mnt_drop_write_and_out;
265
266         /*
267          * Make sure that there are no leases.  get_write_access() protects
268          * against the truncate racing with a lease-granting setlease().
269          */
270         error = break_lease(inode, FMODE_WRITE);
271         if (error)
272                 goto put_write_and_out;
273
274         error = locks_verify_truncate(inode, NULL, length);
275         if (!error) {
276                 DQUOT_INIT(inode);
277                 error = do_truncate(nd.path.dentry, length, 0, NULL);
278         }
279
280 put_write_and_out:
281         put_write_access(inode);
282 mnt_drop_write_and_out:
283         mnt_drop_write(nd.path.mnt);
284 dput_and_out:
285         path_put(&nd.path);
286 out:
287         return error;
288 }
289
290 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
291 {
292         /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
293         return do_sys_truncate(path, (long)length);
294 }
295
296 static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
297 {
298         struct inode * inode;
299         struct dentry *dentry;
300         struct file * file;
301         int error;
302
303         error = -EINVAL;
304         if (length < 0)
305                 goto out;
306         error = -EBADF;
307         file = fget(fd);
308         if (!file)
309                 goto out;
310
311         /* explicitly opened as large or we are on 64-bit box */
312         if (file->f_flags & O_LARGEFILE)
313                 small = 0;
314
315         dentry = file->f_path.dentry;
316         inode = dentry->d_inode;
317         error = -EINVAL;
318         if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
319                 goto out_putf;
320
321         error = -EINVAL;
322         /* Cannot ftruncate over 2^31 bytes without large file support */
323         if (small && length > MAX_NON_LFS)
324                 goto out_putf;
325
326         error = -EPERM;
327         if (IS_APPEND(inode))
328                 goto out_putf;
329
330         error = locks_verify_truncate(inode, file, length);
331         if (!error)
332                 error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
333 out_putf:
334         fput(file);
335 out:
336         return error;
337 }
338
339 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
340 {
341         long ret = do_sys_ftruncate(fd, length, 1);
342         /* avoid REGPARM breakage on x86: */
343         asmlinkage_protect(2, ret, fd, length);
344         return ret;
345 }
346
347 /* LFS versions of truncate are only needed on 32 bit machines */
348 #if BITS_PER_LONG == 32
349 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
350 {
351         return do_sys_truncate(path, length);
352 }
353
354 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
355 {
356         long ret = do_sys_ftruncate(fd, length, 0);
357         /* avoid REGPARM breakage on x86: */
358         asmlinkage_protect(2, ret, fd, length);
359         return ret;
360 }
361 #endif
362
363 asmlinkage long sys_fallocate(int fd, int mode, loff_t offset, loff_t len)
364 {
365         struct file *file;
366         struct inode *inode;
367         long ret = -EINVAL;
368
369         if (offset < 0 || len <= 0)
370                 goto out;
371
372         /* Return error if mode is not supported */
373         ret = -EOPNOTSUPP;
374         if (mode && !(mode & FALLOC_FL_KEEP_SIZE))
375                 goto out;
376
377         ret = -EBADF;
378         file = fget(fd);
379         if (!file)
380                 goto out;
381         if (!(file->f_mode & FMODE_WRITE))
382                 goto out_fput;
383         /*
384          * Revalidate the write permissions, in case security policy has
385          * changed since the files were opened.
386          */
387         ret = security_file_permission(file, MAY_WRITE);
388         if (ret)
389                 goto out_fput;
390
391         inode = file->f_path.dentry->d_inode;
392
393         ret = -ESPIPE;
394         if (S_ISFIFO(inode->i_mode))
395                 goto out_fput;
396
397         ret = -ENODEV;
398         /*
399          * Let individual file system decide if it supports preallocation
400          * for directories or not.
401          */
402         if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
403                 goto out_fput;
404
405         ret = -EFBIG;
406         /* Check for wrap through zero too */
407         if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
408                 goto out_fput;
409
410         if (inode->i_op && inode->i_op->fallocate)
411                 ret = inode->i_op->fallocate(inode, mode, offset, len);
412         else
413                 ret = -EOPNOTSUPP;
414
415 out_fput:
416         fput(file);
417 out:
418         return ret;
419 }
420
421 /*
422  * access() needs to use the real uid/gid, not the effective uid/gid.
423  * We do this by temporarily clearing all FS-related capabilities and
424  * switching the fsuid/fsgid around to the real ones.
425  */
426 asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
427 {
428         struct nameidata nd;
429         int old_fsuid, old_fsgid;
430         kernel_cap_t uninitialized_var(old_cap);  /* !SECURE_NO_SETUID_FIXUP */
431         int res;
432
433         if (mode & ~S_IRWXO)    /* where's F_OK, X_OK, W_OK, R_OK? */
434                 return -EINVAL;
435
436         old_fsuid = current->fsuid;
437         old_fsgid = current->fsgid;
438
439         current->fsuid = current->uid;
440         current->fsgid = current->gid;
441
442         if (!issecure(SECURE_NO_SETUID_FIXUP)) {
443                 /*
444                  * Clear the capabilities if we switch to a non-root user
445                  */
446 #ifndef CONFIG_SECURITY_FILE_CAPABILITIES
447                 /*
448                  * FIXME: There is a race here against sys_capset.  The
449                  * capabilities can change yet we will restore the old
450                  * value below.  We should hold task_capabilities_lock,
451                  * but we cannot because user_path_walk can sleep.
452                  */
453 #endif /* ndef CONFIG_SECURITY_FILE_CAPABILITIES */
454                 if (current->uid)
455                         old_cap = cap_set_effective(__cap_empty_set);
456                 else
457                         old_cap = cap_set_effective(current->cap_permitted);
458         }
459
460         res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
461         if (res)
462                 goto out;
463
464         res = vfs_permission(&nd, mode | MAY_ACCESS);
465         /* SuS v2 requires we report a read only fs too */
466         if(res || !(mode & S_IWOTH) ||
467            special_file(nd.path.dentry->d_inode->i_mode))
468                 goto out_path_release;
469         /*
470          * This is a rare case where using __mnt_is_readonly()
471          * is OK without a mnt_want/drop_write() pair.  Since
472          * no actual write to the fs is performed here, we do
473          * not need to telegraph to that to anyone.
474          *
475          * By doing this, we accept that this access is
476          * inherently racy and know that the fs may change
477          * state before we even see this result.
478          */
479         if (__mnt_is_readonly(nd.path.mnt))
480                 res = -EROFS;
481
482 out_path_release:
483         path_put(&nd.path);
484 out:
485         current->fsuid = old_fsuid;
486         current->fsgid = old_fsgid;
487
488         if (!issecure(SECURE_NO_SETUID_FIXUP))
489                 cap_set_effective(old_cap);
490
491         return res;
492 }
493
494 asmlinkage long sys_access(const char __user *filename, int mode)
495 {
496         return sys_faccessat(AT_FDCWD, filename, mode);
497 }
498
499 asmlinkage long sys_chdir(const char __user * filename)
500 {
501         struct nameidata nd;
502         int error;
503
504         error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
505         if (error)
506                 goto out;
507
508         error = vfs_permission(&nd, MAY_EXEC | MAY_ACCESS);
509         if (error)
510                 goto dput_and_out;
511
512         set_fs_pwd(current->fs, &nd.path);
513
514 dput_and_out:
515         path_put(&nd.path);
516 out:
517         return error;
518 }
519
520 asmlinkage long sys_fchdir(unsigned int fd)
521 {
522         struct file *file;
523         struct inode *inode;
524         int error;
525
526         error = -EBADF;
527         file = fget(fd);
528         if (!file)
529                 goto out;
530
531         inode = file->f_path.dentry->d_inode;
532
533         error = -ENOTDIR;
534         if (!S_ISDIR(inode->i_mode))
535                 goto out_putf;
536
537         error = file_permission(file, MAY_EXEC | MAY_ACCESS);
538         if (!error)
539                 set_fs_pwd(current->fs, &file->f_path);
540 out_putf:
541         fput(file);
542 out:
543         return error;
544 }
545
546 asmlinkage long sys_chroot(const char __user * filename)
547 {
548         struct nameidata nd;
549         int error;
550
551         error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &nd);
552         if (error)
553                 goto out;
554
555         error = vfs_permission(&nd, MAY_EXEC | MAY_ACCESS);
556         if (error)
557                 goto dput_and_out;
558
559         error = -EPERM;
560         if (!capable(CAP_SYS_CHROOT))
561                 goto dput_and_out;
562
563         set_fs_root(current->fs, &nd.path);
564         error = 0;
565 dput_and_out:
566         path_put(&nd.path);
567 out:
568         return error;
569 }
570
571 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
572 {
573         struct inode * inode;
574         struct dentry * dentry;
575         struct file * file;
576         int err = -EBADF;
577         struct iattr newattrs;
578
579         file = fget(fd);
580         if (!file)
581                 goto out;
582
583         dentry = file->f_path.dentry;
584         inode = dentry->d_inode;
585
586         audit_inode(NULL, dentry);
587
588         err = mnt_want_write(file->f_path.mnt);
589         if (err)
590                 goto out_putf;
591         err = -EPERM;
592         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
593                 goto out_drop_write;
594         mutex_lock(&inode->i_mutex);
595         if (mode == (mode_t) -1)
596                 mode = inode->i_mode;
597         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
598         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
599         err = notify_change(dentry, &newattrs);
600         mutex_unlock(&inode->i_mutex);
601
602 out_drop_write:
603         mnt_drop_write(file->f_path.mnt);
604 out_putf:
605         fput(file);
606 out:
607         return err;
608 }
609
610 asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
611                              mode_t mode)
612 {
613         struct nameidata nd;
614         struct inode * inode;
615         int error;
616         struct iattr newattrs;
617
618         error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
619         if (error)
620                 goto out;
621         inode = nd.path.dentry->d_inode;
622
623         error = mnt_want_write(nd.path.mnt);
624         if (error)
625                 goto dput_and_out;
626
627         error = -EPERM;
628         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
629                 goto out_drop_write;
630
631         mutex_lock(&inode->i_mutex);
632         if (mode == (mode_t) -1)
633                 mode = inode->i_mode;
634         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
635         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
636         error = notify_change(nd.path.dentry, &newattrs);
637         mutex_unlock(&inode->i_mutex);
638
639 out_drop_write:
640         mnt_drop_write(nd.path.mnt);
641 dput_and_out:
642         path_put(&nd.path);
643 out:
644         return error;
645 }
646
647 asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
648 {
649         return sys_fchmodat(AT_FDCWD, filename, mode);
650 }
651
652 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
653 {
654         struct inode * inode;
655         int error;
656         struct iattr newattrs;
657
658         error = -ENOENT;
659         if (!(inode = dentry->d_inode)) {
660                 printk(KERN_ERR "chown_common: NULL inode\n");
661                 goto out;
662         }
663         error = -EPERM;
664         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
665                 goto out;
666         newattrs.ia_valid =  ATTR_CTIME;
667         if (user != (uid_t) -1) {
668                 newattrs.ia_valid |= ATTR_UID;
669                 newattrs.ia_uid = user;
670         }
671         if (group != (gid_t) -1) {
672                 newattrs.ia_valid |= ATTR_GID;
673                 newattrs.ia_gid = group;
674         }
675         if (!S_ISDIR(inode->i_mode))
676                 newattrs.ia_valid |=
677                         ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
678         mutex_lock(&inode->i_mutex);
679         error = notify_change(dentry, &newattrs);
680         mutex_unlock(&inode->i_mutex);
681 out:
682         return error;
683 }
684
685 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
686 {
687         struct nameidata nd;
688         int error;
689
690         error = user_path_walk(filename, &nd);
691         if (error)
692                 goto out;
693         error = mnt_want_write(nd.path.mnt);
694         if (error)
695                 goto out_release;
696         error = chown_common(nd.path.dentry, user, group);
697         mnt_drop_write(nd.path.mnt);
698 out_release:
699         path_put(&nd.path);
700 out:
701         return error;
702 }
703
704 asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
705                              gid_t group, int flag)
706 {
707         struct nameidata nd;
708         int error = -EINVAL;
709         int follow;
710
711         if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
712                 goto out;
713
714         follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
715         error = __user_walk_fd(dfd, filename, follow, &nd);
716         if (error)
717                 goto out;
718         error = mnt_want_write(nd.path.mnt);
719         if (error)
720                 goto out_release;
721         error = chown_common(nd.path.dentry, user, group);
722         mnt_drop_write(nd.path.mnt);
723 out_release:
724         path_put(&nd.path);
725 out:
726         return error;
727 }
728
729 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
730 {
731         struct nameidata nd;
732         int error;
733
734         error = user_path_walk_link(filename, &nd);
735         if (error)
736                 goto out;
737         error = mnt_want_write(nd.path.mnt);
738         if (error)
739                 goto out_release;
740         error = chown_common(nd.path.dentry, user, group);
741         mnt_drop_write(nd.path.mnt);
742 out_release:
743         path_put(&nd.path);
744 out:
745         return error;
746 }
747
748
749 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
750 {
751         struct file * file;
752         int error = -EBADF;
753         struct dentry * dentry;
754
755         file = fget(fd);
756         if (!file)
757                 goto out;
758
759         error = mnt_want_write(file->f_path.mnt);
760         if (error)
761                 goto out_fput;
762         dentry = file->f_path.dentry;
763         audit_inode(NULL, dentry);
764         error = chown_common(dentry, user, group);
765         mnt_drop_write(file->f_path.mnt);
766 out_fput:
767         fput(file);
768 out:
769         return error;
770 }
771
772 /*
773  * You have to be very careful that these write
774  * counts get cleaned up in error cases and
775  * upon __fput().  This should probably never
776  * be called outside of __dentry_open().
777  */
778 static inline int __get_file_write_access(struct inode *inode,
779                                           struct vfsmount *mnt)
780 {
781         int error;
782         error = get_write_access(inode);
783         if (error)
784                 return error;
785         /*
786          * Do not take mount writer counts on
787          * special files since no writes to
788          * the mount itself will occur.
789          */
790         if (!special_file(inode->i_mode)) {
791                 /*
792                  * Balanced in __fput()
793                  */
794                 error = mnt_want_write(mnt);
795                 if (error)
796                         put_write_access(inode);
797         }
798         return error;
799 }
800
801 static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
802                                         int flags, struct file *f,
803                                         int (*open)(struct inode *, struct file *))
804 {
805         struct inode *inode;
806         int error;
807
808         f->f_flags = flags;
809         f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
810                                 FMODE_PREAD | FMODE_PWRITE;
811         inode = dentry->d_inode;
812         if (f->f_mode & FMODE_WRITE) {
813                 error = __get_file_write_access(inode, mnt);
814                 if (error)
815                         goto cleanup_file;
816                 if (!special_file(inode->i_mode))
817                         file_take_write(f);
818         }
819
820         f->f_mapping = inode->i_mapping;
821         f->f_path.dentry = dentry;
822         f->f_path.mnt = mnt;
823         f->f_pos = 0;
824         f->f_op = fops_get(inode->i_fop);
825         file_move(f, &inode->i_sb->s_files);
826
827         error = security_dentry_open(f);
828         if (error)
829                 goto cleanup_all;
830
831         if (!open && f->f_op)
832                 open = f->f_op->open;
833         if (open) {
834                 error = open(inode, f);
835                 if (error)
836                         goto cleanup_all;
837         }
838
839         f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
840
841         file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
842
843         /* NB: we're sure to have correct a_ops only after f_op->open */
844         if (f->f_flags & O_DIRECT) {
845                 if (!f->f_mapping->a_ops ||
846                     ((!f->f_mapping->a_ops->direct_IO) &&
847                     (!f->f_mapping->a_ops->get_xip_mem))) {
848                         fput(f);
849                         f = ERR_PTR(-EINVAL);
850                 }
851         }
852
853         return f;
854
855 cleanup_all:
856         fops_put(f->f_op);
857         if (f->f_mode & FMODE_WRITE) {
858                 put_write_access(inode);
859                 if (!special_file(inode->i_mode)) {
860                         /*
861                          * We don't consider this a real
862                          * mnt_want/drop_write() pair
863                          * because it all happenend right
864                          * here, so just reset the state.
865                          */
866                         file_reset_write(f);
867                         mnt_drop_write(mnt);
868                 }
869         }
870         file_kill(f);
871         f->f_path.dentry = NULL;
872         f->f_path.mnt = NULL;
873 cleanup_file:
874         put_filp(f);
875         dput(dentry);
876         mntput(mnt);
877         return ERR_PTR(error);
878 }
879
880 /**
881  * lookup_instantiate_filp - instantiates the open intent filp
882  * @nd: pointer to nameidata
883  * @dentry: pointer to dentry
884  * @open: open callback
885  *
886  * Helper for filesystems that want to use lookup open intents and pass back
887  * a fully instantiated struct file to the caller.
888  * This function is meant to be called from within a filesystem's
889  * lookup method.
890  * Beware of calling it for non-regular files! Those ->open methods might block
891  * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
892  * leading to a deadlock, as nobody can open that fifo anymore, because
893  * another process to open fifo will block on locked parent when doing lookup).
894  * Note that in case of error, nd->intent.open.file is destroyed, but the
895  * path information remains valid.
896  * If the open callback is set to NULL, then the standard f_op->open()
897  * filesystem callback is substituted.
898  */
899 struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
900                 int (*open)(struct inode *, struct file *))
901 {
902         if (IS_ERR(nd->intent.open.file))
903                 goto out;
904         if (IS_ERR(dentry))
905                 goto out_err;
906         nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
907                                              nd->intent.open.flags - 1,
908                                              nd->intent.open.file,
909                                              open);
910 out:
911         return nd->intent.open.file;
912 out_err:
913         release_open_intent(nd);
914         nd->intent.open.file = (struct file *)dentry;
915         goto out;
916 }
917 EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
918
919 /**
920  * nameidata_to_filp - convert a nameidata to an open filp.
921  * @nd: pointer to nameidata
922  * @flags: open flags
923  *
924  * Note that this function destroys the original nameidata
925  */
926 struct file *nameidata_to_filp(struct nameidata *nd, int flags)
927 {
928         struct file *filp;
929
930         /* Pick up the filp from the open intent */
931         filp = nd->intent.open.file;
932         /* Has the filesystem initialised the file for us? */
933         if (filp->f_path.dentry == NULL)
934                 filp = __dentry_open(nd->path.dentry, nd->path.mnt, flags, filp,
935                                      NULL);
936         else
937                 path_put(&nd->path);
938         return filp;
939 }
940
941 /*
942  * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
943  * error.
944  */
945 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
946 {
947         int error;
948         struct file *f;
949
950         /*
951          * We must always pass in a valid mount pointer.   Historically
952          * callers got away with not passing it, but we must enforce this at
953          * the earliest possible point now to avoid strange problems deep in the
954          * filesystem stack.
955          */
956         if (!mnt) {
957                 printk(KERN_WARNING "%s called with NULL vfsmount\n", __func__);
958                 dump_stack();
959                 return ERR_PTR(-EINVAL);
960         }
961
962         error = -ENFILE;
963         f = get_empty_filp();
964         if (f == NULL) {
965                 dput(dentry);
966                 mntput(mnt);
967                 return ERR_PTR(error);
968         }
969
970         return __dentry_open(dentry, mnt, flags, f, NULL);
971 }
972 EXPORT_SYMBOL(dentry_open);
973
974 /*
975  * Find an empty file descriptor entry, and mark it busy.
976  */
977 int get_unused_fd_flags(int flags)
978 {
979         struct files_struct * files = current->files;
980         int fd, error;
981         struct fdtable *fdt;
982
983         error = -EMFILE;
984         spin_lock(&files->file_lock);
985
986 repeat:
987         fdt = files_fdtable(files);
988         fd = find_next_zero_bit(fdt->open_fds->fds_bits, fdt->max_fds,
989                                 files->next_fd);
990
991         /*
992          * N.B. For clone tasks sharing a files structure, this test
993          * will limit the total number of files that can be opened.
994          */
995         if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
996                 goto out;
997
998         /* Do we need to expand the fd array or fd set?  */
999         error = expand_files(files, fd);
1000         if (error < 0)
1001                 goto out;
1002
1003         if (error) {
1004                 /*
1005                  * If we needed to expand the fs array we
1006                  * might have blocked - try again.
1007                  */
1008                 error = -EMFILE;
1009                 goto repeat;
1010         }
1011
1012         FD_SET(fd, fdt->open_fds);
1013         if (flags & O_CLOEXEC)
1014                 FD_SET(fd, fdt->close_on_exec);
1015         else
1016                 FD_CLR(fd, fdt->close_on_exec);
1017         files->next_fd = fd + 1;
1018 #if 1
1019         /* Sanity check */
1020         if (fdt->fd[fd] != NULL) {
1021                 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
1022                 fdt->fd[fd] = NULL;
1023         }
1024 #endif
1025         error = fd;
1026
1027 out:
1028         spin_unlock(&files->file_lock);
1029         return error;
1030 }
1031
1032 int get_unused_fd(void)
1033 {
1034         return get_unused_fd_flags(0);
1035 }
1036
1037 EXPORT_SYMBOL(get_unused_fd);
1038
1039 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
1040 {
1041         struct fdtable *fdt = files_fdtable(files);
1042         __FD_CLR(fd, fdt->open_fds);
1043         if (fd < files->next_fd)
1044                 files->next_fd = fd;
1045 }
1046
1047 void put_unused_fd(unsigned int fd)
1048 {
1049         struct files_struct *files = current->files;
1050         spin_lock(&files->file_lock);
1051         __put_unused_fd(files, fd);
1052         spin_unlock(&files->file_lock);
1053 }
1054
1055 EXPORT_SYMBOL(put_unused_fd);
1056
1057 /*
1058  * Install a file pointer in the fd array.
1059  *
1060  * The VFS is full of places where we drop the files lock between
1061  * setting the open_fds bitmap and installing the file in the file
1062  * array.  At any such point, we are vulnerable to a dup2() race
1063  * installing a file in the array before us.  We need to detect this and
1064  * fput() the struct file we are about to overwrite in this case.
1065  *
1066  * It should never happen - if we allow dup2() do it, _really_ bad things
1067  * will follow.
1068  */
1069
1070 void fd_install(unsigned int fd, struct file *file)
1071 {
1072         struct files_struct *files = current->files;
1073         struct fdtable *fdt;
1074         spin_lock(&files->file_lock);
1075         fdt = files_fdtable(files);
1076         BUG_ON(fdt->fd[fd] != NULL);
1077         rcu_assign_pointer(fdt->fd[fd], file);
1078         spin_unlock(&files->file_lock);
1079 }
1080
1081 EXPORT_SYMBOL(fd_install);
1082
1083 long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
1084 {
1085         char *tmp = getname(filename);
1086         int fd = PTR_ERR(tmp);
1087
1088         if (!IS_ERR(tmp)) {
1089                 fd = get_unused_fd_flags(flags);
1090                 if (fd >= 0) {
1091                         struct file *f = do_filp_open(dfd, tmp, flags, mode);
1092                         if (IS_ERR(f)) {
1093                                 put_unused_fd(fd);
1094                                 fd = PTR_ERR(f);
1095                         } else {
1096                                 fsnotify_open(f->f_path.dentry);
1097                                 fd_install(fd, f);
1098                         }
1099                 }
1100                 putname(tmp);
1101         }
1102         return fd;
1103 }
1104
1105 asmlinkage long sys_open(const char __user *filename, int flags, int mode)
1106 {
1107         long ret;
1108
1109         if (force_o_largefile())
1110                 flags |= O_LARGEFILE;
1111
1112         ret = do_sys_open(AT_FDCWD, filename, flags, mode);
1113         /* avoid REGPARM breakage on x86: */
1114         asmlinkage_protect(3, ret, filename, flags, mode);
1115         return ret;
1116 }
1117
1118 asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
1119                            int mode)
1120 {
1121         long ret;
1122
1123         if (force_o_largefile())
1124                 flags |= O_LARGEFILE;
1125
1126         ret = do_sys_open(dfd, filename, flags, mode);
1127         /* avoid REGPARM breakage on x86: */
1128         asmlinkage_protect(4, ret, dfd, filename, flags, mode);
1129         return ret;
1130 }
1131
1132 #ifndef __alpha__
1133
1134 /*
1135  * For backward compatibility?  Maybe this should be moved
1136  * into arch/i386 instead?
1137  */
1138 asmlinkage long sys_creat(const char __user * pathname, int mode)
1139 {
1140         return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1141 }
1142
1143 #endif
1144
1145 /*
1146  * "id" is the POSIX thread ID. We use the
1147  * files pointer for this..
1148  */
1149 int filp_close(struct file *filp, fl_owner_t id)
1150 {
1151         int retval = 0;
1152
1153         if (!file_count(filp)) {
1154                 printk(KERN_ERR "VFS: Close: file count is 0\n");
1155                 return 0;
1156         }
1157
1158         if (filp->f_op && filp->f_op->flush)
1159                 retval = filp->f_op->flush(filp, id);
1160
1161         dnotify_flush(filp, id);
1162         locks_remove_posix(filp, id);
1163         fput(filp);
1164         return retval;
1165 }
1166
1167 EXPORT_SYMBOL(filp_close);
1168
1169 /*
1170  * Careful here! We test whether the file pointer is NULL before
1171  * releasing the fd. This ensures that one clone task can't release
1172  * an fd while another clone is opening it.
1173  */
1174 asmlinkage long sys_close(unsigned int fd)
1175 {
1176         struct file * filp;
1177         struct files_struct *files = current->files;
1178         struct fdtable *fdt;
1179         int retval;
1180
1181         spin_lock(&files->file_lock);
1182         fdt = files_fdtable(files);
1183         if (fd >= fdt->max_fds)
1184                 goto out_unlock;
1185         filp = fdt->fd[fd];
1186         if (!filp)
1187                 goto out_unlock;
1188         rcu_assign_pointer(fdt->fd[fd], NULL);
1189         FD_CLR(fd, fdt->close_on_exec);
1190         __put_unused_fd(files, fd);
1191         spin_unlock(&files->file_lock);
1192         retval = filp_close(filp, files);
1193
1194         /* can't restart close syscall because file table entry was cleared */
1195         if (unlikely(retval == -ERESTARTSYS ||
1196                      retval == -ERESTARTNOINTR ||
1197                      retval == -ERESTARTNOHAND ||
1198                      retval == -ERESTART_RESTARTBLOCK))
1199                 retval = -EINTR;
1200
1201         return retval;
1202
1203 out_unlock:
1204         spin_unlock(&files->file_lock);
1205         return -EBADF;
1206 }
1207
1208 EXPORT_SYMBOL(sys_close);
1209
1210 /*
1211  * This routine simulates a hangup on the tty, to arrange that users
1212  * are given clean terminals at login time.
1213  */
1214 asmlinkage long sys_vhangup(void)
1215 {
1216         if (capable(CAP_SYS_TTY_CONFIG)) {
1217                 /* XXX: this needs locking */
1218                 tty_vhangup(current->signal->tty);
1219                 return 0;
1220         }
1221         return -EPERM;
1222 }
1223
1224 /*
1225  * Called when an inode is about to be open.
1226  * We use this to disallow opening large files on 32bit systems if
1227  * the caller didn't specify O_LARGEFILE.  On 64bit systems we force
1228  * on this flag in sys_open.
1229  */
1230 int generic_file_open(struct inode * inode, struct file * filp)
1231 {
1232         if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1233                 return -EOVERFLOW;
1234         return 0;
1235 }
1236
1237 EXPORT_SYMBOL(generic_file_open);
1238
1239 /*
1240  * This is used by subsystems that don't want seekable
1241  * file descriptors
1242  */
1243 int nonseekable_open(struct inode *inode, struct file *filp)
1244 {
1245         filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1246         return 0;
1247 }
1248
1249 EXPORT_SYMBOL(nonseekable_open);