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