2 * kernel/power/tuxonice_block_io.c
4 * Copyright (C) 2004-2007 Nigel Cunningham (nigel at suspend2 net)
6 * Distributed under GPLv2.
8 * This file contains block io functions for TuxOnIce. These are
9 * used by the swapwriter and it is planned that they will also
10 * be used by the NFSwriter.
14 #include <linux/blkdev.h>
15 #include <linux/syscalls.h>
16 #include <linux/suspend.h>
19 #include "tuxonice_sysfs.h"
20 #include "tuxonice_modules.h"
21 #include "tuxonice_prepare_image.h"
22 #include "tuxonice_block_io.h"
23 #include "tuxonice_ui.h"
28 #define PR_DEBUG(a, b...) do { if (pr_index < 20) printk(a, ##b); } while(0)
30 #define PR_DEBUG(a, b...) do { } while(0)
33 #define MAX_OUTSTANDING_IO 2048
34 #define SUBMIT_BATCH_SIZE 128
36 static int max_outstanding_io
= MAX_OUTSTANDING_IO
;
37 static int submit_batch_size
= SUBMIT_BATCH_SIZE
;
40 struct bio
*sys_struct
;
42 struct page
*bio_page
, *dest_page
;
43 int writing
, readahead_index
;
44 struct block_device
*dev
;
45 struct list_head list
;
48 static LIST_HEAD(ioinfo_ready_for_cleanup
);
49 static DEFINE_SPINLOCK(ioinfo_ready_lock
);
51 static LIST_HEAD(ioinfo_submit_batch
);
52 static DEFINE_SPINLOCK(ioinfo_submit_lock
);
54 static LIST_HEAD(ioinfo_busy
);
55 static DEFINE_SPINLOCK(ioinfo_busy_lock
);
57 static struct io_info
*waiting_on
;
59 static atomic_t submit_batch
;
60 static int submit_batched(void);
62 /* [Max] number of I/O operations pending */
63 static atomic_t outstanding_io
;
65 static int extra_page_forward
= 0;
67 static volatile unsigned long toi_readahead_flags
[
68 DIV_ROUND_UP(MAX_OUTSTANDING_IO
, BITS_PER_LONG
)];
69 static spinlock_t toi_readahead_flags_lock
= SPIN_LOCK_UNLOCKED
;
70 static struct page
*toi_ra_pages
[MAX_OUTSTANDING_IO
];
71 static int readahead_index
, ra_submit_index
;
73 static int current_stream
;
74 /* 0 = Header, 1 = Pageset1, 2 = Pageset2 */
75 struct extent_iterate_saved_state toi_writer_posn_save
[3];
77 /* Pointer to current entry being loaded/saved. */
78 struct extent_iterate_state toi_writer_posn
;
80 /* Not static, so that the allocators can setup and complete
81 * writing the header */
82 char *toi_writer_buffer
;
83 int toi_writer_buffer_posn
;
87 static struct toi_bdev_info
*toi_devinfo
;
89 int toi_header_bytes_used
= 0;
91 DEFINE_MUTEX(toi_bio_mutex
);
94 * toi_bio_cleanup_one: Cleanup one bio.
95 * @io_info : Struct io_info to be cleaned up.
97 * Cleanup the bio pointed to by io_info and record as appropriate that the
100 static void toi_bio_cleanup_one(struct io_info
*io_info
)
102 int readahead_index
= io_info
->readahead_index
;
105 if (!io_info
->writing
&& readahead_index
== -1) {
106 char *to
= (char *) kmap(io_info
->dest_page
);
107 char *from
= (char *) kmap(io_info
->bio_page
);
108 memcpy(to
, from
, PAGE_SIZE
);
109 kunmap(io_info
->dest_page
);
110 kunmap(io_info
->bio_page
);
113 put_page(io_info
->bio_page
);
114 if (io_info
->writing
|| readahead_index
== -1)
115 __free_page(io_info
->bio_page
);
117 bio_put(io_info
->sys_struct
);
118 io_info
->sys_struct
= NULL
;
120 if (readahead_index
> -1) {
121 int index
= readahead_index
/BITS_PER_LONG
;
122 int bit
= readahead_index
- (index
* BITS_PER_LONG
);
123 spin_lock_irqsave(&toi_readahead_flags_lock
, flags
);
124 set_bit(bit
, &toi_readahead_flags
[index
]);
125 spin_unlock_irqrestore(&toi_readahead_flags_lock
, flags
);
128 if (waiting_on
== io_info
)
131 atomic_dec(&outstanding_io
);
135 * toi_cleanup_some_completed_io: Cleanup completed TuxOnIce i/o.
137 * Cleanup i/o that has been completed. In the end_bio routine (below), we only
138 * move the associated io_info struct from the busy list to the
139 * ready_for_cleanup list. Now (no longer in an interrupt context), we can we
140 * can do the real work.
142 * This routine is designed so that multiple callers can be in here
145 static void toi_cleanup_some_completed_io(void)
150 spin_lock_irqsave(&ioinfo_ready_lock
, flags
);
151 while(!list_empty(&ioinfo_ready_for_cleanup
)) {
152 struct io_info
*first
= list_entry(
153 ioinfo_ready_for_cleanup
.next
,
154 struct io_info
, list
);
156 list_del_init(&first
->list
);
158 spin_unlock_irqrestore(&ioinfo_ready_lock
, flags
);
159 toi_bio_cleanup_one(first
);
160 spin_lock_irqsave(&ioinfo_ready_lock
, flags
);
163 if (num_cleaned
== submit_batch_size
)
166 spin_unlock_irqrestore(&ioinfo_ready_lock
, flags
);
170 * do_bio_wait: Wait for some TuxOnIce i/o to complete.
172 * Submit any I/O that's batched up (if we're not already doing
173 * that, schedule and clean up whatever we can.
175 static void do_bio_wait(void)
177 struct backing_dev_info
*bdi
;
182 bdi
= waiting_on
->dev
->bd_inode
->i_mapping
->backing_dev_info
;
183 blk_run_backing_dev(bdi
, waiting_on
->bio_page
);
187 toi_cleanup_some_completed_io();
191 * toi_finish_all_io: Complete all outstanding i/o.
193 static void toi_finish_all_io(void)
195 while (atomic_read(&outstanding_io
))
200 * toi_readahead_ready: Is this readahead finished?
202 * Returns whether the readahead requested is ready.
204 static int toi_readahead_ready(int readahead_index
)
206 int index
= readahead_index
/ BITS_PER_LONG
;
207 int bit
= readahead_index
- (index
* BITS_PER_LONG
);
209 return test_bit(bit
, &toi_readahead_flags
[index
]);
213 * toi_wait_on_readahead: Wait on a particular page.
215 * @readahead_index: Index of the readahead to wait for.
217 static void toi_wait_on_readahead(int readahead_index
)
219 while (!toi_readahead_ready(readahead_index
))
223 static int toi_prepare_readahead(int index
)
225 unsigned long new_page
= get_zeroed_page(TOI_ATOMIC_GFP
);
230 toi_ra_pages
[index
] = virt_to_page(new_page
);
234 /* toi_readahead_cleanup
235 * Clean up structures used for readahead */
236 static void toi_cleanup_readahead(int page
)
238 __free_page(toi_ra_pages
[page
]);
239 toi_ra_pages
[page
] = 0;
244 * toi_end_bio: bio completion function.
246 * @bio: bio that has completed.
247 * @bytes_done: Number of bytes written/read.
248 * @err: Error value. Yes, like end_swap_bio_read, we ignore it.
250 * Function called by block driver from interrupt context when I/O is completed.
251 * This is the reason we use spinlocks in manipulating the io_info lists. Nearly
252 * the fs/buffer.c version, but we want to mark the page as done in our own
255 static int toi_end_bio(struct bio
*bio
, unsigned int bytes_done
, int err
)
257 struct io_info
*io_info
= bio
->bi_private
;
260 spin_lock_irqsave(&ioinfo_busy_lock
, flags
);
261 list_del_init(&io_info
->list
);
262 spin_unlock_irqrestore(&ioinfo_busy_lock
, flags
);
264 spin_lock_irqsave(&ioinfo_ready_lock
, flags
);
265 list_add_tail(&io_info
->list
, &ioinfo_ready_for_cleanup
);
266 spin_unlock_irqrestore(&ioinfo_ready_lock
, flags
);
271 * submit - submit BIO request.
272 * @writing: READ or WRITE.
273 * @io_info: IO info structure.
275 * Based on Patrick's pmdisk code from long ago:
276 * "Straight from the textbook - allocate and initialize the bio.
277 * If we're writing, make sure the page is marked as dirty.
278 * Then submit it and carry on."
280 * With a twist, though - we handle block_size != PAGE_SIZE.
281 * Caller has already checked that our page is not fragmented.
283 static int submit(struct io_info
*io_info
)
285 struct bio
*bio
= NULL
;
289 bio
= bio_alloc(GFP_ATOMIC
,1);
294 bio
->bi_bdev
= io_info
->dev
;
295 bio
->bi_sector
= io_info
->first_block
;
296 bio
->bi_private
= io_info
;
297 bio
->bi_end_io
= toi_end_bio
;
298 io_info
->sys_struct
= bio
;
300 if (bio_add_page(bio
, io_info
->bio_page
, PAGE_SIZE
, 0) < PAGE_SIZE
) {
301 printk("ERROR: adding page to bio at %lld\n",
302 (unsigned long long) io_info
->first_block
);
307 if (io_info
->writing
)
308 bio_set_pages_dirty(bio
);
310 spin_lock_irqsave(&ioinfo_busy_lock
, flags
);
311 list_add_tail(&io_info
->list
, &ioinfo_busy
);
312 spin_unlock_irqrestore(&ioinfo_busy_lock
, flags
);
314 submit_bio(io_info
->writing
, bio
);
320 * submit_batched: Submit a batch of bios we've been saving up.
322 * Submit a batch. The submit function can wait on I/O, so we have
323 * simple locking to avoid infinite recursion.
325 static int submit_batched(void)
327 static int running_already
= 0;
328 struct io_info
*first
;
330 int num_submitted
= 0;
336 spin_lock_irqsave(&ioinfo_submit_lock
, flags
);
337 while(!list_empty(&ioinfo_submit_batch
)) {
338 first
= list_entry(ioinfo_submit_batch
.next
, struct io_info
,
340 list_del_init(&first
->list
);
341 atomic_dec(&submit_batch
);
342 spin_unlock_irqrestore(&ioinfo_submit_lock
, flags
);
344 spin_lock_irqsave(&ioinfo_submit_lock
, flags
);
346 if (num_submitted
== submit_batch_size
)
349 spin_unlock_irqrestore(&ioinfo_submit_lock
, flags
);
352 return num_submitted
;
356 * add_to_batch: Add a page of i/o to our batch for later submission.
358 * @io_info: Data structure describing a page of I/O to be done.
360 static void add_to_batch(struct io_info
*io_info
)
365 /* Put our prepared I/O struct on the batch list. */
366 spin_lock_irqsave(&ioinfo_submit_lock
, flags
);
367 list_add_tail(&io_info
->list
, &ioinfo_submit_batch
);
368 waiting
= atomic_add_return(1, &submit_batch
);
369 spin_unlock_irqrestore(&ioinfo_submit_lock
, flags
);
371 if (waiting
>= submit_batch_size
)
376 * get_io_info_struct: Allocate a struct for recording info on i/o submitted.
378 static struct io_info
*get_io_info_struct(void)
380 struct io_info
*this = NULL
;
383 while (atomic_read(&outstanding_io
) >= max_outstanding_io
)
386 this = kmalloc(sizeof(struct io_info
), GFP_ATOMIC
);
389 INIT_LIST_HEAD(&this->list
);
394 * toi_do_io: Prepare to do some i/o on a page and submit or batch it.
396 * @writing: Whether reading or writing.
397 * @bdev: The block device which we're using.
398 * @block0: The first sector we're reading or writing.
399 * @page: The page on which I/O is being done.
400 * @readahead_index: If doing readahead, the index (reset this flag when done).
401 * @syncio: Whether the i/o is being done synchronously.
403 * Prepare and start a read or write operation.
405 * Note that we always work with our own page. If writing, we might be given a
406 * compression buffer that will immediately be used to start compressing the
407 * next page. For reading, we do readahead and therefore don't know the final
408 * address where the data needs to go.
410 * Failure? What's that?
412 static void toi_do_io(int writing
, struct block_device
*bdev
, long block0
,
413 struct page
*page
, int readahead_index
, int syncio
)
415 struct io_info
*io_info
= get_io_info_struct();
416 unsigned long buffer_virt
= 0;
419 /* Done before submitting to avoid races. */
421 waiting_on
= io_info
;
423 /* Copy settings to the io_info struct */
424 io_info
->writing
= writing
;
426 io_info
->first_block
= block0
;
427 io_info
->dest_page
= page
;
428 io_info
->readahead_index
= readahead_index
;
430 if (io_info
->readahead_index
== -1) {
431 while (!(buffer_virt
= get_zeroed_page(TOI_ATOMIC_GFP
)))
434 io_info
->bio_page
= virt_to_page(buffer_virt
);
437 int index
= io_info
->readahead_index
/ BITS_PER_LONG
;
438 int bit
= io_info
->readahead_index
- index
* BITS_PER_LONG
;
440 spin_lock_irqsave(&toi_readahead_flags_lock
, flags
);
441 clear_bit(bit
, &toi_readahead_flags
[index
]);
442 spin_unlock_irqrestore(&toi_readahead_flags_lock
, flags
);
444 io_info
->bio_page
= page
;
448 * If writing, copy our data. The data is probably in lowmem, but we cannot be
449 * certain. If there is no compression, we might be passed the actual source
453 to
= (char *) buffer_virt
;
454 from
= kmap_atomic(page
, KM_USER1
);
455 memcpy(to
, from
, PAGE_SIZE
);
456 kunmap_atomic(from
, KM_USER1
);
459 /* Submit the page */
460 get_page(io_info
->bio_page
);
465 add_to_batch(io_info
);
467 atomic_inc(&outstanding_io
);
470 do { do_bio_wait(); } while (waiting_on
);
474 * toi_bdev_page_io: Simpler interface to do directly i/o on a single page.
476 * @writing: Whether reading or writing.
477 * @bdev: Block device on which we're operating.
478 * @pos: Sector at which page to read starts.
479 * @page: Page to be read/written.
481 * We used to use bread here, but it doesn't correctly handle
482 * blocksize != PAGE_SIZE. Now we create a submit_info to get the data we
483 * want and use our normal routines (synchronously).
485 static void toi_bdev_page_io(int writing
, struct block_device
*bdev
,
486 long pos
, struct page
*page
)
488 toi_do_io(writing
, bdev
, pos
, page
, -1, 1);
492 * toi_bio_memory_needed: Report amount of memory needed for block i/o.
494 * We want to have at least enough memory so as to have max_outstanding_io
495 * transactions on the fly at once. If we can do more, fine.
497 static int toi_bio_memory_needed(void)
499 return (max_outstanding_io
* (PAGE_SIZE
+ sizeof(struct request
) +
500 sizeof(struct bio
) + sizeof(struct io_info
)));
504 * toi_set_devinfo: Set the bdev info used for i/o.
506 * @info: Pointer to array of struct toi_bdev_info - the list of
507 * bdevs and blocks on them in which the image is stored.
509 * Set the list of bdevs and blocks in which the image will be stored.
510 * Sort of like putting a tape in the cassette player.
512 static void toi_set_devinfo(struct toi_bdev_info
*info
)
518 * dump_block_chains: Print the contents of the bdev info array.
520 static void dump_block_chains(void)
524 for (i
= 0; i
< toi_writer_posn
.num_chains
; i
++) {
527 printk("Chain %d:", i
);
529 this = (toi_writer_posn
.chains
+ i
)->first
;
535 printk(" [%lu-%lu]%s", this->minimum
, this->maximum
,
536 this->next
? "," : "");
543 for (i
= 0; i
< 3; i
++)
544 printk("Posn %d: Chain %d, extent %d, offset %lu.\n", i
,
545 toi_writer_posn_save
[i
].chain_num
,
546 toi_writer_posn_save
[i
].extent_num
,
547 toi_writer_posn_save
[i
].offset
);
551 * go_next_page: Skip blocks to the start of the next page.
553 * Go forward one page, or two if extra_page_forward is set. It only gets
554 * set at the start of reading the image header, to skip the first page
555 * of the header, which is read without using the extent chains.
557 static int go_next_page(void)
559 int i
, max
= (toi_writer_posn
.current_chain
== -1) ? 1 :
560 toi_devinfo
[toi_writer_posn
.current_chain
].blocks_per_page
;
562 for (i
= 0; i
< max
; i
++)
563 toi_extent_state_next(&toi_writer_posn
);
565 if (toi_extent_state_eof(&toi_writer_posn
)) {
566 printk("Extent state eof. "
567 "Expected compression ratio too optimistic?\n");
572 if (extra_page_forward
) {
573 extra_page_forward
= 0;
574 return go_next_page();
581 * set_extra_page_forward: Make us skip an extra page on next go_next_page.
583 * Used in reading header, to jump to 2nd page after getting 1st page
584 * direct from image header.
586 static void set_extra_page_forward(void)
588 extra_page_forward
= 1;
592 * toi_bio_rw_page: Do i/o on the next disk page in the image.
594 * @writing: Whether reading or writing.
595 * @page: Page to do i/o on.
596 * @readahead_index: -1 or the index in the readahead ring.
598 * Submit a page for reading or writing, possibly readahead.
600 static int toi_bio_rw_page(int writing
, struct page
*page
,
603 struct toi_bdev_info
*dev_info
;
605 if (test_action_state(TOI_TEST_FILTER_SPEED
))
608 if (go_next_page()) {
609 printk("Failed to advance a page in the extent data.\n");
613 if (current_stream
== 0 && writing
&&
614 toi_writer_posn
.current_chain
== toi_writer_posn_save
[2].chain_num
&&
615 toi_writer_posn
.current_offset
== toi_writer_posn_save
[2].offset
) {
620 dev_info
= &toi_devinfo
[toi_writer_posn
.current_chain
];
622 toi_do_io(writing
, dev_info
->bdev
,
623 toi_writer_posn
.current_offset
<<
624 dev_info
->bmap_shift
,
625 page
, readahead_index
, 0);
631 * toi_rw_init: Prepare to read or write a stream in the image.
633 * @writing: Whether reading or writing.
634 * @stream number: Section of the image being processed.
636 static int toi_rw_init(int writing
, int stream_number
)
638 toi_header_bytes_used
= 0;
640 toi_extent_state_restore(&toi_writer_posn
,
641 &toi_writer_posn_save
[stream_number
]);
643 toi_writer_buffer_posn
= writing
? 0 : PAGE_SIZE
;
645 current_stream
= stream_number
;
647 readahead_index
= ra_submit_index
= -1;
655 * toi_read_header_init: Prepare to read the image header.
657 * Reset readahead indices prior to starting to read a section of the image.
659 static void toi_read_header_init(void)
661 readahead_index
= ra_submit_index
= -1;
665 * toi_rw_cleanup: Cleanup after i/o.
667 * @writing: Whether we were reading or writing.
669 static int toi_rw_cleanup(int writing
)
671 if (writing
&& toi_bio_rw_page(WRITE
,
672 virt_to_page(toi_writer_buffer
), -1))
675 if (writing
&& current_stream
== 2)
676 toi_extent_state_save(&toi_writer_posn
,
677 &toi_writer_posn_save
[1]);
682 while (readahead_index
!= ra_submit_index
) {
683 toi_cleanup_readahead(readahead_index
);
685 if (readahead_index
== max_outstanding_io
)
695 * toi_bio_read_page_with_readahead: Read a disk page with readahead.
697 * Read a page from disk, submitting readahead and cleaning up finished i/o
698 * while we wait for the page we're after.
700 static int toi_bio_read_page_with_readahead(void)
702 static int last_result
;
705 if (readahead_index
== -1) {
707 readahead_index
= ra_submit_index
= 0;
710 /* Start a new readahead? */
712 /* We failed to submit a read, and have cleaned up
713 * all the readahead previously submitted */
714 if (ra_submit_index
== readahead_index
) {
715 abort_hibernate(TOI_FAILED_IO
, "Failed to submit"
716 " a read and no readahead left.");
723 if (toi_prepare_readahead(ra_submit_index
))
726 last_result
= toi_bio_rw_page(READ
,
727 toi_ra_pages
[ra_submit_index
],
731 printk("Begin read chunk for page %d returned %d.\n",
732 ra_submit_index
, last_result
);
733 toi_cleanup_readahead(ra_submit_index
);
739 if (ra_submit_index
== max_outstanding_io
)
742 } while((!last_result
) && (ra_submit_index
!= readahead_index
) &&
743 (!toi_readahead_ready(readahead_index
)));
746 toi_wait_on_readahead(readahead_index
);
748 virt
= kmap_atomic(toi_ra_pages
[readahead_index
], KM_USER1
);
749 memcpy(toi_writer_buffer
, virt
, PAGE_SIZE
);
750 kunmap_atomic(virt
, KM_USER1
);
752 toi_cleanup_readahead(readahead_index
);
755 if (readahead_index
== max_outstanding_io
)
762 * toi_rw_buffer: Combine smaller buffers into PAGE_SIZE I/O.
764 * @writing: Bool - whether writing (or reading).
765 * @buffer: The start of the buffer to write or fill.
766 * @buffer_size: The size of the buffer to write or fill.
768 static int toi_rw_buffer(int writing
, char *buffer
, int buffer_size
)
770 int bytes_left
= buffer_size
;
773 char *source_start
= buffer
+ buffer_size
- bytes_left
;
774 char *dest_start
= toi_writer_buffer
+ toi_writer_buffer_posn
;
775 int capacity
= PAGE_SIZE
- toi_writer_buffer_posn
;
776 char *to
= writing
? dest_start
: source_start
;
777 char *from
= writing
? source_start
: dest_start
;
779 if (bytes_left
<= capacity
) {
780 memcpy(to
, from
, bytes_left
);
781 toi_writer_buffer_posn
+= bytes_left
;
782 toi_header_bytes_used
+= bytes_left
;
786 /* Complete this page and start a new one */
787 memcpy(to
, from
, capacity
);
788 bytes_left
-= capacity
;
789 toi_header_bytes_used
+= capacity
;
792 if (test_toi_state(TOI_TRY_RESUME_RD
))
793 sys_read(toi_read_fd
,
794 toi_writer_buffer
, BLOCK_SIZE
);
796 if (toi_bio_read_page_with_readahead())
798 } else if (toi_bio_rw_page(WRITE
,
799 virt_to_page(toi_writer_buffer
),
803 toi_writer_buffer_posn
= 0;
804 toi_cond_pause(0, NULL
);
811 * toi_bio_read_page - read a page of the image.
813 * @pfn: The pfn where the data belongs.
814 * @buffer_page: The page containing the (possibly compressed) data.
815 * @buf_size: The number of bytes on @buffer_page used.
817 * Read a (possibly compressed) page from the image, into buffer_page,
818 * returning its pfn and the buffer size.
820 static int toi_bio_read_page(unsigned long *pfn
, struct page
*buffer_page
,
821 unsigned int *buf_size
)
824 char *buffer_virt
= kmap(buffer_page
);
828 while (!mutex_trylock(&toi_bio_mutex
))
831 if (toi_rw_buffer(READ
, (char *) pfn
, sizeof(unsigned long)) ||
832 toi_rw_buffer(READ
, (char *) buf_size
, sizeof(int)) ||
833 toi_rw_buffer(READ
, buffer_virt
, *buf_size
)) {
834 abort_hibernate(TOI_FAILED_IO
, "Read of data failed.");
837 PR_DEBUG("%d: PFN %ld, %d bytes.\n", pr_index
, *pfn
, *buf_size
);
839 mutex_unlock(&toi_bio_mutex
);
845 * toi_bio_write_page - Write a page of the image.
847 * @pfn: The pfn where the data belongs.
848 * @buffer_page: The page containing the (possibly compressed) data.
849 * @buf_size: The number of bytes on @buffer_page used.
851 * Write a (possibly compressed) page to the image from the buffer, together
852 * with it's index and buffer size.
854 static int toi_bio_write_page(unsigned long pfn
, struct page
*buffer_page
,
855 unsigned int buf_size
)
857 char *buffer_virt
= kmap(buffer_page
);
862 while (!mutex_trylock(&toi_bio_mutex
))
865 if (toi_rw_buffer(WRITE
, (char *) &pfn
, sizeof(unsigned long)) ||
866 toi_rw_buffer(WRITE
, (char *) &buf_size
, sizeof(int)) ||
867 toi_rw_buffer(WRITE
, buffer_virt
, buf_size
))
870 PR_DEBUG("%d: Index %ld, %d bytes. Result %d.\n", pr_index
, pfn
,
873 mutex_unlock(&toi_bio_mutex
);
879 * toi_rw_header_chunk: Read or write a portion of the image header.
881 * @writing: Whether reading or writing.
882 * @owner: The module for which we're writing. Used for confirming that modules
883 * don't use more header space than they asked for.
884 * @buffer: Address of the data to write.
885 * @buffer_size: Size of the data buffer.
887 static int toi_rw_header_chunk(int writing
,
888 struct toi_module_ops
*owner
,
889 char *buffer
, int buffer_size
)
892 owner
->header_used
+= buffer_size
;
893 if (owner
->header_used
> owner
->header_requested
) {
894 printk(KERN_EMERG
"TuxOnIce module %s is using more"
895 "header space (%u) than it requested (%u).\n",
898 owner
->header_requested
);
903 return toi_rw_buffer(writing
, buffer
, buffer_size
);
907 * write_header_chunk_finish: Flush any buffered header data.
909 static int write_header_chunk_finish(void)
911 if (!toi_writer_buffer_posn
)
914 return toi_bio_rw_page(WRITE
, virt_to_page(toi_writer_buffer
),
919 * toi_bio_storage_needed: Get the amount of storage needed for my fns.
921 static int toi_bio_storage_needed(void)
923 return 2 * sizeof(int);
927 * toi_bio_save_config_info: Save block i/o config to image header.
929 * @buf: PAGE_SIZE'd buffer into which data should be saved.
931 static int toi_bio_save_config_info(char *buf
)
933 int *ints
= (int *) buf
;
934 ints
[0] = max_outstanding_io
;
935 ints
[1] = submit_batch_size
;
936 return 2 * sizeof(int);
940 * toi_bio_load_config_info: Restore block i/o config.
942 * @buf: Data to be reloaded.
943 * @size: Size of the buffer saved.
945 static void toi_bio_load_config_info(char *buf
, int size
)
947 int *ints
= (int *) buf
;
948 max_outstanding_io
= ints
[0];
949 submit_batch_size
= ints
[1];
953 * toi_bio_initialise: Initialise bio code at start of some action.
955 * @starting_cycle: Whether starting a hibernation cycle, or just reading or
956 * writing a sysfs value.
958 static int toi_bio_initialise(int starting_cycle
)
960 toi_writer_buffer
= (char *) get_zeroed_page(TOI_ATOMIC_GFP
);
962 return toi_writer_buffer
? 0 : -ENOMEM
;
966 * toi_bio_cleanup: Cleanup after some action.
968 * @finishing_cycle: Whether completing a cycle.
970 static void toi_bio_cleanup(int finishing_cycle
)
972 if (toi_writer_buffer
) {
973 free_page((unsigned long) toi_writer_buffer
);
974 toi_writer_buffer
= NULL
;
978 struct toi_bio_ops toi_bio_ops
= {
979 .bdev_page_io
= toi_bdev_page_io
,
980 .finish_all_io
= toi_finish_all_io
,
981 .forward_one_page
= go_next_page
,
982 .set_extra_page_forward
= set_extra_page_forward
,
983 .set_devinfo
= toi_set_devinfo
,
984 .read_page
= toi_bio_read_page
,
985 .write_page
= toi_bio_write_page
,
986 .rw_init
= toi_rw_init
,
987 .rw_cleanup
= toi_rw_cleanup
,
988 .read_header_init
= toi_read_header_init
,
989 .rw_header_chunk
= toi_rw_header_chunk
,
990 .write_header_chunk_finish
= write_header_chunk_finish
,
993 static struct toi_sysfs_data sysfs_params
[] = {
994 { TOI_ATTR("max_outstanding_io", SYSFS_RW
),
995 SYSFS_INT(&max_outstanding_io
, 16, MAX_OUTSTANDING_IO
, 0),
998 { TOI_ATTR("submit_batch_size", SYSFS_RW
),
999 SYSFS_INT(&submit_batch_size
, 16, SUBMIT_BATCH_SIZE
, 0),
1003 static struct toi_module_ops toi_blockwriter_ops
=
1005 .name
= "lowlevel i/o",
1006 .type
= MISC_HIDDEN_MODULE
,
1007 .directory
= "block_io",
1008 .module
= THIS_MODULE
,
1009 .memory_needed
= toi_bio_memory_needed
,
1010 .storage_needed
= toi_bio_storage_needed
,
1011 .save_config_info
= toi_bio_save_config_info
,
1012 .load_config_info
= toi_bio_load_config_info
,
1013 .initialise
= toi_bio_initialise
,
1014 .cleanup
= toi_bio_cleanup
,
1016 .sysfs_data
= sysfs_params
,
1017 .num_sysfs_entries
= sizeof(sysfs_params
) / sizeof(struct toi_sysfs_data
),
1021 * toi_block_io_load: Load time routine for block i/o module.
1023 * Register block i/o ops and sysfs entries.
1025 static __init
int toi_block_io_load(void)
1027 return toi_register_module(&toi_blockwriter_ops
);
1030 #ifdef CONFIG_TOI_FILE_EXPORTS
1031 EXPORT_SYMBOL_GPL(toi_read_fd
);
1033 #if defined(CONFIG_TOI_FILE_EXPORTS) || defined(CONFIG_TOI_SWAP_EXPORTS)
1034 EXPORT_SYMBOL_GPL(toi_writer_posn
);
1035 EXPORT_SYMBOL_GPL(toi_writer_posn_save
);
1036 EXPORT_SYMBOL_GPL(toi_writer_buffer
);
1037 EXPORT_SYMBOL_GPL(toi_writer_buffer_posn
);
1038 EXPORT_SYMBOL_GPL(toi_header_bytes_used
);
1039 EXPORT_SYMBOL_GPL(toi_bio_ops
);
1042 static __exit
void toi_block_io_unload(void)
1044 toi_unregister_module(&toi_blockwriter_ops
);
1047 module_init(toi_block_io_load
);
1048 module_exit(toi_block_io_unload
);
1049 MODULE_LICENSE("GPL");
1050 MODULE_AUTHOR("Nigel Cunningham");
1051 MODULE_DESCRIPTION("TuxOnIce block io functions");
1053 late_initcall(toi_block_io_load
);