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