~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/ubifs/tnc_misc.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /fs/ubifs/tnc_misc.c (Version linux-6.12-rc7) and /fs/ubifs/tnc_misc.c (Version linux-4.11.12)


  1 // SPDX-License-Identifier: GPL-2.0-only       << 
  2 /*                                                  1 /*
  3  * This file is part of UBIFS.                      2  * This file is part of UBIFS.
  4  *                                                  3  *
  5  * Copyright (C) 2006-2008 Nokia Corporation.       4  * Copyright (C) 2006-2008 Nokia Corporation.
  6  *                                                  5  *
                                                   >>   6  * This program is free software; you can redistribute it and/or modify it
                                                   >>   7  * under the terms of the GNU General Public License version 2 as published by
                                                   >>   8  * the Free Software Foundation.
                                                   >>   9  *
                                                   >>  10  * This program is distributed in the hope that it will be useful, but WITHOUT
                                                   >>  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
                                                   >>  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
                                                   >>  13  * more details.
                                                   >>  14  *
                                                   >>  15  * You should have received a copy of the GNU General Public License along with
                                                   >>  16  * this program; if not, write to the Free Software Foundation, Inc., 51
                                                   >>  17  * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
                                                   >>  18  *
  7  * Authors: Adrian Hunter                          19  * Authors: Adrian Hunter
  8  *          Artem Bityutskiy (Битюцкий     20  *          Artem Bityutskiy (Битюцкий Артём)
  9  */                                                21  */
 10                                                    22 
 11 /*                                                 23 /*
 12  * This file contains miscelanious TNC-related     24  * This file contains miscelanious TNC-related functions shared betweend
 13  * different files. This file does not form an     25  * different files. This file does not form any logically separate TNC
 14  * sub-system. The file was created because th     26  * sub-system. The file was created because there is a lot of TNC code and
 15  * putting it all in one file would make that      27  * putting it all in one file would make that file too big and unreadable.
 16  */                                                28  */
 17                                                    29 
 18 #include "ubifs.h"                                 30 #include "ubifs.h"
 19                                                    31 
 20 /**                                                32 /**
 21  * ubifs_tnc_levelorder_next - next TNC tree e     33  * ubifs_tnc_levelorder_next - next TNC tree element in levelorder traversal.
 22  * @c: UBIFS file-system description object    << 
 23  * @zr: root of the subtree to traverse            34  * @zr: root of the subtree to traverse
 24  * @znode: previous znode                          35  * @znode: previous znode
 25  *                                                 36  *
 26  * This function implements levelorder TNC tra     37  * This function implements levelorder TNC traversal. The LNC is ignored.
 27  * Returns the next element or %NULL if @znode     38  * Returns the next element or %NULL if @znode is already the last one.
 28  */                                                39  */
 29 struct ubifs_znode *ubifs_tnc_levelorder_next( !!  40 struct ubifs_znode *ubifs_tnc_levelorder_next(struct ubifs_znode *zr,
 30                                                << 
 31                                                    41                                               struct ubifs_znode *znode)
 32 {                                                  42 {
 33         int level, iip, level_search = 0;          43         int level, iip, level_search = 0;
 34         struct ubifs_znode *zn;                    44         struct ubifs_znode *zn;
 35                                                    45 
 36         ubifs_assert(c, zr);                   !!  46         ubifs_assert(zr);
 37                                                    47 
 38         if (unlikely(!znode))                      48         if (unlikely(!znode))
 39                 return zr;                         49                 return zr;
 40                                                    50 
 41         if (unlikely(znode == zr)) {               51         if (unlikely(znode == zr)) {
 42                 if (znode->level == 0)             52                 if (znode->level == 0)
 43                         return NULL;               53                         return NULL;
 44                 return ubifs_tnc_find_child(zr     54                 return ubifs_tnc_find_child(zr, 0);
 45         }                                          55         }
 46                                                    56 
 47         level = znode->level;                      57         level = znode->level;
 48                                                    58 
 49         iip = znode->iip;                          59         iip = znode->iip;
 50         while (1) {                                60         while (1) {
 51                 ubifs_assert(c, znode->level < !!  61                 ubifs_assert(znode->level <= zr->level);
 52                                                    62 
 53                 /*                                 63                 /*
 54                  * First walk up until there i     64                  * First walk up until there is a znode with next branch to
 55                  * look at.                        65                  * look at.
 56                  */                                66                  */
 57                 while (znode->parent != zr &&      67                 while (znode->parent != zr && iip >= znode->parent->child_cnt) {
 58                         znode = znode->parent;     68                         znode = znode->parent;
 59                         iip = znode->iip;          69                         iip = znode->iip;
 60                 }                                  70                 }
 61                                                    71 
 62                 if (unlikely(znode->parent ==      72                 if (unlikely(znode->parent == zr &&
 63                              iip >= znode->par     73                              iip >= znode->parent->child_cnt)) {
 64                         /* This level is done,     74                         /* This level is done, switch to the lower one */
 65                         level -= 1;                75                         level -= 1;
 66                         if (level_search || le     76                         if (level_search || level < 0)
 67                                 /*                 77                                 /*
 68                                  * We were alr     78                                  * We were already looking for znode at lower
 69                                  * level ('lev     79                                  * level ('level_search'). As we are here
 70                                  * again, it j     80                                  * again, it just does not exist. Or all levels
 71                                  * were finish     81                                  * were finished ('level < 0').
 72                                  */                82                                  */
 73                                 return NULL;       83                                 return NULL;
 74                                                    84 
 75                         level_search = 1;          85                         level_search = 1;
 76                         iip = -1;                  86                         iip = -1;
 77                         znode = ubifs_tnc_find     87                         znode = ubifs_tnc_find_child(zr, 0);
 78                         ubifs_assert(c, znode) !!  88                         ubifs_assert(znode);
 79                 }                                  89                 }
 80                                                    90 
 81                 /* Switch to the next index */     91                 /* Switch to the next index */
 82                 zn = ubifs_tnc_find_child(znod     92                 zn = ubifs_tnc_find_child(znode->parent, iip + 1);
 83                 if (!zn) {                         93                 if (!zn) {
 84                         /* No more children to     94                         /* No more children to look at, we have walk up */
 85                         iip = znode->parent->c     95                         iip = znode->parent->child_cnt;
 86                         continue;                  96                         continue;
 87                 }                                  97                 }
 88                                                    98 
 89                 /* Walk back down to the level     99                 /* Walk back down to the level we came from ('level') */
 90                 while (zn->level != level) {      100                 while (zn->level != level) {
 91                         znode = zn;               101                         znode = zn;
 92                         zn = ubifs_tnc_find_ch    102                         zn = ubifs_tnc_find_child(zn, 0);
 93                         if (!zn) {                103                         if (!zn) {
 94                                 /*                104                                 /*
 95                                  * This path i    105                                  * This path is not too deep so it does not
 96                                  * reach 'leve    106                                  * reach 'level'. Try next path.
 97                                  */               107                                  */
 98                                 iip = znode->i    108                                 iip = znode->iip;
 99                                 break;            109                                 break;
100                         }                         110                         }
101                 }                                 111                 }
102                                                   112 
103                 if (zn) {                         113                 if (zn) {
104                         ubifs_assert(c, zn->le !! 114                         ubifs_assert(zn->level >= 0);
105                         return zn;                115                         return zn;
106                 }                                 116                 }
107         }                                         117         }
108 }                                                 118 }
109                                                   119 
110 /**                                               120 /**
111  * ubifs_search_zbranch - search znode branch.    121  * ubifs_search_zbranch - search znode branch.
112  * @c: UBIFS file-system description object       122  * @c: UBIFS file-system description object
113  * @znode: znode to search in                     123  * @znode: znode to search in
114  * @key: key to search for                        124  * @key: key to search for
115  * @n: znode branch slot number is returned he    125  * @n: znode branch slot number is returned here
116  *                                                126  *
117  * This is a helper function which search bran    127  * This is a helper function which search branch with key @key in @znode using
118  * binary search. The result of the search may    128  * binary search. The result of the search may be:
119  *   o exact match, then %1 is returned, and t    129  *   o exact match, then %1 is returned, and the slot number of the branch is
120  *     stored in @n;                              130  *     stored in @n;
121  *   o no exact match, then %0 is returned and    131  *   o no exact match, then %0 is returned and the slot number of the left
122  *     closest branch is returned in @n; the s    132  *     closest branch is returned in @n; the slot if all keys in this znode are
123  *     greater than @key, then %-1 is returned    133  *     greater than @key, then %-1 is returned in @n.
124  */                                               134  */
125 int ubifs_search_zbranch(const struct ubifs_in    135 int ubifs_search_zbranch(const struct ubifs_info *c,
126                          const struct ubifs_zn    136                          const struct ubifs_znode *znode,
127                          const union ubifs_key    137                          const union ubifs_key *key, int *n)
128 {                                                 138 {
129         int beg = 0, end = znode->child_cnt, m !! 139         int beg = 0, end = znode->child_cnt, uninitialized_var(mid);
130         int cmp;                               !! 140         int uninitialized_var(cmp);
131         const struct ubifs_zbranch *zbr = &zno    141         const struct ubifs_zbranch *zbr = &znode->zbranch[0];
132                                                   142 
133         ubifs_assert(c, end > beg);            !! 143         ubifs_assert(end > beg);
134                                                   144 
135         while (end > beg) {                       145         while (end > beg) {
136                 mid = (beg + end) >> 1;           146                 mid = (beg + end) >> 1;
137                 cmp = keys_cmp(c, key, &zbr[mi    147                 cmp = keys_cmp(c, key, &zbr[mid].key);
138                 if (cmp > 0)                      148                 if (cmp > 0)
139                         beg = mid + 1;            149                         beg = mid + 1;
140                 else if (cmp < 0)                 150                 else if (cmp < 0)
141                         end = mid;                151                         end = mid;
142                 else {                            152                 else {
143                         *n = mid;                 153                         *n = mid;
144                         return 1;                 154                         return 1;
145                 }                                 155                 }
146         }                                         156         }
147                                                   157 
148         *n = end - 1;                             158         *n = end - 1;
149                                                   159 
150         /* The insert point is after *n */        160         /* The insert point is after *n */
151         ubifs_assert(c, *n >= -1 && *n < znode !! 161         ubifs_assert(*n >= -1 && *n < znode->child_cnt);
152         if (*n == -1)                             162         if (*n == -1)
153                 ubifs_assert(c, keys_cmp(c, ke !! 163                 ubifs_assert(keys_cmp(c, key, &zbr[0].key) < 0);
154         else                                      164         else
155                 ubifs_assert(c, keys_cmp(c, ke !! 165                 ubifs_assert(keys_cmp(c, key, &zbr[*n].key) > 0);
156         if (*n + 1 < znode->child_cnt)            166         if (*n + 1 < znode->child_cnt)
157                 ubifs_assert(c, keys_cmp(c, ke !! 167                 ubifs_assert(keys_cmp(c, key, &zbr[*n + 1].key) < 0);
158                                                   168 
159         return 0;                                 169         return 0;
160 }                                                 170 }
161                                                   171 
162 /**                                               172 /**
163  * ubifs_tnc_postorder_first - find first znod    173  * ubifs_tnc_postorder_first - find first znode to do postorder tree traversal.
164  * @znode: znode to start at (root of the sub-    174  * @znode: znode to start at (root of the sub-tree to traverse)
165  *                                                175  *
166  * Find the lowest leftmost znode in a subtree    176  * Find the lowest leftmost znode in a subtree of the TNC tree. The LNC is
167  * ignored.                                       177  * ignored.
168  */                                               178  */
169 struct ubifs_znode *ubifs_tnc_postorder_first(    179 struct ubifs_znode *ubifs_tnc_postorder_first(struct ubifs_znode *znode)
170 {                                                 180 {
171         if (unlikely(!znode))                     181         if (unlikely(!znode))
172                 return NULL;                      182                 return NULL;
173                                                   183 
174         while (znode->level > 0) {                184         while (znode->level > 0) {
175                 struct ubifs_znode *child;        185                 struct ubifs_znode *child;
176                                                   186 
177                 child = ubifs_tnc_find_child(z    187                 child = ubifs_tnc_find_child(znode, 0);
178                 if (!child)                       188                 if (!child)
179                         return znode;             189                         return znode;
180                 znode = child;                    190                 znode = child;
181         }                                         191         }
182                                                   192 
183         return znode;                             193         return znode;
184 }                                                 194 }
185                                                   195 
186 /**                                               196 /**
187  * ubifs_tnc_postorder_next - next TNC tree el    197  * ubifs_tnc_postorder_next - next TNC tree element in postorder traversal.
188  * @c: UBIFS file-system description object    << 
189  * @znode: previous znode                         198  * @znode: previous znode
190  *                                                199  *
191  * This function implements postorder TNC trav    200  * This function implements postorder TNC traversal. The LNC is ignored.
192  * Returns the next element or %NULL if @znode    201  * Returns the next element or %NULL if @znode is already the last one.
193  */                                               202  */
194 struct ubifs_znode *ubifs_tnc_postorder_next(c !! 203 struct ubifs_znode *ubifs_tnc_postorder_next(struct ubifs_znode *znode)
195                                              s << 
196 {                                                 204 {
197         struct ubifs_znode *zn;                   205         struct ubifs_znode *zn;
198                                                   206 
199         ubifs_assert(c, znode);                !! 207         ubifs_assert(znode);
200         if (unlikely(!znode->parent))             208         if (unlikely(!znode->parent))
201                 return NULL;                      209                 return NULL;
202                                                   210 
203         /* Switch to the next index in the par    211         /* Switch to the next index in the parent */
204         zn = ubifs_tnc_find_child(znode->paren    212         zn = ubifs_tnc_find_child(znode->parent, znode->iip + 1);
205         if (!zn)                                  213         if (!zn)
206                 /* This is in fact the last ch    214                 /* This is in fact the last child, return parent */
207                 return znode->parent;             215                 return znode->parent;
208                                                   216 
209         /* Go to the first znode in this new s    217         /* Go to the first znode in this new subtree */
210         return ubifs_tnc_postorder_first(zn);     218         return ubifs_tnc_postorder_first(zn);
211 }                                                 219 }
212                                                   220 
213 /**                                               221 /**
214  * ubifs_destroy_tnc_subtree - destroy all zno    222  * ubifs_destroy_tnc_subtree - destroy all znodes connected to a subtree.
215  * @c: UBIFS file-system description object    << 
216  * @znode: znode defining subtree to destroy      223  * @znode: znode defining subtree to destroy
217  *                                                224  *
218  * This function destroys subtree of the TNC t    225  * This function destroys subtree of the TNC tree. Returns number of clean
219  * znodes in the subtree.                         226  * znodes in the subtree.
220  */                                               227  */
221 long ubifs_destroy_tnc_subtree(const struct ub !! 228 long ubifs_destroy_tnc_subtree(struct ubifs_znode *znode)
222                                struct ubifs_zn << 
223 {                                                 229 {
224         struct ubifs_znode *zn = ubifs_tnc_pos    230         struct ubifs_znode *zn = ubifs_tnc_postorder_first(znode);
225         long clean_freed = 0;                     231         long clean_freed = 0;
226         int n;                                    232         int n;
227                                                   233 
228         ubifs_assert(c, zn);                   !! 234         ubifs_assert(zn);
229         while (1) {                               235         while (1) {
230                 for (n = 0; n < zn->child_cnt;    236                 for (n = 0; n < zn->child_cnt; n++) {
231                         if (!zn->zbranch[n].zn    237                         if (!zn->zbranch[n].znode)
232                                 continue;         238                                 continue;
233                                                   239 
234                         if (zn->level > 0 &&      240                         if (zn->level > 0 &&
235                             !ubifs_zn_dirty(zn    241                             !ubifs_zn_dirty(zn->zbranch[n].znode))
236                                 clean_freed +=    242                                 clean_freed += 1;
237                                                   243 
238                         cond_resched();           244                         cond_resched();
239                         kfree(zn->zbranch[n].z    245                         kfree(zn->zbranch[n].znode);
240                 }                                 246                 }
241                                                   247 
242                 if (zn == znode) {                248                 if (zn == znode) {
243                         if (!ubifs_zn_dirty(zn    249                         if (!ubifs_zn_dirty(zn))
244                                 clean_freed +=    250                                 clean_freed += 1;
245                         kfree(zn);                251                         kfree(zn);
246                         return clean_freed;       252                         return clean_freed;
247                 }                                 253                 }
248                                                   254 
249                 zn = ubifs_tnc_postorder_next( !! 255                 zn = ubifs_tnc_postorder_next(zn);
250         }                                         256         }
251 }                                                 257 }
252                                                   258 
253 /**                                               259 /**
254  * ubifs_destroy_tnc_tree - destroy all znodes << 
255  * @c: UBIFS file-system description object    << 
256  *                                             << 
257  * This function destroys the whole TNC tree a << 
258  * count.                                      << 
259  */                                            << 
260 void ubifs_destroy_tnc_tree(struct ubifs_info  << 
261 {                                              << 
262         long n, freed;                         << 
263                                                << 
264         if (!c->zroot.znode)                   << 
265                 return;                        << 
266                                                << 
267         n = atomic_long_read(&c->clean_zn_cnt) << 
268         freed = ubifs_destroy_tnc_subtree(c, c << 
269         ubifs_assert(c, freed == n);           << 
270         atomic_long_sub(n, &ubifs_clean_zn_cnt << 
271                                                << 
272         c->zroot.znode = NULL;                 << 
273 }                                              << 
274                                                << 
275 /**                                            << 
276  * read_znode - read an indexing node from fla    260  * read_znode - read an indexing node from flash and fill znode.
277  * @c: UBIFS file-system description object       261  * @c: UBIFS file-system description object
278  * @zzbr: the zbranch describing the node to r !! 262  * @lnum: LEB of the indexing node to read
                                                   >> 263  * @offs: node offset
                                                   >> 264  * @len: node length
279  * @znode: znode to read to                       265  * @znode: znode to read to
280  *                                                266  *
281  * This function reads an indexing node from t    267  * This function reads an indexing node from the flash media and fills znode
282  * with the read data. Returns zero in case of    268  * with the read data. Returns zero in case of success and a negative error
283  * code in case of failure. The read indexing     269  * code in case of failure. The read indexing node is validated and if anything
284  * is wrong with it, this function prints comp    270  * is wrong with it, this function prints complaint messages and returns
285  * %-EINVAL.                                      271  * %-EINVAL.
286  */                                               272  */
287 static int read_znode(struct ubifs_info *c, st !! 273 static int read_znode(struct ubifs_info *c, int lnum, int offs, int len,
288                       struct ubifs_znode *znod    274                       struct ubifs_znode *znode)
289 {                                                 275 {
290         int lnum = zzbr->lnum;                 << 
291         int offs = zzbr->offs;                 << 
292         int len = zzbr->len;                   << 
293         int i, err, type, cmp;                    276         int i, err, type, cmp;
294         struct ubifs_idx_node *idx;               277         struct ubifs_idx_node *idx;
295                                                   278 
296         idx = kmalloc(c->max_idx_node_sz, GFP_    279         idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
297         if (!idx)                                 280         if (!idx)
298                 return -ENOMEM;                   281                 return -ENOMEM;
299                                                   282 
300         err = ubifs_read_node(c, idx, UBIFS_ID    283         err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
301         if (err < 0) {                            284         if (err < 0) {
302                 kfree(idx);                       285                 kfree(idx);
303                 return err;                       286                 return err;
304         }                                         287         }
305                                                   288 
306         err = ubifs_node_check_hash(c, idx, zz << 
307         if (err) {                             << 
308                 ubifs_bad_hash(c, idx, zzbr->h << 
309                 kfree(idx);                    << 
310                 return err;                    << 
311         }                                      << 
312                                                << 
313         znode->child_cnt = le16_to_cpu(idx->ch    289         znode->child_cnt = le16_to_cpu(idx->child_cnt);
314         znode->level = le16_to_cpu(idx->level)    290         znode->level = le16_to_cpu(idx->level);
315                                                   291 
316         dbg_tnc("LEB %d:%d, level %d, %d branc    292         dbg_tnc("LEB %d:%d, level %d, %d branch",
317                 lnum, offs, znode->level, znod    293                 lnum, offs, znode->level, znode->child_cnt);
318                                                   294 
319         if (znode->child_cnt > c->fanout || zn    295         if (znode->child_cnt > c->fanout || znode->level > UBIFS_MAX_LEVELS) {
320                 ubifs_err(c, "current fanout %    296                 ubifs_err(c, "current fanout %d, branch count %d",
321                           c->fanout, znode->ch    297                           c->fanout, znode->child_cnt);
322                 ubifs_err(c, "max levels %d, z    298                 ubifs_err(c, "max levels %d, znode level %d",
323                           UBIFS_MAX_LEVELS, zn    299                           UBIFS_MAX_LEVELS, znode->level);
324                 err = 1;                          300                 err = 1;
325                 goto out_dump;                    301                 goto out_dump;
326         }                                         302         }
327                                                   303 
328         for (i = 0; i < znode->child_cnt; i++)    304         for (i = 0; i < znode->child_cnt; i++) {
329                 struct ubifs_branch *br = ubif !! 305                 const struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
330                 struct ubifs_zbranch *zbr = &z    306                 struct ubifs_zbranch *zbr = &znode->zbranch[i];
331                                                   307 
332                 key_read(c, &br->key, &zbr->ke    308                 key_read(c, &br->key, &zbr->key);
333                 zbr->lnum = le32_to_cpu(br->ln    309                 zbr->lnum = le32_to_cpu(br->lnum);
334                 zbr->offs = le32_to_cpu(br->of    310                 zbr->offs = le32_to_cpu(br->offs);
335                 zbr->len  = le32_to_cpu(br->le    311                 zbr->len  = le32_to_cpu(br->len);
336                 ubifs_copy_hash(c, ubifs_branc << 
337                 zbr->znode = NULL;                312                 zbr->znode = NULL;
338                                                   313 
339                 /* Validate branch */             314                 /* Validate branch */
340                                                   315 
341                 if (zbr->lnum < c->main_first     316                 if (zbr->lnum < c->main_first ||
342                     zbr->lnum >= c->leb_cnt ||    317                     zbr->lnum >= c->leb_cnt || zbr->offs < 0 ||
343                     zbr->offs + zbr->len > c->    318                     zbr->offs + zbr->len > c->leb_size || zbr->offs & 7) {
344                         ubifs_err(c, "bad bran    319                         ubifs_err(c, "bad branch %d", i);
345                         err = 2;                  320                         err = 2;
346                         goto out_dump;            321                         goto out_dump;
347                 }                                 322                 }
348                                                   323 
349                 switch (key_type(c, &zbr->key)    324                 switch (key_type(c, &zbr->key)) {
350                 case UBIFS_INO_KEY:               325                 case UBIFS_INO_KEY:
351                 case UBIFS_DATA_KEY:              326                 case UBIFS_DATA_KEY:
352                 case UBIFS_DENT_KEY:              327                 case UBIFS_DENT_KEY:
353                 case UBIFS_XENT_KEY:              328                 case UBIFS_XENT_KEY:
354                         break;                    329                         break;
355                 default:                          330                 default:
356                         ubifs_err(c, "bad key     331                         ubifs_err(c, "bad key type at slot %d: %d",
357                                   i, key_type(    332                                   i, key_type(c, &zbr->key));
358                         err = 3;                  333                         err = 3;
359                         goto out_dump;            334                         goto out_dump;
360                 }                                 335                 }
361                                                   336 
362                 if (znode->level)                 337                 if (znode->level)
363                         continue;                 338                         continue;
364                                                   339 
365                 type = key_type(c, &zbr->key);    340                 type = key_type(c, &zbr->key);
366                 if (c->ranges[type].max_len ==    341                 if (c->ranges[type].max_len == 0) {
367                         if (zbr->len != c->ran    342                         if (zbr->len != c->ranges[type].len) {
368                                 ubifs_err(c, "    343                                 ubifs_err(c, "bad target node (type %d) length (%d)",
369                                           type    344                                           type, zbr->len);
370                                 ubifs_err(c, "    345                                 ubifs_err(c, "have to be %d", c->ranges[type].len);
371                                 err = 4;          346                                 err = 4;
372                                 goto out_dump;    347                                 goto out_dump;
373                         }                         348                         }
374                 } else if (zbr->len < c->range    349                 } else if (zbr->len < c->ranges[type].min_len ||
375                            zbr->len > c->range    350                            zbr->len > c->ranges[type].max_len) {
376                         ubifs_err(c, "bad targ    351                         ubifs_err(c, "bad target node (type %d) length (%d)",
377                                   type, zbr->l    352                                   type, zbr->len);
378                         ubifs_err(c, "have to     353                         ubifs_err(c, "have to be in range of %d-%d",
379                                   c->ranges[ty    354                                   c->ranges[type].min_len,
380                                   c->ranges[ty    355                                   c->ranges[type].max_len);
381                         err = 5;                  356                         err = 5;
382                         goto out_dump;            357                         goto out_dump;
383                 }                                 358                 }
384         }                                         359         }
385                                                   360 
386         /*                                        361         /*
387          * Ensure that the next key is greater    362          * Ensure that the next key is greater or equivalent to the
388          * previous one.                          363          * previous one.
389          */                                       364          */
390         for (i = 0; i < znode->child_cnt - 1;     365         for (i = 0; i < znode->child_cnt - 1; i++) {
391                 const union ubifs_key *key1, *    366                 const union ubifs_key *key1, *key2;
392                                                   367 
393                 key1 = &znode->zbranch[i].key;    368                 key1 = &znode->zbranch[i].key;
394                 key2 = &znode->zbranch[i + 1].    369                 key2 = &znode->zbranch[i + 1].key;
395                                                   370 
396                 cmp = keys_cmp(c, key1, key2);    371                 cmp = keys_cmp(c, key1, key2);
397                 if (cmp > 0) {                    372                 if (cmp > 0) {
398                         ubifs_err(c, "bad key     373                         ubifs_err(c, "bad key order (keys %d and %d)", i, i + 1);
399                         err = 6;                  374                         err = 6;
400                         goto out_dump;            375                         goto out_dump;
401                 } else if (cmp == 0 && !is_has    376                 } else if (cmp == 0 && !is_hash_key(c, key1)) {
402                         /* These can only be k    377                         /* These can only be keys with colliding hash */
403                         ubifs_err(c, "keys %d     378                         ubifs_err(c, "keys %d and %d are not hashed but equivalent",
404                                   i, i + 1);      379                                   i, i + 1);
405                         err = 7;                  380                         err = 7;
406                         goto out_dump;            381                         goto out_dump;
407                 }                                 382                 }
408         }                                         383         }
409                                                   384 
410         kfree(idx);                               385         kfree(idx);
411         return 0;                                 386         return 0;
412                                                   387 
413 out_dump:                                         388 out_dump:
414         ubifs_err(c, "bad indexing node at LEB    389         ubifs_err(c, "bad indexing node at LEB %d:%d, error %d", lnum, offs, err);
415         ubifs_dump_node(c, idx, c->max_idx_nod !! 390         ubifs_dump_node(c, idx);
416         kfree(idx);                               391         kfree(idx);
417         return -EINVAL;                           392         return -EINVAL;
418 }                                                 393 }
419                                                   394 
420 /**                                               395 /**
421  * ubifs_load_znode - load znode to TNC cache.    396  * ubifs_load_znode - load znode to TNC cache.
422  * @c: UBIFS file-system description object       397  * @c: UBIFS file-system description object
423  * @zbr: znode branch                             398  * @zbr: znode branch
424  * @parent: znode's parent                        399  * @parent: znode's parent
425  * @iip: index in parent                          400  * @iip: index in parent
426  *                                                401  *
427  * This function loads znode pointed to by @zb    402  * This function loads znode pointed to by @zbr into the TNC cache and
428  * returns pointer to it in case of success an    403  * returns pointer to it in case of success and a negative error code in case
429  * of failure.                                    404  * of failure.
430  */                                               405  */
431 struct ubifs_znode *ubifs_load_znode(struct ub    406 struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
432                                      struct ub    407                                      struct ubifs_zbranch *zbr,
433                                      struct ub    408                                      struct ubifs_znode *parent, int iip)
434 {                                                 409 {
435         int err;                                  410         int err;
436         struct ubifs_znode *znode;                411         struct ubifs_znode *znode;
437                                                   412 
438         ubifs_assert(c, !zbr->znode);          !! 413         ubifs_assert(!zbr->znode);
439         /*                                        414         /*
440          * A slab cache is not presently used     415          * A slab cache is not presently used for znodes because the znode size
441          * depends on the fanout which is stor    416          * depends on the fanout which is stored in the superblock.
442          */                                       417          */
443         znode = kzalloc(c->max_znode_sz, GFP_N    418         znode = kzalloc(c->max_znode_sz, GFP_NOFS);
444         if (!znode)                               419         if (!znode)
445                 return ERR_PTR(-ENOMEM);          420                 return ERR_PTR(-ENOMEM);
446                                                   421 
447         err = read_znode(c, zbr, znode);       !! 422         err = read_znode(c, zbr->lnum, zbr->offs, zbr->len, znode);
448         if (err)                                  423         if (err)
449                 goto out;                         424                 goto out;
450                                                   425 
451         atomic_long_inc(&c->clean_zn_cnt);        426         atomic_long_inc(&c->clean_zn_cnt);
452                                                   427 
453         /*                                        428         /*
454          * Increment the global clean znode co    429          * Increment the global clean znode counter as well. It is OK that
455          * global and per-FS clean znode count    430          * global and per-FS clean znode counters may be inconsistent for some
456          * short time (because we might be pre    431          * short time (because we might be preempted at this point), the global
457          * one is only used in shrinker.          432          * one is only used in shrinker.
458          */                                       433          */
459         atomic_long_inc(&ubifs_clean_zn_cnt);     434         atomic_long_inc(&ubifs_clean_zn_cnt);
460                                                   435 
461         zbr->znode = znode;                       436         zbr->znode = znode;
462         znode->parent = parent;                   437         znode->parent = parent;
463         znode->time = ktime_get_seconds();     !! 438         znode->time = get_seconds();
464         znode->iip = iip;                         439         znode->iip = iip;
465                                                   440 
466         return znode;                             441         return znode;
467                                                   442 
468 out:                                              443 out:
469         kfree(znode);                             444         kfree(znode);
470         return ERR_PTR(err);                      445         return ERR_PTR(err);
471 }                                                 446 }
472                                                   447 
473 /**                                               448 /**
474  * ubifs_tnc_read_node - read a leaf node from    449  * ubifs_tnc_read_node - read a leaf node from the flash media.
475  * @c: UBIFS file-system description object       450  * @c: UBIFS file-system description object
476  * @zbr: key and position of the node             451  * @zbr: key and position of the node
477  * @node: node is returned here                   452  * @node: node is returned here
478  *                                                453  *
479  * This function reads a node defined by @zbr     454  * This function reads a node defined by @zbr from the flash media. Returns
480  * zero in case of success or a negative error !! 455  * zero in case of success or a negative negative error code in case of
                                                   >> 456  * failure.
481  */                                               457  */
482 int ubifs_tnc_read_node(struct ubifs_info *c,     458 int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
483                         void *node)               459                         void *node)
484 {                                                 460 {
485         union ubifs_key key1, *key = &zbr->key    461         union ubifs_key key1, *key = &zbr->key;
486         int err, type = key_type(c, key);         462         int err, type = key_type(c, key);
487         struct ubifs_wbuf *wbuf;                  463         struct ubifs_wbuf *wbuf;
488                                                   464 
489         /*                                        465         /*
490          * 'zbr' has to point to on-flash node    466          * 'zbr' has to point to on-flash node. The node may sit in a bud and
491          * may even be in a write buffer, so w    467          * may even be in a write buffer, so we have to take care about this.
492          */                                       468          */
493         wbuf = ubifs_get_wbuf(c, zbr->lnum);      469         wbuf = ubifs_get_wbuf(c, zbr->lnum);
494         if (wbuf)                                 470         if (wbuf)
495                 err = ubifs_read_node_wbuf(wbu    471                 err = ubifs_read_node_wbuf(wbuf, node, type, zbr->len,
496                                            zbr    472                                            zbr->lnum, zbr->offs);
497         else                                      473         else
498                 err = ubifs_read_node(c, node,    474                 err = ubifs_read_node(c, node, type, zbr->len, zbr->lnum,
499                                       zbr->off    475                                       zbr->offs);
500                                                   476 
501         if (err) {                                477         if (err) {
502                 dbg_tnck(key, "key ");            478                 dbg_tnck(key, "key ");
503                 return err;                       479                 return err;
504         }                                         480         }
505                                                   481 
506         /* Make sure the key of the read node     482         /* Make sure the key of the read node is correct */
507         key_read(c, node + UBIFS_KEY_OFFSET, &    483         key_read(c, node + UBIFS_KEY_OFFSET, &key1);
508         if (!keys_eq(c, key, &key1)) {            484         if (!keys_eq(c, key, &key1)) {
509                 ubifs_err(c, "bad key in node     485                 ubifs_err(c, "bad key in node at LEB %d:%d",
510                           zbr->lnum, zbr->offs    486                           zbr->lnum, zbr->offs);
511                 dbg_tnck(key, "looked for key     487                 dbg_tnck(key, "looked for key ");
512                 dbg_tnck(&key1, "but found nod    488                 dbg_tnck(&key1, "but found node's key ");
513                 ubifs_dump_node(c, node, zbr-> !! 489                 ubifs_dump_node(c, node);
514                 return -EINVAL;                   490                 return -EINVAL;
515         }                                      << 
516                                                << 
517         err = ubifs_node_check_hash(c, node, z << 
518         if (err) {                             << 
519                 ubifs_bad_hash(c, node, zbr->h << 
520                 return err;                    << 
521         }                                         491         }
522                                                   492 
523         return 0;                                 493         return 0;
524 }                                                 494 }
525                                                   495 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php