dcssblk.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. void **kaddr, unsigned long *pfn);
  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. struct dcssblk_dev_info {
  44. struct list_head lh;
  45. struct device dev;
  46. char segment_name[BUS_ID_SIZE];
  47. atomic_t use_count;
  48. struct gendisk *gd;
  49. unsigned long start;
  50. unsigned long end;
  51. int segment_type;
  52. unsigned char save_pending;
  53. unsigned char is_shared;
  54. struct request_queue *dcssblk_queue;
  55. int num_of_segments;
  56. struct list_head seg_list;
  57. };
  58. struct segment_info {
  59. struct list_head lh;
  60. char segment_name[BUS_ID_SIZE];
  61. unsigned long start;
  62. unsigned long end;
  63. int segment_type;
  64. };
  65. static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
  66. size_t count);
  67. static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
  68. size_t count);
  69. static ssize_t dcssblk_save_store(struct device * dev, struct device_attribute *attr, const char * buf,
  70. size_t count);
  71. static ssize_t dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf);
  72. static ssize_t dcssblk_shared_store(struct device * dev, struct device_attribute *attr, const char * buf,
  73. size_t count);
  74. static ssize_t dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf);
  75. static ssize_t dcssblk_seglist_show(struct device *dev,
  76. struct device_attribute *attr,
  77. char *buf);
  78. static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
  79. static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
  80. static DEVICE_ATTR(save, S_IWUSR | S_IRUSR, dcssblk_save_show,
  81. dcssblk_save_store);
  82. static DEVICE_ATTR(shared, S_IWUSR | S_IRUSR, dcssblk_shared_show,
  83. dcssblk_shared_store);
  84. static DEVICE_ATTR(seglist, S_IRUSR, dcssblk_seglist_show, NULL);
  85. static struct device *dcssblk_root_dev;
  86. static LIST_HEAD(dcssblk_devices);
  87. static struct rw_semaphore dcssblk_devices_sem;
  88. /*
  89. * release function for segment device.
  90. */
  91. static void
  92. dcssblk_release_segment(struct device *dev)
  93. {
  94. struct dcssblk_dev_info *dev_info;
  95. struct segment_info *entry, *temp;
  96. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  97. list_for_each_entry_safe(entry, temp, &dev_info->seg_list, lh) {
  98. list_del(&entry->lh);
  99. kfree(entry);
  100. }
  101. kfree(dev_info);
  102. module_put(THIS_MODULE);
  103. }
  104. /*
  105. * get a minor number. needs to be called with
  106. * down_write(&dcssblk_devices_sem) and the
  107. * device needs to be enqueued before the semaphore is
  108. * freed.
  109. */
  110. static int
  111. dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info)
  112. {
  113. int minor, found;
  114. struct dcssblk_dev_info *entry;
  115. if (dev_info == NULL)
  116. return -EINVAL;
  117. for (minor = 0; minor < (1<<MINORBITS); minor++) {
  118. found = 0;
  119. // test if minor available
  120. list_for_each_entry(entry, &dcssblk_devices, lh)
  121. if (minor == MINOR(disk_devt(entry->gd)))
  122. found++;
  123. if (!found) break; // got unused minor
  124. }
  125. if (found)
  126. return -EBUSY;
  127. dev_info->gd->first_minor = minor;
  128. return 0;
  129. }
  130. /*
  131. * get the struct dcssblk_dev_info from dcssblk_devices
  132. * for the given name.
  133. * down_read(&dcssblk_devices_sem) must be held.
  134. */
  135. static struct dcssblk_dev_info *
  136. dcssblk_get_device_by_name(char *name)
  137. {
  138. struct dcssblk_dev_info *entry;
  139. list_for_each_entry(entry, &dcssblk_devices, lh) {
  140. if (!strcmp(name, entry->segment_name)) {
  141. return entry;
  142. }
  143. }
  144. return NULL;
  145. }
  146. /*
  147. * get the struct segment_info from seg_list
  148. * for the given name.
  149. * down_read(&dcssblk_devices_sem) must be held.
  150. */
  151. static struct segment_info *
  152. dcssblk_get_segment_by_name(char *name)
  153. {
  154. struct dcssblk_dev_info *dev_info;
  155. struct segment_info *entry;
  156. list_for_each_entry(dev_info, &dcssblk_devices, lh) {
  157. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  158. if (!strcmp(name, entry->segment_name))
  159. return entry;
  160. }
  161. }
  162. return NULL;
  163. }
  164. /*
  165. * get the highest address of the multi-segment block.
  166. */
  167. static unsigned long
  168. dcssblk_find_highest_addr(struct dcssblk_dev_info *dev_info)
  169. {
  170. unsigned long highest_addr;
  171. struct segment_info *entry;
  172. highest_addr = 0;
  173. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  174. if (highest_addr < entry->end)
  175. highest_addr = entry->end;
  176. }
  177. return highest_addr;
  178. }
  179. /*
  180. * get the lowest address of the multi-segment block.
  181. */
  182. static unsigned long
  183. dcssblk_find_lowest_addr(struct dcssblk_dev_info *dev_info)
  184. {
  185. int set_first;
  186. unsigned long lowest_addr;
  187. struct segment_info *entry;
  188. set_first = 0;
  189. lowest_addr = 0;
  190. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  191. if (set_first == 0) {
  192. lowest_addr = entry->start;
  193. set_first = 1;
  194. } else {
  195. if (lowest_addr > entry->start)
  196. lowest_addr = entry->start;
  197. }
  198. }
  199. return lowest_addr;
  200. }
  201. /*
  202. * Check continuity of segments.
  203. */
  204. static int
  205. dcssblk_is_continuous(struct dcssblk_dev_info *dev_info)
  206. {
  207. int i, j, rc;
  208. struct segment_info *sort_list, *entry, temp;
  209. if (dev_info->num_of_segments <= 1)
  210. return 0;
  211. sort_list = kzalloc(
  212. sizeof(struct segment_info) * dev_info->num_of_segments,
  213. GFP_KERNEL);
  214. if (sort_list == NULL)
  215. return -ENOMEM;
  216. i = 0;
  217. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  218. memcpy(&sort_list[i], entry, sizeof(struct segment_info));
  219. i++;
  220. }
  221. /* sort segments */
  222. for (i = 0; i < dev_info->num_of_segments; i++)
  223. for (j = 0; j < dev_info->num_of_segments; j++)
  224. if (sort_list[j].start > sort_list[i].start) {
  225. memcpy(&temp, &sort_list[i],
  226. sizeof(struct segment_info));
  227. memcpy(&sort_list[i], &sort_list[j],
  228. sizeof(struct segment_info));
  229. memcpy(&sort_list[j], &temp,
  230. sizeof(struct segment_info));
  231. }
  232. /* check continuity */
  233. for (i = 0; i < dev_info->num_of_segments - 1; i++) {
  234. if ((sort_list[i].end + 1) != sort_list[i+1].start) {
  235. PRINT_ERR("Segment %s is not contiguous with "
  236. "segment %s\n",
  237. sort_list[i].segment_name,
  238. sort_list[i+1].segment_name);
  239. rc = -EINVAL;
  240. goto out;
  241. }
  242. /* EN and EW are allowed in a block device */
  243. if (sort_list[i].segment_type != sort_list[i+1].segment_type) {
  244. if (!(sort_list[i].segment_type & SEGMENT_EXCLUSIVE) ||
  245. (sort_list[i].segment_type == SEG_TYPE_ER) ||
  246. !(sort_list[i+1].segment_type &
  247. SEGMENT_EXCLUSIVE) ||
  248. (sort_list[i+1].segment_type == SEG_TYPE_ER)) {
  249. PRINT_ERR("Segment %s has different type from "
  250. "segment %s\n",
  251. sort_list[i].segment_name,
  252. sort_list[i+1].segment_name);
  253. rc = -EINVAL;
  254. goto out;
  255. }
  256. }
  257. }
  258. rc = 0;
  259. out:
  260. kfree(sort_list);
  261. return rc;
  262. }
  263. /*
  264. * Load a segment
  265. */
  266. static int
  267. dcssblk_load_segment(char *name, struct segment_info **seg_info)
  268. {
  269. int rc;
  270. /* already loaded? */
  271. down_read(&dcssblk_devices_sem);
  272. *seg_info = dcssblk_get_segment_by_name(name);
  273. up_read(&dcssblk_devices_sem);
  274. if (*seg_info != NULL)
  275. return -EEXIST;
  276. /* get a struct segment_info */
  277. *seg_info = kzalloc(sizeof(struct segment_info), GFP_KERNEL);
  278. if (*seg_info == NULL)
  279. return -ENOMEM;
  280. strcpy((*seg_info)->segment_name, name);
  281. /* load the segment */
  282. rc = segment_load(name, SEGMENT_SHARED,
  283. &(*seg_info)->start, &(*seg_info)->end);
  284. if (rc < 0) {
  285. segment_warning(rc, (*seg_info)->segment_name);
  286. kfree(*seg_info);
  287. } else {
  288. INIT_LIST_HEAD(&(*seg_info)->lh);
  289. (*seg_info)->segment_type = rc;
  290. }
  291. return rc;
  292. }
  293. static void dcssblk_unregister_callback(struct device *dev)
  294. {
  295. device_unregister(dev);
  296. put_device(dev);
  297. }
  298. /*
  299. * device attribute for switching shared/nonshared (exclusive)
  300. * operation (show + store)
  301. */
  302. static ssize_t
  303. dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
  304. {
  305. struct dcssblk_dev_info *dev_info;
  306. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  307. return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
  308. }
  309. static ssize_t
  310. dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
  311. {
  312. struct dcssblk_dev_info *dev_info;
  313. struct segment_info *entry, *temp;
  314. int rc;
  315. if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
  316. return -EINVAL;
  317. down_write(&dcssblk_devices_sem);
  318. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  319. if (atomic_read(&dev_info->use_count)) {
  320. rc = -EBUSY;
  321. goto out;
  322. }
  323. if (inbuf[0] == '1') {
  324. /* reload segments in shared mode */
  325. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  326. rc = segment_modify_shared(entry->segment_name,
  327. SEGMENT_SHARED);
  328. if (rc < 0) {
  329. BUG_ON(rc == -EINVAL);
  330. if (rc != -EAGAIN)
  331. goto removeseg;
  332. }
  333. }
  334. dev_info->is_shared = 1;
  335. switch (dev_info->segment_type) {
  336. case SEG_TYPE_SR:
  337. case SEG_TYPE_ER:
  338. case SEG_TYPE_SC:
  339. set_disk_ro(dev_info->gd, 1);
  340. }
  341. } else if (inbuf[0] == '0') {
  342. /* reload segments in exclusive mode */
  343. if (dev_info->segment_type == SEG_TYPE_SC) {
  344. PRINT_ERR("Segment type SC (%s) cannot be loaded in "
  345. "non-shared mode\n", dev_info->segment_name);
  346. rc = -EINVAL;
  347. goto out;
  348. }
  349. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  350. rc = segment_modify_shared(entry->segment_name,
  351. SEGMENT_EXCLUSIVE);
  352. if (rc < 0) {
  353. BUG_ON(rc == -EINVAL);
  354. if (rc != -EAGAIN)
  355. goto removeseg;
  356. }
  357. }
  358. dev_info->is_shared = 0;
  359. set_disk_ro(dev_info->gd, 0);
  360. } else {
  361. rc = -EINVAL;
  362. goto out;
  363. }
  364. rc = count;
  365. goto out;
  366. removeseg:
  367. PRINT_ERR("Could not reload segment(s) of the device %s, removing "
  368. "segment(s) now!\n",
  369. dev_info->segment_name);
  370. temp = entry;
  371. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  372. if (entry != temp)
  373. segment_unload(entry->segment_name);
  374. }
  375. list_del(&dev_info->lh);
  376. del_gendisk(dev_info->gd);
  377. blk_cleanup_queue(dev_info->dcssblk_queue);
  378. dev_info->gd->queue = NULL;
  379. put_disk(dev_info->gd);
  380. rc = device_schedule_callback(dev, dcssblk_unregister_callback);
  381. out:
  382. up_write(&dcssblk_devices_sem);
  383. return rc;
  384. }
  385. /*
  386. * device attribute for save operation on current copy
  387. * of the segment. If the segment is busy, saving will
  388. * become pending until it gets released, which can be
  389. * undone by storing a non-true value to this entry.
  390. * (show + store)
  391. */
  392. static ssize_t
  393. dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
  394. {
  395. struct dcssblk_dev_info *dev_info;
  396. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  397. return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
  398. }
  399. static ssize_t
  400. dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
  401. {
  402. struct dcssblk_dev_info *dev_info;
  403. struct segment_info *entry;
  404. if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
  405. return -EINVAL;
  406. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  407. down_write(&dcssblk_devices_sem);
  408. if (inbuf[0] == '1') {
  409. if (atomic_read(&dev_info->use_count) == 0) {
  410. // device is idle => we save immediately
  411. PRINT_INFO("Saving segment(s) of the device %s\n",
  412. dev_info->segment_name);
  413. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  414. segment_save(entry->segment_name);
  415. }
  416. } else {
  417. // device is busy => we save it when it becomes
  418. // idle in dcssblk_release
  419. PRINT_INFO("Device %s is currently busy, segment(s) "
  420. "will be saved when it becomes idle...\n",
  421. dev_info->segment_name);
  422. dev_info->save_pending = 1;
  423. }
  424. } else if (inbuf[0] == '0') {
  425. if (dev_info->save_pending) {
  426. // device is busy & the user wants to undo his save
  427. // request
  428. dev_info->save_pending = 0;
  429. PRINT_INFO("Pending save for segment(s) of the device "
  430. "%s deactivated\n",
  431. dev_info->segment_name);
  432. }
  433. } else {
  434. up_write(&dcssblk_devices_sem);
  435. return -EINVAL;
  436. }
  437. up_write(&dcssblk_devices_sem);
  438. return count;
  439. }
  440. /*
  441. * device attribute for showing all segments in a device
  442. */
  443. static ssize_t
  444. dcssblk_seglist_show(struct device *dev, struct device_attribute *attr,
  445. char *buf)
  446. {
  447. int i;
  448. struct dcssblk_dev_info *dev_info;
  449. struct segment_info *entry;
  450. down_read(&dcssblk_devices_sem);
  451. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  452. i = 0;
  453. buf[0] = '\0';
  454. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  455. strcpy(&buf[i], entry->segment_name);
  456. i += strlen(entry->segment_name);
  457. buf[i] = '\n';
  458. i++;
  459. }
  460. up_read(&dcssblk_devices_sem);
  461. return i;
  462. }
  463. /*
  464. * device attribute for adding devices
  465. */
  466. static ssize_t
  467. dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  468. {
  469. int rc, i, j, num_of_segments;
  470. struct dcssblk_dev_info *dev_info;
  471. struct segment_info *seg_info, *temp;
  472. char *local_buf;
  473. unsigned long seg_byte_size;
  474. dev_info = NULL;
  475. seg_info = NULL;
  476. if (dev != dcssblk_root_dev) {
  477. rc = -EINVAL;
  478. goto out_nobuf;
  479. }
  480. if ((count < 1) || (buf[0] == '\0') || (buf[0] == '\n')) {
  481. rc = -ENAMETOOLONG;
  482. goto out_nobuf;
  483. }
  484. local_buf = kmalloc(count + 1, GFP_KERNEL);
  485. if (local_buf == NULL) {
  486. rc = -ENOMEM;
  487. goto out_nobuf;
  488. }
  489. /*
  490. * parse input
  491. */
  492. num_of_segments = 0;
  493. for (i = 0; ((buf[i] != '\0') && (buf[i] != '\n') && i < count); i++) {
  494. for (j = i; (buf[j] != ':') &&
  495. (buf[j] != '\0') &&
  496. (buf[j] != '\n') &&
  497. j < count; j++) {
  498. local_buf[j-i] = toupper(buf[j]);
  499. }
  500. local_buf[j-i] = '\0';
  501. if (((j - i) == 0) || ((j - i) > 8)) {
  502. rc = -ENAMETOOLONG;
  503. goto seg_list_del;
  504. }
  505. rc = dcssblk_load_segment(local_buf, &seg_info);
  506. if (rc < 0)
  507. goto seg_list_del;
  508. /*
  509. * get a struct dcssblk_dev_info
  510. */
  511. if (num_of_segments == 0) {
  512. dev_info = kzalloc(sizeof(struct dcssblk_dev_info),
  513. GFP_KERNEL);
  514. if (dev_info == NULL) {
  515. rc = -ENOMEM;
  516. goto out;
  517. }
  518. strcpy(dev_info->segment_name, local_buf);
  519. dev_info->segment_type = seg_info->segment_type;
  520. INIT_LIST_HEAD(&dev_info->seg_list);
  521. }
  522. list_add_tail(&seg_info->lh, &dev_info->seg_list);
  523. num_of_segments++;
  524. i = j;
  525. if ((buf[j] == '\0') || (buf[j] == '\n'))
  526. break;
  527. }
  528. /* no trailing colon at the end of the input */
  529. if ((i > 0) && (buf[i-1] == ':')) {
  530. rc = -ENAMETOOLONG;
  531. goto seg_list_del;
  532. }
  533. strlcpy(local_buf, buf, i + 1);
  534. dev_info->num_of_segments = num_of_segments;
  535. rc = dcssblk_is_continuous(dev_info);
  536. if (rc < 0)
  537. goto seg_list_del;
  538. dev_info->start = dcssblk_find_lowest_addr(dev_info);
  539. dev_info->end = dcssblk_find_highest_addr(dev_info);
  540. dev_set_name(&dev_info->dev, dev_info->segment_name);
  541. dev_info->dev.release = dcssblk_release_segment;
  542. INIT_LIST_HEAD(&dev_info->lh);
  543. dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
  544. if (dev_info->gd == NULL) {
  545. rc = -ENOMEM;
  546. goto seg_list_del;
  547. }
  548. dev_info->gd->major = dcssblk_major;
  549. dev_info->gd->fops = &dcssblk_devops;
  550. dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL);
  551. dev_info->gd->queue = dev_info->dcssblk_queue;
  552. dev_info->gd->private_data = dev_info;
  553. dev_info->gd->driverfs_dev = &dev_info->dev;
  554. blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
  555. blk_queue_hardsect_size(dev_info->dcssblk_queue, 4096);
  556. seg_byte_size = (dev_info->end - dev_info->start + 1);
  557. set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
  558. PRINT_INFO("Loaded segment(s) %s, size = %lu Byte, "
  559. "capacity = %lu (512 Byte) sectors\n", local_buf,
  560. seg_byte_size, seg_byte_size >> 9);
  561. dev_info->save_pending = 0;
  562. dev_info->is_shared = 1;
  563. dev_info->dev.parent = dcssblk_root_dev;
  564. /*
  565. *get minor, add to list
  566. */
  567. down_write(&dcssblk_devices_sem);
  568. if (dcssblk_get_segment_by_name(local_buf)) {
  569. rc = -EEXIST;
  570. goto release_gd;
  571. }
  572. rc = dcssblk_assign_free_minor(dev_info);
  573. if (rc)
  574. goto release_gd;
  575. sprintf(dev_info->gd->disk_name, "dcssblk%d",
  576. MINOR(disk_devt(dev_info->gd)));
  577. list_add_tail(&dev_info->lh, &dcssblk_devices);
  578. if (!try_module_get(THIS_MODULE)) {
  579. rc = -ENODEV;
  580. goto dev_list_del;
  581. }
  582. /*
  583. * register the device
  584. */
  585. rc = device_register(&dev_info->dev);
  586. if (rc) {
  587. module_put(THIS_MODULE);
  588. goto dev_list_del;
  589. }
  590. get_device(&dev_info->dev);
  591. rc = device_create_file(&dev_info->dev, &dev_attr_shared);
  592. if (rc)
  593. goto unregister_dev;
  594. rc = device_create_file(&dev_info->dev, &dev_attr_save);
  595. if (rc)
  596. goto unregister_dev;
  597. rc = device_create_file(&dev_info->dev, &dev_attr_seglist);
  598. if (rc)
  599. goto unregister_dev;
  600. add_disk(dev_info->gd);
  601. switch (dev_info->segment_type) {
  602. case SEG_TYPE_SR:
  603. case SEG_TYPE_ER:
  604. case SEG_TYPE_SC:
  605. set_disk_ro(dev_info->gd,1);
  606. break;
  607. default:
  608. set_disk_ro(dev_info->gd,0);
  609. break;
  610. }
  611. up_write(&dcssblk_devices_sem);
  612. rc = count;
  613. goto out;
  614. unregister_dev:
  615. list_del(&dev_info->lh);
  616. blk_cleanup_queue(dev_info->dcssblk_queue);
  617. dev_info->gd->queue = NULL;
  618. put_disk(dev_info->gd);
  619. device_unregister(&dev_info->dev);
  620. list_for_each_entry(seg_info, &dev_info->seg_list, lh) {
  621. segment_unload(seg_info->segment_name);
  622. }
  623. put_device(&dev_info->dev);
  624. up_write(&dcssblk_devices_sem);
  625. goto out;
  626. dev_list_del:
  627. list_del(&dev_info->lh);
  628. release_gd:
  629. blk_cleanup_queue(dev_info->dcssblk_queue);
  630. dev_info->gd->queue = NULL;
  631. put_disk(dev_info->gd);
  632. up_write(&dcssblk_devices_sem);
  633. seg_list_del:
  634. if (dev_info == NULL)
  635. goto out;
  636. list_for_each_entry_safe(seg_info, temp, &dev_info->seg_list, lh) {
  637. list_del(&seg_info->lh);
  638. segment_unload(seg_info->segment_name);
  639. kfree(seg_info);
  640. }
  641. kfree(dev_info);
  642. out:
  643. kfree(local_buf);
  644. out_nobuf:
  645. return rc;
  646. }
  647. /*
  648. * device attribute for removing devices
  649. */
  650. static ssize_t
  651. dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  652. {
  653. struct dcssblk_dev_info *dev_info;
  654. struct segment_info *entry;
  655. int rc, i;
  656. char *local_buf;
  657. if (dev != dcssblk_root_dev) {
  658. return -EINVAL;
  659. }
  660. local_buf = kmalloc(count + 1, GFP_KERNEL);
  661. if (local_buf == NULL) {
  662. return -ENOMEM;
  663. }
  664. /*
  665. * parse input
  666. */
  667. for (i = 0; ((*(buf+i)!='\0') && (*(buf+i)!='\n') && i < count); i++) {
  668. local_buf[i] = toupper(buf[i]);
  669. }
  670. local_buf[i] = '\0';
  671. if ((i == 0) || (i > 8)) {
  672. rc = -ENAMETOOLONG;
  673. goto out_buf;
  674. }
  675. down_write(&dcssblk_devices_sem);
  676. dev_info = dcssblk_get_device_by_name(local_buf);
  677. if (dev_info == NULL) {
  678. up_write(&dcssblk_devices_sem);
  679. PRINT_WARN("Device %s is not loaded!\n", local_buf);
  680. rc = -ENODEV;
  681. goto out_buf;
  682. }
  683. if (atomic_read(&dev_info->use_count) != 0) {
  684. up_write(&dcssblk_devices_sem);
  685. PRINT_WARN("Device %s is in use!\n", local_buf);
  686. rc = -EBUSY;
  687. goto out_buf;
  688. }
  689. list_del(&dev_info->lh);
  690. del_gendisk(dev_info->gd);
  691. blk_cleanup_queue(dev_info->dcssblk_queue);
  692. dev_info->gd->queue = NULL;
  693. put_disk(dev_info->gd);
  694. device_unregister(&dev_info->dev);
  695. /* unload all related segments */
  696. list_for_each_entry(entry, &dev_info->seg_list, lh)
  697. segment_unload(entry->segment_name);
  698. put_device(&dev_info->dev);
  699. up_write(&dcssblk_devices_sem);
  700. rc = count;
  701. out_buf:
  702. kfree(local_buf);
  703. return rc;
  704. }
  705. static int
  706. dcssblk_open(struct inode *inode, struct file *filp)
  707. {
  708. struct dcssblk_dev_info *dev_info;
  709. int rc;
  710. dev_info = inode->i_bdev->bd_disk->private_data;
  711. if (NULL == dev_info) {
  712. rc = -ENODEV;
  713. goto out;
  714. }
  715. atomic_inc(&dev_info->use_count);
  716. inode->i_bdev->bd_block_size = 4096;
  717. rc = 0;
  718. out:
  719. return rc;
  720. }
  721. static int
  722. dcssblk_release(struct inode *inode, struct file *filp)
  723. {
  724. struct dcssblk_dev_info *dev_info;
  725. struct segment_info *entry;
  726. int rc;
  727. dev_info = inode->i_bdev->bd_disk->private_data;
  728. if (NULL == dev_info) {
  729. rc = -ENODEV;
  730. goto out;
  731. }
  732. down_write(&dcssblk_devices_sem);
  733. if (atomic_dec_and_test(&dev_info->use_count)
  734. && (dev_info->save_pending)) {
  735. PRINT_INFO("Device %s became idle and is being saved now\n",
  736. dev_info->segment_name);
  737. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  738. segment_save(entry->segment_name);
  739. }
  740. dev_info->save_pending = 0;
  741. }
  742. up_write(&dcssblk_devices_sem);
  743. rc = 0;
  744. out:
  745. return rc;
  746. }
  747. static int
  748. dcssblk_make_request(struct request_queue *q, struct bio *bio)
  749. {
  750. struct dcssblk_dev_info *dev_info;
  751. struct bio_vec *bvec;
  752. unsigned long index;
  753. unsigned long page_addr;
  754. unsigned long source_addr;
  755. unsigned long bytes_done;
  756. int i;
  757. bytes_done = 0;
  758. dev_info = bio->bi_bdev->bd_disk->private_data;
  759. if (dev_info == NULL)
  760. goto fail;
  761. if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
  762. /* Request is not page-aligned. */
  763. goto fail;
  764. if (((bio->bi_size >> 9) + bio->bi_sector)
  765. > get_capacity(bio->bi_bdev->bd_disk)) {
  766. /* Request beyond end of DCSS segment. */
  767. goto fail;
  768. }
  769. /* verify data transfer direction */
  770. if (dev_info->is_shared) {
  771. switch (dev_info->segment_type) {
  772. case SEG_TYPE_SR:
  773. case SEG_TYPE_ER:
  774. case SEG_TYPE_SC:
  775. /* cannot write to these segments */
  776. if (bio_data_dir(bio) == WRITE) {
  777. PRINT_WARN("rejecting write to ro device %s\n",
  778. dev_name(&dev_info->dev));
  779. goto fail;
  780. }
  781. }
  782. }
  783. index = (bio->bi_sector >> 3);
  784. bio_for_each_segment(bvec, bio, i) {
  785. page_addr = (unsigned long)
  786. page_address(bvec->bv_page) + bvec->bv_offset;
  787. source_addr = dev_info->start + (index<<12) + bytes_done;
  788. if (unlikely((page_addr & 4095) != 0) || (bvec->bv_len & 4095) != 0)
  789. // More paranoia.
  790. goto fail;
  791. if (bio_data_dir(bio) == READ) {
  792. memcpy((void*)page_addr, (void*)source_addr,
  793. bvec->bv_len);
  794. } else {
  795. memcpy((void*)source_addr, (void*)page_addr,
  796. bvec->bv_len);
  797. }
  798. bytes_done += bvec->bv_len;
  799. }
  800. bio_endio(bio, 0);
  801. return 0;
  802. fail:
  803. bio_io_error(bio);
  804. return 0;
  805. }
  806. static int
  807. dcssblk_direct_access (struct block_device *bdev, sector_t secnum,
  808. void **kaddr, unsigned long *pfn)
  809. {
  810. struct dcssblk_dev_info *dev_info;
  811. unsigned long pgoff;
  812. dev_info = bdev->bd_disk->private_data;
  813. if (!dev_info)
  814. return -ENODEV;
  815. if (secnum % (PAGE_SIZE/512))
  816. return -EINVAL;
  817. pgoff = secnum / (PAGE_SIZE / 512);
  818. if ((pgoff+1)*PAGE_SIZE-1 > dev_info->end - dev_info->start)
  819. return -ERANGE;
  820. *kaddr = (void *) (dev_info->start+pgoff*PAGE_SIZE);
  821. *pfn = virt_to_phys(*kaddr) >> PAGE_SHIFT;
  822. return 0;
  823. }
  824. static void
  825. dcssblk_check_params(void)
  826. {
  827. int rc, i, j, k;
  828. char buf[DCSSBLK_PARM_LEN + 1];
  829. struct dcssblk_dev_info *dev_info;
  830. for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
  831. i++) {
  832. for (j = i; (dcssblk_segments[j] != ',') &&
  833. (dcssblk_segments[j] != '\0') &&
  834. (dcssblk_segments[j] != '(') &&
  835. (j < DCSSBLK_PARM_LEN); j++)
  836. {
  837. buf[j-i] = dcssblk_segments[j];
  838. }
  839. buf[j-i] = '\0';
  840. rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
  841. if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
  842. for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++)
  843. buf[k] = toupper(buf[k]);
  844. buf[k] = '\0';
  845. if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
  846. down_read(&dcssblk_devices_sem);
  847. dev_info = dcssblk_get_device_by_name(buf);
  848. up_read(&dcssblk_devices_sem);
  849. if (dev_info)
  850. dcssblk_shared_store(&dev_info->dev,
  851. NULL, "0\n", 2);
  852. }
  853. }
  854. while ((dcssblk_segments[j] != ',') &&
  855. (dcssblk_segments[j] != '\0'))
  856. {
  857. j++;
  858. }
  859. if (dcssblk_segments[j] == '\0')
  860. break;
  861. i = j;
  862. }
  863. }
  864. /*
  865. * The init/exit functions.
  866. */
  867. static void __exit
  868. dcssblk_exit(void)
  869. {
  870. s390_root_dev_unregister(dcssblk_root_dev);
  871. unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
  872. }
  873. static int __init
  874. dcssblk_init(void)
  875. {
  876. int rc;
  877. dcssblk_root_dev = s390_root_dev_register("dcssblk");
  878. if (IS_ERR(dcssblk_root_dev))
  879. return PTR_ERR(dcssblk_root_dev);
  880. rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
  881. if (rc) {
  882. s390_root_dev_unregister(dcssblk_root_dev);
  883. return rc;
  884. }
  885. rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
  886. if (rc) {
  887. s390_root_dev_unregister(dcssblk_root_dev);
  888. return rc;
  889. }
  890. rc = register_blkdev(0, DCSSBLK_NAME);
  891. if (rc < 0) {
  892. s390_root_dev_unregister(dcssblk_root_dev);
  893. return rc;
  894. }
  895. dcssblk_major = rc;
  896. init_rwsem(&dcssblk_devices_sem);
  897. dcssblk_check_params();
  898. return 0;
  899. }
  900. module_init(dcssblk_init);
  901. module_exit(dcssblk_exit);
  902. module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
  903. MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
  904. "comma-separated list, names in each set separated "
  905. "by commas are separated by colons, each set contains "
  906. "names of contiguous segments and each name max. 8 chars.\n"
  907. "Adding \"(local)\" to the end of each set equals echoing 0 "
  908. "to /sys/devices/dcssblk/<device name>/shared after loading "
  909. "the contiguous segments - \n"
  910. "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
  911. MODULE_LICENSE("GPL");