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 version 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>
16 #include <linux/pagemap.h>
17 #include <linux/writeback.h>
18 #include <linux/swap.h>
19 #include <linux/delay.h>
20 #include <linux/bio.h>
21 #include <linux/gfs2_ondisk.h>
22 #include <linux/lm_interface.h>
35 #include "ops_address.h"
37 static int aspace_get_block(struct inode *inode, sector_t lblock,
38 struct buffer_head *bh_result, int create)
40 gfs2_assert_warn(inode->i_sb->s_fs_info, 0);
44 static int gfs2_aspace_writepage(struct page *page,
45 struct writeback_control *wbc)
47 return block_write_full_page(page, aspace_get_block, wbc);
50 static const struct address_space_operations aspace_aops = {
51 .writepage = gfs2_aspace_writepage,
52 .releasepage = gfs2_releasepage,
56 * gfs2_aspace_get - Create and initialize a struct inode structure
57 * @sdp: the filesystem the aspace is in
59 * Right now a struct inode is just a struct inode. Maybe Linux
60 * will supply a more lightweight address space construct (that works)
63 * Make sure pages/buffers in this aspace aren't in high memory.
68 struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp)
72 aspace = new_inode(sdp->sd_vfs);
74 mapping_set_gfp_mask(aspace->i_mapping, GFP_NOFS);
75 aspace->i_mapping->a_ops = &aspace_aops;
76 aspace->i_size = ~0ULL;
77 aspace->i_private = NULL;
78 insert_inode_hash(aspace);
83 void gfs2_aspace_put(struct inode *aspace)
85 remove_inode_hash(aspace);
90 * gfs2_meta_inval - Invalidate all buffers associated with a glock
95 void gfs2_meta_inval(struct gfs2_glock *gl)
97 struct gfs2_sbd *sdp = gl->gl_sbd;
98 struct inode *aspace = gl->gl_aspace;
99 struct address_space *mapping = gl->gl_aspace->i_mapping;
101 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
103 atomic_inc(&aspace->i_writecount);
104 truncate_inode_pages(mapping, 0);
105 atomic_dec(&aspace->i_writecount);
107 gfs2_assert_withdraw(sdp, !mapping->nrpages);
111 * gfs2_meta_sync - Sync all buffers associated with a glock
116 void gfs2_meta_sync(struct gfs2_glock *gl)
118 struct address_space *mapping = gl->gl_aspace->i_mapping;
121 filemap_fdatawrite(mapping);
122 error = filemap_fdatawait(mapping);
125 gfs2_io_error(gl->gl_sbd);
129 * getbuf - Get a buffer with a given address space
131 * @blkno: the block number (filesystem scope)
132 * @create: 1 if the buffer should be created
134 * Returns: the buffer
137 static struct buffer_head *getbuf(struct gfs2_glock *gl, u64 blkno, int create)
139 struct address_space *mapping = gl->gl_aspace->i_mapping;
140 struct gfs2_sbd *sdp = gl->gl_sbd;
142 struct buffer_head *bh;
147 shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift;
148 index = blkno >> shift; /* convert block to page */
149 bufnum = blkno - (index << shift); /* block buf index within page */
153 page = grab_cache_page(mapping, index);
159 page = find_lock_page(mapping, index);
164 if (!page_has_buffers(page))
165 create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
167 /* Locate header for our buffer within our page */
168 for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
172 if (!buffer_mapped(bh))
173 map_bh(bh, sdp->sd_vfs, blkno);
176 mark_page_accessed(page);
177 page_cache_release(page);
182 static void meta_prep_new(struct buffer_head *bh)
184 struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
187 clear_buffer_dirty(bh);
188 set_buffer_uptodate(bh);
191 mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
195 * gfs2_meta_new - Get a block
196 * @gl: The glock associated with this block
197 * @blkno: The block number
199 * Returns: The buffer
202 struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
204 struct buffer_head *bh;
205 bh = getbuf(gl, blkno, CREATE);
211 * gfs2_meta_read - Read a block from disk
212 * @gl: The glock covering the block
213 * @blkno: The block number
215 * @bhp: the place where the buffer is returned (NULL on failure)
220 int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags,
221 struct buffer_head **bhp)
223 *bhp = getbuf(gl, blkno, CREATE);
224 if (!buffer_uptodate(*bhp))
225 ll_rw_block(READ_META, 1, bhp);
226 if (flags & DIO_WAIT) {
227 int error = gfs2_meta_wait(gl->gl_sbd, *bhp);
238 * gfs2_meta_wait - Reread a block from disk
239 * @sdp: the filesystem
240 * @bh: The block to wait for
245 int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh)
247 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
252 if (!buffer_uptodate(bh)) {
253 struct gfs2_trans *tr = current->journal_info;
254 if (tr && tr->tr_touched)
255 gfs2_io_error_bh(sdp, bh);
258 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
265 * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
266 * @gl: the glock the buffer belongs to
267 * @bh: The buffer to be attached to
268 * @meta: Flag to indicate whether its metadata or not
271 void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh,
274 struct gfs2_bufdata *bd;
277 lock_page(bh->b_page);
281 unlock_page(bh->b_page);
285 bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL),
289 INIT_LIST_HEAD(&bd->bd_list_tr);
291 lops_init_le(&bd->bd_le, &gfs2_buf_lops);
293 lops_init_le(&bd->bd_le, &gfs2_databuf_lops);
297 unlock_page(bh->b_page);
301 * gfs2_pin - Pin a buffer in memory
302 * @sdp: the filesystem the buffer belongs to
303 * @bh: The buffer to be pinned
307 void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
309 struct gfs2_bufdata *bd = bh->b_private;
311 gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
313 if (test_set_buffer_pinned(bh))
314 gfs2_assert_withdraw(sdp, 0);
318 /* If this buffer is in the AIL and it has already been written
319 to in-place disk block, remove it from the AIL. */
322 if (bd->bd_ail && !buffer_in_io(bh))
323 list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
324 gfs2_log_unlock(sdp);
326 clear_buffer_dirty(bh);
329 if (!buffer_uptodate(bh))
330 gfs2_io_error_bh(sdp, bh);
336 * gfs2_unpin - Unpin a buffer
337 * @sdp: the filesystem the buffer belongs to
338 * @bh: The buffer to unpin
343 void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
346 struct gfs2_bufdata *bd = bh->b_private;
348 gfs2_assert_withdraw(sdp, buffer_uptodate(bh));
350 if (!buffer_pinned(bh))
351 gfs2_assert_withdraw(sdp, 0);
353 mark_buffer_dirty(bh);
354 clear_buffer_pinned(bh);
358 list_del(&bd->bd_ail_st_list);
361 struct gfs2_glock *gl = bd->bd_gl;
362 list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
363 atomic_inc(&gl->gl_ail_count);
366 list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
367 gfs2_log_unlock(sdp);
371 * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
372 * @ip: the inode who owns the buffers
373 * @bstart: the first buffer in the run
374 * @blen: the number of buffers in the run
378 void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
380 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
381 struct buffer_head *bh;
384 bh = getbuf(ip->i_gl, bstart, NO_CREATE);
386 struct gfs2_bufdata *bd = bh->b_private;
388 if (test_clear_buffer_pinned(bh)) {
389 struct gfs2_trans *tr = current->journal_info;
390 struct gfs2_inode *bh_ip =
391 GFS2_I(bh->b_page->mapping->host);
394 list_del_init(&bd->bd_le.le_list);
395 gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
396 sdp->sd_log_num_buf--;
397 gfs2_log_unlock(sdp);
398 if (bh_ip->i_inode.i_private != NULL)
399 tr->tr_num_databuf_rm++;
407 u64 blkno = bh->b_blocknr;
409 list_del(&bd->bd_ail_st_list);
410 list_del(&bd->bd_ail_gl_list);
411 atomic_dec(&bd->bd_gl->gl_ail_count);
413 gfs2_log_unlock(sdp);
414 gfs2_trans_add_revoke(sdp, blkno);
416 gfs2_log_unlock(sdp);
420 clear_buffer_dirty(bh);
421 clear_buffer_uptodate(bh);
433 * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
434 * @ip: The GFS2 inode
436 * This releases buffers that are in the most-recently-used array of
437 * blocks used for indirect block addressing for this inode.
440 void gfs2_meta_cache_flush(struct gfs2_inode *ip)
442 struct buffer_head **bh_slot;
445 spin_lock(&ip->i_spin);
447 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
448 bh_slot = &ip->i_cache[x];
455 spin_unlock(&ip->i_spin);
459 * gfs2_meta_indirect_buffer - Get a metadata buffer
460 * @ip: The GFS2 inode
461 * @height: The level of this buf in the metadata (indir addr) tree (if any)
462 * @num: The block number (device relative) of the buffer
463 * @new: Non-zero if we may create a new buffer
464 * @bhp: the buffer is returned here
466 * Try to use the gfs2_inode's MRU metadata tree cache.
471 int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num,
472 int new, struct buffer_head **bhp)
474 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
475 struct gfs2_glock *gl = ip->i_gl;
476 struct buffer_head *bh = NULL, **bh_slot = ip->i_cache + height;
482 spin_lock(&ip->i_spin);
483 if (*bh_slot && (*bh_slot)->b_blocknr == num) {
488 spin_unlock(&ip->i_spin);
491 bh = getbuf(gl, num, CREATE);
497 if (gfs2_assert_warn(sdp, height))
500 gfs2_trans_add_bh(ip->i_gl, bh, 1);
501 gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
502 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
504 u32 mtype = height ? GFS2_METATYPE_IN : GFS2_METATYPE_DI;
505 if (!buffer_uptodate(bh)) {
506 ll_rw_block(READ_META, 1, &bh);
507 if (gfs2_meta_wait(sdp, bh))
510 if (gfs2_metatype_check(sdp, bh, mtype))
515 spin_lock(&ip->i_spin);
520 spin_unlock(&ip->i_spin);
531 * gfs2_meta_ra - start readahead on an extent of a file
532 * @gl: the glock the blocks belong to
533 * @dblock: the starting disk block
534 * @extlen: the number of blocks in the extent
536 * returns: the first buffer in the extent
539 struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
541 struct gfs2_sbd *sdp = gl->gl_sbd;
542 struct buffer_head *first_bh, *bh;
543 u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
544 sdp->sd_sb.sb_bsize_shift;
553 first_bh = getbuf(gl, dblock, CREATE);
555 if (buffer_uptodate(first_bh))
557 if (!buffer_locked(first_bh))
558 ll_rw_block(READ_META, 1, &first_bh);
564 bh = getbuf(gl, dblock, CREATE);
566 if (!buffer_uptodate(bh) && !buffer_locked(bh))
567 ll_rw_block(READA, 1, &bh);
571 if (!buffer_locked(first_bh) && buffer_uptodate(first_bh))
575 wait_on_buffer(first_bh);