dcssblk.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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 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 inline 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 == -EIO || rc == -ENOENT)
  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 == -EIO || rc == -ENOENT)
  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_put_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 = kmalloc(sizeof(struct dcssblk_dev_info), GFP_KERNEL);
  361. if (dev_info == NULL) {
  362. rc = -ENOMEM;
  363. goto out;
  364. }
  365. memset(dev_info, 0, sizeof(struct dcssblk_dev_info));
  366. strcpy(dev_info->segment_name, local_buf);
  367. strlcpy(dev_info->dev.bus_id, local_buf, BUS_ID_SIZE);
  368. dev_info->dev.release = dcssblk_release_segment;
  369. INIT_LIST_HEAD(&dev_info->lh);
  370. dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
  371. if (dev_info->gd == NULL) {
  372. rc = -ENOMEM;
  373. goto free_dev_info;
  374. }
  375. dev_info->gd->major = dcssblk_major;
  376. dev_info->gd->fops = &dcssblk_devops;
  377. dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL);
  378. dev_info->gd->queue = dev_info->dcssblk_queue;
  379. dev_info->gd->private_data = dev_info;
  380. dev_info->gd->driverfs_dev = &dev_info->dev;
  381. /*
  382. * load the segment
  383. */
  384. rc = segment_load(local_buf, SEGMENT_SHARED,
  385. &dev_info->start, &dev_info->end);
  386. if (rc < 0) {
  387. dcssblk_segment_warn(rc, dev_info->segment_name);
  388. goto dealloc_gendisk;
  389. }
  390. seg_byte_size = (dev_info->end - dev_info->start + 1);
  391. set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
  392. PRINT_INFO("Loaded segment %s, size = %lu Byte, "
  393. "capacity = %lu (512 Byte) sectors\n", local_buf,
  394. seg_byte_size, seg_byte_size >> 9);
  395. dev_info->segment_type = rc;
  396. dev_info->save_pending = 0;
  397. dev_info->is_shared = 1;
  398. dev_info->dev.parent = dcssblk_root_dev;
  399. /*
  400. * get minor, add to list
  401. */
  402. down_write(&dcssblk_devices_sem);
  403. rc = dcssblk_assign_free_minor(dev_info);
  404. if (rc) {
  405. up_write(&dcssblk_devices_sem);
  406. PRINT_ERR("No free minor number available! "
  407. "Unloading segment...\n");
  408. goto unload_seg;
  409. }
  410. sprintf(dev_info->gd->disk_name, "dcssblk%d",
  411. dev_info->gd->first_minor);
  412. list_add_tail(&dev_info->lh, &dcssblk_devices);
  413. if (!try_module_get(THIS_MODULE)) {
  414. rc = -ENODEV;
  415. goto list_del;
  416. }
  417. /*
  418. * register the device
  419. */
  420. rc = device_register(&dev_info->dev);
  421. if (rc) {
  422. PRINT_ERR("Segment %s could not be registered RC=%d\n",
  423. local_buf, rc);
  424. module_put(THIS_MODULE);
  425. goto list_del;
  426. }
  427. get_device(&dev_info->dev);
  428. rc = device_create_file(&dev_info->dev, &dev_attr_shared);
  429. if (rc)
  430. goto unregister_dev;
  431. rc = device_create_file(&dev_info->dev, &dev_attr_save);
  432. if (rc)
  433. goto unregister_dev;
  434. add_disk(dev_info->gd);
  435. blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
  436. blk_queue_hardsect_size(dev_info->dcssblk_queue, 4096);
  437. switch (dev_info->segment_type) {
  438. case SEG_TYPE_SR:
  439. case SEG_TYPE_ER:
  440. case SEG_TYPE_SC:
  441. set_disk_ro(dev_info->gd,1);
  442. break;
  443. default:
  444. set_disk_ro(dev_info->gd,0);
  445. break;
  446. }
  447. PRINT_DEBUG("Segment %s loaded successfully\n", local_buf);
  448. up_write(&dcssblk_devices_sem);
  449. rc = count;
  450. goto out;
  451. unregister_dev:
  452. PRINT_ERR("device_create_file() failed!\n");
  453. list_del(&dev_info->lh);
  454. blk_put_queue(dev_info->dcssblk_queue);
  455. dev_info->gd->queue = NULL;
  456. put_disk(dev_info->gd);
  457. device_unregister(&dev_info->dev);
  458. segment_unload(dev_info->segment_name);
  459. put_device(&dev_info->dev);
  460. up_write(&dcssblk_devices_sem);
  461. goto out;
  462. list_del:
  463. list_del(&dev_info->lh);
  464. up_write(&dcssblk_devices_sem);
  465. unload_seg:
  466. segment_unload(local_buf);
  467. dealloc_gendisk:
  468. blk_put_queue(dev_info->dcssblk_queue);
  469. dev_info->gd->queue = NULL;
  470. put_disk(dev_info->gd);
  471. free_dev_info:
  472. kfree(dev_info);
  473. out:
  474. kfree(local_buf);
  475. out_nobuf:
  476. return rc;
  477. }
  478. /*
  479. * device attribute for removing devices
  480. */
  481. static ssize_t
  482. dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  483. {
  484. struct dcssblk_dev_info *dev_info;
  485. int rc, i;
  486. char *local_buf;
  487. if (dev != dcssblk_root_dev) {
  488. return -EINVAL;
  489. }
  490. local_buf = kmalloc(count + 1, GFP_KERNEL);
  491. if (local_buf == NULL) {
  492. return -ENOMEM;
  493. }
  494. /*
  495. * parse input
  496. */
  497. for (i = 0; ((*(buf+i)!='\0') && (*(buf+i)!='\n') && i < count); i++) {
  498. local_buf[i] = toupper(buf[i]);
  499. }
  500. local_buf[i] = '\0';
  501. if ((i == 0) || (i > 8)) {
  502. rc = -ENAMETOOLONG;
  503. goto out_buf;
  504. }
  505. down_write(&dcssblk_devices_sem);
  506. dev_info = dcssblk_get_device_by_name(local_buf);
  507. if (dev_info == NULL) {
  508. up_write(&dcssblk_devices_sem);
  509. PRINT_WARN("Segment %s is not loaded!\n", local_buf);
  510. rc = -ENODEV;
  511. goto out_buf;
  512. }
  513. if (atomic_read(&dev_info->use_count) != 0) {
  514. up_write(&dcssblk_devices_sem);
  515. PRINT_WARN("Segment %s is in use!\n", local_buf);
  516. rc = -EBUSY;
  517. goto out_buf;
  518. }
  519. list_del(&dev_info->lh);
  520. del_gendisk(dev_info->gd);
  521. blk_put_queue(dev_info->dcssblk_queue);
  522. dev_info->gd->queue = NULL;
  523. put_disk(dev_info->gd);
  524. device_unregister(&dev_info->dev);
  525. segment_unload(dev_info->segment_name);
  526. PRINT_DEBUG("Segment %s unloaded successfully\n",
  527. dev_info->segment_name);
  528. put_device(&dev_info->dev);
  529. up_write(&dcssblk_devices_sem);
  530. rc = count;
  531. out_buf:
  532. kfree(local_buf);
  533. return rc;
  534. }
  535. static int
  536. dcssblk_open(struct inode *inode, struct file *filp)
  537. {
  538. struct dcssblk_dev_info *dev_info;
  539. int rc;
  540. dev_info = inode->i_bdev->bd_disk->private_data;
  541. if (NULL == dev_info) {
  542. rc = -ENODEV;
  543. goto out;
  544. }
  545. atomic_inc(&dev_info->use_count);
  546. inode->i_bdev->bd_block_size = 4096;
  547. rc = 0;
  548. out:
  549. return rc;
  550. }
  551. static int
  552. dcssblk_release(struct inode *inode, struct file *filp)
  553. {
  554. struct dcssblk_dev_info *dev_info;
  555. int rc;
  556. dev_info = inode->i_bdev->bd_disk->private_data;
  557. if (NULL == dev_info) {
  558. rc = -ENODEV;
  559. goto out;
  560. }
  561. down_write(&dcssblk_devices_sem);
  562. if (atomic_dec_and_test(&dev_info->use_count)
  563. && (dev_info->save_pending)) {
  564. PRINT_INFO("Segment %s became idle and is being saved now\n",
  565. dev_info->segment_name);
  566. segment_save(dev_info->segment_name);
  567. dev_info->save_pending = 0;
  568. }
  569. up_write(&dcssblk_devices_sem);
  570. rc = 0;
  571. out:
  572. return rc;
  573. }
  574. static int
  575. dcssblk_make_request(request_queue_t *q, struct bio *bio)
  576. {
  577. struct dcssblk_dev_info *dev_info;
  578. struct bio_vec *bvec;
  579. unsigned long index;
  580. unsigned long page_addr;
  581. unsigned long source_addr;
  582. unsigned long bytes_done;
  583. int i;
  584. bytes_done = 0;
  585. dev_info = bio->bi_bdev->bd_disk->private_data;
  586. if (dev_info == NULL)
  587. goto fail;
  588. if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
  589. /* Request is not page-aligned. */
  590. goto fail;
  591. if (((bio->bi_size >> 9) + bio->bi_sector)
  592. > get_capacity(bio->bi_bdev->bd_disk)) {
  593. /* Request beyond end of DCSS segment. */
  594. goto fail;
  595. }
  596. /* verify data transfer direction */
  597. if (dev_info->is_shared) {
  598. switch (dev_info->segment_type) {
  599. case SEG_TYPE_SR:
  600. case SEG_TYPE_ER:
  601. case SEG_TYPE_SC:
  602. /* cannot write to these segments */
  603. if (bio_data_dir(bio) == WRITE) {
  604. PRINT_WARN("rejecting write to ro segment %s\n", dev_info->dev.bus_id);
  605. goto fail;
  606. }
  607. }
  608. }
  609. index = (bio->bi_sector >> 3);
  610. bio_for_each_segment(bvec, bio, i) {
  611. page_addr = (unsigned long)
  612. page_address(bvec->bv_page) + bvec->bv_offset;
  613. source_addr = dev_info->start + (index<<12) + bytes_done;
  614. if (unlikely(page_addr & 4095) != 0 || (bvec->bv_len & 4095) != 0)
  615. // More paranoia.
  616. goto fail;
  617. if (bio_data_dir(bio) == READ) {
  618. memcpy((void*)page_addr, (void*)source_addr,
  619. bvec->bv_len);
  620. } else {
  621. memcpy((void*)source_addr, (void*)page_addr,
  622. bvec->bv_len);
  623. }
  624. bytes_done += bvec->bv_len;
  625. }
  626. bio_endio(bio, bytes_done, 0);
  627. return 0;
  628. fail:
  629. bio_io_error(bio, bio->bi_size);
  630. return 0;
  631. }
  632. static int
  633. dcssblk_direct_access (struct block_device *bdev, sector_t secnum,
  634. unsigned long *data)
  635. {
  636. struct dcssblk_dev_info *dev_info;
  637. unsigned long pgoff;
  638. dev_info = bdev->bd_disk->private_data;
  639. if (!dev_info)
  640. return -ENODEV;
  641. if (secnum % (PAGE_SIZE/512))
  642. return -EINVAL;
  643. pgoff = secnum / (PAGE_SIZE / 512);
  644. if ((pgoff+1)*PAGE_SIZE-1 > dev_info->end - dev_info->start)
  645. return -ERANGE;
  646. *data = (unsigned long) (dev_info->start+pgoff*PAGE_SIZE);
  647. return 0;
  648. }
  649. static void
  650. dcssblk_check_params(void)
  651. {
  652. int rc, i, j, k;
  653. char buf[9];
  654. struct dcssblk_dev_info *dev_info;
  655. for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
  656. i++) {
  657. for (j = i; (dcssblk_segments[j] != ',') &&
  658. (dcssblk_segments[j] != '\0') &&
  659. (dcssblk_segments[j] != '(') &&
  660. (j - i) < 8; j++)
  661. {
  662. buf[j-i] = dcssblk_segments[j];
  663. }
  664. buf[j-i] = '\0';
  665. rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
  666. if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
  667. for (k = 0; buf[k] != '\0'; k++)
  668. buf[k] = toupper(buf[k]);
  669. if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
  670. down_read(&dcssblk_devices_sem);
  671. dev_info = dcssblk_get_device_by_name(buf);
  672. up_read(&dcssblk_devices_sem);
  673. if (dev_info)
  674. dcssblk_shared_store(&dev_info->dev,
  675. NULL, "0\n", 2);
  676. }
  677. }
  678. while ((dcssblk_segments[j] != ',') &&
  679. (dcssblk_segments[j] != '\0'))
  680. {
  681. j++;
  682. }
  683. if (dcssblk_segments[j] == '\0')
  684. break;
  685. i = j;
  686. }
  687. }
  688. /*
  689. * The init/exit functions.
  690. */
  691. static void __exit
  692. dcssblk_exit(void)
  693. {
  694. int rc;
  695. PRINT_DEBUG("DCSSBLOCK EXIT...\n");
  696. s390_root_dev_unregister(dcssblk_root_dev);
  697. rc = unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
  698. if (rc) {
  699. PRINT_ERR("unregister_blkdev() failed!\n");
  700. }
  701. PRINT_DEBUG("...finished!\n");
  702. }
  703. static int __init
  704. dcssblk_init(void)
  705. {
  706. int rc;
  707. PRINT_DEBUG("DCSSBLOCK INIT...\n");
  708. dcssblk_root_dev = s390_root_dev_register("dcssblk");
  709. if (IS_ERR(dcssblk_root_dev)) {
  710. PRINT_ERR("device_register() failed!\n");
  711. return PTR_ERR(dcssblk_root_dev);
  712. }
  713. rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
  714. if (rc) {
  715. PRINT_ERR("device_create_file(add) failed!\n");
  716. s390_root_dev_unregister(dcssblk_root_dev);
  717. return rc;
  718. }
  719. rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
  720. if (rc) {
  721. PRINT_ERR("device_create_file(remove) failed!\n");
  722. s390_root_dev_unregister(dcssblk_root_dev);
  723. return rc;
  724. }
  725. rc = register_blkdev(0, DCSSBLK_NAME);
  726. if (rc < 0) {
  727. PRINT_ERR("Can't get dynamic major!\n");
  728. s390_root_dev_unregister(dcssblk_root_dev);
  729. return rc;
  730. }
  731. dcssblk_major = rc;
  732. init_rwsem(&dcssblk_devices_sem);
  733. dcssblk_check_params();
  734. PRINT_DEBUG("...finished!\n");
  735. return 0;
  736. }
  737. module_init(dcssblk_init);
  738. module_exit(dcssblk_exit);
  739. module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
  740. MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
  741. "comma-separated list, each name max. 8 chars.\n"
  742. "Adding \"(local)\" to segment name equals echoing 0 to "
  743. "/sys/devices/dcssblk/<segment name>/shared after loading "
  744. "the segment - \n"
  745. "e.g. segments=\"mydcss1,mydcss2,mydcss3(local)\"");
  746. MODULE_LICENSE("GPL");