1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker * extent.c --- routines to implement extents support
3*6a54128fSAndroid Build Coastguard Worker *
4*6a54128fSAndroid Build Coastguard Worker * Copyright (C) 2007 Theodore Ts'o.
5*6a54128fSAndroid Build Coastguard Worker *
6*6a54128fSAndroid Build Coastguard Worker * %Begin-Header%
7*6a54128fSAndroid Build Coastguard Worker * This file may be redistributed under the terms of the GNU Library
8*6a54128fSAndroid Build Coastguard Worker * General Public License, version 2.
9*6a54128fSAndroid Build Coastguard Worker * %End-Header%
10*6a54128fSAndroid Build Coastguard Worker */
11*6a54128fSAndroid Build Coastguard Worker
12*6a54128fSAndroid Build Coastguard Worker #include "config.h"
13*6a54128fSAndroid Build Coastguard Worker #include <stdio.h>
14*6a54128fSAndroid Build Coastguard Worker #include <string.h>
15*6a54128fSAndroid Build Coastguard Worker #if HAVE_UNISTD_H
16*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
17*6a54128fSAndroid Build Coastguard Worker #endif
18*6a54128fSAndroid Build Coastguard Worker #if HAVE_ERRNO_H
19*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
20*6a54128fSAndroid Build Coastguard Worker #endif
21*6a54128fSAndroid Build Coastguard Worker #if HAVE_SYS_STAT_H
22*6a54128fSAndroid Build Coastguard Worker #include <sys/stat.h>
23*6a54128fSAndroid Build Coastguard Worker #endif
24*6a54128fSAndroid Build Coastguard Worker #if HAVE_SYS_TYPES_H
25*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
26*6a54128fSAndroid Build Coastguard Worker #endif
27*6a54128fSAndroid Build Coastguard Worker
28*6a54128fSAndroid Build Coastguard Worker #include "ext2_fs.h"
29*6a54128fSAndroid Build Coastguard Worker #include "ext2fsP.h"
30*6a54128fSAndroid Build Coastguard Worker #include "e2image.h"
31*6a54128fSAndroid Build Coastguard Worker
32*6a54128fSAndroid Build Coastguard Worker #undef DEBUG
33*6a54128fSAndroid Build Coastguard Worker
34*6a54128fSAndroid Build Coastguard Worker /*
35*6a54128fSAndroid Build Coastguard Worker * Definitions to be dropped in lib/ext2fs/ext2fs.h
36*6a54128fSAndroid Build Coastguard Worker */
37*6a54128fSAndroid Build Coastguard Worker
38*6a54128fSAndroid Build Coastguard Worker /*
39*6a54128fSAndroid Build Coastguard Worker * Private definitions
40*6a54128fSAndroid Build Coastguard Worker */
41*6a54128fSAndroid Build Coastguard Worker
42*6a54128fSAndroid Build Coastguard Worker struct extent_path {
43*6a54128fSAndroid Build Coastguard Worker char *buf;
44*6a54128fSAndroid Build Coastguard Worker int entries;
45*6a54128fSAndroid Build Coastguard Worker int max_entries;
46*6a54128fSAndroid Build Coastguard Worker int left;
47*6a54128fSAndroid Build Coastguard Worker int visit_num;
48*6a54128fSAndroid Build Coastguard Worker int flags;
49*6a54128fSAndroid Build Coastguard Worker blk64_t end_blk;
50*6a54128fSAndroid Build Coastguard Worker blk64_t blk;
51*6a54128fSAndroid Build Coastguard Worker void *curr;
52*6a54128fSAndroid Build Coastguard Worker };
53*6a54128fSAndroid Build Coastguard Worker
54*6a54128fSAndroid Build Coastguard Worker
55*6a54128fSAndroid Build Coastguard Worker struct ext2_extent_handle {
56*6a54128fSAndroid Build Coastguard Worker errcode_t magic;
57*6a54128fSAndroid Build Coastguard Worker ext2_filsys fs;
58*6a54128fSAndroid Build Coastguard Worker ext2_ino_t ino;
59*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode;
60*6a54128fSAndroid Build Coastguard Worker struct ext2_inode inodebuf;
61*6a54128fSAndroid Build Coastguard Worker int type;
62*6a54128fSAndroid Build Coastguard Worker int level;
63*6a54128fSAndroid Build Coastguard Worker int max_depth;
64*6a54128fSAndroid Build Coastguard Worker int max_paths;
65*6a54128fSAndroid Build Coastguard Worker struct extent_path *path;
66*6a54128fSAndroid Build Coastguard Worker };
67*6a54128fSAndroid Build Coastguard Worker
68*6a54128fSAndroid Build Coastguard Worker struct ext2_extent_path {
69*6a54128fSAndroid Build Coastguard Worker errcode_t magic;
70*6a54128fSAndroid Build Coastguard Worker int leaf_height;
71*6a54128fSAndroid Build Coastguard Worker blk64_t lblk;
72*6a54128fSAndroid Build Coastguard Worker };
73*6a54128fSAndroid Build Coastguard Worker
74*6a54128fSAndroid Build Coastguard Worker /*
75*6a54128fSAndroid Build Coastguard Worker * Useful Debugging stuff
76*6a54128fSAndroid Build Coastguard Worker */
77*6a54128fSAndroid Build Coastguard Worker
78*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
dbg_show_header(struct ext3_extent_header * eh)79*6a54128fSAndroid Build Coastguard Worker static void dbg_show_header(struct ext3_extent_header *eh)
80*6a54128fSAndroid Build Coastguard Worker {
81*6a54128fSAndroid Build Coastguard Worker printf("header: magic=%x entries=%u max=%u depth=%u generation=%u\n",
82*6a54128fSAndroid Build Coastguard Worker ext2fs_le16_to_cpu(eh->eh_magic),
83*6a54128fSAndroid Build Coastguard Worker ext2fs_le16_to_cpu(eh->eh_entries),
84*6a54128fSAndroid Build Coastguard Worker ext2fs_le16_to_cpu(eh->eh_max),
85*6a54128fSAndroid Build Coastguard Worker ext2fs_le16_to_cpu(eh->eh_depth),
86*6a54128fSAndroid Build Coastguard Worker ext2fs_le32_to_cpu(eh->eh_generation));
87*6a54128fSAndroid Build Coastguard Worker }
88*6a54128fSAndroid Build Coastguard Worker
dbg_show_index(struct ext3_extent_idx * ix)89*6a54128fSAndroid Build Coastguard Worker static void dbg_show_index(struct ext3_extent_idx *ix)
90*6a54128fSAndroid Build Coastguard Worker {
91*6a54128fSAndroid Build Coastguard Worker printf("index: block=%u leaf=%u leaf_hi=%u unused=%u\n",
92*6a54128fSAndroid Build Coastguard Worker ext2fs_le32_to_cpu(ix->ei_block),
93*6a54128fSAndroid Build Coastguard Worker ext2fs_le32_to_cpu(ix->ei_leaf),
94*6a54128fSAndroid Build Coastguard Worker ext2fs_le16_to_cpu(ix->ei_leaf_hi),
95*6a54128fSAndroid Build Coastguard Worker ext2fs_le16_to_cpu(ix->ei_unused));
96*6a54128fSAndroid Build Coastguard Worker }
97*6a54128fSAndroid Build Coastguard Worker
dbg_show_extent(struct ext3_extent * ex)98*6a54128fSAndroid Build Coastguard Worker static void dbg_show_extent(struct ext3_extent *ex)
99*6a54128fSAndroid Build Coastguard Worker {
100*6a54128fSAndroid Build Coastguard Worker printf("extent: block=%u-%u len=%u start=%u start_hi=%u\n",
101*6a54128fSAndroid Build Coastguard Worker ext2fs_le32_to_cpu(ex->ee_block),
102*6a54128fSAndroid Build Coastguard Worker ext2fs_le32_to_cpu(ex->ee_block) +
103*6a54128fSAndroid Build Coastguard Worker ext2fs_le16_to_cpu(ex->ee_len) - 1,
104*6a54128fSAndroid Build Coastguard Worker ext2fs_le16_to_cpu(ex->ee_len),
105*6a54128fSAndroid Build Coastguard Worker ext2fs_le32_to_cpu(ex->ee_start),
106*6a54128fSAndroid Build Coastguard Worker ext2fs_le16_to_cpu(ex->ee_start_hi));
107*6a54128fSAndroid Build Coastguard Worker }
108*6a54128fSAndroid Build Coastguard Worker
dbg_print_extent(char * desc,struct ext2fs_extent * extent)109*6a54128fSAndroid Build Coastguard Worker static void dbg_print_extent(char *desc, struct ext2fs_extent *extent)
110*6a54128fSAndroid Build Coastguard Worker {
111*6a54128fSAndroid Build Coastguard Worker if (desc)
112*6a54128fSAndroid Build Coastguard Worker printf("%s: ", desc);
113*6a54128fSAndroid Build Coastguard Worker printf("extent: lblk %llu--%llu, len %u, pblk %llu, flags: ",
114*6a54128fSAndroid Build Coastguard Worker extent->e_lblk, extent->e_lblk + extent->e_len - 1,
115*6a54128fSAndroid Build Coastguard Worker extent->e_len, extent->e_pblk);
116*6a54128fSAndroid Build Coastguard Worker if (extent->e_flags & EXT2_EXTENT_FLAGS_LEAF)
117*6a54128fSAndroid Build Coastguard Worker fputs("LEAF ", stdout);
118*6a54128fSAndroid Build Coastguard Worker if (extent->e_flags & EXT2_EXTENT_FLAGS_UNINIT)
119*6a54128fSAndroid Build Coastguard Worker fputs("UNINIT ", stdout);
120*6a54128fSAndroid Build Coastguard Worker if (extent->e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
121*6a54128fSAndroid Build Coastguard Worker fputs("2ND_VISIT ", stdout);
122*6a54128fSAndroid Build Coastguard Worker if (!extent->e_flags)
123*6a54128fSAndroid Build Coastguard Worker fputs("(none)", stdout);
124*6a54128fSAndroid Build Coastguard Worker fputc('\n', stdout);
125*6a54128fSAndroid Build Coastguard Worker
126*6a54128fSAndroid Build Coastguard Worker }
127*6a54128fSAndroid Build Coastguard Worker
dump_path(const char * tag,struct ext2_extent_handle * handle,struct extent_path * path)128*6a54128fSAndroid Build Coastguard Worker static void dump_path(const char *tag, struct ext2_extent_handle *handle,
129*6a54128fSAndroid Build Coastguard Worker struct extent_path *path)
130*6a54128fSAndroid Build Coastguard Worker {
131*6a54128fSAndroid Build Coastguard Worker struct extent_path *ppp = path;
132*6a54128fSAndroid Build Coastguard Worker printf("%s: level=%d\n", tag, handle->level);
133*6a54128fSAndroid Build Coastguard Worker
134*6a54128fSAndroid Build Coastguard Worker do {
135*6a54128fSAndroid Build Coastguard Worker printf("%s: path=%ld buf=%p entries=%d max_entries=%d left=%d "
136*6a54128fSAndroid Build Coastguard Worker "visit_num=%d flags=0x%x end_blk=%llu curr=%p(%ld)\n",
137*6a54128fSAndroid Build Coastguard Worker tag, (ppp - handle->path), ppp->buf, ppp->entries,
138*6a54128fSAndroid Build Coastguard Worker ppp->max_entries, ppp->left, ppp->visit_num, ppp->flags,
139*6a54128fSAndroid Build Coastguard Worker ppp->end_blk, ppp->curr, ppp->curr - (void *)ppp->buf);
140*6a54128fSAndroid Build Coastguard Worker printf(" ");
141*6a54128fSAndroid Build Coastguard Worker dbg_show_header((struct ext3_extent_header *)ppp->buf);
142*6a54128fSAndroid Build Coastguard Worker if (ppp->curr) {
143*6a54128fSAndroid Build Coastguard Worker printf(" ");
144*6a54128fSAndroid Build Coastguard Worker dbg_show_index(ppp->curr);
145*6a54128fSAndroid Build Coastguard Worker printf(" ");
146*6a54128fSAndroid Build Coastguard Worker dbg_show_extent(ppp->curr);
147*6a54128fSAndroid Build Coastguard Worker }
148*6a54128fSAndroid Build Coastguard Worker ppp--;
149*6a54128fSAndroid Build Coastguard Worker } while (ppp >= handle->path);
150*6a54128fSAndroid Build Coastguard Worker fflush(stdout);
151*6a54128fSAndroid Build Coastguard Worker
152*6a54128fSAndroid Build Coastguard Worker return;
153*6a54128fSAndroid Build Coastguard Worker }
154*6a54128fSAndroid Build Coastguard Worker
155*6a54128fSAndroid Build Coastguard Worker #else
156*6a54128fSAndroid Build Coastguard Worker #define dbg_show_header(eh) do { } while (0)
157*6a54128fSAndroid Build Coastguard Worker #define dbg_show_index(ix) do { } while (0)
158*6a54128fSAndroid Build Coastguard Worker #define dbg_show_extent(ex) do { } while (0)
159*6a54128fSAndroid Build Coastguard Worker #define dbg_print_extent(desc, ex) do { } while (0)
160*6a54128fSAndroid Build Coastguard Worker #define dump_path(tag, handle, path) do { } while (0)
161*6a54128fSAndroid Build Coastguard Worker #endif
162*6a54128fSAndroid Build Coastguard Worker
163*6a54128fSAndroid Build Coastguard Worker /*
164*6a54128fSAndroid Build Coastguard Worker * Verify the extent header as being sane
165*6a54128fSAndroid Build Coastguard Worker */
ext2fs_extent_header_verify(void * ptr,int size)166*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_extent_header_verify(void *ptr, int size)
167*6a54128fSAndroid Build Coastguard Worker {
168*6a54128fSAndroid Build Coastguard Worker int eh_max, entry_size;
169*6a54128fSAndroid Build Coastguard Worker struct ext3_extent_header *eh = ptr;
170*6a54128fSAndroid Build Coastguard Worker
171*6a54128fSAndroid Build Coastguard Worker dbg_show_header(eh);
172*6a54128fSAndroid Build Coastguard Worker if (ext2fs_le16_to_cpu(eh->eh_magic) != EXT3_EXT_MAGIC)
173*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_HEADER_BAD;
174*6a54128fSAndroid Build Coastguard Worker if (ext2fs_le16_to_cpu(eh->eh_entries) > ext2fs_le16_to_cpu(eh->eh_max))
175*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_HEADER_BAD;
176*6a54128fSAndroid Build Coastguard Worker if (eh->eh_depth == 0)
177*6a54128fSAndroid Build Coastguard Worker entry_size = sizeof(struct ext3_extent);
178*6a54128fSAndroid Build Coastguard Worker else
179*6a54128fSAndroid Build Coastguard Worker entry_size = sizeof(struct ext3_extent_idx);
180*6a54128fSAndroid Build Coastguard Worker
181*6a54128fSAndroid Build Coastguard Worker eh_max = (size - sizeof(*eh)) / entry_size;
182*6a54128fSAndroid Build Coastguard Worker /* Allow two extent-sized items at the end of the block, for
183*6a54128fSAndroid Build Coastguard Worker * ext4_extent_tail with checksum in the future. */
184*6a54128fSAndroid Build Coastguard Worker if ((ext2fs_le16_to_cpu(eh->eh_max) > eh_max) ||
185*6a54128fSAndroid Build Coastguard Worker (ext2fs_le16_to_cpu(eh->eh_max) < (eh_max - 2)))
186*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_HEADER_BAD;
187*6a54128fSAndroid Build Coastguard Worker
188*6a54128fSAndroid Build Coastguard Worker return 0;
189*6a54128fSAndroid Build Coastguard Worker }
190*6a54128fSAndroid Build Coastguard Worker
191*6a54128fSAndroid Build Coastguard Worker
192*6a54128fSAndroid Build Coastguard Worker /*
193*6a54128fSAndroid Build Coastguard Worker * Begin functions to handle an inode's extent information
194*6a54128fSAndroid Build Coastguard Worker */
ext2fs_extent_free(ext2_extent_handle_t handle)195*6a54128fSAndroid Build Coastguard Worker void ext2fs_extent_free(ext2_extent_handle_t handle)
196*6a54128fSAndroid Build Coastguard Worker {
197*6a54128fSAndroid Build Coastguard Worker int i;
198*6a54128fSAndroid Build Coastguard Worker
199*6a54128fSAndroid Build Coastguard Worker if (!handle)
200*6a54128fSAndroid Build Coastguard Worker return;
201*6a54128fSAndroid Build Coastguard Worker
202*6a54128fSAndroid Build Coastguard Worker if (handle->path) {
203*6a54128fSAndroid Build Coastguard Worker for (i = 1; i < handle->max_paths; i++) {
204*6a54128fSAndroid Build Coastguard Worker if (handle->path[i].buf)
205*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&handle->path[i].buf);
206*6a54128fSAndroid Build Coastguard Worker }
207*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&handle->path);
208*6a54128fSAndroid Build Coastguard Worker }
209*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&handle);
210*6a54128fSAndroid Build Coastguard Worker }
211*6a54128fSAndroid Build Coastguard Worker
ext2fs_extent_open(ext2_filsys fs,ext2_ino_t ino,ext2_extent_handle_t * ret_handle)212*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_extent_open(ext2_filsys fs, ext2_ino_t ino,
213*6a54128fSAndroid Build Coastguard Worker ext2_extent_handle_t *ret_handle)
214*6a54128fSAndroid Build Coastguard Worker {
215*6a54128fSAndroid Build Coastguard Worker return ext2fs_extent_open2(fs, ino, NULL, ret_handle);
216*6a54128fSAndroid Build Coastguard Worker }
217*6a54128fSAndroid Build Coastguard Worker
ext2fs_extent_open2(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,ext2_extent_handle_t * ret_handle)218*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_extent_open2(ext2_filsys fs, ext2_ino_t ino,
219*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode,
220*6a54128fSAndroid Build Coastguard Worker ext2_extent_handle_t *ret_handle)
221*6a54128fSAndroid Build Coastguard Worker {
222*6a54128fSAndroid Build Coastguard Worker struct ext2_extent_handle *handle;
223*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
224*6a54128fSAndroid Build Coastguard Worker int i;
225*6a54128fSAndroid Build Coastguard Worker struct ext3_extent_header *eh;
226*6a54128fSAndroid Build Coastguard Worker
227*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
228*6a54128fSAndroid Build Coastguard Worker
229*6a54128fSAndroid Build Coastguard Worker if (!inode)
230*6a54128fSAndroid Build Coastguard Worker if ((ino == 0) || (ino > fs->super->s_inodes_count))
231*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_BAD_INODE_NUM;
232*6a54128fSAndroid Build Coastguard Worker
233*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(sizeof(struct ext2_extent_handle), &handle);
234*6a54128fSAndroid Build Coastguard Worker if (retval)
235*6a54128fSAndroid Build Coastguard Worker return retval;
236*6a54128fSAndroid Build Coastguard Worker memset(handle, 0, sizeof(struct ext2_extent_handle));
237*6a54128fSAndroid Build Coastguard Worker
238*6a54128fSAndroid Build Coastguard Worker handle->ino = ino;
239*6a54128fSAndroid Build Coastguard Worker handle->fs = fs;
240*6a54128fSAndroid Build Coastguard Worker
241*6a54128fSAndroid Build Coastguard Worker if (inode) {
242*6a54128fSAndroid Build Coastguard Worker handle->inode = inode;
243*6a54128fSAndroid Build Coastguard Worker } else {
244*6a54128fSAndroid Build Coastguard Worker handle->inode = &handle->inodebuf;
245*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_read_inode(fs, ino, handle->inode);
246*6a54128fSAndroid Build Coastguard Worker if (retval)
247*6a54128fSAndroid Build Coastguard Worker goto errout;
248*6a54128fSAndroid Build Coastguard Worker }
249*6a54128fSAndroid Build Coastguard Worker
250*6a54128fSAndroid Build Coastguard Worker eh = (struct ext3_extent_header *) &handle->inode->i_block[0];
251*6a54128fSAndroid Build Coastguard Worker
252*6a54128fSAndroid Build Coastguard Worker for (i=0; i < EXT2_N_BLOCKS; i++)
253*6a54128fSAndroid Build Coastguard Worker if (handle->inode->i_block[i])
254*6a54128fSAndroid Build Coastguard Worker break;
255*6a54128fSAndroid Build Coastguard Worker if (i >= EXT2_N_BLOCKS) {
256*6a54128fSAndroid Build Coastguard Worker eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
257*6a54128fSAndroid Build Coastguard Worker eh->eh_depth = 0;
258*6a54128fSAndroid Build Coastguard Worker eh->eh_entries = 0;
259*6a54128fSAndroid Build Coastguard Worker i = (sizeof(handle->inode->i_block) - sizeof(*eh)) /
260*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext3_extent);
261*6a54128fSAndroid Build Coastguard Worker eh->eh_max = ext2fs_cpu_to_le16(i);
262*6a54128fSAndroid Build Coastguard Worker handle->inode->i_flags |= EXT4_EXTENTS_FL;
263*6a54128fSAndroid Build Coastguard Worker }
264*6a54128fSAndroid Build Coastguard Worker
265*6a54128fSAndroid Build Coastguard Worker if (!(handle->inode->i_flags & EXT4_EXTENTS_FL)) {
266*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_INODE_NOT_EXTENT;
267*6a54128fSAndroid Build Coastguard Worker goto errout;
268*6a54128fSAndroid Build Coastguard Worker }
269*6a54128fSAndroid Build Coastguard Worker
270*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_header_verify(eh, sizeof(handle->inode->i_block));
271*6a54128fSAndroid Build Coastguard Worker if (retval)
272*6a54128fSAndroid Build Coastguard Worker goto errout;
273*6a54128fSAndroid Build Coastguard Worker
274*6a54128fSAndroid Build Coastguard Worker handle->max_depth = ext2fs_le16_to_cpu(eh->eh_depth);
275*6a54128fSAndroid Build Coastguard Worker handle->type = ext2fs_le16_to_cpu(eh->eh_magic);
276*6a54128fSAndroid Build Coastguard Worker
277*6a54128fSAndroid Build Coastguard Worker handle->max_paths = handle->max_depth + 1;
278*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_memzero(handle->max_paths *
279*6a54128fSAndroid Build Coastguard Worker sizeof(struct extent_path),
280*6a54128fSAndroid Build Coastguard Worker &handle->path);
281*6a54128fSAndroid Build Coastguard Worker handle->path[0].buf = (char *) handle->inode->i_block;
282*6a54128fSAndroid Build Coastguard Worker
283*6a54128fSAndroid Build Coastguard Worker handle->path[0].left = handle->path[0].entries =
284*6a54128fSAndroid Build Coastguard Worker ext2fs_le16_to_cpu(eh->eh_entries);
285*6a54128fSAndroid Build Coastguard Worker handle->path[0].max_entries = ext2fs_le16_to_cpu(eh->eh_max);
286*6a54128fSAndroid Build Coastguard Worker handle->path[0].curr = 0;
287*6a54128fSAndroid Build Coastguard Worker handle->path[0].end_blk =
288*6a54128fSAndroid Build Coastguard Worker (EXT2_I_SIZE(handle->inode) + fs->blocksize - 1) >>
289*6a54128fSAndroid Build Coastguard Worker EXT2_BLOCK_SIZE_BITS(fs->super);
290*6a54128fSAndroid Build Coastguard Worker handle->path[0].blk = 0;
291*6a54128fSAndroid Build Coastguard Worker handle->path[0].visit_num = 1;
292*6a54128fSAndroid Build Coastguard Worker handle->level = 0;
293*6a54128fSAndroid Build Coastguard Worker handle->magic = EXT2_ET_MAGIC_EXTENT_HANDLE;
294*6a54128fSAndroid Build Coastguard Worker
295*6a54128fSAndroid Build Coastguard Worker *ret_handle = handle;
296*6a54128fSAndroid Build Coastguard Worker return 0;
297*6a54128fSAndroid Build Coastguard Worker
298*6a54128fSAndroid Build Coastguard Worker errout:
299*6a54128fSAndroid Build Coastguard Worker ext2fs_extent_free(handle);
300*6a54128fSAndroid Build Coastguard Worker return retval;
301*6a54128fSAndroid Build Coastguard Worker }
302*6a54128fSAndroid Build Coastguard Worker
303*6a54128fSAndroid Build Coastguard Worker /*
304*6a54128fSAndroid Build Coastguard Worker * This function is responsible for (optionally) moving through the
305*6a54128fSAndroid Build Coastguard Worker * extent tree and then returning the current extent
306*6a54128fSAndroid Build Coastguard Worker */
ext2fs_extent_get(ext2_extent_handle_t handle,int flags,struct ext2fs_extent * extent)307*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_extent_get(ext2_extent_handle_t handle,
308*6a54128fSAndroid Build Coastguard Worker int flags, struct ext2fs_extent *extent)
309*6a54128fSAndroid Build Coastguard Worker {
310*6a54128fSAndroid Build Coastguard Worker struct extent_path *path, *newpath, *tp;
311*6a54128fSAndroid Build Coastguard Worker struct ext3_extent_header *eh;
312*6a54128fSAndroid Build Coastguard Worker struct ext3_extent_idx *ix = 0;
313*6a54128fSAndroid Build Coastguard Worker struct ext3_extent *ex;
314*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
315*6a54128fSAndroid Build Coastguard Worker blk64_t blk;
316*6a54128fSAndroid Build Coastguard Worker blk64_t end_blk;
317*6a54128fSAndroid Build Coastguard Worker int orig_op, op, l;
318*6a54128fSAndroid Build Coastguard Worker int failed_csum = 0;
319*6a54128fSAndroid Build Coastguard Worker
320*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EXTENT_HANDLE);
321*6a54128fSAndroid Build Coastguard Worker
322*6a54128fSAndroid Build Coastguard Worker if (!handle->path)
323*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_CURRENT_NODE;
324*6a54128fSAndroid Build Coastguard Worker
325*6a54128fSAndroid Build Coastguard Worker orig_op = op = flags & EXT2_EXTENT_MOVE_MASK;
326*6a54128fSAndroid Build Coastguard Worker
327*6a54128fSAndroid Build Coastguard Worker retry:
328*6a54128fSAndroid Build Coastguard Worker path = handle->path + handle->level;
329*6a54128fSAndroid Build Coastguard Worker if ((orig_op == EXT2_EXTENT_NEXT) ||
330*6a54128fSAndroid Build Coastguard Worker (orig_op == EXT2_EXTENT_NEXT_LEAF)) {
331*6a54128fSAndroid Build Coastguard Worker if (handle->level < handle->max_depth) {
332*6a54128fSAndroid Build Coastguard Worker /* interior node */
333*6a54128fSAndroid Build Coastguard Worker if (path->visit_num == 0) {
334*6a54128fSAndroid Build Coastguard Worker path->visit_num++;
335*6a54128fSAndroid Build Coastguard Worker op = EXT2_EXTENT_DOWN;
336*6a54128fSAndroid Build Coastguard Worker } else if (path->left > 0)
337*6a54128fSAndroid Build Coastguard Worker op = EXT2_EXTENT_NEXT_SIB;
338*6a54128fSAndroid Build Coastguard Worker else if (handle->level > 0)
339*6a54128fSAndroid Build Coastguard Worker op = EXT2_EXTENT_UP;
340*6a54128fSAndroid Build Coastguard Worker else
341*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_NO_NEXT;
342*6a54128fSAndroid Build Coastguard Worker } else {
343*6a54128fSAndroid Build Coastguard Worker /* leaf node */
344*6a54128fSAndroid Build Coastguard Worker if (path->left > 0)
345*6a54128fSAndroid Build Coastguard Worker op = EXT2_EXTENT_NEXT_SIB;
346*6a54128fSAndroid Build Coastguard Worker else if (handle->level > 0)
347*6a54128fSAndroid Build Coastguard Worker op = EXT2_EXTENT_UP;
348*6a54128fSAndroid Build Coastguard Worker else
349*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_NO_NEXT;
350*6a54128fSAndroid Build Coastguard Worker }
351*6a54128fSAndroid Build Coastguard Worker if (op != EXT2_EXTENT_NEXT_SIB) {
352*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG_GET_EXTENT
353*6a54128fSAndroid Build Coastguard Worker printf("<<<< OP = %s\n",
354*6a54128fSAndroid Build Coastguard Worker (op == EXT2_EXTENT_DOWN) ? "down" :
355*6a54128fSAndroid Build Coastguard Worker ((op == EXT2_EXTENT_UP) ? "up" : "unknown"));
356*6a54128fSAndroid Build Coastguard Worker #endif
357*6a54128fSAndroid Build Coastguard Worker }
358*6a54128fSAndroid Build Coastguard Worker }
359*6a54128fSAndroid Build Coastguard Worker
360*6a54128fSAndroid Build Coastguard Worker if ((orig_op == EXT2_EXTENT_PREV) ||
361*6a54128fSAndroid Build Coastguard Worker (orig_op == EXT2_EXTENT_PREV_LEAF)) {
362*6a54128fSAndroid Build Coastguard Worker if (handle->level < handle->max_depth) {
363*6a54128fSAndroid Build Coastguard Worker /* interior node */
364*6a54128fSAndroid Build Coastguard Worker if (path->visit_num > 0 ) {
365*6a54128fSAndroid Build Coastguard Worker /* path->visit_num = 0; */
366*6a54128fSAndroid Build Coastguard Worker op = EXT2_EXTENT_DOWN_AND_LAST;
367*6a54128fSAndroid Build Coastguard Worker } else if (path->left < path->entries-1)
368*6a54128fSAndroid Build Coastguard Worker op = EXT2_EXTENT_PREV_SIB;
369*6a54128fSAndroid Build Coastguard Worker else if (handle->level > 0)
370*6a54128fSAndroid Build Coastguard Worker op = EXT2_EXTENT_UP;
371*6a54128fSAndroid Build Coastguard Worker else
372*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_NO_PREV;
373*6a54128fSAndroid Build Coastguard Worker } else {
374*6a54128fSAndroid Build Coastguard Worker /* leaf node */
375*6a54128fSAndroid Build Coastguard Worker if (path->left < path->entries-1)
376*6a54128fSAndroid Build Coastguard Worker op = EXT2_EXTENT_PREV_SIB;
377*6a54128fSAndroid Build Coastguard Worker else if (handle->level > 0)
378*6a54128fSAndroid Build Coastguard Worker op = EXT2_EXTENT_UP;
379*6a54128fSAndroid Build Coastguard Worker else
380*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_NO_PREV;
381*6a54128fSAndroid Build Coastguard Worker }
382*6a54128fSAndroid Build Coastguard Worker if (op != EXT2_EXTENT_PREV_SIB) {
383*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG_GET_EXTENT
384*6a54128fSAndroid Build Coastguard Worker printf("<<<< OP = %s\n",
385*6a54128fSAndroid Build Coastguard Worker (op == EXT2_EXTENT_DOWN_AND_LAST) ? "down/last" :
386*6a54128fSAndroid Build Coastguard Worker ((op == EXT2_EXTENT_UP) ? "up" : "unknown"));
387*6a54128fSAndroid Build Coastguard Worker #endif
388*6a54128fSAndroid Build Coastguard Worker }
389*6a54128fSAndroid Build Coastguard Worker }
390*6a54128fSAndroid Build Coastguard Worker
391*6a54128fSAndroid Build Coastguard Worker if (orig_op == EXT2_EXTENT_LAST_LEAF) {
392*6a54128fSAndroid Build Coastguard Worker if ((handle->level < handle->max_depth) &&
393*6a54128fSAndroid Build Coastguard Worker (path->left == 0))
394*6a54128fSAndroid Build Coastguard Worker op = EXT2_EXTENT_DOWN;
395*6a54128fSAndroid Build Coastguard Worker else
396*6a54128fSAndroid Build Coastguard Worker op = EXT2_EXTENT_LAST_SIB;
397*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG_GET_EXTENT
398*6a54128fSAndroid Build Coastguard Worker printf("<<<< OP = %s\n",
399*6a54128fSAndroid Build Coastguard Worker (op == EXT2_EXTENT_DOWN) ? "down" : "last_sib");
400*6a54128fSAndroid Build Coastguard Worker #endif
401*6a54128fSAndroid Build Coastguard Worker }
402*6a54128fSAndroid Build Coastguard Worker
403*6a54128fSAndroid Build Coastguard Worker switch (op) {
404*6a54128fSAndroid Build Coastguard Worker case EXT2_EXTENT_CURRENT:
405*6a54128fSAndroid Build Coastguard Worker ix = path->curr;
406*6a54128fSAndroid Build Coastguard Worker break;
407*6a54128fSAndroid Build Coastguard Worker case EXT2_EXTENT_ROOT:
408*6a54128fSAndroid Build Coastguard Worker handle->level = 0;
409*6a54128fSAndroid Build Coastguard Worker path = handle->path + handle->level;
410*6a54128fSAndroid Build Coastguard Worker /* fallthrough */
411*6a54128fSAndroid Build Coastguard Worker case EXT2_EXTENT_FIRST_SIB:
412*6a54128fSAndroid Build Coastguard Worker path->left = path->entries;
413*6a54128fSAndroid Build Coastguard Worker path->curr = 0;
414*6a54128fSAndroid Build Coastguard Worker /* fallthrough */
415*6a54128fSAndroid Build Coastguard Worker case EXT2_EXTENT_NEXT_SIB:
416*6a54128fSAndroid Build Coastguard Worker if (path->left <= 0)
417*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_NO_NEXT;
418*6a54128fSAndroid Build Coastguard Worker if (path->curr) {
419*6a54128fSAndroid Build Coastguard Worker ix = path->curr;
420*6a54128fSAndroid Build Coastguard Worker ix++;
421*6a54128fSAndroid Build Coastguard Worker } else {
422*6a54128fSAndroid Build Coastguard Worker eh = (struct ext3_extent_header *) path->buf;
423*6a54128fSAndroid Build Coastguard Worker ix = EXT_FIRST_INDEX(eh);
424*6a54128fSAndroid Build Coastguard Worker }
425*6a54128fSAndroid Build Coastguard Worker path->left--;
426*6a54128fSAndroid Build Coastguard Worker path->curr = ix;
427*6a54128fSAndroid Build Coastguard Worker path->visit_num = 0;
428*6a54128fSAndroid Build Coastguard Worker break;
429*6a54128fSAndroid Build Coastguard Worker case EXT2_EXTENT_PREV_SIB:
430*6a54128fSAndroid Build Coastguard Worker if (!path->curr ||
431*6a54128fSAndroid Build Coastguard Worker path->left+1 >= path->entries)
432*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_NO_PREV;
433*6a54128fSAndroid Build Coastguard Worker ix = path->curr;
434*6a54128fSAndroid Build Coastguard Worker ix--;
435*6a54128fSAndroid Build Coastguard Worker path->curr = ix;
436*6a54128fSAndroid Build Coastguard Worker path->left++;
437*6a54128fSAndroid Build Coastguard Worker if (handle->level < handle->max_depth)
438*6a54128fSAndroid Build Coastguard Worker path->visit_num = 1;
439*6a54128fSAndroid Build Coastguard Worker break;
440*6a54128fSAndroid Build Coastguard Worker case EXT2_EXTENT_LAST_SIB:
441*6a54128fSAndroid Build Coastguard Worker eh = (struct ext3_extent_header *) path->buf;
442*6a54128fSAndroid Build Coastguard Worker path->curr = EXT_LAST_EXTENT(eh);
443*6a54128fSAndroid Build Coastguard Worker ix = path->curr;
444*6a54128fSAndroid Build Coastguard Worker path->left = 0;
445*6a54128fSAndroid Build Coastguard Worker path->visit_num = 0;
446*6a54128fSAndroid Build Coastguard Worker break;
447*6a54128fSAndroid Build Coastguard Worker case EXT2_EXTENT_UP:
448*6a54128fSAndroid Build Coastguard Worker if (handle->level <= 0)
449*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_NO_UP;
450*6a54128fSAndroid Build Coastguard Worker handle->level--;
451*6a54128fSAndroid Build Coastguard Worker path--;
452*6a54128fSAndroid Build Coastguard Worker ix = path->curr;
453*6a54128fSAndroid Build Coastguard Worker if ((orig_op == EXT2_EXTENT_PREV) ||
454*6a54128fSAndroid Build Coastguard Worker (orig_op == EXT2_EXTENT_PREV_LEAF))
455*6a54128fSAndroid Build Coastguard Worker path->visit_num = 0;
456*6a54128fSAndroid Build Coastguard Worker break;
457*6a54128fSAndroid Build Coastguard Worker case EXT2_EXTENT_DOWN:
458*6a54128fSAndroid Build Coastguard Worker case EXT2_EXTENT_DOWN_AND_LAST:
459*6a54128fSAndroid Build Coastguard Worker if (!path->curr ||(handle->level >= handle->max_depth))
460*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_NO_DOWN;
461*6a54128fSAndroid Build Coastguard Worker
462*6a54128fSAndroid Build Coastguard Worker ix = path->curr;
463*6a54128fSAndroid Build Coastguard Worker newpath = path + 1;
464*6a54128fSAndroid Build Coastguard Worker if (!newpath->buf) {
465*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(handle->fs->blocksize,
466*6a54128fSAndroid Build Coastguard Worker &newpath->buf);
467*6a54128fSAndroid Build Coastguard Worker if (retval)
468*6a54128fSAndroid Build Coastguard Worker return retval;
469*6a54128fSAndroid Build Coastguard Worker }
470*6a54128fSAndroid Build Coastguard Worker blk = ext2fs_le32_to_cpu(ix->ei_leaf) +
471*6a54128fSAndroid Build Coastguard Worker ((__u64) ext2fs_le16_to_cpu(ix->ei_leaf_hi) << 32);
472*6a54128fSAndroid Build Coastguard Worker for (l = handle->level, tp = path; l > 0; l--, tp--) {
473*6a54128fSAndroid Build Coastguard Worker if (blk == tp->blk)
474*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_CYCLE;
475*6a54128fSAndroid Build Coastguard Worker }
476*6a54128fSAndroid Build Coastguard Worker newpath->blk = blk;
477*6a54128fSAndroid Build Coastguard Worker if ((handle->fs->flags & EXT2_FLAG_IMAGE_FILE) &&
478*6a54128fSAndroid Build Coastguard Worker (handle->fs->io != handle->fs->image_io))
479*6a54128fSAndroid Build Coastguard Worker memset(newpath->buf, 0, handle->fs->blocksize);
480*6a54128fSAndroid Build Coastguard Worker else {
481*6a54128fSAndroid Build Coastguard Worker retval = io_channel_read_blk64(handle->fs->io,
482*6a54128fSAndroid Build Coastguard Worker blk, 1, newpath->buf);
483*6a54128fSAndroid Build Coastguard Worker if (retval)
484*6a54128fSAndroid Build Coastguard Worker return retval;
485*6a54128fSAndroid Build Coastguard Worker }
486*6a54128fSAndroid Build Coastguard Worker handle->level++;
487*6a54128fSAndroid Build Coastguard Worker
488*6a54128fSAndroid Build Coastguard Worker eh = (struct ext3_extent_header *) newpath->buf;
489*6a54128fSAndroid Build Coastguard Worker
490*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_header_verify(eh, handle->fs->blocksize);
491*6a54128fSAndroid Build Coastguard Worker if (retval) {
492*6a54128fSAndroid Build Coastguard Worker handle->level--;
493*6a54128fSAndroid Build Coastguard Worker return retval;
494*6a54128fSAndroid Build Coastguard Worker }
495*6a54128fSAndroid Build Coastguard Worker
496*6a54128fSAndroid Build Coastguard Worker if (!(handle->fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
497*6a54128fSAndroid Build Coastguard Worker !ext2fs_extent_block_csum_verify(handle->fs, handle->ino,
498*6a54128fSAndroid Build Coastguard Worker eh))
499*6a54128fSAndroid Build Coastguard Worker failed_csum = 1;
500*6a54128fSAndroid Build Coastguard Worker
501*6a54128fSAndroid Build Coastguard Worker newpath->left = newpath->entries =
502*6a54128fSAndroid Build Coastguard Worker ext2fs_le16_to_cpu(eh->eh_entries);
503*6a54128fSAndroid Build Coastguard Worker newpath->max_entries = ext2fs_le16_to_cpu(eh->eh_max);
504*6a54128fSAndroid Build Coastguard Worker
505*6a54128fSAndroid Build Coastguard Worker /* Make sure there is at least one extent present */
506*6a54128fSAndroid Build Coastguard Worker if (newpath->left <= 0)
507*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_NO_DOWN;
508*6a54128fSAndroid Build Coastguard Worker
509*6a54128fSAndroid Build Coastguard Worker if (path->left > 0) {
510*6a54128fSAndroid Build Coastguard Worker ix++;
511*6a54128fSAndroid Build Coastguard Worker newpath->end_blk = ext2fs_le32_to_cpu(ix->ei_block);
512*6a54128fSAndroid Build Coastguard Worker } else
513*6a54128fSAndroid Build Coastguard Worker newpath->end_blk = path->end_blk;
514*6a54128fSAndroid Build Coastguard Worker
515*6a54128fSAndroid Build Coastguard Worker path = newpath;
516*6a54128fSAndroid Build Coastguard Worker if (op == EXT2_EXTENT_DOWN) {
517*6a54128fSAndroid Build Coastguard Worker ix = EXT_FIRST_INDEX((struct ext3_extent_header *) eh);
518*6a54128fSAndroid Build Coastguard Worker path->curr = ix;
519*6a54128fSAndroid Build Coastguard Worker path->left = path->entries - 1;
520*6a54128fSAndroid Build Coastguard Worker path->visit_num = 0;
521*6a54128fSAndroid Build Coastguard Worker } else {
522*6a54128fSAndroid Build Coastguard Worker ix = EXT_LAST_INDEX((struct ext3_extent_header *) eh);
523*6a54128fSAndroid Build Coastguard Worker path->curr = ix;
524*6a54128fSAndroid Build Coastguard Worker path->left = 0;
525*6a54128fSAndroid Build Coastguard Worker if (handle->level < handle->max_depth)
526*6a54128fSAndroid Build Coastguard Worker path->visit_num = 1;
527*6a54128fSAndroid Build Coastguard Worker }
528*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG_GET_EXTENT
529*6a54128fSAndroid Build Coastguard Worker printf("Down to level %d/%d, end_blk=%llu\n",
530*6a54128fSAndroid Build Coastguard Worker handle->level, handle->max_depth,
531*6a54128fSAndroid Build Coastguard Worker path->end_blk);
532*6a54128fSAndroid Build Coastguard Worker #endif
533*6a54128fSAndroid Build Coastguard Worker break;
534*6a54128fSAndroid Build Coastguard Worker default:
535*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_OP_NOT_SUPPORTED;
536*6a54128fSAndroid Build Coastguard Worker }
537*6a54128fSAndroid Build Coastguard Worker
538*6a54128fSAndroid Build Coastguard Worker if (!ix)
539*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_CURRENT_NODE;
540*6a54128fSAndroid Build Coastguard Worker
541*6a54128fSAndroid Build Coastguard Worker extent->e_flags = 0;
542*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG_GET_EXTENT
543*6a54128fSAndroid Build Coastguard Worker printf("(Left %d)\n", path->left);
544*6a54128fSAndroid Build Coastguard Worker #endif
545*6a54128fSAndroid Build Coastguard Worker
546*6a54128fSAndroid Build Coastguard Worker if (handle->level == handle->max_depth) {
547*6a54128fSAndroid Build Coastguard Worker ex = (struct ext3_extent *) ix;
548*6a54128fSAndroid Build Coastguard Worker
549*6a54128fSAndroid Build Coastguard Worker extent->e_pblk = ext2fs_le32_to_cpu(ex->ee_start) +
550*6a54128fSAndroid Build Coastguard Worker ((__u64) ext2fs_le16_to_cpu(ex->ee_start_hi) << 32);
551*6a54128fSAndroid Build Coastguard Worker extent->e_lblk = ext2fs_le32_to_cpu(ex->ee_block);
552*6a54128fSAndroid Build Coastguard Worker extent->e_len = ext2fs_le16_to_cpu(ex->ee_len);
553*6a54128fSAndroid Build Coastguard Worker extent->e_flags |= EXT2_EXTENT_FLAGS_LEAF;
554*6a54128fSAndroid Build Coastguard Worker if (extent->e_len > EXT_INIT_MAX_LEN) {
555*6a54128fSAndroid Build Coastguard Worker extent->e_len -= EXT_INIT_MAX_LEN;
556*6a54128fSAndroid Build Coastguard Worker extent->e_flags |= EXT2_EXTENT_FLAGS_UNINIT;
557*6a54128fSAndroid Build Coastguard Worker }
558*6a54128fSAndroid Build Coastguard Worker } else {
559*6a54128fSAndroid Build Coastguard Worker extent->e_pblk = ext2fs_le32_to_cpu(ix->ei_leaf) +
560*6a54128fSAndroid Build Coastguard Worker ((__u64) ext2fs_le16_to_cpu(ix->ei_leaf_hi) << 32);
561*6a54128fSAndroid Build Coastguard Worker extent->e_lblk = ext2fs_le32_to_cpu(ix->ei_block);
562*6a54128fSAndroid Build Coastguard Worker if (path->left > 0) {
563*6a54128fSAndroid Build Coastguard Worker ix++;
564*6a54128fSAndroid Build Coastguard Worker end_blk = ext2fs_le32_to_cpu(ix->ei_block);
565*6a54128fSAndroid Build Coastguard Worker } else
566*6a54128fSAndroid Build Coastguard Worker end_blk = path->end_blk;
567*6a54128fSAndroid Build Coastguard Worker
568*6a54128fSAndroid Build Coastguard Worker extent->e_len = end_blk - extent->e_lblk;
569*6a54128fSAndroid Build Coastguard Worker }
570*6a54128fSAndroid Build Coastguard Worker if (path->visit_num)
571*6a54128fSAndroid Build Coastguard Worker extent->e_flags |= EXT2_EXTENT_FLAGS_SECOND_VISIT;
572*6a54128fSAndroid Build Coastguard Worker
573*6a54128fSAndroid Build Coastguard Worker if (((orig_op == EXT2_EXTENT_NEXT_LEAF) ||
574*6a54128fSAndroid Build Coastguard Worker (orig_op == EXT2_EXTENT_PREV_LEAF)) &&
575*6a54128fSAndroid Build Coastguard Worker (handle->level != handle->max_depth))
576*6a54128fSAndroid Build Coastguard Worker goto retry;
577*6a54128fSAndroid Build Coastguard Worker
578*6a54128fSAndroid Build Coastguard Worker if ((orig_op == EXT2_EXTENT_LAST_LEAF) &&
579*6a54128fSAndroid Build Coastguard Worker ((handle->level != handle->max_depth) ||
580*6a54128fSAndroid Build Coastguard Worker (path->left != 0)))
581*6a54128fSAndroid Build Coastguard Worker goto retry;
582*6a54128fSAndroid Build Coastguard Worker
583*6a54128fSAndroid Build Coastguard Worker if (failed_csum)
584*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_CSUM_INVALID;
585*6a54128fSAndroid Build Coastguard Worker
586*6a54128fSAndroid Build Coastguard Worker return 0;
587*6a54128fSAndroid Build Coastguard Worker }
588*6a54128fSAndroid Build Coastguard Worker
update_path(ext2_extent_handle_t handle)589*6a54128fSAndroid Build Coastguard Worker static errcode_t update_path(ext2_extent_handle_t handle)
590*6a54128fSAndroid Build Coastguard Worker {
591*6a54128fSAndroid Build Coastguard Worker blk64_t blk;
592*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
593*6a54128fSAndroid Build Coastguard Worker struct ext3_extent_idx *ix;
594*6a54128fSAndroid Build Coastguard Worker struct ext3_extent_header *eh;
595*6a54128fSAndroid Build Coastguard Worker
596*6a54128fSAndroid Build Coastguard Worker if (handle->level == 0) {
597*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_write_inode(handle->fs, handle->ino,
598*6a54128fSAndroid Build Coastguard Worker handle->inode);
599*6a54128fSAndroid Build Coastguard Worker } else {
600*6a54128fSAndroid Build Coastguard Worker ix = handle->path[handle->level - 1].curr;
601*6a54128fSAndroid Build Coastguard Worker blk = ext2fs_le32_to_cpu(ix->ei_leaf) +
602*6a54128fSAndroid Build Coastguard Worker ((__u64) ext2fs_le16_to_cpu(ix->ei_leaf_hi) << 32);
603*6a54128fSAndroid Build Coastguard Worker
604*6a54128fSAndroid Build Coastguard Worker /* then update the checksum */
605*6a54128fSAndroid Build Coastguard Worker eh = (struct ext3_extent_header *)
606*6a54128fSAndroid Build Coastguard Worker handle->path[handle->level].buf;
607*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_block_csum_set(handle->fs, handle->ino,
608*6a54128fSAndroid Build Coastguard Worker eh);
609*6a54128fSAndroid Build Coastguard Worker if (retval)
610*6a54128fSAndroid Build Coastguard Worker return retval;
611*6a54128fSAndroid Build Coastguard Worker
612*6a54128fSAndroid Build Coastguard Worker retval = io_channel_write_blk64(handle->fs->io,
613*6a54128fSAndroid Build Coastguard Worker blk, 1, handle->path[handle->level].buf);
614*6a54128fSAndroid Build Coastguard Worker }
615*6a54128fSAndroid Build Coastguard Worker return retval;
616*6a54128fSAndroid Build Coastguard Worker }
617*6a54128fSAndroid Build Coastguard Worker
618*6a54128fSAndroid Build Coastguard Worker #if 0
619*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_extent_save_path(ext2_extent_handle_t handle,
620*6a54128fSAndroid Build Coastguard Worker ext2_extent_path_t *ret_path)
621*6a54128fSAndroid Build Coastguard Worker {
622*6a54128fSAndroid Build Coastguard Worker ext2_extent_path_t save_path;
623*6a54128fSAndroid Build Coastguard Worker struct ext2fs_extent extent;
624*6a54128fSAndroid Build Coastguard Worker struct ext2_extent_info info;
625*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
626*6a54128fSAndroid Build Coastguard Worker
627*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT, &extent);
628*6a54128fSAndroid Build Coastguard Worker if (retval)
629*6a54128fSAndroid Build Coastguard Worker return retval;
630*6a54128fSAndroid Build Coastguard Worker
631*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get_info(handle, &info);
632*6a54128fSAndroid Build Coastguard Worker if (retval)
633*6a54128fSAndroid Build Coastguard Worker return retval;
634*6a54128fSAndroid Build Coastguard Worker
635*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_mem(sizeof(struct ext2_extent_path), &save_path);
636*6a54128fSAndroid Build Coastguard Worker if (retval)
637*6a54128fSAndroid Build Coastguard Worker return retval;
638*6a54128fSAndroid Build Coastguard Worker memset(save_path, 0, sizeof(struct ext2_extent_path));
639*6a54128fSAndroid Build Coastguard Worker
640*6a54128fSAndroid Build Coastguard Worker save_path->magic = EXT2_ET_MAGIC_EXTENT_PATH;
641*6a54128fSAndroid Build Coastguard Worker save_path->leaf_height = info.max_depth - info.curr_level - 1;
642*6a54128fSAndroid Build Coastguard Worker save_path->lblk = extent.e_lblk;
643*6a54128fSAndroid Build Coastguard Worker
644*6a54128fSAndroid Build Coastguard Worker *ret_path = save_path;
645*6a54128fSAndroid Build Coastguard Worker return 0;
646*6a54128fSAndroid Build Coastguard Worker }
647*6a54128fSAndroid Build Coastguard Worker
648*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_extent_free_path(ext2_extent_path_t path)
649*6a54128fSAndroid Build Coastguard Worker {
650*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(path, EXT2_ET_MAGIC_EXTENT_PATH);
651*6a54128fSAndroid Build Coastguard Worker
652*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&path);
653*6a54128fSAndroid Build Coastguard Worker return 0;
654*6a54128fSAndroid Build Coastguard Worker }
655*6a54128fSAndroid Build Coastguard Worker #endif
656*6a54128fSAndroid Build Coastguard Worker
657*6a54128fSAndroid Build Coastguard Worker /*
658*6a54128fSAndroid Build Coastguard Worker * Go to the node at leaf_level which contains logical block blk.
659*6a54128fSAndroid Build Coastguard Worker *
660*6a54128fSAndroid Build Coastguard Worker * leaf_level is height from the leaf node level, i.e.
661*6a54128fSAndroid Build Coastguard Worker * leaf_level 0 is at leaf node, leaf_level 1 is 1 above etc.
662*6a54128fSAndroid Build Coastguard Worker *
663*6a54128fSAndroid Build Coastguard Worker * If "blk" has no mapping (hole) then handle is left at last
664*6a54128fSAndroid Build Coastguard Worker * extent before blk.
665*6a54128fSAndroid Build Coastguard Worker */
ext2fs_extent_goto2(ext2_extent_handle_t handle,int leaf_level,blk64_t blk)666*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_extent_goto2(ext2_extent_handle_t handle,
667*6a54128fSAndroid Build Coastguard Worker int leaf_level, blk64_t blk)
668*6a54128fSAndroid Build Coastguard Worker {
669*6a54128fSAndroid Build Coastguard Worker struct ext2fs_extent extent;
670*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
671*6a54128fSAndroid Build Coastguard Worker
672*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent);
673*6a54128fSAndroid Build Coastguard Worker if (retval) {
674*6a54128fSAndroid Build Coastguard Worker if (retval == EXT2_ET_EXTENT_NO_NEXT)
675*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_EXTENT_NOT_FOUND;
676*6a54128fSAndroid Build Coastguard Worker return retval;
677*6a54128fSAndroid Build Coastguard Worker }
678*6a54128fSAndroid Build Coastguard Worker
679*6a54128fSAndroid Build Coastguard Worker if (leaf_level > handle->max_depth) {
680*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
681*6a54128fSAndroid Build Coastguard Worker printf("leaf level %d greater than tree depth %d\n",
682*6a54128fSAndroid Build Coastguard Worker leaf_level, handle->max_depth);
683*6a54128fSAndroid Build Coastguard Worker #endif
684*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_OP_NOT_SUPPORTED;
685*6a54128fSAndroid Build Coastguard Worker }
686*6a54128fSAndroid Build Coastguard Worker
687*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
688*6a54128fSAndroid Build Coastguard Worker printf("goto extent ino %u, level %d, %llu\n", handle->ino,
689*6a54128fSAndroid Build Coastguard Worker leaf_level, blk);
690*6a54128fSAndroid Build Coastguard Worker #endif
691*6a54128fSAndroid Build Coastguard Worker
692*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG_GOTO_EXTENTS
693*6a54128fSAndroid Build Coastguard Worker dbg_print_extent("root", &extent);
694*6a54128fSAndroid Build Coastguard Worker #endif
695*6a54128fSAndroid Build Coastguard Worker while (1) {
696*6a54128fSAndroid Build Coastguard Worker if (handle->max_depth - handle->level == leaf_level) {
697*6a54128fSAndroid Build Coastguard Worker /* block is in this &extent */
698*6a54128fSAndroid Build Coastguard Worker if ((blk >= extent.e_lblk) &&
699*6a54128fSAndroid Build Coastguard Worker (blk < extent.e_lblk + extent.e_len))
700*6a54128fSAndroid Build Coastguard Worker return 0;
701*6a54128fSAndroid Build Coastguard Worker if (blk < extent.e_lblk) {
702*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle,
703*6a54128fSAndroid Build Coastguard Worker EXT2_EXTENT_PREV_SIB,
704*6a54128fSAndroid Build Coastguard Worker &extent);
705*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_NOT_FOUND;
706*6a54128fSAndroid Build Coastguard Worker }
707*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle,
708*6a54128fSAndroid Build Coastguard Worker EXT2_EXTENT_NEXT_SIB,
709*6a54128fSAndroid Build Coastguard Worker &extent);
710*6a54128fSAndroid Build Coastguard Worker if (retval == EXT2_ET_EXTENT_NO_NEXT)
711*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_NOT_FOUND;
712*6a54128fSAndroid Build Coastguard Worker if (retval)
713*6a54128fSAndroid Build Coastguard Worker return retval;
714*6a54128fSAndroid Build Coastguard Worker continue;
715*6a54128fSAndroid Build Coastguard Worker }
716*6a54128fSAndroid Build Coastguard Worker
717*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle, EXT2_EXTENT_NEXT_SIB,
718*6a54128fSAndroid Build Coastguard Worker &extent);
719*6a54128fSAndroid Build Coastguard Worker if (retval == EXT2_ET_EXTENT_NO_NEXT)
720*6a54128fSAndroid Build Coastguard Worker goto go_down;
721*6a54128fSAndroid Build Coastguard Worker if (retval)
722*6a54128fSAndroid Build Coastguard Worker return retval;
723*6a54128fSAndroid Build Coastguard Worker
724*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG_GOTO_EXTENTS
725*6a54128fSAndroid Build Coastguard Worker dbg_print_extent("next", &extent);
726*6a54128fSAndroid Build Coastguard Worker #endif
727*6a54128fSAndroid Build Coastguard Worker if (blk == extent.e_lblk)
728*6a54128fSAndroid Build Coastguard Worker goto go_down;
729*6a54128fSAndroid Build Coastguard Worker if (blk > extent.e_lblk)
730*6a54128fSAndroid Build Coastguard Worker continue;
731*6a54128fSAndroid Build Coastguard Worker
732*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle, EXT2_EXTENT_PREV_SIB,
733*6a54128fSAndroid Build Coastguard Worker &extent);
734*6a54128fSAndroid Build Coastguard Worker if (retval)
735*6a54128fSAndroid Build Coastguard Worker return retval;
736*6a54128fSAndroid Build Coastguard Worker
737*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG_GOTO_EXTENTS
738*6a54128fSAndroid Build Coastguard Worker dbg_print_extent("prev", &extent);
739*6a54128fSAndroid Build Coastguard Worker #endif
740*6a54128fSAndroid Build Coastguard Worker
741*6a54128fSAndroid Build Coastguard Worker go_down:
742*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle, EXT2_EXTENT_DOWN,
743*6a54128fSAndroid Build Coastguard Worker &extent);
744*6a54128fSAndroid Build Coastguard Worker if (retval)
745*6a54128fSAndroid Build Coastguard Worker return retval;
746*6a54128fSAndroid Build Coastguard Worker
747*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG_GOTO_EXTENTS
748*6a54128fSAndroid Build Coastguard Worker dbg_print_extent("down", &extent);
749*6a54128fSAndroid Build Coastguard Worker #endif
750*6a54128fSAndroid Build Coastguard Worker }
751*6a54128fSAndroid Build Coastguard Worker }
752*6a54128fSAndroid Build Coastguard Worker
ext2fs_extent_goto(ext2_extent_handle_t handle,blk64_t blk)753*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_extent_goto(ext2_extent_handle_t handle,
754*6a54128fSAndroid Build Coastguard Worker blk64_t blk)
755*6a54128fSAndroid Build Coastguard Worker {
756*6a54128fSAndroid Build Coastguard Worker return ext2fs_extent_goto2(handle, 0, blk);
757*6a54128fSAndroid Build Coastguard Worker }
758*6a54128fSAndroid Build Coastguard Worker
759*6a54128fSAndroid Build Coastguard Worker /*
760*6a54128fSAndroid Build Coastguard Worker * Traverse back up to root fixing parents of current node as needed.
761*6a54128fSAndroid Build Coastguard Worker *
762*6a54128fSAndroid Build Coastguard Worker * If we changed start of first entry in a node, fix parent index start
763*6a54128fSAndroid Build Coastguard Worker * and so on.
764*6a54128fSAndroid Build Coastguard Worker *
765*6a54128fSAndroid Build Coastguard Worker * Safe to call for any position in node; if not at the first entry,
766*6a54128fSAndroid Build Coastguard Worker * it will simply return.
767*6a54128fSAndroid Build Coastguard Worker *
768*6a54128fSAndroid Build Coastguard Worker * Note a subtlety of this function -- if there happen to be two extents
769*6a54128fSAndroid Build Coastguard Worker * mapping the same lblk and someone calls fix_parents on the second of the two
770*6a54128fSAndroid Build Coastguard Worker * extents, the position of the extent handle after the call will be the second
771*6a54128fSAndroid Build Coastguard Worker * extent if nothing happened, or the first extent if something did. A caller
772*6a54128fSAndroid Build Coastguard Worker * in this situation must use ext2fs_extent_goto() after calling this function.
773*6a54128fSAndroid Build Coastguard Worker * Or simply don't map the same lblk with two extents, ever.
774*6a54128fSAndroid Build Coastguard Worker */
ext2fs_extent_fix_parents(ext2_extent_handle_t handle)775*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_extent_fix_parents(ext2_extent_handle_t handle)
776*6a54128fSAndroid Build Coastguard Worker {
777*6a54128fSAndroid Build Coastguard Worker int retval = 0;
778*6a54128fSAndroid Build Coastguard Worker int orig_height;
779*6a54128fSAndroid Build Coastguard Worker blk64_t start;
780*6a54128fSAndroid Build Coastguard Worker struct extent_path *path;
781*6a54128fSAndroid Build Coastguard Worker struct ext2fs_extent extent;
782*6a54128fSAndroid Build Coastguard Worker struct ext2_extent_info info;
783*6a54128fSAndroid Build Coastguard Worker
784*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EXTENT_HANDLE);
785*6a54128fSAndroid Build Coastguard Worker
786*6a54128fSAndroid Build Coastguard Worker if (!(handle->fs->flags & EXT2_FLAG_RW))
787*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_RO_FILSYS;
788*6a54128fSAndroid Build Coastguard Worker
789*6a54128fSAndroid Build Coastguard Worker if (!handle->path)
790*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_CURRENT_NODE;
791*6a54128fSAndroid Build Coastguard Worker
792*6a54128fSAndroid Build Coastguard Worker path = handle->path + handle->level;
793*6a54128fSAndroid Build Coastguard Worker if (!path->curr)
794*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_CURRENT_NODE;
795*6a54128fSAndroid Build Coastguard Worker
796*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT, &extent);
797*6a54128fSAndroid Build Coastguard Worker if (retval)
798*6a54128fSAndroid Build Coastguard Worker goto done;
799*6a54128fSAndroid Build Coastguard Worker
800*6a54128fSAndroid Build Coastguard Worker /* modified node's start block */
801*6a54128fSAndroid Build Coastguard Worker start = extent.e_lblk;
802*6a54128fSAndroid Build Coastguard Worker
803*6a54128fSAndroid Build Coastguard Worker if ((retval = ext2fs_extent_get_info(handle, &info)))
804*6a54128fSAndroid Build Coastguard Worker return retval;
805*6a54128fSAndroid Build Coastguard Worker orig_height = info.max_depth - info.curr_level;
806*6a54128fSAndroid Build Coastguard Worker
807*6a54128fSAndroid Build Coastguard Worker /* traverse up until index not first, or startblk matches, or top */
808*6a54128fSAndroid Build Coastguard Worker while (handle->level > 0 &&
809*6a54128fSAndroid Build Coastguard Worker (path->left == path->entries - 1)) {
810*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle, EXT2_EXTENT_UP, &extent);
811*6a54128fSAndroid Build Coastguard Worker if (retval)
812*6a54128fSAndroid Build Coastguard Worker goto done;
813*6a54128fSAndroid Build Coastguard Worker if (extent.e_lblk == start)
814*6a54128fSAndroid Build Coastguard Worker break;
815*6a54128fSAndroid Build Coastguard Worker path = handle->path + handle->level;
816*6a54128fSAndroid Build Coastguard Worker extent.e_len += (extent.e_lblk - start);
817*6a54128fSAndroid Build Coastguard Worker extent.e_lblk = start;
818*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_replace(handle, 0, &extent);
819*6a54128fSAndroid Build Coastguard Worker if (retval)
820*6a54128fSAndroid Build Coastguard Worker goto done;
821*6a54128fSAndroid Build Coastguard Worker update_path(handle);
822*6a54128fSAndroid Build Coastguard Worker }
823*6a54128fSAndroid Build Coastguard Worker
824*6a54128fSAndroid Build Coastguard Worker /* put handle back to where we started */
825*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_goto2(handle, orig_height, start);
826*6a54128fSAndroid Build Coastguard Worker done:
827*6a54128fSAndroid Build Coastguard Worker return retval;
828*6a54128fSAndroid Build Coastguard Worker }
829*6a54128fSAndroid Build Coastguard Worker
ext2fs_extent_replace(ext2_extent_handle_t handle,int flags EXT2FS_ATTR ((unused)),struct ext2fs_extent * extent)830*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_extent_replace(ext2_extent_handle_t handle,
831*6a54128fSAndroid Build Coastguard Worker int flags EXT2FS_ATTR((unused)),
832*6a54128fSAndroid Build Coastguard Worker struct ext2fs_extent *extent)
833*6a54128fSAndroid Build Coastguard Worker {
834*6a54128fSAndroid Build Coastguard Worker struct extent_path *path;
835*6a54128fSAndroid Build Coastguard Worker struct ext3_extent_idx *ix;
836*6a54128fSAndroid Build Coastguard Worker struct ext3_extent *ex;
837*6a54128fSAndroid Build Coastguard Worker
838*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EXTENT_HANDLE);
839*6a54128fSAndroid Build Coastguard Worker
840*6a54128fSAndroid Build Coastguard Worker if (!(handle->fs->flags & EXT2_FLAG_RW))
841*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_RO_FILSYS;
842*6a54128fSAndroid Build Coastguard Worker
843*6a54128fSAndroid Build Coastguard Worker if (!handle->path)
844*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_CURRENT_NODE;
845*6a54128fSAndroid Build Coastguard Worker
846*6a54128fSAndroid Build Coastguard Worker path = handle->path + handle->level;
847*6a54128fSAndroid Build Coastguard Worker if (!path->curr)
848*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_CURRENT_NODE;
849*6a54128fSAndroid Build Coastguard Worker
850*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
851*6a54128fSAndroid Build Coastguard Worker printf("extent replace: %u ", handle->ino);
852*6a54128fSAndroid Build Coastguard Worker dbg_print_extent(0, extent);
853*6a54128fSAndroid Build Coastguard Worker #endif
854*6a54128fSAndroid Build Coastguard Worker
855*6a54128fSAndroid Build Coastguard Worker if (handle->level == handle->max_depth) {
856*6a54128fSAndroid Build Coastguard Worker ex = path->curr;
857*6a54128fSAndroid Build Coastguard Worker
858*6a54128fSAndroid Build Coastguard Worker ex->ee_block = ext2fs_cpu_to_le32(extent->e_lblk);
859*6a54128fSAndroid Build Coastguard Worker ex->ee_start = ext2fs_cpu_to_le32(extent->e_pblk & 0xFFFFFFFF);
860*6a54128fSAndroid Build Coastguard Worker ex->ee_start_hi = ext2fs_cpu_to_le16(extent->e_pblk >> 32);
861*6a54128fSAndroid Build Coastguard Worker if (extent->e_flags & EXT2_EXTENT_FLAGS_UNINIT) {
862*6a54128fSAndroid Build Coastguard Worker if (extent->e_len > EXT_UNINIT_MAX_LEN)
863*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_INVALID_LENGTH;
864*6a54128fSAndroid Build Coastguard Worker ex->ee_len = ext2fs_cpu_to_le16(extent->e_len +
865*6a54128fSAndroid Build Coastguard Worker EXT_INIT_MAX_LEN);
866*6a54128fSAndroid Build Coastguard Worker } else {
867*6a54128fSAndroid Build Coastguard Worker if (extent->e_len > EXT_INIT_MAX_LEN)
868*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_INVALID_LENGTH;
869*6a54128fSAndroid Build Coastguard Worker ex->ee_len = ext2fs_cpu_to_le16(extent->e_len);
870*6a54128fSAndroid Build Coastguard Worker }
871*6a54128fSAndroid Build Coastguard Worker } else {
872*6a54128fSAndroid Build Coastguard Worker ix = path->curr;
873*6a54128fSAndroid Build Coastguard Worker
874*6a54128fSAndroid Build Coastguard Worker ix->ei_leaf = ext2fs_cpu_to_le32(extent->e_pblk & 0xFFFFFFFF);
875*6a54128fSAndroid Build Coastguard Worker ix->ei_leaf_hi = ext2fs_cpu_to_le16(extent->e_pblk >> 32);
876*6a54128fSAndroid Build Coastguard Worker ix->ei_block = ext2fs_cpu_to_le32(extent->e_lblk);
877*6a54128fSAndroid Build Coastguard Worker ix->ei_unused = 0;
878*6a54128fSAndroid Build Coastguard Worker }
879*6a54128fSAndroid Build Coastguard Worker update_path(handle);
880*6a54128fSAndroid Build Coastguard Worker return 0;
881*6a54128fSAndroid Build Coastguard Worker }
882*6a54128fSAndroid Build Coastguard Worker
splitting_at_eof(struct ext2_extent_handle * handle,struct extent_path * path)883*6a54128fSAndroid Build Coastguard Worker static int splitting_at_eof(struct ext2_extent_handle *handle,
884*6a54128fSAndroid Build Coastguard Worker struct extent_path *path)
885*6a54128fSAndroid Build Coastguard Worker {
886*6a54128fSAndroid Build Coastguard Worker struct extent_path *ppp = path;
887*6a54128fSAndroid Build Coastguard Worker dump_path(__func__, handle, path);
888*6a54128fSAndroid Build Coastguard Worker
889*6a54128fSAndroid Build Coastguard Worker if (handle->level == 0)
890*6a54128fSAndroid Build Coastguard Worker return 0;
891*6a54128fSAndroid Build Coastguard Worker
892*6a54128fSAndroid Build Coastguard Worker do {
893*6a54128fSAndroid Build Coastguard Worker if (ppp->left)
894*6a54128fSAndroid Build Coastguard Worker return 0;
895*6a54128fSAndroid Build Coastguard Worker ppp--;
896*6a54128fSAndroid Build Coastguard Worker } while (ppp >= handle->path);
897*6a54128fSAndroid Build Coastguard Worker
898*6a54128fSAndroid Build Coastguard Worker return 1;
899*6a54128fSAndroid Build Coastguard Worker }
900*6a54128fSAndroid Build Coastguard Worker
901*6a54128fSAndroid Build Coastguard Worker /*
902*6a54128fSAndroid Build Coastguard Worker * allocate a new block, move half the current node to it, and update parent
903*6a54128fSAndroid Build Coastguard Worker *
904*6a54128fSAndroid Build Coastguard Worker * handle will be left pointing at original record.
905*6a54128fSAndroid Build Coastguard Worker */
extent_node_split(ext2_extent_handle_t handle,int expand_allowed)906*6a54128fSAndroid Build Coastguard Worker static errcode_t extent_node_split(ext2_extent_handle_t handle,
907*6a54128fSAndroid Build Coastguard Worker int expand_allowed)
908*6a54128fSAndroid Build Coastguard Worker {
909*6a54128fSAndroid Build Coastguard Worker errcode_t retval = 0;
910*6a54128fSAndroid Build Coastguard Worker blk64_t new_node_pblk;
911*6a54128fSAndroid Build Coastguard Worker blk64_t new_node_start;
912*6a54128fSAndroid Build Coastguard Worker blk64_t orig_lblk;
913*6a54128fSAndroid Build Coastguard Worker blk64_t goal_blk = 0;
914*6a54128fSAndroid Build Coastguard Worker int orig_height;
915*6a54128fSAndroid Build Coastguard Worker char *block_buf = NULL;
916*6a54128fSAndroid Build Coastguard Worker struct ext2fs_extent extent;
917*6a54128fSAndroid Build Coastguard Worker struct extent_path *path, *newpath = 0;
918*6a54128fSAndroid Build Coastguard Worker struct ext3_extent_header *eh, *neweh;
919*6a54128fSAndroid Build Coastguard Worker int tocopy;
920*6a54128fSAndroid Build Coastguard Worker int new_root = 0;
921*6a54128fSAndroid Build Coastguard Worker struct ext2_extent_info info;
922*6a54128fSAndroid Build Coastguard Worker int no_balance;
923*6a54128fSAndroid Build Coastguard Worker
924*6a54128fSAndroid Build Coastguard Worker /* basic sanity */
925*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EXTENT_HANDLE);
926*6a54128fSAndroid Build Coastguard Worker
927*6a54128fSAndroid Build Coastguard Worker if (!(handle->fs->flags & EXT2_FLAG_RW))
928*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_RO_FILSYS;
929*6a54128fSAndroid Build Coastguard Worker
930*6a54128fSAndroid Build Coastguard Worker if (!handle->path)
931*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_CURRENT_NODE;
932*6a54128fSAndroid Build Coastguard Worker
933*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
934*6a54128fSAndroid Build Coastguard Worker printf("splitting node at level %d\n", handle->level);
935*6a54128fSAndroid Build Coastguard Worker #endif
936*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT, &extent);
937*6a54128fSAndroid Build Coastguard Worker if (retval)
938*6a54128fSAndroid Build Coastguard Worker goto done;
939*6a54128fSAndroid Build Coastguard Worker
940*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get_info(handle, &info);
941*6a54128fSAndroid Build Coastguard Worker if (retval)
942*6a54128fSAndroid Build Coastguard Worker goto done;
943*6a54128fSAndroid Build Coastguard Worker
944*6a54128fSAndroid Build Coastguard Worker /* save the position we were originally splitting... */
945*6a54128fSAndroid Build Coastguard Worker orig_height = info.max_depth - info.curr_level;
946*6a54128fSAndroid Build Coastguard Worker orig_lblk = extent.e_lblk;
947*6a54128fSAndroid Build Coastguard Worker
948*6a54128fSAndroid Build Coastguard Worker /* Try to put the index block before the first extent */
949*6a54128fSAndroid Build Coastguard Worker path = handle->path + handle->level;
950*6a54128fSAndroid Build Coastguard Worker eh = (struct ext3_extent_header *) path->buf;
951*6a54128fSAndroid Build Coastguard Worker if (handle->level == handle->max_depth) {
952*6a54128fSAndroid Build Coastguard Worker struct ext3_extent *ex;
953*6a54128fSAndroid Build Coastguard Worker
954*6a54128fSAndroid Build Coastguard Worker ex = EXT_FIRST_EXTENT(eh);
955*6a54128fSAndroid Build Coastguard Worker goal_blk = ext2fs_le32_to_cpu(ex->ee_start) +
956*6a54128fSAndroid Build Coastguard Worker ((__u64) ext2fs_le16_to_cpu(ex->ee_start_hi) << 32);
957*6a54128fSAndroid Build Coastguard Worker } else {
958*6a54128fSAndroid Build Coastguard Worker struct ext3_extent_idx *ix;
959*6a54128fSAndroid Build Coastguard Worker
960*6a54128fSAndroid Build Coastguard Worker ix = EXT_FIRST_INDEX(eh);
961*6a54128fSAndroid Build Coastguard Worker goal_blk = ext2fs_le32_to_cpu(ix->ei_leaf) +
962*6a54128fSAndroid Build Coastguard Worker ((__u64) ext2fs_le16_to_cpu(ix->ei_leaf_hi) << 32);
963*6a54128fSAndroid Build Coastguard Worker }
964*6a54128fSAndroid Build Coastguard Worker goal_blk -= EXT2FS_CLUSTER_RATIO(handle->fs);
965*6a54128fSAndroid Build Coastguard Worker goal_blk &= ~EXT2FS_CLUSTER_MASK(handle->fs);
966*6a54128fSAndroid Build Coastguard Worker
967*6a54128fSAndroid Build Coastguard Worker /* Is there room in the parent for a new entry? */
968*6a54128fSAndroid Build Coastguard Worker if (handle->level &&
969*6a54128fSAndroid Build Coastguard Worker (handle->path[handle->level - 1].entries >=
970*6a54128fSAndroid Build Coastguard Worker handle->path[handle->level - 1].max_entries)) {
971*6a54128fSAndroid Build Coastguard Worker
972*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
973*6a54128fSAndroid Build Coastguard Worker printf("parent level %d full; splitting it too\n",
974*6a54128fSAndroid Build Coastguard Worker handle->level - 1);
975*6a54128fSAndroid Build Coastguard Worker #endif
976*6a54128fSAndroid Build Coastguard Worker /* split the parent */
977*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle, EXT2_EXTENT_UP, &extent);
978*6a54128fSAndroid Build Coastguard Worker if (retval)
979*6a54128fSAndroid Build Coastguard Worker goto done;
980*6a54128fSAndroid Build Coastguard Worker
981*6a54128fSAndroid Build Coastguard Worker retval = extent_node_split(handle, expand_allowed);
982*6a54128fSAndroid Build Coastguard Worker if (retval)
983*6a54128fSAndroid Build Coastguard Worker goto done;
984*6a54128fSAndroid Build Coastguard Worker
985*6a54128fSAndroid Build Coastguard Worker /* get handle back to our original split position */
986*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_goto2(handle, orig_height, orig_lblk);
987*6a54128fSAndroid Build Coastguard Worker if (retval)
988*6a54128fSAndroid Build Coastguard Worker goto done;
989*6a54128fSAndroid Build Coastguard Worker }
990*6a54128fSAndroid Build Coastguard Worker
991*6a54128fSAndroid Build Coastguard Worker /* At this point, parent should have room for this split */
992*6a54128fSAndroid Build Coastguard Worker path = handle->path + handle->level;
993*6a54128fSAndroid Build Coastguard Worker if (!path->curr)
994*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_CURRENT_NODE;
995*6a54128fSAndroid Build Coastguard Worker
996*6a54128fSAndroid Build Coastguard Worker /*
997*6a54128fSAndroid Build Coastguard Worker * Normally, we try to split a full node in half. This doesn't turn
998*6a54128fSAndroid Build Coastguard Worker * out so well if we're tacking extents on the end of the file because
999*6a54128fSAndroid Build Coastguard Worker * then we're stuck with a tree of half-full extent blocks. This of
1000*6a54128fSAndroid Build Coastguard Worker * course doesn't apply to the root level.
1001*6a54128fSAndroid Build Coastguard Worker */
1002*6a54128fSAndroid Build Coastguard Worker no_balance = expand_allowed ? splitting_at_eof(handle, path) : 0;
1003*6a54128fSAndroid Build Coastguard Worker
1004*6a54128fSAndroid Build Coastguard Worker /* extent header of the current node we'll split */
1005*6a54128fSAndroid Build Coastguard Worker eh = (struct ext3_extent_header *)path->buf;
1006*6a54128fSAndroid Build Coastguard Worker
1007*6a54128fSAndroid Build Coastguard Worker /* splitting root level means moving them all out */
1008*6a54128fSAndroid Build Coastguard Worker if (handle->level == 0) {
1009*6a54128fSAndroid Build Coastguard Worker new_root = 1;
1010*6a54128fSAndroid Build Coastguard Worker tocopy = ext2fs_le16_to_cpu(eh->eh_entries);
1011*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_get_memzero((handle->max_paths + 1) *
1012*6a54128fSAndroid Build Coastguard Worker sizeof(struct extent_path),
1013*6a54128fSAndroid Build Coastguard Worker &newpath);
1014*6a54128fSAndroid Build Coastguard Worker if (retval)
1015*6a54128fSAndroid Build Coastguard Worker goto done;
1016*6a54128fSAndroid Build Coastguard Worker } else {
1017*6a54128fSAndroid Build Coastguard Worker if (no_balance)
1018*6a54128fSAndroid Build Coastguard Worker tocopy = 1;
1019*6a54128fSAndroid Build Coastguard Worker else
1020*6a54128fSAndroid Build Coastguard Worker tocopy = ext2fs_le16_to_cpu(eh->eh_entries) / 2;
1021*6a54128fSAndroid Build Coastguard Worker }
1022*6a54128fSAndroid Build Coastguard Worker
1023*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
1024*6a54128fSAndroid Build Coastguard Worker printf("will copy out %d of %d entries at level %d\n",
1025*6a54128fSAndroid Build Coastguard Worker tocopy, ext2fs_le16_to_cpu(eh->eh_entries),
1026*6a54128fSAndroid Build Coastguard Worker handle->level);
1027*6a54128fSAndroid Build Coastguard Worker #endif
1028*6a54128fSAndroid Build Coastguard Worker
1029*6a54128fSAndroid Build Coastguard Worker if (!tocopy && !no_balance) {
1030*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
1031*6a54128fSAndroid Build Coastguard Worker printf("Nothing to copy to new block!\n");
1032*6a54128fSAndroid Build Coastguard Worker #endif
1033*6a54128fSAndroid Build Coastguard Worker retval = EXT2_ET_CANT_SPLIT_EXTENT;
1034*6a54128fSAndroid Build Coastguard Worker goto done;
1035*6a54128fSAndroid Build Coastguard Worker }
1036*6a54128fSAndroid Build Coastguard Worker
1037*6a54128fSAndroid Build Coastguard Worker /* first we need a new block, or can do nothing. */
1038*6a54128fSAndroid Build Coastguard Worker block_buf = malloc(handle->fs->blocksize);
1039*6a54128fSAndroid Build Coastguard Worker if (!block_buf) {
1040*6a54128fSAndroid Build Coastguard Worker retval = ENOMEM;
1041*6a54128fSAndroid Build Coastguard Worker goto done;
1042*6a54128fSAndroid Build Coastguard Worker }
1043*6a54128fSAndroid Build Coastguard Worker
1044*6a54128fSAndroid Build Coastguard Worker if (!goal_blk)
1045*6a54128fSAndroid Build Coastguard Worker goal_blk = ext2fs_find_inode_goal(handle->fs, handle->ino,
1046*6a54128fSAndroid Build Coastguard Worker handle->inode, 0);
1047*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_alloc_block2(handle->fs, goal_blk, block_buf,
1048*6a54128fSAndroid Build Coastguard Worker &new_node_pblk);
1049*6a54128fSAndroid Build Coastguard Worker if (retval)
1050*6a54128fSAndroid Build Coastguard Worker goto done;
1051*6a54128fSAndroid Build Coastguard Worker
1052*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
1053*6a54128fSAndroid Build Coastguard Worker printf("will copy to new node at block %lu\n",
1054*6a54128fSAndroid Build Coastguard Worker (unsigned long) new_node_pblk);
1055*6a54128fSAndroid Build Coastguard Worker #endif
1056*6a54128fSAndroid Build Coastguard Worker
1057*6a54128fSAndroid Build Coastguard Worker /* Copy data into new block buffer */
1058*6a54128fSAndroid Build Coastguard Worker /* First the header for the new block... */
1059*6a54128fSAndroid Build Coastguard Worker neweh = (struct ext3_extent_header *) block_buf;
1060*6a54128fSAndroid Build Coastguard Worker memcpy(neweh, eh, sizeof(struct ext3_extent_header));
1061*6a54128fSAndroid Build Coastguard Worker neweh->eh_entries = ext2fs_cpu_to_le16(tocopy);
1062*6a54128fSAndroid Build Coastguard Worker neweh->eh_max = ext2fs_cpu_to_le16((handle->fs->blocksize -
1063*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext3_extent_header)) /
1064*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext3_extent));
1065*6a54128fSAndroid Build Coastguard Worker
1066*6a54128fSAndroid Build Coastguard Worker /* then the entries for the new block... */
1067*6a54128fSAndroid Build Coastguard Worker memcpy(EXT_FIRST_INDEX(neweh),
1068*6a54128fSAndroid Build Coastguard Worker EXT_FIRST_INDEX(eh) +
1069*6a54128fSAndroid Build Coastguard Worker (ext2fs_le16_to_cpu(eh->eh_entries) - tocopy),
1070*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext3_extent_idx) * tocopy);
1071*6a54128fSAndroid Build Coastguard Worker
1072*6a54128fSAndroid Build Coastguard Worker new_node_start = ext2fs_le32_to_cpu(EXT_FIRST_INDEX(neweh)->ei_block);
1073*6a54128fSAndroid Build Coastguard Worker
1074*6a54128fSAndroid Build Coastguard Worker /* then update the checksum */
1075*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_block_csum_set(handle->fs, handle->ino, neweh);
1076*6a54128fSAndroid Build Coastguard Worker if (retval)
1077*6a54128fSAndroid Build Coastguard Worker goto done;
1078*6a54128fSAndroid Build Coastguard Worker
1079*6a54128fSAndroid Build Coastguard Worker /* ...and write the new node block out to disk. */
1080*6a54128fSAndroid Build Coastguard Worker retval = io_channel_write_blk64(handle->fs->io, new_node_pblk, 1,
1081*6a54128fSAndroid Build Coastguard Worker block_buf);
1082*6a54128fSAndroid Build Coastguard Worker
1083*6a54128fSAndroid Build Coastguard Worker if (retval)
1084*6a54128fSAndroid Build Coastguard Worker goto done;
1085*6a54128fSAndroid Build Coastguard Worker
1086*6a54128fSAndroid Build Coastguard Worker /* OK! we've created the new node; now adjust the tree */
1087*6a54128fSAndroid Build Coastguard Worker
1088*6a54128fSAndroid Build Coastguard Worker /* current path now has fewer active entries, we copied some out */
1089*6a54128fSAndroid Build Coastguard Worker if (handle->level == 0) {
1090*6a54128fSAndroid Build Coastguard Worker memcpy(newpath, path,
1091*6a54128fSAndroid Build Coastguard Worker sizeof(struct extent_path) * handle->max_paths);
1092*6a54128fSAndroid Build Coastguard Worker handle->path = newpath;
1093*6a54128fSAndroid Build Coastguard Worker newpath = path;
1094*6a54128fSAndroid Build Coastguard Worker path = handle->path;
1095*6a54128fSAndroid Build Coastguard Worker path->entries = 1;
1096*6a54128fSAndroid Build Coastguard Worker path->left = path->max_entries - 1;
1097*6a54128fSAndroid Build Coastguard Worker handle->max_depth++;
1098*6a54128fSAndroid Build Coastguard Worker handle->max_paths++;
1099*6a54128fSAndroid Build Coastguard Worker eh->eh_depth = ext2fs_cpu_to_le16(handle->max_depth);
1100*6a54128fSAndroid Build Coastguard Worker } else {
1101*6a54128fSAndroid Build Coastguard Worker path->entries -= tocopy;
1102*6a54128fSAndroid Build Coastguard Worker path->left -= tocopy;
1103*6a54128fSAndroid Build Coastguard Worker }
1104*6a54128fSAndroid Build Coastguard Worker
1105*6a54128fSAndroid Build Coastguard Worker eh->eh_entries = ext2fs_cpu_to_le16(path->entries);
1106*6a54128fSAndroid Build Coastguard Worker /* this writes out the node, incl. the modified header */
1107*6a54128fSAndroid Build Coastguard Worker retval = update_path(handle);
1108*6a54128fSAndroid Build Coastguard Worker if (retval)
1109*6a54128fSAndroid Build Coastguard Worker goto done;
1110*6a54128fSAndroid Build Coastguard Worker
1111*6a54128fSAndroid Build Coastguard Worker /* now go up and insert/replace index for new node we created */
1112*6a54128fSAndroid Build Coastguard Worker if (new_root) {
1113*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle, EXT2_EXTENT_FIRST_SIB, &extent);
1114*6a54128fSAndroid Build Coastguard Worker if (retval)
1115*6a54128fSAndroid Build Coastguard Worker goto done;
1116*6a54128fSAndroid Build Coastguard Worker
1117*6a54128fSAndroid Build Coastguard Worker extent.e_lblk = new_node_start;
1118*6a54128fSAndroid Build Coastguard Worker extent.e_pblk = new_node_pblk;
1119*6a54128fSAndroid Build Coastguard Worker extent.e_len = handle->path[0].end_blk - extent.e_lblk;
1120*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_replace(handle, 0, &extent);
1121*6a54128fSAndroid Build Coastguard Worker if (retval)
1122*6a54128fSAndroid Build Coastguard Worker goto done;
1123*6a54128fSAndroid Build Coastguard Worker } else {
1124*6a54128fSAndroid Build Coastguard Worker __u32 new_node_length;
1125*6a54128fSAndroid Build Coastguard Worker
1126*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle, EXT2_EXTENT_UP, &extent);
1127*6a54128fSAndroid Build Coastguard Worker /* will insert after this one; it's length is shorter now */
1128*6a54128fSAndroid Build Coastguard Worker new_node_length = new_node_start - extent.e_lblk;
1129*6a54128fSAndroid Build Coastguard Worker extent.e_len -= new_node_length;
1130*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_replace(handle, 0, &extent);
1131*6a54128fSAndroid Build Coastguard Worker if (retval)
1132*6a54128fSAndroid Build Coastguard Worker goto done;
1133*6a54128fSAndroid Build Coastguard Worker
1134*6a54128fSAndroid Build Coastguard Worker /* now set up the new extent and insert it */
1135*6a54128fSAndroid Build Coastguard Worker extent.e_lblk = new_node_start;
1136*6a54128fSAndroid Build Coastguard Worker extent.e_pblk = new_node_pblk;
1137*6a54128fSAndroid Build Coastguard Worker extent.e_len = new_node_length;
1138*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_insert(handle, EXT2_EXTENT_INSERT_AFTER, &extent);
1139*6a54128fSAndroid Build Coastguard Worker if (retval)
1140*6a54128fSAndroid Build Coastguard Worker goto done;
1141*6a54128fSAndroid Build Coastguard Worker }
1142*6a54128fSAndroid Build Coastguard Worker
1143*6a54128fSAndroid Build Coastguard Worker /* get handle back to our original position */
1144*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_goto2(handle, orig_height, orig_lblk);
1145*6a54128fSAndroid Build Coastguard Worker if (retval)
1146*6a54128fSAndroid Build Coastguard Worker goto done;
1147*6a54128fSAndroid Build Coastguard Worker
1148*6a54128fSAndroid Build Coastguard Worker /* new node hooked in, so update inode block count (do this here?) */
1149*6a54128fSAndroid Build Coastguard Worker ext2fs_iblk_add_blocks(handle->fs, handle->inode, 1);
1150*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_write_inode(handle->fs, handle->ino,
1151*6a54128fSAndroid Build Coastguard Worker handle->inode);
1152*6a54128fSAndroid Build Coastguard Worker if (retval)
1153*6a54128fSAndroid Build Coastguard Worker goto done;
1154*6a54128fSAndroid Build Coastguard Worker
1155*6a54128fSAndroid Build Coastguard Worker done:
1156*6a54128fSAndroid Build Coastguard Worker if (newpath)
1157*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&newpath);
1158*6a54128fSAndroid Build Coastguard Worker free(block_buf);
1159*6a54128fSAndroid Build Coastguard Worker
1160*6a54128fSAndroid Build Coastguard Worker return retval;
1161*6a54128fSAndroid Build Coastguard Worker }
1162*6a54128fSAndroid Build Coastguard Worker
ext2fs_extent_node_split(ext2_extent_handle_t handle)1163*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_extent_node_split(ext2_extent_handle_t handle)
1164*6a54128fSAndroid Build Coastguard Worker {
1165*6a54128fSAndroid Build Coastguard Worker return extent_node_split(handle, 0);
1166*6a54128fSAndroid Build Coastguard Worker }
1167*6a54128fSAndroid Build Coastguard Worker
ext2fs_extent_insert(ext2_extent_handle_t handle,int flags,struct ext2fs_extent * extent)1168*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_extent_insert(ext2_extent_handle_t handle, int flags,
1169*6a54128fSAndroid Build Coastguard Worker struct ext2fs_extent *extent)
1170*6a54128fSAndroid Build Coastguard Worker {
1171*6a54128fSAndroid Build Coastguard Worker struct extent_path *path;
1172*6a54128fSAndroid Build Coastguard Worker struct ext3_extent_idx *ix;
1173*6a54128fSAndroid Build Coastguard Worker struct ext3_extent_header *eh;
1174*6a54128fSAndroid Build Coastguard Worker errcode_t retval;
1175*6a54128fSAndroid Build Coastguard Worker
1176*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EXTENT_HANDLE);
1177*6a54128fSAndroid Build Coastguard Worker
1178*6a54128fSAndroid Build Coastguard Worker if (!(handle->fs->flags & EXT2_FLAG_RW))
1179*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_RO_FILSYS;
1180*6a54128fSAndroid Build Coastguard Worker
1181*6a54128fSAndroid Build Coastguard Worker if (!handle->path)
1182*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_CURRENT_NODE;
1183*6a54128fSAndroid Build Coastguard Worker
1184*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
1185*6a54128fSAndroid Build Coastguard Worker printf("extent insert: %u ", handle->ino);
1186*6a54128fSAndroid Build Coastguard Worker dbg_print_extent(0, extent);
1187*6a54128fSAndroid Build Coastguard Worker #endif
1188*6a54128fSAndroid Build Coastguard Worker
1189*6a54128fSAndroid Build Coastguard Worker path = handle->path + handle->level;
1190*6a54128fSAndroid Build Coastguard Worker
1191*6a54128fSAndroid Build Coastguard Worker if (path->entries >= path->max_entries) {
1192*6a54128fSAndroid Build Coastguard Worker if (flags & EXT2_EXTENT_INSERT_NOSPLIT) {
1193*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_CANT_INSERT_EXTENT;
1194*6a54128fSAndroid Build Coastguard Worker } else {
1195*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
1196*6a54128fSAndroid Build Coastguard Worker printf("node full (level %d) - splitting\n",
1197*6a54128fSAndroid Build Coastguard Worker handle->level);
1198*6a54128fSAndroid Build Coastguard Worker #endif
1199*6a54128fSAndroid Build Coastguard Worker retval = extent_node_split(handle, 1);
1200*6a54128fSAndroid Build Coastguard Worker if (retval)
1201*6a54128fSAndroid Build Coastguard Worker return retval;
1202*6a54128fSAndroid Build Coastguard Worker path = handle->path + handle->level;
1203*6a54128fSAndroid Build Coastguard Worker }
1204*6a54128fSAndroid Build Coastguard Worker }
1205*6a54128fSAndroid Build Coastguard Worker
1206*6a54128fSAndroid Build Coastguard Worker eh = (struct ext3_extent_header *) path->buf;
1207*6a54128fSAndroid Build Coastguard Worker if (path->curr) {
1208*6a54128fSAndroid Build Coastguard Worker ix = path->curr;
1209*6a54128fSAndroid Build Coastguard Worker if (flags & EXT2_EXTENT_INSERT_AFTER) {
1210*6a54128fSAndroid Build Coastguard Worker ix++;
1211*6a54128fSAndroid Build Coastguard Worker path->left--;
1212*6a54128fSAndroid Build Coastguard Worker }
1213*6a54128fSAndroid Build Coastguard Worker } else {
1214*6a54128fSAndroid Build Coastguard Worker ix = EXT_FIRST_INDEX(eh);
1215*6a54128fSAndroid Build Coastguard Worker path->left = -1;
1216*6a54128fSAndroid Build Coastguard Worker }
1217*6a54128fSAndroid Build Coastguard Worker
1218*6a54128fSAndroid Build Coastguard Worker path->curr = ix;
1219*6a54128fSAndroid Build Coastguard Worker
1220*6a54128fSAndroid Build Coastguard Worker if (path->left >= 0)
1221*6a54128fSAndroid Build Coastguard Worker memmove(ix + 1, ix,
1222*6a54128fSAndroid Build Coastguard Worker (path->left+1) * sizeof(struct ext3_extent_idx));
1223*6a54128fSAndroid Build Coastguard Worker path->left++;
1224*6a54128fSAndroid Build Coastguard Worker path->entries++;
1225*6a54128fSAndroid Build Coastguard Worker
1226*6a54128fSAndroid Build Coastguard Worker eh = (struct ext3_extent_header *) path->buf;
1227*6a54128fSAndroid Build Coastguard Worker eh->eh_entries = ext2fs_cpu_to_le16(path->entries);
1228*6a54128fSAndroid Build Coastguard Worker
1229*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_replace(handle, 0, extent);
1230*6a54128fSAndroid Build Coastguard Worker if (retval)
1231*6a54128fSAndroid Build Coastguard Worker goto errout;
1232*6a54128fSAndroid Build Coastguard Worker
1233*6a54128fSAndroid Build Coastguard Worker retval = update_path(handle);
1234*6a54128fSAndroid Build Coastguard Worker if (retval)
1235*6a54128fSAndroid Build Coastguard Worker goto errout;
1236*6a54128fSAndroid Build Coastguard Worker
1237*6a54128fSAndroid Build Coastguard Worker return 0;
1238*6a54128fSAndroid Build Coastguard Worker
1239*6a54128fSAndroid Build Coastguard Worker errout:
1240*6a54128fSAndroid Build Coastguard Worker ext2fs_extent_delete(handle, 0);
1241*6a54128fSAndroid Build Coastguard Worker return retval;
1242*6a54128fSAndroid Build Coastguard Worker }
1243*6a54128fSAndroid Build Coastguard Worker
1244*6a54128fSAndroid Build Coastguard Worker /*
1245*6a54128fSAndroid Build Coastguard Worker * Sets the physical block for a logical file block in the extent tree.
1246*6a54128fSAndroid Build Coastguard Worker *
1247*6a54128fSAndroid Build Coastguard Worker * May: map unmapped, unmap mapped, or remap mapped blocks.
1248*6a54128fSAndroid Build Coastguard Worker *
1249*6a54128fSAndroid Build Coastguard Worker * Mapping an unmapped block adds a single-block extent.
1250*6a54128fSAndroid Build Coastguard Worker *
1251*6a54128fSAndroid Build Coastguard Worker * Unmapping first or last block modifies extent in-place
1252*6a54128fSAndroid Build Coastguard Worker * - But may need to fix parent's starts too in first-block case
1253*6a54128fSAndroid Build Coastguard Worker *
1254*6a54128fSAndroid Build Coastguard Worker * Mapping any unmapped block requires adding a (single-block) extent
1255*6a54128fSAndroid Build Coastguard Worker * and inserting into proper point in tree.
1256*6a54128fSAndroid Build Coastguard Worker *
1257*6a54128fSAndroid Build Coastguard Worker * Modifying (unmapping or remapping) a block in the middle
1258*6a54128fSAndroid Build Coastguard Worker * of an extent requires splitting the extent.
1259*6a54128fSAndroid Build Coastguard Worker * - Remapping case requires new single-block extent.
1260*6a54128fSAndroid Build Coastguard Worker *
1261*6a54128fSAndroid Build Coastguard Worker * Remapping first or last block adds an extent.
1262*6a54128fSAndroid Build Coastguard Worker *
1263*6a54128fSAndroid Build Coastguard Worker * We really need extent adding to be smart about merging.
1264*6a54128fSAndroid Build Coastguard Worker */
1265*6a54128fSAndroid Build Coastguard Worker
ext2fs_extent_set_bmap(ext2_extent_handle_t handle,blk64_t logical,blk64_t physical,int flags)1266*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_extent_set_bmap(ext2_extent_handle_t handle,
1267*6a54128fSAndroid Build Coastguard Worker blk64_t logical, blk64_t physical, int flags)
1268*6a54128fSAndroid Build Coastguard Worker {
1269*6a54128fSAndroid Build Coastguard Worker errcode_t ec, retval = 0;
1270*6a54128fSAndroid Build Coastguard Worker int mapped = 1; /* logical is mapped? */
1271*6a54128fSAndroid Build Coastguard Worker int orig_height;
1272*6a54128fSAndroid Build Coastguard Worker int extent_uninit = 0;
1273*6a54128fSAndroid Build Coastguard Worker int prev_uninit = 0;
1274*6a54128fSAndroid Build Coastguard Worker int next_uninit = 0;
1275*6a54128fSAndroid Build Coastguard Worker int new_uninit = 0;
1276*6a54128fSAndroid Build Coastguard Worker int max_len = EXT_INIT_MAX_LEN;
1277*6a54128fSAndroid Build Coastguard Worker int has_prev, has_next;
1278*6a54128fSAndroid Build Coastguard Worker blk64_t orig_lblk;
1279*6a54128fSAndroid Build Coastguard Worker struct extent_path *path;
1280*6a54128fSAndroid Build Coastguard Worker struct ext2fs_extent extent, next_extent, prev_extent;
1281*6a54128fSAndroid Build Coastguard Worker struct ext2fs_extent newextent;
1282*6a54128fSAndroid Build Coastguard Worker struct ext2_extent_info info;
1283*6a54128fSAndroid Build Coastguard Worker
1284*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EXTENT_HANDLE);
1285*6a54128fSAndroid Build Coastguard Worker
1286*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
1287*6a54128fSAndroid Build Coastguard Worker printf("set_bmap ino %u log %lld phys %lld flags %d\n",
1288*6a54128fSAndroid Build Coastguard Worker handle->ino, logical, physical, flags);
1289*6a54128fSAndroid Build Coastguard Worker #endif
1290*6a54128fSAndroid Build Coastguard Worker
1291*6a54128fSAndroid Build Coastguard Worker if (!(handle->fs->flags & EXT2_FLAG_RW))
1292*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_RO_FILSYS;
1293*6a54128fSAndroid Build Coastguard Worker
1294*6a54128fSAndroid Build Coastguard Worker if (!handle->path)
1295*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_CURRENT_NODE;
1296*6a54128fSAndroid Build Coastguard Worker
1297*6a54128fSAndroid Build Coastguard Worker path = handle->path + handle->level;
1298*6a54128fSAndroid Build Coastguard Worker
1299*6a54128fSAndroid Build Coastguard Worker if (flags & EXT2_EXTENT_SET_BMAP_UNINIT) {
1300*6a54128fSAndroid Build Coastguard Worker new_uninit = 1;
1301*6a54128fSAndroid Build Coastguard Worker max_len = EXT_UNINIT_MAX_LEN;
1302*6a54128fSAndroid Build Coastguard Worker }
1303*6a54128fSAndroid Build Coastguard Worker
1304*6a54128fSAndroid Build Coastguard Worker /* if (re)mapping, set up new extent to insert */
1305*6a54128fSAndroid Build Coastguard Worker if (physical) {
1306*6a54128fSAndroid Build Coastguard Worker newextent.e_len = 1;
1307*6a54128fSAndroid Build Coastguard Worker newextent.e_pblk = physical;
1308*6a54128fSAndroid Build Coastguard Worker newextent.e_lblk = logical;
1309*6a54128fSAndroid Build Coastguard Worker newextent.e_flags = EXT2_EXTENT_FLAGS_LEAF;
1310*6a54128fSAndroid Build Coastguard Worker if (new_uninit)
1311*6a54128fSAndroid Build Coastguard Worker newextent.e_flags |= EXT2_EXTENT_FLAGS_UNINIT;
1312*6a54128fSAndroid Build Coastguard Worker }
1313*6a54128fSAndroid Build Coastguard Worker
1314*6a54128fSAndroid Build Coastguard Worker /* special case if the extent tree is completely empty */
1315*6a54128fSAndroid Build Coastguard Worker if ((handle->max_depth == 0) && (path->entries == 0)) {
1316*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_insert(handle, 0, &newextent);
1317*6a54128fSAndroid Build Coastguard Worker return retval;
1318*6a54128fSAndroid Build Coastguard Worker }
1319*6a54128fSAndroid Build Coastguard Worker
1320*6a54128fSAndroid Build Coastguard Worker /* save our original location in the extent tree */
1321*6a54128fSAndroid Build Coastguard Worker if ((retval = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT,
1322*6a54128fSAndroid Build Coastguard Worker &extent))) {
1323*6a54128fSAndroid Build Coastguard Worker if (retval != EXT2_ET_NO_CURRENT_NODE)
1324*6a54128fSAndroid Build Coastguard Worker return retval;
1325*6a54128fSAndroid Build Coastguard Worker memset(&extent, 0, sizeof(extent));
1326*6a54128fSAndroid Build Coastguard Worker }
1327*6a54128fSAndroid Build Coastguard Worker if ((retval = ext2fs_extent_get_info(handle, &info)))
1328*6a54128fSAndroid Build Coastguard Worker return retval;
1329*6a54128fSAndroid Build Coastguard Worker orig_height = info.max_depth - info.curr_level;
1330*6a54128fSAndroid Build Coastguard Worker orig_lblk = extent.e_lblk;
1331*6a54128fSAndroid Build Coastguard Worker
1332*6a54128fSAndroid Build Coastguard Worker /* go to the logical spot we want to (re/un)map */
1333*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_goto(handle, logical);
1334*6a54128fSAndroid Build Coastguard Worker if (retval) {
1335*6a54128fSAndroid Build Coastguard Worker if (retval == EXT2_ET_EXTENT_NOT_FOUND) {
1336*6a54128fSAndroid Build Coastguard Worker retval = 0;
1337*6a54128fSAndroid Build Coastguard Worker mapped = 0;
1338*6a54128fSAndroid Build Coastguard Worker if (!physical) {
1339*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
1340*6a54128fSAndroid Build Coastguard Worker printf("block %llu already unmapped\n",
1341*6a54128fSAndroid Build Coastguard Worker logical);
1342*6a54128fSAndroid Build Coastguard Worker #endif
1343*6a54128fSAndroid Build Coastguard Worker goto done;
1344*6a54128fSAndroid Build Coastguard Worker }
1345*6a54128fSAndroid Build Coastguard Worker } else
1346*6a54128fSAndroid Build Coastguard Worker goto done;
1347*6a54128fSAndroid Build Coastguard Worker }
1348*6a54128fSAndroid Build Coastguard Worker
1349*6a54128fSAndroid Build Coastguard Worker /*
1350*6a54128fSAndroid Build Coastguard Worker * This may be the extent *before* the requested logical,
1351*6a54128fSAndroid Build Coastguard Worker * if it's currently unmapped.
1352*6a54128fSAndroid Build Coastguard Worker *
1353*6a54128fSAndroid Build Coastguard Worker * Get the previous and next leaf extents, if they are present.
1354*6a54128fSAndroid Build Coastguard Worker */
1355*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT, &extent);
1356*6a54128fSAndroid Build Coastguard Worker if (retval)
1357*6a54128fSAndroid Build Coastguard Worker goto done;
1358*6a54128fSAndroid Build Coastguard Worker if (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT)
1359*6a54128fSAndroid Build Coastguard Worker extent_uninit = 1;
1360*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle, EXT2_EXTENT_NEXT_LEAF, &next_extent);
1361*6a54128fSAndroid Build Coastguard Worker if (retval) {
1362*6a54128fSAndroid Build Coastguard Worker has_next = 0;
1363*6a54128fSAndroid Build Coastguard Worker if (retval != EXT2_ET_EXTENT_NO_NEXT)
1364*6a54128fSAndroid Build Coastguard Worker goto done;
1365*6a54128fSAndroid Build Coastguard Worker } else {
1366*6a54128fSAndroid Build Coastguard Worker dbg_print_extent("set_bmap: next_extent",
1367*6a54128fSAndroid Build Coastguard Worker &next_extent);
1368*6a54128fSAndroid Build Coastguard Worker has_next = 1;
1369*6a54128fSAndroid Build Coastguard Worker if (next_extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT)
1370*6a54128fSAndroid Build Coastguard Worker next_uninit = 1;
1371*6a54128fSAndroid Build Coastguard Worker }
1372*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_goto(handle, logical);
1373*6a54128fSAndroid Build Coastguard Worker if (retval && retval != EXT2_ET_EXTENT_NOT_FOUND)
1374*6a54128fSAndroid Build Coastguard Worker goto done;
1375*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle, EXT2_EXTENT_PREV_LEAF, &prev_extent);
1376*6a54128fSAndroid Build Coastguard Worker if (retval) {
1377*6a54128fSAndroid Build Coastguard Worker has_prev = 0;
1378*6a54128fSAndroid Build Coastguard Worker if (retval != EXT2_ET_EXTENT_NO_PREV)
1379*6a54128fSAndroid Build Coastguard Worker goto done;
1380*6a54128fSAndroid Build Coastguard Worker } else {
1381*6a54128fSAndroid Build Coastguard Worker has_prev = 1;
1382*6a54128fSAndroid Build Coastguard Worker dbg_print_extent("set_bmap: prev_extent",
1383*6a54128fSAndroid Build Coastguard Worker &prev_extent);
1384*6a54128fSAndroid Build Coastguard Worker if (prev_extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT)
1385*6a54128fSAndroid Build Coastguard Worker prev_uninit = 1;
1386*6a54128fSAndroid Build Coastguard Worker }
1387*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_goto(handle, logical);
1388*6a54128fSAndroid Build Coastguard Worker if (retval && retval != EXT2_ET_EXTENT_NOT_FOUND)
1389*6a54128fSAndroid Build Coastguard Worker goto done;
1390*6a54128fSAndroid Build Coastguard Worker
1391*6a54128fSAndroid Build Coastguard Worker /* check if already pointing to the requested physical */
1392*6a54128fSAndroid Build Coastguard Worker if (mapped && (new_uninit == extent_uninit) &&
1393*6a54128fSAndroid Build Coastguard Worker (extent.e_pblk + (logical - extent.e_lblk) == physical)) {
1394*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
1395*6a54128fSAndroid Build Coastguard Worker printf("physical block (at %llu) unchanged\n", logical);
1396*6a54128fSAndroid Build Coastguard Worker #endif
1397*6a54128fSAndroid Build Coastguard Worker goto done;
1398*6a54128fSAndroid Build Coastguard Worker }
1399*6a54128fSAndroid Build Coastguard Worker
1400*6a54128fSAndroid Build Coastguard Worker if (!mapped) {
1401*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
1402*6a54128fSAndroid Build Coastguard Worker printf("mapping unmapped logical block %llu\n", logical);
1403*6a54128fSAndroid Build Coastguard Worker #endif
1404*6a54128fSAndroid Build Coastguard Worker if ((logical == extent.e_lblk + extent.e_len) &&
1405*6a54128fSAndroid Build Coastguard Worker (physical == extent.e_pblk + extent.e_len) &&
1406*6a54128fSAndroid Build Coastguard Worker (new_uninit == extent_uninit) &&
1407*6a54128fSAndroid Build Coastguard Worker ((int) extent.e_len < max_len-1)) {
1408*6a54128fSAndroid Build Coastguard Worker extent.e_len++;
1409*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_replace(handle, 0, &extent);
1410*6a54128fSAndroid Build Coastguard Worker } else if ((logical == extent.e_lblk - 1) &&
1411*6a54128fSAndroid Build Coastguard Worker (physical == extent.e_pblk - 1) &&
1412*6a54128fSAndroid Build Coastguard Worker (new_uninit == extent_uninit) &&
1413*6a54128fSAndroid Build Coastguard Worker ((int) extent.e_len < max_len - 1)) {
1414*6a54128fSAndroid Build Coastguard Worker extent.e_len++;
1415*6a54128fSAndroid Build Coastguard Worker extent.e_lblk--;
1416*6a54128fSAndroid Build Coastguard Worker extent.e_pblk--;
1417*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_replace(handle, 0, &extent);
1418*6a54128fSAndroid Build Coastguard Worker } else if (has_next &&
1419*6a54128fSAndroid Build Coastguard Worker (logical == next_extent.e_lblk - 1) &&
1420*6a54128fSAndroid Build Coastguard Worker (physical == next_extent.e_pblk - 1) &&
1421*6a54128fSAndroid Build Coastguard Worker (new_uninit == next_uninit) &&
1422*6a54128fSAndroid Build Coastguard Worker ((int) next_extent.e_len < max_len - 1)) {
1423*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle,
1424*6a54128fSAndroid Build Coastguard Worker EXT2_EXTENT_NEXT_LEAF,
1425*6a54128fSAndroid Build Coastguard Worker &next_extent);
1426*6a54128fSAndroid Build Coastguard Worker if (retval)
1427*6a54128fSAndroid Build Coastguard Worker goto done;
1428*6a54128fSAndroid Build Coastguard Worker next_extent.e_len++;
1429*6a54128fSAndroid Build Coastguard Worker next_extent.e_lblk--;
1430*6a54128fSAndroid Build Coastguard Worker next_extent.e_pblk--;
1431*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_replace(handle, 0, &next_extent);
1432*6a54128fSAndroid Build Coastguard Worker } else if (logical < extent.e_lblk)
1433*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_insert(handle, 0, &newextent);
1434*6a54128fSAndroid Build Coastguard Worker else
1435*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_insert(handle,
1436*6a54128fSAndroid Build Coastguard Worker EXT2_EXTENT_INSERT_AFTER, &newextent);
1437*6a54128fSAndroid Build Coastguard Worker if (retval)
1438*6a54128fSAndroid Build Coastguard Worker goto done;
1439*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_fix_parents(handle);
1440*6a54128fSAndroid Build Coastguard Worker if (retval)
1441*6a54128fSAndroid Build Coastguard Worker goto done;
1442*6a54128fSAndroid Build Coastguard Worker } else if ((logical == extent.e_lblk) && (extent.e_len == 1)) {
1443*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
1444*6a54128fSAndroid Build Coastguard Worker printf("(re/un)mapping only block in extent\n");
1445*6a54128fSAndroid Build Coastguard Worker #endif
1446*6a54128fSAndroid Build Coastguard Worker if (physical) {
1447*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_replace(handle, 0, &newextent);
1448*6a54128fSAndroid Build Coastguard Worker } else {
1449*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_delete(handle, 0);
1450*6a54128fSAndroid Build Coastguard Worker if (retval)
1451*6a54128fSAndroid Build Coastguard Worker goto done;
1452*6a54128fSAndroid Build Coastguard Worker ec = ext2fs_extent_fix_parents(handle);
1453*6a54128fSAndroid Build Coastguard Worker if (ec != EXT2_ET_NO_CURRENT_NODE)
1454*6a54128fSAndroid Build Coastguard Worker retval = ec;
1455*6a54128fSAndroid Build Coastguard Worker }
1456*6a54128fSAndroid Build Coastguard Worker
1457*6a54128fSAndroid Build Coastguard Worker if (retval)
1458*6a54128fSAndroid Build Coastguard Worker goto done;
1459*6a54128fSAndroid Build Coastguard Worker } else if (logical == extent.e_lblk + extent.e_len - 1) {
1460*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
1461*6a54128fSAndroid Build Coastguard Worker printf("(re/un)mapping last block in extent\n");
1462*6a54128fSAndroid Build Coastguard Worker #endif
1463*6a54128fSAndroid Build Coastguard Worker if (physical) {
1464*6a54128fSAndroid Build Coastguard Worker if (has_next &&
1465*6a54128fSAndroid Build Coastguard Worker (logical == (next_extent.e_lblk - 1)) &&
1466*6a54128fSAndroid Build Coastguard Worker (physical == (next_extent.e_pblk - 1)) &&
1467*6a54128fSAndroid Build Coastguard Worker (new_uninit == next_uninit) &&
1468*6a54128fSAndroid Build Coastguard Worker ((int) next_extent.e_len < max_len - 1)) {
1469*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle,
1470*6a54128fSAndroid Build Coastguard Worker EXT2_EXTENT_NEXT_LEAF, &next_extent);
1471*6a54128fSAndroid Build Coastguard Worker if (retval)
1472*6a54128fSAndroid Build Coastguard Worker goto done;
1473*6a54128fSAndroid Build Coastguard Worker next_extent.e_len++;
1474*6a54128fSAndroid Build Coastguard Worker next_extent.e_lblk--;
1475*6a54128fSAndroid Build Coastguard Worker next_extent.e_pblk--;
1476*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_replace(handle, 0,
1477*6a54128fSAndroid Build Coastguard Worker &next_extent);
1478*6a54128fSAndroid Build Coastguard Worker if (retval)
1479*6a54128fSAndroid Build Coastguard Worker goto done;
1480*6a54128fSAndroid Build Coastguard Worker } else
1481*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_insert(handle,
1482*6a54128fSAndroid Build Coastguard Worker EXT2_EXTENT_INSERT_AFTER, &newextent);
1483*6a54128fSAndroid Build Coastguard Worker if (retval)
1484*6a54128fSAndroid Build Coastguard Worker goto done;
1485*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_fix_parents(handle);
1486*6a54128fSAndroid Build Coastguard Worker if (retval)
1487*6a54128fSAndroid Build Coastguard Worker goto done;
1488*6a54128fSAndroid Build Coastguard Worker /*
1489*6a54128fSAndroid Build Coastguard Worker * Now pointing at inserted extent; move back to prev.
1490*6a54128fSAndroid Build Coastguard Worker *
1491*6a54128fSAndroid Build Coastguard Worker * We cannot use EXT2_EXTENT_PREV to go back; note the
1492*6a54128fSAndroid Build Coastguard Worker * subtlety in the comment for fix_parents().
1493*6a54128fSAndroid Build Coastguard Worker */
1494*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_goto(handle, logical);
1495*6a54128fSAndroid Build Coastguard Worker if (retval)
1496*6a54128fSAndroid Build Coastguard Worker goto done;
1497*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle,
1498*6a54128fSAndroid Build Coastguard Worker EXT2_EXTENT_CURRENT,
1499*6a54128fSAndroid Build Coastguard Worker &extent);
1500*6a54128fSAndroid Build Coastguard Worker if (retval)
1501*6a54128fSAndroid Build Coastguard Worker goto done;
1502*6a54128fSAndroid Build Coastguard Worker }
1503*6a54128fSAndroid Build Coastguard Worker extent.e_len--;
1504*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_replace(handle, 0, &extent);
1505*6a54128fSAndroid Build Coastguard Worker if (retval)
1506*6a54128fSAndroid Build Coastguard Worker goto done;
1507*6a54128fSAndroid Build Coastguard Worker } else if (logical == extent.e_lblk) {
1508*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
1509*6a54128fSAndroid Build Coastguard Worker printf("(re/un)mapping first block in extent\n");
1510*6a54128fSAndroid Build Coastguard Worker #endif
1511*6a54128fSAndroid Build Coastguard Worker if (physical) {
1512*6a54128fSAndroid Build Coastguard Worker if (has_prev &&
1513*6a54128fSAndroid Build Coastguard Worker (logical == (prev_extent.e_lblk +
1514*6a54128fSAndroid Build Coastguard Worker prev_extent.e_len)) &&
1515*6a54128fSAndroid Build Coastguard Worker (physical == (prev_extent.e_pblk +
1516*6a54128fSAndroid Build Coastguard Worker prev_extent.e_len)) &&
1517*6a54128fSAndroid Build Coastguard Worker (new_uninit == prev_uninit) &&
1518*6a54128fSAndroid Build Coastguard Worker ((int) prev_extent.e_len < max_len-1)) {
1519*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle,
1520*6a54128fSAndroid Build Coastguard Worker EXT2_EXTENT_PREV_LEAF, &prev_extent);
1521*6a54128fSAndroid Build Coastguard Worker if (retval)
1522*6a54128fSAndroid Build Coastguard Worker goto done;
1523*6a54128fSAndroid Build Coastguard Worker prev_extent.e_len++;
1524*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_replace(handle, 0,
1525*6a54128fSAndroid Build Coastguard Worker &prev_extent);
1526*6a54128fSAndroid Build Coastguard Worker } else
1527*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_insert(handle,
1528*6a54128fSAndroid Build Coastguard Worker 0, &newextent);
1529*6a54128fSAndroid Build Coastguard Worker if (retval)
1530*6a54128fSAndroid Build Coastguard Worker goto done;
1531*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_fix_parents(handle);
1532*6a54128fSAndroid Build Coastguard Worker if (retval)
1533*6a54128fSAndroid Build Coastguard Worker goto done;
1534*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle,
1535*6a54128fSAndroid Build Coastguard Worker EXT2_EXTENT_NEXT_LEAF,
1536*6a54128fSAndroid Build Coastguard Worker &extent);
1537*6a54128fSAndroid Build Coastguard Worker if (retval)
1538*6a54128fSAndroid Build Coastguard Worker goto done;
1539*6a54128fSAndroid Build Coastguard Worker }
1540*6a54128fSAndroid Build Coastguard Worker extent.e_pblk++;
1541*6a54128fSAndroid Build Coastguard Worker extent.e_lblk++;
1542*6a54128fSAndroid Build Coastguard Worker extent.e_len--;
1543*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_replace(handle, 0, &extent);
1544*6a54128fSAndroid Build Coastguard Worker if (retval)
1545*6a54128fSAndroid Build Coastguard Worker goto done;
1546*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_fix_parents(handle);
1547*6a54128fSAndroid Build Coastguard Worker if (retval)
1548*6a54128fSAndroid Build Coastguard Worker goto done;
1549*6a54128fSAndroid Build Coastguard Worker } else {
1550*6a54128fSAndroid Build Coastguard Worker __u32 save_length;
1551*6a54128fSAndroid Build Coastguard Worker blk64_t save_lblk;
1552*6a54128fSAndroid Build Coastguard Worker struct ext2fs_extent save_extent;
1553*6a54128fSAndroid Build Coastguard Worker errcode_t r2;
1554*6a54128fSAndroid Build Coastguard Worker
1555*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
1556*6a54128fSAndroid Build Coastguard Worker printf("(re/un)mapping in middle of extent\n");
1557*6a54128fSAndroid Build Coastguard Worker #endif
1558*6a54128fSAndroid Build Coastguard Worker /* need to split this extent; later */
1559*6a54128fSAndroid Build Coastguard Worker save_lblk = extent.e_lblk;
1560*6a54128fSAndroid Build Coastguard Worker save_length = extent.e_len;
1561*6a54128fSAndroid Build Coastguard Worker save_extent = extent;
1562*6a54128fSAndroid Build Coastguard Worker
1563*6a54128fSAndroid Build Coastguard Worker /* shorten pre-split extent */
1564*6a54128fSAndroid Build Coastguard Worker extent.e_len = (logical - extent.e_lblk);
1565*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_replace(handle, 0, &extent);
1566*6a54128fSAndroid Build Coastguard Worker if (retval)
1567*6a54128fSAndroid Build Coastguard Worker goto done;
1568*6a54128fSAndroid Build Coastguard Worker /* insert our new extent, if any */
1569*6a54128fSAndroid Build Coastguard Worker if (physical) {
1570*6a54128fSAndroid Build Coastguard Worker /* insert new extent after current */
1571*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_insert(handle,
1572*6a54128fSAndroid Build Coastguard Worker EXT2_EXTENT_INSERT_AFTER, &newextent);
1573*6a54128fSAndroid Build Coastguard Worker if (retval) {
1574*6a54128fSAndroid Build Coastguard Worker r2 = ext2fs_extent_goto(handle, save_lblk);
1575*6a54128fSAndroid Build Coastguard Worker if (r2 == 0)
1576*6a54128fSAndroid Build Coastguard Worker (void)ext2fs_extent_replace(handle, 0,
1577*6a54128fSAndroid Build Coastguard Worker &save_extent);
1578*6a54128fSAndroid Build Coastguard Worker goto done;
1579*6a54128fSAndroid Build Coastguard Worker }
1580*6a54128fSAndroid Build Coastguard Worker }
1581*6a54128fSAndroid Build Coastguard Worker /* add post-split extent */
1582*6a54128fSAndroid Build Coastguard Worker extent.e_pblk += extent.e_len + 1;
1583*6a54128fSAndroid Build Coastguard Worker extent.e_lblk += extent.e_len + 1;
1584*6a54128fSAndroid Build Coastguard Worker extent.e_len = save_length - extent.e_len - 1;
1585*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_insert(handle,
1586*6a54128fSAndroid Build Coastguard Worker EXT2_EXTENT_INSERT_AFTER, &extent);
1587*6a54128fSAndroid Build Coastguard Worker if (retval) {
1588*6a54128fSAndroid Build Coastguard Worker if (physical) {
1589*6a54128fSAndroid Build Coastguard Worker r2 = ext2fs_extent_goto(handle,
1590*6a54128fSAndroid Build Coastguard Worker newextent.e_lblk);
1591*6a54128fSAndroid Build Coastguard Worker if (r2 == 0)
1592*6a54128fSAndroid Build Coastguard Worker (void)ext2fs_extent_delete(handle, 0);
1593*6a54128fSAndroid Build Coastguard Worker }
1594*6a54128fSAndroid Build Coastguard Worker r2 = ext2fs_extent_goto(handle, save_lblk);
1595*6a54128fSAndroid Build Coastguard Worker if (r2 == 0)
1596*6a54128fSAndroid Build Coastguard Worker (void)ext2fs_extent_replace(handle, 0,
1597*6a54128fSAndroid Build Coastguard Worker &save_extent);
1598*6a54128fSAndroid Build Coastguard Worker goto done;
1599*6a54128fSAndroid Build Coastguard Worker }
1600*6a54128fSAndroid Build Coastguard Worker }
1601*6a54128fSAndroid Build Coastguard Worker
1602*6a54128fSAndroid Build Coastguard Worker done:
1603*6a54128fSAndroid Build Coastguard Worker /* get handle back to its position */
1604*6a54128fSAndroid Build Coastguard Worker if (orig_height > handle->max_depth)
1605*6a54128fSAndroid Build Coastguard Worker orig_height = handle->max_depth; /* In case we shortened the tree */
1606*6a54128fSAndroid Build Coastguard Worker ext2fs_extent_goto2(handle, orig_height, orig_lblk);
1607*6a54128fSAndroid Build Coastguard Worker return retval;
1608*6a54128fSAndroid Build Coastguard Worker }
1609*6a54128fSAndroid Build Coastguard Worker
ext2fs_extent_delete(ext2_extent_handle_t handle,int flags)1610*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_extent_delete(ext2_extent_handle_t handle, int flags)
1611*6a54128fSAndroid Build Coastguard Worker {
1612*6a54128fSAndroid Build Coastguard Worker struct extent_path *path;
1613*6a54128fSAndroid Build Coastguard Worker char *cp;
1614*6a54128fSAndroid Build Coastguard Worker struct ext3_extent_header *eh;
1615*6a54128fSAndroid Build Coastguard Worker errcode_t retval = 0;
1616*6a54128fSAndroid Build Coastguard Worker
1617*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EXTENT_HANDLE);
1618*6a54128fSAndroid Build Coastguard Worker
1619*6a54128fSAndroid Build Coastguard Worker if (!(handle->fs->flags & EXT2_FLAG_RW))
1620*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_RO_FILSYS;
1621*6a54128fSAndroid Build Coastguard Worker
1622*6a54128fSAndroid Build Coastguard Worker if (!handle->path)
1623*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_CURRENT_NODE;
1624*6a54128fSAndroid Build Coastguard Worker
1625*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
1626*6a54128fSAndroid Build Coastguard Worker {
1627*6a54128fSAndroid Build Coastguard Worker struct ext2fs_extent extent;
1628*6a54128fSAndroid Build Coastguard Worker
1629*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT,
1630*6a54128fSAndroid Build Coastguard Worker &extent);
1631*6a54128fSAndroid Build Coastguard Worker if (retval == 0) {
1632*6a54128fSAndroid Build Coastguard Worker printf("extent delete %u ", handle->ino);
1633*6a54128fSAndroid Build Coastguard Worker dbg_print_extent(0, &extent);
1634*6a54128fSAndroid Build Coastguard Worker }
1635*6a54128fSAndroid Build Coastguard Worker }
1636*6a54128fSAndroid Build Coastguard Worker #endif
1637*6a54128fSAndroid Build Coastguard Worker
1638*6a54128fSAndroid Build Coastguard Worker path = handle->path + handle->level;
1639*6a54128fSAndroid Build Coastguard Worker if (!path->curr)
1640*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_NO_CURRENT_NODE;
1641*6a54128fSAndroid Build Coastguard Worker
1642*6a54128fSAndroid Build Coastguard Worker cp = path->curr;
1643*6a54128fSAndroid Build Coastguard Worker
1644*6a54128fSAndroid Build Coastguard Worker /* Sanity check before memmove() */
1645*6a54128fSAndroid Build Coastguard Worker if (path->left < 0)
1646*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_EXTENT_LEAF_BAD;
1647*6a54128fSAndroid Build Coastguard Worker
1648*6a54128fSAndroid Build Coastguard Worker if (path->left) {
1649*6a54128fSAndroid Build Coastguard Worker memmove(cp, cp + sizeof(struct ext3_extent_idx),
1650*6a54128fSAndroid Build Coastguard Worker path->left * sizeof(struct ext3_extent_idx));
1651*6a54128fSAndroid Build Coastguard Worker path->left--;
1652*6a54128fSAndroid Build Coastguard Worker } else {
1653*6a54128fSAndroid Build Coastguard Worker struct ext3_extent_idx *ix = path->curr;
1654*6a54128fSAndroid Build Coastguard Worker ix--;
1655*6a54128fSAndroid Build Coastguard Worker path->curr = ix;
1656*6a54128fSAndroid Build Coastguard Worker }
1657*6a54128fSAndroid Build Coastguard Worker if (--path->entries == 0)
1658*6a54128fSAndroid Build Coastguard Worker path->curr = 0;
1659*6a54128fSAndroid Build Coastguard Worker
1660*6a54128fSAndroid Build Coastguard Worker /* if non-root node has no entries left, remove it & parent ptr to it */
1661*6a54128fSAndroid Build Coastguard Worker if (path->entries == 0 && handle->level) {
1662*6a54128fSAndroid Build Coastguard Worker if (!(flags & EXT2_EXTENT_DELETE_KEEP_EMPTY)) {
1663*6a54128fSAndroid Build Coastguard Worker struct ext2fs_extent extent;
1664*6a54128fSAndroid Build Coastguard Worker
1665*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_get(handle, EXT2_EXTENT_UP,
1666*6a54128fSAndroid Build Coastguard Worker &extent);
1667*6a54128fSAndroid Build Coastguard Worker if (retval)
1668*6a54128fSAndroid Build Coastguard Worker return retval;
1669*6a54128fSAndroid Build Coastguard Worker
1670*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_extent_delete(handle, flags);
1671*6a54128fSAndroid Build Coastguard Worker handle->inode->i_blocks -=
1672*6a54128fSAndroid Build Coastguard Worker (handle->fs->blocksize *
1673*6a54128fSAndroid Build Coastguard Worker EXT2FS_CLUSTER_RATIO(handle->fs)) / 512;
1674*6a54128fSAndroid Build Coastguard Worker retval = ext2fs_write_inode(handle->fs, handle->ino,
1675*6a54128fSAndroid Build Coastguard Worker handle->inode);
1676*6a54128fSAndroid Build Coastguard Worker ext2fs_block_alloc_stats2(handle->fs,
1677*6a54128fSAndroid Build Coastguard Worker extent.e_pblk, -1);
1678*6a54128fSAndroid Build Coastguard Worker }
1679*6a54128fSAndroid Build Coastguard Worker } else {
1680*6a54128fSAndroid Build Coastguard Worker eh = (struct ext3_extent_header *) path->buf;
1681*6a54128fSAndroid Build Coastguard Worker eh->eh_entries = ext2fs_cpu_to_le16(path->entries);
1682*6a54128fSAndroid Build Coastguard Worker if ((path->entries == 0) && (handle->level == 0)) {
1683*6a54128fSAndroid Build Coastguard Worker eh->eh_depth = 0;
1684*6a54128fSAndroid Build Coastguard Worker handle->max_depth = 0;
1685*6a54128fSAndroid Build Coastguard Worker }
1686*6a54128fSAndroid Build Coastguard Worker retval = update_path(handle);
1687*6a54128fSAndroid Build Coastguard Worker }
1688*6a54128fSAndroid Build Coastguard Worker return retval;
1689*6a54128fSAndroid Build Coastguard Worker }
1690*6a54128fSAndroid Build Coastguard Worker
ext2fs_extent_get_info(ext2_extent_handle_t handle,struct ext2_extent_info * info)1691*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_extent_get_info(ext2_extent_handle_t handle,
1692*6a54128fSAndroid Build Coastguard Worker struct ext2_extent_info *info)
1693*6a54128fSAndroid Build Coastguard Worker {
1694*6a54128fSAndroid Build Coastguard Worker struct extent_path *path;
1695*6a54128fSAndroid Build Coastguard Worker
1696*6a54128fSAndroid Build Coastguard Worker EXT2_CHECK_MAGIC(handle, EXT2_ET_MAGIC_EXTENT_HANDLE);
1697*6a54128fSAndroid Build Coastguard Worker
1698*6a54128fSAndroid Build Coastguard Worker memset(info, 0, sizeof(struct ext2_extent_info));
1699*6a54128fSAndroid Build Coastguard Worker
1700*6a54128fSAndroid Build Coastguard Worker path = handle->path + handle->level;
1701*6a54128fSAndroid Build Coastguard Worker if (path) {
1702*6a54128fSAndroid Build Coastguard Worker if (path->curr)
1703*6a54128fSAndroid Build Coastguard Worker info->curr_entry = ((char *) path->curr - path->buf) /
1704*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext3_extent_idx);
1705*6a54128fSAndroid Build Coastguard Worker else
1706*6a54128fSAndroid Build Coastguard Worker info->curr_entry = 0;
1707*6a54128fSAndroid Build Coastguard Worker info->num_entries = path->entries;
1708*6a54128fSAndroid Build Coastguard Worker info->max_entries = path->max_entries;
1709*6a54128fSAndroid Build Coastguard Worker info->bytes_avail = (path->max_entries - path->entries) *
1710*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext3_extent);
1711*6a54128fSAndroid Build Coastguard Worker }
1712*6a54128fSAndroid Build Coastguard Worker
1713*6a54128fSAndroid Build Coastguard Worker info->curr_level = handle->level;
1714*6a54128fSAndroid Build Coastguard Worker info->max_depth = handle->max_depth;
1715*6a54128fSAndroid Build Coastguard Worker info->max_lblk = EXT_MAX_EXTENT_LBLK;
1716*6a54128fSAndroid Build Coastguard Worker info->max_pblk = EXT_MAX_EXTENT_PBLK;
1717*6a54128fSAndroid Build Coastguard Worker info->max_len = EXT_INIT_MAX_LEN;
1718*6a54128fSAndroid Build Coastguard Worker info->max_uninit_len = EXT_UNINIT_MAX_LEN;
1719*6a54128fSAndroid Build Coastguard Worker
1720*6a54128fSAndroid Build Coastguard Worker return 0;
1721*6a54128fSAndroid Build Coastguard Worker }
1722*6a54128fSAndroid Build Coastguard Worker
ul_log2(unsigned long arg)1723*6a54128fSAndroid Build Coastguard Worker static int ul_log2(unsigned long arg)
1724*6a54128fSAndroid Build Coastguard Worker {
1725*6a54128fSAndroid Build Coastguard Worker int l = 0;
1726*6a54128fSAndroid Build Coastguard Worker
1727*6a54128fSAndroid Build Coastguard Worker arg >>= 1;
1728*6a54128fSAndroid Build Coastguard Worker while (arg) {
1729*6a54128fSAndroid Build Coastguard Worker l++;
1730*6a54128fSAndroid Build Coastguard Worker arg >>= 1;
1731*6a54128fSAndroid Build Coastguard Worker }
1732*6a54128fSAndroid Build Coastguard Worker return l;
1733*6a54128fSAndroid Build Coastguard Worker }
1734*6a54128fSAndroid Build Coastguard Worker
ext2fs_max_extent_depth(ext2_extent_handle_t handle)1735*6a54128fSAndroid Build Coastguard Worker size_t ext2fs_max_extent_depth(ext2_extent_handle_t handle)
1736*6a54128fSAndroid Build Coastguard Worker {
1737*6a54128fSAndroid Build Coastguard Worker size_t iblock_sz = sizeof(((struct ext2_inode *)NULL)->i_block);
1738*6a54128fSAndroid Build Coastguard Worker size_t iblock_extents = (iblock_sz - sizeof(struct ext3_extent_header)) /
1739*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext3_extent);
1740*6a54128fSAndroid Build Coastguard Worker size_t extents_per_block = (handle->fs->blocksize -
1741*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext3_extent_header)) /
1742*6a54128fSAndroid Build Coastguard Worker sizeof(struct ext3_extent);
1743*6a54128fSAndroid Build Coastguard Worker static unsigned int last_blocksize = 0;
1744*6a54128fSAndroid Build Coastguard Worker static size_t last_result = 0;
1745*6a54128fSAndroid Build Coastguard Worker
1746*6a54128fSAndroid Build Coastguard Worker if (last_blocksize && last_blocksize == handle->fs->blocksize)
1747*6a54128fSAndroid Build Coastguard Worker return last_result;
1748*6a54128fSAndroid Build Coastguard Worker
1749*6a54128fSAndroid Build Coastguard Worker last_result = 1 + ((ul_log2(EXT_MAX_EXTENT_LBLK) - ul_log2(iblock_extents)) /
1750*6a54128fSAndroid Build Coastguard Worker ul_log2(extents_per_block));
1751*6a54128fSAndroid Build Coastguard Worker last_blocksize = handle->fs->blocksize;
1752*6a54128fSAndroid Build Coastguard Worker return last_result;
1753*6a54128fSAndroid Build Coastguard Worker }
1754*6a54128fSAndroid Build Coastguard Worker
ext2fs_fix_extents_checksums(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode)1755*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_fix_extents_checksums(ext2_filsys fs, ext2_ino_t ino,
1756*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode)
1757*6a54128fSAndroid Build Coastguard Worker {
1758*6a54128fSAndroid Build Coastguard Worker ext2_extent_handle_t handle;
1759*6a54128fSAndroid Build Coastguard Worker struct ext2fs_extent extent;
1760*6a54128fSAndroid Build Coastguard Worker errcode_t errcode;
1761*6a54128fSAndroid Build Coastguard Worker int save_flags = fs->flags;
1762*6a54128fSAndroid Build Coastguard Worker
1763*6a54128fSAndroid Build Coastguard Worker if (!ext2fs_has_feature_metadata_csum(fs->super) ||
1764*6a54128fSAndroid Build Coastguard Worker (inode && !(inode->i_flags & EXT4_EXTENTS_FL)))
1765*6a54128fSAndroid Build Coastguard Worker return 0;
1766*6a54128fSAndroid Build Coastguard Worker
1767*6a54128fSAndroid Build Coastguard Worker errcode = ext2fs_extent_open2(fs, ino, inode, &handle);
1768*6a54128fSAndroid Build Coastguard Worker if (errcode) {
1769*6a54128fSAndroid Build Coastguard Worker if (errcode == EXT2_ET_INODE_NOT_EXTENT)
1770*6a54128fSAndroid Build Coastguard Worker errcode = 0;
1771*6a54128fSAndroid Build Coastguard Worker return errcode;
1772*6a54128fSAndroid Build Coastguard Worker }
1773*6a54128fSAndroid Build Coastguard Worker
1774*6a54128fSAndroid Build Coastguard Worker fs->flags &= ~EXT2_FLAG_IGNORE_CSUM_ERRORS;
1775*6a54128fSAndroid Build Coastguard Worker errcode = ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent);
1776*6a54128fSAndroid Build Coastguard Worker if (errcode)
1777*6a54128fSAndroid Build Coastguard Worker goto out;
1778*6a54128fSAndroid Build Coastguard Worker
1779*6a54128fSAndroid Build Coastguard Worker do {
1780*6a54128fSAndroid Build Coastguard Worker /* Skip to the end of a block of leaf nodes */
1781*6a54128fSAndroid Build Coastguard Worker if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
1782*6a54128fSAndroid Build Coastguard Worker errcode = ext2fs_extent_get(handle,
1783*6a54128fSAndroid Build Coastguard Worker EXT2_EXTENT_LAST_SIB,
1784*6a54128fSAndroid Build Coastguard Worker &extent);
1785*6a54128fSAndroid Build Coastguard Worker if (errcode)
1786*6a54128fSAndroid Build Coastguard Worker break;
1787*6a54128fSAndroid Build Coastguard Worker }
1788*6a54128fSAndroid Build Coastguard Worker
1789*6a54128fSAndroid Build Coastguard Worker errcode = ext2fs_extent_get(handle, EXT2_EXTENT_NEXT, &extent);
1790*6a54128fSAndroid Build Coastguard Worker if (errcode == EXT2_ET_EXTENT_CSUM_INVALID)
1791*6a54128fSAndroid Build Coastguard Worker errcode = update_path(handle);
1792*6a54128fSAndroid Build Coastguard Worker } while (errcode == 0);
1793*6a54128fSAndroid Build Coastguard Worker
1794*6a54128fSAndroid Build Coastguard Worker out:
1795*6a54128fSAndroid Build Coastguard Worker /* Ok if we run off the end */
1796*6a54128fSAndroid Build Coastguard Worker if (errcode == EXT2_ET_EXTENT_NO_NEXT)
1797*6a54128fSAndroid Build Coastguard Worker errcode = 0;
1798*6a54128fSAndroid Build Coastguard Worker ext2fs_extent_free(handle);
1799*6a54128fSAndroid Build Coastguard Worker fs->flags = save_flags;
1800*6a54128fSAndroid Build Coastguard Worker return errcode;
1801*6a54128fSAndroid Build Coastguard Worker }
1802*6a54128fSAndroid Build Coastguard Worker
ext2fs_decode_extent(struct ext2fs_extent * to,void * addr,int len)1803*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_decode_extent(struct ext2fs_extent *to, void *addr, int len)
1804*6a54128fSAndroid Build Coastguard Worker {
1805*6a54128fSAndroid Build Coastguard Worker struct ext3_extent *from = (struct ext3_extent *)addr;
1806*6a54128fSAndroid Build Coastguard Worker
1807*6a54128fSAndroid Build Coastguard Worker if (len != sizeof(struct ext3_extent))
1808*6a54128fSAndroid Build Coastguard Worker return EXT2_ET_INVALID_ARGUMENT;
1809*6a54128fSAndroid Build Coastguard Worker
1810*6a54128fSAndroid Build Coastguard Worker to->e_pblk = ext2fs_le32_to_cpu(from->ee_start) +
1811*6a54128fSAndroid Build Coastguard Worker ((__u64) ext2fs_le16_to_cpu(from->ee_start_hi)
1812*6a54128fSAndroid Build Coastguard Worker << 32);
1813*6a54128fSAndroid Build Coastguard Worker to->e_lblk = ext2fs_le32_to_cpu(from->ee_block);
1814*6a54128fSAndroid Build Coastguard Worker to->e_len = ext2fs_le16_to_cpu(from->ee_len);
1815*6a54128fSAndroid Build Coastguard Worker to->e_flags = EXT2_EXTENT_FLAGS_LEAF;
1816*6a54128fSAndroid Build Coastguard Worker if (to->e_len > EXT_INIT_MAX_LEN) {
1817*6a54128fSAndroid Build Coastguard Worker to->e_len -= EXT_INIT_MAX_LEN;
1818*6a54128fSAndroid Build Coastguard Worker to->e_flags |= EXT2_EXTENT_FLAGS_UNINIT;
1819*6a54128fSAndroid Build Coastguard Worker }
1820*6a54128fSAndroid Build Coastguard Worker
1821*6a54128fSAndroid Build Coastguard Worker return 0;
1822*6a54128fSAndroid Build Coastguard Worker }
1823*6a54128fSAndroid Build Coastguard Worker
ext2fs_count_blocks(ext2_filsys fs,ext2_ino_t ino,struct ext2_inode * inode,blk64_t * ret_count)1824*6a54128fSAndroid Build Coastguard Worker errcode_t ext2fs_count_blocks(ext2_filsys fs, ext2_ino_t ino,
1825*6a54128fSAndroid Build Coastguard Worker struct ext2_inode *inode, blk64_t *ret_count)
1826*6a54128fSAndroid Build Coastguard Worker {
1827*6a54128fSAndroid Build Coastguard Worker ext2_extent_handle_t handle = NULL;
1828*6a54128fSAndroid Build Coastguard Worker struct ext2fs_extent extent;
1829*6a54128fSAndroid Build Coastguard Worker errcode_t errcode;
1830*6a54128fSAndroid Build Coastguard Worker int i;
1831*6a54128fSAndroid Build Coastguard Worker blk64_t blkcount = 0;
1832*6a54128fSAndroid Build Coastguard Worker blk64_t *intermediate_nodes;
1833*6a54128fSAndroid Build Coastguard Worker
1834*6a54128fSAndroid Build Coastguard Worker errcode = ext2fs_extent_open2(fs, ino, inode, &handle);
1835*6a54128fSAndroid Build Coastguard Worker if (errcode)
1836*6a54128fSAndroid Build Coastguard Worker goto out;
1837*6a54128fSAndroid Build Coastguard Worker
1838*6a54128fSAndroid Build Coastguard Worker errcode = ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent);
1839*6a54128fSAndroid Build Coastguard Worker if (errcode)
1840*6a54128fSAndroid Build Coastguard Worker goto out;
1841*6a54128fSAndroid Build Coastguard Worker
1842*6a54128fSAndroid Build Coastguard Worker errcode = ext2fs_get_array(handle->max_depth, sizeof(blk64_t),
1843*6a54128fSAndroid Build Coastguard Worker &intermediate_nodes);
1844*6a54128fSAndroid Build Coastguard Worker if (errcode)
1845*6a54128fSAndroid Build Coastguard Worker goto out;
1846*6a54128fSAndroid Build Coastguard Worker
1847*6a54128fSAndroid Build Coastguard Worker blkcount = handle->level;
1848*6a54128fSAndroid Build Coastguard Worker while (!errcode) {
1849*6a54128fSAndroid Build Coastguard Worker if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
1850*6a54128fSAndroid Build Coastguard Worker blkcount += extent.e_len;
1851*6a54128fSAndroid Build Coastguard Worker for (i = 0; i < handle->level; i++) {
1852*6a54128fSAndroid Build Coastguard Worker if (intermediate_nodes[i] !=
1853*6a54128fSAndroid Build Coastguard Worker handle->path[i].end_blk) {
1854*6a54128fSAndroid Build Coastguard Worker blkcount++;
1855*6a54128fSAndroid Build Coastguard Worker intermediate_nodes[i] =
1856*6a54128fSAndroid Build Coastguard Worker handle->path[i].end_blk;
1857*6a54128fSAndroid Build Coastguard Worker }
1858*6a54128fSAndroid Build Coastguard Worker }
1859*6a54128fSAndroid Build Coastguard Worker }
1860*6a54128fSAndroid Build Coastguard Worker errcode = ext2fs_extent_get(handle, EXT2_EXTENT_NEXT, &extent);
1861*6a54128fSAndroid Build Coastguard Worker }
1862*6a54128fSAndroid Build Coastguard Worker ext2fs_free_mem(&intermediate_nodes);
1863*6a54128fSAndroid Build Coastguard Worker out:
1864*6a54128fSAndroid Build Coastguard Worker *ret_count = blkcount;
1865*6a54128fSAndroid Build Coastguard Worker ext2fs_extent_free(handle);
1866*6a54128fSAndroid Build Coastguard Worker
1867*6a54128fSAndroid Build Coastguard Worker return 0;
1868*6a54128fSAndroid Build Coastguard Worker }
1869*6a54128fSAndroid Build Coastguard Worker
1870*6a54128fSAndroid Build Coastguard Worker #ifdef DEBUG
1871*6a54128fSAndroid Build Coastguard Worker /*
1872*6a54128fSAndroid Build Coastguard Worker * Override debugfs's prompt
1873*6a54128fSAndroid Build Coastguard Worker */
1874*6a54128fSAndroid Build Coastguard Worker const char *debug_prog_name = "tst_extents";
1875*6a54128fSAndroid Build Coastguard Worker
1876*6a54128fSAndroid Build Coastguard Worker #endif
1877*6a54128fSAndroid Build Coastguard Worker
1878