Blame view

fs/ocfs2/extent_map.c 24.2 KB
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1
2
3
4
5
  /* -*- mode: c; c-basic-offset: 8; -*-
   * vim: noexpandtab sw=8 ts=8 sts=0:
   *
   * extent_map.c
   *
363041a5f   Mark Fasheh   ocfs2: temporaril...
6
   * Block/Cluster mapping functions
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
   *
   * Copyright (C) 2004 Oracle.  All rights reserved.
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public
   * License, version 2,  as published by the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * General Public License for more details.
   *
   * You should have received a copy of the GNU General Public
   * License along with this program; if not, write to the
   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   * Boston, MA 021110-1307, USA.
   */
  
  #include <linux/fs.h>
  #include <linux/init.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
27
  #include <linux/slab.h>
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
28
  #include <linux/types.h>
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
29
  #include <linux/fiemap.h>
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
30

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
31
32
33
  #include <cluster/masklog.h>
  
  #include "ocfs2.h"
363041a5f   Mark Fasheh   ocfs2: temporaril...
34
  #include "alloc.h"
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
35
  #include "dlmglue.h"
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
36
37
38
  #include "extent_map.h"
  #include "inode.h"
  #include "super.h"
86239d59e   Tristan Ye   Ocfs2: Let ocfs2 ...
39
  #include "symlink.h"
ac604d3cd   Gang He   ocfs2: add ocfs2_...
40
  #include "aops.h"
