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>
26 * gfs2_pte_inval - Sync and invalidate all PTEs associated with a glock
31 void gfs2_pte_inval(struct gfs2_glock
*gl
)
33 struct gfs2_inode
*ip
;
37 if (!ip
|| !S_ISREG(ip
->i_di
.di_mode
))
40 if (!test_bit(GIF_PAGED
, &ip
->i_flags
))
43 inode
= gfs2_ip2v_lookup(ip
);
45 unmap_shared_mapping_range(inode
->i_mapping
, 0, 0);
48 if (test_bit(GIF_SW_PAGED
, &ip
->i_flags
))
49 set_bit(GLF_DIRTY
, &gl
->gl_flags
);
52 clear_bit(GIF_SW_PAGED
, &ip
->i_flags
);
56 * gfs2_page_inval - Invalidate all pages associated with a glock
61 void gfs2_page_inval(struct gfs2_glock
*gl
)
63 struct gfs2_inode
*ip
;
67 if (!ip
|| !S_ISREG(ip
->i_di
.di_mode
))
70 inode
= gfs2_ip2v_lookup(ip
);
72 struct address_space
*mapping
= inode
->i_mapping
;
74 truncate_inode_pages(mapping
, 0);
75 gfs2_assert_withdraw(ip
->i_sbd
, !mapping
->nrpages
);
80 clear_bit(GIF_PAGED
, &ip
->i_flags
);
84 * gfs2_page_sync - Sync the data pages (not metadata) associated with a glock
86 * @flags: DIO_START | DIO_WAIT
88 * Syncs data (not metadata) for a regular file.
89 * No-op for all other types.
92 void gfs2_page_sync(struct gfs2_glock
*gl
, int flags
)
94 struct gfs2_inode
*ip
;
98 if (!ip
|| !S_ISREG(ip
->i_di
.di_mode
))
101 inode
= gfs2_ip2v_lookup(ip
);
103 struct address_space
*mapping
= inode
->i_mapping
;
106 if (flags
& DIO_START
)
107 filemap_fdatawrite(mapping
);
108 if (!error
&& (flags
& DIO_WAIT
))
109 error
= filemap_fdatawait(mapping
);
111 /* Put back any errors cleared by filemap_fdatawait()
112 so they can be caught by someone who can pass them
115 if (error
== -ENOSPC
)
116 set_bit(AS_ENOSPC
, &mapping
->flags
);
118 set_bit(AS_EIO
, &mapping
->flags
);
125 * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
127 * @dibh: the dinode buffer
128 * @block: the block number that was allocated
129 * @private: any locked page held by the caller process
134 int gfs2_unstuffer_page(struct gfs2_inode
*ip
, struct buffer_head
*dibh
,
135 uint64_t block
, void *private)
137 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
138 struct inode
*inode
= ip
->i_vnode
;
139 struct page
*page
= (struct page
*)private;
140 struct buffer_head
*bh
;
143 if (!page
|| page
->index
) {
144 page
= grab_cache_page(inode
->i_mapping
, 0);
150 if (!PageUptodate(page
)) {
151 void *kaddr
= kmap(page
);
154 dibh
->b_data
+ sizeof(struct gfs2_dinode
),
156 memset(kaddr
+ ip
->i_di
.di_size
,
158 PAGE_CACHE_SIZE
- ip
->i_di
.di_size
);
161 SetPageUptodate(page
);
164 if (!page_has_buffers(page
))
165 create_empty_buffers(page
, 1 << inode
->i_blkbits
,
168 bh
= page_buffers(page
);
170 if (!buffer_mapped(bh
))
171 map_bh(bh
, inode
->i_sb
, block
);
173 set_buffer_uptodate(bh
);
174 if (sdp
->sd_args
.ar_data
== GFS2_DATA_ORDERED
)
175 gfs2_trans_add_databuf(sdp
, bh
);
176 mark_buffer_dirty(bh
);
180 page_cache_release(page
);
187 * gfs2_truncator_page - truncate a partial data block in the page cache
189 * @size: the size the file should be
194 int gfs2_truncator_page(struct gfs2_inode
*ip
, uint64_t size
)
196 struct gfs2_sbd
*sdp
= ip
->i_sbd
;
197 struct inode
*inode
= ip
->i_vnode
;
199 struct buffer_head
*bh
;
208 lbn
= size
>> inode
->i_blkbits
;
209 error
= gfs2_block_map(ip
, lbn
, &new, &dbn
, NULL
);
213 index
= size
>> PAGE_CACHE_SHIFT
;
214 offset
= size
& (PAGE_CACHE_SIZE
- 1);
215 bufnum
= lbn
- (index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
));
217 page
= read_cache_page(inode
->i_mapping
, index
,
218 (filler_t
*)inode
->i_mapping
->a_ops
->readpage
,
221 return PTR_ERR(page
);
225 if (!PageUptodate(page
) || PageError(page
)) {
231 memset(kaddr
+ offset
, 0, PAGE_CACHE_SIZE
- offset
);
234 if (!page_has_buffers(page
))
235 create_empty_buffers(page
, 1 << inode
->i_blkbits
,
238 for (bh
= page_buffers(page
); bufnum
--; bh
= bh
->b_this_page
)
241 if (!buffer_mapped(bh
))
242 map_bh(bh
, inode
->i_sb
, dbn
);
244 set_buffer_uptodate(bh
);
245 if (sdp
->sd_args
.ar_data
== GFS2_DATA_ORDERED
)
246 gfs2_trans_add_databuf(sdp
, bh
);
247 mark_buffer_dirty(bh
);
251 page_cache_release(page
);
256 void gfs2_page_add_databufs(struct gfs2_sbd
*sdp
, struct page
*page
,
257 unsigned int from
, unsigned int to
)
259 struct buffer_head
*head
= page_buffers(page
);
260 unsigned int bsize
= head
->b_size
;
261 struct buffer_head
*bh
;
262 unsigned int start
, end
;
264 for (bh
= head
, start
= 0;
265 bh
!= head
|| !start
;
266 bh
= bh
->b_this_page
, start
= end
) {
268 if (end
<= from
|| start
>= to
)
270 gfs2_trans_add_databuf(sdp
, bh
);