| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * segment.c - NILFS segment constructor. | 
|  | 3 | * | 
|  | 4 | * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. | 
|  | 5 | * | 
|  | 6 | * This program is free software; you can redistribute it and/or modify | 
|  | 7 | * it under the terms of the GNU General Public License as published by | 
|  | 8 | * the Free Software Foundation; either version 2 of the License, or | 
|  | 9 | * (at your option) any later version. | 
|  | 10 | * | 
|  | 11 | * This program is distributed in the hope that it will be useful, | 
|  | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 14 | * GNU General Public License for more details. | 
|  | 15 | * | 
|  | 16 | * You should have received a copy of the GNU General Public License | 
|  | 17 | * along with this program; if not, write to the Free Software | 
|  | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA | 
|  | 19 | * | 
|  | 20 | * Written by Ryusuke Konishi <ryusuke@osrg.net> | 
|  | 21 | * | 
|  | 22 | */ | 
|  | 23 |  | 
|  | 24 | #include <linux/pagemap.h> | 
|  | 25 | #include <linux/buffer_head.h> | 
|  | 26 | #include <linux/writeback.h> | 
|  | 27 | #include <linux/bio.h> | 
|  | 28 | #include <linux/completion.h> | 
|  | 29 | #include <linux/blkdev.h> | 
|  | 30 | #include <linux/backing-dev.h> | 
|  | 31 | #include <linux/freezer.h> | 
|  | 32 | #include <linux/kthread.h> | 
|  | 33 | #include <linux/crc32.h> | 
|  | 34 | #include <linux/pagevec.h> | 
|  | 35 | #include <linux/slab.h> | 
|  | 36 | #include "nilfs.h" | 
|  | 37 | #include "btnode.h" | 
|  | 38 | #include "page.h" | 
|  | 39 | #include "segment.h" | 
|  | 40 | #include "sufile.h" | 
|  | 41 | #include "cpfile.h" | 
|  | 42 | #include "ifile.h" | 
|  | 43 | #include "segbuf.h" | 
|  | 44 |  | 
|  | 45 |  | 
|  | 46 | /* | 
|  | 47 | * Segment constructor | 
|  | 48 | */ | 
|  | 49 | #define SC_N_INODEVEC	16   /* Size of locally allocated inode vector */ | 
|  | 50 |  | 
|  | 51 | #define SC_MAX_SEGDELTA 64   /* Upper limit of the number of segments | 
|  | 52 | appended in collection retry loop */ | 
|  | 53 |  | 
|  | 54 | /* Construction mode */ | 
|  | 55 | enum { | 
|  | 56 | SC_LSEG_SR = 1,	/* Make a logical segment having a super root */ | 
|  | 57 | SC_LSEG_DSYNC,	/* Flush data blocks of a given file and make | 
|  | 58 | a logical segment without a super root */ | 
|  | 59 | SC_FLUSH_FILE,	/* Flush data files, leads to segment writes without | 
|  | 60 | creating a checkpoint */ | 
|  | 61 | SC_FLUSH_DAT,	/* Flush DAT file. This also creates segments without | 
|  | 62 | a checkpoint */ | 
|  | 63 | }; | 
|  | 64 |  | 
|  | 65 | /* Stage numbers of dirty block collection */ | 
|  | 66 | enum { | 
|  | 67 | NILFS_ST_INIT = 0, | 
|  | 68 | NILFS_ST_GC,		/* Collecting dirty blocks for GC */ | 
|  | 69 | NILFS_ST_FILE, | 
|  | 70 | NILFS_ST_IFILE, | 
|  | 71 | NILFS_ST_CPFILE, | 
|  | 72 | NILFS_ST_SUFILE, | 
|  | 73 | NILFS_ST_DAT, | 
|  | 74 | NILFS_ST_SR,		/* Super root */ | 
|  | 75 | NILFS_ST_DSYNC,		/* Data sync blocks */ | 
|  | 76 | NILFS_ST_DONE, | 
|  | 77 | }; | 
|  | 78 |  | 
|  | 79 | /* State flags of collection */ | 
|  | 80 | #define NILFS_CF_NODE		0x0001	/* Collecting node blocks */ | 
|  | 81 | #define NILFS_CF_IFILE_STARTED	0x0002	/* IFILE stage has started */ | 
|  | 82 | #define NILFS_CF_SUFREED	0x0004	/* segment usages has been freed */ | 
|  | 83 | #define NILFS_CF_HISTORY_MASK	(NILFS_CF_IFILE_STARTED | NILFS_CF_SUFREED) | 
|  | 84 |  | 
|  | 85 | /* Operations depending on the construction mode and file type */ | 
|  | 86 | struct nilfs_sc_operations { | 
|  | 87 | int (*collect_data)(struct nilfs_sc_info *, struct buffer_head *, | 
|  | 88 | struct inode *); | 
|  | 89 | int (*collect_node)(struct nilfs_sc_info *, struct buffer_head *, | 
|  | 90 | struct inode *); | 
|  | 91 | int (*collect_bmap)(struct nilfs_sc_info *, struct buffer_head *, | 
|  | 92 | struct inode *); | 
|  | 93 | void (*write_data_binfo)(struct nilfs_sc_info *, | 
|  | 94 | struct nilfs_segsum_pointer *, | 
|  | 95 | union nilfs_binfo *); | 
|  | 96 | void (*write_node_binfo)(struct nilfs_sc_info *, | 
|  | 97 | struct nilfs_segsum_pointer *, | 
|  | 98 | union nilfs_binfo *); | 
|  | 99 | }; | 
|  | 100 |  | 
|  | 101 | /* | 
|  | 102 | * Other definitions | 
|  | 103 | */ | 
|  | 104 | static void nilfs_segctor_start_timer(struct nilfs_sc_info *); | 
|  | 105 | static void nilfs_segctor_do_flush(struct nilfs_sc_info *, int); | 
|  | 106 | static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *); | 
|  | 107 | static void nilfs_dispose_list(struct the_nilfs *, struct list_head *, int); | 
|  | 108 |  | 
|  | 109 | #define nilfs_cnt32_gt(a, b)   \ | 
|  | 110 | (typecheck(__u32, a) && typecheck(__u32, b) && \ | 
|  | 111 | ((__s32)(b) - (__s32)(a) < 0)) | 
|  | 112 | #define nilfs_cnt32_ge(a, b)   \ | 
|  | 113 | (typecheck(__u32, a) && typecheck(__u32, b) && \ | 
|  | 114 | ((__s32)(a) - (__s32)(b) >= 0)) | 
|  | 115 | #define nilfs_cnt32_lt(a, b)  nilfs_cnt32_gt(b, a) | 
|  | 116 | #define nilfs_cnt32_le(a, b)  nilfs_cnt32_ge(b, a) | 
|  | 117 |  | 
|  | 118 | static int nilfs_prepare_segment_lock(struct nilfs_transaction_info *ti) | 
|  | 119 | { | 
|  | 120 | struct nilfs_transaction_info *cur_ti = current->journal_info; | 
|  | 121 | void *save = NULL; | 
|  | 122 |  | 
|  | 123 | if (cur_ti) { | 
|  | 124 | if (cur_ti->ti_magic == NILFS_TI_MAGIC) | 
|  | 125 | return ++cur_ti->ti_count; | 
|  | 126 | else { | 
|  | 127 | /* | 
|  | 128 | * If journal_info field is occupied by other FS, | 
|  | 129 | * it is saved and will be restored on | 
|  | 130 | * nilfs_transaction_commit(). | 
|  | 131 | */ | 
|  | 132 | printk(KERN_WARNING | 
|  | 133 | "NILFS warning: journal info from a different " | 
|  | 134 | "FS\n"); | 
|  | 135 | save = current->journal_info; | 
|  | 136 | } | 
|  | 137 | } | 
|  | 138 | if (!ti) { | 
|  | 139 | ti = kmem_cache_alloc(nilfs_transaction_cachep, GFP_NOFS); | 
|  | 140 | if (!ti) | 
|  | 141 | return -ENOMEM; | 
|  | 142 | ti->ti_flags = NILFS_TI_DYNAMIC_ALLOC; | 
|  | 143 | } else { | 
|  | 144 | ti->ti_flags = 0; | 
|  | 145 | } | 
|  | 146 | ti->ti_count = 0; | 
|  | 147 | ti->ti_save = save; | 
|  | 148 | ti->ti_magic = NILFS_TI_MAGIC; | 
|  | 149 | current->journal_info = ti; | 
|  | 150 | return 0; | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | /** | 
|  | 154 | * nilfs_transaction_begin - start indivisible file operations. | 
|  | 155 | * @sb: super block | 
|  | 156 | * @ti: nilfs_transaction_info | 
|  | 157 | * @vacancy_check: flags for vacancy rate checks | 
|  | 158 | * | 
|  | 159 | * nilfs_transaction_begin() acquires a reader/writer semaphore, called | 
|  | 160 | * the segment semaphore, to make a segment construction and write tasks | 
|  | 161 | * exclusive.  The function is used with nilfs_transaction_commit() in pairs. | 
|  | 162 | * The region enclosed by these two functions can be nested.  To avoid a | 
|  | 163 | * deadlock, the semaphore is only acquired or released in the outermost call. | 
|  | 164 | * | 
|  | 165 | * This function allocates a nilfs_transaction_info struct to keep context | 
|  | 166 | * information on it.  It is initialized and hooked onto the current task in | 
|  | 167 | * the outermost call.  If a pre-allocated struct is given to @ti, it is used | 
|  | 168 | * instead; otherwise a new struct is assigned from a slab. | 
|  | 169 | * | 
|  | 170 | * When @vacancy_check flag is set, this function will check the amount of | 
|  | 171 | * free space, and will wait for the GC to reclaim disk space if low capacity. | 
|  | 172 | * | 
|  | 173 | * Return Value: On success, 0 is returned. On error, one of the following | 
|  | 174 | * negative error code is returned. | 
|  | 175 | * | 
|  | 176 | * %-ENOMEM - Insufficient memory available. | 
|  | 177 | * | 
|  | 178 | * %-ENOSPC - No space left on device | 
|  | 179 | */ | 
|  | 180 | int nilfs_transaction_begin(struct super_block *sb, | 
|  | 181 | struct nilfs_transaction_info *ti, | 
|  | 182 | int vacancy_check) | 
|  | 183 | { | 
|  | 184 | struct the_nilfs *nilfs; | 
|  | 185 | int ret = nilfs_prepare_segment_lock(ti); | 
|  | 186 |  | 
|  | 187 | if (unlikely(ret < 0)) | 
|  | 188 | return ret; | 
|  | 189 | if (ret > 0) | 
|  | 190 | return 0; | 
|  | 191 |  | 
|  | 192 | vfs_check_frozen(sb, SB_FREEZE_WRITE); | 
|  | 193 |  | 
|  | 194 | nilfs = sb->s_fs_info; | 
|  | 195 | down_read(&nilfs->ns_segctor_sem); | 
|  | 196 | if (vacancy_check && nilfs_near_disk_full(nilfs)) { | 
|  | 197 | up_read(&nilfs->ns_segctor_sem); | 
|  | 198 | ret = -ENOSPC; | 
|  | 199 | goto failed; | 
|  | 200 | } | 
|  | 201 | return 0; | 
|  | 202 |  | 
|  | 203 | failed: | 
|  | 204 | ti = current->journal_info; | 
|  | 205 | current->journal_info = ti->ti_save; | 
|  | 206 | if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC) | 
|  | 207 | kmem_cache_free(nilfs_transaction_cachep, ti); | 
|  | 208 | return ret; | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | /** | 
|  | 212 | * nilfs_transaction_commit - commit indivisible file operations. | 
|  | 213 | * @sb: super block | 
|  | 214 | * | 
|  | 215 | * nilfs_transaction_commit() releases the read semaphore which is | 
|  | 216 | * acquired by nilfs_transaction_begin(). This is only performed | 
|  | 217 | * in outermost call of this function.  If a commit flag is set, | 
|  | 218 | * nilfs_transaction_commit() sets a timer to start the segment | 
|  | 219 | * constructor.  If a sync flag is set, it starts construction | 
|  | 220 | * directly. | 
|  | 221 | */ | 
|  | 222 | int nilfs_transaction_commit(struct super_block *sb) | 
|  | 223 | { | 
|  | 224 | struct nilfs_transaction_info *ti = current->journal_info; | 
|  | 225 | struct the_nilfs *nilfs = sb->s_fs_info; | 
|  | 226 | int err = 0; | 
|  | 227 |  | 
|  | 228 | BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC); | 
|  | 229 | ti->ti_flags |= NILFS_TI_COMMIT; | 
|  | 230 | if (ti->ti_count > 0) { | 
|  | 231 | ti->ti_count--; | 
|  | 232 | return 0; | 
|  | 233 | } | 
|  | 234 | if (nilfs->ns_writer) { | 
|  | 235 | struct nilfs_sc_info *sci = nilfs->ns_writer; | 
|  | 236 |  | 
|  | 237 | if (ti->ti_flags & NILFS_TI_COMMIT) | 
|  | 238 | nilfs_segctor_start_timer(sci); | 
|  | 239 | if (atomic_read(&nilfs->ns_ndirtyblks) > sci->sc_watermark) | 
|  | 240 | nilfs_segctor_do_flush(sci, 0); | 
|  | 241 | } | 
|  | 242 | up_read(&nilfs->ns_segctor_sem); | 
|  | 243 | current->journal_info = ti->ti_save; | 
|  | 244 |  | 
|  | 245 | if (ti->ti_flags & NILFS_TI_SYNC) | 
|  | 246 | err = nilfs_construct_segment(sb); | 
|  | 247 | if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC) | 
|  | 248 | kmem_cache_free(nilfs_transaction_cachep, ti); | 
|  | 249 | return err; | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | void nilfs_transaction_abort(struct super_block *sb) | 
|  | 253 | { | 
|  | 254 | struct nilfs_transaction_info *ti = current->journal_info; | 
|  | 255 | struct the_nilfs *nilfs = sb->s_fs_info; | 
|  | 256 |  | 
|  | 257 | BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC); | 
|  | 258 | if (ti->ti_count > 0) { | 
|  | 259 | ti->ti_count--; | 
|  | 260 | return; | 
|  | 261 | } | 
|  | 262 | up_read(&nilfs->ns_segctor_sem); | 
|  | 263 |  | 
|  | 264 | current->journal_info = ti->ti_save; | 
|  | 265 | if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC) | 
|  | 266 | kmem_cache_free(nilfs_transaction_cachep, ti); | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | void nilfs_relax_pressure_in_lock(struct super_block *sb) | 
|  | 270 | { | 
|  | 271 | struct the_nilfs *nilfs = sb->s_fs_info; | 
|  | 272 | struct nilfs_sc_info *sci = nilfs->ns_writer; | 
|  | 273 |  | 
|  | 274 | if (!sci || !sci->sc_flush_request) | 
|  | 275 | return; | 
|  | 276 |  | 
|  | 277 | set_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags); | 
|  | 278 | up_read(&nilfs->ns_segctor_sem); | 
|  | 279 |  | 
|  | 280 | down_write(&nilfs->ns_segctor_sem); | 
|  | 281 | if (sci->sc_flush_request && | 
|  | 282 | test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags)) { | 
|  | 283 | struct nilfs_transaction_info *ti = current->journal_info; | 
|  | 284 |  | 
|  | 285 | ti->ti_flags |= NILFS_TI_WRITER; | 
|  | 286 | nilfs_segctor_do_immediate_flush(sci); | 
|  | 287 | ti->ti_flags &= ~NILFS_TI_WRITER; | 
|  | 288 | } | 
|  | 289 | downgrade_write(&nilfs->ns_segctor_sem); | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 | static void nilfs_transaction_lock(struct super_block *sb, | 
|  | 293 | struct nilfs_transaction_info *ti, | 
|  | 294 | int gcflag) | 
|  | 295 | { | 
|  | 296 | struct nilfs_transaction_info *cur_ti = current->journal_info; | 
|  | 297 | struct the_nilfs *nilfs = sb->s_fs_info; | 
|  | 298 | struct nilfs_sc_info *sci = nilfs->ns_writer; | 
|  | 299 |  | 
|  | 300 | WARN_ON(cur_ti); | 
|  | 301 | ti->ti_flags = NILFS_TI_WRITER; | 
|  | 302 | ti->ti_count = 0; | 
|  | 303 | ti->ti_save = cur_ti; | 
|  | 304 | ti->ti_magic = NILFS_TI_MAGIC; | 
|  | 305 | current->journal_info = ti; | 
|  | 306 |  | 
|  | 307 | for (;;) { | 
|  | 308 | down_write(&nilfs->ns_segctor_sem); | 
|  | 309 | if (!test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags)) | 
|  | 310 | break; | 
|  | 311 |  | 
|  | 312 | nilfs_segctor_do_immediate_flush(sci); | 
|  | 313 |  | 
|  | 314 | up_write(&nilfs->ns_segctor_sem); | 
|  | 315 | yield(); | 
|  | 316 | } | 
|  | 317 | if (gcflag) | 
|  | 318 | ti->ti_flags |= NILFS_TI_GC; | 
|  | 319 | } | 
|  | 320 |  | 
|  | 321 | static void nilfs_transaction_unlock(struct super_block *sb) | 
|  | 322 | { | 
|  | 323 | struct nilfs_transaction_info *ti = current->journal_info; | 
|  | 324 | struct the_nilfs *nilfs = sb->s_fs_info; | 
|  | 325 |  | 
|  | 326 | BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC); | 
|  | 327 | BUG_ON(ti->ti_count > 0); | 
|  | 328 |  | 
|  | 329 | up_write(&nilfs->ns_segctor_sem); | 
|  | 330 | current->journal_info = ti->ti_save; | 
|  | 331 | } | 
|  | 332 |  | 
|  | 333 | static void *nilfs_segctor_map_segsum_entry(struct nilfs_sc_info *sci, | 
|  | 334 | struct nilfs_segsum_pointer *ssp, | 
|  | 335 | unsigned bytes) | 
|  | 336 | { | 
|  | 337 | struct nilfs_segment_buffer *segbuf = sci->sc_curseg; | 
|  | 338 | unsigned blocksize = sci->sc_super->s_blocksize; | 
|  | 339 | void *p; | 
|  | 340 |  | 
|  | 341 | if (unlikely(ssp->offset + bytes > blocksize)) { | 
|  | 342 | ssp->offset = 0; | 
|  | 343 | BUG_ON(NILFS_SEGBUF_BH_IS_LAST(ssp->bh, | 
|  | 344 | &segbuf->sb_segsum_buffers)); | 
|  | 345 | ssp->bh = NILFS_SEGBUF_NEXT_BH(ssp->bh); | 
|  | 346 | } | 
|  | 347 | p = ssp->bh->b_data + ssp->offset; | 
|  | 348 | ssp->offset += bytes; | 
|  | 349 | return p; | 
|  | 350 | } | 
|  | 351 |  | 
|  | 352 | /** | 
|  | 353 | * nilfs_segctor_reset_segment_buffer - reset the current segment buffer | 
|  | 354 | * @sci: nilfs_sc_info | 
|  | 355 | */ | 
|  | 356 | static int nilfs_segctor_reset_segment_buffer(struct nilfs_sc_info *sci) | 
|  | 357 | { | 
|  | 358 | struct nilfs_segment_buffer *segbuf = sci->sc_curseg; | 
|  | 359 | struct buffer_head *sumbh; | 
|  | 360 | unsigned sumbytes; | 
|  | 361 | unsigned flags = 0; | 
|  | 362 | int err; | 
|  | 363 |  | 
|  | 364 | if (nilfs_doing_gc()) | 
|  | 365 | flags = NILFS_SS_GC; | 
|  | 366 | err = nilfs_segbuf_reset(segbuf, flags, sci->sc_seg_ctime, sci->sc_cno); | 
|  | 367 | if (unlikely(err)) | 
|  | 368 | return err; | 
|  | 369 |  | 
|  | 370 | sumbh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers); | 
|  | 371 | sumbytes = segbuf->sb_sum.sumbytes; | 
|  | 372 | sci->sc_finfo_ptr.bh = sumbh;  sci->sc_finfo_ptr.offset = sumbytes; | 
|  | 373 | sci->sc_binfo_ptr.bh = sumbh;  sci->sc_binfo_ptr.offset = sumbytes; | 
|  | 374 | sci->sc_blk_cnt = sci->sc_datablk_cnt = 0; | 
|  | 375 | return 0; | 
|  | 376 | } | 
|  | 377 |  | 
|  | 378 | static int nilfs_segctor_feed_segment(struct nilfs_sc_info *sci) | 
|  | 379 | { | 
|  | 380 | sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks; | 
|  | 381 | if (NILFS_SEGBUF_IS_LAST(sci->sc_curseg, &sci->sc_segbufs)) | 
|  | 382 | return -E2BIG; /* The current segment is filled up | 
|  | 383 | (internal code) */ | 
|  | 384 | sci->sc_curseg = NILFS_NEXT_SEGBUF(sci->sc_curseg); | 
|  | 385 | return nilfs_segctor_reset_segment_buffer(sci); | 
|  | 386 | } | 
|  | 387 |  | 
|  | 388 | static int nilfs_segctor_add_super_root(struct nilfs_sc_info *sci) | 
|  | 389 | { | 
|  | 390 | struct nilfs_segment_buffer *segbuf = sci->sc_curseg; | 
|  | 391 | int err; | 
|  | 392 |  | 
|  | 393 | if (segbuf->sb_sum.nblocks >= segbuf->sb_rest_blocks) { | 
|  | 394 | err = nilfs_segctor_feed_segment(sci); | 
|  | 395 | if (err) | 
|  | 396 | return err; | 
|  | 397 | segbuf = sci->sc_curseg; | 
|  | 398 | } | 
|  | 399 | err = nilfs_segbuf_extend_payload(segbuf, &segbuf->sb_super_root); | 
|  | 400 | if (likely(!err)) | 
|  | 401 | segbuf->sb_sum.flags |= NILFS_SS_SR; | 
|  | 402 | return err; | 
|  | 403 | } | 
|  | 404 |  | 
|  | 405 | /* | 
|  | 406 | * Functions for making segment summary and payloads | 
|  | 407 | */ | 
|  | 408 | static int nilfs_segctor_segsum_block_required( | 
|  | 409 | struct nilfs_sc_info *sci, const struct nilfs_segsum_pointer *ssp, | 
|  | 410 | unsigned binfo_size) | 
|  | 411 | { | 
|  | 412 | unsigned blocksize = sci->sc_super->s_blocksize; | 
|  | 413 | /* Size of finfo and binfo is enough small against blocksize */ | 
|  | 414 |  | 
|  | 415 | return ssp->offset + binfo_size + | 
|  | 416 | (!sci->sc_blk_cnt ? sizeof(struct nilfs_finfo) : 0) > | 
|  | 417 | blocksize; | 
|  | 418 | } | 
|  | 419 |  | 
|  | 420 | static void nilfs_segctor_begin_finfo(struct nilfs_sc_info *sci, | 
|  | 421 | struct inode *inode) | 
|  | 422 | { | 
|  | 423 | sci->sc_curseg->sb_sum.nfinfo++; | 
|  | 424 | sci->sc_binfo_ptr = sci->sc_finfo_ptr; | 
|  | 425 | nilfs_segctor_map_segsum_entry( | 
|  | 426 | sci, &sci->sc_binfo_ptr, sizeof(struct nilfs_finfo)); | 
|  | 427 |  | 
|  | 428 | if (NILFS_I(inode)->i_root && | 
|  | 429 | !test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags)) | 
|  | 430 | set_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags); | 
|  | 431 | /* skip finfo */ | 
|  | 432 | } | 
|  | 433 |  | 
|  | 434 | static void nilfs_segctor_end_finfo(struct nilfs_sc_info *sci, | 
|  | 435 | struct inode *inode) | 
|  | 436 | { | 
|  | 437 | struct nilfs_finfo *finfo; | 
|  | 438 | struct nilfs_inode_info *ii; | 
|  | 439 | struct nilfs_segment_buffer *segbuf; | 
|  | 440 | __u64 cno; | 
|  | 441 |  | 
|  | 442 | if (sci->sc_blk_cnt == 0) | 
|  | 443 | return; | 
|  | 444 |  | 
|  | 445 | ii = NILFS_I(inode); | 
|  | 446 |  | 
|  | 447 | if (test_bit(NILFS_I_GCINODE, &ii->i_state)) | 
|  | 448 | cno = ii->i_cno; | 
|  | 449 | else if (NILFS_ROOT_METADATA_FILE(inode->i_ino)) | 
|  | 450 | cno = 0; | 
|  | 451 | else | 
|  | 452 | cno = sci->sc_cno; | 
|  | 453 |  | 
|  | 454 | finfo = nilfs_segctor_map_segsum_entry(sci, &sci->sc_finfo_ptr, | 
|  | 455 | sizeof(*finfo)); | 
|  | 456 | finfo->fi_ino = cpu_to_le64(inode->i_ino); | 
|  | 457 | finfo->fi_nblocks = cpu_to_le32(sci->sc_blk_cnt); | 
|  | 458 | finfo->fi_ndatablk = cpu_to_le32(sci->sc_datablk_cnt); | 
|  | 459 | finfo->fi_cno = cpu_to_le64(cno); | 
|  | 460 |  | 
|  | 461 | segbuf = sci->sc_curseg; | 
|  | 462 | segbuf->sb_sum.sumbytes = sci->sc_binfo_ptr.offset + | 
|  | 463 | sci->sc_super->s_blocksize * (segbuf->sb_sum.nsumblk - 1); | 
|  | 464 | sci->sc_finfo_ptr = sci->sc_binfo_ptr; | 
|  | 465 | sci->sc_blk_cnt = sci->sc_datablk_cnt = 0; | 
|  | 466 | } | 
|  | 467 |  | 
|  | 468 | static int nilfs_segctor_add_file_block(struct nilfs_sc_info *sci, | 
|  | 469 | struct buffer_head *bh, | 
|  | 470 | struct inode *inode, | 
|  | 471 | unsigned binfo_size) | 
|  | 472 | { | 
|  | 473 | struct nilfs_segment_buffer *segbuf; | 
|  | 474 | int required, err = 0; | 
|  | 475 |  | 
|  | 476 | retry: | 
|  | 477 | segbuf = sci->sc_curseg; | 
|  | 478 | required = nilfs_segctor_segsum_block_required( | 
|  | 479 | sci, &sci->sc_binfo_ptr, binfo_size); | 
|  | 480 | if (segbuf->sb_sum.nblocks + required + 1 > segbuf->sb_rest_blocks) { | 
|  | 481 | nilfs_segctor_end_finfo(sci, inode); | 
|  | 482 | err = nilfs_segctor_feed_segment(sci); | 
|  | 483 | if (err) | 
|  | 484 | return err; | 
|  | 485 | goto retry; | 
|  | 486 | } | 
|  | 487 | if (unlikely(required)) { | 
|  | 488 | err = nilfs_segbuf_extend_segsum(segbuf); | 
|  | 489 | if (unlikely(err)) | 
|  | 490 | goto failed; | 
|  | 491 | } | 
|  | 492 | if (sci->sc_blk_cnt == 0) | 
|  | 493 | nilfs_segctor_begin_finfo(sci, inode); | 
|  | 494 |  | 
|  | 495 | nilfs_segctor_map_segsum_entry(sci, &sci->sc_binfo_ptr, binfo_size); | 
|  | 496 | /* Substitution to vblocknr is delayed until update_blocknr() */ | 
|  | 497 | nilfs_segbuf_add_file_buffer(segbuf, bh); | 
|  | 498 | sci->sc_blk_cnt++; | 
|  | 499 | failed: | 
|  | 500 | return err; | 
|  | 501 | } | 
|  | 502 |  | 
|  | 503 | /* | 
|  | 504 | * Callback functions that enumerate, mark, and collect dirty blocks | 
|  | 505 | */ | 
|  | 506 | static int nilfs_collect_file_data(struct nilfs_sc_info *sci, | 
|  | 507 | struct buffer_head *bh, struct inode *inode) | 
|  | 508 | { | 
|  | 509 | int err; | 
|  | 510 |  | 
|  | 511 | err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh); | 
|  | 512 | if (err < 0) | 
|  | 513 | return err; | 
|  | 514 |  | 
|  | 515 | err = nilfs_segctor_add_file_block(sci, bh, inode, | 
|  | 516 | sizeof(struct nilfs_binfo_v)); | 
|  | 517 | if (!err) | 
|  | 518 | sci->sc_datablk_cnt++; | 
|  | 519 | return err; | 
|  | 520 | } | 
|  | 521 |  | 
|  | 522 | static int nilfs_collect_file_node(struct nilfs_sc_info *sci, | 
|  | 523 | struct buffer_head *bh, | 
|  | 524 | struct inode *inode) | 
|  | 525 | { | 
|  | 526 | return nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh); | 
|  | 527 | } | 
|  | 528 |  | 
|  | 529 | static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci, | 
|  | 530 | struct buffer_head *bh, | 
|  | 531 | struct inode *inode) | 
|  | 532 | { | 
|  | 533 | WARN_ON(!buffer_dirty(bh)); | 
|  | 534 | return nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64)); | 
|  | 535 | } | 
|  | 536 |  | 
|  | 537 | static void nilfs_write_file_data_binfo(struct nilfs_sc_info *sci, | 
|  | 538 | struct nilfs_segsum_pointer *ssp, | 
|  | 539 | union nilfs_binfo *binfo) | 
|  | 540 | { | 
|  | 541 | struct nilfs_binfo_v *binfo_v = nilfs_segctor_map_segsum_entry( | 
|  | 542 | sci, ssp, sizeof(*binfo_v)); | 
|  | 543 | *binfo_v = binfo->bi_v; | 
|  | 544 | } | 
|  | 545 |  | 
|  | 546 | static void nilfs_write_file_node_binfo(struct nilfs_sc_info *sci, | 
|  | 547 | struct nilfs_segsum_pointer *ssp, | 
|  | 548 | union nilfs_binfo *binfo) | 
|  | 549 | { | 
|  | 550 | __le64 *vblocknr = nilfs_segctor_map_segsum_entry( | 
|  | 551 | sci, ssp, sizeof(*vblocknr)); | 
|  | 552 | *vblocknr = binfo->bi_v.bi_vblocknr; | 
|  | 553 | } | 
|  | 554 |  | 
|  | 555 | static struct nilfs_sc_operations nilfs_sc_file_ops = { | 
|  | 556 | .collect_data = nilfs_collect_file_data, | 
|  | 557 | .collect_node = nilfs_collect_file_node, | 
|  | 558 | .collect_bmap = nilfs_collect_file_bmap, | 
|  | 559 | .write_data_binfo = nilfs_write_file_data_binfo, | 
|  | 560 | .write_node_binfo = nilfs_write_file_node_binfo, | 
|  | 561 | }; | 
|  | 562 |  | 
|  | 563 | static int nilfs_collect_dat_data(struct nilfs_sc_info *sci, | 
|  | 564 | struct buffer_head *bh, struct inode *inode) | 
|  | 565 | { | 
|  | 566 | int err; | 
|  | 567 |  | 
|  | 568 | err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh); | 
|  | 569 | if (err < 0) | 
|  | 570 | return err; | 
|  | 571 |  | 
|  | 572 | err = nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64)); | 
|  | 573 | if (!err) | 
|  | 574 | sci->sc_datablk_cnt++; | 
|  | 575 | return err; | 
|  | 576 | } | 
|  | 577 |  | 
|  | 578 | static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci, | 
|  | 579 | struct buffer_head *bh, struct inode *inode) | 
|  | 580 | { | 
|  | 581 | WARN_ON(!buffer_dirty(bh)); | 
|  | 582 | return nilfs_segctor_add_file_block(sci, bh, inode, | 
|  | 583 | sizeof(struct nilfs_binfo_dat)); | 
|  | 584 | } | 
|  | 585 |  | 
|  | 586 | static void nilfs_write_dat_data_binfo(struct nilfs_sc_info *sci, | 
|  | 587 | struct nilfs_segsum_pointer *ssp, | 
|  | 588 | union nilfs_binfo *binfo) | 
|  | 589 | { | 
|  | 590 | __le64 *blkoff = nilfs_segctor_map_segsum_entry(sci, ssp, | 
|  | 591 | sizeof(*blkoff)); | 
|  | 592 | *blkoff = binfo->bi_dat.bi_blkoff; | 
|  | 593 | } | 
|  | 594 |  | 
|  | 595 | static void nilfs_write_dat_node_binfo(struct nilfs_sc_info *sci, | 
|  | 596 | struct nilfs_segsum_pointer *ssp, | 
|  | 597 | union nilfs_binfo *binfo) | 
|  | 598 | { | 
|  | 599 | struct nilfs_binfo_dat *binfo_dat = | 
|  | 600 | nilfs_segctor_map_segsum_entry(sci, ssp, sizeof(*binfo_dat)); | 
|  | 601 | *binfo_dat = binfo->bi_dat; | 
|  | 602 | } | 
|  | 603 |  | 
|  | 604 | static struct nilfs_sc_operations nilfs_sc_dat_ops = { | 
|  | 605 | .collect_data = nilfs_collect_dat_data, | 
|  | 606 | .collect_node = nilfs_collect_file_node, | 
|  | 607 | .collect_bmap = nilfs_collect_dat_bmap, | 
|  | 608 | .write_data_binfo = nilfs_write_dat_data_binfo, | 
|  | 609 | .write_node_binfo = nilfs_write_dat_node_binfo, | 
|  | 610 | }; | 
|  | 611 |  | 
|  | 612 | static struct nilfs_sc_operations nilfs_sc_dsync_ops = { | 
|  | 613 | .collect_data = nilfs_collect_file_data, | 
|  | 614 | .collect_node = NULL, | 
|  | 615 | .collect_bmap = NULL, | 
|  | 616 | .write_data_binfo = nilfs_write_file_data_binfo, | 
|  | 617 | .write_node_binfo = NULL, | 
|  | 618 | }; | 
|  | 619 |  | 
|  | 620 | static size_t nilfs_lookup_dirty_data_buffers(struct inode *inode, | 
|  | 621 | struct list_head *listp, | 
|  | 622 | size_t nlimit, | 
|  | 623 | loff_t start, loff_t end) | 
|  | 624 | { | 
|  | 625 | struct address_space *mapping = inode->i_mapping; | 
|  | 626 | struct pagevec pvec; | 
|  | 627 | pgoff_t index = 0, last = ULONG_MAX; | 
|  | 628 | size_t ndirties = 0; | 
|  | 629 | int i; | 
|  | 630 |  | 
|  | 631 | if (unlikely(start != 0 || end != LLONG_MAX)) { | 
|  | 632 | /* | 
|  | 633 | * A valid range is given for sync-ing data pages. The | 
|  | 634 | * range is rounded to per-page; extra dirty buffers | 
|  | 635 | * may be included if blocksize < pagesize. | 
|  | 636 | */ | 
|  | 637 | index = start >> PAGE_SHIFT; | 
|  | 638 | last = end >> PAGE_SHIFT; | 
|  | 639 | } | 
|  | 640 | pagevec_init(&pvec, 0); | 
|  | 641 | repeat: | 
|  | 642 | if (unlikely(index > last) || | 
|  | 643 | !pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY, | 
|  | 644 | min_t(pgoff_t, last - index, | 
|  | 645 | PAGEVEC_SIZE - 1) + 1)) | 
|  | 646 | return ndirties; | 
|  | 647 |  | 
|  | 648 | for (i = 0; i < pagevec_count(&pvec); i++) { | 
|  | 649 | struct buffer_head *bh, *head; | 
|  | 650 | struct page *page = pvec.pages[i]; | 
|  | 651 |  | 
|  | 652 | if (unlikely(page->index > last)) | 
|  | 653 | break; | 
|  | 654 |  | 
|  | 655 | lock_page(page); | 
|  | 656 | if (!page_has_buffers(page)) | 
|  | 657 | create_empty_buffers(page, 1 << inode->i_blkbits, 0); | 
|  | 658 | unlock_page(page); | 
|  | 659 |  | 
|  | 660 | bh = head = page_buffers(page); | 
|  | 661 | do { | 
|  | 662 | if (!buffer_dirty(bh) || buffer_async_write(bh)) | 
|  | 663 | continue; | 
|  | 664 | get_bh(bh); | 
|  | 665 | list_add_tail(&bh->b_assoc_buffers, listp); | 
|  | 666 | ndirties++; | 
|  | 667 | if (unlikely(ndirties >= nlimit)) { | 
|  | 668 | pagevec_release(&pvec); | 
|  | 669 | cond_resched(); | 
|  | 670 | return ndirties; | 
|  | 671 | } | 
|  | 672 | } while (bh = bh->b_this_page, bh != head); | 
|  | 673 | } | 
|  | 674 | pagevec_release(&pvec); | 
|  | 675 | cond_resched(); | 
|  | 676 | goto repeat; | 
|  | 677 | } | 
|  | 678 |  | 
|  | 679 | static void nilfs_lookup_dirty_node_buffers(struct inode *inode, | 
|  | 680 | struct list_head *listp) | 
|  | 681 | { | 
|  | 682 | struct nilfs_inode_info *ii = NILFS_I(inode); | 
|  | 683 | struct address_space *mapping = &ii->i_btnode_cache; | 
|  | 684 | struct pagevec pvec; | 
|  | 685 | struct buffer_head *bh, *head; | 
|  | 686 | unsigned int i; | 
|  | 687 | pgoff_t index = 0; | 
|  | 688 |  | 
|  | 689 | pagevec_init(&pvec, 0); | 
|  | 690 |  | 
|  | 691 | while (pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY, | 
|  | 692 | PAGEVEC_SIZE)) { | 
|  | 693 | for (i = 0; i < pagevec_count(&pvec); i++) { | 
|  | 694 | bh = head = page_buffers(pvec.pages[i]); | 
|  | 695 | do { | 
|  | 696 | if (buffer_dirty(bh) && | 
|  | 697 | !buffer_async_write(bh)) { | 
|  | 698 | get_bh(bh); | 
|  | 699 | list_add_tail(&bh->b_assoc_buffers, | 
|  | 700 | listp); | 
|  | 701 | } | 
|  | 702 | bh = bh->b_this_page; | 
|  | 703 | } while (bh != head); | 
|  | 704 | } | 
|  | 705 | pagevec_release(&pvec); | 
|  | 706 | cond_resched(); | 
|  | 707 | } | 
|  | 708 | } | 
|  | 709 |  | 
|  | 710 | static void nilfs_dispose_list(struct the_nilfs *nilfs, | 
|  | 711 | struct list_head *head, int force) | 
|  | 712 | { | 
|  | 713 | struct nilfs_inode_info *ii, *n; | 
|  | 714 | struct nilfs_inode_info *ivec[SC_N_INODEVEC], **pii; | 
|  | 715 | unsigned nv = 0; | 
|  | 716 |  | 
|  | 717 | while (!list_empty(head)) { | 
|  | 718 | spin_lock(&nilfs->ns_inode_lock); | 
|  | 719 | list_for_each_entry_safe(ii, n, head, i_dirty) { | 
|  | 720 | list_del_init(&ii->i_dirty); | 
|  | 721 | if (force) { | 
|  | 722 | if (unlikely(ii->i_bh)) { | 
|  | 723 | brelse(ii->i_bh); | 
|  | 724 | ii->i_bh = NULL; | 
|  | 725 | } | 
|  | 726 | } else if (test_bit(NILFS_I_DIRTY, &ii->i_state)) { | 
|  | 727 | set_bit(NILFS_I_QUEUED, &ii->i_state); | 
|  | 728 | list_add_tail(&ii->i_dirty, | 
|  | 729 | &nilfs->ns_dirty_files); | 
|  | 730 | continue; | 
|  | 731 | } | 
|  | 732 | ivec[nv++] = ii; | 
|  | 733 | if (nv == SC_N_INODEVEC) | 
|  | 734 | break; | 
|  | 735 | } | 
|  | 736 | spin_unlock(&nilfs->ns_inode_lock); | 
|  | 737 |  | 
|  | 738 | for (pii = ivec; nv > 0; pii++, nv--) | 
|  | 739 | iput(&(*pii)->vfs_inode); | 
|  | 740 | } | 
|  | 741 | } | 
|  | 742 |  | 
|  | 743 | static void nilfs_iput_work_func(struct work_struct *work) | 
|  | 744 | { | 
|  | 745 | struct nilfs_sc_info *sci = container_of(work, struct nilfs_sc_info, | 
|  | 746 | sc_iput_work); | 
|  | 747 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; | 
|  | 748 |  | 
|  | 749 | nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 0); | 
|  | 750 | } | 
|  | 751 |  | 
|  | 752 | static int nilfs_test_metadata_dirty(struct the_nilfs *nilfs, | 
|  | 753 | struct nilfs_root *root) | 
|  | 754 | { | 
|  | 755 | int ret = 0; | 
|  | 756 |  | 
|  | 757 | if (nilfs_mdt_fetch_dirty(root->ifile)) | 
|  | 758 | ret++; | 
|  | 759 | if (nilfs_mdt_fetch_dirty(nilfs->ns_cpfile)) | 
|  | 760 | ret++; | 
|  | 761 | if (nilfs_mdt_fetch_dirty(nilfs->ns_sufile)) | 
|  | 762 | ret++; | 
|  | 763 | if ((ret || nilfs_doing_gc()) && nilfs_mdt_fetch_dirty(nilfs->ns_dat)) | 
|  | 764 | ret++; | 
|  | 765 | return ret; | 
|  | 766 | } | 
|  | 767 |  | 
|  | 768 | static int nilfs_segctor_clean(struct nilfs_sc_info *sci) | 
|  | 769 | { | 
|  | 770 | return list_empty(&sci->sc_dirty_files) && | 
|  | 771 | !test_bit(NILFS_SC_DIRTY, &sci->sc_flags) && | 
|  | 772 | sci->sc_nfreesegs == 0 && | 
|  | 773 | (!nilfs_doing_gc() || list_empty(&sci->sc_gc_inodes)); | 
|  | 774 | } | 
|  | 775 |  | 
|  | 776 | static int nilfs_segctor_confirm(struct nilfs_sc_info *sci) | 
|  | 777 | { | 
|  | 778 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; | 
|  | 779 | int ret = 0; | 
|  | 780 |  | 
|  | 781 | if (nilfs_test_metadata_dirty(nilfs, sci->sc_root)) | 
|  | 782 | set_bit(NILFS_SC_DIRTY, &sci->sc_flags); | 
|  | 783 |  | 
|  | 784 | spin_lock(&nilfs->ns_inode_lock); | 
|  | 785 | if (list_empty(&nilfs->ns_dirty_files) && nilfs_segctor_clean(sci)) | 
|  | 786 | ret++; | 
|  | 787 |  | 
|  | 788 | spin_unlock(&nilfs->ns_inode_lock); | 
|  | 789 | return ret; | 
|  | 790 | } | 
|  | 791 |  | 
|  | 792 | static void nilfs_segctor_clear_metadata_dirty(struct nilfs_sc_info *sci) | 
|  | 793 | { | 
|  | 794 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; | 
|  | 795 |  | 
|  | 796 | nilfs_mdt_clear_dirty(sci->sc_root->ifile); | 
|  | 797 | nilfs_mdt_clear_dirty(nilfs->ns_cpfile); | 
|  | 798 | nilfs_mdt_clear_dirty(nilfs->ns_sufile); | 
|  | 799 | nilfs_mdt_clear_dirty(nilfs->ns_dat); | 
|  | 800 | } | 
|  | 801 |  | 
|  | 802 | static int nilfs_segctor_create_checkpoint(struct nilfs_sc_info *sci) | 
|  | 803 | { | 
|  | 804 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; | 
|  | 805 | struct buffer_head *bh_cp; | 
|  | 806 | struct nilfs_checkpoint *raw_cp; | 
|  | 807 | int err; | 
|  | 808 |  | 
|  | 809 | /* XXX: this interface will be changed */ | 
|  | 810 | err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 1, | 
|  | 811 | &raw_cp, &bh_cp); | 
|  | 812 | if (likely(!err)) { | 
|  | 813 | /* The following code is duplicated with cpfile.  But, it is | 
|  | 814 | needed to collect the checkpoint even if it was not newly | 
|  | 815 | created */ | 
|  | 816 | mark_buffer_dirty(bh_cp); | 
|  | 817 | nilfs_mdt_mark_dirty(nilfs->ns_cpfile); | 
|  | 818 | nilfs_cpfile_put_checkpoint( | 
|  | 819 | nilfs->ns_cpfile, nilfs->ns_cno, bh_cp); | 
|  | 820 | } else | 
|  | 821 | WARN_ON(err == -EINVAL || err == -ENOENT); | 
|  | 822 |  | 
|  | 823 | return err; | 
|  | 824 | } | 
|  | 825 |  | 
|  | 826 | static int nilfs_segctor_fill_in_checkpoint(struct nilfs_sc_info *sci) | 
|  | 827 | { | 
|  | 828 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; | 
|  | 829 | struct buffer_head *bh_cp; | 
|  | 830 | struct nilfs_checkpoint *raw_cp; | 
|  | 831 | int err; | 
|  | 832 |  | 
|  | 833 | err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 0, | 
|  | 834 | &raw_cp, &bh_cp); | 
|  | 835 | if (unlikely(err)) { | 
|  | 836 | WARN_ON(err == -EINVAL || err == -ENOENT); | 
|  | 837 | goto failed_ibh; | 
|  | 838 | } | 
|  | 839 | raw_cp->cp_snapshot_list.ssl_next = 0; | 
|  | 840 | raw_cp->cp_snapshot_list.ssl_prev = 0; | 
|  | 841 | raw_cp->cp_inodes_count = | 
|  | 842 | cpu_to_le64(atomic_read(&sci->sc_root->inodes_count)); | 
|  | 843 | raw_cp->cp_blocks_count = | 
|  | 844 | cpu_to_le64(atomic_read(&sci->sc_root->blocks_count)); | 
|  | 845 | raw_cp->cp_nblk_inc = | 
|  | 846 | cpu_to_le64(sci->sc_nblk_inc + sci->sc_nblk_this_inc); | 
|  | 847 | raw_cp->cp_create = cpu_to_le64(sci->sc_seg_ctime); | 
|  | 848 | raw_cp->cp_cno = cpu_to_le64(nilfs->ns_cno); | 
|  | 849 |  | 
|  | 850 | if (test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags)) | 
|  | 851 | nilfs_checkpoint_clear_minor(raw_cp); | 
|  | 852 | else | 
|  | 853 | nilfs_checkpoint_set_minor(raw_cp); | 
|  | 854 |  | 
|  | 855 | nilfs_write_inode_common(sci->sc_root->ifile, | 
|  | 856 | &raw_cp->cp_ifile_inode, 1); | 
|  | 857 | nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, bh_cp); | 
|  | 858 | return 0; | 
|  | 859 |  | 
|  | 860 | failed_ibh: | 
|  | 861 | return err; | 
|  | 862 | } | 
|  | 863 |  | 
|  | 864 | static void nilfs_fill_in_file_bmap(struct inode *ifile, | 
|  | 865 | struct nilfs_inode_info *ii) | 
|  | 866 |  | 
|  | 867 | { | 
|  | 868 | struct buffer_head *ibh; | 
|  | 869 | struct nilfs_inode *raw_inode; | 
|  | 870 |  | 
|  | 871 | if (test_bit(NILFS_I_BMAP, &ii->i_state)) { | 
|  | 872 | ibh = ii->i_bh; | 
|  | 873 | BUG_ON(!ibh); | 
|  | 874 | raw_inode = nilfs_ifile_map_inode(ifile, ii->vfs_inode.i_ino, | 
|  | 875 | ibh); | 
|  | 876 | nilfs_bmap_write(ii->i_bmap, raw_inode); | 
|  | 877 | nilfs_ifile_unmap_inode(ifile, ii->vfs_inode.i_ino, ibh); | 
|  | 878 | } | 
|  | 879 | } | 
|  | 880 |  | 
|  | 881 | static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info *sci) | 
|  | 882 | { | 
|  | 883 | struct nilfs_inode_info *ii; | 
|  | 884 |  | 
|  | 885 | list_for_each_entry(ii, &sci->sc_dirty_files, i_dirty) { | 
|  | 886 | nilfs_fill_in_file_bmap(sci->sc_root->ifile, ii); | 
|  | 887 | set_bit(NILFS_I_COLLECTED, &ii->i_state); | 
|  | 888 | } | 
|  | 889 | } | 
|  | 890 |  | 
|  | 891 | static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info *sci, | 
|  | 892 | struct the_nilfs *nilfs) | 
|  | 893 | { | 
|  | 894 | struct buffer_head *bh_sr; | 
|  | 895 | struct nilfs_super_root *raw_sr; | 
|  | 896 | unsigned isz, srsz; | 
|  | 897 |  | 
|  | 898 | bh_sr = NILFS_LAST_SEGBUF(&sci->sc_segbufs)->sb_super_root; | 
|  | 899 | raw_sr = (struct nilfs_super_root *)bh_sr->b_data; | 
|  | 900 | isz = nilfs->ns_inode_size; | 
|  | 901 | srsz = NILFS_SR_BYTES(isz); | 
|  | 902 |  | 
|  | 903 | raw_sr->sr_bytes = cpu_to_le16(srsz); | 
|  | 904 | raw_sr->sr_nongc_ctime | 
|  | 905 | = cpu_to_le64(nilfs_doing_gc() ? | 
|  | 906 | nilfs->ns_nongc_ctime : sci->sc_seg_ctime); | 
|  | 907 | raw_sr->sr_flags = 0; | 
|  | 908 |  | 
|  | 909 | nilfs_write_inode_common(nilfs->ns_dat, (void *)raw_sr + | 
|  | 910 | NILFS_SR_DAT_OFFSET(isz), 1); | 
|  | 911 | nilfs_write_inode_common(nilfs->ns_cpfile, (void *)raw_sr + | 
|  | 912 | NILFS_SR_CPFILE_OFFSET(isz), 1); | 
|  | 913 | nilfs_write_inode_common(nilfs->ns_sufile, (void *)raw_sr + | 
|  | 914 | NILFS_SR_SUFILE_OFFSET(isz), 1); | 
|  | 915 | memset((void *)raw_sr + srsz, 0, nilfs->ns_blocksize - srsz); | 
|  | 916 | } | 
|  | 917 |  | 
|  | 918 | static void nilfs_redirty_inodes(struct list_head *head) | 
|  | 919 | { | 
|  | 920 | struct nilfs_inode_info *ii; | 
|  | 921 |  | 
|  | 922 | list_for_each_entry(ii, head, i_dirty) { | 
|  | 923 | if (test_bit(NILFS_I_COLLECTED, &ii->i_state)) | 
|  | 924 | clear_bit(NILFS_I_COLLECTED, &ii->i_state); | 
|  | 925 | } | 
|  | 926 | } | 
|  | 927 |  | 
|  | 928 | static void nilfs_drop_collected_inodes(struct list_head *head) | 
|  | 929 | { | 
|  | 930 | struct nilfs_inode_info *ii; | 
|  | 931 |  | 
|  | 932 | list_for_each_entry(ii, head, i_dirty) { | 
|  | 933 | if (!test_and_clear_bit(NILFS_I_COLLECTED, &ii->i_state)) | 
|  | 934 | continue; | 
|  | 935 |  | 
|  | 936 | clear_bit(NILFS_I_INODE_DIRTY, &ii->i_state); | 
|  | 937 | set_bit(NILFS_I_UPDATED, &ii->i_state); | 
|  | 938 | } | 
|  | 939 | } | 
|  | 940 |  | 
|  | 941 | static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci, | 
|  | 942 | struct inode *inode, | 
|  | 943 | struct list_head *listp, | 
|  | 944 | int (*collect)(struct nilfs_sc_info *, | 
|  | 945 | struct buffer_head *, | 
|  | 946 | struct inode *)) | 
|  | 947 | { | 
|  | 948 | struct buffer_head *bh, *n; | 
|  | 949 | int err = 0; | 
|  | 950 |  | 
|  | 951 | if (collect) { | 
|  | 952 | list_for_each_entry_safe(bh, n, listp, b_assoc_buffers) { | 
|  | 953 | list_del_init(&bh->b_assoc_buffers); | 
|  | 954 | err = collect(sci, bh, inode); | 
|  | 955 | brelse(bh); | 
|  | 956 | if (unlikely(err)) | 
|  | 957 | goto dispose_buffers; | 
|  | 958 | } | 
|  | 959 | return 0; | 
|  | 960 | } | 
|  | 961 |  | 
|  | 962 | dispose_buffers: | 
|  | 963 | while (!list_empty(listp)) { | 
|  | 964 | bh = list_first_entry(listp, struct buffer_head, | 
|  | 965 | b_assoc_buffers); | 
|  | 966 | list_del_init(&bh->b_assoc_buffers); | 
|  | 967 | brelse(bh); | 
|  | 968 | } | 
|  | 969 | return err; | 
|  | 970 | } | 
|  | 971 |  | 
|  | 972 | static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info *sci) | 
|  | 973 | { | 
|  | 974 | /* Remaining number of blocks within segment buffer */ | 
|  | 975 | return sci->sc_segbuf_nblocks - | 
|  | 976 | (sci->sc_nblk_this_inc + sci->sc_curseg->sb_sum.nblocks); | 
|  | 977 | } | 
|  | 978 |  | 
|  | 979 | static int nilfs_segctor_scan_file(struct nilfs_sc_info *sci, | 
|  | 980 | struct inode *inode, | 
|  | 981 | struct nilfs_sc_operations *sc_ops) | 
|  | 982 | { | 
|  | 983 | LIST_HEAD(data_buffers); | 
|  | 984 | LIST_HEAD(node_buffers); | 
|  | 985 | int err; | 
|  | 986 |  | 
|  | 987 | if (!(sci->sc_stage.flags & NILFS_CF_NODE)) { | 
|  | 988 | size_t n, rest = nilfs_segctor_buffer_rest(sci); | 
|  | 989 |  | 
|  | 990 | n = nilfs_lookup_dirty_data_buffers( | 
|  | 991 | inode, &data_buffers, rest + 1, 0, LLONG_MAX); | 
|  | 992 | if (n > rest) { | 
|  | 993 | err = nilfs_segctor_apply_buffers( | 
|  | 994 | sci, inode, &data_buffers, | 
|  | 995 | sc_ops->collect_data); | 
|  | 996 | BUG_ON(!err); /* always receive -E2BIG or true error */ | 
|  | 997 | goto break_or_fail; | 
|  | 998 | } | 
|  | 999 | } | 
|  | 1000 | nilfs_lookup_dirty_node_buffers(inode, &node_buffers); | 
|  | 1001 |  | 
|  | 1002 | if (!(sci->sc_stage.flags & NILFS_CF_NODE)) { | 
|  | 1003 | err = nilfs_segctor_apply_buffers( | 
|  | 1004 | sci, inode, &data_buffers, sc_ops->collect_data); | 
|  | 1005 | if (unlikely(err)) { | 
|  | 1006 | /* dispose node list */ | 
|  | 1007 | nilfs_segctor_apply_buffers( | 
|  | 1008 | sci, inode, &node_buffers, NULL); | 
|  | 1009 | goto break_or_fail; | 
|  | 1010 | } | 
|  | 1011 | sci->sc_stage.flags |= NILFS_CF_NODE; | 
|  | 1012 | } | 
|  | 1013 | /* Collect node */ | 
|  | 1014 | err = nilfs_segctor_apply_buffers( | 
|  | 1015 | sci, inode, &node_buffers, sc_ops->collect_node); | 
|  | 1016 | if (unlikely(err)) | 
|  | 1017 | goto break_or_fail; | 
|  | 1018 |  | 
|  | 1019 | nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode)->i_bmap, &node_buffers); | 
|  | 1020 | err = nilfs_segctor_apply_buffers( | 
|  | 1021 | sci, inode, &node_buffers, sc_ops->collect_bmap); | 
|  | 1022 | if (unlikely(err)) | 
|  | 1023 | goto break_or_fail; | 
|  | 1024 |  | 
|  | 1025 | nilfs_segctor_end_finfo(sci, inode); | 
|  | 1026 | sci->sc_stage.flags &= ~NILFS_CF_NODE; | 
|  | 1027 |  | 
|  | 1028 | break_or_fail: | 
|  | 1029 | return err; | 
|  | 1030 | } | 
|  | 1031 |  | 
|  | 1032 | static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci, | 
|  | 1033 | struct inode *inode) | 
|  | 1034 | { | 
|  | 1035 | LIST_HEAD(data_buffers); | 
|  | 1036 | size_t n, rest = nilfs_segctor_buffer_rest(sci); | 
|  | 1037 | int err; | 
|  | 1038 |  | 
|  | 1039 | n = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, rest + 1, | 
|  | 1040 | sci->sc_dsync_start, | 
|  | 1041 | sci->sc_dsync_end); | 
|  | 1042 |  | 
|  | 1043 | err = nilfs_segctor_apply_buffers(sci, inode, &data_buffers, | 
|  | 1044 | nilfs_collect_file_data); | 
|  | 1045 | if (!err) { | 
|  | 1046 | nilfs_segctor_end_finfo(sci, inode); | 
|  | 1047 | BUG_ON(n > rest); | 
|  | 1048 | /* always receive -E2BIG or true error if n > rest */ | 
|  | 1049 | } | 
|  | 1050 | return err; | 
|  | 1051 | } | 
|  | 1052 |  | 
|  | 1053 | static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode) | 
|  | 1054 | { | 
|  | 1055 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; | 
|  | 1056 | struct list_head *head; | 
|  | 1057 | struct nilfs_inode_info *ii; | 
|  | 1058 | size_t ndone; | 
|  | 1059 | int err = 0; | 
|  | 1060 |  | 
|  | 1061 | switch (sci->sc_stage.scnt) { | 
|  | 1062 | case NILFS_ST_INIT: | 
|  | 1063 | /* Pre-processes */ | 
|  | 1064 | sci->sc_stage.flags = 0; | 
|  | 1065 |  | 
|  | 1066 | if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) { | 
|  | 1067 | sci->sc_nblk_inc = 0; | 
|  | 1068 | sci->sc_curseg->sb_sum.flags = NILFS_SS_LOGBGN; | 
|  | 1069 | if (mode == SC_LSEG_DSYNC) { | 
|  | 1070 | sci->sc_stage.scnt = NILFS_ST_DSYNC; | 
|  | 1071 | goto dsync_mode; | 
|  | 1072 | } | 
|  | 1073 | } | 
|  | 1074 |  | 
|  | 1075 | sci->sc_stage.dirty_file_ptr = NULL; | 
|  | 1076 | sci->sc_stage.gc_inode_ptr = NULL; | 
|  | 1077 | if (mode == SC_FLUSH_DAT) { | 
|  | 1078 | sci->sc_stage.scnt = NILFS_ST_DAT; | 
|  | 1079 | goto dat_stage; | 
|  | 1080 | } | 
|  | 1081 | sci->sc_stage.scnt++;  /* Fall through */ | 
|  | 1082 | case NILFS_ST_GC: | 
|  | 1083 | if (nilfs_doing_gc()) { | 
|  | 1084 | head = &sci->sc_gc_inodes; | 
|  | 1085 | ii = list_prepare_entry(sci->sc_stage.gc_inode_ptr, | 
|  | 1086 | head, i_dirty); | 
|  | 1087 | list_for_each_entry_continue(ii, head, i_dirty) { | 
|  | 1088 | err = nilfs_segctor_scan_file( | 
|  | 1089 | sci, &ii->vfs_inode, | 
|  | 1090 | &nilfs_sc_file_ops); | 
|  | 1091 | if (unlikely(err)) { | 
|  | 1092 | sci->sc_stage.gc_inode_ptr = list_entry( | 
|  | 1093 | ii->i_dirty.prev, | 
|  | 1094 | struct nilfs_inode_info, | 
|  | 1095 | i_dirty); | 
|  | 1096 | goto break_or_fail; | 
|  | 1097 | } | 
|  | 1098 | set_bit(NILFS_I_COLLECTED, &ii->i_state); | 
|  | 1099 | } | 
|  | 1100 | sci->sc_stage.gc_inode_ptr = NULL; | 
|  | 1101 | } | 
|  | 1102 | sci->sc_stage.scnt++;  /* Fall through */ | 
|  | 1103 | case NILFS_ST_FILE: | 
|  | 1104 | head = &sci->sc_dirty_files; | 
|  | 1105 | ii = list_prepare_entry(sci->sc_stage.dirty_file_ptr, head, | 
|  | 1106 | i_dirty); | 
|  | 1107 | list_for_each_entry_continue(ii, head, i_dirty) { | 
|  | 1108 | clear_bit(NILFS_I_DIRTY, &ii->i_state); | 
|  | 1109 |  | 
|  | 1110 | err = nilfs_segctor_scan_file(sci, &ii->vfs_inode, | 
|  | 1111 | &nilfs_sc_file_ops); | 
|  | 1112 | if (unlikely(err)) { | 
|  | 1113 | sci->sc_stage.dirty_file_ptr = | 
|  | 1114 | list_entry(ii->i_dirty.prev, | 
|  | 1115 | struct nilfs_inode_info, | 
|  | 1116 | i_dirty); | 
|  | 1117 | goto break_or_fail; | 
|  | 1118 | } | 
|  | 1119 | /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */ | 
|  | 1120 | /* XXX: required ? */ | 
|  | 1121 | } | 
|  | 1122 | sci->sc_stage.dirty_file_ptr = NULL; | 
|  | 1123 | if (mode == SC_FLUSH_FILE) { | 
|  | 1124 | sci->sc_stage.scnt = NILFS_ST_DONE; | 
|  | 1125 | return 0; | 
|  | 1126 | } | 
|  | 1127 | sci->sc_stage.scnt++; | 
|  | 1128 | sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED; | 
|  | 1129 | /* Fall through */ | 
|  | 1130 | case NILFS_ST_IFILE: | 
|  | 1131 | err = nilfs_segctor_scan_file(sci, sci->sc_root->ifile, | 
|  | 1132 | &nilfs_sc_file_ops); | 
|  | 1133 | if (unlikely(err)) | 
|  | 1134 | break; | 
|  | 1135 | sci->sc_stage.scnt++; | 
|  | 1136 | /* Creating a checkpoint */ | 
|  | 1137 | err = nilfs_segctor_create_checkpoint(sci); | 
|  | 1138 | if (unlikely(err)) | 
|  | 1139 | break; | 
|  | 1140 | /* Fall through */ | 
|  | 1141 | case NILFS_ST_CPFILE: | 
|  | 1142 | err = nilfs_segctor_scan_file(sci, nilfs->ns_cpfile, | 
|  | 1143 | &nilfs_sc_file_ops); | 
|  | 1144 | if (unlikely(err)) | 
|  | 1145 | break; | 
|  | 1146 | sci->sc_stage.scnt++;  /* Fall through */ | 
|  | 1147 | case NILFS_ST_SUFILE: | 
|  | 1148 | err = nilfs_sufile_freev(nilfs->ns_sufile, sci->sc_freesegs, | 
|  | 1149 | sci->sc_nfreesegs, &ndone); | 
|  | 1150 | if (unlikely(err)) { | 
|  | 1151 | nilfs_sufile_cancel_freev(nilfs->ns_sufile, | 
|  | 1152 | sci->sc_freesegs, ndone, | 
|  | 1153 | NULL); | 
|  | 1154 | break; | 
|  | 1155 | } | 
|  | 1156 | sci->sc_stage.flags |= NILFS_CF_SUFREED; | 
|  | 1157 |  | 
|  | 1158 | err = nilfs_segctor_scan_file(sci, nilfs->ns_sufile, | 
|  | 1159 | &nilfs_sc_file_ops); | 
|  | 1160 | if (unlikely(err)) | 
|  | 1161 | break; | 
|  | 1162 | sci->sc_stage.scnt++;  /* Fall through */ | 
|  | 1163 | case NILFS_ST_DAT: | 
|  | 1164 | dat_stage: | 
|  | 1165 | err = nilfs_segctor_scan_file(sci, nilfs->ns_dat, | 
|  | 1166 | &nilfs_sc_dat_ops); | 
|  | 1167 | if (unlikely(err)) | 
|  | 1168 | break; | 
|  | 1169 | if (mode == SC_FLUSH_DAT) { | 
|  | 1170 | sci->sc_stage.scnt = NILFS_ST_DONE; | 
|  | 1171 | return 0; | 
|  | 1172 | } | 
|  | 1173 | sci->sc_stage.scnt++;  /* Fall through */ | 
|  | 1174 | case NILFS_ST_SR: | 
|  | 1175 | if (mode == SC_LSEG_SR) { | 
|  | 1176 | /* Appending a super root */ | 
|  | 1177 | err = nilfs_segctor_add_super_root(sci); | 
|  | 1178 | if (unlikely(err)) | 
|  | 1179 | break; | 
|  | 1180 | } | 
|  | 1181 | /* End of a logical segment */ | 
|  | 1182 | sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND; | 
|  | 1183 | sci->sc_stage.scnt = NILFS_ST_DONE; | 
|  | 1184 | return 0; | 
|  | 1185 | case NILFS_ST_DSYNC: | 
|  | 1186 | dsync_mode: | 
|  | 1187 | sci->sc_curseg->sb_sum.flags |= NILFS_SS_SYNDT; | 
|  | 1188 | ii = sci->sc_dsync_inode; | 
|  | 1189 | if (!test_bit(NILFS_I_BUSY, &ii->i_state)) | 
|  | 1190 | break; | 
|  | 1191 |  | 
|  | 1192 | err = nilfs_segctor_scan_file_dsync(sci, &ii->vfs_inode); | 
|  | 1193 | if (unlikely(err)) | 
|  | 1194 | break; | 
|  | 1195 | sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND; | 
|  | 1196 | sci->sc_stage.scnt = NILFS_ST_DONE; | 
|  | 1197 | return 0; | 
|  | 1198 | case NILFS_ST_DONE: | 
|  | 1199 | return 0; | 
|  | 1200 | default: | 
|  | 1201 | BUG(); | 
|  | 1202 | } | 
|  | 1203 |  | 
|  | 1204 | break_or_fail: | 
|  | 1205 | return err; | 
|  | 1206 | } | 
|  | 1207 |  | 
|  | 1208 | /** | 
|  | 1209 | * nilfs_segctor_begin_construction - setup segment buffer to make a new log | 
|  | 1210 | * @sci: nilfs_sc_info | 
|  | 1211 | * @nilfs: nilfs object | 
|  | 1212 | */ | 
|  | 1213 | static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci, | 
|  | 1214 | struct the_nilfs *nilfs) | 
|  | 1215 | { | 
|  | 1216 | struct nilfs_segment_buffer *segbuf, *prev; | 
|  | 1217 | __u64 nextnum; | 
|  | 1218 | int err, alloc = 0; | 
|  | 1219 |  | 
|  | 1220 | segbuf = nilfs_segbuf_new(sci->sc_super); | 
|  | 1221 | if (unlikely(!segbuf)) | 
|  | 1222 | return -ENOMEM; | 
|  | 1223 |  | 
|  | 1224 | if (list_empty(&sci->sc_write_logs)) { | 
|  | 1225 | nilfs_segbuf_map(segbuf, nilfs->ns_segnum, | 
|  | 1226 | nilfs->ns_pseg_offset, nilfs); | 
|  | 1227 | if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) { | 
|  | 1228 | nilfs_shift_to_next_segment(nilfs); | 
|  | 1229 | nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs); | 
|  | 1230 | } | 
|  | 1231 |  | 
|  | 1232 | segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq; | 
|  | 1233 | nextnum = nilfs->ns_nextnum; | 
|  | 1234 |  | 
|  | 1235 | if (nilfs->ns_segnum == nilfs->ns_nextnum) | 
|  | 1236 | /* Start from the head of a new full segment */ | 
|  | 1237 | alloc++; | 
|  | 1238 | } else { | 
|  | 1239 | /* Continue logs */ | 
|  | 1240 | prev = NILFS_LAST_SEGBUF(&sci->sc_write_logs); | 
|  | 1241 | nilfs_segbuf_map_cont(segbuf, prev); | 
|  | 1242 | segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq; | 
|  | 1243 | nextnum = prev->sb_nextnum; | 
|  | 1244 |  | 
|  | 1245 | if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) { | 
|  | 1246 | nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs); | 
|  | 1247 | segbuf->sb_sum.seg_seq++; | 
|  | 1248 | alloc++; | 
|  | 1249 | } | 
|  | 1250 | } | 
|  | 1251 |  | 
|  | 1252 | err = nilfs_sufile_mark_dirty(nilfs->ns_sufile, segbuf->sb_segnum); | 
|  | 1253 | if (err) | 
|  | 1254 | goto failed; | 
|  | 1255 |  | 
|  | 1256 | if (alloc) { | 
|  | 1257 | err = nilfs_sufile_alloc(nilfs->ns_sufile, &nextnum); | 
|  | 1258 | if (err) | 
|  | 1259 | goto failed; | 
|  | 1260 | } | 
|  | 1261 | nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs); | 
|  | 1262 |  | 
|  | 1263 | BUG_ON(!list_empty(&sci->sc_segbufs)); | 
|  | 1264 | list_add_tail(&segbuf->sb_list, &sci->sc_segbufs); | 
|  | 1265 | sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks; | 
|  | 1266 | return 0; | 
|  | 1267 |  | 
|  | 1268 | failed: | 
|  | 1269 | nilfs_segbuf_free(segbuf); | 
|  | 1270 | return err; | 
|  | 1271 | } | 
|  | 1272 |  | 
|  | 1273 | static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci, | 
|  | 1274 | struct the_nilfs *nilfs, int nadd) | 
|  | 1275 | { | 
|  | 1276 | struct nilfs_segment_buffer *segbuf, *prev; | 
|  | 1277 | struct inode *sufile = nilfs->ns_sufile; | 
|  | 1278 | __u64 nextnextnum; | 
|  | 1279 | LIST_HEAD(list); | 
|  | 1280 | int err, ret, i; | 
|  | 1281 |  | 
|  | 1282 | prev = NILFS_LAST_SEGBUF(&sci->sc_segbufs); | 
|  | 1283 | /* | 
|  | 1284 | * Since the segment specified with nextnum might be allocated during | 
|  | 1285 | * the previous construction, the buffer including its segusage may | 
|  | 1286 | * not be dirty.  The following call ensures that the buffer is dirty | 
|  | 1287 | * and will pin the buffer on memory until the sufile is written. | 
|  | 1288 | */ | 
|  | 1289 | err = nilfs_sufile_mark_dirty(sufile, prev->sb_nextnum); | 
|  | 1290 | if (unlikely(err)) | 
|  | 1291 | return err; | 
|  | 1292 |  | 
|  | 1293 | for (i = 0; i < nadd; i++) { | 
|  | 1294 | /* extend segment info */ | 
|  | 1295 | err = -ENOMEM; | 
|  | 1296 | segbuf = nilfs_segbuf_new(sci->sc_super); | 
|  | 1297 | if (unlikely(!segbuf)) | 
|  | 1298 | goto failed; | 
|  | 1299 |  | 
|  | 1300 | /* map this buffer to region of segment on-disk */ | 
|  | 1301 | nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs); | 
|  | 1302 | sci->sc_segbuf_nblocks += segbuf->sb_rest_blocks; | 
|  | 1303 |  | 
|  | 1304 | /* allocate the next next full segment */ | 
|  | 1305 | err = nilfs_sufile_alloc(sufile, &nextnextnum); | 
|  | 1306 | if (unlikely(err)) | 
|  | 1307 | goto failed_segbuf; | 
|  | 1308 |  | 
|  | 1309 | segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq + 1; | 
|  | 1310 | nilfs_segbuf_set_next_segnum(segbuf, nextnextnum, nilfs); | 
|  | 1311 |  | 
|  | 1312 | list_add_tail(&segbuf->sb_list, &list); | 
|  | 1313 | prev = segbuf; | 
|  | 1314 | } | 
|  | 1315 | list_splice_tail(&list, &sci->sc_segbufs); | 
|  | 1316 | return 0; | 
|  | 1317 |  | 
|  | 1318 | failed_segbuf: | 
|  | 1319 | nilfs_segbuf_free(segbuf); | 
|  | 1320 | failed: | 
|  | 1321 | list_for_each_entry(segbuf, &list, sb_list) { | 
|  | 1322 | ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum); | 
|  | 1323 | WARN_ON(ret); /* never fails */ | 
|  | 1324 | } | 
|  | 1325 | nilfs_destroy_logs(&list); | 
|  | 1326 | return err; | 
|  | 1327 | } | 
|  | 1328 |  | 
|  | 1329 | static void nilfs_free_incomplete_logs(struct list_head *logs, | 
|  | 1330 | struct the_nilfs *nilfs) | 
|  | 1331 | { | 
|  | 1332 | struct nilfs_segment_buffer *segbuf, *prev; | 
|  | 1333 | struct inode *sufile = nilfs->ns_sufile; | 
|  | 1334 | int ret; | 
|  | 1335 |  | 
|  | 1336 | segbuf = NILFS_FIRST_SEGBUF(logs); | 
|  | 1337 | if (nilfs->ns_nextnum != segbuf->sb_nextnum) { | 
|  | 1338 | ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum); | 
|  | 1339 | WARN_ON(ret); /* never fails */ | 
|  | 1340 | } | 
|  | 1341 | if (atomic_read(&segbuf->sb_err)) { | 
|  | 1342 | /* Case 1: The first segment failed */ | 
|  | 1343 | if (segbuf->sb_pseg_start != segbuf->sb_fseg_start) | 
|  | 1344 | /* Case 1a:  Partial segment appended into an existing | 
|  | 1345 | segment */ | 
|  | 1346 | nilfs_terminate_segment(nilfs, segbuf->sb_fseg_start, | 
|  | 1347 | segbuf->sb_fseg_end); | 
|  | 1348 | else /* Case 1b:  New full segment */ | 
|  | 1349 | set_nilfs_discontinued(nilfs); | 
|  | 1350 | } | 
|  | 1351 |  | 
|  | 1352 | prev = segbuf; | 
|  | 1353 | list_for_each_entry_continue(segbuf, logs, sb_list) { | 
|  | 1354 | if (prev->sb_nextnum != segbuf->sb_nextnum) { | 
|  | 1355 | ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum); | 
|  | 1356 | WARN_ON(ret); /* never fails */ | 
|  | 1357 | } | 
|  | 1358 | if (atomic_read(&segbuf->sb_err) && | 
|  | 1359 | segbuf->sb_segnum != nilfs->ns_nextnum) | 
|  | 1360 | /* Case 2: extended segment (!= next) failed */ | 
|  | 1361 | nilfs_sufile_set_error(sufile, segbuf->sb_segnum); | 
|  | 1362 | prev = segbuf; | 
|  | 1363 | } | 
|  | 1364 | } | 
|  | 1365 |  | 
|  | 1366 | static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci, | 
|  | 1367 | struct inode *sufile) | 
|  | 1368 | { | 
|  | 1369 | struct nilfs_segment_buffer *segbuf; | 
|  | 1370 | unsigned long live_blocks; | 
|  | 1371 | int ret; | 
|  | 1372 |  | 
|  | 1373 | list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) { | 
|  | 1374 | live_blocks = segbuf->sb_sum.nblocks + | 
|  | 1375 | (segbuf->sb_pseg_start - segbuf->sb_fseg_start); | 
|  | 1376 | ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum, | 
|  | 1377 | live_blocks, | 
|  | 1378 | sci->sc_seg_ctime); | 
|  | 1379 | WARN_ON(ret); /* always succeed because the segusage is dirty */ | 
|  | 1380 | } | 
|  | 1381 | } | 
|  | 1382 |  | 
|  | 1383 | static void nilfs_cancel_segusage(struct list_head *logs, struct inode *sufile) | 
|  | 1384 | { | 
|  | 1385 | struct nilfs_segment_buffer *segbuf; | 
|  | 1386 | int ret; | 
|  | 1387 |  | 
|  | 1388 | segbuf = NILFS_FIRST_SEGBUF(logs); | 
|  | 1389 | ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum, | 
|  | 1390 | segbuf->sb_pseg_start - | 
|  | 1391 | segbuf->sb_fseg_start, 0); | 
|  | 1392 | WARN_ON(ret); /* always succeed because the segusage is dirty */ | 
|  | 1393 |  | 
|  | 1394 | list_for_each_entry_continue(segbuf, logs, sb_list) { | 
|  | 1395 | ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum, | 
|  | 1396 | 0, 0); | 
|  | 1397 | WARN_ON(ret); /* always succeed */ | 
|  | 1398 | } | 
|  | 1399 | } | 
|  | 1400 |  | 
|  | 1401 | static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci, | 
|  | 1402 | struct nilfs_segment_buffer *last, | 
|  | 1403 | struct inode *sufile) | 
|  | 1404 | { | 
|  | 1405 | struct nilfs_segment_buffer *segbuf = last; | 
|  | 1406 | int ret; | 
|  | 1407 |  | 
|  | 1408 | list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) { | 
|  | 1409 | sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks; | 
|  | 1410 | ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum); | 
|  | 1411 | WARN_ON(ret); | 
|  | 1412 | } | 
|  | 1413 | nilfs_truncate_logs(&sci->sc_segbufs, last); | 
|  | 1414 | } | 
|  | 1415 |  | 
|  | 1416 |  | 
|  | 1417 | static int nilfs_segctor_collect(struct nilfs_sc_info *sci, | 
|  | 1418 | struct the_nilfs *nilfs, int mode) | 
|  | 1419 | { | 
|  | 1420 | struct nilfs_cstage prev_stage = sci->sc_stage; | 
|  | 1421 | int err, nadd = 1; | 
|  | 1422 |  | 
|  | 1423 | /* Collection retry loop */ | 
|  | 1424 | for (;;) { | 
|  | 1425 | sci->sc_nblk_this_inc = 0; | 
|  | 1426 | sci->sc_curseg = NILFS_FIRST_SEGBUF(&sci->sc_segbufs); | 
|  | 1427 |  | 
|  | 1428 | err = nilfs_segctor_reset_segment_buffer(sci); | 
|  | 1429 | if (unlikely(err)) | 
|  | 1430 | goto failed; | 
|  | 1431 |  | 
|  | 1432 | err = nilfs_segctor_collect_blocks(sci, mode); | 
|  | 1433 | sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks; | 
|  | 1434 | if (!err) | 
|  | 1435 | break; | 
|  | 1436 |  | 
|  | 1437 | if (unlikely(err != -E2BIG)) | 
|  | 1438 | goto failed; | 
|  | 1439 |  | 
|  | 1440 | /* The current segment is filled up */ | 
|  | 1441 | if (mode != SC_LSEG_SR || sci->sc_stage.scnt < NILFS_ST_CPFILE) | 
|  | 1442 | break; | 
|  | 1443 |  | 
|  | 1444 | nilfs_clear_logs(&sci->sc_segbufs); | 
|  | 1445 |  | 
|  | 1446 | if (sci->sc_stage.flags & NILFS_CF_SUFREED) { | 
|  | 1447 | err = nilfs_sufile_cancel_freev(nilfs->ns_sufile, | 
|  | 1448 | sci->sc_freesegs, | 
|  | 1449 | sci->sc_nfreesegs, | 
|  | 1450 | NULL); | 
|  | 1451 | WARN_ON(err); /* do not happen */ | 
|  | 1452 | sci->sc_stage.flags &= ~NILFS_CF_SUFREED; | 
|  | 1453 | } | 
|  | 1454 |  | 
|  | 1455 | err = nilfs_segctor_extend_segments(sci, nilfs, nadd); | 
|  | 1456 | if (unlikely(err)) | 
|  | 1457 | return err; | 
|  | 1458 |  | 
|  | 1459 | nadd = min_t(int, nadd << 1, SC_MAX_SEGDELTA); | 
|  | 1460 | sci->sc_stage = prev_stage; | 
|  | 1461 | } | 
|  | 1462 | nilfs_segctor_truncate_segments(sci, sci->sc_curseg, nilfs->ns_sufile); | 
|  | 1463 | return 0; | 
|  | 1464 |  | 
|  | 1465 | failed: | 
|  | 1466 | return err; | 
|  | 1467 | } | 
|  | 1468 |  | 
|  | 1469 | static void nilfs_list_replace_buffer(struct buffer_head *old_bh, | 
|  | 1470 | struct buffer_head *new_bh) | 
|  | 1471 | { | 
|  | 1472 | BUG_ON(!list_empty(&new_bh->b_assoc_buffers)); | 
|  | 1473 |  | 
|  | 1474 | list_replace_init(&old_bh->b_assoc_buffers, &new_bh->b_assoc_buffers); | 
|  | 1475 | /* The caller must release old_bh */ | 
|  | 1476 | } | 
|  | 1477 |  | 
|  | 1478 | static int | 
|  | 1479 | nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info *sci, | 
|  | 1480 | struct nilfs_segment_buffer *segbuf, | 
|  | 1481 | int mode) | 
|  | 1482 | { | 
|  | 1483 | struct inode *inode = NULL; | 
|  | 1484 | sector_t blocknr; | 
|  | 1485 | unsigned long nfinfo = segbuf->sb_sum.nfinfo; | 
|  | 1486 | unsigned long nblocks = 0, ndatablk = 0; | 
|  | 1487 | struct nilfs_sc_operations *sc_op = NULL; | 
|  | 1488 | struct nilfs_segsum_pointer ssp; | 
|  | 1489 | struct nilfs_finfo *finfo = NULL; | 
|  | 1490 | union nilfs_binfo binfo; | 
|  | 1491 | struct buffer_head *bh, *bh_org; | 
|  | 1492 | ino_t ino = 0; | 
|  | 1493 | int err = 0; | 
|  | 1494 |  | 
|  | 1495 | if (!nfinfo) | 
|  | 1496 | goto out; | 
|  | 1497 |  | 
|  | 1498 | blocknr = segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk; | 
|  | 1499 | ssp.bh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers); | 
|  | 1500 | ssp.offset = sizeof(struct nilfs_segment_summary); | 
|  | 1501 |  | 
|  | 1502 | list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) { | 
|  | 1503 | if (bh == segbuf->sb_super_root) | 
|  | 1504 | break; | 
|  | 1505 | if (!finfo) { | 
|  | 1506 | finfo =	nilfs_segctor_map_segsum_entry( | 
|  | 1507 | sci, &ssp, sizeof(*finfo)); | 
|  | 1508 | ino = le64_to_cpu(finfo->fi_ino); | 
|  | 1509 | nblocks = le32_to_cpu(finfo->fi_nblocks); | 
|  | 1510 | ndatablk = le32_to_cpu(finfo->fi_ndatablk); | 
|  | 1511 |  | 
|  | 1512 | inode = bh->b_page->mapping->host; | 
|  | 1513 |  | 
|  | 1514 | if (mode == SC_LSEG_DSYNC) | 
|  | 1515 | sc_op = &nilfs_sc_dsync_ops; | 
|  | 1516 | else if (ino == NILFS_DAT_INO) | 
|  | 1517 | sc_op = &nilfs_sc_dat_ops; | 
|  | 1518 | else /* file blocks */ | 
|  | 1519 | sc_op = &nilfs_sc_file_ops; | 
|  | 1520 | } | 
|  | 1521 | bh_org = bh; | 
|  | 1522 | get_bh(bh_org); | 
|  | 1523 | err = nilfs_bmap_assign(NILFS_I(inode)->i_bmap, &bh, blocknr, | 
|  | 1524 | &binfo); | 
|  | 1525 | if (bh != bh_org) | 
|  | 1526 | nilfs_list_replace_buffer(bh_org, bh); | 
|  | 1527 | brelse(bh_org); | 
|  | 1528 | if (unlikely(err)) | 
|  | 1529 | goto failed_bmap; | 
|  | 1530 |  | 
|  | 1531 | if (ndatablk > 0) | 
|  | 1532 | sc_op->write_data_binfo(sci, &ssp, &binfo); | 
|  | 1533 | else | 
|  | 1534 | sc_op->write_node_binfo(sci, &ssp, &binfo); | 
|  | 1535 |  | 
|  | 1536 | blocknr++; | 
|  | 1537 | if (--nblocks == 0) { | 
|  | 1538 | finfo = NULL; | 
|  | 1539 | if (--nfinfo == 0) | 
|  | 1540 | break; | 
|  | 1541 | } else if (ndatablk > 0) | 
|  | 1542 | ndatablk--; | 
|  | 1543 | } | 
|  | 1544 | out: | 
|  | 1545 | return 0; | 
|  | 1546 |  | 
|  | 1547 | failed_bmap: | 
|  | 1548 | return err; | 
|  | 1549 | } | 
|  | 1550 |  | 
|  | 1551 | static int nilfs_segctor_assign(struct nilfs_sc_info *sci, int mode) | 
|  | 1552 | { | 
|  | 1553 | struct nilfs_segment_buffer *segbuf; | 
|  | 1554 | int err; | 
|  | 1555 |  | 
|  | 1556 | list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) { | 
|  | 1557 | err = nilfs_segctor_update_payload_blocknr(sci, segbuf, mode); | 
|  | 1558 | if (unlikely(err)) | 
|  | 1559 | return err; | 
|  | 1560 | nilfs_segbuf_fill_in_segsum(segbuf); | 
|  | 1561 | } | 
|  | 1562 | return 0; | 
|  | 1563 | } | 
|  | 1564 |  | 
|  | 1565 | static void nilfs_begin_page_io(struct page *page) | 
|  | 1566 | { | 
|  | 1567 | if (!page || PageWriteback(page)) | 
|  | 1568 | /* For split b-tree node pages, this function may be called | 
|  | 1569 | twice.  We ignore the 2nd or later calls by this check. */ | 
|  | 1570 | return; | 
|  | 1571 |  | 
|  | 1572 | lock_page(page); | 
|  | 1573 | clear_page_dirty_for_io(page); | 
|  | 1574 | set_page_writeback(page); | 
|  | 1575 | unlock_page(page); | 
|  | 1576 | } | 
|  | 1577 |  | 
|  | 1578 | static void nilfs_segctor_prepare_write(struct nilfs_sc_info *sci) | 
|  | 1579 | { | 
|  | 1580 | struct nilfs_segment_buffer *segbuf; | 
|  | 1581 | struct page *bd_page = NULL, *fs_page = NULL; | 
|  | 1582 |  | 
|  | 1583 | list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) { | 
|  | 1584 | struct buffer_head *bh; | 
|  | 1585 |  | 
|  | 1586 | list_for_each_entry(bh, &segbuf->sb_segsum_buffers, | 
|  | 1587 | b_assoc_buffers) { | 
|  | 1588 | set_buffer_async_write(bh); | 
|  | 1589 | if (bh->b_page != bd_page) { | 
|  | 1590 | if (bd_page) { | 
|  | 1591 | lock_page(bd_page); | 
|  | 1592 | clear_page_dirty_for_io(bd_page); | 
|  | 1593 | set_page_writeback(bd_page); | 
|  | 1594 | unlock_page(bd_page); | 
|  | 1595 | } | 
|  | 1596 | bd_page = bh->b_page; | 
|  | 1597 | } | 
|  | 1598 | } | 
|  | 1599 |  | 
|  | 1600 | list_for_each_entry(bh, &segbuf->sb_payload_buffers, | 
|  | 1601 | b_assoc_buffers) { | 
|  | 1602 | set_buffer_async_write(bh); | 
|  | 1603 | if (bh == segbuf->sb_super_root) { | 
|  | 1604 | if (bh->b_page != bd_page) { | 
|  | 1605 | lock_page(bd_page); | 
|  | 1606 | clear_page_dirty_for_io(bd_page); | 
|  | 1607 | set_page_writeback(bd_page); | 
|  | 1608 | unlock_page(bd_page); | 
|  | 1609 | bd_page = bh->b_page; | 
|  | 1610 | } | 
|  | 1611 | break; | 
|  | 1612 | } | 
|  | 1613 | if (bh->b_page != fs_page) { | 
|  | 1614 | nilfs_begin_page_io(fs_page); | 
|  | 1615 | fs_page = bh->b_page; | 
|  | 1616 | } | 
|  | 1617 | } | 
|  | 1618 | } | 
|  | 1619 | if (bd_page) { | 
|  | 1620 | lock_page(bd_page); | 
|  | 1621 | clear_page_dirty_for_io(bd_page); | 
|  | 1622 | set_page_writeback(bd_page); | 
|  | 1623 | unlock_page(bd_page); | 
|  | 1624 | } | 
|  | 1625 | nilfs_begin_page_io(fs_page); | 
|  | 1626 | } | 
|  | 1627 |  | 
|  | 1628 | static int nilfs_segctor_write(struct nilfs_sc_info *sci, | 
|  | 1629 | struct the_nilfs *nilfs) | 
|  | 1630 | { | 
|  | 1631 | int ret; | 
|  | 1632 |  | 
|  | 1633 | ret = nilfs_write_logs(&sci->sc_segbufs, nilfs); | 
|  | 1634 | list_splice_tail_init(&sci->sc_segbufs, &sci->sc_write_logs); | 
|  | 1635 | return ret; | 
|  | 1636 | } | 
|  | 1637 |  | 
|  | 1638 | static void nilfs_end_page_io(struct page *page, int err) | 
|  | 1639 | { | 
|  | 1640 | if (!page) | 
|  | 1641 | return; | 
|  | 1642 |  | 
|  | 1643 | if (buffer_nilfs_node(page_buffers(page)) && !PageWriteback(page)) { | 
|  | 1644 | /* | 
|  | 1645 | * For b-tree node pages, this function may be called twice | 
|  | 1646 | * or more because they might be split in a segment. | 
|  | 1647 | */ | 
|  | 1648 | if (PageDirty(page)) { | 
|  | 1649 | /* | 
|  | 1650 | * For pages holding split b-tree node buffers, dirty | 
|  | 1651 | * flag on the buffers may be cleared discretely. | 
|  | 1652 | * In that case, the page is once redirtied for | 
|  | 1653 | * remaining buffers, and it must be cancelled if | 
|  | 1654 | * all the buffers get cleaned later. | 
|  | 1655 | */ | 
|  | 1656 | lock_page(page); | 
|  | 1657 | if (nilfs_page_buffers_clean(page)) | 
|  | 1658 | __nilfs_clear_page_dirty(page); | 
|  | 1659 | unlock_page(page); | 
|  | 1660 | } | 
|  | 1661 | return; | 
|  | 1662 | } | 
|  | 1663 |  | 
|  | 1664 | if (!err) { | 
|  | 1665 | if (!nilfs_page_buffers_clean(page)) | 
|  | 1666 | __set_page_dirty_nobuffers(page); | 
|  | 1667 | ClearPageError(page); | 
|  | 1668 | } else { | 
|  | 1669 | __set_page_dirty_nobuffers(page); | 
|  | 1670 | SetPageError(page); | 
|  | 1671 | } | 
|  | 1672 |  | 
|  | 1673 | end_page_writeback(page); | 
|  | 1674 | } | 
|  | 1675 |  | 
|  | 1676 | static void nilfs_abort_logs(struct list_head *logs, int err) | 
|  | 1677 | { | 
|  | 1678 | struct nilfs_segment_buffer *segbuf; | 
|  | 1679 | struct page *bd_page = NULL, *fs_page = NULL; | 
|  | 1680 | struct buffer_head *bh; | 
|  | 1681 |  | 
|  | 1682 | if (list_empty(logs)) | 
|  | 1683 | return; | 
|  | 1684 |  | 
|  | 1685 | list_for_each_entry(segbuf, logs, sb_list) { | 
|  | 1686 | list_for_each_entry(bh, &segbuf->sb_segsum_buffers, | 
|  | 1687 | b_assoc_buffers) { | 
|  | 1688 | clear_buffer_async_write(bh); | 
|  | 1689 | if (bh->b_page != bd_page) { | 
|  | 1690 | if (bd_page) | 
|  | 1691 | end_page_writeback(bd_page); | 
|  | 1692 | bd_page = bh->b_page; | 
|  | 1693 | } | 
|  | 1694 | } | 
|  | 1695 |  | 
|  | 1696 | list_for_each_entry(bh, &segbuf->sb_payload_buffers, | 
|  | 1697 | b_assoc_buffers) { | 
|  | 1698 | clear_buffer_async_write(bh); | 
|  | 1699 | if (bh == segbuf->sb_super_root) { | 
|  | 1700 | if (bh->b_page != bd_page) { | 
|  | 1701 | end_page_writeback(bd_page); | 
|  | 1702 | bd_page = bh->b_page; | 
|  | 1703 | } | 
|  | 1704 | break; | 
|  | 1705 | } | 
|  | 1706 | if (bh->b_page != fs_page) { | 
|  | 1707 | nilfs_end_page_io(fs_page, err); | 
|  | 1708 | fs_page = bh->b_page; | 
|  | 1709 | } | 
|  | 1710 | } | 
|  | 1711 | } | 
|  | 1712 | if (bd_page) | 
|  | 1713 | end_page_writeback(bd_page); | 
|  | 1714 |  | 
|  | 1715 | nilfs_end_page_io(fs_page, err); | 
|  | 1716 | } | 
|  | 1717 |  | 
|  | 1718 | static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci, | 
|  | 1719 | struct the_nilfs *nilfs, int err) | 
|  | 1720 | { | 
|  | 1721 | LIST_HEAD(logs); | 
|  | 1722 | int ret; | 
|  | 1723 |  | 
|  | 1724 | list_splice_tail_init(&sci->sc_write_logs, &logs); | 
|  | 1725 | ret = nilfs_wait_on_logs(&logs); | 
|  | 1726 | nilfs_abort_logs(&logs, ret ? : err); | 
|  | 1727 |  | 
|  | 1728 | list_splice_tail_init(&sci->sc_segbufs, &logs); | 
|  | 1729 | nilfs_cancel_segusage(&logs, nilfs->ns_sufile); | 
|  | 1730 | nilfs_free_incomplete_logs(&logs, nilfs); | 
|  | 1731 |  | 
|  | 1732 | if (sci->sc_stage.flags & NILFS_CF_SUFREED) { | 
|  | 1733 | ret = nilfs_sufile_cancel_freev(nilfs->ns_sufile, | 
|  | 1734 | sci->sc_freesegs, | 
|  | 1735 | sci->sc_nfreesegs, | 
|  | 1736 | NULL); | 
|  | 1737 | WARN_ON(ret); /* do not happen */ | 
|  | 1738 | } | 
|  | 1739 |  | 
|  | 1740 | nilfs_destroy_logs(&logs); | 
|  | 1741 | } | 
|  | 1742 |  | 
|  | 1743 | static void nilfs_set_next_segment(struct the_nilfs *nilfs, | 
|  | 1744 | struct nilfs_segment_buffer *segbuf) | 
|  | 1745 | { | 
|  | 1746 | nilfs->ns_segnum = segbuf->sb_segnum; | 
|  | 1747 | nilfs->ns_nextnum = segbuf->sb_nextnum; | 
|  | 1748 | nilfs->ns_pseg_offset = segbuf->sb_pseg_start - segbuf->sb_fseg_start | 
|  | 1749 | + segbuf->sb_sum.nblocks; | 
|  | 1750 | nilfs->ns_seg_seq = segbuf->sb_sum.seg_seq; | 
|  | 1751 | nilfs->ns_ctime = segbuf->sb_sum.ctime; | 
|  | 1752 | } | 
|  | 1753 |  | 
|  | 1754 | static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci) | 
|  | 1755 | { | 
|  | 1756 | struct nilfs_segment_buffer *segbuf; | 
|  | 1757 | struct page *bd_page = NULL, *fs_page = NULL; | 
|  | 1758 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; | 
|  | 1759 | int update_sr = false; | 
|  | 1760 |  | 
|  | 1761 | list_for_each_entry(segbuf, &sci->sc_write_logs, sb_list) { | 
|  | 1762 | struct buffer_head *bh; | 
|  | 1763 |  | 
|  | 1764 | list_for_each_entry(bh, &segbuf->sb_segsum_buffers, | 
|  | 1765 | b_assoc_buffers) { | 
|  | 1766 | set_buffer_uptodate(bh); | 
|  | 1767 | clear_buffer_dirty(bh); | 
|  | 1768 | clear_buffer_async_write(bh); | 
|  | 1769 | if (bh->b_page != bd_page) { | 
|  | 1770 | if (bd_page) | 
|  | 1771 | end_page_writeback(bd_page); | 
|  | 1772 | bd_page = bh->b_page; | 
|  | 1773 | } | 
|  | 1774 | } | 
|  | 1775 | /* | 
|  | 1776 | * We assume that the buffers which belong to the same page | 
|  | 1777 | * continue over the buffer list. | 
|  | 1778 | * Under this assumption, the last BHs of pages is | 
|  | 1779 | * identifiable by the discontinuity of bh->b_page | 
|  | 1780 | * (page != fs_page). | 
|  | 1781 | * | 
|  | 1782 | * For B-tree node blocks, however, this assumption is not | 
|  | 1783 | * guaranteed.  The cleanup code of B-tree node pages needs | 
|  | 1784 | * special care. | 
|  | 1785 | */ | 
|  | 1786 | list_for_each_entry(bh, &segbuf->sb_payload_buffers, | 
|  | 1787 | b_assoc_buffers) { | 
|  | 1788 | set_buffer_uptodate(bh); | 
|  | 1789 | clear_buffer_dirty(bh); | 
|  | 1790 | clear_buffer_async_write(bh); | 
|  | 1791 | clear_buffer_delay(bh); | 
|  | 1792 | clear_buffer_nilfs_volatile(bh); | 
|  | 1793 | clear_buffer_nilfs_redirected(bh); | 
|  | 1794 | if (bh == segbuf->sb_super_root) { | 
|  | 1795 | if (bh->b_page != bd_page) { | 
|  | 1796 | end_page_writeback(bd_page); | 
|  | 1797 | bd_page = bh->b_page; | 
|  | 1798 | } | 
|  | 1799 | update_sr = true; | 
|  | 1800 | break; | 
|  | 1801 | } | 
|  | 1802 | if (bh->b_page != fs_page) { | 
|  | 1803 | nilfs_end_page_io(fs_page, 0); | 
|  | 1804 | fs_page = bh->b_page; | 
|  | 1805 | } | 
|  | 1806 | } | 
|  | 1807 |  | 
|  | 1808 | if (!nilfs_segbuf_simplex(segbuf)) { | 
|  | 1809 | if (segbuf->sb_sum.flags & NILFS_SS_LOGBGN) { | 
|  | 1810 | set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags); | 
|  | 1811 | sci->sc_lseg_stime = jiffies; | 
|  | 1812 | } | 
|  | 1813 | if (segbuf->sb_sum.flags & NILFS_SS_LOGEND) | 
|  | 1814 | clear_bit(NILFS_SC_UNCLOSED, &sci->sc_flags); | 
|  | 1815 | } | 
|  | 1816 | } | 
|  | 1817 | /* | 
|  | 1818 | * Since pages may continue over multiple segment buffers, | 
|  | 1819 | * end of the last page must be checked outside of the loop. | 
|  | 1820 | */ | 
|  | 1821 | if (bd_page) | 
|  | 1822 | end_page_writeback(bd_page); | 
|  | 1823 |  | 
|  | 1824 | nilfs_end_page_io(fs_page, 0); | 
|  | 1825 |  | 
|  | 1826 | nilfs_drop_collected_inodes(&sci->sc_dirty_files); | 
|  | 1827 |  | 
|  | 1828 | if (nilfs_doing_gc()) | 
|  | 1829 | nilfs_drop_collected_inodes(&sci->sc_gc_inodes); | 
|  | 1830 | else | 
|  | 1831 | nilfs->ns_nongc_ctime = sci->sc_seg_ctime; | 
|  | 1832 |  | 
|  | 1833 | sci->sc_nblk_inc += sci->sc_nblk_this_inc; | 
|  | 1834 |  | 
|  | 1835 | segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs); | 
|  | 1836 | nilfs_set_next_segment(nilfs, segbuf); | 
|  | 1837 |  | 
|  | 1838 | if (update_sr) { | 
|  | 1839 | nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start, | 
|  | 1840 | segbuf->sb_sum.seg_seq, nilfs->ns_cno++); | 
|  | 1841 |  | 
|  | 1842 | clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags); | 
|  | 1843 | clear_bit(NILFS_SC_DIRTY, &sci->sc_flags); | 
|  | 1844 | set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags); | 
|  | 1845 | nilfs_segctor_clear_metadata_dirty(sci); | 
|  | 1846 | } else | 
|  | 1847 | clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags); | 
|  | 1848 | } | 
|  | 1849 |  | 
|  | 1850 | static int nilfs_segctor_wait(struct nilfs_sc_info *sci) | 
|  | 1851 | { | 
|  | 1852 | int ret; | 
|  | 1853 |  | 
|  | 1854 | ret = nilfs_wait_on_logs(&sci->sc_write_logs); | 
|  | 1855 | if (!ret) { | 
|  | 1856 | nilfs_segctor_complete_write(sci); | 
|  | 1857 | nilfs_destroy_logs(&sci->sc_write_logs); | 
|  | 1858 | } | 
|  | 1859 | return ret; | 
|  | 1860 | } | 
|  | 1861 |  | 
|  | 1862 | static int nilfs_segctor_collect_dirty_files(struct nilfs_sc_info *sci, | 
|  | 1863 | struct the_nilfs *nilfs) | 
|  | 1864 | { | 
|  | 1865 | struct nilfs_inode_info *ii, *n; | 
|  | 1866 | struct inode *ifile = sci->sc_root->ifile; | 
|  | 1867 |  | 
|  | 1868 | spin_lock(&nilfs->ns_inode_lock); | 
|  | 1869 | retry: | 
|  | 1870 | list_for_each_entry_safe(ii, n, &nilfs->ns_dirty_files, i_dirty) { | 
|  | 1871 | if (!ii->i_bh) { | 
|  | 1872 | struct buffer_head *ibh; | 
|  | 1873 | int err; | 
|  | 1874 |  | 
|  | 1875 | spin_unlock(&nilfs->ns_inode_lock); | 
|  | 1876 | err = nilfs_ifile_get_inode_block( | 
|  | 1877 | ifile, ii->vfs_inode.i_ino, &ibh); | 
|  | 1878 | if (unlikely(err)) { | 
|  | 1879 | nilfs_warning(sci->sc_super, __func__, | 
|  | 1880 | "failed to get inode block.\n"); | 
|  | 1881 | return err; | 
|  | 1882 | } | 
|  | 1883 | mark_buffer_dirty(ibh); | 
|  | 1884 | nilfs_mdt_mark_dirty(ifile); | 
|  | 1885 | spin_lock(&nilfs->ns_inode_lock); | 
|  | 1886 | if (likely(!ii->i_bh)) | 
|  | 1887 | ii->i_bh = ibh; | 
|  | 1888 | else | 
|  | 1889 | brelse(ibh); | 
|  | 1890 | goto retry; | 
|  | 1891 | } | 
|  | 1892 |  | 
|  | 1893 | clear_bit(NILFS_I_QUEUED, &ii->i_state); | 
|  | 1894 | set_bit(NILFS_I_BUSY, &ii->i_state); | 
|  | 1895 | list_move_tail(&ii->i_dirty, &sci->sc_dirty_files); | 
|  | 1896 | } | 
|  | 1897 | spin_unlock(&nilfs->ns_inode_lock); | 
|  | 1898 |  | 
|  | 1899 | return 0; | 
|  | 1900 | } | 
|  | 1901 |  | 
|  | 1902 | static void nilfs_segctor_drop_written_files(struct nilfs_sc_info *sci, | 
|  | 1903 | struct the_nilfs *nilfs) | 
|  | 1904 | { | 
|  | 1905 | struct nilfs_inode_info *ii, *n; | 
|  | 1906 | int during_mount = !(sci->sc_super->s_flags & MS_ACTIVE); | 
|  | 1907 | int defer_iput = false; | 
|  | 1908 |  | 
|  | 1909 | spin_lock(&nilfs->ns_inode_lock); | 
|  | 1910 | list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) { | 
|  | 1911 | if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) || | 
|  | 1912 | test_bit(NILFS_I_DIRTY, &ii->i_state)) | 
|  | 1913 | continue; | 
|  | 1914 |  | 
|  | 1915 | clear_bit(NILFS_I_BUSY, &ii->i_state); | 
|  | 1916 | brelse(ii->i_bh); | 
|  | 1917 | ii->i_bh = NULL; | 
|  | 1918 | list_del_init(&ii->i_dirty); | 
|  | 1919 | if (!ii->vfs_inode.i_nlink || during_mount) { | 
|  | 1920 | /* | 
|  | 1921 | * Defer calling iput() to avoid deadlocks if | 
|  | 1922 | * i_nlink == 0 or mount is not yet finished. | 
|  | 1923 | */ | 
|  | 1924 | list_add_tail(&ii->i_dirty, &sci->sc_iput_queue); | 
|  | 1925 | defer_iput = true; | 
|  | 1926 | } else { | 
|  | 1927 | spin_unlock(&nilfs->ns_inode_lock); | 
|  | 1928 | iput(&ii->vfs_inode); | 
|  | 1929 | spin_lock(&nilfs->ns_inode_lock); | 
|  | 1930 | } | 
|  | 1931 | } | 
|  | 1932 | spin_unlock(&nilfs->ns_inode_lock); | 
|  | 1933 |  | 
|  | 1934 | if (defer_iput) | 
|  | 1935 | schedule_work(&sci->sc_iput_work); | 
|  | 1936 | } | 
|  | 1937 |  | 
|  | 1938 | /* | 
|  | 1939 | * Main procedure of segment constructor | 
|  | 1940 | */ | 
|  | 1941 | static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode) | 
|  | 1942 | { | 
|  | 1943 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; | 
|  | 1944 | int err; | 
|  | 1945 |  | 
|  | 1946 | sci->sc_stage.scnt = NILFS_ST_INIT; | 
|  | 1947 | sci->sc_cno = nilfs->ns_cno; | 
|  | 1948 |  | 
|  | 1949 | err = nilfs_segctor_collect_dirty_files(sci, nilfs); | 
|  | 1950 | if (unlikely(err)) | 
|  | 1951 | goto out; | 
|  | 1952 |  | 
|  | 1953 | if (nilfs_test_metadata_dirty(nilfs, sci->sc_root)) | 
|  | 1954 | set_bit(NILFS_SC_DIRTY, &sci->sc_flags); | 
|  | 1955 |  | 
|  | 1956 | if (nilfs_segctor_clean(sci)) | 
|  | 1957 | goto out; | 
|  | 1958 |  | 
|  | 1959 | do { | 
|  | 1960 | sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK; | 
|  | 1961 |  | 
|  | 1962 | err = nilfs_segctor_begin_construction(sci, nilfs); | 
|  | 1963 | if (unlikely(err)) | 
|  | 1964 | goto out; | 
|  | 1965 |  | 
|  | 1966 | /* Update time stamp */ | 
|  | 1967 | sci->sc_seg_ctime = get_seconds(); | 
|  | 1968 |  | 
|  | 1969 | err = nilfs_segctor_collect(sci, nilfs, mode); | 
|  | 1970 | if (unlikely(err)) | 
|  | 1971 | goto failed; | 
|  | 1972 |  | 
|  | 1973 | /* Avoid empty segment */ | 
|  | 1974 | if (sci->sc_stage.scnt == NILFS_ST_DONE && | 
|  | 1975 | nilfs_segbuf_empty(sci->sc_curseg)) { | 
|  | 1976 | nilfs_segctor_abort_construction(sci, nilfs, 1); | 
|  | 1977 | goto out; | 
|  | 1978 | } | 
|  | 1979 |  | 
|  | 1980 | err = nilfs_segctor_assign(sci, mode); | 
|  | 1981 | if (unlikely(err)) | 
|  | 1982 | goto failed; | 
|  | 1983 |  | 
|  | 1984 | if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED) | 
|  | 1985 | nilfs_segctor_fill_in_file_bmap(sci); | 
|  | 1986 |  | 
|  | 1987 | if (mode == SC_LSEG_SR && | 
|  | 1988 | sci->sc_stage.scnt >= NILFS_ST_CPFILE) { | 
|  | 1989 | err = nilfs_segctor_fill_in_checkpoint(sci); | 
|  | 1990 | if (unlikely(err)) | 
|  | 1991 | goto failed_to_write; | 
|  | 1992 |  | 
|  | 1993 | nilfs_segctor_fill_in_super_root(sci, nilfs); | 
|  | 1994 | } | 
|  | 1995 | nilfs_segctor_update_segusage(sci, nilfs->ns_sufile); | 
|  | 1996 |  | 
|  | 1997 | /* Write partial segments */ | 
|  | 1998 | nilfs_segctor_prepare_write(sci); | 
|  | 1999 |  | 
|  | 2000 | nilfs_add_checksums_on_logs(&sci->sc_segbufs, | 
|  | 2001 | nilfs->ns_crc_seed); | 
|  | 2002 |  | 
|  | 2003 | err = nilfs_segctor_write(sci, nilfs); | 
|  | 2004 | if (unlikely(err)) | 
|  | 2005 | goto failed_to_write; | 
|  | 2006 |  | 
|  | 2007 | if (sci->sc_stage.scnt == NILFS_ST_DONE || | 
|  | 2008 | nilfs->ns_blocksize_bits != PAGE_CACHE_SHIFT) { | 
|  | 2009 | /* | 
|  | 2010 | * At this point, we avoid double buffering | 
|  | 2011 | * for blocksize < pagesize because page dirty | 
|  | 2012 | * flag is turned off during write and dirty | 
|  | 2013 | * buffers are not properly collected for | 
|  | 2014 | * pages crossing over segments. | 
|  | 2015 | */ | 
|  | 2016 | err = nilfs_segctor_wait(sci); | 
|  | 2017 | if (err) | 
|  | 2018 | goto failed_to_write; | 
|  | 2019 | } | 
|  | 2020 | } while (sci->sc_stage.scnt != NILFS_ST_DONE); | 
|  | 2021 |  | 
|  | 2022 | out: | 
|  | 2023 | nilfs_segctor_drop_written_files(sci, nilfs); | 
|  | 2024 | return err; | 
|  | 2025 |  | 
|  | 2026 | failed_to_write: | 
|  | 2027 | if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED) | 
|  | 2028 | nilfs_redirty_inodes(&sci->sc_dirty_files); | 
|  | 2029 |  | 
|  | 2030 | failed: | 
|  | 2031 | if (nilfs_doing_gc()) | 
|  | 2032 | nilfs_redirty_inodes(&sci->sc_gc_inodes); | 
|  | 2033 | nilfs_segctor_abort_construction(sci, nilfs, err); | 
|  | 2034 | goto out; | 
|  | 2035 | } | 
|  | 2036 |  | 
|  | 2037 | /** | 
|  | 2038 | * nilfs_segctor_start_timer - set timer of background write | 
|  | 2039 | * @sci: nilfs_sc_info | 
|  | 2040 | * | 
|  | 2041 | * If the timer has already been set, it ignores the new request. | 
|  | 2042 | * This function MUST be called within a section locking the segment | 
|  | 2043 | * semaphore. | 
|  | 2044 | */ | 
|  | 2045 | static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci) | 
|  | 2046 | { | 
|  | 2047 | spin_lock(&sci->sc_state_lock); | 
|  | 2048 | if (!(sci->sc_state & NILFS_SEGCTOR_COMMIT)) { | 
|  | 2049 | sci->sc_timer.expires = jiffies + sci->sc_interval; | 
|  | 2050 | add_timer(&sci->sc_timer); | 
|  | 2051 | sci->sc_state |= NILFS_SEGCTOR_COMMIT; | 
|  | 2052 | } | 
|  | 2053 | spin_unlock(&sci->sc_state_lock); | 
|  | 2054 | } | 
|  | 2055 |  | 
|  | 2056 | static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn) | 
|  | 2057 | { | 
|  | 2058 | spin_lock(&sci->sc_state_lock); | 
|  | 2059 | if (!(sci->sc_flush_request & (1 << bn))) { | 
|  | 2060 | unsigned long prev_req = sci->sc_flush_request; | 
|  | 2061 |  | 
|  | 2062 | sci->sc_flush_request |= (1 << bn); | 
|  | 2063 | if (!prev_req) | 
|  | 2064 | wake_up(&sci->sc_wait_daemon); | 
|  | 2065 | } | 
|  | 2066 | spin_unlock(&sci->sc_state_lock); | 
|  | 2067 | } | 
|  | 2068 |  | 
|  | 2069 | /** | 
|  | 2070 | * nilfs_flush_segment - trigger a segment construction for resource control | 
|  | 2071 | * @sb: super block | 
|  | 2072 | * @ino: inode number of the file to be flushed out. | 
|  | 2073 | */ | 
|  | 2074 | void nilfs_flush_segment(struct super_block *sb, ino_t ino) | 
|  | 2075 | { | 
|  | 2076 | struct the_nilfs *nilfs = sb->s_fs_info; | 
|  | 2077 | struct nilfs_sc_info *sci = nilfs->ns_writer; | 
|  | 2078 |  | 
|  | 2079 | if (!sci || nilfs_doing_construction()) | 
|  | 2080 | return; | 
|  | 2081 | nilfs_segctor_do_flush(sci, NILFS_MDT_INODE(sb, ino) ? ino : 0); | 
|  | 2082 | /* assign bit 0 to data files */ | 
|  | 2083 | } | 
|  | 2084 |  | 
|  | 2085 | struct nilfs_segctor_wait_request { | 
|  | 2086 | wait_queue_t	wq; | 
|  | 2087 | __u32		seq; | 
|  | 2088 | int		err; | 
|  | 2089 | atomic_t	done; | 
|  | 2090 | }; | 
|  | 2091 |  | 
|  | 2092 | static int nilfs_segctor_sync(struct nilfs_sc_info *sci) | 
|  | 2093 | { | 
|  | 2094 | struct nilfs_segctor_wait_request wait_req; | 
|  | 2095 | int err = 0; | 
|  | 2096 |  | 
|  | 2097 | spin_lock(&sci->sc_state_lock); | 
|  | 2098 | init_wait(&wait_req.wq); | 
|  | 2099 | wait_req.err = 0; | 
|  | 2100 | atomic_set(&wait_req.done, 0); | 
|  | 2101 | wait_req.seq = ++sci->sc_seq_request; | 
|  | 2102 | spin_unlock(&sci->sc_state_lock); | 
|  | 2103 |  | 
|  | 2104 | init_waitqueue_entry(&wait_req.wq, current); | 
|  | 2105 | add_wait_queue(&sci->sc_wait_request, &wait_req.wq); | 
|  | 2106 | set_current_state(TASK_INTERRUPTIBLE); | 
|  | 2107 | wake_up(&sci->sc_wait_daemon); | 
|  | 2108 |  | 
|  | 2109 | for (;;) { | 
|  | 2110 | if (atomic_read(&wait_req.done)) { | 
|  | 2111 | err = wait_req.err; | 
|  | 2112 | break; | 
|  | 2113 | } | 
|  | 2114 | if (!signal_pending(current)) { | 
|  | 2115 | schedule(); | 
|  | 2116 | continue; | 
|  | 2117 | } | 
|  | 2118 | err = -ERESTARTSYS; | 
|  | 2119 | break; | 
|  | 2120 | } | 
|  | 2121 | finish_wait(&sci->sc_wait_request, &wait_req.wq); | 
|  | 2122 | return err; | 
|  | 2123 | } | 
|  | 2124 |  | 
|  | 2125 | static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err) | 
|  | 2126 | { | 
|  | 2127 | struct nilfs_segctor_wait_request *wrq, *n; | 
|  | 2128 | unsigned long flags; | 
|  | 2129 |  | 
|  | 2130 | spin_lock_irqsave(&sci->sc_wait_request.lock, flags); | 
|  | 2131 | list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.task_list, | 
|  | 2132 | wq.task_list) { | 
|  | 2133 | if (!atomic_read(&wrq->done) && | 
|  | 2134 | nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq)) { | 
|  | 2135 | wrq->err = err; | 
|  | 2136 | atomic_set(&wrq->done, 1); | 
|  | 2137 | } | 
|  | 2138 | if (atomic_read(&wrq->done)) { | 
|  | 2139 | wrq->wq.func(&wrq->wq, | 
|  | 2140 | TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, | 
|  | 2141 | 0, NULL); | 
|  | 2142 | } | 
|  | 2143 | } | 
|  | 2144 | spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags); | 
|  | 2145 | } | 
|  | 2146 |  | 
|  | 2147 | /** | 
|  | 2148 | * nilfs_construct_segment - construct a logical segment | 
|  | 2149 | * @sb: super block | 
|  | 2150 | * | 
|  | 2151 | * Return Value: On success, 0 is retured. On errors, one of the following | 
|  | 2152 | * negative error code is returned. | 
|  | 2153 | * | 
|  | 2154 | * %-EROFS - Read only filesystem. | 
|  | 2155 | * | 
|  | 2156 | * %-EIO - I/O error | 
|  | 2157 | * | 
|  | 2158 | * %-ENOSPC - No space left on device (only in a panic state). | 
|  | 2159 | * | 
|  | 2160 | * %-ERESTARTSYS - Interrupted. | 
|  | 2161 | * | 
|  | 2162 | * %-ENOMEM - Insufficient memory available. | 
|  | 2163 | */ | 
|  | 2164 | int nilfs_construct_segment(struct super_block *sb) | 
|  | 2165 | { | 
|  | 2166 | struct the_nilfs *nilfs = sb->s_fs_info; | 
|  | 2167 | struct nilfs_sc_info *sci = nilfs->ns_writer; | 
|  | 2168 | struct nilfs_transaction_info *ti; | 
|  | 2169 | int err; | 
|  | 2170 |  | 
|  | 2171 | if (!sci) | 
|  | 2172 | return -EROFS; | 
|  | 2173 |  | 
|  | 2174 | /* A call inside transactions causes a deadlock. */ | 
|  | 2175 | BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC); | 
|  | 2176 |  | 
|  | 2177 | err = nilfs_segctor_sync(sci); | 
|  | 2178 | return err; | 
|  | 2179 | } | 
|  | 2180 |  | 
|  | 2181 | /** | 
|  | 2182 | * nilfs_construct_dsync_segment - construct a data-only logical segment | 
|  | 2183 | * @sb: super block | 
|  | 2184 | * @inode: inode whose data blocks should be written out | 
|  | 2185 | * @start: start byte offset | 
|  | 2186 | * @end: end byte offset (inclusive) | 
|  | 2187 | * | 
|  | 2188 | * Return Value: On success, 0 is retured. On errors, one of the following | 
|  | 2189 | * negative error code is returned. | 
|  | 2190 | * | 
|  | 2191 | * %-EROFS - Read only filesystem. | 
|  | 2192 | * | 
|  | 2193 | * %-EIO - I/O error | 
|  | 2194 | * | 
|  | 2195 | * %-ENOSPC - No space left on device (only in a panic state). | 
|  | 2196 | * | 
|  | 2197 | * %-ERESTARTSYS - Interrupted. | 
|  | 2198 | * | 
|  | 2199 | * %-ENOMEM - Insufficient memory available. | 
|  | 2200 | */ | 
|  | 2201 | int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode, | 
|  | 2202 | loff_t start, loff_t end) | 
|  | 2203 | { | 
|  | 2204 | struct the_nilfs *nilfs = sb->s_fs_info; | 
|  | 2205 | struct nilfs_sc_info *sci = nilfs->ns_writer; | 
|  | 2206 | struct nilfs_inode_info *ii; | 
|  | 2207 | struct nilfs_transaction_info ti; | 
|  | 2208 | int err = 0; | 
|  | 2209 |  | 
|  | 2210 | if (!sci) | 
|  | 2211 | return -EROFS; | 
|  | 2212 |  | 
|  | 2213 | nilfs_transaction_lock(sb, &ti, 0); | 
|  | 2214 |  | 
|  | 2215 | ii = NILFS_I(inode); | 
|  | 2216 | if (test_bit(NILFS_I_INODE_DIRTY, &ii->i_state) || | 
|  | 2217 | nilfs_test_opt(nilfs, STRICT_ORDER) || | 
|  | 2218 | test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) || | 
|  | 2219 | nilfs_discontinued(nilfs)) { | 
|  | 2220 | nilfs_transaction_unlock(sb); | 
|  | 2221 | err = nilfs_segctor_sync(sci); | 
|  | 2222 | return err; | 
|  | 2223 | } | 
|  | 2224 |  | 
|  | 2225 | spin_lock(&nilfs->ns_inode_lock); | 
|  | 2226 | if (!test_bit(NILFS_I_QUEUED, &ii->i_state) && | 
|  | 2227 | !test_bit(NILFS_I_BUSY, &ii->i_state)) { | 
|  | 2228 | spin_unlock(&nilfs->ns_inode_lock); | 
|  | 2229 | nilfs_transaction_unlock(sb); | 
|  | 2230 | return 0; | 
|  | 2231 | } | 
|  | 2232 | spin_unlock(&nilfs->ns_inode_lock); | 
|  | 2233 | sci->sc_dsync_inode = ii; | 
|  | 2234 | sci->sc_dsync_start = start; | 
|  | 2235 | sci->sc_dsync_end = end; | 
|  | 2236 |  | 
|  | 2237 | err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC); | 
|  | 2238 |  | 
|  | 2239 | nilfs_transaction_unlock(sb); | 
|  | 2240 | return err; | 
|  | 2241 | } | 
|  | 2242 |  | 
|  | 2243 | #define FLUSH_FILE_BIT	(0x1) /* data file only */ | 
|  | 2244 | #define FLUSH_DAT_BIT	(1 << NILFS_DAT_INO) /* DAT only */ | 
|  | 2245 |  | 
|  | 2246 | /** | 
|  | 2247 | * nilfs_segctor_accept - record accepted sequence count of log-write requests | 
|  | 2248 | * @sci: segment constructor object | 
|  | 2249 | */ | 
|  | 2250 | static void nilfs_segctor_accept(struct nilfs_sc_info *sci) | 
|  | 2251 | { | 
|  | 2252 | spin_lock(&sci->sc_state_lock); | 
|  | 2253 | sci->sc_seq_accepted = sci->sc_seq_request; | 
|  | 2254 | spin_unlock(&sci->sc_state_lock); | 
|  | 2255 | del_timer_sync(&sci->sc_timer); | 
|  | 2256 | } | 
|  | 2257 |  | 
|  | 2258 | /** | 
|  | 2259 | * nilfs_segctor_notify - notify the result of request to caller threads | 
|  | 2260 | * @sci: segment constructor object | 
|  | 2261 | * @mode: mode of log forming | 
|  | 2262 | * @err: error code to be notified | 
|  | 2263 | */ | 
|  | 2264 | static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err) | 
|  | 2265 | { | 
|  | 2266 | /* Clear requests (even when the construction failed) */ | 
|  | 2267 | spin_lock(&sci->sc_state_lock); | 
|  | 2268 |  | 
|  | 2269 | if (mode == SC_LSEG_SR) { | 
|  | 2270 | sci->sc_state &= ~NILFS_SEGCTOR_COMMIT; | 
|  | 2271 | sci->sc_seq_done = sci->sc_seq_accepted; | 
|  | 2272 | nilfs_segctor_wakeup(sci, err); | 
|  | 2273 | sci->sc_flush_request = 0; | 
|  | 2274 | } else { | 
|  | 2275 | if (mode == SC_FLUSH_FILE) | 
|  | 2276 | sci->sc_flush_request &= ~FLUSH_FILE_BIT; | 
|  | 2277 | else if (mode == SC_FLUSH_DAT) | 
|  | 2278 | sci->sc_flush_request &= ~FLUSH_DAT_BIT; | 
|  | 2279 |  | 
|  | 2280 | /* re-enable timer if checkpoint creation was not done */ | 
|  | 2281 | if ((sci->sc_state & NILFS_SEGCTOR_COMMIT) && | 
|  | 2282 | time_before(jiffies, sci->sc_timer.expires)) | 
|  | 2283 | add_timer(&sci->sc_timer); | 
|  | 2284 | } | 
|  | 2285 | spin_unlock(&sci->sc_state_lock); | 
|  | 2286 | } | 
|  | 2287 |  | 
|  | 2288 | /** | 
|  | 2289 | * nilfs_segctor_construct - form logs and write them to disk | 
|  | 2290 | * @sci: segment constructor object | 
|  | 2291 | * @mode: mode of log forming | 
|  | 2292 | */ | 
|  | 2293 | static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode) | 
|  | 2294 | { | 
|  | 2295 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; | 
|  | 2296 | struct nilfs_super_block **sbp; | 
|  | 2297 | int err = 0; | 
|  | 2298 |  | 
|  | 2299 | nilfs_segctor_accept(sci); | 
|  | 2300 |  | 
|  | 2301 | if (nilfs_discontinued(nilfs)) | 
|  | 2302 | mode = SC_LSEG_SR; | 
|  | 2303 | if (!nilfs_segctor_confirm(sci)) | 
|  | 2304 | err = nilfs_segctor_do_construct(sci, mode); | 
|  | 2305 |  | 
|  | 2306 | if (likely(!err)) { | 
|  | 2307 | if (mode != SC_FLUSH_DAT) | 
|  | 2308 | atomic_set(&nilfs->ns_ndirtyblks, 0); | 
|  | 2309 | if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) && | 
|  | 2310 | nilfs_discontinued(nilfs)) { | 
|  | 2311 | down_write(&nilfs->ns_sem); | 
|  | 2312 | err = -EIO; | 
|  | 2313 | sbp = nilfs_prepare_super(sci->sc_super, | 
|  | 2314 | nilfs_sb_will_flip(nilfs)); | 
|  | 2315 | if (likely(sbp)) { | 
|  | 2316 | nilfs_set_log_cursor(sbp[0], nilfs); | 
|  | 2317 | err = nilfs_commit_super(sci->sc_super, | 
|  | 2318 | NILFS_SB_COMMIT); | 
|  | 2319 | } | 
|  | 2320 | up_write(&nilfs->ns_sem); | 
|  | 2321 | } | 
|  | 2322 | } | 
|  | 2323 |  | 
|  | 2324 | nilfs_segctor_notify(sci, mode, err); | 
|  | 2325 | return err; | 
|  | 2326 | } | 
|  | 2327 |  | 
|  | 2328 | static void nilfs_construction_timeout(unsigned long data) | 
|  | 2329 | { | 
|  | 2330 | struct task_struct *p = (struct task_struct *)data; | 
|  | 2331 | wake_up_process(p); | 
|  | 2332 | } | 
|  | 2333 |  | 
|  | 2334 | static void | 
|  | 2335 | nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head) | 
|  | 2336 | { | 
|  | 2337 | struct nilfs_inode_info *ii, *n; | 
|  | 2338 |  | 
|  | 2339 | list_for_each_entry_safe(ii, n, head, i_dirty) { | 
|  | 2340 | if (!test_bit(NILFS_I_UPDATED, &ii->i_state)) | 
|  | 2341 | continue; | 
|  | 2342 | list_del_init(&ii->i_dirty); | 
|  | 2343 | truncate_inode_pages(&ii->vfs_inode.i_data, 0); | 
|  | 2344 | nilfs_btnode_cache_clear(&ii->i_btnode_cache); | 
|  | 2345 | iput(&ii->vfs_inode); | 
|  | 2346 | } | 
|  | 2347 | } | 
|  | 2348 |  | 
|  | 2349 | int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv, | 
|  | 2350 | void **kbufs) | 
|  | 2351 | { | 
|  | 2352 | struct the_nilfs *nilfs = sb->s_fs_info; | 
|  | 2353 | struct nilfs_sc_info *sci = nilfs->ns_writer; | 
|  | 2354 | struct nilfs_transaction_info ti; | 
|  | 2355 | int err; | 
|  | 2356 |  | 
|  | 2357 | if (unlikely(!sci)) | 
|  | 2358 | return -EROFS; | 
|  | 2359 |  | 
|  | 2360 | nilfs_transaction_lock(sb, &ti, 1); | 
|  | 2361 |  | 
|  | 2362 | err = nilfs_mdt_save_to_shadow_map(nilfs->ns_dat); | 
|  | 2363 | if (unlikely(err)) | 
|  | 2364 | goto out_unlock; | 
|  | 2365 |  | 
|  | 2366 | err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs); | 
|  | 2367 | if (unlikely(err)) { | 
|  | 2368 | nilfs_mdt_restore_from_shadow_map(nilfs->ns_dat); | 
|  | 2369 | goto out_unlock; | 
|  | 2370 | } | 
|  | 2371 |  | 
|  | 2372 | sci->sc_freesegs = kbufs[4]; | 
|  | 2373 | sci->sc_nfreesegs = argv[4].v_nmembs; | 
|  | 2374 | list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes); | 
|  | 2375 |  | 
|  | 2376 | for (;;) { | 
|  | 2377 | err = nilfs_segctor_construct(sci, SC_LSEG_SR); | 
|  | 2378 | nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes); | 
|  | 2379 |  | 
|  | 2380 | if (likely(!err)) | 
|  | 2381 | break; | 
|  | 2382 |  | 
|  | 2383 | nilfs_warning(sb, __func__, | 
|  | 2384 | "segment construction failed. (err=%d)", err); | 
|  | 2385 | set_current_state(TASK_INTERRUPTIBLE); | 
|  | 2386 | schedule_timeout(sci->sc_interval); | 
|  | 2387 | } | 
|  | 2388 | if (nilfs_test_opt(nilfs, DISCARD)) { | 
|  | 2389 | int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs, | 
|  | 2390 | sci->sc_nfreesegs); | 
|  | 2391 | if (ret) { | 
|  | 2392 | printk(KERN_WARNING | 
|  | 2393 | "NILFS warning: error %d on discard request, " | 
|  | 2394 | "turning discards off for the device\n", ret); | 
|  | 2395 | nilfs_clear_opt(nilfs, DISCARD); | 
|  | 2396 | } | 
|  | 2397 | } | 
|  | 2398 |  | 
|  | 2399 | out_unlock: | 
|  | 2400 | sci->sc_freesegs = NULL; | 
|  | 2401 | sci->sc_nfreesegs = 0; | 
|  | 2402 | nilfs_mdt_clear_shadow_map(nilfs->ns_dat); | 
|  | 2403 | nilfs_transaction_unlock(sb); | 
|  | 2404 | return err; | 
|  | 2405 | } | 
|  | 2406 |  | 
|  | 2407 | static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode) | 
|  | 2408 | { | 
|  | 2409 | struct nilfs_transaction_info ti; | 
|  | 2410 |  | 
|  | 2411 | nilfs_transaction_lock(sci->sc_super, &ti, 0); | 
|  | 2412 | nilfs_segctor_construct(sci, mode); | 
|  | 2413 |  | 
|  | 2414 | /* | 
|  | 2415 | * Unclosed segment should be retried.  We do this using sc_timer. | 
|  | 2416 | * Timeout of sc_timer will invoke complete construction which leads | 
|  | 2417 | * to close the current logical segment. | 
|  | 2418 | */ | 
|  | 2419 | if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) | 
|  | 2420 | nilfs_segctor_start_timer(sci); | 
|  | 2421 |  | 
|  | 2422 | nilfs_transaction_unlock(sci->sc_super); | 
|  | 2423 | } | 
|  | 2424 |  | 
|  | 2425 | static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci) | 
|  | 2426 | { | 
|  | 2427 | int mode = 0; | 
|  | 2428 | int err; | 
|  | 2429 |  | 
|  | 2430 | spin_lock(&sci->sc_state_lock); | 
|  | 2431 | mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ? | 
|  | 2432 | SC_FLUSH_DAT : SC_FLUSH_FILE; | 
|  | 2433 | spin_unlock(&sci->sc_state_lock); | 
|  | 2434 |  | 
|  | 2435 | if (mode) { | 
|  | 2436 | err = nilfs_segctor_do_construct(sci, mode); | 
|  | 2437 |  | 
|  | 2438 | spin_lock(&sci->sc_state_lock); | 
|  | 2439 | sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ? | 
|  | 2440 | ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT; | 
|  | 2441 | spin_unlock(&sci->sc_state_lock); | 
|  | 2442 | } | 
|  | 2443 | clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags); | 
|  | 2444 | } | 
|  | 2445 |  | 
|  | 2446 | static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci) | 
|  | 2447 | { | 
|  | 2448 | if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) || | 
|  | 2449 | time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) { | 
|  | 2450 | if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT)) | 
|  | 2451 | return SC_FLUSH_FILE; | 
|  | 2452 | else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT)) | 
|  | 2453 | return SC_FLUSH_DAT; | 
|  | 2454 | } | 
|  | 2455 | return SC_LSEG_SR; | 
|  | 2456 | } | 
|  | 2457 |  | 
|  | 2458 | /** | 
|  | 2459 | * nilfs_segctor_thread - main loop of the segment constructor thread. | 
|  | 2460 | * @arg: pointer to a struct nilfs_sc_info. | 
|  | 2461 | * | 
|  | 2462 | * nilfs_segctor_thread() initializes a timer and serves as a daemon | 
|  | 2463 | * to execute segment constructions. | 
|  | 2464 | */ | 
|  | 2465 | static int nilfs_segctor_thread(void *arg) | 
|  | 2466 | { | 
|  | 2467 | struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg; | 
|  | 2468 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; | 
|  | 2469 | int timeout = 0; | 
|  | 2470 |  | 
|  | 2471 | sci->sc_timer.data = (unsigned long)current; | 
|  | 2472 | sci->sc_timer.function = nilfs_construction_timeout; | 
|  | 2473 |  | 
|  | 2474 | /* start sync. */ | 
|  | 2475 | sci->sc_task = current; | 
|  | 2476 | wake_up(&sci->sc_wait_task); /* for nilfs_segctor_start_thread() */ | 
|  | 2477 | printk(KERN_INFO | 
|  | 2478 | "segctord starting. Construction interval = %lu seconds, " | 
|  | 2479 | "CP frequency < %lu seconds\n", | 
|  | 2480 | sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ); | 
|  | 2481 |  | 
|  | 2482 | spin_lock(&sci->sc_state_lock); | 
|  | 2483 | loop: | 
|  | 2484 | for (;;) { | 
|  | 2485 | int mode; | 
|  | 2486 |  | 
|  | 2487 | if (sci->sc_state & NILFS_SEGCTOR_QUIT) | 
|  | 2488 | goto end_thread; | 
|  | 2489 |  | 
|  | 2490 | if (timeout || sci->sc_seq_request != sci->sc_seq_done) | 
|  | 2491 | mode = SC_LSEG_SR; | 
|  | 2492 | else if (!sci->sc_flush_request) | 
|  | 2493 | break; | 
|  | 2494 | else | 
|  | 2495 | mode = nilfs_segctor_flush_mode(sci); | 
|  | 2496 |  | 
|  | 2497 | spin_unlock(&sci->sc_state_lock); | 
|  | 2498 | nilfs_segctor_thread_construct(sci, mode); | 
|  | 2499 | spin_lock(&sci->sc_state_lock); | 
|  | 2500 | timeout = 0; | 
|  | 2501 | } | 
|  | 2502 |  | 
|  | 2503 |  | 
|  | 2504 | if (freezing(current)) { | 
|  | 2505 | spin_unlock(&sci->sc_state_lock); | 
|  | 2506 | try_to_freeze(); | 
|  | 2507 | spin_lock(&sci->sc_state_lock); | 
|  | 2508 | } else { | 
|  | 2509 | DEFINE_WAIT(wait); | 
|  | 2510 | int should_sleep = 1; | 
|  | 2511 |  | 
|  | 2512 | prepare_to_wait(&sci->sc_wait_daemon, &wait, | 
|  | 2513 | TASK_INTERRUPTIBLE); | 
|  | 2514 |  | 
|  | 2515 | if (sci->sc_seq_request != sci->sc_seq_done) | 
|  | 2516 | should_sleep = 0; | 
|  | 2517 | else if (sci->sc_flush_request) | 
|  | 2518 | should_sleep = 0; | 
|  | 2519 | else if (sci->sc_state & NILFS_SEGCTOR_COMMIT) | 
|  | 2520 | should_sleep = time_before(jiffies, | 
|  | 2521 | sci->sc_timer.expires); | 
|  | 2522 |  | 
|  | 2523 | if (should_sleep) { | 
|  | 2524 | spin_unlock(&sci->sc_state_lock); | 
|  | 2525 | schedule(); | 
|  | 2526 | spin_lock(&sci->sc_state_lock); | 
|  | 2527 | } | 
|  | 2528 | finish_wait(&sci->sc_wait_daemon, &wait); | 
|  | 2529 | timeout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) && | 
|  | 2530 | time_after_eq(jiffies, sci->sc_timer.expires)); | 
|  | 2531 |  | 
|  | 2532 | if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs)) | 
|  | 2533 | set_nilfs_discontinued(nilfs); | 
|  | 2534 | } | 
|  | 2535 | goto loop; | 
|  | 2536 |  | 
|  | 2537 | end_thread: | 
|  | 2538 | spin_unlock(&sci->sc_state_lock); | 
|  | 2539 |  | 
|  | 2540 | /* end sync. */ | 
|  | 2541 | sci->sc_task = NULL; | 
|  | 2542 | wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */ | 
|  | 2543 | return 0; | 
|  | 2544 | } | 
|  | 2545 |  | 
|  | 2546 | static int nilfs_segctor_start_thread(struct nilfs_sc_info *sci) | 
|  | 2547 | { | 
|  | 2548 | struct task_struct *t; | 
|  | 2549 |  | 
|  | 2550 | t = kthread_run(nilfs_segctor_thread, sci, "segctord"); | 
|  | 2551 | if (IS_ERR(t)) { | 
|  | 2552 | int err = PTR_ERR(t); | 
|  | 2553 |  | 
|  | 2554 | printk(KERN_ERR "NILFS: error %d creating segctord thread\n", | 
|  | 2555 | err); | 
|  | 2556 | return err; | 
|  | 2557 | } | 
|  | 2558 | wait_event(sci->sc_wait_task, sci->sc_task != NULL); | 
|  | 2559 | return 0; | 
|  | 2560 | } | 
|  | 2561 |  | 
|  | 2562 | static void nilfs_segctor_kill_thread(struct nilfs_sc_info *sci) | 
|  | 2563 | __acquires(&sci->sc_state_lock) | 
|  | 2564 | __releases(&sci->sc_state_lock) | 
|  | 2565 | { | 
|  | 2566 | sci->sc_state |= NILFS_SEGCTOR_QUIT; | 
|  | 2567 |  | 
|  | 2568 | while (sci->sc_task) { | 
|  | 2569 | wake_up(&sci->sc_wait_daemon); | 
|  | 2570 | spin_unlock(&sci->sc_state_lock); | 
|  | 2571 | wait_event(sci->sc_wait_task, sci->sc_task == NULL); | 
|  | 2572 | spin_lock(&sci->sc_state_lock); | 
|  | 2573 | } | 
|  | 2574 | } | 
|  | 2575 |  | 
|  | 2576 | /* | 
|  | 2577 | * Setup & clean-up functions | 
|  | 2578 | */ | 
|  | 2579 | static struct nilfs_sc_info *nilfs_segctor_new(struct super_block *sb, | 
|  | 2580 | struct nilfs_root *root) | 
|  | 2581 | { | 
|  | 2582 | struct the_nilfs *nilfs = sb->s_fs_info; | 
|  | 2583 | struct nilfs_sc_info *sci; | 
|  | 2584 |  | 
|  | 2585 | sci = kzalloc(sizeof(*sci), GFP_KERNEL); | 
|  | 2586 | if (!sci) | 
|  | 2587 | return NULL; | 
|  | 2588 |  | 
|  | 2589 | sci->sc_super = sb; | 
|  | 2590 |  | 
|  | 2591 | nilfs_get_root(root); | 
|  | 2592 | sci->sc_root = root; | 
|  | 2593 |  | 
|  | 2594 | init_waitqueue_head(&sci->sc_wait_request); | 
|  | 2595 | init_waitqueue_head(&sci->sc_wait_daemon); | 
|  | 2596 | init_waitqueue_head(&sci->sc_wait_task); | 
|  | 2597 | spin_lock_init(&sci->sc_state_lock); | 
|  | 2598 | INIT_LIST_HEAD(&sci->sc_dirty_files); | 
|  | 2599 | INIT_LIST_HEAD(&sci->sc_segbufs); | 
|  | 2600 | INIT_LIST_HEAD(&sci->sc_write_logs); | 
|  | 2601 | INIT_LIST_HEAD(&sci->sc_gc_inodes); | 
|  | 2602 | INIT_LIST_HEAD(&sci->sc_iput_queue); | 
|  | 2603 | INIT_WORK(&sci->sc_iput_work, nilfs_iput_work_func); | 
|  | 2604 | init_timer(&sci->sc_timer); | 
|  | 2605 |  | 
|  | 2606 | sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT; | 
|  | 2607 | sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ; | 
|  | 2608 | sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK; | 
|  | 2609 |  | 
|  | 2610 | if (nilfs->ns_interval) | 
|  | 2611 | sci->sc_interval = HZ * nilfs->ns_interval; | 
|  | 2612 | if (nilfs->ns_watermark) | 
|  | 2613 | sci->sc_watermark = nilfs->ns_watermark; | 
|  | 2614 | return sci; | 
|  | 2615 | } | 
|  | 2616 |  | 
|  | 2617 | static void nilfs_segctor_write_out(struct nilfs_sc_info *sci) | 
|  | 2618 | { | 
|  | 2619 | int ret, retrycount = NILFS_SC_CLEANUP_RETRY; | 
|  | 2620 |  | 
|  | 2621 | /* The segctord thread was stopped and its timer was removed. | 
|  | 2622 | But some tasks remain. */ | 
|  | 2623 | do { | 
|  | 2624 | struct nilfs_transaction_info ti; | 
|  | 2625 |  | 
|  | 2626 | nilfs_transaction_lock(sci->sc_super, &ti, 0); | 
|  | 2627 | ret = nilfs_segctor_construct(sci, SC_LSEG_SR); | 
|  | 2628 | nilfs_transaction_unlock(sci->sc_super); | 
|  | 2629 |  | 
|  | 2630 | flush_work(&sci->sc_iput_work); | 
|  | 2631 |  | 
|  | 2632 | } while (ret && retrycount-- > 0); | 
|  | 2633 | } | 
|  | 2634 |  | 
|  | 2635 | /** | 
|  | 2636 | * nilfs_segctor_destroy - destroy the segment constructor. | 
|  | 2637 | * @sci: nilfs_sc_info | 
|  | 2638 | * | 
|  | 2639 | * nilfs_segctor_destroy() kills the segctord thread and frees | 
|  | 2640 | * the nilfs_sc_info struct. | 
|  | 2641 | * Caller must hold the segment semaphore. | 
|  | 2642 | */ | 
|  | 2643 | static void nilfs_segctor_destroy(struct nilfs_sc_info *sci) | 
|  | 2644 | { | 
|  | 2645 | struct the_nilfs *nilfs = sci->sc_super->s_fs_info; | 
|  | 2646 | int flag; | 
|  | 2647 |  | 
|  | 2648 | up_write(&nilfs->ns_segctor_sem); | 
|  | 2649 |  | 
|  | 2650 | spin_lock(&sci->sc_state_lock); | 
|  | 2651 | nilfs_segctor_kill_thread(sci); | 
|  | 2652 | flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request | 
|  | 2653 | || sci->sc_seq_request != sci->sc_seq_done); | 
|  | 2654 | spin_unlock(&sci->sc_state_lock); | 
|  | 2655 |  | 
|  | 2656 | if (flush_work(&sci->sc_iput_work)) | 
|  | 2657 | flag = true; | 
|  | 2658 |  | 
|  | 2659 | if (flag || !nilfs_segctor_confirm(sci)) | 
|  | 2660 | nilfs_segctor_write_out(sci); | 
|  | 2661 |  | 
|  | 2662 | if (!list_empty(&sci->sc_dirty_files)) { | 
|  | 2663 | nilfs_warning(sci->sc_super, __func__, | 
|  | 2664 | "dirty file(s) after the final construction\n"); | 
|  | 2665 | nilfs_dispose_list(nilfs, &sci->sc_dirty_files, 1); | 
|  | 2666 | } | 
|  | 2667 |  | 
|  | 2668 | if (!list_empty(&sci->sc_iput_queue)) { | 
|  | 2669 | nilfs_warning(sci->sc_super, __func__, | 
|  | 2670 | "iput queue is not empty\n"); | 
|  | 2671 | nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 1); | 
|  | 2672 | } | 
|  | 2673 |  | 
|  | 2674 | WARN_ON(!list_empty(&sci->sc_segbufs)); | 
|  | 2675 | WARN_ON(!list_empty(&sci->sc_write_logs)); | 
|  | 2676 |  | 
|  | 2677 | nilfs_put_root(sci->sc_root); | 
|  | 2678 |  | 
|  | 2679 | down_write(&nilfs->ns_segctor_sem); | 
|  | 2680 |  | 
|  | 2681 | del_timer_sync(&sci->sc_timer); | 
|  | 2682 | kfree(sci); | 
|  | 2683 | } | 
|  | 2684 |  | 
|  | 2685 | /** | 
|  | 2686 | * nilfs_attach_log_writer - attach log writer | 
|  | 2687 | * @sb: super block instance | 
|  | 2688 | * @root: root object of the current filesystem tree | 
|  | 2689 | * | 
|  | 2690 | * This allocates a log writer object, initializes it, and starts the | 
|  | 2691 | * log writer. | 
|  | 2692 | * | 
|  | 2693 | * Return Value: On success, 0 is returned. On error, one of the following | 
|  | 2694 | * negative error code is returned. | 
|  | 2695 | * | 
|  | 2696 | * %-ENOMEM - Insufficient memory available. | 
|  | 2697 | */ | 
|  | 2698 | int nilfs_attach_log_writer(struct super_block *sb, struct nilfs_root *root) | 
|  | 2699 | { | 
|  | 2700 | struct the_nilfs *nilfs = sb->s_fs_info; | 
|  | 2701 | int err; | 
|  | 2702 |  | 
|  | 2703 | if (nilfs->ns_writer) { | 
|  | 2704 | /* | 
|  | 2705 | * This happens if the filesystem was remounted | 
|  | 2706 | * read/write after nilfs_error degenerated it into a | 
|  | 2707 | * read-only mount. | 
|  | 2708 | */ | 
|  | 2709 | nilfs_detach_log_writer(sb); | 
|  | 2710 | } | 
|  | 2711 |  | 
|  | 2712 | nilfs->ns_writer = nilfs_segctor_new(sb, root); | 
|  | 2713 | if (!nilfs->ns_writer) | 
|  | 2714 | return -ENOMEM; | 
|  | 2715 |  | 
|  | 2716 | err = nilfs_segctor_start_thread(nilfs->ns_writer); | 
|  | 2717 | if (err) { | 
|  | 2718 | kfree(nilfs->ns_writer); | 
|  | 2719 | nilfs->ns_writer = NULL; | 
|  | 2720 | } | 
|  | 2721 | return err; | 
|  | 2722 | } | 
|  | 2723 |  | 
|  | 2724 | /** | 
|  | 2725 | * nilfs_detach_log_writer - destroy log writer | 
|  | 2726 | * @sb: super block instance | 
|  | 2727 | * | 
|  | 2728 | * This kills log writer daemon, frees the log writer object, and | 
|  | 2729 | * destroys list of dirty files. | 
|  | 2730 | */ | 
|  | 2731 | void nilfs_detach_log_writer(struct super_block *sb) | 
|  | 2732 | { | 
|  | 2733 | struct the_nilfs *nilfs = sb->s_fs_info; | 
|  | 2734 | LIST_HEAD(garbage_list); | 
|  | 2735 |  | 
|  | 2736 | down_write(&nilfs->ns_segctor_sem); | 
|  | 2737 | if (nilfs->ns_writer) { | 
|  | 2738 | nilfs_segctor_destroy(nilfs->ns_writer); | 
|  | 2739 | nilfs->ns_writer = NULL; | 
|  | 2740 | } | 
|  | 2741 |  | 
|  | 2742 | /* Force to free the list of dirty files */ | 
|  | 2743 | spin_lock(&nilfs->ns_inode_lock); | 
|  | 2744 | if (!list_empty(&nilfs->ns_dirty_files)) { | 
|  | 2745 | list_splice_init(&nilfs->ns_dirty_files, &garbage_list); | 
|  | 2746 | nilfs_warning(sb, __func__, | 
|  | 2747 | "Hit dirty file after stopped log writer\n"); | 
|  | 2748 | } | 
|  | 2749 | spin_unlock(&nilfs->ns_inode_lock); | 
|  | 2750 | up_write(&nilfs->ns_segctor_sem); | 
|  | 2751 |  | 
|  | 2752 | nilfs_dispose_list(nilfs, &garbage_list, 1); | 
|  | 2753 | } |