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> 53783f6184SRyusuke Konishi #include "nilfs.h" 54783f6184SRyusuke Konishi #include "mdt.h" 55783f6184SRyusuke Konishi #include "alloc.h" 56783f6184SRyusuke Konishi #include "page.h" 57783f6184SRyusuke Konishi #include "cpfile.h" 58783f6184SRyusuke Konishi #include "ifile.h" 59783f6184SRyusuke Konishi #include "dat.h" 60783f6184SRyusuke Konishi #include "segment.h" 61783f6184SRyusuke Konishi #include "segbuf.h" 62783f6184SRyusuke Konishi 63783f6184SRyusuke Konishi MODULE_AUTHOR("NTT Corp."); 64783f6184SRyusuke Konishi MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem " 65783f6184SRyusuke Konishi "(NILFS)"); 66783f6184SRyusuke Konishi MODULE_VERSION(NILFS_VERSION); 67783f6184SRyusuke Konishi MODULE_LICENSE("GPL"); 68783f6184SRyusuke Konishi 69783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data); 70783f6184SRyusuke Konishi static int test_exclusive_mount(struct file_system_type *fs_type, 71783f6184SRyusuke Konishi struct block_device *bdev, int flags); 72783f6184SRyusuke Konishi 73783f6184SRyusuke Konishi /** 74783f6184SRyusuke Konishi * nilfs_error() - report failure condition on a filesystem 75783f6184SRyusuke Konishi * 76783f6184SRyusuke Konishi * nilfs_error() sets an ERROR_FS flag on the superblock as well as 77783f6184SRyusuke Konishi * reporting an error message. It should be called when NILFS detects 78783f6184SRyusuke Konishi * incoherences or defects of meta data on disk. As for sustainable 79783f6184SRyusuke Konishi * errors such as a single-shot I/O error, nilfs_warning() or the printk() 80783f6184SRyusuke Konishi * function should be used instead. 81783f6184SRyusuke Konishi * 82783f6184SRyusuke Konishi * The segment constructor must not call this function because it can 83783f6184SRyusuke Konishi * kill itself. 84783f6184SRyusuke Konishi */ 85783f6184SRyusuke Konishi void nilfs_error(struct super_block *sb, const char *function, 86783f6184SRyusuke Konishi const char *fmt, ...) 87783f6184SRyusuke Konishi { 88783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 89783f6184SRyusuke Konishi va_list args; 90783f6184SRyusuke Konishi 91783f6184SRyusuke Konishi va_start(args, fmt); 92783f6184SRyusuke Konishi printk(KERN_CRIT "NILFS error (device %s): %s: ", sb->s_id, function); 93783f6184SRyusuke Konishi vprintk(fmt, args); 94783f6184SRyusuke Konishi printk("\n"); 95783f6184SRyusuke Konishi va_end(args); 96783f6184SRyusuke Konishi 97783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 98783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 99783f6184SRyusuke Konishi 100783f6184SRyusuke Konishi if (!nilfs_test_opt(sbi, ERRORS_CONT)) 101783f6184SRyusuke Konishi nilfs_detach_segment_constructor(sbi); 102783f6184SRyusuke Konishi 103783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 104783f6184SRyusuke Konishi if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) { 105783f6184SRyusuke Konishi nilfs->ns_mount_state |= NILFS_ERROR_FS; 106783f6184SRyusuke Konishi nilfs->ns_sbp->s_state |= cpu_to_le16(NILFS_ERROR_FS); 107783f6184SRyusuke Konishi nilfs_commit_super(sbi); 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 137783f6184SRyusuke Konishi struct inode *nilfs_alloc_inode(struct super_block *sb) 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; 147783f6184SRyusuke Konishi nilfs_btnode_cache_init(&ii->i_btnode_cache); 148783f6184SRyusuke Konishi return &ii->vfs_inode; 149783f6184SRyusuke Konishi } 150783f6184SRyusuke Konishi 151783f6184SRyusuke Konishi void nilfs_destroy_inode(struct inode *inode) 152783f6184SRyusuke Konishi { 153783f6184SRyusuke Konishi kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode)); 154783f6184SRyusuke Konishi } 155783f6184SRyusuke Konishi 156783f6184SRyusuke Konishi static void init_once(void *obj) 157783f6184SRyusuke Konishi { 158783f6184SRyusuke Konishi struct nilfs_inode_info *ii = obj; 159783f6184SRyusuke Konishi 160783f6184SRyusuke Konishi INIT_LIST_HEAD(&ii->i_dirty); 161783f6184SRyusuke Konishi #ifdef CONFIG_NILFS_XATTR 162783f6184SRyusuke Konishi init_rwsem(&ii->xattr_sem); 163783f6184SRyusuke Konishi #endif 164783f6184SRyusuke Konishi nilfs_btnode_cache_init_once(&ii->i_btnode_cache); 165783f6184SRyusuke Konishi ii->i_bmap = (struct nilfs_bmap *)&ii->i_bmap_union; 166783f6184SRyusuke Konishi inode_init_once(&ii->vfs_inode); 167783f6184SRyusuke Konishi } 168783f6184SRyusuke Konishi 169783f6184SRyusuke Konishi static int nilfs_init_inode_cache(void) 170783f6184SRyusuke Konishi { 171783f6184SRyusuke Konishi nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache", 172783f6184SRyusuke Konishi sizeof(struct nilfs_inode_info), 173783f6184SRyusuke Konishi 0, SLAB_RECLAIM_ACCOUNT, 174783f6184SRyusuke Konishi init_once); 175783f6184SRyusuke Konishi 176783f6184SRyusuke Konishi return (nilfs_inode_cachep == NULL) ? -ENOMEM : 0; 177783f6184SRyusuke Konishi } 178783f6184SRyusuke Konishi 179783f6184SRyusuke Konishi static inline void nilfs_destroy_inode_cache(void) 180783f6184SRyusuke Konishi { 181783f6184SRyusuke Konishi kmem_cache_destroy(nilfs_inode_cachep); 182783f6184SRyusuke Konishi } 183783f6184SRyusuke Konishi 184783f6184SRyusuke Konishi static void nilfs_clear_inode(struct inode *inode) 185783f6184SRyusuke Konishi { 186783f6184SRyusuke Konishi struct nilfs_inode_info *ii = NILFS_I(inode); 187783f6184SRyusuke Konishi 188783f6184SRyusuke Konishi #ifdef CONFIG_NILFS_POSIX_ACL 189783f6184SRyusuke Konishi if (ii->i_acl && ii->i_acl != NILFS_ACL_NOT_CACHED) { 190783f6184SRyusuke Konishi posix_acl_release(ii->i_acl); 191783f6184SRyusuke Konishi ii->i_acl = NILFS_ACL_NOT_CACHED; 192783f6184SRyusuke Konishi } 193783f6184SRyusuke Konishi if (ii->i_default_acl && ii->i_default_acl != NILFS_ACL_NOT_CACHED) { 194783f6184SRyusuke Konishi posix_acl_release(ii->i_default_acl); 195783f6184SRyusuke Konishi ii->i_default_acl = NILFS_ACL_NOT_CACHED; 196783f6184SRyusuke Konishi } 197783f6184SRyusuke Konishi #endif 198783f6184SRyusuke Konishi /* 199783f6184SRyusuke Konishi * Free resources allocated in nilfs_read_inode(), here. 200783f6184SRyusuke Konishi */ 201a2e7d2dfSRyusuke Konishi BUG_ON(!list_empty(&ii->i_dirty)); 202783f6184SRyusuke Konishi brelse(ii->i_bh); 203783f6184SRyusuke Konishi ii->i_bh = NULL; 204783f6184SRyusuke Konishi 205783f6184SRyusuke Konishi if (test_bit(NILFS_I_BMAP, &ii->i_state)) 206783f6184SRyusuke Konishi nilfs_bmap_clear(ii->i_bmap); 207783f6184SRyusuke Konishi 208783f6184SRyusuke Konishi nilfs_btnode_cache_clear(&ii->i_btnode_cache); 209783f6184SRyusuke Konishi } 210783f6184SRyusuke Konishi 211783f6184SRyusuke Konishi /** 212783f6184SRyusuke Konishi * nilfs_update_last_segment - change pointer to the latest segment 213783f6184SRyusuke Konishi * @sbi: nilfs_sb_info 214783f6184SRyusuke Konishi * @update_cno: flag whether to update checkpoint number. 215783f6184SRyusuke Konishi * 216783f6184SRyusuke Konishi * nilfs_update_last_segment() changes information in the super block 217783f6184SRyusuke Konishi * after a partial segment is written out successfully. The super 218783f6184SRyusuke Konishi * block is marked dirty. It will be written out at the next VFS sync 219783f6184SRyusuke Konishi * operations such as sync_supers() and generic_shutdown_super(). 220783f6184SRyusuke Konishi */ 221783f6184SRyusuke Konishi void nilfs_update_last_segment(struct nilfs_sb_info *sbi, int update_cno) 222783f6184SRyusuke Konishi { 223783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 224783f6184SRyusuke Konishi struct nilfs_super_block *sbp = nilfs->ns_sbp; 225783f6184SRyusuke Konishi 226783f6184SRyusuke Konishi /* nilfs->sem must be locked by the caller. */ 227783f6184SRyusuke Konishi spin_lock(&nilfs->ns_last_segment_lock); 228783f6184SRyusuke Konishi if (update_cno) 229783f6184SRyusuke Konishi nilfs->ns_last_cno = nilfs->ns_cno++; 230783f6184SRyusuke Konishi sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq); 231783f6184SRyusuke Konishi sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg); 232783f6184SRyusuke Konishi sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno); 233783f6184SRyusuke Konishi spin_unlock(&nilfs->ns_last_segment_lock); 234783f6184SRyusuke Konishi 235783f6184SRyusuke Konishi sbi->s_super->s_dirt = 1; /* must be set if delaying the call of 236783f6184SRyusuke Konishi nilfs_commit_super() */ 237783f6184SRyusuke Konishi } 238783f6184SRyusuke Konishi 239783f6184SRyusuke Konishi static int nilfs_sync_super(struct nilfs_sb_info *sbi) 240783f6184SRyusuke Konishi { 241783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 242783f6184SRyusuke Konishi int err; 243783f6184SRyusuke Konishi int barrier_done = 0; 244783f6184SRyusuke Konishi 245783f6184SRyusuke Konishi if (nilfs_test_opt(sbi, BARRIER)) { 246783f6184SRyusuke Konishi set_buffer_ordered(nilfs->ns_sbh); 247783f6184SRyusuke Konishi barrier_done = 1; 248783f6184SRyusuke Konishi } 249783f6184SRyusuke Konishi retry: 250783f6184SRyusuke Konishi set_buffer_dirty(nilfs->ns_sbh); 251783f6184SRyusuke Konishi err = sync_dirty_buffer(nilfs->ns_sbh); 252783f6184SRyusuke Konishi if (err == -EOPNOTSUPP && barrier_done) { 253783f6184SRyusuke Konishi nilfs_warning(sbi->s_super, __func__, 254783f6184SRyusuke Konishi "barrier-based sync failed. " 255783f6184SRyusuke Konishi "disabling barriers\n"); 256783f6184SRyusuke Konishi nilfs_clear_opt(sbi, BARRIER); 257783f6184SRyusuke Konishi barrier_done = 0; 258783f6184SRyusuke Konishi clear_buffer_ordered(nilfs->ns_sbh); 259783f6184SRyusuke Konishi goto retry; 260783f6184SRyusuke Konishi } 261783f6184SRyusuke Konishi if (unlikely(err)) 262783f6184SRyusuke Konishi printk(KERN_ERR 263783f6184SRyusuke Konishi "NILFS: unable to write superblock (err=%d)\n", err); 264783f6184SRyusuke Konishi else { 265783f6184SRyusuke Konishi clear_nilfs_discontinued(nilfs); 2662c2e52fcSRyusuke Konishi spin_lock(&nilfs->ns_last_segment_lock); 2672c2e52fcSRyusuke Konishi nilfs->ns_prot_seq = le64_to_cpu(nilfs->ns_sbp->s_last_seq); 2682c2e52fcSRyusuke Konishi spin_unlock(&nilfs->ns_last_segment_lock); 269783f6184SRyusuke Konishi } 270783f6184SRyusuke Konishi 271783f6184SRyusuke Konishi return err; 272783f6184SRyusuke Konishi } 273783f6184SRyusuke Konishi 274783f6184SRyusuke Konishi int nilfs_commit_super(struct nilfs_sb_info *sbi) 275783f6184SRyusuke Konishi { 276783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 277783f6184SRyusuke Konishi struct nilfs_super_block *sbp = nilfs->ns_sbp; 278783f6184SRyusuke Konishi sector_t nfreeblocks; 279783f6184SRyusuke Konishi int err; 280783f6184SRyusuke Konishi 281783f6184SRyusuke Konishi /* nilfs->sem must be locked by the caller. */ 282783f6184SRyusuke Konishi err = nilfs_count_free_blocks(nilfs, &nfreeblocks); 283783f6184SRyusuke Konishi if (unlikely(err)) { 284783f6184SRyusuke Konishi printk(KERN_ERR "NILFS: failed to count free blocks\n"); 285783f6184SRyusuke Konishi return err; 286783f6184SRyusuke Konishi } 287783f6184SRyusuke Konishi sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks); 288783f6184SRyusuke Konishi sbp->s_wtime = cpu_to_le64(get_seconds()); 289783f6184SRyusuke Konishi sbp->s_sum = 0; 290783f6184SRyusuke Konishi sbp->s_sum = crc32_le(nilfs->ns_crc_seed, (unsigned char *)sbp, 291783f6184SRyusuke Konishi le16_to_cpu(sbp->s_bytes)); 292783f6184SRyusuke Konishi 293783f6184SRyusuke Konishi sbi->s_super->s_dirt = 0; 294783f6184SRyusuke Konishi return nilfs_sync_super(sbi); 295783f6184SRyusuke Konishi } 296783f6184SRyusuke Konishi 297783f6184SRyusuke Konishi static void nilfs_put_super(struct super_block *sb) 298783f6184SRyusuke Konishi { 299783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 300783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 301783f6184SRyusuke Konishi 302783f6184SRyusuke Konishi nilfs_detach_segment_constructor(sbi); 303783f6184SRyusuke Konishi 304783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 305783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 306783f6184SRyusuke Konishi nilfs->ns_sbp->s_state = cpu_to_le16(nilfs->ns_mount_state); 307783f6184SRyusuke Konishi nilfs_commit_super(sbi); 308783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 309783f6184SRyusuke Konishi } 310783f6184SRyusuke Konishi 311783f6184SRyusuke Konishi nilfs_detach_checkpoint(sbi); 312783f6184SRyusuke Konishi put_nilfs(sbi->s_nilfs); 313783f6184SRyusuke Konishi sbi->s_super = NULL; 314783f6184SRyusuke Konishi sb->s_fs_info = NULL; 315783f6184SRyusuke Konishi kfree(sbi); 316783f6184SRyusuke Konishi } 317783f6184SRyusuke Konishi 318783f6184SRyusuke Konishi /** 319783f6184SRyusuke Konishi * nilfs_write_super - write super block(s) of NILFS 320783f6184SRyusuke Konishi * @sb: super_block 321783f6184SRyusuke Konishi * 322783f6184SRyusuke Konishi * nilfs_write_super() gets a fs-dependent lock, writes super block(s), and 323783f6184SRyusuke Konishi * clears s_dirt. This function is called in the section protected by 324783f6184SRyusuke Konishi * lock_super(). 325783f6184SRyusuke Konishi * 326783f6184SRyusuke Konishi * The s_dirt flag is managed by each filesystem and we protect it by ns_sem 327783f6184SRyusuke Konishi * of the struct the_nilfs. Lock order must be as follows: 328783f6184SRyusuke Konishi * 329783f6184SRyusuke Konishi * 1. lock_super() 330783f6184SRyusuke Konishi * 2. down_write(&nilfs->ns_sem) 331783f6184SRyusuke Konishi * 332783f6184SRyusuke Konishi * Inside NILFS, locking ns_sem is enough to protect s_dirt and the buffer 333783f6184SRyusuke Konishi * of the super block (nilfs->ns_sbp). 334783f6184SRyusuke Konishi * 335783f6184SRyusuke Konishi * In most cases, VFS functions call lock_super() before calling these 336783f6184SRyusuke Konishi * methods. So we must be careful not to bring on deadlocks when using 337783f6184SRyusuke Konishi * lock_super(); see generic_shutdown_super(), write_super(), and so on. 338783f6184SRyusuke Konishi * 339783f6184SRyusuke Konishi * Note that order of lock_kernel() and lock_super() depends on contexts 340783f6184SRyusuke Konishi * of VFS. We should also note that lock_kernel() can be used in its 341783f6184SRyusuke Konishi * protective section and only the outermost one has an effect. 342783f6184SRyusuke Konishi */ 343783f6184SRyusuke Konishi static void nilfs_write_super(struct super_block *sb) 344783f6184SRyusuke Konishi { 345783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 346783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 347783f6184SRyusuke Konishi 348783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 349783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) 350783f6184SRyusuke Konishi nilfs_commit_super(sbi); 351783f6184SRyusuke Konishi sb->s_dirt = 0; 352783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 353783f6184SRyusuke Konishi } 354783f6184SRyusuke Konishi 355783f6184SRyusuke Konishi static int nilfs_sync_fs(struct super_block *sb, int wait) 356783f6184SRyusuke Konishi { 357783f6184SRyusuke Konishi int err = 0; 358783f6184SRyusuke Konishi 359783f6184SRyusuke Konishi /* This function is called when super block should be written back */ 360783f6184SRyusuke Konishi if (wait) 361783f6184SRyusuke Konishi err = nilfs_construct_segment(sb); 362783f6184SRyusuke Konishi return err; 363783f6184SRyusuke Konishi } 364783f6184SRyusuke Konishi 365783f6184SRyusuke Konishi int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno) 366783f6184SRyusuke Konishi { 367783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 368783f6184SRyusuke Konishi struct nilfs_checkpoint *raw_cp; 369783f6184SRyusuke Konishi struct buffer_head *bh_cp; 370783f6184SRyusuke Konishi int err; 371783f6184SRyusuke Konishi 372783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 373783f6184SRyusuke Konishi list_add(&sbi->s_list, &nilfs->ns_supers); 374783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 375783f6184SRyusuke Konishi 376783f6184SRyusuke Konishi sbi->s_ifile = nilfs_mdt_new( 377783f6184SRyusuke Konishi nilfs, sbi->s_super, NILFS_IFILE_INO, NILFS_IFILE_GFP); 378783f6184SRyusuke Konishi if (!sbi->s_ifile) 379783f6184SRyusuke Konishi return -ENOMEM; 380783f6184SRyusuke Konishi 381783f6184SRyusuke Konishi err = nilfs_palloc_init_blockgroup(sbi->s_ifile, nilfs->ns_inode_size); 382783f6184SRyusuke Konishi if (unlikely(err)) 383783f6184SRyusuke Konishi goto failed; 384783f6184SRyusuke Konishi 385783f6184SRyusuke Konishi err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp, 386783f6184SRyusuke Konishi &bh_cp); 387783f6184SRyusuke Konishi if (unlikely(err)) { 388783f6184SRyusuke Konishi if (err == -ENOENT || err == -EINVAL) { 389783f6184SRyusuke Konishi printk(KERN_ERR 390783f6184SRyusuke Konishi "NILFS: Invalid checkpoint " 391783f6184SRyusuke Konishi "(checkpoint number=%llu)\n", 392783f6184SRyusuke Konishi (unsigned long long)cno); 393783f6184SRyusuke Konishi err = -EINVAL; 394783f6184SRyusuke Konishi } 395783f6184SRyusuke Konishi goto failed; 396783f6184SRyusuke Konishi } 397783f6184SRyusuke Konishi err = nilfs_read_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode); 398783f6184SRyusuke Konishi if (unlikely(err)) 399783f6184SRyusuke Konishi goto failed_bh; 400783f6184SRyusuke Konishi atomic_set(&sbi->s_inodes_count, le64_to_cpu(raw_cp->cp_inodes_count)); 401783f6184SRyusuke Konishi atomic_set(&sbi->s_blocks_count, le64_to_cpu(raw_cp->cp_blocks_count)); 402783f6184SRyusuke Konishi 403783f6184SRyusuke Konishi nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp); 404783f6184SRyusuke Konishi return 0; 405783f6184SRyusuke Konishi 406783f6184SRyusuke Konishi failed_bh: 407783f6184SRyusuke Konishi nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp); 408783f6184SRyusuke Konishi failed: 409783f6184SRyusuke Konishi nilfs_mdt_destroy(sbi->s_ifile); 410783f6184SRyusuke Konishi sbi->s_ifile = NULL; 411783f6184SRyusuke Konishi 412783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 413783f6184SRyusuke Konishi list_del_init(&sbi->s_list); 414783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 415783f6184SRyusuke Konishi 416783f6184SRyusuke Konishi return err; 417783f6184SRyusuke Konishi } 418783f6184SRyusuke Konishi 419783f6184SRyusuke Konishi void nilfs_detach_checkpoint(struct nilfs_sb_info *sbi) 420783f6184SRyusuke Konishi { 421783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 422783f6184SRyusuke Konishi 423783f6184SRyusuke Konishi nilfs_mdt_clear(sbi->s_ifile); 424783f6184SRyusuke Konishi nilfs_mdt_destroy(sbi->s_ifile); 425783f6184SRyusuke Konishi sbi->s_ifile = NULL; 426783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 427783f6184SRyusuke Konishi list_del_init(&sbi->s_list); 428783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 429783f6184SRyusuke Konishi } 430783f6184SRyusuke Konishi 431783f6184SRyusuke Konishi static int nilfs_mark_recovery_complete(struct nilfs_sb_info *sbi) 432783f6184SRyusuke Konishi { 433783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 434783f6184SRyusuke Konishi int err = 0; 435783f6184SRyusuke Konishi 436783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 437783f6184SRyusuke Konishi if (!(nilfs->ns_mount_state & NILFS_VALID_FS)) { 438783f6184SRyusuke Konishi nilfs->ns_mount_state |= NILFS_VALID_FS; 439783f6184SRyusuke Konishi err = nilfs_commit_super(sbi); 440783f6184SRyusuke Konishi if (likely(!err)) 441783f6184SRyusuke Konishi printk(KERN_INFO "NILFS: recovery complete.\n"); 442783f6184SRyusuke Konishi } 443783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 444783f6184SRyusuke Konishi return err; 445783f6184SRyusuke Konishi } 446783f6184SRyusuke Konishi 447783f6184SRyusuke Konishi static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf) 448783f6184SRyusuke Konishi { 449783f6184SRyusuke Konishi struct super_block *sb = dentry->d_sb; 450783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 451783f6184SRyusuke Konishi unsigned long long blocks; 452783f6184SRyusuke Konishi unsigned long overhead; 453783f6184SRyusuke Konishi unsigned long nrsvblocks; 454783f6184SRyusuke Konishi sector_t nfreeblocks; 455783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 456783f6184SRyusuke Konishi int err; 457783f6184SRyusuke Konishi 458783f6184SRyusuke Konishi /* 459783f6184SRyusuke Konishi * Compute all of the segment blocks 460783f6184SRyusuke Konishi * 461783f6184SRyusuke Konishi * The blocks before first segment and after last segment 462783f6184SRyusuke Konishi * are excluded. 463783f6184SRyusuke Konishi */ 464783f6184SRyusuke Konishi blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments 465783f6184SRyusuke Konishi - nilfs->ns_first_data_block; 466783f6184SRyusuke Konishi nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment; 467783f6184SRyusuke Konishi 468783f6184SRyusuke Konishi /* 469783f6184SRyusuke Konishi * Compute the overhead 470783f6184SRyusuke Konishi * 471783f6184SRyusuke Konishi * When distributing meta data blocks outside semgent structure, 472783f6184SRyusuke Konishi * We must count them as the overhead. 473783f6184SRyusuke Konishi */ 474783f6184SRyusuke Konishi overhead = 0; 475783f6184SRyusuke Konishi 476783f6184SRyusuke Konishi err = nilfs_count_free_blocks(nilfs, &nfreeblocks); 477783f6184SRyusuke Konishi if (unlikely(err)) 478783f6184SRyusuke Konishi return err; 479783f6184SRyusuke Konishi 480783f6184SRyusuke Konishi buf->f_type = NILFS_SUPER_MAGIC; 481783f6184SRyusuke Konishi buf->f_bsize = sb->s_blocksize; 482783f6184SRyusuke Konishi buf->f_blocks = blocks - overhead; 483783f6184SRyusuke Konishi buf->f_bfree = nfreeblocks; 484783f6184SRyusuke Konishi buf->f_bavail = (buf->f_bfree >= nrsvblocks) ? 485783f6184SRyusuke Konishi (buf->f_bfree - nrsvblocks) : 0; 486783f6184SRyusuke Konishi buf->f_files = atomic_read(&sbi->s_inodes_count); 487783f6184SRyusuke Konishi buf->f_ffree = 0; /* nilfs_count_free_inodes(sb); */ 488783f6184SRyusuke Konishi buf->f_namelen = NILFS_NAME_LEN; 489783f6184SRyusuke Konishi return 0; 490783f6184SRyusuke Konishi } 491783f6184SRyusuke Konishi 492783f6184SRyusuke Konishi static struct super_operations nilfs_sops = { 493783f6184SRyusuke Konishi .alloc_inode = nilfs_alloc_inode, 494783f6184SRyusuke Konishi .destroy_inode = nilfs_destroy_inode, 495783f6184SRyusuke Konishi .dirty_inode = nilfs_dirty_inode, 496783f6184SRyusuke Konishi /* .write_inode = nilfs_write_inode, */ 497783f6184SRyusuke Konishi /* .put_inode = nilfs_put_inode, */ 498783f6184SRyusuke Konishi /* .drop_inode = nilfs_drop_inode, */ 499783f6184SRyusuke Konishi .delete_inode = nilfs_delete_inode, 500783f6184SRyusuke Konishi .put_super = nilfs_put_super, 501783f6184SRyusuke Konishi .write_super = nilfs_write_super, 502783f6184SRyusuke Konishi .sync_fs = nilfs_sync_fs, 503783f6184SRyusuke Konishi /* .write_super_lockfs */ 504783f6184SRyusuke Konishi /* .unlockfs */ 505783f6184SRyusuke Konishi .statfs = nilfs_statfs, 506783f6184SRyusuke Konishi .remount_fs = nilfs_remount, 507783f6184SRyusuke Konishi .clear_inode = nilfs_clear_inode, 508783f6184SRyusuke Konishi /* .umount_begin */ 509783f6184SRyusuke Konishi /* .show_options */ 510783f6184SRyusuke Konishi }; 511783f6184SRyusuke Konishi 512783f6184SRyusuke Konishi static struct inode * 513783f6184SRyusuke Konishi nilfs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation) 514783f6184SRyusuke Konishi { 515783f6184SRyusuke Konishi struct inode *inode; 516783f6184SRyusuke Konishi 517783f6184SRyusuke Konishi if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO && 518783f6184SRyusuke Konishi ino != NILFS_SKETCH_INO) 519783f6184SRyusuke Konishi return ERR_PTR(-ESTALE); 520783f6184SRyusuke Konishi 521783f6184SRyusuke Konishi inode = nilfs_iget(sb, ino); 522783f6184SRyusuke Konishi if (IS_ERR(inode)) 523783f6184SRyusuke Konishi return ERR_CAST(inode); 524783f6184SRyusuke Konishi if (generation && inode->i_generation != generation) { 525783f6184SRyusuke Konishi iput(inode); 526783f6184SRyusuke Konishi return ERR_PTR(-ESTALE); 527783f6184SRyusuke Konishi } 528783f6184SRyusuke Konishi 529783f6184SRyusuke Konishi return inode; 530783f6184SRyusuke Konishi } 531783f6184SRyusuke Konishi 532783f6184SRyusuke Konishi static struct dentry * 533783f6184SRyusuke Konishi nilfs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, 534783f6184SRyusuke Konishi int fh_type) 535783f6184SRyusuke Konishi { 536783f6184SRyusuke Konishi return generic_fh_to_dentry(sb, fid, fh_len, fh_type, 537783f6184SRyusuke Konishi nilfs_nfs_get_inode); 538783f6184SRyusuke Konishi } 539783f6184SRyusuke Konishi 540783f6184SRyusuke Konishi static struct dentry * 541783f6184SRyusuke Konishi nilfs_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, 542783f6184SRyusuke Konishi int fh_type) 543783f6184SRyusuke Konishi { 544783f6184SRyusuke Konishi return generic_fh_to_parent(sb, fid, fh_len, fh_type, 545783f6184SRyusuke Konishi nilfs_nfs_get_inode); 546783f6184SRyusuke Konishi } 547783f6184SRyusuke Konishi 548783f6184SRyusuke Konishi static struct export_operations nilfs_export_ops = { 549783f6184SRyusuke Konishi .fh_to_dentry = nilfs_fh_to_dentry, 550783f6184SRyusuke Konishi .fh_to_parent = nilfs_fh_to_parent, 551783f6184SRyusuke Konishi .get_parent = nilfs_get_parent, 552783f6184SRyusuke Konishi }; 553783f6184SRyusuke Konishi 554783f6184SRyusuke Konishi enum { 555783f6184SRyusuke Konishi Opt_err_cont, Opt_err_panic, Opt_err_ro, 556783f6184SRyusuke Konishi Opt_barrier, Opt_snapshot, Opt_order, 557783f6184SRyusuke Konishi Opt_err, 558783f6184SRyusuke Konishi }; 559783f6184SRyusuke Konishi 560783f6184SRyusuke Konishi static match_table_t tokens = { 561783f6184SRyusuke Konishi {Opt_err_cont, "errors=continue"}, 562783f6184SRyusuke Konishi {Opt_err_panic, "errors=panic"}, 563783f6184SRyusuke Konishi {Opt_err_ro, "errors=remount-ro"}, 564783f6184SRyusuke Konishi {Opt_barrier, "barrier=%s"}, 565783f6184SRyusuke Konishi {Opt_snapshot, "cp=%u"}, 566783f6184SRyusuke Konishi {Opt_order, "order=%s"}, 567783f6184SRyusuke Konishi {Opt_err, NULL} 568783f6184SRyusuke Konishi }; 569783f6184SRyusuke Konishi 570783f6184SRyusuke Konishi static int match_bool(substring_t *s, int *result) 571783f6184SRyusuke Konishi { 572783f6184SRyusuke Konishi int len = s->to - s->from; 573783f6184SRyusuke Konishi 574783f6184SRyusuke Konishi if (strncmp(s->from, "on", len) == 0) 575783f6184SRyusuke Konishi *result = 1; 576783f6184SRyusuke Konishi else if (strncmp(s->from, "off", len) == 0) 577783f6184SRyusuke Konishi *result = 0; 578783f6184SRyusuke Konishi else 579783f6184SRyusuke Konishi return 1; 580783f6184SRyusuke Konishi return 0; 581783f6184SRyusuke Konishi } 582783f6184SRyusuke Konishi 583783f6184SRyusuke Konishi static int parse_options(char *options, struct super_block *sb) 584783f6184SRyusuke Konishi { 585783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 586783f6184SRyusuke Konishi char *p; 587783f6184SRyusuke Konishi substring_t args[MAX_OPT_ARGS]; 588783f6184SRyusuke Konishi int option; 589783f6184SRyusuke Konishi 590783f6184SRyusuke Konishi if (!options) 591783f6184SRyusuke Konishi return 1; 592783f6184SRyusuke Konishi 593783f6184SRyusuke Konishi while ((p = strsep(&options, ",")) != NULL) { 594783f6184SRyusuke Konishi int token; 595783f6184SRyusuke Konishi if (!*p) 596783f6184SRyusuke Konishi continue; 597783f6184SRyusuke Konishi 598783f6184SRyusuke Konishi token = match_token(p, tokens, args); 599783f6184SRyusuke Konishi switch (token) { 600783f6184SRyusuke Konishi case Opt_barrier: 601783f6184SRyusuke Konishi if (match_bool(&args[0], &option)) 602783f6184SRyusuke Konishi return 0; 603783f6184SRyusuke Konishi if (option) 604783f6184SRyusuke Konishi nilfs_set_opt(sbi, BARRIER); 605783f6184SRyusuke Konishi else 606783f6184SRyusuke Konishi nilfs_clear_opt(sbi, BARRIER); 607783f6184SRyusuke Konishi break; 608783f6184SRyusuke Konishi case Opt_order: 609783f6184SRyusuke Konishi if (strcmp(args[0].from, "relaxed") == 0) 610783f6184SRyusuke Konishi /* Ordered data semantics */ 611783f6184SRyusuke Konishi nilfs_clear_opt(sbi, STRICT_ORDER); 612783f6184SRyusuke Konishi else if (strcmp(args[0].from, "strict") == 0) 613783f6184SRyusuke Konishi /* Strict in-order semantics */ 614783f6184SRyusuke Konishi nilfs_set_opt(sbi, STRICT_ORDER); 615783f6184SRyusuke Konishi else 616783f6184SRyusuke Konishi return 0; 617783f6184SRyusuke Konishi break; 618783f6184SRyusuke Konishi case Opt_err_panic: 619783f6184SRyusuke Konishi nilfs_write_opt(sbi, ERROR_MODE, ERRORS_PANIC); 620783f6184SRyusuke Konishi break; 621783f6184SRyusuke Konishi case Opt_err_ro: 622783f6184SRyusuke Konishi nilfs_write_opt(sbi, ERROR_MODE, ERRORS_RO); 623783f6184SRyusuke Konishi break; 624783f6184SRyusuke Konishi case Opt_err_cont: 625783f6184SRyusuke Konishi nilfs_write_opt(sbi, ERROR_MODE, ERRORS_CONT); 626783f6184SRyusuke Konishi break; 627783f6184SRyusuke Konishi case Opt_snapshot: 628783f6184SRyusuke Konishi if (match_int(&args[0], &option) || option <= 0) 629783f6184SRyusuke Konishi return 0; 630783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) 631783f6184SRyusuke Konishi return 0; 632783f6184SRyusuke Konishi sbi->s_snapshot_cno = option; 633783f6184SRyusuke Konishi nilfs_set_opt(sbi, SNAPSHOT); 634783f6184SRyusuke Konishi break; 635783f6184SRyusuke Konishi default: 636783f6184SRyusuke Konishi printk(KERN_ERR 637783f6184SRyusuke Konishi "NILFS: Unrecognized mount option \"%s\"\n", p); 638783f6184SRyusuke Konishi return 0; 639783f6184SRyusuke Konishi } 640783f6184SRyusuke Konishi } 641783f6184SRyusuke Konishi return 1; 642783f6184SRyusuke Konishi } 643783f6184SRyusuke Konishi 644783f6184SRyusuke Konishi static inline void 645783f6184SRyusuke Konishi nilfs_set_default_options(struct nilfs_sb_info *sbi, 646783f6184SRyusuke Konishi struct nilfs_super_block *sbp) 647783f6184SRyusuke Konishi { 648783f6184SRyusuke Konishi sbi->s_mount_opt = 649783f6184SRyusuke Konishi NILFS_MOUNT_ERRORS_CONT | NILFS_MOUNT_BARRIER; 650783f6184SRyusuke Konishi } 651783f6184SRyusuke Konishi 652783f6184SRyusuke Konishi static int nilfs_setup_super(struct nilfs_sb_info *sbi) 653783f6184SRyusuke Konishi { 654783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 655783f6184SRyusuke Konishi struct nilfs_super_block *sbp = nilfs->ns_sbp; 656783f6184SRyusuke Konishi int max_mnt_count = le16_to_cpu(sbp->s_max_mnt_count); 657783f6184SRyusuke Konishi int mnt_count = le16_to_cpu(sbp->s_mnt_count); 658783f6184SRyusuke Konishi 659783f6184SRyusuke Konishi /* nilfs->sem must be locked by the caller. */ 660783f6184SRyusuke Konishi if (!(nilfs->ns_mount_state & NILFS_VALID_FS)) { 661783f6184SRyusuke Konishi printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n"); 662783f6184SRyusuke Konishi } else if (nilfs->ns_mount_state & NILFS_ERROR_FS) { 663783f6184SRyusuke Konishi printk(KERN_WARNING 664783f6184SRyusuke Konishi "NILFS warning: mounting fs with errors\n"); 665783f6184SRyusuke Konishi #if 0 666783f6184SRyusuke Konishi } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) { 667783f6184SRyusuke Konishi printk(KERN_WARNING 668783f6184SRyusuke Konishi "NILFS warning: maximal mount count reached\n"); 669783f6184SRyusuke Konishi #endif 670783f6184SRyusuke Konishi } 671783f6184SRyusuke Konishi if (!max_mnt_count) 672783f6184SRyusuke Konishi sbp->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT); 673783f6184SRyusuke Konishi 674783f6184SRyusuke Konishi sbp->s_mnt_count = cpu_to_le16(mnt_count + 1); 675783f6184SRyusuke Konishi sbp->s_state = cpu_to_le16(le16_to_cpu(sbp->s_state) & ~NILFS_VALID_FS); 676783f6184SRyusuke Konishi sbp->s_mtime = cpu_to_le64(get_seconds()); 677783f6184SRyusuke Konishi return nilfs_commit_super(sbi); 678783f6184SRyusuke Konishi } 679783f6184SRyusuke Konishi 680783f6184SRyusuke Konishi struct nilfs_super_block * 681783f6184SRyusuke Konishi nilfs_load_super_block(struct super_block *sb, struct buffer_head **pbh) 682783f6184SRyusuke Konishi { 683783f6184SRyusuke Konishi int blocksize; 684783f6184SRyusuke Konishi unsigned long offset, sb_index; 685783f6184SRyusuke Konishi 686783f6184SRyusuke Konishi /* 687783f6184SRyusuke Konishi * Adjusting block size 688783f6184SRyusuke Konishi * Blocksize will be enlarged when it is smaller than hardware 689783f6184SRyusuke Konishi * sector size. 690783f6184SRyusuke Konishi * Disk format of superblock does not change. 691783f6184SRyusuke Konishi */ 692783f6184SRyusuke Konishi blocksize = sb_min_blocksize(sb, BLOCK_SIZE); 693783f6184SRyusuke Konishi if (!blocksize) { 694783f6184SRyusuke Konishi printk(KERN_ERR 695783f6184SRyusuke Konishi "NILFS: unable to set blocksize of superblock\n"); 696783f6184SRyusuke Konishi return NULL; 697783f6184SRyusuke Konishi } 698783f6184SRyusuke Konishi sb_index = NILFS_SB_OFFSET_BYTES / blocksize; 699783f6184SRyusuke Konishi offset = NILFS_SB_OFFSET_BYTES % blocksize; 700783f6184SRyusuke Konishi 701783f6184SRyusuke Konishi *pbh = sb_bread(sb, sb_index); 702783f6184SRyusuke Konishi if (!*pbh) { 703783f6184SRyusuke Konishi printk(KERN_ERR "NILFS: unable to read superblock\n"); 704783f6184SRyusuke Konishi return NULL; 705783f6184SRyusuke Konishi } 706783f6184SRyusuke Konishi return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset); 707783f6184SRyusuke Konishi } 708783f6184SRyusuke Konishi 709783f6184SRyusuke Konishi struct nilfs_super_block * 710783f6184SRyusuke Konishi nilfs_reload_super_block(struct super_block *sb, struct buffer_head **pbh, 711783f6184SRyusuke Konishi int blocksize) 712783f6184SRyusuke Konishi { 713783f6184SRyusuke Konishi struct nilfs_super_block *sbp; 714783f6184SRyusuke Konishi unsigned long offset, sb_index; 715783f6184SRyusuke Konishi int hw_blocksize = bdev_hardsect_size(sb->s_bdev); 716783f6184SRyusuke Konishi 717783f6184SRyusuke Konishi if (blocksize < hw_blocksize) { 718783f6184SRyusuke Konishi printk(KERN_ERR 719783f6184SRyusuke Konishi "NILFS: blocksize %d too small for device " 720783f6184SRyusuke Konishi "(sector-size = %d).\n", 721783f6184SRyusuke Konishi blocksize, hw_blocksize); 722783f6184SRyusuke Konishi goto failed_sbh; 723783f6184SRyusuke Konishi } 724783f6184SRyusuke Konishi brelse(*pbh); 725783f6184SRyusuke Konishi sb_set_blocksize(sb, blocksize); 726783f6184SRyusuke Konishi 727783f6184SRyusuke Konishi sb_index = NILFS_SB_OFFSET_BYTES / blocksize; 728783f6184SRyusuke Konishi offset = NILFS_SB_OFFSET_BYTES % blocksize; 729783f6184SRyusuke Konishi 730783f6184SRyusuke Konishi *pbh = sb_bread(sb, sb_index); 731783f6184SRyusuke Konishi if (!*pbh) { 732783f6184SRyusuke Konishi printk(KERN_ERR 733783f6184SRyusuke Konishi "NILFS: cannot read superblock on 2nd try.\n"); 734783f6184SRyusuke Konishi goto failed; 735783f6184SRyusuke Konishi } 736783f6184SRyusuke Konishi 737783f6184SRyusuke Konishi sbp = (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset); 738783f6184SRyusuke Konishi if (sbp->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) { 739783f6184SRyusuke Konishi printk(KERN_ERR 740783f6184SRyusuke Konishi "NILFS: !? Magic mismatch on 2nd try.\n"); 741783f6184SRyusuke Konishi goto failed_sbh; 742783f6184SRyusuke Konishi } 743783f6184SRyusuke Konishi return sbp; 744783f6184SRyusuke Konishi 745783f6184SRyusuke Konishi failed_sbh: 746783f6184SRyusuke Konishi brelse(*pbh); 747783f6184SRyusuke Konishi 748783f6184SRyusuke Konishi failed: 749783f6184SRyusuke Konishi return NULL; 750783f6184SRyusuke Konishi } 751783f6184SRyusuke Konishi 752783f6184SRyusuke Konishi int nilfs_store_magic_and_option(struct super_block *sb, 753783f6184SRyusuke Konishi struct nilfs_super_block *sbp, 754783f6184SRyusuke Konishi char *data) 755783f6184SRyusuke Konishi { 756783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 757783f6184SRyusuke Konishi 758783f6184SRyusuke Konishi /* trying to fill super (1st stage) */ 759783f6184SRyusuke Konishi sb->s_magic = le16_to_cpu(sbp->s_magic); 760783f6184SRyusuke Konishi 761783f6184SRyusuke Konishi /* FS independent flags */ 762783f6184SRyusuke Konishi #ifdef NILFS_ATIME_DISABLE 763783f6184SRyusuke Konishi sb->s_flags |= MS_NOATIME; 764783f6184SRyusuke Konishi #endif 765783f6184SRyusuke Konishi 766783f6184SRyusuke Konishi if (sb->s_magic != NILFS_SUPER_MAGIC) { 767783f6184SRyusuke Konishi printk("NILFS: Can't find nilfs on dev %s.\n", sb->s_id); 768783f6184SRyusuke Konishi return -EINVAL; 769783f6184SRyusuke Konishi } 770783f6184SRyusuke Konishi 771783f6184SRyusuke Konishi nilfs_set_default_options(sbi, sbp); 772783f6184SRyusuke Konishi 773783f6184SRyusuke Konishi sbi->s_resuid = le16_to_cpu(sbp->s_def_resuid); 774783f6184SRyusuke Konishi sbi->s_resgid = le16_to_cpu(sbp->s_def_resgid); 775783f6184SRyusuke Konishi sbi->s_interval = le32_to_cpu(sbp->s_c_interval); 776783f6184SRyusuke Konishi sbi->s_watermark = le32_to_cpu(sbp->s_c_block_max); 777783f6184SRyusuke Konishi 778783f6184SRyusuke Konishi if (!parse_options(data, sb)) 779783f6184SRyusuke Konishi return -EINVAL; 780783f6184SRyusuke Konishi 781783f6184SRyusuke Konishi return 0; 782783f6184SRyusuke Konishi } 783783f6184SRyusuke Konishi 784783f6184SRyusuke Konishi /** 785783f6184SRyusuke Konishi * nilfs_fill_super() - initialize a super block instance 786783f6184SRyusuke Konishi * @sb: super_block 787783f6184SRyusuke Konishi * @data: mount options 788783f6184SRyusuke Konishi * @silent: silent mode flag 789783f6184SRyusuke Konishi * @nilfs: the_nilfs struct 790783f6184SRyusuke Konishi * 791783f6184SRyusuke Konishi * This function is called exclusively by bd_mount_mutex. 792783f6184SRyusuke Konishi * So, the recovery process is protected from other simultaneous mounts. 793783f6184SRyusuke Konishi */ 794783f6184SRyusuke Konishi static int 795783f6184SRyusuke Konishi nilfs_fill_super(struct super_block *sb, void *data, int silent, 796783f6184SRyusuke Konishi struct the_nilfs *nilfs) 797783f6184SRyusuke Konishi { 798783f6184SRyusuke Konishi struct nilfs_sb_info *sbi; 799783f6184SRyusuke Konishi struct inode *root; 800783f6184SRyusuke Konishi __u64 cno; 801783f6184SRyusuke Konishi int err; 802783f6184SRyusuke Konishi 803783f6184SRyusuke Konishi sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); 804783f6184SRyusuke Konishi if (!sbi) 805783f6184SRyusuke Konishi return -ENOMEM; 806783f6184SRyusuke Konishi 807783f6184SRyusuke Konishi sb->s_fs_info = sbi; 808783f6184SRyusuke Konishi 809783f6184SRyusuke Konishi get_nilfs(nilfs); 810783f6184SRyusuke Konishi sbi->s_nilfs = nilfs; 811783f6184SRyusuke Konishi sbi->s_super = sb; 812783f6184SRyusuke Konishi 813783f6184SRyusuke Konishi err = init_nilfs(nilfs, sbi, (char *)data); 814783f6184SRyusuke Konishi if (err) 815783f6184SRyusuke Konishi goto failed_sbi; 816783f6184SRyusuke Konishi 817783f6184SRyusuke Konishi spin_lock_init(&sbi->s_inode_lock); 818783f6184SRyusuke Konishi INIT_LIST_HEAD(&sbi->s_dirty_files); 819783f6184SRyusuke Konishi INIT_LIST_HEAD(&sbi->s_list); 820783f6184SRyusuke Konishi 821783f6184SRyusuke Konishi /* 822783f6184SRyusuke Konishi * Following initialization is overlapped because 823783f6184SRyusuke Konishi * nilfs_sb_info structure has been cleared at the beginning. 824783f6184SRyusuke Konishi * But we reserve them to keep our interest and make ready 825783f6184SRyusuke Konishi * for the future change. 826783f6184SRyusuke Konishi */ 827783f6184SRyusuke Konishi get_random_bytes(&sbi->s_next_generation, 828783f6184SRyusuke Konishi sizeof(sbi->s_next_generation)); 829783f6184SRyusuke Konishi spin_lock_init(&sbi->s_next_gen_lock); 830783f6184SRyusuke Konishi 831783f6184SRyusuke Konishi sb->s_op = &nilfs_sops; 832783f6184SRyusuke Konishi sb->s_export_op = &nilfs_export_ops; 833783f6184SRyusuke Konishi sb->s_root = NULL; 834783f6184SRyusuke Konishi 835783f6184SRyusuke Konishi if (!nilfs_loaded(nilfs)) { 836783f6184SRyusuke Konishi err = load_nilfs(nilfs, sbi); 837783f6184SRyusuke Konishi if (err) 838783f6184SRyusuke Konishi goto failed_sbi; 839783f6184SRyusuke Konishi } 840783f6184SRyusuke Konishi cno = nilfs_last_cno(nilfs); 841783f6184SRyusuke Konishi 842783f6184SRyusuke Konishi if (sb->s_flags & MS_RDONLY) { 843783f6184SRyusuke Konishi if (nilfs_test_opt(sbi, SNAPSHOT)) { 844*1f5abe7eSRyusuke Konishi err = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, 845*1f5abe7eSRyusuke Konishi sbi->s_snapshot_cno); 846*1f5abe7eSRyusuke Konishi if (err < 0) 847*1f5abe7eSRyusuke Konishi goto failed_sbi; 848*1f5abe7eSRyusuke Konishi if (!err) { 849783f6184SRyusuke Konishi printk(KERN_ERR 850783f6184SRyusuke Konishi "NILFS: The specified checkpoint is " 851783f6184SRyusuke Konishi "not a snapshot " 852783f6184SRyusuke Konishi "(checkpoint number=%llu).\n", 853783f6184SRyusuke Konishi (unsigned long long)sbi->s_snapshot_cno); 854783f6184SRyusuke Konishi err = -EINVAL; 855783f6184SRyusuke Konishi goto failed_sbi; 856783f6184SRyusuke Konishi } 857783f6184SRyusuke Konishi cno = sbi->s_snapshot_cno; 858783f6184SRyusuke Konishi } else 859783f6184SRyusuke Konishi /* Read-only mount */ 860783f6184SRyusuke Konishi sbi->s_snapshot_cno = cno; 861783f6184SRyusuke Konishi } 862783f6184SRyusuke Konishi 863783f6184SRyusuke Konishi err = nilfs_attach_checkpoint(sbi, cno); 864783f6184SRyusuke Konishi if (err) { 865783f6184SRyusuke Konishi printk(KERN_ERR "NILFS: error loading a checkpoint" 866783f6184SRyusuke Konishi " (checkpoint number=%llu).\n", (unsigned long long)cno); 867783f6184SRyusuke Konishi goto failed_sbi; 868783f6184SRyusuke Konishi } 869783f6184SRyusuke Konishi 870783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 871783f6184SRyusuke Konishi err = nilfs_attach_segment_constructor(sbi, NULL); 872783f6184SRyusuke Konishi if (err) 873783f6184SRyusuke Konishi goto failed_checkpoint; 874783f6184SRyusuke Konishi } 875783f6184SRyusuke Konishi 876783f6184SRyusuke Konishi root = nilfs_iget(sb, NILFS_ROOT_INO); 877783f6184SRyusuke Konishi if (IS_ERR(root)) { 878783f6184SRyusuke Konishi printk(KERN_ERR "NILFS: get root inode failed\n"); 879783f6184SRyusuke Konishi err = PTR_ERR(root); 880783f6184SRyusuke Konishi goto failed_segctor; 881783f6184SRyusuke Konishi } 882783f6184SRyusuke Konishi if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) { 883783f6184SRyusuke Konishi iput(root); 884783f6184SRyusuke Konishi printk(KERN_ERR "NILFS: corrupt root inode.\n"); 885783f6184SRyusuke Konishi err = -EINVAL; 886783f6184SRyusuke Konishi goto failed_segctor; 887783f6184SRyusuke Konishi } 888783f6184SRyusuke Konishi sb->s_root = d_alloc_root(root); 889783f6184SRyusuke Konishi if (!sb->s_root) { 890783f6184SRyusuke Konishi iput(root); 891783f6184SRyusuke Konishi printk(KERN_ERR "NILFS: get root dentry failed\n"); 892783f6184SRyusuke Konishi err = -ENOMEM; 893783f6184SRyusuke Konishi goto failed_segctor; 894783f6184SRyusuke Konishi } 895783f6184SRyusuke Konishi 896783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 897783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 898783f6184SRyusuke Konishi nilfs_setup_super(sbi); 899783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 900783f6184SRyusuke Konishi } 901783f6184SRyusuke Konishi 902783f6184SRyusuke Konishi err = nilfs_mark_recovery_complete(sbi); 903783f6184SRyusuke Konishi if (unlikely(err)) { 904783f6184SRyusuke Konishi printk(KERN_ERR "NILFS: recovery failed.\n"); 905783f6184SRyusuke Konishi goto failed_root; 906783f6184SRyusuke Konishi } 907783f6184SRyusuke Konishi 908783f6184SRyusuke Konishi return 0; 909783f6184SRyusuke Konishi 910783f6184SRyusuke Konishi failed_root: 911783f6184SRyusuke Konishi dput(sb->s_root); 912783f6184SRyusuke Konishi sb->s_root = NULL; 913783f6184SRyusuke Konishi 914783f6184SRyusuke Konishi failed_segctor: 915783f6184SRyusuke Konishi nilfs_detach_segment_constructor(sbi); 916783f6184SRyusuke Konishi 917783f6184SRyusuke Konishi failed_checkpoint: 918783f6184SRyusuke Konishi nilfs_detach_checkpoint(sbi); 919783f6184SRyusuke Konishi 920783f6184SRyusuke Konishi failed_sbi: 921783f6184SRyusuke Konishi put_nilfs(nilfs); 922783f6184SRyusuke Konishi sb->s_fs_info = NULL; 923783f6184SRyusuke Konishi kfree(sbi); 924783f6184SRyusuke Konishi return err; 925783f6184SRyusuke Konishi } 926783f6184SRyusuke Konishi 927783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data) 928783f6184SRyusuke Konishi { 929783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 930783f6184SRyusuke Konishi struct nilfs_super_block *sbp; 931783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 932783f6184SRyusuke Konishi unsigned long old_sb_flags; 933783f6184SRyusuke Konishi struct nilfs_mount_options old_opts; 934783f6184SRyusuke Konishi int err; 935783f6184SRyusuke Konishi 936783f6184SRyusuke Konishi old_sb_flags = sb->s_flags; 937783f6184SRyusuke Konishi old_opts.mount_opt = sbi->s_mount_opt; 938783f6184SRyusuke Konishi old_opts.snapshot_cno = sbi->s_snapshot_cno; 939783f6184SRyusuke Konishi 940783f6184SRyusuke Konishi if (!parse_options(data, sb)) { 941783f6184SRyusuke Konishi err = -EINVAL; 942783f6184SRyusuke Konishi goto restore_opts; 943783f6184SRyusuke Konishi } 944783f6184SRyusuke Konishi sb->s_flags = (sb->s_flags & ~MS_POSIXACL); 945783f6184SRyusuke Konishi 946783f6184SRyusuke Konishi if ((*flags & MS_RDONLY) && 947783f6184SRyusuke Konishi sbi->s_snapshot_cno != old_opts.snapshot_cno) { 948783f6184SRyusuke Konishi printk(KERN_WARNING "NILFS (device %s): couldn't " 949783f6184SRyusuke Konishi "remount to a different snapshot. \n", 950783f6184SRyusuke Konishi sb->s_id); 951783f6184SRyusuke Konishi err = -EINVAL; 952783f6184SRyusuke Konishi goto restore_opts; 953783f6184SRyusuke Konishi } 954783f6184SRyusuke Konishi 955783f6184SRyusuke Konishi if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 956783f6184SRyusuke Konishi goto out; 957783f6184SRyusuke Konishi if (*flags & MS_RDONLY) { 958783f6184SRyusuke Konishi /* Shutting down the segment constructor */ 959783f6184SRyusuke Konishi nilfs_detach_segment_constructor(sbi); 960783f6184SRyusuke Konishi sb->s_flags |= MS_RDONLY; 961783f6184SRyusuke Konishi 962783f6184SRyusuke Konishi sbi->s_snapshot_cno = nilfs_last_cno(nilfs); 963783f6184SRyusuke Konishi /* nilfs_set_opt(sbi, SNAPSHOT); */ 964783f6184SRyusuke Konishi 965783f6184SRyusuke Konishi /* 966783f6184SRyusuke Konishi * Remounting a valid RW partition RDONLY, so set 967783f6184SRyusuke Konishi * the RDONLY flag and then mark the partition as valid again. 968783f6184SRyusuke Konishi */ 969783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 970783f6184SRyusuke Konishi sbp = nilfs->ns_sbp; 971783f6184SRyusuke Konishi if (!(sbp->s_state & le16_to_cpu(NILFS_VALID_FS)) && 972783f6184SRyusuke Konishi (nilfs->ns_mount_state & NILFS_VALID_FS)) 973783f6184SRyusuke Konishi sbp->s_state = cpu_to_le16(nilfs->ns_mount_state); 974783f6184SRyusuke Konishi sbp->s_mtime = cpu_to_le64(get_seconds()); 975783f6184SRyusuke Konishi nilfs_commit_super(sbi); 976783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 977783f6184SRyusuke Konishi } else { 978783f6184SRyusuke Konishi /* 979783f6184SRyusuke Konishi * Mounting a RDONLY partition read-write, so reread and 980783f6184SRyusuke Konishi * store the current valid flag. (It may have been changed 981783f6184SRyusuke Konishi * by fsck since we originally mounted the partition.) 982783f6184SRyusuke Konishi */ 983783f6184SRyusuke Konishi down(&sb->s_bdev->bd_mount_sem); 984783f6184SRyusuke Konishi /* Check existing RW-mount */ 985783f6184SRyusuke Konishi if (test_exclusive_mount(sb->s_type, sb->s_bdev, 0)) { 986783f6184SRyusuke Konishi printk(KERN_WARNING "NILFS (device %s): couldn't " 987783f6184SRyusuke Konishi "remount because a RW-mount exists.\n", 988783f6184SRyusuke Konishi sb->s_id); 989783f6184SRyusuke Konishi err = -EBUSY; 990783f6184SRyusuke Konishi goto rw_remount_failed; 991783f6184SRyusuke Konishi } 992783f6184SRyusuke Konishi if (sbi->s_snapshot_cno != nilfs_last_cno(nilfs)) { 993783f6184SRyusuke Konishi printk(KERN_WARNING "NILFS (device %s): couldn't " 994783f6184SRyusuke Konishi "remount because the current RO-mount is not " 995783f6184SRyusuke Konishi "the latest one.\n", 996783f6184SRyusuke Konishi sb->s_id); 997783f6184SRyusuke Konishi err = -EINVAL; 998783f6184SRyusuke Konishi goto rw_remount_failed; 999783f6184SRyusuke Konishi } 1000783f6184SRyusuke Konishi sb->s_flags &= ~MS_RDONLY; 1001783f6184SRyusuke Konishi nilfs_clear_opt(sbi, SNAPSHOT); 1002783f6184SRyusuke Konishi sbi->s_snapshot_cno = 0; 1003783f6184SRyusuke Konishi 1004783f6184SRyusuke Konishi err = nilfs_attach_segment_constructor(sbi, NULL); 1005783f6184SRyusuke Konishi if (err) 1006783f6184SRyusuke Konishi goto rw_remount_failed; 1007783f6184SRyusuke Konishi 1008783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 1009783f6184SRyusuke Konishi nilfs_setup_super(sbi); 1010783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 1011783f6184SRyusuke Konishi 1012783f6184SRyusuke Konishi up(&sb->s_bdev->bd_mount_sem); 1013783f6184SRyusuke Konishi } 1014783f6184SRyusuke Konishi out: 1015783f6184SRyusuke Konishi return 0; 1016783f6184SRyusuke Konishi 1017783f6184SRyusuke Konishi rw_remount_failed: 1018783f6184SRyusuke Konishi up(&sb->s_bdev->bd_mount_sem); 1019783f6184SRyusuke Konishi restore_opts: 1020783f6184SRyusuke Konishi sb->s_flags = old_sb_flags; 1021783f6184SRyusuke Konishi sbi->s_mount_opt = old_opts.mount_opt; 1022783f6184SRyusuke Konishi sbi->s_snapshot_cno = old_opts.snapshot_cno; 1023783f6184SRyusuke Konishi return err; 1024783f6184SRyusuke Konishi } 1025783f6184SRyusuke Konishi 1026783f6184SRyusuke Konishi struct nilfs_super_data { 1027783f6184SRyusuke Konishi struct block_device *bdev; 1028783f6184SRyusuke Konishi __u64 cno; 1029783f6184SRyusuke Konishi int flags; 1030783f6184SRyusuke Konishi }; 1031783f6184SRyusuke Konishi 1032783f6184SRyusuke Konishi /** 1033783f6184SRyusuke Konishi * nilfs_identify - pre-read mount options needed to identify mount instance 1034783f6184SRyusuke Konishi * @data: mount options 1035783f6184SRyusuke Konishi * @sd: nilfs_super_data 1036783f6184SRyusuke Konishi */ 1037783f6184SRyusuke Konishi static int nilfs_identify(char *data, struct nilfs_super_data *sd) 1038783f6184SRyusuke Konishi { 1039783f6184SRyusuke Konishi char *p, *options = data; 1040783f6184SRyusuke Konishi substring_t args[MAX_OPT_ARGS]; 1041783f6184SRyusuke Konishi int option, token; 1042783f6184SRyusuke Konishi int ret = 0; 1043783f6184SRyusuke Konishi 1044783f6184SRyusuke Konishi do { 1045783f6184SRyusuke Konishi p = strsep(&options, ","); 1046783f6184SRyusuke Konishi if (p != NULL && *p) { 1047783f6184SRyusuke Konishi token = match_token(p, tokens, args); 1048783f6184SRyusuke Konishi if (token == Opt_snapshot) { 1049783f6184SRyusuke Konishi if (!(sd->flags & MS_RDONLY)) 1050783f6184SRyusuke Konishi ret++; 1051783f6184SRyusuke Konishi else { 1052783f6184SRyusuke Konishi ret = match_int(&args[0], &option); 1053783f6184SRyusuke Konishi if (!ret) { 1054783f6184SRyusuke Konishi if (option > 0) 1055783f6184SRyusuke Konishi sd->cno = option; 1056783f6184SRyusuke Konishi else 1057783f6184SRyusuke Konishi ret++; 1058783f6184SRyusuke Konishi } 1059783f6184SRyusuke Konishi } 1060783f6184SRyusuke Konishi } 1061783f6184SRyusuke Konishi if (ret) 1062783f6184SRyusuke Konishi printk(KERN_ERR 1063783f6184SRyusuke Konishi "NILFS: invalid mount option: %s\n", p); 1064783f6184SRyusuke Konishi } 1065783f6184SRyusuke Konishi if (!options) 1066783f6184SRyusuke Konishi break; 1067783f6184SRyusuke Konishi BUG_ON(options == data); 1068783f6184SRyusuke Konishi *(options - 1) = ','; 1069783f6184SRyusuke Konishi } while (!ret); 1070783f6184SRyusuke Konishi return ret; 1071783f6184SRyusuke Konishi } 1072783f6184SRyusuke Konishi 1073783f6184SRyusuke Konishi static int nilfs_set_bdev_super(struct super_block *s, void *data) 1074783f6184SRyusuke Konishi { 1075783f6184SRyusuke Konishi struct nilfs_super_data *sd = data; 1076783f6184SRyusuke Konishi 1077783f6184SRyusuke Konishi s->s_bdev = sd->bdev; 1078783f6184SRyusuke Konishi s->s_dev = s->s_bdev->bd_dev; 1079783f6184SRyusuke Konishi return 0; 1080783f6184SRyusuke Konishi } 1081783f6184SRyusuke Konishi 1082783f6184SRyusuke Konishi static int nilfs_test_bdev_super(struct super_block *s, void *data) 1083783f6184SRyusuke Konishi { 1084783f6184SRyusuke Konishi struct nilfs_super_data *sd = data; 1085783f6184SRyusuke Konishi 1086783f6184SRyusuke Konishi return s->s_bdev == sd->bdev; 1087783f6184SRyusuke Konishi } 1088783f6184SRyusuke Konishi 1089783f6184SRyusuke Konishi static int nilfs_test_bdev_super2(struct super_block *s, void *data) 1090783f6184SRyusuke Konishi { 1091783f6184SRyusuke Konishi struct nilfs_super_data *sd = data; 1092783f6184SRyusuke Konishi int ret; 1093783f6184SRyusuke Konishi 1094783f6184SRyusuke Konishi if (s->s_bdev != sd->bdev) 1095783f6184SRyusuke Konishi return 0; 1096783f6184SRyusuke Konishi 1097783f6184SRyusuke Konishi if (!((s->s_flags | sd->flags) & MS_RDONLY)) 1098783f6184SRyusuke Konishi return 1; /* Reuse an old R/W-mode super_block */ 1099783f6184SRyusuke Konishi 1100783f6184SRyusuke Konishi if (s->s_flags & sd->flags & MS_RDONLY) { 1101783f6184SRyusuke Konishi if (down_read_trylock(&s->s_umount)) { 1102783f6184SRyusuke Konishi ret = s->s_root && 1103783f6184SRyusuke Konishi (sd->cno == NILFS_SB(s)->s_snapshot_cno); 1104783f6184SRyusuke Konishi up_read(&s->s_umount); 1105783f6184SRyusuke Konishi /* 1106783f6184SRyusuke Konishi * This path is locked with sb_lock by sget(). 1107783f6184SRyusuke Konishi * So, drop_super() causes deadlock. 1108783f6184SRyusuke Konishi */ 1109783f6184SRyusuke Konishi return ret; 1110783f6184SRyusuke Konishi } 1111783f6184SRyusuke Konishi } 1112783f6184SRyusuke Konishi return 0; 1113783f6184SRyusuke Konishi } 1114783f6184SRyusuke Konishi 1115783f6184SRyusuke Konishi static int 1116783f6184SRyusuke Konishi nilfs_get_sb(struct file_system_type *fs_type, int flags, 1117783f6184SRyusuke Konishi const char *dev_name, void *data, struct vfsmount *mnt) 1118783f6184SRyusuke Konishi { 1119783f6184SRyusuke Konishi struct nilfs_super_data sd; 1120783f6184SRyusuke Konishi struct super_block *s, *s2; 1121783f6184SRyusuke Konishi struct the_nilfs *nilfs = NULL; 1122783f6184SRyusuke Konishi int err, need_to_close = 1; 1123783f6184SRyusuke Konishi 1124783f6184SRyusuke Konishi sd.bdev = open_bdev_exclusive(dev_name, flags, fs_type); 1125783f6184SRyusuke Konishi if (IS_ERR(sd.bdev)) 1126783f6184SRyusuke Konishi return PTR_ERR(sd.bdev); 1127783f6184SRyusuke Konishi 1128783f6184SRyusuke Konishi /* 1129783f6184SRyusuke Konishi * To get mount instance using sget() vfs-routine, NILFS needs 1130783f6184SRyusuke Konishi * much more information than normal filesystems to identify mount 1131783f6184SRyusuke Konishi * instance. For snapshot mounts, not only a mount type (ro-mount 1132783f6184SRyusuke Konishi * or rw-mount) but also a checkpoint number is required. 1133783f6184SRyusuke Konishi * The results are passed in sget() using nilfs_super_data. 1134783f6184SRyusuke Konishi */ 1135783f6184SRyusuke Konishi sd.cno = 0; 1136783f6184SRyusuke Konishi sd.flags = flags; 1137783f6184SRyusuke Konishi if (nilfs_identify((char *)data, &sd)) { 1138783f6184SRyusuke Konishi err = -EINVAL; 1139783f6184SRyusuke Konishi goto failed; 1140783f6184SRyusuke Konishi } 1141783f6184SRyusuke Konishi 1142783f6184SRyusuke Konishi /* 1143783f6184SRyusuke Konishi * once the super is inserted into the list by sget, s_umount 1144783f6184SRyusuke Konishi * will protect the lockfs code from trying to start a snapshot 1145783f6184SRyusuke Konishi * while we are mounting 1146783f6184SRyusuke Konishi */ 1147783f6184SRyusuke Konishi down(&sd.bdev->bd_mount_sem); 1148783f6184SRyusuke Konishi if (!sd.cno && 1149783f6184SRyusuke Konishi (err = test_exclusive_mount(fs_type, sd.bdev, flags ^ MS_RDONLY))) { 1150783f6184SRyusuke Konishi err = (err < 0) ? : -EBUSY; 1151783f6184SRyusuke Konishi goto failed_unlock; 1152783f6184SRyusuke Konishi } 1153783f6184SRyusuke Konishi 1154783f6184SRyusuke Konishi /* 1155783f6184SRyusuke Konishi * Phase-1: search any existent instance and get the_nilfs 1156783f6184SRyusuke Konishi */ 1157783f6184SRyusuke Konishi s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, &sd); 1158783f6184SRyusuke Konishi if (IS_ERR(s)) 1159783f6184SRyusuke Konishi goto error_s; 1160783f6184SRyusuke Konishi 1161783f6184SRyusuke Konishi if (!s->s_root) { 1162783f6184SRyusuke Konishi err = -ENOMEM; 1163783f6184SRyusuke Konishi nilfs = alloc_nilfs(sd.bdev); 1164783f6184SRyusuke Konishi if (!nilfs) 1165783f6184SRyusuke Konishi goto cancel_new; 1166783f6184SRyusuke Konishi } else { 1167783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(s); 1168783f6184SRyusuke Konishi 1169783f6184SRyusuke Konishi /* 1170783f6184SRyusuke Konishi * s_umount protects super_block from unmount process; 1171783f6184SRyusuke Konishi * It covers pointers of nilfs_sb_info and the_nilfs. 1172783f6184SRyusuke Konishi */ 1173783f6184SRyusuke Konishi nilfs = sbi->s_nilfs; 1174783f6184SRyusuke Konishi get_nilfs(nilfs); 1175783f6184SRyusuke Konishi up_write(&s->s_umount); 1176783f6184SRyusuke Konishi 1177783f6184SRyusuke Konishi /* 1178783f6184SRyusuke Konishi * Phase-2: search specified snapshot or R/W mode super_block 1179783f6184SRyusuke Konishi */ 1180783f6184SRyusuke Konishi if (!sd.cno) 1181783f6184SRyusuke Konishi /* trying to get the latest checkpoint. */ 1182783f6184SRyusuke Konishi sd.cno = nilfs_last_cno(nilfs); 1183783f6184SRyusuke Konishi 1184783f6184SRyusuke Konishi s2 = sget(fs_type, nilfs_test_bdev_super2, 1185783f6184SRyusuke Konishi nilfs_set_bdev_super, &sd); 1186783f6184SRyusuke Konishi deactivate_super(s); 1187783f6184SRyusuke Konishi /* 1188783f6184SRyusuke Konishi * Although deactivate_super() invokes close_bdev_exclusive() at 1189783f6184SRyusuke Konishi * kill_block_super(). Here, s is an existent mount; we need 1190783f6184SRyusuke Konishi * one more close_bdev_exclusive() call. 1191783f6184SRyusuke Konishi */ 1192783f6184SRyusuke Konishi s = s2; 1193783f6184SRyusuke Konishi if (IS_ERR(s)) 1194783f6184SRyusuke Konishi goto error_s; 1195783f6184SRyusuke Konishi } 1196783f6184SRyusuke Konishi 1197783f6184SRyusuke Konishi if (!s->s_root) { 1198783f6184SRyusuke Konishi char b[BDEVNAME_SIZE]; 1199783f6184SRyusuke Konishi 1200783f6184SRyusuke Konishi s->s_flags = flags; 1201783f6184SRyusuke Konishi strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id)); 1202783f6184SRyusuke Konishi sb_set_blocksize(s, block_size(sd.bdev)); 1203783f6184SRyusuke Konishi 1204783f6184SRyusuke Konishi err = nilfs_fill_super(s, data, flags & MS_VERBOSE, nilfs); 1205783f6184SRyusuke Konishi if (err) 1206783f6184SRyusuke Konishi goto cancel_new; 1207783f6184SRyusuke Konishi 1208783f6184SRyusuke Konishi s->s_flags |= MS_ACTIVE; 1209783f6184SRyusuke Konishi need_to_close = 0; 1210783f6184SRyusuke Konishi } else if (!(s->s_flags & MS_RDONLY)) { 1211783f6184SRyusuke Konishi err = -EBUSY; 1212783f6184SRyusuke Konishi } 1213783f6184SRyusuke Konishi 1214783f6184SRyusuke Konishi up(&sd.bdev->bd_mount_sem); 1215783f6184SRyusuke Konishi put_nilfs(nilfs); 1216783f6184SRyusuke Konishi if (need_to_close) 1217783f6184SRyusuke Konishi close_bdev_exclusive(sd.bdev, flags); 1218783f6184SRyusuke Konishi simple_set_mnt(mnt, s); 1219783f6184SRyusuke Konishi return 0; 1220783f6184SRyusuke Konishi 1221783f6184SRyusuke Konishi error_s: 1222783f6184SRyusuke Konishi up(&sd.bdev->bd_mount_sem); 1223783f6184SRyusuke Konishi if (nilfs) 1224783f6184SRyusuke Konishi put_nilfs(nilfs); 1225783f6184SRyusuke Konishi close_bdev_exclusive(sd.bdev, flags); 1226783f6184SRyusuke Konishi return PTR_ERR(s); 1227783f6184SRyusuke Konishi 1228783f6184SRyusuke Konishi failed_unlock: 1229783f6184SRyusuke Konishi up(&sd.bdev->bd_mount_sem); 1230783f6184SRyusuke Konishi failed: 1231783f6184SRyusuke Konishi close_bdev_exclusive(sd.bdev, flags); 1232783f6184SRyusuke Konishi 1233783f6184SRyusuke Konishi return err; 1234783f6184SRyusuke Konishi 1235783f6184SRyusuke Konishi cancel_new: 1236783f6184SRyusuke Konishi /* Abandoning the newly allocated superblock */ 1237783f6184SRyusuke Konishi up(&sd.bdev->bd_mount_sem); 1238783f6184SRyusuke Konishi if (nilfs) 1239783f6184SRyusuke Konishi put_nilfs(nilfs); 1240783f6184SRyusuke Konishi up_write(&s->s_umount); 1241783f6184SRyusuke Konishi deactivate_super(s); 1242783f6184SRyusuke Konishi /* 1243783f6184SRyusuke Konishi * deactivate_super() invokes close_bdev_exclusive(). 1244783f6184SRyusuke Konishi * We must finish all post-cleaning before this call; 1245783f6184SRyusuke Konishi * put_nilfs() and unlocking bd_mount_sem need the block device. 1246783f6184SRyusuke Konishi */ 1247783f6184SRyusuke Konishi return err; 1248783f6184SRyusuke Konishi } 1249783f6184SRyusuke Konishi 1250783f6184SRyusuke Konishi static int nilfs_test_bdev_super3(struct super_block *s, void *data) 1251783f6184SRyusuke Konishi { 1252783f6184SRyusuke Konishi struct nilfs_super_data *sd = data; 1253783f6184SRyusuke Konishi int ret; 1254783f6184SRyusuke Konishi 1255783f6184SRyusuke Konishi if (s->s_bdev != sd->bdev) 1256783f6184SRyusuke Konishi return 0; 1257783f6184SRyusuke Konishi if (down_read_trylock(&s->s_umount)) { 1258783f6184SRyusuke Konishi ret = (s->s_flags & MS_RDONLY) && s->s_root && 1259783f6184SRyusuke Konishi nilfs_test_opt(NILFS_SB(s), SNAPSHOT); 1260783f6184SRyusuke Konishi up_read(&s->s_umount); 1261783f6184SRyusuke Konishi if (ret) 1262783f6184SRyusuke Konishi return 0; /* ignore snapshot mounts */ 1263783f6184SRyusuke Konishi } 1264783f6184SRyusuke Konishi return !((sd->flags ^ s->s_flags) & MS_RDONLY); 1265783f6184SRyusuke Konishi } 1266783f6184SRyusuke Konishi 1267783f6184SRyusuke Konishi static int __false_bdev_super(struct super_block *s, void *data) 1268783f6184SRyusuke Konishi { 1269783f6184SRyusuke Konishi #if 0 /* XXX: workaround for lock debug. This is not good idea */ 1270783f6184SRyusuke Konishi up_write(&s->s_umount); 1271783f6184SRyusuke Konishi #endif 1272783f6184SRyusuke Konishi return -EFAULT; 1273783f6184SRyusuke Konishi } 1274783f6184SRyusuke Konishi 1275783f6184SRyusuke Konishi /** 1276783f6184SRyusuke Konishi * test_exclusive_mount - check whether an exclusive RW/RO mount exists or not. 1277783f6184SRyusuke Konishi * fs_type: filesystem type 1278783f6184SRyusuke Konishi * bdev: block device 1279783f6184SRyusuke Konishi * flag: 0 (check rw-mount) or MS_RDONLY (check ro-mount) 1280783f6184SRyusuke Konishi * res: pointer to an integer to store result 1281783f6184SRyusuke Konishi * 1282783f6184SRyusuke Konishi * This function must be called within a section protected by bd_mount_mutex. 1283783f6184SRyusuke Konishi */ 1284783f6184SRyusuke Konishi static int test_exclusive_mount(struct file_system_type *fs_type, 1285783f6184SRyusuke Konishi struct block_device *bdev, int flags) 1286783f6184SRyusuke Konishi { 1287783f6184SRyusuke Konishi struct super_block *s; 1288783f6184SRyusuke Konishi struct nilfs_super_data sd = { .flags = flags, .bdev = bdev }; 1289783f6184SRyusuke Konishi 1290783f6184SRyusuke Konishi s = sget(fs_type, nilfs_test_bdev_super3, __false_bdev_super, &sd); 1291783f6184SRyusuke Konishi if (IS_ERR(s)) { 1292783f6184SRyusuke Konishi if (PTR_ERR(s) != -EFAULT) 1293783f6184SRyusuke Konishi return PTR_ERR(s); 1294783f6184SRyusuke Konishi return 0; /* Not found */ 1295783f6184SRyusuke Konishi } 1296783f6184SRyusuke Konishi up_write(&s->s_umount); 1297783f6184SRyusuke Konishi deactivate_super(s); 1298783f6184SRyusuke Konishi return 1; /* Found */ 1299783f6184SRyusuke Konishi } 1300783f6184SRyusuke Konishi 1301783f6184SRyusuke Konishi struct file_system_type nilfs_fs_type = { 1302783f6184SRyusuke Konishi .owner = THIS_MODULE, 1303783f6184SRyusuke Konishi .name = "nilfs2", 1304783f6184SRyusuke Konishi .get_sb = nilfs_get_sb, 1305783f6184SRyusuke Konishi .kill_sb = kill_block_super, 1306783f6184SRyusuke Konishi .fs_flags = FS_REQUIRES_DEV, 1307783f6184SRyusuke Konishi }; 1308783f6184SRyusuke Konishi 1309783f6184SRyusuke Konishi static int __init init_nilfs_fs(void) 1310783f6184SRyusuke Konishi { 1311783f6184SRyusuke Konishi int err; 1312783f6184SRyusuke Konishi 1313783f6184SRyusuke Konishi err = nilfs_init_inode_cache(); 1314783f6184SRyusuke Konishi if (err) 1315783f6184SRyusuke Konishi goto failed; 1316783f6184SRyusuke Konishi 1317783f6184SRyusuke Konishi err = nilfs_init_transaction_cache(); 1318783f6184SRyusuke Konishi if (err) 1319783f6184SRyusuke Konishi goto failed_inode_cache; 1320783f6184SRyusuke Konishi 1321783f6184SRyusuke Konishi err = nilfs_init_segbuf_cache(); 1322783f6184SRyusuke Konishi if (err) 1323783f6184SRyusuke Konishi goto failed_transaction_cache; 1324783f6184SRyusuke Konishi 1325783f6184SRyusuke Konishi err = nilfs_btree_path_cache_init(); 1326783f6184SRyusuke Konishi if (err) 1327783f6184SRyusuke Konishi goto failed_segbuf_cache; 1328783f6184SRyusuke Konishi 1329783f6184SRyusuke Konishi err = register_filesystem(&nilfs_fs_type); 1330783f6184SRyusuke Konishi if (err) 1331783f6184SRyusuke Konishi goto failed_btree_path_cache; 1332783f6184SRyusuke Konishi 1333783f6184SRyusuke Konishi return 0; 1334783f6184SRyusuke Konishi 1335783f6184SRyusuke Konishi failed_btree_path_cache: 1336783f6184SRyusuke Konishi nilfs_btree_path_cache_destroy(); 1337783f6184SRyusuke Konishi 1338783f6184SRyusuke Konishi failed_segbuf_cache: 1339783f6184SRyusuke Konishi nilfs_destroy_segbuf_cache(); 1340783f6184SRyusuke Konishi 1341783f6184SRyusuke Konishi failed_transaction_cache: 1342783f6184SRyusuke Konishi nilfs_destroy_transaction_cache(); 1343783f6184SRyusuke Konishi 1344783f6184SRyusuke Konishi failed_inode_cache: 1345783f6184SRyusuke Konishi nilfs_destroy_inode_cache(); 1346783f6184SRyusuke Konishi 1347783f6184SRyusuke Konishi failed: 1348783f6184SRyusuke Konishi return err; 1349783f6184SRyusuke Konishi } 1350783f6184SRyusuke Konishi 1351783f6184SRyusuke Konishi static void __exit exit_nilfs_fs(void) 1352783f6184SRyusuke Konishi { 1353783f6184SRyusuke Konishi nilfs_destroy_segbuf_cache(); 1354783f6184SRyusuke Konishi nilfs_destroy_transaction_cache(); 1355783f6184SRyusuke Konishi nilfs_destroy_inode_cache(); 1356783f6184SRyusuke Konishi nilfs_btree_path_cache_destroy(); 1357783f6184SRyusuke Konishi unregister_filesystem(&nilfs_fs_type); 1358783f6184SRyusuke Konishi } 1359783f6184SRyusuke Konishi 1360783f6184SRyusuke Konishi module_init(init_nilfs_fs) 1361783f6184SRyusuke Konishi module_exit(exit_nilfs_fs) 1362