ocfs2: move nonsparse hole-filling into ocfs2_write_begin()
[deliverable/linux.git] / fs / ocfs2 / aops.c
CommitLineData
ccd979bd
MF
1/* -*- 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>
9517bac6 27#include <linux/swap.h>
6af67d82 28#include <linux/pipe_fs_i.h>
ccd979bd
MF
29
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"
9517bac6 42#include "suballoc.h"
ccd979bd
MF
43#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)) {
b0697053 80 mlog(ML_ERROR, "Invalid dinode #%llu: signature = %.*s\n",
1ca1a111
MF
81 (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
82 fe->i_signature);
ccd979bd
MF
83 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;
49cb8d2d 141 unsigned int ext_flags;
ccd979bd 142 u64 p_blkno, past_eof;
25baf2da 143 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
ccd979bd
MF
144
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
49cb8d2d
MF
158 err = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, NULL,
159 &ext_flags);
ccd979bd
MF
160 if (err) {
161 mlog(ML_ERROR, "Error %d from get_blocks(0x%p, %llu, 1, "
b0697053
MF
162 "%llu, NULL)\n", err, inode, (unsigned long long)iblock,
163 (unsigned long long)p_blkno);
ccd979bd
MF
164 goto bail;
165 }
166
25baf2da
MF
167 /*
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);
176
49cb8d2d
MF
177 /* Treat the unwritten extent as a hole for zeroing purposes. */
178 if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
25baf2da
MF
179 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 }
ccd979bd 192
25baf2da
MF
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);
ccd979bd 196
25baf2da
MF
197 if (create && (iblock >= past_eof))
198 set_buffer_new(bh_result);
199 }
ccd979bd
MF
200
201bail:
202 if (err < 0)
203 err = -EIO;
204
205 mlog_exit(err);
206 return err;
207}
208
209static int ocfs2_readpage(struct file *file, struct page *page)
210{
211 struct inode *inode = page->mapping->host;
212 loff_t start = (loff_t)page->index << PAGE_CACHE_SHIFT;
213 int ret, unlock = 1;
214
215 mlog_entry("(0x%p, %lu)\n", file, (page ? page->index : 0));
216
4bcec184 217 ret = ocfs2_meta_lock_with_page(inode, NULL, 0, page);
ccd979bd
MF
218 if (ret != 0) {
219 if (ret == AOP_TRUNCATED_PAGE)
220 unlock = 0;
221 mlog_errno(ret);
222 goto out;
223 }
224
e9dfc0b2
MF
225 if (down_read_trylock(&OCFS2_I(inode)->ip_alloc_sem) == 0) {
226 ret = AOP_TRUNCATED_PAGE;
227 goto out_meta_unlock;
228 }
ccd979bd
MF
229
230 /*
231 * i_size might have just been updated as we grabed the meta lock. We
232 * might now be discovering a truncate that hit on another node.
233 * block_read_full_page->get_block freaks out if it is asked to read
234 * beyond the end of a file, so we check here. Callers
54cb8821 235 * (generic_file_read, vm_ops->fault) are clever enough to check i_size
ccd979bd
MF
236 * and notice that the page they just read isn't needed.
237 *
238 * XXX sys_readahead() seems to get that wrong?
239 */
240 if (start >= i_size_read(inode)) {
5c3c6bb7 241 zero_user_page(page, 0, PAGE_SIZE, KM_USER0);
ccd979bd
MF
242 SetPageUptodate(page);
243 ret = 0;
244 goto out_alloc;
245 }
246
247 ret = ocfs2_data_lock_with_page(inode, 0, page);
248 if (ret != 0) {
249 if (ret == AOP_TRUNCATED_PAGE)
250 unlock = 0;
251 mlog_errno(ret);
252 goto out_alloc;
253 }
254
255 ret = block_read_full_page(page, ocfs2_get_block);
256 unlock = 0;
257
258 ocfs2_data_unlock(inode, 0);
259out_alloc:
260 up_read(&OCFS2_I(inode)->ip_alloc_sem);
e9dfc0b2 261out_meta_unlock:
ccd979bd
MF
262 ocfs2_meta_unlock(inode, 0);
263out:
264 if (unlock)
265 unlock_page(page);
266 mlog_exit(ret);
267 return ret;
268}
269
270/* Note: Because we don't support holes, our allocation has
271 * already happened (allocation writes zeros to the file data)
272 * so we don't have to worry about ordered writes in
273 * ocfs2_writepage.
274 *
275 * ->writepage is called during the process of invalidating the page cache
276 * during blocked lock processing. It can't block on any cluster locks
277 * to during block mapping. It's relying on the fact that the block
278 * mapping can't have disappeared under the dirty pages that it is
279 * being asked to write back.
280 */
281static int ocfs2_writepage(struct page *page, struct writeback_control *wbc)
282{
283 int ret;
284
285 mlog_entry("(0x%p)\n", page);
286
287 ret = block_write_full_page(page, ocfs2_get_block, wbc);
288
289 mlog_exit(ret);
290
291 return ret;
292}
293
5069120b
MF
294/*
295 * This is called from ocfs2_write_zero_page() which has handled it's
296 * own cluster locking and has ensured allocation exists for those
297 * blocks to be written.
298 */
53013cba
MF
299int ocfs2_prepare_write_nolock(struct inode *inode, struct page *page,
300 unsigned from, unsigned to)
301{
302 int ret;
303
53013cba
MF
304 ret = block_prepare_write(page, from, to, ocfs2_get_block);
305
53013cba
MF
306 return ret;
307}
308
ccd979bd
MF
309/* Taken from ext3. We don't necessarily need the full blown
310 * functionality yet, but IMHO it's better to cut and paste the whole
311 * thing so we can avoid introducing our own bugs (and easily pick up
312 * their fixes when they happen) --Mark */
60b11392
MF
313int walk_page_buffers( handle_t *handle,
314 struct buffer_head *head,
315 unsigned from,
316 unsigned to,
317 int *partial,
318 int (*fn)( handle_t *handle,
319 struct buffer_head *bh))
ccd979bd
MF
320{
321 struct buffer_head *bh;
322 unsigned block_start, block_end;
323 unsigned blocksize = head->b_size;
324 int err, ret = 0;
325 struct buffer_head *next;
326
327 for ( bh = head, block_start = 0;
328 ret == 0 && (bh != head || !block_start);
329 block_start = block_end, bh = next)
330 {
331 next = bh->b_this_page;
332 block_end = block_start + blocksize;
333 if (block_end <= from || block_start >= to) {
334 if (partial && !buffer_uptodate(bh))
335 *partial = 1;
336 continue;
337 }
338 err = (*fn)(handle, bh);
339 if (!ret)
340 ret = err;
341 }
342 return ret;
343}
344
1fabe148 345handle_t *ocfs2_start_walk_page_trans(struct inode *inode,
ccd979bd
MF
346 struct page *page,
347 unsigned from,
348 unsigned to)
349{
350 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1fabe148 351 handle_t *handle = NULL;
ccd979bd
MF
352 int ret = 0;
353
65eff9cc 354 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
ccd979bd
MF
355 if (!handle) {
356 ret = -ENOMEM;
357 mlog_errno(ret);
358 goto out;
359 }
360
361 if (ocfs2_should_order_data(inode)) {
1fabe148 362 ret = walk_page_buffers(handle,
ccd979bd
MF
363 page_buffers(page),
364 from, to, NULL,
365 ocfs2_journal_dirty_data);
366 if (ret < 0)
367 mlog_errno(ret);
368 }
369out:
370 if (ret) {
371 if (handle)
02dc1af4 372 ocfs2_commit_trans(osb, handle);
ccd979bd
MF
373 handle = ERR_PTR(ret);
374 }
375 return handle;
376}
377
ccd979bd
MF
378static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block)
379{
380 sector_t status;
381 u64 p_blkno = 0;
382 int err = 0;
383 struct inode *inode = mapping->host;
384
385 mlog_entry("(block = %llu)\n", (unsigned long long)block);
386
387 /* We don't need to lock journal system files, since they aren't
388 * accessed concurrently from multiple nodes.
389 */
390 if (!INODE_JOURNAL(inode)) {
4bcec184 391 err = ocfs2_meta_lock(inode, NULL, 0);
ccd979bd
MF
392 if (err) {
393 if (err != -ENOENT)
394 mlog_errno(err);
395 goto bail;
396 }
397 down_read(&OCFS2_I(inode)->ip_alloc_sem);
398 }
399
49cb8d2d 400 err = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL, NULL);
ccd979bd
MF
401
402 if (!INODE_JOURNAL(inode)) {
403 up_read(&OCFS2_I(inode)->ip_alloc_sem);
404 ocfs2_meta_unlock(inode, 0);
405 }
406
407 if (err) {
408 mlog(ML_ERROR, "get_blocks() failed, block = %llu\n",
409 (unsigned long long)block);
410 mlog_errno(err);
411 goto bail;
412 }
413
414
415bail:
416 status = err ? 0 : p_blkno;
417
418 mlog_exit((int)status);
419
420 return status;
421}
422
423/*
424 * TODO: Make this into a generic get_blocks function.
425 *
426 * From do_direct_io in direct-io.c:
427 * "So what we do is to permit the ->get_blocks function to populate
428 * bh.b_size with the size of IO which is permitted at this offset and
429 * this i_blkbits."
430 *
431 * This function is called directly from get_more_blocks in direct-io.c.
432 *
433 * called like this: dio->get_blocks(dio->inode, fs_startblk,
434 * fs_count, map_bh, dio->rw == WRITE);
435 */
436static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
ccd979bd
MF
437 struct buffer_head *bh_result, int create)
438{
439 int ret;
4f902c37 440 u64 p_blkno, inode_blocks, contig_blocks;
49cb8d2d 441 unsigned int ext_flags;
184d7d20 442 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1d8fa7a2 443 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
ccd979bd 444
ccd979bd
MF
445 /* This function won't even be called if the request isn't all
446 * nicely aligned and of the right size, so there's no need
447 * for us to check any of that. */
448
25baf2da 449 inode_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
564f8a32
MF
450
451 /*
452 * Any write past EOF is not allowed because we'd be extending.
453 */
454 if (create && (iblock + max_blocks) > inode_blocks) {
ccd979bd
MF
455 ret = -EIO;
456 goto bail;
457 }
ccd979bd
MF
458
459 /* This figures out the size of the next contiguous block, and
460 * our logical offset */
363041a5 461 ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
49cb8d2d 462 &contig_blocks, &ext_flags);
ccd979bd
MF
463 if (ret) {
464 mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
465 (unsigned long long)iblock);
466 ret = -EIO;
467 goto bail;
468 }
469
25baf2da
MF
470 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)) && !p_blkno) {
471 ocfs2_error(inode->i_sb,
472 "Inode %llu has a hole at block %llu\n",
473 (unsigned long long)OCFS2_I(inode)->ip_blkno,
474 (unsigned long long)iblock);
475 ret = -EROFS;
476 goto bail;
477 }
478
479 /*
480 * get_more_blocks() expects us to describe a hole by clearing
481 * the mapped bit on bh_result().
49cb8d2d
MF
482 *
483 * Consider an unwritten extent as a hole.
25baf2da 484 */
49cb8d2d 485 if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
25baf2da
MF
486 map_bh(bh_result, inode->i_sb, p_blkno);
487 else {
488 /*
489 * ocfs2_prepare_inode_for_write() should have caught
490 * the case where we'd be filling a hole and triggered
491 * a buffered write instead.
492 */
493 if (create) {
494 ret = -EIO;
495 mlog_errno(ret);
496 goto bail;
497 }
498
499 clear_buffer_mapped(bh_result);
500 }
ccd979bd
MF
501
502 /* make sure we don't map more than max_blocks blocks here as
503 that's all the kernel will handle at this point. */
504 if (max_blocks < contig_blocks)
505 contig_blocks = max_blocks;
506 bh_result->b_size = contig_blocks << blocksize_bits;
507bail:
508 return ret;
509}
510
511/*
512 * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're
513 * particularly interested in the aio/dio case. Like the core uses
514 * i_alloc_sem, we use the rw_lock DLM lock to protect io on one node from
515 * truncation on another.
516 */
517static void ocfs2_dio_end_io(struct kiocb *iocb,
518 loff_t offset,
519 ssize_t bytes,
520 void *private)
521{
d28c9174 522 struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
7cdfc3a1 523 int level;
ccd979bd
MF
524
525 /* this io's submitter should not have unlocked this before we could */
526 BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
7cdfc3a1 527
ccd979bd 528 ocfs2_iocb_clear_rw_locked(iocb);
7cdfc3a1
MF
529
530 level = ocfs2_iocb_rw_locked_level(iocb);
531 if (!level)
532 up_read(&inode->i_alloc_sem);
533 ocfs2_rw_unlock(inode, level);
ccd979bd
MF
534}
535
03f981cf
JB
536/*
537 * ocfs2_invalidatepage() and ocfs2_releasepage() are shamelessly stolen
538 * from ext3. PageChecked() bits have been removed as OCFS2 does not
539 * do journalled data.
540 */
541static void ocfs2_invalidatepage(struct page *page, unsigned long offset)
542{
543 journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
544
545 journal_invalidatepage(journal, page, offset);
546}
547
548static int ocfs2_releasepage(struct page *page, gfp_t wait)
549{
550 journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
551
552 if (!page_has_buffers(page))
553 return 0;
554 return journal_try_to_free_buffers(journal, page, wait);
555}
556
ccd979bd
MF
557static ssize_t ocfs2_direct_IO(int rw,
558 struct kiocb *iocb,
559 const struct iovec *iov,
560 loff_t offset,
561 unsigned long nr_segs)
562{
563 struct file *file = iocb->ki_filp;
d28c9174 564 struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host;
ccd979bd
MF
565 int ret;
566
567 mlog_entry_void();
53013cba 568
9517bac6
MF
569 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
570 /*
571 * We get PR data locks even for O_DIRECT. This
572 * allows concurrent O_DIRECT I/O but doesn't let
573 * O_DIRECT with extending and buffered zeroing writes
574 * race. If they did race then the buffered zeroing
575 * could be written back after the O_DIRECT I/O. It's
576 * one thing to tell people not to mix buffered and
577 * O_DIRECT writes, but expecting them to understand
578 * that file extension is also an implicit buffered
579 * write is too much. By getting the PR we force
580 * writeback of the buffered zeroing before
581 * proceeding.
582 */
583 ret = ocfs2_data_lock(inode, 0);
584 if (ret < 0) {
585 mlog_errno(ret);
586 goto out;
587 }
588 ocfs2_data_unlock(inode, 0);
53013cba 589 }
53013cba 590
ccd979bd
MF
591 ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
592 inode->i_sb->s_bdev, iov, offset,
593 nr_segs,
594 ocfs2_direct_IO_get_blocks,
595 ocfs2_dio_end_io);
53013cba 596out:
ccd979bd
MF
597 mlog_exit(ret);
598 return ret;
599}
600
9517bac6
MF
601static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb,
602 u32 cpos,
603 unsigned int *start,
604 unsigned int *end)
605{
606 unsigned int cluster_start = 0, cluster_end = PAGE_CACHE_SIZE;
607
608 if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) {
609 unsigned int cpp;
610
611 cpp = 1 << (PAGE_CACHE_SHIFT - osb->s_clustersize_bits);
612
613 cluster_start = cpos % cpp;
614 cluster_start = cluster_start << osb->s_clustersize_bits;
615
616 cluster_end = cluster_start + osb->s_clustersize;
617 }
618
619 BUG_ON(cluster_start > PAGE_SIZE);
620 BUG_ON(cluster_end > PAGE_SIZE);
621
622 if (start)
623 *start = cluster_start;
624 if (end)
625 *end = cluster_end;
626}
627
628/*
629 * 'from' and 'to' are the region in the page to avoid zeroing.
630 *
631 * If pagesize > clustersize, this function will avoid zeroing outside
632 * of the cluster boundary.
633 *
634 * from == to == 0 is code for "zero the entire cluster region"
635 */
636static void ocfs2_clear_page_regions(struct page *page,
637 struct ocfs2_super *osb, u32 cpos,
638 unsigned from, unsigned to)
639{
640 void *kaddr;
641 unsigned int cluster_start, cluster_end;
642
643 ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end);
644
645 kaddr = kmap_atomic(page, KM_USER0);
646
647 if (from || to) {
648 if (from > cluster_start)
649 memset(kaddr + cluster_start, 0, from - cluster_start);
650 if (to < cluster_end)
651 memset(kaddr + to, 0, cluster_end - to);
652 } else {
653 memset(kaddr + cluster_start, 0, cluster_end - cluster_start);
654 }
655
656 kunmap_atomic(kaddr, KM_USER0);
657}
658
659/*
660 * Some of this taken from block_prepare_write(). We already have our
661 * mapping by now though, and the entire write will be allocating or
662 * it won't, so not much need to use BH_New.
663 *
664 * This will also skip zeroing, which is handled externally.
665 */
60b11392
MF
666int ocfs2_map_page_blocks(struct page *page, u64 *p_blkno,
667 struct inode *inode, unsigned int from,
668 unsigned int to, int new)
9517bac6
MF
669{
670 int ret = 0;
671 struct buffer_head *head, *bh, *wait[2], **wait_bh = wait;
672 unsigned int block_end, block_start;
673 unsigned int bsize = 1 << inode->i_blkbits;
674
675 if (!page_has_buffers(page))
676 create_empty_buffers(page, bsize, 0);
677
678 head = page_buffers(page);
679 for (bh = head, block_start = 0; bh != head || !block_start;
680 bh = bh->b_this_page, block_start += bsize) {
681 block_end = block_start + bsize;
682
3a307ffc
MF
683 clear_buffer_new(bh);
684
9517bac6
MF
685 /*
686 * Ignore blocks outside of our i/o range -
687 * they may belong to unallocated clusters.
688 */
60b11392 689 if (block_start >= to || block_end <= from) {
9517bac6
MF
690 if (PageUptodate(page))
691 set_buffer_uptodate(bh);
692 continue;
693 }
694
695 /*
696 * For an allocating write with cluster size >= page
697 * size, we always write the entire page.
698 */
3a307ffc
MF
699 if (new)
700 set_buffer_new(bh);
9517bac6
MF
701
702 if (!buffer_mapped(bh)) {
703 map_bh(bh, inode->i_sb, *p_blkno);
704 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
705 }
706
707 if (PageUptodate(page)) {
708 if (!buffer_uptodate(bh))
709 set_buffer_uptodate(bh);
710 } else if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
bce99768
MF
711 !buffer_new(bh) &&
712 (block_start < from || block_end > to)) {
9517bac6
MF
713 ll_rw_block(READ, 1, &bh);
714 *wait_bh++=bh;
715 }
716
717 *p_blkno = *p_blkno + 1;
718 }
719
720 /*
721 * If we issued read requests - let them complete.
722 */
723 while(wait_bh > wait) {
724 wait_on_buffer(*--wait_bh);
725 if (!buffer_uptodate(*wait_bh))
726 ret = -EIO;
727 }
728
729 if (ret == 0 || !new)
730 return ret;
731
732 /*
733 * If we get -EIO above, zero out any newly allocated blocks
734 * to avoid exposing stale data.
735 */
736 bh = head;
737 block_start = 0;
738 do {
9517bac6
MF
739 block_end = block_start + bsize;
740 if (block_end <= from)
741 goto next_bh;
742 if (block_start >= to)
743 break;
744
54c57dc3 745 zero_user_page(page, block_start, bh->b_size, KM_USER0);
9517bac6
MF
746 set_buffer_uptodate(bh);
747 mark_buffer_dirty(bh);
748
749next_bh:
750 block_start = block_end;
751 bh = bh->b_this_page;
752 } while (bh != head);
753
754 return ret;
755}
756
3a307ffc
MF
757#if (PAGE_CACHE_SIZE >= OCFS2_MAX_CLUSTERSIZE)
758#define OCFS2_MAX_CTXT_PAGES 1
759#else
760#define OCFS2_MAX_CTXT_PAGES (OCFS2_MAX_CLUSTERSIZE / PAGE_CACHE_SIZE)
761#endif
762
763#define OCFS2_MAX_CLUSTERS_PER_PAGE (PAGE_CACHE_SIZE / OCFS2_MIN_CLUSTERSIZE)
764
6af67d82 765/*
3a307ffc 766 * Describe the state of a single cluster to be written to.
6af67d82 767 */
3a307ffc
MF
768struct ocfs2_write_cluster_desc {
769 u32 c_cpos;
770 u32 c_phys;
771 /*
772 * Give this a unique field because c_phys eventually gets
773 * filled.
774 */
775 unsigned c_new;
b27b7cbc 776 unsigned c_unwritten;
3a307ffc 777};
6af67d82 778
b27b7cbc
MF
779static inline int ocfs2_should_zero_cluster(struct ocfs2_write_cluster_desc *d)
780{
781 return d->c_new || d->c_unwritten;
782}
783
3a307ffc
MF
784struct ocfs2_write_ctxt {
785 /* Logical cluster position / len of write */
786 u32 w_cpos;
787 u32 w_clen;
6af67d82 788
3a307ffc 789 struct ocfs2_write_cluster_desc w_desc[OCFS2_MAX_CLUSTERS_PER_PAGE];
6af67d82 790
3a307ffc
MF
791 /*
792 * This is true if page_size > cluster_size.
793 *
794 * It triggers a set of special cases during write which might
795 * have to deal with allocating writes to partial pages.
796 */
797 unsigned int w_large_pages;
6af67d82 798
3a307ffc
MF
799 /*
800 * Pages involved in this write.
801 *
802 * w_target_page is the page being written to by the user.
803 *
804 * w_pages is an array of pages which always contains
805 * w_target_page, and in the case of an allocating write with
806 * page_size < cluster size, it will contain zero'd and mapped
807 * pages adjacent to w_target_page which need to be written
808 * out in so that future reads from that region will get
809 * zero's.
810 */
811 struct page *w_pages[OCFS2_MAX_CTXT_PAGES];
812 unsigned int w_num_pages;
813 struct page *w_target_page;
eeb47d12 814
3a307ffc
MF
815 /*
816 * ocfs2_write_end() uses this to know what the real range to
817 * write in the target should be.
818 */
819 unsigned int w_target_from;
820 unsigned int w_target_to;
821
822 /*
823 * We could use journal_current_handle() but this is cleaner,
824 * IMHO -Mark
825 */
826 handle_t *w_handle;
827
828 struct buffer_head *w_di_bh;
b27b7cbc
MF
829
830 struct ocfs2_cached_dealloc_ctxt w_dealloc;
3a307ffc
MF
831};
832
833static void ocfs2_free_write_ctxt(struct ocfs2_write_ctxt *wc)
834{
835 int i;
836
837 for(i = 0; i < wc->w_num_pages; i++) {
838 if (wc->w_pages[i] == NULL)
839 continue;
840
841 unlock_page(wc->w_pages[i]);
842 mark_page_accessed(wc->w_pages[i]);
843 page_cache_release(wc->w_pages[i]);
6af67d82
MF
844 }
845
3a307ffc
MF
846 brelse(wc->w_di_bh);
847 kfree(wc);
848}
849
850static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp,
851 struct ocfs2_super *osb, loff_t pos,
607d44aa 852 unsigned len, struct buffer_head *di_bh)
3a307ffc 853{
30b8548f 854 u32 cend;
3a307ffc
MF
855 struct ocfs2_write_ctxt *wc;
856
857 wc = kzalloc(sizeof(struct ocfs2_write_ctxt), GFP_NOFS);
858 if (!wc)
859 return -ENOMEM;
6af67d82 860
3a307ffc 861 wc->w_cpos = pos >> osb->s_clustersize_bits;
30b8548f 862 cend = (pos + len - 1) >> osb->s_clustersize_bits;
863 wc->w_clen = cend - wc->w_cpos + 1;
607d44aa
MF
864 get_bh(di_bh);
865 wc->w_di_bh = di_bh;
6af67d82 866
3a307ffc
MF
867 if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits))
868 wc->w_large_pages = 1;
869 else
870 wc->w_large_pages = 0;
871
b27b7cbc
MF
872 ocfs2_init_dealloc_ctxt(&wc->w_dealloc);
873
3a307ffc 874 *wcp = wc;
6af67d82 875
3a307ffc 876 return 0;
6af67d82
MF
877}
878
9517bac6 879/*
3a307ffc
MF
880 * If a page has any new buffers, zero them out here, and mark them uptodate
881 * and dirty so they'll be written out (in order to prevent uninitialised
882 * block data from leaking). And clear the new bit.
9517bac6 883 */
3a307ffc 884static void ocfs2_zero_new_buffers(struct page *page, unsigned from, unsigned to)
9517bac6 885{
3a307ffc
MF
886 unsigned int block_start, block_end;
887 struct buffer_head *head, *bh;
9517bac6 888
3a307ffc
MF
889 BUG_ON(!PageLocked(page));
890 if (!page_has_buffers(page))
891 return;
9517bac6 892
3a307ffc
MF
893 bh = head = page_buffers(page);
894 block_start = 0;
895 do {
896 block_end = block_start + bh->b_size;
897
898 if (buffer_new(bh)) {
899 if (block_end > from && block_start < to) {
900 if (!PageUptodate(page)) {
901 unsigned start, end;
3a307ffc
MF
902
903 start = max(from, block_start);
904 end = min(to, block_end);
905
54c57dc3 906 zero_user_page(page, start, end - start, KM_USER0);
3a307ffc
MF
907 set_buffer_uptodate(bh);
908 }
909
910 clear_buffer_new(bh);
911 mark_buffer_dirty(bh);
912 }
913 }
9517bac6 914
3a307ffc
MF
915 block_start = block_end;
916 bh = bh->b_this_page;
917 } while (bh != head);
918}
919
920/*
921 * Only called when we have a failure during allocating write to write
922 * zero's to the newly allocated region.
923 */
924static void ocfs2_write_failure(struct inode *inode,
925 struct ocfs2_write_ctxt *wc,
926 loff_t user_pos, unsigned user_len)
927{
928 int i;
5c26a7b7
MF
929 unsigned from = user_pos & (PAGE_CACHE_SIZE - 1),
930 to = user_pos + user_len;
3a307ffc
MF
931 struct page *tmppage;
932
5c26a7b7 933 ocfs2_zero_new_buffers(wc->w_target_page, from, to);
9517bac6 934
3a307ffc
MF
935 for(i = 0; i < wc->w_num_pages; i++) {
936 tmppage = wc->w_pages[i];
9517bac6 937
3a307ffc
MF
938 if (ocfs2_should_order_data(inode))
939 walk_page_buffers(wc->w_handle, page_buffers(tmppage),
940 from, to, NULL,
941 ocfs2_journal_dirty_data);
eeb47d12 942
3a307ffc 943 block_commit_write(tmppage, from, to);
9517bac6 944 }
9517bac6
MF
945}
946
3a307ffc
MF
947static int ocfs2_prepare_page_for_write(struct inode *inode, u64 *p_blkno,
948 struct ocfs2_write_ctxt *wc,
949 struct page *page, u32 cpos,
950 loff_t user_pos, unsigned user_len,
951 int new)
9517bac6 952{
3a307ffc
MF
953 int ret;
954 unsigned int map_from = 0, map_to = 0;
9517bac6 955 unsigned int cluster_start, cluster_end;
3a307ffc 956 unsigned int user_data_from = 0, user_data_to = 0;
9517bac6 957
3a307ffc 958 ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), cpos,
9517bac6
MF
959 &cluster_start, &cluster_end);
960
3a307ffc
MF
961 if (page == wc->w_target_page) {
962 map_from = user_pos & (PAGE_CACHE_SIZE - 1);
963 map_to = map_from + user_len;
964
965 if (new)
966 ret = ocfs2_map_page_blocks(page, p_blkno, inode,
967 cluster_start, cluster_end,
968 new);
969 else
970 ret = ocfs2_map_page_blocks(page, p_blkno, inode,
971 map_from, map_to, new);
972 if (ret) {
9517bac6
MF
973 mlog_errno(ret);
974 goto out;
975 }
976
3a307ffc
MF
977 user_data_from = map_from;
978 user_data_to = map_to;
9517bac6 979 if (new) {
3a307ffc
MF
980 map_from = cluster_start;
981 map_to = cluster_end;
9517bac6
MF
982 }
983 } else {
984 /*
985 * If we haven't allocated the new page yet, we
986 * shouldn't be writing it out without copying user
987 * data. This is likely a math error from the caller.
988 */
989 BUG_ON(!new);
990
3a307ffc
MF
991 map_from = cluster_start;
992 map_to = cluster_end;
9517bac6
MF
993
994 ret = ocfs2_map_page_blocks(page, p_blkno, inode,
3a307ffc 995 cluster_start, cluster_end, new);
9517bac6
MF
996 if (ret) {
997 mlog_errno(ret);
998 goto out;
999 }
1000 }
1001
1002 /*
1003 * Parts of newly allocated pages need to be zero'd.
1004 *
1005 * Above, we have also rewritten 'to' and 'from' - as far as
1006 * the rest of the function is concerned, the entire cluster
1007 * range inside of a page needs to be written.
1008 *
1009 * We can skip this if the page is up to date - it's already
1010 * been zero'd from being read in as a hole.
1011 */
1012 if (new && !PageUptodate(page))
1013 ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb),
3a307ffc 1014 cpos, user_data_from, user_data_to);
9517bac6
MF
1015
1016 flush_dcache_page(page);
1017
9517bac6 1018out:
3a307ffc 1019 return ret;
9517bac6
MF
1020}
1021
1022/*
3a307ffc 1023 * This function will only grab one clusters worth of pages.
9517bac6 1024 */
3a307ffc
MF
1025static int ocfs2_grab_pages_for_write(struct address_space *mapping,
1026 struct ocfs2_write_ctxt *wc,
7307de80
MF
1027 u32 cpos, loff_t user_pos, int new,
1028 struct page *mmap_page)
9517bac6 1029{
3a307ffc
MF
1030 int ret = 0, i;
1031 unsigned long start, target_index, index;
9517bac6 1032 struct inode *inode = mapping->host;
9517bac6 1033
3a307ffc 1034 target_index = user_pos >> PAGE_CACHE_SHIFT;
9517bac6
MF
1035
1036 /*
1037 * Figure out how many pages we'll be manipulating here. For
60b11392
MF
1038 * non allocating write, we just change the one
1039 * page. Otherwise, we'll need a whole clusters worth.
9517bac6 1040 */
9517bac6 1041 if (new) {
3a307ffc
MF
1042 wc->w_num_pages = ocfs2_pages_per_cluster(inode->i_sb);
1043 start = ocfs2_align_clusters_to_page_index(inode->i_sb, cpos);
9517bac6 1044 } else {
3a307ffc
MF
1045 wc->w_num_pages = 1;
1046 start = target_index;
9517bac6
MF
1047 }
1048
3a307ffc 1049 for(i = 0; i < wc->w_num_pages; i++) {
9517bac6
MF
1050 index = start + i;
1051
7307de80
MF
1052 if (index == target_index && mmap_page) {
1053 /*
1054 * ocfs2_pagemkwrite() is a little different
1055 * and wants us to directly use the page
1056 * passed in.
1057 */
1058 lock_page(mmap_page);
1059
1060 if (mmap_page->mapping != mapping) {
1061 unlock_page(mmap_page);
1062 /*
1063 * Sanity check - the locking in
1064 * ocfs2_pagemkwrite() should ensure
1065 * that this code doesn't trigger.
1066 */
1067 ret = -EINVAL;
1068 mlog_errno(ret);
1069 goto out;
1070 }
1071
1072 page_cache_get(mmap_page);
1073 wc->w_pages[i] = mmap_page;
1074 } else {
1075 wc->w_pages[i] = find_or_create_page(mapping, index,
1076 GFP_NOFS);
1077 if (!wc->w_pages[i]) {
1078 ret = -ENOMEM;
1079 mlog_errno(ret);
1080 goto out;
1081 }
9517bac6 1082 }
3a307ffc
MF
1083
1084 if (index == target_index)
1085 wc->w_target_page = wc->w_pages[i];
9517bac6 1086 }
3a307ffc
MF
1087out:
1088 return ret;
1089}
1090
1091/*
1092 * Prepare a single cluster for write one cluster into the file.
1093 */
1094static int ocfs2_write_cluster(struct address_space *mapping,
b27b7cbc
MF
1095 u32 phys, unsigned int unwritten,
1096 struct ocfs2_alloc_context *data_ac,
3a307ffc
MF
1097 struct ocfs2_alloc_context *meta_ac,
1098 struct ocfs2_write_ctxt *wc, u32 cpos,
1099 loff_t user_pos, unsigned user_len)
1100{
b27b7cbc 1101 int ret, i, new, should_zero = 0;
3a307ffc
MF
1102 u64 v_blkno, p_blkno;
1103 struct inode *inode = mapping->host;
1104
1105 new = phys == 0 ? 1 : 0;
b27b7cbc
MF
1106 if (new || unwritten)
1107 should_zero = 1;
9517bac6
MF
1108
1109 if (new) {
3a307ffc
MF
1110 u32 tmp_pos;
1111
9517bac6
MF
1112 /*
1113 * This is safe to call with the page locks - it won't take
1114 * any additional semaphores or cluster locks.
1115 */
3a307ffc 1116 tmp_pos = cpos;
9517bac6 1117 ret = ocfs2_do_extend_allocation(OCFS2_SB(inode->i_sb), inode,
2ae99a60 1118 &tmp_pos, 1, 0, wc->w_di_bh,
3a307ffc
MF
1119 wc->w_handle, data_ac,
1120 meta_ac, NULL);
9517bac6
MF
1121 /*
1122 * This shouldn't happen because we must have already
1123 * calculated the correct meta data allocation required. The
1124 * internal tree allocation code should know how to increase
1125 * transaction credits itself.
1126 *
1127 * If need be, we could handle -EAGAIN for a
1128 * RESTART_TRANS here.
1129 */
1130 mlog_bug_on_msg(ret == -EAGAIN,
1131 "Inode %llu: EAGAIN return during allocation.\n",
1132 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1133 if (ret < 0) {
1134 mlog_errno(ret);
1135 goto out;
1136 }
b27b7cbc
MF
1137 } else if (unwritten) {
1138 ret = ocfs2_mark_extent_written(inode, wc->w_di_bh,
1139 wc->w_handle, cpos, 1, phys,
1140 meta_ac, &wc->w_dealloc);
1141 if (ret < 0) {
1142 mlog_errno(ret);
1143 goto out;
1144 }
1145 }
3a307ffc 1146
b27b7cbc 1147 if (should_zero)
3a307ffc 1148 v_blkno = ocfs2_clusters_to_blocks(inode->i_sb, cpos);
b27b7cbc 1149 else
3a307ffc 1150 v_blkno = user_pos >> inode->i_sb->s_blocksize_bits;
9517bac6 1151
3a307ffc
MF
1152 /*
1153 * The only reason this should fail is due to an inability to
1154 * find the extent added.
1155 */
49cb8d2d
MF
1156 ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL,
1157 NULL);
9517bac6 1158 if (ret < 0) {
3a307ffc
MF
1159 ocfs2_error(inode->i_sb, "Corrupting extend for inode %llu, "
1160 "at logical block %llu",
1161 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1162 (unsigned long long)v_blkno);
9517bac6
MF
1163 goto out;
1164 }
1165
1166 BUG_ON(p_blkno == 0);
1167
3a307ffc
MF
1168 for(i = 0; i < wc->w_num_pages; i++) {
1169 int tmpret;
9517bac6 1170
3a307ffc
MF
1171 tmpret = ocfs2_prepare_page_for_write(inode, &p_blkno, wc,
1172 wc->w_pages[i], cpos,
b27b7cbc
MF
1173 user_pos, user_len,
1174 should_zero);
3a307ffc
MF
1175 if (tmpret) {
1176 mlog_errno(tmpret);
1177 if (ret == 0)
1178 tmpret = ret;
1179 }
9517bac6
MF
1180 }
1181
3a307ffc
MF
1182 /*
1183 * We only have cleanup to do in case of allocating write.
1184 */
1185 if (ret && new)
1186 ocfs2_write_failure(inode, wc, user_pos, user_len);
1187
9517bac6 1188out:
9517bac6 1189
3a307ffc 1190 return ret;
9517bac6
MF
1191}
1192
0d172baa
MF
1193static int ocfs2_write_cluster_by_desc(struct address_space *mapping,
1194 struct ocfs2_alloc_context *data_ac,
1195 struct ocfs2_alloc_context *meta_ac,
1196 struct ocfs2_write_ctxt *wc,
1197 loff_t pos, unsigned len)
1198{
1199 int ret, i;
db56246c
MF
1200 loff_t cluster_off;
1201 unsigned int local_len = len;
0d172baa 1202 struct ocfs2_write_cluster_desc *desc;
db56246c 1203 struct ocfs2_super *osb = OCFS2_SB(mapping->host->i_sb);
0d172baa
MF
1204
1205 for (i = 0; i < wc->w_clen; i++) {
1206 desc = &wc->w_desc[i];
1207
db56246c
MF
1208 /*
1209 * We have to make sure that the total write passed in
1210 * doesn't extend past a single cluster.
1211 */
1212 local_len = len;
1213 cluster_off = pos & (osb->s_clustersize - 1);
1214 if ((cluster_off + local_len) > osb->s_clustersize)
1215 local_len = osb->s_clustersize - cluster_off;
1216
b27b7cbc
MF
1217 ret = ocfs2_write_cluster(mapping, desc->c_phys,
1218 desc->c_unwritten, data_ac, meta_ac,
db56246c 1219 wc, desc->c_cpos, pos, local_len);
0d172baa
MF
1220 if (ret) {
1221 mlog_errno(ret);
1222 goto out;
1223 }
db56246c
MF
1224
1225 len -= local_len;
1226 pos += local_len;
0d172baa
MF
1227 }
1228
1229 ret = 0;
1230out:
1231 return ret;
1232}
1233
3a307ffc
MF
1234/*
1235 * ocfs2_write_end() wants to know which parts of the target page it
1236 * should complete the write on. It's easiest to compute them ahead of
1237 * time when a more complete view of the write is available.
1238 */
1239static void ocfs2_set_target_boundaries(struct ocfs2_super *osb,
1240 struct ocfs2_write_ctxt *wc,
1241 loff_t pos, unsigned len, int alloc)
9517bac6 1242{
3a307ffc 1243 struct ocfs2_write_cluster_desc *desc;
9517bac6 1244
3a307ffc
MF
1245 wc->w_target_from = pos & (PAGE_CACHE_SIZE - 1);
1246 wc->w_target_to = wc->w_target_from + len;
1247
1248 if (alloc == 0)
1249 return;
1250
1251 /*
1252 * Allocating write - we may have different boundaries based
1253 * on page size and cluster size.
1254 *
1255 * NOTE: We can no longer compute one value from the other as
1256 * the actual write length and user provided length may be
1257 * different.
1258 */
9517bac6 1259
3a307ffc
MF
1260 if (wc->w_large_pages) {
1261 /*
1262 * We only care about the 1st and last cluster within
b27b7cbc 1263 * our range and whether they should be zero'd or not. Either
3a307ffc
MF
1264 * value may be extended out to the start/end of a
1265 * newly allocated cluster.
1266 */
1267 desc = &wc->w_desc[0];
b27b7cbc 1268 if (ocfs2_should_zero_cluster(desc))
3a307ffc
MF
1269 ocfs2_figure_cluster_boundaries(osb,
1270 desc->c_cpos,
1271 &wc->w_target_from,
1272 NULL);
1273
1274 desc = &wc->w_desc[wc->w_clen - 1];
b27b7cbc 1275 if (ocfs2_should_zero_cluster(desc))
3a307ffc
MF
1276 ocfs2_figure_cluster_boundaries(osb,
1277 desc->c_cpos,
1278 NULL,
1279 &wc->w_target_to);
1280 } else {
1281 wc->w_target_from = 0;
1282 wc->w_target_to = PAGE_CACHE_SIZE;
1283 }
9517bac6
MF
1284}
1285
0d172baa
MF
1286/*
1287 * Populate each single-cluster write descriptor in the write context
1288 * with information about the i/o to be done.
b27b7cbc
MF
1289 *
1290 * Returns the number of clusters that will have to be allocated, as
1291 * well as a worst case estimate of the number of extent records that
1292 * would have to be created during a write to an unwritten region.
0d172baa
MF
1293 */
1294static int ocfs2_populate_write_desc(struct inode *inode,
1295 struct ocfs2_write_ctxt *wc,
b27b7cbc
MF
1296 unsigned int *clusters_to_alloc,
1297 unsigned int *extents_to_split)
9517bac6 1298{
0d172baa 1299 int ret;
3a307ffc 1300 struct ocfs2_write_cluster_desc *desc;
0d172baa 1301 unsigned int num_clusters = 0;
b27b7cbc 1302 unsigned int ext_flags = 0;
0d172baa
MF
1303 u32 phys = 0;
1304 int i;
9517bac6 1305
b27b7cbc
MF
1306 *clusters_to_alloc = 0;
1307 *extents_to_split = 0;
1308
3a307ffc
MF
1309 for (i = 0; i < wc->w_clen; i++) {
1310 desc = &wc->w_desc[i];
1311 desc->c_cpos = wc->w_cpos + i;
1312
1313 if (num_clusters == 0) {
b27b7cbc
MF
1314 /*
1315 * Need to look up the next extent record.
1316 */
3a307ffc 1317 ret = ocfs2_get_clusters(inode, desc->c_cpos, &phys,
b27b7cbc 1318 &num_clusters, &ext_flags);
3a307ffc
MF
1319 if (ret) {
1320 mlog_errno(ret);
607d44aa 1321 goto out;
3a307ffc 1322 }
b27b7cbc
MF
1323
1324 /*
1325 * Assume worst case - that we're writing in
1326 * the middle of the extent.
1327 *
1328 * We can assume that the write proceeds from
1329 * left to right, in which case the extent
1330 * insert code is smart enough to coalesce the
1331 * next splits into the previous records created.
1332 */
1333 if (ext_flags & OCFS2_EXT_UNWRITTEN)
1334 *extents_to_split = *extents_to_split + 2;
3a307ffc
MF
1335 } else if (phys) {
1336 /*
1337 * Only increment phys if it doesn't describe
1338 * a hole.
1339 */
1340 phys++;
1341 }
1342
1343 desc->c_phys = phys;
1344 if (phys == 0) {
1345 desc->c_new = 1;
0d172baa 1346 *clusters_to_alloc = *clusters_to_alloc + 1;
3a307ffc 1347 }
b27b7cbc
MF
1348 if (ext_flags & OCFS2_EXT_UNWRITTEN)
1349 desc->c_unwritten = 1;
3a307ffc
MF
1350
1351 num_clusters--;
9517bac6
MF
1352 }
1353
0d172baa
MF
1354 ret = 0;
1355out:
1356 return ret;
1357}
1358
65ed39d6
MF
1359/*
1360 * This function only does anything for file systems which can't
1361 * handle sparse files.
1362 *
1363 * What we want to do here is fill in any hole between the current end
1364 * of allocation and the end of our write. That way the rest of the
1365 * write path can treat it as an non-allocating write, which has no
1366 * special case code for sparse/nonsparse files.
1367 */
1368static int ocfs2_expand_nonsparse_inode(struct inode *inode, loff_t pos,
1369 unsigned len,
1370 struct ocfs2_write_ctxt *wc)
1371{
1372 int ret;
1373 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1374 loff_t newsize = pos + len;
1375
1376 if (ocfs2_sparse_alloc(osb))
1377 return 0;
1378
1379 if (newsize <= i_size_read(inode))
1380 return 0;
1381
1382 ret = ocfs2_extend_no_holes(inode, newsize, newsize - len);
1383 if (ret)
1384 mlog_errno(ret);
1385
1386 return ret;
1387}
1388
0d172baa
MF
1389int ocfs2_write_begin_nolock(struct address_space *mapping,
1390 loff_t pos, unsigned len, unsigned flags,
1391 struct page **pagep, void **fsdata,
1392 struct buffer_head *di_bh, struct page *mmap_page)
1393{
1394 int ret, credits = OCFS2_INODE_UPDATE_CREDITS;
b27b7cbc 1395 unsigned int clusters_to_alloc, extents_to_split;
0d172baa
MF
1396 struct ocfs2_write_ctxt *wc;
1397 struct inode *inode = mapping->host;
1398 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1399 struct ocfs2_dinode *di;
1400 struct ocfs2_alloc_context *data_ac = NULL;
1401 struct ocfs2_alloc_context *meta_ac = NULL;
1402 handle_t *handle;
1403
1404 ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, di_bh);
1405 if (ret) {
1406 mlog_errno(ret);
1407 return ret;
1408 }
1409
65ed39d6
MF
1410 ret = ocfs2_expand_nonsparse_inode(inode, pos, len, wc);
1411 if (ret) {
1412 mlog_errno(ret);
1413 goto out;
1414 }
1415
b27b7cbc
MF
1416 ret = ocfs2_populate_write_desc(inode, wc, &clusters_to_alloc,
1417 &extents_to_split);
0d172baa
MF
1418 if (ret) {
1419 mlog_errno(ret);
1420 goto out;
1421 }
1422
1423 di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
1424
3a307ffc
MF
1425 /*
1426 * We set w_target_from, w_target_to here so that
1427 * ocfs2_write_end() knows which range in the target page to
1428 * write out. An allocation requires that we write the entire
1429 * cluster range.
1430 */
b27b7cbc 1431 if (clusters_to_alloc || extents_to_split) {
3a307ffc
MF
1432 /*
1433 * XXX: We are stretching the limits of
b27b7cbc 1434 * ocfs2_lock_allocators(). It greatly over-estimates
3a307ffc
MF
1435 * the work to be done.
1436 */
1437 ret = ocfs2_lock_allocators(inode, di, clusters_to_alloc,
b27b7cbc 1438 extents_to_split, &data_ac, &meta_ac);
9517bac6
MF
1439 if (ret) {
1440 mlog_errno(ret);
607d44aa 1441 goto out;
9517bac6
MF
1442 }
1443
3a307ffc
MF
1444 credits = ocfs2_calc_extend_credits(inode->i_sb, di,
1445 clusters_to_alloc);
1446
9517bac6
MF
1447 }
1448
b27b7cbc
MF
1449 ocfs2_set_target_boundaries(osb, wc, pos, len,
1450 clusters_to_alloc + extents_to_split);
3a307ffc 1451
9517bac6
MF
1452 handle = ocfs2_start_trans(osb, credits);
1453 if (IS_ERR(handle)) {
1454 ret = PTR_ERR(handle);
1455 mlog_errno(ret);
607d44aa 1456 goto out;
9517bac6
MF
1457 }
1458
3a307ffc
MF
1459 wc->w_handle = handle;
1460
1461 /*
1462 * We don't want this to fail in ocfs2_write_end(), so do it
1463 * here.
1464 */
1465 ret = ocfs2_journal_access(handle, inode, wc->w_di_bh,
1466 OCFS2_JOURNAL_ACCESS_WRITE);
1467 if (ret) {
9517bac6
MF
1468 mlog_errno(ret);
1469 goto out_commit;
1470 }
1471
3a307ffc
MF
1472 /*
1473 * Fill our page array first. That way we've grabbed enough so
1474 * that we can zero and flush if we error after adding the
1475 * extent.
1476 */
1477 ret = ocfs2_grab_pages_for_write(mapping, wc, wc->w_cpos, pos,
b27b7cbc
MF
1478 clusters_to_alloc + extents_to_split,
1479 mmap_page);
9517bac6
MF
1480 if (ret) {
1481 mlog_errno(ret);
1482 goto out_commit;
1483 }
1484
0d172baa
MF
1485 ret = ocfs2_write_cluster_by_desc(mapping, data_ac, meta_ac, wc, pos,
1486 len);
1487 if (ret) {
1488 mlog_errno(ret);
1489 goto out_commit;
9517bac6 1490 }
9517bac6 1491
3a307ffc
MF
1492 if (data_ac)
1493 ocfs2_free_alloc_context(data_ac);
1494 if (meta_ac)
1495 ocfs2_free_alloc_context(meta_ac);
9517bac6 1496
3a307ffc
MF
1497 *pagep = wc->w_target_page;
1498 *fsdata = wc;
1499 return 0;
9517bac6
MF
1500out_commit:
1501 ocfs2_commit_trans(osb, handle);
1502
9517bac6 1503out:
3a307ffc
MF
1504 ocfs2_free_write_ctxt(wc);
1505
9517bac6
MF
1506 if (data_ac)
1507 ocfs2_free_alloc_context(data_ac);
1508 if (meta_ac)
1509 ocfs2_free_alloc_context(meta_ac);
3a307ffc
MF
1510 return ret;
1511}
1512
607d44aa
MF
1513int ocfs2_write_begin(struct file *file, struct address_space *mapping,
1514 loff_t pos, unsigned len, unsigned flags,
1515 struct page **pagep, void **fsdata)
1516{
1517 int ret;
1518 struct buffer_head *di_bh = NULL;
1519 struct inode *inode = mapping->host;
1520
1521 ret = ocfs2_meta_lock(inode, &di_bh, 1);
1522 if (ret) {
1523 mlog_errno(ret);
1524 return ret;
1525 }
1526
1527 /*
1528 * Take alloc sem here to prevent concurrent lookups. That way
1529 * the mapping, zeroing and tree manipulation within
1530 * ocfs2_write() will be safe against ->readpage(). This
1531 * should also serve to lock out allocation from a shared
1532 * writeable region.
1533 */
1534 down_write(&OCFS2_I(inode)->ip_alloc_sem);
1535
1536 ret = ocfs2_data_lock(inode, 1);
1537 if (ret) {
1538 mlog_errno(ret);
1539 goto out_fail;
1540 }
1541
1542 ret = ocfs2_write_begin_nolock(mapping, pos, len, flags, pagep,
7307de80 1543 fsdata, di_bh, NULL);
607d44aa
MF
1544 if (ret) {
1545 mlog_errno(ret);
1546 goto out_fail_data;
1547 }
1548
1549 brelse(di_bh);
1550
1551 return 0;
1552
1553out_fail_data:
1554 ocfs2_data_unlock(inode, 1);
1555out_fail:
1556 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1557
1558 brelse(di_bh);
1559 ocfs2_meta_unlock(inode, 1);
1560
1561 return ret;
1562}
1563
7307de80
MF
1564int ocfs2_write_end_nolock(struct address_space *mapping,
1565 loff_t pos, unsigned len, unsigned copied,
1566 struct page *page, void *fsdata)
3a307ffc
MF
1567{
1568 int i;
1569 unsigned from, to, start = pos & (PAGE_CACHE_SIZE - 1);
1570 struct inode *inode = mapping->host;
1571 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1572 struct ocfs2_write_ctxt *wc = fsdata;
1573 struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
1574 handle_t *handle = wc->w_handle;
1575 struct page *tmppage;
1576
1577 if (unlikely(copied < len)) {
1578 if (!PageUptodate(wc->w_target_page))
1579 copied = 0;
1580
1581 ocfs2_zero_new_buffers(wc->w_target_page, start+copied,
1582 start+len);
1583 }
1584 flush_dcache_page(wc->w_target_page);
1585
1586 for(i = 0; i < wc->w_num_pages; i++) {
1587 tmppage = wc->w_pages[i];
1588
1589 if (tmppage == wc->w_target_page) {
1590 from = wc->w_target_from;
1591 to = wc->w_target_to;
1592
1593 BUG_ON(from > PAGE_CACHE_SIZE ||
1594 to > PAGE_CACHE_SIZE ||
1595 to < from);
1596 } else {
1597 /*
1598 * Pages adjacent to the target (if any) imply
1599 * a hole-filling write in which case we want
1600 * to flush their entire range.
1601 */
1602 from = 0;
1603 to = PAGE_CACHE_SIZE;
1604 }
1605
1606 if (ocfs2_should_order_data(inode))
1607 walk_page_buffers(wc->w_handle, page_buffers(tmppage),
1608 from, to, NULL,
1609 ocfs2_journal_dirty_data);
1610
1611 block_commit_write(tmppage, from, to);
1612 }
1613
1614 pos += copied;
1615 if (pos > inode->i_size) {
1616 i_size_write(inode, pos);
1617 mark_inode_dirty(inode);
1618 }
1619 inode->i_blocks = ocfs2_inode_sector_count(inode);
1620 di->i_size = cpu_to_le64((u64)i_size_read(inode));
1621 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1622 di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
1623 di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
3a307ffc
MF
1624 ocfs2_journal_dirty(handle, wc->w_di_bh);
1625
1626 ocfs2_commit_trans(osb, handle);
59a5e416 1627
b27b7cbc
MF
1628 ocfs2_run_deallocs(osb, &wc->w_dealloc);
1629
607d44aa
MF
1630 ocfs2_free_write_ctxt(wc);
1631
1632 return copied;
1633}
1634
1635int ocfs2_write_end(struct file *file, struct address_space *mapping,
1636 loff_t pos, unsigned len, unsigned copied,
1637 struct page *page, void *fsdata)
1638{
1639 int ret;
1640 struct inode *inode = mapping->host;
1641
1642 ret = ocfs2_write_end_nolock(mapping, pos, len, copied, page, fsdata);
1643
3a307ffc
MF
1644 ocfs2_data_unlock(inode, 1);
1645 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1646 ocfs2_meta_unlock(inode, 1);
9517bac6 1647
607d44aa 1648 return ret;
9517bac6
MF
1649}
1650
f5e54d6e 1651const struct address_space_operations ocfs2_aops = {
ccd979bd
MF
1652 .readpage = ocfs2_readpage,
1653 .writepage = ocfs2_writepage,
ccd979bd
MF
1654 .bmap = ocfs2_bmap,
1655 .sync_page = block_sync_page,
03f981cf
JB
1656 .direct_IO = ocfs2_direct_IO,
1657 .invalidatepage = ocfs2_invalidatepage,
1658 .releasepage = ocfs2_releasepage,
1659 .migratepage = buffer_migrate_page,
ccd979bd 1660};
This page took 0.363971 seconds and 5 git commands to generate.