2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
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.
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
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
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
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.
30 #include <linux/math64.h>
32 #define INODE_LOCKED_MAX 64
34 struct super_block
*ubifs_sb
;
35 static struct inode
*inodes_locked_down
[INODE_LOCKED_MAX
];
39 /* List of all UBIFS file-system instances */
40 struct list_head ubifs_infos
;
42 /* linux/fs/super.c */
44 static int sb_set(struct super_block
*sb
, void *data
)
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
59 struct super_block
*sget(struct file_system_type
*type
,
60 int (*test
)(struct super_block
*,void *),
61 int (*set
)(struct super_block
*,void *),
64 struct super_block
*s
= NULL
;
67 s
= kzalloc(sizeof(struct super_block
), GFP_USER
);
73 INIT_LIST_HEAD(&s
->s_instances
);
74 INIT_LIST_HEAD(&s
->s_inodes
);
75 s
->s_time_gran
= 1000000000;
82 strncpy(s
->s_id
, type
->name
, sizeof(s
->s_id
));
83 list_add(&s
->s_instances
, &type
->fs_supers
);
88 * validate_inode - validate inode.
89 * @c: UBIFS file-system description object
90 * @inode: the inode to validate
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.
97 static int validate_inode(struct ubifs_info
*c
, const struct inode
*inode
)
100 const struct ubifs_inode
*ui
= ubifs_inode(inode
);
102 if (inode
->i_size
> c
->max_inode_sz
) {
103 ubifs_err("inode is too large (%lld)",
104 (long long)inode
->i_size
);
108 if (ui
->compr_type
< 0 || ui
->compr_type
>= UBIFS_COMPR_TYPES_CNT
) {
109 ubifs_err("unknown compression type %d", ui
->compr_type
);
113 if (ui
->data_len
< 0 || ui
->data_len
> UBIFS_MAX_INO_DATA
)
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
));
122 err
= dbg_check_dir_size(c
, inode
);
126 struct inode
*iget_locked(struct super_block
*sb
, unsigned long ino
)
130 inode
= (struct inode
*)malloc(sizeof(struct ubifs_inode
));
134 list_add(&inode
->i_sb_list
, &sb
->s_inodes
);
135 inode
->i_state
= I_LOCK
| I_NEW
;
141 int ubifs_iput(struct inode
*inode
)
143 list_del_init(&inode
->i_sb_list
);
150 * Lock (save) inode in inode array for readback after recovery
152 void iput(struct inode
*inode
)
160 for (i
= 0; i
< INODE_LOCKED_MAX
; i
++) {
161 if (inodes_locked_down
[i
] == NULL
)
165 if (i
>= INODE_LOCKED_MAX
) {
166 ubifs_err("Error, can't lock (save) more inodes while recovery!!!");
171 * Allocate and use new inode
173 ino
= (struct inode
*)malloc(sizeof(struct ubifs_inode
));
174 memcpy(ino
, inode
, sizeof(struct ubifs_inode
));
177 * Finally save inode in array
179 inodes_locked_down
[i
] = ino
;
182 struct inode
*ubifs_iget(struct super_block
*sb
, unsigned long inum
)
186 struct ubifs_ino_node
*ino
;
187 struct ubifs_info
*c
= sb
->s_fs_info
;
189 struct ubifs_inode
*ui
;
192 dbg_gen("inode %lu", inum
);
195 * U-Boot special handling of locked down inodes via recovery
196 * e.g. ubifs_recover_size()
198 for (i
= 0; i
< INODE_LOCKED_MAX
; i
++) {
200 * Exit on last entry (NULL), inode not found in list
202 if (inodes_locked_down
[i
] == NULL
)
205 if (inodes_locked_down
[i
]->i_ino
== inum
) {
207 * We found the locked down inode in our array,
208 * so just return this pointer instead of creating
211 return inodes_locked_down
[i
];
215 inode
= iget_locked(sb
, inum
);
217 return ERR_PTR(-ENOMEM
);
218 if (!(inode
->i_state
& I_NEW
))
220 ui
= ubifs_inode(inode
);
222 ino
= kmalloc(UBIFS_MAX_INO_NODE_SZ
, GFP_NOFS
);
228 ino_key_init(c
, &key
, inode
->i_ino
);
230 err
= ubifs_tnc_lookup(c
, &key
, ino
);
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
);
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
;
253 err
= validate_inode(c
, inode
);
257 if ((inode
->i_mode
& S_IFMT
) == S_IFLNK
) {
258 if (ui
->data_len
<= 0 || ui
->data_len
> UBIFS_MAX_INO_DATA
) {
262 ui
->data
= kmalloc(ui
->data_len
+ 1, GFP_NOFS
);
267 memcpy(ui
->data
, ino
->data
, ui
->data_len
);
268 ((char *)ui
->data
)[ui
->data_len
] = '\0';
272 inode
->i_state
&= ~(I_LOCK
| I_NEW
);
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
);
283 ubifs_err("failed to read inode %lu, error %d", inode
->i_ino
, err
);
288 * init_constants_early - initialize UBIFS constants.
289 * @c: UBIFS file-system description object
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
296 static int init_constants_early(struct ubifs_info
*c
)
298 if (c
->vi
.corrupted
) {
299 ubifs_warn("UBI volume is corrupted - read-only mode");
304 ubifs_msg("read-only UBI device");
308 if (c
->vi
.vol_type
== UBI_STATIC_VOLUME
) {
309 ubifs_msg("static UBI volume - read-only mode");
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;
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
);
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
);
331 if (!is_power_of_2(c
->min_io_size
)) {
332 ubifs_err("bad min. I/O size %d", c
->min_io_size
);
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
341 if (c
->min_io_size
< 8) {
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
);
350 * Initialize node length ranges which are mostly needed for node
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
;
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
;
372 * Minimum indexing node size is amended later when superblock is
373 * read and the key length is known.
375 c
->ranges
[UBIFS_IDX_NODE
].min_len
= UBIFS_IDX_NODE_SZ
+ UBIFS_BRANCH_SZ
;
377 * Maximum indexing node size is amended later when superblock is
378 * read and the fanout is known.
380 c
->ranges
[UBIFS_IDX_NODE
].max_len
= INT_MAX
;
383 * Initialize dead and dark LEB space watermarks. See gc.c for comments
384 * about these values.
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
);
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.
394 c
->leb_overhead
= c
->leb_size
% UBIFS_MAX_DATA_NODE_SZ
;
400 * init_constants_sb - initialize UBIFS constants.
401 * @c: UBIFS file-system description object
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.
408 static int init_constants_sb(struct ubifs_info
*c
)
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
);
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);
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);
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",
435 * Make sure that the log is large enough to fit reference nodes for
436 * all buds plus one reserved LEB.
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);
443 if (c
->log_lebs
< tmp
) {
444 dbg_err("too small log %d LEBs, required min. %d LEBs",
450 * When budgeting we assume worst-case scenarios when the pages are not
451 * be compressed and direntries are of the maximum size.
453 * Note, data, which may be stored in inodes is budgeted separately, so
454 * it is not included into 'c->inode_budget'.
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
;
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.
467 c
->bg_bud_bytes
= (c
->max_bud_bytes
* 13) >> 4;
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
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
;
481 err
= ubifs_calc_lpt_geom(c
);
489 * init_constants_master - initialize UBIFS constants.
490 * @c: UBIFS file-system description object
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.
496 static void init_constants_master(struct ubifs_info
*c
)
500 c
->min_idx_lebs
= ubifs_calc_min_idx_lebs(c
);
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.
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
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
;
518 * free_orphans - free orphans.
519 * @c: UBIFS file-system description object
521 static void free_orphans(struct ubifs_info
*c
)
523 struct ubifs_orphan
*orph
;
525 while (c
->orph_dnext
) {
526 orph
= c
->orph_dnext
;
527 c
->orph_dnext
= orph
->dnext
;
528 list_del(&orph
->list
);
532 while (!list_empty(&c
->orph_list
)) {
533 orph
= list_entry(c
->orph_list
.next
, struct ubifs_orphan
, list
);
534 list_del(&orph
->list
);
536 dbg_err("orphan list not empty at unmount");
544 * check_volume_empty - check if the UBI volume is empty.
545 * @c: UBIFS file-system description object
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
552 static int check_volume_empty(struct ubifs_info
*c
)
557 for (lnum
= 0; lnum
< c
->leb_cnt
; lnum
++) {
558 err
= ubi_is_mapped(c
->ubi
, lnum
);
559 if (unlikely(err
< 0))
573 * mount_ubifs - mount UBIFS file-system.
574 * @c: UBIFS file-system description object
576 * This function mounts UBIFS file system. Returns zero in case of success and
577 * a negative error code in case of failure.
579 * Note, the function does not de-allocate resources it it fails half way
580 * through, and the caller has to do this instead.
582 static int mount_ubifs(struct ubifs_info
*c
)
584 struct super_block
*sb
= c
->vfs_sb
;
585 int err
, mounted_read_only
= (sb
->s_flags
& MS_RDONLY
);
589 err
= init_constants_early(c
);
593 err
= ubifs_debugging_init(c
);
597 err
= check_volume_empty(c
);
601 if (c
->empty
&& (mounted_read_only
|| c
->ro_media
)) {
603 * This UBI volume is empty, and read-only, or the file system
604 * is mounted read-only - we cannot format it.
606 ubifs_err("can't format empty UBI volume: read-only %s",
607 c
->ro_media
? "UBI volume" : "mount");
612 if (c
->ro_media
&& !mounted_read_only
) {
613 ubifs_err("cannot mount read-write - read-only media");
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
624 c
->bottom_up_buf
= kmalloc(BOTTOM_UP_HEIGHT
* sizeof(int), GFP_KERNEL
);
625 if (!c
->bottom_up_buf
)
628 c
->sbuf
= vmalloc(c
->leb_size
);
633 * We have to check all CRCs, even for data nodes, when we mount the FS
634 * (specifically, when we are replaying).
636 c
->always_chk_crc
= 1;
638 err
= ubifs_read_superblock(c
);
643 * Make sure the compressor which is set as default in the superblock
644 * or overridden by mount options is actually compiled in.
646 if (!ubifs_compr_present(c
->default_compr
)) {
647 ubifs_err("'compressor \"%s\" is not compiled in",
648 ubifs_compr_name(c
->default_compr
));
652 dbg_failure_mode_registration(c
);
654 err
= init_constants_sb(c
);
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
);
666 sprintf(c
->bgt_name
, BGT_NAME_PATTERN
, c
->vi
.ubi_num
, c
->vi
.vol_id
);
668 err
= ubifs_read_master(c
);
672 init_constants_master(c
);
674 if ((c
->mst_node
->flags
& cpu_to_le32(UBIFS_MST_DIRTY
)) != 0) {
675 ubifs_msg("recovery needed");
676 c
->need_recovery
= 1;
679 err
= ubifs_lpt_init(c
, 1, !mounted_read_only
);
683 err
= dbg_check_idx_size(c
, c
->old_idx_sz
);
687 err
= ubifs_replay_journal(c
);
691 err
= ubifs_mount_orphans(c
, c
->need_recovery
, mounted_read_only
);
695 if (c
->need_recovery
) {
696 err
= ubifs_recover_size(c
);
701 spin_lock(&ubifs_infos_lock
);
702 list_add_tail(&c
->infos_list
, &ubifs_infos
);
703 spin_unlock(&ubifs_infos_lock
);
705 if (c
->need_recovery
) {
706 if (mounted_read_only
)
707 ubifs_msg("recovery deferred");
709 c
->need_recovery
= 0;
710 ubifs_msg("recovery completed");
714 err
= dbg_check_filesystem(c
);
718 c
->always_chk_crc
= 0;
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: 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
);
733 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);
737 dbg_msg("compiled on: " __DATE__
" at " __TIME__
);
738 dbg_msg("min. I/O unit size: %d bytes", c
->min_io_size
);
739 dbg_msg("LEB size: %d bytes (%d KiB)",
740 c
->leb_size
, c
->leb_size
>> 10);
741 dbg_msg("data journal heads: %d",
742 c
->jhead_cnt
- NONDATA_JHEADS_CNT
);
743 dbg_msg("UUID: %02X%02X%02X%02X-%02X%02X"
744 "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
745 c
->uuid
[0], c
->uuid
[1], c
->uuid
[2], c
->uuid
[3],
746 c
->uuid
[4], c
->uuid
[5], c
->uuid
[6], c
->uuid
[7],
747 c
->uuid
[8], c
->uuid
[9], c
->uuid
[10], c
->uuid
[11],
748 c
->uuid
[12], c
->uuid
[13], c
->uuid
[14], c
->uuid
[15]);
749 dbg_msg("big_lpt %d", c
->big_lpt
);
750 dbg_msg("log LEBs: %d (%d - %d)",
751 c
->log_lebs
, UBIFS_LOG_LNUM
, c
->log_last
);
752 dbg_msg("LPT area LEBs: %d (%d - %d)",
753 c
->lpt_lebs
, c
->lpt_first
, c
->lpt_last
);
754 dbg_msg("orphan area LEBs: %d (%d - %d)",
755 c
->orph_lebs
, c
->orph_first
, c
->orph_last
);
756 dbg_msg("main area LEBs: %d (%d - %d)",
757 c
->main_lebs
, c
->main_first
, c
->leb_cnt
- 1);
758 dbg_msg("index LEBs: %d", c
->lst
.idx_lebs
);
759 dbg_msg("total index bytes: %lld (%lld KiB, %lld MiB)",
760 c
->old_idx_sz
, c
->old_idx_sz
>> 10, c
->old_idx_sz
>> 20);
761 dbg_msg("key hash type: %d", c
->key_hash_type
);
762 dbg_msg("tree fanout: %d", c
->fanout
);
763 dbg_msg("reserved GC LEB: %d", c
->gc_lnum
);
764 dbg_msg("first main LEB: %d", c
->main_first
);
765 dbg_msg("max. znode size %d", c
->max_znode_sz
);
766 dbg_msg("max. index node size %d", c
->max_idx_node_sz
);
767 dbg_msg("node sizes: data %zu, inode %zu, dentry %zu",
768 UBIFS_DATA_NODE_SZ
, UBIFS_INO_NODE_SZ
, UBIFS_DENT_NODE_SZ
);
769 dbg_msg("node sizes: trun %zu, sb %zu, master %zu",
770 UBIFS_TRUN_NODE_SZ
, UBIFS_SB_NODE_SZ
, UBIFS_MST_NODE_SZ
);
771 dbg_msg("node sizes: ref %zu, cmt. start %zu, orph %zu",
772 UBIFS_REF_NODE_SZ
, UBIFS_CS_NODE_SZ
, UBIFS_ORPH_NODE_SZ
);
773 dbg_msg("max. node sizes: data %zu, inode %zu dentry %zu",
774 UBIFS_MAX_DATA_NODE_SZ
, UBIFS_MAX_INO_NODE_SZ
,
775 UBIFS_MAX_DENT_NODE_SZ
);
776 dbg_msg("dead watermark: %d", c
->dead_wm
);
777 dbg_msg("dark watermark: %d", c
->dark_wm
);
778 dbg_msg("LEB overhead: %d", c
->leb_overhead
);
779 x
= (long long)c
->main_lebs
* c
->dark_wm
;
780 dbg_msg("max. dark space: %lld (%lld KiB, %lld MiB)",
781 x
, x
>> 10, x
>> 20);
782 dbg_msg("maximum bud bytes: %lld (%lld KiB, %lld MiB)",
783 c
->max_bud_bytes
, c
->max_bud_bytes
>> 10,
784 c
->max_bud_bytes
>> 20);
785 dbg_msg("BG commit bud bytes: %lld (%lld KiB, %lld MiB)",
786 c
->bg_bud_bytes
, c
->bg_bud_bytes
>> 10,
787 c
->bg_bud_bytes
>> 20);
788 dbg_msg("current bud bytes %lld (%lld KiB, %lld MiB)",
789 c
->bud_bytes
, c
->bud_bytes
>> 10, c
->bud_bytes
>> 20);
790 dbg_msg("max. seq. number: %llu", c
->max_sqnum
);
791 dbg_msg("commit number: %llu", c
->cmt_no
);
796 spin_lock(&ubifs_infos_lock
);
797 list_del(&c
->infos_list
);
798 spin_unlock(&ubifs_infos_lock
);
803 ubifs_lpt_free(c
, 0);
806 kfree(c
->rcvrd_mst_node
);
808 kthread_stop(c
->bgt
);
813 kfree(c
->bottom_up_buf
);
814 ubifs_debugging_exit(c
);
819 * ubifs_umount - un-mount UBIFS file-system.
820 * @c: UBIFS file-system description object
822 * Note, this function is called to free allocated resourced when un-mounting,
823 * as well as free resources when an error occurred while we were half way
824 * through mounting (error path cleanup function). So it has to make sure the
825 * resource was actually allocated before freeing it.
827 void ubifs_umount(struct ubifs_info
*c
)
829 dbg_gen("un-mounting UBI device %d, volume %d", c
->vi
.ubi_num
,
832 spin_lock(&ubifs_infos_lock
);
833 list_del(&c
->infos_list
);
834 spin_unlock(&ubifs_infos_lock
);
837 kthread_stop(c
->bgt
);
840 ubifs_lpt_free(c
, 0);
843 kfree(c
->rcvrd_mst_node
);
847 kfree(c
->bottom_up_buf
);
848 ubifs_debugging_exit(c
);
850 /* Finally free U-Boot's global copy of superblock */
851 if (ubifs_sb
!= NULL
) {
852 free(ubifs_sb
->s_fs_info
);
858 * open_ubi - parse UBI device name string and open the UBI device.
859 * @name: UBI volume name
860 * @mode: UBI volume open mode
862 * There are several ways to specify UBI volumes when mounting UBIFS:
863 * o ubiX_Y - UBI device number X, volume Y;
864 * o ubiY - UBI device number 0, volume Y;
865 * o ubiX:NAME - mount UBI device X, volume with name NAME;
866 * o ubi:NAME - mount UBI device 0, volume with name NAME.
868 * Alternative '!' separator may be used instead of ':' (because some shells
869 * like busybox may interpret ':' as an NFS host name separator). This function
870 * returns ubi volume object in case of success and a negative error code in
873 static struct ubi_volume_desc
*open_ubi(const char *name
, int mode
)
878 if (name
[0] != 'u' || name
[1] != 'b' || name
[2] != 'i')
879 return ERR_PTR(-EINVAL
);
881 /* ubi:NAME method */
882 if ((name
[3] == ':' || name
[3] == '!') && name
[4] != '\0')
883 return ubi_open_volume_nm(0, name
+ 4, mode
);
885 if (!isdigit(name
[3]))
886 return ERR_PTR(-EINVAL
);
888 dev
= simple_strtoul(name
+ 3, &endptr
, 0);
892 return ubi_open_volume(0, dev
, mode
);
895 if (*endptr
== '_' && isdigit(endptr
[1])) {
896 vol
= simple_strtoul(endptr
+ 1, &endptr
, 0);
898 return ERR_PTR(-EINVAL
);
899 return ubi_open_volume(dev
, vol
, mode
);
902 /* ubiX:NAME method */
903 if ((*endptr
== ':' || *endptr
== '!') && endptr
[1] != '\0')
904 return ubi_open_volume_nm(dev
, ++endptr
, mode
);
906 return ERR_PTR(-EINVAL
);
909 static int ubifs_fill_super(struct super_block
*sb
, void *data
, int silent
)
911 struct ubi_volume_desc
*ubi
= sb
->s_fs_info
;
912 struct ubifs_info
*c
;
916 c
= kzalloc(sizeof(struct ubifs_info
), GFP_KERNEL
);
920 spin_lock_init(&c
->cnt_lock
);
921 spin_lock_init(&c
->cs_lock
);
922 spin_lock_init(&c
->buds_lock
);
923 spin_lock_init(&c
->space_lock
);
924 spin_lock_init(&c
->orphan_lock
);
925 init_rwsem(&c
->commit_sem
);
926 mutex_init(&c
->lp_mutex
);
927 mutex_init(&c
->tnc_mutex
);
928 mutex_init(&c
->log_mutex
);
929 mutex_init(&c
->mst_mutex
);
930 mutex_init(&c
->umount_mutex
);
931 init_waitqueue_head(&c
->cmt_wq
);
933 c
->old_idx
= RB_ROOT
;
934 c
->size_tree
= RB_ROOT
;
935 c
->orph_tree
= RB_ROOT
;
936 INIT_LIST_HEAD(&c
->infos_list
);
937 INIT_LIST_HEAD(&c
->idx_gc
);
938 INIT_LIST_HEAD(&c
->replay_list
);
939 INIT_LIST_HEAD(&c
->replay_buds
);
940 INIT_LIST_HEAD(&c
->uncat_list
);
941 INIT_LIST_HEAD(&c
->empty_list
);
942 INIT_LIST_HEAD(&c
->freeable_list
);
943 INIT_LIST_HEAD(&c
->frdi_idx_list
);
944 INIT_LIST_HEAD(&c
->unclean_leb_list
);
945 INIT_LIST_HEAD(&c
->old_buds
);
946 INIT_LIST_HEAD(&c
->orph_list
);
947 INIT_LIST_HEAD(&c
->orph_new
);
949 c
->highest_inum
= UBIFS_FIRST_INO
;
950 c
->lhead_lnum
= c
->ltail_lnum
= UBIFS_LOG_LNUM
;
952 ubi_get_volume_info(ubi
, &c
->vi
);
953 ubi_get_device_info(c
->vi
.ubi_num
, &c
->di
);
955 /* Re-open the UBI device in read-write mode */
956 c
->ubi
= ubi_open_volume(c
->vi
.ubi_num
, c
->vi
.vol_id
, UBI_READONLY
);
957 if (IS_ERR(c
->ubi
)) {
958 err
= PTR_ERR(c
->ubi
);
965 sb
->s_magic
= UBIFS_SUPER_MAGIC
;
966 sb
->s_blocksize
= UBIFS_BLOCK_SIZE
;
967 sb
->s_blocksize_bits
= UBIFS_BLOCK_SHIFT
;
968 sb
->s_dev
= c
->vi
.cdev
;
969 sb
->s_maxbytes
= c
->max_inode_sz
= key_max_inode_size(c
);
970 if (c
->max_inode_sz
> MAX_LFS_FILESIZE
)
971 sb
->s_maxbytes
= c
->max_inode_sz
= MAX_LFS_FILESIZE
;
973 if (c
->rw_incompat
) {
974 ubifs_err("the file-system is not R/W-compatible");
975 ubifs_msg("on-flash format version is w%d/r%d, but software "
976 "only supports up to version w%d/r%d", c
->fmt_version
,
977 c
->ro_compat_version
, UBIFS_FORMAT_VERSION
,
978 UBIFS_RO_COMPAT_VERSION
);
982 mutex_lock(&c
->umount_mutex
);
983 err
= mount_ubifs(c
);
985 ubifs_assert(err
< 0);
989 /* Read the root inode */
990 root
= ubifs_iget(sb
, UBIFS_ROOT_INO
);
998 mutex_unlock(&c
->umount_mutex
);
1004 mutex_unlock(&c
->umount_mutex
);
1005 ubi_close_volume(c
->ubi
);
1011 static int sb_test(struct super_block
*sb
, void *data
)
1015 return sb
->s_dev
== *dev
;
1018 static int ubifs_get_sb(struct file_system_type
*fs_type
, int flags
,
1019 const char *name
, void *data
, struct vfsmount
*mnt
)
1021 struct ubi_volume_desc
*ubi
;
1022 struct ubi_volume_info vi
;
1023 struct super_block
*sb
;
1026 dbg_gen("name %s, flags %#x", name
, flags
);
1029 * Get UBI device number and volume ID. Mount it read-only so far
1030 * because this might be a new mount point, and UBI allows only one
1031 * read-write user at a time.
1033 ubi
= open_ubi(name
, UBI_READONLY
);
1035 ubifs_err("cannot open \"%s\", error %d",
1036 name
, (int)PTR_ERR(ubi
));
1037 return PTR_ERR(ubi
);
1039 ubi_get_volume_info(ubi
, &vi
);
1041 dbg_gen("opened ubi%d_%d", vi
.ubi_num
, vi
.vol_id
);
1043 sb
= sget(fs_type
, &sb_test
, &sb_set
, &vi
.cdev
);
1050 /* A new mount point for already mounted UBIFS */
1051 dbg_gen("this ubi volume is already mounted");
1052 if ((flags
^ sb
->s_flags
) & MS_RDONLY
) {
1057 sb
->s_flags
= flags
;
1059 * Pass 'ubi' to 'fill_super()' in sb->s_fs_info where it is
1062 sb
->s_fs_info
= ubi
;
1063 err
= ubifs_fill_super(sb
, data
, flags
& MS_SILENT
? 1 : 0);
1066 /* We do not support atime */
1067 sb
->s_flags
|= MS_ACTIVE
| MS_NOATIME
;
1070 /* 'fill_super()' opens ubi again so we must close it here */
1071 ubi_close_volume(ubi
);
1077 up_write(&sb
->s_umount
);
1079 ubi_close_volume(ubi
);
1083 int __init
ubifs_init(void)
1087 BUILD_BUG_ON(sizeof(struct ubifs_ch
) != 24);
1089 /* Make sure node sizes are 8-byte aligned */
1090 BUILD_BUG_ON(UBIFS_CH_SZ
& 7);
1091 BUILD_BUG_ON(UBIFS_INO_NODE_SZ
& 7);
1092 BUILD_BUG_ON(UBIFS_DENT_NODE_SZ
& 7);
1093 BUILD_BUG_ON(UBIFS_XENT_NODE_SZ
& 7);
1094 BUILD_BUG_ON(UBIFS_DATA_NODE_SZ
& 7);
1095 BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ
& 7);
1096 BUILD_BUG_ON(UBIFS_SB_NODE_SZ
& 7);
1097 BUILD_BUG_ON(UBIFS_MST_NODE_SZ
& 7);
1098 BUILD_BUG_ON(UBIFS_REF_NODE_SZ
& 7);
1099 BUILD_BUG_ON(UBIFS_CS_NODE_SZ
& 7);
1100 BUILD_BUG_ON(UBIFS_ORPH_NODE_SZ
& 7);
1102 BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ
& 7);
1103 BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ
& 7);
1104 BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ
& 7);
1105 BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ
& 7);
1106 BUILD_BUG_ON(UBIFS_MAX_NODE_SZ
& 7);
1107 BUILD_BUG_ON(MIN_WRITE_SZ
& 7);
1109 /* Check min. node size */
1110 BUILD_BUG_ON(UBIFS_INO_NODE_SZ
< MIN_WRITE_SZ
);
1111 BUILD_BUG_ON(UBIFS_DENT_NODE_SZ
< MIN_WRITE_SZ
);
1112 BUILD_BUG_ON(UBIFS_XENT_NODE_SZ
< MIN_WRITE_SZ
);
1113 BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ
< MIN_WRITE_SZ
);
1115 BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ
> UBIFS_MAX_NODE_SZ
);
1116 BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ
> UBIFS_MAX_NODE_SZ
);
1117 BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ
> UBIFS_MAX_NODE_SZ
);
1118 BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ
> UBIFS_MAX_NODE_SZ
);
1120 /* Defined node sizes */
1121 BUILD_BUG_ON(UBIFS_SB_NODE_SZ
!= 4096);
1122 BUILD_BUG_ON(UBIFS_MST_NODE_SZ
!= 512);
1123 BUILD_BUG_ON(UBIFS_INO_NODE_SZ
!= 160);
1124 BUILD_BUG_ON(UBIFS_REF_NODE_SZ
!= 64);
1127 * We use 2 bit wide bit-fields to store compression type, which should
1128 * be amended if more compressors are added. The bit-fields are:
1129 * @compr_type in 'struct ubifs_inode', @default_compr in
1130 * 'struct ubifs_info' and @compr_type in 'struct ubifs_mount_opts'.
1132 BUILD_BUG_ON(UBIFS_COMPR_TYPES_CNT
> 4);
1135 * We require that PAGE_CACHE_SIZE is greater-than-or-equal-to
1136 * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2.
1138 if (PAGE_CACHE_SIZE
< UBIFS_BLOCK_SIZE
) {
1139 ubifs_err("VFS page cache size is %u bytes, but UBIFS requires"
1140 " at least 4096 bytes",
1141 (unsigned int)PAGE_CACHE_SIZE
);
1147 err
= ubifs_compressors_init();
1161 static struct file_system_type ubifs_fs_type
= {
1163 .owner
= THIS_MODULE
,
1164 .get_sb
= ubifs_get_sb
,
1167 int ubifs_mount(char *name
)
1171 struct vfsmount
*mnt
;
1173 struct ubifs_info
*c
;
1176 * First unmount if allready mounted
1179 ubifs_umount(ubifs_sb
->s_fs_info
);
1181 INIT_LIST_HEAD(&ubifs_infos
);
1182 INIT_LIST_HEAD(&ubifs_fs_type
.fs_supers
);
1185 * Mount in read-only mode
1190 ret
= ubifs_get_sb(&ubifs_fs_type
, flags
, name
, data
, mnt
);
1192 ubifs_err("Error reading superblock on volume '%s' errno=%d!\n", name
, ret
);
1196 c
= ubifs_sb
->s_fs_info
;
1197 ubi_close_volume(c
->ubi
);