aboutsummaryrefslogtreecommitdiffstats
path: root/xen-2.4.16/drivers/block/blkpg.c
blob: 2e27a1aa2b739ccfc679cd06b8b3db477e04f4af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Partition table and disk geometry handling
 *
 * This obsoletes the partition-handling code in genhd.c:
 * Userspace can look at a disk in arbitrary format and tell
 * the kernel what partitions there are on the disk, and how
 * these should be numbered.
 * It also allows one to repartition a disk that is being used.
 *
 * A single ioctl with lots of subfunctions:
 *
 * Device number stuff:
 *    get_whole_disk()          (given the device number of a partition, find
 *                               the device number of the encompassing disk)
 *    get_all_partitions()      (given the device number of a disk, return the
 *                               device numbers of all its known partitions)
 *
 * Partition stuff:
 *    add_partition()
 *    delete_partition()
 *    test_partition_in_use()   (also for test_disk_in_use)
 *
 * Geometry stuff:
 *    get_geometry()
 *    set_geometry()
 *    get_bios_drivedata()
 *
 * For today, only the partition stuff - aeb, 990515
 */

#include <xeno/config.h>
#include <xeno/types.h>
#include <xeno/errno.h>
/*#include <xeno/fs.h>	*/		/* for BLKRASET, ... */
#include <xeno/sched.h>		/* for capable() */
#include <xeno/blk.h>			/* for set_device_ro() */
#include <xeno/blkpg.h>
#include <xeno/genhd.h>
/*#include <xeno/swap.h>*/			/* for is_swap_partition() */
#include <xeno/module.h>               /* for EXPORT_SYMBOL */

#include <asm/uaccess.h>

#define is_mounted(_dev)         (0)
#define is_swap_partition(_dev)  (0)

#define fsync_dev(_dev) (panic("fsync_dev???"))
#define invalidate_buffers(_dev) (panic("invalidate_buffers???"))

/*
 * What is the data describing a partition?
 *
 * 1. a device number (kdev_t)
 * 2. a starting sector and number of sectors (hd_struct)
 *    given in the part[] array of the gendisk structure for the drive.
 *
 * The number of sectors is replicated in the sizes[] array of
 * the gendisk structure for the major, which again is copied to
 * the blk_size[][] array.
 * (However, hd_struct has the number of 512-byte sectors,
 *  g->sizes[] and blk_size[][] have the number of 1024-byte blocks.)
 * Note that several drives may have the same major.
 */

/*
 * Add a partition.
 *
 * returns: EINVAL: bad parameters
 *          ENXIO: cannot find drive
 *          EBUSY: proposed partition overlaps an existing one
 *                 or has the same number as an existing one
 *          0: all OK.
 */
int add_partition(kdev_t dev, struct blkpg_partition *p) {
	struct gendisk *g;
	long long ppstart, pplength;
	long pstart, plength;
	int i, drive, first_minor, end_minor, minor;

	/* convert bytes to sectors, check for fit in a hd_struct */
	ppstart = (p->start >> 9);
	pplength = (p->length >> 9);
	pstart = ppstart;
	plength = pplength;
	if (pstart != ppstart || plength != pplength
	    || pstart < 0 || plength < 0)
		return -EINVAL;

	/* find the drive major */
	g = get_gendisk(dev);
	if (!g)
		return -ENXIO;

	/* existing drive? */
	drive = (MINOR(dev) >> g->minor_shift);
	first_minor = (drive << g->minor_shift);
	end_minor   = first_minor + g->max_p;
	if (drive >= g->nr_real)
		return -ENXIO;

	/* drive and partition number OK? */
	if (first_minor != MINOR(dev) || p->pno <= 0 || p->pno >= g->max_p)
		return -EINVAL;

	/* partition number in use? */
	minor = first_minor + p->pno;
	if (g->part[minor].nr_sects != 0)
		return -EBUSY;

	/* overlap? */
	for (i=first_minor+1; i<end_minor; i++)
		if (!(pstart+plength <= g->part[i].start_sect ||
		      pstart >= g->part[i].start_sect + g->part[i].nr_sects))
			return -EBUSY;

	/* all seems OK */
	g->part[minor].start_sect = pstart;
	g->part[minor].nr_sects = plength;
	if (g->sizes)
		g->sizes[minor] = (plength >> (BLOCK_SIZE_BITS - 9));
#ifdef DEVFS_MUST_DIE
	devfs_register_partitions (g, first_minor, 0);
#endif
	return 0;
}

/*
 * Delete a partition given by partition number
 *
 * returns: EINVAL: bad parameters
 *          ENXIO: cannot find partition
 *          EBUSY: partition is busy
 *          0: all OK.
 *
 * Note that the dev argument refers to the entire disk, not the partition.
 */
