aoedev.c 11 KB

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