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