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