blob: 3e7160352e6cccf24860773bf22afed4fbd88040 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese2fc10f62009-03-19 15:35:05 +01002/*
3 * This file is part of UBIFS.
4 *
5 * Copyright (C) 2006-2008 Nokia Corporation.
6 *
Stefan Roese2fc10f62009-03-19 15:35:05 +01007 * Authors: Artem Bityutskiy (Битюцкий Артём)
8 * Adrian Hunter
9 */
10
11/*
12 * This file implements UBIFS initialization and VFS superblock operations. Some
13 * initialization stuff which is rather large and complex is placed at
14 * corresponding subsystems, but most of it is here.
15 */
16
Heiko Schocherf5895d12014-06-24 10:10:04 +020017#ifndef __UBOOT__
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070019#include <dm/devres.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020020#include <linux/init.h>
21#include <linux/slab.h>
22#include <linux/module.h>
23#include <linux/ctype.h>
24#include <linux/kthread.h>
25#include <linux/parser.h>
26#include <linux/seq_file.h>
27#include <linux/mount.h>
Stefan Roese2fc10f62009-03-19 15:35:05 +010028#include <linux/math64.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020029#include <linux/writeback.h>
30#else
Stefan Roese2fc10f62009-03-19 15:35:05 +010031
Simon Glassa87fc0a2015-09-02 17:24:57 -060032#include <common.h>
33#include <malloc.h>
34#include <memalign.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060035#include <linux/bitops.h>
Masahiro Yamada78eeb912016-01-24 23:27:48 +090036#include <linux/bug.h>
Fabio Estevam0297d1e2015-11-05 12:43:39 -020037#include <linux/log2.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060038#include <linux/printk.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020039#include <linux/stat.h>
40#include <linux/err.h>
41#include "ubifs.h"
42#include <ubi_uboot.h>
Simon Glassfb64e362020-05-10 11:40:09 -060043#include <linux/stringify.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020044#include <mtd/ubi-user.h>
Stefan Roese2fc10f62009-03-19 15:35:05 +010045
Heiko Schocherf5895d12014-06-24 10:10:04 +020046struct dentry;
47struct file;
48struct iattr;
49struct kstat;
50struct vfsmount;
Stefan Roese2fc10f62009-03-19 15:35:05 +010051
Heiko Schocherf5895d12014-06-24 10:10:04 +020052#define INODE_LOCKED_MAX 64
Stefan Roese2fc10f62009-03-19 15:35:05 +010053
Heiko Schocherf5895d12014-06-24 10:10:04 +020054struct super_block *ubifs_sb;
Stefan Roese2fc10f62009-03-19 15:35:05 +010055
Heiko Schocherf5895d12014-06-24 10:10:04 +020056static struct inode *inodes_locked_down[INODE_LOCKED_MAX];
Stefan Roese2fc10f62009-03-19 15:35:05 +010057
Heiko Schocherf5895d12014-06-24 10:10:04 +020058int set_anon_super(struct super_block *s, void *data)
Stefan Roese2fc10f62009-03-19 15:35:05 +010059{
Stefan Roese2fc10f62009-03-19 15:35:05 +010060 return 0;
61}
62
Stefan Roese2fc10f62009-03-19 15:35:05 +010063struct inode *iget_locked(struct super_block *sb, unsigned long ino)
64{
65 struct inode *inode;
66
Marcel Ziswilerabc574b2015-08-18 13:06:37 +020067 inode = (struct inode *)malloc_cache_aligned(
68 sizeof(struct ubifs_inode));
Stefan Roese2fc10f62009-03-19 15:35:05 +010069 if (inode) {
70 inode->i_ino = ino;
71 inode->i_sb = sb;
72 list_add(&inode->i_sb_list, &sb->s_inodes);
73 inode->i_state = I_LOCK | I_NEW;
74 }
75
76 return inode;
77}
78
Heiko Schocherf5895d12014-06-24 10:10:04 +020079void iget_failed(struct inode *inode)
80{
81}
82
Stefan Roese2fc10f62009-03-19 15:35:05 +010083int ubifs_iput(struct inode *inode)
84{
85 list_del_init(&inode->i_sb_list);
86
87 free(inode);
88 return 0;
89}
90
91/*
92 * Lock (save) inode in inode array for readback after recovery
93 */
94void iput(struct inode *inode)
95{
96 int i;
97 struct inode *ino;
98
99 /*
100 * Search end of list
101 */
102 for (i = 0; i < INODE_LOCKED_MAX; i++) {
103 if (inodes_locked_down[i] == NULL)
104 break;
105 }
106
107 if (i >= INODE_LOCKED_MAX) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200108 dbg_gen("Error, can't lock (save) more inodes while recovery!!!");
Stefan Roese2fc10f62009-03-19 15:35:05 +0100109 return;
110 }
111
112 /*
113 * Allocate and use new inode
114 */
Marcel Ziswilerabc574b2015-08-18 13:06:37 +0200115 ino = (struct inode *)malloc_cache_aligned(sizeof(struct ubifs_inode));
Stefan Roese2fc10f62009-03-19 15:35:05 +0100116 memcpy(ino, inode, sizeof(struct ubifs_inode));
117
118 /*
119 * Finally save inode in array
120 */
121 inodes_locked_down[i] = ino;
122}
123
Heiko Schocherf5895d12014-06-24 10:10:04 +0200124/* from fs/inode.c */
125/**
126 * clear_nlink - directly zero an inode's link count
127 * @inode: inode
128 *
129 * This is a low-level filesystem helper to replace any
130 * direct filesystem manipulation of i_nlink. See
131 * drop_nlink() for why we care about i_nlink hitting zero.
132 */
133void clear_nlink(struct inode *inode)
134{
135 if (inode->i_nlink) {
136 inode->__i_nlink = 0;
137 atomic_long_inc(&inode->i_sb->s_remove_count);
138 }
139}
140EXPORT_SYMBOL(clear_nlink);
141
142/**
143 * set_nlink - directly set an inode's link count
144 * @inode: inode
145 * @nlink: new nlink (should be non-zero)
146 *
147 * This is a low-level filesystem helper to replace any
148 * direct filesystem manipulation of i_nlink.
149 */
150void set_nlink(struct inode *inode, unsigned int nlink)
151{
152 if (!nlink) {
153 clear_nlink(inode);
154 } else {
155 /* Yes, some filesystems do change nlink from zero to one */
156 if (inode->i_nlink == 0)
157 atomic_long_dec(&inode->i_sb->s_remove_count);
158
159 inode->__i_nlink = nlink;
160 }
161}
162EXPORT_SYMBOL(set_nlink);
163
164/* from include/linux/fs.h */
165static inline void i_uid_write(struct inode *inode, uid_t uid)
166{
167 inode->i_uid.val = uid;
168}
169
170static inline void i_gid_write(struct inode *inode, gid_t gid)
171{
172 inode->i_gid.val = gid;
173}
174
175void unlock_new_inode(struct inode *inode)
176{
177 return;
178}
179#endif
180
181/*
182 * Maximum amount of memory we may 'kmalloc()' without worrying that we are
183 * allocating too much.
184 */
185#define UBIFS_KMALLOC_OK (128*1024)
186
187/* Slab cache for UBIFS inodes */
188struct kmem_cache *ubifs_inode_slab;
189
190#ifndef __UBOOT__
191/* UBIFS TNC shrinker description */
192static struct shrinker ubifs_shrinker_info = {
193 .scan_objects = ubifs_shrink_scan,
194 .count_objects = ubifs_shrink_count,
195 .seeks = DEFAULT_SEEKS,
196};
197#endif
198
199/**
200 * validate_inode - validate inode.
201 * @c: UBIFS file-system description object
202 * @inode: the inode to validate
203 *
204 * This is a helper function for 'ubifs_iget()' which validates various fields
205 * of a newly built inode to make sure they contain sane values and prevent
206 * possible vulnerabilities. Returns zero if the inode is all right and
207 * a non-zero error code if not.
208 */
209static int validate_inode(struct ubifs_info *c, const struct inode *inode)
210{
211 int err;
212 const struct ubifs_inode *ui = ubifs_inode(inode);
213
214 if (inode->i_size > c->max_inode_sz) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200215 ubifs_err(c, "inode is too large (%lld)",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200216 (long long)inode->i_size);
217 return 1;
218 }
219
Heiko Schocher94b66de2015-10-22 06:19:21 +0200220 if (ui->compr_type >= UBIFS_COMPR_TYPES_CNT) {
221 ubifs_err(c, "unknown compression type %d", ui->compr_type);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200222 return 2;
223 }
224
225 if (ui->xattr_names + ui->xattr_cnt > XATTR_LIST_MAX)
226 return 3;
227
228 if (ui->data_len < 0 || ui->data_len > UBIFS_MAX_INO_DATA)
229 return 4;
230
231 if (ui->xattr && !S_ISREG(inode->i_mode))
232 return 5;
233
234 if (!ubifs_compr_present(ui->compr_type)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200235 ubifs_warn(c, "inode %lu uses '%s' compression, but it was not compiled in",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200236 inode->i_ino, ubifs_compr_name(ui->compr_type));
237 }
238
239 err = dbg_check_dir(c, inode);
240 return err;
241}
242
Stefan Roese2fc10f62009-03-19 15:35:05 +0100243struct inode *ubifs_iget(struct super_block *sb, unsigned long inum)
244{
245 int err;
246 union ubifs_key key;
247 struct ubifs_ino_node *ino;
248 struct ubifs_info *c = sb->s_fs_info;
249 struct inode *inode;
250 struct ubifs_inode *ui;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200251#ifdef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100252 int i;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200253#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100254
255 dbg_gen("inode %lu", inum);
256
Heiko Schocherf5895d12014-06-24 10:10:04 +0200257#ifdef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100258 /*
259 * U-Boot special handling of locked down inodes via recovery
260 * e.g. ubifs_recover_size()
261 */
262 for (i = 0; i < INODE_LOCKED_MAX; i++) {
263 /*
264 * Exit on last entry (NULL), inode not found in list
265 */
266 if (inodes_locked_down[i] == NULL)
267 break;
268
269 if (inodes_locked_down[i]->i_ino == inum) {
270 /*
271 * We found the locked down inode in our array,
272 * so just return this pointer instead of creating
273 * a new one.
274 */
275 return inodes_locked_down[i];
276 }
277 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200278#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100279
280 inode = iget_locked(sb, inum);
281 if (!inode)
282 return ERR_PTR(-ENOMEM);
283 if (!(inode->i_state & I_NEW))
284 return inode;
285 ui = ubifs_inode(inode);
286
287 ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
288 if (!ino) {
289 err = -ENOMEM;
290 goto out;
291 }
292
293 ino_key_init(c, &key, inode->i_ino);
294
295 err = ubifs_tnc_lookup(c, &key, ino);
296 if (err)
297 goto out_ino;
298
299 inode->i_flags |= (S_NOCMTIME | S_NOATIME);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200300 set_nlink(inode, le32_to_cpu(ino->nlink));
301 i_uid_write(inode, le32_to_cpu(ino->uid));
302 i_gid_write(inode, le32_to_cpu(ino->gid));
Stefan Roese2fc10f62009-03-19 15:35:05 +0100303 inode->i_atime.tv_sec = (int64_t)le64_to_cpu(ino->atime_sec);
304 inode->i_atime.tv_nsec = le32_to_cpu(ino->atime_nsec);
305 inode->i_mtime.tv_sec = (int64_t)le64_to_cpu(ino->mtime_sec);
306 inode->i_mtime.tv_nsec = le32_to_cpu(ino->mtime_nsec);
307 inode->i_ctime.tv_sec = (int64_t)le64_to_cpu(ino->ctime_sec);
308 inode->i_ctime.tv_nsec = le32_to_cpu(ino->ctime_nsec);
309 inode->i_mode = le32_to_cpu(ino->mode);
310 inode->i_size = le64_to_cpu(ino->size);
311
312 ui->data_len = le32_to_cpu(ino->data_len);
313 ui->flags = le32_to_cpu(ino->flags);
314 ui->compr_type = le16_to_cpu(ino->compr_type);
315 ui->creat_sqnum = le64_to_cpu(ino->creat_sqnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200316 ui->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
317 ui->xattr_size = le32_to_cpu(ino->xattr_size);
318 ui->xattr_names = le32_to_cpu(ino->xattr_names);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100319 ui->synced_i_size = ui->ui_size = inode->i_size;
320
Heiko Schocherf5895d12014-06-24 10:10:04 +0200321 ui->xattr = (ui->flags & UBIFS_XATTR_FL) ? 1 : 0;
322
Stefan Roese2fc10f62009-03-19 15:35:05 +0100323 err = validate_inode(c, inode);
324 if (err)
325 goto out_invalid;
326
Heiko Schocherf5895d12014-06-24 10:10:04 +0200327#ifndef __UBOOT__
Heiko Schocherf5895d12014-06-24 10:10:04 +0200328 switch (inode->i_mode & S_IFMT) {
329 case S_IFREG:
330 inode->i_mapping->a_ops = &ubifs_file_address_operations;
331 inode->i_op = &ubifs_file_inode_operations;
332 inode->i_fop = &ubifs_file_operations;
333 if (ui->xattr) {
334 ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
335 if (!ui->data) {
336 err = -ENOMEM;
337 goto out_ino;
338 }
339 memcpy(ui->data, ino->data, ui->data_len);
340 ((char *)ui->data)[ui->data_len] = '\0';
341 } else if (ui->data_len != 0) {
342 err = 10;
343 goto out_invalid;
344 }
345 break;
346 case S_IFDIR:
347 inode->i_op = &ubifs_dir_inode_operations;
348 inode->i_fop = &ubifs_dir_operations;
349 if (ui->data_len != 0) {
350 err = 11;
351 goto out_invalid;
352 }
353 break;
354 case S_IFLNK:
355 inode->i_op = &ubifs_symlink_inode_operations;
356 if (ui->data_len <= 0 || ui->data_len > UBIFS_MAX_INO_DATA) {
357 err = 12;
358 goto out_invalid;
359 }
360 ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
361 if (!ui->data) {
362 err = -ENOMEM;
363 goto out_ino;
364 }
365 memcpy(ui->data, ino->data, ui->data_len);
366 ((char *)ui->data)[ui->data_len] = '\0';
Heiko Schocher94b66de2015-10-22 06:19:21 +0200367 inode->i_link = ui->data;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200368 break;
369 case S_IFBLK:
370 case S_IFCHR:
371 {
372 dev_t rdev;
373 union ubifs_dev_desc *dev;
374
375 ui->data = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
376 if (!ui->data) {
377 err = -ENOMEM;
378 goto out_ino;
379 }
380
381 dev = (union ubifs_dev_desc *)ino->data;
382 if (ui->data_len == sizeof(dev->new))
383 rdev = new_decode_dev(le32_to_cpu(dev->new));
384 else if (ui->data_len == sizeof(dev->huge))
385 rdev = huge_decode_dev(le64_to_cpu(dev->huge));
386 else {
387 err = 13;
388 goto out_invalid;
389 }
390 memcpy(ui->data, ino->data, ui->data_len);
391 inode->i_op = &ubifs_file_inode_operations;
392 init_special_inode(inode, inode->i_mode, rdev);
393 break;
394 }
395 case S_IFSOCK:
396 case S_IFIFO:
397 inode->i_op = &ubifs_file_inode_operations;
398 init_special_inode(inode, inode->i_mode, 0);
399 if (ui->data_len != 0) {
400 err = 14;
401 goto out_invalid;
402 }
403 break;
404 default:
405 err = 15;
406 goto out_invalid;
407 }
408#else
Stefan Roese2fc10f62009-03-19 15:35:05 +0100409 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
410 if (ui->data_len <= 0 || ui->data_len > UBIFS_MAX_INO_DATA) {
411 err = 12;
412 goto out_invalid;
413 }
414 ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
415 if (!ui->data) {
416 err = -ENOMEM;
417 goto out_ino;
418 }
419 memcpy(ui->data, ino->data, ui->data_len);
420 ((char *)ui->data)[ui->data_len] = '\0';
421 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200422#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100423
424 kfree(ino);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200425#ifndef __UBOOT__
426 ubifs_set_inode_flags(inode);
427#endif
428 unlock_new_inode(inode);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100429 return inode;
430
431out_invalid:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200432 ubifs_err(c, "inode %lu validation failed, error %d", inode->i_ino, err);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200433 ubifs_dump_node(c, ino);
434 ubifs_dump_inode(c, inode);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100435 err = -EINVAL;
436out_ino:
437 kfree(ino);
438out:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200439 ubifs_err(c, "failed to read inode %lu, error %d", inode->i_ino, err);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200440 iget_failed(inode);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100441 return ERR_PTR(err);
442}
443
Heiko Schocherf5895d12014-06-24 10:10:04 +0200444static struct inode *ubifs_alloc_inode(struct super_block *sb)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100445{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200446 struct ubifs_inode *ui;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100447
Heiko Schocherf5895d12014-06-24 10:10:04 +0200448 ui = kmem_cache_alloc(ubifs_inode_slab, GFP_NOFS);
449 if (!ui)
450 return NULL;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100451
Heiko Schocherf5895d12014-06-24 10:10:04 +0200452 memset((void *)ui + sizeof(struct inode), 0,
453 sizeof(struct ubifs_inode) - sizeof(struct inode));
454 mutex_init(&ui->ui_mutex);
455 spin_lock_init(&ui->ui_lock);
456 return &ui->vfs_inode;
457};
Stefan Roese2fc10f62009-03-19 15:35:05 +0100458
Heiko Schocherf5895d12014-06-24 10:10:04 +0200459#ifndef __UBOOT__
460static void ubifs_i_callback(struct rcu_head *head)
461{
462 struct inode *inode = container_of(head, struct inode, i_rcu);
463 struct ubifs_inode *ui = ubifs_inode(inode);
464 kmem_cache_free(ubifs_inode_slab, ui);
465}
Stefan Roese2fc10f62009-03-19 15:35:05 +0100466
Heiko Schocherf5895d12014-06-24 10:10:04 +0200467static void ubifs_destroy_inode(struct inode *inode)
468{
469 struct ubifs_inode *ui = ubifs_inode(inode);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100470
Heiko Schocherf5895d12014-06-24 10:10:04 +0200471 kfree(ui->data);
472 call_rcu(&inode->i_rcu, ubifs_i_callback);
473}
Stefan Roese2fc10f62009-03-19 15:35:05 +0100474
Heiko Schocherf5895d12014-06-24 10:10:04 +0200475/*
476 * Note, Linux write-back code calls this without 'i_mutex'.
477 */
478static int ubifs_write_inode(struct inode *inode, struct writeback_control *wbc)
479{
480 int err = 0;
481 struct ubifs_info *c = inode->i_sb->s_fs_info;
482 struct ubifs_inode *ui = ubifs_inode(inode);
483
484 ubifs_assert(!ui->xattr);
485 if (is_bad_inode(inode))
486 return 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100487
Heiko Schocherf5895d12014-06-24 10:10:04 +0200488 mutex_lock(&ui->ui_mutex);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100489 /*
Heiko Schocherf5895d12014-06-24 10:10:04 +0200490 * Due to races between write-back forced by budgeting
491 * (see 'sync_some_inodes()') and background write-back, the inode may
492 * have already been synchronized, do not do this again. This might
493 * also happen if it was synchronized in an VFS operation, e.g.
494 * 'ubifs_link()'.
Stefan Roese2fc10f62009-03-19 15:35:05 +0100495 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200496 if (!ui->dirty) {
497 mutex_unlock(&ui->ui_mutex);
498 return 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100499 }
500
Stefan Roese2fc10f62009-03-19 15:35:05 +0100501 /*
Heiko Schocherf5895d12014-06-24 10:10:04 +0200502 * As an optimization, do not write orphan inodes to the media just
503 * because this is not needed.
Stefan Roese2fc10f62009-03-19 15:35:05 +0100504 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200505 dbg_gen("inode %lu, mode %#x, nlink %u",
506 inode->i_ino, (int)inode->i_mode, inode->i_nlink);
507 if (inode->i_nlink) {
508 err = ubifs_jnl_write_inode(c, inode);
509 if (err)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200510 ubifs_err(c, "can't write inode %lu, error %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200511 inode->i_ino, err);
512 else
513 err = dbg_check_inode_size(c, inode, ui->ui_size);
514 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100515
Heiko Schocherf5895d12014-06-24 10:10:04 +0200516 ui->dirty = 0;
517 mutex_unlock(&ui->ui_mutex);
518 ubifs_release_dirty_inode_budget(c, ui);
519 return err;
520}
521
522static void ubifs_evict_inode(struct inode *inode)
523{
524 int err;
525 struct ubifs_info *c = inode->i_sb->s_fs_info;
526 struct ubifs_inode *ui = ubifs_inode(inode);
527
528 if (ui->xattr)
529 /*
530 * Extended attribute inode deletions are fully handled in
531 * 'ubifs_removexattr()'. These inodes are special and have
532 * limited usage, so there is nothing to do here.
533 */
534 goto out;
535
536 dbg_gen("inode %lu, mode %#x", inode->i_ino, (int)inode->i_mode);
537 ubifs_assert(!atomic_read(&inode->i_count));
538
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200539 truncate_inode_pages_final(&inode->i_data);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200540
541 if (inode->i_nlink)
542 goto done;
543
544 if (is_bad_inode(inode))
545 goto out;
546
547 ui->ui_size = inode->i_size = 0;
548 err = ubifs_jnl_delete_inode(c, inode);
549 if (err)
550 /*
551 * Worst case we have a lost orphan inode wasting space, so a
552 * simple error message is OK here.
553 */
Heiko Schocher94b66de2015-10-22 06:19:21 +0200554 ubifs_err(c, "can't delete inode %lu, error %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200555 inode->i_ino, err);
556
557out:
558 if (ui->dirty)
559 ubifs_release_dirty_inode_budget(c, ui);
560 else {
561 /* We've deleted something - clean the "no space" flags */
562 c->bi.nospace = c->bi.nospace_rp = 0;
563 smp_wmb();
564 }
565done:
566 clear_inode(inode);
567}
568#endif
569
570static void ubifs_dirty_inode(struct inode *inode, int flags)
571{
572 struct ubifs_inode *ui = ubifs_inode(inode);
573
574 ubifs_assert(mutex_is_locked(&ui->ui_mutex));
575 if (!ui->dirty) {
576 ui->dirty = 1;
577 dbg_gen("inode %lu", inode->i_ino);
578 }
579}
580
581#ifndef __UBOOT__
582static int ubifs_statfs(struct dentry *dentry, struct kstatfs *buf)
583{
584 struct ubifs_info *c = dentry->d_sb->s_fs_info;
585 unsigned long long free;
586 __le32 *uuid = (__le32 *)c->uuid;
587
588 free = ubifs_get_free_space(c);
589 dbg_gen("free space %lld bytes (%lld blocks)",
590 free, free >> UBIFS_BLOCK_SHIFT);
591
592 buf->f_type = UBIFS_SUPER_MAGIC;
593 buf->f_bsize = UBIFS_BLOCK_SIZE;
594 buf->f_blocks = c->block_cnt;
595 buf->f_bfree = free >> UBIFS_BLOCK_SHIFT;
596 if (free > c->report_rp_size)
597 buf->f_bavail = (free - c->report_rp_size) >> UBIFS_BLOCK_SHIFT;
598 else
599 buf->f_bavail = 0;
600 buf->f_files = 0;
601 buf->f_ffree = 0;
602 buf->f_namelen = UBIFS_MAX_NLEN;
603 buf->f_fsid.val[0] = le32_to_cpu(uuid[0]) ^ le32_to_cpu(uuid[2]);
604 buf->f_fsid.val[1] = le32_to_cpu(uuid[1]) ^ le32_to_cpu(uuid[3]);
605 ubifs_assert(buf->f_bfree <= c->block_cnt);
606 return 0;
607}
608
609static int ubifs_show_options(struct seq_file *s, struct dentry *root)
610{
611 struct ubifs_info *c = root->d_sb->s_fs_info;
612
613 if (c->mount_opts.unmount_mode == 2)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200614 seq_puts(s, ",fast_unmount");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200615 else if (c->mount_opts.unmount_mode == 1)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200616 seq_puts(s, ",norm_unmount");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200617
618 if (c->mount_opts.bulk_read == 2)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200619 seq_puts(s, ",bulk_read");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200620 else if (c->mount_opts.bulk_read == 1)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200621 seq_puts(s, ",no_bulk_read");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200622
623 if (c->mount_opts.chk_data_crc == 2)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200624 seq_puts(s, ",chk_data_crc");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200625 else if (c->mount_opts.chk_data_crc == 1)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200626 seq_puts(s, ",no_chk_data_crc");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200627
628 if (c->mount_opts.override_compr) {
629 seq_printf(s, ",compr=%s",
630 ubifs_compr_name(c->mount_opts.compr_type));
631 }
632
633 return 0;
634}
635
636static int ubifs_sync_fs(struct super_block *sb, int wait)
637{
638 int i, err;
639 struct ubifs_info *c = sb->s_fs_info;
640
641 /*
642 * Zero @wait is just an advisory thing to help the file system shove
643 * lots of data into the queues, and there will be the second
644 * '->sync_fs()' call, with non-zero @wait.
645 */
646 if (!wait)
647 return 0;
648
649 /*
650 * Synchronize write buffers, because 'ubifs_run_commit()' does not
651 * do this if it waits for an already running commit.
652 */
653 for (i = 0; i < c->jhead_cnt; i++) {
654 err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
655 if (err)
656 return err;
657 }
658
659 /*
660 * Strictly speaking, it is not necessary to commit the journal here,
661 * synchronizing write-buffers would be enough. But committing makes
662 * UBIFS free space predictions much more accurate, so we want to let
663 * the user be able to get more accurate results of 'statfs()' after
664 * they synchronize the file system.
665 */
666 err = ubifs_run_commit(c);
667 if (err)
668 return err;
669
670 return ubi_sync(c->vi.ubi_num);
671}
672#endif
673
674/**
675 * init_constants_early - initialize UBIFS constants.
676 * @c: UBIFS file-system description object
677 *
678 * This function initialize UBIFS constants which do not need the superblock to
679 * be read. It also checks that the UBI volume satisfies basic UBIFS
680 * requirements. Returns zero in case of success and a negative error code in
681 * case of failure.
682 */
683static int init_constants_early(struct ubifs_info *c)
684{
685 if (c->vi.corrupted) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200686 ubifs_warn(c, "UBI volume is corrupted - read-only mode");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200687 c->ro_media = 1;
688 }
689
690 if (c->di.ro_mode) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200691 ubifs_msg(c, "read-only UBI device");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200692 c->ro_media = 1;
693 }
694
695 if (c->vi.vol_type == UBI_STATIC_VOLUME) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200696 ubifs_msg(c, "static UBI volume - read-only mode");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200697 c->ro_media = 1;
698 }
699
700 c->leb_cnt = c->vi.size;
701 c->leb_size = c->vi.usable_leb_size;
702 c->leb_start = c->di.leb_start;
703 c->half_leb_size = c->leb_size / 2;
704 c->min_io_size = c->di.min_io_size;
705 c->min_io_shift = fls(c->min_io_size) - 1;
706 c->max_write_size = c->di.max_write_size;
707 c->max_write_shift = fls(c->max_write_size) - 1;
708
709 if (c->leb_size < UBIFS_MIN_LEB_SZ) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200710 ubifs_err(c, "too small LEBs (%d bytes), min. is %d bytes",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200711 c->leb_size, UBIFS_MIN_LEB_SZ);
712 return -EINVAL;
713 }
714
715 if (c->leb_cnt < UBIFS_MIN_LEB_CNT) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200716 ubifs_err(c, "too few LEBs (%d), min. is %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200717 c->leb_cnt, UBIFS_MIN_LEB_CNT);
718 return -EINVAL;
719 }
720
721 if (!is_power_of_2(c->min_io_size)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200722 ubifs_err(c, "bad min. I/O size %d", c->min_io_size);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200723 return -EINVAL;
724 }
725
726 /*
727 * Maximum write size has to be greater or equivalent to min. I/O
728 * size, and be multiple of min. I/O size.
729 */
730 if (c->max_write_size < c->min_io_size ||
731 c->max_write_size % c->min_io_size ||
732 !is_power_of_2(c->max_write_size)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200733 ubifs_err(c, "bad write buffer size %d for %d min. I/O unit",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200734 c->max_write_size, c->min_io_size);
735 return -EINVAL;
736 }
737
738 /*
739 * UBIFS aligns all node to 8-byte boundary, so to make function in
740 * io.c simpler, assume minimum I/O unit size to be 8 bytes if it is
741 * less than 8.
742 */
743 if (c->min_io_size < 8) {
744 c->min_io_size = 8;
745 c->min_io_shift = 3;
746 if (c->max_write_size < c->min_io_size) {
747 c->max_write_size = c->min_io_size;
748 c->max_write_shift = c->min_io_shift;
749 }
750 }
751
752 c->ref_node_alsz = ALIGN(UBIFS_REF_NODE_SZ, c->min_io_size);
753 c->mst_node_alsz = ALIGN(UBIFS_MST_NODE_SZ, c->min_io_size);
754
755 /*
756 * Initialize node length ranges which are mostly needed for node
757 * length validation.
758 */
759 c->ranges[UBIFS_PAD_NODE].len = UBIFS_PAD_NODE_SZ;
760 c->ranges[UBIFS_SB_NODE].len = UBIFS_SB_NODE_SZ;
761 c->ranges[UBIFS_MST_NODE].len = UBIFS_MST_NODE_SZ;
762 c->ranges[UBIFS_REF_NODE].len = UBIFS_REF_NODE_SZ;
763 c->ranges[UBIFS_TRUN_NODE].len = UBIFS_TRUN_NODE_SZ;
764 c->ranges[UBIFS_CS_NODE].len = UBIFS_CS_NODE_SZ;
765
766 c->ranges[UBIFS_INO_NODE].min_len = UBIFS_INO_NODE_SZ;
767 c->ranges[UBIFS_INO_NODE].max_len = UBIFS_MAX_INO_NODE_SZ;
768 c->ranges[UBIFS_ORPH_NODE].min_len =
Stefan Roese2fc10f62009-03-19 15:35:05 +0100769 UBIFS_ORPH_NODE_SZ + sizeof(__le64);
770 c->ranges[UBIFS_ORPH_NODE].max_len = c->leb_size;
771 c->ranges[UBIFS_DENT_NODE].min_len = UBIFS_DENT_NODE_SZ;
772 c->ranges[UBIFS_DENT_NODE].max_len = UBIFS_MAX_DENT_NODE_SZ;
773 c->ranges[UBIFS_XENT_NODE].min_len = UBIFS_XENT_NODE_SZ;
774 c->ranges[UBIFS_XENT_NODE].max_len = UBIFS_MAX_XENT_NODE_SZ;
775 c->ranges[UBIFS_DATA_NODE].min_len = UBIFS_DATA_NODE_SZ;
776 c->ranges[UBIFS_DATA_NODE].max_len = UBIFS_MAX_DATA_NODE_SZ;
777 /*
778 * Minimum indexing node size is amended later when superblock is
779 * read and the key length is known.
780 */
781 c->ranges[UBIFS_IDX_NODE].min_len = UBIFS_IDX_NODE_SZ + UBIFS_BRANCH_SZ;
782 /*
783 * Maximum indexing node size is amended later when superblock is
784 * read and the fanout is known.
785 */
786 c->ranges[UBIFS_IDX_NODE].max_len = INT_MAX;
787
788 /*
789 * Initialize dead and dark LEB space watermarks. See gc.c for comments
790 * about these values.
791 */
792 c->dead_wm = ALIGN(MIN_WRITE_SZ, c->min_io_size);
793 c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size);
794
795 /*
796 * Calculate how many bytes would be wasted at the end of LEB if it was
797 * fully filled with data nodes of maximum size. This is used in
798 * calculations when reporting free space.
799 */
800 c->leb_overhead = c->leb_size % UBIFS_MAX_DATA_NODE_SZ;
801
Heiko Schocherf5895d12014-06-24 10:10:04 +0200802 /* Buffer size for bulk-reads */
803 c->max_bu_buf_len = UBIFS_MAX_BULK_READ * UBIFS_MAX_DATA_NODE_SZ;
804 if (c->max_bu_buf_len > c->leb_size)
805 c->max_bu_buf_len = c->leb_size;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100806 return 0;
807}
808
Heiko Schocherf5895d12014-06-24 10:10:04 +0200809/**
810 * bud_wbuf_callback - bud LEB write-buffer synchronization call-back.
811 * @c: UBIFS file-system description object
812 * @lnum: LEB the write-buffer was synchronized to
813 * @free: how many free bytes left in this LEB
814 * @pad: how many bytes were padded
815 *
816 * This is a callback function which is called by the I/O unit when the
817 * write-buffer is synchronized. We need this to correctly maintain space
818 * accounting in bud logical eraseblocks. This function returns zero in case of
819 * success and a negative error code in case of failure.
820 *
821 * This function actually belongs to the journal, but we keep it here because
822 * we want to keep it static.
823 */
824static int bud_wbuf_callback(struct ubifs_info *c, int lnum, int free, int pad)
825{
826 return ubifs_update_one_lp(c, lnum, free, pad, 0, 0);
827}
828
Stefan Roese2fc10f62009-03-19 15:35:05 +0100829/*
830 * init_constants_sb - initialize UBIFS constants.
831 * @c: UBIFS file-system description object
832 *
833 * This is a helper function which initializes various UBIFS constants after
834 * the superblock has been read. It also checks various UBIFS parameters and
835 * makes sure they are all right. Returns zero in case of success and a
836 * negative error code in case of failure.
837 */
838static int init_constants_sb(struct ubifs_info *c)
839{
840 int tmp, err;
841 long long tmp64;
842
843 c->main_bytes = (long long)c->main_lebs * c->leb_size;
844 c->max_znode_sz = sizeof(struct ubifs_znode) +
845 c->fanout * sizeof(struct ubifs_zbranch);
846
847 tmp = ubifs_idx_node_sz(c, 1);
848 c->ranges[UBIFS_IDX_NODE].min_len = tmp;
849 c->min_idx_node_sz = ALIGN(tmp, 8);
850
851 tmp = ubifs_idx_node_sz(c, c->fanout);
852 c->ranges[UBIFS_IDX_NODE].max_len = tmp;
853 c->max_idx_node_sz = ALIGN(tmp, 8);
854
855 /* Make sure LEB size is large enough to fit full commit */
856 tmp = UBIFS_CS_NODE_SZ + UBIFS_REF_NODE_SZ * c->jhead_cnt;
857 tmp = ALIGN(tmp, c->min_io_size);
858 if (tmp > c->leb_size) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200859 ubifs_err(c, "too small LEB size %d, at least %d needed",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200860 c->leb_size, tmp);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100861 return -EINVAL;
862 }
863
864 /*
865 * Make sure that the log is large enough to fit reference nodes for
866 * all buds plus one reserved LEB.
867 */
868 tmp64 = c->max_bud_bytes + c->leb_size - 1;
869 c->max_bud_cnt = div_u64(tmp64, c->leb_size);
870 tmp = (c->ref_node_alsz * c->max_bud_cnt + c->leb_size - 1);
871 tmp /= c->leb_size;
872 tmp += 1;
873 if (c->log_lebs < tmp) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200874 ubifs_err(c, "too small log %d LEBs, required min. %d LEBs",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200875 c->log_lebs, tmp);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100876 return -EINVAL;
877 }
878
879 /*
880 * When budgeting we assume worst-case scenarios when the pages are not
881 * be compressed and direntries are of the maximum size.
882 *
883 * Note, data, which may be stored in inodes is budgeted separately, so
Heiko Schocherf5895d12014-06-24 10:10:04 +0200884 * it is not included into 'c->bi.inode_budget'.
Stefan Roese2fc10f62009-03-19 15:35:05 +0100885 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200886 c->bi.page_budget = UBIFS_MAX_DATA_NODE_SZ * UBIFS_BLOCKS_PER_PAGE;
887 c->bi.inode_budget = UBIFS_INO_NODE_SZ;
888 c->bi.dent_budget = UBIFS_MAX_DENT_NODE_SZ;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100889
890 /*
891 * When the amount of flash space used by buds becomes
892 * 'c->max_bud_bytes', UBIFS just blocks all writers and starts commit.
893 * The writers are unblocked when the commit is finished. To avoid
894 * writers to be blocked UBIFS initiates background commit in advance,
895 * when number of bud bytes becomes above the limit defined below.
896 */
897 c->bg_bud_bytes = (c->max_bud_bytes * 13) >> 4;
898
899 /*
900 * Ensure minimum journal size. All the bytes in the journal heads are
901 * considered to be used, when calculating the current journal usage.
902 * Consequently, if the journal is too small, UBIFS will treat it as
903 * always full.
904 */
905 tmp64 = (long long)(c->jhead_cnt + 1) * c->leb_size + 1;
906 if (c->bg_bud_bytes < tmp64)
907 c->bg_bud_bytes = tmp64;
908 if (c->max_bud_bytes < tmp64 + c->leb_size)
909 c->max_bud_bytes = tmp64 + c->leb_size;
910
911 err = ubifs_calc_lpt_geom(c);
912 if (err)
913 return err;
914
Heiko Schocherf5895d12014-06-24 10:10:04 +0200915 /* Initialize effective LEB size used in budgeting calculations */
916 c->idx_leb_size = c->leb_size - c->max_idx_node_sz;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100917 return 0;
918}
919
920/*
921 * init_constants_master - initialize UBIFS constants.
922 * @c: UBIFS file-system description object
923 *
924 * This is a helper function which initializes various UBIFS constants after
925 * the master node has been read. It also checks various UBIFS parameters and
926 * makes sure they are all right.
927 */
928static void init_constants_master(struct ubifs_info *c)
929{
930 long long tmp64;
931
Heiko Schocherf5895d12014-06-24 10:10:04 +0200932 c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
933 c->report_rp_size = ubifs_reported_space(c, c->rp_size);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100934
935 /*
936 * Calculate total amount of FS blocks. This number is not used
937 * internally because it does not make much sense for UBIFS, but it is
938 * necessary to report something for the 'statfs()' call.
939 *
940 * Subtract the LEB reserved for GC, the LEB which is reserved for
941 * deletions, minimum LEBs for the index, and assume only one journal
942 * head is available.
943 */
944 tmp64 = c->main_lebs - 1 - 1 - MIN_INDEX_LEBS - c->jhead_cnt + 1;
945 tmp64 *= (long long)c->leb_size - c->leb_overhead;
946 tmp64 = ubifs_reported_space(c, tmp64);
947 c->block_cnt = tmp64 >> UBIFS_BLOCK_SHIFT;
948}
949
950/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200951 * take_gc_lnum - reserve GC LEB.
952 * @c: UBIFS file-system description object
953 *
954 * This function ensures that the LEB reserved for garbage collection is marked
955 * as "taken" in lprops. We also have to set free space to LEB size and dirty
956 * space to zero, because lprops may contain out-of-date information if the
957 * file-system was un-mounted before it has been committed. This function
958 * returns zero in case of success and a negative error code in case of
959 * failure.
960 */
961static int take_gc_lnum(struct ubifs_info *c)
962{
963 int err;
964
965 if (c->gc_lnum == -1) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200966 ubifs_err(c, "no LEB for GC");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200967 return -EINVAL;
968 }
969
970 /* And we have to tell lprops that this LEB is taken */
971 err = ubifs_change_one_lp(c, c->gc_lnum, c->leb_size, 0,
972 LPROPS_TAKEN, 0, 0);
973 return err;
974}
975
976/**
977 * alloc_wbufs - allocate write-buffers.
978 * @c: UBIFS file-system description object
979 *
980 * This helper function allocates and initializes UBIFS write-buffers. Returns
981 * zero in case of success and %-ENOMEM in case of failure.
982 */
983static int alloc_wbufs(struct ubifs_info *c)
984{
985 int i, err;
986
Heiko Schocher94b66de2015-10-22 06:19:21 +0200987 c->jheads = kcalloc(c->jhead_cnt, sizeof(struct ubifs_jhead),
988 GFP_KERNEL);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200989 if (!c->jheads)
990 return -ENOMEM;
991
992 /* Initialize journal heads */
993 for (i = 0; i < c->jhead_cnt; i++) {
994 INIT_LIST_HEAD(&c->jheads[i].buds_list);
995 err = ubifs_wbuf_init(c, &c->jheads[i].wbuf);
996 if (err)
997 return err;
998
999 c->jheads[i].wbuf.sync_callback = &bud_wbuf_callback;
1000 c->jheads[i].wbuf.jhead = i;
1001 c->jheads[i].grouped = 1;
1002 }
1003
1004 /*
1005 * Garbage Collector head does not need to be synchronized by timer.
1006 * Also GC head nodes are not grouped.
1007 */
1008 c->jheads[GCHD].wbuf.no_timer = 1;
1009 c->jheads[GCHD].grouped = 0;
1010
1011 return 0;
1012}
1013
1014/**
1015 * free_wbufs - free write-buffers.
1016 * @c: UBIFS file-system description object
1017 */
1018static void free_wbufs(struct ubifs_info *c)
1019{
1020 int i;
1021
1022 if (c->jheads) {
1023 for (i = 0; i < c->jhead_cnt; i++) {
1024 kfree(c->jheads[i].wbuf.buf);
1025 kfree(c->jheads[i].wbuf.inodes);
1026 }
1027 kfree(c->jheads);
1028 c->jheads = NULL;
1029 }
1030}
1031
1032/**
Stefan Roese2fc10f62009-03-19 15:35:05 +01001033 * free_orphans - free orphans.
1034 * @c: UBIFS file-system description object
1035 */
1036static void free_orphans(struct ubifs_info *c)
1037{
1038 struct ubifs_orphan *orph;
1039
1040 while (c->orph_dnext) {
1041 orph = c->orph_dnext;
1042 c->orph_dnext = orph->dnext;
1043 list_del(&orph->list);
1044 kfree(orph);
1045 }
1046
1047 while (!list_empty(&c->orph_list)) {
1048 orph = list_entry(c->orph_list.next, struct ubifs_orphan, list);
1049 list_del(&orph->list);
1050 kfree(orph);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001051 ubifs_err(c, "orphan list not empty at unmount");
Stefan Roese2fc10f62009-03-19 15:35:05 +01001052 }
1053
1054 vfree(c->orph_buf);
1055 c->orph_buf = NULL;
1056}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001057
Heiko Schocherf5895d12014-06-24 10:10:04 +02001058/**
1059 * free_buds - free per-bud objects.
1060 * @c: UBIFS file-system description object
1061 */
1062static void free_buds(struct ubifs_info *c)
1063{
1064 struct ubifs_bud *bud, *n;
1065
1066 rbtree_postorder_for_each_entry_safe(bud, n, &c->buds, rb)
1067 kfree(bud);
1068}
Stefan Roese2fc10f62009-03-19 15:35:05 +01001069
1070/**
1071 * check_volume_empty - check if the UBI volume is empty.
1072 * @c: UBIFS file-system description object
1073 *
1074 * This function checks if the UBIFS volume is empty by looking if its LEBs are
1075 * mapped or not. The result of checking is stored in the @c->empty variable.
1076 * Returns zero in case of success and a negative error code in case of
1077 * failure.
1078 */
1079static int check_volume_empty(struct ubifs_info *c)
1080{
1081 int lnum, err;
1082
1083 c->empty = 1;
1084 for (lnum = 0; lnum < c->leb_cnt; lnum++) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02001085 err = ubifs_is_mapped(c, lnum);
Stefan Roese2fc10f62009-03-19 15:35:05 +01001086 if (unlikely(err < 0))
1087 return err;
1088 if (err == 1) {
1089 c->empty = 0;
1090 break;
1091 }
1092
1093 cond_resched();
1094 }
1095
1096 return 0;
1097}
1098
Heiko Schocherf5895d12014-06-24 10:10:04 +02001099/*
1100 * UBIFS mount options.
1101 *
1102 * Opt_fast_unmount: do not run a journal commit before un-mounting
1103 * Opt_norm_unmount: run a journal commit before un-mounting
1104 * Opt_bulk_read: enable bulk-reads
1105 * Opt_no_bulk_read: disable bulk-reads
1106 * Opt_chk_data_crc: check CRCs when reading data nodes
1107 * Opt_no_chk_data_crc: do not check CRCs when reading data nodes
1108 * Opt_override_compr: override default compressor
1109 * Opt_err: just end of array marker
1110 */
1111enum {
1112 Opt_fast_unmount,
1113 Opt_norm_unmount,
1114 Opt_bulk_read,
1115 Opt_no_bulk_read,
1116 Opt_chk_data_crc,
1117 Opt_no_chk_data_crc,
1118 Opt_override_compr,
1119 Opt_err,
1120};
1121
1122#ifndef __UBOOT__
1123static const match_table_t tokens = {
1124 {Opt_fast_unmount, "fast_unmount"},
1125 {Opt_norm_unmount, "norm_unmount"},
1126 {Opt_bulk_read, "bulk_read"},
1127 {Opt_no_bulk_read, "no_bulk_read"},
1128 {Opt_chk_data_crc, "chk_data_crc"},
1129 {Opt_no_chk_data_crc, "no_chk_data_crc"},
1130 {Opt_override_compr, "compr=%s"},
1131 {Opt_err, NULL},
1132};
1133
1134/**
1135 * parse_standard_option - parse a standard mount option.
1136 * @option: the option to parse
1137 *
1138 * Normally, standard mount options like "sync" are passed to file-systems as
1139 * flags. However, when a "rootflags=" kernel boot parameter is used, they may
1140 * be present in the options string. This function tries to deal with this
1141 * situation and parse standard options. Returns 0 if the option was not
1142 * recognized, and the corresponding integer flag if it was.
1143 *
1144 * UBIFS is only interested in the "sync" option, so do not check for anything
1145 * else.
1146 */
1147static int parse_standard_option(const char *option)
1148{
Heiko Schocher94b66de2015-10-22 06:19:21 +02001149
1150 pr_notice("UBIFS: parse %s\n", option);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001151 if (!strcmp(option, "sync"))
1152 return MS_SYNCHRONOUS;
1153 return 0;
1154}
1155
1156/**
1157 * ubifs_parse_options - parse mount parameters.
1158 * @c: UBIFS file-system description object
1159 * @options: parameters to parse
1160 * @is_remount: non-zero if this is FS re-mount
1161 *
1162 * This function parses UBIFS mount options and returns zero in case success
1163 * and a negative error code in case of failure.
1164 */
1165static int ubifs_parse_options(struct ubifs_info *c, char *options,
1166 int is_remount)
1167{
1168 char *p;
1169 substring_t args[MAX_OPT_ARGS];
1170
1171 if (!options)
1172 return 0;
1173
1174 while ((p = strsep(&options, ","))) {
1175 int token;
1176
1177 if (!*p)
1178 continue;
1179
1180 token = match_token(p, tokens, args);
1181 switch (token) {
1182 /*
1183 * %Opt_fast_unmount and %Opt_norm_unmount options are ignored.
1184 * We accept them in order to be backward-compatible. But this
1185 * should be removed at some point.
1186 */
1187 case Opt_fast_unmount:
1188 c->mount_opts.unmount_mode = 2;
1189 break;
1190 case Opt_norm_unmount:
1191 c->mount_opts.unmount_mode = 1;
1192 break;
1193 case Opt_bulk_read:
1194 c->mount_opts.bulk_read = 2;
1195 c->bulk_read = 1;
1196 break;
1197 case Opt_no_bulk_read:
1198 c->mount_opts.bulk_read = 1;
1199 c->bulk_read = 0;
1200 break;
1201 case Opt_chk_data_crc:
1202 c->mount_opts.chk_data_crc = 2;
1203 c->no_chk_data_crc = 0;
1204 break;
1205 case Opt_no_chk_data_crc:
1206 c->mount_opts.chk_data_crc = 1;
1207 c->no_chk_data_crc = 1;
1208 break;
1209 case Opt_override_compr:
1210 {
1211 char *name = match_strdup(&args[0]);
1212
1213 if (!name)
1214 return -ENOMEM;
1215 if (!strcmp(name, "none"))
1216 c->mount_opts.compr_type = UBIFS_COMPR_NONE;
1217 else if (!strcmp(name, "lzo"))
1218 c->mount_opts.compr_type = UBIFS_COMPR_LZO;
1219 else if (!strcmp(name, "zlib"))
1220 c->mount_opts.compr_type = UBIFS_COMPR_ZLIB;
1221 else {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001222 ubifs_err(c, "unknown compressor \"%s\"", name); //FIXME: is c ready?
Heiko Schocherf5895d12014-06-24 10:10:04 +02001223 kfree(name);
1224 return -EINVAL;
1225 }
1226 kfree(name);
1227 c->mount_opts.override_compr = 1;
1228 c->default_compr = c->mount_opts.compr_type;
1229 break;
1230 }
1231 default:
1232 {
1233 unsigned long flag;
1234 struct super_block *sb = c->vfs_sb;
1235
1236 flag = parse_standard_option(p);
1237 if (!flag) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001238 ubifs_err(c, "unrecognized mount option \"%s\" or missing value",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001239 p);
1240 return -EINVAL;
1241 }
1242 sb->s_flags |= flag;
1243 break;
1244 }
1245 }
1246 }
1247
1248 return 0;
1249}
Anton Habegger7c470312015-01-22 22:29:11 +01001250#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +02001251
Stefan Roese2fc10f62009-03-19 15:35:05 +01001252/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001253 * destroy_journal - destroy journal data structures.
1254 * @c: UBIFS file-system description object
1255 *
1256 * This function destroys journal data structures including those that may have
1257 * been created by recovery functions.
1258 */
1259static void destroy_journal(struct ubifs_info *c)
1260{
1261 while (!list_empty(&c->unclean_leb_list)) {
1262 struct ubifs_unclean_leb *ucleb;
1263
1264 ucleb = list_entry(c->unclean_leb_list.next,
1265 struct ubifs_unclean_leb, list);
1266 list_del(&ucleb->list);
1267 kfree(ucleb);
1268 }
1269 while (!list_empty(&c->old_buds)) {
1270 struct ubifs_bud *bud;
1271
1272 bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
1273 list_del(&bud->list);
1274 kfree(bud);
1275 }
1276 ubifs_destroy_idx_gc(c);
1277 ubifs_destroy_size_tree(c);
1278 ubifs_tnc_close(c);
1279 free_buds(c);
1280}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001281
1282/**
1283 * bu_init - initialize bulk-read information.
1284 * @c: UBIFS file-system description object
1285 */
1286static void bu_init(struct ubifs_info *c)
1287{
1288 ubifs_assert(c->bulk_read == 1);
1289
1290 if (c->bu.buf)
1291 return; /* Already initialized */
1292
1293again:
1294 c->bu.buf = kmalloc(c->max_bu_buf_len, GFP_KERNEL | __GFP_NOWARN);
1295 if (!c->bu.buf) {
1296 if (c->max_bu_buf_len > UBIFS_KMALLOC_OK) {
1297 c->max_bu_buf_len = UBIFS_KMALLOC_OK;
1298 goto again;
1299 }
1300
1301 /* Just disable bulk-read */
Heiko Schocher94b66de2015-10-22 06:19:21 +02001302 ubifs_warn(c, "cannot allocate %d bytes of memory for bulk-read, disabling it",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001303 c->max_bu_buf_len);
1304 c->mount_opts.bulk_read = 1;
1305 c->bulk_read = 0;
1306 return;
1307 }
1308}
1309
1310#ifndef __UBOOT__
1311/**
1312 * check_free_space - check if there is enough free space to mount.
1313 * @c: UBIFS file-system description object
1314 *
1315 * This function makes sure UBIFS has enough free space to be mounted in
1316 * read/write mode. UBIFS must always have some free space to allow deletions.
1317 */
1318static int check_free_space(struct ubifs_info *c)
1319{
1320 ubifs_assert(c->dark_wm > 0);
1321 if (c->lst.total_free + c->lst.total_dirty < c->dark_wm) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001322 ubifs_err(c, "insufficient free space to mount in R/W mode");
Heiko Schocherf5895d12014-06-24 10:10:04 +02001323 ubifs_dump_budg(c, &c->bi);
1324 ubifs_dump_lprops(c);
1325 return -ENOSPC;
1326 }
1327 return 0;
1328}
1329#endif
1330
1331/**
Stefan Roese2fc10f62009-03-19 15:35:05 +01001332 * mount_ubifs - mount UBIFS file-system.
1333 * @c: UBIFS file-system description object
1334 *
1335 * This function mounts UBIFS file system. Returns zero in case of success and
1336 * a negative error code in case of failure.
Stefan Roese2fc10f62009-03-19 15:35:05 +01001337 */
1338static int mount_ubifs(struct ubifs_info *c)
1339{
Heiko Schocherf5895d12014-06-24 10:10:04 +02001340 int err;
Petr Vorel05239e82018-03-24 01:49:23 +01001341 long long x;
1342#ifndef CONFIG_UBIFS_SILENCE_MSG
1343 long long y;
1344#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +01001345 size_t sz;
1346
Heiko Schocherf5895d12014-06-24 10:10:04 +02001347 c->ro_mount = !!(c->vfs_sb->s_flags & MS_RDONLY);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001348 /* Suppress error messages while probing if MS_SILENT is set */
1349 c->probing = !!(c->vfs_sb->s_flags & MS_SILENT);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001350#ifdef __UBOOT__
1351 if (!c->ro_mount) {
1352 printf("UBIFS: only ro mode in U-Boot allowed.\n");
1353 return -EACCES;
1354 }
1355#endif
1356
Stefan Roese2fc10f62009-03-19 15:35:05 +01001357 err = init_constants_early(c);
1358 if (err)
1359 return err;
1360
1361 err = ubifs_debugging_init(c);
1362 if (err)
1363 return err;
1364
1365 err = check_volume_empty(c);
1366 if (err)
1367 goto out_free;
1368
Heiko Schocherf5895d12014-06-24 10:10:04 +02001369 if (c->empty && (c->ro_mount || c->ro_media)) {
Stefan Roese2fc10f62009-03-19 15:35:05 +01001370 /*
1371 * This UBI volume is empty, and read-only, or the file system
1372 * is mounted read-only - we cannot format it.
1373 */
Heiko Schocher94b66de2015-10-22 06:19:21 +02001374 ubifs_err(c, "can't format empty UBI volume: read-only %s",
Stefan Roese2fc10f62009-03-19 15:35:05 +01001375 c->ro_media ? "UBI volume" : "mount");
1376 err = -EROFS;
1377 goto out_free;
1378 }
1379
Heiko Schocherf5895d12014-06-24 10:10:04 +02001380 if (c->ro_media && !c->ro_mount) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001381 ubifs_err(c, "cannot mount read-write - read-only media");
Stefan Roese2fc10f62009-03-19 15:35:05 +01001382 err = -EROFS;
1383 goto out_free;
1384 }
1385
1386 /*
1387 * The requirement for the buffer is that it should fit indexing B-tree
1388 * height amount of integers. We assume the height if the TNC tree will
1389 * never exceed 64.
1390 */
1391 err = -ENOMEM;
1392 c->bottom_up_buf = kmalloc(BOTTOM_UP_HEIGHT * sizeof(int), GFP_KERNEL);
1393 if (!c->bottom_up_buf)
1394 goto out_free;
1395
1396 c->sbuf = vmalloc(c->leb_size);
1397 if (!c->sbuf)
1398 goto out_free;
1399
Heiko Schocherf5895d12014-06-24 10:10:04 +02001400#ifndef __UBOOT__
1401 if (!c->ro_mount) {
1402 c->ileb_buf = vmalloc(c->leb_size);
1403 if (!c->ileb_buf)
1404 goto out_free;
1405 }
1406#endif
1407
1408 if (c->bulk_read == 1)
1409 bu_init(c);
1410
1411#ifndef __UBOOT__
1412 if (!c->ro_mount) {
1413 c->write_reserve_buf = kmalloc(COMPRESSED_DATA_NODE_BUF_SZ,
1414 GFP_KERNEL);
1415 if (!c->write_reserve_buf)
1416 goto out_free;
1417 }
1418#endif
1419
1420 c->mounting = 1;
Stefan Roese2fc10f62009-03-19 15:35:05 +01001421
1422 err = ubifs_read_superblock(c);
1423 if (err)
1424 goto out_free;
1425
Heiko Schocher94b66de2015-10-22 06:19:21 +02001426 c->probing = 0;
1427
Stefan Roese2fc10f62009-03-19 15:35:05 +01001428 /*
1429 * Make sure the compressor which is set as default in the superblock
1430 * or overridden by mount options is actually compiled in.
1431 */
1432 if (!ubifs_compr_present(c->default_compr)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001433 ubifs_err(c, "'compressor \"%s\" is not compiled in",
Stefan Roese2fc10f62009-03-19 15:35:05 +01001434 ubifs_compr_name(c->default_compr));
Heiko Schocherf5895d12014-06-24 10:10:04 +02001435 err = -ENOTSUPP;
Stefan Roese2fc10f62009-03-19 15:35:05 +01001436 goto out_free;
1437 }
1438
Stefan Roese2fc10f62009-03-19 15:35:05 +01001439 err = init_constants_sb(c);
1440 if (err)
1441 goto out_free;
1442
1443 sz = ALIGN(c->max_idx_node_sz, c->min_io_size);
1444 sz = ALIGN(sz + c->max_idx_node_sz, c->min_io_size);
1445 c->cbuf = kmalloc(sz, GFP_NOFS);
1446 if (!c->cbuf) {
1447 err = -ENOMEM;
1448 goto out_free;
1449 }
1450
Heiko Schocherf5895d12014-06-24 10:10:04 +02001451 err = alloc_wbufs(c);
1452 if (err)
1453 goto out_cbuf;
1454
Stefan Roese2fc10f62009-03-19 15:35:05 +01001455 sprintf(c->bgt_name, BGT_NAME_PATTERN, c->vi.ubi_num, c->vi.vol_id);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001456#ifndef __UBOOT__
1457 if (!c->ro_mount) {
1458 /* Create background thread */
1459 c->bgt = kthread_create(ubifs_bg_thread, c, "%s", c->bgt_name);
1460 if (IS_ERR(c->bgt)) {
1461 err = PTR_ERR(c->bgt);
1462 c->bgt = NULL;
Heiko Schocher94b66de2015-10-22 06:19:21 +02001463 ubifs_err(c, "cannot spawn \"%s\", error %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001464 c->bgt_name, err);
1465 goto out_wbufs;
1466 }
1467 wake_up_process(c->bgt);
1468 }
1469#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +01001470
1471 err = ubifs_read_master(c);
1472 if (err)
1473 goto out_master;
1474
1475 init_constants_master(c);
1476
1477 if ((c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY)) != 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001478 ubifs_msg(c, "recovery needed");
Stefan Roese2fc10f62009-03-19 15:35:05 +01001479 c->need_recovery = 1;
1480 }
1481
Heiko Schocherf5895d12014-06-24 10:10:04 +02001482#ifndef __UBOOT__
1483 if (c->need_recovery && !c->ro_mount) {
1484 err = ubifs_recover_inl_heads(c, c->sbuf);
1485 if (err)
1486 goto out_master;
1487 }
1488#endif
1489
1490 err = ubifs_lpt_init(c, 1, !c->ro_mount);
Stefan Roese2fc10f62009-03-19 15:35:05 +01001491 if (err)
Heiko Schocherf5895d12014-06-24 10:10:04 +02001492 goto out_master;
1493
1494#ifndef __UBOOT__
1495 if (!c->ro_mount && c->space_fixup) {
1496 err = ubifs_fixup_free_space(c);
1497 if (err)
1498 goto out_lpt;
1499 }
1500
Heiko Schocher94b66de2015-10-22 06:19:21 +02001501 if (!c->ro_mount && !c->need_recovery) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02001502 /*
1503 * Set the "dirty" flag so that if we reboot uncleanly we
1504 * will notice this immediately on the next mount.
1505 */
1506 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
1507 err = ubifs_write_master(c);
1508 if (err)
1509 goto out_lpt;
1510 }
1511#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +01001512
Heiko Schocherf5895d12014-06-24 10:10:04 +02001513 err = dbg_check_idx_size(c, c->bi.old_idx_sz);
Stefan Roese2fc10f62009-03-19 15:35:05 +01001514 if (err)
1515 goto out_lpt;
1516
1517 err = ubifs_replay_journal(c);
1518 if (err)
1519 goto out_journal;
1520
Heiko Schocherf5895d12014-06-24 10:10:04 +02001521 /* Calculate 'min_idx_lebs' after journal replay */
1522 c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
1523
1524 err = ubifs_mount_orphans(c, c->need_recovery, c->ro_mount);
Stefan Roese2fc10f62009-03-19 15:35:05 +01001525 if (err)
1526 goto out_orphans;
1527
Heiko Schocherf5895d12014-06-24 10:10:04 +02001528 if (!c->ro_mount) {
1529#ifndef __UBOOT__
1530 int lnum;
1531
1532 err = check_free_space(c);
1533 if (err)
1534 goto out_orphans;
1535
1536 /* Check for enough log space */
1537 lnum = c->lhead_lnum + 1;
1538 if (lnum >= UBIFS_LOG_LNUM + c->log_lebs)
1539 lnum = UBIFS_LOG_LNUM;
1540 if (lnum == c->ltail_lnum) {
1541 err = ubifs_consolidate_log(c);
1542 if (err)
1543 goto out_orphans;
1544 }
1545
1546 if (c->need_recovery) {
1547 err = ubifs_recover_size(c);
1548 if (err)
1549 goto out_orphans;
1550 err = ubifs_rcvry_gc_commit(c);
1551 if (err)
1552 goto out_orphans;
1553 } else {
1554 err = take_gc_lnum(c);
1555 if (err)
1556 goto out_orphans;
1557
1558 /*
1559 * GC LEB may contain garbage if there was an unclean
1560 * reboot, and it should be un-mapped.
1561 */
1562 err = ubifs_leb_unmap(c, c->gc_lnum);
1563 if (err)
1564 goto out_orphans;
1565 }
1566
1567 err = dbg_check_lprops(c);
1568 if (err)
1569 goto out_orphans;
1570#endif
1571 } else if (c->need_recovery) {
Stefan Roese2fc10f62009-03-19 15:35:05 +01001572 err = ubifs_recover_size(c);
1573 if (err)
1574 goto out_orphans;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001575 } else {
1576 /*
1577 * Even if we mount read-only, we have to set space in GC LEB
1578 * to proper value because this affects UBIFS free space
1579 * reporting. We do not want to have a situation when
1580 * re-mounting from R/O to R/W changes amount of free space.
1581 */
1582 err = take_gc_lnum(c);
1583 if (err)
1584 goto out_orphans;
Stefan Roese2fc10f62009-03-19 15:35:05 +01001585 }
1586
Heiko Schocherf5895d12014-06-24 10:10:04 +02001587#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +01001588 spin_lock(&ubifs_infos_lock);
1589 list_add_tail(&c->infos_list, &ubifs_infos);
1590 spin_unlock(&ubifs_infos_lock);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001591#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +01001592
1593 if (c->need_recovery) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02001594 if (c->ro_mount)
Heiko Schocher94b66de2015-10-22 06:19:21 +02001595 ubifs_msg(c, "recovery deferred");
Stefan Roese2fc10f62009-03-19 15:35:05 +01001596 else {
1597 c->need_recovery = 0;
Heiko Schocher94b66de2015-10-22 06:19:21 +02001598 ubifs_msg(c, "recovery completed");
Heiko Schocherf5895d12014-06-24 10:10:04 +02001599 /*
1600 * GC LEB has to be empty and taken at this point. But
1601 * the journal head LEBs may also be accounted as
1602 * "empty taken" if they are empty.
1603 */
1604 ubifs_assert(c->lst.taken_empty_lebs > 0);
Stefan Roese2fc10f62009-03-19 15:35:05 +01001605 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001606 } else
1607 ubifs_assert(c->lst.taken_empty_lebs > 0);
Stefan Roese2fc10f62009-03-19 15:35:05 +01001608
1609 err = dbg_check_filesystem(c);
1610 if (err)
1611 goto out_infos;
1612
Heiko Schocherf5895d12014-06-24 10:10:04 +02001613 err = dbg_debugfs_init_fs(c);
1614 if (err)
1615 goto out_infos;
1616
1617 c->mounting = 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +01001618
Heiko Schocher94b66de2015-10-22 06:19:21 +02001619 ubifs_msg(c, "UBIFS: mounted UBI device %d, volume %d, name \"%s\"%s",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001620 c->vi.ubi_num, c->vi.vol_id, c->vi.name,
1621 c->ro_mount ? ", R/O mode" : "");
Stefan Roese2fc10f62009-03-19 15:35:05 +01001622 x = (long long)c->main_lebs * c->leb_size;
Petr Vorel05239e82018-03-24 01:49:23 +01001623#ifndef CONFIG_UBIFS_SILENCE_MSG
Heiko Schocherf5895d12014-06-24 10:10:04 +02001624 y = (long long)c->log_lebs * c->leb_size + c->max_bud_bytes;
Petr Vorel05239e82018-03-24 01:49:23 +01001625#endif
Heiko Schocher94b66de2015-10-22 06:19:21 +02001626 ubifs_msg(c, "LEB size: %d bytes (%d KiB), min./max. I/O unit sizes: %d bytes/%d bytes",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001627 c->leb_size, c->leb_size >> 10, c->min_io_size,
1628 c->max_write_size);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001629 ubifs_msg(c, "FS size: %lld bytes (%lld MiB, %d LEBs), journal size %lld bytes (%lld MiB, %d LEBs)",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001630 x, x >> 20, c->main_lebs,
1631 y, y >> 20, c->log_lebs + c->max_bud_cnt);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001632 ubifs_msg(c, "reserved for root: %llu bytes (%llu KiB)",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001633 c->report_rp_size, c->report_rp_size >> 10);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001634 ubifs_msg(c, "media format: w%d/r%d (latest is w%d/r%d), UUID %pUB%s",
Artem Bityutskiy619697a2009-03-27 10:21:14 +01001635 c->fmt_version, c->ro_compat_version,
Heiko Schocherf5895d12014-06-24 10:10:04 +02001636 UBIFS_FORMAT_VERSION, UBIFS_RO_COMPAT_VERSION, c->uuid,
1637 c->big_lpt ? ", big LPT model" : ", small LPT model");
Stefan Roese2fc10f62009-03-19 15:35:05 +01001638
Heiko Schocherf5895d12014-06-24 10:10:04 +02001639 dbg_gen("default compressor: %s", ubifs_compr_name(c->default_compr));
1640 dbg_gen("data journal heads: %d",
Stefan Roese2fc10f62009-03-19 15:35:05 +01001641 c->jhead_cnt - NONDATA_JHEADS_CNT);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001642 dbg_gen("log LEBs: %d (%d - %d)",
Stefan Roese2fc10f62009-03-19 15:35:05 +01001643 c->log_lebs, UBIFS_LOG_LNUM, c->log_last);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001644 dbg_gen("LPT area LEBs: %d (%d - %d)",
Stefan Roese2fc10f62009-03-19 15:35:05 +01001645 c->lpt_lebs, c->lpt_first, c->lpt_last);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001646 dbg_gen("orphan area LEBs: %d (%d - %d)",
Stefan Roese2fc10f62009-03-19 15:35:05 +01001647 c->orph_lebs, c->orph_first, c->orph_last);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001648 dbg_gen("main area LEBs: %d (%d - %d)",
Stefan Roese2fc10f62009-03-19 15:35:05 +01001649 c->main_lebs, c->main_first, c->leb_cnt - 1);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001650 dbg_gen("index LEBs: %d", c->lst.idx_lebs);
1651 dbg_gen("total index bytes: %lld (%lld KiB, %lld MiB)",
1652 c->bi.old_idx_sz, c->bi.old_idx_sz >> 10,
1653 c->bi.old_idx_sz >> 20);
1654 dbg_gen("key hash type: %d", c->key_hash_type);
1655 dbg_gen("tree fanout: %d", c->fanout);
1656 dbg_gen("reserved GC LEB: %d", c->gc_lnum);
1657 dbg_gen("max. znode size %d", c->max_znode_sz);
1658 dbg_gen("max. index node size %d", c->max_idx_node_sz);
1659 dbg_gen("node sizes: data %zu, inode %zu, dentry %zu",
Stefan Roese2fc10f62009-03-19 15:35:05 +01001660 UBIFS_DATA_NODE_SZ, UBIFS_INO_NODE_SZ, UBIFS_DENT_NODE_SZ);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001661 dbg_gen("node sizes: trun %zu, sb %zu, master %zu",
Stefan Roese2fc10f62009-03-19 15:35:05 +01001662 UBIFS_TRUN_NODE_SZ, UBIFS_SB_NODE_SZ, UBIFS_MST_NODE_SZ);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001663 dbg_gen("node sizes: ref %zu, cmt. start %zu, orph %zu",
Stefan Roese2fc10f62009-03-19 15:35:05 +01001664 UBIFS_REF_NODE_SZ, UBIFS_CS_NODE_SZ, UBIFS_ORPH_NODE_SZ);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001665 dbg_gen("max. node sizes: data %zu, inode %zu dentry %zu, idx %d",
Wolfgang Denkec7fbf52013-10-04 17:43:24 +02001666 UBIFS_MAX_DATA_NODE_SZ, UBIFS_MAX_INO_NODE_SZ,
Heiko Schocherf5895d12014-06-24 10:10:04 +02001667 UBIFS_MAX_DENT_NODE_SZ, ubifs_idx_node_sz(c, c->fanout));
1668 dbg_gen("dead watermark: %d", c->dead_wm);
1669 dbg_gen("dark watermark: %d", c->dark_wm);
1670 dbg_gen("LEB overhead: %d", c->leb_overhead);
Stefan Roese2fc10f62009-03-19 15:35:05 +01001671 x = (long long)c->main_lebs * c->dark_wm;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001672 dbg_gen("max. dark space: %lld (%lld KiB, %lld MiB)",
Stefan Roese2fc10f62009-03-19 15:35:05 +01001673 x, x >> 10, x >> 20);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001674 dbg_gen("maximum bud bytes: %lld (%lld KiB, %lld MiB)",
Stefan Roese2fc10f62009-03-19 15:35:05 +01001675 c->max_bud_bytes, c->max_bud_bytes >> 10,
1676 c->max_bud_bytes >> 20);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001677 dbg_gen("BG commit bud bytes: %lld (%lld KiB, %lld MiB)",
Stefan Roese2fc10f62009-03-19 15:35:05 +01001678 c->bg_bud_bytes, c->bg_bud_bytes >> 10,
1679 c->bg_bud_bytes >> 20);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001680 dbg_gen("current bud bytes %lld (%lld KiB, %lld MiB)",
Stefan Roese2fc10f62009-03-19 15:35:05 +01001681 c->bud_bytes, c->bud_bytes >> 10, c->bud_bytes >> 20);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001682 dbg_gen("max. seq. number: %llu", c->max_sqnum);
1683 dbg_gen("commit number: %llu", c->cmt_no);
1684
1685 return 0;
1686
1687out_infos:
1688 spin_lock(&ubifs_infos_lock);
1689 list_del(&c->infos_list);
1690 spin_unlock(&ubifs_infos_lock);
1691out_orphans:
1692 free_orphans(c);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001693out_journal:
1694 destroy_journal(c);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001695out_lpt:
1696 ubifs_lpt_free(c, 0);
1697out_master:
1698 kfree(c->mst_node);
1699 kfree(c->rcvrd_mst_node);
1700 if (c->bgt)
1701 kthread_stop(c->bgt);
1702#ifndef __UBOOT__
1703out_wbufs:
1704#endif
1705 free_wbufs(c);
1706out_cbuf:
1707 kfree(c->cbuf);
1708out_free:
1709 kfree(c->write_reserve_buf);
1710 kfree(c->bu.buf);
1711 vfree(c->ileb_buf);
1712 vfree(c->sbuf);
1713 kfree(c->bottom_up_buf);
1714 ubifs_debugging_exit(c);
1715 return err;
1716}
1717
1718/**
1719 * ubifs_umount - un-mount UBIFS file-system.
1720 * @c: UBIFS file-system description object
1721 *
1722 * Note, this function is called to free allocated resourced when un-mounting,
1723 * as well as free resources when an error occurred while we were half way
1724 * through mounting (error path cleanup function). So it has to make sure the
1725 * resource was actually allocated before freeing it.
1726 */
1727#ifndef __UBOOT__
1728static void ubifs_umount(struct ubifs_info *c)
1729#else
1730void ubifs_umount(struct ubifs_info *c)
1731#endif
1732{
1733 dbg_gen("un-mounting UBI device %d, volume %d", c->vi.ubi_num,
1734 c->vi.vol_id);
1735
1736 dbg_debugfs_exit_fs(c);
1737 spin_lock(&ubifs_infos_lock);
1738 list_del(&c->infos_list);
1739 spin_unlock(&ubifs_infos_lock);
1740
1741#ifndef __UBOOT__
1742 if (c->bgt)
1743 kthread_stop(c->bgt);
1744
1745 destroy_journal(c);
1746#endif
1747 free_wbufs(c);
1748 free_orphans(c);
1749 ubifs_lpt_free(c, 0);
1750
1751 kfree(c->cbuf);
1752 kfree(c->rcvrd_mst_node);
1753 kfree(c->mst_node);
1754 kfree(c->write_reserve_buf);
1755 kfree(c->bu.buf);
1756 vfree(c->ileb_buf);
1757 vfree(c->sbuf);
1758 kfree(c->bottom_up_buf);
1759 ubifs_debugging_exit(c);
1760#ifdef __UBOOT__
Pali Rohárc3bdb7c2022-05-30 11:09:11 +02001761 ubi_close_volume(c->ubi);
1762 mutex_unlock(&c->umount_mutex);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001763 /* Finally free U-Boot's global copy of superblock */
1764 if (ubifs_sb != NULL) {
1765 free(ubifs_sb->s_fs_info);
1766 free(ubifs_sb);
1767 }
1768#endif
1769}
1770
1771#ifndef __UBOOT__
1772/**
1773 * ubifs_remount_rw - re-mount in read-write mode.
1774 * @c: UBIFS file-system description object
1775 *
1776 * UBIFS avoids allocating many unnecessary resources when mounted in read-only
1777 * mode. This function allocates the needed resources and re-mounts UBIFS in
1778 * read-write mode.
1779 */
1780static int ubifs_remount_rw(struct ubifs_info *c)
1781{
1782 int err, lnum;
1783
1784 if (c->rw_incompat) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001785 ubifs_err(c, "the file-system is not R/W-compatible");
1786 ubifs_msg(c, "on-flash format version is w%d/r%d, but software only supports up to version w%d/r%d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001787 c->fmt_version, c->ro_compat_version,
1788 UBIFS_FORMAT_VERSION, UBIFS_RO_COMPAT_VERSION);
1789 return -EROFS;
1790 }
1791
1792 mutex_lock(&c->umount_mutex);
1793 dbg_save_space_info(c);
1794 c->remounting_rw = 1;
1795 c->ro_mount = 0;
1796
1797 if (c->space_fixup) {
1798 err = ubifs_fixup_free_space(c);
1799 if (err)
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001800 goto out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001801 }
1802
1803 err = check_free_space(c);
1804 if (err)
1805 goto out;
1806
1807 if (c->old_leb_cnt != c->leb_cnt) {
1808 struct ubifs_sb_node *sup;
1809
1810 sup = ubifs_read_sb_node(c);
1811 if (IS_ERR(sup)) {
1812 err = PTR_ERR(sup);
1813 goto out;
1814 }
1815 sup->leb_cnt = cpu_to_le32(c->leb_cnt);
1816 err = ubifs_write_sb_node(c, sup);
1817 kfree(sup);
1818 if (err)
1819 goto out;
1820 }
1821
1822 if (c->need_recovery) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001823 ubifs_msg(c, "completing deferred recovery");
Heiko Schocherf5895d12014-06-24 10:10:04 +02001824 err = ubifs_write_rcvrd_mst_node(c);
1825 if (err)
1826 goto out;
1827 err = ubifs_recover_size(c);
1828 if (err)
1829 goto out;
1830 err = ubifs_clean_lebs(c, c->sbuf);
1831 if (err)
1832 goto out;
1833 err = ubifs_recover_inl_heads(c, c->sbuf);
1834 if (err)
1835 goto out;
1836 } else {
1837 /* A readonly mount is not allowed to have orphans */
1838 ubifs_assert(c->tot_orphans == 0);
1839 err = ubifs_clear_orphans(c);
1840 if (err)
1841 goto out;
1842 }
1843
1844 if (!(c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY))) {
1845 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
1846 err = ubifs_write_master(c);
1847 if (err)
1848 goto out;
1849 }
1850
1851 c->ileb_buf = vmalloc(c->leb_size);
1852 if (!c->ileb_buf) {
1853 err = -ENOMEM;
1854 goto out;
1855 }
1856
1857 c->write_reserve_buf = kmalloc(COMPRESSED_DATA_NODE_BUF_SZ, GFP_KERNEL);
1858 if (!c->write_reserve_buf) {
1859 err = -ENOMEM;
1860 goto out;
1861 }
1862
1863 err = ubifs_lpt_init(c, 0, 1);
1864 if (err)
1865 goto out;
1866
1867 /* Create background thread */
1868 c->bgt = kthread_create(ubifs_bg_thread, c, "%s", c->bgt_name);
1869 if (IS_ERR(c->bgt)) {
1870 err = PTR_ERR(c->bgt);
1871 c->bgt = NULL;
Heiko Schocher94b66de2015-10-22 06:19:21 +02001872 ubifs_err(c, "cannot spawn \"%s\", error %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001873 c->bgt_name, err);
1874 goto out;
1875 }
1876 wake_up_process(c->bgt);
1877
1878 c->orph_buf = vmalloc(c->leb_size);
1879 if (!c->orph_buf) {
1880 err = -ENOMEM;
1881 goto out;
1882 }
1883
1884 /* Check for enough log space */
1885 lnum = c->lhead_lnum + 1;
1886 if (lnum >= UBIFS_LOG_LNUM + c->log_lebs)
1887 lnum = UBIFS_LOG_LNUM;
1888 if (lnum == c->ltail_lnum) {
1889 err = ubifs_consolidate_log(c);
1890 if (err)
1891 goto out;
1892 }
Stefan Roese2fc10f62009-03-19 15:35:05 +01001893
Heiko Schocherf5895d12014-06-24 10:10:04 +02001894 if (c->need_recovery)
1895 err = ubifs_rcvry_gc_commit(c);
1896 else
1897 err = ubifs_leb_unmap(c, c->gc_lnum);
1898 if (err)
1899 goto out;
Stefan Roese2fc10f62009-03-19 15:35:05 +01001900
Heiko Schocherf5895d12014-06-24 10:10:04 +02001901 dbg_gen("re-mounted read-write");
1902 c->remounting_rw = 0;
1903
1904 if (c->need_recovery) {
1905 c->need_recovery = 0;
Heiko Schocher94b66de2015-10-22 06:19:21 +02001906 ubifs_msg(c, "deferred recovery completed");
Heiko Schocherf5895d12014-06-24 10:10:04 +02001907 } else {
1908 /*
1909 * Do not run the debugging space check if the were doing
1910 * recovery, because when we saved the information we had the
1911 * file-system in a state where the TNC and lprops has been
1912 * modified in memory, but all the I/O operations (including a
1913 * commit) were deferred. So the file-system was in
1914 * "non-committed" state. Now the file-system is in committed
1915 * state, and of course the amount of free space will change
1916 * because, for example, the old index size was imprecise.
1917 */
1918 err = dbg_check_space_info(c);
1919 }
1920
1921 mutex_unlock(&c->umount_mutex);
1922 return err;
1923
1924out:
1925 c->ro_mount = 1;
1926 vfree(c->orph_buf);
1927 c->orph_buf = NULL;
1928 if (c->bgt) {
Stefan Roese2fc10f62009-03-19 15:35:05 +01001929 kthread_stop(c->bgt);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001930 c->bgt = NULL;
1931 }
1932 free_wbufs(c);
1933 kfree(c->write_reserve_buf);
1934 c->write_reserve_buf = NULL;
Stefan Roese2fc10f62009-03-19 15:35:05 +01001935 vfree(c->ileb_buf);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001936 c->ileb_buf = NULL;
1937 ubifs_lpt_free(c, 1);
1938 c->remounting_rw = 0;
1939 mutex_unlock(&c->umount_mutex);
Stefan Roese2fc10f62009-03-19 15:35:05 +01001940 return err;
1941}
1942
1943/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001944 * ubifs_remount_ro - re-mount in read-only mode.
Stefan Roese2fc10f62009-03-19 15:35:05 +01001945 * @c: UBIFS file-system description object
1946 *
Heiko Schocherf5895d12014-06-24 10:10:04 +02001947 * We assume VFS has stopped writing. Possibly the background thread could be
1948 * running a commit, however kthread_stop will wait in that case.
Stefan Roese2fc10f62009-03-19 15:35:05 +01001949 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02001950static void ubifs_remount_ro(struct ubifs_info *c)
Stefan Roese2fc10f62009-03-19 15:35:05 +01001951{
Heiko Schocherf5895d12014-06-24 10:10:04 +02001952 int i, err;
Stefan Roese2fc10f62009-03-19 15:35:05 +01001953
Heiko Schocherf5895d12014-06-24 10:10:04 +02001954 ubifs_assert(!c->need_recovery);
1955 ubifs_assert(!c->ro_mount);
Stefan Roese2fc10f62009-03-19 15:35:05 +01001956
Heiko Schocherf5895d12014-06-24 10:10:04 +02001957 mutex_lock(&c->umount_mutex);
1958 if (c->bgt) {
Stefan Roese2fc10f62009-03-19 15:35:05 +01001959 kthread_stop(c->bgt);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001960 c->bgt = NULL;
1961 }
Stefan Roese2fc10f62009-03-19 15:35:05 +01001962
Heiko Schocherf5895d12014-06-24 10:10:04 +02001963 dbg_save_space_info(c);
Stefan Roese2fc10f62009-03-19 15:35:05 +01001964
Heiko Schocherf5895d12014-06-24 10:10:04 +02001965 for (i = 0; i < c->jhead_cnt; i++)
1966 ubifs_wbuf_sync(&c->jheads[i].wbuf);
1967
1968 c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
1969 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
1970 c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
1971 err = ubifs_write_master(c);
1972 if (err)
1973 ubifs_ro_mode(c, err);
1974
1975 vfree(c->orph_buf);
1976 c->orph_buf = NULL;
1977 kfree(c->write_reserve_buf);
1978 c->write_reserve_buf = NULL;
Stefan Roese2fc10f62009-03-19 15:35:05 +01001979 vfree(c->ileb_buf);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001980 c->ileb_buf = NULL;
1981 ubifs_lpt_free(c, 1);
1982 c->ro_mount = 1;
1983 err = dbg_check_space_info(c);
1984 if (err)
1985 ubifs_ro_mode(c, err);
1986 mutex_unlock(&c->umount_mutex);
1987}
Stefan Roese2fc10f62009-03-19 15:35:05 +01001988
Heiko Schocherf5895d12014-06-24 10:10:04 +02001989static void ubifs_put_super(struct super_block *sb)
1990{
1991 int i;
1992 struct ubifs_info *c = sb->s_fs_info;
1993
Heiko Schocher94b66de2015-10-22 06:19:21 +02001994 ubifs_msg(c, "un-mount UBI device %d", c->vi.ubi_num);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001995
1996 /*
1997 * The following asserts are only valid if there has not been a failure
1998 * of the media. For example, there will be dirty inodes if we failed
1999 * to write them back because of I/O errors.
2000 */
2001 if (!c->ro_error) {
2002 ubifs_assert(c->bi.idx_growth == 0);
2003 ubifs_assert(c->bi.dd_growth == 0);
2004 ubifs_assert(c->bi.data_growth == 0);
2005 }
2006
2007 /*
2008 * The 'c->umount_lock' prevents races between UBIFS memory shrinker
2009 * and file system un-mount. Namely, it prevents the shrinker from
2010 * picking this superblock for shrinking - it will be just skipped if
2011 * the mutex is locked.
2012 */
2013 mutex_lock(&c->umount_mutex);
2014 if (!c->ro_mount) {
2015 /*
2016 * First of all kill the background thread to make sure it does
2017 * not interfere with un-mounting and freeing resources.
2018 */
2019 if (c->bgt) {
2020 kthread_stop(c->bgt);
2021 c->bgt = NULL;
2022 }
2023
2024 /*
2025 * On fatal errors c->ro_error is set to 1, in which case we do
2026 * not write the master node.
2027 */
2028 if (!c->ro_error) {
2029 int err;
2030
2031 /* Synchronize write-buffers */
2032 for (i = 0; i < c->jhead_cnt; i++)
2033 ubifs_wbuf_sync(&c->jheads[i].wbuf);
2034
2035 /*
2036 * We are being cleanly unmounted which means the
2037 * orphans were killed - indicate this in the master
2038 * node. Also save the reserved GC LEB number.
2039 */
2040 c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
2041 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
2042 c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
2043 err = ubifs_write_master(c);
2044 if (err)
2045 /*
2046 * Recovery will attempt to fix the master area
2047 * next mount, so we just print a message and
2048 * continue to unmount normally.
2049 */
Heiko Schocher94b66de2015-10-22 06:19:21 +02002050 ubifs_err(c, "failed to write master node, error %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002051 err);
2052 } else {
2053#ifndef __UBOOT__
2054 for (i = 0; i < c->jhead_cnt; i++)
2055 /* Make sure write-buffer timers are canceled */
2056 hrtimer_cancel(&c->jheads[i].wbuf.timer);
2057#endif
2058 }
2059 }
2060
2061 ubifs_umount(c);
2062#ifndef __UBOOT__
2063 bdi_destroy(&c->bdi);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002064 ubi_close_volume(c->ubi);
2065 mutex_unlock(&c->umount_mutex);
Pali Rohárc3bdb7c2022-05-30 11:09:11 +02002066#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +02002067}
2068#endif
2069
2070#ifndef __UBOOT__
2071static int ubifs_remount_fs(struct super_block *sb, int *flags, char *data)
2072{
2073 int err;
2074 struct ubifs_info *c = sb->s_fs_info;
2075
Heiko Schocher081fe9e2014-07-15 16:08:43 +02002076 sync_filesystem(sb);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002077 dbg_gen("old flags %#lx, new flags %#x", sb->s_flags, *flags);
2078
2079 err = ubifs_parse_options(c, data, 1);
2080 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002081 ubifs_err(c, "invalid or unknown remount parameter");
Heiko Schocherf5895d12014-06-24 10:10:04 +02002082 return err;
2083 }
2084
2085 if (c->ro_mount && !(*flags & MS_RDONLY)) {
2086 if (c->ro_error) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002087 ubifs_msg(c, "cannot re-mount R/W due to prior errors");
Heiko Schocherf5895d12014-06-24 10:10:04 +02002088 return -EROFS;
2089 }
2090 if (c->ro_media) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002091 ubifs_msg(c, "cannot re-mount R/W - UBI volume is R/O");
Heiko Schocherf5895d12014-06-24 10:10:04 +02002092 return -EROFS;
2093 }
2094 err = ubifs_remount_rw(c);
2095 if (err)
2096 return err;
2097 } else if (!c->ro_mount && (*flags & MS_RDONLY)) {
2098 if (c->ro_error) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002099 ubifs_msg(c, "cannot re-mount R/O due to prior errors");
Heiko Schocherf5895d12014-06-24 10:10:04 +02002100 return -EROFS;
2101 }
2102 ubifs_remount_ro(c);
Lars Poeschel384b1fb2011-10-12 11:31:19 +02002103 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02002104
2105 if (c->bulk_read == 1)
2106 bu_init(c);
2107 else {
2108 dbg_gen("disable bulk-read");
2109 kfree(c->bu.buf);
2110 c->bu.buf = NULL;
2111 }
2112
2113 ubifs_assert(c->lst.taken_empty_lebs > 0);
2114 return 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +01002115}
Heiko Schocherf5895d12014-06-24 10:10:04 +02002116#endif
2117
2118const struct super_operations ubifs_super_operations = {
2119 .alloc_inode = ubifs_alloc_inode,
2120#ifndef __UBOOT__
2121 .destroy_inode = ubifs_destroy_inode,
2122 .put_super = ubifs_put_super,
2123 .write_inode = ubifs_write_inode,
2124 .evict_inode = ubifs_evict_inode,
2125 .statfs = ubifs_statfs,
2126#endif
2127 .dirty_inode = ubifs_dirty_inode,
2128#ifndef __UBOOT__
2129 .remount_fs = ubifs_remount_fs,
2130 .show_options = ubifs_show_options,
2131 .sync_fs = ubifs_sync_fs,
2132#endif
2133};
Stefan Roese2fc10f62009-03-19 15:35:05 +01002134
2135/**
2136 * open_ubi - parse UBI device name string and open the UBI device.
2137 * @name: UBI volume name
2138 * @mode: UBI volume open mode
2139 *
Heiko Schocherf5895d12014-06-24 10:10:04 +02002140 * The primary method of mounting UBIFS is by specifying the UBI volume
2141 * character device node path. However, UBIFS may also be mounted withoug any
2142 * character device node using one of the following methods:
2143 *
2144 * o ubiX_Y - mount UBI device number X, volume Y;
2145 * o ubiY - mount UBI device number 0, volume Y;
Stefan Roese2fc10f62009-03-19 15:35:05 +01002146 * o ubiX:NAME - mount UBI device X, volume with name NAME;
2147 * o ubi:NAME - mount UBI device 0, volume with name NAME.
2148 *
2149 * Alternative '!' separator may be used instead of ':' (because some shells
2150 * like busybox may interpret ':' as an NFS host name separator). This function
Heiko Schocherf5895d12014-06-24 10:10:04 +02002151 * returns UBI volume description object in case of success and a negative
2152 * error code in case of failure.
Stefan Roese2fc10f62009-03-19 15:35:05 +01002153 */
2154static struct ubi_volume_desc *open_ubi(const char *name, int mode)
2155{
Heiko Schocherf5895d12014-06-24 10:10:04 +02002156#ifndef __UBOOT__
2157 struct ubi_volume_desc *ubi;
2158#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +01002159 int dev, vol;
2160 char *endptr;
2161
Heiko Schocherf5895d12014-06-24 10:10:04 +02002162#ifndef __UBOOT__
2163 /* First, try to open using the device node path method */
2164 ubi = ubi_open_volume_path(name, mode);
2165 if (!IS_ERR(ubi))
2166 return ubi;
2167#endif
2168
2169 /* Try the "nodev" method */
Stefan Roese2fc10f62009-03-19 15:35:05 +01002170 if (name[0] != 'u' || name[1] != 'b' || name[2] != 'i')
2171 return ERR_PTR(-EINVAL);
2172
2173 /* ubi:NAME method */
2174 if ((name[3] == ':' || name[3] == '!') && name[4] != '\0')
2175 return ubi_open_volume_nm(0, name + 4, mode);
2176
2177 if (!isdigit(name[3]))
2178 return ERR_PTR(-EINVAL);
2179
2180 dev = simple_strtoul(name + 3, &endptr, 0);
2181
2182 /* ubiY method */
2183 if (*endptr == '\0')
2184 return ubi_open_volume(0, dev, mode);
2185
2186 /* ubiX_Y method */
2187 if (*endptr == '_' && isdigit(endptr[1])) {
2188 vol = simple_strtoul(endptr + 1, &endptr, 0);
2189 if (*endptr != '\0')
2190 return ERR_PTR(-EINVAL);
2191 return ubi_open_volume(dev, vol, mode);
2192 }
2193
2194 /* ubiX:NAME method */
2195 if ((*endptr == ':' || *endptr == '!') && endptr[1] != '\0')
2196 return ubi_open_volume_nm(dev, ++endptr, mode);
2197
2198 return ERR_PTR(-EINVAL);
2199}
2200
Heiko Schocherf5895d12014-06-24 10:10:04 +02002201static struct ubifs_info *alloc_ubifs_info(struct ubi_volume_desc *ubi)
Stefan Roese2fc10f62009-03-19 15:35:05 +01002202{
Stefan Roese2fc10f62009-03-19 15:35:05 +01002203 struct ubifs_info *c;
Stefan Roese2fc10f62009-03-19 15:35:05 +01002204
2205 c = kzalloc(sizeof(struct ubifs_info), GFP_KERNEL);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002206 if (c) {
2207 spin_lock_init(&c->cnt_lock);
2208 spin_lock_init(&c->cs_lock);
2209 spin_lock_init(&c->buds_lock);
2210 spin_lock_init(&c->space_lock);
2211 spin_lock_init(&c->orphan_lock);
2212 init_rwsem(&c->commit_sem);
2213 mutex_init(&c->lp_mutex);
2214 mutex_init(&c->tnc_mutex);
2215 mutex_init(&c->log_mutex);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002216 mutex_init(&c->umount_mutex);
2217 mutex_init(&c->bu_mutex);
2218 mutex_init(&c->write_reserve_mutex);
2219 init_waitqueue_head(&c->cmt_wq);
2220 c->buds = RB_ROOT;
2221 c->old_idx = RB_ROOT;
2222 c->size_tree = RB_ROOT;
2223 c->orph_tree = RB_ROOT;
2224 INIT_LIST_HEAD(&c->infos_list);
2225 INIT_LIST_HEAD(&c->idx_gc);
2226 INIT_LIST_HEAD(&c->replay_list);
2227 INIT_LIST_HEAD(&c->replay_buds);
2228 INIT_LIST_HEAD(&c->uncat_list);
2229 INIT_LIST_HEAD(&c->empty_list);
2230 INIT_LIST_HEAD(&c->freeable_list);
2231 INIT_LIST_HEAD(&c->frdi_idx_list);
2232 INIT_LIST_HEAD(&c->unclean_leb_list);
2233 INIT_LIST_HEAD(&c->old_buds);
2234 INIT_LIST_HEAD(&c->orph_list);
2235 INIT_LIST_HEAD(&c->orph_new);
2236 c->no_chk_data_crc = 1;
Stefan Roese2fc10f62009-03-19 15:35:05 +01002237
Heiko Schocherf5895d12014-06-24 10:10:04 +02002238 c->highest_inum = UBIFS_FIRST_INO;
2239 c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM;
Stefan Roese2fc10f62009-03-19 15:35:05 +01002240
Heiko Schocherf5895d12014-06-24 10:10:04 +02002241 ubi_get_volume_info(ubi, &c->vi);
2242 ubi_get_device_info(c->vi.ubi_num, &c->di);
2243 }
2244 return c;
2245}
Stefan Roese2fc10f62009-03-19 15:35:05 +01002246
Heiko Schocherf5895d12014-06-24 10:10:04 +02002247static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
2248{
2249 struct ubifs_info *c = sb->s_fs_info;
2250 struct inode *root;
2251 int err;
Stefan Roese2fc10f62009-03-19 15:35:05 +01002252
Heiko Schocherf5895d12014-06-24 10:10:04 +02002253 c->vfs_sb = sb;
Heiko Schocherb24c4272014-07-15 16:08:42 +02002254#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +01002255 /* Re-open the UBI device in read-write mode */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002256 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READWRITE);
Heiko Schocherb24c4272014-07-15 16:08:42 +02002257#else
2258 /* U-Boot read only mode */
2259 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
2260#endif
2261
Stefan Roese2fc10f62009-03-19 15:35:05 +01002262 if (IS_ERR(c->ubi)) {
2263 err = PTR_ERR(c->ubi);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002264 goto out;
Stefan Roese2fc10f62009-03-19 15:35:05 +01002265 }
2266
Heiko Schocherf5895d12014-06-24 10:10:04 +02002267#ifndef __UBOOT__
2268 /*
2269 * UBIFS provides 'backing_dev_info' in order to disable read-ahead. For
2270 * UBIFS, I/O is not deferred, it is done immediately in readpage,
2271 * which means the user would have to wait not just for their own I/O
2272 * but the read-ahead I/O as well i.e. completely pointless.
2273 *
2274 * Read-ahead will be disabled because @c->bdi.ra_pages is 0.
2275 */
Heiko Schocher94b66de2015-10-22 06:19:21 +02002276 c->bdi.name = "ubifs",
2277 c->bdi.capabilities = 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002278 err = bdi_init(&c->bdi);
2279 if (err)
2280 goto out_close;
2281 err = bdi_register(&c->bdi, NULL, "ubifs_%d_%d",
2282 c->vi.ubi_num, c->vi.vol_id);
2283 if (err)
2284 goto out_bdi;
2285
2286 err = ubifs_parse_options(c, data, 0);
2287 if (err)
2288 goto out_bdi;
Stefan Roese2fc10f62009-03-19 15:35:05 +01002289
Heiko Schocherf5895d12014-06-24 10:10:04 +02002290 sb->s_bdi = &c->bdi;
2291#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +01002292 sb->s_fs_info = c;
2293 sb->s_magic = UBIFS_SUPER_MAGIC;
2294 sb->s_blocksize = UBIFS_BLOCK_SIZE;
2295 sb->s_blocksize_bits = UBIFS_BLOCK_SHIFT;
Stefan Roese2fc10f62009-03-19 15:35:05 +01002296 sb->s_maxbytes = c->max_inode_sz = key_max_inode_size(c);
2297 if (c->max_inode_sz > MAX_LFS_FILESIZE)
2298 sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002299 sb->s_op = &ubifs_super_operations;
Heiko Schocher94b66de2015-10-22 06:19:21 +02002300#ifndef __UBOOT__
2301 sb->s_xattr = ubifs_xattr_handlers;
2302#endif
Artem Bityutskiy619697a2009-03-27 10:21:14 +01002303
Stefan Roese2fc10f62009-03-19 15:35:05 +01002304 mutex_lock(&c->umount_mutex);
2305 err = mount_ubifs(c);
2306 if (err) {
2307 ubifs_assert(err < 0);
2308 goto out_unlock;
2309 }
2310
2311 /* Read the root inode */
2312 root = ubifs_iget(sb, UBIFS_ROOT_INO);
2313 if (IS_ERR(root)) {
2314 err = PTR_ERR(root);
2315 goto out_umount;
2316 }
2317
Heiko Schocherf5895d12014-06-24 10:10:04 +02002318#ifndef __UBOOT__
2319 sb->s_root = d_make_root(root);
2320 if (!sb->s_root) {
2321 err = -ENOMEM;
2322 goto out_umount;
2323 }
2324#else
Stefan Roese2fc10f62009-03-19 15:35:05 +01002325 sb->s_root = NULL;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002326#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +01002327
2328 mutex_unlock(&c->umount_mutex);
2329 return 0;
2330
2331out_umount:
2332 ubifs_umount(c);
Pali Rohárc3bdb7c2022-05-30 11:09:11 +02002333#ifdef __UBOOT__
2334 goto out;
2335#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +01002336out_unlock:
2337 mutex_unlock(&c->umount_mutex);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002338#ifndef __UBOOT__
2339out_bdi:
2340 bdi_destroy(&c->bdi);
2341out_close:
2342#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +01002343 ubi_close_volume(c->ubi);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002344out:
Stefan Roese2fc10f62009-03-19 15:35:05 +01002345 return err;
2346}
2347
2348static int sb_test(struct super_block *sb, void *data)
2349{
Heiko Schocherf5895d12014-06-24 10:10:04 +02002350 struct ubifs_info *c1 = data;
2351 struct ubifs_info *c = sb->s_fs_info;
2352
2353 return c->vi.cdev == c1->vi.cdev;
2354}
2355
2356static int sb_set(struct super_block *sb, void *data)
2357{
2358 sb->s_fs_info = data;
2359 return set_anon_super(sb, NULL);
2360}
2361
2362static struct super_block *alloc_super(struct file_system_type *type, int flags)
2363{
2364 struct super_block *s;
2365 int err;
2366
2367 s = kzalloc(sizeof(struct super_block), GFP_USER);
2368 if (!s) {
2369 err = -ENOMEM;
2370 return ERR_PTR(err);
2371 }
2372
Christophe Kerellod700a2b2018-06-27 10:06:59 +02002373#ifndef __UBOOT__
Heiko Schocherf5895d12014-06-24 10:10:04 +02002374 INIT_HLIST_NODE(&s->s_instances);
Christophe Kerellod700a2b2018-06-27 10:06:59 +02002375#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +02002376 INIT_LIST_HEAD(&s->s_inodes);
2377 s->s_time_gran = 1000000000;
2378 s->s_flags = flags;
2379
2380 return s;
2381}
2382
2383/**
2384 * sget - find or create a superblock
2385 * @type: filesystem type superblock should belong to
2386 * @test: comparison callback
2387 * @set: setup callback
2388 * @flags: mount flags
2389 * @data: argument to each of them
2390 */
2391struct super_block *sget(struct file_system_type *type,
2392 int (*test)(struct super_block *,void *),
2393 int (*set)(struct super_block *,void *),
2394 int flags,
2395 void *data)
2396{
2397 struct super_block *s = NULL;
2398#ifndef __UBOOT__
2399 struct super_block *old;
2400#endif
2401 int err;
Stefan Roese2fc10f62009-03-19 15:35:05 +01002402
Heiko Schocherf5895d12014-06-24 10:10:04 +02002403#ifndef __UBOOT__
2404retry:
2405 spin_lock(&sb_lock);
2406 if (test) {
2407 hlist_for_each_entry(old, &type->fs_supers, s_instances) {
2408 if (!test(old, data))
2409 continue;
2410 if (!grab_super(old))
2411 goto retry;
2412 if (s) {
2413 up_write(&s->s_umount);
2414 destroy_super(s);
2415 s = NULL;
2416 }
2417 return old;
2418 }
2419 }
2420#endif
2421 if (!s) {
2422 spin_unlock(&sb_lock);
2423 s = alloc_super(type, flags);
2424 if (!s)
2425 return ERR_PTR(-ENOMEM);
2426#ifndef __UBOOT__
2427 goto retry;
2428#endif
2429 }
2430
2431 err = set(s, data);
2432 if (err) {
2433#ifndef __UBOOT__
2434 spin_unlock(&sb_lock);
2435 up_write(&s->s_umount);
2436 destroy_super(s);
2437#endif
2438 return ERR_PTR(err);
2439 }
2440 s->s_type = type;
2441#ifndef __UBOOT__
2442 strlcpy(s->s_id, type->name, sizeof(s->s_id));
Heiko Schochera79e3c82016-04-21 12:16:58 +02002443 list_add_tail(&s->s_list, &super_blocks);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002444 hlist_add_head(&s->s_instances, &type->fs_supers);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002445 spin_unlock(&sb_lock);
2446 get_filesystem(type);
2447 register_shrinker(&s->s_shrink);
Christophe Kerellod700a2b2018-06-27 10:06:59 +02002448#else
2449 strncpy(s->s_id, type->name, sizeof(s->s_id));
Heiko Schocherf5895d12014-06-24 10:10:04 +02002450#endif
2451 return s;
Stefan Roese2fc10f62009-03-19 15:35:05 +01002452}
2453
Heiko Schocherf5895d12014-06-24 10:10:04 +02002454EXPORT_SYMBOL(sget);
2455
2456
2457static struct dentry *ubifs_mount(struct file_system_type *fs_type, int flags,
2458 const char *name, void *data)
Stefan Roese2fc10f62009-03-19 15:35:05 +01002459{
2460 struct ubi_volume_desc *ubi;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002461 struct ubifs_info *c;
Stefan Roese2fc10f62009-03-19 15:35:05 +01002462 struct super_block *sb;
2463 int err;
2464
2465 dbg_gen("name %s, flags %#x", name, flags);
2466
2467 /*
2468 * Get UBI device number and volume ID. Mount it read-only so far
2469 * because this might be a new mount point, and UBI allows only one
2470 * read-write user at a time.
2471 */
2472 ubi = open_ubi(name, UBI_READONLY);
2473 if (IS_ERR(ubi)) {
Stefan Roesef49f8722018-08-09 09:09:20 +02002474 pr_err("UBIFS error (pid: %d): cannot open \"%s\", error %d\n",
Heiko Schocher94b66de2015-10-22 06:19:21 +02002475 current->pid, name, (int)PTR_ERR(ubi));
Heiko Schocherf5895d12014-06-24 10:10:04 +02002476 return ERR_CAST(ubi);
2477 }
2478
2479 c = alloc_ubifs_info(ubi);
2480 if (!c) {
2481 err = -ENOMEM;
2482 goto out_close;
Stefan Roese2fc10f62009-03-19 15:35:05 +01002483 }
Stefan Roese2fc10f62009-03-19 15:35:05 +01002484
Heiko Schocherf5895d12014-06-24 10:10:04 +02002485 dbg_gen("opened ubi%d_%d", c->vi.ubi_num, c->vi.vol_id);
Stefan Roese2fc10f62009-03-19 15:35:05 +01002486
Heiko Schocherf5895d12014-06-24 10:10:04 +02002487 sb = sget(fs_type, sb_test, sb_set, flags, c);
Stefan Roese2fc10f62009-03-19 15:35:05 +01002488 if (IS_ERR(sb)) {
2489 err = PTR_ERR(sb);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002490 kfree(c);
Stefan Roese2fc10f62009-03-19 15:35:05 +01002491 goto out_close;
2492 }
2493
2494 if (sb->s_root) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02002495 struct ubifs_info *c1 = sb->s_fs_info;
2496 kfree(c);
Stefan Roese2fc10f62009-03-19 15:35:05 +01002497 /* A new mount point for already mounted UBIFS */
2498 dbg_gen("this ubi volume is already mounted");
Heiko Schocherf5895d12014-06-24 10:10:04 +02002499 if (!!(flags & MS_RDONLY) != c1->ro_mount) {
Stefan Roese2fc10f62009-03-19 15:35:05 +01002500 err = -EBUSY;
2501 goto out_deact;
2502 }
2503 } else {
Stefan Roese2fc10f62009-03-19 15:35:05 +01002504 err = ubifs_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
2505 if (err)
2506 goto out_deact;
2507 /* We do not support atime */
2508 sb->s_flags |= MS_ACTIVE | MS_NOATIME;
2509 }
2510
2511 /* 'fill_super()' opens ubi again so we must close it here */
2512 ubi_close_volume(ubi);
2513
Heiko Schocherf5895d12014-06-24 10:10:04 +02002514#ifdef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +01002515 ubifs_sb = sb;
2516 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002517#else
2518 return dget(sb->s_root);
2519#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +01002520
2521out_deact:
Heiko Schocherf5895d12014-06-24 10:10:04 +02002522#ifndef __UBOOT__
2523 deactivate_locked_super(sb);
2524#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +01002525out_close:
2526 ubi_close_volume(ubi);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002527 return ERR_PTR(err);
2528}
2529
2530static void kill_ubifs_super(struct super_block *s)
2531{
2532 struct ubifs_info *c = s->s_fs_info;
2533#ifndef __UBOOT__
2534 kill_anon_super(s);
2535#endif
2536 kfree(c);
Stefan Roese2fc10f62009-03-19 15:35:05 +01002537}
2538
Heiko Schocherf5895d12014-06-24 10:10:04 +02002539static struct file_system_type ubifs_fs_type = {
2540 .name = "ubifs",
2541 .owner = THIS_MODULE,
2542 .mount = ubifs_mount,
2543 .kill_sb = kill_ubifs_super,
2544};
2545#ifndef __UBOOT__
2546MODULE_ALIAS_FS("ubifs");
2547
2548/*
2549 * Inode slab cache constructor.
2550 */
2551static void inode_slab_ctor(void *obj)
2552{
2553 struct ubifs_inode *ui = obj;
2554 inode_init_once(&ui->vfs_inode);
2555}
2556
2557static int __init ubifs_init(void)
2558#else
2559int ubifs_init(void)
2560#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +01002561{
2562 int err;
2563
2564 BUILD_BUG_ON(sizeof(struct ubifs_ch) != 24);
2565
2566 /* Make sure node sizes are 8-byte aligned */
2567 BUILD_BUG_ON(UBIFS_CH_SZ & 7);
2568 BUILD_BUG_ON(UBIFS_INO_NODE_SZ & 7);
2569 BUILD_BUG_ON(UBIFS_DENT_NODE_SZ & 7);
2570 BUILD_BUG_ON(UBIFS_XENT_NODE_SZ & 7);
2571 BUILD_BUG_ON(UBIFS_DATA_NODE_SZ & 7);
2572 BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ & 7);
2573 BUILD_BUG_ON(UBIFS_SB_NODE_SZ & 7);
2574 BUILD_BUG_ON(UBIFS_MST_NODE_SZ & 7);
2575 BUILD_BUG_ON(UBIFS_REF_NODE_SZ & 7);
2576 BUILD_BUG_ON(UBIFS_CS_NODE_SZ & 7);
2577 BUILD_BUG_ON(UBIFS_ORPH_NODE_SZ & 7);
2578
2579 BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ & 7);
2580 BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ & 7);
2581 BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ & 7);
2582 BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ & 7);
2583 BUILD_BUG_ON(UBIFS_MAX_NODE_SZ & 7);
2584 BUILD_BUG_ON(MIN_WRITE_SZ & 7);
2585
2586 /* Check min. node size */
2587 BUILD_BUG_ON(UBIFS_INO_NODE_SZ < MIN_WRITE_SZ);
2588 BUILD_BUG_ON(UBIFS_DENT_NODE_SZ < MIN_WRITE_SZ);
2589 BUILD_BUG_ON(UBIFS_XENT_NODE_SZ < MIN_WRITE_SZ);
2590 BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ < MIN_WRITE_SZ);
2591
2592 BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
2593 BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
2594 BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ > UBIFS_MAX_NODE_SZ);
2595 BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ > UBIFS_MAX_NODE_SZ);
2596
2597 /* Defined node sizes */
2598 BUILD_BUG_ON(UBIFS_SB_NODE_SZ != 4096);
2599 BUILD_BUG_ON(UBIFS_MST_NODE_SZ != 512);
2600 BUILD_BUG_ON(UBIFS_INO_NODE_SZ != 160);
2601 BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64);
2602
2603 /*
2604 * We use 2 bit wide bit-fields to store compression type, which should
2605 * be amended if more compressors are added. The bit-fields are:
2606 * @compr_type in 'struct ubifs_inode', @default_compr in
2607 * 'struct ubifs_info' and @compr_type in 'struct ubifs_mount_opts'.
2608 */
2609 BUILD_BUG_ON(UBIFS_COMPR_TYPES_CNT > 4);
2610
2611 /*
2612 * We require that PAGE_CACHE_SIZE is greater-than-or-equal-to
2613 * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2.
2614 */
2615 if (PAGE_CACHE_SIZE < UBIFS_BLOCK_SIZE) {
Stefan Roesef49f8722018-08-09 09:09:20 +02002616 pr_err("UBIFS error (pid %d): VFS page cache size is %u bytes, but UBIFS requires at least 4096 bytes\n",
Heiko Schocher94b66de2015-10-22 06:19:21 +02002617 current->pid, (unsigned int)PAGE_CACHE_SIZE);
Stefan Roese2fc10f62009-03-19 15:35:05 +01002618 return -EINVAL;
2619 }
2620
Heiko Schocherf5895d12014-06-24 10:10:04 +02002621#ifndef __UBOOT__
2622 ubifs_inode_slab = kmem_cache_create("ubifs_inode_slab",
2623 sizeof(struct ubifs_inode), 0,
2624 SLAB_MEM_SPREAD | SLAB_RECLAIM_ACCOUNT,
2625 &inode_slab_ctor);
2626 if (!ubifs_inode_slab)
2627 return -ENOMEM;
2628
Heiko Schocher94b66de2015-10-22 06:19:21 +02002629 err = register_shrinker(&ubifs_shrinker_info);
2630 if (err)
2631 goto out_slab;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002632#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +01002633
2634 err = ubifs_compressors_init();
2635 if (err)
2636 goto out_shrinker;
2637
Heiko Schocherf5895d12014-06-24 10:10:04 +02002638#ifndef __UBOOT__
2639 err = dbg_debugfs_init();
2640 if (err)
2641 goto out_compr;
2642
2643 err = register_filesystem(&ubifs_fs_type);
2644 if (err) {
Stefan Roesef49f8722018-08-09 09:09:20 +02002645 pr_err("UBIFS error (pid %d): cannot register file system, error %d\n",
Heiko Schocher94b66de2015-10-22 06:19:21 +02002646 current->pid, err);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002647 goto out_dbg;
2648 }
2649#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +01002650 return 0;
2651
Heiko Schocherf5895d12014-06-24 10:10:04 +02002652#ifndef __UBOOT__
2653out_dbg:
2654 dbg_debugfs_exit();
2655out_compr:
2656 ubifs_compressors_exit();
2657#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +01002658out_shrinker:
Heiko Schocherf5895d12014-06-24 10:10:04 +02002659#ifndef __UBOOT__
2660 unregister_shrinker(&ubifs_shrinker_info);
Heiko Schocher94b66de2015-10-22 06:19:21 +02002661out_slab:
Heiko Schocherf5895d12014-06-24 10:10:04 +02002662#endif
2663 kmem_cache_destroy(ubifs_inode_slab);
Stefan Roese2fc10f62009-03-19 15:35:05 +01002664 return err;
2665}
Heiko Schocherf5895d12014-06-24 10:10:04 +02002666/* late_initcall to let compressors initialize first */
2667late_initcall(ubifs_init);
Stefan Roese2fc10f62009-03-19 15:35:05 +01002668
Heiko Schocherf5895d12014-06-24 10:10:04 +02002669#ifndef __UBOOT__
2670static void __exit ubifs_exit(void)
2671{
2672 ubifs_assert(list_empty(&ubifs_infos));
2673 ubifs_assert(atomic_long_read(&ubifs_clean_zn_cnt) == 0);
Stefan Roese2fc10f62009-03-19 15:35:05 +01002674
Heiko Schocherf5895d12014-06-24 10:10:04 +02002675 dbg_debugfs_exit();
2676 ubifs_compressors_exit();
2677 unregister_shrinker(&ubifs_shrinker_info);
Stefan Roese2fc10f62009-03-19 15:35:05 +01002678
Heiko Schocherf5895d12014-06-24 10:10:04 +02002679 /*
2680 * Make sure all delayed rcu free inodes are flushed before we
2681 * destroy cache.
2682 */
2683 rcu_barrier();
2684 kmem_cache_destroy(ubifs_inode_slab);
2685 unregister_filesystem(&ubifs_fs_type);
2686}
2687module_exit(ubifs_exit);
2688
2689MODULE_LICENSE("GPL");
2690MODULE_VERSION(__stringify(UBIFS_VERSION));
2691MODULE_AUTHOR("Artem Bityutskiy, Adrian Hunter");
2692MODULE_DESCRIPTION("UBIFS - UBI File System");
2693#else
2694int uboot_ubifs_mount(char *vol_name)
Stefan Roese2fc10f62009-03-19 15:35:05 +01002695{
Heiko Schocherf5895d12014-06-24 10:10:04 +02002696 struct dentry *ret;
Stefan Roese2fc10f62009-03-19 15:35:05 +01002697 int flags;
Stefan Roese2fc10f62009-03-19 15:35:05 +01002698
2699 /*
2700 * First unmount if allready mounted
2701 */
2702 if (ubifs_sb)
2703 ubifs_umount(ubifs_sb->s_fs_info);
2704
Stefan Roese2fc10f62009-03-19 15:35:05 +01002705 /*
2706 * Mount in read-only mode
2707 */
2708 flags = MS_RDONLY;
Heiko Schocherf5895d12014-06-24 10:10:04 +02002709 ret = ubifs_mount(&ubifs_fs_type, flags, vol_name, NULL);
2710 if (IS_ERR(ret)) {
2711 printf("Error reading superblock on volume '%s' " \
2712 "errno=%d!\n", vol_name, (int)PTR_ERR(ret));
Stefan Roese2fc10f62009-03-19 15:35:05 +01002713 return -1;
2714 }
2715
Stefan Roese2fc10f62009-03-19 15:35:05 +01002716 return 0;
2717}
Heiko Schocherf5895d12014-06-24 10:10:04 +02002718#endif