a716357c4   Tao Ma   ocfs2: Remove mas...
41
  #include "ocfs2_trace.h"
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
42
43
  
  #include "buffer_head_io.h"
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
44
  /*
834189788   Mark Fasheh   ocfs2: Cache exte...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
   * The extent caching implementation is intentionally trivial.
   *
   * We only cache a small number of extents stored directly on the
   * inode, so linear order operations are acceptable. If we ever want
   * to increase the size of the extent map, then these algorithms must
   * get smarter.
   */
  
  void ocfs2_extent_map_init(struct inode *inode)
  {
  	struct ocfs2_inode_info *oi = OCFS2_I(inode);
  
  	oi->ip_extent_map.em_num_items = 0;
  	INIT_LIST_HEAD(&oi->ip_extent_map.em_list);
  }
  
  static void __ocfs2_extent_map_lookup(struct ocfs2_extent_map *em,
  				      unsigned int cpos,
  				      struct ocfs2_extent_map_item **ret_emi)
  {
  	unsigned int range;
  	struct ocfs2_extent_map_item *emi;
  
  	*ret_emi = NULL;
  
  	list_for_each_entry(emi, &em->em_list, ei_list) {
  		range = emi->ei_cpos + emi->ei_clusters;
  
  		if (cpos >= emi->ei_cpos && cpos < range) {
  			list_move(&emi->ei_list, &em->em_list);
  
  			*ret_emi = emi;
  			break;
  		}
  	}
  }
  
  static int ocfs2_extent_map_lookup(struct inode *inode, unsigned int cpos,
  				   unsigned int *phys, unsigned int *len,
  				   unsigned int *flags)
  {
  	unsigned int coff;
  	struct ocfs2_inode_info *oi = OCFS2_I(inode);
  	struct ocfs2_extent_map_item *emi;
  
  	spin_lock(&oi->ip_lock);
  
  	__ocfs2_extent_map_lookup(&oi->ip_extent_map, cpos, &emi);
  	if (emi) {
  		coff = cpos - emi->ei_cpos;
  		*phys = emi->ei_phys + coff;
  		if (len)
  			*len = emi->ei_clusters - coff;
  		if (flags)
  			*flags = emi->ei_flags;
  	}
  
  	spin_unlock(&oi->ip_lock);
  
  	if (emi == NULL)
  		return -ENOENT;
  
  	return 0;
  }
  
  /*
   * Forget about all clusters equal to or greater than cpos.
   */
  void ocfs2_extent_map_trunc(struct inode *inode, unsigned int cpos)
  {
800deef3f   Christoph Hellwig   [PATCH] ocfs2: us...
115
  	struct ocfs2_extent_map_item *emi, *n;
834189788   Mark Fasheh   ocfs2: Cache exte...
116
117
118
119
120
121
  	struct ocfs2_inode_info *oi = OCFS2_I(inode);
  	struct ocfs2_extent_map *em = &oi->ip_extent_map;
  	LIST_HEAD(tmp_list);
  	unsigned int range;
  
  	spin_lock(&oi->ip_lock);
800deef3f   Christoph Hellwig   [PATCH] ocfs2: us...
122
  	list_for_each_entry_safe(emi, n, &em->em_list, ei_list) {
834189788   Mark Fasheh   ocfs2: Cache exte...
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  		if (emi->ei_cpos >= cpos) {
  			/* Full truncate of this record. */
  			list_move(&emi->ei_list, &tmp_list);
  			BUG_ON(em->em_num_items == 0);
  			em->em_num_items--;
  			continue;
  		}
  
  		range = emi->ei_cpos + emi->ei_clusters;
  		if (range > cpos) {
  			/* Partial truncate */
  			emi->ei_clusters = cpos - emi->ei_cpos;
  		}
  	}
  	spin_unlock(&oi->ip_lock);
800deef3f   Christoph Hellwig   [PATCH] ocfs2: us...
138
  	list_for_each_entry_safe(emi, n, &tmp_list, ei_list) {
834189788   Mark Fasheh   ocfs2: Cache exte...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  		list_del(&emi->ei_list);
  		kfree(emi);
  	}
  }
  
  /*
   * Is any part of emi2 contained within emi1
   */
  static int ocfs2_ei_is_contained(struct ocfs2_extent_map_item *emi1,
  				 struct ocfs2_extent_map_item *emi2)
  {
  	unsigned int range1, range2;
  
  	/*
  	 * Check if logical start of emi2 is inside emi1
  	 */
  	range1 = emi1->ei_cpos + emi1->ei_clusters;
  	if (emi2->ei_cpos >= emi1->ei_cpos && emi2->ei_cpos < range1)
  		return 1;
  
  	/*
  	 * Check if logical end of emi2 is inside emi1
  	 */
  	range2 = emi2->ei_cpos + emi2->ei_clusters;
  	if (range2 > emi1->ei_cpos && range2 <= range1)
  		return 1;
  
  	return 0;
  }
  
  static void ocfs2_copy_emi_fields(struct ocfs2_extent_map_item *dest,
  				  struct ocfs2_extent_map_item *src)
  {
  	dest->ei_cpos = src->ei_cpos;
  	dest->ei_phys = src->ei_phys;
  	dest->ei_clusters = src->ei_clusters;
  	dest->ei_flags = src->ei_flags;
  }
  
  /*
   * Try to merge emi with ins. Returns 1 if merge succeeds, zero
   * otherwise.
   */
  static int ocfs2_try_to_merge_extent_map(struct ocfs2_extent_map_item *emi,
  					 struct ocfs2_extent_map_item *ins)
  {
  	/*
  	 * Handle contiguousness
  	 */
  	if (ins->ei_phys == (emi->ei_phys + emi->ei_clusters) &&
  	    ins->ei_cpos == (emi->ei_cpos + emi->ei_clusters) &&
  	    ins->ei_flags == emi->ei_flags) {
  		emi->ei_clusters += ins->ei_clusters;
  		return 1;
  	} else if ((ins->ei_phys + ins->ei_clusters) == emi->ei_phys &&
bd6b0bf87   Roel Kluin   ocfs2: Fix contig...
194
  		   (ins->ei_cpos + ins->ei_clusters) == emi->ei_cpos &&
834189788   Mark Fasheh   ocfs2: Cache exte...
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
  		   ins->ei_flags == emi->ei_flags) {
  		emi->ei_phys = ins->ei_phys;
  		emi->ei_cpos = ins->ei_cpos;
  		emi->ei_clusters += ins->ei_clusters;
  		return 1;
  	}
  
  	/*
  	 * Overlapping extents - this shouldn't happen unless we've
  	 * split an extent to change it's flags. That is exceedingly
  	 * rare, so there's no sense in trying to optimize it yet.
  	 */
  	if (ocfs2_ei_is_contained(emi, ins) ||
  	    ocfs2_ei_is_contained(ins, emi)) {
  		ocfs2_copy_emi_fields(emi, ins);
  		return 1;
  	}
  
  	/* No merge was possible. */
  	return 0;
  }
  
  /*
   * In order to reduce complexity on the caller, this insert function
   * is intentionally liberal in what it will accept.
   *
   * The only rule is that the truncate call *must* be used whenever
   * records have been deleted. This avoids inserting overlapping
   * records with different physical mappings.
   */
  void ocfs2_extent_map_insert_rec(struct inode *inode,
  				 struct ocfs2_extent_rec *rec)
  {
  	struct ocfs2_inode_info *oi = OCFS2_I(inode);
  	struct ocfs2_extent_map *em = &oi->ip_extent_map;
  	struct ocfs2_extent_map_item *emi, *new_emi = NULL;
  	struct ocfs2_extent_map_item ins;
  
  	ins.ei_cpos = le32_to_cpu(rec->e_cpos);
  	ins.ei_phys = ocfs2_blocks_to_clusters(inode->i_sb,
  					       le64_to_cpu(rec->e_blkno));
  	ins.ei_clusters = le16_to_cpu(rec->e_leaf_clusters);
  	ins.ei_flags = rec->e_flags;
  
  search:
  	spin_lock(&oi->ip_lock);
  
  	list_for_each_entry(emi, &em->em_list, ei_list) {
  		if (ocfs2_try_to_merge_extent_map(emi, &ins)) {
  			list_move(&emi->ei_list, &em->em_list);
  			spin_unlock(&oi->ip_lock);
  			goto out;
  		}
  	}
  
  	/*
  	 * No item could be merged.
  	 *
  	 * Either allocate and add a new item, or overwrite the last recently
  	 * inserted.
  	 */
  
  	if (em->em_num_items < OCFS2_MAX_EXTENT_MAP_ITEMS) {
  		if (new_emi == NULL) {
  			spin_unlock(&oi->ip_lock);
  
  			new_emi = kmalloc(sizeof(*new_emi), GFP_NOFS);
  			if (new_emi == NULL)
  				goto out;
  
  			goto search;
  		}
  
  		ocfs2_copy_emi_fields(new_emi, &ins);
  		list_add(&new_emi->ei_list, &em->em_list);
  		em->em_num_items++;
  		new_emi = NULL;
  	} else {
  		BUG_ON(list_empty(&em->em_list) || em->em_num_items == 0);
  		emi = list_entry(em->em_list.prev,
  				 struct ocfs2_extent_map_item, ei_list);
  		list_move(&emi->ei_list, &em->em_list);
  		ocfs2_copy_emi_fields(emi, &ins);
  	}
  
  	spin_unlock(&oi->ip_lock);
  
  out:
d787ab097   Tim Gardner   ocfs2: remove kfr...
283
  	kfree(new_emi);
834189788   Mark Fasheh   ocfs2: Cache exte...
284
  }
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
285
286
287
288
289
290
291
292
  static int ocfs2_last_eb_is_empty(struct inode *inode,
  				  struct ocfs2_dinode *di)
  {
  	int ret, next_free;
  	u64 last_eb_blk = le64_to_cpu(di->i_last_eb_blk);
  	struct buffer_head *eb_bh = NULL;
  	struct ocfs2_extent_block *eb;
  	struct ocfs2_extent_list *el;
3d03a305d   Joel Becker   ocfs2: Pass ocfs2...
293
  	ret = ocfs2_read_extent_block(INODE_CACHE(inode), last_eb_blk, &eb_bh);
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
294
295
296
297
298
299
300
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  	el = &eb->h_list;
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
301
302
  	if (el->l_tree_depth) {
  		ocfs2_error(inode->i_sb,
7ecef14ab   Joe Perches   ocfs2: neaten do_...
303
304
305
  			    "Inode %lu has non zero tree depth in leaf block %llu
  ",
  			    inode->i_ino,
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
  			    (unsigned long long)eb_bh->b_blocknr);
  		ret = -EROFS;
  		goto out;
  	}
  
  	next_free = le16_to_cpu(el->l_next_free_rec);
  
  	if (next_free == 0 ||
  	    (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0])))
  		ret = 1;
  
  out:
  	brelse(eb_bh);
  	return ret;
  }
