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