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