segment.c 24 KB

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