~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/nilfs2/the_nilfs.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /fs/nilfs2/the_nilfs.c (Version linux-6.12-rc7) and /fs/nilfs2/the_nilfs.c (Version linux-6.9.12)


  1 // SPDX-License-Identifier: GPL-2.0+                1 // SPDX-License-Identifier: GPL-2.0+
  2 /*                                                  2 /*
  3  * the_nilfs shared structure.                      3  * the_nilfs shared structure.
  4  *                                                  4  *
  5  * Copyright (C) 2005-2008 Nippon Telegraph an      5  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  6  *                                                  6  *
  7  * Written by Ryusuke Konishi.                      7  * Written by Ryusuke Konishi.
  8  *                                                  8  *
  9  */                                                 9  */
 10                                                    10 
 11 #include <linux/buffer_head.h>                     11 #include <linux/buffer_head.h>
 12 #include <linux/slab.h>                            12 #include <linux/slab.h>
 13 #include <linux/blkdev.h>                          13 #include <linux/blkdev.h>
 14 #include <linux/backing-dev.h>                     14 #include <linux/backing-dev.h>
                                                   >>  15 #include <linux/random.h>
 15 #include <linux/log2.h>                            16 #include <linux/log2.h>
 16 #include <linux/crc32.h>                           17 #include <linux/crc32.h>
 17 #include "nilfs.h"                                 18 #include "nilfs.h"
 18 #include "segment.h"                               19 #include "segment.h"
 19 #include "alloc.h"                                 20 #include "alloc.h"
 20 #include "cpfile.h"                                21 #include "cpfile.h"
 21 #include "sufile.h"                                22 #include "sufile.h"
 22 #include "dat.h"                                   23 #include "dat.h"
 23 #include "segbuf.h"                                24 #include "segbuf.h"
 24                                                    25 
 25                                                    26 
 26 static int nilfs_valid_sb(struct nilfs_super_b     27 static int nilfs_valid_sb(struct nilfs_super_block *sbp);
 27                                                    28 
 28 void nilfs_set_last_segment(struct the_nilfs *     29 void nilfs_set_last_segment(struct the_nilfs *nilfs,
 29                             sector_t start_blo     30                             sector_t start_blocknr, u64 seq, __u64 cno)
 30 {                                                  31 {
 31         spin_lock(&nilfs->ns_last_segment_lock     32         spin_lock(&nilfs->ns_last_segment_lock);
 32         nilfs->ns_last_pseg = start_blocknr;       33         nilfs->ns_last_pseg = start_blocknr;
 33         nilfs->ns_last_seq = seq;                  34         nilfs->ns_last_seq = seq;
 34         nilfs->ns_last_cno = cno;                  35         nilfs->ns_last_cno = cno;
 35                                                    36 
 36         if (!nilfs_sb_dirty(nilfs)) {              37         if (!nilfs_sb_dirty(nilfs)) {
 37                 if (nilfs->ns_prev_seq == nilf     38                 if (nilfs->ns_prev_seq == nilfs->ns_last_seq)
 38                         goto stay_cursor;          39                         goto stay_cursor;
 39                                                    40 
 40                 set_nilfs_sb_dirty(nilfs);         41                 set_nilfs_sb_dirty(nilfs);
 41         }                                          42         }
 42         nilfs->ns_prev_seq = nilfs->ns_last_se     43         nilfs->ns_prev_seq = nilfs->ns_last_seq;
 43                                                    44 
 44  stay_cursor:                                      45  stay_cursor:
 45         spin_unlock(&nilfs->ns_last_segment_lo     46         spin_unlock(&nilfs->ns_last_segment_lock);
 46 }                                                  47 }
 47                                                    48 
 48 /**                                                49 /**
 49  * alloc_nilfs - allocate a nilfs object           50  * alloc_nilfs - allocate a nilfs object
 50  * @sb: super block instance                       51  * @sb: super block instance
 51  *                                                 52  *
 52  * Return Value: On success, pointer to the_ni     53  * Return Value: On success, pointer to the_nilfs is returned.
 53  * On error, NULL is returned.                     54  * On error, NULL is returned.
 54  */                                                55  */
 55 struct the_nilfs *alloc_nilfs(struct super_blo     56 struct the_nilfs *alloc_nilfs(struct super_block *sb)
 56 {                                                  57 {
 57         struct the_nilfs *nilfs;                   58         struct the_nilfs *nilfs;
 58                                                    59 
 59         nilfs = kzalloc(sizeof(*nilfs), GFP_KE     60         nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL);
 60         if (!nilfs)                                61         if (!nilfs)
 61                 return NULL;                       62                 return NULL;
 62                                                    63 
 63         nilfs->ns_sb = sb;                         64         nilfs->ns_sb = sb;
 64         nilfs->ns_bdev = sb->s_bdev;               65         nilfs->ns_bdev = sb->s_bdev;
 65         atomic_set(&nilfs->ns_ndirtyblks, 0);      66         atomic_set(&nilfs->ns_ndirtyblks, 0);
 66         init_rwsem(&nilfs->ns_sem);                67         init_rwsem(&nilfs->ns_sem);
 67         mutex_init(&nilfs->ns_snapshot_mount_m     68         mutex_init(&nilfs->ns_snapshot_mount_mutex);
 68         INIT_LIST_HEAD(&nilfs->ns_dirty_files)     69         INIT_LIST_HEAD(&nilfs->ns_dirty_files);
 69         INIT_LIST_HEAD(&nilfs->ns_gc_inodes);      70         INIT_LIST_HEAD(&nilfs->ns_gc_inodes);
 70         spin_lock_init(&nilfs->ns_inode_lock);     71         spin_lock_init(&nilfs->ns_inode_lock);
                                                   >>  72         spin_lock_init(&nilfs->ns_next_gen_lock);
 71         spin_lock_init(&nilfs->ns_last_segment     73         spin_lock_init(&nilfs->ns_last_segment_lock);
 72         nilfs->ns_cptree = RB_ROOT;                74         nilfs->ns_cptree = RB_ROOT;
 73         spin_lock_init(&nilfs->ns_cptree_lock)     75         spin_lock_init(&nilfs->ns_cptree_lock);
 74         init_rwsem(&nilfs->ns_segctor_sem);        76         init_rwsem(&nilfs->ns_segctor_sem);
 75         nilfs->ns_sb_update_freq = NILFS_SB_FR     77         nilfs->ns_sb_update_freq = NILFS_SB_FREQ;
 76                                                    78 
 77         return nilfs;                              79         return nilfs;
 78 }                                                  80 }
 79                                                    81 
 80 /**                                                82 /**
 81  * destroy_nilfs - destroy nilfs object            83  * destroy_nilfs - destroy nilfs object
 82  * @nilfs: nilfs object to be released             84  * @nilfs: nilfs object to be released
 83  */                                                85  */
 84 void destroy_nilfs(struct the_nilfs *nilfs)        86 void destroy_nilfs(struct the_nilfs *nilfs)
 85 {                                                  87 {
 86         might_sleep();                             88         might_sleep();
 87         if (nilfs_init(nilfs)) {                   89         if (nilfs_init(nilfs)) {
 88                 brelse(nilfs->ns_sbh[0]);          90                 brelse(nilfs->ns_sbh[0]);
 89                 brelse(nilfs->ns_sbh[1]);          91                 brelse(nilfs->ns_sbh[1]);
 90         }                                          92         }
 91         kfree(nilfs);                              93         kfree(nilfs);
 92 }                                                  94 }
 93                                                    95 
 94 static int nilfs_load_super_root(struct the_ni     96 static int nilfs_load_super_root(struct the_nilfs *nilfs,
 95                                  struct super_     97                                  struct super_block *sb, sector_t sr_block)
 96 {                                                  98 {
 97         struct buffer_head *bh_sr;                 99         struct buffer_head *bh_sr;
 98         struct nilfs_super_root *raw_sr;          100         struct nilfs_super_root *raw_sr;
 99         struct nilfs_super_block **sbp = nilfs    101         struct nilfs_super_block **sbp = nilfs->ns_sbp;
100         struct nilfs_inode *rawi;                 102         struct nilfs_inode *rawi;
101         unsigned int dat_entry_size, segment_u    103         unsigned int dat_entry_size, segment_usage_size, checkpoint_size;
102         unsigned int inode_size;                  104         unsigned int inode_size;
103         int err;                                  105         int err;
104                                                   106 
105         err = nilfs_read_super_root_block(nilf    107         err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
106         if (unlikely(err))                        108         if (unlikely(err))
107                 return err;                       109                 return err;
108                                                   110 
109         down_read(&nilfs->ns_sem);                111         down_read(&nilfs->ns_sem);
110         dat_entry_size = le16_to_cpu(sbp[0]->s    112         dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
111         checkpoint_size = le16_to_cpu(sbp[0]->    113         checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
112         segment_usage_size = le16_to_cpu(sbp[0    114         segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
113         up_read(&nilfs->ns_sem);                  115         up_read(&nilfs->ns_sem);
114                                                   116 
115         inode_size = nilfs->ns_inode_size;        117         inode_size = nilfs->ns_inode_size;
116                                                   118 
117         rawi = (void *)bh_sr->b_data + NILFS_S    119         rawi = (void *)bh_sr->b_data + NILFS_SR_DAT_OFFSET(inode_size);
118         err = nilfs_dat_read(sb, dat_entry_siz    120         err = nilfs_dat_read(sb, dat_entry_size, rawi, &nilfs->ns_dat);
119         if (err)                                  121         if (err)
120                 goto failed;                      122                 goto failed;
121                                                   123 
122         rawi = (void *)bh_sr->b_data + NILFS_S    124         rawi = (void *)bh_sr->b_data + NILFS_SR_CPFILE_OFFSET(inode_size);
123         err = nilfs_cpfile_read(sb, checkpoint    125         err = nilfs_cpfile_read(sb, checkpoint_size, rawi, &nilfs->ns_cpfile);
124         if (err)                                  126         if (err)
125                 goto failed_dat;                  127                 goto failed_dat;
126                                                   128 
127         rawi = (void *)bh_sr->b_data + NILFS_S    129         rawi = (void *)bh_sr->b_data + NILFS_SR_SUFILE_OFFSET(inode_size);
128         err = nilfs_sufile_read(sb, segment_us    130         err = nilfs_sufile_read(sb, segment_usage_size, rawi,
129                                 &nilfs->ns_suf    131                                 &nilfs->ns_sufile);
130         if (err)                                  132         if (err)
131                 goto failed_cpfile;               133                 goto failed_cpfile;
132                                                   134 
133         raw_sr = (struct nilfs_super_root *)bh    135         raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
134         nilfs->ns_nongc_ctime = le64_to_cpu(ra    136         nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
135                                                   137 
136  failed:                                          138  failed:
137         brelse(bh_sr);                            139         brelse(bh_sr);
138         return err;                               140         return err;
139                                                   141 
140  failed_cpfile:                                   142  failed_cpfile:
141         iput(nilfs->ns_cpfile);                   143         iput(nilfs->ns_cpfile);
142                                                   144 
143  failed_dat:                                      145  failed_dat:
144         iput(nilfs->ns_dat);                      146         iput(nilfs->ns_dat);
145         goto failed;                              147         goto failed;
146 }                                                 148 }
147                                                   149 
148 static void nilfs_init_recovery_info(struct ni    150 static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
149 {                                                 151 {
150         memset(ri, 0, sizeof(*ri));               152         memset(ri, 0, sizeof(*ri));
151         INIT_LIST_HEAD(&ri->ri_used_segments);    153         INIT_LIST_HEAD(&ri->ri_used_segments);
152 }                                                 154 }
153                                                   155 
154 static void nilfs_clear_recovery_info(struct n    156 static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
155 {                                                 157 {
156         nilfs_dispose_segment_list(&ri->ri_use    158         nilfs_dispose_segment_list(&ri->ri_used_segments);
157 }                                                 159 }
158                                                   160 
159 /**                                               161 /**
160  * nilfs_store_log_cursor - load log cursor fr    162  * nilfs_store_log_cursor - load log cursor from a super block
161  * @nilfs: nilfs object                           163  * @nilfs: nilfs object
162  * @sbp: buffer storing super block to be read    164  * @sbp: buffer storing super block to be read
163  *                                                165  *
164  * nilfs_store_log_cursor() reads the last pos    166  * nilfs_store_log_cursor() reads the last position of the log
165  * containing a super root from a given super     167  * containing a super root from a given super block, and initializes
166  * relevant information on the nilfs object pr    168  * relevant information on the nilfs object preparatory for log
167  * scanning and recovery.                         169  * scanning and recovery.
168  */                                               170  */
169 static int nilfs_store_log_cursor(struct the_n    171 static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
170                                   struct nilfs    172                                   struct nilfs_super_block *sbp)
171 {                                                 173 {
172         int ret = 0;                              174         int ret = 0;
173                                                   175 
174         nilfs->ns_last_pseg = le64_to_cpu(sbp-    176         nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
175         nilfs->ns_last_cno = le64_to_cpu(sbp->    177         nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
176         nilfs->ns_last_seq = le64_to_cpu(sbp->    178         nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
177                                                   179 
178         nilfs->ns_prev_seq = nilfs->ns_last_se    180         nilfs->ns_prev_seq = nilfs->ns_last_seq;
179         nilfs->ns_seg_seq = nilfs->ns_last_seq    181         nilfs->ns_seg_seq = nilfs->ns_last_seq;
180         nilfs->ns_segnum =                        182         nilfs->ns_segnum =
181                 nilfs_get_segnum_of_block(nilf    183                 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
182         nilfs->ns_cno = nilfs->ns_last_cno + 1    184         nilfs->ns_cno = nilfs->ns_last_cno + 1;
183         if (nilfs->ns_segnum >= nilfs->ns_nseg    185         if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
184                 nilfs_err(nilfs->ns_sb,           186                 nilfs_err(nilfs->ns_sb,
185                           "pointed segment num    187                           "pointed segment number is out of range: segnum=%llu, nsegments=%lu",
186                           (unsigned long long)    188                           (unsigned long long)nilfs->ns_segnum,
187                           nilfs->ns_nsegments)    189                           nilfs->ns_nsegments);
188                 ret = -EINVAL;                    190                 ret = -EINVAL;
189         }                                         191         }
190         return ret;                               192         return ret;
191 }                                                 193 }
192                                                   194 
193 /**                                               195 /**
194  * nilfs_get_blocksize - get block size from r    196  * nilfs_get_blocksize - get block size from raw superblock data
195  * @sb: super block instance                      197  * @sb: super block instance
196  * @sbp: superblock raw data buffer               198  * @sbp: superblock raw data buffer
197  * @blocksize: place to store block size          199  * @blocksize: place to store block size
198  *                                                200  *
199  * nilfs_get_blocksize() calculates the block     201  * nilfs_get_blocksize() calculates the block size from the block size
200  * exponent information written in @sbp and st    202  * exponent information written in @sbp and stores it in @blocksize,
201  * or aborts with an error message if it's too    203  * or aborts with an error message if it's too large.
202  *                                                204  *
203  * Return Value: On success, 0 is returned. If    205  * Return Value: On success, 0 is returned. If the block size is too
204  * large, -EINVAL is returned.                    206  * large, -EINVAL is returned.
205  */                                               207  */
206 static int nilfs_get_blocksize(struct super_bl    208 static int nilfs_get_blocksize(struct super_block *sb,
207                                struct nilfs_su    209                                struct nilfs_super_block *sbp, int *blocksize)
208 {                                                 210 {
209         unsigned int shift_bits = le32_to_cpu(    211         unsigned int shift_bits = le32_to_cpu(sbp->s_log_block_size);
210                                                   212 
211         if (unlikely(shift_bits >                 213         if (unlikely(shift_bits >
212                      ilog2(NILFS_MAX_BLOCK_SIZ    214                      ilog2(NILFS_MAX_BLOCK_SIZE) - BLOCK_SIZE_BITS)) {
213                 nilfs_err(sb, "too large files    215                 nilfs_err(sb, "too large filesystem blocksize: 2 ^ %u KiB",
214                           shift_bits);            216                           shift_bits);
215                 return -EINVAL;                   217                 return -EINVAL;
216         }                                         218         }
217         *blocksize = BLOCK_SIZE << shift_bits;    219         *blocksize = BLOCK_SIZE << shift_bits;
218         return 0;                                 220         return 0;
219 }                                                 221 }
220                                                   222 
221 /**                                               223 /**
222  * load_nilfs - load and recover the nilfs        224  * load_nilfs - load and recover the nilfs
223  * @nilfs: the_nilfs structure to be released     225  * @nilfs: the_nilfs structure to be released
224  * @sb: super block instance used to recover p    226  * @sb: super block instance used to recover past segment
225  *                                                227  *
226  * load_nilfs() searches and load the latest s    228  * load_nilfs() searches and load the latest super root,
227  * attaches the last segment, and does recover    229  * attaches the last segment, and does recovery if needed.
228  * The caller must call this exclusively for s    230  * The caller must call this exclusively for simultaneous mounts.
229  */                                               231  */
230 int load_nilfs(struct the_nilfs *nilfs, struct    232 int load_nilfs(struct the_nilfs *nilfs, struct super_block *sb)
231 {                                                 233 {
232         struct nilfs_recovery_info ri;            234         struct nilfs_recovery_info ri;
233         unsigned int s_flags = sb->s_flags;       235         unsigned int s_flags = sb->s_flags;
234         int really_read_only = bdev_read_only(    236         int really_read_only = bdev_read_only(nilfs->ns_bdev);
235         int valid_fs = nilfs_valid_fs(nilfs);     237         int valid_fs = nilfs_valid_fs(nilfs);
236         int err;                                  238         int err;
237                                                   239 
238         if (!valid_fs) {                          240         if (!valid_fs) {
239                 nilfs_warn(sb, "mounting unche    241                 nilfs_warn(sb, "mounting unchecked fs");
240                 if (s_flags & SB_RDONLY) {        242                 if (s_flags & SB_RDONLY) {
241                         nilfs_info(sb,            243                         nilfs_info(sb,
242                                    "recovery r    244                                    "recovery required for readonly filesystem");
243                         nilfs_info(sb,            245                         nilfs_info(sb,
244                                    "write acce    246                                    "write access will be enabled during recovery");
245                 }                                 247                 }
246         }                                         248         }
247                                                   249 
248         nilfs_init_recovery_info(&ri);            250         nilfs_init_recovery_info(&ri);
249                                                   251 
250         err = nilfs_search_super_root(nilfs, &    252         err = nilfs_search_super_root(nilfs, &ri);
251         if (unlikely(err)) {                      253         if (unlikely(err)) {
252                 struct nilfs_super_block **sbp    254                 struct nilfs_super_block **sbp = nilfs->ns_sbp;
253                 int blocksize;                    255                 int blocksize;
254                                                   256 
255                 if (err != -EINVAL)               257                 if (err != -EINVAL)
256                         goto scan_error;          258                         goto scan_error;
257                                                   259 
258                 if (!nilfs_valid_sb(sbp[1])) {    260                 if (!nilfs_valid_sb(sbp[1])) {
259                         nilfs_warn(sb,            261                         nilfs_warn(sb,
260                                    "unable to     262                                    "unable to fall back to spare super block");
261                         goto scan_error;          263                         goto scan_error;
262                 }                                 264                 }
263                 nilfs_info(sb, "trying rollbac    265                 nilfs_info(sb, "trying rollback from an earlier position");
264                                                   266 
265                 /*                                267                 /*
266                  * restore super block with it    268                  * restore super block with its spare and reconfigure
267                  * relevant states of the nilf    269                  * relevant states of the nilfs object.
268                  */                               270                  */
269                 memcpy(sbp[0], sbp[1], nilfs->    271                 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
270                 nilfs->ns_crc_seed = le32_to_c    272                 nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
271                 nilfs->ns_sbwtime = le64_to_cp    273                 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
272                                                   274 
273                 /* verify consistency between     275                 /* verify consistency between two super blocks */
274                 err = nilfs_get_blocksize(sb,     276                 err = nilfs_get_blocksize(sb, sbp[0], &blocksize);
275                 if (err)                          277                 if (err)
276                         goto scan_error;          278                         goto scan_error;
277                                                   279 
278                 if (blocksize != nilfs->ns_blo    280                 if (blocksize != nilfs->ns_blocksize) {
279                         nilfs_warn(sb,            281                         nilfs_warn(sb,
280                                    "blocksize     282                                    "blocksize differs between two super blocks (%d != %d)",
281                                    blocksize,     283                                    blocksize, nilfs->ns_blocksize);
282                         err = -EINVAL;            284                         err = -EINVAL;
283                         goto scan_error;          285                         goto scan_error;
284                 }                                 286                 }
285                                                   287 
286                 err = nilfs_store_log_cursor(n    288                 err = nilfs_store_log_cursor(nilfs, sbp[0]);
287                 if (err)                          289                 if (err)
288                         goto scan_error;          290                         goto scan_error;
289                                                   291 
290                 /* drop clean flag to allow ro    292                 /* drop clean flag to allow roll-forward and recovery */
291                 nilfs->ns_mount_state &= ~NILF    293                 nilfs->ns_mount_state &= ~NILFS_VALID_FS;
292                 valid_fs = 0;                     294                 valid_fs = 0;
293                                                   295 
294                 err = nilfs_search_super_root(    296                 err = nilfs_search_super_root(nilfs, &ri);
295                 if (err)                          297                 if (err)
296                         goto scan_error;          298                         goto scan_error;
297         }                                         299         }
298                                                   300 
299         err = nilfs_load_super_root(nilfs, sb,    301         err = nilfs_load_super_root(nilfs, sb, ri.ri_super_root);
300         if (unlikely(err)) {                      302         if (unlikely(err)) {
301                 nilfs_err(sb, "error %d while     303                 nilfs_err(sb, "error %d while loading super root", err);
302                 goto failed;                      304                 goto failed;
303         }                                         305         }
304                                                   306 
305         err = nilfs_sysfs_create_device_group(    307         err = nilfs_sysfs_create_device_group(sb);
306         if (unlikely(err))                        308         if (unlikely(err))
307                 goto sysfs_error;                 309                 goto sysfs_error;
308                                                   310 
309         if (valid_fs)                             311         if (valid_fs)
310                 goto skip_recovery;               312                 goto skip_recovery;
311                                                   313 
312         if (s_flags & SB_RDONLY) {                314         if (s_flags & SB_RDONLY) {
313                 __u64 features;                   315                 __u64 features;
314                                                   316 
315                 if (nilfs_test_opt(nilfs, NORE    317                 if (nilfs_test_opt(nilfs, NORECOVERY)) {
316                         nilfs_info(sb,            318                         nilfs_info(sb,
317                                    "norecovery    319                                    "norecovery option specified, skipping roll-forward recovery");
318                         goto skip_recovery;       320                         goto skip_recovery;
319                 }                                 321                 }
320                 features = le64_to_cpu(nilfs->    322                 features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
321                         ~NILFS_FEATURE_COMPAT_    323                         ~NILFS_FEATURE_COMPAT_RO_SUPP;
322                 if (features) {                   324                 if (features) {
323                         nilfs_err(sb,             325                         nilfs_err(sb,
324                                   "couldn't pr    326                                   "couldn't proceed with recovery because of unsupported optional features (%llx)",
325                                   (unsigned lo    327                                   (unsigned long long)features);
326                         err = -EROFS;             328                         err = -EROFS;
327                         goto failed_unload;       329                         goto failed_unload;
328                 }                                 330                 }
329                 if (really_read_only) {           331                 if (really_read_only) {
330                         nilfs_err(sb,             332                         nilfs_err(sb,
331                                   "write acces    333                                   "write access unavailable, cannot proceed");
332                         err = -EROFS;             334                         err = -EROFS;
333                         goto failed_unload;       335                         goto failed_unload;
334                 }                                 336                 }
335                 sb->s_flags &= ~SB_RDONLY;        337                 sb->s_flags &= ~SB_RDONLY;
336         } else if (nilfs_test_opt(nilfs, NOREC    338         } else if (nilfs_test_opt(nilfs, NORECOVERY)) {
337                 nilfs_err(sb,                     339                 nilfs_err(sb,
338                           "recovery cancelled     340                           "recovery cancelled because norecovery option was specified for a read/write mount");
339                 err = -EINVAL;                    341                 err = -EINVAL;
340                 goto failed_unload;               342                 goto failed_unload;
341         }                                         343         }
342                                                   344 
343         err = nilfs_salvage_orphan_logs(nilfs,    345         err = nilfs_salvage_orphan_logs(nilfs, sb, &ri);
344         if (err)                                  346         if (err)
345                 goto failed_unload;               347                 goto failed_unload;
346                                                   348 
347         down_write(&nilfs->ns_sem);               349         down_write(&nilfs->ns_sem);
348         nilfs->ns_mount_state |= NILFS_VALID_F    350         nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
349         err = nilfs_cleanup_super(sb);            351         err = nilfs_cleanup_super(sb);
350         up_write(&nilfs->ns_sem);                 352         up_write(&nilfs->ns_sem);
351                                                   353 
352         if (err) {                                354         if (err) {
353                 nilfs_err(sb,                     355                 nilfs_err(sb,
354                           "error %d updating s    356                           "error %d updating super block. recovery unfinished.",
355                           err);                   357                           err);
356                 goto failed_unload;               358                 goto failed_unload;
357         }                                         359         }
358         nilfs_info(sb, "recovery complete");      360         nilfs_info(sb, "recovery complete");
359                                                   361 
360  skip_recovery:                                   362  skip_recovery:
361         nilfs_clear_recovery_info(&ri);           363         nilfs_clear_recovery_info(&ri);
362         sb->s_flags = s_flags;                    364         sb->s_flags = s_flags;
363         return 0;                                 365         return 0;
364                                                   366 
365  scan_error:                                      367  scan_error:
366         nilfs_err(sb, "error %d while searchin    368         nilfs_err(sb, "error %d while searching super root", err);
367         goto failed;                              369         goto failed;
368                                                   370 
369  failed_unload:                                   371  failed_unload:
370         nilfs_sysfs_delete_device_group(nilfs)    372         nilfs_sysfs_delete_device_group(nilfs);
371                                                   373 
372  sysfs_error:                                     374  sysfs_error:
373         iput(nilfs->ns_cpfile);                   375         iput(nilfs->ns_cpfile);
374         iput(nilfs->ns_sufile);                   376         iput(nilfs->ns_sufile);
375         iput(nilfs->ns_dat);                      377         iput(nilfs->ns_dat);
376                                                   378 
377  failed:                                          379  failed:
378         nilfs_clear_recovery_info(&ri);           380         nilfs_clear_recovery_info(&ri);
379         sb->s_flags = s_flags;                    381         sb->s_flags = s_flags;
380         return err;                               382         return err;
381 }                                                 383 }
382                                                   384 
383 static unsigned long long nilfs_max_size(unsig    385 static unsigned long long nilfs_max_size(unsigned int blkbits)
384 {                                                 386 {
385         unsigned int max_bits;                    387         unsigned int max_bits;
386         unsigned long long res = MAX_LFS_FILES    388         unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
387                                                   389 
388         max_bits = blkbits + NILFS_BMAP_KEY_BI    390         max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
389         if (max_bits < 64)                        391         if (max_bits < 64)
390                 res = min_t(unsigned long long    392                 res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
391         return res;                               393         return res;
392 }                                                 394 }
393                                                   395 
394 /**                                               396 /**
395  * nilfs_nrsvsegs - calculate the number of re    397  * nilfs_nrsvsegs - calculate the number of reserved segments
396  * @nilfs: nilfs object                           398  * @nilfs: nilfs object
397  * @nsegs: total number of segments               399  * @nsegs: total number of segments
398  */                                               400  */
399 unsigned long nilfs_nrsvsegs(struct the_nilfs     401 unsigned long nilfs_nrsvsegs(struct the_nilfs *nilfs, unsigned long nsegs)
400 {                                                 402 {
401         return max_t(unsigned long, NILFS_MIN_    403         return max_t(unsigned long, NILFS_MIN_NRSVSEGS,
402                      DIV_ROUND_UP(nsegs * nilf    404                      DIV_ROUND_UP(nsegs * nilfs->ns_r_segments_percentage,
403                                   100));          405                                   100));
404 }                                                 406 }
405                                                   407 
406 /**                                               408 /**
407  * nilfs_max_segment_count - calculate the max    409  * nilfs_max_segment_count - calculate the maximum number of segments
408  * @nilfs: nilfs object                           410  * @nilfs: nilfs object
409  */                                               411  */
410 static u64 nilfs_max_segment_count(struct the_    412 static u64 nilfs_max_segment_count(struct the_nilfs *nilfs)
411 {                                                 413 {
412         u64 max_count = U64_MAX;                  414         u64 max_count = U64_MAX;
413                                                   415 
414         max_count = div64_ul(max_count, nilfs-    416         max_count = div64_ul(max_count, nilfs->ns_blocks_per_segment);
415         return min_t(u64, max_count, ULONG_MAX    417         return min_t(u64, max_count, ULONG_MAX);
416 }                                                 418 }
417                                                   419 
418 void nilfs_set_nsegments(struct the_nilfs *nil    420 void nilfs_set_nsegments(struct the_nilfs *nilfs, unsigned long nsegs)
419 {                                                 421 {
420         nilfs->ns_nsegments = nsegs;              422         nilfs->ns_nsegments = nsegs;
421         nilfs->ns_nrsvsegs = nilfs_nrsvsegs(ni    423         nilfs->ns_nrsvsegs = nilfs_nrsvsegs(nilfs, nsegs);
422 }                                                 424 }
423                                                   425 
424 static int nilfs_store_disk_layout(struct the_    426 static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
425                                    struct nilf    427                                    struct nilfs_super_block *sbp)
426 {                                                 428 {
427         u64 nsegments, nblocks;                   429         u64 nsegments, nblocks;
428                                                   430 
429         if (le32_to_cpu(sbp->s_rev_level) < NI    431         if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
430                 nilfs_err(nilfs->ns_sb,           432                 nilfs_err(nilfs->ns_sb,
431                           "unsupported revisio    433                           "unsupported revision (superblock rev.=%d.%d, current rev.=%d.%d). Please check the version of mkfs.nilfs(2).",
432                           le32_to_cpu(sbp->s_r    434                           le32_to_cpu(sbp->s_rev_level),
433                           le16_to_cpu(sbp->s_m    435                           le16_to_cpu(sbp->s_minor_rev_level),
434                           NILFS_CURRENT_REV, N    436                           NILFS_CURRENT_REV, NILFS_MINOR_REV);
435                 return -EINVAL;                   437                 return -EINVAL;
436         }                                         438         }
437         nilfs->ns_sbsize = le16_to_cpu(sbp->s_    439         nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
438         if (nilfs->ns_sbsize > BLOCK_SIZE)        440         if (nilfs->ns_sbsize > BLOCK_SIZE)
439                 return -EINVAL;                   441                 return -EINVAL;
440                                                   442 
441         nilfs->ns_inode_size = le16_to_cpu(sbp    443         nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
442         if (nilfs->ns_inode_size > nilfs->ns_b    444         if (nilfs->ns_inode_size > nilfs->ns_blocksize) {
443                 nilfs_err(nilfs->ns_sb, "too l    445                 nilfs_err(nilfs->ns_sb, "too large inode size: %d bytes",
444                           nilfs->ns_inode_size    446                           nilfs->ns_inode_size);
445                 return -EINVAL;                   447                 return -EINVAL;
446         } else if (nilfs->ns_inode_size < NILF    448         } else if (nilfs->ns_inode_size < NILFS_MIN_INODE_SIZE) {
447                 nilfs_err(nilfs->ns_sb, "too s    449                 nilfs_err(nilfs->ns_sb, "too small inode size: %d bytes",
448                           nilfs->ns_inode_size    450                           nilfs->ns_inode_size);
449                 return -EINVAL;                   451                 return -EINVAL;
450         }                                         452         }
451                                                   453 
452         nilfs->ns_first_ino = le32_to_cpu(sbp-    454         nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
453         if (nilfs->ns_first_ino < NILFS_USER_I    455         if (nilfs->ns_first_ino < NILFS_USER_INO) {
454                 nilfs_err(nilfs->ns_sb,           456                 nilfs_err(nilfs->ns_sb,
455                           "too small lower lim    457                           "too small lower limit for non-reserved inode numbers: %u",
456                           nilfs->ns_first_ino)    458                           nilfs->ns_first_ino);
457                 return -EINVAL;                   459                 return -EINVAL;
458         }                                         460         }
459                                                   461 
460         nilfs->ns_blocks_per_segment = le32_to    462         nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
461         if (nilfs->ns_blocks_per_segment < NIL    463         if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
462                 nilfs_err(nilfs->ns_sb, "too s    464                 nilfs_err(nilfs->ns_sb, "too short segment: %lu blocks",
463                           nilfs->ns_blocks_per    465                           nilfs->ns_blocks_per_segment);
464                 return -EINVAL;                   466                 return -EINVAL;
465         }                                         467         }
466                                                   468 
467         nilfs->ns_first_data_block = le64_to_c    469         nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
468         nilfs->ns_r_segments_percentage =         470         nilfs->ns_r_segments_percentage =
469                 le32_to_cpu(sbp->s_r_segments_    471                 le32_to_cpu(sbp->s_r_segments_percentage);
470         if (nilfs->ns_r_segments_percentage <     472         if (nilfs->ns_r_segments_percentage < 1 ||
471             nilfs->ns_r_segments_percentage >     473             nilfs->ns_r_segments_percentage > 99) {
472                 nilfs_err(nilfs->ns_sb,           474                 nilfs_err(nilfs->ns_sb,
473                           "invalid reserved se    475                           "invalid reserved segments percentage: %lu",
474                           nilfs->ns_r_segments    476                           nilfs->ns_r_segments_percentage);
475                 return -EINVAL;                   477                 return -EINVAL;
476         }                                         478         }
477                                                   479 
478         nsegments = le64_to_cpu(sbp->s_nsegmen    480         nsegments = le64_to_cpu(sbp->s_nsegments);
479         if (nsegments > nilfs_max_segment_coun    481         if (nsegments > nilfs_max_segment_count(nilfs)) {
480                 nilfs_err(nilfs->ns_sb,           482                 nilfs_err(nilfs->ns_sb,
481                           "segment count %llu     483                           "segment count %llu exceeds upper limit (%llu segments)",
482                           (unsigned long long)    484                           (unsigned long long)nsegments,
483                           (unsigned long long)    485                           (unsigned long long)nilfs_max_segment_count(nilfs));
484                 return -EINVAL;                   486                 return -EINVAL;
485         }                                         487         }
486                                                   488 
487         nblocks = sb_bdev_nr_blocks(nilfs->ns_    489         nblocks = sb_bdev_nr_blocks(nilfs->ns_sb);
488         if (nblocks) {                            490         if (nblocks) {
489                 u64 min_block_count = nsegment    491                 u64 min_block_count = nsegments * nilfs->ns_blocks_per_segment;
490                 /*                                492                 /*
491                  * To avoid failing to mount e    493                  * To avoid failing to mount early device images without a
492                  * second superblock, exclude     494                  * second superblock, exclude that block count from the
493                  * "min_block_count" calculati    495                  * "min_block_count" calculation.
494                  */                               496                  */
495                                                   497 
496                 if (nblocks < min_block_count)    498                 if (nblocks < min_block_count) {
497                         nilfs_err(nilfs->ns_sb    499                         nilfs_err(nilfs->ns_sb,
498                                   "total numbe    500                                   "total number of segment blocks %llu exceeds device size (%llu blocks)",
499                                   (unsigned lo    501                                   (unsigned long long)min_block_count,
500                                   (unsigned lo    502                                   (unsigned long long)nblocks);
501                         return -EINVAL;           503                         return -EINVAL;
502                 }                                 504                 }
503         }                                         505         }
504                                                   506 
505         nilfs_set_nsegments(nilfs, nsegments);    507         nilfs_set_nsegments(nilfs, nsegments);
506         nilfs->ns_crc_seed = le32_to_cpu(sbp->    508         nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
507         return 0;                                 509         return 0;
508 }                                                 510 }
509                                                   511 
510 static int nilfs_valid_sb(struct nilfs_super_b    512 static int nilfs_valid_sb(struct nilfs_super_block *sbp)
511 {                                                 513 {
512         static unsigned char sum[4];              514         static unsigned char sum[4];
513         const int sumoff = offsetof(struct nil    515         const int sumoff = offsetof(struct nilfs_super_block, s_sum);
514         size_t bytes;                             516         size_t bytes;
515         u32 crc;                                  517         u32 crc;
516                                                   518 
517         if (!sbp || le16_to_cpu(sbp->s_magic)     519         if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
518                 return 0;                         520                 return 0;
519         bytes = le16_to_cpu(sbp->s_bytes);        521         bytes = le16_to_cpu(sbp->s_bytes);
520         if (bytes < sumoff + 4 || bytes > BLOC    522         if (bytes < sumoff + 4 || bytes > BLOCK_SIZE)
521                 return 0;                         523                 return 0;
522         crc = crc32_le(le32_to_cpu(sbp->s_crc_    524         crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
523                        sumoff);                   525                        sumoff);
524         crc = crc32_le(crc, sum, 4);              526         crc = crc32_le(crc, sum, 4);
525         crc = crc32_le(crc, (unsigned char *)s    527         crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
526                        bytes - sumoff - 4);       528                        bytes - sumoff - 4);
527         return crc == le32_to_cpu(sbp->s_sum);    529         return crc == le32_to_cpu(sbp->s_sum);
528 }                                                 530 }
529                                                   531 
530 /**                                               532 /**
531  * nilfs_sb2_bad_offset - check the location o    533  * nilfs_sb2_bad_offset - check the location of the second superblock
532  * @sbp: superblock raw data buffer               534  * @sbp: superblock raw data buffer
533  * @offset: byte offset of second superblock c    535  * @offset: byte offset of second superblock calculated from device size
534  *                                                536  *
535  * nilfs_sb2_bad_offset() checks if the positi    537  * nilfs_sb2_bad_offset() checks if the position on the second
536  * superblock is valid or not based on the fil    538  * superblock is valid or not based on the filesystem parameters
537  * stored in @sbp.  If @offset points to a loc    539  * stored in @sbp.  If @offset points to a location within the segment
538  * area, or if the parameters themselves are n    540  * area, or if the parameters themselves are not normal, it is
539  * determined to be invalid.                      541  * determined to be invalid.
540  *                                                542  *
541  * Return Value: true if invalid, false if val    543  * Return Value: true if invalid, false if valid.
542  */                                               544  */
543 static bool nilfs_sb2_bad_offset(struct nilfs_    545 static bool nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
544 {                                                 546 {
545         unsigned int shift_bits = le32_to_cpu(    547         unsigned int shift_bits = le32_to_cpu(sbp->s_log_block_size);
546         u32 blocks_per_segment = le32_to_cpu(s    548         u32 blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
547         u64 nsegments = le64_to_cpu(sbp->s_nse    549         u64 nsegments = le64_to_cpu(sbp->s_nsegments);
548         u64 index;                                550         u64 index;
549                                                   551 
550         if (blocks_per_segment < NILFS_SEG_MIN    552         if (blocks_per_segment < NILFS_SEG_MIN_BLOCKS ||
551             shift_bits > ilog2(NILFS_MAX_BLOCK    553             shift_bits > ilog2(NILFS_MAX_BLOCK_SIZE) - BLOCK_SIZE_BITS)
552                 return true;                      554                 return true;
553                                                   555 
554         index = offset >> (shift_bits + BLOCK_    556         index = offset >> (shift_bits + BLOCK_SIZE_BITS);
555         do_div(index, blocks_per_segment);        557         do_div(index, blocks_per_segment);
556         return index < nsegments;                 558         return index < nsegments;
557 }                                                 559 }
558                                                   560 
559 static void nilfs_release_super_block(struct t    561 static void nilfs_release_super_block(struct the_nilfs *nilfs)
560 {                                                 562 {
561         int i;                                    563         int i;
562                                                   564 
563         for (i = 0; i < 2; i++) {                 565         for (i = 0; i < 2; i++) {
564                 if (nilfs->ns_sbp[i]) {           566                 if (nilfs->ns_sbp[i]) {
565                         brelse(nilfs->ns_sbh[i    567                         brelse(nilfs->ns_sbh[i]);
566                         nilfs->ns_sbh[i] = NUL    568                         nilfs->ns_sbh[i] = NULL;
567                         nilfs->ns_sbp[i] = NUL    569                         nilfs->ns_sbp[i] = NULL;
568                 }                                 570                 }
569         }                                         571         }
570 }                                                 572 }
571                                                   573 
572 void nilfs_fall_back_super_block(struct the_ni    574 void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
573 {                                                 575 {
574         brelse(nilfs->ns_sbh[0]);                 576         brelse(nilfs->ns_sbh[0]);
575         nilfs->ns_sbh[0] = nilfs->ns_sbh[1];      577         nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
576         nilfs->ns_sbp[0] = nilfs->ns_sbp[1];      578         nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
577         nilfs->ns_sbh[1] = NULL;                  579         nilfs->ns_sbh[1] = NULL;
578         nilfs->ns_sbp[1] = NULL;                  580         nilfs->ns_sbp[1] = NULL;
579 }                                                 581 }
580                                                   582 
581 void nilfs_swap_super_block(struct the_nilfs *    583 void nilfs_swap_super_block(struct the_nilfs *nilfs)
582 {                                                 584 {
583         struct buffer_head *tsbh = nilfs->ns_s    585         struct buffer_head *tsbh = nilfs->ns_sbh[0];
584         struct nilfs_super_block *tsbp = nilfs    586         struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
585                                                   587 
586         nilfs->ns_sbh[0] = nilfs->ns_sbh[1];      588         nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
587         nilfs->ns_sbp[0] = nilfs->ns_sbp[1];      589         nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
588         nilfs->ns_sbh[1] = tsbh;                  590         nilfs->ns_sbh[1] = tsbh;
589         nilfs->ns_sbp[1] = tsbp;                  591         nilfs->ns_sbp[1] = tsbp;
590 }                                                 592 }
591                                                   593 
592 static int nilfs_load_super_block(struct the_n    594 static int nilfs_load_super_block(struct the_nilfs *nilfs,
593                                   struct super    595                                   struct super_block *sb, int blocksize,
594                                   struct nilfs    596                                   struct nilfs_super_block **sbpp)
595 {                                                 597 {
596         struct nilfs_super_block **sbp = nilfs    598         struct nilfs_super_block **sbp = nilfs->ns_sbp;
597         struct buffer_head **sbh = nilfs->ns_s    599         struct buffer_head **sbh = nilfs->ns_sbh;
598         u64 sb2off, devsize = bdev_nr_bytes(ni    600         u64 sb2off, devsize = bdev_nr_bytes(nilfs->ns_bdev);
599         int valid[2], swp = 0, older;          !! 601         int valid[2], swp = 0;
600                                                   602 
601         if (devsize < NILFS_SEG_MIN_BLOCKS * N    603         if (devsize < NILFS_SEG_MIN_BLOCKS * NILFS_MIN_BLOCK_SIZE + 4096) {
602                 nilfs_err(sb, "device size too    604                 nilfs_err(sb, "device size too small");
603                 return -EINVAL;                   605                 return -EINVAL;
604         }                                         606         }
605         sb2off = NILFS_SB2_OFFSET_BYTES(devsiz    607         sb2off = NILFS_SB2_OFFSET_BYTES(devsize);
606                                                   608 
607         sbp[0] = nilfs_read_super_block(sb, NI    609         sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
608                                         &sbh[0    610                                         &sbh[0]);
609         sbp[1] = nilfs_read_super_block(sb, sb    611         sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
610                                                   612 
611         if (!sbp[0]) {                            613         if (!sbp[0]) {
612                 if (!sbp[1]) {                    614                 if (!sbp[1]) {
613                         nilfs_err(sb, "unable     615                         nilfs_err(sb, "unable to read superblock");
614                         return -EIO;              616                         return -EIO;
615                 }                                 617                 }
616                 nilfs_warn(sb,                    618                 nilfs_warn(sb,
617                            "unable to read pri    619                            "unable to read primary superblock (blocksize = %d)",
618                            blocksize);            620                            blocksize);
619         } else if (!sbp[1]) {                     621         } else if (!sbp[1]) {
620                 nilfs_warn(sb,                    622                 nilfs_warn(sb,
621                            "unable to read sec    623                            "unable to read secondary superblock (blocksize = %d)",
622                            blocksize);            624                            blocksize);
623         }                                         625         }
624                                                   626 
625         /*                                        627         /*
626          * Compare two super blocks and set 1     628          * Compare two super blocks and set 1 in swp if the secondary
627          * super block is valid and newer.  Ot    629          * super block is valid and newer.  Otherwise, set 0 in swp.
628          */                                       630          */
629         valid[0] = nilfs_valid_sb(sbp[0]);        631         valid[0] = nilfs_valid_sb(sbp[0]);
630         valid[1] = nilfs_valid_sb(sbp[1]);        632         valid[1] = nilfs_valid_sb(sbp[1]);
631         swp = valid[1] && (!valid[0] ||           633         swp = valid[1] && (!valid[0] ||
632                            le64_to_cpu(sbp[1]-    634                            le64_to_cpu(sbp[1]->s_last_cno) >
633                            le64_to_cpu(sbp[0]-    635                            le64_to_cpu(sbp[0]->s_last_cno));
634                                                   636 
635         if (valid[swp] && nilfs_sb2_bad_offset    637         if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
636                 brelse(sbh[1]);                   638                 brelse(sbh[1]);
637                 sbh[1] = NULL;                    639                 sbh[1] = NULL;
638                 sbp[1] = NULL;                    640                 sbp[1] = NULL;
639                 valid[1] = 0;                     641                 valid[1] = 0;
640                 swp = 0;                          642                 swp = 0;
641         }                                         643         }
642         if (!valid[swp]) {                        644         if (!valid[swp]) {
643                 nilfs_release_super_block(nilf    645                 nilfs_release_super_block(nilfs);
644                 nilfs_err(sb, "couldn't find n    646                 nilfs_err(sb, "couldn't find nilfs on the device");
645                 return -EINVAL;                   647                 return -EINVAL;
646         }                                         648         }
647                                                   649 
648         if (!valid[!swp])                         650         if (!valid[!swp])
649                 nilfs_warn(sb,                    651                 nilfs_warn(sb,
650                            "broken superblock,    652                            "broken superblock, retrying with spare superblock (blocksize = %d)",
651                            blocksize);            653                            blocksize);
652         if (swp)                                  654         if (swp)
653                 nilfs_swap_super_block(nilfs);    655                 nilfs_swap_super_block(nilfs);
654                                                   656 
655         /*                                     << 
656          * Calculate the array index of the ol << 
657          * If one has been dropped, set index  << 
658          * otherwise set index 1 pointing to t << 
659          * are the same).                      << 
660          *                                     << 
661          *  Divided case             valid[0]  << 
662          *  ---------------------------------- << 
663          *  Both SBs are invalid        0      << 
664          *  SB1 is invalid              0      << 
665          *  SB2 is invalid              1      << 
666          *  SB2 is newer                1      << 
667          *  SB2 is older or the same    1      << 
668          */                                    << 
669         older = valid[1] ^ swp;                << 
670                                                << 
671         nilfs->ns_sbwcount = 0;                   657         nilfs->ns_sbwcount = 0;
672         nilfs->ns_sbwtime = le64_to_cpu(sbp[0]    658         nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
673         nilfs->ns_prot_seq = le64_to_cpu(sbp[o !! 659         nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
674         *sbpp = sbp[0];                           660         *sbpp = sbp[0];
675         return 0;                                 661         return 0;
676 }                                                 662 }
677                                                   663 
678 /**                                               664 /**
679  * init_nilfs - initialize a NILFS instance.      665  * init_nilfs - initialize a NILFS instance.
680  * @nilfs: the_nilfs structure                    666  * @nilfs: the_nilfs structure
681  * @sb: super block                               667  * @sb: super block
                                                   >> 668  * @data: mount options
682  *                                                669  *
683  * init_nilfs() performs common initialization    670  * init_nilfs() performs common initialization per block device (e.g.
684  * reading the super block, getting disk layou    671  * reading the super block, getting disk layout information, initializing
685  * shared fields in the_nilfs).                   672  * shared fields in the_nilfs).
686  *                                                673  *
687  * Return Value: On success, 0 is returned. On    674  * Return Value: On success, 0 is returned. On error, a negative error
688  * code is returned.                              675  * code is returned.
689  */                                               676  */
690 int init_nilfs(struct the_nilfs *nilfs, struct !! 677 int init_nilfs(struct the_nilfs *nilfs, struct super_block *sb, char *data)
691 {                                                 678 {
692         struct nilfs_super_block *sbp;            679         struct nilfs_super_block *sbp;
693         int blocksize;                            680         int blocksize;
694         int err;                                  681         int err;
695                                                   682 
696         down_write(&nilfs->ns_sem);               683         down_write(&nilfs->ns_sem);
697                                                   684 
698         blocksize = sb_min_blocksize(sb, NILFS    685         blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
699         if (!blocksize) {                         686         if (!blocksize) {
700                 nilfs_err(sb, "unable to set b    687                 nilfs_err(sb, "unable to set blocksize");
701                 err = -EINVAL;                    688                 err = -EINVAL;
702                 goto out;                         689                 goto out;
703         }                                         690         }
704         err = nilfs_load_super_block(nilfs, sb    691         err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
705         if (err)                                  692         if (err)
706                 goto out;                         693                 goto out;
707                                                   694 
708         err = nilfs_store_magic(sb, sbp);      !! 695         err = nilfs_store_magic_and_option(sb, sbp, data);
709         if (err)                                  696         if (err)
710                 goto failed_sbh;                  697                 goto failed_sbh;
711                                                   698 
712         err = nilfs_check_feature_compatibilit    699         err = nilfs_check_feature_compatibility(sb, sbp);
713         if (err)                                  700         if (err)
714                 goto failed_sbh;                  701                 goto failed_sbh;
715                                                   702 
716         err = nilfs_get_blocksize(sb, sbp, &bl    703         err = nilfs_get_blocksize(sb, sbp, &blocksize);
717         if (err)                                  704         if (err)
718                 goto failed_sbh;                  705                 goto failed_sbh;
719                                                   706 
720         if (blocksize < NILFS_MIN_BLOCK_SIZE)     707         if (blocksize < NILFS_MIN_BLOCK_SIZE) {
721                 nilfs_err(sb,                     708                 nilfs_err(sb,
722                           "couldn't mount beca    709                           "couldn't mount because of unsupported filesystem blocksize %d",
723                           blocksize);             710                           blocksize);
724                 err = -EINVAL;                    711                 err = -EINVAL;
725                 goto failed_sbh;                  712                 goto failed_sbh;
726         }                                         713         }
727         if (sb->s_blocksize != blocksize) {       714         if (sb->s_blocksize != blocksize) {
728                 int hw_blocksize = bdev_logica    715                 int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
729                                                   716 
730                 if (blocksize < hw_blocksize)     717                 if (blocksize < hw_blocksize) {
731                         nilfs_err(sb,             718                         nilfs_err(sb,
732                                   "blocksize %    719                                   "blocksize %d too small for device (sector-size = %d)",
733                                   blocksize, h    720                                   blocksize, hw_blocksize);
734                         err = -EINVAL;            721                         err = -EINVAL;
735                         goto failed_sbh;          722                         goto failed_sbh;
736                 }                                 723                 }
737                 nilfs_release_super_block(nilf    724                 nilfs_release_super_block(nilfs);
738                 if (!sb_set_blocksize(sb, bloc    725                 if (!sb_set_blocksize(sb, blocksize)) {
739                         nilfs_err(sb, "bad blo    726                         nilfs_err(sb, "bad blocksize %d", blocksize);
740                         err = -EINVAL;            727                         err = -EINVAL;
741                         goto out;                 728                         goto out;
742                 }                                 729                 }
743                                                   730 
744                 err = nilfs_load_super_block(n    731                 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
745                 if (err)                          732                 if (err)
746                         goto out;                 733                         goto out;
747                         /*                        734                         /*
748                          * Not to failed_sbh;     735                          * Not to failed_sbh; sbh is released automatically
749                          * when reloading fail    736                          * when reloading fails.
750                          */                       737                          */
751         }                                         738         }
752         nilfs->ns_blocksize_bits = sb->s_block    739         nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
753         nilfs->ns_blocksize = blocksize;          740         nilfs->ns_blocksize = blocksize;
                                                   >> 741 
                                                   >> 742         get_random_bytes(&nilfs->ns_next_generation,
                                                   >> 743                          sizeof(nilfs->ns_next_generation));
754                                                   744 
755         err = nilfs_store_disk_layout(nilfs, s    745         err = nilfs_store_disk_layout(nilfs, sbp);
756         if (err)                                  746         if (err)
757                 goto failed_sbh;                  747                 goto failed_sbh;
758                                                   748 
759         sb->s_maxbytes = nilfs_max_size(sb->s_    749         sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
760                                                   750 
761         nilfs->ns_mount_state = le16_to_cpu(sb    751         nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
762                                                   752 
763         err = nilfs_store_log_cursor(nilfs, sb    753         err = nilfs_store_log_cursor(nilfs, sbp);
764         if (err)                                  754         if (err)
765                 goto failed_sbh;                  755                 goto failed_sbh;
766                                                   756 
767         set_nilfs_init(nilfs);                    757         set_nilfs_init(nilfs);
768         err = 0;                                  758         err = 0;
769  out:                                             759  out:
770         up_write(&nilfs->ns_sem);                 760         up_write(&nilfs->ns_sem);
771         return err;                               761         return err;
772                                                   762 
773  failed_sbh:                                      763  failed_sbh:
774         nilfs_release_super_block(nilfs);         764         nilfs_release_super_block(nilfs);
775         goto out;                                 765         goto out;
776 }                                                 766 }
777                                                   767 
778 int nilfs_discard_segments(struct the_nilfs *n    768 int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
779                             size_t nsegs)         769                             size_t nsegs)
780 {                                                 770 {
781         sector_t seg_start, seg_end;              771         sector_t seg_start, seg_end;
782         sector_t start = 0, nblocks = 0;          772         sector_t start = 0, nblocks = 0;
783         unsigned int sects_per_block;             773         unsigned int sects_per_block;
784         __u64 *sn;                                774         __u64 *sn;
785         int ret = 0;                              775         int ret = 0;
786                                                   776 
787         sects_per_block = (1 << nilfs->ns_bloc    777         sects_per_block = (1 << nilfs->ns_blocksize_bits) /
788                 bdev_logical_block_size(nilfs-    778                 bdev_logical_block_size(nilfs->ns_bdev);
789         for (sn = segnump; sn < segnump + nseg    779         for (sn = segnump; sn < segnump + nsegs; sn++) {
790                 nilfs_get_segment_range(nilfs,    780                 nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
791                                                   781 
792                 if (!nblocks) {                   782                 if (!nblocks) {
793                         start = seg_start;        783                         start = seg_start;
794                         nblocks = seg_end - se    784                         nblocks = seg_end - seg_start + 1;
795                 } else if (start + nblocks ==     785                 } else if (start + nblocks == seg_start) {
796                         nblocks += seg_end - s    786                         nblocks += seg_end - seg_start + 1;
797                 } else {                          787                 } else {
798                         ret = blkdev_issue_dis    788                         ret = blkdev_issue_discard(nilfs->ns_bdev,
799                                                   789                                                    start * sects_per_block,
800                                                   790                                                    nblocks * sects_per_block,
801                                                   791                                                    GFP_NOFS);
802                         if (ret < 0)              792                         if (ret < 0)
803                                 return ret;       793                                 return ret;
804                         nblocks = 0;              794                         nblocks = 0;
805                 }                                 795                 }
806         }                                         796         }
807         if (nblocks)                              797         if (nblocks)
808                 ret = blkdev_issue_discard(nil    798                 ret = blkdev_issue_discard(nilfs->ns_bdev,
809                                            sta    799                                            start * sects_per_block,
810                                            nbl    800                                            nblocks * sects_per_block,
811                                            GFP    801                                            GFP_NOFS);
812         return ret;                               802         return ret;
813 }                                                 803 }
814                                                   804 
815 int nilfs_count_free_blocks(struct the_nilfs *    805 int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
816 {                                                 806 {
817         unsigned long ncleansegs;                 807         unsigned long ncleansegs;
818                                                   808 
819         ncleansegs = nilfs_sufile_get_ncleanse    809         ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
820         *nblocks = (sector_t)ncleansegs * nilf    810         *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
821         return 0;                                 811         return 0;
822 }                                                 812 }
823                                                   813 
824 int nilfs_near_disk_full(struct the_nilfs *nil    814 int nilfs_near_disk_full(struct the_nilfs *nilfs)
825 {                                                 815 {
826         unsigned long ncleansegs, nincsegs;       816         unsigned long ncleansegs, nincsegs;
827                                                   817 
828         ncleansegs = nilfs_sufile_get_ncleanse    818         ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
829         nincsegs = atomic_read(&nilfs->ns_ndir    819         nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
830                 nilfs->ns_blocks_per_segment +    820                 nilfs->ns_blocks_per_segment + 1;
831                                                   821 
832         return ncleansegs <= nilfs->ns_nrsvseg    822         return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
833 }                                                 823 }
834                                                   824 
835 struct nilfs_root *nilfs_lookup_root(struct th    825 struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
836 {                                                 826 {
837         struct rb_node *n;                        827         struct rb_node *n;
838         struct nilfs_root *root;                  828         struct nilfs_root *root;
839                                                   829 
840         spin_lock(&nilfs->ns_cptree_lock);        830         spin_lock(&nilfs->ns_cptree_lock);
841         n = nilfs->ns_cptree.rb_node;             831         n = nilfs->ns_cptree.rb_node;
842         while (n) {                               832         while (n) {
843                 root = rb_entry(n, struct nilf    833                 root = rb_entry(n, struct nilfs_root, rb_node);
844                                                   834 
845                 if (cno < root->cno) {            835                 if (cno < root->cno) {
846                         n = n->rb_left;           836                         n = n->rb_left;
847                 } else if (cno > root->cno) {     837                 } else if (cno > root->cno) {
848                         n = n->rb_right;          838                         n = n->rb_right;
849                 } else {                          839                 } else {
850                         refcount_inc(&root->co    840                         refcount_inc(&root->count);
851                         spin_unlock(&nilfs->ns    841                         spin_unlock(&nilfs->ns_cptree_lock);
852                         return root;              842                         return root;
853                 }                                 843                 }
854         }                                         844         }
855         spin_unlock(&nilfs->ns_cptree_lock);      845         spin_unlock(&nilfs->ns_cptree_lock);
856                                                   846 
857         return NULL;                              847         return NULL;
858 }                                                 848 }
859                                                   849 
860 struct nilfs_root *                               850 struct nilfs_root *
861 nilfs_find_or_create_root(struct the_nilfs *ni    851 nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
862 {                                                 852 {
863         struct rb_node **p, *parent;              853         struct rb_node **p, *parent;
864         struct nilfs_root *root, *new;            854         struct nilfs_root *root, *new;
865         int err;                                  855         int err;
866                                                   856 
867         root = nilfs_lookup_root(nilfs, cno);     857         root = nilfs_lookup_root(nilfs, cno);
868         if (root)                                 858         if (root)
869                 return root;                      859                 return root;
870                                                   860 
871         new = kzalloc(sizeof(*root), GFP_KERNE    861         new = kzalloc(sizeof(*root), GFP_KERNEL);
872         if (!new)                                 862         if (!new)
873                 return NULL;                      863                 return NULL;
874                                                   864 
875         spin_lock(&nilfs->ns_cptree_lock);        865         spin_lock(&nilfs->ns_cptree_lock);
876                                                   866 
877         p = &nilfs->ns_cptree.rb_node;            867         p = &nilfs->ns_cptree.rb_node;
878         parent = NULL;                            868         parent = NULL;
879                                                   869 
880         while (*p) {                              870         while (*p) {
881                 parent = *p;                      871                 parent = *p;
882                 root = rb_entry(parent, struct    872                 root = rb_entry(parent, struct nilfs_root, rb_node);
883                                                   873 
884                 if (cno < root->cno) {            874                 if (cno < root->cno) {
885                         p = &(*p)->rb_left;       875                         p = &(*p)->rb_left;
886                 } else if (cno > root->cno) {     876                 } else if (cno > root->cno) {
887                         p = &(*p)->rb_right;      877                         p = &(*p)->rb_right;
888                 } else {                          878                 } else {
889                         refcount_inc(&root->co    879                         refcount_inc(&root->count);
890                         spin_unlock(&nilfs->ns    880                         spin_unlock(&nilfs->ns_cptree_lock);
891                         kfree(new);               881                         kfree(new);
892                         return root;              882                         return root;
893                 }                                 883                 }
894         }                                         884         }
895                                                   885 
896         new->cno = cno;                           886         new->cno = cno;
897         new->ifile = NULL;                        887         new->ifile = NULL;
898         new->nilfs = nilfs;                       888         new->nilfs = nilfs;
899         refcount_set(&new->count, 1);             889         refcount_set(&new->count, 1);
900         atomic64_set(&new->inodes_count, 0);      890         atomic64_set(&new->inodes_count, 0);
901         atomic64_set(&new->blocks_count, 0);      891         atomic64_set(&new->blocks_count, 0);
902                                                   892 
903         rb_link_node(&new->rb_node, parent, p)    893         rb_link_node(&new->rb_node, parent, p);
904         rb_insert_color(&new->rb_node, &nilfs-    894         rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
905                                                   895 
906         spin_unlock(&nilfs->ns_cptree_lock);      896         spin_unlock(&nilfs->ns_cptree_lock);
907                                                   897 
908         err = nilfs_sysfs_create_snapshot_grou    898         err = nilfs_sysfs_create_snapshot_group(new);
909         if (err) {                                899         if (err) {
910                 kfree(new);                       900                 kfree(new);
911                 new = NULL;                       901                 new = NULL;
912         }                                         902         }
913                                                   903 
914         return new;                               904         return new;
915 }                                                 905 }
916                                                   906 
917 void nilfs_put_root(struct nilfs_root *root)      907 void nilfs_put_root(struct nilfs_root *root)
918 {                                                 908 {
919         struct the_nilfs *nilfs = root->nilfs;    909         struct the_nilfs *nilfs = root->nilfs;
920                                                   910 
921         if (refcount_dec_and_lock(&root->count    911         if (refcount_dec_and_lock(&root->count, &nilfs->ns_cptree_lock)) {
922                 rb_erase(&root->rb_node, &nilf    912                 rb_erase(&root->rb_node, &nilfs->ns_cptree);
923                 spin_unlock(&nilfs->ns_cptree_    913                 spin_unlock(&nilfs->ns_cptree_lock);
924                                                   914 
925                 nilfs_sysfs_delete_snapshot_gr    915                 nilfs_sysfs_delete_snapshot_group(root);
926                 iput(root->ifile);                916                 iput(root->ifile);
927                                                   917 
928                 kfree(root);                      918                 kfree(root);
929         }                                         919         }
930 }                                                 920 }
931                                                   921 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php