]> git.ipfire.org Git - people/arne_f/kernel.git/blame - fs/gfs2/log.c
[GFS2] Mark nlink cleared so VFS sees it happen
[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>
7d308590 17#include <linux/lm_interface.h>
b3b94faa
DT
18
19#include "gfs2.h"
5c676f6d 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;
74669416
SW
65 struct list_head *first;
66 struct gfs2_ail *first_ai, *ai, *tmp;
67 int done = 0;
b3b94faa
DT
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;
74669416 79 gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
b3b94faa
DT
80
81 if (flags & DIO_ALL)
82 first = NULL;
83
74669416 84 while(!done) {
484adff8
SW
85 if (first && (head->prev != first ||
86 gfs2_ail1_empty_one(sdp, first_ai, 0)))
b3b94faa
DT
87 break;
88
74669416
SW
89 done = 1;
90 list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
b3b94faa
DT
91 if (ai->ai_sync_gen >= sync_gen)
92 continue;
93 ai->ai_sync_gen = sync_gen;
74669416
SW
94 gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
95 done = 0;
b3b94faa
DT
96 break;
97 }
b3b94faa
DT
98 }
99
100 gfs2_log_unlock(sdp);
101}
102
103int 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
124static 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
158int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
159{
b3b94faa
DT
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
71b86f56 166 mutex_lock(&sdp->sd_log_reserve_mutex);
484adff8
SW
167 gfs2_log_lock(sdp);
168 while(sdp->sd_log_blks_free <= blks) {
b3b94faa 169 gfs2_log_unlock(sdp);
b3b94faa 170 gfs2_ail1_empty(sdp, 0);
b09e593d 171 gfs2_log_flush(sdp, NULL);
b3b94faa
DT
172
173 if (try++)
174 gfs2_ail1_start(sdp, 0);
484adff8 175 gfs2_log_lock(sdp);
b3b94faa 176 }
484adff8
SW
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);
b3b94faa
DT
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
193void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
194{
b3b94faa
DT
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);
ed386507 201 up_read(&sdp->sd_log_flush_lock);
b3b94faa
DT
202}
203
cd915493 204static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
b3b94faa 205{
b3b94faa 206 int error;
7a6bbacb 207 struct buffer_head bh_map;
b3b94faa 208
7a6bbacb
SW
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);
b3b94faa 213
7a6bbacb 214 return bh_map.b_blocknr;
b3b94faa
DT
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
faa31ce8 229static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
b3b94faa
DT
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
241static 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
faa31ce8 248 if (list_empty(&sdp->sd_ail1_list)) {
b3b94faa 249 tail = sdp->sd_log_head;
faa31ce8
SW
250 } else {
251 ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
b3b94faa
DT
252 tail = ai->ai_first;
253 }
254
255 gfs2_log_unlock(sdp);
256
257 return tail;
258}
259
260static inline void log_incr_head(struct gfs2_sbd *sdp)
261{
262 if (sdp->sd_log_flush_head == sdp->sd_log_tail)
faa31ce8 263 gfs2_assert_withdraw(sdp, sdp->sd_log_flush_head == sdp->sd_log_head);
b3b94faa
DT
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
278struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
279{
cd915493 280 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
b3b94faa
DT
281 struct gfs2_log_buf *lb;
282 struct buffer_head *bh;
283
4f3df041 284 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
b3b94faa
DT
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
307struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
308 struct buffer_head *real)
309{
cd915493 310 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
b3b94faa
DT
311 struct gfs2_log_buf *lb;
312 struct buffer_head *bh;
313
4f3df041 314 lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
b3b94faa
DT
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);
18ec7d5c 321 set_bh_page(bh, real->b_page, bh_offset(real));
b3b94faa
DT
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
331static 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);
c5392124 338 sdp->sd_log_blks_free += dist - (pull ? 1 : 0);
faa31ce8 339 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
b3b94faa
DT
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
cd915493 352static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
b3b94faa 353{
cd915493 354 u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
b3b94faa
DT
355 struct buffer_head *bh;
356 struct gfs2_log_header *lh;
357 unsigned int tail;
cd915493 358 u32 hash;
b3b94faa 359
b3b94faa
DT
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);
e3167ded
SW
373 lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
374 lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
e0f2bf78
SW
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);
b3b94faa
DT
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
396static 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;
b3b94faa
DT
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/**
b09e593d 423 * gfs2_log_flush - flush incore transaction(s)
b3b94faa
DT
424 * @sdp: the filesystem
425 * @gl: The glock structure to flush. If NULL, flush the whole incore log
426 *
427 */
428
b09e593d 429void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
b3b94faa
DT
430{
431 struct gfs2_ail *ai;
432
484adff8 433 down_write(&sdp->sd_log_flush_lock);
f55ab26a
SW
434
435 if (gl) {
436 gfs2_log_lock(sdp);
437 if (list_empty(&gl->gl_le.le_list)) {
438 gfs2_log_unlock(sdp);
484adff8 439 up_write(&sdp->sd_log_flush_lock);
f55ab26a
SW
440 return;
441 }
442 gfs2_log_unlock(sdp);
443 }
444
b09e593d
SW
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);
b3b94faa 448
faa31ce8 449 gfs2_assert_withdraw(sdp, sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
b3b94faa
DT
450 gfs2_assert_withdraw(sdp,
451 sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
452
b3b94faa
DT
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);
b3b94faa 463 sdp->sd_log_head = sdp->sd_log_flush_head;
b09e593d 464
f4154ea0 465 sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs;
b3b94faa 466
faa31ce8
SW
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;
b3b94faa
DT
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
b3b94faa 479 sdp->sd_vfs->s_dirt = 0;
484adff8 480 up_write(&sdp->sd_log_flush_lock);
b3b94faa
DT
481
482 kfree(ai);
483}
484
485static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
486{
5dc39fe6 487 unsigned int reserved = 0;
b3b94faa
DT
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)
f4154ea0 498 reserved += sdp->sd_log_commited_buf;
b3b94faa
DT
499 if (sdp->sd_log_commited_revoke)
500 reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
cd915493 501 sizeof(u64));
5dc39fe6
BM
502 if (reserved)
503 reserved++;
b3b94faa
DT
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
b09e593d 509 gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
b3b94faa 510 gfs2_assert_withdraw(sdp,
f4154ea0
SW
511 sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks +
512 sdp->sd_log_num_hdrs);
b3b94faa
DT
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
527void 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;
484adff8 533 up_read(&sdp->sd_log_flush_lock);
b3b94faa 534
b3b94faa
DT
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);
b09e593d 538 gfs2_log_flush(sdp, NULL);
faa31ce8 539 } else {
b3b94faa 540 gfs2_log_unlock(sdp);
faa31ce8 541 }
b3b94faa
DT
542}
543
544/**
545 * gfs2_log_shutdown - write a shutdown header into a journal
546 * @sdp: the filesystem
547 *
548 */
549
550void gfs2_log_shutdown(struct gfs2_sbd *sdp)
551{
484adff8 552 down_write(&sdp->sd_log_flush_lock);
b3b94faa 553
b3b94faa
DT
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);
18ec7d5c 557 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
b3b94faa
DT
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);
190562bd 561 gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs);
b3b94faa
DT
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
a74604be
SW
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));
b3b94faa
DT
572
573 sdp->sd_log_head = sdp->sd_log_flush_head;
b3b94faa
DT
574 sdp->sd_log_tail = sdp->sd_log_head;
575
484adff8 576 up_write(&sdp->sd_log_flush_lock);
b3b94faa
DT
577}
578