bmap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. * bmap.c - NILFS block mapping.
  3. *
  4. * Copyright (C) 2006-2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Written by Koji Sato <koji@osrg.net>.
  21. */
  22. #include <linux/fs.h>
  23. #include <linux/string.h>
  24. #include <linux/errno.h>
  25. #include "nilfs.h"
  26. #include "bmap.h"
  27. #include "sb.h"
  28. #include "btnode.h"
  29. #include "mdt.h"
  30. #include "dat.h"
  31. #include "alloc.h"
  32. int nilfs_bmap_lookup_at_level(struct nilfs_bmap *bmap, __u64 key, int level,
  33. __u64 *ptrp)
  34. {
  35. __u64 ptr;
  36. int ret;
  37. down_read(&bmap->b_sem);
  38. ret = bmap->b_ops->bop_lookup(bmap, key, level, ptrp);
  39. if (ret < 0)
  40. goto out;
  41. if (bmap->b_pops->bpop_translate != NULL) {
  42. ret = bmap->b_pops->bpop_translate(bmap, *ptrp, &ptr);
  43. if (ret < 0)
  44. goto out;
  45. *ptrp = ptr;
  46. }
  47. out:
  48. up_read(&bmap->b_sem);
  49. return ret;
  50. }
  51. /**
  52. * nilfs_bmap_lookup - find a record
  53. * @bmap: bmap
  54. * @key: key
  55. * @recp: pointer to record
  56. *
  57. * Description: nilfs_bmap_lookup() finds a record whose key matches @key in
  58. * @bmap.
  59. *
  60. * Return Value: On success, 0 is returned and the record associated with @key
  61. * is stored in the place pointed by @recp. On error, one of the following
  62. * negative error codes is returned.
  63. *
  64. * %-EIO - I/O error.
  65. *
  66. * %-ENOMEM - Insufficient amount of memory available.
  67. *
  68. * %-ENOENT - A record associated with @key does not exist.
  69. */
  70. int nilfs_bmap_lookup(struct nilfs_bmap *bmap,
  71. unsigned long key,
  72. unsigned long *recp)
  73. {
  74. __u64 ptr;
  75. int ret;
  76. /* XXX: use macro for level 1 */
  77. ret = nilfs_bmap_lookup_at_level(bmap, key, 1, &ptr);
  78. if (recp != NULL)
  79. *recp = ptr;
  80. return ret;
  81. }
  82. static int nilfs_bmap_do_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
  83. {
  84. __u64 keys[NILFS_BMAP_SMALL_HIGH + 1];
  85. __u64 ptrs[NILFS_BMAP_SMALL_HIGH + 1];
  86. int ret, n;
  87. if (bmap->b_ops->bop_check_insert != NULL) {
  88. ret = bmap->b_ops->bop_check_insert(bmap, key);
  89. if (ret > 0) {
  90. n = bmap->b_ops->bop_gather_data(
  91. bmap, keys, ptrs, NILFS_BMAP_SMALL_HIGH + 1);
  92. if (n < 0)
  93. return n;
  94. ret = nilfs_btree_convert_and_insert(
  95. bmap, key, ptr, keys, ptrs, n,
  96. NILFS_BMAP_LARGE_LOW, NILFS_BMAP_LARGE_HIGH);
  97. if (ret == 0)
  98. bmap->b_u.u_flags |= NILFS_BMAP_LARGE;
  99. return ret;
  100. } else if (ret < 0)
  101. return ret;
  102. }
  103. return bmap->b_ops->bop_insert(bmap, key, ptr);
  104. }
  105. /**
  106. * nilfs_bmap_insert - insert a new key-record pair into a bmap
  107. * @bmap: bmap
  108. * @key: key
  109. * @rec: record
  110. *
  111. * Description: nilfs_bmap_insert() inserts the new key-record pair specified
  112. * by @key and @rec into @bmap.
  113. *
  114. * Return Value: On success, 0 is returned. On error, one of the following
  115. * negative error codes is returned.
  116. *
  117. * %-EIO - I/O error.
  118. *
  119. * %-ENOMEM - Insufficient amount of memory available.
  120. *
  121. * %-EEXIST - A record associated with @key already exist.
  122. */
  123. int nilfs_bmap_insert(struct nilfs_bmap *bmap,
  124. unsigned long key,
  125. unsigned long rec)
  126. {
  127. int ret;
  128. down_write(&bmap->b_sem);
  129. ret = nilfs_bmap_do_insert(bmap, key, rec);
  130. up_write(&bmap->b_sem);
  131. return ret;
  132. }
  133. static int nilfs_bmap_do_delete(struct nilfs_bmap *bmap, __u64 key)
  134. {
  135. __u64 keys[NILFS_BMAP_LARGE_LOW + 1];
  136. __u64 ptrs[NILFS_BMAP_LARGE_LOW + 1];
  137. int ret, n;
  138. if (bmap->b_ops->bop_check_delete != NULL) {
  139. ret = bmap->b_ops->bop_check_delete(bmap, key);
  140. if (ret > 0) {
  141. n = bmap->b_ops->bop_gather_data(
  142. bmap, keys, ptrs, NILFS_BMAP_LARGE_LOW + 1);
  143. if (n < 0)
  144. return n;
  145. ret = nilfs_direct_delete_and_convert(
  146. bmap, key, keys, ptrs, n,
  147. NILFS_BMAP_SMALL_LOW, NILFS_BMAP_SMALL_HIGH);
  148. if (ret == 0)
  149. bmap->b_u.u_flags &= ~NILFS_BMAP_LARGE;
  150. return ret;
  151. } else if (ret < 0)
  152. return ret;
  153. }
  154. return bmap->b_ops->bop_delete(bmap, key);
  155. }
  156. int nilfs_bmap_last_key(struct nilfs_bmap *bmap, unsigned long *key)
  157. {
  158. __u64 lastkey;
  159. int ret;
  160. down_read(&bmap->b_sem);
  161. ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
  162. if (!ret)
  163. *key = lastkey;
  164. up_read(&bmap->b_sem);
  165. return ret;
  166. }
  167. /**
  168. * nilfs_bmap_delete - delete a key-record pair from a bmap
  169. * @bmap: bmap
  170. * @key: key
  171. *
  172. * Description: nilfs_bmap_delete() deletes the key-record pair specified by
  173. * @key from @bmap.
  174. *
  175. * Return Value: On success, 0 is returned. On error, one of the following
  176. * negative error codes is returned.
  177. *
  178. * %-EIO - I/O error.
  179. *
  180. * %-ENOMEM - Insufficient amount of memory available.
  181. *
  182. * %-ENOENT - A record associated with @key does not exist.
  183. */
  184. int nilfs_bmap_delete(struct nilfs_bmap *bmap, unsigned long key)
  185. {
  186. int ret;
  187. down_write(&bmap->b_sem);
  188. ret = nilfs_bmap_do_delete(bmap, key);
  189. up_write(&bmap->b_sem);
  190. return ret;
  191. }
  192. static int nilfs_bmap_do_truncate(struct nilfs_bmap *bmap, unsigned long key)
  193. {
  194. __u64 lastkey;
  195. int ret;
  196. ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
  197. if (ret < 0) {
  198. if (ret == -ENOENT)
  199. ret = 0;
  200. return ret;
  201. }
  202. while (key <= lastkey) {
  203. ret = nilfs_bmap_do_delete(bmap, lastkey);
  204. if (ret < 0)
  205. return ret;
  206. ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
  207. if (ret < 0) {
  208. if (ret == -ENOENT)
  209. ret = 0;
  210. return ret;
  211. }
  212. }
  213. return 0;
  214. }
  215. /**
  216. * nilfs_bmap_truncate - truncate a bmap to a specified key
  217. * @bmap: bmap
  218. * @key: key
  219. *
  220. * Description: nilfs_bmap_truncate() removes key-record pairs whose keys are
  221. * greater than or equal to @key from @bmap.
  222. *
  223. * Return Value: On success, 0 is returned. On error, one of the following
  224. * negative error codes is returned.
  225. *
  226. * %-EIO - I/O error.
  227. *
  228. * %-ENOMEM - Insufficient amount of memory available.
  229. */
  230. int nilfs_bmap_truncate(struct nilfs_bmap *bmap, unsigned long key)
  231. {
  232. int ret;
  233. down_write(&bmap->b_sem);
  234. ret = nilfs_bmap_do_truncate(bmap, key);
  235. up_write(&bmap->b_sem);
  236. return ret;
  237. }
  238. /**
  239. * nilfs_bmap_clear - free resources a bmap holds
  240. * @bmap: bmap
  241. *
  242. * Description: nilfs_bmap_clear() frees resources associated with @bmap.
  243. */
  244. void nilfs_bmap_clear(struct nilfs_bmap *bmap)
  245. {
  246. down_write(&bmap->b_sem);
  247. if (bmap->b_ops->bop_clear != NULL)
  248. bmap->b_ops->bop_clear(bmap);
  249. up_write(&bmap->b_sem);
  250. }
  251. /**
  252. * nilfs_bmap_propagate - propagate dirty state
  253. * @bmap: bmap
  254. * @bh: buffer head
  255. *
  256. * Description: nilfs_bmap_propagate() marks the buffers that directly or
  257. * indirectly refer to the block specified by @bh dirty.
  258. *
  259. * Return Value: On success, 0 is returned. On error, one of the following
  260. * negative error codes is returned.
  261. *
  262. * %-EIO - I/O error.
  263. *
  264. * %-ENOMEM - Insufficient amount of memory available.
  265. */
  266. int nilfs_bmap_propagate(struct nilfs_bmap *bmap, struct buffer_head *bh)
  267. {
  268. int ret;
  269. down_write(&bmap->b_sem);
  270. ret = bmap->b_ops->bop_propagate(bmap, bh);
  271. up_write(&bmap->b_sem);
  272. return ret;
  273. }
  274. /**
  275. * nilfs_bmap_lookup_dirty_buffers -
  276. * @bmap: bmap
  277. * @listp: pointer to buffer head list
  278. */
  279. void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *bmap,
  280. struct list_head *listp)
  281. {
  282. if (bmap->b_ops->bop_lookup_dirty_buffers != NULL)
  283. bmap->b_ops->bop_lookup_dirty_buffers(bmap, listp);
  284. }
  285. /**
  286. * nilfs_bmap_assign - assign a new block number to a block
  287. * @bmap: bmap
  288. * @bhp: pointer to buffer head
  289. * @blocknr: block number
  290. * @binfo: block information
  291. *
  292. * Description: nilfs_bmap_assign() assigns the block number @blocknr to the
  293. * buffer specified by @bh.
  294. *
  295. * Return Value: On success, 0 is returned and the buffer head of a newly
  296. * create buffer and the block information associated with the buffer are
  297. * stored in the place pointed by @bh and @binfo, respectively. On error, one
  298. * of the following negative error codes is returned.
  299. *
  300. * %-EIO - I/O error.
  301. *
  302. * %-ENOMEM - Insufficient amount of memory available.
  303. */
  304. int nilfs_bmap_assign(struct nilfs_bmap *bmap,
  305. struct buffer_head **bh,
  306. unsigned long blocknr,
  307. union nilfs_binfo *binfo)
  308. {
  309. int ret;
  310. down_write(&bmap->b_sem);
  311. ret = bmap->b_ops->bop_assign(bmap, bh, blocknr, binfo);
  312. up_write(&bmap->b_sem);
  313. return ret;
  314. }
  315. /**
  316. * nilfs_bmap_mark - mark block dirty
  317. * @bmap: bmap
  318. * @key: key
  319. * @level: level
  320. *
  321. * Description: nilfs_bmap_mark() marks the block specified by @key and @level
  322. * as dirty.
  323. *
  324. * Return Value: On success, 0 is returned. On error, one of the following
  325. * negative error codes is returned.
  326. *
  327. * %-EIO - I/O error.
  328. *
  329. * %-ENOMEM - Insufficient amount of memory available.
  330. */
  331. int nilfs_bmap_mark(struct nilfs_bmap *bmap, __u64 key, int level)
  332. {
  333. int ret;
  334. if (bmap->b_ops->bop_mark == NULL)
  335. return 0;
  336. down_write(&bmap->b_sem);
  337. ret = bmap->b_ops->bop_mark(bmap, key, level);
  338. up_write(&bmap->b_sem);
  339. return ret;
  340. }
  341. /**
  342. * nilfs_bmap_test_and_clear_dirty - test and clear a bmap dirty state
  343. * @bmap: bmap
  344. *
  345. * Description: nilfs_test_and_clear() is the atomic operation to test and
  346. * clear the dirty state of @bmap.
  347. *
  348. * Return Value: 1 is returned if @bmap is dirty, or 0 if clear.
  349. */
  350. int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap *bmap)
  351. {
  352. int ret;
  353. down_write(&bmap->b_sem);
  354. ret = nilfs_bmap_dirty(bmap);
  355. nilfs_bmap_clear_dirty(bmap);
  356. up_write(&bmap->b_sem);
  357. return ret;
  358. }
  359. /*
  360. * Internal use only
  361. */
  362. void nilfs_bmap_add_blocks(const struct nilfs_bmap *bmap, int n)
  363. {
  364. inode_add_bytes(bmap->b_inode, (1 << bmap->b_inode->i_blkbits) * n);
  365. if (NILFS_MDT(bmap->b_inode))
  366. nilfs_mdt_mark_dirty(bmap->b_inode);
  367. else
  368. mark_inode_dirty(bmap->b_inode);
  369. }
  370. void nilfs_bmap_sub_blocks(const struct nilfs_bmap *bmap, int n)
  371. {
  372. inode_sub_bytes(bmap->b_inode, (1 << bmap->b_inode->i_blkbits) * n);
  373. if (NILFS_MDT(bmap->b_inode))
  374. nilfs_mdt_mark_dirty(bmap->b_inode);
  375. else
  376. mark_inode_dirty(bmap->b_inode);
  377. }
  378. int nilfs_bmap_get_block(const struct nilfs_bmap *bmap, __u64 ptr,
  379. struct buffer_head **bhp)
  380. {
  381. return nilfs_btnode_get(&NILFS_BMAP_I(bmap)->i_btnode_cache,
  382. ptr, 0, bhp, 0);
  383. }
  384. void nilfs_bmap_put_block(const struct nilfs_bmap *bmap,
  385. struct buffer_head *bh)
  386. {
  387. brelse(bh);
  388. }
  389. int nilfs_bmap_get_new_block(const struct nilfs_bmap *bmap, __u64 ptr,
  390. struct buffer_head **bhp)
  391. {
  392. int ret;
  393. ret = nilfs_btnode_get(&NILFS_BMAP_I(bmap)->i_btnode_cache,
  394. ptr, 0, bhp, 1);
  395. if (ret < 0)
  396. return ret;
  397. set_buffer_nilfs_volatile(*bhp);
  398. return 0;
  399. }
  400. void nilfs_bmap_delete_block(const struct nilfs_bmap *bmap,
  401. struct buffer_head *bh)
  402. {
  403. nilfs_btnode_delete(bh);
  404. }
  405. __u64 nilfs_bmap_data_get_key(const struct nilfs_bmap *bmap,
  406. const struct buffer_head *bh)
  407. {
  408. struct buffer_head *pbh;
  409. __u64 key;
  410. key = page_index(bh->b_page) << (PAGE_CACHE_SHIFT -
  411. bmap->b_inode->i_blkbits);
  412. for (pbh = page_buffers(bh->b_page); pbh != bh;
  413. pbh = pbh->b_this_page, key++);
  414. return key;
  415. }
  416. __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *bmap, __u64 key)
  417. {
  418. __s64 diff;
  419. diff = key - bmap->b_last_allocated_key;
  420. if ((nilfs_bmap_keydiff_abs(diff) < NILFS_INODE_BMAP_SIZE) &&
  421. (bmap->b_last_allocated_ptr != NILFS_BMAP_INVALID_PTR) &&
  422. (bmap->b_last_allocated_ptr + diff > 0))
  423. return bmap->b_last_allocated_ptr + diff;
  424. else
  425. return NILFS_BMAP_INVALID_PTR;
  426. }
  427. static struct inode *nilfs_bmap_get_dat(const struct nilfs_bmap *bmap)
  428. {
  429. return nilfs_dat_inode(NILFS_I_NILFS(bmap->b_inode));
  430. }
  431. #define NILFS_BMAP_GROUP_DIV 8
  432. __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *bmap)
  433. {
  434. struct inode *dat = nilfs_bmap_get_dat(bmap);
  435. unsigned long entries_per_group = nilfs_palloc_entries_per_group(dat);
  436. unsigned long group = bmap->b_inode->i_ino / entries_per_group;
  437. return group * entries_per_group +
  438. (bmap->b_inode->i_ino % NILFS_BMAP_GROUP_DIV) *
  439. (entries_per_group / NILFS_BMAP_GROUP_DIV);
  440. }
  441. static int nilfs_bmap_prepare_alloc_v(struct nilfs_bmap *bmap,
  442. union nilfs_bmap_ptr_req *req)
  443. {
  444. return nilfs_dat_prepare_alloc(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  445. }
  446. static void nilfs_bmap_commit_alloc_v(struct nilfs_bmap *bmap,
  447. union nilfs_bmap_ptr_req *req)
  448. {
  449. nilfs_dat_commit_alloc(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  450. }
  451. static void nilfs_bmap_abort_alloc_v(struct nilfs_bmap *bmap,
  452. union nilfs_bmap_ptr_req *req)
  453. {
  454. nilfs_dat_abort_alloc(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  455. }
  456. static int nilfs_bmap_prepare_start_v(struct nilfs_bmap *bmap,
  457. union nilfs_bmap_ptr_req *req)
  458. {
  459. return nilfs_dat_prepare_start(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  460. }
  461. static void nilfs_bmap_commit_start_v(struct nilfs_bmap *bmap,
  462. union nilfs_bmap_ptr_req *req,
  463. sector_t blocknr)
  464. {
  465. nilfs_dat_commit_start(nilfs_bmap_get_dat(bmap), &req->bpr_req,
  466. blocknr);
  467. }
  468. static void nilfs_bmap_abort_start_v(struct nilfs_bmap *bmap,
  469. union nilfs_bmap_ptr_req *req)
  470. {
  471. nilfs_dat_abort_start(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  472. }
  473. static int nilfs_bmap_prepare_end_v(struct nilfs_bmap *bmap,
  474. union nilfs_bmap_ptr_req *req)
  475. {
  476. return nilfs_dat_prepare_end(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  477. }
  478. static void nilfs_bmap_commit_end_v(struct nilfs_bmap *bmap,
  479. union nilfs_bmap_ptr_req *req)
  480. {
  481. nilfs_dat_commit_end(nilfs_bmap_get_dat(bmap), &req->bpr_req, 0);
  482. }
  483. static void nilfs_bmap_commit_end_vmdt(struct nilfs_bmap *bmap,
  484. union nilfs_bmap_ptr_req *req)
  485. {
  486. nilfs_dat_commit_end(nilfs_bmap_get_dat(bmap), &req->bpr_req, 1);
  487. }
  488. static void nilfs_bmap_abort_end_v(struct nilfs_bmap *bmap,
  489. union nilfs_bmap_ptr_req *req)
  490. {
  491. nilfs_dat_abort_end(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  492. }
  493. int nilfs_bmap_move_v(const struct nilfs_bmap *bmap, __u64 vblocknr,
  494. sector_t blocknr)
  495. {
  496. return nilfs_dat_move(nilfs_bmap_get_dat(bmap), vblocknr, blocknr);
  497. }
  498. int nilfs_bmap_mark_dirty(const struct nilfs_bmap *bmap, __u64 vblocknr)
  499. {
  500. return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(bmap), vblocknr);
  501. }
  502. int nilfs_bmap_prepare_update(struct nilfs_bmap *bmap,
  503. union nilfs_bmap_ptr_req *oldreq,
  504. union nilfs_bmap_ptr_req *newreq)
  505. {
  506. int ret;
  507. ret = bmap->b_pops->bpop_prepare_end_ptr(bmap, oldreq);
  508. if (ret < 0)
  509. return ret;
  510. ret = bmap->b_pops->bpop_prepare_alloc_ptr(bmap, newreq);
  511. if (ret < 0)
  512. bmap->b_pops->bpop_abort_end_ptr(bmap, oldreq);
  513. return ret;
  514. }
  515. void nilfs_bmap_commit_update(struct nilfs_bmap *bmap,
  516. union nilfs_bmap_ptr_req *oldreq,
  517. union nilfs_bmap_ptr_req *newreq)
  518. {
  519. bmap->b_pops->bpop_commit_end_ptr(bmap, oldreq);
  520. bmap->b_pops->bpop_commit_alloc_ptr(bmap, newreq);
  521. }
  522. void nilfs_bmap_abort_update(struct nilfs_bmap *bmap,
  523. union nilfs_bmap_ptr_req *oldreq,
  524. union nilfs_bmap_ptr_req *newreq)
  525. {
  526. bmap->b_pops->bpop_abort_end_ptr(bmap, oldreq);
  527. bmap->b_pops->bpop_abort_alloc_ptr(bmap, newreq);
  528. }
  529. static int nilfs_bmap_translate_v(const struct nilfs_bmap *bmap, __u64 ptr,
  530. __u64 *ptrp)
  531. {
  532. sector_t blocknr;
  533. int ret;
  534. ret = nilfs_dat_translate(nilfs_bmap_get_dat(bmap), ptr, &blocknr);
  535. if (ret < 0)
  536. return ret;
  537. if (ptrp != NULL)
  538. *ptrp = blocknr;
  539. return 0;
  540. }
  541. static int nilfs_bmap_prepare_alloc_p(struct nilfs_bmap *bmap,
  542. union nilfs_bmap_ptr_req *req)
  543. {
  544. /* ignore target ptr */
  545. req->bpr_ptr = bmap->b_last_allocated_ptr++;
  546. return 0;
  547. }
  548. static void nilfs_bmap_commit_alloc_p(struct nilfs_bmap *bmap,
  549. union nilfs_bmap_ptr_req *req)
  550. {
  551. /* do nothing */
  552. }
  553. static void nilfs_bmap_abort_alloc_p(struct nilfs_bmap *bmap,
  554. union nilfs_bmap_ptr_req *req)
  555. {
  556. bmap->b_last_allocated_ptr--;
  557. }
  558. static const struct nilfs_bmap_ptr_operations nilfs_bmap_ptr_ops_v = {
  559. .bpop_prepare_alloc_ptr = nilfs_bmap_prepare_alloc_v,
  560. .bpop_commit_alloc_ptr = nilfs_bmap_commit_alloc_v,
  561. .bpop_abort_alloc_ptr = nilfs_bmap_abort_alloc_v,
  562. .bpop_prepare_start_ptr = nilfs_bmap_prepare_start_v,
  563. .bpop_commit_start_ptr = nilfs_bmap_commit_start_v,
  564. .bpop_abort_start_ptr = nilfs_bmap_abort_start_v,
  565. .bpop_prepare_end_ptr = nilfs_bmap_prepare_end_v,
  566. .bpop_commit_end_ptr = nilfs_bmap_commit_end_v,
  567. .bpop_abort_end_ptr = nilfs_bmap_abort_end_v,
  568. .bpop_translate = nilfs_bmap_translate_v,
  569. };
  570. static const struct nilfs_bmap_ptr_operations nilfs_bmap_ptr_ops_vmdt = {
  571. .bpop_prepare_alloc_ptr = nilfs_bmap_prepare_alloc_v,
  572. .bpop_commit_alloc_ptr = nilfs_bmap_commit_alloc_v,
  573. .bpop_abort_alloc_ptr = nilfs_bmap_abort_alloc_v,
  574. .bpop_prepare_start_ptr = nilfs_bmap_prepare_start_v,
  575. .bpop_commit_start_ptr = nilfs_bmap_commit_start_v,
  576. .bpop_abort_start_ptr = nilfs_bmap_abort_start_v,
  577. .bpop_prepare_end_ptr = nilfs_bmap_prepare_end_v,
  578. .bpop_commit_end_ptr = nilfs_bmap_commit_end_vmdt,
  579. .bpop_abort_end_ptr = nilfs_bmap_abort_end_v,
  580. .bpop_translate = nilfs_bmap_translate_v,
  581. };
  582. static const struct nilfs_bmap_ptr_operations nilfs_bmap_ptr_ops_p = {
  583. .bpop_prepare_alloc_ptr = nilfs_bmap_prepare_alloc_p,
  584. .bpop_commit_alloc_ptr = nilfs_bmap_commit_alloc_p,
  585. .bpop_abort_alloc_ptr = nilfs_bmap_abort_alloc_p,
  586. .bpop_prepare_start_ptr = NULL,
  587. .bpop_commit_start_ptr = NULL,
  588. .bpop_abort_start_ptr = NULL,
  589. .bpop_prepare_end_ptr = NULL,
  590. .bpop_commit_end_ptr = NULL,
  591. .bpop_abort_end_ptr = NULL,
  592. .bpop_translate = NULL,
  593. };
  594. static const struct nilfs_bmap_ptr_operations nilfs_bmap_ptr_ops_gc = {
  595. .bpop_prepare_alloc_ptr = NULL,
  596. .bpop_commit_alloc_ptr = NULL,
  597. .bpop_abort_alloc_ptr = NULL,
  598. .bpop_prepare_start_ptr = NULL,
  599. .bpop_commit_start_ptr = NULL,
  600. .bpop_abort_start_ptr = NULL,
  601. .bpop_prepare_end_ptr = NULL,
  602. .bpop_commit_end_ptr = NULL,
  603. .bpop_abort_end_ptr = NULL,
  604. .bpop_translate = NULL,
  605. };
  606. static struct lock_class_key nilfs_bmap_dat_lock_key;
  607. /**
  608. * nilfs_bmap_read - read a bmap from an inode
  609. * @bmap: bmap
  610. * @raw_inode: on-disk inode
  611. *
  612. * Description: nilfs_bmap_read() initializes the bmap @bmap.
  613. *
  614. * Return Value: On success, 0 is returned. On error, the following negative
  615. * error code is returned.
  616. *
  617. * %-ENOMEM - Insufficient amount of memory available.
  618. */
  619. int nilfs_bmap_read(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
  620. {
  621. if (raw_inode == NULL)
  622. memset(bmap->b_u.u_data, 0, NILFS_BMAP_SIZE);
  623. else
  624. memcpy(bmap->b_u.u_data, raw_inode->i_bmap, NILFS_BMAP_SIZE);
  625. init_rwsem(&bmap->b_sem);
  626. bmap->b_state = 0;
  627. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  628. switch (bmap->b_inode->i_ino) {
  629. case NILFS_DAT_INO:
  630. bmap->b_pops = &nilfs_bmap_ptr_ops_p;
  631. bmap->b_last_allocated_key = 0; /* XXX: use macro */
  632. bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
  633. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
  634. break;
  635. case NILFS_CPFILE_INO:
  636. case NILFS_SUFILE_INO:
  637. bmap->b_pops = &nilfs_bmap_ptr_ops_vmdt;
  638. bmap->b_last_allocated_key = 0; /* XXX: use macro */
  639. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  640. break;
  641. default:
  642. bmap->b_pops = &nilfs_bmap_ptr_ops_v;
  643. bmap->b_last_allocated_key = 0; /* XXX: use macro */
  644. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  645. break;
  646. }
  647. return (bmap->b_u.u_flags & NILFS_BMAP_LARGE) ?
  648. nilfs_btree_init(bmap,
  649. NILFS_BMAP_LARGE_LOW,
  650. NILFS_BMAP_LARGE_HIGH) :
  651. nilfs_direct_init(bmap,
  652. NILFS_BMAP_SMALL_LOW,
  653. NILFS_BMAP_SMALL_HIGH);
  654. }
  655. /**
  656. * nilfs_bmap_write - write back a bmap to an inode
  657. * @bmap: bmap
  658. * @raw_inode: on-disk inode
  659. *
  660. * Description: nilfs_bmap_write() stores @bmap in @raw_inode.
  661. */
  662. void nilfs_bmap_write(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
  663. {
  664. down_write(&bmap->b_sem);
  665. memcpy(raw_inode->i_bmap, bmap->b_u.u_data,
  666. NILFS_INODE_BMAP_SIZE * sizeof(__le64));
  667. if (bmap->b_inode->i_ino == NILFS_DAT_INO)
  668. bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
  669. up_write(&bmap->b_sem);
  670. }
  671. void nilfs_bmap_init_gc(struct nilfs_bmap *bmap)
  672. {
  673. memset(&bmap->b_u, 0, NILFS_BMAP_SIZE);
  674. init_rwsem(&bmap->b_sem);
  675. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  676. bmap->b_pops = &nilfs_bmap_ptr_ops_gc;
  677. bmap->b_last_allocated_key = 0;
  678. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  679. bmap->b_state = 0;
  680. nilfs_btree_init_gc(bmap);
  681. }
  682. void nilfs_bmap_init_gcdat(struct nilfs_bmap *gcbmap, struct nilfs_bmap *bmap)
  683. {
  684. memcpy(gcbmap, bmap, sizeof(union nilfs_bmap_union));
  685. init_rwsem(&gcbmap->b_sem);
  686. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
  687. gcbmap->b_inode = &NILFS_BMAP_I(gcbmap)->vfs_inode;
  688. }
  689. void nilfs_bmap_commit_gcdat(struct nilfs_bmap *gcbmap, struct nilfs_bmap *bmap)
  690. {
  691. memcpy(bmap, gcbmap, sizeof(union nilfs_bmap_union));
  692. init_rwsem(&bmap->b_sem);
  693. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
  694. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  695. }