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