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