Blame view

fs/nilfs2/the_nilfs.h 10.4 KB
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  /*
   * the_nilfs.h - the_nilfs shared structure.
   *
   * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   *
   * Written by Ryusuke Konishi <ryusuke@osrg.net>
   *
   */
  
  #ifndef _THE_NILFS_H
  #define _THE_NILFS_H
  
  #include <linux/types.h>
  #include <linux/buffer_head.h>
  #include <linux/fs.h>
  #include <linux/blkdev.h>
  #include <linux/backing-dev.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
32
  #include <linux/slab.h>
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
33
34
35
36
37
38
39
40
  #include "sb.h"
  
  /* the_nilfs struct */
  enum {
  	THE_NILFS_INIT = 0,     /* Information from super_block is set */
  	THE_NILFS_LOADED,       /* Roll-back/roll-forward has done and
  				   the latest checkpoint was loaded */
  	THE_NILFS_DISCONTINUED,	/* 'next' pointer chain has broken */
1cf58fa84   Jiro SEKIBA   nilfs2: shorten f...
41
  	THE_NILFS_GC_RUNNING,	/* gc process is running */
e605f0a72   Ryusuke Konishi   nilfs2: get rid o...
42
  	THE_NILFS_SB_DIRTY,	/* super block is dirty */
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
43
44
45
46
47
48
  };
  
  /**
   * struct the_nilfs - struct to supervise multiple nilfs mount points
   * @ns_flags: flags
   * @ns_count: reference count
33c8e57c8   Ryusuke Konishi   nilfs2: get rid o...
49
   * @ns_list: list head for nilfs_list
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
50
51
52
53
   * @ns_bdev: block device
   * @ns_bdi: backing dev info
   * @ns_writer: back pointer to writable nilfs_sb_info
   * @ns_sem: semaphore for shared states
e59399d01   Ryusuke Konishi   nilfs2: correct e...
54
   * @ns_super_sem: semaphore for global operations across super block instances
aa7dfb895   Ryusuke Konishi   nilfs2: get rid o...
55
   * @ns_mount_mutex: mutex protecting mount process of nilfs
027d6404e   Ryusuke Konishi   nilfs2: use semap...
56
   * @ns_writer_sem: semaphore protecting ns_writer attach/detach
3f82ff551   Ryusuke Konishi   nilfs2: get rid o...
57
   * @ns_current: back pointer to current mount
e339ad31f   Ryusuke Konishi   nilfs2: introduce...
58
59
   * @ns_sbh: buffer heads of on-disk super blocks
   * @ns_sbp: pointers to super block data
b2ac86e1a   Jiro SEKIBA   nilfs2: sync supe...
60
61
   * @ns_sbwtime: previous write time of super block
   * @ns_sbwcount: write count of super block
e339ad31f   Ryusuke Konishi   nilfs2: introduce...
62
   * @ns_sbsize: size of valid data in super block
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
63
64
65
66
67
68
69
70
71
72
73
74
75
   * @ns_supers: list of nilfs super block structs
   * @ns_seg_seq: segment sequence counter
   * @ns_segnum: index number of the latest full segment.
   * @ns_nextnum: index number of the full segment index to be used next
   * @ns_pseg_offset: offset of next partial segment in the current full segment
   * @ns_cno: next checkpoint number
   * @ns_ctime: write time of the last segment
   * @ns_nongc_ctime: write time of the last segment not for cleaner operation
   * @ns_ndirtyblks: Number of dirty data blocks
   * @ns_last_segment_lock: lock protecting fields for the latest segment
   * @ns_last_pseg: start block number of the latest segment
   * @ns_last_seq: sequence value of the latest segment
   * @ns_last_cno: checkpoint number of the latest segment
2c2e52fc4   Ryusuke Konishi   nilfs2: extend ni...
76
   * @ns_prot_seq: least sequence number of segments which must not be reclaimed
325020477   Ryusuke Konishi   nilfs2: do not up...
77
   * @ns_prev_seq: base sequence number used to decide if advance log cursor
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
78
79
80
81
82
83
84
   * @ns_segctor_sem: segment constructor semaphore
   * @ns_dat: DAT file inode
   * @ns_cpfile: checkpoint file inode
   * @ns_sufile: segusage file inode
   * @ns_gc_dat: shadow inode of the DAT file inode for GC
   * @ns_gc_inodes: dummy inodes to keep live blocks
   * @ns_gc_inodes_h: hash list to keep dummy inode holding live blocks
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
85
   * @ns_blocksize_bits: bit length of block size
92c60ccaf   Ryusuke Konishi   nilfs2: add block...
86
   * @ns_blocksize: block size
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
87
88
89
90
91
92
93
94
95
96
97
98
   * @ns_nsegments: number of segments in filesystem
   * @ns_blocks_per_segment: number of blocks per segment
   * @ns_r_segments_percentage: reserved segments percentage
   * @ns_nrsvsegs: number of reserved segments
   * @ns_first_data_block: block number of first data block
   * @ns_inode_size: size of on-disk inode
   * @ns_first_ino: first not-special inode number
   * @ns_crc_seed: seed value of CRC32 calculation
   */
  struct the_nilfs {
  	unsigned long		ns_flags;
  	atomic_t		ns_count;
33c8e57c8   Ryusuke Konishi   nilfs2: get rid o...
99
  	struct list_head	ns_list;
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
100
101
102
103
104
  
  	struct block_device    *ns_bdev;
  	struct backing_dev_info *ns_bdi;
  	struct nilfs_sb_info   *ns_writer;
  	struct rw_semaphore	ns_sem;
e59399d01   Ryusuke Konishi   nilfs2: correct e...
105
  	struct rw_semaphore	ns_super_sem;
aa7dfb895   Ryusuke Konishi   nilfs2: get rid o...
106
  	struct mutex		ns_mount_mutex;
027d6404e   Ryusuke Konishi   nilfs2: use semap...
107
  	struct rw_semaphore	ns_writer_sem;
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
108

e59399d01   Ryusuke Konishi   nilfs2: correct e...
109
110
111
  	/*
  	 * components protected by ns_super_sem
  	 */
3f82ff551   Ryusuke Konishi   nilfs2: get rid o...
112
  	struct nilfs_sb_info   *ns_current;
e59399d01   Ryusuke Konishi   nilfs2: correct e...
113
  	struct list_head	ns_supers;
3f82ff551   Ryusuke Konishi   nilfs2: get rid o...
114

65b4643d3   Ryusuke Konishi   nilfs2: add inode...
115
116
117
118
119
120
121
  	/*
  	 * used for
  	 * - loading the latest checkpoint exclusively.
  	 * - allocating a new full segment.
  	 * - protecting s_dirt in the super_block struct
  	 *   (see nilfs_write_super) and the following fields.
  	 */
e339ad31f   Ryusuke Konishi   nilfs2: introduce...
122
123
  	struct buffer_head     *ns_sbh[2];
  	struct nilfs_super_block *ns_sbp[2];
b2ac86e1a   Jiro SEKIBA   nilfs2: sync supe...
124
125
  	time_t			ns_sbwtime;
  	unsigned		ns_sbwcount;
e339ad31f   Ryusuke Konishi   nilfs2: introduce...
126
  	unsigned		ns_sbsize;
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
127
  	unsigned		ns_mount_state;
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
128
129
130
131
  
  	/*
  	 * Following fields are dedicated to a writable FS-instance.
  	 * Except for the period seeking checkpoint, code outside the segment
47420c799   Ryusuke Konishi   nilfs2: avoid dou...
132
133
  	 * constructor must lock a segment semaphore while accessing these
  	 * fields.
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  	 * The writable FS-instance is sole during a lifetime of the_nilfs.
  	 */
  	u64			ns_seg_seq;
  	__u64			ns_segnum;
  	__u64			ns_nextnum;
  	unsigned long		ns_pseg_offset;
  	__u64			ns_cno;
  	time_t			ns_ctime;
  	time_t			ns_nongc_ctime;
  	atomic_t		ns_ndirtyblks;
  
  	/*
  	 * The following fields hold information on the latest partial segment
  	 * written to disk with a super root.  These fields are protected by
  	 * ns_last_segment_lock.
  	 */
  	spinlock_t		ns_last_segment_lock;
  	sector_t		ns_last_pseg;
  	u64			ns_last_seq;
  	__u64			ns_last_cno;
2c2e52fc4   Ryusuke Konishi   nilfs2: extend ni...
154
  	u64			ns_prot_seq;
325020477   Ryusuke Konishi   nilfs2: do not up...
155
  	u64			ns_prev_seq;
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
  
  	struct rw_semaphore	ns_segctor_sem;
  
  	/*
  	 * Following fields are lock free except for the period before
  	 * the_nilfs is initialized.
  	 */
  	struct inode	       *ns_dat;
  	struct inode	       *ns_cpfile;
  	struct inode	       *ns_sufile;
  	struct inode	       *ns_gc_dat;
  
  	/* GC inode list and hash table head */
  	struct list_head	ns_gc_inodes;
  	struct hlist_head      *ns_gc_inodes_h;
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
171
172
  	/* Disk layout information (static) */
  	unsigned int		ns_blocksize_bits;
92c60ccaf   Ryusuke Konishi   nilfs2: add block...
173
  	unsigned int		ns_blocksize;
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  	unsigned long		ns_nsegments;
  	unsigned long		ns_blocks_per_segment;
  	unsigned long		ns_r_segments_percentage;
  	unsigned long		ns_nrsvsegs;
  	unsigned long		ns_first_data_block;
  	int			ns_inode_size;
  	int			ns_first_ino;
  	u32			ns_crc_seed;
  };
  
  #define NILFS_GCINODE_HASH_BITS		8
  #define NILFS_GCINODE_HASH_SIZE		(1<<NILFS_GCINODE_HASH_BITS)
  
  #define THE_NILFS_FNS(bit, name)					\
  static inline void set_nilfs_##name(struct the_nilfs *nilfs)		\
  {									\
  	set_bit(THE_NILFS_##bit, &(nilfs)->ns_flags);			\
  }									\
  static inline void clear_nilfs_##name(struct the_nilfs *nilfs)		\
  {									\
  	clear_bit(THE_NILFS_##bit, &(nilfs)->ns_flags);			\
  }									\
  static inline int nilfs_##name(struct the_nilfs *nilfs)			\
  {									\
  	return test_bit(THE_NILFS_##bit, &(nilfs)->ns_flags);		\
  }
  
  THE_NILFS_FNS(INIT, init)
  THE_NILFS_FNS(LOADED, loaded)
  THE_NILFS_FNS(DISCONTINUED, discontinued)
1cf58fa84   Jiro SEKIBA   nilfs2: shorten f...
204
  THE_NILFS_FNS(GC_RUNNING, gc_running)
e605f0a72   Ryusuke Konishi   nilfs2: get rid o...
205
  THE_NILFS_FNS(SB_DIRTY, sb_dirty)
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
206

e339ad31f   Ryusuke Konishi   nilfs2: introduce...
207
208
  /* Minimum interval of periodical update of superblocks (in seconds) */
  #define NILFS_SB_FREQ		10
e339ad31f   Ryusuke Konishi   nilfs2: introduce...
209

79efdd941   Jiro SEKIBA   nilfs2: clean up ...
210
211
212
  static inline int nilfs_sb_need_update(struct the_nilfs *nilfs)
  {
  	u64 t = get_seconds();
b2ac86e1a   Jiro SEKIBA   nilfs2: sync supe...
213
  	return t < nilfs->ns_sbwtime || t > nilfs->ns_sbwtime + NILFS_SB_FREQ;
79efdd941   Jiro SEKIBA   nilfs2: clean up ...
214
  }
b2ac86e1a   Jiro SEKIBA   nilfs2: sync supe...
215
  static inline int nilfs_sb_will_flip(struct the_nilfs *nilfs)
79efdd941   Jiro SEKIBA   nilfs2: clean up ...
216
  {
b2ac86e1a   Jiro SEKIBA   nilfs2: sync supe...
217
218
  	int flip_bits = nilfs->ns_sbwcount & 0x0FL;
  	return (flip_bits != 0x08 && flip_bits != 0x0F);
79efdd941   Jiro SEKIBA   nilfs2: clean up ...
219
  }
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
220
  void nilfs_set_last_segment(struct the_nilfs *, sector_t, u64, __u64);
33c8e57c8   Ryusuke Konishi   nilfs2: get rid o...
221
  struct the_nilfs *find_or_create_nilfs(struct block_device *);
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
222
223
224
  void put_nilfs(struct the_nilfs *);
  int init_nilfs(struct the_nilfs *, struct nilfs_sb_info *, char *);
  int load_nilfs(struct the_nilfs *, struct nilfs_sb_info *);
e902ec990   Jiro SEKIBA   nilfs2: issue dis...
225
  int nilfs_discard_segments(struct the_nilfs *, __u64 *, size_t);
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
226
  int nilfs_count_free_blocks(struct the_nilfs *, sector_t *);
6dd474066   Ryusuke Konishi   nilfs2: simplify ...
227
  struct nilfs_sb_info *nilfs_find_sbinfo(struct the_nilfs *, int, __u64);
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
228
229
  int nilfs_checkpoint_is_mounted(struct the_nilfs *, __u64, int);
  int nilfs_near_disk_full(struct the_nilfs *);
e339ad31f   Ryusuke Konishi   nilfs2: introduce...
230
231
  void nilfs_fall_back_super_block(struct the_nilfs *);
  void nilfs_swap_super_block(struct the_nilfs *);
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
232
233
234
235
236
237
238
  
  
  static inline void get_nilfs(struct the_nilfs *nilfs)
  {
  	/* Caller must have at least one reference of the_nilfs. */
  	atomic_inc(&nilfs->ns_count);
  }
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
239
240
241
  static inline void
  nilfs_attach_writer(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
  {
027d6404e   Ryusuke Konishi   nilfs2: use semap...
242
  	down_write(&nilfs->ns_writer_sem);
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
243
  	nilfs->ns_writer = sbi;
027d6404e   Ryusuke Konishi   nilfs2: use semap...
244
  	up_write(&nilfs->ns_writer_sem);
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
245
246
247
248
249
  }
  
  static inline void
  nilfs_detach_writer(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
  {
027d6404e   Ryusuke Konishi   nilfs2: use semap...
250
  	down_write(&nilfs->ns_writer_sem);
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
251
252
  	if (sbi == nilfs->ns_writer)
  		nilfs->ns_writer = NULL;
027d6404e   Ryusuke Konishi   nilfs2: use semap...
253
  	up_write(&nilfs->ns_writer_sem);
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
254
  }
6dd474066   Ryusuke Konishi   nilfs2: simplify ...
255
256
  static inline void nilfs_put_sbinfo(struct nilfs_sb_info *sbi)
  {
a92458603   Ryusuke Konishi   nilfs2: fix oopse...
257
  	if (atomic_dec_and_test(&sbi->s_count))
6dd474066   Ryusuke Konishi   nilfs2: simplify ...
258
259
  		kfree(sbi);
  }
a057d2c01   Ryusuke Konishi   nilfs2: add helpe...
260
261
262
263
264
265
266
267
268
  static inline int nilfs_valid_fs(struct the_nilfs *nilfs)
  {
  	unsigned valid_fs;
  
  	down_read(&nilfs->ns_sem);
  	valid_fs = (nilfs->ns_mount_state & NILFS_VALID_FS);
  	up_read(&nilfs->ns_sem);
  	return valid_fs;
  }
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
  static inline void
  nilfs_get_segment_range(struct the_nilfs *nilfs, __u64 segnum,
  			sector_t *seg_start, sector_t *seg_end)
  {
  	*seg_start = (sector_t)nilfs->ns_blocks_per_segment * segnum;
  	*seg_end = *seg_start + nilfs->ns_blocks_per_segment - 1;
  	if (segnum == 0)
  		*seg_start = nilfs->ns_first_data_block;
  }
  
  static inline sector_t
  nilfs_get_segment_start_blocknr(struct the_nilfs *nilfs, __u64 segnum)
  {
  	return (segnum == 0) ? nilfs->ns_first_data_block :
  		(sector_t)nilfs->ns_blocks_per_segment * segnum;
  }
  
  static inline __u64
  nilfs_get_segnum_of_block(struct the_nilfs *nilfs, sector_t blocknr)
  {
  	sector_t segnum = blocknr;
  
  	sector_div(segnum, nilfs->ns_blocks_per_segment);
  	return segnum;
  }
  
  static inline void
  nilfs_terminate_segment(struct the_nilfs *nilfs, sector_t seg_start,
  			sector_t seg_end)
  {
  	/* terminate the current full segment (used in case of I/O-error) */
  	nilfs->ns_pseg_offset = seg_end - seg_start + 1;
  }
  
  static inline void nilfs_shift_to_next_segment(struct the_nilfs *nilfs)
  {
  	/* move forward with a full segment */
  	nilfs->ns_segnum = nilfs->ns_nextnum;
  	nilfs->ns_pseg_offset = 0;
  	nilfs->ns_seg_seq++;
  }
  
  static inline __u64 nilfs_last_cno(struct the_nilfs *nilfs)
  {
  	__u64 cno;
  
  	spin_lock(&nilfs->ns_last_segment_lock);
  	cno = nilfs->ns_last_cno;
  	spin_unlock(&nilfs->ns_last_segment_lock);
  	return cno;
  }
cece55207   Ryusuke Konishi   nilfs2: simplify ...
320
321
322
323
  static inline int nilfs_segment_is_active(struct the_nilfs *nilfs, __u64 n)
  {
  	return n == nilfs->ns_segnum || n == nilfs->ns_nextnum;
  }
65b4643d3   Ryusuke Konishi   nilfs2: add inode...
324
  #endif /* _THE_NILFS_H */