File: kcopyd.c.async

package info (click to toggle)
devmapper 2%3A1.01.00-4sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,044 kB
  • ctags: 2,336
  • sloc: ansic: 11,777; sh: 2,695; makefile: 291; perl: 16
file content (637 lines) | stat: -rw-r--r-- 16,292 bytes parent folder | download | duplicates (2)
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
 * kcopyd.c
 *
 * Copyright (C) 2002 Sistina Software (UK) Limited.
 *
 * This file is released under the GPL.
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/fs.h>
#include <linux/blkdev.h>
#include <linux/device-mapper.h>

#include "dm.h"


/* Hard sector size used all over the kernel */
#define SECTOR_SIZE 512

/* Number of entries in the free list */
#define FREE_LIST_SIZE 32

/* Number of iobufs we have, therefore the number of I/Os we
   can be doing at once */
#define NUM_IOBUFS 16

/* Slab cache for work entries when the freelist runs out */
static kmem_cache_t *entry_cachep;

/* Structure of work to do in the list */
struct copy_work
{
	unsigned long fromsec;
	unsigned long tosec;
	unsigned long nr_sectors;
	unsigned long done_sectors;
	kdev_t fromdev;
	kdev_t todev;
	int    throttle;
	int    priority;      /* 0=highest */
	void   (*callback)(copy_cb_reason_t, void *, long);
	void   *context;      /* Parameter for callback */
	int    freelist;      /* Whether we came from the free list */
	struct iobuf_entry *iobuf;
	struct list_head list;
};

/* The free list of iobufs */
struct iobuf_entry
{
	struct kiobuf    *iobuf;
	struct copy_work *work;     /* Work entry we are doing */
	struct list_head list;
	copy_cb_reason_t complete_reason;
	long     nr_sectors;
	int      rw;
};

static LIST_HEAD(work_list);     /* Work to do or multiple-read blocks in progress */
static LIST_HEAD(write_list);    /* Writes to do */
static LIST_HEAD(free_list);     /* Free work units */
static LIST_HEAD(iobuf_list);    /* Free iobufs */
static LIST_HEAD(complete_list); /* work entries completed waiting notification */
static struct task_struct *copy_task = NULL;
static struct rw_semaphore work_list_lock;
static struct rw_semaphore free_list_lock;
static spinlock_t write_list_spinlock = SPIN_LOCK_UNLOCKED;
static spinlock_t complete_list_spinlock = SPIN_LOCK_UNLOCKED;
static DECLARE_MUTEX(start_lock);
static DECLARE_MUTEX(run_lock);
static DECLARE_WAIT_QUEUE_HEAD(start_waitq);
static DECLARE_WAIT_QUEUE_HEAD(work_waitq);
static DECLARE_WAIT_QUEUE_HEAD(freelist_waitq);
static int thread_exit = 0;

/* Find a free entry from the free-list or allocate a new one.
   This routine always returns a valid pointer even if it has to wait
   for it */
static struct copy_work *get_work_struct(void)
{
	struct copy_work *entry = NULL;

	while (!entry) {
		down_write(&free_list_lock);
		if (!list_empty(&free_list)) {
			entry = list_entry(free_list.next, struct copy_work, list);
			list_del(&entry->list);
		}
		up_write(&free_list_lock);

		/* Nothing on the free-list - try to allocate one without doing IO */
		if (!entry) {
			entry = kmem_cache_alloc(entry_cachep, GFP_NOIO);

			/* Make sure we know it didn't come from the free list */
			if (entry) {
				entry->freelist = 0;
			}
		}

		/* Failed...wait for IO to finish */
		if (!entry) {
			DECLARE_WAITQUEUE(wq, current);

			set_task_state(current, TASK_INTERRUPTIBLE);
			add_wait_queue(&freelist_waitq, &wq);

			if (list_empty(&free_list))
				schedule();

			set_task_state(current, TASK_RUNNING);
			remove_wait_queue(&freelist_waitq, &wq);
		}
	}

	return entry;
}

/* Add a new entry to the work list - in priority+FIFO order.
   The work_list_lock semaphore must be held */
