2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
24 * This file contains miscelanious TNC-related functions shared betweend
25 * different files. This file does not form any logically separate TNC
26 * sub-system. The file was created because there is a lot of TNC code and
27 * putting it all in one file would make that file too big and unreadable.
33 * ubifs_tnc_levelorder_next - next TNC tree element in levelorder traversal.
34 * @zr: root of the subtree to traverse
35 * @znode: previous znode
37 * This function implements levelorder TNC traversal. The LNC is ignored.
38 * Returns the next element or %NULL if @znode is already the last one.
40 struct ubifs_znode *ubifs_tnc_levelorder_next(struct ubifs_znode *zr,
41 struct ubifs_znode *znode)
43 int level, iip, level_search = 0;
44 struct ubifs_znode *zn;
51 if (unlikely(znode == zr)) {
52 if (znode->level == 0)
54 return ubifs_tnc_find_child(zr, 0);
61 ubifs_assert(znode->level <= zr->level);
64 * First walk up until there is a znode with next branch to
67 while (znode->parent != zr && iip >= znode->parent->child_cnt) {
68 znode = znode->parent;
72 if (unlikely(znode->parent == zr &&
73 iip >= znode->parent->child_cnt)) {
74 /* This level is done, switch to the lower one */
76 if (level_search || level < 0)
78 * We were already looking for znode at lower
79 * level ('level_search'). As we are here
80 * again, it just does not exist. Or all levels
81 * were finished ('level < 0').
87 znode = ubifs_tnc_find_child(zr, 0);
91 /* Switch to the next index */
92 zn = ubifs_tnc_find_child(znode->parent, iip + 1);
94 /* No more children to look at, we have walk up */
95 iip = znode->parent->child_cnt;
99 /* Walk back down to the level we came from ('level') */
100 while (zn->level != level) {
102 zn = ubifs_tnc_find_child(zn, 0);
105 * This path is not too deep so it does not
106 * reach 'level'. Try next path.
114 ubifs_assert(zn->level >= 0);
121 * ubifs_search_zbranch - search znode branch.
122 * @c: UBIFS file-system description object
123 * @znode: znode to search in
124 * @key: key to search for
125 * @n: znode branch slot number is returned here
127 * This is a helper function which search branch with key @key in @znode using
128 * binary search. The result of the search may be:
129 * o exact match, then %1 is returned, and the slot number of the branch is
131 * o no exact match, then %0 is returned and the slot number of the left
132 * closest branch is returned in @n; the slot if all keys in this znode are
133 * greater than @key, then %-1 is returned in @n.
135 int ubifs_search_zbranch(const struct ubifs_info *c,
136 const struct ubifs_znode *znode,
137 const union ubifs_key *key, int *n)
139 int beg = 0, end = znode->child_cnt, uninitialized_var(mid);
140 int uninitialized_var(cmp);
141 const struct ubifs_zbranch *zbr = &znode->zbranch[0];
143 ubifs_assert(end > beg);
146 mid = (beg + end) >> 1;
147 cmp = keys_cmp(c, key, &zbr[mid].key);
160 /* The insert point is after *n */
161 ubifs_assert(*n >= -1 && *n < znode->child_cnt);
163 ubifs_assert(keys_cmp(c, key, &zbr[0].key) < 0);
165 ubifs_assert(keys_cmp(c, key, &zbr[*n].key) > 0);
166 if (*n + 1 < znode->child_cnt)
167 ubifs_assert(keys_cmp(c, key, &zbr[*n + 1].key) < 0);
173 * ubifs_tnc_postorder_first - find first znode to do postorder tree traversal.
174 * @znode: znode to start at (root of the sub-tree to traverse)
176 * Find the lowest leftmost znode in a subtree of the TNC tree. The LNC is
179 struct ubifs_znode *ubifs_tnc_postorder_first(struct ubifs_znode *znode)
181 if (unlikely(!znode))
184 while (znode->level > 0) {
185 struct ubifs_znode *child;
187 child = ubifs_tnc_find_child(znode, 0);
197 * ubifs_tnc_postorder_next - next TNC tree element in postorder traversal.
198 * @znode: previous znode
200 * This function implements postorder TNC traversal. The LNC is ignored.
201 * Returns the next element or %NULL if @znode is already the last one.
203 struct ubifs_znode *ubifs_tnc_postorder_next(struct ubifs_znode *znode)
205 struct ubifs_znode *zn;
208 if (unlikely(!znode->parent))
211 /* Switch to the next index in the parent */
212 zn = ubifs_tnc_find_child(znode->parent, znode->iip + 1);
214 /* This is in fact the last child, return parent */
215 return znode->parent;
217 /* Go to the first znode in this new subtree */
218 return ubifs_tnc_postorder_first(zn);
222 * read_znode - read an indexing node from flash and fill znode.
223 * @c: UBIFS file-system description object
224 * @lnum: LEB of the indexing node to read
227 * @znode: znode to read to
229 * This function reads an indexing node from the flash media and fills znode
230 * with the read data. Returns zero in case of success and a negative error
231 * code in case of failure. The read indexing node is validated and if anything
232 * is wrong with it, this function prints complaint messages and returns
235 static int read_znode(struct ubifs_info *c, int lnum, int offs, int len,
236 struct ubifs_znode *znode)
238 int i, err, type, cmp;
239 struct ubifs_idx_node *idx;
241 idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
245 err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
251 znode->child_cnt = le16_to_cpu(idx->child_cnt);
252 znode->level = le16_to_cpu(idx->level);
254 dbg_tnc("LEB %d:%d, level %d, %d branch",
255 lnum, offs, znode->level, znode->child_cnt);
257 if (znode->child_cnt > c->fanout || znode->level > UBIFS_MAX_LEVELS) {
258 dbg_err("current fanout %d, branch count %d",
259 c->fanout, znode->child_cnt);
260 dbg_err("max levels %d, znode level %d",
261 UBIFS_MAX_LEVELS, znode->level);
266 for (i = 0; i < znode->child_cnt; i++) {
267 const struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
268 struct ubifs_zbranch *zbr = &znode->zbranch[i];
270 key_read(c, &br->key, &zbr->key);
271 zbr->lnum = le32_to_cpu(br->lnum);
272 zbr->offs = le32_to_cpu(br->offs);
273 zbr->len = le32_to_cpu(br->len);
276 /* Validate branch */
278 if (zbr->lnum < c->main_first ||
279 zbr->lnum >= c->leb_cnt || zbr->offs < 0 ||
280 zbr->offs + zbr->len > c->leb_size || zbr->offs & 7) {
281 dbg_err("bad branch %d", i);
286 switch (key_type(c, &zbr->key)) {
293 dbg_msg("bad key type at slot %d: %s", i,
302 type = key_type(c, &zbr->key);
303 if (c->ranges[type].max_len == 0) {
304 if (zbr->len != c->ranges[type].len) {
305 dbg_err("bad target node (type %d) length (%d)",
307 dbg_err("have to be %d", c->ranges[type].len);
311 } else if (zbr->len < c->ranges[type].min_len ||
312 zbr->len > c->ranges[type].max_len) {
313 dbg_err("bad target node (type %d) length (%d)",
315 dbg_err("have to be in range of %d-%d",
316 c->ranges[type].min_len,
317 c->ranges[type].max_len);
324 * Ensure that the next key is greater or equivalent to the
327 for (i = 0; i < znode->child_cnt - 1; i++) {
328 const union ubifs_key *key1, *key2;
330 key1 = &znode->zbranch[i].key;
331 key2 = &znode->zbranch[i + 1].key;
333 cmp = keys_cmp(c, key1, key2);
335 dbg_err("bad key order (keys %d and %d)", i, i + 1);
338 } else if (cmp == 0 && !is_hash_key(c, key1)) {
339 /* These can only be keys with colliding hash */
340 dbg_err("keys %d and %d are not hashed but equivalent",
351 ubifs_err("bad indexing node at LEB %d:%d, error %d", lnum, offs, err);
352 dbg_dump_node(c, idx);
358 * ubifs_load_znode - load znode to TNC cache.
359 * @c: UBIFS file-system description object
361 * @parent: znode's parent
362 * @iip: index in parent
364 * This function loads znode pointed to by @zbr into the TNC cache and
365 * returns pointer to it in case of success and a negative error code in case
368 struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
369 struct ubifs_zbranch *zbr,
370 struct ubifs_znode *parent, int iip)
373 struct ubifs_znode *znode;
375 ubifs_assert(!zbr->znode);
377 * A slab cache is not presently used for znodes because the znode size
378 * depends on the fanout which is stored in the superblock.
380 znode = kzalloc(c->max_znode_sz, GFP_NOFS);
382 return ERR_PTR(-ENOMEM);
384 err = read_znode(c, zbr->lnum, zbr->offs, zbr->len, znode);
389 znode->parent = parent;
390 znode->time = get_seconds();
401 * ubifs_tnc_read_node - read a leaf node from the flash media.
402 * @c: UBIFS file-system description object
403 * @zbr: key and position of the node
404 * @node: node is returned here
406 * This function reads a node defined by @zbr from the flash media. Returns
407 * zero in case of success or a negative negative error code in case of
410 int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
413 union ubifs_key key1, *key = &zbr->key;
414 int err, type = key_type(c, key);
416 err = ubifs_read_node(c, node, type, zbr->len, zbr->lnum, zbr->offs);
419 dbg_tnc("key %s", DBGKEY(key));
423 /* Make sure the key of the read node is correct */
424 key_read(c, node + UBIFS_KEY_OFFSET, &key1);
425 if (!keys_eq(c, key, &key1)) {
426 ubifs_err("bad key in node at LEB %d:%d",
427 zbr->lnum, zbr->offs);
428 dbg_tnc("looked for key %s found node's key %s",
429 DBGKEY(key), DBGKEY1(&key1));
430 dbg_dump_node(c, node);