Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * mdt.c - meta data file for NILFS |
| 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 | #include <linux/buffer_head.h> |
| 24 | #include <linux/mpage.h> |
| 25 | #include <linux/mm.h> |
| 26 | #include <linux/writeback.h> |
| 27 | #include <linux/backing-dev.h> |
| 28 | #include <linux/swap.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 30 | #include "nilfs.h" |
| 31 | #include "segment.h" |
| 32 | #include "page.h" |
| 33 | #include "mdt.h" |
| 34 | |
| 35 | |
| 36 | #define NILFS_MDT_MAX_RA_BLOCKS (16 - 1) |
| 37 | |
| 38 | #define INIT_UNUSED_INODE_FIELDS |
| 39 | |
| 40 | static int |
| 41 | nilfs_mdt_insert_new_block(struct inode *inode, unsigned long block, |
| 42 | struct buffer_head *bh, |
| 43 | void (*init_block)(struct inode *, |
| 44 | struct buffer_head *, void *)) |
| 45 | { |
| 46 | struct nilfs_inode_info *ii = NILFS_I(inode); |
| 47 | void *kaddr; |
| 48 | int ret; |
| 49 | |
| 50 | /* Caller exclude read accesses using page lock */ |
| 51 | |
| 52 | /* set_buffer_new(bh); */ |
| 53 | bh->b_blocknr = 0; |
| 54 | |
| 55 | ret = nilfs_bmap_insert(ii->i_bmap, block, (unsigned long)bh); |
| 56 | if (unlikely(ret)) |
| 57 | return ret; |
| 58 | |
| 59 | set_buffer_mapped(bh); |
| 60 | |
| 61 | kaddr = kmap_atomic(bh->b_page, KM_USER0); |
| 62 | memset(kaddr + bh_offset(bh), 0, 1 << inode->i_blkbits); |
| 63 | if (init_block) |
| 64 | init_block(inode, bh, kaddr); |
| 65 | flush_dcache_page(bh->b_page); |
| 66 | kunmap_atomic(kaddr, KM_USER0); |
| 67 | |
| 68 | set_buffer_uptodate(bh); |
| 69 | nilfs_mark_buffer_dirty(bh); |
| 70 | nilfs_mdt_mark_dirty(inode); |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int nilfs_mdt_create_block(struct inode *inode, unsigned long block, |
| 75 | struct buffer_head **out_bh, |
| 76 | void (*init_block)(struct inode *, |
| 77 | struct buffer_head *, |
| 78 | void *)) |
| 79 | { |
| 80 | struct the_nilfs *nilfs = NILFS_MDT(inode)->mi_nilfs; |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 81 | struct super_block *sb = inode->i_sb; |
| 82 | struct nilfs_transaction_info ti; |
| 83 | struct buffer_head *bh; |
| 84 | int err; |
| 85 | |
| 86 | if (!sb) { |
Ryusuke Konishi | 201913e | 2009-04-28 21:04:59 +0900 | [diff] [blame] | 87 | /* |
| 88 | * Make sure this function is not called from any |
| 89 | * read-only context. |
| 90 | */ |
| 91 | if (!nilfs->ns_writer) { |
| 92 | WARN_ON(1); |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 93 | err = -EROFS; |
| 94 | goto out; |
| 95 | } |
Ryusuke Konishi | 201913e | 2009-04-28 21:04:59 +0900 | [diff] [blame] | 96 | sb = nilfs->ns_writer->s_super; |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | nilfs_transaction_begin(sb, &ti, 0); |
| 100 | |
| 101 | err = -ENOMEM; |
| 102 | bh = nilfs_grab_buffer(inode, inode->i_mapping, block, 0); |
| 103 | if (unlikely(!bh)) |
| 104 | goto failed_unlock; |
| 105 | |
| 106 | err = -EEXIST; |
Ryusuke Konishi | 1435110 | 2009-09-06 17:49:49 +0900 | [diff] [blame] | 107 | if (buffer_uptodate(bh)) |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 108 | goto failed_bh; |
Ryusuke Konishi | 1435110 | 2009-09-06 17:49:49 +0900 | [diff] [blame] | 109 | |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 110 | wait_on_buffer(bh); |
| 111 | if (buffer_uptodate(bh)) |
| 112 | goto failed_bh; |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 113 | |
| 114 | bh->b_bdev = nilfs->ns_bdev; |
| 115 | err = nilfs_mdt_insert_new_block(inode, block, bh, init_block); |
| 116 | if (likely(!err)) { |
| 117 | get_bh(bh); |
| 118 | *out_bh = bh; |
| 119 | } |
| 120 | |
| 121 | failed_bh: |
| 122 | unlock_page(bh->b_page); |
| 123 | page_cache_release(bh->b_page); |
| 124 | brelse(bh); |
| 125 | |
| 126 | failed_unlock: |
Ryusuke Konishi | 47420c7 | 2009-04-06 19:01:45 -0700 | [diff] [blame] | 127 | if (likely(!err)) |
| 128 | err = nilfs_transaction_commit(sb); |
| 129 | else |
| 130 | nilfs_transaction_abort(sb); |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 131 | out: |
| 132 | return err; |
| 133 | } |
| 134 | |
| 135 | static int |
| 136 | nilfs_mdt_submit_block(struct inode *inode, unsigned long blkoff, |
| 137 | int mode, struct buffer_head **out_bh) |
| 138 | { |
| 139 | struct buffer_head *bh; |
Ryusuke Konishi | 0f3fe33 | 2009-08-15 02:29:28 +0900 | [diff] [blame] | 140 | __u64 blknum = 0; |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 141 | int ret = -ENOMEM; |
| 142 | |
| 143 | bh = nilfs_grab_buffer(inode, inode->i_mapping, blkoff, 0); |
| 144 | if (unlikely(!bh)) |
| 145 | goto failed; |
| 146 | |
| 147 | ret = -EEXIST; /* internal code */ |
| 148 | if (buffer_uptodate(bh)) |
| 149 | goto out; |
| 150 | |
| 151 | if (mode == READA) { |
| 152 | if (!trylock_buffer(bh)) { |
| 153 | ret = -EBUSY; |
| 154 | goto failed_bh; |
| 155 | } |
Ryusuke Konishi | 1f5abe7 | 2009-04-06 19:01:55 -0700 | [diff] [blame] | 156 | } else /* mode == READ */ |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 157 | lock_buffer(bh); |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 158 | |
| 159 | if (buffer_uptodate(bh)) { |
| 160 | unlock_buffer(bh); |
| 161 | goto out; |
| 162 | } |
Ryusuke Konishi | 1435110 | 2009-09-06 17:49:49 +0900 | [diff] [blame] | 163 | |
| 164 | ret = nilfs_bmap_lookup(NILFS_I(inode)->i_bmap, blkoff, &blknum); |
| 165 | if (unlikely(ret)) { |
| 166 | unlock_buffer(bh); |
| 167 | goto failed_bh; |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 168 | } |
Ryusuke Konishi | 1435110 | 2009-09-06 17:49:49 +0900 | [diff] [blame] | 169 | bh->b_bdev = NILFS_MDT(inode)->mi_nilfs->ns_bdev; |
Ryusuke Konishi | 0f3fe33 | 2009-08-15 02:29:28 +0900 | [diff] [blame] | 170 | bh->b_blocknr = (sector_t)blknum; |
Ryusuke Konishi | 1435110 | 2009-09-06 17:49:49 +0900 | [diff] [blame] | 171 | set_buffer_mapped(bh); |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 172 | |
| 173 | bh->b_end_io = end_buffer_read_sync; |
| 174 | get_bh(bh); |
| 175 | submit_bh(mode, bh); |
| 176 | ret = 0; |
| 177 | out: |
| 178 | get_bh(bh); |
| 179 | *out_bh = bh; |
| 180 | |
| 181 | failed_bh: |
| 182 | unlock_page(bh->b_page); |
| 183 | page_cache_release(bh->b_page); |
| 184 | brelse(bh); |
| 185 | failed: |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | static int nilfs_mdt_read_block(struct inode *inode, unsigned long block, |
Ryusuke Konishi | b34a650 | 2009-11-14 00:09:47 +0900 | [diff] [blame] | 190 | int readahead, struct buffer_head **out_bh) |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 191 | { |
| 192 | struct buffer_head *first_bh, *bh; |
| 193 | unsigned long blkoff; |
| 194 | int i, nr_ra_blocks = NILFS_MDT_MAX_RA_BLOCKS; |
| 195 | int err; |
| 196 | |
| 197 | err = nilfs_mdt_submit_block(inode, block, READ, &first_bh); |
| 198 | if (err == -EEXIST) /* internal code */ |
| 199 | goto out; |
| 200 | |
| 201 | if (unlikely(err)) |
| 202 | goto failed; |
| 203 | |
Ryusuke Konishi | b34a650 | 2009-11-14 00:09:47 +0900 | [diff] [blame] | 204 | if (readahead) { |
| 205 | blkoff = block + 1; |
| 206 | for (i = 0; i < nr_ra_blocks; i++, blkoff++) { |
| 207 | err = nilfs_mdt_submit_block(inode, blkoff, READA, &bh); |
| 208 | if (likely(!err || err == -EEXIST)) |
| 209 | brelse(bh); |
| 210 | else if (err != -EBUSY) |
| 211 | break; |
| 212 | /* abort readahead if bmap lookup failed */ |
| 213 | if (!buffer_locked(first_bh)) |
| 214 | goto out_no_wait; |
| 215 | } |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | wait_on_buffer(first_bh); |
| 219 | |
| 220 | out_no_wait: |
| 221 | err = -EIO; |
| 222 | if (!buffer_uptodate(first_bh)) |
| 223 | goto failed_bh; |
| 224 | out: |
| 225 | *out_bh = first_bh; |
| 226 | return 0; |
| 227 | |
| 228 | failed_bh: |
| 229 | brelse(first_bh); |
| 230 | failed: |
| 231 | return err; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * nilfs_mdt_get_block - read or create a buffer on meta data file. |
| 236 | * @inode: inode of the meta data file |
| 237 | * @blkoff: block offset |
| 238 | * @create: create flag |
| 239 | * @init_block: initializer used for newly allocated block |
| 240 | * @out_bh: output of a pointer to the buffer_head |
| 241 | * |
| 242 | * nilfs_mdt_get_block() looks up the specified buffer and tries to create |
| 243 | * a new buffer if @create is not zero. On success, the returned buffer is |
| 244 | * assured to be either existing or formatted using a buffer lock on success. |
| 245 | * @out_bh is substituted only when zero is returned. |
| 246 | * |
| 247 | * Return Value: On success, it returns 0. On error, the following negative |
| 248 | * error code is returned. |
| 249 | * |
| 250 | * %-ENOMEM - Insufficient memory available. |
| 251 | * |
| 252 | * %-EIO - I/O error |
| 253 | * |
| 254 | * %-ENOENT - the specified block does not exist (hole block) |
| 255 | * |
| 256 | * %-EINVAL - bmap is broken. (the caller should call nilfs_error()) |
| 257 | * |
| 258 | * %-EROFS - Read only filesystem (for create mode) |
| 259 | */ |
| 260 | int nilfs_mdt_get_block(struct inode *inode, unsigned long blkoff, int create, |
| 261 | void (*init_block)(struct inode *, |
| 262 | struct buffer_head *, void *), |
| 263 | struct buffer_head **out_bh) |
| 264 | { |
| 265 | int ret; |
| 266 | |
| 267 | /* Should be rewritten with merging nilfs_mdt_read_block() */ |
| 268 | retry: |
Ryusuke Konishi | b34a650 | 2009-11-14 00:09:47 +0900 | [diff] [blame] | 269 | ret = nilfs_mdt_read_block(inode, blkoff, !create, out_bh); |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 270 | if (!create || ret != -ENOENT) |
| 271 | return ret; |
| 272 | |
| 273 | ret = nilfs_mdt_create_block(inode, blkoff, out_bh, init_block); |
| 274 | if (unlikely(ret == -EEXIST)) { |
| 275 | /* create = 0; */ /* limit read-create loop retries */ |
| 276 | goto retry; |
| 277 | } |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * nilfs_mdt_delete_block - make a hole on the meta data file. |
| 283 | * @inode: inode of the meta data file |
| 284 | * @block: block offset |
| 285 | * |
| 286 | * Return Value: On success, zero is returned. |
| 287 | * On error, one of the following negative error code is returned. |
| 288 | * |
| 289 | * %-ENOMEM - Insufficient memory available. |
| 290 | * |
| 291 | * %-EIO - I/O error |
| 292 | * |
| 293 | * %-EINVAL - bmap is broken. (the caller should call nilfs_error()) |
| 294 | */ |
| 295 | int nilfs_mdt_delete_block(struct inode *inode, unsigned long block) |
| 296 | { |
| 297 | struct nilfs_inode_info *ii = NILFS_I(inode); |
| 298 | int err; |
| 299 | |
| 300 | err = nilfs_bmap_delete(ii->i_bmap, block); |
Ryusuke Konishi | 8433823 | 2009-05-05 21:52:06 +0900 | [diff] [blame] | 301 | if (!err || err == -ENOENT) { |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 302 | nilfs_mdt_mark_dirty(inode); |
| 303 | nilfs_mdt_forget_block(inode, block); |
| 304 | } |
| 305 | return err; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * nilfs_mdt_forget_block - discard dirty state and try to remove the page |
| 310 | * @inode: inode of the meta data file |
| 311 | * @block: block offset |
| 312 | * |
| 313 | * nilfs_mdt_forget_block() clears a dirty flag of the specified buffer, and |
| 314 | * tries to release the page including the buffer from a page cache. |
| 315 | * |
| 316 | * Return Value: On success, 0 is returned. On error, one of the following |
| 317 | * negative error code is returned. |
| 318 | * |
| 319 | * %-EBUSY - page has an active buffer. |
| 320 | * |
| 321 | * %-ENOENT - page cache has no page addressed by the offset. |
| 322 | */ |
| 323 | int nilfs_mdt_forget_block(struct inode *inode, unsigned long block) |
| 324 | { |
| 325 | pgoff_t index = (pgoff_t)block >> |
| 326 | (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 327 | struct page *page; |
| 328 | unsigned long first_block; |
| 329 | int ret = 0; |
| 330 | int still_dirty; |
| 331 | |
| 332 | page = find_lock_page(inode->i_mapping, index); |
| 333 | if (!page) |
| 334 | return -ENOENT; |
| 335 | |
| 336 | wait_on_page_writeback(page); |
| 337 | |
| 338 | first_block = (unsigned long)index << |
| 339 | (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 340 | if (page_has_buffers(page)) { |
| 341 | struct buffer_head *bh; |
| 342 | |
| 343 | bh = nilfs_page_get_nth_block(page, block - first_block); |
| 344 | nilfs_forget_buffer(bh); |
| 345 | } |
| 346 | still_dirty = PageDirty(page); |
| 347 | unlock_page(page); |
| 348 | page_cache_release(page); |
| 349 | |
| 350 | if (still_dirty || |
| 351 | invalidate_inode_pages2_range(inode->i_mapping, index, index) != 0) |
| 352 | ret = -EBUSY; |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * nilfs_mdt_mark_block_dirty - mark a block on the meta data file dirty. |
| 358 | * @inode: inode of the meta data file |
| 359 | * @block: block offset |
| 360 | * |
| 361 | * Return Value: On success, it returns 0. On error, the following negative |
| 362 | * error code is returned. |
| 363 | * |
| 364 | * %-ENOMEM - Insufficient memory available. |
| 365 | * |
| 366 | * %-EIO - I/O error |
| 367 | * |
| 368 | * %-ENOENT - the specified block does not exist (hole block) |
| 369 | * |
| 370 | * %-EINVAL - bmap is broken. (the caller should call nilfs_error()) |
| 371 | */ |
| 372 | int nilfs_mdt_mark_block_dirty(struct inode *inode, unsigned long block) |
| 373 | { |
| 374 | struct buffer_head *bh; |
| 375 | int err; |
| 376 | |
Ryusuke Konishi | b34a650 | 2009-11-14 00:09:47 +0900 | [diff] [blame] | 377 | err = nilfs_mdt_read_block(inode, block, 0, &bh); |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 378 | if (unlikely(err)) |
| 379 | return err; |
| 380 | nilfs_mark_buffer_dirty(bh); |
| 381 | nilfs_mdt_mark_dirty(inode); |
| 382 | brelse(bh); |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | int nilfs_mdt_fetch_dirty(struct inode *inode) |
| 387 | { |
| 388 | struct nilfs_inode_info *ii = NILFS_I(inode); |
| 389 | |
| 390 | if (nilfs_bmap_test_and_clear_dirty(ii->i_bmap)) { |
| 391 | set_bit(NILFS_I_DIRTY, &ii->i_state); |
| 392 | return 1; |
| 393 | } |
| 394 | return test_bit(NILFS_I_DIRTY, &ii->i_state); |
| 395 | } |
| 396 | |
| 397 | static int |
| 398 | nilfs_mdt_write_page(struct page *page, struct writeback_control *wbc) |
| 399 | { |
| 400 | struct inode *inode = container_of(page->mapping, |
| 401 | struct inode, i_data); |
| 402 | struct super_block *sb = inode->i_sb; |
Ryusuke Konishi | 027d640 | 2009-08-02 22:45:33 +0900 | [diff] [blame] | 403 | struct the_nilfs *nilfs = NILFS_MDT(inode)->mi_nilfs; |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 404 | struct nilfs_sb_info *writer = NULL; |
| 405 | int err = 0; |
| 406 | |
| 407 | redirty_page_for_writepage(wbc, page); |
| 408 | unlock_page(page); |
| 409 | |
| 410 | if (page->mapping->assoc_mapping) |
| 411 | return 0; /* Do not request flush for shadow page cache */ |
| 412 | if (!sb) { |
Ryusuke Konishi | 027d640 | 2009-08-02 22:45:33 +0900 | [diff] [blame] | 413 | down_read(&nilfs->ns_writer_sem); |
| 414 | writer = nilfs->ns_writer; |
Ryusuke Konishi | 01a261e | 2009-08-02 17:45:55 +0900 | [diff] [blame] | 415 | if (!writer) { |
Ryusuke Konishi | 027d640 | 2009-08-02 22:45:33 +0900 | [diff] [blame] | 416 | up_read(&nilfs->ns_writer_sem); |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 417 | return -EROFS; |
Ryusuke Konishi | 01a261e | 2009-08-02 17:45:55 +0900 | [diff] [blame] | 418 | } |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 419 | sb = writer->s_super; |
| 420 | } |
| 421 | |
| 422 | if (wbc->sync_mode == WB_SYNC_ALL) |
| 423 | err = nilfs_construct_segment(sb); |
| 424 | else if (wbc->for_reclaim) |
| 425 | nilfs_flush_segment(sb, inode->i_ino); |
| 426 | |
| 427 | if (writer) |
Ryusuke Konishi | 027d640 | 2009-08-02 22:45:33 +0900 | [diff] [blame] | 428 | up_read(&nilfs->ns_writer_sem); |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 429 | return err; |
| 430 | } |
| 431 | |
| 432 | |
Alexey Dobriyan | 7f09410 | 2009-09-21 17:01:10 -0700 | [diff] [blame] | 433 | static const struct address_space_operations def_mdt_aops = { |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 434 | .writepage = nilfs_mdt_write_page, |
Ryusuke Konishi | fa03274 | 2009-05-27 22:44:34 +0900 | [diff] [blame] | 435 | .sync_page = block_sync_page, |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 436 | }; |
| 437 | |
Alexey Dobriyan | 6e1d5dc | 2009-09-21 17:01:11 -0700 | [diff] [blame] | 438 | static const struct inode_operations def_mdt_iops; |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 439 | static const struct file_operations def_mdt_fops; |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 440 | |
| 441 | /* |
| 442 | * NILFS2 uses pseudo inodes for meta data files such as DAT, cpfile, sufile, |
| 443 | * ifile, or gcinodes. This allows the B-tree code and segment constructor |
| 444 | * to treat them like regular files, and this helps to simplify the |
| 445 | * implementation. |
| 446 | * On the other hand, some of the pseudo inodes have an irregular point: |
| 447 | * They don't have valid inode->i_sb pointer because their lifetimes are |
| 448 | * longer than those of the super block structs; they may continue for |
| 449 | * several consecutive mounts/umounts. This would need discussions. |
| 450 | */ |
Ryusuke Konishi | 5731e19 | 2009-11-12 22:42:04 +0900 | [diff] [blame] | 451 | /** |
| 452 | * nilfs_mdt_new_common - allocate a pseudo inode for metadata file |
| 453 | * @nilfs: nilfs object |
| 454 | * @sb: super block instance the metadata file belongs to |
| 455 | * @ino: inode number |
| 456 | * @gfp_mask: gfp mask for data pages |
| 457 | * @objsz: size of the private object attached to inode->i_private |
| 458 | */ |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 459 | struct inode * |
| 460 | nilfs_mdt_new_common(struct the_nilfs *nilfs, struct super_block *sb, |
Ryusuke Konishi | 5731e19 | 2009-11-12 22:42:04 +0900 | [diff] [blame] | 461 | ino_t ino, gfp_t gfp_mask, size_t objsz) |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 462 | { |
Ryusuke Konishi | a53b475 | 2009-05-27 22:11:46 +0900 | [diff] [blame] | 463 | struct inode *inode = nilfs_alloc_inode_common(nilfs); |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 464 | |
| 465 | if (!inode) |
| 466 | return NULL; |
| 467 | else { |
| 468 | struct address_space * const mapping = &inode->i_data; |
Ryusuke Konishi | 5731e19 | 2009-11-12 22:42:04 +0900 | [diff] [blame] | 469 | struct nilfs_mdt_info *mi; |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 470 | |
Ryusuke Konishi | 5731e19 | 2009-11-12 22:42:04 +0900 | [diff] [blame] | 471 | mi = kzalloc(max(sizeof(*mi), objsz), GFP_NOFS); |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 472 | if (!mi) { |
| 473 | nilfs_destroy_inode(inode); |
| 474 | return NULL; |
| 475 | } |
| 476 | mi->mi_nilfs = nilfs; |
| 477 | init_rwsem(&mi->mi_sem); |
| 478 | |
| 479 | inode->i_sb = sb; /* sb may be NULL for some meta data files */ |
| 480 | inode->i_blkbits = nilfs->ns_blocksize_bits; |
| 481 | inode->i_flags = 0; |
| 482 | atomic_set(&inode->i_count, 1); |
| 483 | inode->i_nlink = 1; |
| 484 | inode->i_ino = ino; |
| 485 | inode->i_mode = S_IFREG; |
| 486 | inode->i_private = mi; |
| 487 | |
| 488 | #ifdef INIT_UNUSED_INODE_FIELDS |
| 489 | atomic_set(&inode->i_writecount, 0); |
| 490 | inode->i_size = 0; |
| 491 | inode->i_blocks = 0; |
| 492 | inode->i_bytes = 0; |
| 493 | inode->i_generation = 0; |
| 494 | #ifdef CONFIG_QUOTA |
| 495 | memset(&inode->i_dquot, 0, sizeof(inode->i_dquot)); |
| 496 | #endif |
| 497 | inode->i_pipe = NULL; |
| 498 | inode->i_bdev = NULL; |
| 499 | inode->i_cdev = NULL; |
| 500 | inode->i_rdev = 0; |
| 501 | #ifdef CONFIG_SECURITY |
| 502 | inode->i_security = NULL; |
| 503 | #endif |
| 504 | inode->dirtied_when = 0; |
| 505 | |
| 506 | INIT_LIST_HEAD(&inode->i_list); |
| 507 | INIT_LIST_HEAD(&inode->i_sb_list); |
| 508 | inode->i_state = 0; |
| 509 | #endif |
| 510 | |
| 511 | spin_lock_init(&inode->i_lock); |
| 512 | mutex_init(&inode->i_mutex); |
| 513 | init_rwsem(&inode->i_alloc_sem); |
| 514 | |
| 515 | mapping->host = NULL; /* instead of inode */ |
| 516 | mapping->flags = 0; |
| 517 | mapping_set_gfp_mask(mapping, gfp_mask); |
| 518 | mapping->assoc_mapping = NULL; |
| 519 | mapping->backing_dev_info = nilfs->ns_bdi; |
| 520 | |
| 521 | inode->i_mapping = mapping; |
| 522 | } |
| 523 | |
| 524 | return inode; |
| 525 | } |
| 526 | |
| 527 | struct inode *nilfs_mdt_new(struct the_nilfs *nilfs, struct super_block *sb, |
Ryusuke Konishi | 5731e19 | 2009-11-12 22:42:04 +0900 | [diff] [blame] | 528 | ino_t ino, size_t objsz) |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 529 | { |
Ryusuke Konishi | 5731e19 | 2009-11-12 22:42:04 +0900 | [diff] [blame] | 530 | struct inode *inode; |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 531 | |
Ryusuke Konishi | 5731e19 | 2009-11-12 22:42:04 +0900 | [diff] [blame] | 532 | inode = nilfs_mdt_new_common(nilfs, sb, ino, NILFS_MDT_GFP, objsz); |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 533 | if (!inode) |
| 534 | return NULL; |
| 535 | |
| 536 | inode->i_op = &def_mdt_iops; |
| 537 | inode->i_fop = &def_mdt_fops; |
| 538 | inode->i_mapping->a_ops = &def_mdt_aops; |
| 539 | return inode; |
| 540 | } |
| 541 | |
| 542 | void nilfs_mdt_set_entry_size(struct inode *inode, unsigned entry_size, |
| 543 | unsigned header_size) |
| 544 | { |
| 545 | struct nilfs_mdt_info *mi = NILFS_MDT(inode); |
| 546 | |
| 547 | mi->mi_entry_size = entry_size; |
| 548 | mi->mi_entries_per_block = (1 << inode->i_blkbits) / entry_size; |
| 549 | mi->mi_first_entry_offset = DIV_ROUND_UP(header_size, entry_size); |
| 550 | } |
| 551 | |
| 552 | void nilfs_mdt_set_shadow(struct inode *orig, struct inode *shadow) |
| 553 | { |
| 554 | shadow->i_mapping->assoc_mapping = orig->i_mapping; |
| 555 | NILFS_I(shadow)->i_btnode_cache.assoc_mapping = |
| 556 | &NILFS_I(orig)->i_btnode_cache; |
| 557 | } |
| 558 | |
Ryusuke Konishi | fd66c0d | 2009-11-13 02:25:41 +0900 | [diff] [blame] | 559 | static void nilfs_mdt_clear(struct inode *inode) |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 560 | { |
| 561 | struct nilfs_inode_info *ii = NILFS_I(inode); |
| 562 | |
| 563 | invalidate_mapping_pages(inode->i_mapping, 0, -1); |
| 564 | truncate_inode_pages(inode->i_mapping, 0); |
| 565 | |
Ryusuke Konishi | fd66c0d | 2009-11-13 02:25:41 +0900 | [diff] [blame] | 566 | if (test_bit(NILFS_I_BMAP, &ii->i_state)) |
| 567 | nilfs_bmap_clear(ii->i_bmap); |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 568 | nilfs_btnode_cache_clear(&ii->i_btnode_cache); |
| 569 | } |
| 570 | |
| 571 | void nilfs_mdt_destroy(struct inode *inode) |
| 572 | { |
| 573 | struct nilfs_mdt_info *mdi = NILFS_MDT(inode); |
| 574 | |
Ryusuke Konishi | db38d5a | 2009-11-14 15:54:27 +0900 | [diff] [blame] | 575 | if (mdi->mi_palloc_cache) |
| 576 | nilfs_palloc_destroy_cache(inode); |
Ryusuke Konishi | fd66c0d | 2009-11-13 02:25:41 +0900 | [diff] [blame] | 577 | nilfs_mdt_clear(inode); |
| 578 | |
Ryusuke Konishi | 5eb563f | 2009-04-06 19:01:28 -0700 | [diff] [blame] | 579 | kfree(mdi->mi_bgl); /* kfree(NULL) is safe */ |
| 580 | kfree(mdi); |
| 581 | nilfs_destroy_inode(inode); |
| 582 | } |