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
);
158 memcpy(kaddr
, dibh
->b_data
+ sizeof(struct gfs2_dinode
),
160 memset(kaddr
+ ip
->i_di
.di_size
, 0,
161 PAGE_CACHE_SIZE
- ip
->i_di
.di_size
);
164 SetPageUptodate(page
);
167 if (!page_has_buffers(page
))
168 create_empty_buffers(page
, 1 << inode
->i_blkbits
,
171 bh
= page_buffers(page
);
173 if (!buffer_mapped(bh
))
174 map_bh(bh
, inode
->i_sb
, block
);
176 set_buffer_uptodate(bh
);
177 if ((sdp
->sd_args
.ar_data
== GFS2_DATA_ORDERED
) || gfs2_is_jdata(ip
))
178 gfs2_trans_add_bh(ip
->i_gl
, bh
, 0);
179 mark_buffer_dirty(bh
);
183 page_cache_release(page
);
190 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
192 * This is partly borrowed from ext3.
194 int gfs2_block_truncate_page(struct address_space
*mapping
)
196 struct inode
*inode
= mapping
->host
;
197 struct gfs2_inode
*ip
= inode
->u
.generic_ip
;
198 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
199 loff_t from
= inode
->i_size
;
200 unsigned long index
= from
>> PAGE_CACHE_SHIFT
;
201 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
202 unsigned blocksize
, iblock
, length
, pos
;
203 struct buffer_head
*bh
;
208 page
= grab_cache_page(mapping
, index
);
212 blocksize
= inode
->i_sb
->s_blocksize
;
213 length
= blocksize
- (offset
& (blocksize
- 1));
214 iblock
= index
<< (PAGE_CACHE_SHIFT
- inode
->i_sb
->s_blocksize_bits
);
216 if (!page_has_buffers(page
))
217 create_empty_buffers(page
, blocksize
, 0);
219 /* Find the buffer that contains "offset" */
220 bh
= page_buffers(page
);
222 while (offset
>= pos
) {
223 bh
= bh
->b_this_page
;
230 if (!buffer_mapped(bh
)) {
231 gfs2_get_block(inode
, iblock
, bh
, 0);
232 /* unmapped? It's a hole - nothing to do */
233 if (!buffer_mapped(bh
))
237 /* Ok, it's mapped. Make sure it's up-to-date */
238 if (PageUptodate(page
))
239 set_buffer_uptodate(bh
);
241 if (!buffer_uptodate(bh
)) {
243 ll_rw_block(READ
, 1, &bh
);
245 /* Uhhuh. Read error. Complain and punt. */
246 if (!buffer_uptodate(bh
))
250 if (sdp
->sd_args
.ar_data
== GFS2_DATA_ORDERED
|| gfs2_is_jdata(ip
))
251 gfs2_trans_add_bh(ip
->i_gl
, bh
, 0);
253 kaddr
= kmap_atomic(page
, KM_USER0
);
254 memset(kaddr
+ offset
, 0, length
);
255 flush_dcache_page(page
);
256 kunmap_atomic(kaddr
, KM_USER0
);
260 page_cache_release(page
);
264 void gfs2_page_add_databufs(struct gfs2_inode
*ip
, struct page
*page
,
265 unsigned int from
, unsigned int to
)
267 struct buffer_head
*head
= page_buffers(page
);
268 unsigned int bsize
= head
->b_size
;
269 struct buffer_head
*bh
;
270 unsigned int start
, end
;
272 for (bh
= head
, start
= 0;
273 bh
!= head
|| !start
;
274 bh
= bh
->b_this_page
, start
= end
) {
276 if (end
<= from
|| start
>= to
)
278 gfs2_trans_add_bh(ip
->i_gl
, bh
, 0);