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