blob: aa82ae6b5a8167e56ad8f3ee2540c11ee229f231 [file] [log] [blame]
Theodore Ts'odc8ce342005-01-06 00:04:24 -05001/*
2 * ind_block.c --- indirect block I/O routines
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
Theodore Ts'odc8ce342005-01-06 00:04:24 -05005 * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
6 *
7 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04008 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
Theodore Ts'odc8ce342005-01-06 00:04:24 -050010 * %End-Header%
11 */
12
Theodore Ts'od1154eb2011-09-18 17:34:37 -040013#include "config.h"
Theodore Ts'odc8ce342005-01-06 00:04:24 -050014#include <stdio.h>
15#include <string.h>
16#if HAVE_UNISTD_H
17#include <unistd.h>
18#endif
19
20#include "ext2_fs.h"
21#include "ext2fs.h"
22
23errcode_t ext2fs_read_ind_block(ext2_filsys fs, blk_t blk, void *buf)
24{
25 errcode_t retval;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -040026#ifdef WORDS_BIGENDIAN
Theodore Ts'odc8ce342005-01-06 00:04:24 -050027 blk_t *block_nr;
28 int i;
29 int limit = fs->blocksize >> 2;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -040030#endif
Theodore Ts'odc8ce342005-01-06 00:04:24 -050031
32 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) &&
33 (fs->io != fs->image_io))
34 memset(buf, 0, fs->blocksize);
35 else {
36 retval = io_channel_read_blk(fs->io, blk, 1, buf);
37 if (retval)
Theodore Ts'ob1ae1192005-04-09 01:21:21 -040038 return retval;
Theodore Ts'odc8ce342005-01-06 00:04:24 -050039 }
Theodore Ts'o126a2912007-08-11 01:56:48 -040040#ifdef WORDS_BIGENDIAN
41 block_nr = (blk_t *) buf;
42 for (i = 0; i < limit; i++, block_nr++)
43 *block_nr = ext2fs_swab32(*block_nr);
Theodore Ts'odc8ce342005-01-06 00:04:24 -050044#endif
45 return 0;
46}
47
48errcode_t ext2fs_write_ind_block(ext2_filsys fs, blk_t blk, void *buf)
49{
Theodore Ts'o2d328bb2008-03-17 23:17:13 -040050#ifdef WORDS_BIGENDIAN
Theodore Ts'odc8ce342005-01-06 00:04:24 -050051 blk_t *block_nr;
52 int i;
53 int limit = fs->blocksize >> 2;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -040054#endif
Theodore Ts'odc8ce342005-01-06 00:04:24 -050055
56 if (fs->flags & EXT2_FLAG_IMAGE_FILE)
57 return 0;
58
Theodore Ts'o126a2912007-08-11 01:56:48 -040059#ifdef WORDS_BIGENDIAN
60 block_nr = (blk_t *) buf;
61 for (i = 0; i < limit; i++, block_nr++)
62 *block_nr = ext2fs_swab32(*block_nr);
Theodore Ts'odc8ce342005-01-06 00:04:24 -050063#endif
64 return io_channel_write_blk(fs->io, blk, 1, buf);
65}
66
67