]> err.no Git - linux-2.6/blob - fs/gfs2/log.c
[GFS2] More style changes
[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 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
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         u64 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         gfs2_log_unlock(sdp);
181         mutex_unlock(&sdp->sd_log_reserve_mutex);
182
183         down_read(&sdp->sd_log_flush_lock);
184
185         return 0;
186 }
187
188 /**
189  * gfs2_log_release - Release a given number of log blocks
190  * @sdp: The GFS2 superblock
191  * @blks: The number of blocks
192  *
193  */
194
195 void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
196 {
197
198         gfs2_log_lock(sdp);
199         sdp->sd_log_blks_free += blks;
200         gfs2_assert_withdraw(sdp,
201                              sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
202         gfs2_log_unlock(sdp);
203         up_read(&sdp->sd_log_flush_lock);
204 }
205
206 static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
207 {
208         int new = 0;
209         u64 dbn;
210         int error;
211         int bdy;
212
213         error = gfs2_block_map(sdp->sd_jdesc->jd_inode, lbn, &new, &dbn, &bdy);
214         if (error || !dbn)
215                 printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error, (unsigned long long)dbn, lbn);
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, struct gfs2_ail,
257                                 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         u64 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         u64 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, u32 flags, int pull)
362 {
363         u64 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         u32 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 = cpu_to_be64(sdp->sd_log_sequence++);
387         lh->lh_flags = cpu_to_be32(flags);
388         lh->lh_tail = cpu_to_be32(tail);
389         lh->lh_blkno = cpu_to_be32(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
415         while (!list_empty(head)) {
416                 lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
417                 list_del(&lb->lb_list);
418                 bh = lb->lb_bh;
419
420                 wait_on_buffer(bh);
421                 if (!buffer_uptodate(bh))
422                         gfs2_io_error_bh(sdp, bh);
423                 if (lb->lb_real) {
424                         while (atomic_read(&bh->b_count) != 1)  /* Grrrr... */
425                                 schedule();
426                         free_buffer_head(bh);
427                 } else
428                         brelse(bh);
429                 kfree(lb);
430         }
431
432         log_write_header(sdp, 0, 0);
433 }
434
435 /**
436  * gfs2_log_flush - flush incore transaction(s)
437  * @sdp: the filesystem
438  * @gl: The glock structure to flush.  If NULL, flush the whole incore log
439  *
440  */
441
442 void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
443 {
444         struct gfs2_ail *ai;
445
446         down_write(&sdp->sd_log_flush_lock);
447
448         if (gl) {
449                 gfs2_log_lock(sdp);
450                 if (list_empty(&gl->gl_le.le_list)) {
451                         gfs2_log_unlock(sdp);
452                         up_write(&sdp->sd_log_flush_lock);
453                         return;
454                 }
455                 gfs2_log_unlock(sdp);
456         }
457
458         ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
459         INIT_LIST_HEAD(&ai->ai_ail1_list);
460         INIT_LIST_HEAD(&ai->ai_ail2_list);
461
462         gfs2_assert_withdraw(sdp,
463                         sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
464         gfs2_assert_withdraw(sdp,
465                         sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
466
467         sdp->sd_log_flush_head = sdp->sd_log_head;
468         sdp->sd_log_flush_wrapped = 0;
469         ai->ai_first = sdp->sd_log_flush_head;
470
471         lops_before_commit(sdp);
472         if (!list_empty(&sdp->sd_log_flush_list))
473                 log_flush_commit(sdp);
474         else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
475                 log_write_header(sdp, 0, PULL);
476         lops_after_commit(sdp, ai);
477         sdp->sd_log_head = sdp->sd_log_flush_head;
478
479         /* printk(KERN_INFO "sd_log_num_hdrs %u\n", sdp->sd_log_num_hdrs); */
480         sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs;
481
482         sdp->sd_log_blks_reserved =
483                 sdp->sd_log_commited_buf =
484                 sdp->sd_log_num_hdrs = 
485                 sdp->sd_log_commited_revoke = 0;
486
487         gfs2_log_lock(sdp);
488         if (!list_empty(&ai->ai_ail1_list)) {
489                 list_add(&ai->ai_list, &sdp->sd_ail1_list);
490                 ai = NULL;
491         }
492         gfs2_log_unlock(sdp);
493
494         sdp->sd_vfs->s_dirt = 0;
495         up_write(&sdp->sd_log_flush_lock);
496
497         kfree(ai);
498 }
499
500 static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
501 {
502         unsigned int reserved = 0;
503         unsigned int old;
504
505         gfs2_log_lock(sdp);
506
507         sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
508         gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
509         sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
510         gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
511
512         if (sdp->sd_log_commited_buf)
513                 reserved += sdp->sd_log_commited_buf;
514         if (sdp->sd_log_commited_revoke)
515                 reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
516                                             sizeof(u64));
517         if (reserved)
518                 reserved++;
519
520         old = sdp->sd_log_blks_free;
521         sdp->sd_log_blks_free += tr->tr_reserved -
522                                  (reserved - sdp->sd_log_blks_reserved);
523
524         gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
525         gfs2_assert_withdraw(sdp,
526                              sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks +
527                              sdp->sd_log_num_hdrs);
528
529         sdp->sd_log_blks_reserved = reserved;
530
531         gfs2_log_unlock(sdp);
532 }
533
534 /**
535  * gfs2_log_commit - Commit a transaction to the log
536  * @sdp: the filesystem
537  * @tr: the transaction
538  *
539  * Returns: errno
540  */
541
542 void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
543 {
544         log_refund(sdp, tr);
545         lops_incore_commit(sdp, tr);
546
547         sdp->sd_vfs->s_dirt = 1;
548         up_read(&sdp->sd_log_flush_lock);
549
550         gfs2_log_lock(sdp);
551         if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) {
552                 gfs2_log_unlock(sdp);
553                 gfs2_log_flush(sdp, NULL);
554         } else
555                 gfs2_log_unlock(sdp);
556 }
557
558 /**
559  * gfs2_log_shutdown - write a shutdown header into a journal
560  * @sdp: the filesystem
561  *
562  */
563
564 void gfs2_log_shutdown(struct gfs2_sbd *sdp)
565 {
566         down_write(&sdp->sd_log_flush_lock);
567
568         gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
569         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
570         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
571         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
572         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
573         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
574         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
575         gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs);
576         gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
577
578         sdp->sd_log_flush_head = sdp->sd_log_head;
579         sdp->sd_log_flush_wrapped = 0;
580
581         log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
582
583         /* printk(KERN_INFO "sd_log_blks_free %u, sd_jdesc->jd_blocks %u\n", sdp->sd_log_blks_free, sdp->sd_jdesc->jd_blocks); */
584         gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
585         gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
586         gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
587
588         sdp->sd_log_head = sdp->sd_log_flush_head;
589         sdp->sd_log_tail = sdp->sd_log_head;
590
591         up_write(&sdp->sd_log_flush_lock);
592 }
593