commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.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/utime.h>
10 #include <linux/file.h>
11 #include <linux/smp_lock.h>
12 #include <linux/quotaops.h>
13 #include <linux/dnotify.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/tty.h>
17 #include <linux/namei.h>
18 #include <linux/backing-dev.h>
19 #include <linux/security.h>
20 #include <linux/mount.h>
21 #include <linux/vfs.h>
22 #include <asm/uaccess.h>
23 #include <linux/fs.h>
24
25 int vfs_statfs(struct super_block *sb, struct kstatfs *buf)
26 {
27         int retval = -ENODEV;
28
29         if (sb) {
30                 retval = -ENOSYS;
31                 if (sb->s_op->statfs) {
32                         memset(buf, 0, sizeof(*buf));
33                         retval = security_sb_statfs(sb);
34                         if (retval)
35                                 return retval;
36                         retval = sb->s_op->statfs(sb, buf);
37                         if (retval == 0 && buf->f_frsize == 0)
38                                 buf->f_frsize = buf->f_bsize;
39                 }
40         }
41         return retval;
42 }
43
44 static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
45 {
46         struct kstatfs st;
47         int retval;
48
49         retval = vfs_statfs(sb, &st);
50         if (retval)
51                 return retval;
52
53         if (sizeof(*buf) == sizeof(st))
54                 memcpy(buf, &st, sizeof(st));
55         else {
56                 if (sizeof buf->f_blocks == 4) {
57                         if ((st.f_blocks | st.f_bfree |
58                              st.f_bavail | st.f_files | st.f_ffree) &
59                             0xffffffff00000000ULL)
60                                 return -EOVERFLOW;
61                 }
62
63                 buf->f_type = st.f_type;
64                 buf->f_bsize = st.f_bsize;
65                 buf->f_blocks = st.f_blocks;
66                 buf->f_bfree = st.f_bfree;
67                 buf->f_bavail = st.f_bavail;
68                 buf->f_files = st.f_files;
69                 buf->f_ffree = st.f_ffree;
70                 buf->f_fsid = st.f_fsid;
71                 buf->f_namelen = st.f_namelen;
72                 buf->f_frsize = st.f_frsize;
73                 memset(buf->f_spare, 0, sizeof(buf->f_spare));
74         }
75         return 0;
76 }
77
78 static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
79 {
80         struct kstatfs st;
81         int retval;
82
83         retval = vfs_statfs(sb, &st);
84         if (retval)
85                 return retval;
86
87         if (sizeof(*buf) == sizeof(st))
88                 memcpy(buf, &st, sizeof(st));
89         else {
90                 buf->f_type = st.f_type;
91                 buf->f_bsize = st.f_bsize;
92                 buf->f_blocks = st.f_blocks;
93                 buf->f_bfree = st.f_bfree;
94                 buf->f_bavail = st.f_bavail;
95                 buf->f_files = st.f_files;
96                 buf->f_ffree = st.f_ffree;
97                 buf->f_fsid = st.f_fsid;
98                 buf->f_namelen = st.f_namelen;
99                 buf->f_frsize = st.f_frsize;
100                 memset(buf->f_spare, 0, sizeof(buf->f_spare));
101         }
102         return 0;
103 }
104
105 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
106 {
107         struct nameidata nd;
108         int error;
109
110         error = user_path_walk(path, &nd);
111         if (!error) {
112                 struct statfs tmp;
113                 error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
114                 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
115                         error = -EFAULT;
116                 path_release(&nd);
117         }
118         return error;
119 }
120
121
122 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
123 {
124         struct nameidata nd;
125         long error;
126
127         if (sz != sizeof(*buf))
128                 return -EINVAL;
129         error = user_path_walk(path, &nd);
130         if (!error) {
131                 struct statfs64 tmp;
132                 error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
133                 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
134                         error = -EFAULT;
135                 path_release(&nd);
136         }
137         return error;
138 }
139
140
141 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
142 {
143         struct file * file;
144         struct statfs tmp;
145         int error;
146
147         error = -EBADF;
148         file = fget(fd);
149         if (!file)
150                 goto out;
151         error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
152         if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
153                 error = -EFAULT;
154         fput(file);
155 out:
156         return error;
157 }
158
159 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
160 {
161         struct file * file;
162         struct statfs64 tmp;
163         int error;
164
165         if (sz != sizeof(*buf))
166                 return -EINVAL;
167
168         error = -EBADF;
169         file = fget(fd);
170         if (!file)
171                 goto out;
172         error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
173         if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
174                 error = -EFAULT;
175         fput(file);
176 out:
177         return error;
178 }
179
180 int do_truncate(struct dentry *dentry, loff_t length)
181 {
182         int err;
183         struct iattr newattrs;
184
185         /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
186         if (length < 0)
187                 return -EINVAL;
188
189         newattrs.ia_size = length;
190         newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
191         down(&dentry->d_inode->i_sem);
192         err = notify_change(dentry, &newattrs);
193         up(&dentry->d_inode->i_sem);
194         return err;
195 }
196
197 static inline long do_sys_truncate(const char __user * path, loff_t length)
198 {
199         struct nameidata nd;
200         struct inode * inode;
201         int error;
202
203         error = -EINVAL;
204         if (length < 0) /* sorry, but loff_t says... */
205                 goto out;
206
207         error = user_path_walk(path, &nd);
208         if (error)
209                 goto out;
210         inode = nd.dentry->d_inode;
211
212         /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
213         error = -EISDIR;
214         if (S_ISDIR(inode->i_mode))
215                 goto dput_and_out;
216
217         error = -EINVAL;
218         if (!S_ISREG(inode->i_mode))
219                 goto dput_and_out;
220
221         error = permission(inode,MAY_WRITE,&nd);
222         if (error)
223                 goto dput_and_out;
224
225         error = -EROFS;
226         if (IS_RDONLY(inode))
227                 goto dput_and_out;
228
229         error = -EPERM;
230         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
231                 goto dput_and_out;
232
233         /*
234          * Make sure that there are no leases.
235          */
236         error = break_lease(inode, FMODE_WRITE);
237         if (error)
238                 goto dput_and_out;
239
240         error = get_write_access(inode);
241         if (error)
242                 goto dput_and_out;
243
244         error = locks_verify_truncate(inode, NULL, length);
245         if (!error) {
246                 DQUOT_INIT(inode);
247                 error = do_truncate(nd.dentry, length);
248         }
249         put_write_access(inode);
250
251 dput_and_out:
252         path_release(&nd);
253 out:
254         return error;
255 }
256
257 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
258 {
259         /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
260         return do_sys_truncate(path, (long)length);
261 }
262
263 static inline long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
264 {
265         struct inode * inode;
266         struct dentry *dentry;
267         struct file * file;
268         int error;
269
270         error = -EINVAL;
271         if (length < 0)
272                 goto out;
273         error = -EBADF;
274         file = fget(fd);
275         if (!file)
276                 goto out;
277
278         /* explicitly opened as large or we are on 64-bit box */
279         if (file->f_flags & O_LARGEFILE)
280                 small = 0;
281
282         dentry = file->f_dentry;
283         inode = dentry->d_inode;
284         error = -EINVAL;
285         if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
286                 goto out_putf;
287
288         error = -EINVAL;
289         /* Cannot ftruncate over 2^31 bytes without large file support */
290         if (small && length > MAX_NON_LFS)
291                 goto out_putf;
292
293         error = -EPERM;
294         if (IS_APPEND(inode))
295                 goto out_putf;
296
297         error = locks_verify_truncate(inode, file, length);
298         if (!error)
299                 error = do_truncate(dentry, length);
300 out_putf:
301         fput(file);
302 out:
303         return error;
304 }
305
306 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
307 {
308         return do_sys_ftruncate(fd, length, 1);
309 }
310
311 /* LFS versions of truncate are only needed on 32 bit machines */
312 #if BITS_PER_LONG == 32
313 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
314 {
315         return do_sys_truncate(path, length);
316 }
317
318 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
319 {
320         return do_sys_ftruncate(fd, length, 0);
321 }
322 #endif
323
324 #if !(defined(__alpha__) || defined(__ia64__))
325
326 /*
327  * sys_utime() can be implemented in user-level using sys_utimes().
328  * Is this for backwards compatibility?  If so, why not move it
329  * into the appropriate arch directory (for those architectures that
330  * need it).
331  */
332
333 /* If times==NULL, set access and modification to current time,
334  * must be owner or have write permission.
335  * Else, update from *times, must be owner or super user.
336  */
337 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
338 {
339         int error;
340         struct nameidata nd;
341         struct inode * inode;
342         struct iattr newattrs;
343
344         error = user_path_walk(filename, &nd);
345         if (error)
346                 goto out;
347         inode = nd.dentry->d_inode;
348
349         error = -EROFS;
350         if (IS_RDONLY(inode))
351                 goto dput_and_out;
352
353         /* Don't worry, the checks are done in inode_change_ok() */
354         newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
355         if (times) {
356                 error = get_user(newattrs.ia_atime.tv_sec, &times->actime);
357                 newattrs.ia_atime.tv_nsec = 0;
358                 if (!error) 
359                         error = get_user(newattrs.ia_mtime.tv_sec, &times->modtime);
360                 newattrs.ia_mtime.tv_nsec = 0;
361                 if (error)
362                         goto dput_and_out;
363
364                 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
365         } else {
366                 if (current->fsuid != inode->i_uid &&
367                     (error = permission(inode,MAY_WRITE,&nd)) != 0)
368                         goto dput_and_out;
369         }
370         down(&inode->i_sem);
371         error = notify_change(nd.dentry, &newattrs);
372         up(&inode->i_sem);
373 dput_and_out:
374         path_release(&nd);
375 out:
376         return error;
377 }
378
379 #endif
380
381 /* If times==NULL, set access and modification to current time,
382  * must be owner or have write permission.
383  * Else, update from *times, must be owner or super user.
384  */
385 long do_utimes(char __user * filename, struct timeval * times)
386 {
387         int error;
388         struct nameidata nd;
389         struct inode * inode;
390         struct iattr newattrs;
391
392         error = user_path_walk(filename, &nd);
393
394         if (error)
395                 goto out;
396         inode = nd.dentry->d_inode;
397
398         error = -EROFS;
399         if (IS_RDONLY(inode))
400                 goto dput_and_out;
401
402         /* Don't worry, the checks are done in inode_change_ok() */
403         newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
404         if (times) {
405                 newattrs.ia_atime.tv_sec = times[0].tv_sec;
406                 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
407                 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
408                 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
409                 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
410         } else {
411                 if (current->fsuid != inode->i_uid &&
412                     (error = permission(inode,MAY_WRITE,&nd)) != 0)
413                         goto dput_and_out;
414         }
415         down(&inode->i_sem);
416         error = notify_change(nd.dentry, &newattrs);
417         up(&inode->i_sem);
418 dput_and_out:
419         path_release(&nd);
420 out:
421         return error;
422 }
423
424 asmlinkage long sys_utimes(char __user * filename, struct timeval __user * utimes)
425 {
426         struct timeval times[2];
427
428         if (utimes && copy_from_user(&times, utimes, sizeof(times)))
429                 return -EFAULT;
430         return do_utimes(filename, utimes ? times : NULL);
431 }
432
433
434 /*
435  * access() needs to use the real uid/gid, not the effective uid/gid.
436  * We do this by temporarily clearing all FS-related capabilities and
437  * switching the fsuid/fsgid around to the real ones.
438  */
439 asmlinkage long sys_access(const char __user * filename, int mode)
440 {
441         struct nameidata nd;
442         int old_fsuid, old_fsgid;
443         kernel_cap_t old_cap;
444         int res;
445
446         if (mode & ~S_IRWXO)    /* where's F_OK, X_OK, W_OK, R_OK? */
447                 return -EINVAL;
448
449         old_fsuid = current->fsuid;
450         old_fsgid = current->fsgid;
451         old_cap = current->cap_effective;
452
453         current->fsuid = current->uid;
454         current->fsgid = current->gid;
455
456         /*
457          * Clear the capabilities if we switch to a non-root user
458          *
459          * FIXME: There is a race here against sys_capset.  The
460          * capabilities can change yet we will restore the old
461          * value below.  We should hold task_capabilities_lock,
462          * but we cannot because user_path_walk can sleep.
463          */
464         if (current->uid)
465                 cap_clear(current->cap_effective);
466         else
467                 current->cap_effective = current->cap_permitted;
468
469         res = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
470         if (!res) {
471                 res = permission(nd.dentry->d_inode, mode, &nd);
472                 /* SuS v2 requires we report a read only fs too */
473                 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
474                    && !special_file(nd.dentry->d_inode->i_mode))
475                         res = -EROFS;
476                 path_release(&nd);
477         }
478
479         current->fsuid = old_fsuid;
480         current->fsgid = old_fsgid;
481         current->cap_effective = old_cap;
482
483         return res;
484 }
485
486 asmlinkage long sys_chdir(const char __user * filename)
487 {
488         struct nameidata nd;
489         int error;
490
491         error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
492         if (error)
493                 goto out;
494
495         error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
496         if (error)
497                 goto dput_and_out;
498
499         set_fs_pwd(current->fs, nd.mnt, nd.dentry);
500
501 dput_and_out:
502         path_release(&nd);
503 out:
504         return error;
505 }
506
507 asmlinkage long sys_fchdir(unsigned int fd)
508 {
509         struct file *file;
510         struct dentry *dentry;
511         struct inode *inode;
512         struct vfsmount *mnt;
513         int error;
514
515         error = -EBADF;
516         file = fget(fd);
517         if (!file)
518                 goto out;
519
520         dentry = file->f_dentry;
521         mnt = file->f_vfsmnt;
522         inode = dentry->d_inode;
523
524         error = -ENOTDIR;
525         if (!S_ISDIR(inode->i_mode))
526                 goto out_putf;
527
528         error = permission(inode, MAY_EXEC, NULL);
529         if (!error)
530                 set_fs_pwd(current->fs, mnt, dentry);
531 out_putf:
532         fput(file);
533 out:
534         return error;
535 }
536
537 asmlinkage long sys_chroot(const char __user * filename)
538 {
539         struct nameidata nd;
540         int error;
541
542         error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
543         if (error)
544                 goto out;
545
546         error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
547         if (error)
548                 goto dput_and_out;
549
550         error = -EPERM;
551         if (!capable(CAP_SYS_CHROOT))
552                 goto dput_and_out;
553
554         set_fs_root(current->fs, nd.mnt, nd.dentry);
555         set_fs_altroot();
556         error = 0;
557 dput_and_out:
558         path_release(&nd);
559 out:
560         return error;
561 }
562
563 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
564 {
565         struct inode * inode;
566         struct dentry * dentry;
567         struct file * file;
568         int err = -EBADF;
569         struct iattr newattrs;
570
571         file = fget(fd);
572         if (!file)
573                 goto out;
574
575         dentry = file->f_dentry;
576         inode = dentry->d_inode;
577
578         err = -EROFS;
579         if (IS_RDONLY(inode))
580                 goto out_putf;
581         err = -EPERM;
582         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
583                 goto out_putf;
584         down(&inode->i_sem);
585         if (mode == (mode_t) -1)
586                 mode = inode->i_mode;
587         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
588         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
589         err = notify_change(dentry, &newattrs);
590         up(&inode->i_sem);
591
592 out_putf:
593         fput(file);
594 out:
595         return err;
596 }
597
598 asmlinkage long sys_chmod(const char __user * filename, mode_t mode)
599 {
600         struct nameidata nd;
601         struct inode * inode;
602         int error;
603         struct iattr newattrs;
604
605         error = user_path_walk(filename, &nd);
606         if (error)
607                 goto out;
608         inode = nd.dentry->d_inode;
609
610         error = -EROFS;
611         if (IS_RDONLY(inode))
612                 goto dput_and_out;
613
614         error = -EPERM;
615         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
616                 goto dput_and_out;
617
618         down(&inode->i_sem);
619         if (mode == (mode_t) -1)
620                 mode = inode->i_mode;
621         newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
622         newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
623         error = notify_change(nd.dentry, &newattrs);
624         up(&inode->i_sem);
625
626 dput_and_out:
627         path_release(&nd);
628 out:
629         return error;
630 }
631
632 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
633 {
634         struct inode * inode;
635         int error;
636         struct iattr newattrs;
637
638         error = -ENOENT;
639         if (!(inode = dentry->d_inode)) {
640                 printk(KERN_ERR "chown_common: NULL inode\n");
641                 goto out;
642         }
643         error = -EROFS;
644         if (IS_RDONLY(inode))
645                 goto out;
646         error = -EPERM;
647         if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
648                 goto out;
649         newattrs.ia_valid =  ATTR_CTIME;
650         if (user != (uid_t) -1) {
651                 newattrs.ia_valid |= ATTR_UID;
652                 newattrs.ia_uid = user;
653         }
654         if (group != (gid_t) -1) {
655                 newattrs.ia_valid |= ATTR_GID;
656                 newattrs.ia_gid = group;
657         }
658         if (!S_ISDIR(inode->i_mode))
659                 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
660         down(&inode->i_sem);
661         error = notify_change(dentry, &newattrs);
662         up(&inode->i_sem);
663 out:
664         return error;
665 }
666
667 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
668 {
669         struct nameidata nd;
670         int error;
671
672         error = user_path_walk(filename, &nd);
673         if (!error) {
674                 error = chown_common(nd.dentry, user, group);
675                 path_release(&nd);
676         }
677         return error;
678 }
679
680 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
681 {
682         struct nameidata nd;
683         int error;
684
685         error = user_path_walk_link(filename, &nd);
686         if (!error) {
687                 error = chown_common(nd.dentry, user, group);
688                 path_release(&nd);
689         }
690         return error;
691 }
692
693
694 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
695 {
696         struct file * file;
697         int error = -EBADF;
698
699         file = fget(fd);
700         if (file) {
701                 error = chown_common(file->f_dentry, user, group);
702                 fput(file);
703         }
704         return error;
705 }
706
707 /*
708  * Note that while the flag value (low two bits) for sys_open means:
709  *      00 - read-only
710  *      01 - write-only
711  *      10 - read-write
712  *      11 - special
713  * it is changed into
714  *      00 - no permissions needed
715  *      01 - read-permission
716  *      10 - write-permission
717  *      11 - read-write
718  * for the internal routines (ie open_namei()/follow_link() etc). 00 is
719  * used by symlinks.
720  */
721 struct file *filp_open(const char * filename, int flags, int mode)
722 {
723         int namei_flags, error;
724         struct nameidata nd;
725
726         namei_flags = flags;
727         if ((namei_flags+1) & O_ACCMODE)
728                 namei_flags++;
729         if (namei_flags & O_TRUNC)
730                 namei_flags |= 2;
731
732         error = open_namei(filename, namei_flags, mode, &nd);
733         if (!error)
734                 return dentry_open(nd.dentry, nd.mnt, flags);
735
736         return ERR_PTR(error);
737 }
738
739 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
740 {
741         struct file * f;
742         struct inode *inode;
743         int error;
744
745         error = -ENFILE;
746         f = get_empty_filp();
747         if (!f)
748                 goto cleanup_dentry;
749         f->f_flags = flags;
750         f->f_mode = (flags+1) & O_ACCMODE;
751         inode = dentry->d_inode;
752         if (f->f_mode & FMODE_WRITE) {
753                 error = get_write_access(inode);
754                 if (error)
755                         goto cleanup_file;
756         }
757
758         file_ra_state_init(&f->f_ra, inode->i_mapping);
759         f->f_dentry = dentry;
760         f->f_vfsmnt = mnt;
761         f->f_pos = 0;
762         f->f_op = fops_get(inode->i_fop);
763         file_move(f, &inode->i_sb->s_files);
764
765         if (f->f_op && f->f_op->open) {
766                 error = f->f_op->open(inode,f);
767                 if (error)
768                         goto cleanup_all;
769         }
770         f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
771
772         /* NB: we're sure to have correct a_ops only after f_op->open */
773         if (f->f_flags & O_DIRECT) {
774                 if (!inode->i_mapping || !inode->i_mapping->a_ops ||
775                         !inode->i_mapping->a_ops->direct_IO) {
776                                 fput(f);
777                                 f = ERR_PTR(-EINVAL);
778                 }
779         }
780
781         return f;
782
783 cleanup_all:
784         fops_put(f->f_op);
785         if (f->f_mode & FMODE_WRITE)
786                 put_write_access(inode);
787         file_kill(f);
788         f->f_dentry = NULL;
789         f->f_vfsmnt = NULL;
790 cleanup_file:
791         put_filp(f);
792 cleanup_dentry:
793         dput(dentry);
794         mntput(mnt);
795         return ERR_PTR(error);
796 }
797
798 /*
799  * Find an empty file descriptor entry, and mark it busy.
800  */
801 int get_unused_fd(void)
802 {
803         struct files_struct * files = current->files;
804         int fd, error;
805
806         error = -EMFILE;
807         spin_lock(&files->file_lock);
808
809 repeat:
810         fd = find_next_zero_bit(files->open_fds->fds_bits, 
811                                 files->max_fdset, 
812                                 files->next_fd);
813
814         /*
815          * N.B. For clone tasks sharing a files structure, this test
816          * will limit the total number of files that can be opened.
817          */
818         if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
819                 goto out;
820
821         /* Do we need to expand the fdset array? */
822         if (fd >= files->max_fdset) {
823                 error = expand_fdset(files, fd);
824                 if (!error) {
825                         error = -EMFILE;
826                         goto repeat;
827                 }
828                 goto out;
829         }
830         
831         /* 
832          * Check whether we need to expand the fd array.
833          */
834         if (fd >= files->max_fds) {
835                 error = expand_fd_array(files, fd);
836                 if (!error) {
837                         error = -EMFILE;
838                         goto repeat;
839                 }
840                 goto out;
841         }
842
843         FD_SET(fd, files->open_fds);
844         FD_CLR(fd, files->close_on_exec);
845         files->next_fd = fd + 1;
846 #if 1
847         /* Sanity check */
848         if (files->fd[fd] != NULL) {
849                 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
850                 files->fd[fd] = NULL;
851         }
852 #endif
853         error = fd;
854
855 out:
856         spin_unlock(&files->file_lock);
857         return error;
858 }
859
860 static inline void __put_unused_fd(struct files_struct *files, unsigned int fd)
861 {
862         __FD_CLR(fd, files->open_fds);
863         if (fd < files->next_fd)
864                 files->next_fd = fd;
865 }
866
867 void put_unused_fd(unsigned int fd)
868 {
869         struct files_struct *files = current->files;
870         spin_lock(&files->file_lock);
871         __put_unused_fd(files, fd);
872         spin_unlock(&files->file_lock);
873 }
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         spin_lock(&files->file_lock);
892         if (unlikely(files->fd[fd] != NULL))
893                 BUG();
894         files->fd[fd] = file;
895         spin_unlock(&files->file_lock);
896 }
897
898 asmlinkage long sys_open(const char __user * filename, int flags, int mode)
899 {
900         char * tmp;
901         int fd, error;
902
903 #if BITS_PER_LONG != 32
904         flags |= O_LARGEFILE;
905 #endif
906         tmp = getname(filename);
907         fd = PTR_ERR(tmp);
908         if (!IS_ERR(tmp)) {
909                 fd = get_unused_fd();
910                 if (fd >= 0) {
911                         struct file *f = filp_open(tmp, flags, mode);
912                         error = PTR_ERR(f);
913                         if (IS_ERR(f))
914                                 goto out_error;
915                         fd_install(fd, f);
916                 }
917 out:
918                 putname(tmp);
919         }
920         return fd;
921
922 out_error:
923         put_unused_fd(fd);
924         fd = error;
925         goto out;
926 }
927
928 #ifndef __alpha__
929
930 /*
931  * For backward compatibility?  Maybe this should be moved
932  * into arch/i386 instead?
933  */
934 asmlinkage long sys_creat(const char __user * pathname, int mode)
935 {
936         return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
937 }
938
939 #endif
940
941 /*
942  * "id" is the POSIX thread ID. We use the
943  * files pointer for this..
944  */
945 int filp_close(struct file *filp, fl_owner_t id)
946 {
947         int retval;
948
949         if (!file_count(filp)) {
950                 printk(KERN_ERR "VFS: Close: file count is 0\n");
951                 return 0;
952         }
953         retval = 0;
954         if (filp->f_op && filp->f_op->flush)
955                 retval = filp->f_op->flush(filp);
956         dnotify_flush(filp, id);
957         locks_remove_posix(filp, id);
958         fput(filp);
959         return retval;
960 }
961
962 /*
963  * Careful here! We test whether the file pointer is NULL before
964  * releasing the fd. This ensures that one clone task can't release
965  * an fd while another clone is opening it.
966  */
967 asmlinkage long sys_close(unsigned int fd)
968 {
969         struct file * filp;
970         struct files_struct *files = current->files;
971
972         spin_lock(&files->file_lock);
973         if (fd >= files->max_fds)
974                 goto out_unlock;
975         filp = files->fd[fd];
976         if (!filp)
977                 goto out_unlock;
978         files->fd[fd] = NULL;
979         FD_CLR(fd, files->close_on_exec);
980         __put_unused_fd(files, fd);
981         spin_unlock(&files->file_lock);
982         return filp_close(filp, files);
983
984 out_unlock:
985         spin_unlock(&files->file_lock);
986         return -EBADF;
987 }
988
989 /*
990  * This routine simulates a hangup on the tty, to arrange that users
991  * are given clean terminals at login time.
992  */
993 asmlinkage long sys_vhangup(void)
994 {
995         if (capable(CAP_SYS_TTY_CONFIG)) {
996                 tty_vhangup(current->tty);
997                 return 0;
998         }
999         return -EPERM;
1000 }
1001
1002 /*
1003  * Called when an inode is about to be open.
1004  * We use this to disallow opening large files on 32bit systems if
1005  * the caller didn't specify O_LARGEFILE.  On 64bit systems we force
1006  * on this flag in sys_open.
1007  */
1008 int generic_file_open(struct inode * inode, struct file * filp)
1009 {
1010         if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1011                 return -EFBIG;
1012         return 0;
1013 }
1014
1015 EXPORT_SYMBOL(generic_file_open);