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