Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | * fs/logfs/segment.c - Handling the Object Store |
| 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 | * Object store or ostore makes up the complete device with exception of |
| 9 | * the superblock and journal areas. Apart from its own metadata it stores |
| 10 | * three kinds of objects: inodes, dentries and blocks, both data and indirect. |
| 11 | */ |
| 12 | #include "logfs.h" |
| 13 | |
| 14 | static int logfs_mark_segment_bad(struct super_block *sb, u32 segno) |
| 15 | { |
| 16 | struct logfs_super *super = logfs_super(sb); |
| 17 | struct btree_head32 *head = &super->s_reserved_segments; |
| 18 | int err; |
| 19 | |
| 20 | err = btree_insert32(head, segno, (void *)1, GFP_NOFS); |
| 21 | if (err) |
| 22 | return err; |
| 23 | logfs_super(sb)->s_bad_segments++; |
| 24 | /* FIXME: write to journal */ |
| 25 | return 0; |
| 26 | } |
| 27 | |
Joern Engel | 9421502 | 2010-03-04 21:30:58 +0100 | [diff] [blame] | 28 | int logfs_erase_segment(struct super_block *sb, u32 segno, int ensure_erase) |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 29 | { |
| 30 | struct logfs_super *super = logfs_super(sb); |
| 31 | |
| 32 | super->s_gec++; |
| 33 | |
| 34 | return super->s_devops->erase(sb, (u64)segno << super->s_segshift, |
Joern Engel | 9421502 | 2010-03-04 21:30:58 +0100 | [diff] [blame] | 35 | super->s_segsize, ensure_erase); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | static s64 logfs_get_free_bytes(struct logfs_area *area, size_t bytes) |
| 39 | { |
| 40 | s32 ofs; |
| 41 | |
| 42 | logfs_open_area(area, bytes); |
| 43 | |
| 44 | ofs = area->a_used_bytes; |
| 45 | area->a_used_bytes += bytes; |
| 46 | BUG_ON(area->a_used_bytes >= logfs_super(area->a_sb)->s_segsize); |
| 47 | |
| 48 | return dev_ofs(area->a_sb, area->a_segno, ofs); |
| 49 | } |
| 50 | |
| 51 | static struct page *get_mapping_page(struct super_block *sb, pgoff_t index, |
| 52 | int use_filler) |
| 53 | { |
| 54 | struct logfs_super *super = logfs_super(sb); |
| 55 | struct address_space *mapping = super->s_mapping_inode->i_mapping; |
| 56 | filler_t *filler = super->s_devops->readpage; |
| 57 | struct page *page; |
| 58 | |
| 59 | BUG_ON(mapping_gfp_mask(mapping) & __GFP_FS); |
| 60 | if (use_filler) |
| 61 | page = read_cache_page(mapping, index, filler, sb); |
| 62 | else { |
| 63 | page = find_or_create_page(mapping, index, GFP_NOFS); |
| 64 | unlock_page(page); |
| 65 | } |
| 66 | return page; |
| 67 | } |
| 68 | |
| 69 | void __logfs_buf_write(struct logfs_area *area, u64 ofs, void *buf, size_t len, |
| 70 | int use_filler) |
| 71 | { |
| 72 | pgoff_t index = ofs >> PAGE_SHIFT; |
| 73 | struct page *page; |
| 74 | long offset = ofs & (PAGE_SIZE-1); |
| 75 | long copylen; |
| 76 | |
| 77 | /* Only logfs_wbuf_recover may use len==0 */ |
| 78 | BUG_ON(!len && !use_filler); |
| 79 | do { |
| 80 | copylen = min((ulong)len, PAGE_SIZE - offset); |
| 81 | |
| 82 | page = get_mapping_page(area->a_sb, index, use_filler); |
| 83 | SetPageUptodate(page); |
| 84 | BUG_ON(!page); /* FIXME: reserve a pool */ |
| 85 | memcpy(page_address(page) + offset, buf, copylen); |
| 86 | SetPagePrivate(page); |
| 87 | page_cache_release(page); |
| 88 | |
| 89 | buf += copylen; |
| 90 | len -= copylen; |
| 91 | offset = 0; |
| 92 | index++; |
| 93 | } while (len); |
| 94 | } |
| 95 | |
Joern Engel | 81def6b | 2010-03-28 12:47:09 +0200 | [diff] [blame] | 96 | static void pad_partial_page(struct logfs_area *area) |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 97 | { |
| 98 | struct super_block *sb = area->a_sb; |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 99 | struct page *page; |
| 100 | u64 ofs = dev_ofs(sb, area->a_segno, area->a_used_bytes); |
| 101 | pgoff_t index = ofs >> PAGE_SHIFT; |
| 102 | long offset = ofs & (PAGE_SIZE-1); |
| 103 | u32 len = PAGE_SIZE - offset; |
| 104 | |
Joern Engel | 81def6b | 2010-03-28 12:47:09 +0200 | [diff] [blame] | 105 | if (len % PAGE_SIZE) { |
| 106 | page = get_mapping_page(sb, index, 0); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 107 | BUG_ON(!page); /* FIXME: reserve a pool */ |
| 108 | memset(page_address(page) + offset, 0xff, len); |
| 109 | SetPagePrivate(page); |
| 110 | page_cache_release(page); |
| 111 | } |
Joern Engel | 81def6b | 2010-03-28 12:47:09 +0200 | [diff] [blame] | 112 | } |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 113 | |
Joern Engel | 81def6b | 2010-03-28 12:47:09 +0200 | [diff] [blame] | 114 | static void pad_full_pages(struct logfs_area *area) |
| 115 | { |
| 116 | struct super_block *sb = area->a_sb; |
| 117 | struct logfs_super *super = logfs_super(sb); |
| 118 | u64 ofs = dev_ofs(sb, area->a_segno, area->a_used_bytes); |
| 119 | u32 len = super->s_segsize - area->a_used_bytes; |
| 120 | pgoff_t index = PAGE_CACHE_ALIGN(ofs) >> PAGE_CACHE_SHIFT; |
| 121 | pgoff_t no_indizes = len >> PAGE_CACHE_SHIFT; |
| 122 | struct page *page; |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 123 | |
Joern Engel | 81def6b | 2010-03-28 12:47:09 +0200 | [diff] [blame] | 124 | while (no_indizes) { |
| 125 | page = get_mapping_page(sb, index, 0); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 126 | BUG_ON(!page); /* FIXME: reserve a pool */ |
Joern Engel | 81def6b | 2010-03-28 12:47:09 +0200 | [diff] [blame] | 127 | SetPageUptodate(page); |
| 128 | memset(page_address(page), 0xff, PAGE_CACHE_SIZE); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 129 | SetPagePrivate(page); |
| 130 | page_cache_release(page); |
Joern Engel | 81def6b | 2010-03-28 12:47:09 +0200 | [diff] [blame] | 131 | index++; |
| 132 | no_indizes--; |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | /* |
Joern Engel | 81def6b | 2010-03-28 12:47:09 +0200 | [diff] [blame] | 137 | * bdev_writeseg will write full pages. Memset the tail to prevent data leaks. |
| 138 | * Also make sure we allocate (and memset) all pages for final writeout. |
| 139 | */ |
| 140 | static void pad_wbuf(struct logfs_area *area, int final) |
| 141 | { |
| 142 | pad_partial_page(area); |
| 143 | if (final) |
| 144 | pad_full_pages(area); |
| 145 | } |
| 146 | |
| 147 | /* |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 148 | * We have to be careful with the alias tree. Since lookup is done by bix, |
| 149 | * it needs to be normalized, so 14, 15, 16, etc. all match when dealing with |
| 150 | * indirect blocks. So always use it through accessor functions. |
| 151 | */ |
| 152 | static void *alias_tree_lookup(struct super_block *sb, u64 ino, u64 bix, |
| 153 | level_t level) |
| 154 | { |
| 155 | struct btree_head128 *head = &logfs_super(sb)->s_object_alias_tree; |
| 156 | pgoff_t index = logfs_pack_index(bix, level); |
| 157 | |
| 158 | return btree_lookup128(head, ino, index); |
| 159 | } |
| 160 | |
| 161 | static int alias_tree_insert(struct super_block *sb, u64 ino, u64 bix, |
| 162 | level_t level, void *val) |
| 163 | { |
| 164 | struct btree_head128 *head = &logfs_super(sb)->s_object_alias_tree; |
| 165 | pgoff_t index = logfs_pack_index(bix, level); |
| 166 | |
| 167 | return btree_insert128(head, ino, index, val, GFP_NOFS); |
| 168 | } |
| 169 | |
| 170 | static int btree_write_alias(struct super_block *sb, struct logfs_block *block, |
| 171 | write_alias_t *write_one_alias) |
| 172 | { |
| 173 | struct object_alias_item *item; |
| 174 | int err; |
| 175 | |
| 176 | list_for_each_entry(item, &block->item_list, list) { |
| 177 | err = write_alias_journal(sb, block->ino, block->bix, |
| 178 | block->level, item->child_no, item->val); |
| 179 | if (err) |
| 180 | return err; |
| 181 | } |
| 182 | return 0; |
| 183 | } |
| 184 | |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 185 | static struct logfs_block_ops btree_block_ops = { |
| 186 | .write_block = btree_write_block, |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 187 | .free_block = __free_block, |
| 188 | .write_alias = btree_write_alias, |
| 189 | }; |
| 190 | |
| 191 | int logfs_load_object_aliases(struct super_block *sb, |
| 192 | struct logfs_obj_alias *oa, int count) |
| 193 | { |
| 194 | struct logfs_super *super = logfs_super(sb); |
| 195 | struct logfs_block *block; |
| 196 | struct object_alias_item *item; |
| 197 | u64 ino, bix; |
| 198 | level_t level; |
| 199 | int i, err; |
| 200 | |
| 201 | super->s_flags |= LOGFS_SB_FLAG_OBJ_ALIAS; |
| 202 | count /= sizeof(*oa); |
| 203 | for (i = 0; i < count; i++) { |
| 204 | item = mempool_alloc(super->s_alias_pool, GFP_NOFS); |
| 205 | if (!item) |
| 206 | return -ENOMEM; |
| 207 | memset(item, 0, sizeof(*item)); |
| 208 | |
| 209 | super->s_no_object_aliases++; |
| 210 | item->val = oa[i].val; |
| 211 | item->child_no = be16_to_cpu(oa[i].child_no); |
| 212 | |
| 213 | ino = be64_to_cpu(oa[i].ino); |
| 214 | bix = be64_to_cpu(oa[i].bix); |
| 215 | level = LEVEL(oa[i].level); |
| 216 | |
| 217 | log_aliases("logfs_load_object_aliases(%llx, %llx, %x, %x) %llx\n", |
| 218 | ino, bix, level, item->child_no, |
| 219 | be64_to_cpu(item->val)); |
| 220 | block = alias_tree_lookup(sb, ino, bix, level); |
| 221 | if (!block) { |
| 222 | block = __alloc_block(sb, ino, bix, level); |
| 223 | block->ops = &btree_block_ops; |
| 224 | err = alias_tree_insert(sb, ino, bix, level, block); |
| 225 | BUG_ON(err); /* mempool empty */ |
| 226 | } |
| 227 | if (test_and_set_bit(item->child_no, block->alias_map)) { |
| 228 | printk(KERN_ERR"LogFS: Alias collision detected\n"); |
| 229 | return -EIO; |
| 230 | } |
| 231 | list_move_tail(&block->alias_list, &super->s_object_alias); |
| 232 | list_add(&item->list, &block->item_list); |
| 233 | } |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static void kill_alias(void *_block, unsigned long ignore0, |
| 238 | u64 ignore1, u64 ignore2, size_t ignore3) |
| 239 | { |
| 240 | struct logfs_block *block = _block; |
| 241 | struct super_block *sb = block->sb; |
| 242 | struct logfs_super *super = logfs_super(sb); |
| 243 | struct object_alias_item *item; |
| 244 | |
| 245 | while (!list_empty(&block->item_list)) { |
| 246 | item = list_entry(block->item_list.next, typeof(*item), list); |
| 247 | list_del(&item->list); |
| 248 | mempool_free(item, super->s_alias_pool); |
| 249 | } |
| 250 | block->ops->free_block(sb, block); |
| 251 | } |
| 252 | |
| 253 | static int obj_type(struct inode *inode, level_t level) |
| 254 | { |
| 255 | if (level == 0) { |
| 256 | if (S_ISDIR(inode->i_mode)) |
| 257 | return OBJ_DENTRY; |
| 258 | if (inode->i_ino == LOGFS_INO_MASTER) |
| 259 | return OBJ_INODE; |
| 260 | } |
| 261 | return OBJ_BLOCK; |
| 262 | } |
| 263 | |
| 264 | static int obj_len(struct super_block *sb, int obj_type) |
| 265 | { |
| 266 | switch (obj_type) { |
| 267 | case OBJ_DENTRY: |
| 268 | return sizeof(struct logfs_disk_dentry); |
| 269 | case OBJ_INODE: |
| 270 | return sizeof(struct logfs_disk_inode); |
| 271 | case OBJ_BLOCK: |
| 272 | return sb->s_blocksize; |
| 273 | default: |
| 274 | BUG(); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | static int __logfs_segment_write(struct inode *inode, void *buf, |
| 279 | struct logfs_shadow *shadow, int type, int len, int compr) |
| 280 | { |
| 281 | struct logfs_area *area; |
| 282 | struct super_block *sb = inode->i_sb; |
| 283 | s64 ofs; |
| 284 | struct logfs_object_header h; |
| 285 | int acc_len; |
| 286 | |
| 287 | if (shadow->gc_level == 0) |
| 288 | acc_len = len; |
| 289 | else |
| 290 | acc_len = obj_len(sb, type); |
| 291 | |
| 292 | area = get_area(sb, shadow->gc_level); |
| 293 | ofs = logfs_get_free_bytes(area, len + LOGFS_OBJECT_HEADERSIZE); |
| 294 | LOGFS_BUG_ON(ofs <= 0, sb); |
| 295 | /* |
| 296 | * Order is important. logfs_get_free_bytes(), by modifying the |
| 297 | * segment file, may modify the content of the very page we're about |
| 298 | * to write now. Which is fine, as long as the calculated crc and |
| 299 | * written data still match. So do the modifications _before_ |
| 300 | * calculating the crc. |
| 301 | */ |
| 302 | |
| 303 | h.len = cpu_to_be16(len); |
| 304 | h.type = type; |
| 305 | h.compr = compr; |
| 306 | h.ino = cpu_to_be64(inode->i_ino); |
| 307 | h.bix = cpu_to_be64(shadow->bix); |
| 308 | h.crc = logfs_crc32(&h, sizeof(h) - 4, 4); |
| 309 | h.data_crc = logfs_crc32(buf, len, 0); |
| 310 | |
| 311 | logfs_buf_write(area, ofs, &h, sizeof(h)); |
| 312 | logfs_buf_write(area, ofs + LOGFS_OBJECT_HEADERSIZE, buf, len); |
| 313 | |
| 314 | shadow->new_ofs = ofs; |
| 315 | shadow->new_len = acc_len + LOGFS_OBJECT_HEADERSIZE; |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static s64 logfs_segment_write_compress(struct inode *inode, void *buf, |
| 321 | struct logfs_shadow *shadow, int type, int len) |
| 322 | { |
| 323 | struct super_block *sb = inode->i_sb; |
| 324 | void *compressor_buf = logfs_super(sb)->s_compressed_je; |
| 325 | ssize_t compr_len; |
| 326 | int ret; |
| 327 | |
| 328 | mutex_lock(&logfs_super(sb)->s_journal_mutex); |
| 329 | compr_len = logfs_compress(buf, compressor_buf, len, len); |
| 330 | |
| 331 | if (compr_len >= 0) { |
| 332 | ret = __logfs_segment_write(inode, compressor_buf, shadow, |
| 333 | type, compr_len, COMPR_ZLIB); |
| 334 | } else { |
| 335 | ret = __logfs_segment_write(inode, buf, shadow, type, len, |
| 336 | COMPR_NONE); |
| 337 | } |
| 338 | mutex_unlock(&logfs_super(sb)->s_journal_mutex); |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * logfs_segment_write - write data block to object store |
| 344 | * @inode: inode containing data |
| 345 | * |
| 346 | * Returns an errno or zero. |
| 347 | */ |
| 348 | int logfs_segment_write(struct inode *inode, struct page *page, |
| 349 | struct logfs_shadow *shadow) |
| 350 | { |
| 351 | struct super_block *sb = inode->i_sb; |
| 352 | struct logfs_super *super = logfs_super(sb); |
| 353 | int do_compress, type, len; |
| 354 | int ret; |
| 355 | void *buf; |
| 356 | |
Joern Engel | c6d3830 | 2010-03-04 21:36:19 +0100 | [diff] [blame] | 357 | super->s_flags |= LOGFS_SB_FLAG_DIRTY; |
| 358 | BUG_ON(super->s_flags & LOGFS_SB_FLAG_SHUTDOWN); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 359 | do_compress = logfs_inode(inode)->li_flags & LOGFS_IF_COMPRESSED; |
| 360 | if (shadow->gc_level != 0) { |
| 361 | /* temporarily disable compression for indirect blocks */ |
| 362 | do_compress = 0; |
| 363 | } |
| 364 | |
| 365 | type = obj_type(inode, shrink_level(shadow->gc_level)); |
| 366 | len = obj_len(sb, type); |
| 367 | buf = kmap(page); |
| 368 | if (do_compress) |
| 369 | ret = logfs_segment_write_compress(inode, buf, shadow, type, |
| 370 | len); |
| 371 | else |
| 372 | ret = __logfs_segment_write(inode, buf, shadow, type, len, |
| 373 | COMPR_NONE); |
| 374 | kunmap(page); |
| 375 | |
| 376 | log_segment("logfs_segment_write(%llx, %llx, %x) %llx->%llx %x->%x\n", |
| 377 | shadow->ino, shadow->bix, shadow->gc_level, |
| 378 | shadow->old_ofs, shadow->new_ofs, |
| 379 | shadow->old_len, shadow->new_len); |
| 380 | /* this BUG_ON did catch a locking bug. useful */ |
| 381 | BUG_ON(!(shadow->new_ofs & (super->s_segsize - 1))); |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | int wbuf_read(struct super_block *sb, u64 ofs, size_t len, void *buf) |
| 386 | { |
| 387 | pgoff_t index = ofs >> PAGE_SHIFT; |
| 388 | struct page *page; |
| 389 | long offset = ofs & (PAGE_SIZE-1); |
| 390 | long copylen; |
| 391 | |
| 392 | while (len) { |
| 393 | copylen = min((ulong)len, PAGE_SIZE - offset); |
| 394 | |
| 395 | page = get_mapping_page(sb, index, 1); |
| 396 | if (IS_ERR(page)) |
| 397 | return PTR_ERR(page); |
| 398 | memcpy(buf, page_address(page) + offset, copylen); |
| 399 | page_cache_release(page); |
| 400 | |
| 401 | buf += copylen; |
| 402 | len -= copylen; |
| 403 | offset = 0; |
| 404 | index++; |
| 405 | } |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * The "position" of indirect blocks is ambiguous. It can be the position |
| 411 | * of any data block somewhere behind this indirect block. So we need to |
| 412 | * normalize the positions through logfs_block_mask() before comparing. |
| 413 | */ |
| 414 | static int check_pos(struct super_block *sb, u64 pos1, u64 pos2, level_t level) |
| 415 | { |
| 416 | return (pos1 & logfs_block_mask(sb, level)) != |
| 417 | (pos2 & logfs_block_mask(sb, level)); |
| 418 | } |
| 419 | |
| 420 | #if 0 |
| 421 | static int read_seg_header(struct super_block *sb, u64 ofs, |
| 422 | struct logfs_segment_header *sh) |
| 423 | { |
| 424 | __be32 crc; |
| 425 | int err; |
| 426 | |
| 427 | err = wbuf_read(sb, ofs, sizeof(*sh), sh); |
| 428 | if (err) |
| 429 | return err; |
| 430 | crc = logfs_crc32(sh, sizeof(*sh), 4); |
| 431 | if (crc != sh->crc) { |
| 432 | printk(KERN_ERR"LOGFS: header crc error at %llx: expected %x, " |
| 433 | "got %x\n", ofs, be32_to_cpu(sh->crc), |
| 434 | be32_to_cpu(crc)); |
| 435 | return -EIO; |
| 436 | } |
| 437 | return 0; |
| 438 | } |
| 439 | #endif |
| 440 | |
| 441 | static int read_obj_header(struct super_block *sb, u64 ofs, |
| 442 | struct logfs_object_header *oh) |
| 443 | { |
| 444 | __be32 crc; |
| 445 | int err; |
| 446 | |
| 447 | err = wbuf_read(sb, ofs, sizeof(*oh), oh); |
| 448 | if (err) |
| 449 | return err; |
| 450 | crc = logfs_crc32(oh, sizeof(*oh) - 4, 4); |
| 451 | if (crc != oh->crc) { |
| 452 | printk(KERN_ERR"LOGFS: header crc error at %llx: expected %x, " |
| 453 | "got %x\n", ofs, be32_to_cpu(oh->crc), |
| 454 | be32_to_cpu(crc)); |
| 455 | return -EIO; |
| 456 | } |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | static void move_btree_to_page(struct inode *inode, struct page *page, |
| 461 | __be64 *data) |
| 462 | { |
| 463 | struct super_block *sb = inode->i_sb; |
| 464 | struct logfs_super *super = logfs_super(sb); |
| 465 | struct btree_head128 *head = &super->s_object_alias_tree; |
| 466 | struct logfs_block *block; |
| 467 | struct object_alias_item *item, *next; |
| 468 | |
| 469 | if (!(super->s_flags & LOGFS_SB_FLAG_OBJ_ALIAS)) |
| 470 | return; |
| 471 | |
| 472 | block = btree_remove128(head, inode->i_ino, page->index); |
| 473 | if (!block) |
| 474 | return; |
| 475 | |
| 476 | log_blockmove("move_btree_to_page(%llx, %llx, %x)\n", |
| 477 | block->ino, block->bix, block->level); |
| 478 | list_for_each_entry_safe(item, next, &block->item_list, list) { |
| 479 | data[item->child_no] = item->val; |
| 480 | list_del(&item->list); |
| 481 | mempool_free(item, super->s_alias_pool); |
| 482 | } |
| 483 | block->page = page; |
| 484 | SetPagePrivate(page); |
| 485 | page->private = (unsigned long)block; |
| 486 | block->ops = &indirect_block_ops; |
| 487 | initialize_block_counters(page, block, data, 0); |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * This silences a false, yet annoying gcc warning. I hate it when my editor |
| 492 | * jumps into bitops.h each time I recompile this file. |
| 493 | * TODO: Complain to gcc folks about this and upgrade compiler. |
| 494 | */ |
| 495 | static unsigned long fnb(const unsigned long *addr, |
| 496 | unsigned long size, unsigned long offset) |
| 497 | { |
| 498 | return find_next_bit(addr, size, offset); |
| 499 | } |
| 500 | |
| 501 | void move_page_to_btree(struct page *page) |
| 502 | { |
| 503 | struct logfs_block *block = logfs_block(page); |
| 504 | struct super_block *sb = block->sb; |
| 505 | struct logfs_super *super = logfs_super(sb); |
| 506 | struct object_alias_item *item; |
| 507 | unsigned long pos; |
| 508 | __be64 *child; |
| 509 | int err; |
| 510 | |
| 511 | if (super->s_flags & LOGFS_SB_FLAG_SHUTDOWN) { |
| 512 | block->ops->free_block(sb, block); |
| 513 | return; |
| 514 | } |
| 515 | log_blockmove("move_page_to_btree(%llx, %llx, %x)\n", |
| 516 | block->ino, block->bix, block->level); |
| 517 | super->s_flags |= LOGFS_SB_FLAG_OBJ_ALIAS; |
| 518 | |
| 519 | for (pos = 0; ; pos++) { |
| 520 | pos = fnb(block->alias_map, LOGFS_BLOCK_FACTOR, pos); |
| 521 | if (pos >= LOGFS_BLOCK_FACTOR) |
| 522 | break; |
| 523 | |
| 524 | item = mempool_alloc(super->s_alias_pool, GFP_NOFS); |
| 525 | BUG_ON(!item); /* mempool empty */ |
| 526 | memset(item, 0, sizeof(*item)); |
| 527 | |
| 528 | child = kmap_atomic(page, KM_USER0); |
| 529 | item->val = child[pos]; |
| 530 | kunmap_atomic(child, KM_USER0); |
| 531 | item->child_no = pos; |
| 532 | list_add(&item->list, &block->item_list); |
| 533 | } |
| 534 | block->page = NULL; |
| 535 | ClearPagePrivate(page); |
| 536 | page->private = 0; |
| 537 | block->ops = &btree_block_ops; |
| 538 | err = alias_tree_insert(block->sb, block->ino, block->bix, block->level, |
| 539 | block); |
| 540 | BUG_ON(err); /* mempool empty */ |
| 541 | ClearPageUptodate(page); |
| 542 | } |
| 543 | |
| 544 | static int __logfs_segment_read(struct inode *inode, void *buf, |
| 545 | u64 ofs, u64 bix, level_t level) |
| 546 | { |
| 547 | struct super_block *sb = inode->i_sb; |
| 548 | void *compressor_buf = logfs_super(sb)->s_compressed_je; |
| 549 | struct logfs_object_header oh; |
| 550 | __be32 crc; |
| 551 | u16 len; |
| 552 | int err, block_len; |
| 553 | |
| 554 | block_len = obj_len(sb, obj_type(inode, level)); |
| 555 | err = read_obj_header(sb, ofs, &oh); |
| 556 | if (err) |
| 557 | goto out_err; |
| 558 | |
| 559 | err = -EIO; |
| 560 | if (be64_to_cpu(oh.ino) != inode->i_ino |
| 561 | || check_pos(sb, be64_to_cpu(oh.bix), bix, level)) { |
| 562 | printk(KERN_ERR"LOGFS: (ino, bix) don't match at %llx: " |
| 563 | "expected (%lx, %llx), got (%llx, %llx)\n", |
| 564 | ofs, inode->i_ino, bix, |
| 565 | be64_to_cpu(oh.ino), be64_to_cpu(oh.bix)); |
| 566 | goto out_err; |
| 567 | } |
| 568 | |
| 569 | len = be16_to_cpu(oh.len); |
| 570 | |
| 571 | switch (oh.compr) { |
| 572 | case COMPR_NONE: |
| 573 | err = wbuf_read(sb, ofs + LOGFS_OBJECT_HEADERSIZE, len, buf); |
| 574 | if (err) |
| 575 | goto out_err; |
| 576 | crc = logfs_crc32(buf, len, 0); |
| 577 | if (crc != oh.data_crc) { |
| 578 | printk(KERN_ERR"LOGFS: uncompressed data crc error at " |
| 579 | "%llx: expected %x, got %x\n", ofs, |
| 580 | be32_to_cpu(oh.data_crc), |
| 581 | be32_to_cpu(crc)); |
| 582 | goto out_err; |
| 583 | } |
| 584 | break; |
| 585 | case COMPR_ZLIB: |
| 586 | mutex_lock(&logfs_super(sb)->s_journal_mutex); |
| 587 | err = wbuf_read(sb, ofs + LOGFS_OBJECT_HEADERSIZE, len, |
| 588 | compressor_buf); |
| 589 | if (err) { |
| 590 | mutex_unlock(&logfs_super(sb)->s_journal_mutex); |
| 591 | goto out_err; |
| 592 | } |
| 593 | crc = logfs_crc32(compressor_buf, len, 0); |
| 594 | if (crc != oh.data_crc) { |
| 595 | printk(KERN_ERR"LOGFS: compressed data crc error at " |
| 596 | "%llx: expected %x, got %x\n", ofs, |
| 597 | be32_to_cpu(oh.data_crc), |
| 598 | be32_to_cpu(crc)); |
| 599 | mutex_unlock(&logfs_super(sb)->s_journal_mutex); |
| 600 | goto out_err; |
| 601 | } |
| 602 | err = logfs_uncompress(compressor_buf, buf, len, block_len); |
| 603 | mutex_unlock(&logfs_super(sb)->s_journal_mutex); |
| 604 | if (err) { |
| 605 | printk(KERN_ERR"LOGFS: uncompress error at %llx\n", ofs); |
| 606 | goto out_err; |
| 607 | } |
| 608 | break; |
| 609 | default: |
| 610 | LOGFS_BUG(sb); |
| 611 | err = -EIO; |
| 612 | goto out_err; |
| 613 | } |
| 614 | return 0; |
| 615 | |
| 616 | out_err: |
| 617 | logfs_set_ro(sb); |
| 618 | printk(KERN_ERR"LOGFS: device is read-only now\n"); |
| 619 | LOGFS_BUG(sb); |
| 620 | return err; |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * logfs_segment_read - read data block from object store |
| 625 | * @inode: inode containing data |
| 626 | * @buf: data buffer |
| 627 | * @ofs: physical data offset |
| 628 | * @bix: block index |
| 629 | * @level: block level |
| 630 | * |
| 631 | * Returns 0 on success or a negative errno. |
| 632 | */ |
| 633 | int logfs_segment_read(struct inode *inode, struct page *page, |
| 634 | u64 ofs, u64 bix, level_t level) |
| 635 | { |
| 636 | int err; |
| 637 | void *buf; |
| 638 | |
| 639 | if (PageUptodate(page)) |
| 640 | return 0; |
| 641 | |
| 642 | ofs &= ~LOGFS_FULLY_POPULATED; |
| 643 | |
| 644 | buf = kmap(page); |
| 645 | err = __logfs_segment_read(inode, buf, ofs, bix, level); |
| 646 | if (!err) { |
| 647 | move_btree_to_page(inode, page, buf); |
| 648 | SetPageUptodate(page); |
| 649 | } |
| 650 | kunmap(page); |
| 651 | log_segment("logfs_segment_read(%lx, %llx, %x) %llx (%d)\n", |
| 652 | inode->i_ino, bix, level, ofs, err); |
| 653 | return err; |
| 654 | } |
| 655 | |
| 656 | int logfs_segment_delete(struct inode *inode, struct logfs_shadow *shadow) |
| 657 | { |
| 658 | struct super_block *sb = inode->i_sb; |
Joern Engel | c6d3830 | 2010-03-04 21:36:19 +0100 | [diff] [blame] | 659 | struct logfs_super *super = logfs_super(sb); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 660 | struct logfs_object_header h; |
| 661 | u16 len; |
| 662 | int err; |
| 663 | |
Joern Engel | c6d3830 | 2010-03-04 21:36:19 +0100 | [diff] [blame] | 664 | super->s_flags |= LOGFS_SB_FLAG_DIRTY; |
| 665 | BUG_ON(super->s_flags & LOGFS_SB_FLAG_SHUTDOWN); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 666 | BUG_ON(shadow->old_ofs & LOGFS_FULLY_POPULATED); |
| 667 | if (!shadow->old_ofs) |
| 668 | return 0; |
| 669 | |
| 670 | log_segment("logfs_segment_delete(%llx, %llx, %x) %llx->%llx %x->%x\n", |
| 671 | shadow->ino, shadow->bix, shadow->gc_level, |
| 672 | shadow->old_ofs, shadow->new_ofs, |
| 673 | shadow->old_len, shadow->new_len); |
| 674 | err = read_obj_header(sb, shadow->old_ofs, &h); |
| 675 | LOGFS_BUG_ON(err, sb); |
| 676 | LOGFS_BUG_ON(be64_to_cpu(h.ino) != inode->i_ino, sb); |
| 677 | LOGFS_BUG_ON(check_pos(sb, shadow->bix, be64_to_cpu(h.bix), |
| 678 | shrink_level(shadow->gc_level)), sb); |
| 679 | |
| 680 | if (shadow->gc_level == 0) |
| 681 | len = be16_to_cpu(h.len); |
| 682 | else |
| 683 | len = obj_len(sb, h.type); |
| 684 | shadow->old_len = len + sizeof(h); |
| 685 | return 0; |
| 686 | } |
| 687 | |
Joern Engel | 723b2ff | 2010-03-28 18:10:07 +0200 | [diff] [blame] | 688 | void freeseg(struct super_block *sb, u32 segno) |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 689 | { |
| 690 | struct logfs_super *super = logfs_super(sb); |
| 691 | struct address_space *mapping = super->s_mapping_inode->i_mapping; |
| 692 | struct page *page; |
| 693 | u64 ofs, start, end; |
| 694 | |
| 695 | start = dev_ofs(sb, segno, 0); |
| 696 | end = dev_ofs(sb, segno + 1, 0); |
| 697 | for (ofs = start; ofs < end; ofs += PAGE_SIZE) { |
| 698 | page = find_get_page(mapping, ofs >> PAGE_SHIFT); |
| 699 | if (!page) |
| 700 | continue; |
| 701 | ClearPagePrivate(page); |
| 702 | page_cache_release(page); |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | int logfs_open_area(struct logfs_area *area, size_t bytes) |
| 707 | { |
| 708 | struct super_block *sb = area->a_sb; |
| 709 | struct logfs_super *super = logfs_super(sb); |
| 710 | int err, closed = 0; |
| 711 | |
| 712 | if (area->a_is_open && area->a_used_bytes + bytes <= super->s_segsize) |
| 713 | return 0; |
| 714 | |
| 715 | if (area->a_is_open) { |
| 716 | u64 ofs = dev_ofs(sb, area->a_segno, area->a_written_bytes); |
| 717 | u32 len = super->s_segsize - area->a_written_bytes; |
| 718 | |
| 719 | log_gc("logfs_close_area(%x)\n", area->a_segno); |
| 720 | pad_wbuf(area, 1); |
| 721 | super->s_devops->writeseg(area->a_sb, ofs, len); |
| 722 | freeseg(sb, area->a_segno); |
| 723 | closed = 1; |
| 724 | } |
| 725 | |
| 726 | area->a_used_bytes = 0; |
| 727 | area->a_written_bytes = 0; |
| 728 | again: |
| 729 | area->a_ops->get_free_segment(area); |
| 730 | area->a_ops->get_erase_count(area); |
| 731 | |
| 732 | log_gc("logfs_open_area(%x, %x)\n", area->a_segno, area->a_level); |
| 733 | err = area->a_ops->erase_segment(area); |
| 734 | if (err) { |
| 735 | printk(KERN_WARNING "LogFS: Error erasing segment %x\n", |
| 736 | area->a_segno); |
| 737 | logfs_mark_segment_bad(sb, area->a_segno); |
| 738 | goto again; |
| 739 | } |
| 740 | area->a_is_open = 1; |
| 741 | return closed; |
| 742 | } |
| 743 | |
| 744 | void logfs_sync_area(struct logfs_area *area) |
| 745 | { |
| 746 | struct super_block *sb = area->a_sb; |
| 747 | struct logfs_super *super = logfs_super(sb); |
| 748 | u64 ofs = dev_ofs(sb, area->a_segno, area->a_written_bytes); |
| 749 | u32 len = (area->a_used_bytes - area->a_written_bytes); |
| 750 | |
| 751 | if (super->s_writesize) |
| 752 | len &= ~(super->s_writesize - 1); |
| 753 | if (len == 0) |
| 754 | return; |
| 755 | pad_wbuf(area, 0); |
| 756 | super->s_devops->writeseg(sb, ofs, len); |
| 757 | area->a_written_bytes += len; |
| 758 | } |
| 759 | |
| 760 | void logfs_sync_segments(struct super_block *sb) |
| 761 | { |
| 762 | struct logfs_super *super = logfs_super(sb); |
| 763 | int i; |
| 764 | |
| 765 | for_each_area(i) |
| 766 | logfs_sync_area(super->s_area[i]); |
| 767 | } |
| 768 | |
| 769 | /* |
| 770 | * Pick a free segment to be used for this area. Effectively takes a |
| 771 | * candidate from the free list (not really a candidate anymore). |
| 772 | */ |
| 773 | static void ostore_get_free_segment(struct logfs_area *area) |
| 774 | { |
| 775 | struct super_block *sb = area->a_sb; |
| 776 | struct logfs_super *super = logfs_super(sb); |
| 777 | |
| 778 | if (super->s_free_list.count == 0) { |
| 779 | printk(KERN_ERR"LOGFS: ran out of free segments\n"); |
| 780 | LOGFS_BUG(sb); |
| 781 | } |
| 782 | |
| 783 | area->a_segno = get_best_cand(sb, &super->s_free_list, NULL); |
| 784 | } |
| 785 | |
| 786 | static void ostore_get_erase_count(struct logfs_area *area) |
| 787 | { |
| 788 | struct logfs_segment_entry se; |
| 789 | u32 ec_level; |
| 790 | |
| 791 | logfs_get_segment_entry(area->a_sb, area->a_segno, &se); |
| 792 | BUG_ON(se.ec_level == cpu_to_be32(BADSEG) || |
| 793 | se.valid == cpu_to_be32(RESERVED)); |
| 794 | |
| 795 | ec_level = be32_to_cpu(se.ec_level); |
| 796 | area->a_erase_count = (ec_level >> 4) + 1; |
| 797 | } |
| 798 | |
| 799 | static int ostore_erase_segment(struct logfs_area *area) |
| 800 | { |
| 801 | struct super_block *sb = area->a_sb; |
| 802 | struct logfs_segment_header sh; |
| 803 | u64 ofs; |
| 804 | int err; |
| 805 | |
Joern Engel | 9421502 | 2010-03-04 21:30:58 +0100 | [diff] [blame] | 806 | err = logfs_erase_segment(sb, area->a_segno, 0); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 807 | if (err) |
| 808 | return err; |
| 809 | |
| 810 | sh.pad = 0; |
| 811 | sh.type = SEG_OSTORE; |
| 812 | sh.level = (__force u8)area->a_level; |
| 813 | sh.segno = cpu_to_be32(area->a_segno); |
| 814 | sh.ec = cpu_to_be32(area->a_erase_count); |
| 815 | sh.gec = cpu_to_be64(logfs_super(sb)->s_gec); |
| 816 | sh.crc = logfs_crc32(&sh, sizeof(sh), 4); |
| 817 | |
| 818 | logfs_set_segment_erased(sb, area->a_segno, area->a_erase_count, |
| 819 | area->a_level); |
| 820 | |
| 821 | ofs = dev_ofs(sb, area->a_segno, 0); |
| 822 | area->a_used_bytes = sizeof(sh); |
| 823 | logfs_buf_write(area, ofs, &sh, sizeof(sh)); |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | static const struct logfs_area_ops ostore_area_ops = { |
| 828 | .get_free_segment = ostore_get_free_segment, |
| 829 | .get_erase_count = ostore_get_erase_count, |
| 830 | .erase_segment = ostore_erase_segment, |
| 831 | }; |
| 832 | |
| 833 | static void free_area(struct logfs_area *area) |
| 834 | { |
| 835 | if (area) |
| 836 | freeseg(area->a_sb, area->a_segno); |
| 837 | kfree(area); |
| 838 | } |
| 839 | |
| 840 | static struct logfs_area *alloc_area(struct super_block *sb) |
| 841 | { |
| 842 | struct logfs_area *area; |
| 843 | |
| 844 | area = kzalloc(sizeof(*area), GFP_KERNEL); |
| 845 | if (!area) |
| 846 | return NULL; |
| 847 | |
| 848 | area->a_sb = sb; |
| 849 | return area; |
| 850 | } |
| 851 | |
| 852 | static void map_invalidatepage(struct page *page, unsigned long l) |
| 853 | { |
| 854 | BUG(); |
| 855 | } |
| 856 | |
| 857 | static int map_releasepage(struct page *page, gfp_t g) |
| 858 | { |
| 859 | /* Don't release these pages */ |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | static const struct address_space_operations mapping_aops = { |
| 864 | .invalidatepage = map_invalidatepage, |
| 865 | .releasepage = map_releasepage, |
| 866 | .set_page_dirty = __set_page_dirty_nobuffers, |
| 867 | }; |
| 868 | |
| 869 | int logfs_init_mapping(struct super_block *sb) |
| 870 | { |
| 871 | struct logfs_super *super = logfs_super(sb); |
| 872 | struct address_space *mapping; |
| 873 | struct inode *inode; |
| 874 | |
| 875 | inode = logfs_new_meta_inode(sb, LOGFS_INO_MAPPING); |
| 876 | if (IS_ERR(inode)) |
| 877 | return PTR_ERR(inode); |
| 878 | super->s_mapping_inode = inode; |
| 879 | mapping = inode->i_mapping; |
| 880 | mapping->a_ops = &mapping_aops; |
| 881 | /* Would it be possible to use __GFP_HIGHMEM as well? */ |
| 882 | mapping_set_gfp_mask(mapping, GFP_NOFS); |
| 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | int logfs_init_areas(struct super_block *sb) |
| 887 | { |
| 888 | struct logfs_super *super = logfs_super(sb); |
| 889 | int i = -1; |
| 890 | |
| 891 | super->s_alias_pool = mempool_create_kmalloc_pool(600, |
| 892 | sizeof(struct object_alias_item)); |
| 893 | if (!super->s_alias_pool) |
| 894 | return -ENOMEM; |
| 895 | |
| 896 | super->s_journal_area = alloc_area(sb); |
| 897 | if (!super->s_journal_area) |
| 898 | goto err; |
| 899 | |
| 900 | for_each_area(i) { |
| 901 | super->s_area[i] = alloc_area(sb); |
| 902 | if (!super->s_area[i]) |
| 903 | goto err; |
| 904 | super->s_area[i]->a_level = GC_LEVEL(i); |
| 905 | super->s_area[i]->a_ops = &ostore_area_ops; |
| 906 | } |
| 907 | btree_init_mempool128(&super->s_object_alias_tree, |
| 908 | super->s_btree_pool); |
| 909 | return 0; |
| 910 | |
| 911 | err: |
| 912 | for (i--; i >= 0; i--) |
| 913 | free_area(super->s_area[i]); |
| 914 | free_area(super->s_journal_area); |
Joern Engel | 1f1b000 | 2010-04-15 08:03:57 +0200 | [diff] [blame^] | 915 | logfs_mempool_destroy(super->s_alias_pool); |
Joern Engel | 5db53f3 | 2009-11-20 20:13:39 +0100 | [diff] [blame] | 916 | return -ENOMEM; |
| 917 | } |
| 918 | |
| 919 | void logfs_cleanup_areas(struct super_block *sb) |
| 920 | { |
| 921 | struct logfs_super *super = logfs_super(sb); |
| 922 | int i; |
| 923 | |
| 924 | btree_grim_visitor128(&super->s_object_alias_tree, 0, kill_alias); |
| 925 | for_each_area(i) |
| 926 | free_area(super->s_area[i]); |
| 927 | free_area(super->s_journal_area); |
| 928 | destroy_meta_inode(super->s_mapping_inode); |
| 929 | } |