aoedev.c 11 KB

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