]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/gfs2/meta_io.c
[GFS2] Use slab operations for all gfs2_bufdata allocations
[net-next-2.6.git] / fs / gfs2 / meta_io.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3a8a9a10 3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
b3b94faa
DT
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
e9fc2aa0 7 * of the GNU General Public License version 2.
b3b94faa
DT
8 */
9
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/mm.h>
16#include <linux/pagemap.h>
17#include <linux/writeback.h>
18#include <linux/swap.h>
19#include <linux/delay.h>
2e565bb6 20#include <linux/bio.h>
5c676f6d 21#include <linux/gfs2_ondisk.h>
7d308590 22#include <linux/lm_interface.h>
b3b94faa
DT
23
24#include "gfs2.h"
5c676f6d 25#include "incore.h"
b3b94faa
DT
26#include "glock.h"
27#include "glops.h"
28#include "inode.h"
29#include "log.h"
30#include "lops.h"
31#include "meta_io.h"
32#include "rgrp.h"
33#include "trans.h"
5c676f6d 34#include "util.h"
4340fe62 35#include "ops_address.h"
b3b94faa 36
b3b94faa
DT
37static int aspace_get_block(struct inode *inode, sector_t lblock,
38 struct buffer_head *bh_result, int create)
39{
5c676f6d 40 gfs2_assert_warn(inode->i_sb->s_fs_info, 0);
b3b94faa
DT
41 return -EOPNOTSUPP;
42}
43
44static int gfs2_aspace_writepage(struct page *page,
45 struct writeback_control *wbc)
46{
47 return block_write_full_page(page, aspace_get_block, wbc);
48}
49
66de045d 50static const struct address_space_operations aspace_aops = {
b3b94faa 51 .writepage = gfs2_aspace_writepage,
4340fe62 52 .releasepage = gfs2_releasepage,
b3b94faa
DT
53};
54
55/**
56 * gfs2_aspace_get - Create and initialize a struct inode structure
57 * @sdp: the filesystem the aspace is in
58 *
59 * Right now a struct inode is just a struct inode. Maybe Linux
60 * will supply a more lightweight address space construct (that works)
61 * in the future.
62 *
63 * Make sure pages/buffers in this aspace aren't in high memory.
64 *
65 * Returns: the aspace
66 */
67
68struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp)
69{
70 struct inode *aspace;
71
72 aspace = new_inode(sdp->sd_vfs);
73 if (aspace) {
f3bba03f 74 mapping_set_gfp_mask(aspace->i_mapping, GFP_NOFS);
b3b94faa
DT
75 aspace->i_mapping->a_ops = &aspace_aops;
76 aspace->i_size = ~0ULL;
bba9dfd8 77 aspace->i_private = NULL;
b3b94faa
DT
78 insert_inode_hash(aspace);
79 }
b3b94faa
DT
80 return aspace;
81}
82
83void gfs2_aspace_put(struct inode *aspace)
84{
85 remove_inode_hash(aspace);
86 iput(aspace);
87}
88
b3b94faa
DT
89/**
90 * gfs2_meta_inval - Invalidate all buffers associated with a glock
91 * @gl: the glock
92 *
93 */
94
95void gfs2_meta_inval(struct gfs2_glock *gl)
96{
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;
100
101 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
102
103 atomic_inc(&aspace->i_writecount);
104 truncate_inode_pages(mapping, 0);
105 atomic_dec(&aspace->i_writecount);
106
107 gfs2_assert_withdraw(sdp, !mapping->nrpages);
108}
109
110/**
111 * gfs2_meta_sync - Sync all buffers associated with a glock
112 * @gl: The glock
b3b94faa
DT
113 *
114 */
115
7276b3b0 116void gfs2_meta_sync(struct gfs2_glock *gl)
b3b94faa
DT
117{
118 struct address_space *mapping = gl->gl_aspace->i_mapping;
7276b3b0 119 int error;
b3b94faa 120
7276b3b0
SW
121 filemap_fdatawrite(mapping);
122 error = filemap_fdatawait(mapping);
b3b94faa
DT
123
124 if (error)
125 gfs2_io_error(gl->gl_sbd);
126}
127
128/**
129 * getbuf - Get a buffer with a given address space
cb4c0313 130 * @gl: the glock
b3b94faa
DT
131 * @blkno: the block number (filesystem scope)
132 * @create: 1 if the buffer should be created
133 *
134 * Returns: the buffer
135 */
136
cb4c0313 137static struct buffer_head *getbuf(struct gfs2_glock *gl, u64 blkno, int create)
b3b94faa 138{
cb4c0313
SW
139 struct address_space *mapping = gl->gl_aspace->i_mapping;
140 struct gfs2_sbd *sdp = gl->gl_sbd;
b3b94faa
DT
141 struct page *page;
142 struct buffer_head *bh;
143 unsigned int shift;
144 unsigned long index;
145 unsigned int bufnum;
146
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 */
150
151 if (create) {
152 for (;;) {
cb4c0313 153 page = grab_cache_page(mapping, index);
b3b94faa
DT
154 if (page)
155 break;
156 yield();
157 }
158 } else {
cb4c0313 159 page = find_lock_page(mapping, index);
b3b94faa
DT
160 if (!page)
161 return NULL;
162 }
163
164 if (!page_has_buffers(page))
165 create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
166
167 /* Locate header for our buffer within our page */
168 for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
169 /* Do nothing */;
170 get_bh(bh);
171
172 if (!buffer_mapped(bh))
173 map_bh(bh, sdp->sd_vfs, blkno);
174
175 unlock_page(page);
176 mark_page_accessed(page);
177 page_cache_release(page);
178
179 return bh;
180}
181
182static void meta_prep_new(struct buffer_head *bh)
183{
184 struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
185
186 lock_buffer(bh);
187 clear_buffer_dirty(bh);
188 set_buffer_uptodate(bh);
189 unlock_buffer(bh);
190
191 mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
192}
193
194/**
195 * gfs2_meta_new - Get a block
196 * @gl: The glock associated with this block
197 * @blkno: The block number
198 *
199 * Returns: The buffer
200 */
201
cd915493 202struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, u64 blkno)
b3b94faa
DT
203{
204 struct buffer_head *bh;
cb4c0313 205 bh = getbuf(gl, blkno, CREATE);
b3b94faa
DT
206 meta_prep_new(bh);
207 return bh;
208}
209
210/**
211 * gfs2_meta_read - Read a block from disk
212 * @gl: The glock covering the block
213 * @blkno: The block number
7276b3b0 214 * @flags: flags
b3b94faa
DT
215 * @bhp: the place where the buffer is returned (NULL on failure)
216 *
217 * Returns: errno
218 */
219
cd915493 220int gfs2_meta_read(struct gfs2_glock *gl, u64 blkno, int flags,
b3b94faa
DT
221 struct buffer_head **bhp)
222{
cb4c0313 223 *bhp = getbuf(gl, blkno, CREATE);
7276b3b0 224 if (!buffer_uptodate(*bhp))
2e565bb6 225 ll_rw_block(READ_META, 1, bhp);
7276b3b0
SW
226 if (flags & DIO_WAIT) {
227 int error = gfs2_meta_wait(gl->gl_sbd, *bhp);
228 if (error) {
229 brelse(*bhp);
230 return error;
231 }
232 }
b3b94faa 233
7276b3b0 234 return 0;
b3b94faa
DT
235}
236
237/**
7276b3b0 238 * gfs2_meta_wait - Reread a block from disk
b3b94faa 239 * @sdp: the filesystem
7276b3b0 240 * @bh: The block to wait for
b3b94faa
DT
241 *
242 * Returns: errno
243 */
244
7276b3b0 245int gfs2_meta_wait(struct gfs2_sbd *sdp, struct buffer_head *bh)
b3b94faa
DT
246{
247 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
248 return -EIO;
249
7276b3b0 250 wait_on_buffer(bh);
b3b94faa 251
7276b3b0
SW
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);
256 return -EIO;
b3b94faa 257 }
7276b3b0
SW
258 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
259 return -EIO;
b3b94faa
DT
260
261 return 0;
262}
263
264/**
586dfdaa 265 * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
b3b94faa
DT
266 * @gl: the glock the buffer belongs to
267 * @bh: The buffer to be attached to
586dfdaa 268 * @meta: Flag to indicate whether its metadata or not
b3b94faa
DT
269 */
270
568f4c96
SW
271void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh,
272 int meta)
b3b94faa
DT
273{
274 struct gfs2_bufdata *bd;
275
18ec7d5c
SW
276 if (meta)
277 lock_page(bh->b_page);
b3b94faa 278
5c676f6d 279 if (bh->b_private) {
18ec7d5c
SW
280 if (meta)
281 unlock_page(bh->b_page);
b3b94faa
DT
282 return;
283 }
284
c3762229 285 bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL),
b3b94faa
DT
286 bd->bd_bh = bh;
287 bd->bd_gl = gl;
288
289 INIT_LIST_HEAD(&bd->bd_list_tr);
82ffa516 290 if (meta)
586dfdaa 291 lops_init_le(&bd->bd_le, &gfs2_buf_lops);
82ffa516 292 else
586dfdaa 293 lops_init_le(&bd->bd_le, &gfs2_databuf_lops);
5c676f6d 294 bh->b_private = bd;
b3b94faa 295
18ec7d5c
SW
296 if (meta)
297 unlock_page(bh->b_page);
b3b94faa
DT
298}
299
b3b94faa
DT
300/**
301 * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
302 * @ip: the inode who owns the buffers
303 * @bstart: the first buffer in the run
304 * @blen: the number of buffers in the run
305 *
306 */
307
cd915493 308void gfs2_meta_wipe(struct gfs2_inode *ip, u64 bstart, u32 blen)
b3b94faa 309{
feaa7bba 310 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
b3b94faa
DT
311 struct buffer_head *bh;
312
313 while (blen) {
cb4c0313 314 bh = getbuf(ip->i_gl, bstart, NO_CREATE);
b3b94faa 315 if (bh) {
5c676f6d 316 struct gfs2_bufdata *bd = bh->b_private;
b3b94faa
DT
317
318 if (test_clear_buffer_pinned(bh)) {
5c676f6d 319 struct gfs2_trans *tr = current->journal_info;
2332c443
RP
320 struct gfs2_inode *bh_ip =
321 GFS2_I(bh->b_page->mapping->host);
322
b3b94faa
DT
323 gfs2_log_lock(sdp);
324 list_del_init(&bd->bd_le.le_list);
325 gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
326 sdp->sd_log_num_buf--;
327 gfs2_log_unlock(sdp);
2332c443
RP
328 if (bh_ip->i_inode.i_private != NULL)
329 tr->tr_num_databuf_rm++;
330 else
331 tr->tr_num_buf_rm++;
b3b94faa
DT
332 brelse(bh);
333 }
334 if (bd) {
335 gfs2_log_lock(sdp);
336 if (bd->bd_ail) {
cd915493 337 u64 blkno = bh->b_blocknr;
b3b94faa
DT
338 bd->bd_ail = NULL;
339 list_del(&bd->bd_ail_st_list);
340 list_del(&bd->bd_ail_gl_list);
341 atomic_dec(&bd->bd_gl->gl_ail_count);
342 brelse(bh);
343 gfs2_log_unlock(sdp);
344 gfs2_trans_add_revoke(sdp, blkno);
345 } else
346 gfs2_log_unlock(sdp);
347 }
348
349 lock_buffer(bh);
350 clear_buffer_dirty(bh);
351 clear_buffer_uptodate(bh);
352 unlock_buffer(bh);
353
354 brelse(bh);
355 }
356
357 bstart++;
358 blen--;
359 }
360}
361
362/**
363 * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
364 * @ip: The GFS2 inode
365 *
366 * This releases buffers that are in the most-recently-used array of
367 * blocks used for indirect block addressing for this inode.
368 */
369
370void gfs2_meta_cache_flush(struct gfs2_inode *ip)
371{
372 struct buffer_head **bh_slot;
373 unsigned int x;
374
375 spin_lock(&ip->i_spin);
376
377 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
378 bh_slot = &ip->i_cache[x];
379 if (!*bh_slot)
380 break;
381 brelse(*bh_slot);
382 *bh_slot = NULL;
383 }
384
385 spin_unlock(&ip->i_spin);
386}
387
388/**
389 * gfs2_meta_indirect_buffer - Get a metadata buffer
390 * @ip: The GFS2 inode
391 * @height: The level of this buf in the metadata (indir addr) tree (if any)
392 * @num: The block number (device relative) of the buffer
393 * @new: Non-zero if we may create a new buffer
394 * @bhp: the buffer is returned here
395 *
396 * Try to use the gfs2_inode's MRU metadata tree cache.
397 *
398 * Returns: errno
399 */
400
cd915493 401int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, u64 num,
b3b94faa
DT
402 int new, struct buffer_head **bhp)
403{
7276b3b0
SW
404 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
405 struct gfs2_glock *gl = ip->i_gl;
406 struct buffer_head *bh = NULL, **bh_slot = ip->i_cache + height;
407 int in_cache = 0;
b3b94faa 408
b004157a
SW
409 BUG_ON(!gl);
410 BUG_ON(!sdp);
411
b3b94faa 412 spin_lock(&ip->i_spin);
7276b3b0
SW
413 if (*bh_slot && (*bh_slot)->b_blocknr == num) {
414 bh = *bh_slot;
415 get_bh(bh);
416 in_cache = 1;
b3b94faa
DT
417 }
418 spin_unlock(&ip->i_spin);
419
7276b3b0 420 if (!bh)
cb4c0313 421 bh = getbuf(gl, num, CREATE);
b3b94faa 422
7276b3b0
SW
423 if (!bh)
424 return -ENOBUFS;
b3b94faa
DT
425
426 if (new) {
7276b3b0
SW
427 if (gfs2_assert_warn(sdp, height))
428 goto err;
429 meta_prep_new(bh);
d4e9c4c3 430 gfs2_trans_add_bh(ip->i_gl, bh, 1);
b3b94faa
DT
431 gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
432 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
7276b3b0
SW
433 } else {
434 u32 mtype = height ? GFS2_METATYPE_IN : GFS2_METATYPE_DI;
435 if (!buffer_uptodate(bh)) {
2e565bb6 436 ll_rw_block(READ_META, 1, &bh);
7276b3b0
SW
437 if (gfs2_meta_wait(sdp, bh))
438 goto err;
439 }
440 if (gfs2_metatype_check(sdp, bh, mtype))
441 goto err;
442 }
b3b94faa 443
7276b3b0
SW
444 if (!in_cache) {
445 spin_lock(&ip->i_spin);
446 if (*bh_slot)
447 brelse(*bh_slot);
448 *bh_slot = bh;
449 get_bh(bh);
450 spin_unlock(&ip->i_spin);
b3b94faa
DT
451 }
452
453 *bhp = bh;
b3b94faa 454 return 0;
7276b3b0
SW
455err:
456 brelse(bh);
457 return -EIO;
b3b94faa
DT
458}
459
460/**
461 * gfs2_meta_ra - start readahead on an extent of a file
462 * @gl: the glock the blocks belong to
463 * @dblock: the starting disk block
464 * @extlen: the number of blocks in the extent
465 *
7276b3b0 466 * returns: the first buffer in the extent
b3b94faa
DT
467 */
468
7276b3b0 469struct buffer_head *gfs2_meta_ra(struct gfs2_glock *gl, u64 dblock, u32 extlen)
b3b94faa
DT
470{
471 struct gfs2_sbd *sdp = gl->gl_sbd;
b3b94faa 472 struct buffer_head *first_bh, *bh;
cd915493 473 u32 max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
568f4c96 474 sdp->sd_sb.sb_bsize_shift;
b3b94faa 475
7276b3b0
SW
476 BUG_ON(!extlen);
477
478 if (max_ra < 1)
479 max_ra = 1;
b3b94faa
DT
480 if (extlen > max_ra)
481 extlen = max_ra;
482
cb4c0313 483 first_bh = getbuf(gl, dblock, CREATE);
b3b94faa
DT
484
485 if (buffer_uptodate(first_bh))
486 goto out;
7276b3b0 487 if (!buffer_locked(first_bh))
2e565bb6 488 ll_rw_block(READ_META, 1, &first_bh);
b3b94faa
DT
489
490 dblock++;
491 extlen--;
492
493 while (extlen) {
cb4c0313 494 bh = getbuf(gl, dblock, CREATE);
b3b94faa 495
7276b3b0
SW
496 if (!buffer_uptodate(bh) && !buffer_locked(bh))
497 ll_rw_block(READA, 1, &bh);
498 brelse(bh);
b3b94faa
DT
499 dblock++;
500 extlen--;
7276b3b0
SW
501 if (!buffer_locked(first_bh) && buffer_uptodate(first_bh))
502 goto out;
b3b94faa
DT
503 }
504
7276b3b0 505 wait_on_buffer(first_bh);
a91ea69f 506out:
7276b3b0 507 return first_bh;
b3b94faa
DT
508}
509