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