ACPI: thinkpad-acpi: keep track of module state
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ocfs2 / aops.c
blob0b5e35f393805e044d69739c16723aa3cb5520dc
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
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.
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.
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.
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>
27 #include <linux/swap.h>
28 #include <linux/pipe_fs_i.h>
30 #define MLOG_MASK_PREFIX ML_FILE_IO
31 #include <cluster/masklog.h>
33 #include "ocfs2.h"
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"
42 #include "suballoc.h"
43 #include "super.h"
44 #include "symlink.h"
46 #include "buffer_head_io.h"
48 static int ocfs2_symlink_get_block(struct inode *inode, sector_t iblock,
49 struct buffer_head *bh_result, int create)
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;
59 mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode,
60 (unsigned long long)iblock, bh_result, create);
62 BUG_ON(ocfs2_inode_is_fast_symlink(inode));
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;
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;
77 fe = (struct ocfs2_dinode *) bh->b_data;
79 if (!OCFS2_IS_VALID_DINODE(fe)) {
80 mlog(ML_ERROR, "Invalid dinode #%llu: signature = %.*s\n",
81 (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
82 fe->i_signature);
83 goto bail;
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;
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;
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;
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);
121 brelse(buffer_cache_bh);
124 map_bh(bh_result, inode->i_sb,
125 le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) + iblock);
127 err = 0;
129 bail:
130 if (bh)
131 brelse(bh);
133 mlog_exit(err);
134 return err;
137 static int ocfs2_get_block(struct inode *inode, sector_t iblock,
138 struct buffer_head *bh_result, int create)
140 int err = 0;
141 unsigned int ext_flags;
142 u64 p_blkno, past_eof;
143 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
145 mlog_entry("(0x%p, %llu, 0x%p, %d)\n", inode,
146 (unsigned long long)iblock, bh_result, create);
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);
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;
158 err = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, NULL,
159 &ext_flags);
160 if (err) {
161 mlog(ML_ERROR, "Error %d from get_blocks(0x%p, %llu, 1, "
162 "%llu, NULL)\n", err, inode, (unsigned long long)iblock,
163 (unsigned long long)p_blkno);
164 goto bail;
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.
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);
177 /* Treat the unwritten extent as a hole for zeroing purposes. */
178 if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
179 map_bh(bh_result, inode->i_sb, p_blkno);
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();
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);
197 if (create && (iblock >= past_eof))
198 set_buffer_new(bh_result);
201 bail:
202 if (err < 0)
203 err = -EIO;
205 mlog_exit(err);
206 return err;
209 static int ocfs2_readpage(struct file *file, struct page *page)
211 struct inode *inode = page->mapping->host;
212 loff_t start = (loff_t)page->index << PAGE_CACHE_SHIFT;
213 int ret, unlock = 1;
215 mlog_entry("(0x%p, %lu)\n", file, (page ? page->index : 0));
217 ret = ocfs2_meta_lock_with_page(inode, NULL, 0, page);
218 if (ret != 0) {
219 if (ret == AOP_TRUNCATED_PAGE)
220 unlock = 0;
221 mlog_errno(ret);
222 goto out;
225 if (down_read_trylock(&OCFS2_I(inode)->ip_alloc_sem) == 0) {
226 ret = AOP_TRUNCATED_PAGE;
227 goto out_meta_unlock;
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
235 * (generic_file_read, vm_ops->fault) are clever enough to check i_size
236 * and notice that the page they just read isn't needed.
238 * XXX sys_readahead() seems to get that wrong?
240 if (start >= i_size_read(inode)) {
241 zero_user_page(page, 0, PAGE_SIZE, KM_USER0);
242 SetPageUptodate(page);
243 ret = 0;
244 goto out_alloc;
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;
255 ret = block_read_full_page(page, ocfs2_get_block);
256 unlock = 0;
258 ocfs2_data_unlock(inode, 0);
259 out_alloc:
260 up_read(&OCFS2_I(inode)->ip_alloc_sem);
261 out_meta_unlock:
262 ocfs2_meta_unlock(inode, 0);
263 out:
264 if (unlock)
265 unlock_page(page);
266 mlog_exit(ret);
267 return ret;
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.
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.
281 static int ocfs2_writepage(struct page *page, struct writeback_control *wbc)
283 int ret;
285 mlog_entry("(0x%p)\n", page);
287 ret = block_write_full_page(page, ocfs2_get_block, wbc);
289 mlog_exit(ret);
291 return ret;
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.
299 int ocfs2_prepare_write_nolock(struct inode *inode, struct page *page,
300 unsigned from, unsigned to)
302 int ret;
304 down_read(&OCFS2_I(inode)->ip_alloc_sem);
306 ret = block_prepare_write(page, from, to, ocfs2_get_block);
308 up_read(&OCFS2_I(inode)->ip_alloc_sem);
310 return ret;
313 /* Taken from ext3. We don't necessarily need the full blown
314 * functionality yet, but IMHO it's better to cut and paste the whole
315 * thing so we can avoid introducing our own bugs (and easily pick up
316 * their fixes when they happen) --Mark */
317 int walk_page_buffers( handle_t *handle,
318 struct buffer_head *head,
319 unsigned from,
320 unsigned to,
321 int *partial,
322 int (*fn)( handle_t *handle,
323 struct buffer_head *bh))
325 struct buffer_head *bh;
326 unsigned block_start, block_end;
327 unsigned blocksize = head->b_size;
328 int err, ret = 0;
329 struct buffer_head *next;
331 for ( bh = head, block_start = 0;
332 ret == 0 && (bh != head || !block_start);
333 block_start = block_end, bh = next)
335 next = bh->b_this_page;
336 block_end = block_start + blocksize;
337 if (block_end <= from || block_start >= to) {
338 if (partial && !buffer_uptodate(bh))
339 *partial = 1;
340 continue;
342 err = (*fn)(handle, bh);
343 if (!ret)
344 ret = err;
346 return ret;
349 handle_t *ocfs2_start_walk_page_trans(struct inode *inode,
350 struct page *page,
351 unsigned from,
352 unsigned to)
354 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
355 handle_t *handle = NULL;
356 int ret = 0;
358 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
359 if (!handle) {
360 ret = -ENOMEM;
361 mlog_errno(ret);
362 goto out;
365 if (ocfs2_should_order_data(inode)) {
366 ret = walk_page_buffers(handle,
367 page_buffers(page),
368 from, to, NULL,
369 ocfs2_journal_dirty_data);
370 if (ret < 0)
371 mlog_errno(ret);
373 out:
374 if (ret) {
375 if (handle)
376 ocfs2_commit_trans(osb, handle);
377 handle = ERR_PTR(ret);
379 return handle;
382 static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block)
384 sector_t status;
385 u64 p_blkno = 0;
386 int err = 0;
387 struct inode *inode = mapping->host;
389 mlog_entry("(block = %llu)\n", (unsigned long long)block);
391 /* We don't need to lock journal system files, since they aren't
392 * accessed concurrently from multiple nodes.
394 if (!INODE_JOURNAL(inode)) {
395 err = ocfs2_meta_lock(inode, NULL, 0);
396 if (err) {
397 if (err != -ENOENT)
398 mlog_errno(err);
399 goto bail;
401 down_read(&OCFS2_I(inode)->ip_alloc_sem);
404 err = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL, NULL);
406 if (!INODE_JOURNAL(inode)) {
407 up_read(&OCFS2_I(inode)->ip_alloc_sem);
408 ocfs2_meta_unlock(inode, 0);
411 if (err) {
412 mlog(ML_ERROR, "get_blocks() failed, block = %llu\n",
413 (unsigned long long)block);
414 mlog_errno(err);
415 goto bail;
419 bail:
420 status = err ? 0 : p_blkno;
422 mlog_exit((int)status);
424 return status;
428 * TODO: Make this into a generic get_blocks function.
430 * From do_direct_io in direct-io.c:
431 * "So what we do is to permit the ->get_blocks function to populate
432 * bh.b_size with the size of IO which is permitted at this offset and
433 * this i_blkbits."
435 * This function is called directly from get_more_blocks in direct-io.c.
437 * called like this: dio->get_blocks(dio->inode, fs_startblk,
438 * fs_count, map_bh, dio->rw == WRITE);
440 static int ocfs2_direct_IO_get_blocks(struct inode *inode, sector_t iblock,
441 struct buffer_head *bh_result, int create)
443 int ret;
444 u64 p_blkno, inode_blocks, contig_blocks;
445 unsigned int ext_flags;
446 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
447 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
449 /* This function won't even be called if the request isn't all
450 * nicely aligned and of the right size, so there's no need
451 * for us to check any of that. */
453 inode_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
456 * Any write past EOF is not allowed because we'd be extending.
458 if (create && (iblock + max_blocks) > inode_blocks) {
459 ret = -EIO;
460 goto bail;
463 /* This figures out the size of the next contiguous block, and
464 * our logical offset */
465 ret = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno,
466 &contig_blocks, &ext_flags);
467 if (ret) {
468 mlog(ML_ERROR, "get_blocks() failed iblock=%llu\n",
469 (unsigned long long)iblock);
470 ret = -EIO;
471 goto bail;
474 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)) && !p_blkno) {
475 ocfs2_error(inode->i_sb,
476 "Inode %llu has a hole at block %llu\n",
477 (unsigned long long)OCFS2_I(inode)->ip_blkno,
478 (unsigned long long)iblock);
479 ret = -EROFS;
480 goto bail;
484 * get_more_blocks() expects us to describe a hole by clearing
485 * the mapped bit on bh_result().
487 * Consider an unwritten extent as a hole.
489 if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
490 map_bh(bh_result, inode->i_sb, p_blkno);
491 else {
493 * ocfs2_prepare_inode_for_write() should have caught
494 * the case where we'd be filling a hole and triggered
495 * a buffered write instead.
497 if (create) {
498 ret = -EIO;
499 mlog_errno(ret);
500 goto bail;
503 clear_buffer_mapped(bh_result);
506 /* make sure we don't map more than max_blocks blocks here as
507 that's all the kernel will handle at this point. */
508 if (max_blocks < contig_blocks)
509 contig_blocks = max_blocks;
510 bh_result->b_size = contig_blocks << blocksize_bits;
511 bail:
512 return ret;
516 * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're
517 * particularly interested in the aio/dio case. Like the core uses
518 * i_alloc_sem, we use the rw_lock DLM lock to protect io on one node from
519 * truncation on another.
521 static void ocfs2_dio_end_io(struct kiocb *iocb,
522 loff_t offset,
523 ssize_t bytes,
524 void *private)
526 struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
527 int level;
529 /* this io's submitter should not have unlocked this before we could */
530 BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
532 ocfs2_iocb_clear_rw_locked(iocb);
534 level = ocfs2_iocb_rw_locked_level(iocb);
535 if (!level)
536 up_read(&inode->i_alloc_sem);
537 ocfs2_rw_unlock(inode, level);
541 * ocfs2_invalidatepage() and ocfs2_releasepage() are shamelessly stolen
542 * from ext3. PageChecked() bits have been removed as OCFS2 does not
543 * do journalled data.
545 static void ocfs2_invalidatepage(struct page *page, unsigned long offset)
547 journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
549 journal_invalidatepage(journal, page, offset);
552 static int ocfs2_releasepage(struct page *page, gfp_t wait)
554 journal_t *journal = OCFS2_SB(page->mapping->host->i_sb)->journal->j_journal;
556 if (!page_has_buffers(page))
557 return 0;
558 return journal_try_to_free_buffers(journal, page, wait);
561 static ssize_t ocfs2_direct_IO(int rw,
562 struct kiocb *iocb,
563 const struct iovec *iov,
564 loff_t offset,
565 unsigned long nr_segs)
567 struct file *file = iocb->ki_filp;
568 struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host;
569 int ret;
571 mlog_entry_void();
573 if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) {
575 * We get PR data locks even for O_DIRECT. This
576 * allows concurrent O_DIRECT I/O but doesn't let
577 * O_DIRECT with extending and buffered zeroing writes
578 * race. If they did race then the buffered zeroing
579 * could be written back after the O_DIRECT I/O. It's
580 * one thing to tell people not to mix buffered and
581 * O_DIRECT writes, but expecting them to understand
582 * that file extension is also an implicit buffered
583 * write is too much. By getting the PR we force
584 * writeback of the buffered zeroing before
585 * proceeding.
587 ret = ocfs2_data_lock(inode, 0);
588 if (ret < 0) {
589 mlog_errno(ret);
590 goto out;
592 ocfs2_data_unlock(inode, 0);
595 ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
596 inode->i_sb->s_bdev, iov, offset,
597 nr_segs,
598 ocfs2_direct_IO_get_blocks,
599 ocfs2_dio_end_io);
600 out:
601 mlog_exit(ret);
602 return ret;
605 static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb,
606 u32 cpos,
607 unsigned int *start,
608 unsigned int *end)
610 unsigned int cluster_start = 0, cluster_end = PAGE_CACHE_SIZE;
612 if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits)) {
613 unsigned int cpp;
615 cpp = 1 << (PAGE_CACHE_SHIFT - osb->s_clustersize_bits);
617 cluster_start = cpos % cpp;
618 cluster_start = cluster_start << osb->s_clustersize_bits;
620 cluster_end = cluster_start + osb->s_clustersize;
623 BUG_ON(cluster_start > PAGE_SIZE);
624 BUG_ON(cluster_end > PAGE_SIZE);
626 if (start)
627 *start = cluster_start;
628 if (end)
629 *end = cluster_end;
633 * 'from' and 'to' are the region in the page to avoid zeroing.
635 * If pagesize > clustersize, this function will avoid zeroing outside
636 * of the cluster boundary.
638 * from == to == 0 is code for "zero the entire cluster region"
640 static void ocfs2_clear_page_regions(struct page *page,
641 struct ocfs2_super *osb, u32 cpos,
642 unsigned from, unsigned to)
644 void *kaddr;
645 unsigned int cluster_start, cluster_end;
647 ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end);
649 kaddr = kmap_atomic(page, KM_USER0);
651 if (from || to) {
652 if (from > cluster_start)
653 memset(kaddr + cluster_start, 0, from - cluster_start);
654 if (to < cluster_end)
655 memset(kaddr + to, 0, cluster_end - to);
656 } else {
657 memset(kaddr + cluster_start, 0, cluster_end - cluster_start);
660 kunmap_atomic(kaddr, KM_USER0);
664 * Nonsparse file systems fully allocate before we get to the write
665 * code. This prevents ocfs2_write() from tagging the write as an
666 * allocating one, which means ocfs2_map_page_blocks() might try to
667 * read-in the blocks at the tail of our file. Avoid reading them by
668 * testing i_size against each block offset.
670 static int ocfs2_should_read_blk(struct inode *inode, struct page *page,
671 unsigned int block_start)
673 u64 offset = page_offset(page) + block_start;
675 if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
676 return 1;
678 if (i_size_read(inode) > offset)
679 return 1;
681 return 0;
685 * Some of this taken from block_prepare_write(). We already have our
686 * mapping by now though, and the entire write will be allocating or
687 * it won't, so not much need to use BH_New.
689 * This will also skip zeroing, which is handled externally.
691 int ocfs2_map_page_blocks(struct page *page, u64 *p_blkno,
692 struct inode *inode, unsigned int from,
693 unsigned int to, int new)
695 int ret = 0;
696 struct buffer_head *head, *bh, *wait[2], **wait_bh = wait;
697 unsigned int block_end, block_start;
698 unsigned int bsize = 1 << inode->i_blkbits;
700 if (!page_has_buffers(page))
701 create_empty_buffers(page, bsize, 0);
703 head = page_buffers(page);
704 for (bh = head, block_start = 0; bh != head || !block_start;
705 bh = bh->b_this_page, block_start += bsize) {
706 block_end = block_start + bsize;
708 clear_buffer_new(bh);
711 * Ignore blocks outside of our i/o range -
712 * they may belong to unallocated clusters.
714 if (block_start >= to || block_end <= from) {
715 if (PageUptodate(page))
716 set_buffer_uptodate(bh);
717 continue;
721 * For an allocating write with cluster size >= page
722 * size, we always write the entire page.
724 if (new)
725 set_buffer_new(bh);
727 if (!buffer_mapped(bh)) {
728 map_bh(bh, inode->i_sb, *p_blkno);
729 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
732 if (PageUptodate(page)) {
733 if (!buffer_uptodate(bh))
734 set_buffer_uptodate(bh);
735 } else if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
736 !buffer_new(bh) &&
737 ocfs2_should_read_blk(inode, page, block_start) &&
738 (block_start < from || block_end > to)) {
739 ll_rw_block(READ, 1, &bh);
740 *wait_bh++=bh;
743 *p_blkno = *p_blkno + 1;
747 * If we issued read requests - let them complete.
749 while(wait_bh > wait) {
750 wait_on_buffer(*--wait_bh);
751 if (!buffer_uptodate(*wait_bh))
752 ret = -EIO;
755 if (ret == 0 || !new)
756 return ret;
759 * If we get -EIO above, zero out any newly allocated blocks
760 * to avoid exposing stale data.
762 bh = head;
763 block_start = 0;
764 do {
765 block_end = block_start + bsize;
766 if (block_end <= from)
767 goto next_bh;
768 if (block_start >= to)
769 break;
771 zero_user_page(page, block_start, bh->b_size, KM_USER0);
772 set_buffer_uptodate(bh);
773 mark_buffer_dirty(bh);
775 next_bh:
776 block_start = block_end;
777 bh = bh->b_this_page;
778 } while (bh != head);
780 return ret;
783 #if (PAGE_CACHE_SIZE >= OCFS2_MAX_CLUSTERSIZE)
784 #define OCFS2_MAX_CTXT_PAGES 1
785 #else
786 #define OCFS2_MAX_CTXT_PAGES (OCFS2_MAX_CLUSTERSIZE / PAGE_CACHE_SIZE)
787 #endif
789 #define OCFS2_MAX_CLUSTERS_PER_PAGE (PAGE_CACHE_SIZE / OCFS2_MIN_CLUSTERSIZE)
792 * Describe the state of a single cluster to be written to.
794 struct ocfs2_write_cluster_desc {
795 u32 c_cpos;
796 u32 c_phys;
798 * Give this a unique field because c_phys eventually gets
799 * filled.
801 unsigned c_new;
802 unsigned c_unwritten;
805 static inline int ocfs2_should_zero_cluster(struct ocfs2_write_cluster_desc *d)
807 return d->c_new || d->c_unwritten;
810 struct ocfs2_write_ctxt {
811 /* Logical cluster position / len of write */
812 u32 w_cpos;
813 u32 w_clen;
815 struct ocfs2_write_cluster_desc w_desc[OCFS2_MAX_CLUSTERS_PER_PAGE];
818 * This is true if page_size > cluster_size.
820 * It triggers a set of special cases during write which might
821 * have to deal with allocating writes to partial pages.
823 unsigned int w_large_pages;
826 * Pages involved in this write.
828 * w_target_page is the page being written to by the user.
830 * w_pages is an array of pages which always contains
831 * w_target_page, and in the case of an allocating write with
832 * page_size < cluster size, it will contain zero'd and mapped
833 * pages adjacent to w_target_page which need to be written
834 * out in so that future reads from that region will get
835 * zero's.
837 struct page *w_pages[OCFS2_MAX_CTXT_PAGES];
838 unsigned int w_num_pages;
839 struct page *w_target_page;
842 * ocfs2_write_end() uses this to know what the real range to
843 * write in the target should be.
845 unsigned int w_target_from;
846 unsigned int w_target_to;
849 * We could use journal_current_handle() but this is cleaner,
850 * IMHO -Mark
852 handle_t *w_handle;
854 struct buffer_head *w_di_bh;
856 struct ocfs2_cached_dealloc_ctxt w_dealloc;
859 static void ocfs2_free_write_ctxt(struct ocfs2_write_ctxt *wc)
861 int i;
863 for(i = 0; i < wc->w_num_pages; i++) {
864 if (wc->w_pages[i] == NULL)
865 continue;
867 unlock_page(wc->w_pages[i]);
868 mark_page_accessed(wc->w_pages[i]);
869 page_cache_release(wc->w_pages[i]);
872 brelse(wc->w_di_bh);
873 kfree(wc);
876 static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp,
877 struct ocfs2_super *osb, loff_t pos,
878 unsigned len, struct buffer_head *di_bh)
880 u32 cend;
881 struct ocfs2_write_ctxt *wc;
883 wc = kzalloc(sizeof(struct ocfs2_write_ctxt), GFP_NOFS);
884 if (!wc)
885 return -ENOMEM;
887 wc->w_cpos = pos >> osb->s_clustersize_bits;
888 cend = (pos + len - 1) >> osb->s_clustersize_bits;
889 wc->w_clen = cend - wc->w_cpos + 1;
890 get_bh(di_bh);
891 wc->w_di_bh = di_bh;
893 if (unlikely(PAGE_CACHE_SHIFT > osb->s_clustersize_bits))
894 wc->w_large_pages = 1;
895 else
896 wc->w_large_pages = 0;
898 ocfs2_init_dealloc_ctxt(&wc->w_dealloc);
900 *wcp = wc;
902 return 0;
906 * If a page has any new buffers, zero them out here, and mark them uptodate
907 * and dirty so they'll be written out (in order to prevent uninitialised
908 * block data from leaking). And clear the new bit.
910 static void ocfs2_zero_new_buffers(struct page *page, unsigned from, unsigned to)
912 unsigned int block_start, block_end;
913 struct buffer_head *head, *bh;
915 BUG_ON(!PageLocked(page));
916 if (!page_has_buffers(page))
917 return;
919 bh = head = page_buffers(page);
920 block_start = 0;
921 do {
922 block_end = block_start + bh->b_size;
924 if (buffer_new(bh)) {
925 if (block_end > from && block_start < to) {
926 if (!PageUptodate(page)) {
927 unsigned start, end;
929 start = max(from, block_start);
930 end = min(to, block_end);
932 zero_user_page(page, start, end - start, KM_USER0);
933 set_buffer_uptodate(bh);
936 clear_buffer_new(bh);
937 mark_buffer_dirty(bh);
941 block_start = block_end;
942 bh = bh->b_this_page;
943 } while (bh != head);
947 * Only called when we have a failure during allocating write to write
948 * zero's to the newly allocated region.
950 static void ocfs2_write_failure(struct inode *inode,
951 struct ocfs2_write_ctxt *wc,
952 loff_t user_pos, unsigned user_len)
954 int i;
955 unsigned from = user_pos & (PAGE_CACHE_SIZE - 1),
956 to = user_pos + user_len;
957 struct page *tmppage;
959 ocfs2_zero_new_buffers(wc->w_target_page, from, to);
961 for(i = 0; i < wc->w_num_pages; i++) {
962 tmppage = wc->w_pages[i];
964 if (ocfs2_should_order_data(inode))
965 walk_page_buffers(wc->w_handle, page_buffers(tmppage),
966 from, to, NULL,
967 ocfs2_journal_dirty_data);
969 block_commit_write(tmppage, from, to);
973 static int ocfs2_prepare_page_for_write(struct inode *inode, u64 *p_blkno,
974 struct ocfs2_write_ctxt *wc,
975 struct page *page, u32 cpos,
976 loff_t user_pos, unsigned user_len,
977 int new)
979 int ret;
980 unsigned int map_from = 0, map_to = 0;
981 unsigned int cluster_start, cluster_end;
982 unsigned int user_data_from = 0, user_data_to = 0;
984 ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), cpos,
985 &cluster_start, &cluster_end);
987 if (page == wc->w_target_page) {
988 map_from = user_pos & (PAGE_CACHE_SIZE - 1);
989 map_to = map_from + user_len;
991 if (new)
992 ret = ocfs2_map_page_blocks(page, p_blkno, inode,
993 cluster_start, cluster_end,
994 new);
995 else
996 ret = ocfs2_map_page_blocks(page, p_blkno, inode,
997 map_from, map_to, new);
998 if (ret) {
999 mlog_errno(ret);
1000 goto out;
1003 user_data_from = map_from;
1004 user_data_to = map_to;
1005 if (new) {
1006 map_from = cluster_start;
1007 map_to = cluster_end;
1009 } else {
1011 * If we haven't allocated the new page yet, we
1012 * shouldn't be writing it out without copying user
1013 * data. This is likely a math error from the caller.
1015 BUG_ON(!new);
1017 map_from = cluster_start;
1018 map_to = cluster_end;
1020 ret = ocfs2_map_page_blocks(page, p_blkno, inode,
1021 cluster_start, cluster_end, new);
1022 if (ret) {
1023 mlog_errno(ret);
1024 goto out;
1029 * Parts of newly allocated pages need to be zero'd.
1031 * Above, we have also rewritten 'to' and 'from' - as far as
1032 * the rest of the function is concerned, the entire cluster
1033 * range inside of a page needs to be written.
1035 * We can skip this if the page is up to date - it's already
1036 * been zero'd from being read in as a hole.
1038 if (new && !PageUptodate(page))
1039 ocfs2_clear_page_regions(page, OCFS2_SB(inode->i_sb),
1040 cpos, user_data_from, user_data_to);
1042 flush_dcache_page(page);
1044 out:
1045 return ret;
1049 * This function will only grab one clusters worth of pages.
1051 static int ocfs2_grab_pages_for_write(struct address_space *mapping,
1052 struct ocfs2_write_ctxt *wc,
1053 u32 cpos, loff_t user_pos, int new,
1054 struct page *mmap_page)
1056 int ret = 0, i;
1057 unsigned long start, target_index, index;
1058 struct inode *inode = mapping->host;
1060 target_index = user_pos >> PAGE_CACHE_SHIFT;
1063 * Figure out how many pages we'll be manipulating here. For
1064 * non allocating write, we just change the one
1065 * page. Otherwise, we'll need a whole clusters worth.
1067 if (new) {
1068 wc->w_num_pages = ocfs2_pages_per_cluster(inode->i_sb);
1069 start = ocfs2_align_clusters_to_page_index(inode->i_sb, cpos);
1070 } else {
1071 wc->w_num_pages = 1;
1072 start = target_index;
1075 for(i = 0; i < wc->w_num_pages; i++) {
1076 index = start + i;
1078 if (index == target_index && mmap_page) {
1080 * ocfs2_pagemkwrite() is a little different
1081 * and wants us to directly use the page
1082 * passed in.
1084 lock_page(mmap_page);
1086 if (mmap_page->mapping != mapping) {
1087 unlock_page(mmap_page);
1089 * Sanity check - the locking in
1090 * ocfs2_pagemkwrite() should ensure
1091 * that this code doesn't trigger.
1093 ret = -EINVAL;
1094 mlog_errno(ret);
1095 goto out;
1098 page_cache_get(mmap_page);
1099 wc->w_pages[i] = mmap_page;
1100 } else {
1101 wc->w_pages[i] = find_or_create_page(mapping, index,
1102 GFP_NOFS);
1103 if (!wc->w_pages[i]) {
1104 ret = -ENOMEM;
1105 mlog_errno(ret);
1106 goto out;
1110 if (index == target_index)
1111 wc->w_target_page = wc->w_pages[i];
1113 out:
1114 return ret;
1118 * Prepare a single cluster for write one cluster into the file.
1120 static int ocfs2_write_cluster(struct address_space *mapping,
1121 u32 phys, unsigned int unwritten,
1122 struct ocfs2_alloc_context *data_ac,
1123 struct ocfs2_alloc_context *meta_ac,
1124 struct ocfs2_write_ctxt *wc, u32 cpos,
1125 loff_t user_pos, unsigned user_len)
1127 int ret, i, new, should_zero = 0;
1128 u64 v_blkno, p_blkno;
1129 struct inode *inode = mapping->host;
1131 new = phys == 0 ? 1 : 0;
1132 if (new || unwritten)
1133 should_zero = 1;
1135 if (new) {
1136 u32 tmp_pos;
1139 * This is safe to call with the page locks - it won't take
1140 * any additional semaphores or cluster locks.
1142 tmp_pos = cpos;
1143 ret = ocfs2_do_extend_allocation(OCFS2_SB(inode->i_sb), inode,
1144 &tmp_pos, 1, 0, wc->w_di_bh,
1145 wc->w_handle, data_ac,
1146 meta_ac, NULL);
1148 * This shouldn't happen because we must have already
1149 * calculated the correct meta data allocation required. The
1150 * internal tree allocation code should know how to increase
1151 * transaction credits itself.
1153 * If need be, we could handle -EAGAIN for a
1154 * RESTART_TRANS here.
1156 mlog_bug_on_msg(ret == -EAGAIN,
1157 "Inode %llu: EAGAIN return during allocation.\n",
1158 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1159 if (ret < 0) {
1160 mlog_errno(ret);
1161 goto out;
1163 } else if (unwritten) {
1164 ret = ocfs2_mark_extent_written(inode, wc->w_di_bh,
1165 wc->w_handle, cpos, 1, phys,
1166 meta_ac, &wc->w_dealloc);
1167 if (ret < 0) {
1168 mlog_errno(ret);
1169 goto out;
1173 if (should_zero)
1174 v_blkno = ocfs2_clusters_to_blocks(inode->i_sb, cpos);
1175 else
1176 v_blkno = user_pos >> inode->i_sb->s_blocksize_bits;
1179 * The only reason this should fail is due to an inability to
1180 * find the extent added.
1182 ret = ocfs2_extent_map_get_blocks(inode, v_blkno, &p_blkno, NULL,
1183 NULL);
1184 if (ret < 0) {
1185 ocfs2_error(inode->i_sb, "Corrupting extend for inode %llu, "
1186 "at logical block %llu",
1187 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1188 (unsigned long long)v_blkno);
1189 goto out;
1192 BUG_ON(p_blkno == 0);
1194 for(i = 0; i < wc->w_num_pages; i++) {
1195 int tmpret;
1197 tmpret = ocfs2_prepare_page_for_write(inode, &p_blkno, wc,
1198 wc->w_pages[i], cpos,
1199 user_pos, user_len,
1200 should_zero);
1201 if (tmpret) {
1202 mlog_errno(tmpret);
1203 if (ret == 0)
1204 tmpret = ret;
1209 * We only have cleanup to do in case of allocating write.
1211 if (ret && new)
1212 ocfs2_write_failure(inode, wc, user_pos, user_len);
1214 out:
1216 return ret;
1219 static int ocfs2_write_cluster_by_desc(struct address_space *mapping,
1220 struct ocfs2_alloc_context *data_ac,
1221 struct ocfs2_alloc_context *meta_ac,
1222 struct ocfs2_write_ctxt *wc,
1223 loff_t pos, unsigned len)
1225 int ret, i;
1226 loff_t cluster_off;
1227 unsigned int local_len = len;
1228 struct ocfs2_write_cluster_desc *desc;
1229 struct ocfs2_super *osb = OCFS2_SB(mapping->host->i_sb);
1231 for (i = 0; i < wc->w_clen; i++) {
1232 desc = &wc->w_desc[i];
1235 * We have to make sure that the total write passed in
1236 * doesn't extend past a single cluster.
1238 local_len = len;
1239 cluster_off = pos & (osb->s_clustersize - 1);
1240 if ((cluster_off + local_len) > osb->s_clustersize)
1241 local_len = osb->s_clustersize - cluster_off;
1243 ret = ocfs2_write_cluster(mapping, desc->c_phys,
1244 desc->c_unwritten, data_ac, meta_ac,
1245 wc, desc->c_cpos, pos, local_len);
1246 if (ret) {
1247 mlog_errno(ret);
1248 goto out;
1251 len -= local_len;
1252 pos += local_len;
1255 ret = 0;
1256 out:
1257 return ret;
1261 * ocfs2_write_end() wants to know which parts of the target page it
1262 * should complete the write on. It's easiest to compute them ahead of
1263 * time when a more complete view of the write is available.
1265 static void ocfs2_set_target_boundaries(struct ocfs2_super *osb,
1266 struct ocfs2_write_ctxt *wc,
1267 loff_t pos, unsigned len, int alloc)
1269 struct ocfs2_write_cluster_desc *desc;
1271 wc->w_target_from = pos & (PAGE_CACHE_SIZE - 1);
1272 wc->w_target_to = wc->w_target_from + len;
1274 if (alloc == 0)
1275 return;
1278 * Allocating write - we may have different boundaries based
1279 * on page size and cluster size.
1281 * NOTE: We can no longer compute one value from the other as
1282 * the actual write length and user provided length may be
1283 * different.
1286 if (wc->w_large_pages) {
1288 * We only care about the 1st and last cluster within
1289 * our range and whether they should be zero'd or not. Either
1290 * value may be extended out to the start/end of a
1291 * newly allocated cluster.
1293 desc = &wc->w_desc[0];
1294 if (ocfs2_should_zero_cluster(desc))
1295 ocfs2_figure_cluster_boundaries(osb,
1296 desc->c_cpos,
1297 &wc->w_target_from,
1298 NULL);
1300 desc = &wc->w_desc[wc->w_clen - 1];
1301 if (ocfs2_should_zero_cluster(desc))
1302 ocfs2_figure_cluster_boundaries(osb,
1303 desc->c_cpos,
1304 NULL,
1305 &wc->w_target_to);
1306 } else {
1307 wc->w_target_from = 0;
1308 wc->w_target_to = PAGE_CACHE_SIZE;
1313 * Populate each single-cluster write descriptor in the write context
1314 * with information about the i/o to be done.
1316 * Returns the number of clusters that will have to be allocated, as
1317 * well as a worst case estimate of the number of extent records that
1318 * would have to be created during a write to an unwritten region.
1320 static int ocfs2_populate_write_desc(struct inode *inode,
1321 struct ocfs2_write_ctxt *wc,
1322 unsigned int *clusters_to_alloc,
1323 unsigned int *extents_to_split)
1325 int ret;
1326 struct ocfs2_write_cluster_desc *desc;
1327 unsigned int num_clusters = 0;
1328 unsigned int ext_flags = 0;
1329 u32 phys = 0;
1330 int i;
1332 *clusters_to_alloc = 0;
1333 *extents_to_split = 0;
1335 for (i = 0; i < wc->w_clen; i++) {
1336 desc = &wc->w_desc[i];
1337 desc->c_cpos = wc->w_cpos + i;
1339 if (num_clusters == 0) {
1341 * Need to look up the next extent record.
1343 ret = ocfs2_get_clusters(inode, desc->c_cpos, &phys,
1344 &num_clusters, &ext_flags);
1345 if (ret) {
1346 mlog_errno(ret);
1347 goto out;
1351 * Assume worst case - that we're writing in
1352 * the middle of the extent.
1354 * We can assume that the write proceeds from
1355 * left to right, in which case the extent
1356 * insert code is smart enough to coalesce the
1357 * next splits into the previous records created.
1359 if (ext_flags & OCFS2_EXT_UNWRITTEN)
1360 *extents_to_split = *extents_to_split + 2;
1361 } else if (phys) {
1363 * Only increment phys if it doesn't describe
1364 * a hole.
1366 phys++;
1369 desc->c_phys = phys;
1370 if (phys == 0) {
1371 desc->c_new = 1;
1372 *clusters_to_alloc = *clusters_to_alloc + 1;
1374 if (ext_flags & OCFS2_EXT_UNWRITTEN)
1375 desc->c_unwritten = 1;
1377 num_clusters--;
1380 ret = 0;
1381 out:
1382 return ret;
1385 int ocfs2_write_begin_nolock(struct address_space *mapping,
1386 loff_t pos, unsigned len, unsigned flags,
1387 struct page **pagep, void **fsdata,
1388 struct buffer_head *di_bh, struct page *mmap_page)
1390 int ret, credits = OCFS2_INODE_UPDATE_CREDITS;
1391 unsigned int clusters_to_alloc, extents_to_split;
1392 struct ocfs2_write_ctxt *wc;
1393 struct inode *inode = mapping->host;
1394 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1395 struct ocfs2_dinode *di;
1396 struct ocfs2_alloc_context *data_ac = NULL;
1397 struct ocfs2_alloc_context *meta_ac = NULL;
1398 handle_t *handle;
1400 ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, di_bh);
1401 if (ret) {
1402 mlog_errno(ret);
1403 return ret;
1406 ret = ocfs2_populate_write_desc(inode, wc, &clusters_to_alloc,
1407 &extents_to_split);
1408 if (ret) {
1409 mlog_errno(ret);
1410 goto out;
1413 di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
1416 * We set w_target_from, w_target_to here so that
1417 * ocfs2_write_end() knows which range in the target page to
1418 * write out. An allocation requires that we write the entire
1419 * cluster range.
1421 if (clusters_to_alloc || extents_to_split) {
1423 * XXX: We are stretching the limits of
1424 * ocfs2_lock_allocators(). It greatly over-estimates
1425 * the work to be done.
1427 ret = ocfs2_lock_allocators(inode, di, clusters_to_alloc,
1428 extents_to_split, &data_ac, &meta_ac);
1429 if (ret) {
1430 mlog_errno(ret);
1431 goto out;
1434 credits = ocfs2_calc_extend_credits(inode->i_sb, di,
1435 clusters_to_alloc);
1439 ocfs2_set_target_boundaries(osb, wc, pos, len,
1440 clusters_to_alloc + extents_to_split);
1442 handle = ocfs2_start_trans(osb, credits);
1443 if (IS_ERR(handle)) {
1444 ret = PTR_ERR(handle);
1445 mlog_errno(ret);
1446 goto out;
1449 wc->w_handle = handle;
1452 * We don't want this to fail in ocfs2_write_end(), so do it
1453 * here.
1455 ret = ocfs2_journal_access(handle, inode, wc->w_di_bh,
1456 OCFS2_JOURNAL_ACCESS_WRITE);
1457 if (ret) {
1458 mlog_errno(ret);
1459 goto out_commit;
1463 * Fill our page array first. That way we've grabbed enough so
1464 * that we can zero and flush if we error after adding the
1465 * extent.
1467 ret = ocfs2_grab_pages_for_write(mapping, wc, wc->w_cpos, pos,
1468 clusters_to_alloc + extents_to_split,
1469 mmap_page);
1470 if (ret) {
1471 mlog_errno(ret);
1472 goto out_commit;
1475 ret = ocfs2_write_cluster_by_desc(mapping, data_ac, meta_ac, wc, pos,
1476 len);
1477 if (ret) {
1478 mlog_errno(ret);
1479 goto out_commit;
1482 if (data_ac)
1483 ocfs2_free_alloc_context(data_ac);
1484 if (meta_ac)
1485 ocfs2_free_alloc_context(meta_ac);
1487 *pagep = wc->w_target_page;
1488 *fsdata = wc;
1489 return 0;
1490 out_commit:
1491 ocfs2_commit_trans(osb, handle);
1493 out:
1494 ocfs2_free_write_ctxt(wc);
1496 if (data_ac)
1497 ocfs2_free_alloc_context(data_ac);
1498 if (meta_ac)
1499 ocfs2_free_alloc_context(meta_ac);
1500 return ret;
1503 int ocfs2_write_begin(struct file *file, struct address_space *mapping,
1504 loff_t pos, unsigned len, unsigned flags,
1505 struct page **pagep, void **fsdata)
1507 int ret;
1508 struct buffer_head *di_bh = NULL;
1509 struct inode *inode = mapping->host;
1511 ret = ocfs2_meta_lock(inode, &di_bh, 1);
1512 if (ret) {
1513 mlog_errno(ret);
1514 return ret;
1518 * Take alloc sem here to prevent concurrent lookups. That way
1519 * the mapping, zeroing and tree manipulation within
1520 * ocfs2_write() will be safe against ->readpage(). This
1521 * should also serve to lock out allocation from a shared
1522 * writeable region.
1524 down_write(&OCFS2_I(inode)->ip_alloc_sem);
1526 ret = ocfs2_data_lock(inode, 1);
1527 if (ret) {
1528 mlog_errno(ret);
1529 goto out_fail;
1532 ret = ocfs2_write_begin_nolock(mapping, pos, len, flags, pagep,
1533 fsdata, di_bh, NULL);
1534 if (ret) {
1535 mlog_errno(ret);
1536 goto out_fail_data;
1539 brelse(di_bh);
1541 return 0;
1543 out_fail_data:
1544 ocfs2_data_unlock(inode, 1);
1545 out_fail:
1546 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1548 brelse(di_bh);
1549 ocfs2_meta_unlock(inode, 1);
1551 return ret;
1554 int ocfs2_write_end_nolock(struct address_space *mapping,
1555 loff_t pos, unsigned len, unsigned copied,
1556 struct page *page, void *fsdata)
1558 int i;
1559 unsigned from, to, start = pos & (PAGE_CACHE_SIZE - 1);
1560 struct inode *inode = mapping->host;
1561 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1562 struct ocfs2_write_ctxt *wc = fsdata;
1563 struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
1564 handle_t *handle = wc->w_handle;
1565 struct page *tmppage;
1567 if (unlikely(copied < len)) {
1568 if (!PageUptodate(wc->w_target_page))
1569 copied = 0;
1571 ocfs2_zero_new_buffers(wc->w_target_page, start+copied,
1572 start+len);
1574 flush_dcache_page(wc->w_target_page);
1576 for(i = 0; i < wc->w_num_pages; i++) {
1577 tmppage = wc->w_pages[i];
1579 if (tmppage == wc->w_target_page) {
1580 from = wc->w_target_from;
1581 to = wc->w_target_to;
1583 BUG_ON(from > PAGE_CACHE_SIZE ||
1584 to > PAGE_CACHE_SIZE ||
1585 to < from);
1586 } else {
1588 * Pages adjacent to the target (if any) imply
1589 * a hole-filling write in which case we want
1590 * to flush their entire range.
1592 from = 0;
1593 to = PAGE_CACHE_SIZE;
1596 if (ocfs2_should_order_data(inode))
1597 walk_page_buffers(wc->w_handle, page_buffers(tmppage),
1598 from, to, NULL,
1599 ocfs2_journal_dirty_data);
1601 block_commit_write(tmppage, from, to);
1604 pos += copied;
1605 if (pos > inode->i_size) {
1606 i_size_write(inode, pos);
1607 mark_inode_dirty(inode);
1609 inode->i_blocks = ocfs2_inode_sector_count(inode);
1610 di->i_size = cpu_to_le64((u64)i_size_read(inode));
1611 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1612 di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
1613 di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
1614 ocfs2_journal_dirty(handle, wc->w_di_bh);
1616 ocfs2_commit_trans(osb, handle);
1618 ocfs2_run_deallocs(osb, &wc->w_dealloc);
1620 ocfs2_free_write_ctxt(wc);
1622 return copied;
1625 int ocfs2_write_end(struct file *file, struct address_space *mapping,
1626 loff_t pos, unsigned len, unsigned copied,
1627 struct page *page, void *fsdata)
1629 int ret;
1630 struct inode *inode = mapping->host;
1632 ret = ocfs2_write_end_nolock(mapping, pos, len, copied, page, fsdata);
1634 ocfs2_data_unlock(inode, 1);
1635 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1636 ocfs2_meta_unlock(inode, 1);
1638 return ret;
1641 const struct address_space_operations ocfs2_aops = {
1642 .readpage = ocfs2_readpage,
1643 .writepage = ocfs2_writepage,
1644 .bmap = ocfs2_bmap,
1645 .sync_page = block_sync_page,
1646 .direct_IO = ocfs2_direct_IO,
1647 .invalidatepage = ocfs2_invalidatepage,
1648 .releasepage = ocfs2_releasepage,
1649 .migratepage = buffer_migrate_page,