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