834189788   Mark Fasheh   ocfs2: Cache exte...
321
  /*
4f902c377   Mark Fasheh   ocfs2: Fix extent...
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
   * Return the 1st index within el which contains an extent start
   * larger than v_cluster.
   */
  static int ocfs2_search_for_hole_index(struct ocfs2_extent_list *el,
  				       u32 v_cluster)
  {
  	int i;
  	struct ocfs2_extent_rec *rec;
  
  	for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  		rec = &el->l_recs[i];
  
  		if (v_cluster < le32_to_cpu(rec->e_cpos))
  			break;
  	}
  
  	return i;
  }
  
  /*
   * Figure out the size of a hole which starts at v_cluster within the given
   * extent list.
   *
   * If there is no more allocation past v_cluster, we return the maximum
   * cluster size minus v_cluster.
   *
   * If we have in-inode extents, then el points to the dinode list and
   * eb_bh is NULL. Otherwise, eb_bh should point to the extent block
   * containing el.
   */
e73a819db   Tao Ma   ocfs2: Add suppor...
352
353
354
355
356
  int ocfs2_figure_hole_clusters(struct ocfs2_caching_info *ci,
  			       struct ocfs2_extent_list *el,
  			       struct buffer_head *eb_bh,
  			       u32 v_cluster,
  			       u32 *num_clusters)
4f902c377   Mark Fasheh   ocfs2: Fix extent...
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
  {
  	int ret, i;
  	struct buffer_head *next_eb_bh = NULL;
  	struct ocfs2_extent_block *eb, *next_eb;
  
  	i = ocfs2_search_for_hole_index(el, v_cluster);
  
  	if (i == le16_to_cpu(el->l_next_free_rec) && eb_bh) {
  		eb = (struct ocfs2_extent_block *)eb_bh->b_data;
  
  		/*
  		 * Check the next leaf for any extents.
  		 */
  
  		if (le64_to_cpu(eb->h_next_leaf_blk) == 0ULL)
  			goto no_more_extents;
e73a819db   Tao Ma   ocfs2: Add suppor...
373
  		ret = ocfs2_read_extent_block(ci,
5e96581a3   Joel Becker   ocfs2: Wrap exten...
374
375
  					      le64_to_cpu(eb->h_next_leaf_blk),
  					      &next_eb_bh);
4f902c377   Mark Fasheh   ocfs2: Fix extent...
376
377
378
379
  		if (ret) {
  			mlog_errno(ret);
  			goto out;
  		}
4f902c377   Mark Fasheh   ocfs2: Fix extent...
380

5e96581a3   Joel Becker   ocfs2: Wrap exten...
381
  		next_eb = (struct ocfs2_extent_block *)next_eb_bh->b_data;
4f902c377   Mark Fasheh   ocfs2: Fix extent...
382
  		el = &next_eb->h_list;
4f902c377   Mark Fasheh   ocfs2: Fix extent...
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
  		i = ocfs2_search_for_hole_index(el, v_cluster);
  	}
  
  no_more_extents:
  	if (i == le16_to_cpu(el->l_next_free_rec)) {
  		/*
  		 * We're at the end of our existing allocation. Just
  		 * return the maximum number of clusters we could
  		 * possibly allocate.
  		 */
  		*num_clusters = UINT_MAX - v_cluster;
  	} else {
  		*num_clusters = le32_to_cpu(el->l_recs[i].e_cpos) - v_cluster;
  	}
  
  	ret = 0;
  out:
  	brelse(next_eb_bh);
  	return ret;
  }
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
403
404
405
406
407
  static int ocfs2_get_clusters_nocache(struct inode *inode,
  				      struct buffer_head *di_bh,
  				      u32 v_cluster, unsigned int *hole_len,
  				      struct ocfs2_extent_rec *ret_rec,
  				      unsigned int *is_last)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
