blob: 95f2a412508ec2e02b1c5ba8b57c7f02ee7774cc [file] [log] [blame]
Stefan Roese2fc10f62009-03-19 15:35:05 +01001/*
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/*
24 * This file implements UBIFS initialization and VFS superblock operations. Some
25 * initialization stuff which is rather large and complex is placed at
26 * corresponding subsystems, but most of it is here.
27 */
28
29#include "ubifs.h"
30#include <linux/math64.h>
31
32#define INODE_LOCKED_MAX 64
33
34struct super_block *ubifs_sb;
35static struct inode *inodes_locked_down[INODE_LOCKED_MAX];
36
37/* shrinker.c */
38
39/* List of all UBIFS file-system instances */
40struct list_head ubifs_infos;
41
42/* linux/fs/super.c */
43
44static int sb_set(struct super_block *sb, void *data)
45{
46 dev_t *dev = data;
47
48 sb->s_dev = *dev;
49 return 0;
50}
51
52/**
53 * sget - find or create a superblock
54 * @type: filesystem type superblock should belong to
55 * @test: comparison callback
56 * @set: setup callback
57 * @data: argument to each of them
58 */
59struct super_block *sget(struct file_system_type *type,
60 int (*test)(struct super_block *,void *),
61 int (*set)(struct super_block *,void *),
62 void *data)
63{
64 struct super_block *s = NULL;
65 int err;
66
67 s = kzalloc(sizeof(struct super_block), GFP_USER);
68 if (!s) {
69 err = -ENOMEM;
70 return ERR_PTR(err);
71 }
72
73 INIT_LIST_HEAD(&s->s_instances);
74 INIT_LIST_HEAD(&s->s_inodes);
75 s->s_time_gran = 1000000000;
76
77 err = set(s, data);
78 if (err) {
79 return ERR_PTR(err);
80 }
81 s->s_type = type;
82 strncpy(s->s_id, type->name, sizeof(s->s_id));
83 list_add(&s->s_instances, &type->fs_supers);
84 return s;
85}
86
87/**
88 * validate_inode - validate inode.
89 * @c: UBIFS file-system description object
90 * @inode: the inode to validate
91 *
92 * This is a helper function for 'ubifs_iget()' which validates various fields
93 * of a newly built inode to make sure they contain sane values and prevent
94 * possible vulnerabilities. Returns zero if the inode is all right and
95 * a non-zero error code if not.
96 */
97static int validate_inode(struct ubifs_info *c, const struct inode *inode)
98{
99 int err;
100 const struct ubifs_inode *ui = ubifs_inode(inode);
101
102 if (inode->i_size > c->max_inode_sz) {
103 ubifs_err("inode is too large (%lld)",
104 (long long)inode->i_size);
105 return 1;
106 }
107
108 if (ui->compr_type < 0 || ui->compr_type >= UBIFS_COMPR_TYPES_CNT) {
109 ubifs_err("unknown compression type %d", ui->compr_type);
110 return 2;
111 }
112
113 if (ui->data_len < 0 || ui->data_len > UBIFS_MAX_INO_DATA)
114 return 4;
115
116 if (!ubifs_compr_present(ui->compr_type)) {
117 ubifs_warn("inode %lu uses '%s' compression, but it was not "
118 "compiled in", inode->i_ino,
119 ubifs_compr_name(ui->compr_type));
120 }
121
122 err = dbg_check_dir_size(c, inode);
123 return err;
124}
125
126struct inode *iget_locked(struct super_block *sb, unsigned long ino)
127{
128 struct inode *inode;
129
130 inode = (struct inode *)malloc(sizeof(struct ubifs_inode));
131 if (inode) {
132 inode->i_ino = ino;
133 inode->i_sb = sb;
134 list_add(&inode->i_sb_list, &sb->s_inodes);
135 inode->i_state = I_LOCK | I_NEW;
136 }
137
138 return inode;
139}
140
141int ubifs_iput(struct inode *inode)
142{
143 list_del_init(&inode->i_sb_list);
144
145 free(inode);
146 return 0;
147}
148
149/*
150 * Lock (save) inode in inode array for readback after recovery
151 */
152void iput(struct inode *inode)
153{
154 int i;
155 struct inode *ino;
156
157 /*
158 * Search end of list
159 */
160 for (i = 0; i < INODE_LOCKED_MAX; i++) {
161 if (inodes_locked_down[i] == NULL)
162 break;
163 }
164
165 if (i >= INODE_LOCKED_MAX) {
166 ubifs_err("Error, can't lock (save) more inodes while recovery!!!");
167 return;
168 }
169
170 /*
171 * Allocate and use new inode
172 */
173 ino = (struct inode *)malloc(sizeof(struct ubifs_inode));
174 memcpy(ino, inode, sizeof(struct ubifs_inode));
175
176 /*
177 * Finally save inode in array
178 */
179 inodes_locked_down[i] = ino;
180}
181
182struct inode *ubifs_iget(struct super_block *sb, unsigned long inum)
183{
184 int err;
185 union ubifs_key key;
186 struct ubifs_ino_node *ino;
187 struct ubifs_info *c = sb->s_fs_info;
188 struct inode *inode;
189 struct ubifs_inode *ui;
190 int i;
191
192 dbg_gen("inode %lu", inum);
193
194 /*
195 * U-Boot special handling of locked down inodes via recovery
196 * e.g. ubifs_recover_size()
197 */
198 for (i = 0; i < INODE_LOCKED_MAX; i++) {
199 /*
200 * Exit on last entry (NULL), inode not found in list
201 */
202 if (inodes_locked_down[i] == NULL)
203 break;
204
205 if (inodes_locked_down[i]->i_ino == inum) {
206 /*
207 * We found the locked down inode in our array,
208 * so just return this pointer instead of creating
209 * a new one.
210 */
211 return inodes_locked_down[i];
212 }
213 }
214
215 inode = iget_locked(sb, inum);
216 if (!inode)
217 return ERR_PTR(-ENOMEM);
218 if (!(inode->i_state & I_NEW))
219 return inode;
220 ui = ubifs_inode(inode);
221
222 ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
223 if (!ino) {
224 err = -ENOMEM;
225 goto out;
226 }
227
228 ino_key_init(c, &key, inode->i_ino);
229
230 err = ubifs_tnc_lookup(c, &key, ino);
231 if (err)
232 goto out_ino;
233
234 inode->i_flags |= (S_NOCMTIME | S_NOATIME);
235 inode->i_nlink = le32_to_cpu(ino->nlink);
236 inode->i_uid = le32_to_cpu(ino->uid);
237 inode->i_gid = le32_to_cpu(ino->gid);
238 inode->i_atime.tv_sec = (int64_t)le64_to_cpu(ino->atime_sec);
239 inode->i_atime.tv_nsec = le32_to_cpu(ino->atime_nsec);
240 inode->i_mtime.tv_sec = (int64_t)le64_to_cpu(ino->mtime_sec);
241 inode->i_mtime.tv_nsec = le32_to_cpu(ino->mtime_nsec);
242 inode->i_ctime.tv_sec = (int64_t)le64_to_cpu(ino->ctime_sec);
243 inode->i_ctime.tv_nsec = le32_to_cpu(ino->ctime_nsec);
244 inode->i_mode = le32_to_cpu(ino->mode);
245 inode->i_size = le64_to_cpu(ino->size);
246
247 ui->data_len = le32_to_cpu(ino->data_len);
248 ui->flags = le32_to_cpu(ino->flags);
249 ui->compr_type = le16_to_cpu(ino->compr_type);
250 ui->creat_sqnum = le64_to_cpu(ino->creat_sqnum);
251 ui->synced_i_size = ui->ui_size = inode->i_size;
252
253 err = validate_inode(c, inode);
254 if (err)
255 goto out_invalid;
256
257 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
258 if (ui->data_len <= 0 || ui->data_len > UBIFS_MAX_INO_DATA) {
259 err = 12;
260 goto out_invalid;
261 }
262 ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
263 if (!ui->data) {
264 err = -ENOMEM;
265 goto out_ino;
266 }
267 memcpy(ui->data, ino->data, ui->data_len);
268 ((char *)ui->data)[ui->data_len] = '\0';
269 }
270
271 kfree(ino);
272 inode->i_state &= ~(I_LOCK | I_NEW);
273 return inode;
274
275out_invalid:
276 ubifs_err("inode %lu validation failed, error %d", inode->i_ino, err);
277 dbg_dump_node(c, ino);
278 dbg_dump_inode(c, inode);
279 err = -EINVAL;
280out_ino:
281 kfree(ino);
282out:
283 ubifs_err("failed to read inode %lu, error %d", inode->i_ino, err);
284 return ERR_PTR(err);
285}
286
287/**
288 * init_constants_early - initialize UBIFS constants.
289 * @c: UBIFS file-system description object
290 *
291 * This function initialize UBIFS constants which do not need the superblock to
292 * be read. It also checks that the UBI volume satisfies basic UBIFS
293 * requirements. Returns zero in case of success and a negative error code in
294 * case of failure.
295 */
296static int init_constants_early(struct ubifs_info *c)
297{
298 if (c->vi.corrupted) {
299 ubifs_warn("UBI volume is corrupted - read-only mode");
300 c->ro_media = 1;
301 }
302
303 if (c->di.ro_mode) {
304 ubifs_msg("read-only UBI device");
305 c->ro_media = 1;
306 }
307
308 if (c->vi.vol_type == UBI_STATIC_VOLUME) {
309 ubifs_msg("static UBI volume - read-only mode");
310 c->ro_media = 1;
311 }
312
313 c->leb_cnt = c->vi.size;
314 c->leb_size = c->vi.usable_leb_size;
315 c->half_leb_size = c->leb_size / 2;
316 c->min_io_size = c->di.min_io_size;
317 c->min_io_shift = fls(c->min_io_size) - 1;
318
319 if (c->leb_size < UBIFS_MIN_LEB_SZ) {
320 ubifs_err("too small LEBs (%d bytes), min. is %d bytes",
321 c->leb_size, UBIFS_MIN_LEB_SZ);
322 return -EINVAL;
323 }
324
325 if (c->leb_cnt < UBIFS_MIN_LEB_CNT) {
326 ubifs_err("too few LEBs (%d), min. is %d",
327 c->leb_cnt, UBIFS_MIN_LEB_CNT);
328 return -EINVAL;
329 }
330
331 if (!is_power_of_2(c->min_io_size)) {
332 ubifs_err("bad min. I/O size %d", c->min_io_size);
333 return -EINVAL;
334 }
335
336 /*
337 * UBIFS aligns all node to 8-byte boundary, so to make function in
338 * io.c simpler, assume minimum I/O unit size to be 8 bytes if it is
339 * less than 8.
340 */
341 if (c->min_io_size < 8) {
342 c->min_io_size = 8;
343 c->min_io_shift = 3;
344 }
345
346 c->ref_node_alsz = ALIGN(UBIFS_REF_NODE_SZ, c->min_io_size);
347 c->mst_node_alsz = ALIGN(UBIFS_MST_NODE_SZ, c->min_io_size);
348
349 /*
350 * Initialize node length ranges which are mostly needed for node
351 * length validation.
352 */
353 c->ranges[UBIFS_PAD_NODE].len = UBIFS_PAD_NODE_SZ;
354 c->ranges[UBIFS_SB_NODE].len = UBIFS_SB_NODE_SZ;
355 c->ranges[UBIFS_MST_NODE].len = UBIFS_MST_NODE_SZ;
356 c->ranges[UBIFS_REF_NODE].len = UBIFS_REF_NODE_SZ;
357 c->ranges[UBIFS_TRUN_NODE].len = UBIFS_TRUN_NODE_SZ;
358 c->ranges[UBIFS_CS_NODE].len = UBIFS_CS_NODE_SZ;
359
360 c->ranges[UBIFS_INO_NODE].min_len = UBIFS_INO_NODE_SZ;
361 c->ranges[UBIFS_INO_NODE].max_len = UBIFS_MAX_INO_NODE_SZ;
362 c->ranges[UBIFS_ORPH_NODE].min_len =
363 UBIFS_ORPH_NODE_SZ + sizeof(__le64);
364 c->ranges[UBIFS_ORPH_NODE].max_len = c->leb_size;
365 c->ranges[UBIFS_DENT_NODE].min_len = UBIFS_DENT_NODE_SZ;
366 c->ranges[UBIFS_DENT_NODE].max_len = UBIFS_MAX_DENT_NODE_SZ;
367 c->ranges[UBIFS_XENT_NODE].min_len = UBIFS_XENT_NODE_SZ;
368 c->ranges[UBIFS_XENT_NODE].max_len = UBIFS_MAX_XENT_NODE_SZ;
369 c->ranges[UBIFS_DATA_NODE].min_len = UBIFS_DATA_NODE_SZ;
370 c->ranges[UBIFS_DATA_NODE].max_len = UBIFS_MAX_DATA_NODE_SZ;
371 /*
372 * Minimum indexing node size is amended later when superblock is
373 * read and the key length is known.
374 */
375 c->ranges[UBIFS_IDX_NODE].min_len = UBIFS_IDX_NODE_SZ + UBIFS_BRANCH_SZ;
376 /*
377 * Maximum indexing node size is amended later when superblock is
378 * read and the fanout is known.
379 */
380 c->ranges[UBIFS_IDX_NODE].max_len = INT_MAX;
381
382 /*
383 * Initialize dead and dark LEB space watermarks. See gc.c for comments
384 * about these values.
385 */
386 c->dead_wm = ALIGN(MIN_WRITE_SZ, c->min_io_size);
387 c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size);
388
389 /*
390 * Calculate how many bytes would be wasted at the end of LEB if it was
391 * fully filled with data nodes of maximum size. This is used in
392 * calculations when reporting free space.
393 */
394 c->leb_overhead = c->leb_size % UBIFS_MAX_DATA_NODE_SZ;
395
396 return 0;
397}
398
399/*
400 * init_constants_sb - initialize UBIFS constants.
401 * @c: UBIFS file-system description object
402 *
403 * This is a helper function which initializes various UBIFS constants after
404 * the superblock has been read. It also checks various UBIFS parameters and
405 * makes sure they are all right. Returns zero in case of success and a
406 * negative error code in case of failure.
407 */
408static int init_constants_sb(struct ubifs_info *c)
409{
410 int tmp, err;
411 long long tmp64;
412
413 c->main_bytes = (long long)c->main_lebs * c->leb_size;
414 c->max_znode_sz = sizeof(struct ubifs_znode) +
415 c->fanout * sizeof(struct ubifs_zbranch);
416
417 tmp = ubifs_idx_node_sz(c, 1);
418 c->ranges[UBIFS_IDX_NODE].min_len = tmp;
419 c->min_idx_node_sz = ALIGN(tmp, 8);
420
421 tmp = ubifs_idx_node_sz(c, c->fanout);
422 c->ranges[UBIFS_IDX_NODE].max_len = tmp;
423 c->max_idx_node_sz = ALIGN(tmp, 8);
424
425 /* Make sure LEB size is large enough to fit full commit */
426 tmp = UBIFS_CS_NODE_SZ + UBIFS_REF_NODE_SZ * c->jhead_cnt;
427 tmp = ALIGN(tmp, c->min_io_size);
428 if (tmp > c->leb_size) {
429 dbg_err("too small LEB size %d, at least %d needed",
430 c->leb_size, tmp);
431 return -EINVAL;
432 }
433
434 /*
435 * Make sure that the log is large enough to fit reference nodes for
436 * all buds plus one reserved LEB.
437 */
438 tmp64 = c->max_bud_bytes + c->leb_size - 1;
439 c->max_bud_cnt = div_u64(tmp64, c->leb_size);
440 tmp = (c->ref_node_alsz * c->max_bud_cnt + c->leb_size - 1);
441 tmp /= c->leb_size;
442 tmp += 1;
443 if (c->log_lebs < tmp) {
444 dbg_err("too small log %d LEBs, required min. %d LEBs",
445 c->log_lebs, tmp);
446 return -EINVAL;
447 }
448
449 /*
450 * When budgeting we assume worst-case scenarios when the pages are not
451 * be compressed and direntries are of the maximum size.
452 *
453 * Note, data, which may be stored in inodes is budgeted separately, so
454 * it is not included into 'c->inode_budget'.
455 */
456 c->page_budget = UBIFS_MAX_DATA_NODE_SZ * UBIFS_BLOCKS_PER_PAGE;
457 c->inode_budget = UBIFS_INO_NODE_SZ;
458 c->dent_budget = UBIFS_MAX_DENT_NODE_SZ;
459
460 /*
461 * When the amount of flash space used by buds becomes
462 * 'c->max_bud_bytes', UBIFS just blocks all writers and starts commit.
463 * The writers are unblocked when the commit is finished. To avoid
464 * writers to be blocked UBIFS initiates background commit in advance,
465 * when number of bud bytes becomes above the limit defined below.
466 */
467 c->bg_bud_bytes = (c->max_bud_bytes * 13) >> 4;
468
469 /*
470 * Ensure minimum journal size. All the bytes in the journal heads are
471 * considered to be used, when calculating the current journal usage.
472 * Consequently, if the journal is too small, UBIFS will treat it as
473 * always full.
474 */
475 tmp64 = (long long)(c->jhead_cnt + 1) * c->leb_size + 1;
476 if (c->bg_bud_bytes < tmp64)
477 c->bg_bud_bytes = tmp64;
478 if (c->max_bud_bytes < tmp64 + c->leb_size)
479 c->max_bud_bytes = tmp64 + c->leb_size;
480
481 err = ubifs_calc_lpt_geom(c);
482 if (err)
483 return err;
484
485 return 0;
486}
487
488/*
489 * init_constants_master - initialize UBIFS constants.
490 * @c: UBIFS file-system description object
491 *
492 * This is a helper function which initializes various UBIFS constants after
493 * the master node has been read. It also checks various UBIFS parameters and
494 * makes sure they are all right.
495 */
496static void init_constants_master(struct ubifs_info *c)
497{
498 long long tmp64;
499
500 c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
501
502 /*
503 * Calculate total amount of FS blocks. This number is not used
504 * internally because it does not make much sense for UBIFS, but it is
505 * necessary to report something for the 'statfs()' call.
506 *
507 * Subtract the LEB reserved for GC, the LEB which is reserved for
508 * deletions, minimum LEBs for the index, and assume only one journal
509 * head is available.
510 */
511 tmp64 = c->main_lebs - 1 - 1 - MIN_INDEX_LEBS - c->jhead_cnt + 1;
512 tmp64 *= (long long)c->leb_size - c->leb_overhead;
513 tmp64 = ubifs_reported_space(c, tmp64);
514 c->block_cnt = tmp64 >> UBIFS_BLOCK_SHIFT;
515}
516
517/**
518 * free_orphans - free orphans.
519 * @c: UBIFS file-system description object
520 */
521static void free_orphans(struct ubifs_info *c)
522{
523 struct ubifs_orphan *orph;
524
525 while (c->orph_dnext) {
526 orph = c->orph_dnext;
527 c->orph_dnext = orph->dnext;
528 list_del(&orph->list);
529 kfree(orph);
530 }
531
532 while (!list_empty(&c->orph_list)) {
533 orph = list_entry(c->orph_list.next, struct ubifs_orphan, list);
534 list_del(&orph->list);
535 kfree(orph);
536 dbg_err("orphan list not empty at unmount");
537 }
538
539 vfree(c->orph_buf);
540 c->orph_buf = NULL;
541}
542
543/**
544 * check_volume_empty - check if the UBI volume is empty.
545 * @c: UBIFS file-system description object
546 *
547 * This function checks if the UBIFS volume is empty by looking if its LEBs are
548 * mapped or not. The result of checking is stored in the @c->empty variable.
549 * Returns zero in case of success and a negative error code in case of
550 * failure.
551 */
552static int check_volume_empty(struct ubifs_info *c)
553{
554 int lnum, err;
555
556 c->empty = 1;
557 for (lnum = 0; lnum < c->leb_cnt; lnum++) {
558 err = ubi_is_mapped(c->ubi, lnum);
559 if (unlikely(err < 0))
560 return err;
561 if (err == 1) {
562 c->empty = 0;
563 break;
564 }
565
566 cond_resched();
567 }
568
569 return 0;
570}
571
572/**
573 * mount_ubifs - mount UBIFS file-system.
574 * @c: UBIFS file-system description object
575 *
576 * This function mounts UBIFS file system. Returns zero in case of success and
577 * a negative error code in case of failure.
578 *
579 * Note, the function does not de-allocate resources it it fails half way
580 * through, and the caller has to do this instead.
581 */
582static int mount_ubifs(struct ubifs_info *c)
583{
584 struct super_block *sb = c->vfs_sb;
585 int err, mounted_read_only = (sb->s_flags & MS_RDONLY);
586 long long x;
587 size_t sz;
588
589 err = init_constants_early(c);
590 if (err)
591 return err;
592
593 err = ubifs_debugging_init(c);
594 if (err)
595 return err;
596
597 err = check_volume_empty(c);
598 if (err)
599 goto out_free;
600
601 if (c->empty && (mounted_read_only || c->ro_media)) {
602 /*
603 * This UBI volume is empty, and read-only, or the file system
604 * is mounted read-only - we cannot format it.
605 */
606 ubifs_err("can't format empty UBI volume: read-only %s",
607 c->ro_media ? "UBI volume" : "mount");
608 err = -EROFS;
609 goto out_free;
610 }
611
612 if (c->ro_media && !mounted_read_only) {
613 ubifs_err("cannot mount read-write - read-only media");
614 err = -EROFS;
615 goto out_free;
616 }
617
618 /*
619 * The requirement for the buffer is that it should fit indexing B-tree
620 * height amount of integers. We assume the height if the TNC tree will
621 * never exceed 64.
622 */
623 err = -ENOMEM;
624 c->bottom_up_buf = kmalloc(BOTTOM_UP_HEIGHT * sizeof(int), GFP_KERNEL);
625 if (!c->bottom_up_buf)
626 goto out_free;
627
628 c->sbuf = vmalloc(c->leb_size);
629 if (!c->sbuf)
630 goto out_free;
631
632 /*
633 * We have to check all CRCs, even for data nodes, when we mount the FS
634 * (specifically, when we are replaying).
635 */
636 c->always_chk_crc = 1;
637
638 err = ubifs_read_superblock(c);
639 if (err)
640 goto out_free;
641
642 /*
643 * Make sure the compressor which is set as default in the superblock
644 * or overridden by mount options is actually compiled in.
645 */
646 if (!ubifs_compr_present(c->default_compr)) {
647 ubifs_err("'compressor \"%s\" is not compiled in",
648 ubifs_compr_name(c->default_compr));
649 goto out_free;
650 }
651
652 dbg_failure_mode_registration(c);
653
654 err = init_constants_sb(c);
655 if (err)
656 goto out_free;
657
658 sz = ALIGN(c->max_idx_node_sz, c->min_io_size);
659 sz = ALIGN(sz + c->max_idx_node_sz, c->min_io_size);
660 c->cbuf = kmalloc(sz, GFP_NOFS);
661 if (!c->cbuf) {
662 err = -ENOMEM;
663 goto out_free;
664 }
665
666 sprintf(c->bgt_name, BGT_NAME_PATTERN, c->vi.ubi_num, c->vi.vol_id);
667
668 err = ubifs_read_master(c);
669 if (err)
670 goto out_master;
671
672 init_constants_master(c);
673
674 if ((c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY)) != 0) {
675 ubifs_msg("recovery needed");
676 c->need_recovery = 1;
677 }
678
679 err = ubifs_lpt_init(c, 1, !mounted_read_only);
680 if (err)
681 goto out_lpt;
682
683 err = dbg_check_idx_size(c, c->old_idx_sz);
684 if (err)
685 goto out_lpt;
686
687 err = ubifs_replay_journal(c);
688 if (err)
689 goto out_journal;
690
691 err = ubifs_mount_orphans(c, c->need_recovery, mounted_read_only);
692 if (err)
693 goto out_orphans;
694
695 if (c->need_recovery) {
696 err = ubifs_recover_size(c);
697 if (err)
698 goto out_orphans;
699 }
700
701 spin_lock(&ubifs_infos_lock);
702 list_add_tail(&c->infos_list, &ubifs_infos);
703 spin_unlock(&ubifs_infos_lock);
704
705 if (c->need_recovery) {
706 if (mounted_read_only)
707 ubifs_msg("recovery deferred");
708 else {
709 c->need_recovery = 0;
710 ubifs_msg("recovery completed");
711 }
712 }
713
714 err = dbg_check_filesystem(c);
715 if (err)
716 goto out_infos;
717
718 c->always_chk_crc = 0;
719
720 ubifs_msg("mounted UBI device %d, volume %d, name \"%s\"",
721 c->vi.ubi_num, c->vi.vol_id, c->vi.name);
722 if (mounted_read_only)
723 ubifs_msg("mounted read-only");
724 x = (long long)c->main_lebs * c->leb_size;
725 ubifs_msg("file system size: %lld bytes (%lld KiB, %lld MiB, %d "
726 "LEBs)", x, x >> 10, x >> 20, c->main_lebs);
727 x = (long long)c->log_lebs * c->leb_size + c->max_bud_bytes;
728 ubifs_msg("journal size: %lld bytes (%lld KiB, %lld MiB, %d "
729 "LEBs)", x, x >> 10, x >> 20, c->log_lebs + c->max_bud_cnt);
730 ubifs_msg("media format: %d (latest is %d)",
731 c->fmt_version, UBIFS_FORMAT_VERSION);
732 ubifs_msg("default compressor: %s", ubifs_compr_name(c->default_compr));
733 ubifs_msg("reserved for root: %llu bytes (%llu KiB)",
734 c->report_rp_size, c->report_rp_size >> 10);
735
736 dbg_msg("compiled on: " __DATE__ " at " __TIME__);
737 dbg_msg("min. I/O unit size: %d bytes", c->min_io_size);
738 dbg_msg("LEB size: %d bytes (%d KiB)",
739 c->leb_size, c->leb_size >> 10);
740 dbg_msg("data journal heads: %d",
741 c->jhead_cnt - NONDATA_JHEADS_CNT);
742 dbg_msg("UUID: %02X%02X%02X%02X-%02X%02X"
743 "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
744 c->uuid[0], c->uuid[1], c->uuid[2], c->uuid[3],
745 c->uuid[4], c->uuid[5], c->uuid[6], c->uuid[7],
746 c->uuid[8], c->uuid[9], c->uuid[10], c->uuid[11],
747 c->uuid[12], c->uuid[13], c->uuid[14], c->uuid[15]);
748 dbg_msg("big_lpt %d", c->big_lpt);
749 dbg_msg("log LEBs: %d (%d - %d)",
750 c->log_lebs, UBIFS_LOG_LNUM, c->log_last);
751 dbg_msg("LPT area LEBs: %d (%d - %d)",
752 c->lpt_lebs, c->lpt_first, c->lpt_last);
753 dbg_msg("orphan area LEBs: %d (%d - %d)",
754 c->orph_lebs, c->orph_first, c->orph_last);
755 dbg_msg("main area LEBs: %d (%d - %d)",
756 c->main_lebs, c->main_first, c->leb_cnt - 1);
757 dbg_msg("index LEBs: %d", c->lst.idx_lebs);
758 dbg_msg("total index bytes: %lld (%lld KiB, %lld MiB)",
759 c->old_idx_sz, c->old_idx_sz >> 10, c->old_idx_sz >> 20);
760 dbg_msg("key hash type: %d", c->key_hash_type);
761 dbg_msg("tree fanout: %d", c->fanout);
762 dbg_msg("reserved GC LEB: %d", c->gc_lnum);
763 dbg_msg("first main LEB: %d", c->main_first);
764 dbg_msg("max. znode size %d", c->max_znode_sz);
765 dbg_msg("max. index node size %d", c->max_idx_node_sz);
766 dbg_msg("node sizes: data %zu, inode %zu, dentry %zu",
767 UBIFS_DATA_NODE_SZ, UBIFS_INO_NODE_SZ, UBIFS_DENT_NODE_SZ);
768 dbg_msg("node sizes: trun %zu, sb %zu, master %zu",
769 UBIFS_TRUN_NODE_SZ, UBIFS_SB_NODE_SZ, UBIFS_MST_NODE_SZ);
770 dbg_msg("node sizes: ref %zu, cmt. start %zu, orph %zu",
771 UBIFS_REF_NODE_SZ, UBIFS_CS_NODE_SZ, UBIFS_ORPH_NODE_SZ);
772 dbg_msg("max. node sizes: data %zu, inode %zu dentry %zu",
773 UBIFS_MAX_DATA_NODE_SZ, UBIFS_MAX_INO_NODE_SZ,
774 UBIFS_MAX_DENT_NODE_SZ);
775 dbg_msg("dead watermark: %d", c->dead_wm);
776 dbg_msg("dark watermark: %d", c->dark_wm);
777 dbg_msg("LEB overhead: %d", c->leb_overhead);
778 x = (long long)c->main_lebs * c->dark_wm;
779 dbg_msg("max. dark space: %lld (%lld KiB, %lld MiB)",
780 x, x >> 10, x >> 20);
781 dbg_msg("maximum bud bytes: %lld (%lld KiB, %lld MiB)",
782 c->max_bud_bytes, c->max_bud_bytes >> 10,
783 c->max_bud_bytes >> 20);
784 dbg_msg("BG commit bud bytes: %lld (%lld KiB, %lld MiB)",
785 c->bg_bud_bytes, c->bg_bud_bytes >> 10,
786 c->bg_bud_bytes >> 20);
787 dbg_msg("current bud bytes %lld (%lld KiB, %lld MiB)",
788 c->bud_bytes, c->bud_bytes >> 10, c->bud_bytes >> 20);
789 dbg_msg("max. seq. number: %llu", c->max_sqnum);
790 dbg_msg("commit number: %llu", c->cmt_no);
791
792 return 0;
793
794out_infos:
795 spin_lock(&ubifs_infos_lock);
796 list_del(&c->infos_list);
797 spin_unlock(&ubifs_infos_lock);
798out_orphans:
799 free_orphans(c);
800out_journal:
801out_lpt:
802 ubifs_lpt_free(c, 0);
803out_master:
804 kfree(c->mst_node);
805 kfree(c->rcvrd_mst_node);
806 if (c->bgt)
807 kthread_stop(c->bgt);
808 kfree(c->cbuf);
809out_free:
810 vfree(c->ileb_buf);
811 vfree(c->sbuf);
812 kfree(c->bottom_up_buf);
813 ubifs_debugging_exit(c);
814 return err;
815}
816
817/**
818 * ubifs_umount - un-mount UBIFS file-system.
819 * @c: UBIFS file-system description object
820 *
821 * Note, this function is called to free allocated resourced when un-mounting,
822 * as well as free resources when an error occurred while we were half way
823 * through mounting (error path cleanup function). So it has to make sure the
824 * resource was actually allocated before freeing it.
825 */
826static void ubifs_umount(struct ubifs_info *c)
827{
828 dbg_gen("un-mounting UBI device %d, volume %d", c->vi.ubi_num,
829 c->vi.vol_id);
830
831 spin_lock(&ubifs_infos_lock);
832 list_del(&c->infos_list);
833 spin_unlock(&ubifs_infos_lock);
834
835 if (c->bgt)
836 kthread_stop(c->bgt);
837
838 free_orphans(c);
839 ubifs_lpt_free(c, 0);
840
841 kfree(c->cbuf);
842 kfree(c->rcvrd_mst_node);
843 kfree(c->mst_node);
844 vfree(c->ileb_buf);
845 vfree(c->sbuf);
846 kfree(c->bottom_up_buf);
847 ubifs_debugging_exit(c);
848
849 /* Finally free U-Boot's global copy of superblock */
850 free(ubifs_sb->s_fs_info);
851 free(ubifs_sb);
852}
853
854/**
855 * open_ubi - parse UBI device name string and open the UBI device.
856 * @name: UBI volume name
857 * @mode: UBI volume open mode
858 *
859 * There are several ways to specify UBI volumes when mounting UBIFS:
860 * o ubiX_Y - UBI device number X, volume Y;
861 * o ubiY - UBI device number 0, volume Y;
862 * o ubiX:NAME - mount UBI device X, volume with name NAME;
863 * o ubi:NAME - mount UBI device 0, volume with name NAME.
864 *
865 * Alternative '!' separator may be used instead of ':' (because some shells
866 * like busybox may interpret ':' as an NFS host name separator). This function
867 * returns ubi volume object in case of success and a negative error code in
868 * case of failure.
869 */
870static struct ubi_volume_desc *open_ubi(const char *name, int mode)
871{
872 int dev, vol;
873 char *endptr;
874
875 if (name[0] != 'u' || name[1] != 'b' || name[2] != 'i')
876 return ERR_PTR(-EINVAL);
877
878 /* ubi:NAME method */
879 if ((name[3] == ':' || name[3] == '!') && name[4] != '\0')
880 return ubi_open_volume_nm(0, name + 4, mode);
881
882 if (!isdigit(name[3]))
883 return ERR_PTR(-EINVAL);
884
885 dev = simple_strtoul(name + 3, &endptr, 0);
886
887 /* ubiY method */
888 if (*endptr == '\0')
889 return ubi_open_volume(0, dev, mode);
890
891 /* ubiX_Y method */
892 if (*endptr == '_' && isdigit(endptr[1])) {
893 vol = simple_strtoul(endptr + 1, &endptr, 0);
894 if (*endptr != '\0')
895 return ERR_PTR(-EINVAL);
896 return ubi_open_volume(dev, vol, mode);
897 }
898
899 /* ubiX:NAME method */
900 if ((*endptr == ':' || *endptr == '!') && endptr[1] != '\0')
901 return ubi_open_volume_nm(dev, ++endptr, mode);
902
903 return ERR_PTR(-EINVAL);
904}
905
906static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
907{
908 struct ubi_volume_desc *ubi = sb->s_fs_info;
909 struct ubifs_info *c;
910 struct inode *root;
911 int err;
912
913 c = kzalloc(sizeof(struct ubifs_info), GFP_KERNEL);
914 if (!c)
915 return -ENOMEM;
916
917 spin_lock_init(&c->cnt_lock);
918 spin_lock_init(&c->cs_lock);
919 spin_lock_init(&c->buds_lock);
920 spin_lock_init(&c->space_lock);
921 spin_lock_init(&c->orphan_lock);
922 init_rwsem(&c->commit_sem);
923 mutex_init(&c->lp_mutex);
924 mutex_init(&c->tnc_mutex);
925 mutex_init(&c->log_mutex);
926 mutex_init(&c->mst_mutex);
927 mutex_init(&c->umount_mutex);
928 init_waitqueue_head(&c->cmt_wq);
929 c->buds = RB_ROOT;
930 c->old_idx = RB_ROOT;
931 c->size_tree = RB_ROOT;
932 c->orph_tree = RB_ROOT;
933 INIT_LIST_HEAD(&c->infos_list);
934 INIT_LIST_HEAD(&c->idx_gc);
935 INIT_LIST_HEAD(&c->replay_list);
936 INIT_LIST_HEAD(&c->replay_buds);
937 INIT_LIST_HEAD(&c->uncat_list);
938 INIT_LIST_HEAD(&c->empty_list);
939 INIT_LIST_HEAD(&c->freeable_list);
940 INIT_LIST_HEAD(&c->frdi_idx_list);
941 INIT_LIST_HEAD(&c->unclean_leb_list);
942 INIT_LIST_HEAD(&c->old_buds);
943 INIT_LIST_HEAD(&c->orph_list);
944 INIT_LIST_HEAD(&c->orph_new);
945
946 c->highest_inum = UBIFS_FIRST_INO;
947 c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM;
948
949 ubi_get_volume_info(ubi, &c->vi);
950 ubi_get_device_info(c->vi.ubi_num, &c->di);
951
952 /* Re-open the UBI device in read-write mode */
953 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
954 if (IS_ERR(c->ubi)) {
955 err = PTR_ERR(c->ubi);
956 goto out_free;
957 }
958
959 c->vfs_sb = sb;
960
961 sb->s_fs_info = c;
962 sb->s_magic = UBIFS_SUPER_MAGIC;
963 sb->s_blocksize = UBIFS_BLOCK_SIZE;
964 sb->s_blocksize_bits = UBIFS_BLOCK_SHIFT;
965 sb->s_dev = c->vi.cdev;
966 sb->s_maxbytes = c->max_inode_sz = key_max_inode_size(c);
967 if (c->max_inode_sz > MAX_LFS_FILESIZE)
968 sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE;
969
970 mutex_lock(&c->umount_mutex);
971 err = mount_ubifs(c);
972 if (err) {
973 ubifs_assert(err < 0);
974 goto out_unlock;
975 }
976
977 /* Read the root inode */
978 root = ubifs_iget(sb, UBIFS_ROOT_INO);
979 if (IS_ERR(root)) {
980 err = PTR_ERR(root);
981 goto out_umount;
982 }
983
984 sb->s_root = NULL;
985
986 mutex_unlock(&c->umount_mutex);
987 return 0;
988
989out_umount:
990 ubifs_umount(c);
991out_unlock:
992 mutex_unlock(&c->umount_mutex);
993 ubi_close_volume(c->ubi);
994out_free:
995 kfree(c);
996 return err;
997}
998
999static int sb_test(struct super_block *sb, void *data)
1000{
1001 dev_t *dev = data;
1002
1003 return sb->s_dev == *dev;
1004}
1005
1006static int ubifs_get_sb(struct file_system_type *fs_type, int flags,
1007 const char *name, void *data, struct vfsmount *mnt)
1008{
1009 struct ubi_volume_desc *ubi;
1010 struct ubi_volume_info vi;
1011 struct super_block *sb;
1012 int err;
1013
1014 dbg_gen("name %s, flags %#x", name, flags);
1015
1016 /*
1017 * Get UBI device number and volume ID. Mount it read-only so far
1018 * because this might be a new mount point, and UBI allows only one
1019 * read-write user at a time.
1020 */
1021 ubi = open_ubi(name, UBI_READONLY);
1022 if (IS_ERR(ubi)) {
1023 ubifs_err("cannot open \"%s\", error %d",
1024 name, (int)PTR_ERR(ubi));
1025 return PTR_ERR(ubi);
1026 }
1027 ubi_get_volume_info(ubi, &vi);
1028
1029 dbg_gen("opened ubi%d_%d", vi.ubi_num, vi.vol_id);
1030
1031 sb = sget(fs_type, &sb_test, &sb_set, &vi.cdev);
1032 if (IS_ERR(sb)) {
1033 err = PTR_ERR(sb);
1034 goto out_close;
1035 }
1036
1037 if (sb->s_root) {
1038 /* A new mount point for already mounted UBIFS */
1039 dbg_gen("this ubi volume is already mounted");
1040 if ((flags ^ sb->s_flags) & MS_RDONLY) {
1041 err = -EBUSY;
1042 goto out_deact;
1043 }
1044 } else {
1045 sb->s_flags = flags;
1046 /*
1047 * Pass 'ubi' to 'fill_super()' in sb->s_fs_info where it is
1048 * replaced by 'c'.
1049 */
1050 sb->s_fs_info = ubi;
1051 err = ubifs_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
1052 if (err)
1053 goto out_deact;
1054 /* We do not support atime */
1055 sb->s_flags |= MS_ACTIVE | MS_NOATIME;
1056 }
1057
1058 /* 'fill_super()' opens ubi again so we must close it here */
1059 ubi_close_volume(ubi);
1060
1061 ubifs_sb = sb;
1062 return 0;
1063
1064out_deact:
1065 up_write(&sb->s_umount);
1066out_close:
1067 ubi_close_volume(ubi);
1068 return err;
1069}
1070
1071int __init ubifs_init(void)
1072{
1073 int err;
1074
1075 BUILD_BUG_ON(sizeof(struct ubifs_ch) != 24);
1076
1077 /* Make sure node sizes are 8-byte aligned */
1078 BUILD_BUG_ON(UBIFS_CH_SZ & 7);
1079 BUILD_BUG_ON(UBIFS_INO_NODE_SZ & 7);
1080 BUILD_BUG_ON(UBIFS_DENT_NODE_SZ & 7);
1081 BUILD_BUG_ON(UBIFS_XENT_NODE_SZ & 7);
1082 BUILD_BUG_ON(UBIFS_DATA_NODE_SZ & 7);
1083 BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ & 7);
1084 BUILD_BUG_ON(UBIFS_SB_NODE_SZ & 7);
1085 BUILD_BUG_ON(UBIFS_MST_NODE_SZ & 7);
1086 BUILD_BUG_ON(UBIFS_REF_NODE_SZ & 7);
1087 BUILD_BUG_ON(UBIFS_CS_NODE_SZ & 7);
1088 BUILD_BUG_ON(UBIFS_ORPH_NODE_SZ & 7);
1089
1090 BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ & 7);
1091 BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ & 7);
1092 BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ & 7);
1093 BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ & 7);
1094 BUILD_BUG_ON(UBIFS_MAX_NODE_SZ & 7);
1095 BUILD_BUG_ON(MIN_WRITE_SZ & 7);
1096
1097 /* Check min. node size */
1098 BUILD_BUG_ON(UBIFS_INO_NODE_SZ < MIN_WRITE_SZ);
1099 BUILD_BUG_ON(UBIFS_DENT_NODE_SZ < MIN_WRITE_SZ);
1100 BUILD_BUG_ON(UBIFS_XENT_NODE_SZ < MIN_WRITE_SZ);
1101 BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ < MIN_WRITE_SZ);
1102
1103 BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
1104 BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
1105 BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ > UBIFS_MAX_NODE_SZ);
1106 BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ > UBIFS_MAX_NODE_SZ);
1107
1108 /* Defined node sizes */
1109 BUILD_BUG_ON(UBIFS_SB_NODE_SZ != 4096);
1110 BUILD_BUG_ON(UBIFS_MST_NODE_SZ != 512);
1111 BUILD_BUG_ON(UBIFS_INO_NODE_SZ != 160);
1112 BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64);
1113
1114 /*
1115 * We use 2 bit wide bit-fields to store compression type, which should
1116 * be amended if more compressors are added. The bit-fields are:
1117 * @compr_type in 'struct ubifs_inode', @default_compr in
1118 * 'struct ubifs_info' and @compr_type in 'struct ubifs_mount_opts'.
1119 */
1120 BUILD_BUG_ON(UBIFS_COMPR_TYPES_CNT > 4);
1121
1122 /*
1123 * We require that PAGE_CACHE_SIZE is greater-than-or-equal-to
1124 * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2.
1125 */
1126 if (PAGE_CACHE_SIZE < UBIFS_BLOCK_SIZE) {
1127 ubifs_err("VFS page cache size is %u bytes, but UBIFS requires"
1128 " at least 4096 bytes",
1129 (unsigned int)PAGE_CACHE_SIZE);
1130 return -EINVAL;
1131 }
1132
1133 err = -ENOMEM;
1134
1135 err = ubifs_compressors_init();
1136 if (err)
1137 goto out_shrinker;
1138
1139 return 0;
1140
1141out_shrinker:
1142 return err;
1143}
1144
1145/*
1146 * ubifsmount...
1147 */
1148
1149static struct file_system_type ubifs_fs_type = {
1150 .name = "ubifs",
1151 .owner = THIS_MODULE,
1152 .get_sb = ubifs_get_sb,
1153};
1154
1155int ubifs_mount(char *vol_name)
1156{
1157 int flags;
1158 char name[80] = "ubi:";
1159 void *data;
1160 struct vfsmount *mnt;
1161 int ret;
1162 struct ubifs_info *c;
1163
1164 /*
1165 * First unmount if allready mounted
1166 */
1167 if (ubifs_sb)
1168 ubifs_umount(ubifs_sb->s_fs_info);
1169
1170 INIT_LIST_HEAD(&ubifs_infos);
1171
1172 /*
1173 * Mount in read-only mode
1174 */
1175 flags = MS_RDONLY;
1176 strcat(name, vol_name);
1177 data = NULL;
1178 mnt = NULL;
1179 ret = ubifs_get_sb(&ubifs_fs_type, flags, name, data, mnt);
1180 if (ret) {
1181 printf("Error reading superblock on volume '%s'!\n", name);
1182 return -1;
1183 }
1184
1185 c = ubifs_sb->s_fs_info;
1186 ubi_close_volume(c->ubi);
1187
1188 return 0;
1189}