xref: /openbmc/linux/drivers/s390/block/dasd_ioctl.c (revision b04b4f78)
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 
20 #include <asm/ccwdev.h>
21 #include <asm/cmb.h>
22 #include <asm/uaccess.h>
23 
24 /* This is ugly... */
25 #define PRINTK_HEADER "dasd_ioctl:"
26 
27 #include "dasd_int.h"
28 
29 
30 static int
31 dasd_ioctl_api_version(void __user *argp)
32 {
33 	int ver = DASD_API_VERSION;
34 	return put_user(ver, (int __user *)argp);
35 }
36 
37 /*
38  * Enable device.
39  * used by dasdfmt after BIODASDDISABLE to retrigger blocksize detection
40  */
41 static int
42 dasd_ioctl_enable(struct block_device *bdev)
43 {
44 	struct dasd_block *block = bdev->bd_disk->private_data;
45 
46 	if (!capable(CAP_SYS_ADMIN))
47 		return -EACCES;
48 
49 	dasd_enable_device(block->base);
50 	/* Formatting the dasd device can change the capacity. */
51 	mutex_lock(&bdev->bd_mutex);
52 	i_size_write(bdev->bd_inode, (loff_t)get_capacity(block->gdp) << 9);
53 	mutex_unlock(&bdev->bd_mutex);
54 	return 0;
55 }
56 
57 /*
58  * Disable device.
59  * Used by dasdfmt. Disable I/O operations but allow ioctls.
60  */
61 static int
62 dasd_ioctl_disable(struct block_device *bdev)
63 {
64 	struct dasd_block *block = bdev->bd_disk->private_data;
65 
66 	if (!capable(CAP_SYS_ADMIN))
67 		return -EACCES;
68 
69 	/*
70 	 * Man this is sick. We don't do a real disable but only downgrade
71 	 * the device to DASD_STATE_BASIC. The reason is that dasdfmt uses
72 	 * BIODASDDISABLE to disable accesses to the device via the block
73 	 * device layer but it still wants to do i/o on the device by
74 	 * using the BIODASDFMT ioctl. Therefore the correct state for the
75 	 * device is DASD_STATE_BASIC that allows to do basic i/o.
76 	 */
77 	dasd_set_target_state(block->base, DASD_STATE_BASIC);
78 	/*
79 	 * Set i_size to zero, since read, write, etc. check against this
80 	 * value.
81 	 */
82 	mutex_lock(&bdev->bd_mutex);
83 	i_size_write(bdev->bd_inode, 0);
84 	mutex_unlock(&bdev->bd_mutex);
85 	return 0;
86 }
87 
88 /*
89  * Quiesce device.
90  */
91 static int dasd_ioctl_quiesce(struct dasd_block *block)
92 {
93 	unsigned long flags;
94 	struct dasd_device *base;
95 
96 	base = block->base;
97 	if (!capable (CAP_SYS_ADMIN))
98 		return -EACCES;
99 
100 	dev_info(&base->cdev->dev, "The DASD has been put in the quiesce "
101 		 "state\n");
102 	spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
103 	base->stopped |= DASD_STOPPED_QUIESCE;
104 	spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
105 	return 0;
106 }
107 
108 
109 /*
110  * Resume device.
111  */
112 static int dasd_ioctl_resume(struct dasd_block *block)
113 {
114 	unsigned long flags;
115 	struct dasd_device *base;
116 
117 	base = block->base;
118 	if (!capable (CAP_SYS_ADMIN))
119 		return -EACCES;
120 
121 	dev_info(&base->cdev->dev, "I/O operations have been resumed "
122 		 "on the DASD\n");
123 	spin_lock_irqsave(get_ccwdev_lock(base->cdev), flags);
124 	base->stopped &= ~DASD_STOPPED_QUIESCE;
125 	spin_unlock_irqrestore(get_ccwdev_lock(base->cdev), flags);
126 
127 	dasd_schedule_block_bh(block);
128 	return 0;
129 }
130 
131 /*
132  * performs formatting of _device_ according to _fdata_
133  * Note: The discipline's format_function is assumed to deliver formatting
134  * commands to format a single unit of the device. In terms of the ECKD
135  * devices this means CCWs are generated to format a single track.
136  */
137 static int dasd_format(struct dasd_block *block, struct format_data_t *fdata)
138 {
139 	struct dasd_ccw_req *cqr;
140 	struct dasd_device *base;
141 	int rc;
142 
143 	base = block->base;
144 	if (base->discipline->format_device == NULL)
145 		return -EPERM;
146 
147 	if (base->state != DASD_STATE_BASIC) {
148 		dev_warn(&base->cdev->dev,
149 			 "The DASD cannot be formatted while it is enabled\n");
150 		return -EBUSY;
151 	}
152 
153 	DBF_DEV_EVENT(DBF_NOTICE, base,
154 		      "formatting units %u to %u (%u B blocks) flags %u",
155 		      fdata->start_unit,
156 		      fdata->stop_unit, fdata->blksize, fdata->intensity);
157 
158 	/* Since dasdfmt keeps the device open after it was disabled,
159 	 * there still exists an inode for this device.
160 	 * We must update i_blkbits, otherwise we might get errors when
161 	 * enabling the device later.
162 	 */
163 	if (fdata->start_unit == 0) {
164 		struct block_device *bdev = bdget_disk(block->gdp, 0);
165 		bdev->bd_inode->i_blkbits = blksize_bits(fdata->blksize);
166 		bdput(bdev);
167 	}
168 
169 	while (fdata->start_unit <= fdata->stop_unit) {
170 		cqr = base->discipline->format_device(base, fdata);
171 		if (IS_ERR(cqr))
172 			return PTR_ERR(cqr);
173 		rc = dasd_sleep_on_interruptible(cqr);
174 		dasd_sfree_request(cqr, cqr->memdev);
175 		if (rc) {
176 			if (rc != -ERESTARTSYS)
177 				dev_err(&base->cdev->dev,
178 					"Formatting unit %d failed with "
179 					"rc=%d\n", fdata->start_unit, rc);
180 			return rc;
181 		}
182 		fdata->start_unit++;
183 	}
184 	return 0;
185 }
186 
187 /*
188  * Format device.
189  */
190 static int
191 dasd_ioctl_format(struct block_device *bdev, void __user *argp)
192 {
193 	struct dasd_block *block = bdev->bd_disk->private_data;
194 	struct format_data_t fdata;
195 
196 	if (!capable(CAP_SYS_ADMIN))
197 		return -EACCES;
198 	if (!argp)
199 		return -EINVAL;
200 
201 	if (block->base->features & DASD_FEATURE_READONLY)
202 		return -EROFS;
203 	if (copy_from_user(&fdata, argp, sizeof(struct format_data_t)))
204 		return -EFAULT;
205 	if (bdev != bdev->bd_contains) {
206 		dev_warn(&block->base->cdev->dev,
207 			 "The specified DASD is a partition and cannot be "
208 			 "formatted\n");
209 		return -EINVAL;
210 	}
211 	return dasd_format(block, &fdata);
212 }
213 
214 #ifdef CONFIG_DASD_PROFILE
215 /*
216  * Reset device profile information
217  */
218 static int dasd_ioctl_reset_profile(struct dasd_block *block)
219 {
220 	memset(&block->profile, 0, sizeof(struct dasd_profile_info_t));
221 	return 0;
222 }
223 
224 /*
225  * Return device profile information
226  */
227 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
228 {
229 	if (dasd_profile_level == DASD_PROFILE_OFF)
230 		return -EIO;
231 	if (copy_to_user(argp, &block->profile,
232 			 sizeof(struct dasd_profile_info_t)))
233 		return -EFAULT;
234 	return 0;
235 }
236 #else
237 static int dasd_ioctl_reset_profile(struct dasd_block *block)
238 {
239 	return -ENOSYS;
240 }
241 
242 static int dasd_ioctl_read_profile(struct dasd_block *block, void __user *argp)
243 {
244 	return -ENOSYS;
245 }
246 #endif
247 
248 /*
249  * Return dasd information. Used for BIODASDINFO and BIODASDINFO2.
250  */
251 static int dasd_ioctl_information(struct dasd_block *block,
252 				  unsigned int cmd, void __user *argp)
253 {
254 	struct dasd_information2_t *dasd_info;
255 	unsigned long flags;
256 	int rc;
257 	struct dasd_device *base;
258 	struct ccw_device *cdev;
259 	struct ccw_dev_id dev_id;
260 
261 	base = block->base;
262 	if (!base->discipline->fill_info)
263 		return -EINVAL;
264 
265 	dasd_info = kzalloc(sizeof(struct dasd_information2_t), GFP_KERNEL);
266 	if (dasd_info == NULL)
267 		return -ENOMEM;
268 
269 	rc = base->discipline->fill_info(base, dasd_info);
270 	if (rc) {
271 		kfree(dasd_info);
272 		return rc;
273 	}
274 
275 	cdev = base->cdev;
276 	ccw_device_get_id(cdev, &dev_id);
277 
278 	dasd_info->devno = dev_id.devno;
279 	dasd_info->schid = _ccw_device_get_subchannel_number(base->cdev);
280 	dasd_info->cu_type = cdev->id.cu_type;
281 	dasd_info->cu_model = cdev->id.cu_model;
282 	dasd_info->dev_type = cdev->id.dev_type;
283 	dasd_info->dev_model = cdev->id.dev_model;
284 	dasd_info->status = base->state;
285 	/*
286 	 * The open_count is increased for every opener, that includes
287 	 * the blkdev_get in dasd_scan_partitions.
288 	 * This must be hidden from user-space.
289 	 */
290 	dasd_info->open_count = atomic_read(&block->open_count);
291 	if (!block->bdev)
292 		dasd_info->open_count++;
293 
294 	/*
295 	 * check if device is really formatted
296 	 * LDL / CDL was returned by 'fill_info'
297 	 */
298 	if ((base->state < DASD_STATE_READY) ||
299 	    (dasd_check_blocksize(block->bp_block)))
300 		dasd_info->format = DASD_FORMAT_NONE;
301 
302 	dasd_info->features |=
303 		((base->features & DASD_FEATURE_READONLY) != 0);
304 
305 	if (base->discipline)
306 		memcpy(dasd_info->type, base->discipline->name, 4);
307 	else
308 		memcpy(dasd_info->type, "none", 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 
355 	set_disk_ro(bdev->bd_disk, intval);
356 	return dasd_set_feature(block->base->cdev, DASD_FEATURE_READONLY, intval);
357 }
358 
359 static int dasd_ioctl_readall_cmb(struct dasd_block *block, unsigned int cmd,
360 		unsigned long arg)
361 {
362 	struct cmbdata __user *argp = (void __user *) arg;
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 = (void __user *)arg;
379 
380 	if (!block)
381                 return -ENODEV;
382 
383 	if ((_IOC_DIR(cmd) != _IOC_NONE) && !arg) {
384 		PRINT_DEBUG("empty data ptr");
385 		return -EINVAL;
386 	}
387 
388 	switch (cmd) {
389 	case BIODASDDISABLE:
390 		return dasd_ioctl_disable(bdev);
391 	case BIODASDENABLE:
392 		return dasd_ioctl_enable(bdev);
393 	case BIODASDQUIESCE:
394 		return dasd_ioctl_quiesce(block);
395 	case BIODASDRESUME:
396 		return dasd_ioctl_resume(block);
397 	case BIODASDFMT:
398 		return dasd_ioctl_format(bdev, argp);
399 	case BIODASDINFO:
400 		return dasd_ioctl_information(block, cmd, argp);
401 	case BIODASDINFO2:
402 		return dasd_ioctl_information(block, cmd, argp);
403 	case BIODASDPRRD:
404 		return dasd_ioctl_read_profile(block, argp);
405 	case BIODASDPRRST:
406 		return dasd_ioctl_reset_profile(block);
407 	case BLKROSET:
408 		return dasd_ioctl_set_ro(bdev, argp);
409 	case DASDAPIVER:
410 		return dasd_ioctl_api_version(argp);
411 	case BIODASDCMFENABLE:
412 		return enable_cmf(block->base->cdev);
413 	case BIODASDCMFDISABLE:
414 		return disable_cmf(block->base->cdev);
415 	case BIODASDREADALLCMB:
416 		return dasd_ioctl_readall_cmb(block, cmd, arg);
417 	default:
418 		/* if the discipline has an ioctl method try it. */
419 		if (block->base->discipline->ioctl) {
420 			int rval = block->base->discipline->ioctl(block, cmd, argp);
421 			if (rval != -ENOIOCTLCMD)
422 				return rval;
423 		}
424 
425 		return -EINVAL;
426 	}
427 }
428 
429 int dasd_ioctl(struct block_device *bdev, fmode_t mode,
430 	       unsigned int cmd, unsigned long arg)
431 {
432 	int rc;
433 
434 	lock_kernel();
435 	rc = dasd_do_ioctl(bdev, mode, cmd, arg);
436 	unlock_kernel();
437 	return rc;
438 }
439