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