blob: 41c7d5666ff07907c00a9805f4640fe8c289c39c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002/*
3 * This file is part of UBIFS.
4 *
5 * Copyright (C) 2006-2008 Nokia Corporation.
6 *
Stefan Roese9eefe2a2009-03-19 15:35:05 +01007 * Authors: Adrian Hunter
8 * Artem Bityutskiy (Битюцкий Артём)
9 */
10
11/*
12 * This file implements TNC (Tree Node Cache) which caches indexing nodes of
13 * the UBIFS B-tree.
14 *
15 * At the moment the locking rules of the TNC tree are quite simple and
16 * straightforward. We just have a mutex and lock it when we traverse the
17 * tree. If a znode is not in memory, we read it from flash while still having
18 * the mutex locked.
19 */
20
Heiko Schocherff94bc42014-06-24 10:10:04 +020021#ifndef __UBOOT__
Simon Glassf7ae49f2020-05-10 11:40:05 -060022#include <log.h>
Simon Glass61b29b82020-02-03 07:36:15 -070023#include <dm/devres.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020024#include <linux/crc32.h>
25#include <linux/slab.h>
Simon Glass3db71102019-11-14 12:57:16 -070026#include <u-boot/crc.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020027#else
Simon Glasseb41d8a2020-05-10 11:40:08 -060028#include <linux/bug.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020029#include <linux/compat.h>
30#include <linux/err.h>
31#include <linux/stat.h>
32#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +010033#include "ubifs.h"
34
35/*
36 * Returned codes of 'matches_name()' and 'fallible_matches_name()' functions.
37 * @NAME_LESS: name corresponding to the first argument is less than second
38 * @NAME_MATCHES: names match
39 * @NAME_GREATER: name corresponding to the second argument is greater than
40 * first
41 * @NOT_ON_MEDIA: node referred by zbranch does not exist on the media
42 *
43 * These constants were introduce to improve readability.
44 */
45enum {
46 NAME_LESS = 0,
47 NAME_MATCHES = 1,
48 NAME_GREATER = 2,
49 NOT_ON_MEDIA = 3,
50};
51
52/**
53 * insert_old_idx - record an index node obsoleted since the last commit start.
54 * @c: UBIFS file-system description object
55 * @lnum: LEB number of obsoleted index node
56 * @offs: offset of obsoleted index node
57 *
58 * Returns %0 on success, and a negative error code on failure.
59 *
60 * For recovery, there must always be a complete intact version of the index on
61 * flash at all times. That is called the "old index". It is the index as at the
62 * time of the last successful commit. Many of the index nodes in the old index
63 * may be dirty, but they must not be erased until the next successful commit
64 * (at which point that index becomes the old index).
65 *
66 * That means that the garbage collection and the in-the-gaps method of
67 * committing must be able to determine if an index node is in the old index.
68 * Most of the old index nodes can be found by looking up the TNC using the
69 * 'lookup_znode()' function. However, some of the old index nodes may have
70 * been deleted from the current index or may have been changed so much that
71 * they cannot be easily found. In those cases, an entry is added to an RB-tree.
72 * That is what this function does. The RB-tree is ordered by LEB number and
73 * offset because they uniquely identify the old index node.
74 */
75static int insert_old_idx(struct ubifs_info *c, int lnum, int offs)
76{
77 struct ubifs_old_idx *old_idx, *o;
78 struct rb_node **p, *parent = NULL;
79
80 old_idx = kmalloc(sizeof(struct ubifs_old_idx), GFP_NOFS);
81 if (unlikely(!old_idx))
82 return -ENOMEM;
83 old_idx->lnum = lnum;
84 old_idx->offs = offs;
85
86 p = &c->old_idx.rb_node;
87 while (*p) {
88 parent = *p;
89 o = rb_entry(parent, struct ubifs_old_idx, rb);
90 if (lnum < o->lnum)
91 p = &(*p)->rb_left;
92 else if (lnum > o->lnum)
93 p = &(*p)->rb_right;
94 else if (offs < o->offs)
95 p = &(*p)->rb_left;
96 else if (offs > o->offs)
97 p = &(*p)->rb_right;
98 else {
Heiko Schocher0195a7b2015-10-22 06:19:21 +020099 ubifs_err(c, "old idx added twice!");
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100100 kfree(old_idx);
101 return 0;
102 }
103 }
104 rb_link_node(&old_idx->rb, parent, p);
105 rb_insert_color(&old_idx->rb, &c->old_idx);
106 return 0;
107}
108
109/**
110 * insert_old_idx_znode - record a znode obsoleted since last commit start.
111 * @c: UBIFS file-system description object
112 * @znode: znode of obsoleted index node
113 *
114 * Returns %0 on success, and a negative error code on failure.
115 */
116int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode)
117{
118 if (znode->parent) {
119 struct ubifs_zbranch *zbr;
120
121 zbr = &znode->parent->zbranch[znode->iip];
122 if (zbr->len)
123 return insert_old_idx(c, zbr->lnum, zbr->offs);
124 } else
125 if (c->zroot.len)
126 return insert_old_idx(c, c->zroot.lnum,
127 c->zroot.offs);
128 return 0;
129}
130
131/**
132 * ins_clr_old_idx_znode - record a znode obsoleted since last commit start.
133 * @c: UBIFS file-system description object
134 * @znode: znode of obsoleted index node
135 *
136 * Returns %0 on success, and a negative error code on failure.
137 */
138static int ins_clr_old_idx_znode(struct ubifs_info *c,
139 struct ubifs_znode *znode)
140{
141 int err;
142
143 if (znode->parent) {
144 struct ubifs_zbranch *zbr;
145
146 zbr = &znode->parent->zbranch[znode->iip];
147 if (zbr->len) {
148 err = insert_old_idx(c, zbr->lnum, zbr->offs);
149 if (err)
150 return err;
151 zbr->lnum = 0;
152 zbr->offs = 0;
153 zbr->len = 0;
154 }
155 } else
156 if (c->zroot.len) {
157 err = insert_old_idx(c, c->zroot.lnum, c->zroot.offs);
158 if (err)
159 return err;
160 c->zroot.lnum = 0;
161 c->zroot.offs = 0;
162 c->zroot.len = 0;
163 }
164 return 0;
165}
166
167/**
168 * destroy_old_idx - destroy the old_idx RB-tree.
169 * @c: UBIFS file-system description object
170 *
171 * During start commit, the old_idx RB-tree is used to avoid overwriting index
172 * nodes that were in the index last commit but have since been deleted. This
173 * is necessary for recovery i.e. the old index must be kept intact until the
174 * new index is successfully written. The old-idx RB-tree is used for the
175 * in-the-gaps method of writing index nodes and is destroyed every commit.
176 */
177void destroy_old_idx(struct ubifs_info *c)
178{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200179 struct ubifs_old_idx *old_idx, *n;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100180
Heiko Schocherff94bc42014-06-24 10:10:04 +0200181 rbtree_postorder_for_each_entry_safe(old_idx, n, &c->old_idx, rb)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100182 kfree(old_idx);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200183
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100184 c->old_idx = RB_ROOT;
185}
186
187/**
188 * copy_znode - copy a dirty znode.
189 * @c: UBIFS file-system description object
190 * @znode: znode to copy
191 *
192 * A dirty znode being committed may not be changed, so it is copied.
193 */
194static struct ubifs_znode *copy_znode(struct ubifs_info *c,
195 struct ubifs_znode *znode)
196{
197 struct ubifs_znode *zn;
198
199 zn = kmalloc(c->max_znode_sz, GFP_NOFS);
200 if (unlikely(!zn))
201 return ERR_PTR(-ENOMEM);
202
203 memcpy(zn, znode, c->max_znode_sz);
204 zn->cnext = NULL;
205 __set_bit(DIRTY_ZNODE, &zn->flags);
206 __clear_bit(COW_ZNODE, &zn->flags);
207
Heiko Schocherff94bc42014-06-24 10:10:04 +0200208 ubifs_assert(!ubifs_zn_obsolete(znode));
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100209 __set_bit(OBSOLETE_ZNODE, &znode->flags);
210
211 if (znode->level != 0) {
212 int i;
213 const int n = zn->child_cnt;
214
215 /* The children now have new parent */
216 for (i = 0; i < n; i++) {
217 struct ubifs_zbranch *zbr = &zn->zbranch[i];
218
219 if (zbr->znode)
220 zbr->znode->parent = zn;
221 }
222 }
223
224 atomic_long_inc(&c->dirty_zn_cnt);
225 return zn;
226}
227
228/**
229 * add_idx_dirt - add dirt due to a dirty znode.
230 * @c: UBIFS file-system description object
231 * @lnum: LEB number of index node
232 * @dirt: size of index node
233 *
234 * This function updates lprops dirty space and the new size of the index.
235 */
236static int add_idx_dirt(struct ubifs_info *c, int lnum, int dirt)
237{
238 c->calc_idx_sz -= ALIGN(dirt, 8);
239 return ubifs_add_dirt(c, lnum, dirt);
240}
241
242/**
243 * dirty_cow_znode - ensure a znode is not being committed.
244 * @c: UBIFS file-system description object
245 * @zbr: branch of znode to check
246 *
247 * Returns dirtied znode on success or negative error code on failure.
248 */
249static struct ubifs_znode *dirty_cow_znode(struct ubifs_info *c,
250 struct ubifs_zbranch *zbr)
251{
252 struct ubifs_znode *znode = zbr->znode;
253 struct ubifs_znode *zn;
254 int err;
255
Heiko Schocherff94bc42014-06-24 10:10:04 +0200256 if (!ubifs_zn_cow(znode)) {
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100257 /* znode is not being committed */
258 if (!test_and_set_bit(DIRTY_ZNODE, &znode->flags)) {
259 atomic_long_inc(&c->dirty_zn_cnt);
260 atomic_long_dec(&c->clean_zn_cnt);
261 atomic_long_dec(&ubifs_clean_zn_cnt);
262 err = add_idx_dirt(c, zbr->lnum, zbr->len);
263 if (unlikely(err))
264 return ERR_PTR(err);
265 }
266 return znode;
267 }
268
269 zn = copy_znode(c, znode);
270 if (IS_ERR(zn))
271 return zn;
272
273 if (zbr->len) {
274 err = insert_old_idx(c, zbr->lnum, zbr->offs);
275 if (unlikely(err))
276 return ERR_PTR(err);
277 err = add_idx_dirt(c, zbr->lnum, zbr->len);
278 } else
279 err = 0;
280
281 zbr->znode = zn;
282 zbr->lnum = 0;
283 zbr->offs = 0;
284 zbr->len = 0;
285
286 if (unlikely(err))
287 return ERR_PTR(err);
288 return zn;
289}
290
291/**
292 * lnc_add - add a leaf node to the leaf node cache.
293 * @c: UBIFS file-system description object
294 * @zbr: zbranch of leaf node
295 * @node: leaf node
296 *
297 * Leaf nodes are non-index nodes directory entry nodes or data nodes. The
298 * purpose of the leaf node cache is to save re-reading the same leaf node over
299 * and over again. Most things are cached by VFS, however the file system must
300 * cache directory entries for readdir and for resolving hash collisions. The
301 * present implementation of the leaf node cache is extremely simple, and
302 * allows for error returns that are not used but that may be needed if a more
303 * complex implementation is created.
304 *
305 * Note, this function does not add the @node object to LNC directly, but
306 * allocates a copy of the object and adds the copy to LNC. The reason for this
307 * is that @node has been allocated outside of the TNC subsystem and will be
308 * used with @c->tnc_mutex unlock upon return from the TNC subsystem. But LNC
309 * may be changed at any time, e.g. freed by the shrinker.
310 */
311static int lnc_add(struct ubifs_info *c, struct ubifs_zbranch *zbr,
312 const void *node)
313{
314 int err;
315 void *lnc_node;
316 const struct ubifs_dent_node *dent = node;
317
318 ubifs_assert(!zbr->leaf);
319 ubifs_assert(zbr->len != 0);
320 ubifs_assert(is_hash_key(c, &zbr->key));
321
322 err = ubifs_validate_entry(c, dent);
323 if (err) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200324 dump_stack();
325 ubifs_dump_node(c, dent);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100326 return err;
327 }
328
Heiko Schocherff94bc42014-06-24 10:10:04 +0200329 lnc_node = kmemdup(node, zbr->len, GFP_NOFS);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100330 if (!lnc_node)
331 /* We don't have to have the cache, so no error */
332 return 0;
333
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100334 zbr->leaf = lnc_node;
335 return 0;
336}
337
338 /**
339 * lnc_add_directly - add a leaf node to the leaf-node-cache.
340 * @c: UBIFS file-system description object
341 * @zbr: zbranch of leaf node
342 * @node: leaf node
343 *
344 * This function is similar to 'lnc_add()', but it does not create a copy of
345 * @node but inserts @node to TNC directly.
346 */
347static int lnc_add_directly(struct ubifs_info *c, struct ubifs_zbranch *zbr,
348 void *node)
349{
350 int err;
351
352 ubifs_assert(!zbr->leaf);
353 ubifs_assert(zbr->len != 0);
354
355 err = ubifs_validate_entry(c, node);
356 if (err) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200357 dump_stack();
358 ubifs_dump_node(c, node);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100359 return err;
360 }
361
362 zbr->leaf = node;
363 return 0;
364}
365
366/**
367 * lnc_free - remove a leaf node from the leaf node cache.
368 * @zbr: zbranch of leaf node
369 * @node: leaf node
370 */
371static void lnc_free(struct ubifs_zbranch *zbr)
372{
373 if (!zbr->leaf)
374 return;
375 kfree(zbr->leaf);
376 zbr->leaf = NULL;
377}
378
379/**
380 * tnc_read_node_nm - read a "hashed" leaf node.
381 * @c: UBIFS file-system description object
382 * @zbr: key and position of the node
383 * @node: node is returned here
384 *
385 * This function reads a "hashed" node defined by @zbr from the leaf node cache
386 * (in it is there) or from the hash media, in which case the node is also
387 * added to LNC. Returns zero in case of success or a negative negative error
388 * code in case of failure.
389 */
390static int tnc_read_node_nm(struct ubifs_info *c, struct ubifs_zbranch *zbr,
391 void *node)
392{
393 int err;
394
395 ubifs_assert(is_hash_key(c, &zbr->key));
396
397 if (zbr->leaf) {
398 /* Read from the leaf node cache */
399 ubifs_assert(zbr->len != 0);
400 memcpy(node, zbr->leaf, zbr->len);
401 return 0;
402 }
403
404 err = ubifs_tnc_read_node(c, zbr, node);
405 if (err)
406 return err;
407
408 /* Add the node to the leaf node cache */
409 err = lnc_add(c, zbr, node);
410 return err;
411}
412
413/**
414 * try_read_node - read a node if it is a node.
415 * @c: UBIFS file-system description object
416 * @buf: buffer to read to
417 * @type: node type
418 * @len: node length (not aligned)
419 * @lnum: LEB number of node to read
420 * @offs: offset of node to read
421 *
422 * This function tries to read a node of known type and length, checks it and
423 * stores it in @buf. This function returns %1 if a node is present and %0 if
424 * a node is not present. A negative error code is returned for I/O errors.
425 * This function performs that same function as ubifs_read_node except that
426 * it does not require that there is actually a node present and instead
427 * the return code indicates if a node was read.
428 *
429 * Note, this function does not check CRC of data nodes if @c->no_chk_data_crc
430 * is true (it is controlled by corresponding mount option). However, if
Heiko Schocherff94bc42014-06-24 10:10:04 +0200431 * @c->mounting or @c->remounting_rw is true (we are mounting or re-mounting to
432 * R/W mode), @c->no_chk_data_crc is ignored and CRC is checked. This is
433 * because during mounting or re-mounting from R/O mode to R/W mode we may read
434 * journal nodes (when replying the journal or doing the recovery) and the
435 * journal nodes may potentially be corrupted, so checking is required.
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100436 */
437static int try_read_node(const struct ubifs_info *c, void *buf, int type,
438 int len, int lnum, int offs)
439{
440 int err, node_len;
441 struct ubifs_ch *ch = buf;
442 uint32_t crc, node_crc;
443
444 dbg_io("LEB %d:%d, %s, length %d", lnum, offs, dbg_ntype(type), len);
445
Heiko Schocherff94bc42014-06-24 10:10:04 +0200446 err = ubifs_leb_read(c, lnum, buf, offs, len, 1);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100447 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200448 ubifs_err(c, "cannot read node type %d from LEB %d:%d, error %d",
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100449 type, lnum, offs, err);
450 return err;
451 }
452
453 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
454 return 0;
455
456 if (ch->node_type != type)
457 return 0;
458
459 node_len = le32_to_cpu(ch->len);
460 if (node_len != len)
461 return 0;
462
Heiko Schocherff94bc42014-06-24 10:10:04 +0200463 if (type == UBIFS_DATA_NODE && c->no_chk_data_crc && !c->mounting &&
464 !c->remounting_rw)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100465 return 1;
466
467 crc = crc32(UBIFS_CRC32_INIT, buf + 8, node_len - 8);
468 node_crc = le32_to_cpu(ch->crc);
469 if (crc != node_crc)
470 return 0;
471
472 return 1;
473}
474
475/**
476 * fallible_read_node - try to read a leaf node.
477 * @c: UBIFS file-system description object
478 * @key: key of node to read
479 * @zbr: position of node
480 * @node: node returned
481 *
482 * This function tries to read a node and returns %1 if the node is read, %0
483 * if the node is not present, and a negative error code in the case of error.
484 */
485static int fallible_read_node(struct ubifs_info *c, const union ubifs_key *key,
486 struct ubifs_zbranch *zbr, void *node)
487{
488 int ret;
489
Heiko Schocherff94bc42014-06-24 10:10:04 +0200490 dbg_tnck(key, "LEB %d:%d, key ", zbr->lnum, zbr->offs);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100491
492 ret = try_read_node(c, node, key_type(c, key), zbr->len, zbr->lnum,
493 zbr->offs);
494 if (ret == 1) {
495 union ubifs_key node_key;
496 struct ubifs_dent_node *dent = node;
497
498 /* All nodes have key in the same place */
499 key_read(c, &dent->key, &node_key);
500 if (keys_cmp(c, key, &node_key) != 0)
501 ret = 0;
502 }
503 if (ret == 0 && c->replaying)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200504 dbg_mntk(key, "dangling branch LEB %d:%d len %d, key ",
505 zbr->lnum, zbr->offs, zbr->len);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100506 return ret;
507}
508
509/**
510 * matches_name - determine if a direntry or xattr entry matches a given name.
511 * @c: UBIFS file-system description object
512 * @zbr: zbranch of dent
513 * @nm: name to match
514 *
515 * This function checks if xentry/direntry referred by zbranch @zbr matches name
516 * @nm. Returns %NAME_MATCHES if it does, %NAME_LESS if the name referred by
517 * @zbr is less than @nm, and %NAME_GREATER if it is greater than @nm. In case
518 * of failure, a negative error code is returned.
519 */
520static int matches_name(struct ubifs_info *c, struct ubifs_zbranch *zbr,
521 const struct qstr *nm)
522{
523 struct ubifs_dent_node *dent;
524 int nlen, err;
525
526 /* If possible, match against the dent in the leaf node cache */
527 if (!zbr->leaf) {
528 dent = kmalloc(zbr->len, GFP_NOFS);
529 if (!dent)
530 return -ENOMEM;
531
532 err = ubifs_tnc_read_node(c, zbr, dent);
533 if (err)
534 goto out_free;
535
536 /* Add the node to the leaf node cache */
537 err = lnc_add_directly(c, zbr, dent);
538 if (err)
539 goto out_free;
540 } else
541 dent = zbr->leaf;
542
543 nlen = le16_to_cpu(dent->nlen);
544 err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len));
545 if (err == 0) {
546 if (nlen == nm->len)
547 return NAME_MATCHES;
548 else if (nlen < nm->len)
549 return NAME_LESS;
550 else
551 return NAME_GREATER;
552 } else if (err < 0)
553 return NAME_LESS;
554 else
555 return NAME_GREATER;
556
557out_free:
558 kfree(dent);
559 return err;
560}
561
562/**
563 * get_znode - get a TNC znode that may not be loaded yet.
564 * @c: UBIFS file-system description object
565 * @znode: parent znode
566 * @n: znode branch slot number
567 *
568 * This function returns the znode or a negative error code.
569 */
570static struct ubifs_znode *get_znode(struct ubifs_info *c,
571 struct ubifs_znode *znode, int n)
572{
573 struct ubifs_zbranch *zbr;
574
575 zbr = &znode->zbranch[n];
576 if (zbr->znode)
577 znode = zbr->znode;
578 else
579 znode = ubifs_load_znode(c, zbr, znode, n);
580 return znode;
581}
582
583/**
584 * tnc_next - find next TNC entry.
585 * @c: UBIFS file-system description object
586 * @zn: znode is passed and returned here
587 * @n: znode branch slot number is passed and returned here
588 *
589 * This function returns %0 if the next TNC entry is found, %-ENOENT if there is
590 * no next entry, or a negative error code otherwise.
591 */
592static int tnc_next(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
593{
594 struct ubifs_znode *znode = *zn;
595 int nn = *n;
596
597 nn += 1;
598 if (nn < znode->child_cnt) {
599 *n = nn;
600 return 0;
601 }
602 while (1) {
603 struct ubifs_znode *zp;
604
605 zp = znode->parent;
606 if (!zp)
607 return -ENOENT;
608 nn = znode->iip + 1;
609 znode = zp;
610 if (nn < znode->child_cnt) {
611 znode = get_znode(c, znode, nn);
612 if (IS_ERR(znode))
613 return PTR_ERR(znode);
614 while (znode->level != 0) {
615 znode = get_znode(c, znode, 0);
616 if (IS_ERR(znode))
617 return PTR_ERR(znode);
618 }
619 nn = 0;
620 break;
621 }
622 }
623 *zn = znode;
624 *n = nn;
625 return 0;
626}
627
628/**
629 * tnc_prev - find previous TNC entry.
630 * @c: UBIFS file-system description object
631 * @zn: znode is returned here
632 * @n: znode branch slot number is passed and returned here
633 *
634 * This function returns %0 if the previous TNC entry is found, %-ENOENT if
635 * there is no next entry, or a negative error code otherwise.
636 */
637static int tnc_prev(struct ubifs_info *c, struct ubifs_znode **zn, int *n)
638{
639 struct ubifs_znode *znode = *zn;
640 int nn = *n;
641
642 if (nn > 0) {
643 *n = nn - 1;
644 return 0;
645 }
646 while (1) {
647 struct ubifs_znode *zp;
648
649 zp = znode->parent;
650 if (!zp)
651 return -ENOENT;
652 nn = znode->iip - 1;
653 znode = zp;
654 if (nn >= 0) {
655 znode = get_znode(c, znode, nn);
656 if (IS_ERR(znode))
657 return PTR_ERR(znode);
658 while (znode->level != 0) {
659 nn = znode->child_cnt - 1;
660 znode = get_znode(c, znode, nn);
661 if (IS_ERR(znode))
662 return PTR_ERR(znode);
663 }
664 nn = znode->child_cnt - 1;
665 break;
666 }
667 }
668 *zn = znode;
669 *n = nn;
670 return 0;
671}
672
673/**
674 * resolve_collision - resolve a collision.
675 * @c: UBIFS file-system description object
676 * @key: key of a directory or extended attribute entry
677 * @zn: znode is returned here
678 * @n: zbranch number is passed and returned here
679 * @nm: name of the entry
680 *
681 * This function is called for "hashed" keys to make sure that the found key
682 * really corresponds to the looked up node (directory or extended attribute
683 * entry). It returns %1 and sets @zn and @n if the collision is resolved.
684 * %0 is returned if @nm is not found and @zn and @n are set to the previous
685 * entry, i.e. to the entry after which @nm could follow if it were in TNC.
686 * This means that @n may be set to %-1 if the leftmost key in @zn is the
687 * previous one. A negative error code is returned on failures.
688 */
689static int resolve_collision(struct ubifs_info *c, const union ubifs_key *key,
690 struct ubifs_znode **zn, int *n,
691 const struct qstr *nm)
692{
693 int err;
694
695 err = matches_name(c, &(*zn)->zbranch[*n], nm);
696 if (unlikely(err < 0))
697 return err;
698 if (err == NAME_MATCHES)
699 return 1;
700
701 if (err == NAME_GREATER) {
702 /* Look left */
703 while (1) {
704 err = tnc_prev(c, zn, n);
705 if (err == -ENOENT) {
706 ubifs_assert(*n == 0);
707 *n = -1;
708 return 0;
709 }
710 if (err < 0)
711 return err;
712 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
713 /*
714 * We have found the branch after which we would
715 * like to insert, but inserting in this znode
716 * may still be wrong. Consider the following 3
717 * znodes, in the case where we are resolving a
718 * collision with Key2.
719 *
720 * znode zp
721 * ----------------------
722 * level 1 | Key0 | Key1 |
723 * -----------------------
724 * | |
725 * znode za | | znode zb
726 * ------------ ------------
727 * level 0 | Key0 | | Key2 |
728 * ------------ ------------
729 *
730 * The lookup finds Key2 in znode zb. Lets say
731 * there is no match and the name is greater so
732 * we look left. When we find Key0, we end up
733 * here. If we return now, we will insert into
734 * znode za at slot n = 1. But that is invalid
735 * according to the parent's keys. Key2 must
736 * be inserted into znode zb.
737 *
738 * Note, this problem is not relevant for the
739 * case when we go right, because
740 * 'tnc_insert()' would correct the parent key.
741 */
742 if (*n == (*zn)->child_cnt - 1) {
743 err = tnc_next(c, zn, n);
744 if (err) {
745 /* Should be impossible */
746 ubifs_assert(0);
747 if (err == -ENOENT)
748 err = -EINVAL;
749 return err;
750 }
751 ubifs_assert(*n == 0);
752 *n = -1;
753 }
754 return 0;
755 }
756 err = matches_name(c, &(*zn)->zbranch[*n], nm);
757 if (err < 0)
758 return err;
759 if (err == NAME_LESS)
760 return 0;
761 if (err == NAME_MATCHES)
762 return 1;
763 ubifs_assert(err == NAME_GREATER);
764 }
765 } else {
766 int nn = *n;
767 struct ubifs_znode *znode = *zn;
768
769 /* Look right */
770 while (1) {
771 err = tnc_next(c, &znode, &nn);
772 if (err == -ENOENT)
773 return 0;
774 if (err < 0)
775 return err;
776 if (keys_cmp(c, &znode->zbranch[nn].key, key))
777 return 0;
778 err = matches_name(c, &znode->zbranch[nn], nm);
779 if (err < 0)
780 return err;
781 if (err == NAME_GREATER)
782 return 0;
783 *zn = znode;
784 *n = nn;
785 if (err == NAME_MATCHES)
786 return 1;
787 ubifs_assert(err == NAME_LESS);
788 }
789 }
790}
791
792/**
793 * fallible_matches_name - determine if a dent matches a given name.
794 * @c: UBIFS file-system description object
795 * @zbr: zbranch of dent
796 * @nm: name to match
797 *
798 * This is a "fallible" version of 'matches_name()' function which does not
799 * panic if the direntry/xentry referred by @zbr does not exist on the media.
800 *
801 * This function checks if xentry/direntry referred by zbranch @zbr matches name
802 * @nm. Returns %NAME_MATCHES it does, %NAME_LESS if the name referred by @zbr
803 * is less than @nm, %NAME_GREATER if it is greater than @nm, and @NOT_ON_MEDIA
804 * if xentry/direntry referred by @zbr does not exist on the media. A negative
805 * error code is returned in case of failure.
806 */
807static int fallible_matches_name(struct ubifs_info *c,
808 struct ubifs_zbranch *zbr,
809 const struct qstr *nm)
810{
811 struct ubifs_dent_node *dent;
812 int nlen, err;
813
814 /* If possible, match against the dent in the leaf node cache */
815 if (!zbr->leaf) {
816 dent = kmalloc(zbr->len, GFP_NOFS);
817 if (!dent)
818 return -ENOMEM;
819
820 err = fallible_read_node(c, &zbr->key, zbr, dent);
821 if (err < 0)
822 goto out_free;
823 if (err == 0) {
824 /* The node was not present */
825 err = NOT_ON_MEDIA;
826 goto out_free;
827 }
828 ubifs_assert(err == 1);
829
830 err = lnc_add_directly(c, zbr, dent);
831 if (err)
832 goto out_free;
833 } else
834 dent = zbr->leaf;
835
836 nlen = le16_to_cpu(dent->nlen);
837 err = memcmp(dent->name, nm->name, min_t(int, nlen, nm->len));
838 if (err == 0) {
839 if (nlen == nm->len)
840 return NAME_MATCHES;
841 else if (nlen < nm->len)
842 return NAME_LESS;
843 else
844 return NAME_GREATER;
845 } else if (err < 0)
846 return NAME_LESS;
847 else
848 return NAME_GREATER;
849
850out_free:
851 kfree(dent);
852 return err;
853}
854
855/**
856 * fallible_resolve_collision - resolve a collision even if nodes are missing.
857 * @c: UBIFS file-system description object
858 * @key: key
859 * @zn: znode is returned here
860 * @n: branch number is passed and returned here
861 * @nm: name of directory entry
862 * @adding: indicates caller is adding a key to the TNC
863 *
864 * This is a "fallible" version of the 'resolve_collision()' function which
865 * does not panic if one of the nodes referred to by TNC does not exist on the
866 * media. This may happen when replaying the journal if a deleted node was
867 * Garbage-collected and the commit was not done. A branch that refers to a node
868 * that is not present is called a dangling branch. The following are the return
869 * codes for this function:
870 * o if @nm was found, %1 is returned and @zn and @n are set to the found
871 * branch;
872 * o if we are @adding and @nm was not found, %0 is returned;
873 * o if we are not @adding and @nm was not found, but a dangling branch was
874 * found, then %1 is returned and @zn and @n are set to the dangling branch;
875 * o a negative error code is returned in case of failure.
876 */
877static int fallible_resolve_collision(struct ubifs_info *c,
878 const union ubifs_key *key,
879 struct ubifs_znode **zn, int *n,
880 const struct qstr *nm, int adding)
881{
882 struct ubifs_znode *o_znode = NULL, *znode = *zn;
883 int uninitialized_var(o_n), err, cmp, unsure = 0, nn = *n;
884
885 cmp = fallible_matches_name(c, &znode->zbranch[nn], nm);
886 if (unlikely(cmp < 0))
887 return cmp;
888 if (cmp == NAME_MATCHES)
889 return 1;
890 if (cmp == NOT_ON_MEDIA) {
891 o_znode = znode;
892 o_n = nn;
893 /*
894 * We are unlucky and hit a dangling branch straight away.
895 * Now we do not really know where to go to find the needed
896 * branch - to the left or to the right. Well, let's try left.
897 */
898 unsure = 1;
899 } else if (!adding)
900 unsure = 1; /* Remove a dangling branch wherever it is */
901
902 if (cmp == NAME_GREATER || unsure) {
903 /* Look left */
904 while (1) {
905 err = tnc_prev(c, zn, n);
906 if (err == -ENOENT) {
907 ubifs_assert(*n == 0);
908 *n = -1;
909 break;
910 }
911 if (err < 0)
912 return err;
913 if (keys_cmp(c, &(*zn)->zbranch[*n].key, key)) {
914 /* See comments in 'resolve_collision()' */
915 if (*n == (*zn)->child_cnt - 1) {
916 err = tnc_next(c, zn, n);
917 if (err) {
918 /* Should be impossible */
919 ubifs_assert(0);
920 if (err == -ENOENT)
921 err = -EINVAL;
922 return err;
923 }
924 ubifs_assert(*n == 0);
925 *n = -1;
926 }
927 break;
928 }
929 err = fallible_matches_name(c, &(*zn)->zbranch[*n], nm);
930 if (err < 0)
931 return err;
932 if (err == NAME_MATCHES)
933 return 1;
934 if (err == NOT_ON_MEDIA) {
935 o_znode = *zn;
936 o_n = *n;
937 continue;
938 }
939 if (!adding)
940 continue;
941 if (err == NAME_LESS)
942 break;
943 else
944 unsure = 0;
945 }
946 }
947
948 if (cmp == NAME_LESS || unsure) {
949 /* Look right */
950 *zn = znode;
951 *n = nn;
952 while (1) {
953 err = tnc_next(c, &znode, &nn);
954 if (err == -ENOENT)
955 break;
956 if (err < 0)
957 return err;
958 if (keys_cmp(c, &znode->zbranch[nn].key, key))
959 break;
960 err = fallible_matches_name(c, &znode->zbranch[nn], nm);
961 if (err < 0)
962 return err;
963 if (err == NAME_GREATER)
964 break;
965 *zn = znode;
966 *n = nn;
967 if (err == NAME_MATCHES)
968 return 1;
969 if (err == NOT_ON_MEDIA) {
970 o_znode = znode;
971 o_n = nn;
972 }
973 }
974 }
975
976 /* Never match a dangling branch when adding */
977 if (adding || !o_znode)
978 return 0;
979
Heiko Schocherff94bc42014-06-24 10:10:04 +0200980 dbg_mntk(key, "dangling match LEB %d:%d len %d key ",
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100981 o_znode->zbranch[o_n].lnum, o_znode->zbranch[o_n].offs,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200982 o_znode->zbranch[o_n].len);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100983 *zn = o_znode;
984 *n = o_n;
985 return 1;
986}
987
988/**
989 * matches_position - determine if a zbranch matches a given position.
990 * @zbr: zbranch of dent
991 * @lnum: LEB number of dent to match
992 * @offs: offset of dent to match
993 *
994 * This function returns %1 if @lnum:@offs matches, and %0 otherwise.
995 */
996static int matches_position(struct ubifs_zbranch *zbr, int lnum, int offs)
997{
998 if (zbr->lnum == lnum && zbr->offs == offs)
999 return 1;
1000 else
1001 return 0;
1002}
1003
1004/**
1005 * resolve_collision_directly - resolve a collision directly.
1006 * @c: UBIFS file-system description object
1007 * @key: key of directory entry
1008 * @zn: znode is passed and returned here
1009 * @n: zbranch number is passed and returned here
1010 * @lnum: LEB number of dent node to match
1011 * @offs: offset of dent node to match
1012 *
1013 * This function is used for "hashed" keys to make sure the found directory or
1014 * extended attribute entry node is what was looked for. It is used when the
1015 * flash address of the right node is known (@lnum:@offs) which makes it much
1016 * easier to resolve collisions (no need to read entries and match full
1017 * names). This function returns %1 and sets @zn and @n if the collision is
1018 * resolved, %0 if @lnum:@offs is not found and @zn and @n are set to the
1019 * previous directory entry. Otherwise a negative error code is returned.
1020 */
1021static int resolve_collision_directly(struct ubifs_info *c,
1022 const union ubifs_key *key,
1023 struct ubifs_znode **zn, int *n,
1024 int lnum, int offs)
1025{
1026 struct ubifs_znode *znode;
1027 int nn, err;
1028
1029 znode = *zn;
1030 nn = *n;
1031 if (matches_position(&znode->zbranch[nn], lnum, offs))
1032 return 1;
1033
1034 /* Look left */
1035 while (1) {
1036 err = tnc_prev(c, &znode, &nn);
1037 if (err == -ENOENT)
1038 break;
1039 if (err < 0)
1040 return err;
1041 if (keys_cmp(c, &znode->zbranch[nn].key, key))
1042 break;
1043 if (matches_position(&znode->zbranch[nn], lnum, offs)) {
1044 *zn = znode;
1045 *n = nn;
1046 return 1;
1047 }
1048 }
1049
1050 /* Look right */
1051 znode = *zn;
1052 nn = *n;
1053 while (1) {
1054 err = tnc_next(c, &znode, &nn);
1055 if (err == -ENOENT)
1056 return 0;
1057 if (err < 0)
1058 return err;
1059 if (keys_cmp(c, &znode->zbranch[nn].key, key))
1060 return 0;
1061 *zn = znode;
1062 *n = nn;
1063 if (matches_position(&znode->zbranch[nn], lnum, offs))
1064 return 1;
1065 }
1066}
1067
1068/**
1069 * dirty_cow_bottom_up - dirty a znode and its ancestors.
1070 * @c: UBIFS file-system description object
1071 * @znode: znode to dirty
1072 *
1073 * If we do not have a unique key that resides in a znode, then we cannot
1074 * dirty that znode from the top down (i.e. by using lookup_level0_dirty)
1075 * This function records the path back to the last dirty ancestor, and then
1076 * dirties the znodes on that path.
1077 */
1078static struct ubifs_znode *dirty_cow_bottom_up(struct ubifs_info *c,
1079 struct ubifs_znode *znode)
1080{
1081 struct ubifs_znode *zp;
1082 int *path = c->bottom_up_buf, p = 0;
1083
1084 ubifs_assert(c->zroot.znode);
1085 ubifs_assert(znode);
1086 if (c->zroot.znode->level > BOTTOM_UP_HEIGHT) {
1087 kfree(c->bottom_up_buf);
1088 c->bottom_up_buf = kmalloc(c->zroot.znode->level * sizeof(int),
1089 GFP_NOFS);
1090 if (!c->bottom_up_buf)
1091 return ERR_PTR(-ENOMEM);
1092 path = c->bottom_up_buf;
1093 }
1094 if (c->zroot.znode->level) {
1095 /* Go up until parent is dirty */
1096 while (1) {
1097 int n;
1098
1099 zp = znode->parent;
1100 if (!zp)
1101 break;
1102 n = znode->iip;
1103 ubifs_assert(p < c->zroot.znode->level);
1104 path[p++] = n;
1105 if (!zp->cnext && ubifs_zn_dirty(znode))
1106 break;
1107 znode = zp;
1108 }
1109 }
1110
1111 /* Come back down, dirtying as we go */
1112 while (1) {
1113 struct ubifs_zbranch *zbr;
1114
1115 zp = znode->parent;
1116 if (zp) {
1117 ubifs_assert(path[p - 1] >= 0);
1118 ubifs_assert(path[p - 1] < zp->child_cnt);
1119 zbr = &zp->zbranch[path[--p]];
1120 znode = dirty_cow_znode(c, zbr);
1121 } else {
1122 ubifs_assert(znode == c->zroot.znode);
1123 znode = dirty_cow_znode(c, &c->zroot);
1124 }
1125 if (IS_ERR(znode) || !p)
1126 break;
1127 ubifs_assert(path[p - 1] >= 0);
1128 ubifs_assert(path[p - 1] < znode->child_cnt);
1129 znode = znode->zbranch[path[p - 1]].znode;
1130 }
1131
1132 return znode;
1133}
1134
1135/**
1136 * ubifs_lookup_level0 - search for zero-level znode.
1137 * @c: UBIFS file-system description object
1138 * @key: key to lookup
1139 * @zn: znode is returned here
1140 * @n: znode branch slot number is returned here
1141 *
1142 * This function looks up the TNC tree and search for zero-level znode which
1143 * refers key @key. The found zero-level znode is returned in @zn. There are 3
1144 * cases:
1145 * o exact match, i.e. the found zero-level znode contains key @key, then %1
1146 * is returned and slot number of the matched branch is stored in @n;
1147 * o not exact match, which means that zero-level znode does not contain
Heiko Schocherff94bc42014-06-24 10:10:04 +02001148 * @key, then %0 is returned and slot number of the closest branch is stored
1149 * in @n;
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001150 * o @key is so small that it is even less than the lowest key of the
1151 * leftmost zero-level node, then %0 is returned and %0 is stored in @n.
1152 *
1153 * Note, when the TNC tree is traversed, some znodes may be absent, then this
1154 * function reads corresponding indexing nodes and inserts them to TNC. In
1155 * case of failure, a negative error code is returned.
1156 */
1157int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key,
1158 struct ubifs_znode **zn, int *n)
1159{
1160 int err, exact;
1161 struct ubifs_znode *znode;
1162 unsigned long time = get_seconds();
1163
Heiko Schocherff94bc42014-06-24 10:10:04 +02001164 dbg_tnck(key, "search key ");
1165 ubifs_assert(key_type(c, key) < UBIFS_INVALID_KEY);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001166
1167 znode = c->zroot.znode;
1168 if (unlikely(!znode)) {
1169 znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1170 if (IS_ERR(znode))
1171 return PTR_ERR(znode);
1172 }
1173
1174 znode->time = time;
1175
1176 while (1) {
1177 struct ubifs_zbranch *zbr;
1178
1179 exact = ubifs_search_zbranch(c, znode, key, n);
1180
1181 if (znode->level == 0)
1182 break;
1183
1184 if (*n < 0)
1185 *n = 0;
1186 zbr = &znode->zbranch[*n];
1187
1188 if (zbr->znode) {
1189 znode->time = time;
1190 znode = zbr->znode;
1191 continue;
1192 }
1193
1194 /* znode is not in TNC cache, load it from the media */
1195 znode = ubifs_load_znode(c, zbr, znode, *n);
1196 if (IS_ERR(znode))
1197 return PTR_ERR(znode);
1198 }
1199
1200 *zn = znode;
1201 if (exact || !is_hash_key(c, key) || *n != -1) {
1202 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
1203 return exact;
1204 }
1205
1206 /*
1207 * Here is a tricky place. We have not found the key and this is a
1208 * "hashed" key, which may collide. The rest of the code deals with
1209 * situations like this:
1210 *
1211 * | 3 | 5 |
1212 * / \
1213 * | 3 | 5 | | 6 | 7 | (x)
1214 *
1215 * Or more a complex example:
1216 *
1217 * | 1 | 5 |
1218 * / \
1219 * | 1 | 3 | | 5 | 8 |
1220 * \ /
1221 * | 5 | 5 | | 6 | 7 | (x)
1222 *
1223 * In the examples, if we are looking for key "5", we may reach nodes
1224 * marked with "(x)". In this case what we have do is to look at the
1225 * left and see if there is "5" key there. If there is, we have to
1226 * return it.
1227 *
1228 * Note, this whole situation is possible because we allow to have
1229 * elements which are equivalent to the next key in the parent in the
1230 * children of current znode. For example, this happens if we split a
1231 * znode like this: | 3 | 5 | 5 | 6 | 7 |, which results in something
1232 * like this:
1233 * | 3 | 5 |
1234 * / \
1235 * | 3 | 5 | | 5 | 6 | 7 |
1236 * ^
1237 * And this becomes what is at the first "picture" after key "5" marked
1238 * with "^" is removed. What could be done is we could prohibit
1239 * splitting in the middle of the colliding sequence. Also, when
1240 * removing the leftmost key, we would have to correct the key of the
1241 * parent node, which would introduce additional complications. Namely,
Heiko Schocherff94bc42014-06-24 10:10:04 +02001242 * if we changed the leftmost key of the parent znode, the garbage
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001243 * collector would be unable to find it (GC is doing this when GC'ing
1244 * indexing LEBs). Although we already have an additional RB-tree where
1245 * we save such changed znodes (see 'ins_clr_old_idx_znode()') until
1246 * after the commit. But anyway, this does not look easy to implement
1247 * so we did not try this.
1248 */
1249 err = tnc_prev(c, &znode, n);
1250 if (err == -ENOENT) {
1251 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1252 *n = -1;
1253 return 0;
1254 }
1255 if (unlikely(err < 0))
1256 return err;
1257 if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
1258 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1259 *n = -1;
1260 return 0;
1261 }
1262
1263 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
1264 *zn = znode;
1265 return 1;
1266}
1267
1268/**
1269 * lookup_level0_dirty - search for zero-level znode dirtying.
1270 * @c: UBIFS file-system description object
1271 * @key: key to lookup
1272 * @zn: znode is returned here
1273 * @n: znode branch slot number is returned here
1274 *
1275 * This function looks up the TNC tree and search for zero-level znode which
1276 * refers key @key. The found zero-level znode is returned in @zn. There are 3
1277 * cases:
1278 * o exact match, i.e. the found zero-level znode contains key @key, then %1
1279 * is returned and slot number of the matched branch is stored in @n;
1280 * o not exact match, which means that zero-level znode does not contain @key
1281 * then %0 is returned and slot number of the closed branch is stored in
1282 * @n;
1283 * o @key is so small that it is even less than the lowest key of the
1284 * leftmost zero-level node, then %0 is returned and %-1 is stored in @n.
1285 *
1286 * Additionally all znodes in the path from the root to the located zero-level
1287 * znode are marked as dirty.
1288 *
1289 * Note, when the TNC tree is traversed, some znodes may be absent, then this
1290 * function reads corresponding indexing nodes and inserts them to TNC. In
1291 * case of failure, a negative error code is returned.
1292 */
1293static int lookup_level0_dirty(struct ubifs_info *c, const union ubifs_key *key,
1294 struct ubifs_znode **zn, int *n)
1295{
1296 int err, exact;
1297 struct ubifs_znode *znode;
1298 unsigned long time = get_seconds();
1299
Heiko Schocherff94bc42014-06-24 10:10:04 +02001300 dbg_tnck(key, "search and dirty key ");
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001301
1302 znode = c->zroot.znode;
1303 if (unlikely(!znode)) {
1304 znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1305 if (IS_ERR(znode))
1306 return PTR_ERR(znode);
1307 }
1308
1309 znode = dirty_cow_znode(c, &c->zroot);
1310 if (IS_ERR(znode))
1311 return PTR_ERR(znode);
1312
1313 znode->time = time;
1314
1315 while (1) {
1316 struct ubifs_zbranch *zbr;
1317
1318 exact = ubifs_search_zbranch(c, znode, key, n);
1319
1320 if (znode->level == 0)
1321 break;
1322
1323 if (*n < 0)
1324 *n = 0;
1325 zbr = &znode->zbranch[*n];
1326
1327 if (zbr->znode) {
1328 znode->time = time;
1329 znode = dirty_cow_znode(c, zbr);
1330 if (IS_ERR(znode))
1331 return PTR_ERR(znode);
1332 continue;
1333 }
1334
1335 /* znode is not in TNC cache, load it from the media */
1336 znode = ubifs_load_znode(c, zbr, znode, *n);
1337 if (IS_ERR(znode))
1338 return PTR_ERR(znode);
1339 znode = dirty_cow_znode(c, zbr);
1340 if (IS_ERR(znode))
1341 return PTR_ERR(znode);
1342 }
1343
1344 *zn = znode;
1345 if (exact || !is_hash_key(c, key) || *n != -1) {
1346 dbg_tnc("found %d, lvl %d, n %d", exact, znode->level, *n);
1347 return exact;
1348 }
1349
1350 /*
1351 * See huge comment at 'lookup_level0_dirty()' what is the rest of the
1352 * code.
1353 */
1354 err = tnc_prev(c, &znode, n);
1355 if (err == -ENOENT) {
1356 *n = -1;
1357 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1358 return 0;
1359 }
1360 if (unlikely(err < 0))
1361 return err;
1362 if (keys_cmp(c, key, &znode->zbranch[*n].key)) {
1363 *n = -1;
1364 dbg_tnc("found 0, lvl %d, n -1", znode->level);
1365 return 0;
1366 }
1367
1368 if (znode->cnext || !ubifs_zn_dirty(znode)) {
1369 znode = dirty_cow_bottom_up(c, znode);
1370 if (IS_ERR(znode))
1371 return PTR_ERR(znode);
1372 }
1373
1374 dbg_tnc("found 1, lvl %d, n %d", znode->level, *n);
1375 *zn = znode;
1376 return 1;
1377}
1378
1379/**
1380 * maybe_leb_gced - determine if a LEB may have been garbage collected.
1381 * @c: UBIFS file-system description object
1382 * @lnum: LEB number
1383 * @gc_seq1: garbage collection sequence number
1384 *
1385 * This function determines if @lnum may have been garbage collected since
1386 * sequence number @gc_seq1. If it may have been then %1 is returned, otherwise
1387 * %0 is returned.
1388 */
1389static int maybe_leb_gced(struct ubifs_info *c, int lnum, int gc_seq1)
1390{
Heiko Schocherff94bc42014-06-24 10:10:04 +02001391#ifndef __UBOOT__
1392 int gc_seq2, gced_lnum;
1393
1394 gced_lnum = c->gced_lnum;
1395 smp_rmb();
1396 gc_seq2 = c->gc_seq;
1397 /* Same seq means no GC */
1398 if (gc_seq1 == gc_seq2)
1399 return 0;
1400 /* Different by more than 1 means we don't know */
1401 if (gc_seq1 + 1 != gc_seq2)
1402 return 1;
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001403 /*
Heiko Schocherff94bc42014-06-24 10:10:04 +02001404 * We have seen the sequence number has increased by 1. Now we need to
1405 * be sure we read the right LEB number, so read it again.
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001406 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001407 smp_rmb();
1408 if (gced_lnum != c->gced_lnum)
1409 return 1;
1410 /* Finally we can check lnum */
1411 if (gced_lnum == lnum)
1412 return 1;
1413#else
1414 /* No garbage collection in the read-only U-Boot implementation */
1415#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001416 return 0;
1417}
1418
1419/**
1420 * ubifs_tnc_locate - look up a file-system node and return it and its location.
1421 * @c: UBIFS file-system description object
1422 * @key: node key to lookup
1423 * @node: the node is returned here
1424 * @lnum: LEB number is returned here
1425 * @offs: offset is returned here
1426 *
Heiko Schocherff94bc42014-06-24 10:10:04 +02001427 * This function looks up and reads node with key @key. The caller has to make
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001428 * sure the @node buffer is large enough to fit the node. Returns zero in case
1429 * of success, %-ENOENT if the node was not found, and a negative error code in
1430 * case of failure. The node location can be returned in @lnum and @offs.
1431 */
1432int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key,
1433 void *node, int *lnum, int *offs)
1434{
1435 int found, n, err, safely = 0, gc_seq1;
1436 struct ubifs_znode *znode;
1437 struct ubifs_zbranch zbr, *zt;
1438
1439again:
1440 mutex_lock(&c->tnc_mutex);
1441 found = ubifs_lookup_level0(c, key, &znode, &n);
1442 if (!found) {
1443 err = -ENOENT;
1444 goto out;
1445 } else if (found < 0) {
1446 err = found;
1447 goto out;
1448 }
1449 zt = &znode->zbranch[n];
1450 if (lnum) {
1451 *lnum = zt->lnum;
1452 *offs = zt->offs;
1453 }
1454 if (is_hash_key(c, key)) {
1455 /*
1456 * In this case the leaf node cache gets used, so we pass the
1457 * address of the zbranch and keep the mutex locked
1458 */
1459 err = tnc_read_node_nm(c, zt, node);
1460 goto out;
1461 }
1462 if (safely) {
1463 err = ubifs_tnc_read_node(c, zt, node);
1464 goto out;
1465 }
1466 /* Drop the TNC mutex prematurely and race with garbage collection */
1467 zbr = znode->zbranch[n];
1468 gc_seq1 = c->gc_seq;
1469 mutex_unlock(&c->tnc_mutex);
1470
Heiko Schocherff94bc42014-06-24 10:10:04 +02001471 if (ubifs_get_wbuf(c, zbr.lnum)) {
1472 /* We do not GC journal heads */
1473 err = ubifs_tnc_read_node(c, &zbr, node);
1474 return err;
1475 }
1476
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001477 err = fallible_read_node(c, key, &zbr, node);
1478 if (err <= 0 || maybe_leb_gced(c, zbr.lnum, gc_seq1)) {
1479 /*
1480 * The node may have been GC'ed out from under us so try again
1481 * while keeping the TNC mutex locked.
1482 */
1483 safely = 1;
1484 goto again;
1485 }
1486 return 0;
1487
1488out:
1489 mutex_unlock(&c->tnc_mutex);
1490 return err;
1491}
1492
1493/**
1494 * ubifs_tnc_get_bu_keys - lookup keys for bulk-read.
1495 * @c: UBIFS file-system description object
1496 * @bu: bulk-read parameters and results
1497 *
1498 * Lookup consecutive data node keys for the same inode that reside
1499 * consecutively in the same LEB. This function returns zero in case of success
1500 * and a negative error code in case of failure.
1501 *
1502 * Note, if the bulk-read buffer length (@bu->buf_len) is known, this function
1503 * makes sure bulk-read nodes fit the buffer. Otherwise, this function prepares
1504 * maximum possible amount of nodes for bulk-read.
1505 */
1506int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu)
1507{
1508 int n, err = 0, lnum = -1, uninitialized_var(offs);
1509 int uninitialized_var(len);
1510 unsigned int block = key_block(c, &bu->key);
1511 struct ubifs_znode *znode;
1512
1513 bu->cnt = 0;
1514 bu->blk_cnt = 0;
1515 bu->eof = 0;
1516
1517 mutex_lock(&c->tnc_mutex);
1518 /* Find first key */
1519 err = ubifs_lookup_level0(c, &bu->key, &znode, &n);
1520 if (err < 0)
1521 goto out;
1522 if (err) {
1523 /* Key found */
1524 len = znode->zbranch[n].len;
1525 /* The buffer must be big enough for at least 1 node */
1526 if (len > bu->buf_len) {
1527 err = -EINVAL;
1528 goto out;
1529 }
1530 /* Add this key */
1531 bu->zbranch[bu->cnt++] = znode->zbranch[n];
1532 bu->blk_cnt += 1;
1533 lnum = znode->zbranch[n].lnum;
1534 offs = ALIGN(znode->zbranch[n].offs + len, 8);
1535 }
1536 while (1) {
1537 struct ubifs_zbranch *zbr;
1538 union ubifs_key *key;
1539 unsigned int next_block;
1540
1541 /* Find next key */
1542 err = tnc_next(c, &znode, &n);
1543 if (err)
1544 goto out;
1545 zbr = &znode->zbranch[n];
1546 key = &zbr->key;
1547 /* See if there is another data key for this file */
1548 if (key_inum(c, key) != key_inum(c, &bu->key) ||
1549 key_type(c, key) != UBIFS_DATA_KEY) {
1550 err = -ENOENT;
1551 goto out;
1552 }
1553 if (lnum < 0) {
1554 /* First key found */
1555 lnum = zbr->lnum;
1556 offs = ALIGN(zbr->offs + zbr->len, 8);
1557 len = zbr->len;
1558 if (len > bu->buf_len) {
1559 err = -EINVAL;
1560 goto out;
1561 }
1562 } else {
1563 /*
1564 * The data nodes must be in consecutive positions in
1565 * the same LEB.
1566 */
1567 if (zbr->lnum != lnum || zbr->offs != offs)
1568 goto out;
1569 offs += ALIGN(zbr->len, 8);
1570 len = ALIGN(len, 8) + zbr->len;
1571 /* Must not exceed buffer length */
1572 if (len > bu->buf_len)
1573 goto out;
1574 }
1575 /* Allow for holes */
1576 next_block = key_block(c, key);
1577 bu->blk_cnt += (next_block - block - 1);
1578 if (bu->blk_cnt >= UBIFS_MAX_BULK_READ)
1579 goto out;
1580 block = next_block;
1581 /* Add this key */
1582 bu->zbranch[bu->cnt++] = *zbr;
1583 bu->blk_cnt += 1;
1584 /* See if we have room for more */
1585 if (bu->cnt >= UBIFS_MAX_BULK_READ)
1586 goto out;
1587 if (bu->blk_cnt >= UBIFS_MAX_BULK_READ)
1588 goto out;
1589 }
1590out:
1591 if (err == -ENOENT) {
1592 bu->eof = 1;
1593 err = 0;
1594 }
1595 bu->gc_seq = c->gc_seq;
1596 mutex_unlock(&c->tnc_mutex);
1597 if (err)
1598 return err;
1599 /*
1600 * An enormous hole could cause bulk-read to encompass too many
1601 * page cache pages, so limit the number here.
1602 */
1603 if (bu->blk_cnt > UBIFS_MAX_BULK_READ)
1604 bu->blk_cnt = UBIFS_MAX_BULK_READ;
1605 /*
1606 * Ensure that bulk-read covers a whole number of page cache
1607 * pages.
1608 */
1609 if (UBIFS_BLOCKS_PER_PAGE == 1 ||
1610 !(bu->blk_cnt & (UBIFS_BLOCKS_PER_PAGE - 1)))
1611 return 0;
1612 if (bu->eof) {
1613 /* At the end of file we can round up */
1614 bu->blk_cnt += UBIFS_BLOCKS_PER_PAGE - 1;
1615 return 0;
1616 }
1617 /* Exclude data nodes that do not make up a whole page cache page */
1618 block = key_block(c, &bu->key) + bu->blk_cnt;
1619 block &= ~(UBIFS_BLOCKS_PER_PAGE - 1);
1620 while (bu->cnt) {
1621 if (key_block(c, &bu->zbranch[bu->cnt - 1].key) < block)
1622 break;
1623 bu->cnt -= 1;
1624 }
1625 return 0;
1626}
1627
1628/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001629 * read_wbuf - bulk-read from a LEB with a wbuf.
1630 * @wbuf: wbuf that may overlap the read
1631 * @buf: buffer into which to read
1632 * @len: read length
1633 * @lnum: LEB number from which to read
1634 * @offs: offset from which to read
1635 *
1636 * This functions returns %0 on success or a negative error code on failure.
1637 */
1638static int read_wbuf(struct ubifs_wbuf *wbuf, void *buf, int len, int lnum,
1639 int offs)
1640{
1641 const struct ubifs_info *c = wbuf->c;
1642 int rlen, overlap;
1643
1644 dbg_io("LEB %d:%d, length %d", lnum, offs, len);
1645 ubifs_assert(wbuf && lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
1646 ubifs_assert(!(offs & 7) && offs < c->leb_size);
1647 ubifs_assert(offs + len <= c->leb_size);
1648
1649 spin_lock(&wbuf->lock);
1650 overlap = (lnum == wbuf->lnum && offs + len > wbuf->offs);
1651 if (!overlap) {
1652 /* We may safely unlock the write-buffer and read the data */
1653 spin_unlock(&wbuf->lock);
1654 return ubifs_leb_read(c, lnum, buf, offs, len, 0);
1655 }
1656
1657 /* Don't read under wbuf */
1658 rlen = wbuf->offs - offs;
1659 if (rlen < 0)
1660 rlen = 0;
1661
1662 /* Copy the rest from the write-buffer */
1663 memcpy(buf + rlen, wbuf->buf + offs + rlen - wbuf->offs, len - rlen);
1664 spin_unlock(&wbuf->lock);
1665
1666 if (rlen > 0)
1667 /* Read everything that goes before write-buffer */
1668 return ubifs_leb_read(c, lnum, buf, offs, rlen, 0);
1669
1670 return 0;
1671}
1672
1673/**
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001674 * validate_data_node - validate data nodes for bulk-read.
1675 * @c: UBIFS file-system description object
1676 * @buf: buffer containing data node to validate
1677 * @zbr: zbranch of data node to validate
1678 *
1679 * This functions returns %0 on success or a negative error code on failure.
1680 */
1681static int validate_data_node(struct ubifs_info *c, void *buf,
1682 struct ubifs_zbranch *zbr)
1683{
1684 union ubifs_key key1;
1685 struct ubifs_ch *ch = buf;
1686 int err, len;
1687
1688 if (ch->node_type != UBIFS_DATA_NODE) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001689 ubifs_err(c, "bad node type (%d but expected %d)",
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001690 ch->node_type, UBIFS_DATA_NODE);
1691 goto out_err;
1692 }
1693
1694 err = ubifs_check_node(c, buf, zbr->lnum, zbr->offs, 0, 0);
1695 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001696 ubifs_err(c, "expected node type %d", UBIFS_DATA_NODE);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001697 goto out;
1698 }
1699
1700 len = le32_to_cpu(ch->len);
1701 if (len != zbr->len) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001702 ubifs_err(c, "bad node length %d, expected %d", len, zbr->len);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001703 goto out_err;
1704 }
1705
1706 /* Make sure the key of the read node is correct */
1707 key_read(c, buf + UBIFS_KEY_OFFSET, &key1);
1708 if (!keys_eq(c, &zbr->key, &key1)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001709 ubifs_err(c, "bad key in node at LEB %d:%d",
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001710 zbr->lnum, zbr->offs);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001711 dbg_tnck(&zbr->key, "looked for key ");
1712 dbg_tnck(&key1, "found node's key ");
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001713 goto out_err;
1714 }
1715
1716 return 0;
1717
1718out_err:
1719 err = -EINVAL;
1720out:
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001721 ubifs_err(c, "bad node at LEB %d:%d", zbr->lnum, zbr->offs);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001722 ubifs_dump_node(c, buf);
1723 dump_stack();
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001724 return err;
1725}
1726
1727/**
1728 * ubifs_tnc_bulk_read - read a number of data nodes in one go.
1729 * @c: UBIFS file-system description object
1730 * @bu: bulk-read parameters and results
1731 *
1732 * This functions reads and validates the data nodes that were identified by the
1733 * 'ubifs_tnc_get_bu_keys()' function. This functions returns %0 on success,
1734 * -EAGAIN to indicate a race with GC, or another negative error code on
1735 * failure.
1736 */
1737int ubifs_tnc_bulk_read(struct ubifs_info *c, struct bu_info *bu)
1738{
1739 int lnum = bu->zbranch[0].lnum, offs = bu->zbranch[0].offs, len, err, i;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001740 struct ubifs_wbuf *wbuf;
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001741 void *buf;
1742
1743 len = bu->zbranch[bu->cnt - 1].offs;
1744 len += bu->zbranch[bu->cnt - 1].len - offs;
1745 if (len > bu->buf_len) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001746 ubifs_err(c, "buffer too small %d vs %d", bu->buf_len, len);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001747 return -EINVAL;
1748 }
1749
1750 /* Do the read */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001751 wbuf = ubifs_get_wbuf(c, lnum);
1752 if (wbuf)
1753 err = read_wbuf(wbuf, bu->buf, len, lnum, offs);
1754 else
1755 err = ubifs_leb_read(c, lnum, bu->buf, offs, len, 0);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001756
1757 /* Check for a race with GC */
1758 if (maybe_leb_gced(c, lnum, bu->gc_seq))
1759 return -EAGAIN;
1760
1761 if (err && err != -EBADMSG) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001762 ubifs_err(c, "failed to read from LEB %d:%d, error %d",
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001763 lnum, offs, err);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001764 dump_stack();
1765 dbg_tnck(&bu->key, "key ");
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001766 return err;
1767 }
1768
1769 /* Validate the nodes read */
1770 buf = bu->buf;
1771 for (i = 0; i < bu->cnt; i++) {
1772 err = validate_data_node(c, buf, &bu->zbranch[i]);
1773 if (err)
1774 return err;
1775 buf = buf + ALIGN(bu->zbranch[i].len, 8);
1776 }
1777
1778 return 0;
1779}
1780
1781/**
1782 * do_lookup_nm- look up a "hashed" node.
1783 * @c: UBIFS file-system description object
1784 * @key: node key to lookup
1785 * @node: the node is returned here
1786 * @nm: node name
1787 *
1788 * This function look up and reads a node which contains name hash in the key.
1789 * Since the hash may have collisions, there may be many nodes with the same
1790 * key, so we have to sequentially look to all of them until the needed one is
1791 * found. This function returns zero in case of success, %-ENOENT if the node
1792 * was not found, and a negative error code in case of failure.
1793 */
1794static int do_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1795 void *node, const struct qstr *nm)
1796{
1797 int found, n, err;
1798 struct ubifs_znode *znode;
1799
Heiko Schocherff94bc42014-06-24 10:10:04 +02001800 dbg_tnck(key, "name '%.*s' key ", nm->len, nm->name);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001801 mutex_lock(&c->tnc_mutex);
1802 found = ubifs_lookup_level0(c, key, &znode, &n);
1803 if (!found) {
1804 err = -ENOENT;
1805 goto out_unlock;
1806 } else if (found < 0) {
1807 err = found;
1808 goto out_unlock;
1809 }
1810
1811 ubifs_assert(n >= 0);
1812
1813 err = resolve_collision(c, key, &znode, &n, nm);
1814 dbg_tnc("rc returned %d, znode %p, n %d", err, znode, n);
1815 if (unlikely(err < 0))
1816 goto out_unlock;
1817 if (err == 0) {
1818 err = -ENOENT;
1819 goto out_unlock;
1820 }
1821
1822 err = tnc_read_node_nm(c, &znode->zbranch[n], node);
1823
1824out_unlock:
1825 mutex_unlock(&c->tnc_mutex);
1826 return err;
1827}
1828
1829/**
1830 * ubifs_tnc_lookup_nm - look up a "hashed" node.
1831 * @c: UBIFS file-system description object
1832 * @key: node key to lookup
1833 * @node: the node is returned here
1834 * @nm: node name
1835 *
1836 * This function look up and reads a node which contains name hash in the key.
1837 * Since the hash may have collisions, there may be many nodes with the same
1838 * key, so we have to sequentially look to all of them until the needed one is
1839 * found. This function returns zero in case of success, %-ENOENT if the node
1840 * was not found, and a negative error code in case of failure.
1841 */
1842int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1843 void *node, const struct qstr *nm)
1844{
1845 int err, len;
1846 const struct ubifs_dent_node *dent = node;
1847
1848 /*
1849 * We assume that in most of the cases there are no name collisions and
1850 * 'ubifs_tnc_lookup()' returns us the right direntry.
1851 */
1852 err = ubifs_tnc_lookup(c, key, node);
1853 if (err)
1854 return err;
1855
1856 len = le16_to_cpu(dent->nlen);
1857 if (nm->len == len && !memcmp(dent->name, nm->name, len))
1858 return 0;
1859
1860 /*
1861 * Unluckily, there are hash collisions and we have to iterate over
1862 * them look at each direntry with colliding name hash sequentially.
1863 */
1864 return do_lookup_nm(c, key, node, nm);
1865}
1866
1867/**
1868 * correct_parent_keys - correct parent znodes' keys.
1869 * @c: UBIFS file-system description object
1870 * @znode: znode to correct parent znodes for
1871 *
1872 * This is a helper function for 'tnc_insert()'. When the key of the leftmost
1873 * zbranch changes, keys of parent znodes have to be corrected. This helper
1874 * function is called in such situations and corrects the keys if needed.
1875 */
1876static void correct_parent_keys(const struct ubifs_info *c,
1877 struct ubifs_znode *znode)
1878{
1879 union ubifs_key *key, *key1;
1880
1881 ubifs_assert(znode->parent);
1882 ubifs_assert(znode->iip == 0);
1883
1884 key = &znode->zbranch[0].key;
1885 key1 = &znode->parent->zbranch[0].key;
1886
1887 while (keys_cmp(c, key, key1) < 0) {
1888 key_copy(c, key, key1);
1889 znode = znode->parent;
1890 znode->alt = 1;
1891 if (!znode->parent || znode->iip)
1892 break;
1893 key1 = &znode->parent->zbranch[0].key;
1894 }
1895}
1896
1897/**
1898 * insert_zbranch - insert a zbranch into a znode.
1899 * @znode: znode into which to insert
1900 * @zbr: zbranch to insert
1901 * @n: slot number to insert to
1902 *
1903 * This is a helper function for 'tnc_insert()'. UBIFS does not allow "gaps" in
1904 * znode's array of zbranches and keeps zbranches consolidated, so when a new
1905 * zbranch has to be inserted to the @znode->zbranches[]' array at the @n-th
1906 * slot, zbranches starting from @n have to be moved right.
1907 */
1908static void insert_zbranch(struct ubifs_znode *znode,
1909 const struct ubifs_zbranch *zbr, int n)
1910{
1911 int i;
1912
1913 ubifs_assert(ubifs_zn_dirty(znode));
1914
1915 if (znode->level) {
1916 for (i = znode->child_cnt; i > n; i--) {
1917 znode->zbranch[i] = znode->zbranch[i - 1];
1918 if (znode->zbranch[i].znode)
1919 znode->zbranch[i].znode->iip = i;
1920 }
1921 if (zbr->znode)
1922 zbr->znode->iip = n;
1923 } else
1924 for (i = znode->child_cnt; i > n; i--)
1925 znode->zbranch[i] = znode->zbranch[i - 1];
1926
1927 znode->zbranch[n] = *zbr;
1928 znode->child_cnt += 1;
1929
1930 /*
1931 * After inserting at slot zero, the lower bound of the key range of
1932 * this znode may have changed. If this znode is subsequently split
1933 * then the upper bound of the key range may change, and furthermore
1934 * it could change to be lower than the original lower bound. If that
1935 * happens, then it will no longer be possible to find this znode in the
1936 * TNC using the key from the index node on flash. That is bad because
1937 * if it is not found, we will assume it is obsolete and may overwrite
1938 * it. Then if there is an unclean unmount, we will start using the
1939 * old index which will be broken.
1940 *
1941 * So we first mark znodes that have insertions at slot zero, and then
1942 * if they are split we add their lnum/offs to the old_idx tree.
1943 */
1944 if (n == 0)
1945 znode->alt = 1;
1946}
1947
1948/**
1949 * tnc_insert - insert a node into TNC.
1950 * @c: UBIFS file-system description object
1951 * @znode: znode to insert into
1952 * @zbr: branch to insert
1953 * @n: slot number to insert new zbranch to
1954 *
1955 * This function inserts a new node described by @zbr into znode @znode. If
1956 * znode does not have a free slot for new zbranch, it is split. Parent znodes
1957 * are splat as well if needed. Returns zero in case of success or a negative
1958 * error code in case of failure.
1959 */
1960static int tnc_insert(struct ubifs_info *c, struct ubifs_znode *znode,
1961 struct ubifs_zbranch *zbr, int n)
1962{
1963 struct ubifs_znode *zn, *zi, *zp;
1964 int i, keep, move, appending = 0;
1965 union ubifs_key *key = &zbr->key, *key1;
1966
1967 ubifs_assert(n >= 0 && n <= c->fanout);
1968
1969 /* Implement naive insert for now */
1970again:
1971 zp = znode->parent;
1972 if (znode->child_cnt < c->fanout) {
1973 ubifs_assert(n != c->fanout);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001974 dbg_tnck(key, "inserted at %d level %d, key ", n, znode->level);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001975
1976 insert_zbranch(znode, zbr, n);
1977
1978 /* Ensure parent's key is correct */
1979 if (n == 0 && zp && znode->iip == 0)
1980 correct_parent_keys(c, znode);
1981
1982 return 0;
1983 }
1984
1985 /*
1986 * Unfortunately, @znode does not have more empty slots and we have to
1987 * split it.
1988 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001989 dbg_tnck(key, "splitting level %d, key ", znode->level);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001990
1991 if (znode->alt)
1992 /*
1993 * We can no longer be sure of finding this znode by key, so we
1994 * record it in the old_idx tree.
1995 */
1996 ins_clr_old_idx_znode(c, znode);
1997
1998 zn = kzalloc(c->max_znode_sz, GFP_NOFS);
1999 if (!zn)
2000 return -ENOMEM;
2001 zn->parent = zp;
2002 zn->level = znode->level;
2003
2004 /* Decide where to split */
2005 if (znode->level == 0 && key_type(c, key) == UBIFS_DATA_KEY) {
2006 /* Try not to split consecutive data keys */
2007 if (n == c->fanout) {
2008 key1 = &znode->zbranch[n - 1].key;
2009 if (key_inum(c, key1) == key_inum(c, key) &&
2010 key_type(c, key1) == UBIFS_DATA_KEY)
2011 appending = 1;
2012 } else
2013 goto check_split;
2014 } else if (appending && n != c->fanout) {
2015 /* Try not to split consecutive data keys */
2016 appending = 0;
2017check_split:
2018 if (n >= (c->fanout + 1) / 2) {
2019 key1 = &znode->zbranch[0].key;
2020 if (key_inum(c, key1) == key_inum(c, key) &&
2021 key_type(c, key1) == UBIFS_DATA_KEY) {
2022 key1 = &znode->zbranch[n].key;
2023 if (key_inum(c, key1) != key_inum(c, key) ||
2024 key_type(c, key1) != UBIFS_DATA_KEY) {
2025 keep = n;
2026 move = c->fanout - keep;
2027 zi = znode;
2028 goto do_split;
2029 }
2030 }
2031 }
2032 }
2033
2034 if (appending) {
2035 keep = c->fanout;
2036 move = 0;
2037 } else {
2038 keep = (c->fanout + 1) / 2;
2039 move = c->fanout - keep;
2040 }
2041
2042 /*
2043 * Although we don't at present, we could look at the neighbors and see
2044 * if we can move some zbranches there.
2045 */
2046
2047 if (n < keep) {
2048 /* Insert into existing znode */
2049 zi = znode;
2050 move += 1;
2051 keep -= 1;
2052 } else {
2053 /* Insert into new znode */
2054 zi = zn;
2055 n -= keep;
2056 /* Re-parent */
2057 if (zn->level != 0)
2058 zbr->znode->parent = zn;
2059 }
2060
2061do_split:
2062
2063 __set_bit(DIRTY_ZNODE, &zn->flags);
2064 atomic_long_inc(&c->dirty_zn_cnt);
2065
2066 zn->child_cnt = move;
2067 znode->child_cnt = keep;
2068
2069 dbg_tnc("moving %d, keeping %d", move, keep);
2070
2071 /* Move zbranch */
2072 for (i = 0; i < move; i++) {
2073 zn->zbranch[i] = znode->zbranch[keep + i];
2074 /* Re-parent */
2075 if (zn->level != 0)
2076 if (zn->zbranch[i].znode) {
2077 zn->zbranch[i].znode->parent = zn;
2078 zn->zbranch[i].znode->iip = i;
2079 }
2080 }
2081
2082 /* Insert new key and branch */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002083 dbg_tnck(key, "inserting at %d level %d, key ", n, zn->level);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002084
2085 insert_zbranch(zi, zbr, n);
2086
2087 /* Insert new znode (produced by spitting) into the parent */
2088 if (zp) {
2089 if (n == 0 && zi == znode && znode->iip == 0)
2090 correct_parent_keys(c, znode);
2091
2092 /* Locate insertion point */
2093 n = znode->iip + 1;
2094
2095 /* Tail recursion */
2096 zbr->key = zn->zbranch[0].key;
2097 zbr->znode = zn;
2098 zbr->lnum = 0;
2099 zbr->offs = 0;
2100 zbr->len = 0;
2101 znode = zp;
2102
2103 goto again;
2104 }
2105
2106 /* We have to split root znode */
2107 dbg_tnc("creating new zroot at level %d", znode->level + 1);
2108
2109 zi = kzalloc(c->max_znode_sz, GFP_NOFS);
2110 if (!zi)
2111 return -ENOMEM;
2112
2113 zi->child_cnt = 2;
2114 zi->level = znode->level + 1;
2115
2116 __set_bit(DIRTY_ZNODE, &zi->flags);
2117 atomic_long_inc(&c->dirty_zn_cnt);
2118
2119 zi->zbranch[0].key = znode->zbranch[0].key;
2120 zi->zbranch[0].znode = znode;
2121 zi->zbranch[0].lnum = c->zroot.lnum;
2122 zi->zbranch[0].offs = c->zroot.offs;
2123 zi->zbranch[0].len = c->zroot.len;
2124 zi->zbranch[1].key = zn->zbranch[0].key;
2125 zi->zbranch[1].znode = zn;
2126
2127 c->zroot.lnum = 0;
2128 c->zroot.offs = 0;
2129 c->zroot.len = 0;
2130 c->zroot.znode = zi;
2131
2132 zn->parent = zi;
2133 zn->iip = 1;
2134 znode->parent = zi;
2135 znode->iip = 0;
2136
2137 return 0;
2138}
2139
2140/**
2141 * ubifs_tnc_add - add a node to TNC.
2142 * @c: UBIFS file-system description object
2143 * @key: key to add
2144 * @lnum: LEB number of node
2145 * @offs: node offset
2146 * @len: node length
2147 *
2148 * This function adds a node with key @key to TNC. The node may be new or it may
2149 * obsolete some existing one. Returns %0 on success or negative error code on
2150 * failure.
2151 */
2152int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum,
2153 int offs, int len)
2154{
2155 int found, n, err = 0;
2156 struct ubifs_znode *znode;
2157
2158 mutex_lock(&c->tnc_mutex);
Heiko Schocherff94bc42014-06-24 10:10:04 +02002159 dbg_tnck(key, "%d:%d, len %d, key ", lnum, offs, len);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002160 found = lookup_level0_dirty(c, key, &znode, &n);
2161 if (!found) {
2162 struct ubifs_zbranch zbr;
2163
2164 zbr.znode = NULL;
2165 zbr.lnum = lnum;
2166 zbr.offs = offs;
2167 zbr.len = len;
2168 key_copy(c, key, &zbr.key);
2169 err = tnc_insert(c, znode, &zbr, n + 1);
2170 } else if (found == 1) {
2171 struct ubifs_zbranch *zbr = &znode->zbranch[n];
2172
2173 lnc_free(zbr);
2174 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2175 zbr->lnum = lnum;
2176 zbr->offs = offs;
2177 zbr->len = len;
2178 } else
2179 err = found;
2180 if (!err)
2181 err = dbg_check_tnc(c, 0);
2182 mutex_unlock(&c->tnc_mutex);
2183
2184 return err;
2185}
2186
2187/**
2188 * ubifs_tnc_replace - replace a node in the TNC only if the old node is found.
2189 * @c: UBIFS file-system description object
2190 * @key: key to add
2191 * @old_lnum: LEB number of old node
2192 * @old_offs: old node offset
2193 * @lnum: LEB number of node
2194 * @offs: node offset
2195 * @len: node length
2196 *
2197 * This function replaces a node with key @key in the TNC only if the old node
2198 * is found. This function is called by garbage collection when node are moved.
2199 * Returns %0 on success or negative error code on failure.
2200 */
2201int ubifs_tnc_replace(struct ubifs_info *c, const union ubifs_key *key,
2202 int old_lnum, int old_offs, int lnum, int offs, int len)
2203{
2204 int found, n, err = 0;
2205 struct ubifs_znode *znode;
2206
2207 mutex_lock(&c->tnc_mutex);
Heiko Schocherff94bc42014-06-24 10:10:04 +02002208 dbg_tnck(key, "old LEB %d:%d, new LEB %d:%d, len %d, key ", old_lnum,
2209 old_offs, lnum, offs, len);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002210 found = lookup_level0_dirty(c, key, &znode, &n);
2211 if (found < 0) {
2212 err = found;
2213 goto out_unlock;
2214 }
2215
2216 if (found == 1) {
2217 struct ubifs_zbranch *zbr = &znode->zbranch[n];
2218
2219 found = 0;
2220 if (zbr->lnum == old_lnum && zbr->offs == old_offs) {
2221 lnc_free(zbr);
2222 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2223 if (err)
2224 goto out_unlock;
2225 zbr->lnum = lnum;
2226 zbr->offs = offs;
2227 zbr->len = len;
2228 found = 1;
2229 } else if (is_hash_key(c, key)) {
2230 found = resolve_collision_directly(c, key, &znode, &n,
2231 old_lnum, old_offs);
2232 dbg_tnc("rc returned %d, znode %p, n %d, LEB %d:%d",
2233 found, znode, n, old_lnum, old_offs);
2234 if (found < 0) {
2235 err = found;
2236 goto out_unlock;
2237 }
2238
2239 if (found) {
2240 /* Ensure the znode is dirtied */
2241 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2242 znode = dirty_cow_bottom_up(c, znode);
2243 if (IS_ERR(znode)) {
2244 err = PTR_ERR(znode);
2245 goto out_unlock;
2246 }
2247 }
2248 zbr = &znode->zbranch[n];
2249 lnc_free(zbr);
2250 err = ubifs_add_dirt(c, zbr->lnum,
2251 zbr->len);
2252 if (err)
2253 goto out_unlock;
2254 zbr->lnum = lnum;
2255 zbr->offs = offs;
2256 zbr->len = len;
2257 }
2258 }
2259 }
2260
2261 if (!found)
2262 err = ubifs_add_dirt(c, lnum, len);
2263
2264 if (!err)
2265 err = dbg_check_tnc(c, 0);
2266
2267out_unlock:
2268 mutex_unlock(&c->tnc_mutex);
2269 return err;
2270}
2271
2272/**
2273 * ubifs_tnc_add_nm - add a "hashed" node to TNC.
2274 * @c: UBIFS file-system description object
2275 * @key: key to add
2276 * @lnum: LEB number of node
2277 * @offs: node offset
2278 * @len: node length
2279 * @nm: node name
2280 *
2281 * This is the same as 'ubifs_tnc_add()' but it should be used with keys which
2282 * may have collisions, like directory entry keys.
2283 */
2284int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key,
2285 int lnum, int offs, int len, const struct qstr *nm)
2286{
2287 int found, n, err = 0;
2288 struct ubifs_znode *znode;
2289
2290 mutex_lock(&c->tnc_mutex);
Heiko Schocherff94bc42014-06-24 10:10:04 +02002291 dbg_tnck(key, "LEB %d:%d, name '%.*s', key ",
2292 lnum, offs, nm->len, nm->name);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002293 found = lookup_level0_dirty(c, key, &znode, &n);
2294 if (found < 0) {
2295 err = found;
2296 goto out_unlock;
2297 }
2298
2299 if (found == 1) {
2300 if (c->replaying)
2301 found = fallible_resolve_collision(c, key, &znode, &n,
2302 nm, 1);
2303 else
2304 found = resolve_collision(c, key, &znode, &n, nm);
2305 dbg_tnc("rc returned %d, znode %p, n %d", found, znode, n);
2306 if (found < 0) {
2307 err = found;
2308 goto out_unlock;
2309 }
2310
2311 /* Ensure the znode is dirtied */
2312 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2313 znode = dirty_cow_bottom_up(c, znode);
2314 if (IS_ERR(znode)) {
2315 err = PTR_ERR(znode);
2316 goto out_unlock;
2317 }
2318 }
2319
2320 if (found == 1) {
2321 struct ubifs_zbranch *zbr = &znode->zbranch[n];
2322
2323 lnc_free(zbr);
2324 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2325 zbr->lnum = lnum;
2326 zbr->offs = offs;
2327 zbr->len = len;
2328 goto out_unlock;
2329 }
2330 }
2331
2332 if (!found) {
2333 struct ubifs_zbranch zbr;
2334
2335 zbr.znode = NULL;
2336 zbr.lnum = lnum;
2337 zbr.offs = offs;
2338 zbr.len = len;
2339 key_copy(c, key, &zbr.key);
2340 err = tnc_insert(c, znode, &zbr, n + 1);
2341 if (err)
2342 goto out_unlock;
2343 if (c->replaying) {
2344 /*
2345 * We did not find it in the index so there may be a
2346 * dangling branch still in the index. So we remove it
2347 * by passing 'ubifs_tnc_remove_nm()' the same key but
2348 * an unmatchable name.
2349 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02002350 struct qstr noname = { .name = "" };
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002351
2352 err = dbg_check_tnc(c, 0);
2353 mutex_unlock(&c->tnc_mutex);
2354 if (err)
2355 return err;
2356 return ubifs_tnc_remove_nm(c, key, &noname);
2357 }
2358 }
2359
2360out_unlock:
2361 if (!err)
2362 err = dbg_check_tnc(c, 0);
2363 mutex_unlock(&c->tnc_mutex);
2364 return err;
2365}
2366
2367/**
2368 * tnc_delete - delete a znode form TNC.
2369 * @c: UBIFS file-system description object
2370 * @znode: znode to delete from
2371 * @n: zbranch slot number to delete
2372 *
2373 * This function deletes a leaf node from @n-th slot of @znode. Returns zero in
2374 * case of success and a negative error code in case of failure.
2375 */
2376static int tnc_delete(struct ubifs_info *c, struct ubifs_znode *znode, int n)
2377{
2378 struct ubifs_zbranch *zbr;
2379 struct ubifs_znode *zp;
2380 int i, err;
2381
2382 /* Delete without merge for now */
2383 ubifs_assert(znode->level == 0);
2384 ubifs_assert(n >= 0 && n < c->fanout);
Heiko Schocherff94bc42014-06-24 10:10:04 +02002385 dbg_tnck(&znode->zbranch[n].key, "deleting key ");
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002386
2387 zbr = &znode->zbranch[n];
2388 lnc_free(zbr);
2389
2390 err = ubifs_add_dirt(c, zbr->lnum, zbr->len);
2391 if (err) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002392 ubifs_dump_znode(c, znode);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002393 return err;
2394 }
2395
2396 /* We do not "gap" zbranch slots */
2397 for (i = n; i < znode->child_cnt - 1; i++)
2398 znode->zbranch[i] = znode->zbranch[i + 1];
2399 znode->child_cnt -= 1;
2400
2401 if (znode->child_cnt > 0)
2402 return 0;
2403
2404 /*
2405 * This was the last zbranch, we have to delete this znode from the
2406 * parent.
2407 */
2408
2409 do {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002410 ubifs_assert(!ubifs_zn_obsolete(znode));
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002411 ubifs_assert(ubifs_zn_dirty(znode));
2412
2413 zp = znode->parent;
2414 n = znode->iip;
2415
2416 atomic_long_dec(&c->dirty_zn_cnt);
2417
2418 err = insert_old_idx_znode(c, znode);
2419 if (err)
2420 return err;
2421
2422 if (znode->cnext) {
2423 __set_bit(OBSOLETE_ZNODE, &znode->flags);
2424 atomic_long_inc(&c->clean_zn_cnt);
2425 atomic_long_inc(&ubifs_clean_zn_cnt);
2426 } else
2427 kfree(znode);
2428 znode = zp;
2429 } while (znode->child_cnt == 1); /* while removing last child */
2430
2431 /* Remove from znode, entry n - 1 */
2432 znode->child_cnt -= 1;
2433 ubifs_assert(znode->level != 0);
2434 for (i = n; i < znode->child_cnt; i++) {
2435 znode->zbranch[i] = znode->zbranch[i + 1];
2436 if (znode->zbranch[i].znode)
2437 znode->zbranch[i].znode->iip = i;
2438 }
2439
2440 /*
2441 * If this is the root and it has only 1 child then
2442 * collapse the tree.
2443 */
2444 if (!znode->parent) {
2445 while (znode->child_cnt == 1 && znode->level != 0) {
2446 zp = znode;
2447 zbr = &znode->zbranch[0];
2448 znode = get_znode(c, znode, 0);
2449 if (IS_ERR(znode))
2450 return PTR_ERR(znode);
2451 znode = dirty_cow_znode(c, zbr);
2452 if (IS_ERR(znode))
2453 return PTR_ERR(znode);
2454 znode->parent = NULL;
2455 znode->iip = 0;
2456 if (c->zroot.len) {
2457 err = insert_old_idx(c, c->zroot.lnum,
2458 c->zroot.offs);
2459 if (err)
2460 return err;
2461 }
2462 c->zroot.lnum = zbr->lnum;
2463 c->zroot.offs = zbr->offs;
2464 c->zroot.len = zbr->len;
2465 c->zroot.znode = znode;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002466 ubifs_assert(!ubifs_zn_obsolete(zp));
2467 ubifs_assert(ubifs_zn_dirty(zp));
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002468 atomic_long_dec(&c->dirty_zn_cnt);
2469
2470 if (zp->cnext) {
2471 __set_bit(OBSOLETE_ZNODE, &zp->flags);
2472 atomic_long_inc(&c->clean_zn_cnt);
2473 atomic_long_inc(&ubifs_clean_zn_cnt);
2474 } else
2475 kfree(zp);
2476 }
2477 }
2478
2479 return 0;
2480}
2481
2482/**
2483 * ubifs_tnc_remove - remove an index entry of a node.
2484 * @c: UBIFS file-system description object
2485 * @key: key of node
2486 *
2487 * Returns %0 on success or negative error code on failure.
2488 */
2489int ubifs_tnc_remove(struct ubifs_info *c, const union ubifs_key *key)
2490{
2491 int found, n, err = 0;
2492 struct ubifs_znode *znode;
2493
2494 mutex_lock(&c->tnc_mutex);
Heiko Schocherff94bc42014-06-24 10:10:04 +02002495 dbg_tnck(key, "key ");
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002496 found = lookup_level0_dirty(c, key, &znode, &n);
2497 if (found < 0) {
2498 err = found;
2499 goto out_unlock;
2500 }
2501 if (found == 1)
2502 err = tnc_delete(c, znode, n);
2503 if (!err)
2504 err = dbg_check_tnc(c, 0);
2505
2506out_unlock:
2507 mutex_unlock(&c->tnc_mutex);
2508 return err;
2509}
2510
2511/**
2512 * ubifs_tnc_remove_nm - remove an index entry for a "hashed" node.
2513 * @c: UBIFS file-system description object
2514 * @key: key of node
2515 * @nm: directory entry name
2516 *
2517 * Returns %0 on success or negative error code on failure.
2518 */
2519int ubifs_tnc_remove_nm(struct ubifs_info *c, const union ubifs_key *key,
2520 const struct qstr *nm)
2521{
2522 int n, err;
2523 struct ubifs_znode *znode;
2524
2525 mutex_lock(&c->tnc_mutex);
Heiko Schocherff94bc42014-06-24 10:10:04 +02002526 dbg_tnck(key, "%.*s, key ", nm->len, nm->name);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002527 err = lookup_level0_dirty(c, key, &znode, &n);
2528 if (err < 0)
2529 goto out_unlock;
2530
2531 if (err) {
2532 if (c->replaying)
2533 err = fallible_resolve_collision(c, key, &znode, &n,
2534 nm, 0);
2535 else
2536 err = resolve_collision(c, key, &znode, &n, nm);
2537 dbg_tnc("rc returned %d, znode %p, n %d", err, znode, n);
2538 if (err < 0)
2539 goto out_unlock;
2540 if (err) {
2541 /* Ensure the znode is dirtied */
2542 if (znode->cnext || !ubifs_zn_dirty(znode)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002543 znode = dirty_cow_bottom_up(c, znode);
2544 if (IS_ERR(znode)) {
2545 err = PTR_ERR(znode);
2546 goto out_unlock;
2547 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002548 }
2549 err = tnc_delete(c, znode, n);
2550 }
2551 }
2552
2553out_unlock:
2554 if (!err)
2555 err = dbg_check_tnc(c, 0);
2556 mutex_unlock(&c->tnc_mutex);
2557 return err;
2558}
2559
2560/**
2561 * key_in_range - determine if a key falls within a range of keys.
2562 * @c: UBIFS file-system description object
2563 * @key: key to check
2564 * @from_key: lowest key in range
2565 * @to_key: highest key in range
2566 *
2567 * This function returns %1 if the key is in range and %0 otherwise.
2568 */
2569static int key_in_range(struct ubifs_info *c, union ubifs_key *key,
2570 union ubifs_key *from_key, union ubifs_key *to_key)
2571{
2572 if (keys_cmp(c, key, from_key) < 0)
2573 return 0;
2574 if (keys_cmp(c, key, to_key) > 0)
2575 return 0;
2576 return 1;
2577}
2578
2579/**
2580 * ubifs_tnc_remove_range - remove index entries in range.
2581 * @c: UBIFS file-system description object
2582 * @from_key: lowest key to remove
2583 * @to_key: highest key to remove
2584 *
2585 * This function removes index entries starting at @from_key and ending at
2586 * @to_key. This function returns zero in case of success and a negative error
2587 * code in case of failure.
2588 */
2589int ubifs_tnc_remove_range(struct ubifs_info *c, union ubifs_key *from_key,
2590 union ubifs_key *to_key)
2591{
2592 int i, n, k, err = 0;
2593 struct ubifs_znode *znode;
2594 union ubifs_key *key;
2595
2596 mutex_lock(&c->tnc_mutex);
2597 while (1) {
2598 /* Find first level 0 znode that contains keys to remove */
2599 err = ubifs_lookup_level0(c, from_key, &znode, &n);
2600 if (err < 0)
2601 goto out_unlock;
2602
2603 if (err)
2604 key = from_key;
2605 else {
2606 err = tnc_next(c, &znode, &n);
2607 if (err == -ENOENT) {
2608 err = 0;
2609 goto out_unlock;
2610 }
2611 if (err < 0)
2612 goto out_unlock;
2613 key = &znode->zbranch[n].key;
2614 if (!key_in_range(c, key, from_key, to_key)) {
2615 err = 0;
2616 goto out_unlock;
2617 }
2618 }
2619
2620 /* Ensure the znode is dirtied */
2621 if (znode->cnext || !ubifs_zn_dirty(znode)) {
2622 znode = dirty_cow_bottom_up(c, znode);
2623 if (IS_ERR(znode)) {
2624 err = PTR_ERR(znode);
2625 goto out_unlock;
2626 }
2627 }
2628
2629 /* Remove all keys in range except the first */
2630 for (i = n + 1, k = 0; i < znode->child_cnt; i++, k++) {
2631 key = &znode->zbranch[i].key;
2632 if (!key_in_range(c, key, from_key, to_key))
2633 break;
2634 lnc_free(&znode->zbranch[i]);
2635 err = ubifs_add_dirt(c, znode->zbranch[i].lnum,
2636 znode->zbranch[i].len);
2637 if (err) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02002638 ubifs_dump_znode(c, znode);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002639 goto out_unlock;
2640 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02002641 dbg_tnck(key, "removing key ");
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002642 }
2643 if (k) {
2644 for (i = n + 1 + k; i < znode->child_cnt; i++)
2645 znode->zbranch[i - k] = znode->zbranch[i];
2646 znode->child_cnt -= k;
2647 }
2648
2649 /* Now delete the first */
2650 err = tnc_delete(c, znode, n);
2651 if (err)
2652 goto out_unlock;
2653 }
2654
2655out_unlock:
2656 if (!err)
2657 err = dbg_check_tnc(c, 0);
2658 mutex_unlock(&c->tnc_mutex);
2659 return err;
2660}
2661
2662/**
2663 * ubifs_tnc_remove_ino - remove an inode from TNC.
2664 * @c: UBIFS file-system description object
2665 * @inum: inode number to remove
2666 *
2667 * This function remove inode @inum and all the extended attributes associated
2668 * with the anode from TNC and returns zero in case of success or a negative
2669 * error code in case of failure.
2670 */
2671int ubifs_tnc_remove_ino(struct ubifs_info *c, ino_t inum)
2672{
2673 union ubifs_key key1, key2;
2674 struct ubifs_dent_node *xent, *pxent = NULL;
2675 struct qstr nm = { .name = NULL };
2676
2677 dbg_tnc("ino %lu", (unsigned long)inum);
2678
2679 /*
2680 * Walk all extended attribute entries and remove them together with
2681 * corresponding extended attribute inodes.
2682 */
2683 lowest_xent_key(c, &key1, inum);
2684 while (1) {
2685 ino_t xattr_inum;
2686 int err;
2687
2688 xent = ubifs_tnc_next_ent(c, &key1, &nm);
2689 if (IS_ERR(xent)) {
2690 err = PTR_ERR(xent);
2691 if (err == -ENOENT)
2692 break;
2693 return err;
2694 }
2695
2696 xattr_inum = le64_to_cpu(xent->inum);
2697 dbg_tnc("xent '%s', ino %lu", xent->name,
2698 (unsigned long)xattr_inum);
2699
Heiko Schocherff94bc42014-06-24 10:10:04 +02002700 nm.name = xent->name;
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002701 nm.len = le16_to_cpu(xent->nlen);
2702 err = ubifs_tnc_remove_nm(c, &key1, &nm);
2703 if (err) {
2704 kfree(xent);
2705 return err;
2706 }
2707
2708 lowest_ino_key(c, &key1, xattr_inum);
2709 highest_ino_key(c, &key2, xattr_inum);
2710 err = ubifs_tnc_remove_range(c, &key1, &key2);
2711 if (err) {
2712 kfree(xent);
2713 return err;
2714 }
2715
2716 kfree(pxent);
2717 pxent = xent;
2718 key_read(c, &xent->key, &key1);
2719 }
2720
2721 kfree(pxent);
2722 lowest_ino_key(c, &key1, inum);
2723 highest_ino_key(c, &key2, inum);
2724
2725 return ubifs_tnc_remove_range(c, &key1, &key2);
2726}
2727
2728/**
2729 * ubifs_tnc_next_ent - walk directory or extended attribute entries.
2730 * @c: UBIFS file-system description object
2731 * @key: key of last entry
2732 * @nm: name of last entry found or %NULL
2733 *
2734 * This function finds and reads the next directory or extended attribute entry
2735 * after the given key (@key) if there is one. @nm is used to resolve
2736 * collisions.
2737 *
2738 * If the name of the current entry is not known and only the key is known,
2739 * @nm->name has to be %NULL. In this case the semantics of this function is a
2740 * little bit different and it returns the entry corresponding to this key, not
2741 * the next one. If the key was not found, the closest "right" entry is
2742 * returned.
2743 *
2744 * If the fist entry has to be found, @key has to contain the lowest possible
2745 * key value for this inode and @name has to be %NULL.
2746 *
2747 * This function returns the found directory or extended attribute entry node
2748 * in case of success, %-ENOENT is returned if no entry was found, and a
2749 * negative error code is returned in case of failure.
2750 */
2751struct ubifs_dent_node *ubifs_tnc_next_ent(struct ubifs_info *c,
2752 union ubifs_key *key,
2753 const struct qstr *nm)
2754{
2755 int n, err, type = key_type(c, key);
2756 struct ubifs_znode *znode;
2757 struct ubifs_dent_node *dent;
2758 struct ubifs_zbranch *zbr;
2759 union ubifs_key *dkey;
2760
Heiko Schocherff94bc42014-06-24 10:10:04 +02002761 dbg_tnck(key, "%s ", nm->name ? (char *)nm->name : "(lowest)");
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002762 ubifs_assert(is_hash_key(c, key));
2763
2764 mutex_lock(&c->tnc_mutex);
2765 err = ubifs_lookup_level0(c, key, &znode, &n);
2766 if (unlikely(err < 0))
2767 goto out_unlock;
2768
2769 if (nm->name) {
2770 if (err) {
2771 /* Handle collisions */
2772 err = resolve_collision(c, key, &znode, &n, nm);
2773 dbg_tnc("rc returned %d, znode %p, n %d",
2774 err, znode, n);
2775 if (unlikely(err < 0))
2776 goto out_unlock;
2777 }
2778
2779 /* Now find next entry */
2780 err = tnc_next(c, &znode, &n);
2781 if (unlikely(err))
2782 goto out_unlock;
2783 } else {
2784 /*
2785 * The full name of the entry was not given, in which case the
2786 * behavior of this function is a little different and it
2787 * returns current entry, not the next one.
2788 */
2789 if (!err) {
2790 /*
2791 * However, the given key does not exist in the TNC
2792 * tree and @znode/@n variables contain the closest
2793 * "preceding" element. Switch to the next one.
2794 */
2795 err = tnc_next(c, &znode, &n);
2796 if (err)
2797 goto out_unlock;
2798 }
2799 }
2800
2801 zbr = &znode->zbranch[n];
2802 dent = kmalloc(zbr->len, GFP_NOFS);
2803 if (unlikely(!dent)) {
2804 err = -ENOMEM;
2805 goto out_unlock;
2806 }
2807
2808 /*
2809 * The above 'tnc_next()' call could lead us to the next inode, check
2810 * this.
2811 */
2812 dkey = &zbr->key;
2813 if (key_inum(c, dkey) != key_inum(c, key) ||
2814 key_type(c, dkey) != type) {
2815 err = -ENOENT;
2816 goto out_free;
2817 }
2818
2819 err = tnc_read_node_nm(c, zbr, dent);
2820 if (unlikely(err))
2821 goto out_free;
2822
2823 mutex_unlock(&c->tnc_mutex);
2824 return dent;
2825
2826out_free:
2827 kfree(dent);
2828out_unlock:
2829 mutex_unlock(&c->tnc_mutex);
2830 return ERR_PTR(err);
2831}
Heiko Schocherff94bc42014-06-24 10:10:04 +02002832
Heiko Schocherff94bc42014-06-24 10:10:04 +02002833/**
2834 * tnc_destroy_cnext - destroy left-over obsolete znodes from a failed commit.
2835 * @c: UBIFS file-system description object
2836 *
2837 * Destroy left-over obsolete znodes from a failed commit.
2838 */
2839static void tnc_destroy_cnext(struct ubifs_info *c)
2840{
2841 struct ubifs_znode *cnext;
2842
2843 if (!c->cnext)
2844 return;
2845 ubifs_assert(c->cmt_state == COMMIT_BROKEN);
2846 cnext = c->cnext;
2847 do {
2848 struct ubifs_znode *znode = cnext;
2849
2850 cnext = cnext->cnext;
2851 if (ubifs_zn_obsolete(znode))
2852 kfree(znode);
2853 } while (cnext && cnext != c->cnext);
2854}
2855
2856/**
2857 * ubifs_tnc_close - close TNC subsystem and free all related resources.
2858 * @c: UBIFS file-system description object
2859 */
2860void ubifs_tnc_close(struct ubifs_info *c)
2861{
2862 tnc_destroy_cnext(c);
2863 if (c->zroot.znode) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02002864 long n, freed;
Heiko Schocherff94bc42014-06-24 10:10:04 +02002865
Heiko Schocherff94bc42014-06-24 10:10:04 +02002866 n = atomic_long_read(&c->clean_zn_cnt);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02002867 freed = ubifs_destroy_tnc_subtree(c->zroot.znode);
2868 ubifs_assert(freed == n);
Heiko Schocherff94bc42014-06-24 10:10:04 +02002869 atomic_long_sub(n, &ubifs_clean_zn_cnt);
2870 }
2871 kfree(c->gap_lebs);
2872 kfree(c->ilebs);
2873 destroy_old_idx(c);
2874}
Heiko Schocherff94bc42014-06-24 10:10:04 +02002875
2876/**
2877 * left_znode - get the znode to the left.
2878 * @c: UBIFS file-system description object
2879 * @znode: znode
2880 *
2881 * This function returns a pointer to the znode to the left of @znode or NULL if
2882 * there is not one. A negative error code is returned on failure.
2883 */
2884static struct ubifs_znode *left_znode(struct ubifs_info *c,
2885 struct ubifs_znode *znode)
2886{
2887 int level = znode->level;
2888
2889 while (1) {
2890 int n = znode->iip - 1;
2891
2892 /* Go up until we can go left */
2893 znode = znode->parent;
2894 if (!znode)
2895 return NULL;
2896 if (n >= 0) {
2897 /* Now go down the rightmost branch to 'level' */
2898 znode = get_znode(c, znode, n);
2899 if (IS_ERR(znode))
2900 return znode;
2901 while (znode->level != level) {
2902 n = znode->child_cnt - 1;
2903 znode = get_znode(c, znode, n);
2904 if (IS_ERR(znode))
2905 return znode;
2906 }
2907 break;
2908 }
2909 }
2910 return znode;
2911}
2912
2913/**
2914 * right_znode - get the znode to the right.
2915 * @c: UBIFS file-system description object
2916 * @znode: znode
2917 *
2918 * This function returns a pointer to the znode to the right of @znode or NULL
2919 * if there is not one. A negative error code is returned on failure.
2920 */
2921static struct ubifs_znode *right_znode(struct ubifs_info *c,
2922 struct ubifs_znode *znode)
2923{
2924 int level = znode->level;
2925
2926 while (1) {
2927 int n = znode->iip + 1;
2928
2929 /* Go up until we can go right */
2930 znode = znode->parent;
2931 if (!znode)
2932 return NULL;
2933 if (n < znode->child_cnt) {
2934 /* Now go down the leftmost branch to 'level' */
2935 znode = get_znode(c, znode, n);
2936 if (IS_ERR(znode))
2937 return znode;
2938 while (znode->level != level) {
2939 znode = get_znode(c, znode, 0);
2940 if (IS_ERR(znode))
2941 return znode;
2942 }
2943 break;
2944 }
2945 }
2946 return znode;
2947}
2948
2949/**
2950 * lookup_znode - find a particular indexing node from TNC.
2951 * @c: UBIFS file-system description object
2952 * @key: index node key to lookup
2953 * @level: index node level
2954 * @lnum: index node LEB number
2955 * @offs: index node offset
2956 *
2957 * This function searches an indexing node by its first key @key and its
2958 * address @lnum:@offs. It looks up the indexing tree by pulling all indexing
2959 * nodes it traverses to TNC. This function is called for indexing nodes which
2960 * were found on the media by scanning, for example when garbage-collecting or
2961 * when doing in-the-gaps commit. This means that the indexing node which is
2962 * looked for does not have to have exactly the same leftmost key @key, because
2963 * the leftmost key may have been changed, in which case TNC will contain a
2964 * dirty znode which still refers the same @lnum:@offs. This function is clever
2965 * enough to recognize such indexing nodes.
2966 *
2967 * Note, if a znode was deleted or changed too much, then this function will
2968 * not find it. For situations like this UBIFS has the old index RB-tree
2969 * (indexed by @lnum:@offs).
2970 *
2971 * This function returns a pointer to the znode found or %NULL if it is not
2972 * found. A negative error code is returned on failure.
2973 */
2974static struct ubifs_znode *lookup_znode(struct ubifs_info *c,
2975 union ubifs_key *key, int level,
2976 int lnum, int offs)
2977{
2978 struct ubifs_znode *znode, *zn;
2979 int n, nn;
2980
2981 ubifs_assert(key_type(c, key) < UBIFS_INVALID_KEY);
2982
2983 /*
2984 * The arguments have probably been read off flash, so don't assume
2985 * they are valid.
2986 */
2987 if (level < 0)
2988 return ERR_PTR(-EINVAL);
2989
2990 /* Get the root znode */
2991 znode = c->zroot.znode;
2992 if (!znode) {
2993 znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
2994 if (IS_ERR(znode))
2995 return znode;
2996 }
2997 /* Check if it is the one we are looking for */
2998 if (c->zroot.lnum == lnum && c->zroot.offs == offs)
2999 return znode;
3000 /* Descend to the parent level i.e. (level + 1) */
3001 if (level >= znode->level)
3002 return NULL;
3003 while (1) {
3004 ubifs_search_zbranch(c, znode, key, &n);
3005 if (n < 0) {
3006 /*
3007 * We reached a znode where the leftmost key is greater
3008 * than the key we are searching for. This is the same
3009 * situation as the one described in a huge comment at
3010 * the end of the 'ubifs_lookup_level0()' function. And
3011 * for exactly the same reasons we have to try to look
3012 * left before giving up.
3013 */
3014 znode = left_znode(c, znode);
3015 if (!znode)
3016 return NULL;
3017 if (IS_ERR(znode))
3018 return znode;
3019 ubifs_search_zbranch(c, znode, key, &n);
3020 ubifs_assert(n >= 0);
3021 }
3022 if (znode->level == level + 1)
3023 break;
3024 znode = get_znode(c, znode, n);
3025 if (IS_ERR(znode))
3026 return znode;
3027 }
3028 /* Check if the child is the one we are looking for */
3029 if (znode->zbranch[n].lnum == lnum && znode->zbranch[n].offs == offs)
3030 return get_znode(c, znode, n);
3031 /* If the key is unique, there is nowhere else to look */
3032 if (!is_hash_key(c, key))
3033 return NULL;
3034 /*
3035 * The key is not unique and so may be also in the znodes to either
3036 * side.
3037 */
3038 zn = znode;
3039 nn = n;
3040 /* Look left */
3041 while (1) {
3042 /* Move one branch to the left */
3043 if (n)
3044 n -= 1;
3045 else {
3046 znode = left_znode(c, znode);
3047 if (!znode)
3048 break;
3049 if (IS_ERR(znode))
3050 return znode;
3051 n = znode->child_cnt - 1;
3052 }
3053 /* Check it */
3054 if (znode->zbranch[n].lnum == lnum &&
3055 znode->zbranch[n].offs == offs)
3056 return get_znode(c, znode, n);
3057 /* Stop if the key is less than the one we are looking for */
3058 if (keys_cmp(c, &znode->zbranch[n].key, key) < 0)
3059 break;
3060 }
3061 /* Back to the middle */
3062 znode = zn;
3063 n = nn;
3064 /* Look right */
3065 while (1) {
3066 /* Move one branch to the right */
3067 if (++n >= znode->child_cnt) {
3068 znode = right_znode(c, znode);
3069 if (!znode)
3070 break;
3071 if (IS_ERR(znode))
3072 return znode;
3073 n = 0;
3074 }
3075 /* Check it */
3076 if (znode->zbranch[n].lnum == lnum &&
3077 znode->zbranch[n].offs == offs)
3078 return get_znode(c, znode, n);
3079 /* Stop if the key is greater than the one we are looking for */
3080 if (keys_cmp(c, &znode->zbranch[n].key, key) > 0)
3081 break;
3082 }
3083 return NULL;
3084}
3085
3086/**
3087 * is_idx_node_in_tnc - determine if an index node is in the TNC.
3088 * @c: UBIFS file-system description object
3089 * @key: key of index node
3090 * @level: index node level
3091 * @lnum: LEB number of index node
3092 * @offs: offset of index node
3093 *
3094 * This function returns %0 if the index node is not referred to in the TNC, %1
3095 * if the index node is referred to in the TNC and the corresponding znode is
3096 * dirty, %2 if an index node is referred to in the TNC and the corresponding
3097 * znode is clean, and a negative error code in case of failure.
3098 *
3099 * Note, the @key argument has to be the key of the first child. Also note,
3100 * this function relies on the fact that 0:0 is never a valid LEB number and
3101 * offset for a main-area node.
3102 */
3103int is_idx_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, int level,
3104 int lnum, int offs)
3105{
3106 struct ubifs_znode *znode;
3107
3108 znode = lookup_znode(c, key, level, lnum, offs);
3109 if (!znode)
3110 return 0;
3111 if (IS_ERR(znode))
3112 return PTR_ERR(znode);
3113
3114 return ubifs_zn_dirty(znode) ? 1 : 2;
3115}
3116
3117/**
3118 * is_leaf_node_in_tnc - determine if a non-indexing not is in the TNC.
3119 * @c: UBIFS file-system description object
3120 * @key: node key
3121 * @lnum: node LEB number
3122 * @offs: node offset
3123 *
3124 * This function returns %1 if the node is referred to in the TNC, %0 if it is
3125 * not, and a negative error code in case of failure.
3126 *
3127 * Note, this function relies on the fact that 0:0 is never a valid LEB number
3128 * and offset for a main-area node.
3129 */
3130static int is_leaf_node_in_tnc(struct ubifs_info *c, union ubifs_key *key,
3131 int lnum, int offs)
3132{
3133 struct ubifs_zbranch *zbr;
3134 struct ubifs_znode *znode, *zn;
3135 int n, found, err, nn;
3136 const int unique = !is_hash_key(c, key);
3137
3138 found = ubifs_lookup_level0(c, key, &znode, &n);
3139 if (found < 0)
3140 return found; /* Error code */
3141 if (!found)
3142 return 0;
3143 zbr = &znode->zbranch[n];
3144 if (lnum == zbr->lnum && offs == zbr->offs)
3145 return 1; /* Found it */
3146 if (unique)
3147 return 0;
3148 /*
3149 * Because the key is not unique, we have to look left
3150 * and right as well
3151 */
3152 zn = znode;
3153 nn = n;
3154 /* Look left */
3155 while (1) {
3156 err = tnc_prev(c, &znode, &n);
3157 if (err == -ENOENT)
3158 break;
3159 if (err)
3160 return err;
3161 if (keys_cmp(c, key, &znode->zbranch[n].key))
3162 break;
3163 zbr = &znode->zbranch[n];
3164 if (lnum == zbr->lnum && offs == zbr->offs)
3165 return 1; /* Found it */
3166 }
3167 /* Look right */
3168 znode = zn;
3169 n = nn;
3170 while (1) {
3171 err = tnc_next(c, &znode, &n);
3172 if (err) {
3173 if (err == -ENOENT)
3174 return 0;
3175 return err;
3176 }
3177 if (keys_cmp(c, key, &znode->zbranch[n].key))
3178 break;
3179 zbr = &znode->zbranch[n];
3180 if (lnum == zbr->lnum && offs == zbr->offs)
3181 return 1; /* Found it */
3182 }
3183 return 0;
3184}
3185
3186/**
3187 * ubifs_tnc_has_node - determine whether a node is in the TNC.
3188 * @c: UBIFS file-system description object
3189 * @key: node key
3190 * @level: index node level (if it is an index node)
3191 * @lnum: node LEB number
3192 * @offs: node offset
3193 * @is_idx: non-zero if the node is an index node
3194 *
3195 * This function returns %1 if the node is in the TNC, %0 if it is not, and a
3196 * negative error code in case of failure. For index nodes, @key has to be the
3197 * key of the first child. An index node is considered to be in the TNC only if
3198 * the corresponding znode is clean or has not been loaded.
3199 */
3200int ubifs_tnc_has_node(struct ubifs_info *c, union ubifs_key *key, int level,
3201 int lnum, int offs, int is_idx)
3202{
3203 int err;
3204
3205 mutex_lock(&c->tnc_mutex);
3206 if (is_idx) {
3207 err = is_idx_node_in_tnc(c, key, level, lnum, offs);
3208 if (err < 0)
3209 goto out_unlock;
3210 if (err == 1)
3211 /* The index node was found but it was dirty */
3212 err = 0;
3213 else if (err == 2)
3214 /* The index node was found and it was clean */
3215 err = 1;
3216 else
3217 BUG_ON(err != 0);
3218 } else
3219 err = is_leaf_node_in_tnc(c, key, lnum, offs);
3220
3221out_unlock:
3222 mutex_unlock(&c->tnc_mutex);
3223 return err;
3224}
3225
3226/**
3227 * ubifs_dirty_idx_node - dirty an index node.
3228 * @c: UBIFS file-system description object
3229 * @key: index node key
3230 * @level: index node level
3231 * @lnum: index node LEB number
3232 * @offs: index node offset
3233 *
3234 * This function loads and dirties an index node so that it can be garbage
3235 * collected. The @key argument has to be the key of the first child. This
3236 * function relies on the fact that 0:0 is never a valid LEB number and offset
3237 * for a main-area node. Returns %0 on success and a negative error code on
3238 * failure.
3239 */
3240int ubifs_dirty_idx_node(struct ubifs_info *c, union ubifs_key *key, int level,
3241 int lnum, int offs)
3242{
3243 struct ubifs_znode *znode;
3244 int err = 0;
3245
3246 mutex_lock(&c->tnc_mutex);
3247 znode = lookup_znode(c, key, level, lnum, offs);
3248 if (!znode)
3249 goto out_unlock;
3250 if (IS_ERR(znode)) {
3251 err = PTR_ERR(znode);
3252 goto out_unlock;
3253 }
3254 znode = dirty_cow_bottom_up(c, znode);
3255 if (IS_ERR(znode)) {
3256 err = PTR_ERR(znode);
3257 goto out_unlock;
3258 }
3259
3260out_unlock:
3261 mutex_unlock(&c->tnc_mutex);
3262 return err;
3263}
3264
3265/**
3266 * dbg_check_inode_size - check if inode size is correct.
3267 * @c: UBIFS file-system description object
3268 * @inum: inode number
3269 * @size: inode size
3270 *
3271 * This function makes sure that the inode size (@size) is correct and it does
3272 * not have any pages beyond @size. Returns zero if the inode is OK, %-EINVAL
3273 * if it has a data page beyond @size, and other negative error code in case of
3274 * other errors.
3275 */
3276int dbg_check_inode_size(struct ubifs_info *c, const struct inode *inode,
3277 loff_t size)
3278{
3279 int err, n;
3280 union ubifs_key from_key, to_key, *key;
3281 struct ubifs_znode *znode;
3282 unsigned int block;
3283
3284 if (!S_ISREG(inode->i_mode))
3285 return 0;
3286 if (!dbg_is_chk_gen(c))
3287 return 0;
3288
3289 block = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
3290 data_key_init(c, &from_key, inode->i_ino, block);
3291 highest_data_key(c, &to_key, inode->i_ino);
3292
3293 mutex_lock(&c->tnc_mutex);
3294 err = ubifs_lookup_level0(c, &from_key, &znode, &n);
3295 if (err < 0)
3296 goto out_unlock;
3297
3298 if (err) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02003299 key = &from_key;
3300 goto out_dump;
3301 }
3302
3303 err = tnc_next(c, &znode, &n);
3304 if (err == -ENOENT) {
3305 err = 0;
3306 goto out_unlock;
3307 }
3308 if (err < 0)
3309 goto out_unlock;
3310
3311 ubifs_assert(err == 0);
3312 key = &znode->zbranch[n].key;
3313 if (!key_in_range(c, key, &from_key, &to_key))
3314 goto out_unlock;
3315
3316out_dump:
3317 block = key_block(c, key);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02003318 ubifs_err(c, "inode %lu has size %lld, but there are data at offset %lld",
Heiko Schocherff94bc42014-06-24 10:10:04 +02003319 (unsigned long)inode->i_ino, size,
3320 ((loff_t)block) << UBIFS_BLOCK_SHIFT);
3321 mutex_unlock(&c->tnc_mutex);
3322 ubifs_dump_inode(c, inode);
3323 dump_stack();
3324 return -EINVAL;
3325
3326out_unlock:
3327 mutex_unlock(&c->tnc_mutex);
3328 return err;
3329}