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