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