10 #include <linux/sched.h>
11 #include <linux/slab.h>
40 unsigned int from,
unsigned int to)
42 struct buffer_head *
head = page_buffers(page);
43 unsigned int bsize = head->b_size;
44 struct buffer_head *bh;
47 for (bh = head, start = 0; bh != head || !
start;
48 bh = bh->b_this_page, start =
end) {
50 if (end <= from || start >= to)
52 if (gfs2_is_jdata(ip))
53 set_buffer_uptodate(bh);
69 struct buffer_head *bh_result,
int create)
76 if (!buffer_mapped(bh_result))
81 static int gfs2_get_block_direct(
struct inode *inode,
sector_t lblock,
82 struct buffer_head *bh_result,
int create)
95 static int gfs2_writepage_common(
struct page *page,
98 struct inode *inode = page->
mapping->host;
100 struct gfs2_sbd *sdp = GFS2_SB(inode);
101 loff_t i_size = i_size_read(inode);
111 if (page->
index > end_index || (page->
index == end_index && !offset)) {
112 page->
mapping->a_ops->invalidatepage(page, 0);
130 static int gfs2_writeback_writepage(
struct page *page,
135 ret = gfs2_writepage_common(page, wbc);
149 static int gfs2_ordered_writepage(
struct page *page,
152 struct inode *inode = page->
mapping->host;
156 ret = gfs2_writepage_common(page, wbc);
160 if (!page_has_buffers(page)) {
162 (1 << BH_Dirty)|(1 << BH_Uptodate));
164 gfs2_page_add_databufs(ip, page, 0, inode->
i_sb->s_blocksize-1);
179 static int __gfs2_jdata_writepage(
struct page *page,
struct writeback_control *wbc)
181 struct inode *inode = page->
mapping->host;
183 struct gfs2_sbd *sdp = GFS2_SB(inode);
185 if (PageChecked(page)) {
186 ClearPageChecked(page);
187 if (!page_has_buffers(page)) {
189 (1 << BH_Dirty)|(1 << BH_Uptodate));
191 gfs2_page_add_databufs(ip, page, 0, sdp->
sd_vfs->s_blocksize-1);
204 static int gfs2_jdata_writepage(
struct page *page,
struct writeback_control *wbc)
206 struct inode *inode = page->
mapping->host;
207 struct gfs2_sbd *sdp = GFS2_SB(inode);
211 if (PageChecked(page)) {
219 ret = gfs2_writepage_common(page, wbc);
221 ret = __gfs2_jdata_writepage(page, wbc);
263 struct inode *inode = mapping->
host;
264 struct gfs2_sbd *sdp = GFS2_SB(inode);
265 loff_t i_size = i_size_read(inode);
276 for(i = 0; i < nr_pages; i++) {
277 struct page *page = pvec->
pages[
i];
293 wait_on_page_writeback(page);
295 if (PageWriteback(page) ||
302 if (page->
index > end_index || (page->
index == end_index && !offset)) {
303 page->
mapping->a_ops->invalidatepage(page, 0);
308 ret = __gfs2_jdata_writepage(page, wbc);
329 static int gfs2_write_cache_jdata(
struct address_space *mapping,
341 pagevec_init(&pvec, 0);
354 while (!done && (index <= end) &&
359 ret = gfs2_write_jdata_pagevec(mapping, wbc, &pvec, nr_pages, end);
365 pagevec_release(&pvec);
369 if (!scanned && !done) {
392 static int gfs2_jdata_writepages(
struct address_space *mapping,
399 ret = gfs2_write_cache_jdata(mapping, wbc);
402 ret = gfs2_write_cache_jdata(mapping, wbc);
415 static int stuffed_readpage(
struct gfs2_inode *ip,
struct page *page)
417 struct buffer_head *dibh;
429 SetPageUptodate(page);
433 error = gfs2_meta_inode_buffer(ip, &dibh);
438 if (dsize > (dibh->b_size -
sizeof(
struct gfs2_dinode)))
439 dsize = (dibh->b_size -
sizeof(
struct gfs2_dinode));
445 SetPageUptodate(page);
462 static int __gfs2_readpage(
void *
file,
struct page *page)
468 if (gfs2_is_stuffed(ip)) {
469 error = stuffed_readpage(ip, page);
491 static int gfs2_readpage(
struct file *file,
struct page *page)
505 if (page->
mapping == mapping && !PageUptodate(page))
506 error = __gfs2_readpage(file, page);
543 return PTR_ERR(page);
545 memcpy(buf + copied, p + offset, amt);
552 }
while(copied < size);
571 static int gfs2_readpages(
struct file *file,
struct address_space *mapping,
574 struct inode *inode = mapping->
host;
576 struct gfs2_sbd *sdp = GFS2_SB(inode);
584 if (!gfs2_is_stuffed(ip))
607 static int gfs2_write_begin(
struct file *file,
struct address_space *mapping,
608 loff_t
pos,
unsigned len,
unsigned flags,
609 struct page **pagep,
void **fsdata)
614 unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
615 unsigned requested = 0;
637 if (alloc_required || gfs2_is_jdata(ip))
638 gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
640 if (alloc_required) {
641 error = gfs2_quota_lock_check(ip);
645 requested = data_blocks + ind_blocks;
652 if (gfs2_is_jdata(ip))
653 rblocks += data_blocks ? data_blocks : 1;
654 if (ind_blocks || data_blocks)
659 rblocks += gfs2_rg_blocks(ip, requested);
673 if (gfs2_is_stuffed(ip)) {
679 }
else if (!PageUptodate(page)) {
680 error = stuffed_readpage(ip, page);
695 if (pos + len > ip->
i_inode.i_size)
702 if (alloc_required) {
722 static void adjust_fs_space(
struct inode *inode)
729 struct buffer_head *m_bh, *l_bh;
730 u64 fs_total, new_free;
734 if (gfs2_meta_inode_buffer(m_ip, &m_bh) != 0)
745 fs_warn(sdp,
"File system extended by %llu blocks.\n",
746 (
unsigned long long)new_free);
749 if (gfs2_meta_inode_buffer(l_ip, &l_bh) != 0)
771 static int gfs2_stuffed_write_end(
struct inode *inode,
struct buffer_head *dibh,
772 loff_t pos,
unsigned len,
unsigned copied,
776 struct gfs2_sbd *sdp = GFS2_SB(inode);
778 u64 to = pos + copied;
784 memcpy(buf + pos, kaddr + pos, copied);
785 memset(kaddr + pos + copied, 0, len - copied);
789 if (!PageUptodate(page))
790 SetPageUptodate(page);
796 i_size_write(inode, to);
797 mark_inode_dirty(inode);
801 adjust_fs_space(inode);
833 static int gfs2_write_end(
struct file *file,
struct address_space *mapping,
834 loff_t pos,
unsigned len,
unsigned copied,
835 struct page *page,
void *fsdata)
837 struct inode *inode = page->
mapping->host;
839 struct gfs2_sbd *sdp = GFS2_SB(inode);
841 struct buffer_head *dibh;
843 unsigned int to = from + len;
848 ret = gfs2_meta_inode_buffer(ip, &dibh);
857 if (gfs2_is_stuffed(ip))
858 return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page);
860 if (!gfs2_is_writeback(ip))
861 gfs2_page_add_databufs(ip, page, from, to);
866 adjust_fs_space(inode);
874 if (ip->
i_res->rs_qa_qd_num)
892 static int gfs2_set_page_dirty(
struct page *page)
894 SetPageChecked(page);
917 if (!gfs2_is_stuffed(ip))
925 static void gfs2_discard(
struct gfs2_sbd *sdp,
struct buffer_head *bh)
931 clear_buffer_dirty(bh);
934 if (!list_empty(&bd->
bd_list) && !buffer_pinned(bh))
940 clear_buffer_mapped(bh);
941 clear_buffer_req(bh);
942 clear_buffer_new(bh);
943 gfs2_log_unlock(sdp);
947 static void gfs2_invalidatepage(
struct page *page,
unsigned long offset)
950 struct buffer_head *bh, *
head;
951 unsigned long pos = 0;
953 BUG_ON(!PageLocked(page));
955 ClearPageChecked(page);
956 if (!page_has_buffers(page))
959 bh = head = page_buffers(page);
962 gfs2_discard(sdp, bh);
964 bh = bh->b_this_page;
965 }
while (bh != head);
980 static int gfs2_ok_for_dio(
struct gfs2_inode *ip,
int rw, loff_t offset)
987 if (gfs2_is_stuffed(ip))
990 if (offset >= i_size_read(&ip->
i_inode))
998 const struct iovec *iov, loff_t offset,
999 unsigned long nr_segs)
1001 struct file *file = iocb->
ki_filp;
1002 struct inode *inode = file->
f_mapping->host;
1019 rv = gfs2_ok_for_dio(ip, rw, offset);
1024 offset, nr_segs, gfs2_get_block_direct,
1046 struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
1047 struct buffer_head *bh, *
head;
1050 if (!page_has_buffers(page))
1055 head = bh = page_buffers(page);
1058 goto cannot_release;
1061 goto cannot_release;
1062 if (buffer_pinned(bh) || buffer_dirty(bh))
1064 bh = bh->b_this_page;
1065 }
while(bh != head);
1067 gfs2_log_unlock(sdp);
1069 head = bh = page_buffers(page);
1075 if (!list_empty(&bd->
bd_list)) {
1076 if (!buffer_pinned(bh))
1083 bh->b_private =
NULL;
1085 gfs2_log_unlock(sdp);
1089 bh = bh->b_this_page;
1090 }
while (bh != head);
1099 gfs2_log_unlock(sdp);
1104 .writepage = gfs2_writeback_writepage,
1105 .writepages = gfs2_writeback_writepages,
1106 .readpage = gfs2_readpage,
1107 .readpages = gfs2_readpages,
1108 .write_begin = gfs2_write_begin,
1109 .write_end = gfs2_write_end,
1111 .invalidatepage = gfs2_invalidatepage,
1113 .direct_IO = gfs2_direct_IO,
1120 .writepage = gfs2_ordered_writepage,
1121 .readpage = gfs2_readpage,
1122 .readpages = gfs2_readpages,
1123 .write_begin = gfs2_write_begin,
1124 .write_end = gfs2_write_end,
1125 .set_page_dirty = gfs2_set_page_dirty,
1127 .invalidatepage = gfs2_invalidatepage,
1129 .direct_IO = gfs2_direct_IO,
1136 .writepage = gfs2_jdata_writepage,
1137 .writepages = gfs2_jdata_writepages,
1138 .readpage = gfs2_readpage,
1139 .readpages = gfs2_readpages,
1140 .write_begin = gfs2_write_begin,
1141 .write_end = gfs2_write_end,
1142 .set_page_dirty = gfs2_set_page_dirty,
1144 .invalidatepage = gfs2_invalidatepage,
1154 if (gfs2_is_writeback(ip))
1155 inode->
i_mapping->a_ops = &gfs2_writeback_aops;
1156 else if (gfs2_is_ordered(ip))
1157 inode->
i_mapping->a_ops = &gfs2_ordered_aops;
1158 else if (gfs2_is_jdata(ip))
1159 inode->
i_mapping->a_ops = &gfs2_jdata_aops;