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