static void add_to_work_list(struct copy_work *item)
{
	struct list_head *entry;

	list_for_each(entry, &work_list) {
		struct copy_work *cw;

		cw = list_entry(entry, struct copy_work, list);
		if (cw->priority > item->priority) {
			__list_add(&item->list, cw->list.prev, &cw->list);
			return;
		}
	}
	list_add_tail(&item->list, &work_list);
}

/* Called when the kio has finished - add the used bits back to their
   free lists and notify the user */
static void end_copy(struct iobuf_entry *ioe, copy_cb_reason_t reason)
{
        /* Tell the caller */
	if (ioe->work->callback)
		ioe->work->callback(reason, ioe->work->context, ioe->work->done_sectors);

	down_write(&free_list_lock);
	if (ioe->work->freelist) {
		list_add(&ioe->work->list, &free_list);
	}
	else {
		kmem_cache_free(entry_cachep, ioe->work);
	}
	list_add(&ioe->list, &iobuf_list);
	up_write(&free_list_lock);
	wake_up_interruptible(&freelist_waitq);
}

/* A single BH has completed */
static void end_bh(struct buffer_head *bh, int uptodate)
{
	struct kiobuf *kiobuf = bh->b_private;

	mark_buffer_uptodate(bh, uptodate);
	unlock_buffer(bh);

	if ((!uptodate) && !kiobuf->errno)
		kiobuf->errno = -EIO;

	/* Have all of them done ? */
	if (atomic_dec_and_test(&kiobuf->io_count)) {
		if (kiobuf->end_io)
			kiobuf->end_io(kiobuf);
	}
}

/* The whole iobuf has finished */
static void end_kiobuf(struct kiobuf *iobuf)
{
	struct iobuf_entry *ioe;

	/* Now, where did we leave that pointer...ah yes... */
	ioe = (struct iobuf_entry *)iobuf->blocks[0];

	if (ioe->rw == READ) {
		if (iobuf->errno) {
			ioe->complete_reason = COPY_CB_FAILED_READ;
			spin_lock_irq(&complete_list_spinlock);
			list_add(&ioe->list, &complete_list);
			spin_unlock_irq(&complete_list_spinlock);
			wake_up_interruptible(&work_waitq);
		}
		else {
			/* Put it on the write list */
			spin_lock_irq(&write_list_spinlock);
			list_add(&ioe->work->list, &write_list);
			spin_unlock_irq(&write_list_spinlock);
			wake_up_interruptible(&work_waitq);
		}
	}
	else {
		/* WRITE */
		if (iobuf->errno) {
			ioe->complete_reason = COPY_CB_FAILED_WRITE;
			spin_lock_irq(&complete_list_spinlock);
			list_add(&ioe->list, &complete_list);
			spin_unlock_irq(&complete_list_spinlock);
			wake_up_interruptible(&work_waitq);
		}
		else {
			/* All went well */
			ioe->work->done_sectors += ioe->nr_sectors;

			/* If not finished yet then do a progress callback */
			if (ioe->work->done_sectors < ioe->work->nr_sectors) {
				if (ioe->work->callback)
					ioe->work->callback(COPY_CB_PROGRESS, ioe->work->context, ioe->work->done_sectors);

				/* Put it back in the queue */
				down_write(&work_list_lock);
				add_to_work_list(ioe->work);
				up_write(&work_list_lock);
				wake_up_interruptible(&work_waitq);
			}
			else {
				ioe->complete_reason = COPY_CB_COMPLETE;
				spin_lock_irq(&complete_list_spinlock);
				list_add(&ioe->list, &complete_list);
				spin_unlock_irq(&complete_list_spinlock);
				wake_up_interruptible(&work_waitq);
			}
		}
	}
}

