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