23 #include <linux/slab.h>
26 #include <asm/byteorder.h>
52 struct buffer_head *bh_result,
int create)
57 struct buffer_head *bh =
NULL;
58 struct buffer_head *buffer_cache_bh =
NULL;
62 trace_ocfs2_symlink_get_block(
63 (
unsigned long long)OCFS2_I(inode)->ip_blkno,
64 (
unsigned long long)iblock, bh_result, create);
66 BUG_ON(ocfs2_inode_is_fast_symlink(inode));
68 if ((iblock << inode->
i_sb->s_blocksize_bits) >
PATH_MAX + 1) {
70 (
unsigned long long)iblock);
81 if ((
u64)iblock >= ocfs2_clusters_to_blocks(inode->
i_sb,
83 mlog(
ML_ERROR,
"block offset is outside the allocated size: "
84 "%llu\n", (
unsigned long long)iblock);
90 if (!buffer_uptodate(bh_result) && ocfs2_inode_is_new(inode)) {
93 buffer_cache_bh = sb_getblk(osb->
sb, blkno);
94 if (!buffer_cache_bh) {
103 if (buffer_jbd(buffer_cache_bh)
104 && ocfs2_inode_is_new(inode)) {
110 memcpy(kaddr + (bh_result->b_size * iblock),
111 buffer_cache_bh->b_data,
114 set_buffer_uptodate(bh_result);
116 brelse(buffer_cache_bh);
119 map_bh(bh_result, inode->
i_sb,
131 struct buffer_head *bh_result,
int create)
139 trace_ocfs2_get_block((
unsigned long long)OCFS2_I(inode)->ip_blkno,
140 (
unsigned long long)iblock, bh_result, create);
144 inode, inode->
i_ino);
148 err = ocfs2_symlink_get_block(inode, iblock, bh_result, create);
155 mlog(
ML_ERROR,
"Error %d from get_blocks(0x%p, %llu, 1, "
156 "%llu, NULL)\n", err, inode, (
unsigned long long)iblock,
157 (
unsigned long long)p_blkno);
161 if (max_blocks < count)
175 if (create && p_blkno == 0 && ocfs2_sparse_alloc(osb)) {
176 clear_buffer_dirty(bh_result);
177 clear_buffer_uptodate(bh_result);
183 map_bh(bh_result, inode->
i_sb, p_blkno);
185 bh_result->b_size = count << inode->
i_blkbits;
187 if (!ocfs2_sparse_alloc(osb)) {
191 "iblock = %llu p_blkno = %llu blkno=(%llu)\n",
192 (
unsigned long long)iblock,
193 (
unsigned long long)p_blkno,
194 (
unsigned long long)OCFS2_I(inode)->ip_blkno);
195 mlog(
ML_ERROR,
"Size %llu, clusters %u\n", (
unsigned long long)i_size_read(inode), OCFS2_I(inode)->ip_clusters);
201 past_eof = ocfs2_blocks_for_bytes(inode->
i_sb, i_size_read(inode));
203 trace_ocfs2_get_block_end((
unsigned long long)OCFS2_I(inode)->ip_blkno,
204 (
unsigned long long)past_eof);
205 if (create && (iblock >= past_eof))
206 set_buffer_new(bh_result);
216 struct buffer_head *di_bh)
224 (
unsigned long long)OCFS2_I(inode)->ip_blkno);
228 size = i_size_read(inode);
231 size > ocfs2_max_inline_data_with_xattr(inode->
i_sb, di)) {
233 "Inode %llu has with inline data has bad size: %Lu",
234 (
unsigned long long)OCFS2_I(inode)->ip_blkno,
235 (
unsigned long long)size);
247 SetPageUptodate(page);
252 static int ocfs2_readpage_inline(
struct inode *inode,
struct page *
page)
255 struct buffer_head *di_bh =
NULL;
257 BUG_ON(!PageLocked(page));
274 static int ocfs2_readpage(
struct file *
file,
struct page *page)
276 struct inode *inode = page->
mapping->host;
281 trace_ocfs2_readpage((
unsigned long long)oi->
ip_blkno,
282 (page ? page->
index : 0));
302 goto out_inode_unlock;
315 if (start >= i_size_read(inode)) {
317 SetPageUptodate(page);
323 ret = ocfs2_readpage_inline(inode, page);
351 struct inode *inode = mapping->
host;
382 if (start >= i_size_read(inode))
407 trace_ocfs2_writepage(
408 (
unsigned long long)OCFS2_I(page->
mapping->host)->ip_blkno,
419 struct buffer_head *
head,
423 int (*
fn)( handle_t *handle,
424 struct buffer_head *bh))
426 struct buffer_head *bh;
427 unsigned block_start, block_end;
428 unsigned blocksize = head->b_size;
430 struct buffer_head *
next;
432 for ( bh = head, block_start = 0;
433 ret == 0 && (bh != head || !block_start);
434 block_start = block_end, bh =
next)
436 next = bh->b_this_page;
437 block_end = block_start + blocksize;
438 if (block_end <= from || block_start >= to) {
439 if (partial && !buffer_uptodate(bh))
455 struct inode *inode = mapping->
host;
457 trace_ocfs2_bmap((
unsigned long long)OCFS2_I(inode)->ip_blkno,
458 (
unsigned long long)block);
470 down_read(&OCFS2_I(inode)->ip_alloc_sem);
478 up_read(&OCFS2_I(inode)->ip_alloc_sem);
484 (
unsigned long long)block);
490 status = err ? 0 : p_blkno;
511 static int ocfs2_direct_IO_get_blocks(
struct inode *inode,
sector_t iblock,
512 struct buffer_head *bh_result,
int create)
515 u64 p_blkno, inode_blocks, contig_blocks;
517 unsigned char blocksize_bits = inode->
i_sb->s_blocksize_bits;
518 unsigned long max_blocks = bh_result->b_size >> inode->
i_blkbits;
524 inode_blocks = ocfs2_blocks_for_bytes(inode->
i_sb, i_size_read(inode));
529 &contig_blocks, &ext_flags);
532 (
unsigned long long)iblock);
547 map_bh(bh_result, inode->
i_sb, p_blkno);
549 clear_buffer_mapped(bh_result);
553 if (max_blocks < contig_blocks)
554 contig_blocks = max_blocks;
555 bh_result->b_size = contig_blocks << blocksize_bits;
565 static void ocfs2_dio_end_io(
struct kiocb *
iocb,
572 struct inode *inode = iocb->
ki_filp->f_path.dentry->d_inode;
586 waitqueue_active(wq)) {
606 static void ocfs2_invalidatepage(
struct page *page,
unsigned long offset)
608 journal_t *journal =
OCFS2_SB(page->
mapping->host->i_sb)->journal->j_journal;
613 static int ocfs2_releasepage(
struct page *page,
gfp_t wait)
615 journal_t *journal =
OCFS2_SB(page->
mapping->host->i_sb)->journal->j_journal;
617 if (!page_has_buffers(page))
624 const struct iovec *iov,
626 unsigned long nr_segs)
628 struct file *file = iocb->
ki_filp;
629 struct inode *inode = file->
f_path.dentry->d_inode->i_mapping->host;
635 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
639 if (i_size_read(inode) <= offset)
643 iov, offset, nr_segs,
644 ocfs2_direct_IO_get_blocks,
645 ocfs2_dio_end_io,
NULL, 0);
648 static void ocfs2_figure_cluster_boundaries(
struct ocfs2_super *osb,
660 cluster_start = cpos %
cpp;
670 *start = cluster_start;
683 static void ocfs2_clear_page_regions(
struct page *page,
685 unsigned from,
unsigned to)
688 unsigned int cluster_start, cluster_end;
690 ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end);
695 if (from > cluster_start)
696 memset(kaddr + cluster_start, 0, from - cluster_start);
697 if (to < cluster_end)
698 memset(kaddr + to, 0, cluster_end - to);
700 memset(kaddr + cluster_start, 0, cluster_end - cluster_start);
713 static int ocfs2_should_read_blk(
struct inode *inode,
struct page *page,
714 unsigned int block_start)
721 if (i_size_read(inode) > offset)
735 struct inode *inode,
unsigned int from,
736 unsigned int to,
int new)
739 struct buffer_head *
head, *bh, *wait[2], **wait_bh =
wait;
740 unsigned int block_end, block_start;
741 unsigned int bsize = 1 << inode->
i_blkbits;
743 if (!page_has_buffers(page))
746 head = page_buffers(page);
747 for (bh = head, block_start = 0; bh != head || !block_start;
748 bh = bh->b_this_page, block_start += bsize) {
749 block_end = block_start + bsize;
751 clear_buffer_new(bh);
757 if (block_start >= to || block_end <= from) {
758 if (PageUptodate(page))
759 set_buffer_uptodate(bh);
770 if (!buffer_mapped(bh)) {
771 map_bh(bh, inode->
i_sb, *p_blkno);
775 if (PageUptodate(page)) {
776 if (!buffer_uptodate(bh))
777 set_buffer_uptodate(bh);
778 }
else if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
780 ocfs2_should_read_blk(inode, page, block_start) &&
781 (block_start < from || block_end > to)) {
786 *p_blkno = *p_blkno + 1;
792 while(wait_bh > wait) {
793 wait_on_buffer(*--wait_bh);
794 if (!buffer_uptodate(*wait_bh))
798 if (ret == 0 || !
new)
808 block_end = block_start + bsize;
809 if (block_end <= from)
811 if (block_start >= to)
814 zero_user(page, block_start, bh->b_size);
815 set_buffer_uptodate(bh);
819 block_start = block_end;
820 bh = bh->b_this_page;
821 }
while (bh != head);
826 #if (PAGE_CACHE_SIZE >= OCFS2_MAX_CLUSTERSIZE)
827 #define OCFS2_MAX_CTXT_PAGES 1
829 #define OCFS2_MAX_CTXT_PAGES (OCFS2_MAX_CLUSTERSIZE / PAGE_CACHE_SIZE)
832 #define OCFS2_MAX_CLUSTERS_PER_PAGE (PAGE_CACHE_SIZE / OCFS2_MIN_CLUSTERSIZE)
948 unsigned len,
struct buffer_head *di_bh)
981 static void ocfs2_zero_new_buffers(
struct page *page,
unsigned from,
unsigned to)
983 unsigned int block_start, block_end;
984 struct buffer_head *
head, *bh;
986 BUG_ON(!PageLocked(page));
987 if (!page_has_buffers(page))
990 bh = head = page_buffers(page);
993 block_end = block_start + bh->b_size;
995 if (buffer_new(bh)) {
996 if (block_end > from && block_start < to) {
997 if (!PageUptodate(page)) {
1000 start =
max(from, block_start);
1001 end =
min(to, block_end);
1003 zero_user_segment(page, start, end);
1004 set_buffer_uptodate(bh);
1007 clear_buffer_new(bh);
1012 block_start = block_end;
1013 bh = bh->b_this_page;
1014 }
while (bh != head);
1021 static void ocfs2_write_failure(
struct inode *inode,
1023 loff_t user_pos,
unsigned user_len)
1027 to = user_pos + user_len;
1028 struct page *tmppage;
1035 if (page_has_buffers(tmppage)) {
1036 if (ocfs2_should_order_data(inode))
1037 ocfs2_jbd2_file_inode(wc->
w_handle, inode);
1044 static int ocfs2_prepare_page_for_write(
struct inode *inode,
u64 *p_blkno,
1046 struct page *page,
u32 cpos,
1047 loff_t user_pos,
unsigned user_len,
1051 unsigned int map_from = 0, map_to = 0;
1052 unsigned int cluster_start, cluster_end;
1053 unsigned int user_data_from = 0, user_data_to = 0;
1055 ocfs2_figure_cluster_boundaries(
OCFS2_SB(inode->
i_sb), cpos,
1056 &cluster_start, &cluster_end);
1061 new =
new | ((i_size_read(inode) <=
page_offset(page)) &&
1066 map_to = map_from + user_len;
1070 cluster_start, cluster_end,
1074 map_from, map_to,
new);
1080 user_data_from = map_from;
1081 user_data_to = map_to;
1083 map_from = cluster_start;
1084 map_to = cluster_end;
1094 map_from = cluster_start;
1095 map_to = cluster_end;
1098 cluster_start, cluster_end,
new);
1115 if (
new && !PageUptodate(page))
1117 cpos, user_data_from, user_data_to);
1128 static int ocfs2_grab_pages_for_write(
struct address_space *mapping,
1130 u32 cpos, loff_t user_pos,
1131 unsigned user_len,
int new,
1132 struct page *mmap_page)
1135 unsigned long start, target_index, end_index,
index;
1136 struct inode *inode = mapping->
host;
1150 start = ocfs2_align_clusters_to_page_index(inode->
i_sb, cpos);
1156 last_byte =
max(user_pos + user_len, i_size_read(inode));
1158 end_index = ((last_byte - 1) >> PAGE_CACHE_SHIFT) + 1;
1163 start = target_index;
1169 if (index == target_index && mmap_page) {
1175 lock_page(mmap_page);
1178 if (mmap_page->
mapping != mapping) {
1198 if (index == target_index)
1210 static int ocfs2_write_cluster(
struct address_space *mapping,
1211 u32 phys,
unsigned int unwritten,
1212 unsigned int should_zero,
1216 loff_t user_pos,
unsigned user_len)
1219 u64 v_blkno, p_blkno;
1220 struct inode *inode = mapping->
host;
1223 new = phys == 0 ? 1 : 0;
1246 "Inode %llu: EAGAIN return during allocation.\n",
1247 (
unsigned long long)OCFS2_I(inode)->ip_blkno);
1252 }
else if (unwritten) {
1265 v_blkno = ocfs2_clusters_to_blocks(inode->
i_sb, cpos);
1267 v_blkno = user_pos >> inode->
i_sb->s_blocksize_bits;
1277 "at logical block %llu",
1278 (
unsigned long long)OCFS2_I(inode)->ip_blkno,
1279 (
unsigned long long)v_blkno);
1288 tmpret = ocfs2_prepare_page_for_write(inode, &p_blkno, wc,
1303 ocfs2_write_failure(inode, wc, user_pos, user_len);
1310 static int ocfs2_write_cluster_by_desc(
struct address_space *mapping,
1314 loff_t pos,
unsigned len)
1318 unsigned int local_len = len;
1322 for (i = 0; i < wc->
w_clen; i++) {
1334 ret = ocfs2_write_cluster(mapping, desc->
c_phys,
1338 wc, desc->
c_cpos, pos, local_len);
1358 static void ocfs2_set_target_boundaries(
struct ocfs2_super *osb,
1360 loff_t pos,
unsigned len,
int alloc)
1388 ocfs2_figure_cluster_boundaries(osb,
1395 ocfs2_figure_cluster_boundaries(osb,
1413 static int ocfs2_populate_write_desc(
struct inode *inode,
1415 unsigned int *clusters_to_alloc,
1416 unsigned int *extents_to_split)
1420 unsigned int num_clusters = 0;
1421 unsigned int ext_flags = 0;
1425 *clusters_to_alloc = 0;
1426 *extents_to_split = 0;
1428 for (i = 0; i < wc->
w_clen; i++) {
1432 if (num_clusters == 0) {
1437 &num_clusters, &ext_flags);
1455 if (ext_flags & OCFS2_EXT_UNWRITTEN)
1456 *extents_to_split = *extents_to_split + 2;
1480 *clusters_to_alloc = *clusters_to_alloc + 1;
1483 if (ext_flags & OCFS2_EXT_UNWRITTEN) {
1496 static int ocfs2_write_begin_inline(
struct address_space *mapping,
1497 struct inode *inode,
1520 if (IS_ERR(handle)) {
1521 ret = PTR_ERR(handle);
1535 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL))
1538 if (!PageUptodate(page)) {
1561 static int ocfs2_try_to_write_inline_data(
struct address_space *mapping,
1562 struct inode *inode, loff_t pos,
1563 unsigned len,
struct page *mmap_page,
1566 int ret, written = 0;
1567 loff_t end = pos + len;
1571 trace_ocfs2_try_to_write_inline_data((
unsigned long long)oi->
ip_blkno,
1572 len, (
unsigned long long)pos,
1579 if (mmap_page ==
NULL &&
1581 goto do_inline_write;
1596 if (oi->
ip_clusters != 0 || i_size_read(inode) != 0)
1604 end > ocfs2_max_inline_data_with_xattr(inode->
i_sb, di))
1608 ret = ocfs2_write_begin_inline(mapping, inode, wc);
1620 return written ? written :
ret;
1632 static int ocfs2_expand_nonsparse_inode(
struct inode *inode,
1633 struct buffer_head *di_bh,
1634 loff_t pos,
unsigned len,
1638 loff_t newsize = pos + len;
1642 if (newsize <= i_size_read(inode))
1650 ocfs2_clusters_for_bytes(inode->
i_sb, i_size_read(inode));
1655 static int ocfs2_zero_tail(
struct inode *inode,
struct buffer_head *di_bh,
1661 if (pos > i_size_read(inode))
1672 static int ocfs2_try_to_free_truncate_log(
struct ocfs2_super *osb,
1673 unsigned int needed)
1677 unsigned int truncated_clusters;
1687 if (truncated_clusters < needed)
1706 loff_t pos,
unsigned len,
unsigned flags,
1707 struct page **pagep,
void **fsdata,
1708 struct buffer_head *di_bh,
struct page *mmap_page)
1711 unsigned int clusters_to_alloc, extents_to_split, clusters_need = 0;
1713 struct inode *inode = mapping->
host;
1720 int try_free = 1, ret1;
1723 ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, di_bh);
1729 if (ocfs2_supports_inline_data(osb)) {
1730 ret = ocfs2_try_to_write_inline_data(mapping, inode, pos, len,
1742 if (ocfs2_sparse_alloc(osb))
1743 ret = ocfs2_zero_tail(inode, di_bh, pos);
1745 ret = ocfs2_expand_nonsparse_inode(inode, di_bh, pos, len,
1756 }
else if (ret == 1) {
1757 clusters_need = wc->
w_clen;
1766 ret = ocfs2_populate_write_desc(inode, wc, &clusters_to_alloc,
1772 clusters_need += clusters_to_alloc;
1776 trace_ocfs2_write_begin_nolock(
1777 (
unsigned long long)OCFS2_I(inode)->ip_blkno,
1778 (
long long)i_size_read(inode),
1781 clusters_to_alloc, extents_to_split);
1789 if (clusters_to_alloc || extents_to_split) {
1798 clusters_to_alloc, extents_to_split,
1799 &data_ac, &meta_ac);
1806 data_ac->
ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
1808 credits = ocfs2_calc_extend_credits(inode->
i_sb,
1819 if (clusters_to_alloc || extents_to_split ||
1822 cluster_of_pages = 1;
1824 cluster_of_pages = 0;
1826 ocfs2_set_target_boundaries(osb, wc, pos, len, cluster_of_pages);
1829 if (IS_ERR(handle)) {
1830 ret = PTR_ERR(handle);
1837 if (clusters_to_alloc) {
1838 ret = dquot_alloc_space_nodirty(inode,
1839 ocfs2_clusters_to_bytes(osb->
sb, clusters_to_alloc));
1859 ret = ocfs2_grab_pages_for_write(mapping, wc, wc->
w_cpos, pos, len,
1860 cluster_of_pages, mmap_page);
1861 if (ret && ret != -
EAGAIN) {
1878 ret = ocfs2_write_cluster_by_desc(mapping, data_ac, meta_ac, wc, pos,
1895 if (clusters_to_alloc)
1896 dquot_free_space(inode,
1897 ocfs2_clusters_to_bytes(osb->
sb, clusters_to_alloc));
1902 ocfs2_free_write_ctxt(wc);
1909 if (ret == -
ENOSPC && try_free) {
1916 ret1 = ocfs2_try_to_free_truncate_log(osb, clusters_need);
1927 static int ocfs2_write_begin(
struct file *file,
struct address_space *mapping,
1928 loff_t pos,
unsigned len,
unsigned flags,
1929 struct page **pagep,
void **fsdata)
1932 struct buffer_head *di_bh =
NULL;
1933 struct inode *inode = mapping->
host;
1951 fsdata, di_bh,
NULL);
1962 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1970 static void ocfs2_write_end_inline(
struct inode *inode, loff_t pos,
1971 unsigned len,
unsigned *copied,
1988 trace_ocfs2_write_end_inline(
1989 (
unsigned long long)OCFS2_I(inode)->ip_blkno,
1990 (
unsigned long long)pos, *copied,
1996 loff_t pos,
unsigned len,
unsigned copied,
1997 struct page *page,
void *fsdata)
2001 struct inode *inode = mapping->
host;
2006 struct page *tmppage;
2009 ocfs2_write_end_inline(inode, pos, len, &copied, di, wc);
2010 goto out_write_size;
2042 if (page_has_buffers(tmppage)) {
2043 if (ocfs2_should_order_data(inode))
2044 ocfs2_jbd2_file_inode(wc->
w_handle, inode);
2051 if (pos > inode->
i_size) {
2052 i_size_write(inode, pos);
2053 mark_inode_dirty(inode);
2055 inode->
i_blocks = ocfs2_inode_sector_count(inode);
2066 ocfs2_free_write_ctxt(wc);
2071 static int ocfs2_write_end(
struct file *file,
struct address_space *mapping,
2072 loff_t pos,
unsigned len,
unsigned copied,
2073 struct page *page,
void *fsdata)
2076 struct inode *inode = mapping->
host;
2080 up_write(&OCFS2_I(inode)->ip_alloc_sem);
2087 .readpage = ocfs2_readpage,
2088 .readpages = ocfs2_readpages,
2089 .writepage = ocfs2_writepage,
2090 .write_begin = ocfs2_write_begin,
2091 .write_end = ocfs2_write_end,
2093 .direct_IO = ocfs2_direct_IO,
2094 .invalidatepage = ocfs2_invalidatepage,
2095 .releasepage = ocfs2_releasepage,