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