MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / open.c
blob8e539937cecabeaaf584b1335eb23f8690c08977
1 /*
2 * linux/fs/open.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
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>
26 #include <asm/unistd.h>
28 int vfs_statfs(struct super_block *sb, struct kstatfs *buf)
30 int retval = -ENODEV;
32 if (sb) {
33 retval = -ENOSYS;
34 if (sb->s_op->statfs) {
35 memset(buf, 0, sizeof(*buf));
36 retval = security_sb_statfs(sb);
37 if (retval)
38 return retval;
39 retval = sb->s_op->statfs(sb, buf);
40 if (retval == 0 && buf->f_frsize == 0)
41 buf->f_frsize = buf->f_bsize;
44 return retval;
47 EXPORT_SYMBOL(vfs_statfs);
49 static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
51 struct kstatfs st;
52 int retval;
54 retval = vfs_statfs(sb, &st);
55 if (retval)
56 return retval;
58 if (sizeof(*buf) == sizeof(st))
59 memcpy(buf, &st, sizeof(st));
60 else {
61 if (sizeof buf->f_blocks == 4) {
62 if ((st.f_blocks | st.f_bfree | st.f_bavail) &
63 0xffffffff00000000ULL)
64 return -EOVERFLOW;
66 * f_files and f_ffree may be -1; it's okay to stuff
67 * that into 32 bits
69 if (st.f_files != -1 &&
70 (st.f_files & 0xffffffff00000000ULL))
71 return -EOVERFLOW;
72 if (st.f_ffree != -1 &&
73 (st.f_ffree & 0xffffffff00000000ULL))
74 return -EOVERFLOW;
77 buf->f_type = st.f_type;
78 buf->f_bsize = st.f_bsize;
79 buf->f_blocks = st.f_blocks;
80 buf->f_bfree = st.f_bfree;
81 buf->f_bavail = st.f_bavail;
82 buf->f_files = st.f_files;
83 buf->f_ffree = st.f_ffree;
84 buf->f_fsid = st.f_fsid;
85 buf->f_namelen = st.f_namelen;
86 buf->f_frsize = st.f_frsize;
87 memset(buf->f_spare, 0, sizeof(buf->f_spare));
89 return 0;
92 static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
94 struct kstatfs st;
95 int retval;
97 retval = vfs_statfs(sb, &st);
98 if (retval)
99 return retval;
101 if (sizeof(*buf) == sizeof(st))
102 memcpy(buf, &st, sizeof(st));
103 else {
104 buf->f_type = st.f_type;
105 buf->f_bsize = st.f_bsize;
106 buf->f_blocks = st.f_blocks;
107 buf->f_bfree = st.f_bfree;
108 buf->f_bavail = st.f_bavail;
109 buf->f_files = st.f_files;
110 buf->f_ffree = st.f_ffree;
111 buf->f_fsid = st.f_fsid;
112 buf->f_namelen = st.f_namelen;
113 buf->f_frsize = st.f_frsize;
114 memset(buf->f_spare, 0, sizeof(buf->f_spare));
116 return 0;
119 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
121 struct nameidata nd;
122 int error;
124 error = user_path_walk(path, &nd);
125 if (!error) {
126 struct statfs tmp;
127 error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
128 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
129 error = -EFAULT;
130 path_release(&nd);
132 return error;
136 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
138 struct nameidata nd;
139 long error;
141 if (sz != sizeof(*buf))
142 return -EINVAL;
143 error = user_path_walk(path, &nd);
144 if (!error) {
145 struct statfs64 tmp;
146 error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
147 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
148 error = -EFAULT;
149 path_release(&nd);
151 return error;
155 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
157 struct file * file;
158 struct statfs tmp;
159 int error;
161 error = -EBADF;
162 file = fget(fd);
163 if (!file)
164 goto out;
165 error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
166 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
167 error = -EFAULT;
168 fput(file);
169 out:
170 return error;
173 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
175 struct file * file;
176 struct statfs64 tmp;
177 int error;
179 if (sz != sizeof(*buf))
180 return -EINVAL;
182 error = -EBADF;
183 file = fget(fd);
184 if (!file)
185 goto out;
186 error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
187 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
188 error = -EFAULT;
189 fput(file);
190 out:
191 return error;
194 int do_truncate(struct dentry *dentry, loff_t length)
196 int err;
197 struct iattr newattrs;
199 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
200 if (length < 0)
201 return -EINVAL;
203 newattrs.ia_size = length;
204 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
205 down(&dentry->d_inode->i_sem);
206 down_write(&dentry->d_inode->i_alloc_sem);
207 err = notify_change(dentry, &newattrs);
208 up_write(&dentry->d_inode->i_alloc_sem);
209 up(&dentry->d_inode->i_sem);
210 return err;
213 static inline long do_sys_truncate(const char __user * path, loff_t length)
215 struct nameidata nd;
216 struct inode * inode;
217 int error;
219 error = -EINVAL;
220 if (length < 0) /* sorry, but loff_t says... */
221 goto out;
223 error = user_path_walk(path, &nd);
224 if (error)
225 goto out;
226 inode = nd.dentry->d_inode;
228 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
229 error = -EISDIR;
230 if (S_ISDIR(inode->i_mode))
231 goto dput_and_out;
233 error = -EINVAL;
234 if (!S_ISREG(inode->i_mode))
235 goto dput_and_out;
237 error = permission(inode,MAY_WRITE,&nd);
238 if (error)
239 goto dput_and_out;
241 error = -EROFS;
242 if (IS_RDONLY(inode))
243 goto dput_and_out;
245 error = -EPERM;
246 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
247 goto dput_and_out;
250 * Make sure that there are no leases.
252 error = break_lease(inode, FMODE_WRITE);
253 if (error)
254 goto dput_and_out;
256 error = get_write_access(inode);
257 if (error)
258 goto dput_and_out;
260 error = locks_verify_truncate(inode, NULL, length);
261 if (!error) {
262 DQUOT_INIT(inode);
263 error = do_truncate(nd.dentry, length);
265 put_write_access(inode);
267 dput_and_out:
268 path_release(&nd);
269 out:
270 return error;
273 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
275 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
276 return do_sys_truncate(path, (long)length);
279 static inline long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
281 struct inode * inode;
282 struct dentry *dentry;
283 struct file * file;
284 int error;
286 error = -EINVAL;
287 if (length < 0)
288 goto out;
289 error = -EBADF;
290 file = fget(fd);
291 if (!file)
292 goto out;
294 /* explicitly opened as large or we are on 64-bit box */
295 if (file->f_flags & O_LARGEFILE)
296 small = 0;
298 dentry = file->f_dentry;
299 inode = dentry->d_inode;
300 error = -EINVAL;
301 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
302 goto out_putf;
304 error = -EINVAL;
305 /* Cannot ftruncate over 2^31 bytes without large file support */
306 if (small && length > MAX_NON_LFS)
307 goto out_putf;
309 error = -EPERM;
310 if (IS_APPEND(inode))
311 goto out_putf;
313 error = locks_verify_truncate(inode, file, length);
314 if (!error)
315 error = do_truncate(dentry, length);
316 out_putf:
317 fput(file);
318 out:
319 return error;
322 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
324 return do_sys_ftruncate(fd, length, 1);
327 /* LFS versions of truncate are only needed on 32 bit machines */
328 #if BITS_PER_LONG == 32
329 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
331 return do_sys_truncate(path, length);
334 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
336 return do_sys_ftruncate(fd, length, 0);
338 #endif
340 #ifdef __ARCH_WANT_SYS_UTIME
343 * sys_utime() can be implemented in user-level using sys_utimes().
344 * Is this for backwards compatibility? If so, why not move it
345 * into the appropriate arch directory (for those architectures that
346 * need it).
349 /* If times==NULL, set access and modification to current time,
350 * must be owner or have write permission.
351 * Else, update from *times, must be owner or super user.
353 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
355 int error;
356 struct nameidata nd;
357 struct inode * inode;
358 struct iattr newattrs;
360 error = user_path_walk(filename, &nd);
361 if (error)
362 goto out;
363 inode = nd.dentry->d_inode;
365 error = -EROFS;
366 if (IS_RDONLY(inode))
367 goto dput_and_out;
369 /* Don't worry, the checks are done in inode_change_ok() */
370 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
371 if (times) {
372 error = -EPERM;
373 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
374 goto dput_and_out;
376 error = get_user(newattrs.ia_atime.tv_sec, &times->actime);
377 newattrs.ia_atime.tv_nsec = 0;
378 if (!error)
379 error = get_user(newattrs.ia_mtime.tv_sec, &times->modtime);
380 newattrs.ia_mtime.tv_nsec = 0;
381 if (error)
382 goto dput_and_out;
384 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
385 } else {
386 error = -EACCES;
387 if (IS_IMMUTABLE(inode))
388 goto dput_and_out;
390 if (current->fsuid != inode->i_uid &&
391 (error = permission(inode,MAY_WRITE,&nd)) != 0)
392 goto dput_and_out;
394 down(&inode->i_sem);
395 error = notify_change(nd.dentry, &newattrs);
396 up(&inode->i_sem);
397 dput_and_out:
398 path_release(&nd);
399 out:
400 return error;
403 #endif
405 /* If times==NULL, set access and modification to current time,
406 * must be owner or have write permission.
407 * Else, update from *times, must be owner or super user.
409 long do_utimes(char __user * filename, struct timeval * times)
411 int error;
412 struct nameidata nd;
413 struct inode * inode;
414 struct iattr newattrs;
416 error = user_path_walk(filename, &nd);
418 if (error)
419 goto out;
420 inode = nd.dentry->d_inode;
422 error = -EROFS;
423 if (IS_RDONLY(inode))
424 goto dput_and_out;
426 /* Don't worry, the checks are done in inode_change_ok() */
427 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
428 if (times) {
429 error = -EPERM;
430 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
431 goto dput_and_out;
433 newattrs.ia_atime.tv_sec = times[0].tv_sec;
434 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
435 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
436 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
437 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
438 } else {
439 error = -EACCES;
440 if (IS_IMMUTABLE(inode))
441 goto dput_and_out;
443 if (current->fsuid != inode->i_uid &&
444 (error = permission(inode,MAY_WRITE,&nd)) != 0)
445 goto dput_and_out;
447 down(&inode->i_sem);
448 error = notify_change(nd.dentry, &newattrs);
449 up(&inode->i_sem);
450 dput_and_out:
451 path_release(&nd);
452 out:
453 return error;
456 asmlinkage long sys_utimes(char __user * filename, struct timeval __user * utimes)
458 struct timeval times[2];
460 if (utimes && copy_from_user(&times, utimes, sizeof(times)))
461 return -EFAULT;
462 return do_utimes(filename, utimes ? times : NULL);
467 * access() needs to use the real uid/gid, not the effective uid/gid.
468 * We do this by temporarily clearing all FS-related capabilities and
469 * switching the fsuid/fsgid around to the real ones.
471 asmlinkage long sys_access(const char __user * filename, int mode)
473 struct nameidata nd;
474 int old_fsuid, old_fsgid;
475 kernel_cap_t old_cap;
476 int res;
478 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
479 return -EINVAL;
481 old_fsuid = current->fsuid;
482 old_fsgid = current->fsgid;
483 old_cap = current->cap_effective;
485 current->fsuid = current->uid;
486 current->fsgid = current->gid;
489 * Clear the capabilities if we switch to a non-root user
491 * FIXME: There is a race here against sys_capset. The
492 * capabilities can change yet we will restore the old
493 * value below. We should hold task_capabilities_lock,
494 * but we cannot because user_path_walk can sleep.
496 if (current->uid)
497 cap_clear(current->cap_effective);
498 else
499 current->cap_effective = current->cap_permitted;
501 res = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
502 if (!res) {
503 res = permission(nd.dentry->d_inode, mode, &nd);
504 /* SuS v2 requires we report a read only fs too */
505 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
506 && !special_file(nd.dentry->d_inode->i_mode))
507 res = -EROFS;
508 path_release(&nd);
511 current->fsuid = old_fsuid;
512 current->fsgid = old_fsgid;
513 current->cap_effective = old_cap;
515 return res;
518 asmlinkage long sys_chdir(const char __user * filename)
520 struct nameidata nd;
521 int error;
523 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
524 if (error)
525 goto out;
527 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
528 if (error)
529 goto dput_and_out;
531 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
533 dput_and_out:
534 path_release(&nd);
535 out:
536 return error;
539 asmlinkage long sys_fchdir(unsigned int fd)
541 struct file *file;
542 struct dentry *dentry;
543 struct inode *inode;
544 struct vfsmount *mnt;
545 int error;
547 error = -EBADF;
548 file = fget(fd);
549 if (!file)
550 goto out;
552 dentry = file->f_dentry;
553 mnt = file->f_vfsmnt;
554 inode = dentry->d_inode;
556 error = -ENOTDIR;
557 if (!S_ISDIR(inode->i_mode))
558 goto out_putf;
560 error = permission(inode, MAY_EXEC, NULL);
561 if (!error)
562 set_fs_pwd(current->fs, mnt, dentry);
563 out_putf:
564 fput(file);
565 out:
566 return error;
569 asmlinkage long sys_chroot(const char __user * filename)
571 struct nameidata nd;
572 int error;
574 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
575 if (error)
576 goto out;
578 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
579 if (error)
580 goto dput_and_out;
582 error = -EPERM;
583 if (!capable(CAP_SYS_CHROOT))
584 goto dput_and_out;
586 set_fs_root(current->fs, nd.mnt, nd.dentry);
587 set_fs_altroot();
588 error = 0;
589 dput_and_out:
590 path_release(&nd);
591 out:
592 return error;
595 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
597 struct inode * inode;
598 struct dentry * dentry;
599 struct file * file;
600 int err = -EBADF;
601 struct iattr newattrs;
603 file = fget(fd);
604 if (!file)
605 goto out;
607 dentry = file->f_dentry;
608 inode = dentry->d_inode;
610 err = -EROFS;
611 if (IS_RDONLY(inode))
612 goto out_putf;
613 err = -EPERM;
614 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
615 goto out_putf;
616 down(&inode->i_sem);
617 if (mode == (mode_t) -1)
618 mode = inode->i_mode;
619 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
620 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
621 err = notify_change(dentry, &newattrs);
622 up(&inode->i_sem);
624 out_putf:
625 fput(file);
626 out:
627 return err;
630 asmlinkage long sys_chmod(const char __user * filename, mode_t mode)
632 struct nameidata nd;
633 struct inode * inode;
634 int error;
635 struct iattr newattrs;
637 error = user_path_walk(filename, &nd);
638 if (error)
639 goto out;
640 inode = nd.dentry->d_inode;
642 error = -EROFS;
643 if (IS_RDONLY(inode))
644 goto dput_and_out;
646 error = -EPERM;
647 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
648 goto dput_and_out;
650 down(&inode->i_sem);
651 if (mode == (mode_t) -1)
652 mode = inode->i_mode;
653 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
654 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
655 error = notify_change(nd.dentry, &newattrs);
656 up(&inode->i_sem);
658 dput_and_out:
659 path_release(&nd);
660 out:
661 return error;
664 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
666 struct inode * inode;
667 int error;
668 struct iattr newattrs;
670 error = -ENOENT;
671 if (!(inode = dentry->d_inode)) {
672 printk(KERN_ERR "chown_common: NULL inode\n");
673 goto out;
675 error = -EROFS;
676 if (IS_RDONLY(inode))
677 goto out;
678 error = -EPERM;
679 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
680 goto out;
681 newattrs.ia_valid = ATTR_CTIME;
682 if (user != (uid_t) -1) {
683 newattrs.ia_valid |= ATTR_UID;
684 newattrs.ia_uid = user;
686 if (group != (gid_t) -1) {
687 newattrs.ia_valid |= ATTR_GID;
688 newattrs.ia_gid = group;
690 if (!S_ISDIR(inode->i_mode))
691 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
692 down(&inode->i_sem);
693 error = notify_change(dentry, &newattrs);
694 up(&inode->i_sem);
695 out:
696 return error;
699 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
701 struct nameidata nd;
702 int error;
704 error = user_path_walk(filename, &nd);
705 if (!error) {
706 error = chown_common(nd.dentry, user, group);
707 path_release(&nd);
709 return error;
712 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
714 struct nameidata nd;
715 int error;
717 error = user_path_walk_link(filename, &nd);
718 if (!error) {
719 error = chown_common(nd.dentry, user, group);
720 path_release(&nd);
722 return error;
726 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
728 struct file * file;
729 int error = -EBADF;
731 file = fget(fd);
732 if (file) {
733 error = chown_common(file->f_dentry, user, group);
734 fput(file);
736 return error;
740 * Note that while the flag value (low two bits) for sys_open means:
741 * 00 - read-only
742 * 01 - write-only
743 * 10 - read-write
744 * 11 - special
745 * it is changed into
746 * 00 - no permissions needed
747 * 01 - read-permission
748 * 10 - write-permission
749 * 11 - read-write
750 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
751 * used by symlinks.
753 struct file *filp_open(const char * filename, int flags, int mode)
755 int namei_flags, error;
756 struct nameidata nd;
758 namei_flags = flags;
759 if ((namei_flags+1) & O_ACCMODE)
760 namei_flags++;
761 if (namei_flags & O_TRUNC)
762 namei_flags |= 2;
764 error = open_namei(filename, namei_flags, mode, &nd);
765 if (!error)
766 return dentry_open(nd.dentry, nd.mnt, flags);
768 return ERR_PTR(error);
771 EXPORT_SYMBOL(filp_open);
773 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
775 struct file * f;
776 struct inode *inode;
777 int error;
779 error = -ENFILE;
780 f = get_empty_filp();
781 if (!f)
782 goto cleanup_dentry;
783 f->f_flags = flags;
784 f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
785 inode = dentry->d_inode;
786 if (f->f_mode & FMODE_WRITE) {
787 error = get_write_access(inode);
788 if (error)
789 goto cleanup_file;
792 f->f_mapping = inode->i_mapping;
793 f->f_dentry = dentry;
794 f->f_vfsmnt = mnt;
795 f->f_pos = 0;
796 f->f_op = fops_get(inode->i_fop);
797 file_move(f, &inode->i_sb->s_files);
799 if (f->f_op && f->f_op->open) {
800 error = f->f_op->open(inode,f);
801 if (error)
802 goto cleanup_all;
804 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
806 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
808 /* NB: we're sure to have correct a_ops only after f_op->open */
809 if (f->f_flags & O_DIRECT) {
810 if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO) {
811 fput(f);
812 f = ERR_PTR(-EINVAL);
816 return f;
818 cleanup_all:
819 fops_put(f->f_op);
820 if (f->f_mode & FMODE_WRITE)
821 put_write_access(inode);
822 file_kill(f);
823 f->f_dentry = NULL;
824 f->f_vfsmnt = NULL;
825 cleanup_file:
826 put_filp(f);
827 cleanup_dentry:
828 dput(dentry);
829 mntput(mnt);
830 return ERR_PTR(error);
833 EXPORT_SYMBOL(dentry_open);
836 * Find an empty file descriptor entry, and mark it busy.
838 int get_unused_fd(void)
840 struct files_struct * files = current->files;
841 int fd, error;
843 error = -EMFILE;
844 spin_lock(&files->file_lock);
846 repeat:
847 fd = find_next_zero_bit(files->open_fds->fds_bits,
848 files->max_fdset,
849 files->next_fd);
852 * N.B. For clone tasks sharing a files structure, this test
853 * will limit the total number of files that can be opened.
855 if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
856 goto out;
858 /* Do we need to expand the fdset array? */
859 if (fd >= files->max_fdset) {
860 error = expand_fdset(files, fd);
861 if (!error) {
862 error = -EMFILE;
863 goto repeat;
865 goto out;
869 * Check whether we need to expand the fd array.
871 if (fd >= files->max_fds) {
872 error = expand_fd_array(files, fd);
873 if (!error) {
874 error = -EMFILE;
875 goto repeat;
877 goto out;
880 FD_SET(fd, files->open_fds);
881 FD_CLR(fd, files->close_on_exec);
882 files->next_fd = fd + 1;
883 #if 1
884 /* Sanity check */
885 if (files->fd[fd] != NULL) {
886 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
887 files->fd[fd] = NULL;
889 #endif
890 error = fd;
892 out:
893 spin_unlock(&files->file_lock);
894 return error;
897 EXPORT_SYMBOL(get_unused_fd);
899 static inline void __put_unused_fd(struct files_struct *files, unsigned int fd)
901 __FD_CLR(fd, files->open_fds);
902 if (fd < files->next_fd)
903 files->next_fd = fd;
906 void fastcall put_unused_fd(unsigned int fd)
908 struct files_struct *files = current->files;
909 spin_lock(&files->file_lock);
910 __put_unused_fd(files, fd);
911 spin_unlock(&files->file_lock);
914 EXPORT_SYMBOL(put_unused_fd);
917 * Install a file pointer in the fd array.
919 * The VFS is full of places where we drop the files lock between
920 * setting the open_fds bitmap and installing the file in the file
921 * array. At any such point, we are vulnerable to a dup2() race
922 * installing a file in the array before us. We need to detect this and
923 * fput() the struct file we are about to overwrite in this case.
925 * It should never happen - if we allow dup2() do it, _really_ bad things
926 * will follow.
929 void fastcall fd_install(unsigned int fd, struct file * file)
931 struct files_struct *files = current->files;
932 spin_lock(&files->file_lock);
933 if (unlikely(files->fd[fd] != NULL))
934 BUG();
935 files->fd[fd] = file;
936 spin_unlock(&files->file_lock);
939 EXPORT_SYMBOL(fd_install);
941 asmlinkage long sys_open(const char __user * filename, int flags, int mode)
943 char * tmp;
944 int fd, error;
946 #if BITS_PER_LONG != 32
947 flags |= O_LARGEFILE;
948 #endif
949 tmp = getname(filename);
950 fd = PTR_ERR(tmp);
951 if (!IS_ERR(tmp)) {
952 fd = get_unused_fd();
953 if (fd >= 0) {
954 struct file *f = filp_open(tmp, flags, mode);
955 error = PTR_ERR(f);
956 if (IS_ERR(f))
957 goto out_error;
958 fd_install(fd, f);
960 out:
961 putname(tmp);
963 return fd;
965 out_error:
966 put_unused_fd(fd);
967 fd = error;
968 goto out;
970 EXPORT_SYMBOL_GPL(sys_open);
972 #ifndef __alpha__
975 * For backward compatibility? Maybe this should be moved
976 * into arch/i386 instead?
978 asmlinkage long sys_creat(const char __user * pathname, int mode)
980 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
983 #endif
986 * "id" is the POSIX thread ID. We use the
987 * files pointer for this..
989 int filp_close(struct file *filp, fl_owner_t id)
991 int retval;
993 /* Report and clear outstanding errors */
994 retval = filp->f_error;
995 if (retval)
996 filp->f_error = 0;
998 if (!file_count(filp)) {
999 printk(KERN_ERR "VFS: Close: file count is 0\n");
1000 return retval;
1003 if (filp->f_op && filp->f_op->flush) {
1004 int err = filp->f_op->flush(filp);
1005 if (!retval)
1006 retval = err;
1009 dnotify_flush(filp, id);
1010 locks_remove_posix(filp, id);
1011 fput(filp);
1012 return retval;
1015 EXPORT_SYMBOL(filp_close);
1018 * Careful here! We test whether the file pointer is NULL before
1019 * releasing the fd. This ensures that one clone task can't release
1020 * an fd while another clone is opening it.
1022 asmlinkage long sys_close(unsigned int fd)
1024 struct file * filp;
1025 struct files_struct *files = current->files;
1027 spin_lock(&files->file_lock);
1028 if (fd >= files->max_fds)
1029 goto out_unlock;
1030 filp = files->fd[fd];
1031 if (!filp)
1032 goto out_unlock;
1033 files->fd[fd] = NULL;
1034 FD_CLR(fd, files->close_on_exec);
1035 __put_unused_fd(files, fd);
1036 spin_unlock(&files->file_lock);
1037 return filp_close(filp, files);
1039 out_unlock:
1040 spin_unlock(&files->file_lock);
1041 return -EBADF;
1044 EXPORT_SYMBOL(sys_close);
1047 * This routine simulates a hangup on the tty, to arrange that users
1048 * are given clean terminals at login time.
1050 asmlinkage long sys_vhangup(void)
1052 if (capable(CAP_SYS_TTY_CONFIG)) {
1053 tty_vhangup(current->signal->tty);
1054 return 0;
1056 return -EPERM;
1060 * Called when an inode is about to be open.
1061 * We use this to disallow opening large files on 32bit systems if
1062 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1063 * on this flag in sys_open.
1065 int generic_file_open(struct inode * inode, struct file * filp)
1067 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1068 return -EFBIG;
1069 return 0;
1072 EXPORT_SYMBOL(generic_file_open);
1075 * This is used by subsystems that don't want seekable
1076 * file descriptors
1078 int nonseekable_open(struct inode *inode, struct file *filp)
1080 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1081 return 0;
1084 EXPORT_SYMBOL(nonseekable_open);