btrfs-progs: remove NULL-ptr judge before free for btrfs-progs
[platform/upstream/btrfs-progs.git] / volumes.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18 #define _XOPEN_SOURCE 600
19 #define __USE_XOPEN2K
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <uuid/uuid.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include "ctree.h"
28 #include "disk-io.h"
29 #include "transaction.h"
30 #include "print-tree.h"
31 #include "volumes.h"
32 #include "math.h"
33
34 struct stripe {
35         struct btrfs_device *dev;
36         u64 physical;
37 };
38
39 static inline int nr_parity_stripes(struct map_lookup *map)
40 {
41         if (map->type & BTRFS_BLOCK_GROUP_RAID5)
42                 return 1;
43         else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
44                 return 2;
45         else
46                 return 0;
47 }
48
49 static inline int nr_data_stripes(struct map_lookup *map)
50 {
51         return map->num_stripes - nr_parity_stripes(map);
52 }
53
54 #define is_parity_stripe(x) ( ((x) == BTRFS_RAID5_P_STRIPE) || ((x) == BTRFS_RAID6_Q_STRIPE) )
55
56 static LIST_HEAD(fs_uuids);
57
58 static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
59                                           u8 *uuid)
60 {
61         struct btrfs_device *dev;
62         struct list_head *cur;
63
64         list_for_each(cur, head) {
65                 dev = list_entry(cur, struct btrfs_device, dev_list);
66                 if (dev->devid == devid &&
67                     !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE)) {
68                         return dev;
69                 }
70         }
71         return NULL;
72 }
73
74 static struct btrfs_fs_devices *find_fsid(u8 *fsid)
75 {
76         struct list_head *cur;
77         struct btrfs_fs_devices *fs_devices;
78
79         list_for_each(cur, &fs_uuids) {
80                 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
81                 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
82                         return fs_devices;
83         }
84         return NULL;
85 }
86
87 static int device_list_add(const char *path,
88                            struct btrfs_super_block *disk_super,
89                            u64 devid, struct btrfs_fs_devices **fs_devices_ret)
90 {
91         struct btrfs_device *device;
92         struct btrfs_fs_devices *fs_devices;
93         u64 found_transid = btrfs_super_generation(disk_super);
94
95         fs_devices = find_fsid(disk_super->fsid);
96         if (!fs_devices) {
97                 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
98                 if (!fs_devices)
99                         return -ENOMEM;
100                 INIT_LIST_HEAD(&fs_devices->devices);
101                 list_add(&fs_devices->list, &fs_uuids);
102                 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
103                 fs_devices->latest_devid = devid;
104                 fs_devices->latest_trans = found_transid;
105                 fs_devices->lowest_devid = (u64)-1;
106                 device = NULL;
107         } else {
108                 device = __find_device(&fs_devices->devices, devid,
109                                        disk_super->dev_item.uuid);
110         }
111         if (!device) {
112                 device = kzalloc(sizeof(*device), GFP_NOFS);
113                 if (!device) {
114                         /* we can safely leave the fs_devices entry around */
115                         return -ENOMEM;
116                 }
117                 device->fd = -1;
118                 device->devid = devid;
119                 memcpy(device->uuid, disk_super->dev_item.uuid,
120                        BTRFS_UUID_SIZE);
121                 device->name = kstrdup(path, GFP_NOFS);
122                 if (!device->name) {
123                         kfree(device);
124                         return -ENOMEM;
125                 }
126                 device->label = kstrdup(disk_super->label, GFP_NOFS);
127                 if (!device->label) {
128                         kfree(device->name);
129                         kfree(device);
130                         return -ENOMEM;
131                 }
132                 device->total_devs = btrfs_super_num_devices(disk_super);
133                 device->super_bytes_used = btrfs_super_bytes_used(disk_super);
134                 device->total_bytes =
135                         btrfs_stack_device_total_bytes(&disk_super->dev_item);
136                 device->bytes_used =
137                         btrfs_stack_device_bytes_used(&disk_super->dev_item);
138                 list_add(&device->dev_list, &fs_devices->devices);
139                 device->fs_devices = fs_devices;
140         } else if (!device->name || strcmp(device->name, path)) {
141                 char *name = strdup(path);
142                 if (!name)
143                         return -ENOMEM;
144                 kfree(device->name);
145                 device->name = name;
146         }
147
148
149         if (found_transid > fs_devices->latest_trans) {
150                 fs_devices->latest_devid = devid;
151                 fs_devices->latest_trans = found_transid;
152         }
153         if (fs_devices->lowest_devid > devid) {
154                 fs_devices->lowest_devid = devid;
155         }
156         *fs_devices_ret = fs_devices;
157         return 0;
158 }
159
160 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
161 {
162         struct btrfs_fs_devices *seed_devices;
163         struct list_head *cur;
164         struct btrfs_device *device;
165 again:
166         list_for_each(cur, &fs_devices->devices) {
167                 device = list_entry(cur, struct btrfs_device, dev_list);
168                 if (device->fd != -1) {
169                         fsync(device->fd);
170                         if (posix_fadvise(device->fd, 0, 0, POSIX_FADV_DONTNEED))
171                                 fprintf(stderr, "Warning, could not drop caches\n");
172                         close(device->fd);
173                         device->fd = -1;
174                 }
175                 device->writeable = 0;
176         }
177
178         seed_devices = fs_devices->seed;
179         fs_devices->seed = NULL;
180         if (seed_devices) {
181                 fs_devices = seed_devices;
182                 goto again;
183         }
184
185         return 0;
186 }
187
188 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, int flags)
189 {
190         int fd;
191         struct list_head *head = &fs_devices->devices;
192         struct list_head *cur;
193         struct btrfs_device *device;
194         int ret;
195
196         list_for_each(cur, head) {
197                 device = list_entry(cur, struct btrfs_device, dev_list);
198                 if (!device->name) {
199                         printk("no name for device %llu, skip it now\n", device->devid);
200                         continue;
201                 }
202
203                 fd = open(device->name, flags);
204                 if (fd < 0) {
205                         ret = -errno;
206                         goto fail;
207                 }
208
209                 if (posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED))
210                         fprintf(stderr, "Warning, could not drop caches\n");
211
212                 if (device->devid == fs_devices->latest_devid)
213                         fs_devices->latest_bdev = fd;
214                 if (device->devid == fs_devices->lowest_devid)
215                         fs_devices->lowest_bdev = fd;
216                 device->fd = fd;
217                 if (flags == O_RDWR)
218                         device->writeable = 1;
219         }
220         return 0;
221 fail:
222         btrfs_close_devices(fs_devices);
223         return ret;
224 }
225
226 int btrfs_scan_one_device(int fd, const char *path,
227                           struct btrfs_fs_devices **fs_devices_ret,
228                           u64 *total_devs, u64 super_offset)
229 {
230         struct btrfs_super_block *disk_super;
231         char *buf;
232         int ret;
233         u64 devid;
234
235         buf = malloc(4096);
236         if (!buf) {
237                 ret = -ENOMEM;
238                 goto error;
239         }
240         disk_super = (struct btrfs_super_block *)buf;
241         ret = btrfs_read_dev_super(fd, disk_super, super_offset);
242         if (ret < 0) {
243                 ret = -EIO;
244                 goto error_brelse;
245         }
246         devid = btrfs_stack_device_id(&disk_super->dev_item);
247         if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)
248                 *total_devs = 1;
249         else
250                 *total_devs = btrfs_super_num_devices(disk_super);
251
252         ret = device_list_add(path, disk_super, devid, fs_devices_ret);
253
254 error_brelse:
255         free(buf);
256 error:
257         return ret;
258 }
259
260 /*
261  * this uses a pretty simple search, the expectation is that it is
262  * called very infrequently and that a given device has a small number
263  * of extents
264  */
265 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
266                                 struct btrfs_device *device,
267                                 struct btrfs_path *path,
268                                 u64 num_bytes, u64 *start)
269 {
270         struct btrfs_key key;
271         struct btrfs_root *root = device->dev_root;
272         struct btrfs_dev_extent *dev_extent = NULL;
273         u64 hole_size = 0;
274         u64 last_byte = 0;
275         u64 search_start = root->fs_info->alloc_start;
276         u64 search_end = device->total_bytes;
277         int ret;
278         int slot = 0;
279         int start_found;
280         struct extent_buffer *l;
281
282         start_found = 0;
283         path->reada = 2;
284
285         /* FIXME use last free of some kind */
286
287         /* we don't want to overwrite the superblock on the drive,
288          * so we make sure to start at an offset of at least 1MB
289          */
290         search_start = max(BTRFS_BLOCK_RESERVED_1M_FOR_SUPER, search_start);
291
292         if (search_start >= search_end) {
293                 ret = -ENOSPC;
294                 goto error;
295         }
296
297         key.objectid = device->devid;
298         key.offset = search_start;
299         key.type = BTRFS_DEV_EXTENT_KEY;
300         ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
301         if (ret < 0)
302                 goto error;
303         ret = btrfs_previous_item(root, path, 0, key.type);
304         if (ret < 0)
305                 goto error;
306         l = path->nodes[0];
307         btrfs_item_key_to_cpu(l, &key, path->slots[0]);
308         while (1) {
309                 l = path->nodes[0];
310                 slot = path->slots[0];
311                 if (slot >= btrfs_header_nritems(l)) {
312                         ret = btrfs_next_leaf(root, path);
313                         if (ret == 0)
314                                 continue;
315                         if (ret < 0)
316                                 goto error;
317 no_more_items:
318                         if (!start_found) {
319                                 if (search_start >= search_end) {
320                                         ret = -ENOSPC;
321                                         goto error;
322                                 }
323                                 *start = search_start;
324                                 start_found = 1;
325                                 goto check_pending;
326                         }
327                         *start = last_byte > search_start ?
328                                 last_byte : search_start;
329                         if (search_end <= *start) {
330                                 ret = -ENOSPC;
331                                 goto error;
332                         }
333                         goto check_pending;
334                 }
335                 btrfs_item_key_to_cpu(l, &key, slot);
336
337                 if (key.objectid < device->devid)
338                         goto next;
339
340                 if (key.objectid > device->devid)
341                         goto no_more_items;
342
343                 if (key.offset >= search_start && key.offset > last_byte &&
344                     start_found) {
345                         if (last_byte < search_start)
346                                 last_byte = search_start;
347                         hole_size = key.offset - last_byte;
348                         if (key.offset > last_byte &&
349                             hole_size >= num_bytes) {
350                                 *start = last_byte;
351                                 goto check_pending;
352                         }
353                 }
354                 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
355                         goto next;
356                 }
357
358                 start_found = 1;
359                 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
360                 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
361 next:
362                 path->slots[0]++;
363                 cond_resched();
364         }
365 check_pending:
366         /* we have to make sure we didn't find an extent that has already
367          * been allocated by the map tree or the original allocation
368          */
369         btrfs_release_path(path);
370         BUG_ON(*start < search_start);
371
372         if (*start + num_bytes > search_end) {
373                 ret = -ENOSPC;
374                 goto error;
375         }
376         /* check for pending inserts here */
377         return 0;
378
379 error:
380         btrfs_release_path(path);
381         return ret;
382 }
383
384 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
385                                   struct btrfs_device *device,
386                                   u64 chunk_tree, u64 chunk_objectid,
387                                   u64 chunk_offset,
388                                   u64 num_bytes, u64 *start)
389 {
390         int ret;
391         struct btrfs_path *path;
392         struct btrfs_root *root = device->dev_root;
393         struct btrfs_dev_extent *extent;
394         struct extent_buffer *leaf;
395         struct btrfs_key key;
396
397         path = btrfs_alloc_path();
398         if (!path)
399                 return -ENOMEM;
400
401         ret = find_free_dev_extent(trans, device, path, num_bytes, start);
402         if (ret) {
403                 goto err;
404         }
405
406         key.objectid = device->devid;
407         key.offset = *start;
408         key.type = BTRFS_DEV_EXTENT_KEY;
409         ret = btrfs_insert_empty_item(trans, root, path, &key,
410                                       sizeof(*extent));
411         BUG_ON(ret);
412
413         leaf = path->nodes[0];
414         extent = btrfs_item_ptr(leaf, path->slots[0],
415                                 struct btrfs_dev_extent);
416         btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
417         btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
418         btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
419
420         write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
421                     (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
422                     BTRFS_UUID_SIZE);
423
424         btrfs_set_dev_extent_length(leaf, extent, num_bytes);
425         btrfs_mark_buffer_dirty(leaf);
426 err:
427         btrfs_free_path(path);
428         return ret;
429 }
430
431 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
432 {
433         struct btrfs_path *path;
434         int ret;
435         struct btrfs_key key;
436         struct btrfs_chunk *chunk;
437         struct btrfs_key found_key;
438
439         path = btrfs_alloc_path();
440         BUG_ON(!path);
441
442         key.objectid = objectid;
443         key.offset = (u64)-1;
444         key.type = BTRFS_CHUNK_ITEM_KEY;
445
446         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
447         if (ret < 0)
448                 goto error;
449
450         BUG_ON(ret == 0);
451
452         ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
453         if (ret) {
454                 *offset = 0;
455         } else {
456                 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
457                                       path->slots[0]);
458                 if (found_key.objectid != objectid)
459                         *offset = 0;
460                 else {
461                         chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
462                                                struct btrfs_chunk);
463                         *offset = found_key.offset +
464                                 btrfs_chunk_length(path->nodes[0], chunk);
465                 }
466         }
467         ret = 0;
468 error:
469         btrfs_free_path(path);
470         return ret;
471 }
472
473 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
474                            u64 *objectid)
475 {
476         int ret;
477         struct btrfs_key key;
478         struct btrfs_key found_key;
479
480         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
481         key.type = BTRFS_DEV_ITEM_KEY;
482         key.offset = (u64)-1;
483
484         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
485         if (ret < 0)
486                 goto error;
487
488         BUG_ON(ret == 0);
489
490         ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
491                                   BTRFS_DEV_ITEM_KEY);
492         if (ret) {
493                 *objectid = 1;
494         } else {
495                 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
496                                       path->slots[0]);
497                 *objectid = found_key.offset + 1;
498         }
499         ret = 0;
500 error:
501         btrfs_release_path(path);
502         return ret;
503 }
504
505 /*
506  * the device information is stored in the chunk root
507  * the btrfs_device struct should be fully filled in
508  */
509 int btrfs_add_device(struct btrfs_trans_handle *trans,
510                      struct btrfs_root *root,
511                      struct btrfs_device *device)
512 {
513         int ret;
514         struct btrfs_path *path;
515         struct btrfs_dev_item *dev_item;
516         struct extent_buffer *leaf;
517         struct btrfs_key key;
518         unsigned long ptr;
519         u64 free_devid = 0;
520
521         root = root->fs_info->chunk_root;
522
523         path = btrfs_alloc_path();
524         if (!path)
525                 return -ENOMEM;
526
527         ret = find_next_devid(root, path, &free_devid);
528         if (ret)
529                 goto out;
530
531         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
532         key.type = BTRFS_DEV_ITEM_KEY;
533         key.offset = free_devid;
534
535         ret = btrfs_insert_empty_item(trans, root, path, &key,
536                                       sizeof(*dev_item));
537         if (ret)
538                 goto out;
539
540         leaf = path->nodes[0];
541         dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
542
543         device->devid = free_devid;
544         btrfs_set_device_id(leaf, dev_item, device->devid);
545         btrfs_set_device_generation(leaf, dev_item, 0);
546         btrfs_set_device_type(leaf, dev_item, device->type);
547         btrfs_set_device_io_align(leaf, dev_item, device->io_align);
548         btrfs_set_device_io_width(leaf, dev_item, device->io_width);
549         btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
550         btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
551         btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
552         btrfs_set_device_group(leaf, dev_item, 0);
553         btrfs_set_device_seek_speed(leaf, dev_item, 0);
554         btrfs_set_device_bandwidth(leaf, dev_item, 0);
555         btrfs_set_device_start_offset(leaf, dev_item, 0);
556
557         ptr = (unsigned long)btrfs_device_uuid(dev_item);
558         write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
559         ptr = (unsigned long)btrfs_device_fsid(dev_item);
560         write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
561         btrfs_mark_buffer_dirty(leaf);
562         ret = 0;
563
564 out:
565         btrfs_free_path(path);
566         return ret;
567 }
568
569 int btrfs_update_device(struct btrfs_trans_handle *trans,
570                         struct btrfs_device *device)
571 {
572         int ret;
573         struct btrfs_path *path;
574         struct btrfs_root *root;
575         struct btrfs_dev_item *dev_item;
576         struct extent_buffer *leaf;
577         struct btrfs_key key;
578
579         root = device->dev_root->fs_info->chunk_root;
580
581         path = btrfs_alloc_path();
582         if (!path)
583                 return -ENOMEM;
584
585         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
586         key.type = BTRFS_DEV_ITEM_KEY;
587         key.offset = device->devid;
588
589         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
590         if (ret < 0)
591                 goto out;
592
593         if (ret > 0) {
594                 ret = -ENOENT;
595                 goto out;
596         }
597
598         leaf = path->nodes[0];
599         dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
600
601         btrfs_set_device_id(leaf, dev_item, device->devid);
602         btrfs_set_device_type(leaf, dev_item, device->type);
603         btrfs_set_device_io_align(leaf, dev_item, device->io_align);
604         btrfs_set_device_io_width(leaf, dev_item, device->io_width);
605         btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
606         btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
607         btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
608         btrfs_mark_buffer_dirty(leaf);
609
610 out:
611         btrfs_free_path(path);
612         return ret;
613 }
614
615 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
616                            struct btrfs_root *root,
617                            struct btrfs_key *key,
618                            struct btrfs_chunk *chunk, int item_size)
619 {
620         struct btrfs_super_block *super_copy = root->fs_info->super_copy;
621         struct btrfs_disk_key disk_key;
622         u32 array_size;
623         u8 *ptr;
624
625         array_size = btrfs_super_sys_array_size(super_copy);
626         if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
627                 return -EFBIG;
628
629         ptr = super_copy->sys_chunk_array + array_size;
630         btrfs_cpu_key_to_disk(&disk_key, key);
631         memcpy(ptr, &disk_key, sizeof(disk_key));
632         ptr += sizeof(disk_key);
633         memcpy(ptr, chunk, item_size);
634         item_size += sizeof(disk_key);
635         btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
636         return 0;
637 }
638
639 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
640                                int sub_stripes)
641 {
642         if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
643                 return calc_size;
644         else if (type & BTRFS_BLOCK_GROUP_RAID10)
645                 return calc_size * (num_stripes / sub_stripes);
646         else if (type & BTRFS_BLOCK_GROUP_RAID5)
647                 return calc_size * (num_stripes - 1);
648         else if (type & BTRFS_BLOCK_GROUP_RAID6)
649                 return calc_size * (num_stripes - 2);
650         else
651                 return calc_size * num_stripes;
652 }
653
654
655 static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
656 {
657         /* TODO, add a way to store the preferred stripe size */
658         return 64 * 1024;
659 }
660
661 /*
662  * btrfs_device_avail_bytes - count bytes available for alloc_chunk
663  *
664  * It is not equal to "device->total_bytes - device->bytes_used".
665  * We do not allocate any chunk in 1M at beginning of device, and not
666  * allowed to allocate any chunk before alloc_start if it is specified.
667  * So search holes from max(1M, alloc_start) to device->total_bytes.
668  */
669 static int btrfs_device_avail_bytes(struct btrfs_trans_handle *trans,
670                                     struct btrfs_device *device,
671                                     u64 *avail_bytes)
672 {
673         struct btrfs_path *path;
674         struct btrfs_root *root = device->dev_root;
675         struct btrfs_key key;
676         struct btrfs_dev_extent *dev_extent = NULL;
677         struct extent_buffer *l;
678         u64 search_start = root->fs_info->alloc_start;
679         u64 search_end = device->total_bytes;
680         u64 extent_end = 0;
681         u64 free_bytes = 0;
682         int ret;
683         int slot = 0;
684
685         search_start = max(BTRFS_BLOCK_RESERVED_1M_FOR_SUPER, search_start);
686
687         path = btrfs_alloc_path();
688         if (!path)
689                 return -ENOMEM;
690
691         key.objectid = device->devid;
692         key.offset = root->fs_info->alloc_start;
693         key.type = BTRFS_DEV_EXTENT_KEY;
694
695         path->reada = 2;
696         ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
697         if (ret < 0)
698                 goto error;
699         ret = btrfs_previous_item(root, path, 0, key.type);
700         if (ret < 0)
701                 goto error;
702
703         while (1) {
704                 l = path->nodes[0];
705                 slot = path->slots[0];
706                 if (slot >= btrfs_header_nritems(l)) {
707                         ret = btrfs_next_leaf(root, path);
708                         if (ret == 0)
709                                 continue;
710                         if (ret < 0)
711                                 goto error;
712                         break;
713                 }
714                 btrfs_item_key_to_cpu(l, &key, slot);
715
716                 if (key.objectid < device->devid)
717                         goto next;
718                 if (key.objectid > device->devid)
719                         break;
720                 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
721                         goto next;
722                 if (key.offset > search_end)
723                         break;
724                 if (key.offset > search_start)
725                         free_bytes += key.offset - search_start;
726
727                 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
728                 extent_end = key.offset + btrfs_dev_extent_length(l,
729                                                                   dev_extent);
730                 if (extent_end > search_start)
731                         search_start = extent_end;
732                 if (search_start > search_end)
733                         break;
734 next:
735                 path->slots[0]++;
736                 cond_resched();
737         }
738
739         if (search_start < search_end)
740                 free_bytes += search_end - search_start;
741
742         *avail_bytes = free_bytes;
743         ret = 0;
744 error:
745         btrfs_free_path(path);
746         return ret;
747 }
748
749 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
750                       struct btrfs_root *extent_root, u64 *start,
751                       u64 *num_bytes, u64 type)
752 {
753         u64 dev_offset;
754         struct btrfs_fs_info *info = extent_root->fs_info;
755         struct btrfs_root *chunk_root = info->chunk_root;
756         struct btrfs_stripe *stripes;
757         struct btrfs_device *device = NULL;
758         struct btrfs_chunk *chunk;
759         struct list_head private_devs;
760         struct list_head *dev_list = &info->fs_devices->devices;
761         struct list_head *cur;
762         struct map_lookup *map;
763         int min_stripe_size = 1 * 1024 * 1024;
764         u64 calc_size = 8 * 1024 * 1024;
765         u64 min_free;
766         u64 max_chunk_size = 4 * calc_size;
767         u64 avail = 0;
768         u64 max_avail = 0;
769         u64 percent_max;
770         int num_stripes = 1;
771         int min_stripes = 1;
772         int sub_stripes = 0;
773         int looped = 0;
774         int ret;
775         int index;
776         int stripe_len = 64 * 1024;
777         struct btrfs_key key;
778         u64 offset;
779
780         if (list_empty(dev_list)) {
781                 return -ENOSPC;
782         }
783
784         if (type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
785                     BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
786                     BTRFS_BLOCK_GROUP_RAID10 |
787                     BTRFS_BLOCK_GROUP_DUP)) {
788                 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
789                         calc_size = 8 * 1024 * 1024;
790                         max_chunk_size = calc_size * 2;
791                         min_stripe_size = 1 * 1024 * 1024;
792                 } else if (type & BTRFS_BLOCK_GROUP_DATA) {
793                         calc_size = 1024 * 1024 * 1024;
794                         max_chunk_size = 10 * calc_size;
795                         min_stripe_size = 64 * 1024 * 1024;
796                 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
797                         calc_size = 1024 * 1024 * 1024;
798                         max_chunk_size = 4 * calc_size;
799                         min_stripe_size = 32 * 1024 * 1024;
800                 }
801         }
802         if (type & BTRFS_BLOCK_GROUP_RAID1) {
803                 num_stripes = min_t(u64, 2,
804                                   btrfs_super_num_devices(info->super_copy));
805                 if (num_stripes < 2)
806                         return -ENOSPC;
807                 min_stripes = 2;
808         }
809         if (type & BTRFS_BLOCK_GROUP_DUP) {
810                 num_stripes = 2;
811                 min_stripes = 2;
812         }
813         if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
814                 num_stripes = btrfs_super_num_devices(info->super_copy);
815                 min_stripes = 2;
816         }
817         if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
818                 num_stripes = btrfs_super_num_devices(info->super_copy);
819                 if (num_stripes < 4)
820                         return -ENOSPC;
821                 num_stripes &= ~(u32)1;
822                 sub_stripes = 2;
823                 min_stripes = 4;
824         }
825         if (type & (BTRFS_BLOCK_GROUP_RAID5)) {
826                 num_stripes = btrfs_super_num_devices(info->super_copy);
827                 if (num_stripes < 2)
828                         return -ENOSPC;
829                 min_stripes = 2;
830                 stripe_len = find_raid56_stripe_len(num_stripes - 1,
831                                     btrfs_super_stripesize(info->super_copy));
832         }
833         if (type & (BTRFS_BLOCK_GROUP_RAID6)) {
834                 num_stripes = btrfs_super_num_devices(info->super_copy);
835                 if (num_stripes < 3)
836                         return -ENOSPC;
837                 min_stripes = 3;
838                 stripe_len = find_raid56_stripe_len(num_stripes - 2,
839                                     btrfs_super_stripesize(info->super_copy));
840         }
841
842         /* we don't want a chunk larger than 10% of the FS */
843         percent_max = div_factor(btrfs_super_total_bytes(info->super_copy), 1);
844         max_chunk_size = min(percent_max, max_chunk_size);
845
846 again:
847         if (chunk_bytes_by_type(type, calc_size, num_stripes, sub_stripes) >
848             max_chunk_size) {
849                 calc_size = max_chunk_size;
850                 calc_size /= num_stripes;
851                 calc_size /= stripe_len;
852                 calc_size *= stripe_len;
853         }
854         /* we don't want tiny stripes */
855         calc_size = max_t(u64, calc_size, min_stripe_size);
856
857         calc_size /= stripe_len;
858         calc_size *= stripe_len;
859         INIT_LIST_HEAD(&private_devs);
860         cur = dev_list->next;
861         index = 0;
862
863         if (type & BTRFS_BLOCK_GROUP_DUP)
864                 min_free = calc_size * 2;
865         else
866                 min_free = calc_size;
867
868         /* build a private list of devices we will allocate from */
869         while(index < num_stripes) {
870                 device = list_entry(cur, struct btrfs_device, dev_list);
871                 ret = btrfs_device_avail_bytes(trans, device, &avail);
872                 if (ret)
873                         return ret;
874                 cur = cur->next;
875                 if (avail >= min_free) {
876                         list_move_tail(&device->dev_list, &private_devs);
877                         index++;
878                         if (type & BTRFS_BLOCK_GROUP_DUP)
879                                 index++;
880                 } else if (avail > max_avail)
881                         max_avail = avail;
882                 if (cur == dev_list)
883                         break;
884         }
885         if (index < num_stripes) {
886                 list_splice(&private_devs, dev_list);
887                 if (index >= min_stripes) {
888                         num_stripes = index;
889                         if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
890                                 num_stripes /= sub_stripes;
891                                 num_stripes *= sub_stripes;
892                         }
893                         looped = 1;
894                         goto again;
895                 }
896                 if (!looped && max_avail > 0) {
897                         looped = 1;
898                         calc_size = max_avail;
899                         goto again;
900                 }
901                 return -ENOSPC;
902         }
903         ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
904                               &offset);
905         if (ret)
906                 return ret;
907         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
908         key.type = BTRFS_CHUNK_ITEM_KEY;
909         key.offset = offset;
910
911         chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
912         if (!chunk)
913                 return -ENOMEM;
914
915         map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
916         if (!map) {
917                 kfree(chunk);
918                 return -ENOMEM;
919         }
920
921         stripes = &chunk->stripe;
922         *num_bytes = chunk_bytes_by_type(type, calc_size,
923                                          num_stripes, sub_stripes);
924         index = 0;
925         while(index < num_stripes) {
926                 struct btrfs_stripe *stripe;
927                 BUG_ON(list_empty(&private_devs));
928                 cur = private_devs.next;
929                 device = list_entry(cur, struct btrfs_device, dev_list);
930
931                 /* loop over this device again if we're doing a dup group */
932                 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
933                     (index == num_stripes - 1))
934                         list_move_tail(&device->dev_list, dev_list);
935
936                 ret = btrfs_alloc_dev_extent(trans, device,
937                              info->chunk_root->root_key.objectid,
938                              BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
939                              calc_size, &dev_offset);
940                 BUG_ON(ret);
941
942                 device->bytes_used += calc_size;
943                 ret = btrfs_update_device(trans, device);
944                 BUG_ON(ret);
945
946                 map->stripes[index].dev = device;
947                 map->stripes[index].physical = dev_offset;
948                 stripe = stripes + index;
949                 btrfs_set_stack_stripe_devid(stripe, device->devid);
950                 btrfs_set_stack_stripe_offset(stripe, dev_offset);
951                 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
952                 index++;
953         }
954         BUG_ON(!list_empty(&private_devs));
955
956         /* key was set above */
957         btrfs_set_stack_chunk_length(chunk, *num_bytes);
958         btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
959         btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
960         btrfs_set_stack_chunk_type(chunk, type);
961         btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
962         btrfs_set_stack_chunk_io_align(chunk, stripe_len);
963         btrfs_set_stack_chunk_io_width(chunk, stripe_len);
964         btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
965         btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
966         map->sector_size = extent_root->sectorsize;
967         map->stripe_len = stripe_len;
968         map->io_align = stripe_len;
969         map->io_width = stripe_len;
970         map->type = type;
971         map->num_stripes = num_stripes;
972         map->sub_stripes = sub_stripes;
973
974         ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
975                                 btrfs_chunk_item_size(num_stripes));
976         BUG_ON(ret);
977         *start = key.offset;;
978
979         map->ce.start = key.offset;
980         map->ce.size = *num_bytes;
981
982         ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
983         BUG_ON(ret);
984
985         if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
986                 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
987                                     chunk, btrfs_chunk_item_size(num_stripes));
988                 BUG_ON(ret);
989         }
990
991         kfree(chunk);
992         return ret;
993 }
994
995 int btrfs_alloc_data_chunk(struct btrfs_trans_handle *trans,
996                            struct btrfs_root *extent_root, u64 *start,
997                            u64 num_bytes, u64 type)
998 {
999         u64 dev_offset;
1000         struct btrfs_fs_info *info = extent_root->fs_info;
1001         struct btrfs_root *chunk_root = info->chunk_root;
1002         struct btrfs_stripe *stripes;
1003         struct btrfs_device *device = NULL;
1004         struct btrfs_chunk *chunk;
1005         struct list_head *dev_list = &info->fs_devices->devices;
1006         struct list_head *cur;
1007         struct map_lookup *map;
1008         u64 calc_size = 8 * 1024 * 1024;
1009         int num_stripes = 1;
1010         int sub_stripes = 0;
1011         int ret;
1012         int index;
1013         int stripe_len = 64 * 1024;
1014         struct btrfs_key key;
1015
1016         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1017         key.type = BTRFS_CHUNK_ITEM_KEY;
1018         ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1019                               &key.offset);
1020         if (ret)
1021                 return ret;
1022
1023         chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
1024         if (!chunk)
1025                 return -ENOMEM;
1026
1027         map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
1028         if (!map) {
1029                 kfree(chunk);
1030                 return -ENOMEM;
1031         }
1032
1033         stripes = &chunk->stripe;
1034         calc_size = num_bytes;
1035
1036         index = 0;
1037         cur = dev_list->next;
1038         device = list_entry(cur, struct btrfs_device, dev_list);
1039
1040         while (index < num_stripes) {
1041                 struct btrfs_stripe *stripe;
1042
1043                 ret = btrfs_alloc_dev_extent(trans, device,
1044                              info->chunk_root->root_key.objectid,
1045                              BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
1046                              calc_size, &dev_offset);
1047                 BUG_ON(ret);
1048
1049                 device->bytes_used += calc_size;
1050                 ret = btrfs_update_device(trans, device);
1051                 BUG_ON(ret);
1052
1053                 map->stripes[index].dev = device;
1054                 map->stripes[index].physical = dev_offset;
1055                 stripe = stripes + index;
1056                 btrfs_set_stack_stripe_devid(stripe, device->devid);
1057                 btrfs_set_stack_stripe_offset(stripe, dev_offset);
1058                 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1059                 index++;
1060         }
1061
1062         /* key was set above */
1063         btrfs_set_stack_chunk_length(chunk, num_bytes);
1064         btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
1065         btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
1066         btrfs_set_stack_chunk_type(chunk, type);
1067         btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
1068         btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1069         btrfs_set_stack_chunk_io_width(chunk, stripe_len);
1070         btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
1071         btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
1072         map->sector_size = extent_root->sectorsize;
1073         map->stripe_len = stripe_len;
1074         map->io_align = stripe_len;
1075         map->io_width = stripe_len;
1076         map->type = type;
1077         map->num_stripes = num_stripes;
1078         map->sub_stripes = sub_stripes;
1079
1080         ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1081                                 btrfs_chunk_item_size(num_stripes));
1082         BUG_ON(ret);
1083         *start = key.offset;
1084
1085         map->ce.start = key.offset;
1086         map->ce.size = num_bytes;
1087
1088         ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
1089         BUG_ON(ret);
1090
1091         kfree(chunk);
1092         return ret;
1093 }
1094
1095 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1096 {
1097         struct cache_extent *ce;
1098         struct map_lookup *map;
1099         int ret;
1100
1101         ce = search_cache_extent(&map_tree->cache_tree, logical);
1102         BUG_ON(!ce);
1103         BUG_ON(ce->start > logical || ce->start + ce->size < logical);
1104         map = container_of(ce, struct map_lookup, ce);
1105
1106         if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1107                 ret = map->num_stripes;
1108         else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1109                 ret = map->sub_stripes;
1110         else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
1111                 ret = 2;
1112         else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1113                 ret = 3;
1114         else
1115                 ret = 1;
1116         return ret;
1117 }
1118
1119 int btrfs_next_metadata(struct btrfs_mapping_tree *map_tree, u64 *logical,
1120                         u64 *size)
1121 {
1122         struct cache_extent *ce;
1123         struct map_lookup *map;
1124
1125         ce = search_cache_extent(&map_tree->cache_tree, *logical);
1126
1127         while (ce) {
1128                 ce = next_cache_extent(ce);
1129                 if (!ce)
1130                         return -ENOENT;
1131
1132                 map = container_of(ce, struct map_lookup, ce);
1133                 if (map->type & BTRFS_BLOCK_GROUP_METADATA) {
1134                         *logical = ce->start;
1135                         *size = ce->size;
1136                         return 0;
1137                 }
1138         }
1139
1140         return -ENOENT;
1141 }
1142
1143 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
1144                      u64 chunk_start, u64 physical, u64 devid,
1145                      u64 **logical, int *naddrs, int *stripe_len)
1146 {
1147         struct cache_extent *ce;
1148         struct map_lookup *map;
1149         u64 *buf;
1150         u64 bytenr;
1151         u64 length;
1152         u64 stripe_nr;
1153         u64 rmap_len;
1154         int i, j, nr = 0;
1155
1156         ce = search_cache_extent(&map_tree->cache_tree, chunk_start);
1157         BUG_ON(!ce);
1158         map = container_of(ce, struct map_lookup, ce);
1159
1160         length = ce->size;
1161         rmap_len = map->stripe_len;
1162         if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1163                 length = ce->size / (map->num_stripes / map->sub_stripes);
1164         else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
1165                 length = ce->size / map->num_stripes;
1166         else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1167                               BTRFS_BLOCK_GROUP_RAID6)) {
1168                 length = ce->size / nr_data_stripes(map);
1169                 rmap_len = map->stripe_len * nr_data_stripes(map);
1170         }
1171
1172         buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1173
1174         for (i = 0; i < map->num_stripes; i++) {
1175                 if (devid && map->stripes[i].dev->devid != devid)
1176                         continue;
1177                 if (map->stripes[i].physical > physical ||
1178                     map->stripes[i].physical + length <= physical)
1179                         continue;
1180
1181                 stripe_nr = (physical - map->stripes[i].physical) /
1182                             map->stripe_len;
1183
1184                 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1185                         stripe_nr = (stripe_nr * map->num_stripes + i) /
1186                                     map->sub_stripes;
1187                 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1188                         stripe_nr = stripe_nr * map->num_stripes + i;
1189                 } /* else if RAID[56], multiply by nr_data_stripes().
1190                    * Alternatively, just use rmap_len below instead of
1191                    * map->stripe_len */
1192
1193                 bytenr = ce->start + stripe_nr * rmap_len;
1194                 for (j = 0; j < nr; j++) {
1195                         if (buf[j] == bytenr)
1196                                 break;
1197                 }
1198                 if (j == nr)
1199                         buf[nr++] = bytenr;
1200         }
1201
1202         *logical = buf;
1203         *naddrs = nr;
1204         *stripe_len = rmap_len;
1205
1206         return 0;
1207 }
1208
1209 static inline int parity_smaller(u64 a, u64 b)
1210 {
1211         return a > b;
1212 }
1213
1214 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
1215 static void sort_parity_stripes(struct btrfs_multi_bio *bbio, u64 *raid_map)
1216 {
1217         struct btrfs_bio_stripe s;
1218         int i;
1219         u64 l;
1220         int again = 1;
1221
1222         while (again) {
1223                 again = 0;
1224                 for (i = 0; i < bbio->num_stripes - 1; i++) {
1225                         if (parity_smaller(raid_map[i], raid_map[i+1])) {
1226                                 s = bbio->stripes[i];
1227                                 l = raid_map[i];
1228                                 bbio->stripes[i] = bbio->stripes[i+1];
1229                                 raid_map[i] = raid_map[i+1];
1230                                 bbio->stripes[i+1] = s;
1231                                 raid_map[i+1] = l;
1232                                 again = 1;
1233                         }
1234                 }
1235         }
1236 }
1237
1238 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1239                     u64 logical, u64 *length,
1240                     struct btrfs_multi_bio **multi_ret, int mirror_num,
1241                     u64 **raid_map_ret)
1242 {
1243         return __btrfs_map_block(map_tree, rw, logical, length, NULL,
1244                                  multi_ret, mirror_num, raid_map_ret);
1245 }
1246
1247 int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1248                     u64 logical, u64 *length, u64 *type,
1249                     struct btrfs_multi_bio **multi_ret, int mirror_num,
1250                     u64 **raid_map_ret)
1251 {
1252         struct cache_extent *ce;
1253         struct map_lookup *map;
1254         u64 offset;
1255         u64 stripe_offset;
1256         u64 stripe_nr;
1257         u64 *raid_map = NULL;
1258         int stripes_allocated = 8;
1259         int stripes_required = 1;
1260         int stripe_index;
1261         int i;
1262         struct btrfs_multi_bio *multi = NULL;
1263
1264         if (multi_ret && rw == READ) {
1265                 stripes_allocated = 1;
1266         }
1267 again:
1268         ce = search_cache_extent(&map_tree->cache_tree, logical);
1269         if (!ce) {
1270                 kfree(multi);
1271                 return -ENOENT;
1272         }
1273         if (ce->start > logical || ce->start + ce->size < logical) {
1274                 kfree(multi);
1275                 return -ENOENT;
1276         }
1277
1278         if (multi_ret) {
1279                 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1280                                 GFP_NOFS);
1281                 if (!multi)
1282                         return -ENOMEM;
1283         }
1284         map = container_of(ce, struct map_lookup, ce);
1285         offset = logical - ce->start;
1286
1287         if (rw == WRITE) {
1288                 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1289                                  BTRFS_BLOCK_GROUP_DUP)) {
1290                         stripes_required = map->num_stripes;
1291                 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1292                         stripes_required = map->sub_stripes;
1293                 }
1294         }
1295         if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)
1296             && multi_ret && ((rw & WRITE) || mirror_num > 1) && raid_map_ret) {
1297                     /* RAID[56] write or recovery. Return all stripes */
1298                     stripes_required = map->num_stripes;
1299
1300                     /* Only allocate the map if we've already got a large enough multi_ret */
1301                     if (stripes_allocated >= stripes_required) {
1302                             raid_map = kmalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1303                             if (!raid_map) {
1304                                     kfree(multi);
1305                                     return -ENOMEM;
1306                             }
1307                     }
1308         }
1309
1310         /* if our multi bio struct is too small, back off and try again */
1311         if (multi_ret && stripes_allocated < stripes_required) {
1312                 stripes_allocated = stripes_required;
1313                 kfree(multi);
1314                 multi = NULL;
1315                 goto again;
1316         }
1317         stripe_nr = offset;
1318         /*
1319          * stripe_nr counts the total number of stripes we have to stride
1320          * to get to this block
1321          */
1322         stripe_nr = stripe_nr / map->stripe_len;
1323
1324         stripe_offset = stripe_nr * map->stripe_len;
1325         BUG_ON(offset < stripe_offset);
1326
1327         /* stripe_offset is the offset of this block in its stripe*/
1328         stripe_offset = offset - stripe_offset;
1329
1330         if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
1331                          BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
1332                          BTRFS_BLOCK_GROUP_RAID10 |
1333                          BTRFS_BLOCK_GROUP_DUP)) {
1334                 /* we limit the length of each bio to what fits in a stripe */
1335                 *length = min_t(u64, ce->size - offset,
1336                               map->stripe_len - stripe_offset);
1337         } else {
1338                 *length = ce->size - offset;
1339         }
1340
1341         if (!multi_ret)
1342                 goto out;
1343
1344         multi->num_stripes = 1;
1345         stripe_index = 0;
1346         if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1347                 if (rw == WRITE)
1348                         multi->num_stripes = map->num_stripes;
1349                 else if (mirror_num)
1350                         stripe_index = mirror_num - 1;
1351                 else
1352                         stripe_index = stripe_nr % map->num_stripes;
1353         } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1354                 int factor = map->num_stripes / map->sub_stripes;
1355
1356                 stripe_index = stripe_nr % factor;
1357                 stripe_index *= map->sub_stripes;
1358
1359                 if (rw == WRITE)
1360                         multi->num_stripes = map->sub_stripes;
1361                 else if (mirror_num)
1362                         stripe_index += mirror_num - 1;
1363
1364                 stripe_nr = stripe_nr / factor;
1365         } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1366                 if (rw == WRITE)
1367                         multi->num_stripes = map->num_stripes;
1368                 else if (mirror_num)
1369                         stripe_index = mirror_num - 1;
1370         } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1371                                 BTRFS_BLOCK_GROUP_RAID6)) {
1372
1373                 if (raid_map) {
1374                         int rot;
1375                         u64 tmp;
1376                         u64 raid56_full_stripe_start;
1377                         u64 full_stripe_len = nr_data_stripes(map) * map->stripe_len;
1378
1379                         /*
1380                          * align the start of our data stripe in the logical
1381                          * address space
1382                          */
1383                         raid56_full_stripe_start = offset / full_stripe_len;
1384                         raid56_full_stripe_start *= full_stripe_len;
1385
1386                         /* get the data stripe number */
1387                         stripe_nr = raid56_full_stripe_start / map->stripe_len;
1388                         stripe_nr = stripe_nr / nr_data_stripes(map);
1389
1390                         /* Work out the disk rotation on this stripe-set */
1391                         rot = stripe_nr % map->num_stripes;
1392
1393                         /* Fill in the logical address of each stripe */
1394                         tmp = stripe_nr * nr_data_stripes(map);
1395
1396                         for (i = 0; i < nr_data_stripes(map); i++)
1397                                 raid_map[(i+rot) % map->num_stripes] =
1398                                         ce->start + (tmp + i) * map->stripe_len;
1399
1400                         raid_map[(i+rot) % map->num_stripes] = BTRFS_RAID5_P_STRIPE;
1401                         if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1402                                 raid_map[(i+rot+1) % map->num_stripes] = BTRFS_RAID6_Q_STRIPE;
1403
1404                         *length = map->stripe_len;
1405                         stripe_index = 0;
1406                         stripe_offset = 0;
1407                         multi->num_stripes = map->num_stripes;
1408                 } else {
1409                         stripe_index = stripe_nr % nr_data_stripes(map);
1410                         stripe_nr = stripe_nr / nr_data_stripes(map);
1411
1412                         /*
1413                          * Mirror #0 or #1 means the original data block.
1414                          * Mirror #2 is RAID5 parity block.
1415                          * Mirror #3 is RAID6 Q block.
1416                          */
1417                         if (mirror_num > 1)
1418                                 stripe_index = nr_data_stripes(map) + mirror_num - 2;
1419
1420                         /* We distribute the parity blocks across stripes */
1421                         stripe_index = (stripe_nr + stripe_index) % map->num_stripes;
1422                 }
1423         } else {
1424                 /*
1425                  * after this do_div call, stripe_nr is the number of stripes
1426                  * on this device we have to walk to find the data, and
1427                  * stripe_index is the number of our device in the stripe array
1428                  */
1429                 stripe_index = stripe_nr % map->num_stripes;
1430                 stripe_nr = stripe_nr / map->num_stripes;
1431         }
1432         BUG_ON(stripe_index >= map->num_stripes);
1433
1434         for (i = 0; i < multi->num_stripes; i++) {
1435                 multi->stripes[i].physical =
1436                         map->stripes[stripe_index].physical + stripe_offset +
1437                         stripe_nr * map->stripe_len;
1438                 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1439                 stripe_index++;
1440         }
1441         *multi_ret = multi;
1442
1443         if (type)
1444                 *type = map->type;
1445
1446         if (raid_map) {
1447                 sort_parity_stripes(multi, raid_map);
1448                 *raid_map_ret = raid_map;
1449         }
1450 out:
1451         return 0;
1452 }
1453
1454 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1455                                        u8 *uuid, u8 *fsid)
1456 {
1457         struct btrfs_device *device;
1458         struct btrfs_fs_devices *cur_devices;
1459
1460         cur_devices = root->fs_info->fs_devices;
1461         while (cur_devices) {
1462                 if (!fsid ||
1463                     !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1464                         device = __find_device(&cur_devices->devices,
1465                                                devid, uuid);
1466                         if (device)
1467                                 return device;
1468                 }
1469                 cur_devices = cur_devices->seed;
1470         }
1471         return NULL;
1472 }
1473
1474 struct btrfs_device *
1475 btrfs_find_device_by_devid(struct btrfs_fs_devices *fs_devices,
1476                            u64 devid, int instance)
1477 {
1478         struct list_head *head = &fs_devices->devices;
1479         struct btrfs_device *dev;
1480         int num_found = 0;
1481
1482         list_for_each_entry(dev, head, dev_list) {
1483                 if (dev->devid == devid && num_found++ == instance)
1484                         return dev;
1485         }
1486         return NULL;
1487 }
1488
1489 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
1490 {
1491         struct cache_extent *ce;
1492         struct map_lookup *map;
1493         struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1494         int readonly = 0;
1495         int i;
1496
1497         /*
1498          * During chunk recovering, we may fail to find block group's
1499          * corresponding chunk, we will rebuild it later
1500          */
1501         ce = search_cache_extent(&map_tree->cache_tree, chunk_offset);
1502         if (!root->fs_info->is_chunk_recover)
1503                 BUG_ON(!ce);
1504         else
1505                 return 0;
1506
1507         map = container_of(ce, struct map_lookup, ce);
1508         for (i = 0; i < map->num_stripes; i++) {
1509                 if (!map->stripes[i].dev->writeable) {
1510                         readonly = 1;
1511                         break;
1512                 }
1513         }
1514
1515         return readonly;
1516 }
1517
1518 static struct btrfs_device *fill_missing_device(u64 devid)
1519 {
1520         struct btrfs_device *device;
1521
1522         device = kzalloc(sizeof(*device), GFP_NOFS);
1523         device->devid = devid;
1524         device->fd = -1;
1525         return device;
1526 }
1527
1528 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1529                           struct extent_buffer *leaf,
1530                           struct btrfs_chunk *chunk)
1531 {
1532         struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1533         struct map_lookup *map;
1534         struct cache_extent *ce;
1535         u64 logical;
1536         u64 length;
1537         u64 devid;
1538         u8 uuid[BTRFS_UUID_SIZE];
1539         int num_stripes;
1540         int ret;
1541         int i;
1542
1543         logical = key->offset;
1544         length = btrfs_chunk_length(leaf, chunk);
1545
1546         ce = search_cache_extent(&map_tree->cache_tree, logical);
1547
1548         /* already mapped? */
1549         if (ce && ce->start <= logical && ce->start + ce->size > logical) {
1550                 return 0;
1551         }
1552
1553         num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1554         map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
1555         if (!map)
1556                 return -ENOMEM;
1557
1558         map->ce.start = logical;
1559         map->ce.size = length;
1560         map->num_stripes = num_stripes;
1561         map->io_width = btrfs_chunk_io_width(leaf, chunk);
1562         map->io_align = btrfs_chunk_io_align(leaf, chunk);
1563         map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1564         map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1565         map->type = btrfs_chunk_type(leaf, chunk);
1566         map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1567
1568         for (i = 0; i < num_stripes; i++) {
1569                 map->stripes[i].physical =
1570                         btrfs_stripe_offset_nr(leaf, chunk, i);
1571                 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1572                 read_extent_buffer(leaf, uuid, (unsigned long)
1573                                    btrfs_stripe_dev_uuid_nr(chunk, i),
1574                                    BTRFS_UUID_SIZE);
1575                 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
1576                                                         NULL);
1577                 if (!map->stripes[i].dev) {
1578                         map->stripes[i].dev = fill_missing_device(devid);
1579                         printf("warning, device %llu is missing\n",
1580                                (unsigned long long)devid);
1581                 }
1582
1583         }
1584         ret = insert_cache_extent(&map_tree->cache_tree, &map->ce);
1585         BUG_ON(ret);
1586
1587         return 0;
1588 }
1589
1590 static int fill_device_from_item(struct extent_buffer *leaf,
1591                                  struct btrfs_dev_item *dev_item,
1592                                  struct btrfs_device *device)
1593 {
1594         unsigned long ptr;
1595
1596         device->devid = btrfs_device_id(leaf, dev_item);
1597         device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1598         device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1599         device->type = btrfs_device_type(leaf, dev_item);
1600         device->io_align = btrfs_device_io_align(leaf, dev_item);
1601         device->io_width = btrfs_device_io_width(leaf, dev_item);
1602         device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1603
1604         ptr = (unsigned long)btrfs_device_uuid(dev_item);
1605         read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1606
1607         return 0;
1608 }
1609
1610 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
1611 {
1612         struct btrfs_fs_devices *fs_devices;
1613         int ret;
1614
1615         fs_devices = root->fs_info->fs_devices->seed;
1616         while (fs_devices) {
1617                 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1618                         ret = 0;
1619                         goto out;
1620                 }
1621                 fs_devices = fs_devices->seed;
1622         }
1623
1624         fs_devices = find_fsid(fsid);
1625         if (!fs_devices) {
1626                 ret = -ENOENT;
1627                 goto out;
1628         }
1629
1630         ret = btrfs_open_devices(fs_devices, O_RDONLY);
1631         if (ret)
1632                 goto out;
1633
1634         fs_devices->seed = root->fs_info->fs_devices->seed;
1635         root->fs_info->fs_devices->seed = fs_devices;
1636 out:
1637         return ret;
1638 }
1639
1640 static int read_one_dev(struct btrfs_root *root,
1641                         struct extent_buffer *leaf,
1642                         struct btrfs_dev_item *dev_item)
1643 {
1644         struct btrfs_device *device;
1645         u64 devid;
1646         int ret = 0;
1647         u8 fs_uuid[BTRFS_UUID_SIZE];
1648         u8 dev_uuid[BTRFS_UUID_SIZE];
1649
1650         devid = btrfs_device_id(leaf, dev_item);
1651         read_extent_buffer(leaf, dev_uuid,
1652                            (unsigned long)btrfs_device_uuid(dev_item),
1653                            BTRFS_UUID_SIZE);
1654         read_extent_buffer(leaf, fs_uuid,
1655                            (unsigned long)btrfs_device_fsid(dev_item),
1656                            BTRFS_UUID_SIZE);
1657
1658         if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
1659                 ret = open_seed_devices(root, fs_uuid);
1660                 if (ret)
1661                         return ret;
1662         }
1663
1664         device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1665         if (!device) {
1666                 printk("warning devid %llu not found already\n",
1667                         (unsigned long long)devid);
1668                 device = kzalloc(sizeof(*device), GFP_NOFS);
1669                 if (!device)
1670                         return -ENOMEM;
1671                 device->fd = -1;
1672                 list_add(&device->dev_list,
1673                          &root->fs_info->fs_devices->devices);
1674         }
1675
1676         fill_device_from_item(leaf, dev_item, device);
1677         device->dev_root = root->fs_info->dev_root;
1678         return ret;
1679 }
1680
1681 int btrfs_read_sys_array(struct btrfs_root *root)
1682 {
1683         struct btrfs_super_block *super_copy = root->fs_info->super_copy;
1684         struct extent_buffer *sb;
1685         struct btrfs_disk_key *disk_key;
1686         struct btrfs_chunk *chunk;
1687         struct btrfs_key key;
1688         u32 num_stripes;
1689         u32 len = 0;
1690         u8 *ptr;
1691         u8 *array_end;
1692         int ret = 0;
1693
1694         sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
1695                                           BTRFS_SUPER_INFO_SIZE);
1696         if (!sb)
1697                 return -ENOMEM;
1698         btrfs_set_buffer_uptodate(sb);
1699         write_extent_buffer(sb, super_copy, 0, sizeof(*super_copy));
1700         array_end = ((u8 *)super_copy->sys_chunk_array) +
1701                     btrfs_super_sys_array_size(super_copy);
1702
1703         /*
1704          * we do this loop twice, once for the device items and
1705          * once for all of the chunks.  This way there are device
1706          * structs filled in for every chunk
1707          */
1708         ptr = super_copy->sys_chunk_array;
1709
1710         while (ptr < array_end) {
1711                 disk_key = (struct btrfs_disk_key *)ptr;
1712                 btrfs_disk_key_to_cpu(&key, disk_key);
1713
1714                 len = sizeof(*disk_key);
1715                 ptr += len;
1716
1717                 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1718                         chunk = (struct btrfs_chunk *)(ptr - (u8 *)super_copy);
1719                         ret = read_one_chunk(root, &key, sb, chunk);
1720                         if (ret)
1721                                 break;
1722                         num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1723                         len = btrfs_chunk_item_size(num_stripes);
1724                 } else {
1725                         BUG();
1726                 }
1727                 ptr += len;
1728         }
1729         free_extent_buffer(sb);
1730         return ret;
1731 }
1732
1733 int btrfs_read_chunk_tree(struct btrfs_root *root)
1734 {
1735         struct btrfs_path *path;
1736         struct extent_buffer *leaf;
1737         struct btrfs_key key;
1738         struct btrfs_key found_key;
1739         int ret;
1740         int slot;
1741
1742         root = root->fs_info->chunk_root;
1743
1744         path = btrfs_alloc_path();
1745         if (!path)
1746                 return -ENOMEM;
1747
1748         /*
1749          * Read all device items, and then all the chunk items. All
1750          * device items are found before any chunk item (their object id
1751          * is smaller than the lowest possible object id for a chunk
1752          * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
1753          */
1754         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1755         key.offset = 0;
1756         key.type = 0;
1757         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1758         if (ret < 0)
1759                 goto error;
1760         while(1) {
1761                 leaf = path->nodes[0];
1762                 slot = path->slots[0];
1763                 if (slot >= btrfs_header_nritems(leaf)) {
1764                         ret = btrfs_next_leaf(root, path);
1765                         if (ret == 0)
1766                                 continue;
1767                         if (ret < 0)
1768                                 goto error;
1769                         break;
1770                 }
1771                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1772                 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1773                         struct btrfs_dev_item *dev_item;
1774                         dev_item = btrfs_item_ptr(leaf, slot,
1775                                                   struct btrfs_dev_item);
1776                         ret = read_one_dev(root, leaf, dev_item);
1777                         BUG_ON(ret);
1778                 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1779                         struct btrfs_chunk *chunk;
1780                         chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1781                         ret = read_one_chunk(root, &found_key, leaf, chunk);
1782                         BUG_ON(ret);
1783                 }
1784                 path->slots[0]++;
1785         }
1786
1787         ret = 0;
1788 error:
1789         btrfs_free_path(path);
1790         return ret;
1791 }
1792
1793 struct list_head *btrfs_scanned_uuids(void)
1794 {
1795         return &fs_uuids;
1796 }
1797
1798 static int rmw_eb(struct btrfs_fs_info *info,
1799                   struct extent_buffer *eb, struct extent_buffer *orig_eb)
1800 {
1801         int ret;
1802         unsigned long orig_off = 0;
1803         unsigned long dest_off = 0;
1804         unsigned long copy_len = eb->len;
1805
1806         ret = read_whole_eb(info, eb, 0);
1807         if (ret)
1808                 return ret;
1809
1810         if (eb->start + eb->len <= orig_eb->start ||
1811             eb->start >= orig_eb->start + orig_eb->len)
1812                 return 0;
1813         /*
1814          * | ----- orig_eb ------- |
1815          *         | ----- stripe -------  |
1816          *         | ----- orig_eb ------- |
1817          *              | ----- orig_eb ------- |
1818          */
1819         if (eb->start > orig_eb->start)
1820                 orig_off = eb->start - orig_eb->start;
1821         if (orig_eb->start > eb->start)
1822                 dest_off = orig_eb->start - eb->start;
1823
1824         if (copy_len > orig_eb->len - orig_off)
1825                 copy_len = orig_eb->len - orig_off;
1826         if (copy_len > eb->len - dest_off)
1827                 copy_len = eb->len - dest_off;
1828
1829         memcpy(eb->data + dest_off, orig_eb->data + orig_off, copy_len);
1830         return 0;
1831 }
1832
1833 static void split_eb_for_raid56(struct btrfs_fs_info *info,
1834                                 struct extent_buffer *orig_eb,
1835                                struct extent_buffer **ebs,
1836                                u64 stripe_len, u64 *raid_map,
1837                                int num_stripes)
1838 {
1839         struct extent_buffer *eb;
1840         u64 start = orig_eb->start;
1841         u64 this_eb_start;
1842         int i;
1843         int ret;
1844
1845         for (i = 0; i < num_stripes; i++) {
1846                 if (raid_map[i] >= BTRFS_RAID5_P_STRIPE)
1847                         break;
1848
1849                 eb = malloc(sizeof(struct extent_buffer) + stripe_len);
1850                 if (!eb)
1851                         BUG();
1852                 memset(eb, 0, sizeof(struct extent_buffer) + stripe_len);
1853
1854                 eb->start = raid_map[i];
1855                 eb->len = stripe_len;
1856                 eb->refs = 1;
1857                 eb->flags = 0;
1858                 eb->fd = -1;
1859                 eb->dev_bytenr = (u64)-1;
1860
1861                 this_eb_start = raid_map[i];
1862
1863                 if (start > this_eb_start ||
1864                     start + orig_eb->len < this_eb_start + stripe_len) {
1865                         ret = rmw_eb(info, eb, orig_eb);
1866                         BUG_ON(ret);
1867                 } else {
1868                         memcpy(eb->data, orig_eb->data + eb->start - start, stripe_len);
1869                 }
1870                 ebs[i] = eb;
1871         }
1872 }
1873
1874 int write_raid56_with_parity(struct btrfs_fs_info *info,
1875                              struct extent_buffer *eb,
1876                              struct btrfs_multi_bio *multi,
1877                              u64 stripe_len, u64 *raid_map)
1878 {
1879         struct extent_buffer **ebs, *p_eb = NULL, *q_eb = NULL;
1880         int i;
1881         int j;
1882         int ret;
1883         int alloc_size = eb->len;
1884
1885         ebs = kmalloc(sizeof(*ebs) * multi->num_stripes, GFP_NOFS);
1886         BUG_ON(!ebs);
1887
1888         if (stripe_len > alloc_size)
1889                 alloc_size = stripe_len;
1890
1891         split_eb_for_raid56(info, eb, ebs, stripe_len, raid_map,
1892                             multi->num_stripes);
1893
1894         for (i = 0; i < multi->num_stripes; i++) {
1895                 struct extent_buffer *new_eb;
1896                 if (raid_map[i] < BTRFS_RAID5_P_STRIPE) {
1897                         ebs[i]->dev_bytenr = multi->stripes[i].physical;
1898                         ebs[i]->fd = multi->stripes[i].dev->fd;
1899                         multi->stripes[i].dev->total_ios++;
1900                         BUG_ON(ebs[i]->start != raid_map[i]);
1901                         continue;
1902                 }
1903                 new_eb = kmalloc(sizeof(*eb) + alloc_size, GFP_NOFS);
1904                 BUG_ON(!new_eb);
1905                 new_eb->dev_bytenr = multi->stripes[i].physical;
1906                 new_eb->fd = multi->stripes[i].dev->fd;
1907                 multi->stripes[i].dev->total_ios++;
1908                 new_eb->len = stripe_len;
1909
1910                 if (raid_map[i] == BTRFS_RAID5_P_STRIPE)
1911                         p_eb = new_eb;
1912                 else if (raid_map[i] == BTRFS_RAID6_Q_STRIPE)
1913                         q_eb = new_eb;
1914         }
1915         if (q_eb) {
1916                 void **pointers;
1917
1918                 pointers = kmalloc(sizeof(*pointers) * multi->num_stripes,
1919                                    GFP_NOFS);
1920                 BUG_ON(!pointers);
1921
1922                 ebs[multi->num_stripes - 2] = p_eb;
1923                 ebs[multi->num_stripes - 1] = q_eb;
1924
1925                 for (i = 0; i < multi->num_stripes; i++)
1926                         pointers[i] = ebs[i]->data;
1927
1928                 raid6_gen_syndrome(multi->num_stripes, stripe_len, pointers);
1929                 kfree(pointers);
1930         } else {
1931                 ebs[multi->num_stripes - 1] = p_eb;
1932                 memcpy(p_eb->data, ebs[0]->data, stripe_len);
1933                 for (j = 1; j < multi->num_stripes - 1; j++) {
1934                         for (i = 0; i < stripe_len; i += sizeof(unsigned long)) {
1935                                 *(unsigned long *)(p_eb->data + i) ^=
1936                                         *(unsigned long *)(ebs[j]->data + i);
1937                         }
1938                 }
1939         }
1940
1941         for (i = 0; i < multi->num_stripes; i++) {
1942                 ret = write_extent_to_disk(ebs[i]);
1943                 BUG_ON(ret);
1944                 if (ebs[i] != eb)
1945                         kfree(ebs[i]);
1946         }
1947
1948         kfree(ebs);
1949
1950         return 0;
1951 }