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 LIST_HEAD(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. static void dcssblk_unregister_callback(struct device *dev)
  178. {
  179. device_unregister(dev);
  180. put_device(dev);
  181. }
  182. /*
  183. * device attribute for switching shared/nonshared (exclusive)
  184. * operation (show + store)
  185. */
  186. static ssize_t
  187. dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
  188. {
  189. struct dcssblk_dev_info *dev_info;
  190. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  191. return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
  192. }
  193. static ssize_t
  194. dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
  195. {
  196. struct dcssblk_dev_info *dev_info;
  197. int rc;
  198. if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0')) {
  199. PRINT_WARN("Invalid value, must be 0 or 1\n");
  200. return -EINVAL;
  201. }
  202. down_write(&dcssblk_devices_sem);
  203. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  204. if (atomic_read(&dev_info->use_count)) {
  205. PRINT_ERR("share: segment %s is busy!\n",
  206. dev_info->segment_name);
  207. rc = -EBUSY;
  208. goto out;
  209. }
  210. if (inbuf[0] == '1') {
  211. // reload segment in shared mode
  212. rc = segment_modify_shared(dev_info->segment_name,
  213. SEGMENT_SHARED);
  214. if (rc < 0) {
  215. BUG_ON(rc == -EINVAL);
  216. if (rc != -EAGAIN)
  217. goto removeseg;
  218. } else {
  219. dev_info->is_shared = 1;
  220. switch (dev_info->segment_type) {
  221. case SEG_TYPE_SR:
  222. case SEG_TYPE_ER:
  223. case SEG_TYPE_SC:
  224. set_disk_ro(dev_info->gd,1);
  225. }
  226. }
  227. } else if (inbuf[0] == '0') {
  228. // reload segment in exclusive mode
  229. if (dev_info->segment_type == SEG_TYPE_SC) {
  230. PRINT_ERR("Segment type SC (%s) cannot be loaded in "
  231. "non-shared mode\n", dev_info->segment_name);
  232. rc = -EINVAL;
  233. goto out;
  234. }
  235. rc = segment_modify_shared(dev_info->segment_name,
  236. SEGMENT_EXCLUSIVE);
  237. if (rc < 0) {
  238. BUG_ON(rc == -EINVAL);
  239. if (rc != -EAGAIN)
  240. goto removeseg;
  241. } else {
  242. dev_info->is_shared = 0;
  243. set_disk_ro(dev_info->gd, 0);
  244. }
  245. } else {
  246. PRINT_WARN("Invalid value, must be 0 or 1\n");
  247. rc = -EINVAL;
  248. goto out;
  249. }
  250. rc = count;
  251. goto out;
  252. removeseg:
  253. PRINT_ERR("Could not reload segment %s, removing it now!\n",
  254. dev_info->segment_name);
  255. list_del(&dev_info->lh);
  256. del_gendisk(dev_info->gd);
  257. blk_cleanup_queue(dev_info->dcssblk_queue);
  258. dev_info->gd->queue = NULL;
  259. put_disk(dev_info->gd);
  260. rc = device_schedule_callback(dev, dcssblk_unregister_callback);
  261. out:
  262. up_write(&dcssblk_devices_sem);
  263. return rc;
  264. }
  265. /*
  266. * device attribute for save operation on current copy
  267. * of the segment. If the segment is busy, saving will
  268. * become pending until it gets released, which can be
  269. * undone by storing a non-true value to this entry.
  270. * (show + store)
  271. */
  272. static ssize_t
  273. dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
  274. {
  275. struct dcssblk_dev_info *dev_info;
  276. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  277. return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
  278. }
  279. static ssize_t
  280. dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
  281. {
  282. struct dcssblk_dev_info *dev_info;
  283. if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0')) {
  284. PRINT_WARN("Invalid value, must be 0 or 1\n");
  285. return -EINVAL;
  286. }
  287. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  288. down_write(&dcssblk_devices_sem);
  289. if (inbuf[0] == '1') {
  290. if (atomic_read(&dev_info->use_count) == 0) {
  291. // device is idle => we save immediately
  292. PRINT_INFO("Saving segment %s\n",
  293. dev_info->segment_name);
  294. segment_save(dev_info->segment_name);
  295. } else {
  296. // device is busy => we save it when it becomes
  297. // idle in dcssblk_release
  298. PRINT_INFO("Segment %s is currently busy, it will "
  299. "be saved when it becomes idle...\n",
  300. dev_info->segment_name);
  301. dev_info->save_pending = 1;
  302. }
  303. } else if (inbuf[0] == '0') {
  304. if (dev_info->save_pending) {
  305. // device is busy & the user wants to undo his save
  306. // request
  307. dev_info->save_pending = 0;
  308. PRINT_INFO("Pending save for segment %s deactivated\n",
  309. dev_info->segment_name);
  310. }
  311. } else {
  312. up_write(&dcssblk_devices_sem);
  313. PRINT_WARN("Invalid value, must be 0 or 1\n");
  314. return -EINVAL;
  315. }
  316. up_write(&dcssblk_devices_sem);
  317. return count;
  318. }
  319. /*
  320. * device attribute for adding devices
  321. */
  322. static ssize_t
  323. dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  324. {
  325. int rc, i;
  326. struct dcssblk_dev_info *dev_info;
  327. char *local_buf;
  328. unsigned long seg_byte_size;
  329. dev_info = NULL;
  330. if (dev != dcssblk_root_dev) {
  331. rc = -EINVAL;
  332. goto out_nobuf;
  333. }
  334. local_buf = kmalloc(count + 1, GFP_KERNEL);
  335. if (local_buf == NULL) {
  336. rc = -ENOMEM;
  337. goto out_nobuf;
  338. }
  339. /*
  340. * parse input
  341. */
  342. for (i = 0; ((buf[i] != '\0') && (buf[i] != '\n') && i < count); i++) {
  343. local_buf[i] = toupper(buf[i]);
  344. }
  345. local_buf[i] = '\0';
  346. if ((i == 0) || (i > 8)) {
  347. rc = -ENAMETOOLONG;
  348. goto out;
  349. }
  350. /*
  351. * already loaded?
  352. */
  353. down_read(&dcssblk_devices_sem);
  354. dev_info = dcssblk_get_device_by_name(local_buf);
  355. up_read(&dcssblk_devices_sem);
  356. if (dev_info != NULL) {
  357. PRINT_WARN("Segment %s already loaded!\n", local_buf);
  358. rc = -EEXIST;
  359. goto out;
  360. }
  361. /*
  362. * get a struct dcssblk_dev_info
  363. */
  364. dev_info = kzalloc(sizeof(struct dcssblk_dev_info), GFP_KERNEL);
  365. if (dev_info == NULL) {
  366. rc = -ENOMEM;
  367. goto out;
  368. }
  369. strcpy(dev_info->segment_name, local_buf);
  370. strlcpy(dev_info->dev.bus_id, local_buf, BUS_ID_SIZE);
  371. dev_info->dev.release = dcssblk_release_segment;
  372. INIT_LIST_HEAD(&dev_info->lh);
  373. dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
  374. if (dev_info->gd == NULL) {
  375. rc = -ENOMEM;
  376. goto free_dev_info;
  377. }
  378. dev_info->gd->major = dcssblk_major;
  379. dev_info->gd->fops = &dcssblk_devops;
  380. dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL);
  381. dev_info->gd->queue = dev_info->dcssblk_queue;
  382. dev_info->gd->private_data = dev_info;
  383. dev_info->gd->driverfs_dev = &dev_info->dev;
  384. /*
  385. * load the segment
  386. */
  387. rc = segment_load(local_buf, SEGMENT_SHARED,
  388. &dev_info->start, &dev_info->end);
  389. if (rc < 0) {
  390. dcssblk_segment_warn(rc, dev_info->segment_name);
  391. goto dealloc_gendisk;
  392. }
  393. seg_byte_size = (dev_info->end - dev_info->start + 1);
  394. set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
  395. PRINT_INFO("Loaded segment %s, size = %lu Byte, "
  396. "capacity = %lu (512 Byte) sectors\n", local_buf,
  397. seg_byte_size, seg_byte_size >> 9);
  398. dev_info->segment_type = rc;
  399. dev_info->save_pending = 0;
  400. dev_info->is_shared = 1;
  401. dev_info->dev.parent = dcssblk_root_dev;
  402. /*
  403. * get minor, add to list
  404. */
  405. down_write(&dcssblk_devices_sem);
  406. rc = dcssblk_assign_free_minor(dev_info);
  407. if (rc) {
  408. up_write(&dcssblk_devices_sem);
  409. PRINT_ERR("No free minor number available! "
  410. "Unloading segment...\n");
  411. goto unload_seg;
  412. }
  413. sprintf(dev_info->gd->disk_name, "dcssblk%d",
  414. dev_info->gd->first_minor);
  415. list_add_tail(&dev_info->lh, &dcssblk_devices);
  416. if (!try_module_get(THIS_MODULE)) {
  417. rc = -ENODEV;
  418. goto list_del;
  419. }
  420. /*
  421. * register the device
  422. */
  423. rc = device_register(&dev_info->dev);
  424. if (rc) {
  425. PRINT_ERR("Segment %s could not be registered RC=%d\n",
  426. local_buf, rc);
  427. module_put(THIS_MODULE);
  428. goto list_del;
  429. }
  430. get_device(&dev_info->dev);
  431. rc = device_create_file(&dev_info->dev, &dev_attr_shared);
  432. if (rc)
  433. goto unregister_dev;
  434. rc = device_create_file(&dev_info->dev, &dev_attr_save);
  435. if (rc)
  436. goto unregister_dev;
  437. blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
  438. blk_queue_hardsect_size(dev_info->dcssblk_queue, 4096);
  439. add_disk(dev_info->gd);
  440. switch (dev_info->segment_type) {
  441. case SEG_TYPE_SR:
  442. case SEG_TYPE_ER:
  443. case SEG_TYPE_SC:
  444. set_disk_ro(dev_info->gd,1);
  445. break;
  446. default:
  447. set_disk_ro(dev_info->gd,0);
  448. break;
  449. }
  450. PRINT_DEBUG("Segment %s loaded successfully\n", local_buf);
  451. up_write(&dcssblk_devices_sem);
  452. rc = count;
  453. goto out;
  454. unregister_dev:
  455. PRINT_ERR("device_create_file() failed!\n");
  456. list_del(&dev_info->lh);
  457. blk_cleanup_queue(dev_info->dcssblk_queue);
  458. dev_info->gd->queue = NULL;
  459. put_disk(dev_info->gd);
  460. device_unregister(&dev_info->dev);
  461. segment_unload(dev_info->segment_name);
  462. put_device(&dev_info->dev);
  463. up_write(&dcssblk_devices_sem);
  464. goto out;
  465. list_del:
  466. list_del(&dev_info->lh);
  467. up_write(&dcssblk_devices_sem);
  468. unload_seg:
  469. segment_unload(local_buf);
  470. dealloc_gendisk:
  471. blk_cleanup_queue(dev_info->dcssblk_queue);
  472. dev_info->gd->queue = NULL;
  473. put_disk(dev_info->gd);
  474. free_dev_info:
  475. kfree(dev_info);
  476. out:
  477. kfree(local_buf);
  478. out_nobuf:
  479. return rc;
  480. }
  481. /*
  482. * device attribute for removing devices
  483. */
  484. static ssize_t
  485. dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  486. {
  487. struct dcssblk_dev_info *dev_info;
  488. int rc, i;
  489. char *local_buf;
  490. if (dev != dcssblk_root_dev) {
  491. return -EINVAL;
  492. }
  493. local_buf = kmalloc(count + 1, GFP_KERNEL);
  494. if (local_buf == NULL) {
  495. return -ENOMEM;
  496. }
  497. /*
  498. * parse input
  499. */
  500. for (i = 0; ((*(buf+i)!='\0') && (*(buf+i)!='\n') && i < count); i++) {
  501. local_buf[i] = toupper(buf[i]);
  502. }
  503. local_buf[i] = '\0';
  504. if ((i == 0) || (i > 8)) {
  505. rc = -ENAMETOOLONG;
  506. goto out_buf;
  507. }
  508. down_write(&dcssblk_devices_sem);
  509. dev_info = dcssblk_get_device_by_name(local_buf);
  510. if (dev_info == NULL) {
  511. up_write(&dcssblk_devices_sem);
  512. PRINT_WARN("Segment %s is not loaded!\n", local_buf);
  513. rc = -ENODEV;
  514. goto out_buf;
  515. }
  516. if (atomic_read(&dev_info->use_count) != 0) {
  517. up_write(&dcssblk_devices_sem);
  518. PRINT_WARN("Segment %s is in use!\n", local_buf);
  519. rc = -EBUSY;
  520. goto out_buf;
  521. }
  522. list_del(&dev_info->lh);
  523. del_gendisk(dev_info->gd);
  524. blk_cleanup_queue(dev_info->dcssblk_queue);
  525. dev_info->gd->queue = NULL;
  526. put_disk(dev_info->gd);
  527. device_unregister(&dev_info->dev);
  528. segment_unload(dev_info->segment_name);
  529. PRINT_DEBUG("Segment %s unloaded successfully\n",
  530. dev_info->segment_name);
  531. put_device(&dev_info->dev);
  532. up_write(&dcssblk_devices_sem);
  533. rc = count;
  534. out_buf:
  535. kfree(local_buf);
  536. return rc;
  537. }
  538. static int
  539. dcssblk_open(struct inode *inode, struct file *filp)
  540. {
  541. struct dcssblk_dev_info *dev_info;
  542. int rc;
  543. dev_info = inode->i_bdev->bd_disk->private_data;
  544. if (NULL == dev_info) {
  545. rc = -ENODEV;
  546. goto out;
  547. }
  548. atomic_inc(&dev_info->use_count);
  549. inode->i_bdev->bd_block_size = 4096;
  550. rc = 0;
  551. out:
  552. return rc;
  553. }
  554. static int
  555. dcssblk_release(struct inode *inode, struct file *filp)
  556. {
  557. struct dcssblk_dev_info *dev_info;
  558. int rc;
  559. dev_info = inode->i_bdev->bd_disk->private_data;
  560. if (NULL == dev_info) {
  561. rc = -ENODEV;
  562. goto out;
  563. }
  564. down_write(&dcssblk_devices_sem);
  565. if (atomic_dec_and_test(&dev_info->use_count)
  566. && (dev_info->save_pending)) {
  567. PRINT_INFO("Segment %s became idle and is being saved now\n",
  568. dev_info->segment_name);
  569. segment_save(dev_info->segment_name);
  570. dev_info->save_pending = 0;
  571. }
  572. up_write(&dcssblk_devices_sem);
  573. rc = 0;
  574. out:
  575. return rc;
  576. }
  577. static int
  578. dcssblk_make_request(struct request_queue *q, struct bio *bio)
  579. {
  580. struct dcssblk_dev_info *dev_info;
  581. struct bio_vec *bvec;
  582. unsigned long index;
  583. unsigned long page_addr;
  584. unsigned long source_addr;
  585. unsigned long bytes_done;
  586. int i;
  587. bytes_done = 0;
  588. dev_info = bio->bi_bdev->bd_disk->private_data;
  589. if (dev_info == NULL)
  590. goto fail;
  591. if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
  592. /* Request is not page-aligned. */
  593. goto fail;
  594. if (((bio->bi_size >> 9) + bio->bi_sector)
  595. > get_capacity(bio->bi_bdev->bd_disk)) {
  596. /* Request beyond end of DCSS segment. */
  597. goto fail;
  598. }
  599. /* verify data transfer direction */
  600. if (dev_info->is_shared) {
  601. switch (dev_info->segment_type) {
  602. case SEG_TYPE_SR:
  603. case SEG_TYPE_ER:
  604. case SEG_TYPE_SC:
  605. /* cannot write to these segments */
  606. if (bio_data_dir(bio) == WRITE) {
  607. PRINT_WARN("rejecting write to ro segment %s\n", dev_info->dev.bus_id);
  608. goto fail;
  609. }
  610. }
  611. }
  612. index = (bio->bi_sector >> 3);
  613. bio_for_each_segment(bvec, bio, i) {
  614. page_addr = (unsigned long)
  615. page_address(bvec->bv_page) + bvec->bv_offset;
  616. source_addr = dev_info->start + (index<<12) + bytes_done;
  617. if (unlikely(page_addr & 4095) != 0 || (bvec->bv_len & 4095) != 0)
  618. // More paranoia.
  619. goto fail;
  620. if (bio_data_dir(bio) == READ) {
  621. memcpy((void*)page_addr, (void*)source_addr,
  622. bvec->bv_len);
  623. } else {
  624. memcpy((void*)source_addr, (void*)page_addr,
  625. bvec->bv_len);
  626. }
  627. bytes_done += bvec->bv_len;
  628. }
  629. bio_endio(bio, 0);
  630. return 0;
  631. fail:
  632. bio_io_error(bio);
  633. return 0;
  634. }
  635. static int
  636. dcssblk_direct_access (struct block_device *bdev, sector_t secnum,
  637. unsigned long *data)
  638. {
  639. struct dcssblk_dev_info *dev_info;
  640. unsigned long pgoff;
  641. dev_info = bdev->bd_disk->private_data;
  642. if (!dev_info)
  643. return -ENODEV;
  644. if (secnum % (PAGE_SIZE/512))
  645. return -EINVAL;
  646. pgoff = secnum / (PAGE_SIZE / 512);
  647. if ((pgoff+1)*PAGE_SIZE-1 > dev_info->end - dev_info->start)
  648. return -ERANGE;
  649. *data = (unsigned long) (dev_info->start+pgoff*PAGE_SIZE);
  650. return 0;
  651. }
  652. static void
  653. dcssblk_check_params(void)
  654. {
  655. int rc, i, j, k;
  656. char buf[9];
  657. struct dcssblk_dev_info *dev_info;
  658. for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
  659. i++) {
  660. for (j = i; (dcssblk_segments[j] != ',') &&
  661. (dcssblk_segments[j] != '\0') &&
  662. (dcssblk_segments[j] != '(') &&
  663. (j - i) < 8; j++)
  664. {
  665. buf[j-i] = dcssblk_segments[j];
  666. }
  667. buf[j-i] = '\0';
  668. rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
  669. if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
  670. for (k = 0; buf[k] != '\0'; k++)
  671. buf[k] = toupper(buf[k]);
  672. if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
  673. down_read(&dcssblk_devices_sem);
  674. dev_info = dcssblk_get_device_by_name(buf);
  675. up_read(&dcssblk_devices_sem);
  676. if (dev_info)
  677. dcssblk_shared_store(&dev_info->dev,
  678. NULL, "0\n", 2);
  679. }
  680. }
  681. while ((dcssblk_segments[j] != ',') &&
  682. (dcssblk_segments[j] != '\0'))
  683. {
  684. j++;
  685. }
  686. if (dcssblk_segments[j] == '\0')
  687. break;
  688. i = j;
  689. }
  690. }
  691. /*
  692. * The init/exit functions.
  693. */
  694. static void __exit
  695. dcssblk_exit(void)
  696. {
  697. PRINT_DEBUG("DCSSBLOCK EXIT...\n");
  698. s390_root_dev_unregister(dcssblk_root_dev);
  699. unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
  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");