btrfs-progs: use on-stack buffer for btrfs_scan_one_device
[btrfs-progs-unstable/devel.git] / volumes.c
blob00f3edf1a06938830ca17a707165439f6c394714
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <uuid/uuid.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include "ctree.h"
26 #include "disk-io.h"
27 #include "transaction.h"
28 #include "print-tree.h"
29 #include "volumes.h"
30 #include "utils.h"
32 struct stripe {
33 struct btrfs_device *dev;
34 u64 physical;
37 static inline int nr_parity_stripes(struct map_lookup *map)
39 if (map->type & BTRFS_BLOCK_GROUP_RAID5)
40 return 1;
41 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
42 return 2;
43 else
44 return 0;
47 static inline int nr_data_stripes(struct map_lookup *map)
49 return map->num_stripes - nr_parity_stripes(map);
52 #define is_parity_stripe(x) ( ((x) == BTRFS_RAID5_P_STRIPE) || ((x) == BTRFS_RAID6_Q_STRIPE) )
54 static LIST_HEAD(fs_uuids);
56 static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
57 u8 *uuid)
59 struct btrfs_device *dev;
60 struct list_head *cur;
62 list_for_each(cur, head) {
63 dev = list_entry(cur, struct btrfs_device, dev_list);
64 if (dev->devid == devid &&
65 !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE)) {
66 return dev;
69 return NULL;
72 static struct btrfs_fs_devices *find_fsid(u8 *fsid)
74 struct list_head *cur;
75 struct btrfs_fs_devices *fs_devices;
77 list_for_each(cur, &fs_uuids) {
78 fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
79 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
80 return fs_devices;
82 return NULL;
85 static int device_list_add(const char *path,
86 struct btrfs_super_block *disk_super,
87 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
89 struct btrfs_device *device;
90 struct btrfs_fs_devices *fs_devices;
91 u64 found_transid = btrfs_super_generation(disk_super);
93 fs_devices = find_fsid(disk_super->fsid);
94 if (!fs_devices) {
95 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
96 if (!fs_devices)
97 return -ENOMEM;
98 INIT_LIST_HEAD(&fs_devices->devices);
99 list_add(&fs_devices->list, &fs_uuids);
100 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
101 fs_devices->latest_devid = devid;
102 fs_devices->latest_trans = found_transid;
103 fs_devices->lowest_devid = (u64)-1;
104 device = NULL;
105 } else {
106 device = __find_device(&fs_devices->devices, devid,
107 disk_super->dev_item.uuid);
109 if (!device) {
110 device = kzalloc(sizeof(*device), GFP_NOFS);
111 if (!device) {
112 /* we can safely leave the fs_devices entry around */
113 return -ENOMEM;
115 device->fd = -1;
116 device->devid = devid;
117 device->generation = found_transid;
118 memcpy(device->uuid, disk_super->dev_item.uuid,
119 BTRFS_UUID_SIZE);
120 device->name = kstrdup(path, GFP_NOFS);
121 if (!device->name) {
122 kfree(device);
123 return -ENOMEM;
125 device->label = kstrdup(disk_super->label, GFP_NOFS);
126 if (!device->label) {
127 kfree(device->name);
128 kfree(device);
129 return -ENOMEM;
131 device->total_devs = btrfs_super_num_devices(disk_super);
132 device->super_bytes_used = btrfs_super_bytes_used(disk_super);
133 device->total_bytes =
134 btrfs_stack_device_total_bytes(&disk_super->dev_item);
135 device->bytes_used =
136 btrfs_stack_device_bytes_used(&disk_super->dev_item);
137 list_add(&device->dev_list, &fs_devices->devices);
138 device->fs_devices = fs_devices;
139 } else if (!device->name || strcmp(device->name, path)) {
140 char *name = strdup(path);
141 if (!name)
142 return -ENOMEM;
143 kfree(device->name);
144 device->name = name;
148 if (found_transid > fs_devices->latest_trans) {
149 fs_devices->latest_devid = devid;
150 fs_devices->latest_trans = found_transid;
152 if (fs_devices->lowest_devid > devid) {
153 fs_devices->lowest_devid = devid;
155 *fs_devices_ret = fs_devices;
156 return 0;
159 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
161 struct btrfs_fs_devices *seed_devices;
162 struct btrfs_device *device;
164 again:
165 while (!list_empty(&fs_devices->devices)) {
166 device = list_entry(fs_devices->devices.next,
167 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;
175 device->writeable = 0;
176 list_del(&device->dev_list);
177 /* free the memory */
178 free(device->name);
179 free(device->label);
180 free(device);
183 seed_devices = fs_devices->seed;
184 fs_devices->seed = NULL;
185 if (seed_devices) {
186 struct btrfs_fs_devices *orig;
188 orig = fs_devices;
189 fs_devices = seed_devices;
190 list_del(&orig->list);
191 free(orig);
192 goto again;
193 } else {
194 list_del(&fs_devices->list);
195 free(fs_devices);
198 return 0;
201 void btrfs_close_all_devices(void)
203 struct btrfs_fs_devices *fs_devices;
205 while (!list_empty(&fs_uuids)) {
206 fs_devices = list_entry(fs_uuids.next, struct btrfs_fs_devices,
207 list);
208 btrfs_close_devices(fs_devices);
212 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, int flags)
214 int fd;
215 struct list_head *head = &fs_devices->devices;
216 struct list_head *cur;
217 struct btrfs_device *device;
218 int ret;
220 list_for_each(cur, head) {
221 device = list_entry(cur, struct btrfs_device, dev_list);
222 if (!device->name) {
223 printk("no name for device %llu, skip it now\n", device->devid);
224 continue;
227 fd = open(device->name, flags);
228 if (fd < 0) {
229 ret = -errno;
230 goto fail;
233 if (posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED))
234 fprintf(stderr, "Warning, could not drop caches\n");
236 if (device->devid == fs_devices->latest_devid)
237 fs_devices->latest_bdev = fd;
238 if (device->devid == fs_devices->lowest_devid)
239 fs_devices->lowest_bdev = fd;
240 device->fd = fd;
241 if (flags & O_RDWR)
242 device->writeable = 1;
244 return 0;
245 fail:
246 btrfs_close_devices(fs_devices);
247 return ret;
250 int btrfs_scan_one_device(int fd, const char *path,
251 struct btrfs_fs_devices **fs_devices_ret,
252 u64 *total_devs, u64 super_offset, int super_recover)
254 struct btrfs_super_block *disk_super;
255 char buf[BTRFS_SUPER_INFO_SIZE];
256 int ret;
257 u64 devid;
259 disk_super = (struct btrfs_super_block *)buf;
260 ret = btrfs_read_dev_super(fd, disk_super, super_offset, super_recover);
261 if (ret < 0)
262 return -EIO;
263 devid = btrfs_stack_device_id(&disk_super->dev_item);
264 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_METADUMP)
265 *total_devs = 1;
266 else
267 *total_devs = btrfs_super_num_devices(disk_super);
269 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
271 return ret;
275 * this uses a pretty simple search, the expectation is that it is
276 * called very infrequently and that a given device has a small number
277 * of extents
279 static int find_free_dev_extent(struct btrfs_trans_handle *trans,
280 struct btrfs_device *device,
281 struct btrfs_path *path,
282 u64 num_bytes, u64 *start)
284 struct btrfs_key key;
285 struct btrfs_root *root = device->dev_root;
286 struct btrfs_dev_extent *dev_extent = NULL;
287 u64 hole_size = 0;
288 u64 last_byte = 0;
289 u64 search_start = root->fs_info->alloc_start;
290 u64 search_end = device->total_bytes;
291 int ret;
292 int slot = 0;
293 int start_found;
294 struct extent_buffer *l;
296 start_found = 0;
297 path->reada = 2;
299 /* FIXME use last free of some kind */
301 /* we don't want to overwrite the superblock on the drive,
302 * so we make sure to start at an offset of at least 1MB
304 search_start = max(BTRFS_BLOCK_RESERVED_1M_FOR_SUPER, search_start);
306 if (search_start >= search_end) {
307 ret = -ENOSPC;
308 goto error;
311 key.objectid = device->devid;
312 key.offset = search_start;
313 key.type = BTRFS_DEV_EXTENT_KEY;
314 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
315 if (ret < 0)
316 goto error;
317 ret = btrfs_previous_item(root, path, 0, key.type);
318 if (ret < 0)
319 goto error;
320 l = path->nodes[0];
321 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
322 while (1) {
323 l = path->nodes[0];
324 slot = path->slots[0];
325 if (slot >= btrfs_header_nritems(l)) {
326 ret = btrfs_next_leaf(root, path);
327 if (ret == 0)
328 continue;
329 if (ret < 0)
330 goto error;
331 no_more_items:
332 if (!start_found) {
333 if (search_start >= search_end) {
334 ret = -ENOSPC;
335 goto error;
337 *start = search_start;
338 start_found = 1;
339 goto check_pending;
341 *start = last_byte > search_start ?
342 last_byte : search_start;
343 if (search_end <= *start) {
344 ret = -ENOSPC;
345 goto error;
347 goto check_pending;
349 btrfs_item_key_to_cpu(l, &key, slot);
351 if (key.objectid < device->devid)
352 goto next;
354 if (key.objectid > device->devid)
355 goto no_more_items;
357 if (key.offset >= search_start && key.offset > last_byte &&
358 start_found) {
359 if (last_byte < search_start)
360 last_byte = search_start;
361 hole_size = key.offset - last_byte;
362 if (key.offset > last_byte &&
363 hole_size >= num_bytes) {
364 *start = last_byte;
365 goto check_pending;
368 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
369 goto next;
372 start_found = 1;
373 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
374 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
375 next:
376 path->slots[0]++;
377 cond_resched();
379 check_pending:
380 /* we have to make sure we didn't find an extent that has already
381 * been allocated by the map tree or the original allocation
383 btrfs_release_path(path);
384 BUG_ON(*start < search_start);
386 if (*start + num_bytes > search_end) {
387 ret = -ENOSPC;
388 goto error;
390 /* check for pending inserts here */
391 return 0;
393 error:
394 btrfs_release_path(path);
395 return ret;
398 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
399 struct btrfs_device *device,
400 u64 chunk_tree, u64 chunk_objectid,
401 u64 chunk_offset,
402 u64 num_bytes, u64 *start)
404 int ret;
405 struct btrfs_path *path;
406 struct btrfs_root *root = device->dev_root;
407 struct btrfs_dev_extent *extent;
408 struct extent_buffer *leaf;
409 struct btrfs_key key;
411 path = btrfs_alloc_path();
412 if (!path)
413 return -ENOMEM;
415 ret = find_free_dev_extent(trans, device, path, num_bytes, start);
416 if (ret) {
417 goto err;
420 key.objectid = device->devid;
421 key.offset = *start;
422 key.type = BTRFS_DEV_EXTENT_KEY;
423 ret = btrfs_insert_empty_item(trans, root, path, &key,
424 sizeof(*extent));
425 BUG_ON(ret);
427 leaf = path->nodes[0];
428 extent = btrfs_item_ptr(leaf, path->slots[0],
429 struct btrfs_dev_extent);
430 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
431 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
432 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
434 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
435 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
436 BTRFS_UUID_SIZE);
438 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
439 btrfs_mark_buffer_dirty(leaf);
440 err:
441 btrfs_free_path(path);
442 return ret;
445 static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
447 struct btrfs_path *path;
448 int ret;
449 struct btrfs_key key;
450 struct btrfs_chunk *chunk;
451 struct btrfs_key found_key;
453 path = btrfs_alloc_path();
454 BUG_ON(!path);
456 key.objectid = objectid;
457 key.offset = (u64)-1;
458 key.type = BTRFS_CHUNK_ITEM_KEY;
460 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
461 if (ret < 0)
462 goto error;
464 BUG_ON(ret == 0);
466 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
467 if (ret) {
468 *offset = 0;
469 } else {
470 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
471 path->slots[0]);
472 if (found_key.objectid != objectid)
473 *offset = 0;
474 else {
475 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
476 struct btrfs_chunk);
477 *offset = found_key.offset +
478 btrfs_chunk_length(path->nodes[0], chunk);
481 ret = 0;
482 error:
483 btrfs_free_path(path);
484 return ret;
487 static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
488 u64 *objectid)
490 int ret;
491 struct btrfs_key key;
492 struct btrfs_key found_key;
494 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
495 key.type = BTRFS_DEV_ITEM_KEY;
496 key.offset = (u64)-1;
498 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
499 if (ret < 0)
500 goto error;
502 BUG_ON(ret == 0);
504 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
505 BTRFS_DEV_ITEM_KEY);
506 if (ret) {
507 *objectid = 1;
508 } else {
509 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
510 path->slots[0]);
511 *objectid = found_key.offset + 1;
513 ret = 0;
514 error:
515 btrfs_release_path(path);
516 return ret;
520 * the device information is stored in the chunk root
521 * the btrfs_device struct should be fully filled in
523 int btrfs_add_device(struct btrfs_trans_handle *trans,
524 struct btrfs_root *root,
525 struct btrfs_device *device)
527 int ret;
528 struct btrfs_path *path;
529 struct btrfs_dev_item *dev_item;
530 struct extent_buffer *leaf;
531 struct btrfs_key key;
532 unsigned long ptr;
533 u64 free_devid = 0;
535 root = root->fs_info->chunk_root;
537 path = btrfs_alloc_path();
538 if (!path)
539 return -ENOMEM;
541 ret = find_next_devid(root, path, &free_devid);
542 if (ret)
543 goto out;
545 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
546 key.type = BTRFS_DEV_ITEM_KEY;
547 key.offset = free_devid;
549 ret = btrfs_insert_empty_item(trans, root, path, &key,
550 sizeof(*dev_item));
551 if (ret)
552 goto out;
554 leaf = path->nodes[0];
555 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
557 device->devid = free_devid;
558 btrfs_set_device_id(leaf, dev_item, device->devid);
559 btrfs_set_device_generation(leaf, dev_item, 0);
560 btrfs_set_device_type(leaf, dev_item, device->type);
561 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
562 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
563 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
564 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
565 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
566 btrfs_set_device_group(leaf, dev_item, 0);
567 btrfs_set_device_seek_speed(leaf, dev_item, 0);
568 btrfs_set_device_bandwidth(leaf, dev_item, 0);
569 btrfs_set_device_start_offset(leaf, dev_item, 0);
571 ptr = (unsigned long)btrfs_device_uuid(dev_item);
572 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
573 ptr = (unsigned long)btrfs_device_fsid(dev_item);
574 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
575 btrfs_mark_buffer_dirty(leaf);
576 ret = 0;
578 out:
579 btrfs_free_path(path);
580 return ret;
583 int btrfs_update_device(struct btrfs_trans_handle *trans,
584 struct btrfs_device *device)
586 int ret;
587 struct btrfs_path *path;
588 struct btrfs_root *root;
589 struct btrfs_dev_item *dev_item;
590 struct extent_buffer *leaf;
591 struct btrfs_key key;
593 root = device->dev_root->fs_info->chunk_root;
595 path = btrfs_alloc_path();
596 if (!path)
597 return -ENOMEM;
599 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
600 key.type = BTRFS_DEV_ITEM_KEY;
601 key.offset = device->devid;
603 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
604 if (ret < 0)
605 goto out;
607 if (ret > 0) {
608 ret = -ENOENT;
609 goto out;
612 leaf = path->nodes[0];
613 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
615 btrfs_set_device_id(leaf, dev_item, device->devid);
616 btrfs_set_device_type(leaf, dev_item, device->type);
617 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
618 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
619 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
620 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
621 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
622 btrfs_mark_buffer_dirty(leaf);
624 out:
625 btrfs_free_path(path);
626 return ret;
629 int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
630 struct btrfs_root *root,
631 struct btrfs_key *key,
632 struct btrfs_chunk *chunk, int item_size)
634 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
635 struct btrfs_disk_key disk_key;
636 u32 array_size;
637 u8 *ptr;
639 array_size = btrfs_super_sys_array_size(super_copy);
640 if (array_size + item_size + sizeof(disk_key)
641 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
642 return -EFBIG;
644 ptr = super_copy->sys_chunk_array + array_size;
645 btrfs_cpu_key_to_disk(&disk_key, key);
646 memcpy(ptr, &disk_key, sizeof(disk_key));
647 ptr += sizeof(disk_key);
648 memcpy(ptr, chunk, item_size);
649 item_size += sizeof(disk_key);
650 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
651 return 0;
654 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
655 int sub_stripes)
657 if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
658 return calc_size;
659 else if (type & BTRFS_BLOCK_GROUP_RAID10)
660 return calc_size * (num_stripes / sub_stripes);
661 else if (type & BTRFS_BLOCK_GROUP_RAID5)
662 return calc_size * (num_stripes - 1);
663 else if (type & BTRFS_BLOCK_GROUP_RAID6)
664 return calc_size * (num_stripes - 2);
665 else
666 return calc_size * num_stripes;
670 static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
672 /* TODO, add a way to store the preferred stripe size */
673 return BTRFS_STRIPE_LEN;
677 * btrfs_device_avail_bytes - count bytes available for alloc_chunk
679 * It is not equal to "device->total_bytes - device->bytes_used".
680 * We do not allocate any chunk in 1M at beginning of device, and not
681 * allowed to allocate any chunk before alloc_start if it is specified.
682 * So search holes from max(1M, alloc_start) to device->total_bytes.
684 static int btrfs_device_avail_bytes(struct btrfs_trans_handle *trans,
685 struct btrfs_device *device,
686 u64 *avail_bytes)
688 struct btrfs_path *path;
689 struct btrfs_root *root = device->dev_root;
690 struct btrfs_key key;
691 struct btrfs_dev_extent *dev_extent = NULL;
692 struct extent_buffer *l;
693 u64 search_start = root->fs_info->alloc_start;
694 u64 search_end = device->total_bytes;
695 u64 extent_end = 0;
696 u64 free_bytes = 0;
697 int ret;
698 int slot = 0;
700 search_start = max(BTRFS_BLOCK_RESERVED_1M_FOR_SUPER, search_start);
702 path = btrfs_alloc_path();
703 if (!path)
704 return -ENOMEM;
706 key.objectid = device->devid;
707 key.offset = root->fs_info->alloc_start;
708 key.type = BTRFS_DEV_EXTENT_KEY;
710 path->reada = 2;
711 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
712 if (ret < 0)
713 goto error;
714 ret = btrfs_previous_item(root, path, 0, key.type);
715 if (ret < 0)
716 goto error;
718 while (1) {
719 l = path->nodes[0];
720 slot = path->slots[0];
721 if (slot >= btrfs_header_nritems(l)) {
722 ret = btrfs_next_leaf(root, path);
723 if (ret == 0)
724 continue;
725 if (ret < 0)
726 goto error;
727 break;
729 btrfs_item_key_to_cpu(l, &key, slot);
731 if (key.objectid < device->devid)
732 goto next;
733 if (key.objectid > device->devid)
734 break;
735 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
736 goto next;
737 if (key.offset > search_end)
738 break;
739 if (key.offset > search_start)
740 free_bytes += key.offset - search_start;
742 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
743 extent_end = key.offset + btrfs_dev_extent_length(l,
744 dev_extent);
745 if (extent_end > search_start)
746 search_start = extent_end;
747 if (search_start > search_end)
748 break;
749 next:
750 path->slots[0]++;
751 cond_resched();
754 if (search_start < search_end)
755 free_bytes += search_end - search_start;
757 *avail_bytes = free_bytes;
758 ret = 0;
759 error:
760 btrfs_free_path(path);
761 return ret;
764 #define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
765 - sizeof(struct btrfs_item) \
766 - sizeof(struct btrfs_chunk)) \
767 / sizeof(struct btrfs_stripe) + 1)
769 #define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
770 - 2 * sizeof(struct btrfs_disk_key) \
771 - 2 * sizeof(struct btrfs_chunk)) \
772 / sizeof(struct btrfs_stripe) + 1)
774 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
775 struct btrfs_root *extent_root, u64 *start,
776 u64 *num_bytes, u64 type)
778 u64 dev_offset;
779 struct btrfs_fs_info *info = extent_root->fs_info;
780 struct btrfs_root *chunk_root = info->chunk_root;
781 struct btrfs_stripe *stripes;
782 struct btrfs_device *device = NULL;
783 struct btrfs_chunk *chunk;
784 struct list_head private_devs;
785 struct list_head *dev_list = &info->fs_devices->devices;
786 struct list_head *cur;
787 struct map_lookup *map;
788 int min_stripe_size = 1 * 1024 * 1024;
789 u64 calc_size = 8 * 1024 * 1024;
790 u64 min_free;
791 u64 max_chunk_size = 4 * calc_size;
792 u64 avail = 0;
793 u64 max_avail = 0;
794 u64 percent_max;
795 int num_stripes = 1;
796 int max_stripes = 0;
797 int min_stripes = 1;
798 int sub_stripes = 0;
799 int looped = 0;
800 int ret;
801 int index;
802 int stripe_len = BTRFS_STRIPE_LEN;
803 struct btrfs_key key;
804 u64 offset;
806 if (list_empty(dev_list)) {
807 return -ENOSPC;
810 if (type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
811 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
812 BTRFS_BLOCK_GROUP_RAID10 |
813 BTRFS_BLOCK_GROUP_DUP)) {
814 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
815 calc_size = 8 * 1024 * 1024;
816 max_chunk_size = calc_size * 2;
817 min_stripe_size = 1 * 1024 * 1024;
818 max_stripes = BTRFS_MAX_DEVS_SYS_CHUNK;
819 } else if (type & BTRFS_BLOCK_GROUP_DATA) {
820 calc_size = 1024 * 1024 * 1024;
821 max_chunk_size = 10 * calc_size;
822 min_stripe_size = 64 * 1024 * 1024;
823 max_stripes = BTRFS_MAX_DEVS(chunk_root);
824 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
825 calc_size = 1024 * 1024 * 1024;
826 max_chunk_size = 4 * calc_size;
827 min_stripe_size = 32 * 1024 * 1024;
828 max_stripes = BTRFS_MAX_DEVS(chunk_root);
831 if (type & BTRFS_BLOCK_GROUP_RAID1) {
832 num_stripes = min_t(u64, 2,
833 btrfs_super_num_devices(info->super_copy));
834 if (num_stripes < 2)
835 return -ENOSPC;
836 min_stripes = 2;
838 if (type & BTRFS_BLOCK_GROUP_DUP) {
839 num_stripes = 2;
840 min_stripes = 2;
842 if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
843 num_stripes = btrfs_super_num_devices(info->super_copy);
844 if (num_stripes > max_stripes)
845 num_stripes = max_stripes;
846 min_stripes = 2;
848 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
849 num_stripes = btrfs_super_num_devices(info->super_copy);
850 if (num_stripes > max_stripes)
851 num_stripes = max_stripes;
852 if (num_stripes < 4)
853 return -ENOSPC;
854 num_stripes &= ~(u32)1;
855 sub_stripes = 2;
856 min_stripes = 4;
858 if (type & (BTRFS_BLOCK_GROUP_RAID5)) {
859 num_stripes = btrfs_super_num_devices(info->super_copy);
860 if (num_stripes > max_stripes)
861 num_stripes = max_stripes;
862 if (num_stripes < 2)
863 return -ENOSPC;
864 min_stripes = 2;
865 stripe_len = find_raid56_stripe_len(num_stripes - 1,
866 btrfs_super_stripesize(info->super_copy));
868 if (type & (BTRFS_BLOCK_GROUP_RAID6)) {
869 num_stripes = btrfs_super_num_devices(info->super_copy);
870 if (num_stripes > max_stripes)
871 num_stripes = max_stripes;
872 if (num_stripes < 3)
873 return -ENOSPC;
874 min_stripes = 3;
875 stripe_len = find_raid56_stripe_len(num_stripes - 2,
876 btrfs_super_stripesize(info->super_copy));
879 /* we don't want a chunk larger than 10% of the FS */
880 percent_max = div_factor(btrfs_super_total_bytes(info->super_copy), 1);
881 max_chunk_size = min(percent_max, max_chunk_size);
883 again:
884 if (chunk_bytes_by_type(type, calc_size, num_stripes, sub_stripes) >
885 max_chunk_size) {
886 calc_size = max_chunk_size;
887 calc_size /= num_stripes;
888 calc_size /= stripe_len;
889 calc_size *= stripe_len;
891 /* we don't want tiny stripes */
892 calc_size = max_t(u64, calc_size, min_stripe_size);
894 calc_size /= stripe_len;
895 calc_size *= stripe_len;
896 INIT_LIST_HEAD(&private_devs);
897 cur = dev_list->next;
898 index = 0;
900 if (type & BTRFS_BLOCK_GROUP_DUP)
901 min_free = calc_size * 2;
902 else
903 min_free = calc_size;
905 /* build a private list of devices we will allocate from */
906 while(index < num_stripes) {
907 device = list_entry(cur, struct btrfs_device, dev_list);
908 ret = btrfs_device_avail_bytes(trans, device, &avail);
909 if (ret)
910 return ret;
911 cur = cur->next;
912 if (avail >= min_free) {
913 list_move_tail(&device->dev_list, &private_devs);
914 index++;
915 if (type & BTRFS_BLOCK_GROUP_DUP)
916 index++;
917 } else if (avail > max_avail)
918 max_avail = avail;
919 if (cur == dev_list)
920 break;
922 if (index < num_stripes) {
923 list_splice(&private_devs, dev_list);
924 if (index >= min_stripes) {
925 num_stripes = index;
926 if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
927 num_stripes /= sub_stripes;
928 num_stripes *= sub_stripes;
930 looped = 1;
931 goto again;
933 if (!looped && max_avail > 0) {
934 looped = 1;
935 calc_size = max_avail;
936 goto again;
938 return -ENOSPC;
940 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
941 &offset);
942 if (ret)
943 return ret;
944 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
945 key.type = BTRFS_CHUNK_ITEM_KEY;
946 key.offset = offset;
948 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
949 if (!chunk)
950 return -ENOMEM;
952 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
953 if (!map) {
954 kfree(chunk);
955 return -ENOMEM;
958 stripes = &chunk->stripe;
959 *num_bytes = chunk_bytes_by_type(type, calc_size,
960 num_stripes, sub_stripes);
961 index = 0;
962 while(index < num_stripes) {
963 struct btrfs_stripe *stripe;
964 BUG_ON(list_empty(&private_devs));
965 cur = private_devs.next;
966 device = list_entry(cur, struct btrfs_device, dev_list);
968 /* loop over this device again if we're doing a dup group */
969 if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
970 (index == num_stripes - 1))
971 list_move_tail(&device->dev_list, dev_list);
973 ret = btrfs_alloc_dev_extent(trans, device,
974 info->chunk_root->root_key.objectid,
975 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
976 calc_size, &dev_offset);
977 BUG_ON(ret);
979 device->bytes_used += calc_size;
980 ret = btrfs_update_device(trans, device);
981 BUG_ON(ret);
983 map->stripes[index].dev = device;
984 map->stripes[index].physical = dev_offset;
985 stripe = stripes + index;
986 btrfs_set_stack_stripe_devid(stripe, device->devid);
987 btrfs_set_stack_stripe_offset(stripe, dev_offset);
988 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
989 index++;
991 BUG_ON(!list_empty(&private_devs));
993 /* key was set above */
994 btrfs_set_stack_chunk_length(chunk, *num_bytes);
995 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
996 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
997 btrfs_set_stack_chunk_type(chunk, type);
998 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
999 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1000 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
1001 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
1002 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
1003 map->sector_size = extent_root->sectorsize;
1004 map->stripe_len = stripe_len;
1005 map->io_align = stripe_len;
1006 map->io_width = stripe_len;
1007 map->type = type;
1008 map->num_stripes = num_stripes;
1009 map->sub_stripes = sub_stripes;
1011 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1012 btrfs_chunk_item_size(num_stripes));
1013 BUG_ON(ret);
1014 *start = key.offset;;
1016 map->ce.start = key.offset;
1017 map->ce.size = *num_bytes;
1019 ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
1020 BUG_ON(ret);
1022 if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
1023 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
1024 chunk, btrfs_chunk_item_size(num_stripes));
1025 BUG_ON(ret);
1028 kfree(chunk);
1029 return ret;
1032 int btrfs_alloc_data_chunk(struct btrfs_trans_handle *trans,
1033 struct btrfs_root *extent_root, u64 *start,
1034 u64 num_bytes, u64 type)
1036 u64 dev_offset;
1037 struct btrfs_fs_info *info = extent_root->fs_info;
1038 struct btrfs_root *chunk_root = info->chunk_root;
1039 struct btrfs_stripe *stripes;
1040 struct btrfs_device *device = NULL;
1041 struct btrfs_chunk *chunk;
1042 struct list_head *dev_list = &info->fs_devices->devices;
1043 struct list_head *cur;
1044 struct map_lookup *map;
1045 u64 calc_size = 8 * 1024 * 1024;
1046 int num_stripes = 1;
1047 int sub_stripes = 0;
1048 int ret;
1049 int index;
1050 int stripe_len = BTRFS_STRIPE_LEN;
1051 struct btrfs_key key;
1053 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1054 key.type = BTRFS_CHUNK_ITEM_KEY;
1055 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1056 &key.offset);
1057 if (ret)
1058 return ret;
1060 chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
1061 if (!chunk)
1062 return -ENOMEM;
1064 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
1065 if (!map) {
1066 kfree(chunk);
1067 return -ENOMEM;
1070 stripes = &chunk->stripe;
1071 calc_size = num_bytes;
1073 index = 0;
1074 cur = dev_list->next;
1075 device = list_entry(cur, struct btrfs_device, dev_list);
1077 while (index < num_stripes) {
1078 struct btrfs_stripe *stripe;
1080 ret = btrfs_alloc_dev_extent(trans, device,
1081 info->chunk_root->root_key.objectid,
1082 BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
1083 calc_size, &dev_offset);
1084 BUG_ON(ret);
1086 device->bytes_used += calc_size;
1087 ret = btrfs_update_device(trans, device);
1088 BUG_ON(ret);
1090 map->stripes[index].dev = device;
1091 map->stripes[index].physical = dev_offset;
1092 stripe = stripes + index;
1093 btrfs_set_stack_stripe_devid(stripe, device->devid);
1094 btrfs_set_stack_stripe_offset(stripe, dev_offset);
1095 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1096 index++;
1099 /* key was set above */
1100 btrfs_set_stack_chunk_length(chunk, num_bytes);
1101 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
1102 btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
1103 btrfs_set_stack_chunk_type(chunk, type);
1104 btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
1105 btrfs_set_stack_chunk_io_align(chunk, stripe_len);
1106 btrfs_set_stack_chunk_io_width(chunk, stripe_len);
1107 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
1108 btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
1109 map->sector_size = extent_root->sectorsize;
1110 map->stripe_len = stripe_len;
1111 map->io_align = stripe_len;
1112 map->io_width = stripe_len;
1113 map->type = type;
1114 map->num_stripes = num_stripes;
1115 map->sub_stripes = sub_stripes;
1117 ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
1118 btrfs_chunk_item_size(num_stripes));
1119 BUG_ON(ret);
1120 *start = key.offset;
1122 map->ce.start = key.offset;
1123 map->ce.size = num_bytes;
1125 ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
1126 BUG_ON(ret);
1128 kfree(chunk);
1129 return ret;
1132 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
1134 struct cache_extent *ce;
1135 struct map_lookup *map;
1136 int ret;
1138 ce = search_cache_extent(&map_tree->cache_tree, logical);
1139 if (!ce) {
1140 fprintf(stderr, "No mapping for %llu-%llu\n",
1141 (unsigned long long)logical,
1142 (unsigned long long)logical+len);
1143 return 1;
1145 if (ce->start > logical || ce->start + ce->size < logical) {
1146 fprintf(stderr, "Invalid mapping for %llu-%llu, got "
1147 "%llu-%llu\n", (unsigned long long)logical,
1148 (unsigned long long)logical+len,
1149 (unsigned long long)ce->start,
1150 (unsigned long long)ce->start + ce->size);
1151 return 1;
1153 map = container_of(ce, struct map_lookup, ce);
1155 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
1156 ret = map->num_stripes;
1157 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1158 ret = map->sub_stripes;
1159 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
1160 ret = 2;
1161 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1162 ret = 3;
1163 else
1164 ret = 1;
1165 return ret;
1168 int btrfs_next_metadata(struct btrfs_mapping_tree *map_tree, u64 *logical,
1169 u64 *size)
1171 struct cache_extent *ce;
1172 struct map_lookup *map;
1174 ce = search_cache_extent(&map_tree->cache_tree, *logical);
1176 while (ce) {
1177 ce = next_cache_extent(ce);
1178 if (!ce)
1179 return -ENOENT;
1181 map = container_of(ce, struct map_lookup, ce);
1182 if (map->type & BTRFS_BLOCK_GROUP_METADATA) {
1183 *logical = ce->start;
1184 *size = ce->size;
1185 return 0;
1189 return -ENOENT;
1192 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
1193 u64 chunk_start, u64 physical, u64 devid,
1194 u64 **logical, int *naddrs, int *stripe_len)
1196 struct cache_extent *ce;
1197 struct map_lookup *map;
1198 u64 *buf;
1199 u64 bytenr;
1200 u64 length;
1201 u64 stripe_nr;
1202 u64 rmap_len;
1203 int i, j, nr = 0;
1205 ce = search_cache_extent(&map_tree->cache_tree, chunk_start);
1206 BUG_ON(!ce);
1207 map = container_of(ce, struct map_lookup, ce);
1209 length = ce->size;
1210 rmap_len = map->stripe_len;
1211 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
1212 length = ce->size / (map->num_stripes / map->sub_stripes);
1213 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
1214 length = ce->size / map->num_stripes;
1215 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1216 BTRFS_BLOCK_GROUP_RAID6)) {
1217 length = ce->size / nr_data_stripes(map);
1218 rmap_len = map->stripe_len * nr_data_stripes(map);
1221 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1223 for (i = 0; i < map->num_stripes; i++) {
1224 if (devid && map->stripes[i].dev->devid != devid)
1225 continue;
1226 if (map->stripes[i].physical > physical ||
1227 map->stripes[i].physical + length <= physical)
1228 continue;
1230 stripe_nr = (physical - map->stripes[i].physical) /
1231 map->stripe_len;
1233 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1234 stripe_nr = (stripe_nr * map->num_stripes + i) /
1235 map->sub_stripes;
1236 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
1237 stripe_nr = stripe_nr * map->num_stripes + i;
1238 } /* else if RAID[56], multiply by nr_data_stripes().
1239 * Alternatively, just use rmap_len below instead of
1240 * map->stripe_len */
1242 bytenr = ce->start + stripe_nr * rmap_len;
1243 for (j = 0; j < nr; j++) {
1244 if (buf[j] == bytenr)
1245 break;
1247 if (j == nr)
1248 buf[nr++] = bytenr;
1251 *logical = buf;
1252 *naddrs = nr;
1253 *stripe_len = rmap_len;
1255 return 0;
1258 static inline int parity_smaller(u64 a, u64 b)
1260 return a > b;
1263 /* Bubble-sort the stripe set to put the parity/syndrome stripes last */
1264 static void sort_parity_stripes(struct btrfs_multi_bio *bbio, u64 *raid_map)
1266 struct btrfs_bio_stripe s;
1267 int i;
1268 u64 l;
1269 int again = 1;
1271 while (again) {
1272 again = 0;
1273 for (i = 0; i < bbio->num_stripes - 1; i++) {
1274 if (parity_smaller(raid_map[i], raid_map[i+1])) {
1275 s = bbio->stripes[i];
1276 l = raid_map[i];
1277 bbio->stripes[i] = bbio->stripes[i+1];
1278 raid_map[i] = raid_map[i+1];
1279 bbio->stripes[i+1] = s;
1280 raid_map[i+1] = l;
1281 again = 1;
1287 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1288 u64 logical, u64 *length,
1289 struct btrfs_multi_bio **multi_ret, int mirror_num,
1290 u64 **raid_map_ret)
1292 return __btrfs_map_block(map_tree, rw, logical, length, NULL,
1293 multi_ret, mirror_num, raid_map_ret);
1296 int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
1297 u64 logical, u64 *length, u64 *type,
1298 struct btrfs_multi_bio **multi_ret, int mirror_num,
1299 u64 **raid_map_ret)
1301 struct cache_extent *ce;
1302 struct map_lookup *map;
1303 u64 offset;
1304 u64 stripe_offset;
1305 u64 stripe_nr;
1306 u64 *raid_map = NULL;
1307 int stripes_allocated = 8;
1308 int stripes_required = 1;
1309 int stripe_index;
1310 int i;
1311 struct btrfs_multi_bio *multi = NULL;
1313 if (multi_ret && rw == READ) {
1314 stripes_allocated = 1;
1316 again:
1317 ce = search_cache_extent(&map_tree->cache_tree, logical);
1318 if (!ce) {
1319 kfree(multi);
1320 *length = (u64)-1;
1321 return -ENOENT;
1323 if (ce->start > logical) {
1324 kfree(multi);
1325 *length = ce->start - logical;
1326 return -ENOENT;
1329 if (multi_ret) {
1330 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
1331 GFP_NOFS);
1332 if (!multi)
1333 return -ENOMEM;
1335 map = container_of(ce, struct map_lookup, ce);
1336 offset = logical - ce->start;
1338 if (rw == WRITE) {
1339 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
1340 BTRFS_BLOCK_GROUP_DUP)) {
1341 stripes_required = map->num_stripes;
1342 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1343 stripes_required = map->sub_stripes;
1346 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)
1347 && multi_ret && ((rw & WRITE) || mirror_num > 1) && raid_map_ret) {
1348 /* RAID[56] write or recovery. Return all stripes */
1349 stripes_required = map->num_stripes;
1351 /* Only allocate the map if we've already got a large enough multi_ret */
1352 if (stripes_allocated >= stripes_required) {
1353 raid_map = kmalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
1354 if (!raid_map) {
1355 kfree(multi);
1356 return -ENOMEM;
1361 /* if our multi bio struct is too small, back off and try again */
1362 if (multi_ret && stripes_allocated < stripes_required) {
1363 stripes_allocated = stripes_required;
1364 kfree(multi);
1365 multi = NULL;
1366 goto again;
1368 stripe_nr = offset;
1370 * stripe_nr counts the total number of stripes we have to stride
1371 * to get to this block
1373 stripe_nr = stripe_nr / map->stripe_len;
1375 stripe_offset = stripe_nr * map->stripe_len;
1376 BUG_ON(offset < stripe_offset);
1378 /* stripe_offset is the offset of this block in its stripe*/
1379 stripe_offset = offset - stripe_offset;
1381 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
1382 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
1383 BTRFS_BLOCK_GROUP_RAID10 |
1384 BTRFS_BLOCK_GROUP_DUP)) {
1385 /* we limit the length of each bio to what fits in a stripe */
1386 *length = min_t(u64, ce->size - offset,
1387 map->stripe_len - stripe_offset);
1388 } else {
1389 *length = ce->size - offset;
1392 if (!multi_ret)
1393 goto out;
1395 multi->num_stripes = 1;
1396 stripe_index = 0;
1397 if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
1398 if (rw == WRITE)
1399 multi->num_stripes = map->num_stripes;
1400 else if (mirror_num)
1401 stripe_index = mirror_num - 1;
1402 else
1403 stripe_index = stripe_nr % map->num_stripes;
1404 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
1405 int factor = map->num_stripes / map->sub_stripes;
1407 stripe_index = stripe_nr % factor;
1408 stripe_index *= map->sub_stripes;
1410 if (rw == WRITE)
1411 multi->num_stripes = map->sub_stripes;
1412 else if (mirror_num)
1413 stripe_index += mirror_num - 1;
1415 stripe_nr = stripe_nr / factor;
1416 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
1417 if (rw == WRITE)
1418 multi->num_stripes = map->num_stripes;
1419 else if (mirror_num)
1420 stripe_index = mirror_num - 1;
1421 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
1422 BTRFS_BLOCK_GROUP_RAID6)) {
1424 if (raid_map) {
1425 int rot;
1426 u64 tmp;
1427 u64 raid56_full_stripe_start;
1428 u64 full_stripe_len = nr_data_stripes(map) * map->stripe_len;
1431 * align the start of our data stripe in the logical
1432 * address space
1434 raid56_full_stripe_start = offset / full_stripe_len;
1435 raid56_full_stripe_start *= full_stripe_len;
1437 /* get the data stripe number */
1438 stripe_nr = raid56_full_stripe_start / map->stripe_len;
1439 stripe_nr = stripe_nr / nr_data_stripes(map);
1441 /* Work out the disk rotation on this stripe-set */
1442 rot = stripe_nr % map->num_stripes;
1444 /* Fill in the logical address of each stripe */
1445 tmp = stripe_nr * nr_data_stripes(map);
1447 for (i = 0; i < nr_data_stripes(map); i++)
1448 raid_map[(i+rot) % map->num_stripes] =
1449 ce->start + (tmp + i) * map->stripe_len;
1451 raid_map[(i+rot) % map->num_stripes] = BTRFS_RAID5_P_STRIPE;
1452 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
1453 raid_map[(i+rot+1) % map->num_stripes] = BTRFS_RAID6_Q_STRIPE;
1455 *length = map->stripe_len;
1456 stripe_index = 0;
1457 stripe_offset = 0;
1458 multi->num_stripes = map->num_stripes;
1459 } else {
1460 stripe_index = stripe_nr % nr_data_stripes(map);
1461 stripe_nr = stripe_nr / nr_data_stripes(map);
1464 * Mirror #0 or #1 means the original data block.
1465 * Mirror #2 is RAID5 parity block.
1466 * Mirror #3 is RAID6 Q block.
1468 if (mirror_num > 1)
1469 stripe_index = nr_data_stripes(map) + mirror_num - 2;
1471 /* We distribute the parity blocks across stripes */
1472 stripe_index = (stripe_nr + stripe_index) % map->num_stripes;
1474 } else {
1476 * after this do_div call, stripe_nr is the number of stripes
1477 * on this device we have to walk to find the data, and
1478 * stripe_index is the number of our device in the stripe array
1480 stripe_index = stripe_nr % map->num_stripes;
1481 stripe_nr = stripe_nr / map->num_stripes;
1483 BUG_ON(stripe_index >= map->num_stripes);
1485 for (i = 0; i < multi->num_stripes; i++) {
1486 multi->stripes[i].physical =
1487 map->stripes[stripe_index].physical + stripe_offset +
1488 stripe_nr * map->stripe_len;
1489 multi->stripes[i].dev = map->stripes[stripe_index].dev;
1490 stripe_index++;
1492 *multi_ret = multi;
1494 if (type)
1495 *type = map->type;
1497 if (raid_map) {
1498 sort_parity_stripes(multi, raid_map);
1499 *raid_map_ret = raid_map;
1501 out:
1502 return 0;
1505 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
1506 u8 *uuid, u8 *fsid)
1508 struct btrfs_device *device;
1509 struct btrfs_fs_devices *cur_devices;
1511 cur_devices = root->fs_info->fs_devices;
1512 while (cur_devices) {
1513 if (!fsid ||
1514 (!memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE) ||
1515 root->fs_info->ignore_fsid_mismatch)) {
1516 device = __find_device(&cur_devices->devices,
1517 devid, uuid);
1518 if (device)
1519 return device;
1521 cur_devices = cur_devices->seed;
1523 return NULL;
1526 struct btrfs_device *
1527 btrfs_find_device_by_devid(struct btrfs_fs_devices *fs_devices,
1528 u64 devid, int instance)
1530 struct list_head *head = &fs_devices->devices;
1531 struct btrfs_device *dev;
1532 int num_found = 0;
1534 list_for_each_entry(dev, head, dev_list) {
1535 if (dev->devid == devid && num_found++ == instance)
1536 return dev;
1538 return NULL;
1541 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
1543 struct cache_extent *ce;
1544 struct map_lookup *map;
1545 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1546 int readonly = 0;
1547 int i;
1550 * During chunk recovering, we may fail to find block group's
1551 * corresponding chunk, we will rebuild it later
1553 ce = search_cache_extent(&map_tree->cache_tree, chunk_offset);
1554 if (!root->fs_info->is_chunk_recover)
1555 BUG_ON(!ce);
1556 else
1557 return 0;
1559 map = container_of(ce, struct map_lookup, ce);
1560 for (i = 0; i < map->num_stripes; i++) {
1561 if (!map->stripes[i].dev->writeable) {
1562 readonly = 1;
1563 break;
1567 return readonly;
1570 static struct btrfs_device *fill_missing_device(u64 devid)
1572 struct btrfs_device *device;
1574 device = kzalloc(sizeof(*device), GFP_NOFS);
1575 device->devid = devid;
1576 device->fd = -1;
1577 return device;
1581 * Slot is used to verfy the chunk item is valid
1583 * For sys chunk in superblock, pass -1 to indicate sys chunk.
1585 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
1586 struct extent_buffer *leaf,
1587 struct btrfs_chunk *chunk, int slot)
1589 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1590 struct map_lookup *map;
1591 struct cache_extent *ce;
1592 u64 logical;
1593 u64 length;
1594 u64 devid;
1595 u8 uuid[BTRFS_UUID_SIZE];
1596 int num_stripes;
1597 int ret;
1598 int i;
1600 logical = key->offset;
1601 length = btrfs_chunk_length(leaf, chunk);
1603 ce = search_cache_extent(&map_tree->cache_tree, logical);
1605 /* already mapped? */
1606 if (ce && ce->start <= logical && ce->start + ce->size > logical) {
1607 return 0;
1610 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
1611 map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
1612 if (!map)
1613 return -ENOMEM;
1615 map->ce.start = logical;
1616 map->ce.size = length;
1617 map->num_stripes = num_stripes;
1618 map->io_width = btrfs_chunk_io_width(leaf, chunk);
1619 map->io_align = btrfs_chunk_io_align(leaf, chunk);
1620 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
1621 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
1622 map->type = btrfs_chunk_type(leaf, chunk);
1623 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
1625 /* Check on chunk item type */
1626 if (map->type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
1627 BTRFS_BLOCK_GROUP_PROFILE_MASK)) {
1628 fprintf(stderr, "Unknown chunk type bits: %llu\n",
1629 map->type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
1630 BTRFS_BLOCK_GROUP_PROFILE_MASK));
1631 ret = -EIO;
1632 goto out;
1636 * Btrfs_chunk contains at least one stripe, and for sys_chunk
1637 * it can't exceed the system chunk array size
1638 * For normal chunk, it should match its chunk item size.
1640 if (num_stripes < 1 ||
1641 (slot == -1 && sizeof(struct btrfs_stripe) * num_stripes >
1642 BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) ||
1643 (slot >= 0 && sizeof(struct btrfs_stripe) * (num_stripes - 1) >
1644 btrfs_item_size_nr(leaf, slot))) {
1645 fprintf(stderr, "Invalid num_stripes: %u\n",
1646 num_stripes);
1647 ret = -EIO;
1648 goto out;
1652 * Device number check against profile
1654 if ((map->type & BTRFS_BLOCK_GROUP_RAID10 && map->sub_stripes == 0) ||
1655 (map->type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
1656 (map->type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
1657 (map->type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
1658 (map->type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
1659 ((map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
1660 num_stripes != 1)) {
1661 fprintf(stderr,
1662 "Invalid num_stripes:sub_stripes %u:%u for profile %llu\n",
1663 num_stripes, map->sub_stripes,
1664 map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
1665 ret = -EIO;
1666 goto out;
1669 for (i = 0; i < num_stripes; i++) {
1670 map->stripes[i].physical =
1671 btrfs_stripe_offset_nr(leaf, chunk, i);
1672 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
1673 read_extent_buffer(leaf, uuid, (unsigned long)
1674 btrfs_stripe_dev_uuid_nr(chunk, i),
1675 BTRFS_UUID_SIZE);
1676 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
1677 NULL);
1678 if (!map->stripes[i].dev) {
1679 map->stripes[i].dev = fill_missing_device(devid);
1680 printf("warning, device %llu is missing\n",
1681 (unsigned long long)devid);
1685 ret = insert_cache_extent(&map_tree->cache_tree, &map->ce);
1686 BUG_ON(ret);
1688 return 0;
1689 out:
1690 free(map);
1691 return ret;
1694 static int fill_device_from_item(struct extent_buffer *leaf,
1695 struct btrfs_dev_item *dev_item,
1696 struct btrfs_device *device)
1698 unsigned long ptr;
1700 device->devid = btrfs_device_id(leaf, dev_item);
1701 device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
1702 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
1703 device->type = btrfs_device_type(leaf, dev_item);
1704 device->io_align = btrfs_device_io_align(leaf, dev_item);
1705 device->io_width = btrfs_device_io_width(leaf, dev_item);
1706 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
1708 ptr = (unsigned long)btrfs_device_uuid(dev_item);
1709 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1711 return 0;
1714 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
1716 struct btrfs_fs_devices *fs_devices;
1717 int ret;
1719 fs_devices = root->fs_info->fs_devices->seed;
1720 while (fs_devices) {
1721 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
1722 ret = 0;
1723 goto out;
1725 fs_devices = fs_devices->seed;
1728 fs_devices = find_fsid(fsid);
1729 if (!fs_devices) {
1730 /* missing all seed devices */
1731 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1732 if (!fs_devices) {
1733 ret = -ENOMEM;
1734 goto out;
1736 INIT_LIST_HEAD(&fs_devices->devices);
1737 list_add(&fs_devices->list, &fs_uuids);
1738 memcpy(fs_devices->fsid, fsid, BTRFS_FSID_SIZE);
1741 ret = btrfs_open_devices(fs_devices, O_RDONLY);
1742 if (ret)
1743 goto out;
1745 fs_devices->seed = root->fs_info->fs_devices->seed;
1746 root->fs_info->fs_devices->seed = fs_devices;
1747 out:
1748 return ret;
1751 static int read_one_dev(struct btrfs_root *root,
1752 struct extent_buffer *leaf,
1753 struct btrfs_dev_item *dev_item)
1755 struct btrfs_device *device;
1756 u64 devid;
1757 int ret = 0;
1758 u8 fs_uuid[BTRFS_UUID_SIZE];
1759 u8 dev_uuid[BTRFS_UUID_SIZE];
1761 devid = btrfs_device_id(leaf, dev_item);
1762 read_extent_buffer(leaf, dev_uuid,
1763 (unsigned long)btrfs_device_uuid(dev_item),
1764 BTRFS_UUID_SIZE);
1765 read_extent_buffer(leaf, fs_uuid,
1766 (unsigned long)btrfs_device_fsid(dev_item),
1767 BTRFS_UUID_SIZE);
1769 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
1770 ret = open_seed_devices(root, fs_uuid);
1771 if (ret)
1772 return ret;
1775 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1776 if (!device) {
1777 printk("warning devid %llu not found already\n",
1778 (unsigned long long)devid);
1779 device = kzalloc(sizeof(*device), GFP_NOFS);
1780 if (!device)
1781 return -ENOMEM;
1782 device->fd = -1;
1783 list_add(&device->dev_list,
1784 &root->fs_info->fs_devices->devices);
1787 fill_device_from_item(leaf, dev_item, device);
1788 device->dev_root = root->fs_info->dev_root;
1789 return ret;
1792 int btrfs_read_sys_array(struct btrfs_root *root)
1794 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
1795 struct extent_buffer *sb;
1796 struct btrfs_disk_key *disk_key;
1797 struct btrfs_chunk *chunk;
1798 struct btrfs_key key;
1799 u32 num_stripes;
1800 u32 len = 0;
1801 u8 *ptr;
1802 u8 *array_end;
1803 int ret = 0;
1805 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
1806 BTRFS_SUPER_INFO_SIZE);
1807 if (!sb)
1808 return -ENOMEM;
1809 btrfs_set_buffer_uptodate(sb);
1810 write_extent_buffer(sb, super_copy, 0, sizeof(*super_copy));
1811 array_end = ((u8 *)super_copy->sys_chunk_array) +
1812 btrfs_super_sys_array_size(super_copy);
1815 * we do this loop twice, once for the device items and
1816 * once for all of the chunks. This way there are device
1817 * structs filled in for every chunk
1819 ptr = super_copy->sys_chunk_array;
1821 while (ptr < array_end) {
1822 disk_key = (struct btrfs_disk_key *)ptr;
1823 btrfs_disk_key_to_cpu(&key, disk_key);
1825 len = sizeof(*disk_key);
1826 ptr += len;
1828 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1829 chunk = (struct btrfs_chunk *)(ptr - (u8 *)super_copy);
1830 ret = read_one_chunk(root, &key, sb, chunk, -1);
1831 if (ret)
1832 break;
1833 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
1834 len = btrfs_chunk_item_size(num_stripes);
1835 } else {
1836 BUG();
1838 ptr += len;
1840 free_extent_buffer(sb);
1841 return ret;
1844 int btrfs_read_chunk_tree(struct btrfs_root *root)
1846 struct btrfs_path *path;
1847 struct extent_buffer *leaf;
1848 struct btrfs_key key;
1849 struct btrfs_key found_key;
1850 int ret;
1851 int slot;
1853 root = root->fs_info->chunk_root;
1855 path = btrfs_alloc_path();
1856 if (!path)
1857 return -ENOMEM;
1860 * Read all device items, and then all the chunk items. All
1861 * device items are found before any chunk item (their object id
1862 * is smaller than the lowest possible object id for a chunk
1863 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
1865 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1866 key.offset = 0;
1867 key.type = 0;
1868 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1869 if (ret < 0)
1870 goto error;
1871 while(1) {
1872 leaf = path->nodes[0];
1873 slot = path->slots[0];
1874 if (slot >= btrfs_header_nritems(leaf)) {
1875 ret = btrfs_next_leaf(root, path);
1876 if (ret == 0)
1877 continue;
1878 if (ret < 0)
1879 goto error;
1880 break;
1882 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1883 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
1884 struct btrfs_dev_item *dev_item;
1885 dev_item = btrfs_item_ptr(leaf, slot,
1886 struct btrfs_dev_item);
1887 ret = read_one_dev(root, leaf, dev_item);
1888 BUG_ON(ret);
1889 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
1890 struct btrfs_chunk *chunk;
1891 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1892 ret = read_one_chunk(root, &found_key, leaf, chunk,
1893 slot);
1894 BUG_ON(ret);
1896 path->slots[0]++;
1899 ret = 0;
1900 error:
1901 btrfs_free_path(path);
1902 return ret;
1905 struct list_head *btrfs_scanned_uuids(void)
1907 return &fs_uuids;
1910 static int rmw_eb(struct btrfs_fs_info *info,
1911 struct extent_buffer *eb, struct extent_buffer *orig_eb)
1913 int ret;
1914 unsigned long orig_off = 0;
1915 unsigned long dest_off = 0;
1916 unsigned long copy_len = eb->len;
1918 ret = read_whole_eb(info, eb, 0);
1919 if (ret)
1920 return ret;
1922 if (eb->start + eb->len <= orig_eb->start ||
1923 eb->start >= orig_eb->start + orig_eb->len)
1924 return 0;
1926 * | ----- orig_eb ------- |
1927 * | ----- stripe ------- |
1928 * | ----- orig_eb ------- |
1929 * | ----- orig_eb ------- |
1931 if (eb->start > orig_eb->start)
1932 orig_off = eb->start - orig_eb->start;
1933 if (orig_eb->start > eb->start)
1934 dest_off = orig_eb->start - eb->start;
1936 if (copy_len > orig_eb->len - orig_off)
1937 copy_len = orig_eb->len - orig_off;
1938 if (copy_len > eb->len - dest_off)
1939 copy_len = eb->len - dest_off;
1941 memcpy(eb->data + dest_off, orig_eb->data + orig_off, copy_len);
1942 return 0;
1945 static void split_eb_for_raid56(struct btrfs_fs_info *info,
1946 struct extent_buffer *orig_eb,
1947 struct extent_buffer **ebs,
1948 u64 stripe_len, u64 *raid_map,
1949 int num_stripes)
1951 struct extent_buffer *eb;
1952 u64 start = orig_eb->start;
1953 u64 this_eb_start;
1954 int i;
1955 int ret;
1957 for (i = 0; i < num_stripes; i++) {
1958 if (raid_map[i] >= BTRFS_RAID5_P_STRIPE)
1959 break;
1961 eb = calloc(1, sizeof(struct extent_buffer) + stripe_len);
1962 if (!eb)
1963 BUG();
1965 eb->start = raid_map[i];
1966 eb->len = stripe_len;
1967 eb->refs = 1;
1968 eb->flags = 0;
1969 eb->fd = -1;
1970 eb->dev_bytenr = (u64)-1;
1972 this_eb_start = raid_map[i];
1974 if (start > this_eb_start ||
1975 start + orig_eb->len < this_eb_start + stripe_len) {
1976 ret = rmw_eb(info, eb, orig_eb);
1977 BUG_ON(ret);
1978 } else {
1979 memcpy(eb->data, orig_eb->data + eb->start - start, stripe_len);
1981 ebs[i] = eb;
1985 int write_raid56_with_parity(struct btrfs_fs_info *info,
1986 struct extent_buffer *eb,
1987 struct btrfs_multi_bio *multi,
1988 u64 stripe_len, u64 *raid_map)
1990 struct extent_buffer **ebs, *p_eb = NULL, *q_eb = NULL;
1991 int i;
1992 int j;
1993 int ret;
1994 int alloc_size = eb->len;
1996 ebs = kmalloc(sizeof(*ebs) * multi->num_stripes, GFP_NOFS);
1997 BUG_ON(!ebs);
1999 if (stripe_len > alloc_size)
2000 alloc_size = stripe_len;
2002 split_eb_for_raid56(info, eb, ebs, stripe_len, raid_map,
2003 multi->num_stripes);
2005 for (i = 0; i < multi->num_stripes; i++) {
2006 struct extent_buffer *new_eb;
2007 if (raid_map[i] < BTRFS_RAID5_P_STRIPE) {
2008 ebs[i]->dev_bytenr = multi->stripes[i].physical;
2009 ebs[i]->fd = multi->stripes[i].dev->fd;
2010 multi->stripes[i].dev->total_ios++;
2011 BUG_ON(ebs[i]->start != raid_map[i]);
2012 continue;
2014 new_eb = kmalloc(sizeof(*eb) + alloc_size, GFP_NOFS);
2015 BUG_ON(!new_eb);
2016 new_eb->dev_bytenr = multi->stripes[i].physical;
2017 new_eb->fd = multi->stripes[i].dev->fd;
2018 multi->stripes[i].dev->total_ios++;
2019 new_eb->len = stripe_len;
2021 if (raid_map[i] == BTRFS_RAID5_P_STRIPE)
2022 p_eb = new_eb;
2023 else if (raid_map[i] == BTRFS_RAID6_Q_STRIPE)
2024 q_eb = new_eb;
2026 if (q_eb) {
2027 void **pointers;
2029 pointers = kmalloc(sizeof(*pointers) * multi->num_stripes,
2030 GFP_NOFS);
2031 BUG_ON(!pointers);
2033 ebs[multi->num_stripes - 2] = p_eb;
2034 ebs[multi->num_stripes - 1] = q_eb;
2036 for (i = 0; i < multi->num_stripes; i++)
2037 pointers[i] = ebs[i]->data;
2039 raid6_gen_syndrome(multi->num_stripes, stripe_len, pointers);
2040 kfree(pointers);
2041 } else {
2042 ebs[multi->num_stripes - 1] = p_eb;
2043 memcpy(p_eb->data, ebs[0]->data, stripe_len);
2044 for (j = 1; j < multi->num_stripes - 1; j++) {
2045 for (i = 0; i < stripe_len; i += sizeof(unsigned long)) {
2046 *(unsigned long *)(p_eb->data + i) ^=
2047 *(unsigned long *)(ebs[j]->data + i);
2052 for (i = 0; i < multi->num_stripes; i++) {
2053 ret = write_extent_to_disk(ebs[i]);
2054 BUG_ON(ret);
2055 if (ebs[i] != eb)
2056 kfree(ebs[i]);
2059 kfree(ebs);
2061 return 0;