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