blob: c32c89d6d8dbed71f831e94f8aa0c67eade8cfc1 [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{
334 return do_sys_ftruncate(fd, length, 1);
335}
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{
346 return do_sys_ftruncate(fd, length, 0);
347}
348#endif
349
350#ifdef __ARCH_WANT_SYS_UTIME
351
352/*
353 * sys_utime() can be implemented in user-level using sys_utimes().
354 * Is this for backwards compatibility? If so, why not move it
355 * into the appropriate arch directory (for those architectures that
356 * need it).
357 */
358
359/* If times==NULL, set access and modification to current time,
360 * must be owner or have write permission.
361 * Else, update from *times, must be owner or super user.
362 */
363asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
364{
365 int error;
366 struct nameidata nd;
367 struct inode * inode;
368 struct iattr newattrs;
369
370 error = user_path_walk(filename, &nd);
371 if (error)
372 goto out;
373 inode = nd.dentry->d_inode;
374
375 error = -EROFS;
376 if (IS_RDONLY(inode))
377 goto dput_and_out;
378
379 /* Don't worry, the checks are done in inode_change_ok() */
380 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
381 if (times) {
382 error = -EPERM;
383 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
384 goto dput_and_out;
385
386 error = get_user(newattrs.ia_atime.tv_sec, &times->actime);
387 newattrs.ia_atime.tv_nsec = 0;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800388 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 error = get_user(newattrs.ia_mtime.tv_sec, &times->modtime);
390 newattrs.ia_mtime.tv_nsec = 0;
391 if (error)
392 goto dput_and_out;
393
394 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
395 } else {
396 error = -EACCES;
397 if (IS_IMMUTABLE(inode))
398 goto dput_and_out;
399
400 if (current->fsuid != inode->i_uid &&
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800401 (error = vfs_permission(&nd, MAY_WRITE)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 goto dput_and_out;
403 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800404 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 error = notify_change(nd.dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800406 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407dput_and_out:
408 path_release(&nd);
409out:
410 return error;
411}
412
413#endif
414
415/* If times==NULL, set access and modification to current time,
416 * must be owner or have write permission.
417 * Else, update from *times, must be owner or super user.
418 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800419long do_utimes(int dfd, char __user *filename, struct timeval *times)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 int error;
422 struct nameidata nd;
423 struct inode * inode;
424 struct iattr newattrs;
425
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800426 error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 if (error)
429 goto out;
430 inode = nd.dentry->d_inode;
431
432 error = -EROFS;
433 if (IS_RDONLY(inode))
434 goto dput_and_out;
435
436 /* Don't worry, the checks are done in inode_change_ok() */
437 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
438 if (times) {
439 error = -EPERM;
440 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
441 goto dput_and_out;
442
443 newattrs.ia_atime.tv_sec = times[0].tv_sec;
444 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
445 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
446 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
447 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
448 } else {
449 error = -EACCES;
450 if (IS_IMMUTABLE(inode))
451 goto dput_and_out;
452
453 if (current->fsuid != inode->i_uid &&
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800454 (error = vfs_permission(&nd, MAY_WRITE)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 goto dput_and_out;
456 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800457 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 error = notify_change(nd.dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800459 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460dput_and_out:
461 path_release(&nd);
462out:
463 return error;
464}
465
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800466asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 struct timeval times[2];
469
470 if (utimes && copy_from_user(&times, utimes, sizeof(times)))
471 return -EFAULT;
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800472 return do_utimes(dfd, filename, utimes ? times : NULL);
473}
474
475asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
476{
477 return sys_futimesat(AT_FDCWD, filename, utimes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
480
481/*
482 * access() needs to use the real uid/gid, not the effective uid/gid.
483 * We do this by temporarily clearing all FS-related capabilities and
484 * switching the fsuid/fsgid around to the real ones.
485 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800486asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 struct nameidata nd;
489 int old_fsuid, old_fsgid;
490 kernel_cap_t old_cap;
491 int res;
492
493 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
494 return -EINVAL;
495
496 old_fsuid = current->fsuid;
497 old_fsgid = current->fsgid;
498 old_cap = current->cap_effective;
499
500 current->fsuid = current->uid;
501 current->fsgid = current->gid;
502
503 /*
504 * Clear the capabilities if we switch to a non-root user
505 *
506 * FIXME: There is a race here against sys_capset. The
507 * capabilities can change yet we will restore the old
508 * value below. We should hold task_capabilities_lock,
509 * but we cannot because user_path_walk can sleep.
510 */
511 if (current->uid)
512 cap_clear(current->cap_effective);
513 else
514 current->cap_effective = current->cap_permitted;
515
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800516 res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (!res) {
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800518 res = vfs_permission(&nd, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /* SuS v2 requires we report a read only fs too */
520 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
521 && !special_file(nd.dentry->d_inode->i_mode))
522 res = -EROFS;
523 path_release(&nd);
524 }
525
526 current->fsuid = old_fsuid;
527 current->fsgid = old_fsgid;
528 current->cap_effective = old_cap;
529
530 return res;
531}
532
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800533asmlinkage long sys_access(const char __user *filename, int mode)
534{
535 return sys_faccessat(AT_FDCWD, filename, mode);
536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538asmlinkage long sys_chdir(const char __user * filename)
539{
540 struct nameidata nd;
541 int error;
542
543 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
544 if (error)
545 goto out;
546
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800547 error = vfs_permission(&nd, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (error)
549 goto dput_and_out;
550
551 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
552
553dput_and_out:
554 path_release(&nd);
555out:
556 return error;
557}
558
559asmlinkage long sys_fchdir(unsigned int fd)
560{
561 struct file *file;
562 struct dentry *dentry;
563 struct inode *inode;
564 struct vfsmount *mnt;
565 int error;
566
567 error = -EBADF;
568 file = fget(fd);
569 if (!file)
570 goto out;
571
572 dentry = file->f_dentry;
573 mnt = file->f_vfsmnt;
574 inode = dentry->d_inode;
575
576 error = -ENOTDIR;
577 if (!S_ISDIR(inode->i_mode))
578 goto out_putf;
579
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800580 error = file_permission(file, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (!error)
582 set_fs_pwd(current->fs, mnt, dentry);
583out_putf:
584 fput(file);
585out:
586 return error;
587}
588
589asmlinkage long sys_chroot(const char __user * filename)
590{
591 struct nameidata nd;
592 int error;
593
594 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
595 if (error)
596 goto out;
597
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800598 error = vfs_permission(&nd, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (error)
600 goto dput_and_out;
601
602 error = -EPERM;
603 if (!capable(CAP_SYS_CHROOT))
604 goto dput_and_out;
605
606 set_fs_root(current->fs, nd.mnt, nd.dentry);
607 set_fs_altroot();
608 error = 0;
609dput_and_out:
610 path_release(&nd);
611out:
612 return error;
613}
614
615asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
616{
617 struct inode * inode;
618 struct dentry * dentry;
619 struct file * file;
620 int err = -EBADF;
621 struct iattr newattrs;
622
623 file = fget(fd);
624 if (!file)
625 goto out;
626
627 dentry = file->f_dentry;
628 inode = dentry->d_inode;
629
Amy Griffis73241cc2005-11-03 16:00:25 +0000630 audit_inode(NULL, inode, 0);
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 err = -EROFS;
633 if (IS_RDONLY(inode))
634 goto out_putf;
635 err = -EPERM;
636 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
637 goto out_putf;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800638 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (mode == (mode_t) -1)
640 mode = inode->i_mode;
641 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
642 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
643 err = notify_change(dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800644 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646out_putf:
647 fput(file);
648out:
649 return err;
650}
651
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800652asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
653 mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 struct nameidata nd;
656 struct inode * inode;
657 int error;
658 struct iattr newattrs;
659
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800660 error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (error)
662 goto out;
663 inode = nd.dentry->d_inode;
664
665 error = -EROFS;
666 if (IS_RDONLY(inode))
667 goto dput_and_out;
668
669 error = -EPERM;
670 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
671 goto dput_and_out;
672
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800673 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (mode == (mode_t) -1)
675 mode = inode->i_mode;
676 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
677 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
678 error = notify_change(nd.dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800679 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681dput_and_out:
682 path_release(&nd);
683out:
684 return error;
685}
686
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800687asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
688{
689 return sys_fchmodat(AT_FDCWD, filename, mode);
690}
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
693{
694 struct inode * inode;
695 int error;
696 struct iattr newattrs;
697
698 error = -ENOENT;
699 if (!(inode = dentry->d_inode)) {
700 printk(KERN_ERR "chown_common: NULL inode\n");
701 goto out;
702 }
703 error = -EROFS;
704 if (IS_RDONLY(inode))
705 goto out;
706 error = -EPERM;
707 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
708 goto out;
709 newattrs.ia_valid = ATTR_CTIME;
710 if (user != (uid_t) -1) {
711 newattrs.ia_valid |= ATTR_UID;
712 newattrs.ia_uid = user;
713 }
714 if (group != (gid_t) -1) {
715 newattrs.ia_valid |= ATTR_GID;
716 newattrs.ia_gid = group;
717 }
718 if (!S_ISDIR(inode->i_mode))
719 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800720 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 error = notify_change(dentry, &newattrs);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800722 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723out:
724 return error;
725}
726
727asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
728{
729 struct nameidata nd;
730 int error;
731
732 error = user_path_walk(filename, &nd);
733 if (!error) {
734 error = chown_common(nd.dentry, user, group);
735 path_release(&nd);
736 }
737 return error;
738}
739
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800740asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
741 gid_t group, int flag)
742{
743 struct nameidata nd;
744 int error = -EINVAL;
745 int follow;
746
747 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
748 goto out;
749
750 follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
751 error = __user_walk_fd(dfd, filename, follow, &nd);
752 if (!error) {
753 error = chown_common(nd.dentry, user, group);
754 path_release(&nd);
755 }
756out:
757 return error;
758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
761{
762 struct nameidata nd;
763 int error;
764
765 error = user_path_walk_link(filename, &nd);
766 if (!error) {
767 error = chown_common(nd.dentry, user, group);
768 path_release(&nd);
769 }
770 return error;
771}
772
773
774asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
775{
776 struct file * file;
777 int error = -EBADF;
778
779 file = fget(fd);
780 if (file) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000781 struct dentry * dentry;
782 dentry = file->f_dentry;
783 audit_inode(NULL, dentry->d_inode, 0);
784 error = chown_common(dentry, user, group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 fput(file);
786 }
787 return error;
788}
789
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700790static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
Trond Myklebust834f2a42005-10-18 14:20:16 -0700791 int flags, struct file *f,
792 int (*open)(struct inode *, struct file *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct inode *inode;
795 int error;
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 f->f_flags = flags;
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700798 f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
799 FMODE_PREAD | FMODE_PWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 inode = dentry->d_inode;
801 if (f->f_mode & FMODE_WRITE) {
802 error = get_write_access(inode);
803 if (error)
804 goto cleanup_file;
805 }
806
807 f->f_mapping = inode->i_mapping;
808 f->f_dentry = dentry;
809 f->f_vfsmnt = mnt;
810 f->f_pos = 0;
811 f->f_op = fops_get(inode->i_fop);
812 file_move(f, &inode->i_sb->s_files);
813
Trond Myklebust834f2a42005-10-18 14:20:16 -0700814 if (!open && f->f_op)
815 open = f->f_op->open;
816 if (open) {
817 error = open(inode, f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (error)
819 goto cleanup_all;
820 }
Trond Myklebust834f2a42005-10-18 14:20:16 -0700821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
823
824 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
825
826 /* NB: we're sure to have correct a_ops only after f_op->open */
827 if (f->f_flags & O_DIRECT) {
Carsten Otteceffc072005-06-23 22:05:25 -0700828 if (!f->f_mapping->a_ops ||
829 ((!f->f_mapping->a_ops->direct_IO) &&
830 (!f->f_mapping->a_ops->get_xip_page))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 fput(f);
832 f = ERR_PTR(-EINVAL);
833 }
834 }
835
836 return f;
837
838cleanup_all:
839 fops_put(f->f_op);
840 if (f->f_mode & FMODE_WRITE)
841 put_write_access(inode);
842 file_kill(f);
843 f->f_dentry = NULL;
844 f->f_vfsmnt = NULL;
845cleanup_file:
846 put_filp(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 dput(dentry);
848 mntput(mnt);
849 return ERR_PTR(error);
850}
851
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700852/*
853 * Note that while the flag value (low two bits) for sys_open means:
854 * 00 - read-only
855 * 01 - write-only
856 * 10 - read-write
857 * 11 - special
858 * it is changed into
859 * 00 - no permissions needed
860 * 01 - read-permission
861 * 10 - write-permission
862 * 11 - read-write
863 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
864 * used by symlinks.
865 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800866static struct file *do_filp_open(int dfd, const char *filename, int flags,
867 int mode)
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700868{
869 int namei_flags, error;
870 struct nameidata nd;
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700871
872 namei_flags = flags;
873 if ((namei_flags+1) & O_ACCMODE)
874 namei_flags++;
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700875
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800876 error = open_namei(dfd, filename, namei_flags, mode, &nd);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700877 if (!error)
Trond Myklebust834f2a42005-10-18 14:20:16 -0700878 return nameidata_to_filp(&nd, flags);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700879
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700880 return ERR_PTR(error);
881}
Ulrich Drepper5590ff02006-01-18 17:43:53 -0800882
883struct file *filp_open(const char *filename, int flags, int mode)
884{
885 return do_filp_open(AT_FDCWD, filename, flags, mode);
886}
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700887EXPORT_SYMBOL(filp_open);
888
Trond Myklebust834f2a42005-10-18 14:20:16 -0700889/**
890 * lookup_instantiate_filp - instantiates the open intent filp
891 * @nd: pointer to nameidata
892 * @dentry: pointer to dentry
893 * @open: open callback
894 *
895 * Helper for filesystems that want to use lookup open intents and pass back
896 * a fully instantiated struct file to the caller.
897 * This function is meant to be called from within a filesystem's
898 * lookup method.
Oleg Drokin9a56c212006-03-25 03:07:02 -0800899 * Beware of calling it for non-regular files! Those ->open methods might block
900 * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
901 * leading to a deadlock, as nobody can open that fifo anymore, because
902 * another process to open fifo will block on locked parent when doing lookup).
Trond Myklebust834f2a42005-10-18 14:20:16 -0700903 * Note that in case of error, nd->intent.open.file is destroyed, but the
904 * path information remains valid.
905 * If the open callback is set to NULL, then the standard f_op->open()
906 * filesystem callback is substituted.
907 */
908struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
909 int (*open)(struct inode *, struct file *))
910{
911 if (IS_ERR(nd->intent.open.file))
912 goto out;
913 if (IS_ERR(dentry))
914 goto out_err;
915 nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->mnt),
916 nd->intent.open.flags - 1,
917 nd->intent.open.file,
918 open);
919out:
920 return nd->intent.open.file;
921out_err:
922 release_open_intent(nd);
923 nd->intent.open.file = (struct file *)dentry;
924 goto out;
925}
926EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
927
928/**
929 * nameidata_to_filp - convert a nameidata to an open filp.
930 * @nd: pointer to nameidata
931 * @flags: open flags
932 *
933 * Note that this function destroys the original nameidata
934 */
935struct file *nameidata_to_filp(struct nameidata *nd, int flags)
936{
937 struct file *filp;
938
939 /* Pick up the filp from the open intent */
940 filp = nd->intent.open.file;
941 /* Has the filesystem initialised the file for us? */
942 if (filp->f_dentry == NULL)
943 filp = __dentry_open(nd->dentry, nd->mnt, flags, filp, NULL);
944 else
945 path_release(nd);
946 return filp;
947}
948
Peter Staubach6fdcc212005-11-07 00:59:42 -0800949/*
950 * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
951 * error.
952 */
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700953struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
954{
955 int error;
956 struct file *f;
957
958 error = -ENFILE;
959 f = get_empty_filp();
Peter Staubach6fdcc212005-11-07 00:59:42 -0800960 if (f == NULL) {
961 dput(dentry);
962 mntput(mnt);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700963 return ERR_PTR(error);
Peter Staubach6fdcc212005-11-07 00:59:42 -0800964 }
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700965
Trond Myklebust834f2a42005-10-18 14:20:16 -0700966 return __dentry_open(dentry, mnt, flags, f, NULL);
Peter Staubacha1a5b3d2005-09-13 01:25:12 -0700967}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968EXPORT_SYMBOL(dentry_open);
969
970/*
971 * Find an empty file descriptor entry, and mark it busy.
972 */
973int get_unused_fd(void)
974{
975 struct files_struct * files = current->files;
976 int fd, error;
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700977 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 error = -EMFILE;
980 spin_lock(&files->file_lock);
981
982repeat:
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700983 fdt = files_fdtable(files);
984 fd = find_next_zero_bit(fdt->open_fds->fds_bits,
985 fdt->max_fdset,
Eric Dumazet0c9e63f2006-03-23 03:00:12 -0800986 files->next_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 /*
989 * N.B. For clone tasks sharing a files structure, this test
990 * will limit the total number of files that can be opened.
991 */
992 if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
993 goto out;
994
995 /* Do we need to expand the fd array or fd set? */
996 error = expand_files(files, fd);
997 if (error < 0)
998 goto out;
999
1000 if (error) {
1001 /*
1002 * If we needed to expand the fs array we
1003 * might have blocked - try again.
1004 */
1005 error = -EMFILE;
1006 goto repeat;
1007 }
1008
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001009 FD_SET(fd, fdt->open_fds);
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
1026EXPORT_SYMBOL(get_unused_fd);
1027
Matt Mackallb01ec0e2006-01-08 01:05:20 -08001028static void __put_unused_fd(struct files_struct *files, unsigned int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001030 struct fdtable *fdt = files_fdtable(files);
1031 __FD_CLR(fd, fdt->open_fds);
Eric Dumazet0c9e63f2006-03-23 03:00:12 -08001032 if (fd < files->next_fd)
1033 files->next_fd = fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
1036void fastcall put_unused_fd(unsigned int fd)
1037{
1038 struct files_struct *files = current->files;
1039 spin_lock(&files->file_lock);
1040 __put_unused_fd(files, fd);
1041 spin_unlock(&files->file_lock);
1042}
1043
1044EXPORT_SYMBOL(put_unused_fd);
1045
1046/*
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001047 * Install a file pointer in the fd array.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 *
1049 * The VFS is full of places where we drop the files lock between
1050 * setting the open_fds bitmap and installing the file in the file
1051 * array. At any such point, we are vulnerable to a dup2() race
1052 * installing a file in the array before us. We need to detect this and
1053 * fput() the struct file we are about to overwrite in this case.
1054 *
1055 * It should never happen - if we allow dup2() do it, _really_ bad things
1056 * will follow.
1057 */
1058
1059void fastcall fd_install(unsigned int fd, struct file * file)
1060{
1061 struct files_struct *files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001062 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 spin_lock(&files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001064 fdt = files_fdtable(files);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -07001065 BUG_ON(fdt->fd[fd] != NULL);
1066 rcu_assign_pointer(fdt->fd[fd], file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 spin_unlock(&files->file_lock);
1068}
1069
1070EXPORT_SYMBOL(fd_install);
1071
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001072long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Miklos Szeredie922efc2005-09-06 15:18:25 -07001074 char *tmp = getname(filename);
1075 int fd = PTR_ERR(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (!IS_ERR(tmp)) {
1078 fd = get_unused_fd();
1079 if (fd >= 0) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001080 struct file *f = do_filp_open(dfd, tmp, flags, mode);
Telemaque Ndizihiwefed2fc12005-06-23 00:10:33 -07001081 if (IS_ERR(f)) {
1082 put_unused_fd(fd);
1083 fd = PTR_ERR(f);
1084 } else {
Robert Love0eeca282005-07-12 17:06:03 -04001085 fsnotify_open(f->f_dentry);
Telemaque Ndizihiwefed2fc12005-06-23 00:10:33 -07001086 fd_install(fd, f);
1087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 putname(tmp);
1090 }
1091 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
Miklos Szeredie922efc2005-09-06 15:18:25 -07001093
1094asmlinkage long sys_open(const char __user *filename, int flags, int mode)
1095{
1096 if (force_o_largefile())
1097 flags |= O_LARGEFILE;
1098
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001099 return do_sys_open(AT_FDCWD, filename, flags, mode);
Miklos Szeredie922efc2005-09-06 15:18:25 -07001100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101EXPORT_SYMBOL_GPL(sys_open);
1102
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001103asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
1104 int mode)
1105{
1106 if (force_o_largefile())
1107 flags |= O_LARGEFILE;
1108
1109 return do_sys_open(dfd, filename, flags, mode);
1110}
1111EXPORT_SYMBOL_GPL(sys_openat);
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113#ifndef __alpha__
1114
1115/*
1116 * For backward compatibility? Maybe this should be moved
1117 * into arch/i386 instead?
1118 */
1119asmlinkage long sys_creat(const char __user * pathname, int mode)
1120{
1121 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1122}
1123
1124#endif
1125
1126/*
1127 * "id" is the POSIX thread ID. We use the
1128 * files pointer for this..
1129 */
1130int filp_close(struct file *filp, fl_owner_t id)
1131{
Christoph Lameter45778ca2005-06-23 00:10:17 -07001132 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 if (!file_count(filp)) {
1135 printk(KERN_ERR "VFS: Close: file count is 0\n");
Christoph Lameter45778ca2005-06-23 00:10:17 -07001136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138
Christoph Lameter45778ca2005-06-23 00:10:17 -07001139 if (filp->f_op && filp->f_op->flush)
1140 retval = filp->f_op->flush(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 dnotify_flush(filp, id);
1143 locks_remove_posix(filp, id);
1144 fput(filp);
1145 return retval;
1146}
1147
1148EXPORT_SYMBOL(filp_close);
1149
1150/*
1151 * Careful here! We test whether the file pointer is NULL before
1152 * releasing the fd. This ensures that one clone task can't release
1153 * an fd while another clone is opening it.
1154 */
1155asmlinkage long sys_close(unsigned int fd)
1156{
1157 struct file * filp;
1158 struct files_struct *files = current->files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001159 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 spin_lock(&files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001162 fdt = files_fdtable(files);
1163 if (fd >= fdt->max_fds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 goto out_unlock;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001165 filp = fdt->fd[fd];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (!filp)
1167 goto out_unlock;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -07001168 rcu_assign_pointer(fdt->fd[fd], NULL);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001169 FD_CLR(fd, fdt->close_on_exec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 __put_unused_fd(files, fd);
1171 spin_unlock(&files->file_lock);
1172 return filp_close(filp, files);
1173
1174out_unlock:
1175 spin_unlock(&files->file_lock);
1176 return -EBADF;
1177}
1178
1179EXPORT_SYMBOL(sys_close);
1180
1181/*
1182 * This routine simulates a hangup on the tty, to arrange that users
1183 * are given clean terminals at login time.
1184 */
1185asmlinkage long sys_vhangup(void)
1186{
1187 if (capable(CAP_SYS_TTY_CONFIG)) {
1188 tty_vhangup(current->signal->tty);
1189 return 0;
1190 }
1191 return -EPERM;
1192}
1193
1194/*
1195 * Called when an inode is about to be open.
1196 * We use this to disallow opening large files on 32bit systems if
1197 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1198 * on this flag in sys_open.
1199 */
1200int generic_file_open(struct inode * inode, struct file * filp)
1201{
1202 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1203 return -EFBIG;
1204 return 0;
1205}
1206
1207EXPORT_SYMBOL(generic_file_open);
1208
1209/*
1210 * This is used by subsystems that don't want seekable
1211 * file descriptors
1212 */
1213int nonseekable_open(struct inode *inode, struct file *filp)
1214{
1215 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1216 return 0;
1217}
1218
1219EXPORT_SYMBOL(nonseekable_open);