blob: fef0186a91cd53257a0e60b38353b01f90436ae1 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA.
20 */
21
22#include <linux/fs.h>
23#include <linux/slab.h>
24#include <linux/highmem.h>
25#include <linux/pagemap.h>
26#include <asm/byteorder.h>
Mark Fasheh9517bac2007-02-09 20:24:12 -080027#include <linux/swap.h>
Mark Fasheh6af67d82007-03-06 17:24:46 -080028#include <linux/pipe_fs_i.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080029
30#define MLOG_MASK_PREFIX ML_FILE_IO
31#include <cluster/masklog.h>
32
33#include "ocfs2.h"
34
35#include "alloc.h"
36#include "aops.h"
37#include "dlmglue.h"
38#include "extent_map.h"
39#include "file.h"
40#include "inode.h"
41#include "journal.h"
Mark Fasheh9517bac2007-02-09 20:24:12 -080042#include "suballoc.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080043#include "super.h"
44#include "symlink.h"
45
46#include "buffer_head_io.h"
47
48static int ocfs2_symlink_get_block(struct inode *inode, sector_t iblock,
49 struct buffer_head *bh_result, int create)
50{
51 int err = -EIO;
52 int status;
53 struct ocfs2_dinode *fe = NULL;
54 struct buffer_head *bh = NULL;
55 struct buffer_head *buffer_cache_bh = NULL;
56 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
57 void *kaddr;
58
59 mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode,
60 (unsigned long long)iblock, bh_result, create);
61
62 BUG_ON(ocfs2_inode_is_fast_symlink(inode));
63
64 if ((iblock << inode->i_sb->s_blocksize_bits) > PATH_MAX + 1) {
65 mlog(ML_ERROR, "block offset > PATH_MAX: %llu",
66 (unsigned long long)iblock);
67 goto bail;
68 }
69
70 status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
71 OCFS2_I(inode)->ip_blkno,
72 &bh, OCFS2_BH_CACHED, inode);
73 if (status < 0) {
74 mlog_errno(status);
75 goto bail;
76 }
77 fe = (struct ocfs2_dinode *) bh->b_data;
78
79 if (!OCFS2_IS_VALID_DINODE(fe)) {
Mark Fashehb06970532006-03-03 10:24:33 -080080 mlog(ML_ERROR, "Invalid dinode #%llu: signature = %.*s\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -070081 (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
82 fe->i_signature);
Mark Fashehccd979b2005-12-15 14:31:24 -080083 goto bail;
84 }
85
86 if ((u64)iblock >= ocfs2_clusters_to_blocks(inode->i_sb,
87 le32_to_cpu(fe->i_clusters))) {
88 mlog(ML_ERROR, "block offset is outside the allocated size: "
89 "%llu\n", (unsigned long long)iblock);
90 goto bail;
91 }
92
93 /* We don't use the page cache to create symlink data, so if
94 * need be, copy it over from the buffer cache. */
95 if (!buffer_uptodate(bh_result) && ocfs2_inode_is_new(inode)) {
96 u64 blkno = le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) +
97 iblock;
98 buffer_cache_bh = sb_getblk(osb->sb, blkno);
99 if (!buffer_cache_bh) {
100 mlog(ML_ERROR, "couldn't getblock for symlink!\n");
101 goto bail;
102 }
103
104 /* we haven't locked out transactions, so a commit
105 * could've happened. Since we've got a reference on
106 * the bh, even if it commits while we're doing the
107 * copy, the data is still good. */
108 if (buffer_jbd(buffer_cache_bh)
109 && ocfs2_inode_is_new(inode)) {
110 kaddr = kmap_atomic(bh_result->b_page, KM_USER0);
111 if (!kaddr) {
112 mlog(ML_ERROR, "couldn't kmap!\n");
113 goto bail;
114 }
115 memcpy(kaddr + (bh_result->b_size * iblock),
116 buffer_cache_bh->b_data,
117 bh_result->b_size);
118 kunmap_atomic(kaddr, KM_USER0);
119 set_buffer_uptodate(bh_result);
120 }
121 brelse(buffer_cache_bh);
122 }
123
124 map_bh(bh_result, inode->i_sb,
125 le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) + iblock);
126
127 err = 0;
128
129bail:
130 if (bh)
131 brelse(bh);
132
133 mlog_exit(err);
134 return err;
135}
136
137static int ocfs2_get_block(struct inode *inode, sector_t iblock,
138 struct buffer_head *bh_result, int create)
139{
140 int err = 0;
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800141 unsigned int ext_flags;
Mark Fashehccd979b2005-12-15 14:31:24 -0800142 u64 p_blkno, past_eof;
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800143 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800144
145 mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode,
146 (unsigned long long)iblock, bh_result, create);
147
148 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE)
149 mlog(ML_NOTICE, "get_block on system inode 0x%p (%lu)\n",
150 inode, inode->i_ino);
151
152 if (S_ISLNK(inode->i_mode)) {
153 /* this always does I/O for some reason. */
154 err = ocfs2_symlink_get_block(inode, iblock, bh_result, create);
155 goto bail;
156 }
157
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800158 err = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, NULL,
159 &ext_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800160 if (err) {
161 mlog(ML_ERROR, "Error %d from get_blocks(0x%p, %llu, 1, "
Mark Fashehb06970532006-03-03 10:24:33 -0800162 "%llu, NULL)\n", err, inode, (unsigned long long)iblock,
163 (unsigned long long)p_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800164 goto bail;
165 }
166
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800167 /*
168 * ocfs2 never allocates in this function - the only time we
169 * need to use BH_New is when we're extending i_size on a file
170 * system which doesn't support holes, in which case BH_New
171 * allows block_prepare_write() to zero.
172 */
173 mlog_bug_on_msg(create && p_blkno == 0 && ocfs2_sparse_alloc(osb),
174 "ino %lu, iblock %llu\n", inode->i_ino,
175 (unsigned long long)iblock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800176
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800177 /* Treat the unwritten extent as a hole for zeroing purposes. */
178 if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800179 map_bh(bh_result, inode->i_sb, p_blkno);
180
181 if (!ocfs2_sparse_alloc(osb)) {
182 if (p_blkno == 0) {
183 err = -EIO;
184 mlog(ML_ERROR,
185 "iblock = %llu p_blkno = %llu blkno=(%llu)\n",
186 (unsigned long long)iblock,
187 (unsigned long long)p_blkno,
188 (unsigned long long)OCFS2_I(inode)->ip_blkno);
189 mlog(ML_ERROR, "Size %llu, clusters %u\n", (unsigned long long)i_size_read(inode), OCFS2_I(inode)->ip_clusters);
190 dump_stack();
191 }
192
193 past_eof = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
194 mlog(0, "Inode %lu, past_eof = %llu\n", inode->i_ino,
195 (unsigned long long)past_eof);
196
197 if (create && (iblock >= past_eof))
198 set_buffer_new(bh_result);
Mark Fashehccd979b2005-12-15 14:31:24 -0800199 }
200
Mark Fashehccd979b2005-12-15 14:31:24 -0800201bail:
202 if (err < 0)
203 err = -EIO;
204
205 mlog_exit(err);
206 return err;
207}
208
Mark Fasheh6798d352007-09-07 14:05:51 -0700209static int ocfs2_read_inline_data(struct inode *inode, struct page *page,
210 struct buffer_head *di_bh)
211{
212 void *kaddr;
213 unsigned int size;
214 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
215
216 if (!(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL)) {
217 ocfs2_error(inode->i_sb, "Inode %llu lost inline data flag",
218 (unsigned long long)OCFS2_I(inode)->ip_blkno);
219 return -EROFS;
220 }
221
222 size = i_size_read(inode);
223
224 if (size > PAGE_CACHE_SIZE ||
225 size > ocfs2_max_inline_data(inode->i_sb)) {
226 ocfs2_error(inode->i_sb,
227 "Inode %llu has with inline data has bad size: %u",
228 (unsigned long long)OCFS2_I(inode)->ip_blkno, size);
229 return -EROFS;
230 }
231
232 kaddr = kmap_atomic(page, KM_USER0);
233 if (size)
234 memcpy(kaddr, di->id2.i_data.id_data, size);
235 /* Clear the remaining part of the page */
236 memset(kaddr + size, 0, PAGE_CACHE_SIZE - size);
237 flush_dcache_page(page);
238 kunmap_atomic(kaddr, KM_USER0);
239
240 SetPageUptodate(page);
241
242 return 0;
243}
244
245static int ocfs2_readpage_inline(struct inode *inode, struct page *page)
246{
247 int ret;
248 struct buffer_head *di_bh = NULL;
249 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
250
251 BUG_ON(!PageLocked(page));
252 BUG_ON(!OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
253
254 ret = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &di_bh,
255 OCFS2_BH_CACHED, inode);
256 if (ret) {
257 mlog_errno(ret);
258 goto out;
259 }
260
261 ret = ocfs2_read_inline_data(inode, page, di_bh);
262out:
263 unlock_page(page);
264
265 brelse(di_bh);
266 return ret;
267}
268
Mark Fashehccd979b2005-12-15 14:31:24 -0800269static int ocfs2_readpage(struct file *file, struct page *page)
270{
271 struct inode *inode = page->mapping->host;
Mark Fasheh6798d352007-09-07 14:05:51 -0700272 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800273 loff_t start = (loff_t)page->index << PAGE_CACHE_SHIFT;
274 int ret, unlock = 1;
275
276 mlog_entry("(0x%p, %lu)\n", file, (page ? page->index : 0));
277
Mark Fasheh4bcec182006-10-09 16:02:40 -0700278 ret = ocfs2_meta_lock_with_page(inode, NULL, 0, page);
Mark Fashehccd979b2005-12-15 14:31:24 -0800279 if (ret != 0) {
280 if (ret == AOP_TRUNCATED_PAGE)
281 unlock = 0;
282 mlog_errno(ret);
283 goto out;
284 }
285
Mark Fasheh6798d352007-09-07 14:05:51 -0700286 if (down_read_trylock(&oi->ip_alloc_sem) == 0) {
Mark Fashehe9dfc0b2007-05-14 11:38:51 -0700287 ret = AOP_TRUNCATED_PAGE;
288 goto out_meta_unlock;
289 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800290
291 /*
292 * i_size might have just been updated as we grabed the meta lock. We
293 * might now be discovering a truncate that hit on another node.
294 * block_read_full_page->get_block freaks out if it is asked to read
295 * beyond the end of a file, so we check here. Callers
Nick Piggin54cb8822007-07-19 01:46:59 -0700296 * (generic_file_read, vm_ops->fault) are clever enough to check i_size
Mark Fashehccd979b2005-12-15 14:31:24 -0800297 * and notice that the page they just read isn't needed.
298 *
299 * XXX sys_readahead() seems to get that wrong?
300 */
301 if (start >= i_size_read(inode)) {
Nate Diller5c3c6bb2007-05-10 22:56:01 -0700302 zero_user_page(page, 0, PAGE_SIZE, KM_USER0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800303 SetPageUptodate(page);
304 ret = 0;
305 goto out_alloc;
306 }
307
308 ret = ocfs2_data_lock_with_page(inode, 0, page);
309 if (ret != 0) {
310 if (ret == AOP_TRUNCATED_PAGE)
311 unlock = 0;
312 mlog_errno(ret);
313 goto out_alloc;
314 }
315
Mark Fasheh6798d352007-09-07 14:05:51 -0700316 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
317 ret = ocfs2_readpage_inline(inode, page);
318 else
319 ret = block_read_full_page(page, ocfs2_get_block);
Mark Fashehccd979b2005-12-15 14:31:24 -0800320 unlock = 0;
321
322 ocfs2_data_unlock(inode, 0);
323out_alloc:
324 up_read(&OCFS2_I(inode)->ip_alloc_sem);
Mark Fashehe9dfc0b2007-05-14 11:38:51 -0700325out_meta_unlock:
Mark Fashehccd979b2005-12-15 14:31:24 -0800326 ocfs2_meta_unlock(inode, 0);
327out:
328 if (unlock)
329 unlock_page(page);
330 mlog_exit(ret);
331 return ret;
332}
333
334/* Note: Because we don't support holes, our allocation has
335 * already happened (allocation writes zeros to the file data)
336 * so we don't have to worry about ordered writes in
337 * ocfs2_writepage.
338 *
339 * ->writepage is called during the process of invalidating the page cache
340 * during blocked lock processing. It can't block on any cluster locks
341 * to during block mapping. It's relying on the fact that the block
342 * mapping can't have disappeared under the dirty pages that it is
343 * being asked to write back.
344 */
345static int ocfs2_writepage(struct page *page, struct writeback_control *wbc)
346{
347 int ret;
348
349 mlog_entry("(0x%p)\n", page);
350
351 ret = block_write_full_page(page, ocfs2_get_block, wbc);
352
353 mlog_exit(ret);
354
355 return ret;
356}
357
Mark Fasheh50691202007-02-09 20:52:53 -0800358/*
359 * This is called from ocfs2_write_zero_page() which has handled it's
360 * own cluster locking and has ensured allocation exists for those
361 * blocks to be written.
362 */
Mark Fasheh53013cb2006-05-05 19:04:03 -0700363int ocfs2_prepare_write_nolock(struct inode *inode, struct page *page,
364 unsigned from, unsigned to)
365{
366 int ret;
367
Mark Fasheh53013cb2006-05-05 19:04:03 -0700368 ret = block_prepare_write(page, from, to, ocfs2_get_block);
369
Mark Fasheh53013cb2006-05-05 19:04:03 -0700370 return ret;
371}
372
Mark Fashehccd979b2005-12-15 14:31:24 -0800373/* Taken from ext3. We don't necessarily need the full blown
374 * functionality yet, but IMHO it's better to cut and paste the whole
375 * thing so we can avoid introducing our own bugs (and easily pick up
376 * their fixes when they happen) --Mark */
Mark Fasheh60b11392007-02-16 11:46:50 -0800377int walk_page_buffers( handle_t *handle,
378 struct buffer_head *head,
379 unsigned from,
380 unsigned to,
381 int *partial,
382 int (*fn)( handle_t *handle,
383 struct buffer_head *bh))
Mark Fashehccd979b2005-12-15 14:31:24 -0800384{
385 struct buffer_head *bh;
386 unsigned block_start, block_end;
387 unsigned blocksize = head->b_size;
388 int err, ret = 0;
389 struct buffer_head *next;
390
391 for ( bh = head, block_start = 0;
392 ret == 0 && (bh != head || !block_start);
393 block_start = block_end, bh = next)
394 {
395 next = bh->b_this_page;
396 block_end = block_start + blocksize;
397 if (block_end <= from || block_start >= to) {
398 if (partial && !buffer_uptodate(bh))
399 *partial = 1;
400 continue;
401 }
402 err = (*fn)(handle, bh);
403 if (!ret)
404 ret = err;
405 }
406 return ret;
407}
408
Mark Fasheh1fabe142006-10-09 18:11:45 -0700409handle_t *ocfs2_start_walk_page_trans(struct inode *inode,
Mark Fashehccd979b2005-12-15 14:31:24 -0800410 struct page *page,
411 unsigned from,
412 unsigned to)
413{
414 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700415 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800416 int ret = 0;
417
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700418 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
Mark Fashehccd979b2005-12-15 14:31:24 -0800419 if (!handle) {
420 ret = -ENOMEM;
421 mlog_errno(ret);
422 goto out;
423 }
424
425 if (ocfs2_should_order_data(inode)) {
Mark Fasheh1fabe142006-10-09 18:11:45 -0700426 ret = walk_page_buffers(handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800427 page_buffers(page),
428 from, to, NULL,
429 ocfs2_journal_dirty_data);
430 if (ret < 0)
431 mlog_errno(ret);
432 }
433out:
434 if (ret) {
435 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700436 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800437 handle = ERR_PTR(ret);
438 }
439 return handle;
440}
441
Mark Fashehccd979b2005-12-15 14:31:24 -0800442static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block)
443{
444 sector_t status;
445 u64 p_blkno = 0;
446 int err = 0;
447 struct inode *inode = mapping->host;
448
449 mlog_entry("(block = %llu)\n", (unsigned long long)block);
450
451 /* We don't need to lock journal system files, since they aren't
452 * accessed concurrently from multiple nodes.
453 */
454 if (!INODE_JOURNAL(inode)) {
Mark Fasheh4bcec182006-10-09 16:02:40 -0700455 err = ocfs2_meta_lock(inode, NULL, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800456 if (err) {
457 if (err != -ENOENT)
458 mlog_errno(err);
459 goto bail;
460 }
461 down_read(&OCFS2_I(inode)->ip_alloc_sem);
462 }
463
Mark Fasheh6798d352007-09-07 14:05:51 -0700464 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL))
465 err = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL,
466 NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800467
468 if (!INODE_JOURNAL(inode)) {
469 up_read(&OCFS2_I(inode)->ip_alloc_sem);
470 ocfs2_meta_unlock(inode, 0);
471 }
472
473 if (err) {
474 mlog(ML_ERROR, "get_blocks() failed, block = %llu\n",
475 (unsigned long long)block);
476 mlog_errno(err);
477 goto bail;
478 }
479
Mark Fashehccd979b2005-12-15 14:31:24 -0800480bail:
481 status = err ? 0 : p_blkno;
482
483 mlog_exit((int)status);
484
485 return status;
486}
487
488/*
489 * TODO: Make this into a generic get_blocks function.
490 *
491 * From do_direct_io in direct-io.c:
492 * "So what we do is to permit the ->get_blocks function to populate
493 * bh.b_size with the size of IO which is permitted at this offset and
494 * this i_blkbits."
495 *
496 * This function is called directly from get_more_blocks in direct-io.c.
497 *
498 * called like this: dio->get_blocks(dio->inode, fs_startblk,
499 * fs_count, map_bh, dio->rw == WRITE);
500 */
501static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
Mark Fashehccd979b2005-12-15 14:31:24 -0800502 struct buffer_head *bh_result, int create)
503{
504 int ret;
Mark Fasheh4f902c32007-03-09 16:26:50 -0800505 u64 p_blkno, inode_blocks, contig_blocks;
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800506 unsigned int ext_flags;
Florin Malita184d7d22006-06-03 19:30:10 -0400507 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -0800508 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
Mark Fashehccd979b2005-12-15 14:31:24 -0800509
Mark Fashehccd979b2005-12-15 14:31:24 -0800510 /* This function won't even be called if the request isn't all
511 * nicely aligned and of the right size, so there's no need
512 * for us to check any of that. */
513
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800514 inode_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
Mark Fasheh564f8a32006-12-14 13:01:05 -0800515
516 /*
517 * Any write past EOF is not allowed because we'd be extending.
518 */
519 if (create && (iblock + max_blocks) > inode_blocks) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800520 ret = -EIO;
521 goto bail;
522 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800523
524 /* This figures out the size of the next contiguous block, and
525 * our logical offset */
Mark Fasheh363041a2007-01-17 12:31:35 -0800526 ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800527 &contig_blocks, &ext_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800528 if (ret) {
529 mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
530 (unsigned long long)iblock);
531 ret = -EIO;
532 goto bail;
533 }
534
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800535 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)) && !p_blkno) {
536 ocfs2_error(inode->i_sb,
537 "Inode %llu has a hole at block %llu\n",
538 (unsigned long long)OCFS2_I(inode)->ip_blkno,
539 (unsigned long long)iblock);
540 ret = -EROFS;
541 goto bail;
542 }
543
544 /*
545 * get_more_blocks() expects us to describe a hole by clearing
546 * the mapped bit on bh_result().
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800547 *
548 * Consider an unwritten extent as a hole.
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800549 */
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800550 if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
Mark Fasheh25baf2d2007-02-14 15:30:30 -0800551 map_bh(bh_result, inode->i_sb, p_blkno);
552 else {
553 /*
554 * ocfs2_prepare_inode_for_write() should have caught
555 * the case where we'd be filling a hole and triggered
556 * a buffered write instead.
557 */
558 if (create) {
559 ret = -EIO;
560 mlog_errno(ret);
561 goto bail;
562 }
563
564 clear_buffer_mapped(bh_result);
565 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800566
567 /* make sure we don't map more than max_blocks blocks here as
568 that's all the kernel will handle at this point. */
569 if (max_blocks < contig_blocks)
570 contig_blocks = max_blocks;
571 bh_result->b_size = contig_blocks << blocksize_bits;
572bail:
573 return ret;
574}
575
576/*
577 * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're
578 * particularly interested in the aio/dio case. Like the core uses
579 * i_alloc_sem, we use the rw_lock DLM lock to protect io on one node from
580 * truncation on another.
581 */
582static void ocfs2_dio_end_io(struct kiocb *iocb,
583 loff_t offset,
584 ssize_t bytes,
585 void *private)
586{
Josef Sipekd28c9172006-12-08 02:37:25 -0800587 struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -0700588 int level;
Mark Fashehccd979b2005-12-15 14:31:24 -0800589
590 /* this io's submitter should not have unlocked this before we could */
591 BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -0700592
Mark Fashehccd979b2005-12-15 14:31:24 -0800593 ocfs2_iocb_clear_rw_locked(iocb);
Mark Fasheh7cdfc3a2007-04-16 17:28:51 -0700594
595 level = ocfs2_iocb_rw_locked_level(iocb);
596 if (!level)
597 up_read(&inode->i_alloc_sem);
598 ocfs2_rw_unlock(inode, level);
Mark Fashehccd979b2005-12-15 14:31:24 -0800599}
600
Joel Becker03f981c2007-01-04 14:54:41 -0800601/*
602 * ocfs2_invalidatepage() and ocfs2_releasepage() are shamelessly stolen
603 * from ext3. PageChecked() bits have been removed as OCFS2 does not
604 * do journalled data.
605 */
606static void ocfs2_invalidatepage(struct page *page, unsigned long offset)
607{
608 journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
609
610 journal_invalidatepage(journal, page, offset);
611}
612
613static int ocfs2_releasepage(struct page *page, gfp_t wait)
614{
615 journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
616
617 if (!page_has_buffers(page))
618 return 0;
619 return journal_try_to_free_buffers(journal, page, wait);
620}
621
Mark Fashehccd979b2005-12-15 14:31:24 -0800622static ssize_t ocfs2_direct_IO(int rw,
623 struct kiocb *iocb,
624 const struct iovec *iov,
625 loff_t offset,
626 unsigned long nr_segs)
627{
628 struct file *file = iocb->ki_filp;
Josef Sipekd28c9172006-12-08 02:37:25 -0800629 struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host;
Mark Fashehccd979b2005-12-15 14:31:24 -0800630 int ret;
631
632 mlog_entry_void();
Mark Fasheh53013cb2006-05-05 19:04:03 -0700633
Mark Fasheh6798d352007-09-07 14:05:51 -0700634 /*
635 * Fallback to buffered I/O if we see an inode without
636 * extents.
637 */
638 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
639 return 0;
640
Mark Fasheh9517bac2007-02-09 20:24:12 -0800641 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
642 /*
643 * We get PR data locks even for O_DIRECT. This
644 * allows concurrent O_DIRECT I/O but doesn't let
645 * O_DIRECT with extending and buffered zeroing writes
646 * race. If they did race then the buffered zeroing
647 * could be written back after the O_DIRECT I/O. It's
648 * one thing to tell people not to mix buffered and
649 * O_DIRECT writes, but expecting them to understand
650 * that file extension is also an implicit buffered
651 * write is too much. By getting the PR we force
652 * writeback of the buffered zeroing before
653 * proceeding.
654 */
655 ret = ocfs2_data_lock(inode, 0);
656 if (ret < 0) {
657 mlog_errno(ret);
658 goto out;
659 }
660 ocfs2_data_unlock(inode, 0);
Mark Fasheh53013cb2006-05-05 19:04:03 -0700661 }
Mark Fasheh53013cb2006-05-05 19:04:03 -0700662
Mark Fashehccd979b2005-12-15 14:31:24 -0800663 ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
664 inode->i_sb->s_bdev, iov, offset,
665 nr_segs,
666 ocfs2_direct_IO_get_blocks,
667 ocfs2_dio_end_io);
Mark Fasheh53013cb2006-05-05 19:04:03 -0700668out:
Mark Fashehccd979b2005-12-15 14:31:24 -0800669 mlog_exit(ret);
670 return ret;
671}
672
Mark Fasheh9517bac2007-02-09 20:24:12 -0800673static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb,
674 u32 cpos,
675 unsigned int *start,
676 unsigned int *end)
677{
678 unsigned int cluster_start = 0, cluster_end = PAGE_CACHE_SIZE;
679
680 if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) {
681 unsigned int cpp;
682
683 cpp = 1 << (PAGE_CACHE_SHIFT - osb->s_clustersize_bits);
684
685 cluster_start = cpos % cpp;
686 cluster_start = cluster_start << osb->s_clustersize_bits;
687
688 cluster_end = cluster_start + osb->s_clustersize;
689 }
690
691 BUG_ON(cluster_start > PAGE_SIZE);
692 BUG_ON(cluster_end > PAGE_SIZE);
693
694 if (start)
695 *start = cluster_start;
696 if (end)
697 *end = cluster_end;
698}
699
700/*
701 * 'from' and 'to' are the region in the page to avoid zeroing.
702 *
703 * If pagesize > clustersize, this function will avoid zeroing outside
704 * of the cluster boundary.
705 *
706 * from == to == 0 is code for "zero the entire cluster region"
707 */
708static void ocfs2_clear_page_regions(struct page *page,
709 struct ocfs2_super *osb, u32 cpos,
710 unsigned from, unsigned to)
711{
712 void *kaddr;
713 unsigned int cluster_start, cluster_end;
714
715 ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end);
716
717 kaddr = kmap_atomic(page, KM_USER0);
718
719 if (from || to) {
720 if (from > cluster_start)
721 memset(kaddr + cluster_start, 0, from - cluster_start);
722 if (to < cluster_end)
723 memset(kaddr + to, 0, cluster_end - to);
724 } else {
725 memset(kaddr + cluster_start, 0, cluster_end - cluster_start);
726 }
727
728 kunmap_atomic(kaddr, KM_USER0);
729}
730
731/*
732 * Some of this taken from block_prepare_write(). We already have our
733 * mapping by now though, and the entire write will be allocating or
734 * it won't, so not much need to use BH_New.
735 *
736 * This will also skip zeroing, which is handled externally.
737 */
Mark Fasheh60b11392007-02-16 11:46:50 -0800738int ocfs2_map_page_blocks(struct page *page, u64 *p_blkno,
739 struct inode *inode, unsigned int from,
740 unsigned int to, int new)
Mark Fasheh9517bac2007-02-09 20:24:12 -0800741{
742 int ret = 0;
743 struct buffer_head *head, *bh, *wait[2], **wait_bh = wait;
744 unsigned int block_end, block_start;
745 unsigned int bsize = 1 << inode->i_blkbits;
746
747 if (!page_has_buffers(page))
748 create_empty_buffers(page, bsize, 0);
749
750 head = page_buffers(page);
751 for (bh = head, block_start = 0; bh != head || !block_start;
752 bh = bh->b_this_page, block_start += bsize) {
753 block_end = block_start + bsize;
754
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700755 clear_buffer_new(bh);
756
Mark Fasheh9517bac2007-02-09 20:24:12 -0800757 /*
758 * Ignore blocks outside of our i/o range -
759 * they may belong to unallocated clusters.
760 */
Mark Fasheh60b11392007-02-16 11:46:50 -0800761 if (block_start >= to || block_end <= from) {
Mark Fasheh9517bac2007-02-09 20:24:12 -0800762 if (PageUptodate(page))
763 set_buffer_uptodate(bh);
764 continue;
765 }
766
767 /*
768 * For an allocating write with cluster size >= page
769 * size, we always write the entire page.
770 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700771 if (new)
772 set_buffer_new(bh);
Mark Fasheh9517bac2007-02-09 20:24:12 -0800773
774 if (!buffer_mapped(bh)) {
775 map_bh(bh, inode->i_sb, *p_blkno);
776 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
777 }
778
779 if (PageUptodate(page)) {
780 if (!buffer_uptodate(bh))
781 set_buffer_uptodate(bh);
782 } else if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
Mark Fashehbce99762007-06-18 11:12:36 -0700783 !buffer_new(bh) &&
784 (block_start < from || block_end > to)) {
Mark Fasheh9517bac2007-02-09 20:24:12 -0800785 ll_rw_block(READ, 1, &bh);
786 *wait_bh++=bh;
787 }
788
789 *p_blkno = *p_blkno + 1;
790 }
791
792 /*
793 * If we issued read requests - let them complete.
794 */
795 while(wait_bh > wait) {
796 wait_on_buffer(*--wait_bh);
797 if (!buffer_uptodate(*wait_bh))
798 ret = -EIO;
799 }
800
801 if (ret == 0 || !new)
802 return ret;
803
804 /*
805 * If we get -EIO above, zero out any newly allocated blocks
806 * to avoid exposing stale data.
807 */
808 bh = head;
809 block_start = 0;
810 do {
Mark Fasheh9517bac2007-02-09 20:24:12 -0800811 block_end = block_start + bsize;
812 if (block_end <= from)
813 goto next_bh;
814 if (block_start >= to)
815 break;
816
Eric Sandeen54c57dc2007-06-20 17:15:10 -0700817 zero_user_page(page, block_start, bh->b_size, KM_USER0);
Mark Fasheh9517bac2007-02-09 20:24:12 -0800818 set_buffer_uptodate(bh);
819 mark_buffer_dirty(bh);
820
821next_bh:
822 block_start = block_end;
823 bh = bh->b_this_page;
824 } while (bh != head);
825
826 return ret;
827}
828
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700829#if (PAGE_CACHE_SIZE >= OCFS2_MAX_CLUSTERSIZE)
830#define OCFS2_MAX_CTXT_PAGES 1
831#else
832#define OCFS2_MAX_CTXT_PAGES (OCFS2_MAX_CLUSTERSIZE / PAGE_CACHE_SIZE)
833#endif
Mark Fasheh6af67d82007-03-06 17:24:46 -0800834
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700835#define OCFS2_MAX_CLUSTERS_PER_PAGE (PAGE_CACHE_SIZE / OCFS2_MIN_CLUSTERSIZE)
Mark Fasheh6af67d82007-03-06 17:24:46 -0800836
837/*
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700838 * Describe the state of a single cluster to be written to.
Mark Fasheh9517bac2007-02-09 20:24:12 -0800839 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700840struct ocfs2_write_cluster_desc {
841 u32 c_cpos;
842 u32 c_phys;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800843 /*
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700844 * Give this a unique field because c_phys eventually gets
845 * filled.
Mark Fasheh9517bac2007-02-09 20:24:12 -0800846 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700847 unsigned c_new;
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700848 unsigned c_unwritten;
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700849};
Mark Fasheh9517bac2007-02-09 20:24:12 -0800850
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700851static inline int ocfs2_should_zero_cluster(struct ocfs2_write_cluster_desc *d)
852{
853 return d->c_new || d->c_unwritten;
854}
855
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700856struct ocfs2_write_ctxt {
857 /* Logical cluster position / len of write */
858 u32 w_cpos;
859 u32 w_clen;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800860
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700861 struct ocfs2_write_cluster_desc w_desc[OCFS2_MAX_CLUSTERS_PER_PAGE];
Mark Fasheh9517bac2007-02-09 20:24:12 -0800862
863 /*
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700864 * This is true if page_size > cluster_size.
Mark Fasheh9517bac2007-02-09 20:24:12 -0800865 *
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700866 * It triggers a set of special cases during write which might
867 * have to deal with allocating writes to partial pages.
Mark Fasheh9517bac2007-02-09 20:24:12 -0800868 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700869 unsigned int w_large_pages;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800870
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700871 /*
872 * Pages involved in this write.
873 *
874 * w_target_page is the page being written to by the user.
875 *
876 * w_pages is an array of pages which always contains
877 * w_target_page, and in the case of an allocating write with
878 * page_size < cluster size, it will contain zero'd and mapped
879 * pages adjacent to w_target_page which need to be written
880 * out in so that future reads from that region will get
881 * zero's.
882 */
883 struct page *w_pages[OCFS2_MAX_CTXT_PAGES];
884 unsigned int w_num_pages;
885 struct page *w_target_page;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800886
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700887 /*
888 * ocfs2_write_end() uses this to know what the real range to
889 * write in the target should be.
890 */
891 unsigned int w_target_from;
892 unsigned int w_target_to;
893
894 /*
895 * We could use journal_current_handle() but this is cleaner,
896 * IMHO -Mark
897 */
898 handle_t *w_handle;
899
900 struct buffer_head *w_di_bh;
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700901
902 struct ocfs2_cached_dealloc_ctxt w_dealloc;
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700903};
904
Mark Fasheh1d410a62007-09-07 14:20:45 -0700905void ocfs2_unlock_and_free_pages(struct page **pages, int num_pages)
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700906{
907 int i;
908
Mark Fasheh1d410a62007-09-07 14:20:45 -0700909 for(i = 0; i < num_pages; i++) {
910 if (pages[i]) {
911 unlock_page(pages[i]);
912 mark_page_accessed(pages[i]);
913 page_cache_release(pages[i]);
914 }
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700915 }
Mark Fasheh1d410a62007-09-07 14:20:45 -0700916}
917
918static void ocfs2_free_write_ctxt(struct ocfs2_write_ctxt *wc)
919{
920 ocfs2_unlock_and_free_pages(wc->w_pages, wc->w_num_pages);
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700921
922 brelse(wc->w_di_bh);
923 kfree(wc);
924}
925
926static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp,
927 struct ocfs2_super *osb, loff_t pos,
Mark Fasheh607d44a2007-05-09 15:14:45 -0700928 unsigned len, struct buffer_head *di_bh)
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700929{
tao.ma@oracle.com30b85482007-09-06 08:02:25 +0800930 u32 cend;
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700931 struct ocfs2_write_ctxt *wc;
932
933 wc = kzalloc(sizeof(struct ocfs2_write_ctxt), GFP_NOFS);
934 if (!wc)
935 return -ENOMEM;
936
937 wc->w_cpos = pos >> osb->s_clustersize_bits;
tao.ma@oracle.com30b85482007-09-06 08:02:25 +0800938 cend = (pos + len - 1) >> osb->s_clustersize_bits;
939 wc->w_clen = cend - wc->w_cpos + 1;
Mark Fasheh607d44a2007-05-09 15:14:45 -0700940 get_bh(di_bh);
941 wc->w_di_bh = di_bh;
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700942
943 if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits))
944 wc->w_large_pages = 1;
945 else
946 wc->w_large_pages = 0;
947
Mark Fashehb27b7cb2007-06-18 11:22:56 -0700948 ocfs2_init_dealloc_ctxt(&wc->w_dealloc);
949
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700950 *wcp = wc;
951
952 return 0;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800953}
954
955/*
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700956 * If a page has any new buffers, zero them out here, and mark them uptodate
957 * and dirty so they'll be written out (in order to prevent uninitialised
958 * block data from leaking). And clear the new bit.
Mark Fasheh9517bac2007-02-09 20:24:12 -0800959 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700960static void ocfs2_zero_new_buffers(struct page *page, unsigned from, unsigned to)
Mark Fasheh9517bac2007-02-09 20:24:12 -0800961{
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700962 unsigned int block_start, block_end;
963 struct buffer_head *head, *bh;
Mark Fasheh9517bac2007-02-09 20:24:12 -0800964
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700965 BUG_ON(!PageLocked(page));
966 if (!page_has_buffers(page))
967 return;
968
969 bh = head = page_buffers(page);
970 block_start = 0;
971 do {
972 block_end = block_start + bh->b_size;
973
974 if (buffer_new(bh)) {
975 if (block_end > from && block_start < to) {
976 if (!PageUptodate(page)) {
977 unsigned start, end;
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700978
979 start = max(from, block_start);
980 end = min(to, block_end);
981
Eric Sandeen54c57dc2007-06-20 17:15:10 -0700982 zero_user_page(page, start, end - start, KM_USER0);
Mark Fasheh3a307ff2007-05-08 17:47:32 -0700983 set_buffer_uptodate(bh);
984 }
985
986 clear_buffer_new(bh);
987 mark_buffer_dirty(bh);
988 }
989 }
990
991 block_start = block_end;
992 bh = bh->b_this_page;
993 } while (bh != head);
994}
995
996/*
997 * Only called when we have a failure during allocating write to write
998 * zero's to the newly allocated region.
999 */
1000static void ocfs2_write_failure(struct inode *inode,
1001 struct ocfs2_write_ctxt *wc,
1002 loff_t user_pos, unsigned user_len)
1003{
1004 int i;
Mark Fasheh5c26a7b2007-09-18 17:49:29 -07001005 unsigned from = user_pos & (PAGE_CACHE_SIZE - 1),
1006 to = user_pos + user_len;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001007 struct page *tmppage;
1008
Mark Fasheh5c26a7b2007-09-18 17:49:29 -07001009 ocfs2_zero_new_buffers(wc->w_target_page, from, to);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001010
1011 for(i = 0; i < wc->w_num_pages; i++) {
1012 tmppage = wc->w_pages[i];
1013
1014 if (ocfs2_should_order_data(inode))
1015 walk_page_buffers(wc->w_handle, page_buffers(tmppage),
1016 from, to, NULL,
1017 ocfs2_journal_dirty_data);
1018
1019 block_commit_write(tmppage, from, to);
1020 }
1021}
1022
1023static int ocfs2_prepare_page_for_write(struct inode *inode, u64 *p_blkno,
1024 struct ocfs2_write_ctxt *wc,
1025 struct page *page, u32 cpos,
1026 loff_t user_pos, unsigned user_len,
1027 int new)
1028{
1029 int ret;
1030 unsigned int map_from = 0, map_to = 0;
1031 unsigned int cluster_start, cluster_end;
1032 unsigned int user_data_from = 0, user_data_to = 0;
1033
1034 ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), cpos,
Mark Fasheh9517bac2007-02-09 20:24:12 -08001035 &cluster_start, &cluster_end);
1036
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001037 if (page == wc->w_target_page) {
1038 map_from = user_pos & (PAGE_CACHE_SIZE - 1);
1039 map_to = map_from + user_len;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001040
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001041 if (new)
1042 ret = ocfs2_map_page_blocks(page, p_blkno, inode,
1043 cluster_start, cluster_end,
1044 new);
1045 else
1046 ret = ocfs2_map_page_blocks(page, p_blkno, inode,
1047 map_from, map_to, new);
1048 if (ret) {
Mark Fasheh9517bac2007-02-09 20:24:12 -08001049 mlog_errno(ret);
1050 goto out;
1051 }
1052
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001053 user_data_from = map_from;
1054 user_data_to = map_to;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001055 if (new) {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001056 map_from = cluster_start;
1057 map_to = cluster_end;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001058 }
1059 } else {
1060 /*
1061 * If we haven't allocated the new page yet, we
1062 * shouldn't be writing it out without copying user
1063 * data. This is likely a math error from the caller.
1064 */
1065 BUG_ON(!new);
1066
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001067 map_from = cluster_start;
1068 map_to = cluster_end;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001069
1070 ret = ocfs2_map_page_blocks(page, p_blkno, inode,
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001071 cluster_start, cluster_end, new);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001072 if (ret) {
1073 mlog_errno(ret);
1074 goto out;
1075 }
1076 }
1077
1078 /*
1079 * Parts of newly allocated pages need to be zero'd.
1080 *
1081 * Above, we have also rewritten 'to' and 'from' - as far as
1082 * the rest of the function is concerned, the entire cluster
1083 * range inside of a page needs to be written.
1084 *
1085 * We can skip this if the page is up to date - it's already
1086 * been zero'd from being read in as a hole.
1087 */
1088 if (new && !PageUptodate(page))
1089 ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb),
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001090 cpos, user_data_from, user_data_to);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001091
1092 flush_dcache_page(page);
1093
Mark Fasheh9517bac2007-02-09 20:24:12 -08001094out:
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001095 return ret;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001096}
1097
1098/*
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001099 * This function will only grab one clusters worth of pages.
Mark Fasheh9517bac2007-02-09 20:24:12 -08001100 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001101static int ocfs2_grab_pages_for_write(struct address_space *mapping,
1102 struct ocfs2_write_ctxt *wc,
Mark Fasheh7307de82007-05-09 15:16:19 -07001103 u32 cpos, loff_t user_pos, int new,
1104 struct page *mmap_page)
Mark Fasheh9517bac2007-02-09 20:24:12 -08001105{
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001106 int ret = 0, i;
1107 unsigned long start, target_index, index;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001108 struct inode *inode = mapping->host;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001109
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001110 target_index = user_pos >> PAGE_CACHE_SHIFT;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001111
1112 /*
1113 * Figure out how many pages we'll be manipulating here. For
Mark Fasheh60b11392007-02-16 11:46:50 -08001114 * non allocating write, we just change the one
1115 * page. Otherwise, we'll need a whole clusters worth.
Mark Fasheh9517bac2007-02-09 20:24:12 -08001116 */
Mark Fasheh9517bac2007-02-09 20:24:12 -08001117 if (new) {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001118 wc->w_num_pages = ocfs2_pages_per_cluster(inode->i_sb);
1119 start = ocfs2_align_clusters_to_page_index(inode->i_sb, cpos);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001120 } else {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001121 wc->w_num_pages = 1;
1122 start = target_index;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001123 }
1124
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001125 for(i = 0; i < wc->w_num_pages; i++) {
Mark Fasheh9517bac2007-02-09 20:24:12 -08001126 index = start + i;
1127
Mark Fasheh7307de82007-05-09 15:16:19 -07001128 if (index == target_index && mmap_page) {
1129 /*
1130 * ocfs2_pagemkwrite() is a little different
1131 * and wants us to directly use the page
1132 * passed in.
1133 */
1134 lock_page(mmap_page);
1135
1136 if (mmap_page->mapping != mapping) {
1137 unlock_page(mmap_page);
1138 /*
1139 * Sanity check - the locking in
1140 * ocfs2_pagemkwrite() should ensure
1141 * that this code doesn't trigger.
1142 */
1143 ret = -EINVAL;
1144 mlog_errno(ret);
1145 goto out;
1146 }
1147
1148 page_cache_get(mmap_page);
1149 wc->w_pages[i] = mmap_page;
1150 } else {
1151 wc->w_pages[i] = find_or_create_page(mapping, index,
1152 GFP_NOFS);
1153 if (!wc->w_pages[i]) {
1154 ret = -ENOMEM;
1155 mlog_errno(ret);
1156 goto out;
1157 }
Mark Fasheh9517bac2007-02-09 20:24:12 -08001158 }
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001159
1160 if (index == target_index)
1161 wc->w_target_page = wc->w_pages[i];
Mark Fasheh9517bac2007-02-09 20:24:12 -08001162 }
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001163out:
1164 return ret;
1165}
1166
1167/*
1168 * Prepare a single cluster for write one cluster into the file.
1169 */
1170static int ocfs2_write_cluster(struct address_space *mapping,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001171 u32 phys, unsigned int unwritten,
1172 struct ocfs2_alloc_context *data_ac,
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001173 struct ocfs2_alloc_context *meta_ac,
1174 struct ocfs2_write_ctxt *wc, u32 cpos,
1175 loff_t user_pos, unsigned user_len)
1176{
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001177 int ret, i, new, should_zero = 0;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001178 u64 v_blkno, p_blkno;
1179 struct inode *inode = mapping->host;
1180
1181 new = phys == 0 ? 1 : 0;
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001182 if (new || unwritten)
1183 should_zero = 1;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001184
1185 if (new) {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001186 u32 tmp_pos;
1187
Mark Fasheh9517bac2007-02-09 20:24:12 -08001188 /*
1189 * This is safe to call with the page locks - it won't take
1190 * any additional semaphores or cluster locks.
1191 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001192 tmp_pos = cpos;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001193 ret = ocfs2_do_extend_allocation(OCFS2_SB(inode->i_sb), inode,
Mark Fasheh2ae99a62007-03-09 16:43:28 -08001194 &tmp_pos, 1, 0, wc->w_di_bh,
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001195 wc->w_handle, data_ac,
1196 meta_ac, NULL);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001197 /*
1198 * This shouldn't happen because we must have already
1199 * calculated the correct meta data allocation required. The
1200 * internal tree allocation code should know how to increase
1201 * transaction credits itself.
1202 *
1203 * If need be, we could handle -EAGAIN for a
1204 * RESTART_TRANS here.
1205 */
1206 mlog_bug_on_msg(ret == -EAGAIN,
1207 "Inode %llu: EAGAIN return during allocation.\n",
1208 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1209 if (ret < 0) {
1210 mlog_errno(ret);
1211 goto out;
1212 }
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001213 } else if (unwritten) {
1214 ret = ocfs2_mark_extent_written(inode, wc->w_di_bh,
1215 wc->w_handle, cpos, 1, phys,
1216 meta_ac, &wc->w_dealloc);
1217 if (ret < 0) {
1218 mlog_errno(ret);
1219 goto out;
1220 }
Mark Fasheh9517bac2007-02-09 20:24:12 -08001221 }
1222
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001223 if (should_zero)
1224 v_blkno = ocfs2_clusters_to_blocks(inode->i_sb, cpos);
1225 else
1226 v_blkno = user_pos >> inode->i_sb->s_blocksize_bits;
1227
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001228 /*
1229 * The only reason this should fail is due to an inability to
1230 * find the extent added.
1231 */
Mark Fasheh49cb8d22007-03-09 16:21:46 -08001232 ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL,
1233 NULL);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001234 if (ret < 0) {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001235 ocfs2_error(inode->i_sb, "Corrupting extend for inode %llu, "
1236 "at logical block %llu",
1237 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1238 (unsigned long long)v_blkno);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001239 goto out;
1240 }
1241
1242 BUG_ON(p_blkno == 0);
1243
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001244 for(i = 0; i < wc->w_num_pages; i++) {
1245 int tmpret;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001246
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001247 tmpret = ocfs2_prepare_page_for_write(inode, &p_blkno, wc,
1248 wc->w_pages[i], cpos,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001249 user_pos, user_len,
1250 should_zero);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001251 if (tmpret) {
1252 mlog_errno(tmpret);
1253 if (ret == 0)
1254 tmpret = ret;
1255 }
Mark Fasheh9517bac2007-02-09 20:24:12 -08001256 }
1257
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001258 /*
1259 * We only have cleanup to do in case of allocating write.
1260 */
1261 if (ret && new)
1262 ocfs2_write_failure(inode, wc, user_pos, user_len);
1263
Mark Fasheh9517bac2007-02-09 20:24:12 -08001264out:
Mark Fasheh9517bac2007-02-09 20:24:12 -08001265
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001266 return ret;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001267}
1268
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001269static int ocfs2_write_cluster_by_desc(struct address_space *mapping,
1270 struct ocfs2_alloc_context *data_ac,
1271 struct ocfs2_alloc_context *meta_ac,
1272 struct ocfs2_write_ctxt *wc,
1273 loff_t pos, unsigned len)
1274{
1275 int ret, i;
Mark Fashehdb562462007-09-17 09:06:29 -07001276 loff_t cluster_off;
1277 unsigned int local_len = len;
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001278 struct ocfs2_write_cluster_desc *desc;
Mark Fashehdb562462007-09-17 09:06:29 -07001279 struct ocfs2_super *osb = OCFS2_SB(mapping->host->i_sb);
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001280
1281 for (i = 0; i < wc->w_clen; i++) {
1282 desc = &wc->w_desc[i];
1283
Mark Fashehdb562462007-09-17 09:06:29 -07001284 /*
1285 * We have to make sure that the total write passed in
1286 * doesn't extend past a single cluster.
1287 */
1288 local_len = len;
1289 cluster_off = pos & (osb->s_clustersize - 1);
1290 if ((cluster_off + local_len) > osb->s_clustersize)
1291 local_len = osb->s_clustersize - cluster_off;
1292
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001293 ret = ocfs2_write_cluster(mapping, desc->c_phys,
1294 desc->c_unwritten, data_ac, meta_ac,
Mark Fashehdb562462007-09-17 09:06:29 -07001295 wc, desc->c_cpos, pos, local_len);
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001296 if (ret) {
1297 mlog_errno(ret);
1298 goto out;
1299 }
Mark Fashehdb562462007-09-17 09:06:29 -07001300
1301 len -= local_len;
1302 pos += local_len;
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001303 }
1304
1305 ret = 0;
1306out:
1307 return ret;
1308}
1309
Mark Fasheh9517bac2007-02-09 20:24:12 -08001310/*
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001311 * ocfs2_write_end() wants to know which parts of the target page it
1312 * should complete the write on. It's easiest to compute them ahead of
1313 * time when a more complete view of the write is available.
Mark Fasheh9517bac2007-02-09 20:24:12 -08001314 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001315static void ocfs2_set_target_boundaries(struct ocfs2_super *osb,
1316 struct ocfs2_write_ctxt *wc,
1317 loff_t pos, unsigned len, int alloc)
Mark Fasheh9517bac2007-02-09 20:24:12 -08001318{
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001319 struct ocfs2_write_cluster_desc *desc;
1320
1321 wc->w_target_from = pos & (PAGE_CACHE_SIZE - 1);
1322 wc->w_target_to = wc->w_target_from + len;
1323
1324 if (alloc == 0)
1325 return;
1326
1327 /*
1328 * Allocating write - we may have different boundaries based
1329 * on page size and cluster size.
1330 *
1331 * NOTE: We can no longer compute one value from the other as
1332 * the actual write length and user provided length may be
1333 * different.
1334 */
1335
1336 if (wc->w_large_pages) {
1337 /*
1338 * We only care about the 1st and last cluster within
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001339 * our range and whether they should be zero'd or not. Either
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001340 * value may be extended out to the start/end of a
1341 * newly allocated cluster.
1342 */
1343 desc = &wc->w_desc[0];
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001344 if (ocfs2_should_zero_cluster(desc))
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001345 ocfs2_figure_cluster_boundaries(osb,
1346 desc->c_cpos,
1347 &wc->w_target_from,
1348 NULL);
1349
1350 desc = &wc->w_desc[wc->w_clen - 1];
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001351 if (ocfs2_should_zero_cluster(desc))
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001352 ocfs2_figure_cluster_boundaries(osb,
1353 desc->c_cpos,
1354 NULL,
1355 &wc->w_target_to);
1356 } else {
1357 wc->w_target_from = 0;
1358 wc->w_target_to = PAGE_CACHE_SIZE;
1359 }
1360}
1361
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001362/*
1363 * Populate each single-cluster write descriptor in the write context
1364 * with information about the i/o to be done.
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001365 *
1366 * Returns the number of clusters that will have to be allocated, as
1367 * well as a worst case estimate of the number of extent records that
1368 * would have to be created during a write to an unwritten region.
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001369 */
1370static int ocfs2_populate_write_desc(struct inode *inode,
1371 struct ocfs2_write_ctxt *wc,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001372 unsigned int *clusters_to_alloc,
1373 unsigned int *extents_to_split)
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001374{
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001375 int ret;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001376 struct ocfs2_write_cluster_desc *desc;
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001377 unsigned int num_clusters = 0;
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001378 unsigned int ext_flags = 0;
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001379 u32 phys = 0;
1380 int i;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001381
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001382 *clusters_to_alloc = 0;
1383 *extents_to_split = 0;
1384
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001385 for (i = 0; i < wc->w_clen; i++) {
1386 desc = &wc->w_desc[i];
1387 desc->c_cpos = wc->w_cpos + i;
1388
1389 if (num_clusters == 0) {
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001390 /*
1391 * Need to look up the next extent record.
1392 */
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001393 ret = ocfs2_get_clusters(inode, desc->c_cpos, &phys,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001394 &num_clusters, &ext_flags);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001395 if (ret) {
1396 mlog_errno(ret);
Mark Fasheh607d44a2007-05-09 15:14:45 -07001397 goto out;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001398 }
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001399
1400 /*
1401 * Assume worst case - that we're writing in
1402 * the middle of the extent.
1403 *
1404 * We can assume that the write proceeds from
1405 * left to right, in which case the extent
1406 * insert code is smart enough to coalesce the
1407 * next splits into the previous records created.
1408 */
1409 if (ext_flags & OCFS2_EXT_UNWRITTEN)
1410 *extents_to_split = *extents_to_split + 2;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001411 } else if (phys) {
1412 /*
1413 * Only increment phys if it doesn't describe
1414 * a hole.
1415 */
1416 phys++;
1417 }
1418
1419 desc->c_phys = phys;
1420 if (phys == 0) {
1421 desc->c_new = 1;
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001422 *clusters_to_alloc = *clusters_to_alloc + 1;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001423 }
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001424 if (ext_flags & OCFS2_EXT_UNWRITTEN)
1425 desc->c_unwritten = 1;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001426
1427 num_clusters--;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001428 }
1429
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001430 ret = 0;
1431out:
1432 return ret;
1433}
1434
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001435/*
1436 * This function only does anything for file systems which can't
1437 * handle sparse files.
1438 *
1439 * What we want to do here is fill in any hole between the current end
1440 * of allocation and the end of our write. That way the rest of the
1441 * write path can treat it as an non-allocating write, which has no
1442 * special case code for sparse/nonsparse files.
1443 */
1444static int ocfs2_expand_nonsparse_inode(struct inode *inode, loff_t pos,
1445 unsigned len,
1446 struct ocfs2_write_ctxt *wc)
1447{
1448 int ret;
1449 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1450 loff_t newsize = pos + len;
1451
1452 if (ocfs2_sparse_alloc(osb))
1453 return 0;
1454
1455 if (newsize <= i_size_read(inode))
1456 return 0;
1457
1458 ret = ocfs2_extend_no_holes(inode, newsize, newsize - len);
1459 if (ret)
1460 mlog_errno(ret);
1461
1462 return ret;
1463}
1464
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001465int ocfs2_write_begin_nolock(struct address_space *mapping,
1466 loff_t pos, unsigned len, unsigned flags,
1467 struct page **pagep, void **fsdata,
1468 struct buffer_head *di_bh, struct page *mmap_page)
1469{
1470 int ret, credits = OCFS2_INODE_UPDATE_CREDITS;
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001471 unsigned int clusters_to_alloc, extents_to_split;
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001472 struct ocfs2_write_ctxt *wc;
1473 struct inode *inode = mapping->host;
1474 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1475 struct ocfs2_dinode *di;
1476 struct ocfs2_alloc_context *data_ac = NULL;
1477 struct ocfs2_alloc_context *meta_ac = NULL;
1478 handle_t *handle;
1479
1480 ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, di_bh);
1481 if (ret) {
1482 mlog_errno(ret);
1483 return ret;
1484 }
1485
Mark Fasheh65ed39d2007-08-28 17:13:23 -07001486 ret = ocfs2_expand_nonsparse_inode(inode, pos, len, wc);
1487 if (ret) {
1488 mlog_errno(ret);
1489 goto out;
1490 }
1491
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001492 ret = ocfs2_populate_write_desc(inode, wc, &clusters_to_alloc,
1493 &extents_to_split);
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001494 if (ret) {
1495 mlog_errno(ret);
1496 goto out;
1497 }
1498
1499 di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
1500
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001501 /*
1502 * We set w_target_from, w_target_to here so that
1503 * ocfs2_write_end() knows which range in the target page to
1504 * write out. An allocation requires that we write the entire
1505 * cluster range.
1506 */
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001507 if (clusters_to_alloc || extents_to_split) {
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001508 /*
1509 * XXX: We are stretching the limits of
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001510 * ocfs2_lock_allocators(). It greatly over-estimates
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001511 * the work to be done.
1512 */
1513 ret = ocfs2_lock_allocators(inode, di, clusters_to_alloc,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001514 extents_to_split, &data_ac, &meta_ac);
Mark Fasheh9517bac2007-02-09 20:24:12 -08001515 if (ret) {
1516 mlog_errno(ret);
Mark Fasheh607d44a2007-05-09 15:14:45 -07001517 goto out;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001518 }
1519
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001520 credits = ocfs2_calc_extend_credits(inode->i_sb, di,
1521 clusters_to_alloc);
1522
Mark Fasheh9517bac2007-02-09 20:24:12 -08001523 }
1524
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001525 ocfs2_set_target_boundaries(osb, wc, pos, len,
1526 clusters_to_alloc + extents_to_split);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001527
Mark Fasheh9517bac2007-02-09 20:24:12 -08001528 handle = ocfs2_start_trans(osb, credits);
1529 if (IS_ERR(handle)) {
1530 ret = PTR_ERR(handle);
1531 mlog_errno(ret);
Mark Fasheh607d44a2007-05-09 15:14:45 -07001532 goto out;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001533 }
1534
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001535 wc->w_handle = handle;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001536
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001537 /*
1538 * We don't want this to fail in ocfs2_write_end(), so do it
1539 * here.
1540 */
1541 ret = ocfs2_journal_access(handle, inode, wc->w_di_bh,
Mark Fasheh9517bac2007-02-09 20:24:12 -08001542 OCFS2_JOURNAL_ACCESS_WRITE);
1543 if (ret) {
1544 mlog_errno(ret);
1545 goto out_commit;
1546 }
1547
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001548 /*
1549 * Fill our page array first. That way we've grabbed enough so
1550 * that we can zero and flush if we error after adding the
1551 * extent.
1552 */
1553 ret = ocfs2_grab_pages_for_write(mapping, wc, wc->w_cpos, pos,
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001554 clusters_to_alloc + extents_to_split,
1555 mmap_page);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001556 if (ret) {
Mark Fasheh9517bac2007-02-09 20:24:12 -08001557 mlog_errno(ret);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001558 goto out_commit;
1559 }
Mark Fasheh9517bac2007-02-09 20:24:12 -08001560
Mark Fasheh0d172ba2007-05-14 18:09:54 -07001561 ret = ocfs2_write_cluster_by_desc(mapping, data_ac, meta_ac, wc, pos,
1562 len);
1563 if (ret) {
1564 mlog_errno(ret);
1565 goto out_commit;
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001566 }
1567
1568 if (data_ac)
1569 ocfs2_free_alloc_context(data_ac);
1570 if (meta_ac)
1571 ocfs2_free_alloc_context(meta_ac);
1572
1573 *pagep = wc->w_target_page;
1574 *fsdata = wc;
1575 return 0;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001576out_commit:
1577 ocfs2_commit_trans(osb, handle);
1578
Mark Fasheh9517bac2007-02-09 20:24:12 -08001579out:
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001580 ocfs2_free_write_ctxt(wc);
1581
Mark Fasheh9517bac2007-02-09 20:24:12 -08001582 if (data_ac)
1583 ocfs2_free_alloc_context(data_ac);
1584 if (meta_ac)
1585 ocfs2_free_alloc_context(meta_ac);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001586 return ret;
1587}
Mark Fasheh9517bac2007-02-09 20:24:12 -08001588
Mark Fasheh607d44a2007-05-09 15:14:45 -07001589int ocfs2_write_begin(struct file *file, struct address_space *mapping,
1590 loff_t pos, unsigned len, unsigned flags,
1591 struct page **pagep, void **fsdata)
1592{
1593 int ret;
1594 struct buffer_head *di_bh = NULL;
1595 struct inode *inode = mapping->host;
1596
1597 ret = ocfs2_meta_lock(inode, &di_bh, 1);
1598 if (ret) {
1599 mlog_errno(ret);
1600 return ret;
1601 }
1602
1603 /*
1604 * Take alloc sem here to prevent concurrent lookups. That way
1605 * the mapping, zeroing and tree manipulation within
1606 * ocfs2_write() will be safe against ->readpage(). This
1607 * should also serve to lock out allocation from a shared
1608 * writeable region.
1609 */
1610 down_write(&OCFS2_I(inode)->ip_alloc_sem);
1611
1612 ret = ocfs2_data_lock(inode, 1);
1613 if (ret) {
1614 mlog_errno(ret);
1615 goto out_fail;
1616 }
1617
1618 ret = ocfs2_write_begin_nolock(mapping, pos, len, flags, pagep,
Mark Fasheh7307de82007-05-09 15:16:19 -07001619 fsdata, di_bh, NULL);
Mark Fasheh607d44a2007-05-09 15:14:45 -07001620 if (ret) {
1621 mlog_errno(ret);
1622 goto out_fail_data;
1623 }
1624
1625 brelse(di_bh);
1626
1627 return 0;
1628
1629out_fail_data:
1630 ocfs2_data_unlock(inode, 1);
1631out_fail:
1632 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1633
1634 brelse(di_bh);
1635 ocfs2_meta_unlock(inode, 1);
1636
1637 return ret;
1638}
1639
Mark Fasheh7307de82007-05-09 15:16:19 -07001640int ocfs2_write_end_nolock(struct address_space *mapping,
1641 loff_t pos, unsigned len, unsigned copied,
1642 struct page *page, void *fsdata)
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001643{
1644 int i;
1645 unsigned from, to, start = pos & (PAGE_CACHE_SIZE - 1);
1646 struct inode *inode = mapping->host;
1647 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1648 struct ocfs2_write_ctxt *wc = fsdata;
1649 struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
1650 handle_t *handle = wc->w_handle;
1651 struct page *tmppage;
1652
1653 if (unlikely(copied < len)) {
1654 if (!PageUptodate(wc->w_target_page))
1655 copied = 0;
1656
1657 ocfs2_zero_new_buffers(wc->w_target_page, start+copied,
1658 start+len);
1659 }
1660 flush_dcache_page(wc->w_target_page);
1661
1662 for(i = 0; i < wc->w_num_pages; i++) {
1663 tmppage = wc->w_pages[i];
1664
1665 if (tmppage == wc->w_target_page) {
1666 from = wc->w_target_from;
1667 to = wc->w_target_to;
1668
1669 BUG_ON(from > PAGE_CACHE_SIZE ||
1670 to > PAGE_CACHE_SIZE ||
1671 to < from);
1672 } else {
1673 /*
1674 * Pages adjacent to the target (if any) imply
1675 * a hole-filling write in which case we want
1676 * to flush their entire range.
1677 */
1678 from = 0;
1679 to = PAGE_CACHE_SIZE;
1680 }
1681
1682 if (ocfs2_should_order_data(inode))
1683 walk_page_buffers(wc->w_handle, page_buffers(tmppage),
1684 from, to, NULL,
1685 ocfs2_journal_dirty_data);
1686
1687 block_commit_write(tmppage, from, to);
1688 }
1689
1690 pos += copied;
1691 if (pos > inode->i_size) {
1692 i_size_write(inode, pos);
1693 mark_inode_dirty(inode);
1694 }
1695 inode->i_blocks = ocfs2_inode_sector_count(inode);
1696 di->i_size = cpu_to_le64((u64)i_size_read(inode));
1697 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1698 di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
1699 di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001700 ocfs2_journal_dirty(handle, wc->w_di_bh);
1701
1702 ocfs2_commit_trans(osb, handle);
Mark Fasheh59a5e412007-06-22 15:52:36 -07001703
Mark Fashehb27b7cb2007-06-18 11:22:56 -07001704 ocfs2_run_deallocs(osb, &wc->w_dealloc);
1705
Mark Fasheh3a307ff2007-05-08 17:47:32 -07001706 ocfs2_free_write_ctxt(wc);
1707
1708 return copied;
Mark Fasheh9517bac2007-02-09 20:24:12 -08001709}
1710
Mark Fasheh607d44a2007-05-09 15:14:45 -07001711int ocfs2_write_end(struct file *file, struct address_space *mapping,
1712 loff_t pos, unsigned len, unsigned copied,
1713 struct page *page, void *fsdata)
1714{
1715 int ret;
1716 struct inode *inode = mapping->host;
1717
1718 ret = ocfs2_write_end_nolock(mapping, pos, len, copied, page, fsdata);
1719
1720 ocfs2_data_unlock(inode, 1);
1721 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1722 ocfs2_meta_unlock(inode, 1);
1723
1724 return ret;
1725}
1726
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001727const struct address_space_operations ocfs2_aops = {
Mark Fashehccd979b2005-12-15 14:31:24 -08001728 .readpage = ocfs2_readpage,
1729 .writepage = ocfs2_writepage,
Mark Fashehccd979b2005-12-15 14:31:24 -08001730 .bmap = ocfs2_bmap,
1731 .sync_page = block_sync_page,
Joel Becker03f981c2007-01-04 14:54:41 -08001732 .direct_IO = ocfs2_direct_IO,
1733 .invalidatepage = ocfs2_invalidatepage,
1734 .releasepage = ocfs2_releasepage,
1735 .migratepage = buffer_migrate_page,
Mark Fashehccd979b2005-12-15 14:31:24 -08001736};