PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/block/aoe/aoedev.c

https://gitlab.com/SpasilliumNexus/frankenclark_kernel
C | 529 lines | 420 code | 65 blank | 44 comment | 71 complexity | 9e7eef3e61f30e0b79e1834fe9d0f4e4 MD5 | raw file
  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 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(ulong vp)
  128. {
  129. struct aoedev *d;
  130. d = (struct aoedev *)vp;
  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 bio *bio;
  141. unsigned long n;
  142. aoe_failbuf(d, d->ip.buf);
  143. rq = d->ip.rq;
  144. if (rq == NULL)
  145. return;
  146. while ((bio = d->ip.nxbio)) {
  147. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  148. d->ip.nxbio = bio->bi_next;
  149. n = (unsigned long) rq->special;
  150. rq->special = (void *) --n;
  151. }
  152. if ((unsigned long) rq->special == 0)
  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;
  173. int i;
  174. d->flags &= ~DEVFL_UP;
  175. /* clean out active and to-be-retransmitted buffers */
  176. for (i = 0; i < NFACTIVE; i++) {
  177. head = &d->factive[i];
  178. list_for_each_safe(pos, nx, head)
  179. downdev_frame(pos);
  180. }
  181. head = &d->rexmitq;
  182. list_for_each_safe(pos, nx, head)
  183. downdev_frame(pos);
  184. /* reset window dressings */
  185. tt = d->targets;
  186. te = tt + d->ntargets;
  187. for (; tt < te && (t = *tt); tt++) {
  188. aoecmd_wreset(t);
  189. t->nout = 0;
  190. }
  191. /* clean out the in-process request (if any) */
  192. aoe_failip(d);
  193. /* fast fail all pending I/O */
  194. if (d->blkq) {
  195. while ((rq = blk_peek_request(d->blkq))) {
  196. blk_start_request(rq);
  197. aoe_end_request(d, rq, 1);
  198. }
  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. char *p;
  210. size_t lim;
  211. if (!d->gd)
  212. return 0;
  213. p = strrchr(d->gd->disk_name, '/');
  214. if (!p)
  215. p = d->gd->disk_name;
  216. else
  217. p += 1;
  218. lim = sizeof(d->gd->disk_name);
  219. lim -= p - d->gd->disk_name;
  220. if (slen < lim)
  221. lim = slen;
  222. return !strncmp(s, p, lim);
  223. }
  224. static void
  225. freedev(struct aoedev *d)
  226. {
  227. struct aoetgt **t, **e;
  228. int freeing = 0;
  229. unsigned long flags;
  230. spin_lock_irqsave(&d->lock, flags);
  231. if (d->flags & DEVFL_TKILL
  232. && !(d->flags & DEVFL_FREEING)) {
  233. d->flags |= DEVFL_FREEING;
  234. freeing = 1;
  235. }
  236. spin_unlock_irqrestore(&d->lock, flags);
  237. if (!freeing)
  238. return;
  239. del_timer_sync(&d->timer);
  240. if (d->gd) {
  241. aoedisk_rm_sysfs(d);
  242. del_gendisk(d->gd);
  243. put_disk(d->gd);
  244. blk_cleanup_queue(d->blkq);
  245. }
  246. t = d->targets;
  247. e = t + d->ntargets;
  248. for (; t < e && *t; t++)
  249. freetgt(d, *t);
  250. if (d->bufpool)
  251. mempool_destroy(d->bufpool);
  252. skbpoolfree(d);
  253. minor_free(d->sysminor);
  254. spin_lock_irqsave(&d->lock, flags);
  255. d->flags |= DEVFL_FREED;
  256. spin_unlock_irqrestore(&d->lock, flags);
  257. }
  258. enum flush_parms {
  259. NOT_EXITING = 0,
  260. EXITING = 1,
  261. };
  262. static int
  263. flush(const char __user *str, size_t cnt, int exiting)
  264. {
  265. ulong flags;
  266. struct aoedev *d, **dd;
  267. char buf[16];
  268. int all = 0;
  269. int specified = 0; /* flush a specific device */
  270. unsigned int skipflags;
  271. skipflags = DEVFL_GDALLOC | DEVFL_NEWSIZE | DEVFL_TKILL;
  272. if (!exiting && cnt >= 3) {
  273. if (cnt > sizeof buf)
  274. cnt = sizeof buf;
  275. if (copy_from_user(buf, str, cnt))
  276. return -EFAULT;
  277. all = !strncmp(buf, "all", 3);
  278. if (!all)
  279. specified = 1;
  280. }
  281. flush_scheduled_work();
  282. /* pass one: without sleeping, do aoedev_downdev */
  283. spin_lock_irqsave(&devlist_lock, flags);
  284. for (d = devlist; d; d = d->next) {
  285. spin_lock(&d->lock);
  286. if (exiting) {
  287. /* unconditionally take each device down */
  288. } else if (specified) {
  289. if (!user_req(buf, cnt, d))
  290. goto cont;
  291. } else if ((!all && (d->flags & DEVFL_UP))
  292. || d->flags & skipflags
  293. || d->nopen
  294. || d->ref)
  295. goto cont;
  296. aoedev_downdev(d);
  297. d->flags |= DEVFL_TKILL;
  298. cont:
  299. spin_unlock(&d->lock);
  300. }
  301. spin_unlock_irqrestore(&devlist_lock, flags);
  302. /* pass two: call freedev, which might sleep,
  303. * for aoedevs marked with DEVFL_TKILL
  304. */
  305. restart:
  306. spin_lock_irqsave(&devlist_lock, flags);
  307. for (d = devlist; d; d = d->next) {
  308. spin_lock(&d->lock);
  309. if (d->flags & DEVFL_TKILL
  310. && !(d->flags & DEVFL_FREEING)) {
  311. spin_unlock(&d->lock);
  312. spin_unlock_irqrestore(&devlist_lock, flags);
  313. freedev(d);
  314. goto restart;
  315. }
  316. spin_unlock(&d->lock);
  317. }
  318. /* pass three: remove aoedevs marked with DEVFL_FREED */
  319. for (dd = &devlist, d = *dd; d; d = *dd) {
  320. struct aoedev *doomed = NULL;
  321. spin_lock(&d->lock);
  322. if (d->flags & DEVFL_FREED) {
  323. *dd = d->next;
  324. doomed = d;
  325. } else {
  326. dd = &d->next;
  327. }
  328. spin_unlock(&d->lock);
  329. if (doomed)
  330. kfree(doomed->targets);
  331. kfree(doomed);
  332. }
  333. spin_unlock_irqrestore(&devlist_lock, flags);
  334. return 0;
  335. }
  336. int
  337. aoedev_flush(const char __user *str, size_t cnt)
  338. {
  339. return flush(str, cnt, NOT_EXITING);
  340. }
  341. /* This has been confirmed to occur once with Tms=3*1000 due to the
  342. * driver changing link and not processing its transmit ring. The
  343. * problem is hard enough to solve by returning an error that I'm
  344. * still punting on "solving" this.
  345. */
  346. static void
  347. skbfree(struct sk_buff *skb)
  348. {
  349. enum { Sms = 250, Tms = 30 * 1000};
  350. int i = Tms / Sms;
  351. if (skb == NULL)
  352. return;
  353. while (atomic_read(&skb_shinfo(skb)->dataref) != 1 && i-- > 0)
  354. msleep(Sms);
  355. if (i < 0) {
  356. printk(KERN_ERR
  357. "aoe: %s holds ref: %s\n",
  358. skb->dev ? skb->dev->name : "netif",
  359. "cannot free skb -- memory leaked.");
  360. return;
  361. }
  362. skb->truesize -= skb->data_len;
  363. skb_shinfo(skb)->nr_frags = skb->data_len = 0;
  364. skb_trim(skb, 0);
  365. dev_kfree_skb(skb);
  366. }
  367. static void
  368. skbpoolfree(struct aoedev *d)
  369. {
  370. struct sk_buff *skb, *tmp;
  371. skb_queue_walk_safe(&d->skbpool, skb, tmp)
  372. skbfree(skb);
  373. __skb_queue_head_init(&d->skbpool);
  374. }
  375. /* find it or allocate it */
  376. struct aoedev *
  377. aoedev_by_aoeaddr(ulong maj, int min, int do_alloc)
  378. {
  379. struct aoedev *d;
  380. int i;
  381. ulong flags;
  382. ulong sysminor = 0;
  383. spin_lock_irqsave(&devlist_lock, flags);
  384. for (d=devlist; d; d=d->next)
  385. if (d->aoemajor == maj && d->aoeminor == min) {
  386. spin_lock(&d->lock);
  387. if (d->flags & DEVFL_TKILL) {
  388. spin_unlock(&d->lock);
  389. d = NULL;
  390. goto out;
  391. }
  392. d->ref++;
  393. spin_unlock(&d->lock);
  394. break;
  395. }
  396. if (d || !do_alloc || minor_get(&sysminor, maj, min) < 0)
  397. goto out;
  398. d = kcalloc(1, sizeof *d, GFP_ATOMIC);
  399. if (!d)
  400. goto out;
  401. d->targets = kcalloc(NTARGETS, sizeof(*d->targets), GFP_ATOMIC);
  402. if (!d->targets) {
  403. kfree(d);
  404. d = NULL;
  405. goto out;
  406. }
  407. d->ntargets = NTARGETS;
  408. INIT_WORK(&d->work, aoecmd_sleepwork);
  409. spin_lock_init(&d->lock);
  410. skb_queue_head_init(&d->skbpool);
  411. init_timer(&d->timer);
  412. d->timer.data = (ulong) d;
  413. d->timer.function = dummy_timer;
  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_scheduled_work();
  457. aoe_flush_iocq();
  458. flush(NULL, 0, EXITING);
  459. }
  460. int __init
  461. aoedev_init(void)
  462. {
  463. return 0;
  464. }