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/vfs.h> 49783f6184SRyusuke Konishi #include <linux/writeback.h> 50783f6184SRyusuke Konishi #include <linux/kobject.h> 51b58a285bSJiro SEKIBA #include <linux/seq_file.h> 52b58a285bSJiro SEKIBA #include <linux/mount.h> 53783f6184SRyusuke Konishi #include "nilfs.h" 548e656fd5SRyusuke Konishi #include "export.h" 55783f6184SRyusuke Konishi #include "mdt.h" 56783f6184SRyusuke Konishi #include "alloc.h" 5705d0e94bSRyusuke Konishi #include "btree.h" 5805d0e94bSRyusuke Konishi #include "btnode.h" 59783f6184SRyusuke Konishi #include "page.h" 60783f6184SRyusuke Konishi #include "cpfile.h" 61783f6184SRyusuke Konishi #include "ifile.h" 62783f6184SRyusuke Konishi #include "dat.h" 63783f6184SRyusuke Konishi #include "segment.h" 64783f6184SRyusuke Konishi #include "segbuf.h" 65783f6184SRyusuke Konishi 66783f6184SRyusuke Konishi MODULE_AUTHOR("NTT Corp."); 67783f6184SRyusuke Konishi MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem " 68783f6184SRyusuke Konishi "(NILFS)"); 69783f6184SRyusuke Konishi MODULE_LICENSE("GPL"); 70783f6184SRyusuke Konishi 7141c88bd7SLi Hong struct kmem_cache *nilfs_inode_cachep; 7241c88bd7SLi Hong struct kmem_cache *nilfs_transaction_cachep; 7341c88bd7SLi Hong struct kmem_cache *nilfs_segbuf_cachep; 7441c88bd7SLi Hong struct kmem_cache *nilfs_btree_path_cache; 7541c88bd7SLi Hong 76783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data); 77783f6184SRyusuke Konishi 78c8a11c8aSRyusuke Konishi static void nilfs_set_error(struct nilfs_sb_info *sbi) 79c8a11c8aSRyusuke Konishi { 80c8a11c8aSRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 81d26493b6SJiro SEKIBA struct nilfs_super_block **sbp; 82c8a11c8aSRyusuke Konishi 83c8a11c8aSRyusuke Konishi down_write(&nilfs->ns_sem); 84c8a11c8aSRyusuke Konishi if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) { 85c8a11c8aSRyusuke Konishi nilfs->ns_mount_state |= NILFS_ERROR_FS; 86b2ac86e1SJiro SEKIBA sbp = nilfs_prepare_super(sbi, 0); 87d26493b6SJiro SEKIBA if (likely(sbp)) { 88d26493b6SJiro SEKIBA sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS); 89b2ac86e1SJiro SEKIBA if (sbp[1]) 90b2ac86e1SJiro SEKIBA sbp[1]->s_state |= cpu_to_le16(NILFS_ERROR_FS); 91b2ac86e1SJiro SEKIBA nilfs_commit_super(sbi, NILFS_SB_COMMIT_ALL); 92c8a11c8aSRyusuke Konishi } 93d26493b6SJiro SEKIBA } 94c8a11c8aSRyusuke Konishi up_write(&nilfs->ns_sem); 95c8a11c8aSRyusuke Konishi } 96c8a11c8aSRyusuke Konishi 97783f6184SRyusuke Konishi /** 98783f6184SRyusuke Konishi * nilfs_error() - report failure condition on a filesystem 99783f6184SRyusuke Konishi * 100783f6184SRyusuke Konishi * nilfs_error() sets an ERROR_FS flag on the superblock as well as 101783f6184SRyusuke Konishi * reporting an error message. It should be called when NILFS detects 102783f6184SRyusuke Konishi * incoherences or defects of meta data on disk. As for sustainable 103783f6184SRyusuke Konishi * errors such as a single-shot I/O error, nilfs_warning() or the printk() 104783f6184SRyusuke Konishi * function should be used instead. 105783f6184SRyusuke Konishi * 106783f6184SRyusuke Konishi * The segment constructor must not call this function because it can 107783f6184SRyusuke Konishi * kill itself. 108783f6184SRyusuke Konishi */ 109783f6184SRyusuke Konishi void nilfs_error(struct super_block *sb, const char *function, 110783f6184SRyusuke Konishi const char *fmt, ...) 111783f6184SRyusuke Konishi { 112783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 113783f6184SRyusuke Konishi va_list args; 114783f6184SRyusuke Konishi 115783f6184SRyusuke Konishi va_start(args, fmt); 116783f6184SRyusuke Konishi printk(KERN_CRIT "NILFS error (device %s): %s: ", sb->s_id, function); 117783f6184SRyusuke Konishi vprintk(fmt, args); 118783f6184SRyusuke Konishi printk("\n"); 119783f6184SRyusuke Konishi va_end(args); 120783f6184SRyusuke Konishi 121783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 122c8a11c8aSRyusuke Konishi nilfs_set_error(sbi); 123783f6184SRyusuke Konishi 124783f6184SRyusuke Konishi if (nilfs_test_opt(sbi, ERRORS_RO)) { 125783f6184SRyusuke Konishi printk(KERN_CRIT "Remounting filesystem read-only\n"); 126783f6184SRyusuke Konishi sb->s_flags |= MS_RDONLY; 127783f6184SRyusuke Konishi } 128783f6184SRyusuke Konishi } 129783f6184SRyusuke Konishi 130783f6184SRyusuke Konishi if (nilfs_test_opt(sbi, ERRORS_PANIC)) 131783f6184SRyusuke Konishi panic("NILFS (device %s): panic forced after error\n", 132783f6184SRyusuke Konishi sb->s_id); 133783f6184SRyusuke Konishi } 134783f6184SRyusuke Konishi 135783f6184SRyusuke Konishi void nilfs_warning(struct super_block *sb, const char *function, 136783f6184SRyusuke Konishi const char *fmt, ...) 137783f6184SRyusuke Konishi { 138783f6184SRyusuke Konishi va_list args; 139783f6184SRyusuke Konishi 140783f6184SRyusuke Konishi va_start(args, fmt); 141783f6184SRyusuke Konishi printk(KERN_WARNING "NILFS warning (device %s): %s: ", 142783f6184SRyusuke Konishi sb->s_id, function); 143783f6184SRyusuke Konishi vprintk(fmt, args); 144783f6184SRyusuke Konishi printk("\n"); 145783f6184SRyusuke Konishi va_end(args); 146783f6184SRyusuke Konishi } 147783f6184SRyusuke Konishi 148783f6184SRyusuke Konishi 149a53b4751SRyusuke Konishi struct inode *nilfs_alloc_inode_common(struct the_nilfs *nilfs) 150783f6184SRyusuke Konishi { 151783f6184SRyusuke Konishi struct nilfs_inode_info *ii; 152783f6184SRyusuke Konishi 153783f6184SRyusuke Konishi ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS); 154783f6184SRyusuke Konishi if (!ii) 155783f6184SRyusuke Konishi return NULL; 156783f6184SRyusuke Konishi ii->i_bh = NULL; 157783f6184SRyusuke Konishi ii->i_state = 0; 1580e14a359SRyusuke Konishi ii->i_cno = 0; 159783f6184SRyusuke Konishi ii->vfs_inode.i_version = 1; 160a53b4751SRyusuke Konishi nilfs_btnode_cache_init(&ii->i_btnode_cache, nilfs->ns_bdi); 161783f6184SRyusuke Konishi return &ii->vfs_inode; 162783f6184SRyusuke Konishi } 163783f6184SRyusuke Konishi 164a53b4751SRyusuke Konishi struct inode *nilfs_alloc_inode(struct super_block *sb) 165a53b4751SRyusuke Konishi { 166a53b4751SRyusuke Konishi return nilfs_alloc_inode_common(NILFS_SB(sb)->s_nilfs); 167a53b4751SRyusuke Konishi } 168a53b4751SRyusuke Konishi 169783f6184SRyusuke Konishi void nilfs_destroy_inode(struct inode *inode) 170783f6184SRyusuke Konishi { 171b91c9a97SRyusuke Konishi struct nilfs_mdt_info *mdi = NILFS_MDT(inode); 172b91c9a97SRyusuke Konishi 173b91c9a97SRyusuke Konishi if (mdi) { 174b91c9a97SRyusuke Konishi kfree(mdi->mi_bgl); /* kfree(NULL) is safe */ 175b91c9a97SRyusuke Konishi kfree(mdi); 176b91c9a97SRyusuke Konishi } 177783f6184SRyusuke Konishi kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode)); 178783f6184SRyusuke Konishi } 179783f6184SRyusuke Konishi 180b2ac86e1SJiro SEKIBA static int nilfs_sync_super(struct nilfs_sb_info *sbi, int flag) 181783f6184SRyusuke Konishi { 182783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 183783f6184SRyusuke Konishi int err; 184783f6184SRyusuke Konishi 185783f6184SRyusuke Konishi retry: 186e339ad31SRyusuke Konishi set_buffer_dirty(nilfs->ns_sbh[0]); 18787e99511SChristoph Hellwig 18887e99511SChristoph Hellwig if (nilfs_test_opt(sbi, BARRIER)) { 18987e99511SChristoph Hellwig err = __sync_dirty_buffer(nilfs->ns_sbh[0], 19087e99511SChristoph Hellwig WRITE_SYNC | WRITE_BARRIER); 19187e99511SChristoph Hellwig if (err == -EOPNOTSUPP) { 192783f6184SRyusuke Konishi nilfs_warning(sbi->s_super, __func__, 193783f6184SRyusuke Konishi "barrier-based sync failed. " 194783f6184SRyusuke Konishi "disabling barriers\n"); 195783f6184SRyusuke Konishi nilfs_clear_opt(sbi, BARRIER); 196783f6184SRyusuke Konishi goto retry; 197783f6184SRyusuke Konishi } 19887e99511SChristoph Hellwig } else { 19987e99511SChristoph Hellwig err = sync_dirty_buffer(nilfs->ns_sbh[0]); 20087e99511SChristoph Hellwig } 20187e99511SChristoph Hellwig 202e339ad31SRyusuke Konishi if (unlikely(err)) { 203783f6184SRyusuke Konishi printk(KERN_ERR 204783f6184SRyusuke Konishi "NILFS: unable to write superblock (err=%d)\n", err); 205e339ad31SRyusuke Konishi if (err == -EIO && nilfs->ns_sbh[1]) { 206b2ac86e1SJiro SEKIBA /* 207b2ac86e1SJiro SEKIBA * sbp[0] points to newer log than sbp[1], 208b2ac86e1SJiro SEKIBA * so copy sbp[0] to sbp[1] to take over sbp[0]. 209b2ac86e1SJiro SEKIBA */ 210b2ac86e1SJiro SEKIBA memcpy(nilfs->ns_sbp[1], nilfs->ns_sbp[0], 211b2ac86e1SJiro SEKIBA nilfs->ns_sbsize); 212e339ad31SRyusuke Konishi nilfs_fall_back_super_block(nilfs); 213e339ad31SRyusuke Konishi goto retry; 214e339ad31SRyusuke Konishi } 215e339ad31SRyusuke Konishi } else { 216e339ad31SRyusuke Konishi struct nilfs_super_block *sbp = nilfs->ns_sbp[0]; 217e339ad31SRyusuke Konishi 218b2ac86e1SJiro SEKIBA nilfs->ns_sbwcount++; 219b2ac86e1SJiro SEKIBA 220e339ad31SRyusuke Konishi /* 221e339ad31SRyusuke Konishi * The latest segment becomes trailable from the position 222e339ad31SRyusuke Konishi * written in superblock. 223e339ad31SRyusuke Konishi */ 224783f6184SRyusuke Konishi clear_nilfs_discontinued(nilfs); 225e339ad31SRyusuke Konishi 226e339ad31SRyusuke Konishi /* update GC protection for recent segments */ 227e339ad31SRyusuke Konishi if (nilfs->ns_sbh[1]) { 228b2ac86e1SJiro SEKIBA if (flag == NILFS_SB_COMMIT_ALL) { 229e339ad31SRyusuke Konishi set_buffer_dirty(nilfs->ns_sbh[1]); 230b2ac86e1SJiro SEKIBA if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0) 231b2ac86e1SJiro SEKIBA goto out; 232b2ac86e1SJiro SEKIBA } 233b2ac86e1SJiro SEKIBA if (le64_to_cpu(nilfs->ns_sbp[1]->s_last_cno) < 234b2ac86e1SJiro SEKIBA le64_to_cpu(nilfs->ns_sbp[0]->s_last_cno)) 235e339ad31SRyusuke Konishi sbp = nilfs->ns_sbp[1]; 236e339ad31SRyusuke Konishi } 237b2ac86e1SJiro SEKIBA 2382c2e52fcSRyusuke Konishi spin_lock(&nilfs->ns_last_segment_lock); 239e339ad31SRyusuke Konishi nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq); 2402c2e52fcSRyusuke Konishi spin_unlock(&nilfs->ns_last_segment_lock); 241783f6184SRyusuke Konishi } 242b2ac86e1SJiro SEKIBA out: 243783f6184SRyusuke Konishi return err; 244783f6184SRyusuke Konishi } 245783f6184SRyusuke Konishi 24660f46b7eSRyusuke Konishi void nilfs_set_log_cursor(struct nilfs_super_block *sbp, 24760f46b7eSRyusuke Konishi struct the_nilfs *nilfs) 24860f46b7eSRyusuke Konishi { 24960f46b7eSRyusuke Konishi sector_t nfreeblocks; 25060f46b7eSRyusuke Konishi 25160f46b7eSRyusuke Konishi /* nilfs->ns_sem must be locked by the caller. */ 25260f46b7eSRyusuke Konishi nilfs_count_free_blocks(nilfs, &nfreeblocks); 25360f46b7eSRyusuke Konishi sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks); 25460f46b7eSRyusuke Konishi 25560f46b7eSRyusuke Konishi spin_lock(&nilfs->ns_last_segment_lock); 25660f46b7eSRyusuke Konishi sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq); 25760f46b7eSRyusuke Konishi sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg); 25860f46b7eSRyusuke Konishi sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno); 25960f46b7eSRyusuke Konishi spin_unlock(&nilfs->ns_last_segment_lock); 26060f46b7eSRyusuke Konishi } 26160f46b7eSRyusuke Konishi 262b2ac86e1SJiro SEKIBA struct nilfs_super_block **nilfs_prepare_super(struct nilfs_sb_info *sbi, 263b2ac86e1SJiro SEKIBA int flip) 264d26493b6SJiro SEKIBA { 265d26493b6SJiro SEKIBA struct the_nilfs *nilfs = sbi->s_nilfs; 266d26493b6SJiro SEKIBA struct nilfs_super_block **sbp = nilfs->ns_sbp; 267d26493b6SJiro SEKIBA 268d26493b6SJiro SEKIBA /* nilfs->ns_sem must be locked by the caller. */ 269d26493b6SJiro SEKIBA if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) { 270d26493b6SJiro SEKIBA if (sbp[1] && 271d26493b6SJiro SEKIBA sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) { 272b2ac86e1SJiro SEKIBA memcpy(sbp[0], sbp[1], nilfs->ns_sbsize); 273d26493b6SJiro SEKIBA } else { 274d26493b6SJiro SEKIBA printk(KERN_CRIT "NILFS: superblock broke on dev %s\n", 275d26493b6SJiro SEKIBA sbi->s_super->s_id); 276d26493b6SJiro SEKIBA return NULL; 277d26493b6SJiro SEKIBA } 278b2ac86e1SJiro SEKIBA } else if (sbp[1] && 279b2ac86e1SJiro SEKIBA sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) { 280b2ac86e1SJiro SEKIBA memcpy(sbp[1], sbp[0], nilfs->ns_sbsize); 281d26493b6SJiro SEKIBA } 282b2ac86e1SJiro SEKIBA 283b2ac86e1SJiro SEKIBA if (flip && sbp[1]) 284b2ac86e1SJiro SEKIBA nilfs_swap_super_block(nilfs); 285b2ac86e1SJiro SEKIBA 286d26493b6SJiro SEKIBA return sbp; 287d26493b6SJiro SEKIBA } 288d26493b6SJiro SEKIBA 289b2ac86e1SJiro SEKIBA int nilfs_commit_super(struct nilfs_sb_info *sbi, int flag) 290783f6184SRyusuke Konishi { 291783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 292e339ad31SRyusuke Konishi struct nilfs_super_block **sbp = nilfs->ns_sbp; 293e339ad31SRyusuke Konishi time_t t; 294783f6184SRyusuke Konishi 295d26493b6SJiro SEKIBA /* nilfs->ns_sem must be locked by the caller. */ 296e339ad31SRyusuke Konishi t = get_seconds(); 297b2ac86e1SJiro SEKIBA nilfs->ns_sbwtime = t; 298e339ad31SRyusuke Konishi sbp[0]->s_wtime = cpu_to_le64(t); 299e339ad31SRyusuke Konishi sbp[0]->s_sum = 0; 300e339ad31SRyusuke Konishi sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed, 301e339ad31SRyusuke Konishi (unsigned char *)sbp[0], 302e339ad31SRyusuke Konishi nilfs->ns_sbsize)); 303b2ac86e1SJiro SEKIBA if (flag == NILFS_SB_COMMIT_ALL && sbp[1]) { 304b2ac86e1SJiro SEKIBA sbp[1]->s_wtime = sbp[0]->s_wtime; 305b2ac86e1SJiro SEKIBA sbp[1]->s_sum = 0; 306b2ac86e1SJiro SEKIBA sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed, 307b2ac86e1SJiro SEKIBA (unsigned char *)sbp[1], 308b2ac86e1SJiro SEKIBA nilfs->ns_sbsize)); 309e339ad31SRyusuke Konishi } 310e605f0a7SRyusuke Konishi clear_nilfs_sb_dirty(nilfs); 311b2ac86e1SJiro SEKIBA return nilfs_sync_super(sbi, flag); 312783f6184SRyusuke Konishi } 313783f6184SRyusuke Konishi 3147ecaa46cSRyusuke Konishi /** 3157ecaa46cSRyusuke Konishi * nilfs_cleanup_super() - write filesystem state for cleanup 3167ecaa46cSRyusuke Konishi * @sbi: nilfs_sb_info to be unmounted or degraded to read-only 3177ecaa46cSRyusuke Konishi * 3187ecaa46cSRyusuke Konishi * This function restores state flags in the on-disk super block. 3197ecaa46cSRyusuke Konishi * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the 3207ecaa46cSRyusuke Konishi * filesystem was not clean previously. 3217ecaa46cSRyusuke Konishi */ 3227ecaa46cSRyusuke Konishi int nilfs_cleanup_super(struct nilfs_sb_info *sbi) 3237ecaa46cSRyusuke Konishi { 324d26493b6SJiro SEKIBA struct nilfs_super_block **sbp; 325b2ac86e1SJiro SEKIBA int flag = NILFS_SB_COMMIT; 326d26493b6SJiro SEKIBA int ret = -EIO; 3277ecaa46cSRyusuke Konishi 328b2ac86e1SJiro SEKIBA sbp = nilfs_prepare_super(sbi, 0); 329d26493b6SJiro SEKIBA if (sbp) { 3307ecaa46cSRyusuke Konishi sbp[0]->s_state = cpu_to_le16(sbi->s_nilfs->ns_mount_state); 331b2ac86e1SJiro SEKIBA nilfs_set_log_cursor(sbp[0], sbi->s_nilfs); 332b2ac86e1SJiro SEKIBA if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) { 333b2ac86e1SJiro SEKIBA /* 334b2ac86e1SJiro SEKIBA * make the "clean" flag also to the opposite 335b2ac86e1SJiro SEKIBA * super block if both super blocks point to 336b2ac86e1SJiro SEKIBA * the same checkpoint. 337b2ac86e1SJiro SEKIBA */ 338b2ac86e1SJiro SEKIBA sbp[1]->s_state = sbp[0]->s_state; 339b2ac86e1SJiro SEKIBA flag = NILFS_SB_COMMIT_ALL; 340b2ac86e1SJiro SEKIBA } 341b2ac86e1SJiro SEKIBA ret = nilfs_commit_super(sbi, flag); 342d26493b6SJiro SEKIBA } 3437ecaa46cSRyusuke Konishi return ret; 3447ecaa46cSRyusuke Konishi } 3457ecaa46cSRyusuke Konishi 346783f6184SRyusuke Konishi static void nilfs_put_super(struct super_block *sb) 347783f6184SRyusuke Konishi { 348783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 349783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 350783f6184SRyusuke Konishi 351783f6184SRyusuke Konishi nilfs_detach_segment_constructor(sbi); 352783f6184SRyusuke Konishi 353783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 354783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 3557ecaa46cSRyusuke Konishi nilfs_cleanup_super(sbi); 356783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 357783f6184SRyusuke Konishi } 358783f6184SRyusuke Konishi 359*348fe8daSRyusuke Konishi destroy_nilfs(nilfs); 360783f6184SRyusuke Konishi sbi->s_super = NULL; 361783f6184SRyusuke Konishi sb->s_fs_info = NULL; 362f11459adSRyusuke Konishi kfree(sbi); 363783f6184SRyusuke Konishi } 364783f6184SRyusuke Konishi 365783f6184SRyusuke Konishi static int nilfs_sync_fs(struct super_block *sb, int wait) 366783f6184SRyusuke Konishi { 3676233caa9SJiro SEKIBA struct nilfs_sb_info *sbi = NILFS_SB(sb); 3686233caa9SJiro SEKIBA struct the_nilfs *nilfs = sbi->s_nilfs; 369d26493b6SJiro SEKIBA struct nilfs_super_block **sbp; 370783f6184SRyusuke Konishi int err = 0; 371783f6184SRyusuke Konishi 372783f6184SRyusuke Konishi /* This function is called when super block should be written back */ 373783f6184SRyusuke Konishi if (wait) 374783f6184SRyusuke Konishi err = nilfs_construct_segment(sb); 3756233caa9SJiro SEKIBA 3766233caa9SJiro SEKIBA down_write(&nilfs->ns_sem); 377d26493b6SJiro SEKIBA if (nilfs_sb_dirty(nilfs)) { 378b2ac86e1SJiro SEKIBA sbp = nilfs_prepare_super(sbi, nilfs_sb_will_flip(nilfs)); 379b2ac86e1SJiro SEKIBA if (likely(sbp)) { 380b2ac86e1SJiro SEKIBA nilfs_set_log_cursor(sbp[0], nilfs); 381b2ac86e1SJiro SEKIBA nilfs_commit_super(sbi, NILFS_SB_COMMIT); 382b2ac86e1SJiro SEKIBA } 383d26493b6SJiro SEKIBA } 3846233caa9SJiro SEKIBA up_write(&nilfs->ns_sem); 3856233caa9SJiro SEKIBA 386783f6184SRyusuke Konishi return err; 387783f6184SRyusuke Konishi } 388783f6184SRyusuke Konishi 3894d8d9293SRyusuke Konishi int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno, int curr_mnt, 3904d8d9293SRyusuke Konishi struct nilfs_root **rootp) 391783f6184SRyusuke Konishi { 392783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 3934d8d9293SRyusuke Konishi struct nilfs_root *root; 394783f6184SRyusuke Konishi struct nilfs_checkpoint *raw_cp; 395783f6184SRyusuke Konishi struct buffer_head *bh_cp; 3964d8d9293SRyusuke Konishi int err = -ENOMEM; 3974d8d9293SRyusuke Konishi 3984d8d9293SRyusuke Konishi root = nilfs_find_or_create_root( 3994d8d9293SRyusuke Konishi nilfs, curr_mnt ? NILFS_CPTREE_CURRENT_CNO : cno); 4004d8d9293SRyusuke Konishi if (!root) 4014d8d9293SRyusuke Konishi return err; 402783f6184SRyusuke Konishi 403e912a5b6SRyusuke Konishi if (root->ifile) 404e912a5b6SRyusuke Konishi goto reuse; /* already attached checkpoint */ 405783f6184SRyusuke Konishi 406e912a5b6SRyusuke Konishi root->ifile = nilfs_ifile_new(sbi, nilfs->ns_inode_size); 407e912a5b6SRyusuke Konishi if (!root->ifile) 408e912a5b6SRyusuke Konishi goto failed; 409783f6184SRyusuke Konishi 4101154ecbdSZhang Qiang down_read(&nilfs->ns_segctor_sem); 411783f6184SRyusuke Konishi err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp, 412783f6184SRyusuke Konishi &bh_cp); 4131154ecbdSZhang Qiang up_read(&nilfs->ns_segctor_sem); 414783f6184SRyusuke Konishi if (unlikely(err)) { 415783f6184SRyusuke Konishi if (err == -ENOENT || err == -EINVAL) { 416783f6184SRyusuke Konishi printk(KERN_ERR 417783f6184SRyusuke Konishi "NILFS: Invalid checkpoint " 418783f6184SRyusuke Konishi "(checkpoint number=%llu)\n", 419783f6184SRyusuke Konishi (unsigned long long)cno); 420783f6184SRyusuke Konishi err = -EINVAL; 421783f6184SRyusuke Konishi } 422783f6184SRyusuke Konishi goto failed; 423783f6184SRyusuke Konishi } 424e912a5b6SRyusuke Konishi err = nilfs_read_inode_common(root->ifile, &raw_cp->cp_ifile_inode); 425783f6184SRyusuke Konishi if (unlikely(err)) 426783f6184SRyusuke Konishi goto failed_bh; 427b7c06342SRyusuke Konishi 428b7c06342SRyusuke Konishi atomic_set(&root->inodes_count, le64_to_cpu(raw_cp->cp_inodes_count)); 429b7c06342SRyusuke Konishi atomic_set(&root->blocks_count, le64_to_cpu(raw_cp->cp_blocks_count)); 430783f6184SRyusuke Konishi 431783f6184SRyusuke Konishi nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp); 4324d8d9293SRyusuke Konishi 433e912a5b6SRyusuke Konishi reuse: 4344d8d9293SRyusuke Konishi *rootp = root; 435783f6184SRyusuke Konishi return 0; 436783f6184SRyusuke Konishi 437783f6184SRyusuke Konishi failed_bh: 438783f6184SRyusuke Konishi nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp); 439783f6184SRyusuke Konishi failed: 4404d8d9293SRyusuke Konishi nilfs_put_root(root); 441783f6184SRyusuke Konishi 442783f6184SRyusuke Konishi return err; 443783f6184SRyusuke Konishi } 444783f6184SRyusuke Konishi 445783f6184SRyusuke Konishi static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf) 446783f6184SRyusuke Konishi { 447783f6184SRyusuke Konishi struct super_block *sb = dentry->d_sb; 448b7c06342SRyusuke Konishi struct nilfs_root *root = NILFS_I(dentry->d_inode)->i_root; 449b7c06342SRyusuke Konishi struct the_nilfs *nilfs = root->nilfs; 450c306af23SRyusuke Konishi u64 id = huge_encode_dev(sb->s_bdev->bd_dev); 451783f6184SRyusuke Konishi unsigned long long blocks; 452783f6184SRyusuke Konishi unsigned long overhead; 453783f6184SRyusuke Konishi unsigned long nrsvblocks; 454783f6184SRyusuke Konishi sector_t nfreeblocks; 455783f6184SRyusuke Konishi int err; 456783f6184SRyusuke Konishi 457783f6184SRyusuke Konishi /* 458783f6184SRyusuke Konishi * Compute all of the segment blocks 459783f6184SRyusuke Konishi * 460783f6184SRyusuke Konishi * The blocks before first segment and after last segment 461783f6184SRyusuke Konishi * are excluded. 462783f6184SRyusuke Konishi */ 463783f6184SRyusuke Konishi blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments 464783f6184SRyusuke Konishi - nilfs->ns_first_data_block; 465783f6184SRyusuke Konishi nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment; 466783f6184SRyusuke Konishi 467783f6184SRyusuke Konishi /* 468783f6184SRyusuke Konishi * Compute the overhead 469783f6184SRyusuke Konishi * 4707a65004bSRyusuke Konishi * When distributing meta data blocks outside segment structure, 471783f6184SRyusuke Konishi * We must count them as the overhead. 472783f6184SRyusuke Konishi */ 473783f6184SRyusuke Konishi overhead = 0; 474783f6184SRyusuke Konishi 475783f6184SRyusuke Konishi err = nilfs_count_free_blocks(nilfs, &nfreeblocks); 476783f6184SRyusuke Konishi if (unlikely(err)) 477783f6184SRyusuke Konishi return err; 478783f6184SRyusuke Konishi 479783f6184SRyusuke Konishi buf->f_type = NILFS_SUPER_MAGIC; 480783f6184SRyusuke Konishi buf->f_bsize = sb->s_blocksize; 481783f6184SRyusuke Konishi buf->f_blocks = blocks - overhead; 482783f6184SRyusuke Konishi buf->f_bfree = nfreeblocks; 483783f6184SRyusuke Konishi buf->f_bavail = (buf->f_bfree >= nrsvblocks) ? 484783f6184SRyusuke Konishi (buf->f_bfree - nrsvblocks) : 0; 485b7c06342SRyusuke Konishi buf->f_files = atomic_read(&root->inodes_count); 486783f6184SRyusuke Konishi buf->f_ffree = 0; /* nilfs_count_free_inodes(sb); */ 487783f6184SRyusuke Konishi buf->f_namelen = NILFS_NAME_LEN; 488c306af23SRyusuke Konishi buf->f_fsid.val[0] = (u32)id; 489c306af23SRyusuke Konishi buf->f_fsid.val[1] = (u32)(id >> 32); 490c306af23SRyusuke Konishi 491783f6184SRyusuke Konishi return 0; 492783f6184SRyusuke Konishi } 493783f6184SRyusuke Konishi 494b58a285bSJiro SEKIBA static int nilfs_show_options(struct seq_file *seq, struct vfsmount *vfs) 495b58a285bSJiro SEKIBA { 496b58a285bSJiro SEKIBA struct super_block *sb = vfs->mnt_sb; 497b58a285bSJiro SEKIBA struct nilfs_sb_info *sbi = NILFS_SB(sb); 498f11459adSRyusuke Konishi struct nilfs_root *root = NILFS_I(vfs->mnt_root->d_inode)->i_root; 499b58a285bSJiro SEKIBA 500b58a285bSJiro SEKIBA if (!nilfs_test_opt(sbi, BARRIER)) 501c6b4d57dSRyusuke Konishi seq_puts(seq, ",nobarrier"); 502f11459adSRyusuke Konishi if (root->cno != NILFS_CPTREE_CURRENT_CNO) 503f11459adSRyusuke Konishi seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno); 504b58a285bSJiro SEKIBA if (nilfs_test_opt(sbi, ERRORS_PANIC)) 505c6b4d57dSRyusuke Konishi seq_puts(seq, ",errors=panic"); 506277a6a34SRyusuke Konishi if (nilfs_test_opt(sbi, ERRORS_CONT)) 507c6b4d57dSRyusuke Konishi seq_puts(seq, ",errors=continue"); 508b58a285bSJiro SEKIBA if (nilfs_test_opt(sbi, STRICT_ORDER)) 509c6b4d57dSRyusuke Konishi seq_puts(seq, ",order=strict"); 5100234576dSRyusuke Konishi if (nilfs_test_opt(sbi, NORECOVERY)) 511c6b4d57dSRyusuke Konishi seq_puts(seq, ",norecovery"); 512e902ec99SJiro SEKIBA if (nilfs_test_opt(sbi, DISCARD)) 513c6b4d57dSRyusuke Konishi seq_puts(seq, ",discard"); 514b58a285bSJiro SEKIBA 515b58a285bSJiro SEKIBA return 0; 516b58a285bSJiro SEKIBA } 517b58a285bSJiro SEKIBA 518b87221deSAlexey Dobriyan static const struct super_operations nilfs_sops = { 519783f6184SRyusuke Konishi .alloc_inode = nilfs_alloc_inode, 520783f6184SRyusuke Konishi .destroy_inode = nilfs_destroy_inode, 521783f6184SRyusuke Konishi .dirty_inode = nilfs_dirty_inode, 522783f6184SRyusuke Konishi /* .write_inode = nilfs_write_inode, */ 523783f6184SRyusuke Konishi /* .put_inode = nilfs_put_inode, */ 524783f6184SRyusuke Konishi /* .drop_inode = nilfs_drop_inode, */ 5256fd1e5c9SAl Viro .evict_inode = nilfs_evict_inode, 526783f6184SRyusuke Konishi .put_super = nilfs_put_super, 5271dfa2710SJiro SEKIBA /* .write_super = nilfs_write_super, */ 528783f6184SRyusuke Konishi .sync_fs = nilfs_sync_fs, 529783f6184SRyusuke Konishi /* .write_super_lockfs */ 530783f6184SRyusuke Konishi /* .unlockfs */ 531783f6184SRyusuke Konishi .statfs = nilfs_statfs, 532783f6184SRyusuke Konishi .remount_fs = nilfs_remount, 533783f6184SRyusuke Konishi /* .umount_begin */ 534b58a285bSJiro SEKIBA .show_options = nilfs_show_options 535783f6184SRyusuke Konishi }; 536783f6184SRyusuke Konishi 537783f6184SRyusuke Konishi enum { 538783f6184SRyusuke Konishi Opt_err_cont, Opt_err_panic, Opt_err_ro, 539773bc4f3SRyusuke Konishi Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery, 540802d3177SRyusuke Konishi Opt_discard, Opt_nodiscard, Opt_err, 541783f6184SRyusuke Konishi }; 542783f6184SRyusuke Konishi 543783f6184SRyusuke Konishi static match_table_t tokens = { 544783f6184SRyusuke Konishi {Opt_err_cont, "errors=continue"}, 545783f6184SRyusuke Konishi {Opt_err_panic, "errors=panic"}, 546783f6184SRyusuke Konishi {Opt_err_ro, "errors=remount-ro"}, 547773bc4f3SRyusuke Konishi {Opt_barrier, "barrier"}, 54891f1953bSJiro SEKIBA {Opt_nobarrier, "nobarrier"}, 549783f6184SRyusuke Konishi {Opt_snapshot, "cp=%u"}, 550783f6184SRyusuke Konishi {Opt_order, "order=%s"}, 5510234576dSRyusuke Konishi {Opt_norecovery, "norecovery"}, 552e902ec99SJiro SEKIBA {Opt_discard, "discard"}, 553802d3177SRyusuke Konishi {Opt_nodiscard, "nodiscard"}, 554783f6184SRyusuke Konishi {Opt_err, NULL} 555783f6184SRyusuke Konishi }; 556783f6184SRyusuke Konishi 5577c017457SRyusuke Konishi static int parse_options(char *options, struct super_block *sb, int is_remount) 558783f6184SRyusuke Konishi { 559783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 560783f6184SRyusuke Konishi char *p; 561783f6184SRyusuke Konishi substring_t args[MAX_OPT_ARGS]; 562783f6184SRyusuke Konishi int option; 563783f6184SRyusuke Konishi 564783f6184SRyusuke Konishi if (!options) 565783f6184SRyusuke Konishi return 1; 566783f6184SRyusuke Konishi 567783f6184SRyusuke Konishi while ((p = strsep(&options, ",")) != NULL) { 568783f6184SRyusuke Konishi int token; 569783f6184SRyusuke Konishi if (!*p) 570783f6184SRyusuke Konishi continue; 571783f6184SRyusuke Konishi 572783f6184SRyusuke Konishi token = match_token(p, tokens, args); 573783f6184SRyusuke Konishi switch (token) { 574773bc4f3SRyusuke Konishi case Opt_barrier: 575773bc4f3SRyusuke Konishi nilfs_set_opt(sbi, BARRIER); 576773bc4f3SRyusuke Konishi break; 57791f1953bSJiro SEKIBA case Opt_nobarrier: 578783f6184SRyusuke Konishi nilfs_clear_opt(sbi, BARRIER); 579783f6184SRyusuke Konishi break; 580783f6184SRyusuke Konishi case Opt_order: 581783f6184SRyusuke Konishi if (strcmp(args[0].from, "relaxed") == 0) 582783f6184SRyusuke Konishi /* Ordered data semantics */ 583783f6184SRyusuke Konishi nilfs_clear_opt(sbi, STRICT_ORDER); 584783f6184SRyusuke Konishi else if (strcmp(args[0].from, "strict") == 0) 585783f6184SRyusuke Konishi /* Strict in-order semantics */ 586783f6184SRyusuke Konishi nilfs_set_opt(sbi, STRICT_ORDER); 587783f6184SRyusuke Konishi else 588783f6184SRyusuke Konishi return 0; 589783f6184SRyusuke Konishi break; 590783f6184SRyusuke Konishi case Opt_err_panic: 591783f6184SRyusuke Konishi nilfs_write_opt(sbi, ERROR_MODE, ERRORS_PANIC); 592783f6184SRyusuke Konishi break; 593783f6184SRyusuke Konishi case Opt_err_ro: 594783f6184SRyusuke Konishi nilfs_write_opt(sbi, ERROR_MODE, ERRORS_RO); 595783f6184SRyusuke Konishi break; 596783f6184SRyusuke Konishi case Opt_err_cont: 597783f6184SRyusuke Konishi nilfs_write_opt(sbi, ERROR_MODE, ERRORS_CONT); 598783f6184SRyusuke Konishi break; 599783f6184SRyusuke Konishi case Opt_snapshot: 600783f6184SRyusuke Konishi if (match_int(&args[0], &option) || option <= 0) 601783f6184SRyusuke Konishi return 0; 6027c017457SRyusuke Konishi if (is_remount) { 6037c017457SRyusuke Konishi printk(KERN_ERR 604f11459adSRyusuke Konishi "NILFS: \"%s\" option is invalid " 605f11459adSRyusuke Konishi "for remount.\n", p); 6067c017457SRyusuke Konishi return 0; 6077c017457SRyusuke Konishi } 6087c017457SRyusuke Konishi break; 6090234576dSRyusuke Konishi case Opt_norecovery: 6100234576dSRyusuke Konishi nilfs_set_opt(sbi, NORECOVERY); 6110234576dSRyusuke Konishi break; 612e902ec99SJiro SEKIBA case Opt_discard: 613e902ec99SJiro SEKIBA nilfs_set_opt(sbi, DISCARD); 614e902ec99SJiro SEKIBA break; 615802d3177SRyusuke Konishi case Opt_nodiscard: 616802d3177SRyusuke Konishi nilfs_clear_opt(sbi, DISCARD); 617802d3177SRyusuke Konishi break; 618783f6184SRyusuke Konishi default: 619783f6184SRyusuke Konishi printk(KERN_ERR 620783f6184SRyusuke Konishi "NILFS: Unrecognized mount option \"%s\"\n", p); 621783f6184SRyusuke Konishi return 0; 622783f6184SRyusuke Konishi } 623783f6184SRyusuke Konishi } 624783f6184SRyusuke Konishi return 1; 625783f6184SRyusuke Konishi } 626783f6184SRyusuke Konishi 627783f6184SRyusuke Konishi static inline void 628783f6184SRyusuke Konishi nilfs_set_default_options(struct nilfs_sb_info *sbi, 629783f6184SRyusuke Konishi struct nilfs_super_block *sbp) 630783f6184SRyusuke Konishi { 631783f6184SRyusuke Konishi sbi->s_mount_opt = 632277a6a34SRyusuke Konishi NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER; 633783f6184SRyusuke Konishi } 634783f6184SRyusuke Konishi 635783f6184SRyusuke Konishi static int nilfs_setup_super(struct nilfs_sb_info *sbi) 636783f6184SRyusuke Konishi { 637783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 638d26493b6SJiro SEKIBA struct nilfs_super_block **sbp; 639d26493b6SJiro SEKIBA int max_mnt_count; 640d26493b6SJiro SEKIBA int mnt_count; 641783f6184SRyusuke Konishi 642d26493b6SJiro SEKIBA /* nilfs->ns_sem must be locked by the caller. */ 643b2ac86e1SJiro SEKIBA sbp = nilfs_prepare_super(sbi, 0); 644d26493b6SJiro SEKIBA if (!sbp) 645d26493b6SJiro SEKIBA return -EIO; 646d26493b6SJiro SEKIBA 647d26493b6SJiro SEKIBA max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count); 648d26493b6SJiro SEKIBA mnt_count = le16_to_cpu(sbp[0]->s_mnt_count); 649d26493b6SJiro SEKIBA 650f50a4c81SRyusuke Konishi if (nilfs->ns_mount_state & NILFS_ERROR_FS) { 651783f6184SRyusuke Konishi printk(KERN_WARNING 652783f6184SRyusuke Konishi "NILFS warning: mounting fs with errors\n"); 653783f6184SRyusuke Konishi #if 0 654783f6184SRyusuke Konishi } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) { 655783f6184SRyusuke Konishi printk(KERN_WARNING 656783f6184SRyusuke Konishi "NILFS warning: maximal mount count reached\n"); 657783f6184SRyusuke Konishi #endif 658783f6184SRyusuke Konishi } 659783f6184SRyusuke Konishi if (!max_mnt_count) 660d26493b6SJiro SEKIBA sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT); 661783f6184SRyusuke Konishi 662d26493b6SJiro SEKIBA sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1); 663d26493b6SJiro SEKIBA sbp[0]->s_state = 664d26493b6SJiro SEKIBA cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS); 665d26493b6SJiro SEKIBA sbp[0]->s_mtime = cpu_to_le64(get_seconds()); 666b2ac86e1SJiro SEKIBA /* synchronize sbp[1] with sbp[0] */ 667b2ac86e1SJiro SEKIBA memcpy(sbp[1], sbp[0], nilfs->ns_sbsize); 668b2ac86e1SJiro SEKIBA return nilfs_commit_super(sbi, NILFS_SB_COMMIT_ALL); 669783f6184SRyusuke Konishi } 670783f6184SRyusuke Konishi 671e339ad31SRyusuke Konishi struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb, 672e339ad31SRyusuke Konishi u64 pos, int blocksize, 673e339ad31SRyusuke Konishi struct buffer_head **pbh) 674783f6184SRyusuke Konishi { 675e339ad31SRyusuke Konishi unsigned long long sb_index = pos; 676e339ad31SRyusuke Konishi unsigned long offset; 677783f6184SRyusuke Konishi 678e339ad31SRyusuke Konishi offset = do_div(sb_index, blocksize); 679783f6184SRyusuke Konishi *pbh = sb_bread(sb, sb_index); 680e339ad31SRyusuke Konishi if (!*pbh) 681783f6184SRyusuke Konishi return NULL; 682783f6184SRyusuke Konishi return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset); 683783f6184SRyusuke Konishi } 684783f6184SRyusuke Konishi 685783f6184SRyusuke Konishi int nilfs_store_magic_and_option(struct super_block *sb, 686783f6184SRyusuke Konishi struct nilfs_super_block *sbp, 687783f6184SRyusuke Konishi char *data) 688783f6184SRyusuke Konishi { 689783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 690783f6184SRyusuke Konishi 691783f6184SRyusuke Konishi sb->s_magic = le16_to_cpu(sbp->s_magic); 692783f6184SRyusuke Konishi 693783f6184SRyusuke Konishi /* FS independent flags */ 694783f6184SRyusuke Konishi #ifdef NILFS_ATIME_DISABLE 695783f6184SRyusuke Konishi sb->s_flags |= MS_NOATIME; 696783f6184SRyusuke Konishi #endif 697783f6184SRyusuke Konishi 698783f6184SRyusuke Konishi nilfs_set_default_options(sbi, sbp); 699783f6184SRyusuke Konishi 700783f6184SRyusuke Konishi sbi->s_resuid = le16_to_cpu(sbp->s_def_resuid); 701783f6184SRyusuke Konishi sbi->s_resgid = le16_to_cpu(sbp->s_def_resgid); 702783f6184SRyusuke Konishi sbi->s_interval = le32_to_cpu(sbp->s_c_interval); 703783f6184SRyusuke Konishi sbi->s_watermark = le32_to_cpu(sbp->s_c_block_max); 704783f6184SRyusuke Konishi 7057c017457SRyusuke Konishi return !parse_options(data, sb, 0) ? -EINVAL : 0 ; 706783f6184SRyusuke Konishi } 707783f6184SRyusuke Konishi 708c5ca48aaSRyusuke Konishi int nilfs_check_feature_compatibility(struct super_block *sb, 709c5ca48aaSRyusuke Konishi struct nilfs_super_block *sbp) 710c5ca48aaSRyusuke Konishi { 711c5ca48aaSRyusuke Konishi __u64 features; 712c5ca48aaSRyusuke Konishi 713c5ca48aaSRyusuke Konishi features = le64_to_cpu(sbp->s_feature_incompat) & 714c5ca48aaSRyusuke Konishi ~NILFS_FEATURE_INCOMPAT_SUPP; 715c5ca48aaSRyusuke Konishi if (features) { 716c5ca48aaSRyusuke Konishi printk(KERN_ERR "NILFS: couldn't mount because of unsupported " 717c5ca48aaSRyusuke Konishi "optional features (%llx)\n", 718c5ca48aaSRyusuke Konishi (unsigned long long)features); 719c5ca48aaSRyusuke Konishi return -EINVAL; 720c5ca48aaSRyusuke Konishi } 721c5ca48aaSRyusuke Konishi features = le64_to_cpu(sbp->s_feature_compat_ro) & 722c5ca48aaSRyusuke Konishi ~NILFS_FEATURE_COMPAT_RO_SUPP; 723c5ca48aaSRyusuke Konishi if (!(sb->s_flags & MS_RDONLY) && features) { 724c5ca48aaSRyusuke Konishi printk(KERN_ERR "NILFS: couldn't mount RDWR because of " 725c5ca48aaSRyusuke Konishi "unsupported optional features (%llx)\n", 726c5ca48aaSRyusuke Konishi (unsigned long long)features); 727c5ca48aaSRyusuke Konishi return -EINVAL; 728c5ca48aaSRyusuke Konishi } 729c5ca48aaSRyusuke Konishi return 0; 730c5ca48aaSRyusuke Konishi } 731c5ca48aaSRyusuke Konishi 732367ea334SRyusuke Konishi static int nilfs_get_root_dentry(struct super_block *sb, 733367ea334SRyusuke Konishi struct nilfs_root *root, 734367ea334SRyusuke Konishi struct dentry **root_dentry) 735367ea334SRyusuke Konishi { 736367ea334SRyusuke Konishi struct inode *inode; 737367ea334SRyusuke Konishi struct dentry *dentry; 738367ea334SRyusuke Konishi int ret = 0; 739367ea334SRyusuke Konishi 740367ea334SRyusuke Konishi inode = nilfs_iget(sb, root, NILFS_ROOT_INO); 741367ea334SRyusuke Konishi if (IS_ERR(inode)) { 742367ea334SRyusuke Konishi printk(KERN_ERR "NILFS: get root inode failed\n"); 743367ea334SRyusuke Konishi ret = PTR_ERR(inode); 744367ea334SRyusuke Konishi goto out; 745367ea334SRyusuke Konishi } 746367ea334SRyusuke Konishi if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) { 747367ea334SRyusuke Konishi iput(inode); 748367ea334SRyusuke Konishi printk(KERN_ERR "NILFS: corrupt root inode.\n"); 749367ea334SRyusuke Konishi ret = -EINVAL; 750367ea334SRyusuke Konishi goto out; 751367ea334SRyusuke Konishi } 752367ea334SRyusuke Konishi 753f11459adSRyusuke Konishi if (root->cno == NILFS_CPTREE_CURRENT_CNO) { 754f11459adSRyusuke Konishi dentry = d_find_alias(inode); 755f11459adSRyusuke Konishi if (!dentry) { 756367ea334SRyusuke Konishi dentry = d_alloc_root(inode); 757367ea334SRyusuke Konishi if (!dentry) { 758367ea334SRyusuke Konishi iput(inode); 759367ea334SRyusuke Konishi ret = -ENOMEM; 760f11459adSRyusuke Konishi goto failed_dentry; 761f11459adSRyusuke Konishi } 762f11459adSRyusuke Konishi } else { 763f11459adSRyusuke Konishi iput(inode); 764f11459adSRyusuke Konishi } 765f11459adSRyusuke Konishi } else { 766f11459adSRyusuke Konishi dentry = d_obtain_alias(inode); 767f11459adSRyusuke Konishi if (IS_ERR(dentry)) { 768f11459adSRyusuke Konishi ret = PTR_ERR(dentry); 769f11459adSRyusuke Konishi goto failed_dentry; 770f11459adSRyusuke Konishi } 771367ea334SRyusuke Konishi } 772367ea334SRyusuke Konishi *root_dentry = dentry; 773367ea334SRyusuke Konishi out: 774367ea334SRyusuke Konishi return ret; 775f11459adSRyusuke Konishi 776f11459adSRyusuke Konishi failed_dentry: 777f11459adSRyusuke Konishi printk(KERN_ERR "NILFS: get root dentry failed\n"); 778f11459adSRyusuke Konishi goto out; 779367ea334SRyusuke Konishi } 780367ea334SRyusuke Konishi 781ab4d8f7eSRyusuke Konishi static int nilfs_attach_snapshot(struct super_block *s, __u64 cno, 782ab4d8f7eSRyusuke Konishi struct dentry **root_dentry) 783ab4d8f7eSRyusuke Konishi { 784ab4d8f7eSRyusuke Konishi struct the_nilfs *nilfs = NILFS_SB(s)->s_nilfs; 785ab4d8f7eSRyusuke Konishi struct nilfs_root *root; 786ab4d8f7eSRyusuke Konishi int ret; 787ab4d8f7eSRyusuke Konishi 788ab4d8f7eSRyusuke Konishi down_read(&nilfs->ns_segctor_sem); 789ab4d8f7eSRyusuke Konishi ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno); 790ab4d8f7eSRyusuke Konishi up_read(&nilfs->ns_segctor_sem); 791ab4d8f7eSRyusuke Konishi if (ret < 0) { 792ab4d8f7eSRyusuke Konishi ret = (ret == -ENOENT) ? -EINVAL : ret; 793ab4d8f7eSRyusuke Konishi goto out; 794ab4d8f7eSRyusuke Konishi } else if (!ret) { 795ab4d8f7eSRyusuke Konishi printk(KERN_ERR "NILFS: The specified checkpoint is " 796ab4d8f7eSRyusuke Konishi "not a snapshot (checkpoint number=%llu).\n", 797ab4d8f7eSRyusuke Konishi (unsigned long long)cno); 798ab4d8f7eSRyusuke Konishi ret = -EINVAL; 799ab4d8f7eSRyusuke Konishi goto out; 800ab4d8f7eSRyusuke Konishi } 801ab4d8f7eSRyusuke Konishi 802ab4d8f7eSRyusuke Konishi ret = nilfs_attach_checkpoint(NILFS_SB(s), cno, false, &root); 803ab4d8f7eSRyusuke Konishi if (ret) { 804ab4d8f7eSRyusuke Konishi printk(KERN_ERR "NILFS: error loading snapshot " 805ab4d8f7eSRyusuke Konishi "(checkpoint number=%llu).\n", 806ab4d8f7eSRyusuke Konishi (unsigned long long)cno); 807ab4d8f7eSRyusuke Konishi goto out; 808ab4d8f7eSRyusuke Konishi } 809ab4d8f7eSRyusuke Konishi ret = nilfs_get_root_dentry(s, root, root_dentry); 810ab4d8f7eSRyusuke Konishi nilfs_put_root(root); 811ab4d8f7eSRyusuke Konishi out: 812ab4d8f7eSRyusuke Konishi return ret; 813ab4d8f7eSRyusuke Konishi } 814ab4d8f7eSRyusuke Konishi 815f11459adSRyusuke Konishi static int nilfs_tree_was_touched(struct dentry *root_dentry) 816f11459adSRyusuke Konishi { 817f11459adSRyusuke Konishi return atomic_read(&root_dentry->d_count) > 1; 818f11459adSRyusuke Konishi } 819f11459adSRyusuke Konishi 820f11459adSRyusuke Konishi /** 821f11459adSRyusuke Konishi * nilfs_try_to_shrink_tree() - try to shrink dentries of a checkpoint 822f11459adSRyusuke Konishi * @root_dentry: root dentry of the tree to be shrunk 823f11459adSRyusuke Konishi * 824f11459adSRyusuke Konishi * This function returns true if the tree was in-use. 825f11459adSRyusuke Konishi */ 826f11459adSRyusuke Konishi static int nilfs_try_to_shrink_tree(struct dentry *root_dentry) 827f11459adSRyusuke Konishi { 828f11459adSRyusuke Konishi if (have_submounts(root_dentry)) 829f11459adSRyusuke Konishi return true; 830f11459adSRyusuke Konishi shrink_dcache_parent(root_dentry); 831f11459adSRyusuke Konishi return nilfs_tree_was_touched(root_dentry); 832f11459adSRyusuke Konishi } 833f11459adSRyusuke Konishi 834783f6184SRyusuke Konishi /** 835783f6184SRyusuke Konishi * nilfs_fill_super() - initialize a super block instance 836783f6184SRyusuke Konishi * @sb: super_block 837783f6184SRyusuke Konishi * @data: mount options 838783f6184SRyusuke Konishi * @silent: silent mode flag 839783f6184SRyusuke Konishi * 840aa7dfb89SRyusuke Konishi * This function is called exclusively by nilfs->ns_mount_mutex. 841783f6184SRyusuke Konishi * So, the recovery process is protected from other simultaneous mounts. 842783f6184SRyusuke Konishi */ 843783f6184SRyusuke Konishi static int 844*348fe8daSRyusuke Konishi nilfs_fill_super(struct super_block *sb, void *data, int silent) 845783f6184SRyusuke Konishi { 846*348fe8daSRyusuke Konishi struct the_nilfs *nilfs; 847783f6184SRyusuke Konishi struct nilfs_sb_info *sbi; 8484d8d9293SRyusuke Konishi struct nilfs_root *fsroot; 849783f6184SRyusuke Konishi __u64 cno; 850ab4d8f7eSRyusuke Konishi int err; 851783f6184SRyusuke Konishi 852783f6184SRyusuke Konishi sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); 853783f6184SRyusuke Konishi if (!sbi) 854783f6184SRyusuke Konishi return -ENOMEM; 855783f6184SRyusuke Konishi 856783f6184SRyusuke Konishi sb->s_fs_info = sbi; 857783f6184SRyusuke Konishi sbi->s_super = sb; 858783f6184SRyusuke Konishi 859*348fe8daSRyusuke Konishi nilfs = alloc_nilfs(sb->s_bdev); 860*348fe8daSRyusuke Konishi if (!nilfs) { 861*348fe8daSRyusuke Konishi err = -ENOMEM; 862*348fe8daSRyusuke Konishi goto failed_sbi; 863*348fe8daSRyusuke Konishi } 864*348fe8daSRyusuke Konishi sbi->s_nilfs = nilfs; 865*348fe8daSRyusuke Konishi 866783f6184SRyusuke Konishi err = init_nilfs(nilfs, sbi, (char *)data); 867783f6184SRyusuke Konishi if (err) 868*348fe8daSRyusuke Konishi goto failed_nilfs; 869783f6184SRyusuke Konishi 870783f6184SRyusuke Konishi spin_lock_init(&sbi->s_inode_lock); 871783f6184SRyusuke Konishi INIT_LIST_HEAD(&sbi->s_dirty_files); 872783f6184SRyusuke Konishi 873783f6184SRyusuke Konishi /* 874783f6184SRyusuke Konishi * Following initialization is overlapped because 875783f6184SRyusuke Konishi * nilfs_sb_info structure has been cleared at the beginning. 876783f6184SRyusuke Konishi * But we reserve them to keep our interest and make ready 877783f6184SRyusuke Konishi * for the future change. 878783f6184SRyusuke Konishi */ 879783f6184SRyusuke Konishi get_random_bytes(&sbi->s_next_generation, 880783f6184SRyusuke Konishi sizeof(sbi->s_next_generation)); 881783f6184SRyusuke Konishi spin_lock_init(&sbi->s_next_gen_lock); 882783f6184SRyusuke Konishi 883783f6184SRyusuke Konishi sb->s_op = &nilfs_sops; 884783f6184SRyusuke Konishi sb->s_export_op = &nilfs_export_ops; 885783f6184SRyusuke Konishi sb->s_root = NULL; 88661239230SRyusuke Konishi sb->s_time_gran = 1; 887973bec34SRyusuke Konishi sb->s_bdi = nilfs->ns_bdi; 888783f6184SRyusuke Konishi 889783f6184SRyusuke Konishi err = load_nilfs(nilfs, sbi); 890783f6184SRyusuke Konishi if (err) 891*348fe8daSRyusuke Konishi goto failed_nilfs; 892f50a4c81SRyusuke Konishi 893ab4d8f7eSRyusuke Konishi cno = nilfs_last_cno(nilfs); 894ab4d8f7eSRyusuke Konishi err = nilfs_attach_checkpoint(sbi, cno, true, &fsroot); 895783f6184SRyusuke Konishi if (err) { 896f11459adSRyusuke Konishi printk(KERN_ERR "NILFS: error loading last checkpoint " 897783f6184SRyusuke Konishi "(checkpoint number=%llu).\n", (unsigned long long)cno); 898*348fe8daSRyusuke Konishi goto failed_nilfs; 899783f6184SRyusuke Konishi } 900783f6184SRyusuke Konishi 901783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 902e912a5b6SRyusuke Konishi err = nilfs_attach_segment_constructor(sbi, fsroot); 903783f6184SRyusuke Konishi if (err) 904783f6184SRyusuke Konishi goto failed_checkpoint; 905783f6184SRyusuke Konishi } 906783f6184SRyusuke Konishi 907367ea334SRyusuke Konishi err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root); 908367ea334SRyusuke Konishi if (err) 909783f6184SRyusuke Konishi goto failed_segctor; 910783f6184SRyusuke Konishi 9114d8d9293SRyusuke Konishi nilfs_put_root(fsroot); 9124d8d9293SRyusuke Konishi 913783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 914783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 915783f6184SRyusuke Konishi nilfs_setup_super(sbi); 916783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 917783f6184SRyusuke Konishi } 918783f6184SRyusuke Konishi 919783f6184SRyusuke Konishi return 0; 920783f6184SRyusuke Konishi 921783f6184SRyusuke Konishi failed_segctor: 922783f6184SRyusuke Konishi nilfs_detach_segment_constructor(sbi); 923783f6184SRyusuke Konishi 924783f6184SRyusuke Konishi failed_checkpoint: 9254d8d9293SRyusuke Konishi nilfs_put_root(fsroot); 926783f6184SRyusuke Konishi 927*348fe8daSRyusuke Konishi failed_nilfs: 928*348fe8daSRyusuke Konishi destroy_nilfs(nilfs); 929*348fe8daSRyusuke Konishi 930783f6184SRyusuke Konishi failed_sbi: 931783f6184SRyusuke Konishi sb->s_fs_info = NULL; 932f11459adSRyusuke Konishi kfree(sbi); 933783f6184SRyusuke Konishi return err; 934783f6184SRyusuke Konishi } 935783f6184SRyusuke Konishi 936783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data) 937783f6184SRyusuke Konishi { 938783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 939783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 940783f6184SRyusuke Konishi unsigned long old_sb_flags; 941783f6184SRyusuke Konishi struct nilfs_mount_options old_opts; 942f11459adSRyusuke Konishi int err; 943783f6184SRyusuke Konishi 944783f6184SRyusuke Konishi old_sb_flags = sb->s_flags; 945783f6184SRyusuke Konishi old_opts.mount_opt = sbi->s_mount_opt; 946783f6184SRyusuke Konishi 9477c017457SRyusuke Konishi if (!parse_options(data, sb, 1)) { 948783f6184SRyusuke Konishi err = -EINVAL; 949783f6184SRyusuke Konishi goto restore_opts; 950783f6184SRyusuke Konishi } 951783f6184SRyusuke Konishi sb->s_flags = (sb->s_flags & ~MS_POSIXACL); 952783f6184SRyusuke Konishi 953d240e067SRyusuke Konishi err = -EINVAL; 954783f6184SRyusuke Konishi 9550234576dSRyusuke Konishi if (!nilfs_valid_fs(nilfs)) { 9560234576dSRyusuke Konishi printk(KERN_WARNING "NILFS (device %s): couldn't " 9570234576dSRyusuke Konishi "remount because the filesystem is in an " 9580234576dSRyusuke Konishi "incomplete recovery state.\n", sb->s_id); 9590234576dSRyusuke Konishi goto restore_opts; 9600234576dSRyusuke Konishi } 9610234576dSRyusuke Konishi 962783f6184SRyusuke Konishi if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 963783f6184SRyusuke Konishi goto out; 964783f6184SRyusuke Konishi if (*flags & MS_RDONLY) { 965783f6184SRyusuke Konishi /* Shutting down the segment constructor */ 966783f6184SRyusuke Konishi nilfs_detach_segment_constructor(sbi); 967783f6184SRyusuke Konishi sb->s_flags |= MS_RDONLY; 968783f6184SRyusuke Konishi 969783f6184SRyusuke Konishi /* 970783f6184SRyusuke Konishi * Remounting a valid RW partition RDONLY, so set 971783f6184SRyusuke Konishi * the RDONLY flag and then mark the partition as valid again. 972783f6184SRyusuke Konishi */ 973783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 9747ecaa46cSRyusuke Konishi nilfs_cleanup_super(sbi); 975783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 976783f6184SRyusuke Konishi } else { 977c5ca48aaSRyusuke Konishi __u64 features; 978e912a5b6SRyusuke Konishi struct nilfs_root *root; 979c5ca48aaSRyusuke Konishi 980783f6184SRyusuke Konishi /* 981783f6184SRyusuke Konishi * Mounting a RDONLY partition read-write, so reread and 982783f6184SRyusuke Konishi * store the current valid flag. (It may have been changed 983783f6184SRyusuke Konishi * by fsck since we originally mounted the partition.) 984783f6184SRyusuke Konishi */ 985c5ca48aaSRyusuke Konishi down_read(&nilfs->ns_sem); 986c5ca48aaSRyusuke Konishi features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) & 987c5ca48aaSRyusuke Konishi ~NILFS_FEATURE_COMPAT_RO_SUPP; 988c5ca48aaSRyusuke Konishi up_read(&nilfs->ns_sem); 989c5ca48aaSRyusuke Konishi if (features) { 990c5ca48aaSRyusuke Konishi printk(KERN_WARNING "NILFS (device %s): couldn't " 991c5ca48aaSRyusuke Konishi "remount RDWR because of unsupported optional " 992c5ca48aaSRyusuke Konishi "features (%llx)\n", 993c5ca48aaSRyusuke Konishi sb->s_id, (unsigned long long)features); 994c5ca48aaSRyusuke Konishi err = -EROFS; 995c5ca48aaSRyusuke Konishi goto restore_opts; 996c5ca48aaSRyusuke Konishi } 997c5ca48aaSRyusuke Konishi 998783f6184SRyusuke Konishi sb->s_flags &= ~MS_RDONLY; 999783f6184SRyusuke Konishi 1000e912a5b6SRyusuke Konishi root = NILFS_I(sb->s_root->d_inode)->i_root; 1001e912a5b6SRyusuke Konishi err = nilfs_attach_segment_constructor(sbi, root); 1002783f6184SRyusuke Konishi if (err) 1003e59399d0SRyusuke Konishi goto restore_opts; 1004783f6184SRyusuke Konishi 1005783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 1006783f6184SRyusuke Konishi nilfs_setup_super(sbi); 1007783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 1008783f6184SRyusuke Konishi } 1009783f6184SRyusuke Konishi out: 1010783f6184SRyusuke Konishi return 0; 1011783f6184SRyusuke Konishi 1012783f6184SRyusuke Konishi restore_opts: 1013783f6184SRyusuke Konishi sb->s_flags = old_sb_flags; 1014783f6184SRyusuke Konishi sbi->s_mount_opt = old_opts.mount_opt; 1015783f6184SRyusuke Konishi return err; 1016783f6184SRyusuke Konishi } 1017783f6184SRyusuke Konishi 1018783f6184SRyusuke Konishi struct nilfs_super_data { 1019783f6184SRyusuke Konishi struct block_device *bdev; 10206dd47406SRyusuke Konishi struct nilfs_sb_info *sbi; 1021783f6184SRyusuke Konishi __u64 cno; 1022783f6184SRyusuke Konishi int flags; 1023783f6184SRyusuke Konishi }; 1024783f6184SRyusuke Konishi 1025783f6184SRyusuke Konishi /** 1026783f6184SRyusuke Konishi * nilfs_identify - pre-read mount options needed to identify mount instance 1027783f6184SRyusuke Konishi * @data: mount options 1028783f6184SRyusuke Konishi * @sd: nilfs_super_data 1029783f6184SRyusuke Konishi */ 1030783f6184SRyusuke Konishi static int nilfs_identify(char *data, struct nilfs_super_data *sd) 1031783f6184SRyusuke Konishi { 1032783f6184SRyusuke Konishi char *p, *options = data; 1033783f6184SRyusuke Konishi substring_t args[MAX_OPT_ARGS]; 1034783f6184SRyusuke Konishi int option, token; 1035783f6184SRyusuke Konishi int ret = 0; 1036783f6184SRyusuke Konishi 1037783f6184SRyusuke Konishi do { 1038783f6184SRyusuke Konishi p = strsep(&options, ","); 1039783f6184SRyusuke Konishi if (p != NULL && *p) { 1040783f6184SRyusuke Konishi token = match_token(p, tokens, args); 1041783f6184SRyusuke Konishi if (token == Opt_snapshot) { 1042783f6184SRyusuke Konishi if (!(sd->flags & MS_RDONLY)) 1043783f6184SRyusuke Konishi ret++; 1044783f6184SRyusuke Konishi else { 1045783f6184SRyusuke Konishi ret = match_int(&args[0], &option); 1046783f6184SRyusuke Konishi if (!ret) { 1047783f6184SRyusuke Konishi if (option > 0) 1048783f6184SRyusuke Konishi sd->cno = option; 1049783f6184SRyusuke Konishi else 1050783f6184SRyusuke Konishi ret++; 1051783f6184SRyusuke Konishi } 1052783f6184SRyusuke Konishi } 1053783f6184SRyusuke Konishi } 1054783f6184SRyusuke Konishi if (ret) 1055783f6184SRyusuke Konishi printk(KERN_ERR 1056783f6184SRyusuke Konishi "NILFS: invalid mount option: %s\n", p); 1057783f6184SRyusuke Konishi } 1058783f6184SRyusuke Konishi if (!options) 1059783f6184SRyusuke Konishi break; 1060783f6184SRyusuke Konishi BUG_ON(options == data); 1061783f6184SRyusuke Konishi *(options - 1) = ','; 1062783f6184SRyusuke Konishi } while (!ret); 1063783f6184SRyusuke Konishi return ret; 1064783f6184SRyusuke Konishi } 1065783f6184SRyusuke Konishi 1066783f6184SRyusuke Konishi static int nilfs_set_bdev_super(struct super_block *s, void *data) 1067783f6184SRyusuke Konishi { 1068f11459adSRyusuke Konishi s->s_bdev = data; 1069783f6184SRyusuke Konishi s->s_dev = s->s_bdev->bd_dev; 1070783f6184SRyusuke Konishi return 0; 1071783f6184SRyusuke Konishi } 1072783f6184SRyusuke Konishi 1073783f6184SRyusuke Konishi static int nilfs_test_bdev_super(struct super_block *s, void *data) 1074783f6184SRyusuke Konishi { 1075f11459adSRyusuke Konishi return (void *)s->s_bdev == data; 1076783f6184SRyusuke Konishi } 1077783f6184SRyusuke Konishi 1078783f6184SRyusuke Konishi static int 1079783f6184SRyusuke Konishi nilfs_get_sb(struct file_system_type *fs_type, int flags, 1080783f6184SRyusuke Konishi const char *dev_name, void *data, struct vfsmount *mnt) 1081783f6184SRyusuke Konishi { 1082783f6184SRyusuke Konishi struct nilfs_super_data sd; 108333c8e57cSRyusuke Konishi struct super_block *s; 108413e90559SRyusuke Konishi fmode_t mode = FMODE_READ; 1085f11459adSRyusuke Konishi struct dentry *root_dentry; 1086f11459adSRyusuke Konishi int err, s_new = false; 1087783f6184SRyusuke Konishi 108813e90559SRyusuke Konishi if (!(flags & MS_RDONLY)) 108913e90559SRyusuke Konishi mode |= FMODE_WRITE; 109013e90559SRyusuke Konishi 109113e90559SRyusuke Konishi sd.bdev = open_bdev_exclusive(dev_name, mode, fs_type); 1092d6d4c19cSJan Blunck if (IS_ERR(sd.bdev)) 1093783f6184SRyusuke Konishi return PTR_ERR(sd.bdev); 1094783f6184SRyusuke Konishi 1095783f6184SRyusuke Konishi sd.cno = 0; 1096783f6184SRyusuke Konishi sd.flags = flags; 1097783f6184SRyusuke Konishi if (nilfs_identify((char *)data, &sd)) { 1098783f6184SRyusuke Konishi err = -EINVAL; 1099783f6184SRyusuke Konishi goto failed; 1100783f6184SRyusuke Konishi } 1101783f6184SRyusuke Konishi 1102f11459adSRyusuke Konishi s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, sd.bdev); 110333c8e57cSRyusuke Konishi if (IS_ERR(s)) { 110433c8e57cSRyusuke Konishi err = PTR_ERR(s); 1105*348fe8daSRyusuke Konishi goto failed; 1106783f6184SRyusuke Konishi } 1107783f6184SRyusuke Konishi 1108783f6184SRyusuke Konishi if (!s->s_root) { 1109783f6184SRyusuke Konishi char b[BDEVNAME_SIZE]; 1110783f6184SRyusuke Konishi 1111f11459adSRyusuke Konishi s_new = true; 1112f11459adSRyusuke Konishi 111333c8e57cSRyusuke Konishi /* New superblock instance created */ 1114783f6184SRyusuke Konishi s->s_flags = flags; 11154571b82cSRyusuke Konishi s->s_mode = mode; 1116783f6184SRyusuke Konishi strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id)); 1117783f6184SRyusuke Konishi sb_set_blocksize(s, block_size(sd.bdev)); 1118783f6184SRyusuke Konishi 1119*348fe8daSRyusuke Konishi err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0); 1120783f6184SRyusuke Konishi if (err) 1121*348fe8daSRyusuke Konishi goto failed_super; 1122783f6184SRyusuke Konishi 1123783f6184SRyusuke Konishi s->s_flags |= MS_ACTIVE; 1124f11459adSRyusuke Konishi } else if (!sd.cno) { 1125f11459adSRyusuke Konishi int busy = false; 1126f11459adSRyusuke Konishi 1127f11459adSRyusuke Konishi if (nilfs_tree_was_touched(s->s_root)) { 1128f11459adSRyusuke Konishi busy = nilfs_try_to_shrink_tree(s->s_root); 1129f11459adSRyusuke Konishi if (busy && (flags ^ s->s_flags) & MS_RDONLY) { 1130f11459adSRyusuke Konishi printk(KERN_ERR "NILFS: the device already " 1131f11459adSRyusuke Konishi "has a %s mount.\n", 1132f11459adSRyusuke Konishi (s->s_flags & MS_RDONLY) ? 1133f11459adSRyusuke Konishi "read-only" : "read/write"); 1134f11459adSRyusuke Konishi err = -EBUSY; 1135f11459adSRyusuke Konishi goto failed_super; 1136f11459adSRyusuke Konishi } 1137f11459adSRyusuke Konishi } 1138f11459adSRyusuke Konishi if (!busy) { 1139f11459adSRyusuke Konishi /* 1140f11459adSRyusuke Konishi * Try remount to setup mount states if the current 1141f11459adSRyusuke Konishi * tree is not mounted and only snapshots use this sb. 1142f11459adSRyusuke Konishi */ 1143f11459adSRyusuke Konishi err = nilfs_remount(s, &flags, data); 1144f11459adSRyusuke Konishi if (err) 1145f11459adSRyusuke Konishi goto failed_super; 1146f11459adSRyusuke Konishi } 1147f11459adSRyusuke Konishi } 1148f11459adSRyusuke Konishi 1149f11459adSRyusuke Konishi if (sd.cno) { 1150f11459adSRyusuke Konishi err = nilfs_attach_snapshot(s, sd.cno, &root_dentry); 1151*348fe8daSRyusuke Konishi if (err) 1152f11459adSRyusuke Konishi goto failed_super; 1153f11459adSRyusuke Konishi } else { 1154f11459adSRyusuke Konishi root_dentry = dget(s->s_root); 1155783f6184SRyusuke Konishi } 1156783f6184SRyusuke Konishi 1157f11459adSRyusuke Konishi if (!s_new) 115813e90559SRyusuke Konishi close_bdev_exclusive(sd.bdev, mode); 1159f11459adSRyusuke Konishi 1160f11459adSRyusuke Konishi mnt->mnt_sb = s; 1161f11459adSRyusuke Konishi mnt->mnt_root = root_dentry; 1162783f6184SRyusuke Konishi return 0; 1163783f6184SRyusuke Konishi 1164f11459adSRyusuke Konishi failed_super: 1165f11459adSRyusuke Konishi deactivate_locked_super(s); 1166783f6184SRyusuke Konishi 1167*348fe8daSRyusuke Konishi failed: 1168*348fe8daSRyusuke Konishi if (!s_new) 1169*348fe8daSRyusuke Konishi close_bdev_exclusive(sd.bdev, mode); 1170783f6184SRyusuke Konishi return err; 1171783f6184SRyusuke Konishi } 1172783f6184SRyusuke Konishi 1173783f6184SRyusuke Konishi struct file_system_type nilfs_fs_type = { 1174783f6184SRyusuke Konishi .owner = THIS_MODULE, 1175783f6184SRyusuke Konishi .name = "nilfs2", 1176783f6184SRyusuke Konishi .get_sb = nilfs_get_sb, 1177783f6184SRyusuke Konishi .kill_sb = kill_block_super, 1178783f6184SRyusuke Konishi .fs_flags = FS_REQUIRES_DEV, 1179783f6184SRyusuke Konishi }; 1180783f6184SRyusuke Konishi 118141c88bd7SLi Hong static void nilfs_inode_init_once(void *obj) 118241c88bd7SLi Hong { 118341c88bd7SLi Hong struct nilfs_inode_info *ii = obj; 118441c88bd7SLi Hong 118541c88bd7SLi Hong INIT_LIST_HEAD(&ii->i_dirty); 118641c88bd7SLi Hong #ifdef CONFIG_NILFS_XATTR 118741c88bd7SLi Hong init_rwsem(&ii->xattr_sem); 118841c88bd7SLi Hong #endif 118941c88bd7SLi Hong nilfs_btnode_cache_init_once(&ii->i_btnode_cache); 119005d0e94bSRyusuke Konishi ii->i_bmap = &ii->i_bmap_data; 119141c88bd7SLi Hong inode_init_once(&ii->vfs_inode); 119241c88bd7SLi Hong } 119341c88bd7SLi Hong 119441c88bd7SLi Hong static void nilfs_segbuf_init_once(void *obj) 119541c88bd7SLi Hong { 119641c88bd7SLi Hong memset(obj, 0, sizeof(struct nilfs_segment_buffer)); 119741c88bd7SLi Hong } 119841c88bd7SLi Hong 119941c88bd7SLi Hong static void nilfs_destroy_cachep(void) 120041c88bd7SLi Hong { 120141c88bd7SLi Hong if (nilfs_inode_cachep) 120241c88bd7SLi Hong kmem_cache_destroy(nilfs_inode_cachep); 120341c88bd7SLi Hong if (nilfs_transaction_cachep) 120441c88bd7SLi Hong kmem_cache_destroy(nilfs_transaction_cachep); 120541c88bd7SLi Hong if (nilfs_segbuf_cachep) 120641c88bd7SLi Hong kmem_cache_destroy(nilfs_segbuf_cachep); 120741c88bd7SLi Hong if (nilfs_btree_path_cache) 120841c88bd7SLi Hong kmem_cache_destroy(nilfs_btree_path_cache); 120941c88bd7SLi Hong } 121041c88bd7SLi Hong 121141c88bd7SLi Hong static int __init nilfs_init_cachep(void) 121241c88bd7SLi Hong { 121341c88bd7SLi Hong nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache", 121441c88bd7SLi Hong sizeof(struct nilfs_inode_info), 0, 121541c88bd7SLi Hong SLAB_RECLAIM_ACCOUNT, nilfs_inode_init_once); 121641c88bd7SLi Hong if (!nilfs_inode_cachep) 121741c88bd7SLi Hong goto fail; 121841c88bd7SLi Hong 121941c88bd7SLi Hong nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache", 122041c88bd7SLi Hong sizeof(struct nilfs_transaction_info), 0, 122141c88bd7SLi Hong SLAB_RECLAIM_ACCOUNT, NULL); 122241c88bd7SLi Hong if (!nilfs_transaction_cachep) 122341c88bd7SLi Hong goto fail; 122441c88bd7SLi Hong 122541c88bd7SLi Hong nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache", 122641c88bd7SLi Hong sizeof(struct nilfs_segment_buffer), 0, 122741c88bd7SLi Hong SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once); 122841c88bd7SLi Hong if (!nilfs_segbuf_cachep) 122941c88bd7SLi Hong goto fail; 123041c88bd7SLi Hong 123141c88bd7SLi Hong nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache", 123241c88bd7SLi Hong sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX, 123341c88bd7SLi Hong 0, 0, NULL); 123441c88bd7SLi Hong if (!nilfs_btree_path_cache) 123541c88bd7SLi Hong goto fail; 123641c88bd7SLi Hong 123741c88bd7SLi Hong return 0; 123841c88bd7SLi Hong 123941c88bd7SLi Hong fail: 124041c88bd7SLi Hong nilfs_destroy_cachep(); 124141c88bd7SLi Hong return -ENOMEM; 124241c88bd7SLi Hong } 124341c88bd7SLi Hong 1244783f6184SRyusuke Konishi static int __init init_nilfs_fs(void) 1245783f6184SRyusuke Konishi { 1246783f6184SRyusuke Konishi int err; 1247783f6184SRyusuke Konishi 124841c88bd7SLi Hong err = nilfs_init_cachep(); 1249783f6184SRyusuke Konishi if (err) 125041c88bd7SLi Hong goto fail; 1251783f6184SRyusuke Konishi 1252783f6184SRyusuke Konishi err = register_filesystem(&nilfs_fs_type); 1253783f6184SRyusuke Konishi if (err) 125441c88bd7SLi Hong goto free_cachep; 1255783f6184SRyusuke Konishi 12569f130263SLi Hong printk(KERN_INFO "NILFS version 2 loaded\n"); 1257783f6184SRyusuke Konishi return 0; 1258783f6184SRyusuke Konishi 125941c88bd7SLi Hong free_cachep: 126041c88bd7SLi Hong nilfs_destroy_cachep(); 126141c88bd7SLi Hong fail: 1262783f6184SRyusuke Konishi return err; 1263783f6184SRyusuke Konishi } 1264783f6184SRyusuke Konishi 1265783f6184SRyusuke Konishi static void __exit exit_nilfs_fs(void) 1266783f6184SRyusuke Konishi { 126741c88bd7SLi Hong nilfs_destroy_cachep(); 1268783f6184SRyusuke Konishi unregister_filesystem(&nilfs_fs_type); 1269783f6184SRyusuke Konishi } 1270783f6184SRyusuke Konishi 1271783f6184SRyusuke Konishi module_init(init_nilfs_fs) 1272783f6184SRyusuke Konishi module_exit(exit_nilfs_fs) 1273