1 // SPDX-License-Identifier: GPL-2.0-only
2 /* -*- mode: c; c-basic-offset: 8; -*-
3 * vim: noexpandtab sw=8 ts=8 sts=0:
7 * Block/Cluster mapping functions
9 * Copyright (C) 2004 Oracle. All rights reserved.
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/fiemap.h>
18 #include <cluster/masklog.h>
24 #include "extent_map.h"
29 #include "ocfs2_trace.h"
31 #include "buffer_head_io.h"
34 * The extent caching implementation is intentionally trivial.
36 * We only cache a small number of extents stored directly on the
37 * inode, so linear order operations are acceptable. If we ever want
38 * to increase the size of the extent map, then these algorithms must
42 void ocfs2_extent_map_init(struct inode
*inode
)
44 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
46 oi
->ip_extent_map
.em_num_items
= 0;
47 INIT_LIST_HEAD(&oi
->ip_extent_map
.em_list
);
50 static void __ocfs2_extent_map_lookup(struct ocfs2_extent_map
*em
,
52 struct ocfs2_extent_map_item
**ret_emi
)
55 struct ocfs2_extent_map_item
*emi
;
59 list_for_each_entry(emi
, &em
->em_list
, ei_list
) {
60 range
= emi
->ei_cpos
+ emi
->ei_clusters
;
62 if (cpos
>= emi
->ei_cpos
&& cpos
< range
) {
63 list_move(&emi
->ei_list
, &em
->em_list
);
71 static int ocfs2_extent_map_lookup(struct inode
*inode
, unsigned int cpos
,
72 unsigned int *phys
, unsigned int *len
,
76 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
77 struct ocfs2_extent_map_item
*emi
;
79 spin_lock(&oi
->ip_lock
);
81 __ocfs2_extent_map_lookup(&oi
->ip_extent_map
, cpos
, &emi
);
83 coff
= cpos
- emi
->ei_cpos
;
84 *phys
= emi
->ei_phys
+ coff
;
86 *len
= emi
->ei_clusters
- coff
;
88 *flags
= emi
->ei_flags
;
91 spin_unlock(&oi
->ip_lock
);
100 * Forget about all clusters equal to or greater than cpos.
102 void ocfs2_extent_map_trunc(struct inode
*inode
, unsigned int cpos
)
104 struct ocfs2_extent_map_item
*emi
, *n
;
105 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
106 struct ocfs2_extent_map
*em
= &oi
->ip_extent_map
;
110 spin_lock(&oi
->ip_lock
);
111 list_for_each_entry_safe(emi
, n
, &em
->em_list
, ei_list
) {
112 if (emi
->ei_cpos
>= cpos
) {
113 /* Full truncate of this record. */
114 list_move(&emi
->ei_list
, &tmp_list
);
115 BUG_ON(em
->em_num_items
== 0);
120 range
= emi
->ei_cpos
+ emi
->ei_clusters
;
122 /* Partial truncate */
123 emi
->ei_clusters
= cpos
- emi
->ei_cpos
;
126 spin_unlock(&oi
->ip_lock
);
128 list_for_each_entry_safe(emi
, n
, &tmp_list
, ei_list
) {
129 list_del(&emi
->ei_list
);
135 * Is any part of emi2 contained within emi1
137 static int ocfs2_ei_is_contained(struct ocfs2_extent_map_item
*emi1
,
138 struct ocfs2_extent_map_item
*emi2
)
140 unsigned int range1
, range2
;
143 * Check if logical start of emi2 is inside emi1
145 range1
= emi1
->ei_cpos
+ emi1
->ei_clusters
;
146 if (emi2
->ei_cpos
>= emi1
->ei_cpos
&& emi2
->ei_cpos
< range1
)
150 * Check if logical end of emi2 is inside emi1
152 range2
= emi2
->ei_cpos
+ emi2
->ei_clusters
;
153 if (range2
> emi1
->ei_cpos
&& range2
<= range1
)
159 static void ocfs2_copy_emi_fields(struct ocfs2_extent_map_item
*dest
,
160 struct ocfs2_extent_map_item
*src
)
162 dest
->ei_cpos
= src
->ei_cpos
;
163 dest
->ei_phys
= src
->ei_phys
;
164 dest
->ei_clusters
= src
->ei_clusters
;
165 dest
->ei_flags
= src
->ei_flags
;
169 * Try to merge emi with ins. Returns 1 if merge succeeds, zero
172 static int ocfs2_try_to_merge_extent_map(struct ocfs2_extent_map_item
*emi
,
173 struct ocfs2_extent_map_item
*ins
)
176 * Handle contiguousness
178 if (ins
->ei_phys
== (emi
->ei_phys
+ emi
->ei_clusters
) &&
179 ins
->ei_cpos
== (emi
->ei_cpos
+ emi
->ei_clusters
) &&
180 ins
->ei_flags
== emi
->ei_flags
) {
181 emi
->ei_clusters
+= ins
->ei_clusters
;
183 } else if ((ins
->ei_phys
+ ins
->ei_clusters
) == emi
->ei_phys
&&
184 (ins
->ei_cpos
+ ins
->ei_clusters
) == emi
->ei_cpos
&&
185 ins
->ei_flags
== emi
->ei_flags
) {
186 emi
->ei_phys
= ins
->ei_phys
;
187 emi
->ei_cpos
= ins
->ei_cpos
;
188 emi
->ei_clusters
+= ins
->ei_clusters
;
193 * Overlapping extents - this shouldn't happen unless we've
194 * split an extent to change it's flags. That is exceedingly
195 * rare, so there's no sense in trying to optimize it yet.
197 if (ocfs2_ei_is_contained(emi
, ins
) ||
198 ocfs2_ei_is_contained(ins
, emi
)) {
199 ocfs2_copy_emi_fields(emi
, ins
);
203 /* No merge was possible. */
208 * In order to reduce complexity on the caller, this insert function
209 * is intentionally liberal in what it will accept.
211 * The only rule is that the truncate call *must* be used whenever
212 * records have been deleted. This avoids inserting overlapping
213 * records with different physical mappings.
215 void ocfs2_extent_map_insert_rec(struct inode
*inode
,
216 struct ocfs2_extent_rec
*rec
)
218 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
219 struct ocfs2_extent_map
*em
= &oi
->ip_extent_map
;
220 struct ocfs2_extent_map_item
*emi
, *new_emi
= NULL
;
221 struct ocfs2_extent_map_item ins
;
223 ins
.ei_cpos
= le32_to_cpu(rec
->e_cpos
);
224 ins
.ei_phys
= ocfs2_blocks_to_clusters(inode
->i_sb
,
225 le64_to_cpu(rec
->e_blkno
));
226 ins
.ei_clusters
= le16_to_cpu(rec
->e_leaf_clusters
);
227 ins
.ei_flags
= rec
->e_flags
;
230 spin_lock(&oi
->ip_lock
);
232 list_for_each_entry(emi
, &em
->em_list
, ei_list
) {
233 if (ocfs2_try_to_merge_extent_map(emi
, &ins
)) {
234 list_move(&emi
->ei_list
, &em
->em_list
);
235 spin_unlock(&oi
->ip_lock
);
241 * No item could be merged.
243 * Either allocate and add a new item, or overwrite the last recently
247 if (em
->em_num_items
< OCFS2_MAX_EXTENT_MAP_ITEMS
) {
248 if (new_emi
== NULL
) {
249 spin_unlock(&oi
->ip_lock
);
251 new_emi
= kmalloc(sizeof(*new_emi
), GFP_NOFS
);
258 ocfs2_copy_emi_fields(new_emi
, &ins
);
259 list_add(&new_emi
->ei_list
, &em
->em_list
);
263 BUG_ON(list_empty(&em
->em_list
) || em
->em_num_items
== 0);
264 emi
= list_entry(em
->em_list
.prev
,
265 struct ocfs2_extent_map_item
, ei_list
);
266 list_move(&emi
->ei_list
, &em
->em_list
);
267 ocfs2_copy_emi_fields(emi
, &ins
);
270 spin_unlock(&oi
->ip_lock
);
276 static int ocfs2_last_eb_is_empty(struct inode
*inode
,
277 struct ocfs2_dinode
*di
)
280 u64 last_eb_blk
= le64_to_cpu(di
->i_last_eb_blk
);
281 struct buffer_head
*eb_bh
= NULL
;
282 struct ocfs2_extent_block
*eb
;
283 struct ocfs2_extent_list
*el
;
285 ret
= ocfs2_read_extent_block(INODE_CACHE(inode
), last_eb_blk
, &eb_bh
);
291 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
294 if (el
->l_tree_depth
) {
295 ocfs2_error(inode
->i_sb
,
296 "Inode %lu has non zero tree depth in leaf block %llu\n",
298 (unsigned long long)eb_bh
->b_blocknr
);
303 next_free
= le16_to_cpu(el
->l_next_free_rec
);
305 if (next_free
== 0 ||
306 (next_free
== 1 && ocfs2_is_empty_extent(&el
->l_recs
[0])))
315 * Return the 1st index within el which contains an extent start
316 * larger than v_cluster.
318 static int ocfs2_search_for_hole_index(struct ocfs2_extent_list
*el
,
322 struct ocfs2_extent_rec
*rec
;
324 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
325 rec
= &el
->l_recs
[i
];
327 if (v_cluster
< le32_to_cpu(rec
->e_cpos
))
335 * Figure out the size of a hole which starts at v_cluster within the given
338 * If there is no more allocation past v_cluster, we return the maximum
339 * cluster size minus v_cluster.
341 * If we have in-inode extents, then el points to the dinode list and
342 * eb_bh is NULL. Otherwise, eb_bh should point to the extent block
345 int ocfs2_figure_hole_clusters(struct ocfs2_caching_info
*ci
,
346 struct ocfs2_extent_list
*el
,
347 struct buffer_head
*eb_bh
,
352 struct buffer_head
*next_eb_bh
= NULL
;
353 struct ocfs2_extent_block
*eb
, *next_eb
;
355 i
= ocfs2_search_for_hole_index(el
, v_cluster
);
357 if (i
== le16_to_cpu(el
->l_next_free_rec
) && eb_bh
) {
358 eb
= (struct ocfs2_extent_block
*)eb_bh
->b_data
;
361 * Check the next leaf for any extents.
364 if (le64_to_cpu(eb
->h_next_leaf_blk
) == 0ULL)
365 goto no_more_extents
;
367 ret
= ocfs2_read_extent_block(ci
,
368 le64_to_cpu(eb
->h_next_leaf_blk
),
375 next_eb
= (struct ocfs2_extent_block
*)next_eb_bh
->b_data
;
376 el
= &next_eb
->h_list
;
377 i
= ocfs2_search_for_hole_index(el
, v_cluster
);
381 if (i
== le16_to_cpu(el
->l_next_free_rec
)) {
383 * We're at the end of our existing allocation. Just
384 * return the maximum number of clusters we could
387 *num_clusters
= UINT_MAX
- v_cluster
;
389 *num_clusters
= le32_to_cpu(el
->l_recs
[i
].e_cpos
) - v_cluster
;
398 static int ocfs2_get_clusters_nocache(struct inode
*inode
,
399 struct buffer_head
*di_bh
,
400 u32 v_cluster
, unsigned int *hole_len
,
401 struct ocfs2_extent_rec
*ret_rec
,
402 unsigned int *is_last
)
404 int i
, ret
, tree_height
, len
;
405 struct ocfs2_dinode
*di
;
406 struct ocfs2_extent_block
*uninitialized_var(eb
);
407 struct ocfs2_extent_list
*el
;
408 struct ocfs2_extent_rec
*rec
;
409 struct buffer_head
*eb_bh
= NULL
;
411 memset(ret_rec
, 0, sizeof(*ret_rec
));
415 di
= (struct ocfs2_dinode
*) di_bh
->b_data
;
416 el
= &di
->id2
.i_list
;
417 tree_height
= le16_to_cpu(el
->l_tree_depth
);
419 if (tree_height
> 0) {
420 ret
= ocfs2_find_leaf(INODE_CACHE(inode
), el
, v_cluster
,
427 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
430 if (el
->l_tree_depth
) {
431 ocfs2_error(inode
->i_sb
,
432 "Inode %lu has non zero tree depth in leaf block %llu\n",
434 (unsigned long long)eb_bh
->b_blocknr
);
440 i
= ocfs2_search_extent_list(el
, v_cluster
);
443 * Holes can be larger than the maximum size of an
444 * extent, so we return their lengths in a separate
448 ret
= ocfs2_figure_hole_clusters(INODE_CACHE(inode
),
461 rec
= &el
->l_recs
[i
];
463 BUG_ON(v_cluster
< le32_to_cpu(rec
->e_cpos
));
466 ocfs2_error(inode
->i_sb
,
467 "Inode %lu has bad extent record (%u, %u, 0)\n",
469 le32_to_cpu(rec
->e_cpos
),
470 ocfs2_rec_clusters(el
, rec
));
478 * Checking for last extent is potentially expensive - we
479 * might have to look at the next leaf over to see if it's
482 * The first two checks are to see whether the caller even
483 * cares for this information, and if the extent is at least
484 * the last in it's list.
486 * If those hold true, then the extent is last if any of the
487 * additional conditions hold true:
488 * - Extent list is in-inode
489 * - Extent list is right-most
490 * - Extent list is 2nd to rightmost, with empty right-most
493 if (i
== (le16_to_cpu(el
->l_next_free_rec
) - 1)) {
494 if (tree_height
== 0)
496 else if (eb
->h_blkno
== di
->i_last_eb_blk
)
498 else if (eb
->h_next_leaf_blk
== di
->i_last_eb_blk
) {
499 ret
= ocfs2_last_eb_is_empty(inode
, di
);
517 static void ocfs2_relative_extent_offsets(struct super_block
*sb
,
519 struct ocfs2_extent_rec
*rec
,
520 u32
*p_cluster
, u32
*num_clusters
)
523 u32 coff
= v_cluster
- le32_to_cpu(rec
->e_cpos
);
525 *p_cluster
= ocfs2_blocks_to_clusters(sb
, le64_to_cpu(rec
->e_blkno
));
526 *p_cluster
= *p_cluster
+ coff
;
529 *num_clusters
= le16_to_cpu(rec
->e_leaf_clusters
) - coff
;
532 int ocfs2_xattr_get_clusters(struct inode
*inode
, u32 v_cluster
,
533 u32
*p_cluster
, u32
*num_clusters
,
534 struct ocfs2_extent_list
*el
,
535 unsigned int *extent_flags
)
538 struct buffer_head
*eb_bh
= NULL
;
539 struct ocfs2_extent_block
*eb
;
540 struct ocfs2_extent_rec
*rec
;
543 if (el
->l_tree_depth
) {
544 ret
= ocfs2_find_leaf(INODE_CACHE(inode
), el
, v_cluster
,
551 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
554 if (el
->l_tree_depth
) {
555 ocfs2_error(inode
->i_sb
,
556 "Inode %lu has non zero tree depth in xattr leaf block %llu\n",
558 (unsigned long long)eb_bh
->b_blocknr
);
564 i
= ocfs2_search_extent_list(el
, v_cluster
);
570 rec
= &el
->l_recs
[i
];
571 BUG_ON(v_cluster
< le32_to_cpu(rec
->e_cpos
));
574 ocfs2_error(inode
->i_sb
,
575 "Inode %lu has bad extent record (%u, %u, 0) in xattr\n",
577 le32_to_cpu(rec
->e_cpos
),
578 ocfs2_rec_clusters(el
, rec
));
582 coff
= v_cluster
- le32_to_cpu(rec
->e_cpos
);
583 *p_cluster
= ocfs2_blocks_to_clusters(inode
->i_sb
,
584 le64_to_cpu(rec
->e_blkno
));
585 *p_cluster
= *p_cluster
+ coff
;
587 *num_clusters
= ocfs2_rec_clusters(el
, rec
) - coff
;
590 *extent_flags
= rec
->e_flags
;
597 int ocfs2_get_clusters(struct inode
*inode
, u32 v_cluster
,
598 u32
*p_cluster
, u32
*num_clusters
,
599 unsigned int *extent_flags
)
602 unsigned int uninitialized_var(hole_len
), flags
= 0;
603 struct buffer_head
*di_bh
= NULL
;
604 struct ocfs2_extent_rec rec
;
606 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
612 ret
= ocfs2_extent_map_lookup(inode
, v_cluster
, p_cluster
,
613 num_clusters
, extent_flags
);
617 ret
= ocfs2_read_inode_block(inode
, &di_bh
);
623 ret
= ocfs2_get_clusters_nocache(inode
, di_bh
, v_cluster
, &hole_len
,
630 if (rec
.e_blkno
== 0ULL) {
632 * A hole was found. Return some canned values that
633 * callers can key on. If asked for, num_clusters will
634 * be populated with the size of the hole.
638 *num_clusters
= hole_len
;
641 ocfs2_relative_extent_offsets(inode
->i_sb
, v_cluster
, &rec
,
642 p_cluster
, num_clusters
);
645 ocfs2_extent_map_insert_rec(inode
, &rec
);
649 *extent_flags
= flags
;
657 * This expects alloc_sem to be held. The allocation cannot change at
658 * all while the map is in the process of being updated.
660 int ocfs2_extent_map_get_blocks(struct inode
*inode
, u64 v_blkno
, u64
*p_blkno
,
661 u64
*ret_count
, unsigned int *extent_flags
)
664 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
665 u32 cpos
, num_clusters
, p_cluster
;
668 cpos
= ocfs2_blocks_to_clusters(inode
->i_sb
, v_blkno
);
670 ret
= ocfs2_get_clusters(inode
, cpos
, &p_cluster
, &num_clusters
,
678 * p_cluster == 0 indicates a hole.
681 boff
= ocfs2_clusters_to_blocks(inode
->i_sb
, p_cluster
);
682 boff
+= (v_blkno
& (u64
)(bpc
- 1));
688 *ret_count
= ocfs2_clusters_to_blocks(inode
->i_sb
, num_clusters
);
689 *ret_count
-= v_blkno
& (u64
)(bpc
- 1);
697 * The ocfs2_fiemap_inline() may be a little bit misleading, since
698 * it not only handles the fiemap for inlined files, but also deals
699 * with the fast symlink, cause they have no difference for extent
702 static int ocfs2_fiemap_inline(struct inode
*inode
, struct buffer_head
*di_bh
,
703 struct fiemap_extent_info
*fieinfo
,
707 unsigned int id_count
;
708 struct ocfs2_dinode
*di
;
710 u32 flags
= FIEMAP_EXTENT_DATA_INLINE
|FIEMAP_EXTENT_LAST
;
711 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
713 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
714 if (ocfs2_inode_is_fast_symlink(inode
))
715 id_count
= ocfs2_fast_symlink_chars(inode
->i_sb
);
717 id_count
= le16_to_cpu(di
->id2
.i_data
.id_count
);
719 if (map_start
< id_count
) {
720 phys
= oi
->ip_blkno
<< inode
->i_sb
->s_blocksize_bits
;
721 if (ocfs2_inode_is_fast_symlink(inode
))
722 phys
+= offsetof(struct ocfs2_dinode
, id2
.i_symlink
);
724 phys
+= offsetof(struct ocfs2_dinode
,
727 ret
= fiemap_fill_next_extent(fieinfo
, 0, phys
, id_count
,
736 #define OCFS2_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
738 int ocfs2_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
739 u64 map_start
, u64 map_len
)
742 u32 mapping_end
, cpos
;
743 unsigned int hole_size
;
744 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
745 u64 len_bytes
, phys_bytes
, virt_bytes
;
746 struct buffer_head
*di_bh
= NULL
;
747 struct ocfs2_extent_rec rec
;
749 ret
= fiemap_check_flags(fieinfo
, OCFS2_FIEMAP_FLAGS
);
753 ret
= ocfs2_inode_lock(inode
, &di_bh
, 0);
759 down_read(&OCFS2_I(inode
)->ip_alloc_sem
);
762 * Handle inline-data and fast symlink separately.
764 if ((OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) ||
765 ocfs2_inode_is_fast_symlink(inode
)) {
766 ret
= ocfs2_fiemap_inline(inode
, di_bh
, fieinfo
, map_start
);
770 cpos
= map_start
>> osb
->s_clustersize_bits
;
771 mapping_end
= ocfs2_clusters_for_bytes(inode
->i_sb
,
772 map_start
+ map_len
);
774 while (cpos
< mapping_end
&& !is_last
) {
777 ret
= ocfs2_get_clusters_nocache(inode
, di_bh
, cpos
,
778 &hole_size
, &rec
, &is_last
);
784 if (rec
.e_blkno
== 0ULL) {
790 if (rec
.e_flags
& OCFS2_EXT_UNWRITTEN
)
791 fe_flags
|= FIEMAP_EXTENT_UNWRITTEN
;
792 if (rec
.e_flags
& OCFS2_EXT_REFCOUNTED
)
793 fe_flags
|= FIEMAP_EXTENT_SHARED
;
795 fe_flags
|= FIEMAP_EXTENT_LAST
;
796 len_bytes
= (u64
)le16_to_cpu(rec
.e_leaf_clusters
) << osb
->s_clustersize_bits
;
797 phys_bytes
= le64_to_cpu(rec
.e_blkno
) << osb
->sb
->s_blocksize_bits
;
798 virt_bytes
= (u64
)le32_to_cpu(rec
.e_cpos
) << osb
->s_clustersize_bits
;
800 ret
= fiemap_fill_next_extent(fieinfo
, virt_bytes
, phys_bytes
,
801 len_bytes
, fe_flags
);
805 cpos
= le32_to_cpu(rec
.e_cpos
)+ le16_to_cpu(rec
.e_leaf_clusters
);
814 up_read(&OCFS2_I(inode
)->ip_alloc_sem
);
816 ocfs2_inode_unlock(inode
, 0);
822 /* Is IO overwriting allocated blocks? */
823 int ocfs2_overwrite_io(struct inode
*inode
, struct buffer_head
*di_bh
,
824 u64 map_start
, u64 map_len
)
826 int ret
= 0, is_last
;
827 u32 mapping_end
, cpos
;
828 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
829 struct ocfs2_extent_rec rec
;
831 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
832 if (ocfs2_size_fits_inline_data(di_bh
, map_start
+ map_len
))
838 cpos
= map_start
>> osb
->s_clustersize_bits
;
839 mapping_end
= ocfs2_clusters_for_bytes(inode
->i_sb
,
840 map_start
+ map_len
);
842 while (cpos
< mapping_end
&& !is_last
) {
843 ret
= ocfs2_get_clusters_nocache(inode
, di_bh
, cpos
,
844 NULL
, &rec
, &is_last
);
850 if (rec
.e_blkno
== 0ULL)
853 if (rec
.e_flags
& OCFS2_EXT_REFCOUNTED
)
856 cpos
= le32_to_cpu(rec
.e_cpos
) +
857 le16_to_cpu(rec
.e_leaf_clusters
);
860 if (cpos
< mapping_end
)
866 int ocfs2_seek_data_hole_offset(struct file
*file
, loff_t
*offset
, int whence
)
868 struct inode
*inode
= file
->f_mapping
->host
;
870 unsigned int is_last
= 0, is_data
= 0;
871 u16 cs_bits
= OCFS2_SB(inode
->i_sb
)->s_clustersize_bits
;
872 u32 cpos
, cend
, clen
, hole_size
;
874 struct buffer_head
*di_bh
= NULL
;
875 struct ocfs2_extent_rec rec
;
877 BUG_ON(whence
!= SEEK_DATA
&& whence
!= SEEK_HOLE
);
879 ret
= ocfs2_inode_lock(inode
, &di_bh
, 0);
885 down_read(&OCFS2_I(inode
)->ip_alloc_sem
);
887 if (*offset
>= i_size_read(inode
)) {
892 if (OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) {
893 if (whence
== SEEK_HOLE
)
894 *offset
= i_size_read(inode
);
899 cpos
= *offset
>> cs_bits
;
900 cend
= ocfs2_clusters_for_bytes(inode
->i_sb
, i_size_read(inode
));
902 while (cpos
< cend
&& !is_last
) {
903 ret
= ocfs2_get_clusters_nocache(inode
, di_bh
, cpos
, &hole_size
,
913 if (rec
.e_blkno
== 0ULL) {
917 clen
= le16_to_cpu(rec
.e_leaf_clusters
) -
918 (cpos
- le32_to_cpu(rec
.e_cpos
));
919 is_data
= (rec
.e_flags
& OCFS2_EXT_UNWRITTEN
) ? 0 : 1;
922 if ((!is_data
&& whence
== SEEK_HOLE
) ||
923 (is_data
&& whence
== SEEK_DATA
)) {
924 if (extoff
> *offset
)
933 if (whence
== SEEK_HOLE
) {
939 if ((extoff
+ extlen
) > i_size_read(inode
))
940 extlen
= i_size_read(inode
) - extoff
;
942 if (extoff
> *offset
)
953 up_read(&OCFS2_I(inode
)->ip_alloc_sem
);
955 ocfs2_inode_unlock(inode
, 0);
960 int ocfs2_read_virt_blocks(struct inode
*inode
, u64 v_block
, int nr
,
961 struct buffer_head
*bhs
[], int flags
,
962 int (*validate
)(struct super_block
*sb
,
963 struct buffer_head
*bh
))
966 u64 p_block
, p_count
;
967 int i
, count
, done
= 0;
969 trace_ocfs2_read_virt_blocks(
970 inode
, (unsigned long long)v_block
, nr
, bhs
, flags
,
973 if (((v_block
+ nr
- 1) << inode
->i_sb
->s_blocksize_bits
) >=
974 i_size_read(inode
)) {
975 BUG_ON(!(flags
& OCFS2_BH_READAHEAD
));
980 down_read(&OCFS2_I(inode
)->ip_alloc_sem
);
981 rc
= ocfs2_extent_map_get_blocks(inode
, v_block
+ done
,
982 &p_block
, &p_count
, NULL
);
983 up_read(&OCFS2_I(inode
)->ip_alloc_sem
);
992 "Inode #%llu contains a hole at offset %llu\n",
993 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
994 (unsigned long long)(v_block
+ done
) <<
995 inode
->i_sb
->s_blocksize_bits
);
1000 if (p_count
< count
)
1004 * If the caller passed us bhs, they should have come
1005 * from a previous readahead call to this function. Thus,
1006 * they should have the right b_blocknr.
1008 for (i
= 0; i
< count
; i
++) {
1011 BUG_ON(bhs
[done
+ i
]->b_blocknr
!= (p_block
+ i
));
1014 rc
= ocfs2_read_blocks(INODE_CACHE(inode
), p_block
, count
,
1015 bhs
+ done
, flags
, validate
);