Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | * fs/logfs/readwrite.c |
| 3 | * |
| 4 | * As should be obvious for Linux kernel code, license is GPLv2 |
| 5 | * |
| 6 | * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org> |
| 7 | * |
| 8 | * |
| 9 | * Actually contains five sets of very similar functions: |
| 10 | * read read blocks from a file |
| 11 | * seek_hole find next hole |
| 12 | * seek_data find next data block |
| 13 | * valid check whether a block still belongs to a file |
| 14 | * write write blocks to a file |
| 15 | * delete delete a block (for directories and ifile) |
| 16 | * rewrite move existing blocks of a file to a new location (gc helper) |
| 17 | * truncate truncate a file |
| 18 | */ |
| 19 | #include "logfs.h" |
| 20 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 22 | |
| 23 | static u64 adjust_bix(u64 bix, level_t level) |
| 24 | { |
| 25 | switch (level) { |
| 26 | case 0: |
| 27 | return bix; |
| 28 | case LEVEL(1): |
| 29 | return max_t(u64, bix, I0_BLOCKS); |
| 30 | case LEVEL(2): |
| 31 | return max_t(u64, bix, I1_BLOCKS); |
| 32 | case LEVEL(3): |
| 33 | return max_t(u64, bix, I2_BLOCKS); |
| 34 | case LEVEL(4): |
| 35 | return max_t(u64, bix, I3_BLOCKS); |
| 36 | case LEVEL(5): |
| 37 | return max_t(u64, bix, I4_BLOCKS); |
| 38 | default: |
| 39 | WARN_ON(1); |
| 40 | return bix; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | static inline u64 maxbix(u8 height) |
| 45 | { |
| 46 | return 1ULL << (LOGFS_BLOCK_BITS * height); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * The inode address space is cut in two halves. Lower half belongs to data |
| 51 | * pages, upper half to indirect blocks. If the high bit (INDIRECT_BIT) is |
| 52 | * set, the actual block index (bix) and level can be derived from the page |
| 53 | * index. |
| 54 | * |
| 55 | * The lowest three bits of the block index are set to 0 after packing and |
| 56 | * unpacking. Since the lowest n bits (9 for 4KiB blocksize) are ignored |
| 57 | * anyway this is harmless. |
| 58 | */ |
| 59 | #define ARCH_SHIFT (BITS_PER_LONG - 32) |
| 60 | #define INDIRECT_BIT (0x80000000UL << ARCH_SHIFT) |
| 61 | #define LEVEL_SHIFT (28 + ARCH_SHIFT) |
| 62 | static inline pgoff_t first_indirect_block(void) |
| 63 | { |
| 64 | return INDIRECT_BIT | (1ULL << LEVEL_SHIFT); |
| 65 | } |
| 66 | |
| 67 | pgoff_t logfs_pack_index(u64 bix, level_t level) |
| 68 | { |
| 69 | pgoff_t index; |
| 70 | |
| 71 | BUG_ON(bix >= INDIRECT_BIT); |
| 72 | if (level == 0) |
| 73 | return bix; |
| 74 | |
| 75 | index = INDIRECT_BIT; |
| 76 | index |= (__force long)level << LEVEL_SHIFT; |
| 77 | index |= bix >> ((__force u8)level * LOGFS_BLOCK_BITS); |
| 78 | return index; |
| 79 | } |
| 80 | |
| 81 | void logfs_unpack_index(pgoff_t index, u64 *bix, level_t *level) |
| 82 | { |
| 83 | u8 __level; |
| 84 | |
| 85 | if (!(index & INDIRECT_BIT)) { |
| 86 | *bix = index; |
| 87 | *level = 0; |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | __level = (index & ~INDIRECT_BIT) >> LEVEL_SHIFT; |
| 92 | *level = LEVEL(__level); |
| 93 | *bix = (index << (__level * LOGFS_BLOCK_BITS)) & ~INDIRECT_BIT; |
| 94 | *bix = adjust_bix(*bix, *level); |
| 95 | return; |
| 96 | } |
| 97 | #undef ARCH_SHIFT |
| 98 | #undef INDIRECT_BIT |
| 99 | #undef LEVEL_SHIFT |
| 100 | |
| 101 | /* |
| 102 | * Time is stored as nanoseconds since the epoch. |
| 103 | */ |
| 104 | static struct timespec be64_to_timespec(__be64 betime) |
| 105 | { |
| 106 | return ns_to_timespec(be64_to_cpu(betime)); |
| 107 | } |
| 108 | |
| 109 | static __be64 timespec_to_be64(struct timespec tsp) |
| 110 | { |
| 111 | return cpu_to_be64((u64)tsp.tv_sec * NSEC_PER_SEC + tsp.tv_nsec); |
| 112 | } |
| 113 | |
| 114 | static void logfs_disk_to_inode(struct logfs_disk_inode *di, struct inode*inode) |
| 115 | { |
| 116 | struct logfs_inode *li = logfs_inode(inode); |
| 117 | int i; |
| 118 | |
| 119 | inode->i_mode = be16_to_cpu(di->di_mode); |
| 120 | li->li_height = di->di_height; |
| 121 | li->li_flags = be32_to_cpu(di->di_flags); |
| 122 | inode->i_uid = be32_to_cpu(di->di_uid); |
| 123 | inode->i_gid = be32_to_cpu(di->di_gid); |
| 124 | inode->i_size = be64_to_cpu(di->di_size); |
| 125 | logfs_set_blocks(inode, be64_to_cpu(di->di_used_bytes)); |
| 126 | inode->i_atime = be64_to_timespec(di->di_atime); |
| 127 | inode->i_ctime = be64_to_timespec(di->di_ctime); |
| 128 | inode->i_mtime = be64_to_timespec(di->di_mtime); |
| 129 | inode->i_nlink = be32_to_cpu(di->di_refcount); |
| 130 | inode->i_generation = be32_to_cpu(di->di_generation); |
| 131 | |
| 132 | switch (inode->i_mode & S_IFMT) { |
| 133 | case S_IFSOCK: /* fall through */ |
| 134 | case S_IFBLK: /* fall through */ |
| 135 | case S_IFCHR: /* fall through */ |
| 136 | case S_IFIFO: |
| 137 | inode->i_rdev = be64_to_cpu(di->di_data[0]); |
| 138 | break; |
| 139 | case S_IFDIR: /* fall through */ |
| 140 | case S_IFREG: /* fall through */ |
| 141 | case S_IFLNK: |
| 142 | for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++) |
| 143 | li->li_data[i] = be64_to_cpu(di->di_data[i]); |
| 144 | break; |
| 145 | default: |
| 146 | BUG(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | static void logfs_inode_to_disk(struct inode *inode, struct logfs_disk_inode*di) |
| 151 | { |
| 152 | struct logfs_inode *li = logfs_inode(inode); |
| 153 | int i; |
| 154 | |
| 155 | di->di_mode = cpu_to_be16(inode->i_mode); |
| 156 | di->di_height = li->li_height; |
| 157 | di->di_pad = 0; |
| 158 | di->di_flags = cpu_to_be32(li->li_flags); |
| 159 | di->di_uid = cpu_to_be32(inode->i_uid); |
| 160 | di->di_gid = cpu_to_be32(inode->i_gid); |
| 161 | di->di_size = cpu_to_be64(i_size_read(inode)); |
| 162 | di->di_used_bytes = cpu_to_be64(li->li_used_bytes); |
| 163 | di->di_atime = timespec_to_be64(inode->i_atime); |
| 164 | di->di_ctime = timespec_to_be64(inode->i_ctime); |
| 165 | di->di_mtime = timespec_to_be64(inode->i_mtime); |
| 166 | di->di_refcount = cpu_to_be32(inode->i_nlink); |
| 167 | di->di_generation = cpu_to_be32(inode->i_generation); |
| 168 | |
| 169 | switch (inode->i_mode & S_IFMT) { |
| 170 | case S_IFSOCK: /* fall through */ |
| 171 | case S_IFBLK: /* fall through */ |
| 172 | case S_IFCHR: /* fall through */ |
| 173 | case S_IFIFO: |
| 174 | di->di_data[0] = cpu_to_be64(inode->i_rdev); |
| 175 | break; |
| 176 | case S_IFDIR: /* fall through */ |
| 177 | case S_IFREG: /* fall through */ |
| 178 | case S_IFLNK: |
| 179 | for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++) |
| 180 | di->di_data[i] = cpu_to_be64(li->li_data[i]); |
| 181 | break; |
| 182 | default: |
| 183 | BUG(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | static void __logfs_set_blocks(struct inode *inode) |
| 188 | { |
| 189 | struct super_block *sb = inode->i_sb; |
| 190 | struct logfs_inode *li = logfs_inode(inode); |
| 191 | |
| 192 | inode->i_blocks = ULONG_MAX; |
| 193 | if (li->li_used_bytes >> sb->s_blocksize_bits < ULONG_MAX) |
| 194 | inode->i_blocks = ALIGN(li->li_used_bytes, 512) >> 9; |
| 195 | } |
| 196 | |
| 197 | void logfs_set_blocks(struct inode *inode, u64 bytes) |
| 198 | { |
| 199 | struct logfs_inode *li = logfs_inode(inode); |
| 200 | |
| 201 | li->li_used_bytes = bytes; |
| 202 | __logfs_set_blocks(inode); |
| 203 | } |
| 204 | |
| 205 | static void prelock_page(struct super_block *sb, struct page *page, int lock) |
| 206 | { |
| 207 | struct logfs_super *super = logfs_super(sb); |
| 208 | |
| 209 | BUG_ON(!PageLocked(page)); |
| 210 | if (lock) { |
| 211 | BUG_ON(PagePreLocked(page)); |
| 212 | SetPagePreLocked(page); |
| 213 | } else { |
| 214 | /* We are in GC path. */ |
| 215 | if (PagePreLocked(page)) |
| 216 | super->s_lock_count++; |
| 217 | else |
| 218 | SetPagePreLocked(page); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | static void preunlock_page(struct super_block *sb, struct page *page, int lock) |
| 223 | { |
| 224 | struct logfs_super *super = logfs_super(sb); |
| 225 | |
| 226 | BUG_ON(!PageLocked(page)); |
| 227 | if (lock) |
| 228 | ClearPagePreLocked(page); |
| 229 | else { |
| 230 | /* We are in GC path. */ |
| 231 | BUG_ON(!PagePreLocked(page)); |
| 232 | if (super->s_lock_count) |
| 233 | super->s_lock_count--; |
| 234 | else |
| 235 | ClearPagePreLocked(page); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Logfs is prone to an AB-BA deadlock where one task tries to acquire |
| 241 | * s_write_mutex with a locked page and GC tries to get that page while holding |
| 242 | * s_write_mutex. |
| 243 | * To solve this issue logfs will ignore the page lock iff the page in question |
| 244 | * is waiting for s_write_mutex. We annotate this fact by setting PG_pre_locked |
| 245 | * in addition to PG_locked. |
| 246 | */ |
| 247 | static void logfs_get_wblocks(struct super_block *sb, struct page *page, |
| 248 | int lock) |
| 249 | { |
| 250 | struct logfs_super *super = logfs_super(sb); |
| 251 | |
| 252 | if (page) |
| 253 | prelock_page(sb, page, lock); |
| 254 | |
| 255 | if (lock) { |
| 256 | mutex_lock(&super->s_write_mutex); |
| 257 | logfs_gc_pass(sb); |
| 258 | /* FIXME: We also have to check for shadowed space |
| 259 | * and mempool fill grade */ |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | static void logfs_put_wblocks(struct super_block *sb, struct page *page, |
| 264 | int lock) |
| 265 | { |
| 266 | struct logfs_super *super = logfs_super(sb); |
| 267 | |
| 268 | if (page) |
| 269 | preunlock_page(sb, page, lock); |
| 270 | /* Order matters - we must clear PG_pre_locked before releasing |
| 271 | * s_write_mutex or we could race against another task. */ |
| 272 | if (lock) |
| 273 | mutex_unlock(&super->s_write_mutex); |
| 274 | } |
| 275 | |
| 276 | static struct page *logfs_get_read_page(struct inode *inode, u64 bix, |
| 277 | level_t level) |
| 278 | { |
| 279 | return find_or_create_page(inode->i_mapping, |
| 280 | logfs_pack_index(bix, level), GFP_NOFS); |
| 281 | } |
| 282 | |
| 283 | static void logfs_put_read_page(struct page *page) |
| 284 | { |
| 285 | unlock_page(page); |
| 286 | page_cache_release(page); |
| 287 | } |
| 288 | |
| 289 | static void logfs_lock_write_page(struct page *page) |
| 290 | { |
| 291 | int loop = 0; |
| 292 | |
| 293 | while (unlikely(!trylock_page(page))) { |
| 294 | if (loop++ > 0x1000) { |
| 295 | /* Has been observed once so far... */ |
| 296 | printk(KERN_ERR "stack at %p\n", &loop); |
| 297 | BUG(); |
| 298 | } |
| 299 | if (PagePreLocked(page)) { |
| 300 | /* Holder of page lock is waiting for us, it |
| 301 | * is safe to use this page. */ |
| 302 | break; |
| 303 | } |
| 304 | /* Some other process has this page locked and has |
| 305 | * nothing to do with us. Wait for it to finish. |
| 306 | */ |
| 307 | schedule(); |
| 308 | } |
| 309 | BUG_ON(!PageLocked(page)); |
| 310 | } |
| 311 | |
| 312 | static struct page *logfs_get_write_page(struct inode *inode, u64 bix, |
| 313 | level_t level) |
| 314 | { |
| 315 | struct address_space *mapping = inode->i_mapping; |
| 316 | pgoff_t index = logfs_pack_index(bix, level); |
| 317 | struct page *page; |
| 318 | int err; |
| 319 | |
| 320 | repeat: |
| 321 | page = find_get_page(mapping, index); |
| 322 | if (!page) { |
| 323 | page = __page_cache_alloc(GFP_NOFS); |
| 324 | if (!page) |
| 325 | return NULL; |
| 326 | err = add_to_page_cache_lru(page, mapping, index, GFP_NOFS); |
| 327 | if (unlikely(err)) { |
| 328 | page_cache_release(page); |
| 329 | if (err == -EEXIST) |
| 330 | goto repeat; |
| 331 | return NULL; |
| 332 | } |
| 333 | } else logfs_lock_write_page(page); |
| 334 | BUG_ON(!PageLocked(page)); |
| 335 | return page; |
| 336 | } |
| 337 | |
| 338 | static void logfs_unlock_write_page(struct page *page) |
| 339 | { |
| 340 | if (!PagePreLocked(page)) |
| 341 | unlock_page(page); |
| 342 | } |
| 343 | |
| 344 | static void logfs_put_write_page(struct page *page) |
| 345 | { |
| 346 | logfs_unlock_write_page(page); |
| 347 | page_cache_release(page); |
| 348 | } |
| 349 | |
| 350 | static struct page *logfs_get_page(struct inode *inode, u64 bix, level_t level, |
| 351 | int rw) |
| 352 | { |
| 353 | if (rw == READ) |
| 354 | return logfs_get_read_page(inode, bix, level); |
| 355 | else |
| 356 | return logfs_get_write_page(inode, bix, level); |
| 357 | } |
| 358 | |
| 359 | static void logfs_put_page(struct page *page, int rw) |
| 360 | { |
| 361 | if (rw == READ) |
| 362 | logfs_put_read_page(page); |
| 363 | else |
| 364 | logfs_put_write_page(page); |
| 365 | } |
| 366 | |
| 367 | static unsigned long __get_bits(u64 val, int skip, int no) |
| 368 | { |
| 369 | u64 ret = val; |
| 370 | |
| 371 | ret >>= skip * no; |
| 372 | ret <<= 64 - no; |
| 373 | ret >>= 64 - no; |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | static unsigned long get_bits(u64 val, level_t skip) |
| 378 | { |
| 379 | return __get_bits(val, (__force int)skip, LOGFS_BLOCK_BITS); |
| 380 | } |
| 381 | |
| 382 | static inline void init_shadow_tree(struct super_block *sb, |
| 383 | struct shadow_tree *tree) |
| 384 | { |
| 385 | struct logfs_super *super = logfs_super(sb); |
| 386 | |
| 387 | btree_init_mempool64(&tree->new, super->s_btree_pool); |
| 388 | btree_init_mempool64(&tree->old, super->s_btree_pool); |
| 389 | } |
| 390 | |
| 391 | static void indirect_write_block(struct logfs_block *block) |
| 392 | { |
| 393 | struct page *page; |
| 394 | struct inode *inode; |
| 395 | int ret; |
| 396 | |
| 397 | page = block->page; |
| 398 | inode = page->mapping->host; |
| 399 | logfs_lock_write_page(page); |
| 400 | ret = logfs_write_buf(inode, page, 0); |
| 401 | logfs_unlock_write_page(page); |
| 402 | /* |
| 403 | * This needs some rework. Unless you want your filesystem to run |
| 404 | * completely synchronously (you don't), the filesystem will always |
| 405 | * report writes as 'successful' before the actual work has been |
| 406 | * done. The actual work gets done here and this is where any errors |
| 407 | * will show up. And there isn't much we can do about it, really. |
| 408 | * |
| 409 | * Some attempts to fix the errors (move from bad blocks, retry io,...) |
| 410 | * have already been done, so anything left should be either a broken |
| 411 | * device or a bug somewhere in logfs itself. Being relatively new, |
| 412 | * the odds currently favor a bug, so for now the line below isn't |
| 413 | * entirely tasteles. |
| 414 | */ |
| 415 | BUG_ON(ret); |
| 416 | } |
| 417 | |
| 418 | static void inode_write_block(struct logfs_block *block) |
| 419 | { |
| 420 | struct inode *inode; |
| 421 | int ret; |
| 422 | |
| 423 | inode = block->inode; |
| 424 | if (inode->i_ino == LOGFS_INO_MASTER) |
Joern Engel | c6d38301 | 2010-03-04 21:36:19 +0100 | [diff] [blame] | 425 | logfs_write_anchor(inode->i_sb); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 426 | else { |
| 427 | ret = __logfs_write_inode(inode, 0); |
| 428 | /* see indirect_write_block comment */ |
| 429 | BUG_ON(ret); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | static gc_level_t inode_block_level(struct logfs_block *block) |
| 434 | { |
| 435 | BUG_ON(block->inode->i_ino == LOGFS_INO_MASTER); |
| 436 | return GC_LEVEL(LOGFS_MAX_LEVELS); |
| 437 | } |
| 438 | |
| 439 | static gc_level_t indirect_block_level(struct logfs_block *block) |
| 440 | { |
| 441 | struct page *page; |
| 442 | struct inode *inode; |
| 443 | u64 bix; |
| 444 | level_t level; |
| 445 | |
| 446 | page = block->page; |
| 447 | inode = page->mapping->host; |
| 448 | logfs_unpack_index(page->index, &bix, &level); |
| 449 | return expand_level(inode->i_ino, level); |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * This silences a false, yet annoying gcc warning. I hate it when my editor |
| 454 | * jumps into bitops.h each time I recompile this file. |
| 455 | * TODO: Complain to gcc folks about this and upgrade compiler. |
| 456 | */ |
| 457 | static unsigned long fnb(const unsigned long *addr, |
| 458 | unsigned long size, unsigned long offset) |
| 459 | { |
| 460 | return find_next_bit(addr, size, offset); |
| 461 | } |
| 462 | |
| 463 | static __be64 inode_val0(struct inode *inode) |
| 464 | { |
| 465 | struct logfs_inode *li = logfs_inode(inode); |
| 466 | u64 val; |
| 467 | |
| 468 | /* |
| 469 | * Explicit shifting generates good code, but must match the format |
| 470 | * of the structure. Add some paranoia just in case. |
| 471 | */ |
| 472 | BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_mode) != 0); |
| 473 | BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_height) != 2); |
| 474 | BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_flags) != 4); |
| 475 | |
| 476 | val = (u64)inode->i_mode << 48 | |
| 477 | (u64)li->li_height << 40 | |
| 478 | (u64)li->li_flags; |
| 479 | return cpu_to_be64(val); |
| 480 | } |
| 481 | |
| 482 | static int inode_write_alias(struct super_block *sb, |
| 483 | struct logfs_block *block, write_alias_t *write_one_alias) |
| 484 | { |
| 485 | struct inode *inode = block->inode; |
| 486 | struct logfs_inode *li = logfs_inode(inode); |
| 487 | unsigned long pos; |
| 488 | u64 ino , bix; |
| 489 | __be64 val; |
| 490 | level_t level; |
| 491 | int err; |
| 492 | |
| 493 | for (pos = 0; ; pos++) { |
| 494 | pos = fnb(block->alias_map, LOGFS_BLOCK_FACTOR, pos); |
| 495 | if (pos >= LOGFS_EMBEDDED_FIELDS + INODE_POINTER_OFS) |
| 496 | return 0; |
| 497 | |
| 498 | switch (pos) { |
| 499 | case INODE_HEIGHT_OFS: |
| 500 | val = inode_val0(inode); |
| 501 | break; |
| 502 | case INODE_USED_OFS: |
| 503 | val = cpu_to_be64(li->li_used_bytes);; |
| 504 | break; |
| 505 | case INODE_SIZE_OFS: |
| 506 | val = cpu_to_be64(i_size_read(inode)); |
| 507 | break; |
| 508 | case INODE_POINTER_OFS ... INODE_POINTER_OFS + LOGFS_EMBEDDED_FIELDS - 1: |
| 509 | val = cpu_to_be64(li->li_data[pos - INODE_POINTER_OFS]); |
| 510 | break; |
| 511 | default: |
| 512 | BUG(); |
| 513 | } |
| 514 | |
| 515 | ino = LOGFS_INO_MASTER; |
| 516 | bix = inode->i_ino; |
| 517 | level = LEVEL(0); |
| 518 | err = write_one_alias(sb, ino, bix, level, pos, val); |
| 519 | if (err) |
| 520 | return err; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | static int indirect_write_alias(struct super_block *sb, |
| 525 | struct logfs_block *block, write_alias_t *write_one_alias) |
| 526 | { |
| 527 | unsigned long pos; |
| 528 | struct page *page = block->page; |
| 529 | u64 ino , bix; |
| 530 | __be64 *child, val; |
| 531 | level_t level; |
| 532 | int err; |
| 533 | |
| 534 | for (pos = 0; ; pos++) { |
| 535 | pos = fnb(block->alias_map, LOGFS_BLOCK_FACTOR, pos); |
| 536 | if (pos >= LOGFS_BLOCK_FACTOR) |
| 537 | return 0; |
| 538 | |
| 539 | ino = page->mapping->host->i_ino; |
| 540 | logfs_unpack_index(page->index, &bix, &level); |
| 541 | child = kmap_atomic(page, KM_USER0); |
| 542 | val = child[pos]; |
| 543 | kunmap_atomic(child, KM_USER0); |
| 544 | err = write_one_alias(sb, ino, bix, level, pos, val); |
| 545 | if (err) |
| 546 | return err; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | int logfs_write_obj_aliases_pagecache(struct super_block *sb) |
| 551 | { |
| 552 | struct logfs_super *super = logfs_super(sb); |
| 553 | struct logfs_block *block; |
| 554 | int err; |
| 555 | |
| 556 | list_for_each_entry(block, &super->s_object_alias, alias_list) { |
| 557 | err = block->ops->write_alias(sb, block, write_alias_journal); |
| 558 | if (err) |
| 559 | return err; |
| 560 | } |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | void __free_block(struct super_block *sb, struct logfs_block *block) |
| 565 | { |
| 566 | BUG_ON(!list_empty(&block->item_list)); |
| 567 | list_del(&block->alias_list); |
| 568 | mempool_free(block, logfs_super(sb)->s_block_pool); |
| 569 | } |
| 570 | |
| 571 | static void inode_free_block(struct super_block *sb, struct logfs_block *block) |
| 572 | { |
| 573 | struct inode *inode = block->inode; |
| 574 | |
| 575 | logfs_inode(inode)->li_block = NULL; |
| 576 | __free_block(sb, block); |
| 577 | } |
| 578 | |
| 579 | static void indirect_free_block(struct super_block *sb, |
| 580 | struct logfs_block *block) |
| 581 | { |
| 582 | ClearPagePrivate(block->page); |
| 583 | block->page->private = 0; |
| 584 | __free_block(sb, block); |
| 585 | } |
| 586 | |
| 587 | |
| 588 | static struct logfs_block_ops inode_block_ops = { |
| 589 | .write_block = inode_write_block, |
| 590 | .block_level = inode_block_level, |
| 591 | .free_block = inode_free_block, |
| 592 | .write_alias = inode_write_alias, |
| 593 | }; |
| 594 | |
| 595 | struct logfs_block_ops indirect_block_ops = { |
| 596 | .write_block = indirect_write_block, |
| 597 | .block_level = indirect_block_level, |
| 598 | .free_block = indirect_free_block, |
| 599 | .write_alias = indirect_write_alias, |
| 600 | }; |
| 601 | |
| 602 | struct logfs_block *__alloc_block(struct super_block *sb, |
| 603 | u64 ino, u64 bix, level_t level) |
| 604 | { |
| 605 | struct logfs_super *super = logfs_super(sb); |
| 606 | struct logfs_block *block; |
| 607 | |
| 608 | block = mempool_alloc(super->s_block_pool, GFP_NOFS); |
| 609 | memset(block, 0, sizeof(*block)); |
| 610 | INIT_LIST_HEAD(&block->alias_list); |
| 611 | INIT_LIST_HEAD(&block->item_list); |
| 612 | block->sb = sb; |
| 613 | block->ino = ino; |
| 614 | block->bix = bix; |
| 615 | block->level = level; |
| 616 | return block; |
| 617 | } |
| 618 | |
| 619 | static void alloc_inode_block(struct inode *inode) |
| 620 | { |
| 621 | struct logfs_inode *li = logfs_inode(inode); |
| 622 | struct logfs_block *block; |
| 623 | |
| 624 | if (li->li_block) |
| 625 | return; |
| 626 | |
| 627 | block = __alloc_block(inode->i_sb, LOGFS_INO_MASTER, inode->i_ino, 0); |
| 628 | block->inode = inode; |
| 629 | li->li_block = block; |
| 630 | block->ops = &inode_block_ops; |
| 631 | } |
| 632 | |
| 633 | void initialize_block_counters(struct page *page, struct logfs_block *block, |
| 634 | __be64 *array, int page_is_empty) |
| 635 | { |
| 636 | u64 ptr; |
| 637 | int i, start; |
| 638 | |
| 639 | block->partial = 0; |
| 640 | block->full = 0; |
| 641 | start = 0; |
| 642 | if (page->index < first_indirect_block()) { |
| 643 | /* Counters are pointless on level 0 */ |
| 644 | return; |
| 645 | } |
| 646 | if (page->index == first_indirect_block()) { |
| 647 | /* Skip unused pointers */ |
| 648 | start = I0_BLOCKS; |
| 649 | block->full = I0_BLOCKS; |
| 650 | } |
| 651 | if (!page_is_empty) { |
| 652 | for (i = start; i < LOGFS_BLOCK_FACTOR; i++) { |
| 653 | ptr = be64_to_cpu(array[i]); |
| 654 | if (ptr) |
| 655 | block->partial++; |
| 656 | if (ptr & LOGFS_FULLY_POPULATED) |
| 657 | block->full++; |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | static void alloc_data_block(struct inode *inode, struct page *page) |
| 663 | { |
| 664 | struct logfs_block *block; |
| 665 | u64 bix; |
| 666 | level_t level; |
| 667 | |
| 668 | if (PagePrivate(page)) |
| 669 | return; |
| 670 | |
| 671 | logfs_unpack_index(page->index, &bix, &level); |
| 672 | block = __alloc_block(inode->i_sb, inode->i_ino, bix, level); |
| 673 | block->page = page; |
| 674 | SetPagePrivate(page); |
| 675 | page->private = (unsigned long)block; |
| 676 | block->ops = &indirect_block_ops; |
| 677 | } |
| 678 | |
| 679 | static void alloc_indirect_block(struct inode *inode, struct page *page, |
| 680 | int page_is_empty) |
| 681 | { |
| 682 | struct logfs_block *block; |
| 683 | __be64 *array; |
| 684 | |
| 685 | if (PagePrivate(page)) |
| 686 | return; |
| 687 | |
| 688 | alloc_data_block(inode, page); |
| 689 | |
| 690 | block = logfs_block(page); |
| 691 | array = kmap_atomic(page, KM_USER0); |
| 692 | initialize_block_counters(page, block, array, page_is_empty); |
| 693 | kunmap_atomic(array, KM_USER0); |
| 694 | } |
| 695 | |
| 696 | static void block_set_pointer(struct page *page, int index, u64 ptr) |
| 697 | { |
| 698 | struct logfs_block *block = logfs_block(page); |
| 699 | __be64 *array; |
| 700 | u64 oldptr; |
| 701 | |
| 702 | BUG_ON(!block); |
| 703 | array = kmap_atomic(page, KM_USER0); |
| 704 | oldptr = be64_to_cpu(array[index]); |
| 705 | array[index] = cpu_to_be64(ptr); |
| 706 | kunmap_atomic(array, KM_USER0); |
| 707 | SetPageUptodate(page); |
| 708 | |
| 709 | block->full += !!(ptr & LOGFS_FULLY_POPULATED) |
| 710 | - !!(oldptr & LOGFS_FULLY_POPULATED); |
| 711 | block->partial += !!ptr - !!oldptr; |
| 712 | } |
| 713 | |
| 714 | static u64 block_get_pointer(struct page *page, int index) |
| 715 | { |
| 716 | __be64 *block; |
| 717 | u64 ptr; |
| 718 | |
| 719 | block = kmap_atomic(page, KM_USER0); |
| 720 | ptr = be64_to_cpu(block[index]); |
| 721 | kunmap_atomic(block, KM_USER0); |
| 722 | return ptr; |
| 723 | } |
| 724 | |
| 725 | static int logfs_read_empty(struct page *page) |
| 726 | { |
| 727 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | static int logfs_read_direct(struct inode *inode, struct page *page) |
| 732 | { |
| 733 | struct logfs_inode *li = logfs_inode(inode); |
| 734 | pgoff_t index = page->index; |
| 735 | u64 block; |
| 736 | |
| 737 | block = li->li_data[index]; |
| 738 | if (!block) |
| 739 | return logfs_read_empty(page); |
| 740 | |
| 741 | return logfs_segment_read(inode, page, block, index, 0); |
| 742 | } |
| 743 | |
| 744 | static int logfs_read_loop(struct inode *inode, struct page *page, |
| 745 | int rw_context) |
| 746 | { |
| 747 | struct logfs_inode *li = logfs_inode(inode); |
| 748 | u64 bix, bofs = li->li_data[INDIRECT_INDEX]; |
| 749 | level_t level, target_level; |
| 750 | int ret; |
| 751 | struct page *ipage; |
| 752 | |
| 753 | logfs_unpack_index(page->index, &bix, &target_level); |
| 754 | if (!bofs) |
| 755 | return logfs_read_empty(page); |
| 756 | |
| 757 | if (bix >= maxbix(li->li_height)) |
| 758 | return logfs_read_empty(page); |
| 759 | |
| 760 | for (level = LEVEL(li->li_height); |
| 761 | (__force u8)level > (__force u8)target_level; |
| 762 | level = SUBLEVEL(level)){ |
| 763 | ipage = logfs_get_page(inode, bix, level, rw_context); |
| 764 | if (!ipage) |
| 765 | return -ENOMEM; |
| 766 | |
| 767 | ret = logfs_segment_read(inode, ipage, bofs, bix, level); |
| 768 | if (ret) { |
| 769 | logfs_put_read_page(ipage); |
| 770 | return ret; |
| 771 | } |
| 772 | |
| 773 | bofs = block_get_pointer(ipage, get_bits(bix, SUBLEVEL(level))); |
| 774 | logfs_put_page(ipage, rw_context); |
| 775 | if (!bofs) |
| 776 | return logfs_read_empty(page); |
| 777 | } |
| 778 | |
| 779 | return logfs_segment_read(inode, page, bofs, bix, 0); |
| 780 | } |
| 781 | |
| 782 | static int logfs_read_block(struct inode *inode, struct page *page, |
| 783 | int rw_context) |
| 784 | { |
| 785 | pgoff_t index = page->index; |
| 786 | |
| 787 | if (index < I0_BLOCKS) |
| 788 | return logfs_read_direct(inode, page); |
| 789 | return logfs_read_loop(inode, page, rw_context); |
| 790 | } |
| 791 | |
| 792 | static int logfs_exist_loop(struct inode *inode, u64 bix) |
| 793 | { |
| 794 | struct logfs_inode *li = logfs_inode(inode); |
| 795 | u64 bofs = li->li_data[INDIRECT_INDEX]; |
| 796 | level_t level; |
| 797 | int ret; |
| 798 | struct page *ipage; |
| 799 | |
| 800 | if (!bofs) |
| 801 | return 0; |
| 802 | if (bix >= maxbix(li->li_height)) |
| 803 | return 0; |
| 804 | |
| 805 | for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)) { |
| 806 | ipage = logfs_get_read_page(inode, bix, level); |
| 807 | if (!ipage) |
| 808 | return -ENOMEM; |
| 809 | |
| 810 | ret = logfs_segment_read(inode, ipage, bofs, bix, level); |
| 811 | if (ret) { |
| 812 | logfs_put_read_page(ipage); |
| 813 | return ret; |
| 814 | } |
| 815 | |
| 816 | bofs = block_get_pointer(ipage, get_bits(bix, SUBLEVEL(level))); |
| 817 | logfs_put_read_page(ipage); |
| 818 | if (!bofs) |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | return 1; |
| 823 | } |
| 824 | |
| 825 | int logfs_exist_block(struct inode *inode, u64 bix) |
| 826 | { |
| 827 | struct logfs_inode *li = logfs_inode(inode); |
| 828 | |
| 829 | if (bix < I0_BLOCKS) |
| 830 | return !!li->li_data[bix]; |
| 831 | return logfs_exist_loop(inode, bix); |
| 832 | } |
| 833 | |
| 834 | static u64 seek_holedata_direct(struct inode *inode, u64 bix, int data) |
| 835 | { |
| 836 | struct logfs_inode *li = logfs_inode(inode); |
| 837 | |
| 838 | for (; bix < I0_BLOCKS; bix++) |
| 839 | if (data ^ (li->li_data[bix] == 0)) |
| 840 | return bix; |
| 841 | return I0_BLOCKS; |
| 842 | } |
| 843 | |
| 844 | static u64 seek_holedata_loop(struct inode *inode, u64 bix, int data) |
| 845 | { |
| 846 | struct logfs_inode *li = logfs_inode(inode); |
| 847 | __be64 *rblock; |
| 848 | u64 increment, bofs = li->li_data[INDIRECT_INDEX]; |
| 849 | level_t level; |
| 850 | int ret, slot; |
| 851 | struct page *page; |
| 852 | |
| 853 | BUG_ON(!bofs); |
| 854 | |
| 855 | for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)) { |
| 856 | increment = 1 << (LOGFS_BLOCK_BITS * ((__force u8)level-1)); |
| 857 | page = logfs_get_read_page(inode, bix, level); |
| 858 | if (!page) |
| 859 | return bix; |
| 860 | |
| 861 | ret = logfs_segment_read(inode, page, bofs, bix, level); |
| 862 | if (ret) { |
| 863 | logfs_put_read_page(page); |
| 864 | return bix; |
| 865 | } |
| 866 | |
| 867 | slot = get_bits(bix, SUBLEVEL(level)); |
| 868 | rblock = kmap_atomic(page, KM_USER0); |
| 869 | while (slot < LOGFS_BLOCK_FACTOR) { |
| 870 | if (data && (rblock[slot] != 0)) |
| 871 | break; |
| 872 | if (!data && !(be64_to_cpu(rblock[slot]) & LOGFS_FULLY_POPULATED)) |
| 873 | break; |
| 874 | slot++; |
| 875 | bix += increment; |
| 876 | bix &= ~(increment - 1); |
| 877 | } |
| 878 | if (slot >= LOGFS_BLOCK_FACTOR) { |
| 879 | kunmap_atomic(rblock, KM_USER0); |
| 880 | logfs_put_read_page(page); |
| 881 | return bix; |
| 882 | } |
| 883 | bofs = be64_to_cpu(rblock[slot]); |
| 884 | kunmap_atomic(rblock, KM_USER0); |
| 885 | logfs_put_read_page(page); |
| 886 | if (!bofs) { |
| 887 | BUG_ON(data); |
| 888 | return bix; |
| 889 | } |
| 890 | } |
| 891 | return bix; |
| 892 | } |
| 893 | |
| 894 | /** |
| 895 | * logfs_seek_hole - find next hole starting at a given block index |
| 896 | * @inode: inode to search in |
| 897 | * @bix: block index to start searching |
| 898 | * |
| 899 | * Returns next hole. If the file doesn't contain any further holes, the |
| 900 | * block address next to eof is returned instead. |
| 901 | */ |
| 902 | u64 logfs_seek_hole(struct inode *inode, u64 bix) |
| 903 | { |
| 904 | struct logfs_inode *li = logfs_inode(inode); |
| 905 | |
| 906 | if (bix < I0_BLOCKS) { |
| 907 | bix = seek_holedata_direct(inode, bix, 0); |
| 908 | if (bix < I0_BLOCKS) |
| 909 | return bix; |
| 910 | } |
| 911 | |
| 912 | if (!li->li_data[INDIRECT_INDEX]) |
| 913 | return bix; |
| 914 | else if (li->li_data[INDIRECT_INDEX] & LOGFS_FULLY_POPULATED) |
| 915 | bix = maxbix(li->li_height); |
| 916 | else { |
| 917 | bix = seek_holedata_loop(inode, bix, 0); |
| 918 | if (bix < maxbix(li->li_height)) |
| 919 | return bix; |
| 920 | /* Should not happen anymore. But if some port writes semi- |
| 921 | * corrupt images (as this one used to) we might run into it. |
| 922 | */ |
| 923 | WARN_ON_ONCE(bix == maxbix(li->li_height)); |
| 924 | } |
| 925 | |
| 926 | return bix; |
| 927 | } |
| 928 | |
| 929 | static u64 __logfs_seek_data(struct inode *inode, u64 bix) |
| 930 | { |
| 931 | struct logfs_inode *li = logfs_inode(inode); |
| 932 | |
| 933 | if (bix < I0_BLOCKS) { |
| 934 | bix = seek_holedata_direct(inode, bix, 1); |
| 935 | if (bix < I0_BLOCKS) |
| 936 | return bix; |
| 937 | } |
| 938 | |
| 939 | if (bix < maxbix(li->li_height)) { |
| 940 | if (!li->li_data[INDIRECT_INDEX]) |
| 941 | bix = maxbix(li->li_height); |
| 942 | else |
| 943 | return seek_holedata_loop(inode, bix, 1); |
| 944 | } |
| 945 | |
| 946 | return bix; |
| 947 | } |
| 948 | |
| 949 | /** |
| 950 | * logfs_seek_data - find next data block after a given block index |
| 951 | * @inode: inode to search in |
| 952 | * @bix: block index to start searching |
| 953 | * |
| 954 | * Returns next data block. If the file doesn't contain any further data |
| 955 | * blocks, the last block in the file is returned instead. |
| 956 | */ |
| 957 | u64 logfs_seek_data(struct inode *inode, u64 bix) |
| 958 | { |
| 959 | struct super_block *sb = inode->i_sb; |
| 960 | u64 ret, end; |
| 961 | |
| 962 | ret = __logfs_seek_data(inode, bix); |
| 963 | end = i_size_read(inode) >> sb->s_blocksize_bits; |
| 964 | if (ret >= end) |
| 965 | ret = max(bix, end); |
| 966 | return ret; |
| 967 | } |
| 968 | |
| 969 | static int logfs_is_valid_direct(struct logfs_inode *li, u64 bix, u64 ofs) |
| 970 | { |
| 971 | return pure_ofs(li->li_data[bix]) == ofs; |
| 972 | } |
| 973 | |
| 974 | static int __logfs_is_valid_loop(struct inode *inode, u64 bix, |
| 975 | u64 ofs, u64 bofs) |
| 976 | { |
| 977 | struct logfs_inode *li = logfs_inode(inode); |
| 978 | level_t level; |
| 979 | int ret; |
| 980 | struct page *page; |
| 981 | |
| 982 | for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)){ |
| 983 | page = logfs_get_write_page(inode, bix, level); |
| 984 | BUG_ON(!page); |
| 985 | |
| 986 | ret = logfs_segment_read(inode, page, bofs, bix, level); |
| 987 | if (ret) { |
| 988 | logfs_put_write_page(page); |
| 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | bofs = block_get_pointer(page, get_bits(bix, SUBLEVEL(level))); |
| 993 | logfs_put_write_page(page); |
| 994 | if (!bofs) |
| 995 | return 0; |
| 996 | |
| 997 | if (pure_ofs(bofs) == ofs) |
| 998 | return 1; |
| 999 | } |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | static int logfs_is_valid_loop(struct inode *inode, u64 bix, u64 ofs) |
| 1004 | { |
| 1005 | struct logfs_inode *li = logfs_inode(inode); |
| 1006 | u64 bofs = li->li_data[INDIRECT_INDEX]; |
| 1007 | |
| 1008 | if (!bofs) |
| 1009 | return 0; |
| 1010 | |
| 1011 | if (bix >= maxbix(li->li_height)) |
| 1012 | return 0; |
| 1013 | |
| 1014 | if (pure_ofs(bofs) == ofs) |
| 1015 | return 1; |
| 1016 | |
| 1017 | return __logfs_is_valid_loop(inode, bix, ofs, bofs); |
| 1018 | } |
| 1019 | |
| 1020 | static int __logfs_is_valid_block(struct inode *inode, u64 bix, u64 ofs) |
| 1021 | { |
| 1022 | struct logfs_inode *li = logfs_inode(inode); |
| 1023 | |
| 1024 | if ((inode->i_nlink == 0) && atomic_read(&inode->i_count) == 1) |
| 1025 | return 0; |
| 1026 | |
| 1027 | if (bix < I0_BLOCKS) |
| 1028 | return logfs_is_valid_direct(li, bix, ofs); |
| 1029 | return logfs_is_valid_loop(inode, bix, ofs); |
| 1030 | } |
| 1031 | |
| 1032 | /** |
| 1033 | * logfs_is_valid_block - check whether this block is still valid |
| 1034 | * |
| 1035 | * @sb - superblock |
| 1036 | * @ofs - block physical offset |
| 1037 | * @ino - block inode number |
| 1038 | * @bix - block index |
| 1039 | * @level - block level |
| 1040 | * |
| 1041 | * Returns 0 if the block is invalid, 1 if it is valid and 2 if it will |
| 1042 | * become invalid once the journal is written. |
| 1043 | */ |
| 1044 | int logfs_is_valid_block(struct super_block *sb, u64 ofs, u64 ino, u64 bix, |
| 1045 | gc_level_t gc_level) |
| 1046 | { |
| 1047 | struct logfs_super *super = logfs_super(sb); |
| 1048 | struct inode *inode; |
| 1049 | int ret, cookie; |
| 1050 | |
| 1051 | /* Umount closes a segment with free blocks remaining. Those |
| 1052 | * blocks are by definition invalid. */ |
| 1053 | if (ino == -1) |
| 1054 | return 0; |
| 1055 | |
| 1056 | LOGFS_BUG_ON((u64)(u_long)ino != ino, sb); |
| 1057 | |
| 1058 | inode = logfs_safe_iget(sb, ino, &cookie); |
| 1059 | if (IS_ERR(inode)) |
| 1060 | goto invalid; |
| 1061 | |
| 1062 | ret = __logfs_is_valid_block(inode, bix, ofs); |
| 1063 | logfs_safe_iput(inode, cookie); |
| 1064 | if (ret) |
| 1065 | return ret; |
| 1066 | |
| 1067 | invalid: |
| 1068 | /* Block is nominally invalid, but may still sit in the shadow tree, |
| 1069 | * waiting for a journal commit. |
| 1070 | */ |
| 1071 | if (btree_lookup64(&super->s_shadow_tree.old, ofs)) |
| 1072 | return 2; |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
| 1076 | int logfs_readpage_nolock(struct page *page) |
| 1077 | { |
| 1078 | struct inode *inode = page->mapping->host; |
| 1079 | int ret = -EIO; |
| 1080 | |
| 1081 | ret = logfs_read_block(inode, page, READ); |
| 1082 | |
| 1083 | if (ret) { |
| 1084 | ClearPageUptodate(page); |
| 1085 | SetPageError(page); |
| 1086 | } else { |
| 1087 | SetPageUptodate(page); |
| 1088 | ClearPageError(page); |
| 1089 | } |
| 1090 | flush_dcache_page(page); |
| 1091 | |
| 1092 | return ret; |
| 1093 | } |
| 1094 | |
| 1095 | static int logfs_reserve_bytes(struct inode *inode, int bytes) |
| 1096 | { |
| 1097 | struct logfs_super *super = logfs_super(inode->i_sb); |
| 1098 | u64 available = super->s_free_bytes + super->s_dirty_free_bytes |
| 1099 | - super->s_dirty_used_bytes - super->s_dirty_pages; |
| 1100 | |
| 1101 | if (!bytes) |
| 1102 | return 0; |
| 1103 | |
| 1104 | if (available < bytes) |
| 1105 | return -ENOSPC; |
| 1106 | |
| 1107 | if (available < bytes + super->s_root_reserve && |
| 1108 | !capable(CAP_SYS_RESOURCE)) |
| 1109 | return -ENOSPC; |
| 1110 | |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | int get_page_reserve(struct inode *inode, struct page *page) |
| 1115 | { |
| 1116 | struct logfs_super *super = logfs_super(inode->i_sb); |
| 1117 | int ret; |
| 1118 | |
| 1119 | if (logfs_block(page) && logfs_block(page)->reserved_bytes) |
| 1120 | return 0; |
| 1121 | |
| 1122 | logfs_get_wblocks(inode->i_sb, page, WF_LOCK); |
| 1123 | ret = logfs_reserve_bytes(inode, 6 * LOGFS_MAX_OBJECTSIZE); |
| 1124 | if (!ret) { |
| 1125 | alloc_data_block(inode, page); |
| 1126 | logfs_block(page)->reserved_bytes += 6 * LOGFS_MAX_OBJECTSIZE; |
| 1127 | super->s_dirty_pages += 6 * LOGFS_MAX_OBJECTSIZE; |
| 1128 | } |
| 1129 | logfs_put_wblocks(inode->i_sb, page, WF_LOCK); |
| 1130 | return ret; |
| 1131 | } |
| 1132 | |
| 1133 | /* |
| 1134 | * We are protected by write lock. Push victims up to superblock level |
| 1135 | * and release transaction when appropriate. |
| 1136 | */ |
| 1137 | /* FIXME: This is currently called from the wrong spots. */ |
| 1138 | static void logfs_handle_transaction(struct inode *inode, |
| 1139 | struct logfs_transaction *ta) |
| 1140 | { |
| 1141 | struct logfs_super *super = logfs_super(inode->i_sb); |
| 1142 | |
| 1143 | if (!ta) |
| 1144 | return; |
| 1145 | logfs_inode(inode)->li_block->ta = NULL; |
| 1146 | |
| 1147 | if (inode->i_ino != LOGFS_INO_MASTER) { |
| 1148 | BUG(); /* FIXME: Yes, this needs more thought */ |
| 1149 | /* just remember the transaction until inode is written */ |
| 1150 | //BUG_ON(logfs_inode(inode)->li_transaction); |
| 1151 | //logfs_inode(inode)->li_transaction = ta; |
| 1152 | return; |
| 1153 | } |
| 1154 | |
| 1155 | switch (ta->state) { |
| 1156 | case CREATE_1: /* fall through */ |
| 1157 | case UNLINK_1: |
| 1158 | BUG_ON(super->s_victim_ino); |
| 1159 | super->s_victim_ino = ta->ino; |
| 1160 | break; |
| 1161 | case CREATE_2: /* fall through */ |
| 1162 | case UNLINK_2: |
| 1163 | BUG_ON(super->s_victim_ino != ta->ino); |
| 1164 | super->s_victim_ino = 0; |
| 1165 | /* transaction ends here - free it */ |
| 1166 | kfree(ta); |
| 1167 | break; |
| 1168 | case CROSS_RENAME_1: |
| 1169 | BUG_ON(super->s_rename_dir); |
| 1170 | BUG_ON(super->s_rename_pos); |
| 1171 | super->s_rename_dir = ta->dir; |
| 1172 | super->s_rename_pos = ta->pos; |
| 1173 | break; |
| 1174 | case CROSS_RENAME_2: |
| 1175 | BUG_ON(super->s_rename_dir != ta->dir); |
| 1176 | BUG_ON(super->s_rename_pos != ta->pos); |
| 1177 | super->s_rename_dir = 0; |
| 1178 | super->s_rename_pos = 0; |
| 1179 | kfree(ta); |
| 1180 | break; |
| 1181 | case TARGET_RENAME_1: |
| 1182 | BUG_ON(super->s_rename_dir); |
| 1183 | BUG_ON(super->s_rename_pos); |
| 1184 | BUG_ON(super->s_victim_ino); |
| 1185 | super->s_rename_dir = ta->dir; |
| 1186 | super->s_rename_pos = ta->pos; |
| 1187 | super->s_victim_ino = ta->ino; |
| 1188 | break; |
| 1189 | case TARGET_RENAME_2: |
| 1190 | BUG_ON(super->s_rename_dir != ta->dir); |
| 1191 | BUG_ON(super->s_rename_pos != ta->pos); |
| 1192 | BUG_ON(super->s_victim_ino != ta->ino); |
| 1193 | super->s_rename_dir = 0; |
| 1194 | super->s_rename_pos = 0; |
| 1195 | break; |
| 1196 | case TARGET_RENAME_3: |
| 1197 | BUG_ON(super->s_rename_dir); |
| 1198 | BUG_ON(super->s_rename_pos); |
| 1199 | BUG_ON(super->s_victim_ino != ta->ino); |
| 1200 | super->s_victim_ino = 0; |
| 1201 | kfree(ta); |
| 1202 | break; |
| 1203 | default: |
| 1204 | BUG(); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | /* |
| 1209 | * Not strictly a reservation, but rather a check that we still have enough |
| 1210 | * space to satisfy the write. |
| 1211 | */ |
| 1212 | static int logfs_reserve_blocks(struct inode *inode, int blocks) |
| 1213 | { |
| 1214 | return logfs_reserve_bytes(inode, blocks * LOGFS_MAX_OBJECTSIZE); |
| 1215 | } |
| 1216 | |
| 1217 | struct write_control { |
| 1218 | u64 ofs; |
| 1219 | long flags; |
| 1220 | }; |
| 1221 | |
| 1222 | static struct logfs_shadow *alloc_shadow(struct inode *inode, u64 bix, |
| 1223 | level_t level, u64 old_ofs) |
| 1224 | { |
| 1225 | struct logfs_super *super = logfs_super(inode->i_sb); |
| 1226 | struct logfs_shadow *shadow; |
| 1227 | |
| 1228 | shadow = mempool_alloc(super->s_shadow_pool, GFP_NOFS); |
| 1229 | memset(shadow, 0, sizeof(*shadow)); |
| 1230 | shadow->ino = inode->i_ino; |
| 1231 | shadow->bix = bix; |
| 1232 | shadow->gc_level = expand_level(inode->i_ino, level); |
| 1233 | shadow->old_ofs = old_ofs & ~LOGFS_FULLY_POPULATED; |
| 1234 | return shadow; |
| 1235 | } |
| 1236 | |
| 1237 | static void free_shadow(struct inode *inode, struct logfs_shadow *shadow) |
| 1238 | { |
| 1239 | struct logfs_super *super = logfs_super(inode->i_sb); |
| 1240 | |
| 1241 | mempool_free(shadow, super->s_shadow_pool); |
| 1242 | } |
| 1243 | |
| 1244 | /** |
| 1245 | * fill_shadow_tree - Propagate shadow tree changes due to a write |
| 1246 | * @inode: Inode owning the page |
| 1247 | * @page: Struct page that was written |
| 1248 | * @shadow: Shadow for the current write |
| 1249 | * |
| 1250 | * Writes in logfs can result in two semi-valid objects. The old object |
| 1251 | * is still valid as long as it can be reached by following pointers on |
| 1252 | * the medium. Only when writes propagate all the way up to the journal |
| 1253 | * has the new object safely replaced the old one. |
| 1254 | * |
| 1255 | * To handle this problem, a struct logfs_shadow is used to represent |
| 1256 | * every single write. It is attached to the indirect block, which is |
| 1257 | * marked dirty. When the indirect block is written, its shadows are |
| 1258 | * handed up to the next indirect block (or inode). Untimately they |
| 1259 | * will reach the master inode and be freed upon journal commit. |
| 1260 | * |
| 1261 | * This function handles a single step in the propagation. It adds the |
| 1262 | * shadow for the current write to the tree, along with any shadows in |
| 1263 | * the page's tree, in case it was an indirect block. If a page is |
| 1264 | * written, the inode parameter is left NULL, if an inode is written, |
| 1265 | * the page parameter is left NULL. |
| 1266 | */ |
| 1267 | static void fill_shadow_tree(struct inode *inode, struct page *page, |
| 1268 | struct logfs_shadow *shadow) |
| 1269 | { |
| 1270 | struct logfs_super *super = logfs_super(inode->i_sb); |
| 1271 | struct logfs_block *block = logfs_block(page); |
| 1272 | struct shadow_tree *tree = &super->s_shadow_tree; |
| 1273 | |
| 1274 | if (PagePrivate(page)) { |
| 1275 | if (block->alias_map) |
| 1276 | super->s_no_object_aliases -= bitmap_weight( |
| 1277 | block->alias_map, LOGFS_BLOCK_FACTOR); |
| 1278 | logfs_handle_transaction(inode, block->ta); |
| 1279 | block->ops->free_block(inode->i_sb, block); |
| 1280 | } |
| 1281 | if (shadow) { |
| 1282 | if (shadow->old_ofs) |
| 1283 | btree_insert64(&tree->old, shadow->old_ofs, shadow, |
| 1284 | GFP_NOFS); |
| 1285 | else |
| 1286 | btree_insert64(&tree->new, shadow->new_ofs, shadow, |
| 1287 | GFP_NOFS); |
| 1288 | |
| 1289 | super->s_dirty_used_bytes += shadow->new_len; |
| 1290 | super->s_dirty_free_bytes += shadow->old_len; |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | static void logfs_set_alias(struct super_block *sb, struct logfs_block *block, |
| 1295 | long child_no) |
| 1296 | { |
| 1297 | struct logfs_super *super = logfs_super(sb); |
| 1298 | |
| 1299 | if (block->inode && block->inode->i_ino == LOGFS_INO_MASTER) { |
| 1300 | /* Aliases in the master inode are pointless. */ |
| 1301 | return; |
| 1302 | } |
| 1303 | |
| 1304 | if (!test_bit(child_no, block->alias_map)) { |
| 1305 | set_bit(child_no, block->alias_map); |
| 1306 | super->s_no_object_aliases++; |
| 1307 | } |
| 1308 | list_move_tail(&block->alias_list, &super->s_object_alias); |
| 1309 | } |
| 1310 | |
| 1311 | /* |
| 1312 | * Object aliases can and often do change the size and occupied space of a |
| 1313 | * file. So not only do we have to change the pointers, we also have to |
| 1314 | * change inode->i_size and li->li_used_bytes. Which is done by setting |
| 1315 | * another two object aliases for the inode itself. |
| 1316 | */ |
| 1317 | static void set_iused(struct inode *inode, struct logfs_shadow *shadow) |
| 1318 | { |
| 1319 | struct logfs_inode *li = logfs_inode(inode); |
| 1320 | |
| 1321 | if (shadow->new_len == shadow->old_len) |
| 1322 | return; |
| 1323 | |
| 1324 | alloc_inode_block(inode); |
| 1325 | li->li_used_bytes += shadow->new_len - shadow->old_len; |
| 1326 | __logfs_set_blocks(inode); |
| 1327 | logfs_set_alias(inode->i_sb, li->li_block, INODE_USED_OFS); |
| 1328 | logfs_set_alias(inode->i_sb, li->li_block, INODE_SIZE_OFS); |
| 1329 | } |
| 1330 | |
| 1331 | static int logfs_write_i0(struct inode *inode, struct page *page, |
| 1332 | struct write_control *wc) |
| 1333 | { |
| 1334 | struct logfs_shadow *shadow; |
| 1335 | u64 bix; |
| 1336 | level_t level; |
| 1337 | int full, err = 0; |
| 1338 | |
| 1339 | logfs_unpack_index(page->index, &bix, &level); |
| 1340 | if (wc->ofs == 0) |
| 1341 | if (logfs_reserve_blocks(inode, 1)) |
| 1342 | return -ENOSPC; |
| 1343 | |
| 1344 | shadow = alloc_shadow(inode, bix, level, wc->ofs); |
| 1345 | if (wc->flags & WF_WRITE) |
| 1346 | err = logfs_segment_write(inode, page, shadow); |
| 1347 | if (wc->flags & WF_DELETE) |
| 1348 | logfs_segment_delete(inode, shadow); |
| 1349 | if (err) { |
| 1350 | free_shadow(inode, shadow); |
| 1351 | return err; |
| 1352 | } |
| 1353 | |
| 1354 | set_iused(inode, shadow); |
| 1355 | full = 1; |
| 1356 | if (level != 0) { |
| 1357 | alloc_indirect_block(inode, page, 0); |
| 1358 | full = logfs_block(page)->full == LOGFS_BLOCK_FACTOR; |
| 1359 | } |
| 1360 | fill_shadow_tree(inode, page, shadow); |
| 1361 | wc->ofs = shadow->new_ofs; |
| 1362 | if (wc->ofs && full) |
| 1363 | wc->ofs |= LOGFS_FULLY_POPULATED; |
| 1364 | return 0; |
| 1365 | } |
| 1366 | |
| 1367 | static int logfs_write_direct(struct inode *inode, struct page *page, |
| 1368 | long flags) |
| 1369 | { |
| 1370 | struct logfs_inode *li = logfs_inode(inode); |
| 1371 | struct write_control wc = { |
| 1372 | .ofs = li->li_data[page->index], |
| 1373 | .flags = flags, |
| 1374 | }; |
| 1375 | int err; |
| 1376 | |
| 1377 | alloc_inode_block(inode); |
| 1378 | |
| 1379 | err = logfs_write_i0(inode, page, &wc); |
| 1380 | if (err) |
| 1381 | return err; |
| 1382 | |
| 1383 | li->li_data[page->index] = wc.ofs; |
| 1384 | logfs_set_alias(inode->i_sb, li->li_block, |
| 1385 | page->index + INODE_POINTER_OFS); |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
| 1389 | static int ptr_change(u64 ofs, struct page *page) |
| 1390 | { |
| 1391 | struct logfs_block *block = logfs_block(page); |
| 1392 | int empty0, empty1, full0, full1; |
| 1393 | |
| 1394 | empty0 = ofs == 0; |
| 1395 | empty1 = block->partial == 0; |
| 1396 | if (empty0 != empty1) |
| 1397 | return 1; |
| 1398 | |
| 1399 | /* The !! is necessary to shrink result to int */ |
| 1400 | full0 = !!(ofs & LOGFS_FULLY_POPULATED); |
| 1401 | full1 = block->full == LOGFS_BLOCK_FACTOR; |
| 1402 | if (full0 != full1) |
| 1403 | return 1; |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
| 1407 | static int __logfs_write_rec(struct inode *inode, struct page *page, |
| 1408 | struct write_control *this_wc, |
| 1409 | pgoff_t bix, level_t target_level, level_t level) |
| 1410 | { |
| 1411 | int ret, page_empty = 0; |
| 1412 | int child_no = get_bits(bix, SUBLEVEL(level)); |
| 1413 | struct page *ipage; |
| 1414 | struct write_control child_wc = { |
| 1415 | .flags = this_wc->flags, |
| 1416 | }; |
| 1417 | |
| 1418 | ipage = logfs_get_write_page(inode, bix, level); |
| 1419 | if (!ipage) |
| 1420 | return -ENOMEM; |
| 1421 | |
| 1422 | if (this_wc->ofs) { |
| 1423 | ret = logfs_segment_read(inode, ipage, this_wc->ofs, bix, level); |
| 1424 | if (ret) |
| 1425 | goto out; |
| 1426 | } else if (!PageUptodate(ipage)) { |
| 1427 | page_empty = 1; |
| 1428 | logfs_read_empty(ipage); |
| 1429 | } |
| 1430 | |
| 1431 | child_wc.ofs = block_get_pointer(ipage, child_no); |
| 1432 | |
| 1433 | if ((__force u8)level-1 > (__force u8)target_level) |
| 1434 | ret = __logfs_write_rec(inode, page, &child_wc, bix, |
| 1435 | target_level, SUBLEVEL(level)); |
| 1436 | else |
| 1437 | ret = logfs_write_i0(inode, page, &child_wc); |
| 1438 | |
| 1439 | if (ret) |
| 1440 | goto out; |
| 1441 | |
| 1442 | alloc_indirect_block(inode, ipage, page_empty); |
| 1443 | block_set_pointer(ipage, child_no, child_wc.ofs); |
| 1444 | /* FIXME: first condition seems superfluous */ |
| 1445 | if (child_wc.ofs || logfs_block(ipage)->partial) |
| 1446 | this_wc->flags |= WF_WRITE; |
| 1447 | /* the condition on this_wc->ofs ensures that we won't consume extra |
| 1448 | * space for indirect blocks in the future, which we cannot reserve */ |
| 1449 | if (!this_wc->ofs || ptr_change(this_wc->ofs, ipage)) |
| 1450 | ret = logfs_write_i0(inode, ipage, this_wc); |
| 1451 | else |
| 1452 | logfs_set_alias(inode->i_sb, logfs_block(ipage), child_no); |
| 1453 | out: |
| 1454 | logfs_put_write_page(ipage); |
| 1455 | return ret; |
| 1456 | } |
| 1457 | |
| 1458 | static int logfs_write_rec(struct inode *inode, struct page *page, |
| 1459 | pgoff_t bix, level_t target_level, long flags) |
| 1460 | { |
| 1461 | struct logfs_inode *li = logfs_inode(inode); |
| 1462 | struct write_control wc = { |
| 1463 | .ofs = li->li_data[INDIRECT_INDEX], |
| 1464 | .flags = flags, |
| 1465 | }; |
| 1466 | int ret; |
| 1467 | |
| 1468 | alloc_inode_block(inode); |
| 1469 | |
| 1470 | if (li->li_height > (__force u8)target_level) |
| 1471 | ret = __logfs_write_rec(inode, page, &wc, bix, target_level, |
| 1472 | LEVEL(li->li_height)); |
| 1473 | else |
| 1474 | ret = logfs_write_i0(inode, page, &wc); |
| 1475 | if (ret) |
| 1476 | return ret; |
| 1477 | |
| 1478 | if (li->li_data[INDIRECT_INDEX] != wc.ofs) { |
| 1479 | li->li_data[INDIRECT_INDEX] = wc.ofs; |
| 1480 | logfs_set_alias(inode->i_sb, li->li_block, |
| 1481 | INDIRECT_INDEX + INODE_POINTER_OFS); |
| 1482 | } |
| 1483 | return ret; |
| 1484 | } |
| 1485 | |
| 1486 | void logfs_add_transaction(struct inode *inode, struct logfs_transaction *ta) |
| 1487 | { |
| 1488 | alloc_inode_block(inode); |
| 1489 | logfs_inode(inode)->li_block->ta = ta; |
| 1490 | } |
| 1491 | |
| 1492 | void logfs_del_transaction(struct inode *inode, struct logfs_transaction *ta) |
| 1493 | { |
| 1494 | struct logfs_block *block = logfs_inode(inode)->li_block; |
| 1495 | |
| 1496 | if (block && block->ta) |
| 1497 | block->ta = NULL; |
| 1498 | } |
| 1499 | |
| 1500 | static int grow_inode(struct inode *inode, u64 bix, level_t level) |
| 1501 | { |
| 1502 | struct logfs_inode *li = logfs_inode(inode); |
| 1503 | u8 height = (__force u8)level; |
| 1504 | struct page *page; |
| 1505 | struct write_control wc = { |
| 1506 | .flags = WF_WRITE, |
| 1507 | }; |
| 1508 | int err; |
| 1509 | |
| 1510 | BUG_ON(height > 5 || li->li_height > 5); |
| 1511 | while (height > li->li_height || bix >= maxbix(li->li_height)) { |
| 1512 | page = logfs_get_write_page(inode, I0_BLOCKS + 1, |
| 1513 | LEVEL(li->li_height + 1)); |
| 1514 | if (!page) |
| 1515 | return -ENOMEM; |
| 1516 | logfs_read_empty(page); |
| 1517 | alloc_indirect_block(inode, page, 1); |
| 1518 | block_set_pointer(page, 0, li->li_data[INDIRECT_INDEX]); |
| 1519 | err = logfs_write_i0(inode, page, &wc); |
| 1520 | logfs_put_write_page(page); |
| 1521 | if (err) |
| 1522 | return err; |
| 1523 | li->li_data[INDIRECT_INDEX] = wc.ofs; |
| 1524 | wc.ofs = 0; |
| 1525 | li->li_height++; |
| 1526 | logfs_set_alias(inode->i_sb, li->li_block, INODE_HEIGHT_OFS); |
| 1527 | } |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
| 1531 | static int __logfs_write_buf(struct inode *inode, struct page *page, long flags) |
| 1532 | { |
| 1533 | struct logfs_super *super = logfs_super(inode->i_sb); |
| 1534 | pgoff_t index = page->index; |
| 1535 | u64 bix; |
| 1536 | level_t level; |
| 1537 | int err; |
| 1538 | |
| 1539 | flags |= WF_WRITE | WF_DELETE; |
| 1540 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
| 1541 | |
| 1542 | logfs_unpack_index(index, &bix, &level); |
| 1543 | if (logfs_block(page) && logfs_block(page)->reserved_bytes) |
| 1544 | super->s_dirty_pages -= logfs_block(page)->reserved_bytes; |
| 1545 | |
| 1546 | if (index < I0_BLOCKS) |
| 1547 | return logfs_write_direct(inode, page, flags); |
| 1548 | |
| 1549 | bix = adjust_bix(bix, level); |
| 1550 | err = grow_inode(inode, bix, level); |
| 1551 | if (err) |
| 1552 | return err; |
| 1553 | return logfs_write_rec(inode, page, bix, level, flags); |
| 1554 | } |
| 1555 | |
| 1556 | int logfs_write_buf(struct inode *inode, struct page *page, long flags) |
| 1557 | { |
| 1558 | struct super_block *sb = inode->i_sb; |
| 1559 | int ret; |
| 1560 | |
| 1561 | logfs_get_wblocks(sb, page, flags & WF_LOCK); |
| 1562 | ret = __logfs_write_buf(inode, page, flags); |
| 1563 | logfs_put_wblocks(sb, page, flags & WF_LOCK); |
| 1564 | return ret; |
| 1565 | } |
| 1566 | |
| 1567 | static int __logfs_delete(struct inode *inode, struct page *page) |
| 1568 | { |
| 1569 | long flags = WF_DELETE; |
| 1570 | |
| 1571 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
| 1572 | |
| 1573 | if (page->index < I0_BLOCKS) |
| 1574 | return logfs_write_direct(inode, page, flags); |
| 1575 | return logfs_write_rec(inode, page, page->index, 0, flags); |
| 1576 | } |
| 1577 | |
| 1578 | int logfs_delete(struct inode *inode, pgoff_t index, |
| 1579 | struct shadow_tree *shadow_tree) |
| 1580 | { |
| 1581 | struct super_block *sb = inode->i_sb; |
| 1582 | struct page *page; |
| 1583 | int ret; |
| 1584 | |
| 1585 | page = logfs_get_read_page(inode, index, 0); |
| 1586 | if (!page) |
| 1587 | return -ENOMEM; |
| 1588 | |
| 1589 | logfs_get_wblocks(sb, page, 1); |
| 1590 | ret = __logfs_delete(inode, page); |
| 1591 | logfs_put_wblocks(sb, page, 1); |
| 1592 | |
| 1593 | logfs_put_read_page(page); |
| 1594 | |
| 1595 | return ret; |
| 1596 | } |
| 1597 | |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 1598 | int logfs_rewrite_block(struct inode *inode, u64 bix, u64 ofs, |
| 1599 | gc_level_t gc_level, long flags) |
| 1600 | { |
| 1601 | level_t level = shrink_level(gc_level); |
| 1602 | struct page *page; |
| 1603 | int err; |
| 1604 | |
| 1605 | page = logfs_get_write_page(inode, bix, level); |
| 1606 | if (!page) |
| 1607 | return -ENOMEM; |
| 1608 | |
| 1609 | err = logfs_segment_read(inode, page, ofs, bix, level); |
| 1610 | if (!err) { |
| 1611 | if (level != 0) |
| 1612 | alloc_indirect_block(inode, page, 0); |
| 1613 | err = logfs_write_buf(inode, page, flags); |
Joern Engel | 1932191 | 2010-03-27 09:56:58 +0100 | [diff] [blame] | 1614 | if (!err && shrink_level(gc_level) == 0) { |
| 1615 | /* Rewrite cannot mark the inode dirty but has to |
| 1616 | * write it immediatly. |
| 1617 | * Q: Can't we just create an alias for the inode |
| 1618 | * instead? And if not, why not? |
| 1619 | */ |
| 1620 | if (inode->i_ino == LOGFS_INO_MASTER) |
| 1621 | logfs_write_anchor(inode->i_sb); |
| 1622 | else { |
| 1623 | err = __logfs_write_inode(inode, flags); |
| 1624 | } |
| 1625 | } |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 1626 | } |
| 1627 | logfs_put_write_page(page); |
| 1628 | return err; |
| 1629 | } |
| 1630 | |
| 1631 | static int truncate_data_block(struct inode *inode, struct page *page, |
| 1632 | u64 ofs, struct logfs_shadow *shadow, u64 size) |
| 1633 | { |
| 1634 | loff_t pageofs = page->index << inode->i_sb->s_blocksize_bits; |
| 1635 | u64 bix; |
| 1636 | level_t level; |
| 1637 | int err; |
| 1638 | |
| 1639 | /* Does truncation happen within this page? */ |
| 1640 | if (size <= pageofs || size - pageofs >= PAGE_SIZE) |
| 1641 | return 0; |
| 1642 | |
| 1643 | logfs_unpack_index(page->index, &bix, &level); |
| 1644 | BUG_ON(level != 0); |
| 1645 | |
| 1646 | err = logfs_segment_read(inode, page, ofs, bix, level); |
| 1647 | if (err) |
| 1648 | return err; |
| 1649 | |
| 1650 | zero_user_segment(page, size - pageofs, PAGE_CACHE_SIZE); |
| 1651 | return logfs_segment_write(inode, page, shadow); |
| 1652 | } |
| 1653 | |
| 1654 | static int logfs_truncate_i0(struct inode *inode, struct page *page, |
| 1655 | struct write_control *wc, u64 size) |
| 1656 | { |
| 1657 | struct logfs_shadow *shadow; |
| 1658 | u64 bix; |
| 1659 | level_t level; |
| 1660 | int err = 0; |
| 1661 | |
| 1662 | logfs_unpack_index(page->index, &bix, &level); |
| 1663 | BUG_ON(level != 0); |
| 1664 | shadow = alloc_shadow(inode, bix, level, wc->ofs); |
| 1665 | |
| 1666 | err = truncate_data_block(inode, page, wc->ofs, shadow, size); |
| 1667 | if (err) { |
| 1668 | free_shadow(inode, shadow); |
| 1669 | return err; |
| 1670 | } |
| 1671 | |
| 1672 | logfs_segment_delete(inode, shadow); |
| 1673 | set_iused(inode, shadow); |
| 1674 | fill_shadow_tree(inode, page, shadow); |
| 1675 | wc->ofs = shadow->new_ofs; |
| 1676 | return 0; |
| 1677 | } |
| 1678 | |
| 1679 | static int logfs_truncate_direct(struct inode *inode, u64 size) |
| 1680 | { |
| 1681 | struct logfs_inode *li = logfs_inode(inode); |
| 1682 | struct write_control wc; |
| 1683 | struct page *page; |
| 1684 | int e; |
| 1685 | int err; |
| 1686 | |
| 1687 | alloc_inode_block(inode); |
| 1688 | |
| 1689 | for (e = I0_BLOCKS - 1; e >= 0; e--) { |
| 1690 | if (size > (e+1) * LOGFS_BLOCKSIZE) |
| 1691 | break; |
| 1692 | |
| 1693 | wc.ofs = li->li_data[e]; |
| 1694 | if (!wc.ofs) |
| 1695 | continue; |
| 1696 | |
| 1697 | page = logfs_get_write_page(inode, e, 0); |
| 1698 | if (!page) |
| 1699 | return -ENOMEM; |
| 1700 | err = logfs_segment_read(inode, page, wc.ofs, e, 0); |
| 1701 | if (err) { |
| 1702 | logfs_put_write_page(page); |
| 1703 | return err; |
| 1704 | } |
| 1705 | err = logfs_truncate_i0(inode, page, &wc, size); |
| 1706 | logfs_put_write_page(page); |
| 1707 | if (err) |
| 1708 | return err; |
| 1709 | |
| 1710 | li->li_data[e] = wc.ofs; |
| 1711 | } |
| 1712 | return 0; |
| 1713 | } |
| 1714 | |
| 1715 | /* FIXME: these need to become per-sb once we support different blocksizes */ |
| 1716 | static u64 __logfs_step[] = { |
| 1717 | 1, |
| 1718 | I1_BLOCKS, |
| 1719 | I2_BLOCKS, |
| 1720 | I3_BLOCKS, |
| 1721 | }; |
| 1722 | |
| 1723 | static u64 __logfs_start_index[] = { |
| 1724 | I0_BLOCKS, |
| 1725 | I1_BLOCKS, |
| 1726 | I2_BLOCKS, |
| 1727 | I3_BLOCKS |
| 1728 | }; |
| 1729 | |
| 1730 | static inline u64 logfs_step(level_t level) |
| 1731 | { |
| 1732 | return __logfs_step[(__force u8)level]; |
| 1733 | } |
| 1734 | |
| 1735 | static inline u64 logfs_factor(u8 level) |
| 1736 | { |
| 1737 | return __logfs_step[level] * LOGFS_BLOCKSIZE; |
| 1738 | } |
| 1739 | |
| 1740 | static inline u64 logfs_start_index(level_t level) |
| 1741 | { |
| 1742 | return __logfs_start_index[(__force u8)level]; |
| 1743 | } |
| 1744 | |
| 1745 | static void logfs_unpack_raw_index(pgoff_t index, u64 *bix, level_t *level) |
| 1746 | { |
| 1747 | logfs_unpack_index(index, bix, level); |
| 1748 | if (*bix <= logfs_start_index(SUBLEVEL(*level))) |
| 1749 | *bix = 0; |
| 1750 | } |
| 1751 | |
| 1752 | static int __logfs_truncate_rec(struct inode *inode, struct page *ipage, |
| 1753 | struct write_control *this_wc, u64 size) |
| 1754 | { |
| 1755 | int truncate_happened = 0; |
| 1756 | int e, err = 0; |
| 1757 | u64 bix, child_bix, next_bix; |
| 1758 | level_t level; |
| 1759 | struct page *page; |
| 1760 | struct write_control child_wc = { /* FIXME: flags */ }; |
| 1761 | |
| 1762 | logfs_unpack_raw_index(ipage->index, &bix, &level); |
| 1763 | err = logfs_segment_read(inode, ipage, this_wc->ofs, bix, level); |
| 1764 | if (err) |
| 1765 | return err; |
| 1766 | |
| 1767 | for (e = LOGFS_BLOCK_FACTOR - 1; e >= 0; e--) { |
| 1768 | child_bix = bix + e * logfs_step(SUBLEVEL(level)); |
| 1769 | next_bix = child_bix + logfs_step(SUBLEVEL(level)); |
| 1770 | if (size > next_bix * LOGFS_BLOCKSIZE) |
| 1771 | break; |
| 1772 | |
| 1773 | child_wc.ofs = pure_ofs(block_get_pointer(ipage, e)); |
| 1774 | if (!child_wc.ofs) |
| 1775 | continue; |
| 1776 | |
| 1777 | page = logfs_get_write_page(inode, child_bix, SUBLEVEL(level)); |
| 1778 | if (!page) |
| 1779 | return -ENOMEM; |
| 1780 | |
| 1781 | if ((__force u8)level > 1) |
| 1782 | err = __logfs_truncate_rec(inode, page, &child_wc, size); |
| 1783 | else |
| 1784 | err = logfs_truncate_i0(inode, page, &child_wc, size); |
| 1785 | logfs_put_write_page(page); |
| 1786 | if (err) |
| 1787 | return err; |
| 1788 | |
| 1789 | truncate_happened = 1; |
| 1790 | alloc_indirect_block(inode, ipage, 0); |
| 1791 | block_set_pointer(ipage, e, child_wc.ofs); |
| 1792 | } |
| 1793 | |
| 1794 | if (!truncate_happened) { |
| 1795 | printk("ineffectual truncate (%lx, %lx, %llx)\n", inode->i_ino, ipage->index, size); |
| 1796 | return 0; |
| 1797 | } |
| 1798 | |
| 1799 | this_wc->flags = WF_DELETE; |
| 1800 | if (logfs_block(ipage)->partial) |
| 1801 | this_wc->flags |= WF_WRITE; |
| 1802 | |
| 1803 | return logfs_write_i0(inode, ipage, this_wc); |
| 1804 | } |
| 1805 | |
| 1806 | static int logfs_truncate_rec(struct inode *inode, u64 size) |
| 1807 | { |
| 1808 | struct logfs_inode *li = logfs_inode(inode); |
| 1809 | struct write_control wc = { |
| 1810 | .ofs = li->li_data[INDIRECT_INDEX], |
| 1811 | }; |
| 1812 | struct page *page; |
| 1813 | int err; |
| 1814 | |
| 1815 | alloc_inode_block(inode); |
| 1816 | |
| 1817 | if (!wc.ofs) |
| 1818 | return 0; |
| 1819 | |
| 1820 | page = logfs_get_write_page(inode, 0, LEVEL(li->li_height)); |
| 1821 | if (!page) |
| 1822 | return -ENOMEM; |
| 1823 | |
| 1824 | err = __logfs_truncate_rec(inode, page, &wc, size); |
| 1825 | logfs_put_write_page(page); |
| 1826 | if (err) |
| 1827 | return err; |
| 1828 | |
| 1829 | if (li->li_data[INDIRECT_INDEX] != wc.ofs) |
| 1830 | li->li_data[INDIRECT_INDEX] = wc.ofs; |
| 1831 | return 0; |
| 1832 | } |
| 1833 | |
| 1834 | static int __logfs_truncate(struct inode *inode, u64 size) |
| 1835 | { |
| 1836 | int ret; |
| 1837 | |
| 1838 | if (size >= logfs_factor(logfs_inode(inode)->li_height)) |
| 1839 | return 0; |
| 1840 | |
| 1841 | ret = logfs_truncate_rec(inode, size); |
| 1842 | if (ret) |
| 1843 | return ret; |
| 1844 | |
| 1845 | return logfs_truncate_direct(inode, size); |
| 1846 | } |
| 1847 | |
| 1848 | int logfs_truncate(struct inode *inode, u64 size) |
| 1849 | { |
| 1850 | struct super_block *sb = inode->i_sb; |
| 1851 | int err; |
| 1852 | |
| 1853 | logfs_get_wblocks(sb, NULL, 1); |
| 1854 | err = __logfs_truncate(inode, size); |
| 1855 | if (!err) |
| 1856 | err = __logfs_write_inode(inode, 0); |
| 1857 | logfs_put_wblocks(sb, NULL, 1); |
| 1858 | |
| 1859 | if (!err) |
| 1860 | err = vmtruncate(inode, size); |
| 1861 | |
| 1862 | /* I don't trust error recovery yet. */ |
| 1863 | WARN_ON(err); |
| 1864 | return err; |
| 1865 | } |
| 1866 | |
| 1867 | static void move_page_to_inode(struct inode *inode, struct page *page) |
| 1868 | { |
| 1869 | struct logfs_inode *li = logfs_inode(inode); |
| 1870 | struct logfs_block *block = logfs_block(page); |
| 1871 | |
| 1872 | if (!block) |
| 1873 | return; |
| 1874 | |
| 1875 | log_blockmove("move_page_to_inode(%llx, %llx, %x)\n", |
| 1876 | block->ino, block->bix, block->level); |
| 1877 | BUG_ON(li->li_block); |
| 1878 | block->ops = &inode_block_ops; |
| 1879 | block->inode = inode; |
| 1880 | li->li_block = block; |
| 1881 | |
| 1882 | block->page = NULL; |
| 1883 | page->private = 0; |
| 1884 | ClearPagePrivate(page); |
| 1885 | } |
| 1886 | |
| 1887 | static void move_inode_to_page(struct page *page, struct inode *inode) |
| 1888 | { |
| 1889 | struct logfs_inode *li = logfs_inode(inode); |
| 1890 | struct logfs_block *block = li->li_block; |
| 1891 | |
| 1892 | if (!block) |
| 1893 | return; |
| 1894 | |
| 1895 | log_blockmove("move_inode_to_page(%llx, %llx, %x)\n", |
| 1896 | block->ino, block->bix, block->level); |
| 1897 | BUG_ON(PagePrivate(page)); |
| 1898 | block->ops = &indirect_block_ops; |
| 1899 | block->page = page; |
| 1900 | page->private = (unsigned long)block; |
| 1901 | SetPagePrivate(page); |
| 1902 | |
| 1903 | block->inode = NULL; |
| 1904 | li->li_block = NULL; |
| 1905 | } |
| 1906 | |
| 1907 | int logfs_read_inode(struct inode *inode) |
| 1908 | { |
| 1909 | struct super_block *sb = inode->i_sb; |
| 1910 | struct logfs_super *super = logfs_super(sb); |
| 1911 | struct inode *master_inode = super->s_master_inode; |
| 1912 | struct page *page; |
| 1913 | struct logfs_disk_inode *di; |
| 1914 | u64 ino = inode->i_ino; |
| 1915 | |
| 1916 | if (ino << sb->s_blocksize_bits > i_size_read(master_inode)) |
| 1917 | return -ENODATA; |
| 1918 | if (!logfs_exist_block(master_inode, ino)) |
| 1919 | return -ENODATA; |
| 1920 | |
| 1921 | page = read_cache_page(master_inode->i_mapping, ino, |
| 1922 | (filler_t *)logfs_readpage, NULL); |
| 1923 | if (IS_ERR(page)) |
| 1924 | return PTR_ERR(page); |
| 1925 | |
| 1926 | di = kmap_atomic(page, KM_USER0); |
| 1927 | logfs_disk_to_inode(di, inode); |
| 1928 | kunmap_atomic(di, KM_USER0); |
| 1929 | move_page_to_inode(inode, page); |
| 1930 | page_cache_release(page); |
| 1931 | return 0; |
| 1932 | } |
| 1933 | |
| 1934 | /* Caller must logfs_put_write_page(page); */ |
| 1935 | static struct page *inode_to_page(struct inode *inode) |
| 1936 | { |
| 1937 | struct inode *master_inode = logfs_super(inode->i_sb)->s_master_inode; |
| 1938 | struct logfs_disk_inode *di; |
| 1939 | struct page *page; |
| 1940 | |
| 1941 | BUG_ON(inode->i_ino == LOGFS_INO_MASTER); |
| 1942 | |
| 1943 | page = logfs_get_write_page(master_inode, inode->i_ino, 0); |
| 1944 | if (!page) |
| 1945 | return NULL; |
| 1946 | |
| 1947 | di = kmap_atomic(page, KM_USER0); |
| 1948 | logfs_inode_to_disk(inode, di); |
| 1949 | kunmap_atomic(di, KM_USER0); |
| 1950 | move_inode_to_page(page, inode); |
| 1951 | return page; |
| 1952 | } |
| 1953 | |
| 1954 | /* Cheaper version of write_inode. All changes are concealed in |
| 1955 | * aliases, which are moved back. No write to the medium happens. |
| 1956 | */ |
| 1957 | void logfs_clear_inode(struct inode *inode) |
| 1958 | { |
| 1959 | struct super_block *sb = inode->i_sb; |
| 1960 | struct logfs_inode *li = logfs_inode(inode); |
| 1961 | struct logfs_block *block = li->li_block; |
| 1962 | struct page *page; |
| 1963 | |
| 1964 | /* Only deleted files may be dirty at this point */ |
| 1965 | BUG_ON(inode->i_state & I_DIRTY && inode->i_nlink); |
| 1966 | if (!block) |
| 1967 | return; |
| 1968 | if ((logfs_super(sb)->s_flags & LOGFS_SB_FLAG_SHUTDOWN)) { |
| 1969 | block->ops->free_block(inode->i_sb, block); |
| 1970 | return; |
| 1971 | } |
| 1972 | |
| 1973 | BUG_ON(inode->i_ino < LOGFS_RESERVED_INOS); |
| 1974 | page = inode_to_page(inode); |
| 1975 | BUG_ON(!page); /* FIXME: Use emergency page */ |
| 1976 | logfs_put_write_page(page); |
| 1977 | } |
| 1978 | |
| 1979 | static int do_write_inode(struct inode *inode) |
| 1980 | { |
| 1981 | struct super_block *sb = inode->i_sb; |
| 1982 | struct inode *master_inode = logfs_super(sb)->s_master_inode; |
| 1983 | loff_t size = (inode->i_ino + 1) << inode->i_sb->s_blocksize_bits; |
| 1984 | struct page *page; |
| 1985 | int err; |
| 1986 | |
| 1987 | BUG_ON(inode->i_ino == LOGFS_INO_MASTER); |
| 1988 | /* FIXME: lock inode */ |
| 1989 | |
| 1990 | if (i_size_read(master_inode) < size) |
| 1991 | i_size_write(master_inode, size); |
| 1992 | |
| 1993 | /* TODO: Tell vfs this inode is clean now */ |
| 1994 | |
| 1995 | page = inode_to_page(inode); |
| 1996 | if (!page) |
| 1997 | return -ENOMEM; |
| 1998 | |
| 1999 | /* FIXME: transaction is part of logfs_block now. Is that enough? */ |
| 2000 | err = logfs_write_buf(master_inode, page, 0); |
| 2001 | logfs_put_write_page(page); |
| 2002 | return err; |
| 2003 | } |
| 2004 | |
| 2005 | static void logfs_mod_segment_entry(struct super_block *sb, u32 segno, |
| 2006 | int write, |
| 2007 | void (*change_se)(struct logfs_segment_entry *, long), |
| 2008 | long arg) |
| 2009 | { |
| 2010 | struct logfs_super *super = logfs_super(sb); |
| 2011 | struct inode *inode; |
| 2012 | struct page *page; |
| 2013 | struct logfs_segment_entry *se; |
| 2014 | pgoff_t page_no; |
| 2015 | int child_no; |
| 2016 | |
| 2017 | page_no = segno >> (sb->s_blocksize_bits - 3); |
| 2018 | child_no = segno & ((sb->s_blocksize >> 3) - 1); |
| 2019 | |
| 2020 | inode = super->s_segfile_inode; |
| 2021 | page = logfs_get_write_page(inode, page_no, 0); |
| 2022 | BUG_ON(!page); /* FIXME: We need some reserve page for this case */ |
| 2023 | if (!PageUptodate(page)) |
| 2024 | logfs_read_block(inode, page, WRITE); |
| 2025 | |
| 2026 | if (write) |
| 2027 | alloc_indirect_block(inode, page, 0); |
| 2028 | se = kmap_atomic(page, KM_USER0); |
| 2029 | change_se(se + child_no, arg); |
| 2030 | if (write) { |
| 2031 | logfs_set_alias(sb, logfs_block(page), child_no); |
| 2032 | BUG_ON((int)be32_to_cpu(se[child_no].valid) > super->s_segsize); |
| 2033 | } |
| 2034 | kunmap_atomic(se, KM_USER0); |
| 2035 | |
| 2036 | logfs_put_write_page(page); |
| 2037 | } |
| 2038 | |
| 2039 | static void __get_segment_entry(struct logfs_segment_entry *se, long _target) |
| 2040 | { |
| 2041 | struct logfs_segment_entry *target = (void *)_target; |
| 2042 | |
| 2043 | *target = *se; |
| 2044 | } |
| 2045 | |
| 2046 | void logfs_get_segment_entry(struct super_block *sb, u32 segno, |
| 2047 | struct logfs_segment_entry *se) |
| 2048 | { |
| 2049 | logfs_mod_segment_entry(sb, segno, 0, __get_segment_entry, (long)se); |
| 2050 | } |
| 2051 | |
| 2052 | static void __set_segment_used(struct logfs_segment_entry *se, long increment) |
| 2053 | { |
| 2054 | u32 valid; |
| 2055 | |
| 2056 | valid = be32_to_cpu(se->valid); |
| 2057 | valid += increment; |
| 2058 | se->valid = cpu_to_be32(valid); |
| 2059 | } |
| 2060 | |
| 2061 | void logfs_set_segment_used(struct super_block *sb, u64 ofs, int increment) |
| 2062 | { |
| 2063 | struct logfs_super *super = logfs_super(sb); |
| 2064 | u32 segno = ofs >> super->s_segshift; |
| 2065 | |
| 2066 | if (!increment) |
| 2067 | return; |
| 2068 | |
| 2069 | logfs_mod_segment_entry(sb, segno, 1, __set_segment_used, increment); |
| 2070 | } |
| 2071 | |
| 2072 | static void __set_segment_erased(struct logfs_segment_entry *se, long ec_level) |
| 2073 | { |
| 2074 | se->ec_level = cpu_to_be32(ec_level); |
| 2075 | } |
| 2076 | |
| 2077 | void logfs_set_segment_erased(struct super_block *sb, u32 segno, u32 ec, |
| 2078 | gc_level_t gc_level) |
| 2079 | { |
| 2080 | u32 ec_level = ec << 4 | (__force u8)gc_level; |
| 2081 | |
| 2082 | logfs_mod_segment_entry(sb, segno, 1, __set_segment_erased, ec_level); |
| 2083 | } |
| 2084 | |
| 2085 | static void __set_segment_reserved(struct logfs_segment_entry *se, long ignore) |
| 2086 | { |
| 2087 | se->valid = cpu_to_be32(RESERVED); |
| 2088 | } |
| 2089 | |
| 2090 | void logfs_set_segment_reserved(struct super_block *sb, u32 segno) |
| 2091 | { |
| 2092 | logfs_mod_segment_entry(sb, segno, 1, __set_segment_reserved, 0); |
| 2093 | } |
| 2094 | |
| 2095 | static void __set_segment_unreserved(struct logfs_segment_entry *se, |
| 2096 | long ec_level) |
| 2097 | { |
| 2098 | se->valid = 0; |
| 2099 | se->ec_level = cpu_to_be32(ec_level); |
| 2100 | } |
| 2101 | |
| 2102 | void logfs_set_segment_unreserved(struct super_block *sb, u32 segno, u32 ec) |
| 2103 | { |
| 2104 | u32 ec_level = ec << 4; |
| 2105 | |
| 2106 | logfs_mod_segment_entry(sb, segno, 1, __set_segment_unreserved, |
| 2107 | ec_level); |
| 2108 | } |
| 2109 | |
| 2110 | int __logfs_write_inode(struct inode *inode, long flags) |
| 2111 | { |
| 2112 | struct super_block *sb = inode->i_sb; |
| 2113 | int ret; |
| 2114 | |
| 2115 | logfs_get_wblocks(sb, NULL, flags & WF_LOCK); |
| 2116 | ret = do_write_inode(inode); |
| 2117 | logfs_put_wblocks(sb, NULL, flags & WF_LOCK); |
| 2118 | return ret; |
| 2119 | } |
| 2120 | |
| 2121 | static int do_delete_inode(struct inode *inode) |
| 2122 | { |
| 2123 | struct super_block *sb = inode->i_sb; |
| 2124 | struct inode *master_inode = logfs_super(sb)->s_master_inode; |
| 2125 | struct page *page; |
| 2126 | int ret; |
| 2127 | |
| 2128 | page = logfs_get_write_page(master_inode, inode->i_ino, 0); |
| 2129 | if (!page) |
| 2130 | return -ENOMEM; |
| 2131 | |
| 2132 | move_inode_to_page(page, inode); |
| 2133 | |
| 2134 | logfs_get_wblocks(sb, page, 1); |
| 2135 | ret = __logfs_delete(master_inode, page); |
| 2136 | logfs_put_wblocks(sb, page, 1); |
| 2137 | |
| 2138 | logfs_put_write_page(page); |
| 2139 | return ret; |
| 2140 | } |
| 2141 | |
| 2142 | /* |
| 2143 | * ZOMBIE inodes have already been deleted before and should remain dead, |
| 2144 | * if it weren't for valid checking. No need to kill them again here. |
| 2145 | */ |
| 2146 | void logfs_delete_inode(struct inode *inode) |
| 2147 | { |
| 2148 | struct logfs_inode *li = logfs_inode(inode); |
| 2149 | |
| 2150 | if (!(li->li_flags & LOGFS_IF_ZOMBIE)) { |
| 2151 | li->li_flags |= LOGFS_IF_ZOMBIE; |
| 2152 | if (i_size_read(inode) > 0) |
| 2153 | logfs_truncate(inode, 0); |
| 2154 | do_delete_inode(inode); |
| 2155 | } |
| 2156 | truncate_inode_pages(&inode->i_data, 0); |
| 2157 | clear_inode(inode); |
| 2158 | } |
| 2159 | |
| 2160 | void btree_write_block(struct logfs_block *block) |
| 2161 | { |
| 2162 | struct inode *inode; |
| 2163 | struct page *page; |
| 2164 | int err, cookie; |
| 2165 | |
| 2166 | inode = logfs_safe_iget(block->sb, block->ino, &cookie); |
| 2167 | page = logfs_get_write_page(inode, block->bix, block->level); |
| 2168 | |
| 2169 | err = logfs_readpage_nolock(page); |
| 2170 | BUG_ON(err); |
| 2171 | BUG_ON(!PagePrivate(page)); |
| 2172 | BUG_ON(logfs_block(page) != block); |
| 2173 | err = __logfs_write_buf(inode, page, 0); |
| 2174 | BUG_ON(err); |
| 2175 | BUG_ON(PagePrivate(page) || page->private); |
| 2176 | |
| 2177 | logfs_put_write_page(page); |
| 2178 | logfs_safe_iput(inode, cookie); |
| 2179 | } |
| 2180 | |
| 2181 | /** |
| 2182 | * logfs_inode_write - write inode or dentry objects |
| 2183 | * |
| 2184 | * @inode: parent inode (ifile or directory) |
| 2185 | * @buf: object to write (inode or dentry) |
| 2186 | * @n: object size |
| 2187 | * @_pos: object number (file position in blocks/objects) |
| 2188 | * @flags: write flags |
| 2189 | * @lock: 0 if write lock is already taken, 1 otherwise |
| 2190 | * @shadow_tree: shadow below this inode |
| 2191 | * |
| 2192 | * FIXME: All caller of this put a 200-300 byte variable on the stack, |
| 2193 | * only to call here and do a memcpy from that stack variable. A good |
| 2194 | * example of wasted performance and stack space. |
| 2195 | */ |
| 2196 | int logfs_inode_write(struct inode *inode, const void *buf, size_t count, |
| 2197 | loff_t bix, long flags, struct shadow_tree *shadow_tree) |
| 2198 | { |
| 2199 | loff_t pos = bix << inode->i_sb->s_blocksize_bits; |
| 2200 | int err; |
| 2201 | struct page *page; |
| 2202 | void *pagebuf; |
| 2203 | |
| 2204 | BUG_ON(pos & (LOGFS_BLOCKSIZE-1)); |
| 2205 | BUG_ON(count > LOGFS_BLOCKSIZE); |
| 2206 | page = logfs_get_write_page(inode, bix, 0); |
| 2207 | if (!page) |
| 2208 | return -ENOMEM; |
| 2209 | |
| 2210 | pagebuf = kmap_atomic(page, KM_USER0); |
| 2211 | memcpy(pagebuf, buf, count); |
| 2212 | flush_dcache_page(page); |
| 2213 | kunmap_atomic(pagebuf, KM_USER0); |
| 2214 | |
| 2215 | if (i_size_read(inode) < pos + LOGFS_BLOCKSIZE) |
| 2216 | i_size_write(inode, pos + LOGFS_BLOCKSIZE); |
| 2217 | |
| 2218 | err = logfs_write_buf(inode, page, flags); |
| 2219 | logfs_put_write_page(page); |
| 2220 | return err; |
| 2221 | } |
| 2222 | |
| 2223 | int logfs_open_segfile(struct super_block *sb) |
| 2224 | { |
| 2225 | struct logfs_super *super = logfs_super(sb); |
| 2226 | struct inode *inode; |
| 2227 | |
| 2228 | inode = logfs_read_meta_inode(sb, LOGFS_INO_SEGFILE); |
| 2229 | if (IS_ERR(inode)) |
| 2230 | return PTR_ERR(inode); |
| 2231 | super->s_segfile_inode = inode; |
| 2232 | return 0; |
| 2233 | } |
| 2234 | |
| 2235 | int logfs_init_rw(struct super_block *sb) |
| 2236 | { |
| 2237 | struct logfs_super *super = logfs_super(sb); |
| 2238 | int min_fill = 3 * super->s_no_blocks; |
| 2239 | |
| 2240 | INIT_LIST_HEAD(&super->s_object_alias); |
| 2241 | mutex_init(&super->s_write_mutex); |
| 2242 | super->s_block_pool = mempool_create_kmalloc_pool(min_fill, |
| 2243 | sizeof(struct logfs_block)); |
| 2244 | super->s_shadow_pool = mempool_create_kmalloc_pool(min_fill, |
| 2245 | sizeof(struct logfs_shadow)); |
| 2246 | return 0; |
| 2247 | } |
| 2248 | |
| 2249 | void logfs_cleanup_rw(struct super_block *sb) |
| 2250 | { |
| 2251 | struct logfs_super *super = logfs_super(sb); |
| 2252 | |
| 2253 | destroy_meta_inode(super->s_segfile_inode); |
| 2254 | if (super->s_block_pool) |
| 2255 | mempool_destroy(super->s_block_pool); |
| 2256 | if (super->s_shadow_pool) |
| 2257 | mempool_destroy(super->s_shadow_pool); |
| 2258 | } |