usb: gadget: udc: reduce indentation
[linux-2.6-block.git] / fs / ubifs / master.c
1 /*
2  * This file is part of UBIFS.
3  *
4  * Copyright (C) 2006-2008 Nokia Corporation.
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  *
19  * Authors: Artem Bityutskiy (Битюцкий Артём)
20  *          Adrian Hunter
21  */
22
23 /* This file implements reading and writing the master node */
24
25 #include "ubifs.h"
26
27 /**
28  * ubifs_compare_master_node - compare two UBIFS master nodes
29  * @c: UBIFS file-system description object
30  * @m1: the first node
31  * @m2: the second node
32  *
33  * This function compares two UBIFS master nodes. Returns 0 if they are equal
34  * and nonzero if not.
35  */
36 int ubifs_compare_master_node(struct ubifs_info *c, void *m1, void *m2)
37 {
38         int ret;
39         int behind;
40         int hmac_offs = offsetof(struct ubifs_mst_node, hmac);
41
42         /*
43          * Do not compare the common node header since the sequence number and
44          * hence the CRC are different.
45          */
46         ret = memcmp(m1 + UBIFS_CH_SZ, m2 + UBIFS_CH_SZ,
47                      hmac_offs - UBIFS_CH_SZ);
48         if (ret)
49                 return ret;
50
51         /*
52          * Do not compare the embedded HMAC aswell which also must be different
53          * due to the different common node header.
54          */
55         behind = hmac_offs + UBIFS_MAX_HMAC_LEN;
56
57         if (UBIFS_MST_NODE_SZ > behind)
58                 return memcmp(m1 + behind, m2 + behind, UBIFS_MST_NODE_SZ - behind);
59
60         return 0;
61 }
62
63 /**
64  * scan_for_master - search the valid master node.
65  * @c: UBIFS file-system description object
66  *
67  * This function scans the master node LEBs and search for the latest master
68  * node. Returns zero in case of success, %-EUCLEAN if there master area is
69  * corrupted and requires recovery, and a negative error code in case of
70  * failure.
71  */
72 static int scan_for_master(struct ubifs_info *c)
73 {
74         struct ubifs_scan_leb *sleb;
75         struct ubifs_scan_node *snod;
76         int lnum, offs = 0, nodes_cnt, err;
77
78         lnum = UBIFS_MST_LNUM;
79
80         sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
81         if (IS_ERR(sleb))
82                 return PTR_ERR(sleb);
83         nodes_cnt = sleb->nodes_cnt;
84         if (nodes_cnt > 0) {
85                 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
86                                   list);
87                 if (snod->type != UBIFS_MST_NODE)
88                         goto out_dump;
89                 memcpy(c->mst_node, snod->node, snod->len);
90                 offs = snod->offs;
91         }
92         ubifs_scan_destroy(sleb);
93
94         lnum += 1;
95
96         sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
97         if (IS_ERR(sleb))
98                 return PTR_ERR(sleb);
99         if (sleb->nodes_cnt != nodes_cnt)
100                 goto out;
101         if (!sleb->nodes_cnt)
102                 goto out;
103         snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list);
104         if (snod->type != UBIFS_MST_NODE)
105                 goto out_dump;
106         if (snod->offs != offs)
107                 goto out;
108         if (ubifs_compare_master_node(c, c->mst_node, snod->node))
109                 goto out;
110
111         c->mst_offs = offs;
112         ubifs_scan_destroy(sleb);
113
114         if (!ubifs_authenticated(c))
115                 return 0;
116
117         err = ubifs_node_verify_hmac(c, c->mst_node,
118                                      sizeof(struct ubifs_mst_node),
119                                      offsetof(struct ubifs_mst_node, hmac));
120         if (err) {
121                 ubifs_err(c, "Failed to verify master node HMAC");
122                 return -EPERM;
123         }
124
125         return 0;
126
127 out:
128         ubifs_scan_destroy(sleb);
129         return -EUCLEAN;
130
131 out_dump:
132         ubifs_err(c, "unexpected node type %d master LEB %d:%d",
133                   snod->type, lnum, snod->offs);
134         ubifs_scan_destroy(sleb);
135         return -EINVAL;
136 }
137
138 /**
139  * validate_master - validate master node.
140  * @c: UBIFS file-system description object
141  *
142  * This function validates data which was read from master node. Returns zero
143  * if the data is all right and %-EINVAL if not.
144  */
145 static int validate_master(const struct ubifs_info *c)
146 {
147         long long main_sz;
148         int err;
149
150         if (c->max_sqnum >= SQNUM_WATERMARK) {
151                 err = 1;
152                 goto out;
153         }
154
155         if (c->cmt_no >= c->max_sqnum) {
156                 err = 2;
157                 goto out;
158         }
159
160         if (c->highest_inum >= INUM_WATERMARK) {
161                 err = 3;
162                 goto out;
163         }
164
165         if (c->lhead_lnum < UBIFS_LOG_LNUM ||
166             c->lhead_lnum >= UBIFS_LOG_LNUM + c->log_lebs ||
167             c->lhead_offs < 0 || c->lhead_offs >= c->leb_size ||
168             c->lhead_offs & (c->min_io_size - 1)) {
169                 err = 4;
170                 goto out;
171         }
172
173         if (c->zroot.lnum >= c->leb_cnt || c->zroot.lnum < c->main_first ||
174             c->zroot.offs >= c->leb_size || c->zroot.offs & 7) {
175                 err = 5;
176                 goto out;
177         }
178
179         if (c->zroot.len < c->ranges[UBIFS_IDX_NODE].min_len ||
180             c->zroot.len > c->ranges[UBIFS_IDX_NODE].max_len) {
181                 err = 6;
182                 goto out;
183         }
184
185         if (c->gc_lnum >= c->leb_cnt || c->gc_lnum < c->main_first) {
186                 err = 7;
187                 goto out;
188         }
189
190         if (c->ihead_lnum >= c->leb_cnt || c->ihead_lnum < c->main_first ||
191             c->ihead_offs % c->min_io_size || c->ihead_offs < 0 ||
192             c->ihead_offs > c->leb_size || c->ihead_offs & 7) {
193                 err = 8;
194                 goto out;
195         }
196
197         main_sz = (long long)c->main_lebs * c->leb_size;
198         if (c->bi.old_idx_sz & 7 || c->bi.old_idx_sz >= main_sz) {
199                 err = 9;
200                 goto out;
201         }
202
203         if (c->lpt_lnum < c->lpt_first || c->lpt_lnum > c->lpt_last ||
204             c->lpt_offs < 0 || c->lpt_offs + c->nnode_sz > c->leb_size) {
205                 err = 10;
206                 goto out;
207         }
208
209         if (c->nhead_lnum < c->lpt_first || c->nhead_lnum > c->lpt_last ||
210             c->nhead_offs < 0 || c->nhead_offs % c->min_io_size ||
211             c->nhead_offs > c->leb_size) {
212                 err = 11;
213                 goto out;
214         }
215
216         if (c->ltab_lnum < c->lpt_first || c->ltab_lnum > c->lpt_last ||
217             c->ltab_offs < 0 ||
218             c->ltab_offs + c->ltab_sz > c->leb_size) {
219                 err = 12;
220                 goto out;
221         }
222
223         if (c->big_lpt && (c->lsave_lnum < c->lpt_first ||
224             c->lsave_lnum > c->lpt_last || c->lsave_offs < 0 ||
225             c->lsave_offs + c->lsave_sz > c->leb_size)) {
226                 err = 13;
227                 goto out;
228         }
229
230         if (c->lscan_lnum < c->main_first || c->lscan_lnum >= c->leb_cnt) {
231                 err = 14;
232                 goto out;
233         }
234
235         if (c->lst.empty_lebs < 0 || c->lst.empty_lebs > c->main_lebs - 2) {
236                 err = 15;
237                 goto out;
238         }
239
240         if (c->lst.idx_lebs < 0 || c->lst.idx_lebs > c->main_lebs - 1) {
241                 err = 16;
242                 goto out;
243         }
244
245         if (c->lst.total_free < 0 || c->lst.total_free > main_sz ||
246             c->lst.total_free & 7) {
247                 err = 17;
248                 goto out;
249         }
250
251         if (c->lst.total_dirty < 0 || (c->lst.total_dirty & 7)) {
252                 err = 18;
253                 goto out;
254         }
255
256         if (c->lst.total_used < 0 || (c->lst.total_used & 7)) {
257                 err = 19;
258                 goto out;
259         }
260
261         if (c->lst.total_free + c->lst.total_dirty +
262             c->lst.total_used > main_sz) {
263                 err = 20;
264                 goto out;
265         }
266
267         if (c->lst.total_dead + c->lst.total_dark +
268             c->lst.total_used + c->bi.old_idx_sz > main_sz) {
269                 err = 21;
270                 goto out;
271         }
272
273         if (c->lst.total_dead < 0 ||
274             c->lst.total_dead > c->lst.total_free + c->lst.total_dirty ||
275             c->lst.total_dead & 7) {
276                 err = 22;
277                 goto out;
278         }
279
280         if (c->lst.total_dark < 0 ||
281             c->lst.total_dark > c->lst.total_free + c->lst.total_dirty ||
282             c->lst.total_dark & 7) {
283                 err = 23;
284                 goto out;
285         }
286
287         return 0;
288
289 out:
290         ubifs_err(c, "bad master node at offset %d error %d", c->mst_offs, err);
291         ubifs_dump_node(c, c->mst_node);
292         return -EINVAL;
293 }
294
295 /**
296  * ubifs_read_master - read master node.
297  * @c: UBIFS file-system description object
298  *
299  * This function finds and reads the master node during file-system mount. If
300  * the flash is empty, it creates default master node as well. Returns zero in
301  * case of success and a negative error code in case of failure.
302  */
303 int ubifs_read_master(struct ubifs_info *c)
304 {
305         int err, old_leb_cnt;
306
307         c->mst_node = kzalloc(c->mst_node_alsz, GFP_KERNEL);
308         if (!c->mst_node)
309                 return -ENOMEM;
310
311         err = scan_for_master(c);
312         if (err) {
313                 if (err == -EUCLEAN)
314                         err = ubifs_recover_master_node(c);
315                 if (err)
316                         /*
317                          * Note, we do not free 'c->mst_node' here because the
318                          * unmount routine will take care of this.
319                          */
320                         return err;
321         }
322
323         /* Make sure that the recovery flag is clear */
324         c->mst_node->flags &= cpu_to_le32(~UBIFS_MST_RCVRY);
325
326         c->max_sqnum       = le64_to_cpu(c->mst_node->ch.sqnum);
327         c->highest_inum    = le64_to_cpu(c->mst_node->highest_inum);
328         c->cmt_no          = le64_to_cpu(c->mst_node->cmt_no);
329         c->zroot.lnum      = le32_to_cpu(c->mst_node->root_lnum);
330         c->zroot.offs      = le32_to_cpu(c->mst_node->root_offs);
331         c->zroot.len       = le32_to_cpu(c->mst_node->root_len);
332         c->lhead_lnum      = le32_to_cpu(c->mst_node->log_lnum);
333         c->gc_lnum         = le32_to_cpu(c->mst_node->gc_lnum);
334         c->ihead_lnum      = le32_to_cpu(c->mst_node->ihead_lnum);
335         c->ihead_offs      = le32_to_cpu(c->mst_node->ihead_offs);
336         c->bi.old_idx_sz   = le64_to_cpu(c->mst_node->index_size);
337         c->lpt_lnum        = le32_to_cpu(c->mst_node->lpt_lnum);
338         c->lpt_offs        = le32_to_cpu(c->mst_node->lpt_offs);
339         c->nhead_lnum      = le32_to_cpu(c->mst_node->nhead_lnum);
340         c->nhead_offs      = le32_to_cpu(c->mst_node->nhead_offs);
341         c->ltab_lnum       = le32_to_cpu(c->mst_node->ltab_lnum);
342         c->ltab_offs       = le32_to_cpu(c->mst_node->ltab_offs);
343         c->lsave_lnum      = le32_to_cpu(c->mst_node->lsave_lnum);
344         c->lsave_offs      = le32_to_cpu(c->mst_node->lsave_offs);
345         c->lscan_lnum      = le32_to_cpu(c->mst_node->lscan_lnum);
346         c->lst.empty_lebs  = le32_to_cpu(c->mst_node->empty_lebs);
347         c->lst.idx_lebs    = le32_to_cpu(c->mst_node->idx_lebs);
348         old_leb_cnt        = le32_to_cpu(c->mst_node->leb_cnt);
349         c->lst.total_free  = le64_to_cpu(c->mst_node->total_free);
350         c->lst.total_dirty = le64_to_cpu(c->mst_node->total_dirty);
351         c->lst.total_used  = le64_to_cpu(c->mst_node->total_used);
352         c->lst.total_dead  = le64_to_cpu(c->mst_node->total_dead);
353         c->lst.total_dark  = le64_to_cpu(c->mst_node->total_dark);
354
355         ubifs_copy_hash(c, c->mst_node->hash_root_idx, c->zroot.hash);
356
357         c->calc_idx_sz = c->bi.old_idx_sz;
358
359         if (c->mst_node->flags & cpu_to_le32(UBIFS_MST_NO_ORPHS))
360                 c->no_orphs = 1;
361
362         if (old_leb_cnt != c->leb_cnt) {
363                 /* The file system has been resized */
364                 int growth = c->leb_cnt - old_leb_cnt;
365
366                 if (c->leb_cnt < old_leb_cnt ||
367                     c->leb_cnt < UBIFS_MIN_LEB_CNT) {
368                         ubifs_err(c, "bad leb_cnt on master node");
369                         ubifs_dump_node(c, c->mst_node);
370                         return -EINVAL;
371                 }
372
373                 dbg_mnt("Auto resizing (master) from %d LEBs to %d LEBs",
374                         old_leb_cnt, c->leb_cnt);
375                 c->lst.empty_lebs += growth;
376                 c->lst.total_free += growth * (long long)c->leb_size;
377                 c->lst.total_dark += growth * (long long)c->dark_wm;
378
379                 /*
380                  * Reflect changes back onto the master node. N.B. the master
381                  * node gets written immediately whenever mounting (or
382                  * remounting) in read-write mode, so we do not need to write it
383                  * here.
384                  */
385                 c->mst_node->leb_cnt = cpu_to_le32(c->leb_cnt);
386                 c->mst_node->empty_lebs = cpu_to_le32(c->lst.empty_lebs);
387                 c->mst_node->total_free = cpu_to_le64(c->lst.total_free);
388                 c->mst_node->total_dark = cpu_to_le64(c->lst.total_dark);
389         }
390
391         err = validate_master(c);
392         if (err)
393                 return err;
394
395         err = dbg_old_index_check_init(c, &c->zroot);
396
397         return err;
398 }
399
400 /**
401  * ubifs_write_master - write master node.
402  * @c: UBIFS file-system description object
403  *
404  * This function writes the master node. Returns zero in case of success and a
405  * negative error code in case of failure. The master node is written twice to
406  * enable recovery.
407  */
408 int ubifs_write_master(struct ubifs_info *c)
409 {
410         int err, lnum, offs, len;
411
412         ubifs_assert(c, !c->ro_media && !c->ro_mount);
413         if (c->ro_error)
414                 return -EROFS;
415
416         lnum = UBIFS_MST_LNUM;
417         offs = c->mst_offs + c->mst_node_alsz;
418         len = UBIFS_MST_NODE_SZ;
419
420         if (offs + UBIFS_MST_NODE_SZ > c->leb_size) {
421                 err = ubifs_leb_unmap(c, lnum);
422                 if (err)
423                         return err;
424                 offs = 0;
425         }
426
427         c->mst_offs = offs;
428         c->mst_node->highest_inum = cpu_to_le64(c->highest_inum);
429
430         ubifs_copy_hash(c, c->zroot.hash, c->mst_node->hash_root_idx);
431         err = ubifs_write_node_hmac(c, c->mst_node, len, lnum, offs,
432                                     offsetof(struct ubifs_mst_node, hmac));
433         if (err)
434                 return err;
435
436         lnum += 1;
437
438         if (offs == 0) {
439                 err = ubifs_leb_unmap(c, lnum);
440                 if (err)
441                         return err;
442         }
443         err = ubifs_write_node_hmac(c, c->mst_node, len, lnum, offs,
444                                     offsetof(struct ubifs_mst_node, hmac));
445
446         return err;
447 }