aoedev.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* Copyright (c) 2006 Coraid, Inc. See COPYING for GPL terms. */
  2. /*
  3. * aoedev.c
  4. * AoE device utility functions; maintains device list.
  5. */
  6. #include <linux/hdreg.h>
  7. #include <linux/blkdev.h>
  8. #include <linux/netdevice.h>
  9. #include "aoe.h"
  10. static struct aoedev *devlist;
  11. static spinlock_t devlist_lock;
  12. int
  13. aoedev_isbusy(struct aoedev *d)
  14. {
  15. struct frame *f, *e;
  16. f = d->frames;
  17. e = f + d->nframes;
  18. do {
  19. if (f->tag != FREETAG)
  20. return 1;
  21. } while (++f < e);
  22. return 0;
  23. }
  24. struct aoedev *
  25. aoedev_by_aoeaddr(int maj, int min)
  26. {
  27. struct aoedev *d;
  28. ulong flags;
  29. spin_lock_irqsave(&devlist_lock, flags);
  30. for (d=devlist; d; d=d->next)
  31. if (d->aoemajor == maj && d->aoeminor == min)
  32. break;
  33. spin_unlock_irqrestore(&devlist_lock, flags);
  34. return d;
  35. }
  36. static void
  37. dummy_timer(ulong vp)
  38. {
  39. struct aoedev *d;
  40. d = (struct aoedev *)vp;
  41. if (d->flags & DEVFL_TKILL)
  42. return;
  43. d->timer.expires = jiffies + HZ;
  44. add_timer(&d->timer);
  45. }
  46. /* called with devlist lock held */
  47. static struct aoedev *
  48. aoedev_newdev(ulong nframes)
  49. {
  50. struct aoedev *d;
  51. struct frame *f, *e;
  52. d = kzalloc(sizeof *d, GFP_ATOMIC);
  53. f = kcalloc(nframes, sizeof *f, GFP_ATOMIC);
  54. switch (!d || !f) {
  55. case 0:
  56. d->nframes = nframes;
  57. d->frames = f;
  58. e = f + nframes;
  59. for (; f<e; f++) {
  60. f->tag = FREETAG;
  61. f->skb = new_skb(ETH_ZLEN);
  62. if (!f->skb)
  63. break;
  64. }
  65. if (f == e)
  66. break;
  67. while (f > d->frames) {
  68. f--;
  69. dev_kfree_skb(f->skb);
  70. }
  71. default:
  72. if (f)
  73. kfree(f);
  74. if (d)
  75. kfree(d);
  76. return NULL;
  77. }
  78. INIT_WORK(&d->work, aoecmd_sleepwork, d);
  79. spin_lock_init(&d->lock);
  80. init_timer(&d->timer);
  81. d->timer.data = (ulong) d;
  82. d->timer.function = dummy_timer;
  83. d->timer.expires = jiffies + HZ;
  84. add_timer(&d->timer);
  85. d->bufpool = NULL; /* defer to aoeblk_gdalloc */
  86. INIT_LIST_HEAD(&d->bufq);
  87. d->next = devlist;
  88. devlist = d;
  89. return d;
  90. }
  91. void
  92. aoedev_downdev(struct aoedev *d)
  93. {
  94. struct frame *f, *e;
  95. struct buf *buf;
  96. struct bio *bio;
  97. f = d->frames;
  98. e = f + d->nframes;
  99. for (; f<e; f->tag = FREETAG, f->buf = NULL, f++) {
  100. if (f->tag == FREETAG || f->buf == NULL)
  101. continue;
  102. buf = f->buf;
  103. bio = buf->bio;
  104. if (--buf->nframesout == 0) {
  105. mempool_free(buf, d->bufpool);
  106. bio_endio(bio, bio->bi_size, -EIO);
  107. }
  108. skb_shinfo(f->skb)->nr_frags = f->skb->data_len = 0;
  109. }
  110. d->inprocess = NULL;
  111. while (!list_empty(&d->bufq)) {
  112. buf = container_of(d->bufq.next, struct buf, bufs);
  113. list_del(d->bufq.next);
  114. bio = buf->bio;
  115. mempool_free(buf, d->bufpool);
  116. bio_endio(bio, bio->bi_size, -EIO);
  117. }
  118. if (d->gd)
  119. d->gd->capacity = 0;
  120. d->flags &= ~(DEVFL_UP | DEVFL_PAUSE);
  121. }
  122. /* find it or malloc it */
  123. struct aoedev *
  124. aoedev_by_sysminor_m(ulong sysminor, ulong bufcnt)
  125. {
  126. struct aoedev *d;
  127. ulong flags;
  128. spin_lock_irqsave(&devlist_lock, flags);
  129. for (d=devlist; d; d=d->next)
  130. if (d->sysminor == sysminor)
  131. break;
  132. if (d == NULL) {
  133. d = aoedev_newdev(bufcnt);
  134. if (d == NULL) {
  135. spin_unlock_irqrestore(&devlist_lock, flags);
  136. iprintk("aoedev_newdev failure.\n");
  137. return NULL;
  138. }
  139. d->sysminor = sysminor;
  140. d->aoemajor = AOEMAJOR(sysminor);
  141. d->aoeminor = AOEMINOR(sysminor);
  142. }
  143. spin_unlock_irqrestore(&devlist_lock, flags);
  144. return d;
  145. }
  146. static void
  147. aoedev_freedev(struct aoedev *d)
  148. {
  149. struct frame *f, *e;
  150. if (d->gd) {
  151. aoedisk_rm_sysfs(d);
  152. del_gendisk(d->gd);
  153. put_disk(d->gd);
  154. }
  155. f = d->frames;
  156. e = f + d->nframes;
  157. for (; f<e; f++) {
  158. skb_shinfo(f->skb)->nr_frags = 0;
  159. dev_kfree_skb(f->skb);
  160. }
  161. kfree(d->frames);
  162. if (d->bufpool)
  163. mempool_destroy(d->bufpool);
  164. kfree(d);
  165. }
  166. void
  167. aoedev_exit(void)
  168. {
  169. struct aoedev *d;
  170. ulong flags;
  171. flush_scheduled_work();
  172. while ((d = devlist)) {
  173. devlist = d->next;
  174. spin_lock_irqsave(&d->lock, flags);
  175. aoedev_downdev(d);
  176. d->flags |= DEVFL_TKILL;
  177. spin_unlock_irqrestore(&d->lock, flags);
  178. del_timer_sync(&d->timer);
  179. aoedev_freedev(d);
  180. }
  181. }
  182. int __init
  183. aoedev_init(void)
  184. {
  185. spin_lock_init(&devlist_lock);
  186. return 0;
  187. }