Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / s390 / block / dasd_ioctl.c
1 /*
2  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
3  *                  Horst Hummel <Horst.Hummel@de.ibm.com>
4  *                  Carsten Otte <Cotte@de.ibm.com>
5  *                  Martin Schwidefsky <schwidefsky@de.ibm.com>
6  * Bugreports.to..: <Linux390@de.ibm.com>
7  * Copyright IBM Corp. 1999, 2001
8  *
9  * i/o controls for the dasd driver.
10  */
11
12 #define KMSG_COMPONENT "dasd"
13
14 #include <linux/interrupt.h>
15 #include <linux/compat.h>
16 #include <linux/major.h>
17 #include <linux/fs.h>
18 #include <linux/blkpg.h>
19 #include <linux/slab.h>
20 #include <asm/compat.h>
21 #include <asm/ccwdev.h>
22 #include <asm/schid.h>
23 #include <asm/cmb.h>
24 #include <asm/uaccess.h>
25
26 /* This is ugly... */
27 #define PRINTK_HEADER "dasd_ioctl:"
28
29 #include "dasd_int.h"
30
31
32 static int
33 dasd_ioctl_api_version(void __user *argp)
34 {
35         int ver = DASD_API_VERSION;
36         return put_user(ver, (int __user *)argp);
37 }
38
39 /*
40  * Enable device.
41  * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
42  */
43 static int
44 dasd_ioctl_enable(struct block_device *bdev)
45 {
46         struct dasd_device *base;
47
48         if (!capable(CAP_SYS_ADMIN))
49                 return -EACCES;
50
51         base = dasd_device_from_gendisk(bdev->bd_disk);
52         if (!base)
53                 return -ENODEV;
54
55         dasd_enable_device(base);
56         /* Formatting the dasd device can change the capacity. */
57         mutex_lock(&bdev->bd_mutex);
58         i_size_write(bdev->bd_inode,
59                      (loff_t)get_capacity(base->block->gdp) << 9);
60         mutex_unlock(&bdev->bd_mutex);
61         dasd_put_device(base);
62         return 0;
63 }
64
65 /*
66  * Disable device.
67  * Used by dasdfmt. Disable I/O operations but allow ioctls.
68  */
69 static int
70 dasd_ioctl_disable(struct block_device *bdev)
71 {
72         struct dasd_device *base;
73
74         if (!capable(CAP_SYS_ADMIN))
75                 return -EACCES;
76
77         base = dasd_device_from_gendisk(bdev->bd_disk);
78         if (!base)
79                 return -ENODEV;
80         /*
81          * Man this is sick. We don't do a real disable but only downgrade
82          * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
83          * BIODASDDISABLE to disable accesses to the device via the block
84          * device layer but it still wants to do i/o on the device by
85          * using the BIODASDFMT ioctl. Therefore the correct state for the
86          * device is DASD_STATE_BASIC that allows to do basic i/o.
87          */
88         dasd_set_target_state(base, DASD_STATE_BASIC);
89         /*
90          * Set i_size to zero, since read, write, etc. check against this
91          * value.
92          */
93         mutex_lock(&bdev->bd_mutex);
94         i_size_write(bdev->bd_inode, 0);
95         mutex_unlock(&bdev->bd_mutex);
96         dasd_put_device(base);
97         return 0;
98 }
99
100 /*
101  * Quiesce device.
102  */
103 static int dasd_ioctl_quiesce(struct dasd_block *block)
104 {
105         unsigned long flags;
106         struct dasd_device *base;
107
108         base = block->base;
109         if (!capable (CAP_SYS_ADMIN))
110                 return -EACCES;
111
112         pr_info("%s: The DASD has been put in the quiesce "
113                 "state\n", dev_name(&base->cdev->dev));
114         spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
115         dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
116         spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
117         return 0;
118 }
119
120
121 /*
122  * Resume device.
123  */
124 static int dasd_ioctl_resume(struct dasd_block *block)
125 {
126         unsigned long flags;
127         struct dasd_device *base;
128
129         base = block->base;
130         if (!capable (CAP_SYS_ADMIN))
131                 return -EACCES;
132
133         pr_info("%s: I/O operations have been resumed "
134                 "on the DASD\n", dev_name(&base->cdev->dev));
135         spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
136         dasd_device_remove_stop_bits(base, DASD_STOPPED_QUIESCE);
137         spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
138
139         dasd_schedule_block_bh(block);
140         return 0;
141 }
142
143 /*
144  * Abort all failfast I/O on a device.
145  */
146 static int dasd_ioctl_abortio(struct dasd_block *block)
147 {
148         unsigned long flags;
149         struct dasd_device *base;
150         struct dasd_ccw_req *cqr, *n;
151
152         base = block->base;
153         if (!capable(CAP_SYS_ADMIN))
154                 return -EACCES;
155
156         if (test_and_set_bit(DASD_FLAG_ABORTALL, &base->flags))
157                 return 0;
158         DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag set");
159
160         spin_lock_irqsave(&block->request_queue_lock, flags);
161         spin_lock(&block->queue_lock);
162         list_for_each_entry_safe(cqr, n, &block->ccw_queue, blocklist) {
163                 if (test_bit(DASD_CQR_FLAGS_FAILFAST, &cqr->flags) &&
164                     cqr->callback_data &&
165                     cqr->callback_data != DASD_SLEEPON_START_TAG &&
166                     cqr->callback_data != DASD_SLEEPON_END_TAG) {
167                         spin_unlock(&block->queue_lock);
168                         blk_abort_request(cqr->callback_data);
169                         spin_lock(&block->queue_lock);
170                 }
171         }
172         spin_unlock(&block->queue_lock);
173         spin_unlock_irqrestore(&block->request_queue_lock, flags);
174
175         dasd_schedule_block_bh(block);
176         return 0;
177 }
178
179 /*
180  * Allow I/O on a device
181  */
182 static int dasd_ioctl_allowio(struct dasd_block *block)
183 {
184         struct dasd_device *base;
185
186         base = block->base;
187         if (!capable(CAP_SYS_ADMIN))
188                 return -EACCES;
189
190         if (test_and_clear_bit(DASD_FLAG_ABORTALL, &base->flags))
191                 DBF_DEV_EVENT(DBF_NOTICE, base, "%s", "abortall flag unset");
192
193         return 0;
194 }
195
196 /*
197  * performs formatting of _device_ according to _fdata_
198  * Note: The discipline's format_function is assumed to deliver formatting
199  * commands to format multiple units of the device. In terms of the ECKD
200  * devices this means CCWs are generated to format multiple tracks.
201  */
202 static int
203 dasd_format(struct dasd_block *block, struct format_data_t *fdata)
204 {
205         struct dasd_device *base;
206         int enable_pav = 1;
207         int rc, retries;
208         int start, stop;
209
210         base = block->base;
211         if (base->discipline->format_device == NULL)
212                 return -EPERM;
213
214         if (base->state != DASD_STATE_BASIC) {
215                 pr_warn("%s: The DASD cannot be formatted while it is enabled\n",
216                         dev_name(&base->cdev->dev));
217                 return -EBUSY;
218         }
219
220         DBF_DEV_EVENT(DBF_NOTICE, base,
221                       "formatting units %u to %u (%u B blocks) flags %u",
222                       fdata->start_unit,
223                       fdata->stop_unit, fdata->blksize, fdata->intensity);
224
225         /* Since dasdfmt keeps the device open after it was disabled,
226          * there still exists an inode for this device.
227          * We must update i_blkbits, otherwise we might get errors when
228          * enabling the device later.
229          */
230         if (fdata->start_unit == 0) {
231                 struct block_device *bdev = bdget_disk(block->gdp, 0);
232                 bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
233                 bdput(bdev);
234         }
235
236         retries = 255;
237         /* backup start- and endtrack for retries */
238         start = fdata->start_unit;
239         stop = fdata->stop_unit;
240         do {
241                 rc = base->discipline->format_device(base, fdata, enable_pav);
242                 if (rc) {
243                         if (rc == -EAGAIN) {
244                                 retries--;
245                                 /* disable PAV in case of errors */
246                                 enable_pav = 0;
247                                 fdata->start_unit = start;
248                                 fdata->stop_unit = stop;
249                         } else
250                                 return rc;
251                 } else
252                         /* success */
253                         break;
254         } while (retries);
255
256         if (!retries)
257                 return -EIO;
258         else
259                 return 0;
260 }
261
262 /*
263  * Format device.
264  */
265 static int
266 dasd_ioctl_format(struct block_device *bdev, void __user *argp)
267 {
268         struct dasd_device *base;
269         struct format_data_t fdata;
270         int rc;
271
272         if (!capable(CAP_SYS_ADMIN))
273                 return -EACCES;
274         if (!argp)
275                 return -EINVAL;
276         base = dasd_device_from_gendisk(bdev->bd_disk);
277         if (!base)
278                 return -ENODEV;
279         if (base->features & DASD_FEATURE_READONLY ||
280             test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
281                 dasd_put_device(base);
282                 return -EROFS;
283         }
284         if (copy_from_user(&fdata, argp, sizeof(struct format_data_t))) {
285                 dasd_put_device(base);
286                 return -EFAULT;
287         }
288         if (bdev != bdev->bd_contains) {
289                 pr_warning("%s: The specified DASD is a partition and cannot "
290                            "be formatted\n",
291                            dev_name(&base->cdev->dev));
292                 dasd_put_device(base);
293                 return -EINVAL;
294         }
295         rc = dasd_format(base->block, &fdata);
296         dasd_put_device(base);
297         return rc;
298 }
299
300 #ifdef CONFIG_DASD_PROFILE
301 /*
302  * Reset device profile information
303  */
304 static int dasd_ioctl_reset_profile(struct dasd_block *block)
305 {
306         dasd_profile_reset(&block->profile);
307         return 0;
308 }
309
310 /*
311  * Return device profile information
312  */
313 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
314 {
315         struct dasd_profile_info_t *data;
316         int rc = 0;
317
318         data = kmalloc(sizeof(*data), GFP_KERNEL);
319         if (!data)
320                 return -ENOMEM;
321
322         spin_lock_bh(&block->profile.lock);
323         if (block->profile.data) {
324                 data->dasd_io_reqs = block->profile.data->dasd_io_reqs;
325                 data->dasd_io_sects = block->profile.data->dasd_io_sects;
326                 memcpy(data->dasd_io_secs, block->profile.data->dasd_io_secs,
327                        sizeof(data->dasd_io_secs));
328                 memcpy(data->dasd_io_times, block->profile.data->dasd_io_times,
329                        sizeof(data->dasd_io_times));
330                 memcpy(data->dasd_io_timps, block->profile.data->dasd_io_timps,
331                        sizeof(data->dasd_io_timps));
332                 memcpy(data->dasd_io_time1, block->profile.data->dasd_io_time1,
333                        sizeof(data->dasd_io_time1));
334                 memcpy(data->dasd_io_time2, block->profile.data->dasd_io_time2,
335                        sizeof(data->dasd_io_time2));
336                 memcpy(data->dasd_io_time2ps,
337                        block->profile.data->dasd_io_time2ps,
338                        sizeof(data->dasd_io_time2ps));
339                 memcpy(data->dasd_io_time3, block->profile.data->dasd_io_time3,
340                        sizeof(data->dasd_io_time3));
341                 memcpy(data->dasd_io_nr_req,
342                        block->profile.data->dasd_io_nr_req,
343                        sizeof(data->dasd_io_nr_req));
344                 spin_unlock_bh(&block->profile.lock);
345         } else {
346                 spin_unlock_bh(&block->profile.lock);
347                 rc = -EIO;
348                 goto out;
349         }
350         if (copy_to_user(argp, data, sizeof(*data)))
351                 rc = -EFAULT;
352 out:
353         kfree(data);
354         return rc;
355 }
356 #else
357 static int dasd_ioctl_reset_profile(struct dasd_block *block)
358 {
359         return -ENOTTY;
360 }
361
362 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
363 {
364         return -ENOTTY;
365 }
366 #endif
367
368 /*
369  * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
370  */
371 static int dasd_ioctl_information(struct dasd_block *block,
372                                   unsigned int cmd, void __user *argp)
373 {
374         struct dasd_information2_t *dasd_info;
375         struct subchannel_id sch_id;
376         struct ccw_dev_id dev_id;
377         struct dasd_device *base;
378         struct ccw_device *cdev;
379         unsigned long flags;
380         int rc;
381
382         base = block->base;
383         if (!base->discipline || !base->discipline->fill_info)
384                 return -EINVAL;
385
386         dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
387         if (dasd_info == NULL)
388                 return -ENOMEM;
389
390         rc = base->discipline->fill_info(base, dasd_info);
391         if (rc) {
392                 kfree(dasd_info);
393                 return rc;
394         }
395
396         cdev = base->cdev;
397         ccw_device_get_id(cdev, &dev_id);
398         ccw_device_get_schid(cdev, &sch_id);
399
400         dasd_info->devno = dev_id.devno;
401         dasd_info->schid = sch_id.sch_no;
402         dasd_info->cu_type = cdev->id.cu_type;
403         dasd_info->cu_model = cdev->id.cu_model;
404         dasd_info->dev_type = cdev->id.dev_type;
405         dasd_info->dev_model = cdev->id.dev_model;
406         dasd_info->status = base->state;
407         /*
408          * The open_count is increased for every opener, that includes
409          * the blkdev_get in dasd_scan_partitions.
410          * This must be hidden from user-space.
411          */
412         dasd_info->open_count = atomic_read(&block->open_count);
413         if (!block->bdev)
414                 dasd_info->open_count++;
415
416         /*
417          * check if device is really formatted
418          * LDL / CDL was returned by 'fill_info'
419          */
420         if ((base->state < DASD_STATE_READY) ||
421             (dasd_check_blocksize(block->bp_block)))
422                 dasd_info->format = DASD_FORMAT_NONE;
423
424         dasd_info->features |=
425                 ((base->features & DASD_FEATURE_READONLY) != 0);
426
427         memcpy(dasd_info->type, base->discipline->name, 4);
428
429         if (block->request_queue->request_fn) {
430                 struct list_head *l;
431 #ifdef DASD_EXTENDED_PROFILING
432                 {
433                         struct list_head *l;
434                         spin_lock_irqsave(&block->lock, flags);
435                         list_for_each(l, &block->request_queue->queue_head)
436                                 dasd_info->req_queue_len++;
437                         spin_unlock_irqrestore(&block->lock, flags);
438                 }
439 #endif                          /* DASD_EXTENDED_PROFILING */
440                 spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
441                 list_for_each(l, &base->ccw_queue)
442                         dasd_info->chanq_len++;
443                 spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
444                                        flags);
445         }
446
447         rc = 0;
448         if (copy_to_user(argp, dasd_info,
449                          ((cmd == (unsigned int) BIODASDINFO2) ?
450                           sizeof(struct dasd_information2_t) :
451                           sizeof(struct dasd_information_t))))
452                 rc = -EFAULT;
453         kfree(dasd_info);
454         return rc;
455 }
456
457 /*
458  * Set read only
459  */
460 static int
461 dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
462 {
463         struct dasd_device *base;
464         int intval, rc;
465
466         if (!capable(CAP_SYS_ADMIN))
467                 return -EACCES;
468         if (bdev != bdev->bd_contains)
469                 // ro setting is not allowed for partitions
470                 return -EINVAL;
471         if (get_user(intval, (int __user *)argp))
472                 return -EFAULT;
473         base = dasd_device_from_gendisk(bdev->bd_disk);
474         if (!base)
475                 return -ENODEV;
476         if (!intval && test_bit(DASD_FLAG_DEVICE_RO, &base->flags)) {
477                 dasd_put_device(base);
478                 return -EROFS;
479         }
480         set_disk_ro(bdev->bd_disk, intval);
481         rc = dasd_set_feature(base->cdev, DASD_FEATURE_READONLY, intval);
482         dasd_put_device(base);
483         return rc;
484 }
485
486 static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
487                                   struct cmbdata __user *argp)
488 {
489         size_t size = _IOC_SIZE(cmd);
490         struct cmbdata data;
491         int ret;
492
493         ret = cmf_readall(block->base->cdev, &data);
494         if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
495                 return -EFAULT;
496         return ret;
497 }
498
499 int dasd_ioctl(struct block_device *bdev, fmode_t mode,
500                unsigned int cmd, unsigned long arg)
501 {
502         struct dasd_block *block;
503         struct dasd_device *base;
504         void __user *argp;
505         int rc;
506
507         if (is_compat_task())
508                 argp = compat_ptr(arg);
509         else
510                 argp = (void __user *)arg;
511
512         if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
513                 PRINT_DEBUG("empty data ptr");
514                 return -EINVAL;
515         }
516
517         base = dasd_device_from_gendisk(bdev->bd_disk);
518         if (!base)
519                 return -ENODEV;
520         block = base->block;
521         rc = 0;
522         switch (cmd) {
523         case BIODASDDISABLE:
524                 rc = dasd_ioctl_disable(bdev);
525                 break;
526         case BIODASDENABLE:
527                 rc = dasd_ioctl_enable(bdev);
528                 break;
529         case BIODASDQUIESCE:
530                 rc = dasd_ioctl_quiesce(block);
531                 break;
532         case BIODASDRESUME:
533                 rc = dasd_ioctl_resume(block);
534                 break;
535         case BIODASDABORTIO:
536                 rc = dasd_ioctl_abortio(block);
537                 break;
538         case BIODASDALLOWIO:
539                 rc = dasd_ioctl_allowio(block);
540                 break;
541         case BIODASDFMT:
542                 rc = dasd_ioctl_format(bdev, argp);
543                 break;
544         case BIODASDINFO:
545                 rc = dasd_ioctl_information(block, cmd, argp);
546                 break;
547         case BIODASDINFO2:
548                 rc = dasd_ioctl_information(block, cmd, argp);
549                 break;
550         case BIODASDPRRD:
551                 rc = dasd_ioctl_read_profile(block, argp);
552                 break;
553         case BIODASDPRRST:
554                 rc = dasd_ioctl_reset_profile(block);
555                 break;
556         case BLKROSET:
557                 rc = dasd_ioctl_set_ro(bdev, argp);
558                 break;
559         case DASDAPIVER:
560                 rc = dasd_ioctl_api_version(argp);
561                 break;
562         case BIODASDCMFENABLE:
563                 rc = enable_cmf(base->cdev);
564                 break;
565         case BIODASDCMFDISABLE:
566                 rc = disable_cmf(base->cdev);
567                 break;
568         case BIODASDREADALLCMB:
569                 rc = dasd_ioctl_readall_cmb(block, cmd, argp);
570                 break;
571         default:
572                 /* if the discipline has an ioctl method try it. */
573                 rc = -ENOTTY;
574                 if (base->discipline->ioctl)
575                         rc = base->discipline->ioctl(block, cmd, argp);
576         }
577         dasd_put_device(base);
578         return rc;
579 }