ocfs2: move tree path functions to alloc.h.
[deliverable/linux.git] / fs / ocfs2 / extent_map.c
CommitLineData
ccd979bd
MF
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * extent_map.c
5 *
363041a5 6 * Block/Cluster mapping functions
ccd979bd
MF
7 *
8 * Copyright (C) 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License, version 2, as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 021110-1307, USA.
23 */
24
25#include <linux/fs.h>
26#include <linux/init.h>
27#include <linux/types.h>
00dc417f 28#include <linux/fiemap.h>
ccd979bd
MF
29
30#define MLOG_MASK_PREFIX ML_EXTENT_MAP
31#include <cluster/masklog.h>
32
33#include "ocfs2.h"
34
363041a5 35#include "alloc.h"
00dc417f 36#include "dlmglue.h"
ccd979bd
MF
37#include "extent_map.h"
38#include "inode.h"
39#include "super.h"
40
41#include "buffer_head_io.h"
42
83418978
MF
43/*
44 * The extent caching implementation is intentionally trivial.
45 *
46 * We only cache a small number of extents stored directly on the
47 * inode, so linear order operations are acceptable. If we ever want
48 * to increase the size of the extent map, then these algorithms must
49 * get smarter.
50 */
51
52void ocfs2_extent_map_init(struct inode *inode)
53{
54 struct ocfs2_inode_info *oi = OCFS2_I(inode);
55
56 oi->ip_extent_map.em_num_items = 0;
57 INIT_LIST_HEAD(&oi->ip_extent_map.em_list);
58}
59
60static void __ocfs2_extent_map_lookup(struct ocfs2_extent_map *em,
61 unsigned int cpos,
62 struct ocfs2_extent_map_item **ret_emi)
63{
64 unsigned int range;
65 struct ocfs2_extent_map_item *emi;
66
67 *ret_emi = NULL;
68
69 list_for_each_entry(emi, &em->em_list, ei_list) {
70 range = emi->ei_cpos + emi->ei_clusters;
71
72 if (cpos >= emi->ei_cpos && cpos < range) {
73 list_move(&emi->ei_list, &em->em_list);
74
75 *ret_emi = emi;
76 break;
77 }
78 }
79}
80
81static int ocfs2_extent_map_lookup(struct inode *inode, unsigned int cpos,
82 unsigned int *phys, unsigned int *len,
83 unsigned int *flags)
84{
85 unsigned int coff;
86 struct ocfs2_inode_info *oi = OCFS2_I(inode);
87 struct ocfs2_extent_map_item *emi;
88
89 spin_lock(&oi->ip_lock);
90
91 __ocfs2_extent_map_lookup(&oi->ip_extent_map, cpos, &emi);
92 if (emi) {
93 coff = cpos - emi->ei_cpos;
94 *phys = emi->ei_phys + coff;
95 if (len)
96 *len = emi->ei_clusters - coff;
97 if (flags)
98 *flags = emi->ei_flags;
99 }
100
101 spin_unlock(&oi->ip_lock);
102
103 if (emi == NULL)
104 return -ENOENT;
105
106 return 0;
107}
108
109/*
110 * Forget about all clusters equal to or greater than cpos.
111 */
112void ocfs2_extent_map_trunc(struct inode *inode, unsigned int cpos)
113{
800deef3 114 struct ocfs2_extent_map_item *emi, *n;
83418978
MF
115 struct ocfs2_inode_info *oi = OCFS2_I(inode);
116 struct ocfs2_extent_map *em = &oi->ip_extent_map;
117 LIST_HEAD(tmp_list);
118 unsigned int range;
119
120 spin_lock(&oi->ip_lock);
800deef3 121 list_for_each_entry_safe(emi, n, &em->em_list, ei_list) {
83418978
MF
122 if (emi->ei_cpos >= cpos) {
123 /* Full truncate of this record. */
124 list_move(&emi->ei_list, &tmp_list);
125 BUG_ON(em->em_num_items == 0);
126 em->em_num_items--;
127 continue;
128 }
129
130 range = emi->ei_cpos + emi->ei_clusters;
131 if (range > cpos) {
132 /* Partial truncate */
133 emi->ei_clusters = cpos - emi->ei_cpos;
134 }
135 }
136 spin_unlock(&oi->ip_lock);
137
800deef3 138 list_for_each_entry_safe(emi, n, &tmp_list, ei_list) {
83418978
MF
139 list_del(&emi->ei_list);
140 kfree(emi);
141 }
142}
143
144/*
145 * Is any part of emi2 contained within emi1
146 */
147static int ocfs2_ei_is_contained(struct ocfs2_extent_map_item *emi1,
148 struct ocfs2_extent_map_item *emi2)
149{
150 unsigned int range1, range2;
151
152 /*
153 * Check if logical start of emi2 is inside emi1
154 */
155 range1 = emi1->ei_cpos + emi1->ei_clusters;
156 if (emi2->ei_cpos >= emi1->ei_cpos && emi2->ei_cpos < range1)
157 return 1;
158
159 /*
160 * Check if logical end of emi2 is inside emi1
161 */
162 range2 = emi2->ei_cpos + emi2->ei_clusters;
163 if (range2 > emi1->ei_cpos && range2 <= range1)
164 return 1;
165
166 return 0;
167}
168
169static void ocfs2_copy_emi_fields(struct ocfs2_extent_map_item *dest,
170 struct ocfs2_extent_map_item *src)
171{
172 dest->ei_cpos = src->ei_cpos;
173 dest->ei_phys = src->ei_phys;
174 dest->ei_clusters = src->ei_clusters;
175 dest->ei_flags = src->ei_flags;
176}
177
178/*
179 * Try to merge emi with ins. Returns 1 if merge succeeds, zero
180 * otherwise.
181 */
182static int ocfs2_try_to_merge_extent_map(struct ocfs2_extent_map_item *emi,
183 struct ocfs2_extent_map_item *ins)
184{
185 /*
186 * Handle contiguousness
187 */
188 if (ins->ei_phys == (emi->ei_phys + emi->ei_clusters) &&
189 ins->ei_cpos == (emi->ei_cpos + emi->ei_clusters) &&
190 ins->ei_flags == emi->ei_flags) {
191 emi->ei_clusters += ins->ei_clusters;
192 return 1;
193 } else if ((ins->ei_phys + ins->ei_clusters) == emi->ei_phys &&
194 (ins->ei_cpos + ins->ei_clusters) == emi->ei_phys &&
195 ins->ei_flags == emi->ei_flags) {
196 emi->ei_phys = ins->ei_phys;
197 emi->ei_cpos = ins->ei_cpos;
198 emi->ei_clusters += ins->ei_clusters;
199 return 1;
200 }
201
202 /*
203 * Overlapping extents - this shouldn't happen unless we've
204 * split an extent to change it's flags. That is exceedingly
205 * rare, so there's no sense in trying to optimize it yet.
206 */
207 if (ocfs2_ei_is_contained(emi, ins) ||
208 ocfs2_ei_is_contained(ins, emi)) {
209 ocfs2_copy_emi_fields(emi, ins);
210 return 1;
211 }
212
213 /* No merge was possible. */
214 return 0;
215}
216
217/*
218 * In order to reduce complexity on the caller, this insert function
219 * is intentionally liberal in what it will accept.
220 *
221 * The only rule is that the truncate call *must* be used whenever
222 * records have been deleted. This avoids inserting overlapping
223 * records with different physical mappings.
224 */
225void ocfs2_extent_map_insert_rec(struct inode *inode,
226 struct ocfs2_extent_rec *rec)
227{
228 struct ocfs2_inode_info *oi = OCFS2_I(inode);
229 struct ocfs2_extent_map *em = &oi->ip_extent_map;
230 struct ocfs2_extent_map_item *emi, *new_emi = NULL;
231 struct ocfs2_extent_map_item ins;
232
233 ins.ei_cpos = le32_to_cpu(rec->e_cpos);
234 ins.ei_phys = ocfs2_blocks_to_clusters(inode->i_sb,
235 le64_to_cpu(rec->e_blkno));
236 ins.ei_clusters = le16_to_cpu(rec->e_leaf_clusters);
237 ins.ei_flags = rec->e_flags;
238
239search:
240 spin_lock(&oi->ip_lock);
241
242 list_for_each_entry(emi, &em->em_list, ei_list) {
243 if (ocfs2_try_to_merge_extent_map(emi, &ins)) {
244 list_move(&emi->ei_list, &em->em_list);
245 spin_unlock(&oi->ip_lock);
246 goto out;
247 }
248 }
249
250 /*
251 * No item could be merged.
252 *
253 * Either allocate and add a new item, or overwrite the last recently
254 * inserted.
255 */
256
257 if (em->em_num_items < OCFS2_MAX_EXTENT_MAP_ITEMS) {
258 if (new_emi == NULL) {
259 spin_unlock(&oi->ip_lock);
260
261 new_emi = kmalloc(sizeof(*new_emi), GFP_NOFS);
262 if (new_emi == NULL)
263 goto out;
264
265 goto search;
266 }
267
268 ocfs2_copy_emi_fields(new_emi, &ins);
269 list_add(&new_emi->ei_list, &em->em_list);
270 em->em_num_items++;
271 new_emi = NULL;
272 } else {
273 BUG_ON(list_empty(&em->em_list) || em->em_num_items == 0);
274 emi = list_entry(em->em_list.prev,
275 struct ocfs2_extent_map_item, ei_list);
276 list_move(&emi->ei_list, &em->em_list);
277 ocfs2_copy_emi_fields(emi, &ins);
278 }
279
280 spin_unlock(&oi->ip_lock);
281
282out:
283 if (new_emi)
284 kfree(new_emi);
285}
286
00dc417f
MF
287static int ocfs2_last_eb_is_empty(struct inode *inode,
288 struct ocfs2_dinode *di)
289{
290 int ret, next_free;
291 u64 last_eb_blk = le64_to_cpu(di->i_last_eb_blk);
292 struct buffer_head *eb_bh = NULL;
293 struct ocfs2_extent_block *eb;
294 struct ocfs2_extent_list *el;
295
3d03a305 296 ret = ocfs2_read_extent_block(INODE_CACHE(inode), last_eb_blk, &eb_bh);
00dc417f
MF
297 if (ret) {
298 mlog_errno(ret);
299 goto out;
300 }
301
302 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
303 el = &eb->h_list;
304
00dc417f
MF
305 if (el->l_tree_depth) {
306 ocfs2_error(inode->i_sb,
307 "Inode %lu has non zero tree depth in "
308 "leaf block %llu\n", inode->i_ino,
309 (unsigned long long)eb_bh->b_blocknr);
310 ret = -EROFS;
311 goto out;
312 }
313
314 next_free = le16_to_cpu(el->l_next_free_rec);
315
316 if (next_free == 0 ||
317 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0])))
318 ret = 1;
319
320out:
321 brelse(eb_bh);
322 return ret;
323}
324
4f902c37
MF
325/*
326 * Return the 1st index within el which contains an extent start
327 * larger than v_cluster.
328 */
329static int ocfs2_search_for_hole_index(struct ocfs2_extent_list *el,
330 u32 v_cluster)
331{
332 int i;
333 struct ocfs2_extent_rec *rec;
334
335 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
336 rec = &el->l_recs[i];
337
338 if (v_cluster < le32_to_cpu(rec->e_cpos))
339 break;
340 }
341
342 return i;
343}
344
345/*
346 * Figure out the size of a hole which starts at v_cluster within the given
347 * extent list.
348 *
349 * If there is no more allocation past v_cluster, we return the maximum
350 * cluster size minus v_cluster.
351 *
352 * If we have in-inode extents, then el points to the dinode list and
353 * eb_bh is NULL. Otherwise, eb_bh should point to the extent block
354 * containing el.
355 */
356static int ocfs2_figure_hole_clusters(struct inode *inode,
357 struct ocfs2_extent_list *el,
358 struct buffer_head *eb_bh,
359 u32 v_cluster,
360 u32 *num_clusters)
361{
362 int ret, i;
363 struct buffer_head *next_eb_bh = NULL;
364 struct ocfs2_extent_block *eb, *next_eb;
365
366 i = ocfs2_search_for_hole_index(el, v_cluster);
367
368 if (i == le16_to_cpu(el->l_next_free_rec) && eb_bh) {
369 eb = (struct ocfs2_extent_block *)eb_bh->b_data;
370
371 /*
372 * Check the next leaf for any extents.
373 */
374
375 if (le64_to_cpu(eb->h_next_leaf_blk) == 0ULL)
376 goto no_more_extents;
377
3d03a305 378 ret = ocfs2_read_extent_block(INODE_CACHE(inode),
5e96581a
JB
379 le64_to_cpu(eb->h_next_leaf_blk),
380 &next_eb_bh);
4f902c37
MF
381 if (ret) {
382 mlog_errno(ret);
383 goto out;
384 }
4f902c37 385
5e96581a 386 next_eb = (struct ocfs2_extent_block *)next_eb_bh->b_data;
4f902c37 387 el = &next_eb->h_list;
4f902c37
MF
388 i = ocfs2_search_for_hole_index(el, v_cluster);
389 }
390
391no_more_extents:
392 if (i == le16_to_cpu(el->l_next_free_rec)) {
393 /*
394 * We're at the end of our existing allocation. Just
395 * return the maximum number of clusters we could
396 * possibly allocate.
397 */
398 *num_clusters = UINT_MAX - v_cluster;
399 } else {
400 *num_clusters = le32_to_cpu(el->l_recs[i].e_cpos) - v_cluster;
401 }
402
403 ret = 0;
404out:
405 brelse(next_eb_bh);
406 return ret;
407}
408
00dc417f
MF
409static int ocfs2_get_clusters_nocache(struct inode *inode,
410 struct buffer_head *di_bh,
411 u32 v_cluster, unsigned int *hole_len,
412 struct ocfs2_extent_rec *ret_rec,
413 unsigned int *is_last)
ccd979bd 414{
00dc417f 415 int i, ret, tree_height, len;
ccd979bd 416 struct ocfs2_dinode *di;
00dc417f 417 struct ocfs2_extent_block *uninitialized_var(eb);
ccd979bd 418 struct ocfs2_extent_list *el;
363041a5 419 struct ocfs2_extent_rec *rec;
00dc417f 420 struct buffer_head *eb_bh = NULL;
83418978 421
00dc417f
MF
422 memset(ret_rec, 0, sizeof(*ret_rec));
423 if (is_last)
424 *is_last = 0;
ccd979bd 425
363041a5
MF
426 di = (struct ocfs2_dinode *) di_bh->b_data;
427 el = &di->id2.i_list;
00dc417f 428 tree_height = le16_to_cpu(el->l_tree_depth);
ccd979bd 429
00dc417f 430 if (tree_height > 0) {
facdb77f
JB
431 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, v_cluster,
432 &eb_bh);
363041a5
MF
433 if (ret) {
434 mlog_errno(ret);
435 goto out;
436 }
ccd979bd 437
363041a5
MF
438 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
439 el = &eb->h_list;
e48edee2
MF
440
441 if (el->l_tree_depth) {
442 ocfs2_error(inode->i_sb,
443 "Inode %lu has non zero tree depth in "
444 "leaf block %llu\n", inode->i_ino,
445 (unsigned long long)eb_bh->b_blocknr);
446 ret = -EROFS;
447 goto out;
448 }
a43db30c 449 }
ccd979bd 450
363041a5
MF
451 i = ocfs2_search_extent_list(el, v_cluster);
452 if (i == -1) {
a43db30c 453 /*
00dc417f
MF
454 * Holes can be larger than the maximum size of an
455 * extent, so we return their lengths in a seperate
456 * field.
a43db30c 457 */
00dc417f 458 if (hole_len) {
4f902c37 459 ret = ocfs2_figure_hole_clusters(inode, el, eb_bh,
00dc417f 460 v_cluster, &len);
4f902c37
MF
461 if (ret) {
462 mlog_errno(ret);
463 goto out;
464 }
00dc417f
MF
465
466 *hole_len = len;
4f902c37 467 }
00dc417f
MF
468 goto out_hole;
469 }
ccd979bd 470
00dc417f 471 rec = &el->l_recs[i];
ccd979bd 472
00dc417f
MF
473 BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
474
475 if (!rec->e_blkno) {
476 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
477 "record (%u, %u, 0)", inode->i_ino,
478 le32_to_cpu(rec->e_cpos),
479 ocfs2_rec_clusters(el, rec));
480 ret = -EROFS;
481 goto out;
482 }
483
484 *ret_rec = *rec;
485
486 /*
487 * Checking for last extent is potentially expensive - we
488 * might have to look at the next leaf over to see if it's
489 * empty.
490 *
491 * The first two checks are to see whether the caller even
492 * cares for this information, and if the extent is at least
493 * the last in it's list.
494 *
495 * If those hold true, then the extent is last if any of the
496 * additional conditions hold true:
497 * - Extent list is in-inode
498 * - Extent list is right-most
499 * - Extent list is 2nd to rightmost, with empty right-most
500 */
501 if (is_last) {
502 if (i == (le16_to_cpu(el->l_next_free_rec) - 1)) {
503 if (tree_height == 0)
504 *is_last = 1;
505 else if (eb->h_blkno == di->i_last_eb_blk)
506 *is_last = 1;
507 else if (eb->h_next_leaf_blk == di->i_last_eb_blk) {
508 ret = ocfs2_last_eb_is_empty(inode, di);
509 if (ret < 0) {
510 mlog_errno(ret);
511 goto out;
512 }
513 if (ret == 1)
514 *is_last = 1;
515 }
ccd979bd 516 }
00dc417f
MF
517 }
518
519out_hole:
520 ret = 0;
521out:
522 brelse(eb_bh);
523 return ret;
524}
525
526static void ocfs2_relative_extent_offsets(struct super_block *sb,
527 u32 v_cluster,
528 struct ocfs2_extent_rec *rec,
529 u32 *p_cluster, u32 *num_clusters)
530
531{
532 u32 coff = v_cluster - le32_to_cpu(rec->e_cpos);
533
534 *p_cluster = ocfs2_blocks_to_clusters(sb, le64_to_cpu(rec->e_blkno));
535 *p_cluster = *p_cluster + coff;
536
537 if (num_clusters)
538 *num_clusters = le16_to_cpu(rec->e_leaf_clusters) - coff;
539}
540
f56654c4
TM
541int ocfs2_xattr_get_clusters(struct inode *inode, u32 v_cluster,
542 u32 *p_cluster, u32 *num_clusters,
543 struct ocfs2_extent_list *el)
544{
545 int ret = 0, i;
546 struct buffer_head *eb_bh = NULL;
547 struct ocfs2_extent_block *eb;
548 struct ocfs2_extent_rec *rec;
549 u32 coff;
550
551 if (el->l_tree_depth) {
facdb77f
JB
552 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, v_cluster,
553 &eb_bh);
f56654c4
TM
554 if (ret) {
555 mlog_errno(ret);
556 goto out;
557 }
558
559 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
560 el = &eb->h_list;
561
562 if (el->l_tree_depth) {
563 ocfs2_error(inode->i_sb,
564 "Inode %lu has non zero tree depth in "
565 "xattr leaf block %llu\n", inode->i_ino,
566 (unsigned long long)eb_bh->b_blocknr);
567 ret = -EROFS;
568 goto out;
569 }
570 }
571
572 i = ocfs2_search_extent_list(el, v_cluster);
573 if (i == -1) {
574 ret = -EROFS;
575 mlog_errno(ret);
576 goto out;
577 } else {
578 rec = &el->l_recs[i];
579 BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
580
581 if (!rec->e_blkno) {
582 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
583 "record (%u, %u, 0) in xattr", inode->i_ino,
584 le32_to_cpu(rec->e_cpos),
585 ocfs2_rec_clusters(el, rec));
586 ret = -EROFS;
587 goto out;
588 }
589 coff = v_cluster - le32_to_cpu(rec->e_cpos);
590 *p_cluster = ocfs2_blocks_to_clusters(inode->i_sb,
591 le64_to_cpu(rec->e_blkno));
592 *p_cluster = *p_cluster + coff;
593 if (num_clusters)
594 *num_clusters = ocfs2_rec_clusters(el, rec) - coff;
595 }
596out:
597 if (eb_bh)
598 brelse(eb_bh);
599 return ret;
600}
601
00dc417f
MF
602int ocfs2_get_clusters(struct inode *inode, u32 v_cluster,
603 u32 *p_cluster, u32 *num_clusters,
604 unsigned int *extent_flags)
605{
606 int ret;
607 unsigned int uninitialized_var(hole_len), flags = 0;
608 struct buffer_head *di_bh = NULL;
609 struct ocfs2_extent_rec rec;
ccd979bd 610
00dc417f
MF
611 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
612 ret = -ERANGE;
613 mlog_errno(ret);
614 goto out;
615 }
ccd979bd 616
00dc417f
MF
617 ret = ocfs2_extent_map_lookup(inode, v_cluster, p_cluster,
618 num_clusters, extent_flags);
619 if (ret == 0)
620 goto out;
ccd979bd 621
b657c95c 622 ret = ocfs2_read_inode_block(inode, &di_bh);
00dc417f
MF
623 if (ret) {
624 mlog_errno(ret);
625 goto out;
626 }
49cb8d2d 627
00dc417f
MF
628 ret = ocfs2_get_clusters_nocache(inode, di_bh, v_cluster, &hole_len,
629 &rec, NULL);
630 if (ret) {
631 mlog_errno(ret);
632 goto out;
633 }
83418978 634
00dc417f
MF
635 if (rec.e_blkno == 0ULL) {
636 /*
637 * A hole was found. Return some canned values that
638 * callers can key on. If asked for, num_clusters will
639 * be populated with the size of the hole.
640 */
641 *p_cluster = 0;
642 if (num_clusters) {
643 *num_clusters = hole_len;
644 }
645 } else {
646 ocfs2_relative_extent_offsets(inode->i_sb, v_cluster, &rec,
647 p_cluster, num_clusters);
648 flags = rec.e_flags;
649
650 ocfs2_extent_map_insert_rec(inode, &rec);
ccd979bd
MF
651 }
652
49cb8d2d
MF
653 if (extent_flags)
654 *extent_flags = flags;
655
363041a5
MF
656out:
657 brelse(di_bh);
ccd979bd
MF
658 return ret;
659}
660
ccd979bd 661/*
363041a5
MF
662 * This expects alloc_sem to be held. The allocation cannot change at
663 * all while the map is in the process of being updated.
ccd979bd 664 */
363041a5 665int ocfs2_extent_map_get_blocks(struct inode *inode, u64 v_blkno, u64 *p_blkno,
4f902c37 666 u64 *ret_count, unsigned int *extent_flags)
ccd979bd
MF
667{
668 int ret;
ccd979bd 669 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
363041a5
MF
670 u32 cpos, num_clusters, p_cluster;
671 u64 boff = 0;
ccd979bd
MF
672
673 cpos = ocfs2_blocks_to_clusters(inode->i_sb, v_blkno);
ccd979bd 674
49cb8d2d
MF
675 ret = ocfs2_get_clusters(inode, cpos, &p_cluster, &num_clusters,
676 extent_flags);
ccd979bd
MF
677 if (ret) {
678 mlog_errno(ret);
363041a5 679 goto out;
ccd979bd
MF
680 }
681
363041a5
MF
682 /*
683 * p_cluster == 0 indicates a hole.
684 */
685 if (p_cluster) {
686 boff = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
ccd979bd 687 boff += (v_blkno & (u64)(bpc - 1));
ccd979bd
MF
688 }
689
363041a5 690 *p_blkno = boff;
ccd979bd 691
363041a5
MF
692 if (ret_count) {
693 *ret_count = ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
694 *ret_count -= v_blkno & (u64)(bpc - 1);
ccd979bd 695 }
ccd979bd 696
363041a5
MF
697out:
698 return ret;
ccd979bd 699}
00dc417f
MF
700
701static int ocfs2_fiemap_inline(struct inode *inode, struct buffer_head *di_bh,
702 struct fiemap_extent_info *fieinfo,
703 u64 map_start)
704{
705 int ret;
706 unsigned int id_count;
707 struct ocfs2_dinode *di;
708 u64 phys;
709 u32 flags = FIEMAP_EXTENT_DATA_INLINE|FIEMAP_EXTENT_LAST;
710 struct ocfs2_inode_info *oi = OCFS2_I(inode);
711
712 di = (struct ocfs2_dinode *)di_bh->b_data;
713 id_count = le16_to_cpu(di->id2.i_data.id_count);
714
715 if (map_start < id_count) {
716 phys = oi->ip_blkno << inode->i_sb->s_blocksize_bits;
717 phys += offsetof(struct ocfs2_dinode, id2.i_data.id_data);
718
719 ret = fiemap_fill_next_extent(fieinfo, 0, phys, id_count,
720 flags);
721 if (ret < 0)
722 return ret;
723 }
724
725 return 0;
726}
727
728#define OCFS2_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
729
730int ocfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
731 u64 map_start, u64 map_len)
732{
733 int ret, is_last;
734 u32 mapping_end, cpos;
735 unsigned int hole_size;
736 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
737 u64 len_bytes, phys_bytes, virt_bytes;
738 struct buffer_head *di_bh = NULL;
739 struct ocfs2_extent_rec rec;
740
741 ret = fiemap_check_flags(fieinfo, OCFS2_FIEMAP_FLAGS);
742 if (ret)
743 return ret;
744
745 ret = ocfs2_inode_lock(inode, &di_bh, 0);
746 if (ret) {
747 mlog_errno(ret);
748 goto out;
749 }
750
751 down_read(&OCFS2_I(inode)->ip_alloc_sem);
752
753 /*
754 * Handle inline-data separately.
755 */
756 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
757 ret = ocfs2_fiemap_inline(inode, di_bh, fieinfo, map_start);
758 goto out_unlock;
759 }
760
761 cpos = map_start >> osb->s_clustersize_bits;
762 mapping_end = ocfs2_clusters_for_bytes(inode->i_sb,
763 map_start + map_len);
764 mapping_end -= cpos;
765 is_last = 0;
766 while (cpos < mapping_end && !is_last) {
767 u32 fe_flags;
768
769 ret = ocfs2_get_clusters_nocache(inode, di_bh, cpos,
770 &hole_size, &rec, &is_last);
771 if (ret) {
772 mlog_errno(ret);
773 goto out;
774 }
775
776 if (rec.e_blkno == 0ULL) {
777 cpos += hole_size;
778 continue;
779 }
780
781 fe_flags = 0;
782 if (rec.e_flags & OCFS2_EXT_UNWRITTEN)
783 fe_flags |= FIEMAP_EXTENT_UNWRITTEN;
784 if (is_last)
785 fe_flags |= FIEMAP_EXTENT_LAST;
786 len_bytes = (u64)le16_to_cpu(rec.e_leaf_clusters) << osb->s_clustersize_bits;
787 phys_bytes = le64_to_cpu(rec.e_blkno) << osb->sb->s_blocksize_bits;
788 virt_bytes = (u64)le32_to_cpu(rec.e_cpos) << osb->s_clustersize_bits;
789
790 ret = fiemap_fill_next_extent(fieinfo, virt_bytes, phys_bytes,
791 len_bytes, fe_flags);
792 if (ret)
793 break;
794
795 cpos = le32_to_cpu(rec.e_cpos)+ le16_to_cpu(rec.e_leaf_clusters);
796 }
797
798 if (ret > 0)
799 ret = 0;
800
801out_unlock:
802 brelse(di_bh);
803
804 up_read(&OCFS2_I(inode)->ip_alloc_sem);
805
806 ocfs2_inode_unlock(inode, 0);
807out:
808
809 return ret;
810}
a8549fb5
JB
811
812int ocfs2_read_virt_blocks(struct inode *inode, u64 v_block, int nr,
813 struct buffer_head *bhs[], int flags,
814 int (*validate)(struct super_block *sb,
815 struct buffer_head *bh))
816{
817 int rc = 0;
818 u64 p_block, p_count;
819 int i, count, done = 0;
820
821 mlog_entry("(inode = %p, v_block = %llu, nr = %d, bhs = %p, "
822 "flags = %x, validate = %p)\n",
823 inode, (unsigned long long)v_block, nr, bhs, flags,
824 validate);
825
826 if (((v_block + nr - 1) << inode->i_sb->s_blocksize_bits) >=
827 i_size_read(inode)) {
828 BUG_ON(!(flags & OCFS2_BH_READAHEAD));
829 goto out;
830 }
831
832 while (done < nr) {
833 down_read(&OCFS2_I(inode)->ip_alloc_sem);
834 rc = ocfs2_extent_map_get_blocks(inode, v_block + done,
835 &p_block, &p_count, NULL);
836 up_read(&OCFS2_I(inode)->ip_alloc_sem);
837 if (rc) {
838 mlog_errno(rc);
839 break;
840 }
841
842 if (!p_block) {
843 rc = -EIO;
844 mlog(ML_ERROR,
845 "Inode #%llu contains a hole at offset %llu\n",
846 (unsigned long long)OCFS2_I(inode)->ip_blkno,
847 (unsigned long long)(v_block + done) <<
848 inode->i_sb->s_blocksize_bits);
849 break;
850 }
851
852 count = nr - done;
853 if (p_count < count)
854 count = p_count;
855
856 /*
857 * If the caller passed us bhs, they should have come
858 * from a previous readahead call to this function. Thus,
859 * they should have the right b_blocknr.
860 */
861 for (i = 0; i < count; i++) {
862 if (!bhs[done + i])
863 continue;
864 BUG_ON(bhs[done + i]->b_blocknr != (p_block + i));
865 }
866
8cb471e8
JB
867 rc = ocfs2_read_blocks(INODE_CACHE(inode), p_block, count,
868 bhs + done, flags, validate);
a8549fb5
JB
869 if (rc) {
870 mlog_errno(rc);
871 break;
872 }
873 done += count;
874 }
875
876out:
877 mlog_exit(rc);
878 return rc;
879}
880
881
This page took 0.500682 seconds and 5 git commands to generate.