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