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 71*abc0b50bSJiro SEKIBA static 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 765beb6e0bSRyusuke Konishi static int nilfs_setup_super(struct nilfs_sb_info *sbi, int is_mount); 77783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data); 78783f6184SRyusuke Konishi 79c8a11c8aSRyusuke Konishi static void nilfs_set_error(struct nilfs_sb_info *sbi) 80c8a11c8aSRyusuke Konishi { 81c8a11c8aSRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 82d26493b6SJiro SEKIBA struct nilfs_super_block **sbp; 83c8a11c8aSRyusuke Konishi 84c8a11c8aSRyusuke Konishi down_write(&nilfs->ns_sem); 85c8a11c8aSRyusuke Konishi if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) { 86c8a11c8aSRyusuke Konishi nilfs->ns_mount_state |= NILFS_ERROR_FS; 87b2ac86e1SJiro SEKIBA sbp = nilfs_prepare_super(sbi, 0); 88d26493b6SJiro SEKIBA if (likely(sbp)) { 89d26493b6SJiro SEKIBA sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS); 90b2ac86e1SJiro SEKIBA if (sbp[1]) 91b2ac86e1SJiro SEKIBA sbp[1]->s_state |= cpu_to_le16(NILFS_ERROR_FS); 92b2ac86e1SJiro SEKIBA nilfs_commit_super(sbi, NILFS_SB_COMMIT_ALL); 93c8a11c8aSRyusuke Konishi } 94d26493b6SJiro SEKIBA } 95c8a11c8aSRyusuke Konishi up_write(&nilfs->ns_sem); 96c8a11c8aSRyusuke Konishi } 97c8a11c8aSRyusuke Konishi 98783f6184SRyusuke Konishi /** 99783f6184SRyusuke Konishi * nilfs_error() - report failure condition on a filesystem 100783f6184SRyusuke Konishi * 101783f6184SRyusuke Konishi * nilfs_error() sets an ERROR_FS flag on the superblock as well as 102783f6184SRyusuke Konishi * reporting an error message. It should be called when NILFS detects 103783f6184SRyusuke Konishi * incoherences or defects of meta data on disk. As for sustainable 104783f6184SRyusuke Konishi * errors such as a single-shot I/O error, nilfs_warning() or the printk() 105783f6184SRyusuke Konishi * function should be used instead. 106783f6184SRyusuke Konishi * 107783f6184SRyusuke Konishi * The segment constructor must not call this function because it can 108783f6184SRyusuke Konishi * kill itself. 109783f6184SRyusuke Konishi */ 110783f6184SRyusuke Konishi void nilfs_error(struct super_block *sb, const char *function, 111783f6184SRyusuke Konishi const char *fmt, ...) 112783f6184SRyusuke Konishi { 113783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 114783f6184SRyusuke Konishi va_list args; 115783f6184SRyusuke Konishi 116783f6184SRyusuke Konishi va_start(args, fmt); 117783f6184SRyusuke Konishi printk(KERN_CRIT "NILFS error (device %s): %s: ", sb->s_id, function); 118783f6184SRyusuke Konishi vprintk(fmt, args); 119783f6184SRyusuke Konishi printk("\n"); 120783f6184SRyusuke Konishi va_end(args); 121783f6184SRyusuke Konishi 122783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 123c8a11c8aSRyusuke Konishi nilfs_set_error(sbi); 124783f6184SRyusuke Konishi 125783f6184SRyusuke Konishi if (nilfs_test_opt(sbi, ERRORS_RO)) { 126783f6184SRyusuke Konishi printk(KERN_CRIT "Remounting filesystem read-only\n"); 127783f6184SRyusuke Konishi sb->s_flags |= MS_RDONLY; 128783f6184SRyusuke Konishi } 129783f6184SRyusuke Konishi } 130783f6184SRyusuke Konishi 131783f6184SRyusuke Konishi if (nilfs_test_opt(sbi, ERRORS_PANIC)) 132783f6184SRyusuke Konishi panic("NILFS (device %s): panic forced after error\n", 133783f6184SRyusuke Konishi sb->s_id); 134783f6184SRyusuke Konishi } 135783f6184SRyusuke Konishi 136783f6184SRyusuke Konishi void nilfs_warning(struct super_block *sb, const char *function, 137783f6184SRyusuke Konishi const char *fmt, ...) 138783f6184SRyusuke Konishi { 139783f6184SRyusuke Konishi va_list args; 140783f6184SRyusuke Konishi 141783f6184SRyusuke Konishi va_start(args, fmt); 142783f6184SRyusuke Konishi printk(KERN_WARNING "NILFS warning (device %s): %s: ", 143783f6184SRyusuke Konishi sb->s_id, function); 144783f6184SRyusuke Konishi vprintk(fmt, args); 145783f6184SRyusuke Konishi printk("\n"); 146783f6184SRyusuke Konishi va_end(args); 147783f6184SRyusuke Konishi } 148783f6184SRyusuke Konishi 149783f6184SRyusuke Konishi 1502879ed66SRyusuke Konishi struct inode *nilfs_alloc_inode(struct super_block *sb) 151783f6184SRyusuke Konishi { 152783f6184SRyusuke Konishi struct nilfs_inode_info *ii; 153783f6184SRyusuke Konishi 154783f6184SRyusuke Konishi ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS); 155783f6184SRyusuke Konishi if (!ii) 156783f6184SRyusuke Konishi return NULL; 157783f6184SRyusuke Konishi ii->i_bh = NULL; 158783f6184SRyusuke Konishi ii->i_state = 0; 1590e14a359SRyusuke Konishi ii->i_cno = 0; 160783f6184SRyusuke Konishi ii->vfs_inode.i_version = 1; 1612879ed66SRyusuke Konishi nilfs_btnode_cache_init(&ii->i_btnode_cache, sb->s_bdi); 162783f6184SRyusuke Konishi return &ii->vfs_inode; 163783f6184SRyusuke Konishi } 164783f6184SRyusuke Konishi 165783f6184SRyusuke Konishi void nilfs_destroy_inode(struct inode *inode) 166783f6184SRyusuke Konishi { 167b91c9a97SRyusuke Konishi struct nilfs_mdt_info *mdi = NILFS_MDT(inode); 168b91c9a97SRyusuke Konishi 169b91c9a97SRyusuke Konishi if (mdi) { 170b91c9a97SRyusuke Konishi kfree(mdi->mi_bgl); /* kfree(NULL) is safe */ 171b91c9a97SRyusuke Konishi kfree(mdi); 172b91c9a97SRyusuke Konishi } 173783f6184SRyusuke Konishi kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode)); 174783f6184SRyusuke Konishi } 175783f6184SRyusuke Konishi 176b2ac86e1SJiro SEKIBA static int nilfs_sync_super(struct nilfs_sb_info *sbi, int flag) 177783f6184SRyusuke Konishi { 178783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 179783f6184SRyusuke Konishi int err; 180783f6184SRyusuke Konishi 181783f6184SRyusuke Konishi retry: 182e339ad31SRyusuke Konishi set_buffer_dirty(nilfs->ns_sbh[0]); 18387e99511SChristoph Hellwig 18487e99511SChristoph Hellwig if (nilfs_test_opt(sbi, BARRIER)) { 18587e99511SChristoph Hellwig err = __sync_dirty_buffer(nilfs->ns_sbh[0], 18687e99511SChristoph Hellwig WRITE_SYNC | WRITE_BARRIER); 18787e99511SChristoph Hellwig if (err == -EOPNOTSUPP) { 188783f6184SRyusuke Konishi nilfs_warning(sbi->s_super, __func__, 189783f6184SRyusuke Konishi "barrier-based sync failed. " 190783f6184SRyusuke Konishi "disabling barriers\n"); 191783f6184SRyusuke Konishi nilfs_clear_opt(sbi, BARRIER); 192783f6184SRyusuke Konishi goto retry; 193783f6184SRyusuke Konishi } 19487e99511SChristoph Hellwig } else { 19587e99511SChristoph Hellwig err = sync_dirty_buffer(nilfs->ns_sbh[0]); 19687e99511SChristoph Hellwig } 19787e99511SChristoph Hellwig 198e339ad31SRyusuke Konishi if (unlikely(err)) { 199783f6184SRyusuke Konishi printk(KERN_ERR 200783f6184SRyusuke Konishi "NILFS: unable to write superblock (err=%d)\n", err); 201e339ad31SRyusuke Konishi if (err == -EIO && nilfs->ns_sbh[1]) { 202b2ac86e1SJiro SEKIBA /* 203b2ac86e1SJiro SEKIBA * sbp[0] points to newer log than sbp[1], 204b2ac86e1SJiro SEKIBA * so copy sbp[0] to sbp[1] to take over sbp[0]. 205b2ac86e1SJiro SEKIBA */ 206b2ac86e1SJiro SEKIBA memcpy(nilfs->ns_sbp[1], nilfs->ns_sbp[0], 207b2ac86e1SJiro SEKIBA nilfs->ns_sbsize); 208e339ad31SRyusuke Konishi nilfs_fall_back_super_block(nilfs); 209e339ad31SRyusuke Konishi goto retry; 210e339ad31SRyusuke Konishi } 211e339ad31SRyusuke Konishi } else { 212e339ad31SRyusuke Konishi struct nilfs_super_block *sbp = nilfs->ns_sbp[0]; 213e339ad31SRyusuke Konishi 214b2ac86e1SJiro SEKIBA nilfs->ns_sbwcount++; 215b2ac86e1SJiro SEKIBA 216e339ad31SRyusuke Konishi /* 217e339ad31SRyusuke Konishi * The latest segment becomes trailable from the position 218e339ad31SRyusuke Konishi * written in superblock. 219e339ad31SRyusuke Konishi */ 220783f6184SRyusuke Konishi clear_nilfs_discontinued(nilfs); 221e339ad31SRyusuke Konishi 222e339ad31SRyusuke Konishi /* update GC protection for recent segments */ 223e339ad31SRyusuke Konishi if (nilfs->ns_sbh[1]) { 224b2ac86e1SJiro SEKIBA if (flag == NILFS_SB_COMMIT_ALL) { 225e339ad31SRyusuke Konishi set_buffer_dirty(nilfs->ns_sbh[1]); 226b2ac86e1SJiro SEKIBA if (sync_dirty_buffer(nilfs->ns_sbh[1]) < 0) 227b2ac86e1SJiro SEKIBA goto out; 228b2ac86e1SJiro SEKIBA } 229b2ac86e1SJiro SEKIBA if (le64_to_cpu(nilfs->ns_sbp[1]->s_last_cno) < 230b2ac86e1SJiro SEKIBA le64_to_cpu(nilfs->ns_sbp[0]->s_last_cno)) 231e339ad31SRyusuke Konishi sbp = nilfs->ns_sbp[1]; 232e339ad31SRyusuke Konishi } 233b2ac86e1SJiro SEKIBA 2342c2e52fcSRyusuke Konishi spin_lock(&nilfs->ns_last_segment_lock); 235e339ad31SRyusuke Konishi nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq); 2362c2e52fcSRyusuke Konishi spin_unlock(&nilfs->ns_last_segment_lock); 237783f6184SRyusuke Konishi } 238b2ac86e1SJiro SEKIBA out: 239783f6184SRyusuke Konishi return err; 240783f6184SRyusuke Konishi } 241783f6184SRyusuke Konishi 24260f46b7eSRyusuke Konishi void nilfs_set_log_cursor(struct nilfs_super_block *sbp, 24360f46b7eSRyusuke Konishi struct the_nilfs *nilfs) 24460f46b7eSRyusuke Konishi { 24560f46b7eSRyusuke Konishi sector_t nfreeblocks; 24660f46b7eSRyusuke Konishi 24760f46b7eSRyusuke Konishi /* nilfs->ns_sem must be locked by the caller. */ 24860f46b7eSRyusuke Konishi nilfs_count_free_blocks(nilfs, &nfreeblocks); 24960f46b7eSRyusuke Konishi sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks); 25060f46b7eSRyusuke Konishi 25160f46b7eSRyusuke Konishi spin_lock(&nilfs->ns_last_segment_lock); 25260f46b7eSRyusuke Konishi sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq); 25360f46b7eSRyusuke Konishi sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg); 25460f46b7eSRyusuke Konishi sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno); 25560f46b7eSRyusuke Konishi spin_unlock(&nilfs->ns_last_segment_lock); 25660f46b7eSRyusuke Konishi } 25760f46b7eSRyusuke Konishi 258b2ac86e1SJiro SEKIBA struct nilfs_super_block **nilfs_prepare_super(struct nilfs_sb_info *sbi, 259b2ac86e1SJiro SEKIBA int flip) 260d26493b6SJiro SEKIBA { 261d26493b6SJiro SEKIBA struct the_nilfs *nilfs = sbi->s_nilfs; 262d26493b6SJiro SEKIBA struct nilfs_super_block **sbp = nilfs->ns_sbp; 263d26493b6SJiro SEKIBA 264d26493b6SJiro SEKIBA /* nilfs->ns_sem must be locked by the caller. */ 265d26493b6SJiro SEKIBA if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) { 266d26493b6SJiro SEKIBA if (sbp[1] && 267d26493b6SJiro SEKIBA sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) { 268b2ac86e1SJiro SEKIBA memcpy(sbp[0], sbp[1], nilfs->ns_sbsize); 269d26493b6SJiro SEKIBA } else { 270d26493b6SJiro SEKIBA printk(KERN_CRIT "NILFS: superblock broke on dev %s\n", 271d26493b6SJiro SEKIBA sbi->s_super->s_id); 272d26493b6SJiro SEKIBA return NULL; 273d26493b6SJiro SEKIBA } 274b2ac86e1SJiro SEKIBA } else if (sbp[1] && 275b2ac86e1SJiro SEKIBA sbp[1]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) { 276b2ac86e1SJiro SEKIBA memcpy(sbp[1], sbp[0], nilfs->ns_sbsize); 277d26493b6SJiro SEKIBA } 278b2ac86e1SJiro SEKIBA 279b2ac86e1SJiro SEKIBA if (flip && sbp[1]) 280b2ac86e1SJiro SEKIBA nilfs_swap_super_block(nilfs); 281b2ac86e1SJiro SEKIBA 282d26493b6SJiro SEKIBA return sbp; 283d26493b6SJiro SEKIBA } 284d26493b6SJiro SEKIBA 285b2ac86e1SJiro SEKIBA int nilfs_commit_super(struct nilfs_sb_info *sbi, int flag) 286783f6184SRyusuke Konishi { 287783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 288e339ad31SRyusuke Konishi struct nilfs_super_block **sbp = nilfs->ns_sbp; 289e339ad31SRyusuke Konishi time_t t; 290783f6184SRyusuke Konishi 291d26493b6SJiro SEKIBA /* nilfs->ns_sem must be locked by the caller. */ 292e339ad31SRyusuke Konishi t = get_seconds(); 293b2ac86e1SJiro SEKIBA nilfs->ns_sbwtime = t; 294e339ad31SRyusuke Konishi sbp[0]->s_wtime = cpu_to_le64(t); 295e339ad31SRyusuke Konishi sbp[0]->s_sum = 0; 296e339ad31SRyusuke Konishi sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed, 297e339ad31SRyusuke Konishi (unsigned char *)sbp[0], 298e339ad31SRyusuke Konishi nilfs->ns_sbsize)); 299b2ac86e1SJiro SEKIBA if (flag == NILFS_SB_COMMIT_ALL && sbp[1]) { 300b2ac86e1SJiro SEKIBA sbp[1]->s_wtime = sbp[0]->s_wtime; 301b2ac86e1SJiro SEKIBA sbp[1]->s_sum = 0; 302b2ac86e1SJiro SEKIBA sbp[1]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed, 303b2ac86e1SJiro SEKIBA (unsigned char *)sbp[1], 304b2ac86e1SJiro SEKIBA nilfs->ns_sbsize)); 305e339ad31SRyusuke Konishi } 306e605f0a7SRyusuke Konishi clear_nilfs_sb_dirty(nilfs); 307b2ac86e1SJiro SEKIBA return nilfs_sync_super(sbi, flag); 308783f6184SRyusuke Konishi } 309783f6184SRyusuke Konishi 3107ecaa46cSRyusuke Konishi /** 3117ecaa46cSRyusuke Konishi * nilfs_cleanup_super() - write filesystem state for cleanup 3127ecaa46cSRyusuke Konishi * @sbi: nilfs_sb_info to be unmounted or degraded to read-only 3137ecaa46cSRyusuke Konishi * 3147ecaa46cSRyusuke Konishi * This function restores state flags in the on-disk super block. 3157ecaa46cSRyusuke Konishi * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the 3167ecaa46cSRyusuke Konishi * filesystem was not clean previously. 3177ecaa46cSRyusuke Konishi */ 3187ecaa46cSRyusuke Konishi int nilfs_cleanup_super(struct nilfs_sb_info *sbi) 3197ecaa46cSRyusuke Konishi { 320d26493b6SJiro SEKIBA struct nilfs_super_block **sbp; 321b2ac86e1SJiro SEKIBA int flag = NILFS_SB_COMMIT; 322d26493b6SJiro SEKIBA int ret = -EIO; 3237ecaa46cSRyusuke Konishi 324b2ac86e1SJiro SEKIBA sbp = nilfs_prepare_super(sbi, 0); 325d26493b6SJiro SEKIBA if (sbp) { 3267ecaa46cSRyusuke Konishi sbp[0]->s_state = cpu_to_le16(sbi->s_nilfs->ns_mount_state); 327b2ac86e1SJiro SEKIBA nilfs_set_log_cursor(sbp[0], sbi->s_nilfs); 328b2ac86e1SJiro SEKIBA if (sbp[1] && sbp[0]->s_last_cno == sbp[1]->s_last_cno) { 329b2ac86e1SJiro SEKIBA /* 330b2ac86e1SJiro SEKIBA * make the "clean" flag also to the opposite 331b2ac86e1SJiro SEKIBA * super block if both super blocks point to 332b2ac86e1SJiro SEKIBA * the same checkpoint. 333b2ac86e1SJiro SEKIBA */ 334b2ac86e1SJiro SEKIBA sbp[1]->s_state = sbp[0]->s_state; 335b2ac86e1SJiro SEKIBA flag = NILFS_SB_COMMIT_ALL; 336b2ac86e1SJiro SEKIBA } 337b2ac86e1SJiro SEKIBA ret = nilfs_commit_super(sbi, flag); 338d26493b6SJiro SEKIBA } 3397ecaa46cSRyusuke Konishi return ret; 3407ecaa46cSRyusuke Konishi } 3417ecaa46cSRyusuke Konishi 342783f6184SRyusuke Konishi static void nilfs_put_super(struct super_block *sb) 343783f6184SRyusuke Konishi { 344783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 345783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 346783f6184SRyusuke Konishi 347783f6184SRyusuke Konishi nilfs_detach_segment_constructor(sbi); 348783f6184SRyusuke Konishi 349783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 350783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 3517ecaa46cSRyusuke Konishi nilfs_cleanup_super(sbi); 352783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 353783f6184SRyusuke Konishi } 354783f6184SRyusuke Konishi 355f1e89c86SRyusuke Konishi iput(nilfs->ns_sufile); 356f1e89c86SRyusuke Konishi iput(nilfs->ns_cpfile); 357f1e89c86SRyusuke Konishi iput(nilfs->ns_dat); 358f1e89c86SRyusuke Konishi 359348fe8daSRyusuke 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 4061154ecbdSZhang Qiang down_read(&nilfs->ns_segctor_sem); 407783f6184SRyusuke Konishi err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp, 408783f6184SRyusuke Konishi &bh_cp); 4091154ecbdSZhang Qiang up_read(&nilfs->ns_segctor_sem); 410783f6184SRyusuke Konishi if (unlikely(err)) { 411783f6184SRyusuke Konishi if (err == -ENOENT || err == -EINVAL) { 412783f6184SRyusuke Konishi printk(KERN_ERR 413783f6184SRyusuke Konishi "NILFS: Invalid checkpoint " 414783f6184SRyusuke Konishi "(checkpoint number=%llu)\n", 415783f6184SRyusuke Konishi (unsigned long long)cno); 416783f6184SRyusuke Konishi err = -EINVAL; 417783f6184SRyusuke Konishi } 418783f6184SRyusuke Konishi goto failed; 419783f6184SRyusuke Konishi } 420f1e89c86SRyusuke Konishi 421f1e89c86SRyusuke Konishi err = nilfs_ifile_read(sbi->s_super, root, nilfs->ns_inode_size, 422f1e89c86SRyusuke Konishi &raw_cp->cp_ifile_inode, &root->ifile); 423f1e89c86SRyusuke Konishi if (err) 424783f6184SRyusuke Konishi goto failed_bh; 425b7c06342SRyusuke Konishi 426b7c06342SRyusuke Konishi atomic_set(&root->inodes_count, le64_to_cpu(raw_cp->cp_inodes_count)); 427b7c06342SRyusuke Konishi atomic_set(&root->blocks_count, le64_to_cpu(raw_cp->cp_blocks_count)); 428783f6184SRyusuke Konishi 429783f6184SRyusuke Konishi nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp); 4304d8d9293SRyusuke Konishi 431e912a5b6SRyusuke Konishi reuse: 4324d8d9293SRyusuke Konishi *rootp = root; 433783f6184SRyusuke Konishi return 0; 434783f6184SRyusuke Konishi 435783f6184SRyusuke Konishi failed_bh: 436783f6184SRyusuke Konishi nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp); 437783f6184SRyusuke Konishi failed: 4384d8d9293SRyusuke Konishi nilfs_put_root(root); 439783f6184SRyusuke Konishi 440783f6184SRyusuke Konishi return err; 441783f6184SRyusuke Konishi } 442783f6184SRyusuke Konishi 4435beb6e0bSRyusuke Konishi static int nilfs_freeze(struct super_block *sb) 4445beb6e0bSRyusuke Konishi { 4455beb6e0bSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 4465beb6e0bSRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 4475beb6e0bSRyusuke Konishi int err; 4485beb6e0bSRyusuke Konishi 4495beb6e0bSRyusuke Konishi if (sb->s_flags & MS_RDONLY) 4505beb6e0bSRyusuke Konishi return 0; 4515beb6e0bSRyusuke Konishi 4525beb6e0bSRyusuke Konishi /* Mark super block clean */ 4535beb6e0bSRyusuke Konishi down_write(&nilfs->ns_sem); 4545beb6e0bSRyusuke Konishi err = nilfs_cleanup_super(sbi); 4555beb6e0bSRyusuke Konishi up_write(&nilfs->ns_sem); 4565beb6e0bSRyusuke Konishi return err; 4575beb6e0bSRyusuke Konishi } 4585beb6e0bSRyusuke Konishi 4595beb6e0bSRyusuke Konishi static int nilfs_unfreeze(struct super_block *sb) 4605beb6e0bSRyusuke Konishi { 4615beb6e0bSRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 4625beb6e0bSRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 4635beb6e0bSRyusuke Konishi 4645beb6e0bSRyusuke Konishi if (sb->s_flags & MS_RDONLY) 4655beb6e0bSRyusuke Konishi return 0; 4665beb6e0bSRyusuke Konishi 4675beb6e0bSRyusuke Konishi down_write(&nilfs->ns_sem); 4685beb6e0bSRyusuke Konishi nilfs_setup_super(sbi, false); 4695beb6e0bSRyusuke Konishi up_write(&nilfs->ns_sem); 4705beb6e0bSRyusuke Konishi return 0; 4715beb6e0bSRyusuke Konishi } 4725beb6e0bSRyusuke Konishi 473783f6184SRyusuke Konishi static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf) 474783f6184SRyusuke Konishi { 475783f6184SRyusuke Konishi struct super_block *sb = dentry->d_sb; 476b7c06342SRyusuke Konishi struct nilfs_root *root = NILFS_I(dentry->d_inode)->i_root; 477b7c06342SRyusuke Konishi struct the_nilfs *nilfs = root->nilfs; 478c306af23SRyusuke Konishi u64 id = huge_encode_dev(sb->s_bdev->bd_dev); 479783f6184SRyusuke Konishi unsigned long long blocks; 480783f6184SRyusuke Konishi unsigned long overhead; 481783f6184SRyusuke Konishi unsigned long nrsvblocks; 482783f6184SRyusuke Konishi sector_t nfreeblocks; 483783f6184SRyusuke Konishi int err; 484783f6184SRyusuke Konishi 485783f6184SRyusuke Konishi /* 486783f6184SRyusuke Konishi * Compute all of the segment blocks 487783f6184SRyusuke Konishi * 488783f6184SRyusuke Konishi * The blocks before first segment and after last segment 489783f6184SRyusuke Konishi * are excluded. 490783f6184SRyusuke Konishi */ 491783f6184SRyusuke Konishi blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments 492783f6184SRyusuke Konishi - nilfs->ns_first_data_block; 493783f6184SRyusuke Konishi nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment; 494783f6184SRyusuke Konishi 495783f6184SRyusuke Konishi /* 496783f6184SRyusuke Konishi * Compute the overhead 497783f6184SRyusuke Konishi * 4987a65004bSRyusuke Konishi * When distributing meta data blocks outside segment structure, 499783f6184SRyusuke Konishi * We must count them as the overhead. 500783f6184SRyusuke Konishi */ 501783f6184SRyusuke Konishi overhead = 0; 502783f6184SRyusuke Konishi 503783f6184SRyusuke Konishi err = nilfs_count_free_blocks(nilfs, &nfreeblocks); 504783f6184SRyusuke Konishi if (unlikely(err)) 505783f6184SRyusuke Konishi return err; 506783f6184SRyusuke Konishi 507783f6184SRyusuke Konishi buf->f_type = NILFS_SUPER_MAGIC; 508783f6184SRyusuke Konishi buf->f_bsize = sb->s_blocksize; 509783f6184SRyusuke Konishi buf->f_blocks = blocks - overhead; 510783f6184SRyusuke Konishi buf->f_bfree = nfreeblocks; 511783f6184SRyusuke Konishi buf->f_bavail = (buf->f_bfree >= nrsvblocks) ? 512783f6184SRyusuke Konishi (buf->f_bfree - nrsvblocks) : 0; 513b7c06342SRyusuke Konishi buf->f_files = atomic_read(&root->inodes_count); 514783f6184SRyusuke Konishi buf->f_ffree = 0; /* nilfs_count_free_inodes(sb); */ 515783f6184SRyusuke Konishi buf->f_namelen = NILFS_NAME_LEN; 516c306af23SRyusuke Konishi buf->f_fsid.val[0] = (u32)id; 517c306af23SRyusuke Konishi buf->f_fsid.val[1] = (u32)(id >> 32); 518c306af23SRyusuke Konishi 519783f6184SRyusuke Konishi return 0; 520783f6184SRyusuke Konishi } 521783f6184SRyusuke Konishi 522b58a285bSJiro SEKIBA static int nilfs_show_options(struct seq_file *seq, struct vfsmount *vfs) 523b58a285bSJiro SEKIBA { 524b58a285bSJiro SEKIBA struct super_block *sb = vfs->mnt_sb; 525b58a285bSJiro SEKIBA struct nilfs_sb_info *sbi = NILFS_SB(sb); 526f11459adSRyusuke Konishi struct nilfs_root *root = NILFS_I(vfs->mnt_root->d_inode)->i_root; 527b58a285bSJiro SEKIBA 528b58a285bSJiro SEKIBA if (!nilfs_test_opt(sbi, BARRIER)) 529c6b4d57dSRyusuke Konishi seq_puts(seq, ",nobarrier"); 530f11459adSRyusuke Konishi if (root->cno != NILFS_CPTREE_CURRENT_CNO) 531f11459adSRyusuke Konishi seq_printf(seq, ",cp=%llu", (unsigned long long)root->cno); 532b58a285bSJiro SEKIBA if (nilfs_test_opt(sbi, ERRORS_PANIC)) 533c6b4d57dSRyusuke Konishi seq_puts(seq, ",errors=panic"); 534277a6a34SRyusuke Konishi if (nilfs_test_opt(sbi, ERRORS_CONT)) 535c6b4d57dSRyusuke Konishi seq_puts(seq, ",errors=continue"); 536b58a285bSJiro SEKIBA if (nilfs_test_opt(sbi, STRICT_ORDER)) 537c6b4d57dSRyusuke Konishi seq_puts(seq, ",order=strict"); 5380234576dSRyusuke Konishi if (nilfs_test_opt(sbi, NORECOVERY)) 539c6b4d57dSRyusuke Konishi seq_puts(seq, ",norecovery"); 540e902ec99SJiro SEKIBA if (nilfs_test_opt(sbi, DISCARD)) 541c6b4d57dSRyusuke Konishi seq_puts(seq, ",discard"); 542b58a285bSJiro SEKIBA 543b58a285bSJiro SEKIBA return 0; 544b58a285bSJiro SEKIBA } 545b58a285bSJiro SEKIBA 546b87221deSAlexey Dobriyan static const struct super_operations nilfs_sops = { 547783f6184SRyusuke Konishi .alloc_inode = nilfs_alloc_inode, 548783f6184SRyusuke Konishi .destroy_inode = nilfs_destroy_inode, 549783f6184SRyusuke Konishi .dirty_inode = nilfs_dirty_inode, 550783f6184SRyusuke Konishi /* .write_inode = nilfs_write_inode, */ 551783f6184SRyusuke Konishi /* .put_inode = nilfs_put_inode, */ 552783f6184SRyusuke Konishi /* .drop_inode = nilfs_drop_inode, */ 5536fd1e5c9SAl Viro .evict_inode = nilfs_evict_inode, 554783f6184SRyusuke Konishi .put_super = nilfs_put_super, 5551dfa2710SJiro SEKIBA /* .write_super = nilfs_write_super, */ 556783f6184SRyusuke Konishi .sync_fs = nilfs_sync_fs, 5575beb6e0bSRyusuke Konishi .freeze_fs = nilfs_freeze, 5585beb6e0bSRyusuke Konishi .unfreeze_fs = nilfs_unfreeze, 559783f6184SRyusuke Konishi /* .write_super_lockfs */ 560783f6184SRyusuke Konishi /* .unlockfs */ 561783f6184SRyusuke Konishi .statfs = nilfs_statfs, 562783f6184SRyusuke Konishi .remount_fs = nilfs_remount, 563783f6184SRyusuke Konishi /* .umount_begin */ 564b58a285bSJiro SEKIBA .show_options = nilfs_show_options 565783f6184SRyusuke Konishi }; 566783f6184SRyusuke Konishi 567783f6184SRyusuke Konishi enum { 568783f6184SRyusuke Konishi Opt_err_cont, Opt_err_panic, Opt_err_ro, 569773bc4f3SRyusuke Konishi Opt_barrier, Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery, 570802d3177SRyusuke Konishi Opt_discard, Opt_nodiscard, Opt_err, 571783f6184SRyusuke Konishi }; 572783f6184SRyusuke Konishi 573783f6184SRyusuke Konishi static match_table_t tokens = { 574783f6184SRyusuke Konishi {Opt_err_cont, "errors=continue"}, 575783f6184SRyusuke Konishi {Opt_err_panic, "errors=panic"}, 576783f6184SRyusuke Konishi {Opt_err_ro, "errors=remount-ro"}, 577773bc4f3SRyusuke Konishi {Opt_barrier, "barrier"}, 57891f1953bSJiro SEKIBA {Opt_nobarrier, "nobarrier"}, 579783f6184SRyusuke Konishi {Opt_snapshot, "cp=%u"}, 580783f6184SRyusuke Konishi {Opt_order, "order=%s"}, 5810234576dSRyusuke Konishi {Opt_norecovery, "norecovery"}, 582e902ec99SJiro SEKIBA {Opt_discard, "discard"}, 583802d3177SRyusuke Konishi {Opt_nodiscard, "nodiscard"}, 584783f6184SRyusuke Konishi {Opt_err, NULL} 585783f6184SRyusuke Konishi }; 586783f6184SRyusuke Konishi 5877c017457SRyusuke Konishi static int parse_options(char *options, struct super_block *sb, int is_remount) 588783f6184SRyusuke Konishi { 589783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 590783f6184SRyusuke Konishi char *p; 591783f6184SRyusuke Konishi substring_t args[MAX_OPT_ARGS]; 592783f6184SRyusuke Konishi 593783f6184SRyusuke Konishi if (!options) 594783f6184SRyusuke Konishi return 1; 595783f6184SRyusuke Konishi 596783f6184SRyusuke Konishi while ((p = strsep(&options, ",")) != NULL) { 597783f6184SRyusuke Konishi int token; 598783f6184SRyusuke Konishi if (!*p) 599783f6184SRyusuke Konishi continue; 600783f6184SRyusuke Konishi 601783f6184SRyusuke Konishi token = match_token(p, tokens, args); 602783f6184SRyusuke Konishi switch (token) { 603773bc4f3SRyusuke Konishi case Opt_barrier: 604773bc4f3SRyusuke Konishi nilfs_set_opt(sbi, BARRIER); 605773bc4f3SRyusuke Konishi break; 60691f1953bSJiro SEKIBA case Opt_nobarrier: 607783f6184SRyusuke Konishi nilfs_clear_opt(sbi, BARRIER); 608783f6184SRyusuke Konishi break; 609783f6184SRyusuke Konishi case Opt_order: 610783f6184SRyusuke Konishi if (strcmp(args[0].from, "relaxed") == 0) 611783f6184SRyusuke Konishi /* Ordered data semantics */ 612783f6184SRyusuke Konishi nilfs_clear_opt(sbi, STRICT_ORDER); 613783f6184SRyusuke Konishi else if (strcmp(args[0].from, "strict") == 0) 614783f6184SRyusuke Konishi /* Strict in-order semantics */ 615783f6184SRyusuke Konishi nilfs_set_opt(sbi, STRICT_ORDER); 616783f6184SRyusuke Konishi else 617783f6184SRyusuke Konishi return 0; 618783f6184SRyusuke Konishi break; 619783f6184SRyusuke Konishi case Opt_err_panic: 620783f6184SRyusuke Konishi nilfs_write_opt(sbi, ERROR_MODE, ERRORS_PANIC); 621783f6184SRyusuke Konishi break; 622783f6184SRyusuke Konishi case Opt_err_ro: 623783f6184SRyusuke Konishi nilfs_write_opt(sbi, ERROR_MODE, ERRORS_RO); 624783f6184SRyusuke Konishi break; 625783f6184SRyusuke Konishi case Opt_err_cont: 626783f6184SRyusuke Konishi nilfs_write_opt(sbi, ERROR_MODE, ERRORS_CONT); 627783f6184SRyusuke Konishi break; 628783f6184SRyusuke Konishi case Opt_snapshot: 6297c017457SRyusuke Konishi if (is_remount) { 6307c017457SRyusuke Konishi printk(KERN_ERR 631f11459adSRyusuke Konishi "NILFS: \"%s\" option is invalid " 632f11459adSRyusuke Konishi "for remount.\n", p); 6337c017457SRyusuke Konishi return 0; 6347c017457SRyusuke Konishi } 6357c017457SRyusuke Konishi break; 6360234576dSRyusuke Konishi case Opt_norecovery: 6370234576dSRyusuke Konishi nilfs_set_opt(sbi, NORECOVERY); 6380234576dSRyusuke Konishi break; 639e902ec99SJiro SEKIBA case Opt_discard: 640e902ec99SJiro SEKIBA nilfs_set_opt(sbi, DISCARD); 641e902ec99SJiro SEKIBA break; 642802d3177SRyusuke Konishi case Opt_nodiscard: 643802d3177SRyusuke Konishi nilfs_clear_opt(sbi, DISCARD); 644802d3177SRyusuke Konishi break; 645783f6184SRyusuke Konishi default: 646783f6184SRyusuke Konishi printk(KERN_ERR 647783f6184SRyusuke Konishi "NILFS: Unrecognized mount option \"%s\"\n", p); 648783f6184SRyusuke Konishi return 0; 649783f6184SRyusuke Konishi } 650783f6184SRyusuke Konishi } 651783f6184SRyusuke Konishi return 1; 652783f6184SRyusuke Konishi } 653783f6184SRyusuke Konishi 654783f6184SRyusuke Konishi static inline void 655783f6184SRyusuke Konishi nilfs_set_default_options(struct nilfs_sb_info *sbi, 656783f6184SRyusuke Konishi struct nilfs_super_block *sbp) 657783f6184SRyusuke Konishi { 658783f6184SRyusuke Konishi sbi->s_mount_opt = 659277a6a34SRyusuke Konishi NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER; 660783f6184SRyusuke Konishi } 661783f6184SRyusuke Konishi 6625beb6e0bSRyusuke Konishi static int nilfs_setup_super(struct nilfs_sb_info *sbi, int is_mount) 663783f6184SRyusuke Konishi { 664783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 665d26493b6SJiro SEKIBA struct nilfs_super_block **sbp; 666d26493b6SJiro SEKIBA int max_mnt_count; 667d26493b6SJiro SEKIBA int mnt_count; 668783f6184SRyusuke Konishi 669d26493b6SJiro SEKIBA /* nilfs->ns_sem must be locked by the caller. */ 670b2ac86e1SJiro SEKIBA sbp = nilfs_prepare_super(sbi, 0); 671d26493b6SJiro SEKIBA if (!sbp) 672d26493b6SJiro SEKIBA return -EIO; 673d26493b6SJiro SEKIBA 6745beb6e0bSRyusuke Konishi if (!is_mount) 6755beb6e0bSRyusuke Konishi goto skip_mount_setup; 6765beb6e0bSRyusuke Konishi 677d26493b6SJiro SEKIBA max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count); 678d26493b6SJiro SEKIBA mnt_count = le16_to_cpu(sbp[0]->s_mnt_count); 679d26493b6SJiro SEKIBA 680f50a4c81SRyusuke Konishi if (nilfs->ns_mount_state & NILFS_ERROR_FS) { 681783f6184SRyusuke Konishi printk(KERN_WARNING 682783f6184SRyusuke Konishi "NILFS warning: mounting fs with errors\n"); 683783f6184SRyusuke Konishi #if 0 684783f6184SRyusuke Konishi } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) { 685783f6184SRyusuke Konishi printk(KERN_WARNING 686783f6184SRyusuke Konishi "NILFS warning: maximal mount count reached\n"); 687783f6184SRyusuke Konishi #endif 688783f6184SRyusuke Konishi } 689783f6184SRyusuke Konishi if (!max_mnt_count) 690d26493b6SJiro SEKIBA sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT); 691783f6184SRyusuke Konishi 692d26493b6SJiro SEKIBA sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1); 6935beb6e0bSRyusuke Konishi sbp[0]->s_mtime = cpu_to_le64(get_seconds()); 6945beb6e0bSRyusuke Konishi 6955beb6e0bSRyusuke Konishi skip_mount_setup: 696d26493b6SJiro SEKIBA sbp[0]->s_state = 697d26493b6SJiro SEKIBA cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS); 698b2ac86e1SJiro SEKIBA /* synchronize sbp[1] with sbp[0] */ 699b2ac86e1SJiro SEKIBA memcpy(sbp[1], sbp[0], nilfs->ns_sbsize); 700b2ac86e1SJiro SEKIBA return nilfs_commit_super(sbi, NILFS_SB_COMMIT_ALL); 701783f6184SRyusuke Konishi } 702783f6184SRyusuke Konishi 703e339ad31SRyusuke Konishi struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb, 704e339ad31SRyusuke Konishi u64 pos, int blocksize, 705e339ad31SRyusuke Konishi struct buffer_head **pbh) 706783f6184SRyusuke Konishi { 707e339ad31SRyusuke Konishi unsigned long long sb_index = pos; 708e339ad31SRyusuke Konishi unsigned long offset; 709783f6184SRyusuke Konishi 710e339ad31SRyusuke Konishi offset = do_div(sb_index, blocksize); 711783f6184SRyusuke Konishi *pbh = sb_bread(sb, sb_index); 712e339ad31SRyusuke Konishi if (!*pbh) 713783f6184SRyusuke Konishi return NULL; 714783f6184SRyusuke Konishi return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset); 715783f6184SRyusuke Konishi } 716783f6184SRyusuke Konishi 717783f6184SRyusuke Konishi int nilfs_store_magic_and_option(struct super_block *sb, 718783f6184SRyusuke Konishi struct nilfs_super_block *sbp, 719783f6184SRyusuke Konishi char *data) 720783f6184SRyusuke Konishi { 721783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 722783f6184SRyusuke Konishi 723783f6184SRyusuke Konishi sb->s_magic = le16_to_cpu(sbp->s_magic); 724783f6184SRyusuke Konishi 725783f6184SRyusuke Konishi /* FS independent flags */ 726783f6184SRyusuke Konishi #ifdef NILFS_ATIME_DISABLE 727783f6184SRyusuke Konishi sb->s_flags |= MS_NOATIME; 728783f6184SRyusuke Konishi #endif 729783f6184SRyusuke Konishi 730783f6184SRyusuke Konishi nilfs_set_default_options(sbi, sbp); 731783f6184SRyusuke Konishi 732783f6184SRyusuke Konishi sbi->s_resuid = le16_to_cpu(sbp->s_def_resuid); 733783f6184SRyusuke Konishi sbi->s_resgid = le16_to_cpu(sbp->s_def_resgid); 734783f6184SRyusuke Konishi sbi->s_interval = le32_to_cpu(sbp->s_c_interval); 735783f6184SRyusuke Konishi sbi->s_watermark = le32_to_cpu(sbp->s_c_block_max); 736783f6184SRyusuke Konishi 7377c017457SRyusuke Konishi return !parse_options(data, sb, 0) ? -EINVAL : 0 ; 738783f6184SRyusuke Konishi } 739783f6184SRyusuke Konishi 740c5ca48aaSRyusuke Konishi int nilfs_check_feature_compatibility(struct super_block *sb, 741c5ca48aaSRyusuke Konishi struct nilfs_super_block *sbp) 742c5ca48aaSRyusuke Konishi { 743c5ca48aaSRyusuke Konishi __u64 features; 744c5ca48aaSRyusuke Konishi 745c5ca48aaSRyusuke Konishi features = le64_to_cpu(sbp->s_feature_incompat) & 746c5ca48aaSRyusuke Konishi ~NILFS_FEATURE_INCOMPAT_SUPP; 747c5ca48aaSRyusuke Konishi if (features) { 748c5ca48aaSRyusuke Konishi printk(KERN_ERR "NILFS: couldn't mount because of unsupported " 749c5ca48aaSRyusuke Konishi "optional features (%llx)\n", 750c5ca48aaSRyusuke Konishi (unsigned long long)features); 751c5ca48aaSRyusuke Konishi return -EINVAL; 752c5ca48aaSRyusuke Konishi } 753c5ca48aaSRyusuke Konishi features = le64_to_cpu(sbp->s_feature_compat_ro) & 754c5ca48aaSRyusuke Konishi ~NILFS_FEATURE_COMPAT_RO_SUPP; 755c5ca48aaSRyusuke Konishi if (!(sb->s_flags & MS_RDONLY) && features) { 756c5ca48aaSRyusuke Konishi printk(KERN_ERR "NILFS: couldn't mount RDWR because of " 757c5ca48aaSRyusuke Konishi "unsupported optional features (%llx)\n", 758c5ca48aaSRyusuke Konishi (unsigned long long)features); 759c5ca48aaSRyusuke Konishi return -EINVAL; 760c5ca48aaSRyusuke Konishi } 761c5ca48aaSRyusuke Konishi return 0; 762c5ca48aaSRyusuke Konishi } 763c5ca48aaSRyusuke Konishi 764367ea334SRyusuke Konishi static int nilfs_get_root_dentry(struct super_block *sb, 765367ea334SRyusuke Konishi struct nilfs_root *root, 766367ea334SRyusuke Konishi struct dentry **root_dentry) 767367ea334SRyusuke Konishi { 768367ea334SRyusuke Konishi struct inode *inode; 769367ea334SRyusuke Konishi struct dentry *dentry; 770367ea334SRyusuke Konishi int ret = 0; 771367ea334SRyusuke Konishi 772367ea334SRyusuke Konishi inode = nilfs_iget(sb, root, NILFS_ROOT_INO); 773367ea334SRyusuke Konishi if (IS_ERR(inode)) { 774367ea334SRyusuke Konishi printk(KERN_ERR "NILFS: get root inode failed\n"); 775367ea334SRyusuke Konishi ret = PTR_ERR(inode); 776367ea334SRyusuke Konishi goto out; 777367ea334SRyusuke Konishi } 778367ea334SRyusuke Konishi if (!S_ISDIR(inode->i_mode) || !inode->i_blocks || !inode->i_size) { 779367ea334SRyusuke Konishi iput(inode); 780367ea334SRyusuke Konishi printk(KERN_ERR "NILFS: corrupt root inode.\n"); 781367ea334SRyusuke Konishi ret = -EINVAL; 782367ea334SRyusuke Konishi goto out; 783367ea334SRyusuke Konishi } 784367ea334SRyusuke Konishi 785f11459adSRyusuke Konishi if (root->cno == NILFS_CPTREE_CURRENT_CNO) { 786f11459adSRyusuke Konishi dentry = d_find_alias(inode); 787f11459adSRyusuke Konishi if (!dentry) { 788367ea334SRyusuke Konishi dentry = d_alloc_root(inode); 789367ea334SRyusuke Konishi if (!dentry) { 790367ea334SRyusuke Konishi iput(inode); 791367ea334SRyusuke Konishi ret = -ENOMEM; 792f11459adSRyusuke Konishi goto failed_dentry; 793f11459adSRyusuke Konishi } 794f11459adSRyusuke Konishi } else { 795f11459adSRyusuke Konishi iput(inode); 796f11459adSRyusuke Konishi } 797f11459adSRyusuke Konishi } else { 798f11459adSRyusuke Konishi dentry = d_obtain_alias(inode); 799f11459adSRyusuke Konishi if (IS_ERR(dentry)) { 800f11459adSRyusuke Konishi ret = PTR_ERR(dentry); 801f11459adSRyusuke Konishi goto failed_dentry; 802f11459adSRyusuke Konishi } 803367ea334SRyusuke Konishi } 804367ea334SRyusuke Konishi *root_dentry = dentry; 805367ea334SRyusuke Konishi out: 806367ea334SRyusuke Konishi return ret; 807f11459adSRyusuke Konishi 808f11459adSRyusuke Konishi failed_dentry: 809f11459adSRyusuke Konishi printk(KERN_ERR "NILFS: get root dentry failed\n"); 810f11459adSRyusuke Konishi goto out; 811367ea334SRyusuke Konishi } 812367ea334SRyusuke Konishi 813ab4d8f7eSRyusuke Konishi static int nilfs_attach_snapshot(struct super_block *s, __u64 cno, 814ab4d8f7eSRyusuke Konishi struct dentry **root_dentry) 815ab4d8f7eSRyusuke Konishi { 816ab4d8f7eSRyusuke Konishi struct the_nilfs *nilfs = NILFS_SB(s)->s_nilfs; 817ab4d8f7eSRyusuke Konishi struct nilfs_root *root; 818ab4d8f7eSRyusuke Konishi int ret; 819ab4d8f7eSRyusuke Konishi 820ab4d8f7eSRyusuke Konishi down_read(&nilfs->ns_segctor_sem); 821ab4d8f7eSRyusuke Konishi ret = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, cno); 822ab4d8f7eSRyusuke Konishi up_read(&nilfs->ns_segctor_sem); 823ab4d8f7eSRyusuke Konishi if (ret < 0) { 824ab4d8f7eSRyusuke Konishi ret = (ret == -ENOENT) ? -EINVAL : ret; 825ab4d8f7eSRyusuke Konishi goto out; 826ab4d8f7eSRyusuke Konishi } else if (!ret) { 827ab4d8f7eSRyusuke Konishi printk(KERN_ERR "NILFS: The specified checkpoint is " 828ab4d8f7eSRyusuke Konishi "not a snapshot (checkpoint number=%llu).\n", 829ab4d8f7eSRyusuke Konishi (unsigned long long)cno); 830ab4d8f7eSRyusuke Konishi ret = -EINVAL; 831ab4d8f7eSRyusuke Konishi goto out; 832ab4d8f7eSRyusuke Konishi } 833ab4d8f7eSRyusuke Konishi 834ab4d8f7eSRyusuke Konishi ret = nilfs_attach_checkpoint(NILFS_SB(s), cno, false, &root); 835ab4d8f7eSRyusuke Konishi if (ret) { 836ab4d8f7eSRyusuke Konishi printk(KERN_ERR "NILFS: error loading snapshot " 837ab4d8f7eSRyusuke Konishi "(checkpoint number=%llu).\n", 838ab4d8f7eSRyusuke Konishi (unsigned long long)cno); 839ab4d8f7eSRyusuke Konishi goto out; 840ab4d8f7eSRyusuke Konishi } 841ab4d8f7eSRyusuke Konishi ret = nilfs_get_root_dentry(s, root, root_dentry); 842ab4d8f7eSRyusuke Konishi nilfs_put_root(root); 843ab4d8f7eSRyusuke Konishi out: 844ab4d8f7eSRyusuke Konishi return ret; 845ab4d8f7eSRyusuke Konishi } 846ab4d8f7eSRyusuke Konishi 847f11459adSRyusuke Konishi static int nilfs_tree_was_touched(struct dentry *root_dentry) 848f11459adSRyusuke Konishi { 849f11459adSRyusuke Konishi return atomic_read(&root_dentry->d_count) > 1; 850f11459adSRyusuke Konishi } 851f11459adSRyusuke Konishi 852f11459adSRyusuke Konishi /** 853f11459adSRyusuke Konishi * nilfs_try_to_shrink_tree() - try to shrink dentries of a checkpoint 854f11459adSRyusuke Konishi * @root_dentry: root dentry of the tree to be shrunk 855f11459adSRyusuke Konishi * 856f11459adSRyusuke Konishi * This function returns true if the tree was in-use. 857f11459adSRyusuke Konishi */ 858f11459adSRyusuke Konishi static int nilfs_try_to_shrink_tree(struct dentry *root_dentry) 859f11459adSRyusuke Konishi { 860f11459adSRyusuke Konishi if (have_submounts(root_dentry)) 861f11459adSRyusuke Konishi return true; 862f11459adSRyusuke Konishi shrink_dcache_parent(root_dentry); 863f11459adSRyusuke Konishi return nilfs_tree_was_touched(root_dentry); 864f11459adSRyusuke Konishi } 865f11459adSRyusuke Konishi 866032dbb3bSRyusuke Konishi int nilfs_checkpoint_is_mounted(struct super_block *sb, __u64 cno) 867032dbb3bSRyusuke Konishi { 868032dbb3bSRyusuke Konishi struct the_nilfs *nilfs = NILFS_SB(sb)->s_nilfs; 869032dbb3bSRyusuke Konishi struct nilfs_root *root; 870032dbb3bSRyusuke Konishi struct inode *inode; 871032dbb3bSRyusuke Konishi struct dentry *dentry; 872032dbb3bSRyusuke Konishi int ret; 873032dbb3bSRyusuke Konishi 874032dbb3bSRyusuke Konishi if (cno < 0 || cno > nilfs->ns_cno) 875032dbb3bSRyusuke Konishi return false; 876032dbb3bSRyusuke Konishi 877032dbb3bSRyusuke Konishi if (cno >= nilfs_last_cno(nilfs)) 878032dbb3bSRyusuke Konishi return true; /* protect recent checkpoints */ 879032dbb3bSRyusuke Konishi 880032dbb3bSRyusuke Konishi ret = false; 881032dbb3bSRyusuke Konishi root = nilfs_lookup_root(NILFS_SB(sb)->s_nilfs, cno); 882032dbb3bSRyusuke Konishi if (root) { 883032dbb3bSRyusuke Konishi inode = nilfs_ilookup(sb, root, NILFS_ROOT_INO); 884032dbb3bSRyusuke Konishi if (inode) { 885032dbb3bSRyusuke Konishi dentry = d_find_alias(inode); 886032dbb3bSRyusuke Konishi if (dentry) { 887032dbb3bSRyusuke Konishi if (nilfs_tree_was_touched(dentry)) 888032dbb3bSRyusuke Konishi ret = nilfs_try_to_shrink_tree(dentry); 889032dbb3bSRyusuke Konishi dput(dentry); 890032dbb3bSRyusuke Konishi } 891032dbb3bSRyusuke Konishi iput(inode); 892032dbb3bSRyusuke Konishi } 893032dbb3bSRyusuke Konishi nilfs_put_root(root); 894032dbb3bSRyusuke Konishi } 895032dbb3bSRyusuke Konishi return ret; 896032dbb3bSRyusuke Konishi } 897032dbb3bSRyusuke Konishi 898783f6184SRyusuke Konishi /** 899783f6184SRyusuke Konishi * nilfs_fill_super() - initialize a super block instance 900783f6184SRyusuke Konishi * @sb: super_block 901783f6184SRyusuke Konishi * @data: mount options 902783f6184SRyusuke Konishi * @silent: silent mode flag 903783f6184SRyusuke Konishi * 904aa7dfb89SRyusuke Konishi * This function is called exclusively by nilfs->ns_mount_mutex. 905783f6184SRyusuke Konishi * So, the recovery process is protected from other simultaneous mounts. 906783f6184SRyusuke Konishi */ 907783f6184SRyusuke Konishi static int 908348fe8daSRyusuke Konishi nilfs_fill_super(struct super_block *sb, void *data, int silent) 909783f6184SRyusuke Konishi { 910348fe8daSRyusuke Konishi struct the_nilfs *nilfs; 911783f6184SRyusuke Konishi struct nilfs_sb_info *sbi; 9124d8d9293SRyusuke Konishi struct nilfs_root *fsroot; 913026a7d63SRyusuke Konishi struct backing_dev_info *bdi; 914783f6184SRyusuke Konishi __u64 cno; 915ab4d8f7eSRyusuke Konishi int err; 916783f6184SRyusuke Konishi 917783f6184SRyusuke Konishi sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); 918783f6184SRyusuke Konishi if (!sbi) 919783f6184SRyusuke Konishi return -ENOMEM; 920783f6184SRyusuke Konishi 921783f6184SRyusuke Konishi sb->s_fs_info = sbi; 922783f6184SRyusuke Konishi sbi->s_super = sb; 923783f6184SRyusuke Konishi 924348fe8daSRyusuke Konishi nilfs = alloc_nilfs(sb->s_bdev); 925348fe8daSRyusuke Konishi if (!nilfs) { 926348fe8daSRyusuke Konishi err = -ENOMEM; 927348fe8daSRyusuke Konishi goto failed_sbi; 928348fe8daSRyusuke Konishi } 929348fe8daSRyusuke Konishi sbi->s_nilfs = nilfs; 930348fe8daSRyusuke Konishi 931783f6184SRyusuke Konishi err = init_nilfs(nilfs, sbi, (char *)data); 932783f6184SRyusuke Konishi if (err) 933348fe8daSRyusuke Konishi goto failed_nilfs; 934783f6184SRyusuke Konishi 935783f6184SRyusuke Konishi spin_lock_init(&sbi->s_inode_lock); 936783f6184SRyusuke Konishi INIT_LIST_HEAD(&sbi->s_dirty_files); 937783f6184SRyusuke Konishi 938783f6184SRyusuke Konishi /* 939783f6184SRyusuke Konishi * Following initialization is overlapped because 940783f6184SRyusuke Konishi * nilfs_sb_info structure has been cleared at the beginning. 941783f6184SRyusuke Konishi * But we reserve them to keep our interest and make ready 942783f6184SRyusuke Konishi * for the future change. 943783f6184SRyusuke Konishi */ 944783f6184SRyusuke Konishi get_random_bytes(&sbi->s_next_generation, 945783f6184SRyusuke Konishi sizeof(sbi->s_next_generation)); 946783f6184SRyusuke Konishi spin_lock_init(&sbi->s_next_gen_lock); 947783f6184SRyusuke Konishi 948783f6184SRyusuke Konishi sb->s_op = &nilfs_sops; 949783f6184SRyusuke Konishi sb->s_export_op = &nilfs_export_ops; 950783f6184SRyusuke Konishi sb->s_root = NULL; 95161239230SRyusuke Konishi sb->s_time_gran = 1; 952026a7d63SRyusuke Konishi 953026a7d63SRyusuke Konishi bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info; 954026a7d63SRyusuke Konishi sb->s_bdi = bdi ? : &default_backing_dev_info; 955783f6184SRyusuke Konishi 956783f6184SRyusuke Konishi err = load_nilfs(nilfs, sbi); 957783f6184SRyusuke Konishi if (err) 958348fe8daSRyusuke Konishi goto failed_nilfs; 959f50a4c81SRyusuke Konishi 960ab4d8f7eSRyusuke Konishi cno = nilfs_last_cno(nilfs); 961ab4d8f7eSRyusuke Konishi err = nilfs_attach_checkpoint(sbi, cno, true, &fsroot); 962783f6184SRyusuke Konishi if (err) { 963f11459adSRyusuke Konishi printk(KERN_ERR "NILFS: error loading last checkpoint " 964783f6184SRyusuke Konishi "(checkpoint number=%llu).\n", (unsigned long long)cno); 965f1e89c86SRyusuke Konishi goto failed_unload; 966783f6184SRyusuke Konishi } 967783f6184SRyusuke Konishi 968783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 969e912a5b6SRyusuke Konishi err = nilfs_attach_segment_constructor(sbi, fsroot); 970783f6184SRyusuke Konishi if (err) 971783f6184SRyusuke Konishi goto failed_checkpoint; 972783f6184SRyusuke Konishi } 973783f6184SRyusuke Konishi 974367ea334SRyusuke Konishi err = nilfs_get_root_dentry(sb, fsroot, &sb->s_root); 975367ea334SRyusuke Konishi if (err) 976783f6184SRyusuke Konishi goto failed_segctor; 977783f6184SRyusuke Konishi 9784d8d9293SRyusuke Konishi nilfs_put_root(fsroot); 9794d8d9293SRyusuke Konishi 980783f6184SRyusuke Konishi if (!(sb->s_flags & MS_RDONLY)) { 981783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 9825beb6e0bSRyusuke Konishi nilfs_setup_super(sbi, true); 983783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 984783f6184SRyusuke Konishi } 985783f6184SRyusuke Konishi 986783f6184SRyusuke Konishi return 0; 987783f6184SRyusuke Konishi 988783f6184SRyusuke Konishi failed_segctor: 989783f6184SRyusuke Konishi nilfs_detach_segment_constructor(sbi); 990783f6184SRyusuke Konishi 991783f6184SRyusuke Konishi failed_checkpoint: 9924d8d9293SRyusuke Konishi nilfs_put_root(fsroot); 993783f6184SRyusuke Konishi 994f1e89c86SRyusuke Konishi failed_unload: 995f1e89c86SRyusuke Konishi iput(nilfs->ns_sufile); 996f1e89c86SRyusuke Konishi iput(nilfs->ns_cpfile); 997f1e89c86SRyusuke Konishi iput(nilfs->ns_dat); 998f1e89c86SRyusuke Konishi 999348fe8daSRyusuke Konishi failed_nilfs: 1000348fe8daSRyusuke Konishi destroy_nilfs(nilfs); 1001348fe8daSRyusuke Konishi 1002783f6184SRyusuke Konishi failed_sbi: 1003783f6184SRyusuke Konishi sb->s_fs_info = NULL; 1004f11459adSRyusuke Konishi kfree(sbi); 1005783f6184SRyusuke Konishi return err; 1006783f6184SRyusuke Konishi } 1007783f6184SRyusuke Konishi 1008783f6184SRyusuke Konishi static int nilfs_remount(struct super_block *sb, int *flags, char *data) 1009783f6184SRyusuke Konishi { 1010783f6184SRyusuke Konishi struct nilfs_sb_info *sbi = NILFS_SB(sb); 1011783f6184SRyusuke Konishi struct the_nilfs *nilfs = sbi->s_nilfs; 1012783f6184SRyusuke Konishi unsigned long old_sb_flags; 1013783f6184SRyusuke Konishi struct nilfs_mount_options old_opts; 1014f11459adSRyusuke Konishi int err; 1015783f6184SRyusuke Konishi 1016783f6184SRyusuke Konishi old_sb_flags = sb->s_flags; 1017783f6184SRyusuke Konishi old_opts.mount_opt = sbi->s_mount_opt; 1018783f6184SRyusuke Konishi 10197c017457SRyusuke Konishi if (!parse_options(data, sb, 1)) { 1020783f6184SRyusuke Konishi err = -EINVAL; 1021783f6184SRyusuke Konishi goto restore_opts; 1022783f6184SRyusuke Konishi } 1023783f6184SRyusuke Konishi sb->s_flags = (sb->s_flags & ~MS_POSIXACL); 1024783f6184SRyusuke Konishi 1025d240e067SRyusuke Konishi err = -EINVAL; 1026783f6184SRyusuke Konishi 10270234576dSRyusuke Konishi if (!nilfs_valid_fs(nilfs)) { 10280234576dSRyusuke Konishi printk(KERN_WARNING "NILFS (device %s): couldn't " 10290234576dSRyusuke Konishi "remount because the filesystem is in an " 10300234576dSRyusuke Konishi "incomplete recovery state.\n", sb->s_id); 10310234576dSRyusuke Konishi goto restore_opts; 10320234576dSRyusuke Konishi } 10330234576dSRyusuke Konishi 1034783f6184SRyusuke Konishi if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) 1035783f6184SRyusuke Konishi goto out; 1036783f6184SRyusuke Konishi if (*flags & MS_RDONLY) { 1037783f6184SRyusuke Konishi /* Shutting down the segment constructor */ 1038783f6184SRyusuke Konishi nilfs_detach_segment_constructor(sbi); 1039783f6184SRyusuke Konishi sb->s_flags |= MS_RDONLY; 1040783f6184SRyusuke Konishi 1041783f6184SRyusuke Konishi /* 1042783f6184SRyusuke Konishi * Remounting a valid RW partition RDONLY, so set 1043783f6184SRyusuke Konishi * the RDONLY flag and then mark the partition as valid again. 1044783f6184SRyusuke Konishi */ 1045783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 10467ecaa46cSRyusuke Konishi nilfs_cleanup_super(sbi); 1047783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 1048783f6184SRyusuke Konishi } else { 1049c5ca48aaSRyusuke Konishi __u64 features; 1050e912a5b6SRyusuke Konishi struct nilfs_root *root; 1051c5ca48aaSRyusuke Konishi 1052783f6184SRyusuke Konishi /* 1053783f6184SRyusuke Konishi * Mounting a RDONLY partition read-write, so reread and 1054783f6184SRyusuke Konishi * store the current valid flag. (It may have been changed 1055783f6184SRyusuke Konishi * by fsck since we originally mounted the partition.) 1056783f6184SRyusuke Konishi */ 1057c5ca48aaSRyusuke Konishi down_read(&nilfs->ns_sem); 1058c5ca48aaSRyusuke Konishi features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) & 1059c5ca48aaSRyusuke Konishi ~NILFS_FEATURE_COMPAT_RO_SUPP; 1060c5ca48aaSRyusuke Konishi up_read(&nilfs->ns_sem); 1061c5ca48aaSRyusuke Konishi if (features) { 1062c5ca48aaSRyusuke Konishi printk(KERN_WARNING "NILFS (device %s): couldn't " 1063c5ca48aaSRyusuke Konishi "remount RDWR because of unsupported optional " 1064c5ca48aaSRyusuke Konishi "features (%llx)\n", 1065c5ca48aaSRyusuke Konishi sb->s_id, (unsigned long long)features); 1066c5ca48aaSRyusuke Konishi err = -EROFS; 1067c5ca48aaSRyusuke Konishi goto restore_opts; 1068c5ca48aaSRyusuke Konishi } 1069c5ca48aaSRyusuke Konishi 1070783f6184SRyusuke Konishi sb->s_flags &= ~MS_RDONLY; 1071783f6184SRyusuke Konishi 1072e912a5b6SRyusuke Konishi root = NILFS_I(sb->s_root->d_inode)->i_root; 1073e912a5b6SRyusuke Konishi err = nilfs_attach_segment_constructor(sbi, root); 1074783f6184SRyusuke Konishi if (err) 1075e59399d0SRyusuke Konishi goto restore_opts; 1076783f6184SRyusuke Konishi 1077783f6184SRyusuke Konishi down_write(&nilfs->ns_sem); 10785beb6e0bSRyusuke Konishi nilfs_setup_super(sbi, true); 1079783f6184SRyusuke Konishi up_write(&nilfs->ns_sem); 1080783f6184SRyusuke Konishi } 1081783f6184SRyusuke Konishi out: 1082783f6184SRyusuke Konishi return 0; 1083783f6184SRyusuke Konishi 1084783f6184SRyusuke Konishi restore_opts: 1085783f6184SRyusuke Konishi sb->s_flags = old_sb_flags; 1086783f6184SRyusuke Konishi sbi->s_mount_opt = old_opts.mount_opt; 1087783f6184SRyusuke Konishi return err; 1088783f6184SRyusuke Konishi } 1089783f6184SRyusuke Konishi 1090783f6184SRyusuke Konishi struct nilfs_super_data { 1091783f6184SRyusuke Konishi struct block_device *bdev; 10926dd47406SRyusuke Konishi struct nilfs_sb_info *sbi; 1093783f6184SRyusuke Konishi __u64 cno; 1094783f6184SRyusuke Konishi int flags; 1095783f6184SRyusuke Konishi }; 1096783f6184SRyusuke Konishi 1097783f6184SRyusuke Konishi /** 1098783f6184SRyusuke Konishi * nilfs_identify - pre-read mount options needed to identify mount instance 1099783f6184SRyusuke Konishi * @data: mount options 1100783f6184SRyusuke Konishi * @sd: nilfs_super_data 1101783f6184SRyusuke Konishi */ 1102783f6184SRyusuke Konishi static int nilfs_identify(char *data, struct nilfs_super_data *sd) 1103783f6184SRyusuke Konishi { 1104783f6184SRyusuke Konishi char *p, *options = data; 1105783f6184SRyusuke Konishi substring_t args[MAX_OPT_ARGS]; 1106c05dbfc2SRyusuke Konishi int token; 1107783f6184SRyusuke Konishi int ret = 0; 1108783f6184SRyusuke Konishi 1109783f6184SRyusuke Konishi do { 1110783f6184SRyusuke Konishi p = strsep(&options, ","); 1111783f6184SRyusuke Konishi if (p != NULL && *p) { 1112783f6184SRyusuke Konishi token = match_token(p, tokens, args); 1113783f6184SRyusuke Konishi if (token == Opt_snapshot) { 1114c05dbfc2SRyusuke Konishi if (!(sd->flags & MS_RDONLY)) { 1115783f6184SRyusuke Konishi ret++; 1116c05dbfc2SRyusuke Konishi } else { 1117c05dbfc2SRyusuke Konishi sd->cno = simple_strtoull(args[0].from, 1118c05dbfc2SRyusuke Konishi NULL, 0); 1119c05dbfc2SRyusuke Konishi /* 1120c05dbfc2SRyusuke Konishi * No need to see the end pointer; 1121c05dbfc2SRyusuke Konishi * match_token() has done syntax 1122c05dbfc2SRyusuke Konishi * checking. 1123c05dbfc2SRyusuke Konishi */ 1124c05dbfc2SRyusuke Konishi if (sd->cno == 0) 1125783f6184SRyusuke Konishi ret++; 1126783f6184SRyusuke Konishi } 1127783f6184SRyusuke Konishi } 1128783f6184SRyusuke Konishi if (ret) 1129783f6184SRyusuke Konishi printk(KERN_ERR 1130783f6184SRyusuke Konishi "NILFS: invalid mount option: %s\n", p); 1131783f6184SRyusuke Konishi } 1132783f6184SRyusuke Konishi if (!options) 1133783f6184SRyusuke Konishi break; 1134783f6184SRyusuke Konishi BUG_ON(options == data); 1135783f6184SRyusuke Konishi *(options - 1) = ','; 1136783f6184SRyusuke Konishi } while (!ret); 1137783f6184SRyusuke Konishi return ret; 1138783f6184SRyusuke Konishi } 1139783f6184SRyusuke Konishi 1140783f6184SRyusuke Konishi static int nilfs_set_bdev_super(struct super_block *s, void *data) 1141783f6184SRyusuke Konishi { 1142f11459adSRyusuke Konishi s->s_bdev = data; 1143783f6184SRyusuke Konishi s->s_dev = s->s_bdev->bd_dev; 1144783f6184SRyusuke Konishi return 0; 1145783f6184SRyusuke Konishi } 1146783f6184SRyusuke Konishi 1147783f6184SRyusuke Konishi static int nilfs_test_bdev_super(struct super_block *s, void *data) 1148783f6184SRyusuke Konishi { 1149f11459adSRyusuke Konishi return (void *)s->s_bdev == data; 1150783f6184SRyusuke Konishi } 1151783f6184SRyusuke Konishi 1152783f6184SRyusuke Konishi static int 1153783f6184SRyusuke Konishi nilfs_get_sb(struct file_system_type *fs_type, int flags, 1154783f6184SRyusuke Konishi const char *dev_name, void *data, struct vfsmount *mnt) 1155783f6184SRyusuke Konishi { 1156783f6184SRyusuke Konishi struct nilfs_super_data sd; 115733c8e57cSRyusuke Konishi struct super_block *s; 115813e90559SRyusuke Konishi fmode_t mode = FMODE_READ; 1159f11459adSRyusuke Konishi struct dentry *root_dentry; 1160f11459adSRyusuke Konishi int err, s_new = false; 1161783f6184SRyusuke Konishi 116213e90559SRyusuke Konishi if (!(flags & MS_RDONLY)) 116313e90559SRyusuke Konishi mode |= FMODE_WRITE; 116413e90559SRyusuke Konishi 116513e90559SRyusuke Konishi sd.bdev = open_bdev_exclusive(dev_name, mode, fs_type); 1166d6d4c19cSJan Blunck if (IS_ERR(sd.bdev)) 1167783f6184SRyusuke Konishi return PTR_ERR(sd.bdev); 1168783f6184SRyusuke Konishi 1169783f6184SRyusuke Konishi sd.cno = 0; 1170783f6184SRyusuke Konishi sd.flags = flags; 1171783f6184SRyusuke Konishi if (nilfs_identify((char *)data, &sd)) { 1172783f6184SRyusuke Konishi err = -EINVAL; 1173783f6184SRyusuke Konishi goto failed; 1174783f6184SRyusuke Konishi } 1175783f6184SRyusuke Konishi 11765beb6e0bSRyusuke Konishi /* 11775beb6e0bSRyusuke Konishi * once the super is inserted into the list by sget, s_umount 11785beb6e0bSRyusuke Konishi * will protect the lockfs code from trying to start a snapshot 11795beb6e0bSRyusuke Konishi * while we are mounting 11805beb6e0bSRyusuke Konishi */ 11815beb6e0bSRyusuke Konishi mutex_lock(&sd.bdev->bd_fsfreeze_mutex); 11825beb6e0bSRyusuke Konishi if (sd.bdev->bd_fsfreeze_count > 0) { 11835beb6e0bSRyusuke Konishi mutex_unlock(&sd.bdev->bd_fsfreeze_mutex); 11845beb6e0bSRyusuke Konishi err = -EBUSY; 11855beb6e0bSRyusuke Konishi goto failed; 11865beb6e0bSRyusuke Konishi } 1187f11459adSRyusuke Konishi s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, sd.bdev); 11885beb6e0bSRyusuke Konishi mutex_unlock(&sd.bdev->bd_fsfreeze_mutex); 118933c8e57cSRyusuke Konishi if (IS_ERR(s)) { 119033c8e57cSRyusuke Konishi err = PTR_ERR(s); 1191348fe8daSRyusuke Konishi goto failed; 1192783f6184SRyusuke Konishi } 1193783f6184SRyusuke Konishi 1194783f6184SRyusuke Konishi if (!s->s_root) { 1195783f6184SRyusuke Konishi char b[BDEVNAME_SIZE]; 1196783f6184SRyusuke Konishi 1197f11459adSRyusuke Konishi s_new = true; 1198f11459adSRyusuke Konishi 119933c8e57cSRyusuke Konishi /* New superblock instance created */ 1200783f6184SRyusuke Konishi s->s_flags = flags; 12014571b82cSRyusuke Konishi s->s_mode = mode; 1202783f6184SRyusuke Konishi strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id)); 1203783f6184SRyusuke Konishi sb_set_blocksize(s, block_size(sd.bdev)); 1204783f6184SRyusuke Konishi 1205348fe8daSRyusuke Konishi err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0); 1206783f6184SRyusuke Konishi if (err) 1207348fe8daSRyusuke Konishi goto failed_super; 1208783f6184SRyusuke Konishi 1209783f6184SRyusuke Konishi s->s_flags |= MS_ACTIVE; 1210f11459adSRyusuke Konishi } else if (!sd.cno) { 1211f11459adSRyusuke Konishi int busy = false; 1212f11459adSRyusuke Konishi 1213f11459adSRyusuke Konishi if (nilfs_tree_was_touched(s->s_root)) { 1214f11459adSRyusuke Konishi busy = nilfs_try_to_shrink_tree(s->s_root); 1215f11459adSRyusuke Konishi if (busy && (flags ^ s->s_flags) & MS_RDONLY) { 1216f11459adSRyusuke Konishi printk(KERN_ERR "NILFS: the device already " 1217f11459adSRyusuke Konishi "has a %s mount.\n", 1218f11459adSRyusuke Konishi (s->s_flags & MS_RDONLY) ? 1219f11459adSRyusuke Konishi "read-only" : "read/write"); 1220f11459adSRyusuke Konishi err = -EBUSY; 1221f11459adSRyusuke Konishi goto failed_super; 1222f11459adSRyusuke Konishi } 1223f11459adSRyusuke Konishi } 1224f11459adSRyusuke Konishi if (!busy) { 1225f11459adSRyusuke Konishi /* 1226f11459adSRyusuke Konishi * Try remount to setup mount states if the current 1227f11459adSRyusuke Konishi * tree is not mounted and only snapshots use this sb. 1228f11459adSRyusuke Konishi */ 1229f11459adSRyusuke Konishi err = nilfs_remount(s, &flags, data); 1230f11459adSRyusuke Konishi if (err) 1231f11459adSRyusuke Konishi goto failed_super; 1232f11459adSRyusuke Konishi } 1233f11459adSRyusuke Konishi } 1234f11459adSRyusuke Konishi 1235f11459adSRyusuke Konishi if (sd.cno) { 1236f11459adSRyusuke Konishi err = nilfs_attach_snapshot(s, sd.cno, &root_dentry); 1237348fe8daSRyusuke Konishi if (err) 1238f11459adSRyusuke Konishi goto failed_super; 1239f11459adSRyusuke Konishi } else { 1240f11459adSRyusuke Konishi root_dentry = dget(s->s_root); 1241783f6184SRyusuke Konishi } 1242783f6184SRyusuke Konishi 1243f11459adSRyusuke Konishi if (!s_new) 124413e90559SRyusuke Konishi close_bdev_exclusive(sd.bdev, mode); 1245f11459adSRyusuke Konishi 1246f11459adSRyusuke Konishi mnt->mnt_sb = s; 1247f11459adSRyusuke Konishi mnt->mnt_root = root_dentry; 1248783f6184SRyusuke Konishi return 0; 1249783f6184SRyusuke Konishi 1250f11459adSRyusuke Konishi failed_super: 1251f11459adSRyusuke Konishi deactivate_locked_super(s); 1252783f6184SRyusuke Konishi 1253348fe8daSRyusuke Konishi failed: 1254348fe8daSRyusuke Konishi if (!s_new) 1255348fe8daSRyusuke Konishi close_bdev_exclusive(sd.bdev, mode); 1256783f6184SRyusuke Konishi return err; 1257783f6184SRyusuke Konishi } 1258783f6184SRyusuke Konishi 1259783f6184SRyusuke Konishi struct file_system_type nilfs_fs_type = { 1260783f6184SRyusuke Konishi .owner = THIS_MODULE, 1261783f6184SRyusuke Konishi .name = "nilfs2", 1262783f6184SRyusuke Konishi .get_sb = nilfs_get_sb, 1263783f6184SRyusuke Konishi .kill_sb = kill_block_super, 1264783f6184SRyusuke Konishi .fs_flags = FS_REQUIRES_DEV, 1265783f6184SRyusuke Konishi }; 1266783f6184SRyusuke Konishi 126741c88bd7SLi Hong static void nilfs_inode_init_once(void *obj) 126841c88bd7SLi Hong { 126941c88bd7SLi Hong struct nilfs_inode_info *ii = obj; 127041c88bd7SLi Hong 127141c88bd7SLi Hong INIT_LIST_HEAD(&ii->i_dirty); 127241c88bd7SLi Hong #ifdef CONFIG_NILFS_XATTR 127341c88bd7SLi Hong init_rwsem(&ii->xattr_sem); 127441c88bd7SLi Hong #endif 127541c88bd7SLi Hong nilfs_btnode_cache_init_once(&ii->i_btnode_cache); 127605d0e94bSRyusuke Konishi ii->i_bmap = &ii->i_bmap_data; 127741c88bd7SLi Hong inode_init_once(&ii->vfs_inode); 127841c88bd7SLi Hong } 127941c88bd7SLi Hong 128041c88bd7SLi Hong static void nilfs_segbuf_init_once(void *obj) 128141c88bd7SLi Hong { 128241c88bd7SLi Hong memset(obj, 0, sizeof(struct nilfs_segment_buffer)); 128341c88bd7SLi Hong } 128441c88bd7SLi Hong 128541c88bd7SLi Hong static void nilfs_destroy_cachep(void) 128641c88bd7SLi Hong { 128741c88bd7SLi Hong if (nilfs_inode_cachep) 128841c88bd7SLi Hong kmem_cache_destroy(nilfs_inode_cachep); 128941c88bd7SLi Hong if (nilfs_transaction_cachep) 129041c88bd7SLi Hong kmem_cache_destroy(nilfs_transaction_cachep); 129141c88bd7SLi Hong if (nilfs_segbuf_cachep) 129241c88bd7SLi Hong kmem_cache_destroy(nilfs_segbuf_cachep); 129341c88bd7SLi Hong if (nilfs_btree_path_cache) 129441c88bd7SLi Hong kmem_cache_destroy(nilfs_btree_path_cache); 129541c88bd7SLi Hong } 129641c88bd7SLi Hong 129741c88bd7SLi Hong static int __init nilfs_init_cachep(void) 129841c88bd7SLi Hong { 129941c88bd7SLi Hong nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache", 130041c88bd7SLi Hong sizeof(struct nilfs_inode_info), 0, 130141c88bd7SLi Hong SLAB_RECLAIM_ACCOUNT, nilfs_inode_init_once); 130241c88bd7SLi Hong if (!nilfs_inode_cachep) 130341c88bd7SLi Hong goto fail; 130441c88bd7SLi Hong 130541c88bd7SLi Hong nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache", 130641c88bd7SLi Hong sizeof(struct nilfs_transaction_info), 0, 130741c88bd7SLi Hong SLAB_RECLAIM_ACCOUNT, NULL); 130841c88bd7SLi Hong if (!nilfs_transaction_cachep) 130941c88bd7SLi Hong goto fail; 131041c88bd7SLi Hong 131141c88bd7SLi Hong nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache", 131241c88bd7SLi Hong sizeof(struct nilfs_segment_buffer), 0, 131341c88bd7SLi Hong SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once); 131441c88bd7SLi Hong if (!nilfs_segbuf_cachep) 131541c88bd7SLi Hong goto fail; 131641c88bd7SLi Hong 131741c88bd7SLi Hong nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache", 131841c88bd7SLi Hong sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX, 131941c88bd7SLi Hong 0, 0, NULL); 132041c88bd7SLi Hong if (!nilfs_btree_path_cache) 132141c88bd7SLi Hong goto fail; 132241c88bd7SLi Hong 132341c88bd7SLi Hong return 0; 132441c88bd7SLi Hong 132541c88bd7SLi Hong fail: 132641c88bd7SLi Hong nilfs_destroy_cachep(); 132741c88bd7SLi Hong return -ENOMEM; 132841c88bd7SLi Hong } 132941c88bd7SLi Hong 1330783f6184SRyusuke Konishi static int __init init_nilfs_fs(void) 1331783f6184SRyusuke Konishi { 1332783f6184SRyusuke Konishi int err; 1333783f6184SRyusuke Konishi 133441c88bd7SLi Hong err = nilfs_init_cachep(); 1335783f6184SRyusuke Konishi if (err) 133641c88bd7SLi Hong goto fail; 1337783f6184SRyusuke Konishi 1338783f6184SRyusuke Konishi err = register_filesystem(&nilfs_fs_type); 1339783f6184SRyusuke Konishi if (err) 134041c88bd7SLi Hong goto free_cachep; 1341783f6184SRyusuke Konishi 13429f130263SLi Hong printk(KERN_INFO "NILFS version 2 loaded\n"); 1343783f6184SRyusuke Konishi return 0; 1344783f6184SRyusuke Konishi 134541c88bd7SLi Hong free_cachep: 134641c88bd7SLi Hong nilfs_destroy_cachep(); 134741c88bd7SLi Hong fail: 1348783f6184SRyusuke Konishi return err; 1349783f6184SRyusuke Konishi } 1350783f6184SRyusuke Konishi 1351783f6184SRyusuke Konishi static void __exit exit_nilfs_fs(void) 1352783f6184SRyusuke Konishi { 135341c88bd7SLi Hong nilfs_destroy_cachep(); 1354783f6184SRyusuke Konishi unregister_filesystem(&nilfs_fs_type); 1355783f6184SRyusuke Konishi } 1356783f6184SRyusuke Konishi 1357783f6184SRyusuke Konishi module_init(init_nilfs_fs) 1358783f6184SRyusuke Konishi module_exit(exit_nilfs_fs) 1359