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>
18 #include <asm/semaphore.h>
21 #include "lm_interface.h"
27 #include "ops_address.h"
31 * gfs2_pte_inval - Sync and invalidate all PTEs associated with a glock
36 void gfs2_pte_inval(struct gfs2_glock *gl)
38 struct gfs2_inode *ip;
42 if (!ip || !S_ISREG(ip->i_di.di_mode))
45 if (!test_bit(GIF_PAGED, &ip->i_flags))
48 inode = gfs2_ip2v_lookup(ip);
50 unmap_shared_mapping_range(inode->i_mapping, 0, 0);
53 if (test_bit(GIF_SW_PAGED, &ip->i_flags))
54 set_bit(GLF_DIRTY, &gl->gl_flags);
57 clear_bit(GIF_SW_PAGED, &ip->i_flags);
61 * gfs2_page_inval - Invalidate all pages associated with a glock
66 void gfs2_page_inval(struct gfs2_glock *gl)
68 struct gfs2_inode *ip;
72 if (!ip || !S_ISREG(ip->i_di.di_mode))
75 inode = gfs2_ip2v_lookup(ip);
77 struct address_space *mapping = inode->i_mapping;
79 truncate_inode_pages(mapping, 0);
80 gfs2_assert_withdraw(ip->i_sbd, !mapping->nrpages);
85 clear_bit(GIF_PAGED, &ip->i_flags);
89 * gfs2_page_sync - Sync the data pages (not metadata) associated with a glock
91 * @flags: DIO_START | DIO_WAIT
93 * Syncs data (not metadata) for a regular file.
94 * No-op for all other types.
97 void gfs2_page_sync(struct gfs2_glock *gl, int flags)
99 struct gfs2_inode *ip;
103 if (!ip || !S_ISREG(ip->i_di.di_mode))
106 inode = gfs2_ip2v_lookup(ip);
108 struct address_space *mapping = inode->i_mapping;
111 if (flags & DIO_START)
112 filemap_fdatawrite(mapping);
113 if (!error && (flags & DIO_WAIT))
114 error = filemap_fdatawait(mapping);
116 /* Put back any errors cleared by filemap_fdatawait()
117 so they can be caught by someone who can pass them
120 if (error == -ENOSPC)
121 set_bit(AS_ENOSPC, &mapping->flags);
123 set_bit(AS_EIO, &mapping->flags);
130 * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
132 * @dibh: the dinode buffer
133 * @block: the block number that was allocated
134 * @private: any locked page held by the caller process
139 int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
140 uint64_t block, void *private)
142 struct gfs2_sbd *sdp = ip->i_sbd;
143 struct inode *inode = ip->i_vnode;
144 struct page *page = (struct page *)private;
145 struct buffer_head *bh;
148 if (!page || page->index) {
149 page = grab_cache_page(inode->i_mapping, 0);
155 if (!PageUptodate(page)) {
156 void *kaddr = kmap(page);
159 dibh->b_data + sizeof(struct gfs2_dinode),
161 memset(kaddr + ip->i_di.di_size,
163 PAGE_CACHE_SIZE - ip->i_di.di_size);
166 SetPageUptodate(page);
169 if (!page_has_buffers(page))
170 create_empty_buffers(page, 1 << inode->i_blkbits,
173 bh = page_buffers(page);
175 if (!buffer_mapped(bh))
176 map_bh(bh, inode->i_sb, block);
178 set_buffer_uptodate(bh);
179 if ((sdp->sd_args.ar_data == GFS2_DATA_ORDERED) || gfs2_is_jdata(ip))
180 gfs2_trans_add_bh(ip->i_gl, bh, 0);
181 mark_buffer_dirty(bh);
185 page_cache_release(page);
192 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
194 * This is partly borrowed from ext3.
196 int gfs2_block_truncate_page(struct address_space *mapping)
198 struct inode *inode = mapping->host;
199 struct gfs2_inode *ip = inode->u.generic_ip;
200 struct gfs2_sbd *sdp = ip->i_sbd;
201 loff_t from = inode->i_size;
202 unsigned long index = from >> PAGE_CACHE_SHIFT;
203 unsigned offset = from & (PAGE_CACHE_SIZE-1);
204 unsigned blocksize, iblock, length, pos;
205 struct buffer_head *bh;
210 page = grab_cache_page(mapping, index);
214 blocksize = inode->i_sb->s_blocksize;
215 length = blocksize - (offset & (blocksize - 1));
216 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
218 if (!page_has_buffers(page))
219 create_empty_buffers(page, blocksize, 0);
221 /* Find the buffer that contains "offset" */
222 bh = page_buffers(page);
224 while (offset >= pos) {
225 bh = bh->b_this_page;
232 if (!buffer_mapped(bh)) {
233 gfs2_get_block(inode, iblock, bh, 0);
234 /* unmapped? It's a hole - nothing to do */
235 if (!buffer_mapped(bh))
239 /* Ok, it's mapped. Make sure it's up-to-date */
240 if (PageUptodate(page))
241 set_buffer_uptodate(bh);
243 if (!buffer_uptodate(bh)) {
245 ll_rw_block(READ, 1, &bh);
247 /* Uhhuh. Read error. Complain and punt. */
248 if (!buffer_uptodate(bh))
252 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
253 gfs2_trans_add_bh(ip->i_gl, bh, 0);
255 kaddr = kmap_atomic(page, KM_USER0);
256 memset(kaddr + offset, 0, length);
257 flush_dcache_page(page);
258 kunmap_atomic(kaddr, KM_USER0);
262 page_cache_release(page);
266 void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
267 unsigned int from, unsigned int to)
269 struct buffer_head *head = page_buffers(page);
270 unsigned int bsize = head->b_size;
271 struct buffer_head *bh;
272 unsigned int start, end;
274 for (bh = head, start = 0;
275 bh != head || !start;
276 bh = bh->b_this_page, start = end) {
278 if (end <= from || start >= to)
280 gfs2_trans_add_bh(ip->i_gl, bh, 0);