/* Asynchronous simplified version of brw_kiovec */
static int brw_kiobuf_async(int rw, struct iobuf_entry *ioe, unsigned long blocknr, kdev_t dev)
{
	int r, length, pi, bi = 0, offset, bsize;
	int nr_pages, nr_blocks;
	struct page *map;
	struct buffer_head *bh = 0;
	struct buffer_head **bhs = 0;

	length = ioe->iobuf->length;
	ioe->iobuf->errno = 0;
	bhs = ioe->iobuf->bh;
	bsize = get_hardsect_size(dev);
	nr_pages = length / PAGE_SIZE;
	nr_blocks = ioe->nr_sectors / (bsize/SECTOR_SIZE);

	/* Squirrel our pointer away somewhere secret */
	ioe->iobuf->blocks[0] = (long)ioe;
	ioe->iobuf->end_io = end_kiobuf;

	for (pi = 0; pi < nr_pages; pi++) {
		if (!(map = ioe->iobuf->maplist[pi])) {
			r = -EFAULT;
			goto bad;
		}
		offset = 0;

		while (offset < PAGE_SIZE) {
			bh = bhs[bi++];

			bh->b_dev = B_FREE;
			bh->b_size = bsize;
			set_bh_page(bh, map, offset);
			bh->b_this_page = bh;

			init_buffer(bh, end_bh, ioe->iobuf);
			bh->b_dev = dev;
			bh->b_blocknr = blocknr++;
			bh->b_private = ioe->iobuf;
			bh->b_state = ((1 << BH_Mapped) |
				       (1 << BH_Lock)   |
				       (1 << BH_Req));

			set_bit(BH_Uptodate, &bh->b_state);
			if (rw == WRITE)
				clear_bit(BH_Dirty, &bh->b_state);

			offset += bsize;
			atomic_inc(&ioe->iobuf->io_count);
			submit_bh(rw, bh);

			if (atomic_read(&ioe->iobuf->io_count) >= nr_blocks)
				break;
		}
	}
	return 0;

 bad:
	ioe->iobuf->errno = r;
	return r;
}

/* Allocate pages for a kiobuf */
static int alloc_iobuf_pages(struct kiobuf *iobuf, int nr_sectors)
{
	int nr_pages, err, i;

	if (nr_sectors > KIO_MAX_SECTORS)
		return -1;

	nr_pages = nr_sectors / (PAGE_SIZE/SECTOR_SIZE);
	err = expand_kiobuf(iobuf, nr_pages);
	if (err) goto out;

	err = -ENOMEM;
	iobuf->locked = 1;
	iobuf->nr_pages = 0;
	for (i = 0; i < nr_pages; i++) {
		struct page * page;

		page = alloc_page(GFP_KERNEL);
		if (!page) goto out;

		iobuf->maplist[i] = page;
		LockPage(page);
		iobuf->nr_pages++;
	}
	iobuf->offset = 0;

	err = 0;

out:
	return err;
}

/* Read/write chunk of data */
static int do_io(int rw, struct iobuf_entry *ioe, kdev_t dev, unsigned long start, int nr_sectors)
{
	int sectors_per_block;
	int blocksize = get_hardsect_size(dev);

	sectors_per_block = blocksize / SECTOR_SIZE;

	start /= sectors_per_block;

	ioe->iobuf->length = nr_sectors << 9;
	ioe->rw = rw;
	ioe->nr_sectors = nr_sectors;

	return brw_kiobuf_async(rw, ioe, start, dev);
}

