]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/gfs2/log.c
Merge branch 'master' into gfs2
[linux-2.6-omap-h63xx.git] / fs / gfs2 / log.c
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
4  *
5  * This copyrighted material is made available to anyone wishing to use,
6  * modify, copy, or redistribute it subject to the terms and conditions
7  * of the GNU General Public License version 2.
8  */
9
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/gfs2_ondisk.h>
16 #include <linux/crc32.h>
17 #include <linux/lm_interface.h>
18
19 #include "gfs2.h"
20 #include "incore.h"
21 #include "bmap.h"
22 #include "glock.h"
23 #include "log.h"
24 #include "lops.h"
25 #include "meta_io.h"
26 #include "util.h"
27 #include "dir.h"
28
29 #define PULL 1
30
31 /**
32  * gfs2_struct2blk - compute stuff
33  * @sdp: the filesystem
34  * @nstruct: the number of structures
35  * @ssize: the size of the structures
36  *
37  * Compute the number of log descriptor blocks needed to hold a certain number
38  * of structures of a certain size.
39  *
40  * Returns: the number of blocks needed (minimum is always 1)
41  */
42
43 unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
44                              unsigned int ssize)
45 {
46         unsigned int blks;
47         unsigned int first, second;
48
49         blks = 1;
50         first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
51
52         if (nstruct > first) {
53                 second = (sdp->sd_sb.sb_bsize -
54                           sizeof(struct gfs2_meta_header)) / ssize;
55                 blks += DIV_ROUND_UP(nstruct - first, second);
56         }
57
58         return blks;
59 }
60
61 void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
62 {
63         struct list_head *head = &sdp->sd_ail1_list;
64         u64 sync_gen;
65         struct list_head *first;
66         struct gfs2_ail *first_ai, *ai, *tmp;
67         int done = 0;
68
69         gfs2_log_lock(sdp);
70         if (list_empty(head)) {
71                 gfs2_log_unlock(sdp);
72                 return;
73         }
74         sync_gen = sdp->sd_ail_sync_gen++;
75
76         first = head->prev;
77         first_ai = list_entry(first, struct gfs2_ail, ai_list);
78         first_ai->ai_sync_gen = sync_gen;
79         gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
80
81         if (flags & DIO_ALL)
82                 first = NULL;
83
84         while(!done) {
85                 if (first && (head->prev != first ||
86                               gfs2_ail1_empty_one(sdp, first_ai, 0)))
87                         break;
88
89                 done = 1;
90                 list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
91                         if (ai->ai_sync_gen >= sync_gen)
92                                 continue;
93                         ai->ai_sync_gen = sync_gen;
94                         gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
95                         done = 0;
96                         break;
97                 }
98         }
99
100         gfs2_log_unlock(sdp);
101 }
102
103 int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
104 {
105         struct gfs2_ail *ai, *s;
106         int ret;
107
108         gfs2_log_lock(sdp);
109
110         list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
111                 if (gfs2_ail1_empty_one(sdp, ai, flags))
112                         list_move(&ai->ai_list, &sdp->sd_ail2_list);
113                 else if (!(flags & DIO_ALL))
114                         break;
115         }
116
117         ret = list_empty(&sdp->sd_ail1_list);
118
119         gfs2_log_unlock(sdp);
120
121         return ret;
122 }
123
124 static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
125 {
126         struct gfs2_ail *ai, *safe;
127         unsigned int old_tail = sdp->sd_log_tail;
128         int wrap = (new_tail < old_tail);
129         int a, b, rm;
130
131         gfs2_log_lock(sdp);
132
133         list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
134                 a = (old_tail <= ai->ai_first);
135                 b = (ai->ai_first < new_tail);
136                 rm = (wrap) ? (a || b) : (a && b);
137                 if (!rm)
138                         continue;
139
140                 gfs2_ail2_empty_one(sdp, ai);
141                 list_del(&ai->ai_list);
142                 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
143                 gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
144                 kfree(ai);
145         }
146
147         gfs2_log_unlock(sdp);
148 }
149
150 /**
151  * gfs2_log_reserve - Make a log reservation
152  * @sdp: The GFS2 superblock
153  * @blks: The number of blocks to reserve
154  *
155  * Returns: errno
156  */
157
158 int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
159 {
160         unsigned int try = 0;
161
162         if (gfs2_assert_warn(sdp, blks) ||
163             gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
164                 return -EINVAL;
165
166         mutex_lock(&sdp->sd_log_reserve_mutex);
167         gfs2_log_lock(sdp);
168         while(sdp->sd_log_blks_free <= blks) {
169                 gfs2_log_unlock(sdp);
170                 gfs2_ail1_empty(sdp, 0);
171                 gfs2_log_flush(sdp, NULL);
172
173                 if (try++)
174                         gfs2_ail1_start(sdp, 0);
175                 gfs2_log_lock(sdp);
176         }
177         sdp->sd_log_blks_free -= blks;
178         gfs2_log_unlock(sdp);
179         mutex_unlock(&sdp->sd_log_reserve_mutex);
180
181         down_read(&sdp->sd_log_flush_lock);
182
183         return 0;
184 }
185
186 /**
187  * gfs2_log_release - Release a given number of log blocks
188  * @sdp: The GFS2 superblock
189  * @blks: The number of blocks
190  *
191  */
192
193 void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
194 {
195
196         gfs2_log_lock(sdp);
197         sdp->sd_log_blks_free += blks;
198         gfs2_assert_withdraw(sdp,
199                              sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
200         gfs2_log_unlock(sdp);
201         up_read(&sdp->sd_log_flush_lock);
202 }
203
204 static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
205 {
206         int error;
207         struct buffer_head bh_map;
208
209         error = gfs2_block_map(sdp->sd_jdesc->jd_inode, lbn, 0, &bh_map, 1);
210         if (error || !bh_map.b_blocknr)
211                 printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error, bh_map.b_blocknr, lbn);
212         gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr);
213
214         return bh_map.b_blocknr;
215 }
216
217 /**
218  * log_distance - Compute distance between two journal blocks
219  * @sdp: The GFS2 superblock
220  * @newer: The most recent journal block of the pair
221  * @older: The older journal block of the pair
222  *
223  *   Compute the distance (in the journal direction) between two
224  *   blocks in the journal
225  *
226  * Returns: the distance in blocks
227  */
228
229 static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
230                                         unsigned int older)
231 {
232         int dist;
233
234         dist = newer - older;
235         if (dist < 0)
236                 dist += sdp->sd_jdesc->jd_blocks;
237
238         return dist;
239 }
240
241 static unsigned int current_tail(struct gfs2_sbd *sdp)
242 {
243         struct gfs2_ail *ai;
244         unsigned int tail;
245
246         gfs2_log_lock(sdp);
247
248         if (list_empty(&sdp->sd_ail1_list)) {
249                 tail = sdp->sd_log_head;
250         } else {
251                 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
252                 tail = ai->ai_first;
253         }
254
255         gfs2_log_unlock(sdp);
256
257         return tail;
258 }
259
260 static inline void log_incr_head(struct gfs2_sbd *sdp)
261 {
262         if (sdp->sd_log_flush_head == sdp->sd_log_tail)
263                 gfs2_assert_withdraw(sdp, sdp->sd_log_flush_head == sdp->sd_log_head);
264
265         if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
266                 sdp->sd_log_flush_head = 0;
267                 sdp->sd_log_flush_wrapped = 1;
268         }
269 }
270
271 /**
272  * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
273  * @sdp: The GFS2 superblock
274  *
275  * Returns: the buffer_head
276  */
277
278 struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
279 {
280         u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
281         struct gfs2_log_buf *lb;
282         struct buffer_head *bh;
283
284         lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
285         list_add(&lb->lb_list, &sdp->sd_log_flush_list);
286
287         bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
288         lock_buffer(bh);
289         memset(bh->b_data, 0, bh->b_size);
290         set_buffer_uptodate(bh);
291         clear_buffer_dirty(bh);
292         unlock_buffer(bh);
293
294         log_incr_head(sdp);
295
296         return bh;
297 }
298
299 /**
300  * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
301  * @sdp: the filesystem
302  * @data: the data the buffer_head should point to
303  *
304  * Returns: the log buffer descriptor
305  */
306
307 struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
308                                       struct buffer_head *real)
309 {
310         u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
311         struct gfs2_log_buf *lb;
312         struct buffer_head *bh;
313
314         lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
315         list_add(&lb->lb_list, &sdp->sd_log_flush_list);
316         lb->lb_real = real;
317
318         bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
319         atomic_set(&bh->b_count, 1);
320         bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
321         set_bh_page(bh, real->b_page, bh_offset(real));
322         bh->b_blocknr = blkno;
323         bh->b_size = sdp->sd_sb.sb_bsize;
324         bh->b_bdev = sdp->sd_vfs->s_bdev;
325
326         log_incr_head(sdp);
327
328         return bh;
329 }
330
331 static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
332 {
333         unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
334
335         ail2_empty(sdp, new_tail);
336
337         gfs2_log_lock(sdp);
338         sdp->sd_log_blks_free += dist - (pull ? 1 : 0);
339         gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
340         gfs2_log_unlock(sdp);
341
342         sdp->sd_log_tail = new_tail;
343 }
344
345 /**
346  * log_write_header - Get and initialize a journal header buffer
347  * @sdp: The GFS2 superblock
348  *
349  * Returns: the initialized log buffer descriptor
350  */
351
352 static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
353 {
354         u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
355         struct buffer_head *bh;
356         struct gfs2_log_header *lh;
357         unsigned int tail;
358         u32 hash;
359
360         bh = sb_getblk(sdp->sd_vfs, blkno);
361         lock_buffer(bh);
362         memset(bh->b_data, 0, bh->b_size);
363         set_buffer_uptodate(bh);
364         clear_buffer_dirty(bh);
365         unlock_buffer(bh);
366
367         gfs2_ail1_empty(sdp, 0);
368         tail = current_tail(sdp);
369
370         lh = (struct gfs2_log_header *)bh->b_data;
371         memset(lh, 0, sizeof(struct gfs2_log_header));
372         lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
373         lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
374         lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
375         lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
376         lh->lh_flags = cpu_to_be32(flags);
377         lh->lh_tail = cpu_to_be32(tail);
378         lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
379         hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
380         lh->lh_hash = cpu_to_be32(hash);
381
382         set_buffer_dirty(bh);
383         if (sync_dirty_buffer(bh))
384                 gfs2_io_error_bh(sdp, bh);
385         brelse(bh);
386
387         if (sdp->sd_log_tail != tail)
388                 log_pull_tail(sdp, tail, pull);
389         else
390                 gfs2_assert_withdraw(sdp, !pull);
391
392         sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
393         log_incr_head(sdp);
394 }
395
396 static void log_flush_commit(struct gfs2_sbd *sdp)
397 {
398         struct list_head *head = &sdp->sd_log_flush_list;
399         struct gfs2_log_buf *lb;
400         struct buffer_head *bh;
401
402         while (!list_empty(head)) {
403                 lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
404                 list_del(&lb->lb_list);
405                 bh = lb->lb_bh;
406
407                 wait_on_buffer(bh);
408                 if (!buffer_uptodate(bh))
409                         gfs2_io_error_bh(sdp, bh);
410                 if (lb->lb_real) {
411                         while (atomic_read(&bh->b_count) != 1)  /* Grrrr... */
412                                 schedule();
413                         free_buffer_head(bh);
414                 } else
415                         brelse(bh);
416                 kfree(lb);
417         }
418
419         log_write_header(sdp, 0, 0);
420 }
421
422 /**
423  * gfs2_log_flush - flush incore transaction(s)
424  * @sdp: the filesystem
425  * @gl: The glock structure to flush.  If NULL, flush the whole incore log
426  *
427  */
428
429 void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
430 {
431         struct gfs2_ail *ai;
432
433         down_write(&sdp->sd_log_flush_lock);
434
435         if (gl) {
436                 gfs2_log_lock(sdp);
437                 if (list_empty(&gl->gl_le.le_list)) {
438                         gfs2_log_unlock(sdp);
439                         up_write(&sdp->sd_log_flush_lock);
440                         return;
441                 }
442                 gfs2_log_unlock(sdp);
443         }
444
445         ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
446         INIT_LIST_HEAD(&ai->ai_ail1_list);
447         INIT_LIST_HEAD(&ai->ai_ail2_list);
448
449         gfs2_assert_withdraw(sdp, sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
450         gfs2_assert_withdraw(sdp,
451                         sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
452
453         sdp->sd_log_flush_head = sdp->sd_log_head;
454         sdp->sd_log_flush_wrapped = 0;
455         ai->ai_first = sdp->sd_log_flush_head;
456
457         lops_before_commit(sdp);
458         if (!list_empty(&sdp->sd_log_flush_list))
459                 log_flush_commit(sdp);
460         else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
461                 log_write_header(sdp, 0, PULL);
462         lops_after_commit(sdp, ai);
463         sdp->sd_log_head = sdp->sd_log_flush_head;
464
465         sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs;
466
467         sdp->sd_log_blks_reserved = 0;
468         sdp->sd_log_commited_buf = 0;
469         sdp->sd_log_num_hdrs = 0;
470         sdp->sd_log_commited_revoke = 0;
471
472         gfs2_log_lock(sdp);
473         if (!list_empty(&ai->ai_ail1_list)) {
474                 list_add(&ai->ai_list, &sdp->sd_ail1_list);
475                 ai = NULL;
476         }
477         gfs2_log_unlock(sdp);
478
479         sdp->sd_vfs->s_dirt = 0;
480         up_write(&sdp->sd_log_flush_lock);
481
482         kfree(ai);
483 }
484
485 static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
486 {
487         unsigned int reserved = 0;
488         unsigned int old;
489
490         gfs2_log_lock(sdp);
491
492         sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
493         gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
494         sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
495         gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
496
497         if (sdp->sd_log_commited_buf)
498                 reserved += sdp->sd_log_commited_buf;
499         if (sdp->sd_log_commited_revoke)
500                 reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
501                                             sizeof(u64));
502         if (reserved)
503                 reserved++;
504
505         old = sdp->sd_log_blks_free;
506         sdp->sd_log_blks_free += tr->tr_reserved -
507                                  (reserved - sdp->sd_log_blks_reserved);
508
509         gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
510         gfs2_assert_withdraw(sdp,
511                              sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks +
512                              sdp->sd_log_num_hdrs);
513
514         sdp->sd_log_blks_reserved = reserved;
515
516         gfs2_log_unlock(sdp);
517 }
518
519 /**
520  * gfs2_log_commit - Commit a transaction to the log
521  * @sdp: the filesystem
522  * @tr: the transaction
523  *
524  * Returns: errno
525  */
526
527 void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
528 {
529         log_refund(sdp, tr);
530         lops_incore_commit(sdp, tr);
531
532         sdp->sd_vfs->s_dirt = 1;
533         up_read(&sdp->sd_log_flush_lock);
534
535         gfs2_log_lock(sdp);
536         if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) {
537                 gfs2_log_unlock(sdp);
538                 gfs2_log_flush(sdp, NULL);
539         } else {
540                 gfs2_log_unlock(sdp);
541         }
542 }
543
544 /**
545  * gfs2_log_shutdown - write a shutdown header into a journal
546  * @sdp: the filesystem
547  *
548  */
549
550 void gfs2_log_shutdown(struct gfs2_sbd *sdp)
551 {
552         down_write(&sdp->sd_log_flush_lock);
553
554         gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
555         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
556         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
557         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
558         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
559         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
560         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
561         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs);
562         gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
563
564         sdp->sd_log_flush_head = sdp->sd_log_head;
565         sdp->sd_log_flush_wrapped = 0;
566
567         log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
568
569         gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
570         gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
571         gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
572
573         sdp->sd_log_head = sdp->sd_log_flush_head;
574         sdp->sd_log_tail = sdp->sd_log_head;
575
576         up_write(&sdp->sd_log_flush_lock);
577 }
578