aoedev.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /* Copyright (c) 2012 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 <linux/delay.h>
  10. #include <linux/slab.h>
  11. #include <linux/bitmap.h>
  12. #include <linux/kdev_t.h>
  13. #include <linux/moduleparam.h>
  14. #include "aoe.h"
  15. static void dummy_timer(ulong);
  16. static void aoedev_freedev(struct aoedev *);
  17. static void freetgt(struct aoedev *d, struct aoetgt *t);
  18. static void skbpoolfree(struct aoedev *d);
  19. static int aoe_dyndevs = 1;
  20. module_param(aoe_dyndevs, int, 0644);
  21. MODULE_PARM_DESC(aoe_dyndevs, "Use dynamic minor numbers for devices.");
  22. static struct aoedev *devlist;
  23. static DEFINE_SPINLOCK(devlist_lock);
  24. /* Because some systems will have one, many, or no
  25. * - partitions,
  26. * - slots per shelf,
  27. * - or shelves,
  28. * we need some flexibility in the way the minor numbers
  29. * are allocated. So they are dynamic.
  30. */
  31. #define N_DEVS ((1U<<MINORBITS)/AOE_PARTITIONS)
  32. static DEFINE_SPINLOCK(used_minors_lock);
  33. static DECLARE_BITMAP(used_minors, N_DEVS);
  34. static int
  35. minor_get_dyn(ulong *sysminor)
  36. {
  37. ulong flags;
  38. ulong n;
  39. int error = 0;
  40. spin_lock_irqsave(&used_minors_lock, flags);
  41. n = find_first_zero_bit(used_minors, N_DEVS);
  42. if (n < N_DEVS)
  43. set_bit(n, used_minors);
  44. else
  45. error = -1;
  46. spin_unlock_irqrestore(&used_minors_lock, flags);
  47. *sysminor = n * AOE_PARTITIONS;
  48. return error;
  49. }
  50. static int
  51. minor_get_static(ulong *sysminor, ulong aoemaj, int aoemin)
  52. {
  53. ulong flags;
  54. ulong n;
  55. int error = 0;
  56. enum {
  57. /* for backwards compatibility when !aoe_dyndevs,
  58. * a static number of supported slots per shelf */
  59. NPERSHELF = 16,
  60. };
  61. if (aoemin >= NPERSHELF) {
  62. pr_err("aoe: %s %d slots per shelf\n",
  63. "static minor device numbers support only",
  64. NPERSHELF);
  65. error = -1;
  66. goto out;
  67. }
  68. n = aoemaj * NPERSHELF + aoemin;
  69. if (n >= N_DEVS) {
  70. pr_err("aoe: %s with e%ld.%d\n",
  71. "cannot use static minor device numbers",
  72. aoemaj, aoemin);
  73. error = -1;
  74. goto out;
  75. }
  76. spin_lock_irqsave(&used_minors_lock, flags);
  77. if (test_bit(n, used_minors)) {
  78. pr_err("aoe: %s %lu\n",
  79. "existing device already has static minor number",
  80. n);
  81. error = -1;
  82. } else
  83. set_bit(n, used_minors);
  84. spin_unlock_irqrestore(&used_minors_lock, flags);
  85. *sysminor = n;
  86. out:
  87. return error;
  88. }
  89. static int
  90. minor_get(ulong *sysminor, ulong aoemaj, int aoemin)
  91. {
  92. if (aoe_dyndevs)
  93. return minor_get_dyn(sysminor);
  94. else
  95. return minor_get_static(sysminor, aoemaj, aoemin);
  96. }
  97. static void
  98. minor_free(ulong minor)
  99. {
  100. ulong flags;
  101. minor /= AOE_PARTITIONS;
  102. BUG_ON(minor >= N_DEVS);
  103. spin_lock_irqsave(&used_minors_lock, flags);
  104. BUG_ON(!test_bit(minor, used_minors));
  105. clear_bit(minor, used_minors);
  106. spin_unlock_irqrestore(&used_minors_lock, flags);
  107. }
  108. /*
  109. * Users who grab a pointer to the device with aoedev_by_aoeaddr
  110. * automatically get a reference count and must be responsible
  111. * for performing a aoedev_put. With the addition of async
  112. * kthread processing I'm no longer confident that we can
  113. * guarantee consistency in the face of device flushes.
  114. *
  115. * For the time being, we only bother to add extra references for
  116. * frames sitting on the iocq. When the kthreads finish processing
  117. * these frames, they will aoedev_put the device.
  118. */
  119. void
  120. aoedev_put(struct aoedev *d)
  121. {
  122. ulong flags;
  123. spin_lock_irqsave(&devlist_lock, flags);
  124. d->ref--;
  125. spin_unlock_irqrestore(&devlist_lock, flags);
  126. }
  127. static void
  128. dummy_timer(ulong vp)
  129. {
  130. struct aoedev *d;
  131. d = (struct aoedev *)vp;
  132. if (d->flags & DEVFL_TKILL)
  133. return;
  134. d->timer.expires = jiffies + HZ;
  135. add_timer(&d->timer);
  136. }
  137. static void
  138. aoe_failip(struct aoedev *d)
  139. {
  140. struct request *rq;
  141. struct bio *bio;
  142. unsigned long n;
  143. aoe_failbuf(d, d->ip.buf);
  144. rq = d->ip.rq;
  145. if (rq == NULL)
  146. return;
  147. while ((bio = d->ip.nxbio)) {
  148. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  149. d->ip.nxbio = bio->bi_next;
  150. n = (unsigned long) rq->special;
  151. rq->special = (void *) --n;
  152. }
  153. if ((unsigned long) rq->special == 0)
  154. aoe_end_request(d, rq, 0);
  155. }
  156. static void
  157. downdev_frame(struct list_head *pos)
  158. {
  159. struct frame *f;
  160. f = list_entry(pos, struct frame, head);
  161. list_del(pos);
  162. if (f->buf) {
  163. f->buf->nframesout--;
  164. aoe_failbuf(f->t->d, f->buf);
  165. }
  166. aoe_freetframe(f);
  167. }
  168. void
  169. aoedev_downdev(struct aoedev *d)
  170. {
  171. struct aoetgt *t, **tt, **te;
  172. struct list_head *head, *pos, *nx;
  173. struct request *rq;
  174. int i;
  175. d->flags &= ~DEVFL_UP;
  176. /* clean out active and to-be-retransmitted buffers */
  177. for (i = 0; i < NFACTIVE; i++) {
  178. head = &d->factive[i];
  179. list_for_each_safe(pos, nx, head)
  180. downdev_frame(pos);
  181. }
  182. head = &d->rexmitq;
  183. list_for_each_safe(pos, nx, head)
  184. downdev_frame(pos);
  185. /* reset window dressings */
  186. tt = d->targets;
  187. te = tt + NTARGETS;
  188. for (; tt < te && (t = *tt); tt++) {
  189. aoecmd_wreset(t);
  190. t->nout = 0;
  191. }
  192. /* clean out the in-process request (if any) */
  193. aoe_failip(d);
  194. d->htgt = NULL;
  195. /* fast fail all pending I/O */
  196. if (d->blkq) {
  197. while ((rq = blk_peek_request(d->blkq))) {
  198. blk_start_request(rq);
  199. aoe_end_request(d, rq, 1);
  200. }
  201. }
  202. if (d->gd)
  203. set_capacity(d->gd, 0);
  204. }
  205. static void
  206. aoedev_freedev(struct aoedev *d)
  207. {
  208. struct aoetgt **t, **e;
  209. cancel_work_sync(&d->work);
  210. if (d->gd) {
  211. aoedisk_rm_sysfs(d);
  212. del_gendisk(d->gd);
  213. put_disk(d->gd);
  214. blk_cleanup_queue(d->blkq);
  215. }
  216. t = d->targets;
  217. e = t + NTARGETS;
  218. for (; t < e && *t; t++)
  219. freetgt(d, *t);
  220. if (d->bufpool)
  221. mempool_destroy(d->bufpool);
  222. skbpoolfree(d);
  223. minor_free(d->sysminor);
  224. kfree(d);
  225. }
  226. /* return whether the user asked for this particular
  227. * device to be flushed
  228. */
  229. static int
  230. user_req(char *s, size_t slen, struct aoedev *d)
  231. {
  232. char *p;
  233. size_t lim;
  234. if (!d->gd)
  235. return 0;
  236. p = strrchr(d->gd->disk_name, '/');
  237. if (!p)
  238. p = d->gd->disk_name;
  239. else
  240. p += 1;
  241. lim = sizeof(d->gd->disk_name);
  242. lim -= p - d->gd->disk_name;
  243. if (slen < lim)
  244. lim = slen;
  245. return !strncmp(s, p, lim);
  246. }
  247. int
  248. aoedev_flush(const char __user *str, size_t cnt)
  249. {
  250. ulong flags;
  251. struct aoedev *d, **dd;
  252. struct aoedev *rmd = NULL;
  253. char buf[16];
  254. int all = 0;
  255. int specified = 0; /* flush a specific device */
  256. if (cnt >= 3) {
  257. if (cnt > sizeof buf)
  258. cnt = sizeof buf;
  259. if (copy_from_user(buf, str, cnt))
  260. return -EFAULT;
  261. all = !strncmp(buf, "all", 3);
  262. if (!all)
  263. specified = 1;
  264. }
  265. spin_lock_irqsave(&devlist_lock, flags);
  266. dd = &devlist;
  267. while ((d = *dd)) {
  268. spin_lock(&d->lock);
  269. if (specified) {
  270. if (!user_req(buf, cnt, d))
  271. goto skip;
  272. } else if ((!all && (d->flags & DEVFL_UP))
  273. || (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
  274. || d->nopen
  275. || d->ref)
  276. goto skip;
  277. *dd = d->next;
  278. aoedev_downdev(d);
  279. d->flags |= DEVFL_TKILL;
  280. spin_unlock(&d->lock);
  281. d->next = rmd;
  282. rmd = d;
  283. continue;
  284. skip:
  285. spin_unlock(&d->lock);
  286. dd = &d->next;
  287. }
  288. spin_unlock_irqrestore(&devlist_lock, flags);
  289. while ((d = rmd)) {
  290. rmd = d->next;
  291. del_timer_sync(&d->timer);
  292. aoedev_freedev(d); /* must be able to sleep */
  293. }
  294. return 0;
  295. }
  296. /* This has been confirmed to occur once with Tms=3*1000 due to the
  297. * driver changing link and not processing its transmit ring. The
  298. * problem is hard enough to solve by returning an error that I'm
  299. * still punting on "solving" this.
  300. */
  301. static void
  302. skbfree(struct sk_buff *skb)
  303. {
  304. enum { Sms = 250, Tms = 30 * 1000};
  305. int i = Tms / Sms;
  306. if (skb == NULL)
  307. return;
  308. while (atomic_read(&skb_shinfo(skb)->dataref) != 1 && i-- > 0)
  309. msleep(Sms);
  310. if (i < 0) {
  311. printk(KERN_ERR
  312. "aoe: %s holds ref: %s\n",
  313. skb->dev ? skb->dev->name : "netif",
  314. "cannot free skb -- memory leaked.");
  315. return;
  316. }
  317. skb->truesize -= skb->data_len;
  318. skb_shinfo(skb)->nr_frags = skb->data_len = 0;
  319. skb_trim(skb, 0);
  320. dev_kfree_skb(skb);
  321. }
  322. static void
  323. skbpoolfree(struct aoedev *d)
  324. {
  325. struct sk_buff *skb, *tmp;
  326. skb_queue_walk_safe(&d->skbpool, skb, tmp)
  327. skbfree(skb);
  328. __skb_queue_head_init(&d->skbpool);
  329. }
  330. /* find it or allocate it */
  331. struct aoedev *
  332. aoedev_by_aoeaddr(ulong maj, int min, int do_alloc)
  333. {
  334. struct aoedev *d;
  335. int i;
  336. ulong flags;
  337. ulong sysminor;
  338. spin_lock_irqsave(&devlist_lock, flags);
  339. for (d=devlist; d; d=d->next)
  340. if (d->aoemajor == maj && d->aoeminor == min) {
  341. d->ref++;
  342. break;
  343. }
  344. if (d || !do_alloc || minor_get(&sysminor, maj, min) < 0)
  345. goto out;
  346. d = kcalloc(1, sizeof *d, GFP_ATOMIC);
  347. if (!d)
  348. goto out;
  349. INIT_WORK(&d->work, aoecmd_sleepwork);
  350. spin_lock_init(&d->lock);
  351. skb_queue_head_init(&d->skbpool);
  352. init_timer(&d->timer);
  353. d->timer.data = (ulong) d;
  354. d->timer.function = dummy_timer;
  355. d->timer.expires = jiffies + HZ;
  356. add_timer(&d->timer);
  357. d->bufpool = NULL; /* defer to aoeblk_gdalloc */
  358. d->tgt = d->targets;
  359. d->ref = 1;
  360. for (i = 0; i < NFACTIVE; i++)
  361. INIT_LIST_HEAD(&d->factive[i]);
  362. INIT_LIST_HEAD(&d->rexmitq);
  363. d->sysminor = sysminor;
  364. d->aoemajor = maj;
  365. d->aoeminor = min;
  366. d->rttavg = RTTAVG_INIT;
  367. d->rttdev = RTTDEV_INIT;
  368. d->next = devlist;
  369. devlist = d;
  370. out:
  371. spin_unlock_irqrestore(&devlist_lock, flags);
  372. return d;
  373. }
  374. static void
  375. freetgt(struct aoedev *d, struct aoetgt *t)
  376. {
  377. struct frame *f;
  378. struct list_head *pos, *nx, *head;
  379. struct aoeif *ifp;
  380. for (ifp = t->ifs; ifp < &t->ifs[NAOEIFS]; ++ifp) {
  381. if (!ifp->nd)
  382. break;
  383. dev_put(ifp->nd);
  384. }
  385. head = &t->ffree;
  386. list_for_each_safe(pos, nx, head) {
  387. list_del(pos);
  388. f = list_entry(pos, struct frame, head);
  389. skbfree(f->skb);
  390. kfree(f);
  391. }
  392. kfree(t);
  393. }
  394. void
  395. aoedev_exit(void)
  396. {
  397. struct aoedev *d;
  398. ulong flags;
  399. aoe_flush_iocq();
  400. while ((d = devlist)) {
  401. devlist = d->next;
  402. spin_lock_irqsave(&d->lock, flags);
  403. aoedev_downdev(d);
  404. d->flags |= DEVFL_TKILL;
  405. spin_unlock_irqrestore(&d->lock, flags);
  406. del_timer_sync(&d->timer);
  407. aoedev_freedev(d);
  408. }
  409. }
  410. int __init
  411. aoedev_init(void)
  412. {
  413. return 0;
  414. }