Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * dcssblk.c -- the S/390 block driver for dcss memory
0004  *
0005  * Authors: Carsten Otte, Stefan Weinhuber, Gerald Schaefer
0006  */
0007 
0008 #define KMSG_COMPONENT "dcssblk"
0009 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
0010 
0011 #include <linux/module.h>
0012 #include <linux/moduleparam.h>
0013 #include <linux/ctype.h>
0014 #include <linux/errno.h>
0015 #include <linux/init.h>
0016 #include <linux/slab.h>
0017 #include <linux/blkdev.h>
0018 #include <linux/completion.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/pfn_t.h>
0021 #include <linux/uio.h>
0022 #include <linux/dax.h>
0023 #include <asm/extmem.h>
0024 #include <asm/io.h>
0025 
0026 #define DCSSBLK_NAME "dcssblk"
0027 #define DCSSBLK_MINORS_PER_DISK 1
0028 #define DCSSBLK_PARM_LEN 400
0029 #define DCSS_BUS_ID_SIZE 20
0030 
0031 static int dcssblk_open(struct block_device *bdev, fmode_t mode);
0032 static void dcssblk_release(struct gendisk *disk, fmode_t mode);
0033 static void dcssblk_submit_bio(struct bio *bio);
0034 static long dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
0035         long nr_pages, enum dax_access_mode mode, void **kaddr,
0036         pfn_t *pfn);
0037 
0038 static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0";
0039 
0040 static int dcssblk_major;
0041 static const struct block_device_operations dcssblk_devops = {
0042     .owner      = THIS_MODULE,
0043     .submit_bio = dcssblk_submit_bio,
0044     .open       = dcssblk_open,
0045     .release    = dcssblk_release,
0046 };
0047 
0048 static int dcssblk_dax_zero_page_range(struct dax_device *dax_dev,
0049                        pgoff_t pgoff, size_t nr_pages)
0050 {
0051     long rc;
0052     void *kaddr;
0053 
0054     rc = dax_direct_access(dax_dev, pgoff, nr_pages, DAX_ACCESS,
0055             &kaddr, NULL);
0056     if (rc < 0)
0057         return rc;
0058     memset(kaddr, 0, nr_pages << PAGE_SHIFT);
0059     dax_flush(dax_dev, kaddr, nr_pages << PAGE_SHIFT);
0060     return 0;
0061 }
0062 
0063 static const struct dax_operations dcssblk_dax_ops = {
0064     .direct_access = dcssblk_dax_direct_access,
0065     .zero_page_range = dcssblk_dax_zero_page_range,
0066 };
0067 
0068 struct dcssblk_dev_info {
0069     struct list_head lh;
0070     struct device dev;
0071     char segment_name[DCSS_BUS_ID_SIZE];
0072     atomic_t use_count;
0073     struct gendisk *gd;
0074     unsigned long start;
0075     unsigned long end;
0076     int segment_type;
0077     unsigned char save_pending;
0078     unsigned char is_shared;
0079     int num_of_segments;
0080     struct list_head seg_list;
0081     struct dax_device *dax_dev;
0082 };
0083 
0084 struct segment_info {
0085     struct list_head lh;
0086     char segment_name[DCSS_BUS_ID_SIZE];
0087     unsigned long start;
0088     unsigned long end;
0089     int segment_type;
0090 };
0091 
0092 static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
0093                   size_t count);
0094 static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
0095                   size_t count);
0096 
0097 static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
0098 static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
0099 
0100 static struct device *dcssblk_root_dev;
0101 
0102 static LIST_HEAD(dcssblk_devices);
0103 static struct rw_semaphore dcssblk_devices_sem;
0104 
0105 /*
0106  * release function for segment device.
0107  */
0108 static void
0109 dcssblk_release_segment(struct device *dev)
0110 {
0111     struct dcssblk_dev_info *dev_info;
0112     struct segment_info *entry, *temp;
0113 
0114     dev_info = container_of(dev, struct dcssblk_dev_info, dev);
0115     list_for_each_entry_safe(entry, temp, &dev_info->seg_list, lh) {
0116         list_del(&entry->lh);
0117         kfree(entry);
0118     }
0119     kfree(dev_info);
0120     module_put(THIS_MODULE);
0121 }
0122 
0123 /*
0124  * get a minor number. needs to be called with
0125  * down_write(&dcssblk_devices_sem) and the
0126  * device needs to be enqueued before the semaphore is
0127  * freed.
0128  */
0129 static int
0130 dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info)
0131 {
0132     int minor, found;
0133     struct dcssblk_dev_info *entry;
0134 
0135     if (dev_info == NULL)
0136         return -EINVAL;
0137     for (minor = 0; minor < (1<<MINORBITS); minor++) {
0138         found = 0;
0139         // test if minor available
0140         list_for_each_entry(entry, &dcssblk_devices, lh)
0141             if (minor == entry->gd->first_minor)
0142                 found++;
0143         if (!found) break; // got unused minor
0144     }
0145     if (found)
0146         return -EBUSY;
0147     dev_info->gd->first_minor = minor;
0148     return 0;
0149 }
0150 
0151 /*
0152  * get the struct dcssblk_dev_info from dcssblk_devices
0153  * for the given name.
0154  * down_read(&dcssblk_devices_sem) must be held.
0155  */
0156 static struct dcssblk_dev_info *
0157 dcssblk_get_device_by_name(char *name)
0158 {
0159     struct dcssblk_dev_info *entry;
0160 
0161     list_for_each_entry(entry, &dcssblk_devices, lh) {
0162         if (!strcmp(name, entry->segment_name)) {
0163             return entry;
0164         }
0165     }
0166     return NULL;
0167 }
0168 
0169 /*
0170  * get the struct segment_info from seg_list
0171  * for the given name.
0172  * down_read(&dcssblk_devices_sem) must be held.
0173  */
0174 static struct segment_info *
0175 dcssblk_get_segment_by_name(char *name)
0176 {
0177     struct dcssblk_dev_info *dev_info;
0178     struct segment_info *entry;
0179 
0180     list_for_each_entry(dev_info, &dcssblk_devices, lh) {
0181         list_for_each_entry(entry, &dev_info->seg_list, lh) {
0182             if (!strcmp(name, entry->segment_name))
0183                 return entry;
0184         }
0185     }
0186     return NULL;
0187 }
0188 
0189 /*
0190  * get the highest address of the multi-segment block.
0191  */
0192 static unsigned long
0193 dcssblk_find_highest_addr(struct dcssblk_dev_info *dev_info)
0194 {
0195     unsigned long highest_addr;
0196     struct segment_info *entry;
0197 
0198     highest_addr = 0;
0199     list_for_each_entry(entry, &dev_info->seg_list, lh) {
0200         if (highest_addr < entry->end)
0201             highest_addr = entry->end;
0202     }
0203     return highest_addr;
0204 }
0205 
0206 /*
0207  * get the lowest address of the multi-segment block.
0208  */
0209 static unsigned long
0210 dcssblk_find_lowest_addr(struct dcssblk_dev_info *dev_info)
0211 {
0212     int set_first;
0213     unsigned long lowest_addr;
0214     struct segment_info *entry;
0215 
0216     set_first = 0;
0217     lowest_addr = 0;
0218     list_for_each_entry(entry, &dev_info->seg_list, lh) {
0219         if (set_first == 0) {
0220             lowest_addr = entry->start;
0221             set_first = 1;
0222         } else {
0223             if (lowest_addr > entry->start)
0224                 lowest_addr = entry->start;
0225         }
0226     }
0227     return lowest_addr;
0228 }
0229 
0230 /*
0231  * Check continuity of segments.
0232  */
0233 static int
0234 dcssblk_is_continuous(struct dcssblk_dev_info *dev_info)
0235 {
0236     int i, j, rc;
0237     struct segment_info *sort_list, *entry, temp;
0238 
0239     if (dev_info->num_of_segments <= 1)
0240         return 0;
0241 
0242     sort_list = kcalloc(dev_info->num_of_segments,
0243                 sizeof(struct segment_info),
0244                 GFP_KERNEL);
0245     if (sort_list == NULL)
0246         return -ENOMEM;
0247     i = 0;
0248     list_for_each_entry(entry, &dev_info->seg_list, lh) {
0249         memcpy(&sort_list[i], entry, sizeof(struct segment_info));
0250         i++;
0251     }
0252 
0253     /* sort segments */
0254     for (i = 0; i < dev_info->num_of_segments; i++)
0255         for (j = 0; j < dev_info->num_of_segments; j++)
0256             if (sort_list[j].start > sort_list[i].start) {
0257                 memcpy(&temp, &sort_list[i],
0258                     sizeof(struct segment_info));
0259                 memcpy(&sort_list[i], &sort_list[j],
0260                     sizeof(struct segment_info));
0261                 memcpy(&sort_list[j], &temp,
0262                     sizeof(struct segment_info));
0263             }
0264 
0265     /* check continuity */
0266     for (i = 0; i < dev_info->num_of_segments - 1; i++) {
0267         if ((sort_list[i].end + 1) != sort_list[i+1].start) {
0268             pr_err("Adjacent DCSSs %s and %s are not "
0269                    "contiguous\n", sort_list[i].segment_name,
0270                    sort_list[i+1].segment_name);
0271             rc = -EINVAL;
0272             goto out;
0273         }
0274         /* EN and EW are allowed in a block device */
0275         if (sort_list[i].segment_type != sort_list[i+1].segment_type) {
0276             if (!(sort_list[i].segment_type & SEGMENT_EXCLUSIVE) ||
0277                 (sort_list[i].segment_type == SEG_TYPE_ER) ||
0278                 !(sort_list[i+1].segment_type &
0279                 SEGMENT_EXCLUSIVE) ||
0280                 (sort_list[i+1].segment_type == SEG_TYPE_ER)) {
0281                 pr_err("DCSS %s and DCSS %s have "
0282                        "incompatible types\n",
0283                        sort_list[i].segment_name,
0284                        sort_list[i+1].segment_name);
0285                 rc = -EINVAL;
0286                 goto out;
0287             }
0288         }
0289     }
0290     rc = 0;
0291 out:
0292     kfree(sort_list);
0293     return rc;
0294 }
0295 
0296 /*
0297  * Load a segment
0298  */
0299 static int
0300 dcssblk_load_segment(char *name, struct segment_info **seg_info)
0301 {
0302     int rc;
0303 
0304     /* already loaded? */
0305     down_read(&dcssblk_devices_sem);
0306     *seg_info = dcssblk_get_segment_by_name(name);
0307     up_read(&dcssblk_devices_sem);
0308     if (*seg_info != NULL)
0309         return -EEXIST;
0310 
0311     /* get a struct segment_info */
0312     *seg_info = kzalloc(sizeof(struct segment_info), GFP_KERNEL);
0313     if (*seg_info == NULL)
0314         return -ENOMEM;
0315 
0316     strcpy((*seg_info)->segment_name, name);
0317 
0318     /* load the segment */
0319     rc = segment_load(name, SEGMENT_SHARED,
0320             &(*seg_info)->start, &(*seg_info)->end);
0321     if (rc < 0) {
0322         segment_warning(rc, (*seg_info)->segment_name);
0323         kfree(*seg_info);
0324     } else {
0325         INIT_LIST_HEAD(&(*seg_info)->lh);
0326         (*seg_info)->segment_type = rc;
0327     }
0328     return rc;
0329 }
0330 
0331 /*
0332  * device attribute for switching shared/nonshared (exclusive)
0333  * operation (show + store)
0334  */
0335 static ssize_t
0336 dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
0337 {
0338     struct dcssblk_dev_info *dev_info;
0339 
0340     dev_info = container_of(dev, struct dcssblk_dev_info, dev);
0341     return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
0342 }
0343 
0344 static ssize_t
0345 dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
0346 {
0347     struct dcssblk_dev_info *dev_info;
0348     struct segment_info *entry, *temp;
0349     int rc;
0350 
0351     if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
0352         return -EINVAL;
0353     down_write(&dcssblk_devices_sem);
0354     dev_info = container_of(dev, struct dcssblk_dev_info, dev);
0355     if (atomic_read(&dev_info->use_count)) {
0356         rc = -EBUSY;
0357         goto out;
0358     }
0359     if (inbuf[0] == '1') {
0360         /* reload segments in shared mode */
0361         list_for_each_entry(entry, &dev_info->seg_list, lh) {
0362             rc = segment_modify_shared(entry->segment_name,
0363                         SEGMENT_SHARED);
0364             if (rc < 0) {
0365                 BUG_ON(rc == -EINVAL);
0366                 if (rc != -EAGAIN)
0367                     goto removeseg;
0368             }
0369         }
0370         dev_info->is_shared = 1;
0371         switch (dev_info->segment_type) {
0372         case SEG_TYPE_SR:
0373         case SEG_TYPE_ER:
0374         case SEG_TYPE_SC:
0375             set_disk_ro(dev_info->gd, 1);
0376         }
0377     } else if (inbuf[0] == '0') {
0378         /* reload segments in exclusive mode */
0379         if (dev_info->segment_type == SEG_TYPE_SC) {
0380             pr_err("DCSS %s is of type SC and cannot be "
0381                    "loaded as exclusive-writable\n",
0382                    dev_info->segment_name);
0383             rc = -EINVAL;
0384             goto out;
0385         }
0386         list_for_each_entry(entry, &dev_info->seg_list, lh) {
0387             rc = segment_modify_shared(entry->segment_name,
0388                            SEGMENT_EXCLUSIVE);
0389             if (rc < 0) {
0390                 BUG_ON(rc == -EINVAL);
0391                 if (rc != -EAGAIN)
0392                     goto removeseg;
0393             }
0394         }
0395         dev_info->is_shared = 0;
0396         set_disk_ro(dev_info->gd, 0);
0397     } else {
0398         rc = -EINVAL;
0399         goto out;
0400     }
0401     rc = count;
0402     goto out;
0403 
0404 removeseg:
0405     pr_err("DCSS device %s is removed after a failed access mode "
0406            "change\n", dev_info->segment_name);
0407     temp = entry;
0408     list_for_each_entry(entry, &dev_info->seg_list, lh) {
0409         if (entry != temp)
0410             segment_unload(entry->segment_name);
0411     }
0412     list_del(&dev_info->lh);
0413 
0414     kill_dax(dev_info->dax_dev);
0415     put_dax(dev_info->dax_dev);
0416     del_gendisk(dev_info->gd);
0417     put_disk(dev_info->gd);
0418     up_write(&dcssblk_devices_sem);
0419 
0420     if (device_remove_file_self(dev, attr)) {
0421         device_unregister(dev);
0422         put_device(dev);
0423     }
0424     return rc;
0425 out:
0426     up_write(&dcssblk_devices_sem);
0427     return rc;
0428 }
0429 static DEVICE_ATTR(shared, S_IWUSR | S_IRUSR, dcssblk_shared_show,
0430            dcssblk_shared_store);
0431 
0432 /*
0433  * device attribute for save operation on current copy
0434  * of the segment. If the segment is busy, saving will
0435  * become pending until it gets released, which can be
0436  * undone by storing a non-true value to this entry.
0437  * (show + store)
0438  */
0439 static ssize_t
0440 dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
0441 {
0442     struct dcssblk_dev_info *dev_info;
0443 
0444     dev_info = container_of(dev, struct dcssblk_dev_info, dev);
0445     return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
0446 }
0447 
0448 static ssize_t
0449 dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
0450 {
0451     struct dcssblk_dev_info *dev_info;
0452     struct segment_info *entry;
0453 
0454     if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
0455         return -EINVAL;
0456     dev_info = container_of(dev, struct dcssblk_dev_info, dev);
0457 
0458     down_write(&dcssblk_devices_sem);
0459     if (inbuf[0] == '1') {
0460         if (atomic_read(&dev_info->use_count) == 0) {
0461             // device is idle => we save immediately
0462             pr_info("All DCSSs that map to device %s are "
0463                 "saved\n", dev_info->segment_name);
0464             list_for_each_entry(entry, &dev_info->seg_list, lh) {
0465                 if (entry->segment_type == SEG_TYPE_EN ||
0466                     entry->segment_type == SEG_TYPE_SN)
0467                     pr_warn("DCSS %s is of type SN or EN"
0468                         " and cannot be saved\n",
0469                         entry->segment_name);
0470                 else
0471                     segment_save(entry->segment_name);
0472             }
0473         }  else {
0474             // device is busy => we save it when it becomes
0475             // idle in dcssblk_release
0476             pr_info("Device %s is in use, its DCSSs will be "
0477                 "saved when it becomes idle\n",
0478                 dev_info->segment_name);
0479             dev_info->save_pending = 1;
0480         }
0481     } else if (inbuf[0] == '0') {
0482         if (dev_info->save_pending) {
0483             // device is busy & the user wants to undo his save
0484             // request
0485             dev_info->save_pending = 0;
0486             pr_info("A pending save request for device %s "
0487                 "has been canceled\n",
0488                 dev_info->segment_name);
0489         }
0490     } else {
0491         up_write(&dcssblk_devices_sem);
0492         return -EINVAL;
0493     }
0494     up_write(&dcssblk_devices_sem);
0495     return count;
0496 }
0497 static DEVICE_ATTR(save, S_IWUSR | S_IRUSR, dcssblk_save_show,
0498            dcssblk_save_store);
0499 
0500 /*
0501  * device attribute for showing all segments in a device
0502  */
0503 static ssize_t
0504 dcssblk_seglist_show(struct device *dev, struct device_attribute *attr,
0505         char *buf)
0506 {
0507     int i;
0508 
0509     struct dcssblk_dev_info *dev_info;
0510     struct segment_info *entry;
0511 
0512     down_read(&dcssblk_devices_sem);
0513     dev_info = container_of(dev, struct dcssblk_dev_info, dev);
0514     i = 0;
0515     buf[0] = '\0';
0516     list_for_each_entry(entry, &dev_info->seg_list, lh) {
0517         strcpy(&buf[i], entry->segment_name);
0518         i += strlen(entry->segment_name);
0519         buf[i] = '\n';
0520         i++;
0521     }
0522     up_read(&dcssblk_devices_sem);
0523     return i;
0524 }
0525 static DEVICE_ATTR(seglist, S_IRUSR, dcssblk_seglist_show, NULL);
0526 
0527 static struct attribute *dcssblk_dev_attrs[] = {
0528     &dev_attr_shared.attr,
0529     &dev_attr_save.attr,
0530     &dev_attr_seglist.attr,
0531     NULL,
0532 };
0533 static struct attribute_group dcssblk_dev_attr_group = {
0534     .attrs = dcssblk_dev_attrs,
0535 };
0536 static const struct attribute_group *dcssblk_dev_attr_groups[] = {
0537     &dcssblk_dev_attr_group,
0538     NULL,
0539 };
0540 
0541 /*
0542  * device attribute for adding devices
0543  */
0544 static ssize_t
0545 dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
0546 {
0547     int rc, i, j, num_of_segments;
0548     struct dcssblk_dev_info *dev_info;
0549     struct segment_info *seg_info, *temp;
0550     char *local_buf;
0551     unsigned long seg_byte_size;
0552 
0553     dev_info = NULL;
0554     seg_info = NULL;
0555     if (dev != dcssblk_root_dev) {
0556         rc = -EINVAL;
0557         goto out_nobuf;
0558     }
0559     if ((count < 1) || (buf[0] == '\0') || (buf[0] == '\n')) {
0560         rc = -ENAMETOOLONG;
0561         goto out_nobuf;
0562     }
0563 
0564     local_buf = kmalloc(count + 1, GFP_KERNEL);
0565     if (local_buf == NULL) {
0566         rc = -ENOMEM;
0567         goto out_nobuf;
0568     }
0569 
0570     /*
0571      * parse input
0572      */
0573     num_of_segments = 0;
0574     for (i = 0; (i < count && (buf[i] != '\0') && (buf[i] != '\n')); i++) {
0575         for (j = i; j < count &&
0576             (buf[j] != ':') &&
0577             (buf[j] != '\0') &&
0578             (buf[j] != '\n'); j++) {
0579             local_buf[j-i] = toupper(buf[j]);
0580         }
0581         local_buf[j-i] = '\0';
0582         if (((j - i) == 0) || ((j - i) > 8)) {
0583             rc = -ENAMETOOLONG;
0584             goto seg_list_del;
0585         }
0586 
0587         rc = dcssblk_load_segment(local_buf, &seg_info);
0588         if (rc < 0)
0589             goto seg_list_del;
0590         /*
0591          * get a struct dcssblk_dev_info
0592          */
0593         if (num_of_segments == 0) {
0594             dev_info = kzalloc(sizeof(struct dcssblk_dev_info),
0595                     GFP_KERNEL);
0596             if (dev_info == NULL) {
0597                 rc = -ENOMEM;
0598                 goto out;
0599             }
0600             strcpy(dev_info->segment_name, local_buf);
0601             dev_info->segment_type = seg_info->segment_type;
0602             INIT_LIST_HEAD(&dev_info->seg_list);
0603         }
0604         list_add_tail(&seg_info->lh, &dev_info->seg_list);
0605         num_of_segments++;
0606         i = j;
0607 
0608         if ((buf[j] == '\0') || (buf[j] == '\n'))
0609             break;
0610     }
0611 
0612     /* no trailing colon at the end of the input */
0613     if ((i > 0) && (buf[i-1] == ':')) {
0614         rc = -ENAMETOOLONG;
0615         goto seg_list_del;
0616     }
0617     strlcpy(local_buf, buf, i + 1);
0618     dev_info->num_of_segments = num_of_segments;
0619     rc = dcssblk_is_continuous(dev_info);
0620     if (rc < 0)
0621         goto seg_list_del;
0622 
0623     dev_info->start = dcssblk_find_lowest_addr(dev_info);
0624     dev_info->end = dcssblk_find_highest_addr(dev_info);
0625 
0626     dev_set_name(&dev_info->dev, "%s", dev_info->segment_name);
0627     dev_info->dev.release = dcssblk_release_segment;
0628     dev_info->dev.groups = dcssblk_dev_attr_groups;
0629     INIT_LIST_HEAD(&dev_info->lh);
0630     dev_info->gd = blk_alloc_disk(NUMA_NO_NODE);
0631     if (dev_info->gd == NULL) {
0632         rc = -ENOMEM;
0633         goto seg_list_del;
0634     }
0635     dev_info->gd->major = dcssblk_major;
0636     dev_info->gd->minors = DCSSBLK_MINORS_PER_DISK;
0637     dev_info->gd->fops = &dcssblk_devops;
0638     dev_info->gd->private_data = dev_info;
0639     blk_queue_logical_block_size(dev_info->gd->queue, 4096);
0640     blk_queue_flag_set(QUEUE_FLAG_DAX, dev_info->gd->queue);
0641 
0642     seg_byte_size = (dev_info->end - dev_info->start + 1);
0643     set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
0644     pr_info("Loaded %s with total size %lu bytes and capacity %lu "
0645         "sectors\n", local_buf, seg_byte_size, seg_byte_size >> 9);
0646 
0647     dev_info->save_pending = 0;
0648     dev_info->is_shared = 1;
0649     dev_info->dev.parent = dcssblk_root_dev;
0650 
0651     /*
0652      *get minor, add to list
0653      */
0654     down_write(&dcssblk_devices_sem);
0655     if (dcssblk_get_segment_by_name(local_buf)) {
0656         rc = -EEXIST;
0657         goto release_gd;
0658     }
0659     rc = dcssblk_assign_free_minor(dev_info);
0660     if (rc)
0661         goto release_gd;
0662     sprintf(dev_info->gd->disk_name, "dcssblk%d",
0663         dev_info->gd->first_minor);
0664     list_add_tail(&dev_info->lh, &dcssblk_devices);
0665 
0666     if (!try_module_get(THIS_MODULE)) {
0667         rc = -ENODEV;
0668         goto dev_list_del;
0669     }
0670     /*
0671      * register the device
0672      */
0673     rc = device_register(&dev_info->dev);
0674     if (rc)
0675         goto put_dev;
0676 
0677     dev_info->dax_dev = alloc_dax(dev_info, &dcssblk_dax_ops);
0678     if (IS_ERR(dev_info->dax_dev)) {
0679         rc = PTR_ERR(dev_info->dax_dev);
0680         dev_info->dax_dev = NULL;
0681         goto put_dev;
0682     }
0683     set_dax_synchronous(dev_info->dax_dev);
0684     rc = dax_add_host(dev_info->dax_dev, dev_info->gd);
0685     if (rc)
0686         goto out_dax;
0687 
0688     get_device(&dev_info->dev);
0689     rc = device_add_disk(&dev_info->dev, dev_info->gd, NULL);
0690     if (rc)
0691         goto out_dax_host;
0692 
0693     switch (dev_info->segment_type) {
0694         case SEG_TYPE_SR:
0695         case SEG_TYPE_ER:
0696         case SEG_TYPE_SC:
0697             set_disk_ro(dev_info->gd,1);
0698             break;
0699         default:
0700             set_disk_ro(dev_info->gd,0);
0701             break;
0702     }
0703     up_write(&dcssblk_devices_sem);
0704     rc = count;
0705     goto out;
0706 
0707 out_dax_host:
0708     dax_remove_host(dev_info->gd);
0709 out_dax:
0710     put_device(&dev_info->dev);
0711     kill_dax(dev_info->dax_dev);
0712     put_dax(dev_info->dax_dev);
0713 put_dev:
0714     list_del(&dev_info->lh);
0715     put_disk(dev_info->gd);
0716     list_for_each_entry(seg_info, &dev_info->seg_list, lh) {
0717         segment_unload(seg_info->segment_name);
0718     }
0719     put_device(&dev_info->dev);
0720     up_write(&dcssblk_devices_sem);
0721     goto out;
0722 dev_list_del:
0723     list_del(&dev_info->lh);
0724 release_gd:
0725     put_disk(dev_info->gd);
0726     up_write(&dcssblk_devices_sem);
0727 seg_list_del:
0728     if (dev_info == NULL)
0729         goto out;
0730     list_for_each_entry_safe(seg_info, temp, &dev_info->seg_list, lh) {
0731         list_del(&seg_info->lh);
0732         segment_unload(seg_info->segment_name);
0733         kfree(seg_info);
0734     }
0735     kfree(dev_info);
0736 out:
0737     kfree(local_buf);
0738 out_nobuf:
0739     return rc;
0740 }
0741 
0742 /*
0743  * device attribute for removing devices
0744  */
0745 static ssize_t
0746 dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
0747 {
0748     struct dcssblk_dev_info *dev_info;
0749     struct segment_info *entry;
0750     int rc, i;
0751     char *local_buf;
0752 
0753     if (dev != dcssblk_root_dev) {
0754         return -EINVAL;
0755     }
0756     local_buf = kmalloc(count + 1, GFP_KERNEL);
0757     if (local_buf == NULL) {
0758         return -ENOMEM;
0759     }
0760     /*
0761      * parse input
0762      */
0763     for (i = 0; (i < count && (*(buf+i)!='\0') && (*(buf+i)!='\n')); i++) {
0764         local_buf[i] = toupper(buf[i]);
0765     }
0766     local_buf[i] = '\0';
0767     if ((i == 0) || (i > 8)) {
0768         rc = -ENAMETOOLONG;
0769         goto out_buf;
0770     }
0771 
0772     down_write(&dcssblk_devices_sem);
0773     dev_info = dcssblk_get_device_by_name(local_buf);
0774     if (dev_info == NULL) {
0775         up_write(&dcssblk_devices_sem);
0776         pr_warn("Device %s cannot be removed because it is not a known device\n",
0777             local_buf);
0778         rc = -ENODEV;
0779         goto out_buf;
0780     }
0781     if (atomic_read(&dev_info->use_count) != 0) {
0782         up_write(&dcssblk_devices_sem);
0783         pr_warn("Device %s cannot be removed while it is in use\n",
0784             local_buf);
0785         rc = -EBUSY;
0786         goto out_buf;
0787     }
0788 
0789     list_del(&dev_info->lh);
0790     kill_dax(dev_info->dax_dev);
0791     put_dax(dev_info->dax_dev);
0792     del_gendisk(dev_info->gd);
0793     put_disk(dev_info->gd);
0794 
0795     /* unload all related segments */
0796     list_for_each_entry(entry, &dev_info->seg_list, lh)
0797         segment_unload(entry->segment_name);
0798 
0799     up_write(&dcssblk_devices_sem);
0800 
0801     device_unregister(&dev_info->dev);
0802     put_device(&dev_info->dev);
0803 
0804     rc = count;
0805 out_buf:
0806     kfree(local_buf);
0807     return rc;
0808 }
0809 
0810 static int
0811 dcssblk_open(struct block_device *bdev, fmode_t mode)
0812 {
0813     struct dcssblk_dev_info *dev_info;
0814     int rc;
0815 
0816     dev_info = bdev->bd_disk->private_data;
0817     if (NULL == dev_info) {
0818         rc = -ENODEV;
0819         goto out;
0820     }
0821     atomic_inc(&dev_info->use_count);
0822     rc = 0;
0823 out:
0824     return rc;
0825 }
0826 
0827 static void
0828 dcssblk_release(struct gendisk *disk, fmode_t mode)
0829 {
0830     struct dcssblk_dev_info *dev_info = disk->private_data;
0831     struct segment_info *entry;
0832 
0833     if (!dev_info) {
0834         WARN_ON(1);
0835         return;
0836     }
0837     down_write(&dcssblk_devices_sem);
0838     if (atomic_dec_and_test(&dev_info->use_count)
0839         && (dev_info->save_pending)) {
0840         pr_info("Device %s has become idle and is being saved "
0841             "now\n", dev_info->segment_name);
0842         list_for_each_entry(entry, &dev_info->seg_list, lh) {
0843             if (entry->segment_type == SEG_TYPE_EN ||
0844                 entry->segment_type == SEG_TYPE_SN)
0845                 pr_warn("DCSS %s is of type SN or EN and cannot"
0846                     " be saved\n", entry->segment_name);
0847             else
0848                 segment_save(entry->segment_name);
0849         }
0850         dev_info->save_pending = 0;
0851     }
0852     up_write(&dcssblk_devices_sem);
0853 }
0854 
0855 static void
0856 dcssblk_submit_bio(struct bio *bio)
0857 {
0858     struct dcssblk_dev_info *dev_info;
0859     struct bio_vec bvec;
0860     struct bvec_iter iter;
0861     unsigned long index;
0862     unsigned long page_addr;
0863     unsigned long source_addr;
0864     unsigned long bytes_done;
0865 
0866     bio = bio_split_to_limits(bio);
0867 
0868     bytes_done = 0;
0869     dev_info = bio->bi_bdev->bd_disk->private_data;
0870     if (dev_info == NULL)
0871         goto fail;
0872     if ((bio->bi_iter.bi_sector & 7) != 0 ||
0873         (bio->bi_iter.bi_size & 4095) != 0)
0874         /* Request is not page-aligned. */
0875         goto fail;
0876     /* verify data transfer direction */
0877     if (dev_info->is_shared) {
0878         switch (dev_info->segment_type) {
0879         case SEG_TYPE_SR:
0880         case SEG_TYPE_ER:
0881         case SEG_TYPE_SC:
0882             /* cannot write to these segments */
0883             if (bio_data_dir(bio) == WRITE) {
0884                 pr_warn("Writing to %s failed because it is a read-only device\n",
0885                     dev_name(&dev_info->dev));
0886                 goto fail;
0887             }
0888         }
0889     }
0890 
0891     index = (bio->bi_iter.bi_sector >> 3);
0892     bio_for_each_segment(bvec, bio, iter) {
0893         page_addr = (unsigned long)bvec_virt(&bvec);
0894         source_addr = dev_info->start + (index<<12) + bytes_done;
0895         if (unlikely((page_addr & 4095) != 0) || (bvec.bv_len & 4095) != 0)
0896             // More paranoia.
0897             goto fail;
0898         if (bio_data_dir(bio) == READ) {
0899             memcpy((void*)page_addr, (void*)source_addr,
0900                 bvec.bv_len);
0901         } else {
0902             memcpy((void*)source_addr, (void*)page_addr,
0903                 bvec.bv_len);
0904         }
0905         bytes_done += bvec.bv_len;
0906     }
0907     bio_endio(bio);
0908     return;
0909 fail:
0910     bio_io_error(bio);
0911 }
0912 
0913 static long
0914 __dcssblk_direct_access(struct dcssblk_dev_info *dev_info, pgoff_t pgoff,
0915         long nr_pages, void **kaddr, pfn_t *pfn)
0916 {
0917     resource_size_t offset = pgoff * PAGE_SIZE;
0918     unsigned long dev_sz;
0919 
0920     dev_sz = dev_info->end - dev_info->start + 1;
0921     if (kaddr)
0922         *kaddr = (void *) dev_info->start + offset;
0923     if (pfn)
0924         *pfn = __pfn_to_pfn_t(PFN_DOWN(dev_info->start + offset),
0925                 PFN_DEV|PFN_SPECIAL);
0926 
0927     return (dev_sz - offset) / PAGE_SIZE;
0928 }
0929 
0930 static long
0931 dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
0932         long nr_pages, enum dax_access_mode mode, void **kaddr,
0933         pfn_t *pfn)
0934 {
0935     struct dcssblk_dev_info *dev_info = dax_get_private(dax_dev);
0936 
0937     return __dcssblk_direct_access(dev_info, pgoff, nr_pages, kaddr, pfn);
0938 }
0939 
0940 static void
0941 dcssblk_check_params(void)
0942 {
0943     int rc, i, j, k;
0944     char buf[DCSSBLK_PARM_LEN + 1];
0945     struct dcssblk_dev_info *dev_info;
0946 
0947     for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
0948          i++) {
0949         for (j = i; (j < DCSSBLK_PARM_LEN) &&
0950                 (dcssblk_segments[j] != ',')  &&
0951                 (dcssblk_segments[j] != '\0') &&
0952                 (dcssblk_segments[j] != '('); j++)
0953         {
0954             buf[j-i] = dcssblk_segments[j];
0955         }
0956         buf[j-i] = '\0';
0957         rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
0958         if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
0959             for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++)
0960                 buf[k] = toupper(buf[k]);
0961             buf[k] = '\0';
0962             if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
0963                 down_read(&dcssblk_devices_sem);
0964                 dev_info = dcssblk_get_device_by_name(buf);
0965                 up_read(&dcssblk_devices_sem);
0966                 if (dev_info)
0967                     dcssblk_shared_store(&dev_info->dev,
0968                                  NULL, "0\n", 2);
0969             }
0970         }
0971         while ((dcssblk_segments[j] != ',') &&
0972                (dcssblk_segments[j] != '\0'))
0973         {
0974             j++;
0975         }
0976         if (dcssblk_segments[j] == '\0')
0977             break;
0978         i = j;
0979     }
0980 }
0981 
0982 /*
0983  * The init/exit functions.
0984  */
0985 static void __exit
0986 dcssblk_exit(void)
0987 {
0988     root_device_unregister(dcssblk_root_dev);
0989     unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
0990 }
0991 
0992 static int __init
0993 dcssblk_init(void)
0994 {
0995     int rc;
0996 
0997     dcssblk_root_dev = root_device_register("dcssblk");
0998     if (IS_ERR(dcssblk_root_dev))
0999         return PTR_ERR(dcssblk_root_dev);
1000     rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
1001     if (rc)
1002         goto out_root;
1003     rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
1004     if (rc)
1005         goto out_root;
1006     rc = register_blkdev(0, DCSSBLK_NAME);
1007     if (rc < 0)
1008         goto out_root;
1009     dcssblk_major = rc;
1010     init_rwsem(&dcssblk_devices_sem);
1011 
1012     dcssblk_check_params();
1013     return 0;
1014 
1015 out_root:
1016     root_device_unregister(dcssblk_root_dev);
1017 
1018     return rc;
1019 }
1020 
1021 module_init(dcssblk_init);
1022 module_exit(dcssblk_exit);
1023 
1024 module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
1025 MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
1026          "comma-separated list, names in each set separated "
1027          "by commas are separated by colons, each set contains "
1028          "names of contiguous segments and each name max. 8 chars.\n"
1029          "Adding \"(local)\" to the end of each set equals echoing 0 "
1030          "to /sys/devices/dcssblk/<device name>/shared after loading "
1031          "the contiguous segments - \n"
1032          "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
1033 
1034 MODULE_LICENSE("GPL");