aoedev.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. }
  109. d->inprocess = NULL;
  110. while (!list_empty(&d->bufq)) {
  111. buf = container_of(d->bufq.next, struct buf, bufs);
  112. list_del(d->bufq.next);
  113. bio = buf->bio;
  114. mempool_free(buf, d->bufpool);
  115. bio_endio(bio, bio->bi_size, -EIO);
  116. }
  117. if (d->gd)
  118. d->gd->capacity = 0;
  119. d->flags &= ~(DEVFL_UP | DEVFL_PAUSE);
  120. }
  121. /* find it or malloc it */
  122. struct aoedev *
  123. aoedev_by_sysminor_m(ulong sysminor, ulong bufcnt)
  124. {
  125. struct aoedev *d;
  126. ulong flags;
  127. spin_lock_irqsave(&devlist_lock, flags);
  128. for (d=devlist; d; d=d->next)
  129. if (d->sysminor == sysminor)
  130. break;
  131. if (d == NULL) {
  132. d = aoedev_newdev(bufcnt);
  133. if (d == NULL) {
  134. spin_unlock_irqrestore(&devlist_lock, flags);
  135. iprintk("aoedev_newdev failure.\n");
  136. return NULL;
  137. }
  138. d->sysminor = sysminor;
  139. d->aoemajor = AOEMAJOR(sysminor);
  140. d->aoeminor = AOEMINOR(sysminor);
  141. }
  142. spin_unlock_irqrestore(&devlist_lock, flags);
  143. return d;
  144. }
  145. static void
  146. aoedev_freedev(struct aoedev *d)
  147. {
  148. struct frame *f, *e;
  149. if (d->gd) {
  150. aoedisk_rm_sysfs(d);
  151. del_gendisk(d->gd);
  152. put_disk(d->gd);
  153. }
  154. f = d->frames;
  155. e = f + d->nframes;
  156. for (; f<e; f++) {
  157. skb_shinfo(f->skb)->nr_frags = 0;
  158. dev_kfree_skb(f->skb);
  159. }
  160. kfree(d->frames);
  161. if (d->bufpool)
  162. mempool_destroy(d->bufpool);
  163. kfree(d);
  164. }
  165. void
  166. aoedev_exit(void)
  167. {
  168. struct aoedev *d;
  169. ulong flags;
  170. flush_scheduled_work();
  171. while ((d = devlist)) {
  172. devlist = d->next;
  173. spin_lock_irqsave(&d->lock, flags);
  174. aoedev_downdev(d);
  175. d->flags |= DEVFL_TKILL;
  176. spin_unlock_irqrestore(&d->lock, flags);
  177. del_timer_sync(&d->timer);
  178. aoedev_freedev(d);
  179. }
  180. }
  181. int __init
  182. aoedev_init(void)
  183. {
  184. spin_lock_init(&devlist_lock);
  185. return 0;
  186. }