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> |
| 19 | #include <linux/security.h> |
| 20 | #include <linux/mount.h> |
| 21 | #include <linux/vfs.h> |
| 22 | #include <asm/uaccess.h> |
| 23 | #include <linux/fs.h> |
Yoav Zach | ef3daed | 2005-06-23 00:09:58 -0700 | [diff] [blame] | 24 | #include <linux/personality.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/pagemap.h> |
| 26 | #include <linux/syscalls.h> |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 27 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #include <asm/unistd.h> |
| 30 | |
| 31 | int vfs_statfs(struct super_block *sb, struct kstatfs *buf) |
| 32 | { |
| 33 | int retval = -ENODEV; |
| 34 | |
| 35 | if (sb) { |
| 36 | retval = -ENOSYS; |
| 37 | if (sb->s_op->statfs) { |
| 38 | memset(buf, 0, sizeof(*buf)); |
| 39 | retval = security_sb_statfs(sb); |
| 40 | if (retval) |
| 41 | return retval; |
| 42 | retval = sb->s_op->statfs(sb, buf); |
| 43 | if (retval == 0 && buf->f_frsize == 0) |
| 44 | buf->f_frsize = buf->f_bsize; |
| 45 | } |
| 46 | } |
| 47 | return retval; |
| 48 | } |
| 49 | |
| 50 | EXPORT_SYMBOL(vfs_statfs); |
| 51 | |
| 52 | static int vfs_statfs_native(struct super_block *sb, struct statfs *buf) |
| 53 | { |
| 54 | struct kstatfs st; |
| 55 | int retval; |
| 56 | |
| 57 | retval = vfs_statfs(sb, &st); |
| 58 | if (retval) |
| 59 | return retval; |
| 60 | |
| 61 | if (sizeof(*buf) == sizeof(st)) |
| 62 | memcpy(buf, &st, sizeof(st)); |
| 63 | else { |
| 64 | if (sizeof buf->f_blocks == 4) { |
| 65 | if ((st.f_blocks | st.f_bfree | st.f_bavail) & |
| 66 | 0xffffffff00000000ULL) |
| 67 | return -EOVERFLOW; |
| 68 | /* |
| 69 | * f_files and f_ffree may be -1; it's okay to stuff |
| 70 | * that into 32 bits |
| 71 | */ |
| 72 | if (st.f_files != -1 && |
| 73 | (st.f_files & 0xffffffff00000000ULL)) |
| 74 | return -EOVERFLOW; |
| 75 | if (st.f_ffree != -1 && |
| 76 | (st.f_ffree & 0xffffffff00000000ULL)) |
| 77 | return -EOVERFLOW; |
| 78 | } |
| 79 | |
| 80 | buf->f_type = st.f_type; |
| 81 | buf->f_bsize = st.f_bsize; |
| 82 | buf->f_blocks = st.f_blocks; |
| 83 | buf->f_bfree = st.f_bfree; |
| 84 | buf->f_bavail = st.f_bavail; |
| 85 | buf->f_files = st.f_files; |
| 86 | buf->f_ffree = st.f_ffree; |
| 87 | buf->f_fsid = st.f_fsid; |
| 88 | buf->f_namelen = st.f_namelen; |
| 89 | buf->f_frsize = st.f_frsize; |
| 90 | memset(buf->f_spare, 0, sizeof(buf->f_spare)); |
| 91 | } |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf) |
| 96 | { |
| 97 | struct kstatfs st; |
| 98 | int retval; |
| 99 | |
| 100 | retval = vfs_statfs(sb, &st); |
| 101 | if (retval) |
| 102 | return retval; |
| 103 | |
| 104 | if (sizeof(*buf) == sizeof(st)) |
| 105 | memcpy(buf, &st, sizeof(st)); |
| 106 | else { |
| 107 | buf->f_type = st.f_type; |
| 108 | buf->f_bsize = st.f_bsize; |
| 109 | buf->f_blocks = st.f_blocks; |
| 110 | buf->f_bfree = st.f_bfree; |
| 111 | buf->f_bavail = st.f_bavail; |
| 112 | buf->f_files = st.f_files; |
| 113 | buf->f_ffree = st.f_ffree; |
| 114 | buf->f_fsid = st.f_fsid; |
| 115 | buf->f_namelen = st.f_namelen; |
| 116 | buf->f_frsize = st.f_frsize; |
| 117 | memset(buf->f_spare, 0, sizeof(buf->f_spare)); |
| 118 | } |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf) |
| 123 | { |
| 124 | struct nameidata nd; |
| 125 | int error; |
| 126 | |
| 127 | error = user_path_walk(path, &nd); |
| 128 | if (!error) { |
| 129 | struct statfs tmp; |
| 130 | error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp); |
| 131 | if (!error && copy_to_user(buf, &tmp, sizeof(tmp))) |
| 132 | error = -EFAULT; |
| 133 | path_release(&nd); |
| 134 | } |
| 135 | return error; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf) |
| 140 | { |
| 141 | struct nameidata nd; |
| 142 | long error; |
| 143 | |
| 144 | if (sz != sizeof(*buf)) |
| 145 | return -EINVAL; |
| 146 | error = user_path_walk(path, &nd); |
| 147 | if (!error) { |
| 148 | struct statfs64 tmp; |
| 149 | error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp); |
| 150 | if (!error && copy_to_user(buf, &tmp, sizeof(tmp))) |
| 151 | error = -EFAULT; |
| 152 | path_release(&nd); |
| 153 | } |
| 154 | return error; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf) |
| 159 | { |
| 160 | struct file * file; |
| 161 | struct statfs tmp; |
| 162 | int error; |
| 163 | |
| 164 | error = -EBADF; |
| 165 | file = fget(fd); |
| 166 | if (!file) |
| 167 | goto out; |
| 168 | error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp); |
| 169 | if (!error && copy_to_user(buf, &tmp, sizeof(tmp))) |
| 170 | error = -EFAULT; |
| 171 | fput(file); |
| 172 | out: |
| 173 | return error; |
| 174 | } |
| 175 | |
| 176 | asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf) |
| 177 | { |
| 178 | struct file * file; |
| 179 | struct statfs64 tmp; |
| 180 | int error; |
| 181 | |
| 182 | if (sz != sizeof(*buf)) |
| 183 | return -EINVAL; |
| 184 | |
| 185 | error = -EBADF; |
| 186 | file = fget(fd); |
| 187 | if (!file) |
| 188 | goto out; |
| 189 | error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp); |
| 190 | if (!error && copy_to_user(buf, &tmp, sizeof(tmp))) |
| 191 | error = -EFAULT; |
| 192 | fput(file); |
| 193 | out: |
| 194 | return error; |
| 195 | } |
| 196 | |
Miklos Szeredi | cc4e69d | 2005-11-07 00:59:49 -0800 | [diff] [blame] | 197 | int do_truncate(struct dentry *dentry, loff_t length, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
| 199 | int err; |
| 200 | struct iattr newattrs; |
| 201 | |
| 202 | /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */ |
| 203 | if (length < 0) |
| 204 | return -EINVAL; |
| 205 | |
| 206 | newattrs.ia_size = length; |
| 207 | newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME; |
Miklos Szeredi | cc4e69d | 2005-11-07 00:59:49 -0800 | [diff] [blame] | 208 | if (filp) { |
| 209 | newattrs.ia_file = filp; |
| 210 | newattrs.ia_valid |= ATTR_FILE; |
| 211 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | down(&dentry->d_inode->i_sem); |
| 214 | err = notify_change(dentry, &newattrs); |
| 215 | up(&dentry->d_inode->i_sem); |
| 216 | return err; |
| 217 | } |
| 218 | |
| 219 | static inline long do_sys_truncate(const char __user * path, loff_t length) |
| 220 | { |
| 221 | struct nameidata nd; |
| 222 | struct inode * inode; |
| 223 | int error; |
| 224 | |
| 225 | error = -EINVAL; |
| 226 | if (length < 0) /* sorry, but loff_t says... */ |
| 227 | goto out; |
| 228 | |
| 229 | error = user_path_walk(path, &nd); |
| 230 | if (error) |
| 231 | goto out; |
| 232 | inode = nd.dentry->d_inode; |
| 233 | |
| 234 | /* For directories it's -EISDIR, for other non-regulars - -EINVAL */ |
| 235 | error = -EISDIR; |
| 236 | if (S_ISDIR(inode->i_mode)) |
| 237 | goto dput_and_out; |
| 238 | |
| 239 | error = -EINVAL; |
| 240 | if (!S_ISREG(inode->i_mode)) |
| 241 | goto dput_and_out; |
| 242 | |
Christoph Hellwig | e4543ed | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 243 | error = vfs_permission(&nd, MAY_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | if (error) |
| 245 | goto dput_and_out; |
| 246 | |
| 247 | error = -EROFS; |
| 248 | if (IS_RDONLY(inode)) |
| 249 | goto dput_and_out; |
| 250 | |
| 251 | error = -EPERM; |
| 252 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 253 | goto dput_and_out; |
| 254 | |
| 255 | /* |
| 256 | * Make sure that there are no leases. |
| 257 | */ |
| 258 | error = break_lease(inode, FMODE_WRITE); |
| 259 | if (error) |
| 260 | goto dput_and_out; |
| 261 | |
| 262 | error = get_write_access(inode); |
| 263 | if (error) |
| 264 | goto dput_and_out; |
| 265 | |
| 266 | error = locks_verify_truncate(inode, NULL, length); |
| 267 | if (!error) { |
| 268 | DQUOT_INIT(inode); |
Miklos Szeredi | cc4e69d | 2005-11-07 00:59:49 -0800 | [diff] [blame] | 269 | error = do_truncate(nd.dentry, length, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
| 271 | put_write_access(inode); |
| 272 | |
| 273 | dput_and_out: |
| 274 | path_release(&nd); |
| 275 | out: |
| 276 | return error; |
| 277 | } |
| 278 | |
| 279 | asmlinkage long sys_truncate(const char __user * path, unsigned long length) |
| 280 | { |
| 281 | /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */ |
| 282 | return do_sys_truncate(path, (long)length); |
| 283 | } |
| 284 | |
| 285 | static inline long do_sys_ftruncate(unsigned int fd, loff_t length, int small) |
| 286 | { |
| 287 | struct inode * inode; |
| 288 | struct dentry *dentry; |
| 289 | struct file * file; |
| 290 | int error; |
| 291 | |
| 292 | error = -EINVAL; |
| 293 | if (length < 0) |
| 294 | goto out; |
| 295 | error = -EBADF; |
| 296 | file = fget(fd); |
| 297 | if (!file) |
| 298 | goto out; |
| 299 | |
| 300 | /* explicitly opened as large or we are on 64-bit box */ |
| 301 | if (file->f_flags & O_LARGEFILE) |
| 302 | small = 0; |
| 303 | |
| 304 | dentry = file->f_dentry; |
| 305 | inode = dentry->d_inode; |
| 306 | error = -EINVAL; |
| 307 | if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE)) |
| 308 | goto out_putf; |
| 309 | |
| 310 | error = -EINVAL; |
| 311 | /* Cannot ftruncate over 2^31 bytes without large file support */ |
| 312 | if (small && length > MAX_NON_LFS) |
| 313 | goto out_putf; |
| 314 | |
| 315 | error = -EPERM; |
| 316 | if (IS_APPEND(inode)) |
| 317 | goto out_putf; |
| 318 | |
| 319 | error = locks_verify_truncate(inode, file, length); |
| 320 | if (!error) |
Miklos Szeredi | cc4e69d | 2005-11-07 00:59:49 -0800 | [diff] [blame] | 321 | error = do_truncate(dentry, length, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | out_putf: |
| 323 | fput(file); |
| 324 | out: |
| 325 | return error; |
| 326 | } |
| 327 | |
| 328 | asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length) |
| 329 | { |
| 330 | return do_sys_ftruncate(fd, length, 1); |
| 331 | } |
| 332 | |
| 333 | /* LFS versions of truncate are only needed on 32 bit machines */ |
| 334 | #if BITS_PER_LONG == 32 |
| 335 | asmlinkage long sys_truncate64(const char __user * path, loff_t length) |
| 336 | { |
| 337 | return do_sys_truncate(path, length); |
| 338 | } |
| 339 | |
| 340 | asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length) |
| 341 | { |
| 342 | return do_sys_ftruncate(fd, length, 0); |
| 343 | } |
| 344 | #endif |
| 345 | |
| 346 | #ifdef __ARCH_WANT_SYS_UTIME |
| 347 | |
| 348 | /* |
| 349 | * sys_utime() can be implemented in user-level using sys_utimes(). |
| 350 | * Is this for backwards compatibility? If so, why not move it |
| 351 | * into the appropriate arch directory (for those architectures that |
| 352 | * need it). |
| 353 | */ |
| 354 | |
| 355 | /* If times==NULL, set access and modification to current time, |
| 356 | * must be owner or have write permission. |
| 357 | * Else, update from *times, must be owner or super user. |
| 358 | */ |
| 359 | asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times) |
| 360 | { |
| 361 | int error; |
| 362 | struct nameidata nd; |
| 363 | struct inode * inode; |
| 364 | struct iattr newattrs; |
| 365 | |
| 366 | error = user_path_walk(filename, &nd); |
| 367 | if (error) |
| 368 | goto out; |
| 369 | inode = nd.dentry->d_inode; |
| 370 | |
| 371 | error = -EROFS; |
| 372 | if (IS_RDONLY(inode)) |
| 373 | goto dput_and_out; |
| 374 | |
| 375 | /* Don't worry, the checks are done in inode_change_ok() */ |
| 376 | newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME; |
| 377 | if (times) { |
| 378 | error = -EPERM; |
| 379 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 380 | goto dput_and_out; |
| 381 | |
| 382 | error = get_user(newattrs.ia_atime.tv_sec, ×->actime); |
| 383 | newattrs.ia_atime.tv_nsec = 0; |
| 384 | if (!error) |
| 385 | error = get_user(newattrs.ia_mtime.tv_sec, ×->modtime); |
| 386 | newattrs.ia_mtime.tv_nsec = 0; |
| 387 | if (error) |
| 388 | goto dput_and_out; |
| 389 | |
| 390 | newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET; |
| 391 | } else { |
| 392 | error = -EACCES; |
| 393 | if (IS_IMMUTABLE(inode)) |
| 394 | goto dput_and_out; |
| 395 | |
| 396 | if (current->fsuid != inode->i_uid && |
Christoph Hellwig | e4543ed | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 397 | (error = vfs_permission(&nd, MAY_WRITE)) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | goto dput_and_out; |
| 399 | } |
| 400 | down(&inode->i_sem); |
| 401 | error = notify_change(nd.dentry, &newattrs); |
| 402 | up(&inode->i_sem); |
| 403 | dput_and_out: |
| 404 | path_release(&nd); |
| 405 | out: |
| 406 | return error; |
| 407 | } |
| 408 | |
| 409 | #endif |
| 410 | |
| 411 | /* If times==NULL, set access and modification to current time, |
| 412 | * must be owner or have write permission. |
| 413 | * Else, update from *times, must be owner or super user. |
| 414 | */ |
| 415 | long do_utimes(char __user * filename, struct timeval * times) |
| 416 | { |
| 417 | int error; |
| 418 | struct nameidata nd; |
| 419 | struct inode * inode; |
| 420 | struct iattr newattrs; |
| 421 | |
| 422 | error = user_path_walk(filename, &nd); |
| 423 | |
| 424 | if (error) |
| 425 | goto out; |
| 426 | inode = nd.dentry->d_inode; |
| 427 | |
| 428 | error = -EROFS; |
| 429 | if (IS_RDONLY(inode)) |
| 430 | goto dput_and_out; |
| 431 | |
| 432 | /* Don't worry, the checks are done in inode_change_ok() */ |
| 433 | newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME; |
| 434 | if (times) { |
| 435 | error = -EPERM; |
| 436 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 437 | goto dput_and_out; |
| 438 | |
| 439 | newattrs.ia_atime.tv_sec = times[0].tv_sec; |
| 440 | newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000; |
| 441 | newattrs.ia_mtime.tv_sec = times[1].tv_sec; |
| 442 | newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000; |
| 443 | newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET; |
| 444 | } else { |
| 445 | error = -EACCES; |
| 446 | if (IS_IMMUTABLE(inode)) |
| 447 | goto dput_and_out; |
| 448 | |
| 449 | if (current->fsuid != inode->i_uid && |
Christoph Hellwig | e4543ed | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 450 | (error = vfs_permission(&nd, MAY_WRITE)) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | goto dput_and_out; |
| 452 | } |
| 453 | down(&inode->i_sem); |
| 454 | error = notify_change(nd.dentry, &newattrs); |
| 455 | up(&inode->i_sem); |
| 456 | dput_and_out: |
| 457 | path_release(&nd); |
| 458 | out: |
| 459 | return error; |
| 460 | } |
| 461 | |
| 462 | asmlinkage long sys_utimes(char __user * filename, struct timeval __user * utimes) |
| 463 | { |
| 464 | struct timeval times[2]; |
| 465 | |
| 466 | if (utimes && copy_from_user(×, utimes, sizeof(times))) |
| 467 | return -EFAULT; |
| 468 | return do_utimes(filename, utimes ? times : NULL); |
| 469 | } |
| 470 | |
| 471 | |
| 472 | /* |
| 473 | * access() needs to use the real uid/gid, not the effective uid/gid. |
| 474 | * We do this by temporarily clearing all FS-related capabilities and |
| 475 | * switching the fsuid/fsgid around to the real ones. |
| 476 | */ |
| 477 | asmlinkage long sys_access(const char __user * filename, int mode) |
| 478 | { |
| 479 | struct nameidata nd; |
| 480 | int old_fsuid, old_fsgid; |
| 481 | kernel_cap_t old_cap; |
| 482 | int res; |
| 483 | |
| 484 | if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */ |
| 485 | return -EINVAL; |
| 486 | |
| 487 | old_fsuid = current->fsuid; |
| 488 | old_fsgid = current->fsgid; |
| 489 | old_cap = current->cap_effective; |
| 490 | |
| 491 | current->fsuid = current->uid; |
| 492 | current->fsgid = current->gid; |
| 493 | |
| 494 | /* |
| 495 | * Clear the capabilities if we switch to a non-root user |
| 496 | * |
| 497 | * FIXME: There is a race here against sys_capset. The |
| 498 | * capabilities can change yet we will restore the old |
| 499 | * value below. We should hold task_capabilities_lock, |
| 500 | * but we cannot because user_path_walk can sleep. |
| 501 | */ |
| 502 | if (current->uid) |
| 503 | cap_clear(current->cap_effective); |
| 504 | else |
| 505 | current->cap_effective = current->cap_permitted; |
| 506 | |
| 507 | res = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd); |
| 508 | if (!res) { |
Christoph Hellwig | e4543ed | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 509 | res = vfs_permission(&nd, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | /* SuS v2 requires we report a read only fs too */ |
| 511 | if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode) |
| 512 | && !special_file(nd.dentry->d_inode->i_mode)) |
| 513 | res = -EROFS; |
| 514 | path_release(&nd); |
| 515 | } |
| 516 | |
| 517 | current->fsuid = old_fsuid; |
| 518 | current->fsgid = old_fsgid; |
| 519 | current->cap_effective = old_cap; |
| 520 | |
| 521 | return res; |
| 522 | } |
| 523 | |
| 524 | asmlinkage long sys_chdir(const char __user * filename) |
| 525 | { |
| 526 | struct nameidata nd; |
| 527 | int error; |
| 528 | |
| 529 | error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd); |
| 530 | if (error) |
| 531 | goto out; |
| 532 | |
Christoph Hellwig | e4543ed | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 533 | error = vfs_permission(&nd, MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | if (error) |
| 535 | goto dput_and_out; |
| 536 | |
| 537 | set_fs_pwd(current->fs, nd.mnt, nd.dentry); |
| 538 | |
| 539 | dput_and_out: |
| 540 | path_release(&nd); |
| 541 | out: |
| 542 | return error; |
| 543 | } |
| 544 | |
| 545 | asmlinkage long sys_fchdir(unsigned int fd) |
| 546 | { |
| 547 | struct file *file; |
| 548 | struct dentry *dentry; |
| 549 | struct inode *inode; |
| 550 | struct vfsmount *mnt; |
| 551 | int error; |
| 552 | |
| 553 | error = -EBADF; |
| 554 | file = fget(fd); |
| 555 | if (!file) |
| 556 | goto out; |
| 557 | |
| 558 | dentry = file->f_dentry; |
| 559 | mnt = file->f_vfsmnt; |
| 560 | inode = dentry->d_inode; |
| 561 | |
| 562 | error = -ENOTDIR; |
| 563 | if (!S_ISDIR(inode->i_mode)) |
| 564 | goto out_putf; |
| 565 | |
Christoph Hellwig | 8c744fb | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 566 | error = file_permission(file, MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | if (!error) |
| 568 | set_fs_pwd(current->fs, mnt, dentry); |
| 569 | out_putf: |
| 570 | fput(file); |
| 571 | out: |
| 572 | return error; |
| 573 | } |
| 574 | |
| 575 | asmlinkage long sys_chroot(const char __user * filename) |
| 576 | { |
| 577 | struct nameidata nd; |
| 578 | int error; |
| 579 | |
| 580 | error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd); |
| 581 | if (error) |
| 582 | goto out; |
| 583 | |
Christoph Hellwig | e4543ed | 2005-11-08 21:35:04 -0800 | [diff] [blame] | 584 | error = vfs_permission(&nd, MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | if (error) |
| 586 | goto dput_and_out; |
| 587 | |
| 588 | error = -EPERM; |
| 589 | if (!capable(CAP_SYS_CHROOT)) |
| 590 | goto dput_and_out; |
| 591 | |
| 592 | set_fs_root(current->fs, nd.mnt, nd.dentry); |
| 593 | set_fs_altroot(); |
| 594 | error = 0; |
| 595 | dput_and_out: |
| 596 | path_release(&nd); |
| 597 | out: |
| 598 | return error; |
| 599 | } |
| 600 | |
| 601 | asmlinkage long sys_fchmod(unsigned int fd, mode_t mode) |
| 602 | { |
| 603 | struct inode * inode; |
| 604 | struct dentry * dentry; |
| 605 | struct file * file; |
| 606 | int err = -EBADF; |
| 607 | struct iattr newattrs; |
| 608 | |
| 609 | file = fget(fd); |
| 610 | if (!file) |
| 611 | goto out; |
| 612 | |
| 613 | dentry = file->f_dentry; |
| 614 | inode = dentry->d_inode; |
| 615 | |
| 616 | err = -EROFS; |
| 617 | if (IS_RDONLY(inode)) |
| 618 | goto out_putf; |
| 619 | err = -EPERM; |
| 620 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 621 | goto out_putf; |
| 622 | down(&inode->i_sem); |
| 623 | if (mode == (mode_t) -1) |
| 624 | mode = inode->i_mode; |
| 625 | newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO); |
| 626 | newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; |
| 627 | err = notify_change(dentry, &newattrs); |
| 628 | up(&inode->i_sem); |
| 629 | |
| 630 | out_putf: |
| 631 | fput(file); |
| 632 | out: |
| 633 | return err; |
| 634 | } |
| 635 | |
| 636 | asmlinkage long sys_chmod(const char __user * filename, mode_t mode) |
| 637 | { |
| 638 | struct nameidata nd; |
| 639 | struct inode * inode; |
| 640 | int error; |
| 641 | struct iattr newattrs; |
| 642 | |
| 643 | error = user_path_walk(filename, &nd); |
| 644 | if (error) |
| 645 | goto out; |
| 646 | inode = nd.dentry->d_inode; |
| 647 | |
| 648 | error = -EROFS; |
| 649 | if (IS_RDONLY(inode)) |
| 650 | goto dput_and_out; |
| 651 | |
| 652 | error = -EPERM; |
| 653 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 654 | goto dput_and_out; |
| 655 | |
| 656 | down(&inode->i_sem); |
| 657 | if (mode == (mode_t) -1) |
| 658 | mode = inode->i_mode; |
| 659 | newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO); |
| 660 | newattrs.ia_valid = ATTR_MODE | ATTR_CTIME; |
| 661 | error = notify_change(nd.dentry, &newattrs); |
| 662 | up(&inode->i_sem); |
| 663 | |
| 664 | dput_and_out: |
| 665 | path_release(&nd); |
| 666 | out: |
| 667 | return error; |
| 668 | } |
| 669 | |
| 670 | static int chown_common(struct dentry * dentry, uid_t user, gid_t group) |
| 671 | { |
| 672 | struct inode * inode; |
| 673 | int error; |
| 674 | struct iattr newattrs; |
| 675 | |
| 676 | error = -ENOENT; |
| 677 | if (!(inode = dentry->d_inode)) { |
| 678 | printk(KERN_ERR "chown_common: NULL inode\n"); |
| 679 | goto out; |
| 680 | } |
| 681 | error = -EROFS; |
| 682 | if (IS_RDONLY(inode)) |
| 683 | goto out; |
| 684 | error = -EPERM; |
| 685 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) |
| 686 | goto out; |
| 687 | newattrs.ia_valid = ATTR_CTIME; |
| 688 | if (user != (uid_t) -1) { |
| 689 | newattrs.ia_valid |= ATTR_UID; |
| 690 | newattrs.ia_uid = user; |
| 691 | } |
| 692 | if (group != (gid_t) -1) { |
| 693 | newattrs.ia_valid |= ATTR_GID; |
| 694 | newattrs.ia_gid = group; |
| 695 | } |
| 696 | if (!S_ISDIR(inode->i_mode)) |
| 697 | newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID; |
| 698 | down(&inode->i_sem); |
| 699 | error = notify_change(dentry, &newattrs); |
| 700 | up(&inode->i_sem); |
| 701 | out: |
| 702 | return error; |
| 703 | } |
| 704 | |
| 705 | asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group) |
| 706 | { |
| 707 | struct nameidata nd; |
| 708 | int error; |
| 709 | |
| 710 | error = user_path_walk(filename, &nd); |
| 711 | if (!error) { |
| 712 | error = chown_common(nd.dentry, user, group); |
| 713 | path_release(&nd); |
| 714 | } |
| 715 | return error; |
| 716 | } |
| 717 | |
| 718 | asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group) |
| 719 | { |
| 720 | struct nameidata nd; |
| 721 | int error; |
| 722 | |
| 723 | error = user_path_walk_link(filename, &nd); |
| 724 | if (!error) { |
| 725 | error = chown_common(nd.dentry, user, group); |
| 726 | path_release(&nd); |
| 727 | } |
| 728 | return error; |
| 729 | } |
| 730 | |
| 731 | |
| 732 | asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group) |
| 733 | { |
| 734 | struct file * file; |
| 735 | int error = -EBADF; |
| 736 | |
| 737 | file = fget(fd); |
| 738 | if (file) { |
| 739 | error = chown_common(file->f_dentry, user, group); |
| 740 | fput(file); |
| 741 | } |
| 742 | return error; |
| 743 | } |
| 744 | |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 745 | static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt, |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 746 | int flags, struct file *f, |
| 747 | int (*open)(struct inode *, struct file *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | struct inode *inode; |
| 750 | int error; |
| 751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | f->f_flags = flags; |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 753 | f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK | |
| 754 | FMODE_PREAD | FMODE_PWRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | inode = dentry->d_inode; |
| 756 | if (f->f_mode & FMODE_WRITE) { |
| 757 | error = get_write_access(inode); |
| 758 | if (error) |
| 759 | goto cleanup_file; |
| 760 | } |
| 761 | |
| 762 | f->f_mapping = inode->i_mapping; |
| 763 | f->f_dentry = dentry; |
| 764 | f->f_vfsmnt = mnt; |
| 765 | f->f_pos = 0; |
| 766 | f->f_op = fops_get(inode->i_fop); |
| 767 | file_move(f, &inode->i_sb->s_files); |
| 768 | |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 769 | if (!open && f->f_op) |
| 770 | open = f->f_op->open; |
| 771 | if (open) { |
| 772 | error = open(inode, f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | if (error) |
| 774 | goto cleanup_all; |
| 775 | } |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); |
| 778 | |
| 779 | file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping); |
| 780 | |
| 781 | /* NB: we're sure to have correct a_ops only after f_op->open */ |
| 782 | if (f->f_flags & O_DIRECT) { |
Carsten Otte | ceffc07 | 2005-06-23 22:05:25 -0700 | [diff] [blame] | 783 | if (!f->f_mapping->a_ops || |
| 784 | ((!f->f_mapping->a_ops->direct_IO) && |
| 785 | (!f->f_mapping->a_ops->get_xip_page))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | fput(f); |
| 787 | f = ERR_PTR(-EINVAL); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | return f; |
| 792 | |
| 793 | cleanup_all: |
| 794 | fops_put(f->f_op); |
| 795 | if (f->f_mode & FMODE_WRITE) |
| 796 | put_write_access(inode); |
| 797 | file_kill(f); |
| 798 | f->f_dentry = NULL; |
| 799 | f->f_vfsmnt = NULL; |
| 800 | cleanup_file: |
| 801 | put_filp(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | dput(dentry); |
| 803 | mntput(mnt); |
| 804 | return ERR_PTR(error); |
| 805 | } |
| 806 | |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 807 | /* |
| 808 | * Note that while the flag value (low two bits) for sys_open means: |
| 809 | * 00 - read-only |
| 810 | * 01 - write-only |
| 811 | * 10 - read-write |
| 812 | * 11 - special |
| 813 | * it is changed into |
| 814 | * 00 - no permissions needed |
| 815 | * 01 - read-permission |
| 816 | * 10 - write-permission |
| 817 | * 11 - read-write |
| 818 | * for the internal routines (ie open_namei()/follow_link() etc). 00 is |
| 819 | * used by symlinks. |
| 820 | */ |
| 821 | struct file *filp_open(const char * filename, int flags, int mode) |
| 822 | { |
| 823 | int namei_flags, error; |
| 824 | struct nameidata nd; |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 825 | |
| 826 | namei_flags = flags; |
| 827 | if ((namei_flags+1) & O_ACCMODE) |
| 828 | namei_flags++; |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 829 | |
| 830 | error = open_namei(filename, namei_flags, mode, &nd); |
| 831 | if (!error) |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 832 | return nameidata_to_filp(&nd, flags); |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 833 | |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 834 | return ERR_PTR(error); |
| 835 | } |
| 836 | EXPORT_SYMBOL(filp_open); |
| 837 | |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 838 | /** |
| 839 | * lookup_instantiate_filp - instantiates the open intent filp |
| 840 | * @nd: pointer to nameidata |
| 841 | * @dentry: pointer to dentry |
| 842 | * @open: open callback |
| 843 | * |
| 844 | * Helper for filesystems that want to use lookup open intents and pass back |
| 845 | * a fully instantiated struct file to the caller. |
| 846 | * This function is meant to be called from within a filesystem's |
| 847 | * lookup method. |
| 848 | * Note that in case of error, nd->intent.open.file is destroyed, but the |
| 849 | * path information remains valid. |
| 850 | * If the open callback is set to NULL, then the standard f_op->open() |
| 851 | * filesystem callback is substituted. |
| 852 | */ |
| 853 | struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry, |
| 854 | int (*open)(struct inode *, struct file *)) |
| 855 | { |
| 856 | if (IS_ERR(nd->intent.open.file)) |
| 857 | goto out; |
| 858 | if (IS_ERR(dentry)) |
| 859 | goto out_err; |
| 860 | nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->mnt), |
| 861 | nd->intent.open.flags - 1, |
| 862 | nd->intent.open.file, |
| 863 | open); |
| 864 | out: |
| 865 | return nd->intent.open.file; |
| 866 | out_err: |
| 867 | release_open_intent(nd); |
| 868 | nd->intent.open.file = (struct file *)dentry; |
| 869 | goto out; |
| 870 | } |
| 871 | EXPORT_SYMBOL_GPL(lookup_instantiate_filp); |
| 872 | |
| 873 | /** |
| 874 | * nameidata_to_filp - convert a nameidata to an open filp. |
| 875 | * @nd: pointer to nameidata |
| 876 | * @flags: open flags |
| 877 | * |
| 878 | * Note that this function destroys the original nameidata |
| 879 | */ |
| 880 | struct file *nameidata_to_filp(struct nameidata *nd, int flags) |
| 881 | { |
| 882 | struct file *filp; |
| 883 | |
| 884 | /* Pick up the filp from the open intent */ |
| 885 | filp = nd->intent.open.file; |
| 886 | /* Has the filesystem initialised the file for us? */ |
| 887 | if (filp->f_dentry == NULL) |
| 888 | filp = __dentry_open(nd->dentry, nd->mnt, flags, filp, NULL); |
| 889 | else |
| 890 | path_release(nd); |
| 891 | return filp; |
| 892 | } |
| 893 | |
Peter Staubach | 6fdcc21 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 894 | /* |
| 895 | * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an |
| 896 | * error. |
| 897 | */ |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 898 | struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags) |
| 899 | { |
| 900 | int error; |
| 901 | struct file *f; |
| 902 | |
| 903 | error = -ENFILE; |
| 904 | f = get_empty_filp(); |
Peter Staubach | 6fdcc21 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 905 | if (f == NULL) { |
| 906 | dput(dentry); |
| 907 | mntput(mnt); |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 908 | return ERR_PTR(error); |
Peter Staubach | 6fdcc21 | 2005-11-07 00:59:42 -0800 | [diff] [blame] | 909 | } |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 910 | |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 911 | return __dentry_open(dentry, mnt, flags, f, NULL); |
Peter Staubach | a1a5b3d | 2005-09-13 01:25:12 -0700 | [diff] [blame] | 912 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | EXPORT_SYMBOL(dentry_open); |
| 914 | |
| 915 | /* |
| 916 | * Find an empty file descriptor entry, and mark it busy. |
| 917 | */ |
| 918 | int get_unused_fd(void) |
| 919 | { |
| 920 | struct files_struct * files = current->files; |
| 921 | int fd, error; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 922 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
| 924 | error = -EMFILE; |
| 925 | spin_lock(&files->file_lock); |
| 926 | |
| 927 | repeat: |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 928 | fdt = files_fdtable(files); |
| 929 | fd = find_next_zero_bit(fdt->open_fds->fds_bits, |
| 930 | fdt->max_fdset, |
| 931 | fdt->next_fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | |
| 933 | /* |
| 934 | * N.B. For clone tasks sharing a files structure, this test |
| 935 | * will limit the total number of files that can be opened. |
| 936 | */ |
| 937 | if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur) |
| 938 | goto out; |
| 939 | |
| 940 | /* Do we need to expand the fd array or fd set? */ |
| 941 | error = expand_files(files, fd); |
| 942 | if (error < 0) |
| 943 | goto out; |
| 944 | |
| 945 | if (error) { |
| 946 | /* |
| 947 | * If we needed to expand the fs array we |
| 948 | * might have blocked - try again. |
| 949 | */ |
| 950 | error = -EMFILE; |
| 951 | goto repeat; |
| 952 | } |
| 953 | |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 954 | FD_SET(fd, fdt->open_fds); |
| 955 | FD_CLR(fd, fdt->close_on_exec); |
| 956 | fdt->next_fd = fd + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | #if 1 |
| 958 | /* Sanity check */ |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 959 | if (fdt->fd[fd] != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 961 | fdt->fd[fd] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | } |
| 963 | #endif |
| 964 | error = fd; |
| 965 | |
| 966 | out: |
| 967 | spin_unlock(&files->file_lock); |
| 968 | return error; |
| 969 | } |
| 970 | |
| 971 | EXPORT_SYMBOL(get_unused_fd); |
| 972 | |
| 973 | static inline void __put_unused_fd(struct files_struct *files, unsigned int fd) |
| 974 | { |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 975 | struct fdtable *fdt = files_fdtable(files); |
| 976 | __FD_CLR(fd, fdt->open_fds); |
| 977 | if (fd < fdt->next_fd) |
| 978 | fdt->next_fd = fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | void fastcall put_unused_fd(unsigned int fd) |
| 982 | { |
| 983 | struct files_struct *files = current->files; |
| 984 | spin_lock(&files->file_lock); |
| 985 | __put_unused_fd(files, fd); |
| 986 | spin_unlock(&files->file_lock); |
| 987 | } |
| 988 | |
| 989 | EXPORT_SYMBOL(put_unused_fd); |
| 990 | |
| 991 | /* |
| 992 | * Install a file pointer in the fd array. |
| 993 | * |
| 994 | * The VFS is full of places where we drop the files lock between |
| 995 | * setting the open_fds bitmap and installing the file in the file |
| 996 | * array. At any such point, we are vulnerable to a dup2() race |
| 997 | * installing a file in the array before us. We need to detect this and |
| 998 | * fput() the struct file we are about to overwrite in this case. |
| 999 | * |
| 1000 | * It should never happen - if we allow dup2() do it, _really_ bad things |
| 1001 | * will follow. |
| 1002 | */ |
| 1003 | |
| 1004 | void fastcall fd_install(unsigned int fd, struct file * file) |
| 1005 | { |
| 1006 | struct files_struct *files = current->files; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1007 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | spin_lock(&files->file_lock); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1009 | fdt = files_fdtable(files); |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 1010 | BUG_ON(fdt->fd[fd] != NULL); |
| 1011 | rcu_assign_pointer(fdt->fd[fd], file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | spin_unlock(&files->file_lock); |
| 1013 | } |
| 1014 | |
| 1015 | EXPORT_SYMBOL(fd_install); |
| 1016 | |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1017 | long do_sys_open(const char __user *filename, int flags, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | { |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1019 | char *tmp = getname(filename); |
| 1020 | int fd = PTR_ERR(tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | if (!IS_ERR(tmp)) { |
| 1023 | fd = get_unused_fd(); |
| 1024 | if (fd >= 0) { |
| 1025 | struct file *f = filp_open(tmp, flags, mode); |
Telemaque Ndizihiwe | fed2fc1 | 2005-06-23 00:10:33 -0700 | [diff] [blame] | 1026 | if (IS_ERR(f)) { |
| 1027 | put_unused_fd(fd); |
| 1028 | fd = PTR_ERR(f); |
| 1029 | } else { |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 1030 | fsnotify_open(f->f_dentry); |
Telemaque Ndizihiwe | fed2fc1 | 2005-06-23 00:10:33 -0700 | [diff] [blame] | 1031 | fd_install(fd, f); |
| 1032 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | putname(tmp); |
| 1035 | } |
| 1036 | return fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | } |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1038 | |
| 1039 | asmlinkage long sys_open(const char __user *filename, int flags, int mode) |
| 1040 | { |
| 1041 | if (force_o_largefile()) |
| 1042 | flags |= O_LARGEFILE; |
| 1043 | |
| 1044 | return do_sys_open(filename, flags, mode); |
| 1045 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | EXPORT_SYMBOL_GPL(sys_open); |
| 1047 | |
| 1048 | #ifndef __alpha__ |
| 1049 | |
| 1050 | /* |
| 1051 | * For backward compatibility? Maybe this should be moved |
| 1052 | * into arch/i386 instead? |
| 1053 | */ |
| 1054 | asmlinkage long sys_creat(const char __user * pathname, int mode) |
| 1055 | { |
| 1056 | return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode); |
| 1057 | } |
| 1058 | |
| 1059 | #endif |
| 1060 | |
| 1061 | /* |
| 1062 | * "id" is the POSIX thread ID. We use the |
| 1063 | * files pointer for this.. |
| 1064 | */ |
| 1065 | int filp_close(struct file *filp, fl_owner_t id) |
| 1066 | { |
Christoph Lameter | 45778ca | 2005-06-23 00:10:17 -0700 | [diff] [blame] | 1067 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | |
| 1069 | if (!file_count(filp)) { |
| 1070 | printk(KERN_ERR "VFS: Close: file count is 0\n"); |
Christoph Lameter | 45778ca | 2005-06-23 00:10:17 -0700 | [diff] [blame] | 1071 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Christoph Lameter | 45778ca | 2005-06-23 00:10:17 -0700 | [diff] [blame] | 1074 | if (filp->f_op && filp->f_op->flush) |
| 1075 | retval = filp->f_op->flush(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | |
| 1077 | dnotify_flush(filp, id); |
| 1078 | locks_remove_posix(filp, id); |
| 1079 | fput(filp); |
| 1080 | return retval; |
| 1081 | } |
| 1082 | |
| 1083 | EXPORT_SYMBOL(filp_close); |
| 1084 | |
| 1085 | /* |
| 1086 | * Careful here! We test whether the file pointer is NULL before |
| 1087 | * releasing the fd. This ensures that one clone task can't release |
| 1088 | * an fd while another clone is opening it. |
| 1089 | */ |
| 1090 | asmlinkage long sys_close(unsigned int fd) |
| 1091 | { |
| 1092 | struct file * filp; |
| 1093 | struct files_struct *files = current->files; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1094 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | |
| 1096 | spin_lock(&files->file_lock); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1097 | fdt = files_fdtable(files); |
| 1098 | if (fd >= fdt->max_fds) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | goto out_unlock; |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1100 | filp = fdt->fd[fd]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | if (!filp) |
| 1102 | goto out_unlock; |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 1103 | rcu_assign_pointer(fdt->fd[fd], NULL); |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 1104 | FD_CLR(fd, fdt->close_on_exec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | __put_unused_fd(files, fd); |
| 1106 | spin_unlock(&files->file_lock); |
| 1107 | return filp_close(filp, files); |
| 1108 | |
| 1109 | out_unlock: |
| 1110 | spin_unlock(&files->file_lock); |
| 1111 | return -EBADF; |
| 1112 | } |
| 1113 | |
| 1114 | EXPORT_SYMBOL(sys_close); |
| 1115 | |
| 1116 | /* |
| 1117 | * This routine simulates a hangup on the tty, to arrange that users |
| 1118 | * are given clean terminals at login time. |
| 1119 | */ |
| 1120 | asmlinkage long sys_vhangup(void) |
| 1121 | { |
| 1122 | if (capable(CAP_SYS_TTY_CONFIG)) { |
| 1123 | tty_vhangup(current->signal->tty); |
| 1124 | return 0; |
| 1125 | } |
| 1126 | return -EPERM; |
| 1127 | } |
| 1128 | |
| 1129 | /* |
| 1130 | * Called when an inode is about to be open. |
| 1131 | * We use this to disallow opening large files on 32bit systems if |
| 1132 | * the caller didn't specify O_LARGEFILE. On 64bit systems we force |
| 1133 | * on this flag in sys_open. |
| 1134 | */ |
| 1135 | int generic_file_open(struct inode * inode, struct file * filp) |
| 1136 | { |
| 1137 | if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS) |
| 1138 | return -EFBIG; |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
| 1142 | EXPORT_SYMBOL(generic_file_open); |
| 1143 | |
| 1144 | /* |
| 1145 | * This is used by subsystems that don't want seekable |
| 1146 | * file descriptors |
| 1147 | */ |
| 1148 | int nonseekable_open(struct inode *inode, struct file *filp) |
| 1149 | { |
| 1150 | filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE); |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | EXPORT_SYMBOL(nonseekable_open); |