dcssblk.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * dcssblk.c -- the S/390 block driver for dcss memory
  4. *
  5. * Authors: Carsten Otte, Stefan Weinhuber, Gerald Schaefer
  6. */
  7. #define KMSG_COMPONENT "dcssblk"
  8. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/ctype.h>
  12. #include <linux/errno.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/completion.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/pfn_t.h>
  19. #include <linux/uio.h>
  20. #include <linux/dax.h>
  21. #include <asm/extmem.h>
  22. #include <asm/io.h>
  23. #define DCSSBLK_NAME "dcssblk"
  24. #define DCSSBLK_MINORS_PER_DISK 1
  25. #define DCSSBLK_PARM_LEN 400
  26. #define DCSS_BUS_ID_SIZE 20
  27. static int dcssblk_open(struct block_device *bdev, fmode_t mode);
  28. static void dcssblk_release(struct gendisk *disk, fmode_t mode);
  29. static void dcssblk_submit_bio(struct bio *bio);
  30. static long dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
  31. long nr_pages, enum dax_access_mode mode, void **kaddr,
  32. pfn_t *pfn);
  33. static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0";
  34. static int dcssblk_major;
  35. static const struct block_device_operations dcssblk_devops = {
  36. .owner = THIS_MODULE,
  37. .submit_bio = dcssblk_submit_bio,
  38. .open = dcssblk_open,
  39. .release = dcssblk_release,
  40. };
  41. static int dcssblk_dax_zero_page_range(struct dax_device *dax_dev,
  42. pgoff_t pgoff, size_t nr_pages)
  43. {
  44. long rc;
  45. void *kaddr;
  46. rc = dax_direct_access(dax_dev, pgoff, nr_pages, DAX_ACCESS,
  47. &kaddr, NULL);
  48. if (rc < 0)
  49. return rc;
  50. memset(kaddr, 0, nr_pages << PAGE_SHIFT);
  51. dax_flush(dax_dev, kaddr, nr_pages << PAGE_SHIFT);
  52. return 0;
  53. }
  54. static const struct dax_operations dcssblk_dax_ops = {
  55. .direct_access = dcssblk_dax_direct_access,
  56. .zero_page_range = dcssblk_dax_zero_page_range,
  57. };
  58. struct dcssblk_dev_info {
  59. struct list_head lh;
  60. struct device dev;
  61. char segment_name[DCSS_BUS_ID_SIZE];
  62. atomic_t use_count;
  63. struct gendisk *gd;
  64. unsigned long start;
  65. unsigned long end;
  66. int segment_type;
  67. unsigned char save_pending;
  68. unsigned char is_shared;
  69. int num_of_segments;
  70. struct list_head seg_list;
  71. struct dax_device *dax_dev;
  72. };
  73. struct segment_info {
  74. struct list_head lh;
  75. char segment_name[DCSS_BUS_ID_SIZE];
  76. unsigned long start;
  77. unsigned long end;
  78. int segment_type;
  79. };
  80. static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
  81. size_t count);
  82. static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
  83. size_t count);
  84. static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
  85. static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
  86. static struct device *dcssblk_root_dev;
  87. static LIST_HEAD(dcssblk_devices);
  88. static struct rw_semaphore dcssblk_devices_sem;
  89. /*
  90. * release function for segment device.
  91. */
  92. static void
  93. dcssblk_release_segment(struct device *dev)
  94. {
  95. struct dcssblk_dev_info *dev_info;
  96. struct segment_info *entry, *temp;
  97. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  98. list_for_each_entry_safe(entry, temp, &dev_info->seg_list, lh) {
  99. list_del(&entry->lh);
  100. kfree(entry);
  101. }
  102. kfree(dev_info);
  103. module_put(THIS_MODULE);
  104. }
  105. /*
  106. * get a minor number. needs to be called with
  107. * down_write(&dcssblk_devices_sem) and the
  108. * device needs to be enqueued before the semaphore is
  109. * freed.
  110. */
  111. static int
  112. dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info)
  113. {
  114. int minor, found;
  115. struct dcssblk_dev_info *entry;
  116. if (dev_info == NULL)
  117. return -EINVAL;
  118. for (minor = 0; minor < (1<<MINORBITS); minor++) {
  119. found = 0;
  120. // test if minor available
  121. list_for_each_entry(entry, &dcssblk_devices, lh)
  122. if (minor == entry->gd->first_minor)
  123. found++;
  124. if (!found) break; // got unused minor
  125. }
  126. if (found)
  127. return -EBUSY;
  128. dev_info->gd->first_minor = minor;
  129. return 0;
  130. }
  131. /*
  132. * get the struct dcssblk_dev_info from dcssblk_devices
  133. * for the given name.
  134. * down_read(&dcssblk_devices_sem) must be held.
  135. */
  136. static struct dcssblk_dev_info *
  137. dcssblk_get_device_by_name(char *name)
  138. {
  139. struct dcssblk_dev_info *entry;
  140. list_for_each_entry(entry, &dcssblk_devices, lh) {
  141. if (!strcmp(name, entry->segment_name)) {
  142. return entry;
  143. }
  144. }
  145. return NULL;
  146. }
  147. /*
  148. * get the struct segment_info from seg_list
  149. * for the given name.
  150. * down_read(&dcssblk_devices_sem) must be held.
  151. */
  152. static struct segment_info *
  153. dcssblk_get_segment_by_name(char *name)
  154. {
  155. struct dcssblk_dev_info *dev_info;
  156. struct segment_info *entry;
  157. list_for_each_entry(dev_info, &dcssblk_devices, lh) {
  158. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  159. if (!strcmp(name, entry->segment_name))
  160. return entry;
  161. }
  162. }
  163. return NULL;
  164. }
  165. /*
  166. * get the highest address of the multi-segment block.
  167. */
  168. static unsigned long
  169. dcssblk_find_highest_addr(struct dcssblk_dev_info *dev_info)
  170. {
  171. unsigned long highest_addr;
  172. struct segment_info *entry;
  173. highest_addr = 0;
  174. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  175. if (highest_addr < entry->end)
  176. highest_addr = entry->end;
  177. }
  178. return highest_addr;
  179. }
  180. /*
  181. * get the lowest address of the multi-segment block.
  182. */
  183. static unsigned long
  184. dcssblk_find_lowest_addr(struct dcssblk_dev_info *dev_info)
  185. {
  186. int set_first;
  187. unsigned long lowest_addr;
  188. struct segment_info *entry;
  189. set_first = 0;
  190. lowest_addr = 0;
  191. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  192. if (set_first == 0) {
  193. lowest_addr = entry->start;
  194. set_first = 1;
  195. } else {
  196. if (lowest_addr > entry->start)
  197. lowest_addr = entry->start;
  198. }
  199. }
  200. return lowest_addr;
  201. }
  202. /*
  203. * Check continuity of segments.
  204. */
  205. static int
  206. dcssblk_is_continuous(struct dcssblk_dev_info *dev_info)
  207. {
  208. int i, j, rc;
  209. struct segment_info *sort_list, *entry, temp;
  210. if (dev_info->num_of_segments <= 1)
  211. return 0;
  212. sort_list = kcalloc(dev_info->num_of_segments,
  213. sizeof(struct segment_info),
  214. GFP_KERNEL);
  215. if (sort_list == NULL)
  216. return -ENOMEM;
  217. i = 0;
  218. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  219. memcpy(&sort_list[i], entry, sizeof(struct segment_info));
  220. i++;
  221. }
  222. /* sort segments */
  223. for (i = 0; i < dev_info->num_of_segments; i++)
  224. for (j = 0; j < dev_info->num_of_segments; j++)
  225. if (sort_list[j].start > sort_list[i].start) {
  226. memcpy(&temp, &sort_list[i],
  227. sizeof(struct segment_info));
  228. memcpy(&sort_list[i], &sort_list[j],
  229. sizeof(struct segment_info));
  230. memcpy(&sort_list[j], &temp,
  231. sizeof(struct segment_info));
  232. }
  233. /* check continuity */
  234. for (i = 0; i < dev_info->num_of_segments - 1; i++) {
  235. if ((sort_list[i].end + 1) != sort_list[i+1].start) {
  236. pr_err("Adjacent DCSSs %s and %s are not "
  237. "contiguous\n", sort_list[i].segment_name,
  238. sort_list[i+1].segment_name);
  239. rc = -EINVAL;
  240. goto out;
  241. }
  242. /* EN and EW are allowed in a block device */
  243. if (sort_list[i].segment_type != sort_list[i+1].segment_type) {
  244. if (!(sort_list[i].segment_type & SEGMENT_EXCLUSIVE) ||
  245. (sort_list[i].segment_type == SEG_TYPE_ER) ||
  246. !(sort_list[i+1].segment_type &
  247. SEGMENT_EXCLUSIVE) ||
  248. (sort_list[i+1].segment_type == SEG_TYPE_ER)) {
  249. pr_err("DCSS %s and DCSS %s have "
  250. "incompatible types\n",
  251. sort_list[i].segment_name,
  252. sort_list[i+1].segment_name);
  253. rc = -EINVAL;
  254. goto out;
  255. }
  256. }
  257. }
  258. rc = 0;
  259. out:
  260. kfree(sort_list);
  261. return rc;
  262. }
  263. /*
  264. * Load a segment
  265. */
  266. static int
  267. dcssblk_load_segment(char *name, struct segment_info **seg_info)
  268. {
  269. int rc;
  270. /* already loaded? */
  271. down_read(&dcssblk_devices_sem);
  272. *seg_info = dcssblk_get_segment_by_name(name);
  273. up_read(&dcssblk_devices_sem);
  274. if (*seg_info != NULL)
  275. return -EEXIST;
  276. /* get a struct segment_info */
  277. *seg_info = kzalloc(sizeof(struct segment_info), GFP_KERNEL);
  278. if (*seg_info == NULL)
  279. return -ENOMEM;
  280. strcpy((*seg_info)->segment_name, name);
  281. /* load the segment */
  282. rc = segment_load(name, SEGMENT_SHARED,
  283. &(*seg_info)->start, &(*seg_info)->end);
  284. if (rc < 0) {
  285. segment_warning(rc, (*seg_info)->segment_name);
  286. kfree(*seg_info);
  287. } else {
  288. INIT_LIST_HEAD(&(*seg_info)->lh);
  289. (*seg_info)->segment_type = rc;
  290. }
  291. return rc;
  292. }
  293. /*
  294. * device attribute for switching shared/nonshared (exclusive)
  295. * operation (show + store)
  296. */
  297. static ssize_t
  298. dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
  299. {
  300. struct dcssblk_dev_info *dev_info;
  301. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  302. return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
  303. }
  304. static ssize_t
  305. dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
  306. {
  307. struct dcssblk_dev_info *dev_info;
  308. struct segment_info *entry, *temp;
  309. int rc;
  310. if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
  311. return -EINVAL;
  312. down_write(&dcssblk_devices_sem);
  313. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  314. if (atomic_read(&dev_info->use_count)) {
  315. rc = -EBUSY;
  316. goto out;
  317. }
  318. if (inbuf[0] == '1') {
  319. /* reload segments in shared mode */
  320. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  321. rc = segment_modify_shared(entry->segment_name,
  322. SEGMENT_SHARED);
  323. if (rc < 0) {
  324. BUG_ON(rc == -EINVAL);
  325. if (rc != -EAGAIN)
  326. goto removeseg;
  327. }
  328. }
  329. dev_info->is_shared = 1;
  330. switch (dev_info->segment_type) {
  331. case SEG_TYPE_SR:
  332. case SEG_TYPE_ER:
  333. case SEG_TYPE_SC:
  334. set_disk_ro(dev_info->gd, 1);
  335. }
  336. } else if (inbuf[0] == '0') {
  337. /* reload segments in exclusive mode */
  338. if (dev_info->segment_type == SEG_TYPE_SC) {
  339. pr_err("DCSS %s is of type SC and cannot be "
  340. "loaded as exclusive-writable\n",
  341. dev_info->segment_name);
  342. rc = -EINVAL;
  343. goto out;
  344. }
  345. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  346. rc = segment_modify_shared(entry->segment_name,
  347. SEGMENT_EXCLUSIVE);
  348. if (rc < 0) {
  349. BUG_ON(rc == -EINVAL);
  350. if (rc != -EAGAIN)
  351. goto removeseg;
  352. }
  353. }
  354. dev_info->is_shared = 0;
  355. set_disk_ro(dev_info->gd, 0);
  356. } else {
  357. rc = -EINVAL;
  358. goto out;
  359. }
  360. rc = count;
  361. goto out;
  362. removeseg:
  363. pr_err("DCSS device %s is removed after a failed access mode "
  364. "change\n", dev_info->segment_name);
  365. temp = entry;
  366. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  367. if (entry != temp)
  368. segment_unload(entry->segment_name);
  369. }
  370. list_del(&dev_info->lh);
  371. dax_remove_host(dev_info->gd);
  372. kill_dax(dev_info->dax_dev);
  373. put_dax(dev_info->dax_dev);
  374. del_gendisk(dev_info->gd);
  375. put_disk(dev_info->gd);
  376. up_write(&dcssblk_devices_sem);
  377. if (device_remove_file_self(dev, attr)) {
  378. device_unregister(dev);
  379. put_device(dev);
  380. }
  381. return rc;
  382. out:
  383. up_write(&dcssblk_devices_sem);
  384. return rc;
  385. }
  386. static DEVICE_ATTR(shared, S_IWUSR | S_IRUSR, dcssblk_shared_show,
  387. dcssblk_shared_store);
  388. /*
  389. * device attribute for save operation on current copy
  390. * of the segment. If the segment is busy, saving will
  391. * become pending until it gets released, which can be
  392. * undone by storing a non-true value to this entry.
  393. * (show + store)
  394. */
  395. static ssize_t
  396. dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
  397. {
  398. struct dcssblk_dev_info *dev_info;
  399. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  400. return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
  401. }
  402. static ssize_t
  403. dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
  404. {
  405. struct dcssblk_dev_info *dev_info;
  406. struct segment_info *entry;
  407. if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
  408. return -EINVAL;
  409. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  410. down_write(&dcssblk_devices_sem);
  411. if (inbuf[0] == '1') {
  412. if (atomic_read(&dev_info->use_count) == 0) {
  413. // device is idle => we save immediately
  414. pr_info("All DCSSs that map to device %s are "
  415. "saved\n", dev_info->segment_name);
  416. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  417. if (entry->segment_type == SEG_TYPE_EN ||
  418. entry->segment_type == SEG_TYPE_SN)
  419. pr_warn("DCSS %s is of type SN or EN"
  420. " and cannot be saved\n",
  421. entry->segment_name);
  422. else
  423. segment_save(entry->segment_name);
  424. }
  425. } else {
  426. // device is busy => we save it when it becomes
  427. // idle in dcssblk_release
  428. pr_info("Device %s is in use, its DCSSs will be "
  429. "saved when it becomes idle\n",
  430. dev_info->segment_name);
  431. dev_info->save_pending = 1;
  432. }
  433. } else if (inbuf[0] == '0') {
  434. if (dev_info->save_pending) {
  435. // device is busy & the user wants to undo his save
  436. // request
  437. dev_info->save_pending = 0;
  438. pr_info("A pending save request for device %s "
  439. "has been canceled\n",
  440. dev_info->segment_name);
  441. }
  442. } else {
  443. up_write(&dcssblk_devices_sem);
  444. return -EINVAL;
  445. }
  446. up_write(&dcssblk_devices_sem);
  447. return count;
  448. }
  449. static DEVICE_ATTR(save, S_IWUSR | S_IRUSR, dcssblk_save_show,
  450. dcssblk_save_store);
  451. /*
  452. * device attribute for showing all segments in a device
  453. */
  454. static ssize_t
  455. dcssblk_seglist_show(struct device *dev, struct device_attribute *attr,
  456. char *buf)
  457. {
  458. int i;
  459. struct dcssblk_dev_info *dev_info;
  460. struct segment_info *entry;
  461. down_read(&dcssblk_devices_sem);
  462. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  463. i = 0;
  464. buf[0] = '\0';
  465. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  466. strcpy(&buf[i], entry->segment_name);
  467. i += strlen(entry->segment_name);
  468. buf[i] = '\n';
  469. i++;
  470. }
  471. up_read(&dcssblk_devices_sem);
  472. return i;
  473. }
  474. static DEVICE_ATTR(seglist, S_IRUSR, dcssblk_seglist_show, NULL);
  475. static struct attribute *dcssblk_dev_attrs[] = {
  476. &dev_attr_shared.attr,
  477. &dev_attr_save.attr,
  478. &dev_attr_seglist.attr,
  479. NULL,
  480. };
  481. static struct attribute_group dcssblk_dev_attr_group = {
  482. .attrs = dcssblk_dev_attrs,
  483. };
  484. static const struct attribute_group *dcssblk_dev_attr_groups[] = {
  485. &dcssblk_dev_attr_group,
  486. NULL,
  487. };
  488. /*
  489. * device attribute for adding devices
  490. */
  491. static ssize_t
  492. dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  493. {
  494. int rc, i, j, num_of_segments;
  495. struct dcssblk_dev_info *dev_info;
  496. struct segment_info *seg_info, *temp;
  497. char *local_buf;
  498. unsigned long seg_byte_size;
  499. dev_info = NULL;
  500. seg_info = NULL;
  501. if (dev != dcssblk_root_dev) {
  502. rc = -EINVAL;
  503. goto out_nobuf;
  504. }
  505. if ((count < 1) || (buf[0] == '\0') || (buf[0] == '\n')) {
  506. rc = -ENAMETOOLONG;
  507. goto out_nobuf;
  508. }
  509. local_buf = kmalloc(count + 1, GFP_KERNEL);
  510. if (local_buf == NULL) {
  511. rc = -ENOMEM;
  512. goto out_nobuf;
  513. }
  514. /*
  515. * parse input
  516. */
  517. num_of_segments = 0;
  518. for (i = 0; (i < count && (buf[i] != '\0') && (buf[i] != '\n')); i++) {
  519. for (j = i; j < count &&
  520. (buf[j] != ':') &&
  521. (buf[j] != '\0') &&
  522. (buf[j] != '\n'); j++) {
  523. local_buf[j-i] = toupper(buf[j]);
  524. }
  525. local_buf[j-i] = '\0';
  526. if (((j - i) == 0) || ((j - i) > 8)) {
  527. rc = -ENAMETOOLONG;
  528. goto seg_list_del;
  529. }
  530. rc = dcssblk_load_segment(local_buf, &seg_info);
  531. if (rc < 0)
  532. goto seg_list_del;
  533. /*
  534. * get a struct dcssblk_dev_info
  535. */
  536. if (num_of_segments == 0) {
  537. dev_info = kzalloc(sizeof(struct dcssblk_dev_info),
  538. GFP_KERNEL);
  539. if (dev_info == NULL) {
  540. rc = -ENOMEM;
  541. goto out;
  542. }
  543. strcpy(dev_info->segment_name, local_buf);
  544. dev_info->segment_type = seg_info->segment_type;
  545. INIT_LIST_HEAD(&dev_info->seg_list);
  546. }
  547. list_add_tail(&seg_info->lh, &dev_info->seg_list);
  548. num_of_segments++;
  549. i = j;
  550. if ((buf[j] == '\0') || (buf[j] == '\n'))
  551. break;
  552. }
  553. /* no trailing colon at the end of the input */
  554. if ((i > 0) && (buf[i-1] == ':')) {
  555. rc = -ENAMETOOLONG;
  556. goto seg_list_del;
  557. }
  558. strscpy(local_buf, buf, i + 1);
  559. dev_info->num_of_segments = num_of_segments;
  560. rc = dcssblk_is_continuous(dev_info);
  561. if (rc < 0)
  562. goto seg_list_del;
  563. dev_info->start = dcssblk_find_lowest_addr(dev_info);
  564. dev_info->end = dcssblk_find_highest_addr(dev_info);
  565. dev_set_name(&dev_info->dev, "%s", dev_info->segment_name);
  566. dev_info->dev.release = dcssblk_release_segment;
  567. dev_info->dev.groups = dcssblk_dev_attr_groups;
  568. INIT_LIST_HEAD(&dev_info->lh);
  569. dev_info->gd = blk_alloc_disk(NUMA_NO_NODE);
  570. if (dev_info->gd == NULL) {
  571. rc = -ENOMEM;
  572. goto seg_list_del;
  573. }
  574. dev_info->gd->major = dcssblk_major;
  575. dev_info->gd->minors = DCSSBLK_MINORS_PER_DISK;
  576. dev_info->gd->fops = &dcssblk_devops;
  577. dev_info->gd->private_data = dev_info;
  578. dev_info->gd->flags |= GENHD_FL_NO_PART;
  579. blk_queue_logical_block_size(dev_info->gd->queue, 4096);
  580. blk_queue_flag_set(QUEUE_FLAG_DAX, dev_info->gd->queue);
  581. seg_byte_size = (dev_info->end - dev_info->start + 1);
  582. set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
  583. pr_info("Loaded %s with total size %lu bytes and capacity %lu "
  584. "sectors\n", local_buf, seg_byte_size, seg_byte_size >> 9);
  585. dev_info->save_pending = 0;
  586. dev_info->is_shared = 1;
  587. dev_info->dev.parent = dcssblk_root_dev;
  588. /*
  589. *get minor, add to list
  590. */
  591. down_write(&dcssblk_devices_sem);
  592. if (dcssblk_get_segment_by_name(local_buf)) {
  593. rc = -EEXIST;
  594. goto release_gd;
  595. }
  596. rc = dcssblk_assign_free_minor(dev_info);
  597. if (rc)
  598. goto release_gd;
  599. sprintf(dev_info->gd->disk_name, "dcssblk%d",
  600. dev_info->gd->first_minor);
  601. list_add_tail(&dev_info->lh, &dcssblk_devices);
  602. if (!try_module_get(THIS_MODULE)) {
  603. rc = -ENODEV;
  604. goto dev_list_del;
  605. }
  606. /*
  607. * register the device
  608. */
  609. rc = device_register(&dev_info->dev);
  610. if (rc)
  611. goto put_dev;
  612. dev_info->dax_dev = alloc_dax(dev_info, &dcssblk_dax_ops);
  613. if (IS_ERR(dev_info->dax_dev)) {
  614. rc = PTR_ERR(dev_info->dax_dev);
  615. dev_info->dax_dev = NULL;
  616. goto put_dev;
  617. }
  618. set_dax_synchronous(dev_info->dax_dev);
  619. rc = dax_add_host(dev_info->dax_dev, dev_info->gd);
  620. if (rc)
  621. goto out_dax;
  622. get_device(&dev_info->dev);
  623. rc = device_add_disk(&dev_info->dev, dev_info->gd, NULL);
  624. if (rc)
  625. goto out_dax_host;
  626. switch (dev_info->segment_type) {
  627. case SEG_TYPE_SR:
  628. case SEG_TYPE_ER:
  629. case SEG_TYPE_SC:
  630. set_disk_ro(dev_info->gd,1);
  631. break;
  632. default:
  633. set_disk_ro(dev_info->gd,0);
  634. break;
  635. }
  636. up_write(&dcssblk_devices_sem);
  637. rc = count;
  638. goto out;
  639. out_dax_host:
  640. put_device(&dev_info->dev);
  641. dax_remove_host(dev_info->gd);
  642. out_dax:
  643. kill_dax(dev_info->dax_dev);
  644. put_dax(dev_info->dax_dev);
  645. put_dev:
  646. list_del(&dev_info->lh);
  647. put_disk(dev_info->gd);
  648. list_for_each_entry(seg_info, &dev_info->seg_list, lh) {
  649. segment_unload(seg_info->segment_name);
  650. }
  651. put_device(&dev_info->dev);
  652. up_write(&dcssblk_devices_sem);
  653. goto out;
  654. dev_list_del:
  655. list_del(&dev_info->lh);
  656. release_gd:
  657. put_disk(dev_info->gd);
  658. up_write(&dcssblk_devices_sem);
  659. seg_list_del:
  660. if (dev_info == NULL)
  661. goto out;
  662. list_for_each_entry_safe(seg_info, temp, &dev_info->seg_list, lh) {
  663. list_del(&seg_info->lh);
  664. segment_unload(seg_info->segment_name);
  665. kfree(seg_info);
  666. }
  667. kfree(dev_info);
  668. out:
  669. kfree(local_buf);
  670. out_nobuf:
  671. return rc;
  672. }
  673. /*
  674. * device attribute for removing devices
  675. */
  676. static ssize_t
  677. dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  678. {
  679. struct dcssblk_dev_info *dev_info;
  680. struct segment_info *entry;
  681. int rc, i;
  682. char *local_buf;
  683. if (dev != dcssblk_root_dev) {
  684. return -EINVAL;
  685. }
  686. local_buf = kmalloc(count + 1, GFP_KERNEL);
  687. if (local_buf == NULL) {
  688. return -ENOMEM;
  689. }
  690. /*
  691. * parse input
  692. */
  693. for (i = 0; (i < count && (*(buf+i)!='\0') && (*(buf+i)!='\n')); i++) {
  694. local_buf[i] = toupper(buf[i]);
  695. }
  696. local_buf[i] = '\0';
  697. if ((i == 0) || (i > 8)) {
  698. rc = -ENAMETOOLONG;
  699. goto out_buf;
  700. }
  701. down_write(&dcssblk_devices_sem);
  702. dev_info = dcssblk_get_device_by_name(local_buf);
  703. if (dev_info == NULL) {
  704. up_write(&dcssblk_devices_sem);
  705. pr_warn("Device %s cannot be removed because it is not a known device\n",
  706. local_buf);
  707. rc = -ENODEV;
  708. goto out_buf;
  709. }
  710. if (atomic_read(&dev_info->use_count) != 0) {
  711. up_write(&dcssblk_devices_sem);
  712. pr_warn("Device %s cannot be removed while it is in use\n",
  713. local_buf);
  714. rc = -EBUSY;
  715. goto out_buf;
  716. }
  717. list_del(&dev_info->lh);
  718. dax_remove_host(dev_info->gd);
  719. kill_dax(dev_info->dax_dev);
  720. put_dax(dev_info->dax_dev);
  721. del_gendisk(dev_info->gd);
  722. put_disk(dev_info->gd);
  723. /* unload all related segments */
  724. list_for_each_entry(entry, &dev_info->seg_list, lh)
  725. segment_unload(entry->segment_name);
  726. up_write(&dcssblk_devices_sem);
  727. device_unregister(&dev_info->dev);
  728. put_device(&dev_info->dev);
  729. rc = count;
  730. out_buf:
  731. kfree(local_buf);
  732. return rc;
  733. }
  734. static int
  735. dcssblk_open(struct block_device *bdev, fmode_t mode)
  736. {
  737. struct dcssblk_dev_info *dev_info;
  738. int rc;
  739. dev_info = bdev->bd_disk->private_data;
  740. if (NULL == dev_info) {
  741. rc = -ENODEV;
  742. goto out;
  743. }
  744. atomic_inc(&dev_info->use_count);
  745. rc = 0;
  746. out:
  747. return rc;
  748. }
  749. static void
  750. dcssblk_release(struct gendisk *disk, fmode_t mode)
  751. {
  752. struct dcssblk_dev_info *dev_info = disk->private_data;
  753. struct segment_info *entry;
  754. if (!dev_info) {
  755. WARN_ON(1);
  756. return;
  757. }
  758. down_write(&dcssblk_devices_sem);
  759. if (atomic_dec_and_test(&dev_info->use_count)
  760. && (dev_info->save_pending)) {
  761. pr_info("Device %s has become idle and is being saved "
  762. "now\n", dev_info->segment_name);
  763. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  764. if (entry->segment_type == SEG_TYPE_EN ||
  765. entry->segment_type == SEG_TYPE_SN)
  766. pr_warn("DCSS %s is of type SN or EN and cannot"
  767. " be saved\n", entry->segment_name);
  768. else
  769. segment_save(entry->segment_name);
  770. }
  771. dev_info->save_pending = 0;
  772. }
  773. up_write(&dcssblk_devices_sem);
  774. }
  775. static void
  776. dcssblk_submit_bio(struct bio *bio)
  777. {
  778. struct dcssblk_dev_info *dev_info;
  779. struct bio_vec bvec;
  780. struct bvec_iter iter;
  781. unsigned long index;
  782. unsigned long page_addr;
  783. unsigned long source_addr;
  784. unsigned long bytes_done;
  785. bio = bio_split_to_limits(bio);
  786. if (!bio)
  787. return;
  788. bytes_done = 0;
  789. dev_info = bio->bi_bdev->bd_disk->private_data;
  790. if (dev_info == NULL)
  791. goto fail;
  792. if ((bio->bi_iter.bi_sector & 7) != 0 ||
  793. (bio->bi_iter.bi_size & 4095) != 0)
  794. /* Request is not page-aligned. */
  795. goto fail;
  796. /* verify data transfer direction */
  797. if (dev_info->is_shared) {
  798. switch (dev_info->segment_type) {
  799. case SEG_TYPE_SR:
  800. case SEG_TYPE_ER:
  801. case SEG_TYPE_SC:
  802. /* cannot write to these segments */
  803. if (bio_data_dir(bio) == WRITE) {
  804. pr_warn("Writing to %s failed because it is a read-only device\n",
  805. dev_name(&dev_info->dev));
  806. goto fail;
  807. }
  808. }
  809. }
  810. index = (bio->bi_iter.bi_sector >> 3);
  811. bio_for_each_segment(bvec, bio, iter) {
  812. page_addr = (unsigned long)bvec_virt(&bvec);
  813. source_addr = dev_info->start + (index<<12) + bytes_done;
  814. if (unlikely((page_addr & 4095) != 0) || (bvec.bv_len & 4095) != 0)
  815. // More paranoia.
  816. goto fail;
  817. if (bio_data_dir(bio) == READ) {
  818. memcpy((void*)page_addr, (void*)source_addr,
  819. bvec.bv_len);
  820. } else {
  821. memcpy((void*)source_addr, (void*)page_addr,
  822. bvec.bv_len);
  823. }
  824. bytes_done += bvec.bv_len;
  825. }
  826. bio_endio(bio);
  827. return;
  828. fail:
  829. bio_io_error(bio);
  830. }
  831. static long
  832. __dcssblk_direct_access(struct dcssblk_dev_info *dev_info, pgoff_t pgoff,
  833. long nr_pages, void **kaddr, pfn_t *pfn)
  834. {
  835. resource_size_t offset = pgoff * PAGE_SIZE;
  836. unsigned long dev_sz;
  837. dev_sz = dev_info->end - dev_info->start + 1;
  838. if (kaddr)
  839. *kaddr = (void *) dev_info->start + offset;
  840. if (pfn)
  841. *pfn = __pfn_to_pfn_t(PFN_DOWN(dev_info->start + offset),
  842. PFN_DEV|PFN_SPECIAL);
  843. return (dev_sz - offset) / PAGE_SIZE;
  844. }
  845. static long
  846. dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
  847. long nr_pages, enum dax_access_mode mode, void **kaddr,
  848. pfn_t *pfn)
  849. {
  850. struct dcssblk_dev_info *dev_info = dax_get_private(dax_dev);
  851. return __dcssblk_direct_access(dev_info, pgoff, nr_pages, kaddr, pfn);
  852. }
  853. static void
  854. dcssblk_check_params(void)
  855. {
  856. int rc, i, j, k;
  857. char buf[DCSSBLK_PARM_LEN + 1];
  858. struct dcssblk_dev_info *dev_info;
  859. for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
  860. i++) {
  861. for (j = i; (j < DCSSBLK_PARM_LEN) &&
  862. (dcssblk_segments[j] != ',') &&
  863. (dcssblk_segments[j] != '\0') &&
  864. (dcssblk_segments[j] != '('); j++)
  865. {
  866. buf[j-i] = dcssblk_segments[j];
  867. }
  868. buf[j-i] = '\0';
  869. rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
  870. if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
  871. for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++)
  872. buf[k] = toupper(buf[k]);
  873. buf[k] = '\0';
  874. if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
  875. down_read(&dcssblk_devices_sem);
  876. dev_info = dcssblk_get_device_by_name(buf);
  877. up_read(&dcssblk_devices_sem);
  878. if (dev_info)
  879. dcssblk_shared_store(&dev_info->dev,
  880. NULL, "0\n", 2);
  881. }
  882. }
  883. while ((dcssblk_segments[j] != ',') &&
  884. (dcssblk_segments[j] != '\0'))
  885. {
  886. j++;
  887. }
  888. if (dcssblk_segments[j] == '\0')
  889. break;
  890. i = j;
  891. }
  892. }
  893. /*
  894. * The init/exit functions.
  895. */
  896. static void __exit
  897. dcssblk_exit(void)
  898. {
  899. root_device_unregister(dcssblk_root_dev);
  900. unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
  901. }
  902. static int __init
  903. dcssblk_init(void)
  904. {
  905. int rc;
  906. dcssblk_root_dev = root_device_register("dcssblk");
  907. if (IS_ERR(dcssblk_root_dev))
  908. return PTR_ERR(dcssblk_root_dev);
  909. rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
  910. if (rc)
  911. goto out_root;
  912. rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
  913. if (rc)
  914. goto out_root;
  915. rc = register_blkdev(0, DCSSBLK_NAME);
  916. if (rc < 0)
  917. goto out_root;
  918. dcssblk_major = rc;
  919. init_rwsem(&dcssblk_devices_sem);
  920. dcssblk_check_params();
  921. return 0;
  922. out_root:
  923. root_device_unregister(dcssblk_root_dev);
  924. return rc;
  925. }
  926. module_init(dcssblk_init);
  927. module_exit(dcssblk_exit);
  928. module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
  929. MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
  930. "comma-separated list, names in each set separated "
  931. "by commas are separated by colons, each set contains "
  932. "names of contiguous segments and each name max. 8 chars.\n"
  933. "Adding \"(local)\" to the end of each set equals echoing 0 "
  934. "to /sys/devices/dcssblk/<device name>/shared after loading "
  935. "the contiguous segments - \n"
  936. "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
  937. MODULE_LICENSE("GPL");