ddcd4dc1081d53437a79a146fb243d594df025c6
[pandora-kernel.git] / fs / gfs2 / meta_io.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2006 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
22 #include "gfs2.h"
23 #include "lm_interface.h"
24 #include "incore.h"
25 #include "glock.h"
26 #include "glops.h"
27 #include "inode.h"
28 #include "log.h"
29 #include "lops.h"
30 #include "meta_io.h"
31 #include "rgrp.h"
32 #include "trans.h"
33 #include "util.h"
34
35 #define buffer_busy(bh) \
36 ((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock) | (1ul << BH_Pinned)))
37 #define buffer_in_io(bh) \
38 ((bh)->b_state & ((1ul << BH_Dirty) | (1ul << BH_Lock)))
39
40 static int aspace_get_block(struct inode *inode, sector_t lblock,
41                             struct buffer_head *bh_result, int create)
42 {
43         gfs2_assert_warn(inode->i_sb->s_fs_info, 0);
44         return -EOPNOTSUPP;
45 }
46
47 static int gfs2_aspace_writepage(struct page *page,
48                                  struct writeback_control *wbc)
49 {
50         return block_write_full_page(page, aspace_get_block, wbc);
51 }
52
53 /**
54  * stuck_releasepage - We're stuck in gfs2_releasepage().  Print stuff out.
55  * @bh: the buffer we're stuck on
56  *
57  */
58
59 static void stuck_releasepage(struct buffer_head *bh)
60 {
61         struct inode *inode = bh->b_page->mapping->host;
62         struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
63         struct gfs2_bufdata *bd = bh->b_private;
64         struct gfs2_glock *gl;
65
66         fs_warn(sdp, "stuck in gfs2_releasepage() %p\n", inode);
67         fs_warn(sdp, "blkno = %llu, bh->b_count = %d\n",
68                 (unsigned long long)bh->b_blocknr, atomic_read(&bh->b_count));
69         fs_warn(sdp, "pinned = %u\n", buffer_pinned(bh));
70         fs_warn(sdp, "bh->b_private = %s\n", (bd) ? "!NULL" : "NULL");
71
72         if (!bd)
73                 return;
74
75         gl = bd->bd_gl;
76
77         fs_warn(sdp, "gl = (%u, %llu)\n", 
78                 gl->gl_name.ln_type, (unsigned long long)gl->gl_name.ln_number);
79
80         fs_warn(sdp, "bd_list_tr = %s, bd_le.le_list = %s\n",
81                 (list_empty(&bd->bd_list_tr)) ? "no" : "yes",
82                 (list_empty(&bd->bd_le.le_list)) ? "no" : "yes");
83
84         if (gl->gl_ops == &gfs2_inode_glops) {
85                 struct gfs2_inode *ip = gl->gl_object;
86                 unsigned int x;
87
88                 if (!ip)
89                         return;
90
91                 fs_warn(sdp, "ip = %llu %llu\n",
92                         (unsigned long long)ip->i_num.no_formal_ino,
93                         (unsigned long long)ip->i_num.no_addr);
94
95                 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++)
96                         fs_warn(sdp, "ip->i_cache[%u] = %s\n",
97                                 x, (ip->i_cache[x]) ? "!NULL" : "NULL");
98         }
99 }
100
101 /**
102  * gfs2_aspace_releasepage - free the metadata associated with a page
103  * @page: the page that's being released
104  * @gfp_mask: passed from Linux VFS, ignored by us
105  *
106  * Call try_to_free_buffers() if the buffers in this page can be
107  * released.
108  *
109  * Returns: 0
110  */
111
112 static int gfs2_aspace_releasepage(struct page *page, gfp_t gfp_mask)
113 {
114         struct inode *aspace = page->mapping->host;
115         struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info;
116         struct buffer_head *bh, *head;
117         struct gfs2_bufdata *bd;
118         unsigned long t;
119
120         if (!page_has_buffers(page))
121                 goto out;
122
123         head = bh = page_buffers(page);
124         do {
125                 t = jiffies;
126
127                 while (atomic_read(&bh->b_count)) {
128                         if (atomic_read(&aspace->i_writecount)) {
129                                 if (time_after_eq(jiffies, t +
130                                     gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
131                                         stuck_releasepage(bh);
132                                         t = jiffies;
133                                 }
134
135                                 yield();
136                                 continue;
137                         }
138
139                         return 0;
140                 }
141
142                 gfs2_assert_warn(sdp, !buffer_pinned(bh));
143
144                 bd = bh->b_private;
145                 if (bd) {
146                         gfs2_assert_warn(sdp, bd->bd_bh == bh);
147                         gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
148                         gfs2_assert_warn(sdp, list_empty(&bd->bd_le.le_list));
149                         gfs2_assert_warn(sdp, !bd->bd_ail);
150                         kmem_cache_free(gfs2_bufdata_cachep, bd);
151                         bh->b_private = NULL;
152                 }
153
154                 bh = bh->b_this_page;
155         }
156         while (bh != head);
157
158  out:
159         return try_to_free_buffers(page);
160 }
161
162 static const struct address_space_operations aspace_aops = {
163         .writepage = gfs2_aspace_writepage,
164         .releasepage = gfs2_aspace_releasepage,
165 };
166
167 /**
168  * gfs2_aspace_get - Create and initialize a struct inode structure
169  * @sdp: the filesystem the aspace is in
170  *
171  * Right now a struct inode is just a struct inode.  Maybe Linux
172  * will supply a more lightweight address space construct (that works)
173  * in the future.
174  *
175  * Make sure pages/buffers in this aspace aren't in high memory.
176  *
177  * Returns: the aspace
178  */
179
180 struct inode *gfs2_aspace_get(struct gfs2_sbd *sdp)
181 {
182         struct inode *aspace;
183
184         aspace = new_inode(sdp->sd_vfs);
185         if (aspace) {
186                 mapping_set_gfp_mask(aspace->i_mapping, GFP_KERNEL);
187                 aspace->i_mapping->a_ops = &aspace_aops;
188                 aspace->i_size = ~0ULL;
189                 aspace->u.generic_ip = NULL;
190                 insert_inode_hash(aspace);
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         BUG_ON(!spin_is_locked(&sdp->sd_log_lock));
215
216         do {
217                 retry = 0;
218
219                 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
220                                                  bd_ail_st_list) {
221                         bh = bd->bd_bh;
222
223                         gfs2_assert(sdp, bd->bd_ail == ai);
224
225                         if (!buffer_busy(bh)) {
226                                 if (!buffer_uptodate(bh)) {
227                                         gfs2_log_unlock(sdp);
228                                         gfs2_io_error_bh(sdp, bh);
229                                         gfs2_log_lock(sdp);
230                                 }
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         bd->bd_bh = bh;
568         bd->bd_gl = gl;
569
570         INIT_LIST_HEAD(&bd->bd_list_tr);
571         if (meta) {
572                 lops_init_le(&bd->bd_le, &gfs2_buf_lops);
573         } else {
574                 lops_init_le(&bd->bd_le, &gfs2_databuf_lops);
575                 get_bh(bh);
576         }
577         bh->b_private = bd;
578
579         if (meta)
580                 unlock_page(bh->b_page);
581 }
582
583 /**
584  * gfs2_pin - Pin a buffer in memory
585  * @sdp: the filesystem the buffer belongs to
586  * @bh: The buffer to be pinned
587  *
588  */
589
590 void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
591 {
592         struct gfs2_bufdata *bd = bh->b_private;
593
594         gfs2_assert_withdraw(sdp, test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags));
595
596         if (test_set_buffer_pinned(bh))
597                 gfs2_assert_withdraw(sdp, 0);
598
599         wait_on_buffer(bh);
600
601         /* If this buffer is in the AIL and it has already been written
602            to in-place disk block, remove it from the AIL. */
603
604         gfs2_log_lock(sdp);
605         if (bd->bd_ail && !buffer_in_io(bh))
606                 list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list);
607         gfs2_log_unlock(sdp);
608
609         clear_buffer_dirty(bh);
610         wait_on_buffer(bh);
611
612         if (!buffer_uptodate(bh))
613                 gfs2_io_error_bh(sdp, bh);
614
615         get_bh(bh);
616 }
617
618 /**
619  * gfs2_unpin - Unpin a buffer
620  * @sdp: the filesystem the buffer belongs to
621  * @bh: The buffer to unpin
622  * @ai:
623  *
624  */
625
626 void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
627                 struct gfs2_ail *ai)
628 {
629         struct gfs2_bufdata *bd = bh->b_private;
630
631         gfs2_assert_withdraw(sdp, buffer_uptodate(bh));
632
633         if (!buffer_pinned(bh))
634                 gfs2_assert_withdraw(sdp, 0);
635
636         mark_buffer_dirty(bh);
637         clear_buffer_pinned(bh);
638
639         gfs2_log_lock(sdp);
640         if (bd->bd_ail) {
641                 list_del(&bd->bd_ail_st_list);
642                 brelse(bh);
643         } else {
644                 struct gfs2_glock *gl = bd->bd_gl;
645                 list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
646                 atomic_inc(&gl->gl_ail_count);
647         }
648         bd->bd_ail = ai;
649         list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list);
650         gfs2_log_unlock(sdp);
651 }
652
653 /**
654  * gfs2_meta_wipe - make inode's buffers so they aren't dirty/pinned anymore
655  * @ip: the inode who owns the buffers
656  * @bstart: the first buffer in the run
657  * @blen: the number of buffers in the run
658  *
659  */
660
661 void gfs2_meta_wipe(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
662 {
663         struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
664         struct inode *aspace = ip->i_gl->gl_aspace;
665         struct buffer_head *bh;
666
667         while (blen) {
668                 bh = getbuf(sdp, aspace, bstart, NO_CREATE);
669                 if (bh) {
670                         struct gfs2_bufdata *bd = bh->b_private;
671
672                         if (test_clear_buffer_pinned(bh)) {
673                                 struct gfs2_trans *tr = current->journal_info;
674                                 gfs2_log_lock(sdp);
675                                 list_del_init(&bd->bd_le.le_list);
676                                 gfs2_assert_warn(sdp, sdp->sd_log_num_buf);
677                                 sdp->sd_log_num_buf--;
678                                 gfs2_log_unlock(sdp);
679                                 tr->tr_num_buf_rm++;
680                                 brelse(bh);
681                         }
682                         if (bd) {
683                                 gfs2_log_lock(sdp);
684                                 if (bd->bd_ail) {
685                                         uint64_t blkno = bh->b_blocknr;
686                                         bd->bd_ail = NULL;
687                                         list_del(&bd->bd_ail_st_list);
688                                         list_del(&bd->bd_ail_gl_list);
689                                         atomic_dec(&bd->bd_gl->gl_ail_count);
690                                         brelse(bh);
691                                         gfs2_log_unlock(sdp);
692                                         gfs2_trans_add_revoke(sdp, blkno);
693                                 } else
694                                         gfs2_log_unlock(sdp);
695                         }
696
697                         lock_buffer(bh);
698                         clear_buffer_dirty(bh);
699                         clear_buffer_uptodate(bh);
700                         unlock_buffer(bh);
701
702                         brelse(bh);
703                 }
704
705                 bstart++;
706                 blen--;
707         }
708 }
709
710 /**
711  * gfs2_meta_cache_flush - get rid of any references on buffers for this inode
712  * @ip: The GFS2 inode
713  *
714  * This releases buffers that are in the most-recently-used array of
715  * blocks used for indirect block addressing for this inode.
716  */
717
718 void gfs2_meta_cache_flush(struct gfs2_inode *ip)
719 {
720         struct buffer_head **bh_slot;
721         unsigned int x;
722
723         spin_lock(&ip->i_spin);
724
725         for (x = 0; x < GFS2_MAX_META_HEIGHT; x++) {
726                 bh_slot = &ip->i_cache[x];
727                 if (!*bh_slot)
728                         break;
729                 brelse(*bh_slot);
730                 *bh_slot = NULL;
731         }
732
733         spin_unlock(&ip->i_spin);
734 }
735
736 /**
737  * gfs2_meta_indirect_buffer - Get a metadata buffer
738  * @ip: The GFS2 inode
739  * @height: The level of this buf in the metadata (indir addr) tree (if any)
740  * @num: The block number (device relative) of the buffer
741  * @new: Non-zero if we may create a new buffer
742  * @bhp: the buffer is returned here
743  *
744  * Try to use the gfs2_inode's MRU metadata tree cache.
745  *
746  * Returns: errno
747  */
748
749 int gfs2_meta_indirect_buffer(struct gfs2_inode *ip, int height, uint64_t num,
750                               int new, struct buffer_head **bhp)
751 {
752         struct buffer_head *bh, **bh_slot = ip->i_cache + height;
753         int error;
754
755         spin_lock(&ip->i_spin);
756         bh = *bh_slot;
757         if (bh) {
758                 if (bh->b_blocknr == num)
759                         get_bh(bh);
760                 else
761                         bh = NULL;
762         }
763         spin_unlock(&ip->i_spin);
764
765         if (bh) {
766                 if (new)
767                         meta_prep_new(bh);
768                 else {
769                         error = gfs2_meta_reread(GFS2_SB(&ip->i_inode), bh,
770                                                  DIO_START | DIO_WAIT);
771                         if (error) {
772                                 brelse(bh);
773                                 return error;
774                         }
775                 }
776         } else {
777                 if (new)
778                         bh = gfs2_meta_new(ip->i_gl, num);
779                 else {
780                         error = gfs2_meta_read(ip->i_gl, num,
781                                                DIO_START | DIO_WAIT, &bh);
782                         if (error)
783                                 return error;
784                 }
785
786                 spin_lock(&ip->i_spin);
787                 if (*bh_slot != bh) {
788                         brelse(*bh_slot);
789                         *bh_slot = bh;
790                         get_bh(bh);
791                 }
792                 spin_unlock(&ip->i_spin);
793         }
794
795         if (new) {
796                 if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), height)) {
797                         brelse(bh);
798                         return -EIO;
799                 }
800                 gfs2_trans_add_bh(ip->i_gl, bh, 1);
801                 gfs2_metatype_set(bh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
802                 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
803
804         } else if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh,
805                              (height) ? GFS2_METATYPE_IN : GFS2_METATYPE_DI)) {
806                 brelse(bh);
807                 return -EIO;
808         }
809
810         *bhp = bh;
811
812         return 0;
813 }
814
815 /**
816  * gfs2_meta_ra - start readahead on an extent of a file
817  * @gl: the glock the blocks belong to
818  * @dblock: the starting disk block
819  * @extlen: the number of blocks in the extent
820  *
821  */
822
823 void gfs2_meta_ra(struct gfs2_glock *gl, uint64_t dblock, uint32_t extlen)
824 {
825         struct gfs2_sbd *sdp = gl->gl_sbd;
826         struct inode *aspace = gl->gl_aspace;
827         struct buffer_head *first_bh, *bh;
828         uint32_t max_ra = gfs2_tune_get(sdp, gt_max_readahead) >>
829                           sdp->sd_sb.sb_bsize_shift;
830         int error;
831
832         if (!extlen || !max_ra)
833                 return;
834         if (extlen > max_ra)
835                 extlen = max_ra;
836
837         first_bh = getbuf(sdp, aspace, dblock, CREATE);
838
839         if (buffer_uptodate(first_bh))
840                 goto out;
841         if (!buffer_locked(first_bh)) {
842                 error = gfs2_meta_reread(sdp, first_bh, DIO_START);
843                 if (error)
844                         goto out;
845         }
846
847         dblock++;
848         extlen--;
849
850         while (extlen) {
851                 bh = getbuf(sdp, aspace, dblock, CREATE);
852
853                 if (!buffer_uptodate(bh) && !buffer_locked(bh)) {
854                         error = gfs2_meta_reread(sdp, bh, DIO_START);
855                         brelse(bh);
856                         if (error)
857                                 goto out;
858                 } else
859                         brelse(bh);
860
861                 dblock++;
862                 extlen--;
863
864                 if (buffer_uptodate(first_bh))
865                         break;
866         }
867
868  out:
869         brelse(first_bh);
870 }
871
872 /**
873  * gfs2_meta_syncfs - sync all the buffers in a filesystem
874  * @sdp: the filesystem
875  *
876  */
877
878 void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
879 {
880         gfs2_log_flush(sdp, NULL);
881         for (;;) {
882                 gfs2_ail1_start(sdp, DIO_ALL);
883                 if (gfs2_ail1_empty(sdp, DIO_ALL))
884                         break;
885                 msleep(10);
886         }
887 }
888