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