int del_partition(kdev_t dev, struct blkpg_partition *p) {
	struct gendisk *g;
	kdev_t devp;
	int drive, first_minor, minor;

	/* find the drive major */
	g = get_gendisk(dev);
	if (!g)
		return -ENXIO;

	/* drive and partition number OK? */
	drive = (MINOR(dev) >> g->minor_shift);
	first_minor = (drive << g->minor_shift);
	if (first_minor != MINOR(dev) || p->pno <= 0 || p->pno >= g->max_p)
		return -EINVAL;

	/* existing drive and partition? */
	minor = first_minor + p->pno;
	if (drive >= g->nr_real || g->part[minor].nr_sects == 0)
		return -ENXIO;

	/* partition in use? Incomplete check for now. */
	devp = MKDEV(MAJOR(dev), minor);
	if (is_mounted(devp) || is_swap_partition(devp))
		return -EBUSY;

	/* all seems OK */
	fsync_dev(devp);
	invalidate_buffers(devp);

	g->part[minor].start_sect = 0;
	g->part[minor].nr_sects = 0;
	if (g->sizes)
		g->sizes[minor] = 0;
#ifdef DEVFS_MUST_DIE
	devfs_register_partitions (g, first_minor, 0);
#endif

	return 0;
}

int blkpg_ioctl(kdev_t dev, struct blkpg_ioctl_arg *arg)
{
	struct blkpg_ioctl_arg a;
	struct blkpg_partition p;
	int len;

	if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
		return -EFAULT;

	switch (a.op) {
		case BLKPG_ADD_PARTITION:
		case BLKPG_DEL_PARTITION:
			len = a.datalen;
			if (len < sizeof(struct blkpg_partition))
				return -EINVAL;
			if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
				return -EFAULT;
			if (!capable(CAP_SYS_ADMIN))
				return -EACCES;
			if (a.op == BLKPG_ADD_PARTITION)
				return add_partition(dev, &p);
			else
				return del_partition(dev, &p);
		default:
			return -EINVAL;
	}
}

/*
 * Common ioctl's for block devices
 */

int blk_ioctl(kdev_t dev, unsigned int cmd, unsigned long arg)
{
#if 1
    printk("May want to check out blk_ioctl...\n");
    return -EINVAL;
#else
	struct gendisk *g;
	u64 ullval = 0;
	int intval;

	if (!dev)
		return -EINVAL;

	switch (cmd) {
		case BLKROSET:
			if (!capable(CAP_SYS_ADMIN))
				return -EACCES;
			if (get_user(intval, (int *)(arg)))
				return -EFAULT;
			set_device_ro(dev, intval);
			return 0;
		case BLKROGET:
			intval = (is_read_only(dev) != 0);
			return put_user(intval, (int *)(arg));

		case BLKRASET:
			if(!capable(CAP_SYS_ADMIN))
				return -EACCES;
			if(arg > 0xff)
				return -EINVAL;
			read_ahead[MAJOR(dev)] = arg;
			return 0;
		case BLKRAGET:
			if (!arg)
				return -EINVAL;
			return put_user(read_ahead[MAJOR(dev)], (long *) arg);

		case BLKFLSBUF:
			if(!capable(CAP_SYS_ADMIN))
				return -EACCES;
			fsync_dev(dev);
			invalidate_buffers(dev);
			return 0;

		case BLKSSZGET:
			/* get block device sector size as needed e.g. by fdisk */
			intval = get_hardsect_size(dev);
			return put_user(intval, (int *) arg);

		case BLKGETSIZE:
		case BLKGETSIZE64:
			g = get_gendisk(dev);
			if (g)
				ullval = g->part[MINOR(dev)].nr_sects;

			if (cmd == BLKGETSIZE)
				return put_user((unsigned long)ullval, (unsigned long *)arg);
			else
				return put_user(ullval << 9, (u64 *)arg);
#if 0
		case BLKRRPART: /* Re-read partition tables */
			if (!capable(CAP_SYS_ADMIN)) 
				return -EACCES;
			return reread_partitions(dev, 1);
#endif

		case BLKPG:
			return blkpg_ioctl(dev, (struct blkpg_ioctl_arg *) arg);
			
		case BLKELVGET:
			return blkelvget_ioctl(&blk_get_queue(dev)->elevator,
					       (blkelv_ioctl_arg_t *) arg);
		case BLKELVSET:
			return blkelvset_ioctl(&blk_get_queue(dev)->elevator,
					       (blkelv_ioctl_arg_t *) arg);

		case BLKBSZGET:
			/* get the logical block size (cf. BLKSSZGET) */
			intval = BLOCK_SIZE;
			if (blksize_size[MAJOR(dev)])
				intval = blksize_size[MAJOR(dev)][MINOR(dev)];
			return put_user (intval, (int *) arg);

		case BLKBSZSET:
			/* set the logical block size */
			if (!capable (CAP_SYS_ADMIN))
				return -EACCES;
			if (!dev || !arg)
				return -EINVAL;
			if (get_user (intval, (int *) arg))
				return -EFAULT;
			if (intval > PAGE_SIZE || intval < 512 ||
			    (intval & (intval - 1)))
				return -EINVAL;
			if (is_mounted (dev) || is_swap_partition (dev))
				return -EBUSY;
			set_blocksize (dev, intval);
			return 0;

		default:
			return -EINVAL;
	}
#endif
}

EXPORT_SYMBOL(blk_ioctl);