Print the path even if we do not understand the filesystem type.
[dragonfly.git] / sys / vfs / hammer / hammer_flusher.c
1 /*
2  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/vfs/hammer/hammer_flusher.c,v 1.9 2008/05/02 01:00:42 dillon Exp $
35  */
36 /*
37  * HAMMER dependancy flusher thread
38  *
39  * Meta data updates create buffer dependancies which are arranged as a
40  * hierarchy of lists.
41  */
42
43 #include "hammer.h"
44
45 static void hammer_flusher_thread(void *arg);
46 static void hammer_flusher_clean_loose_ios(hammer_mount_t hmp);
47 static void hammer_flusher_flush(hammer_mount_t hmp);
48 static int hammer_must_finalize_undo(hammer_mount_t hmp);
49 static void hammer_flusher_finalize(hammer_mount_t hmp,
50                     hammer_volume_t root_volume, hammer_off_t start_offset);
51
52 void
53 hammer_flusher_sync(hammer_mount_t hmp)
54 {
55         int seq;
56
57         if (hmp->flusher_td) {
58                 seq = hmp->flusher_next;
59                 if (hmp->flusher_signal == 0) {
60                         hmp->flusher_signal = 1;
61                         wakeup(&hmp->flusher_signal);
62                 }
63                 while ((int)(seq - hmp->flusher_done) > 0)
64                         tsleep(&hmp->flusher_done, 0, "hmrfls", 0);
65         }
66 }
67
68 void
69 hammer_flusher_async(hammer_mount_t hmp)
70 {
71         if (hmp->flusher_td) {
72                 if (hmp->flusher_signal == 0) {
73                         hmp->flusher_signal = 1;
74                         wakeup(&hmp->flusher_signal);
75                 }
76         }
77 }
78
79 void
80 hammer_flusher_create(hammer_mount_t hmp)
81 {
82         hmp->flusher_signal = 0;
83         hmp->flusher_act = 0;
84         hmp->flusher_done = 0;
85         hmp->flusher_next = 1;
86         lwkt_create(hammer_flusher_thread, hmp, &hmp->flusher_td, NULL,
87                     0, -1, "hammer");
88 }
89
90 void
91 hammer_flusher_destroy(hammer_mount_t hmp)
92 {
93         if (hmp->flusher_td) {
94                 hmp->flusher_exiting = 1;
95                 while (hmp->flusher_td) {
96                         hmp->flusher_signal = 1;
97                         wakeup(&hmp->flusher_signal);
98                         tsleep(&hmp->flusher_exiting, 0, "hmrwex", 0);
99                 }
100         }
101 }
102
103 static void
104 hammer_flusher_thread(void *arg)
105 {
106         hammer_mount_t hmp = arg;
107
108         for (;;) {
109                 hmp->flusher_act = hmp->flusher_next;
110                 ++hmp->flusher_next;
111                 kprintf("F");
112                 hammer_flusher_clean_loose_ios(hmp);
113                 hammer_flusher_flush(hmp);
114                 hammer_flusher_clean_loose_ios(hmp);
115                 hmp->flusher_done = hmp->flusher_act;
116
117                 wakeup(&hmp->flusher_done);
118
119                 /*
120                  * Wait for activity.
121                  */
122                 if (hmp->flusher_exiting && TAILQ_EMPTY(&hmp->flush_list))
123                         break;
124                 kprintf("E");
125
126                 while (hmp->flusher_signal == 0 &&
127                        TAILQ_EMPTY(&hmp->flush_list)) {
128                         tsleep(&hmp->flusher_signal, 0, "hmrwwa", 0);
129                 }
130                 hmp->flusher_signal = 0;
131         }
132         hmp->flusher_td = NULL;
133         wakeup(&hmp->flusher_exiting);
134         lwkt_exit();
135 }
136
137 static void
138 hammer_flusher_clean_loose_ios(hammer_mount_t hmp)
139 {
140         hammer_buffer_t buffer;
141         hammer_io_t io;
142
143         /*
144          * loose ends - buffers without bp's aren't tracked by the kernel
145          * and can build up, so clean them out.  This can occur when an
146          * IO completes on a buffer with no references left.
147          */
148         while ((io = TAILQ_FIRST(&hmp->lose_list)) != NULL) {
149                 KKASSERT(io->mod_list == &hmp->lose_list);
150                 TAILQ_REMOVE(io->mod_list, io, mod_entry);
151                 io->mod_list = NULL;
152                 hammer_ref(&io->lock);
153                 buffer = (void *)io;
154                 hammer_rel_buffer(buffer, 0);
155         }
156 }
157
158 /*
159  * Flush stuff
160  */
161 static void
162 hammer_flusher_flush(hammer_mount_t hmp)
163 {
164         hammer_volume_t root_volume;
165         hammer_blockmap_t rootmap;
166         hammer_inode_t ip;
167         hammer_off_t start_offset;
168         int error;
169
170         root_volume = hammer_get_root_volume(hmp, &error);
171         rootmap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
172         start_offset = rootmap->next_offset;
173
174         while ((ip = TAILQ_FIRST(&hmp->flush_list)) != NULL) {
175                 /*
176                  * Stop when we hit a different flush group
177                  */
178                 if (ip->flush_group != hmp->flusher_act)
179                         break;
180
181                 /*
182                  * Remove the inode from the flush list and inherit
183                  * its reference, sync, and clean-up.
184                  */
185                 TAILQ_REMOVE(&hmp->flush_list, ip, flush_entry);
186                 kprintf("s");
187                 ip->error = hammer_sync_inode(ip);
188                 hammer_flush_inode_done(ip);
189
190                 /*
191                  * XXX this breaks atomicy
192                  */
193                 if (hammer_must_finalize_undo(hmp)) {
194                         Debugger("Too many undos!!");
195                         hammer_flusher_finalize(hmp, root_volume, start_offset);
196                         start_offset = rootmap->next_offset;
197                 }
198         }
199         hammer_flusher_finalize(hmp, root_volume, start_offset);
200         hammer_rel_volume(root_volume, 0);
201 }
202
203 /*
204  * If the UNDO area gets over half full we have to flush it.  We can't
205  * afford the UNDO area becoming completely full as that would break
206  * the crash recovery atomicy.
207  */
208 static
209 int
210 hammer_must_finalize_undo(hammer_mount_t hmp)
211 {
212         if (hammer_undo_space(hmp) < hammer_undo_max(hmp) / 2) {
213                 kprintf("*");
214                 return(1);
215         } else {
216                 return(0);
217         }
218 }
219
220 /*
221  * To finalize the flush we finish flushing all undo and data buffers
222  * still present, then we update the volume header and flush it,
223  * then we flush out the mata-data (that can now be undone).
224  *
225  * Note that as long as the undo fifo's start and end points do not
226  * match, we always must at least update the volume header.
227  *
228  * The sync_lock is used by other threads to issue modifying operations
229  * to HAMMER media without crossing a synchronization boundary or messing
230  * up the media synchronization operation.  Specifically, the pruning
231  * the reblocking ioctls, and allowing the frontend strategy code to
232  * allocate media data space.
233  */
234 static
235 void
236 hammer_flusher_finalize(hammer_mount_t hmp, hammer_volume_t root_volume,
237                         hammer_off_t start_offset)
238 {
239         hammer_blockmap_t rootmap;
240         hammer_io_t io;
241
242         hammer_lock_ex(&hmp->sync_lock);
243
244         /*
245          * Flush undo bufs
246          */
247         while ((io = TAILQ_FIRST(&hmp->undo_list)) != NULL) {
248                 KKASSERT(io->modify_refs == 0);
249                 hammer_ref(&io->lock);
250                 KKASSERT(io->type != HAMMER_STRUCTURE_VOLUME);
251                 hammer_io_flush(io);
252                 hammer_rel_buffer((hammer_buffer_t)io, 1);
253         }
254
255         /*
256          * Flush data bufs
257          */
258         while ((io = TAILQ_FIRST(&hmp->data_list)) != NULL) {
259                 KKASSERT(io->modify_refs == 0);
260                 hammer_ref(&io->lock);
261                 KKASSERT(io->type != HAMMER_STRUCTURE_VOLUME);
262                 hammer_io_flush(io);
263                 hammer_rel_buffer((hammer_buffer_t)io, 1);
264         }
265
266         /*
267          * Wait for I/O to complete
268          */
269         crit_enter();
270         while (hmp->io_running_count) {
271                 kprintf("W[%d]", hmp->io_running_count);
272                 tsleep(&hmp->io_running_count, 0, "hmrfl1", 0);
273         }
274         crit_exit();
275
276         /*
277          * Update the volume header
278          */
279         rootmap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
280         if (rootmap->first_offset != start_offset) {
281                 hammer_modify_volume(NULL, root_volume, NULL, 0);
282                 rootmap->first_offset = start_offset;
283                 hammer_modify_volume_done(root_volume);
284         }
285         if (root_volume->ondisk->vol0_next_tid != hmp->next_tid) {
286                 hammer_modify_volume(NULL, root_volume, NULL, 0);
287                 root_volume->ondisk->vol0_next_tid = hmp->next_tid;
288                 hammer_modify_volume_done(root_volume);
289         }
290
291         /*
292          * Sync our cached blockmap array with the one in the root
293          * volume header.
294          */
295         if (root_volume->io.modified) {
296                 bcopy(hmp->blockmap, root_volume->ondisk->vol0_blockmap,
297                       sizeof(hmp->blockmap));
298                 hammer_io_flush(&root_volume->io);
299         }
300
301         /*
302          * Wait for I/O to complete
303          */
304         crit_enter();
305         while (hmp->io_running_count) {
306                 tsleep(&hmp->io_running_count, 0, "hmrfl2", 0);
307         }
308         crit_exit();
309
310         /*
311          * Flush meta-data
312          */
313         while ((io = TAILQ_FIRST(&hmp->meta_list)) != NULL) {
314                 KKASSERT(io->modify_refs == 0);
315                 hammer_ref(&io->lock);
316                 KKASSERT(io->type != HAMMER_STRUCTURE_VOLUME);
317                 hammer_io_flush(io);
318                 hammer_rel_buffer((hammer_buffer_t)io, 1);
319         }
320         hammer_unlock(&hmp->sync_lock);
321 }
322