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