blob: 35c3e454458ea4e434db071fcd380eda574ce919 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/file.h>
10#include <linux/smp_lock.h>
11#include <linux/quotaops.h>
Robert Love0eeca282005-07-12 17:06:03 -040012#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080018#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/security.h>
20#include <linux/mount.h>
21#include <linux/vfs.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080022#include <linux/fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
24#include <linux/fs.h>
Yoav Zachef3daed2005-06-23 00:09:58 -070025#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/pagemap.h>
27#include <linux/syscalls.h>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070028#include <linux/rcupdate.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000029#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
David Howells726c3342006-06-23 02:02:58 -070031int vfs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 int retval = -ENODEV;
34
David Howells726c3342006-06-23 02:02:58 -070035 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 retval = -ENOSYS;
David Howells726c3342006-06-23 02:02:58 -070037 if (dentry->d_sb->s_op->statfs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 memset(buf, 0, sizeof(*buf));
David Howells726c3342006-06-23 02:02:58 -070039 retval = security_sb_statfs(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 if (retval)
41 return retval;
David Howells726c3342006-06-23 02:02:58 -070042 retval = dentry->d_sb->s_op->statfs(dentry, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 if (retval == 0 && buf->f_frsize == 0)
44 buf->f_frsize = buf->f_bsize;
45 }
46 }
47 return retval;
48}
49
50EXPORT_SYMBOL(vfs_statfs);
51
David Howells726c3342006-06-23 02:02:58 -070052static int vfs_statfs_native(struct dentry *dentry, struct statfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 struct kstatfs st;
55 int retval;
56
David Howells726c3342006-06-23 02:02:58 -070057 retval = vfs_statfs(dentry, &st);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 if (retval)
59 return retval;
60
61 if (sizeof(*buf) == sizeof(st))
62 memcpy(buf, &st, sizeof(st));
63 else {
64 if (sizeof buf->f_blocks == 4) {
65 if ((st.f_blocks | st.f_bfree | st.f_bavail) &
66 0xffffffff00000000ULL)
67 return -EOVERFLOW;
68 /*
69 * f_files and f_ffree may be -1; it's okay to stuff
70 * that into 32 bits
71 */
72 if (st.f_files != -1 &&
73 (st.f_files & 0xffffffff00000000ULL))
74 return -EOVERFLOW;
75 if (st.f_ffree != -1 &&
76 (st.f_ffree & 0xffffffff00000000ULL))
77 return -EOVERFLOW;
78 }
79
80 buf->f_type = st.f_type;
81 buf->f_bsize = st.f_bsize;
82 buf->f_blocks = st.f_blocks;
83 buf->f_bfree = st.f_bfree;
84 buf->f_bavail = st.f_bavail;
85 buf->f_files = st.f_files;
86 buf->f_ffree = st.f_ffree;
87 buf->f_fsid = st.f_fsid;
88 buf->f_namelen = st.f_namelen;
89 buf->f_frsize = st.f_frsize;
90 memset(buf->f_spare, 0, sizeof(buf->f_spare));
91 }
92 return 0;
93}
94
David Howells726c3342006-06-23 02:02:58 -070095static int vfs_statfs64(struct dentry *dentry, struct statfs64 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 struct kstatfs st;
98 int retval;
99
David Howells726c3342006-06-23 02:02:58 -0700100 retval = vfs_statfs(dentry, &st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (retval)
102 return retval;
103
104 if (sizeof(*buf) == sizeof(st))
105 memcpy(buf, &st, sizeof(st));
106 else {
107 buf->f_type = st.f_type;
108 buf->f_bsize = st.f_bsize;
109 buf->f_blocks = st.f_blocks;
110 buf->f_bfree = st.f_bfree;
111 buf->f_bavail = st.f_bavail;
112 buf->f_files = st.f_files;
113 buf->f_ffree = st.f_ffree;
114 buf->f_fsid = st.f_fsid;
115 buf->f_namelen = st.f_namelen;
116 buf->f_frsize = st.f_frsize;
117 memset(buf->f_spare, 0, sizeof(buf->f_spare));
118 }
119 return 0;
120}
121
122asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
123{
124 struct nameidata nd;
125 int error;
126
127 error = user_path_walk(path, &nd);
128 if (!error) {
129 struct statfs tmp;
David Howells726c3342006-06-23 02:02:58 -0700130 error = vfs_statfs_native(nd.dentry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
132 error = -EFAULT;
133 path_release(&nd);
134 }
135 return error;
136}
137
138
139asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
140{
141 struct nameidata nd;
142 long error;
143
144 if (sz != sizeof(*buf))
145 return -EINVAL;
146 error = user_path_walk(path, &nd);
147 if (!error) {
148 struct statfs64 tmp;
David Howells726c3342006-06-23 02:02:58 -0700149 error = vfs_statfs64(nd.dentry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
151 error = -EFAULT;
152 path_release(&nd);
153 }
154 return error;
155}
156
157
158asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
159{
160 struct file * file;
161 struct statfs tmp;
162 int error;
163
164 error = -EBADF;
165 file = fget(fd);
166 if (!file)
167 goto out;
David Howells726c3342006-06-23 02:02:58 -0700168 error = vfs_statfs_native(file->f_dentry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
170 error = -EFAULT;
171 fput(file);
172out:
173 return error;
174}
175
176asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
177{
178 struct file * file;
179 struct statfs64 tmp;
180 int error;
181
182 if (sz != sizeof(*buf))
183 return -EINVAL;
184
185 error = -EBADF;
186 file = fget(fd);
187 if (!file)
188 goto out;
David Howells726c3342006-06-23 02:02:58 -0700189 error = vfs_statfs64(file->f_dentry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
191 error = -EFAULT;
192 fput(file);
193out:
194 return error;
195}
196
NeilBrown4a301312006-01-08 01:02:39 -0800197int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
198 struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 int err;
201 struct iattr newattrs;
202
203 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
204 if (length < 0)
205 return -EINVAL;
206
207 newattrs.ia_size = length;
NeilBrown4a301312006-01-08 01:02:39 -0800208 newattrs.ia_valid = ATTR_SIZE | time_attrs;
Miklos Szeredicc4e69d2005-11-07 00:59:49 -0800209 if (filp) {
210 newattrs.ia_file = filp;
211 newattrs.ia_valid |= ATTR_FILE;
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800214 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 err = notify_change(dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800216 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return err;
218}
219
Matt Mackallb01ec0e2006-01-08 01:05:20 -0800220static long do_sys_truncate(const char __user * path, loff_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 struct nameidata nd;
223 struct inode * inode;
224 int error;
225
226 error = -EINVAL;
227 if (length < 0) /* sorry, but loff_t says... */
228 goto out;
229
230 error = user_path_walk(path, &nd);
231 if (error)
232 goto out;
233 inode = nd.dentry->d_inode;
234
235 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
236 error = -EISDIR;
237 if (S_ISDIR(inode->i_mode))
238 goto dput_and_out;
239
240 error = -EINVAL;
241 if (!S_ISREG(inode->i_mode))
242 goto dput_and_out;
243
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800244 error = vfs_permission(&nd, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (error)
246 goto dput_and_out;
247
248 error = -EROFS;
249 if (IS_RDONLY(inode))
250 goto dput_and_out;
251
252 error = -EPERM;
253 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
254 goto dput_and_out;
255
256 /*
257 * Make sure that there are no leases.
258 */
259 error = break_lease(inode, FMODE_WRITE);
260 if (error)
261 goto dput_and_out;
262
263 error = get_write_access(inode);
264 if (error)
265 goto dput_and_out;
266
267 error = locks_verify_truncate(inode, NULL, length);
268 if (!error) {
269 DQUOT_INIT(inode);
NeilBrown4a301312006-01-08 01:02:39 -0800270 error = do_truncate(nd.dentry, length, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 put_write_access(inode);
273
274dput_and_out:
275 path_release(&nd);
276out:
277 return error;
278}
279
280asmlinkage long sys_truncate(const char __user * path, unsigned long length)
281{
282 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
283 return do_sys_truncate(path, (long)length);
284}
285
Matt Mackallb01ec0e2006-01-08 01:05:20 -0800286static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 struct inode * inode;
289 struct dentry *dentry;
290 struct file * file;
291 int error;
292
293 error = -EINVAL;
294 if (length < 0)
295 goto out;
296 error = -EBADF;
297 file = fget(fd);
298 if (!file)
299 goto out;
300
301 /* explicitly opened as large or we are on 64-bit box */
302 if (file->f_flags & O_LARGEFILE)
303 small = 0;
304
305 dentry = file->f_dentry;
306 inode = dentry->d_inode;
307 error = -EINVAL;
308 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
309 goto out_putf;
310
311 error = -EINVAL;
312 /* Cannot ftruncate over 2^31 bytes without large file support */
313 if (small && length > MAX_NON_LFS)
314 goto out_putf;
315
316 error = -EPERM;
317 if (IS_APPEND(inode))
318 goto out_putf;
319
320 error = locks_verify_truncate(inode, file, length);
321 if (!error)
Peter Staubach6e656be2006-06-25 05:48:36 -0700322 error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323out_putf:
324 fput(file);
325out:
326 return error;
327}
328
329asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
330{
Linus Torvalds0a489cb2006-04-18 13:02:48 -0700331 long ret = do_sys_ftruncate(fd, length, 1);
Linus Torvalds385910f2006-04-18 13:22:59 -0700332 /* avoid REGPARM breakage on x86: */
Linus Torvalds0a489cb2006-04-18 13:02:48 -0700333 prevent_tail_call(ret);
334 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337/* LFS versions of truncate are only needed on 32 bit machines */
338#if BITS_PER_LONG == 32
339asmlinkage long sys_truncate64(const char __user * path, loff_t length)
340{
341 return do_sys_truncate(path, length);
342}
343
344asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
345{
Linus Torvalds0a489cb2006-04-18 13:02:48 -0700346 long ret = do_sys_ftruncate(fd, length, 0);
Linus Torvalds385910f2006-04-18 13:22:59 -0700347 /* avoid REGPARM breakage on x86: */
Linus Torvalds0a489cb2006-04-18 13:02:48 -0700348 prevent_tail_call(ret);
349 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351#endif
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353/*
354 * access() needs to use the real uid/gid, not the effective uid/gid.
355 * We do this by temporarily clearing all FS-related capabilities and
356 * switching the fsuid/fsgid around to the real ones.
357 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800358asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 struct nameidata nd;
361 int old_fsuid, old_fsgid;
362 kernel_cap_t old_cap;
363 int res;
364
365 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
366 return -EINVAL;
367
368 old_fsuid = current->fsuid;
369 old_fsgid = current->fsgid;
370 old_cap = current->cap_effective;
371
372 current->fsuid = current->uid;
373 current->fsgid = current->gid;
374
375 /*
376 * Clear the capabilities if we switch to a non-root user
377 *
378 * FIXME: There is a race here against sys_capset. The
379 * capabilities can change yet we will restore the old
380 * value below. We should hold task_capabilities_lock,
381 * but we cannot because user_path_walk can sleep.
382 */
383 if (current->uid)
384 cap_clear(current->cap_effective);
385 else
386 current->cap_effective = current->cap_permitted;
387
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800388 res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (!res) {
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800390 res = vfs_permission(&nd, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /* SuS v2 requires we report a read only fs too */
392 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
393 && !special_file(nd.dentry->d_inode->i_mode))
394 res = -EROFS;
395 path_release(&nd);
396 }
397
398 current->fsuid = old_fsuid;
399 current->fsgid = old_fsgid;
400 current->cap_effective = old_cap;
401
402 return res;
403}
404
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800405asmlinkage long sys_access(const char __user *filename, int mode)
406{
407 return sys_faccessat(AT_FDCWD, filename, mode);
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410asmlinkage long sys_chdir(const char __user * filename)
411{
412 struct nameidata nd;
413 int error;
414
Miklos Szeredi650a8982006-09-29 01:59:35 -0700415 error = __user_walk(filename,
416 LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (error)
418 goto out;
419
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800420 error = vfs_permission(&nd, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (error)
422 goto dput_and_out;
423
424 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
425
426dput_and_out:
427 path_release(&nd);
428out:
429 return error;
430}
431
432asmlinkage long sys_fchdir(unsigned int fd)
433{
434 struct file *file;
435 struct dentry *dentry;
436 struct inode *inode;
437 struct vfsmount *mnt;
438 int error;
439
440 error = -EBADF;
441 file = fget(fd);
442 if (!file)
443 goto out;
444
445 dentry = file->f_dentry;
446 mnt = file->f_vfsmnt;
447 inode = dentry->d_inode;
448
449 error = -ENOTDIR;
450 if (!S_ISDIR(inode->i_mode))
451 goto out_putf;
452
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800453 error = file_permission(file, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!error)
455 set_fs_pwd(current->fs, mnt, dentry);
456out_putf:
457 fput(file);
458out:
459 return error;
460}
461
462asmlinkage long sys_chroot(const char __user * filename)
463{
464 struct nameidata nd;
465 int error;
466
467 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
468 if (error)
469 goto out;
470
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800471 error = vfs_permission(&nd, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (error)
473 goto dput_and_out;
474
475 error = -EPERM;
476 if (!capable(CAP_SYS_CHROOT))
477 goto dput_and_out;
478
479 set_fs_root(current->fs, nd.mnt, nd.dentry);
480 set_fs_altroot();
481 error = 0;
482dput_and_out:
483 path_release(&nd);
484out:
485 return error;
486}
487
488asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
489{
490 struct inode * inode;
491 struct dentry * dentry;
492 struct file * file;
493 int err = -EBADF;
494 struct iattr newattrs;
495
496 file = fget(fd);
497 if (!file)
498 goto out;
499
500 dentry = file->f_dentry;
501 inode = dentry->d_inode;
502
Amy Griffis9c937dc2006-06-08 23:19:31 -0400503 audit_inode(NULL, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +0000504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 err = -EROFS;
506 if (IS_RDONLY(inode))
507 goto out_putf;
508 err = -EPERM;
509 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
510 goto out_putf;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800511 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (mode == (mode_t) -1)
513 mode = inode->i_mode;
514 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
515 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
516 err = notify_change(dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800517 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519out_putf:
520 fput(file);
521out:
522 return err;
523}
524
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800525asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
526 mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct nameidata nd;
529 struct inode * inode;
530 int error;
531 struct iattr newattrs;
532
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800533 error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (error)
535 goto out;
536 inode = nd.dentry->d_inode;
537
538 error = -EROFS;
539 if (IS_RDONLY(inode))
540 goto dput_and_out;
541
542 error = -EPERM;
543 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
544 goto dput_and_out;
545
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800546 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (mode == (mode_t) -1)
548 mode = inode->i_mode;
549 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
550 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
551 error = notify_change(nd.dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800552 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554dput_and_out:
555 path_release(&nd);
556out:
557 return error;
558}
559
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800560asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
561{
562 return sys_fchmodat(AT_FDCWD, filename, mode);
563}
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
566{
567 struct inode * inode;
568 int error;
569 struct iattr newattrs;
570
571 error = -ENOENT;
572 if (!(inode = dentry->d_inode)) {
573 printk(KERN_ERR "chown_common: NULL inode\n");
574 goto out;
575 }
576 error = -EROFS;
577 if (IS_RDONLY(inode))
578 goto out;
579 error = -EPERM;
580 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
581 goto out;
582 newattrs.ia_valid = ATTR_CTIME;
583 if (user != (uid_t) -1) {
584 newattrs.ia_valid |= ATTR_UID;
585 newattrs.ia_uid = user;
586 }
587 if (group != (gid_t) -1) {
588 newattrs.ia_valid |= ATTR_GID;
589 newattrs.ia_gid = group;
590 }
591 if (!S_ISDIR(inode->i_mode))
592 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800593 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 error = notify_change(dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800595 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596out:
597 return error;
598}
599
600asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
601{
602 struct nameidata nd;
603 int error;
604
605 error = user_path_walk(filename, &nd);
606 if (!error) {
607 error = chown_common(nd.dentry, user, group);
608 path_release(&nd);
609 }
610 return error;
611}
612
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800613asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
614 gid_t group, int flag)
615{
616 struct nameidata nd;
617 int error = -EINVAL;
618 int follow;
619
620 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
621 goto out;
622
623 follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
624 error = __user_walk_fd(dfd, filename, follow, &nd);
625 if (!error) {
626 error = chown_common(nd.dentry, user, group);
627 path_release(&nd);
628 }
629out:
630 return error;
631}
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
634{
635 struct nameidata nd;
636 int error;
637
638 error = user_path_walk_link(filename, &nd);
639 if (!error) {
640 error = chown_common(nd.dentry, user, group);
641 path_release(&nd);
642 }
643 return error;
644}
645
646
647asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
648{
649 struct file * file;
650 int error = -EBADF;
651
652 file = fget(fd);
653 if (file) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000654 struct dentry * dentry;
655 dentry = file->f_dentry;
Amy Griffis9c937dc2006-06-08 23:19:31 -0400656 audit_inode(NULL, dentry->d_inode);
Amy Griffis73241cc2005-11-03 16:00:25 +0000657 error = chown_common(dentry, user, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 fput(file);
659 }
660 return error;
661}
662
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700663static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
Trond Myklebust834f2a42005-10-18 14:20:16 -0700664 int flags, struct file *f,
665 int (*open)(struct inode *, struct file *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 struct inode *inode;
668 int error;
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 f->f_flags = flags;
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700671 f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
672 FMODE_PREAD | FMODE_PWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 inode = dentry->d_inode;
674 if (f->f_mode & FMODE_WRITE) {
675 error = get_write_access(inode);
676 if (error)
677 goto cleanup_file;
678 }
679
680 f->f_mapping = inode->i_mapping;
681 f->f_dentry = dentry;
682 f->f_vfsmnt = mnt;
683 f->f_pos = 0;
684 f->f_op = fops_get(inode->i_fop);
685 file_move(f, &inode->i_sb->s_files);
686
Trond Myklebust834f2a42005-10-18 14:20:16 -0700687 if (!open && f->f_op)
688 open = f->f_op->open;
689 if (open) {
690 error = open(inode, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (error)
692 goto cleanup_all;
693 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
696
697 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
698
699 /* NB: we're sure to have correct a_ops only after f_op->open */
700 if (f->f_flags & O_DIRECT) {
Carsten Otteceffc072005-06-23 22:05:25 -0700701 if (!f->f_mapping->a_ops ||
702 ((!f->f_mapping->a_ops->direct_IO) &&
703 (!f->f_mapping->a_ops->get_xip_page))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 fput(f);
705 f = ERR_PTR(-EINVAL);
706 }
707 }
708
709 return f;
710
711cleanup_all:
712 fops_put(f->f_op);
713 if (f->f_mode & FMODE_WRITE)
714 put_write_access(inode);
715 file_kill(f);
716 f->f_dentry = NULL;
717 f->f_vfsmnt = NULL;
718cleanup_file:
719 put_filp(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 dput(dentry);
721 mntput(mnt);
722 return ERR_PTR(error);
723}
724
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700725/*
726 * Note that while the flag value (low two bits) for sys_open means:
727 * 00 - read-only
728 * 01 - write-only
729 * 10 - read-write
730 * 11 - special
731 * it is changed into
732 * 00 - no permissions needed
733 * 01 - read-permission
734 * 10 - write-permission
735 * 11 - read-write
736 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
737 * used by symlinks.
738 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800739static struct file *do_filp_open(int dfd, const char *filename, int flags,
740 int mode)
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700741{
742 int namei_flags, error;
743 struct nameidata nd;
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700744
745 namei_flags = flags;
746 if ((namei_flags+1) & O_ACCMODE)
747 namei_flags++;
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700748
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800749 error = open_namei(dfd, filename, namei_flags, mode, &nd);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700750 if (!error)
Trond Myklebust834f2a42005-10-18 14:20:16 -0700751 return nameidata_to_filp(&nd, flags);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700752
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700753 return ERR_PTR(error);
754}
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800755
756struct file *filp_open(const char *filename, int flags, int mode)
757{
758 return do_filp_open(AT_FDCWD, filename, flags, mode);
759}
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700760EXPORT_SYMBOL(filp_open);
761
Trond Myklebust834f2a42005-10-18 14:20:16 -0700762/**
763 * lookup_instantiate_filp - instantiates the open intent filp
764 * @nd: pointer to nameidata
765 * @dentry: pointer to dentry
766 * @open: open callback
767 *
768 * Helper for filesystems that want to use lookup open intents and pass back
769 * a fully instantiated struct file to the caller.
770 * This function is meant to be called from within a filesystem's
771 * lookup method.
Oleg Drokin9a56c212006-03-25 03:07:02 -0800772 * Beware of calling it for non-regular files! Those ->open methods might block
773 * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
774 * leading to a deadlock, as nobody can open that fifo anymore, because
775 * another process to open fifo will block on locked parent when doing lookup).
Trond Myklebust834f2a42005-10-18 14:20:16 -0700776 * Note that in case of error, nd->intent.open.file is destroyed, but the
777 * path information remains valid.
778 * If the open callback is set to NULL, then the standard f_op->open()
779 * filesystem callback is substituted.
780 */
781struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
782 int (*open)(struct inode *, struct file *))
783{
784 if (IS_ERR(nd->intent.open.file))
785 goto out;
786 if (IS_ERR(dentry))
787 goto out_err;
788 nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->mnt),
789 nd->intent.open.flags - 1,
790 nd->intent.open.file,
791 open);
792out:
793 return nd->intent.open.file;
794out_err:
795 release_open_intent(nd);
796 nd->intent.open.file = (struct file *)dentry;
797 goto out;
798}
799EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
800
801/**
802 * nameidata_to_filp - convert a nameidata to an open filp.
803 * @nd: pointer to nameidata
804 * @flags: open flags
805 *
806 * Note that this function destroys the original nameidata
807 */
808struct file *nameidata_to_filp(struct nameidata *nd, int flags)
809{
810 struct file *filp;
811
812 /* Pick up the filp from the open intent */
813 filp = nd->intent.open.file;
814 /* Has the filesystem initialised the file for us? */
815 if (filp->f_dentry == NULL)
816 filp = __dentry_open(nd->dentry, nd->mnt, flags, filp, NULL);
817 else
818 path_release(nd);
819 return filp;
820}
821
Peter Staubach6fdcc212005-11-07 00:59:42 -0800822/*
823 * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
824 * error.
825 */
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700826struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
827{
828 int error;
829 struct file *f;
830
831 error = -ENFILE;
832 f = get_empty_filp();
Peter Staubach6fdcc212005-11-07 00:59:42 -0800833 if (f == NULL) {
834 dput(dentry);
835 mntput(mnt);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700836 return ERR_PTR(error);
Peter Staubach6fdcc212005-11-07 00:59:42 -0800837 }
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700838
Trond Myklebust834f2a42005-10-18 14:20:16 -0700839 return __dentry_open(dentry, mnt, flags, f, NULL);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700840}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841EXPORT_SYMBOL(dentry_open);
842
843/*
844 * Find an empty file descriptor entry, and mark it busy.
845 */
846int get_unused_fd(void)
847{
848 struct files_struct * files = current->files;
849 int fd, error;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700850 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 error = -EMFILE;
853 spin_lock(&files->file_lock);
854
855repeat:
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700856 fdt = files_fdtable(files);
857 fd = find_next_zero_bit(fdt->open_fds->fds_bits,
858 fdt->max_fdset,
Eric Dumazet0c9e63f2006-03-23 03:00:12 -0800859 files->next_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 /*
862 * N.B. For clone tasks sharing a files structure, this test
863 * will limit the total number of files that can be opened.
864 */
865 if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
866 goto out;
867
868 /* Do we need to expand the fd array or fd set? */
869 error = expand_files(files, fd);
870 if (error < 0)
871 goto out;
872
873 if (error) {
874 /*
875 * If we needed to expand the fs array we
876 * might have blocked - try again.
877 */
878 error = -EMFILE;
879 goto repeat;
880 }
881
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700882 FD_SET(fd, fdt->open_fds);
883 FD_CLR(fd, fdt->close_on_exec);
Eric Dumazet0c9e63f2006-03-23 03:00:12 -0800884 files->next_fd = fd + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885#if 1
886 /* Sanity check */
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700887 if (fdt->fd[fd] != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700889 fdt->fd[fd] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891#endif
892 error = fd;
893
894out:
895 spin_unlock(&files->file_lock);
896 return error;
897}
898
899EXPORT_SYMBOL(get_unused_fd);
900
Matt Mackallb01ec0e2006-01-08 01:05:20 -0800901static void __put_unused_fd(struct files_struct *files, unsigned int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700903 struct fdtable *fdt = files_fdtable(files);
904 __FD_CLR(fd, fdt->open_fds);
Eric Dumazet0c9e63f2006-03-23 03:00:12 -0800905 if (fd < files->next_fd)
906 files->next_fd = fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909void fastcall put_unused_fd(unsigned int fd)
910{
911 struct files_struct *files = current->files;
912 spin_lock(&files->file_lock);
913 __put_unused_fd(files, fd);
914 spin_unlock(&files->file_lock);
915}
916
917EXPORT_SYMBOL(put_unused_fd);
918
919/*
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800920 * Install a file pointer in the fd array.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 *
922 * The VFS is full of places where we drop the files lock between
923 * setting the open_fds bitmap and installing the file in the file
924 * array. At any such point, we are vulnerable to a dup2() race
925 * installing a file in the array before us. We need to detect this and
926 * fput() the struct file we are about to overwrite in this case.
927 *
928 * It should never happen - if we allow dup2() do it, _really_ bad things
929 * will follow.
930 */
931
932void fastcall fd_install(unsigned int fd, struct file * file)
933{
934 struct files_struct *files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700935 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 spin_lock(&files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700937 fdt = files_fdtable(files);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700938 BUG_ON(fdt->fd[fd] != NULL);
939 rcu_assign_pointer(fdt->fd[fd], file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 spin_unlock(&files->file_lock);
941}
942
943EXPORT_SYMBOL(fd_install);
944
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800945long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Miklos Szeredie922efc2005-09-06 15:18:25 -0700947 char *tmp = getname(filename);
948 int fd = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (!IS_ERR(tmp)) {
951 fd = get_unused_fd();
952 if (fd >= 0) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800953 struct file *f = do_filp_open(dfd, tmp, flags, mode);
Telemaque Ndizihiwefed2fc12005-06-23 00:10:33 -0700954 if (IS_ERR(f)) {
955 put_unused_fd(fd);
956 fd = PTR_ERR(f);
957 } else {
Robert Love0eeca282005-07-12 17:06:03 -0400958 fsnotify_open(f->f_dentry);
Telemaque Ndizihiwefed2fc12005-06-23 00:10:33 -0700959 fd_install(fd, f);
960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 putname(tmp);
963 }
964 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
Miklos Szeredie922efc2005-09-06 15:18:25 -0700966
967asmlinkage long sys_open(const char __user *filename, int flags, int mode)
968{
Linus Torvalds385910f2006-04-18 13:22:59 -0700969 long ret;
970
Miklos Szeredie922efc2005-09-06 15:18:25 -0700971 if (force_o_largefile())
972 flags |= O_LARGEFILE;
973
Linus Torvalds385910f2006-04-18 13:22:59 -0700974 ret = do_sys_open(AT_FDCWD, filename, flags, mode);
975 /* avoid REGPARM breakage on x86: */
976 prevent_tail_call(ret);
977 return ret;
Miklos Szeredie922efc2005-09-06 15:18:25 -0700978}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979EXPORT_SYMBOL_GPL(sys_open);
980
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800981asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
982 int mode)
983{
Linus Torvalds385910f2006-04-18 13:22:59 -0700984 long ret;
985
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800986 if (force_o_largefile())
987 flags |= O_LARGEFILE;
988
Linus Torvalds385910f2006-04-18 13:22:59 -0700989 ret = do_sys_open(dfd, filename, flags, mode);
990 /* avoid REGPARM breakage on x86: */
991 prevent_tail_call(ret);
992 return ret;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800993}
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995#ifndef __alpha__
996
997/*
998 * For backward compatibility? Maybe this should be moved
999 * into arch/i386 instead?
1000 */
1001asmlinkage long sys_creat(const char __user * pathname, int mode)
1002{
1003 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1004}
1005
1006#endif
1007
1008/*
1009 * "id" is the POSIX thread ID. We use the
1010 * files pointer for this..
1011 */
1012int filp_close(struct file *filp, fl_owner_t id)
1013{
Christoph Lameter45778ca2005-06-23 00:10:17 -07001014 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 if (!file_count(filp)) {
1017 printk(KERN_ERR "VFS: Close: file count is 0\n");
Christoph Lameter45778ca2005-06-23 00:10:17 -07001018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020
Christoph Lameter45778ca2005-06-23 00:10:17 -07001021 if (filp->f_op && filp->f_op->flush)
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -07001022 retval = filp->f_op->flush(filp, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 dnotify_flush(filp, id);
1025 locks_remove_posix(filp, id);
1026 fput(filp);
1027 return retval;
1028}
1029
1030EXPORT_SYMBOL(filp_close);
1031
1032/*
1033 * Careful here! We test whether the file pointer is NULL before
1034 * releasing the fd. This ensures that one clone task can't release
1035 * an fd while another clone is opening it.
1036 */
1037asmlinkage long sys_close(unsigned int fd)
1038{
1039 struct file * filp;
1040 struct files_struct *files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001041 struct fdtable *fdt;
Ernie Petridesee731f42006-09-29 02:00:13 -07001042 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 spin_lock(&files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001045 fdt = files_fdtable(files);
1046 if (fd >= fdt->max_fds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 goto out_unlock;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001048 filp = fdt->fd[fd];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (!filp)
1050 goto out_unlock;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -07001051 rcu_assign_pointer(fdt->fd[fd], NULL);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001052 FD_CLR(fd, fdt->close_on_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 __put_unused_fd(files, fd);
1054 spin_unlock(&files->file_lock);
Ernie Petridesee731f42006-09-29 02:00:13 -07001055 retval = filp_close(filp, files);
1056
1057 /* can't restart close syscall because file table entry was cleared */
1058 if (unlikely(retval == -ERESTARTSYS ||
1059 retval == -ERESTARTNOINTR ||
1060 retval == -ERESTARTNOHAND ||
1061 retval == -ERESTART_RESTARTBLOCK))
1062 retval = -EINTR;
1063
1064 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066out_unlock:
1067 spin_unlock(&files->file_lock);
1068 return -EBADF;
1069}
1070
1071EXPORT_SYMBOL(sys_close);
1072
1073/*
1074 * This routine simulates a hangup on the tty, to arrange that users
1075 * are given clean terminals at login time.
1076 */
1077asmlinkage long sys_vhangup(void)
1078{
1079 if (capable(CAP_SYS_TTY_CONFIG)) {
1080 tty_vhangup(current->signal->tty);
1081 return 0;
1082 }
1083 return -EPERM;
1084}
1085
1086/*
1087 * Called when an inode is about to be open.
1088 * We use this to disallow opening large files on 32bit systems if
1089 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1090 * on this flag in sys_open.
1091 */
1092int generic_file_open(struct inode * inode, struct file * filp)
1093{
1094 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1095 return -EFBIG;
1096 return 0;
1097}
1098
1099EXPORT_SYMBOL(generic_file_open);
1100
1101/*
1102 * This is used by subsystems that don't want seekable
1103 * file descriptors
1104 */
1105int nonseekable_open(struct inode *inode, struct file *filp)
1106{
1107 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1108 return 0;
1109}
1110
1111EXPORT_SYMBOL(nonseekable_open);