[GFS2] Make the new argument to gfs2_trans_add_bh() actually do something
[linux-2.6.git] / fs / gfs2 / meta_io.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2005 Red Hat, Inc.  All rights reserved.
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
7  * of the GNU General Public License v.2.
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>
20 #include <asm/semaphore.h>
21
22 #include "gfs2.h"
23 #include "glock.h"
24 #include "glops.h"
25 #include "inode.h"
26 #include "log.h"
27 #include "lops.h"
28 #include "meta_io.h"
29 #include "rgrp.h"
30 #include "trans.h"
31
32 #define buffer_busy(bh) \
33 ((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock) | (1ul << BH_Pinned)))
34 #define buffer_in_io(bh) \
35 ((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock)))
36
37 static int aspace_get_block(struct inode *inode, sector_t lblock,
38                             struct buffer_head *bh_result, int create)
39 {
40         gfs2_assert_warn(get_v2sdp(inode->i_sb), 0);
41         return -EOPNOTSUPP;
42 }
43
44 static 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
50 /**
51  * stuck_releasepage - We're stuck in gfs2_releasepage().  Print stuff out.
52  * @bh: the buffer we're stuck on
53  *
54  */
55
56 static void stuck_releasepage(struct buffer_head *bh)
57 {
58         struct gfs2_sbd *sdp = get_v2sdp(bh->b_page->mapping->host->i_sb);
59         struct gfs2_bufdata *bd = get_v2bd(bh);
60         struct gfs2_glock *gl;
61
62         fs_warn(sdp, "stuck in gfs2_releasepage()\n");
63         fs_warn(sdp, "blkno = %llu, bh->b_count = %d\n",
64                 (uint64_t)bh->b_blocknr, atomic_read(&bh->b_count));
65         fs_warn(sdp, "pinned = %u\n", buffer_pinned(bh));
66         fs_warn(sdp, "get_v2bd(bh) = %s\n", (bd) ? "!NULL" : "NULL");
67
68         if (!bd)
69                 return;
70
71         gl = bd->bd_gl;
72
73         fs_warn(sdp, "gl = (%u, %llu)\n", 
74                 gl->gl_name.ln_type, gl->gl_name.ln_number);
75
76         fs_warn(sdp, "bd_list_tr = %s, bd_le.le_list = %s\n",
77                 (list_empty(&bd->bd_list_tr)) ? "no" : "yes",
78                 (list_empty(&bd->bd_le.le_list)) ? "no" : "yes");
79
80         if (gl->gl_ops == &gfs2_inode_glops) {
81                 struct gfs2_inode *ip = get_gl2ip(gl);
82                 unsigned int x;
83
84                 if (!ip)
85                         return;
86
87                 fs_warn(sdp, "ip = %llu %llu\n",
88                         ip->i_num.no_formal_ino, ip->i_num.no_addr);
89                 fs_warn(sdp, "ip->i_count = %d, ip->i_vnode = %s\n",
90                         atomic_read(&ip->i_count),
91                         (ip->i_vnode) ? "!NULL" : "NULL");
92
93                 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++)
94                         fs_warn(sdp, "ip->i_cache[%u] = %s\n",
95                                 x, (ip->i_cache[x]) ? "!NULL" : "NULL");
96         }
97 }
98
99 /**
100  * gfs2_aspace_releasepage - free the metadata associated with a page
101  * @page: the page that's being released
102  * @gfp_mask: passed from Linux VFS, ignored by us
103  *
104  * Call try_to_free_buffers() if the buffers in this page can be
105  * released.
106  *
107  * Returns: 0
108  */
109
110 static int gfs2_aspace_releasepage(struct page *page, gfp_t gfp_mask)
111 {
112         struct inode *aspace = page->mapping->host;
113         struct gfs2_sbd *sdp = get_v2sdp(aspace->i_sb);
114         struct buffer_head *bh, *head;
115         struct gfs2_bufdata *bd;
116         unsigned long t;
117
118         if (!page_has_buffers(page))
119                 goto out;
120
121         head = bh = page_buffers(page);
122         do {
123                 t = jiffies;
124
125                 while (atomic_read(&bh->b_count)) {
126                         if (atomic_read(&aspace->i_writecount)) {
127                                 if (time_after_eq(jiffies, t +
128                                     gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
129                                         stuck_releasepage(bh);
130                                         t = jiffies;
131                                 }
132
133                                 yield();
134                                 continue;
135                         }
136
137                         return 0;
138                 }
139
140                 gfs2_assert_warn(sdp, !buffer_pinned(bh));
141
142                 bd = get_v2bd(bh);
143                 if (bd) {
144                         gfs2_assert_warn(sdp, bd->bd_bh == bh);
145                         gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
146                         gfs2_assert_warn(sdp, list_empty(&bd->bd_le.le_list));
147                         gfs2_assert_warn(sdp, !bd->bd_ail);
148                         kmem_cache_free(gfs2_bufdata_cachep, bd);
149                         atomic_dec(&sdp->sd_bufdata_count);
150                         set_v2bd(bh, NULL);
151                 }
152
153                 bh = bh->b_this_page;
154         }
155         while (bh != head);
156
157  out:
158         return try_to_free_buffers(page);
159 }
160
161 static struct address_space_operations aspace_aops = {
162         .writepage = gfs2_aspace_writepage,
163         .releasepage = gfs2_aspace_releasepage,
164 };
165
166 /**
167  * gfs2_aspace_get - Create and initialize a struct inode structure
168  * @sdp: the filesystem the aspace is in
169  *
170  * Right now a struct inode is just a struct inode.  Maybe Linux
171  * will supply a more lightweight address space construct (that works)
172  * in the future.
173  *
174  * Make sure pages/buffers in this aspace aren't in high memory.
175  *
176  * Returns: the aspace
177  */
178
179 struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp)
180 {
181         struct inode *aspace;
182
183         aspace = new_inode(sdp->sd_vfs);
184         if (aspace) {
185                 mapping_set_gfp_mask(aspace->i_mapping, GFP_KERNEL);
186                 aspace->i_mapping->a_ops = &aspace_aops;
187                 aspace->i_size = ~0ULL;
188                 set_v2ip(aspace, NULL);
189                 insert_inode_hash(aspace);
190         }
191
192         return aspace;
193 }
194
195 void gfs2_aspace_put(struct inode *aspace)
196 {
197         remove_inode_hash(aspace);
198         iput(aspace);
199 }
200
201 /**
202  * gfs2_ail1_start_one - Start I/O on a part of the AIL
203  * @sdp: the filesystem
204  * @tr: the part of the AIL
205  *
206  */
207
208 void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
209 {
210         struct gfs2_bufdata *bd, *s;
211         struct buffer_head *bh;
212         int retry;
213
214         do {
215                 retry = 0;
216
217                 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
218                                                  bd_ail_st_list) {
219                         bh = bd->bd_bh;
220
221                         gfs2_assert(sdp, bd->bd_ail == ai);
222
223                         if (!buffer_busy(bh)) {
224                                 if (!buffer_uptodate(bh))
225                                         gfs2_io_error_bh(sdp, bh);
226                                 list_move(&bd->bd_ail_st_list,
227                                           &ai->ai_ail2_list);
228                                 continue;
229                         }
230
231                         if (!buffer_dirty(bh))
232                                 continue;
233
234                         list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
235
236                         gfs2_log_unlock(sdp);
237                         wait_on_buffer(bh);
238                         ll_rw_block(WRITE, 1, &bh);
239                         gfs2_log_lock(sdp);
240
241                         retry = 1;
242                         break;
243                 }
244         } while (retry);
245 }
246
247 /**
248  * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
249  * @sdp: the filesystem
250  * @ai: the AIL entry
251  *
252  */
253
254 int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
255 {
256         struct gfs2_bufdata *bd, *s;
257         struct buffer_head *bh;
258
259         list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
260                                          bd_ail_st_list) {
261                 bh = bd->bd_bh;
262
263                 gfs2_assert(sdp, bd->bd_ail == ai);
264
265                 if (buffer_busy(bh)) {
266                         if (flags & DIO_ALL)
267                                 continue;
268                         else
269                                 break;
270                 }
271
272                 if (!buffer_uptodate(bh))
273                         gfs2_io_error_bh(sdp, bh);
274
275                 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
276         }
277
278         return list_empty(&ai->ai_ail1_list);
279 }
280
281 /**
282  * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
283  * @sdp: the filesystem
284  * @ai: the AIL entry
285  *
286  */
287
288 void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
289 {
290         struct list_head *head = &ai->ai_ail2_list;
291         struct gfs2_bufdata *bd;
292
293         while (!list_empty(head)) {
294                 bd = list_entry(head->prev, struct gfs2_bufdata,
295                                 bd_ail_st_list);
296                 gfs2_assert(sdp, bd->bd_ail == ai);
297                 bd->bd_ail = NULL;
298                 list_del(&bd->bd_ail_st_list);
299                 list_del(&bd->bd_ail_gl_list);
300                 atomic_dec(&bd->bd_gl->gl_ail_count);
301                 brelse(bd->bd_bh);
302         }
303 }
304
305 /**
306  * ail_empty_gl - remove all buffers for a given lock from the AIL
307  * @gl: the glock
308  *
309  * None of the buffers should be dirty, locked, or pinned.
310  */
311
312 void gfs2_ail_empty_gl(struct gfs2_glock *gl)
313 {
314         struct gfs2_sbd *sdp = gl->gl_sbd;
315         unsigned int blocks;
316         struct list_head *head = &gl->gl_ail_list;
317         struct gfs2_bufdata *bd;
318         struct buffer_head *bh;
319         uint64_t blkno;
320         int error;
321
322         blocks = atomic_read(&gl->gl_ail_count);
323         if (!blocks)
324                 return;
325
326         error = gfs2_trans_begin(sdp, 0, blocks);
327         if (gfs2_assert_withdraw(sdp, !error))
328                 return;
329
330         gfs2_log_lock(sdp);
331         while (!list_empty(head)) {
332                 bd = list_entry(head->next, struct gfs2_bufdata,
333                                 bd_ail_gl_list);
334                 bh = bd->bd_bh;
335                 blkno = bh->b_blocknr;
336                 gfs2_assert_withdraw(sdp, !buffer_busy(bh));
337
338                 bd->bd_ail = NULL;
339                 list_del(&bd->bd_ail_st_list);
340                 list_del(&bd->bd_ail_gl_list);
341                 atomic_dec(&gl->gl_ail_count);
342                 brelse(bh);
343                 gfs2_log_unlock(sdp);
344
345                 gfs2_trans_add_revoke(sdp, blkno);
346
347                 gfs2_log_lock(sdp);
348         }
349         gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
350         gfs2_log_unlock(sdp);
351
352         gfs2_trans_end(sdp);
353         gfs2_log_flush(sdp);
354 }
355
356 /**
357  * gfs2_meta_inval - Invalidate all buffers associated with a glock
358  * @gl: the glock
359  *
360  */
361
362 void gfs2_meta_inval(struct gfs2_glock *gl)
363 {
364         struct gfs2_sbd *sdp = gl->gl_sbd;
365         struct inode *aspace = gl->gl_aspace;
366         struct address_space *mapping = gl->gl_aspace->i_mapping;
367
368         gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
369
370         atomic_inc(&aspace->i_writecount);
371         truncate_inode_pages(mapping, 0);
372         atomic_dec(&aspace->i_writecount);
373
374         gfs2_assert_withdraw(sdp, !mapping->nrpages);
375 }
376
377 /**
378  * gfs2_meta_sync - Sync all buffers associated with a glock
379  * @gl: The glock
380  * @flags: DIO_START | DIO_WAIT
381  *
382  */
383
384 void gfs2_meta_sync(struct gfs2_glock *gl, int flags)
385 {
386         struct address_space *mapping = gl->gl_aspace->i_mapping;
387         int error = 0;
388
389         if (flags & DIO_START)
390                 filemap_fdatawrite(mapping);
391         if (!error && (flags & DIO_WAIT))
392                 error = filemap_fdatawait(mapping);
393
394         if (error)
395                 gfs2_io_error(gl->gl_sbd);
396 }
397
398 /**
399  * getbuf - Get a buffer with a given address space
400  * @sdp: the filesystem
401  * @aspace: the address space
402  * @blkno: the block number (filesystem scope)
403  * @create: 1 if the buffer should be created
404  *
405  * Returns: the buffer
406  */
407
408 static struct buffer_head *getbuf(struct gfs2_sbd *sdp, struct inode *aspace,
409                                   uint64_t blkno, int create)
410 {
411         struct page *page;
412         struct buffer_head *bh;
413         unsigned int shift;
414         unsigned long index;
415         unsigned int bufnum;
416
417         shift = PAGE_CACHE_SHIFT - sdp->sd_sb.sb_bsize_shift;
418         index = blkno >> shift;             /* convert block to page */
419         bufnum = blkno - (index << shift);  /* block buf index within page */
420
421         if (create) {
422                 for (;;) {
423                         page = grab_cache_page(aspace->i_mapping, index);
424                         if (page)
425                                 break;
426                         yield();
427                 }
428         } else {
429                 page = find_lock_page(aspace->i_mapping, index);
430                 if (!page)
431                         return NULL;
432         }
433
434         if (!page_has_buffers(page))
435                 create_empty_buffers(page, sdp->sd_sb.sb_bsize, 0);
436
437         /* Locate header for our buffer within our page */
438         for (bh = page_buffers(page); bufnum--; bh = bh->b_this_page)
439                 /* Do nothing */;
440         get_bh(bh);
441
442         if (!buffer_mapped(bh))
443                 map_bh(bh, sdp->sd_vfs, blkno);
444
445         unlock_page(page);
446         mark_page_accessed(page);
447         page_cache_release(page);
448
449         return bh;
450 }
451
452 static void meta_prep_new(struct buffer_head *bh)
453 {
454         struct gfs2_meta_header *mh = (struct gfs2_meta_header *)bh->b_data;
455
456         lock_buffer(bh);
457         clear_buffer_dirty(bh);
458         set_buffer_uptodate(bh);
459         unlock_buffer(bh);
460
461         mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
462 }
463
464 /**
465  * gfs2_meta_new - Get a block
466  * @gl: The glock associated with this block
467  * @blkno: The block number
468  *
469  * Returns: The buffer
470  */
471
472 struct buffer_head *gfs2_meta_new(struct gfs2_glock *gl, uint64_t blkno)
473 {
474         struct buffer_head *bh;
475         bh = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
476         meta_prep_new(bh);
477         return bh;
478 }
479
480 /**
481  * gfs2_meta_read - Read a block from disk
482  * @gl: The glock covering the block
483  * @blkno: The block number
484  * @flags: flags to gfs2_dreread()
485  * @bhp: the place where the buffer is returned (NULL on failure)
486  *
487  * Returns: errno
488  */
489
490 int gfs2_meta_read(struct gfs2_glock *gl, uint64_t blkno, int flags,
491                    struct buffer_head **bhp)
492 {
493         int error;
494
495         *bhp = getbuf(gl->gl_sbd, gl->gl_aspace, blkno, CREATE);
496         error = gfs2_meta_reread(gl->gl_sbd, *bhp, flags);
497         if (error)
498                 brelse(*bhp);
499
500         return error;
501 }
502
503 /**
504  * gfs2_meta_reread - Reread a block from disk
505  * @sdp: the filesystem
506  * @bh: The block to read
507  * @flags: Flags that control the read
508  *
509  * Returns: errno
510  */
511
512 int gfs2_meta_reread(struct gfs2_sbd *sdp, struct buffer_head *bh, int flags)
513 {
514         if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
515                 return -EIO;
516
517         if (flags & DIO_FORCE)
518                 clear_buffer_uptodate(bh);
519
520         if ((flags & DIO_START) && !buffer_uptodate(bh))
521                 ll_rw_block(READ, 1, &bh);
522
523         if (flags & DIO_WAIT) {
524                 wait_on_buffer(bh);
525
526                 if (!buffer_uptodate(bh)) {
527                         struct gfs2_trans *tr = get_transaction;
528                         if (tr && tr->tr_touched)
529                                 gfs2_io_error_bh(sdp, bh);
530                         return -EIO;
531                 }
532                 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
533                         return -EIO;
534         }
535
536         return 0;
537 }
538
539 /**
540  * gfs2_attach_bufdata - attach a struct gfs2_bufdata structure to a buffer
541  * @gl: the glock the buffer belongs to
542  * @bh: The buffer to be attached to
543  * @meta: Flag to indicate whether its metadata or not
544  */
545
546 void gfs2_attach_bufdata(struct gfs2_glock *gl, struct buffer_head *bh, int meta)
547 {
548         struct gfs2_bufdata *bd;
549
550         lock_page(bh->b_page);
551
552         if (get_v2bd(bh)) {
553                 unlock_page(bh->b_page);
554                 return;
555         }
556
557         bd = kmem_cache_alloc(gfs2_bufdata_cachep, GFP_KERNEL | __GFP_NOFAIL),
558         atomic_inc(&gl->gl_sbd->sd_bufdata_count);
559
560         memset(bd, 0, sizeof(struct gfs2_bufdata));
561
562         bd->bd_bh = bh;
563         bd->bd_gl = gl;
564
565         INIT_LIST_HEAD(&bd->bd_list_tr);
566         if (meta)
567                 lops_init_le(&bd->bd_le, &gfs2_buf_lops);
568         else
569                 lops_init_le(&bd->bd_le, &gfs2_databuf_lops);
570
571         set_v2bd(bh, bd);
572
573         unlock_page(bh->b_page);
574 }
575
576 /**
577  * gfs2_meta_pin - Pin a metadata buffer in memory
578  * @sdp: the filesystem the buffer belongs to
579  * @bh: The buffer to be pinned
580  *
581  */
582
583 void gfs2_meta_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
584 {
585         struct gfs2_bufdata *bd = get_v2bd(bh);
586
587         gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
588
589         if (test_set_buffer_pinned(bh))
590                 gfs2_assert_withdraw(sdp, 0);
591
592         wait_on_buffer(bh);
593
594         /* If this buffer is in the AIL and it has already been written
595            to in-place disk block, remove it from the AIL. */
596
597         gfs2_log_lock(sdp);
598         if (bd->bd_ail && !buffer_in_io(bh))
599                 list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
600         gfs2_log_unlock(sdp);
601
602         clear_buffer_dirty(bh);
603         wait_on_buffer(bh);
604
605         if (!buffer_uptodate(bh))
606                 gfs2_io_error_bh(sdp, bh);
607
608         get_bh(bh);
609 }
610
611 /**
612  * gfs2_meta_unpin - Unpin a buffer
613  * @sdp: the filesystem the buffer belongs to
614  * @bh: The buffer to unpin
615  * @ai:
616  *
617  */
618
619 void gfs2_meta_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
620                      struct gfs2_ail *ai)
621 {
622         struct gfs2_bufdata *bd = get_v2bd(bh);
623
624         gfs2_assert_withdraw(sdp, buffer_uptodate(bh));
625
626         if (!buffer_pinned(bh))
627                 gfs2_assert_withdraw(sdp, 0);
628
629         mark_buffer_dirty(bh);
630         clear_buffer_pinned(bh);
631
632         gfs2_log_lock(sdp);
633         if (bd->bd_ail) {
634                 list_del(&bd->bd_ail_st_list);
635                 brelse(bh);
636         } else {
637                 struct gfs2_glock *gl = bd->bd_gl;
638                 list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
639                 atomic_inc(&gl->gl_ail_count);
640         }
641         bd->bd_ail = ai;
642         list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
643         gfs2_log_unlock(sdp);
644 }
645
646 /**
647  * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
648  * @ip: the inode who owns the buffers
649  * @bstart: the first buffer in the run
650  * @blen: the number of buffers in the run
651  *
652  */
653
654 void gfs2_meta_wipe(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
655 {
656         struct gfs2_sbd *sdp = ip->i_sbd;
657         struct inode *aspace = ip->i_gl->gl_aspace;
658         struct buffer_head *bh;
659
660         while (blen) {
661                 bh = getbuf(sdp, aspace, bstart, NO_CREATE);
662                 if (bh) {
663                         struct gfs2_bufdata *bd = get_v2bd(bh);
664
665                         if (test_clear_buffer_pinned(bh)) {
666                                 gfs2_log_lock(sdp);
667                                 list_del_init(&bd->bd_le.le_list);
668                                 gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
669                                 sdp->sd_log_num_buf--;
670                                 gfs2_log_unlock(sdp);
671                                 get_transaction->tr_num_buf_rm++;
672                                 brelse(bh);
673                         }
674                         if (bd) {
675                                 gfs2_log_lock(sdp);
676                                 if (bd->bd_ail) {
677                                         uint64_t blkno = bh->b_blocknr;
678                                         bd->bd_ail = NULL;
679                                         list_del(&bd->bd_ail_st_list);
680                                         list_del(&bd->bd_ail_gl_list);
681                                         atomic_dec(&bd->bd_gl->gl_ail_count);
682                                         brelse(bh);
683                                         gfs2_log_unlock(sdp);
684                                         gfs2_trans_add_revoke(sdp, blkno);
685                                 } else
686                                         gfs2_log_unlock(sdp);
687                         }
688
689                         lock_buffer(bh);
690                         clear_buffer_dirty(bh);
691                         clear_buffer_uptodate(bh);
692                         unlock_buffer(bh);
693
694                         brelse(bh);
695                 }
696
697                 bstart++;
698                 blen--;
699         }
700 }
701
702 /**
703  * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
704  * @ip: The GFS2 inode
705  *
706  * This releases buffers that are in the most-recently-used array of
707  * blocks used for indirect block addressing for this inode.
708  */
709
710 void gfs2_meta_cache_flush(struct gfs2_inode *ip)
711 {
712         struct buffer_head **bh_slot;
713         unsigned int x;
714
715         spin_lock(&ip->i_spin);
716
717         for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
718                 bh_slot = &ip->i_cache[x];
719                 if (!*bh_slot)
720                         break;
721                 brelse(*bh_slot);
722                 *bh_slot = NULL;
723         }
724
725         spin_unlock(&ip->i_spin);
726 }
727
728 /**
729  * gfs2_meta_indirect_buffer - Get a metadata buffer
730  * @ip: The GFS2 inode
731  * @height: The level of this buf in the metadata (indir addr) tree (if any)
732  * @num: The block number (device relative) of the buffer
733  * @new: Non-zero if we may create a new buffer
734  * @bhp: the buffer is returned here
735  *
736  * Try to use the gfs2_inode's MRU metadata tree cache.
737  *
738  * Returns: errno
739  */
740
741 int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, uint64_t num,
742                               int new, struct buffer_head **bhp)
743 {
744         struct buffer_head *bh, **bh_slot = ip->i_cache + height;
745         int error;
746
747         spin_lock(&ip->i_spin);
748         bh = *bh_slot;
749         if (bh) {
750                 if (bh->b_blocknr == num)
751                         get_bh(bh);
752                 else
753                         bh = NULL;
754         }
755         spin_unlock(&ip->i_spin);
756
757         if (bh) {
758                 if (new)
759                         meta_prep_new(bh);
760                 else {
761                         error = gfs2_meta_reread(ip->i_sbd, bh,
762                                                  DIO_START | DIO_WAIT);
763                         if (error) {
764                                 brelse(bh);
765                                 return error;
766                         }
767                 }
768         } else {
769                 if (new)
770                         bh = gfs2_meta_new(ip->i_gl, num);
771                 else {
772                         error = gfs2_meta_read(ip->i_gl, num,
773                                                DIO_START | DIO_WAIT, &bh);
774                         if (error)
775                                 return error;
776                 }
777
778                 spin_lock(&ip->i_spin);
779                 if (*bh_slot != bh) {
780                         brelse(*bh_slot);
781                         *bh_slot = bh;
782                         get_bh(bh);
783                 }
784                 spin_unlock(&ip->i_spin);
785         }
786
787         if (new) {
788                 if (gfs2_assert_warn(ip->i_sbd, height)) {
789                         brelse(bh);
790                         return -EIO;
791                 }
792                 gfs2_trans_add_bh(ip->i_gl, bh, 1);
793                 gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
794                 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
795
796         } else if (gfs2_metatype_check(ip->i_sbd, bh,
797                              (height) ? GFS2_METATYPE_IN : GFS2_METATYPE_DI)) {
798                 brelse(bh);
799                 return -EIO;
800         }
801
802         *bhp = bh;
803
804         return 0;
805 }
806
807 /**
808  * gfs2_meta_ra - start readahead on an extent of a file
809  * @gl: the glock the blocks belong to
810  * @dblock: the starting disk block
811  * @extlen: the number of blocks in the extent
812  *
813  */
814
815 void gfs2_meta_ra(struct gfs2_glock *gl, uint64_t dblock, uint32_t extlen)
816 {
817         struct gfs2_sbd *sdp = gl->gl_sbd;
818         struct inode *aspace = gl->gl_aspace;
819         struct buffer_head *first_bh, *bh;
820         uint32_t max_ra = gfs2_tune_get(sdp, gt_max_readahead) >> sdp->sd_sb.sb_bsize_shift;
821         int error;
822
823         if (!extlen || !max_ra)
824                 return;
825         if (extlen > max_ra)
826                 extlen = max_ra;
827
828         first_bh = getbuf(sdp, aspace, dblock, CREATE);
829
830         if (buffer_uptodate(first_bh))
831                 goto out;
832         if (!buffer_locked(first_bh)) {
833                 error = gfs2_meta_reread(sdp, first_bh, DIO_START);
834                 if (error)
835                         goto out;
836         }
837
838         dblock++;
839         extlen--;
840
841         while (extlen) {
842                 bh = getbuf(sdp, aspace, dblock, CREATE);
843
844                 if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
845                         error = gfs2_meta_reread(sdp, bh, DIO_START);
846                         brelse(bh);
847                         if (error)
848                                 goto out;
849                 } else
850                         brelse(bh);
851
852                 dblock++;
853                 extlen--;
854
855                 if (buffer_uptodate(first_bh))
856                         break;
857         }
858
859  out:
860         brelse(first_bh);
861 }
862
863 /**
864  * gfs2_meta_syncfs - sync all the buffers in a filesystem
865  * @sdp: the filesystem
866  *
867  */
868
869 void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
870 {
871         gfs2_log_flush(sdp);
872         for (;;) {
873                 gfs2_ail1_start(sdp, DIO_ALL);
874                 if (gfs2_ail1_empty(sdp, DIO_ALL))
875                         break;
876                 msleep(100);
877         }
878 }
879