blob: a1450086e92f87e64007351ab673bf6274037e70 [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>
Al Viro9f3acc32008-04-24 07:44:08 -040010#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#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>
Amit Arora97ac7352007-07-17 21:42:44 -040030#include <linux/falloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
David Howells726c3342006-06-23 02:02:58 -070032int vfs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 int retval = -ENODEV;
35
David Howells726c3342006-06-23 02:02:58 -070036 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 retval = -ENOSYS;
David Howells726c3342006-06-23 02:02:58 -070038 if (dentry->d_sb->s_op->statfs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 memset(buf, 0, sizeof(*buf));
David Howells726c3342006-06-23 02:02:58 -070040 retval = security_sb_statfs(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 if (retval)
42 return retval;
David Howells726c3342006-06-23 02:02:58 -070043 retval = dentry->d_sb->s_op->statfs(dentry, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 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
David Howells726c3342006-06-23 02:02:58 -070053static int vfs_statfs_native(struct dentry *dentry, struct statfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 struct kstatfs st;
56 int retval;
57
David Howells726c3342006-06-23 02:02:58 -070058 retval = vfs_statfs(dentry, &st);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 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
David Howells726c3342006-06-23 02:02:58 -070096static int vfs_statfs64(struct dentry *dentry, struct statfs64 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 struct kstatfs st;
99 int retval;
100
David Howells726c3342006-06-23 02:02:58 -0700101 retval = vfs_statfs(dentry, &st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 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;
Jan Blunck4ac91372008-02-14 19:34:32 -0800131 error = vfs_statfs_native(nd.path.dentry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
133 error = -EFAULT;
Jan Blunck1d957f92008-02-14 19:34:35 -0800134 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
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;
Jan Blunck4ac91372008-02-14 19:34:32 -0800150 error = vfs_statfs64(nd.path.dentry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
152 error = -EFAULT;
Jan Blunck1d957f92008-02-14 19:34:35 -0800153 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
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;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800169 error = vfs_statfs_native(file->f_path.dentry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 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;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800190 error = vfs_statfs64(file->f_path.dentry, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
192 error = -EFAULT;
193 fput(file);
194out:
195 return error;
196}
197
NeilBrown4a301312006-01-08 01:02:39 -0800198int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
199 struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
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;
NeilBrown4a301312006-01-08 01:02:39 -0800209 newattrs.ia_valid = ATTR_SIZE | time_attrs;
Miklos Szeredicc4e69d2005-11-07 00:59:49 -0800210 if (filp) {
211 newattrs.ia_file = filp;
212 newattrs.ia_valid |= ATTR_FILE;
213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Linus Torvalds7b82dc02007-05-08 20:10:00 -0700215 /* Remove suid/sgid on truncate too */
216 newattrs.ia_valid |= should_remove_suid(dentry);
217
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800218 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 err = notify_change(dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800220 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return err;
222}
223
Matt Mackallb01ec0e2006-01-08 01:05:20 -0800224static long do_sys_truncate(const char __user * path, loff_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
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;
Jan Blunck4ac91372008-02-14 19:34:32 -0800237 inode = nd.path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
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
Dave Hansen9ac9b842008-02-15 14:37:52 -0800248 error = mnt_want_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (error)
250 goto dput_and_out;
251
Dave Hansen9ac9b842008-02-15 14:37:52 -0800252 error = vfs_permission(&nd, MAY_WRITE);
253 if (error)
254 goto mnt_drop_write_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 error = -EPERM;
257 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Dave Hansen9ac9b842008-02-15 14:37:52 -0800258 goto mnt_drop_write_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 error = get_write_access(inode);
261 if (error)
Dave Hansen9ac9b842008-02-15 14:37:52 -0800262 goto mnt_drop_write_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
david m. richter97003822007-07-31 00:39:12 -0700264 /*
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);
269 if (error)
270 goto put_write_and_out;
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 error = locks_verify_truncate(inode, NULL, length);
273 if (!error) {
274 DQUOT_INIT(inode);
Jan Blunck4ac91372008-02-14 19:34:32 -0800275 error = do_truncate(nd.path.dentry, length, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
david m. richter97003822007-07-31 00:39:12 -0700278put_write_and_out:
279 put_write_access(inode);
Dave Hansen9ac9b842008-02-15 14:37:52 -0800280mnt_drop_write_and_out:
281 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282dput_and_out:
Jan Blunck1d957f92008-02-14 19:34:35 -0800283 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284out:
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
Matt Mackallb01ec0e2006-01-08 01:05:20 -0800294static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
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
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800313 dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 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)
Peter Staubach6e656be2006-06-25 05:48:36 -0700330 error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331out_putf:
332 fput(file);
333out:
334 return error;
335}
336
337asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
338{
Linus Torvalds0a489cb2006-04-18 13:02:48 -0700339 long ret = do_sys_ftruncate(fd, length, 1);
Linus Torvalds385910f2006-04-18 13:22:59 -0700340 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -0700341 asmlinkage_protect(2, ret, fd, length);
Linus Torvalds0a489cb2006-04-18 13:02:48 -0700342 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
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{
Linus Torvalds0a489cb2006-04-18 13:02:48 -0700354 long ret = do_sys_ftruncate(fd, length, 0);
Linus Torvalds385910f2006-04-18 13:22:59 -0700355 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -0700356 asmlinkage_protect(2, ret, fd, length);
Linus Torvalds0a489cb2006-04-18 13:02:48 -0700357 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359#endif
360
Amit Arora97ac7352007-07-17 21:42:44 -0400361asmlinkage 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
Ulrich Drepper0d786d42007-07-23 18:43:46 -0700411 ret = -EOPNOTSUPP;
Amit Arora97ac7352007-07-17 21:42:44 -0400412
413out_fput:
414 fput(file);
415out:
416 return ret;
417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/*
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 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800424asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
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
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800454 res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
Dave Hansen6902d922006-09-30 23:29:01 -0700455 if (res)
456 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Dave Hansen6902d922006-09-30 23:29:01 -0700458 res = vfs_permission(&nd, mode);
459 /* SuS v2 requires we report a read only fs too */
460 if(res || !(mode & S_IWOTH) ||
Jan Blunck4ac91372008-02-14 19:34:32 -0800461 special_file(nd.path.dentry->d_inode->i_mode))
Dave Hansen6902d922006-09-30 23:29:01 -0700462 goto out_path_release;
Dave Hansen2f676cb2008-02-15 14:37:55 -0800463 /*
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))
Dave Hansen6902d922006-09-30 23:29:01 -0700474 res = -EROFS;
475
476out_path_release:
Jan Blunck1d957f92008-02-14 19:34:35 -0800477 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -0700478out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 current->fsuid = old_fsuid;
480 current->fsgid = old_fsgid;
481 current->cap_effective = old_cap;
482
483 return res;
484}
485
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800486asmlinkage long sys_access(const char __user *filename, int mode)
487{
488 return sys_faccessat(AT_FDCWD, filename, mode);
489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491asmlinkage long sys_chdir(const char __user * filename)
492{
493 struct nameidata nd;
494 int error;
495
Miklos Szeredi650a8982006-09-29 01:59:35 -0700496 error = __user_walk(filename,
497 LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (error)
499 goto out;
500
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800501 error = vfs_permission(&nd, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (error)
503 goto dput_and_out;
504
Jan Blunckac748a02008-02-14 19:34:39 -0800505 set_fs_pwd(current->fs, &nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507dput_and_out:
Jan Blunck1d957f92008-02-14 19:34:35 -0800508 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509out:
510 return error;
511}
512
513asmlinkage long sys_fchdir(unsigned int fd)
514{
515 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 int error;
518
519 error = -EBADF;
520 file = fget(fd);
521 if (!file)
522 goto out;
523
Jan Blunckac748a02008-02-14 19:34:39 -0800524 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 error = -ENOTDIR;
527 if (!S_ISDIR(inode->i_mode))
528 goto out_putf;
529
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800530 error = file_permission(file, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (!error)
Jan Blunckac748a02008-02-14 19:34:39 -0800532 set_fs_pwd(current->fs, &file->f_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533out_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
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800548 error = vfs_permission(&nd, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (error)
550 goto dput_and_out;
551
552 error = -EPERM;
553 if (!capable(CAP_SYS_CHROOT))
554 goto dput_and_out;
555
Jan Blunckac748a02008-02-14 19:34:39 -0800556 set_fs_root(current->fs, &nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 set_fs_altroot();
558 error = 0;
559dput_and_out:
Jan Blunck1d957f92008-02-14 19:34:35 -0800560 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561out:
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
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800577 dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 inode = dentry->d_inode;
579
Al Viro5a190ae2007-06-07 12:19:32 -0400580 audit_inode(NULL, dentry);
Amy Griffis73241cc2005-11-03 16:00:25 +0000581
Dave Hansen2af482a2008-02-15 14:37:50 -0800582 err = mnt_want_write(file->f_path.mnt);
583 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 goto out_putf;
585 err = -EPERM;
586 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Dave Hansen2af482a2008-02-15 14:37:50 -0800587 goto out_drop_write;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800588 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 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);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800594 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Dave Hansen2af482a2008-02-15 14:37:50 -0800596out_drop_write:
597 mnt_drop_write(file->f_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598out_putf:
599 fput(file);
600out:
601 return err;
602}
603
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800604asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
605 mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 struct nameidata nd;
608 struct inode * inode;
609 int error;
610 struct iattr newattrs;
611
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800612 error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (error)
614 goto out;
Jan Blunck4ac91372008-02-14 19:34:32 -0800615 inode = nd.path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Dave Hansen2af482a2008-02-15 14:37:50 -0800617 error = mnt_want_write(nd.path.mnt);
618 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 goto dput_and_out;
620
621 error = -EPERM;
622 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Dave Hansen2af482a2008-02-15 14:37:50 -0800623 goto out_drop_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800625 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 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;
Jan Blunck4ac91372008-02-14 19:34:32 -0800630 error = notify_change(nd.path.dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800631 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Dave Hansen2af482a2008-02-15 14:37:50 -0800633out_drop_write:
634 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635dput_and_out:
Jan Blunck1d957f92008-02-14 19:34:35 -0800636 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637out:
638 return error;
639}
640
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800641asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
642{
643 return sys_fchmodat(AT_FDCWD, filename, mode);
644}
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646static 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 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))
Serge E. Hallynb5376772007-10-16 23:31:36 -0700670 newattrs.ia_valid |=
671 ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800672 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 error = notify_change(dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800674 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675out:
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);
Dave Hansen6902d922006-09-30 23:29:01 -0700685 if (error)
686 goto out;
Dave Hansen2af482a2008-02-15 14:37:50 -0800687 error = mnt_want_write(nd.path.mnt);
688 if (error)
689 goto out_release;
Jan Blunck4ac91372008-02-14 19:34:32 -0800690 error = chown_common(nd.path.dentry, user, group);
Dave Hansen2af482a2008-02-15 14:37:50 -0800691 mnt_drop_write(nd.path.mnt);
692out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -0800693 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -0700694out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return error;
696}
697
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800698asmlinkage 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);
Dave Hansen6902d922006-09-30 23:29:01 -0700710 if (error)
711 goto out;
Dave Hansen2af482a2008-02-15 14:37:50 -0800712 error = mnt_want_write(nd.path.mnt);
713 if (error)
714 goto out_release;
Jan Blunck4ac91372008-02-14 19:34:32 -0800715 error = chown_common(nd.path.dentry, user, group);
Dave Hansen2af482a2008-02-15 14:37:50 -0800716 mnt_drop_write(nd.path.mnt);
717out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -0800718 path_put(&nd.path);
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800719out:
720 return error;
721}
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723asmlinkage 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);
Dave Hansen6902d922006-09-30 23:29:01 -0700729 if (error)
730 goto out;
Dave Hansen2af482a2008-02-15 14:37:50 -0800731 error = mnt_want_write(nd.path.mnt);
732 if (error)
733 goto out_release;
Jan Blunck4ac91372008-02-14 19:34:32 -0800734 error = chown_common(nd.path.dentry, user, group);
Dave Hansen2af482a2008-02-15 14:37:50 -0800735 mnt_drop_write(nd.path.mnt);
736out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -0800737 path_put(&nd.path);
Dave Hansen6902d922006-09-30 23:29:01 -0700738out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 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;
Dave Hansen6902d922006-09-30 23:29:01 -0700747 struct dentry * dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 file = fget(fd);
Dave Hansen6902d922006-09-30 23:29:01 -0700750 if (!file)
751 goto out;
752
Dave Hansen2af482a2008-02-15 14:37:50 -0800753 error = mnt_want_write(file->f_path.mnt);
754 if (error)
755 goto out_fput;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800756 dentry = file->f_path.dentry;
Al Viro5a190ae2007-06-07 12:19:32 -0400757 audit_inode(NULL, dentry);
Dave Hansen6902d922006-09-30 23:29:01 -0700758 error = chown_common(dentry, user, group);
Dave Hansen2af482a2008-02-15 14:37:50 -0800759 mnt_drop_write(file->f_path.mnt);
760out_fput:
Dave Hansen6902d922006-09-30 23:29:01 -0700761 fput(file);
762out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return error;
764}
765
Dave Hansen4a3fd212008-02-15 14:37:48 -0800766/*
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
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700795static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
Trond Myklebust834f2a42005-10-18 14:20:16 -0700796 int flags, struct file *f,
797 int (*open)(struct inode *, struct file *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct inode *inode;
800 int error;
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 f->f_flags = flags;
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700803 f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
804 FMODE_PREAD | FMODE_PWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 inode = dentry->d_inode;
806 if (f->f_mode & FMODE_WRITE) {
Dave Hansen4a3fd212008-02-15 14:37:48 -0800807 error = __get_file_write_access(inode, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (error)
809 goto cleanup_file;
Dave Hansenad775f52008-02-15 14:38:01 -0800810 if (!special_file(inode->i_mode))
811 file_take_write(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
814 f->f_mapping = inode->i_mapping;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800815 f->f_path.dentry = dentry;
816 f->f_path.mnt = mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 f->f_pos = 0;
818 f->f_op = fops_get(inode->i_fop);
819 file_move(f, &inode->i_sb->s_files);
820
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +0900821 error = security_dentry_open(f);
822 if (error)
823 goto cleanup_all;
824
Trond Myklebust834f2a42005-10-18 14:20:16 -0700825 if (!open && f->f_op)
826 open = f->f_op->open;
827 if (open) {
828 error = open(inode, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (error)
830 goto cleanup_all;
831 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 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) {
Carsten Otteceffc072005-06-23 22:05:25 -0700839 if (!f->f_mapping->a_ops ||
840 ((!f->f_mapping->a_ops->direct_IO) &&
Nick Piggin70688e42008-04-28 02:13:02 -0700841 (!f->f_mapping->a_ops->get_xip_mem))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 fput(f);
843 f = ERR_PTR(-EINVAL);
844 }
845 }
846
847 return f;
848
849cleanup_all:
850 fops_put(f->f_op);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800851 if (f->f_mode & FMODE_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 put_write_access(inode);
Dave Hansenad775f52008-02-15 14:38:01 -0800853 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);
Dave Hansen4a3fd212008-02-15 14:37:48 -0800861 mnt_drop_write(mnt);
Dave Hansenad775f52008-02-15 14:38:01 -0800862 }
Dave Hansen4a3fd212008-02-15 14:37:48 -0800863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 file_kill(f);
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800865 f->f_path.dentry = NULL;
866 f->f_path.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867cleanup_file:
868 put_filp(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 dput(dentry);
870 mntput(mnt);
871 return ERR_PTR(error);
872}
873
Trond Myklebust834f2a42005-10-18 14:20:16 -0700874/**
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.
Oleg Drokin9a56c212006-03-25 03:07:02 -0800884 * 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).
Trond Myklebust834f2a42005-10-18 14:20:16 -0700888 * 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;
Jan Blunck4ac91372008-02-14 19:34:32 -0800900 nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
Trond Myklebust834f2a42005-10-18 14:20:16 -0700901 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? */
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800927 if (filp->f_path.dentry == NULL)
Jan Blunck4ac91372008-02-14 19:34:32 -0800928 filp = __dentry_open(nd->path.dentry, nd->path.mnt, flags, filp,
929 NULL);
Trond Myklebust834f2a42005-10-18 14:20:16 -0700930 else
Jan Blunck1d957f92008-02-14 19:34:35 -0800931 path_put(&nd->path);
Trond Myklebust834f2a42005-10-18 14:20:16 -0700932 return filp;
933}
934
Peter Staubach6fdcc212005-11-07 00:59:42 -0800935/*
936 * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
937 * error.
938 */
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700939struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
940{
941 int error;
942 struct file *f;
943
Christoph Hellwig322ee5b2008-02-15 14:37:24 -0800944 /*
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
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700956 error = -ENFILE;
957 f = get_empty_filp();
Peter Staubach6fdcc212005-11-07 00:59:42 -0800958 if (f == NULL) {
959 dput(dentry);
960 mntput(mnt);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700961 return ERR_PTR(error);
Peter Staubach6fdcc212005-11-07 00:59:42 -0800962 }
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700963
Trond Myklebust834f2a42005-10-18 14:20:16 -0700964 return __dentry_open(dentry, mnt, flags, f, NULL);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700965}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966EXPORT_SYMBOL(dentry_open);
967
968/*
969 * Find an empty file descriptor entry, and mark it busy.
970 */
Ulrich Drepper4a195422007-07-15 23:40:34 -0700971int get_unused_fd_flags(int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 struct files_struct * files = current->files;
974 int fd, error;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700975 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 error = -EMFILE;
978 spin_lock(&files->file_lock);
979
980repeat:
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700981 fdt = files_fdtable(files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800982 fd = find_next_zero_bit(fdt->open_fds->fds_bits, fdt->max_fds,
Eric Dumazet0c9e63f2006-03-23 03:00:12 -0800983 files->next_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
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
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001006 FD_SET(fd, fdt->open_fds);
Ulrich Drepperf23513e2007-07-15 23:40:32 -07001007 if (flags & O_CLOEXEC)
1008 FD_SET(fd, fdt->close_on_exec);
1009 else
1010 FD_CLR(fd, fdt->close_on_exec);
Eric Dumazet0c9e63f2006-03-23 03:00:12 -08001011 files->next_fd = fd + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012#if 1
1013 /* Sanity check */
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001014 if (fdt->fd[fd] != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001016 fdt->fd[fd] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018#endif
1019 error = fd;
1020
1021out:
1022 spin_unlock(&files->file_lock);
1023 return error;
1024}
1025
Ulrich Drepperf23513e2007-07-15 23:40:32 -07001026int get_unused_fd(void)
1027{
1028 return get_unused_fd_flags(0);
1029}
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031EXPORT_SYMBOL(get_unused_fd);
1032
Matt Mackallb01ec0e2006-01-08 01:05:20 -08001033static void __put_unused_fd(struct files_struct *files, unsigned int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001035 struct fdtable *fdt = files_fdtable(files);
1036 __FD_CLR(fd, fdt->open_fds);
Eric Dumazet0c9e63f2006-03-23 03:00:12 -08001037 if (fd < files->next_fd)
1038 files->next_fd = fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001041void put_unused_fd(unsigned int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
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/*
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001052 * Install a file pointer in the fd array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 *
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
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001064void fd_install(unsigned int fd, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 struct files_struct *files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001067 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 spin_lock(&files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001069 fdt = files_fdtable(files);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -07001070 BUG_ON(fdt->fd[fd] != NULL);
1071 rcu_assign_pointer(fdt->fd[fd], file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 spin_unlock(&files->file_lock);
1073}
1074
1075EXPORT_SYMBOL(fd_install);
1076
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001077long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Miklos Szeredie922efc2005-09-06 15:18:25 -07001079 char *tmp = getname(filename);
1080 int fd = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (!IS_ERR(tmp)) {
Ulrich Drepperf23513e2007-07-15 23:40:32 -07001083 fd = get_unused_fd_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (fd >= 0) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001085 struct file *f = do_filp_open(dfd, tmp, flags, mode);
Telemaque Ndizihiwefed2fc12005-06-23 00:10:33 -07001086 if (IS_ERR(f)) {
1087 put_unused_fd(fd);
1088 fd = PTR_ERR(f);
1089 } else {
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001090 fsnotify_open(f->f_path.dentry);
Telemaque Ndizihiwefed2fc12005-06-23 00:10:33 -07001091 fd_install(fd, f);
1092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 putname(tmp);
1095 }
1096 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
Miklos Szeredie922efc2005-09-06 15:18:25 -07001098
1099asmlinkage long sys_open(const char __user *filename, int flags, int mode)
1100{
Linus Torvalds385910f2006-04-18 13:22:59 -07001101 long ret;
1102
Miklos Szeredie922efc2005-09-06 15:18:25 -07001103 if (force_o_largefile())
1104 flags |= O_LARGEFILE;
1105
Linus Torvalds385910f2006-04-18 13:22:59 -07001106 ret = do_sys_open(AT_FDCWD, filename, flags, mode);
1107 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -07001108 asmlinkage_protect(3, ret, filename, flags, mode);
Linus Torvalds385910f2006-04-18 13:22:59 -07001109 return ret;
Miklos Szeredie922efc2005-09-06 15:18:25 -07001110}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001112asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
1113 int mode)
1114{
Linus Torvalds385910f2006-04-18 13:22:59 -07001115 long ret;
1116
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001117 if (force_o_largefile())
1118 flags |= O_LARGEFILE;
1119
Linus Torvalds385910f2006-04-18 13:22:59 -07001120 ret = do_sys_open(dfd, filename, flags, mode);
1121 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -07001122 asmlinkage_protect(4, ret, dfd, filename, flags, mode);
Linus Torvalds385910f2006-04-18 13:22:59 -07001123 return ret;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001124}
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126#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{
Christoph Lameter45778ca2005-06-23 00:10:17 -07001145 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 if (!file_count(filp)) {
1148 printk(KERN_ERR "VFS: Close: file count is 0\n");
Christoph Lameter45778ca2005-06-23 00:10:17 -07001149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151
Christoph Lameter45778ca2005-06-23 00:10:17 -07001152 if (filp->f_op && filp->f_op->flush)
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -07001153 retval = filp->f_op->flush(filp, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
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;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001172 struct fdtable *fdt;
Ernie Petridesee731f42006-09-29 02:00:13 -07001173 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 spin_lock(&files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001176 fdt = files_fdtable(files);
1177 if (fd >= fdt->max_fds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 goto out_unlock;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001179 filp = fdt->fd[fd];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (!filp)
1181 goto out_unlock;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -07001182 rcu_assign_pointer(fdt->fd[fd], NULL);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001183 FD_CLR(fd, fdt->close_on_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 __put_unused_fd(files, fd);
1185 spin_unlock(&files->file_lock);
Ernie Petridesee731f42006-09-29 02:00:13 -07001186 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
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)) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001211 /* XXX: this needs locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 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)
Alan Coxa9c62a12007-10-16 23:30:22 -07001227 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 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);