2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 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
;
42 if (!ip
|| !S_ISREG(ip
->i_di
.di_mode
))
45 if (!test_bit(GIF_PAGED
, &ip
->i_flags
))
48 unmap_shared_mapping_range(inode
->i_mapping
, 0, 0);
50 if (test_bit(GIF_SW_PAGED
, &ip
->i_flags
))
51 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
;
69 if (!ip
|| !S_ISREG(ip
->i_di
.di_mode
))
72 truncate_inode_pages(inode
->i_mapping
, 0);
73 gfs2_assert_withdraw(GFS2_SB(&ip
->i_inode
), !inode
->i_mapping
->nrpages
);
74 clear_bit(GIF_PAGED
, &ip
->i_flags
);
78 * gfs2_page_sync - Sync the data pages (not metadata) associated with a glock
80 * @flags: DIO_START | DIO_WAIT
82 * Syncs data (not metadata) for a regular file.
83 * No-op for all other types.
86 void gfs2_page_sync(struct gfs2_glock
*gl
, int flags
)
88 struct gfs2_inode
*ip
;
90 struct address_space
*mapping
;
95 if (!ip
|| !S_ISREG(ip
->i_di
.di_mode
))
98 mapping
= inode
->i_mapping
;
100 if (flags
& DIO_START
)
101 filemap_fdatawrite(mapping
);
102 if (!error
&& (flags
& DIO_WAIT
))
103 error
= filemap_fdatawait(mapping
);
105 /* Put back any errors cleared by filemap_fdatawait()
106 so they can be caught by someone who can pass them
109 if (error
== -ENOSPC
)
110 set_bit(AS_ENOSPC
, &mapping
->flags
);
112 set_bit(AS_EIO
, &mapping
->flags
);
117 * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
119 * @dibh: the dinode buffer
120 * @block: the block number that was allocated
121 * @private: any locked page held by the caller process
126 int gfs2_unstuffer_page(struct gfs2_inode
*ip
, struct buffer_head
*dibh
,
127 uint64_t block
, void *private)
129 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
130 struct inode
*inode
= &ip
->i_inode
;
131 struct page
*page
= (struct page
*)private;
132 struct buffer_head
*bh
;
135 if (!page
|| page
->index
) {
136 page
= grab_cache_page(inode
->i_mapping
, 0);
142 if (!PageUptodate(page
)) {
143 void *kaddr
= kmap(page
);
145 memcpy(kaddr
, dibh
->b_data
+ sizeof(struct gfs2_dinode
),
147 memset(kaddr
+ ip
->i_di
.di_size
, 0,
148 PAGE_CACHE_SIZE
- ip
->i_di
.di_size
);
151 SetPageUptodate(page
);
154 if (!page_has_buffers(page
))
155 create_empty_buffers(page
, 1 << inode
->i_blkbits
,
158 bh
= page_buffers(page
);
160 if (!buffer_mapped(bh
))
161 map_bh(bh
, inode
->i_sb
, block
);
163 set_buffer_uptodate(bh
);
164 if ((sdp
->sd_args
.ar_data
== GFS2_DATA_ORDERED
) || gfs2_is_jdata(ip
))
165 gfs2_trans_add_bh(ip
->i_gl
, bh
, 0);
166 mark_buffer_dirty(bh
);
170 page_cache_release(page
);
177 * gfs2_block_truncate_page - Deal with zeroing out data for truncate
179 * This is partly borrowed from ext3.
181 int gfs2_block_truncate_page(struct address_space
*mapping
)
183 struct inode
*inode
= mapping
->host
;
184 struct gfs2_inode
*ip
= GFS2_I(inode
);
185 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
186 loff_t from
= inode
->i_size
;
187 unsigned long index
= from
>> PAGE_CACHE_SHIFT
;
188 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
189 unsigned blocksize
, iblock
, length
, pos
;
190 struct buffer_head
*bh
;
195 page
= grab_cache_page(mapping
, index
);
199 blocksize
= inode
->i_sb
->s_blocksize
;
200 length
= blocksize
- (offset
& (blocksize
- 1));
201 iblock
= index
<< (PAGE_CACHE_SHIFT
- inode
->i_sb
->s_blocksize_bits
);
203 if (!page_has_buffers(page
))
204 create_empty_buffers(page
, blocksize
, 0);
206 /* Find the buffer that contains "offset" */
207 bh
= page_buffers(page
);
209 while (offset
>= pos
) {
210 bh
= bh
->b_this_page
;
217 if (!buffer_mapped(bh
)) {
218 gfs2_get_block(inode
, iblock
, bh
, 0);
219 /* unmapped? It's a hole - nothing to do */
220 if (!buffer_mapped(bh
))
224 /* Ok, it's mapped. Make sure it's up-to-date */
225 if (PageUptodate(page
))
226 set_buffer_uptodate(bh
);
228 if (!buffer_uptodate(bh
)) {
230 ll_rw_block(READ
, 1, &bh
);
232 /* Uhhuh. Read error. Complain and punt. */
233 if (!buffer_uptodate(bh
))
237 if (sdp
->sd_args
.ar_data
== GFS2_DATA_ORDERED
|| gfs2_is_jdata(ip
))
238 gfs2_trans_add_bh(ip
->i_gl
, bh
, 0);
240 kaddr
= kmap_atomic(page
, KM_USER0
);
241 memset(kaddr
+ offset
, 0, length
);
242 flush_dcache_page(page
);
243 kunmap_atomic(kaddr
, KM_USER0
);
247 page_cache_release(page
);
251 void gfs2_page_add_databufs(struct gfs2_inode
*ip
, struct page
*page
,
252 unsigned int from
, unsigned int to
)
254 struct buffer_head
*head
= page_buffers(page
);
255 unsigned int bsize
= head
->b_size
;
256 struct buffer_head
*bh
;
257 unsigned int start
, end
;
259 for (bh
= head
, start
= 0; bh
!= head
|| !start
;
260 bh
= bh
->b_this_page
, start
= end
) {
262 if (end
<= from
|| start
>= to
)
264 gfs2_trans_add_bh(ip
->i_gl
, bh
, 0);