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