Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/open.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | #include <linux/string.h> |
| 8 | #include <linux/mm.h> |
| 9 | #include <linux/utime.h> |
| 10 | #include <linux/file.h> |
| 11 | #include <linux/smp_lock.h> |
| 12 | #include <linux/quotaops.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 13 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #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 Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 19 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/security.h> |
| 21 | #include <linux/mount.h> |
| 22 | #include <linux/vfs.h> |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 23 | #include <linux/fcntl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <asm/uaccess.h> |
| 25 | #include <linux/fs.h> |
Yoav Zach | ef3daed | 2005-06-23 00:09:58 -0700 | [diff] [blame] | 26 | #include <linux/personality.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/pagemap.h> |
| 28 | #include <linux/syscalls.h> |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 29 | #include <linux/rcupdate.h> |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 30 | #include <linux/audit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | #include <asm/unistd.h> |
| 33 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 34 | int vfs_statfs(struct dentry *dentry, struct kstatfs *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
| 36 | int retval = -ENODEV; |
| 37 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 38 | if (dentry) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | retval = -ENOSYS; |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 40 | if (dentry->d_sb->s_op->statfs) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | memset(buf, 0, sizeof(*buf)); |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 42 | retval = security_sb_statfs(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | if (retval) |
| 44 | return retval; |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 45 | retval = dentry->d_sb->s_op->statfs(dentry, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | if (retval == 0 && buf->f_frsize == 0) |
| 47 | buf->f_frsize = buf->f_bsize; |
| 48 | } |
| 49 | } |
| 50 | return retval; |
| 51 | } |
| 52 | |
| 53 | EXPORT_SYMBOL(vfs_statfs); |
| 54 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 55 | static int vfs_statfs_native(struct dentry *dentry, struct statfs *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
| 57 | struct kstatfs st; |
| 58 | int retval; |
| 59 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 60 | retval = vfs_statfs(dentry, &st); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 98 | static int vfs_statfs64(struct dentry *dentry, struct statfs64 *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
| 100 | struct kstatfs st; |
| 101 | int retval; |
| 102 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 103 | retval = vfs_statfs(dentry, &st); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 125 | asmlinkage 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; |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 133 | error = vfs_statfs_native(nd.dentry, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | if (!error && copy_to_user(buf, &tmp, sizeof(tmp))) |
| 135 | error = -EFAULT; |
| 136 | path_release(&nd); |
| 137 | } |
| 138 | return error; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | asmlinkage 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; |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 152 | error = vfs_statfs64(nd.dentry, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | if (!error && copy_to_user(buf, &tmp, sizeof(tmp))) |
| 154 | error = -EFAULT; |
| 155 | path_release(&nd); |
| 156 | } |
| 157 | return error; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | asmlinkage 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; |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 171 | error = vfs_statfs_native(file->f_dentry, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | if (!error && copy_to_user(buf, &tmp, sizeof(tmp))) |
| 173 | error = -EFAULT; |
| 174 | fput(file); |
| 175 | out: |
| 176 | return error; |
| 177 | } |
| 178 | |
| 179 | asmlinkage 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; |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 192 | error = vfs_statfs64(file->f_dentry, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | if (!error && copy_to_user(buf, &tmp, sizeof(tmp))) |
| 194 | error = -EFAULT; |
| 195 | fput(file); |
| 196 | out: |
| 197 | return error; |
| 198 | } |
| 199 | |
NeilBrown | 4a30131 | 2006-01-08 01:02:39 -0800 | [diff] [blame] | 200 | int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs, |
| 201 | struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
| 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; |
NeilBrown | 4a30131 | 2006-01-08 01:02:39 -0800 | [diff] [blame] | 211 | newattrs.ia_valid = ATTR_SIZE | time_attrs; |
Miklos Szeredi | cc4e69d | 2005-11-07 00:59:49 -0800 | [diff] [blame] | 212 | if (filp) { |
| 213 | newattrs.ia_file = filp; |
| 214 | newattrs.ia_valid |= ATTR_FILE; |
| 215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 217 | mutex_lock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | err = notify_change(dentry, &newattrs); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 219 | mutex_unlock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | return err; |
| 221 | } |
| 222 | |
Matt Mackall | b01ec0e | 2006-01-08 01:05:20 -0800 | [diff] [blame] | 223 | static long do_sys_truncate(const char __user * path, loff_t length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
| 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 Hellwig | e4543ed | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 247 | error = vfs_permission(&nd, MAY_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | 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); |
NeilBrown | 4a30131 | 2006-01-08 01:02:39 -0800 | [diff] [blame] | 273 | error = do_truncate(nd.dentry, length, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
| 275 | put_write_access(inode); |
| 276 | |
| 277 | dput_and_out: |
| 278 | path_release(&nd); |
| 279 | out: |
| 280 | return error; |
| 281 | } |
| 282 | |
| 283 | asmlinkage 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 Mackall | b01ec0e | 2006-01-08 01:05:20 -0800 | [diff] [blame] | 289 | static long do_sys_ftruncate(unsigned int fd, loff_t length, int small) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
| 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) |
Peter Staubach | 6e656be | 2006-06-25 05:48:36 -0700 | [diff] [blame] | 325 | error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | out_putf: |
| 327 | fput(file); |
| 328 | out: |
| 329 | return error; |
| 330 | } |
| 331 | |
| 332 | asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length) |
| 333 | { |
Linus Torvalds | 0a489cb | 2006-04-18 13:02:48 -0700 | [diff] [blame] | 334 | long ret = do_sys_ftruncate(fd, length, 1); |
Linus Torvalds | 385910f | 2006-04-18 13:22:59 -0700 | [diff] [blame] | 335 | /* avoid REGPARM breakage on x86: */ |
Linus Torvalds | 0a489cb | 2006-04-18 13:02:48 -0700 | [diff] [blame] | 336 | prevent_tail_call(ret); |
| 337 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /* LFS versions of truncate are only needed on 32 bit machines */ |
| 341 | #if BITS_PER_LONG == 32 |
| 342 | asmlinkage long sys_truncate64(const char __user * path, loff_t length) |
| 343 | { |
| 344 | return do_sys_truncate(path, length); |
| 345 | } |
| 346 | |
| 347 | asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length) |
| 348 | { |
Linus Torvalds | 0a489cb | 2006-04-18 13:02:48 -0700 | [diff] [blame] | 349 | long ret = do_sys_ftruncate(fd, length, 0); |
Linus Torvalds | 385910f | 2006-04-18 13:22:59 -0700 | [diff] [blame] | 350 | /* avoid REGPARM breakage on x86: */ |
Linus Torvalds | 0a489cb | 2006-04-18 13:02:48 -0700 | [diff] [blame] | 351 | prevent_tail_call(ret); |
| 352 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 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 | */ |
| 369 | asmlinkage 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, ×->actime); |
| 393 | newattrs.ia_atime.tv_nsec = 0; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 394 | if (!error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | error = get_user(newattrs.ia_mtime.tv_sec, ×->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 Hellwig | e4543ed | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 407 | (error = vfs_permission(&nd, MAY_WRITE)) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | goto dput_and_out; |
| 409 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 410 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | error = notify_change(nd.dentry, &newattrs); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 412 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | dput_and_out: |
| 414 | path_release(&nd); |
| 415 | out: |
| 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 Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 425 | long do_utimes(int dfd, char __user *filename, struct timeval *times) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | { |
| 427 | int error; |
| 428 | struct nameidata nd; |
| 429 | struct inode * inode; |
| 430 | struct iattr newattrs; |
| 431 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 432 | error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
| 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 Hellwig | e4543ed | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 460 | (error = vfs_permission(&nd, MAY_WRITE)) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | goto dput_and_out; |
| 462 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 463 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | error = notify_change(nd.dentry, &newattrs); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 465 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | dput_and_out: |
| 467 | path_release(&nd); |
| 468 | out: |
| 469 | return error; |
| 470 | } |
| 471 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 472 | asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | { |
| 474 | struct timeval times[2]; |
| 475 | |
| 476 | if (utimes && copy_from_user(×, utimes, sizeof(times))) |
| 477 | return -EFAULT; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 478 | return do_utimes(dfd, filename, utimes ? times : NULL); |
| 479 | } |
| 480 | |
| 481 | asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes) |
| 482 | { |
| 483 | return sys_futimesat(AT_FDCWD, filename, utimes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
| 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 Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 492 | asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | { |
| 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 Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 522 | res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | if (!res) { |
Christoph Hellwig | e4543ed | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 524 | res = vfs_permission(&nd, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | /* 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 Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 539 | asmlinkage long sys_access(const char __user *filename, int mode) |
| 540 | { |
| 541 | return sys_faccessat(AT_FDCWD, filename, mode); |
| 542 | } |
| 543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | asmlinkage long sys_chdir(const char __user * filename) |
| 545 | { |
| 546 | struct nameidata nd; |
| 547 | int error; |
| 548 | |
Miklos Szeredi | 650a898 | 2006-09-29 01:59:35 -0700 | [diff] [blame] | 549 | error = __user_walk(filename, |
| 550 | LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | if (error) |
| 552 | goto out; |
| 553 | |
Christoph Hellwig | e4543ed | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 554 | error = vfs_permission(&nd, MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | if (error) |
| 556 | goto dput_and_out; |
| 557 | |
| 558 | set_fs_pwd(current->fs, nd.mnt, nd.dentry); |
| 559 | |
| 560 | dput_and_out: |
| 561 | path_release(&nd); |
| 562 | out: |
| 563 | return error; |
| 564 | } |
| 565 | |
| 566 | asmlinkage long sys_fchdir(unsigned int fd) |
| 567 | { |
| 568 | struct file *file; |
| 569 | struct dentry *dentry; |
| 570 | struct inode *inode; |
| 571 | struct vfsmount *mnt; |
| 572 | int error; |
| 573 | |
| 574 | error = -EBADF; |
| 575 | file = fget(fd); |
| 576 | if (!file) |
| 577 | goto out; |
| 578 | |
| 579 | dentry = file->f_dentry; |
| 580 | mnt = file->f_vfsmnt; |
| 581 | inode = dentry->d_inode; |
| 582 | |
| 583 | error = -ENOTDIR; |
| 584 | if (!S_ISDIR(inode->i_mode)) |
| 585 | goto out_putf; |
| 586 | |
Christoph Hellwig | 8c744fb | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 587 | error = file_permission(file, MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | if (!error) |
| 589 | set_fs_pwd(current->fs, mnt, dentry); |
| 590 | out_putf: |
| 591 | fput(file); |
| 592 | out: |
| 593 | return error; |
| 594 | } |
| 595 | |
| 596 | asmlinkage long sys_chroot(const char __user * filename) |
| 597 | { |
| 598 | struct nameidata nd; |
| 599 | int error; |
| 600 | |
| 601 | error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd); |
| 602 | if (error) |
| 603 | goto out; |
| 604 | |
Christoph Hellwig | e4543ed | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 605 | error = vfs_permission(&nd, MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | if (error) |
| 607 | goto dput_and_out; |
| 608 | |
| 609 | error = -EPERM; |
| 610 | if (!capable(CAP_SYS_CHROOT)) |
| 611 | goto dput_and_out; |
| 612 | |
| 613 | set_fs_root(current->fs, nd.mnt, nd.dentry); |
| 614 | set_fs_altroot(); |
| 615 | error = 0; |
| 616 | dput_and_out: |
| 617 | path_release(&nd); |
| 618 | out: |
| 619 | return error; |
| 620 | } |
| 621 | |
| 622 | asmlinkage long sys_fchmod(unsigned int fd, mode_t mode) |
| 623 | { |
| 624 | struct inode * inode; |
| 625 | struct dentry * dentry; |
| 626 | struct file * file; |
| 627 | int err = -EBADF; |
| 628 | struct iattr newattrs; |
| 629 | |
| 630 | file = fget(fd); |
| 631 | if (!file) |
| 632 | goto out; |
| 633 | |
| 634 | dentry = file->f_dentry; |
| 635 | inode = dentry->d_inode; |
| 636 | |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 637 | audit_inode(NULL, inode); |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | err = -EROFS; |
| 640 | if (IS_RDONLY(inode)) |
| 641 | goto out_putf; |
| 642 | err = -EPERM; |
| 643 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 644 | goto out_putf; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 645 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | if (mode == (mode_t) -1) |
| 647 | mode = inode->i_mode; |
| 648 | newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO); |
| 649 | newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; |
| 650 | err = notify_change(dentry, &newattrs); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 651 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
| 653 | out_putf: |
| 654 | fput(file); |
| 655 | out: |
| 656 | return err; |
| 657 | } |
| 658 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 659 | asmlinkage long sys_fchmodat(int dfd, const char __user *filename, |
| 660 | mode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | { |
| 662 | struct nameidata nd; |
| 663 | struct inode * inode; |
| 664 | int error; |
| 665 | struct iattr newattrs; |
| 666 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 667 | error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | if (error) |
| 669 | goto out; |
| 670 | inode = nd.dentry->d_inode; |
| 671 | |
| 672 | error = -EROFS; |
| 673 | if (IS_RDONLY(inode)) |
| 674 | goto dput_and_out; |
| 675 | |
| 676 | error = -EPERM; |
| 677 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 678 | goto dput_and_out; |
| 679 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 680 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | if (mode == (mode_t) -1) |
| 682 | mode = inode->i_mode; |
| 683 | newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO); |
| 684 | newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; |
| 685 | error = notify_change(nd.dentry, &newattrs); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 686 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
| 688 | dput_and_out: |
| 689 | path_release(&nd); |
| 690 | out: |
| 691 | return error; |
| 692 | } |
| 693 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 694 | asmlinkage long sys_chmod(const char __user *filename, mode_t mode) |
| 695 | { |
| 696 | return sys_fchmodat(AT_FDCWD, filename, mode); |
| 697 | } |
| 698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | static int chown_common(struct dentry * dentry, uid_t user, gid_t group) |
| 700 | { |
| 701 | struct inode * inode; |
| 702 | int error; |
| 703 | struct iattr newattrs; |
| 704 | |
| 705 | error = -ENOENT; |
| 706 | if (!(inode = dentry->d_inode)) { |
| 707 | printk(KERN_ERR "chown_common: NULL inode\n"); |
| 708 | goto out; |
| 709 | } |
| 710 | error = -EROFS; |
| 711 | if (IS_RDONLY(inode)) |
| 712 | goto out; |
| 713 | error = -EPERM; |
| 714 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 715 | goto out; |
| 716 | newattrs.ia_valid = ATTR_CTIME; |
| 717 | if (user != (uid_t) -1) { |
| 718 | newattrs.ia_valid |= ATTR_UID; |
| 719 | newattrs.ia_uid = user; |
| 720 | } |
| 721 | if (group != (gid_t) -1) { |
| 722 | newattrs.ia_valid |= ATTR_GID; |
| 723 | newattrs.ia_gid = group; |
| 724 | } |
| 725 | if (!S_ISDIR(inode->i_mode)) |
| 726 | newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 727 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | error = notify_change(dentry, &newattrs); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 729 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | out: |
| 731 | return error; |
| 732 | } |
| 733 | |
| 734 | asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group) |
| 735 | { |
| 736 | struct nameidata nd; |
| 737 | int error; |
| 738 | |
| 739 | error = user_path_walk(filename, &nd); |
| 740 | if (!error) { |
| 741 | error = chown_common(nd.dentry, user, group); |
| 742 | path_release(&nd); |
| 743 | } |
| 744 | return error; |
| 745 | } |
| 746 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 747 | asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user, |
| 748 | gid_t group, int flag) |
| 749 | { |
| 750 | struct nameidata nd; |
| 751 | int error = -EINVAL; |
| 752 | int follow; |
| 753 | |
| 754 | if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0) |
| 755 | goto out; |
| 756 | |
| 757 | follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW; |
| 758 | error = __user_walk_fd(dfd, filename, follow, &nd); |
| 759 | if (!error) { |
| 760 | error = chown_common(nd.dentry, user, group); |
| 761 | path_release(&nd); |
| 762 | } |
| 763 | out: |
| 764 | return error; |
| 765 | } |
| 766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group) |
| 768 | { |
| 769 | struct nameidata nd; |
| 770 | int error; |
| 771 | |
| 772 | error = user_path_walk_link(filename, &nd); |
| 773 | if (!error) { |
| 774 | error = chown_common(nd.dentry, user, group); |
| 775 | path_release(&nd); |
| 776 | } |
| 777 | return error; |
| 778 | } |
| 779 | |
| 780 | |
| 781 | asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group) |
| 782 | { |
| 783 | struct file * file; |
| 784 | int error = -EBADF; |
| 785 | |
| 786 | file = fget(fd); |
| 787 | if (file) { |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 788 | struct dentry * dentry; |
| 789 | dentry = file->f_dentry; |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 790 | audit_inode(NULL, dentry->d_inode); |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 791 | error = chown_common(dentry, user, group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | fput(file); |
| 793 | } |
| 794 | return error; |
| 795 | } |
| 796 | |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 797 | static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt, |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 798 | int flags, struct file *f, |
| 799 | int (*open)(struct inode *, struct file *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | struct inode *inode; |
| 802 | int error; |
| 803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | f->f_flags = flags; |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 805 | f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK | |
| 806 | FMODE_PREAD | FMODE_PWRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | inode = dentry->d_inode; |
| 808 | if (f->f_mode & FMODE_WRITE) { |
| 809 | error = get_write_access(inode); |
| 810 | if (error) |
| 811 | goto cleanup_file; |
| 812 | } |
| 813 | |
| 814 | f->f_mapping = inode->i_mapping; |
| 815 | f->f_dentry = dentry; |
| 816 | f->f_vfsmnt = mnt; |
| 817 | f->f_pos = 0; |
| 818 | f->f_op = fops_get(inode->i_fop); |
| 819 | file_move(f, &inode->i_sb->s_files); |
| 820 | |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 821 | if (!open && f->f_op) |
| 822 | open = f->f_op->open; |
| 823 | if (open) { |
| 824 | error = open(inode, f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | if (error) |
| 826 | goto cleanup_all; |
| 827 | } |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); |
| 830 | |
| 831 | file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping); |
| 832 | |
| 833 | /* NB: we're sure to have correct a_ops only after f_op->open */ |
| 834 | if (f->f_flags & O_DIRECT) { |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 835 | if (!f->f_mapping->a_ops || |
| 836 | ((!f->f_mapping->a_ops->direct_IO) && |
| 837 | (!f->f_mapping->a_ops->get_xip_page))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | fput(f); |
| 839 | f = ERR_PTR(-EINVAL); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | return f; |
| 844 | |
| 845 | cleanup_all: |
| 846 | fops_put(f->f_op); |
| 847 | if (f->f_mode & FMODE_WRITE) |
| 848 | put_write_access(inode); |
| 849 | file_kill(f); |
| 850 | f->f_dentry = NULL; |
| 851 | f->f_vfsmnt = NULL; |
| 852 | cleanup_file: |
| 853 | put_filp(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | dput(dentry); |
| 855 | mntput(mnt); |
| 856 | return ERR_PTR(error); |
| 857 | } |
| 858 | |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 859 | /* |
| 860 | * Note that while the flag value (low two bits) for sys_open means: |
| 861 | * 00 - read-only |
| 862 | * 01 - write-only |
| 863 | * 10 - read-write |
| 864 | * 11 - special |
| 865 | * it is changed into |
| 866 | * 00 - no permissions needed |
| 867 | * 01 - read-permission |
| 868 | * 10 - write-permission |
| 869 | * 11 - read-write |
| 870 | * for the internal routines (ie open_namei()/follow_link() etc). 00 is |
| 871 | * used by symlinks. |
| 872 | */ |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 873 | static struct file *do_filp_open(int dfd, const char *filename, int flags, |
| 874 | int mode) |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 875 | { |
| 876 | int namei_flags, error; |
| 877 | struct nameidata nd; |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 878 | |
| 879 | namei_flags = flags; |
| 880 | if ((namei_flags+1) & O_ACCMODE) |
| 881 | namei_flags++; |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 882 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 883 | error = open_namei(dfd, filename, namei_flags, mode, &nd); |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 884 | if (!error) |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 885 | return nameidata_to_filp(&nd, flags); |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 886 | |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 887 | return ERR_PTR(error); |
| 888 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 889 | |
| 890 | struct file *filp_open(const char *filename, int flags, int mode) |
| 891 | { |
| 892 | return do_filp_open(AT_FDCWD, filename, flags, mode); |
| 893 | } |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 894 | EXPORT_SYMBOL(filp_open); |
| 895 | |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 896 | /** |
| 897 | * lookup_instantiate_filp - instantiates the open intent filp |
| 898 | * @nd: pointer to nameidata |
| 899 | * @dentry: pointer to dentry |
| 900 | * @open: open callback |
| 901 | * |
| 902 | * Helper for filesystems that want to use lookup open intents and pass back |
| 903 | * a fully instantiated struct file to the caller. |
| 904 | * This function is meant to be called from within a filesystem's |
| 905 | * lookup method. |
Oleg Drokin | 9a56c21 | 2006-03-25 03:07:02 -0800 | [diff] [blame] | 906 | * Beware of calling it for non-regular files! Those ->open methods might block |
| 907 | * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo, |
| 908 | * leading to a deadlock, as nobody can open that fifo anymore, because |
| 909 | * another process to open fifo will block on locked parent when doing lookup). |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 910 | * Note that in case of error, nd->intent.open.file is destroyed, but the |
| 911 | * path information remains valid. |
| 912 | * If the open callback is set to NULL, then the standard f_op->open() |
| 913 | * filesystem callback is substituted. |
| 914 | */ |
| 915 | struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry, |
| 916 | int (*open)(struct inode *, struct file *)) |
| 917 | { |
| 918 | if (IS_ERR(nd->intent.open.file)) |
| 919 | goto out; |
| 920 | if (IS_ERR(dentry)) |
| 921 | goto out_err; |
| 922 | nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->mnt), |
| 923 | nd->intent.open.flags - 1, |
| 924 | nd->intent.open.file, |
| 925 | open); |
| 926 | out: |
| 927 | return nd->intent.open.file; |
| 928 | out_err: |
| 929 | release_open_intent(nd); |
| 930 | nd->intent.open.file = (struct file *)dentry; |
| 931 | goto out; |
| 932 | } |
| 933 | EXPORT_SYMBOL_GPL(lookup_instantiate_filp); |
| 934 | |
| 935 | /** |
| 936 | * nameidata_to_filp - convert a nameidata to an open filp. |
| 937 | * @nd: pointer to nameidata |
| 938 | * @flags: open flags |
| 939 | * |
| 940 | * Note that this function destroys the original nameidata |
| 941 | */ |
| 942 | struct file *nameidata_to_filp(struct nameidata *nd, int flags) |
| 943 | { |
| 944 | struct file *filp; |
| 945 | |
| 946 | /* Pick up the filp from the open intent */ |
| 947 | filp = nd->intent.open.file; |
| 948 | /* Has the filesystem initialised the file for us? */ |
| 949 | if (filp->f_dentry == NULL) |
| 950 | filp = __dentry_open(nd->dentry, nd->mnt, flags, filp, NULL); |
| 951 | else |
| 952 | path_release(nd); |
| 953 | return filp; |
| 954 | } |
| 955 | |
Peter Staubach | 6fdcc21 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 956 | /* |
| 957 | * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an |
| 958 | * error. |
| 959 | */ |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 960 | struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags) |
| 961 | { |
| 962 | int error; |
| 963 | struct file *f; |
| 964 | |
| 965 | error = -ENFILE; |
| 966 | f = get_empty_filp(); |
Peter Staubach | 6fdcc21 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 967 | if (f == NULL) { |
| 968 | dput(dentry); |
| 969 | mntput(mnt); |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 970 | return ERR_PTR(error); |
Peter Staubach | 6fdcc21 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 971 | } |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 972 | |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 973 | return __dentry_open(dentry, mnt, flags, f, NULL); |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 974 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | EXPORT_SYMBOL(dentry_open); |
| 976 | |
| 977 | /* |
| 978 | * Find an empty file descriptor entry, and mark it busy. |
| 979 | */ |
| 980 | int get_unused_fd(void) |
| 981 | { |
| 982 | struct files_struct * files = current->files; |
| 983 | int fd, error; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 984 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
| 986 | error = -EMFILE; |
| 987 | spin_lock(&files->file_lock); |
| 988 | |
| 989 | repeat: |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 990 | fdt = files_fdtable(files); |
| 991 | fd = find_next_zero_bit(fdt->open_fds->fds_bits, |
| 992 | fdt->max_fdset, |
Eric Dumazet | 0c9e63f | 2006-03-23 03:00:12 -0800 | [diff] [blame] | 993 | files->next_fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
| 995 | /* |
| 996 | * N.B. For clone tasks sharing a files structure, this test |
| 997 | * will limit the total number of files that can be opened. |
| 998 | */ |
| 999 | if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur) |
| 1000 | goto out; |
| 1001 | |
| 1002 | /* Do we need to expand the fd array or fd set? */ |
| 1003 | error = expand_files(files, fd); |
| 1004 | if (error < 0) |
| 1005 | goto out; |
| 1006 | |
| 1007 | if (error) { |
| 1008 | /* |
| 1009 | * If we needed to expand the fs array we |
| 1010 | * might have blocked - try again. |
| 1011 | */ |
| 1012 | error = -EMFILE; |
| 1013 | goto repeat; |
| 1014 | } |
| 1015 | |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1016 | FD_SET(fd, fdt->open_fds); |
| 1017 | FD_CLR(fd, fdt->close_on_exec); |
Eric Dumazet | 0c9e63f | 2006-03-23 03:00:12 -0800 | [diff] [blame] | 1018 | files->next_fd = fd + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | #if 1 |
| 1020 | /* Sanity check */ |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1021 | if (fdt->fd[fd] != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1023 | fdt->fd[fd] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | } |
| 1025 | #endif |
| 1026 | error = fd; |
| 1027 | |
| 1028 | out: |
| 1029 | spin_unlock(&files->file_lock); |
| 1030 | return error; |
| 1031 | } |
| 1032 | |
| 1033 | EXPORT_SYMBOL(get_unused_fd); |
| 1034 | |
Matt Mackall | b01ec0e | 2006-01-08 01:05:20 -0800 | [diff] [blame] | 1035 | static void __put_unused_fd(struct files_struct *files, unsigned int fd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | { |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1037 | struct fdtable *fdt = files_fdtable(files); |
| 1038 | __FD_CLR(fd, fdt->open_fds); |
Eric Dumazet | 0c9e63f | 2006-03-23 03:00:12 -0800 | [diff] [blame] | 1039 | if (fd < files->next_fd) |
| 1040 | files->next_fd = fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | void fastcall put_unused_fd(unsigned int fd) |
| 1044 | { |
| 1045 | struct files_struct *files = current->files; |
| 1046 | spin_lock(&files->file_lock); |
| 1047 | __put_unused_fd(files, fd); |
| 1048 | spin_unlock(&files->file_lock); |
| 1049 | } |
| 1050 | |
| 1051 | EXPORT_SYMBOL(put_unused_fd); |
| 1052 | |
| 1053 | /* |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1054 | * Install a file pointer in the fd array. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | * |
| 1056 | * The VFS is full of places where we drop the files lock between |
| 1057 | * setting the open_fds bitmap and installing the file in the file |
| 1058 | * array. At any such point, we are vulnerable to a dup2() race |
| 1059 | * installing a file in the array before us. We need to detect this and |
| 1060 | * fput() the struct file we are about to overwrite in this case. |
| 1061 | * |
| 1062 | * It should never happen - if we allow dup2() do it, _really_ bad things |
| 1063 | * will follow. |
| 1064 | */ |
| 1065 | |
| 1066 | void fastcall fd_install(unsigned int fd, struct file * file) |
| 1067 | { |
| 1068 | struct files_struct *files = current->files; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1069 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | spin_lock(&files->file_lock); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1071 | fdt = files_fdtable(files); |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 1072 | BUG_ON(fdt->fd[fd] != NULL); |
| 1073 | rcu_assign_pointer(fdt->fd[fd], file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | spin_unlock(&files->file_lock); |
| 1075 | } |
| 1076 | |
| 1077 | EXPORT_SYMBOL(fd_install); |
| 1078 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1079 | long do_sys_open(int dfd, const char __user *filename, int flags, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | { |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1081 | char *tmp = getname(filename); |
| 1082 | int fd = PTR_ERR(tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | if (!IS_ERR(tmp)) { |
| 1085 | fd = get_unused_fd(); |
| 1086 | if (fd >= 0) { |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1087 | struct file *f = do_filp_open(dfd, tmp, flags, mode); |
Telemaque Ndizihiwe | fed2fc1 | 2005-06-23 00:10:33 -0700 | [diff] [blame] | 1088 | if (IS_ERR(f)) { |
| 1089 | put_unused_fd(fd); |
| 1090 | fd = PTR_ERR(f); |
| 1091 | } else { |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 1092 | fsnotify_open(f->f_dentry); |
Telemaque Ndizihiwe | fed2fc1 | 2005-06-23 00:10:33 -0700 | [diff] [blame] | 1093 | fd_install(fd, f); |
| 1094 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | putname(tmp); |
| 1097 | } |
| 1098 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | } |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1100 | |
| 1101 | asmlinkage long sys_open(const char __user *filename, int flags, int mode) |
| 1102 | { |
Linus Torvalds | 385910f | 2006-04-18 13:22:59 -0700 | [diff] [blame] | 1103 | long ret; |
| 1104 | |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1105 | if (force_o_largefile()) |
| 1106 | flags |= O_LARGEFILE; |
| 1107 | |
Linus Torvalds | 385910f | 2006-04-18 13:22:59 -0700 | [diff] [blame] | 1108 | ret = do_sys_open(AT_FDCWD, filename, flags, mode); |
| 1109 | /* avoid REGPARM breakage on x86: */ |
| 1110 | prevent_tail_call(ret); |
| 1111 | return ret; |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | EXPORT_SYMBOL_GPL(sys_open); |
| 1114 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1115 | asmlinkage long sys_openat(int dfd, const char __user *filename, int flags, |
| 1116 | int mode) |
| 1117 | { |
Linus Torvalds | 385910f | 2006-04-18 13:22:59 -0700 | [diff] [blame] | 1118 | long ret; |
| 1119 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1120 | if (force_o_largefile()) |
| 1121 | flags |= O_LARGEFILE; |
| 1122 | |
Linus Torvalds | 385910f | 2006-04-18 13:22:59 -0700 | [diff] [blame] | 1123 | ret = do_sys_open(dfd, filename, flags, mode); |
| 1124 | /* avoid REGPARM breakage on x86: */ |
| 1125 | prevent_tail_call(ret); |
| 1126 | return ret; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1127 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | #ifndef __alpha__ |
| 1130 | |
| 1131 | /* |
| 1132 | * For backward compatibility? Maybe this should be moved |
| 1133 | * into arch/i386 instead? |
| 1134 | */ |
| 1135 | asmlinkage 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 | */ |
| 1146 | int filp_close(struct file *filp, fl_owner_t id) |
| 1147 | { |
Christoph Lameter | 45778ca | 2005-06-23 00:10:17 -0700 | [diff] [blame] | 1148 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | |
| 1150 | if (!file_count(filp)) { |
| 1151 | printk(KERN_ERR "VFS: Close: file count is 0\n"); |
Christoph Lameter | 45778ca | 2005-06-23 00:10:17 -0700 | [diff] [blame] | 1152 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
Christoph Lameter | 45778ca | 2005-06-23 00:10:17 -0700 | [diff] [blame] | 1155 | if (filp->f_op && filp->f_op->flush) |
Miklos Szeredi | 75e1fcc | 2006-06-23 02:05:12 -0700 | [diff] [blame] | 1156 | retval = filp->f_op->flush(filp, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | |
| 1158 | dnotify_flush(filp, id); |
| 1159 | locks_remove_posix(filp, id); |
| 1160 | fput(filp); |
| 1161 | return retval; |
| 1162 | } |
| 1163 | |
| 1164 | EXPORT_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 | */ |
| 1171 | asmlinkage long sys_close(unsigned int fd) |
| 1172 | { |
| 1173 | struct file * filp; |
| 1174 | struct files_struct *files = current->files; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1175 | struct fdtable *fdt; |
Ernie Petrides | ee731f4 | 2006-09-29 02:00:13 -0700 | [diff] [blame] | 1176 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | |
| 1178 | spin_lock(&files->file_lock); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1179 | fdt = files_fdtable(files); |
| 1180 | if (fd >= fdt->max_fds) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | goto out_unlock; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1182 | filp = fdt->fd[fd]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | if (!filp) |
| 1184 | goto out_unlock; |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 1185 | rcu_assign_pointer(fdt->fd[fd], NULL); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1186 | FD_CLR(fd, fdt->close_on_exec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | __put_unused_fd(files, fd); |
| 1188 | spin_unlock(&files->file_lock); |
Ernie Petrides | ee731f4 | 2006-09-29 02:00:13 -0700 | [diff] [blame] | 1189 | retval = filp_close(filp, files); |
| 1190 | |
| 1191 | /* can't restart close syscall because file table entry was cleared */ |
| 1192 | if (unlikely(retval == -ERESTARTSYS || |
| 1193 | retval == -ERESTARTNOINTR || |
| 1194 | retval == -ERESTARTNOHAND || |
| 1195 | retval == -ERESTART_RESTARTBLOCK)) |
| 1196 | retval = -EINTR; |
| 1197 | |
| 1198 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | |
| 1200 | out_unlock: |
| 1201 | spin_unlock(&files->file_lock); |
| 1202 | return -EBADF; |
| 1203 | } |
| 1204 | |
| 1205 | EXPORT_SYMBOL(sys_close); |
| 1206 | |
| 1207 | /* |
| 1208 | * This routine simulates a hangup on the tty, to arrange that users |
| 1209 | * are given clean terminals at login time. |
| 1210 | */ |
| 1211 | asmlinkage long sys_vhangup(void) |
| 1212 | { |
| 1213 | if (capable(CAP_SYS_TTY_CONFIG)) { |
| 1214 | tty_vhangup(current->signal->tty); |
| 1215 | return 0; |
| 1216 | } |
| 1217 | return -EPERM; |
| 1218 | } |
| 1219 | |
| 1220 | /* |
| 1221 | * Called when an inode is about to be open. |
| 1222 | * We use this to disallow opening large files on 32bit systems if |
| 1223 | * the caller didn't specify O_LARGEFILE. On 64bit systems we force |
| 1224 | * on this flag in sys_open. |
| 1225 | */ |
| 1226 | int generic_file_open(struct inode * inode, struct file * filp) |
| 1227 | { |
| 1228 | if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS) |
| 1229 | return -EFBIG; |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | EXPORT_SYMBOL(generic_file_open); |
| 1234 | |
| 1235 | /* |
| 1236 | * This is used by subsystems that don't want seekable |
| 1237 | * file descriptors |
| 1238 | */ |
| 1239 | int nonseekable_open(struct inode *inode, struct file *filp) |
| 1240 | { |
| 1241 | filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE); |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | EXPORT_SYMBOL(nonseekable_open); |