ll_rw_blk.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:41k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *  linux/drivers/block/ll_rw_blk.c
  3.  *
  4.  * Copyright (C) 1991, 1992 Linus Torvalds
  5.  * Copyright (C) 1994,      Karl Keyte: Added support for disk statistics
  6.  * Elevator latency, (C) 2000  Andrea Arcangeli <andrea@suse.de> SuSE
  7.  * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
  8.  * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au> -  July2000
  9.  */
  10. /*
  11.  * This handles all read/write requests to block devices
  12.  */
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/kernel_stat.h>
  16. #include <linux/errno.h>
  17. #include <linux/string.h>
  18. #include <linux/config.h>
  19. #include <linux/locks.h>
  20. #include <linux/mm.h>
  21. #include <linux/swap.h>
  22. #include <linux/init.h>
  23. #include <linux/smp_lock.h>
  24. #include <linux/completion.h>
  25. #include <linux/bootmem.h>
  26. #include <asm/system.h>
  27. #include <asm/io.h>
  28. #include <linux/blk.h>
  29. #include <linux/highmem.h>
  30. #include <linux/slab.h>
  31. #include <linux/module.h>
  32. /*
  33.  * MAC Floppy IWM hooks
  34.  */
  35. #ifdef CONFIG_MAC_FLOPPY_IWM
  36. extern int mac_floppy_init(void);
  37. #endif
  38. /*
  39.  * For the allocated request tables
  40.  */
  41. static kmem_cache_t *request_cachep;
  42. /*
  43.  * The "disk" task queue is used to start the actual requests
  44.  * after a plug
  45.  */
  46. DECLARE_TASK_QUEUE(tq_disk);
  47. /*
  48.  * Protect the request list against multiple users..
  49.  *
  50.  * With this spinlock the Linux block IO subsystem is 100% SMP threaded
  51.  * from the IRQ event side, and almost 100% SMP threaded from the syscall
  52.  * side (we still have protect against block device array operations, and
  53.  * the do_request() side is casually still unsafe. The kernel lock protects
  54.  * this part currently.).
  55.  *
  56.  * there is a fair chance that things will work just OK if these functions
  57.  * are called with no global kernel lock held ...
  58.  */
  59. spinlock_t io_request_lock = SPIN_LOCK_UNLOCKED;
  60. /* This specifies how many sectors to read ahead on the disk. */
  61. int read_ahead[MAX_BLKDEV];
  62. /* blk_dev_struct is:
  63.  * *request_fn
  64.  * *current_request
  65.  */
  66. struct blk_dev_struct blk_dev[MAX_BLKDEV]; /* initialized by blk_dev_init() */
  67. /*
  68.  * blk_size contains the size of all block-devices in units of 1024 byte
  69.  * sectors:
  70.  *
  71.  * blk_size[MAJOR][MINOR]
  72.  *
  73.  * if (!blk_size[MAJOR]) then no minor size checking is done.
  74.  */
  75. int * blk_size[MAX_BLKDEV];
  76. /*
  77.  * blksize_size contains the size of all block-devices:
  78.  *
  79.  * blksize_size[MAJOR][MINOR]
  80.  *
  81.  * if (!blksize_size[MAJOR]) then 1024 bytes is assumed.
  82.  */
  83. int * blksize_size[MAX_BLKDEV];
  84. /*
  85.  * hardsect_size contains the size of the hardware sector of a device.
  86.  *
  87.  * hardsect_size[MAJOR][MINOR]
  88.  *
  89.  * if (!hardsect_size[MAJOR])
  90.  * then 512 bytes is assumed.
  91.  * else
  92.  * sector_size is hardsect_size[MAJOR][MINOR]
  93.  * This is currently set by some scsi devices and read by the msdos fs driver.
  94.  * Other uses may appear later.
  95.  */
  96. int * hardsect_size[MAX_BLKDEV];
  97. /*
  98.  * The following tunes the read-ahead algorithm in mm/filemap.c
  99.  */
  100. int * max_readahead[MAX_BLKDEV];
  101. /*
  102.  * Max number of sectors per request
  103.  */
  104. int * max_sectors[MAX_BLKDEV];
  105. unsigned long blk_max_low_pfn, blk_max_pfn;
  106. int blk_nohighio = 0;
  107. static inline int get_max_sectors(kdev_t dev)
  108. {
  109. if (!max_sectors[MAJOR(dev)])
  110. return MAX_SECTORS;
  111. return max_sectors[MAJOR(dev)][MINOR(dev)];
  112. }
  113. inline request_queue_t *blk_get_queue(kdev_t dev)
  114. {
  115. struct blk_dev_struct *bdev = blk_dev + MAJOR(dev);
  116. if (bdev->queue)
  117. return bdev->queue(dev);
  118. else
  119. return &blk_dev[MAJOR(dev)].request_queue;
  120. }
  121. static int __blk_cleanup_queue(struct request_list *list)
  122. {
  123. struct list_head *head = &list->free;
  124. struct request *rq;
  125. int i = 0;
  126. while (!list_empty(head)) {
  127. rq = list_entry(head->next, struct request, queue);
  128. list_del(&rq->queue);
  129. kmem_cache_free(request_cachep, rq);
  130. i++;
  131. };
  132. if (i != list->count)
  133. printk("request list leak!n");
  134. list->count = 0;
  135. return i;
  136. }
  137. /**
  138.  * blk_cleanup_queue: - release a &request_queue_t when it is no longer needed
  139.  * @q:    the request queue to be released
  140.  *
  141.  * Description:
  142.  *     blk_cleanup_queue is the pair to blk_init_queue().  It should
  143.  *     be called when a request queue is being released; typically
  144.  *     when a block device is being de-registered.  Currently, its
  145.  *     primary task it to free all the &struct request structures that
  146.  *     were allocated to the queue.
  147.  * Caveat: 
  148.  *     Hopefully the low level driver will have finished any
  149.  *     outstanding requests first...
  150.  **/
  151. void blk_cleanup_queue(request_queue_t * q)
  152. {
  153. int count = q->nr_requests;
  154. count -= __blk_cleanup_queue(&q->rq[READ]);
  155. count -= __blk_cleanup_queue(&q->rq[WRITE]);
  156. if (count)
  157. printk("blk_cleanup_queue: leaked requests (%d)n", count);
  158. memset(q, 0, sizeof(*q));
  159. }
  160. /**
  161.  * blk_queue_headactive - indicate whether head of request queue may be active
  162.  * @q:       The queue which this applies to.
  163.  * @active:  A flag indication where the head of the queue is active.
  164.  *
  165.  * Description:
  166.  *    The driver for a block device may choose to leave the currently active
  167.  *    request on the request queue, removing it only when it has completed.
  168.  *    The queue handling routines assume this by default for safety reasons
  169.  *    and will not involve the head of the request queue in any merging or
  170.  *    reordering of requests when the queue is unplugged (and thus may be
  171.  *    working on this particular request).
  172.  *
  173.  *    If a driver removes requests from the queue before processing them, then
  174.  *    it may indicate that it does so, there by allowing the head of the queue
  175.  *    to be involved in merging and reordering.  This is done be calling
  176.  *    blk_queue_headactive() with an @active flag of %0.
  177.  *
  178.  *    If a driver processes several requests at once, it must remove them (or
  179.  *    at least all but one of them) from the request queue.
  180.  *
  181.  *    When a queue is plugged the head will be assumed to be inactive.
  182.  **/
  183.  
  184. void blk_queue_headactive(request_queue_t * q, int active)
  185. {
  186. q->head_active = active;
  187. }
  188. /**
  189.  * blk_queue_make_request - define an alternate make_request function for a device
  190.  * @q:  the request queue for the device to be affected
  191.  * @mfn: the alternate make_request function
  192.  *
  193.  * Description:
  194.  *    The normal way for &struct buffer_heads to be passed to a device
  195.  *    driver is for them to be collected into requests on a request
  196.  *    queue, and then to allow the device driver to select requests
  197.  *    off that queue when it is ready.  This works well for many block
  198.  *    devices. However some block devices (typically virtual devices
  199.  *    such as md or lvm) do not benefit from the processing on the
  200.  *    request queue, and are served best by having the requests passed
  201.  *    directly to them.  This can be achieved by providing a function
  202.  *    to blk_queue_make_request().
  203.  *
  204.  * Caveat:
  205.  *    The driver that does this *must* be able to deal appropriately
  206.  *    with buffers in "highmemory", either by calling bh_kmap() to get
  207.  *    a kernel mapping, to by calling create_bounce() to create a
  208.  *    buffer in normal memory.
  209.  **/
  210. void blk_queue_make_request(request_queue_t * q, make_request_fn * mfn)
  211. {
  212. q->make_request_fn = mfn;
  213. }
  214. /**
  215.  * blk_queue_bounce_limit - set bounce buffer limit for queue
  216.  * @q:  the request queue for the device
  217.  * @dma_addr:   bus address limit
  218.  *
  219.  * Description:
  220.  *    Different hardware can have different requirements as to what pages
  221.  *    it can do I/O directly to. A low level driver can call
  222.  *    blk_queue_bounce_limit to have lower memory pages allocated as bounce
  223.  *    buffers for doing I/O to pages residing above @page. By default
  224.  *    the block layer sets this to the highest numbered "low" memory page.
  225.  **/
  226. void blk_queue_bounce_limit(request_queue_t *q, u64 dma_addr)
  227. {
  228. unsigned long bounce_pfn = dma_addr >> PAGE_SHIFT;
  229. unsigned long mb = dma_addr >> 20;
  230. static request_queue_t *old_q;
  231. /*
  232.  * keep this for debugging for now...
  233.  */
  234. if (dma_addr != BLK_BOUNCE_HIGH && q != old_q) {
  235. old_q = q;
  236. printk("blk: queue %p, ", q);
  237. if (dma_addr == BLK_BOUNCE_ANY)
  238. printk("no I/O memory limitn");
  239. else
  240. printk("I/O limit %luMb (mask 0x%Lx)n", mb,
  241.        (long long) dma_addr);
  242. }
  243. q->bounce_pfn = bounce_pfn;
  244. }
  245. /*
  246.  * can we merge the two segments, or do we need to start a new one?
  247.  */
  248. inline int blk_seg_merge_ok(struct buffer_head *bh, struct buffer_head *nxt)
  249. {
  250. /*
  251.  * if bh and nxt are contigous and don't cross a 4g boundary, it's ok
  252.  */
  253. if (BH_CONTIG(bh, nxt) && BH_PHYS_4G(bh, nxt))
  254. return 1;
  255. return 0;
  256. }
  257. static inline int ll_new_segment(request_queue_t *q, struct request *req, int max_segments)
  258. {
  259. if (req->nr_segments < max_segments) {
  260. req->nr_segments++;
  261. return 1;
  262. }
  263. return 0;
  264. }
  265. static int ll_back_merge_fn(request_queue_t *q, struct request *req, 
  266.     struct buffer_head *bh, int max_segments)
  267. {
  268. if (blk_seg_merge_ok(req->bhtail, bh))
  269. return 1;
  270. return ll_new_segment(q, req, max_segments);
  271. }
  272. static int ll_front_merge_fn(request_queue_t *q, struct request *req, 
  273.      struct buffer_head *bh, int max_segments)
  274. {
  275. if (blk_seg_merge_ok(bh, req->bh))
  276. return 1;
  277. return ll_new_segment(q, req, max_segments);
  278. }
  279. static int ll_merge_requests_fn(request_queue_t *q, struct request *req,
  280. struct request *next, int max_segments)
  281. {
  282. int total_segments = req->nr_segments + next->nr_segments;
  283. if (blk_seg_merge_ok(req->bhtail, next->bh))
  284. total_segments--;
  285. if (total_segments > max_segments)
  286. return 0;
  287. req->nr_segments = total_segments;
  288. return 1;
  289. }
  290. /*
  291.  * "plug" the device if there are no outstanding requests: this will
  292.  * force the transfer to start only after we have put all the requests
  293.  * on the list.
  294.  *
  295.  * This is called with interrupts off and no requests on the queue.
  296.  * (and with the request spinlock acquired)
  297.  */
  298. static void generic_plug_device(request_queue_t *q, kdev_t dev)
  299. {
  300. /*
  301.  * no need to replug device
  302.  */
  303. if (!list_empty(&q->queue_head) || q->plugged)
  304. return;
  305. q->plugged = 1;
  306. queue_task(&q->plug_tq, &tq_disk);
  307. }
  308. /*
  309.  * remove the plug and let it rip..
  310.  */
  311. static inline void __generic_unplug_device(request_queue_t *q)
  312. {
  313. if (q->plugged) {
  314. q->plugged = 0;
  315. if (!list_empty(&q->queue_head))
  316. q->request_fn(q);
  317. }
  318. }
  319. void generic_unplug_device(void *data)
  320. {
  321. request_queue_t *q = (request_queue_t *) data;
  322. unsigned long flags;
  323. spin_lock_irqsave(&io_request_lock, flags);
  324. __generic_unplug_device(q);
  325. spin_unlock_irqrestore(&io_request_lock, flags);
  326. }
  327. /** blk_grow_request_list
  328.  *  @q: The &request_queue_t
  329.  *  @nr_requests: how many requests are desired
  330.  *
  331.  * More free requests are added to the queue's free lists, bringing
  332.  * the total number of requests to @nr_requests.
  333.  *
  334.  * The requests are added equally to the request queue's read
  335.  * and write freelists.
  336.  *
  337.  * This function can sleep.
  338.  *
  339.  * Returns the (new) number of requests which the queue has available.
  340.  */
  341. int blk_grow_request_list(request_queue_t *q, int nr_requests)
  342. {
  343. unsigned long flags;
  344. /* Several broken drivers assume that this function doesn't sleep,
  345.  * this causes system hangs during boot.
  346.  * As a temporary fix, make the the function non-blocking.
  347.  */
  348. spin_lock_irqsave(&io_request_lock, flags);
  349. while (q->nr_requests < nr_requests) {
  350. struct request *rq;
  351. int rw;
  352. rq = kmem_cache_alloc(request_cachep, SLAB_ATOMIC);
  353. if (rq == NULL)
  354. break;
  355. memset(rq, 0, sizeof(*rq));
  356. rq->rq_status = RQ_INACTIVE;
  357. rw = q->nr_requests & 1;
  358. list_add(&rq->queue, &q->rq[rw].free);
  359. q->rq[rw].count++;
  360. q->nr_requests++;
  361. }
  362. q->batch_requests = q->nr_requests / 4;
  363. if (q->batch_requests > 32)
  364. q->batch_requests = 32;
  365. spin_unlock_irqrestore(&io_request_lock, flags);
  366. return q->nr_requests;
  367. }
  368. static void blk_init_free_list(request_queue_t *q)
  369. {
  370. struct sysinfo si;
  371. int megs; /* Total memory, in megabytes */
  372. int nr_requests;
  373. INIT_LIST_HEAD(&q->rq[READ].free);
  374. INIT_LIST_HEAD(&q->rq[WRITE].free);
  375. q->rq[READ].count = 0;
  376. q->rq[WRITE].count = 0;
  377. q->nr_requests = 0;
  378. si_meminfo(&si);
  379. megs = si.totalram >> (20 - PAGE_SHIFT);
  380. nr_requests = 128;
  381. if (megs < 32)
  382. nr_requests /= 2;
  383. blk_grow_request_list(q, nr_requests);
  384. init_waitqueue_head(&q->wait_for_requests[0]);
  385. init_waitqueue_head(&q->wait_for_requests[1]);
  386. spin_lock_init(&q->queue_lock);
  387. }
  388. static int __make_request(request_queue_t * q, int rw, struct buffer_head * bh);
  389. /**
  390.  * blk_init_queue  - prepare a request queue for use with a block device
  391.  * @q:    The &request_queue_t to be initialised
  392.  * @rfn:  The function to be called to process requests that have been
  393.  *        placed on the queue.
  394.  *
  395.  * Description:
  396.  *    If a block device wishes to use the standard request handling procedures,
  397.  *    which sorts requests and coalesces adjacent requests, then it must
  398.  *    call blk_init_queue().  The function @rfn will be called when there
  399.  *    are requests on the queue that need to be processed.  If the device
  400.  *    supports plugging, then @rfn may not be called immediately when requests
  401.  *    are available on the queue, but may be called at some time later instead.
  402.  *    Plugged queues are generally unplugged when a buffer belonging to one
  403.  *    of the requests on the queue is needed, or due to memory pressure.
  404.  *
  405.  *    @rfn is not required, or even expected, to remove all requests off the
  406.  *    queue, but only as many as it can handle at a time.  If it does leave
  407.  *    requests on the queue, it is responsible for arranging that the requests
  408.  *    get dealt with eventually.
  409.  *
  410.  *    A global spin lock $io_request_lock must be held while manipulating the
  411.  *    requests on the request queue.
  412.  *
  413.  *    The request on the head of the queue is by default assumed to be
  414.  *    potentially active, and it is not considered for re-ordering or merging
  415.  *    whenever the given queue is unplugged. This behaviour can be changed with
  416.  *    blk_queue_headactive().
  417.  *
  418.  * Note:
  419.  *    blk_init_queue() must be paired with a blk_cleanup_queue() call
  420.  *    when the block device is deactivated (such as at module unload).
  421.  **/
  422. void blk_init_queue(request_queue_t * q, request_fn_proc * rfn)
  423. {
  424. INIT_LIST_HEAD(&q->queue_head);
  425. elevator_init(&q->elevator, ELEVATOR_LINUS);
  426. blk_init_free_list(q);
  427. q->request_fn      = rfn;
  428. q->back_merge_fn        = ll_back_merge_fn;
  429. q->front_merge_fn       = ll_front_merge_fn;
  430. q->merge_requests_fn = ll_merge_requests_fn;
  431. q->make_request_fn = __make_request;
  432. q->plug_tq.sync = 0;
  433. q->plug_tq.routine = &generic_unplug_device;
  434. q->plug_tq.data = q;
  435. q->plugged         = 0;
  436. /*
  437.  * These booleans describe the queue properties.  We set the
  438.  * default (and most common) values here.  Other drivers can
  439.  * use the appropriate functions to alter the queue properties.
  440.  * as appropriate.
  441.  */
  442. q->plug_device_fn  = generic_plug_device;
  443. q->head_active     = 1;
  444. blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
  445. }
  446. #define blkdev_free_rq(list) list_entry((list)->next, struct request, queue);
  447. /*
  448.  * Get a free request. io_request_lock must be held and interrupts
  449.  * disabled on the way in.  Returns NULL if there are no free requests.
  450.  */
  451. static struct request *get_request(request_queue_t *q, int rw)
  452. {
  453. struct request *rq = NULL;
  454. struct request_list *rl = q->rq + rw;
  455. if (!list_empty(&rl->free)) {
  456. rq = blkdev_free_rq(&rl->free);
  457. list_del(&rq->queue);
  458. rl->count--;
  459. rq->rq_status = RQ_ACTIVE;
  460. rq->cmd = rw;
  461. rq->special = NULL;
  462. rq->q = q;
  463. }
  464. return rq;
  465. }
  466. /*
  467.  * Here's the request allocation design:
  468.  *
  469.  * 1: Blocking on request exhaustion is a key part of I/O throttling.
  470.  * 
  471.  * 2: We want to be `fair' to all requesters.  We must avoid starvation, and
  472.  *    attempt to ensure that all requesters sleep for a similar duration.  Hence
  473.  *    no stealing requests when there are other processes waiting.
  474.  * 
  475.  * 3: We also wish to support `batching' of requests.  So when a process is
  476.  *    woken, we want to allow it to allocate a decent number of requests
  477.  *    before it blocks again, so they can be nicely merged (this only really
  478.  *    matters if the process happens to be adding requests near the head of
  479.  *    the queue).
  480.  * 
  481.  * 4: We want to avoid scheduling storms.  This isn't really important, because
  482.  *    the system will be I/O bound anyway.  But it's easy.
  483.  * 
  484.  *    There is tension between requirements 2 and 3.  Once a task has woken,
  485.  *    we don't want to allow it to sleep as soon as it takes its second request.
  486.  *    But we don't want currently-running tasks to steal all the requests
  487.  *    from the sleepers.  We handle this with wakeup hysteresis around
  488.  *    0 .. batch_requests and with the assumption that request taking is much,
  489.  *    much faster than request freeing.
  490.  * 
  491.  * So here's what we do:
  492.  * 
  493.  *    a) A READA requester fails if free_requests < batch_requests
  494.  * 
  495.  *       We don't want READA requests to prevent sleepers from ever
  496.  *       waking.  Note that READA is used extremely rarely - a few
  497.  *       filesystems use it for directory readahead.
  498.  * 
  499.  *  When a process wants a new request:
  500.  * 
  501.  *    b) If free_requests == 0, the requester sleeps in FIFO manner.
  502.  * 
  503.  *    b) If 0 <  free_requests < batch_requests and there are waiters,
  504.  *       we still take a request non-blockingly.  This provides batching.
  505.  *
  506.  *    c) If free_requests >= batch_requests, the caller is immediately
  507.  *       granted a new request.
  508.  * 
  509.  *  When a request is released:
  510.  * 
  511.  *    d) If free_requests < batch_requests, do nothing.
  512.  * 
  513.  *    f) If free_requests >= batch_requests, wake up a single waiter.
  514.  * 
  515.  *   The net effect is that when a process is woken at the batch_requests level,
  516.  *   it will be able to take approximately (batch_requests) requests before
  517.  *   blocking again (at the tail of the queue).
  518.  * 
  519.  *   This all assumes that the rate of taking requests is much, much higher
  520.  *   than the rate of releasing them.  Which is very true.
  521.  *
  522.  * -akpm, Feb 2002.
  523.  */
  524. static struct request *__get_request_wait(request_queue_t *q, int rw)
  525. {
  526. register struct request *rq;
  527. DECLARE_WAITQUEUE(wait, current);
  528. generic_unplug_device(q);
  529. add_wait_queue_exclusive(&q->wait_for_requests[rw], &wait);
  530. do {
  531. set_current_state(TASK_UNINTERRUPTIBLE);
  532. if (q->rq[rw].count == 0)
  533. schedule();
  534. spin_lock_irq(&io_request_lock);
  535. rq = get_request(q, rw);
  536. spin_unlock_irq(&io_request_lock);
  537. } while (rq == NULL);
  538. remove_wait_queue(&q->wait_for_requests[rw], &wait);
  539. current->state = TASK_RUNNING;
  540. return rq;
  541. }
  542. /* RO fail safe mechanism */
  543. static long ro_bits[MAX_BLKDEV][8];
  544. int is_read_only(kdev_t dev)
  545. {
  546. int minor,major;
  547. major = MAJOR(dev);
  548. minor = MINOR(dev);
  549. if (major < 0 || major >= MAX_BLKDEV) return 0;
  550. return ro_bits[major][minor >> 5] & (1 << (minor & 31));
  551. }
  552. void set_device_ro(kdev_t dev,int flag)
  553. {
  554. int minor,major;
  555. major = MAJOR(dev);
  556. minor = MINOR(dev);
  557. if (major < 0 || major >= MAX_BLKDEV) return;
  558. if (flag) ro_bits[major][minor >> 5] |= 1 << (minor & 31);
  559. else ro_bits[major][minor >> 5] &= ~(1 << (minor & 31));
  560. }
  561. inline void drive_stat_acct (kdev_t dev, int rw,
  562. unsigned long nr_sectors, int new_io)
  563. {
  564. unsigned int major = MAJOR(dev);
  565. unsigned int index;
  566. index = disk_index(dev);
  567. if ((index >= DK_MAX_DISK) || (major >= DK_MAX_MAJOR))
  568. return;
  569. kstat.dk_drive[major][index] += new_io;
  570. if (rw == READ) {
  571. kstat.dk_drive_rio[major][index] += new_io;
  572. kstat.dk_drive_rblk[major][index] += nr_sectors;
  573. } else if (rw == WRITE) {
  574. kstat.dk_drive_wio[major][index] += new_io;
  575. kstat.dk_drive_wblk[major][index] += nr_sectors;
  576. } else
  577. printk(KERN_ERR "drive_stat_acct: cmd not R/W?n");
  578. }
  579. #ifdef CONFIG_BLK_STATS
  580. /*
  581.  * Return up to two hd_structs on which to do IO accounting for a given
  582.  * request.
  583.  *
  584.  * On a partitioned device, we want to account both against the partition
  585.  * and against the whole disk.
  586.  */
  587. static void locate_hd_struct(struct request *req, 
  588.      struct hd_struct **hd1,
  589.      struct hd_struct **hd2)
  590. {
  591. struct gendisk *gd;
  592. *hd1 = NULL;
  593. *hd2 = NULL;
  594. gd = get_gendisk(req->rq_dev);
  595. if (gd && gd->part) {
  596. /* Mask out the partition bits: account for the entire disk */
  597. int devnr = MINOR(req->rq_dev) >> gd->minor_shift;
  598. int whole_minor = devnr << gd->minor_shift;
  599. *hd1 = &gd->part[whole_minor];
  600. if (whole_minor != MINOR(req->rq_dev))
  601. *hd2= &gd->part[MINOR(req->rq_dev)];
  602. }
  603. }
  604. /*
  605.  * Round off the performance stats on an hd_struct.
  606.  *
  607.  * The average IO queue length and utilisation statistics are maintained
  608.  * by observing the current state of the queue length and the amount of
  609.  * time it has been in this state for.
  610.  * Normally, that accounting is done on IO completion, but that can result
  611.  * in more than a second's worth of IO being accounted for within any one
  612.  * second, leading to >100% utilisation.  To deal with that, we do a
  613.  * round-off before returning the results when reading /proc/partitions,
  614.  * accounting immediately for all queue usage up to the current jiffies and
  615.  * restarting the counters again.
  616.  */
  617. void disk_round_stats(struct hd_struct *hd)
  618. {
  619. unsigned long now = jiffies;
  620. hd->aveq += (hd->ios_in_flight * (jiffies - hd->last_queue_change));
  621. hd->last_queue_change = now;
  622. if (hd->ios_in_flight)
  623. hd->io_ticks += (now - hd->last_idle_time);
  624. hd->last_idle_time = now;
  625. }
  626. static inline void down_ios(struct hd_struct *hd)
  627. {
  628. disk_round_stats(hd);
  629. --hd->ios_in_flight;
  630. }
  631. static inline void up_ios(struct hd_struct *hd)
  632. {
  633. disk_round_stats(hd);
  634. ++hd->ios_in_flight;
  635. }
  636. static void account_io_start(struct hd_struct *hd, struct request *req,
  637.      int merge, int sectors)
  638. {
  639. switch (req->cmd) {
  640. case READ:
  641. if (merge)
  642. hd->rd_merges++;
  643. hd->rd_sectors += sectors;
  644. break;
  645. case WRITE:
  646. if (merge)
  647. hd->wr_merges++;
  648. hd->wr_sectors += sectors;
  649. break;
  650. }
  651. if (!merge)
  652. up_ios(hd);
  653. }
  654. static void account_io_end(struct hd_struct *hd, struct request *req)
  655. {
  656. unsigned long duration = jiffies - req->start_time;
  657. switch (req->cmd) {
  658. case READ:
  659. hd->rd_ticks += duration;
  660. hd->rd_ios++;
  661. break;
  662. case WRITE:
  663. hd->wr_ticks += duration;
  664. hd->wr_ios++;
  665. break;
  666. }
  667. down_ios(hd);
  668. }
  669. void req_new_io(struct request *req, int merge, int sectors)
  670. {
  671. struct hd_struct *hd1, *hd2;
  672. locate_hd_struct(req, &hd1, &hd2);
  673. if (hd1)
  674. account_io_start(hd1, req, merge, sectors);
  675. if (hd2)
  676. account_io_start(hd2, req, merge, sectors);
  677. }
  678. void req_merged_io(struct request *req)
  679. {
  680. struct hd_struct *hd1, *hd2;
  681. locate_hd_struct(req, &hd1, &hd2);
  682. if (hd1)
  683. down_ios(hd1);
  684. if (hd2)
  685. down_ios(hd2);
  686. }
  687. void req_finished_io(struct request *req)
  688. {
  689. struct hd_struct *hd1, *hd2;
  690. locate_hd_struct(req, &hd1, &hd2);
  691. if (hd1)
  692. account_io_end(hd1, req);
  693. if (hd2)
  694. account_io_end(hd2, req);
  695. }
  696. EXPORT_SYMBOL(req_finished_io);
  697. #endif /* CONFIG_BLK_STATS */
  698. /*
  699.  * add-request adds a request to the linked list.
  700.  * io_request_lock is held and interrupts disabled, as we muck with the
  701.  * request queue list.
  702.  *
  703.  * By this point, req->cmd is always either READ/WRITE, never READA,
  704.  * which is important for drive_stat_acct() above.
  705.  */
  706. static inline void add_request(request_queue_t * q, struct request * req,
  707.        struct list_head *insert_here)
  708. {
  709. drive_stat_acct(req->rq_dev, req->cmd, req->nr_sectors, 1);
  710. if (!q->plugged && q->head_active && insert_here == &q->queue_head) {
  711. spin_unlock_irq(&io_request_lock);
  712. BUG();
  713. }
  714. /*
  715.  * elevator indicated where it wants this request to be
  716.  * inserted at elevator_merge time
  717.  */
  718. list_add(&req->queue, insert_here);
  719. }
  720. /*
  721.  * Must be called with io_request_lock held and interrupts disabled
  722.  */
  723. void blkdev_release_request(struct request *req)
  724. {
  725. request_queue_t *q = req->q;
  726. int rw = req->cmd;
  727. req->rq_status = RQ_INACTIVE;
  728. req->q = NULL;
  729. /*
  730.  * Request may not have originated from ll_rw_blk. if not,
  731.  * assume it has free buffers and check waiters
  732.  */
  733. if (q) {
  734. list_add(&req->queue, &q->rq[rw].free);
  735. if (++q->rq[rw].count >= q->batch_requests &&
  736. waitqueue_active(&q->wait_for_requests[rw]))
  737. wake_up(&q->wait_for_requests[rw]);
  738. }
  739. }
  740. /*
  741.  * Has to be called with the request spinlock acquired
  742.  */
  743. static void attempt_merge(request_queue_t * q,
  744.   struct request *req,
  745.   int max_sectors,
  746.   int max_segments)
  747. {
  748. struct request *next;
  749.   
  750. next = blkdev_next_request(req);
  751. if (req->sector + req->nr_sectors != next->sector)
  752. return;
  753. if (req->cmd != next->cmd
  754.     || req->rq_dev != next->rq_dev
  755.     || req->nr_sectors + next->nr_sectors > max_sectors
  756.     || next->waiting)
  757. return;
  758. /*
  759.  * If we are not allowed to merge these requests, then
  760.  * return.  If we are allowed to merge, then the count
  761.  * will have been updated to the appropriate number,
  762.  * and we shouldn't do it here too.
  763.  */
  764. if (!q->merge_requests_fn(q, req, next, max_segments))
  765. return;
  766. q->elevator.elevator_merge_req_fn(req, next);
  767. req->bhtail->b_reqnext = next->bh;
  768. req->bhtail = next->bhtail;
  769. req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
  770. list_del(&next->queue);
  771. /* One last thing: we have removed a request, so we now have one
  772.    less expected IO to complete for accounting purposes. */
  773. req_merged_io(req);
  774. blkdev_release_request(next);
  775. }
  776. static inline void attempt_back_merge(request_queue_t * q,
  777.       struct request *req,
  778.       int max_sectors,
  779.       int max_segments)
  780. {
  781. if (&req->queue == q->queue_head.prev)
  782. return;
  783. attempt_merge(q, req, max_sectors, max_segments);
  784. }
  785. static inline void attempt_front_merge(request_queue_t * q,
  786.        struct list_head * head,
  787.        struct request *req,
  788.        int max_sectors,
  789.        int max_segments)
  790. {
  791. struct list_head * prev;
  792. prev = req->queue.prev;
  793. if (head == prev)
  794. return;
  795. attempt_merge(q, blkdev_entry_to_request(prev), max_sectors, max_segments);
  796. }
  797. static int __make_request(request_queue_t * q, int rw,
  798.   struct buffer_head * bh)
  799. {
  800. unsigned int sector, count;
  801. int max_segments = MAX_SEGMENTS;
  802. struct request * req, *freereq = NULL;
  803. int rw_ahead, max_sectors, el_ret;
  804. struct list_head *head, *insert_here;
  805. int latency;
  806. elevator_t *elevator = &q->elevator;
  807. count = bh->b_size >> 9;
  808. sector = bh->b_rsector;
  809. rw_ahead = 0; /* normal case; gets changed below for READA */
  810. switch (rw) {
  811. case READA:
  812. #if 0 /* bread() misinterprets failed READA attempts as IO errors on SMP */
  813. rw_ahead = 1;
  814. #endif
  815. rw = READ; /* drop into READ */
  816. case READ:
  817. case WRITE:
  818. latency = elevator_request_latency(elevator, rw);
  819. break;
  820. default:
  821. BUG();
  822. goto end_io;
  823. }
  824. /* We'd better have a real physical mapping!
  825.    Check this bit only if the buffer was dirty and just locked
  826.    down by us so at this point flushpage will block and
  827.    won't clear the mapped bit under us. */
  828. if (!buffer_mapped(bh))
  829. BUG();
  830. /*
  831.  * Temporary solution - in 2.5 this will be done by the lowlevel
  832.  * driver. Create a bounce buffer if the buffer data points into
  833.  * high memory - keep the original buffer otherwise.
  834.  */
  835. bh = blk_queue_bounce(q, rw, bh);
  836. /* look for a free request. */
  837. /*
  838.  * Try to coalesce the new request with old requests
  839.  */
  840. max_sectors = get_max_sectors(bh->b_rdev);
  841. again:
  842. req = NULL;
  843. head = &q->queue_head;
  844. /*
  845.  * Now we acquire the request spinlock, we have to be mega careful
  846.  * not to schedule or do something nonatomic
  847.  */
  848. spin_lock_irq(&io_request_lock);
  849. insert_here = head->prev;
  850. if (list_empty(head)) {
  851. q->plug_device_fn(q, bh->b_rdev); /* is atomic */
  852. goto get_rq;
  853. } else if (q->head_active && !q->plugged)
  854. head = head->next;
  855. el_ret = elevator->elevator_merge_fn(q, &req, head, bh, rw,max_sectors);
  856. switch (el_ret) {
  857. case ELEVATOR_BACK_MERGE:
  858. if (!q->back_merge_fn(q, req, bh, max_segments)) {
  859. insert_here = &req->queue;
  860. break;
  861. }
  862. req->bhtail->b_reqnext = bh;
  863. req->bhtail = bh;
  864. req->nr_sectors = req->hard_nr_sectors += count;
  865. blk_started_io(count);
  866. drive_stat_acct(req->rq_dev, req->cmd, count, 0);
  867. req_new_io(req, 1, count);
  868. attempt_back_merge(q, req, max_sectors, max_segments);
  869. goto out;
  870. case ELEVATOR_FRONT_MERGE:
  871. if (!q->front_merge_fn(q, req, bh, max_segments)) {
  872. insert_here = req->queue.prev;
  873. break;
  874. }
  875. bh->b_reqnext = req->bh;
  876. req->bh = bh;
  877. /*
  878.  * may not be valid, but queues not having bounce
  879.  * enabled for highmem pages must not look at
  880.  * ->buffer anyway
  881.  */
  882. req->buffer = bh->b_data;
  883. req->current_nr_sectors = req->hard_cur_sectors = count;
  884. req->sector = req->hard_sector = sector;
  885. req->nr_sectors = req->hard_nr_sectors += count;
  886. blk_started_io(count);
  887. drive_stat_acct(req->rq_dev, req->cmd, count, 0);
  888. req_new_io(req, 1, count);
  889. attempt_front_merge(q, head, req, max_sectors, max_segments);
  890. goto out;
  891. /*
  892.  * elevator says don't/can't merge. get new request
  893.  */
  894. case ELEVATOR_NO_MERGE:
  895. /*
  896.  * use elevator hints as to where to insert the
  897.  * request. if no hints, just add it to the back
  898.  * of the queue
  899.  */
  900. if (req)
  901. insert_here = &req->queue;
  902. break;
  903. default:
  904. printk("elevator returned crap (%d)n", el_ret);
  905. BUG();
  906. }
  907. get_rq:
  908. if (freereq) {
  909. req = freereq;
  910. freereq = NULL;
  911. } else {
  912. /*
  913.  * See description above __get_request_wait()
  914.  */
  915. if (rw_ahead) {
  916. if (q->rq[rw].count < q->batch_requests) {
  917. spin_unlock_irq(&io_request_lock);
  918. goto end_io;
  919. }
  920. req = get_request(q, rw);
  921. if (req == NULL)
  922. BUG();
  923. } else {
  924. req = get_request(q, rw);
  925. if (req == NULL) {
  926. spin_unlock_irq(&io_request_lock);
  927. freereq = __get_request_wait(q, rw);
  928. goto again;
  929. }
  930. }
  931. }
  932. /* fill up the request-info, and add it to the queue */
  933. req->elevator_sequence = latency;
  934. req->cmd = rw;
  935. req->errors = 0;
  936. req->hard_sector = req->sector = sector;
  937. req->hard_nr_sectors = req->nr_sectors = count;
  938. req->current_nr_sectors = req->hard_cur_sectors = count;
  939. req->nr_segments = 1; /* Always 1 for a new request. */
  940. req->nr_hw_segments = 1; /* Always 1 for a new request. */
  941. req->buffer = bh->b_data;
  942. req->waiting = NULL;
  943. req->bh = bh;
  944. req->bhtail = bh;
  945. req->rq_dev = bh->b_rdev;
  946. req->start_time = jiffies;
  947. req_new_io(req, 0, count);
  948. blk_started_io(count);
  949. add_request(q, req, insert_here);
  950. out:
  951. if (freereq)
  952. blkdev_release_request(freereq);
  953. spin_unlock_irq(&io_request_lock);
  954. return 0;
  955. end_io:
  956. bh->b_end_io(bh, test_bit(BH_Uptodate, &bh->b_state));
  957. return 0;
  958. }
  959. /**
  960.  * generic_make_request: hand a buffer head to it's device driver for I/O
  961.  * @rw:  READ, WRITE, or READA - what sort of I/O is desired.
  962.  * @bh:  The buffer head describing the location in memory and on the device.
  963.  *
  964.  * generic_make_request() is used to make I/O requests of block
  965.  * devices. It is passed a &struct buffer_head and a &rw value.  The
  966.  * %READ and %WRITE options are (hopefully) obvious in meaning.  The
  967.  * %READA value means that a read is required, but that the driver is
  968.  * free to fail the request if, for example, it cannot get needed
  969.  * resources immediately.
  970.  *
  971.  * generic_make_request() does not return any status.  The
  972.  * success/failure status of the request, along with notification of
  973.  * completion, is delivered asynchronously through the bh->b_end_io
  974.  * function described (one day) else where.
  975.  *
  976.  * The caller of generic_make_request must make sure that b_page,
  977.  * b_addr, b_size are set to describe the memory buffer, that b_rdev
  978.  * and b_rsector are set to describe the device address, and the
  979.  * b_end_io and optionally b_private are set to describe how
  980.  * completion notification should be signaled.  BH_Mapped should also
  981.  * be set (to confirm that b_dev and b_blocknr are valid).
  982.  *
  983.  * generic_make_request and the drivers it calls may use b_reqnext,
  984.  * and may change b_rdev and b_rsector.  So the values of these fields
  985.  * should NOT be depended on after the call to generic_make_request.
  986.  * Because of this, the caller should record the device address
  987.  * information in b_dev and b_blocknr.
  988.  *
  989.  * Apart from those fields mentioned above, no other fields, and in
  990.  * particular, no other flags, are changed by generic_make_request or
  991.  * any lower level drivers.
  992.  * */
  993. void generic_make_request (int rw, struct buffer_head * bh)
  994. {
  995. int major = MAJOR(bh->b_rdev);
  996. int minorsize = 0;
  997. request_queue_t *q;
  998. if (!bh->b_end_io)
  999. BUG();
  1000. /* Test device size, when known. */
  1001. if (blk_size[major])
  1002. minorsize = blk_size[major][MINOR(bh->b_rdev)];
  1003. if (minorsize) {
  1004. unsigned long maxsector = (minorsize << 1) + 1;
  1005. unsigned long sector = bh->b_rsector;
  1006. unsigned int count = bh->b_size >> 9;
  1007. if (maxsector < count || maxsector - count < sector) {
  1008. /* Yecch */
  1009. bh->b_state &= (1 << BH_Lock) | (1 << BH_Mapped);
  1010. /* This may well happen - the kernel calls bread()
  1011.    without checking the size of the device, e.g.,
  1012.    when mounting a device. */
  1013. printk(KERN_INFO
  1014.        "attempt to access beyond end of devicen");
  1015. printk(KERN_INFO "%s: rw=%d, want=%ld, limit=%dn",
  1016.        kdevname(bh->b_rdev), rw,
  1017.        (sector + count)>>1, minorsize);
  1018. /* Yecch again */
  1019. bh->b_end_io(bh, 0);
  1020. return;
  1021. }
  1022. }
  1023. /*
  1024.  * Resolve the mapping until finished. (drivers are
  1025.  * still free to implement/resolve their own stacking
  1026.  * by explicitly returning 0)
  1027.  */
  1028. /* NOTE: we don't repeat the blk_size check for each new device.
  1029.  * Stacking drivers are expected to know what they are doing.
  1030.  */
  1031. do {
  1032. q = blk_get_queue(bh->b_rdev);
  1033. if (!q) {
  1034. printk(KERN_ERR
  1035.        "generic_make_request: Trying to access "
  1036.        "nonexistent block-device %s (%ld)n",
  1037.        kdevname(bh->b_rdev), bh->b_rsector);
  1038. buffer_IO_error(bh);
  1039. break;
  1040. }
  1041. } while (q->make_request_fn(q, rw, bh));
  1042. }
  1043. /**
  1044.  * submit_bh: submit a buffer_head to the block device later for I/O
  1045.  * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
  1046.  * @bh: The &struct buffer_head which describes the I/O
  1047.  *
  1048.  * submit_bh() is very similar in purpose to generic_make_request(), and
  1049.  * uses that function to do most of the work.
  1050.  *
  1051.  * The extra functionality provided by submit_bh is to determine
  1052.  * b_rsector from b_blocknr and b_size, and to set b_rdev from b_dev.
  1053.  * This is is appropriate for IO requests that come from the buffer
  1054.  * cache and page cache which (currently) always use aligned blocks.
  1055.  */
  1056. void submit_bh(int rw, struct buffer_head * bh)
  1057. {
  1058. int count = bh->b_size >> 9;
  1059. if (!test_bit(BH_Lock, &bh->b_state))
  1060. BUG();
  1061. set_bit(BH_Req, &bh->b_state);
  1062. set_bit(BH_Launder, &bh->b_state);
  1063. /*
  1064.  * First step, 'identity mapping' - RAID or LVM might
  1065.  * further remap this.
  1066.  */
  1067. bh->b_rdev = bh->b_dev;
  1068. bh->b_rsector = bh->b_blocknr * count;
  1069. generic_make_request(rw, bh);
  1070. switch (rw) {
  1071. case WRITE:
  1072. kstat.pgpgout += count;
  1073. break;
  1074. default:
  1075. kstat.pgpgin += count;
  1076. break;
  1077. }
  1078. }
  1079. /**
  1080.  * ll_rw_block: low-level access to block devices
  1081.  * @rw: whether to %READ or %WRITE or maybe %READA (readahead)
  1082.  * @nr: number of &struct buffer_heads in the array
  1083.  * @bhs: array of pointers to &struct buffer_head
  1084.  *
  1085.  * ll_rw_block() takes an array of pointers to &struct buffer_heads,
  1086.  * and requests an I/O operation on them, either a %READ or a %WRITE.
  1087.  * The third %READA option is described in the documentation for
  1088.  * generic_make_request() which ll_rw_block() calls.
  1089.  *
  1090.  * This function provides extra functionality that is not in
  1091.  * generic_make_request() that is relevant to buffers in the buffer
  1092.  * cache or page cache.  In particular it drops any buffer that it
  1093.  * cannot get a lock on (with the BH_Lock state bit), any buffer that
  1094.  * appears to be clean when doing a write request, and any buffer that
  1095.  * appears to be up-to-date when doing read request.  Further it marks
  1096.  * as clean buffers that are processed for writing (the buffer cache
  1097.  * wont assume that they are actually clean until the buffer gets
  1098.  * unlocked).
  1099.  *
  1100.  * ll_rw_block sets b_end_io to simple completion handler that marks
  1101.  * the buffer up-to-date (if approriate), unlocks the buffer and wakes
  1102.  * any waiters.  As client that needs a more interesting completion
  1103.  * routine should call submit_bh() (or generic_make_request())
  1104.  * directly.
  1105.  *
  1106.  * Caveat:
  1107.  *  All of the buffers must be for the same device, and must also be
  1108.  *  of the current approved size for the device.  */
  1109. void ll_rw_block(int rw, int nr, struct buffer_head * bhs[])
  1110. {
  1111. unsigned int major;
  1112. int correct_size;
  1113. int i;
  1114. if (!nr)
  1115. return;
  1116. major = MAJOR(bhs[0]->b_dev);
  1117. /* Determine correct block size for this device. */
  1118. correct_size = get_hardsect_size(bhs[0]->b_dev);
  1119. /* Verify requested block sizes. */
  1120. for (i = 0; i < nr; i++) {
  1121. struct buffer_head *bh = bhs[i];
  1122. if (bh->b_size % correct_size) {
  1123. printk(KERN_NOTICE "ll_rw_block: device %s: "
  1124.        "only %d-char blocks implemented (%u)n",
  1125.        kdevname(bhs[0]->b_dev),
  1126.        correct_size, bh->b_size);
  1127. goto sorry;
  1128. }
  1129. }
  1130. if ((rw & WRITE) && is_read_only(bhs[0]->b_dev)) {
  1131. printk(KERN_NOTICE "Can't write to read-only device %sn",
  1132.        kdevname(bhs[0]->b_dev));
  1133. goto sorry;
  1134. }
  1135. for (i = 0; i < nr; i++) {
  1136. struct buffer_head *bh = bhs[i];
  1137. /* Only one thread can actually submit the I/O. */
  1138. if (test_and_set_bit(BH_Lock, &bh->b_state))
  1139. continue;
  1140. /* We have the buffer lock */
  1141. atomic_inc(&bh->b_count);
  1142. bh->b_end_io = end_buffer_io_sync;
  1143. switch(rw) {
  1144. case WRITE:
  1145. if (!atomic_set_buffer_clean(bh))
  1146. /* Hmmph! Nothing to write */
  1147. goto end_io;
  1148. __mark_buffer_clean(bh);
  1149. break;
  1150. case READA:
  1151. case READ:
  1152. if (buffer_uptodate(bh))
  1153. /* Hmmph! Already have it */
  1154. goto end_io;
  1155. break;
  1156. default:
  1157. BUG();
  1158. end_io:
  1159. bh->b_end_io(bh, test_bit(BH_Uptodate, &bh->b_state));
  1160. continue;
  1161. }
  1162. submit_bh(rw, bh);
  1163. }
  1164. return;
  1165. sorry:
  1166. /* Make sure we don't get infinite dirty retries.. */
  1167. for (i = 0; i < nr; i++)
  1168. mark_buffer_clean(bhs[i]);
  1169. }
  1170. #ifdef CONFIG_STRAM_SWAP
  1171. extern int stram_device_init (void);
  1172. #endif
  1173. /**
  1174.  * end_that_request_first - end I/O on one buffer.
  1175.  * @req:      the request being processed
  1176.  * @uptodate: 0 for I/O error
  1177.  * @name:     the name printed for an I/O error
  1178.  *
  1179.  * Description:
  1180.  *     Ends I/O on the first buffer attached to @req, and sets it up
  1181.  *     for the next buffer_head (if any) in the cluster.
  1182.  *     
  1183.  * Return:
  1184.  *     0 - we are done with this request, call end_that_request_last()
  1185.  *     1 - still buffers pending for this request
  1186.  *
  1187.  * Caveat: 
  1188.  *     Drivers implementing their own end_request handling must call
  1189.  *     blk_finished_io() appropriately.
  1190.  **/
  1191. int end_that_request_first (struct request *req, int uptodate, char *name)
  1192. {
  1193. struct buffer_head * bh;
  1194. int nsect;
  1195. req->errors = 0;
  1196. if (!uptodate)
  1197. printk("end_request: I/O error, dev %s (%s), sector %lun",
  1198. kdevname(req->rq_dev), name, req->sector);
  1199. if ((bh = req->bh) != NULL) {
  1200. nsect = bh->b_size >> 9;
  1201. blk_finished_io(nsect);
  1202. req->bh = bh->b_reqnext;
  1203. bh->b_reqnext = NULL;
  1204. bh->b_end_io(bh, uptodate);
  1205. if ((bh = req->bh) != NULL) {
  1206. req->hard_sector += nsect;
  1207. req->hard_nr_sectors -= nsect;
  1208. req->sector = req->hard_sector;
  1209. req->nr_sectors = req->hard_nr_sectors;
  1210. req->current_nr_sectors = bh->b_size >> 9;
  1211. req->hard_cur_sectors = req->current_nr_sectors;
  1212. if (req->nr_sectors < req->current_nr_sectors) {
  1213. req->nr_sectors = req->current_nr_sectors;
  1214. printk("end_request: buffer-list destroyedn");
  1215. }
  1216. req->buffer = bh->b_data;
  1217. return 1;
  1218. }
  1219. }
  1220. return 0;
  1221. }
  1222. void end_that_request_last(struct request *req)
  1223. {
  1224. if (req->waiting != NULL)
  1225. complete(req->waiting);
  1226. req_finished_io(req);
  1227. blkdev_release_request(req);
  1228. }
  1229. int __init blk_dev_init(void)
  1230. {
  1231. struct blk_dev_struct *dev;
  1232. request_cachep = kmem_cache_create("blkdev_requests",
  1233.    sizeof(struct request),
  1234.    0, SLAB_HWCACHE_ALIGN, NULL, NULL);
  1235. if (!request_cachep)
  1236. panic("Can't create request pool slab cachen");
  1237. for (dev = blk_dev + MAX_BLKDEV; dev-- != blk_dev;)
  1238. dev->queue = NULL;
  1239. memset(ro_bits,0,sizeof(ro_bits));
  1240. memset(max_readahead, 0, sizeof(max_readahead));
  1241. memset(max_sectors, 0, sizeof(max_sectors));
  1242. blk_max_low_pfn = max_low_pfn - 1;
  1243. blk_max_pfn = max_pfn - 1;
  1244. #ifdef CONFIG_AMIGA_Z2RAM
  1245. z2_init();
  1246. #endif
  1247. #ifdef CONFIG_STRAM_SWAP
  1248. stram_device_init();
  1249. #endif
  1250. #ifdef CONFIG_ISP16_CDI
  1251. isp16_init();
  1252. #endif
  1253. #if defined(CONFIG_IDE) && defined(CONFIG_BLK_DEV_IDE)
  1254. ide_init(); /* this MUST precede hd_init */
  1255. #endif
  1256. #if defined(CONFIG_IDE) && defined(CONFIG_BLK_DEV_HD)
  1257. hd_init();
  1258. #endif
  1259. #ifdef CONFIG_BLK_DEV_PS2
  1260. ps2esdi_init();
  1261. #endif
  1262. #ifdef CONFIG_BLK_DEV_XD
  1263. xd_init();
  1264. #endif
  1265. #ifdef CONFIG_BLK_DEV_MFM
  1266. mfm_init();
  1267. #endif
  1268. #ifdef CONFIG_PARIDE
  1269. { extern void paride_init(void); paride_init(); };
  1270. #endif
  1271. #ifdef CONFIG_MAC_FLOPPY
  1272. swim3_init();
  1273. #endif
  1274. #ifdef CONFIG_BLK_DEV_SWIM_IOP
  1275. swimiop_init();
  1276. #endif
  1277. #ifdef CONFIG_AMIGA_FLOPPY
  1278. amiga_floppy_init();
  1279. #endif
  1280. #ifdef CONFIG_ATARI_FLOPPY
  1281. atari_floppy_init();
  1282. #endif
  1283. #ifdef CONFIG_BLK_DEV_FD
  1284. floppy_init();
  1285. #else
  1286. #if defined(__i386__) /* Do we even need this? */
  1287. outb_p(0xc, 0x3f2);
  1288. #endif
  1289. #endif
  1290. #ifdef CONFIG_CDU31A
  1291. cdu31a_init();
  1292. #endif
  1293. #ifdef CONFIG_ATARI_ACSI
  1294. acsi_init();
  1295. #endif
  1296. #ifdef CONFIG_MCD
  1297. mcd_init();
  1298. #endif
  1299. #ifdef CONFIG_MCDX
  1300. mcdx_init();
  1301. #endif
  1302. #ifdef CONFIG_SBPCD
  1303. sbpcd_init();
  1304. #endif
  1305. #ifdef CONFIG_AZTCD
  1306. aztcd_init();
  1307. #endif
  1308. #ifdef CONFIG_CDU535
  1309. sony535_init();
  1310. #endif
  1311. #ifdef CONFIG_GSCD
  1312. gscd_init();
  1313. #endif
  1314. #ifdef CONFIG_CM206
  1315. cm206_init();
  1316. #endif
  1317. #ifdef CONFIG_OPTCD
  1318. optcd_init();
  1319. #endif
  1320. #ifdef CONFIG_SJCD
  1321. sjcd_init();
  1322. #endif
  1323. #ifdef CONFIG_APBLOCK
  1324. ap_init();
  1325. #endif
  1326. #ifdef CONFIG_DDV
  1327. ddv_init();
  1328. #endif
  1329. #ifdef CONFIG_MDISK
  1330. mdisk_init();
  1331. #endif
  1332. #ifdef CONFIG_DASD
  1333. dasd_init();
  1334. #endif
  1335. #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_BLOCK)
  1336. tapeblock_init();
  1337. #endif
  1338. #ifdef CONFIG_BLK_DEV_XPRAM
  1339.         xpram_init();
  1340. #endif
  1341. #ifdef CONFIG_SUN_JSFLASH
  1342. jsfd_init();
  1343. #endif
  1344. return 0;
  1345. };
  1346. EXPORT_SYMBOL(io_request_lock);
  1347. EXPORT_SYMBOL(end_that_request_first);
  1348. EXPORT_SYMBOL(end_that_request_last);
  1349. EXPORT_SYMBOL(blk_grow_request_list);
  1350. EXPORT_SYMBOL(blk_init_queue);
  1351. EXPORT_SYMBOL(blk_get_queue);
  1352. EXPORT_SYMBOL(blk_cleanup_queue);
  1353. EXPORT_SYMBOL(blk_queue_headactive);
  1354. EXPORT_SYMBOL(blk_queue_make_request);
  1355. EXPORT_SYMBOL(generic_make_request);
  1356. EXPORT_SYMBOL(blkdev_release_request);
  1357. EXPORT_SYMBOL(generic_unplug_device);
  1358. EXPORT_SYMBOL(blk_queue_bounce_limit);
  1359. EXPORT_SYMBOL(blk_max_low_pfn);
  1360. EXPORT_SYMBOL(blk_max_pfn);
  1361. EXPORT_SYMBOL(blk_seg_merge_ok);
  1362. EXPORT_SYMBOL(blk_nohighio);