408
  {
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
409
  	int i, ret, tree_height, len;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
410
  	struct ocfs2_dinode *di;
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
411
  	struct ocfs2_extent_block *uninitialized_var(eb);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
412
  	struct ocfs2_extent_list *el;
363041a5f   Mark Fasheh   ocfs2: temporaril...
413
  	struct ocfs2_extent_rec *rec;
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
414
  	struct buffer_head *eb_bh = NULL;
834189788   Mark Fasheh   ocfs2: Cache exte...
415

00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
416
417
418
  	memset(ret_rec, 0, sizeof(*ret_rec));
  	if (is_last)
  		*is_last = 0;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
419

363041a5f   Mark Fasheh   ocfs2: temporaril...
420
421
  	di = (struct ocfs2_dinode *) di_bh->b_data;
  	el = &di->id2.i_list;
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
422
  	tree_height = le16_to_cpu(el->l_tree_depth);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
423

00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
424
  	if (tree_height > 0) {
facdb77f5   Joel Becker   ocfs2: ocfs2_find...
425
426
  		ret = ocfs2_find_leaf(INODE_CACHE(inode), el, v_cluster,
  				      &eb_bh);
363041a5f   Mark Fasheh   ocfs2: temporaril...
427
428
429
430
  		if (ret) {
  			mlog_errno(ret);
  			goto out;
  		}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
431

363041a5f   Mark Fasheh   ocfs2: temporaril...
432
433
  		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  		el = &eb->h_list;
e48edee2d   Mark Fasheh   ocfs2: make room ...
434
435
436
  
  		if (el->l_tree_depth) {
  			ocfs2_error(inode->i_sb,
7ecef14ab   Joe Perches   ocfs2: neaten do_...
437
438
439
  				    "Inode %lu has non zero tree depth in leaf block %llu
  ",
  				    inode->i_ino,
e48edee2d   Mark Fasheh   ocfs2: make room ...
440
441
442
443
  				    (unsigned long long)eb_bh->b_blocknr);
  			ret = -EROFS;
  			goto out;
  		}
a43db30c7   Joel Becker   ocfs2: silence -E...
444
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
445

363041a5f   Mark Fasheh   ocfs2: temporaril...
446
447
  	i = ocfs2_search_extent_list(el, v_cluster);
  	if (i == -1) {
a43db30c7   Joel Becker   ocfs2: silence -E...
448
  		/*
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
449
  		 * Holes can be larger than the maximum size of an
3ad2f3fbb   Daniel Mack   tree-wide: Assort...
450
  		 * extent, so we return their lengths in a separate
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
451
  		 * field.
a43db30c7   Joel Becker   ocfs2: silence -E...
452
  		 */
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
453
  		if (hole_len) {
e73a819db   Tao Ma   ocfs2: Add suppor...
454
455
  			ret = ocfs2_figure_hole_clusters(INODE_CACHE(inode),
  							 el, eb_bh,
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
456
  							 v_cluster, &len);
4f902c377   Mark Fasheh   ocfs2: Fix extent...
457
458
459
460
  			if (ret) {
  				mlog_errno(ret);
  				goto out;
  			}
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
461
462
  
  			*hole_len = len;
4f902c377   Mark Fasheh   ocfs2: Fix extent...
463
  		}
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
464
465
  		goto out_hole;
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
466

00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
467
  	rec = &el->l_recs[i];
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
468

00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
469
470
471
  	BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
  
  	if (!rec->e_blkno) {
7ecef14ab   Joe Perches   ocfs2: neaten do_...
472
473
474
475
  		ocfs2_error(inode->i_sb,
  			    "Inode %lu has bad extent record (%u, %u, 0)
  ",
  			    inode->i_ino,
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
  			    le32_to_cpu(rec->e_cpos),
  			    ocfs2_rec_clusters(el, rec));
  		ret = -EROFS;
  		goto out;
  	}
  
  	*ret_rec = *rec;
  
  	/*
  	 * Checking for last extent is potentially expensive - we
  	 * might have to look at the next leaf over to see if it's
  	 * empty.
  	 *
  	 * The first two checks are to see whether the caller even
  	 * cares for this information, and if the extent is at least
  	 * the last in it's list.
  	 *
  	 * If those hold true, then the extent is last if any of the
  	 * additional conditions hold true:
  	 *  - Extent list is in-inode
  	 *  - Extent list is right-most
  	 *  - Extent list is 2nd to rightmost, with empty right-most
  	 */
  	if (is_last) {
  		if (i == (le16_to_cpu(el->l_next_free_rec) - 1)) {
  			if (tree_height == 0)
  				*is_last = 1;
  			else if (eb->h_blkno == di->i_last_eb_blk)
  				*is_last = 1;
  			else if (eb->h_next_leaf_blk == di->i_last_eb_blk) {
  				ret = ocfs2_last_eb_is_empty(inode, di);
  				if (ret < 0) {
  					mlog_errno(ret);
  					goto out;
  				}
  				if (ret == 1)
  					*is_last = 1;
  			}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
514
  		}
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
  	}
  
  out_hole:
  	ret = 0;
  out:
  	brelse(eb_bh);
  	return ret;
  }
  
  static void ocfs2_relative_extent_offsets(struct super_block *sb,
  					  u32 v_cluster,
  					  struct ocfs2_extent_rec *rec,
  					  u32 *p_cluster, u32 *num_clusters)
  
  {
  	u32 coff = v_cluster - le32_to_cpu(rec->e_cpos);
  
  	*p_cluster = ocfs2_blocks_to_clusters(sb, le64_to_cpu(rec->e_blkno));
  	*p_cluster = *p_cluster + coff;
  
  	if (num_clusters)
  		*num_clusters = le16_to_cpu(rec->e_leaf_clusters) - coff;
  }
f56654c43   Tao Ma   ocfs2: Add extent...
538
539
  int ocfs2_xattr_get_clusters(struct inode *inode, u32 v_cluster,
  			     u32 *p_cluster, u32 *num_clusters,
1061f9c1c   Tao Ma   ocfs2: Return ext...
540
541
  			     struct ocfs2_extent_list *el,
  			     unsigned int *extent_flags)
f56654c43   Tao Ma   ocfs2: Add extent...
542
543
544
545
546
547
548
549
  {
  	int ret = 0, i;
  	struct buffer_head *eb_bh = NULL;
  	struct ocfs2_extent_block *eb;
  	struct ocfs2_extent_rec *rec;
  	u32 coff;
  
  	if (el->l_tree_depth) {
facdb77f5   Joel Becker   ocfs2: ocfs2_find...
550
551
  		ret = ocfs2_find_leaf(INODE_CACHE(inode), el, v_cluster,
  				      &eb_bh);
f56654c43   Tao Ma   ocfs2: Add extent...
552
553
554
555
556
557
558
559
560
561
  		if (ret) {
  			mlog_errno(ret);
  			goto out;
  		}
  
  		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  		el = &eb->h_list;
  
  		if (el->l_tree_depth) {
  			ocfs2_error(inode->i_sb,
7ecef14ab   Joe Perches   ocfs2: neaten do_...
562
563
564
  				    "Inode %lu has non zero tree depth in xattr leaf block %llu
  ",
  				    inode->i_ino,
f56654c43   Tao Ma   ocfs2: Add extent...
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
  				    (unsigned long long)eb_bh->b_blocknr);
  			ret = -EROFS;
  			goto out;
  		}
  	}
  
  	i = ocfs2_search_extent_list(el, v_cluster);
  	if (i == -1) {
  		ret = -EROFS;
  		mlog_errno(ret);
  		goto out;
  	} else {
  		rec = &el->l_recs[i];
  		BUG_ON(v_cluster < le32_to_cpu(rec->e_cpos));
  
  		if (!rec->e_blkno) {
7ecef14ab   Joe Perches   ocfs2: neaten do_...
581
582
583
584
  			ocfs2_error(inode->i_sb,
  				    "Inode %lu has bad extent record (%u, %u, 0) in xattr
  ",
  				    inode->i_ino,
f56654c43   Tao Ma   ocfs2: Add extent...
585
586
587
588
589
590
591
592
593
594
595
  				    le32_to_cpu(rec->e_cpos),
  				    ocfs2_rec_clusters(el, rec));
  			ret = -EROFS;
  			goto out;
  		}
  		coff = v_cluster - le32_to_cpu(rec->e_cpos);
  		*p_cluster = ocfs2_blocks_to_clusters(inode->i_sb,
  						    le64_to_cpu(rec->e_blkno));
  		*p_cluster = *p_cluster + coff;
  		if (num_clusters)
  			*num_clusters = ocfs2_rec_clusters(el, rec) - coff;
1061f9c1c   Tao Ma   ocfs2: Return ext...
596
597
598
  
  		if (extent_flags)
  			*extent_flags = rec->e_flags;
f56654c43   Tao Ma   ocfs2: Add extent...
599
600
601
602
603
604
  	}
  out:
  	if (eb_bh)
  		brelse(eb_bh);
  	return ret;
  }
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
605
606
607
608
609
610
611
612
  int ocfs2_get_clusters(struct inode *inode, u32 v_cluster,
  		       u32 *p_cluster, u32 *num_clusters,
  		       unsigned int *extent_flags)
  {
  	int ret;
  	unsigned int uninitialized_var(hole_len), flags = 0;
  	struct buffer_head *di_bh = NULL;
  	struct ocfs2_extent_rec rec;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
613

00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
614
615
616
617
618
  	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  		ret = -ERANGE;
  		mlog_errno(ret);
  		goto out;
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
619

00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
620
621
622
623
  	ret = ocfs2_extent_map_lookup(inode, v_cluster, p_cluster,
  				      num_clusters, extent_flags);
  	if (ret == 0)
  		goto out;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
624

b657c95c1   Joel Becker   ocfs2: Wrap inode...
625
  	ret = ocfs2_read_inode_block(inode, &di_bh);
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
626
627
628
629
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
49cb8d2d4   Mark Fasheh   ocfs2: Read from ...
630

00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
631
632
633
634
635
636
  	ret = ocfs2_get_clusters_nocache(inode, di_bh, v_cluster, &hole_len,
  					 &rec, NULL);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
834189788   Mark Fasheh   ocfs2: Cache exte...
637

00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
  	if (rec.e_blkno == 0ULL) {
  		/*
  		 * A hole was found. Return some canned values that
  		 * callers can key on. If asked for, num_clusters will
  		 * be populated with the size of the hole.
  		 */
  		*p_cluster = 0;
  		if (num_clusters) {
  			*num_clusters = hole_len;
  		}
  	} else {
  		ocfs2_relative_extent_offsets(inode->i_sb, v_cluster, &rec,
  					      p_cluster, num_clusters);
  		flags = rec.e_flags;
  
  		ocfs2_extent_map_insert_rec(inode, &rec);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
654
  	}
49cb8d2d4   Mark Fasheh   ocfs2: Read from ...
655
656
  	if (extent_flags)
  		*extent_flags = flags;
363041a5f   Mark Fasheh   ocfs2: temporaril...
657
658
  out:
  	brelse(di_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
659
660
  	return ret;
  }
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
661
  /*
363041a5f   Mark Fasheh   ocfs2: temporaril...
662
663
   * This expects alloc_sem to be held. The allocation cannot change at
   * all while the map is in the process of being updated.
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
664
   */
363041a5f   Mark Fasheh   ocfs2: temporaril...
665
  int ocfs2_extent_map_get_blocks(struct inode *inode, u64 v_blkno, u64 *p_blkno,
4f902c377   Mark Fasheh   ocfs2: Fix extent...
666
  				u64 *ret_count, unsigned int *extent_flags)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
667
668
  {
  	int ret;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
669
  	int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
363041a5f   Mark Fasheh   ocfs2: temporaril...
670
671
  	u32 cpos, num_clusters, p_cluster;
  	u64 boff = 0;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
672
673
  
  	cpos = ocfs2_blocks_to_clusters(inode->i_sb, v_blkno);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
674

49cb8d2d4   Mark Fasheh   ocfs2: Read from ...
675
676
  	ret = ocfs2_get_clusters(inode, cpos, &p_cluster, &num_clusters,
  				 extent_flags);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
677
678
  	if (ret) {
  		mlog_errno(ret);
363041a5f   Mark Fasheh   ocfs2: temporaril...
679
  		goto out;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
680
  	}
363041a5f   Mark Fasheh   ocfs2: temporaril...
681
682
683
684
685
  	/*
  	 * p_cluster == 0 indicates a hole.
  	 */
  	if (p_cluster) {
  		boff = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
686
  		boff += (v_blkno & (u64)(bpc - 1));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
687
  	}
363041a5f   Mark Fasheh   ocfs2: temporaril...
688
  	*p_blkno = boff;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
689

363041a5f   Mark Fasheh   ocfs2: temporaril...
690
691
692
  	if (ret_count) {
  		*ret_count = ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
  		*ret_count -= v_blkno & (u64)(bpc - 1);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
693
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
694

363041a5f   Mark Fasheh   ocfs2: temporaril...
695
696
  out:
  	return ret;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
697
  }
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
698

86239d59e   Tristan Ye   Ocfs2: Let ocfs2 ...
699
700
701
702
703
704
  /*
   * The ocfs2_fiemap_inline() may be a little bit misleading, since
   * it not only handles the fiemap for inlined files, but also deals
   * with the fast symlink, cause they have no difference for extent
   * mapping per se.
   */
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
705
706
707
708
709
710
711
712
713
714
715
716
  static int ocfs2_fiemap_inline(struct inode *inode, struct buffer_head *di_bh,
  			       struct fiemap_extent_info *fieinfo,
  			       u64 map_start)
  {
  	int ret;
  	unsigned int id_count;
  	struct ocfs2_dinode *di;
  	u64 phys;
  	u32 flags = FIEMAP_EXTENT_DATA_INLINE|FIEMAP_EXTENT_LAST;
  	struct ocfs2_inode_info *oi = OCFS2_I(inode);
  
  	di = (struct ocfs2_dinode *)di_bh->b_data;
86239d59e   Tristan Ye   Ocfs2: Let ocfs2 ...
717
718
719
720
  	if (ocfs2_inode_is_fast_symlink(inode))
  		id_count = ocfs2_fast_symlink_chars(inode->i_sb);
  	else
  		id_count = le16_to_cpu(di->id2.i_data.id_count);
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
721
722
723
  
  	if (map_start < id_count) {
  		phys = oi->ip_blkno << inode->i_sb->s_blocksize_bits;
86239d59e   Tristan Ye   Ocfs2: Let ocfs2 ...
724
725
726
727
728
  		if (ocfs2_inode_is_fast_symlink(inode))
  			phys += offsetof(struct ocfs2_dinode, id2.i_symlink);
  		else
  			phys += offsetof(struct ocfs2_dinode,
  					 id2.i_data.id_data);
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
  
  		ret = fiemap_fill_next_extent(fieinfo, 0, phys, id_count,
  					      flags);
  		if (ret < 0)
  			return ret;
  	}
  
  	return 0;
  }
  
  #define OCFS2_FIEMAP_FLAGS	(FIEMAP_FLAG_SYNC)
  
  int ocfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  		 u64 map_start, u64 map_len)
  {
  	int ret, is_last;
  	u32 mapping_end, cpos;
  	unsigned int hole_size;
  	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  	u64 len_bytes, phys_bytes, virt_bytes;
  	struct buffer_head *di_bh = NULL;
  	struct ocfs2_extent_rec rec;
  
  	ret = fiemap_check_flags(fieinfo, OCFS2_FIEMAP_FLAGS);
  	if (ret)
  		return ret;
  
  	ret = ocfs2_inode_lock(inode, &di_bh, 0);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	down_read(&OCFS2_I(inode)->ip_alloc_sem);
  
  	/*
86239d59e   Tristan Ye   Ocfs2: Let ocfs2 ...
765
  	 * Handle inline-data and fast symlink separately.
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
766
  	 */
86239d59e   Tristan Ye   Ocfs2: Let ocfs2 ...
767
768
  	if ((OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
  	    ocfs2_inode_is_fast_symlink(inode)) {
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
769
770
771
772
773
774
775
  		ret = ocfs2_fiemap_inline(inode, di_bh, fieinfo, map_start);
  		goto out_unlock;
  	}
  
  	cpos = map_start >> osb->s_clustersize_bits;
  	mapping_end = ocfs2_clusters_for_bytes(inode->i_sb,
  					       map_start + map_len);
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
776
777
778
779
780
781
782
783
  	is_last = 0;
  	while (cpos < mapping_end && !is_last) {
  		u32 fe_flags;
  
  		ret = ocfs2_get_clusters_nocache(inode, di_bh, cpos,
  						 &hole_size, &rec, &is_last);
  		if (ret) {
  			mlog_errno(ret);
b4ca2b4b5   Joseph Qi   ocfs2: goto out_u...
784
  			goto out_unlock;
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
785
786
787
788
789
790
791
792
793
794
  		}
  
  		if (rec.e_blkno == 0ULL) {
  			cpos += hole_size;
  			continue;
  		}
  
  		fe_flags = 0;
  		if (rec.e_flags & OCFS2_EXT_UNWRITTEN)
  			fe_flags |= FIEMAP_EXTENT_UNWRITTEN;
faf8b70f7   Sunil Mushran   ocfs2: Use FIEMAP...
795
796
  		if (rec.e_flags & OCFS2_EXT_REFCOUNTED)
  			fe_flags |= FIEMAP_EXTENT_SHARED;
00dc417fa   Mark Fasheh   ocfs2: fiemap sup...
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
  		if (is_last)
  			fe_flags |= FIEMAP_EXTENT_LAST;
  		len_bytes = (u64)le16_to_cpu(rec.e_leaf_clusters) << osb->s_clustersize_bits;
  		phys_bytes = le64_to_cpu(rec.e_blkno) << osb->sb->s_blocksize_bits;
  		virt_bytes = (u64)le32_to_cpu(rec.e_cpos) << osb->s_clustersize_bits;
  
  		ret = fiemap_fill_next_extent(fieinfo, virt_bytes, phys_bytes,
  					      len_bytes, fe_flags);
  		if (ret)
  			break;
  
  		cpos = le32_to_cpu(rec.e_cpos)+ le16_to_cpu(rec.e_leaf_clusters);
  	}
  
  	if (ret > 0)
  		ret = 0;
  
  out_unlock:
  	brelse(di_bh);
  
  	up_read(&OCFS2_I(inode)->ip_alloc_sem);
  
  	ocfs2_inode_unlock(inode, 0);
  out:
  
  	return ret;
  }
a8549fb5a   Joel Becker   ocfs2: Wrap virtu...
824

ac604d3cd   Gang He   ocfs2: add ocfs2_...
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
  /* Is IO overwriting allocated blocks? */
  int ocfs2_overwrite_io(struct inode *inode, struct buffer_head *di_bh,
  		       u64 map_start, u64 map_len)
  {
  	int ret = 0, is_last;
  	u32 mapping_end, cpos;
  	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  	struct ocfs2_extent_rec rec;
  
  	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  		if (ocfs2_size_fits_inline_data(di_bh, map_start + map_len))
  			return ret;
  		else
  			return -EAGAIN;
  	}
  
  	cpos = map_start >> osb->s_clustersize_bits;
  	mapping_end = ocfs2_clusters_for_bytes(inode->i_sb,
  					       map_start + map_len);
  	is_last = 0;
  	while (cpos < mapping_end && !is_last) {
  		ret = ocfs2_get_clusters_nocache(inode, di_bh, cpos,
  						 NULL, &rec, &is_last);
  		if (ret) {
  			mlog_errno(ret);
  			goto out;
  		}
  
  		if (rec.e_blkno == 0ULL)
  			break;
  
  		if (rec.e_flags & OCFS2_EXT_REFCOUNTED)
  			break;
  
  		cpos = le32_to_cpu(rec.e_cpos) +
  			le16_to_cpu(rec.e_leaf_clusters);
  	}
  
  	if (cpos < mapping_end)
  		ret = -EAGAIN;
  out:
  	return ret;
  }
965c8e59c   Andrew Morton   lseek: the "whenc...
868
  int ocfs2_seek_data_hole_offset(struct file *file, loff_t *offset, int whence)
93862d5e1   Sunil Mushran   ocfs2: Implement ...
869
870
871
872
873
874
875
876
877
  {
  	struct inode *inode = file->f_mapping->host;
  	int ret;
  	unsigned int is_last = 0, is_data = 0;
  	u16 cs_bits = OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  	u32 cpos, cend, clen, hole_size;
  	u64 extoff, extlen;
  	struct buffer_head *di_bh = NULL;
  	struct ocfs2_extent_rec rec;
965c8e59c   Andrew Morton   lseek: the "whenc...
878
  	BUG_ON(whence != SEEK_DATA && whence != SEEK_HOLE);
93862d5e1   Sunil Mushran   ocfs2: Implement ...
879
880
881
882
883
884
885
886
  
  	ret = ocfs2_inode_lock(inode, &di_bh, 0);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	down_read(&OCFS2_I(inode)->ip_alloc_sem);
f17c20dd2   Junxiao Bi   ocfs2: use i_size...
887
  	if (*offset >= i_size_read(inode)) {
93862d5e1   Sunil Mushran   ocfs2: Implement ...
888
889
890
891
892
  		ret = -ENXIO;
  		goto out_unlock;
  	}
  
  	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
965c8e59c   Andrew Morton   lseek: the "whenc...
893
  		if (whence == SEEK_HOLE)
f17c20dd2   Junxiao Bi   ocfs2: use i_size...
894
  			*offset = i_size_read(inode);
93862d5e1   Sunil Mushran   ocfs2: Implement ...
895
896
897
898
899
  		goto out_unlock;
  	}
  
  	clen = 0;
  	cpos = *offset >> cs_bits;
f17c20dd2   Junxiao Bi   ocfs2: use i_size...
900
  	cend = ocfs2_clusters_for_bytes(inode->i_sb, i_size_read(inode));
93862d5e1   Sunil Mushran   ocfs2: Implement ...
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
  
  	while (cpos < cend && !is_last) {
  		ret = ocfs2_get_clusters_nocache(inode, di_bh, cpos, &hole_size,
  						 &rec, &is_last);
  		if (ret) {
  			mlog_errno(ret);
  			goto out_unlock;
  		}
  
  		extoff = cpos;
  		extoff <<= cs_bits;
  
  		if (rec.e_blkno == 0ULL) {
  			clen = hole_size;
  			is_data = 0;
  		} else {
  			clen = le16_to_cpu(rec.e_leaf_clusters) -
  				(cpos - le32_to_cpu(rec.e_cpos));
  			is_data = (rec.e_flags & OCFS2_EXT_UNWRITTEN) ?  0 : 1;
  		}
965c8e59c   Andrew Morton   lseek: the "whenc...
921
922
  		if ((!is_data && whence == SEEK_HOLE) ||
  		    (is_data && whence == SEEK_DATA)) {
93862d5e1   Sunil Mushran   ocfs2: Implement ...
923
924
925
926
927
928
929
930
  			if (extoff > *offset)
  				*offset = extoff;
  			goto out_unlock;
  		}
  
  		if (!is_last)
  			cpos += clen;
  	}
965c8e59c   Andrew Morton   lseek: the "whenc...
931
  	if (whence == SEEK_HOLE) {
93862d5e1   Sunil Mushran   ocfs2: Implement ...
932
933
934
935
  		extoff = cpos;
  		extoff <<= cs_bits;
  		extlen = clen;
  		extlen <<=  cs_bits;
f17c20dd2   Junxiao Bi   ocfs2: use i_size...
936
937
  		if ((extoff + extlen) > i_size_read(inode))
  			extlen = i_size_read(inode) - extoff;
93862d5e1   Sunil Mushran   ocfs2: Implement ...
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
  		extoff += extlen;
  		if (extoff > *offset)
  			*offset = extoff;
  		goto out_unlock;
  	}
  
  	ret = -ENXIO;
  
  out_unlock:
  
  	brelse(di_bh);
  
  	up_read(&OCFS2_I(inode)->ip_alloc_sem);
  
  	ocfs2_inode_unlock(inode, 0);
  out:
93862d5e1   Sunil Mushran   ocfs2: Implement ...
954
955
  	return ret;
  }
a8549fb5a   Joel Becker   ocfs2: Wrap virtu...
956
957
958
959
960
961
962
963
  int ocfs2_read_virt_blocks(struct inode *inode, u64 v_block, int nr,
  			   struct buffer_head *bhs[], int flags,
  			   int (*validate)(struct super_block *sb,
  					   struct buffer_head *bh))
  {
  	int rc = 0;
  	u64 p_block, p_count;
  	int i, count, done = 0;
a716357c4   Tao Ma   ocfs2: Remove mas...
964
  	trace_ocfs2_read_virt_blocks(
ef6b689b6   Tao Ma   ocfs2: Remove ENT...
965
966
  	     inode, (unsigned long long)v_block, nr, bhs, flags,
  	     validate);
a8549fb5a   Joel Becker   ocfs2: Wrap virtu...
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
  
  	if (((v_block + nr - 1) << inode->i_sb->s_blocksize_bits) >=
  	    i_size_read(inode)) {
  		BUG_ON(!(flags & OCFS2_BH_READAHEAD));
  		goto out;
  	}
  
  	while (done < nr) {
  		down_read(&OCFS2_I(inode)->ip_alloc_sem);
  		rc = ocfs2_extent_map_get_blocks(inode, v_block + done,
  						 &p_block, &p_count, NULL);
  		up_read(&OCFS2_I(inode)->ip_alloc_sem);
  		if (rc) {
  			mlog_errno(rc);
  			break;
  		}
  
  		if (!p_block) {
  			rc = -EIO;
  			mlog(ML_ERROR,
  			     "Inode #%llu contains a hole at offset %llu
  ",
  			     (unsigned long long)OCFS2_I(inode)->ip_blkno,
  			     (unsigned long long)(v_block + done) <<
  			     inode->i_sb->s_blocksize_bits);
  			break;
  		}
  
  		count = nr - done;
  		if (p_count < count)
  			count = p_count;
  
  		/*
  		 * If the caller passed us bhs, they should have come
  		 * from a previous readahead call to this function.  Thus,
  		 * they should have the right b_blocknr.
  		 */
  		for (i = 0; i < count; i++) {
  			if (!bhs[done + i])
  				continue;
  			BUG_ON(bhs[done + i]->b_blocknr != (p_block + i));
  		}
8cb471e8f   Joel Becker   ocfs2: Take the i...
1009
1010
  		rc = ocfs2_read_blocks(INODE_CACHE(inode), p_block, count,
  				       bhs + done, flags, validate);
a8549fb5a   Joel Becker   ocfs2: Wrap virtu...
1011
1012
1013
1014
1015
1016
1017
1018
  		if (rc) {
  			mlog_errno(rc);
  			break;
  		}
  		done += count;
  	}
  
  out:
a8549fb5a   Joel Becker   ocfs2: Wrap virtu...
1019
1020
  	return rc;
  }