]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Grow.c
Grow: get component_size before using it.
[thirdparty/mdadm.git] / Grow.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@suse.de>
23 */
24 #include "mdadm.h"
25 #include "dlink.h"
26 #include <sys/mman.h>
27
28 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
29 #error no endian defined
30 #endif
31 #include "md_u.h"
32 #include "md_p.h"
33
34 #ifndef offsetof
35 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
36 #endif
37
38 int Grow_Add_device(char *devname, int fd, char *newdev)
39 {
40 /* Add a device to an active array.
41 * Currently, just extend a linear array.
42 * This requires writing a new superblock on the
43 * new device, calling the kernel to add the device,
44 * and if that succeeds, update the superblock on
45 * all other devices.
46 * This means that we need to *find* all other devices.
47 */
48 struct mdinfo info;
49
50 struct stat stb;
51 int nfd, fd2;
52 int d, nd;
53 struct supertype *st = NULL;
54
55
56 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
57 fprintf(stderr, Name ": cannot get array info for %s\n", devname);
58 return 1;
59 }
60
61 st = super_by_fd(fd);
62 if (!st) {
63 fprintf(stderr, Name ": cannot handle arrays with superblock version %d\n", info.array.major_version);
64 return 1;
65 }
66
67 if (info.array.level != -1) {
68 fprintf(stderr, Name ": can only add devices to linear arrays\n");
69 return 1;
70 }
71
72 nfd = open(newdev, O_RDWR|O_EXCL|O_DIRECT);
73 if (nfd < 0) {
74 fprintf(stderr, Name ": cannot open %s\n", newdev);
75 return 1;
76 }
77 fstat(nfd, &stb);
78 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
79 fprintf(stderr, Name ": %s is not a block device!\n", newdev);
80 close(nfd);
81 return 1;
82 }
83 /* now check out all the devices and make sure we can read the superblock */
84 for (d=0 ; d < info.array.raid_disks ; d++) {
85 mdu_disk_info_t disk;
86 char *dv;
87
88 disk.number = d;
89 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
90 fprintf(stderr, Name ": cannot get device detail for device %d\n",
91 d);
92 return 1;
93 }
94 dv = map_dev(disk.major, disk.minor, 1);
95 if (!dv) {
96 fprintf(stderr, Name ": cannot find device file for device %d\n",
97 d);
98 return 1;
99 }
100 fd2 = dev_open(dv, O_RDWR);
101 if (!fd2) {
102 fprintf(stderr, Name ": cannot open device file %s\n", dv);
103 return 1;
104 }
105 st->ss->free_super(st);
106
107 if (st->ss->load_super(st, fd2, NULL)) {
108 fprintf(stderr, Name ": cannot find super block on %s\n", dv);
109 close(fd2);
110 return 1;
111 }
112 close(fd2);
113 }
114 /* Ok, looks good. Lets update the superblock and write it out to
115 * newdev.
116 */
117
118 info.disk.number = d;
119 info.disk.major = major(stb.st_rdev);
120 info.disk.minor = minor(stb.st_rdev);
121 info.disk.raid_disk = d;
122 info.disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
123 st->ss->update_super(st, &info, "linear-grow-new", newdev,
124 0, 0, NULL);
125
126 if (st->ss->store_super(st, nfd)) {
127 fprintf(stderr, Name ": Cannot store new superblock on %s\n",
128 newdev);
129 close(nfd);
130 return 1;
131 }
132 close(nfd);
133
134 if (ioctl(fd, ADD_NEW_DISK, &info.disk) != 0) {
135 fprintf(stderr, Name ": Cannot add new disk to this array\n");
136 return 1;
137 }
138 /* Well, that seems to have worked.
139 * Now go through and update all superblocks
140 */
141
142 if (ioctl(fd, GET_ARRAY_INFO, &info.array) < 0) {
143 fprintf(stderr, Name ": cannot get array info for %s\n", devname);
144 return 1;
145 }
146
147 nd = d;
148 for (d=0 ; d < info.array.raid_disks ; d++) {
149 mdu_disk_info_t disk;
150 char *dv;
151
152 disk.number = d;
153 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
154 fprintf(stderr, Name ": cannot get device detail for device %d\n",
155 d);
156 return 1;
157 }
158 dv = map_dev(disk.major, disk.minor, 1);
159 if (!dv) {
160 fprintf(stderr, Name ": cannot find device file for device %d\n",
161 d);
162 return 1;
163 }
164 fd2 = dev_open(dv, O_RDWR);
165 if (fd2 < 0) {
166 fprintf(stderr, Name ": cannot open device file %s\n", dv);
167 return 1;
168 }
169 if (st->ss->load_super(st, fd2, NULL)) {
170 fprintf(stderr, Name ": cannot find super block on %s\n", dv);
171 close(fd);
172 return 1;
173 }
174 info.array.raid_disks = nd+1;
175 info.array.nr_disks = nd+1;
176 info.array.active_disks = nd+1;
177 info.array.working_disks = nd+1;
178
179 st->ss->update_super(st, &info, "linear-grow-update", dv,
180 0, 0, NULL);
181
182 if (st->ss->store_super(st, fd2)) {
183 fprintf(stderr, Name ": Cannot store new superblock on %s\n", dv);
184 close(fd2);
185 return 1;
186 }
187 close(fd2);
188 }
189
190 return 0;
191 }
192
193 int Grow_addbitmap(char *devname, int fd, char *file, int chunk, int delay, int write_behind, int force)
194 {
195 /*
196 * First check that array doesn't have a bitmap
197 * Then create the bitmap
198 * Then add it
199 *
200 * For internal bitmaps, we need to check the version,
201 * find all the active devices, and write the bitmap block
202 * to all devices
203 */
204 mdu_bitmap_file_t bmf;
205 mdu_array_info_t array;
206 struct supertype *st;
207 int major = BITMAP_MAJOR_HI;
208 int vers = md_get_version(fd);
209 unsigned long long bitmapsize, array_size;
210
211 if (vers < 9003) {
212 major = BITMAP_MAJOR_HOSTENDIAN;
213 #ifdef __BIG_ENDIAN
214 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
215 " between different architectured. Consider upgrading the Linux kernel.\n");
216 #endif
217 }
218
219 if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) {
220 if (errno == ENOMEM)
221 fprintf(stderr, Name ": Memory allocation failure.\n");
222 else
223 fprintf(stderr, Name ": bitmaps not supported by this kernel.\n");
224 return 1;
225 }
226 if (bmf.pathname[0]) {
227 if (strcmp(file,"none")==0) {
228 if (ioctl(fd, SET_BITMAP_FILE, -1)!= 0) {
229 fprintf(stderr, Name ": failed to remove bitmap %s\n",
230 bmf.pathname);
231 return 1;
232 }
233 return 0;
234 }
235 fprintf(stderr, Name ": %s already has a bitmap (%s)\n",
236 devname, bmf.pathname);
237 return 1;
238 }
239 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
240 fprintf(stderr, Name ": cannot get array status for %s\n", devname);
241 return 1;
242 }
243 if (array.state & (1<<MD_SB_BITMAP_PRESENT)) {
244 if (strcmp(file, "none")==0) {
245 array.state &= ~(1<<MD_SB_BITMAP_PRESENT);
246 if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
247 fprintf(stderr, Name ": failed to remove internal bitmap.\n");
248 return 1;
249 }
250 return 0;
251 }
252 fprintf(stderr, Name ": Internal bitmap already present on %s\n",
253 devname);
254 return 1;
255 }
256 if (array.level <= 0) {
257 fprintf(stderr, Name ": Bitmaps not meaningful with level %s\n",
258 map_num(pers, array.level)?:"of this array");
259 return 1;
260 }
261 bitmapsize = array.size;
262 bitmapsize <<= 1;
263 if (get_dev_size(fd, NULL, &array_size) &&
264 array_size > (0x7fffffffULL<<9)) {
265 /* Array is big enough that we cannot trust array.size
266 * try other approaches
267 */
268 bitmapsize = get_component_size(fd);
269 }
270 if (bitmapsize == 0) {
271 fprintf(stderr, Name ": Cannot reliably determine size of array to create bitmap - sorry.\n");
272 return 1;
273 }
274
275 if (array.level == 10) {
276 int ncopies = (array.layout&255)*((array.layout>>8)&255);
277 bitmapsize = bitmapsize * array.raid_disks / ncopies;
278 }
279
280 st = super_by_fd(fd);
281 if (!st) {
282 fprintf(stderr, Name ": Cannot understand version %d.%d\n",
283 array.major_version, array.minor_version);
284 return 1;
285 }
286 if (strcmp(file, "none") == 0) {
287 fprintf(stderr, Name ": no bitmap found on %s\n", devname);
288 return 1;
289 } else if (strcmp(file, "internal") == 0) {
290 int d;
291 for (d=0; d< st->max_devs; d++) {
292 mdu_disk_info_t disk;
293 char *dv;
294 disk.number = d;
295 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
296 continue;
297 if (disk.major == 0 &&
298 disk.minor == 0)
299 continue;
300 if ((disk.state & (1<<MD_DISK_SYNC))==0)
301 continue;
302 dv = map_dev(disk.major, disk.minor, 1);
303 if (dv) {
304 int fd2 = dev_open(dv, O_RDWR);
305 if (fd2 < 0)
306 continue;
307 if (st->ss->load_super(st, fd2, NULL)==0) {
308 if (st->ss->add_internal_bitmap(
309 st,
310 &chunk, delay, write_behind,
311 bitmapsize, 0, major)
312 )
313 st->ss->write_bitmap(st, fd2);
314 else {
315 fprintf(stderr, Name ": failed to create internal bitmap - chunksize problem.\n");
316 close(fd2);
317 return 1;
318 }
319 }
320 close(fd2);
321 }
322 }
323 array.state |= (1<<MD_SB_BITMAP_PRESENT);
324 if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
325 fprintf(stderr, Name ": failed to set internal bitmap.\n");
326 return 1;
327 }
328 } else {
329 int uuid[4];
330 int bitmap_fd;
331 int d;
332 int max_devs = st->max_devs;
333
334 /* try to load a superblock */
335 for (d=0; d<max_devs; d++) {
336 mdu_disk_info_t disk;
337 char *dv;
338 int fd2;
339 disk.number = d;
340 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
341 continue;
342 if ((disk.major==0 && disk.minor==0) ||
343 (disk.state & (1<<MD_DISK_REMOVED)))
344 continue;
345 dv = map_dev(disk.major, disk.minor, 1);
346 if (!dv) continue;
347 fd2 = dev_open(dv, O_RDONLY);
348 if (fd2 >= 0 &&
349 st->ss->load_super(st, fd2, NULL) == 0) {
350 close(fd2);
351 st->ss->uuid_from_super(st, uuid);
352 break;
353 }
354 close(fd2);
355 }
356 if (d == max_devs) {
357 fprintf(stderr, Name ": cannot find UUID for array!\n");
358 return 1;
359 }
360 if (CreateBitmap(file, force, (char*)uuid, chunk,
361 delay, write_behind, bitmapsize, major)) {
362 return 1;
363 }
364 bitmap_fd = open(file, O_RDWR);
365 if (bitmap_fd < 0) {
366 fprintf(stderr, Name ": weird: %s cannot be opened\n",
367 file);
368 return 1;
369 }
370 if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) {
371 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
372 devname, strerror(errno));
373 return 1;
374 }
375 }
376
377 return 0;
378 }
379
380
381 /*
382 * When reshaping an array we might need to backup some data.
383 * This is written to all spares with a 'super_block' describing it.
384 * The superblock goes 1K form the end of the used space on the
385 * device.
386 * It if written after the backup is complete.
387 * It has the following structure.
388 */
389
390 static struct mdp_backup_super {
391 char magic[16]; /* md_backup_data-1 or -2 */
392 __u8 set_uuid[16];
393 __u64 mtime;
394 /* start/sizes in 512byte sectors */
395 __u64 devstart; /* address on backup device/file of data */
396 __u64 arraystart;
397 __u64 length;
398 __u32 sb_csum; /* csum of preceeding bytes. */
399 __u32 pad1;
400 __u64 devstart2; /* offset in to data of second section */
401 __u64 arraystart2;
402 __u64 length2;
403 __u32 sb_csum2; /* csum of preceeding bytes. */
404 __u8 pad[512-68-32];
405 } __attribute__((aligned(512))) bsb, bsb2;
406
407 int bsb_csum(char *buf, int len)
408 {
409 int i;
410 int csum = 0;
411 for (i=0; i<len; i++)
412 csum = (csum<<3) + buf[0];
413 return __cpu_to_le32(csum);
414 }
415
416 static int child_grow(int afd, struct mdinfo *sra, unsigned long blocks,
417 int *fds, unsigned long long *offsets,
418 int disks, int chunk, int level, int layout, int data,
419 int dests, int *destfd, unsigned long long *destoffsets);
420 static int child_shrink(int afd, struct mdinfo *sra, unsigned long blocks,
421 int *fds, unsigned long long *offsets,
422 int disks, int chunk, int level, int layout, int data,
423 int dests, int *destfd, unsigned long long *destoffsets);
424 static int child_same_size(int afd, struct mdinfo *sra, unsigned long blocks,
425 int *fds, unsigned long long *offsets,
426 unsigned long long start,
427 int disks, int chunk, int level, int layout, int data,
428 int dests, int *destfd, unsigned long long *destoffsets);
429
430 int freeze_array(struct mdinfo *sra)
431 {
432 /* Try to freeze resync on this array.
433 * Return -1 if the array is busy,
434 * return 0 if this kernel doesn't support 'frozen'
435 * return 1 if it worked.
436 */
437 char buf[20];
438 if (sysfs_get_str(sra, NULL, "sync_action", buf, 20) <= 0)
439 return 0;
440 if (strcmp(buf, "idle\n") != 0 &&
441 strcmp(buf, "frozen\n") != 0)
442 return -1;
443 if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0)
444 return 0;
445 return 1;
446 }
447
448 void unfreeze_array(struct mdinfo *sra, int frozen)
449 {
450 /* If 'frozen' is 1, unfreeze the array */
451 if (frozen > 0)
452 sysfs_set_str(sra, NULL, "sync_action", "idle");
453 }
454
455 void wait_reshape(struct mdinfo *sra)
456 {
457 int fd = sysfs_get_fd(sra, NULL, "sync_action");
458 char action[20];
459
460 do {
461 fd_set rfds;
462 FD_ZERO(&rfds);
463 FD_SET(fd, &rfds);
464 select(fd+1, NULL, NULL, &rfds, NULL);
465
466 if (sysfs_fd_get_str(fd, action, 20) < 0) {
467 close(fd);
468 return;
469 }
470 } while (strncmp(action, "reshape", 7) == 0);
471 }
472
473
474 int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
475 long long size,
476 int level, char *layout_str, int chunksize, int raid_disks)
477 {
478 /* Make some changes in the shape of an array.
479 * The kernel must support the change.
480 *
481 * There are three different changes. Each can trigger
482 * a resync or recovery so we freeze that until we have
483 * requested everything (if kernel supports freezing - 2.6.30).
484 * The steps are:
485 * - change size (i.e. component_size)
486 * - change level
487 * - change layout/chunksize/ndisks
488 *
489 * The last can require a reshape. It is different on different
490 * levels so we need to check the level before actioning it.
491 * Some times the level change needs to be requested after the
492 * reshape (e.g. raid6->raid5, raid5->raid0)
493 *
494 */
495 struct mdu_array_info_s array, orig;
496 char *c;
497 int rv = 0;
498 struct supertype *st;
499
500 int nchunk, ochunk;
501 int nlayout, olayout;
502 int ndisks, odisks;
503 int ndata, odata;
504 int orig_level = UnSet;
505 char alt_layout[40];
506 int *fdlist;
507 unsigned long long *offsets;
508 int d, i;
509 int nrdisks;
510 int err;
511 int frozen;
512 unsigned long a,b, blocks, stripes;
513 int cache;
514 unsigned long long array_size;
515 int changed = 0;
516 int done;
517
518 struct mdinfo *sra;
519 struct mdinfo *sd;
520
521 if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
522 fprintf(stderr, Name ": %s is not an active md array - aborting\n",
523 devname);
524 return 1;
525 }
526
527 if (raid_disks && raid_disks < array.raid_disks && array.level > 1 &&
528 get_linux_version() < 2006032 &&
529 !check_env("MDADM_FORCE_FEWER")) {
530 fprintf(stderr, Name ": reducing the number of devices is not safe before Linux 2.6.32\n"
531 " Please use a newer kernel\n");
532 return 1;
533 }
534 sra = sysfs_read(fd, 0, GET_LEVEL);
535 frozen = freeze_array(sra);
536 if (frozen < 0) {
537 fprintf(stderr, Name ": %s is performing resync/recovery and cannot"
538 " be reshaped\n", devname);
539 return 1;
540 }
541
542 /* ========= set size =============== */
543 if (size >= 0 && (size == 0 || size != array.size)) {
544 array.size = size;
545 if (array.size != size) {
546 /* got truncated to 32bit, write to
547 * component_size instead
548 */
549 if (sra)
550 rv = sysfs_set_num(sra, NULL,
551 "component_size", size);
552 else
553 rv = -1;
554 } else
555 rv = ioctl(fd, SET_ARRAY_INFO, &array);
556 if (rv != 0) {
557 fprintf(stderr, Name ": Cannot set device size for %s: %s\n",
558 devname, strerror(errno));
559 rv = 1;
560 goto release;
561 }
562 ioctl(fd, GET_ARRAY_INFO, &array);
563 if (!quiet)
564 fprintf(stderr, Name ": component size of %s has been set to %dK\n",
565 devname, array.size);
566 changed = 1;
567 }
568
569 /* ======= set level =========== */
570 if (level != UnSet && level != array.level) {
571 /* Trying to change the level.
572 * We might need to change layout first and schedule a
573 * level change for later.
574 * Level changes that can happen immediately are:
575 * 0->4,5,6 1->5 4->5,6 5->1,6
576 * Level changes that need a layout change first are:
577 * 6->5,4,0 : need a -6 layout, or parity-last
578 * 5->4,0 : need parity-last
579 */
580 if ((array.level == 6 || array.level == 5) &&
581 (level == 5 || level == 4 || level == 0)) {
582 /* Don't change level yet, but choose intermediate
583 * layout
584 */
585 if (level == 5) {
586 if (layout_str == NULL)
587 switch (array.layout) {
588 case ALGORITHM_LEFT_ASYMMETRIC:
589 case ALGORITHM_LEFT_ASYMMETRIC_6:
590 case ALGORITHM_ROTATING_N_RESTART:
591 layout_str = "left-asymmetric-6";
592 break;
593 case ALGORITHM_LEFT_SYMMETRIC:
594 case ALGORITHM_LEFT_SYMMETRIC_6:
595 case ALGORITHM_ROTATING_N_CONTINUE:
596 layout_str = "left-symmetric-6";
597 break;
598 case ALGORITHM_RIGHT_ASYMMETRIC:
599 case ALGORITHM_RIGHT_ASYMMETRIC_6:
600 case ALGORITHM_ROTATING_ZERO_RESTART:
601 layout_str = "right-asymmetric-6";
602 break;
603 case ALGORITHM_RIGHT_SYMMETRIC:
604 case ALGORITHM_RIGHT_SYMMETRIC_6:
605 layout_str = "right-symmetric-6";
606 break;
607 case ALGORITHM_PARITY_0:
608 case ALGORITHM_PARITY_0_6:
609 layout_str = "parity-first-6";
610 break;
611 case ALGORITHM_PARITY_N:
612 layout_str = "parity-last";
613 break;
614 default:
615 fprintf(stderr, Name ": %s: cannot"
616 "convert layout to RAID5 equivalent\n",
617 devname);
618 rv = 1;
619 goto release;
620 }
621 else {
622 int l = map_name(r5layout, layout_str);
623 if (l == UnSet) {
624 fprintf(stderr, Name ": %s: layout '%s' not recognised\n",
625 devname, layout_str);
626 rv = 1;
627 goto release;
628 }
629 if (l != ALGORITHM_PARITY_N) {
630 /* need the -6 version */
631 char *ls = map_num(r5layout, l);
632 strcat(strcpy(alt_layout, ls),
633 "-6");
634 layout_str = alt_layout;
635 }
636 }
637 if (raid_disks)
638 /* The final raid6->raid5 conversion
639 * will reduce the number of disks,
640 * so now we need to aim higher
641 */
642 raid_disks++;
643 } else
644 layout_str = "parity-last";
645 } else {
646 c = map_num(pers, level);
647 if (c == NULL)
648 return 1;/* not possible */
649 err = sysfs_set_str(sra, NULL, "level", c);
650 if (err) {
651 fprintf(stderr, Name ": %s: could not set level to %s\n",
652 devname, c);
653 rv = 1;
654 goto release;
655 }
656 orig = array;
657 orig_level = orig.level;
658 ioctl(fd, GET_ARRAY_INFO, &array);
659 if (layout_str == NULL &&
660 orig.level == 5 && level == 6 &&
661 array.layout != orig.layout)
662 layout_str = map_num(r5layout, orig.layout);
663 if (!quiet)
664 fprintf(stderr, Name " level of %s changed to %s\n",
665 devname, c);
666 changed = 1;
667 }
668 }
669
670 /* ========= set shape (chunk_size / layout / ndisks) ============== */
671 /* Check if layout change is a no-op */
672 if (layout_str) switch(array.level) {
673 case 5:
674 if (array.layout == map_name(r5layout, layout_str))
675 layout_str = NULL;
676 break;
677 case 6:
678 if (layout_str == NULL &&
679 ((chunksize && chunksize * 1024 != array.chunk_size) ||
680 (raid_disks && raid_disks != array.raid_disks)) &&
681 array.layout >= 16) {
682 fprintf(stderr, Name
683 ": %s has a non-standard layout. If you wish to preserve this\n"
684 " during the reshape, please specify --layout=preserve\n"
685 " If you want to change it, specify a layout or use --layout=normalise\n",
686 devname);
687 rv = 1;
688 goto release;
689 }
690 if (strcmp(layout_str, "normalise") == 0 ||
691 strcmp(layout_str, "normalize") == 0) {
692 char *hyphen;
693 strcpy(alt_layout, map_num(r6layout, array.layout));
694 hyphen = strrchr(alt_layout, '-');
695 if (hyphen && strcmp(hyphen, "-6") == 0) {
696 *hyphen = 0;
697 layout_str = alt_layout;
698 }
699 }
700
701 if (array.layout == map_name(r6layout, layout_str))
702 layout_str = NULL;
703 if (layout_str && strcmp(layout_str, "preserve") == 0)
704 layout_str = NULL;
705 break;
706 }
707 if (layout_str == NULL
708 && (chunksize == 0 || chunksize*1024 == array.chunk_size)
709 && (raid_disks == 0 || raid_disks == array.raid_disks)) {
710 rv = 0;
711 if (level != UnSet && level != array.level) {
712 /* Looks like this level change doesn't need
713 * a reshape after all.
714 */
715 c = map_num(pers, level);
716 if (c) {
717 rv = sysfs_set_str(sra, NULL, "level", c);
718 if (rv)
719 fprintf(stderr, Name ": %s: could not set level to %s\n",
720 devname, c);
721 }
722 } else if (!changed && !quiet)
723 fprintf(stderr, Name ": %s: no change requested\n",
724 devname);
725 goto release;
726 }
727
728 c = map_num(pers, array.level);
729 if (c == NULL) c = "-unknown-";
730 switch(array.level) {
731 default: /* raid0, linear, multipath cannot be reconfigured */
732 fprintf(stderr, Name ": %s array %s cannot be reshaped.\n",
733 c, devname);
734 rv = 1;
735 break;
736
737 case LEVEL_FAULTY: /* only 'layout' change is permitted */
738
739 if (chunksize || raid_disks) {
740 fprintf(stderr, Name ": %s: Cannot change chunksize or disks of a 'faulty' array\n",
741 devname);
742 rv = 1;
743 break;
744 }
745 if (layout_str == NULL)
746 break; /* nothing to do.... */
747
748 array.layout = parse_layout_faulty(layout_str);
749 if (array.layout < 0) {
750 int rv;
751 fprintf(stderr, Name ": %s: layout %s not understood for 'faulty' array\n",
752 devname, layout_str);
753 rv = 1;
754 break;
755 }
756 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
757 fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
758 devname, strerror(errno));
759 rv = 1;
760 } else if (!quiet)
761 printf("layout for %s set to %d\n", devname, array.layout);
762 break;
763
764 case 1: /* only raid_disks can each be changed. */
765
766 if (chunksize || layout_str != NULL) {
767 fprintf(stderr, Name ": %s: Cannot change chunk size or layout for a RAID1 array.\n",
768 devname);
769 rv = 1;
770 break;
771 }
772 if (raid_disks > 0) {
773 array.raid_disks = raid_disks;
774 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
775 fprintf(stderr, Name ": Cannot set raid-devices for %s: %s\n",
776 devname, strerror(errno));
777 rv = 1;
778 }
779 }
780 break;
781
782 case 4:
783 case 5:
784 case 6:
785
786 /*
787 * layout/chunksize/raid_disks can be changed
788 * though the kernel may not support it all.
789 */
790 st = super_by_fd(fd);
791
792 /*
793 * There are three possibilities.
794 * 1/ The array will shrink.
795 * We need to ensure the reshape will pause before reaching
796 * the 'critical section'. We also need to fork and wait for
797 * that to happen. When it does we
798 * suspend/backup/complete/unfreeze
799 *
800 * 2/ The array will not change size.
801 * This requires that we keep a backup of a sliding window
802 * so that we can restore data after a crash. So we need
803 * to fork and monitor progress.
804 *
805 * 3/ The array will grow. This is relatively easy.
806 * However the kernel's restripe routines will cheerfully
807 * overwrite some early data before it is safe. So we
808 * need to make a backup of the early parts of the array
809 * and be ready to restore it if rebuild aborts very early.
810 *
811 * We backup data by writing it to one spare, or to a
812 * file which was given on command line.
813 *
814 * [FOLLOWING IS OLD AND PARTLY WRONG]
815 * So: we enumerate the devices in the array and
816 * make sure we can open all of them.
817 * Then we freeze the early part of the array and
818 * backup to the various spares.
819 * Then we request changes and start the reshape.
820 * Monitor progress until it has passed the danger zone.
821 * and finally invalidate the copied data and unfreeze the
822 * start of the array.
823 *
824 * In each case, we first make sure that storage is available
825 * for the required backup.
826 * Then we:
827 * - request the shape change.
828 * - for to handle backup etc.
829 */
830 nchunk = ochunk = array.chunk_size;
831 nlayout = olayout = array.layout;
832 ndisks = odisks = array.raid_disks;
833
834 if (chunksize) {
835 nchunk = chunksize * 1024;
836 if (array.size % chunksize) {
837 fprintf(stderr, Name ": component size %dK is not"
838 " a multiple of chunksize %dK\n",
839 array.size, chunksize);
840 break;
841 }
842 }
843 if (layout_str != NULL)
844 switch(array.level) {
845 case 4: /* ignore layout */
846 break;
847 case 5:
848 nlayout = map_name(r5layout, layout_str);
849 if (nlayout == UnSet) {
850 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
851 layout_str);
852 return 1;
853 }
854 break;
855
856 case 6:
857 nlayout = map_name(r6layout, layout_str);
858 if (nlayout == UnSet) {
859 fprintf(stderr, Name ": layout %s not understood for raid6.\n",
860 layout_str);
861 return 1;
862 }
863 break;
864 }
865 if (raid_disks) ndisks = raid_disks;
866
867 odata = odisks-1;
868 ndata = ndisks-1;
869 if (array.level == 6) {
870 odata--; /* number of data disks */
871 ndata--;
872 }
873
874 /* Check that we can hold all the data */
875 size = ndata * array.size;
876 get_dev_size(fd, NULL, &array_size);
877 if (size < (array_size/1024)) {
878 fprintf(stderr, Name ": this change will reduce the size of the array.\n"
879 " use --grow --array-size first to truncate array.\n"
880 " e.g. mdadm --grow %s --array-size %llu\n",
881 devname, size);
882 rv = 1;
883 break;
884 }
885
886 /* So how much do we need to backup.
887 * We need an amount of data which is both a whole number of
888 * old stripes and a whole number of new stripes.
889 * So LCM for (chunksize*datadisks).
890 */
891 a = ochunk/512 * odata;
892 b = nchunk/512 * ndata;
893 /* Find GCD */
894 while (a != b) {
895 if (a < b)
896 b -= a;
897 if (b < a)
898 a -= b;
899 }
900 /* LCM == product / GCD */
901 blocks = ochunk/512 * nchunk/512 * odata * ndata / a;
902
903 sysfs_free(sra);
904 sra = sysfs_read(fd, 0,
905 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|
906 GET_CACHE);
907
908 if (ndata == odata) {
909 /* Make 'blocks' bigger for better throughput, but
910 * not so big that we reject it below.
911 */
912 if (blocks * 32 < sra->component_size)
913 blocks *= 16;
914 } else
915 fprintf(stderr, Name ": Need to backup %luK of critical "
916 "section..\n", blocks/2);
917
918 if (!sra) {
919 fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n",
920 devname);
921 rv = 1;
922 break;
923 }
924
925 if (blocks >= sra->component_size/2) {
926 fprintf(stderr, Name ": %s: Something wrong - reshape aborted\n",
927 devname);
928 rv = 1;
929 break;
930 }
931 nrdisks = array.nr_disks + sra->array.spare_disks;
932 /* Now we need to open all these devices so we can read/write.
933 */
934 fdlist = malloc((1+nrdisks) * sizeof(int));
935 offsets = malloc((1+nrdisks) * sizeof(offsets[0]));
936 if (!fdlist || !offsets) {
937 fprintf(stderr, Name ": malloc failed: grow aborted\n");
938 rv = 1;
939 break;
940 }
941 for (d=0; d <= nrdisks; d++)
942 fdlist[d] = -1;
943 d = array.raid_disks;
944 for (sd = sra->devs; sd; sd=sd->next) {
945 if (sd->disk.state & (1<<MD_DISK_FAULTY))
946 continue;
947 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
948 char *dn = map_dev(sd->disk.major,
949 sd->disk.minor, 1);
950 fdlist[sd->disk.raid_disk]
951 = dev_open(dn, O_RDONLY);
952 offsets[sd->disk.raid_disk] = sd->data_offset*512;
953 if (fdlist[sd->disk.raid_disk] < 0) {
954 fprintf(stderr, Name ": %s: cannot open component %s\n",
955 devname, dn?dn:"-unknown-");
956 rv = 1;
957 goto release;
958 }
959 } else if (backup_file == NULL) {
960 /* spare */
961 char *dn = map_dev(sd->disk.major,
962 sd->disk.minor, 1);
963 fdlist[d] = dev_open(dn, O_RDWR);
964 offsets[d] = (sra->component_size - blocks - 8)*512;
965 if (fdlist[d]<0) {
966 fprintf(stderr, Name ": %s: cannot open component %s\n",
967 devname, dn?dn:"-unknown");
968 rv = 1;
969 goto release;
970 }
971 d++;
972 }
973 }
974 if (backup_file == NULL) {
975 if (ndata <= odata) {
976 fprintf(stderr, Name ": %s: Cannot grow - need backup-file\n",
977 devname);
978 rv = 1;
979 break;
980 } else if (sra->array.spare_disks == 0) {
981 fprintf(stderr, Name ": %s: Cannot grow - need a spare or "
982 "backup-file to backup critical section\n",
983 devname);
984 rv = 1;
985 break;
986 }
987 if (d == array.raid_disks) {
988 fprintf(stderr, Name ": %s: No spare device for backup\n",
989 devname);
990 rv = 1;
991 break;
992 }
993 } else {
994 /* need to check backup file is large enough */
995 char buf[512];
996 fdlist[d] = open(backup_file, O_RDWR|O_CREAT|O_EXCL,
997 S_IRUSR | S_IWUSR);
998 offsets[d] = 8 * 512;
999 if (fdlist[d] < 0) {
1000 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
1001 devname, backup_file, strerror(errno));
1002 rv = 1;
1003 break;
1004 }
1005 memset(buf, 0, 512);
1006 for (i=0; i < blocks + 1 ; i++) {
1007 if (write(fdlist[d], buf, 512) != 512) {
1008 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
1009 devname, backup_file, strerror(errno));
1010 rv = 1;
1011 break;
1012 }
1013 }
1014 if (fsync(fdlist[d]) != 0) {
1015 fprintf(stderr, Name ": %s: cannot create backup file %s: %s\n",
1016 devname, backup_file, strerror(errno));
1017 rv = 1;
1018 break;
1019 }
1020 d++;
1021 }
1022
1023 /* lastly, check that the internal stripe cache is
1024 * large enough, or it won't work.
1025 */
1026
1027 cache = (nchunk < ochunk) ? ochunk : nchunk;
1028 cache = cache * 4 / 4096;
1029 if (sra->cache_size < cache)
1030 sysfs_set_num(sra, NULL, "stripe_cache_size",
1031 cache+1);
1032 /* Right, everything seems fine. Let's kick things off.
1033 * If only changing raid_disks, use ioctl, else use
1034 * sysfs.
1035 */
1036 if (ochunk == nchunk && olayout == nlayout) {
1037 array.raid_disks = ndisks;
1038 if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
1039 rv = 1;
1040 fprintf(stderr, Name ": Cannot set device shape for %s: %s\n",
1041 devname, strerror(errno));
1042 if (ndisks < odisks &&
1043 get_linux_version() < 2006030)
1044 fprintf(stderr, Name ": linux 2.6.30 or later required\n");
1045
1046 break;
1047 }
1048 } else {
1049 /* set them all just in case some old 'new_*' value
1050 * persists from some earlier problem
1051 */
1052 if (sysfs_set_num(sra, NULL, "chunk_size", nchunk) < 0)
1053 rv = 1;
1054 if (sysfs_set_num(sra, NULL, "layout", nlayout) < 0)
1055 rv = 1;
1056 if (sysfs_set_num(sra, NULL, "raid_disks", ndisks) < 0)
1057 rv = 1;
1058 if (rv) {
1059 fprintf(stderr, Name ": Cannot set device shape for %s\n",
1060 devname);
1061 if (get_linux_version() < 2006030)
1062 fprintf(stderr, Name ": linux 2.6.30 or later required\n");
1063 break;
1064 }
1065 }
1066
1067 if (ndisks == 2 && odisks == 2) {
1068 /* No reshape is needed in this trivial case */
1069 rv = 0;
1070 break;
1071 }
1072
1073 /* set up the backup-super-block. This requires the
1074 * uuid from the array.
1075 */
1076 /* Find a superblock */
1077 for (sd = sra->devs; sd; sd = sd->next) {
1078 char *dn;
1079 int devfd;
1080 int ok;
1081 if (sd->disk.state & (1<<MD_DISK_FAULTY))
1082 continue;
1083 dn = map_dev(sd->disk.major, sd->disk.minor, 1);
1084 devfd = dev_open(dn, O_RDONLY);
1085 if (devfd < 0)
1086 continue;
1087 ok = st->ss->load_super(st, devfd, NULL);
1088 close(devfd);
1089 if (ok >= 0)
1090 break;
1091 }
1092 if (!sd) {
1093 fprintf(stderr, Name ": %s: Cannot find a superblock\n",
1094 devname);
1095 rv = 1;
1096 break;
1097 }
1098
1099 memset(&bsb, 0, 512);
1100 memcpy(bsb.magic, "md_backup_data-1", 16);
1101 st->ss->uuid_from_super(st, (int*)&bsb.set_uuid);
1102 bsb.mtime = __cpu_to_le64(time(0));
1103 bsb.devstart2 = blocks;
1104 stripes = blocks / (ochunk/512) / odata;
1105 /* Now we just need to kick off the reshape and watch, while
1106 * handling backups of the data...
1107 * This is all done by a forked background process.
1108 */
1109 switch(fork()) {
1110 case 0:
1111 close(fd);
1112 if (check_env("MDADM_GROW_VERIFY"))
1113 fd = open(devname, O_RDONLY | O_DIRECT);
1114 else
1115 fd = -1;
1116 mlockall(MCL_FUTURE);
1117
1118 if (odata < ndata)
1119 done = child_grow(fd, sra, stripes,
1120 fdlist, offsets,
1121 odisks, ochunk, array.level, olayout, odata,
1122 d - odisks, fdlist+odisks, offsets+odisks);
1123 else if (odata > ndata)
1124 done = child_shrink(fd, sra, stripes,
1125 fdlist, offsets,
1126 odisks, ochunk, array.level, olayout, odata,
1127 d - odisks, fdlist+odisks, offsets+odisks);
1128 else
1129 done = child_same_size(fd, sra, stripes,
1130 fdlist, offsets,
1131 0,
1132 odisks, ochunk, array.level, olayout, odata,
1133 d - odisks, fdlist+odisks, offsets+odisks);
1134 if (backup_file && done)
1135 unlink(backup_file);
1136 if (level != UnSet && level != array.level) {
1137 /* We need to wait for the reshape to finish
1138 * (which will have happened unless odata < ndata)
1139 * and then set the level
1140 */
1141
1142 c = map_num(pers, level);
1143 if (c == NULL)
1144 exit(0);/* not possible */
1145
1146 if (odata < ndata)
1147 wait_reshape(sra);
1148 err = sysfs_set_str(sra, NULL, "level", c);
1149 if (err)
1150 fprintf(stderr, Name ": %s: could not set level to %s\n",
1151 devname, c);
1152 }
1153 exit(0);
1154 case -1:
1155 fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n",
1156 strerror(errno));
1157 rv = 1;
1158 break;
1159 default:
1160 /* The child will take care of unfreezing the array */
1161 frozen = 0;
1162 break;
1163 }
1164 break;
1165
1166 }
1167
1168 release:
1169 if (rv && orig_level != UnSet && sra) {
1170 c = map_num(pers, orig_level);
1171 if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
1172 fprintf(stderr, Name ": aborting level change\n");
1173 }
1174 if (sra)
1175 unfreeze_array(sra, frozen);
1176 return rv;
1177 }
1178
1179 /*
1180 * We run a child process in the background which performs the following
1181 * steps:
1182 * - wait for resync to reach a certain point
1183 * - suspend io to the following section
1184 * - backup that section
1185 * - allow resync to proceed further
1186 * - resume io
1187 * - discard the backup.
1188 *
1189 * When are combined in slightly different ways in the three cases.
1190 * Grow:
1191 * - suspend/backup/allow/wait/resume/discard
1192 * Shrink:
1193 * - allow/wait/suspend/backup/allow/wait/resume/discard
1194 * same-size:
1195 * - wait/resume/discard/suspend/backup/allow
1196 *
1197 * suspend/backup/allow always come together
1198 * wait/resume/discard do too.
1199 * For the same-size case we have two backups to improve flow.
1200 *
1201 */
1202
1203 int grow_backup(struct mdinfo *sra,
1204 unsigned long long offset, /* per device */
1205 unsigned long stripes, /* per device */
1206 int *sources, unsigned long long *offsets,
1207 int disks, int chunk, int level, int layout,
1208 int dests, int *destfd, unsigned long long *destoffsets,
1209 int part, int *degraded,
1210 char *buf)
1211 {
1212 /* Backup 'blocks' sectors at 'offset' on each device of the array,
1213 * to storage 'destfd' (offset 'destoffsets'), after first
1214 * suspending IO. Then allow resync to continue
1215 * over the suspended section.
1216 * Use part 'part' of the backup-super-block.
1217 */
1218 int odata = disks;
1219 int rv = 0;
1220 int i;
1221 unsigned long long new_degraded;
1222 //printf("offset %llu\n", offset);
1223 if (level >= 4)
1224 odata--;
1225 if (level == 6)
1226 odata--;
1227 sysfs_set_num(sra, NULL, "suspend_hi", (offset + stripes * chunk/512) * odata);
1228 /* Check that array hasn't become degraded, else we might backup the wrong data */
1229 sysfs_get_ll(sra, NULL, "degraded", &new_degraded);
1230 if (new_degraded != *degraded) {
1231 /* check each device to ensure it is still working */
1232 struct mdinfo *sd;
1233 for (sd = sra->devs ; sd ; sd = sd->next) {
1234 if (sd->disk.state & (1<<MD_DISK_FAULTY))
1235 continue;
1236 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
1237 char sbuf[20];
1238 if (sysfs_get_str(sra, sd, "state", sbuf, 20) < 0 ||
1239 strstr(sbuf, "faulty") ||
1240 strstr(sbuf, "in_sync") == NULL) {
1241 /* this device is dead */
1242 sd->disk.state = (1<<MD_DISK_FAULTY);
1243 if (sd->disk.raid_disk >= 0 &&
1244 sources[sd->disk.raid_disk] >= 0) {
1245 close(sources[sd->disk.raid_disk]);
1246 sources[sd->disk.raid_disk] = -1;
1247 }
1248 }
1249 }
1250 }
1251 *degraded = new_degraded;
1252 }
1253 if (part) {
1254 bsb.arraystart2 = __cpu_to_le64(offset * odata);
1255 bsb.length2 = __cpu_to_le64(stripes * chunk/512 * odata);
1256 } else {
1257 bsb.arraystart = __cpu_to_le64(offset * odata);
1258 bsb.length = __cpu_to_le64(stripes * chunk/512 * odata);
1259 }
1260 if (part)
1261 bsb.magic[15] = '2';
1262 for (i = 0; i < dests; i++)
1263 if (part)
1264 lseek64(destfd[i], destoffsets[i] + __le64_to_cpu(bsb.devstart2)*512, 0);
1265 else
1266 lseek64(destfd[i], destoffsets[i], 0);
1267
1268 rv = save_stripes(sources, offsets,
1269 disks, chunk, level, layout,
1270 dests, destfd,
1271 offset*512*odata, stripes * chunk * odata,
1272 buf);
1273
1274 if (rv)
1275 return rv;
1276 bsb.mtime = __cpu_to_le64(time(0));
1277 for (i = 0; i < dests; i++) {
1278 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
1279
1280 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
1281 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
1282 bsb.sb_csum2 = bsb_csum((char*)&bsb,
1283 ((char*)&bsb.sb_csum2)-((char*)&bsb));
1284
1285 lseek64(destfd[i], destoffsets[i] - 4096, 0);
1286 write(destfd[i], &bsb, 512);
1287 fsync(destfd[i]);
1288 }
1289
1290 return 0;
1291 }
1292
1293 /* in 2.6.30, the value reported by sync_completed can be
1294 * less that it should be by one stripe.
1295 * This only happens when reshape hits sync_max and pauses.
1296 * So allow wait_backup to either extent sync_max further
1297 * than strictly necessary, or return before the
1298 * sync has got quite as far as we would really like.
1299 * This is what 'blocks2' is for.
1300 * The various caller give appropriate values so that
1301 * every works.
1302 */
1303 int wait_backup(struct mdinfo *sra,
1304 unsigned long long offset, /* per device */
1305 unsigned long long blocks, /* per device */
1306 unsigned long long blocks2, /* per device - hack */
1307 int dests, int *destfd, unsigned long long *destoffsets,
1308 int part)
1309 {
1310 /* Wait for resync to pass the section that was backed up
1311 * then erase the backup and allow IO
1312 */
1313 int fd = sysfs_get_fd(sra, NULL, "sync_completed");
1314 unsigned long long completed;
1315 int i;
1316
1317 if (fd < 0)
1318 return -1;
1319 sysfs_set_num(sra, NULL, "sync_max", offset + blocks + blocks2);
1320 if (offset == 0)
1321 sysfs_set_str(sra, NULL, "sync_action", "reshape");
1322 do {
1323 char action[20];
1324 fd_set rfds;
1325 FD_ZERO(&rfds);
1326 FD_SET(fd, &rfds);
1327 select(fd+1, NULL, NULL, &rfds, NULL);
1328 if (sysfs_fd_get_ll(fd, &completed) < 0) {
1329 close(fd);
1330 return -1;
1331 }
1332 if (sysfs_get_str(sra, NULL, "sync_action",
1333 action, 20) > 0 &&
1334 strncmp(action, "reshape", 7) != 0)
1335 break;
1336 } while (completed < offset + blocks);
1337 close(fd);
1338
1339 if (part) {
1340 bsb.arraystart2 = __cpu_to_le64(0);
1341 bsb.length2 = __cpu_to_le64(0);
1342 } else {
1343 bsb.arraystart = __cpu_to_le64(0);
1344 bsb.length = __cpu_to_le64(0);
1345 }
1346 bsb.mtime = __cpu_to_le64(time(0));
1347 for (i = 0; i < dests; i++) {
1348 bsb.devstart = __cpu_to_le64(destoffsets[i]/512);
1349 bsb.sb_csum = bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb));
1350 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0)
1351 bsb.sb_csum2 = bsb_csum((char*)&bsb,
1352 ((char*)&bsb.sb_csum2)-((char*)&bsb));
1353 lseek64(destfd[i], destoffsets[i]-4096, 0);
1354 write(destfd[i], &bsb, 512);
1355 fsync(destfd[i]);
1356 }
1357 return 0;
1358 }
1359
1360 static void fail(char *msg)
1361 {
1362 write(2, msg, strlen(msg));
1363 write(2, "\n", 1);
1364 exit(1);
1365 }
1366
1367 static char *abuf, *bbuf;
1368 static int abuflen;
1369 static void validate(int afd, int bfd, unsigned long long offset)
1370 {
1371 /* check that the data in the backup against the array.
1372 * This is only used for regression testing and should not
1373 * be used while the array is active
1374 */
1375 if (afd < 0)
1376 return;
1377 lseek64(bfd, offset - 4096, 0);
1378 if (read(bfd, &bsb2, 512) != 512)
1379 fail("cannot read bsb");
1380 if (bsb2.sb_csum != bsb_csum((char*)&bsb2,
1381 ((char*)&bsb2.sb_csum)-((char*)&bsb2)))
1382 fail("first csum bad");
1383 if (memcmp(bsb2.magic, "md_backup_data", 14) != 0)
1384 fail("magic is bad");
1385 if (memcmp(bsb2.magic, "md_backup_data-2", 16) == 0 &&
1386 bsb2.sb_csum2 != bsb_csum((char*)&bsb2,
1387 ((char*)&bsb2.sb_csum2)-((char*)&bsb2)))
1388 fail("second csum bad");
1389
1390 if (__le64_to_cpu(bsb2.devstart)*512 != offset)
1391 fail("devstart is wrong");
1392
1393 if (bsb2.length) {
1394 unsigned long long len = __le64_to_cpu(bsb2.length)*512;
1395
1396 if (abuflen < len) {
1397 free(abuf);
1398 free(bbuf);
1399 abuflen = len;
1400 posix_memalign((void**)&abuf, 4096, abuflen);
1401 posix_memalign((void**)&bbuf, 4096, abuflen);
1402 }
1403
1404 lseek64(bfd, offset, 0);
1405 if (read(bfd, bbuf, len) != len) {
1406 printf("len %llu\n", len);
1407 fail("read first backup failed");
1408 }
1409 lseek64(afd, __le64_to_cpu(bsb2.arraystart)*512, 0);
1410 if (read(afd, abuf, len) != len)
1411 fail("read first from array failed");
1412 if (memcmp(bbuf, abuf, len) != 0) {
1413 int i;
1414 printf("offset=%llu len=%llu\n",
1415 __le64_to_cpu(bsb2.arraystart)*512, len);
1416 for (i=0; i<len; i++)
1417 if (bbuf[i] != abuf[i]) {
1418 printf("first diff byte %d\n", i);
1419 break;
1420 }
1421 fail("data1 compare failed");
1422 }
1423 }
1424 if (bsb2.length2) {
1425 unsigned long long len = __le64_to_cpu(bsb2.length2)*512;
1426
1427 if (abuflen < len) {
1428 free(abuf);
1429 free(bbuf);
1430 abuflen = len;
1431 abuf = malloc(abuflen);
1432 bbuf = malloc(abuflen);
1433 }
1434
1435 lseek64(bfd, offset+__le64_to_cpu(bsb2.devstart2)*512, 0);
1436 if (read(bfd, bbuf, len) != len)
1437 fail("read second backup failed");
1438 lseek64(afd, __le64_to_cpu(bsb2.arraystart2)*512, 0);
1439 if (read(afd, abuf, len) != len)
1440 fail("read second from array failed");
1441 if (memcmp(bbuf, abuf, len) != 0)
1442 fail("data2 compare failed");
1443 }
1444 }
1445
1446 static int child_grow(int afd, struct mdinfo *sra, unsigned long stripes,
1447 int *fds, unsigned long long *offsets,
1448 int disks, int chunk, int level, int layout, int data,
1449 int dests, int *destfd, unsigned long long *destoffsets)
1450 {
1451 char *buf;
1452 int degraded = 0;
1453
1454 posix_memalign((void**)&buf, 4096, disks * chunk);
1455 sysfs_set_num(sra, NULL, "suspend_hi", 0);
1456 sysfs_set_num(sra, NULL, "suspend_lo", 0);
1457 grow_backup(sra, 0, stripes,
1458 fds, offsets, disks, chunk, level, layout,
1459 dests, destfd, destoffsets,
1460 0, &degraded, buf);
1461 validate(afd, destfd[0], destoffsets[0]);
1462 wait_backup(sra, 0, stripes * chunk / 512, stripes * chunk / 512,
1463 dests, destfd, destoffsets,
1464 0);
1465 sysfs_set_num(sra, NULL, "suspend_lo", (stripes * chunk/512) * data);
1466 free(buf);
1467 /* FIXME this should probably be numeric */
1468 sysfs_set_str(sra, NULL, "sync_max", "max");
1469 return 1;
1470 }
1471
1472 static int child_shrink(int afd, struct mdinfo *sra, unsigned long stripes,
1473 int *fds, unsigned long long *offsets,
1474 int disks, int chunk, int level, int layout, int data,
1475 int dests, int *destfd, unsigned long long *destoffsets)
1476 {
1477 char *buf;
1478 unsigned long long start;
1479 int rv;
1480 int degraded = 0;
1481
1482 posix_memalign((void**)&buf, 4096, disks * chunk);
1483 start = sra->component_size - stripes * chunk/512;
1484 sysfs_set_num(sra, NULL, "sync_max", start);
1485 sysfs_set_str(sra, NULL, "sync_action", "reshape");
1486 sysfs_set_num(sra, NULL, "suspend_lo", 0);
1487 sysfs_set_num(sra, NULL, "suspend_hi", 0);
1488 rv = wait_backup(sra, 0, start - stripes * chunk/512, stripes * chunk/512,
1489 dests, destfd, destoffsets, 0);
1490 if (rv < 0)
1491 return 0;
1492 grow_backup(sra, 0, stripes,
1493 fds, offsets,
1494 disks, chunk, level, layout,
1495 dests, destfd, destoffsets,
1496 0, &degraded, buf);
1497 validate(afd, destfd[0], destoffsets[0]);
1498 wait_backup(sra, start, stripes*chunk/512, 0,
1499 dests, destfd, destoffsets, 0);
1500 sysfs_set_num(sra, NULL, "suspend_lo", (stripes * chunk/512) * data);
1501 free(buf);
1502 /* FIXME this should probably be numeric */
1503 sysfs_set_str(sra, NULL, "sync_max", "max");
1504 return 1;
1505 }
1506
1507 static int child_same_size(int afd, struct mdinfo *sra, unsigned long stripes,
1508 int *fds, unsigned long long *offsets,
1509 unsigned long long start,
1510 int disks, int chunk, int level, int layout, int data,
1511 int dests, int *destfd, unsigned long long *destoffsets)
1512 {
1513 unsigned long long size;
1514 unsigned long tailstripes = stripes;
1515 int part;
1516 char *buf;
1517 unsigned long long speed;
1518 int degraded = 0;
1519
1520
1521 posix_memalign((void**)&buf, 4096, disks * chunk);
1522
1523 sysfs_set_num(sra, NULL, "suspend_lo", 0);
1524 sysfs_set_num(sra, NULL, "suspend_hi", 0);
1525
1526 sysfs_get_ll(sra, NULL, "sync_speed_min", &speed);
1527 sysfs_set_num(sra, NULL, "sync_speed_min", 200000);
1528
1529 grow_backup(sra, start, stripes,
1530 fds, offsets,
1531 disks, chunk, level, layout,
1532 dests, destfd, destoffsets,
1533 0, &degraded, buf);
1534 grow_backup(sra, (start + stripes) * chunk/512, stripes,
1535 fds, offsets,
1536 disks, chunk, level, layout,
1537 dests, destfd, destoffsets,
1538 1, &degraded, buf);
1539 validate(afd, destfd[0], destoffsets[0]);
1540 part = 0;
1541 start += stripes * 2; /* where to read next */
1542 size = sra->component_size / (chunk/512);
1543 while (start < size) {
1544 if (wait_backup(sra, (start-stripes*2)*chunk/512,
1545 stripes*chunk/512, 0,
1546 dests, destfd, destoffsets,
1547 part) < 0)
1548 return 0;
1549 sysfs_set_num(sra, NULL, "suspend_lo", start*chunk/512 * data);
1550 if (start + stripes > size)
1551 tailstripes = (size - start);
1552
1553 grow_backup(sra, start*chunk/512, tailstripes,
1554 fds, offsets,
1555 disks, chunk, level, layout,
1556 dests, destfd, destoffsets,
1557 part, &degraded, buf);
1558 start += stripes;
1559 part = 1 - part;
1560 validate(afd, destfd[0], destoffsets[0]);
1561 }
1562 if (wait_backup(sra, (start-stripes*2) * chunk/512, stripes * chunk/512, 0,
1563 dests, destfd, destoffsets,
1564 part) < 0)
1565 return 0;
1566 sysfs_set_num(sra, NULL, "suspend_lo", ((start-stripes)*chunk/512) * data);
1567 wait_backup(sra, (start-stripes) * chunk/512, tailstripes * chunk/512, 0,
1568 dests, destfd, destoffsets,
1569 1-part);
1570 sysfs_set_num(sra, NULL, "suspend_lo", (size*chunk/512) * data);
1571 sysfs_set_num(sra, NULL, "sync_speed_min", speed);
1572 free(buf);
1573 return 1;
1574 }
1575
1576 /*
1577 * If any spare contains md_back_data-1 which is recent wrt mtime,
1578 * write that data into the array and update the super blocks with
1579 * the new reshape_progress
1580 */
1581 int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt,
1582 char *backup_file, int verbose)
1583 {
1584 int i, j;
1585 int old_disks;
1586 unsigned long long *offsets;
1587 unsigned long long nstripe, ostripe;
1588 int ndata, odata;
1589
1590 if (info->new_level != info->array.level)
1591 return 1; /* Cannot handle level changes (they are instantaneous) */
1592
1593 odata = info->array.raid_disks - info->delta_disks - 1;
1594 if (info->array.level == 6) odata--; /* number of data disks */
1595 ndata = info->array.raid_disks - 1;
1596 if (info->new_level == 6) ndata--;
1597
1598 old_disks = info->array.raid_disks - info->delta_disks;
1599
1600 if (info->delta_disks <= 0)
1601 /* Didn't grow, so the backup file must have
1602 * been used
1603 */
1604 old_disks = cnt;
1605 for (i=old_disks-(backup_file?1:0); i<cnt; i++) {
1606 struct mdinfo dinfo;
1607 char buf[4096];
1608 int fd;
1609 int bsbsize;
1610 char *devname, namebuf[20];
1611
1612 /* This was a spare and may have some saved data on it.
1613 * Load the superblock, find and load the
1614 * backup_super_block.
1615 * If either fail, go on to next device.
1616 * If the backup contains no new info, just return
1617 * else restore data and update all superblocks
1618 */
1619 if (i == old_disks-1) {
1620 fd = open(backup_file, O_RDONLY);
1621 if (fd<0) {
1622 fprintf(stderr, Name ": backup file %s inaccessible: %s\n",
1623 backup_file, strerror(errno));
1624 continue;
1625 }
1626 devname = backup_file;
1627 } else {
1628 fd = fdlist[i];
1629 if (fd < 0)
1630 continue;
1631 if (st->ss->load_super(st, fd, NULL))
1632 continue;
1633
1634 st->ss->getinfo_super(st, &dinfo);
1635 st->ss->free_super(st);
1636
1637 if (lseek64(fd,
1638 (dinfo.data_offset + dinfo.component_size - 8) <<9,
1639 0) < 0) {
1640 fprintf(stderr, Name ": Cannot seek on device %d\n", i);
1641 continue; /* Cannot seek */
1642 }
1643 sprintf(namebuf, "device-%d", i);
1644 devname = namebuf;
1645 }
1646 if (read(fd, &bsb, sizeof(bsb)) != sizeof(bsb)) {
1647 if (verbose)
1648 fprintf(stderr, Name ": Cannot read from %s\n", devname);
1649 continue; /* Cannot read */
1650 }
1651 if (memcmp(bsb.magic, "md_backup_data-1", 16) != 0 &&
1652 memcmp(bsb.magic, "md_backup_data-2", 16) != 0) {
1653 if (verbose)
1654 fprintf(stderr, Name ": No backup metadata on %s\n", devname);
1655 continue;
1656 }
1657 if (bsb.sb_csum != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum)-((char*)&bsb))) {
1658 if (verbose)
1659 fprintf(stderr, Name ": Bad backup-metadata checksum on %s\n", devname);
1660 continue; /* bad checksum */
1661 }
1662 if (memcmp(bsb.magic, "md_backup_data-2", 16) == 0 &&
1663 bsb.sb_csum2 != bsb_csum((char*)&bsb, ((char*)&bsb.sb_csum2)-((char*)&bsb))) {
1664 if (verbose)
1665 fprintf(stderr, Name ": Bad backup-metadata checksum2 on %s\n", devname);
1666 continue; /* Bad second checksum */
1667 }
1668 if (memcmp(bsb.set_uuid,info->uuid, 16) != 0) {
1669 if (verbose)
1670 fprintf(stderr, Name ": Wrong uuid on backup-metadata on %s\n", devname);
1671 continue; /* Wrong uuid */
1672 }
1673
1674 if (info->array.utime > __le64_to_cpu(bsb.mtime) + 10*60 ||
1675 info->array.utime < __le64_to_cpu(bsb.mtime) - 10*60) {
1676 if (verbose)
1677 fprintf(stderr, Name ": too-old timestamp on backup-metadata on %s\n", devname);
1678 continue; /* time stamp is too bad */
1679 }
1680
1681 if (bsb.magic[15] == '1') {
1682 if (info->delta_disks >= 0) {
1683 /* reshape_progress is increasing */
1684 if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) <
1685 info->reshape_progress) {
1686 nonew:
1687 if (verbose)
1688 fprintf(stderr, Name ": backup-metadata found on %s but is not needed\n", devname);
1689 continue; /* No new data here */
1690 }
1691 } else {
1692 /* reshape_progress is decreasing */
1693 if (__le64_to_cpu(bsb.arraystart) >=
1694 info->reshape_progress)
1695 goto nonew; /* No new data here */
1696 }
1697 } else {
1698 if (info->delta_disks >= 0) {
1699 /* reshape_progress is increasing */
1700 if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) <
1701 info->reshape_progress &&
1702 __le64_to_cpu(bsb.arraystart2) + __le64_to_cpu(bsb.length2) <
1703 info->reshape_progress)
1704 goto nonew; /* No new data here */
1705 } else {
1706 /* reshape_progress is decreasing */
1707 if (__le64_to_cpu(bsb.arraystart) >=
1708 info->reshape_progress &&
1709 __le64_to_cpu(bsb.arraystart2) >=
1710 info->reshape_progress)
1711 goto nonew; /* No new data here */
1712 }
1713 }
1714 if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) {
1715 second_fail:
1716 if (verbose)
1717 fprintf(stderr, Name ": Failed to verify secondary backup-metadata block on %s\n",
1718 devname);
1719 continue; /* Cannot seek */
1720 }
1721 /* There should be a duplicate backup superblock 4k before here */
1722 if (lseek64(fd, -4096, 1) < 0 ||
1723 read(fd, buf, 4096) != 4096)
1724 goto second_fail; /* Cannot find leading superblock */
1725 if (bsb.magic[15] == '1')
1726 bsbsize = offsetof(struct mdp_backup_super, pad1);
1727 else
1728 bsbsize = offsetof(struct mdp_backup_super, pad);
1729 if (memcmp(buf, &bsb, bsbsize) != 0)
1730 goto second_fail; /* Cannot find leading superblock */
1731
1732 /* Now need the data offsets for all devices. */
1733 offsets = malloc(sizeof(*offsets)*info->array.raid_disks);
1734 for(j=0; j<info->array.raid_disks; j++) {
1735 if (fdlist[j] < 0)
1736 continue;
1737 if (st->ss->load_super(st, fdlist[j], NULL))
1738 /* FIXME should be this be an error */
1739 continue;
1740 st->ss->getinfo_super(st, &dinfo);
1741 st->ss->free_super(st);
1742 offsets[j] = dinfo.data_offset;
1743 }
1744 printf(Name ": restoring critical section\n");
1745
1746 if (restore_stripes(fdlist, offsets,
1747 info->array.raid_disks,
1748 info->new_chunk,
1749 info->new_level,
1750 info->new_layout,
1751 fd, __le64_to_cpu(bsb.devstart)*512,
1752 __le64_to_cpu(bsb.arraystart)*512,
1753 __le64_to_cpu(bsb.length)*512)) {
1754 /* didn't succeed, so giveup */
1755 if (verbose)
1756 fprintf(stderr, Name ": Error restoring backup from %s\n",
1757 devname);
1758 return 1;
1759 }
1760
1761 if (bsb.magic[15] == '2' &&
1762 restore_stripes(fdlist, offsets,
1763 info->array.raid_disks,
1764 info->new_chunk,
1765 info->new_level,
1766 info->new_layout,
1767 fd, __le64_to_cpu(bsb.devstart)*512 +
1768 __le64_to_cpu(bsb.devstart2)*512,
1769 __le64_to_cpu(bsb.arraystart2)*512,
1770 __le64_to_cpu(bsb.length2)*512)) {
1771 /* didn't succeed, so giveup */
1772 if (verbose)
1773 fprintf(stderr, Name ": Error restoring second backup from %s\n",
1774 devname);
1775 return 1;
1776 }
1777
1778
1779 /* Ok, so the data is restored. Let's update those superblocks. */
1780
1781 if (info->delta_disks >= 0) {
1782 info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
1783 __le64_to_cpu(bsb.length);
1784 if (bsb.magic[15] == '2') {
1785 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2) +
1786 __le64_to_cpu(bsb.length2);
1787 if (p2 > info->reshape_progress)
1788 info->reshape_progress = p2;
1789 }
1790 } else {
1791 info->reshape_progress = __le64_to_cpu(bsb.arraystart);
1792 if (bsb.magic[15] == '2') {
1793 unsigned long long p2 = __le64_to_cpu(bsb.arraystart2);
1794 if (p2 < info->reshape_progress)
1795 info->reshape_progress = p2;
1796 }
1797 }
1798 for (j=0; j<info->array.raid_disks; j++) {
1799 if (fdlist[j] < 0) continue;
1800 if (st->ss->load_super(st, fdlist[j], NULL))
1801 continue;
1802 st->ss->getinfo_super(st, &dinfo);
1803 dinfo.reshape_progress = info->reshape_progress;
1804 st->ss->update_super(st, &dinfo,
1805 "_reshape_progress",
1806 NULL,0, 0, NULL);
1807 st->ss->store_super(st, fdlist[j]);
1808 st->ss->free_super(st);
1809 }
1810 return 0;
1811 }
1812 /* Didn't find any backup data, try to see if any
1813 * was needed.
1814 */
1815 if (info->delta_disks < 0) {
1816 /* When shrinking, the critical section is at the end.
1817 * So see if we are before the critical section.
1818 */
1819 unsigned long long first_block;
1820 nstripe = ostripe = 0;
1821 first_block = 0;
1822 while (ostripe >= nstripe) {
1823 ostripe += info->array.chunk_size / 512;
1824 first_block = ostripe * odata;
1825 nstripe = first_block / ndata / (info->new_chunk/512) *
1826 (info->new_chunk/512);
1827 }
1828
1829 if (info->reshape_progress >= first_block)
1830 return 0;
1831 }
1832 if (info->delta_disks > 0) {
1833 /* See if we are beyond the critical section. */
1834 unsigned long long last_block;
1835 nstripe = ostripe = 0;
1836 last_block = 0;
1837 while (nstripe >= ostripe) {
1838 nstripe += info->new_chunk / 512;
1839 last_block = nstripe * ndata;
1840 ostripe = last_block / odata / (info->array.chunk_size/512) *
1841 (info->array.chunk_size/512);
1842 }
1843
1844 if (info->reshape_progress >= last_block)
1845 return 0;
1846 }
1847 /* needed to recover critical section! */
1848 if (verbose)
1849 fprintf(stderr, Name ": Failed to find backup of critical section\n");
1850 return 1;
1851 }
1852
1853 int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
1854 char *backup_file)
1855 {
1856 /* Array is assembled and ready to be started, but
1857 * monitoring is probably required.
1858 * So:
1859 * - start read-only
1860 * - set upper bound for resync
1861 * - initialise the 'suspend' boundaries
1862 * - switch to read-write
1863 * - fork and continue monitoring
1864 */
1865 int err;
1866 int backup_list[1];
1867 unsigned long long backup_offsets[1];
1868 int odisks, ndisks, ochunk, nchunk,odata,ndata;
1869 unsigned long a,b,blocks,stripes;
1870 int backup_fd;
1871 int *fds;
1872 unsigned long long *offsets;
1873 int d;
1874 struct mdinfo *sra, *sd;
1875 int rv;
1876 int done = 0;
1877
1878 err = sysfs_set_str(info, NULL, "array_state", "readonly");
1879 if (err)
1880 return err;
1881
1882 /* make sure reshape doesn't progress until we are ready */
1883 sysfs_set_str(info, NULL, "sync_max", "0");
1884 sysfs_set_str(info, NULL, "array_state", "active"); /* FIXME or clean */
1885
1886 /* ndisks is not growing, so raid_disks is old and +delta is new */
1887 odisks = info->array.raid_disks;
1888 ndisks = odisks + info->delta_disks;
1889 odata = odisks - 1;
1890 ndata = ndisks - 1;
1891 if (info->array.level == 6) {
1892 odata--;
1893 ndata--;
1894 }
1895 ochunk = info->array.chunk_size;
1896 nchunk = info->new_chunk;
1897
1898
1899 a = ochunk/512 * odata;
1900 b = nchunk/512 * ndata;
1901 /* Find GCD */
1902 while (a != b) {
1903 if (a < b)
1904 b -= a;
1905 if (b < a)
1906 a -= b;
1907 }
1908 /* LCM == product / GCD */
1909 blocks = ochunk/512 * nchunk/512 * odata * ndata / a;
1910
1911 if (ndata == odata)
1912 blocks *= 16;
1913 stripes = blocks / (info->array.chunk_size/512) / odata;
1914
1915
1916 memset(&bsb, 0, 512);
1917 memcpy(bsb.magic, "md_backup_data-1", 16);
1918 memcpy(&bsb.set_uuid, info->uuid, 16);
1919 bsb.mtime = __cpu_to_le64(time(0));
1920 bsb.devstart2 = blocks;
1921
1922 backup_fd = open(backup_file, O_RDWR|O_CREAT, S_IRUSR | S_IWUSR);
1923 backup_list[0] = backup_fd;
1924 backup_offsets[0] = 8 * 512;
1925 fds = malloc(odisks * sizeof(fds[0]));
1926 offsets = malloc(odisks * sizeof(offsets[0]));
1927 for (d=0; d<odisks; d++)
1928 fds[d] = -1;
1929
1930 sra = sysfs_read(-1, devname2devnum(info->sys_name),
1931 GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|
1932 GET_CACHE);
1933
1934 for (sd = sra->devs; sd; sd = sd->next) {
1935 if (sd->disk.state & (1<<MD_DISK_FAULTY))
1936 continue;
1937 if (sd->disk.state & (1<<MD_DISK_SYNC)) {
1938 char *dn = map_dev(sd->disk.major,
1939 sd->disk.minor, 1);
1940 fds[sd->disk.raid_disk]
1941 = dev_open(dn, O_RDONLY);
1942 offsets[sd->disk.raid_disk] = sd->data_offset*512;
1943 if (fds[sd->disk.raid_disk] < 0) {
1944 fprintf(stderr, Name ": %s: cannot open component %s\n",
1945 info->sys_name, dn?dn:"-unknown-");
1946 rv = 1;
1947 goto release;
1948 }
1949 free(dn);
1950 }
1951 }
1952
1953 switch(fork()) {
1954 case 0:
1955 close(mdfd);
1956 mlockall(MCL_FUTURE);
1957 if (info->delta_disks < 0)
1958 done = child_shrink(-1, info, stripes,
1959 fds, offsets,
1960 info->array.raid_disks,
1961 info->array.chunk_size,
1962 info->array.level, info->array.layout,
1963 odata,
1964 1, backup_list, backup_offsets);
1965 else if (info->delta_disks == 0) {
1966 /* The 'start' is a per-device stripe number.
1967 * reshape_progress is a per-array sector number.
1968 * So divide by ndata * chunk_size
1969 */
1970 unsigned long long start = info->reshape_progress / ndata;
1971 start /= (info->array.chunk_size/512);
1972 done = child_same_size(-1, info, stripes,
1973 fds, offsets,
1974 start,
1975 info->array.raid_disks,
1976 info->array.chunk_size,
1977 info->array.level, info->array.layout,
1978 odata,
1979 1, backup_list, backup_offsets);
1980 }
1981 if (backup_file && done)
1982 unlink(backup_file);
1983 /* FIXME should I intuit a level change */
1984 exit(0);
1985 case -1:
1986 fprintf(stderr, Name ": Cannot run child to continue monitoring reshape: %s\n",
1987 strerror(errno));
1988 return 1;
1989 default:
1990 break;
1991 }
1992 release:
1993 return 0;
1994 }
1995
1996