blob: 748ab6792d0d2055abb82e490121cd9540b1043d [file] [log] [blame]
Stefan Roese9eefe2a2009-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);
Artem Bityutskiyfebd7e42009-03-27 10:21:14 +0100730 ubifs_msg("media format: w%d/r%d (latest is w%d/r%d)",
731 c->fmt_version, c->ro_compat_version,
732 UBIFS_FORMAT_VERSION, UBIFS_RO_COMPAT_VERSION);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100733 ubifs_msg("default compressor: %s", ubifs_compr_name(c->default_compr));
734 ubifs_msg("reserved for root: %llu bytes (%llu KiB)",
735 c->report_rp_size, c->report_rp_size >> 10);
736
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100737 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",
Wolfgang Denk93e14592013-10-04 17:43:24 +0200773 UBIFS_MAX_DATA_NODE_SZ, UBIFS_MAX_INO_NODE_SZ,
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100774 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 */
Stefan Roesecb9c09d2010-10-28 14:09:22 +0200826void ubifs_umount(struct ubifs_info *c)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100827{
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 */
Lars Poeschel349a8d52011-10-12 11:31:19 +0200850 if (ubifs_sb != NULL) {
851 free(ubifs_sb->s_fs_info);
852 free(ubifs_sb);
853 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100854}
855
856/**
857 * open_ubi - parse UBI device name string and open the UBI device.
858 * @name: UBI volume name
859 * @mode: UBI volume open mode
860 *
861 * There are several ways to specify UBI volumes when mounting UBIFS:
862 * o ubiX_Y - UBI device number X, volume Y;
863 * o ubiY - UBI device number 0, volume Y;
864 * o ubiX:NAME - mount UBI device X, volume with name NAME;
865 * o ubi:NAME - mount UBI device 0, volume with name NAME.
866 *
867 * Alternative '!' separator may be used instead of ':' (because some shells
868 * like busybox may interpret ':' as an NFS host name separator). This function
869 * returns ubi volume object in case of success and a negative error code in
870 * case of failure.
871 */
872static struct ubi_volume_desc *open_ubi(const char *name, int mode)
873{
874 int dev, vol;
875 char *endptr;
876
877 if (name[0] != 'u' || name[1] != 'b' || name[2] != 'i')
878 return ERR_PTR(-EINVAL);
879
880 /* ubi:NAME method */
881 if ((name[3] == ':' || name[3] == '!') && name[4] != '\0')
882 return ubi_open_volume_nm(0, name + 4, mode);
883
884 if (!isdigit(name[3]))
885 return ERR_PTR(-EINVAL);
886
887 dev = simple_strtoul(name + 3, &endptr, 0);
888
889 /* ubiY method */
890 if (*endptr == '\0')
891 return ubi_open_volume(0, dev, mode);
892
893 /* ubiX_Y method */
894 if (*endptr == '_' && isdigit(endptr[1])) {
895 vol = simple_strtoul(endptr + 1, &endptr, 0);
896 if (*endptr != '\0')
897 return ERR_PTR(-EINVAL);
898 return ubi_open_volume(dev, vol, mode);
899 }
900
901 /* ubiX:NAME method */
902 if ((*endptr == ':' || *endptr == '!') && endptr[1] != '\0')
903 return ubi_open_volume_nm(dev, ++endptr, mode);
904
905 return ERR_PTR(-EINVAL);
906}
907
908static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
909{
910 struct ubi_volume_desc *ubi = sb->s_fs_info;
911 struct ubifs_info *c;
912 struct inode *root;
913 int err;
914
915 c = kzalloc(sizeof(struct ubifs_info), GFP_KERNEL);
916 if (!c)
917 return -ENOMEM;
918
919 spin_lock_init(&c->cnt_lock);
920 spin_lock_init(&c->cs_lock);
921 spin_lock_init(&c->buds_lock);
922 spin_lock_init(&c->space_lock);
923 spin_lock_init(&c->orphan_lock);
924 init_rwsem(&c->commit_sem);
925 mutex_init(&c->lp_mutex);
926 mutex_init(&c->tnc_mutex);
927 mutex_init(&c->log_mutex);
928 mutex_init(&c->mst_mutex);
929 mutex_init(&c->umount_mutex);
930 init_waitqueue_head(&c->cmt_wq);
931 c->buds = RB_ROOT;
932 c->old_idx = RB_ROOT;
933 c->size_tree = RB_ROOT;
934 c->orph_tree = RB_ROOT;
935 INIT_LIST_HEAD(&c->infos_list);
936 INIT_LIST_HEAD(&c->idx_gc);
937 INIT_LIST_HEAD(&c->replay_list);
938 INIT_LIST_HEAD(&c->replay_buds);
939 INIT_LIST_HEAD(&c->uncat_list);
940 INIT_LIST_HEAD(&c->empty_list);
941 INIT_LIST_HEAD(&c->freeable_list);
942 INIT_LIST_HEAD(&c->frdi_idx_list);
943 INIT_LIST_HEAD(&c->unclean_leb_list);
944 INIT_LIST_HEAD(&c->old_buds);
945 INIT_LIST_HEAD(&c->orph_list);
946 INIT_LIST_HEAD(&c->orph_new);
947
948 c->highest_inum = UBIFS_FIRST_INO;
949 c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM;
950
951 ubi_get_volume_info(ubi, &c->vi);
952 ubi_get_device_info(c->vi.ubi_num, &c->di);
953
954 /* Re-open the UBI device in read-write mode */
955 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
956 if (IS_ERR(c->ubi)) {
957 err = PTR_ERR(c->ubi);
958 goto out_free;
959 }
960
961 c->vfs_sb = sb;
962
963 sb->s_fs_info = c;
964 sb->s_magic = UBIFS_SUPER_MAGIC;
965 sb->s_blocksize = UBIFS_BLOCK_SIZE;
966 sb->s_blocksize_bits = UBIFS_BLOCK_SHIFT;
967 sb->s_dev = c->vi.cdev;
968 sb->s_maxbytes = c->max_inode_sz = key_max_inode_size(c);
969 if (c->max_inode_sz > MAX_LFS_FILESIZE)
970 sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE;
971
Artem Bityutskiyfebd7e42009-03-27 10:21:14 +0100972 if (c->rw_incompat) {
973 ubifs_err("the file-system is not R/W-compatible");
974 ubifs_msg("on-flash format version is w%d/r%d, but software "
975 "only supports up to version w%d/r%d", c->fmt_version,
976 c->ro_compat_version, UBIFS_FORMAT_VERSION,
977 UBIFS_RO_COMPAT_VERSION);
978 return -EROFS;
979 }
980
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100981 mutex_lock(&c->umount_mutex);
982 err = mount_ubifs(c);
983 if (err) {
984 ubifs_assert(err < 0);
985 goto out_unlock;
986 }
987
988 /* Read the root inode */
989 root = ubifs_iget(sb, UBIFS_ROOT_INO);
990 if (IS_ERR(root)) {
991 err = PTR_ERR(root);
992 goto out_umount;
993 }
994
995 sb->s_root = NULL;
996
997 mutex_unlock(&c->umount_mutex);
998 return 0;
999
1000out_umount:
1001 ubifs_umount(c);
1002out_unlock:
1003 mutex_unlock(&c->umount_mutex);
1004 ubi_close_volume(c->ubi);
1005out_free:
1006 kfree(c);
1007 return err;
1008}
1009
1010static int sb_test(struct super_block *sb, void *data)
1011{
1012 dev_t *dev = data;
1013
1014 return sb->s_dev == *dev;
1015}
1016
1017static int ubifs_get_sb(struct file_system_type *fs_type, int flags,
1018 const char *name, void *data, struct vfsmount *mnt)
1019{
1020 struct ubi_volume_desc *ubi;
1021 struct ubi_volume_info vi;
1022 struct super_block *sb;
1023 int err;
1024
1025 dbg_gen("name %s, flags %#x", name, flags);
1026
1027 /*
1028 * Get UBI device number and volume ID. Mount it read-only so far
1029 * because this might be a new mount point, and UBI allows only one
1030 * read-write user at a time.
1031 */
1032 ubi = open_ubi(name, UBI_READONLY);
1033 if (IS_ERR(ubi)) {
1034 ubifs_err("cannot open \"%s\", error %d",
1035 name, (int)PTR_ERR(ubi));
1036 return PTR_ERR(ubi);
1037 }
1038 ubi_get_volume_info(ubi, &vi);
1039
1040 dbg_gen("opened ubi%d_%d", vi.ubi_num, vi.vol_id);
1041
1042 sb = sget(fs_type, &sb_test, &sb_set, &vi.cdev);
1043 if (IS_ERR(sb)) {
1044 err = PTR_ERR(sb);
1045 goto out_close;
1046 }
1047
1048 if (sb->s_root) {
1049 /* A new mount point for already mounted UBIFS */
1050 dbg_gen("this ubi volume is already mounted");
1051 if ((flags ^ sb->s_flags) & MS_RDONLY) {
1052 err = -EBUSY;
1053 goto out_deact;
1054 }
1055 } else {
1056 sb->s_flags = flags;
1057 /*
1058 * Pass 'ubi' to 'fill_super()' in sb->s_fs_info where it is
1059 * replaced by 'c'.
1060 */
1061 sb->s_fs_info = ubi;
1062 err = ubifs_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
1063 if (err)
1064 goto out_deact;
1065 /* We do not support atime */
1066 sb->s_flags |= MS_ACTIVE | MS_NOATIME;
1067 }
1068
1069 /* 'fill_super()' opens ubi again so we must close it here */
1070 ubi_close_volume(ubi);
1071
1072 ubifs_sb = sb;
1073 return 0;
1074
1075out_deact:
1076 up_write(&sb->s_umount);
1077out_close:
1078 ubi_close_volume(ubi);
1079 return err;
1080}
1081
1082int __init ubifs_init(void)
1083{
1084 int err;
1085
1086 BUILD_BUG_ON(sizeof(struct ubifs_ch) != 24);
1087
1088 /* Make sure node sizes are 8-byte aligned */
1089 BUILD_BUG_ON(UBIFS_CH_SZ & 7);
1090 BUILD_BUG_ON(UBIFS_INO_NODE_SZ & 7);
1091 BUILD_BUG_ON(UBIFS_DENT_NODE_SZ & 7);
1092 BUILD_BUG_ON(UBIFS_XENT_NODE_SZ & 7);
1093 BUILD_BUG_ON(UBIFS_DATA_NODE_SZ & 7);
1094 BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ & 7);
1095 BUILD_BUG_ON(UBIFS_SB_NODE_SZ & 7);
1096 BUILD_BUG_ON(UBIFS_MST_NODE_SZ & 7);
1097 BUILD_BUG_ON(UBIFS_REF_NODE_SZ & 7);
1098 BUILD_BUG_ON(UBIFS_CS_NODE_SZ & 7);
1099 BUILD_BUG_ON(UBIFS_ORPH_NODE_SZ & 7);
1100
1101 BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ & 7);
1102 BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ & 7);
1103 BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ & 7);
1104 BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ & 7);
1105 BUILD_BUG_ON(UBIFS_MAX_NODE_SZ & 7);
1106 BUILD_BUG_ON(MIN_WRITE_SZ & 7);
1107
1108 /* Check min. node size */
1109 BUILD_BUG_ON(UBIFS_INO_NODE_SZ < MIN_WRITE_SZ);
1110 BUILD_BUG_ON(UBIFS_DENT_NODE_SZ < MIN_WRITE_SZ);
1111 BUILD_BUG_ON(UBIFS_XENT_NODE_SZ < MIN_WRITE_SZ);
1112 BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ < MIN_WRITE_SZ);
1113
1114 BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
1115 BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
1116 BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ > UBIFS_MAX_NODE_SZ);
1117 BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ > UBIFS_MAX_NODE_SZ);
1118
1119 /* Defined node sizes */
1120 BUILD_BUG_ON(UBIFS_SB_NODE_SZ != 4096);
1121 BUILD_BUG_ON(UBIFS_MST_NODE_SZ != 512);
1122 BUILD_BUG_ON(UBIFS_INO_NODE_SZ != 160);
1123 BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64);
1124
1125 /*
1126 * We use 2 bit wide bit-fields to store compression type, which should
1127 * be amended if more compressors are added. The bit-fields are:
1128 * @compr_type in 'struct ubifs_inode', @default_compr in
1129 * 'struct ubifs_info' and @compr_type in 'struct ubifs_mount_opts'.
1130 */
1131 BUILD_BUG_ON(UBIFS_COMPR_TYPES_CNT > 4);
1132
1133 /*
1134 * We require that PAGE_CACHE_SIZE is greater-than-or-equal-to
1135 * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2.
1136 */
1137 if (PAGE_CACHE_SIZE < UBIFS_BLOCK_SIZE) {
1138 ubifs_err("VFS page cache size is %u bytes, but UBIFS requires"
1139 " at least 4096 bytes",
1140 (unsigned int)PAGE_CACHE_SIZE);
1141 return -EINVAL;
1142 }
1143
1144 err = -ENOMEM;
1145
1146 err = ubifs_compressors_init();
1147 if (err)
1148 goto out_shrinker;
1149
1150 return 0;
1151
1152out_shrinker:
1153 return err;
1154}
1155
1156/*
1157 * ubifsmount...
1158 */
1159
1160static struct file_system_type ubifs_fs_type = {
1161 .name = "ubifs",
1162 .owner = THIS_MODULE,
1163 .get_sb = ubifs_get_sb,
1164};
1165
Joe Hershberger949a7712012-11-01 16:54:18 +00001166int ubifs_mount(char *name)
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001167{
1168 int flags;
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001169 void *data;
1170 struct vfsmount *mnt;
1171 int ret;
1172 struct ubifs_info *c;
1173
1174 /*
1175 * First unmount if allready mounted
1176 */
1177 if (ubifs_sb)
1178 ubifs_umount(ubifs_sb->s_fs_info);
1179
1180 INIT_LIST_HEAD(&ubifs_infos);
Stefano Babic68651682010-04-21 09:47:19 +02001181 INIT_LIST_HEAD(&ubifs_fs_type.fs_supers);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001182
1183 /*
1184 * Mount in read-only mode
1185 */
1186 flags = MS_RDONLY;
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001187 data = NULL;
1188 mnt = NULL;
1189 ret = ubifs_get_sb(&ubifs_fs_type, flags, name, data, mnt);
1190 if (ret) {
Bernhard Wallef75325e2012-04-02 01:58:34 +00001191 ubifs_err("Error reading superblock on volume '%s' errno=%d!\n", name, ret);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001192 return -1;
1193 }
1194
1195 c = ubifs_sb->s_fs_info;
1196 ubi_close_volume(c->ubi);
1197
1198 return 0;
1199}