]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/gfs2/lops.c
[GFS2] Move some code inside the log lock
[linux-2.6-omap-h63xx.git] / fs / gfs2 / lops.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/lm_interface.h>
17
18 #include "gfs2.h"
19 #include "incore.h"
20 #include "inode.h"
21 #include "glock.h"
22 #include "log.h"
23 #include "lops.h"
24 #include "meta_io.h"
25 #include "recovery.h"
26 #include "rgrp.h"
27 #include "trans.h"
28 #include "util.h"
29
30 static void glock_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
31 {
32         struct gfs2_glock *gl;
33         struct gfs2_trans *tr = current->journal_info;
34
35         tr->tr_touched = 1;
36
37         gl = container_of(le, struct gfs2_glock, gl_le);
38         if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl)))
39                 return;
40
41         gfs2_log_lock(sdp);
42         if (!list_empty(&le->le_list)){
43                 gfs2_log_unlock(sdp);
44                 return;
45         }
46         gfs2_glock_hold(gl);
47         set_bit(GLF_DIRTY, &gl->gl_flags);
48         sdp->sd_log_num_gl++;
49         list_add(&le->le_list, &sdp->sd_log_le_gl);
50         gfs2_log_unlock(sdp);
51 }
52
53 static void glock_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
54 {
55         struct list_head *head = &sdp->sd_log_le_gl;
56         struct gfs2_glock *gl;
57
58         while (!list_empty(head)) {
59                 gl = list_entry(head->next, struct gfs2_glock, gl_le.le_list);
60                 list_del_init(&gl->gl_le.le_list);
61                 sdp->sd_log_num_gl--;
62
63                 gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(gl));
64                 gfs2_glock_put(gl);
65         }
66         gfs2_assert_warn(sdp, !sdp->sd_log_num_gl);
67 }
68
69 static void buf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
70 {
71         struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
72         struct gfs2_trans *tr;
73
74         gfs2_log_lock(sdp);
75         if (!list_empty(&bd->bd_list_tr)) {
76                 gfs2_log_unlock(sdp);
77                 return;
78         }
79         tr = current->journal_info;
80         tr->tr_touched = 1;
81         tr->tr_num_buf++;
82         list_add(&bd->bd_list_tr, &tr->tr_list_buf);
83         gfs2_log_unlock(sdp);
84
85         if (!list_empty(&le->le_list))
86                 return;
87
88         gfs2_trans_add_gl(bd->bd_gl);
89
90         gfs2_meta_check(sdp, bd->bd_bh);
91         gfs2_pin(sdp, bd->bd_bh);
92         gfs2_log_lock(sdp);
93         sdp->sd_log_num_buf++;
94         list_add(&le->le_list, &sdp->sd_log_le_buf);
95         gfs2_log_unlock(sdp);
96
97         tr->tr_num_buf_new++;
98 }
99
100 static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
101 {
102         struct list_head *head = &tr->tr_list_buf;
103         struct gfs2_bufdata *bd;
104
105         gfs2_log_lock(sdp);
106         while (!list_empty(head)) {
107                 bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
108                 list_del_init(&bd->bd_list_tr);
109                 tr->tr_num_buf--;
110         }
111         gfs2_log_unlock(sdp);
112         gfs2_assert_warn(sdp, !tr->tr_num_buf);
113 }
114
115 static void buf_lo_before_commit(struct gfs2_sbd *sdp)
116 {
117         struct buffer_head *bh;
118         struct gfs2_log_descriptor *ld;
119         struct gfs2_bufdata *bd1 = NULL, *bd2;
120         unsigned int total;
121         unsigned int offset = BUF_OFFSET;
122         unsigned int limit;
123         unsigned int num;
124         unsigned n;
125         __be64 *ptr;
126
127         limit = buf_limit(sdp);
128         /* for 4k blocks, limit = 503 */
129
130         gfs2_log_lock(sdp);
131         total = sdp->sd_log_num_buf;
132         bd1 = bd2 = list_prepare_entry(bd1, &sdp->sd_log_le_buf, bd_le.le_list);
133         while(total) {
134                 num = total;
135                 if (total > limit)
136                         num = limit;
137                 gfs2_log_unlock(sdp);
138                 bh = gfs2_log_get_buf(sdp);
139                 gfs2_log_lock(sdp);
140                 ld = (struct gfs2_log_descriptor *)bh->b_data;
141                 ptr = (__be64 *)(bh->b_data + offset);
142                 ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
143                 ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
144                 ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
145                 ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_METADATA);
146                 ld->ld_length = cpu_to_be32(num + 1);
147                 ld->ld_data1 = cpu_to_be32(num);
148                 ld->ld_data2 = cpu_to_be32(0);
149                 memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
150
151                 n = 0;
152                 list_for_each_entry_continue(bd1, &sdp->sd_log_le_buf,
153                                              bd_le.le_list) {
154                         *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr);
155                         if (++n >= num)
156                                 break;
157                 }
158
159                 gfs2_log_unlock(sdp);
160                 set_buffer_dirty(bh);
161                 ll_rw_block(WRITE, 1, &bh);
162                 gfs2_log_lock(sdp);
163
164                 n = 0;
165                 list_for_each_entry_continue(bd2, &sdp->sd_log_le_buf,
166                                              bd_le.le_list) {
167                         gfs2_log_unlock(sdp);
168                         bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
169                         set_buffer_dirty(bh);
170                         ll_rw_block(WRITE, 1, &bh);
171                         gfs2_log_lock(sdp);
172                         if (++n >= num)
173                                 break;
174                 }
175
176                 BUG_ON(total < num);
177                 total -= num;
178         }
179         gfs2_log_unlock(sdp);
180 }
181
182 static void buf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
183 {
184         struct list_head *head = &sdp->sd_log_le_buf;
185         struct gfs2_bufdata *bd;
186
187         while (!list_empty(head)) {
188                 bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
189                 list_del_init(&bd->bd_le.le_list);
190                 sdp->sd_log_num_buf--;
191
192                 gfs2_unpin(sdp, bd->bd_bh, ai);
193         }
194         gfs2_assert_warn(sdp, !sdp->sd_log_num_buf);
195 }
196
197 static void buf_lo_before_scan(struct gfs2_jdesc *jd,
198                                struct gfs2_log_header_host *head, int pass)
199 {
200         struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
201
202         if (pass != 0)
203                 return;
204
205         sdp->sd_found_blocks = 0;
206         sdp->sd_replayed_blocks = 0;
207 }
208
209 static int buf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
210                                 struct gfs2_log_descriptor *ld, __be64 *ptr,
211                                 int pass)
212 {
213         struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
214         struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
215         struct gfs2_glock *gl = ip->i_gl;
216         unsigned int blks = be32_to_cpu(ld->ld_data1);
217         struct buffer_head *bh_log, *bh_ip;
218         u64 blkno;
219         int error = 0;
220
221         if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_METADATA)
222                 return 0;
223
224         gfs2_replay_incr_blk(sdp, &start);
225
226         for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
227                 blkno = be64_to_cpu(*ptr++);
228
229                 sdp->sd_found_blocks++;
230
231                 if (gfs2_revoke_check(sdp, blkno, start))
232                         continue;
233
234                 error = gfs2_replay_read_block(jd, start, &bh_log);
235                 if (error)
236                         return error;
237
238                 bh_ip = gfs2_meta_new(gl, blkno);
239                 memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
240
241                 if (gfs2_meta_check(sdp, bh_ip))
242                         error = -EIO;
243                 else
244                         mark_buffer_dirty(bh_ip);
245
246                 brelse(bh_log);
247                 brelse(bh_ip);
248
249                 if (error)
250                         break;
251
252                 sdp->sd_replayed_blocks++;
253         }
254
255         return error;
256 }
257
258 static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
259 {
260         struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
261         struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
262
263         if (error) {
264                 gfs2_meta_sync(ip->i_gl);
265                 return;
266         }
267         if (pass != 1)
268                 return;
269
270         gfs2_meta_sync(ip->i_gl);
271
272         fs_info(sdp, "jid=%u: Replayed %u of %u blocks\n",
273                 jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
274 }
275
276 static void revoke_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
277 {
278         struct gfs2_trans *tr;
279
280         tr = current->journal_info;
281         tr->tr_touched = 1;
282         tr->tr_num_revoke++;
283
284         gfs2_log_lock(sdp);
285         sdp->sd_log_num_revoke++;
286         list_add(&le->le_list, &sdp->sd_log_le_revoke);
287         gfs2_log_unlock(sdp);
288 }
289
290 static void revoke_lo_before_commit(struct gfs2_sbd *sdp)
291 {
292         struct gfs2_log_descriptor *ld;
293         struct gfs2_meta_header *mh;
294         struct buffer_head *bh;
295         unsigned int offset;
296         struct list_head *head = &sdp->sd_log_le_revoke;
297         struct gfs2_revoke *rv;
298
299         if (!sdp->sd_log_num_revoke)
300                 return;
301
302         bh = gfs2_log_get_buf(sdp);
303         ld = (struct gfs2_log_descriptor *)bh->b_data;
304         ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
305         ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
306         ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
307         ld->ld_type = cpu_to_be32(GFS2_LOG_DESC_REVOKE);
308         ld->ld_length = cpu_to_be32(gfs2_struct2blk(sdp, sdp->sd_log_num_revoke,
309                                                     sizeof(u64)));
310         ld->ld_data1 = cpu_to_be32(sdp->sd_log_num_revoke);
311         ld->ld_data2 = cpu_to_be32(0);
312         memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
313         offset = sizeof(struct gfs2_log_descriptor);
314
315         while (!list_empty(head)) {
316                 rv = list_entry(head->next, struct gfs2_revoke, rv_le.le_list);
317                 list_del_init(&rv->rv_le.le_list);
318                 sdp->sd_log_num_revoke--;
319
320                 if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) {
321                         set_buffer_dirty(bh);
322                         ll_rw_block(WRITE, 1, &bh);
323
324                         bh = gfs2_log_get_buf(sdp);
325                         mh = (struct gfs2_meta_header *)bh->b_data;
326                         mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
327                         mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB);
328                         mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB);
329                         offset = sizeof(struct gfs2_meta_header);
330                 }
331
332                 *(__be64 *)(bh->b_data + offset) = cpu_to_be64(rv->rv_blkno);
333                 kfree(rv);
334
335                 offset += sizeof(u64);
336         }
337         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
338
339         set_buffer_dirty(bh);
340         ll_rw_block(WRITE, 1, &bh);
341 }
342
343 static void revoke_lo_before_scan(struct gfs2_jdesc *jd,
344                                   struct gfs2_log_header_host *head, int pass)
345 {
346         struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
347
348         if (pass != 0)
349                 return;
350
351         sdp->sd_found_revokes = 0;
352         sdp->sd_replay_tail = head->lh_tail;
353 }
354
355 static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
356                                    struct gfs2_log_descriptor *ld, __be64 *ptr,
357                                    int pass)
358 {
359         struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
360         unsigned int blks = be32_to_cpu(ld->ld_length);
361         unsigned int revokes = be32_to_cpu(ld->ld_data1);
362         struct buffer_head *bh;
363         unsigned int offset;
364         u64 blkno;
365         int first = 1;
366         int error;
367
368         if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE)
369                 return 0;
370
371         offset = sizeof(struct gfs2_log_descriptor);
372
373         for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
374                 error = gfs2_replay_read_block(jd, start, &bh);
375                 if (error)
376                         return error;
377
378                 if (!first)
379                         gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LB);
380
381                 while (offset + sizeof(u64) <= sdp->sd_sb.sb_bsize) {
382                         blkno = be64_to_cpu(*(__be64 *)(bh->b_data + offset));
383
384                         error = gfs2_revoke_add(sdp, blkno, start);
385                         if (error < 0)
386                                 return error;
387                         else if (error)
388                                 sdp->sd_found_revokes++;
389
390                         if (!--revokes)
391                                 break;
392                         offset += sizeof(u64);
393                 }
394
395                 brelse(bh);
396                 offset = sizeof(struct gfs2_meta_header);
397                 first = 0;
398         }
399
400         return 0;
401 }
402
403 static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
404 {
405         struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
406
407         if (error) {
408                 gfs2_revoke_clean(sdp);
409                 return;
410         }
411         if (pass != 1)
412                 return;
413
414         fs_info(sdp, "jid=%u: Found %u revoke tags\n",
415                 jd->jd_jid, sdp->sd_found_revokes);
416
417         gfs2_revoke_clean(sdp);
418 }
419
420 static void rg_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
421 {
422         struct gfs2_rgrpd *rgd;
423         struct gfs2_trans *tr = current->journal_info;
424
425         tr->tr_touched = 1;
426
427         rgd = container_of(le, struct gfs2_rgrpd, rd_le);
428
429         gfs2_log_lock(sdp);
430         if (!list_empty(&le->le_list)){
431                 gfs2_log_unlock(sdp);
432                 return;
433         }
434         gfs2_rgrp_bh_hold(rgd);
435         sdp->sd_log_num_rg++;
436         list_add(&le->le_list, &sdp->sd_log_le_rg);
437         gfs2_log_unlock(sdp);
438 }
439
440 static void rg_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
441 {
442         struct list_head *head = &sdp->sd_log_le_rg;
443         struct gfs2_rgrpd *rgd;
444
445         while (!list_empty(head)) {
446                 rgd = list_entry(head->next, struct gfs2_rgrpd, rd_le.le_list);
447                 list_del_init(&rgd->rd_le.le_list);
448                 sdp->sd_log_num_rg--;
449
450                 gfs2_rgrp_repolish_clones(rgd);
451                 gfs2_rgrp_bh_put(rgd);
452         }
453         gfs2_assert_warn(sdp, !sdp->sd_log_num_rg);
454 }
455
456 /**
457  * databuf_lo_add - Add a databuf to the transaction.
458  *
459  * This is used in two distinct cases:
460  * i) In ordered write mode
461  *    We put the data buffer on a list so that we can ensure that its
462  *    synced to disk at the right time
463  * ii) In journaled data mode
464  *    We need to journal the data block in the same way as metadata in
465  *    the functions above. The difference is that here we have a tag
466  *    which is two __be64's being the block number (as per meta data)
467  *    and a flag which says whether the data block needs escaping or
468  *    not. This means we need a new log entry for each 251 or so data
469  *    blocks, which isn't an enormous overhead but twice as much as
470  *    for normal metadata blocks.
471  */
472 static void databuf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le)
473 {
474         struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le);
475         struct gfs2_trans *tr = current->journal_info;
476         struct address_space *mapping = bd->bd_bh->b_page->mapping;
477         struct gfs2_inode *ip = GFS2_I(mapping->host);
478
479         gfs2_log_lock(sdp);
480         if (!list_empty(&bd->bd_list_tr)) {
481                 gfs2_log_unlock(sdp);
482                 return;
483         }
484         tr->tr_touched = 1;
485         if (gfs2_is_jdata(ip)) {
486                 tr->tr_num_buf++;
487                 list_add(&bd->bd_list_tr, &tr->tr_list_buf);
488         }
489         gfs2_log_unlock(sdp);
490         if (!list_empty(&le->le_list))
491                 return;
492
493         gfs2_trans_add_gl(bd->bd_gl);
494         if (gfs2_is_jdata(ip)) {
495                 sdp->sd_log_num_jdata++;
496                 gfs2_pin(sdp, bd->bd_bh);
497                 tr->tr_num_databuf_new++;
498         }
499         gfs2_log_lock(sdp);
500         sdp->sd_log_num_databuf++;
501         list_add(&le->le_list, &sdp->sd_log_le_databuf);
502         gfs2_log_unlock(sdp);
503 }
504
505 static int gfs2_check_magic(struct buffer_head *bh)
506 {
507         struct page *page = bh->b_page;
508         void *kaddr;
509         __be32 *ptr;
510         int rv = 0;
511
512         kaddr = kmap_atomic(page, KM_USER0);
513         ptr = kaddr + bh_offset(bh);
514         if (*ptr == cpu_to_be32(GFS2_MAGIC))
515                 rv = 1;
516         kunmap_atomic(kaddr, KM_USER0);
517
518         return rv;
519 }
520
521 /**
522  * databuf_lo_before_commit - Scan the data buffers, writing as we go
523  *
524  * Here we scan through the lists of buffers and make the assumption
525  * that any buffer thats been pinned is being journaled, and that
526  * any unpinned buffer is an ordered write data buffer and therefore
527  * will be written back rather than journaled.
528  */
529 static void databuf_lo_before_commit(struct gfs2_sbd *sdp)
530 {
531         LIST_HEAD(started);
532         struct gfs2_bufdata *bd1 = NULL, *bd2, *bdt;
533         struct buffer_head *bh = NULL,*bh1 = NULL;
534         struct gfs2_log_descriptor *ld;
535         unsigned int limit;
536         unsigned int total_dbuf;
537         unsigned int total_jdata;
538         unsigned int num, n;
539         __be64 *ptr = NULL;
540
541         limit = databuf_limit(sdp);
542
543         /*
544          * Start writing ordered buffers, write journaled buffers
545          * into the log along with a header
546          */
547         gfs2_log_lock(sdp);
548         total_dbuf = sdp->sd_log_num_databuf;
549         total_jdata = sdp->sd_log_num_jdata;
550         bd2 = bd1 = list_prepare_entry(bd1, &sdp->sd_log_le_databuf,
551                                        bd_le.le_list);
552         while(total_dbuf) {
553                 num = total_jdata;
554                 if (num > limit)
555                         num = limit;
556                 n = 0;
557                 list_for_each_entry_safe_continue(bd1, bdt,
558                                                   &sdp->sd_log_le_databuf,
559                                                   bd_le.le_list) {
560                         /* store off the buffer head in a local ptr since
561                          * gfs2_bufdata might change when we drop the log lock
562                          */
563                         bh1 = bd1->bd_bh;
564
565                         /* An ordered write buffer */
566                         if (bh1 && !buffer_pinned(bh1)) {
567                                 list_move(&bd1->bd_le.le_list, &started);
568                                 if (bd1 == bd2) {
569                                         bd2 = NULL;
570                                         bd2 = list_prepare_entry(bd2,
571                                                         &sdp->sd_log_le_databuf,
572                                                         bd_le.le_list);
573                                 }
574                                 total_dbuf--;
575                                 if (bh1) {
576                                         if (buffer_dirty(bh1)) {
577                                                 get_bh(bh1);
578
579                                                 gfs2_log_unlock(sdp);
580
581                                                 ll_rw_block(SWRITE, 1, &bh1);
582                                                 brelse(bh1);
583
584                                                 gfs2_log_lock(sdp);
585                                         }
586                                         continue;
587                                 }
588                                 continue;
589                         } else if (bh1) { /* A journaled buffer */
590                                 int magic;
591                                 gfs2_log_unlock(sdp);
592                                 if (!bh) {
593                                         bh = gfs2_log_get_buf(sdp);
594                                         ld = (struct gfs2_log_descriptor *)
595                                              bh->b_data;
596                                         ptr = (__be64 *)(bh->b_data +
597                                                          DATABUF_OFFSET);
598                                         ld->ld_header.mh_magic =
599                                                 cpu_to_be32(GFS2_MAGIC);
600                                         ld->ld_header.mh_type =
601                                                 cpu_to_be32(GFS2_METATYPE_LD);
602                                         ld->ld_header.mh_format =
603                                                 cpu_to_be32(GFS2_FORMAT_LD);
604                                         ld->ld_type =
605                                                 cpu_to_be32(GFS2_LOG_DESC_JDATA);
606                                         ld->ld_length = cpu_to_be32(num + 1);
607                                         ld->ld_data1 = cpu_to_be32(num);
608                                         ld->ld_data2 = cpu_to_be32(0);
609                                         memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
610                                 }
611                                 magic = gfs2_check_magic(bh1);
612                                 *ptr++ = cpu_to_be64(bh1->b_blocknr);
613                                 *ptr++ = cpu_to_be64((__u64)magic);
614                                 clear_buffer_escaped(bh1);
615                                 if (unlikely(magic != 0))
616                                         set_buffer_escaped(bh1);
617                                 gfs2_log_lock(sdp);
618                                 if (++n >= num)
619                                         break;
620                         } else if (!bh1) {
621                                 total_dbuf--;
622                                 sdp->sd_log_num_databuf--;
623                                 list_del_init(&bd1->bd_le.le_list);
624                                 if (bd1 == bd2) {
625                                         bd2 = NULL;
626                                         bd2 = list_prepare_entry(bd2,
627                                                 &sdp->sd_log_le_databuf,
628                                                 bd_le.le_list);
629                                 }
630                                 kmem_cache_free(gfs2_bufdata_cachep, bd1);
631                         }
632                 }
633                 gfs2_log_unlock(sdp);
634                 if (bh) {
635                         set_buffer_dirty(bh);
636                         ll_rw_block(WRITE, 1, &bh);
637                         bh = NULL;
638                         ptr = NULL;
639                 }
640                 n = 0;
641                 gfs2_log_lock(sdp);
642                 list_for_each_entry_continue(bd2, &sdp->sd_log_le_databuf,
643                                              bd_le.le_list) {
644                         if (!bd2->bd_bh)
645                                 continue;
646                         /* copy buffer if it needs escaping */
647                         gfs2_log_unlock(sdp);
648                         if (unlikely(buffer_escaped(bd2->bd_bh))) {
649                                 void *kaddr;
650                                 struct page *page = bd2->bd_bh->b_page;
651                                 bh = gfs2_log_get_buf(sdp);
652                                 kaddr = kmap_atomic(page, KM_USER0);
653                                 memcpy(bh->b_data,
654                                        kaddr + bh_offset(bd2->bd_bh),
655                                        sdp->sd_sb.sb_bsize);
656                                 kunmap_atomic(kaddr, KM_USER0);
657                                 *(__be32 *)bh->b_data = 0;
658                         } else {
659                                 bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
660                         }
661                         set_buffer_dirty(bh);
662                         ll_rw_block(WRITE, 1, &bh);
663                         gfs2_log_lock(sdp);
664                         if (++n >= num)
665                                 break;
666                 }
667                 bh = NULL;
668                 BUG_ON(total_dbuf < num);
669                 total_dbuf -= num;
670                 total_jdata -= num;
671         }
672         gfs2_log_unlock(sdp);
673
674         /* Wait on all ordered buffers */
675         while (!list_empty(&started)) {
676                 gfs2_log_lock(sdp);
677                 bd1 = list_entry(started.next, struct gfs2_bufdata,
678                                  bd_le.le_list);
679                 list_del_init(&bd1->bd_le.le_list);
680                 sdp->sd_log_num_databuf--;
681                 bh = bd1->bd_bh;
682                 if (bh) {
683                         bh->b_private = NULL;
684                         get_bh(bh);
685                         gfs2_log_unlock(sdp);
686                         wait_on_buffer(bh);
687                         brelse(bh);
688                 } else
689                         gfs2_log_unlock(sdp);
690
691                 kmem_cache_free(gfs2_bufdata_cachep, bd1);
692         }
693
694         /* We've removed all the ordered write bufs here, so only jdata left */
695         gfs2_assert_warn(sdp, sdp->sd_log_num_databuf == sdp->sd_log_num_jdata);
696 }
697
698 static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
699                                     struct gfs2_log_descriptor *ld,
700                                     __be64 *ptr, int pass)
701 {
702         struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
703         struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
704         struct gfs2_glock *gl = ip->i_gl;
705         unsigned int blks = be32_to_cpu(ld->ld_data1);
706         struct buffer_head *bh_log, *bh_ip;
707         u64 blkno;
708         u64 esc;
709         int error = 0;
710
711         if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
712                 return 0;
713
714         gfs2_replay_incr_blk(sdp, &start);
715         for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
716                 blkno = be64_to_cpu(*ptr++);
717                 esc = be64_to_cpu(*ptr++);
718
719                 sdp->sd_found_blocks++;
720
721                 if (gfs2_revoke_check(sdp, blkno, start))
722                         continue;
723
724                 error = gfs2_replay_read_block(jd, start, &bh_log);
725                 if (error)
726                         return error;
727
728                 bh_ip = gfs2_meta_new(gl, blkno);
729                 memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
730
731                 /* Unescape */
732                 if (esc) {
733                         __be32 *eptr = (__be32 *)bh_ip->b_data;
734                         *eptr = cpu_to_be32(GFS2_MAGIC);
735                 }
736                 mark_buffer_dirty(bh_ip);
737
738                 brelse(bh_log);
739                 brelse(bh_ip);
740                 if (error)
741                         break;
742
743                 sdp->sd_replayed_blocks++;
744         }
745
746         return error;
747 }
748
749 /* FIXME: sort out accounting for log blocks etc. */
750
751 static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
752 {
753         struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
754         struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
755
756         if (error) {
757                 gfs2_meta_sync(ip->i_gl);
758                 return;
759         }
760         if (pass != 1)
761                 return;
762
763         /* data sync? */
764         gfs2_meta_sync(ip->i_gl);
765
766         fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n",
767                 jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
768 }
769
770 static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
771 {
772         struct list_head *head = &sdp->sd_log_le_databuf;
773         struct gfs2_bufdata *bd;
774
775         while (!list_empty(head)) {
776                 bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
777                 list_del_init(&bd->bd_le.le_list);
778                 sdp->sd_log_num_databuf--;
779                 sdp->sd_log_num_jdata--;
780                 gfs2_unpin(sdp, bd->bd_bh, ai);
781         }
782         gfs2_assert_warn(sdp, !sdp->sd_log_num_databuf);
783         gfs2_assert_warn(sdp, !sdp->sd_log_num_jdata);
784 }
785
786
787 const struct gfs2_log_operations gfs2_glock_lops = {
788         .lo_add = glock_lo_add,
789         .lo_after_commit = glock_lo_after_commit,
790         .lo_name = "glock",
791 };
792
793 const struct gfs2_log_operations gfs2_buf_lops = {
794         .lo_add = buf_lo_add,
795         .lo_incore_commit = buf_lo_incore_commit,
796         .lo_before_commit = buf_lo_before_commit,
797         .lo_after_commit = buf_lo_after_commit,
798         .lo_before_scan = buf_lo_before_scan,
799         .lo_scan_elements = buf_lo_scan_elements,
800         .lo_after_scan = buf_lo_after_scan,
801         .lo_name = "buf",
802 };
803
804 const struct gfs2_log_operations gfs2_revoke_lops = {
805         .lo_add = revoke_lo_add,
806         .lo_before_commit = revoke_lo_before_commit,
807         .lo_before_scan = revoke_lo_before_scan,
808         .lo_scan_elements = revoke_lo_scan_elements,
809         .lo_after_scan = revoke_lo_after_scan,
810         .lo_name = "revoke",
811 };
812
813 const struct gfs2_log_operations gfs2_rg_lops = {
814         .lo_add = rg_lo_add,
815         .lo_after_commit = rg_lo_after_commit,
816         .lo_name = "rg",
817 };
818
819 const struct gfs2_log_operations gfs2_databuf_lops = {
820         .lo_add = databuf_lo_add,
821         .lo_incore_commit = buf_lo_incore_commit,
822         .lo_before_commit = databuf_lo_before_commit,
823         .lo_after_commit = databuf_lo_after_commit,
824         .lo_scan_elements = databuf_lo_scan_elements,
825         .lo_after_scan = databuf_lo_after_scan,
826         .lo_name = "databuf",
827 };
828
829 const struct gfs2_log_operations *gfs2_log_ops[] = {
830         &gfs2_glock_lops,
831         &gfs2_buf_lops,
832         &gfs2_revoke_lops,
833         &gfs2_rg_lops,
834         &gfs2_databuf_lops,
835         NULL,
836 };
837