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