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> 51783f6184SRyusuke Konishi #include <linux/exportfs.h> 52b58a285bSJiro SEKIBA #include <linux/seq_file.h> 53b58a285bSJiro SEKIBA #include <linux/mount.h> 54783f6184SRyusuke Konishi #include "nilfs.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; 158*0e14a359SRyusuke 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 } 358e59399d0SRyusuke Konishi down_write(&nilfs->ns_super_sem); 3593f82ff55SRyusuke Konishi if (nilfs->ns_current == sbi) 3603f82ff55SRyusuke Konishi nilfs->ns_current = NULL; 361e59399d0SRyusuke Konishi up_write(&nilfs->ns_super_sem); 362783f6184SRyusuke Konishi 363783f6184SRyusuke Konishi nilfs_detach_checkpoint(sbi); 364783f6184SRyusuke Konishi put_nilfs(sbi->s_nilfs); 365783f6184SRyusuke Konishi sbi->s_super = NULL; 366783f6184SRyusuke Konishi sb->s_fs_info = NULL; 3676dd47406SRyusuke Konishi nilfs_put_sbinfo(sbi); 368783f6184SRyusuke Konishi } 369783f6184SRyusuke Konishi 370783f6184SRyusuke Konishi static int nilfs_sync_fs(struct super_block *sb, int wait) 371783f6184SRyusuke Konishi { 3726233caa9SJiro SEKIBA struct nilfs_sb_info *sbi = NILFS_SB(sb); 3736233caa9SJiro SEKIBA struct the_nilfs *nilfs = sbi->s_nilfs; 374d26493b6SJiro SEKIBA struct nilfs_super_block **sbp; 375783f6184SRyusuke Konishi int err = 0; 376783f6184SRyusuke Konishi 377783f6184SRyusuke Konishi /* This function is called when super block should be written back */ 378783f6184SRyusuke Konishi if (wait) 379783f6184SRyusuke Konishi err = nilfs_construct_segment(sb); 3806233caa9SJiro SEKIBA 3816233caa9SJiro SEKIBA down_write(&nilfs->ns_sem); 382d26493b6SJiro SEKIBA if (nilfs_sb_dirty(nilfs)) { 383b2ac86e1SJiro SEKIBA sbp = nilfs_prepare_super(sbi, nilfs_sb_will_flip(nilfs)); 384b2ac86e1SJiro SEKIBA if (likely(sbp)) { 385b2ac86e1SJiro SEKIBA nilfs_set_log_cursor(sbp[0], nilfs); 386b2ac86e1SJiro SEKIBA nilfs_commit_super(sbi, NILFS_SB_COMMIT); 387b2ac86e1SJiro SEKIBA } 388d26493b6SJiro SEKIBA } 3896233caa9SJiro SEKIBA up_write(&nilfs->ns_sem); 3906233caa9SJiro SEKIBA 391783f6184SRyusuke Konishi return err; 392783f6184SRyusuke Konishi } 393783f6184SRyusuke Konishi 394783f6184SRyusuke Konishi int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno) 395783f6184SRyusuke Konishi { 396783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 397783f6184SRyusuke Konishi struct nilfs_checkpoint *raw_cp; 398783f6184SRyusuke Konishi struct buffer_head *bh_cp; 399783f6184SRyusuke Konishi int err; 400783f6184SRyusuke Konishi 401e59399d0SRyusuke Konishi down_write(&nilfs->ns_super_sem); 402783f6184SRyusuke Konishi list_add(&sbi->s_list, &nilfs->ns_supers); 403e59399d0SRyusuke Konishi up_write(&nilfs->ns_super_sem); 404783f6184SRyusuke Konishi 405af4e3631SRyusuke Konishi err = -ENOMEM; 40679739565SRyusuke Konishi sbi->s_ifile = nilfs_ifile_new(sbi, nilfs->ns_inode_size); 407783f6184SRyusuke Konishi if (!sbi->s_ifile) 408af4e3631SRyusuke Konishi goto delist; 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 } 424783f6184SRyusuke Konishi err = nilfs_read_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode); 425783f6184SRyusuke Konishi if (unlikely(err)) 426783f6184SRyusuke Konishi goto failed_bh; 427783f6184SRyusuke Konishi atomic_set(&sbi->s_inodes_count, le64_to_cpu(raw_cp->cp_inodes_count)); 428783f6184SRyusuke Konishi atomic_set(&sbi->s_blocks_count, le64_to_cpu(raw_cp->cp_blocks_count)); 429783f6184SRyusuke Konishi 430783f6184SRyusuke Konishi nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp); 431783f6184SRyusuke Konishi return 0; 432783f6184SRyusuke Konishi 433783f6184SRyusuke Konishi failed_bh: 434783f6184SRyusuke Konishi nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp); 435783f6184SRyusuke Konishi failed: 436783f6184SRyusuke Konishi nilfs_mdt_destroy(sbi->s_ifile); 437783f6184SRyusuke Konishi sbi->s_ifile = NULL; 438783f6184SRyusuke Konishi 439af4e3631SRyusuke Konishi delist: 440e59399d0SRyusuke Konishi down_write(&nilfs->ns_super_sem); 441783f6184SRyusuke Konishi list_del_init(&sbi->s_list); 442e59399d0SRyusuke Konishi up_write(&nilfs->ns_super_sem); 443783f6184SRyusuke Konishi 444783f6184SRyusuke Konishi return err; 445783f6184SRyusuke Konishi } 446783f6184SRyusuke Konishi 447783f6184SRyusuke Konishi void nilfs_detach_checkpoint(struct nilfs_sb_info *sbi) 448783f6184SRyusuke Konishi { 449783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 450783f6184SRyusuke Konishi 451783f6184SRyusuke Konishi nilfs_mdt_destroy(sbi->s_ifile); 452783f6184SRyusuke Konishi sbi->s_ifile = NULL; 453e59399d0SRyusuke Konishi down_write(&nilfs->ns_super_sem); 454783f6184SRyusuke Konishi list_del_init(&sbi->s_list); 455e59399d0SRyusuke Konishi up_write(&nilfs->ns_super_sem); 456783f6184SRyusuke Konishi } 457783f6184SRyusuke Konishi 458783f6184SRyusuke Konishi static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf) 459783f6184SRyusuke Konishi { 460783f6184SRyusuke Konishi struct super_block *sb = dentry->d_sb; 461783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 462c306af23SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 463c306af23SRyusuke Konishi u64 id = huge_encode_dev(sb->s_bdev->bd_dev); 464783f6184SRyusuke Konishi unsigned long long blocks; 465783f6184SRyusuke Konishi unsigned long overhead; 466783f6184SRyusuke Konishi unsigned long nrsvblocks; 467783f6184SRyusuke Konishi sector_t nfreeblocks; 468783f6184SRyusuke Konishi int err; 469783f6184SRyusuke Konishi 470783f6184SRyusuke Konishi /* 471783f6184SRyusuke Konishi * Compute all of the segment blocks 472783f6184SRyusuke Konishi * 473783f6184SRyusuke Konishi * The blocks before first segment and after last segment 474783f6184SRyusuke Konishi * are excluded. 475783f6184SRyusuke Konishi */ 476783f6184SRyusuke Konishi blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments 477783f6184SRyusuke Konishi - nilfs->ns_first_data_block; 478783f6184SRyusuke Konishi nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment; 479783f6184SRyusuke Konishi 480783f6184SRyusuke Konishi /* 481783f6184SRyusuke Konishi * Compute the overhead 482783f6184SRyusuke Konishi * 4837a65004bSRyusuke Konishi * When distributing meta data blocks outside segment structure, 484783f6184SRyusuke Konishi * We must count them as the overhead. 485783f6184SRyusuke Konishi */ 486783f6184SRyusuke Konishi overhead = 0; 487783f6184SRyusuke Konishi 488783f6184SRyusuke Konishi err = nilfs_count_free_blocks(nilfs, &nfreeblocks); 489783f6184SRyusuke Konishi if (unlikely(err)) 490783f6184SRyusuke Konishi return err; 491783f6184SRyusuke Konishi 492783f6184SRyusuke Konishi buf->f_type = NILFS_SUPER_MAGIC; 493783f6184SRyusuke Konishi buf->f_bsize = sb->s_blocksize; 494783f6184SRyusuke Konishi buf->f_blocks = blocks - overhead; 495783f6184SRyusuke Konishi buf->f_bfree = nfreeblocks; 496783f6184SRyusuke Konishi buf->f_bavail = (buf->f_bfree >= nrsvblocks) ? 497783f6184SRyusuke Konishi (buf->f_bfree - nrsvblocks) : 0; 498783f6184SRyusuke Konishi buf->f_files = atomic_read(&sbi->s_inodes_count); 499783f6184SRyusuke Konishi buf->f_ffree = 0; /* nilfs_count_free_inodes(sb); */ 500783f6184SRyusuke Konishi buf->f_namelen = NILFS_NAME_LEN; 501c306af23SRyusuke Konishi buf->f_fsid.val[0] = (u32)id; 502c306af23SRyusuke Konishi buf->f_fsid.val[1] = (u32)(id >> 32); 503c306af23SRyusuke Konishi 504783f6184SRyusuke Konishi return 0; 505783f6184SRyusuke Konishi } 506783f6184SRyusuke Konishi 507b58a285bSJiro SEKIBA static int nilfs_show_options(struct seq_file *seq, struct vfsmount *vfs) 508b58a285bSJiro SEKIBA { 509b58a285bSJiro SEKIBA struct super_block *sb = vfs->mnt_sb; 510b58a285bSJiro SEKIBA struct nilfs_sb_info *sbi = NILFS_SB(sb); 511b58a285bSJiro SEKIBA 512b58a285bSJiro SEKIBA if (!nilfs_test_opt(sbi, BARRIER)) 513c6b4d57dSRyusuke Konishi seq_puts(seq, ",nobarrier"); 514b58a285bSJiro SEKIBA if (nilfs_test_opt(sbi, SNAPSHOT)) 515b58a285bSJiro SEKIBA seq_printf(seq, ",cp=%llu", 516b58a285bSJiro SEKIBA (unsigned long long int)sbi->s_snapshot_cno); 517b58a285bSJiro SEKIBA if (nilfs_test_opt(sbi, ERRORS_PANIC)) 518c6b4d57dSRyusuke Konishi seq_puts(seq, ",errors=panic"); 519277a6a34SRyusuke Konishi if (nilfs_test_opt(sbi, ERRORS_CONT)) 520c6b4d57dSRyusuke Konishi seq_puts(seq, ",errors=continue"); 521b58a285bSJiro SEKIBA if (nilfs_test_opt(sbi, STRICT_ORDER)) 522c6b4d57dSRyusuke Konishi seq_puts(seq, ",order=strict"); 5230234576dSRyusuke Konishi if (nilfs_test_opt(sbi, NORECOVERY)) 524c6b4d57dSRyusuke Konishi seq_puts(seq, ",norecovery"); 525e902ec99SJiro SEKIBA if (nilfs_test_opt(sbi, DISCARD)) 526c6b4d57dSRyusuke Konishi seq_puts(seq, ",discard"); 527b58a285bSJiro SEKIBA 528b58a285bSJiro SEKIBA return 0; 529b58a285bSJiro SEKIBA } 530b58a285bSJiro SEKIBA 531b87221deSAlexey Dobriyan static const struct super_operations nilfs_sops = { 532783f6184SRyusuke Konishi .alloc_inode = nilfs_alloc_inode, 533783f6184SRyusuke Konishi .destroy_inode = nilfs_destroy_inode, 534783f6184SRyusuke Konishi .dirty_inode = nilfs_dirty_inode, 535783f6184SRyusuke Konishi /* .write_inode = nilfs_write_inode, */ 536783f6184SRyusuke Konishi /* .put_inode = nilfs_put_inode, */ 537783f6184SRyusuke Konishi /* .drop_inode = nilfs_drop_inode, */ 5386fd1e5c9SAl Viro .evict_inode = nilfs_evict_inode, 539783f6184SRyusuke Konishi .put_super = nilfs_put_super, 5401dfa2710SJiro SEKIBA /* .write_super = nilfs_write_super, */ 541783f6184SRyusuke Konishi .sync_fs = nilfs_sync_fs, 542783f6184SRyusuke Konishi /* .write_super_lockfs */ 543783f6184SRyusuke Konishi /* .unlockfs */ 544783f6184SRyusuke Konishi .statfs = nilfs_statfs, 545783f6184SRyusuke Konishi .remount_fs = nilfs_remount, 546783f6184SRyusuke Konishi /* .umount_begin */ 547b58a285bSJiro SEKIBA .show_options = nilfs_show_options 548783f6184SRyusuke Konishi }; 549783f6184SRyusuke Konishi 550783f6184SRyusuke Konishi static struct inode * 551783f6184SRyusuke Konishi nilfs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation) 552783f6184SRyusuke Konishi { 553783f6184SRyusuke Konishi struct inode *inode; 554783f6184SRyusuke Konishi 555783f6184SRyusuke Konishi if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO && 556783f6184SRyusuke Konishi ino != NILFS_SKETCH_INO) 557783f6184SRyusuke Konishi return ERR_PTR(-ESTALE); 558783f6184SRyusuke Konishi 559783f6184SRyusuke Konishi inode = nilfs_iget(sb, ino); 560783f6184SRyusuke Konishi if (IS_ERR(inode)) 561783f6184SRyusuke Konishi return ERR_CAST(inode); 562783f6184SRyusuke Konishi if (generation && inode->i_generation != generation) { 563783f6184SRyusuke Konishi iput(inode); 564783f6184SRyusuke Konishi return ERR_PTR(-ESTALE); 565783f6184SRyusuke Konishi } 566783f6184SRyusuke Konishi 567783f6184SRyusuke Konishi return inode; 568783f6184SRyusuke Konishi } 569783f6184SRyusuke Konishi 570783f6184SRyusuke Konishi static struct dentry * 571783f6184SRyusuke Konishi nilfs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, 572783f6184SRyusuke Konishi int fh_type) 573783f6184SRyusuke Konishi { 574783f6184SRyusuke Konishi return generic_fh_to_dentry(sb, fid, fh_len, fh_type, 575783f6184SRyusuke Konishi nilfs_nfs_get_inode); 576783f6184SRyusuke Konishi } 577783f6184SRyusuke Konishi 578783f6184SRyusuke Konishi static struct dentry * 579783f6184SRyusuke Konishi nilfs_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, 580783f6184SRyusuke Konishi int fh_type) 581783f6184SRyusuke Konishi { 582783f6184SRyusuke Konishi return generic_fh_to_parent(sb, fid, fh_len, fh_type, 583783f6184SRyusuke Konishi nilfs_nfs_get_inode); 584783f6184SRyusuke Konishi } 585783f6184SRyusuke Konishi 586ac4cfdd6SAlexey Dobriyan static const struct export_operations nilfs_export_ops = { 587783f6184SRyusuke Konishi .fh_to_dentry = nilfs_fh_to_dentry, 588783f6184SRyusuke Konishi .fh_to_parent = nilfs_fh_to_parent, 589783f6184SRyusuke Konishi .get_parent = nilfs_get_parent, 590783f6184SRyusuke Konishi }; 591783f6184SRyusuke Konishi 592783f6184SRyusuke Konishi enum { 593783f6184SRyusuke Konishi Opt_err_cont, Opt_err_panic, Opt_err_ro, 594773bc4f3SRyusuke Konishi Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery, 595802d3177SRyusuke Konishi Opt_discard, Opt_nodiscard, Opt_err, 596783f6184SRyusuke Konishi }; 597783f6184SRyusuke Konishi 598783f6184SRyusuke Konishi static match_table_t tokens = { 599783f6184SRyusuke Konishi {Opt_err_cont, "errors=continue"}, 600783f6184SRyusuke Konishi {Opt_err_panic, "errors=panic"}, 601783f6184SRyusuke Konishi {Opt_err_ro, "errors=remount-ro"}, 602773bc4f3SRyusuke Konishi {Opt_barrier, "barrier"}, 60391f1953bSJiro SEKIBA {Opt_nobarrier, "nobarrier"}, 604783f6184SRyusuke Konishi {Opt_snapshot, "cp=%u"}, 605783f6184SRyusuke Konishi {Opt_order, "order=%s"}, 6060234576dSRyusuke Konishi {Opt_norecovery, "norecovery"}, 607e902ec99SJiro SEKIBA {Opt_discard, "discard"}, 608802d3177SRyusuke Konishi {Opt_nodiscard, "nodiscard"}, 609783f6184SRyusuke Konishi {Opt_err, NULL} 610783f6184SRyusuke Konishi }; 611783f6184SRyusuke Konishi 6127c017457SRyusuke Konishi static int parse_options(char *options, struct super_block *sb, int is_remount) 613783f6184SRyusuke Konishi { 614783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 615783f6184SRyusuke Konishi char *p; 616783f6184SRyusuke Konishi substring_t args[MAX_OPT_ARGS]; 617783f6184SRyusuke Konishi int option; 618783f6184SRyusuke Konishi 619783f6184SRyusuke Konishi if (!options) 620783f6184SRyusuke Konishi return 1; 621783f6184SRyusuke Konishi 622783f6184SRyusuke Konishi while ((p = strsep(&options, ",")) != NULL) { 623783f6184SRyusuke Konishi int token; 624783f6184SRyusuke Konishi if (!*p) 625783f6184SRyusuke Konishi continue; 626783f6184SRyusuke Konishi 627783f6184SRyusuke Konishi token = match_token(p, tokens, args); 628783f6184SRyusuke Konishi switch (token) { 629773bc4f3SRyusuke Konishi case Opt_barrier: 630773bc4f3SRyusuke Konishi nilfs_set_opt(sbi, BARRIER); 631773bc4f3SRyusuke Konishi break; 63291f1953bSJiro SEKIBA case Opt_nobarrier: 633783f6184SRyusuke Konishi nilfs_clear_opt(sbi, BARRIER); 634783f6184SRyusuke Konishi break; 635783f6184SRyusuke Konishi case Opt_order: 636783f6184SRyusuke Konishi if (strcmp(args[0].from, "relaxed") == 0) 637783f6184SRyusuke Konishi /* Ordered data semantics */ 638783f6184SRyusuke Konishi nilfs_clear_opt(sbi, STRICT_ORDER); 639783f6184SRyusuke Konishi else if (strcmp(args[0].from, "strict") == 0) 640783f6184SRyusuke Konishi /* Strict in-order semantics */ 641783f6184SRyusuke Konishi nilfs_set_opt(sbi, STRICT_ORDER); 642783f6184SRyusuke Konishi else 643783f6184SRyusuke Konishi return 0; 644783f6184SRyusuke Konishi break; 645783f6184SRyusuke Konishi case Opt_err_panic: 646783f6184SRyusuke Konishi nilfs_write_opt(sbi, ERROR_MODE, ERRORS_PANIC); 647783f6184SRyusuke Konishi break; 648783f6184SRyusuke Konishi case Opt_err_ro: 649783f6184SRyusuke Konishi nilfs_write_opt(sbi, ERROR_MODE, ERRORS_RO); 650783f6184SRyusuke Konishi break; 651783f6184SRyusuke Konishi case Opt_err_cont: 652783f6184SRyusuke Konishi nilfs_write_opt(sbi, ERROR_MODE, ERRORS_CONT); 653783f6184SRyusuke Konishi break; 654783f6184SRyusuke Konishi case Opt_snapshot: 655783f6184SRyusuke Konishi if (match_int(&args[0], &option) || option <= 0) 656783f6184SRyusuke Konishi return 0; 6577c017457SRyusuke Konishi if (is_remount) { 6587c017457SRyusuke Konishi if (!nilfs_test_opt(sbi, SNAPSHOT)) { 6597c017457SRyusuke Konishi printk(KERN_ERR 6607c017457SRyusuke Konishi "NILFS: cannot change regular " 6617c017457SRyusuke Konishi "mount to snapshot.\n"); 662783f6184SRyusuke Konishi return 0; 6637c017457SRyusuke Konishi } else if (option != sbi->s_snapshot_cno) { 6647c017457SRyusuke Konishi printk(KERN_ERR 6657c017457SRyusuke Konishi "NILFS: cannot remount to a " 6667c017457SRyusuke Konishi "different snapshot.\n"); 6677c017457SRyusuke Konishi return 0; 6687c017457SRyusuke Konishi } 6697c017457SRyusuke Konishi break; 6707c017457SRyusuke Konishi } 6717c017457SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 6727c017457SRyusuke Konishi printk(KERN_ERR "NILFS: cannot mount snapshot " 6737c017457SRyusuke Konishi "read/write. A read-only option is " 6747c017457SRyusuke Konishi "required.\n"); 6757c017457SRyusuke Konishi return 0; 6767c017457SRyusuke Konishi } 677783f6184SRyusuke Konishi sbi->s_snapshot_cno = option; 678783f6184SRyusuke Konishi nilfs_set_opt(sbi, SNAPSHOT); 679783f6184SRyusuke Konishi break; 6800234576dSRyusuke Konishi case Opt_norecovery: 6810234576dSRyusuke Konishi nilfs_set_opt(sbi, NORECOVERY); 6820234576dSRyusuke Konishi break; 683e902ec99SJiro SEKIBA case Opt_discard: 684e902ec99SJiro SEKIBA nilfs_set_opt(sbi, DISCARD); 685e902ec99SJiro SEKIBA break; 686802d3177SRyusuke Konishi case Opt_nodiscard: 687802d3177SRyusuke Konishi nilfs_clear_opt(sbi, DISCARD); 688802d3177SRyusuke Konishi break; 689783f6184SRyusuke Konishi default: 690783f6184SRyusuke Konishi printk(KERN_ERR 691783f6184SRyusuke Konishi "NILFS: Unrecognized mount option \"%s\"\n", p); 692783f6184SRyusuke Konishi return 0; 693783f6184SRyusuke Konishi } 694783f6184SRyusuke Konishi } 695783f6184SRyusuke Konishi return 1; 696783f6184SRyusuke Konishi } 697783f6184SRyusuke Konishi 698783f6184SRyusuke Konishi static inline void 699783f6184SRyusuke Konishi nilfs_set_default_options(struct nilfs_sb_info *sbi, 700783f6184SRyusuke Konishi struct nilfs_super_block *sbp) 701783f6184SRyusuke Konishi { 702783f6184SRyusuke Konishi sbi->s_mount_opt = 703277a6a34SRyusuke Konishi NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER; 704783f6184SRyusuke Konishi } 705783f6184SRyusuke Konishi 706783f6184SRyusuke Konishi static int nilfs_setup_super(struct nilfs_sb_info *sbi) 707783f6184SRyusuke Konishi { 708783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 709d26493b6SJiro SEKIBA struct nilfs_super_block **sbp; 710d26493b6SJiro SEKIBA int max_mnt_count; 711d26493b6SJiro SEKIBA int mnt_count; 712783f6184SRyusuke Konishi 713d26493b6SJiro SEKIBA /* nilfs->ns_sem must be locked by the caller. */ 714b2ac86e1SJiro SEKIBA sbp = nilfs_prepare_super(sbi, 0); 715d26493b6SJiro SEKIBA if (!sbp) 716d26493b6SJiro SEKIBA return -EIO; 717d26493b6SJiro SEKIBA 718d26493b6SJiro SEKIBA max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count); 719d26493b6SJiro SEKIBA mnt_count = le16_to_cpu(sbp[0]->s_mnt_count); 720d26493b6SJiro SEKIBA 721f50a4c81SRyusuke Konishi if (nilfs->ns_mount_state & NILFS_ERROR_FS) { 722783f6184SRyusuke Konishi printk(KERN_WARNING 723783f6184SRyusuke Konishi "NILFS warning: mounting fs with errors\n"); 724783f6184SRyusuke Konishi #if 0 725783f6184SRyusuke Konishi } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) { 726783f6184SRyusuke Konishi printk(KERN_WARNING 727783f6184SRyusuke Konishi "NILFS warning: maximal mount count reached\n"); 728783f6184SRyusuke Konishi #endif 729783f6184SRyusuke Konishi } 730783f6184SRyusuke Konishi if (!max_mnt_count) 731d26493b6SJiro SEKIBA sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT); 732783f6184SRyusuke Konishi 733d26493b6SJiro SEKIBA sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1); 734d26493b6SJiro SEKIBA sbp[0]->s_state = 735d26493b6SJiro SEKIBA cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS); 736d26493b6SJiro SEKIBA sbp[0]->s_mtime = cpu_to_le64(get_seconds()); 737b2ac86e1SJiro SEKIBA /* synchronize sbp[1] with sbp[0] */ 738b2ac86e1SJiro SEKIBA memcpy(sbp[1], sbp[0], nilfs->ns_sbsize); 739b2ac86e1SJiro SEKIBA return nilfs_commit_super(sbi, NILFS_SB_COMMIT_ALL); 740783f6184SRyusuke Konishi } 741783f6184SRyusuke Konishi 742e339ad31SRyusuke Konishi struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb, 743e339ad31SRyusuke Konishi u64 pos, int blocksize, 744e339ad31SRyusuke Konishi struct buffer_head **pbh) 745783f6184SRyusuke Konishi { 746e339ad31SRyusuke Konishi unsigned long long sb_index = pos; 747e339ad31SRyusuke Konishi unsigned long offset; 748783f6184SRyusuke Konishi 749e339ad31SRyusuke Konishi offset = do_div(sb_index, blocksize); 750783f6184SRyusuke Konishi *pbh = sb_bread(sb, sb_index); 751e339ad31SRyusuke Konishi if (!*pbh) 752783f6184SRyusuke Konishi return NULL; 753783f6184SRyusuke Konishi return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset); 754783f6184SRyusuke Konishi } 755783f6184SRyusuke Konishi 756783f6184SRyusuke Konishi int nilfs_store_magic_and_option(struct super_block *sb, 757783f6184SRyusuke Konishi struct nilfs_super_block *sbp, 758783f6184SRyusuke Konishi char *data) 759783f6184SRyusuke Konishi { 760783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 761783f6184SRyusuke Konishi 762783f6184SRyusuke Konishi sb->s_magic = le16_to_cpu(sbp->s_magic); 763783f6184SRyusuke Konishi 764783f6184SRyusuke Konishi /* FS independent flags */ 765783f6184SRyusuke Konishi #ifdef NILFS_ATIME_DISABLE 766783f6184SRyusuke Konishi sb->s_flags |= MS_NOATIME; 767783f6184SRyusuke Konishi #endif 768783f6184SRyusuke Konishi 769783f6184SRyusuke Konishi nilfs_set_default_options(sbi, sbp); 770783f6184SRyusuke Konishi 771783f6184SRyusuke Konishi sbi->s_resuid = le16_to_cpu(sbp->s_def_resuid); 772783f6184SRyusuke Konishi sbi->s_resgid = le16_to_cpu(sbp->s_def_resgid); 773783f6184SRyusuke Konishi sbi->s_interval = le32_to_cpu(sbp->s_c_interval); 774783f6184SRyusuke Konishi sbi->s_watermark = le32_to_cpu(sbp->s_c_block_max); 775783f6184SRyusuke Konishi 7767c017457SRyusuke Konishi return !parse_options(data, sb, 0) ? -EINVAL : 0 ; 777783f6184SRyusuke Konishi } 778783f6184SRyusuke Konishi 779c5ca48aaSRyusuke Konishi int nilfs_check_feature_compatibility(struct super_block *sb, 780c5ca48aaSRyusuke Konishi struct nilfs_super_block *sbp) 781c5ca48aaSRyusuke Konishi { 782c5ca48aaSRyusuke Konishi __u64 features; 783c5ca48aaSRyusuke Konishi 784c5ca48aaSRyusuke Konishi features = le64_to_cpu(sbp->s_feature_incompat) & 785c5ca48aaSRyusuke Konishi ~NILFS_FEATURE_INCOMPAT_SUPP; 786c5ca48aaSRyusuke Konishi if (features) { 787c5ca48aaSRyusuke Konishi printk(KERN_ERR "NILFS: couldn't mount because of unsupported " 788c5ca48aaSRyusuke Konishi "optional features (%llx)\n", 789c5ca48aaSRyusuke Konishi (unsigned long long)features); 790c5ca48aaSRyusuke Konishi return -EINVAL; 791c5ca48aaSRyusuke Konishi } 792c5ca48aaSRyusuke Konishi features = le64_to_cpu(sbp->s_feature_compat_ro) & 793c5ca48aaSRyusuke Konishi ~NILFS_FEATURE_COMPAT_RO_SUPP; 794c5ca48aaSRyusuke Konishi if (!(sb->s_flags & MS_RDONLY) && features) { 795c5ca48aaSRyusuke Konishi printk(KERN_ERR "NILFS: couldn't mount RDWR because of " 796c5ca48aaSRyusuke Konishi "unsupported optional features (%llx)\n", 797c5ca48aaSRyusuke Konishi (unsigned long long)features); 798c5ca48aaSRyusuke Konishi return -EINVAL; 799c5ca48aaSRyusuke Konishi } 800c5ca48aaSRyusuke Konishi return 0; 801c5ca48aaSRyusuke Konishi } 802c5ca48aaSRyusuke Konishi 803783f6184SRyusuke Konishi /** 804783f6184SRyusuke Konishi * nilfs_fill_super() - initialize a super block instance 805783f6184SRyusuke Konishi * @sb: super_block 806783f6184SRyusuke Konishi * @data: mount options 807783f6184SRyusuke Konishi * @silent: silent mode flag 808783f6184SRyusuke Konishi * @nilfs: the_nilfs struct 809783f6184SRyusuke Konishi * 810aa7dfb89SRyusuke Konishi * This function is called exclusively by nilfs->ns_mount_mutex. 811783f6184SRyusuke Konishi * So, the recovery process is protected from other simultaneous mounts. 812783f6184SRyusuke Konishi */ 813783f6184SRyusuke Konishi static int 814783f6184SRyusuke Konishi nilfs_fill_super(struct super_block *sb, void *data, int silent, 815783f6184SRyusuke Konishi struct the_nilfs *nilfs) 816783f6184SRyusuke Konishi { 817783f6184SRyusuke Konishi struct nilfs_sb_info *sbi; 818783f6184SRyusuke Konishi struct inode *root; 819783f6184SRyusuke Konishi __u64 cno; 820783f6184SRyusuke Konishi int err; 821783f6184SRyusuke Konishi 822783f6184SRyusuke Konishi sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); 823783f6184SRyusuke Konishi if (!sbi) 824783f6184SRyusuke Konishi return -ENOMEM; 825783f6184SRyusuke Konishi 826783f6184SRyusuke Konishi sb->s_fs_info = sbi; 827783f6184SRyusuke Konishi 828783f6184SRyusuke Konishi get_nilfs(nilfs); 829783f6184SRyusuke Konishi sbi->s_nilfs = nilfs; 830783f6184SRyusuke Konishi sbi->s_super = sb; 8316dd47406SRyusuke Konishi atomic_set(&sbi->s_count, 1); 832783f6184SRyusuke Konishi 833783f6184SRyusuke Konishi err = init_nilfs(nilfs, sbi, (char *)data); 834783f6184SRyusuke Konishi if (err) 835783f6184SRyusuke Konishi goto failed_sbi; 836783f6184SRyusuke Konishi 837783f6184SRyusuke Konishi spin_lock_init(&sbi->s_inode_lock); 838783f6184SRyusuke Konishi INIT_LIST_HEAD(&sbi->s_dirty_files); 839783f6184SRyusuke Konishi INIT_LIST_HEAD(&sbi->s_list); 840783f6184SRyusuke Konishi 841783f6184SRyusuke Konishi /* 842783f6184SRyusuke Konishi * Following initialization is overlapped because 843783f6184SRyusuke Konishi * nilfs_sb_info structure has been cleared at the beginning. 844783f6184SRyusuke Konishi * But we reserve them to keep our interest and make ready 845783f6184SRyusuke Konishi * for the future change. 846783f6184SRyusuke Konishi */ 847783f6184SRyusuke Konishi get_random_bytes(&sbi->s_next_generation, 848783f6184SRyusuke Konishi sizeof(sbi->s_next_generation)); 849783f6184SRyusuke Konishi spin_lock_init(&sbi->s_next_gen_lock); 850783f6184SRyusuke Konishi 851783f6184SRyusuke Konishi sb->s_op = &nilfs_sops; 852783f6184SRyusuke Konishi sb->s_export_op = &nilfs_export_ops; 853783f6184SRyusuke Konishi sb->s_root = NULL; 85461239230SRyusuke Konishi sb->s_time_gran = 1; 855973bec34SRyusuke Konishi sb->s_bdi = nilfs->ns_bdi; 856783f6184SRyusuke Konishi 857783f6184SRyusuke Konishi err = load_nilfs(nilfs, sbi); 858783f6184SRyusuke Konishi if (err) 859783f6184SRyusuke Konishi goto failed_sbi; 860f50a4c81SRyusuke Konishi 861783f6184SRyusuke Konishi cno = nilfs_last_cno(nilfs); 862783f6184SRyusuke Konishi 863783f6184SRyusuke Konishi if (sb->s_flags & MS_RDONLY) { 864783f6184SRyusuke Konishi if (nilfs_test_opt(sbi, SNAPSHOT)) { 86543be0ec0SZhu Yanhai down_read(&nilfs->ns_segctor_sem); 8661f5abe7eSRyusuke Konishi err = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, 8671f5abe7eSRyusuke Konishi sbi->s_snapshot_cno); 86843be0ec0SZhu Yanhai up_read(&nilfs->ns_segctor_sem); 86943be0ec0SZhu Yanhai if (err < 0) { 87043be0ec0SZhu Yanhai if (err == -ENOENT) 87143be0ec0SZhu Yanhai err = -EINVAL; 8721f5abe7eSRyusuke Konishi goto failed_sbi; 87343be0ec0SZhu Yanhai } 8741f5abe7eSRyusuke Konishi if (!err) { 875783f6184SRyusuke Konishi printk(KERN_ERR 876783f6184SRyusuke Konishi "NILFS: The specified checkpoint is " 877783f6184SRyusuke Konishi "not a snapshot " 878783f6184SRyusuke Konishi "(checkpoint number=%llu).\n", 879783f6184SRyusuke Konishi (unsigned long long)sbi->s_snapshot_cno); 880783f6184SRyusuke Konishi err = -EINVAL; 881783f6184SRyusuke Konishi goto failed_sbi; 882783f6184SRyusuke Konishi } 883783f6184SRyusuke Konishi cno = sbi->s_snapshot_cno; 884d240e067SRyusuke Konishi } 885783f6184SRyusuke Konishi } 886783f6184SRyusuke Konishi 887783f6184SRyusuke Konishi err = nilfs_attach_checkpoint(sbi, cno); 888783f6184SRyusuke Konishi if (err) { 889783f6184SRyusuke Konishi printk(KERN_ERR "NILFS: error loading a checkpoint" 890783f6184SRyusuke Konishi " (checkpoint number=%llu).\n", (unsigned long long)cno); 891783f6184SRyusuke Konishi goto failed_sbi; 892783f6184SRyusuke Konishi } 893783f6184SRyusuke Konishi 894783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 895cece5520SRyusuke Konishi err = nilfs_attach_segment_constructor(sbi); 896783f6184SRyusuke Konishi if (err) 897783f6184SRyusuke Konishi goto failed_checkpoint; 898783f6184SRyusuke Konishi } 899783f6184SRyusuke Konishi 900783f6184SRyusuke Konishi root = nilfs_iget(sb, NILFS_ROOT_INO); 901783f6184SRyusuke Konishi if (IS_ERR(root)) { 902783f6184SRyusuke Konishi printk(KERN_ERR "NILFS: get root inode failed\n"); 903783f6184SRyusuke Konishi err = PTR_ERR(root); 904783f6184SRyusuke Konishi goto failed_segctor; 905783f6184SRyusuke Konishi } 906783f6184SRyusuke Konishi if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) { 907783f6184SRyusuke Konishi iput(root); 908783f6184SRyusuke Konishi printk(KERN_ERR "NILFS: corrupt root inode.\n"); 909783f6184SRyusuke Konishi err = -EINVAL; 910783f6184SRyusuke Konishi goto failed_segctor; 911783f6184SRyusuke Konishi } 912783f6184SRyusuke Konishi sb->s_root = d_alloc_root(root); 913783f6184SRyusuke Konishi if (!sb->s_root) { 914783f6184SRyusuke Konishi iput(root); 915783f6184SRyusuke Konishi printk(KERN_ERR "NILFS: get root dentry failed\n"); 916783f6184SRyusuke Konishi err = -ENOMEM; 917783f6184SRyusuke Konishi goto failed_segctor; 918783f6184SRyusuke Konishi } 919783f6184SRyusuke Konishi 920783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 921783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 922783f6184SRyusuke Konishi nilfs_setup_super(sbi); 923783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 924783f6184SRyusuke Konishi } 925783f6184SRyusuke Konishi 926e59399d0SRyusuke Konishi down_write(&nilfs->ns_super_sem); 9273f82ff55SRyusuke Konishi if (!nilfs_test_opt(sbi, SNAPSHOT)) 9283f82ff55SRyusuke Konishi nilfs->ns_current = sbi; 929e59399d0SRyusuke Konishi up_write(&nilfs->ns_super_sem); 9303f82ff55SRyusuke Konishi 931783f6184SRyusuke Konishi return 0; 932783f6184SRyusuke Konishi 933783f6184SRyusuke Konishi failed_segctor: 934783f6184SRyusuke Konishi nilfs_detach_segment_constructor(sbi); 935783f6184SRyusuke Konishi 936783f6184SRyusuke Konishi failed_checkpoint: 937783f6184SRyusuke Konishi nilfs_detach_checkpoint(sbi); 938783f6184SRyusuke Konishi 939783f6184SRyusuke Konishi failed_sbi: 940783f6184SRyusuke Konishi put_nilfs(nilfs); 941783f6184SRyusuke Konishi sb->s_fs_info = NULL; 9426dd47406SRyusuke Konishi nilfs_put_sbinfo(sbi); 943783f6184SRyusuke Konishi return err; 944783f6184SRyusuke Konishi } 945783f6184SRyusuke Konishi 946783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data) 947783f6184SRyusuke Konishi { 948783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 949783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 950783f6184SRyusuke Konishi unsigned long old_sb_flags; 951783f6184SRyusuke Konishi struct nilfs_mount_options old_opts; 952d240e067SRyusuke Konishi int was_snapshot, err; 953783f6184SRyusuke Konishi 954e59399d0SRyusuke Konishi down_write(&nilfs->ns_super_sem); 955783f6184SRyusuke Konishi old_sb_flags = sb->s_flags; 956783f6184SRyusuke Konishi old_opts.mount_opt = sbi->s_mount_opt; 957783f6184SRyusuke Konishi old_opts.snapshot_cno = sbi->s_snapshot_cno; 958d240e067SRyusuke Konishi was_snapshot = nilfs_test_opt(sbi, SNAPSHOT); 959783f6184SRyusuke Konishi 9607c017457SRyusuke Konishi if (!parse_options(data, sb, 1)) { 961783f6184SRyusuke Konishi err = -EINVAL; 962783f6184SRyusuke Konishi goto restore_opts; 963783f6184SRyusuke Konishi } 964783f6184SRyusuke Konishi sb->s_flags = (sb->s_flags & ~MS_POSIXACL); 965783f6184SRyusuke Konishi 966d240e067SRyusuke Konishi err = -EINVAL; 9677c017457SRyusuke Konishi if (was_snapshot && !(*flags & MS_RDONLY)) { 9687c017457SRyusuke Konishi printk(KERN_ERR "NILFS (device %s): cannot remount snapshot " 9697c017457SRyusuke Konishi "read/write.\n", sb->s_id); 970d240e067SRyusuke Konishi goto restore_opts; 971d240e067SRyusuke Konishi } 972783f6184SRyusuke Konishi 9730234576dSRyusuke Konishi if (!nilfs_valid_fs(nilfs)) { 9740234576dSRyusuke Konishi printk(KERN_WARNING "NILFS (device %s): couldn't " 9750234576dSRyusuke Konishi "remount because the filesystem is in an " 9760234576dSRyusuke Konishi "incomplete recovery state.\n", sb->s_id); 9770234576dSRyusuke Konishi goto restore_opts; 9780234576dSRyusuke Konishi } 9790234576dSRyusuke Konishi 980783f6184SRyusuke Konishi if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 981783f6184SRyusuke Konishi goto out; 982783f6184SRyusuke Konishi if (*flags & MS_RDONLY) { 983783f6184SRyusuke Konishi /* Shutting down the segment constructor */ 984783f6184SRyusuke Konishi nilfs_detach_segment_constructor(sbi); 985783f6184SRyusuke Konishi sb->s_flags |= MS_RDONLY; 986783f6184SRyusuke Konishi 987783f6184SRyusuke Konishi /* 988783f6184SRyusuke Konishi * Remounting a valid RW partition RDONLY, so set 989783f6184SRyusuke Konishi * the RDONLY flag and then mark the partition as valid again. 990783f6184SRyusuke Konishi */ 991783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 9927ecaa46cSRyusuke Konishi nilfs_cleanup_super(sbi); 993783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 994783f6184SRyusuke Konishi } else { 995c5ca48aaSRyusuke Konishi __u64 features; 996c5ca48aaSRyusuke Konishi 997783f6184SRyusuke Konishi /* 998783f6184SRyusuke Konishi * Mounting a RDONLY partition read-write, so reread and 999783f6184SRyusuke Konishi * store the current valid flag. (It may have been changed 1000783f6184SRyusuke Konishi * by fsck since we originally mounted the partition.) 1001783f6184SRyusuke Konishi */ 1002c5ca48aaSRyusuke Konishi down_read(&nilfs->ns_sem); 1003c5ca48aaSRyusuke Konishi features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) & 1004c5ca48aaSRyusuke Konishi ~NILFS_FEATURE_COMPAT_RO_SUPP; 1005c5ca48aaSRyusuke Konishi up_read(&nilfs->ns_sem); 1006c5ca48aaSRyusuke Konishi if (features) { 1007c5ca48aaSRyusuke Konishi printk(KERN_WARNING "NILFS (device %s): couldn't " 1008c5ca48aaSRyusuke Konishi "remount RDWR because of unsupported optional " 1009c5ca48aaSRyusuke Konishi "features (%llx)\n", 1010c5ca48aaSRyusuke Konishi sb->s_id, (unsigned long long)features); 1011c5ca48aaSRyusuke Konishi err = -EROFS; 1012c5ca48aaSRyusuke Konishi goto restore_opts; 1013c5ca48aaSRyusuke Konishi } 1014c5ca48aaSRyusuke Konishi 1015783f6184SRyusuke Konishi sb->s_flags &= ~MS_RDONLY; 1016783f6184SRyusuke Konishi 1017cece5520SRyusuke Konishi err = nilfs_attach_segment_constructor(sbi); 1018783f6184SRyusuke Konishi if (err) 1019e59399d0SRyusuke Konishi goto restore_opts; 1020783f6184SRyusuke Konishi 1021783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 1022783f6184SRyusuke Konishi nilfs_setup_super(sbi); 1023783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 1024783f6184SRyusuke Konishi } 1025783f6184SRyusuke Konishi out: 1026e59399d0SRyusuke Konishi up_write(&nilfs->ns_super_sem); 1027783f6184SRyusuke Konishi return 0; 1028783f6184SRyusuke Konishi 1029783f6184SRyusuke Konishi restore_opts: 1030783f6184SRyusuke Konishi sb->s_flags = old_sb_flags; 1031783f6184SRyusuke Konishi sbi->s_mount_opt = old_opts.mount_opt; 1032783f6184SRyusuke Konishi sbi->s_snapshot_cno = old_opts.snapshot_cno; 1033e59399d0SRyusuke Konishi up_write(&nilfs->ns_super_sem); 1034783f6184SRyusuke Konishi return err; 1035783f6184SRyusuke Konishi } 1036783f6184SRyusuke Konishi 1037783f6184SRyusuke Konishi struct nilfs_super_data { 1038783f6184SRyusuke Konishi struct block_device *bdev; 10396dd47406SRyusuke Konishi struct nilfs_sb_info *sbi; 1040783f6184SRyusuke Konishi __u64 cno; 1041783f6184SRyusuke Konishi int flags; 1042783f6184SRyusuke Konishi }; 1043783f6184SRyusuke Konishi 1044783f6184SRyusuke Konishi /** 1045783f6184SRyusuke Konishi * nilfs_identify - pre-read mount options needed to identify mount instance 1046783f6184SRyusuke Konishi * @data: mount options 1047783f6184SRyusuke Konishi * @sd: nilfs_super_data 1048783f6184SRyusuke Konishi */ 1049783f6184SRyusuke Konishi static int nilfs_identify(char *data, struct nilfs_super_data *sd) 1050783f6184SRyusuke Konishi { 1051783f6184SRyusuke Konishi char *p, *options = data; 1052783f6184SRyusuke Konishi substring_t args[MAX_OPT_ARGS]; 1053783f6184SRyusuke Konishi int option, token; 1054783f6184SRyusuke Konishi int ret = 0; 1055783f6184SRyusuke Konishi 1056783f6184SRyusuke Konishi do { 1057783f6184SRyusuke Konishi p = strsep(&options, ","); 1058783f6184SRyusuke Konishi if (p != NULL && *p) { 1059783f6184SRyusuke Konishi token = match_token(p, tokens, args); 1060783f6184SRyusuke Konishi if (token == Opt_snapshot) { 1061783f6184SRyusuke Konishi if (!(sd->flags & MS_RDONLY)) 1062783f6184SRyusuke Konishi ret++; 1063783f6184SRyusuke Konishi else { 1064783f6184SRyusuke Konishi ret = match_int(&args[0], &option); 1065783f6184SRyusuke Konishi if (!ret) { 1066783f6184SRyusuke Konishi if (option > 0) 1067783f6184SRyusuke Konishi sd->cno = option; 1068783f6184SRyusuke Konishi else 1069783f6184SRyusuke Konishi ret++; 1070783f6184SRyusuke Konishi } 1071783f6184SRyusuke Konishi } 1072783f6184SRyusuke Konishi } 1073783f6184SRyusuke Konishi if (ret) 1074783f6184SRyusuke Konishi printk(KERN_ERR 1075783f6184SRyusuke Konishi "NILFS: invalid mount option: %s\n", p); 1076783f6184SRyusuke Konishi } 1077783f6184SRyusuke Konishi if (!options) 1078783f6184SRyusuke Konishi break; 1079783f6184SRyusuke Konishi BUG_ON(options == data); 1080783f6184SRyusuke Konishi *(options - 1) = ','; 1081783f6184SRyusuke Konishi } while (!ret); 1082783f6184SRyusuke Konishi return ret; 1083783f6184SRyusuke Konishi } 1084783f6184SRyusuke Konishi 1085783f6184SRyusuke Konishi static int nilfs_set_bdev_super(struct super_block *s, void *data) 1086783f6184SRyusuke Konishi { 1087783f6184SRyusuke Konishi struct nilfs_super_data *sd = data; 1088783f6184SRyusuke Konishi 1089783f6184SRyusuke Konishi s->s_bdev = sd->bdev; 1090783f6184SRyusuke Konishi s->s_dev = s->s_bdev->bd_dev; 1091783f6184SRyusuke Konishi return 0; 1092783f6184SRyusuke Konishi } 1093783f6184SRyusuke Konishi 1094783f6184SRyusuke Konishi static int nilfs_test_bdev_super(struct super_block *s, void *data) 1095783f6184SRyusuke Konishi { 1096783f6184SRyusuke Konishi struct nilfs_super_data *sd = data; 1097783f6184SRyusuke Konishi 10986dd47406SRyusuke Konishi return sd->sbi && s->s_fs_info == (void *)sd->sbi; 1099783f6184SRyusuke Konishi } 1100783f6184SRyusuke Konishi 1101783f6184SRyusuke Konishi static int 1102783f6184SRyusuke Konishi nilfs_get_sb(struct file_system_type *fs_type, int flags, 1103783f6184SRyusuke Konishi const char *dev_name, void *data, struct vfsmount *mnt) 1104783f6184SRyusuke Konishi { 1105783f6184SRyusuke Konishi struct nilfs_super_data sd; 110633c8e57cSRyusuke Konishi struct super_block *s; 110713e90559SRyusuke Konishi fmode_t mode = FMODE_READ; 110833c8e57cSRyusuke Konishi struct the_nilfs *nilfs; 1109783f6184SRyusuke Konishi int err, need_to_close = 1; 1110783f6184SRyusuke Konishi 111113e90559SRyusuke Konishi if (!(flags & MS_RDONLY)) 111213e90559SRyusuke Konishi mode |= FMODE_WRITE; 111313e90559SRyusuke Konishi 111413e90559SRyusuke Konishi sd.bdev = open_bdev_exclusive(dev_name, mode, fs_type); 1115d6d4c19cSJan Blunck if (IS_ERR(sd.bdev)) 1116783f6184SRyusuke Konishi return PTR_ERR(sd.bdev); 1117783f6184SRyusuke Konishi 1118783f6184SRyusuke Konishi /* 1119783f6184SRyusuke Konishi * To get mount instance using sget() vfs-routine, NILFS needs 1120783f6184SRyusuke Konishi * much more information than normal filesystems to identify mount 1121783f6184SRyusuke Konishi * instance. For snapshot mounts, not only a mount type (ro-mount 1122783f6184SRyusuke Konishi * or rw-mount) but also a checkpoint number is required. 1123783f6184SRyusuke Konishi */ 1124783f6184SRyusuke Konishi sd.cno = 0; 1125783f6184SRyusuke Konishi sd.flags = flags; 1126783f6184SRyusuke Konishi if (nilfs_identify((char *)data, &sd)) { 1127783f6184SRyusuke Konishi err = -EINVAL; 1128783f6184SRyusuke Konishi goto failed; 1129783f6184SRyusuke Konishi } 1130783f6184SRyusuke Konishi 113133c8e57cSRyusuke Konishi nilfs = find_or_create_nilfs(sd.bdev); 113233c8e57cSRyusuke Konishi if (!nilfs) { 113333c8e57cSRyusuke Konishi err = -ENOMEM; 113433c8e57cSRyusuke Konishi goto failed; 113533c8e57cSRyusuke Konishi } 113633c8e57cSRyusuke Konishi 1137aa7dfb89SRyusuke Konishi mutex_lock(&nilfs->ns_mount_mutex); 11383f82ff55SRyusuke Konishi 11393f82ff55SRyusuke Konishi if (!sd.cno) { 11403f82ff55SRyusuke Konishi /* 11413f82ff55SRyusuke Konishi * Check if an exclusive mount exists or not. 11423f82ff55SRyusuke Konishi * Snapshot mounts coexist with a current mount 11433f82ff55SRyusuke Konishi * (i.e. rw-mount or ro-mount), whereas rw-mount and 11443f82ff55SRyusuke Konishi * ro-mount are mutually exclusive. 11453f82ff55SRyusuke Konishi */ 1146e59399d0SRyusuke Konishi down_read(&nilfs->ns_super_sem); 11473f82ff55SRyusuke Konishi if (nilfs->ns_current && 11483f82ff55SRyusuke Konishi ((nilfs->ns_current->s_super->s_flags ^ flags) 11493f82ff55SRyusuke Konishi & MS_RDONLY)) { 1150e59399d0SRyusuke Konishi up_read(&nilfs->ns_super_sem); 11513f82ff55SRyusuke Konishi err = -EBUSY; 1152783f6184SRyusuke Konishi goto failed_unlock; 1153783f6184SRyusuke Konishi } 1154e59399d0SRyusuke Konishi up_read(&nilfs->ns_super_sem); 11553f82ff55SRyusuke Konishi } 1156783f6184SRyusuke Konishi 1157783f6184SRyusuke Konishi /* 11586dd47406SRyusuke Konishi * Find existing nilfs_sb_info struct 1159783f6184SRyusuke Konishi */ 11606dd47406SRyusuke Konishi sd.sbi = nilfs_find_sbinfo(nilfs, !(flags & MS_RDONLY), sd.cno); 11616dd47406SRyusuke Konishi 11626dd47406SRyusuke Konishi /* 11636dd47406SRyusuke Konishi * Get super block instance holding the nilfs_sb_info struct. 11646dd47406SRyusuke Konishi * A new instance is allocated if no existing mount is present or 11656dd47406SRyusuke Konishi * existing instance has been unmounted. 11666dd47406SRyusuke Konishi */ 116733c8e57cSRyusuke Konishi s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, &sd); 11686dd47406SRyusuke Konishi if (sd.sbi) 11696dd47406SRyusuke Konishi nilfs_put_sbinfo(sd.sbi); 11706dd47406SRyusuke Konishi 117133c8e57cSRyusuke Konishi if (IS_ERR(s)) { 117233c8e57cSRyusuke Konishi err = PTR_ERR(s); 117333c8e57cSRyusuke Konishi goto failed_unlock; 1174783f6184SRyusuke Konishi } 1175783f6184SRyusuke Konishi 1176783f6184SRyusuke Konishi if (!s->s_root) { 1177783f6184SRyusuke Konishi char b[BDEVNAME_SIZE]; 1178783f6184SRyusuke Konishi 117933c8e57cSRyusuke Konishi /* New superblock instance created */ 1180783f6184SRyusuke Konishi s->s_flags = flags; 11814571b82cSRyusuke Konishi s->s_mode = mode; 1182783f6184SRyusuke Konishi strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id)); 1183783f6184SRyusuke Konishi sb_set_blocksize(s, block_size(sd.bdev)); 1184783f6184SRyusuke Konishi 1185e2d1591aSRyusuke Konishi err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0, 1186e2d1591aSRyusuke Konishi nilfs); 1187783f6184SRyusuke Konishi if (err) 1188783f6184SRyusuke Konishi goto cancel_new; 1189783f6184SRyusuke Konishi 1190783f6184SRyusuke Konishi s->s_flags |= MS_ACTIVE; 1191783f6184SRyusuke Konishi need_to_close = 0; 1192783f6184SRyusuke Konishi } 1193783f6184SRyusuke Konishi 1194aa7dfb89SRyusuke Konishi mutex_unlock(&nilfs->ns_mount_mutex); 1195783f6184SRyusuke Konishi put_nilfs(nilfs); 1196783f6184SRyusuke Konishi if (need_to_close) 119713e90559SRyusuke Konishi close_bdev_exclusive(sd.bdev, mode); 1198783f6184SRyusuke Konishi simple_set_mnt(mnt, s); 1199783f6184SRyusuke Konishi return 0; 1200783f6184SRyusuke Konishi 1201783f6184SRyusuke Konishi failed_unlock: 1202aa7dfb89SRyusuke Konishi mutex_unlock(&nilfs->ns_mount_mutex); 120333c8e57cSRyusuke Konishi put_nilfs(nilfs); 1204783f6184SRyusuke Konishi failed: 120513e90559SRyusuke Konishi close_bdev_exclusive(sd.bdev, mode); 1206783f6184SRyusuke Konishi return err; 1207783f6184SRyusuke Konishi 1208783f6184SRyusuke Konishi cancel_new: 1209783f6184SRyusuke Konishi /* Abandoning the newly allocated superblock */ 1210aa7dfb89SRyusuke Konishi mutex_unlock(&nilfs->ns_mount_mutex); 1211783f6184SRyusuke Konishi put_nilfs(nilfs); 1212a95161aaSAl Viro deactivate_locked_super(s); 1213783f6184SRyusuke Konishi /* 1214b87ca919SRyusuke Konishi * deactivate_locked_super() invokes close_bdev_exclusive(). 1215783f6184SRyusuke Konishi * We must finish all post-cleaning before this call; 1216aa7dfb89SRyusuke Konishi * put_nilfs() needs the block device. 1217783f6184SRyusuke Konishi */ 1218783f6184SRyusuke Konishi return err; 1219783f6184SRyusuke Konishi } 1220783f6184SRyusuke Konishi 1221783f6184SRyusuke Konishi struct file_system_type nilfs_fs_type = { 1222783f6184SRyusuke Konishi .owner = THIS_MODULE, 1223783f6184SRyusuke Konishi .name = "nilfs2", 1224783f6184SRyusuke Konishi .get_sb = nilfs_get_sb, 1225783f6184SRyusuke Konishi .kill_sb = kill_block_super, 1226783f6184SRyusuke Konishi .fs_flags = FS_REQUIRES_DEV, 1227783f6184SRyusuke Konishi }; 1228783f6184SRyusuke Konishi 122941c88bd7SLi Hong static void nilfs_inode_init_once(void *obj) 123041c88bd7SLi Hong { 123141c88bd7SLi Hong struct nilfs_inode_info *ii = obj; 123241c88bd7SLi Hong 123341c88bd7SLi Hong INIT_LIST_HEAD(&ii->i_dirty); 123441c88bd7SLi Hong #ifdef CONFIG_NILFS_XATTR 123541c88bd7SLi Hong init_rwsem(&ii->xattr_sem); 123641c88bd7SLi Hong #endif 123741c88bd7SLi Hong nilfs_btnode_cache_init_once(&ii->i_btnode_cache); 123805d0e94bSRyusuke Konishi ii->i_bmap = &ii->i_bmap_data; 123941c88bd7SLi Hong inode_init_once(&ii->vfs_inode); 124041c88bd7SLi Hong } 124141c88bd7SLi Hong 124241c88bd7SLi Hong static void nilfs_segbuf_init_once(void *obj) 124341c88bd7SLi Hong { 124441c88bd7SLi Hong memset(obj, 0, sizeof(struct nilfs_segment_buffer)); 124541c88bd7SLi Hong } 124641c88bd7SLi Hong 124741c88bd7SLi Hong static void nilfs_destroy_cachep(void) 124841c88bd7SLi Hong { 124941c88bd7SLi Hong if (nilfs_inode_cachep) 125041c88bd7SLi Hong kmem_cache_destroy(nilfs_inode_cachep); 125141c88bd7SLi Hong if (nilfs_transaction_cachep) 125241c88bd7SLi Hong kmem_cache_destroy(nilfs_transaction_cachep); 125341c88bd7SLi Hong if (nilfs_segbuf_cachep) 125441c88bd7SLi Hong kmem_cache_destroy(nilfs_segbuf_cachep); 125541c88bd7SLi Hong if (nilfs_btree_path_cache) 125641c88bd7SLi Hong kmem_cache_destroy(nilfs_btree_path_cache); 125741c88bd7SLi Hong } 125841c88bd7SLi Hong 125941c88bd7SLi Hong static int __init nilfs_init_cachep(void) 126041c88bd7SLi Hong { 126141c88bd7SLi Hong nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache", 126241c88bd7SLi Hong sizeof(struct nilfs_inode_info), 0, 126341c88bd7SLi Hong SLAB_RECLAIM_ACCOUNT, nilfs_inode_init_once); 126441c88bd7SLi Hong if (!nilfs_inode_cachep) 126541c88bd7SLi Hong goto fail; 126641c88bd7SLi Hong 126741c88bd7SLi Hong nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache", 126841c88bd7SLi Hong sizeof(struct nilfs_transaction_info), 0, 126941c88bd7SLi Hong SLAB_RECLAIM_ACCOUNT, NULL); 127041c88bd7SLi Hong if (!nilfs_transaction_cachep) 127141c88bd7SLi Hong goto fail; 127241c88bd7SLi Hong 127341c88bd7SLi Hong nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache", 127441c88bd7SLi Hong sizeof(struct nilfs_segment_buffer), 0, 127541c88bd7SLi Hong SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once); 127641c88bd7SLi Hong if (!nilfs_segbuf_cachep) 127741c88bd7SLi Hong goto fail; 127841c88bd7SLi Hong 127941c88bd7SLi Hong nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache", 128041c88bd7SLi Hong sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX, 128141c88bd7SLi Hong 0, 0, NULL); 128241c88bd7SLi Hong if (!nilfs_btree_path_cache) 128341c88bd7SLi Hong goto fail; 128441c88bd7SLi Hong 128541c88bd7SLi Hong return 0; 128641c88bd7SLi Hong 128741c88bd7SLi Hong fail: 128841c88bd7SLi Hong nilfs_destroy_cachep(); 128941c88bd7SLi Hong return -ENOMEM; 129041c88bd7SLi Hong } 129141c88bd7SLi Hong 1292783f6184SRyusuke Konishi static int __init init_nilfs_fs(void) 1293783f6184SRyusuke Konishi { 1294783f6184SRyusuke Konishi int err; 1295783f6184SRyusuke Konishi 129641c88bd7SLi Hong err = nilfs_init_cachep(); 1297783f6184SRyusuke Konishi if (err) 129841c88bd7SLi Hong goto fail; 1299783f6184SRyusuke Konishi 1300783f6184SRyusuke Konishi err = register_filesystem(&nilfs_fs_type); 1301783f6184SRyusuke Konishi if (err) 130241c88bd7SLi Hong goto free_cachep; 1303783f6184SRyusuke Konishi 13049f130263SLi Hong printk(KERN_INFO "NILFS version 2 loaded\n"); 1305783f6184SRyusuke Konishi return 0; 1306783f6184SRyusuke Konishi 130741c88bd7SLi Hong free_cachep: 130841c88bd7SLi Hong nilfs_destroy_cachep(); 130941c88bd7SLi Hong fail: 1310783f6184SRyusuke Konishi return err; 1311783f6184SRyusuke Konishi } 1312783f6184SRyusuke Konishi 1313783f6184SRyusuke Konishi static void __exit exit_nilfs_fs(void) 1314783f6184SRyusuke Konishi { 131541c88bd7SLi Hong nilfs_destroy_cachep(); 1316783f6184SRyusuke Konishi unregister_filesystem(&nilfs_fs_type); 1317783f6184SRyusuke Konishi } 1318783f6184SRyusuke Konishi 1319783f6184SRyusuke Konishi module_init(init_nilfs_fs) 1320783f6184SRyusuke Konishi module_exit(exit_nilfs_fs) 1321