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> |
Jens Axboe | d6b29d7 | 2007-06-04 09:59:47 +0200 | [diff] [blame] | 34 | #include <linux/splice.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 | |
Adrian Bunk | 6cb129f | 2007-04-26 00:29:35 -0700 | [diff] [blame] | 210 | static int ocfs2_set_inode_size(handle_t *handle, |
| 211 | struct inode *inode, |
| 212 | struct buffer_head *fe_bh, |
| 213 | u64 new_i_size) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 214 | { |
| 215 | int status; |
| 216 | |
| 217 | mlog_entry_void(); |
| 218 | i_size_write(inode, new_i_size); |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 219 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 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 | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 266 | u64 cluster_bytes; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 267 | |
| 268 | mlog_entry_void(); |
| 269 | |
| 270 | /* TODO: This needs to actually orphan the inode in this |
| 271 | * transaction. */ |
| 272 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 273 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 274 | if (IS_ERR(handle)) { |
| 275 | status = PTR_ERR(handle); |
| 276 | mlog_errno(status); |
| 277 | goto out; |
| 278 | } |
| 279 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 280 | status = ocfs2_journal_access(handle, inode, fe_bh, |
| 281 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 282 | if (status < 0) { |
| 283 | mlog_errno(status); |
| 284 | goto out_commit; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Do this before setting i_size. |
| 289 | */ |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 290 | cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size); |
| 291 | status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size, |
| 292 | cluster_bytes); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 293 | if (status) { |
| 294 | mlog_errno(status); |
| 295 | goto out_commit; |
| 296 | } |
| 297 | |
| 298 | i_size_write(inode, new_i_size); |
| 299 | inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size); |
| 300 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
| 301 | |
| 302 | di = (struct ocfs2_dinode *) fe_bh->b_data; |
| 303 | di->i_size = cpu_to_le64(new_i_size); |
| 304 | di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 305 | di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 306 | |
| 307 | status = ocfs2_journal_dirty(handle, fe_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 308 | if (status < 0) |
| 309 | mlog_errno(status); |
| 310 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 311 | out_commit: |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 312 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 313 | out: |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 314 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 315 | mlog_exit(status); |
| 316 | return status; |
| 317 | } |
| 318 | |
| 319 | static int ocfs2_truncate_file(struct inode *inode, |
| 320 | struct buffer_head *di_bh, |
| 321 | u64 new_i_size) |
| 322 | { |
| 323 | int status = 0; |
| 324 | struct ocfs2_dinode *fe = NULL; |
| 325 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 326 | struct ocfs2_truncate_context *tc = NULL; |
| 327 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 328 | mlog_entry("(inode = %llu, new_i_size = %llu\n", |
| 329 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 330 | (unsigned long long)new_i_size); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 331 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 332 | fe = (struct ocfs2_dinode *) di_bh->b_data; |
| 333 | if (!OCFS2_IS_VALID_DINODE(fe)) { |
| 334 | OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe); |
| 335 | status = -EIO; |
| 336 | goto bail; |
| 337 | } |
| 338 | |
| 339 | 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] | 340 | "Inode %llu, inode i_size = %lld != di " |
| 341 | "i_size = %llu, i_flags = 0x%x\n", |
| 342 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 343 | i_size_read(inode), |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 344 | (unsigned long long)le64_to_cpu(fe->i_size), |
| 345 | le32_to_cpu(fe->i_flags)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 346 | |
| 347 | if (new_i_size > le64_to_cpu(fe->i_size)) { |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 348 | mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n", |
| 349 | (unsigned long long)le64_to_cpu(fe->i_size), |
| 350 | (unsigned long long)new_i_size); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 351 | status = -EINVAL; |
| 352 | mlog_errno(status); |
| 353 | goto bail; |
| 354 | } |
| 355 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 356 | mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n", |
| 357 | (unsigned long long)le64_to_cpu(fe->i_blkno), |
| 358 | (unsigned long long)le64_to_cpu(fe->i_size), |
| 359 | (unsigned long long)new_i_size); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 360 | |
| 361 | /* lets handle the simple truncate cases before doing any more |
| 362 | * cluster locking. */ |
| 363 | if (new_i_size == le64_to_cpu(fe->i_size)) |
| 364 | goto bail; |
| 365 | |
Mark Fasheh | 2e89b2e | 2007-05-09 13:40:18 -0700 | [diff] [blame] | 366 | down_write(&OCFS2_I(inode)->ip_alloc_sem); |
| 367 | |
Mark Fasheh | ab0920c | 2006-03-16 15:06:37 -0800 | [diff] [blame] | 368 | /* This forces other nodes to sync and drop their pages. Do |
| 369 | * this even if we have a truncate without allocation change - |
| 370 | * ocfs2 cluster sizes can be much greater than page size, so |
| 371 | * we have to truncate them anyway. */ |
| 372 | status = ocfs2_data_lock(inode, 1); |
| 373 | if (status < 0) { |
Mark Fasheh | 2e89b2e | 2007-05-09 13:40:18 -0700 | [diff] [blame] | 374 | up_write(&OCFS2_I(inode)->ip_alloc_sem); |
| 375 | |
Mark Fasheh | ab0920c | 2006-03-16 15:06:37 -0800 | [diff] [blame] | 376 | mlog_errno(status); |
| 377 | goto bail; |
| 378 | } |
Mark Fasheh | ab0920c | 2006-03-16 15:06:37 -0800 | [diff] [blame] | 379 | |
Mark Fasheh | 2e89b2e | 2007-05-09 13:40:18 -0700 | [diff] [blame] | 380 | unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1); |
| 381 | truncate_inode_pages(inode->i_mapping, new_i_size); |
| 382 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 383 | /* alright, we're going to need to do a full blown alloc size |
| 384 | * change. Orphan the inode so that recovery can complete the |
| 385 | * truncate if necessary. This does the task of marking |
| 386 | * i_size. */ |
| 387 | status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size); |
| 388 | if (status < 0) { |
| 389 | mlog_errno(status); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 390 | goto bail_unlock_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc); |
| 394 | if (status < 0) { |
| 395 | mlog_errno(status); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 396 | goto bail_unlock_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | status = ocfs2_commit_truncate(osb, inode, di_bh, tc); |
| 400 | if (status < 0) { |
| 401 | mlog_errno(status); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 402 | goto bail_unlock_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /* TODO: orphan dir cleanup here. */ |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 406 | bail_unlock_data: |
| 407 | ocfs2_data_unlock(inode, 1); |
| 408 | |
Mark Fasheh | 2e89b2e | 2007-05-09 13:40:18 -0700 | [diff] [blame] | 409 | up_write(&OCFS2_I(inode)->ip_alloc_sem); |
| 410 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 411 | bail: |
| 412 | |
| 413 | mlog_exit(status); |
| 414 | return status; |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * extend allocation only here. |
| 419 | * we'll update all the disk stuff, and oip->alloc_size |
| 420 | * |
| 421 | * expect stuff to be locked, a transaction started and enough data / |
| 422 | * metadata reservations in the contexts. |
| 423 | * |
| 424 | * Will return -EAGAIN, and a reason if a restart is needed. |
| 425 | * If passed in, *reason will always be set, even in error. |
| 426 | */ |
| 427 | int ocfs2_do_extend_allocation(struct ocfs2_super *osb, |
| 428 | struct inode *inode, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 429 | u32 *logical_offset, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 430 | u32 clusters_to_add, |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 431 | int mark_unwritten, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 432 | struct buffer_head *fe_bh, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 433 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 434 | struct ocfs2_alloc_context *data_ac, |
| 435 | struct ocfs2_alloc_context *meta_ac, |
| 436 | enum ocfs2_alloc_restarted *reason_ret) |
| 437 | { |
| 438 | int status = 0; |
| 439 | int free_extents; |
| 440 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 441 | enum ocfs2_alloc_restarted reason = RESTART_NONE; |
| 442 | u32 bit_off, num_bits; |
| 443 | u64 block; |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 444 | u8 flags = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 445 | |
| 446 | BUG_ON(!clusters_to_add); |
| 447 | |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 448 | if (mark_unwritten) |
| 449 | flags = OCFS2_EXT_UNWRITTEN; |
| 450 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 451 | free_extents = ocfs2_num_free_extents(osb, inode, fe); |
| 452 | if (free_extents < 0) { |
| 453 | status = free_extents; |
| 454 | mlog_errno(status); |
| 455 | goto leave; |
| 456 | } |
| 457 | |
| 458 | /* there are two cases which could cause us to EAGAIN in the |
| 459 | * we-need-more-metadata case: |
| 460 | * 1) we haven't reserved *any* |
| 461 | * 2) we are so fragmented, we've needed to add metadata too |
| 462 | * many times. */ |
| 463 | if (!free_extents && !meta_ac) { |
| 464 | mlog(0, "we haven't reserved any metadata!\n"); |
| 465 | status = -EAGAIN; |
| 466 | reason = RESTART_META; |
| 467 | goto leave; |
| 468 | } else if ((!free_extents) |
| 469 | && (ocfs2_alloc_context_bits_left(meta_ac) |
| 470 | < ocfs2_extend_meta_needed(fe))) { |
| 471 | mlog(0, "filesystem is really fragmented...\n"); |
| 472 | status = -EAGAIN; |
| 473 | reason = RESTART_META; |
| 474 | goto leave; |
| 475 | } |
| 476 | |
| 477 | status = ocfs2_claim_clusters(osb, handle, data_ac, 1, |
| 478 | &bit_off, &num_bits); |
| 479 | if (status < 0) { |
| 480 | if (status != -ENOSPC) |
| 481 | mlog_errno(status); |
| 482 | goto leave; |
| 483 | } |
| 484 | |
| 485 | BUG_ON(num_bits > clusters_to_add); |
| 486 | |
| 487 | /* reserve our write early -- insert_extent may update the inode */ |
| 488 | status = ocfs2_journal_access(handle, inode, fe_bh, |
| 489 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 490 | if (status < 0) { |
| 491 | mlog_errno(status); |
| 492 | goto leave; |
| 493 | } |
| 494 | |
| 495 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 496 | mlog(0, "Allocating %u clusters at block %u for inode %llu\n", |
| 497 | num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 498 | status = ocfs2_insert_extent(osb, handle, inode, fe_bh, |
| 499 | *logical_offset, block, num_bits, |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 500 | flags, meta_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 501 | if (status < 0) { |
| 502 | mlog_errno(status); |
| 503 | goto leave; |
| 504 | } |
| 505 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 506 | status = ocfs2_journal_dirty(handle, fe_bh); |
| 507 | if (status < 0) { |
| 508 | mlog_errno(status); |
| 509 | goto leave; |
| 510 | } |
| 511 | |
| 512 | clusters_to_add -= num_bits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 513 | *logical_offset += num_bits; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 514 | |
| 515 | if (clusters_to_add) { |
| 516 | mlog(0, "need to alloc once more, clusters = %u, wanted = " |
| 517 | "%u\n", fe->i_clusters, clusters_to_add); |
| 518 | status = -EAGAIN; |
| 519 | reason = RESTART_TRANS; |
| 520 | } |
| 521 | |
| 522 | leave: |
| 523 | mlog_exit(status); |
| 524 | if (reason_ret) |
| 525 | *reason_ret = reason; |
| 526 | return status; |
| 527 | } |
| 528 | |
Mark Fasheh | abf8b15 | 2007-01-17 13:07:24 -0800 | [diff] [blame] | 529 | /* |
| 530 | * For a given allocation, determine which allocators will need to be |
| 531 | * accessed, and lock them, reserving the appropriate number of bits. |
| 532 | * |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 533 | * Sparse file systems call this from ocfs2_write_begin_nolock() |
| 534 | * and ocfs2_allocate_unwritten_extents(). |
| 535 | * |
| 536 | * File systems which don't support holes call this from |
| 537 | * ocfs2_extend_allocation(). |
Mark Fasheh | abf8b15 | 2007-01-17 13:07:24 -0800 | [diff] [blame] | 538 | */ |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 539 | int ocfs2_lock_allocators(struct inode *inode, struct ocfs2_dinode *di, |
Mark Fasheh | b27b7cb | 2007-06-18 11:22:56 -0700 | [diff] [blame] | 540 | u32 clusters_to_add, u32 extents_to_split, |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 541 | struct ocfs2_alloc_context **data_ac, |
| 542 | struct ocfs2_alloc_context **meta_ac) |
Mark Fasheh | abf8b15 | 2007-01-17 13:07:24 -0800 | [diff] [blame] | 543 | { |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame^] | 544 | int ret = 0, num_free_extents; |
Mark Fasheh | b27b7cb | 2007-06-18 11:22:56 -0700 | [diff] [blame] | 545 | unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split; |
Mark Fasheh | abf8b15 | 2007-01-17 13:07:24 -0800 | [diff] [blame] | 546 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 547 | |
| 548 | *meta_ac = NULL; |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame^] | 549 | if (data_ac) |
| 550 | *data_ac = NULL; |
| 551 | |
| 552 | BUG_ON(clusters_to_add != 0 && data_ac == NULL); |
Mark Fasheh | abf8b15 | 2007-01-17 13:07:24 -0800 | [diff] [blame] | 553 | |
| 554 | mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, " |
Mark Fasheh | b27b7cb | 2007-06-18 11:22:56 -0700 | [diff] [blame] | 555 | "clusters_to_add = %u, extents_to_split = %u\n", |
Mark Fasheh | abf8b15 | 2007-01-17 13:07:24 -0800 | [diff] [blame] | 556 | (unsigned long long)OCFS2_I(inode)->ip_blkno, i_size_read(inode), |
Mark Fasheh | b27b7cb | 2007-06-18 11:22:56 -0700 | [diff] [blame] | 557 | le32_to_cpu(di->i_clusters), clusters_to_add, extents_to_split); |
Mark Fasheh | abf8b15 | 2007-01-17 13:07:24 -0800 | [diff] [blame] | 558 | |
| 559 | num_free_extents = ocfs2_num_free_extents(osb, inode, di); |
| 560 | if (num_free_extents < 0) { |
| 561 | ret = num_free_extents; |
| 562 | mlog_errno(ret); |
| 563 | goto out; |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Sparse allocation file systems need to be more conservative |
| 568 | * with reserving room for expansion - the actual allocation |
| 569 | * happens while we've got a journal handle open so re-taking |
| 570 | * a cluster lock (because we ran out of room for another |
| 571 | * extent) will violate ordering rules. |
| 572 | * |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 573 | * 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] | 574 | * anyway. |
Mark Fasheh | b27b7cb | 2007-06-18 11:22:56 -0700 | [diff] [blame] | 575 | * |
| 576 | * Always lock for any unwritten extents - we might want to |
| 577 | * add blocks during a split. |
Mark Fasheh | abf8b15 | 2007-01-17 13:07:24 -0800 | [diff] [blame] | 578 | */ |
| 579 | if (!num_free_extents || |
Mark Fasheh | b27b7cb | 2007-06-18 11:22:56 -0700 | [diff] [blame] | 580 | (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) { |
Mark Fasheh | abf8b15 | 2007-01-17 13:07:24 -0800 | [diff] [blame] | 581 | ret = ocfs2_reserve_new_metadata(osb, di, meta_ac); |
| 582 | if (ret < 0) { |
| 583 | if (ret != -ENOSPC) |
| 584 | mlog_errno(ret); |
| 585 | goto out; |
| 586 | } |
| 587 | } |
| 588 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame^] | 589 | if (clusters_to_add == 0) |
| 590 | goto out; |
| 591 | |
Mark Fasheh | abf8b15 | 2007-01-17 13:07:24 -0800 | [diff] [blame] | 592 | ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac); |
| 593 | if (ret < 0) { |
| 594 | if (ret != -ENOSPC) |
| 595 | mlog_errno(ret); |
| 596 | goto out; |
| 597 | } |
| 598 | |
| 599 | out: |
| 600 | if (ret) { |
| 601 | if (*meta_ac) { |
| 602 | ocfs2_free_alloc_context(*meta_ac); |
| 603 | *meta_ac = NULL; |
| 604 | } |
| 605 | |
| 606 | /* |
| 607 | * We cannot have an error and a non null *data_ac. |
| 608 | */ |
| 609 | } |
| 610 | |
| 611 | return ret; |
| 612 | } |
| 613 | |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 614 | static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start, |
| 615 | u32 clusters_to_add, int mark_unwritten) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 616 | { |
| 617 | int status = 0; |
| 618 | int restart_func = 0; |
Mark Fasheh | abf8b15 | 2007-01-17 13:07:24 -0800 | [diff] [blame] | 619 | int credits; |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 620 | u32 prev_clusters; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 621 | struct buffer_head *bh = NULL; |
| 622 | struct ocfs2_dinode *fe = NULL; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 623 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 624 | struct ocfs2_alloc_context *data_ac = NULL; |
| 625 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 626 | enum ocfs2_alloc_restarted why; |
| 627 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 628 | |
| 629 | mlog_entry("(clusters_to_add = %u)\n", clusters_to_add); |
| 630 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 631 | /* |
| 632 | * This function only exists for file systems which don't |
| 633 | * support holes. |
| 634 | */ |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 635 | BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 636 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 637 | status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh, |
| 638 | OCFS2_BH_CACHED, inode); |
| 639 | if (status < 0) { |
| 640 | mlog_errno(status); |
| 641 | goto leave; |
| 642 | } |
| 643 | |
| 644 | fe = (struct ocfs2_dinode *) bh->b_data; |
| 645 | if (!OCFS2_IS_VALID_DINODE(fe)) { |
| 646 | OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe); |
| 647 | status = -EIO; |
| 648 | goto leave; |
| 649 | } |
| 650 | |
| 651 | restart_all: |
| 652 | BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters); |
| 653 | |
Mark Fasheh | b27b7cb | 2007-06-18 11:22:56 -0700 | [diff] [blame] | 654 | status = ocfs2_lock_allocators(inode, fe, clusters_to_add, 0, &data_ac, |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 655 | &meta_ac); |
| 656 | if (status) { |
| 657 | mlog_errno(status); |
| 658 | goto leave; |
| 659 | } |
| 660 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 661 | credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add); |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 662 | handle = ocfs2_start_trans(osb, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 663 | if (IS_ERR(handle)) { |
| 664 | status = PTR_ERR(handle); |
| 665 | handle = NULL; |
| 666 | mlog_errno(status); |
| 667 | goto leave; |
| 668 | } |
| 669 | |
| 670 | restarted_transaction: |
| 671 | /* reserve a write to the file entry early on - that we if we |
| 672 | * run out of credits in the allocation path, we can still |
| 673 | * update i_size. */ |
| 674 | status = ocfs2_journal_access(handle, inode, bh, |
| 675 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 676 | if (status < 0) { |
| 677 | mlog_errno(status); |
| 678 | goto leave; |
| 679 | } |
| 680 | |
| 681 | prev_clusters = OCFS2_I(inode)->ip_clusters; |
| 682 | |
| 683 | status = ocfs2_do_extend_allocation(osb, |
| 684 | inode, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 685 | &logical_start, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 686 | clusters_to_add, |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 687 | mark_unwritten, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 688 | bh, |
| 689 | handle, |
| 690 | data_ac, |
| 691 | meta_ac, |
| 692 | &why); |
| 693 | if ((status < 0) && (status != -EAGAIN)) { |
| 694 | if (status != -ENOSPC) |
| 695 | mlog_errno(status); |
| 696 | goto leave; |
| 697 | } |
| 698 | |
| 699 | status = ocfs2_journal_dirty(handle, bh); |
| 700 | if (status < 0) { |
| 701 | mlog_errno(status); |
| 702 | goto leave; |
| 703 | } |
| 704 | |
| 705 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 706 | clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters); |
| 707 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 708 | |
| 709 | if (why != RESTART_NONE && clusters_to_add) { |
| 710 | if (why == RESTART_META) { |
| 711 | mlog(0, "restarting function.\n"); |
| 712 | restart_func = 1; |
| 713 | } else { |
| 714 | BUG_ON(why != RESTART_TRANS); |
| 715 | |
| 716 | mlog(0, "restarting transaction.\n"); |
| 717 | /* TODO: This can be more intelligent. */ |
| 718 | credits = ocfs2_calc_extend_credits(osb->sb, |
| 719 | fe, |
| 720 | clusters_to_add); |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 721 | status = ocfs2_extend_trans(handle, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 722 | if (status < 0) { |
| 723 | /* handle still has to be committed at |
| 724 | * this point. */ |
| 725 | status = -ENOMEM; |
| 726 | mlog_errno(status); |
| 727 | goto leave; |
| 728 | } |
| 729 | goto restarted_transaction; |
| 730 | } |
| 731 | } |
| 732 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 733 | mlog(0, "fe: i_clusters = %u, i_size=%llu\n", |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 734 | le32_to_cpu(fe->i_clusters), |
| 735 | (unsigned long long)le64_to_cpu(fe->i_size)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 736 | mlog(0, "inode: ip_clusters=%u, i_size=%lld\n", |
| 737 | OCFS2_I(inode)->ip_clusters, i_size_read(inode)); |
| 738 | |
| 739 | leave: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 740 | if (handle) { |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 741 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 742 | handle = NULL; |
| 743 | } |
| 744 | if (data_ac) { |
| 745 | ocfs2_free_alloc_context(data_ac); |
| 746 | data_ac = NULL; |
| 747 | } |
| 748 | if (meta_ac) { |
| 749 | ocfs2_free_alloc_context(meta_ac); |
| 750 | meta_ac = NULL; |
| 751 | } |
| 752 | if ((!status) && restart_func) { |
| 753 | restart_func = 0; |
| 754 | goto restart_all; |
| 755 | } |
| 756 | if (bh) { |
| 757 | brelse(bh); |
| 758 | bh = NULL; |
| 759 | } |
| 760 | |
| 761 | mlog_exit(status); |
| 762 | return status; |
| 763 | } |
| 764 | |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 765 | static int ocfs2_extend_allocation(struct inode *inode, u32 logical_start, |
| 766 | u32 clusters_to_add, int mark_unwritten) |
| 767 | { |
| 768 | int ret; |
| 769 | |
| 770 | /* |
| 771 | * The alloc sem blocks peope in read/write from reading our |
| 772 | * allocation until we're done changing it. We depend on |
| 773 | * i_mutex to block other extend/truncate calls while we're |
| 774 | * here. |
| 775 | */ |
| 776 | down_write(&OCFS2_I(inode)->ip_alloc_sem); |
| 777 | ret = __ocfs2_extend_allocation(inode, logical_start, clusters_to_add, |
| 778 | mark_unwritten); |
| 779 | up_write(&OCFS2_I(inode)->ip_alloc_sem); |
| 780 | |
| 781 | return ret; |
| 782 | } |
| 783 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 784 | /* Some parts of this taken from generic_cont_expand, which turned out |
| 785 | * 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] | 786 | * worry about recursive locking in ->prepare_write() and |
| 787 | * ->commit_write(). */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 788 | static int ocfs2_write_zero_page(struct inode *inode, |
| 789 | u64 size) |
| 790 | { |
| 791 | struct address_space *mapping = inode->i_mapping; |
| 792 | struct page *page; |
| 793 | unsigned long index; |
| 794 | unsigned int offset; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 795 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 796 | int ret; |
| 797 | |
| 798 | offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */ |
| 799 | /* ugh. in prepare/commit_write, if from==to==start of block, we |
| 800 | ** skip the prepare. make sure we never send an offset for the start |
| 801 | ** of a block |
| 802 | */ |
| 803 | if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) { |
| 804 | offset++; |
| 805 | } |
| 806 | index = size >> PAGE_CACHE_SHIFT; |
| 807 | |
| 808 | page = grab_cache_page(mapping, index); |
| 809 | if (!page) { |
| 810 | ret = -ENOMEM; |
| 811 | mlog_errno(ret); |
| 812 | goto out; |
| 813 | } |
| 814 | |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 815 | ret = ocfs2_prepare_write_nolock(inode, page, offset, offset); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 816 | if (ret < 0) { |
| 817 | mlog_errno(ret); |
| 818 | goto out_unlock; |
| 819 | } |
| 820 | |
| 821 | if (ocfs2_should_order_data(inode)) { |
| 822 | handle = ocfs2_start_walk_page_trans(inode, page, offset, |
| 823 | offset); |
| 824 | if (IS_ERR(handle)) { |
| 825 | ret = PTR_ERR(handle); |
| 826 | handle = NULL; |
| 827 | goto out_unlock; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | /* must not update i_size! */ |
| 832 | ret = block_commit_write(page, offset, offset); |
| 833 | if (ret < 0) |
| 834 | mlog_errno(ret); |
| 835 | else |
| 836 | ret = 0; |
| 837 | |
| 838 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 839 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 840 | out_unlock: |
| 841 | unlock_page(page); |
| 842 | page_cache_release(page); |
| 843 | out: |
| 844 | return ret; |
| 845 | } |
| 846 | |
| 847 | static int ocfs2_zero_extend(struct inode *inode, |
| 848 | u64 zero_to_size) |
| 849 | { |
| 850 | int ret = 0; |
| 851 | u64 start_off; |
| 852 | struct super_block *sb = inode->i_sb; |
| 853 | |
| 854 | start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode)); |
| 855 | while (start_off < zero_to_size) { |
| 856 | ret = ocfs2_write_zero_page(inode, start_off); |
| 857 | if (ret < 0) { |
| 858 | mlog_errno(ret); |
| 859 | goto out; |
| 860 | } |
| 861 | |
| 862 | start_off += sb->s_blocksize; |
Mark Fasheh | e2057c5 | 2006-10-03 17:53:05 -0700 | [diff] [blame] | 863 | |
| 864 | /* |
| 865 | * Very large extends have the potential to lock up |
| 866 | * the cpu for extended periods of time. |
| 867 | */ |
| 868 | cond_resched(); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | out: |
| 872 | return ret; |
| 873 | } |
| 874 | |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 875 | /* |
| 876 | * A tail_to_skip value > 0 indicates that we're being called from |
| 877 | * ocfs2_file_aio_write(). This has the following implications: |
| 878 | * |
| 879 | * - we don't want to update i_size |
| 880 | * - di_bh will be NULL, which is fine because it's only used in the |
| 881 | * case where we want to update i_size. |
| 882 | * - ocfs2_zero_extend() will then only be filling the hole created |
| 883 | * between i_size and the start of the write. |
| 884 | */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 885 | static int ocfs2_extend_file(struct inode *inode, |
| 886 | struct buffer_head *di_bh, |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 887 | u64 new_i_size, |
| 888 | size_t tail_to_skip) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 889 | { |
| 890 | int ret = 0; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 891 | u32 clusters_to_add = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 892 | |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 893 | BUG_ON(!tail_to_skip && !di_bh); |
| 894 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 895 | /* setattr sometimes calls us like this. */ |
| 896 | if (new_i_size == 0) |
| 897 | goto out; |
| 898 | |
| 899 | if (i_size_read(inode) == new_i_size) |
| 900 | goto out; |
| 901 | BUG_ON(new_i_size < i_size_read(inode)); |
| 902 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 903 | if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) { |
| 904 | BUG_ON(tail_to_skip != 0); |
| 905 | goto out_update_size; |
| 906 | } |
| 907 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 908 | clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) - |
| 909 | OCFS2_I(inode)->ip_clusters; |
| 910 | |
Mark Fasheh | 0effef7 | 2006-10-03 17:44:42 -0700 | [diff] [blame] | 911 | /* |
| 912 | * protect the pages that ocfs2_zero_extend is going to be |
| 913 | * pulling into the page cache.. we do this before the |
| 914 | * metadata extend so that we don't get into the situation |
| 915 | * where we've extended the metadata but can't get the data |
| 916 | * lock to zero. |
| 917 | */ |
| 918 | ret = ocfs2_data_lock(inode, 1); |
| 919 | if (ret < 0) { |
| 920 | mlog_errno(ret); |
| 921 | goto out; |
| 922 | } |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 923 | |
Mark Fasheh | 0effef7 | 2006-10-03 17:44:42 -0700 | [diff] [blame] | 924 | if (clusters_to_add) { |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 925 | ret = ocfs2_extend_allocation(inode, |
| 926 | OCFS2_I(inode)->ip_clusters, |
| 927 | clusters_to_add, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 928 | if (ret < 0) { |
| 929 | mlog_errno(ret); |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 930 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 931 | } |
Mark Fasheh | 0effef7 | 2006-10-03 17:44:42 -0700 | [diff] [blame] | 932 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 933 | |
Mark Fasheh | 0effef7 | 2006-10-03 17:44:42 -0700 | [diff] [blame] | 934 | /* |
| 935 | * Call this even if we don't add any clusters to the tree. We |
| 936 | * still need to zero the area between the old i_size and the |
| 937 | * new i_size. |
| 938 | */ |
| 939 | ret = ocfs2_zero_extend(inode, (u64)new_i_size - tail_to_skip); |
| 940 | if (ret < 0) { |
| 941 | mlog_errno(ret); |
| 942 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 943 | } |
| 944 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 945 | out_update_size: |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 946 | if (!tail_to_skip) { |
| 947 | /* We're being called from ocfs2_setattr() which wants |
| 948 | * us to update i_size */ |
| 949 | ret = ocfs2_simple_size_update(inode, di_bh, new_i_size); |
| 950 | if (ret < 0) |
| 951 | mlog_errno(ret); |
| 952 | } |
| 953 | |
| 954 | out_unlock: |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 955 | if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) |
| 956 | ocfs2_data_unlock(inode, 1); |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 957 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 958 | out: |
| 959 | return ret; |
| 960 | } |
| 961 | |
| 962 | int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) |
| 963 | { |
| 964 | int status = 0, size_change; |
| 965 | struct inode *inode = dentry->d_inode; |
| 966 | struct super_block *sb = inode->i_sb; |
| 967 | struct ocfs2_super *osb = OCFS2_SB(sb); |
| 968 | struct buffer_head *bh = NULL; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 969 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 970 | |
| 971 | mlog_entry("(0x%p, '%.*s')\n", dentry, |
| 972 | dentry->d_name.len, dentry->d_name.name); |
| 973 | |
| 974 | if (attr->ia_valid & ATTR_MODE) |
| 975 | mlog(0, "mode change: %d\n", attr->ia_mode); |
| 976 | if (attr->ia_valid & ATTR_UID) |
| 977 | mlog(0, "uid change: %d\n", attr->ia_uid); |
| 978 | if (attr->ia_valid & ATTR_GID) |
| 979 | mlog(0, "gid change: %d\n", attr->ia_gid); |
| 980 | if (attr->ia_valid & ATTR_SIZE) |
| 981 | mlog(0, "size change...\n"); |
| 982 | if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME)) |
| 983 | mlog(0, "time change...\n"); |
| 984 | |
| 985 | #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \ |
| 986 | | ATTR_GID | ATTR_UID | ATTR_MODE) |
| 987 | if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) { |
| 988 | mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid); |
| 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | status = inode_change_ok(inode, attr); |
| 993 | if (status) |
| 994 | return status; |
| 995 | |
| 996 | size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE; |
| 997 | if (size_change) { |
| 998 | status = ocfs2_rw_lock(inode, 1); |
| 999 | if (status < 0) { |
| 1000 | mlog_errno(status); |
| 1001 | goto bail; |
| 1002 | } |
| 1003 | } |
| 1004 | |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 1005 | status = ocfs2_meta_lock(inode, &bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1006 | if (status < 0) { |
| 1007 | if (status != -ENOENT) |
| 1008 | mlog_errno(status); |
| 1009 | goto bail_unlock_rw; |
| 1010 | } |
| 1011 | |
| 1012 | if (size_change && attr->ia_size != i_size_read(inode)) { |
| 1013 | if (i_size_read(inode) > attr->ia_size) |
| 1014 | status = ocfs2_truncate_file(inode, bh, attr->ia_size); |
| 1015 | else |
Mark Fasheh | 53013cb | 2006-05-05 19:04:03 -0700 | [diff] [blame] | 1016 | status = ocfs2_extend_file(inode, bh, attr->ia_size, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1017 | if (status < 0) { |
| 1018 | if (status != -ENOSPC) |
| 1019 | mlog_errno(status); |
| 1020 | status = -ENOSPC; |
| 1021 | goto bail_unlock; |
| 1022 | } |
| 1023 | } |
| 1024 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 1025 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1026 | if (IS_ERR(handle)) { |
| 1027 | status = PTR_ERR(handle); |
| 1028 | mlog_errno(status); |
| 1029 | goto bail_unlock; |
| 1030 | } |
| 1031 | |
Mark Fasheh | 7307de8 | 2007-05-09 15:16:19 -0700 | [diff] [blame] | 1032 | /* |
| 1033 | * This will intentionally not wind up calling vmtruncate(), |
| 1034 | * since all the work for a size change has been done above. |
| 1035 | * Otherwise, we could get into problems with truncate as |
| 1036 | * ip_alloc_sem is used there to protect against i_size |
| 1037 | * changes. |
| 1038 | */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1039 | status = inode_setattr(inode, attr); |
| 1040 | if (status < 0) { |
| 1041 | mlog_errno(status); |
| 1042 | goto bail_commit; |
| 1043 | } |
| 1044 | |
| 1045 | status = ocfs2_mark_inode_dirty(handle, inode, bh); |
| 1046 | if (status < 0) |
| 1047 | mlog_errno(status); |
| 1048 | |
| 1049 | bail_commit: |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 1050 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1051 | bail_unlock: |
| 1052 | ocfs2_meta_unlock(inode, 1); |
| 1053 | bail_unlock_rw: |
| 1054 | if (size_change) |
| 1055 | ocfs2_rw_unlock(inode, 1); |
| 1056 | bail: |
| 1057 | if (bh) |
| 1058 | brelse(bh); |
| 1059 | |
| 1060 | mlog_exit(status); |
| 1061 | return status; |
| 1062 | } |
| 1063 | |
| 1064 | int ocfs2_getattr(struct vfsmount *mnt, |
| 1065 | struct dentry *dentry, |
| 1066 | struct kstat *stat) |
| 1067 | { |
| 1068 | struct inode *inode = dentry->d_inode; |
| 1069 | struct super_block *sb = dentry->d_inode->i_sb; |
| 1070 | struct ocfs2_super *osb = sb->s_fs_info; |
| 1071 | int err; |
| 1072 | |
| 1073 | mlog_entry_void(); |
| 1074 | |
| 1075 | err = ocfs2_inode_revalidate(dentry); |
| 1076 | if (err) { |
| 1077 | if (err != -ENOENT) |
| 1078 | mlog_errno(err); |
| 1079 | goto bail; |
| 1080 | } |
| 1081 | |
| 1082 | generic_fillattr(inode, stat); |
| 1083 | |
| 1084 | /* We set the blksize from the cluster size for performance */ |
| 1085 | stat->blksize = osb->s_clustersize; |
| 1086 | |
| 1087 | bail: |
| 1088 | mlog_exit(err); |
| 1089 | |
| 1090 | return err; |
| 1091 | } |
| 1092 | |
Tiger Yang | d38eb8d | 2006-11-27 09:59:21 +0800 | [diff] [blame] | 1093 | int ocfs2_permission(struct inode *inode, int mask, struct nameidata *nd) |
| 1094 | { |
| 1095 | int ret; |
| 1096 | |
| 1097 | mlog_entry_void(); |
| 1098 | |
| 1099 | ret = ocfs2_meta_lock(inode, NULL, 0); |
| 1100 | if (ret) { |
Mark Fasheh | a9f5f70 | 2007-04-26 11:43:43 -0700 | [diff] [blame] | 1101 | if (ret != -ENOENT) |
| 1102 | mlog_errno(ret); |
Tiger Yang | d38eb8d | 2006-11-27 09:59:21 +0800 | [diff] [blame] | 1103 | goto out; |
| 1104 | } |
| 1105 | |
| 1106 | ret = generic_permission(inode, mask, NULL); |
Tiger Yang | d38eb8d | 2006-11-27 09:59:21 +0800 | [diff] [blame] | 1107 | |
| 1108 | ocfs2_meta_unlock(inode, 0); |
| 1109 | out: |
| 1110 | mlog_exit(ret); |
| 1111 | return ret; |
| 1112 | } |
| 1113 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1114 | static int ocfs2_write_remove_suid(struct inode *inode) |
| 1115 | { |
| 1116 | int ret; |
| 1117 | struct buffer_head *bh = NULL; |
| 1118 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1119 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1120 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1121 | struct ocfs2_dinode *di; |
| 1122 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1123 | mlog_entry("(Inode %llu, mode 0%o)\n", |
| 1124 | (unsigned long long)oi->ip_blkno, inode->i_mode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1125 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 1126 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1127 | if (handle == NULL) { |
| 1128 | ret = -ENOMEM; |
| 1129 | mlog_errno(ret); |
| 1130 | goto out; |
| 1131 | } |
| 1132 | |
| 1133 | ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode); |
| 1134 | if (ret < 0) { |
| 1135 | mlog_errno(ret); |
| 1136 | goto out_trans; |
| 1137 | } |
| 1138 | |
| 1139 | ret = ocfs2_journal_access(handle, inode, bh, |
| 1140 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1141 | if (ret < 0) { |
| 1142 | mlog_errno(ret); |
| 1143 | goto out_bh; |
| 1144 | } |
| 1145 | |
| 1146 | inode->i_mode &= ~S_ISUID; |
| 1147 | if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP)) |
| 1148 | inode->i_mode &= ~S_ISGID; |
| 1149 | |
| 1150 | di = (struct ocfs2_dinode *) bh->b_data; |
| 1151 | di->i_mode = cpu_to_le16(inode->i_mode); |
| 1152 | |
| 1153 | ret = ocfs2_journal_dirty(handle, bh); |
| 1154 | if (ret < 0) |
| 1155 | mlog_errno(ret); |
| 1156 | out_bh: |
| 1157 | brelse(bh); |
| 1158 | out_trans: |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 1159 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1160 | out: |
| 1161 | mlog_exit(ret); |
| 1162 | return ret; |
| 1163 | } |
| 1164 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1165 | /* |
| 1166 | * Will look for holes and unwritten extents in the range starting at |
| 1167 | * pos for count bytes (inclusive). |
| 1168 | */ |
| 1169 | static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos, |
| 1170 | size_t count) |
| 1171 | { |
| 1172 | int ret = 0; |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame] | 1173 | unsigned int extent_flags; |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1174 | u32 cpos, clusters, extent_len, phys_cpos; |
| 1175 | struct super_block *sb = inode->i_sb; |
| 1176 | |
| 1177 | cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits; |
| 1178 | clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos; |
| 1179 | |
| 1180 | while (clusters) { |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame] | 1181 | ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len, |
| 1182 | &extent_flags); |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1183 | if (ret < 0) { |
| 1184 | mlog_errno(ret); |
| 1185 | goto out; |
| 1186 | } |
| 1187 | |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame] | 1188 | if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) { |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1189 | ret = 1; |
| 1190 | break; |
| 1191 | } |
| 1192 | |
| 1193 | if (extent_len > clusters) |
| 1194 | extent_len = clusters; |
| 1195 | |
| 1196 | clusters -= extent_len; |
| 1197 | cpos += extent_len; |
| 1198 | } |
| 1199 | out: |
| 1200 | return ret; |
| 1201 | } |
| 1202 | |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 1203 | /* |
| 1204 | * Allocate enough extents to cover the region starting at byte offset |
| 1205 | * start for len bytes. Existing extents are skipped, any extents |
| 1206 | * added are marked as "unwritten". |
| 1207 | */ |
| 1208 | static int ocfs2_allocate_unwritten_extents(struct inode *inode, |
| 1209 | u64 start, u64 len) |
| 1210 | { |
| 1211 | int ret; |
| 1212 | u32 cpos, phys_cpos, clusters, alloc_size; |
| 1213 | |
| 1214 | /* |
| 1215 | * We consider both start and len to be inclusive. |
| 1216 | */ |
| 1217 | cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits; |
| 1218 | clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len); |
| 1219 | clusters -= cpos; |
| 1220 | |
| 1221 | while (clusters) { |
| 1222 | ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, |
| 1223 | &alloc_size, NULL); |
| 1224 | if (ret) { |
| 1225 | mlog_errno(ret); |
| 1226 | goto out; |
| 1227 | } |
| 1228 | |
| 1229 | /* |
| 1230 | * Hole or existing extent len can be arbitrary, so |
| 1231 | * cap it to our own allocation request. |
| 1232 | */ |
| 1233 | if (alloc_size > clusters) |
| 1234 | alloc_size = clusters; |
| 1235 | |
| 1236 | if (phys_cpos) { |
| 1237 | /* |
| 1238 | * We already have an allocation at this |
| 1239 | * region so we can safely skip it. |
| 1240 | */ |
| 1241 | goto next; |
| 1242 | } |
| 1243 | |
| 1244 | ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1); |
| 1245 | if (ret) { |
| 1246 | if (ret != -ENOSPC) |
| 1247 | mlog_errno(ret); |
| 1248 | goto out; |
| 1249 | } |
| 1250 | |
| 1251 | next: |
| 1252 | cpos += alloc_size; |
| 1253 | clusters -= alloc_size; |
| 1254 | } |
| 1255 | |
| 1256 | ret = 0; |
| 1257 | out: |
| 1258 | return ret; |
| 1259 | } |
| 1260 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame^] | 1261 | static int __ocfs2_remove_inode_range(struct inode *inode, |
| 1262 | struct buffer_head *di_bh, |
| 1263 | u32 cpos, u32 phys_cpos, u32 len, |
| 1264 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 1265 | { |
| 1266 | int ret; |
| 1267 | u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); |
| 1268 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1269 | struct inode *tl_inode = osb->osb_tl_inode; |
| 1270 | handle_t *handle; |
| 1271 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 1272 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 1273 | |
| 1274 | ret = ocfs2_lock_allocators(inode, di, 0, 1, NULL, &meta_ac); |
| 1275 | if (ret) { |
| 1276 | mlog_errno(ret); |
| 1277 | return ret; |
| 1278 | } |
| 1279 | |
| 1280 | mutex_lock(&tl_inode->i_mutex); |
| 1281 | |
| 1282 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 1283 | ret = __ocfs2_flush_truncate_log(osb); |
| 1284 | if (ret < 0) { |
| 1285 | mlog_errno(ret); |
| 1286 | goto out; |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS); |
| 1291 | if (handle == NULL) { |
| 1292 | ret = -ENOMEM; |
| 1293 | mlog_errno(ret); |
| 1294 | goto out; |
| 1295 | } |
| 1296 | |
| 1297 | ret = ocfs2_journal_access(handle, inode, di_bh, |
| 1298 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1299 | if (ret) { |
| 1300 | mlog_errno(ret); |
| 1301 | goto out; |
| 1302 | } |
| 1303 | |
| 1304 | ret = ocfs2_remove_extent(inode, di_bh, cpos, len, handle, meta_ac, |
| 1305 | dealloc); |
| 1306 | if (ret) { |
| 1307 | mlog_errno(ret); |
| 1308 | goto out_commit; |
| 1309 | } |
| 1310 | |
| 1311 | OCFS2_I(inode)->ip_clusters -= len; |
| 1312 | di->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters); |
| 1313 | |
| 1314 | ret = ocfs2_journal_dirty(handle, di_bh); |
| 1315 | if (ret) { |
| 1316 | mlog_errno(ret); |
| 1317 | goto out_commit; |
| 1318 | } |
| 1319 | |
| 1320 | ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len); |
| 1321 | if (ret) |
| 1322 | mlog_errno(ret); |
| 1323 | |
| 1324 | out_commit: |
| 1325 | ocfs2_commit_trans(osb, handle); |
| 1326 | out: |
| 1327 | mutex_unlock(&tl_inode->i_mutex); |
| 1328 | |
| 1329 | if (meta_ac) |
| 1330 | ocfs2_free_alloc_context(meta_ac); |
| 1331 | |
| 1332 | return ret; |
| 1333 | } |
| 1334 | |
| 1335 | /* |
| 1336 | * Truncate a byte range, avoiding pages within partial clusters. This |
| 1337 | * preserves those pages for the zeroing code to write to. |
| 1338 | */ |
| 1339 | static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start, |
| 1340 | u64 byte_len) |
| 1341 | { |
| 1342 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1343 | loff_t start, end; |
| 1344 | struct address_space *mapping = inode->i_mapping; |
| 1345 | |
| 1346 | start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start); |
| 1347 | end = byte_start + byte_len; |
| 1348 | end = end & ~(osb->s_clustersize - 1); |
| 1349 | |
| 1350 | if (start < end) { |
| 1351 | unmap_mapping_range(mapping, start, end - start, 0); |
| 1352 | truncate_inode_pages_range(mapping, start, end - 1); |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | static int ocfs2_zero_partial_clusters(struct inode *inode, |
| 1357 | u64 start, u64 len) |
| 1358 | { |
| 1359 | int ret = 0; |
| 1360 | u64 tmpend, end = start + len; |
| 1361 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1362 | unsigned int csize = osb->s_clustersize; |
| 1363 | handle_t *handle; |
| 1364 | |
| 1365 | /* |
| 1366 | * The "start" and "end" values are NOT necessarily part of |
| 1367 | * the range whose allocation is being deleted. Rather, this |
| 1368 | * is what the user passed in with the request. We must zero |
| 1369 | * partial clusters here. There's no need to worry about |
| 1370 | * physical allocation - the zeroing code knows to skip holes. |
| 1371 | */ |
| 1372 | mlog(0, "byte start: %llu, end: %llu\n", |
| 1373 | (unsigned long long)start, (unsigned long long)end); |
| 1374 | |
| 1375 | /* |
| 1376 | * If both edges are on a cluster boundary then there's no |
| 1377 | * zeroing required as the region is part of the allocation to |
| 1378 | * be truncated. |
| 1379 | */ |
| 1380 | if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0) |
| 1381 | goto out; |
| 1382 | |
| 1383 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); |
| 1384 | if (handle == NULL) { |
| 1385 | ret = -ENOMEM; |
| 1386 | mlog_errno(ret); |
| 1387 | goto out; |
| 1388 | } |
| 1389 | |
| 1390 | /* |
| 1391 | * We want to get the byte offset of the end of the 1st cluster. |
| 1392 | */ |
| 1393 | tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1)); |
| 1394 | if (tmpend > end) |
| 1395 | tmpend = end; |
| 1396 | |
| 1397 | mlog(0, "1st range: start: %llu, tmpend: %llu\n", |
| 1398 | (unsigned long long)start, (unsigned long long)tmpend); |
| 1399 | |
| 1400 | ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend); |
| 1401 | if (ret) |
| 1402 | mlog_errno(ret); |
| 1403 | |
| 1404 | if (tmpend < end) { |
| 1405 | /* |
| 1406 | * This may make start and end equal, but the zeroing |
| 1407 | * code will skip any work in that case so there's no |
| 1408 | * need to catch it up here. |
| 1409 | */ |
| 1410 | start = end & ~(osb->s_clustersize - 1); |
| 1411 | |
| 1412 | mlog(0, "2nd range: start: %llu, end: %llu\n", |
| 1413 | (unsigned long long)start, (unsigned long long)end); |
| 1414 | |
| 1415 | ret = ocfs2_zero_range_for_truncate(inode, handle, start, end); |
| 1416 | if (ret) |
| 1417 | mlog_errno(ret); |
| 1418 | } |
| 1419 | |
| 1420 | ocfs2_commit_trans(osb, handle); |
| 1421 | out: |
| 1422 | return ret; |
| 1423 | } |
| 1424 | |
| 1425 | static int ocfs2_remove_inode_range(struct inode *inode, |
| 1426 | struct buffer_head *di_bh, u64 byte_start, |
| 1427 | u64 byte_len) |
| 1428 | { |
| 1429 | int ret = 0; |
| 1430 | u32 trunc_start, trunc_len, cpos, phys_cpos, alloc_size; |
| 1431 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 1432 | struct ocfs2_cached_dealloc_ctxt dealloc; |
| 1433 | |
| 1434 | ocfs2_init_dealloc_ctxt(&dealloc); |
| 1435 | |
| 1436 | if (byte_len == 0) |
| 1437 | return 0; |
| 1438 | |
| 1439 | trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start); |
| 1440 | trunc_len = (byte_start + byte_len) >> osb->s_clustersize_bits; |
| 1441 | if (trunc_len >= trunc_start) |
| 1442 | trunc_len -= trunc_start; |
| 1443 | else |
| 1444 | trunc_len = 0; |
| 1445 | |
| 1446 | mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u\n", |
| 1447 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 1448 | (unsigned long long)byte_start, |
| 1449 | (unsigned long long)byte_len, trunc_start, trunc_len); |
| 1450 | |
| 1451 | ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len); |
| 1452 | if (ret) { |
| 1453 | mlog_errno(ret); |
| 1454 | goto out; |
| 1455 | } |
| 1456 | |
| 1457 | cpos = trunc_start; |
| 1458 | while (trunc_len) { |
| 1459 | ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, |
| 1460 | &alloc_size, NULL); |
| 1461 | if (ret) { |
| 1462 | mlog_errno(ret); |
| 1463 | goto out; |
| 1464 | } |
| 1465 | |
| 1466 | if (alloc_size > trunc_len) |
| 1467 | alloc_size = trunc_len; |
| 1468 | |
| 1469 | /* Only do work for non-holes */ |
| 1470 | if (phys_cpos != 0) { |
| 1471 | ret = __ocfs2_remove_inode_range(inode, di_bh, cpos, |
| 1472 | phys_cpos, alloc_size, |
| 1473 | &dealloc); |
| 1474 | if (ret) { |
| 1475 | mlog_errno(ret); |
| 1476 | goto out; |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | cpos += alloc_size; |
| 1481 | trunc_len -= alloc_size; |
| 1482 | } |
| 1483 | |
| 1484 | ocfs2_truncate_cluster_pages(inode, byte_start, byte_len); |
| 1485 | |
| 1486 | out: |
| 1487 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 1488 | ocfs2_run_deallocs(osb, &dealloc); |
| 1489 | |
| 1490 | return ret; |
| 1491 | } |
| 1492 | |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1493 | static int ocfs2_prepare_inode_for_write(struct dentry *dentry, |
| 1494 | loff_t *ppos, |
| 1495 | size_t count, |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1496 | int appending, |
| 1497 | int *direct_io) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1498 | { |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1499 | int ret = 0, meta_level = appending; |
| 1500 | struct inode *inode = dentry->d_inode; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1501 | u32 clusters; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1502 | loff_t newsize, saved_pos; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1503 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1504 | /* |
| 1505 | * We sample i_size under a read level meta lock to see if our write |
| 1506 | * is extending the file, if it is we back off and get a write level |
| 1507 | * meta lock. |
| 1508 | */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1509 | for(;;) { |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 1510 | ret = ocfs2_meta_lock(inode, NULL, meta_level); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1511 | if (ret < 0) { |
| 1512 | meta_level = -1; |
| 1513 | mlog_errno(ret); |
| 1514 | goto out; |
| 1515 | } |
| 1516 | |
| 1517 | /* Clear suid / sgid if necessary. We do this here |
| 1518 | * instead of later in the write path because |
| 1519 | * remove_suid() calls ->setattr without any hint that |
| 1520 | * we may have already done our cluster locking. Since |
| 1521 | * ocfs2_setattr() *must* take cluster locks to |
| 1522 | * proceeed, this will lead us to recursively lock the |
| 1523 | * inode. There's also the dinode i_size state which |
| 1524 | * can be lost via setattr during extending writes (we |
| 1525 | * set inode->i_size at the end of a write. */ |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1526 | if (should_remove_suid(dentry)) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1527 | if (meta_level == 0) { |
| 1528 | ocfs2_meta_unlock(inode, meta_level); |
| 1529 | meta_level = 1; |
| 1530 | continue; |
| 1531 | } |
| 1532 | |
| 1533 | ret = ocfs2_write_remove_suid(inode); |
| 1534 | if (ret < 0) { |
| 1535 | mlog_errno(ret); |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1536 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | /* work on a copy of ppos until we're sure that we won't have |
| 1541 | * to recalculate it due to relocking. */ |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1542 | if (appending) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1543 | saved_pos = i_size_read(inode); |
| 1544 | mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos); |
| 1545 | } else { |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1546 | saved_pos = *ppos; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1547 | } |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 1548 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1549 | if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) { |
| 1550 | loff_t end = saved_pos + count; |
| 1551 | |
| 1552 | /* |
| 1553 | * Skip the O_DIRECT checks if we don't need |
| 1554 | * them. |
| 1555 | */ |
| 1556 | if (!direct_io || !(*direct_io)) |
| 1557 | break; |
| 1558 | |
| 1559 | /* |
| 1560 | * Allowing concurrent direct writes means |
| 1561 | * i_size changes wouldn't be synchronized, so |
| 1562 | * one node could wind up truncating another |
| 1563 | * nodes writes. |
| 1564 | */ |
| 1565 | if (end > i_size_read(inode)) { |
| 1566 | *direct_io = 0; |
| 1567 | break; |
| 1568 | } |
| 1569 | |
| 1570 | /* |
| 1571 | * We don't fill holes during direct io, so |
| 1572 | * check for them here. If any are found, the |
| 1573 | * caller will have to retake some cluster |
| 1574 | * locks and initiate the io as buffered. |
| 1575 | */ |
| 1576 | ret = ocfs2_check_range_for_holes(inode, saved_pos, |
| 1577 | count); |
| 1578 | if (ret == 1) { |
| 1579 | *direct_io = 0; |
| 1580 | ret = 0; |
| 1581 | } else if (ret < 0) |
| 1582 | mlog_errno(ret); |
| 1583 | break; |
| 1584 | } |
| 1585 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 1586 | /* |
| 1587 | * The rest of this loop is concerned with legacy file |
| 1588 | * systems which don't support sparse files. |
| 1589 | */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 1590 | |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1591 | newsize = count + saved_pos; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1592 | |
Mark Fasheh | 215c7f9 | 2006-02-01 16:42:10 -0800 | [diff] [blame] | 1593 | mlog(0, "pos=%lld newsize=%lld cursize=%lld\n", |
| 1594 | (long long) saved_pos, (long long) newsize, |
| 1595 | (long long) i_size_read(inode)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1596 | |
| 1597 | /* No need for a higher level metadata lock if we're |
| 1598 | * never going past i_size. */ |
| 1599 | if (newsize <= i_size_read(inode)) |
| 1600 | break; |
| 1601 | |
| 1602 | if (meta_level == 0) { |
| 1603 | ocfs2_meta_unlock(inode, meta_level); |
| 1604 | meta_level = 1; |
| 1605 | continue; |
| 1606 | } |
| 1607 | |
| 1608 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 1609 | clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) - |
| 1610 | OCFS2_I(inode)->ip_clusters; |
| 1611 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 1612 | |
| 1613 | mlog(0, "Writing at EOF, may need more allocation: " |
Mark Fasheh | 215c7f9 | 2006-02-01 16:42:10 -0800 | [diff] [blame] | 1614 | "i_size = %lld, newsize = %lld, need %u clusters\n", |
| 1615 | (long long) i_size_read(inode), (long long) newsize, |
| 1616 | clusters); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1617 | |
| 1618 | /* We only want to continue the rest of this loop if |
| 1619 | * our extend will actually require more |
| 1620 | * allocation. */ |
| 1621 | if (!clusters) |
| 1622 | break; |
| 1623 | |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1624 | ret = ocfs2_extend_file(inode, NULL, newsize, count); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1625 | if (ret < 0) { |
| 1626 | if (ret != -ENOSPC) |
| 1627 | mlog_errno(ret); |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1628 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1629 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1630 | break; |
| 1631 | } |
| 1632 | |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1633 | if (appending) |
| 1634 | *ppos = saved_pos; |
| 1635 | |
| 1636 | out_unlock: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1637 | ocfs2_meta_unlock(inode, meta_level); |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1638 | |
| 1639 | out: |
| 1640 | return ret; |
| 1641 | } |
| 1642 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1643 | static inline void |
| 1644 | ocfs2_set_next_iovec(const struct iovec **iovp, size_t *basep, size_t bytes) |
| 1645 | { |
| 1646 | const struct iovec *iov = *iovp; |
| 1647 | size_t base = *basep; |
| 1648 | |
| 1649 | do { |
| 1650 | int copy = min(bytes, iov->iov_len - base); |
| 1651 | |
| 1652 | bytes -= copy; |
| 1653 | base += copy; |
| 1654 | if (iov->iov_len == base) { |
| 1655 | iov++; |
| 1656 | base = 0; |
| 1657 | } |
| 1658 | } while (bytes); |
| 1659 | *iovp = iov; |
| 1660 | *basep = base; |
| 1661 | } |
| 1662 | |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1663 | static struct page * ocfs2_get_write_source(char **ret_src_buf, |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1664 | const struct iovec *cur_iov, |
| 1665 | size_t iov_offset) |
| 1666 | { |
| 1667 | int ret; |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1668 | char *buf = cur_iov->iov_base + iov_offset; |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1669 | struct page *src_page = NULL; |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1670 | unsigned long off; |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1671 | |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1672 | off = (unsigned long)(buf) & ~PAGE_CACHE_MASK; |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1673 | |
| 1674 | if (!segment_eq(get_fs(), KERNEL_DS)) { |
| 1675 | /* |
| 1676 | * Pull in the user page. We want to do this outside |
| 1677 | * of the meta data locks in order to preserve locking |
| 1678 | * order in case of page fault. |
| 1679 | */ |
| 1680 | ret = get_user_pages(current, current->mm, |
| 1681 | (unsigned long)buf & PAGE_CACHE_MASK, 1, |
| 1682 | 0, 0, &src_page, NULL); |
| 1683 | if (ret == 1) |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1684 | *ret_src_buf = kmap(src_page) + off; |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1685 | else |
| 1686 | src_page = ERR_PTR(-EFAULT); |
| 1687 | } else { |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1688 | *ret_src_buf = buf; |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | return src_page; |
| 1692 | } |
| 1693 | |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1694 | static void ocfs2_put_write_source(struct page *page) |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1695 | { |
| 1696 | if (page) { |
| 1697 | kunmap(page); |
| 1698 | page_cache_release(page); |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | static ssize_t ocfs2_file_buffered_write(struct file *file, loff_t *ppos, |
| 1703 | const struct iovec *iov, |
| 1704 | unsigned long nr_segs, |
| 1705 | size_t count, |
| 1706 | ssize_t o_direct_written) |
| 1707 | { |
| 1708 | int ret = 0; |
| 1709 | ssize_t copied, total = 0; |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1710 | size_t iov_offset = 0, bytes; |
| 1711 | loff_t pos; |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1712 | const struct iovec *cur_iov = iov; |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1713 | struct page *user_page, *page; |
| 1714 | char *buf, *dst; |
| 1715 | void *fsdata; |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1716 | |
| 1717 | /* |
| 1718 | * handle partial DIO write. Adjust cur_iov if needed. |
| 1719 | */ |
| 1720 | ocfs2_set_next_iovec(&cur_iov, &iov_offset, o_direct_written); |
| 1721 | |
| 1722 | do { |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1723 | pos = *ppos; |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1724 | |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1725 | user_page = ocfs2_get_write_source(&buf, cur_iov, iov_offset); |
| 1726 | if (IS_ERR(user_page)) { |
| 1727 | ret = PTR_ERR(user_page); |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1728 | goto out; |
| 1729 | } |
| 1730 | |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1731 | /* Stay within our page boundaries */ |
| 1732 | bytes = min((PAGE_CACHE_SIZE - ((unsigned long)pos & ~PAGE_CACHE_MASK)), |
| 1733 | (PAGE_CACHE_SIZE - ((unsigned long)buf & ~PAGE_CACHE_MASK))); |
| 1734 | /* Stay within the vector boundary */ |
| 1735 | bytes = min_t(size_t, bytes, cur_iov->iov_len - iov_offset); |
| 1736 | /* Stay within count */ |
| 1737 | bytes = min(bytes, count); |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1738 | |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1739 | page = NULL; |
| 1740 | ret = ocfs2_write_begin(file, file->f_mapping, pos, bytes, 0, |
| 1741 | &page, &fsdata); |
| 1742 | if (ret) { |
| 1743 | mlog_errno(ret); |
| 1744 | goto out; |
| 1745 | } |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1746 | |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1747 | dst = kmap_atomic(page, KM_USER0); |
| 1748 | memcpy(dst + (pos & (PAGE_CACHE_SIZE - 1)), buf, bytes); |
| 1749 | kunmap_atomic(dst, KM_USER0); |
| 1750 | flush_dcache_page(page); |
| 1751 | ocfs2_put_write_source(user_page); |
| 1752 | |
| 1753 | copied = ocfs2_write_end(file, file->f_mapping, pos, bytes, |
| 1754 | bytes, page, fsdata); |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1755 | if (copied < 0) { |
| 1756 | mlog_errno(copied); |
| 1757 | ret = copied; |
| 1758 | goto out; |
| 1759 | } |
| 1760 | |
| 1761 | total += copied; |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1762 | *ppos = pos + copied; |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1763 | count -= copied; |
| 1764 | |
| 1765 | ocfs2_set_next_iovec(&cur_iov, &iov_offset, copied); |
| 1766 | } while(count); |
| 1767 | |
| 1768 | out: |
| 1769 | return total ? total : ret; |
| 1770 | } |
| 1771 | |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1772 | static ssize_t ocfs2_file_aio_write(struct kiocb *iocb, |
| 1773 | const struct iovec *iov, |
| 1774 | unsigned long nr_segs, |
| 1775 | loff_t pos) |
| 1776 | { |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1777 | int ret, direct_io, appending, rw_level, have_alloc_sem = 0; |
| 1778 | int can_do_direct, sync = 0; |
| 1779 | ssize_t written = 0; |
| 1780 | size_t ocount; /* original count */ |
| 1781 | size_t count; /* after file limit checks */ |
| 1782 | loff_t *ppos = &iocb->ki_pos; |
| 1783 | struct file *file = iocb->ki_filp; |
| 1784 | struct inode *inode = file->f_path.dentry->d_inode; |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1785 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1786 | mlog_entry("(0x%p, %u, '%.*s')\n", file, |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1787 | (unsigned int)nr_segs, |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1788 | file->f_path.dentry->d_name.len, |
| 1789 | file->f_path.dentry->d_name.name); |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1790 | |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1791 | if (iocb->ki_left == 0) |
| 1792 | return 0; |
| 1793 | |
Christoph Hellwig | d9b08b9 | 2007-05-18 13:12:40 +0200 | [diff] [blame] | 1794 | ret = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ); |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1795 | if (ret) |
| 1796 | return ret; |
| 1797 | |
| 1798 | count = ocount; |
| 1799 | |
| 1800 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
| 1801 | |
| 1802 | appending = file->f_flags & O_APPEND ? 1 : 0; |
| 1803 | direct_io = file->f_flags & O_DIRECT ? 1 : 0; |
| 1804 | |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1805 | mutex_lock(&inode->i_mutex); |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1806 | |
| 1807 | relock: |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1808 | /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */ |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1809 | if (direct_io) { |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1810 | down_read(&inode->i_alloc_sem); |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1811 | have_alloc_sem = 1; |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | /* concurrent O_DIRECT writes are allowed */ |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1815 | rw_level = !direct_io; |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1816 | ret = ocfs2_rw_lock(inode, rw_level); |
| 1817 | if (ret < 0) { |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1818 | mlog_errno(ret); |
| 1819 | goto out_sems; |
| 1820 | } |
| 1821 | |
| 1822 | can_do_direct = direct_io; |
| 1823 | ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos, |
| 1824 | iocb->ki_left, appending, |
| 1825 | &can_do_direct); |
| 1826 | if (ret < 0) { |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1827 | mlog_errno(ret); |
| 1828 | goto out; |
| 1829 | } |
| 1830 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1831 | /* |
| 1832 | * We can't complete the direct I/O as requested, fall back to |
| 1833 | * buffered I/O. |
| 1834 | */ |
| 1835 | if (direct_io && !can_do_direct) { |
| 1836 | ocfs2_rw_unlock(inode, rw_level); |
| 1837 | up_read(&inode->i_alloc_sem); |
| 1838 | |
| 1839 | have_alloc_sem = 0; |
| 1840 | rw_level = -1; |
| 1841 | |
| 1842 | direct_io = 0; |
| 1843 | sync = 1; |
| 1844 | goto relock; |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 1845 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1846 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1847 | if (!sync && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) |
| 1848 | sync = 1; |
| 1849 | |
| 1850 | /* |
| 1851 | * XXX: Is it ok to execute these checks a second time? |
| 1852 | */ |
| 1853 | ret = generic_write_checks(file, ppos, &count, S_ISBLK(inode->i_mode)); |
| 1854 | if (ret) |
| 1855 | goto out; |
| 1856 | |
| 1857 | /* |
| 1858 | * Set pos so that sync_page_range_nolock() below understands |
| 1859 | * where to start from. We might've moved it around via the |
| 1860 | * calls above. The range we want to actually sync starts from |
| 1861 | * *ppos here. |
| 1862 | * |
| 1863 | */ |
| 1864 | pos = *ppos; |
| 1865 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1866 | /* communicate with ocfs2_dio_end_io */ |
Mark Fasheh | 7cdfc3a | 2007-04-16 17:28:51 -0700 | [diff] [blame] | 1867 | ocfs2_iocb_set_rw_locked(iocb, rw_level); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1868 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1869 | if (direct_io) { |
| 1870 | written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos, |
| 1871 | ppos, count, ocount); |
| 1872 | if (written < 0) { |
| 1873 | ret = written; |
| 1874 | goto out_dio; |
| 1875 | } |
| 1876 | } else { |
| 1877 | written = ocfs2_file_buffered_write(file, ppos, iov, nr_segs, |
| 1878 | count, written); |
| 1879 | if (written < 0) { |
| 1880 | ret = written; |
| 1881 | if (ret != -EFAULT || ret != -ENOSPC) |
| 1882 | mlog_errno(ret); |
| 1883 | goto out; |
| 1884 | } |
| 1885 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1886 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1887 | out_dio: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1888 | /* buffered aio wouldn't have proper lock coverage today */ |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1889 | BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1890 | |
| 1891 | /* |
| 1892 | * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io |
| 1893 | * function pointer which is called when o_direct io completes so that |
| 1894 | * it can unlock our rw lock. (it's the clustered equivalent of |
| 1895 | * i_alloc_sem; protects truncate from racing with pending ios). |
| 1896 | * Unfortunately there are error cases which call end_io and others |
| 1897 | * that don't. so we don't have to unlock the rw_lock if either an |
| 1898 | * async dio is going to do it in the future or an end_io after an |
| 1899 | * error has already done it. |
| 1900 | */ |
| 1901 | if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) { |
| 1902 | rw_level = -1; |
| 1903 | have_alloc_sem = 0; |
| 1904 | } |
| 1905 | |
| 1906 | out: |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1907 | if (rw_level != -1) |
| 1908 | ocfs2_rw_unlock(inode, rw_level); |
| 1909 | |
| 1910 | out_sems: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1911 | if (have_alloc_sem) |
| 1912 | up_read(&inode->i_alloc_sem); |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1913 | |
| 1914 | if (written > 0 && sync) { |
| 1915 | ssize_t err; |
| 1916 | |
| 1917 | err = sync_page_range_nolock(inode, file->f_mapping, pos, count); |
| 1918 | if (err < 0) |
| 1919 | written = err; |
| 1920 | } |
| 1921 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1922 | mutex_unlock(&inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1923 | |
| 1924 | mlog_exit(ret); |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 1925 | return written ? written : ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1926 | } |
| 1927 | |
Mark Fasheh | 6af67d8 | 2007-03-06 17:24:46 -0800 | [diff] [blame] | 1928 | static int ocfs2_splice_write_actor(struct pipe_inode_info *pipe, |
| 1929 | struct pipe_buffer *buf, |
| 1930 | struct splice_desc *sd) |
| 1931 | { |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1932 | int ret, count; |
Mark Fasheh | 6af67d8 | 2007-03-06 17:24:46 -0800 | [diff] [blame] | 1933 | ssize_t copied = 0; |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1934 | struct file *file = sd->u.file; |
| 1935 | unsigned int offset; |
| 1936 | struct page *page = NULL; |
| 1937 | void *fsdata; |
| 1938 | char *src, *dst; |
Mark Fasheh | 6af67d8 | 2007-03-06 17:24:46 -0800 | [diff] [blame] | 1939 | |
Jens Axboe | cac36bb | 2007-06-14 13:10:48 +0200 | [diff] [blame] | 1940 | ret = buf->ops->confirm(pipe, buf); |
Mark Fasheh | 6af67d8 | 2007-03-06 17:24:46 -0800 | [diff] [blame] | 1941 | if (ret) |
| 1942 | goto out; |
| 1943 | |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1944 | offset = sd->pos & ~PAGE_CACHE_MASK; |
Mark Fasheh | 6af67d8 | 2007-03-06 17:24:46 -0800 | [diff] [blame] | 1945 | count = sd->len; |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1946 | if (count + offset > PAGE_CACHE_SIZE) |
| 1947 | count = PAGE_CACHE_SIZE - offset; |
Mark Fasheh | 6af67d8 | 2007-03-06 17:24:46 -0800 | [diff] [blame] | 1948 | |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1949 | ret = ocfs2_write_begin(file, file->f_mapping, sd->pos, count, 0, |
| 1950 | &page, &fsdata); |
| 1951 | if (ret) { |
| 1952 | mlog_errno(ret); |
| 1953 | goto out; |
| 1954 | } |
Mark Fasheh | 6af67d8 | 2007-03-06 17:24:46 -0800 | [diff] [blame] | 1955 | |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1956 | src = buf->ops->map(pipe, buf, 1); |
| 1957 | dst = kmap_atomic(page, KM_USER1); |
| 1958 | memcpy(dst + offset, src + buf->offset, count); |
| 1959 | kunmap_atomic(page, KM_USER1); |
| 1960 | buf->ops->unmap(pipe, buf, src); |
Mark Fasheh | 6af67d8 | 2007-03-06 17:24:46 -0800 | [diff] [blame] | 1961 | |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1962 | copied = ocfs2_write_end(file, file->f_mapping, sd->pos, count, count, |
| 1963 | page, fsdata); |
| 1964 | if (copied < 0) { |
| 1965 | mlog_errno(copied); |
| 1966 | ret = copied; |
| 1967 | goto out; |
| 1968 | } |
Mark Fasheh | 6af67d8 | 2007-03-06 17:24:46 -0800 | [diff] [blame] | 1969 | out: |
| 1970 | |
Mark Fasheh | 3a307ff | 2007-05-08 17:47:32 -0700 | [diff] [blame] | 1971 | return copied ? copied : ret; |
Mark Fasheh | 6af67d8 | 2007-03-06 17:24:46 -0800 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | static ssize_t __ocfs2_file_splice_write(struct pipe_inode_info *pipe, |
| 1975 | struct file *out, |
| 1976 | loff_t *ppos, |
| 1977 | size_t len, |
| 1978 | unsigned int flags) |
| 1979 | { |
| 1980 | int ret, err; |
| 1981 | struct address_space *mapping = out->f_mapping; |
| 1982 | struct inode *inode = mapping->host; |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1983 | struct splice_desc sd = { |
| 1984 | .total_len = len, |
| 1985 | .flags = flags, |
| 1986 | .pos = *ppos, |
Jens Axboe | 6a14b90 | 2007-06-14 13:08:55 +0200 | [diff] [blame] | 1987 | .u.file = out, |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1988 | }; |
Mark Fasheh | 6af67d8 | 2007-03-06 17:24:46 -0800 | [diff] [blame] | 1989 | |
Jens Axboe | c66ab6f | 2007-06-12 21:17:17 +0200 | [diff] [blame] | 1990 | ret = __splice_from_pipe(pipe, &sd, ocfs2_splice_write_actor); |
Mark Fasheh | 6af67d8 | 2007-03-06 17:24:46 -0800 | [diff] [blame] | 1991 | if (ret > 0) { |
| 1992 | *ppos += ret; |
| 1993 | |
| 1994 | if (unlikely((out->f_flags & O_SYNC) || IS_SYNC(inode))) { |
| 1995 | err = generic_osync_inode(inode, mapping, |
| 1996 | OSYNC_METADATA|OSYNC_DATA); |
| 1997 | if (err) |
| 1998 | ret = err; |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | return ret; |
| 2003 | } |
| 2004 | |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 2005 | static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe, |
| 2006 | struct file *out, |
| 2007 | loff_t *ppos, |
| 2008 | size_t len, |
| 2009 | unsigned int flags) |
| 2010 | { |
| 2011 | int ret; |
Josef Sipek | d28c917 | 2006-12-08 02:37:25 -0800 | [diff] [blame] | 2012 | struct inode *inode = out->f_path.dentry->d_inode; |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 2013 | |
| 2014 | mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe, |
| 2015 | (unsigned int)len, |
Josef Sipek | d28c917 | 2006-12-08 02:37:25 -0800 | [diff] [blame] | 2016 | out->f_path.dentry->d_name.len, |
| 2017 | out->f_path.dentry->d_name.name); |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 2018 | |
| 2019 | inode_double_lock(inode, pipe->inode); |
| 2020 | |
| 2021 | ret = ocfs2_rw_lock(inode, 1); |
| 2022 | if (ret < 0) { |
| 2023 | mlog_errno(ret); |
| 2024 | goto out; |
| 2025 | } |
| 2026 | |
Mark Fasheh | 9517bac | 2007-02-09 20:24:12 -0800 | [diff] [blame] | 2027 | ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0, |
| 2028 | NULL); |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 2029 | if (ret < 0) { |
| 2030 | mlog_errno(ret); |
| 2031 | goto out_unlock; |
| 2032 | } |
| 2033 | |
| 2034 | /* ok, we're done with i_size and alloc work */ |
Mark Fasheh | 6af67d8 | 2007-03-06 17:24:46 -0800 | [diff] [blame] | 2035 | ret = __ocfs2_file_splice_write(pipe, out, ppos, len, flags); |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 2036 | |
| 2037 | out_unlock: |
| 2038 | ocfs2_rw_unlock(inode, 1); |
| 2039 | out: |
| 2040 | inode_double_unlock(inode, pipe->inode); |
| 2041 | |
| 2042 | mlog_exit(ret); |
| 2043 | return ret; |
| 2044 | } |
| 2045 | |
| 2046 | static ssize_t ocfs2_file_splice_read(struct file *in, |
| 2047 | loff_t *ppos, |
| 2048 | struct pipe_inode_info *pipe, |
| 2049 | size_t len, |
| 2050 | unsigned int flags) |
| 2051 | { |
| 2052 | int ret = 0; |
Josef Sipek | d28c917 | 2006-12-08 02:37:25 -0800 | [diff] [blame] | 2053 | struct inode *inode = in->f_path.dentry->d_inode; |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 2054 | |
| 2055 | mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe, |
| 2056 | (unsigned int)len, |
Josef Sipek | d28c917 | 2006-12-08 02:37:25 -0800 | [diff] [blame] | 2057 | in->f_path.dentry->d_name.len, |
| 2058 | in->f_path.dentry->d_name.name); |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 2059 | |
| 2060 | /* |
| 2061 | * See the comment in ocfs2_file_aio_read() |
| 2062 | */ |
| 2063 | ret = ocfs2_meta_lock(inode, NULL, 0); |
| 2064 | if (ret < 0) { |
| 2065 | mlog_errno(ret); |
| 2066 | goto bail; |
| 2067 | } |
| 2068 | ocfs2_meta_unlock(inode, 0); |
| 2069 | |
| 2070 | ret = generic_file_splice_read(in, ppos, pipe, len, flags); |
| 2071 | |
| 2072 | bail: |
| 2073 | mlog_exit(ret); |
| 2074 | return ret; |
| 2075 | } |
| 2076 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2077 | static ssize_t ocfs2_file_aio_read(struct kiocb *iocb, |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 2078 | const struct iovec *iov, |
| 2079 | unsigned long nr_segs, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2080 | loff_t pos) |
| 2081 | { |
Tiger Yang | 25899de | 2006-11-15 15:49:02 +0800 | [diff] [blame] | 2082 | 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] | 2083 | struct file *filp = iocb->ki_filp; |
Josef Sipek | d28c917 | 2006-12-08 02:37:25 -0800 | [diff] [blame] | 2084 | struct inode *inode = filp->f_path.dentry->d_inode; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2085 | |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 2086 | mlog_entry("(0x%p, %u, '%.*s')\n", filp, |
| 2087 | (unsigned int)nr_segs, |
Josef Sipek | d28c917 | 2006-12-08 02:37:25 -0800 | [diff] [blame] | 2088 | filp->f_path.dentry->d_name.len, |
| 2089 | filp->f_path.dentry->d_name.name); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2090 | |
| 2091 | if (!inode) { |
| 2092 | ret = -EINVAL; |
| 2093 | mlog_errno(ret); |
| 2094 | goto bail; |
| 2095 | } |
| 2096 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2097 | /* |
| 2098 | * buffered reads protect themselves in ->readpage(). O_DIRECT reads |
| 2099 | * need locks to protect pending reads from racing with truncate. |
| 2100 | */ |
| 2101 | if (filp->f_flags & O_DIRECT) { |
| 2102 | down_read(&inode->i_alloc_sem); |
| 2103 | have_alloc_sem = 1; |
| 2104 | |
| 2105 | ret = ocfs2_rw_lock(inode, 0); |
| 2106 | if (ret < 0) { |
| 2107 | mlog_errno(ret); |
| 2108 | goto bail; |
| 2109 | } |
| 2110 | rw_level = 0; |
| 2111 | /* communicate with ocfs2_dio_end_io */ |
Mark Fasheh | 7cdfc3a | 2007-04-16 17:28:51 -0700 | [diff] [blame] | 2112 | ocfs2_iocb_set_rw_locked(iocb, rw_level); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2113 | } |
| 2114 | |
Mark Fasheh | c4374f8 | 2006-05-05 19:04:35 -0700 | [diff] [blame] | 2115 | /* |
| 2116 | * We're fine letting folks race truncates and extending |
| 2117 | * writes with read across the cluster, just like they can |
| 2118 | * locally. Hence no rw_lock during read. |
| 2119 | * |
| 2120 | * Take and drop the meta data lock to update inode fields |
| 2121 | * like i_size. This allows the checks down below |
| 2122 | * generic_file_aio_read() a chance of actually working. |
| 2123 | */ |
Tiger Yang | 25899de | 2006-11-15 15:49:02 +0800 | [diff] [blame] | 2124 | ret = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level); |
Mark Fasheh | c4374f8 | 2006-05-05 19:04:35 -0700 | [diff] [blame] | 2125 | if (ret < 0) { |
| 2126 | mlog_errno(ret); |
| 2127 | goto bail; |
| 2128 | } |
Tiger Yang | 25899de | 2006-11-15 15:49:02 +0800 | [diff] [blame] | 2129 | ocfs2_meta_unlock(inode, lock_level); |
Mark Fasheh | c4374f8 | 2006-05-05 19:04:35 -0700 | [diff] [blame] | 2130 | |
Badari Pulavarty | 027445c | 2006-09-30 23:28:46 -0700 | [diff] [blame] | 2131 | ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2132 | if (ret == -EINVAL) |
| 2133 | mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n"); |
| 2134 | |
| 2135 | /* buffered aio wouldn't have proper lock coverage today */ |
| 2136 | BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT)); |
| 2137 | |
| 2138 | /* see ocfs2_file_aio_write */ |
| 2139 | if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) { |
| 2140 | rw_level = -1; |
| 2141 | have_alloc_sem = 0; |
| 2142 | } |
| 2143 | |
| 2144 | bail: |
| 2145 | if (have_alloc_sem) |
| 2146 | up_read(&inode->i_alloc_sem); |
| 2147 | if (rw_level != -1) |
| 2148 | ocfs2_rw_unlock(inode, rw_level); |
| 2149 | mlog_exit(ret); |
| 2150 | |
| 2151 | return ret; |
| 2152 | } |
| 2153 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 2154 | const struct inode_operations ocfs2_file_iops = { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2155 | .setattr = ocfs2_setattr, |
| 2156 | .getattr = ocfs2_getattr, |
Tiger Yang | d38eb8d | 2006-11-27 09:59:21 +0800 | [diff] [blame] | 2157 | .permission = ocfs2_permission, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2158 | }; |
| 2159 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 2160 | const struct inode_operations ocfs2_special_file_iops = { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2161 | .setattr = ocfs2_setattr, |
| 2162 | .getattr = ocfs2_getattr, |
Tiger Yang | d38eb8d | 2006-11-27 09:59:21 +0800 | [diff] [blame] | 2163 | .permission = ocfs2_permission, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2164 | }; |
| 2165 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 2166 | const struct file_operations ocfs2_fops = { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2167 | .read = do_sync_read, |
| 2168 | .write = do_sync_write, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2169 | .mmap = ocfs2_mmap, |
| 2170 | .fsync = ocfs2_sync_file, |
| 2171 | .release = ocfs2_file_release, |
| 2172 | .open = ocfs2_file_open, |
| 2173 | .aio_read = ocfs2_file_aio_read, |
| 2174 | .aio_write = ocfs2_file_aio_write, |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 2175 | .ioctl = ocfs2_ioctl, |
Mark Fasheh | 586d232 | 2007-03-09 15:56:28 -0800 | [diff] [blame] | 2176 | #ifdef CONFIG_COMPAT |
| 2177 | .compat_ioctl = ocfs2_compat_ioctl, |
| 2178 | #endif |
Tiger Yang | 8659ac2 | 2006-10-17 18:29:52 -0700 | [diff] [blame] | 2179 | .splice_read = ocfs2_file_splice_read, |
| 2180 | .splice_write = ocfs2_file_splice_write, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2181 | }; |
| 2182 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 2183 | const struct file_operations ocfs2_dops = { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2184 | .read = generic_read_dir, |
| 2185 | .readdir = ocfs2_readdir, |
| 2186 | .fsync = ocfs2_sync_file, |
Herbert Poetzl | ca4d147 | 2006-07-03 17:27:12 -0700 | [diff] [blame] | 2187 | .ioctl = ocfs2_ioctl, |
Mark Fasheh | 586d232 | 2007-03-09 15:56:28 -0800 | [diff] [blame] | 2188 | #ifdef CONFIG_COMPAT |
| 2189 | .compat_ioctl = ocfs2_compat_ioctl, |
| 2190 | #endif |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2191 | }; |