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 <asm/semaphore.h>
24 #include "ops_address.h"
27 * gfs2_pte_inval - Sync and invalidate all PTEs associated with a glock
32 void gfs2_pte_inval(struct gfs2_glock *gl)
34 struct gfs2_inode *ip;
38 if (!ip || !S_ISREG(ip->i_di.di_mode))
41 if (!test_bit(GIF_PAGED, &ip->i_flags))
44 inode = gfs2_ip2v_lookup(ip);
46 unmap_shared_mapping_range(inode->i_mapping, 0, 0);
49 if (test_bit(GIF_SW_PAGED, &ip->i_flags))
50 set_bit(GLF_DIRTY, &gl->gl_flags);
53 clear_bit(GIF_SW_PAGED, &ip->i_flags);
57 * gfs2_page_inval - Invalidate all pages associated with a glock
62 void gfs2_page_inval(struct gfs2_glock *gl)
64 struct gfs2_inode *ip;
68 if (!ip || !S_ISREG(ip->i_di.di_mode))
71 inode = gfs2_ip2v_lookup(ip);
73 struct address_space *mapping = inode->i_mapping;
75 truncate_inode_pages(mapping, 0);
76 gfs2_assert_withdraw(ip->i_sbd, !mapping->nrpages);
81 clear_bit(GIF_PAGED, &ip->i_flags);
85 * gfs2_page_sync - Sync the data pages (not metadata) associated with a glock
87 * @flags: DIO_START | DIO_WAIT
89 * Syncs data (not metadata) for a regular file.
90 * No-op for all other types.
93 void gfs2_page_sync(struct gfs2_glock *gl, int flags)
95 struct gfs2_inode *ip;
99 if (!ip || !S_ISREG(ip->i_di.di_mode))
102 inode = gfs2_ip2v_lookup(ip);
104 struct address_space *mapping = inode->i_mapping;
107 if (flags & DIO_START)
108 filemap_fdatawrite(mapping);
109 if (!error && (flags & DIO_WAIT))
110 error = filemap_fdatawait(mapping);
112 /* Put back any errors cleared by filemap_fdatawait()
113 so they can be caught by someone who can pass them
116 if (error == -ENOSPC)
117 set_bit(AS_ENOSPC, &mapping->flags);
119 set_bit(AS_EIO, &mapping->flags);
126 * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
128 * @dibh: the dinode buffer
129 * @block: the block number that was allocated
130 * @private: any locked page held by the caller process
135 int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
136 uint64_t block, void *private)
138 struct gfs2_sbd *sdp = ip->i_sbd;
139 struct inode *inode = ip->i_vnode;
140 struct page *page = (struct page *)private;
141 struct buffer_head *bh;
144 if (!page || page->index) {
145 page = grab_cache_page(inode->i_mapping, 0);
151 if (!PageUptodate(page)) {
152 void *kaddr = kmap(page);
155 dibh->b_data + sizeof(struct gfs2_dinode),
157 memset(kaddr + ip->i_di.di_size,
159 PAGE_CACHE_SIZE - ip->i_di.di_size);
162 SetPageUptodate(page);
165 if (!page_has_buffers(page))
166 create_empty_buffers(page, 1 << inode->i_blkbits,
169 bh = page_buffers(page);
171 if (!buffer_mapped(bh))
172 map_bh(bh, inode->i_sb, block);
174 set_buffer_uptodate(bh);
175 if ((sdp->sd_args.ar_data == GFS2_DATA_ORDERED) || gfs2_is_jdata(ip))
176 gfs2_trans_add_bh(ip->i_gl, bh, 0);
177 mark_buffer_dirty(bh);
181 page_cache_release(page);
188 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
190 * This is partly borrowed from ext3.
192 int gfs2_block_truncate_page(struct address_space *mapping)
194 struct inode *inode = mapping->host;
195 struct gfs2_inode *ip = get_v2ip(inode);
196 struct gfs2_sbd *sdp = ip->i_sbd;
197 loff_t from = inode->i_size;
198 unsigned long index = from >> PAGE_CACHE_SHIFT;
199 unsigned offset = from & (PAGE_CACHE_SIZE-1);
200 unsigned blocksize, iblock, length, pos;
201 struct buffer_head *bh;
206 page = grab_cache_page(mapping, index);
210 blocksize = inode->i_sb->s_blocksize;
211 length = blocksize - (offset & (blocksize - 1));
212 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
214 if (!page_has_buffers(page))
215 create_empty_buffers(page, blocksize, 0);
217 /* Find the buffer that contains "offset" */
218 bh = page_buffers(page);
220 while (offset >= pos) {
221 bh = bh->b_this_page;
228 if (!buffer_mapped(bh)) {
229 gfs2_get_block(inode, iblock, bh, 0);
230 /* unmapped? It's a hole - nothing to do */
231 if (!buffer_mapped(bh))
235 /* Ok, it's mapped. Make sure it's up-to-date */
236 if (PageUptodate(page))
237 set_buffer_uptodate(bh);
239 if (!buffer_uptodate(bh)) {
241 ll_rw_block(READ, 1, &bh);
243 /* Uhhuh. Read error. Complain and punt. */
244 if (!buffer_uptodate(bh))
248 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
249 gfs2_trans_add_bh(ip->i_gl, bh, 0);
251 kaddr = kmap_atomic(page, KM_USER0);
252 memset(kaddr + offset, 0, length);
253 flush_dcache_page(page);
254 kunmap_atomic(kaddr, KM_USER0);
258 page_cache_release(page);
262 void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
263 unsigned int from, unsigned int to)
265 struct buffer_head *head = page_buffers(page);
266 unsigned int bsize = head->b_size;
267 struct buffer_head *bh;
268 unsigned int start, end;
270 for (bh = head, start = 0;
271 bh != head || !start;
272 bh = bh->b_this_page, start = end) {
274 if (end <= from || start >= to)
276 gfs2_trans_add_bh(ip->i_gl, bh, 0);