2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/pagemap.h>
17 #include <linux/gfs2_ondisk.h>
20 #include "lm_interface.h"
26 #include "ops_address.h"
30 * gfs2_pte_inval - Sync and invalidate all PTEs associated with a glock
35 void gfs2_pte_inval(struct gfs2_glock *gl)
37 struct gfs2_inode *ip;
41 if (!ip || !S_ISREG(ip->i_di.di_mode))
44 if (!test_bit(GIF_PAGED, &ip->i_flags))
47 inode = gfs2_ip2v_lookup(ip);
49 unmap_shared_mapping_range(inode->i_mapping, 0, 0);
52 if (test_bit(GIF_SW_PAGED, &ip->i_flags))
53 set_bit(GLF_DIRTY, &gl->gl_flags);
56 clear_bit(GIF_SW_PAGED, &ip->i_flags);
60 * gfs2_page_inval - Invalidate all pages associated with a glock
65 void gfs2_page_inval(struct gfs2_glock *gl)
67 struct gfs2_inode *ip;
71 if (!ip || !S_ISREG(ip->i_di.di_mode))
74 inode = gfs2_ip2v_lookup(ip);
76 struct address_space *mapping = inode->i_mapping;
78 truncate_inode_pages(mapping, 0);
79 gfs2_assert_withdraw(ip->i_sbd, !mapping->nrpages);
84 clear_bit(GIF_PAGED, &ip->i_flags);
88 * gfs2_page_sync - Sync the data pages (not metadata) associated with a glock
90 * @flags: DIO_START | DIO_WAIT
92 * Syncs data (not metadata) for a regular file.
93 * No-op for all other types.
96 void gfs2_page_sync(struct gfs2_glock *gl, int flags)
98 struct gfs2_inode *ip;
102 if (!ip || !S_ISREG(ip->i_di.di_mode))
105 inode = gfs2_ip2v_lookup(ip);
107 struct address_space *mapping = inode->i_mapping;
110 if (flags & DIO_START)
111 filemap_fdatawrite(mapping);
112 if (!error && (flags & DIO_WAIT))
113 error = filemap_fdatawait(mapping);
115 /* Put back any errors cleared by filemap_fdatawait()
116 so they can be caught by someone who can pass them
119 if (error == -ENOSPC)
120 set_bit(AS_ENOSPC, &mapping->flags);
122 set_bit(AS_EIO, &mapping->flags);
129 * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
131 * @dibh: the dinode buffer
132 * @block: the block number that was allocated
133 * @private: any locked page held by the caller process
138 int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
139 uint64_t block, void *private)
141 struct gfs2_sbd *sdp = ip->i_sbd;
142 struct inode *inode = ip->i_vnode;
143 struct page *page = (struct page *)private;
144 struct buffer_head *bh;
147 if (!page || page->index) {
148 page = grab_cache_page(inode->i_mapping, 0);
154 if (!PageUptodate(page)) {
155 void *kaddr = kmap(page);
157 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
159 memset(kaddr + ip->i_di.di_size, 0,
160 PAGE_CACHE_SIZE - ip->i_di.di_size);
163 SetPageUptodate(page);
166 if (!page_has_buffers(page))
167 create_empty_buffers(page, 1 << inode->i_blkbits,
170 bh = page_buffers(page);
172 if (!buffer_mapped(bh))
173 map_bh(bh, inode->i_sb, block);
175 set_buffer_uptodate(bh);
176 if ((sdp->sd_args.ar_data == GFS2_DATA_ORDERED) || gfs2_is_jdata(ip))
177 gfs2_trans_add_bh(ip->i_gl, bh, 0);
178 mark_buffer_dirty(bh);
182 page_cache_release(page);
189 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
191 * This is partly borrowed from ext3.
193 int gfs2_block_truncate_page(struct address_space *mapping)
195 struct inode *inode = mapping->host;
196 struct gfs2_inode *ip = inode->u.generic_ip;
197 struct gfs2_sbd *sdp = ip->i_sbd;
198 loff_t from = inode->i_size;
199 unsigned long index = from >> PAGE_CACHE_SHIFT;
200 unsigned offset = from & (PAGE_CACHE_SIZE-1);
201 unsigned blocksize, iblock, length, pos;
202 struct buffer_head *bh;
207 page = grab_cache_page(mapping, index);
211 blocksize = inode->i_sb->s_blocksize;
212 length = blocksize - (offset & (blocksize - 1));
213 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
215 if (!page_has_buffers(page))
216 create_empty_buffers(page, blocksize, 0);
218 /* Find the buffer that contains "offset" */
219 bh = page_buffers(page);
221 while (offset >= pos) {
222 bh = bh->b_this_page;
229 if (!buffer_mapped(bh)) {
230 gfs2_get_block(inode, iblock, bh, 0);
231 /* unmapped? It's a hole - nothing to do */
232 if (!buffer_mapped(bh))
236 /* Ok, it's mapped. Make sure it's up-to-date */
237 if (PageUptodate(page))
238 set_buffer_uptodate(bh);
240 if (!buffer_uptodate(bh)) {
242 ll_rw_block(READ, 1, &bh);
244 /* Uhhuh. Read error. Complain and punt. */
245 if (!buffer_uptodate(bh))
249 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
250 gfs2_trans_add_bh(ip->i_gl, bh, 0);
252 kaddr = kmap_atomic(page, KM_USER0);
253 memset(kaddr + offset, 0, length);
254 flush_dcache_page(page);
255 kunmap_atomic(kaddr, KM_USER0);
259 page_cache_release(page);
263 void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
264 unsigned int from, unsigned int to)
266 struct buffer_head *head = page_buffers(page);
267 unsigned int bsize = head->b_size;
268 struct buffer_head *bh;
269 unsigned int start, end;
271 for (bh = head, start = 0;
272 bh != head || !start;
273 bh = bh->b_this_page, start = end) {
275 if (end <= from || start >= to)
277 gfs2_trans_add_bh(ip->i_gl, bh, 0);