dcssblk.c 28 KB

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