David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
| 3 | * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved. |
| 4 | * |
| 5 | * This copyrighted material is made available to anyone wishing to use, |
| 6 | * modify, copy, or redistribute it subject to the terms and conditions |
| 7 | * of the GNU General Public License v.2. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/completion.h> |
| 14 | #include <linux/buffer_head.h> |
| 15 | #include <linux/pagemap.h> |
| 16 | #include <linux/uio.h> |
| 17 | #include <linux/blkdev.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/smp_lock.h> |
| 20 | #include <linux/gfs2_ioctl.h> |
| 21 | #include <asm/semaphore.h> |
| 22 | #include <asm/uaccess.h> |
| 23 | |
| 24 | #include "gfs2.h" |
| 25 | #include "bmap.h" |
| 26 | #include "dir.h" |
| 27 | #include "glock.h" |
| 28 | #include "glops.h" |
| 29 | #include "inode.h" |
| 30 | #include "jdata.h" |
| 31 | #include "lm.h" |
| 32 | #include "log.h" |
| 33 | #include "meta_io.h" |
| 34 | #include "ops_file.h" |
| 35 | #include "ops_vm.h" |
| 36 | #include "quota.h" |
| 37 | #include "rgrp.h" |
| 38 | #include "trans.h" |
| 39 | |
| 40 | /* "bad" is for NFS support */ |
| 41 | struct filldir_bad_entry { |
| 42 | char *fbe_name; |
| 43 | unsigned int fbe_length; |
| 44 | uint64_t fbe_offset; |
| 45 | struct gfs2_inum fbe_inum; |
| 46 | unsigned int fbe_type; |
| 47 | }; |
| 48 | |
| 49 | struct filldir_bad { |
| 50 | struct gfs2_sbd *fdb_sbd; |
| 51 | |
| 52 | struct filldir_bad_entry *fdb_entry; |
| 53 | unsigned int fdb_entry_num; |
| 54 | unsigned int fdb_entry_off; |
| 55 | |
| 56 | char *fdb_name; |
| 57 | unsigned int fdb_name_size; |
| 58 | unsigned int fdb_name_off; |
| 59 | }; |
| 60 | |
| 61 | /* For regular, non-NFS */ |
| 62 | struct filldir_reg { |
| 63 | struct gfs2_sbd *fdr_sbd; |
| 64 | int fdr_prefetch; |
| 65 | |
| 66 | filldir_t fdr_filldir; |
| 67 | void *fdr_opaque; |
| 68 | }; |
| 69 | |
| 70 | typedef ssize_t(*do_rw_t) (struct file *file, |
| 71 | char __user *buf, |
| 72 | size_t size, loff_t *offset, |
| 73 | unsigned int num_gh, struct gfs2_holder *ghs); |
| 74 | |
| 75 | /** |
| 76 | * gfs2_llseek - seek to a location in a file |
| 77 | * @file: the file |
| 78 | * @offset: the offset |
| 79 | * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END) |
| 80 | * |
| 81 | * SEEK_END requires the glock for the file because it references the |
| 82 | * file's size. |
| 83 | * |
| 84 | * Returns: The new offset, or errno |
| 85 | */ |
| 86 | |
| 87 | static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin) |
| 88 | { |
| 89 | struct gfs2_inode *ip = get_v2ip(file->f_mapping->host); |
| 90 | struct gfs2_holder i_gh; |
| 91 | loff_t error; |
| 92 | |
| 93 | atomic_inc(&ip->i_sbd->sd_ops_file); |
| 94 | |
| 95 | if (origin == 2) { |
| 96 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, |
| 97 | &i_gh); |
| 98 | if (!error) { |
| 99 | error = remote_llseek(file, offset, origin); |
| 100 | gfs2_glock_dq_uninit(&i_gh); |
| 101 | } |
| 102 | } else |
| 103 | error = remote_llseek(file, offset, origin); |
| 104 | |
| 105 | return error; |
| 106 | } |
| 107 | |
| 108 | static inline unsigned int vma2state(struct vm_area_struct *vma) |
| 109 | { |
| 110 | if ((vma->vm_flags & (VM_MAYWRITE | VM_MAYSHARE)) == |
| 111 | (VM_MAYWRITE | VM_MAYSHARE)) |
| 112 | return LM_ST_EXCLUSIVE; |
| 113 | return LM_ST_SHARED; |
| 114 | } |
| 115 | |
| 116 | static ssize_t walk_vm_hard(struct file *file, const char __user *buf, size_t size, |
| 117 | loff_t *offset, do_rw_t operation) |
| 118 | { |
| 119 | struct gfs2_holder *ghs; |
| 120 | unsigned int num_gh = 0; |
| 121 | ssize_t count; |
| 122 | struct super_block *sb = file->f_dentry->d_inode->i_sb; |
| 123 | struct mm_struct *mm = current->mm; |
| 124 | struct vm_area_struct *vma; |
| 125 | unsigned long start = (unsigned long)buf; |
| 126 | unsigned long end = start + size; |
| 127 | int dumping = (current->flags & PF_DUMPCORE); |
| 128 | unsigned int x = 0; |
| 129 | |
| 130 | for (vma = find_vma(mm, start); vma; vma = vma->vm_next) { |
| 131 | if (end <= vma->vm_start) |
| 132 | break; |
| 133 | if (vma->vm_file && |
| 134 | vma->vm_file->f_dentry->d_inode->i_sb == sb) { |
| 135 | num_gh++; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | ghs = kcalloc((num_gh + 1), sizeof(struct gfs2_holder), GFP_KERNEL); |
| 140 | if (!ghs) { |
| 141 | if (!dumping) |
| 142 | up_read(&mm->mmap_sem); |
| 143 | return -ENOMEM; |
| 144 | } |
| 145 | |
| 146 | for (vma = find_vma(mm, start); vma; vma = vma->vm_next) { |
| 147 | if (end <= vma->vm_start) |
| 148 | break; |
| 149 | if (vma->vm_file) { |
| 150 | struct inode *inode = vma->vm_file->f_dentry->d_inode; |
| 151 | if (inode->i_sb == sb) |
| 152 | gfs2_holder_init(get_v2ip(inode)->i_gl, |
| 153 | vma2state(vma), 0, &ghs[x++]); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if (!dumping) |
| 158 | up_read(&mm->mmap_sem); |
| 159 | |
| 160 | gfs2_assert(get_v2sdp(sb), x == num_gh); |
| 161 | |
| 162 | count = operation(file, buf, size, offset, num_gh, ghs); |
| 163 | |
| 164 | while (num_gh--) |
| 165 | gfs2_holder_uninit(&ghs[num_gh]); |
| 166 | kfree(ghs); |
| 167 | |
| 168 | return count; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * walk_vm - Walk the vmas associated with a buffer for read or write. |
| 173 | * If any of them are gfs2, pass the gfs2 inode down to the read/write |
| 174 | * worker function so that locks can be acquired in the correct order. |
| 175 | * @file: The file to read/write from/to |
| 176 | * @buf: The buffer to copy to/from |
| 177 | * @size: The amount of data requested |
| 178 | * @offset: The current file offset |
| 179 | * @operation: The read or write worker function |
| 180 | * |
| 181 | * Outputs: Offset - updated according to number of bytes written |
| 182 | * |
| 183 | * Returns: The number of bytes written, errno on failure |
| 184 | */ |
| 185 | |
| 186 | static ssize_t walk_vm(struct file *file, const char __user *buf, size_t size, |
| 187 | loff_t *offset, do_rw_t operation) |
| 188 | { |
| 189 | struct gfs2_holder gh; |
| 190 | |
| 191 | if (current->mm) { |
| 192 | struct super_block *sb = file->f_dentry->d_inode->i_sb; |
| 193 | struct mm_struct *mm = current->mm; |
| 194 | struct vm_area_struct *vma; |
| 195 | unsigned long start = (unsigned long)buf; |
| 196 | unsigned long end = start + size; |
| 197 | int dumping = (current->flags & PF_DUMPCORE); |
| 198 | |
| 199 | if (!dumping) |
| 200 | down_read(&mm->mmap_sem); |
| 201 | |
| 202 | for (vma = find_vma(mm, start); vma; vma = vma->vm_next) { |
| 203 | if (end <= vma->vm_start) |
| 204 | break; |
| 205 | if (vma->vm_file && |
| 206 | vma->vm_file->f_dentry->d_inode->i_sb == sb) |
| 207 | goto do_locks; |
| 208 | } |
| 209 | |
| 210 | if (!dumping) |
| 211 | up_read(&mm->mmap_sem); |
| 212 | } |
| 213 | |
| 214 | return operation(file, buf, size, offset, 0, &gh); |
| 215 | |
| 216 | do_locks: |
| 217 | return walk_vm_hard(file, buf, size, offset, operation); |
| 218 | } |
| 219 | |
| 220 | static ssize_t do_jdata_read(struct file *file, char __user *buf, size_t size, |
| 221 | loff_t *offset) |
| 222 | { |
| 223 | struct gfs2_inode *ip = get_v2ip(file->f_mapping->host); |
| 224 | ssize_t count = 0; |
| 225 | |
| 226 | if (*offset < 0) |
| 227 | return -EINVAL; |
| 228 | if (!access_ok(VERIFY_WRITE, buf, size)) |
| 229 | return -EFAULT; |
| 230 | |
| 231 | if (!(file->f_flags & O_LARGEFILE)) { |
| 232 | if (*offset >= MAX_NON_LFS) |
| 233 | return -EFBIG; |
| 234 | if (*offset + size > MAX_NON_LFS) |
| 235 | size = MAX_NON_LFS - *offset; |
| 236 | } |
| 237 | |
| 238 | count = gfs2_jdata_read(ip, buf, *offset, size, gfs2_copy2user); |
| 239 | |
| 240 | if (count > 0) |
| 241 | *offset += count; |
| 242 | |
| 243 | return count; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * do_read_direct - Read bytes from a file |
| 248 | * @file: The file to read from |
| 249 | * @buf: The buffer to copy into |
| 250 | * @size: The amount of data requested |
| 251 | * @offset: The current file offset |
| 252 | * @num_gh: The number of other locks we need to do the read |
| 253 | * @ghs: the locks we need plus one for our lock |
| 254 | * |
| 255 | * Outputs: Offset - updated according to number of bytes read |
| 256 | * |
| 257 | * Returns: The number of bytes read, errno on failure |
| 258 | */ |
| 259 | |
| 260 | static ssize_t do_read_direct(struct file *file, char __user *buf, size_t size, |
| 261 | loff_t *offset, unsigned int num_gh, |
| 262 | struct gfs2_holder *ghs) |
| 263 | { |
| 264 | struct inode *inode = file->f_mapping->host; |
| 265 | struct gfs2_inode *ip = get_v2ip(inode); |
| 266 | unsigned int state = LM_ST_DEFERRED; |
| 267 | int flags = 0; |
| 268 | unsigned int x; |
| 269 | ssize_t count = 0; |
| 270 | int error; |
| 271 | |
| 272 | for (x = 0; x < num_gh; x++) |
| 273 | if (ghs[x].gh_gl == ip->i_gl) { |
| 274 | state = LM_ST_SHARED; |
| 275 | flags |= GL_LOCAL_EXCL; |
| 276 | break; |
| 277 | } |
| 278 | |
| 279 | gfs2_holder_init(ip->i_gl, state, flags, &ghs[num_gh]); |
| 280 | |
| 281 | error = gfs2_glock_nq_m(num_gh + 1, ghs); |
| 282 | if (error) |
| 283 | goto out; |
| 284 | |
| 285 | error = -EINVAL; |
| 286 | if (gfs2_is_jdata(ip)) |
| 287 | goto out_gunlock; |
| 288 | |
| 289 | if (gfs2_is_stuffed(ip)) { |
| 290 | size_t mask = bdev_hardsect_size(inode->i_sb->s_bdev) - 1; |
| 291 | |
| 292 | if (((*offset) & mask) || (((unsigned long)buf) & mask)) |
| 293 | goto out_gunlock; |
| 294 | |
| 295 | count = do_jdata_read(file, buf, size & ~mask, offset); |
| 296 | } else |
| 297 | count = generic_file_read(file, buf, size, offset); |
| 298 | |
| 299 | error = 0; |
| 300 | |
| 301 | out_gunlock: |
| 302 | gfs2_glock_dq_m(num_gh + 1, ghs); |
| 303 | |
| 304 | out: |
| 305 | gfs2_holder_uninit(&ghs[num_gh]); |
| 306 | |
| 307 | return (count) ? count : error; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * do_read_buf - Read bytes from a file |
| 312 | * @file: The file to read from |
| 313 | * @buf: The buffer to copy into |
| 314 | * @size: The amount of data requested |
| 315 | * @offset: The current file offset |
| 316 | * @num_gh: The number of other locks we need to do the read |
| 317 | * @ghs: the locks we need plus one for our lock |
| 318 | * |
| 319 | * Outputs: Offset - updated according to number of bytes read |
| 320 | * |
| 321 | * Returns: The number of bytes read, errno on failure |
| 322 | */ |
| 323 | |
| 324 | static ssize_t do_read_buf(struct file *file, char __user *buf, size_t size, |
| 325 | loff_t *offset, unsigned int num_gh, |
| 326 | struct gfs2_holder *ghs) |
| 327 | { |
| 328 | struct gfs2_inode *ip = get_v2ip(file->f_mapping->host); |
| 329 | ssize_t count = 0; |
| 330 | int error; |
| 331 | |
| 332 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &ghs[num_gh]); |
| 333 | |
| 334 | error = gfs2_glock_nq_m_atime(num_gh + 1, ghs); |
| 335 | if (error) |
| 336 | goto out; |
| 337 | |
| 338 | if (gfs2_is_jdata(ip)) |
| 339 | count = do_jdata_read(file, buf, size, offset); |
| 340 | else |
| 341 | count = generic_file_read(file, buf, size, offset); |
| 342 | |
| 343 | gfs2_glock_dq_m(num_gh + 1, ghs); |
| 344 | |
| 345 | out: |
| 346 | gfs2_holder_uninit(&ghs[num_gh]); |
| 347 | |
| 348 | return (count) ? count : error; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * gfs2_read - Read bytes from a file |
| 353 | * @file: The file to read from |
| 354 | * @buf: The buffer to copy into |
| 355 | * @size: The amount of data requested |
| 356 | * @offset: The current file offset |
| 357 | * |
| 358 | * Outputs: Offset - updated according to number of bytes read |
| 359 | * |
| 360 | * Returns: The number of bytes read, errno on failure |
| 361 | */ |
| 362 | |
| 363 | static ssize_t gfs2_read(struct file *file, char __user *buf, size_t size, |
| 364 | loff_t *offset) |
| 365 | { |
| 366 | atomic_inc(&get_v2sdp(file->f_mapping->host->i_sb)->sd_ops_file); |
| 367 | |
| 368 | if (file->f_flags & O_DIRECT) |
| 369 | return walk_vm(file, buf, size, offset, do_read_direct); |
| 370 | else |
| 371 | return walk_vm(file, buf, size, offset, do_read_buf); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * grope_mapping - feel up a mapping that needs to be written |
| 376 | * @buf: the start of the memory to be written |
| 377 | * @size: the size of the memory to be written |
| 378 | * |
| 379 | * We do this after acquiring the locks on the mapping, |
| 380 | * but before starting the write transaction. We need to make |
| 381 | * sure that we don't cause recursive transactions if blocks |
| 382 | * need to be allocated to the file backing the mapping. |
| 383 | * |
| 384 | * Returns: errno |
| 385 | */ |
| 386 | |
| 387 | static int grope_mapping(const char __user *buf, size_t size) |
| 388 | { |
| 389 | const char __user *stop = buf + size; |
| 390 | char c; |
| 391 | |
| 392 | while (buf < stop) { |
| 393 | if (copy_from_user(&c, buf, 1)) |
| 394 | return -EFAULT; |
| 395 | buf += PAGE_CACHE_SIZE; |
| 396 | buf = (const char __user *)PAGE_ALIGN((unsigned long)buf); |
| 397 | } |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * do_write_direct_alloc - Write bytes to a file |
| 404 | * @file: The file to write to |
| 405 | * @buf: The buffer to copy from |
| 406 | * @size: The amount of data requested |
| 407 | * @offset: The current file offset |
| 408 | * |
| 409 | * Outputs: Offset - updated according to number of bytes written |
| 410 | * |
| 411 | * Returns: The number of bytes written, errno on failure |
| 412 | */ |
| 413 | |
| 414 | static ssize_t do_write_direct_alloc(struct file *file, const char __user *buf, size_t size, |
| 415 | loff_t *offset) |
| 416 | { |
| 417 | struct inode *inode = file->f_mapping->host; |
| 418 | struct gfs2_inode *ip = get_v2ip(inode); |
| 419 | struct gfs2_sbd *sdp = ip->i_sbd; |
| 420 | struct gfs2_alloc *al = NULL; |
| 421 | struct iovec local_iov = { .iov_base = buf, .iov_len = size }; |
| 422 | struct buffer_head *dibh; |
| 423 | unsigned int data_blocks, ind_blocks; |
| 424 | ssize_t count; |
| 425 | int error; |
| 426 | |
| 427 | gfs2_write_calc_reserv(ip, size, &data_blocks, &ind_blocks); |
| 428 | |
| 429 | al = gfs2_alloc_get(ip); |
| 430 | |
| 431 | error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); |
| 432 | if (error) |
| 433 | goto fail; |
| 434 | |
| 435 | error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid); |
| 436 | if (error) |
| 437 | goto fail_gunlock_q; |
| 438 | |
| 439 | al->al_requested = data_blocks + ind_blocks; |
| 440 | |
| 441 | error = gfs2_inplace_reserve(ip); |
| 442 | if (error) |
| 443 | goto fail_gunlock_q; |
| 444 | |
| 445 | error = gfs2_trans_begin(sdp, |
| 446 | al->al_rgd->rd_ri.ri_length + ind_blocks + |
| 447 | RES_DINODE + RES_STATFS + RES_QUOTA, 0); |
| 448 | if (error) |
| 449 | goto fail_ipres; |
| 450 | |
| 451 | if ((ip->i_di.di_mode & (S_ISUID | S_ISGID)) && !capable(CAP_FSETID)) { |
| 452 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 453 | if (error) |
| 454 | goto fail_end_trans; |
| 455 | |
| 456 | ip->i_di.di_mode &= (ip->i_di.di_mode & S_IXGRP) ? |
| 457 | (~(S_ISUID | S_ISGID)) : (~S_ISUID); |
| 458 | |
| 459 | gfs2_trans_add_bh(ip->i_gl, dibh); |
| 460 | gfs2_dinode_out(&ip->i_di, dibh->b_data); |
| 461 | brelse(dibh); |
| 462 | } |
| 463 | |
| 464 | if (gfs2_is_stuffed(ip)) { |
| 465 | error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_sync, NULL); |
| 466 | if (error) |
| 467 | goto fail_end_trans; |
| 468 | } |
| 469 | |
| 470 | count = generic_file_write_nolock(file, &local_iov, 1, offset); |
| 471 | if (count < 0) { |
| 472 | error = count; |
| 473 | goto fail_end_trans; |
| 474 | } |
| 475 | |
| 476 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 477 | if (error) |
| 478 | goto fail_end_trans; |
| 479 | |
| 480 | if (ip->i_di.di_size < inode->i_size) |
| 481 | ip->i_di.di_size = inode->i_size; |
| 482 | ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds(); |
| 483 | |
| 484 | gfs2_trans_add_bh(ip->i_gl, dibh); |
| 485 | gfs2_dinode_out(&ip->i_di, dibh->b_data); |
| 486 | brelse(dibh); |
| 487 | |
| 488 | gfs2_trans_end(sdp); |
| 489 | |
| 490 | if (file->f_flags & O_SYNC) |
| 491 | gfs2_log_flush_glock(ip->i_gl); |
| 492 | |
| 493 | gfs2_inplace_release(ip); |
| 494 | gfs2_quota_unlock(ip); |
| 495 | gfs2_alloc_put(ip); |
| 496 | |
| 497 | if (file->f_mapping->nrpages) { |
| 498 | error = filemap_fdatawrite(file->f_mapping); |
| 499 | if (!error) |
| 500 | error = filemap_fdatawait(file->f_mapping); |
| 501 | } |
| 502 | if (error) |
| 503 | return error; |
| 504 | |
| 505 | return count; |
| 506 | |
| 507 | fail_end_trans: |
| 508 | gfs2_trans_end(sdp); |
| 509 | |
| 510 | fail_ipres: |
| 511 | gfs2_inplace_release(ip); |
| 512 | |
| 513 | fail_gunlock_q: |
| 514 | gfs2_quota_unlock(ip); |
| 515 | |
| 516 | fail: |
| 517 | gfs2_alloc_put(ip); |
| 518 | |
| 519 | return error; |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * do_write_direct - Write bytes to a file |
| 524 | * @file: The file to write to |
| 525 | * @buf: The buffer to copy from |
| 526 | * @size: The amount of data requested |
| 527 | * @offset: The current file offset |
| 528 | * @num_gh: The number of other locks we need to do the read |
| 529 | * @gh: the locks we need plus one for our lock |
| 530 | * |
| 531 | * Outputs: Offset - updated according to number of bytes written |
| 532 | * |
| 533 | * Returns: The number of bytes written, errno on failure |
| 534 | */ |
| 535 | |
| 536 | static ssize_t do_write_direct(struct file *file, const char __user *buf, size_t size, |
| 537 | loff_t *offset, unsigned int num_gh, |
| 538 | struct gfs2_holder *ghs) |
| 539 | { |
| 540 | struct gfs2_inode *ip = get_v2ip(file->f_mapping->host); |
| 541 | struct gfs2_sbd *sdp = ip->i_sbd; |
| 542 | struct gfs2_file *fp = get_v2fp(file); |
| 543 | unsigned int state = LM_ST_DEFERRED; |
| 544 | int alloc_required; |
| 545 | unsigned int x; |
| 546 | size_t s; |
| 547 | ssize_t count = 0; |
| 548 | int error; |
| 549 | |
| 550 | if (test_bit(GFF_DID_DIRECT_ALLOC, &fp->f_flags)) |
| 551 | state = LM_ST_EXCLUSIVE; |
| 552 | else |
| 553 | for (x = 0; x < num_gh; x++) |
| 554 | if (ghs[x].gh_gl == ip->i_gl) { |
| 555 | state = LM_ST_EXCLUSIVE; |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | restart: |
| 560 | gfs2_holder_init(ip->i_gl, state, 0, &ghs[num_gh]); |
| 561 | |
| 562 | error = gfs2_glock_nq_m(num_gh + 1, ghs); |
| 563 | if (error) |
| 564 | goto out; |
| 565 | |
| 566 | error = -EINVAL; |
| 567 | if (gfs2_is_jdata(ip)) |
| 568 | goto out_gunlock; |
| 569 | |
| 570 | if (num_gh) { |
| 571 | error = grope_mapping(buf, size); |
| 572 | if (error) |
| 573 | goto out_gunlock; |
| 574 | } |
| 575 | |
| 576 | if (file->f_flags & O_APPEND) |
| 577 | *offset = ip->i_di.di_size; |
| 578 | |
| 579 | if (!(file->f_flags & O_LARGEFILE)) { |
| 580 | error = -EFBIG; |
| 581 | if (*offset >= MAX_NON_LFS) |
| 582 | goto out_gunlock; |
| 583 | if (*offset + size > MAX_NON_LFS) |
| 584 | size = MAX_NON_LFS - *offset; |
| 585 | } |
| 586 | |
| 587 | if (gfs2_is_stuffed(ip) || |
| 588 | *offset + size > ip->i_di.di_size || |
| 589 | ((ip->i_di.di_mode & (S_ISUID | S_ISGID)) && !capable(CAP_FSETID))) |
| 590 | alloc_required = 1; |
| 591 | else { |
| 592 | error = gfs2_write_alloc_required(ip, *offset, size, |
| 593 | &alloc_required); |
| 594 | if (error) |
| 595 | goto out_gunlock; |
| 596 | } |
| 597 | |
| 598 | if (alloc_required && state != LM_ST_EXCLUSIVE) { |
| 599 | gfs2_glock_dq_m(num_gh + 1, ghs); |
| 600 | gfs2_holder_uninit(&ghs[num_gh]); |
| 601 | state = LM_ST_EXCLUSIVE; |
| 602 | goto restart; |
| 603 | } |
| 604 | |
| 605 | if (alloc_required) { |
| 606 | set_bit(GFF_DID_DIRECT_ALLOC, &fp->f_flags); |
| 607 | |
| 608 | /* split large writes into smaller atomic transactions */ |
| 609 | while (size) { |
| 610 | s = gfs2_tune_get(sdp, gt_max_atomic_write); |
| 611 | if (s > size) |
| 612 | s = size; |
| 613 | |
| 614 | error = do_write_direct_alloc(file, buf, s, offset); |
| 615 | if (error < 0) |
| 616 | goto out_gunlock; |
| 617 | |
| 618 | buf += error; |
| 619 | size -= error; |
| 620 | count += error; |
| 621 | } |
| 622 | } else { |
| 623 | struct iovec local_iov = { .iov_base = buf, .iov_len = size }; |
| 624 | struct gfs2_holder t_gh; |
| 625 | |
| 626 | clear_bit(GFF_DID_DIRECT_ALLOC, &fp->f_flags); |
| 627 | |
| 628 | error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED, |
| 629 | GL_NEVER_RECURSE, &t_gh); |
| 630 | if (error) |
| 631 | goto out_gunlock; |
| 632 | |
| 633 | count = generic_file_write_nolock(file, &local_iov, 1, offset); |
| 634 | |
| 635 | gfs2_glock_dq_uninit(&t_gh); |
| 636 | } |
| 637 | |
| 638 | error = 0; |
| 639 | |
| 640 | out_gunlock: |
| 641 | gfs2_glock_dq_m(num_gh + 1, ghs); |
| 642 | |
| 643 | out: |
| 644 | gfs2_holder_uninit(&ghs[num_gh]); |
| 645 | |
| 646 | return (count) ? count : error; |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * do_do_write_buf - Write bytes to a file |
| 651 | * @file: The file to write to |
| 652 | * @buf: The buffer to copy from |
| 653 | * @size: The amount of data requested |
| 654 | * @offset: The current file offset |
| 655 | * |
| 656 | * Outputs: Offset - updated according to number of bytes written |
| 657 | * |
| 658 | * Returns: The number of bytes written, errno on failure |
| 659 | */ |
| 660 | |
| 661 | static ssize_t do_do_write_buf(struct file *file, const char __user *buf, size_t size, |
| 662 | loff_t *offset) |
| 663 | { |
| 664 | struct inode *inode = file->f_mapping->host; |
| 665 | struct gfs2_inode *ip = get_v2ip(inode); |
| 666 | struct gfs2_sbd *sdp = ip->i_sbd; |
| 667 | struct gfs2_alloc *al = NULL; |
| 668 | struct buffer_head *dibh; |
| 669 | unsigned int data_blocks, ind_blocks; |
| 670 | int alloc_required, journaled; |
| 671 | ssize_t count; |
| 672 | int error; |
| 673 | |
| 674 | journaled = gfs2_is_jdata(ip); |
| 675 | |
| 676 | gfs2_write_calc_reserv(ip, size, &data_blocks, &ind_blocks); |
| 677 | |
| 678 | error = gfs2_write_alloc_required(ip, *offset, size, &alloc_required); |
| 679 | if (error) |
| 680 | return error; |
| 681 | |
| 682 | if (alloc_required) { |
| 683 | al = gfs2_alloc_get(ip); |
| 684 | |
| 685 | error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); |
| 686 | if (error) |
| 687 | goto fail; |
| 688 | |
| 689 | error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid); |
| 690 | if (error) |
| 691 | goto fail_gunlock_q; |
| 692 | |
| 693 | al->al_requested = data_blocks + ind_blocks; |
| 694 | |
| 695 | error = gfs2_inplace_reserve(ip); |
| 696 | if (error) |
| 697 | goto fail_gunlock_q; |
| 698 | |
| 699 | error = gfs2_trans_begin(sdp, |
| 700 | al->al_rgd->rd_ri.ri_length + |
| 701 | ind_blocks + |
| 702 | ((journaled) ? data_blocks : 0) + |
| 703 | RES_DINODE + RES_STATFS + RES_QUOTA, |
| 704 | 0); |
| 705 | if (error) |
| 706 | goto fail_ipres; |
| 707 | } else { |
| 708 | error = gfs2_trans_begin(sdp, |
| 709 | ((journaled) ? data_blocks : 0) + |
| 710 | RES_DINODE, |
| 711 | 0); |
| 712 | if (error) |
| 713 | goto fail_ipres; |
| 714 | } |
| 715 | |
| 716 | if ((ip->i_di.di_mode & (S_ISUID | S_ISGID)) && !capable(CAP_FSETID)) { |
| 717 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 718 | if (error) |
| 719 | goto fail_end_trans; |
| 720 | |
| 721 | ip->i_di.di_mode &= (ip->i_di.di_mode & S_IXGRP) ? |
| 722 | (~(S_ISUID | S_ISGID)) : (~S_ISUID); |
| 723 | |
| 724 | gfs2_trans_add_bh(ip->i_gl, dibh); |
| 725 | gfs2_dinode_out(&ip->i_di, dibh->b_data); |
| 726 | brelse(dibh); |
| 727 | } |
| 728 | |
| 729 | if (journaled) { |
| 730 | count = gfs2_jdata_write(ip, buf, *offset, size, |
| 731 | gfs2_copy_from_user); |
| 732 | if (count < 0) { |
| 733 | error = count; |
| 734 | goto fail_end_trans; |
| 735 | } |
| 736 | |
| 737 | *offset += count; |
| 738 | } else { |
| 739 | struct iovec local_iov = { .iov_base = buf, .iov_len = size }; |
| 740 | |
| 741 | count = generic_file_write_nolock(file, &local_iov, 1, offset); |
| 742 | if (count < 0) { |
| 743 | error = count; |
| 744 | goto fail_end_trans; |
| 745 | } |
| 746 | |
| 747 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 748 | if (error) |
| 749 | goto fail_end_trans; |
| 750 | |
| 751 | if (ip->i_di.di_size < inode->i_size) |
| 752 | ip->i_di.di_size = inode->i_size; |
| 753 | ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds(); |
| 754 | |
| 755 | gfs2_trans_add_bh(ip->i_gl, dibh); |
| 756 | gfs2_dinode_out(&ip->i_di, dibh->b_data); |
| 757 | brelse(dibh); |
| 758 | } |
| 759 | |
| 760 | gfs2_trans_end(sdp); |
| 761 | |
| 762 | if (file->f_flags & O_SYNC || IS_SYNC(inode)) { |
| 763 | gfs2_log_flush_glock(ip->i_gl); |
| 764 | error = filemap_fdatawrite(file->f_mapping); |
| 765 | if (error == 0) |
| 766 | error = filemap_fdatawait(file->f_mapping); |
| 767 | if (error) |
| 768 | goto fail_ipres; |
| 769 | } |
| 770 | |
| 771 | if (alloc_required) { |
| 772 | gfs2_assert_warn(sdp, count != size || |
| 773 | al->al_alloced); |
| 774 | gfs2_inplace_release(ip); |
| 775 | gfs2_quota_unlock(ip); |
| 776 | gfs2_alloc_put(ip); |
| 777 | } |
| 778 | |
| 779 | return count; |
| 780 | |
| 781 | fail_end_trans: |
| 782 | gfs2_trans_end(sdp); |
| 783 | |
| 784 | fail_ipres: |
| 785 | if (alloc_required) |
| 786 | gfs2_inplace_release(ip); |
| 787 | |
| 788 | fail_gunlock_q: |
| 789 | if (alloc_required) |
| 790 | gfs2_quota_unlock(ip); |
| 791 | |
| 792 | fail: |
| 793 | if (alloc_required) |
| 794 | gfs2_alloc_put(ip); |
| 795 | |
| 796 | return error; |
| 797 | } |
| 798 | |
| 799 | /** |
| 800 | * do_write_buf - Write bytes to a file |
| 801 | * @file: The file to write to |
| 802 | * @buf: The buffer to copy from |
| 803 | * @size: The amount of data requested |
| 804 | * @offset: The current file offset |
| 805 | * @num_gh: The number of other locks we need to do the read |
| 806 | * @gh: the locks we need plus one for our lock |
| 807 | * |
| 808 | * Outputs: Offset - updated according to number of bytes written |
| 809 | * |
| 810 | * Returns: The number of bytes written, errno on failure |
| 811 | */ |
| 812 | |
| 813 | static ssize_t do_write_buf(struct file *file, const char __user *buf, size_t size, |
| 814 | loff_t *offset, unsigned int num_gh, |
| 815 | struct gfs2_holder *ghs) |
| 816 | { |
| 817 | struct gfs2_inode *ip = get_v2ip(file->f_mapping->host); |
| 818 | struct gfs2_sbd *sdp = ip->i_sbd; |
| 819 | size_t s; |
| 820 | ssize_t count = 0; |
| 821 | int error; |
| 822 | |
| 823 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ghs[num_gh]); |
| 824 | |
| 825 | error = gfs2_glock_nq_m(num_gh + 1, ghs); |
| 826 | if (error) |
| 827 | goto out; |
| 828 | |
| 829 | if (num_gh) { |
| 830 | error = grope_mapping(buf, size); |
| 831 | if (error) |
| 832 | goto out_gunlock; |
| 833 | } |
| 834 | |
| 835 | if (file->f_flags & O_APPEND) |
| 836 | *offset = ip->i_di.di_size; |
| 837 | |
| 838 | if (!(file->f_flags & O_LARGEFILE)) { |
| 839 | error = -EFBIG; |
| 840 | if (*offset >= MAX_NON_LFS) |
| 841 | goto out_gunlock; |
| 842 | if (*offset + size > MAX_NON_LFS) |
| 843 | size = MAX_NON_LFS - *offset; |
| 844 | } |
| 845 | |
| 846 | /* split large writes into smaller atomic transactions */ |
| 847 | while (size) { |
| 848 | s = gfs2_tune_get(sdp, gt_max_atomic_write); |
| 849 | if (s > size) |
| 850 | s = size; |
| 851 | |
| 852 | error = do_do_write_buf(file, buf, s, offset); |
| 853 | if (error < 0) |
| 854 | goto out_gunlock; |
| 855 | |
| 856 | buf += error; |
| 857 | size -= error; |
| 858 | count += error; |
| 859 | } |
| 860 | |
| 861 | error = 0; |
| 862 | |
| 863 | out_gunlock: |
| 864 | gfs2_glock_dq_m(num_gh + 1, ghs); |
| 865 | |
| 866 | out: |
| 867 | gfs2_holder_uninit(&ghs[num_gh]); |
| 868 | |
| 869 | return (count) ? count : error; |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * gfs2_write - Write bytes to a file |
| 874 | * @file: The file to write to |
| 875 | * @buf: The buffer to copy from |
| 876 | * @size: The amount of data requested |
| 877 | * @offset: The current file offset |
| 878 | * |
| 879 | * Outputs: Offset - updated according to number of bytes written |
| 880 | * |
| 881 | * Returns: The number of bytes written, errno on failure |
| 882 | */ |
| 883 | |
| 884 | static ssize_t gfs2_write(struct file *file, const char __user *buf, |
| 885 | size_t size, loff_t *offset) |
| 886 | { |
| 887 | struct inode *inode = file->f_mapping->host; |
| 888 | ssize_t count; |
| 889 | |
| 890 | atomic_inc(&get_v2sdp(inode->i_sb)->sd_ops_file); |
| 891 | |
| 892 | if (*offset < 0) |
| 893 | return -EINVAL; |
| 894 | if (!access_ok(VERIFY_READ, buf, size)) |
| 895 | return -EFAULT; |
| 896 | |
| 897 | mutex_lock(&inode->i_mutex); |
| 898 | if (file->f_flags & O_DIRECT) |
| 899 | count = walk_vm(file, buf, size, offset, |
| 900 | do_write_direct); |
| 901 | else |
| 902 | count = walk_vm(file, buf, size, offset, do_write_buf); |
| 903 | mutex_unlock(&inode->i_mutex); |
| 904 | |
| 905 | return count; |
| 906 | } |
| 907 | |
| 908 | /** |
| 909 | * filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read() |
| 910 | * @opaque: opaque data used by the function |
| 911 | * @name: the name of the directory entry |
| 912 | * @length: the length of the name |
| 913 | * @offset: the entry's offset in the directory |
| 914 | * @inum: the inode number the entry points to |
| 915 | * @type: the type of inode the entry points to |
| 916 | * |
| 917 | * Returns: 0 on success, 1 if buffer full |
| 918 | */ |
| 919 | |
| 920 | static int filldir_reg_func(void *opaque, const char *name, unsigned int length, |
| 921 | uint64_t offset, struct gfs2_inum *inum, |
| 922 | unsigned int type) |
| 923 | { |
| 924 | struct filldir_reg *fdr = (struct filldir_reg *)opaque; |
| 925 | struct gfs2_sbd *sdp = fdr->fdr_sbd; |
| 926 | int error; |
| 927 | |
| 928 | error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset, |
| 929 | inum->no_formal_ino, type); |
| 930 | if (error) |
| 931 | return 1; |
| 932 | |
| 933 | if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) { |
| 934 | gfs2_glock_prefetch_num(sdp, |
| 935 | inum->no_addr, &gfs2_inode_glops, |
| 936 | LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY); |
| 937 | gfs2_glock_prefetch_num(sdp, |
| 938 | inum->no_addr, &gfs2_iopen_glops, |
| 939 | LM_ST_SHARED, LM_FLAG_TRY); |
| 940 | } |
| 941 | |
| 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | /** |
| 946 | * readdir_reg - Read directory entries from a directory |
| 947 | * @file: The directory to read from |
| 948 | * @dirent: Buffer for dirents |
| 949 | * @filldir: Function used to do the copying |
| 950 | * |
| 951 | * Returns: errno |
| 952 | */ |
| 953 | |
| 954 | static int readdir_reg(struct file *file, void *dirent, filldir_t filldir) |
| 955 | { |
| 956 | struct gfs2_inode *dip = get_v2ip(file->f_mapping->host); |
| 957 | struct filldir_reg fdr; |
| 958 | struct gfs2_holder d_gh; |
| 959 | uint64_t offset = file->f_pos; |
| 960 | int error; |
| 961 | |
| 962 | fdr.fdr_sbd = dip->i_sbd; |
| 963 | fdr.fdr_prefetch = 1; |
| 964 | fdr.fdr_filldir = filldir; |
| 965 | fdr.fdr_opaque = dirent; |
| 966 | |
| 967 | gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh); |
| 968 | error = gfs2_glock_nq_atime(&d_gh); |
| 969 | if (error) { |
| 970 | gfs2_holder_uninit(&d_gh); |
| 971 | return error; |
| 972 | } |
| 973 | |
| 974 | error = gfs2_dir_read(dip, &offset, &fdr, filldir_reg_func); |
| 975 | |
| 976 | gfs2_glock_dq_uninit(&d_gh); |
| 977 | |
| 978 | file->f_pos = offset; |
| 979 | |
| 980 | return error; |
| 981 | } |
| 982 | |
| 983 | /** |
| 984 | * filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read() |
| 985 | * @opaque: opaque data used by the function |
| 986 | * @name: the name of the directory entry |
| 987 | * @length: the length of the name |
| 988 | * @offset: the entry's offset in the directory |
| 989 | * @inum: the inode number the entry points to |
| 990 | * @type: the type of inode the entry points to |
| 991 | * |
| 992 | * For supporting NFS. |
| 993 | * |
| 994 | * Returns: 0 on success, 1 if buffer full |
| 995 | */ |
| 996 | |
| 997 | static int filldir_bad_func(void *opaque, const char *name, unsigned int length, |
| 998 | uint64_t offset, struct gfs2_inum *inum, |
| 999 | unsigned int type) |
| 1000 | { |
| 1001 | struct filldir_bad *fdb = (struct filldir_bad *)opaque; |
| 1002 | struct gfs2_sbd *sdp = fdb->fdb_sbd; |
| 1003 | struct filldir_bad_entry *fbe; |
| 1004 | |
| 1005 | if (fdb->fdb_entry_off == fdb->fdb_entry_num || |
| 1006 | fdb->fdb_name_off + length > fdb->fdb_name_size) |
| 1007 | return 1; |
| 1008 | |
| 1009 | fbe = &fdb->fdb_entry[fdb->fdb_entry_off]; |
| 1010 | fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off; |
| 1011 | memcpy(fbe->fbe_name, name, length); |
| 1012 | fbe->fbe_length = length; |
| 1013 | fbe->fbe_offset = offset; |
| 1014 | fbe->fbe_inum = *inum; |
| 1015 | fbe->fbe_type = type; |
| 1016 | |
| 1017 | fdb->fdb_entry_off++; |
| 1018 | fdb->fdb_name_off += length; |
| 1019 | |
| 1020 | if (!(length == 1 && *name == '.')) { |
| 1021 | gfs2_glock_prefetch_num(sdp, |
| 1022 | inum->no_addr, &gfs2_inode_glops, |
| 1023 | LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY); |
| 1024 | gfs2_glock_prefetch_num(sdp, |
| 1025 | inum->no_addr, &gfs2_iopen_glops, |
| 1026 | LM_ST_SHARED, LM_FLAG_TRY); |
| 1027 | } |
| 1028 | |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | /** |
| 1033 | * readdir_bad - Read directory entries from a directory |
| 1034 | * @file: The directory to read from |
| 1035 | * @dirent: Buffer for dirents |
| 1036 | * @filldir: Function used to do the copying |
| 1037 | * |
| 1038 | * For supporting NFS. |
| 1039 | * |
| 1040 | * Returns: errno |
| 1041 | */ |
| 1042 | |
| 1043 | static int readdir_bad(struct file *file, void *dirent, filldir_t filldir) |
| 1044 | { |
| 1045 | struct gfs2_inode *dip = get_v2ip(file->f_mapping->host); |
| 1046 | struct gfs2_sbd *sdp = dip->i_sbd; |
| 1047 | struct filldir_reg fdr; |
| 1048 | unsigned int entries, size; |
| 1049 | struct filldir_bad *fdb; |
| 1050 | struct gfs2_holder d_gh; |
| 1051 | uint64_t offset = file->f_pos; |
| 1052 | unsigned int x; |
| 1053 | struct filldir_bad_entry *fbe; |
| 1054 | int error; |
| 1055 | |
| 1056 | entries = gfs2_tune_get(sdp, gt_entries_per_readdir); |
| 1057 | size = sizeof(struct filldir_bad) + |
| 1058 | entries * (sizeof(struct filldir_bad_entry) + GFS2_FAST_NAME_SIZE); |
| 1059 | |
| 1060 | fdb = kzalloc(size, GFP_KERNEL); |
| 1061 | if (!fdb) |
| 1062 | return -ENOMEM; |
| 1063 | |
| 1064 | fdb->fdb_sbd = sdp; |
| 1065 | fdb->fdb_entry = (struct filldir_bad_entry *)(fdb + 1); |
| 1066 | fdb->fdb_entry_num = entries; |
| 1067 | fdb->fdb_name = ((char *)fdb) + sizeof(struct filldir_bad) + |
| 1068 | entries * sizeof(struct filldir_bad_entry); |
| 1069 | fdb->fdb_name_size = entries * GFS2_FAST_NAME_SIZE; |
| 1070 | |
| 1071 | gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh); |
| 1072 | error = gfs2_glock_nq_atime(&d_gh); |
| 1073 | if (error) { |
| 1074 | gfs2_holder_uninit(&d_gh); |
| 1075 | goto out; |
| 1076 | } |
| 1077 | |
| 1078 | error = gfs2_dir_read(dip, &offset, fdb, filldir_bad_func); |
| 1079 | |
| 1080 | gfs2_glock_dq_uninit(&d_gh); |
| 1081 | |
| 1082 | fdr.fdr_sbd = sdp; |
| 1083 | fdr.fdr_prefetch = 0; |
| 1084 | fdr.fdr_filldir = filldir; |
| 1085 | fdr.fdr_opaque = dirent; |
| 1086 | |
| 1087 | for (x = 0; x < fdb->fdb_entry_off; x++) { |
| 1088 | fbe = &fdb->fdb_entry[x]; |
| 1089 | |
| 1090 | error = filldir_reg_func(&fdr, |
| 1091 | fbe->fbe_name, fbe->fbe_length, |
| 1092 | fbe->fbe_offset, |
| 1093 | &fbe->fbe_inum, fbe->fbe_type); |
| 1094 | if (error) { |
| 1095 | file->f_pos = fbe->fbe_offset; |
| 1096 | error = 0; |
| 1097 | goto out; |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | file->f_pos = offset; |
| 1102 | |
| 1103 | out: |
| 1104 | kfree(fdb); |
| 1105 | |
| 1106 | return error; |
| 1107 | } |
| 1108 | |
| 1109 | /** |
| 1110 | * gfs2_readdir - Read directory entries from a directory |
| 1111 | * @file: The directory to read from |
| 1112 | * @dirent: Buffer for dirents |
| 1113 | * @filldir: Function used to do the copying |
| 1114 | * |
| 1115 | * Returns: errno |
| 1116 | */ |
| 1117 | |
| 1118 | static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir) |
| 1119 | { |
| 1120 | int error; |
| 1121 | |
| 1122 | atomic_inc(&get_v2sdp(file->f_mapping->host->i_sb)->sd_ops_file); |
| 1123 | |
| 1124 | if (strcmp(current->comm, "nfsd") != 0) |
| 1125 | error = readdir_reg(file, dirent, filldir); |
| 1126 | else |
| 1127 | error = readdir_bad(file, dirent, filldir); |
| 1128 | |
| 1129 | return error; |
| 1130 | } |
| 1131 | |
| 1132 | static int gfs2_ioctl_flags(struct gfs2_inode *ip, unsigned int cmd, unsigned long arg) |
| 1133 | { |
| 1134 | unsigned int lmode = (cmd == GFS2_IOCTL_SETFLAGS) ? LM_ST_EXCLUSIVE : LM_ST_SHARED; |
| 1135 | struct buffer_head *dibh; |
| 1136 | struct gfs2_holder i_gh; |
| 1137 | int error; |
| 1138 | __u32 flags = 0, change; |
| 1139 | |
| 1140 | if (cmd == GFS2_IOCTL_SETFLAGS) { |
| 1141 | error = get_user(flags, (__u32 __user *)arg); |
| 1142 | if (error) |
| 1143 | return -EFAULT; |
| 1144 | } |
| 1145 | |
| 1146 | error = gfs2_glock_nq_init(ip->i_gl, lmode, 0, &i_gh); |
| 1147 | if (error) |
| 1148 | return error; |
| 1149 | |
| 1150 | if (cmd == GFS2_IOCTL_SETFLAGS) { |
| 1151 | change = flags ^ ip->i_di.di_flags; |
| 1152 | error = -EPERM; |
| 1153 | if (change & (GFS2_DIF_IMMUTABLE|GFS2_DIF_APPENDONLY)) { |
| 1154 | if (!capable(CAP_LINUX_IMMUTABLE)) |
| 1155 | goto out; |
| 1156 | } |
| 1157 | error = -EINVAL; |
| 1158 | if (flags & (GFS2_DIF_JDATA|GFS2_DIF_DIRECTIO)) { |
| 1159 | if (!S_ISREG(ip->i_di.di_mode)) |
| 1160 | goto out; |
| 1161 | /* FIXME: Would be nice not to require the following test */ |
| 1162 | if ((flags & GFS2_DIF_JDATA) && ip->i_di.di_size) |
| 1163 | goto out; |
| 1164 | } |
| 1165 | if (flags & (GFS2_DIF_INHERIT_JDATA|GFS2_DIF_INHERIT_DIRECTIO)) { |
| 1166 | if (!S_ISDIR(ip->i_di.di_mode)) |
| 1167 | goto out; |
| 1168 | } |
| 1169 | |
| 1170 | error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0); |
| 1171 | if (error) |
| 1172 | goto out; |
| 1173 | |
| 1174 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 1175 | if (error) |
| 1176 | goto out_trans_end; |
| 1177 | |
| 1178 | ip->i_di.di_flags = flags; |
| 1179 | |
| 1180 | gfs2_trans_add_bh(ip->i_gl, dibh); |
| 1181 | gfs2_dinode_out(&ip->i_di, dibh->b_data); |
| 1182 | |
| 1183 | brelse(dibh); |
| 1184 | |
| 1185 | out_trans_end: |
| 1186 | gfs2_trans_end(ip->i_sbd); |
| 1187 | } else { |
| 1188 | flags = ip->i_di.di_flags; |
| 1189 | } |
| 1190 | out: |
| 1191 | gfs2_glock_dq_uninit(&i_gh); |
| 1192 | if (cmd == GFS2_IOCTL_GETFLAGS) { |
| 1193 | if (put_user(flags, (__u32 __user *)arg)) |
| 1194 | return -EFAULT; |
| 1195 | } |
| 1196 | return error; |
| 1197 | } |
| 1198 | |
| 1199 | /** |
| 1200 | * gfs2_ioctl - do an ioctl on a file |
| 1201 | * @inode: the inode |
| 1202 | * @file: the file pointer |
| 1203 | * @cmd: the ioctl command |
| 1204 | * @arg: the argument |
| 1205 | * |
| 1206 | * Returns: errno |
| 1207 | */ |
| 1208 | |
| 1209 | static int gfs2_ioctl(struct inode *inode, struct file *file, unsigned int cmd, |
| 1210 | unsigned long arg) |
| 1211 | { |
| 1212 | struct gfs2_inode *ip = get_v2ip(inode); |
| 1213 | |
| 1214 | atomic_inc(&ip->i_sbd->sd_ops_file); |
| 1215 | |
| 1216 | switch (cmd) { |
| 1217 | case GFS2_IOCTL_IDENTIFY: { |
| 1218 | unsigned int x = GFS2_MAGIC; |
| 1219 | if (copy_to_user((unsigned int __user *)arg, &x, sizeof(unsigned int))) |
| 1220 | return -EFAULT; |
| 1221 | return 0; |
| 1222 | |
| 1223 | case GFS2_IOCTL_SETFLAGS: |
| 1224 | case GFS2_IOCTL_GETFLAGS: |
| 1225 | return gfs2_ioctl_flags(ip, cmd, arg); |
| 1226 | } |
| 1227 | |
| 1228 | default: |
| 1229 | return -ENOTTY; |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | /** |
| 1234 | * gfs2_mmap - |
| 1235 | * @file: The file to map |
| 1236 | * @vma: The VMA which described the mapping |
| 1237 | * |
| 1238 | * Returns: 0 or error code |
| 1239 | */ |
| 1240 | |
| 1241 | static int gfs2_mmap(struct file *file, struct vm_area_struct *vma) |
| 1242 | { |
| 1243 | struct gfs2_inode *ip = get_v2ip(file->f_mapping->host); |
| 1244 | struct gfs2_holder i_gh; |
| 1245 | int error; |
| 1246 | |
| 1247 | atomic_inc(&ip->i_sbd->sd_ops_file); |
| 1248 | |
| 1249 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh); |
| 1250 | error = gfs2_glock_nq_atime(&i_gh); |
| 1251 | if (error) { |
| 1252 | gfs2_holder_uninit(&i_gh); |
| 1253 | return error; |
| 1254 | } |
| 1255 | |
| 1256 | if (gfs2_is_jdata(ip)) { |
| 1257 | if (vma->vm_flags & VM_MAYSHARE) |
| 1258 | error = -EOPNOTSUPP; |
| 1259 | else |
| 1260 | vma->vm_ops = &gfs2_vm_ops_private; |
| 1261 | } else { |
| 1262 | /* This is VM_MAYWRITE instead of VM_WRITE because a call |
| 1263 | to mprotect() can turn on VM_WRITE later. */ |
| 1264 | |
| 1265 | if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) == |
| 1266 | (VM_MAYSHARE | VM_MAYWRITE)) |
| 1267 | vma->vm_ops = &gfs2_vm_ops_sharewrite; |
| 1268 | else |
| 1269 | vma->vm_ops = &gfs2_vm_ops_private; |
| 1270 | } |
| 1271 | |
| 1272 | gfs2_glock_dq_uninit(&i_gh); |
| 1273 | |
| 1274 | return error; |
| 1275 | } |
| 1276 | |
| 1277 | /** |
| 1278 | * gfs2_open - open a file |
| 1279 | * @inode: the inode to open |
| 1280 | * @file: the struct file for this opening |
| 1281 | * |
| 1282 | * Returns: errno |
| 1283 | */ |
| 1284 | |
| 1285 | static int gfs2_open(struct inode *inode, struct file *file) |
| 1286 | { |
| 1287 | struct gfs2_inode *ip = get_v2ip(inode); |
| 1288 | struct gfs2_holder i_gh; |
| 1289 | struct gfs2_file *fp; |
| 1290 | int error; |
| 1291 | |
| 1292 | atomic_inc(&ip->i_sbd->sd_ops_file); |
| 1293 | |
| 1294 | fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL); |
| 1295 | if (!fp) |
| 1296 | return -ENOMEM; |
| 1297 | |
| 1298 | init_MUTEX(&fp->f_fl_mutex); |
| 1299 | |
| 1300 | fp->f_inode = ip; |
| 1301 | fp->f_vfile = file; |
| 1302 | |
| 1303 | gfs2_assert_warn(ip->i_sbd, !get_v2fp(file)); |
| 1304 | set_v2fp(file, fp); |
| 1305 | |
| 1306 | if (S_ISREG(ip->i_di.di_mode)) { |
| 1307 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, |
| 1308 | &i_gh); |
| 1309 | if (error) |
| 1310 | goto fail; |
| 1311 | |
| 1312 | if (!(file->f_flags & O_LARGEFILE) && |
| 1313 | ip->i_di.di_size > MAX_NON_LFS) { |
| 1314 | error = -EFBIG; |
| 1315 | goto fail_gunlock; |
| 1316 | } |
| 1317 | |
| 1318 | /* Listen to the Direct I/O flag */ |
| 1319 | |
| 1320 | if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO) |
| 1321 | file->f_flags |= O_DIRECT; |
| 1322 | |
| 1323 | /* Don't let the user open O_DIRECT on a jdata file */ |
| 1324 | |
| 1325 | if ((file->f_flags & O_DIRECT) && gfs2_is_jdata(ip)) { |
| 1326 | error = -EINVAL; |
| 1327 | goto fail_gunlock; |
| 1328 | } |
| 1329 | |
| 1330 | gfs2_glock_dq_uninit(&i_gh); |
| 1331 | } |
| 1332 | |
| 1333 | return 0; |
| 1334 | |
| 1335 | fail_gunlock: |
| 1336 | gfs2_glock_dq_uninit(&i_gh); |
| 1337 | |
| 1338 | fail: |
| 1339 | set_v2fp(file, NULL); |
| 1340 | kfree(fp); |
| 1341 | |
| 1342 | return error; |
| 1343 | } |
| 1344 | |
| 1345 | /** |
| 1346 | * gfs2_close - called to close a struct file |
| 1347 | * @inode: the inode the struct file belongs to |
| 1348 | * @file: the struct file being closed |
| 1349 | * |
| 1350 | * Returns: errno |
| 1351 | */ |
| 1352 | |
| 1353 | static int gfs2_close(struct inode *inode, struct file *file) |
| 1354 | { |
| 1355 | struct gfs2_sbd *sdp = get_v2sdp(inode->i_sb); |
| 1356 | struct gfs2_file *fp; |
| 1357 | |
| 1358 | atomic_inc(&sdp->sd_ops_file); |
| 1359 | |
| 1360 | fp = get_v2fp(file); |
| 1361 | set_v2fp(file, NULL); |
| 1362 | |
| 1363 | if (gfs2_assert_warn(sdp, fp)) |
| 1364 | return -EIO; |
| 1365 | |
| 1366 | kfree(fp); |
| 1367 | |
| 1368 | return 0; |
| 1369 | } |
| 1370 | |
| 1371 | /** |
| 1372 | * gfs2_fsync - sync the dirty data for a file (across the cluster) |
| 1373 | * @file: the file that points to the dentry (we ignore this) |
| 1374 | * @dentry: the dentry that points to the inode to sync |
| 1375 | * |
| 1376 | * Returns: errno |
| 1377 | */ |
| 1378 | |
| 1379 | static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync) |
| 1380 | { |
| 1381 | struct gfs2_inode *ip = get_v2ip(dentry->d_inode); |
| 1382 | |
| 1383 | atomic_inc(&ip->i_sbd->sd_ops_file); |
| 1384 | gfs2_log_flush_glock(ip->i_gl); |
| 1385 | |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
| 1389 | /** |
| 1390 | * gfs2_lock - acquire/release a posix lock on a file |
| 1391 | * @file: the file pointer |
| 1392 | * @cmd: either modify or retrieve lock state, possibly wait |
| 1393 | * @fl: type and range of lock |
| 1394 | * |
| 1395 | * Returns: errno |
| 1396 | */ |
| 1397 | |
| 1398 | static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl) |
| 1399 | { |
| 1400 | struct gfs2_inode *ip = get_v2ip(file->f_mapping->host); |
| 1401 | struct gfs2_sbd *sdp = ip->i_sbd; |
| 1402 | struct lm_lockname name = |
| 1403 | { .ln_number = ip->i_num.no_addr, |
| 1404 | .ln_type = LM_TYPE_PLOCK }; |
| 1405 | |
| 1406 | atomic_inc(&sdp->sd_ops_file); |
| 1407 | |
| 1408 | if (!(fl->fl_flags & FL_POSIX)) |
| 1409 | return -ENOLCK; |
| 1410 | if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID) |
| 1411 | return -ENOLCK; |
| 1412 | |
| 1413 | if (sdp->sd_args.ar_localflocks) { |
| 1414 | if (IS_GETLK(cmd)) { |
| 1415 | struct file_lock *tmp; |
| 1416 | lock_kernel(); |
| 1417 | tmp = posix_test_lock(file, fl); |
| 1418 | fl->fl_type = F_UNLCK; |
| 1419 | if (tmp) |
| 1420 | memcpy(fl, tmp, sizeof(struct file_lock)); |
| 1421 | unlock_kernel(); |
| 1422 | return 0; |
| 1423 | } else { |
| 1424 | int error; |
| 1425 | lock_kernel(); |
| 1426 | error = posix_lock_file_wait(file, fl); |
| 1427 | unlock_kernel(); |
| 1428 | return error; |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | if (IS_GETLK(cmd)) |
| 1433 | return gfs2_lm_plock_get(sdp, &name, file, fl); |
| 1434 | else if (fl->fl_type == F_UNLCK) |
| 1435 | return gfs2_lm_punlock(sdp, &name, file, fl); |
| 1436 | else |
| 1437 | return gfs2_lm_plock(sdp, &name, file, cmd, fl); |
| 1438 | } |
| 1439 | |
| 1440 | /** |
| 1441 | * gfs2_sendfile - Send bytes to a file or socket |
| 1442 | * @in_file: The file to read from |
| 1443 | * @out_file: The file to write to |
| 1444 | * @count: The amount of data |
| 1445 | * @offset: The beginning file offset |
| 1446 | * |
| 1447 | * Outputs: offset - updated according to number of bytes read |
| 1448 | * |
| 1449 | * Returns: The number of bytes sent, errno on failure |
| 1450 | */ |
| 1451 | |
| 1452 | static ssize_t gfs2_sendfile(struct file *in_file, loff_t *offset, size_t count, |
| 1453 | read_actor_t actor, void *target) |
| 1454 | { |
| 1455 | struct gfs2_inode *ip = get_v2ip(in_file->f_mapping->host); |
| 1456 | struct gfs2_holder gh; |
| 1457 | ssize_t retval; |
| 1458 | |
| 1459 | atomic_inc(&ip->i_sbd->sd_ops_file); |
| 1460 | |
| 1461 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh); |
| 1462 | |
| 1463 | retval = gfs2_glock_nq_atime(&gh); |
| 1464 | if (retval) |
| 1465 | goto out; |
| 1466 | |
| 1467 | if (gfs2_is_jdata(ip)) |
| 1468 | retval = -EOPNOTSUPP; |
| 1469 | else |
| 1470 | retval = generic_file_sendfile(in_file, offset, count, actor, |
| 1471 | target); |
| 1472 | |
| 1473 | gfs2_glock_dq(&gh); |
| 1474 | |
| 1475 | out: |
| 1476 | gfs2_holder_uninit(&gh); |
| 1477 | |
| 1478 | return retval; |
| 1479 | } |
| 1480 | |
| 1481 | static int do_flock(struct file *file, int cmd, struct file_lock *fl) |
| 1482 | { |
| 1483 | struct gfs2_file *fp = get_v2fp(file); |
| 1484 | struct gfs2_holder *fl_gh = &fp->f_fl_gh; |
| 1485 | struct gfs2_inode *ip = fp->f_inode; |
| 1486 | struct gfs2_glock *gl; |
| 1487 | unsigned int state; |
| 1488 | int flags; |
| 1489 | int error = 0; |
| 1490 | |
| 1491 | state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED; |
| 1492 | flags = ((IS_SETLKW(cmd)) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE; |
| 1493 | |
| 1494 | down(&fp->f_fl_mutex); |
| 1495 | |
| 1496 | gl = fl_gh->gh_gl; |
| 1497 | if (gl) { |
| 1498 | if (fl_gh->gh_state == state) |
| 1499 | goto out; |
| 1500 | gfs2_glock_hold(gl); |
| 1501 | flock_lock_file_wait(file, |
| 1502 | &(struct file_lock){.fl_type = F_UNLCK}); |
| 1503 | gfs2_glock_dq_uninit(fl_gh); |
| 1504 | } else { |
| 1505 | error = gfs2_glock_get(ip->i_sbd, |
| 1506 | ip->i_num.no_addr, &gfs2_flock_glops, |
| 1507 | CREATE, &gl); |
| 1508 | if (error) |
| 1509 | goto out; |
| 1510 | } |
| 1511 | |
| 1512 | gfs2_holder_init(gl, state, flags, fl_gh); |
| 1513 | gfs2_glock_put(gl); |
| 1514 | |
| 1515 | error = gfs2_glock_nq(fl_gh); |
| 1516 | if (error) { |
| 1517 | gfs2_holder_uninit(fl_gh); |
| 1518 | if (error == GLR_TRYFAILED) |
| 1519 | error = -EAGAIN; |
| 1520 | } else { |
| 1521 | error = flock_lock_file_wait(file, fl); |
| 1522 | gfs2_assert_warn(ip->i_sbd, !error); |
| 1523 | } |
| 1524 | |
| 1525 | out: |
| 1526 | up(&fp->f_fl_mutex); |
| 1527 | |
| 1528 | return error; |
| 1529 | } |
| 1530 | |
| 1531 | static void do_unflock(struct file *file, struct file_lock *fl) |
| 1532 | { |
| 1533 | struct gfs2_file *fp = get_v2fp(file); |
| 1534 | struct gfs2_holder *fl_gh = &fp->f_fl_gh; |
| 1535 | |
| 1536 | down(&fp->f_fl_mutex); |
| 1537 | flock_lock_file_wait(file, fl); |
| 1538 | if (fl_gh->gh_gl) |
| 1539 | gfs2_glock_dq_uninit(fl_gh); |
| 1540 | up(&fp->f_fl_mutex); |
| 1541 | } |
| 1542 | |
| 1543 | /** |
| 1544 | * gfs2_flock - acquire/release a flock lock on a file |
| 1545 | * @file: the file pointer |
| 1546 | * @cmd: either modify or retrieve lock state, possibly wait |
| 1547 | * @fl: type and range of lock |
| 1548 | * |
| 1549 | * Returns: errno |
| 1550 | */ |
| 1551 | |
| 1552 | static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl) |
| 1553 | { |
| 1554 | struct gfs2_inode *ip = get_v2ip(file->f_mapping->host); |
| 1555 | struct gfs2_sbd *sdp = ip->i_sbd; |
| 1556 | |
| 1557 | atomic_inc(&ip->i_sbd->sd_ops_file); |
| 1558 | |
| 1559 | if (!(fl->fl_flags & FL_FLOCK)) |
| 1560 | return -ENOLCK; |
| 1561 | if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID) |
| 1562 | return -ENOLCK; |
| 1563 | |
| 1564 | if (sdp->sd_args.ar_localflocks) |
| 1565 | return flock_lock_file_wait(file, fl); |
| 1566 | |
| 1567 | if (fl->fl_type == F_UNLCK) { |
| 1568 | do_unflock(file, fl); |
| 1569 | return 0; |
| 1570 | } else |
| 1571 | return do_flock(file, cmd, fl); |
| 1572 | } |
| 1573 | |
| 1574 | struct file_operations gfs2_file_fops = { |
| 1575 | .llseek = gfs2_llseek, |
| 1576 | .read = gfs2_read, |
| 1577 | .write = gfs2_write, |
| 1578 | .ioctl = gfs2_ioctl, |
| 1579 | .mmap = gfs2_mmap, |
| 1580 | .open = gfs2_open, |
| 1581 | .release = gfs2_close, |
| 1582 | .fsync = gfs2_fsync, |
| 1583 | .lock = gfs2_lock, |
| 1584 | .sendfile = gfs2_sendfile, |
| 1585 | .flock = gfs2_flock, |
| 1586 | }; |
| 1587 | |
| 1588 | struct file_operations gfs2_dir_fops = { |
| 1589 | .readdir = gfs2_readdir, |
| 1590 | .ioctl = gfs2_ioctl, |
| 1591 | .open = gfs2_open, |
| 1592 | .release = gfs2_close, |
| 1593 | .fsync = gfs2_fsync, |
| 1594 | .lock = gfs2_lock, |
| 1595 | .flock = gfs2_flock, |
| 1596 | }; |
| 1597 | |