[GFS2] Patch to protect sd_log_num_jdata
[linux-2.6] / 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                 gfs2_pin(sdp, bd->bd_bh);
496                 tr->tr_num_databuf_new++;
497         }
498         gfs2_log_lock(sdp);
499         if (gfs2_is_jdata(ip))
500                 sdp->sd_log_num_jdata++;
501         sdp->sd_log_num_databuf++;
502         list_add(&le->le_list, &sdp->sd_log_le_databuf);
503         gfs2_log_unlock(sdp);
504 }
505
506 static int gfs2_check_magic(struct buffer_head *bh)
507 {
508         struct page *page = bh->b_page;
509         void *kaddr;
510         __be32 *ptr;
511         int rv = 0;
512
513         kaddr = kmap_atomic(page, KM_USER0);
514         ptr = kaddr + bh_offset(bh);
515         if (*ptr == cpu_to_be32(GFS2_MAGIC))
516                 rv = 1;
517         kunmap_atomic(kaddr, KM_USER0);
518
519         return rv;
520 }
521
522 /**
523  * databuf_lo_before_commit - Scan the data buffers, writing as we go
524  *
525  * Here we scan through the lists of buffers and make the assumption
526  * that any buffer thats been pinned is being journaled, and that
527  * any unpinned buffer is an ordered write data buffer and therefore
528  * will be written back rather than journaled.
529  */
530 static void databuf_lo_before_commit(struct gfs2_sbd *sdp)
531 {
532         LIST_HEAD(started);
533         struct gfs2_bufdata *bd1 = NULL, *bd2, *bdt;
534         struct buffer_head *bh = NULL,*bh1 = NULL;
535         struct gfs2_log_descriptor *ld;
536         unsigned int limit;
537         unsigned int total_dbuf;
538         unsigned int total_jdata;
539         unsigned int num, n;
540         __be64 *ptr = NULL;
541
542         limit = databuf_limit(sdp);
543
544         /*
545          * Start writing ordered buffers, write journaled buffers
546          * into the log along with a header
547          */
548         gfs2_log_lock(sdp);
549         total_dbuf = sdp->sd_log_num_databuf;
550         total_jdata = sdp->sd_log_num_jdata;
551         bd2 = bd1 = list_prepare_entry(bd1, &sdp->sd_log_le_databuf,
552                                        bd_le.le_list);
553         while(total_dbuf) {
554                 num = total_jdata;
555                 if (num > limit)
556                         num = limit;
557                 n = 0;
558                 list_for_each_entry_safe_continue(bd1, bdt,
559                                                   &sdp->sd_log_le_databuf,
560                                                   bd_le.le_list) {
561                         /* store off the buffer head in a local ptr since
562                          * gfs2_bufdata might change when we drop the log lock
563                          */
564                         bh1 = bd1->bd_bh;
565
566                         /* An ordered write buffer */
567                         if (bh1 && !buffer_pinned(bh1)) {
568                                 list_move(&bd1->bd_le.le_list, &started);
569                                 if (bd1 == bd2) {
570                                         bd2 = NULL;
571                                         bd2 = list_prepare_entry(bd2,
572                                                         &sdp->sd_log_le_databuf,
573                                                         bd_le.le_list);
574                                 }
575                                 total_dbuf--;
576                                 if (bh1) {
577                                         if (buffer_dirty(bh1)) {
578                                                 get_bh(bh1);
579
580                                                 gfs2_log_unlock(sdp);
581
582                                                 ll_rw_block(SWRITE, 1, &bh1);
583                                                 brelse(bh1);
584
585                                                 gfs2_log_lock(sdp);
586                                         }
587                                         continue;
588                                 }
589                                 continue;
590                         } else if (bh1) { /* A journaled buffer */
591                                 int magic;
592                                 gfs2_log_unlock(sdp);
593                                 if (!bh) {
594                                         bh = gfs2_log_get_buf(sdp);
595                                         ld = (struct gfs2_log_descriptor *)
596                                              bh->b_data;
597                                         ptr = (__be64 *)(bh->b_data +
598                                                          DATABUF_OFFSET);
599                                         ld->ld_header.mh_magic =
600                                                 cpu_to_be32(GFS2_MAGIC);
601                                         ld->ld_header.mh_type =
602                                                 cpu_to_be32(GFS2_METATYPE_LD);
603                                         ld->ld_header.mh_format =
604                                                 cpu_to_be32(GFS2_FORMAT_LD);
605                                         ld->ld_type =
606                                                 cpu_to_be32(GFS2_LOG_DESC_JDATA);
607                                         ld->ld_length = cpu_to_be32(num + 1);
608                                         ld->ld_data1 = cpu_to_be32(num);
609                                         ld->ld_data2 = cpu_to_be32(0);
610                                         memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved));
611                                 }
612                                 magic = gfs2_check_magic(bh1);
613                                 *ptr++ = cpu_to_be64(bh1->b_blocknr);
614                                 *ptr++ = cpu_to_be64((__u64)magic);
615                                 clear_buffer_escaped(bh1);
616                                 if (unlikely(magic != 0))
617                                         set_buffer_escaped(bh1);
618                                 gfs2_log_lock(sdp);
619                                 if (++n >= num)
620                                         break;
621                         } else if (!bh1) {
622                                 total_dbuf--;
623                                 sdp->sd_log_num_databuf--;
624                                 list_del_init(&bd1->bd_le.le_list);
625                                 if (bd1 == bd2) {
626                                         bd2 = NULL;
627                                         bd2 = list_prepare_entry(bd2,
628                                                 &sdp->sd_log_le_databuf,
629                                                 bd_le.le_list);
630                                 }
631                                 kmem_cache_free(gfs2_bufdata_cachep, bd1);
632                         }
633                 }
634                 gfs2_log_unlock(sdp);
635                 if (bh) {
636                         set_buffer_dirty(bh);
637                         ll_rw_block(WRITE, 1, &bh);
638                         bh = NULL;
639                         ptr = NULL;
640                 }
641                 n = 0;
642                 gfs2_log_lock(sdp);
643                 list_for_each_entry_continue(bd2, &sdp->sd_log_le_databuf,
644                                              bd_le.le_list) {
645                         if (!bd2->bd_bh)
646                                 continue;
647                         /* copy buffer if it needs escaping */
648                         gfs2_log_unlock(sdp);
649                         if (unlikely(buffer_escaped(bd2->bd_bh))) {
650                                 void *kaddr;
651                                 struct page *page = bd2->bd_bh->b_page;
652                                 bh = gfs2_log_get_buf(sdp);
653                                 kaddr = kmap_atomic(page, KM_USER0);
654                                 memcpy(bh->b_data,
655                                        kaddr + bh_offset(bd2->bd_bh),
656                                        sdp->sd_sb.sb_bsize);
657                                 kunmap_atomic(kaddr, KM_USER0);
658                                 *(__be32 *)bh->b_data = 0;
659                         } else {
660                                 bh = gfs2_log_fake_buf(sdp, bd2->bd_bh);
661                         }
662                         set_buffer_dirty(bh);
663                         ll_rw_block(WRITE, 1, &bh);
664                         gfs2_log_lock(sdp);
665                         if (++n >= num)
666                                 break;
667                 }
668                 bh = NULL;
669                 BUG_ON(total_dbuf < num);
670                 total_dbuf -= num;
671                 total_jdata -= num;
672         }
673         gfs2_log_unlock(sdp);
674
675         /* Wait on all ordered buffers */
676         while (!list_empty(&started)) {
677                 gfs2_log_lock(sdp);
678                 bd1 = list_entry(started.next, struct gfs2_bufdata,
679                                  bd_le.le_list);
680                 list_del_init(&bd1->bd_le.le_list);
681                 sdp->sd_log_num_databuf--;
682                 bh = bd1->bd_bh;
683                 if (bh) {
684                         bh->b_private = NULL;
685                         get_bh(bh);
686                         gfs2_log_unlock(sdp);
687                         wait_on_buffer(bh);
688                         brelse(bh);
689                 } else
690                         gfs2_log_unlock(sdp);
691
692                 kmem_cache_free(gfs2_bufdata_cachep, bd1);
693         }
694
695         /* We've removed all the ordered write bufs here, so only jdata left */
696         gfs2_assert_warn(sdp, sdp->sd_log_num_databuf == sdp->sd_log_num_jdata);
697 }
698
699 static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start,
700                                     struct gfs2_log_descriptor *ld,
701                                     __be64 *ptr, int pass)
702 {
703         struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
704         struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
705         struct gfs2_glock *gl = ip->i_gl;
706         unsigned int blks = be32_to_cpu(ld->ld_data1);
707         struct buffer_head *bh_log, *bh_ip;
708         u64 blkno;
709         u64 esc;
710         int error = 0;
711
712         if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
713                 return 0;
714
715         gfs2_replay_incr_blk(sdp, &start);
716         for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) {
717                 blkno = be64_to_cpu(*ptr++);
718                 esc = be64_to_cpu(*ptr++);
719
720                 sdp->sd_found_blocks++;
721
722                 if (gfs2_revoke_check(sdp, blkno, start))
723                         continue;
724
725                 error = gfs2_replay_read_block(jd, start, &bh_log);
726                 if (error)
727                         return error;
728
729                 bh_ip = gfs2_meta_new(gl, blkno);
730                 memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
731
732                 /* Unescape */
733                 if (esc) {
734                         __be32 *eptr = (__be32 *)bh_ip->b_data;
735                         *eptr = cpu_to_be32(GFS2_MAGIC);
736                 }
737                 mark_buffer_dirty(bh_ip);
738
739                 brelse(bh_log);
740                 brelse(bh_ip);
741                 if (error)
742                         break;
743
744                 sdp->sd_replayed_blocks++;
745         }
746
747         return error;
748 }
749
750 /* FIXME: sort out accounting for log blocks etc. */
751
752 static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
753 {
754         struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
755         struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
756
757         if (error) {
758                 gfs2_meta_sync(ip->i_gl);
759                 return;
760         }
761         if (pass != 1)
762                 return;
763
764         /* data sync? */
765         gfs2_meta_sync(ip->i_gl);
766
767         fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n",
768                 jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks);
769 }
770
771 static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
772 {
773         struct list_head *head = &sdp->sd_log_le_databuf;
774         struct gfs2_bufdata *bd;
775
776         while (!list_empty(head)) {
777                 bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list);
778                 list_del_init(&bd->bd_le.le_list);
779                 sdp->sd_log_num_databuf--;
780                 sdp->sd_log_num_jdata--;
781                 gfs2_unpin(sdp, bd->bd_bh, ai);
782         }
783         gfs2_assert_warn(sdp, !sdp->sd_log_num_databuf);
784         gfs2_assert_warn(sdp, !sdp->sd_log_num_jdata);
785 }
786
787
788 const struct gfs2_log_operations gfs2_glock_lops = {
789         .lo_add = glock_lo_add,
790         .lo_after_commit = glock_lo_after_commit,
791         .lo_name = "glock",
792 };
793
794 const struct gfs2_log_operations gfs2_buf_lops = {
795         .lo_add = buf_lo_add,
796         .lo_incore_commit = buf_lo_incore_commit,
797         .lo_before_commit = buf_lo_before_commit,
798         .lo_after_commit = buf_lo_after_commit,
799         .lo_before_scan = buf_lo_before_scan,
800         .lo_scan_elements = buf_lo_scan_elements,
801         .lo_after_scan = buf_lo_after_scan,
802         .lo_name = "buf",
803 };
804
805 const struct gfs2_log_operations gfs2_revoke_lops = {
806         .lo_add = revoke_lo_add,
807         .lo_before_commit = revoke_lo_before_commit,
808         .lo_before_scan = revoke_lo_before_scan,
809         .lo_scan_elements = revoke_lo_scan_elements,
810         .lo_after_scan = revoke_lo_after_scan,
811         .lo_name = "revoke",
812 };
813
814 const struct gfs2_log_operations gfs2_rg_lops = {
815         .lo_add = rg_lo_add,
816         .lo_after_commit = rg_lo_after_commit,
817         .lo_name = "rg",
818 };
819
820 const struct gfs2_log_operations gfs2_databuf_lops = {
821         .lo_add = databuf_lo_add,
822         .lo_incore_commit = buf_lo_incore_commit,
823         .lo_before_commit = databuf_lo_before_commit,
824         .lo_after_commit = databuf_lo_after_commit,
825         .lo_scan_elements = databuf_lo_scan_elements,
826         .lo_after_scan = databuf_lo_after_scan,
827         .lo_name = "databuf",
828 };
829
830 const struct gfs2_log_operations *gfs2_log_ops[] = {
831         &gfs2_glock_lops,
832         &gfs2_buf_lops,
833         &gfs2_revoke_lops,
834         &gfs2_rg_lops,
835         &gfs2_databuf_lops,
836         NULL,
837 };
838