Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1 | /* -*- mode: c; c-basic-offset: 8; -*- |
| 2 | * vim: noexpandtab sw=8 ts=8 sts=0: |
| 3 | * |
| 4 | * file.c |
| 5 | * |
| 6 | * File open, close, extend, truncate |
| 7 | * |
| 8 | * Copyright (C) 2002, 2004 Oracle. All rights reserved. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public |
| 21 | * License along with this program; if not, write to the |
| 22 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 | * Boston, MA 021110-1307, USA. |
| 24 | */ |
| 25 | |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 26 | #include <linux/capability.h> |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 27 | #include <linux/fs.h> |
| 28 | #include <linux/types.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/highmem.h> |
| 31 | #include <linux/pagemap.h> |
| 32 | #include <linux/uio.h> |
Mark Fasheh | e2057c5 | 2006-10-03 17:53:05 -0700 | [diff] [blame] | 33 | #include <linux/sched.h> |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 34 | |
| 35 | #define MLOG_MASK_PREFIX ML_INODE |
| 36 | #include <cluster/masklog.h> |
| 37 | |
| 38 | #include "ocfs2.h" |
| 39 | |
| 40 | #include "alloc.h" |
| 41 | #include "aops.h" |
| 42 | #include "dir.h" |
| 43 | #include "dlmglue.h" |
| 44 | #include "extent_map.h" |
| 45 | #include "file.h" |
| 46 | #include "sysfile.h" |
| 47 | #include "inode.h" |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 48 | #include "ioctl.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 49 | #include "journal.h" |
| 50 | #include "mmap.h" |
| 51 | #include "suballoc.h" |
| 52 | #include "super.h" |
| 53 | |
| 54 | #include "buffer_head_io.h" |
| 55 | |
| 56 | static int ocfs2_sync_inode(struct inode *inode) |
| 57 | { |
| 58 | filemap_fdatawrite(inode->i_mapping); |
| 59 | return sync_mapping_buffers(inode->i_mapping); |
| 60 | } |
| 61 | |
| 62 | static int ocfs2_file_open(struct inode *inode, struct file *file) |
| 63 | { |
| 64 | int status; |
| 65 | int mode = file->f_flags; |
| 66 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 67 | |
| 68 | mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file, |
| 69 | file->f_dentry->d_name.len, file->f_dentry->d_name.name); |
| 70 | |
| 71 | spin_lock(&oi->ip_lock); |
| 72 | |
| 73 | /* Check that the inode hasn't been wiped from disk by another |
| 74 | * node. If it hasn't then we're safe as long as we hold the |
| 75 | * spin lock until our increment of open count. */ |
| 76 | if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) { |
| 77 | spin_unlock(&oi->ip_lock); |
| 78 | |
| 79 | status = -ENOENT; |
| 80 | goto leave; |
| 81 | } |
| 82 | |
| 83 | if (mode & O_DIRECT) |
| 84 | oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT; |
| 85 | |
| 86 | oi->ip_open_count++; |
| 87 | spin_unlock(&oi->ip_lock); |
| 88 | status = 0; |
| 89 | leave: |
| 90 | mlog_exit(status); |
| 91 | return status; |
| 92 | } |
| 93 | |
| 94 | static int ocfs2_file_release(struct inode *inode, struct file *file) |
| 95 | { |
| 96 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 97 | |
| 98 | mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file, |
| 99 | file->f_dentry->d_name.len, |
| 100 | file->f_dentry->d_name.name); |
| 101 | |
| 102 | spin_lock(&oi->ip_lock); |
| 103 | if (!--oi->ip_open_count) |
| 104 | oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT; |
| 105 | spin_unlock(&oi->ip_lock); |
| 106 | |
| 107 | mlog_exit(0); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int ocfs2_sync_file(struct file *file, |
| 113 | struct dentry *dentry, |
| 114 | int datasync) |
| 115 | { |
| 116 | int err = 0; |
| 117 | journal_t *journal; |
| 118 | struct inode *inode = dentry->d_inode; |
| 119 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 120 | |
| 121 | mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync, |
| 122 | dentry->d_name.len, dentry->d_name.name); |
| 123 | |
| 124 | err = ocfs2_sync_inode(dentry->d_inode); |
| 125 | if (err) |
| 126 | goto bail; |
| 127 | |
| 128 | journal = osb->journal->j_journal; |
| 129 | err = journal_force_commit(journal); |
| 130 | |
| 131 | bail: |
| 132 | mlog_exit(err); |
| 133 | |
| 134 | return (err < 0) ? -EIO : 0; |
| 135 | } |
| 136 | |
| 137 | int ocfs2_set_inode_size(struct ocfs2_journal_handle *handle, |
| 138 | struct inode *inode, |
| 139 | struct buffer_head *fe_bh, |
| 140 | u64 new_i_size) |
| 141 | { |
| 142 | int status; |
| 143 | |
| 144 | mlog_entry_void(); |
| 145 | i_size_write(inode, new_i_size); |
| 146 | inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size); |
| 147 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
| 148 | |
| 149 | status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); |
| 150 | if (status < 0) { |
| 151 | mlog_errno(status); |
| 152 | goto bail; |
| 153 | } |
| 154 | |
| 155 | bail: |
| 156 | mlog_exit(status); |
| 157 | return status; |
| 158 | } |
| 159 | |
| 160 | static int ocfs2_simple_size_update(struct inode *inode, |
| 161 | struct buffer_head *di_bh, |
| 162 | u64 new_i_size) |
| 163 | { |
| 164 | int ret; |
| 165 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 166 | struct ocfs2_journal_handle *handle = NULL; |
| 167 | |
| 168 | handle = ocfs2_start_trans(osb, NULL, |
| 169 | OCFS2_INODE_UPDATE_CREDITS); |
| 170 | if (handle == NULL) { |
| 171 | ret = -ENOMEM; |
| 172 | mlog_errno(ret); |
| 173 | goto out; |
| 174 | } |
| 175 | |
| 176 | ret = ocfs2_set_inode_size(handle, inode, di_bh, |
| 177 | new_i_size); |
| 178 | if (ret < 0) |
| 179 | mlog_errno(ret); |
| 180 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame^] | 181 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 182 | out: |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb, |
| 187 | struct inode *inode, |
| 188 | struct buffer_head *fe_bh, |
| 189 | u64 new_i_size) |
| 190 | { |
| 191 | int status; |
| 192 | struct ocfs2_journal_handle *handle; |
| 193 | |
| 194 | mlog_entry_void(); |
| 195 | |
| 196 | /* TODO: This needs to actually orphan the inode in this |
| 197 | * transaction. */ |
| 198 | |
| 199 | handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS); |
| 200 | if (IS_ERR(handle)) { |
| 201 | status = PTR_ERR(handle); |
| 202 | mlog_errno(status); |
| 203 | goto out; |
| 204 | } |
| 205 | |
| 206 | status = ocfs2_set_inode_size(handle, inode, fe_bh, new_i_size); |
| 207 | if (status < 0) |
| 208 | mlog_errno(status); |
| 209 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame^] | 210 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 211 | out: |
| 212 | mlog_exit(status); |
| 213 | return status; |
| 214 | } |
| 215 | |
| 216 | static int ocfs2_truncate_file(struct inode *inode, |
| 217 | struct buffer_head *di_bh, |
| 218 | u64 new_i_size) |
| 219 | { |
| 220 | int status = 0; |
| 221 | struct ocfs2_dinode *fe = NULL; |
| 222 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 223 | struct ocfs2_truncate_context *tc = NULL; |
| 224 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 225 | mlog_entry("(inode = %llu, new_i_size = %llu\n", |
| 226 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 227 | (unsigned long long)new_i_size); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 228 | |
| 229 | truncate_inode_pages(inode->i_mapping, new_i_size); |
| 230 | |
| 231 | fe = (struct ocfs2_dinode *) di_bh->b_data; |
| 232 | if (!OCFS2_IS_VALID_DINODE(fe)) { |
| 233 | OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe); |
| 234 | status = -EIO; |
| 235 | goto bail; |
| 236 | } |
| 237 | |
| 238 | mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode), |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 239 | "Inode %llu, inode i_size = %lld != di " |
| 240 | "i_size = %llu, i_flags = 0x%x\n", |
| 241 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 242 | i_size_read(inode), |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 243 | (unsigned long long)le64_to_cpu(fe->i_size), |
| 244 | le32_to_cpu(fe->i_flags)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 245 | |
| 246 | if (new_i_size > le64_to_cpu(fe->i_size)) { |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 247 | mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n", |
| 248 | (unsigned long long)le64_to_cpu(fe->i_size), |
| 249 | (unsigned long long)new_i_size); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 250 | status = -EINVAL; |
| 251 | mlog_errno(status); |
| 252 | goto bail; |
| 253 | } |
| 254 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 255 | mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n", |
| 256 | (unsigned long long)le64_to_cpu(fe->i_blkno), |
| 257 | (unsigned long long)le64_to_cpu(fe->i_size), |
| 258 | (unsigned long long)new_i_size); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 259 | |
| 260 | /* lets handle the simple truncate cases before doing any more |
| 261 | * cluster locking. */ |
| 262 | if (new_i_size == le64_to_cpu(fe->i_size)) |
| 263 | goto bail; |
| 264 | |
Mark Fasheh | ab0920c | 2006-03-16 15:06:37 -0800 | [diff] [blame] | 265 | /* This forces other nodes to sync and drop their pages. Do |
| 266 | * this even if we have a truncate without allocation change - |
| 267 | * ocfs2 cluster sizes can be much greater than page size, so |
| 268 | * we have to truncate them anyway. */ |
| 269 | status = ocfs2_data_lock(inode, 1); |
| 270 | if (status < 0) { |
| 271 | mlog_errno(status); |
| 272 | goto bail; |
| 273 | } |
| 274 | ocfs2_data_unlock(inode, 1); |
| 275 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 276 | if (le32_to_cpu(fe->i_clusters) == |
| 277 | ocfs2_clusters_for_bytes(osb->sb, new_i_size)) { |
| 278 | mlog(0, "fe->i_clusters = %u, so we do a simple truncate\n", |
| 279 | fe->i_clusters); |
| 280 | /* No allocation change is required, so lets fast path |
| 281 | * this truncate. */ |
| 282 | status = ocfs2_simple_size_update(inode, di_bh, new_i_size); |
| 283 | if (status < 0) |
| 284 | mlog_errno(status); |
| 285 | goto bail; |
| 286 | } |
| 287 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 288 | /* alright, we're going to need to do a full blown alloc size |
| 289 | * change. Orphan the inode so that recovery can complete the |
| 290 | * truncate if necessary. This does the task of marking |
| 291 | * i_size. */ |
| 292 | status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size); |
| 293 | if (status < 0) { |
| 294 | mlog_errno(status); |
| 295 | goto bail; |
| 296 | } |
| 297 | |
| 298 | status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc); |
| 299 | if (status < 0) { |
| 300 | mlog_errno(status); |
| 301 | goto bail; |
| 302 | } |
| 303 | |
| 304 | status = ocfs2_commit_truncate(osb, inode, di_bh, tc); |
| 305 | if (status < 0) { |
| 306 | mlog_errno(status); |
| 307 | goto bail; |
| 308 | } |
| 309 | |
| 310 | /* TODO: orphan dir cleanup here. */ |
| 311 | bail: |
| 312 | |
| 313 | mlog_exit(status); |
| 314 | return status; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * extend allocation only here. |
| 319 | * we'll update all the disk stuff, and oip->alloc_size |
| 320 | * |
| 321 | * expect stuff to be locked, a transaction started and enough data / |
| 322 | * metadata reservations in the contexts. |
| 323 | * |
| 324 | * Will return -EAGAIN, and a reason if a restart is needed. |
| 325 | * If passed in, *reason will always be set, even in error. |
| 326 | */ |
| 327 | int ocfs2_do_extend_allocation(struct ocfs2_super *osb, |
| 328 | struct inode *inode, |
| 329 | u32 clusters_to_add, |
| 330 | struct buffer_head *fe_bh, |
| 331 | struct ocfs2_journal_handle *handle, |
| 332 | struct ocfs2_alloc_context *data_ac, |
| 333 | struct ocfs2_alloc_context *meta_ac, |
| 334 | enum ocfs2_alloc_restarted *reason_ret) |
| 335 | { |
| 336 | int status = 0; |
| 337 | int free_extents; |
| 338 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 339 | enum ocfs2_alloc_restarted reason = RESTART_NONE; |
| 340 | u32 bit_off, num_bits; |
| 341 | u64 block; |
| 342 | |
| 343 | BUG_ON(!clusters_to_add); |
| 344 | |
| 345 | free_extents = ocfs2_num_free_extents(osb, inode, fe); |
| 346 | if (free_extents < 0) { |
| 347 | status = free_extents; |
| 348 | mlog_errno(status); |
| 349 | goto leave; |
| 350 | } |
| 351 | |
| 352 | /* there are two cases which could cause us to EAGAIN in the |
| 353 | * we-need-more-metadata case: |
| 354 | * 1) we haven't reserved *any* |
| 355 | * 2) we are so fragmented, we've needed to add metadata too |
| 356 | * many times. */ |
| 357 | if (!free_extents && !meta_ac) { |
| 358 | mlog(0, "we haven't reserved any metadata!\n"); |
| 359 | status = -EAGAIN; |
| 360 | reason = RESTART_META; |
| 361 | goto leave; |
| 362 | } else if ((!free_extents) |
| 363 | && (ocfs2_alloc_context_bits_left(meta_ac) |
| 364 | < ocfs2_extend_meta_needed(fe))) { |
| 365 | mlog(0, "filesystem is really fragmented...\n"); |
| 366 | status = -EAGAIN; |
| 367 | reason = RESTART_META; |
| 368 | goto leave; |
| 369 | } |
| 370 | |
| 371 | status = ocfs2_claim_clusters(osb, handle, data_ac, 1, |
| 372 | &bit_off, &num_bits); |
| 373 | if (status < 0) { |
| 374 | if (status != -ENOSPC) |
| 375 | mlog_errno(status); |
| 376 | goto leave; |
| 377 | } |
| 378 | |
| 379 | BUG_ON(num_bits > clusters_to_add); |
| 380 | |
| 381 | /* reserve our write early -- insert_extent may update the inode */ |
| 382 | status = ocfs2_journal_access(handle, inode, fe_bh, |
| 383 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 384 | if (status < 0) { |
| 385 | mlog_errno(status); |
| 386 | goto leave; |
| 387 | } |
| 388 | |
| 389 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 390 | mlog(0, "Allocating %u clusters at block %u for inode %llu\n", |
| 391 | num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 392 | status = ocfs2_insert_extent(osb, handle, inode, fe_bh, block, |
| 393 | num_bits, meta_ac); |
| 394 | if (status < 0) { |
| 395 | mlog_errno(status); |
| 396 | goto leave; |
| 397 | } |
| 398 | |
| 399 | le32_add_cpu(&fe->i_clusters, num_bits); |
| 400 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 401 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters); |
| 402 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 403 | |
| 404 | status = ocfs2_journal_dirty(handle, fe_bh); |
| 405 | if (status < 0) { |
| 406 | mlog_errno(status); |
| 407 | goto leave; |
| 408 | } |
| 409 | |
| 410 | clusters_to_add -= num_bits; |
| 411 | |
| 412 | if (clusters_to_add) { |
| 413 | mlog(0, "need to alloc once more, clusters = %u, wanted = " |
| 414 | "%u\n", fe->i_clusters, clusters_to_add); |
| 415 | status = -EAGAIN; |
| 416 | reason = RESTART_TRANS; |
| 417 | } |
| 418 | |
| 419 | leave: |
| 420 | mlog_exit(status); |
| 421 | if (reason_ret) |
| 422 | *reason_ret = reason; |
| 423 | return status; |
| 424 | } |
| 425 | |
| 426 | static int ocfs2_extend_allocation(struct inode *inode, |
| 427 | u32 clusters_to_add) |
| 428 | { |
| 429 | int status = 0; |
| 430 | int restart_func = 0; |
| 431 | int drop_alloc_sem = 0; |
| 432 | int credits, num_free_extents; |
| 433 | u32 prev_clusters; |
| 434 | struct buffer_head *bh = NULL; |
| 435 | struct ocfs2_dinode *fe = NULL; |
| 436 | struct ocfs2_journal_handle *handle = NULL; |
| 437 | struct ocfs2_alloc_context *data_ac = NULL; |
| 438 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 439 | enum ocfs2_alloc_restarted why; |
| 440 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 441 | |
| 442 | mlog_entry("(clusters_to_add = %u)\n", clusters_to_add); |
| 443 | |
| 444 | status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh, |
| 445 | OCFS2_BH_CACHED, inode); |
| 446 | if (status < 0) { |
| 447 | mlog_errno(status); |
| 448 | goto leave; |
| 449 | } |
| 450 | |
| 451 | fe = (struct ocfs2_dinode *) bh->b_data; |
| 452 | if (!OCFS2_IS_VALID_DINODE(fe)) { |
| 453 | OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe); |
| 454 | status = -EIO; |
| 455 | goto leave; |
| 456 | } |
| 457 | |
| 458 | restart_all: |
| 459 | BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters); |
| 460 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 461 | mlog(0, "extend inode %llu, i_size = %lld, fe->i_clusters = %u, " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 462 | "clusters_to_add = %u\n", |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 463 | (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode), |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 464 | fe->i_clusters, clusters_to_add); |
| 465 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 466 | num_free_extents = ocfs2_num_free_extents(osb, |
| 467 | inode, |
| 468 | fe); |
| 469 | if (num_free_extents < 0) { |
| 470 | status = num_free_extents; |
| 471 | mlog_errno(status); |
| 472 | goto leave; |
| 473 | } |
| 474 | |
| 475 | if (!num_free_extents) { |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 476 | status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 477 | if (status < 0) { |
| 478 | if (status != -ENOSPC) |
| 479 | mlog_errno(status); |
| 480 | goto leave; |
| 481 | } |
| 482 | } |
| 483 | |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 484 | status = ocfs2_reserve_clusters(osb, clusters_to_add, &data_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 485 | if (status < 0) { |
| 486 | if (status != -ENOSPC) |
| 487 | mlog_errno(status); |
| 488 | goto leave; |
| 489 | } |
| 490 | |
| 491 | /* blocks peope in read/write from reading our allocation |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 492 | * until we're done changing it. We depend on i_mutex to block |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 493 | * other extend/truncate calls while we're here. Ordering wrt |
| 494 | * start_trans is important here -- always do it before! */ |
| 495 | down_write(&OCFS2_I(inode)->ip_alloc_sem); |
| 496 | drop_alloc_sem = 1; |
| 497 | |
| 498 | credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add); |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 499 | handle = ocfs2_start_trans(osb, NULL, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 500 | if (IS_ERR(handle)) { |
| 501 | status = PTR_ERR(handle); |
| 502 | handle = NULL; |
| 503 | mlog_errno(status); |
| 504 | goto leave; |
| 505 | } |
| 506 | |
| 507 | restarted_transaction: |
| 508 | /* reserve a write to the file entry early on - that we if we |
| 509 | * run out of credits in the allocation path, we can still |
| 510 | * update i_size. */ |
| 511 | status = ocfs2_journal_access(handle, inode, bh, |
| 512 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 513 | if (status < 0) { |
| 514 | mlog_errno(status); |
| 515 | goto leave; |
| 516 | } |
| 517 | |
| 518 | prev_clusters = OCFS2_I(inode)->ip_clusters; |
| 519 | |
| 520 | status = ocfs2_do_extend_allocation(osb, |
| 521 | inode, |
| 522 | clusters_to_add, |
| 523 | bh, |
| 524 | handle, |
| 525 | data_ac, |
| 526 | meta_ac, |
| 527 | &why); |
| 528 | if ((status < 0) && (status != -EAGAIN)) { |
| 529 | if (status != -ENOSPC) |
| 530 | mlog_errno(status); |
| 531 | goto leave; |
| 532 | } |
| 533 | |
| 534 | status = ocfs2_journal_dirty(handle, bh); |
| 535 | if (status < 0) { |
| 536 | mlog_errno(status); |
| 537 | goto leave; |
| 538 | } |
| 539 | |
| 540 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 541 | clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters); |
| 542 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 543 | |
| 544 | if (why != RESTART_NONE && clusters_to_add) { |
| 545 | if (why == RESTART_META) { |
| 546 | mlog(0, "restarting function.\n"); |
| 547 | restart_func = 1; |
| 548 | } else { |
| 549 | BUG_ON(why != RESTART_TRANS); |
| 550 | |
| 551 | mlog(0, "restarting transaction.\n"); |
| 552 | /* TODO: This can be more intelligent. */ |
| 553 | credits = ocfs2_calc_extend_credits(osb->sb, |
| 554 | fe, |
| 555 | clusters_to_add); |
Mark Fasheh | 1fc5814 | 2006-10-05 14:15:36 -0700 | [diff] [blame] | 556 | status = ocfs2_extend_trans(handle->k_handle, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 557 | if (status < 0) { |
| 558 | /* handle still has to be committed at |
| 559 | * this point. */ |
| 560 | status = -ENOMEM; |
| 561 | mlog_errno(status); |
| 562 | goto leave; |
| 563 | } |
| 564 | goto restarted_transaction; |
| 565 | } |
| 566 | } |
| 567 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 568 | mlog(0, "fe: i_clusters = %u, i_size=%llu\n", |
| 569 | fe->i_clusters, (unsigned long long)fe->i_size); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 570 | mlog(0, "inode: ip_clusters=%u, i_size=%lld\n", |
| 571 | OCFS2_I(inode)->ip_clusters, i_size_read(inode)); |
| 572 | |
| 573 | leave: |
| 574 | if (drop_alloc_sem) { |
| 575 | up_write(&OCFS2_I(inode)->ip_alloc_sem); |
| 576 | drop_alloc_sem = 0; |
| 577 | } |
| 578 | if (handle) { |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame^] | 579 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 580 | handle = NULL; |
| 581 | } |
| 582 | if (data_ac) { |
| 583 | ocfs2_free_alloc_context(data_ac); |
| 584 | data_ac = NULL; |
| 585 | } |
| 586 | if (meta_ac) { |
| 587 | ocfs2_free_alloc_context(meta_ac); |
| 588 | meta_ac = NULL; |
| 589 | } |
| 590 | if ((!status) && restart_func) { |
| 591 | restart_func = 0; |
| 592 | goto restart_all; |
| 593 | } |
| 594 | if (bh) { |
| 595 | brelse(bh); |
| 596 | bh = NULL; |
| 597 | } |
| 598 | |
| 599 | mlog_exit(status); |
| 600 | return status; |
| 601 | } |
| 602 | |
| 603 | /* Some parts of this taken from generic_cont_expand, which turned out |
| 604 | * to be too fragile to do exactly what we need without us having to |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 605 | * worry about recursive locking in ->prepare_write() and |
| 606 | * ->commit_write(). */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 607 | static int ocfs2_write_zero_page(struct inode *inode, |
| 608 | u64 size) |
| 609 | { |
| 610 | struct address_space *mapping = inode->i_mapping; |
| 611 | struct page *page; |
| 612 | unsigned long index; |
| 613 | unsigned int offset; |
| 614 | struct ocfs2_journal_handle *handle = NULL; |
| 615 | int ret; |
| 616 | |
| 617 | offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */ |
| 618 | /* ugh. in prepare/commit_write, if from==to==start of block, we |
| 619 | ** skip the prepare. make sure we never send an offset for the start |
| 620 | ** of a block |
| 621 | */ |
| 622 | if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) { |
| 623 | offset++; |
| 624 | } |
| 625 | index = size >> PAGE_CACHE_SHIFT; |
| 626 | |
| 627 | page = grab_cache_page(mapping, index); |
| 628 | if (!page) { |
| 629 | ret = -ENOMEM; |
| 630 | mlog_errno(ret); |
| 631 | goto out; |
| 632 | } |
| 633 | |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 634 | ret = ocfs2_prepare_write_nolock(inode, page, offset, offset); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 635 | if (ret < 0) { |
| 636 | mlog_errno(ret); |
| 637 | goto out_unlock; |
| 638 | } |
| 639 | |
| 640 | if (ocfs2_should_order_data(inode)) { |
| 641 | handle = ocfs2_start_walk_page_trans(inode, page, offset, |
| 642 | offset); |
| 643 | if (IS_ERR(handle)) { |
| 644 | ret = PTR_ERR(handle); |
| 645 | handle = NULL; |
| 646 | goto out_unlock; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | /* must not update i_size! */ |
| 651 | ret = block_commit_write(page, offset, offset); |
| 652 | if (ret < 0) |
| 653 | mlog_errno(ret); |
| 654 | else |
| 655 | ret = 0; |
| 656 | |
| 657 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame^] | 658 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 659 | out_unlock: |
| 660 | unlock_page(page); |
| 661 | page_cache_release(page); |
| 662 | out: |
| 663 | return ret; |
| 664 | } |
| 665 | |
| 666 | static int ocfs2_zero_extend(struct inode *inode, |
| 667 | u64 zero_to_size) |
| 668 | { |
| 669 | int ret = 0; |
| 670 | u64 start_off; |
| 671 | struct super_block *sb = inode->i_sb; |
| 672 | |
| 673 | start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode)); |
| 674 | while (start_off < zero_to_size) { |
| 675 | ret = ocfs2_write_zero_page(inode, start_off); |
| 676 | if (ret < 0) { |
| 677 | mlog_errno(ret); |
| 678 | goto out; |
| 679 | } |
| 680 | |
| 681 | start_off += sb->s_blocksize; |
Mark Fasheh | e2057c5 | 2006-10-03 17:53:05 -0700 | [diff] [blame] | 682 | |
| 683 | /* |
| 684 | * Very large extends have the potential to lock up |
| 685 | * the cpu for extended periods of time. |
| 686 | */ |
| 687 | cond_resched(); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | out: |
| 691 | return ret; |
| 692 | } |
| 693 | |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 694 | /* |
| 695 | * A tail_to_skip value > 0 indicates that we're being called from |
| 696 | * ocfs2_file_aio_write(). This has the following implications: |
| 697 | * |
| 698 | * - we don't want to update i_size |
| 699 | * - di_bh will be NULL, which is fine because it's only used in the |
| 700 | * case where we want to update i_size. |
| 701 | * - ocfs2_zero_extend() will then only be filling the hole created |
| 702 | * between i_size and the start of the write. |
| 703 | */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 704 | static int ocfs2_extend_file(struct inode *inode, |
| 705 | struct buffer_head *di_bh, |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 706 | u64 new_i_size, |
| 707 | size_t tail_to_skip) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 708 | { |
| 709 | int ret = 0; |
| 710 | u32 clusters_to_add; |
| 711 | |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 712 | BUG_ON(!tail_to_skip && !di_bh); |
| 713 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 714 | /* setattr sometimes calls us like this. */ |
| 715 | if (new_i_size == 0) |
| 716 | goto out; |
| 717 | |
| 718 | if (i_size_read(inode) == new_i_size) |
| 719 | goto out; |
| 720 | BUG_ON(new_i_size < i_size_read(inode)); |
| 721 | |
| 722 | clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) - |
| 723 | OCFS2_I(inode)->ip_clusters; |
| 724 | |
Mark Fasheh | 0effef7 | 2006-10-03 17:44:42 -0700 | [diff] [blame] | 725 | /* |
| 726 | * protect the pages that ocfs2_zero_extend is going to be |
| 727 | * pulling into the page cache.. we do this before the |
| 728 | * metadata extend so that we don't get into the situation |
| 729 | * where we've extended the metadata but can't get the data |
| 730 | * lock to zero. |
| 731 | */ |
| 732 | ret = ocfs2_data_lock(inode, 1); |
| 733 | if (ret < 0) { |
| 734 | mlog_errno(ret); |
| 735 | goto out; |
| 736 | } |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 737 | |
Mark Fasheh | 0effef7 | 2006-10-03 17:44:42 -0700 | [diff] [blame] | 738 | if (clusters_to_add) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 739 | ret = ocfs2_extend_allocation(inode, clusters_to_add); |
| 740 | if (ret < 0) { |
| 741 | mlog_errno(ret); |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 742 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 743 | } |
Mark Fasheh | 0effef7 | 2006-10-03 17:44:42 -0700 | [diff] [blame] | 744 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 745 | |
Mark Fasheh | 0effef7 | 2006-10-03 17:44:42 -0700 | [diff] [blame] | 746 | /* |
| 747 | * Call this even if we don't add any clusters to the tree. We |
| 748 | * still need to zero the area between the old i_size and the |
| 749 | * new i_size. |
| 750 | */ |
| 751 | ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip); |
| 752 | if (ret < 0) { |
| 753 | mlog_errno(ret); |
| 754 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 755 | } |
| 756 | |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 757 | if (!tail_to_skip) { |
| 758 | /* We're being called from ocfs2_setattr() which wants |
| 759 | * us to update i_size */ |
| 760 | ret = ocfs2_simple_size_update(inode, di_bh, new_i_size); |
| 761 | if (ret < 0) |
| 762 | mlog_errno(ret); |
| 763 | } |
| 764 | |
| 765 | out_unlock: |
Mark Fasheh | 0effef7 | 2006-10-03 17:44:42 -0700 | [diff] [blame] | 766 | ocfs2_data_unlock(inode, 1); |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 767 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 768 | out: |
| 769 | return ret; |
| 770 | } |
| 771 | |
| 772 | int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) |
| 773 | { |
| 774 | int status = 0, size_change; |
| 775 | struct inode *inode = dentry->d_inode; |
| 776 | struct super_block *sb = inode->i_sb; |
| 777 | struct ocfs2_super *osb = OCFS2_SB(sb); |
| 778 | struct buffer_head *bh = NULL; |
| 779 | struct ocfs2_journal_handle *handle = NULL; |
| 780 | |
| 781 | mlog_entry("(0x%p, '%.*s')\n", dentry, |
| 782 | dentry->d_name.len, dentry->d_name.name); |
| 783 | |
| 784 | if (attr->ia_valid & ATTR_MODE) |
| 785 | mlog(0, "mode change: %d\n", attr->ia_mode); |
| 786 | if (attr->ia_valid & ATTR_UID) |
| 787 | mlog(0, "uid change: %d\n", attr->ia_uid); |
| 788 | if (attr->ia_valid & ATTR_GID) |
| 789 | mlog(0, "gid change: %d\n", attr->ia_gid); |
| 790 | if (attr->ia_valid & ATTR_SIZE) |
| 791 | mlog(0, "size change...\n"); |
| 792 | if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME)) |
| 793 | mlog(0, "time change...\n"); |
| 794 | |
| 795 | #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \ |
| 796 | | ATTR_GID | ATTR_UID | ATTR_MODE) |
| 797 | if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) { |
| 798 | mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid); |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | status = inode_change_ok(inode, attr); |
| 803 | if (status) |
| 804 | return status; |
| 805 | |
| 806 | size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE; |
| 807 | if (size_change) { |
| 808 | status = ocfs2_rw_lock(inode, 1); |
| 809 | if (status < 0) { |
| 810 | mlog_errno(status); |
| 811 | goto bail; |
| 812 | } |
| 813 | } |
| 814 | |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 815 | status = ocfs2_meta_lock(inode, &bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 816 | if (status < 0) { |
| 817 | if (status != -ENOENT) |
| 818 | mlog_errno(status); |
| 819 | goto bail_unlock_rw; |
| 820 | } |
| 821 | |
| 822 | if (size_change && attr->ia_size != i_size_read(inode)) { |
| 823 | if (i_size_read(inode) > attr->ia_size) |
| 824 | status = ocfs2_truncate_file(inode, bh, attr->ia_size); |
| 825 | else |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 826 | status = ocfs2_extend_file(inode, bh, attr->ia_size, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 827 | if (status < 0) { |
| 828 | if (status != -ENOSPC) |
| 829 | mlog_errno(status); |
| 830 | status = -ENOSPC; |
| 831 | goto bail_unlock; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS); |
| 836 | if (IS_ERR(handle)) { |
| 837 | status = PTR_ERR(handle); |
| 838 | mlog_errno(status); |
| 839 | goto bail_unlock; |
| 840 | } |
| 841 | |
| 842 | status = inode_setattr(inode, attr); |
| 843 | if (status < 0) { |
| 844 | mlog_errno(status); |
| 845 | goto bail_commit; |
| 846 | } |
| 847 | |
| 848 | status = ocfs2_mark_inode_dirty(handle, inode, bh); |
| 849 | if (status < 0) |
| 850 | mlog_errno(status); |
| 851 | |
| 852 | bail_commit: |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame^] | 853 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 854 | bail_unlock: |
| 855 | ocfs2_meta_unlock(inode, 1); |
| 856 | bail_unlock_rw: |
| 857 | if (size_change) |
| 858 | ocfs2_rw_unlock(inode, 1); |
| 859 | bail: |
| 860 | if (bh) |
| 861 | brelse(bh); |
| 862 | |
| 863 | mlog_exit(status); |
| 864 | return status; |
| 865 | } |
| 866 | |
| 867 | int ocfs2_getattr(struct vfsmount *mnt, |
| 868 | struct dentry *dentry, |
| 869 | struct kstat *stat) |
| 870 | { |
| 871 | struct inode *inode = dentry->d_inode; |
| 872 | struct super_block *sb = dentry->d_inode->i_sb; |
| 873 | struct ocfs2_super *osb = sb->s_fs_info; |
| 874 | int err; |
| 875 | |
| 876 | mlog_entry_void(); |
| 877 | |
| 878 | err = ocfs2_inode_revalidate(dentry); |
| 879 | if (err) { |
| 880 | if (err != -ENOENT) |
| 881 | mlog_errno(err); |
| 882 | goto bail; |
| 883 | } |
| 884 | |
| 885 | generic_fillattr(inode, stat); |
| 886 | |
| 887 | /* We set the blksize from the cluster size for performance */ |
| 888 | stat->blksize = osb->s_clustersize; |
| 889 | |
| 890 | bail: |
| 891 | mlog_exit(err); |
| 892 | |
| 893 | return err; |
| 894 | } |
| 895 | |
| 896 | static int ocfs2_write_remove_suid(struct inode *inode) |
| 897 | { |
| 898 | int ret; |
| 899 | struct buffer_head *bh = NULL; |
| 900 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 901 | struct ocfs2_journal_handle *handle; |
| 902 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 903 | struct ocfs2_dinode *di; |
| 904 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 905 | mlog_entry("(Inode %llu, mode 0%o)\n", |
| 906 | (unsigned long long)oi->ip_blkno, inode->i_mode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 907 | |
| 908 | handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS); |
| 909 | if (handle == NULL) { |
| 910 | ret = -ENOMEM; |
| 911 | mlog_errno(ret); |
| 912 | goto out; |
| 913 | } |
| 914 | |
| 915 | ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode); |
| 916 | if (ret < 0) { |
| 917 | mlog_errno(ret); |
| 918 | goto out_trans; |
| 919 | } |
| 920 | |
| 921 | ret = ocfs2_journal_access(handle, inode, bh, |
| 922 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 923 | if (ret < 0) { |
| 924 | mlog_errno(ret); |
| 925 | goto out_bh; |
| 926 | } |
| 927 | |
| 928 | inode->i_mode &= ~S_ISUID; |
| 929 | if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP)) |
| 930 | inode->i_mode &= ~S_ISGID; |
| 931 | |
| 932 | di = (struct ocfs2_dinode *) bh->b_data; |
| 933 | di->i_mode = cpu_to_le16(inode->i_mode); |
| 934 | |
| 935 | ret = ocfs2_journal_dirty(handle, bh); |
| 936 | if (ret < 0) |
| 937 | mlog_errno(ret); |
| 938 | out_bh: |
| 939 | brelse(bh); |
| 940 | out_trans: |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame^] | 941 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 942 | out: |
| 943 | mlog_exit(ret); |
| 944 | return ret; |
| 945 | } |
| 946 | |
| 947 | static inline int ocfs2_write_should_remove_suid(struct inode *inode) |
| 948 | { |
| 949 | mode_t mode = inode->i_mode; |
| 950 | |
| 951 | if (!capable(CAP_FSETID)) { |
| 952 | if (unlikely(mode & S_ISUID)) |
| 953 | return 1; |
| 954 | |
| 955 | if (unlikely((mode & S_ISGID) && (mode & S_IXGRP))) |
| 956 | return 1; |
| 957 | } |
| 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | static ssize_t ocfs2_file_aio_write(struct kiocb *iocb, |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 962 | const struct iovec *iov, |
| 963 | unsigned long nr_segs, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 964 | loff_t pos) |
| 965 | { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 966 | int ret, rw_level = -1, meta_level = -1, have_alloc_sem = 0; |
| 967 | u32 clusters; |
| 968 | struct file *filp = iocb->ki_filp; |
| 969 | struct inode *inode = filp->f_dentry->d_inode; |
| 970 | loff_t newsize, saved_pos; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 971 | |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 972 | mlog_entry("(0x%p, %u, '%.*s')\n", filp, |
| 973 | (unsigned int)nr_segs, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 974 | filp->f_dentry->d_name.len, |
| 975 | filp->f_dentry->d_name.name); |
| 976 | |
| 977 | /* happy write of zero bytes */ |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 978 | if (iocb->ki_left == 0) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 979 | return 0; |
| 980 | |
| 981 | if (!inode) { |
| 982 | mlog(0, "bad inode\n"); |
| 983 | return -EIO; |
| 984 | } |
| 985 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 986 | mutex_lock(&inode->i_mutex); |
| 987 | /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 988 | if (filp->f_flags & O_DIRECT) { |
| 989 | have_alloc_sem = 1; |
| 990 | down_read(&inode->i_alloc_sem); |
| 991 | } |
| 992 | |
| 993 | /* concurrent O_DIRECT writes are allowed */ |
| 994 | rw_level = (filp->f_flags & O_DIRECT) ? 0 : 1; |
| 995 | ret = ocfs2_rw_lock(inode, rw_level); |
| 996 | if (ret < 0) { |
| 997 | rw_level = -1; |
| 998 | mlog_errno(ret); |
| 999 | goto out; |
| 1000 | } |
| 1001 | |
| 1002 | /* |
| 1003 | * We sample i_size under a read level meta lock to see if our write |
| 1004 | * is extending the file, if it is we back off and get a write level |
| 1005 | * meta lock. |
| 1006 | */ |
| 1007 | meta_level = (filp->f_flags & O_APPEND) ? 1 : 0; |
| 1008 | for(;;) { |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 1009 | ret = ocfs2_meta_lock(inode, NULL, meta_level); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1010 | if (ret < 0) { |
| 1011 | meta_level = -1; |
| 1012 | mlog_errno(ret); |
| 1013 | goto out; |
| 1014 | } |
| 1015 | |
| 1016 | /* Clear suid / sgid if necessary. We do this here |
| 1017 | * instead of later in the write path because |
| 1018 | * remove_suid() calls ->setattr without any hint that |
| 1019 | * we may have already done our cluster locking. Since |
| 1020 | * ocfs2_setattr() *must* take cluster locks to |
| 1021 | * proceeed, this will lead us to recursively lock the |
| 1022 | * inode. There's also the dinode i_size state which |
| 1023 | * can be lost via setattr during extending writes (we |
| 1024 | * set inode->i_size at the end of a write. */ |
| 1025 | if (ocfs2_write_should_remove_suid(inode)) { |
| 1026 | if (meta_level == 0) { |
| 1027 | ocfs2_meta_unlock(inode, meta_level); |
| 1028 | meta_level = 1; |
| 1029 | continue; |
| 1030 | } |
| 1031 | |
| 1032 | ret = ocfs2_write_remove_suid(inode); |
| 1033 | if (ret < 0) { |
| 1034 | mlog_errno(ret); |
| 1035 | goto out; |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | /* work on a copy of ppos until we're sure that we won't have |
| 1040 | * to recalculate it due to relocking. */ |
| 1041 | if (filp->f_flags & O_APPEND) { |
| 1042 | saved_pos = i_size_read(inode); |
| 1043 | mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos); |
| 1044 | } else { |
| 1045 | saved_pos = iocb->ki_pos; |
| 1046 | } |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 1047 | newsize = iocb->ki_left + saved_pos; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1048 | |
Mark Fasheh | 215c7f9 | 2006-02-01 16:42:10 -0800 | [diff] [blame] | 1049 | mlog(0, "pos=%lld newsize=%lld cursize=%lld\n", |
| 1050 | (long long) saved_pos, (long long) newsize, |
| 1051 | (long long) i_size_read(inode)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1052 | |
| 1053 | /* No need for a higher level metadata lock if we're |
| 1054 | * never going past i_size. */ |
| 1055 | if (newsize <= i_size_read(inode)) |
| 1056 | break; |
| 1057 | |
| 1058 | if (meta_level == 0) { |
| 1059 | ocfs2_meta_unlock(inode, meta_level); |
| 1060 | meta_level = 1; |
| 1061 | continue; |
| 1062 | } |
| 1063 | |
| 1064 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 1065 | clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) - |
| 1066 | OCFS2_I(inode)->ip_clusters; |
| 1067 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 1068 | |
| 1069 | mlog(0, "Writing at EOF, may need more allocation: " |
Mark Fasheh | 215c7f9 | 2006-02-01 16:42:10 -0800 | [diff] [blame] | 1070 | "i_size = %lld, newsize = %lld, need %u clusters\n", |
| 1071 | (long long) i_size_read(inode), (long long) newsize, |
| 1072 | clusters); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1073 | |
| 1074 | /* We only want to continue the rest of this loop if |
| 1075 | * our extend will actually require more |
| 1076 | * allocation. */ |
| 1077 | if (!clusters) |
| 1078 | break; |
| 1079 | |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 1080 | ret = ocfs2_extend_file(inode, NULL, newsize, iocb->ki_left); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1081 | if (ret < 0) { |
| 1082 | if (ret != -ENOSPC) |
| 1083 | mlog_errno(ret); |
| 1084 | goto out; |
| 1085 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1086 | break; |
| 1087 | } |
| 1088 | |
| 1089 | /* ok, we're done with i_size and alloc work */ |
| 1090 | iocb->ki_pos = saved_pos; |
| 1091 | ocfs2_meta_unlock(inode, meta_level); |
| 1092 | meta_level = -1; |
| 1093 | |
| 1094 | /* communicate with ocfs2_dio_end_io */ |
| 1095 | ocfs2_iocb_set_rw_locked(iocb); |
| 1096 | |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 1097 | ret = generic_file_aio_write_nolock(iocb, iov, nr_segs, iocb->ki_pos); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1098 | |
| 1099 | /* buffered aio wouldn't have proper lock coverage today */ |
| 1100 | BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT)); |
| 1101 | |
| 1102 | /* |
| 1103 | * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io |
| 1104 | * function pointer which is called when o_direct io completes so that |
| 1105 | * it can unlock our rw lock. (it's the clustered equivalent of |
| 1106 | * i_alloc_sem; protects truncate from racing with pending ios). |
| 1107 | * Unfortunately there are error cases which call end_io and others |
| 1108 | * that don't. so we don't have to unlock the rw_lock if either an |
| 1109 | * async dio is going to do it in the future or an end_io after an |
| 1110 | * error has already done it. |
| 1111 | */ |
| 1112 | if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) { |
| 1113 | rw_level = -1; |
| 1114 | have_alloc_sem = 0; |
| 1115 | } |
| 1116 | |
| 1117 | out: |
| 1118 | if (meta_level != -1) |
| 1119 | ocfs2_meta_unlock(inode, meta_level); |
| 1120 | if (have_alloc_sem) |
| 1121 | up_read(&inode->i_alloc_sem); |
| 1122 | if (rw_level != -1) |
| 1123 | ocfs2_rw_unlock(inode, rw_level); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1124 | mutex_unlock(&inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1125 | |
| 1126 | mlog_exit(ret); |
| 1127 | return ret; |
| 1128 | } |
| 1129 | |
| 1130 | static ssize_t ocfs2_file_aio_read(struct kiocb *iocb, |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 1131 | const struct iovec *iov, |
| 1132 | unsigned long nr_segs, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1133 | loff_t pos) |
| 1134 | { |
| 1135 | int ret = 0, rw_level = -1, have_alloc_sem = 0; |
| 1136 | struct file *filp = iocb->ki_filp; |
| 1137 | struct inode *inode = filp->f_dentry->d_inode; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1138 | |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 1139 | mlog_entry("(0x%p, %u, '%.*s')\n", filp, |
| 1140 | (unsigned int)nr_segs, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1141 | filp->f_dentry->d_name.len, |
| 1142 | filp->f_dentry->d_name.name); |
| 1143 | |
| 1144 | if (!inode) { |
| 1145 | ret = -EINVAL; |
| 1146 | mlog_errno(ret); |
| 1147 | goto bail; |
| 1148 | } |
| 1149 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1150 | /* |
| 1151 | * buffered reads protect themselves in ->readpage(). O_DIRECT reads |
| 1152 | * need locks to protect pending reads from racing with truncate. |
| 1153 | */ |
| 1154 | if (filp->f_flags & O_DIRECT) { |
| 1155 | down_read(&inode->i_alloc_sem); |
| 1156 | have_alloc_sem = 1; |
| 1157 | |
| 1158 | ret = ocfs2_rw_lock(inode, 0); |
| 1159 | if (ret < 0) { |
| 1160 | mlog_errno(ret); |
| 1161 | goto bail; |
| 1162 | } |
| 1163 | rw_level = 0; |
| 1164 | /* communicate with ocfs2_dio_end_io */ |
| 1165 | ocfs2_iocb_set_rw_locked(iocb); |
| 1166 | } |
| 1167 | |
Mark Fasheh | c4374f8 | 2006-05-05 19:04:35 -0700 | [diff] [blame] | 1168 | /* |
| 1169 | * We're fine letting folks race truncates and extending |
| 1170 | * writes with read across the cluster, just like they can |
| 1171 | * locally. Hence no rw_lock during read. |
| 1172 | * |
| 1173 | * Take and drop the meta data lock to update inode fields |
| 1174 | * like i_size. This allows the checks down below |
| 1175 | * generic_file_aio_read() a chance of actually working. |
| 1176 | */ |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 1177 | ret = ocfs2_meta_lock(inode, NULL, 0); |
Mark Fasheh | c4374f8 | 2006-05-05 19:04:35 -0700 | [diff] [blame] | 1178 | if (ret < 0) { |
| 1179 | mlog_errno(ret); |
| 1180 | goto bail; |
| 1181 | } |
| 1182 | ocfs2_meta_unlock(inode, 0); |
| 1183 | |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 1184 | ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1185 | if (ret == -EINVAL) |
| 1186 | mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n"); |
| 1187 | |
| 1188 | /* buffered aio wouldn't have proper lock coverage today */ |
| 1189 | BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT)); |
| 1190 | |
| 1191 | /* see ocfs2_file_aio_write */ |
| 1192 | if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) { |
| 1193 | rw_level = -1; |
| 1194 | have_alloc_sem = 0; |
| 1195 | } |
| 1196 | |
| 1197 | bail: |
| 1198 | if (have_alloc_sem) |
| 1199 | up_read(&inode->i_alloc_sem); |
| 1200 | if (rw_level != -1) |
| 1201 | ocfs2_rw_unlock(inode, rw_level); |
| 1202 | mlog_exit(ret); |
| 1203 | |
| 1204 | return ret; |
| 1205 | } |
| 1206 | |
| 1207 | struct inode_operations ocfs2_file_iops = { |
| 1208 | .setattr = ocfs2_setattr, |
| 1209 | .getattr = ocfs2_getattr, |
| 1210 | }; |
| 1211 | |
| 1212 | struct inode_operations ocfs2_special_file_iops = { |
| 1213 | .setattr = ocfs2_setattr, |
| 1214 | .getattr = ocfs2_getattr, |
| 1215 | }; |
| 1216 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1217 | const struct file_operations ocfs2_fops = { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1218 | .read = do_sync_read, |
| 1219 | .write = do_sync_write, |
| 1220 | .sendfile = generic_file_sendfile, |
| 1221 | .mmap = ocfs2_mmap, |
| 1222 | .fsync = ocfs2_sync_file, |
| 1223 | .release = ocfs2_file_release, |
| 1224 | .open = ocfs2_file_open, |
| 1225 | .aio_read = ocfs2_file_aio_read, |
| 1226 | .aio_write = ocfs2_file_aio_write, |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 1227 | .ioctl = ocfs2_ioctl, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1228 | }; |
| 1229 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1230 | const struct file_operations ocfs2_dops = { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1231 | .read = generic_read_dir, |
| 1232 | .readdir = ocfs2_readdir, |
| 1233 | .fsync = ocfs2_sync_file, |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 1234 | .ioctl = ocfs2_ioctl, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1235 | }; |