[GFS2] Use mutices rather than semaphores
[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         if (meta)
551                 lock_page(bh->b_page);
552
553         if (get_v2bd(bh)) {
554                 if (meta)
555                         unlock_page(bh->b_page);
556                 return;
557         }
558
559         bd = kmem_cache_alloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL),
560         atomic_inc(&gl->gl_sbd->sd_bufdata_count);
561
562         memset(bd, 0, sizeof(struct gfs2_bufdata));
563
564         bd->bd_bh = bh;
565         bd->bd_gl = gl;
566
567         INIT_LIST_HEAD(&bd->bd_list_tr);
568         if (meta) {
569                 lops_init_le(&bd->bd_le, &gfs2_buf_lops);
570         } else {
571                 lops_init_le(&bd->bd_le, &gfs2_databuf_lops);
572                 get_bh(bh);
573         }
574         set_v2bd(bh, bd);
575
576         if (meta)
577                 unlock_page(bh->b_page);
578 }
579
580 /**
581  * gfs2_pin - Pin a buffer in memory
582  * @sdp: the filesystem the buffer belongs to
583  * @bh: The buffer to be pinned
584  *
585  */
586
587 void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
588 {
589         struct gfs2_bufdata *bd = get_v2bd(bh);
590
591         gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
592
593         if (test_set_buffer_pinned(bh))
594                 gfs2_assert_withdraw(sdp, 0);
595
596         wait_on_buffer(bh);
597
598         /* If this buffer is in the AIL and it has already been written
599            to in-place disk block, remove it from the AIL. */
600
601         gfs2_log_lock(sdp);
602         if (bd->bd_ail && !buffer_in_io(bh))
603                 list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
604         gfs2_log_unlock(sdp);
605
606         clear_buffer_dirty(bh);
607         wait_on_buffer(bh);
608
609         if (!buffer_uptodate(bh))
610                 gfs2_io_error_bh(sdp, bh);
611
612         get_bh(bh);
613 }
614
615 /**
616  * gfs2_unpin - Unpin a buffer
617  * @sdp: the filesystem the buffer belongs to
618  * @bh: The buffer to unpin
619  * @ai:
620  *
621  */
622
623 void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
624                 struct gfs2_ail *ai)
625 {
626         struct gfs2_bufdata *bd = get_v2bd(bh);
627
628         gfs2_assert_withdraw(sdp, buffer_uptodate(bh));
629
630         if (!buffer_pinned(bh))
631                 gfs2_assert_withdraw(sdp, 0);
632
633         mark_buffer_dirty(bh);
634         clear_buffer_pinned(bh);
635
636         gfs2_log_lock(sdp);
637         if (bd->bd_ail) {
638                 list_del(&bd->bd_ail_st_list);
639                 brelse(bh);
640         } else {
641                 struct gfs2_glock *gl = bd->bd_gl;
642                 list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
643                 atomic_inc(&gl->gl_ail_count);
644         }
645         bd->bd_ail = ai;
646         list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
647         gfs2_log_unlock(sdp);
648 }
649
650 /**
651  * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
652  * @ip: the inode who owns the buffers
653  * @bstart: the first buffer in the run
654  * @blen: the number of buffers in the run
655  *
656  */
657
658 void gfs2_meta_wipe(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
659 {
660         struct gfs2_sbd *sdp = ip->i_sbd;
661         struct inode *aspace = ip->i_gl->gl_aspace;
662         struct buffer_head *bh;
663
664         while (blen) {
665                 bh = getbuf(sdp, aspace, bstart, NO_CREATE);
666                 if (bh) {
667                         struct gfs2_bufdata *bd = get_v2bd(bh);
668
669                         if (test_clear_buffer_pinned(bh)) {
670                                 gfs2_log_lock(sdp);
671                                 list_del_init(&bd->bd_le.le_list);
672                                 gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
673                                 sdp->sd_log_num_buf--;
674                                 gfs2_log_unlock(sdp);
675                                 get_transaction->tr_num_buf_rm++;
676                                 brelse(bh);
677                         }
678                         if (bd) {
679                                 gfs2_log_lock(sdp);
680                                 if (bd->bd_ail) {
681                                         uint64_t blkno = bh->b_blocknr;
682                                         bd->bd_ail = NULL;
683                                         list_del(&bd->bd_ail_st_list);
684                                         list_del(&bd->bd_ail_gl_list);
685                                         atomic_dec(&bd->bd_gl->gl_ail_count);
686                                         brelse(bh);
687                                         gfs2_log_unlock(sdp);
688                                         gfs2_trans_add_revoke(sdp, blkno);
689                                 } else
690                                         gfs2_log_unlock(sdp);
691                         }
692
693                         lock_buffer(bh);
694                         clear_buffer_dirty(bh);
695                         clear_buffer_uptodate(bh);
696                         unlock_buffer(bh);
697
698                         brelse(bh);
699                 }
700
701                 bstart++;
702                 blen--;
703         }
704 }
705
706 /**
707  * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
708  * @ip: The GFS2 inode
709  *
710  * This releases buffers that are in the most-recently-used array of
711  * blocks used for indirect block addressing for this inode.
712  */
713
714 void gfs2_meta_cache_flush(struct gfs2_inode *ip)
715 {
716         struct buffer_head **bh_slot;
717         unsigned int x;
718
719         spin_lock(&ip->i_spin);
720
721         for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
722                 bh_slot = &ip->i_cache[x];
723                 if (!*bh_slot)
724                         break;
725                 brelse(*bh_slot);
726                 *bh_slot = NULL;
727         }
728
729         spin_unlock(&ip->i_spin);
730 }
731
732 /**
733  * gfs2_meta_indirect_buffer - Get a metadata buffer
734  * @ip: The GFS2 inode
735  * @height: The level of this buf in the metadata (indir addr) tree (if any)
736  * @num: The block number (device relative) of the buffer
737  * @new: Non-zero if we may create a new buffer
738  * @bhp: the buffer is returned here
739  *
740  * Try to use the gfs2_inode's MRU metadata tree cache.
741  *
742  * Returns: errno
743  */
744
745 int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, uint64_t num,
746                               int new, struct buffer_head **bhp)
747 {
748         struct buffer_head *bh, **bh_slot = ip->i_cache + height;
749         int error;
750
751         spin_lock(&ip->i_spin);
752         bh = *bh_slot;
753         if (bh) {
754                 if (bh->b_blocknr == num)
755                         get_bh(bh);
756                 else
757                         bh = NULL;
758         }
759         spin_unlock(&ip->i_spin);
760
761         if (bh) {
762                 if (new)
763                         meta_prep_new(bh);
764                 else {
765                         error = gfs2_meta_reread(ip->i_sbd, bh,
766                                                  DIO_START | DIO_WAIT);
767                         if (error) {
768                                 brelse(bh);
769                                 return error;
770                         }
771                 }
772         } else {
773                 if (new)
774                         bh = gfs2_meta_new(ip->i_gl, num);
775                 else {
776                         error = gfs2_meta_read(ip->i_gl, num,
777                                                DIO_START | DIO_WAIT, &bh);
778                         if (error)
779                                 return error;
780                 }
781
782                 spin_lock(&ip->i_spin);
783                 if (*bh_slot != bh) {
784                         brelse(*bh_slot);
785                         *bh_slot = bh;
786                         get_bh(bh);
787                 }
788                 spin_unlock(&ip->i_spin);
789         }
790
791         if (new) {
792                 if (gfs2_assert_warn(ip->i_sbd, height)) {
793                         brelse(bh);
794                         return -EIO;
795                 }
796                 gfs2_trans_add_bh(ip->i_gl, bh, 1);
797                 gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
798                 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
799
800         } else if (gfs2_metatype_check(ip->i_sbd, bh,
801                              (height) ? GFS2_METATYPE_IN : GFS2_METATYPE_DI)) {
802                 brelse(bh);
803                 return -EIO;
804         }
805
806         *bhp = bh;
807
808         return 0;
809 }
810
811 /**
812  * gfs2_meta_ra - start readahead on an extent of a file
813  * @gl: the glock the blocks belong to
814  * @dblock: the starting disk block
815  * @extlen: the number of blocks in the extent
816  *
817  */
818
819 void gfs2_meta_ra(struct gfs2_glock *gl, uint64_t dblock, uint32_t extlen)
820 {
821         struct gfs2_sbd *sdp = gl->gl_sbd;
822         struct inode *aspace = gl->gl_aspace;
823         struct buffer_head *first_bh, *bh;
824         uint32_t max_ra = gfs2_tune_get(sdp, gt_max_readahead) >> sdp->sd_sb.sb_bsize_shift;
825         int error;
826
827         if (!extlen || !max_ra)
828                 return;
829         if (extlen > max_ra)
830                 extlen = max_ra;
831
832         first_bh = getbuf(sdp, aspace, dblock, CREATE);
833
834         if (buffer_uptodate(first_bh))
835                 goto out;
836         if (!buffer_locked(first_bh)) {
837                 error = gfs2_meta_reread(sdp, first_bh, DIO_START);
838                 if (error)
839                         goto out;
840         }
841
842         dblock++;
843         extlen--;
844
845         while (extlen) {
846                 bh = getbuf(sdp, aspace, dblock, CREATE);
847
848                 if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
849                         error = gfs2_meta_reread(sdp, bh, DIO_START);
850                         brelse(bh);
851                         if (error)
852                                 goto out;
853                 } else
854                         brelse(bh);
855
856                 dblock++;
857                 extlen--;
858
859                 if (buffer_uptodate(first_bh))
860                         break;
861         }
862
863  out:
864         brelse(first_bh);
865 }
866
867 /**
868  * gfs2_meta_syncfs - sync all the buffers in a filesystem
869  * @sdp: the filesystem
870  *
871  */
872
873 void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
874 {
875         gfs2_log_flush(sdp);
876         for (;;) {
877                 gfs2_ail1_start(sdp, DIO_ALL);
878                 if (gfs2_ail1_empty(sdp, DIO_ALL))
879                         break;
880                 msleep(100);
881         }
882 }
883