summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/server/integrity.c76
1 files changed, 51 insertions, 25 deletions
diff --git a/src/server/integrity.c b/src/server/integrity.c
index 4d01ba7..88b7487 100644
--- a/src/server/integrity.c
+++ b/src/server/integrity.c
@@ -13,13 +13,15 @@
#include <unistd.h>
#include <fcntl.h>
-#define CHECK_QUEUE_SIZE 500
+#define CHECK_QUEUE_SIZE 200
+
+#define CHECK_ALL (0x7fffffff)
typedef struct
{
dnbd3_image_t *image; // Image to check
int block; // Block to check
- bool full; // Check all blocks in image; .block will be increased
+ int count; // How many blocks to check starting at .block
} queue_entry;
static pthread_t thread;
@@ -73,13 +75,23 @@ void integrity_shutdown()
*/
void integrity_check(dnbd3_image_t *image, int block)
{
+ if ( !bRunning ) {
+ logadd( LOG_MINOR, "Ignoring check request; thread not running..." );
+ return;
+ }
int i, freeSlot = -1;
pthread_mutex_lock( &integrityQueueLock );
for (i = 0; i < queueLen; ++i) {
if ( freeSlot == -1 && checkQueue[i].image == NULL ) {
freeSlot = i;
} else if ( checkQueue[i].image == image
- && ( checkQueue[i].block == block || checkQueue[i].full ) ) {
+ && checkQueue[i].block <= block && checkQueue[i].block + checkQueue[i].count >= block ) {
+ // Already queued check dominates this one, or at least lies directly before this block
+ if ( checkQueue[i].block + checkQueue[i].count == block ) {
+ // It's directly before this one; expand range
+ checkQueue[i].count += 1;
+ }
+ logadd( LOG_DEBUG2, "Attaching to existing check request (%d/%d) (%d +%d)", i, queueLen, checkQueue[i].block, checkQueue[i].count );
pthread_mutex_unlock( &integrityQueueLock );
return;
}
@@ -87,7 +99,7 @@ void integrity_check(dnbd3_image_t *image, int block)
if ( freeSlot == -1 ) {
if ( queueLen >= CHECK_QUEUE_SIZE ) {
pthread_mutex_unlock( &integrityQueueLock );
- logadd( LOG_DEBUG1, "Check queue full, discarding check request...\n" );
+ logadd( LOG_INFO, "Check queue full, discarding check request...\n" );
return;
}
freeSlot = queueLen++;
@@ -95,10 +107,10 @@ void integrity_check(dnbd3_image_t *image, int block)
checkQueue[freeSlot].image = image;
if ( block == -1 ) {
checkQueue[freeSlot].block = 0;
- checkQueue[freeSlot].full = true;
+ checkQueue[freeSlot].count = CHECK_ALL;
} else {
checkQueue[freeSlot].block = block;
- checkQueue[freeSlot].full = false;
+ checkQueue[freeSlot].count = 1;
}
pthread_cond_signal( &queueSignal );
pthread_mutex_unlock( &integrityQueueLock );
@@ -126,13 +138,13 @@ static void* integrity_main(void * data UNUSED)
for (i = queueLen - 1; i >= 0; --i) {
if ( _shutdown ) break;
dnbd3_image_t * const image = image_lock( checkQueue[i].image );
- if ( !checkQueue[i].full || image == NULL ) {
- checkQueue[i].image = NULL;
+ if ( checkQueue[i].count == 0 || image == NULL ) {
+ checkQueue[i].image = image_release( image );
if ( i + 1 == queueLen ) queueLen--;
+ continue;
}
- if ( image == NULL ) continue;
// We have the image. Call image_release() some time
- bool full = checkQueue[i].full;
+ const int qCount = checkQueue[i].count;
bool foundCorrupted = false;
spin_lock( &image->lock );
if ( image->crc32 != NULL && image->realFilesize != 0 ) {
@@ -158,19 +170,23 @@ static void* integrity_main(void * data UNUSED)
image_ensureOpen( image );
fd = image->readFd;
}
- int checkCount = full ? 5 : 1;
+ int checkCount = MIN( qCount, 5 );
if ( fd != -1 ) {
while ( blocks[0] < numHashBlocks && !_shutdown ) {
const uint64_t start = blocks[0] * HASH_BLOCK_SIZE;
const uint64_t end = MIN( (uint64_t)(blocks[0] + 1) * HASH_BLOCK_SIZE, image->virtualFilesize );
bool complete = true;
- if ( full ) {
+ if ( qCount == CHECK_ALL ) {
// When checking full image, skip incomplete blocks, otherwise assume block is complete
spin_lock( &image->lock );
complete = image_isHashBlockComplete( image->cache_map, blocks[0], fileSize );
spin_unlock( &image->lock );
}
+#if defined(linux) || defined(__linux)
+ if ( sync_file_range( fd, start, end - start, SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE | SYNC_FILE_RANGE_WAIT_AFTER ) == -1 ) {
+#else
if ( fsync( fd ) == -1 ) {
+#endif
logadd( LOG_ERROR, "Cannot flush %s for integrity check", image->path );
exit( 1 );
}
@@ -191,36 +207,46 @@ static void* integrity_main(void * data UNUSED)
logadd( LOG_WARNING, "Hash check for block %d of %s failed!", blocks[0], image->name );
image_updateCachemap( image, start, end, false );
// If this is not a full check, queue one
- if ( !full ) {
+ if ( qCount != CHECK_ALL ) {
logadd( LOG_INFO, "Queueing full check for %s", image->name );
integrity_check( image, -1 );
}
foundCorrupted = true;
}
+ blocks[0]++; // Increase before break, so it always points to the next block to check after loop
if ( complete && --checkCount == 0 ) break;
- blocks[0]++;
}
if ( direct ) {
close( fd );
}
}
pthread_mutex_lock( &integrityQueueLock );
- if ( full ) {
- assert( checkQueue[i].image == image );
- assert( checkQueue[i].full );
- if ( checkCount == 0 ) {
- // Not done yet, keep going
- checkQueue[i].block = blocks[0] + 1;
- } else {
- // Didn't check as many blocks as requested, so we must be done
- checkQueue[i].image = NULL;
- if ( i + 1 == queueLen ) queueLen--;
+ assert( checkQueue[i].image == image );
+ if ( qCount != CHECK_ALL ) {
+ // Not a full check; update the counter
+ checkQueue[i].count -= ( blocks[0] - checkQueue[i].block );
+ if ( checkQueue[i].count < 0 ) {
+ logadd( LOG_WARNING, "BUG! checkQueue counter ran negative" );
+ }
+ }
+ if ( checkCount > 0 || checkQueue[i].count <= 0 || fd == -1 ) {
+ // Done with this task as nothing left, OR we don't have an fd to read from
+ if ( fd == -1 ) {
+ logadd( LOG_WARNING, "Cannot hash check %s: bad fd", image->path );
+ }
+ checkQueue[i].image = NULL;
+ if ( i + 1 == queueLen ) queueLen--;
+ // Mark as working again if applicable
+ if ( !foundCorrupted ) {
spin_lock( &image->lock );
if ( image->uplink != NULL ) { // TODO: image_determineWorkingState() helper?
image->working = image->uplink->fd != -1 && image->readFd != -1;
}
spin_unlock( &image->lock );
}
+ } else {
+ // Still more blocks to go...
+ checkQueue[i].block = blocks[0];
}
} else {
spin_unlock( &image->lock );
@@ -243,6 +269,6 @@ static void* integrity_main(void * data UNUSED)
pthread_mutex_unlock( &integrityQueueLock );
if ( buffer != NULL ) free( buffer );
bRunning = false;
- return NULL ;
+ return NULL;
}
Wilcox 2007-12-04 16:39:55 +0100 dmapool: Fix style problems' href='/openslx/kernel-qcow2-linux.git/commit/mm/dmapool.c?id=e87aa773747fb5e4217d716ea22a573c03b6693a'>e87aa773747f ^
1da177e4c3f4


e87aa773747f ^
1da177e4c3f4

141ecc532014 ^

b2366d68d9ec ^
e87aa773747f ^

141ecc532014 ^

1da177e4c3f4
141ecc532014 ^
e87aa773747f ^
141ecc532014 ^



b2366d68d9ec ^
1da177e4c3f4
e87aa773747f ^
1da177e4c3f4


e87aa773747f ^
1da177e4c3f4
e87aa773747f ^
1da177e4c3f4
e87aa773747f ^

1da177e4c3f4


e87aa773747f ^
1da177e4c3f4
5cbded585d12 ^
1da177e4c3f4

e87aa773747f ^


1da177e4c3f4
e87aa773747f ^
1da177e4c3f4
e87aa773747f ^
1da177e4c3f4
e87aa773747f ^
1da177e4c3f4

e87aa773747f ^
1da177e4c3f4




e87aa773747f ^
1da177e4c3f4








e87aa773747f ^
1da177e4c3f4
e87aa773747f ^
1da177e4c3f4

e87aa773747f ^
1da177e4c3f4
e87aa773747f ^


1da177e4c3f4

1da177e4c3f4







e87aa773747f ^
1da177e4c3f4
b2366d68d9ec ^
e87aa773747f ^


b2366d68d9ec ^
1da177e4c3f4
e87aa773747f ^




1da177e4c3f4
e87aa773747f ^

1da177e4c3f4

e87aa773747f ^


1da177e4c3f4
e87aa773747f ^

1da177e4c3f4
e87aa773747f ^
1da177e4c3f4

e87aa773747f ^
1da177e4c3f4
e87aa773747f ^
1da177e4c3f4










e87aa773747f ^

1da177e4c3f4
e87aa773747f ^







1da177e4c3f4
e87aa773747f ^
1da177e4c3f4

e87aa773747f ^

1da177e4c3f4
e87aa773747f ^
1da177e4c3f4
e87aa773747f ^
1da177e4c3f4





e87aa773747f ^

1da177e4c3f4
e87aa773747f ^
1da177e4c3f4
d9aacccf457d ^
e87aa773747f ^

1da177e4c3f4
e87aa773747f ^
1da177e4c3f4
e87aa773747f ^
1da177e4c3f4





e87aa773747f ^
1da177e4c3f4
e87aa773747f ^
1da177e4c3f4



e87aa773747f ^
1da177e4c3f4
e87aa773747f ^

1da177e4c3f4

e87aa773747f ^
1da177e4c3f4
e87aa773747f ^
1da177e4c3f4
e87aa773747f ^

1da177e4c3f4
e87aa773747f ^
1da177e4c3f4






e87aa773747f ^

1da177e4c3f4


1da177e4c3f4








e87aa773747f ^
1da177e4c3f4
e87aa773747f ^


1da177e4c3f4
e87aa773747f ^

1da177e4c3f4
e87aa773747f ^


1da177e4c3f4
e87aa773747f ^

1da177e4c3f4










e87aa773747f ^


1da177e4c3f4
e87aa773747f ^


1da177e4c3f4

e87aa773747f ^
1da177e4c3f4
e87aa773747f ^

1da177e4c3f4

e87aa773747f ^


1da177e4c3f4

e87aa773747f ^
1da177e4c3f4

e87aa773747f ^
1da177e4c3f4
e87aa773747f ^


1da177e4c3f4




e87aa773747f ^
1da177e4c3f4
e87aa773747f ^
1da177e4c3f4
9ac7849e35f7 ^










































e87aa773747f ^
9ac7849e35f7 ^













e87aa773747f ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477



                                                              



                              
                         
                        






                                                                         









                                              

  





                                                                             


                                                              
 
                                

              
                                                                        













                                                         
                                










                                                                         


                                                                       


                             
                                  


                                

                                                     





















                                                                            

                                                                              
 
                                
















                                               
                                                            


                                     


                                                                         

                              
                                                         


                          

                                           


                                                    
                                            

                  

                        
                                        

                                                                       

                                
                                                                     
                         
                                                                  



                                      
                                          
              
                                               


                      
                               
 
                                                                               
 

                              


                                                                
                                
 
                                                          

                            


                                                                
                          
                                                                             
                         
                                                                         
      
                                                             

                                 
                            




                            
                                                                 








                                        
                                                                        
 
                                   

                         
                                                                 
      


                                                                         

 







                                                                 
                                            
 
                                


                                                               
                                  
 




                                                                        
                                      

                                                                         

                                                                 


                                                                        
                                                                     

                                                   
                      
                                                   

         
                    
 
                                










                                                                       

                                                            
 







                                              
                                                                
                      

                                                     

                                                                            
                                         
                                                        
                                                                  
                                                                     





                                                                       

                                                 
                                             
                                                         
 
                                                                

                                                                   
 
                                                               
 
                                                               





                                     
                                       
                   
       



                                      
                                                          
      

                                                   

                      
                              
 
                                                                             
 

                              
 
                                              






                                                                

                                                   


                    








                                                                        
                                                                      
 


                              
 

                                         
                              


                                                                       
                    

                                                                               










                                                                 


                                                                            
                    


                                                                           

                       
                                                 
                              

                                                                           

                                                                     


                                                                          

                       
                                                     

      
                                              
                       


                                           




                                                                               
                                                   
 
                             
 










































                                                                               
                                













                                                                               
                                 

#include <linux/device.h>
#include <linux/mm.h>
#include <asm/io.h>		/* Needed for i386 to build */
#include <linux/dma-mapping.h>
#include <linux/dmapool.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/poison.h>
#include <linux/sched.h>

/*
 * Pool allocator ... wraps the dma_alloc_coherent page allocator, so
 * small blocks are easily used by drivers for bus mastering controllers.
 * This should probably be sharing the guts of the slab allocator.
 */

struct dma_pool {		/* the pool */
	struct list_head page_list;
	spinlock_t lock;
	size_t blocks_per_page;
	size_t size;
	struct device *dev;
	size_t allocation;
	char name[32];
	wait_queue_head_t waitq;
	struct list_head pools;
};

struct dma_page {		/* cacheable header for 'allocation' bytes */
	struct list_head page_list;
	void *vaddr;
	dma_addr_t dma;
	unsigned in_use;
	unsigned long bitmap[0];
};

#define	POOL_TIMEOUT_JIFFIES	((100 /* msec */ * HZ) / 1000)

static DEFINE_MUTEX(pools_lock);

static ssize_t
show_pools(struct device *dev, struct device_attribute *attr, char *buf)
{
	unsigned temp;
	unsigned size;
	char *next;
	struct dma_page *page;
	struct dma_pool *pool;

	next = buf;
	size = PAGE_SIZE;

	temp = scnprintf(next, size, "poolinfo - 0.1\n");
	size -= temp;
	next += temp;

	mutex_lock(&pools_lock);
	list_for_each_entry(pool, &dev->dma_pools, pools) {
		unsigned pages = 0;
		unsigned blocks = 0;

		list_for_each_entry(page, &pool->page_list, page_list) {
			pages++;
			blocks += page->in_use;
		}

		/* per-pool info, no real statistics yet */
		temp = scnprintf(next, size, "%-16s %4u %4Zu %4Zu %2u\n",
				 pool->name,
				 blocks, pages * pool->blocks_per_page,
				 pool->size, pages);
		size -= temp;
		next += temp;
	}
	mutex_unlock(&pools_lock);

	return PAGE_SIZE - size;
}

static DEVICE_ATTR(pools, S_IRUGO, show_pools, NULL);

/**
 * dma_pool_create - Creates a pool of consistent memory blocks, for dma.
 * @name: name of pool, for diagnostics
 * @dev: device that will be doing the DMA
 * @size: size of the blocks in this pool.
 * @align: alignment requirement for blocks; must be a power of two
 * @allocation: returned blocks won't cross this boundary (or zero)
 * Context: !in_interrupt()
 *
 * Returns a dma allocation pool with the requested characteristics, or
 * null if one can't be created.  Given one of these pools, dma_pool_alloc()
 * may be used to allocate memory.  Such memory will all have "consistent"
 * DMA mappings, accessible by the device and its driver without using
 * cache flushing primitives.  The actual size of blocks allocated may be
 * larger than requested because of alignment.
 *
 * If allocation is nonzero, objects returned from dma_pool_alloc() won't
 * cross that size boundary.  This is useful for devices which have
 * addressing restrictions on individual DMA transfers, such as not crossing
 * boundaries of 4KBytes.
 */
struct dma_pool *dma_pool_create(const char *name, struct device *dev,
				 size_t size, size_t align, size_t allocation)
{
	struct dma_pool *retval;

	if (align == 0)
		align = 1;
	if (size == 0)
		return NULL;
	else if (size < align)
		size = align;
	else if ((size % align) != 0) {
		size += align + 1;
		size &= ~(align - 1);
	}

	if (allocation == 0) {
		if (PAGE_SIZE < size)
			allocation = size;
		else
			allocation = PAGE_SIZE;
		/* FIXME: round up for less fragmentation */
	} else if (allocation < size)
		return NULL;

	if (!
	    (retval =
	     kmalloc_node(sizeof *retval, GFP_KERNEL, dev_to_node(dev))))
		return retval;

	strlcpy(retval->name, name, sizeof retval->name);

	retval->dev = dev;

	INIT_LIST_HEAD(&retval->page_list);
	spin_lock_init(&retval->lock);
	retval->size = size;
	retval->allocation = allocation;
	retval->blocks_per_page = allocation / size;
	init_waitqueue_head(&retval->waitq);

	if (dev) {