Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / sys / kern / subr_disklabel64.c
1 /*
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/kern/subr_disklabel64.c,v 1.4 2007/06/19 06:39:06 dillon Exp $
35  */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/conf.h>
41 #include <sys/disklabel.h>
42 #include <sys/disklabel64.h>
43 #include <sys/diskslice.h>
44 #include <sys/disk.h>
45 #include <sys/kern_syscall.h>
46 #include <sys/buf2.h>
47
48 /*
49  * Retrieve the partition start and extent, in blocks.  Return 0 on success,
50  * EINVAL on error.
51  */
52 static int
53 l64_getpartbounds(struct diskslices *ssp, disklabel_t lp, u_int32_t part,
54                   u_int64_t *start, u_int64_t *blocks)
55 {
56         struct partition64 *pp;
57
58         if (part >= lp.lab64->d_npartitions)
59                 return (EINVAL);
60
61         pp = &lp.lab64->d_partitions[part];
62
63         if ((pp->p_boffset & (ssp->dss_secsize - 1)) ||
64             (pp->p_bsize & (ssp->dss_secsize - 1))) {
65                 return (EINVAL);
66         }
67         *start = pp->p_boffset / ssp->dss_secsize;
68         *blocks = pp->p_bsize / ssp->dss_secsize;
69         return(0);
70 }
71
72 /*
73  * Get the filesystem type XXX - diskslices code needs to use uuids
74  */
75 static void
76 l64_loadpartinfo(disklabel_t lp, u_int32_t part, struct partinfo *dpart)
77 {
78         struct partition64 *pp;
79         const size_t uuid_size = sizeof(struct uuid);
80
81         if (part < lp.lab64->d_npartitions) {
82                 pp = &lp.lab64->d_partitions[part];
83                 dpart->fstype_uuid = pp->p_type_uuid;
84                 dpart->storage_uuid = pp->p_stor_uuid;
85                 dpart->fstype = pp->p_fstype;
86         } else {
87                 bzero(&dpart->fstype_uuid, uuid_size);
88                 bzero(&dpart->storage_uuid, uuid_size);
89                 dpart->fstype = 0;
90         }
91 }
92
93 /*
94  * Get the number of partitions
95  */
96 static u_int32_t
97 l64_getnumparts(disklabel_t lp)
98 {
99         return(lp.lab64->d_npartitions);
100 }
101
102 /*
103  * Attempt to read a disk label from a device.  64 bit disklabels are
104  * sector-agnostic and begin at offset 0 on the device.  64 bit disklabels
105  * may only be used with GPT partitioning schemes.
106  *
107  * Returns NULL on sucess, and an error string on failure.
108  */
109 static const char *
110 l64_readdisklabel(cdev_t dev, struct diskslice *sp, disklabel_t *lpp,
111                   struct disk_info *info)
112 {
113         struct buf *bp;
114         struct disklabel64 *dlp;
115         const char *msg;
116         uint32_t savecrc;
117         size_t dlpcrcsize;
118         size_t bpsize;
119         int secsize;
120
121         secsize = info->d_media_blksize;
122         bpsize = (sizeof(*dlp) + secsize - 1) & ~(secsize - 1);
123
124         bp = geteblk(bpsize);
125         bp->b_bio1.bio_offset = 0;
126         bp->b_bcount = bpsize;
127         bp->b_flags &= ~B_INVAL;
128         bp->b_cmd = BUF_CMD_READ;
129         dev_dstrategy(dev, &bp->b_bio1);
130
131         if (biowait(bp)) {
132                 msg = "I/O error";
133         } else {
134                 dlp = (struct disklabel64 *)bp->b_data;
135                 dlpcrcsize = offsetof(struct disklabel64,
136                                       d_partitions[dlp->d_npartitions]) -
137                              offsetof(struct disklabel64, d_magic);
138                 savecrc = dlp->d_crc;
139                 dlp->d_crc = 0;
140                 if (dlp->d_magic != DISKMAGIC64) {
141                         msg = "no disk label";
142                 } else if (dlp->d_npartitions > MAXPARTITIONS64) {
143                         msg = "disklabel64 corrupted, too many partitions";
144                 } else if (savecrc != crc32(&dlp->d_magic, dlpcrcsize)) {
145                         msg = "disklabel64 corrupted, bad CRC";
146                 } else {
147                         dlp->d_crc = savecrc;
148                         (*lpp).lab64 = kmalloc(sizeof(*dlp),
149                                                M_DEVBUF, M_WAITOK|M_ZERO);
150                         *(*lpp).lab64 = *dlp;
151                         msg = NULL;
152                 }
153         }
154         bp->b_flags |= B_INVAL | B_AGE;
155         brelse(bp);
156         return (msg);
157 }
158
159 /*
160  * If everything is good, copy olpx to nlpx.  Check to see if any
161  * open partitions would change.
162  */
163 static int
164 l64_setdisklabel(disklabel_t olpx, disklabel_t nlpx, struct diskslices *ssp,
165                  struct diskslice *sp, u_int32_t *openmask)
166 {
167         struct disklabel64 *olp, *nlp;
168         struct partition64 *opp, *npp;
169         uint32_t savecrc;
170         uint64_t slicebsize;
171         size_t nlpcrcsize;
172         int part;
173         int i;
174
175         olp = olpx.lab64;
176         nlp = nlpx.lab64;
177
178         slicebsize = (uint64_t)sp->ds_size * ssp->dss_secsize;
179
180         if (nlp->d_magic != DISKMAGIC64)
181                 return (EINVAL);
182         if (nlp->d_npartitions > MAXPARTITIONS64)
183                 return (EINVAL);
184         savecrc = nlp->d_crc;
185         nlp->d_crc = 0;
186         nlpcrcsize = offsetof(struct disklabel64, 
187                               d_partitions[nlp->d_npartitions]) -
188                      offsetof(struct disklabel64, d_magic);
189         if (crc32(&nlp->d_magic, nlpcrcsize) != savecrc) {
190                 nlp->d_crc = savecrc;
191                 return (EINVAL);
192         }
193         nlp->d_crc = savecrc;
194
195         /*
196          * Check if open partitions have changed
197          */
198         i = 0;
199         while (i < 128) {
200                 if (openmask[i >> 5] == 0) {
201                         i += 32;
202                         continue;
203                 }
204                 if ((openmask[i >> 5] & (1 << (i & 31))) == 0) {
205                         ++i;
206                         continue;
207                 }
208                 if (nlp->d_npartitions <= i)
209                         return (EBUSY);
210                 opp = &olp->d_partitions[i];
211                 npp = &nlp->d_partitions[i];
212                 if (npp->p_boffset != opp->p_boffset ||
213                     npp->p_bsize < opp->p_bsize) {
214                         return (EBUSY);
215                 }
216
217                 /*
218                  * Do not allow p_type_uuid or p_stor_uuid to change if
219                  * the partition is currently open.
220                  */
221                 if (bcmp(&npp->p_type_uuid, &opp->p_type_uuid,
222                      sizeof(npp->p_type_uuid)) != 0) {
223                         return (EBUSY);
224                 }
225                 if (bcmp(&npp->p_stor_uuid, &opp->p_stor_uuid,
226                      sizeof(npp->p_stor_uuid)) != 0) {
227                         return (EBUSY);
228                 }
229                 ++i;
230         }
231
232         /*
233          * Make sure the label and partition offsets and sizes are sane.
234          */
235         if (nlp->d_total_size > slicebsize)
236                 return (ENOSPC);
237         if (nlp->d_total_size & (ssp->dss_secsize - 1))
238                 return (EINVAL);
239         if (nlp->d_bbase & (ssp->dss_secsize - 1))
240                 return (EINVAL);
241         if (nlp->d_pbase & (ssp->dss_secsize - 1))
242                 return (EINVAL);
243         if (nlp->d_pstop & (ssp->dss_secsize - 1))
244                 return (EINVAL);
245         if (nlp->d_abase & (ssp->dss_secsize - 1))
246                 return (EINVAL);
247
248         for (part = 0; part < nlp->d_npartitions; ++part) {
249                 npp = &nlp->d_partitions[i];
250                 if (npp->p_bsize == 0) {
251                         if (npp->p_boffset != 0)
252                                 return (EINVAL);
253                         continue;
254                 }
255                 if (npp->p_boffset & (ssp->dss_secsize - 1))
256                         return (EINVAL);
257                 if (npp->p_bsize & (ssp->dss_secsize - 1))
258                         return (EINVAL);
259                 if (npp->p_boffset < nlp->d_pbase)
260                         return (ENOSPC);
261                 if (npp->p_boffset + npp->p_bsize > nlp->d_total_size)
262                         return (ENOSPC);
263         }
264
265         /*
266          * Structurally we may add code to make modifications above in the
267          * future, so regenerate the crc anyway.
268          */
269         nlp->d_crc = 0;
270         nlp->d_crc = crc32(&nlp->d_magic, nlpcrcsize);
271         *olp = *nlp;
272
273         return (0);
274 }
275
276 /*
277  * Write disk label back to device after modification.
278  */
279 static int
280 l64_writedisklabel(cdev_t dev, struct diskslices *ssp,
281                    struct diskslice *sp, disklabel_t lpx)
282 {
283         struct disklabel64 *lp;
284         struct disklabel64 *dlp;
285         struct buf *bp;
286         int error = 0;
287         size_t bpsize;
288         int secsize;
289
290         lp = lpx.lab64;
291
292         secsize = ssp->dss_secsize;
293         bpsize = (sizeof(*lp) + secsize - 1) & ~(secsize - 1);
294
295         bp = geteblk(bpsize);
296         bp->b_bio1.bio_offset = 0;
297         bp->b_bcount = bpsize;
298
299         /*
300          * Because our I/O is larger then the label, and because we do not
301          * write the d_reserved0[] area, do a read-modify-write.
302          */
303         bp->b_flags &= ~B_INVAL;
304         bp->b_cmd = BUF_CMD_READ;
305         dev_dstrategy(dkmodpart(dev, WHOLE_SLICE_PART), &bp->b_bio1);
306         error = biowait(bp);
307         if (error)
308                 goto done;
309
310         dlp = (void *)bp->b_data;
311         bcopy(&lp->d_magic, &dlp->d_magic,
312               sizeof(*lp) - offsetof(struct disklabel64, d_magic));
313         bp->b_cmd = BUF_CMD_WRITE;
314         dev_dstrategy(dkmodpart(dev, WHOLE_SLICE_PART), &bp->b_bio1);
315         error = biowait(bp);
316 done:
317         bp->b_flags |= B_INVAL | B_AGE;
318         brelse(bp);
319         return (error);
320 }
321
322 /*
323  * Create a disklabel based on a disk_info structure for the purposes of
324  * DSO_COMPATLABEL - cases where no real label exists on the storage medium.
325  *
326  * If a diskslice is passed, the label is truncated to the slice.
327  *
328  * NOTE!  This is not a legal label because d_bbase and d_pbase are both
329  * set to 0.
330  */
331 static disklabel_t
332 l64_clone_label(struct disk_info *info, struct diskslice *sp)
333 {
334         struct disklabel64 *lp;
335         disklabel_t res;
336         uint32_t blksize = info->d_media_blksize;
337         size_t lpcrcsize;
338
339         lp = kmalloc(sizeof *lp, M_DEVBUF, M_WAITOK | M_ZERO);
340
341         if (sp)
342                 lp->d_total_size = (uint64_t)sp->ds_size * blksize;
343         else
344                 lp->d_total_size = info->d_media_blocks * blksize;
345
346         lp->d_magic = DISKMAGIC64;
347         lp->d_align = blksize;
348         lp->d_npartitions = MAXPARTITIONS64;
349         lp->d_pstop = lp->d_total_size;
350
351         /*
352          * Create a dummy 'c' part and a dummy 'a' part (if requested).
353          * Note that the 'c' part is really a hack.  64 bit disklabels
354          * do not use 'c' to mean the raw partition.
355          */
356
357         lp->d_partitions[2].p_boffset = 0;
358         lp->d_partitions[2].p_bsize = lp->d_total_size;
359         /* XXX SET FS TYPE */
360
361         if (info->d_dsflags & DSO_COMPATPARTA) {
362                 lp->d_partitions[0].p_boffset = 0;
363                 lp->d_partitions[0].p_bsize = lp->d_total_size;
364                 /* XXX SET FS TYPE */
365         }
366
367         lpcrcsize = offsetof(struct disklabel64,
368                              d_partitions[lp->d_npartitions]) -
369                     offsetof(struct disklabel64, d_magic);
370
371         lp->d_crc = crc32(&lp->d_magic, lpcrcsize);
372         res.lab64 = lp;
373         return (res);
374 }
375
376 /*
377  * Create a virgin disklabel64 suitable for writing to the media.
378  *
379  * disklabel64 always reserves 32KB for a boot area and leaves room
380  * for up to RESPARTITIONS64 partitions.  
381  */
382 static void
383 l64_makevirginlabel(disklabel_t lpx, struct diskslices *ssp,
384                     struct diskslice *sp, struct disk_info *info)
385 {
386         struct disklabel64 *lp = lpx.lab64;
387         struct partition64 *pp;
388         uint32_t blksize;
389         uint32_t ressize;
390         uint64_t blkmask;       /* 64 bits so we can ~ */
391         size_t lpcrcsize;
392
393         /*
394          * Setup the initial label.  Use of a block size of at least 4KB
395          * for calculating the initial reserved areas to allow some degree
396          * of portability between media with different sector sizes.
397          *
398          * Note that the modified blksize is stored in d_align as a hint
399          * to the disklabeling program.
400          */
401         bzero(lp, sizeof(*lp));
402         if ((blksize = info->d_media_blksize) < 4096)
403                 blksize = 4096;
404         blkmask = blksize - 1;
405
406         if (sp)
407                 lp->d_total_size = (uint64_t)sp->ds_size * ssp->dss_secsize;
408         else
409                 lp->d_total_size = info->d_media_blocks * info->d_media_blksize;
410
411         lp->d_magic = DISKMAGIC64;
412         lp->d_align = blksize;
413         lp->d_npartitions = MAXPARTITIONS64;
414         kern_uuidgen(&lp->d_stor_uuid, 1);
415
416         ressize = offsetof(struct disklabel64, d_partitions[RESPARTITIONS64]);
417         ressize = (ressize + (uint32_t)blkmask) & ~blkmask;
418
419         lp->d_bbase = ressize;
420         lp->d_pbase = lp->d_bbase + ((32768 + blkmask) & ~blkmask);
421         lp->d_pstop = (lp->d_total_size - lp->d_bbase) & ~blkmask;
422         lp->d_abase = lp->d_pstop;
423
424         /*
425          * All partitions are left empty unless DSO_COMPATPARTA is set
426          */
427
428         if (info->d_dsflags & DSO_COMPATPARTA) {
429                 pp = &lp->d_partitions[0];
430                 pp->p_boffset = lp->d_pbase;
431                 pp->p_bsize = lp->d_pstop - lp->d_pbase;
432                 /* XXX SET FS TYPE */
433         }
434
435         lpcrcsize = offsetof(struct disklabel64,
436                              d_partitions[lp->d_npartitions]) -
437                     offsetof(struct disklabel64, d_magic);
438         lp->d_crc = crc32(&lp->d_magic, lpcrcsize);
439 }
440
441 /*
442  * Set the number of blocks at the beginning of the slice which have
443  * been reserved for label operations.  This area will be write-protected
444  * when accessed via the slice.
445  *
446  * For now just protect the label area proper.  Do not protect the
447  * boot area.  Note partitions in 64 bit disklabels do not overlap
448  * the disklabel or boot area.
449  */
450 static void
451 l64_adjust_label_reserved(struct diskslices *ssp, int slice,
452                           struct diskslice *sp)
453 {
454         struct disklabel64 *lp = sp->ds_label.lab64;
455
456         sp->ds_reserved = lp->d_bbase / ssp->dss_secsize;
457 }
458
459 struct disklabel_ops disklabel64_ops = {
460         .labelsize = sizeof(struct disklabel64),
461         .op_readdisklabel = l64_readdisklabel,
462         .op_setdisklabel = l64_setdisklabel,
463         .op_writedisklabel = l64_writedisklabel,
464         .op_clone_label = l64_clone_label,
465         .op_adjust_label_reserved = l64_adjust_label_reserved,
466         .op_getpartbounds = l64_getpartbounds,
467         .op_loadpartinfo = l64_loadpartinfo,
468         .op_getnumparts = l64_getnumparts,
469         .op_makevirginlabel = l64_makevirginlabel
470 };
471