blob: 53ec28c367770d5704ecdb24f7549470cb0ccf6a [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>
9#include <linux/utime.h>
10#include <linux/file.h>
11#include <linux/smp_lock.h>
12#include <linux/quotaops.h>
Robert Love0eeca282005-07-12 17:06:03 -040013#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/tty.h>
17#include <linux/namei.h>
18#include <linux/backing-dev.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080019#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/security.h>
21#include <linux/mount.h>
22#include <linux/vfs.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080023#include <linux/fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/uaccess.h>
25#include <linux/fs.h>
Yoav Zachef3daed2005-06-23 00:09:58 -070026#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/pagemap.h>
28#include <linux/syscalls.h>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070029#include <linux/rcupdate.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000030#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/unistd.h>
33
34int vfs_statfs(struct super_block *sb, struct kstatfs *buf)
35{
36 int retval = -ENODEV;
37
38 if (sb) {
39 retval = -ENOSYS;
40 if (sb->s_op->statfs) {
41 memset(buf, 0, sizeof(*buf));
42 retval = security_sb_statfs(sb);
43 if (retval)
44 return retval;
45 retval = sb->s_op->statfs(sb, buf);
46 if (retval == 0 && buf->f_frsize == 0)
47 buf->f_frsize = buf->f_bsize;
48 }
49 }
50 return retval;
51}
52
53EXPORT_SYMBOL(vfs_statfs);
54
55static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
56{
57 struct kstatfs st;
58 int retval;
59
60 retval = vfs_statfs(sb, &st);
61 if (retval)
62 return retval;
63
64 if (sizeof(*buf) == sizeof(st))
65 memcpy(buf, &st, sizeof(st));
66 else {
67 if (sizeof buf->f_blocks == 4) {
68 if ((st.f_blocks | st.f_bfree | st.f_bavail) &
69 0xffffffff00000000ULL)
70 return -EOVERFLOW;
71 /*
72 * f_files and f_ffree may be -1; it's okay to stuff
73 * that into 32 bits
74 */
75 if (st.f_files != -1 &&
76 (st.f_files & 0xffffffff00000000ULL))
77 return -EOVERFLOW;
78 if (st.f_ffree != -1 &&
79 (st.f_ffree & 0xffffffff00000000ULL))
80 return -EOVERFLOW;
81 }
82
83 buf->f_type = st.f_type;
84 buf->f_bsize = st.f_bsize;
85 buf->f_blocks = st.f_blocks;
86 buf->f_bfree = st.f_bfree;
87 buf->f_bavail = st.f_bavail;
88 buf->f_files = st.f_files;
89 buf->f_ffree = st.f_ffree;
90 buf->f_fsid = st.f_fsid;
91 buf->f_namelen = st.f_namelen;
92 buf->f_frsize = st.f_frsize;
93 memset(buf->f_spare, 0, sizeof(buf->f_spare));
94 }
95 return 0;
96}
97
98static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
99{
100 struct kstatfs st;
101 int retval;
102
103 retval = vfs_statfs(sb, &st);
104 if (retval)
105 return retval;
106
107 if (sizeof(*buf) == sizeof(st))
108 memcpy(buf, &st, sizeof(st));
109 else {
110 buf->f_type = st.f_type;
111 buf->f_bsize = st.f_bsize;
112 buf->f_blocks = st.f_blocks;
113 buf->f_bfree = st.f_bfree;
114 buf->f_bavail = st.f_bavail;
115 buf->f_files = st.f_files;
116 buf->f_ffree = st.f_ffree;
117 buf->f_fsid = st.f_fsid;
118 buf->f_namelen = st.f_namelen;
119 buf->f_frsize = st.f_frsize;
120 memset(buf->f_spare, 0, sizeof(buf->f_spare));
121 }
122 return 0;
123}
124
125asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
126{
127 struct nameidata nd;
128 int error;
129
130 error = user_path_walk(path, &nd);
131 if (!error) {
132 struct statfs tmp;
133 error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
134 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
135 error = -EFAULT;
136 path_release(&nd);
137 }
138 return error;
139}
140
141
142asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
143{
144 struct nameidata nd;
145 long error;
146
147 if (sz != sizeof(*buf))
148 return -EINVAL;
149 error = user_path_walk(path, &nd);
150 if (!error) {
151 struct statfs64 tmp;
152 error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
153 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
154 error = -EFAULT;
155 path_release(&nd);
156 }
157 return error;
158}
159
160
161asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
162{
163 struct file * file;
164 struct statfs tmp;
165 int error;
166
167 error = -EBADF;
168 file = fget(fd);
169 if (!file)
170 goto out;
171 error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
172 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
173 error = -EFAULT;
174 fput(file);
175out:
176 return error;
177}
178
179asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
180{
181 struct file * file;
182 struct statfs64 tmp;
183 int error;
184
185 if (sz != sizeof(*buf))
186 return -EINVAL;
187
188 error = -EBADF;
189 file = fget(fd);
190 if (!file)
191 goto out;
192 error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
193 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
194 error = -EFAULT;
195 fput(file);
196out:
197 return error;
198}
199
NeilBrown4a301312006-01-08 01:02:39 -0800200int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
201 struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int err;
204 struct iattr newattrs;
205
206 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
207 if (length < 0)
208 return -EINVAL;
209
210 newattrs.ia_size = length;
NeilBrown4a301312006-01-08 01:02:39 -0800211 newattrs.ia_valid = ATTR_SIZE | time_attrs;
Miklos Szeredicc4e69d2005-11-07 00:59:49 -0800212 if (filp) {
213 newattrs.ia_file = filp;
214 newattrs.ia_valid |= ATTR_FILE;
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800217 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 err = notify_change(dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800219 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return err;
221}
222
Matt Mackallb01ec0e2006-01-08 01:05:20 -0800223static long do_sys_truncate(const char __user * path, loff_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct nameidata nd;
226 struct inode * inode;
227 int error;
228
229 error = -EINVAL;
230 if (length < 0) /* sorry, but loff_t says... */
231 goto out;
232
233 error = user_path_walk(path, &nd);
234 if (error)
235 goto out;
236 inode = nd.dentry->d_inode;
237
238 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
239 error = -EISDIR;
240 if (S_ISDIR(inode->i_mode))
241 goto dput_and_out;
242
243 error = -EINVAL;
244 if (!S_ISREG(inode->i_mode))
245 goto dput_and_out;
246
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800247 error = vfs_permission(&nd, MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (error)
249 goto dput_and_out;
250
251 error = -EROFS;
252 if (IS_RDONLY(inode))
253 goto dput_and_out;
254
255 error = -EPERM;
256 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
257 goto dput_and_out;
258
259 /*
260 * Make sure that there are no leases.
261 */
262 error = break_lease(inode, FMODE_WRITE);
263 if (error)
264 goto dput_and_out;
265
266 error = get_write_access(inode);
267 if (error)
268 goto dput_and_out;
269
270 error = locks_verify_truncate(inode, NULL, length);
271 if (!error) {
272 DQUOT_INIT(inode);
NeilBrown4a301312006-01-08 01:02:39 -0800273 error = do_truncate(nd.dentry, length, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275 put_write_access(inode);
276
277dput_and_out:
278 path_release(&nd);
279out:
280 return error;
281}
282
283asmlinkage long sys_truncate(const char __user * path, unsigned long length)
284{
285 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
286 return do_sys_truncate(path, (long)length);
287}
288
Matt Mackallb01ec0e2006-01-08 01:05:20 -0800289static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 struct inode * inode;
292 struct dentry *dentry;
293 struct file * file;
294 int error;
295
296 error = -EINVAL;
297 if (length < 0)
298 goto out;
299 error = -EBADF;
300 file = fget(fd);
301 if (!file)
302 goto out;
303
304 /* explicitly opened as large or we are on 64-bit box */
305 if (file->f_flags & O_LARGEFILE)
306 small = 0;
307
308 dentry = file->f_dentry;
309 inode = dentry->d_inode;
310 error = -EINVAL;
311 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
312 goto out_putf;
313
314 error = -EINVAL;
315 /* Cannot ftruncate over 2^31 bytes without large file support */
316 if (small && length > MAX_NON_LFS)
317 goto out_putf;
318
319 error = -EPERM;
320 if (IS_APPEND(inode))
321 goto out_putf;
322
323 error = locks_verify_truncate(inode, file, length);
324 if (!error)
NeilBrown4a301312006-01-08 01:02:39 -0800325 error = do_truncate(dentry, length, 0, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326out_putf:
327 fput(file);
328out:
329 return error;
330}
331
332asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
333{
Linus Torvalds0a489cb2006-04-18 13:02:48 -0700334 long ret = do_sys_ftruncate(fd, length, 1);
Linus Torvalds385910f2006-04-18 13:22:59 -0700335 /* avoid REGPARM breakage on x86: */
Linus Torvalds0a489cb2006-04-18 13:02:48 -0700336 prevent_tail_call(ret);
337 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340/* LFS versions of truncate are only needed on 32 bit machines */
341#if BITS_PER_LONG == 32
342asmlinkage long sys_truncate64(const char __user * path, loff_t length)
343{
344 return do_sys_truncate(path, length);
345}
346
347asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
348{
Linus Torvalds0a489cb2006-04-18 13:02:48 -0700349 long ret = do_sys_ftruncate(fd, length, 0);
Linus Torvalds385910f2006-04-18 13:22:59 -0700350 /* avoid REGPARM breakage on x86: */
Linus Torvalds0a489cb2006-04-18 13:02:48 -0700351 prevent_tail_call(ret);
352 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354#endif
355
356#ifdef __ARCH_WANT_SYS_UTIME
357
358/*
359 * sys_utime() can be implemented in user-level using sys_utimes().
360 * Is this for backwards compatibility? If so, why not move it
361 * into the appropriate arch directory (for those architectures that
362 * need it).
363 */
364
365/* If times==NULL, set access and modification to current time,
366 * must be owner or have write permission.
367 * Else, update from *times, must be owner or super user.
368 */
369asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
370{
371 int error;
372 struct nameidata nd;
373 struct inode * inode;
374 struct iattr newattrs;
375
376 error = user_path_walk(filename, &nd);
377 if (error)
378 goto out;
379 inode = nd.dentry->d_inode;
380
381 error = -EROFS;
382 if (IS_RDONLY(inode))
383 goto dput_and_out;
384
385 /* Don't worry, the checks are done in inode_change_ok() */
386 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
387 if (times) {
388 error = -EPERM;
389 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
390 goto dput_and_out;
391
392 error = get_user(newattrs.ia_atime.tv_sec, &times->actime);
393 newattrs.ia_atime.tv_nsec = 0;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800394 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 error = get_user(newattrs.ia_mtime.tv_sec, &times->modtime);
396 newattrs.ia_mtime.tv_nsec = 0;
397 if (error)
398 goto dput_and_out;
399
400 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
401 } else {
402 error = -EACCES;
403 if (IS_IMMUTABLE(inode))
404 goto dput_and_out;
405
406 if (current->fsuid != inode->i_uid &&
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800407 (error = vfs_permission(&nd, MAY_WRITE)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 goto dput_and_out;
409 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800410 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 error = notify_change(nd.dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800412 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413dput_and_out:
414 path_release(&nd);
415out:
416 return error;
417}
418
419#endif
420
421/* If times==NULL, set access and modification to current time,
422 * must be owner or have write permission.
423 * Else, update from *times, must be owner or super user.
424 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800425long do_utimes(int dfd, char __user *filename, struct timeval *times)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 int error;
428 struct nameidata nd;
429 struct inode * inode;
430 struct iattr newattrs;
431
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800432 error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 if (error)
435 goto out;
436 inode = nd.dentry->d_inode;
437
438 error = -EROFS;
439 if (IS_RDONLY(inode))
440 goto dput_and_out;
441
442 /* Don't worry, the checks are done in inode_change_ok() */
443 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
444 if (times) {
445 error = -EPERM;
446 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
447 goto dput_and_out;
448
449 newattrs.ia_atime.tv_sec = times[0].tv_sec;
450 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
451 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
452 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
453 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
454 } else {
455 error = -EACCES;
456 if (IS_IMMUTABLE(inode))
457 goto dput_and_out;
458
459 if (current->fsuid != inode->i_uid &&
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800460 (error = vfs_permission(&nd, MAY_WRITE)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 goto dput_and_out;
462 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800463 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 error = notify_change(nd.dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800465 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466dput_and_out:
467 path_release(&nd);
468out:
469 return error;
470}
471
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800472asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 struct timeval times[2];
475
476 if (utimes && copy_from_user(&times, utimes, sizeof(times)))
477 return -EFAULT;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800478 return do_utimes(dfd, filename, utimes ? times : NULL);
479}
480
481asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
482{
483 return sys_futimesat(AT_FDCWD, filename, utimes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486
487/*
488 * access() needs to use the real uid/gid, not the effective uid/gid.
489 * We do this by temporarily clearing all FS-related capabilities and
490 * switching the fsuid/fsgid around to the real ones.
491 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800492asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 struct nameidata nd;
495 int old_fsuid, old_fsgid;
496 kernel_cap_t old_cap;
497 int res;
498
499 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
500 return -EINVAL;
501
502 old_fsuid = current->fsuid;
503 old_fsgid = current->fsgid;
504 old_cap = current->cap_effective;
505
506 current->fsuid = current->uid;
507 current->fsgid = current->gid;
508
509 /*
510 * Clear the capabilities if we switch to a non-root user
511 *
512 * FIXME: There is a race here against sys_capset. The
513 * capabilities can change yet we will restore the old
514 * value below. We should hold task_capabilities_lock,
515 * but we cannot because user_path_walk can sleep.
516 */
517 if (current->uid)
518 cap_clear(current->cap_effective);
519 else
520 current->cap_effective = current->cap_permitted;
521
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800522 res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (!res) {
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800524 res = vfs_permission(&nd, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* SuS v2 requires we report a read only fs too */
526 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
527 && !special_file(nd.dentry->d_inode->i_mode))
528 res = -EROFS;
529 path_release(&nd);
530 }
531
532 current->fsuid = old_fsuid;
533 current->fsgid = old_fsgid;
534 current->cap_effective = old_cap;
535
536 return res;
537}
538
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800539asmlinkage long sys_access(const char __user *filename, int mode)
540{
541 return sys_faccessat(AT_FDCWD, filename, mode);
542}
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544asmlinkage long sys_chdir(const char __user * filename)
545{
546 struct nameidata nd;
547 int error;
548
549 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
550 if (error)
551 goto out;
552
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800553 error = vfs_permission(&nd, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (error)
555 goto dput_and_out;
556
557 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
558
559dput_and_out:
560 path_release(&nd);
561out:
562 return error;
563}
564
565asmlinkage long sys_fchdir(unsigned int fd)
566{
567 struct file *file;
568 struct dentry *dentry;
569 struct inode *inode;
570 struct vfsmount *mnt;
571 int error;
572
573 error = -EBADF;
574 file = fget(fd);
575 if (!file)
576 goto out;
577
578 dentry = file->f_dentry;
579 mnt = file->f_vfsmnt;
580 inode = dentry->d_inode;
581
582 error = -ENOTDIR;
583 if (!S_ISDIR(inode->i_mode))
584 goto out_putf;
585
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800586 error = file_permission(file, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (!error)
588 set_fs_pwd(current->fs, mnt, dentry);
589out_putf:
590 fput(file);
591out:
592 return error;
593}
594
595asmlinkage long sys_chroot(const char __user * filename)
596{
597 struct nameidata nd;
598 int error;
599
600 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
601 if (error)
602 goto out;
603
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800604 error = vfs_permission(&nd, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (error)
606 goto dput_and_out;
607
608 error = -EPERM;
609 if (!capable(CAP_SYS_CHROOT))
610 goto dput_and_out;
611
612 set_fs_root(current->fs, nd.mnt, nd.dentry);
613 set_fs_altroot();
614 error = 0;
615dput_and_out:
616 path_release(&nd);
617out:
618 return error;
619}
620
621asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
622{
623 struct inode * inode;
624 struct dentry * dentry;
625 struct file * file;
626 int err = -EBADF;
627 struct iattr newattrs;
628
629 file = fget(fd);
630 if (!file)
631 goto out;
632
633 dentry = file->f_dentry;
634 inode = dentry->d_inode;
635
Amy Griffis73241cc2005-11-03 16:00:25 +0000636 audit_inode(NULL, inode, 0);
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 err = -EROFS;
639 if (IS_RDONLY(inode))
640 goto out_putf;
641 err = -EPERM;
642 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
643 goto out_putf;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800644 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (mode == (mode_t) -1)
646 mode = inode->i_mode;
647 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
648 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
649 err = notify_change(dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800650 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652out_putf:
653 fput(file);
654out:
655 return err;
656}
657
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800658asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
659 mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct nameidata nd;
662 struct inode * inode;
663 int error;
664 struct iattr newattrs;
665
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800666 error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (error)
668 goto out;
669 inode = nd.dentry->d_inode;
670
671 error = -EROFS;
672 if (IS_RDONLY(inode))
673 goto dput_and_out;
674
675 error = -EPERM;
676 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
677 goto dput_and_out;
678
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800679 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (mode == (mode_t) -1)
681 mode = inode->i_mode;
682 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
683 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
684 error = notify_change(nd.dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800685 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687dput_and_out:
688 path_release(&nd);
689out:
690 return error;
691}
692
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800693asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
694{
695 return sys_fchmodat(AT_FDCWD, filename, mode);
696}
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
699{
700 struct inode * inode;
701 int error;
702 struct iattr newattrs;
703
704 error = -ENOENT;
705 if (!(inode = dentry->d_inode)) {
706 printk(KERN_ERR "chown_common: NULL inode\n");
707 goto out;
708 }
709 error = -EROFS;
710 if (IS_RDONLY(inode))
711 goto out;
712 error = -EPERM;
713 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
714 goto out;
715 newattrs.ia_valid = ATTR_CTIME;
716 if (user != (uid_t) -1) {
717 newattrs.ia_valid |= ATTR_UID;
718 newattrs.ia_uid = user;
719 }
720 if (group != (gid_t) -1) {
721 newattrs.ia_valid |= ATTR_GID;
722 newattrs.ia_gid = group;
723 }
724 if (!S_ISDIR(inode->i_mode))
725 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800726 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 error = notify_change(dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800728 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729out:
730 return error;
731}
732
733asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
734{
735 struct nameidata nd;
736 int error;
737
738 error = user_path_walk(filename, &nd);
739 if (!error) {
740 error = chown_common(nd.dentry, user, group);
741 path_release(&nd);
742 }
743 return error;
744}
745
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800746asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
747 gid_t group, int flag)
748{
749 struct nameidata nd;
750 int error = -EINVAL;
751 int follow;
752
753 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
754 goto out;
755
756 follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
757 error = __user_walk_fd(dfd, filename, follow, &nd);
758 if (!error) {
759 error = chown_common(nd.dentry, user, group);
760 path_release(&nd);
761 }
762out:
763 return error;
764}
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
767{
768 struct nameidata nd;
769 int error;
770
771 error = user_path_walk_link(filename, &nd);
772 if (!error) {
773 error = chown_common(nd.dentry, user, group);
774 path_release(&nd);
775 }
776 return error;
777}
778
779
780asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
781{
782 struct file * file;
783 int error = -EBADF;
784
785 file = fget(fd);
786 if (file) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000787 struct dentry * dentry;
788 dentry = file->f_dentry;
789 audit_inode(NULL, dentry->d_inode, 0);
790 error = chown_common(dentry, user, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 fput(file);
792 }
793 return error;
794}
795
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700796static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
Trond Myklebust834f2a42005-10-18 14:20:16 -0700797 int flags, struct file *f,
798 int (*open)(struct inode *, struct file *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 struct inode *inode;
801 int error;
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 f->f_flags = flags;
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700804 f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
805 FMODE_PREAD | FMODE_PWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 inode = dentry->d_inode;
807 if (f->f_mode & FMODE_WRITE) {
808 error = get_write_access(inode);
809 if (error)
810 goto cleanup_file;
811 }
812
813 f->f_mapping = inode->i_mapping;
814 f->f_dentry = dentry;
815 f->f_vfsmnt = mnt;
816 f->f_pos = 0;
817 f->f_op = fops_get(inode->i_fop);
818 file_move(f, &inode->i_sb->s_files);
819
Trond Myklebust834f2a42005-10-18 14:20:16 -0700820 if (!open && f->f_op)
821 open = f->f_op->open;
822 if (open) {
823 error = open(inode, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (error)
825 goto cleanup_all;
826 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
829
830 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
831
832 /* NB: we're sure to have correct a_ops only after f_op->open */
833 if (f->f_flags & O_DIRECT) {
Carsten Otteceffc072005-06-23 22:05:25 -0700834 if (!f->f_mapping->a_ops ||
835 ((!f->f_mapping->a_ops->direct_IO) &&
836 (!f->f_mapping->a_ops->get_xip_page))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 fput(f);
838 f = ERR_PTR(-EINVAL);
839 }
840 }
841
842 return f;
843
844cleanup_all:
845 fops_put(f->f_op);
846 if (f->f_mode & FMODE_WRITE)
847 put_write_access(inode);
848 file_kill(f);
849 f->f_dentry = NULL;
850 f->f_vfsmnt = NULL;
851cleanup_file:
852 put_filp(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 dput(dentry);
854 mntput(mnt);
855 return ERR_PTR(error);
856}
857
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700858/*
859 * Note that while the flag value (low two bits) for sys_open means:
860 * 00 - read-only
861 * 01 - write-only
862 * 10 - read-write
863 * 11 - special
864 * it is changed into
865 * 00 - no permissions needed
866 * 01 - read-permission
867 * 10 - write-permission
868 * 11 - read-write
869 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
870 * used by symlinks.
871 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800872static struct file *do_filp_open(int dfd, const char *filename, int flags,
873 int mode)
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700874{
875 int namei_flags, error;
876 struct nameidata nd;
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700877
878 namei_flags = flags;
879 if ((namei_flags+1) & O_ACCMODE)
880 namei_flags++;
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700881
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800882 error = open_namei(dfd, filename, namei_flags, mode, &nd);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700883 if (!error)
Trond Myklebust834f2a42005-10-18 14:20:16 -0700884 return nameidata_to_filp(&nd, flags);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700885
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700886 return ERR_PTR(error);
887}
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800888
889struct file *filp_open(const char *filename, int flags, int mode)
890{
891 return do_filp_open(AT_FDCWD, filename, flags, mode);
892}
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700893EXPORT_SYMBOL(filp_open);
894
Trond Myklebust834f2a42005-10-18 14:20:16 -0700895/**
896 * lookup_instantiate_filp - instantiates the open intent filp
897 * @nd: pointer to nameidata
898 * @dentry: pointer to dentry
899 * @open: open callback
900 *
901 * Helper for filesystems that want to use lookup open intents and pass back
902 * a fully instantiated struct file to the caller.
903 * This function is meant to be called from within a filesystem's
904 * lookup method.
Oleg Drokin9a56c212006-03-25 03:07:02 -0800905 * Beware of calling it for non-regular files! Those ->open methods might block
906 * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
907 * leading to a deadlock, as nobody can open that fifo anymore, because
908 * another process to open fifo will block on locked parent when doing lookup).
Trond Myklebust834f2a42005-10-18 14:20:16 -0700909 * Note that in case of error, nd->intent.open.file is destroyed, but the
910 * path information remains valid.
911 * If the open callback is set to NULL, then the standard f_op->open()
912 * filesystem callback is substituted.
913 */
914struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
915 int (*open)(struct inode *, struct file *))
916{
917 if (IS_ERR(nd->intent.open.file))
918 goto out;
919 if (IS_ERR(dentry))
920 goto out_err;
921 nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->mnt),
922 nd->intent.open.flags - 1,
923 nd->intent.open.file,
924 open);
925out:
926 return nd->intent.open.file;
927out_err:
928 release_open_intent(nd);
929 nd->intent.open.file = (struct file *)dentry;
930 goto out;
931}
932EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
933
934/**
935 * nameidata_to_filp - convert a nameidata to an open filp.
936 * @nd: pointer to nameidata
937 * @flags: open flags
938 *
939 * Note that this function destroys the original nameidata
940 */
941struct file *nameidata_to_filp(struct nameidata *nd, int flags)
942{
943 struct file *filp;
944
945 /* Pick up the filp from the open intent */
946 filp = nd->intent.open.file;
947 /* Has the filesystem initialised the file for us? */
948 if (filp->f_dentry == NULL)
949 filp = __dentry_open(nd->dentry, nd->mnt, flags, filp, NULL);
950 else
951 path_release(nd);
952 return filp;
953}
954
Peter Staubach6fdcc212005-11-07 00:59:42 -0800955/*
956 * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
957 * error.
958 */
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700959struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
960{
961 int error;
962 struct file *f;
963
964 error = -ENFILE;
965 f = get_empty_filp();
Peter Staubach6fdcc212005-11-07 00:59:42 -0800966 if (f == NULL) {
967 dput(dentry);
968 mntput(mnt);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700969 return ERR_PTR(error);
Peter Staubach6fdcc212005-11-07 00:59:42 -0800970 }
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700971
Trond Myklebust834f2a42005-10-18 14:20:16 -0700972 return __dentry_open(dentry, mnt, flags, f, NULL);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700973}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974EXPORT_SYMBOL(dentry_open);
975
976/*
977 * Find an empty file descriptor entry, and mark it busy.
978 */
979int get_unused_fd(void)
980{
981 struct files_struct * files = current->files;
982 int fd, error;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700983 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 error = -EMFILE;
986 spin_lock(&files->file_lock);
987
988repeat:
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700989 fdt = files_fdtable(files);
990 fd = find_next_zero_bit(fdt->open_fds->fds_bits,
991 fdt->max_fdset,
Eric Dumazet0c9e63f2006-03-23 03:00:12 -0800992 files->next_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 /*
995 * N.B. For clone tasks sharing a files structure, this test
996 * will limit the total number of files that can be opened.
997 */
998 if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
999 goto out;
1000
1001 /* Do we need to expand the fd array or fd set? */
1002 error = expand_files(files, fd);
1003 if (error < 0)
1004 goto out;
1005
1006 if (error) {
1007 /*
1008 * If we needed to expand the fs array we
1009 * might have blocked - try again.
1010 */
1011 error = -EMFILE;
1012 goto repeat;
1013 }
1014
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001015 FD_SET(fd, fdt->open_fds);
1016 FD_CLR(fd, fdt->close_on_exec);
Eric Dumazet0c9e63f2006-03-23 03:00:12 -08001017 files->next_fd = fd + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018#if 1
1019 /* Sanity check */
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001020 if (fdt->fd[fd] != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001022 fdt->fd[fd] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024#endif
1025 error = fd;
1026
1027out:
1028 spin_unlock(&files->file_lock);
1029 return error;
1030}
1031
1032EXPORT_SYMBOL(get_unused_fd);
1033
Matt Mackallb01ec0e2006-01-08 01:05:20 -08001034static void __put_unused_fd(struct files_struct *files, unsigned int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001036 struct fdtable *fdt = files_fdtable(files);
1037 __FD_CLR(fd, fdt->open_fds);
Eric Dumazet0c9e63f2006-03-23 03:00:12 -08001038 if (fd < files->next_fd)
1039 files->next_fd = fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042void fastcall put_unused_fd(unsigned int fd)
1043{
1044 struct files_struct *files = current->files;
1045 spin_lock(&files->file_lock);
1046 __put_unused_fd(files, fd);
1047 spin_unlock(&files->file_lock);
1048}
1049
1050EXPORT_SYMBOL(put_unused_fd);
1051
1052/*
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001053 * Install a file pointer in the fd array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 *
1055 * The VFS is full of places where we drop the files lock between
1056 * setting the open_fds bitmap and installing the file in the file
1057 * array. At any such point, we are vulnerable to a dup2() race
1058 * installing a file in the array before us. We need to detect this and
1059 * fput() the struct file we are about to overwrite in this case.
1060 *
1061 * It should never happen - if we allow dup2() do it, _really_ bad things
1062 * will follow.
1063 */
1064
1065void fastcall fd_install(unsigned int fd, struct file * file)
1066{
1067 struct files_struct *files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001068 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 spin_lock(&files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001070 fdt = files_fdtable(files);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -07001071 BUG_ON(fdt->fd[fd] != NULL);
1072 rcu_assign_pointer(fdt->fd[fd], file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 spin_unlock(&files->file_lock);
1074}
1075
1076EXPORT_SYMBOL(fd_install);
1077
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001078long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Miklos Szeredie922efc2005-09-06 15:18:25 -07001080 char *tmp = getname(filename);
1081 int fd = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (!IS_ERR(tmp)) {
1084 fd = get_unused_fd();
1085 if (fd >= 0) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001086 struct file *f = do_filp_open(dfd, tmp, flags, mode);
Telemaque Ndizihiwefed2fc12005-06-23 00:10:33 -07001087 if (IS_ERR(f)) {
1088 put_unused_fd(fd);
1089 fd = PTR_ERR(f);
1090 } else {
Robert Love0eeca282005-07-12 17:06:03 -04001091 fsnotify_open(f->f_dentry);
Telemaque Ndizihiwefed2fc12005-06-23 00:10:33 -07001092 fd_install(fd, f);
1093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 putname(tmp);
1096 }
1097 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
Miklos Szeredie922efc2005-09-06 15:18:25 -07001099
1100asmlinkage long sys_open(const char __user *filename, int flags, int mode)
1101{
Linus Torvalds385910f2006-04-18 13:22:59 -07001102 long ret;
1103
Miklos Szeredie922efc2005-09-06 15:18:25 -07001104 if (force_o_largefile())
1105 flags |= O_LARGEFILE;
1106
Linus Torvalds385910f2006-04-18 13:22:59 -07001107 ret = do_sys_open(AT_FDCWD, filename, flags, mode);
1108 /* avoid REGPARM breakage on x86: */
1109 prevent_tail_call(ret);
1110 return ret;
Miklos Szeredie922efc2005-09-06 15:18:25 -07001111}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112EXPORT_SYMBOL_GPL(sys_open);
1113
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001114asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
1115 int mode)
1116{
Linus Torvalds385910f2006-04-18 13:22:59 -07001117 long ret;
1118
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001119 if (force_o_largefile())
1120 flags |= O_LARGEFILE;
1121
Linus Torvalds385910f2006-04-18 13:22:59 -07001122 ret = do_sys_open(dfd, filename, flags, mode);
1123 /* avoid REGPARM breakage on x86: */
1124 prevent_tail_call(ret);
1125 return ret;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001126}
1127EXPORT_SYMBOL_GPL(sys_openat);
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129#ifndef __alpha__
1130
1131/*
1132 * For backward compatibility? Maybe this should be moved
1133 * into arch/i386 instead?
1134 */
1135asmlinkage long sys_creat(const char __user * pathname, int mode)
1136{
1137 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1138}
1139
1140#endif
1141
1142/*
1143 * "id" is the POSIX thread ID. We use the
1144 * files pointer for this..
1145 */
1146int filp_close(struct file *filp, fl_owner_t id)
1147{
Christoph Lameter45778ca2005-06-23 00:10:17 -07001148 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 if (!file_count(filp)) {
1151 printk(KERN_ERR "VFS: Close: file count is 0\n");
Christoph Lameter45778ca2005-06-23 00:10:17 -07001152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154
Christoph Lameter45778ca2005-06-23 00:10:17 -07001155 if (filp->f_op && filp->f_op->flush)
1156 retval = filp->f_op->flush(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 dnotify_flush(filp, id);
1159 locks_remove_posix(filp, id);
1160 fput(filp);
1161 return retval;
1162}
1163
1164EXPORT_SYMBOL(filp_close);
1165
1166/*
1167 * Careful here! We test whether the file pointer is NULL before
1168 * releasing the fd. This ensures that one clone task can't release
1169 * an fd while another clone is opening it.
1170 */
1171asmlinkage long sys_close(unsigned int fd)
1172{
1173 struct file * filp;
1174 struct files_struct *files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001175 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 spin_lock(&files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001178 fdt = files_fdtable(files);
1179 if (fd >= fdt->max_fds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 goto out_unlock;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001181 filp = fdt->fd[fd];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (!filp)
1183 goto out_unlock;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -07001184 rcu_assign_pointer(fdt->fd[fd], NULL);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001185 FD_CLR(fd, fdt->close_on_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 __put_unused_fd(files, fd);
1187 spin_unlock(&files->file_lock);
1188 return filp_close(filp, files);
1189
1190out_unlock:
1191 spin_unlock(&files->file_lock);
1192 return -EBADF;
1193}
1194
1195EXPORT_SYMBOL(sys_close);
1196
1197/*
1198 * This routine simulates a hangup on the tty, to arrange that users
1199 * are given clean terminals at login time.
1200 */
1201asmlinkage long sys_vhangup(void)
1202{
1203 if (capable(CAP_SYS_TTY_CONFIG)) {
1204 tty_vhangup(current->signal->tty);
1205 return 0;
1206 }
1207 return -EPERM;
1208}
1209
1210/*
1211 * Called when an inode is about to be open.
1212 * We use this to disallow opening large files on 32bit systems if
1213 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1214 * on this flag in sys_open.
1215 */
1216int generic_file_open(struct inode * inode, struct file * filp)
1217{
1218 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1219 return -EFBIG;
1220 return 0;
1221}
1222
1223EXPORT_SYMBOL(generic_file_open);
1224
1225/*
1226 * This is used by subsystems that don't want seekable
1227 * file descriptors
1228 */
1229int nonseekable_open(struct inode *inode, struct file *filp)
1230{
1231 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1232 return 0;
1233}
1234
1235EXPORT_SYMBOL(nonseekable_open);