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.
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>
38 #include "extent_map.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
)
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
);
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
);
70 status
= ocfs2_read_block(OCFS2_SB(inode
->i_sb
),
71 OCFS2_I(inode
)->ip_blkno
,
72 &bh
, OCFS2_BH_CACHED
, inode
);
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,
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
);
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
) +
98 buffer_cache_bh
= sb_getblk(osb
->sb
, blkno
);
99 if (!buffer_cache_bh
) {
100 mlog(ML_ERROR
, "couldn't getblock for symlink!\n");
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
);
112 mlog(ML_ERROR
, "couldn't kmap!\n");
115 memcpy(kaddr
+ (bh_result
->b_size
* iblock
),
116 buffer_cache_bh
->b_data
,
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
);
137 static int ocfs2_get_block(struct inode
*inode
, sector_t iblock
,
138 struct buffer_head
*bh_result
, int create
)
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
);
158 err
= ocfs2_extent_map_get_blocks(inode
, iblock
, &p_blkno
, NULL
,
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
);
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
)) {
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
);
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
);
209 int ocfs2_read_inline_data(struct inode
*inode
, struct page
*page
,
210 struct buffer_head
*di_bh
)
214 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
216 if (!(le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_DATA_FL
)) {
217 ocfs2_error(inode
->i_sb
, "Inode %llu lost inline data flag",
218 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
222 size
= i_size_read(inode
);
224 if (size
> PAGE_CACHE_SIZE
||
225 size
> ocfs2_max_inline_data(inode
->i_sb
)) {
226 ocfs2_error(inode
->i_sb
,
227 "Inode %llu has with inline data has bad size: %u",
228 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, size
);
232 kaddr
= kmap_atomic(page
, KM_USER0
);
234 memcpy(kaddr
, di
->id2
.i_data
.id_data
, size
);
235 /* Clear the remaining part of the page */
236 memset(kaddr
+ size
, 0, PAGE_CACHE_SIZE
- size
);
237 flush_dcache_page(page
);
238 kunmap_atomic(kaddr
, KM_USER0
);
240 SetPageUptodate(page
);
245 static int ocfs2_readpage_inline(struct inode
*inode
, struct page
*page
)
248 struct buffer_head
*di_bh
= NULL
;
249 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
251 BUG_ON(!PageLocked(page
));
252 BUG_ON(!OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
);
254 ret
= ocfs2_read_block(osb
, OCFS2_I(inode
)->ip_blkno
, &di_bh
,
255 OCFS2_BH_CACHED
, inode
);
261 ret
= ocfs2_read_inline_data(inode
, page
, di_bh
);
269 static int ocfs2_readpage(struct file
*file
, struct page
*page
)
271 struct inode
*inode
= page
->mapping
->host
;
272 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
273 loff_t start
= (loff_t
)page
->index
<< PAGE_CACHE_SHIFT
;
276 mlog_entry("(0x%p, %lu)\n", file
, (page
? page
->index
: 0));
278 ret
= ocfs2_inode_lock_with_page(inode
, NULL
, 0, page
);
280 if (ret
== AOP_TRUNCATED_PAGE
)
286 if (down_read_trylock(&oi
->ip_alloc_sem
) == 0) {
287 ret
= AOP_TRUNCATED_PAGE
;
288 goto out_inode_unlock
;
292 * i_size might have just been updated as we grabed the meta lock. We
293 * might now be discovering a truncate that hit on another node.
294 * block_read_full_page->get_block freaks out if it is asked to read
295 * beyond the end of a file, so we check here. Callers
296 * (generic_file_read, vm_ops->fault) are clever enough to check i_size
297 * and notice that the page they just read isn't needed.
299 * XXX sys_readahead() seems to get that wrong?
301 if (start
>= i_size_read(inode
)) {
302 zero_user_page(page
, 0, PAGE_SIZE
, KM_USER0
);
303 SetPageUptodate(page
);
308 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
309 ret
= ocfs2_readpage_inline(inode
, page
);
311 ret
= block_read_full_page(page
, ocfs2_get_block
);
315 up_read(&OCFS2_I(inode
)->ip_alloc_sem
);
317 ocfs2_inode_unlock(inode
, 0);
325 /* Note: Because we don't support holes, our allocation has
326 * already happened (allocation writes zeros to the file data)
327 * so we don't have to worry about ordered writes in
330 * ->writepage is called during the process of invalidating the page cache
331 * during blocked lock processing. It can't block on any cluster locks
332 * to during block mapping. It's relying on the fact that the block
333 * mapping can't have disappeared under the dirty pages that it is
334 * being asked to write back.
336 static int ocfs2_writepage(struct page
*page
, struct writeback_control
*wbc
)
340 mlog_entry("(0x%p)\n", page
);
342 ret
= block_write_full_page(page
, ocfs2_get_block
, wbc
);
350 * This is called from ocfs2_write_zero_page() which has handled it's
351 * own cluster locking and has ensured allocation exists for those
352 * blocks to be written.
354 int ocfs2_prepare_write_nolock(struct inode
*inode
, struct page
*page
,
355 unsigned from
, unsigned to
)
359 ret
= block_prepare_write(page
, from
, to
, ocfs2_get_block
);
364 /* Taken from ext3. We don't necessarily need the full blown
365 * functionality yet, but IMHO it's better to cut and paste the whole
366 * thing so we can avoid introducing our own bugs (and easily pick up
367 * their fixes when they happen) --Mark */
368 int walk_page_buffers( handle_t
*handle
,
369 struct buffer_head
*head
,
373 int (*fn
)( handle_t
*handle
,
374 struct buffer_head
*bh
))
376 struct buffer_head
*bh
;
377 unsigned block_start
, block_end
;
378 unsigned blocksize
= head
->b_size
;
380 struct buffer_head
*next
;
382 for ( bh
= head
, block_start
= 0;
383 ret
== 0 && (bh
!= head
|| !block_start
);
384 block_start
= block_end
, bh
= next
)
386 next
= bh
->b_this_page
;
387 block_end
= block_start
+ blocksize
;
388 if (block_end
<= from
|| block_start
>= to
) {
389 if (partial
&& !buffer_uptodate(bh
))
393 err
= (*fn
)(handle
, bh
);
400 handle_t
*ocfs2_start_walk_page_trans(struct inode
*inode
,
405 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
406 handle_t
*handle
= NULL
;
409 handle
= ocfs2_start_trans(osb
, OCFS2_INODE_UPDATE_CREDITS
);
416 if (ocfs2_should_order_data(inode
)) {
417 ret
= walk_page_buffers(handle
,
420 ocfs2_journal_dirty_data
);
427 ocfs2_commit_trans(osb
, handle
);
428 handle
= ERR_PTR(ret
);
433 static sector_t
ocfs2_bmap(struct address_space
*mapping
, sector_t block
)
438 struct inode
*inode
= mapping
->host
;
440 mlog_entry("(block = %llu)\n", (unsigned long long)block
);
442 /* We don't need to lock journal system files, since they aren't
443 * accessed concurrently from multiple nodes.
445 if (!INODE_JOURNAL(inode
)) {
446 err
= ocfs2_inode_lock(inode
, NULL
, 0);
452 down_read(&OCFS2_I(inode
)->ip_alloc_sem
);
455 if (!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
))
456 err
= ocfs2_extent_map_get_blocks(inode
, block
, &p_blkno
, NULL
,
459 if (!INODE_JOURNAL(inode
)) {
460 up_read(&OCFS2_I(inode
)->ip_alloc_sem
);
461 ocfs2_inode_unlock(inode
, 0);
465 mlog(ML_ERROR
, "get_blocks() failed, block = %llu\n",
466 (unsigned long long)block
);
472 status
= err
? 0 : p_blkno
;
474 mlog_exit((int)status
);
480 * TODO: Make this into a generic get_blocks function.
482 * From do_direct_io in direct-io.c:
483 * "So what we do is to permit the ->get_blocks function to populate
484 * bh.b_size with the size of IO which is permitted at this offset and
487 * This function is called directly from get_more_blocks in direct-io.c.
489 * called like this: dio->get_blocks(dio->inode, fs_startblk,
490 * fs_count, map_bh, dio->rw == WRITE);
492 static int ocfs2_direct_IO_get_blocks(struct inode
*inode
, sector_t iblock
,
493 struct buffer_head
*bh_result
, int create
)
496 u64 p_blkno
, inode_blocks
, contig_blocks
;
497 unsigned int ext_flags
;
498 unsigned char blocksize_bits
= inode
->i_sb
->s_blocksize_bits
;
499 unsigned long max_blocks
= bh_result
->b_size
>> inode
->i_blkbits
;
501 /* This function won't even be called if the request isn't all
502 * nicely aligned and of the right size, so there's no need
503 * for us to check any of that. */
505 inode_blocks
= ocfs2_blocks_for_bytes(inode
->i_sb
, i_size_read(inode
));
508 * Any write past EOF is not allowed because we'd be extending.
510 if (create
&& (iblock
+ max_blocks
) > inode_blocks
) {
515 /* This figures out the size of the next contiguous block, and
516 * our logical offset */
517 ret
= ocfs2_extent_map_get_blocks(inode
, iblock
, &p_blkno
,
518 &contig_blocks
, &ext_flags
);
520 mlog(ML_ERROR
, "get_blocks() failed iblock=%llu\n",
521 (unsigned long long)iblock
);
526 if (!ocfs2_sparse_alloc(OCFS2_SB(inode
->i_sb
)) && !p_blkno
) {
527 ocfs2_error(inode
->i_sb
,
528 "Inode %llu has a hole at block %llu\n",
529 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
530 (unsigned long long)iblock
);
536 * get_more_blocks() expects us to describe a hole by clearing
537 * the mapped bit on bh_result().
539 * Consider an unwritten extent as a hole.
541 if (p_blkno
&& !(ext_flags
& OCFS2_EXT_UNWRITTEN
))
542 map_bh(bh_result
, inode
->i_sb
, p_blkno
);
545 * ocfs2_prepare_inode_for_write() should have caught
546 * the case where we'd be filling a hole and triggered
547 * a buffered write instead.
555 clear_buffer_mapped(bh_result
);
558 /* make sure we don't map more than max_blocks blocks here as
559 that's all the kernel will handle at this point. */
560 if (max_blocks
< contig_blocks
)
561 contig_blocks
= max_blocks
;
562 bh_result
->b_size
= contig_blocks
<< blocksize_bits
;
568 * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're
569 * particularly interested in the aio/dio case. Like the core uses
570 * i_alloc_sem, we use the rw_lock DLM lock to protect io on one node from
571 * truncation on another.
573 static void ocfs2_dio_end_io(struct kiocb
*iocb
,
578 struct inode
*inode
= iocb
->ki_filp
->f_path
.dentry
->d_inode
;
581 /* this io's submitter should not have unlocked this before we could */
582 BUG_ON(!ocfs2_iocb_is_rw_locked(iocb
));
584 ocfs2_iocb_clear_rw_locked(iocb
);
586 level
= ocfs2_iocb_rw_locked_level(iocb
);
588 up_read(&inode
->i_alloc_sem
);
589 ocfs2_rw_unlock(inode
, level
);
593 * ocfs2_invalidatepage() and ocfs2_releasepage() are shamelessly stolen
594 * from ext3. PageChecked() bits have been removed as OCFS2 does not
595 * do journalled data.
597 static void ocfs2_invalidatepage(struct page
*page
, unsigned long offset
)
599 journal_t
*journal
= OCFS2_SB(page
->mapping
->host
->i_sb
)->journal
->j_journal
;
601 journal_invalidatepage(journal
, page
, offset
);
604 static int ocfs2_releasepage(struct page
*page
, gfp_t wait
)
606 journal_t
*journal
= OCFS2_SB(page
->mapping
->host
->i_sb
)->journal
->j_journal
;
608 if (!page_has_buffers(page
))
610 return journal_try_to_free_buffers(journal
, page
, wait
);
613 static ssize_t
ocfs2_direct_IO(int rw
,
615 const struct iovec
*iov
,
617 unsigned long nr_segs
)
619 struct file
*file
= iocb
->ki_filp
;
620 struct inode
*inode
= file
->f_path
.dentry
->d_inode
->i_mapping
->host
;
626 * Fallback to buffered I/O if we see an inode without
629 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
)
632 ret
= blockdev_direct_IO_no_locking(rw
, iocb
, inode
,
633 inode
->i_sb
->s_bdev
, iov
, offset
,
635 ocfs2_direct_IO_get_blocks
,
642 static void ocfs2_figure_cluster_boundaries(struct ocfs2_super
*osb
,
647 unsigned int cluster_start
= 0, cluster_end
= PAGE_CACHE_SIZE
;
649 if (unlikely(PAGE_CACHE_SHIFT
> osb
->s_clustersize_bits
)) {
652 cpp
= 1 << (PAGE_CACHE_SHIFT
- osb
->s_clustersize_bits
);
654 cluster_start
= cpos
% cpp
;
655 cluster_start
= cluster_start
<< osb
->s_clustersize_bits
;
657 cluster_end
= cluster_start
+ osb
->s_clustersize
;
660 BUG_ON(cluster_start
> PAGE_SIZE
);
661 BUG_ON(cluster_end
> PAGE_SIZE
);
664 *start
= cluster_start
;
670 * 'from' and 'to' are the region in the page to avoid zeroing.
672 * If pagesize > clustersize, this function will avoid zeroing outside
673 * of the cluster boundary.
675 * from == to == 0 is code for "zero the entire cluster region"
677 static void ocfs2_clear_page_regions(struct page
*page
,
678 struct ocfs2_super
*osb
, u32 cpos
,
679 unsigned from
, unsigned to
)
682 unsigned int cluster_start
, cluster_end
;
684 ocfs2_figure_cluster_boundaries(osb
, cpos
, &cluster_start
, &cluster_end
);
686 kaddr
= kmap_atomic(page
, KM_USER0
);
689 if (from
> cluster_start
)
690 memset(kaddr
+ cluster_start
, 0, from
- cluster_start
);
691 if (to
< cluster_end
)
692 memset(kaddr
+ to
, 0, cluster_end
- to
);
694 memset(kaddr
+ cluster_start
, 0, cluster_end
- cluster_start
);
697 kunmap_atomic(kaddr
, KM_USER0
);
701 * Nonsparse file systems fully allocate before we get to the write
702 * code. This prevents ocfs2_write() from tagging the write as an
703 * allocating one, which means ocfs2_map_page_blocks() might try to
704 * read-in the blocks at the tail of our file. Avoid reading them by
705 * testing i_size against each block offset.
707 static int ocfs2_should_read_blk(struct inode
*inode
, struct page
*page
,
708 unsigned int block_start
)
710 u64 offset
= page_offset(page
) + block_start
;
712 if (ocfs2_sparse_alloc(OCFS2_SB(inode
->i_sb
)))
715 if (i_size_read(inode
) > offset
)
722 * Some of this taken from block_prepare_write(). We already have our
723 * mapping by now though, and the entire write will be allocating or
724 * it won't, so not much need to use BH_New.
726 * This will also skip zeroing, which is handled externally.
728 int ocfs2_map_page_blocks(struct page
*page
, u64
*p_blkno
,
729 struct inode
*inode
, unsigned int from
,
730 unsigned int to
, int new)
733 struct buffer_head
*head
, *bh
, *wait
[2], **wait_bh
= wait
;
734 unsigned int block_end
, block_start
;
735 unsigned int bsize
= 1 << inode
->i_blkbits
;
737 if (!page_has_buffers(page
))
738 create_empty_buffers(page
, bsize
, 0);
740 head
= page_buffers(page
);
741 for (bh
= head
, block_start
= 0; bh
!= head
|| !block_start
;
742 bh
= bh
->b_this_page
, block_start
+= bsize
) {
743 block_end
= block_start
+ bsize
;
745 clear_buffer_new(bh
);
748 * Ignore blocks outside of our i/o range -
749 * they may belong to unallocated clusters.
751 if (block_start
>= to
|| block_end
<= from
) {
752 if (PageUptodate(page
))
753 set_buffer_uptodate(bh
);
758 * For an allocating write with cluster size >= page
759 * size, we always write the entire page.
764 if (!buffer_mapped(bh
)) {
765 map_bh(bh
, inode
->i_sb
, *p_blkno
);
766 unmap_underlying_metadata(bh
->b_bdev
, bh
->b_blocknr
);
769 if (PageUptodate(page
)) {
770 if (!buffer_uptodate(bh
))
771 set_buffer_uptodate(bh
);
772 } else if (!buffer_uptodate(bh
) && !buffer_delay(bh
) &&
774 ocfs2_should_read_blk(inode
, page
, block_start
) &&
775 (block_start
< from
|| block_end
> to
)) {
776 ll_rw_block(READ
, 1, &bh
);
780 *p_blkno
= *p_blkno
+ 1;
784 * If we issued read requests - let them complete.
786 while(wait_bh
> wait
) {
787 wait_on_buffer(*--wait_bh
);
788 if (!buffer_uptodate(*wait_bh
))
792 if (ret
== 0 || !new)
796 * If we get -EIO above, zero out any newly allocated blocks
797 * to avoid exposing stale data.
802 block_end
= block_start
+ bsize
;
803 if (block_end
<= from
)
805 if (block_start
>= to
)
808 zero_user_page(page
, block_start
, bh
->b_size
, KM_USER0
);
809 set_buffer_uptodate(bh
);
810 mark_buffer_dirty(bh
);
813 block_start
= block_end
;
814 bh
= bh
->b_this_page
;
815 } while (bh
!= head
);
820 #if (PAGE_CACHE_SIZE >= OCFS2_MAX_CLUSTERSIZE)
821 #define OCFS2_MAX_CTXT_PAGES 1
823 #define OCFS2_MAX_CTXT_PAGES (OCFS2_MAX_CLUSTERSIZE / PAGE_CACHE_SIZE)
826 #define OCFS2_MAX_CLUSTERS_PER_PAGE (PAGE_CACHE_SIZE / OCFS2_MIN_CLUSTERSIZE)
829 * Describe the state of a single cluster to be written to.
831 struct ocfs2_write_cluster_desc
{
835 * Give this a unique field because c_phys eventually gets
839 unsigned c_unwritten
;
842 static inline int ocfs2_should_zero_cluster(struct ocfs2_write_cluster_desc
*d
)
844 return d
->c_new
|| d
->c_unwritten
;
847 struct ocfs2_write_ctxt
{
848 /* Logical cluster position / len of write */
852 struct ocfs2_write_cluster_desc w_desc
[OCFS2_MAX_CLUSTERS_PER_PAGE
];
855 * This is true if page_size > cluster_size.
857 * It triggers a set of special cases during write which might
858 * have to deal with allocating writes to partial pages.
860 unsigned int w_large_pages
;
863 * Pages involved in this write.
865 * w_target_page is the page being written to by the user.
867 * w_pages is an array of pages which always contains
868 * w_target_page, and in the case of an allocating write with
869 * page_size < cluster size, it will contain zero'd and mapped
870 * pages adjacent to w_target_page which need to be written
871 * out in so that future reads from that region will get
874 struct page
*w_pages
[OCFS2_MAX_CTXT_PAGES
];
875 unsigned int w_num_pages
;
876 struct page
*w_target_page
;
879 * ocfs2_write_end() uses this to know what the real range to
880 * write in the target should be.
882 unsigned int w_target_from
;
883 unsigned int w_target_to
;
886 * We could use journal_current_handle() but this is cleaner,
891 struct buffer_head
*w_di_bh
;
893 struct ocfs2_cached_dealloc_ctxt w_dealloc
;
896 void ocfs2_unlock_and_free_pages(struct page
**pages
, int num_pages
)
900 for(i
= 0; i
< num_pages
; i
++) {
902 unlock_page(pages
[i
]);
903 mark_page_accessed(pages
[i
]);
904 page_cache_release(pages
[i
]);
909 static void ocfs2_free_write_ctxt(struct ocfs2_write_ctxt
*wc
)
911 ocfs2_unlock_and_free_pages(wc
->w_pages
, wc
->w_num_pages
);
917 static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt
**wcp
,
918 struct ocfs2_super
*osb
, loff_t pos
,
919 unsigned len
, struct buffer_head
*di_bh
)
922 struct ocfs2_write_ctxt
*wc
;
924 wc
= kzalloc(sizeof(struct ocfs2_write_ctxt
), GFP_NOFS
);
928 wc
->w_cpos
= pos
>> osb
->s_clustersize_bits
;
929 cend
= (pos
+ len
- 1) >> osb
->s_clustersize_bits
;
930 wc
->w_clen
= cend
- wc
->w_cpos
+ 1;
934 if (unlikely(PAGE_CACHE_SHIFT
> osb
->s_clustersize_bits
))
935 wc
->w_large_pages
= 1;
937 wc
->w_large_pages
= 0;
939 ocfs2_init_dealloc_ctxt(&wc
->w_dealloc
);
947 * If a page has any new buffers, zero them out here, and mark them uptodate
948 * and dirty so they'll be written out (in order to prevent uninitialised
949 * block data from leaking). And clear the new bit.
951 static void ocfs2_zero_new_buffers(struct page
*page
, unsigned from
, unsigned to
)
953 unsigned int block_start
, block_end
;
954 struct buffer_head
*head
, *bh
;
956 BUG_ON(!PageLocked(page
));
957 if (!page_has_buffers(page
))
960 bh
= head
= page_buffers(page
);
963 block_end
= block_start
+ bh
->b_size
;
965 if (buffer_new(bh
)) {
966 if (block_end
> from
&& block_start
< to
) {
967 if (!PageUptodate(page
)) {
970 start
= max(from
, block_start
);
971 end
= min(to
, block_end
);
973 zero_user_page(page
, start
, end
- start
, KM_USER0
);
974 set_buffer_uptodate(bh
);
977 clear_buffer_new(bh
);
978 mark_buffer_dirty(bh
);
982 block_start
= block_end
;
983 bh
= bh
->b_this_page
;
984 } while (bh
!= head
);
988 * Only called when we have a failure during allocating write to write
989 * zero's to the newly allocated region.
991 static void ocfs2_write_failure(struct inode
*inode
,
992 struct ocfs2_write_ctxt
*wc
,
993 loff_t user_pos
, unsigned user_len
)
996 unsigned from
= user_pos
& (PAGE_CACHE_SIZE
- 1),
997 to
= user_pos
+ user_len
;
998 struct page
*tmppage
;
1000 ocfs2_zero_new_buffers(wc
->w_target_page
, from
, to
);
1002 for(i
= 0; i
< wc
->w_num_pages
; i
++) {
1003 tmppage
= wc
->w_pages
[i
];
1005 if (ocfs2_should_order_data(inode
))
1006 walk_page_buffers(wc
->w_handle
, page_buffers(tmppage
),
1008 ocfs2_journal_dirty_data
);
1010 block_commit_write(tmppage
, from
, to
);
1014 static int ocfs2_prepare_page_for_write(struct inode
*inode
, u64
*p_blkno
,
1015 struct ocfs2_write_ctxt
*wc
,
1016 struct page
*page
, u32 cpos
,
1017 loff_t user_pos
, unsigned user_len
,
1021 unsigned int map_from
= 0, map_to
= 0;
1022 unsigned int cluster_start
, cluster_end
;
1023 unsigned int user_data_from
= 0, user_data_to
= 0;
1025 ocfs2_figure_cluster_boundaries(OCFS2_SB(inode
->i_sb
), cpos
,
1026 &cluster_start
, &cluster_end
);
1028 if (page
== wc
->w_target_page
) {
1029 map_from
= user_pos
& (PAGE_CACHE_SIZE
- 1);
1030 map_to
= map_from
+ user_len
;
1033 ret
= ocfs2_map_page_blocks(page
, p_blkno
, inode
,
1034 cluster_start
, cluster_end
,
1037 ret
= ocfs2_map_page_blocks(page
, p_blkno
, inode
,
1038 map_from
, map_to
, new);
1044 user_data_from
= map_from
;
1045 user_data_to
= map_to
;
1047 map_from
= cluster_start
;
1048 map_to
= cluster_end
;
1052 * If we haven't allocated the new page yet, we
1053 * shouldn't be writing it out without copying user
1054 * data. This is likely a math error from the caller.
1058 map_from
= cluster_start
;
1059 map_to
= cluster_end
;
1061 ret
= ocfs2_map_page_blocks(page
, p_blkno
, inode
,
1062 cluster_start
, cluster_end
, new);
1070 * Parts of newly allocated pages need to be zero'd.
1072 * Above, we have also rewritten 'to' and 'from' - as far as
1073 * the rest of the function is concerned, the entire cluster
1074 * range inside of a page needs to be written.
1076 * We can skip this if the page is up to date - it's already
1077 * been zero'd from being read in as a hole.
1079 if (new && !PageUptodate(page
))
1080 ocfs2_clear_page_regions(page
, OCFS2_SB(inode
->i_sb
),
1081 cpos
, user_data_from
, user_data_to
);
1083 flush_dcache_page(page
);
1090 * This function will only grab one clusters worth of pages.
1092 static int ocfs2_grab_pages_for_write(struct address_space
*mapping
,
1093 struct ocfs2_write_ctxt
*wc
,
1094 u32 cpos
, loff_t user_pos
, int new,
1095 struct page
*mmap_page
)
1098 unsigned long start
, target_index
, index
;
1099 struct inode
*inode
= mapping
->host
;
1101 target_index
= user_pos
>> PAGE_CACHE_SHIFT
;
1104 * Figure out how many pages we'll be manipulating here. For
1105 * non allocating write, we just change the one
1106 * page. Otherwise, we'll need a whole clusters worth.
1109 wc
->w_num_pages
= ocfs2_pages_per_cluster(inode
->i_sb
);
1110 start
= ocfs2_align_clusters_to_page_index(inode
->i_sb
, cpos
);
1112 wc
->w_num_pages
= 1;
1113 start
= target_index
;
1116 for(i
= 0; i
< wc
->w_num_pages
; i
++) {
1119 if (index
== target_index
&& mmap_page
) {
1121 * ocfs2_pagemkwrite() is a little different
1122 * and wants us to directly use the page
1125 lock_page(mmap_page
);
1127 if (mmap_page
->mapping
!= mapping
) {
1128 unlock_page(mmap_page
);
1130 * Sanity check - the locking in
1131 * ocfs2_pagemkwrite() should ensure
1132 * that this code doesn't trigger.
1139 page_cache_get(mmap_page
);
1140 wc
->w_pages
[i
] = mmap_page
;
1142 wc
->w_pages
[i
] = find_or_create_page(mapping
, index
,
1144 if (!wc
->w_pages
[i
]) {
1151 if (index
== target_index
)
1152 wc
->w_target_page
= wc
->w_pages
[i
];
1159 * Prepare a single cluster for write one cluster into the file.
1161 static int ocfs2_write_cluster(struct address_space
*mapping
,
1162 u32 phys
, unsigned int unwritten
,
1163 struct ocfs2_alloc_context
*data_ac
,
1164 struct ocfs2_alloc_context
*meta_ac
,
1165 struct ocfs2_write_ctxt
*wc
, u32 cpos
,
1166 loff_t user_pos
, unsigned user_len
)
1168 int ret
, i
, new, should_zero
= 0;
1169 u64 v_blkno
, p_blkno
;
1170 struct inode
*inode
= mapping
->host
;
1172 new = phys
== 0 ? 1 : 0;
1173 if (new || unwritten
)
1180 * This is safe to call with the page locks - it won't take
1181 * any additional semaphores or cluster locks.
1184 ret
= ocfs2_do_extend_allocation(OCFS2_SB(inode
->i_sb
), inode
,
1185 &tmp_pos
, 1, 0, wc
->w_di_bh
,
1186 wc
->w_handle
, data_ac
,
1189 * This shouldn't happen because we must have already
1190 * calculated the correct meta data allocation required. The
1191 * internal tree allocation code should know how to increase
1192 * transaction credits itself.
1194 * If need be, we could handle -EAGAIN for a
1195 * RESTART_TRANS here.
1197 mlog_bug_on_msg(ret
== -EAGAIN
,
1198 "Inode %llu: EAGAIN return during allocation.\n",
1199 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1204 } else if (unwritten
) {
1205 ret
= ocfs2_mark_extent_written(inode
, wc
->w_di_bh
,
1206 wc
->w_handle
, cpos
, 1, phys
,
1207 meta_ac
, &wc
->w_dealloc
);
1215 v_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, cpos
);
1217 v_blkno
= user_pos
>> inode
->i_sb
->s_blocksize_bits
;
1220 * The only reason this should fail is due to an inability to
1221 * find the extent added.
1223 ret
= ocfs2_extent_map_get_blocks(inode
, v_blkno
, &p_blkno
, NULL
,
1226 ocfs2_error(inode
->i_sb
, "Corrupting extend for inode %llu, "
1227 "at logical block %llu",
1228 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1229 (unsigned long long)v_blkno
);
1233 BUG_ON(p_blkno
== 0);
1235 for(i
= 0; i
< wc
->w_num_pages
; i
++) {
1238 tmpret
= ocfs2_prepare_page_for_write(inode
, &p_blkno
, wc
,
1239 wc
->w_pages
[i
], cpos
,
1250 * We only have cleanup to do in case of allocating write.
1253 ocfs2_write_failure(inode
, wc
, user_pos
, user_len
);
1260 static int ocfs2_write_cluster_by_desc(struct address_space
*mapping
,
1261 struct ocfs2_alloc_context
*data_ac
,
1262 struct ocfs2_alloc_context
*meta_ac
,
1263 struct ocfs2_write_ctxt
*wc
,
1264 loff_t pos
, unsigned len
)
1268 unsigned int local_len
= len
;
1269 struct ocfs2_write_cluster_desc
*desc
;
1270 struct ocfs2_super
*osb
= OCFS2_SB(mapping
->host
->i_sb
);
1272 for (i
= 0; i
< wc
->w_clen
; i
++) {
1273 desc
= &wc
->w_desc
[i
];
1276 * We have to make sure that the total write passed in
1277 * doesn't extend past a single cluster.
1280 cluster_off
= pos
& (osb
->s_clustersize
- 1);
1281 if ((cluster_off
+ local_len
) > osb
->s_clustersize
)
1282 local_len
= osb
->s_clustersize
- cluster_off
;
1284 ret
= ocfs2_write_cluster(mapping
, desc
->c_phys
,
1285 desc
->c_unwritten
, data_ac
, meta_ac
,
1286 wc
, desc
->c_cpos
, pos
, local_len
);
1302 * ocfs2_write_end() wants to know which parts of the target page it
1303 * should complete the write on. It's easiest to compute them ahead of
1304 * time when a more complete view of the write is available.
1306 static void ocfs2_set_target_boundaries(struct ocfs2_super
*osb
,
1307 struct ocfs2_write_ctxt
*wc
,
1308 loff_t pos
, unsigned len
, int alloc
)
1310 struct ocfs2_write_cluster_desc
*desc
;
1312 wc
->w_target_from
= pos
& (PAGE_CACHE_SIZE
- 1);
1313 wc
->w_target_to
= wc
->w_target_from
+ len
;
1319 * Allocating write - we may have different boundaries based
1320 * on page size and cluster size.
1322 * NOTE: We can no longer compute one value from the other as
1323 * the actual write length and user provided length may be
1327 if (wc
->w_large_pages
) {
1329 * We only care about the 1st and last cluster within
1330 * our range and whether they should be zero'd or not. Either
1331 * value may be extended out to the start/end of a
1332 * newly allocated cluster.
1334 desc
= &wc
->w_desc
[0];
1335 if (ocfs2_should_zero_cluster(desc
))
1336 ocfs2_figure_cluster_boundaries(osb
,
1341 desc
= &wc
->w_desc
[wc
->w_clen
- 1];
1342 if (ocfs2_should_zero_cluster(desc
))
1343 ocfs2_figure_cluster_boundaries(osb
,
1348 wc
->w_target_from
= 0;
1349 wc
->w_target_to
= PAGE_CACHE_SIZE
;
1354 * Populate each single-cluster write descriptor in the write context
1355 * with information about the i/o to be done.
1357 * Returns the number of clusters that will have to be allocated, as
1358 * well as a worst case estimate of the number of extent records that
1359 * would have to be created during a write to an unwritten region.
1361 static int ocfs2_populate_write_desc(struct inode
*inode
,
1362 struct ocfs2_write_ctxt
*wc
,
1363 unsigned int *clusters_to_alloc
,
1364 unsigned int *extents_to_split
)
1367 struct ocfs2_write_cluster_desc
*desc
;
1368 unsigned int num_clusters
= 0;
1369 unsigned int ext_flags
= 0;
1373 *clusters_to_alloc
= 0;
1374 *extents_to_split
= 0;
1376 for (i
= 0; i
< wc
->w_clen
; i
++) {
1377 desc
= &wc
->w_desc
[i
];
1378 desc
->c_cpos
= wc
->w_cpos
+ i
;
1380 if (num_clusters
== 0) {
1382 * Need to look up the next extent record.
1384 ret
= ocfs2_get_clusters(inode
, desc
->c_cpos
, &phys
,
1385 &num_clusters
, &ext_flags
);
1392 * Assume worst case - that we're writing in
1393 * the middle of the extent.
1395 * We can assume that the write proceeds from
1396 * left to right, in which case the extent
1397 * insert code is smart enough to coalesce the
1398 * next splits into the previous records created.
1400 if (ext_flags
& OCFS2_EXT_UNWRITTEN
)
1401 *extents_to_split
= *extents_to_split
+ 2;
1404 * Only increment phys if it doesn't describe
1410 desc
->c_phys
= phys
;
1413 *clusters_to_alloc
= *clusters_to_alloc
+ 1;
1415 if (ext_flags
& OCFS2_EXT_UNWRITTEN
)
1416 desc
->c_unwritten
= 1;
1426 static int ocfs2_write_begin_inline(struct address_space
*mapping
,
1427 struct inode
*inode
,
1428 struct ocfs2_write_ctxt
*wc
)
1431 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1434 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)wc
->w_di_bh
->b_data
;
1436 page
= find_or_create_page(mapping
, 0, GFP_NOFS
);
1443 * If we don't set w_num_pages then this page won't get unlocked
1444 * and freed on cleanup of the write context.
1446 wc
->w_pages
[0] = wc
->w_target_page
= page
;
1447 wc
->w_num_pages
= 1;
1449 handle
= ocfs2_start_trans(osb
, OCFS2_INODE_UPDATE_CREDITS
);
1450 if (IS_ERR(handle
)) {
1451 ret
= PTR_ERR(handle
);
1456 ret
= ocfs2_journal_access(handle
, inode
, wc
->w_di_bh
,
1457 OCFS2_JOURNAL_ACCESS_WRITE
);
1459 ocfs2_commit_trans(osb
, handle
);
1465 if (!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
))
1466 ocfs2_set_inode_data_inline(inode
, di
);
1468 if (!PageUptodate(page
)) {
1469 ret
= ocfs2_read_inline_data(inode
, page
, wc
->w_di_bh
);
1471 ocfs2_commit_trans(osb
, handle
);
1477 wc
->w_handle
= handle
;
1482 int ocfs2_size_fits_inline_data(struct buffer_head
*di_bh
, u64 new_size
)
1484 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
1486 if (new_size
<= le16_to_cpu(di
->id2
.i_data
.id_count
))
1491 static int ocfs2_try_to_write_inline_data(struct address_space
*mapping
,
1492 struct inode
*inode
, loff_t pos
,
1493 unsigned len
, struct page
*mmap_page
,
1494 struct ocfs2_write_ctxt
*wc
)
1496 int ret
, written
= 0;
1497 loff_t end
= pos
+ len
;
1498 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1500 mlog(0, "Inode %llu, write of %u bytes at off %llu. features: 0x%x\n",
1501 (unsigned long long)oi
->ip_blkno
, len
, (unsigned long long)pos
,
1502 oi
->ip_dyn_features
);
1505 * Handle inodes which already have inline data 1st.
1507 if (oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1508 if (mmap_page
== NULL
&&
1509 ocfs2_size_fits_inline_data(wc
->w_di_bh
, end
))
1510 goto do_inline_write
;
1513 * The write won't fit - we have to give this inode an
1514 * inline extent list now.
1516 ret
= ocfs2_convert_inline_data_to_extents(inode
, wc
->w_di_bh
);
1523 * Check whether the inode can accept inline data.
1525 if (oi
->ip_clusters
!= 0 || i_size_read(inode
) != 0)
1529 * Check whether the write can fit.
1531 if (mmap_page
|| end
> ocfs2_max_inline_data(inode
->i_sb
))
1535 ret
= ocfs2_write_begin_inline(mapping
, inode
, wc
);
1542 * This signals to the caller that the data can be written
1547 return written
? written
: ret
;
1551 * This function only does anything for file systems which can't
1552 * handle sparse files.
1554 * What we want to do here is fill in any hole between the current end
1555 * of allocation and the end of our write. That way the rest of the
1556 * write path can treat it as an non-allocating write, which has no
1557 * special case code for sparse/nonsparse files.
1559 static int ocfs2_expand_nonsparse_inode(struct inode
*inode
, loff_t pos
,
1561 struct ocfs2_write_ctxt
*wc
)
1564 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1565 loff_t newsize
= pos
+ len
;
1567 if (ocfs2_sparse_alloc(osb
))
1570 if (newsize
<= i_size_read(inode
))
1573 ret
= ocfs2_extend_no_holes(inode
, newsize
, newsize
- len
);
1580 int ocfs2_write_begin_nolock(struct address_space
*mapping
,
1581 loff_t pos
, unsigned len
, unsigned flags
,
1582 struct page
**pagep
, void **fsdata
,
1583 struct buffer_head
*di_bh
, struct page
*mmap_page
)
1585 int ret
, credits
= OCFS2_INODE_UPDATE_CREDITS
;
1586 unsigned int clusters_to_alloc
, extents_to_split
;
1587 struct ocfs2_write_ctxt
*wc
;
1588 struct inode
*inode
= mapping
->host
;
1589 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1590 struct ocfs2_dinode
*di
;
1591 struct ocfs2_alloc_context
*data_ac
= NULL
;
1592 struct ocfs2_alloc_context
*meta_ac
= NULL
;
1595 ret
= ocfs2_alloc_write_ctxt(&wc
, osb
, pos
, len
, di_bh
);
1601 if (ocfs2_supports_inline_data(osb
)) {
1602 ret
= ocfs2_try_to_write_inline_data(mapping
, inode
, pos
, len
,
1614 ret
= ocfs2_expand_nonsparse_inode(inode
, pos
, len
, wc
);
1620 ret
= ocfs2_populate_write_desc(inode
, wc
, &clusters_to_alloc
,
1627 di
= (struct ocfs2_dinode
*)wc
->w_di_bh
->b_data
;
1630 * We set w_target_from, w_target_to here so that
1631 * ocfs2_write_end() knows which range in the target page to
1632 * write out. An allocation requires that we write the entire
1635 if (clusters_to_alloc
|| extents_to_split
) {
1637 * XXX: We are stretching the limits of
1638 * ocfs2_lock_allocators(). It greatly over-estimates
1639 * the work to be done.
1641 ret
= ocfs2_lock_allocators(inode
, di
, clusters_to_alloc
,
1642 extents_to_split
, &data_ac
, &meta_ac
);
1648 credits
= ocfs2_calc_extend_credits(inode
->i_sb
, di
,
1653 ocfs2_set_target_boundaries(osb
, wc
, pos
, len
,
1654 clusters_to_alloc
+ extents_to_split
);
1656 handle
= ocfs2_start_trans(osb
, credits
);
1657 if (IS_ERR(handle
)) {
1658 ret
= PTR_ERR(handle
);
1663 wc
->w_handle
= handle
;
1666 * We don't want this to fail in ocfs2_write_end(), so do it
1669 ret
= ocfs2_journal_access(handle
, inode
, wc
->w_di_bh
,
1670 OCFS2_JOURNAL_ACCESS_WRITE
);
1677 * Fill our page array first. That way we've grabbed enough so
1678 * that we can zero and flush if we error after adding the
1681 ret
= ocfs2_grab_pages_for_write(mapping
, wc
, wc
->w_cpos
, pos
,
1682 clusters_to_alloc
+ extents_to_split
,
1689 ret
= ocfs2_write_cluster_by_desc(mapping
, data_ac
, meta_ac
, wc
, pos
,
1697 ocfs2_free_alloc_context(data_ac
);
1699 ocfs2_free_alloc_context(meta_ac
);
1702 *pagep
= wc
->w_target_page
;
1706 ocfs2_commit_trans(osb
, handle
);
1709 ocfs2_free_write_ctxt(wc
);
1712 ocfs2_free_alloc_context(data_ac
);
1714 ocfs2_free_alloc_context(meta_ac
);
1718 static int ocfs2_write_begin(struct file
*file
, struct address_space
*mapping
,
1719 loff_t pos
, unsigned len
, unsigned flags
,
1720 struct page
**pagep
, void **fsdata
)
1723 struct buffer_head
*di_bh
= NULL
;
1724 struct inode
*inode
= mapping
->host
;
1726 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
1733 * Take alloc sem here to prevent concurrent lookups. That way
1734 * the mapping, zeroing and tree manipulation within
1735 * ocfs2_write() will be safe against ->readpage(). This
1736 * should also serve to lock out allocation from a shared
1739 down_write(&OCFS2_I(inode
)->ip_alloc_sem
);
1741 ret
= ocfs2_write_begin_nolock(mapping
, pos
, len
, flags
, pagep
,
1742 fsdata
, di_bh
, NULL
);
1753 up_write(&OCFS2_I(inode
)->ip_alloc_sem
);
1756 ocfs2_inode_unlock(inode
, 1);
1761 static void ocfs2_write_end_inline(struct inode
*inode
, loff_t pos
,
1762 unsigned len
, unsigned *copied
,
1763 struct ocfs2_dinode
*di
,
1764 struct ocfs2_write_ctxt
*wc
)
1768 if (unlikely(*copied
< len
)) {
1769 if (!PageUptodate(wc
->w_target_page
)) {
1775 kaddr
= kmap_atomic(wc
->w_target_page
, KM_USER0
);
1776 memcpy(di
->id2
.i_data
.id_data
+ pos
, kaddr
+ pos
, *copied
);
1777 kunmap_atomic(kaddr
, KM_USER0
);
1779 mlog(0, "Data written to inode at offset %llu. "
1780 "id_count = %u, copied = %u, i_dyn_features = 0x%x\n",
1781 (unsigned long long)pos
, *copied
,
1782 le16_to_cpu(di
->id2
.i_data
.id_count
),
1783 le16_to_cpu(di
->i_dyn_features
));
1786 int ocfs2_write_end_nolock(struct address_space
*mapping
,
1787 loff_t pos
, unsigned len
, unsigned copied
,
1788 struct page
*page
, void *fsdata
)
1791 unsigned from
, to
, start
= pos
& (PAGE_CACHE_SIZE
- 1);
1792 struct inode
*inode
= mapping
->host
;
1793 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1794 struct ocfs2_write_ctxt
*wc
= fsdata
;
1795 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)wc
->w_di_bh
->b_data
;
1796 handle_t
*handle
= wc
->w_handle
;
1797 struct page
*tmppage
;
1799 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
1800 ocfs2_write_end_inline(inode
, pos
, len
, &copied
, di
, wc
);
1801 goto out_write_size
;
1804 if (unlikely(copied
< len
)) {
1805 if (!PageUptodate(wc
->w_target_page
))
1808 ocfs2_zero_new_buffers(wc
->w_target_page
, start
+copied
,
1811 flush_dcache_page(wc
->w_target_page
);
1813 for(i
= 0; i
< wc
->w_num_pages
; i
++) {
1814 tmppage
= wc
->w_pages
[i
];
1816 if (tmppage
== wc
->w_target_page
) {
1817 from
= wc
->w_target_from
;
1818 to
= wc
->w_target_to
;
1820 BUG_ON(from
> PAGE_CACHE_SIZE
||
1821 to
> PAGE_CACHE_SIZE
||
1825 * Pages adjacent to the target (if any) imply
1826 * a hole-filling write in which case we want
1827 * to flush their entire range.
1830 to
= PAGE_CACHE_SIZE
;
1833 if (ocfs2_should_order_data(inode
))
1834 walk_page_buffers(wc
->w_handle
, page_buffers(tmppage
),
1836 ocfs2_journal_dirty_data
);
1838 block_commit_write(tmppage
, from
, to
);
1843 if (pos
> inode
->i_size
) {
1844 i_size_write(inode
, pos
);
1845 mark_inode_dirty(inode
);
1847 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
1848 di
->i_size
= cpu_to_le64((u64
)i_size_read(inode
));
1849 inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
1850 di
->i_mtime
= di
->i_ctime
= cpu_to_le64(inode
->i_mtime
.tv_sec
);
1851 di
->i_mtime_nsec
= di
->i_ctime_nsec
= cpu_to_le32(inode
->i_mtime
.tv_nsec
);
1852 ocfs2_journal_dirty(handle
, wc
->w_di_bh
);
1854 ocfs2_commit_trans(osb
, handle
);
1856 ocfs2_run_deallocs(osb
, &wc
->w_dealloc
);
1858 ocfs2_free_write_ctxt(wc
);
1863 static int ocfs2_write_end(struct file
*file
, struct address_space
*mapping
,
1864 loff_t pos
, unsigned len
, unsigned copied
,
1865 struct page
*page
, void *fsdata
)
1868 struct inode
*inode
= mapping
->host
;
1870 ret
= ocfs2_write_end_nolock(mapping
, pos
, len
, copied
, page
, fsdata
);
1872 up_write(&OCFS2_I(inode
)->ip_alloc_sem
);
1873 ocfs2_inode_unlock(inode
, 1);
1878 const struct address_space_operations ocfs2_aops
= {
1879 .readpage
= ocfs2_readpage
,
1880 .writepage
= ocfs2_writepage
,
1881 .write_begin
= ocfs2_write_begin
,
1882 .write_end
= ocfs2_write_end
,
1884 .sync_page
= block_sync_page
,
1885 .direct_IO
= ocfs2_direct_IO
,
1886 .invalidatepage
= ocfs2_invalidatepage
,
1887 .releasepage
= ocfs2_releasepage
,
1888 .migratepage
= buffer_migrate_page
,