dcssblk.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /*
  2. * dcssblk.c -- the S/390 block driver for dcss memory
  3. *
  4. * Authors: Carsten Otte, Stefan Weinhuber, Gerald Schaefer
  5. */
  6. #include <linux/module.h>
  7. #include <linux/moduleparam.h>
  8. #include <linux/ctype.h>
  9. #include <linux/errno.h>
  10. #include <linux/init.h>
  11. #include <linux/slab.h>
  12. #include <linux/blkdev.h>
  13. #include <asm/extmem.h>
  14. #include <asm/io.h>
  15. #include <linux/completion.h>
  16. #include <linux/interrupt.h>
  17. #include <asm/ccwdev.h> // for s390_root_dev_(un)register()
  18. //#define DCSSBLK_DEBUG /* Debug messages on/off */
  19. #define DCSSBLK_NAME "dcssblk"
  20. #define DCSSBLK_MINORS_PER_DISK 1
  21. #define DCSSBLK_PARM_LEN 400
  22. #ifdef DCSSBLK_DEBUG
  23. #define PRINT_DEBUG(x...) printk(KERN_DEBUG DCSSBLK_NAME " debug: " x)
  24. #else
  25. #define PRINT_DEBUG(x...) do {} while (0)
  26. #endif
  27. #define PRINT_INFO(x...) printk(KERN_INFO DCSSBLK_NAME " info: " x)
  28. #define PRINT_WARN(x...) printk(KERN_WARNING DCSSBLK_NAME " warning: " x)
  29. #define PRINT_ERR(x...) printk(KERN_ERR DCSSBLK_NAME " error: " x)
  30. static int dcssblk_open(struct inode *inode, struct file *filp);
  31. static int dcssblk_release(struct inode *inode, struct file *filp);
  32. static int dcssblk_make_request(struct request_queue *q, struct bio *bio);
  33. static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0";
  34. static int dcssblk_major;
  35. static struct block_device_operations dcssblk_devops = {
  36. .owner = THIS_MODULE,
  37. .open = dcssblk_open,
  38. .release = dcssblk_release,
  39. };
  40. static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
  41. size_t count);
  42. static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
  43. size_t count);
  44. static ssize_t dcssblk_save_store(struct device * dev, struct device_attribute *attr, const char * buf,
  45. size_t count);
  46. static ssize_t dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf);
  47. static ssize_t dcssblk_shared_store(struct device * dev, struct device_attribute *attr, const char * buf,
  48. size_t count);
  49. static ssize_t dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf);
  50. static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
  51. static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
  52. static DEVICE_ATTR(save, S_IWUSR | S_IRUGO, dcssblk_save_show,
  53. dcssblk_save_store);
  54. static DEVICE_ATTR(shared, S_IWUSR | S_IRUGO, dcssblk_shared_show,
  55. dcssblk_shared_store);
  56. static struct device *dcssblk_root_dev;
  57. struct dcssblk_dev_info {
  58. struct list_head lh;
  59. struct device dev;
  60. char segment_name[BUS_ID_SIZE];
  61. atomic_t use_count;
  62. struct gendisk *gd;
  63. unsigned long start;
  64. unsigned long end;
  65. int segment_type;
  66. unsigned char save_pending;
  67. unsigned char is_shared;
  68. struct request_queue *dcssblk_queue;
  69. };
  70. static struct list_head dcssblk_devices = LIST_HEAD_INIT(dcssblk_devices);
  71. static struct rw_semaphore dcssblk_devices_sem;
  72. /*
  73. * release function for segment device.
  74. */
  75. static void
  76. dcssblk_release_segment(struct device *dev)
  77. {
  78. PRINT_DEBUG("segment release fn called for %s\n", dev->bus_id);
  79. kfree(container_of(dev, struct dcssblk_dev_info, dev));
  80. module_put(THIS_MODULE);
  81. }
  82. /*
  83. * get a minor number. needs to be called with
  84. * down_write(&dcssblk_devices_sem) and the
  85. * device needs to be enqueued before the semaphore is
  86. * freed.
  87. */
  88. static inline int
  89. dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info)
  90. {
  91. int minor, found;
  92. struct dcssblk_dev_info *entry;
  93. if (dev_info == NULL)
  94. return -EINVAL;
  95. for (minor = 0; minor < (1<<MINORBITS); minor++) {
  96. found = 0;
  97. // test if minor available
  98. list_for_each_entry(entry, &dcssblk_devices, lh)
  99. if (minor == entry->gd->first_minor)
  100. found++;
  101. if (!found) break; // got unused minor
  102. }
  103. if (found)
  104. return -EBUSY;
  105. dev_info->gd->first_minor = minor;
  106. return 0;
  107. }
  108. /*
  109. * get the struct dcssblk_dev_info from dcssblk_devices
  110. * for the given name.
  111. * down_read(&dcssblk_devices_sem) must be held.
  112. */
  113. static struct dcssblk_dev_info *
  114. dcssblk_get_device_by_name(char *name)
  115. {
  116. struct dcssblk_dev_info *entry;
  117. list_for_each_entry(entry, &dcssblk_devices, lh) {
  118. if (!strcmp(name, entry->segment_name)) {
  119. return entry;
  120. }
  121. }
  122. return NULL;
  123. }
  124. /*
  125. * print appropriate error message for segment_load()/segment_type()
  126. * return code
  127. */
  128. static void
  129. dcssblk_segment_warn(int rc, char* seg_name)
  130. {
  131. switch (rc) {
  132. case -ENOENT:
  133. PRINT_WARN("cannot load/query segment %s, does not exist\n",
  134. seg_name);
  135. break;
  136. case -ENOSYS:
  137. PRINT_WARN("cannot load/query segment %s, not running on VM\n",
  138. seg_name);
  139. break;
  140. case -EIO:
  141. PRINT_WARN("cannot load/query segment %s, hardware error\n",
  142. seg_name);
  143. break;
  144. case -ENOTSUPP:
  145. PRINT_WARN("cannot load/query segment %s, is a multi-part "
  146. "segment\n", seg_name);
  147. break;
  148. case -ENOSPC:
  149. PRINT_WARN("cannot load/query segment %s, overlaps with "
  150. "storage\n", seg_name);
  151. break;
  152. case -EBUSY:
  153. PRINT_WARN("cannot load/query segment %s, overlaps with "
  154. "already loaded dcss\n", seg_name);
  155. break;
  156. case -EPERM:
  157. PRINT_WARN("cannot load/query segment %s, already loaded in "
  158. "incompatible mode\n", seg_name);
  159. break;
  160. case -ENOMEM:
  161. PRINT_WARN("cannot load/query segment %s, out of memory\n",
  162. seg_name);
  163. break;
  164. case -ERANGE:
  165. PRINT_WARN("cannot load/query segment %s, exceeds kernel "
  166. "mapping range\n", seg_name);
  167. break;
  168. default:
  169. PRINT_WARN("cannot load/query segment %s, return value %i\n",
  170. seg_name, rc);
  171. break;
  172. }
  173. }
  174. /*
  175. * device attribute for switching shared/nonshared (exclusive)
  176. * operation (show + store)
  177. */
  178. static ssize_t
  179. dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
  180. {
  181. struct dcssblk_dev_info *dev_info;
  182. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  183. return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
  184. }
  185. static ssize_t
  186. dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
  187. {
  188. struct dcssblk_dev_info *dev_info;
  189. int rc;
  190. if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0')) {
  191. PRINT_WARN("Invalid value, must be 0 or 1\n");
  192. return -EINVAL;
  193. }
  194. down_write(&dcssblk_devices_sem);
  195. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  196. if (atomic_read(&dev_info->use_count)) {
  197. PRINT_ERR("share: segment %s is busy!\n",
  198. dev_info->segment_name);
  199. rc = -EBUSY;
  200. goto out;
  201. }
  202. if (inbuf[0] == '1') {
  203. // reload segment in shared mode
  204. rc = segment_modify_shared(dev_info->segment_name,
  205. SEGMENT_SHARED);
  206. if (rc < 0) {
  207. BUG_ON(rc == -EINVAL);
  208. if (rc == -EIO || rc == -ENOENT)
  209. goto removeseg;
  210. } else {
  211. dev_info->is_shared = 1;
  212. switch (dev_info->segment_type) {
  213. case SEG_TYPE_SR:
  214. case SEG_TYPE_ER:
  215. case SEG_TYPE_SC:
  216. set_disk_ro(dev_info->gd,1);
  217. }
  218. }
  219. } else if (inbuf[0] == '0') {
  220. // reload segment in exclusive mode
  221. if (dev_info->segment_type == SEG_TYPE_SC) {
  222. PRINT_ERR("Segment type SC (%s) cannot be loaded in "
  223. "non-shared mode\n", dev_info->segment_name);
  224. rc = -EINVAL;
  225. goto out;
  226. }
  227. rc = segment_modify_shared(dev_info->segment_name,
  228. SEGMENT_EXCLUSIVE);
  229. if (rc < 0) {
  230. BUG_ON(rc == -EINVAL);
  231. if (rc == -EIO || rc == -ENOENT)
  232. goto removeseg;
  233. } else {
  234. dev_info->is_shared = 0;
  235. set_disk_ro(dev_info->gd, 0);
  236. }
  237. } else {
  238. PRINT_WARN("Invalid value, must be 0 or 1\n");
  239. rc = -EINVAL;
  240. goto out;
  241. }
  242. rc = count;
  243. goto out;
  244. removeseg:
  245. PRINT_ERR("Could not reload segment %s, removing it now!\n",
  246. dev_info->segment_name);
  247. list_del(&dev_info->lh);
  248. del_gendisk(dev_info->gd);
  249. blk_put_queue(dev_info->dcssblk_queue);
  250. dev_info->gd->queue = NULL;
  251. put_disk(dev_info->gd);
  252. device_unregister(dev);
  253. put_device(dev);
  254. out:
  255. up_write(&dcssblk_devices_sem);
  256. return rc;
  257. }
  258. /*
  259. * device attribute for save operation on current copy
  260. * of the segment. If the segment is busy, saving will
  261. * become pending until it gets released, which can be
  262. * undone by storing a non-true value to this entry.
  263. * (show + store)
  264. */
  265. static ssize_t
  266. dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
  267. {
  268. struct dcssblk_dev_info *dev_info;
  269. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  270. return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
  271. }
  272. static ssize_t
  273. dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
  274. {
  275. struct dcssblk_dev_info *dev_info;
  276. if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0')) {
  277. PRINT_WARN("Invalid value, must be 0 or 1\n");
  278. return -EINVAL;
  279. }
  280. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  281. down_write(&dcssblk_devices_sem);
  282. if (inbuf[0] == '1') {
  283. if (atomic_read(&dev_info->use_count) == 0) {
  284. // device is idle => we save immediately
  285. PRINT_INFO("Saving segment %s\n",
  286. dev_info->segment_name);
  287. segment_save(dev_info->segment_name);
  288. } else {
  289. // device is busy => we save it when it becomes
  290. // idle in dcssblk_release
  291. PRINT_INFO("Segment %s is currently busy, it will "
  292. "be saved when it becomes idle...\n",
  293. dev_info->segment_name);
  294. dev_info->save_pending = 1;
  295. }
  296. } else if (inbuf[0] == '0') {
  297. if (dev_info->save_pending) {
  298. // device is busy & the user wants to undo his save
  299. // request
  300. dev_info->save_pending = 0;
  301. PRINT_INFO("Pending save for segment %s deactivated\n",
  302. dev_info->segment_name);
  303. }
  304. } else {
  305. up_write(&dcssblk_devices_sem);
  306. PRINT_WARN("Invalid value, must be 0 or 1\n");
  307. return -EINVAL;
  308. }
  309. up_write(&dcssblk_devices_sem);
  310. return count;
  311. }
  312. /*
  313. * device attribute for adding devices
  314. */
  315. static ssize_t
  316. dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  317. {
  318. int rc, i;
  319. struct dcssblk_dev_info *dev_info;
  320. char *local_buf;
  321. unsigned long seg_byte_size;
  322. dev_info = NULL;
  323. if (dev != dcssblk_root_dev) {
  324. rc = -EINVAL;
  325. goto out_nobuf;
  326. }
  327. local_buf = kmalloc(count + 1, GFP_KERNEL);
  328. if (local_buf == NULL) {
  329. rc = -ENOMEM;
  330. goto out_nobuf;
  331. }
  332. /*
  333. * parse input
  334. */
  335. for (i = 0; ((buf[i] != '\0') && (buf[i] != '\n') && i < count); i++) {
  336. local_buf[i] = toupper(buf[i]);
  337. }
  338. local_buf[i] = '\0';
  339. if ((i == 0) || (i > 8)) {
  340. rc = -ENAMETOOLONG;
  341. goto out;
  342. }
  343. /*
  344. * already loaded?
  345. */
  346. down_read(&dcssblk_devices_sem);
  347. dev_info = dcssblk_get_device_by_name(local_buf);
  348. up_read(&dcssblk_devices_sem);
  349. if (dev_info != NULL) {
  350. PRINT_WARN("Segment %s already loaded!\n", local_buf);
  351. rc = -EEXIST;
  352. goto out;
  353. }
  354. /*
  355. * get a struct dcssblk_dev_info
  356. */
  357. dev_info = kmalloc(sizeof(struct dcssblk_dev_info), GFP_KERNEL);
  358. if (dev_info == NULL) {
  359. rc = -ENOMEM;
  360. goto out;
  361. }
  362. memset(dev_info, 0, sizeof(struct dcssblk_dev_info));
  363. strcpy(dev_info->segment_name, local_buf);
  364. strlcpy(dev_info->dev.bus_id, local_buf, BUS_ID_SIZE);
  365. dev_info->dev.release = dcssblk_release_segment;
  366. INIT_LIST_HEAD(&dev_info->lh);
  367. dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
  368. if (dev_info->gd == NULL) {
  369. rc = -ENOMEM;
  370. goto free_dev_info;
  371. }
  372. dev_info->gd->major = dcssblk_major;
  373. dev_info->gd->fops = &dcssblk_devops;
  374. dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL);
  375. dev_info->gd->queue = dev_info->dcssblk_queue;
  376. dev_info->gd->private_data = dev_info;
  377. dev_info->gd->driverfs_dev = &dev_info->dev;
  378. /*
  379. * load the segment
  380. */
  381. rc = segment_load(local_buf, SEGMENT_SHARED,
  382. &dev_info->start, &dev_info->end);
  383. if (rc < 0) {
  384. dcssblk_segment_warn(rc, dev_info->segment_name);
  385. goto dealloc_gendisk;
  386. }
  387. seg_byte_size = (dev_info->end - dev_info->start + 1);
  388. set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
  389. PRINT_INFO("Loaded segment %s, size = %lu Byte, "
  390. "capacity = %lu (512 Byte) sectors\n", local_buf,
  391. seg_byte_size, seg_byte_size >> 9);
  392. dev_info->segment_type = rc;
  393. dev_info->save_pending = 0;
  394. dev_info->is_shared = 1;
  395. dev_info->dev.parent = dcssblk_root_dev;
  396. /*
  397. * get minor, add to list
  398. */
  399. down_write(&dcssblk_devices_sem);
  400. rc = dcssblk_assign_free_minor(dev_info);
  401. if (rc) {
  402. up_write(&dcssblk_devices_sem);
  403. PRINT_ERR("No free minor number available! "
  404. "Unloading segment...\n");
  405. goto unload_seg;
  406. }
  407. sprintf(dev_info->gd->disk_name, "dcssblk%d",
  408. dev_info->gd->first_minor);
  409. list_add_tail(&dev_info->lh, &dcssblk_devices);
  410. if (!try_module_get(THIS_MODULE)) {
  411. rc = -ENODEV;
  412. goto list_del;
  413. }
  414. /*
  415. * register the device
  416. */
  417. rc = device_register(&dev_info->dev);
  418. if (rc) {
  419. PRINT_ERR("Segment %s could not be registered RC=%d\n",
  420. local_buf, rc);
  421. module_put(THIS_MODULE);
  422. goto list_del;
  423. }
  424. get_device(&dev_info->dev);
  425. rc = device_create_file(&dev_info->dev, &dev_attr_shared);
  426. if (rc)
  427. goto unregister_dev;
  428. rc = device_create_file(&dev_info->dev, &dev_attr_save);
  429. if (rc)
  430. goto unregister_dev;
  431. add_disk(dev_info->gd);
  432. blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
  433. blk_queue_hardsect_size(dev_info->dcssblk_queue, 4096);
  434. switch (dev_info->segment_type) {
  435. case SEG_TYPE_SR:
  436. case SEG_TYPE_ER:
  437. case SEG_TYPE_SC:
  438. set_disk_ro(dev_info->gd,1);
  439. break;
  440. default:
  441. set_disk_ro(dev_info->gd,0);
  442. break;
  443. }
  444. PRINT_DEBUG("Segment %s loaded successfully\n", local_buf);
  445. up_write(&dcssblk_devices_sem);
  446. rc = count;
  447. goto out;
  448. unregister_dev:
  449. PRINT_ERR("device_create_file() failed!\n");
  450. list_del(&dev_info->lh);
  451. blk_put_queue(dev_info->dcssblk_queue);
  452. dev_info->gd->queue = NULL;
  453. put_disk(dev_info->gd);
  454. device_unregister(&dev_info->dev);
  455. segment_unload(dev_info->segment_name);
  456. put_device(&dev_info->dev);
  457. up_write(&dcssblk_devices_sem);
  458. goto out;
  459. list_del:
  460. list_del(&dev_info->lh);
  461. up_write(&dcssblk_devices_sem);
  462. unload_seg:
  463. segment_unload(local_buf);
  464. dealloc_gendisk:
  465. blk_put_queue(dev_info->dcssblk_queue);
  466. dev_info->gd->queue = NULL;
  467. put_disk(dev_info->gd);
  468. free_dev_info:
  469. kfree(dev_info);
  470. out:
  471. kfree(local_buf);
  472. out_nobuf:
  473. return rc;
  474. }
  475. /*
  476. * device attribute for removing devices
  477. */
  478. static ssize_t
  479. dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  480. {
  481. struct dcssblk_dev_info *dev_info;
  482. int rc, i;
  483. char *local_buf;
  484. if (dev != dcssblk_root_dev) {
  485. return -EINVAL;
  486. }
  487. local_buf = kmalloc(count + 1, GFP_KERNEL);
  488. if (local_buf == NULL) {
  489. return -ENOMEM;
  490. }
  491. /*
  492. * parse input
  493. */
  494. for (i = 0; ((*(buf+i)!='\0') && (*(buf+i)!='\n') && i < count); i++) {
  495. local_buf[i] = toupper(buf[i]);
  496. }
  497. local_buf[i] = '\0';
  498. if ((i == 0) || (i > 8)) {
  499. rc = -ENAMETOOLONG;
  500. goto out_buf;
  501. }
  502. down_write(&dcssblk_devices_sem);
  503. dev_info = dcssblk_get_device_by_name(local_buf);
  504. if (dev_info == NULL) {
  505. up_write(&dcssblk_devices_sem);
  506. PRINT_WARN("Segment %s is not loaded!\n", local_buf);
  507. rc = -ENODEV;
  508. goto out_buf;
  509. }
  510. if (atomic_read(&dev_info->use_count) != 0) {
  511. up_write(&dcssblk_devices_sem);
  512. PRINT_WARN("Segment %s is in use!\n", local_buf);
  513. rc = -EBUSY;
  514. goto out_buf;
  515. }
  516. list_del(&dev_info->lh);
  517. del_gendisk(dev_info->gd);
  518. blk_put_queue(dev_info->dcssblk_queue);
  519. dev_info->gd->queue = NULL;
  520. put_disk(dev_info->gd);
  521. device_unregister(&dev_info->dev);
  522. segment_unload(dev_info->segment_name);
  523. PRINT_DEBUG("Segment %s unloaded successfully\n",
  524. dev_info->segment_name);
  525. put_device(&dev_info->dev);
  526. up_write(&dcssblk_devices_sem);
  527. rc = count;
  528. out_buf:
  529. kfree(local_buf);
  530. return rc;
  531. }
  532. static int
  533. dcssblk_open(struct inode *inode, struct file *filp)
  534. {
  535. struct dcssblk_dev_info *dev_info;
  536. int rc;
  537. dev_info = inode->i_bdev->bd_disk->private_data;
  538. if (NULL == dev_info) {
  539. rc = -ENODEV;
  540. goto out;
  541. }
  542. atomic_inc(&dev_info->use_count);
  543. inode->i_bdev->bd_block_size = 4096;
  544. rc = 0;
  545. out:
  546. return rc;
  547. }
  548. static int
  549. dcssblk_release(struct inode *inode, struct file *filp)
  550. {
  551. struct dcssblk_dev_info *dev_info;
  552. int rc;
  553. dev_info = inode->i_bdev->bd_disk->private_data;
  554. if (NULL == dev_info) {
  555. rc = -ENODEV;
  556. goto out;
  557. }
  558. down_write(&dcssblk_devices_sem);
  559. if (atomic_dec_and_test(&dev_info->use_count)
  560. && (dev_info->save_pending)) {
  561. PRINT_INFO("Segment %s became idle and is being saved now\n",
  562. dev_info->segment_name);
  563. segment_save(dev_info->segment_name);
  564. dev_info->save_pending = 0;
  565. }
  566. up_write(&dcssblk_devices_sem);
  567. rc = 0;
  568. out:
  569. return rc;
  570. }
  571. static int
  572. dcssblk_make_request(request_queue_t *q, struct bio *bio)
  573. {
  574. struct dcssblk_dev_info *dev_info;
  575. struct bio_vec *bvec;
  576. unsigned long index;
  577. unsigned long page_addr;
  578. unsigned long source_addr;
  579. unsigned long bytes_done;
  580. int i;
  581. bytes_done = 0;
  582. dev_info = bio->bi_bdev->bd_disk->private_data;
  583. if (dev_info == NULL)
  584. goto fail;
  585. if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
  586. /* Request is not page-aligned. */
  587. goto fail;
  588. if (((bio->bi_size >> 9) + bio->bi_sector)
  589. > get_capacity(bio->bi_bdev->bd_disk)) {
  590. /* Request beyond end of DCSS segment. */
  591. goto fail;
  592. }
  593. index = (bio->bi_sector >> 3);
  594. bio_for_each_segment(bvec, bio, i) {
  595. page_addr = (unsigned long)
  596. page_address(bvec->bv_page) + bvec->bv_offset;
  597. source_addr = dev_info->start + (index<<12) + bytes_done;
  598. if (unlikely(page_addr & 4095) != 0 || (bvec->bv_len & 4095) != 0)
  599. // More paranoia.
  600. goto fail;
  601. if (bio_data_dir(bio) == READ) {
  602. memcpy((void*)page_addr, (void*)source_addr,
  603. bvec->bv_len);
  604. } else {
  605. memcpy((void*)source_addr, (void*)page_addr,
  606. bvec->bv_len);
  607. }
  608. bytes_done += bvec->bv_len;
  609. }
  610. bio_endio(bio, bytes_done, 0);
  611. return 0;
  612. fail:
  613. bio_io_error(bio, bytes_done);
  614. return 0;
  615. }
  616. static void
  617. dcssblk_check_params(void)
  618. {
  619. int rc, i, j, k;
  620. char buf[9];
  621. struct dcssblk_dev_info *dev_info;
  622. for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
  623. i++) {
  624. for (j = i; (dcssblk_segments[j] != ',') &&
  625. (dcssblk_segments[j] != '\0') &&
  626. (dcssblk_segments[j] != '(') &&
  627. (j - i) < 8; j++)
  628. {
  629. buf[j-i] = dcssblk_segments[j];
  630. }
  631. buf[j-i] = '\0';
  632. rc = dcssblk_add_store(dcssblk_root_dev, buf, j-i);
  633. if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
  634. for (k = 0; buf[k] != '\0'; k++)
  635. buf[k] = toupper(buf[k]);
  636. if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
  637. down_read(&dcssblk_devices_sem);
  638. dev_info = dcssblk_get_device_by_name(buf);
  639. up_read(&dcssblk_devices_sem);
  640. if (dev_info)
  641. dcssblk_shared_store(&dev_info->dev,
  642. "0\n", 2);
  643. }
  644. }
  645. while ((dcssblk_segments[j] != ',') &&
  646. (dcssblk_segments[j] != '\0'))
  647. {
  648. j++;
  649. }
  650. if (dcssblk_segments[j] == '\0')
  651. break;
  652. i = j;
  653. }
  654. }
  655. /*
  656. * The init/exit functions.
  657. */
  658. static void __exit
  659. dcssblk_exit(void)
  660. {
  661. int rc;
  662. PRINT_DEBUG("DCSSBLOCK EXIT...\n");
  663. s390_root_dev_unregister(dcssblk_root_dev);
  664. rc = unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
  665. if (rc) {
  666. PRINT_ERR("unregister_blkdev() failed!\n");
  667. }
  668. PRINT_DEBUG("...finished!\n");
  669. }
  670. static int __init
  671. dcssblk_init(void)
  672. {
  673. int rc;
  674. PRINT_DEBUG("DCSSBLOCK INIT...\n");
  675. dcssblk_root_dev = s390_root_dev_register("dcssblk");
  676. if (IS_ERR(dcssblk_root_dev)) {
  677. PRINT_ERR("device_register() failed!\n");
  678. return PTR_ERR(dcssblk_root_dev);
  679. }
  680. rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
  681. if (rc) {
  682. PRINT_ERR("device_create_file(add) failed!\n");
  683. s390_root_dev_unregister(dcssblk_root_dev);
  684. return rc;
  685. }
  686. rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
  687. if (rc) {
  688. PRINT_ERR("device_create_file(remove) failed!\n");
  689. s390_root_dev_unregister(dcssblk_root_dev);
  690. return rc;
  691. }
  692. rc = register_blkdev(0, DCSSBLK_NAME);
  693. if (rc < 0) {
  694. PRINT_ERR("Can't get dynamic major!\n");
  695. s390_root_dev_unregister(dcssblk_root_dev);
  696. return rc;
  697. }
  698. dcssblk_major = rc;
  699. init_rwsem(&dcssblk_devices_sem);
  700. dcssblk_check_params();
  701. PRINT_DEBUG("...finished!\n");
  702. return 0;
  703. }
  704. module_init(dcssblk_init);
  705. module_exit(dcssblk_exit);
  706. module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
  707. MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
  708. "comma-separated list, each name max. 8 chars.\n"
  709. "Adding \"(local)\" to segment name equals echoing 0 to "
  710. "/sys/devices/dcssblk/<segment name>/shared after loading "
  711. "the segment - \n"
  712. "e.g. segments=\"mydcss1,mydcss2,mydcss3(local)\"");
  713. MODULE_LICENSE("GPL");