[GFS2] Detach buf data during in-place writeback
[linux-2.6-block.git] / fs / gfs2 / log.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 version 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/gfs2_ondisk.h>
16 #include <linux/crc32.h>
17 #include <linux/lm_interface.h>
18 #include <linux/delay.h>
19
20 #include "gfs2.h"
21 #include "incore.h"
22 #include "bmap.h"
23 #include "glock.h"
24 #include "log.h"
25 #include "lops.h"
26 #include "meta_io.h"
27 #include "util.h"
28 #include "dir.h"
29
30 #define PULL 1
31
32 /**
33  * gfs2_struct2blk - compute stuff
34  * @sdp: the filesystem
35  * @nstruct: the number of structures
36  * @ssize: the size of the structures
37  *
38  * Compute the number of log descriptor blocks needed to hold a certain number
39  * of structures of a certain size.
40  *
41  * Returns: the number of blocks needed (minimum is always 1)
42  */
43
44 unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
45                              unsigned int ssize)
46 {
47         unsigned int blks;
48         unsigned int first, second;
49
50         blks = 1;
51         first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
52
53         if (nstruct > first) {
54                 second = (sdp->sd_sb.sb_bsize -
55                           sizeof(struct gfs2_meta_header)) / ssize;
56                 blks += DIV_ROUND_UP(nstruct - first, second);
57         }
58
59         return blks;
60 }
61
62 /**
63  * gfs2_ail1_start_one - Start I/O on a part of the AIL
64  * @sdp: the filesystem
65  * @tr: the part of the AIL
66  *
67  */
68
69 static void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
70 {
71         struct gfs2_bufdata *bd, *s;
72         struct buffer_head *bh;
73         int retry;
74
75         BUG_ON(!spin_is_locked(&sdp->sd_log_lock));
76
77         do {
78                 retry = 0;
79
80                 list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
81                                                  bd_ail_st_list) {
82                         bh = bd->bd_bh;
83
84                         gfs2_assert(sdp, bd->bd_ail == ai);
85
86                         if (!buffer_busy(bh)) {
87                                 if (!buffer_uptodate(bh)) {
88                                         gfs2_log_unlock(sdp);
89                                         gfs2_io_error_bh(sdp, bh);
90                                         gfs2_log_lock(sdp);
91                                 }
92                                 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
93                                 continue;
94                         }
95
96                         if (!buffer_dirty(bh))
97                                 continue;
98
99                         list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
100
101                         gfs2_log_unlock(sdp);
102                         wait_on_buffer(bh);
103                         ll_rw_block(WRITE, 1, &bh);
104                         gfs2_log_lock(sdp);
105
106                         retry = 1;
107                         break;
108                 }
109         } while (retry);
110 }
111
112 /**
113  * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
114  * @sdp: the filesystem
115  * @ai: the AIL entry
116  *
117  */
118
119 static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
120 {
121         struct gfs2_bufdata *bd, *s;
122         struct buffer_head *bh;
123
124         list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
125                                          bd_ail_st_list) {
126                 bh = bd->bd_bh;
127
128                 gfs2_assert(sdp, bd->bd_ail == ai);
129
130                 if (buffer_busy(bh)) {
131                         if (flags & DIO_ALL)
132                                 continue;
133                         else
134                                 break;
135                 }
136
137                 if (!buffer_uptodate(bh))
138                         gfs2_io_error_bh(sdp, bh);
139
140                 list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
141         }
142
143         return list_empty(&ai->ai_ail1_list);
144 }
145
146 static void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
147 {
148         struct list_head *head;
149         u64 sync_gen;
150         struct list_head *first;
151         struct gfs2_ail *first_ai, *ai, *tmp;
152         int done = 0;
153
154         gfs2_log_lock(sdp);
155         head = &sdp->sd_ail1_list;
156         if (list_empty(head)) {
157                 gfs2_log_unlock(sdp);
158                 return;
159         }
160         sync_gen = sdp->sd_ail_sync_gen++;
161
162         first = head->prev;
163         first_ai = list_entry(first, struct gfs2_ail, ai_list);
164         first_ai->ai_sync_gen = sync_gen;
165         gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
166
167         if (flags & DIO_ALL)
168                 first = NULL;
169
170         while(!done) {
171                 if (first && (head->prev != first ||
172                               gfs2_ail1_empty_one(sdp, first_ai, 0)))
173                         break;
174
175                 done = 1;
176                 list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
177                         if (ai->ai_sync_gen >= sync_gen)
178                                 continue;
179                         ai->ai_sync_gen = sync_gen;
180                         gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
181                         done = 0;
182                         break;
183                 }
184         }
185
186         gfs2_log_unlock(sdp);
187 }
188
189 int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
190 {
191         struct gfs2_ail *ai, *s;
192         int ret;
193
194         gfs2_log_lock(sdp);
195
196         list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
197                 if (gfs2_ail1_empty_one(sdp, ai, flags))
198                         list_move(&ai->ai_list, &sdp->sd_ail2_list);
199                 else if (!(flags & DIO_ALL))
200                         break;
201         }
202
203         ret = list_empty(&sdp->sd_ail1_list);
204
205         gfs2_log_unlock(sdp);
206
207         return ret;
208 }
209
210
211 /**
212  * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
213  * @sdp: the filesystem
214  * @ai: the AIL entry
215  *
216  */
217
218 static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
219 {
220         struct list_head *head = &ai->ai_ail2_list;
221         struct gfs2_bufdata *bd;
222         struct gfs2_inode *bh_ip;
223
224         while (!list_empty(head)) {
225                 bd = list_entry(head->prev, struct gfs2_bufdata,
226                                 bd_ail_st_list);
227                 gfs2_assert(sdp, bd->bd_ail == ai);
228                 bd->bd_ail = NULL;
229                 list_del(&bd->bd_ail_st_list);
230                 list_del(&bd->bd_ail_gl_list);
231                 atomic_dec(&bd->bd_gl->gl_ail_count);
232                 bh_ip = GFS2_I(bd->bd_bh->b_page->mapping->host);
233                 gfs2_meta_cache_flush(bh_ip);
234                 brelse(bd->bd_bh);
235         }
236 }
237
238 static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
239 {
240         struct gfs2_ail *ai, *safe;
241         unsigned int old_tail = sdp->sd_log_tail;
242         int wrap = (new_tail < old_tail);
243         int a, b, rm;
244
245         gfs2_log_lock(sdp);
246
247         list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
248                 a = (old_tail <= ai->ai_first);
249                 b = (ai->ai_first < new_tail);
250                 rm = (wrap) ? (a || b) : (a && b);
251                 if (!rm)
252                         continue;
253
254                 gfs2_ail2_empty_one(sdp, ai);
255                 list_del(&ai->ai_list);
256                 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
257                 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
258                 kfree(ai);
259         }
260
261         gfs2_log_unlock(sdp);
262 }
263
264 /**
265  * gfs2_log_reserve - Make a log reservation
266  * @sdp: The GFS2 superblock
267  * @blks: The number of blocks to reserve
268  *
269  * Note that we never give out the last few blocks of the journal. Thats
270  * due to the fact that there is a small number of header blocks
271  * associated with each log flush. The exact number can't be known until
272  * flush time, so we ensure that we have just enough free blocks at all
273  * times to avoid running out during a log flush.
274  *
275  * Returns: errno
276  */
277
278 int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
279 {
280         unsigned int try = 0;
281         unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
282
283         if (gfs2_assert_warn(sdp, blks) ||
284             gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
285                 return -EINVAL;
286
287         mutex_lock(&sdp->sd_log_reserve_mutex);
288         gfs2_log_lock(sdp);
289         while(sdp->sd_log_blks_free <= (blks + reserved_blks)) {
290                 gfs2_log_unlock(sdp);
291                 gfs2_ail1_empty(sdp, 0);
292                 gfs2_log_flush(sdp, NULL);
293
294                 if (try++)
295                         gfs2_ail1_start(sdp, 0);
296                 gfs2_log_lock(sdp);
297         }
298         sdp->sd_log_blks_free -= blks;
299         gfs2_log_unlock(sdp);
300         mutex_unlock(&sdp->sd_log_reserve_mutex);
301
302         down_read(&sdp->sd_log_flush_lock);
303
304         return 0;
305 }
306
307 /**
308  * gfs2_log_release - Release a given number of log blocks
309  * @sdp: The GFS2 superblock
310  * @blks: The number of blocks
311  *
312  */
313
314 void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
315 {
316
317         gfs2_log_lock(sdp);
318         sdp->sd_log_blks_free += blks;
319         gfs2_assert_withdraw(sdp,
320                              sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
321         gfs2_log_unlock(sdp);
322         up_read(&sdp->sd_log_flush_lock);
323 }
324
325 static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
326 {
327         struct inode *inode = sdp->sd_jdesc->jd_inode;
328         int error;
329         struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
330
331         bh_map.b_size = 1 << inode->i_blkbits;
332         error = gfs2_block_map(inode, lbn, 0, &bh_map);
333         if (error || !bh_map.b_blocknr)
334                 printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error,
335                        (unsigned long long)bh_map.b_blocknr, lbn);
336         gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
337
338         return bh_map.b_blocknr;
339 }
340
341 /**
342  * log_distance - Compute distance between two journal blocks
343  * @sdp: The GFS2 superblock
344  * @newer: The most recent journal block of the pair
345  * @older: The older journal block of the pair
346  *
347  *   Compute the distance (in the journal direction) between two
348  *   blocks in the journal
349  *
350  * Returns: the distance in blocks
351  */
352
353 static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
354                                         unsigned int older)
355 {
356         int dist;
357
358         dist = newer - older;
359         if (dist < 0)
360                 dist += sdp->sd_jdesc->jd_blocks;
361
362         return dist;
363 }
364
365 /**
366  * calc_reserved - Calculate the number of blocks to reserve when
367  *                 refunding a transaction's unused buffers.
368  * @sdp: The GFS2 superblock
369  *
370  * This is complex.  We need to reserve room for all our currently used
371  * metadata buffers (e.g. normal file I/O rewriting file time stamps) and 
372  * all our journaled data buffers for journaled files (e.g. files in the 
373  * meta_fs like rindex, or files for which chattr +j was done.)
374  * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
375  * will count it as free space (sd_log_blks_free) and corruption will follow.
376  *
377  * We can have metadata bufs and jdata bufs in the same journal.  So each
378  * type gets its own log header, for which we need to reserve a block.
379  * In fact, each type has the potential for needing more than one header 
380  * in cases where we have more buffers than will fit on a journal page.
381  * Metadata journal entries take up half the space of journaled buffer entries.
382  * Thus, metadata entries have buf_limit (502) and journaled buffers have
383  * databuf_limit (251) before they cause a wrap around.
384  *
385  * Also, we need to reserve blocks for revoke journal entries and one for an
386  * overall header for the lot.
387  *
388  * Returns: the number of blocks reserved
389  */
390 static unsigned int calc_reserved(struct gfs2_sbd *sdp)
391 {
392         unsigned int reserved = 0;
393         unsigned int mbuf_limit, metabufhdrs_needed;
394         unsigned int dbuf_limit, databufhdrs_needed;
395         unsigned int revokes = 0;
396
397         mbuf_limit = buf_limit(sdp);
398         metabufhdrs_needed = (sdp->sd_log_commited_buf +
399                               (mbuf_limit - 1)) / mbuf_limit;
400         dbuf_limit = databuf_limit(sdp);
401         databufhdrs_needed = (sdp->sd_log_commited_databuf +
402                               (dbuf_limit - 1)) / dbuf_limit;
403
404         if (sdp->sd_log_commited_revoke)
405                 revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
406                                           sizeof(u64));
407
408         reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
409                 sdp->sd_log_commited_databuf + databufhdrs_needed +
410                 revokes;
411         /* One for the overall header */
412         if (reserved)
413                 reserved++;
414         return reserved;
415 }
416
417 static unsigned int current_tail(struct gfs2_sbd *sdp)
418 {
419         struct gfs2_ail *ai;
420         unsigned int tail;
421
422         gfs2_log_lock(sdp);
423
424         if (list_empty(&sdp->sd_ail1_list)) {
425                 tail = sdp->sd_log_head;
426         } else {
427                 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
428                 tail = ai->ai_first;
429         }
430
431         gfs2_log_unlock(sdp);
432
433         return tail;
434 }
435
436 static inline void log_incr_head(struct gfs2_sbd *sdp)
437 {
438         if (sdp->sd_log_flush_head == sdp->sd_log_tail)
439                 gfs2_assert_withdraw(sdp, sdp->sd_log_flush_head == sdp->sd_log_head);
440
441         if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
442                 sdp->sd_log_flush_head = 0;
443                 sdp->sd_log_flush_wrapped = 1;
444         }
445 }
446
447 /**
448  * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
449  * @sdp: The GFS2 superblock
450  *
451  * Returns: the buffer_head
452  */
453
454 struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
455 {
456         u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
457         struct gfs2_log_buf *lb;
458         struct buffer_head *bh;
459
460         lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
461         list_add(&lb->lb_list, &sdp->sd_log_flush_list);
462
463         bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
464         lock_buffer(bh);
465         memset(bh->b_data, 0, bh->b_size);
466         set_buffer_uptodate(bh);
467         clear_buffer_dirty(bh);
468         unlock_buffer(bh);
469
470         log_incr_head(sdp);
471
472         return bh;
473 }
474
475 /**
476  * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
477  * @sdp: the filesystem
478  * @data: the data the buffer_head should point to
479  *
480  * Returns: the log buffer descriptor
481  */
482
483 struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
484                                       struct buffer_head *real)
485 {
486         u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
487         struct gfs2_log_buf *lb;
488         struct buffer_head *bh;
489
490         lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
491         list_add(&lb->lb_list, &sdp->sd_log_flush_list);
492         lb->lb_real = real;
493
494         bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
495         atomic_set(&bh->b_count, 1);
496         bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
497         set_bh_page(bh, real->b_page, bh_offset(real));
498         bh->b_blocknr = blkno;
499         bh->b_size = sdp->sd_sb.sb_bsize;
500         bh->b_bdev = sdp->sd_vfs->s_bdev;
501
502         log_incr_head(sdp);
503
504         return bh;
505 }
506
507 static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
508 {
509         unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
510
511         ail2_empty(sdp, new_tail);
512
513         gfs2_log_lock(sdp);
514         sdp->sd_log_blks_free += dist;
515         gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
516         gfs2_log_unlock(sdp);
517
518         sdp->sd_log_tail = new_tail;
519 }
520
521 /**
522  * log_write_header - Get and initialize a journal header buffer
523  * @sdp: The GFS2 superblock
524  *
525  * Returns: the initialized log buffer descriptor
526  */
527
528 static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
529 {
530         u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
531         struct buffer_head *bh;
532         struct gfs2_log_header *lh;
533         unsigned int tail;
534         u32 hash;
535
536         bh = sb_getblk(sdp->sd_vfs, blkno);
537         lock_buffer(bh);
538         memset(bh->b_data, 0, bh->b_size);
539         set_buffer_uptodate(bh);
540         clear_buffer_dirty(bh);
541         unlock_buffer(bh);
542
543         gfs2_ail1_empty(sdp, 0);
544         tail = current_tail(sdp);
545
546         lh = (struct gfs2_log_header *)bh->b_data;
547         memset(lh, 0, sizeof(struct gfs2_log_header));
548         lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
549         lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
550         lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
551         lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
552         lh->lh_flags = cpu_to_be32(flags);
553         lh->lh_tail = cpu_to_be32(tail);
554         lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
555         hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
556         lh->lh_hash = cpu_to_be32(hash);
557
558         set_buffer_dirty(bh);
559         if (sync_dirty_buffer(bh))
560                 gfs2_io_error_bh(sdp, bh);
561         brelse(bh);
562
563         if (sdp->sd_log_tail != tail)
564                 log_pull_tail(sdp, tail);
565         else
566                 gfs2_assert_withdraw(sdp, !pull);
567
568         sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
569         log_incr_head(sdp);
570 }
571
572 static void log_flush_commit(struct gfs2_sbd *sdp)
573 {
574         struct list_head *head = &sdp->sd_log_flush_list;
575         struct gfs2_log_buf *lb;
576         struct buffer_head *bh;
577         int flushcount = 0;
578
579         while (!list_empty(head)) {
580                 lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
581                 list_del(&lb->lb_list);
582                 bh = lb->lb_bh;
583
584                 wait_on_buffer(bh);
585                 if (!buffer_uptodate(bh))
586                         gfs2_io_error_bh(sdp, bh);
587                 if (lb->lb_real) {
588                         while (atomic_read(&bh->b_count) != 1)  /* Grrrr... */
589                                 schedule();
590                         free_buffer_head(bh);
591                 } else
592                         brelse(bh);
593                 kfree(lb);
594                 flushcount++;
595         }
596
597         /* If nothing was journaled, the header is unplanned and unwanted. */
598         if (flushcount) {
599                 log_write_header(sdp, 0, 0);
600         } else {
601                 unsigned int tail;
602                 tail = current_tail(sdp);
603
604                 gfs2_ail1_empty(sdp, 0);
605                 if (sdp->sd_log_tail != tail)
606                         log_pull_tail(sdp, tail);
607         }
608 }
609
610 /**
611  * gfs2_log_flush - flush incore transaction(s)
612  * @sdp: the filesystem
613  * @gl: The glock structure to flush.  If NULL, flush the whole incore log
614  *
615  */
616
617 void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
618 {
619         struct gfs2_ail *ai;
620
621         down_write(&sdp->sd_log_flush_lock);
622
623         if (gl) {
624                 gfs2_log_lock(sdp);
625                 if (list_empty(&gl->gl_le.le_list)) {
626                         gfs2_log_unlock(sdp);
627                         up_write(&sdp->sd_log_flush_lock);
628                         return;
629                 }
630                 gfs2_log_unlock(sdp);
631         }
632
633         ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
634         INIT_LIST_HEAD(&ai->ai_ail1_list);
635         INIT_LIST_HEAD(&ai->ai_ail2_list);
636
637         gfs2_assert_withdraw(sdp,
638                              sdp->sd_log_num_buf + sdp->sd_log_num_jdata ==
639                              sdp->sd_log_commited_buf +
640                              sdp->sd_log_commited_databuf);
641         gfs2_assert_withdraw(sdp,
642                         sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
643
644         sdp->sd_log_flush_head = sdp->sd_log_head;
645         sdp->sd_log_flush_wrapped = 0;
646         ai->ai_first = sdp->sd_log_flush_head;
647
648         lops_before_commit(sdp);
649         if (!list_empty(&sdp->sd_log_flush_list))
650                 log_flush_commit(sdp);
651         else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
652                 gfs2_log_lock(sdp);
653                 sdp->sd_log_blks_free--; /* Adjust for unreserved buffer */
654                 gfs2_log_unlock(sdp);
655                 log_write_header(sdp, 0, PULL);
656         }
657         lops_after_commit(sdp, ai);
658
659         gfs2_log_lock(sdp);
660         sdp->sd_log_head = sdp->sd_log_flush_head;
661         sdp->sd_log_blks_reserved = 0;
662         sdp->sd_log_commited_buf = 0;
663         sdp->sd_log_commited_databuf = 0;
664         sdp->sd_log_commited_revoke = 0;
665
666         if (!list_empty(&ai->ai_ail1_list)) {
667                 list_add(&ai->ai_list, &sdp->sd_ail1_list);
668                 ai = NULL;
669         }
670         gfs2_log_unlock(sdp);
671
672         sdp->sd_vfs->s_dirt = 0;
673         up_write(&sdp->sd_log_flush_lock);
674
675         kfree(ai);
676 }
677
678 static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
679 {
680         unsigned int reserved;
681         unsigned int old;
682
683         gfs2_log_lock(sdp);
684
685         sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
686         sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
687                 tr->tr_num_databuf_rm;
688         gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
689                              (((int)sdp->sd_log_commited_databuf) >= 0));
690         sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
691         gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
692         reserved = calc_reserved(sdp);
693         old = sdp->sd_log_blks_free;
694         sdp->sd_log_blks_free += tr->tr_reserved -
695                                  (reserved - sdp->sd_log_blks_reserved);
696
697         gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
698         gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <=
699                              sdp->sd_jdesc->jd_blocks);
700
701         sdp->sd_log_blks_reserved = reserved;
702
703         gfs2_log_unlock(sdp);
704 }
705
706 /**
707  * gfs2_log_commit - Commit a transaction to the log
708  * @sdp: the filesystem
709  * @tr: the transaction
710  *
711  * Returns: errno
712  */
713
714 void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
715 {
716         log_refund(sdp, tr);
717         lops_incore_commit(sdp, tr);
718
719         sdp->sd_vfs->s_dirt = 1;
720         up_read(&sdp->sd_log_flush_lock);
721
722         gfs2_log_lock(sdp);
723         if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks))
724                 wake_up_process(sdp->sd_logd_process);
725         gfs2_log_unlock(sdp);
726 }
727
728 /**
729  * gfs2_log_shutdown - write a shutdown header into a journal
730  * @sdp: the filesystem
731  *
732  */
733
734 void gfs2_log_shutdown(struct gfs2_sbd *sdp)
735 {
736         down_write(&sdp->sd_log_flush_lock);
737
738         gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
739         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
740         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
741         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
742         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
743         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
744         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
745         gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
746
747         sdp->sd_log_flush_head = sdp->sd_log_head;
748         sdp->sd_log_flush_wrapped = 0;
749
750         log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
751                          (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
752
753         gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
754         gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
755         gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
756
757         sdp->sd_log_head = sdp->sd_log_flush_head;
758         sdp->sd_log_tail = sdp->sd_log_head;
759
760         up_write(&sdp->sd_log_flush_lock);
761 }
762
763
764 /**
765  * gfs2_meta_syncfs - sync all the buffers in a filesystem
766  * @sdp: the filesystem
767  *
768  */
769
770 void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
771 {
772         gfs2_log_flush(sdp, NULL);
773         for (;;) {
774                 gfs2_ail1_start(sdp, DIO_ALL);
775                 if (gfs2_ail1_empty(sdp, DIO_ALL))
776                         break;
777                 msleep(10);
778         }
779 }
780