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