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