xref: /openbmc/linux/drivers/s390/block/dasd_ioctl.c (revision 4dc7ccf7)
1 /*
2  * File...........: linux/drivers/s390/block/dasd_ioctl.c
3  * Author(s)......: Holger Smolinski <Holger.Smolinski@de.ibm.com>
4  *		    Horst Hummel <Horst.Hummel@de.ibm.com>
5  *		    Carsten Otte <Cotte@de.ibm.com>
6  *		    Martin Schwidefsky <schwidefsky@de.ibm.com>
7  * Bugreports.to..: <Linux390@de.ibm.com>
8  * (C) IBM Corporation, IBM Deutschland Entwicklung GmbH, 1999-2001
9  *
10  * i/o controls for the dasd driver.
11  */
12 
13 #define KMSG_COMPONENT "dasd"
14 
15 #include <linux/interrupt.h>
16 #include <linux/major.h>
17 #include <linux/fs.h>
18 #include <linux/blkpg.h>
19 #include <linux/smp_lock.h>
20 #include <linux/slab.h>
21 #include <asm/compat.h>
22 #include <asm/ccwdev.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_block *block = bdev->bd_disk->private_data;
47 
48 	if (!capable(CAP_SYS_ADMIN))
49 		return -EACCES;
50 
51 	dasd_enable_device(block->base);
52 	/* Formatting the dasd device can change the capacity. */
53 	mutex_lock(&bdev->bd_mutex);
54 	i_size_write(bdev->bd_inode, (loff_t)get_capacity(block->gdp) << 9);
55 	mutex_unlock(&bdev->bd_mutex);
56 	return 0;
57 }
58 
59 /*
60  * Disable device.
61  * Used by dasdfmt. Disable I/O operations but allow ioctls.
62  */
63 static int
64 dasd_ioctl_disable(struct block_device *bdev)
65 {
66 	struct dasd_block *block = bdev->bd_disk->private_data;
67 
68 	if (!capable(CAP_SYS_ADMIN))
69 		return -EACCES;
70 
71 	/*
72 	 * Man this is sick. We don't do a real disable but only downgrade
73 	 * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
74 	 * BIODASDDISABLE to disable accesses to the device via the block
75 	 * device layer but it still wants to do i/o on the device by
76 	 * using the BIODASDFMT ioctl. Therefore the correct state for the
77 	 * device is DASD_STATE_BASIC that allows to do basic i/o.
78 	 */
79 	dasd_set_target_state(block->base, DASD_STATE_BASIC);
80 	/*
81 	 * Set i_size to zero, since read, write, etc. check against this
82 	 * value.
83 	 */
84 	mutex_lock(&bdev->bd_mutex);
85 	i_size_write(bdev->bd_inode, 0);
86 	mutex_unlock(&bdev->bd_mutex);
87 	return 0;
88 }
89 
90 /*
91  * Quiesce device.
92  */
93 static int dasd_ioctl_quiesce(struct dasd_block *block)
94 {
95 	unsigned long flags;
96 	struct dasd_device *base;
97 
98 	base = block->base;
99 	if (!capable (CAP_SYS_ADMIN))
100 		return -EACCES;
101 
102 	pr_info("%s: The DASD has been put in the quiesce "
103 		"state\n", dev_name(&base->cdev->dev));
104 	spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
105 	dasd_device_set_stop_bits(base, DASD_STOPPED_QUIESCE);
106 	spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
107 	return 0;
108 }
109 
110 
111 /*
112  * Resume device.
113  */
114 static int dasd_ioctl_resume(struct dasd_block *block)
115 {
116 	unsigned long flags;
117 	struct dasd_device *base;
118 
119 	base = block->base;
120 	if (!capable (CAP_SYS_ADMIN))
121 		return -EACCES;
122 
123 	pr_info("%s: I/O operations have been resumed "
124 		"on the DASD\n", dev_name(&base->cdev->dev));
125 	spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
126 	dasd_device_remove_stop_bits(base, DASD_STOPPED_QUIESCE);
127 	spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
128 
129 	dasd_schedule_block_bh(block);
130 	return 0;
131 }
132 
133 /*
134  * performs formatting of _device_ according to _fdata_
135  * Note: The discipline's format_function is assumed to deliver formatting
136  * commands to format a single unit of the device. In terms of the ECKD
137  * devices this means CCWs are generated to format a single track.
138  */
139 static int dasd_format(struct dasd_block *block, struct format_data_t *fdata)
140 {
141 	struct dasd_ccw_req *cqr;
142 	struct dasd_device *base;
143 	int rc;
144 
145 	base = block->base;
146 	if (base->discipline->format_device == NULL)
147 		return -EPERM;
148 
149 	if (base->state != DASD_STATE_BASIC) {
150 		pr_warning("%s: The DASD cannot be formatted while it is "
151 			   "enabled\n",  dev_name(&base->cdev->dev));
152 		return -EBUSY;
153 	}
154 
155 	DBF_DEV_EVENT(DBF_NOTICE, base,
156 		      "formatting units %u to %u (%u B blocks) flags %u",
157 		      fdata->start_unit,
158 		      fdata->stop_unit, fdata->blksize, fdata->intensity);
159 
160 	/* Since dasdfmt keeps the device open after it was disabled,
161 	 * there still exists an inode for this device.
162 	 * We must update i_blkbits, otherwise we might get errors when
163 	 * enabling the device later.
164 	 */
165 	if (fdata->start_unit == 0) {
166 		struct block_device *bdev = bdget_disk(block->gdp, 0);
167 		bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
168 		bdput(bdev);
169 	}
170 
171 	while (fdata->start_unit <= fdata->stop_unit) {
172 		cqr = base->discipline->format_device(base, fdata);
173 		if (IS_ERR(cqr))
174 			return PTR_ERR(cqr);
175 		rc = dasd_sleep_on_interruptible(cqr);
176 		dasd_sfree_request(cqr, cqr->memdev);
177 		if (rc) {
178 			if (rc != -ERESTARTSYS)
179 				pr_err("%s: Formatting unit %d failed with "
180 				       "rc=%d\n", dev_name(&base->cdev->dev),
181 				       fdata->start_unit, rc);
182 			return rc;
183 		}
184 		fdata->start_unit++;
185 	}
186 	return 0;
187 }
188 
189 /*
190  * Format device.
191  */
192 static int
193 dasd_ioctl_format(struct block_device *bdev, void __user *argp)
194 {
195 	struct dasd_block *block = bdev->bd_disk->private_data;
196 	struct format_data_t fdata;
197 
198 	if (!capable(CAP_SYS_ADMIN))
199 		return -EACCES;
200 	if (!argp)
201 		return -EINVAL;
202 
203 	if (block->base->features & DASD_FEATURE_READONLY ||
204 	    test_bit(DASD_FLAG_DEVICE_RO, &block->base->flags))
205 		return -EROFS;
206 	if (copy_from_user(&fdata, argp, sizeof(struct format_data_t)))
207 		return -EFAULT;
208 	if (bdev != bdev->bd_contains) {
209 		pr_warning("%s: The specified DASD is a partition and cannot "
210 			   "be formatted\n",
211 			   dev_name(&block->base->cdev->dev));
212 		return -EINVAL;
213 	}
214 	return dasd_format(block, &fdata);
215 }
216 
217 #ifdef CONFIG_DASD_PROFILE
218 /*
219  * Reset device profile information
220  */
221 static int dasd_ioctl_reset_profile(struct dasd_block *block)
222 {
223 	memset(&block->profile, 0, sizeof(struct dasd_profile_info_t));
224 	return 0;
225 }
226 
227 /*
228  * Return device profile information
229  */
230 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
231 {
232 	if (dasd_profile_level == DASD_PROFILE_OFF)
233 		return -EIO;
234 	if (copy_to_user(argp, &block->profile,
235 			 sizeof(struct dasd_profile_info_t)))
236 		return -EFAULT;
237 	return 0;
238 }
239 #else
240 static int dasd_ioctl_reset_profile(struct dasd_block *block)
241 {
242 	return -ENOSYS;
243 }
244 
245 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
246 {
247 	return -ENOSYS;
248 }
249 #endif
250 
251 /*
252  * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
253  */
254 static int dasd_ioctl_information(struct dasd_block *block,
255 				  unsigned int cmd, void __user *argp)
256 {
257 	struct dasd_information2_t *dasd_info;
258 	unsigned long flags;
259 	int rc;
260 	struct dasd_device *base;
261 	struct ccw_device *cdev;
262 	struct ccw_dev_id dev_id;
263 
264 	base = block->base;
265 	if (!base->discipline || !base->discipline->fill_info)
266 		return -EINVAL;
267 
268 	dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
269 	if (dasd_info == NULL)
270 		return -ENOMEM;
271 
272 	rc = base->discipline->fill_info(base, dasd_info);
273 	if (rc) {
274 		kfree(dasd_info);
275 		return rc;
276 	}
277 
278 	cdev = base->cdev;
279 	ccw_device_get_id(cdev, &dev_id);
280 
281 	dasd_info->devno = dev_id.devno;
282 	dasd_info->schid = _ccw_device_get_subchannel_number(base->cdev);
283 	dasd_info->cu_type = cdev->id.cu_type;
284 	dasd_info->cu_model = cdev->id.cu_model;
285 	dasd_info->dev_type = cdev->id.dev_type;
286 	dasd_info->dev_model = cdev->id.dev_model;
287 	dasd_info->status = base->state;
288 	/*
289 	 * The open_count is increased for every opener, that includes
290 	 * the blkdev_get in dasd_scan_partitions.
291 	 * This must be hidden from user-space.
292 	 */
293 	dasd_info->open_count = atomic_read(&block->open_count);
294 	if (!block->bdev)
295 		dasd_info->open_count++;
296 
297 	/*
298 	 * check if device is really formatted
299 	 * LDL / CDL was returned by 'fill_info'
300 	 */
301 	if ((base->state < DASD_STATE_READY) ||
302 	    (dasd_check_blocksize(block->bp_block)))
303 		dasd_info->format = DASD_FORMAT_NONE;
304 
305 	dasd_info->features |=
306 		((base->features & DASD_FEATURE_READONLY) != 0);
307 
308 	memcpy(dasd_info->type, base->discipline->name, 4);
309 
310 	if (block->request_queue->request_fn) {
311 		struct list_head *l;
312 #ifdef DASD_EXTENDED_PROFILING
313 		{
314 			struct list_head *l;
315 			spin_lock_irqsave(&block->lock, flags);
316 			list_for_each(l, &block->request_queue->queue_head)
317 				dasd_info->req_queue_len++;
318 			spin_unlock_irqrestore(&block->lock, flags);
319 		}
320 #endif				/* DASD_EXTENDED_PROFILING */
321 		spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
322 		list_for_each(l, &base->ccw_queue)
323 			dasd_info->chanq_len++;
324 		spin_unlock_irqrestore(get_ccwdev_lock(base->cdev),
325 				       flags);
326 	}
327 
328 	rc = 0;
329 	if (copy_to_user(argp, dasd_info,
330 			 ((cmd == (unsigned int) BIODASDINFO2) ?
331 			  sizeof(struct dasd_information2_t) :
332 			  sizeof(struct dasd_information_t))))
333 		rc = -EFAULT;
334 	kfree(dasd_info);
335 	return rc;
336 }
337 
338 /*
339  * Set read only
340  */
341 static int
342 dasd_ioctl_set_ro(struct block_device *bdev, void __user *argp)
343 {
344 	struct dasd_block *block =  bdev->bd_disk->private_data;
345 	int intval;
346 
347 	if (!capable(CAP_SYS_ADMIN))
348 		return -EACCES;
349 	if (bdev != bdev->bd_contains)
350 		// ro setting is not allowed for partitions
351 		return -EINVAL;
352 	if (get_user(intval, (int __user *)argp))
353 		return -EFAULT;
354 	if (!intval && test_bit(DASD_FLAG_DEVICE_RO, &block->base->flags))
355 		return -EROFS;
356 	set_disk_ro(bdev->bd_disk, intval);
357 	return dasd_set_feature(block->base->cdev, DASD_FEATURE_READONLY, intval);
358 }
359 
360 static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
361 				  struct cmbdata __user *argp)
362 {
363 	size_t size = _IOC_SIZE(cmd);
364 	struct cmbdata data;
365 	int ret;
366 
367 	ret = cmf_readall(block->base->cdev, &data);
368 	if (!ret && copy_to_user(argp, &data, min(size, sizeof(*argp))))
369 		return -EFAULT;
370 	return ret;
371 }
372 
373 static int
374 dasd_do_ioctl(struct block_device *bdev, fmode_t mode,
375 	      unsigned int cmd, unsigned long arg)
376 {
377 	struct dasd_block *block = bdev->bd_disk->private_data;
378 	void __user *argp;
379 
380 	if (is_compat_task())
381 		argp = compat_ptr(arg);
382 	else
383 		argp = (void __user *)arg;
384 
385 	if (!block)
386                 return -ENODEV;
387 
388 	if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
389 		PRINT_DEBUG("empty data ptr");
390 		return -EINVAL;
391 	}
392 
393 	switch (cmd) {
394 	case BIODASDDISABLE:
395 		return dasd_ioctl_disable(bdev);
396 	case BIODASDENABLE:
397 		return dasd_ioctl_enable(bdev);
398 	case BIODASDQUIESCE:
399 		return dasd_ioctl_quiesce(block);
400 	case BIODASDRESUME:
401 		return dasd_ioctl_resume(block);
402 	case BIODASDFMT:
403 		return dasd_ioctl_format(bdev, argp);
404 	case BIODASDINFO:
405 		return dasd_ioctl_information(block, cmd, argp);
406 	case BIODASDINFO2:
407 		return dasd_ioctl_information(block, cmd, argp);
408 	case BIODASDPRRD:
409 		return dasd_ioctl_read_profile(block, argp);
410 	case BIODASDPRRST:
411 		return dasd_ioctl_reset_profile(block);
412 	case BLKROSET:
413 		return dasd_ioctl_set_ro(bdev, argp);
414 	case DASDAPIVER:
415 		return dasd_ioctl_api_version(argp);
416 	case BIODASDCMFENABLE:
417 		return enable_cmf(block->base->cdev);
418 	case BIODASDCMFDISABLE:
419 		return disable_cmf(block->base->cdev);
420 	case BIODASDREADALLCMB:
421 		return dasd_ioctl_readall_cmb(block, cmd, argp);
422 	default:
423 		/* if the discipline has an ioctl method try it. */
424 		if (block->base->discipline->ioctl) {
425 			int rval = block->base->discipline->ioctl(block, cmd, argp);
426 			if (rval != -ENOIOCTLCMD)
427 				return rval;
428 		}
429 
430 		return -EINVAL;
431 	}
432 }
433 
434 int dasd_ioctl(struct block_device *bdev, fmode_t mode,
435 	       unsigned int cmd, unsigned long arg)
436 {
437 	int rc;
438 
439 	lock_kernel();
440 	rc = dasd_do_ioctl(bdev, mode, cmd, arg);
441 	unlock_kernel();
442 	return rc;
443 }
444