btrfs-progs: mkfs: delete un-used parameter fd
[platform/upstream/btrfs-progs.git] / extent_io.c
1
2 /*
3  * Copyright (C) 2007 Oracle.  All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public
7  * License v2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 021110-1307, USA.
18  */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include "kerncompat.h"
26 #include "extent_io.h"
27 #include "list.h"
28 #include "ctree.h"
29 #include "volumes.h"
30 #include "internal.h"
31
32 void extent_io_tree_init(struct extent_io_tree *tree)
33 {
34         cache_tree_init(&tree->state);
35         cache_tree_init(&tree->cache);
36         INIT_LIST_HEAD(&tree->lru);
37         tree->cache_size = 0;
38 }
39
40 static struct extent_state *alloc_extent_state(void)
41 {
42         struct extent_state *state;
43
44         state = malloc(sizeof(*state));
45         if (!state)
46                 return NULL;
47         state->cache_node.objectid = 0;
48         state->refs = 1;
49         state->state = 0;
50         state->xprivate = 0;
51         return state;
52 }
53
54 static void btrfs_free_extent_state(struct extent_state *state)
55 {
56         state->refs--;
57         BUG_ON(state->refs < 0);
58         if (state->refs == 0)
59                 free(state);
60 }
61
62 static void free_extent_state_func(struct cache_extent *cache)
63 {
64         struct extent_state *es;
65
66         es = container_of(cache, struct extent_state, cache_node);
67         btrfs_free_extent_state(es);
68 }
69
70 void extent_io_tree_cleanup(struct extent_io_tree *tree)
71 {
72         struct extent_buffer *eb;
73
74         while(!list_empty(&tree->lru)) {
75                 eb = list_entry(tree->lru.next, struct extent_buffer, lru);
76                 fprintf(stderr, "extent buffer leak: "
77                         "start %llu len %u\n",
78                         (unsigned long long)eb->start, eb->len);
79                 free_extent_buffer(eb);
80         }
81
82         cache_tree_free_extents(&tree->state, free_extent_state_func);
83 }
84
85 static inline void update_extent_state(struct extent_state *state)
86 {
87         state->cache_node.start = state->start;
88         state->cache_node.size = state->end + 1 - state->start;
89 }
90
91 /*
92  * Utility function to look for merge candidates inside a given range.
93  * Any extents with matching state are merged together into a single
94  * extent in the tree. Extents with EXTENT_IO in their state field are
95  * not merged
96  */
97 static int merge_state(struct extent_io_tree *tree,
98                        struct extent_state *state)
99 {
100         struct extent_state *other;
101         struct cache_extent *other_node;
102
103         if (state->state & EXTENT_IOBITS)
104                 return 0;
105
106         other_node = prev_cache_extent(&state->cache_node);
107         if (other_node) {
108                 other = container_of(other_node, struct extent_state,
109                                      cache_node);
110                 if (other->end == state->start - 1 &&
111                     other->state == state->state) {
112                         state->start = other->start;
113                         update_extent_state(state);
114                         remove_cache_extent(&tree->state, &other->cache_node);
115                         btrfs_free_extent_state(other);
116                 }
117         }
118         other_node = next_cache_extent(&state->cache_node);
119         if (other_node) {
120                 other = container_of(other_node, struct extent_state,
121                                      cache_node);
122                 if (other->start == state->end + 1 &&
123                     other->state == state->state) {
124                         other->start = state->start;
125                         update_extent_state(other);
126                         remove_cache_extent(&tree->state, &state->cache_node);
127                         btrfs_free_extent_state(state);
128                 }
129         }
130         return 0;
131 }
132
133 /*
134  * insert an extent_state struct into the tree.  'bits' are set on the
135  * struct before it is inserted.
136  */
137 static int insert_state(struct extent_io_tree *tree,
138                         struct extent_state *state, u64 start, u64 end,
139                         int bits)
140 {
141         int ret;
142
143         BUG_ON(end < start);
144         state->state |= bits;
145         state->start = start;
146         state->end = end;
147         update_extent_state(state);
148         ret = insert_cache_extent(&tree->state, &state->cache_node);
149         BUG_ON(ret);
150         merge_state(tree, state);
151         return 0;
152 }
153
154 /*
155  * split a given extent state struct in two, inserting the preallocated
156  * struct 'prealloc' as the newly created second half.  'split' indicates an
157  * offset inside 'orig' where it should be split.
158  */
159 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
160                        struct extent_state *prealloc, u64 split)
161 {
162         int ret;
163         prealloc->start = orig->start;
164         prealloc->end = split - 1;
165         prealloc->state = orig->state;
166         update_extent_state(prealloc);
167         orig->start = split;
168         update_extent_state(orig);
169         ret = insert_cache_extent(&tree->state, &prealloc->cache_node);
170         BUG_ON(ret);
171         return 0;
172 }
173
174 /*
175  * clear some bits on a range in the tree.
176  */
177 static int clear_state_bit(struct extent_io_tree *tree,
178                             struct extent_state *state, int bits)
179 {
180         int ret = state->state & bits;
181
182         state->state &= ~bits;
183         if (state->state == 0) {
184                 remove_cache_extent(&tree->state, &state->cache_node);
185                 btrfs_free_extent_state(state);
186         } else {
187                 merge_state(tree, state);
188         }
189         return ret;
190 }
191
192 /*
193  * clear some bits on a range in the tree.
194  */
195 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, int bits)
196 {
197         struct extent_state *state;
198         struct extent_state *prealloc = NULL;
199         struct cache_extent *node;
200         u64 last_end;
201         int err;
202         int set = 0;
203
204 again:
205         if (!prealloc) {
206                 prealloc = alloc_extent_state();
207                 if (!prealloc)
208                         return -ENOMEM;
209         }
210
211         /*
212          * this search will find the extents that end after
213          * our range starts
214          */
215         node = search_cache_extent(&tree->state, start);
216         if (!node)
217                 goto out;
218         state = container_of(node, struct extent_state, cache_node);
219         if (state->start > end)
220                 goto out;
221         last_end = state->end;
222
223         /*
224          *     | ---- desired range ---- |
225          *  | state | or
226          *  | ------------- state -------------- |
227          *
228          * We need to split the extent we found, and may flip
229          * bits on second half.
230          *
231          * If the extent we found extends past our range, we
232          * just split and search again.  It'll get split again
233          * the next time though.
234          *
235          * If the extent we found is inside our range, we clear
236          * the desired bit on it.
237          */
238         if (state->start < start) {
239                 err = split_state(tree, state, prealloc, start);
240                 BUG_ON(err == -EEXIST);
241                 prealloc = NULL;
242                 if (err)
243                         goto out;
244                 if (state->end <= end) {
245                         set |= clear_state_bit(tree, state, bits);
246                         if (last_end == (u64)-1)
247                                 goto out;
248                         start = last_end + 1;
249                 } else {
250                         start = state->start;
251                 }
252                 goto search_again;
253         }
254         /*
255          * | ---- desired range ---- |
256          *                        | state |
257          * We need to split the extent, and clear the bit
258          * on the first half
259          */
260         if (state->start <= end && state->end > end) {
261                 err = split_state(tree, state, prealloc, end + 1);
262                 BUG_ON(err == -EEXIST);
263
264                 set |= clear_state_bit(tree, prealloc, bits);
265                 prealloc = NULL;
266                 goto out;
267         }
268
269         start = state->end + 1;
270         set |= clear_state_bit(tree, state, bits);
271         if (last_end == (u64)-1)
272                 goto out;
273         start = last_end + 1;
274         goto search_again;
275 out:
276         if (prealloc)
277                 btrfs_free_extent_state(prealloc);
278         return set;
279
280 search_again:
281         if (start > end)
282                 goto out;
283         goto again;
284 }
285
286 /*
287  * set some bits on a range in the tree.
288  */
289 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, int bits)
290 {
291         struct extent_state *state;
292         struct extent_state *prealloc = NULL;
293         struct cache_extent *node;
294         int err = 0;
295         u64 last_start;
296         u64 last_end;
297 again:
298         if (!prealloc) {
299                 prealloc = alloc_extent_state();
300                 if (!prealloc)
301                         return -ENOMEM;
302         }
303
304         /*
305          * this search will find the extents that end after
306          * our range starts
307          */
308         node = search_cache_extent(&tree->state, start);
309         if (!node) {
310                 err = insert_state(tree, prealloc, start, end, bits);
311                 BUG_ON(err == -EEXIST);
312                 prealloc = NULL;
313                 goto out;
314         }
315
316         state = container_of(node, struct extent_state, cache_node);
317         last_start = state->start;
318         last_end = state->end;
319
320         /*
321          * | ---- desired range ---- |
322          * | state |
323          *
324          * Just lock what we found and keep going
325          */
326         if (state->start == start && state->end <= end) {
327                 state->state |= bits;
328                 merge_state(tree, state);
329                 if (last_end == (u64)-1)
330                         goto out;
331                 start = last_end + 1;
332                 goto search_again;
333         }
334         /*
335          *     | ---- desired range ---- |
336          * | state |
337          *   or
338          * | ------------- state -------------- |
339          *
340          * We need to split the extent we found, and may flip bits on
341          * second half.
342          *
343          * If the extent we found extends past our
344          * range, we just split and search again.  It'll get split
345          * again the next time though.
346          *
347          * If the extent we found is inside our range, we set the
348          * desired bit on it.
349          */
350         if (state->start < start) {
351                 err = split_state(tree, state, prealloc, start);
352                 BUG_ON(err == -EEXIST);
353                 prealloc = NULL;
354                 if (err)
355                         goto out;
356                 if (state->end <= end) {
357                         state->state |= bits;
358                         start = state->end + 1;
359                         merge_state(tree, state);
360                         if (last_end == (u64)-1)
361                                 goto out;
362                         start = last_end + 1;
363                 } else {
364                         start = state->start;
365                 }
366                 goto search_again;
367         }
368         /*
369          * | ---- desired range ---- |
370          *     | state | or               | state |
371          *
372          * There's a hole, we need to insert something in it and
373          * ignore the extent we found.
374          */
375         if (state->start > start) {
376                 u64 this_end;
377                 if (end < last_start)
378                         this_end = end;
379                 else
380                         this_end = last_start -1;
381                 err = insert_state(tree, prealloc, start, this_end,
382                                 bits);
383                 BUG_ON(err == -EEXIST);
384                 prealloc = NULL;
385                 if (err)
386                         goto out;
387                 start = this_end + 1;
388                 goto search_again;
389         }
390         /*
391          * | ---- desired range ---- |
392          * | ---------- state ---------- |
393          * We need to split the extent, and set the bit
394          * on the first half
395          */
396         err = split_state(tree, state, prealloc, end + 1);
397         BUG_ON(err == -EEXIST);
398
399         state->state |= bits;
400         merge_state(tree, prealloc);
401         prealloc = NULL;
402 out:
403         if (prealloc)
404                 btrfs_free_extent_state(prealloc);
405         return err;
406 search_again:
407         if (start > end)
408                 goto out;
409         goto again;
410 }
411
412 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end)
413 {
414         return set_extent_bits(tree, start, end, EXTENT_DIRTY);
415 }
416
417 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end)
418 {
419         return clear_extent_bits(tree, start, end, EXTENT_DIRTY);
420 }
421
422 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
423                           u64 *start_ret, u64 *end_ret, int bits)
424 {
425         struct cache_extent *node;
426         struct extent_state *state;
427         int ret = 1;
428
429         /*
430          * this search will find all the extents that end after
431          * our range starts.
432          */
433         node = search_cache_extent(&tree->state, start);
434         if (!node)
435                 goto out;
436
437         while(1) {
438                 state = container_of(node, struct extent_state, cache_node);
439                 if (state->end >= start && (state->state & bits)) {
440                         *start_ret = state->start;
441                         *end_ret = state->end;
442                         ret = 0;
443                         break;
444                 }
445                 node = next_cache_extent(node);
446                 if (!node)
447                         break;
448         }
449 out:
450         return ret;
451 }
452
453 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
454                    int bits, int filled)
455 {
456         struct extent_state *state = NULL;
457         struct cache_extent *node;
458         int bitset = 0;
459
460         node = search_cache_extent(&tree->state, start);
461         while (node && start <= end) {
462                 state = container_of(node, struct extent_state, cache_node);
463
464                 if (filled && state->start > start) {
465                         bitset = 0;
466                         break;
467                 }
468                 if (state->start > end)
469                         break;
470                 if (state->state & bits) {
471                         bitset = 1;
472                         if (!filled)
473                                 break;
474                 } else if (filled) {
475                         bitset = 0;
476                         break;
477                 }
478                 start = state->end + 1;
479                 if (start > end)
480                         break;
481                 node = next_cache_extent(node);
482                 if (!node) {
483                         if (filled)
484                                 bitset = 0;
485                         break;
486                 }
487         }
488         return bitset;
489 }
490
491 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
492 {
493         struct cache_extent *node;
494         struct extent_state *state;
495         int ret = 0;
496
497         node = search_cache_extent(&tree->state, start);
498         if (!node) {
499                 ret = -ENOENT;
500                 goto out;
501         }
502         state = container_of(node, struct extent_state, cache_node);
503         if (state->start != start) {
504                 ret = -ENOENT;
505                 goto out;
506         }
507         state->xprivate = private;
508 out:
509         return ret;
510 }
511
512 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
513 {
514         struct cache_extent *node;
515         struct extent_state *state;
516         int ret = 0;
517
518         node = search_cache_extent(&tree->state, start);
519         if (!node) {
520                 ret = -ENOENT;
521                 goto out;
522         }
523         state = container_of(node, struct extent_state, cache_node);
524         if (state->start != start) {
525                 ret = -ENOENT;
526                 goto out;
527         }
528         *private = state->xprivate;
529 out:
530         return ret;
531 }
532
533 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
534                                                    u64 bytenr, u32 blocksize)
535 {
536         struct extent_buffer *eb;
537
538         eb = calloc(1, sizeof(struct extent_buffer) + blocksize);
539         if (!eb)
540                 return NULL;
541
542         eb->start = bytenr;
543         eb->len = blocksize;
544         eb->refs = 1;
545         eb->flags = 0;
546         eb->tree = tree;
547         eb->fd = -1;
548         eb->dev_bytenr = (u64)-1;
549         eb->cache_node.start = bytenr;
550         eb->cache_node.size = blocksize;
551         INIT_LIST_HEAD(&eb->recow);
552
553         return eb;
554 }
555
556 struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
557 {
558         struct extent_buffer *new;
559
560         new = __alloc_extent_buffer(NULL, src->start, src->len);
561         if (!new)
562                 return NULL;
563
564         copy_extent_buffer(new, src, 0, 0, src->len);
565         new->flags |= EXTENT_BUFFER_DUMMY;
566
567         return new;
568 }
569
570 void free_extent_buffer(struct extent_buffer *eb)
571 {
572         if (!eb || IS_ERR(eb))
573                 return;
574
575         eb->refs--;
576         BUG_ON(eb->refs < 0);
577         if (eb->refs == 0) {
578                 struct extent_io_tree *tree = eb->tree;
579                 BUG_ON(eb->flags & EXTENT_DIRTY);
580                 list_del_init(&eb->lru);
581                 list_del_init(&eb->recow);
582                 if (!(eb->flags & EXTENT_BUFFER_DUMMY)) {
583                         BUG_ON(tree->cache_size < eb->len);
584                         remove_cache_extent(&tree->cache, &eb->cache_node);
585                         tree->cache_size -= eb->len;
586                 }
587                 free(eb);
588         }
589 }
590
591 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
592                                          u64 bytenr, u32 blocksize)
593 {
594         struct extent_buffer *eb = NULL;
595         struct cache_extent *cache;
596
597         cache = lookup_cache_extent(&tree->cache, bytenr, blocksize);
598         if (cache && cache->start == bytenr &&
599             cache->size == blocksize) {
600                 eb = container_of(cache, struct extent_buffer, cache_node);
601                 list_move_tail(&eb->lru, &tree->lru);
602                 eb->refs++;
603         }
604         return eb;
605 }
606
607 struct extent_buffer *find_first_extent_buffer(struct extent_io_tree *tree,
608                                                u64 start)
609 {
610         struct extent_buffer *eb = NULL;
611         struct cache_extent *cache;
612
613         cache = search_cache_extent(&tree->cache, start);
614         if (cache) {
615                 eb = container_of(cache, struct extent_buffer, cache_node);
616                 list_move_tail(&eb->lru, &tree->lru);
617                 eb->refs++;
618         }
619         return eb;
620 }
621
622 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
623                                           u64 bytenr, u32 blocksize)
624 {
625         struct extent_buffer *eb;
626         struct cache_extent *cache;
627
628         cache = lookup_cache_extent(&tree->cache, bytenr, blocksize);
629         if (cache && cache->start == bytenr &&
630             cache->size == blocksize) {
631                 eb = container_of(cache, struct extent_buffer, cache_node);
632                 list_move_tail(&eb->lru, &tree->lru);
633                 eb->refs++;
634         } else {
635                 int ret;
636
637                 if (cache) {
638                         eb = container_of(cache, struct extent_buffer,
639                                           cache_node);
640                         free_extent_buffer(eb);
641                 }
642                 eb = __alloc_extent_buffer(tree, bytenr, blocksize);
643                 if (!eb)
644                         return NULL;
645                 ret = insert_cache_extent(&tree->cache, &eb->cache_node);
646                 if (ret) {
647                         free(eb);
648                         return NULL;
649                 }
650                 list_add_tail(&eb->lru, &tree->lru);
651                 tree->cache_size += blocksize;
652         }
653         return eb;
654 }
655
656 int read_extent_from_disk(struct extent_buffer *eb,
657                           unsigned long offset, unsigned long len)
658 {
659         int ret;
660         ret = pread(eb->fd, eb->data + offset, len, eb->dev_bytenr);
661         if (ret < 0) {
662                 ret = -errno;
663                 goto out;
664         }
665         if (ret != len) {
666                 ret = -EIO;
667                 goto out;
668         }
669         ret = 0;
670 out:
671         return ret;
672 }
673
674 int write_extent_to_disk(struct extent_buffer *eb)
675 {
676         int ret;
677         ret = pwrite(eb->fd, eb->data, eb->len, eb->dev_bytenr);
678         if (ret < 0)
679                 goto out;
680         if (ret != eb->len) {
681                 ret = -EIO;
682                 goto out;
683         }
684         ret = 0;
685 out:
686         return ret;
687 }
688
689 int read_data_from_disk(struct btrfs_fs_info *info, void *buf, u64 offset,
690                         u64 bytes, int mirror)
691 {
692         struct btrfs_multi_bio *multi = NULL;
693         struct btrfs_device *device;
694         u64 bytes_left = bytes;
695         u64 read_len;
696         u64 total_read = 0;
697         int ret;
698
699         while (bytes_left) {
700                 read_len = bytes_left;
701                 ret = btrfs_map_block(info, READ, offset, &read_len, &multi,
702                                       mirror, NULL);
703                 if (ret) {
704                         fprintf(stderr, "Couldn't map the block %Lu\n",
705                                 offset);
706                         return -EIO;
707                 }
708                 device = multi->stripes[0].dev;
709
710                 read_len = min(bytes_left, read_len);
711                 if (device->fd <= 0) {
712                         kfree(multi);
713                         return -EIO;
714                 }
715
716                 ret = pread(device->fd, buf + total_read, read_len,
717                             multi->stripes[0].physical);
718                 kfree(multi);
719                 if (ret < 0) {
720                         fprintf(stderr, "Error reading %Lu, %d\n", offset,
721                                 ret);
722                         return ret;
723                 }
724                 if (ret != read_len) {
725                         fprintf(stderr, "Short read for %Lu, read %d, "
726                                 "read_len %Lu\n", offset, ret, read_len);
727                         return -EIO;
728                 }
729
730                 bytes_left -= read_len;
731                 offset += read_len;
732                 total_read += read_len;
733         }
734
735         return 0;
736 }
737
738 int write_data_to_disk(struct btrfs_fs_info *info, void *buf, u64 offset,
739                       u64 bytes, int mirror)
740 {
741         struct btrfs_multi_bio *multi = NULL;
742         struct btrfs_device *device;
743         u64 bytes_left = bytes;
744         u64 this_len;
745         u64 total_write = 0;
746         u64 *raid_map = NULL;
747         u64 dev_bytenr;
748         int dev_nr;
749         int ret = 0;
750
751         while (bytes_left > 0) {
752                 this_len = bytes_left;
753                 dev_nr = 0;
754
755                 ret = btrfs_map_block(info, WRITE, offset, &this_len, &multi,
756                                       mirror, &raid_map);
757                 if (ret) {
758                         fprintf(stderr, "Couldn't map the block %Lu\n",
759                                 offset);
760                         return -EIO;
761                 }
762
763                 if (raid_map) {
764                         struct extent_buffer *eb;
765                         u64 stripe_len = this_len;
766
767                         this_len = min(this_len, bytes_left);
768                         this_len = min(this_len, (u64)info->nodesize);
769
770                         eb = malloc(sizeof(struct extent_buffer) + this_len);
771                         if (!eb) {
772                                 fprintf(stderr, "cannot allocate memory for eb\n");
773                                 ret = -ENOMEM;
774                                 goto out;
775                         }
776
777                         memset(eb, 0, sizeof(struct extent_buffer) + this_len);
778                         eb->start = offset;
779                         eb->len = this_len;
780
781                         memcpy(eb->data, buf + total_write, this_len);
782                         ret = write_raid56_with_parity(info, eb, multi,
783                                                        stripe_len, raid_map);
784                         BUG_ON(ret);
785
786                         free(eb);
787                         kfree(raid_map);
788                         raid_map = NULL;
789                 } else while (dev_nr < multi->num_stripes) {
790                         device = multi->stripes[dev_nr].dev;
791                         if (device->fd <= 0) {
792                                 kfree(multi);
793                                 return -EIO;
794                         }
795
796                         dev_bytenr = multi->stripes[dev_nr].physical;
797                         this_len = min(this_len, bytes_left);
798                         dev_nr++;
799
800                         ret = pwrite(device->fd, buf + total_write, this_len, dev_bytenr);
801                         if (ret != this_len) {
802                                 if (ret < 0) {
803                                         fprintf(stderr, "Error writing to "
804                                                 "device %d\n", errno);
805                                         ret = errno;
806                                         kfree(multi);
807                                         return ret;
808                                 } else {
809                                         fprintf(stderr, "Short write\n");
810                                         kfree(multi);
811                                         return -EIO;
812                                 }
813                         }
814                 }
815
816                 BUG_ON(bytes_left < this_len);
817
818                 bytes_left -= this_len;
819                 offset += this_len;
820                 total_write += this_len;
821
822                 kfree(multi);
823                 multi = NULL;
824         }
825         return 0;
826
827 out:
828         kfree(raid_map);
829         return ret;
830 }
831
832 int set_extent_buffer_dirty(struct extent_buffer *eb)
833 {
834         struct extent_io_tree *tree = eb->tree;
835         if (!(eb->flags & EXTENT_DIRTY)) {
836                 eb->flags |= EXTENT_DIRTY;
837                 set_extent_dirty(tree, eb->start, eb->start + eb->len - 1);
838                 extent_buffer_get(eb);
839         }
840         return 0;
841 }
842
843 int clear_extent_buffer_dirty(struct extent_buffer *eb)
844 {
845         struct extent_io_tree *tree = eb->tree;
846         if (eb->flags & EXTENT_DIRTY) {
847                 eb->flags &= ~EXTENT_DIRTY;
848                 clear_extent_dirty(tree, eb->start, eb->start + eb->len - 1);
849                 free_extent_buffer(eb);
850         }
851         return 0;
852 }
853
854 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
855                          unsigned long start, unsigned long len)
856 {
857         return memcmp(eb->data + start, ptrv, len);
858 }
859
860 void read_extent_buffer(struct extent_buffer *eb, void *dst,
861                         unsigned long start, unsigned long len)
862 {
863         memcpy(dst, eb->data + start, len);
864 }
865
866 void write_extent_buffer(struct extent_buffer *eb, const void *src,
867                          unsigned long start, unsigned long len)
868 {
869         memcpy(eb->data + start, src, len);
870 }
871
872 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
873                         unsigned long dst_offset, unsigned long src_offset,
874                         unsigned long len)
875 {
876         memcpy(dst->data + dst_offset, src->data + src_offset, len);
877 }
878
879 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
880                            unsigned long src_offset, unsigned long len)
881 {
882         memmove(dst->data + dst_offset, dst->data + src_offset, len);
883 }
884
885 void memset_extent_buffer(struct extent_buffer *eb, char c,
886                           unsigned long start, unsigned long len)
887 {
888         memset(eb->data + start, c, len);
889 }
890
891 int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
892                            unsigned long nr)
893 {
894         return le_test_bit(nr, (u8 *)eb->data + start);
895 }