segment.c 24 KB

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