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