dcssblk.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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_make_request(struct request_queue *q,
  31. struct bio *bio);
  32. static long dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
  33. long nr_pages, void **kaddr, pfn_t *pfn);
  34. static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0";
  35. static int dcssblk_major;
  36. static const struct block_device_operations dcssblk_devops = {
  37. .owner = THIS_MODULE,
  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 const struct dax_operations dcssblk_dax_ops = {
  52. .direct_access = dcssblk_dax_direct_access,
  53. .copy_from_iter = dcssblk_dax_copy_from_iter,
  54. .copy_to_iter = dcssblk_dax_copy_to_iter,
  55. };
  56. struct dcssblk_dev_info {
  57. struct list_head lh;
  58. struct device dev;
  59. char segment_name[DCSS_BUS_ID_SIZE];
  60. atomic_t use_count;
  61. struct gendisk *gd;
  62. unsigned long start;
  63. unsigned long end;
  64. int segment_type;
  65. unsigned char save_pending;
  66. unsigned char is_shared;
  67. struct request_queue *dcssblk_queue;
  68. int num_of_segments;
  69. struct list_head seg_list;
  70. struct dax_device *dax_dev;
  71. };
  72. struct segment_info {
  73. struct list_head lh;
  74. char segment_name[DCSS_BUS_ID_SIZE];
  75. unsigned long start;
  76. unsigned long end;
  77. int segment_type;
  78. };
  79. static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
  80. size_t count);
  81. static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
  82. size_t count);
  83. static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
  84. static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
  85. static struct device *dcssblk_root_dev;
  86. static LIST_HEAD(dcssblk_devices);
  87. static struct rw_semaphore dcssblk_devices_sem;
  88. /*
  89. * release function for segment device.
  90. */
  91. static void
  92. dcssblk_release_segment(struct device *dev)
  93. {
  94. struct dcssblk_dev_info *dev_info;
  95. struct segment_info *entry, *temp;
  96. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  97. list_for_each_entry_safe(entry, temp, &dev_info->seg_list, lh) {
  98. list_del(&entry->lh);
  99. kfree(entry);
  100. }
  101. kfree(dev_info);
  102. module_put(THIS_MODULE);
  103. }
  104. /*
  105. * get a minor number. needs to be called with
  106. * down_write(&dcssblk_devices_sem) and the
  107. * device needs to be enqueued before the semaphore is
  108. * freed.
  109. */
  110. static int
  111. dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info)
  112. {
  113. int minor, found;
  114. struct dcssblk_dev_info *entry;
  115. if (dev_info == NULL)
  116. return -EINVAL;
  117. for (minor = 0; minor < (1<<MINORBITS); minor++) {
  118. found = 0;
  119. // test if minor available
  120. list_for_each_entry(entry, &dcssblk_devices, lh)
  121. if (minor == entry->gd->first_minor)
  122. found++;
  123. if (!found) break; // got unused minor
  124. }
  125. if (found)
  126. return -EBUSY;
  127. dev_info->gd->first_minor = minor;
  128. return 0;
  129. }
  130. /*
  131. * get the struct dcssblk_dev_info from dcssblk_devices
  132. * for the given name.
  133. * down_read(&dcssblk_devices_sem) must be held.
  134. */
  135. static struct dcssblk_dev_info *
  136. dcssblk_get_device_by_name(char *name)
  137. {
  138. struct dcssblk_dev_info *entry;
  139. list_for_each_entry(entry, &dcssblk_devices, lh) {
  140. if (!strcmp(name, entry->segment_name)) {
  141. return entry;
  142. }
  143. }
  144. return NULL;
  145. }
  146. /*
  147. * get the struct segment_info from seg_list
  148. * for the given name.
  149. * down_read(&dcssblk_devices_sem) must be held.
  150. */
  151. static struct segment_info *
  152. dcssblk_get_segment_by_name(char *name)
  153. {
  154. struct dcssblk_dev_info *dev_info;
  155. struct segment_info *entry;
  156. list_for_each_entry(dev_info, &dcssblk_devices, lh) {
  157. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  158. if (!strcmp(name, entry->segment_name))
  159. return entry;
  160. }
  161. }
  162. return NULL;
  163. }
  164. /*
  165. * get the highest address of the multi-segment block.
  166. */
  167. static unsigned long
  168. dcssblk_find_highest_addr(struct dcssblk_dev_info *dev_info)
  169. {
  170. unsigned long highest_addr;
  171. struct segment_info *entry;
  172. highest_addr = 0;
  173. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  174. if (highest_addr < entry->end)
  175. highest_addr = entry->end;
  176. }
  177. return highest_addr;
  178. }
  179. /*
  180. * get the lowest address of the multi-segment block.
  181. */
  182. static unsigned long
  183. dcssblk_find_lowest_addr(struct dcssblk_dev_info *dev_info)
  184. {
  185. int set_first;
  186. unsigned long lowest_addr;
  187. struct segment_info *entry;
  188. set_first = 0;
  189. lowest_addr = 0;
  190. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  191. if (set_first == 0) {
  192. lowest_addr = entry->start;
  193. set_first = 1;
  194. } else {
  195. if (lowest_addr > entry->start)
  196. lowest_addr = entry->start;
  197. }
  198. }
  199. return lowest_addr;
  200. }
  201. /*
  202. * Check continuity of segments.
  203. */
  204. static int
  205. dcssblk_is_continuous(struct dcssblk_dev_info *dev_info)
  206. {
  207. int i, j, rc;
  208. struct segment_info *sort_list, *entry, temp;
  209. if (dev_info->num_of_segments <= 1)
  210. return 0;
  211. sort_list = kcalloc(dev_info->num_of_segments,
  212. sizeof(struct segment_info),
  213. GFP_KERNEL);
  214. if (sort_list == NULL)
  215. return -ENOMEM;
  216. i = 0;
  217. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  218. memcpy(&sort_list[i], entry, sizeof(struct segment_info));
  219. i++;
  220. }
  221. /* sort segments */
  222. for (i = 0; i < dev_info->num_of_segments; i++)
  223. for (j = 0; j < dev_info->num_of_segments; j++)
  224. if (sort_list[j].start > sort_list[i].start) {
  225. memcpy(&temp, &sort_list[i],
  226. sizeof(struct segment_info));
  227. memcpy(&sort_list[i], &sort_list[j],
  228. sizeof(struct segment_info));
  229. memcpy(&sort_list[j], &temp,
  230. sizeof(struct segment_info));
  231. }
  232. /* check continuity */
  233. for (i = 0; i < dev_info->num_of_segments - 1; i++) {
  234. if ((sort_list[i].end + 1) != sort_list[i+1].start) {
  235. pr_err("Adjacent DCSSs %s and %s are not "
  236. "contiguous\n", sort_list[i].segment_name,
  237. sort_list[i+1].segment_name);
  238. rc = -EINVAL;
  239. goto out;
  240. }
  241. /* EN and EW are allowed in a block device */
  242. if (sort_list[i].segment_type != sort_list[i+1].segment_type) {
  243. if (!(sort_list[i].segment_type & SEGMENT_EXCLUSIVE) ||
  244. (sort_list[i].segment_type == SEG_TYPE_ER) ||
  245. !(sort_list[i+1].segment_type &
  246. SEGMENT_EXCLUSIVE) ||
  247. (sort_list[i+1].segment_type == SEG_TYPE_ER)) {
  248. pr_err("DCSS %s and DCSS %s have "
  249. "incompatible types\n",
  250. sort_list[i].segment_name,
  251. sort_list[i+1].segment_name);
  252. rc = -EINVAL;
  253. goto out;
  254. }
  255. }
  256. }
  257. rc = 0;
  258. out:
  259. kfree(sort_list);
  260. return rc;
  261. }
  262. /*
  263. * Load a segment
  264. */
  265. static int
  266. dcssblk_load_segment(char *name, struct segment_info **seg_info)
  267. {
  268. int rc;
  269. /* already loaded? */
  270. down_read(&dcssblk_devices_sem);
  271. *seg_info = dcssblk_get_segment_by_name(name);
  272. up_read(&dcssblk_devices_sem);
  273. if (*seg_info != NULL)
  274. return -EEXIST;
  275. /* get a struct segment_info */
  276. *seg_info = kzalloc(sizeof(struct segment_info), GFP_KERNEL);
  277. if (*seg_info == NULL)
  278. return -ENOMEM;
  279. strcpy((*seg_info)->segment_name, name);
  280. /* load the segment */
  281. rc = segment_load(name, SEGMENT_SHARED,
  282. &(*seg_info)->start, &(*seg_info)->end);
  283. if (rc < 0) {
  284. segment_warning(rc, (*seg_info)->segment_name);
  285. kfree(*seg_info);
  286. } else {
  287. INIT_LIST_HEAD(&(*seg_info)->lh);
  288. (*seg_info)->segment_type = rc;
  289. }
  290. return rc;
  291. }
  292. /*
  293. * device attribute for switching shared/nonshared (exclusive)
  294. * operation (show + store)
  295. */
  296. static ssize_t
  297. dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
  298. {
  299. struct dcssblk_dev_info *dev_info;
  300. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  301. return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
  302. }
  303. static ssize_t
  304. dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
  305. {
  306. struct dcssblk_dev_info *dev_info;
  307. struct segment_info *entry, *temp;
  308. int rc;
  309. if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
  310. return -EINVAL;
  311. down_write(&dcssblk_devices_sem);
  312. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  313. if (atomic_read(&dev_info->use_count)) {
  314. rc = -EBUSY;
  315. goto out;
  316. }
  317. if (inbuf[0] == '1') {
  318. /* reload segments in shared mode */
  319. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  320. rc = segment_modify_shared(entry->segment_name,
  321. SEGMENT_SHARED);
  322. if (rc < 0) {
  323. BUG_ON(rc == -EINVAL);
  324. if (rc != -EAGAIN)
  325. goto removeseg;
  326. }
  327. }
  328. dev_info->is_shared = 1;
  329. switch (dev_info->segment_type) {
  330. case SEG_TYPE_SR:
  331. case SEG_TYPE_ER:
  332. case SEG_TYPE_SC:
  333. set_disk_ro(dev_info->gd, 1);
  334. }
  335. } else if (inbuf[0] == '0') {
  336. /* reload segments in exclusive mode */
  337. if (dev_info->segment_type == SEG_TYPE_SC) {
  338. pr_err("DCSS %s is of type SC and cannot be "
  339. "loaded as exclusive-writable\n",
  340. dev_info->segment_name);
  341. rc = -EINVAL;
  342. goto out;
  343. }
  344. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  345. rc = segment_modify_shared(entry->segment_name,
  346. SEGMENT_EXCLUSIVE);
  347. if (rc < 0) {
  348. BUG_ON(rc == -EINVAL);
  349. if (rc != -EAGAIN)
  350. goto removeseg;
  351. }
  352. }
  353. dev_info->is_shared = 0;
  354. set_disk_ro(dev_info->gd, 0);
  355. } else {
  356. rc = -EINVAL;
  357. goto out;
  358. }
  359. rc = count;
  360. goto out;
  361. removeseg:
  362. pr_err("DCSS device %s is removed after a failed access mode "
  363. "change\n", dev_info->segment_name);
  364. temp = entry;
  365. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  366. if (entry != temp)
  367. segment_unload(entry->segment_name);
  368. }
  369. list_del(&dev_info->lh);
  370. kill_dax(dev_info->dax_dev);
  371. put_dax(dev_info->dax_dev);
  372. del_gendisk(dev_info->gd);
  373. blk_cleanup_queue(dev_info->dcssblk_queue);
  374. dev_info->gd->queue = NULL;
  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. strlcpy(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 = alloc_disk(DCSSBLK_MINORS_PER_DISK);
  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->fops = &dcssblk_devops;
  576. dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL);
  577. dev_info->gd->queue = dev_info->dcssblk_queue;
  578. dev_info->gd->private_data = dev_info;
  579. blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
  580. blk_queue_logical_block_size(dev_info->dcssblk_queue, 4096);
  581. blk_queue_flag_set(QUEUE_FLAG_DAX, dev_info->dcssblk_queue);
  582. seg_byte_size = (dev_info->end - dev_info->start + 1);
  583. set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
  584. pr_info("Loaded %s with total size %lu bytes and capacity %lu "
  585. "sectors\n", local_buf, seg_byte_size, seg_byte_size >> 9);
  586. dev_info->save_pending = 0;
  587. dev_info->is_shared = 1;
  588. dev_info->dev.parent = dcssblk_root_dev;
  589. /*
  590. *get minor, add to list
  591. */
  592. down_write(&dcssblk_devices_sem);
  593. if (dcssblk_get_segment_by_name(local_buf)) {
  594. rc = -EEXIST;
  595. goto release_gd;
  596. }
  597. rc = dcssblk_assign_free_minor(dev_info);
  598. if (rc)
  599. goto release_gd;
  600. sprintf(dev_info->gd->disk_name, "dcssblk%d",
  601. dev_info->gd->first_minor);
  602. list_add_tail(&dev_info->lh, &dcssblk_devices);
  603. if (!try_module_get(THIS_MODULE)) {
  604. rc = -ENODEV;
  605. goto dev_list_del;
  606. }
  607. /*
  608. * register the device
  609. */
  610. rc = device_register(&dev_info->dev);
  611. if (rc)
  612. goto put_dev;
  613. dev_info->dax_dev = alloc_dax(dev_info, dev_info->gd->disk_name,
  614. &dcssblk_dax_ops);
  615. if (!dev_info->dax_dev) {
  616. rc = -ENOMEM;
  617. goto put_dev;
  618. }
  619. get_device(&dev_info->dev);
  620. device_add_disk(&dev_info->dev, dev_info->gd, NULL);
  621. switch (dev_info->segment_type) {
  622. case SEG_TYPE_SR:
  623. case SEG_TYPE_ER:
  624. case SEG_TYPE_SC:
  625. set_disk_ro(dev_info->gd,1);
  626. break;
  627. default:
  628. set_disk_ro(dev_info->gd,0);
  629. break;
  630. }
  631. up_write(&dcssblk_devices_sem);
  632. rc = count;
  633. goto out;
  634. put_dev:
  635. list_del(&dev_info->lh);
  636. blk_cleanup_queue(dev_info->dcssblk_queue);
  637. dev_info->gd->queue = NULL;
  638. put_disk(dev_info->gd);
  639. list_for_each_entry(seg_info, &dev_info->seg_list, lh) {
  640. segment_unload(seg_info->segment_name);
  641. }
  642. put_device(&dev_info->dev);
  643. up_write(&dcssblk_devices_sem);
  644. goto out;
  645. dev_list_del:
  646. list_del(&dev_info->lh);
  647. release_gd:
  648. blk_cleanup_queue(dev_info->dcssblk_queue);
  649. dev_info->gd->queue = NULL;
  650. put_disk(dev_info->gd);
  651. up_write(&dcssblk_devices_sem);
  652. seg_list_del:
  653. if (dev_info == NULL)
  654. goto out;
  655. list_for_each_entry_safe(seg_info, temp, &dev_info->seg_list, lh) {
  656. list_del(&seg_info->lh);
  657. segment_unload(seg_info->segment_name);
  658. kfree(seg_info);
  659. }
  660. kfree(dev_info);
  661. out:
  662. kfree(local_buf);
  663. out_nobuf:
  664. return rc;
  665. }
  666. /*
  667. * device attribute for removing devices
  668. */
  669. static ssize_t
  670. dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  671. {
  672. struct dcssblk_dev_info *dev_info;
  673. struct segment_info *entry;
  674. int rc, i;
  675. char *local_buf;
  676. if (dev != dcssblk_root_dev) {
  677. return -EINVAL;
  678. }
  679. local_buf = kmalloc(count + 1, GFP_KERNEL);
  680. if (local_buf == NULL) {
  681. return -ENOMEM;
  682. }
  683. /*
  684. * parse input
  685. */
  686. for (i = 0; (i < count && (*(buf+i)!='\0') && (*(buf+i)!='\n')); i++) {
  687. local_buf[i] = toupper(buf[i]);
  688. }
  689. local_buf[i] = '\0';
  690. if ((i == 0) || (i > 8)) {
  691. rc = -ENAMETOOLONG;
  692. goto out_buf;
  693. }
  694. down_write(&dcssblk_devices_sem);
  695. dev_info = dcssblk_get_device_by_name(local_buf);
  696. if (dev_info == NULL) {
  697. up_write(&dcssblk_devices_sem);
  698. pr_warn("Device %s cannot be removed because it is not a known device\n",
  699. local_buf);
  700. rc = -ENODEV;
  701. goto out_buf;
  702. }
  703. if (atomic_read(&dev_info->use_count) != 0) {
  704. up_write(&dcssblk_devices_sem);
  705. pr_warn("Device %s cannot be removed while it is in use\n",
  706. local_buf);
  707. rc = -EBUSY;
  708. goto out_buf;
  709. }
  710. list_del(&dev_info->lh);
  711. kill_dax(dev_info->dax_dev);
  712. put_dax(dev_info->dax_dev);
  713. del_gendisk(dev_info->gd);
  714. blk_cleanup_queue(dev_info->dcssblk_queue);
  715. dev_info->gd->queue = NULL;
  716. put_disk(dev_info->gd);
  717. /* unload all related segments */
  718. list_for_each_entry(entry, &dev_info->seg_list, lh)
  719. segment_unload(entry->segment_name);
  720. up_write(&dcssblk_devices_sem);
  721. device_unregister(&dev_info->dev);
  722. put_device(&dev_info->dev);
  723. rc = count;
  724. out_buf:
  725. kfree(local_buf);
  726. return rc;
  727. }
  728. static int
  729. dcssblk_open(struct block_device *bdev, fmode_t mode)
  730. {
  731. struct dcssblk_dev_info *dev_info;
  732. int rc;
  733. dev_info = bdev->bd_disk->private_data;
  734. if (NULL == dev_info) {
  735. rc = -ENODEV;
  736. goto out;
  737. }
  738. atomic_inc(&dev_info->use_count);
  739. bdev->bd_block_size = 4096;
  740. rc = 0;
  741. out:
  742. return rc;
  743. }
  744. static void
  745. dcssblk_release(struct gendisk *disk, fmode_t mode)
  746. {
  747. struct dcssblk_dev_info *dev_info = disk->private_data;
  748. struct segment_info *entry;
  749. if (!dev_info) {
  750. WARN_ON(1);
  751. return;
  752. }
  753. down_write(&dcssblk_devices_sem);
  754. if (atomic_dec_and_test(&dev_info->use_count)
  755. && (dev_info->save_pending)) {
  756. pr_info("Device %s has become idle and is being saved "
  757. "now\n", dev_info->segment_name);
  758. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  759. if (entry->segment_type == SEG_TYPE_EN ||
  760. entry->segment_type == SEG_TYPE_SN)
  761. pr_warn("DCSS %s is of type SN or EN and cannot"
  762. " be saved\n", entry->segment_name);
  763. else
  764. segment_save(entry->segment_name);
  765. }
  766. dev_info->save_pending = 0;
  767. }
  768. up_write(&dcssblk_devices_sem);
  769. }
  770. static blk_qc_t
  771. dcssblk_make_request(struct request_queue *q, struct bio *bio)
  772. {
  773. struct dcssblk_dev_info *dev_info;
  774. struct bio_vec bvec;
  775. struct bvec_iter iter;
  776. unsigned long index;
  777. unsigned long page_addr;
  778. unsigned long source_addr;
  779. unsigned long bytes_done;
  780. blk_queue_split(q, &bio);
  781. bytes_done = 0;
  782. dev_info = bio->bi_disk->private_data;
  783. if (dev_info == NULL)
  784. goto fail;
  785. if ((bio->bi_iter.bi_sector & 7) != 0 ||
  786. (bio->bi_iter.bi_size & 4095) != 0)
  787. /* Request is not page-aligned. */
  788. goto fail;
  789. if (bio_end_sector(bio) > get_capacity(bio->bi_disk)) {
  790. /* Request beyond end of DCSS segment. */
  791. goto fail;
  792. }
  793. /* verify data transfer direction */
  794. if (dev_info->is_shared) {
  795. switch (dev_info->segment_type) {
  796. case SEG_TYPE_SR:
  797. case SEG_TYPE_ER:
  798. case SEG_TYPE_SC:
  799. /* cannot write to these segments */
  800. if (bio_data_dir(bio) == WRITE) {
  801. pr_warn("Writing to %s failed because it is a read-only device\n",
  802. dev_name(&dev_info->dev));
  803. goto fail;
  804. }
  805. }
  806. }
  807. index = (bio->bi_iter.bi_sector >> 3);
  808. bio_for_each_segment(bvec, bio, iter) {
  809. page_addr = (unsigned long)
  810. page_address(bvec.bv_page) + bvec.bv_offset;
  811. source_addr = dev_info->start + (index<<12) + bytes_done;
  812. if (unlikely((page_addr & 4095) != 0) || (bvec.bv_len & 4095) != 0)
  813. // More paranoia.
  814. goto fail;
  815. if (bio_data_dir(bio) == READ) {
  816. memcpy((void*)page_addr, (void*)source_addr,
  817. bvec.bv_len);
  818. } else {
  819. memcpy((void*)source_addr, (void*)page_addr,
  820. bvec.bv_len);
  821. }
  822. bytes_done += bvec.bv_len;
  823. }
  824. bio_endio(bio);
  825. return BLK_QC_T_NONE;
  826. fail:
  827. bio_io_error(bio);
  828. return BLK_QC_T_NONE;
  829. }
  830. static long
  831. __dcssblk_direct_access(struct dcssblk_dev_info *dev_info, pgoff_t pgoff,
  832. long nr_pages, void **kaddr, pfn_t *pfn)
  833. {
  834. resource_size_t offset = pgoff * PAGE_SIZE;
  835. unsigned long dev_sz;
  836. dev_sz = dev_info->end - dev_info->start + 1;
  837. if (kaddr)
  838. *kaddr = (void *) dev_info->start + offset;
  839. if (pfn)
  840. *pfn = __pfn_to_pfn_t(PFN_DOWN(dev_info->start + offset),
  841. PFN_DEV|PFN_SPECIAL);
  842. return (dev_sz - offset) / PAGE_SIZE;
  843. }
  844. static long
  845. dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
  846. long nr_pages, void **kaddr, pfn_t *pfn)
  847. {
  848. struct dcssblk_dev_info *dev_info = dax_get_private(dax_dev);
  849. return __dcssblk_direct_access(dev_info, pgoff, nr_pages, kaddr, pfn);
  850. }
  851. static void
  852. dcssblk_check_params(void)
  853. {
  854. int rc, i, j, k;
  855. char buf[DCSSBLK_PARM_LEN + 1];
  856. struct dcssblk_dev_info *dev_info;
  857. for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
  858. i++) {
  859. for (j = i; (j < DCSSBLK_PARM_LEN) &&
  860. (dcssblk_segments[j] != ',') &&
  861. (dcssblk_segments[j] != '\0') &&
  862. (dcssblk_segments[j] != '('); j++)
  863. {
  864. buf[j-i] = dcssblk_segments[j];
  865. }
  866. buf[j-i] = '\0';
  867. rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
  868. if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
  869. for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++)
  870. buf[k] = toupper(buf[k]);
  871. buf[k] = '\0';
  872. if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
  873. down_read(&dcssblk_devices_sem);
  874. dev_info = dcssblk_get_device_by_name(buf);
  875. up_read(&dcssblk_devices_sem);
  876. if (dev_info)
  877. dcssblk_shared_store(&dev_info->dev,
  878. NULL, "0\n", 2);
  879. }
  880. }
  881. while ((dcssblk_segments[j] != ',') &&
  882. (dcssblk_segments[j] != '\0'))
  883. {
  884. j++;
  885. }
  886. if (dcssblk_segments[j] == '\0')
  887. break;
  888. i = j;
  889. }
  890. }
  891. /*
  892. * Suspend / Resume
  893. */
  894. static int dcssblk_freeze(struct device *dev)
  895. {
  896. struct dcssblk_dev_info *dev_info;
  897. int rc = 0;
  898. list_for_each_entry(dev_info, &dcssblk_devices, lh) {
  899. switch (dev_info->segment_type) {
  900. case SEG_TYPE_SR:
  901. case SEG_TYPE_ER:
  902. case SEG_TYPE_SC:
  903. if (!dev_info->is_shared)
  904. rc = -EINVAL;
  905. break;
  906. default:
  907. rc = -EINVAL;
  908. break;
  909. }
  910. if (rc)
  911. break;
  912. }
  913. if (rc)
  914. pr_err("Suspending the system failed because DCSS device %s "
  915. "is writable\n",
  916. dev_info->segment_name);
  917. return rc;
  918. }
  919. static int dcssblk_restore(struct device *dev)
  920. {
  921. struct dcssblk_dev_info *dev_info;
  922. struct segment_info *entry;
  923. unsigned long start, end;
  924. int rc = 0;
  925. list_for_each_entry(dev_info, &dcssblk_devices, lh) {
  926. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  927. segment_unload(entry->segment_name);
  928. rc = segment_load(entry->segment_name, SEGMENT_SHARED,
  929. &start, &end);
  930. if (rc < 0) {
  931. // TODO in_use check ?
  932. segment_warning(rc, entry->segment_name);
  933. goto out_panic;
  934. }
  935. if (start != entry->start || end != entry->end) {
  936. pr_err("The address range of DCSS %s changed "
  937. "while the system was suspended\n",
  938. entry->segment_name);
  939. goto out_panic;
  940. }
  941. }
  942. }
  943. return 0;
  944. out_panic:
  945. panic("fatal dcssblk resume error\n");
  946. }
  947. static int dcssblk_thaw(struct device *dev)
  948. {
  949. return 0;
  950. }
  951. static const struct dev_pm_ops dcssblk_pm_ops = {
  952. .freeze = dcssblk_freeze,
  953. .thaw = dcssblk_thaw,
  954. .restore = dcssblk_restore,
  955. };
  956. static struct platform_driver dcssblk_pdrv = {
  957. .driver = {
  958. .name = "dcssblk",
  959. .pm = &dcssblk_pm_ops,
  960. },
  961. };
  962. static struct platform_device *dcssblk_pdev;
  963. /*
  964. * The init/exit functions.
  965. */
  966. static void __exit
  967. dcssblk_exit(void)
  968. {
  969. platform_device_unregister(dcssblk_pdev);
  970. platform_driver_unregister(&dcssblk_pdrv);
  971. root_device_unregister(dcssblk_root_dev);
  972. unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
  973. }
  974. static int __init
  975. dcssblk_init(void)
  976. {
  977. int rc;
  978. rc = platform_driver_register(&dcssblk_pdrv);
  979. if (rc)
  980. return rc;
  981. dcssblk_pdev = platform_device_register_simple("dcssblk", -1, NULL,
  982. 0);
  983. if (IS_ERR(dcssblk_pdev)) {
  984. rc = PTR_ERR(dcssblk_pdev);
  985. goto out_pdrv;
  986. }
  987. dcssblk_root_dev = root_device_register("dcssblk");
  988. if (IS_ERR(dcssblk_root_dev)) {
  989. rc = PTR_ERR(dcssblk_root_dev);
  990. goto out_pdev;
  991. }
  992. rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
  993. if (rc)
  994. goto out_root;
  995. rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
  996. if (rc)
  997. goto out_root;
  998. rc = register_blkdev(0, DCSSBLK_NAME);
  999. if (rc < 0)
  1000. goto out_root;
  1001. dcssblk_major = rc;
  1002. init_rwsem(&dcssblk_devices_sem);
  1003. dcssblk_check_params();
  1004. return 0;
  1005. out_root:
  1006. root_device_unregister(dcssblk_root_dev);
  1007. out_pdev:
  1008. platform_device_unregister(dcssblk_pdev);
  1009. out_pdrv:
  1010. platform_driver_unregister(&dcssblk_pdrv);
  1011. return rc;
  1012. }
  1013. module_init(dcssblk_init);
  1014. module_exit(dcssblk_exit);
  1015. module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
  1016. MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
  1017. "comma-separated list, names in each set separated "
  1018. "by commas are separated by colons, each set contains "
  1019. "names of contiguous segments and each name max. 8 chars.\n"
  1020. "Adding \"(local)\" to the end of each set equals echoing 0 "
  1021. "to /sys/devices/dcssblk/<device name>/shared after loading "
  1022. "the contiguous segments - \n"
  1023. "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
  1024. MODULE_LICENSE("GPL");