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