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