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