blob: e276b54d5463d2e4338808934d5f57f33092d4f6 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
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 <linux/init.h>
30#include <linux/slab.h>
31#include <linux/module.h>
32#include <linux/ctype.h>
33#include <linux/kthread.h>
34#include <linux/parser.h>
35#include <linux/seq_file.h>
36#include <linux/mount.h>
37#include <linux/math64.h>
38#include <linux/writeback.h>
39#include "ubifs.h"
40
41/*
42 * Maximum amount of memory we may 'kmalloc()' without worrying that we are
43 * allocating too much.
44 */
45#define UBIFS_KMALLOC_OK (128*1024)
46
47/* Slab cache for UBIFS inodes */
48static struct kmem_cache *ubifs_inode_slab;
49
50/* UBIFS TNC shrinker description */
51static struct shrinker ubifs_shrinker_info = {
52 .scan_objects = ubifs_shrink_scan,
53 .count_objects = ubifs_shrink_count,
54 .seeks = DEFAULT_SEEKS,
55};
56
57/**
58 * validate_inode - validate inode.
59 * @c: UBIFS file-system description object
60 * @inode: the inode to validate
61 *
62 * This is a helper function for 'ubifs_iget()' which validates various fields
63 * of a newly built inode to make sure they contain sane values and prevent
64 * possible vulnerabilities. Returns zero if the inode is all right and
65 * a non-zero error code if not.
66 */
67static int validate_inode(struct ubifs_info *c, const struct inode *inode)
68{
69 int err;
70 const struct ubifs_inode *ui = ubifs_inode(inode);
71
72 if (inode->i_size > c->max_inode_sz) {
73 ubifs_err(c, "inode is too large (%lld)",
74 (long long)inode->i_size);
75 return 1;
76 }
77
78 if (ui->compr_type >= UBIFS_COMPR_TYPES_CNT) {
79 ubifs_err(c, "unknown compression type %d", ui->compr_type);
80 return 2;
81 }
82
83 if (ui->xattr_names + ui->xattr_cnt > XATTR_LIST_MAX)
84 return 3;
85
86 if (ui->data_len < 0 || ui->data_len > UBIFS_MAX_INO_DATA)
87 return 4;
88
89 if (ui->xattr && !S_ISREG(inode->i_mode))
90 return 5;
91
92 if (!ubifs_compr_present(c, ui->compr_type)) {
93 ubifs_warn(c, "inode %lu uses '%s' compression, but it was not compiled in",
94 inode->i_ino, ubifs_compr_name(c, ui->compr_type));
95 }
96
97 err = dbg_check_dir(c, inode);
98 return err;
99}
100
101struct inode *ubifs_iget(struct super_block *sb, unsigned long inum)
102{
103 int err;
104 union ubifs_key key;
105 struct ubifs_ino_node *ino;
106 struct ubifs_info *c = sb->s_fs_info;
107 struct inode *inode;
108 struct ubifs_inode *ui;
109
110 dbg_gen("inode %lu", inum);
111
112 inode = iget_locked(sb, inum);
113 if (!inode)
114 return ERR_PTR(-ENOMEM);
115 if (!(inode->i_state & I_NEW))
116 return inode;
117 ui = ubifs_inode(inode);
118
119 ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
120 if (!ino) {
121 err = -ENOMEM;
122 goto out;
123 }
124
125 ino_key_init(c, &key, inode->i_ino);
126
127 err = ubifs_tnc_lookup(c, &key, ino);
128 if (err)
129 goto out_ino;
130
131 inode->i_flags |= S_NOCMTIME;
132#ifndef CONFIG_UBIFS_ATIME_SUPPORT
133 inode->i_flags |= S_NOATIME;
134#endif
135 set_nlink(inode, le32_to_cpu(ino->nlink));
136 i_uid_write(inode, le32_to_cpu(ino->uid));
137 i_gid_write(inode, le32_to_cpu(ino->gid));
138 inode->i_atime.tv_sec = (int64_t)le64_to_cpu(ino->atime_sec);
139 inode->i_atime.tv_nsec = le32_to_cpu(ino->atime_nsec);
140 inode->i_mtime.tv_sec = (int64_t)le64_to_cpu(ino->mtime_sec);
141 inode->i_mtime.tv_nsec = le32_to_cpu(ino->mtime_nsec);
142 inode->i_ctime.tv_sec = (int64_t)le64_to_cpu(ino->ctime_sec);
143 inode->i_ctime.tv_nsec = le32_to_cpu(ino->ctime_nsec);
144 inode->i_mode = le32_to_cpu(ino->mode);
145 inode->i_size = le64_to_cpu(ino->size);
146
147 ui->data_len = le32_to_cpu(ino->data_len);
148 ui->flags = le32_to_cpu(ino->flags);
149 ui->compr_type = le16_to_cpu(ino->compr_type);
150 ui->creat_sqnum = le64_to_cpu(ino->creat_sqnum);
151 ui->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
152 ui->xattr_size = le32_to_cpu(ino->xattr_size);
153 ui->xattr_names = le32_to_cpu(ino->xattr_names);
154 ui->synced_i_size = ui->ui_size = inode->i_size;
155
156 ui->xattr = (ui->flags & UBIFS_XATTR_FL) ? 1 : 0;
157
158 err = validate_inode(c, inode);
159 if (err)
160 goto out_invalid;
161
162 switch (inode->i_mode & S_IFMT) {
163 case S_IFREG:
164 inode->i_mapping->a_ops = &ubifs_file_address_operations;
165 inode->i_op = &ubifs_file_inode_operations;
166 inode->i_fop = &ubifs_file_operations;
167 if (ui->xattr) {
168 ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
169 if (!ui->data) {
170 err = -ENOMEM;
171 goto out_ino;
172 }
173 memcpy(ui->data, ino->data, ui->data_len);
174 ((char *)ui->data)[ui->data_len] = '\0';
175 } else if (ui->data_len != 0) {
176 err = 10;
177 goto out_invalid;
178 }
179 break;
180 case S_IFDIR:
181 inode->i_op = &ubifs_dir_inode_operations;
182 inode->i_fop = &ubifs_dir_operations;
183 if (ui->data_len != 0) {
184 err = 11;
185 goto out_invalid;
186 }
187 break;
188 case S_IFLNK:
189 inode->i_op = &ubifs_symlink_inode_operations;
190 if (ui->data_len <= 0 || ui->data_len > UBIFS_MAX_INO_DATA) {
191 err = 12;
192 goto out_invalid;
193 }
194 ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
195 if (!ui->data) {
196 err = -ENOMEM;
197 goto out_ino;
198 }
199 memcpy(ui->data, ino->data, ui->data_len);
200 ((char *)ui->data)[ui->data_len] = '\0';
201 break;
202 case S_IFBLK:
203 case S_IFCHR:
204 {
205 dev_t rdev;
206 union ubifs_dev_desc *dev;
207
208 ui->data = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
209 if (!ui->data) {
210 err = -ENOMEM;
211 goto out_ino;
212 }
213
214 dev = (union ubifs_dev_desc *)ino->data;
215 if (ui->data_len == sizeof(dev->new))
216 rdev = new_decode_dev(le32_to_cpu(dev->new));
217 else if (ui->data_len == sizeof(dev->huge))
218 rdev = huge_decode_dev(le64_to_cpu(dev->huge));
219 else {
220 err = 13;
221 goto out_invalid;
222 }
223 memcpy(ui->data, ino->data, ui->data_len);
224 inode->i_op = &ubifs_file_inode_operations;
225 init_special_inode(inode, inode->i_mode, rdev);
226 break;
227 }
228 case S_IFSOCK:
229 case S_IFIFO:
230 inode->i_op = &ubifs_file_inode_operations;
231 init_special_inode(inode, inode->i_mode, 0);
232 if (ui->data_len != 0) {
233 err = 14;
234 goto out_invalid;
235 }
236 break;
237 default:
238 err = 15;
239 goto out_invalid;
240 }
241
242 kfree(ino);
243 ubifs_set_inode_flags(inode);
244 unlock_new_inode(inode);
245 return inode;
246
247out_invalid:
248 ubifs_err(c, "inode %lu validation failed, error %d", inode->i_ino, err);
249 ubifs_dump_node(c, ino);
250 ubifs_dump_inode(c, inode);
251 err = -EINVAL;
252out_ino:
253 kfree(ino);
254out:
255 ubifs_err(c, "failed to read inode %lu, error %d", inode->i_ino, err);
256 iget_failed(inode);
257 return ERR_PTR(err);
258}
259
260static struct inode *ubifs_alloc_inode(struct super_block *sb)
261{
262 struct ubifs_inode *ui;
263
264 ui = kmem_cache_alloc(ubifs_inode_slab, GFP_NOFS);
265 if (!ui)
266 return NULL;
267
268 memset((void *)ui + sizeof(struct inode), 0,
269 sizeof(struct ubifs_inode) - sizeof(struct inode));
270 mutex_init(&ui->ui_mutex);
271 spin_lock_init(&ui->ui_lock);
272 return &ui->vfs_inode;
273};
274
275static void ubifs_i_callback(struct rcu_head *head)
276{
277 struct inode *inode = container_of(head, struct inode, i_rcu);
278 struct ubifs_inode *ui = ubifs_inode(inode);
279
280 fscrypt_free_inode(inode);
281 kmem_cache_free(ubifs_inode_slab, ui);
282}
283
284static void ubifs_destroy_inode(struct inode *inode)
285{
286 struct ubifs_inode *ui = ubifs_inode(inode);
287
288 kfree(ui->data);
289 call_rcu(&inode->i_rcu, ubifs_i_callback);
290}
291
292/*
293 * Note, Linux write-back code calls this without 'i_mutex'.
294 */
295static int ubifs_write_inode(struct inode *inode, struct writeback_control *wbc)
296{
297 int err = 0;
298 struct ubifs_info *c = inode->i_sb->s_fs_info;
299 struct ubifs_inode *ui = ubifs_inode(inode);
300
301 ubifs_assert(c, !ui->xattr);
302 if (is_bad_inode(inode))
303 return 0;
304
305 mutex_lock(&ui->ui_mutex);
306 /*
307 * Due to races between write-back forced by budgeting
308 * (see 'sync_some_inodes()') and background write-back, the inode may
309 * have already been synchronized, do not do this again. This might
310 * also happen if it was synchronized in an VFS operation, e.g.
311 * 'ubifs_link()'.
312 */
313 if (!ui->dirty) {
314 mutex_unlock(&ui->ui_mutex);
315 return 0;
316 }
317
318 /*
319 * As an optimization, do not write orphan inodes to the media just
320 * because this is not needed.
321 */
322 dbg_gen("inode %lu, mode %#x, nlink %u",
323 inode->i_ino, (int)inode->i_mode, inode->i_nlink);
324 if (inode->i_nlink) {
325 err = ubifs_jnl_write_inode(c, inode);
326 if (err)
327 ubifs_err(c, "can't write inode %lu, error %d",
328 inode->i_ino, err);
329 else
330 err = dbg_check_inode_size(c, inode, ui->ui_size);
331 }
332
333 ui->dirty = 0;
334 mutex_unlock(&ui->ui_mutex);
335 ubifs_release_dirty_inode_budget(c, ui);
336 return err;
337}
338
339static int ubifs_drop_inode(struct inode *inode)
340{
341 int drop = generic_drop_inode(inode);
342
343 if (!drop)
344 drop = fscrypt_drop_inode(inode);
345
346 return drop;
347}
348
349static void ubifs_evict_inode(struct inode *inode)
350{
351 int err;
352 struct ubifs_info *c = inode->i_sb->s_fs_info;
353 struct ubifs_inode *ui = ubifs_inode(inode);
354
355 if (ui->xattr)
356 /*
357 * Extended attribute inode deletions are fully handled in
358 * 'ubifs_removexattr()'. These inodes are special and have
359 * limited usage, so there is nothing to do here.
360 */
361 goto out;
362
363 dbg_gen("inode %lu, mode %#x", inode->i_ino, (int)inode->i_mode);
364 ubifs_assert(c, !atomic_read(&inode->i_count));
365
366 truncate_inode_pages_final(&inode->i_data);
367
368 if (inode->i_nlink)
369 goto done;
370
371 if (is_bad_inode(inode))
372 goto out;
373
374 ui->ui_size = inode->i_size = 0;
375 err = ubifs_jnl_delete_inode(c, inode);
376 if (err)
377 /*
378 * Worst case we have a lost orphan inode wasting space, so a
379 * simple error message is OK here.
380 */
381 ubifs_err(c, "can't delete inode %lu, error %d",
382 inode->i_ino, err);
383
384out:
385 if (ui->dirty)
386 ubifs_release_dirty_inode_budget(c, ui);
387 else {
388 /* We've deleted something - clean the "no space" flags */
389 c->bi.nospace = c->bi.nospace_rp = 0;
390 smp_wmb();
391 }
392done:
393 clear_inode(inode);
394 fscrypt_put_encryption_info(inode);
395}
396
397static void ubifs_dirty_inode(struct inode *inode, int flags)
398{
399 struct ubifs_info *c = inode->i_sb->s_fs_info;
400 struct ubifs_inode *ui = ubifs_inode(inode);
401
402 ubifs_assert(c, mutex_is_locked(&ui->ui_mutex));
403 if (!ui->dirty) {
404 ui->dirty = 1;
405 dbg_gen("inode %lu", inode->i_ino);
406 }
407}
408
409static int ubifs_statfs(struct dentry *dentry, struct kstatfs *buf)
410{
411 struct ubifs_info *c = dentry->d_sb->s_fs_info;
412 unsigned long long free;
413 __le32 *uuid = (__le32 *)c->uuid;
414
415 free = ubifs_get_free_space(c);
416 dbg_gen("free space %lld bytes (%lld blocks)",
417 free, free >> UBIFS_BLOCK_SHIFT);
418
419 buf->f_type = UBIFS_SUPER_MAGIC;
420 buf->f_bsize = UBIFS_BLOCK_SIZE;
421 buf->f_blocks = c->block_cnt;
422 buf->f_bfree = free >> UBIFS_BLOCK_SHIFT;
423 if (free > c->report_rp_size)
424 buf->f_bavail = (free - c->report_rp_size) >> UBIFS_BLOCK_SHIFT;
425 else
426 buf->f_bavail = 0;
427 buf->f_files = 0;
428 buf->f_ffree = 0;
429 buf->f_namelen = UBIFS_MAX_NLEN;
430 buf->f_fsid.val[0] = le32_to_cpu(uuid[0]) ^ le32_to_cpu(uuid[2]);
431 buf->f_fsid.val[1] = le32_to_cpu(uuid[1]) ^ le32_to_cpu(uuid[3]);
432 ubifs_assert(c, buf->f_bfree <= c->block_cnt);
433 return 0;
434}
435
436static int ubifs_show_options(struct seq_file *s, struct dentry *root)
437{
438 struct ubifs_info *c = root->d_sb->s_fs_info;
439
440 if (c->mount_opts.unmount_mode == 2)
441 seq_puts(s, ",fast_unmount");
442 else if (c->mount_opts.unmount_mode == 1)
443 seq_puts(s, ",norm_unmount");
444
445 if (c->mount_opts.bulk_read == 2)
446 seq_puts(s, ",bulk_read");
447 else if (c->mount_opts.bulk_read == 1)
448 seq_puts(s, ",no_bulk_read");
449
450 if (c->mount_opts.chk_data_crc == 2)
451 seq_puts(s, ",chk_data_crc");
452 else if (c->mount_opts.chk_data_crc == 1)
453 seq_puts(s, ",no_chk_data_crc");
454
455 if (c->mount_opts.override_compr) {
456 seq_printf(s, ",compr=%s",
457 ubifs_compr_name(c, c->mount_opts.compr_type));
458 }
459
460 seq_printf(s, ",assert=%s", ubifs_assert_action_name(c));
461 seq_printf(s, ",ubi=%d,vol=%d", c->vi.ubi_num, c->vi.vol_id);
462
463 return 0;
464}
465
466static int ubifs_sync_fs(struct super_block *sb, int wait)
467{
468 int i, err;
469 struct ubifs_info *c = sb->s_fs_info;
470
471 /*
472 * Zero @wait is just an advisory thing to help the file system shove
473 * lots of data into the queues, and there will be the second
474 * '->sync_fs()' call, with non-zero @wait.
475 */
476 if (!wait)
477 return 0;
478
479 /*
480 * Synchronize write buffers, because 'ubifs_run_commit()' does not
481 * do this if it waits for an already running commit.
482 */
483 for (i = 0; i < c->jhead_cnt; i++) {
484 err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
485 if (err)
486 return err;
487 }
488
489 /*
490 * Strictly speaking, it is not necessary to commit the journal here,
491 * synchronizing write-buffers would be enough. But committing makes
492 * UBIFS free space predictions much more accurate, so we want to let
493 * the user be able to get more accurate results of 'statfs()' after
494 * they synchronize the file system.
495 */
496 err = ubifs_run_commit(c);
497 if (err)
498 return err;
499
500 return ubi_sync(c->vi.ubi_num);
501}
502
503/**
504 * init_constants_early - initialize UBIFS constants.
505 * @c: UBIFS file-system description object
506 *
507 * This function initialize UBIFS constants which do not need the superblock to
508 * be read. It also checks that the UBI volume satisfies basic UBIFS
509 * requirements. Returns zero in case of success and a negative error code in
510 * case of failure.
511 */
512static int init_constants_early(struct ubifs_info *c)
513{
514 if (c->vi.corrupted) {
515 ubifs_warn(c, "UBI volume is corrupted - read-only mode");
516 c->ro_media = 1;
517 }
518
519 if (c->di.ro_mode) {
520 ubifs_msg(c, "read-only UBI device");
521 c->ro_media = 1;
522 }
523
524 if (c->vi.vol_type == UBI_STATIC_VOLUME) {
525 ubifs_msg(c, "static UBI volume - read-only mode");
526 c->ro_media = 1;
527 }
528
529 c->leb_cnt = c->vi.size;
530 c->leb_size = c->vi.usable_leb_size;
531 c->leb_start = c->di.leb_start;
532 c->half_leb_size = c->leb_size / 2;
533 c->min_io_size = c->di.min_io_size;
534 c->min_io_shift = fls(c->min_io_size) - 1;
535 c->max_write_size = c->di.max_write_size;
536 c->max_write_shift = fls(c->max_write_size) - 1;
537
538 if (c->leb_size < UBIFS_MIN_LEB_SZ) {
539 ubifs_errc(c, "too small LEBs (%d bytes), min. is %d bytes",
540 c->leb_size, UBIFS_MIN_LEB_SZ);
541 return -EINVAL;
542 }
543
544 if (c->leb_cnt < UBIFS_MIN_LEB_CNT) {
545 ubifs_errc(c, "too few LEBs (%d), min. is %d",
546 c->leb_cnt, UBIFS_MIN_LEB_CNT);
547 return -EINVAL;
548 }
549
550 if (!is_power_of_2(c->min_io_size)) {
551 ubifs_errc(c, "bad min. I/O size %d", c->min_io_size);
552 return -EINVAL;
553 }
554
555 /*
556 * Maximum write size has to be greater or equivalent to min. I/O
557 * size, and be multiple of min. I/O size.
558 */
559 if (c->max_write_size < c->min_io_size ||
560 c->max_write_size % c->min_io_size ||
561 !is_power_of_2(c->max_write_size)) {
562 ubifs_errc(c, "bad write buffer size %d for %d min. I/O unit",
563 c->max_write_size, c->min_io_size);
564 return -EINVAL;
565 }
566
567 /*
568 * UBIFS aligns all node to 8-byte boundary, so to make function in
569 * io.c simpler, assume minimum I/O unit size to be 8 bytes if it is
570 * less than 8.
571 */
572 if (c->min_io_size < 8) {
573 c->min_io_size = 8;
574 c->min_io_shift = 3;
575 if (c->max_write_size < c->min_io_size) {
576 c->max_write_size = c->min_io_size;
577 c->max_write_shift = c->min_io_shift;
578 }
579 }
580
581 c->ref_node_alsz = ALIGN(UBIFS_REF_NODE_SZ, c->min_io_size);
582 c->mst_node_alsz = ALIGN(UBIFS_MST_NODE_SZ, c->min_io_size);
583
584 /*
585 * Initialize node length ranges which are mostly needed for node
586 * length validation.
587 */
588 c->ranges[UBIFS_PAD_NODE].len = UBIFS_PAD_NODE_SZ;
589 c->ranges[UBIFS_SB_NODE].len = UBIFS_SB_NODE_SZ;
590 c->ranges[UBIFS_MST_NODE].len = UBIFS_MST_NODE_SZ;
591 c->ranges[UBIFS_REF_NODE].len = UBIFS_REF_NODE_SZ;
592 c->ranges[UBIFS_TRUN_NODE].len = UBIFS_TRUN_NODE_SZ;
593 c->ranges[UBIFS_CS_NODE].len = UBIFS_CS_NODE_SZ;
594
595 c->ranges[UBIFS_INO_NODE].min_len = UBIFS_INO_NODE_SZ;
596 c->ranges[UBIFS_INO_NODE].max_len = UBIFS_MAX_INO_NODE_SZ;
597 c->ranges[UBIFS_ORPH_NODE].min_len =
598 UBIFS_ORPH_NODE_SZ + sizeof(__le64);
599 c->ranges[UBIFS_ORPH_NODE].max_len = c->leb_size;
600 c->ranges[UBIFS_DENT_NODE].min_len = UBIFS_DENT_NODE_SZ;
601 c->ranges[UBIFS_DENT_NODE].max_len = UBIFS_MAX_DENT_NODE_SZ;
602 c->ranges[UBIFS_XENT_NODE].min_len = UBIFS_XENT_NODE_SZ;
603 c->ranges[UBIFS_XENT_NODE].max_len = UBIFS_MAX_XENT_NODE_SZ;
604 c->ranges[UBIFS_DATA_NODE].min_len = UBIFS_DATA_NODE_SZ;
605 c->ranges[UBIFS_DATA_NODE].max_len = UBIFS_MAX_DATA_NODE_SZ;
606 /*
607 * Minimum indexing node size is amended later when superblock is
608 * read and the key length is known.
609 */
610 c->ranges[UBIFS_IDX_NODE].min_len = UBIFS_IDX_NODE_SZ + UBIFS_BRANCH_SZ;
611 /*
612 * Maximum indexing node size is amended later when superblock is
613 * read and the fanout is known.
614 */
615 c->ranges[UBIFS_IDX_NODE].max_len = INT_MAX;
616
617 /*
618 * Initialize dead and dark LEB space watermarks. See gc.c for comments
619 * about these values.
620 */
621 c->dead_wm = ALIGN(MIN_WRITE_SZ, c->min_io_size);
622 c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size);
623
624 /*
625 * Calculate how many bytes would be wasted at the end of LEB if it was
626 * fully filled with data nodes of maximum size. This is used in
627 * calculations when reporting free space.
628 */
629 c->leb_overhead = c->leb_size % UBIFS_MAX_DATA_NODE_SZ;
630
631 /* Buffer size for bulk-reads */
632 c->max_bu_buf_len = UBIFS_MAX_BULK_READ * UBIFS_MAX_DATA_NODE_SZ;
633 if (c->max_bu_buf_len > c->leb_size)
634 c->max_bu_buf_len = c->leb_size;
635 return 0;
636}
637
638/**
639 * bud_wbuf_callback - bud LEB write-buffer synchronization call-back.
640 * @c: UBIFS file-system description object
641 * @lnum: LEB the write-buffer was synchronized to
642 * @free: how many free bytes left in this LEB
643 * @pad: how many bytes were padded
644 *
645 * This is a callback function which is called by the I/O unit when the
646 * write-buffer is synchronized. We need this to correctly maintain space
647 * accounting in bud logical eraseblocks. This function returns zero in case of
648 * success and a negative error code in case of failure.
649 *
650 * This function actually belongs to the journal, but we keep it here because
651 * we want to keep it static.
652 */
653static int bud_wbuf_callback(struct ubifs_info *c, int lnum, int free, int pad)
654{
655 return ubifs_update_one_lp(c, lnum, free, pad, 0, 0);
656}
657
658/*
659 * init_constants_sb - initialize UBIFS constants.
660 * @c: UBIFS file-system description object
661 *
662 * This is a helper function which initializes various UBIFS constants after
663 * the superblock has been read. It also checks various UBIFS parameters and
664 * makes sure they are all right. Returns zero in case of success and a
665 * negative error code in case of failure.
666 */
667static int init_constants_sb(struct ubifs_info *c)
668{
669 int tmp, err;
670 long long tmp64;
671
672 c->main_bytes = (long long)c->main_lebs * c->leb_size;
673 c->max_znode_sz = sizeof(struct ubifs_znode) +
674 c->fanout * sizeof(struct ubifs_zbranch);
675
676 tmp = ubifs_idx_node_sz(c, 1);
677 c->ranges[UBIFS_IDX_NODE].min_len = tmp;
678 c->min_idx_node_sz = ALIGN(tmp, 8);
679
680 tmp = ubifs_idx_node_sz(c, c->fanout);
681 c->ranges[UBIFS_IDX_NODE].max_len = tmp;
682 c->max_idx_node_sz = ALIGN(tmp, 8);
683
684 /* Make sure LEB size is large enough to fit full commit */
685 tmp = UBIFS_CS_NODE_SZ + UBIFS_REF_NODE_SZ * c->jhead_cnt;
686 tmp = ALIGN(tmp, c->min_io_size);
687 if (tmp > c->leb_size) {
688 ubifs_err(c, "too small LEB size %d, at least %d needed",
689 c->leb_size, tmp);
690 return -EINVAL;
691 }
692
693 /*
694 * Make sure that the log is large enough to fit reference nodes for
695 * all buds plus one reserved LEB.
696 */
697 tmp64 = c->max_bud_bytes + c->leb_size - 1;
698 c->max_bud_cnt = div_u64(tmp64, c->leb_size);
699 tmp = (c->ref_node_alsz * c->max_bud_cnt + c->leb_size - 1);
700 tmp /= c->leb_size;
701 tmp += 1;
702 if (c->log_lebs < tmp) {
703 ubifs_err(c, "too small log %d LEBs, required min. %d LEBs",
704 c->log_lebs, tmp);
705 return -EINVAL;
706 }
707
708 /*
709 * When budgeting we assume worst-case scenarios when the pages are not
710 * be compressed and direntries are of the maximum size.
711 *
712 * Note, data, which may be stored in inodes is budgeted separately, so
713 * it is not included into 'c->bi.inode_budget'.
714 */
715 c->bi.page_budget = UBIFS_MAX_DATA_NODE_SZ * UBIFS_BLOCKS_PER_PAGE;
716 c->bi.inode_budget = UBIFS_INO_NODE_SZ;
717 c->bi.dent_budget = UBIFS_MAX_DENT_NODE_SZ;
718
719 /*
720 * When the amount of flash space used by buds becomes
721 * 'c->max_bud_bytes', UBIFS just blocks all writers and starts commit.
722 * The writers are unblocked when the commit is finished. To avoid
723 * writers to be blocked UBIFS initiates background commit in advance,
724 * when number of bud bytes becomes above the limit defined below.
725 */
726 c->bg_bud_bytes = (c->max_bud_bytes * 13) >> 4;
727
728 /*
729 * Ensure minimum journal size. All the bytes in the journal heads are
730 * considered to be used, when calculating the current journal usage.
731 * Consequently, if the journal is too small, UBIFS will treat it as
732 * always full.
733 */
734 tmp64 = (long long)(c->jhead_cnt + 1) * c->leb_size + 1;
735 if (c->bg_bud_bytes < tmp64)
736 c->bg_bud_bytes = tmp64;
737 if (c->max_bud_bytes < tmp64 + c->leb_size)
738 c->max_bud_bytes = tmp64 + c->leb_size;
739
740 err = ubifs_calc_lpt_geom(c);
741 if (err)
742 return err;
743
744 /* Initialize effective LEB size used in budgeting calculations */
745 c->idx_leb_size = c->leb_size - c->max_idx_node_sz;
746 return 0;
747}
748
749/*
750 * init_constants_master - initialize UBIFS constants.
751 * @c: UBIFS file-system description object
752 *
753 * This is a helper function which initializes various UBIFS constants after
754 * the master node has been read. It also checks various UBIFS parameters and
755 * makes sure they are all right.
756 */
757static void init_constants_master(struct ubifs_info *c)
758{
759 long long tmp64;
760
761 c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
762 c->report_rp_size = ubifs_reported_space(c, c->rp_size);
763
764 /*
765 * Calculate total amount of FS blocks. This number is not used
766 * internally because it does not make much sense for UBIFS, but it is
767 * necessary to report something for the 'statfs()' call.
768 *
769 * Subtract the LEB reserved for GC, the LEB which is reserved for
770 * deletions, minimum LEBs for the index, and assume only one journal
771 * head is available.
772 */
773 tmp64 = c->main_lebs - 1 - 1 - MIN_INDEX_LEBS - c->jhead_cnt + 1;
774 tmp64 *= (long long)c->leb_size - c->leb_overhead;
775 tmp64 = ubifs_reported_space(c, tmp64);
776 c->block_cnt = tmp64 >> UBIFS_BLOCK_SHIFT;
777}
778
779/**
780 * take_gc_lnum - reserve GC LEB.
781 * @c: UBIFS file-system description object
782 *
783 * This function ensures that the LEB reserved for garbage collection is marked
784 * as "taken" in lprops. We also have to set free space to LEB size and dirty
785 * space to zero, because lprops may contain out-of-date information if the
786 * file-system was un-mounted before it has been committed. This function
787 * returns zero in case of success and a negative error code in case of
788 * failure.
789 */
790static int take_gc_lnum(struct ubifs_info *c)
791{
792 int err;
793
794 if (c->gc_lnum == -1) {
795 ubifs_err(c, "no LEB for GC");
796 return -EINVAL;
797 }
798
799 /* And we have to tell lprops that this LEB is taken */
800 err = ubifs_change_one_lp(c, c->gc_lnum, c->leb_size, 0,
801 LPROPS_TAKEN, 0, 0);
802 return err;
803}
804
805/**
806 * alloc_wbufs - allocate write-buffers.
807 * @c: UBIFS file-system description object
808 *
809 * This helper function allocates and initializes UBIFS write-buffers. Returns
810 * zero in case of success and %-ENOMEM in case of failure.
811 */
812static int alloc_wbufs(struct ubifs_info *c)
813{
814 int i, err;
815
816 c->jheads = kcalloc(c->jhead_cnt, sizeof(struct ubifs_jhead),
817 GFP_KERNEL);
818 if (!c->jheads)
819 return -ENOMEM;
820
821 /* Initialize journal heads */
822 for (i = 0; i < c->jhead_cnt; i++) {
823 INIT_LIST_HEAD(&c->jheads[i].buds_list);
824 err = ubifs_wbuf_init(c, &c->jheads[i].wbuf);
825 if (err)
826 return err;
827
828 c->jheads[i].wbuf.sync_callback = &bud_wbuf_callback;
829 c->jheads[i].wbuf.jhead = i;
830 c->jheads[i].grouped = 1;
831 }
832
833 /*
834 * Garbage Collector head does not need to be synchronized by timer.
835 * Also GC head nodes are not grouped.
836 */
837 c->jheads[GCHD].wbuf.no_timer = 1;
838 c->jheads[GCHD].grouped = 0;
839
840 return 0;
841}
842
843/**
844 * free_wbufs - free write-buffers.
845 * @c: UBIFS file-system description object
846 */
847static void free_wbufs(struct ubifs_info *c)
848{
849 int i;
850
851 if (c->jheads) {
852 for (i = 0; i < c->jhead_cnt; i++) {
853 kfree(c->jheads[i].wbuf.buf);
854 kfree(c->jheads[i].wbuf.inodes);
855 }
856 kfree(c->jheads);
857 c->jheads = NULL;
858 }
859}
860
861/**
862 * free_orphans - free orphans.
863 * @c: UBIFS file-system description object
864 */
865static void free_orphans(struct ubifs_info *c)
866{
867 struct ubifs_orphan *orph;
868
869 while (c->orph_dnext) {
870 orph = c->orph_dnext;
871 c->orph_dnext = orph->dnext;
872 list_del(&orph->list);
873 kfree(orph);
874 }
875
876 while (!list_empty(&c->orph_list)) {
877 orph = list_entry(c->orph_list.next, struct ubifs_orphan, list);
878 list_del(&orph->list);
879 kfree(orph);
880 ubifs_err(c, "orphan list not empty at unmount");
881 }
882
883 vfree(c->orph_buf);
884 c->orph_buf = NULL;
885}
886
887/**
888 * free_buds - free per-bud objects.
889 * @c: UBIFS file-system description object
890 */
891static void free_buds(struct ubifs_info *c)
892{
893 struct ubifs_bud *bud, *n;
894
895 rbtree_postorder_for_each_entry_safe(bud, n, &c->buds, rb)
896 kfree(bud);
897}
898
899/**
900 * check_volume_empty - check if the UBI volume is empty.
901 * @c: UBIFS file-system description object
902 *
903 * This function checks if the UBIFS volume is empty by looking if its LEBs are
904 * mapped or not. The result of checking is stored in the @c->empty variable.
905 * Returns zero in case of success and a negative error code in case of
906 * failure.
907 */
908static int check_volume_empty(struct ubifs_info *c)
909{
910 int lnum, err;
911
912 c->empty = 1;
913 for (lnum = 0; lnum < c->leb_cnt; lnum++) {
914 err = ubifs_is_mapped(c, lnum);
915 if (unlikely(err < 0))
916 return err;
917 if (err == 1) {
918 c->empty = 0;
919 break;
920 }
921
922 cond_resched();
923 }
924
925 return 0;
926}
927
928/*
929 * UBIFS mount options.
930 *
931 * Opt_fast_unmount: do not run a journal commit before un-mounting
932 * Opt_norm_unmount: run a journal commit before un-mounting
933 * Opt_bulk_read: enable bulk-reads
934 * Opt_no_bulk_read: disable bulk-reads
935 * Opt_chk_data_crc: check CRCs when reading data nodes
936 * Opt_no_chk_data_crc: do not check CRCs when reading data nodes
937 * Opt_override_compr: override default compressor
938 * Opt_assert: set ubifs_assert() action
939 * Opt_err: just end of array marker
940 */
941enum {
942 Opt_fast_unmount,
943 Opt_norm_unmount,
944 Opt_bulk_read,
945 Opt_no_bulk_read,
946 Opt_chk_data_crc,
947 Opt_no_chk_data_crc,
948 Opt_override_compr,
949 Opt_assert,
950 Opt_ignore,
951 Opt_err,
952};
953
954static const match_table_t tokens = {
955 {Opt_fast_unmount, "fast_unmount"},
956 {Opt_norm_unmount, "norm_unmount"},
957 {Opt_bulk_read, "bulk_read"},
958 {Opt_no_bulk_read, "no_bulk_read"},
959 {Opt_chk_data_crc, "chk_data_crc"},
960 {Opt_no_chk_data_crc, "no_chk_data_crc"},
961 {Opt_override_compr, "compr=%s"},
962 {Opt_ignore, "ubi=%s"},
963 {Opt_ignore, "vol=%s"},
964 {Opt_assert, "assert=%s"},
965 {Opt_err, NULL},
966};
967
968/**
969 * parse_standard_option - parse a standard mount option.
970 * @option: the option to parse
971 *
972 * Normally, standard mount options like "sync" are passed to file-systems as
973 * flags. However, when a "rootflags=" kernel boot parameter is used, they may
974 * be present in the options string. This function tries to deal with this
975 * situation and parse standard options. Returns 0 if the option was not
976 * recognized, and the corresponding integer flag if it was.
977 *
978 * UBIFS is only interested in the "sync" option, so do not check for anything
979 * else.
980 */
981static int parse_standard_option(const char *option)
982{
983
984 pr_notice("UBIFS: parse %s\n", option);
985 if (!strcmp(option, "sync"))
986 return SB_SYNCHRONOUS;
987 return 0;
988}
989
990/**
991 * ubifs_parse_options - parse mount parameters.
992 * @c: UBIFS file-system description object
993 * @options: parameters to parse
994 * @is_remount: non-zero if this is FS re-mount
995 *
996 * This function parses UBIFS mount options and returns zero in case success
997 * and a negative error code in case of failure.
998 */
999static int ubifs_parse_options(struct ubifs_info *c, char *options,
1000 int is_remount)
1001{
1002 char *p;
1003 substring_t args[MAX_OPT_ARGS];
1004
1005 if (!options)
1006 return 0;
1007
1008 while ((p = strsep(&options, ","))) {
1009 int token;
1010
1011 if (!*p)
1012 continue;
1013
1014 token = match_token(p, tokens, args);
1015 switch (token) {
1016 /*
1017 * %Opt_fast_unmount and %Opt_norm_unmount options are ignored.
1018 * We accept them in order to be backward-compatible. But this
1019 * should be removed at some point.
1020 */
1021 case Opt_fast_unmount:
1022 c->mount_opts.unmount_mode = 2;
1023 break;
1024 case Opt_norm_unmount:
1025 c->mount_opts.unmount_mode = 1;
1026 break;
1027 case Opt_bulk_read:
1028 c->mount_opts.bulk_read = 2;
1029 c->bulk_read = 1;
1030 break;
1031 case Opt_no_bulk_read:
1032 c->mount_opts.bulk_read = 1;
1033 c->bulk_read = 0;
1034 break;
1035 case Opt_chk_data_crc:
1036 c->mount_opts.chk_data_crc = 2;
1037 c->no_chk_data_crc = 0;
1038 break;
1039 case Opt_no_chk_data_crc:
1040 c->mount_opts.chk_data_crc = 1;
1041 c->no_chk_data_crc = 1;
1042 break;
1043 case Opt_override_compr:
1044 {
1045 char *name = match_strdup(&args[0]);
1046
1047 if (!name)
1048 return -ENOMEM;
1049 if (!strcmp(name, "none"))
1050 c->mount_opts.compr_type = UBIFS_COMPR_NONE;
1051 else if (!strcmp(name, "lzo"))
1052 c->mount_opts.compr_type = UBIFS_COMPR_LZO;
1053 else if (!strcmp(name, "zlib"))
1054 c->mount_opts.compr_type = UBIFS_COMPR_ZLIB;
1055 else {
1056 ubifs_err(c, "unknown compressor \"%s\"", name); //FIXME: is c ready?
1057 kfree(name);
1058 return -EINVAL;
1059 }
1060 kfree(name);
1061 c->mount_opts.override_compr = 1;
1062 c->default_compr = c->mount_opts.compr_type;
1063 break;
1064 }
1065 case Opt_assert:
1066 {
1067 char *act = match_strdup(&args[0]);
1068
1069 if (!act)
1070 return -ENOMEM;
1071 if (!strcmp(act, "report"))
1072 c->assert_action = ASSACT_REPORT;
1073 else if (!strcmp(act, "read-only"))
1074 c->assert_action = ASSACT_RO;
1075 else if (!strcmp(act, "panic"))
1076 c->assert_action = ASSACT_PANIC;
1077 else {
1078 ubifs_err(c, "unknown assert action \"%s\"", act);
1079 kfree(act);
1080 return -EINVAL;
1081 }
1082 kfree(act);
1083 break;
1084 }
1085 case Opt_ignore:
1086 break;
1087 default:
1088 {
1089 unsigned long flag;
1090 struct super_block *sb = c->vfs_sb;
1091
1092 flag = parse_standard_option(p);
1093 if (!flag) {
1094 ubifs_err(c, "unrecognized mount option \"%s\" or missing value",
1095 p);
1096 return -EINVAL;
1097 }
1098 sb->s_flags |= flag;
1099 break;
1100 }
1101 }
1102 }
1103
1104 return 0;
1105}
1106
1107/**
1108 * destroy_journal - destroy journal data structures.
1109 * @c: UBIFS file-system description object
1110 *
1111 * This function destroys journal data structures including those that may have
1112 * been created by recovery functions.
1113 */
1114static void destroy_journal(struct ubifs_info *c)
1115{
1116 while (!list_empty(&c->unclean_leb_list)) {
1117 struct ubifs_unclean_leb *ucleb;
1118
1119 ucleb = list_entry(c->unclean_leb_list.next,
1120 struct ubifs_unclean_leb, list);
1121 list_del(&ucleb->list);
1122 kfree(ucleb);
1123 }
1124 while (!list_empty(&c->old_buds)) {
1125 struct ubifs_bud *bud;
1126
1127 bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
1128 list_del(&bud->list);
1129 kfree(bud);
1130 }
1131 ubifs_destroy_idx_gc(c);
1132 ubifs_destroy_size_tree(c);
1133 ubifs_tnc_close(c);
1134 free_buds(c);
1135}
1136
1137/**
1138 * bu_init - initialize bulk-read information.
1139 * @c: UBIFS file-system description object
1140 */
1141static void bu_init(struct ubifs_info *c)
1142{
1143 ubifs_assert(c, c->bulk_read == 1);
1144
1145 if (c->bu.buf)
1146 return; /* Already initialized */
1147
1148again:
1149 c->bu.buf = kmalloc(c->max_bu_buf_len, GFP_KERNEL | __GFP_NOWARN);
1150 if (!c->bu.buf) {
1151 if (c->max_bu_buf_len > UBIFS_KMALLOC_OK) {
1152 c->max_bu_buf_len = UBIFS_KMALLOC_OK;
1153 goto again;
1154 }
1155
1156 /* Just disable bulk-read */
1157 ubifs_warn(c, "cannot allocate %d bytes of memory for bulk-read, disabling it",
1158 c->max_bu_buf_len);
1159 c->mount_opts.bulk_read = 1;
1160 c->bulk_read = 0;
1161 return;
1162 }
1163}
1164
1165/**
1166 * check_free_space - check if there is enough free space to mount.
1167 * @c: UBIFS file-system description object
1168 *
1169 * This function makes sure UBIFS has enough free space to be mounted in
1170 * read/write mode. UBIFS must always have some free space to allow deletions.
1171 */
1172static int check_free_space(struct ubifs_info *c)
1173{
1174 ubifs_assert(c, c->dark_wm > 0);
1175 if (c->lst.total_free + c->lst.total_dirty < c->dark_wm) {
1176 ubifs_err(c, "insufficient free space to mount in R/W mode");
1177 ubifs_dump_budg(c, &c->bi);
1178 ubifs_dump_lprops(c);
1179 return -ENOSPC;
1180 }
1181 return 0;
1182}
1183
1184/**
1185 * mount_ubifs - mount UBIFS file-system.
1186 * @c: UBIFS file-system description object
1187 *
1188 * This function mounts UBIFS file system. Returns zero in case of success and
1189 * a negative error code in case of failure.
1190 */
1191static int mount_ubifs(struct ubifs_info *c)
1192{
1193 int err;
1194 long long x, y;
1195 size_t sz;
1196
1197 c->ro_mount = !!sb_rdonly(c->vfs_sb);
1198 /* Suppress error messages while probing if SB_SILENT is set */
1199 c->probing = !!(c->vfs_sb->s_flags & SB_SILENT);
1200
1201 err = init_constants_early(c);
1202 if (err)
1203 return err;
1204
1205 err = ubifs_debugging_init(c);
1206 if (err)
1207 return err;
1208
1209 err = check_volume_empty(c);
1210 if (err)
1211 goto out_free;
1212
1213 if (c->empty && (c->ro_mount || c->ro_media)) {
1214 /*
1215 * This UBI volume is empty, and read-only, or the file system
1216 * is mounted read-only - we cannot format it.
1217 */
1218 ubifs_err(c, "can't format empty UBI volume: read-only %s",
1219 c->ro_media ? "UBI volume" : "mount");
1220 err = -EROFS;
1221 goto out_free;
1222 }
1223
1224 if (c->ro_media && !c->ro_mount) {
1225 ubifs_err(c, "cannot mount read-write - read-only media");
1226 err = -EROFS;
1227 goto out_free;
1228 }
1229
1230 /*
1231 * The requirement for the buffer is that it should fit indexing B-tree
1232 * height amount of integers. We assume the height if the TNC tree will
1233 * never exceed 64.
1234 */
1235 err = -ENOMEM;
1236 c->bottom_up_buf = kmalloc_array(BOTTOM_UP_HEIGHT, sizeof(int),
1237 GFP_KERNEL);
1238 if (!c->bottom_up_buf)
1239 goto out_free;
1240
1241 c->sbuf = vmalloc(c->leb_size);
1242 if (!c->sbuf)
1243 goto out_free;
1244
1245 if (!c->ro_mount) {
1246 c->ileb_buf = vmalloc(c->leb_size);
1247 if (!c->ileb_buf)
1248 goto out_free;
1249 }
1250
1251 if (c->bulk_read == 1)
1252 bu_init(c);
1253
1254 if (!c->ro_mount) {
1255 c->write_reserve_buf = kmalloc(COMPRESSED_DATA_NODE_BUF_SZ + \
1256 UBIFS_CIPHER_BLOCK_SIZE,
1257 GFP_KERNEL);
1258 if (!c->write_reserve_buf)
1259 goto out_free;
1260 }
1261
1262 c->mounting = 1;
1263
1264 err = ubifs_read_superblock(c);
1265 if (err)
1266 goto out_free;
1267
1268 c->probing = 0;
1269
1270 /*
1271 * Make sure the compressor which is set as default in the superblock
1272 * or overridden by mount options is actually compiled in.
1273 */
1274 if (!ubifs_compr_present(c, c->default_compr)) {
1275 ubifs_err(c, "'compressor \"%s\" is not compiled in",
1276 ubifs_compr_name(c, c->default_compr));
1277 err = -ENOTSUPP;
1278 goto out_free;
1279 }
1280
1281 err = init_constants_sb(c);
1282 if (err)
1283 goto out_free;
1284
1285 sz = ALIGN(c->max_idx_node_sz, c->min_io_size);
1286 sz = ALIGN(sz + c->max_idx_node_sz, c->min_io_size);
1287 c->cbuf = kmalloc(sz, GFP_NOFS);
1288 if (!c->cbuf) {
1289 err = -ENOMEM;
1290 goto out_free;
1291 }
1292
1293 err = alloc_wbufs(c);
1294 if (err)
1295 goto out_cbuf;
1296
1297 sprintf(c->bgt_name, BGT_NAME_PATTERN, c->vi.ubi_num, c->vi.vol_id);
1298 if (!c->ro_mount) {
1299 /* Create background thread */
1300 c->bgt = kthread_create(ubifs_bg_thread, c, "%s", c->bgt_name);
1301 if (IS_ERR(c->bgt)) {
1302 err = PTR_ERR(c->bgt);
1303 c->bgt = NULL;
1304 ubifs_err(c, "cannot spawn \"%s\", error %d",
1305 c->bgt_name, err);
1306 goto out_wbufs;
1307 }
1308 wake_up_process(c->bgt);
1309 }
1310
1311 err = ubifs_read_master(c);
1312 if (err)
1313 goto out_master;
1314
1315 init_constants_master(c);
1316
1317 if ((c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY)) != 0) {
1318 ubifs_msg(c, "recovery needed");
1319 c->need_recovery = 1;
1320 }
1321
1322 if (c->need_recovery && !c->ro_mount) {
1323 err = ubifs_recover_inl_heads(c, c->sbuf);
1324 if (err)
1325 goto out_master;
1326 }
1327
1328 err = ubifs_lpt_init(c, 1, !c->ro_mount);
1329 if (err)
1330 goto out_master;
1331
1332 if (!c->ro_mount && c->space_fixup) {
1333 err = ubifs_fixup_free_space(c);
1334 if (err)
1335 goto out_lpt;
1336 }
1337
1338 if (!c->ro_mount && !c->need_recovery) {
1339 /*
1340 * Set the "dirty" flag so that if we reboot uncleanly we
1341 * will notice this immediately on the next mount.
1342 */
1343 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
1344 err = ubifs_write_master(c);
1345 if (err)
1346 goto out_lpt;
1347 }
1348
1349 err = dbg_check_idx_size(c, c->bi.old_idx_sz);
1350 if (err)
1351 goto out_lpt;
1352
1353 err = ubifs_replay_journal(c);
1354 if (err)
1355 goto out_journal;
1356
1357 /* Calculate 'min_idx_lebs' after journal replay */
1358 c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
1359
1360 err = ubifs_mount_orphans(c, c->need_recovery, c->ro_mount);
1361 if (err)
1362 goto out_orphans;
1363
1364 if (!c->ro_mount) {
1365 int lnum;
1366
1367 err = check_free_space(c);
1368 if (err)
1369 goto out_orphans;
1370
1371 /* Check for enough log space */
1372 lnum = c->lhead_lnum + 1;
1373 if (lnum >= UBIFS_LOG_LNUM + c->log_lebs)
1374 lnum = UBIFS_LOG_LNUM;
1375 if (lnum == c->ltail_lnum) {
1376 err = ubifs_consolidate_log(c);
1377 if (err)
1378 goto out_orphans;
1379 }
1380
1381 if (c->need_recovery) {
1382 err = ubifs_recover_size(c);
1383 if (err)
1384 goto out_orphans;
1385 err = ubifs_rcvry_gc_commit(c);
1386 if (err)
1387 goto out_orphans;
1388 } else {
1389 err = take_gc_lnum(c);
1390 if (err)
1391 goto out_orphans;
1392
1393 /*
1394 * GC LEB may contain garbage if there was an unclean
1395 * reboot, and it should be un-mapped.
1396 */
1397 err = ubifs_leb_unmap(c, c->gc_lnum);
1398 if (err)
1399 goto out_orphans;
1400 }
1401
1402 err = dbg_check_lprops(c);
1403 if (err)
1404 goto out_orphans;
1405 } else if (c->need_recovery) {
1406 err = ubifs_recover_size(c);
1407 if (err)
1408 goto out_orphans;
1409 } else {
1410 /*
1411 * Even if we mount read-only, we have to set space in GC LEB
1412 * to proper value because this affects UBIFS free space
1413 * reporting. We do not want to have a situation when
1414 * re-mounting from R/O to R/W changes amount of free space.
1415 */
1416 err = take_gc_lnum(c);
1417 if (err)
1418 goto out_orphans;
1419 }
1420
1421 spin_lock(&ubifs_infos_lock);
1422 list_add_tail(&c->infos_list, &ubifs_infos);
1423 spin_unlock(&ubifs_infos_lock);
1424
1425 if (c->need_recovery) {
1426 if (c->ro_mount)
1427 ubifs_msg(c, "recovery deferred");
1428 else {
1429 c->need_recovery = 0;
1430 ubifs_msg(c, "recovery completed");
1431 /*
1432 * GC LEB has to be empty and taken at this point. But
1433 * the journal head LEBs may also be accounted as
1434 * "empty taken" if they are empty.
1435 */
1436 ubifs_assert(c, c->lst.taken_empty_lebs > 0);
1437 }
1438 } else
1439 ubifs_assert(c, c->lst.taken_empty_lebs > 0);
1440
1441 err = dbg_check_filesystem(c);
1442 if (err)
1443 goto out_infos;
1444
1445 err = dbg_debugfs_init_fs(c);
1446 if (err)
1447 goto out_infos;
1448
1449 c->mounting = 0;
1450
1451 ubifs_msg(c, "UBIFS: mounted UBI device %d, volume %d, name \"%s\"%s",
1452 c->vi.ubi_num, c->vi.vol_id, c->vi.name,
1453 c->ro_mount ? ", R/O mode" : "");
1454 x = (long long)c->main_lebs * c->leb_size;
1455 y = (long long)c->log_lebs * c->leb_size + c->max_bud_bytes;
1456 ubifs_msg(c, "LEB size: %d bytes (%d KiB), min./max. I/O unit sizes: %d bytes/%d bytes",
1457 c->leb_size, c->leb_size >> 10, c->min_io_size,
1458 c->max_write_size);
1459 ubifs_msg(c, "FS size: %lld bytes (%lld MiB, %d LEBs), journal size %lld bytes (%lld MiB, %d LEBs)",
1460 x, x >> 20, c->main_lebs,
1461 y, y >> 20, c->log_lebs + c->max_bud_cnt);
1462 ubifs_msg(c, "reserved for root: %llu bytes (%llu KiB)",
1463 c->report_rp_size, c->report_rp_size >> 10);
1464 ubifs_msg(c, "media format: w%d/r%d (latest is w%d/r%d), UUID %pUB%s",
1465 c->fmt_version, c->ro_compat_version,
1466 UBIFS_FORMAT_VERSION, UBIFS_RO_COMPAT_VERSION, c->uuid,
1467 c->big_lpt ? ", big LPT model" : ", small LPT model");
1468
1469 dbg_gen("default compressor: %s", ubifs_compr_name(c, c->default_compr));
1470 dbg_gen("data journal heads: %d",
1471 c->jhead_cnt - NONDATA_JHEADS_CNT);
1472 dbg_gen("log LEBs: %d (%d - %d)",
1473 c->log_lebs, UBIFS_LOG_LNUM, c->log_last);
1474 dbg_gen("LPT area LEBs: %d (%d - %d)",
1475 c->lpt_lebs, c->lpt_first, c->lpt_last);
1476 dbg_gen("orphan area LEBs: %d (%d - %d)",
1477 c->orph_lebs, c->orph_first, c->orph_last);
1478 dbg_gen("main area LEBs: %d (%d - %d)",
1479 c->main_lebs, c->main_first, c->leb_cnt - 1);
1480 dbg_gen("index LEBs: %d", c->lst.idx_lebs);
1481 dbg_gen("total index bytes: %lld (%lld KiB, %lld MiB)",
1482 c->bi.old_idx_sz, c->bi.old_idx_sz >> 10,
1483 c->bi.old_idx_sz >> 20);
1484 dbg_gen("key hash type: %d", c->key_hash_type);
1485 dbg_gen("tree fanout: %d", c->fanout);
1486 dbg_gen("reserved GC LEB: %d", c->gc_lnum);
1487 dbg_gen("max. znode size %d", c->max_znode_sz);
1488 dbg_gen("max. index node size %d", c->max_idx_node_sz);
1489 dbg_gen("node sizes: data %zu, inode %zu, dentry %zu",
1490 UBIFS_DATA_NODE_SZ, UBIFS_INO_NODE_SZ, UBIFS_DENT_NODE_SZ);
1491 dbg_gen("node sizes: trun %zu, sb %zu, master %zu",
1492 UBIFS_TRUN_NODE_SZ, UBIFS_SB_NODE_SZ, UBIFS_MST_NODE_SZ);
1493 dbg_gen("node sizes: ref %zu, cmt. start %zu, orph %zu",
1494 UBIFS_REF_NODE_SZ, UBIFS_CS_NODE_SZ, UBIFS_ORPH_NODE_SZ);
1495 dbg_gen("max. node sizes: data %zu, inode %zu dentry %zu, idx %d",
1496 UBIFS_MAX_DATA_NODE_SZ, UBIFS_MAX_INO_NODE_SZ,
1497 UBIFS_MAX_DENT_NODE_SZ, ubifs_idx_node_sz(c, c->fanout));
1498 dbg_gen("dead watermark: %d", c->dead_wm);
1499 dbg_gen("dark watermark: %d", c->dark_wm);
1500 dbg_gen("LEB overhead: %d", c->leb_overhead);
1501 x = (long long)c->main_lebs * c->dark_wm;
1502 dbg_gen("max. dark space: %lld (%lld KiB, %lld MiB)",
1503 x, x >> 10, x >> 20);
1504 dbg_gen("maximum bud bytes: %lld (%lld KiB, %lld MiB)",
1505 c->max_bud_bytes, c->max_bud_bytes >> 10,
1506 c->max_bud_bytes >> 20);
1507 dbg_gen("BG commit bud bytes: %lld (%lld KiB, %lld MiB)",
1508 c->bg_bud_bytes, c->bg_bud_bytes >> 10,
1509 c->bg_bud_bytes >> 20);
1510 dbg_gen("current bud bytes %lld (%lld KiB, %lld MiB)",
1511 c->bud_bytes, c->bud_bytes >> 10, c->bud_bytes >> 20);
1512 dbg_gen("max. seq. number: %llu", c->max_sqnum);
1513 dbg_gen("commit number: %llu", c->cmt_no);
1514
1515 return 0;
1516
1517out_infos:
1518 spin_lock(&ubifs_infos_lock);
1519 list_del(&c->infos_list);
1520 spin_unlock(&ubifs_infos_lock);
1521out_orphans:
1522 free_orphans(c);
1523out_journal:
1524 destroy_journal(c);
1525out_lpt:
1526 ubifs_lpt_free(c, 0);
1527out_master:
1528 kfree(c->mst_node);
1529 kfree(c->rcvrd_mst_node);
1530 if (c->bgt)
1531 kthread_stop(c->bgt);
1532out_wbufs:
1533 free_wbufs(c);
1534out_cbuf:
1535 kfree(c->cbuf);
1536out_free:
1537 kfree(c->write_reserve_buf);
1538 kfree(c->bu.buf);
1539 vfree(c->ileb_buf);
1540 vfree(c->sbuf);
1541 kfree(c->bottom_up_buf);
1542 ubifs_debugging_exit(c);
1543 return err;
1544}
1545
1546/**
1547 * ubifs_umount - un-mount UBIFS file-system.
1548 * @c: UBIFS file-system description object
1549 *
1550 * Note, this function is called to free allocated resourced when un-mounting,
1551 * as well as free resources when an error occurred while we were half way
1552 * through mounting (error path cleanup function). So it has to make sure the
1553 * resource was actually allocated before freeing it.
1554 */
1555static void ubifs_umount(struct ubifs_info *c)
1556{
1557 dbg_gen("un-mounting UBI device %d, volume %d", c->vi.ubi_num,
1558 c->vi.vol_id);
1559
1560 dbg_debugfs_exit_fs(c);
1561 spin_lock(&ubifs_infos_lock);
1562 list_del(&c->infos_list);
1563 spin_unlock(&ubifs_infos_lock);
1564
1565 if (c->bgt)
1566 kthread_stop(c->bgt);
1567
1568 destroy_journal(c);
1569 free_wbufs(c);
1570 free_orphans(c);
1571 ubifs_lpt_free(c, 0);
1572
1573 kfree(c->cbuf);
1574 kfree(c->rcvrd_mst_node);
1575 kfree(c->mst_node);
1576 kfree(c->write_reserve_buf);
1577 kfree(c->bu.buf);
1578 vfree(c->ileb_buf);
1579 vfree(c->sbuf);
1580 kfree(c->bottom_up_buf);
1581 ubifs_debugging_exit(c);
1582}
1583
1584/**
1585 * ubifs_remount_rw - re-mount in read-write mode.
1586 * @c: UBIFS file-system description object
1587 *
1588 * UBIFS avoids allocating many unnecessary resources when mounted in read-only
1589 * mode. This function allocates the needed resources and re-mounts UBIFS in
1590 * read-write mode.
1591 */
1592static int ubifs_remount_rw(struct ubifs_info *c)
1593{
1594 int err, lnum;
1595
1596 if (c->rw_incompat) {
1597 ubifs_err(c, "the file-system is not R/W-compatible");
1598 ubifs_msg(c, "on-flash format version is w%d/r%d, but software only supports up to version w%d/r%d",
1599 c->fmt_version, c->ro_compat_version,
1600 UBIFS_FORMAT_VERSION, UBIFS_RO_COMPAT_VERSION);
1601 return -EROFS;
1602 }
1603
1604 mutex_lock(&c->umount_mutex);
1605 dbg_save_space_info(c);
1606 c->remounting_rw = 1;
1607 c->ro_mount = 0;
1608
1609 if (c->space_fixup) {
1610 err = ubifs_fixup_free_space(c);
1611 if (err)
1612 goto out;
1613 }
1614
1615 err = check_free_space(c);
1616 if (err)
1617 goto out;
1618
1619 if (c->old_leb_cnt != c->leb_cnt) {
1620 struct ubifs_sb_node *sup;
1621
1622 sup = ubifs_read_sb_node(c);
1623 if (IS_ERR(sup)) {
1624 err = PTR_ERR(sup);
1625 goto out;
1626 }
1627 sup->leb_cnt = cpu_to_le32(c->leb_cnt);
1628 err = ubifs_write_sb_node(c, sup);
1629 kfree(sup);
1630 if (err)
1631 goto out;
1632 }
1633
1634 if (c->need_recovery) {
1635 ubifs_msg(c, "completing deferred recovery");
1636 err = ubifs_write_rcvrd_mst_node(c);
1637 if (err)
1638 goto out;
1639 err = ubifs_recover_size(c);
1640 if (err)
1641 goto out;
1642 err = ubifs_clean_lebs(c, c->sbuf);
1643 if (err)
1644 goto out;
1645 err = ubifs_recover_inl_heads(c, c->sbuf);
1646 if (err)
1647 goto out;
1648 } else {
1649 /* A readonly mount is not allowed to have orphans */
1650 ubifs_assert(c, c->tot_orphans == 0);
1651 err = ubifs_clear_orphans(c);
1652 if (err)
1653 goto out;
1654 }
1655
1656 if (!(c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY))) {
1657 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
1658 err = ubifs_write_master(c);
1659 if (err)
1660 goto out;
1661 }
1662
1663 c->ileb_buf = vmalloc(c->leb_size);
1664 if (!c->ileb_buf) {
1665 err = -ENOMEM;
1666 goto out;
1667 }
1668
1669 c->write_reserve_buf = kmalloc(COMPRESSED_DATA_NODE_BUF_SZ + \
1670 UBIFS_CIPHER_BLOCK_SIZE, GFP_KERNEL);
1671 if (!c->write_reserve_buf) {
1672 err = -ENOMEM;
1673 goto out;
1674 }
1675
1676 err = ubifs_lpt_init(c, 0, 1);
1677 if (err)
1678 goto out;
1679
1680 /* Create background thread */
1681 c->bgt = kthread_create(ubifs_bg_thread, c, "%s", c->bgt_name);
1682 if (IS_ERR(c->bgt)) {
1683 err = PTR_ERR(c->bgt);
1684 c->bgt = NULL;
1685 ubifs_err(c, "cannot spawn \"%s\", error %d",
1686 c->bgt_name, err);
1687 goto out;
1688 }
1689 wake_up_process(c->bgt);
1690
1691 c->orph_buf = vmalloc(c->leb_size);
1692 if (!c->orph_buf) {
1693 err = -ENOMEM;
1694 goto out;
1695 }
1696
1697 /* Check for enough log space */
1698 lnum = c->lhead_lnum + 1;
1699 if (lnum >= UBIFS_LOG_LNUM + c->log_lebs)
1700 lnum = UBIFS_LOG_LNUM;
1701 if (lnum == c->ltail_lnum) {
1702 err = ubifs_consolidate_log(c);
1703 if (err)
1704 goto out;
1705 }
1706
1707 if (c->need_recovery)
1708 err = ubifs_rcvry_gc_commit(c);
1709 else
1710 err = ubifs_leb_unmap(c, c->gc_lnum);
1711 if (err)
1712 goto out;
1713
1714 dbg_gen("re-mounted read-write");
1715 c->remounting_rw = 0;
1716
1717 if (c->need_recovery) {
1718 c->need_recovery = 0;
1719 ubifs_msg(c, "deferred recovery completed");
1720 } else {
1721 /*
1722 * Do not run the debugging space check if the were doing
1723 * recovery, because when we saved the information we had the
1724 * file-system in a state where the TNC and lprops has been
1725 * modified in memory, but all the I/O operations (including a
1726 * commit) were deferred. So the file-system was in
1727 * "non-committed" state. Now the file-system is in committed
1728 * state, and of course the amount of free space will change
1729 * because, for example, the old index size was imprecise.
1730 */
1731 err = dbg_check_space_info(c);
1732 }
1733
1734 mutex_unlock(&c->umount_mutex);
1735 return err;
1736
1737out:
1738 c->ro_mount = 1;
1739 vfree(c->orph_buf);
1740 c->orph_buf = NULL;
1741 if (c->bgt) {
1742 kthread_stop(c->bgt);
1743 c->bgt = NULL;
1744 }
1745 free_wbufs(c);
1746 kfree(c->write_reserve_buf);
1747 c->write_reserve_buf = NULL;
1748 vfree(c->ileb_buf);
1749 c->ileb_buf = NULL;
1750 ubifs_lpt_free(c, 1);
1751 c->remounting_rw = 0;
1752 mutex_unlock(&c->umount_mutex);
1753 return err;
1754}
1755
1756/**
1757 * ubifs_remount_ro - re-mount in read-only mode.
1758 * @c: UBIFS file-system description object
1759 *
1760 * We assume VFS has stopped writing. Possibly the background thread could be
1761 * running a commit, however kthread_stop will wait in that case.
1762 */
1763static void ubifs_remount_ro(struct ubifs_info *c)
1764{
1765 int i, err;
1766
1767 ubifs_assert(c, !c->need_recovery);
1768 ubifs_assert(c, !c->ro_mount);
1769
1770 mutex_lock(&c->umount_mutex);
1771 if (c->bgt) {
1772 kthread_stop(c->bgt);
1773 c->bgt = NULL;
1774 }
1775
1776 dbg_save_space_info(c);
1777
1778 for (i = 0; i < c->jhead_cnt; i++) {
1779 err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
1780 if (err)
1781 ubifs_ro_mode(c, err);
1782 }
1783
1784 c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
1785 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
1786 c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
1787 err = ubifs_write_master(c);
1788 if (err)
1789 ubifs_ro_mode(c, err);
1790
1791 vfree(c->orph_buf);
1792 c->orph_buf = NULL;
1793 kfree(c->write_reserve_buf);
1794 c->write_reserve_buf = NULL;
1795 vfree(c->ileb_buf);
1796 c->ileb_buf = NULL;
1797 ubifs_lpt_free(c, 1);
1798 c->ro_mount = 1;
1799 err = dbg_check_space_info(c);
1800 if (err)
1801 ubifs_ro_mode(c, err);
1802 mutex_unlock(&c->umount_mutex);
1803}
1804
1805static void ubifs_put_super(struct super_block *sb)
1806{
1807 int i;
1808 struct ubifs_info *c = sb->s_fs_info;
1809
1810 ubifs_msg(c, "un-mount UBI device %d", c->vi.ubi_num);
1811
1812 /*
1813 * The following asserts are only valid if there has not been a failure
1814 * of the media. For example, there will be dirty inodes if we failed
1815 * to write them back because of I/O errors.
1816 */
1817 if (!c->ro_error) {
1818 ubifs_assert(c, c->bi.idx_growth == 0);
1819 ubifs_assert(c, c->bi.dd_growth == 0);
1820 ubifs_assert(c, c->bi.data_growth == 0);
1821 }
1822
1823 /*
1824 * The 'c->umount_lock' prevents races between UBIFS memory shrinker
1825 * and file system un-mount. Namely, it prevents the shrinker from
1826 * picking this superblock for shrinking - it will be just skipped if
1827 * the mutex is locked.
1828 */
1829 mutex_lock(&c->umount_mutex);
1830 if (!c->ro_mount) {
1831 /*
1832 * First of all kill the background thread to make sure it does
1833 * not interfere with un-mounting and freeing resources.
1834 */
1835 if (c->bgt) {
1836 kthread_stop(c->bgt);
1837 c->bgt = NULL;
1838 }
1839
1840 /*
1841 * On fatal errors c->ro_error is set to 1, in which case we do
1842 * not write the master node.
1843 */
1844 if (!c->ro_error) {
1845 int err;
1846
1847 /* Synchronize write-buffers */
1848 for (i = 0; i < c->jhead_cnt; i++) {
1849 err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
1850 if (err)
1851 ubifs_ro_mode(c, err);
1852 }
1853
1854 /*
1855 * We are being cleanly unmounted which means the
1856 * orphans were killed - indicate this in the master
1857 * node. Also save the reserved GC LEB number.
1858 */
1859 c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
1860 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
1861 c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
1862 err = ubifs_write_master(c);
1863 if (err)
1864 /*
1865 * Recovery will attempt to fix the master area
1866 * next mount, so we just print a message and
1867 * continue to unmount normally.
1868 */
1869 ubifs_err(c, "failed to write master node, error %d",
1870 err);
1871 } else {
1872 for (i = 0; i < c->jhead_cnt; i++)
1873 /* Make sure write-buffer timers are canceled */
1874 hrtimer_cancel(&c->jheads[i].wbuf.timer);
1875 }
1876 }
1877
1878 ubifs_umount(c);
1879 ubi_close_volume(c->ubi);
1880 mutex_unlock(&c->umount_mutex);
1881}
1882
1883static int ubifs_remount_fs(struct super_block *sb, int *flags, char *data)
1884{
1885 int err;
1886 struct ubifs_info *c = sb->s_fs_info;
1887
1888 sync_filesystem(sb);
1889 dbg_gen("old flags %#lx, new flags %#x", sb->s_flags, *flags);
1890
1891 err = ubifs_parse_options(c, data, 1);
1892 if (err) {
1893 ubifs_err(c, "invalid or unknown remount parameter");
1894 return err;
1895 }
1896
1897 if (c->ro_mount && !(*flags & SB_RDONLY)) {
1898 if (c->ro_error) {
1899 ubifs_msg(c, "cannot re-mount R/W due to prior errors");
1900 return -EROFS;
1901 }
1902 if (c->ro_media) {
1903 ubifs_msg(c, "cannot re-mount R/W - UBI volume is R/O");
1904 return -EROFS;
1905 }
1906 err = ubifs_remount_rw(c);
1907 if (err)
1908 return err;
1909 } else if (!c->ro_mount && (*flags & SB_RDONLY)) {
1910 if (c->ro_error) {
1911 ubifs_msg(c, "cannot re-mount R/O due to prior errors");
1912 return -EROFS;
1913 }
1914 ubifs_remount_ro(c);
1915 }
1916
1917 if (c->bulk_read == 1)
1918 bu_init(c);
1919 else {
1920 dbg_gen("disable bulk-read");
1921 mutex_lock(&c->bu_mutex);
1922 kfree(c->bu.buf);
1923 c->bu.buf = NULL;
1924 mutex_unlock(&c->bu_mutex);
1925 }
1926
1927 if (!c->need_recovery)
1928 ubifs_assert(c, c->lst.taken_empty_lebs > 0);
1929
1930 return 0;
1931}
1932
1933const struct super_operations ubifs_super_operations = {
1934 .alloc_inode = ubifs_alloc_inode,
1935 .destroy_inode = ubifs_destroy_inode,
1936 .put_super = ubifs_put_super,
1937 .write_inode = ubifs_write_inode,
1938 .drop_inode = ubifs_drop_inode,
1939 .evict_inode = ubifs_evict_inode,
1940 .statfs = ubifs_statfs,
1941 .dirty_inode = ubifs_dirty_inode,
1942 .remount_fs = ubifs_remount_fs,
1943 .show_options = ubifs_show_options,
1944 .sync_fs = ubifs_sync_fs,
1945};
1946
1947/**
1948 * open_ubi - parse UBI device name string and open the UBI device.
1949 * @name: UBI volume name
1950 * @mode: UBI volume open mode
1951 *
1952 * The primary method of mounting UBIFS is by specifying the UBI volume
1953 * character device node path. However, UBIFS may also be mounted withoug any
1954 * character device node using one of the following methods:
1955 *
1956 * o ubiX_Y - mount UBI device number X, volume Y;
1957 * o ubiY - mount UBI device number 0, volume Y;
1958 * o ubiX:NAME - mount UBI device X, volume with name NAME;
1959 * o ubi:NAME - mount UBI device 0, volume with name NAME.
1960 *
1961 * Alternative '!' separator may be used instead of ':' (because some shells
1962 * like busybox may interpret ':' as an NFS host name separator). This function
1963 * returns UBI volume description object in case of success and a negative
1964 * error code in case of failure.
1965 */
1966static struct ubi_volume_desc *open_ubi(const char *name, int mode)
1967{
1968 struct ubi_volume_desc *ubi;
1969 int dev, vol;
1970 char *endptr;
1971
1972 if (!name || !*name)
1973 return ERR_PTR(-EINVAL);
1974
1975 /* First, try to open using the device node path method */
1976 ubi = ubi_open_volume_path(name, mode);
1977 if (!IS_ERR(ubi))
1978 return ubi;
1979
1980 /* Try the "nodev" method */
1981 if (name[0] != 'u' || name[1] != 'b' || name[2] != 'i')
1982 return ERR_PTR(-EINVAL);
1983
1984 /* ubi:NAME method */
1985 if ((name[3] == ':' || name[3] == '!') && name[4] != '\0')
1986 return ubi_open_volume_nm(0, name + 4, mode);
1987
1988 if (!isdigit(name[3]))
1989 return ERR_PTR(-EINVAL);
1990
1991 dev = simple_strtoul(name + 3, &endptr, 0);
1992
1993 /* ubiY method */
1994 if (*endptr == '\0')
1995 return ubi_open_volume(0, dev, mode);
1996
1997 /* ubiX_Y method */
1998 if (*endptr == '_' && isdigit(endptr[1])) {
1999 vol = simple_strtoul(endptr + 1, &endptr, 0);
2000 if (*endptr != '\0')
2001 return ERR_PTR(-EINVAL);
2002 return ubi_open_volume(dev, vol, mode);
2003 }
2004
2005 /* ubiX:NAME method */
2006 if ((*endptr == ':' || *endptr == '!') && endptr[1] != '\0')
2007 return ubi_open_volume_nm(dev, ++endptr, mode);
2008
2009 return ERR_PTR(-EINVAL);
2010}
2011
2012static struct ubifs_info *alloc_ubifs_info(struct ubi_volume_desc *ubi)
2013{
2014 struct ubifs_info *c;
2015
2016 c = kzalloc(sizeof(struct ubifs_info), GFP_KERNEL);
2017 if (c) {
2018 spin_lock_init(&c->cnt_lock);
2019 spin_lock_init(&c->cs_lock);
2020 spin_lock_init(&c->buds_lock);
2021 spin_lock_init(&c->space_lock);
2022 spin_lock_init(&c->orphan_lock);
2023 init_rwsem(&c->commit_sem);
2024 mutex_init(&c->lp_mutex);
2025 mutex_init(&c->tnc_mutex);
2026 mutex_init(&c->log_mutex);
2027 mutex_init(&c->umount_mutex);
2028 mutex_init(&c->bu_mutex);
2029 mutex_init(&c->write_reserve_mutex);
2030 init_waitqueue_head(&c->cmt_wq);
2031 c->buds = RB_ROOT;
2032 c->old_idx = RB_ROOT;
2033 c->size_tree = RB_ROOT;
2034 c->orph_tree = RB_ROOT;
2035 INIT_LIST_HEAD(&c->infos_list);
2036 INIT_LIST_HEAD(&c->idx_gc);
2037 INIT_LIST_HEAD(&c->replay_list);
2038 INIT_LIST_HEAD(&c->replay_buds);
2039 INIT_LIST_HEAD(&c->uncat_list);
2040 INIT_LIST_HEAD(&c->empty_list);
2041 INIT_LIST_HEAD(&c->freeable_list);
2042 INIT_LIST_HEAD(&c->frdi_idx_list);
2043 INIT_LIST_HEAD(&c->unclean_leb_list);
2044 INIT_LIST_HEAD(&c->old_buds);
2045 INIT_LIST_HEAD(&c->orph_list);
2046 INIT_LIST_HEAD(&c->orph_new);
2047 c->no_chk_data_crc = 1;
2048 c->assert_action = ASSACT_RO;
2049
2050 c->highest_inum = UBIFS_FIRST_INO;
2051 c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM;
2052
2053 ubi_get_volume_info(ubi, &c->vi);
2054 ubi_get_device_info(c->vi.ubi_num, &c->di);
2055 }
2056 return c;
2057}
2058
2059static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
2060{
2061 struct ubifs_info *c = sb->s_fs_info;
2062 struct inode *root;
2063 int err;
2064
2065 c->vfs_sb = sb;
2066 /* Re-open the UBI device in read-write mode */
2067 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READWRITE);
2068 if (IS_ERR(c->ubi)) {
2069 err = PTR_ERR(c->ubi);
2070 goto out;
2071 }
2072
2073 err = ubifs_parse_options(c, data, 0);
2074 if (err)
2075 goto out_close;
2076
2077 /*
2078 * UBIFS provides 'backing_dev_info' in order to disable read-ahead. For
2079 * UBIFS, I/O is not deferred, it is done immediately in readpage,
2080 * which means the user would have to wait not just for their own I/O
2081 * but the read-ahead I/O as well i.e. completely pointless.
2082 *
2083 * Read-ahead will be disabled because @sb->s_bdi->ra_pages is 0. Also
2084 * @sb->s_bdi->capabilities are initialized to 0 so there won't be any
2085 * writeback happening.
2086 */
2087 err = super_setup_bdi_name(sb, "ubifs_%d_%d", c->vi.ubi_num,
2088 c->vi.vol_id);
2089 if (err)
2090 goto out_close;
2091
2092 sb->s_fs_info = c;
2093 sb->s_magic = UBIFS_SUPER_MAGIC;
2094 sb->s_blocksize = UBIFS_BLOCK_SIZE;
2095 sb->s_blocksize_bits = UBIFS_BLOCK_SHIFT;
2096 sb->s_maxbytes = c->max_inode_sz = key_max_inode_size(c);
2097 if (c->max_inode_sz > MAX_LFS_FILESIZE)
2098 sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE;
2099 sb->s_op = &ubifs_super_operations;
2100#ifdef CONFIG_UBIFS_FS_XATTR
2101 sb->s_xattr = ubifs_xattr_handlers;
2102#endif
2103#ifdef CONFIG_FS_ENCRYPTION
2104 sb->s_cop = &ubifs_crypt_operations;
2105#endif
2106
2107 mutex_lock(&c->umount_mutex);
2108 err = mount_ubifs(c);
2109 if (err) {
2110 ubifs_assert(c, err < 0);
2111 goto out_unlock;
2112 }
2113
2114 /* Read the root inode */
2115 root = ubifs_iget(sb, UBIFS_ROOT_INO);
2116 if (IS_ERR(root)) {
2117 err = PTR_ERR(root);
2118 goto out_umount;
2119 }
2120
2121 sb->s_root = d_make_root(root);
2122 if (!sb->s_root) {
2123 err = -ENOMEM;
2124 goto out_umount;
2125 }
2126
2127 mutex_unlock(&c->umount_mutex);
2128 return 0;
2129
2130out_umount:
2131 ubifs_umount(c);
2132out_unlock:
2133 mutex_unlock(&c->umount_mutex);
2134out_close:
2135 ubi_close_volume(c->ubi);
2136out:
2137 return err;
2138}
2139
2140static int sb_test(struct super_block *sb, void *data)
2141{
2142 struct ubifs_info *c1 = data;
2143 struct ubifs_info *c = sb->s_fs_info;
2144
2145 return c->vi.cdev == c1->vi.cdev;
2146}
2147
2148static int sb_set(struct super_block *sb, void *data)
2149{
2150 sb->s_fs_info = data;
2151 return set_anon_super(sb, NULL);
2152}
2153
2154static struct dentry *ubifs_mount(struct file_system_type *fs_type, int flags,
2155 const char *name, void *data)
2156{
2157 struct ubi_volume_desc *ubi;
2158 struct ubifs_info *c;
2159 struct super_block *sb;
2160 int err;
2161
2162 dbg_gen("name %s, flags %#x", name, flags);
2163
2164 /*
2165 * Get UBI device number and volume ID. Mount it read-only so far
2166 * because this might be a new mount point, and UBI allows only one
2167 * read-write user at a time.
2168 */
2169 ubi = open_ubi(name, UBI_READONLY);
2170 if (IS_ERR(ubi)) {
2171 if (!(flags & SB_SILENT))
2172 pr_err("UBIFS error (pid: %d): cannot open \"%s\", error %d",
2173 current->pid, name, (int)PTR_ERR(ubi));
2174 return ERR_CAST(ubi);
2175 }
2176
2177 c = alloc_ubifs_info(ubi);
2178 if (!c) {
2179 err = -ENOMEM;
2180 goto out_close;
2181 }
2182
2183 dbg_gen("opened ubi%d_%d", c->vi.ubi_num, c->vi.vol_id);
2184
2185 sb = sget(fs_type, sb_test, sb_set, flags, c);
2186 if (IS_ERR(sb)) {
2187 err = PTR_ERR(sb);
2188 kfree(c);
2189 goto out_close;
2190 }
2191
2192 if (sb->s_root) {
2193 struct ubifs_info *c1 = sb->s_fs_info;
2194 kfree(c);
2195 /* A new mount point for already mounted UBIFS */
2196 dbg_gen("this ubi volume is already mounted");
2197 if (!!(flags & SB_RDONLY) != c1->ro_mount) {
2198 err = -EBUSY;
2199 goto out_deact;
2200 }
2201 } else {
2202 err = ubifs_fill_super(sb, data, flags & SB_SILENT ? 1 : 0);
2203 if (err)
2204 goto out_deact;
2205 /* We do not support atime */
2206 sb->s_flags |= SB_ACTIVE;
2207#ifndef CONFIG_UBIFS_ATIME_SUPPORT
2208 sb->s_flags |= SB_NOATIME;
2209#else
2210 ubifs_msg(c, "full atime support is enabled.");
2211#endif
2212 }
2213
2214 /* 'fill_super()' opens ubi again so we must close it here */
2215 ubi_close_volume(ubi);
2216
2217 return dget(sb->s_root);
2218
2219out_deact:
2220 deactivate_locked_super(sb);
2221out_close:
2222 ubi_close_volume(ubi);
2223 return ERR_PTR(err);
2224}
2225
2226static void kill_ubifs_super(struct super_block *s)
2227{
2228 struct ubifs_info *c = s->s_fs_info;
2229 kill_anon_super(s);
2230 kfree(c);
2231}
2232
2233static struct file_system_type ubifs_fs_type = {
2234 .name = "ubifs",
2235 .owner = THIS_MODULE,
2236 .mount = ubifs_mount,
2237 .kill_sb = kill_ubifs_super,
2238};
2239MODULE_ALIAS_FS("ubifs");
2240
2241/*
2242 * Inode slab cache constructor.
2243 */
2244static void inode_slab_ctor(void *obj)
2245{
2246 struct ubifs_inode *ui = obj;
2247 inode_init_once(&ui->vfs_inode);
2248}
2249
2250static int __init ubifs_init(void)
2251{
2252 int err;
2253
2254 BUILD_BUG_ON(sizeof(struct ubifs_ch) != 24);
2255
2256 /* Make sure node sizes are 8-byte aligned */
2257 BUILD_BUG_ON(UBIFS_CH_SZ & 7);
2258 BUILD_BUG_ON(UBIFS_INO_NODE_SZ & 7);
2259 BUILD_BUG_ON(UBIFS_DENT_NODE_SZ & 7);
2260 BUILD_BUG_ON(UBIFS_XENT_NODE_SZ & 7);
2261 BUILD_BUG_ON(UBIFS_DATA_NODE_SZ & 7);
2262 BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ & 7);
2263 BUILD_BUG_ON(UBIFS_SB_NODE_SZ & 7);
2264 BUILD_BUG_ON(UBIFS_MST_NODE_SZ & 7);
2265 BUILD_BUG_ON(UBIFS_REF_NODE_SZ & 7);
2266 BUILD_BUG_ON(UBIFS_CS_NODE_SZ & 7);
2267 BUILD_BUG_ON(UBIFS_ORPH_NODE_SZ & 7);
2268
2269 BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ & 7);
2270 BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ & 7);
2271 BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ & 7);
2272 BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ & 7);
2273 BUILD_BUG_ON(UBIFS_MAX_NODE_SZ & 7);
2274 BUILD_BUG_ON(MIN_WRITE_SZ & 7);
2275
2276 /* Check min. node size */
2277 BUILD_BUG_ON(UBIFS_INO_NODE_SZ < MIN_WRITE_SZ);
2278 BUILD_BUG_ON(UBIFS_DENT_NODE_SZ < MIN_WRITE_SZ);
2279 BUILD_BUG_ON(UBIFS_XENT_NODE_SZ < MIN_WRITE_SZ);
2280 BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ < MIN_WRITE_SZ);
2281
2282 BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
2283 BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
2284 BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ > UBIFS_MAX_NODE_SZ);
2285 BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ > UBIFS_MAX_NODE_SZ);
2286
2287 /* Defined node sizes */
2288 BUILD_BUG_ON(UBIFS_SB_NODE_SZ != 4096);
2289 BUILD_BUG_ON(UBIFS_MST_NODE_SZ != 512);
2290 BUILD_BUG_ON(UBIFS_INO_NODE_SZ != 160);
2291 BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64);
2292
2293 /*
2294 * We use 2 bit wide bit-fields to store compression type, which should
2295 * be amended if more compressors are added. The bit-fields are:
2296 * @compr_type in 'struct ubifs_inode', @default_compr in
2297 * 'struct ubifs_info' and @compr_type in 'struct ubifs_mount_opts'.
2298 */
2299 BUILD_BUG_ON(UBIFS_COMPR_TYPES_CNT > 4);
2300
2301 /*
2302 * We require that PAGE_SIZE is greater-than-or-equal-to
2303 * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2.
2304 */
2305 if (PAGE_SIZE < UBIFS_BLOCK_SIZE) {
2306 pr_err("UBIFS error (pid %d): VFS page cache size is %u bytes, but UBIFS requires at least 4096 bytes",
2307 current->pid, (unsigned int)PAGE_SIZE);
2308 return -EINVAL;
2309 }
2310
2311 ubifs_inode_slab = kmem_cache_create("ubifs_inode_slab",
2312 sizeof(struct ubifs_inode), 0,
2313 SLAB_MEM_SPREAD | SLAB_RECLAIM_ACCOUNT |
2314 SLAB_ACCOUNT, &inode_slab_ctor);
2315 if (!ubifs_inode_slab)
2316 return -ENOMEM;
2317
2318 err = register_shrinker(&ubifs_shrinker_info);
2319 if (err)
2320 goto out_slab;
2321
2322 err = ubifs_compressors_init();
2323 if (err)
2324 goto out_shrinker;
2325
2326 err = dbg_debugfs_init();
2327 if (err)
2328 goto out_compr;
2329
2330 err = register_filesystem(&ubifs_fs_type);
2331 if (err) {
2332 pr_err("UBIFS error (pid %d): cannot register file system, error %d",
2333 current->pid, err);
2334 goto out_dbg;
2335 }
2336 return 0;
2337
2338out_dbg:
2339 dbg_debugfs_exit();
2340out_compr:
2341 ubifs_compressors_exit();
2342out_shrinker:
2343 unregister_shrinker(&ubifs_shrinker_info);
2344out_slab:
2345 kmem_cache_destroy(ubifs_inode_slab);
2346 return err;
2347}
2348/* late_initcall to let compressors initialize first */
2349late_initcall(ubifs_init);
2350
2351static void __exit ubifs_exit(void)
2352{
2353 WARN_ON(!list_empty(&ubifs_infos));
2354 WARN_ON(atomic_long_read(&ubifs_clean_zn_cnt) != 0);
2355
2356 dbg_debugfs_exit();
2357 ubifs_compressors_exit();
2358 unregister_shrinker(&ubifs_shrinker_info);
2359
2360 /*
2361 * Make sure all delayed rcu free inodes are flushed before we
2362 * destroy cache.
2363 */
2364 rcu_barrier();
2365 kmem_cache_destroy(ubifs_inode_slab);
2366 unregister_filesystem(&ubifs_fs_type);
2367}
2368module_exit(ubifs_exit);
2369
2370MODULE_LICENSE("GPL");
2371MODULE_VERSION(__stringify(UBIFS_VERSION));
2372MODULE_AUTHOR("Artem Bityutskiy, Adrian Hunter");
2373MODULE_DESCRIPTION("UBIFS - UBI File System");