block.c revision d8fae3cf421eb5ebc0cf8726885eb81aff0a5d1f
1/*
2 * block.c --- iterate over all blocks in an inode
3 *
4 * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
9 * %End-Header%
10 */
11
12#include <stdio.h>
13#include <string.h>
14#if HAVE_UNISTD_H
15#include <unistd.h>
16#endif
17
18#include "ext2_fs.h"
19#include "ext2fs.h"
20
21struct block_context {
22	ext2_filsys	fs;
23	int (*func)(ext2_filsys	fs,
24		    blk64_t	*blocknr,
25		    e2_blkcnt_t	bcount,
26		    blk64_t	ref_blk,
27		    int		ref_offset,
28		    void	*priv_data);
29	e2_blkcnt_t	bcount;
30	int		bsize;
31	int		flags;
32	errcode_t	errcode;
33	char	*ind_buf;
34	char	*dind_buf;
35	char	*tind_buf;
36	void	*priv_data;
37};
38
39#define check_for_ro_violation_return(ctx, ret)				\
40	do {								\
41		if (((ctx)->flags & BLOCK_FLAG_READ_ONLY) &&		\
42		    ((ret) & BLOCK_CHANGED)) {				\
43			(ctx)->errcode = EXT2_ET_RO_BLOCK_ITERATE;	\
44			ret |= BLOCK_ABORT | BLOCK_ERROR;		\
45			return ret;					\
46		}							\
47	} while (0)
48
49#define check_for_ro_violation_goto(ctx, ret, label)			\
50	do {								\
51		if (((ctx)->flags & BLOCK_FLAG_READ_ONLY) &&		\
52		    ((ret) & BLOCK_CHANGED)) {				\
53			(ctx)->errcode = EXT2_ET_RO_BLOCK_ITERATE;	\
54			ret |= BLOCK_ABORT | BLOCK_ERROR;		\
55			goto label;					\
56		}							\
57	} while (0)
58
59static int block_iterate_ind(blk_t *ind_block, blk_t ref_block,
60			     int ref_offset, struct block_context *ctx)
61{
62	int	ret = 0, changed = 0;
63	int	i, flags, limit, offset;
64	blk_t	*block_nr;
65	blk64_t	blk64;
66
67	limit = ctx->fs->blocksize >> 2;
68	if (!(ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) &&
69	    !(ctx->flags & BLOCK_FLAG_DATA_ONLY)) {
70		blk64 = *ind_block;
71		ret = (*ctx->func)(ctx->fs, &blk64,
72				   BLOCK_COUNT_IND, ref_block,
73				   ref_offset, ctx->priv_data);
74		*ind_block = blk64;
75	}
76	check_for_ro_violation_return(ctx, ret);
77	if (!*ind_block || (ret & BLOCK_ABORT)) {
78		ctx->bcount += limit;
79		return ret;
80	}
81	if (*ind_block >= ext2fs_blocks_count(ctx->fs->super) ||
82	    *ind_block < ctx->fs->super->s_first_data_block) {
83		ctx->errcode = EXT2_ET_BAD_IND_BLOCK;
84		ret |= BLOCK_ERROR;
85		return ret;
86	}
87	ctx->errcode = ext2fs_read_ind_block(ctx->fs, *ind_block,
88					     ctx->ind_buf);
89	if (ctx->errcode) {
90		ret |= BLOCK_ERROR;
91		return ret;
92	}
93
94	block_nr = (blk_t *) ctx->ind_buf;
95	offset = 0;
96	if (ctx->flags & BLOCK_FLAG_APPEND) {
97		for (i = 0; i < limit; i++, ctx->bcount++, block_nr++) {
98			blk64 = *block_nr;
99			flags = (*ctx->func)(ctx->fs, &blk64, ctx->bcount,
100					     *ind_block, offset,
101					     ctx->priv_data);
102			*block_nr = blk64;
103			changed	|= flags;
104			if (flags & BLOCK_ABORT) {
105				ret |= BLOCK_ABORT;
106				break;
107			}
108			offset += sizeof(blk_t);
109		}
110	} else {
111		for (i = 0; i < limit; i++, ctx->bcount++, block_nr++) {
112			if (*block_nr == 0)
113				goto skip_sparse;
114			blk64 = *block_nr;
115			flags = (*ctx->func)(ctx->fs, &blk64, ctx->bcount,
116					     *ind_block, offset,
117					     ctx->priv_data);
118			*block_nr = blk64;
119			changed	|= flags;
120			if (flags & BLOCK_ABORT) {
121				ret |= BLOCK_ABORT;
122				break;
123			}
124		skip_sparse:
125			offset += sizeof(blk_t);
126		}
127	}
128	check_for_ro_violation_return(ctx, changed);
129	if (changed & BLOCK_CHANGED) {
130		ctx->errcode = ext2fs_write_ind_block(ctx->fs, *ind_block,
131						      ctx->ind_buf);
132		if (ctx->errcode)
133			ret |= BLOCK_ERROR | BLOCK_ABORT;
134	}
135	if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) &&
136	    !(ctx->flags & BLOCK_FLAG_DATA_ONLY) &&
137	    !(ret & BLOCK_ABORT)) {
138		blk64 = *ind_block;
139		ret |= (*ctx->func)(ctx->fs, &blk64,
140				    BLOCK_COUNT_IND, ref_block,
141				    ref_offset, ctx->priv_data);
142		*ind_block = blk64;
143	}
144	check_for_ro_violation_return(ctx, ret);
145	return ret;
146}
147
148static int block_iterate_dind(blk_t *dind_block, blk_t ref_block,
149			      int ref_offset, struct block_context *ctx)
150{
151	int	ret = 0, changed = 0;
152	int	i, flags, limit, offset;
153	blk_t	*block_nr;
154	blk64_t	blk64;
155
156	limit = ctx->fs->blocksize >> 2;
157	if (!(ctx->flags & (BLOCK_FLAG_DEPTH_TRAVERSE |
158			    BLOCK_FLAG_DATA_ONLY))) {
159		blk64 = *dind_block;
160		ret = (*ctx->func)(ctx->fs, &blk64,
161				   BLOCK_COUNT_DIND, ref_block,
162				   ref_offset, ctx->priv_data);
163		*dind_block = blk64;
164	}
165	check_for_ro_violation_return(ctx, ret);
166	if (!*dind_block || (ret & BLOCK_ABORT)) {
167		ctx->bcount += limit*limit;
168		return ret;
169	}
170	if (*dind_block >= ext2fs_blocks_count(ctx->fs->super) ||
171	    *dind_block < ctx->fs->super->s_first_data_block) {
172		ctx->errcode = EXT2_ET_BAD_DIND_BLOCK;
173		ret |= BLOCK_ERROR;
174		return ret;
175	}
176	ctx->errcode = ext2fs_read_ind_block(ctx->fs, *dind_block,
177					     ctx->dind_buf);
178	if (ctx->errcode) {
179		ret |= BLOCK_ERROR;
180		return ret;
181	}
182
183	block_nr = (blk_t *) ctx->dind_buf;
184	offset = 0;
185	if (ctx->flags & BLOCK_FLAG_APPEND) {
186		for (i = 0; i < limit; i++, block_nr++) {
187			flags = block_iterate_ind(block_nr,
188						  *dind_block, offset,
189						  ctx);
190			changed |= flags;
191			if (flags & (BLOCK_ABORT | BLOCK_ERROR)) {
192				ret |= flags & (BLOCK_ABORT | BLOCK_ERROR);
193				break;
194			}
195			offset += sizeof(blk_t);
196		}
197	} else {
198		for (i = 0; i < limit; i++, block_nr++) {
199			if (*block_nr == 0) {
200				ctx->bcount += limit;
201				continue;
202			}
203			flags = block_iterate_ind(block_nr,
204						  *dind_block, offset,
205						  ctx);
206			changed |= flags;
207			if (flags & (BLOCK_ABORT | BLOCK_ERROR)) {
208				ret |= flags & (BLOCK_ABORT | BLOCK_ERROR);
209				break;
210			}
211			offset += sizeof(blk_t);
212		}
213	}
214	check_for_ro_violation_return(ctx, changed);
215	if (changed & BLOCK_CHANGED) {
216		ctx->errcode = ext2fs_write_ind_block(ctx->fs, *dind_block,
217						      ctx->dind_buf);
218		if (ctx->errcode)
219			ret |= BLOCK_ERROR | BLOCK_ABORT;
220	}
221	if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) &&
222	    !(ctx->flags & BLOCK_FLAG_DATA_ONLY) &&
223	    !(ret & BLOCK_ABORT)) {
224		blk64 = *dind_block;
225		ret |= (*ctx->func)(ctx->fs, &blk64,
226				    BLOCK_COUNT_DIND, ref_block,
227				    ref_offset, ctx->priv_data);
228		*dind_block = blk64;
229	}
230	check_for_ro_violation_return(ctx, ret);
231	return ret;
232}
233
234static int block_iterate_tind(blk_t *tind_block, blk_t ref_block,
235			      int ref_offset, struct block_context *ctx)
236{
237	int	ret = 0, changed = 0;
238	int	i, flags, limit, offset;
239	blk_t	*block_nr;
240	blk64_t	blk64;
241
242	limit = ctx->fs->blocksize >> 2;
243	if (!(ctx->flags & (BLOCK_FLAG_DEPTH_TRAVERSE |
244			    BLOCK_FLAG_DATA_ONLY))) {
245		blk64 = *tind_block;
246		ret = (*ctx->func)(ctx->fs, &blk64,
247				   BLOCK_COUNT_TIND, ref_block,
248				   ref_offset, ctx->priv_data);
249		*tind_block = blk64;
250	}
251	check_for_ro_violation_return(ctx, ret);
252	if (!*tind_block || (ret & BLOCK_ABORT)) {
253		ctx->bcount += limit*limit*limit;
254		return ret;
255	}
256	if (*tind_block >= ext2fs_blocks_count(ctx->fs->super) ||
257	    *tind_block < ctx->fs->super->s_first_data_block) {
258		ctx->errcode = EXT2_ET_BAD_TIND_BLOCK;
259		ret |= BLOCK_ERROR;
260		return ret;
261	}
262	ctx->errcode = ext2fs_read_ind_block(ctx->fs, *tind_block,
263					     ctx->tind_buf);
264	if (ctx->errcode) {
265		ret |= BLOCK_ERROR;
266		return ret;
267	}
268
269	block_nr = (blk_t *) ctx->tind_buf;
270	offset = 0;
271	if (ctx->flags & BLOCK_FLAG_APPEND) {
272		for (i = 0; i < limit; i++, block_nr++) {
273			flags = block_iterate_dind(block_nr,
274						   *tind_block,
275						   offset, ctx);
276			changed |= flags;
277			if (flags & (BLOCK_ABORT | BLOCK_ERROR)) {
278				ret |= flags & (BLOCK_ABORT | BLOCK_ERROR);
279				break;
280			}
281			offset += sizeof(blk_t);
282		}
283	} else {
284		for (i = 0; i < limit; i++, block_nr++) {
285			if (*block_nr == 0) {
286				ctx->bcount += limit*limit;
287				continue;
288			}
289			flags = block_iterate_dind(block_nr,
290						   *tind_block,
291						   offset, ctx);
292			changed |= flags;
293			if (flags & (BLOCK_ABORT | BLOCK_ERROR)) {
294				ret |= flags & (BLOCK_ABORT | BLOCK_ERROR);
295				break;
296			}
297			offset += sizeof(blk_t);
298		}
299	}
300	check_for_ro_violation_return(ctx, changed);
301	if (changed & BLOCK_CHANGED) {
302		ctx->errcode = ext2fs_write_ind_block(ctx->fs, *tind_block,
303						      ctx->tind_buf);
304		if (ctx->errcode)
305			ret |= BLOCK_ERROR | BLOCK_ABORT;
306	}
307	if ((ctx->flags & BLOCK_FLAG_DEPTH_TRAVERSE) &&
308	    !(ctx->flags & BLOCK_FLAG_DATA_ONLY) &&
309	    !(ret & BLOCK_ABORT)) {
310		blk64 = *tind_block;
311		ret |= (*ctx->func)(ctx->fs, &blk64,
312				    BLOCK_COUNT_TIND, ref_block,
313				    ref_offset, ctx->priv_data);
314		*tind_block = blk64;
315	}
316	check_for_ro_violation_return(ctx, ret);
317	return ret;
318}
319
320errcode_t ext2fs_block_iterate3(ext2_filsys fs,
321				ext2_ino_t ino,
322				int	flags,
323				char *block_buf,
324				int (*func)(ext2_filsys fs,
325					    blk64_t	*blocknr,
326					    e2_blkcnt_t	blockcnt,
327					    blk64_t	ref_blk,
328					    int		ref_offset,
329					    void	*priv_data),
330				void *priv_data)
331{
332	int	i;
333	int	r, ret = 0;
334	struct ext2_inode inode;
335	errcode_t	retval;
336	struct block_context ctx;
337	int	limit;
338	blk64_t	blk64;
339
340	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
341
342	ctx.errcode = ext2fs_read_inode(fs, ino, &inode);
343	if (ctx.errcode)
344		return ctx.errcode;
345
346	/*
347	 * Check to see if we need to limit large files
348	 */
349	if (flags & BLOCK_FLAG_NO_LARGE) {
350		if (!LINUX_S_ISDIR(inode.i_mode) &&
351		    (inode.i_size_high != 0))
352			return EXT2_ET_FILE_TOO_BIG;
353	}
354
355	limit = fs->blocksize >> 2;
356
357	ctx.fs = fs;
358	ctx.func = func;
359	ctx.priv_data = priv_data;
360	ctx.flags = flags;
361	ctx.bcount = 0;
362	if (block_buf) {
363		ctx.ind_buf = block_buf;
364	} else {
365		retval = ext2fs_get_array(3, fs->blocksize, &ctx.ind_buf);
366		if (retval)
367			return retval;
368	}
369	ctx.dind_buf = ctx.ind_buf + fs->blocksize;
370	ctx.tind_buf = ctx.dind_buf + fs->blocksize;
371
372	/*
373	 * Iterate over the HURD translator block (if present)
374	 */
375	if ((fs->super->s_creator_os == EXT2_OS_HURD) &&
376	    !(flags & BLOCK_FLAG_DATA_ONLY)) {
377		if (inode.osd1.hurd1.h_i_translator) {
378			blk64 = inode.osd1.hurd1.h_i_translator;
379			ret |= (*ctx.func)(fs, &blk64,
380					   BLOCK_COUNT_TRANSLATOR,
381					   0, 0, priv_data);
382			inode.osd1.hurd1.h_i_translator = (blk_t) blk64;
383			if (ret & BLOCK_ABORT)
384				goto abort_exit;
385			check_for_ro_violation_goto(&ctx, ret, abort_exit);
386		}
387	}
388
389	if (inode.i_flags & EXT4_EXTENTS_FL) {
390		ext2_extent_handle_t	handle;
391		struct ext2fs_extent	extent;
392		e2_blkcnt_t		blockcnt = 0;
393		blk64_t			blk, new_blk;
394		int			op = EXT2_EXTENT_ROOT;
395		int			uninit;
396		unsigned int		j;
397
398		ctx.errcode = ext2fs_extent_open2(fs, ino, &inode, &handle);
399		if (ctx.errcode)
400			goto abort_exit;
401
402		while (1) {
403			ctx.errcode = ext2fs_extent_get(handle, op, &extent);
404			if (ctx.errcode) {
405				if (ctx.errcode != EXT2_ET_EXTENT_NO_NEXT)
406					break;
407				ctx.errcode = 0;
408				if (!(flags & BLOCK_FLAG_APPEND))
409					break;
410			next_block_set:
411				blk = 0;
412				r = (*ctx.func)(fs, &blk, blockcnt,
413						0, 0, priv_data);
414				ret |= r;
415				check_for_ro_violation_goto(&ctx, ret,
416							    extent_errout);
417				if (r & BLOCK_CHANGED) {
418					ctx.errcode =
419						ext2fs_extent_set_bmap(handle,
420						       (blk64_t) blockcnt++,
421						       (blk64_t) blk, 0);
422					if (ctx.errcode || (ret & BLOCK_ABORT))
423						break;
424					if (blk)
425						goto next_block_set;
426				}
427				break;
428			}
429
430			op = EXT2_EXTENT_NEXT;
431			blk = extent.e_pblk;
432			if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
433				if (ctx.flags & BLOCK_FLAG_DATA_ONLY)
434					continue;
435				if ((!(extent.e_flags &
436				       EXT2_EXTENT_FLAGS_SECOND_VISIT) &&
437				     !(ctx.flags & BLOCK_FLAG_DEPTH_TRAVERSE)) ||
438				    ((extent.e_flags &
439				      EXT2_EXTENT_FLAGS_SECOND_VISIT) &&
440				     (ctx.flags & BLOCK_FLAG_DEPTH_TRAVERSE))) {
441					ret |= (*ctx.func)(fs, &blk,
442							   -1, 0, 0, priv_data);
443					if (ret & BLOCK_CHANGED) {
444						extent.e_pblk = blk;
445						ctx.errcode =
446				ext2fs_extent_replace(handle, 0, &extent);
447						if (ctx.errcode)
448							break;
449					}
450				}
451				continue;
452			}
453			uninit = 0;
454			if (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT)
455				uninit = EXT2_EXTENT_SET_BMAP_UNINIT;
456#if 0
457			printf("lblk %llu pblk %llu len %d blockcnt %llu\n",
458			       extent.e_lblk, extent.e_pblk,
459			       extent.e_len, blockcnt);
460#endif
461			if (extent.e_lblk + extent.e_len <= blockcnt)
462				continue;
463			if (extent.e_lblk > blockcnt)
464				blockcnt = extent.e_lblk;
465			j = blockcnt - extent.e_lblk;
466			blk += j;
467			for (blockcnt = extent.e_lblk, j = 0;
468			     j < extent.e_len;
469			     blk++, blockcnt++, j++) {
470				new_blk = blk;
471				r = (*ctx.func)(fs, &new_blk, blockcnt,
472						0, 0, priv_data);
473				ret |= r;
474				check_for_ro_violation_goto(&ctx, ret,
475							    extent_errout);
476				if (r & BLOCK_CHANGED) {
477					ctx.errcode =
478						ext2fs_extent_set_bmap(handle,
479						       (blk64_t) blockcnt,
480						       new_blk, uninit);
481					if (ctx.errcode)
482						goto extent_errout;
483				}
484				if (ret & BLOCK_ABORT)
485					break;
486			}
487		}
488
489	extent_errout:
490		ext2fs_extent_free(handle);
491		ret |= BLOCK_ERROR | BLOCK_ABORT;
492		goto errout;
493	}
494
495	/*
496	 * Iterate over normal data blocks
497	 */
498	for (i = 0; i < EXT2_NDIR_BLOCKS ; i++, ctx.bcount++) {
499		if (inode.i_block[i] || (flags & BLOCK_FLAG_APPEND)) {
500			blk64 = inode.i_block[i];
501			ret |= (*ctx.func)(fs, &blk64, ctx.bcount, 0, i,
502					   priv_data);
503			inode.i_block[i] = (blk_t) blk64;
504			if (ret & BLOCK_ABORT)
505				goto abort_exit;
506		}
507	}
508	check_for_ro_violation_goto(&ctx, ret, abort_exit);
509	if (inode.i_block[EXT2_IND_BLOCK] || (flags & BLOCK_FLAG_APPEND)) {
510		ret |= block_iterate_ind(&inode.i_block[EXT2_IND_BLOCK],
511					 0, EXT2_IND_BLOCK, &ctx);
512		if (ret & BLOCK_ABORT)
513			goto abort_exit;
514	} else
515		ctx.bcount += limit;
516	if (inode.i_block[EXT2_DIND_BLOCK] || (flags & BLOCK_FLAG_APPEND)) {
517		ret |= block_iterate_dind(&inode.i_block[EXT2_DIND_BLOCK],
518					  0, EXT2_DIND_BLOCK, &ctx);
519		if (ret & BLOCK_ABORT)
520			goto abort_exit;
521	} else
522		ctx.bcount += limit * limit;
523	if (inode.i_block[EXT2_TIND_BLOCK] || (flags & BLOCK_FLAG_APPEND)) {
524		ret |= block_iterate_tind(&inode.i_block[EXT2_TIND_BLOCK],
525					  0, EXT2_TIND_BLOCK, &ctx);
526		if (ret & BLOCK_ABORT)
527			goto abort_exit;
528	}
529
530abort_exit:
531	if (ret & BLOCK_CHANGED) {
532		retval = ext2fs_write_inode(fs, ino, &inode);
533		if (retval) {
534			ret |= BLOCK_ERROR;
535			ctx.errcode = retval;
536		}
537	}
538errout:
539	if (!block_buf)
540		ext2fs_free_mem(&ctx.ind_buf);
541
542	return (ret & BLOCK_ERROR) ? ctx.errcode : 0;
543}
544
545/*
546 * Emulate the old ext2fs_block_iterate function!
547 */
548
549struct xlate64 {
550	int (*func)(ext2_filsys fs,
551		    blk_t	*blocknr,
552		    e2_blkcnt_t	blockcnt,
553		    blk_t	ref_blk,
554		    int		ref_offset,
555		    void	*priv_data);
556	void *real_private;
557};
558
559static int xlate64_func(ext2_filsys fs, blk64_t	*blocknr,
560			e2_blkcnt_t blockcnt, blk64_t ref_blk,
561			int ref_offset, void *priv_data)
562{
563	struct xlate64 *xl = (struct xlate64 *) priv_data;
564	int		ret;
565	blk_t		block32 = *blocknr;
566
567	ret = (*xl->func)(fs, &block32, blockcnt, (blk_t) ref_blk, ref_offset,
568			     xl->real_private);
569	*blocknr = block32;
570	return ret;
571}
572
573errcode_t ext2fs_block_iterate2(ext2_filsys fs,
574				ext2_ino_t ino,
575				int	flags,
576				char *block_buf,
577				int (*func)(ext2_filsys fs,
578					    blk_t	*blocknr,
579					    e2_blkcnt_t	blockcnt,
580					    blk_t	ref_blk,
581					    int		ref_offset,
582					    void	*priv_data),
583				void *priv_data)
584{
585	struct xlate64 xl;
586
587	xl.real_private = priv_data;
588	xl.func = func;
589
590	return ext2fs_block_iterate3(fs, ino, flags, block_buf,
591				     xlate64_func, &xl);
592}
593
594
595struct xlate {
596	int (*func)(ext2_filsys	fs,
597		    blk_t	*blocknr,
598		    int		bcount,
599		    void	*priv_data);
600	void *real_private;
601};
602
603#ifdef __TURBOC__
604 #pragma argsused
605#endif
606static int xlate_func(ext2_filsys fs, blk_t *blocknr, e2_blkcnt_t blockcnt,
607		      blk_t ref_block EXT2FS_ATTR((unused)),
608		      int ref_offset EXT2FS_ATTR((unused)),
609		      void *priv_data)
610{
611	struct xlate *xl = (struct xlate *) priv_data;
612
613	return (*xl->func)(fs, blocknr, (int) blockcnt, xl->real_private);
614}
615
616errcode_t ext2fs_block_iterate(ext2_filsys fs,
617			       ext2_ino_t ino,
618			       int	flags,
619			       char *block_buf,
620			       int (*func)(ext2_filsys fs,
621					   blk_t	*blocknr,
622					   int	blockcnt,
623					   void	*priv_data),
624			       void *priv_data)
625{
626	struct xlate xl;
627
628	xl.real_private = priv_data;
629	xl.func = func;
630
631	return ext2fs_block_iterate2(fs, ino, BLOCK_FLAG_NO_LARGE | flags,
632				     block_buf, xlate_func, &xl);
633}
634
635