/* This is where all the real work happens */
static int copy_kthread(void *unused)
{
	daemonize();
	down(&run_lock);

	strcpy(current->comm, "kcopyd");
	copy_task = current;
	wake_up_interruptible(&start_waitq);

	do {
		DECLARE_WAITQUEUE(wq, current);
		struct task_struct *tsk = current;
		struct list_head   *entry, *temp;

		/* First, check for outstanding writes to do */
		spin_lock_irq(&write_list_spinlock);
		list_for_each_safe(entry, temp, &write_list) {
			struct copy_work *work_item = list_entry(entry, struct copy_work, list);
			struct iobuf_entry *ioe = work_item->iobuf;

			list_del(&work_item->list);
			spin_unlock_irq(&write_list_spinlock);

			/* OK we read the data, now write it to the target device */
			if (do_io(WRITE, ioe, work_item->todev,
				  work_item->tosec + work_item->done_sectors,
				  ioe->nr_sectors) != 0) {
				DMERR("Write blocks to device %s failed", kdevname(work_item->todev));

				end_copy(ioe, COPY_CB_FAILED_WRITE);
			}
			spin_lock_irq(&write_list_spinlock);
		}
		spin_unlock_irq(&write_list_spinlock);

		/* Now look for new work, remember the list is in priority order */
		down_write(&work_list_lock);
		while (!list_empty(&work_list) && !list_empty(&iobuf_list)) {

			struct copy_work *work_item = list_entry(work_list.next, struct copy_work, list);
			struct iobuf_entry *ioe = list_entry(iobuf_list.next, struct iobuf_entry, list);
			long nr_sectors = min((unsigned long)KIO_MAX_SECTORS,
					      work_item->nr_sectors - work_item->done_sectors);

			list_del(&work_item->list);
			list_del(&ioe->list);
			up_write(&work_list_lock);

			/* Exchange pointers, this is legal for structures over 16 */
			ioe->work = work_item;
			work_item->iobuf = ioe;

			/* Read original blocks */
			if (do_io(READ, ioe, work_item->fromdev, work_item->fromsec + work_item->done_sectors,
				  nr_sectors) != 0) {
				DMERR("Read blocks from device %s failed", kdevname(work_item->fromdev));

				end_copy(ioe, COPY_CB_FAILED_READ);
			}

			/* Get the work lock again for the top of the while loop */
			down_write(&work_list_lock);
		}

		up_write(&work_list_lock);

		/* Wait for more work */
		set_task_state(tsk, TASK_INTERRUPTIBLE);
		add_wait_queue(&work_waitq, &wq);

		/* No work, or nothing to do it with */
		if ( (list_empty(&work_list) || list_empty(&iobuf_list)) &&
		     list_empty(&complete_list) &&
		     list_empty(&write_list))
			schedule();

		set_task_state(tsk, TASK_RUNNING);
		remove_wait_queue(&work_waitq, &wq);

		/* Check for completed entries and do the callbacks */
		spin_lock_irq(&complete_list_spinlock);
		list_for_each_safe(entry, temp, &complete_list) {
			struct iobuf_entry *ioe = list_entry(entry, struct iobuf_entry, list);
			list_del(&ioe->list);
			spin_unlock_irq(&complete_list_spinlock);

			end_copy(ioe, ioe->complete_reason);

			spin_lock_irq(&complete_list_spinlock);
		}
		spin_unlock_irq(&complete_list_spinlock);

	} while (thread_exit == 0);

	up(&run_lock);
	DMINFO("kcopyd shutting down");
	return 0;
}

/* API entry point */
int dm_blockcopy(unsigned long fromsec, unsigned long tosec, unsigned long nr_sectors,
		 kdev_t fromdev, kdev_t todev,
		 int priority, int throttle, void (*callback)(copy_cb_reason_t, void *, long), void *context)
{
	struct copy_work *newwork;
	static pid_t thread_pid = 0;
	long from_blocksize = get_hardsect_size(fromdev);
	long to_blocksize = get_hardsect_size(todev);

	/* Make sure the start sectors are on physical block boundaries */
	if (fromsec % (from_blocksize/SECTOR_SIZE))
		return -EINVAL;
	if (tosec % (to_blocksize/SECTOR_SIZE))
		return -EINVAL;

	/* Start the thread if we don't have one already */
	down(&start_lock);
	if (copy_task == NULL) {
		thread_pid = kernel_thread(copy_kthread, NULL, 0);
		if (thread_pid > 0) {

			DECLARE_WAITQUEUE(wq, current);
			struct task_struct *tsk = current;

			DMINFO("Started kcopyd thread, %d buffers", NUM_IOBUFS);

			/* Wait for it to complete it's startup initialisation */
			set_task_state(tsk, TASK_INTERRUPTIBLE);
			add_wait_queue(&start_waitq, &wq);

			if (!copy_task)
				schedule();

			set_task_state(tsk, TASK_RUNNING);
			remove_wait_queue(&start_waitq, &wq);
		}
		else {
			DMERR("Failed to start kcopyd thread");
			up(&start_lock);
			return -EAGAIN;
		}
	}
	up(&start_lock);

	/* This will wait until one is available */
	newwork = get_work_struct();

	newwork->fromsec      = fromsec;
	newwork->tosec        = tosec;
	newwork->fromdev      = fromdev;
	newwork->todev        = todev;
	newwork->nr_sectors   = nr_sectors;
	newwork->done_sectors = 0;
	newwork->throttle     = throttle;
	newwork->priority     = priority;
	newwork->callback     = callback;
	newwork->context      = context;

	down_write(&work_list_lock);
	add_to_work_list(newwork);
	up_write(&work_list_lock);

	wake_up_interruptible(&work_waitq);
	return 0;
}


