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