aoedev.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Copyright (c) 2004 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. struct aoedev *
  13. aoedev_by_aoeaddr(int maj, int min)
  14. {
  15. struct aoedev *d;
  16. ulong flags;
  17. spin_lock_irqsave(&devlist_lock, flags);
  18. for (d=devlist; d; d=d->next)
  19. if (d->aoemajor == maj && d->aoeminor == min)
  20. break;
  21. spin_unlock_irqrestore(&devlist_lock, flags);
  22. return d;
  23. }
  24. /* called with devlist lock held */
  25. static struct aoedev *
  26. aoedev_newdev(ulong nframes)
  27. {
  28. struct aoedev *d;
  29. struct frame *f, *e;
  30. d = kcalloc(1, sizeof *d, GFP_ATOMIC);
  31. if (d == NULL)
  32. return NULL;
  33. f = kcalloc(nframes, sizeof *f, GFP_ATOMIC);
  34. if (f == NULL) {
  35. kfree(d);
  36. return NULL;
  37. }
  38. d->nframes = nframes;
  39. d->frames = f;
  40. e = f + nframes;
  41. for (; f<e; f++)
  42. f->tag = FREETAG;
  43. spin_lock_init(&d->lock);
  44. init_timer(&d->timer);
  45. d->bufpool = NULL; /* defer to aoeblk_gdalloc */
  46. INIT_LIST_HEAD(&d->bufq);
  47. d->next = devlist;
  48. devlist = d;
  49. return d;
  50. }
  51. void
  52. aoedev_downdev(struct aoedev *d)
  53. {
  54. struct frame *f, *e;
  55. struct buf *buf;
  56. struct bio *bio;
  57. d->flags |= DEVFL_TKILL;
  58. del_timer(&d->timer);
  59. f = d->frames;
  60. e = f + d->nframes;
  61. for (; f<e; f->tag = FREETAG, f->buf = NULL, f++) {
  62. if (f->tag == FREETAG || f->buf == NULL)
  63. continue;
  64. buf = f->buf;
  65. bio = buf->bio;
  66. if (--buf->nframesout == 0) {
  67. mempool_free(buf, d->bufpool);
  68. bio_endio(bio, bio->bi_size, -EIO);
  69. }
  70. }
  71. d->inprocess = NULL;
  72. while (!list_empty(&d->bufq)) {
  73. buf = container_of(d->bufq.next, struct buf, bufs);
  74. list_del(d->bufq.next);
  75. bio = buf->bio;
  76. mempool_free(buf, d->bufpool);
  77. bio_endio(bio, bio->bi_size, -EIO);
  78. }
  79. if (d->nopen)
  80. d->flags |= DEVFL_CLOSEWAIT;
  81. if (d->gd)
  82. d->gd->capacity = 0;
  83. d->flags &= ~DEVFL_UP;
  84. }
  85. struct aoedev *
  86. aoedev_set(ulong sysminor, unsigned char *addr, struct net_device *ifp, ulong bufcnt)
  87. {
  88. struct aoedev *d;
  89. ulong flags;
  90. spin_lock_irqsave(&devlist_lock, flags);
  91. for (d=devlist; d; d=d->next)
  92. if (d->sysminor == sysminor
  93. || memcmp(d->addr, addr, sizeof d->addr) == 0)
  94. break;
  95. if (d == NULL && (d = aoedev_newdev(bufcnt)) == NULL) {
  96. spin_unlock_irqrestore(&devlist_lock, flags);
  97. printk(KERN_INFO "aoe: aoedev_set: aoedev_newdev failure.\n");
  98. return NULL;
  99. }
  100. spin_unlock_irqrestore(&devlist_lock, flags);
  101. spin_lock_irqsave(&d->lock, flags);
  102. d->ifp = ifp;
  103. if (d->sysminor != sysminor
  104. || (d->flags & DEVFL_UP) == 0) {
  105. aoedev_downdev(d); /* flushes outstanding frames */
  106. memcpy(d->addr, addr, sizeof d->addr);
  107. d->sysminor = sysminor;
  108. d->aoemajor = AOEMAJOR(sysminor);
  109. d->aoeminor = AOEMINOR(sysminor);
  110. }
  111. spin_unlock_irqrestore(&d->lock, flags);
  112. return d;
  113. }
  114. static void
  115. aoedev_freedev(struct aoedev *d)
  116. {
  117. if (d->gd) {
  118. aoedisk_rm_sysfs(d);
  119. del_gendisk(d->gd);
  120. put_disk(d->gd);
  121. }
  122. kfree(d->frames);
  123. if (d->bufpool)
  124. mempool_destroy(d->bufpool);
  125. kfree(d);
  126. }
  127. void
  128. aoedev_exit(void)
  129. {
  130. struct aoedev *d;
  131. ulong flags;
  132. flush_scheduled_work();
  133. while ((d = devlist)) {
  134. devlist = d->next;
  135. spin_lock_irqsave(&d->lock, flags);
  136. aoedev_downdev(d);
  137. spin_unlock_irqrestore(&d->lock, flags);
  138. del_timer_sync(&d->timer);
  139. aoedev_freedev(d);
  140. }
  141. }
  142. int __init
  143. aoedev_init(void)
  144. {
  145. spin_lock_init(&devlist_lock);
  146. return 0;
  147. }