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 | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 36 | #include <linux/writeback.h> |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 37 | |
| 38 | #define MLOG_MASK_PREFIX ML_INODE |
| 39 | #include <cluster/masklog.h> |
| 40 | |
| 41 | #include "ocfs2.h" |
| 42 | |
| 43 | #include "alloc.h" |
| 44 | #include "aops.h" |
| 45 | #include "dir.h" |
| 46 | #include "dlmglue.h" |
| 47 | #include "extent_map.h" |
| 48 | #include "file.h" |
| 49 | #include "sysfile.h" |
| 50 | #include "inode.h" |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 51 | #include "ioctl.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 52 | #include "journal.h" |
| 53 | #include "mmap.h" |
| 54 | #include "suballoc.h" |
| 55 | #include "super.h" |
| 56 | |
| 57 | #include "buffer_head_io.h" |
| 58 | |
| 59 | static int ocfs2_sync_inode(struct inode *inode) |
| 60 | { |
| 61 | filemap_fdatawrite(inode->i_mapping); |
| 62 | return sync_mapping_buffers(inode->i_mapping); |
| 63 | } |
| 64 | |
| 65 | static int ocfs2_file_open(struct inode *inode, struct file *file) |
| 66 | { |
| 67 | int status; |
| 68 | int mode = file->f_flags; |
| 69 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 70 | |
| 71 | mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file, |
Josef Sipek | d28c917 | 2006-12-08 02:37:25 -0800 | [diff] [blame] | 72 | 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] | 73 | |
| 74 | spin_lock(&oi->ip_lock); |
| 75 | |
| 76 | /* Check that the inode hasn't been wiped from disk by another |
| 77 | * node. If it hasn't then we're safe as long as we hold the |
| 78 | * spin lock until our increment of open count. */ |
| 79 | if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) { |
| 80 | spin_unlock(&oi->ip_lock); |
| 81 | |
| 82 | status = -ENOENT; |
| 83 | goto leave; |
| 84 | } |
| 85 | |
| 86 | if (mode & O_DIRECT) |
| 87 | oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT; |
| 88 | |
| 89 | oi->ip_open_count++; |
| 90 | spin_unlock(&oi->ip_lock); |
| 91 | status = 0; |
| 92 | leave: |
| 93 | mlog_exit(status); |
| 94 | return status; |
| 95 | } |
| 96 | |
| 97 | static int ocfs2_file_release(struct inode *inode, struct file *file) |
| 98 | { |
| 99 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 100 | |
| 101 | mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file, |
Josef Sipek | d28c917 | 2006-12-08 02:37:25 -0800 | [diff] [blame] | 102 | file->f_path.dentry->d_name.len, |
| 103 | file->f_path.dentry->d_name.name); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 104 | |
| 105 | spin_lock(&oi->ip_lock); |
| 106 | if (!--oi->ip_open_count) |
| 107 | oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT; |
| 108 | spin_unlock(&oi->ip_lock); |
| 109 | |
| 110 | mlog_exit(0); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int ocfs2_sync_file(struct file *file, |
| 116 | struct dentry *dentry, |
| 117 | int datasync) |
| 118 | { |
| 119 | int err = 0; |
| 120 | journal_t *journal; |
| 121 | struct inode *inode = dentry->d_inode; |
| 122 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 123 | |
| 124 | mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync, |
| 125 | dentry->d_name.len, dentry->d_name.name); |
| 126 | |
| 127 | err = ocfs2_sync_inode(dentry->d_inode); |
| 128 | if (err) |
| 129 | goto bail; |
| 130 | |
| 131 | journal = osb->journal->j_journal; |
| 132 | err = journal_force_commit(journal); |
| 133 | |
| 134 | bail: |
| 135 | mlog_exit(err); |
| 136 | |
| 137 | return (err < 0) ? -EIO : 0; |
| 138 | } |
| 139 | |
Tiger Yang | 7f1a37e | 2006-11-15 15:48:42 +0800 | [diff] [blame] | 140 | int ocfs2_should_update_atime(struct inode *inode, |
| 141 | struct vfsmount *vfsmnt) |
| 142 | { |
| 143 | struct timespec now; |
| 144 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 145 | |
| 146 | if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) |
| 147 | return 0; |
| 148 | |
| 149 | if ((inode->i_flags & S_NOATIME) || |
| 150 | ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))) |
| 151 | return 0; |
| 152 | |
Mark Fasheh | 6c2aad0 | 2006-12-19 15:25:52 -0800 | [diff] [blame] | 153 | /* |
| 154 | * We can be called with no vfsmnt structure - NFSD will |
| 155 | * sometimes do this. |
| 156 | * |
| 157 | * Note that our action here is different than touch_atime() - |
| 158 | * if we can't tell whether this is a noatime mount, then we |
| 159 | * don't know whether to trust the value of s_atime_quantum. |
| 160 | */ |
| 161 | if (vfsmnt == NULL) |
| 162 | return 0; |
| 163 | |
Tiger Yang | 7f1a37e | 2006-11-15 15:48:42 +0800 | [diff] [blame] | 164 | if ((vfsmnt->mnt_flags & MNT_NOATIME) || |
| 165 | ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))) |
| 166 | return 0; |
| 167 | |
Mark Fasheh | 7e913c5 | 2006-12-13 00:34:35 -0800 | [diff] [blame] | 168 | if (vfsmnt->mnt_flags & MNT_RELATIME) { |
| 169 | if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) || |
| 170 | (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0)) |
| 171 | return 1; |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
Tiger Yang | 7f1a37e | 2006-11-15 15:48:42 +0800 | [diff] [blame] | 176 | now = CURRENT_TIME; |
| 177 | if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum)) |
| 178 | return 0; |
| 179 | else |
| 180 | return 1; |
| 181 | } |
| 182 | |
| 183 | int ocfs2_update_inode_atime(struct inode *inode, |
| 184 | struct buffer_head *bh) |
| 185 | { |
| 186 | int ret; |
| 187 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 188 | handle_t *handle; |
| 189 | |
| 190 | mlog_entry_void(); |
| 191 | |
| 192 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
| 193 | if (handle == NULL) { |
| 194 | ret = -ENOMEM; |
| 195 | mlog_errno(ret); |
| 196 | goto out; |
| 197 | } |
| 198 | |
| 199 | inode->i_atime = CURRENT_TIME; |
| 200 | ret = ocfs2_mark_inode_dirty(handle, inode, bh); |
| 201 | if (ret < 0) |
| 202 | mlog_errno(ret); |
| 203 | |
| 204 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); |
| 205 | out: |
| 206 | mlog_exit(ret); |
| 207 | return ret; |
| 208 | } |
| 209 | |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 210 | int ocfs2_set_inode_size(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 211 | struct inode *inode, |
| 212 | struct buffer_head *fe_bh, |
| 213 | u64 new_i_size) |
| 214 | { |
| 215 | int status; |
| 216 | |
| 217 | mlog_entry_void(); |
| 218 | i_size_write(inode, new_i_size); |
| 219 | inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size); |
| 220 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
| 221 | |
| 222 | status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); |
| 223 | if (status < 0) { |
| 224 | mlog_errno(status); |
| 225 | goto bail; |
| 226 | } |
| 227 | |
| 228 | bail: |
| 229 | mlog_exit(status); |
| 230 | return status; |
| 231 | } |
| 232 | |
| 233 | static int ocfs2_simple_size_update(struct inode *inode, |
| 234 | struct buffer_head *di_bh, |
| 235 | u64 new_i_size) |
| 236 | { |
| 237 | int ret; |
| 238 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 239 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 240 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 241 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 242 | if (handle == NULL) { |
| 243 | ret = -ENOMEM; |
| 244 | mlog_errno(ret); |
| 245 | goto out; |
| 246 | } |
| 247 | |
| 248 | ret = ocfs2_set_inode_size(handle, inode, di_bh, |
| 249 | new_i_size); |
| 250 | if (ret < 0) |
| 251 | mlog_errno(ret); |
| 252 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 253 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 254 | out: |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb, |
| 259 | struct inode *inode, |
| 260 | struct buffer_head *fe_bh, |
| 261 | u64 new_i_size) |
| 262 | { |
| 263 | int status; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 264 | handle_t *handle; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 265 | struct ocfs2_dinode *di; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 266 | |
| 267 | mlog_entry_void(); |
| 268 | |
| 269 | /* TODO: This needs to actually orphan the inode in this |
| 270 | * transaction. */ |
| 271 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 272 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 273 | if (IS_ERR(handle)) { |
| 274 | status = PTR_ERR(handle); |
| 275 | mlog_errno(status); |
| 276 | goto out; |
| 277 | } |
| 278 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 279 | status = ocfs2_journal_access(handle, inode, fe_bh, |
| 280 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 281 | if (status < 0) { |
| 282 | mlog_errno(status); |
| 283 | goto out_commit; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Do this before setting i_size. |
| 288 | */ |
| 289 | status = ocfs2_zero_tail_for_truncate(inode, handle, new_i_size); |
| 290 | if (status) { |
| 291 | mlog_errno(status); |
| 292 | goto out_commit; |
| 293 | } |
| 294 | |
| 295 | i_size_write(inode, new_i_size); |
| 296 | inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size); |
| 297 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
| 298 | |
| 299 | di = (struct ocfs2_dinode *) fe_bh->b_data; |
| 300 | di->i_size = cpu_to_le64(new_i_size); |
| 301 | di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 302 | di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 303 | |
| 304 | status = ocfs2_journal_dirty(handle, fe_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 305 | if (status < 0) |
| 306 | mlog_errno(status); |
| 307 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 308 | out_commit: |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 309 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 310 | out: |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 311 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 312 | mlog_exit(status); |
| 313 | return status; |
| 314 | } |
| 315 | |
| 316 | static int ocfs2_truncate_file(struct inode *inode, |
| 317 | struct buffer_head *di_bh, |
| 318 | u64 new_i_size) |
| 319 | { |
| 320 | int status = 0; |
| 321 | struct ocfs2_dinode *fe = NULL; |
| 322 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 323 | struct ocfs2_truncate_context *tc = NULL; |
| 324 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 325 | mlog_entry("(inode = %llu, new_i_size = %llu\n", |
| 326 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 327 | (unsigned long long)new_i_size); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 328 | |
| 329 | truncate_inode_pages(inode->i_mapping, new_i_size); |
| 330 | |
| 331 | fe = (struct ocfs2_dinode *) di_bh->b_data; |
| 332 | if (!OCFS2_IS_VALID_DINODE(fe)) { |
| 333 | OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe); |
| 334 | status = -EIO; |
| 335 | goto bail; |
| 336 | } |
| 337 | |
| 338 | 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] | 339 | "Inode %llu, inode i_size = %lld != di " |
| 340 | "i_size = %llu, i_flags = 0x%x\n", |
| 341 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 342 | i_size_read(inode), |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 343 | (unsigned long long)le64_to_cpu(fe->i_size), |
| 344 | le32_to_cpu(fe->i_flags)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 345 | |
| 346 | if (new_i_size > le64_to_cpu(fe->i_size)) { |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 347 | mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n", |
| 348 | (unsigned long long)le64_to_cpu(fe->i_size), |
| 349 | (unsigned long long)new_i_size); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 350 | status = -EINVAL; |
| 351 | mlog_errno(status); |
| 352 | goto bail; |
| 353 | } |
| 354 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 355 | mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n", |
| 356 | (unsigned long long)le64_to_cpu(fe->i_blkno), |
| 357 | (unsigned long long)le64_to_cpu(fe->i_size), |
| 358 | (unsigned long long)new_i_size); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 359 | |
| 360 | /* lets handle the simple truncate cases before doing any more |
| 361 | * cluster locking. */ |
| 362 | if (new_i_size == le64_to_cpu(fe->i_size)) |
| 363 | goto bail; |
| 364 | |
Mark Fasheh | ab0920c | 2006-03-16 15:06:37 -0800 | [diff] [blame] | 365 | /* This forces other nodes to sync and drop their pages. Do |
| 366 | * this even if we have a truncate without allocation change - |
| 367 | * ocfs2 cluster sizes can be much greater than page size, so |
| 368 | * we have to truncate them anyway. */ |
| 369 | status = ocfs2_data_lock(inode, 1); |
| 370 | if (status < 0) { |
| 371 | mlog_errno(status); |
| 372 | goto bail; |
| 373 | } |
Mark Fasheh | ab0920c | 2006-03-16 15:06:37 -0800 | [diff] [blame] | 374 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 375 | /* alright, we're going to need to do a full blown alloc size |
| 376 | * change. Orphan the inode so that recovery can complete the |
| 377 | * truncate if necessary. This does the task of marking |
| 378 | * i_size. */ |
| 379 | status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size); |
| 380 | if (status < 0) { |
| 381 | mlog_errno(status); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 382 | goto bail_unlock_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc); |
| 386 | if (status < 0) { |
| 387 | mlog_errno(status); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 388 | goto bail_unlock_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | status = ocfs2_commit_truncate(osb, inode, di_bh, tc); |
| 392 | if (status < 0) { |
| 393 | mlog_errno(status); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 394 | goto bail_unlock_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /* TODO: orphan dir cleanup here. */ |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 398 | bail_unlock_data: |
| 399 | ocfs2_data_unlock(inode, 1); |
| 400 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 401 | bail: |
| 402 | |
| 403 | mlog_exit(status); |
| 404 | return status; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * extend allocation only here. |
| 409 | * we'll update all the disk stuff, and oip->alloc_size |
| 410 | * |
| 411 | * expect stuff to be locked, a transaction started and enough data / |
| 412 | * metadata reservations in the contexts. |
| 413 | * |
| 414 | * Will return -EAGAIN, and a reason if a restart is needed. |
| 415 | * If passed in, *reason will always be set, even in error. |
| 416 | */ |
| 417 | int ocfs2_do_extend_allocation(struct ocfs2_super *osb, |
| 418 | struct inode *inode, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 419 | u32 *logical_offset, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 420 | u32 clusters_to_add, |
| 421 | struct buffer_head *fe_bh, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 422 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 423 | struct ocfs2_alloc_context *data_ac, |
| 424 | struct ocfs2_alloc_context *meta_ac, |
| 425 | enum ocfs2_alloc_restarted *reason_ret) |
| 426 | { |
| 427 | int status = 0; |
| 428 | int free_extents; |
| 429 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 430 | enum ocfs2_alloc_restarted reason = RESTART_NONE; |
| 431 | u32 bit_off, num_bits; |
| 432 | u64 block; |
| 433 | |
| 434 | BUG_ON(!clusters_to_add); |
| 435 | |
| 436 | free_extents = ocfs2_num_free_extents(osb, inode, fe); |
| 437 | if (free_extents < 0) { |
| 438 | status = free_extents; |
| 439 | mlog_errno(status); |
| 440 | goto leave; |
| 441 | } |
| 442 | |
| 443 | /* there are two cases which could cause us to EAGAIN in the |
| 444 | * we-need-more-metadata case: |
| 445 | * 1) we haven't reserved *any* |
| 446 | * 2) we are so fragmented, we've needed to add metadata too |
| 447 | * many times. */ |
| 448 | if (!free_extents && !meta_ac) { |
| 449 | mlog(0, "we haven't reserved any metadata!\n"); |
| 450 | status = -EAGAIN; |
| 451 | reason = RESTART_META; |
| 452 | goto leave; |
| 453 | } else if ((!free_extents) |
| 454 | && (ocfs2_alloc_context_bits_left(meta_ac) |
| 455 | < ocfs2_extend_meta_needed(fe))) { |
| 456 | mlog(0, "filesystem is really fragmented...\n"); |
| 457 | status = -EAGAIN; |
| 458 | reason = RESTART_META; |
| 459 | goto leave; |
| 460 | } |
| 461 | |
| 462 | status = ocfs2_claim_clusters(osb, handle, data_ac, 1, |
| 463 | &bit_off, &num_bits); |
| 464 | if (status < 0) { |
| 465 | if (status != -ENOSPC) |
| 466 | mlog_errno(status); |
| 467 | goto leave; |
| 468 | } |
| 469 | |
| 470 | BUG_ON(num_bits > clusters_to_add); |
| 471 | |
| 472 | /* reserve our write early -- insert_extent may update the inode */ |
| 473 | status = ocfs2_journal_access(handle, inode, fe_bh, |
| 474 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 475 | if (status < 0) { |
| 476 | mlog_errno(status); |
| 477 | goto leave; |
| 478 | } |
| 479 | |
| 480 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 481 | mlog(0, "Allocating %u clusters at block %u for inode %llu\n", |
| 482 | num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 483 | status = ocfs2_insert_extent(osb, handle, inode, fe_bh, |
| 484 | *logical_offset, block, num_bits, |
| 485 | meta_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 486 | if (status < 0) { |
| 487 | mlog_errno(status); |
| 488 | goto leave; |
| 489 | } |
| 490 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 491 | status = ocfs2_journal_dirty(handle, fe_bh); |
| 492 | if (status < 0) { |
| 493 | mlog_errno(status); |
| 494 | goto leave; |
| 495 | } |
| 496 | |
| 497 | clusters_to_add -= num_bits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 498 | *logical_offset += num_bits; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 499 | |
| 500 | if (clusters_to_add) { |
| 501 | mlog(0, "need to alloc once more, clusters = %u, wanted = " |
| 502 | "%u\n", fe->i_clusters, clusters_to_add); |
| 503 | status = -EAGAIN; |
| 504 | reason = RESTART_TRANS; |
| 505 | } |
| 506 | |
| 507 | leave: |
| 508 | mlog_exit(status); |
| 509 | if (reason_ret) |
| 510 | *reason_ret = reason; |
| 511 | return status; |
| 512 | } |
| 513 | |
Mark Fasheh | abf8b15 | 2007-01-17 13:07:24 -0800 | [diff] [blame] | 514 | /* |
| 515 | * For a given allocation, determine which allocators will need to be |
| 516 | * accessed, and lock them, reserving the appropriate number of bits. |
| 517 | * |
| 518 | * Called from ocfs2_extend_allocation() for file systems which don't |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 519 | * support holes, and from ocfs2_write() for file systems which |
| 520 | * understand sparse inodes. |
Mark Fasheh | abf8b15 | 2007-01-17 13:07:24 -0800 | [diff] [blame] | 521 | */ |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 522 | int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di, |
| 523 | u32 clusters_to_add, |
| 524 | struct ocfs2_alloc_context **data_ac, |
| 525 | struct ocfs2_alloc_context **meta_ac) |
Mark Fasheh | abf8b15 | 2007-01-17 13:07:24 -0800 | [diff] [blame] | 526 | { |
| 527 | int ret, num_free_extents; |
| 528 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 529 | |
| 530 | *meta_ac = NULL; |
| 531 | *data_ac = NULL; |
| 532 | |
| 533 | mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, " |
| 534 | "clusters_to_add = %u\n", |
| 535 | (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode), |
| 536 | le32_to_cpu(di->i_clusters), clusters_to_add); |
| 537 | |
| 538 | num_free_extents = ocfs2_num_free_extents(osb, inode, di); |
| 539 | if (num_free_extents < 0) { |
| 540 | ret = num_free_extents; |
| 541 | mlog_errno(ret); |
| 542 | goto out; |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * Sparse allocation file systems need to be more conservative |
| 547 | * with reserving room for expansion - the actual allocation |
| 548 | * happens while we've got a journal handle open so re-taking |
| 549 | * a cluster lock (because we ran out of room for another |
| 550 | * extent) will violate ordering rules. |
| 551 | * |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 552 | * Most of the time we'll only be seeing this 1 cluster at a time |
Mark Fasheh | abf8b15 | 2007-01-17 13:07:24 -0800 | [diff] [blame] | 553 | * anyway. |
| 554 | */ |
| 555 | if (!num_free_extents || |
| 556 | (ocfs2_sparse_alloc(osb) && num_free_extents < clusters_to_add)) { |
| 557 | ret = ocfs2_reserve_new_metadata(osb, di, meta_ac); |
| 558 | if (ret < 0) { |
| 559 | if (ret != -ENOSPC) |
| 560 | mlog_errno(ret); |
| 561 | goto out; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac); |
| 566 | if (ret < 0) { |
| 567 | if (ret != -ENOSPC) |
| 568 | mlog_errno(ret); |
| 569 | goto out; |
| 570 | } |
| 571 | |
| 572 | out: |
| 573 | if (ret) { |
| 574 | if (*meta_ac) { |
| 575 | ocfs2_free_alloc_context(*meta_ac); |
| 576 | *meta_ac = NULL; |
| 577 | } |
| 578 | |
| 579 | /* |
| 580 | * We cannot have an error and a non null *data_ac. |
| 581 | */ |
| 582 | } |
| 583 | |
| 584 | return ret; |
| 585 | } |
| 586 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 587 | static int ocfs2_extend_allocation(struct inode *inode, |
| 588 | u32 clusters_to_add) |
| 589 | { |
| 590 | int status = 0; |
| 591 | int restart_func = 0; |
| 592 | int drop_alloc_sem = 0; |
Mark Fasheh | abf8b15 | 2007-01-17 13:07:24 -0800 | [diff] [blame] | 593 | int credits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 594 | u32 prev_clusters, logical_start; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 595 | struct buffer_head *bh = NULL; |
| 596 | struct ocfs2_dinode *fe = NULL; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 597 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 598 | struct ocfs2_alloc_context *data_ac = NULL; |
| 599 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 600 | enum ocfs2_alloc_restarted why; |
| 601 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 602 | |
| 603 | mlog_entry("(clusters_to_add = %u)\n", clusters_to_add); |
| 604 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 605 | /* |
| 606 | * This function only exists for file systems which don't |
| 607 | * support holes. |
| 608 | */ |
| 609 | BUG_ON(ocfs2_sparse_alloc(osb)); |
| 610 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 611 | status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh, |
| 612 | OCFS2_BH_CACHED, inode); |
| 613 | if (status < 0) { |
| 614 | mlog_errno(status); |
| 615 | goto leave; |
| 616 | } |
| 617 | |
| 618 | fe = (struct ocfs2_dinode *) bh->b_data; |
| 619 | if (!OCFS2_IS_VALID_DINODE(fe)) { |
| 620 | OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe); |
| 621 | status = -EIO; |
| 622 | goto leave; |
| 623 | } |
| 624 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 625 | logical_start = OCFS2_I(inode)->ip_clusters; |
| 626 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 627 | restart_all: |
| 628 | BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters); |
| 629 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 630 | /* blocks peope in read/write from reading our allocation |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 631 | * 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] | 632 | * other extend/truncate calls while we're here. Ordering wrt |
| 633 | * start_trans is important here -- always do it before! */ |
| 634 | down_write(&OCFS2_I(inode)->ip_alloc_sem); |
| 635 | drop_alloc_sem = 1; |
| 636 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 637 | status = ocfs2_lock_allocators(inode, fe, clusters_to_add, &data_ac, |
| 638 | &meta_ac); |
| 639 | if (status) { |
| 640 | mlog_errno(status); |
| 641 | goto leave; |
| 642 | } |
| 643 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 644 | credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add); |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 645 | handle = ocfs2_start_trans(osb, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 646 | if (IS_ERR(handle)) { |
| 647 | status = PTR_ERR(handle); |
| 648 | handle = NULL; |
| 649 | mlog_errno(status); |
| 650 | goto leave; |
| 651 | } |
| 652 | |
| 653 | restarted_transaction: |
| 654 | /* reserve a write to the file entry early on - that we if we |
| 655 | * run out of credits in the allocation path, we can still |
| 656 | * update i_size. */ |
| 657 | status = ocfs2_journal_access(handle, inode, bh, |
| 658 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 659 | if (status < 0) { |
| 660 | mlog_errno(status); |
| 661 | goto leave; |
| 662 | } |
| 663 | |
| 664 | prev_clusters = OCFS2_I(inode)->ip_clusters; |
| 665 | |
| 666 | status = ocfs2_do_extend_allocation(osb, |
| 667 | inode, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 668 | &logical_start, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 669 | clusters_to_add, |
| 670 | bh, |
| 671 | handle, |
| 672 | data_ac, |
| 673 | meta_ac, |
| 674 | &why); |
| 675 | if ((status < 0) && (status != -EAGAIN)) { |
| 676 | if (status != -ENOSPC) |
| 677 | mlog_errno(status); |
| 678 | goto leave; |
| 679 | } |
| 680 | |
| 681 | status = ocfs2_journal_dirty(handle, bh); |
| 682 | if (status < 0) { |
| 683 | mlog_errno(status); |
| 684 | goto leave; |
| 685 | } |
| 686 | |
| 687 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 688 | clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters); |
| 689 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 690 | |
| 691 | if (why != RESTART_NONE && clusters_to_add) { |
| 692 | if (why == RESTART_META) { |
| 693 | mlog(0, "restarting function.\n"); |
| 694 | restart_func = 1; |
| 695 | } else { |
| 696 | BUG_ON(why != RESTART_TRANS); |
| 697 | |
| 698 | mlog(0, "restarting transaction.\n"); |
| 699 | /* TODO: This can be more intelligent. */ |
| 700 | credits = ocfs2_calc_extend_credits(osb->sb, |
| 701 | fe, |
| 702 | clusters_to_add); |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 703 | status = ocfs2_extend_trans(handle, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 704 | if (status < 0) { |
| 705 | /* handle still has to be committed at |
| 706 | * this point. */ |
| 707 | status = -ENOMEM; |
| 708 | mlog_errno(status); |
| 709 | goto leave; |
| 710 | } |
| 711 | goto restarted_transaction; |
| 712 | } |
| 713 | } |
| 714 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 715 | mlog(0, "fe: i_clusters = %u, i_size=%llu\n", |
| 716 | fe->i_clusters, (unsigned long long)fe->i_size); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 717 | mlog(0, "inode: ip_clusters=%u, i_size=%lld\n", |
| 718 | OCFS2_I(inode)->ip_clusters, i_size_read(inode)); |
| 719 | |
| 720 | leave: |
| 721 | if (drop_alloc_sem) { |
| 722 | up_write(&OCFS2_I(inode)->ip_alloc_sem); |
| 723 | drop_alloc_sem = 0; |
| 724 | } |
| 725 | if (handle) { |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 726 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 727 | handle = NULL; |
| 728 | } |
| 729 | if (data_ac) { |
| 730 | ocfs2_free_alloc_context(data_ac); |
| 731 | data_ac = NULL; |
| 732 | } |
| 733 | if (meta_ac) { |
| 734 | ocfs2_free_alloc_context(meta_ac); |
| 735 | meta_ac = NULL; |
| 736 | } |
| 737 | if ((!status) && restart_func) { |
| 738 | restart_func = 0; |
| 739 | goto restart_all; |
| 740 | } |
| 741 | if (bh) { |
| 742 | brelse(bh); |
| 743 | bh = NULL; |
| 744 | } |
| 745 | |
| 746 | mlog_exit(status); |
| 747 | return status; |
| 748 | } |
| 749 | |
| 750 | /* Some parts of this taken from generic_cont_expand, which turned out |
| 751 | * 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] | 752 | * worry about recursive locking in ->prepare_write() and |
| 753 | * ->commit_write(). */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 754 | static int ocfs2_write_zero_page(struct inode *inode, |
| 755 | u64 size) |
| 756 | { |
| 757 | struct address_space *mapping = inode->i_mapping; |
| 758 | struct page *page; |
| 759 | unsigned long index; |
| 760 | unsigned int offset; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 761 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 762 | int ret; |
| 763 | |
| 764 | offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */ |
| 765 | /* ugh. in prepare/commit_write, if from==to==start of block, we |
| 766 | ** skip the prepare. make sure we never send an offset for the start |
| 767 | ** of a block |
| 768 | */ |
| 769 | if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) { |
| 770 | offset++; |
| 771 | } |
| 772 | index = size >> PAGE_CACHE_SHIFT; |
| 773 | |
| 774 | page = grab_cache_page(mapping, index); |
| 775 | if (!page) { |
| 776 | ret = -ENOMEM; |
| 777 | mlog_errno(ret); |
| 778 | goto out; |
| 779 | } |
| 780 | |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 781 | ret = ocfs2_prepare_write_nolock(inode, page, offset, offset); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 782 | if (ret < 0) { |
| 783 | mlog_errno(ret); |
| 784 | goto out_unlock; |
| 785 | } |
| 786 | |
| 787 | if (ocfs2_should_order_data(inode)) { |
| 788 | handle = ocfs2_start_walk_page_trans(inode, page, offset, |
| 789 | offset); |
| 790 | if (IS_ERR(handle)) { |
| 791 | ret = PTR_ERR(handle); |
| 792 | handle = NULL; |
| 793 | goto out_unlock; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | /* must not update i_size! */ |
| 798 | ret = block_commit_write(page, offset, offset); |
| 799 | if (ret < 0) |
| 800 | mlog_errno(ret); |
| 801 | else |
| 802 | ret = 0; |
| 803 | |
| 804 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 805 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 806 | out_unlock: |
| 807 | unlock_page(page); |
| 808 | page_cache_release(page); |
| 809 | out: |
| 810 | return ret; |
| 811 | } |
| 812 | |
| 813 | static int ocfs2_zero_extend(struct inode *inode, |
| 814 | u64 zero_to_size) |
| 815 | { |
| 816 | int ret = 0; |
| 817 | u64 start_off; |
| 818 | struct super_block *sb = inode->i_sb; |
| 819 | |
| 820 | start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode)); |
| 821 | while (start_off < zero_to_size) { |
| 822 | ret = ocfs2_write_zero_page(inode, start_off); |
| 823 | if (ret < 0) { |
| 824 | mlog_errno(ret); |
| 825 | goto out; |
| 826 | } |
| 827 | |
| 828 | start_off += sb->s_blocksize; |
Mark Fasheh | e2057c5 | 2006-10-03 17:53:05 -0700 | [diff] [blame] | 829 | |
| 830 | /* |
| 831 | * Very large extends have the potential to lock up |
| 832 | * the cpu for extended periods of time. |
| 833 | */ |
| 834 | cond_resched(); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | out: |
| 838 | return ret; |
| 839 | } |
| 840 | |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 841 | /* |
| 842 | * A tail_to_skip value > 0 indicates that we're being called from |
| 843 | * ocfs2_file_aio_write(). This has the following implications: |
| 844 | * |
| 845 | * - we don't want to update i_size |
| 846 | * - di_bh will be NULL, which is fine because it's only used in the |
| 847 | * case where we want to update i_size. |
| 848 | * - ocfs2_zero_extend() will then only be filling the hole created |
| 849 | * between i_size and the start of the write. |
| 850 | */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 851 | static int ocfs2_extend_file(struct inode *inode, |
| 852 | struct buffer_head *di_bh, |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 853 | u64 new_i_size, |
| 854 | size_t tail_to_skip) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 855 | { |
| 856 | int ret = 0; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 857 | u32 clusters_to_add = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 858 | |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 859 | BUG_ON(!tail_to_skip && !di_bh); |
| 860 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 861 | /* setattr sometimes calls us like this. */ |
| 862 | if (new_i_size == 0) |
| 863 | goto out; |
| 864 | |
| 865 | if (i_size_read(inode) == new_i_size) |
| 866 | goto out; |
| 867 | BUG_ON(new_i_size < i_size_read(inode)); |
| 868 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 869 | if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) { |
| 870 | BUG_ON(tail_to_skip != 0); |
| 871 | goto out_update_size; |
| 872 | } |
| 873 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 874 | clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) - |
| 875 | OCFS2_I(inode)->ip_clusters; |
| 876 | |
Mark Fasheh | 0effef7 | 2006-10-03 17:44:42 -0700 | [diff] [blame] | 877 | /* |
| 878 | * protect the pages that ocfs2_zero_extend is going to be |
| 879 | * pulling into the page cache.. we do this before the |
| 880 | * metadata extend so that we don't get into the situation |
| 881 | * where we've extended the metadata but can't get the data |
| 882 | * lock to zero. |
| 883 | */ |
| 884 | ret = ocfs2_data_lock(inode, 1); |
| 885 | if (ret < 0) { |
| 886 | mlog_errno(ret); |
| 887 | goto out; |
| 888 | } |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 889 | |
Mark Fasheh | 0effef7 | 2006-10-03 17:44:42 -0700 | [diff] [blame] | 890 | if (clusters_to_add) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 891 | ret = ocfs2_extend_allocation(inode, clusters_to_add); |
| 892 | if (ret < 0) { |
| 893 | mlog_errno(ret); |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 894 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 895 | } |
Mark Fasheh | 0effef7 | 2006-10-03 17:44:42 -0700 | [diff] [blame] | 896 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 897 | |
Mark Fasheh | 0effef7 | 2006-10-03 17:44:42 -0700 | [diff] [blame] | 898 | /* |
| 899 | * Call this even if we don't add any clusters to the tree. We |
| 900 | * still need to zero the area between the old i_size and the |
| 901 | * new i_size. |
| 902 | */ |
| 903 | ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip); |
| 904 | if (ret < 0) { |
| 905 | mlog_errno(ret); |
| 906 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 907 | } |
| 908 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 909 | out_update_size: |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 910 | if (!tail_to_skip) { |
| 911 | /* We're being called from ocfs2_setattr() which wants |
| 912 | * us to update i_size */ |
| 913 | ret = ocfs2_simple_size_update(inode, di_bh, new_i_size); |
| 914 | if (ret < 0) |
| 915 | mlog_errno(ret); |
| 916 | } |
| 917 | |
| 918 | out_unlock: |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 919 | if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) |
| 920 | ocfs2_data_unlock(inode, 1); |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 921 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 922 | out: |
| 923 | return ret; |
| 924 | } |
| 925 | |
| 926 | int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) |
| 927 | { |
| 928 | int status = 0, size_change; |
| 929 | struct inode *inode = dentry->d_inode; |
| 930 | struct super_block *sb = inode->i_sb; |
| 931 | struct ocfs2_super *osb = OCFS2_SB(sb); |
| 932 | struct buffer_head *bh = NULL; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 933 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 934 | |
| 935 | mlog_entry("(0x%p, '%.*s')\n", dentry, |
| 936 | dentry->d_name.len, dentry->d_name.name); |
| 937 | |
| 938 | if (attr->ia_valid & ATTR_MODE) |
| 939 | mlog(0, "mode change: %d\n", attr->ia_mode); |
| 940 | if (attr->ia_valid & ATTR_UID) |
| 941 | mlog(0, "uid change: %d\n", attr->ia_uid); |
| 942 | if (attr->ia_valid & ATTR_GID) |
| 943 | mlog(0, "gid change: %d\n", attr->ia_gid); |
| 944 | if (attr->ia_valid & ATTR_SIZE) |
| 945 | mlog(0, "size change...\n"); |
| 946 | if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME)) |
| 947 | mlog(0, "time change...\n"); |
| 948 | |
| 949 | #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \ |
| 950 | | ATTR_GID | ATTR_UID | ATTR_MODE) |
| 951 | if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) { |
| 952 | mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid); |
| 953 | return 0; |
| 954 | } |
| 955 | |
| 956 | status = inode_change_ok(inode, attr); |
| 957 | if (status) |
| 958 | return status; |
| 959 | |
| 960 | size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE; |
| 961 | if (size_change) { |
| 962 | status = ocfs2_rw_lock(inode, 1); |
| 963 | if (status < 0) { |
| 964 | mlog_errno(status); |
| 965 | goto bail; |
| 966 | } |
| 967 | } |
| 968 | |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 969 | status = ocfs2_meta_lock(inode, &bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 970 | if (status < 0) { |
| 971 | if (status != -ENOENT) |
| 972 | mlog_errno(status); |
| 973 | goto bail_unlock_rw; |
| 974 | } |
| 975 | |
| 976 | if (size_change && attr->ia_size != i_size_read(inode)) { |
| 977 | if (i_size_read(inode) > attr->ia_size) |
| 978 | status = ocfs2_truncate_file(inode, bh, attr->ia_size); |
| 979 | else |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 980 | status = ocfs2_extend_file(inode, bh, attr->ia_size, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 981 | if (status < 0) { |
| 982 | if (status != -ENOSPC) |
| 983 | mlog_errno(status); |
| 984 | status = -ENOSPC; |
| 985 | goto bail_unlock; |
| 986 | } |
| 987 | } |
| 988 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 989 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 990 | if (IS_ERR(handle)) { |
| 991 | status = PTR_ERR(handle); |
| 992 | mlog_errno(status); |
| 993 | goto bail_unlock; |
| 994 | } |
| 995 | |
| 996 | status = inode_setattr(inode, attr); |
| 997 | if (status < 0) { |
| 998 | mlog_errno(status); |
| 999 | goto bail_commit; |
| 1000 | } |
| 1001 | |
| 1002 | status = ocfs2_mark_inode_dirty(handle, inode, bh); |
| 1003 | if (status < 0) |
| 1004 | mlog_errno(status); |
| 1005 | |
| 1006 | bail_commit: |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 1007 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1008 | bail_unlock: |
| 1009 | ocfs2_meta_unlock(inode, 1); |
| 1010 | bail_unlock_rw: |
| 1011 | if (size_change) |
| 1012 | ocfs2_rw_unlock(inode, 1); |
| 1013 | bail: |
| 1014 | if (bh) |
| 1015 | brelse(bh); |
| 1016 | |
| 1017 | mlog_exit(status); |
| 1018 | return status; |
| 1019 | } |
| 1020 | |
| 1021 | int ocfs2_getattr(struct vfsmount *mnt, |
| 1022 | struct dentry *dentry, |
| 1023 | struct kstat *stat) |
| 1024 | { |
| 1025 | struct inode *inode = dentry->d_inode; |
| 1026 | struct super_block *sb = dentry->d_inode->i_sb; |
| 1027 | struct ocfs2_super *osb = sb->s_fs_info; |
| 1028 | int err; |
| 1029 | |
| 1030 | mlog_entry_void(); |
| 1031 | |
| 1032 | err = ocfs2_inode_revalidate(dentry); |
| 1033 | if (err) { |
| 1034 | if (err != -ENOENT) |
| 1035 | mlog_errno(err); |
| 1036 | goto bail; |
| 1037 | } |
| 1038 | |
| 1039 | generic_fillattr(inode, stat); |
| 1040 | |
| 1041 | /* We set the blksize from the cluster size for performance */ |
| 1042 | stat->blksize = osb->s_clustersize; |
| 1043 | |
| 1044 | bail: |
| 1045 | mlog_exit(err); |
| 1046 | |
| 1047 | return err; |
| 1048 | } |
| 1049 | |
Tiger Yang | d38eb8d | 2006-11-27 09:59:21 +0800 | [diff] [blame] | 1050 | int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd) |
| 1051 | { |
| 1052 | int ret; |
| 1053 | |
| 1054 | mlog_entry_void(); |
| 1055 | |
| 1056 | ret = ocfs2_meta_lock(inode, NULL, 0); |
| 1057 | if (ret) { |
Mark Fasheh | a9f5f70 | 2007-04-26 11:43:43 -0700 | [diff] [blame] | 1058 | if (ret != -ENOENT) |
| 1059 | mlog_errno(ret); |
Tiger Yang | d38eb8d | 2006-11-27 09:59:21 +0800 | [diff] [blame] | 1060 | goto out; |
| 1061 | } |
| 1062 | |
| 1063 | ret = generic_permission(inode, mask, NULL); |
Tiger Yang | d38eb8d | 2006-11-27 09:59:21 +0800 | [diff] [blame] | 1064 | |
| 1065 | ocfs2_meta_unlock(inode, 0); |
| 1066 | out: |
| 1067 | mlog_exit(ret); |
| 1068 | return ret; |
| 1069 | } |
| 1070 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1071 | static int ocfs2_write_remove_suid(struct inode *inode) |
| 1072 | { |
| 1073 | int ret; |
| 1074 | struct buffer_head *bh = NULL; |
| 1075 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1076 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1077 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1078 | struct ocfs2_dinode *di; |
| 1079 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1080 | mlog_entry("(Inode %llu, mode 0%o)\n", |
| 1081 | (unsigned long long)oi->ip_blkno, inode->i_mode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1082 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 1083 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1084 | if (handle == NULL) { |
| 1085 | ret = -ENOMEM; |
| 1086 | mlog_errno(ret); |
| 1087 | goto out; |
| 1088 | } |
| 1089 | |
| 1090 | ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode); |
| 1091 | if (ret < 0) { |
| 1092 | mlog_errno(ret); |
| 1093 | goto out_trans; |
| 1094 | } |
| 1095 | |
| 1096 | ret = ocfs2_journal_access(handle, inode, bh, |
| 1097 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1098 | if (ret < 0) { |
| 1099 | mlog_errno(ret); |
| 1100 | goto out_bh; |
| 1101 | } |
| 1102 | |
| 1103 | inode->i_mode &= ~S_ISUID; |
| 1104 | if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP)) |
| 1105 | inode->i_mode &= ~S_ISGID; |
| 1106 | |
| 1107 | di = (struct ocfs2_dinode *) bh->b_data; |
| 1108 | di->i_mode = cpu_to_le16(inode->i_mode); |
| 1109 | |
| 1110 | ret = ocfs2_journal_dirty(handle, bh); |
| 1111 | if (ret < 0) |
| 1112 | mlog_errno(ret); |
| 1113 | out_bh: |
| 1114 | brelse(bh); |
| 1115 | out_trans: |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 1116 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1117 | out: |
| 1118 | mlog_exit(ret); |
| 1119 | return ret; |
| 1120 | } |
| 1121 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1122 | /* |
| 1123 | * Will look for holes and unwritten extents in the range starting at |
| 1124 | * pos for count bytes (inclusive). |
| 1125 | */ |
| 1126 | static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos, |
| 1127 | size_t count) |
| 1128 | { |
| 1129 | int ret = 0; |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame^] | 1130 | unsigned int extent_flags; |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1131 | u32 cpos, clusters, extent_len, phys_cpos; |
| 1132 | struct super_block *sb = inode->i_sb; |
| 1133 | |
| 1134 | cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits; |
| 1135 | clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos; |
| 1136 | |
| 1137 | while (clusters) { |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame^] | 1138 | ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len, |
| 1139 | &extent_flags); |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1140 | if (ret < 0) { |
| 1141 | mlog_errno(ret); |
| 1142 | goto out; |
| 1143 | } |
| 1144 | |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame^] | 1145 | if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) { |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1146 | ret = 1; |
| 1147 | break; |
| 1148 | } |
| 1149 | |
| 1150 | if (extent_len > clusters) |
| 1151 | extent_len = clusters; |
| 1152 | |
| 1153 | clusters -= extent_len; |
| 1154 | cpos += extent_len; |
| 1155 | } |
| 1156 | out: |
| 1157 | return ret; |
| 1158 | } |
| 1159 | |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1160 | static int ocfs2_prepare_inode_for_write(struct dentry *dentry, |
| 1161 | loff_t *ppos, |
| 1162 | size_t count, |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1163 | int appending, |
| 1164 | int *direct_io) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1165 | { |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1166 | int ret = 0, meta_level = appending; |
| 1167 | struct inode *inode = dentry->d_inode; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1168 | u32 clusters; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1169 | loff_t newsize, saved_pos; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1170 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1171 | /* |
| 1172 | * We sample i_size under a read level meta lock to see if our write |
| 1173 | * is extending the file, if it is we back off and get a write level |
| 1174 | * meta lock. |
| 1175 | */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1176 | for(;;) { |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 1177 | ret = ocfs2_meta_lock(inode, NULL, meta_level); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1178 | if (ret < 0) { |
| 1179 | meta_level = -1; |
| 1180 | mlog_errno(ret); |
| 1181 | goto out; |
| 1182 | } |
| 1183 | |
| 1184 | /* Clear suid / sgid if necessary. We do this here |
| 1185 | * instead of later in the write path because |
| 1186 | * remove_suid() calls ->setattr without any hint that |
| 1187 | * we may have already done our cluster locking. Since |
| 1188 | * ocfs2_setattr() *must* take cluster locks to |
| 1189 | * proceeed, this will lead us to recursively lock the |
| 1190 | * inode. There's also the dinode i_size state which |
| 1191 | * can be lost via setattr during extending writes (we |
| 1192 | * set inode->i_size at the end of a write. */ |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1193 | if (should_remove_suid(dentry)) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1194 | if (meta_level == 0) { |
| 1195 | ocfs2_meta_unlock(inode, meta_level); |
| 1196 | meta_level = 1; |
| 1197 | continue; |
| 1198 | } |
| 1199 | |
| 1200 | ret = ocfs2_write_remove_suid(inode); |
| 1201 | if (ret < 0) { |
| 1202 | mlog_errno(ret); |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1203 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | /* work on a copy of ppos until we're sure that we won't have |
| 1208 | * to recalculate it due to relocking. */ |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1209 | if (appending) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1210 | saved_pos = i_size_read(inode); |
| 1211 | mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos); |
| 1212 | } else { |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1213 | saved_pos = *ppos; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1214 | } |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 1215 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1216 | if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) { |
| 1217 | loff_t end = saved_pos + count; |
| 1218 | |
| 1219 | /* |
| 1220 | * Skip the O_DIRECT checks if we don't need |
| 1221 | * them. |
| 1222 | */ |
| 1223 | if (!direct_io || !(*direct_io)) |
| 1224 | break; |
| 1225 | |
| 1226 | /* |
| 1227 | * Allowing concurrent direct writes means |
| 1228 | * i_size changes wouldn't be synchronized, so |
| 1229 | * one node could wind up truncating another |
| 1230 | * nodes writes. |
| 1231 | */ |
| 1232 | if (end > i_size_read(inode)) { |
| 1233 | *direct_io = 0; |
| 1234 | break; |
| 1235 | } |
| 1236 | |
| 1237 | /* |
| 1238 | * We don't fill holes during direct io, so |
| 1239 | * check for them here. If any are found, the |
| 1240 | * caller will have to retake some cluster |
| 1241 | * locks and initiate the io as buffered. |
| 1242 | */ |
| 1243 | ret = ocfs2_check_range_for_holes(inode, saved_pos, |
| 1244 | count); |
| 1245 | if (ret == 1) { |
| 1246 | *direct_io = 0; |
| 1247 | ret = 0; |
| 1248 | } else if (ret < 0) |
| 1249 | mlog_errno(ret); |
| 1250 | break; |
| 1251 | } |
| 1252 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 1253 | /* |
| 1254 | * The rest of this loop is concerned with legacy file |
| 1255 | * systems which don't support sparse files. |
| 1256 | */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 1257 | |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1258 | newsize = count + saved_pos; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1259 | |
Mark Fasheh | 215c7f9 | 2006-02-01 16:42:10 -0800 | [diff] [blame] | 1260 | mlog(0, "pos=%lld newsize=%lld cursize=%lld\n", |
| 1261 | (long long) saved_pos, (long long) newsize, |
| 1262 | (long long) i_size_read(inode)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1263 | |
| 1264 | /* No need for a higher level metadata lock if we're |
| 1265 | * never going past i_size. */ |
| 1266 | if (newsize <= i_size_read(inode)) |
| 1267 | break; |
| 1268 | |
| 1269 | if (meta_level == 0) { |
| 1270 | ocfs2_meta_unlock(inode, meta_level); |
| 1271 | meta_level = 1; |
| 1272 | continue; |
| 1273 | } |
| 1274 | |
| 1275 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 1276 | clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) - |
| 1277 | OCFS2_I(inode)->ip_clusters; |
| 1278 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 1279 | |
| 1280 | mlog(0, "Writing at EOF, may need more allocation: " |
Mark Fasheh | 215c7f9 | 2006-02-01 16:42:10 -0800 | [diff] [blame] | 1281 | "i_size = %lld, newsize = %lld, need %u clusters\n", |
| 1282 | (long long) i_size_read(inode), (long long) newsize, |
| 1283 | clusters); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1284 | |
| 1285 | /* We only want to continue the rest of this loop if |
| 1286 | * our extend will actually require more |
| 1287 | * allocation. */ |
| 1288 | if (!clusters) |
| 1289 | break; |
| 1290 | |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1291 | ret = ocfs2_extend_file(inode, NULL, newsize, count); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1292 | if (ret < 0) { |
| 1293 | if (ret != -ENOSPC) |
| 1294 | mlog_errno(ret); |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1295 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1296 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1297 | break; |
| 1298 | } |
| 1299 | |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1300 | if (appending) |
| 1301 | *ppos = saved_pos; |
| 1302 | |
| 1303 | out_unlock: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1304 | ocfs2_meta_unlock(inode, meta_level); |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1305 | |
| 1306 | out: |
| 1307 | return ret; |
| 1308 | } |
| 1309 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1310 | static inline void |
| 1311 | ocfs2_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes) |
| 1312 | { |
| 1313 | const struct iovec *iov = *iovp; |
| 1314 | size_t base = *basep; |
| 1315 | |
| 1316 | do { |
| 1317 | int copy = min(bytes, iov->iov_len - base); |
| 1318 | |
| 1319 | bytes -= copy; |
| 1320 | base += copy; |
| 1321 | if (iov->iov_len == base) { |
| 1322 | iov++; |
| 1323 | base = 0; |
| 1324 | } |
| 1325 | } while (bytes); |
| 1326 | *iovp = iov; |
| 1327 | *basep = base; |
| 1328 | } |
| 1329 | |
| 1330 | static struct page * ocfs2_get_write_source(struct ocfs2_buffered_write_priv *bp, |
| 1331 | const struct iovec *cur_iov, |
| 1332 | size_t iov_offset) |
| 1333 | { |
| 1334 | int ret; |
| 1335 | char *buf; |
| 1336 | struct page *src_page = NULL; |
| 1337 | |
| 1338 | buf = cur_iov->iov_base + iov_offset; |
| 1339 | |
| 1340 | if (!segment_eq(get_fs(), KERNEL_DS)) { |
| 1341 | /* |
| 1342 | * Pull in the user page. We want to do this outside |
| 1343 | * of the meta data locks in order to preserve locking |
| 1344 | * order in case of page fault. |
| 1345 | */ |
| 1346 | ret = get_user_pages(current, current->mm, |
| 1347 | (unsigned long)buf & PAGE_CACHE_MASK, 1, |
| 1348 | 0, 0, &src_page, NULL); |
| 1349 | if (ret == 1) |
| 1350 | bp->b_src_buf = kmap(src_page); |
| 1351 | else |
| 1352 | src_page = ERR_PTR(-EFAULT); |
| 1353 | } else { |
| 1354 | bp->b_src_buf = buf; |
| 1355 | } |
| 1356 | |
| 1357 | return src_page; |
| 1358 | } |
| 1359 | |
| 1360 | static void ocfs2_put_write_source(struct ocfs2_buffered_write_priv *bp, |
| 1361 | struct page *page) |
| 1362 | { |
| 1363 | if (page) { |
| 1364 | kunmap(page); |
| 1365 | page_cache_release(page); |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | static ssize_t ocfs2_file_buffered_write(struct file *file, loff_t *ppos, |
| 1370 | const struct iovec *iov, |
| 1371 | unsigned long nr_segs, |
| 1372 | size_t count, |
| 1373 | ssize_t o_direct_written) |
| 1374 | { |
| 1375 | int ret = 0; |
| 1376 | ssize_t copied, total = 0; |
| 1377 | size_t iov_offset = 0; |
| 1378 | const struct iovec *cur_iov = iov; |
| 1379 | struct ocfs2_buffered_write_priv bp; |
| 1380 | struct page *page; |
| 1381 | |
| 1382 | /* |
| 1383 | * handle partial DIO write. Adjust cur_iov if needed. |
| 1384 | */ |
| 1385 | ocfs2_set_next_iovec(&cur_iov, &iov_offset, o_direct_written); |
| 1386 | |
| 1387 | do { |
| 1388 | bp.b_cur_off = iov_offset; |
| 1389 | bp.b_cur_iov = cur_iov; |
| 1390 | |
| 1391 | page = ocfs2_get_write_source(&bp, cur_iov, iov_offset); |
| 1392 | if (IS_ERR(page)) { |
| 1393 | ret = PTR_ERR(page); |
| 1394 | goto out; |
| 1395 | } |
| 1396 | |
| 1397 | copied = ocfs2_buffered_write_cluster(file, *ppos, count, |
| 1398 | ocfs2_map_and_write_user_data, |
| 1399 | &bp); |
| 1400 | |
| 1401 | ocfs2_put_write_source(&bp, page); |
| 1402 | |
| 1403 | if (copied < 0) { |
| 1404 | mlog_errno(copied); |
| 1405 | ret = copied; |
| 1406 | goto out; |
| 1407 | } |
| 1408 | |
| 1409 | total += copied; |
| 1410 | *ppos = *ppos + copied; |
| 1411 | count -= copied; |
| 1412 | |
| 1413 | ocfs2_set_next_iovec(&cur_iov, &iov_offset, copied); |
| 1414 | } while(count); |
| 1415 | |
| 1416 | out: |
| 1417 | return total ? total : ret; |
| 1418 | } |
| 1419 | |
| 1420 | static int ocfs2_check_iovec(const struct iovec *iov, size_t *counted, |
| 1421 | unsigned long *nr_segs) |
| 1422 | { |
| 1423 | size_t ocount; /* original count */ |
| 1424 | unsigned long seg; |
| 1425 | |
| 1426 | ocount = 0; |
| 1427 | for (seg = 0; seg < *nr_segs; seg++) { |
| 1428 | const struct iovec *iv = &iov[seg]; |
| 1429 | |
| 1430 | /* |
| 1431 | * If any segment has a negative length, or the cumulative |
| 1432 | * length ever wraps negative then return -EINVAL. |
| 1433 | */ |
| 1434 | ocount += iv->iov_len; |
| 1435 | if (unlikely((ssize_t)(ocount|iv->iov_len) < 0)) |
| 1436 | return -EINVAL; |
| 1437 | if (access_ok(VERIFY_READ, iv->iov_base, iv->iov_len)) |
| 1438 | continue; |
| 1439 | if (seg == 0) |
| 1440 | return -EFAULT; |
| 1441 | *nr_segs = seg; |
| 1442 | ocount -= iv->iov_len; /* This segment is no good */ |
| 1443 | break; |
| 1444 | } |
| 1445 | |
| 1446 | *counted = ocount; |
| 1447 | return 0; |
| 1448 | } |
| 1449 | |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1450 | static ssize_t ocfs2_file_aio_write(struct kiocb *iocb, |
| 1451 | const struct iovec *iov, |
| 1452 | unsigned long nr_segs, |
| 1453 | loff_t pos) |
| 1454 | { |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1455 | int ret, direct_io, appending, rw_level, have_alloc_sem = 0; |
| 1456 | int can_do_direct, sync = 0; |
| 1457 | ssize_t written = 0; |
| 1458 | size_t ocount; /* original count */ |
| 1459 | size_t count; /* after file limit checks */ |
| 1460 | loff_t *ppos = &iocb->ki_pos; |
| 1461 | struct file *file = iocb->ki_filp; |
| 1462 | struct inode *inode = file->f_path.dentry->d_inode; |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1463 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1464 | mlog_entry("(0x%p, %u, '%.*s')\n", file, |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1465 | (unsigned int)nr_segs, |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1466 | file->f_path.dentry->d_name.len, |
| 1467 | file->f_path.dentry->d_name.name); |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1468 | |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1469 | if (iocb->ki_left == 0) |
| 1470 | return 0; |
| 1471 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1472 | ret = ocfs2_check_iovec(iov, &ocount, &nr_segs); |
| 1473 | if (ret) |
| 1474 | return ret; |
| 1475 | |
| 1476 | count = ocount; |
| 1477 | |
| 1478 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
| 1479 | |
| 1480 | appending = file->f_flags & O_APPEND ? 1 : 0; |
| 1481 | direct_io = file->f_flags & O_DIRECT ? 1 : 0; |
| 1482 | |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1483 | mutex_lock(&inode->i_mutex); |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1484 | |
| 1485 | relock: |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1486 | /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */ |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1487 | if (direct_io) { |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1488 | down_read(&inode->i_alloc_sem); |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1489 | have_alloc_sem = 1; |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | /* concurrent O_DIRECT writes are allowed */ |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1493 | rw_level = !direct_io; |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1494 | ret = ocfs2_rw_lock(inode, rw_level); |
| 1495 | if (ret < 0) { |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1496 | mlog_errno(ret); |
| 1497 | goto out_sems; |
| 1498 | } |
| 1499 | |
| 1500 | can_do_direct = direct_io; |
| 1501 | ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos, |
| 1502 | iocb->ki_left, appending, |
| 1503 | &can_do_direct); |
| 1504 | if (ret < 0) { |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1505 | mlog_errno(ret); |
| 1506 | goto out; |
| 1507 | } |
| 1508 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1509 | /* |
| 1510 | * We can't complete the direct I/O as requested, fall back to |
| 1511 | * buffered I/O. |
| 1512 | */ |
| 1513 | if (direct_io && !can_do_direct) { |
| 1514 | ocfs2_rw_unlock(inode, rw_level); |
| 1515 | up_read(&inode->i_alloc_sem); |
| 1516 | |
| 1517 | have_alloc_sem = 0; |
| 1518 | rw_level = -1; |
| 1519 | |
| 1520 | direct_io = 0; |
| 1521 | sync = 1; |
| 1522 | goto relock; |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1523 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1524 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1525 | if (!sync && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) |
| 1526 | sync = 1; |
| 1527 | |
| 1528 | /* |
| 1529 | * XXX: Is it ok to execute these checks a second time? |
| 1530 | */ |
| 1531 | ret = generic_write_checks(file, ppos, &count, S_ISBLK(inode->i_mode)); |
| 1532 | if (ret) |
| 1533 | goto out; |
| 1534 | |
| 1535 | /* |
| 1536 | * Set pos so that sync_page_range_nolock() below understands |
| 1537 | * where to start from. We might've moved it around via the |
| 1538 | * calls above. The range we want to actually sync starts from |
| 1539 | * *ppos here. |
| 1540 | * |
| 1541 | */ |
| 1542 | pos = *ppos; |
| 1543 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1544 | /* communicate with ocfs2_dio_end_io */ |
| 1545 | ocfs2_iocb_set_rw_locked(iocb); |
| 1546 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1547 | if (direct_io) { |
| 1548 | written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos, |
| 1549 | ppos, count, ocount); |
| 1550 | if (written < 0) { |
| 1551 | ret = written; |
| 1552 | goto out_dio; |
| 1553 | } |
| 1554 | } else { |
| 1555 | written = ocfs2_file_buffered_write(file, ppos, iov, nr_segs, |
| 1556 | count, written); |
| 1557 | if (written < 0) { |
| 1558 | ret = written; |
| 1559 | if (ret != -EFAULT || ret != -ENOSPC) |
| 1560 | mlog_errno(ret); |
| 1561 | goto out; |
| 1562 | } |
| 1563 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1564 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1565 | out_dio: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1566 | /* buffered aio wouldn't have proper lock coverage today */ |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1567 | BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1568 | |
| 1569 | /* |
| 1570 | * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io |
| 1571 | * function pointer which is called when o_direct io completes so that |
| 1572 | * it can unlock our rw lock. (it's the clustered equivalent of |
| 1573 | * i_alloc_sem; protects truncate from racing with pending ios). |
| 1574 | * Unfortunately there are error cases which call end_io and others |
| 1575 | * that don't. so we don't have to unlock the rw_lock if either an |
| 1576 | * async dio is going to do it in the future or an end_io after an |
| 1577 | * error has already done it. |
| 1578 | */ |
| 1579 | if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) { |
| 1580 | rw_level = -1; |
| 1581 | have_alloc_sem = 0; |
| 1582 | } |
| 1583 | |
| 1584 | out: |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1585 | if (rw_level != -1) |
| 1586 | ocfs2_rw_unlock(inode, rw_level); |
| 1587 | |
| 1588 | out_sems: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1589 | if (have_alloc_sem) |
| 1590 | up_read(&inode->i_alloc_sem); |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1591 | |
| 1592 | if (written > 0 && sync) { |
| 1593 | ssize_t err; |
| 1594 | |
| 1595 | err = sync_page_range_nolock(inode, file->f_mapping, pos, count); |
| 1596 | if (err < 0) |
| 1597 | written = err; |
| 1598 | } |
| 1599 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1600 | mutex_unlock(&inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1601 | |
| 1602 | mlog_exit(ret); |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1603 | return written ? written : ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1604 | } |
| 1605 | |
Mark Fasheh | 6af67d8 | 2007-03-06 17:24:46 -0800 | [diff] [blame] | 1606 | static int ocfs2_splice_write_actor(struct pipe_inode_info *pipe, |
| 1607 | struct pipe_buffer *buf, |
| 1608 | struct splice_desc *sd) |
| 1609 | { |
| 1610 | int ret, count, total = 0; |
| 1611 | ssize_t copied = 0; |
| 1612 | struct ocfs2_splice_write_priv sp; |
| 1613 | |
| 1614 | ret = buf->ops->pin(pipe, buf); |
| 1615 | if (ret) |
| 1616 | goto out; |
| 1617 | |
| 1618 | sp.s_sd = sd; |
| 1619 | sp.s_buf = buf; |
| 1620 | sp.s_pipe = pipe; |
| 1621 | sp.s_offset = sd->pos & ~PAGE_CACHE_MASK; |
| 1622 | sp.s_buf_offset = buf->offset; |
| 1623 | |
| 1624 | count = sd->len; |
| 1625 | if (count + sp.s_offset > PAGE_CACHE_SIZE) |
| 1626 | count = PAGE_CACHE_SIZE - sp.s_offset; |
| 1627 | |
| 1628 | do { |
| 1629 | /* |
| 1630 | * splice wants us to copy up to one page at a |
| 1631 | * time. For pagesize > cluster size, this means we |
| 1632 | * might enter ocfs2_buffered_write_cluster() more |
| 1633 | * than once, so keep track of our progress here. |
| 1634 | */ |
| 1635 | copied = ocfs2_buffered_write_cluster(sd->file, |
| 1636 | (loff_t)sd->pos + total, |
| 1637 | count, |
| 1638 | ocfs2_map_and_write_splice_data, |
| 1639 | &sp); |
| 1640 | if (copied < 0) { |
| 1641 | mlog_errno(copied); |
| 1642 | ret = copied; |
| 1643 | goto out; |
| 1644 | } |
| 1645 | |
| 1646 | count -= copied; |
| 1647 | sp.s_offset += copied; |
| 1648 | sp.s_buf_offset += copied; |
| 1649 | total += copied; |
| 1650 | } while (count); |
| 1651 | |
| 1652 | ret = 0; |
| 1653 | out: |
| 1654 | |
| 1655 | return total ? total : ret; |
| 1656 | } |
| 1657 | |
| 1658 | static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe, |
| 1659 | struct file *out, |
| 1660 | loff_t *ppos, |
| 1661 | size_t len, |
| 1662 | unsigned int flags) |
| 1663 | { |
| 1664 | int ret, err; |
| 1665 | struct address_space *mapping = out->f_mapping; |
| 1666 | struct inode *inode = mapping->host; |
| 1667 | |
| 1668 | ret = __splice_from_pipe(pipe, out, ppos, len, flags, |
| 1669 | ocfs2_splice_write_actor); |
| 1670 | if (ret > 0) { |
| 1671 | *ppos += ret; |
| 1672 | |
| 1673 | if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) { |
| 1674 | err = generic_osync_inode(inode, mapping, |
| 1675 | OSYNC_METADATA|OSYNC_DATA); |
| 1676 | if (err) |
| 1677 | ret = err; |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | return ret; |
| 1682 | } |
| 1683 | |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1684 | static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe, |
| 1685 | struct file *out, |
| 1686 | loff_t *ppos, |
| 1687 | size_t len, |
| 1688 | unsigned int flags) |
| 1689 | { |
| 1690 | int ret; |
Josef Sipek | d28c917 | 2006-12-08 02:37:25 -0800 | [diff] [blame] | 1691 | struct inode *inode = out->f_path.dentry->d_inode; |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1692 | |
| 1693 | mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe, |
| 1694 | (unsigned int)len, |
Josef Sipek | d28c917 | 2006-12-08 02:37:25 -0800 | [diff] [blame] | 1695 | out->f_path.dentry->d_name.len, |
| 1696 | out->f_path.dentry->d_name.name); |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1697 | |
| 1698 | inode_double_lock(inode, pipe->inode); |
| 1699 | |
| 1700 | ret = ocfs2_rw_lock(inode, 1); |
| 1701 | if (ret < 0) { |
| 1702 | mlog_errno(ret); |
| 1703 | goto out; |
| 1704 | } |
| 1705 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1706 | ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0, |
| 1707 | NULL); |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1708 | if (ret < 0) { |
| 1709 | mlog_errno(ret); |
| 1710 | goto out_unlock; |
| 1711 | } |
| 1712 | |
| 1713 | /* ok, we're done with i_size and alloc work */ |
Mark Fasheh | 6af67d8 | 2007-03-06 17:24:46 -0800 | [diff] [blame] | 1714 | ret = __ocfs2_file_splice_write(pipe, out, ppos, len, flags); |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1715 | |
| 1716 | out_unlock: |
| 1717 | ocfs2_rw_unlock(inode, 1); |
| 1718 | out: |
| 1719 | inode_double_unlock(inode, pipe->inode); |
| 1720 | |
| 1721 | mlog_exit(ret); |
| 1722 | return ret; |
| 1723 | } |
| 1724 | |
| 1725 | static ssize_t ocfs2_file_splice_read(struct file *in, |
| 1726 | loff_t *ppos, |
| 1727 | struct pipe_inode_info *pipe, |
| 1728 | size_t len, |
| 1729 | unsigned int flags) |
| 1730 | { |
| 1731 | int ret = 0; |
Josef Sipek | d28c917 | 2006-12-08 02:37:25 -0800 | [diff] [blame] | 1732 | struct inode *inode = in->f_path.dentry->d_inode; |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1733 | |
| 1734 | mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe, |
| 1735 | (unsigned int)len, |
Josef Sipek | d28c917 | 2006-12-08 02:37:25 -0800 | [diff] [blame] | 1736 | in->f_path.dentry->d_name.len, |
| 1737 | in->f_path.dentry->d_name.name); |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1738 | |
| 1739 | /* |
| 1740 | * See the comment in ocfs2_file_aio_read() |
| 1741 | */ |
| 1742 | ret = ocfs2_meta_lock(inode, NULL, 0); |
| 1743 | if (ret < 0) { |
| 1744 | mlog_errno(ret); |
| 1745 | goto bail; |
| 1746 | } |
| 1747 | ocfs2_meta_unlock(inode, 0); |
| 1748 | |
| 1749 | ret = generic_file_splice_read(in, ppos, pipe, len, flags); |
| 1750 | |
| 1751 | bail: |
| 1752 | mlog_exit(ret); |
| 1753 | return ret; |
| 1754 | } |
| 1755 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1756 | static ssize_t ocfs2_file_aio_read(struct kiocb *iocb, |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 1757 | const struct iovec *iov, |
| 1758 | unsigned long nr_segs, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1759 | loff_t pos) |
| 1760 | { |
Tiger Yang | 25899de | 2006-11-15 15:49:02 +0800 | [diff] [blame] | 1761 | 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] | 1762 | struct file *filp = iocb->ki_filp; |
Josef Sipek | d28c917 | 2006-12-08 02:37:25 -0800 | [diff] [blame] | 1763 | struct inode *inode = filp->f_path.dentry->d_inode; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1764 | |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 1765 | mlog_entry("(0x%p, %u, '%.*s')\n", filp, |
| 1766 | (unsigned int)nr_segs, |
Josef Sipek | d28c917 | 2006-12-08 02:37:25 -0800 | [diff] [blame] | 1767 | filp->f_path.dentry->d_name.len, |
| 1768 | filp->f_path.dentry->d_name.name); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1769 | |
| 1770 | if (!inode) { |
| 1771 | ret = -EINVAL; |
| 1772 | mlog_errno(ret); |
| 1773 | goto bail; |
| 1774 | } |
| 1775 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1776 | /* |
| 1777 | * buffered reads protect themselves in ->readpage(). O_DIRECT reads |
| 1778 | * need locks to protect pending reads from racing with truncate. |
| 1779 | */ |
| 1780 | if (filp->f_flags & O_DIRECT) { |
| 1781 | down_read(&inode->i_alloc_sem); |
| 1782 | have_alloc_sem = 1; |
| 1783 | |
| 1784 | ret = ocfs2_rw_lock(inode, 0); |
| 1785 | if (ret < 0) { |
| 1786 | mlog_errno(ret); |
| 1787 | goto bail; |
| 1788 | } |
| 1789 | rw_level = 0; |
| 1790 | /* communicate with ocfs2_dio_end_io */ |
| 1791 | ocfs2_iocb_set_rw_locked(iocb); |
| 1792 | } |
| 1793 | |
Mark Fasheh | c4374f8 | 2006-05-05 19:04:35 -0700 | [diff] [blame] | 1794 | /* |
| 1795 | * We're fine letting folks race truncates and extending |
| 1796 | * writes with read across the cluster, just like they can |
| 1797 | * locally. Hence no rw_lock during read. |
| 1798 | * |
| 1799 | * Take and drop the meta data lock to update inode fields |
| 1800 | * like i_size. This allows the checks down below |
| 1801 | * generic_file_aio_read() a chance of actually working. |
| 1802 | */ |
Tiger Yang | 25899de | 2006-11-15 15:49:02 +0800 | [diff] [blame] | 1803 | ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level); |
Mark Fasheh | c4374f8 | 2006-05-05 19:04:35 -0700 | [diff] [blame] | 1804 | if (ret < 0) { |
| 1805 | mlog_errno(ret); |
| 1806 | goto bail; |
| 1807 | } |
Tiger Yang | 25899de | 2006-11-15 15:49:02 +0800 | [diff] [blame] | 1808 | ocfs2_meta_unlock(inode, lock_level); |
Mark Fasheh | c4374f8 | 2006-05-05 19:04:35 -0700 | [diff] [blame] | 1809 | |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 1810 | ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1811 | if (ret == -EINVAL) |
| 1812 | mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n"); |
| 1813 | |
| 1814 | /* buffered aio wouldn't have proper lock coverage today */ |
| 1815 | BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT)); |
| 1816 | |
| 1817 | /* see ocfs2_file_aio_write */ |
| 1818 | if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) { |
| 1819 | rw_level = -1; |
| 1820 | have_alloc_sem = 0; |
| 1821 | } |
| 1822 | |
| 1823 | bail: |
| 1824 | if (have_alloc_sem) |
| 1825 | up_read(&inode->i_alloc_sem); |
| 1826 | if (rw_level != -1) |
| 1827 | ocfs2_rw_unlock(inode, rw_level); |
| 1828 | mlog_exit(ret); |
| 1829 | |
| 1830 | return ret; |
| 1831 | } |
| 1832 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 1833 | const struct inode_operations ocfs2_file_iops = { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1834 | .setattr = ocfs2_setattr, |
| 1835 | .getattr = ocfs2_getattr, |
Tiger Yang | d38eb8d | 2006-11-27 09:59:21 +0800 | [diff] [blame] | 1836 | .permission = ocfs2_permission, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1837 | }; |
| 1838 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 1839 | const struct inode_operations ocfs2_special_file_iops = { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1840 | .setattr = ocfs2_setattr, |
| 1841 | .getattr = ocfs2_getattr, |
Tiger Yang | d38eb8d | 2006-11-27 09:59:21 +0800 | [diff] [blame] | 1842 | .permission = ocfs2_permission, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1843 | }; |
| 1844 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1845 | const struct file_operations ocfs2_fops = { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1846 | .read = do_sync_read, |
| 1847 | .write = do_sync_write, |
| 1848 | .sendfile = generic_file_sendfile, |
| 1849 | .mmap = ocfs2_mmap, |
| 1850 | .fsync = ocfs2_sync_file, |
| 1851 | .release = ocfs2_file_release, |
| 1852 | .open = ocfs2_file_open, |
| 1853 | .aio_read = ocfs2_file_aio_read, |
| 1854 | .aio_write = ocfs2_file_aio_write, |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 1855 | .ioctl = ocfs2_ioctl, |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1856 | .splice_read = ocfs2_file_splice_read, |
| 1857 | .splice_write = ocfs2_file_splice_write, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1858 | }; |
| 1859 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 1860 | const struct file_operations ocfs2_dops = { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1861 | .read = generic_read_dir, |
| 1862 | .readdir = ocfs2_readdir, |
| 1863 | .fsync = ocfs2_sync_file, |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 1864 | .ioctl = ocfs2_ioctl, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1865 | }; |