dcssblk.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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 = kzalloc(
  212. sizeof(struct segment_info) * dev_info->num_of_segments,
  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);
  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. *kaddr = (void *) dev_info->start + offset;
  838. *pfn = __pfn_to_pfn_t(PFN_DOWN(dev_info->start + offset),
  839. PFN_DEV|PFN_SPECIAL);
  840. return (dev_sz - offset) / PAGE_SIZE;
  841. }
  842. static long
  843. dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
  844. long nr_pages, void **kaddr, pfn_t *pfn)
  845. {
  846. struct dcssblk_dev_info *dev_info = dax_get_private(dax_dev);
  847. return __dcssblk_direct_access(dev_info, pgoff, nr_pages, kaddr, pfn);
  848. }
  849. static void
  850. dcssblk_check_params(void)
  851. {
  852. int rc, i, j, k;
  853. char buf[DCSSBLK_PARM_LEN + 1];
  854. struct dcssblk_dev_info *dev_info;
  855. for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
  856. i++) {
  857. for (j = i; (j < DCSSBLK_PARM_LEN) &&
  858. (dcssblk_segments[j] != ',') &&
  859. (dcssblk_segments[j] != '\0') &&
  860. (dcssblk_segments[j] != '('); j++)
  861. {
  862. buf[j-i] = dcssblk_segments[j];
  863. }
  864. buf[j-i] = '\0';
  865. rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
  866. if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
  867. for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++)
  868. buf[k] = toupper(buf[k]);
  869. buf[k] = '\0';
  870. if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
  871. down_read(&dcssblk_devices_sem);
  872. dev_info = dcssblk_get_device_by_name(buf);
  873. up_read(&dcssblk_devices_sem);
  874. if (dev_info)
  875. dcssblk_shared_store(&dev_info->dev,
  876. NULL, "0\n", 2);
  877. }
  878. }
  879. while ((dcssblk_segments[j] != ',') &&
  880. (dcssblk_segments[j] != '\0'))
  881. {
  882. j++;
  883. }
  884. if (dcssblk_segments[j] == '\0')
  885. break;
  886. i = j;
  887. }
  888. }
  889. /*
  890. * Suspend / Resume
  891. */
  892. static int dcssblk_freeze(struct device *dev)
  893. {
  894. struct dcssblk_dev_info *dev_info;
  895. int rc = 0;
  896. list_for_each_entry(dev_info, &dcssblk_devices, lh) {
  897. switch (dev_info->segment_type) {
  898. case SEG_TYPE_SR:
  899. case SEG_TYPE_ER:
  900. case SEG_TYPE_SC:
  901. if (!dev_info->is_shared)
  902. rc = -EINVAL;
  903. break;
  904. default:
  905. rc = -EINVAL;
  906. break;
  907. }
  908. if (rc)
  909. break;
  910. }
  911. if (rc)
  912. pr_err("Suspending the system failed because DCSS device %s "
  913. "is writable\n",
  914. dev_info->segment_name);
  915. return rc;
  916. }
  917. static int dcssblk_restore(struct device *dev)
  918. {
  919. struct dcssblk_dev_info *dev_info;
  920. struct segment_info *entry;
  921. unsigned long start, end;
  922. int rc = 0;
  923. list_for_each_entry(dev_info, &dcssblk_devices, lh) {
  924. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  925. segment_unload(entry->segment_name);
  926. rc = segment_load(entry->segment_name, SEGMENT_SHARED,
  927. &start, &end);
  928. if (rc < 0) {
  929. // TODO in_use check ?
  930. segment_warning(rc, entry->segment_name);
  931. goto out_panic;
  932. }
  933. if (start != entry->start || end != entry->end) {
  934. pr_err("The address range of DCSS %s changed "
  935. "while the system was suspended\n",
  936. entry->segment_name);
  937. goto out_panic;
  938. }
  939. }
  940. }
  941. return 0;
  942. out_panic:
  943. panic("fatal dcssblk resume error\n");
  944. }
  945. static int dcssblk_thaw(struct device *dev)
  946. {
  947. return 0;
  948. }
  949. static const struct dev_pm_ops dcssblk_pm_ops = {
  950. .freeze = dcssblk_freeze,
  951. .thaw = dcssblk_thaw,
  952. .restore = dcssblk_restore,
  953. };
  954. static struct platform_driver dcssblk_pdrv = {
  955. .driver = {
  956. .name = "dcssblk",
  957. .pm = &dcssblk_pm_ops,
  958. },
  959. };
  960. static struct platform_device *dcssblk_pdev;
  961. /*
  962. * The init/exit functions.
  963. */
  964. static void __exit
  965. dcssblk_exit(void)
  966. {
  967. platform_device_unregister(dcssblk_pdev);
  968. platform_driver_unregister(&dcssblk_pdrv);
  969. root_device_unregister(dcssblk_root_dev);
  970. unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
  971. }
  972. static int __init
  973. dcssblk_init(void)
  974. {
  975. int rc;
  976. rc = platform_driver_register(&dcssblk_pdrv);
  977. if (rc)
  978. return rc;
  979. dcssblk_pdev = platform_device_register_simple("dcssblk", -1, NULL,
  980. 0);
  981. if (IS_ERR(dcssblk_pdev)) {
  982. rc = PTR_ERR(dcssblk_pdev);
  983. goto out_pdrv;
  984. }
  985. dcssblk_root_dev = root_device_register("dcssblk");
  986. if (IS_ERR(dcssblk_root_dev)) {
  987. rc = PTR_ERR(dcssblk_root_dev);
  988. goto out_pdev;
  989. }
  990. rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
  991. if (rc)
  992. goto out_root;
  993. rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
  994. if (rc)
  995. goto out_root;
  996. rc = register_blkdev(0, DCSSBLK_NAME);
  997. if (rc < 0)
  998. goto out_root;
  999. dcssblk_major = rc;
  1000. init_rwsem(&dcssblk_devices_sem);
  1001. dcssblk_check_params();
  1002. return 0;
  1003. out_root:
  1004. root_device_unregister(dcssblk_root_dev);
  1005. out_pdev:
  1006. platform_device_unregister(dcssblk_pdev);
  1007. out_pdrv:
  1008. platform_driver_unregister(&dcssblk_pdrv);
  1009. return rc;
  1010. }
  1011. module_init(dcssblk_init);
  1012. module_exit(dcssblk_exit);
  1013. module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
  1014. MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
  1015. "comma-separated list, names in each set separated "
  1016. "by commas are separated by colons, each set contains "
  1017. "names of contiguous segments and each name max. 8 chars.\n"
  1018. "Adding \"(local)\" to the end of each set equals echoing 0 "
  1019. "to /sys/devices/dcssblk/<device name>/shared after loading "
  1020. "the contiguous segments - \n"
  1021. "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
  1022. MODULE_LICENSE("GPL");