1ae98043fSRyusuke Konishi // SPDX-License-Identifier: GPL-2.0+
2783f6184SRyusuke Konishi /*
394ee1d91SRyusuke Konishi * NILFS module and super block management.
4783f6184SRyusuke Konishi *
5783f6184SRyusuke Konishi * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6783f6184SRyusuke Konishi *
74b420ab4SRyusuke Konishi * Written by Ryusuke Konishi.
8783f6184SRyusuke Konishi */
9783f6184SRyusuke Konishi /*
10783f6184SRyusuke Konishi * linux/fs/ext2/super.c
11783f6184SRyusuke Konishi *
12783f6184SRyusuke Konishi * Copyright (C) 1992, 1993, 1994, 1995
13783f6184SRyusuke Konishi * Remy Card (card@masi.ibp.fr)
14783f6184SRyusuke Konishi * Laboratoire MASI - Institut Blaise Pascal
15783f6184SRyusuke Konishi * Universite Pierre et Marie Curie (Paris VI)
16783f6184SRyusuke Konishi *
17783f6184SRyusuke Konishi * from
18783f6184SRyusuke Konishi *
19783f6184SRyusuke Konishi * linux/fs/minix/inode.c
20783f6184SRyusuke Konishi *
21783f6184SRyusuke Konishi * Copyright (C) 1991, 1992 Linus Torvalds
22783f6184SRyusuke Konishi *
23783f6184SRyusuke Konishi * Big-endian to little-endian byte-swapping/bitmaps by
24783f6184SRyusuke Konishi * David S. Miller (davem@caip.rutgers.edu), 1995
25783f6184SRyusuke Konishi */
26783f6184SRyusuke Konishi
27783f6184SRyusuke Konishi #include <linux/module.h>
28783f6184SRyusuke Konishi #include <linux/string.h>
29783f6184SRyusuke Konishi #include <linux/slab.h>
30783f6184SRyusuke Konishi #include <linux/init.h>
31783f6184SRyusuke Konishi #include <linux/blkdev.h>
32783f6184SRyusuke Konishi #include <linux/parser.h>
33783f6184SRyusuke Konishi #include <linux/crc32.h>
34783f6184SRyusuke Konishi #include <linux/vfs.h>
35783f6184SRyusuke Konishi #include <linux/writeback.h>
36b58a285bSJiro SEKIBA #include <linux/seq_file.h>
37b58a285bSJiro SEKIBA #include <linux/mount.h>
38*c1e012eaSChristoph Hellwig #include <linux/fs_context.h>
39783f6184SRyusuke Konishi #include "nilfs.h"
408e656fd5SRyusuke Konishi #include "export.h"
41783f6184SRyusuke Konishi #include "mdt.h"
42783f6184SRyusuke Konishi #include "alloc.h"
4305d0e94bSRyusuke Konishi #include "btree.h"
4405d0e94bSRyusuke Konishi #include "btnode.h"
45783f6184SRyusuke Konishi #include "page.h"
46783f6184SRyusuke Konishi #include "cpfile.h"
474e33f9eaSRyusuke Konishi #include "sufile.h" /* nilfs_sufile_resize(), nilfs_sufile_set_alloc_range() */
48783f6184SRyusuke Konishi #include "ifile.h"
49783f6184SRyusuke Konishi #include "dat.h"
50783f6184SRyusuke Konishi #include "segment.h"
51783f6184SRyusuke Konishi #include "segbuf.h"
52783f6184SRyusuke Konishi
53783f6184SRyusuke Konishi MODULE_AUTHOR("NTT Corp.");
54783f6184SRyusuke Konishi MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
55783f6184SRyusuke Konishi "(NILFS)");
56783f6184SRyusuke Konishi MODULE_LICENSE("GPL");
57783f6184SRyusuke Konishi
58abc0b50bSJiro SEKIBA static struct kmem_cache *nilfs_inode_cachep;
5941c88bd7SLi Hong struct kmem_cache *nilfs_transaction_cachep;
6041c88bd7SLi Hong struct kmem_cache *nilfs_segbuf_cachep;
6141c88bd7SLi Hong struct kmem_cache *nilfs_btree_path_cache;
6241c88bd7SLi Hong
63f7545144SRyusuke Konishi static int nilfs_setup_super(struct super_block *sb, int is_mount);
64783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data);
65783f6184SRyusuke Konishi
__nilfs_msg(struct super_block * sb,const char * fmt,...)662987a4cfSJoe Perches void __nilfs_msg(struct super_block *sb, const char *fmt, ...)
67a66dfb0aSRyusuke Konishi {
68a66dfb0aSRyusuke Konishi struct va_format vaf;
69a66dfb0aSRyusuke Konishi va_list args;
702987a4cfSJoe Perches int level;
71a66dfb0aSRyusuke Konishi
72a66dfb0aSRyusuke Konishi va_start(args, fmt);
732987a4cfSJoe Perches
742987a4cfSJoe Perches level = printk_get_level(fmt);
752987a4cfSJoe Perches vaf.fmt = printk_skip_level(fmt);
76a66dfb0aSRyusuke Konishi vaf.va = &args;
772987a4cfSJoe Perches
78a66dfb0aSRyusuke Konishi if (sb)
792987a4cfSJoe Perches printk("%c%cNILFS (%s): %pV\n",
802987a4cfSJoe Perches KERN_SOH_ASCII, level, sb->s_id, &vaf);
81a66dfb0aSRyusuke Konishi else
822987a4cfSJoe Perches printk("%c%cNILFS: %pV\n",
832987a4cfSJoe Perches KERN_SOH_ASCII, level, &vaf);
842987a4cfSJoe Perches
85a66dfb0aSRyusuke Konishi va_end(args);
86a66dfb0aSRyusuke Konishi }
87a66dfb0aSRyusuke Konishi
nilfs_set_error(struct super_block * sb)88f7545144SRyusuke Konishi static void nilfs_set_error(struct super_block *sb)
89c8a11c8aSRyusuke Konishi {
90e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
91d26493b6SJiro SEKIBA struct nilfs_super_block **sbp;
92c8a11c8aSRyusuke Konishi
93c8a11c8aSRyusuke Konishi down_write(&nilfs->ns_sem);
94c8a11c8aSRyusuke Konishi if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) {
95c8a11c8aSRyusuke Konishi nilfs->ns_mount_state |= NILFS_ERROR_FS;
96f7545144SRyusuke Konishi sbp = nilfs_prepare_super(sb, 0);
97d26493b6SJiro SEKIBA if (likely(sbp)) {
98d26493b6SJiro SEKIBA sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
99b2ac86e1SJiro SEKIBA if (sbp[1])
100b2ac86e1SJiro SEKIBA sbp[1]->s_state |= cpu_to_le16(NILFS_ERROR_FS);
101f7545144SRyusuke Konishi nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
102c8a11c8aSRyusuke Konishi }
103d26493b6SJiro SEKIBA }
104c8a11c8aSRyusuke Konishi up_write(&nilfs->ns_sem);
105c8a11c8aSRyusuke Konishi }
106c8a11c8aSRyusuke Konishi
107783f6184SRyusuke Konishi /**
108cae3d4caSRyusuke Konishi * __nilfs_error() - report failure condition on a filesystem
109783f6184SRyusuke Konishi *
110cae3d4caSRyusuke Konishi * __nilfs_error() sets an ERROR_FS flag on the superblock as well as
111cae3d4caSRyusuke Konishi * reporting an error message. This function should be called when
112cae3d4caSRyusuke Konishi * NILFS detects incoherences or defects of meta data on disk.
113783f6184SRyusuke Konishi *
114cae3d4caSRyusuke Konishi * This implements the body of nilfs_error() macro. Normally,
115cae3d4caSRyusuke Konishi * nilfs_error() should be used. As for sustainable errors such as a
116a1d0747aSJoe Perches * single-shot I/O error, nilfs_err() should be used instead.
117cae3d4caSRyusuke Konishi *
118cae3d4caSRyusuke Konishi * Callers should not add a trailing newline since this will do it.
119783f6184SRyusuke Konishi */
__nilfs_error(struct super_block * sb,const char * function,const char * fmt,...)120cae3d4caSRyusuke Konishi void __nilfs_error(struct super_block *sb, const char *function,
121783f6184SRyusuke Konishi const char *fmt, ...)
122783f6184SRyusuke Konishi {
123e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
124b004a5ebSJoe Perches struct va_format vaf;
125783f6184SRyusuke Konishi va_list args;
126783f6184SRyusuke Konishi
127783f6184SRyusuke Konishi va_start(args, fmt);
128b004a5ebSJoe Perches
129b004a5ebSJoe Perches vaf.fmt = fmt;
130b004a5ebSJoe Perches vaf.va = &args;
131b004a5ebSJoe Perches
132b004a5ebSJoe Perches printk(KERN_CRIT "NILFS error (device %s): %s: %pV\n",
133b004a5ebSJoe Perches sb->s_id, function, &vaf);
134b004a5ebSJoe Perches
135783f6184SRyusuke Konishi va_end(args);
136783f6184SRyusuke Konishi
137bc98a42cSDavid Howells if (!sb_rdonly(sb)) {
138f7545144SRyusuke Konishi nilfs_set_error(sb);
139783f6184SRyusuke Konishi
1403b2ce58bSRyusuke Konishi if (nilfs_test_opt(nilfs, ERRORS_RO)) {
141783f6184SRyusuke Konishi printk(KERN_CRIT "Remounting filesystem read-only\n");
1421751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY;
143783f6184SRyusuke Konishi }
144783f6184SRyusuke Konishi }
145783f6184SRyusuke Konishi
1463b2ce58bSRyusuke Konishi if (nilfs_test_opt(nilfs, ERRORS_PANIC))
147783f6184SRyusuke Konishi panic("NILFS (device %s): panic forced after error\n",
148783f6184SRyusuke Konishi sb->s_id);
149783f6184SRyusuke Konishi }
150783f6184SRyusuke Konishi
nilfs_alloc_inode(struct super_block * sb)1512879ed66SRyusuke Konishi struct inode *nilfs_alloc_inode(struct super_block *sb)
152783f6184SRyusuke Konishi {
153783f6184SRyusuke Konishi struct nilfs_inode_info *ii;
154783f6184SRyusuke Konishi
155fd60b288SMuchun Song ii = alloc_inode_sb(sb, nilfs_inode_cachep, GFP_NOFS);
156783f6184SRyusuke Konishi if (!ii)
157783f6184SRyusuke Konishi return NULL;
158783f6184SRyusuke Konishi ii->i_bh = NULL;
159783f6184SRyusuke Konishi ii->i_state = 0;
1600e14a359SRyusuke Konishi ii->i_cno = 0;
161e897be17SRyusuke Konishi ii->i_assoc_inode = NULL;
162e897be17SRyusuke Konishi ii->i_bmap = &ii->i_bmap_data;
163783f6184SRyusuke Konishi return &ii->vfs_inode;
164783f6184SRyusuke Konishi }
165783f6184SRyusuke Konishi
nilfs_free_inode(struct inode * inode)166977c3d18SAl Viro static void nilfs_free_inode(struct inode *inode)
167783f6184SRyusuke Konishi {
1682d19961dSRyusuke Konishi if (nilfs_is_metadata_file_inode(inode))
1692d19961dSRyusuke Konishi nilfs_mdt_destroy(inode);
1702d19961dSRyusuke Konishi
171783f6184SRyusuke Konishi kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
172783f6184SRyusuke Konishi }
173783f6184SRyusuke Konishi
nilfs_sync_super(struct super_block * sb,int flag)174f7545144SRyusuke Konishi static int nilfs_sync_super(struct super_block *sb, int flag)
175783f6184SRyusuke Konishi {
176e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
177783f6184SRyusuke Konishi int err;
178783f6184SRyusuke Konishi
179783f6184SRyusuke Konishi retry:
180e339ad31SRyusuke Konishi set_buffer_dirty(nilfs->ns_sbh[0]);
1813b2ce58bSRyusuke Konishi if (nilfs_test_opt(nilfs, BARRIER)) {
18287e99511SChristoph Hellwig err = __sync_dirty_buffer(nilfs->ns_sbh[0],
18370fd7614SChristoph Hellwig REQ_SYNC | REQ_PREFLUSH | REQ_FUA);
18487e99511SChristoph Hellwig } else {
18587e99511SChristoph Hellwig err = sync_dirty_buffer(nilfs->ns_sbh[0]);
18687e99511SChristoph Hellwig }
18787e99511SChristoph Hellwig
188e339ad31SRyusuke Konishi if (unlikely(err)) {
189a1d0747aSJoe Perches nilfs_err(sb, "unable to write superblock: err=%d", err);
190e339ad31SRyusuke Konishi if (err == -EIO && nilfs->ns_sbh[1]) {
191b2ac86e1SJiro SEKIBA /*
192b2ac86e1SJiro SEKIBA * sbp[0] points to newer log than sbp[1],
193b2ac86e1SJiro SEKIBA * so copy sbp[0] to sbp[1] to take over sbp[0].
194b2ac86e1SJiro SEKIBA */
195b2ac86e1SJiro SEKIBA memcpy(nilfs->ns_sbp[1], nilfs->ns_sbp[0],
196b2ac86e1SJiro SEKIBA nilfs->ns_sbsize);
197e339ad31SRyusuke Konishi nilfs_fall_back_super_block(nilfs);
198e339ad31SRyusuke Konishi goto retry;
199e339ad31SRyusuke Konishi }
200e339ad31SRyusuke Konishi } else {
201e339ad31SRyusuke Konishi struct nilfs_super_block *sbp = nilfs->ns_sbp[0];
202e339ad31SRyusuke Konishi
203b2ac86e1SJiro SEKIBA nilfs->ns_sbwcount++;
204b2ac86e1SJiro SEKIBA
205e339ad31SRyusuke Konishi /*
206e339ad31SRyusuke Konishi * The latest segment becomes trailable from the position
207e339ad31SRyusuke Konishi * written in superblock.
208e339ad31SRyusuke Konishi */
209783f6184SRyusuke Konishi clear_nilfs_discontinued(nilfs);
210e339ad31SRyusuke Konishi
211e339ad31SRyusuke Konishi /* update GC protection for recent segments */
212e339ad31SRyusuke Konishi if (nilfs->ns_sbh[1]) {
213b2ac86e1SJiro SEKIBA if (flag == NILFS_SB_COMMIT_ALL) {
214e339ad31SRyusuke Konishi set_buffer_dirty(nilfs->ns_sbh[1]);
215b2ac86e1SJiro SEKIBA if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0)
216b2ac86e1SJiro SEKIBA goto out;
217b2ac86e1SJiro SEKIBA }
218b2ac86e1SJiro SEKIBA if (le64_to_cpu(nilfs->ns_sbp[1]->s_last_cno) <
219b2ac86e1SJiro SEKIBA le64_to_cpu(nilfs->ns_sbp[0]->s_last_cno))
220e339ad31SRyusuke Konishi sbp = nilfs->ns_sbp[1];
221e339ad31SRyusuke Konishi }
222b2ac86e1SJiro SEKIBA
2232c2e52fcSRyusuke Konishi spin_lock(&nilfs->ns_last_segment_lock);
224e339ad31SRyusuke Konishi nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq);
2252c2e52fcSRyusuke Konishi spin_unlock(&nilfs->ns_last_segment_lock);
226783f6184SRyusuke Konishi }
227b2ac86e1SJiro SEKIBA out:
228783f6184SRyusuke Konishi return err;
229783f6184SRyusuke Konishi }
230783f6184SRyusuke Konishi
nilfs_set_log_cursor(struct nilfs_super_block * sbp,struct the_nilfs * nilfs)23160f46b7eSRyusuke Konishi void nilfs_set_log_cursor(struct nilfs_super_block *sbp,
23260f46b7eSRyusuke Konishi struct the_nilfs *nilfs)
23360f46b7eSRyusuke Konishi {
23460f46b7eSRyusuke Konishi sector_t nfreeblocks;
23560f46b7eSRyusuke Konishi
23660f46b7eSRyusuke Konishi /* nilfs->ns_sem must be locked by the caller. */
23760f46b7eSRyusuke Konishi nilfs_count_free_blocks(nilfs, &nfreeblocks);
23860f46b7eSRyusuke Konishi sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks);
23960f46b7eSRyusuke Konishi
24060f46b7eSRyusuke Konishi spin_lock(&nilfs->ns_last_segment_lock);
24160f46b7eSRyusuke Konishi sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq);
24260f46b7eSRyusuke Konishi sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg);
24360f46b7eSRyusuke Konishi sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno);
24460f46b7eSRyusuke Konishi spin_unlock(&nilfs->ns_last_segment_lock);
24560f46b7eSRyusuke Konishi }
24660f46b7eSRyusuke Konishi
nilfs_prepare_super(struct super_block * sb,int flip)247f7545144SRyusuke Konishi struct nilfs_super_block **nilfs_prepare_super(struct super_block *sb,
248b2ac86e1SJiro SEKIBA int flip)
249d26493b6SJiro SEKIBA {
250e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
251d26493b6SJiro SEKIBA struct nilfs_super_block **sbp = nilfs->ns_sbp;
252d26493b6SJiro SEKIBA
253d26493b6SJiro SEKIBA /* nilfs->ns_sem must be locked by the caller. */
254d26493b6SJiro SEKIBA if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
255d26493b6SJiro SEKIBA if (sbp[1] &&
256d26493b6SJiro SEKIBA sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) {
257b2ac86e1SJiro SEKIBA memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
258d26493b6SJiro SEKIBA } else {
259a1d0747aSJoe Perches nilfs_crit(sb, "superblock broke");
260d26493b6SJiro SEKIBA return NULL;
261d26493b6SJiro SEKIBA }
262b2ac86e1SJiro SEKIBA } else if (sbp[1] &&
263b2ac86e1SJiro SEKIBA sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) {
264b2ac86e1SJiro SEKIBA memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
265d26493b6SJiro SEKIBA }
266b2ac86e1SJiro SEKIBA
267b2ac86e1SJiro SEKIBA if (flip && sbp[1])
268b2ac86e1SJiro SEKIBA nilfs_swap_super_block(nilfs);
269b2ac86e1SJiro SEKIBA
270d26493b6SJiro SEKIBA return sbp;
271d26493b6SJiro SEKIBA }
272d26493b6SJiro SEKIBA
nilfs_commit_super(struct super_block * sb,int flag)273f7545144SRyusuke Konishi int nilfs_commit_super(struct super_block *sb, int flag)
274783f6184SRyusuke Konishi {
275e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
276e339ad31SRyusuke Konishi struct nilfs_super_block **sbp = nilfs->ns_sbp;
277fb04b91bSArnd Bergmann time64_t t;
278783f6184SRyusuke Konishi
279d26493b6SJiro SEKIBA /* nilfs->ns_sem must be locked by the caller. */
280fb04b91bSArnd Bergmann t = ktime_get_real_seconds();
281b2ac86e1SJiro SEKIBA nilfs->ns_sbwtime = t;
282e339ad31SRyusuke Konishi sbp[0]->s_wtime = cpu_to_le64(t);
283e339ad31SRyusuke Konishi sbp[0]->s_sum = 0;
284e339ad31SRyusuke Konishi sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
285e339ad31SRyusuke Konishi (unsigned char *)sbp[0],
286e339ad31SRyusuke Konishi nilfs->ns_sbsize));
287b2ac86e1SJiro SEKIBA if (flag == NILFS_SB_COMMIT_ALL && sbp[1]) {
288b2ac86e1SJiro SEKIBA sbp[1]->s_wtime = sbp[0]->s_wtime;
289b2ac86e1SJiro SEKIBA sbp[1]->s_sum = 0;
290b2ac86e1SJiro SEKIBA sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed,
291b2ac86e1SJiro SEKIBA (unsigned char *)sbp[1],
292b2ac86e1SJiro SEKIBA nilfs->ns_sbsize));
293e339ad31SRyusuke Konishi }
294e605f0a7SRyusuke Konishi clear_nilfs_sb_dirty(nilfs);
295e2c7617aSAndreas Rohner nilfs->ns_flushed_device = 1;
296e2c7617aSAndreas Rohner /* make sure store to ns_flushed_device cannot be reordered */
297e2c7617aSAndreas Rohner smp_wmb();
298f7545144SRyusuke Konishi return nilfs_sync_super(sb, flag);
299783f6184SRyusuke Konishi }
300783f6184SRyusuke Konishi
3017ecaa46cSRyusuke Konishi /**
3027ecaa46cSRyusuke Konishi * nilfs_cleanup_super() - write filesystem state for cleanup
303f7545144SRyusuke Konishi * @sb: super block instance to be unmounted or degraded to read-only
3047ecaa46cSRyusuke Konishi *
3057ecaa46cSRyusuke Konishi * This function restores state flags in the on-disk super block.
3067ecaa46cSRyusuke Konishi * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the
3077ecaa46cSRyusuke Konishi * filesystem was not clean previously.
3087ecaa46cSRyusuke Konishi */
nilfs_cleanup_super(struct super_block * sb)309f7545144SRyusuke Konishi int nilfs_cleanup_super(struct super_block *sb)
3107ecaa46cSRyusuke Konishi {
311e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
312d26493b6SJiro SEKIBA struct nilfs_super_block **sbp;
313b2ac86e1SJiro SEKIBA int flag = NILFS_SB_COMMIT;
314d26493b6SJiro SEKIBA int ret = -EIO;
3157ecaa46cSRyusuke Konishi
316f7545144SRyusuke Konishi sbp = nilfs_prepare_super(sb, 0);
317d26493b6SJiro SEKIBA if (sbp) {
318f7545144SRyusuke Konishi sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
319f7545144SRyusuke Konishi nilfs_set_log_cursor(sbp[0], nilfs);
320b2ac86e1SJiro SEKIBA if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) {
321b2ac86e1SJiro SEKIBA /*
322b2ac86e1SJiro SEKIBA * make the "clean" flag also to the opposite
323b2ac86e1SJiro SEKIBA * super block if both super blocks point to
324b2ac86e1SJiro SEKIBA * the same checkpoint.
325b2ac86e1SJiro SEKIBA */
326b2ac86e1SJiro SEKIBA sbp[1]->s_state = sbp[0]->s_state;
327b2ac86e1SJiro SEKIBA flag = NILFS_SB_COMMIT_ALL;
328b2ac86e1SJiro SEKIBA }
329f7545144SRyusuke Konishi ret = nilfs_commit_super(sb, flag);
330d26493b6SJiro SEKIBA }
3317ecaa46cSRyusuke Konishi return ret;
3327ecaa46cSRyusuke Konishi }
3337ecaa46cSRyusuke Konishi
334cfb0a4bfSRyusuke Konishi /**
335cfb0a4bfSRyusuke Konishi * nilfs_move_2nd_super - relocate secondary super block
336cfb0a4bfSRyusuke Konishi * @sb: super block instance
337cfb0a4bfSRyusuke Konishi * @sb2off: new offset of the secondary super block (in bytes)
338cfb0a4bfSRyusuke Konishi */
nilfs_move_2nd_super(struct super_block * sb,loff_t sb2off)339cfb0a4bfSRyusuke Konishi static int nilfs_move_2nd_super(struct super_block *sb, loff_t sb2off)
340cfb0a4bfSRyusuke Konishi {
341cfb0a4bfSRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
342cfb0a4bfSRyusuke Konishi struct buffer_head *nsbh;
343cfb0a4bfSRyusuke Konishi struct nilfs_super_block *nsbp;
344cfb0a4bfSRyusuke Konishi sector_t blocknr, newblocknr;
345cfb0a4bfSRyusuke Konishi unsigned long offset;
3464f05028fSRyusuke Konishi int sb2i; /* array index of the secondary superblock */
347cfb0a4bfSRyusuke Konishi int ret = 0;
348cfb0a4bfSRyusuke Konishi
349cfb0a4bfSRyusuke Konishi /* nilfs->ns_sem must be locked by the caller. */
350cfb0a4bfSRyusuke Konishi if (nilfs->ns_sbh[1] &&
351cfb0a4bfSRyusuke Konishi nilfs->ns_sbh[1]->b_blocknr > nilfs->ns_first_data_block) {
352cfb0a4bfSRyusuke Konishi sb2i = 1;
353cfb0a4bfSRyusuke Konishi blocknr = nilfs->ns_sbh[1]->b_blocknr;
354cfb0a4bfSRyusuke Konishi } else if (nilfs->ns_sbh[0]->b_blocknr > nilfs->ns_first_data_block) {
355cfb0a4bfSRyusuke Konishi sb2i = 0;
356cfb0a4bfSRyusuke Konishi blocknr = nilfs->ns_sbh[0]->b_blocknr;
3574f05028fSRyusuke Konishi } else {
3584f05028fSRyusuke Konishi sb2i = -1;
3594f05028fSRyusuke Konishi blocknr = 0;
360cfb0a4bfSRyusuke Konishi }
361cfb0a4bfSRyusuke Konishi if (sb2i >= 0 && (u64)blocknr << nilfs->ns_blocksize_bits == sb2off)
362cfb0a4bfSRyusuke Konishi goto out; /* super block location is unchanged */
363cfb0a4bfSRyusuke Konishi
364cfb0a4bfSRyusuke Konishi /* Get new super block buffer */
365cfb0a4bfSRyusuke Konishi newblocknr = sb2off >> nilfs->ns_blocksize_bits;
366cfb0a4bfSRyusuke Konishi offset = sb2off & (nilfs->ns_blocksize - 1);
367cfb0a4bfSRyusuke Konishi nsbh = sb_getblk(sb, newblocknr);
368cfb0a4bfSRyusuke Konishi if (!nsbh) {
369a1d0747aSJoe Perches nilfs_warn(sb,
370feee880fSRyusuke Konishi "unable to move secondary superblock to block %llu",
371feee880fSRyusuke Konishi (unsigned long long)newblocknr);
372cfb0a4bfSRyusuke Konishi ret = -EIO;
373cfb0a4bfSRyusuke Konishi goto out;
374cfb0a4bfSRyusuke Konishi }
375cfb0a4bfSRyusuke Konishi nsbp = (void *)nsbh->b_data + offset;
376679bd7ebSRyusuke Konishi
377679bd7ebSRyusuke Konishi lock_buffer(nsbh);
378679bd7ebSRyusuke Konishi if (sb2i >= 0) {
379679bd7ebSRyusuke Konishi /*
380679bd7ebSRyusuke Konishi * The position of the second superblock only changes by 4KiB,
381679bd7ebSRyusuke Konishi * which is larger than the maximum superblock data size
382679bd7ebSRyusuke Konishi * (= 1KiB), so there is no need to use memmove() to allow
383679bd7ebSRyusuke Konishi * overlap between source and destination.
384679bd7ebSRyusuke Konishi */
385679bd7ebSRyusuke Konishi memcpy(nsbp, nilfs->ns_sbp[sb2i], nilfs->ns_sbsize);
386679bd7ebSRyusuke Konishi
387679bd7ebSRyusuke Konishi /*
388679bd7ebSRyusuke Konishi * Zero fill after copy to avoid overwriting in case of move
389679bd7ebSRyusuke Konishi * within the same block.
390679bd7ebSRyusuke Konishi */
391679bd7ebSRyusuke Konishi memset(nsbh->b_data, 0, offset);
392679bd7ebSRyusuke Konishi memset((void *)nsbp + nilfs->ns_sbsize, 0,
393679bd7ebSRyusuke Konishi nsbh->b_size - offset - nilfs->ns_sbsize);
394679bd7ebSRyusuke Konishi } else {
395679bd7ebSRyusuke Konishi memset(nsbh->b_data, 0, nsbh->b_size);
396679bd7ebSRyusuke Konishi }
397679bd7ebSRyusuke Konishi set_buffer_uptodate(nsbh);
398679bd7ebSRyusuke Konishi unlock_buffer(nsbh);
399cfb0a4bfSRyusuke Konishi
400cfb0a4bfSRyusuke Konishi if (sb2i >= 0) {
401cfb0a4bfSRyusuke Konishi brelse(nilfs->ns_sbh[sb2i]);
402cfb0a4bfSRyusuke Konishi nilfs->ns_sbh[sb2i] = nsbh;
403cfb0a4bfSRyusuke Konishi nilfs->ns_sbp[sb2i] = nsbp;
404cfb0a4bfSRyusuke Konishi } else if (nilfs->ns_sbh[0]->b_blocknr < nilfs->ns_first_data_block) {
405cfb0a4bfSRyusuke Konishi /* secondary super block will be restored to index 1 */
406cfb0a4bfSRyusuke Konishi nilfs->ns_sbh[1] = nsbh;
407cfb0a4bfSRyusuke Konishi nilfs->ns_sbp[1] = nsbp;
408cfb0a4bfSRyusuke Konishi } else {
409cfb0a4bfSRyusuke Konishi brelse(nsbh);
410cfb0a4bfSRyusuke Konishi }
411cfb0a4bfSRyusuke Konishi out:
412cfb0a4bfSRyusuke Konishi return ret;
413cfb0a4bfSRyusuke Konishi }
414cfb0a4bfSRyusuke Konishi
4154e33f9eaSRyusuke Konishi /**
4164e33f9eaSRyusuke Konishi * nilfs_resize_fs - resize the filesystem
4174e33f9eaSRyusuke Konishi * @sb: super block instance
4184e33f9eaSRyusuke Konishi * @newsize: new size of the filesystem (in bytes)
4194e33f9eaSRyusuke Konishi */
nilfs_resize_fs(struct super_block * sb,__u64 newsize)4204e33f9eaSRyusuke Konishi int nilfs_resize_fs(struct super_block *sb, __u64 newsize)
4214e33f9eaSRyusuke Konishi {
4224e33f9eaSRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
4234e33f9eaSRyusuke Konishi struct nilfs_super_block **sbp;
4244e33f9eaSRyusuke Konishi __u64 devsize, newnsegs;
4254e33f9eaSRyusuke Konishi loff_t sb2off;
4264e33f9eaSRyusuke Konishi int ret;
4274e33f9eaSRyusuke Konishi
4284e33f9eaSRyusuke Konishi ret = -ERANGE;
4294fcd6979SChristoph Hellwig devsize = bdev_nr_bytes(sb->s_bdev);
4304e33f9eaSRyusuke Konishi if (newsize > devsize)
4314e33f9eaSRyusuke Konishi goto out;
4324e33f9eaSRyusuke Konishi
4334e33f9eaSRyusuke Konishi /*
43499b9402aSRyusuke Konishi * Prevent underflow in second superblock position calculation.
43599b9402aSRyusuke Konishi * The exact minimum size check is done in nilfs_sufile_resize().
43699b9402aSRyusuke Konishi */
43799b9402aSRyusuke Konishi if (newsize < 4096) {
43899b9402aSRyusuke Konishi ret = -ENOSPC;
43999b9402aSRyusuke Konishi goto out;
44099b9402aSRyusuke Konishi }
44199b9402aSRyusuke Konishi
44299b9402aSRyusuke Konishi /*
4434e33f9eaSRyusuke Konishi * Write lock is required to protect some functions depending
4444e33f9eaSRyusuke Konishi * on the number of segments, the number of reserved segments,
4454e33f9eaSRyusuke Konishi * and so forth.
4464e33f9eaSRyusuke Konishi */
4474e33f9eaSRyusuke Konishi down_write(&nilfs->ns_segctor_sem);
4484e33f9eaSRyusuke Konishi
4494e33f9eaSRyusuke Konishi sb2off = NILFS_SB2_OFFSET_BYTES(newsize);
4504e33f9eaSRyusuke Konishi newnsegs = sb2off >> nilfs->ns_blocksize_bits;
4514e33f9eaSRyusuke Konishi do_div(newnsegs, nilfs->ns_blocks_per_segment);
4524e33f9eaSRyusuke Konishi
4534e33f9eaSRyusuke Konishi ret = nilfs_sufile_resize(nilfs->ns_sufile, newnsegs);
4544e33f9eaSRyusuke Konishi up_write(&nilfs->ns_segctor_sem);
4554e33f9eaSRyusuke Konishi if (ret < 0)
4564e33f9eaSRyusuke Konishi goto out;
4574e33f9eaSRyusuke Konishi
4584e33f9eaSRyusuke Konishi ret = nilfs_construct_segment(sb);
4594e33f9eaSRyusuke Konishi if (ret < 0)
4604e33f9eaSRyusuke Konishi goto out;
4614e33f9eaSRyusuke Konishi
4624e33f9eaSRyusuke Konishi down_write(&nilfs->ns_sem);
4634e33f9eaSRyusuke Konishi nilfs_move_2nd_super(sb, sb2off);
4644e33f9eaSRyusuke Konishi ret = -EIO;
4654e33f9eaSRyusuke Konishi sbp = nilfs_prepare_super(sb, 0);
4664e33f9eaSRyusuke Konishi if (likely(sbp)) {
4674e33f9eaSRyusuke Konishi nilfs_set_log_cursor(sbp[0], nilfs);
4684e33f9eaSRyusuke Konishi /*
4694e33f9eaSRyusuke Konishi * Drop NILFS_RESIZE_FS flag for compatibility with
4704e33f9eaSRyusuke Konishi * mount-time resize which may be implemented in a
4714e33f9eaSRyusuke Konishi * future release.
4724e33f9eaSRyusuke Konishi */
4734e33f9eaSRyusuke Konishi sbp[0]->s_state = cpu_to_le16(le16_to_cpu(sbp[0]->s_state) &
4744e33f9eaSRyusuke Konishi ~NILFS_RESIZE_FS);
4754e33f9eaSRyusuke Konishi sbp[0]->s_dev_size = cpu_to_le64(newsize);
4764e33f9eaSRyusuke Konishi sbp[0]->s_nsegments = cpu_to_le64(nilfs->ns_nsegments);
4774e33f9eaSRyusuke Konishi if (sbp[1])
4784e33f9eaSRyusuke Konishi memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
4794e33f9eaSRyusuke Konishi ret = nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
4804e33f9eaSRyusuke Konishi }
4814e33f9eaSRyusuke Konishi up_write(&nilfs->ns_sem);
4824e33f9eaSRyusuke Konishi
4834e33f9eaSRyusuke Konishi /*
4844e33f9eaSRyusuke Konishi * Reset the range of allocatable segments last. This order
4854e33f9eaSRyusuke Konishi * is important in the case of expansion because the secondary
4864e33f9eaSRyusuke Konishi * superblock must be protected from log write until migration
4874e33f9eaSRyusuke Konishi * completes.
4884e33f9eaSRyusuke Konishi */
4894e33f9eaSRyusuke Konishi if (!ret)
4904e33f9eaSRyusuke Konishi nilfs_sufile_set_alloc_range(nilfs->ns_sufile, 0, newnsegs - 1);
4914e33f9eaSRyusuke Konishi out:
4924e33f9eaSRyusuke Konishi return ret;
4934e33f9eaSRyusuke Konishi }
4944e33f9eaSRyusuke Konishi
nilfs_put_super(struct super_block * sb)495783f6184SRyusuke Konishi static void nilfs_put_super(struct super_block *sb)
496783f6184SRyusuke Konishi {
497e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
498783f6184SRyusuke Konishi
499f7545144SRyusuke Konishi nilfs_detach_log_writer(sb);
500783f6184SRyusuke Konishi
501bc98a42cSDavid Howells if (!sb_rdonly(sb)) {
502783f6184SRyusuke Konishi down_write(&nilfs->ns_sem);
503f7545144SRyusuke Konishi nilfs_cleanup_super(sb);
504783f6184SRyusuke Konishi up_write(&nilfs->ns_sem);
505783f6184SRyusuke Konishi }
506783f6184SRyusuke Konishi
50742560f9cSRyusuke Konishi nilfs_sysfs_delete_device_group(nilfs);
508f1e89c86SRyusuke Konishi iput(nilfs->ns_sufile);
509f1e89c86SRyusuke Konishi iput(nilfs->ns_cpfile);
510f1e89c86SRyusuke Konishi iput(nilfs->ns_dat);
511f1e89c86SRyusuke Konishi
512348fe8daSRyusuke Konishi destroy_nilfs(nilfs);
513783f6184SRyusuke Konishi sb->s_fs_info = NULL;
514783f6184SRyusuke Konishi }
515783f6184SRyusuke Konishi
nilfs_sync_fs(struct super_block * sb,int wait)516783f6184SRyusuke Konishi static int nilfs_sync_fs(struct super_block *sb, int wait)
517783f6184SRyusuke Konishi {
518e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
519d26493b6SJiro SEKIBA struct nilfs_super_block **sbp;
520783f6184SRyusuke Konishi int err = 0;
521783f6184SRyusuke Konishi
522783f6184SRyusuke Konishi /* This function is called when super block should be written back */
523783f6184SRyusuke Konishi if (wait)
524783f6184SRyusuke Konishi err = nilfs_construct_segment(sb);
5256233caa9SJiro SEKIBA
5266233caa9SJiro SEKIBA down_write(&nilfs->ns_sem);
527d26493b6SJiro SEKIBA if (nilfs_sb_dirty(nilfs)) {
528f7545144SRyusuke Konishi sbp = nilfs_prepare_super(sb, nilfs_sb_will_flip(nilfs));
529b2ac86e1SJiro SEKIBA if (likely(sbp)) {
530b2ac86e1SJiro SEKIBA nilfs_set_log_cursor(sbp[0], nilfs);
531f7545144SRyusuke Konishi nilfs_commit_super(sb, NILFS_SB_COMMIT);
532b2ac86e1SJiro SEKIBA }
533d26493b6SJiro SEKIBA }
5346233caa9SJiro SEKIBA up_write(&nilfs->ns_sem);
5356233caa9SJiro SEKIBA
536e2c7617aSAndreas Rohner if (!err)
537e2c7617aSAndreas Rohner err = nilfs_flush_device(nilfs);
538e2c7617aSAndreas Rohner
539783f6184SRyusuke Konishi return err;
540783f6184SRyusuke Konishi }
541783f6184SRyusuke Konishi
nilfs_attach_checkpoint(struct super_block * sb,__u64 cno,int curr_mnt,struct nilfs_root ** rootp)542f7545144SRyusuke Konishi int nilfs_attach_checkpoint(struct super_block *sb, __u64 cno, int curr_mnt,
5434d8d9293SRyusuke Konishi struct nilfs_root **rootp)
544783f6184SRyusuke Konishi {
545e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
5464d8d9293SRyusuke Konishi struct nilfs_root *root;
547783f6184SRyusuke Konishi struct nilfs_checkpoint *raw_cp;
548783f6184SRyusuke Konishi struct buffer_head *bh_cp;
5494d8d9293SRyusuke Konishi int err = -ENOMEM;
550783f6184SRyusuke Konishi
5514d8d9293SRyusuke Konishi root = nilfs_find_or_create_root(
5524d8d9293SRyusuke Konishi nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno);
5534d8d9293SRyusuke Konishi if (!root)
5544d8d9293SRyusuke Konishi return err;
555783f6184SRyusuke Konishi
556e912a5b6SRyusuke Konishi if (root->ifile)
557e912a5b6SRyusuke Konishi goto reuse; /* already attached checkpoint */
558783f6184SRyusuke Konishi
5591154ecbdSZhang Qiang down_read(&nilfs->ns_segctor_sem);
560783f6184SRyusuke Konishi err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
561783f6184SRyusuke Konishi &bh_cp);
5621154ecbdSZhang Qiang up_read(&nilfs->ns_segctor_sem);
563783f6184SRyusuke Konishi if (unlikely(err)) {
564783f6184SRyusuke Konishi if (err == -ENOENT || err == -EINVAL) {
565a1d0747aSJoe Perches nilfs_err(sb,
566feee880fSRyusuke Konishi "Invalid checkpoint (checkpoint number=%llu)",
567783f6184SRyusuke Konishi (unsigned long long)cno);
568783f6184SRyusuke Konishi err = -EINVAL;
569783f6184SRyusuke Konishi }
570783f6184SRyusuke Konishi goto failed;
571783f6184SRyusuke Konishi }
572f1e89c86SRyusuke Konishi
573f7545144SRyusuke Konishi err = nilfs_ifile_read(sb, root, nilfs->ns_inode_size,
574f1e89c86SRyusuke Konishi &raw_cp->cp_ifile_inode, &root->ifile);
575f1e89c86SRyusuke Konishi if (err)
576783f6184SRyusuke Konishi goto failed_bh;
577b7c06342SRyusuke Konishi
578e5f7f848SVyacheslav Dubeyko atomic64_set(&root->inodes_count,
579e5f7f848SVyacheslav Dubeyko le64_to_cpu(raw_cp->cp_inodes_count));
580e5f7f848SVyacheslav Dubeyko atomic64_set(&root->blocks_count,
581e5f7f848SVyacheslav Dubeyko le64_to_cpu(raw_cp->cp_blocks_count));
582783f6184SRyusuke Konishi
583783f6184SRyusuke Konishi nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
5844d8d9293SRyusuke Konishi
585e912a5b6SRyusuke Konishi reuse:
5864d8d9293SRyusuke Konishi *rootp = root;
587783f6184SRyusuke Konishi return 0;
588783f6184SRyusuke Konishi
589783f6184SRyusuke Konishi failed_bh:
590783f6184SRyusuke Konishi nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp);
591783f6184SRyusuke Konishi failed:
5924d8d9293SRyusuke Konishi nilfs_put_root(root);
593783f6184SRyusuke Konishi
594783f6184SRyusuke Konishi return err;
595783f6184SRyusuke Konishi }
596783f6184SRyusuke Konishi
nilfs_freeze(struct super_block * sb)5975beb6e0bSRyusuke Konishi static int nilfs_freeze(struct super_block *sb)
598783f6184SRyusuke Konishi {
599e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
6005beb6e0bSRyusuke Konishi int err;
6015beb6e0bSRyusuke Konishi
602bc98a42cSDavid Howells if (sb_rdonly(sb))
6035beb6e0bSRyusuke Konishi return 0;
6045beb6e0bSRyusuke Konishi
6055beb6e0bSRyusuke Konishi /* Mark super block clean */
6065beb6e0bSRyusuke Konishi down_write(&nilfs->ns_sem);
607f7545144SRyusuke Konishi err = nilfs_cleanup_super(sb);
6085beb6e0bSRyusuke Konishi up_write(&nilfs->ns_sem);
6095beb6e0bSRyusuke Konishi return err;
6105beb6e0bSRyusuke Konishi }
6115beb6e0bSRyusuke Konishi
nilfs_unfreeze(struct super_block * sb)6125beb6e0bSRyusuke Konishi static int nilfs_unfreeze(struct super_block *sb)
6135beb6e0bSRyusuke Konishi {
614e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
615783f6184SRyusuke Konishi
616bc98a42cSDavid Howells if (sb_rdonly(sb))
6175beb6e0bSRyusuke Konishi return 0;
6185beb6e0bSRyusuke Konishi
6195beb6e0bSRyusuke Konishi down_write(&nilfs->ns_sem);
620f7545144SRyusuke Konishi nilfs_setup_super(sb, false);
6215beb6e0bSRyusuke Konishi up_write(&nilfs->ns_sem);
6225beb6e0bSRyusuke Konishi return 0;
623783f6184SRyusuke Konishi }
624783f6184SRyusuke Konishi
nilfs_statfs(struct dentry * dentry,struct kstatfs * buf)625783f6184SRyusuke Konishi static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
626783f6184SRyusuke Konishi {
627783f6184SRyusuke Konishi struct super_block *sb = dentry->d_sb;
6282b0143b5SDavid Howells struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
629b7c06342SRyusuke Konishi struct the_nilfs *nilfs = root->nilfs;
630c306af23SRyusuke Konishi u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
631783f6184SRyusuke Konishi unsigned long long blocks;
632783f6184SRyusuke Konishi unsigned long overhead;
633783f6184SRyusuke Konishi unsigned long nrsvblocks;
634783f6184SRyusuke Konishi sector_t nfreeblocks;
635c7ef972cSVyacheslav Dubeyko u64 nmaxinodes, nfreeinodes;
636783f6184SRyusuke Konishi int err;
637783f6184SRyusuke Konishi
638783f6184SRyusuke Konishi /*
639783f6184SRyusuke Konishi * Compute all of the segment blocks
640783f6184SRyusuke Konishi *
641783f6184SRyusuke Konishi * The blocks before first segment and after last segment
642783f6184SRyusuke Konishi * are excluded.
643783f6184SRyusuke Konishi */
644783f6184SRyusuke Konishi blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments
645783f6184SRyusuke Konishi - nilfs->ns_first_data_block;
646783f6184SRyusuke Konishi nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment;
647783f6184SRyusuke Konishi
648783f6184SRyusuke Konishi /*
649783f6184SRyusuke Konishi * Compute the overhead
650783f6184SRyusuke Konishi *
6517a65004bSRyusuke Konishi * When distributing meta data blocks outside segment structure,
652783f6184SRyusuke Konishi * We must count them as the overhead.
653783f6184SRyusuke Konishi */
654783f6184SRyusuke Konishi overhead = 0;
655783f6184SRyusuke Konishi
656783f6184SRyusuke Konishi err = nilfs_count_free_blocks(nilfs, &nfreeblocks);
657783f6184SRyusuke Konishi if (unlikely(err))
658783f6184SRyusuke Konishi return err;
659783f6184SRyusuke Konishi
660c7ef972cSVyacheslav Dubeyko err = nilfs_ifile_count_free_inodes(root->ifile,
661c7ef972cSVyacheslav Dubeyko &nmaxinodes, &nfreeinodes);
662c7ef972cSVyacheslav Dubeyko if (unlikely(err)) {
663a1d0747aSJoe Perches nilfs_warn(sb, "failed to count free inodes: err=%d", err);
664c7ef972cSVyacheslav Dubeyko if (err == -ERANGE) {
665c7ef972cSVyacheslav Dubeyko /*
666c7ef972cSVyacheslav Dubeyko * If nilfs_palloc_count_max_entries() returns
667c7ef972cSVyacheslav Dubeyko * -ERANGE error code then we simply treat
668c7ef972cSVyacheslav Dubeyko * curent inodes count as maximum possible and
669c7ef972cSVyacheslav Dubeyko * zero as free inodes value.
670c7ef972cSVyacheslav Dubeyko */
671e5f7f848SVyacheslav Dubeyko nmaxinodes = atomic64_read(&root->inodes_count);
672c7ef972cSVyacheslav Dubeyko nfreeinodes = 0;
673c7ef972cSVyacheslav Dubeyko err = 0;
674c7ef972cSVyacheslav Dubeyko } else
675c7ef972cSVyacheslav Dubeyko return err;
676c7ef972cSVyacheslav Dubeyko }
677c7ef972cSVyacheslav Dubeyko
678783f6184SRyusuke Konishi buf->f_type = NILFS_SUPER_MAGIC;
679783f6184SRyusuke Konishi buf->f_bsize = sb->s_blocksize;
680783f6184SRyusuke Konishi buf->f_blocks = blocks - overhead;
681783f6184SRyusuke Konishi buf->f_bfree = nfreeblocks;
682783f6184SRyusuke Konishi buf->f_bavail = (buf->f_bfree >= nrsvblocks) ?
683783f6184SRyusuke Konishi (buf->f_bfree - nrsvblocks) : 0;
684c7ef972cSVyacheslav Dubeyko buf->f_files = nmaxinodes;
685c7ef972cSVyacheslav Dubeyko buf->f_ffree = nfreeinodes;
686783f6184SRyusuke Konishi buf->f_namelen = NILFS_NAME_LEN;
6876d1349c7SAl Viro buf->f_fsid = u64_to_fsid(id);
688c306af23SRyusuke Konishi
689783f6184SRyusuke Konishi return 0;
690783f6184SRyusuke Konishi }
691783f6184SRyusuke Konishi
nilfs_show_options(struct seq_file * seq,struct dentry * dentry)69234c80b1dSAl Viro static int nilfs_show_options(struct seq_file *seq, struct dentry *dentry)
693b58a285bSJiro SEKIBA {
69434c80b1dSAl Viro struct super_block *sb = dentry->d_sb;
695e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
6962b0143b5SDavid Howells struct nilfs_root *root = NILFS_I(d_inode(dentry))->i_root;
697b58a285bSJiro SEKIBA
6983b2ce58bSRyusuke Konishi if (!nilfs_test_opt(nilfs, BARRIER))
699c6b4d57dSRyusuke Konishi seq_puts(seq, ",nobarrier");
700f11459adSRyusuke Konishi if (root->cno != NILFS_CPTREE_CURRENT_CNO)
701f11459adSRyusuke Konishi seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno);
7023b2ce58bSRyusuke Konishi if (nilfs_test_opt(nilfs, ERRORS_PANIC))
703c6b4d57dSRyusuke Konishi seq_puts(seq, ",errors=panic");
7043b2ce58bSRyusuke Konishi if (nilfs_test_opt(nilfs, ERRORS_CONT))
705c6b4d57dSRyusuke Konishi seq_puts(seq, ",errors=continue");
7063b2ce58bSRyusuke Konishi if (nilfs_test_opt(nilfs, STRICT_ORDER))
707c6b4d57dSRyusuke Konishi seq_puts(seq, ",order=strict");
7083b2ce58bSRyusuke Konishi if (nilfs_test_opt(nilfs, NORECOVERY))
709c6b4d57dSRyusuke Konishi seq_puts(seq, ",norecovery");
7103b2ce58bSRyusuke Konishi if (nilfs_test_opt(nilfs, DISCARD))
711c6b4d57dSRyusuke Konishi seq_puts(seq, ",discard");
712b58a285bSJiro SEKIBA
713b58a285bSJiro SEKIBA return 0;
714b58a285bSJiro SEKIBA }
715b58a285bSJiro SEKIBA
716b87221deSAlexey Dobriyan static const struct super_operations nilfs_sops = {
717783f6184SRyusuke Konishi .alloc_inode = nilfs_alloc_inode,
718977c3d18SAl Viro .free_inode = nilfs_free_inode,
719783f6184SRyusuke Konishi .dirty_inode = nilfs_dirty_inode,
7206fd1e5c9SAl Viro .evict_inode = nilfs_evict_inode,
721783f6184SRyusuke Konishi .put_super = nilfs_put_super,
722783f6184SRyusuke Konishi .sync_fs = nilfs_sync_fs,
7235beb6e0bSRyusuke Konishi .freeze_fs = nilfs_freeze,
7245beb6e0bSRyusuke Konishi .unfreeze_fs = nilfs_unfreeze,
725783f6184SRyusuke Konishi .statfs = nilfs_statfs,
726783f6184SRyusuke Konishi .remount_fs = nilfs_remount,
727b58a285bSJiro SEKIBA .show_options = nilfs_show_options
728783f6184SRyusuke Konishi };
729783f6184SRyusuke Konishi
730783f6184SRyusuke Konishi enum {
731783f6184SRyusuke Konishi Opt_err_cont, Opt_err_panic, Opt_err_ro,
732773bc4f3SRyusuke Konishi Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery,
733802d3177SRyusuke Konishi Opt_discard, Opt_nodiscard, Opt_err,
734783f6184SRyusuke Konishi };
735783f6184SRyusuke Konishi
736783f6184SRyusuke Konishi static match_table_t tokens = {
737783f6184SRyusuke Konishi {Opt_err_cont, "errors=continue"},
738783f6184SRyusuke Konishi {Opt_err_panic, "errors=panic"},
739783f6184SRyusuke Konishi {Opt_err_ro, "errors=remount-ro"},
740773bc4f3SRyusuke Konishi {Opt_barrier, "barrier"},
74191f1953bSJiro SEKIBA {Opt_nobarrier, "nobarrier"},
742783f6184SRyusuke Konishi {Opt_snapshot, "cp=%u"},
743783f6184SRyusuke Konishi {Opt_order, "order=%s"},
7440234576dSRyusuke Konishi {Opt_norecovery, "norecovery"},
745e902ec99SJiro SEKIBA {Opt_discard, "discard"},
746802d3177SRyusuke Konishi {Opt_nodiscard, "nodiscard"},
747783f6184SRyusuke Konishi {Opt_err, NULL}
748783f6184SRyusuke Konishi };
749783f6184SRyusuke Konishi
parse_options(char * options,struct super_block * sb,int is_remount)7507c017457SRyusuke Konishi static int parse_options(char *options, struct super_block *sb, int is_remount)
751783f6184SRyusuke Konishi {
752e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
753783f6184SRyusuke Konishi char *p;
754783f6184SRyusuke Konishi substring_t args[MAX_OPT_ARGS];
755783f6184SRyusuke Konishi
756783f6184SRyusuke Konishi if (!options)
757783f6184SRyusuke Konishi return 1;
758783f6184SRyusuke Konishi
759783f6184SRyusuke Konishi while ((p = strsep(&options, ",")) != NULL) {
760783f6184SRyusuke Konishi int token;
7614ad364caSRyusuke Konishi
762783f6184SRyusuke Konishi if (!*p)
763783f6184SRyusuke Konishi continue;
764783f6184SRyusuke Konishi
765783f6184SRyusuke Konishi token = match_token(p, tokens, args);
766783f6184SRyusuke Konishi switch (token) {
767773bc4f3SRyusuke Konishi case Opt_barrier:
7683b2ce58bSRyusuke Konishi nilfs_set_opt(nilfs, BARRIER);
769773bc4f3SRyusuke Konishi break;
77091f1953bSJiro SEKIBA case Opt_nobarrier:
7713b2ce58bSRyusuke Konishi nilfs_clear_opt(nilfs, BARRIER);
772783f6184SRyusuke Konishi break;
773783f6184SRyusuke Konishi case Opt_order:
774783f6184SRyusuke Konishi if (strcmp(args[0].from, "relaxed") == 0)
775783f6184SRyusuke Konishi /* Ordered data semantics */
7763b2ce58bSRyusuke Konishi nilfs_clear_opt(nilfs, STRICT_ORDER);
777783f6184SRyusuke Konishi else if (strcmp(args[0].from, "strict") == 0)
778783f6184SRyusuke Konishi /* Strict in-order semantics */
7793b2ce58bSRyusuke Konishi nilfs_set_opt(nilfs, STRICT_ORDER);
780783f6184SRyusuke Konishi else
781783f6184SRyusuke Konishi return 0;
782783f6184SRyusuke Konishi break;
783783f6184SRyusuke Konishi case Opt_err_panic:
7843b2ce58bSRyusuke Konishi nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_PANIC);
785783f6184SRyusuke Konishi break;
786783f6184SRyusuke Konishi case Opt_err_ro:
7873b2ce58bSRyusuke Konishi nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_RO);
788783f6184SRyusuke Konishi break;
789783f6184SRyusuke Konishi case Opt_err_cont:
7903b2ce58bSRyusuke Konishi nilfs_write_opt(nilfs, ERROR_MODE, ERRORS_CONT);
791783f6184SRyusuke Konishi break;
792783f6184SRyusuke Konishi case Opt_snapshot:
7937c017457SRyusuke Konishi if (is_remount) {
794a1d0747aSJoe Perches nilfs_err(sb,
795feee880fSRyusuke Konishi "\"%s\" option is invalid for remount",
796feee880fSRyusuke Konishi p);
7977c017457SRyusuke Konishi return 0;
7987c017457SRyusuke Konishi }
7997c017457SRyusuke Konishi break;
8000234576dSRyusuke Konishi case Opt_norecovery:
8013b2ce58bSRyusuke Konishi nilfs_set_opt(nilfs, NORECOVERY);
8020234576dSRyusuke Konishi break;
803e902ec99SJiro SEKIBA case Opt_discard:
8043b2ce58bSRyusuke Konishi nilfs_set_opt(nilfs, DISCARD);
805e902ec99SJiro SEKIBA break;
806802d3177SRyusuke Konishi case Opt_nodiscard:
8073b2ce58bSRyusuke Konishi nilfs_clear_opt(nilfs, DISCARD);
808802d3177SRyusuke Konishi break;
809783f6184SRyusuke Konishi default:
810a1d0747aSJoe Perches nilfs_err(sb, "unrecognized mount option \"%s\"", p);
811783f6184SRyusuke Konishi return 0;
812783f6184SRyusuke Konishi }
813783f6184SRyusuke Konishi }
814783f6184SRyusuke Konishi return 1;
815783f6184SRyusuke Konishi }
816783f6184SRyusuke Konishi
817783f6184SRyusuke Konishi static inline void
nilfs_set_default_options(struct super_block * sb,struct nilfs_super_block * sbp)8183b2ce58bSRyusuke Konishi nilfs_set_default_options(struct super_block *sb,
819783f6184SRyusuke Konishi struct nilfs_super_block *sbp)
820783f6184SRyusuke Konishi {
821e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
8223b2ce58bSRyusuke Konishi
8233b2ce58bSRyusuke Konishi nilfs->ns_mount_opt =
824277a6a34SRyusuke Konishi NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER;
825783f6184SRyusuke Konishi }
826783f6184SRyusuke Konishi
nilfs_setup_super(struct super_block * sb,int is_mount)827f7545144SRyusuke Konishi static int nilfs_setup_super(struct super_block *sb, int is_mount)
828783f6184SRyusuke Konishi {
829e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
830d26493b6SJiro SEKIBA struct nilfs_super_block **sbp;
831d26493b6SJiro SEKIBA int max_mnt_count;
832d26493b6SJiro SEKIBA int mnt_count;
833783f6184SRyusuke Konishi
834d26493b6SJiro SEKIBA /* nilfs->ns_sem must be locked by the caller. */
835f7545144SRyusuke Konishi sbp = nilfs_prepare_super(sb, 0);
836d26493b6SJiro SEKIBA if (!sbp)
837d26493b6SJiro SEKIBA return -EIO;
838d26493b6SJiro SEKIBA
8395beb6e0bSRyusuke Konishi if (!is_mount)
8405beb6e0bSRyusuke Konishi goto skip_mount_setup;
8415beb6e0bSRyusuke Konishi
842d26493b6SJiro SEKIBA max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count);
843d26493b6SJiro SEKIBA mnt_count = le16_to_cpu(sbp[0]->s_mnt_count);
844d26493b6SJiro SEKIBA
845f50a4c81SRyusuke Konishi if (nilfs->ns_mount_state & NILFS_ERROR_FS) {
846a1d0747aSJoe Perches nilfs_warn(sb, "mounting fs with errors");
847783f6184SRyusuke Konishi #if 0
848783f6184SRyusuke Konishi } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) {
849a1d0747aSJoe Perches nilfs_warn(sb, "maximal mount count reached");
850783f6184SRyusuke Konishi #endif
851783f6184SRyusuke Konishi }
852783f6184SRyusuke Konishi if (!max_mnt_count)
853d26493b6SJiro SEKIBA sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT);
854783f6184SRyusuke Konishi
855d26493b6SJiro SEKIBA sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1);
85621a1a52dSArnd Bergmann sbp[0]->s_mtime = cpu_to_le64(ktime_get_real_seconds());
8575beb6e0bSRyusuke Konishi
8585beb6e0bSRyusuke Konishi skip_mount_setup:
859d26493b6SJiro SEKIBA sbp[0]->s_state =
860d26493b6SJiro SEKIBA cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS);
861b2ac86e1SJiro SEKIBA /* synchronize sbp[1] with sbp[0] */
8620ca7a5b9SRyusuke Konishi if (sbp[1])
863b2ac86e1SJiro SEKIBA memcpy(sbp[1], sbp[0], nilfs->ns_sbsize);
864f7545144SRyusuke Konishi return nilfs_commit_super(sb, NILFS_SB_COMMIT_ALL);
865783f6184SRyusuke Konishi }
866783f6184SRyusuke Konishi
nilfs_read_super_block(struct super_block * sb,u64 pos,int blocksize,struct buffer_head ** pbh)867e339ad31SRyusuke Konishi struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb,
868e339ad31SRyusuke Konishi u64 pos, int blocksize,
869e339ad31SRyusuke Konishi struct buffer_head **pbh)
870783f6184SRyusuke Konishi {
871e339ad31SRyusuke Konishi unsigned long long sb_index = pos;
872e339ad31SRyusuke Konishi unsigned long offset;
873783f6184SRyusuke Konishi
874e339ad31SRyusuke Konishi offset = do_div(sb_index, blocksize);
875783f6184SRyusuke Konishi *pbh = sb_bread(sb, sb_index);
876e339ad31SRyusuke Konishi if (!*pbh)
877783f6184SRyusuke Konishi return NULL;
878783f6184SRyusuke Konishi return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset);
879783f6184SRyusuke Konishi }
880783f6184SRyusuke Konishi
nilfs_store_magic_and_option(struct super_block * sb,struct nilfs_super_block * sbp,char * data)881783f6184SRyusuke Konishi int nilfs_store_magic_and_option(struct super_block *sb,
882783f6184SRyusuke Konishi struct nilfs_super_block *sbp,
883783f6184SRyusuke Konishi char *data)
884783f6184SRyusuke Konishi {
885e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
886783f6184SRyusuke Konishi
887783f6184SRyusuke Konishi sb->s_magic = le16_to_cpu(sbp->s_magic);
888783f6184SRyusuke Konishi
889783f6184SRyusuke Konishi /* FS independent flags */
890783f6184SRyusuke Konishi #ifdef NILFS_ATIME_DISABLE
8911751e8a6SLinus Torvalds sb->s_flags |= SB_NOATIME;
892783f6184SRyusuke Konishi #endif
893783f6184SRyusuke Konishi
8943b2ce58bSRyusuke Konishi nilfs_set_default_options(sb, sbp);
895783f6184SRyusuke Konishi
896574e6c31SRyusuke Konishi nilfs->ns_resuid = le16_to_cpu(sbp->s_def_resuid);
897574e6c31SRyusuke Konishi nilfs->ns_resgid = le16_to_cpu(sbp->s_def_resgid);
898574e6c31SRyusuke Konishi nilfs->ns_interval = le32_to_cpu(sbp->s_c_interval);
899574e6c31SRyusuke Konishi nilfs->ns_watermark = le32_to_cpu(sbp->s_c_block_max);
900783f6184SRyusuke Konishi
9017c017457SRyusuke Konishi return !parse_options(data, sb, 0) ? -EINVAL : 0;
902783f6184SRyusuke Konishi }
903783f6184SRyusuke Konishi
nilfs_check_feature_compatibility(struct super_block * sb,struct nilfs_super_block * sbp)904c5ca48aaSRyusuke Konishi int nilfs_check_feature_compatibility(struct super_block *sb,
905c5ca48aaSRyusuke Konishi struct nilfs_super_block *sbp)
906c5ca48aaSRyusuke Konishi {
907c5ca48aaSRyusuke Konishi __u64 features;
908c5ca48aaSRyusuke Konishi
909c5ca48aaSRyusuke Konishi features = le64_to_cpu(sbp->s_feature_incompat) &
910c5ca48aaSRyusuke Konishi ~NILFS_FEATURE_INCOMPAT_SUPP;
911c5ca48aaSRyusuke Konishi if (features) {
912a1d0747aSJoe Perches nilfs_err(sb,
913feee880fSRyusuke Konishi "couldn't mount because of unsupported optional features (%llx)",
914c5ca48aaSRyusuke Konishi (unsigned long long)features);
915c5ca48aaSRyusuke Konishi return -EINVAL;
916c5ca48aaSRyusuke Konishi }
917c5ca48aaSRyusuke Konishi features = le64_to_cpu(sbp->s_feature_compat_ro) &
918c5ca48aaSRyusuke Konishi ~NILFS_FEATURE_COMPAT_RO_SUPP;
919bc98a42cSDavid Howells if (!sb_rdonly(sb) && features) {
920a1d0747aSJoe Perches nilfs_err(sb,
921feee880fSRyusuke Konishi "couldn't mount RDWR because of unsupported optional features (%llx)",
922c5ca48aaSRyusuke Konishi (unsigned long long)features);
923c5ca48aaSRyusuke Konishi return -EINVAL;
924c5ca48aaSRyusuke Konishi }
925c5ca48aaSRyusuke Konishi return 0;
926c5ca48aaSRyusuke Konishi }
927c5ca48aaSRyusuke Konishi
nilfs_get_root_dentry(struct super_block * sb,struct nilfs_root * root,struct dentry ** root_dentry)928367ea334SRyusuke Konishi static int nilfs_get_root_dentry(struct super_block *sb,
929367ea334SRyusuke Konishi struct nilfs_root *root,
930367ea334SRyusuke Konishi struct dentry **root_dentry)
931367ea334SRyusuke Konishi {
932367ea334SRyusuke Konishi struct inode *inode;
933367ea334SRyusuke Konishi struct dentry *dentry;
934367ea334SRyusuke Konishi int ret = 0;
935367ea334SRyusuke Konishi
936367ea334SRyusuke Konishi inode = nilfs_iget(sb, root, NILFS_ROOT_INO);
937367ea334SRyusuke Konishi if (IS_ERR(inode)) {
938367ea334SRyusuke Konishi ret = PTR_ERR(inode);
939a1d0747aSJoe Perches nilfs_err(sb, "error %d getting root inode", ret);
940367ea334SRyusuke Konishi goto out;
941367ea334SRyusuke Konishi }
942367ea334SRyusuke Konishi if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) {
943367ea334SRyusuke Konishi iput(inode);
944a1d0747aSJoe Perches nilfs_err(sb, "corrupt root inode");
945367ea334SRyusuke Konishi ret = -EINVAL;
946367ea334SRyusuke Konishi goto out;
947367ea334SRyusuke Konishi }
948367ea334SRyusuke Konishi
949f11459adSRyusuke Konishi if (root->cno == NILFS_CPTREE_CURRENT_CNO) {
950f11459adSRyusuke Konishi dentry = d_find_alias(inode);
951f11459adSRyusuke Konishi if (!dentry) {
95248fde701SAl Viro dentry = d_make_root(inode);
953367ea334SRyusuke Konishi if (!dentry) {
954367ea334SRyusuke Konishi ret = -ENOMEM;
955f11459adSRyusuke Konishi goto failed_dentry;
956f11459adSRyusuke Konishi }
957f11459adSRyusuke Konishi } else {
958f11459adSRyusuke Konishi iput(inode);
959f11459adSRyusuke Konishi }
960f11459adSRyusuke Konishi } else {
9611a0a397eSJ. Bruce Fields dentry = d_obtain_root(inode);
962f11459adSRyusuke Konishi if (IS_ERR(dentry)) {
963f11459adSRyusuke Konishi ret = PTR_ERR(dentry);
964f11459adSRyusuke Konishi goto failed_dentry;
965f11459adSRyusuke Konishi }
966367ea334SRyusuke Konishi }
967367ea334SRyusuke Konishi *root_dentry = dentry;
968367ea334SRyusuke Konishi out:
969367ea334SRyusuke Konishi return ret;
970f11459adSRyusuke Konishi
971f11459adSRyusuke Konishi failed_dentry:
972a1d0747aSJoe Perches nilfs_err(sb, "error %d getting root dentry", ret);
973f11459adSRyusuke Konishi goto out;
974367ea334SRyusuke Konishi }
975367ea334SRyusuke Konishi
nilfs_attach_snapshot(struct super_block * s,__u64 cno,struct dentry ** root_dentry)976ab4d8f7eSRyusuke Konishi static int nilfs_attach_snapshot(struct super_block *s, __u64 cno,
977ab4d8f7eSRyusuke Konishi struct dentry **root_dentry)
978ab4d8f7eSRyusuke Konishi {
979e3154e97SRyusuke Konishi struct the_nilfs *nilfs = s->s_fs_info;
980ab4d8f7eSRyusuke Konishi struct nilfs_root *root;
981ab4d8f7eSRyusuke Konishi int ret;
982ab4d8f7eSRyusuke Konishi
983572d8b39SRyusuke Konishi mutex_lock(&nilfs->ns_snapshot_mount_mutex);
984572d8b39SRyusuke Konishi
985ab4d8f7eSRyusuke Konishi down_read(&nilfs->ns_segctor_sem);
986ab4d8f7eSRyusuke Konishi ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno);
987ab4d8f7eSRyusuke Konishi up_read(&nilfs->ns_segctor_sem);
988ab4d8f7eSRyusuke Konishi if (ret < 0) {
989ab4d8f7eSRyusuke Konishi ret = (ret == -ENOENT) ? -EINVAL : ret;
990ab4d8f7eSRyusuke Konishi goto out;
991ab4d8f7eSRyusuke Konishi } else if (!ret) {
992a1d0747aSJoe Perches nilfs_err(s,
993feee880fSRyusuke Konishi "The specified checkpoint is not a snapshot (checkpoint number=%llu)",
994ab4d8f7eSRyusuke Konishi (unsigned long long)cno);
995ab4d8f7eSRyusuke Konishi ret = -EINVAL;
996ab4d8f7eSRyusuke Konishi goto out;
997ab4d8f7eSRyusuke Konishi }
998ab4d8f7eSRyusuke Konishi
999f7545144SRyusuke Konishi ret = nilfs_attach_checkpoint(s, cno, false, &root);
1000ab4d8f7eSRyusuke Konishi if (ret) {
1001a1d0747aSJoe Perches nilfs_err(s,
1002feee880fSRyusuke Konishi "error %d while loading snapshot (checkpoint number=%llu)",
1003feee880fSRyusuke Konishi ret, (unsigned long long)cno);
1004ab4d8f7eSRyusuke Konishi goto out;
1005ab4d8f7eSRyusuke Konishi }
1006ab4d8f7eSRyusuke Konishi ret = nilfs_get_root_dentry(s, root, root_dentry);
1007ab4d8f7eSRyusuke Konishi nilfs_put_root(root);
1008ab4d8f7eSRyusuke Konishi out:
1009572d8b39SRyusuke Konishi mutex_unlock(&nilfs->ns_snapshot_mount_mutex);
1010ab4d8f7eSRyusuke Konishi return ret;
1011ab4d8f7eSRyusuke Konishi }
1012ab4d8f7eSRyusuke Konishi
1013f11459adSRyusuke Konishi /**
1014e95c311eSAl Viro * nilfs_tree_is_busy() - try to shrink dentries of a checkpoint
1015f11459adSRyusuke Konishi * @root_dentry: root dentry of the tree to be shrunk
1016f11459adSRyusuke Konishi *
1017f11459adSRyusuke Konishi * This function returns true if the tree was in-use.
1018f11459adSRyusuke Konishi */
nilfs_tree_is_busy(struct dentry * root_dentry)1019e95c311eSAl Viro static bool nilfs_tree_is_busy(struct dentry *root_dentry)
1020f11459adSRyusuke Konishi {
1021f11459adSRyusuke Konishi shrink_dcache_parent(root_dentry);
1022e95c311eSAl Viro return d_count(root_dentry) > 1;
1023f11459adSRyusuke Konishi }
1024f11459adSRyusuke Konishi
nilfs_checkpoint_is_mounted(struct super_block * sb,__u64 cno)1025032dbb3bSRyusuke Konishi int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno)
1026032dbb3bSRyusuke Konishi {
1027e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
1028032dbb3bSRyusuke Konishi struct nilfs_root *root;
1029032dbb3bSRyusuke Konishi struct inode *inode;
1030032dbb3bSRyusuke Konishi struct dentry *dentry;
1031032dbb3bSRyusuke Konishi int ret;
1032032dbb3bSRyusuke Konishi
10333377f843SRyusuke Konishi if (cno > nilfs->ns_cno)
1034032dbb3bSRyusuke Konishi return false;
1035032dbb3bSRyusuke Konishi
1036032dbb3bSRyusuke Konishi if (cno >= nilfs_last_cno(nilfs))
1037032dbb3bSRyusuke Konishi return true; /* protect recent checkpoints */
1038032dbb3bSRyusuke Konishi
1039032dbb3bSRyusuke Konishi ret = false;
1040e3154e97SRyusuke Konishi root = nilfs_lookup_root(nilfs, cno);
1041032dbb3bSRyusuke Konishi if (root) {
1042032dbb3bSRyusuke Konishi inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO);
1043032dbb3bSRyusuke Konishi if (inode) {
1044032dbb3bSRyusuke Konishi dentry = d_find_alias(inode);
1045032dbb3bSRyusuke Konishi if (dentry) {
1046e95c311eSAl Viro ret = nilfs_tree_is_busy(dentry);
1047032dbb3bSRyusuke Konishi dput(dentry);
1048032dbb3bSRyusuke Konishi }
1049032dbb3bSRyusuke Konishi iput(inode);
1050032dbb3bSRyusuke Konishi }
1051032dbb3bSRyusuke Konishi nilfs_put_root(root);
1052032dbb3bSRyusuke Konishi }
1053032dbb3bSRyusuke Konishi return ret;
1054032dbb3bSRyusuke Konishi }
1055032dbb3bSRyusuke Konishi
1056783f6184SRyusuke Konishi /**
1057783f6184SRyusuke Konishi * nilfs_fill_super() - initialize a super block instance
1058783f6184SRyusuke Konishi * @sb: super_block
1059783f6184SRyusuke Konishi * @data: mount options
1060783f6184SRyusuke Konishi * @silent: silent mode flag
1061783f6184SRyusuke Konishi *
1062aa7dfb89SRyusuke Konishi * This function is called exclusively by nilfs->ns_mount_mutex.
1063783f6184SRyusuke Konishi * So, the recovery process is protected from other simultaneous mounts.
1064783f6184SRyusuke Konishi */
1065783f6184SRyusuke Konishi static int
nilfs_fill_super(struct super_block * sb,void * data,int silent)1066348fe8daSRyusuke Konishi nilfs_fill_super(struct super_block *sb, void *data, int silent)
1067783f6184SRyusuke Konishi {
1068348fe8daSRyusuke Konishi struct the_nilfs *nilfs;
10694d8d9293SRyusuke Konishi struct nilfs_root *fsroot;
1070783f6184SRyusuke Konishi __u64 cno;
1071783f6184SRyusuke Konishi int err;
1072783f6184SRyusuke Konishi
10736625689eSRyusuke Konishi nilfs = alloc_nilfs(sb);
1074e3154e97SRyusuke Konishi if (!nilfs)
1075783f6184SRyusuke Konishi return -ENOMEM;
1076783f6184SRyusuke Konishi
1077e3154e97SRyusuke Konishi sb->s_fs_info = nilfs;
1078783f6184SRyusuke Konishi
1079f7545144SRyusuke Konishi err = init_nilfs(nilfs, sb, (char *)data);
1080783f6184SRyusuke Konishi if (err)
1081348fe8daSRyusuke Konishi goto failed_nilfs;
1082783f6184SRyusuke Konishi
1083783f6184SRyusuke Konishi sb->s_op = &nilfs_sops;
1084783f6184SRyusuke Konishi sb->s_export_op = &nilfs_export_ops;
1085783f6184SRyusuke Konishi sb->s_root = NULL;
108661239230SRyusuke Konishi sb->s_time_gran = 1;
10878de52778SAl Viro sb->s_max_links = NILFS_LINK_MAX;
1088026a7d63SRyusuke Konishi
1089a11d7fc2SChristoph Hellwig sb->s_bdi = bdi_get(sb->s_bdev->bd_disk->bdi);
1090783f6184SRyusuke Konishi
1091f7545144SRyusuke Konishi err = load_nilfs(nilfs, sb);
1092783f6184SRyusuke Konishi if (err)
1093348fe8daSRyusuke Konishi goto failed_nilfs;
1094f50a4c81SRyusuke Konishi
1095783f6184SRyusuke Konishi cno = nilfs_last_cno(nilfs);
1096f7545144SRyusuke Konishi err = nilfs_attach_checkpoint(sb, cno, true, &fsroot);
1097783f6184SRyusuke Konishi if (err) {
1098a1d0747aSJoe Perches nilfs_err(sb,
1099feee880fSRyusuke Konishi "error %d while loading last checkpoint (checkpoint number=%llu)",
1100feee880fSRyusuke Konishi err, (unsigned long long)cno);
1101f1e89c86SRyusuke Konishi goto failed_unload;
1102783f6184SRyusuke Konishi }
1103783f6184SRyusuke Konishi
1104bc98a42cSDavid Howells if (!sb_rdonly(sb)) {
1105f7545144SRyusuke Konishi err = nilfs_attach_log_writer(sb, fsroot);
1106783f6184SRyusuke Konishi if (err)
1107783f6184SRyusuke Konishi goto failed_checkpoint;
1108783f6184SRyusuke Konishi }
1109783f6184SRyusuke Konishi
1110367ea334SRyusuke Konishi err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root);
1111367ea334SRyusuke Konishi if (err)
1112783f6184SRyusuke Konishi goto failed_segctor;
1113783f6184SRyusuke Konishi
11144d8d9293SRyusuke Konishi nilfs_put_root(fsroot);
1115783f6184SRyusuke Konishi
1116bc98a42cSDavid Howells if (!sb_rdonly(sb)) {
1117783f6184SRyusuke Konishi down_write(&nilfs->ns_sem);
1118f7545144SRyusuke Konishi nilfs_setup_super(sb, true);
1119783f6184SRyusuke Konishi up_write(&nilfs->ns_sem);
1120783f6184SRyusuke Konishi }
1121783f6184SRyusuke Konishi
1122783f6184SRyusuke Konishi return 0;
1123783f6184SRyusuke Konishi
1124783f6184SRyusuke Konishi failed_segctor:
1125f7545144SRyusuke Konishi nilfs_detach_log_writer(sb);
1126783f6184SRyusuke Konishi
1127783f6184SRyusuke Konishi failed_checkpoint:
11284d8d9293SRyusuke Konishi nilfs_put_root(fsroot);
1129783f6184SRyusuke Konishi
1130f1e89c86SRyusuke Konishi failed_unload:
113142560f9cSRyusuke Konishi nilfs_sysfs_delete_device_group(nilfs);
1132f1e89c86SRyusuke Konishi iput(nilfs->ns_sufile);
1133f1e89c86SRyusuke Konishi iput(nilfs->ns_cpfile);
1134f1e89c86SRyusuke Konishi iput(nilfs->ns_dat);
1135f1e89c86SRyusuke Konishi
1136348fe8daSRyusuke Konishi failed_nilfs:
1137348fe8daSRyusuke Konishi destroy_nilfs(nilfs);
1138783f6184SRyusuke Konishi return err;
1139783f6184SRyusuke Konishi }
1140783f6184SRyusuke Konishi
nilfs_remount(struct super_block * sb,int * flags,char * data)1141783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data)
1142783f6184SRyusuke Konishi {
1143e3154e97SRyusuke Konishi struct the_nilfs *nilfs = sb->s_fs_info;
1144783f6184SRyusuke Konishi unsigned long old_sb_flags;
114506df0f99SRyusuke Konishi unsigned long old_mount_opt;
1146f11459adSRyusuke Konishi int err;
1147783f6184SRyusuke Konishi
114802b9984dSTheodore Ts'o sync_filesystem(sb);
1149783f6184SRyusuke Konishi old_sb_flags = sb->s_flags;
11503b2ce58bSRyusuke Konishi old_mount_opt = nilfs->ns_mount_opt;
1151783f6184SRyusuke Konishi
11527c017457SRyusuke Konishi if (!parse_options(data, sb, 1)) {
1153783f6184SRyusuke Konishi err = -EINVAL;
1154783f6184SRyusuke Konishi goto restore_opts;
1155783f6184SRyusuke Konishi }
11561751e8a6SLinus Torvalds sb->s_flags = (sb->s_flags & ~SB_POSIXACL);
1157783f6184SRyusuke Konishi
1158d240e067SRyusuke Konishi err = -EINVAL;
1159783f6184SRyusuke Konishi
11600234576dSRyusuke Konishi if (!nilfs_valid_fs(nilfs)) {
1161a1d0747aSJoe Perches nilfs_warn(sb,
1162feee880fSRyusuke Konishi "couldn't remount because the filesystem is in an incomplete recovery state");
11630234576dSRyusuke Konishi goto restore_opts;
11640234576dSRyusuke Konishi }
11650234576dSRyusuke Konishi
11661751e8a6SLinus Torvalds if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1167783f6184SRyusuke Konishi goto out;
11681751e8a6SLinus Torvalds if (*flags & SB_RDONLY) {
11691751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY;
1170783f6184SRyusuke Konishi
1171783f6184SRyusuke Konishi /*
1172783f6184SRyusuke Konishi * Remounting a valid RW partition RDONLY, so set
1173783f6184SRyusuke Konishi * the RDONLY flag and then mark the partition as valid again.
1174783f6184SRyusuke Konishi */
1175783f6184SRyusuke Konishi down_write(&nilfs->ns_sem);
1176f7545144SRyusuke Konishi nilfs_cleanup_super(sb);
1177783f6184SRyusuke Konishi up_write(&nilfs->ns_sem);
1178783f6184SRyusuke Konishi } else {
1179c5ca48aaSRyusuke Konishi __u64 features;
1180e912a5b6SRyusuke Konishi struct nilfs_root *root;
1181c5ca48aaSRyusuke Konishi
1182783f6184SRyusuke Konishi /*
1183783f6184SRyusuke Konishi * Mounting a RDONLY partition read-write, so reread and
1184783f6184SRyusuke Konishi * store the current valid flag. (It may have been changed
1185783f6184SRyusuke Konishi * by fsck since we originally mounted the partition.)
1186783f6184SRyusuke Konishi */
1187c5ca48aaSRyusuke Konishi down_read(&nilfs->ns_sem);
1188c5ca48aaSRyusuke Konishi features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
1189c5ca48aaSRyusuke Konishi ~NILFS_FEATURE_COMPAT_RO_SUPP;
1190c5ca48aaSRyusuke Konishi up_read(&nilfs->ns_sem);
1191c5ca48aaSRyusuke Konishi if (features) {
1192a1d0747aSJoe Perches nilfs_warn(sb,
1193feee880fSRyusuke Konishi "couldn't remount RDWR because of unsupported optional features (%llx)",
1194feee880fSRyusuke Konishi (unsigned long long)features);
1195c5ca48aaSRyusuke Konishi err = -EROFS;
1196c5ca48aaSRyusuke Konishi goto restore_opts;
1197c5ca48aaSRyusuke Konishi }
1198c5ca48aaSRyusuke Konishi
11991751e8a6SLinus Torvalds sb->s_flags &= ~SB_RDONLY;
1200783f6184SRyusuke Konishi
12012b0143b5SDavid Howells root = NILFS_I(d_inode(sb->s_root))->i_root;
1202f7545144SRyusuke Konishi err = nilfs_attach_log_writer(sb, root);
1203783f6184SRyusuke Konishi if (err)
1204e59399d0SRyusuke Konishi goto restore_opts;
1205783f6184SRyusuke Konishi
1206783f6184SRyusuke Konishi down_write(&nilfs->ns_sem);
1207f7545144SRyusuke Konishi nilfs_setup_super(sb, true);
1208783f6184SRyusuke Konishi up_write(&nilfs->ns_sem);
1209783f6184SRyusuke Konishi }
1210783f6184SRyusuke Konishi out:
1211783f6184SRyusuke Konishi return 0;
1212783f6184SRyusuke Konishi
1213783f6184SRyusuke Konishi restore_opts:
1214783f6184SRyusuke Konishi sb->s_flags = old_sb_flags;
12153b2ce58bSRyusuke Konishi nilfs->ns_mount_opt = old_mount_opt;
1216783f6184SRyusuke Konishi return err;
1217783f6184SRyusuke Konishi }
1218783f6184SRyusuke Konishi
1219783f6184SRyusuke Konishi struct nilfs_super_data {
1220783f6184SRyusuke Konishi __u64 cno;
1221783f6184SRyusuke Konishi int flags;
1222783f6184SRyusuke Konishi };
1223783f6184SRyusuke Konishi
nilfs_parse_snapshot_option(const char * option,const substring_t * arg,struct nilfs_super_data * sd)1224a7d3f104SRyusuke Konishi static int nilfs_parse_snapshot_option(const char *option,
1225a7d3f104SRyusuke Konishi const substring_t *arg,
1226a7d3f104SRyusuke Konishi struct nilfs_super_data *sd)
1227a7d3f104SRyusuke Konishi {
1228a7d3f104SRyusuke Konishi unsigned long long val;
1229a7d3f104SRyusuke Konishi const char *msg = NULL;
1230a7d3f104SRyusuke Konishi int err;
1231a7d3f104SRyusuke Konishi
12321751e8a6SLinus Torvalds if (!(sd->flags & SB_RDONLY)) {
1233a7d3f104SRyusuke Konishi msg = "read-only option is not specified";
1234a7d3f104SRyusuke Konishi goto parse_error;
1235a7d3f104SRyusuke Konishi }
1236a7d3f104SRyusuke Konishi
1237a7d3f104SRyusuke Konishi err = kstrtoull(arg->from, 0, &val);
1238a7d3f104SRyusuke Konishi if (err) {
1239a7d3f104SRyusuke Konishi if (err == -ERANGE)
1240a7d3f104SRyusuke Konishi msg = "too large checkpoint number";
1241a7d3f104SRyusuke Konishi else
1242a7d3f104SRyusuke Konishi msg = "malformed argument";
1243a7d3f104SRyusuke Konishi goto parse_error;
1244a7d3f104SRyusuke Konishi } else if (val == 0) {
1245a7d3f104SRyusuke Konishi msg = "invalid checkpoint number 0";
1246a7d3f104SRyusuke Konishi goto parse_error;
1247a7d3f104SRyusuke Konishi }
1248a7d3f104SRyusuke Konishi sd->cno = val;
1249a7d3f104SRyusuke Konishi return 0;
1250a7d3f104SRyusuke Konishi
1251a7d3f104SRyusuke Konishi parse_error:
1252a1d0747aSJoe Perches nilfs_err(NULL, "invalid option \"%s\": %s", option, msg);
1253a7d3f104SRyusuke Konishi return 1;
1254a7d3f104SRyusuke Konishi }
1255a7d3f104SRyusuke Konishi
1256783f6184SRyusuke Konishi /**
1257783f6184SRyusuke Konishi * nilfs_identify - pre-read mount options needed to identify mount instance
1258783f6184SRyusuke Konishi * @data: mount options
1259783f6184SRyusuke Konishi * @sd: nilfs_super_data
1260783f6184SRyusuke Konishi */
nilfs_identify(char * data,struct nilfs_super_data * sd)1261783f6184SRyusuke Konishi static int nilfs_identify(char *data, struct nilfs_super_data *sd)
1262783f6184SRyusuke Konishi {
1263783f6184SRyusuke Konishi char *p, *options = data;
1264783f6184SRyusuke Konishi substring_t args[MAX_OPT_ARGS];
1265c05dbfc2SRyusuke Konishi int token;
1266783f6184SRyusuke Konishi int ret = 0;
1267783f6184SRyusuke Konishi
1268783f6184SRyusuke Konishi do {
1269783f6184SRyusuke Konishi p = strsep(&options, ",");
1270783f6184SRyusuke Konishi if (p != NULL && *p) {
1271783f6184SRyusuke Konishi token = match_token(p, tokens, args);
1272a7d3f104SRyusuke Konishi if (token == Opt_snapshot)
1273a7d3f104SRyusuke Konishi ret = nilfs_parse_snapshot_option(p, &args[0],
1274a7d3f104SRyusuke Konishi sd);
1275783f6184SRyusuke Konishi }
1276783f6184SRyusuke Konishi if (!options)
1277783f6184SRyusuke Konishi break;
1278783f6184SRyusuke Konishi BUG_ON(options == data);
1279783f6184SRyusuke Konishi *(options - 1) = ',';
1280783f6184SRyusuke Konishi } while (!ret);
1281783f6184SRyusuke Konishi return ret;
1282783f6184SRyusuke Konishi }
1283783f6184SRyusuke Konishi
nilfs_set_bdev_super(struct super_block * s,void * data)1284783f6184SRyusuke Konishi static int nilfs_set_bdev_super(struct super_block *s, void *data)
1285783f6184SRyusuke Konishi {
1286*c1e012eaSChristoph Hellwig s->s_dev = *(dev_t *)data;
1287783f6184SRyusuke Konishi return 0;
1288783f6184SRyusuke Konishi }
1289783f6184SRyusuke Konishi
nilfs_test_bdev_super(struct super_block * s,void * data)1290783f6184SRyusuke Konishi static int nilfs_test_bdev_super(struct super_block *s, void *data)
1291783f6184SRyusuke Konishi {
1292*c1e012eaSChristoph Hellwig return !(s->s_iflags & SB_I_RETIRED) && s->s_dev == *(dev_t *)data;
1293783f6184SRyusuke Konishi }
1294783f6184SRyusuke Konishi
1295e4c59d61SAl Viro static struct dentry *
nilfs_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)1296e4c59d61SAl Viro nilfs_mount(struct file_system_type *fs_type, int flags,
1297e4c59d61SAl Viro const char *dev_name, void *data)
1298783f6184SRyusuke Konishi {
1299*c1e012eaSChristoph Hellwig struct nilfs_super_data sd = { .flags = flags };
130033c8e57cSRyusuke Konishi struct super_block *s;
1301*c1e012eaSChristoph Hellwig dev_t dev;
1302*c1e012eaSChristoph Hellwig int err;
1303783f6184SRyusuke Konishi
1304*c1e012eaSChristoph Hellwig if (nilfs_identify(data, &sd))
1305*c1e012eaSChristoph Hellwig return ERR_PTR(-EINVAL);
1306783f6184SRyusuke Konishi
1307*c1e012eaSChristoph Hellwig err = lookup_bdev(dev_name, &dev);
1308*c1e012eaSChristoph Hellwig if (err)
1309*c1e012eaSChristoph Hellwig return ERR_PTR(err);
1310783f6184SRyusuke Konishi
13119249e17fSDavid Howells s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, flags,
1312*c1e012eaSChristoph Hellwig &dev);
1313*c1e012eaSChristoph Hellwig if (IS_ERR(s))
1314*c1e012eaSChristoph Hellwig return ERR_CAST(s);
1315783f6184SRyusuke Konishi
1316783f6184SRyusuke Konishi if (!s->s_root) {
1317*c1e012eaSChristoph Hellwig /*
1318*c1e012eaSChristoph Hellwig * We drop s_umount here because we need to open the bdev and
1319*c1e012eaSChristoph Hellwig * bdev->open_mutex ranks above s_umount (blkdev_put() ->
1320*c1e012eaSChristoph Hellwig * __invalidate_device()). It is safe because we have active sb
1321*c1e012eaSChristoph Hellwig * reference and SB_BORN is not set yet.
1322*c1e012eaSChristoph Hellwig */
1323*c1e012eaSChristoph Hellwig up_write(&s->s_umount);
1324*c1e012eaSChristoph Hellwig err = setup_bdev_super(s, flags, NULL);
1325*c1e012eaSChristoph Hellwig down_write(&s->s_umount);
1326*c1e012eaSChristoph Hellwig if (!err)
1327*c1e012eaSChristoph Hellwig err = nilfs_fill_super(s, data,
1328*c1e012eaSChristoph Hellwig flags & SB_SILENT ? 1 : 0);
1329783f6184SRyusuke Konishi if (err)
1330348fe8daSRyusuke Konishi goto failed_super;
1331783f6184SRyusuke Konishi
13321751e8a6SLinus Torvalds s->s_flags |= SB_ACTIVE;
1333f11459adSRyusuke Konishi } else if (!sd.cno) {
1334e95c311eSAl Viro if (nilfs_tree_is_busy(s->s_root)) {
13351751e8a6SLinus Torvalds if ((flags ^ s->s_flags) & SB_RDONLY) {
1336a1d0747aSJoe Perches nilfs_err(s,
1337feee880fSRyusuke Konishi "the device already has a %s mount.",
1338bc98a42cSDavid Howells sb_rdonly(s) ? "read-only" : "read/write");
1339f11459adSRyusuke Konishi err = -EBUSY;
1340f11459adSRyusuke Konishi goto failed_super;
1341f11459adSRyusuke Konishi }
1342e95c311eSAl Viro } else {
1343f11459adSRyusuke Konishi /*
1344f11459adSRyusuke Konishi * Try remount to setup mount states if the current
1345f11459adSRyusuke Konishi * tree is not mounted and only snapshots use this sb.
1346f11459adSRyusuke Konishi */
1347f11459adSRyusuke Konishi err = nilfs_remount(s, &flags, data);
1348f11459adSRyusuke Konishi if (err)
1349f11459adSRyusuke Konishi goto failed_super;
1350f11459adSRyusuke Konishi }
1351783f6184SRyusuke Konishi }
1352783f6184SRyusuke Konishi
1353f11459adSRyusuke Konishi if (sd.cno) {
1354*c1e012eaSChristoph Hellwig struct dentry *root_dentry;
1355*c1e012eaSChristoph Hellwig
1356f11459adSRyusuke Konishi err = nilfs_attach_snapshot(s, sd.cno, &root_dentry);
1357348fe8daSRyusuke Konishi if (err)
1358f11459adSRyusuke Konishi goto failed_super;
1359*c1e012eaSChristoph Hellwig return root_dentry;
1360783f6184SRyusuke Konishi }
1361783f6184SRyusuke Konishi
1362*c1e012eaSChristoph Hellwig return dget(s->s_root);
1363783f6184SRyusuke Konishi
1364f11459adSRyusuke Konishi failed_super:
1365a95161aaSAl Viro deactivate_locked_super(s);
1366e4c59d61SAl Viro return ERR_PTR(err);
1367783f6184SRyusuke Konishi }
1368783f6184SRyusuke Konishi
1369783f6184SRyusuke Konishi struct file_system_type nilfs_fs_type = {
1370783f6184SRyusuke Konishi .owner = THIS_MODULE,
1371783f6184SRyusuke Konishi .name = "nilfs2",
1372e4c59d61SAl Viro .mount = nilfs_mount,
1373783f6184SRyusuke Konishi .kill_sb = kill_block_super,
1374783f6184SRyusuke Konishi .fs_flags = FS_REQUIRES_DEV,
1375783f6184SRyusuke Konishi };
13767f78e035SEric W. Biederman MODULE_ALIAS_FS("nilfs2");
1377783f6184SRyusuke Konishi
nilfs_inode_init_once(void * obj)137841c88bd7SLi Hong static void nilfs_inode_init_once(void *obj)
137941c88bd7SLi Hong {
138041c88bd7SLi Hong struct nilfs_inode_info *ii = obj;
138141c88bd7SLi Hong
138241c88bd7SLi Hong INIT_LIST_HEAD(&ii->i_dirty);
138341c88bd7SLi Hong #ifdef CONFIG_NILFS_XATTR
138441c88bd7SLi Hong init_rwsem(&ii->xattr_sem);
138541c88bd7SLi Hong #endif
138641c88bd7SLi Hong inode_init_once(&ii->vfs_inode);
138741c88bd7SLi Hong }
138841c88bd7SLi Hong
nilfs_segbuf_init_once(void * obj)138941c88bd7SLi Hong static void nilfs_segbuf_init_once(void *obj)
139041c88bd7SLi Hong {
139141c88bd7SLi Hong memset(obj, 0, sizeof(struct nilfs_segment_buffer));
139241c88bd7SLi Hong }
139341c88bd7SLi Hong
nilfs_destroy_cachep(void)139441c88bd7SLi Hong static void nilfs_destroy_cachep(void)
139541c88bd7SLi Hong {
13968c0a8537SKirill A. Shutemov /*
13978c0a8537SKirill A. Shutemov * Make sure all delayed rcu free inodes are flushed before we
13988c0a8537SKirill A. Shutemov * destroy cache.
13998c0a8537SKirill A. Shutemov */
14008c0a8537SKirill A. Shutemov rcu_barrier();
14018c0a8537SKirill A. Shutemov
140241c88bd7SLi Hong kmem_cache_destroy(nilfs_inode_cachep);
140341c88bd7SLi Hong kmem_cache_destroy(nilfs_transaction_cachep);
140441c88bd7SLi Hong kmem_cache_destroy(nilfs_segbuf_cachep);
140541c88bd7SLi Hong kmem_cache_destroy(nilfs_btree_path_cache);
140641c88bd7SLi Hong }
140741c88bd7SLi Hong
nilfs_init_cachep(void)140841c88bd7SLi Hong static int __init nilfs_init_cachep(void)
140941c88bd7SLi Hong {
141041c88bd7SLi Hong nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache",
141141c88bd7SLi Hong sizeof(struct nilfs_inode_info), 0,
14125d097056SVladimir Davydov SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT,
14135d097056SVladimir Davydov nilfs_inode_init_once);
141441c88bd7SLi Hong if (!nilfs_inode_cachep)
141541c88bd7SLi Hong goto fail;
141641c88bd7SLi Hong
141741c88bd7SLi Hong nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache",
141841c88bd7SLi Hong sizeof(struct nilfs_transaction_info), 0,
141941c88bd7SLi Hong SLAB_RECLAIM_ACCOUNT, NULL);
142041c88bd7SLi Hong if (!nilfs_transaction_cachep)
142141c88bd7SLi Hong goto fail;
142241c88bd7SLi Hong
142341c88bd7SLi Hong nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache",
142441c88bd7SLi Hong sizeof(struct nilfs_segment_buffer), 0,
142541c88bd7SLi Hong SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once);
142641c88bd7SLi Hong if (!nilfs_segbuf_cachep)
142741c88bd7SLi Hong goto fail;
142841c88bd7SLi Hong
142941c88bd7SLi Hong nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache",
143041c88bd7SLi Hong sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX,
143141c88bd7SLi Hong 0, 0, NULL);
143241c88bd7SLi Hong if (!nilfs_btree_path_cache)
143341c88bd7SLi Hong goto fail;
143441c88bd7SLi Hong
143541c88bd7SLi Hong return 0;
143641c88bd7SLi Hong
143741c88bd7SLi Hong fail:
143841c88bd7SLi Hong nilfs_destroy_cachep();
143941c88bd7SLi Hong return -ENOMEM;
144041c88bd7SLi Hong }
144141c88bd7SLi Hong
init_nilfs_fs(void)1442783f6184SRyusuke Konishi static int __init init_nilfs_fs(void)
1443783f6184SRyusuke Konishi {
1444783f6184SRyusuke Konishi int err;
1445783f6184SRyusuke Konishi
144641c88bd7SLi Hong err = nilfs_init_cachep();
1447783f6184SRyusuke Konishi if (err)
144841c88bd7SLi Hong goto fail;
1449783f6184SRyusuke Konishi
1450dd70edbdSVyacheslav Dubeyko err = nilfs_sysfs_init();
1451783f6184SRyusuke Konishi if (err)
145241c88bd7SLi Hong goto free_cachep;
1453783f6184SRyusuke Konishi
1454dd70edbdSVyacheslav Dubeyko err = register_filesystem(&nilfs_fs_type);
1455dd70edbdSVyacheslav Dubeyko if (err)
1456dd70edbdSVyacheslav Dubeyko goto deinit_sysfs_entry;
1457dd70edbdSVyacheslav Dubeyko
14589f130263SLi Hong printk(KERN_INFO "NILFS version 2 loaded\n");
1459783f6184SRyusuke Konishi return 0;
1460783f6184SRyusuke Konishi
1461dd70edbdSVyacheslav Dubeyko deinit_sysfs_entry:
1462dd70edbdSVyacheslav Dubeyko nilfs_sysfs_exit();
146341c88bd7SLi Hong free_cachep:
146441c88bd7SLi Hong nilfs_destroy_cachep();
146541c88bd7SLi Hong fail:
1466783f6184SRyusuke Konishi return err;
1467783f6184SRyusuke Konishi }
1468783f6184SRyusuke Konishi
exit_nilfs_fs(void)1469783f6184SRyusuke Konishi static void __exit exit_nilfs_fs(void)
1470783f6184SRyusuke Konishi {
147141c88bd7SLi Hong nilfs_destroy_cachep();
1472dd70edbdSVyacheslav Dubeyko nilfs_sysfs_exit();
1473783f6184SRyusuke Konishi unregister_filesystem(&nilfs_fs_type);
1474783f6184SRyusuke Konishi }
1475783f6184SRyusuke Konishi
1476783f6184SRyusuke Konishi module_init(init_nilfs_fs)
1477783f6184SRyusuke Konishi module_exit(exit_nilfs_fs)
1478