dcssblk.c 21 KB

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