/* Pre-allocate some structures for the free list */
static int allocate_free_list(void)
{
	int i;
	struct copy_work *newwork;

	for (i=0; i<FREE_LIST_SIZE; i++) {
		newwork = kmalloc(sizeof(struct copy_work), GFP_KERNEL);
		if (!newwork)
			return i;
		newwork->freelist = 1;
		list_add(&newwork->list, &free_list);
	}
	return i;
}

static void free_iobufs(void)
{
	struct list_head *entry, *temp;

	list_for_each_safe(entry, temp, &iobuf_list) {
		struct iobuf_entry *ioe = list_entry(entry, struct iobuf_entry, list);
		unmap_kiobuf(ioe->iobuf);
		free_kiovec(1, &ioe->iobuf);

		list_del(&ioe->list);
	}
}

int __init kcopyd_init(void)
{
	int i;

	init_rwsem(&work_list_lock);
	init_rwsem(&free_list_lock);
	init_MUTEX(&start_lock);
	init_MUTEX(&run_lock);

	for (i=0; i< NUM_IOBUFS; i++) {
		struct iobuf_entry *entry = kmalloc(sizeof(struct iobuf_entry), GFP_KERNEL);
		if (entry == NULL) {
			DMERR("Unable to allocate memory for kiobuf");
			free_iobufs();
			return -1;
		}
		if (alloc_kiovec(1, &entry->iobuf)) {
			DMERR("Unable to allocate kiobuf for kcopyd");
			kfree(entry);
			free_iobufs();
			return -1;
		}

		if (alloc_iobuf_pages(entry->iobuf, KIO_MAX_SECTORS)) {
			DMERR("Unable to allocate pages for kcopyd");
			free_kiovec(1, &entry->iobuf);
			kfree(entry);
			free_iobufs();
			return -1;
		}
		list_add(&entry->list, &iobuf_list);
	}

	entry_cachep = kmem_cache_create("kcopyd",
					 sizeof(struct copy_work),
					 __alignof__(struct copy_work),
					 0, NULL, NULL);
	if (!entry_cachep) {
		free_iobufs();
		DMERR("Unable to allocate slab cache for kcopyd");
		return -1;
	}

	if (allocate_free_list() == 0) {
		free_iobufs();
		kmem_cache_destroy(entry_cachep);
		DMERR("Unable to allocate any work structures for the free list");
		return -1;
	}

	return 0;
}

void kcopyd_exit(void)
{
	struct list_head *entry, *temp;

	thread_exit = 1;
	wake_up_interruptible(&work_waitq);

	/* Wait for the thread to finish */
	down(&run_lock);
	up(&run_lock);

	/* Free the iobufs */
	free_iobufs();

        /* Free the free list */
	list_for_each_safe(entry, temp, &free_list) {
		struct copy_work *cw;
		cw = list_entry(entry, struct copy_work, list);
		list_del(&cw->list);
		kfree(cw);
	}

	if (entry_cachep)
		kmem_cache_destroy(entry_cachep);
}

EXPORT_SYMBOL(dm_blockcopy);

/*
 * Overrides for Emacs so that we follow Linus's tabbing style.
 * Emacs will notice this stuff at the end of the file and automatically
 * adjust the settings for this buffer only.  This must remain at the end
 * of the file.
 * ---------------------------------------------------------------------------
 * Local variables:
 * c-file-style: "linux"
 * End:
 */