aoedev.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 = from_timer(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. int i;
  173. d->flags &= ~DEVFL_UP;
  174. /* clean out active and to-be-retransmitted buffers */
  175. for (i = 0; i < NFACTIVE; i++) {
  176. head = &d->factive[i];
  177. list_for_each_safe(pos, nx, head)
  178. downdev_frame(pos);
  179. }
  180. head = &d->rexmitq;
  181. list_for_each_safe(pos, nx, head)
  182. downdev_frame(pos);
  183. /* reset window dressings */
  184. tt = d->targets;
  185. te = tt + d->ntargets;
  186. for (; tt < te && (t = *tt); tt++) {
  187. aoecmd_wreset(t);
  188. t->nout = 0;
  189. }
  190. /* clean out the in-process request (if any) */
  191. aoe_failip(d);
  192. /* fast fail all pending I/O */
  193. if (d->blkq) {
  194. /* UP is cleared, freeze+quiesce to insure all are errored */
  195. blk_mq_freeze_queue(d->blkq);
  196. blk_mq_quiesce_queue(d->blkq);
  197. blk_mq_unquiesce_queue(d->blkq);
  198. blk_mq_unfreeze_queue(d->blkq);
  199. }
  200. if (d->gd)
  201. set_capacity(d->gd, 0);
  202. }
  203. /* return whether the user asked for this particular
  204. * device to be flushed
  205. */
  206. static int
  207. user_req(char *s, size_t slen, struct aoedev *d)
  208. {
  209. const char *p;
  210. size_t lim;
  211. if (!d->gd)
  212. return 0;
  213. p = kbasename(d->gd->disk_name);
  214. lim = sizeof(d->gd->disk_name);
  215. lim -= p - d->gd->disk_name;
  216. if (slen < lim)
  217. lim = slen;
  218. return !strncmp(s, p, lim);
  219. }
  220. static void
  221. freedev(struct aoedev *d)
  222. {
  223. struct aoetgt **t, **e;
  224. int freeing = 0;
  225. unsigned long flags;
  226. spin_lock_irqsave(&d->lock, flags);
  227. if (d->flags & DEVFL_TKILL
  228. && !(d->flags & DEVFL_FREEING)) {
  229. d->flags |= DEVFL_FREEING;
  230. freeing = 1;
  231. }
  232. spin_unlock_irqrestore(&d->lock, flags);
  233. if (!freeing)
  234. return;
  235. del_timer_sync(&d->timer);
  236. if (d->gd) {
  237. aoedisk_rm_debugfs(d);
  238. del_gendisk(d->gd);
  239. put_disk(d->gd);
  240. blk_mq_free_tag_set(&d->tag_set);
  241. }
  242. t = d->targets;
  243. e = t + d->ntargets;
  244. for (; t < e && *t; t++)
  245. freetgt(d, *t);
  246. mempool_destroy(d->bufpool);
  247. skbpoolfree(d);
  248. minor_free(d->sysminor);
  249. spin_lock_irqsave(&d->lock, flags);
  250. d->flags |= DEVFL_FREED;
  251. spin_unlock_irqrestore(&d->lock, flags);
  252. }
  253. enum flush_parms {
  254. NOT_EXITING = 0,
  255. EXITING = 1,
  256. };
  257. static int
  258. flush(const char __user *str, size_t cnt, int exiting)
  259. {
  260. ulong flags;
  261. struct aoedev *d, **dd;
  262. char buf[16];
  263. int all = 0;
  264. int specified = 0; /* flush a specific device */
  265. unsigned int skipflags;
  266. skipflags = DEVFL_GDALLOC | DEVFL_NEWSIZE | DEVFL_TKILL;
  267. if (!exiting && cnt >= 3) {
  268. if (cnt > sizeof buf)
  269. cnt = sizeof buf;
  270. if (copy_from_user(buf, str, cnt))
  271. return -EFAULT;
  272. all = !strncmp(buf, "all", 3);
  273. if (!all)
  274. specified = 1;
  275. }
  276. flush_workqueue(aoe_wq);
  277. /* pass one: do aoedev_downdev, which might sleep */
  278. restart1:
  279. spin_lock_irqsave(&devlist_lock, flags);
  280. for (d = devlist; d; d = d->next) {
  281. spin_lock(&d->lock);
  282. if (d->flags & DEVFL_TKILL)
  283. goto cont;
  284. if (exiting) {
  285. /* unconditionally take each device down */
  286. } else if (specified) {
  287. if (!user_req(buf, cnt, d))
  288. goto cont;
  289. } else if ((!all && (d->flags & DEVFL_UP))
  290. || d->flags & skipflags
  291. || d->nopen
  292. || d->ref)
  293. goto cont;
  294. spin_unlock(&d->lock);
  295. spin_unlock_irqrestore(&devlist_lock, flags);
  296. aoedev_downdev(d);
  297. d->flags |= DEVFL_TKILL;
  298. goto restart1;
  299. cont:
  300. spin_unlock(&d->lock);
  301. }
  302. spin_unlock_irqrestore(&devlist_lock, flags);
  303. /* pass two: call freedev, which might sleep,
  304. * for aoedevs marked with DEVFL_TKILL
  305. */
  306. restart2:
  307. spin_lock_irqsave(&devlist_lock, flags);
  308. for (d = devlist; d; d = d->next) {
  309. spin_lock(&d->lock);
  310. if (d->flags & DEVFL_TKILL
  311. && !(d->flags & DEVFL_FREEING)) {
  312. spin_unlock(&d->lock);
  313. spin_unlock_irqrestore(&devlist_lock, flags);
  314. freedev(d);
  315. goto restart2;
  316. }
  317. spin_unlock(&d->lock);
  318. }
  319. /* pass three: remove aoedevs marked with DEVFL_FREED */
  320. for (dd = &devlist, d = *dd; d; d = *dd) {
  321. struct aoedev *doomed = NULL;
  322. spin_lock(&d->lock);
  323. if (d->flags & DEVFL_FREED) {
  324. *dd = d->next;
  325. doomed = d;
  326. } else {
  327. dd = &d->next;
  328. }
  329. spin_unlock(&d->lock);
  330. if (doomed)
  331. kfree(doomed->targets);
  332. kfree(doomed);
  333. }
  334. spin_unlock_irqrestore(&devlist_lock, flags);
  335. return 0;
  336. }
  337. int
  338. aoedev_flush(const char __user *str, size_t cnt)
  339. {
  340. return flush(str, cnt, NOT_EXITING);
  341. }
  342. /* This has been confirmed to occur once with Tms=3*1000 due to the
  343. * driver changing link and not processing its transmit ring. The
  344. * problem is hard enough to solve by returning an error that I'm
  345. * still punting on "solving" this.
  346. */
  347. static void
  348. skbfree(struct sk_buff *skb)
  349. {
  350. enum { Sms = 250, Tms = 30 * 1000};
  351. int i = Tms / Sms;
  352. if (skb == NULL)
  353. return;
  354. while (atomic_read(&skb_shinfo(skb)->dataref) != 1 && i-- > 0)
  355. msleep(Sms);
  356. if (i < 0) {
  357. printk(KERN_ERR
  358. "aoe: %s holds ref: %s\n",
  359. skb->dev ? skb->dev->name : "netif",
  360. "cannot free skb -- memory leaked.");
  361. return;
  362. }
  363. skb->truesize -= skb->data_len;
  364. skb_shinfo(skb)->nr_frags = skb->data_len = 0;
  365. skb_trim(skb, 0);
  366. dev_kfree_skb(skb);
  367. }
  368. static void
  369. skbpoolfree(struct aoedev *d)
  370. {
  371. struct sk_buff *skb, *tmp;
  372. skb_queue_walk_safe(&d->skbpool, skb, tmp)
  373. skbfree(skb);
  374. __skb_queue_head_init(&d->skbpool);
  375. }
  376. /* find it or allocate it */
  377. struct aoedev *
  378. aoedev_by_aoeaddr(ulong maj, int min, int do_alloc)
  379. {
  380. struct aoedev *d;
  381. int i;
  382. ulong flags;
  383. ulong sysminor = 0;
  384. spin_lock_irqsave(&devlist_lock, flags);
  385. for (d=devlist; d; d=d->next)
  386. if (d->aoemajor == maj && d->aoeminor == min) {
  387. spin_lock(&d->lock);
  388. if (d->flags & DEVFL_TKILL) {
  389. spin_unlock(&d->lock);
  390. d = NULL;
  391. goto out;
  392. }
  393. d->ref++;
  394. spin_unlock(&d->lock);
  395. break;
  396. }
  397. if (d || !do_alloc || minor_get(&sysminor, maj, min) < 0)
  398. goto out;
  399. d = kcalloc(1, sizeof *d, GFP_ATOMIC);
  400. if (!d)
  401. goto out;
  402. d->targets = kcalloc(NTARGETS, sizeof(*d->targets), GFP_ATOMIC);
  403. if (!d->targets) {
  404. kfree(d);
  405. d = NULL;
  406. goto out;
  407. }
  408. d->ntargets = NTARGETS;
  409. INIT_WORK(&d->work, aoecmd_sleepwork);
  410. spin_lock_init(&d->lock);
  411. INIT_LIST_HEAD(&d->rq_list);
  412. skb_queue_head_init(&d->skbpool);
  413. timer_setup(&d->timer, dummy_timer, 0);
  414. d->timer.expires = jiffies + HZ;
  415. add_timer(&d->timer);
  416. d->bufpool = NULL; /* defer to aoeblk_gdalloc */
  417. d->tgt = d->targets;
  418. d->ref = 1;
  419. for (i = 0; i < NFACTIVE; i++)
  420. INIT_LIST_HEAD(&d->factive[i]);
  421. INIT_LIST_HEAD(&d->rexmitq);
  422. d->sysminor = sysminor;
  423. d->aoemajor = maj;
  424. d->aoeminor = min;
  425. d->rttavg = RTTAVG_INIT;
  426. d->rttdev = RTTDEV_INIT;
  427. d->next = devlist;
  428. devlist = d;
  429. out:
  430. spin_unlock_irqrestore(&devlist_lock, flags);
  431. return d;
  432. }
  433. static void
  434. freetgt(struct aoedev *d, struct aoetgt *t)
  435. {
  436. struct frame *f;
  437. struct list_head *pos, *nx, *head;
  438. struct aoeif *ifp;
  439. for (ifp = t->ifs; ifp < &t->ifs[NAOEIFS]; ++ifp) {
  440. if (!ifp->nd)
  441. break;
  442. dev_put(ifp->nd);
  443. }
  444. head = &t->ffree;
  445. list_for_each_safe(pos, nx, head) {
  446. list_del(pos);
  447. f = list_entry(pos, struct frame, head);
  448. skbfree(f->skb);
  449. kfree(f);
  450. }
  451. kfree(t);
  452. }
  453. void
  454. aoedev_exit(void)
  455. {
  456. flush_workqueue(aoe_wq);
  457. flush(NULL, 0, EXITING);
  458. }
  459. int __init
  460. aoedev_init(void)
  461. {
  462. return 0;
  463. }