1783f6184SRyusuke Konishi /* 2783f6184SRyusuke Konishi * super.c - NILFS module and super block management. 3783f6184SRyusuke Konishi * 4783f6184SRyusuke Konishi * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. 5783f6184SRyusuke Konishi * 6783f6184SRyusuke Konishi * This program is free software; you can redistribute it and/or modify 7783f6184SRyusuke Konishi * it under the terms of the GNU General Public License as published by 8783f6184SRyusuke Konishi * the Free Software Foundation; either version 2 of the License, or 9783f6184SRyusuke Konishi * (at your option) any later version. 10783f6184SRyusuke Konishi * 11783f6184SRyusuke Konishi * This program is distributed in the hope that it will be useful, 12783f6184SRyusuke Konishi * but WITHOUT ANY WARRANTY; without even the implied warranty of 13783f6184SRyusuke Konishi * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14783f6184SRyusuke Konishi * GNU General Public License for more details. 15783f6184SRyusuke Konishi * 16783f6184SRyusuke Konishi * You should have received a copy of the GNU General Public License 17783f6184SRyusuke Konishi * along with this program; if not, write to the Free Software 18783f6184SRyusuke Konishi * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19783f6184SRyusuke Konishi * 20783f6184SRyusuke Konishi * Written by Ryusuke Konishi <ryusuke@osrg.net> 21783f6184SRyusuke Konishi */ 22783f6184SRyusuke Konishi /* 23783f6184SRyusuke Konishi * linux/fs/ext2/super.c 24783f6184SRyusuke Konishi * 25783f6184SRyusuke Konishi * Copyright (C) 1992, 1993, 1994, 1995 26783f6184SRyusuke Konishi * Remy Card (card@masi.ibp.fr) 27783f6184SRyusuke Konishi * Laboratoire MASI - Institut Blaise Pascal 28783f6184SRyusuke Konishi * Universite Pierre et Marie Curie (Paris VI) 29783f6184SRyusuke Konishi * 30783f6184SRyusuke Konishi * from 31783f6184SRyusuke Konishi * 32783f6184SRyusuke Konishi * linux/fs/minix/inode.c 33783f6184SRyusuke Konishi * 34783f6184SRyusuke Konishi * Copyright (C) 1991, 1992 Linus Torvalds 35783f6184SRyusuke Konishi * 36783f6184SRyusuke Konishi * Big-endian to little-endian byte-swapping/bitmaps by 37783f6184SRyusuke Konishi * David S. Miller (davem@caip.rutgers.edu), 1995 38783f6184SRyusuke Konishi */ 39783f6184SRyusuke Konishi 40783f6184SRyusuke Konishi #include <linux/module.h> 41783f6184SRyusuke Konishi #include <linux/string.h> 42783f6184SRyusuke Konishi #include <linux/slab.h> 43783f6184SRyusuke Konishi #include <linux/init.h> 44783f6184SRyusuke Konishi #include <linux/blkdev.h> 45783f6184SRyusuke Konishi #include <linux/parser.h> 46783f6184SRyusuke Konishi #include <linux/random.h> 47783f6184SRyusuke Konishi #include <linux/crc32.h> 48783f6184SRyusuke Konishi #include <linux/smp_lock.h> 49783f6184SRyusuke Konishi #include <linux/vfs.h> 50783f6184SRyusuke Konishi #include <linux/writeback.h> 51783f6184SRyusuke Konishi #include <linux/kobject.h> 52783f6184SRyusuke Konishi #include <linux/exportfs.h> 53b58a285bSJiro SEKIBA #include <linux/seq_file.h> 54b58a285bSJiro SEKIBA #include <linux/mount.h> 55783f6184SRyusuke Konishi #include "nilfs.h" 56783f6184SRyusuke Konishi #include "mdt.h" 57783f6184SRyusuke Konishi #include "alloc.h" 58783f6184SRyusuke Konishi #include "page.h" 59783f6184SRyusuke Konishi #include "cpfile.h" 60783f6184SRyusuke Konishi #include "ifile.h" 61783f6184SRyusuke Konishi #include "dat.h" 62783f6184SRyusuke Konishi #include "segment.h" 63783f6184SRyusuke Konishi #include "segbuf.h" 64783f6184SRyusuke Konishi 65783f6184SRyusuke Konishi MODULE_AUTHOR("NTT Corp."); 66783f6184SRyusuke Konishi MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem " 67783f6184SRyusuke Konishi "(NILFS)"); 68783f6184SRyusuke Konishi MODULE_LICENSE("GPL"); 69783f6184SRyusuke Konishi 70783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data); 71783f6184SRyusuke Konishi 72783f6184SRyusuke Konishi /** 73783f6184SRyusuke Konishi * nilfs_error() - report failure condition on a filesystem 74783f6184SRyusuke Konishi * 75783f6184SRyusuke Konishi * nilfs_error() sets an ERROR_FS flag on the superblock as well as 76783f6184SRyusuke Konishi * reporting an error message. It should be called when NILFS detects 77783f6184SRyusuke Konishi * incoherences or defects of meta data on disk. As for sustainable 78783f6184SRyusuke Konishi * errors such as a single-shot I/O error, nilfs_warning() or the printk() 79783f6184SRyusuke Konishi * function should be used instead. 80783f6184SRyusuke Konishi * 81783f6184SRyusuke Konishi * The segment constructor must not call this function because it can 82783f6184SRyusuke Konishi * kill itself. 83783f6184SRyusuke Konishi */ 84783f6184SRyusuke Konishi void nilfs_error(struct super_block *sb, const char *function, 85783f6184SRyusuke Konishi const char *fmt, ...) 86783f6184SRyusuke Konishi { 87783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 88783f6184SRyusuke Konishi va_list args; 89783f6184SRyusuke Konishi 90783f6184SRyusuke Konishi va_start(args, fmt); 91783f6184SRyusuke Konishi printk(KERN_CRIT "NILFS error (device %s): %s: ", sb->s_id, function); 92783f6184SRyusuke Konishi vprintk(fmt, args); 93783f6184SRyusuke Konishi printk("\n"); 94783f6184SRyusuke Konishi va_end(args); 95783f6184SRyusuke Konishi 96783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 97783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 98783f6184SRyusuke Konishi 99783f6184SRyusuke Konishi if (!nilfs_test_opt(sbi, ERRORS_CONT)) 100783f6184SRyusuke Konishi nilfs_detach_segment_constructor(sbi); 101783f6184SRyusuke Konishi 102783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 103783f6184SRyusuke Konishi if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) { 104783f6184SRyusuke Konishi nilfs->ns_mount_state |= NILFS_ERROR_FS; 105e339ad31SRyusuke Konishi nilfs->ns_sbp[0]->s_state |= 106e339ad31SRyusuke Konishi cpu_to_le16(NILFS_ERROR_FS); 107e339ad31SRyusuke Konishi nilfs_commit_super(sbi, 1); 108783f6184SRyusuke Konishi } 109783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 110783f6184SRyusuke Konishi 111783f6184SRyusuke Konishi if (nilfs_test_opt(sbi, ERRORS_RO)) { 112783f6184SRyusuke Konishi printk(KERN_CRIT "Remounting filesystem read-only\n"); 113783f6184SRyusuke Konishi sb->s_flags |= MS_RDONLY; 114783f6184SRyusuke Konishi } 115783f6184SRyusuke Konishi } 116783f6184SRyusuke Konishi 117783f6184SRyusuke Konishi if (nilfs_test_opt(sbi, ERRORS_PANIC)) 118783f6184SRyusuke Konishi panic("NILFS (device %s): panic forced after error\n", 119783f6184SRyusuke Konishi sb->s_id); 120783f6184SRyusuke Konishi } 121783f6184SRyusuke Konishi 122783f6184SRyusuke Konishi void nilfs_warning(struct super_block *sb, const char *function, 123783f6184SRyusuke Konishi const char *fmt, ...) 124783f6184SRyusuke Konishi { 125783f6184SRyusuke Konishi va_list args; 126783f6184SRyusuke Konishi 127783f6184SRyusuke Konishi va_start(args, fmt); 128783f6184SRyusuke Konishi printk(KERN_WARNING "NILFS warning (device %s): %s: ", 129783f6184SRyusuke Konishi sb->s_id, function); 130783f6184SRyusuke Konishi vprintk(fmt, args); 131783f6184SRyusuke Konishi printk("\n"); 132783f6184SRyusuke Konishi va_end(args); 133783f6184SRyusuke Konishi } 134783f6184SRyusuke Konishi 135783f6184SRyusuke Konishi static struct kmem_cache *nilfs_inode_cachep; 136783f6184SRyusuke Konishi 137a53b4751SRyusuke Konishi struct inode *nilfs_alloc_inode_common(struct the_nilfs *nilfs) 138783f6184SRyusuke Konishi { 139783f6184SRyusuke Konishi struct nilfs_inode_info *ii; 140783f6184SRyusuke Konishi 141783f6184SRyusuke Konishi ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS); 142783f6184SRyusuke Konishi if (!ii) 143783f6184SRyusuke Konishi return NULL; 144783f6184SRyusuke Konishi ii->i_bh = NULL; 145783f6184SRyusuke Konishi ii->i_state = 0; 146783f6184SRyusuke Konishi ii->vfs_inode.i_version = 1; 147a53b4751SRyusuke Konishi nilfs_btnode_cache_init(&ii->i_btnode_cache, nilfs->ns_bdi); 148783f6184SRyusuke Konishi return &ii->vfs_inode; 149783f6184SRyusuke Konishi } 150783f6184SRyusuke Konishi 151a53b4751SRyusuke Konishi struct inode *nilfs_alloc_inode(struct super_block *sb) 152a53b4751SRyusuke Konishi { 153a53b4751SRyusuke Konishi return nilfs_alloc_inode_common(NILFS_SB(sb)->s_nilfs); 154a53b4751SRyusuke Konishi } 155a53b4751SRyusuke Konishi 156783f6184SRyusuke Konishi void nilfs_destroy_inode(struct inode *inode) 157783f6184SRyusuke Konishi { 158783f6184SRyusuke Konishi kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode)); 159783f6184SRyusuke Konishi } 160783f6184SRyusuke Konishi 161783f6184SRyusuke Konishi static void init_once(void *obj) 162783f6184SRyusuke Konishi { 163783f6184SRyusuke Konishi struct nilfs_inode_info *ii = obj; 164783f6184SRyusuke Konishi 165783f6184SRyusuke Konishi INIT_LIST_HEAD(&ii->i_dirty); 166783f6184SRyusuke Konishi #ifdef CONFIG_NILFS_XATTR 167783f6184SRyusuke Konishi init_rwsem(&ii->xattr_sem); 168783f6184SRyusuke Konishi #endif 169783f6184SRyusuke Konishi nilfs_btnode_cache_init_once(&ii->i_btnode_cache); 170783f6184SRyusuke Konishi ii->i_bmap = (struct nilfs_bmap *)&ii->i_bmap_union; 171783f6184SRyusuke Konishi inode_init_once(&ii->vfs_inode); 172783f6184SRyusuke Konishi } 173783f6184SRyusuke Konishi 174783f6184SRyusuke Konishi static int nilfs_init_inode_cache(void) 175783f6184SRyusuke Konishi { 176783f6184SRyusuke Konishi nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache", 177783f6184SRyusuke Konishi sizeof(struct nilfs_inode_info), 178783f6184SRyusuke Konishi 0, SLAB_RECLAIM_ACCOUNT, 179783f6184SRyusuke Konishi init_once); 180783f6184SRyusuke Konishi 181783f6184SRyusuke Konishi return (nilfs_inode_cachep == NULL) ? -ENOMEM : 0; 182783f6184SRyusuke Konishi } 183783f6184SRyusuke Konishi 184783f6184SRyusuke Konishi static inline void nilfs_destroy_inode_cache(void) 185783f6184SRyusuke Konishi { 186783f6184SRyusuke Konishi kmem_cache_destroy(nilfs_inode_cachep); 187783f6184SRyusuke Konishi } 188783f6184SRyusuke Konishi 189783f6184SRyusuke Konishi static void nilfs_clear_inode(struct inode *inode) 190783f6184SRyusuke Konishi { 191783f6184SRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 192783f6184SRyusuke Konishi 193783f6184SRyusuke Konishi /* 194783f6184SRyusuke Konishi * Free resources allocated in nilfs_read_inode(), here. 195783f6184SRyusuke Konishi */ 196a2e7d2dfSRyusuke Konishi BUG_ON(!list_empty(&ii->i_dirty)); 197783f6184SRyusuke Konishi brelse(ii->i_bh); 198783f6184SRyusuke Konishi ii->i_bh = NULL; 199783f6184SRyusuke Konishi 200783f6184SRyusuke Konishi if (test_bit(NILFS_I_BMAP, &ii->i_state)) 201783f6184SRyusuke Konishi nilfs_bmap_clear(ii->i_bmap); 202783f6184SRyusuke Konishi 203783f6184SRyusuke Konishi nilfs_btnode_cache_clear(&ii->i_btnode_cache); 204783f6184SRyusuke Konishi } 205783f6184SRyusuke Konishi 206e339ad31SRyusuke Konishi static int nilfs_sync_super(struct nilfs_sb_info *sbi, int dupsb) 207783f6184SRyusuke Konishi { 208783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 209783f6184SRyusuke Konishi int err; 210783f6184SRyusuke Konishi int barrier_done = 0; 211783f6184SRyusuke Konishi 212783f6184SRyusuke Konishi if (nilfs_test_opt(sbi, BARRIER)) { 213e339ad31SRyusuke Konishi set_buffer_ordered(nilfs->ns_sbh[0]); 214783f6184SRyusuke Konishi barrier_done = 1; 215783f6184SRyusuke Konishi } 216783f6184SRyusuke Konishi retry: 217e339ad31SRyusuke Konishi set_buffer_dirty(nilfs->ns_sbh[0]); 218e339ad31SRyusuke Konishi err = sync_dirty_buffer(nilfs->ns_sbh[0]); 219783f6184SRyusuke Konishi if (err == -EOPNOTSUPP && barrier_done) { 220783f6184SRyusuke Konishi nilfs_warning(sbi->s_super, __func__, 221783f6184SRyusuke Konishi "barrier-based sync failed. " 222783f6184SRyusuke Konishi "disabling barriers\n"); 223783f6184SRyusuke Konishi nilfs_clear_opt(sbi, BARRIER); 224783f6184SRyusuke Konishi barrier_done = 0; 225e339ad31SRyusuke Konishi clear_buffer_ordered(nilfs->ns_sbh[0]); 226783f6184SRyusuke Konishi goto retry; 227783f6184SRyusuke Konishi } 228e339ad31SRyusuke Konishi if (unlikely(err)) { 229783f6184SRyusuke Konishi printk(KERN_ERR 230783f6184SRyusuke Konishi "NILFS: unable to write superblock (err=%d)\n", err); 231e339ad31SRyusuke Konishi if (err == -EIO && nilfs->ns_sbh[1]) { 232e339ad31SRyusuke Konishi nilfs_fall_back_super_block(nilfs); 233e339ad31SRyusuke Konishi goto retry; 234e339ad31SRyusuke Konishi } 235e339ad31SRyusuke Konishi } else { 236e339ad31SRyusuke Konishi struct nilfs_super_block *sbp = nilfs->ns_sbp[0]; 237e339ad31SRyusuke Konishi 238e339ad31SRyusuke Konishi /* 239e339ad31SRyusuke Konishi * The latest segment becomes trailable from the position 240e339ad31SRyusuke Konishi * written in superblock. 241e339ad31SRyusuke Konishi */ 242783f6184SRyusuke Konishi clear_nilfs_discontinued(nilfs); 243e339ad31SRyusuke Konishi 244e339ad31SRyusuke Konishi /* update GC protection for recent segments */ 245e339ad31SRyusuke Konishi if (nilfs->ns_sbh[1]) { 246e339ad31SRyusuke Konishi sbp = NULL; 247e339ad31SRyusuke Konishi if (dupsb) { 248e339ad31SRyusuke Konishi set_buffer_dirty(nilfs->ns_sbh[1]); 249e339ad31SRyusuke Konishi if (!sync_dirty_buffer(nilfs->ns_sbh[1])) 250e339ad31SRyusuke Konishi sbp = nilfs->ns_sbp[1]; 251e339ad31SRyusuke Konishi } 252e339ad31SRyusuke Konishi } 253e339ad31SRyusuke Konishi if (sbp) { 2542c2e52fcSRyusuke Konishi spin_lock(&nilfs->ns_last_segment_lock); 255e339ad31SRyusuke Konishi nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq); 2562c2e52fcSRyusuke Konishi spin_unlock(&nilfs->ns_last_segment_lock); 257783f6184SRyusuke Konishi } 258e339ad31SRyusuke Konishi } 259783f6184SRyusuke Konishi 260783f6184SRyusuke Konishi return err; 261783f6184SRyusuke Konishi } 262783f6184SRyusuke Konishi 263e339ad31SRyusuke Konishi int nilfs_commit_super(struct nilfs_sb_info *sbi, int dupsb) 264783f6184SRyusuke Konishi { 265783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 266e339ad31SRyusuke Konishi struct nilfs_super_block **sbp = nilfs->ns_sbp; 267783f6184SRyusuke Konishi sector_t nfreeblocks; 268e339ad31SRyusuke Konishi time_t t; 269783f6184SRyusuke Konishi int err; 270783f6184SRyusuke Konishi 271783f6184SRyusuke Konishi /* nilfs->sem must be locked by the caller. */ 272e339ad31SRyusuke Konishi if (sbp[0]->s_magic != NILFS_SUPER_MAGIC) { 273e339ad31SRyusuke Konishi if (sbp[1] && sbp[1]->s_magic == NILFS_SUPER_MAGIC) 274e339ad31SRyusuke Konishi nilfs_swap_super_block(nilfs); 275e339ad31SRyusuke Konishi else { 276e339ad31SRyusuke Konishi printk(KERN_CRIT "NILFS: superblock broke on dev %s\n", 277e339ad31SRyusuke Konishi sbi->s_super->s_id); 278e339ad31SRyusuke Konishi return -EIO; 279e339ad31SRyusuke Konishi } 280e339ad31SRyusuke Konishi } 281783f6184SRyusuke Konishi err = nilfs_count_free_blocks(nilfs, &nfreeblocks); 282783f6184SRyusuke Konishi if (unlikely(err)) { 283783f6184SRyusuke Konishi printk(KERN_ERR "NILFS: failed to count free blocks\n"); 284783f6184SRyusuke Konishi return err; 285783f6184SRyusuke Konishi } 286e339ad31SRyusuke Konishi spin_lock(&nilfs->ns_last_segment_lock); 287e339ad31SRyusuke Konishi sbp[0]->s_last_seq = cpu_to_le64(nilfs->ns_last_seq); 288e339ad31SRyusuke Konishi sbp[0]->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg); 289e339ad31SRyusuke Konishi sbp[0]->s_last_cno = cpu_to_le64(nilfs->ns_last_cno); 290e339ad31SRyusuke Konishi spin_unlock(&nilfs->ns_last_segment_lock); 291e339ad31SRyusuke Konishi 292e339ad31SRyusuke Konishi t = get_seconds(); 293e339ad31SRyusuke Konishi nilfs->ns_sbwtime[0] = t; 294e339ad31SRyusuke Konishi sbp[0]->s_free_blocks_count = cpu_to_le64(nfreeblocks); 295e339ad31SRyusuke Konishi sbp[0]->s_wtime = cpu_to_le64(t); 296e339ad31SRyusuke Konishi sbp[0]->s_sum = 0; 297e339ad31SRyusuke Konishi sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed, 298e339ad31SRyusuke Konishi (unsigned char *)sbp[0], 299e339ad31SRyusuke Konishi nilfs->ns_sbsize)); 300e339ad31SRyusuke Konishi if (dupsb && sbp[1]) { 301e339ad31SRyusuke Konishi memcpy(sbp[1], sbp[0], nilfs->ns_sbsize); 302e339ad31SRyusuke Konishi nilfs->ns_sbwtime[1] = t; 303e339ad31SRyusuke Konishi } 304783f6184SRyusuke Konishi sbi->s_super->s_dirt = 0; 305e339ad31SRyusuke Konishi return nilfs_sync_super(sbi, dupsb); 306783f6184SRyusuke Konishi } 307783f6184SRyusuke Konishi 308783f6184SRyusuke Konishi static void nilfs_put_super(struct super_block *sb) 309783f6184SRyusuke Konishi { 310783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 311783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 312783f6184SRyusuke Konishi 3136cfd0148SChristoph Hellwig lock_kernel(); 3146cfd0148SChristoph Hellwig 315783f6184SRyusuke Konishi nilfs_detach_segment_constructor(sbi); 316783f6184SRyusuke Konishi 317783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 318783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 319e339ad31SRyusuke Konishi nilfs->ns_sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state); 320e339ad31SRyusuke Konishi nilfs_commit_super(sbi, 1); 321783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 322783f6184SRyusuke Konishi } 323e59399d0SRyusuke Konishi down_write(&nilfs->ns_super_sem); 3243f82ff55SRyusuke Konishi if (nilfs->ns_current == sbi) 3253f82ff55SRyusuke Konishi nilfs->ns_current = NULL; 326e59399d0SRyusuke Konishi up_write(&nilfs->ns_super_sem); 327783f6184SRyusuke Konishi 328783f6184SRyusuke Konishi nilfs_detach_checkpoint(sbi); 329783f6184SRyusuke Konishi put_nilfs(sbi->s_nilfs); 330783f6184SRyusuke Konishi sbi->s_super = NULL; 331783f6184SRyusuke Konishi sb->s_fs_info = NULL; 3326dd47406SRyusuke Konishi nilfs_put_sbinfo(sbi); 3336cfd0148SChristoph Hellwig 3346cfd0148SChristoph Hellwig unlock_kernel(); 335783f6184SRyusuke Konishi } 336783f6184SRyusuke Konishi 337783f6184SRyusuke Konishi static int nilfs_sync_fs(struct super_block *sb, int wait) 338783f6184SRyusuke Konishi { 3396233caa9SJiro SEKIBA struct nilfs_sb_info *sbi = NILFS_SB(sb); 3406233caa9SJiro SEKIBA struct the_nilfs *nilfs = sbi->s_nilfs; 341783f6184SRyusuke Konishi int err = 0; 342783f6184SRyusuke Konishi 343783f6184SRyusuke Konishi /* This function is called when super block should be written back */ 344783f6184SRyusuke Konishi if (wait) 345783f6184SRyusuke Konishi err = nilfs_construct_segment(sb); 3466233caa9SJiro SEKIBA 3476233caa9SJiro SEKIBA down_write(&nilfs->ns_sem); 3486233caa9SJiro SEKIBA if (sb->s_dirt) 3496233caa9SJiro SEKIBA nilfs_commit_super(sbi, 1); 3506233caa9SJiro SEKIBA up_write(&nilfs->ns_sem); 3516233caa9SJiro SEKIBA 352783f6184SRyusuke Konishi return err; 353783f6184SRyusuke Konishi } 354783f6184SRyusuke Konishi 355783f6184SRyusuke Konishi int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno) 356783f6184SRyusuke Konishi { 357783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 358783f6184SRyusuke Konishi struct nilfs_checkpoint *raw_cp; 359783f6184SRyusuke Konishi struct buffer_head *bh_cp; 360783f6184SRyusuke Konishi int err; 361783f6184SRyusuke Konishi 362e59399d0SRyusuke Konishi down_write(&nilfs->ns_super_sem); 363783f6184SRyusuke Konishi list_add(&sbi->s_list, &nilfs->ns_supers); 364e59399d0SRyusuke Konishi up_write(&nilfs->ns_super_sem); 365783f6184SRyusuke Konishi 36679739565SRyusuke Konishi sbi->s_ifile = nilfs_ifile_new(sbi, nilfs->ns_inode_size); 367783f6184SRyusuke Konishi if (!sbi->s_ifile) 368783f6184SRyusuke Konishi return -ENOMEM; 369783f6184SRyusuke Konishi 3701154ecbdSZhang Qiang down_read(&nilfs->ns_segctor_sem); 371783f6184SRyusuke Konishi err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp, 372783f6184SRyusuke Konishi &bh_cp); 3731154ecbdSZhang Qiang up_read(&nilfs->ns_segctor_sem); 374783f6184SRyusuke Konishi if (unlikely(err)) { 375783f6184SRyusuke Konishi if (err == -ENOENT || err == -EINVAL) { 376783f6184SRyusuke Konishi printk(KERN_ERR 377783f6184SRyusuke Konishi "NILFS: Invalid checkpoint " 378783f6184SRyusuke Konishi "(checkpoint number=%llu)\n", 379783f6184SRyusuke Konishi (unsigned long long)cno); 380783f6184SRyusuke Konishi err = -EINVAL; 381783f6184SRyusuke Konishi } 382783f6184SRyusuke Konishi goto failed; 383783f6184SRyusuke Konishi } 384783f6184SRyusuke Konishi err = nilfs_read_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode); 385783f6184SRyusuke Konishi if (unlikely(err)) 386783f6184SRyusuke Konishi goto failed_bh; 387783f6184SRyusuke Konishi atomic_set(&sbi->s_inodes_count, le64_to_cpu(raw_cp->cp_inodes_count)); 388783f6184SRyusuke Konishi atomic_set(&sbi->s_blocks_count, le64_to_cpu(raw_cp->cp_blocks_count)); 389783f6184SRyusuke Konishi 390783f6184SRyusuke Konishi nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp); 391783f6184SRyusuke Konishi return 0; 392783f6184SRyusuke Konishi 393783f6184SRyusuke Konishi failed_bh: 394783f6184SRyusuke Konishi nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp); 395783f6184SRyusuke Konishi failed: 396783f6184SRyusuke Konishi nilfs_mdt_destroy(sbi->s_ifile); 397783f6184SRyusuke Konishi sbi->s_ifile = NULL; 398783f6184SRyusuke Konishi 399e59399d0SRyusuke Konishi down_write(&nilfs->ns_super_sem); 400783f6184SRyusuke Konishi list_del_init(&sbi->s_list); 401e59399d0SRyusuke Konishi up_write(&nilfs->ns_super_sem); 402783f6184SRyusuke Konishi 403783f6184SRyusuke Konishi return err; 404783f6184SRyusuke Konishi } 405783f6184SRyusuke Konishi 406783f6184SRyusuke Konishi void nilfs_detach_checkpoint(struct nilfs_sb_info *sbi) 407783f6184SRyusuke Konishi { 408783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 409783f6184SRyusuke Konishi 410783f6184SRyusuke Konishi nilfs_mdt_destroy(sbi->s_ifile); 411783f6184SRyusuke Konishi sbi->s_ifile = NULL; 412e59399d0SRyusuke Konishi down_write(&nilfs->ns_super_sem); 413783f6184SRyusuke Konishi list_del_init(&sbi->s_list); 414e59399d0SRyusuke Konishi up_write(&nilfs->ns_super_sem); 415783f6184SRyusuke Konishi } 416783f6184SRyusuke Konishi 417783f6184SRyusuke Konishi static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf) 418783f6184SRyusuke Konishi { 419783f6184SRyusuke Konishi struct super_block *sb = dentry->d_sb; 420783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 421c306af23SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 422c306af23SRyusuke Konishi u64 id = huge_encode_dev(sb->s_bdev->bd_dev); 423783f6184SRyusuke Konishi unsigned long long blocks; 424783f6184SRyusuke Konishi unsigned long overhead; 425783f6184SRyusuke Konishi unsigned long nrsvblocks; 426783f6184SRyusuke Konishi sector_t nfreeblocks; 427783f6184SRyusuke Konishi int err; 428783f6184SRyusuke Konishi 429783f6184SRyusuke Konishi /* 430783f6184SRyusuke Konishi * Compute all of the segment blocks 431783f6184SRyusuke Konishi * 432783f6184SRyusuke Konishi * The blocks before first segment and after last segment 433783f6184SRyusuke Konishi * are excluded. 434783f6184SRyusuke Konishi */ 435783f6184SRyusuke Konishi blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments 436783f6184SRyusuke Konishi - nilfs->ns_first_data_block; 437783f6184SRyusuke Konishi nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment; 438783f6184SRyusuke Konishi 439783f6184SRyusuke Konishi /* 440783f6184SRyusuke Konishi * Compute the overhead 441783f6184SRyusuke Konishi * 442783f6184SRyusuke Konishi * When distributing meta data blocks outside semgent structure, 443783f6184SRyusuke Konishi * We must count them as the overhead. 444783f6184SRyusuke Konishi */ 445783f6184SRyusuke Konishi overhead = 0; 446783f6184SRyusuke Konishi 447783f6184SRyusuke Konishi err = nilfs_count_free_blocks(nilfs, &nfreeblocks); 448783f6184SRyusuke Konishi if (unlikely(err)) 449783f6184SRyusuke Konishi return err; 450783f6184SRyusuke Konishi 451783f6184SRyusuke Konishi buf->f_type = NILFS_SUPER_MAGIC; 452783f6184SRyusuke Konishi buf->f_bsize = sb->s_blocksize; 453783f6184SRyusuke Konishi buf->f_blocks = blocks - overhead; 454783f6184SRyusuke Konishi buf->f_bfree = nfreeblocks; 455783f6184SRyusuke Konishi buf->f_bavail = (buf->f_bfree >= nrsvblocks) ? 456783f6184SRyusuke Konishi (buf->f_bfree - nrsvblocks) : 0; 457783f6184SRyusuke Konishi buf->f_files = atomic_read(&sbi->s_inodes_count); 458783f6184SRyusuke Konishi buf->f_ffree = 0; /* nilfs_count_free_inodes(sb); */ 459783f6184SRyusuke Konishi buf->f_namelen = NILFS_NAME_LEN; 460c306af23SRyusuke Konishi buf->f_fsid.val[0] = (u32)id; 461c306af23SRyusuke Konishi buf->f_fsid.val[1] = (u32)(id >> 32); 462c306af23SRyusuke Konishi 463783f6184SRyusuke Konishi return 0; 464783f6184SRyusuke Konishi } 465783f6184SRyusuke Konishi 466b58a285bSJiro SEKIBA static int nilfs_show_options(struct seq_file *seq, struct vfsmount *vfs) 467b58a285bSJiro SEKIBA { 468b58a285bSJiro SEKIBA struct super_block *sb = vfs->mnt_sb; 469b58a285bSJiro SEKIBA struct nilfs_sb_info *sbi = NILFS_SB(sb); 470b58a285bSJiro SEKIBA 471b58a285bSJiro SEKIBA if (!nilfs_test_opt(sbi, BARRIER)) 47291f1953bSJiro SEKIBA seq_printf(seq, ",nobarrier"); 473b58a285bSJiro SEKIBA if (nilfs_test_opt(sbi, SNAPSHOT)) 474b58a285bSJiro SEKIBA seq_printf(seq, ",cp=%llu", 475b58a285bSJiro SEKIBA (unsigned long long int)sbi->s_snapshot_cno); 476b58a285bSJiro SEKIBA if (nilfs_test_opt(sbi, ERRORS_RO)) 477b58a285bSJiro SEKIBA seq_printf(seq, ",errors=remount-ro"); 478b58a285bSJiro SEKIBA if (nilfs_test_opt(sbi, ERRORS_PANIC)) 479b58a285bSJiro SEKIBA seq_printf(seq, ",errors=panic"); 480b58a285bSJiro SEKIBA if (nilfs_test_opt(sbi, STRICT_ORDER)) 481b58a285bSJiro SEKIBA seq_printf(seq, ",order=strict"); 482b58a285bSJiro SEKIBA 483b58a285bSJiro SEKIBA return 0; 484b58a285bSJiro SEKIBA } 485b58a285bSJiro SEKIBA 486b87221deSAlexey Dobriyan static const struct super_operations nilfs_sops = { 487783f6184SRyusuke Konishi .alloc_inode = nilfs_alloc_inode, 488783f6184SRyusuke Konishi .destroy_inode = nilfs_destroy_inode, 489783f6184SRyusuke Konishi .dirty_inode = nilfs_dirty_inode, 490783f6184SRyusuke Konishi /* .write_inode = nilfs_write_inode, */ 491783f6184SRyusuke Konishi /* .put_inode = nilfs_put_inode, */ 492783f6184SRyusuke Konishi /* .drop_inode = nilfs_drop_inode, */ 493783f6184SRyusuke Konishi .delete_inode = nilfs_delete_inode, 494783f6184SRyusuke Konishi .put_super = nilfs_put_super, 4951dfa2710SJiro SEKIBA /* .write_super = nilfs_write_super, */ 496783f6184SRyusuke Konishi .sync_fs = nilfs_sync_fs, 497783f6184SRyusuke Konishi /* .write_super_lockfs */ 498783f6184SRyusuke Konishi /* .unlockfs */ 499783f6184SRyusuke Konishi .statfs = nilfs_statfs, 500783f6184SRyusuke Konishi .remount_fs = nilfs_remount, 501783f6184SRyusuke Konishi .clear_inode = nilfs_clear_inode, 502783f6184SRyusuke Konishi /* .umount_begin */ 503b58a285bSJiro SEKIBA .show_options = nilfs_show_options 504783f6184SRyusuke Konishi }; 505783f6184SRyusuke Konishi 506783f6184SRyusuke Konishi static struct inode * 507783f6184SRyusuke Konishi nilfs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation) 508783f6184SRyusuke Konishi { 509783f6184SRyusuke Konishi struct inode *inode; 510783f6184SRyusuke Konishi 511783f6184SRyusuke Konishi if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO && 512783f6184SRyusuke Konishi ino != NILFS_SKETCH_INO) 513783f6184SRyusuke Konishi return ERR_PTR(-ESTALE); 514783f6184SRyusuke Konishi 515783f6184SRyusuke Konishi inode = nilfs_iget(sb, ino); 516783f6184SRyusuke Konishi if (IS_ERR(inode)) 517783f6184SRyusuke Konishi return ERR_CAST(inode); 518783f6184SRyusuke Konishi if (generation && inode->i_generation != generation) { 519783f6184SRyusuke Konishi iput(inode); 520783f6184SRyusuke Konishi return ERR_PTR(-ESTALE); 521783f6184SRyusuke Konishi } 522783f6184SRyusuke Konishi 523783f6184SRyusuke Konishi return inode; 524783f6184SRyusuke Konishi } 525783f6184SRyusuke Konishi 526783f6184SRyusuke Konishi static struct dentry * 527783f6184SRyusuke Konishi nilfs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, 528783f6184SRyusuke Konishi int fh_type) 529783f6184SRyusuke Konishi { 530783f6184SRyusuke Konishi return generic_fh_to_dentry(sb, fid, fh_len, fh_type, 531783f6184SRyusuke Konishi nilfs_nfs_get_inode); 532783f6184SRyusuke Konishi } 533783f6184SRyusuke Konishi 534783f6184SRyusuke Konishi static struct dentry * 535783f6184SRyusuke Konishi nilfs_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, 536783f6184SRyusuke Konishi int fh_type) 537783f6184SRyusuke Konishi { 538783f6184SRyusuke Konishi return generic_fh_to_parent(sb, fid, fh_len, fh_type, 539783f6184SRyusuke Konishi nilfs_nfs_get_inode); 540783f6184SRyusuke Konishi } 541783f6184SRyusuke Konishi 542ac4cfdd6SAlexey Dobriyan static const struct export_operations nilfs_export_ops = { 543783f6184SRyusuke Konishi .fh_to_dentry = nilfs_fh_to_dentry, 544783f6184SRyusuke Konishi .fh_to_parent = nilfs_fh_to_parent, 545783f6184SRyusuke Konishi .get_parent = nilfs_get_parent, 546783f6184SRyusuke Konishi }; 547783f6184SRyusuke Konishi 548783f6184SRyusuke Konishi enum { 549783f6184SRyusuke Konishi Opt_err_cont, Opt_err_panic, Opt_err_ro, 55091f1953bSJiro SEKIBA Opt_nobarrier, Opt_snapshot, Opt_order, 551783f6184SRyusuke Konishi Opt_err, 552783f6184SRyusuke Konishi }; 553783f6184SRyusuke Konishi 554783f6184SRyusuke Konishi static match_table_t tokens = { 555783f6184SRyusuke Konishi {Opt_err_cont, "errors=continue"}, 556783f6184SRyusuke Konishi {Opt_err_panic, "errors=panic"}, 557783f6184SRyusuke Konishi {Opt_err_ro, "errors=remount-ro"}, 55891f1953bSJiro SEKIBA {Opt_nobarrier, "nobarrier"}, 559783f6184SRyusuke Konishi {Opt_snapshot, "cp=%u"}, 560783f6184SRyusuke Konishi {Opt_order, "order=%s"}, 561783f6184SRyusuke Konishi {Opt_err, NULL} 562783f6184SRyusuke Konishi }; 563783f6184SRyusuke Konishi 564783f6184SRyusuke Konishi static int parse_options(char *options, struct super_block *sb) 565783f6184SRyusuke Konishi { 566783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 567783f6184SRyusuke Konishi char *p; 568783f6184SRyusuke Konishi substring_t args[MAX_OPT_ARGS]; 569783f6184SRyusuke Konishi int option; 570783f6184SRyusuke Konishi 571783f6184SRyusuke Konishi if (!options) 572783f6184SRyusuke Konishi return 1; 573783f6184SRyusuke Konishi 574783f6184SRyusuke Konishi while ((p = strsep(&options, ",")) != NULL) { 575783f6184SRyusuke Konishi int token; 576783f6184SRyusuke Konishi if (!*p) 577783f6184SRyusuke Konishi continue; 578783f6184SRyusuke Konishi 579783f6184SRyusuke Konishi token = match_token(p, tokens, args); 580783f6184SRyusuke Konishi switch (token) { 58191f1953bSJiro SEKIBA case Opt_nobarrier: 582783f6184SRyusuke Konishi nilfs_clear_opt(sbi, BARRIER); 583783f6184SRyusuke Konishi break; 584783f6184SRyusuke Konishi case Opt_order: 585783f6184SRyusuke Konishi if (strcmp(args[0].from, "relaxed") == 0) 586783f6184SRyusuke Konishi /* Ordered data semantics */ 587783f6184SRyusuke Konishi nilfs_clear_opt(sbi, STRICT_ORDER); 588783f6184SRyusuke Konishi else if (strcmp(args[0].from, "strict") == 0) 589783f6184SRyusuke Konishi /* Strict in-order semantics */ 590783f6184SRyusuke Konishi nilfs_set_opt(sbi, STRICT_ORDER); 591783f6184SRyusuke Konishi else 592783f6184SRyusuke Konishi return 0; 593783f6184SRyusuke Konishi break; 594783f6184SRyusuke Konishi case Opt_err_panic: 595783f6184SRyusuke Konishi nilfs_write_opt(sbi, ERROR_MODE, ERRORS_PANIC); 596783f6184SRyusuke Konishi break; 597783f6184SRyusuke Konishi case Opt_err_ro: 598783f6184SRyusuke Konishi nilfs_write_opt(sbi, ERROR_MODE, ERRORS_RO); 599783f6184SRyusuke Konishi break; 600783f6184SRyusuke Konishi case Opt_err_cont: 601783f6184SRyusuke Konishi nilfs_write_opt(sbi, ERROR_MODE, ERRORS_CONT); 602783f6184SRyusuke Konishi break; 603783f6184SRyusuke Konishi case Opt_snapshot: 604783f6184SRyusuke Konishi if (match_int(&args[0], &option) || option <= 0) 605783f6184SRyusuke Konishi return 0; 606783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) 607783f6184SRyusuke Konishi return 0; 608783f6184SRyusuke Konishi sbi->s_snapshot_cno = option; 609783f6184SRyusuke Konishi nilfs_set_opt(sbi, SNAPSHOT); 610783f6184SRyusuke Konishi break; 611783f6184SRyusuke Konishi default: 612783f6184SRyusuke Konishi printk(KERN_ERR 613783f6184SRyusuke Konishi "NILFS: Unrecognized mount option \"%s\"\n", p); 614783f6184SRyusuke Konishi return 0; 615783f6184SRyusuke Konishi } 616783f6184SRyusuke Konishi } 617783f6184SRyusuke Konishi return 1; 618783f6184SRyusuke Konishi } 619783f6184SRyusuke Konishi 620783f6184SRyusuke Konishi static inline void 621783f6184SRyusuke Konishi nilfs_set_default_options(struct nilfs_sb_info *sbi, 622783f6184SRyusuke Konishi struct nilfs_super_block *sbp) 623783f6184SRyusuke Konishi { 624783f6184SRyusuke Konishi sbi->s_mount_opt = 625783f6184SRyusuke Konishi NILFS_MOUNT_ERRORS_CONT | NILFS_MOUNT_BARRIER; 626783f6184SRyusuke Konishi } 627783f6184SRyusuke Konishi 628783f6184SRyusuke Konishi static int nilfs_setup_super(struct nilfs_sb_info *sbi) 629783f6184SRyusuke Konishi { 630783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 631e339ad31SRyusuke Konishi struct nilfs_super_block *sbp = nilfs->ns_sbp[0]; 632783f6184SRyusuke Konishi int max_mnt_count = le16_to_cpu(sbp->s_max_mnt_count); 633783f6184SRyusuke Konishi int mnt_count = le16_to_cpu(sbp->s_mnt_count); 634783f6184SRyusuke Konishi 635783f6184SRyusuke Konishi /* nilfs->sem must be locked by the caller. */ 636*f50a4c81SRyusuke Konishi if (nilfs->ns_mount_state & NILFS_ERROR_FS) { 637783f6184SRyusuke Konishi printk(KERN_WARNING 638783f6184SRyusuke Konishi "NILFS warning: mounting fs with errors\n"); 639783f6184SRyusuke Konishi #if 0 640783f6184SRyusuke Konishi } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) { 641783f6184SRyusuke Konishi printk(KERN_WARNING 642783f6184SRyusuke Konishi "NILFS warning: maximal mount count reached\n"); 643783f6184SRyusuke Konishi #endif 644783f6184SRyusuke Konishi } 645783f6184SRyusuke Konishi if (!max_mnt_count) 646783f6184SRyusuke Konishi sbp->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT); 647783f6184SRyusuke Konishi 648783f6184SRyusuke Konishi sbp->s_mnt_count = cpu_to_le16(mnt_count + 1); 649783f6184SRyusuke Konishi sbp->s_state = cpu_to_le16(le16_to_cpu(sbp->s_state) & ~NILFS_VALID_FS); 650783f6184SRyusuke Konishi sbp->s_mtime = cpu_to_le64(get_seconds()); 651e339ad31SRyusuke Konishi return nilfs_commit_super(sbi, 1); 652783f6184SRyusuke Konishi } 653783f6184SRyusuke Konishi 654e339ad31SRyusuke Konishi struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb, 655e339ad31SRyusuke Konishi u64 pos, int blocksize, 656e339ad31SRyusuke Konishi struct buffer_head **pbh) 657783f6184SRyusuke Konishi { 658e339ad31SRyusuke Konishi unsigned long long sb_index = pos; 659e339ad31SRyusuke Konishi unsigned long offset; 660783f6184SRyusuke Konishi 661e339ad31SRyusuke Konishi offset = do_div(sb_index, blocksize); 662783f6184SRyusuke Konishi *pbh = sb_bread(sb, sb_index); 663e339ad31SRyusuke Konishi if (!*pbh) 664783f6184SRyusuke Konishi return NULL; 665783f6184SRyusuke Konishi return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset); 666783f6184SRyusuke Konishi } 667783f6184SRyusuke Konishi 668783f6184SRyusuke Konishi int nilfs_store_magic_and_option(struct super_block *sb, 669783f6184SRyusuke Konishi struct nilfs_super_block *sbp, 670783f6184SRyusuke Konishi char *data) 671783f6184SRyusuke Konishi { 672783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 673783f6184SRyusuke Konishi 674783f6184SRyusuke Konishi sb->s_magic = le16_to_cpu(sbp->s_magic); 675783f6184SRyusuke Konishi 676783f6184SRyusuke Konishi /* FS independent flags */ 677783f6184SRyusuke Konishi #ifdef NILFS_ATIME_DISABLE 678783f6184SRyusuke Konishi sb->s_flags |= MS_NOATIME; 679783f6184SRyusuke Konishi #endif 680783f6184SRyusuke Konishi 681783f6184SRyusuke Konishi nilfs_set_default_options(sbi, sbp); 682783f6184SRyusuke Konishi 683783f6184SRyusuke Konishi sbi->s_resuid = le16_to_cpu(sbp->s_def_resuid); 684783f6184SRyusuke Konishi sbi->s_resgid = le16_to_cpu(sbp->s_def_resgid); 685783f6184SRyusuke Konishi sbi->s_interval = le32_to_cpu(sbp->s_c_interval); 686783f6184SRyusuke Konishi sbi->s_watermark = le32_to_cpu(sbp->s_c_block_max); 687783f6184SRyusuke Konishi 688e339ad31SRyusuke Konishi return !parse_options(data, sb) ? -EINVAL : 0 ; 689783f6184SRyusuke Konishi } 690783f6184SRyusuke Konishi 691783f6184SRyusuke Konishi /** 692783f6184SRyusuke Konishi * nilfs_fill_super() - initialize a super block instance 693783f6184SRyusuke Konishi * @sb: super_block 694783f6184SRyusuke Konishi * @data: mount options 695783f6184SRyusuke Konishi * @silent: silent mode flag 696783f6184SRyusuke Konishi * @nilfs: the_nilfs struct 697783f6184SRyusuke Konishi * 698aa7dfb89SRyusuke Konishi * This function is called exclusively by nilfs->ns_mount_mutex. 699783f6184SRyusuke Konishi * So, the recovery process is protected from other simultaneous mounts. 700783f6184SRyusuke Konishi */ 701783f6184SRyusuke Konishi static int 702783f6184SRyusuke Konishi nilfs_fill_super(struct super_block *sb, void *data, int silent, 703783f6184SRyusuke Konishi struct the_nilfs *nilfs) 704783f6184SRyusuke Konishi { 705783f6184SRyusuke Konishi struct nilfs_sb_info *sbi; 706783f6184SRyusuke Konishi struct inode *root; 707783f6184SRyusuke Konishi __u64 cno; 708783f6184SRyusuke Konishi int err; 709783f6184SRyusuke Konishi 710783f6184SRyusuke Konishi sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); 711783f6184SRyusuke Konishi if (!sbi) 712783f6184SRyusuke Konishi return -ENOMEM; 713783f6184SRyusuke Konishi 714783f6184SRyusuke Konishi sb->s_fs_info = sbi; 715783f6184SRyusuke Konishi 716783f6184SRyusuke Konishi get_nilfs(nilfs); 717783f6184SRyusuke Konishi sbi->s_nilfs = nilfs; 718783f6184SRyusuke Konishi sbi->s_super = sb; 7196dd47406SRyusuke Konishi atomic_set(&sbi->s_count, 1); 720783f6184SRyusuke Konishi 721783f6184SRyusuke Konishi err = init_nilfs(nilfs, sbi, (char *)data); 722783f6184SRyusuke Konishi if (err) 723783f6184SRyusuke Konishi goto failed_sbi; 724783f6184SRyusuke Konishi 725783f6184SRyusuke Konishi spin_lock_init(&sbi->s_inode_lock); 726783f6184SRyusuke Konishi INIT_LIST_HEAD(&sbi->s_dirty_files); 727783f6184SRyusuke Konishi INIT_LIST_HEAD(&sbi->s_list); 728783f6184SRyusuke Konishi 729783f6184SRyusuke Konishi /* 730783f6184SRyusuke Konishi * Following initialization is overlapped because 731783f6184SRyusuke Konishi * nilfs_sb_info structure has been cleared at the beginning. 732783f6184SRyusuke Konishi * But we reserve them to keep our interest and make ready 733783f6184SRyusuke Konishi * for the future change. 734783f6184SRyusuke Konishi */ 735783f6184SRyusuke Konishi get_random_bytes(&sbi->s_next_generation, 736783f6184SRyusuke Konishi sizeof(sbi->s_next_generation)); 737783f6184SRyusuke Konishi spin_lock_init(&sbi->s_next_gen_lock); 738783f6184SRyusuke Konishi 739783f6184SRyusuke Konishi sb->s_op = &nilfs_sops; 740783f6184SRyusuke Konishi sb->s_export_op = &nilfs_export_ops; 741783f6184SRyusuke Konishi sb->s_root = NULL; 74261239230SRyusuke Konishi sb->s_time_gran = 1; 743783f6184SRyusuke Konishi 744783f6184SRyusuke Konishi err = load_nilfs(nilfs, sbi); 745783f6184SRyusuke Konishi if (err) 746783f6184SRyusuke Konishi goto failed_sbi; 747*f50a4c81SRyusuke Konishi 748783f6184SRyusuke Konishi cno = nilfs_last_cno(nilfs); 749783f6184SRyusuke Konishi 750783f6184SRyusuke Konishi if (sb->s_flags & MS_RDONLY) { 751783f6184SRyusuke Konishi if (nilfs_test_opt(sbi, SNAPSHOT)) { 75243be0ec0SZhu Yanhai down_read(&nilfs->ns_segctor_sem); 7531f5abe7eSRyusuke Konishi err = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, 7541f5abe7eSRyusuke Konishi sbi->s_snapshot_cno); 75543be0ec0SZhu Yanhai up_read(&nilfs->ns_segctor_sem); 75643be0ec0SZhu Yanhai if (err < 0) { 75743be0ec0SZhu Yanhai if (err == -ENOENT) 75843be0ec0SZhu Yanhai err = -EINVAL; 7591f5abe7eSRyusuke Konishi goto failed_sbi; 76043be0ec0SZhu Yanhai } 7611f5abe7eSRyusuke Konishi if (!err) { 762783f6184SRyusuke Konishi printk(KERN_ERR 763783f6184SRyusuke Konishi "NILFS: The specified checkpoint is " 764783f6184SRyusuke Konishi "not a snapshot " 765783f6184SRyusuke Konishi "(checkpoint number=%llu).\n", 766783f6184SRyusuke Konishi (unsigned long long)sbi->s_snapshot_cno); 767783f6184SRyusuke Konishi err = -EINVAL; 768783f6184SRyusuke Konishi goto failed_sbi; 769783f6184SRyusuke Konishi } 770783f6184SRyusuke Konishi cno = sbi->s_snapshot_cno; 771783f6184SRyusuke Konishi } else 772783f6184SRyusuke Konishi /* Read-only mount */ 773783f6184SRyusuke Konishi sbi->s_snapshot_cno = cno; 774783f6184SRyusuke Konishi } 775783f6184SRyusuke Konishi 776783f6184SRyusuke Konishi err = nilfs_attach_checkpoint(sbi, cno); 777783f6184SRyusuke Konishi if (err) { 778783f6184SRyusuke Konishi printk(KERN_ERR "NILFS: error loading a checkpoint" 779783f6184SRyusuke Konishi " (checkpoint number=%llu).\n", (unsigned long long)cno); 780783f6184SRyusuke Konishi goto failed_sbi; 781783f6184SRyusuke Konishi } 782783f6184SRyusuke Konishi 783783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 784cece5520SRyusuke Konishi err = nilfs_attach_segment_constructor(sbi); 785783f6184SRyusuke Konishi if (err) 786783f6184SRyusuke Konishi goto failed_checkpoint; 787783f6184SRyusuke Konishi } 788783f6184SRyusuke Konishi 789783f6184SRyusuke Konishi root = nilfs_iget(sb, NILFS_ROOT_INO); 790783f6184SRyusuke Konishi if (IS_ERR(root)) { 791783f6184SRyusuke Konishi printk(KERN_ERR "NILFS: get root inode failed\n"); 792783f6184SRyusuke Konishi err = PTR_ERR(root); 793783f6184SRyusuke Konishi goto failed_segctor; 794783f6184SRyusuke Konishi } 795783f6184SRyusuke Konishi if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) { 796783f6184SRyusuke Konishi iput(root); 797783f6184SRyusuke Konishi printk(KERN_ERR "NILFS: corrupt root inode.\n"); 798783f6184SRyusuke Konishi err = -EINVAL; 799783f6184SRyusuke Konishi goto failed_segctor; 800783f6184SRyusuke Konishi } 801783f6184SRyusuke Konishi sb->s_root = d_alloc_root(root); 802783f6184SRyusuke Konishi if (!sb->s_root) { 803783f6184SRyusuke Konishi iput(root); 804783f6184SRyusuke Konishi printk(KERN_ERR "NILFS: get root dentry failed\n"); 805783f6184SRyusuke Konishi err = -ENOMEM; 806783f6184SRyusuke Konishi goto failed_segctor; 807783f6184SRyusuke Konishi } 808783f6184SRyusuke Konishi 809783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 810783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 811783f6184SRyusuke Konishi nilfs_setup_super(sbi); 812783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 813783f6184SRyusuke Konishi } 814783f6184SRyusuke Konishi 815e59399d0SRyusuke Konishi down_write(&nilfs->ns_super_sem); 8163f82ff55SRyusuke Konishi if (!nilfs_test_opt(sbi, SNAPSHOT)) 8173f82ff55SRyusuke Konishi nilfs->ns_current = sbi; 818e59399d0SRyusuke Konishi up_write(&nilfs->ns_super_sem); 8193f82ff55SRyusuke Konishi 820783f6184SRyusuke Konishi return 0; 821783f6184SRyusuke Konishi 822783f6184SRyusuke Konishi failed_segctor: 823783f6184SRyusuke Konishi nilfs_detach_segment_constructor(sbi); 824783f6184SRyusuke Konishi 825783f6184SRyusuke Konishi failed_checkpoint: 826783f6184SRyusuke Konishi nilfs_detach_checkpoint(sbi); 827783f6184SRyusuke Konishi 828783f6184SRyusuke Konishi failed_sbi: 829783f6184SRyusuke Konishi put_nilfs(nilfs); 830783f6184SRyusuke Konishi sb->s_fs_info = NULL; 8316dd47406SRyusuke Konishi nilfs_put_sbinfo(sbi); 832783f6184SRyusuke Konishi return err; 833783f6184SRyusuke Konishi } 834783f6184SRyusuke Konishi 835783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data) 836783f6184SRyusuke Konishi { 837783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 838783f6184SRyusuke Konishi struct nilfs_super_block *sbp; 839783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 840783f6184SRyusuke Konishi unsigned long old_sb_flags; 841783f6184SRyusuke Konishi struct nilfs_mount_options old_opts; 842783f6184SRyusuke Konishi int err; 843783f6184SRyusuke Konishi 844337eb00aSAlessio Igor Bogani lock_kernel(); 845337eb00aSAlessio Igor Bogani 846e59399d0SRyusuke Konishi down_write(&nilfs->ns_super_sem); 847783f6184SRyusuke Konishi old_sb_flags = sb->s_flags; 848783f6184SRyusuke Konishi old_opts.mount_opt = sbi->s_mount_opt; 849783f6184SRyusuke Konishi old_opts.snapshot_cno = sbi->s_snapshot_cno; 850783f6184SRyusuke Konishi 851783f6184SRyusuke Konishi if (!parse_options(data, sb)) { 852783f6184SRyusuke Konishi err = -EINVAL; 853783f6184SRyusuke Konishi goto restore_opts; 854783f6184SRyusuke Konishi } 855783f6184SRyusuke Konishi sb->s_flags = (sb->s_flags & ~MS_POSIXACL); 856783f6184SRyusuke Konishi 857783f6184SRyusuke Konishi if ((*flags & MS_RDONLY) && 858783f6184SRyusuke Konishi sbi->s_snapshot_cno != old_opts.snapshot_cno) { 859783f6184SRyusuke Konishi printk(KERN_WARNING "NILFS (device %s): couldn't " 860783f6184SRyusuke Konishi "remount to a different snapshot. \n", 861783f6184SRyusuke Konishi sb->s_id); 862783f6184SRyusuke Konishi err = -EINVAL; 863783f6184SRyusuke Konishi goto restore_opts; 864783f6184SRyusuke Konishi } 865783f6184SRyusuke Konishi 866783f6184SRyusuke Konishi if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 867783f6184SRyusuke Konishi goto out; 868783f6184SRyusuke Konishi if (*flags & MS_RDONLY) { 869783f6184SRyusuke Konishi /* Shutting down the segment constructor */ 870783f6184SRyusuke Konishi nilfs_detach_segment_constructor(sbi); 871783f6184SRyusuke Konishi sb->s_flags |= MS_RDONLY; 872783f6184SRyusuke Konishi 873783f6184SRyusuke Konishi sbi->s_snapshot_cno = nilfs_last_cno(nilfs); 874783f6184SRyusuke Konishi /* nilfs_set_opt(sbi, SNAPSHOT); */ 875783f6184SRyusuke Konishi 876783f6184SRyusuke Konishi /* 877783f6184SRyusuke Konishi * Remounting a valid RW partition RDONLY, so set 878783f6184SRyusuke Konishi * the RDONLY flag and then mark the partition as valid again. 879783f6184SRyusuke Konishi */ 880783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 881e339ad31SRyusuke Konishi sbp = nilfs->ns_sbp[0]; 882783f6184SRyusuke Konishi if (!(sbp->s_state & le16_to_cpu(NILFS_VALID_FS)) && 883783f6184SRyusuke Konishi (nilfs->ns_mount_state & NILFS_VALID_FS)) 884783f6184SRyusuke Konishi sbp->s_state = cpu_to_le16(nilfs->ns_mount_state); 885783f6184SRyusuke Konishi sbp->s_mtime = cpu_to_le64(get_seconds()); 886e339ad31SRyusuke Konishi nilfs_commit_super(sbi, 1); 887783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 888783f6184SRyusuke Konishi } else { 889783f6184SRyusuke Konishi /* 890783f6184SRyusuke Konishi * Mounting a RDONLY partition read-write, so reread and 891783f6184SRyusuke Konishi * store the current valid flag. (It may have been changed 892783f6184SRyusuke Konishi * by fsck since we originally mounted the partition.) 893783f6184SRyusuke Konishi */ 8943f82ff55SRyusuke Konishi if (nilfs->ns_current && nilfs->ns_current != sbi) { 895783f6184SRyusuke Konishi printk(KERN_WARNING "NILFS (device %s): couldn't " 8963f82ff55SRyusuke Konishi "remount because an RW-mount exists.\n", 897783f6184SRyusuke Konishi sb->s_id); 898783f6184SRyusuke Konishi err = -EBUSY; 899e59399d0SRyusuke Konishi goto restore_opts; 900783f6184SRyusuke Konishi } 901783f6184SRyusuke Konishi if (sbi->s_snapshot_cno != nilfs_last_cno(nilfs)) { 902783f6184SRyusuke Konishi printk(KERN_WARNING "NILFS (device %s): couldn't " 903783f6184SRyusuke Konishi "remount because the current RO-mount is not " 904783f6184SRyusuke Konishi "the latest one.\n", 905783f6184SRyusuke Konishi sb->s_id); 906783f6184SRyusuke Konishi err = -EINVAL; 907e59399d0SRyusuke Konishi goto restore_opts; 908783f6184SRyusuke Konishi } 909783f6184SRyusuke Konishi sb->s_flags &= ~MS_RDONLY; 910783f6184SRyusuke Konishi nilfs_clear_opt(sbi, SNAPSHOT); 911783f6184SRyusuke Konishi sbi->s_snapshot_cno = 0; 912783f6184SRyusuke Konishi 913cece5520SRyusuke Konishi err = nilfs_attach_segment_constructor(sbi); 914783f6184SRyusuke Konishi if (err) 915e59399d0SRyusuke Konishi goto restore_opts; 916783f6184SRyusuke Konishi 917783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 918783f6184SRyusuke Konishi nilfs_setup_super(sbi); 919783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 920783f6184SRyusuke Konishi 921e59399d0SRyusuke Konishi nilfs->ns_current = sbi; 922783f6184SRyusuke Konishi } 923783f6184SRyusuke Konishi out: 924e59399d0SRyusuke Konishi up_write(&nilfs->ns_super_sem); 925337eb00aSAlessio Igor Bogani unlock_kernel(); 926783f6184SRyusuke Konishi return 0; 927783f6184SRyusuke Konishi 928783f6184SRyusuke Konishi restore_opts: 929783f6184SRyusuke Konishi sb->s_flags = old_sb_flags; 930783f6184SRyusuke Konishi sbi->s_mount_opt = old_opts.mount_opt; 931783f6184SRyusuke Konishi sbi->s_snapshot_cno = old_opts.snapshot_cno; 932e59399d0SRyusuke Konishi up_write(&nilfs->ns_super_sem); 933337eb00aSAlessio Igor Bogani unlock_kernel(); 934783f6184SRyusuke Konishi return err; 935783f6184SRyusuke Konishi } 936783f6184SRyusuke Konishi 937783f6184SRyusuke Konishi struct nilfs_super_data { 938783f6184SRyusuke Konishi struct block_device *bdev; 9396dd47406SRyusuke Konishi struct nilfs_sb_info *sbi; 940783f6184SRyusuke Konishi __u64 cno; 941783f6184SRyusuke Konishi int flags; 942783f6184SRyusuke Konishi }; 943783f6184SRyusuke Konishi 944783f6184SRyusuke Konishi /** 945783f6184SRyusuke Konishi * nilfs_identify - pre-read mount options needed to identify mount instance 946783f6184SRyusuke Konishi * @data: mount options 947783f6184SRyusuke Konishi * @sd: nilfs_super_data 948783f6184SRyusuke Konishi */ 949783f6184SRyusuke Konishi static int nilfs_identify(char *data, struct nilfs_super_data *sd) 950783f6184SRyusuke Konishi { 951783f6184SRyusuke Konishi char *p, *options = data; 952783f6184SRyusuke Konishi substring_t args[MAX_OPT_ARGS]; 953783f6184SRyusuke Konishi int option, token; 954783f6184SRyusuke Konishi int ret = 0; 955783f6184SRyusuke Konishi 956783f6184SRyusuke Konishi do { 957783f6184SRyusuke Konishi p = strsep(&options, ","); 958783f6184SRyusuke Konishi if (p != NULL && *p) { 959783f6184SRyusuke Konishi token = match_token(p, tokens, args); 960783f6184SRyusuke Konishi if (token == Opt_snapshot) { 961783f6184SRyusuke Konishi if (!(sd->flags & MS_RDONLY)) 962783f6184SRyusuke Konishi ret++; 963783f6184SRyusuke Konishi else { 964783f6184SRyusuke Konishi ret = match_int(&args[0], &option); 965783f6184SRyusuke Konishi if (!ret) { 966783f6184SRyusuke Konishi if (option > 0) 967783f6184SRyusuke Konishi sd->cno = option; 968783f6184SRyusuke Konishi else 969783f6184SRyusuke Konishi ret++; 970783f6184SRyusuke Konishi } 971783f6184SRyusuke Konishi } 972783f6184SRyusuke Konishi } 973783f6184SRyusuke Konishi if (ret) 974783f6184SRyusuke Konishi printk(KERN_ERR 975783f6184SRyusuke Konishi "NILFS: invalid mount option: %s\n", p); 976783f6184SRyusuke Konishi } 977783f6184SRyusuke Konishi if (!options) 978783f6184SRyusuke Konishi break; 979783f6184SRyusuke Konishi BUG_ON(options == data); 980783f6184SRyusuke Konishi *(options - 1) = ','; 981783f6184SRyusuke Konishi } while (!ret); 982783f6184SRyusuke Konishi return ret; 983783f6184SRyusuke Konishi } 984783f6184SRyusuke Konishi 985783f6184SRyusuke Konishi static int nilfs_set_bdev_super(struct super_block *s, void *data) 986783f6184SRyusuke Konishi { 987783f6184SRyusuke Konishi struct nilfs_super_data *sd = data; 988783f6184SRyusuke Konishi 989783f6184SRyusuke Konishi s->s_bdev = sd->bdev; 990783f6184SRyusuke Konishi s->s_dev = s->s_bdev->bd_dev; 991783f6184SRyusuke Konishi return 0; 992783f6184SRyusuke Konishi } 993783f6184SRyusuke Konishi 994783f6184SRyusuke Konishi static int nilfs_test_bdev_super(struct super_block *s, void *data) 995783f6184SRyusuke Konishi { 996783f6184SRyusuke Konishi struct nilfs_super_data *sd = data; 997783f6184SRyusuke Konishi 9986dd47406SRyusuke Konishi return sd->sbi && s->s_fs_info == (void *)sd->sbi; 999783f6184SRyusuke Konishi } 1000783f6184SRyusuke Konishi 1001783f6184SRyusuke Konishi static int 1002783f6184SRyusuke Konishi nilfs_get_sb(struct file_system_type *fs_type, int flags, 1003783f6184SRyusuke Konishi const char *dev_name, void *data, struct vfsmount *mnt) 1004783f6184SRyusuke Konishi { 1005783f6184SRyusuke Konishi struct nilfs_super_data sd; 100633c8e57cSRyusuke Konishi struct super_block *s; 100733c8e57cSRyusuke Konishi struct the_nilfs *nilfs; 1008783f6184SRyusuke Konishi int err, need_to_close = 1; 1009783f6184SRyusuke Konishi 1010783f6184SRyusuke Konishi sd.bdev = open_bdev_exclusive(dev_name, flags, fs_type); 1011783f6184SRyusuke Konishi if (IS_ERR(sd.bdev)) 1012783f6184SRyusuke Konishi return PTR_ERR(sd.bdev); 1013783f6184SRyusuke Konishi 1014783f6184SRyusuke Konishi /* 1015783f6184SRyusuke Konishi * To get mount instance using sget() vfs-routine, NILFS needs 1016783f6184SRyusuke Konishi * much more information than normal filesystems to identify mount 1017783f6184SRyusuke Konishi * instance. For snapshot mounts, not only a mount type (ro-mount 1018783f6184SRyusuke Konishi * or rw-mount) but also a checkpoint number is required. 1019783f6184SRyusuke Konishi */ 1020783f6184SRyusuke Konishi sd.cno = 0; 1021783f6184SRyusuke Konishi sd.flags = flags; 1022783f6184SRyusuke Konishi if (nilfs_identify((char *)data, &sd)) { 1023783f6184SRyusuke Konishi err = -EINVAL; 1024783f6184SRyusuke Konishi goto failed; 1025783f6184SRyusuke Konishi } 1026783f6184SRyusuke Konishi 102733c8e57cSRyusuke Konishi nilfs = find_or_create_nilfs(sd.bdev); 102833c8e57cSRyusuke Konishi if (!nilfs) { 102933c8e57cSRyusuke Konishi err = -ENOMEM; 103033c8e57cSRyusuke Konishi goto failed; 103133c8e57cSRyusuke Konishi } 103233c8e57cSRyusuke Konishi 1033aa7dfb89SRyusuke Konishi mutex_lock(&nilfs->ns_mount_mutex); 10343f82ff55SRyusuke Konishi 10353f82ff55SRyusuke Konishi if (!sd.cno) { 10363f82ff55SRyusuke Konishi /* 10373f82ff55SRyusuke Konishi * Check if an exclusive mount exists or not. 10383f82ff55SRyusuke Konishi * Snapshot mounts coexist with a current mount 10393f82ff55SRyusuke Konishi * (i.e. rw-mount or ro-mount), whereas rw-mount and 10403f82ff55SRyusuke Konishi * ro-mount are mutually exclusive. 10413f82ff55SRyusuke Konishi */ 1042e59399d0SRyusuke Konishi down_read(&nilfs->ns_super_sem); 10433f82ff55SRyusuke Konishi if (nilfs->ns_current && 10443f82ff55SRyusuke Konishi ((nilfs->ns_current->s_super->s_flags ^ flags) 10453f82ff55SRyusuke Konishi & MS_RDONLY)) { 1046e59399d0SRyusuke Konishi up_read(&nilfs->ns_super_sem); 10473f82ff55SRyusuke Konishi err = -EBUSY; 1048783f6184SRyusuke Konishi goto failed_unlock; 1049783f6184SRyusuke Konishi } 1050e59399d0SRyusuke Konishi up_read(&nilfs->ns_super_sem); 10513f82ff55SRyusuke Konishi } 1052783f6184SRyusuke Konishi 1053783f6184SRyusuke Konishi /* 10546dd47406SRyusuke Konishi * Find existing nilfs_sb_info struct 1055783f6184SRyusuke Konishi */ 10566dd47406SRyusuke Konishi sd.sbi = nilfs_find_sbinfo(nilfs, !(flags & MS_RDONLY), sd.cno); 10576dd47406SRyusuke Konishi 10586dd47406SRyusuke Konishi /* 10596dd47406SRyusuke Konishi * Get super block instance holding the nilfs_sb_info struct. 10606dd47406SRyusuke Konishi * A new instance is allocated if no existing mount is present or 10616dd47406SRyusuke Konishi * existing instance has been unmounted. 10626dd47406SRyusuke Konishi */ 106333c8e57cSRyusuke Konishi s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, &sd); 10646dd47406SRyusuke Konishi if (sd.sbi) 10656dd47406SRyusuke Konishi nilfs_put_sbinfo(sd.sbi); 10666dd47406SRyusuke Konishi 106733c8e57cSRyusuke Konishi if (IS_ERR(s)) { 106833c8e57cSRyusuke Konishi err = PTR_ERR(s); 106933c8e57cSRyusuke Konishi goto failed_unlock; 1070783f6184SRyusuke Konishi } 1071783f6184SRyusuke Konishi 1072783f6184SRyusuke Konishi if (!s->s_root) { 1073783f6184SRyusuke Konishi char b[BDEVNAME_SIZE]; 1074783f6184SRyusuke Konishi 107533c8e57cSRyusuke Konishi /* New superblock instance created */ 1076783f6184SRyusuke Konishi s->s_flags = flags; 1077783f6184SRyusuke Konishi strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id)); 1078783f6184SRyusuke Konishi sb_set_blocksize(s, block_size(sd.bdev)); 1079783f6184SRyusuke Konishi 1080783f6184SRyusuke Konishi err = nilfs_fill_super(s, data, flags & MS_VERBOSE, nilfs); 1081783f6184SRyusuke Konishi if (err) 1082783f6184SRyusuke Konishi goto cancel_new; 1083783f6184SRyusuke Konishi 1084783f6184SRyusuke Konishi s->s_flags |= MS_ACTIVE; 1085783f6184SRyusuke Konishi need_to_close = 0; 1086783f6184SRyusuke Konishi } 1087783f6184SRyusuke Konishi 1088aa7dfb89SRyusuke Konishi mutex_unlock(&nilfs->ns_mount_mutex); 1089783f6184SRyusuke Konishi put_nilfs(nilfs); 1090783f6184SRyusuke Konishi if (need_to_close) 1091783f6184SRyusuke Konishi close_bdev_exclusive(sd.bdev, flags); 1092783f6184SRyusuke Konishi simple_set_mnt(mnt, s); 1093783f6184SRyusuke Konishi return 0; 1094783f6184SRyusuke Konishi 1095783f6184SRyusuke Konishi failed_unlock: 1096aa7dfb89SRyusuke Konishi mutex_unlock(&nilfs->ns_mount_mutex); 109733c8e57cSRyusuke Konishi put_nilfs(nilfs); 1098783f6184SRyusuke Konishi failed: 1099783f6184SRyusuke Konishi close_bdev_exclusive(sd.bdev, flags); 1100783f6184SRyusuke Konishi 1101783f6184SRyusuke Konishi return err; 1102783f6184SRyusuke Konishi 1103783f6184SRyusuke Konishi cancel_new: 1104783f6184SRyusuke Konishi /* Abandoning the newly allocated superblock */ 1105aa7dfb89SRyusuke Konishi mutex_unlock(&nilfs->ns_mount_mutex); 1106783f6184SRyusuke Konishi put_nilfs(nilfs); 1107783f6184SRyusuke Konishi up_write(&s->s_umount); 1108783f6184SRyusuke Konishi deactivate_super(s); 1109783f6184SRyusuke Konishi /* 1110783f6184SRyusuke Konishi * deactivate_super() invokes close_bdev_exclusive(). 1111783f6184SRyusuke Konishi * We must finish all post-cleaning before this call; 1112aa7dfb89SRyusuke Konishi * put_nilfs() needs the block device. 1113783f6184SRyusuke Konishi */ 1114783f6184SRyusuke Konishi return err; 1115783f6184SRyusuke Konishi } 1116783f6184SRyusuke Konishi 1117783f6184SRyusuke Konishi struct file_system_type nilfs_fs_type = { 1118783f6184SRyusuke Konishi .owner = THIS_MODULE, 1119783f6184SRyusuke Konishi .name = "nilfs2", 1120783f6184SRyusuke Konishi .get_sb = nilfs_get_sb, 1121783f6184SRyusuke Konishi .kill_sb = kill_block_super, 1122783f6184SRyusuke Konishi .fs_flags = FS_REQUIRES_DEV, 1123783f6184SRyusuke Konishi }; 1124783f6184SRyusuke Konishi 1125783f6184SRyusuke Konishi static int __init init_nilfs_fs(void) 1126783f6184SRyusuke Konishi { 1127783f6184SRyusuke Konishi int err; 1128783f6184SRyusuke Konishi 1129783f6184SRyusuke Konishi err = nilfs_init_inode_cache(); 1130783f6184SRyusuke Konishi if (err) 1131783f6184SRyusuke Konishi goto failed; 1132783f6184SRyusuke Konishi 1133783f6184SRyusuke Konishi err = nilfs_init_transaction_cache(); 1134783f6184SRyusuke Konishi if (err) 1135783f6184SRyusuke Konishi goto failed_inode_cache; 1136783f6184SRyusuke Konishi 1137783f6184SRyusuke Konishi err = nilfs_init_segbuf_cache(); 1138783f6184SRyusuke Konishi if (err) 1139783f6184SRyusuke Konishi goto failed_transaction_cache; 1140783f6184SRyusuke Konishi 1141783f6184SRyusuke Konishi err = nilfs_btree_path_cache_init(); 1142783f6184SRyusuke Konishi if (err) 1143783f6184SRyusuke Konishi goto failed_segbuf_cache; 1144783f6184SRyusuke Konishi 1145783f6184SRyusuke Konishi err = register_filesystem(&nilfs_fs_type); 1146783f6184SRyusuke Konishi if (err) 1147783f6184SRyusuke Konishi goto failed_btree_path_cache; 1148783f6184SRyusuke Konishi 1149783f6184SRyusuke Konishi return 0; 1150783f6184SRyusuke Konishi 1151783f6184SRyusuke Konishi failed_btree_path_cache: 1152783f6184SRyusuke Konishi nilfs_btree_path_cache_destroy(); 1153783f6184SRyusuke Konishi 1154783f6184SRyusuke Konishi failed_segbuf_cache: 1155783f6184SRyusuke Konishi nilfs_destroy_segbuf_cache(); 1156783f6184SRyusuke Konishi 1157783f6184SRyusuke Konishi failed_transaction_cache: 1158783f6184SRyusuke Konishi nilfs_destroy_transaction_cache(); 1159783f6184SRyusuke Konishi 1160783f6184SRyusuke Konishi failed_inode_cache: 1161783f6184SRyusuke Konishi nilfs_destroy_inode_cache(); 1162783f6184SRyusuke Konishi 1163783f6184SRyusuke Konishi failed: 1164783f6184SRyusuke Konishi return err; 1165783f6184SRyusuke Konishi } 1166783f6184SRyusuke Konishi 1167783f6184SRyusuke Konishi static void __exit exit_nilfs_fs(void) 1168783f6184SRyusuke Konishi { 1169783f6184SRyusuke Konishi nilfs_destroy_segbuf_cache(); 1170783f6184SRyusuke Konishi nilfs_destroy_transaction_cache(); 1171783f6184SRyusuke Konishi nilfs_destroy_inode_cache(); 1172783f6184SRyusuke Konishi nilfs_btree_path_cache_destroy(); 1173783f6184SRyusuke Konishi unregister_filesystem(&nilfs_fs_type); 1174783f6184SRyusuke Konishi } 1175783f6184SRyusuke Konishi 1176783f6184SRyusuke Konishi module_init(init_nilfs_fs) 1177783f6184SRyusuke Konishi module_exit(exit_nilfs_fs) 1178