bmap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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. if (ret == 0)
  97. bmap->b_u.u_flags |= NILFS_BMAP_LARGE;
  98. return ret;
  99. } else if (ret < 0)
  100. return ret;
  101. }
  102. return bmap->b_ops->bop_insert(bmap, key, ptr);
  103. }
  104. /**
  105. * nilfs_bmap_insert - insert a new key-record pair into a bmap
  106. * @bmap: bmap
  107. * @key: key
  108. * @rec: record
  109. *
  110. * Description: nilfs_bmap_insert() inserts the new key-record pair specified
  111. * by @key and @rec into @bmap.
  112. *
  113. * Return Value: On success, 0 is returned. On error, one of the following
  114. * negative error codes is returned.
  115. *
  116. * %-EIO - I/O error.
  117. *
  118. * %-ENOMEM - Insufficient amount of memory available.
  119. *
  120. * %-EEXIST - A record associated with @key already exist.
  121. */
  122. int nilfs_bmap_insert(struct nilfs_bmap *bmap,
  123. unsigned long key,
  124. unsigned long rec)
  125. {
  126. int ret;
  127. down_write(&bmap->b_sem);
  128. ret = nilfs_bmap_do_insert(bmap, key, rec);
  129. up_write(&bmap->b_sem);
  130. return ret;
  131. }
  132. static int nilfs_bmap_do_delete(struct nilfs_bmap *bmap, __u64 key)
  133. {
  134. __u64 keys[NILFS_BMAP_LARGE_LOW + 1];
  135. __u64 ptrs[NILFS_BMAP_LARGE_LOW + 1];
  136. int ret, n;
  137. if (bmap->b_ops->bop_check_delete != NULL) {
  138. ret = bmap->b_ops->bop_check_delete(bmap, key);
  139. if (ret > 0) {
  140. n = bmap->b_ops->bop_gather_data(
  141. bmap, keys, ptrs, NILFS_BMAP_LARGE_LOW + 1);
  142. if (n < 0)
  143. return n;
  144. ret = nilfs_direct_delete_and_convert(
  145. bmap, key, keys, ptrs, n);
  146. if (ret == 0)
  147. bmap->b_u.u_flags &= ~NILFS_BMAP_LARGE;
  148. return ret;
  149. } else if (ret < 0)
  150. return ret;
  151. }
  152. return bmap->b_ops->bop_delete(bmap, key);
  153. }
  154. int nilfs_bmap_last_key(struct nilfs_bmap *bmap, unsigned long *key)
  155. {
  156. __u64 lastkey;
  157. int ret;
  158. down_read(&bmap->b_sem);
  159. ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
  160. if (!ret)
  161. *key = lastkey;
  162. up_read(&bmap->b_sem);
  163. return ret;
  164. }
  165. /**
  166. * nilfs_bmap_delete - delete a key-record pair from a bmap
  167. * @bmap: bmap
  168. * @key: key
  169. *
  170. * Description: nilfs_bmap_delete() deletes the key-record pair specified by
  171. * @key from @bmap.
  172. *
  173. * Return Value: On success, 0 is returned. On error, one of the following
  174. * negative error codes is returned.
  175. *
  176. * %-EIO - I/O error.
  177. *
  178. * %-ENOMEM - Insufficient amount of memory available.
  179. *
  180. * %-ENOENT - A record associated with @key does not exist.
  181. */
  182. int nilfs_bmap_delete(struct nilfs_bmap *bmap, unsigned long key)
  183. {
  184. int ret;
  185. down_write(&bmap->b_sem);
  186. ret = nilfs_bmap_do_delete(bmap, key);
  187. up_write(&bmap->b_sem);
  188. return ret;
  189. }
  190. static int nilfs_bmap_do_truncate(struct nilfs_bmap *bmap, unsigned long key)
  191. {
  192. __u64 lastkey;
  193. int ret;
  194. ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
  195. if (ret < 0) {
  196. if (ret == -ENOENT)
  197. ret = 0;
  198. return ret;
  199. }
  200. while (key <= lastkey) {
  201. ret = nilfs_bmap_do_delete(bmap, lastkey);
  202. if (ret < 0)
  203. return ret;
  204. ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
  205. if (ret < 0) {
  206. if (ret == -ENOENT)
  207. ret = 0;
  208. return ret;
  209. }
  210. }
  211. return 0;
  212. }
  213. /**
  214. * nilfs_bmap_truncate - truncate a bmap to a specified key
  215. * @bmap: bmap
  216. * @key: key
  217. *
  218. * Description: nilfs_bmap_truncate() removes key-record pairs whose keys are
  219. * greater than or equal to @key from @bmap.
  220. *
  221. * Return Value: On success, 0 is returned. On error, one of the following
  222. * negative error codes is returned.
  223. *
  224. * %-EIO - I/O error.
  225. *
  226. * %-ENOMEM - Insufficient amount of memory available.
  227. */
  228. int nilfs_bmap_truncate(struct nilfs_bmap *bmap, unsigned long key)
  229. {
  230. int ret;
  231. down_write(&bmap->b_sem);
  232. ret = nilfs_bmap_do_truncate(bmap, key);
  233. up_write(&bmap->b_sem);
  234. return ret;
  235. }
  236. /**
  237. * nilfs_bmap_clear - free resources a bmap holds
  238. * @bmap: bmap
  239. *
  240. * Description: nilfs_bmap_clear() frees resources associated with @bmap.
  241. */
  242. void nilfs_bmap_clear(struct nilfs_bmap *bmap)
  243. {
  244. down_write(&bmap->b_sem);
  245. if (bmap->b_ops->bop_clear != NULL)
  246. bmap->b_ops->bop_clear(bmap);
  247. up_write(&bmap->b_sem);
  248. }
  249. /**
  250. * nilfs_bmap_propagate - propagate dirty state
  251. * @bmap: bmap
  252. * @bh: buffer head
  253. *
  254. * Description: nilfs_bmap_propagate() marks the buffers that directly or
  255. * indirectly refer to the block specified by @bh dirty.
  256. *
  257. * Return Value: On success, 0 is returned. On error, one of the following
  258. * negative error codes is returned.
  259. *
  260. * %-EIO - I/O error.
  261. *
  262. * %-ENOMEM - Insufficient amount of memory available.
  263. */
  264. int nilfs_bmap_propagate(struct nilfs_bmap *bmap, struct buffer_head *bh)
  265. {
  266. int ret;
  267. down_write(&bmap->b_sem);
  268. ret = bmap->b_ops->bop_propagate(bmap, bh);
  269. up_write(&bmap->b_sem);
  270. return ret;
  271. }
  272. /**
  273. * nilfs_bmap_lookup_dirty_buffers -
  274. * @bmap: bmap
  275. * @listp: pointer to buffer head list
  276. */
  277. void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *bmap,
  278. struct list_head *listp)
  279. {
  280. if (bmap->b_ops->bop_lookup_dirty_buffers != NULL)
  281. bmap->b_ops->bop_lookup_dirty_buffers(bmap, listp);
  282. }
  283. /**
  284. * nilfs_bmap_assign - assign a new block number to a block
  285. * @bmap: bmap
  286. * @bhp: pointer to buffer head
  287. * @blocknr: block number
  288. * @binfo: block information
  289. *
  290. * Description: nilfs_bmap_assign() assigns the block number @blocknr to the
  291. * buffer specified by @bh.
  292. *
  293. * Return Value: On success, 0 is returned and the buffer head of a newly
  294. * create buffer and the block information associated with the buffer are
  295. * stored in the place pointed by @bh and @binfo, respectively. On error, one
  296. * of the following negative error codes is returned.
  297. *
  298. * %-EIO - I/O error.
  299. *
  300. * %-ENOMEM - Insufficient amount of memory available.
  301. */
  302. int nilfs_bmap_assign(struct nilfs_bmap *bmap,
  303. struct buffer_head **bh,
  304. unsigned long blocknr,
  305. union nilfs_binfo *binfo)
  306. {
  307. int ret;
  308. down_write(&bmap->b_sem);
  309. ret = bmap->b_ops->bop_assign(bmap, bh, blocknr, binfo);
  310. up_write(&bmap->b_sem);
  311. return ret;
  312. }
  313. /**
  314. * nilfs_bmap_mark - mark block dirty
  315. * @bmap: bmap
  316. * @key: key
  317. * @level: level
  318. *
  319. * Description: nilfs_bmap_mark() marks the block specified by @key and @level
  320. * as dirty.
  321. *
  322. * Return Value: On success, 0 is returned. On error, one of the following
  323. * negative error codes is returned.
  324. *
  325. * %-EIO - I/O error.
  326. *
  327. * %-ENOMEM - Insufficient amount of memory available.
  328. */
  329. int nilfs_bmap_mark(struct nilfs_bmap *bmap, __u64 key, int level)
  330. {
  331. int ret;
  332. if (bmap->b_ops->bop_mark == NULL)
  333. return 0;
  334. down_write(&bmap->b_sem);
  335. ret = bmap->b_ops->bop_mark(bmap, key, level);
  336. up_write(&bmap->b_sem);
  337. return ret;
  338. }
  339. /**
  340. * nilfs_bmap_test_and_clear_dirty - test and clear a bmap dirty state
  341. * @bmap: bmap
  342. *
  343. * Description: nilfs_test_and_clear() is the atomic operation to test and
  344. * clear the dirty state of @bmap.
  345. *
  346. * Return Value: 1 is returned if @bmap is dirty, or 0 if clear.
  347. */
  348. int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap *bmap)
  349. {
  350. int ret;
  351. down_write(&bmap->b_sem);
  352. ret = nilfs_bmap_dirty(bmap);
  353. nilfs_bmap_clear_dirty(bmap);
  354. up_write(&bmap->b_sem);
  355. return ret;
  356. }
  357. /*
  358. * Internal use only
  359. */
  360. void nilfs_bmap_add_blocks(const struct nilfs_bmap *bmap, int n)
  361. {
  362. inode_add_bytes(bmap->b_inode, (1 << bmap->b_inode->i_blkbits) * n);
  363. if (NILFS_MDT(bmap->b_inode))
  364. nilfs_mdt_mark_dirty(bmap->b_inode);
  365. else
  366. mark_inode_dirty(bmap->b_inode);
  367. }
  368. void nilfs_bmap_sub_blocks(const struct nilfs_bmap *bmap, int n)
  369. {
  370. inode_sub_bytes(bmap->b_inode, (1 << bmap->b_inode->i_blkbits) * n);
  371. if (NILFS_MDT(bmap->b_inode))
  372. nilfs_mdt_mark_dirty(bmap->b_inode);
  373. else
  374. mark_inode_dirty(bmap->b_inode);
  375. }
  376. __u64 nilfs_bmap_data_get_key(const struct nilfs_bmap *bmap,
  377. const struct buffer_head *bh)
  378. {
  379. struct buffer_head *pbh;
  380. __u64 key;
  381. key = page_index(bh->b_page) << (PAGE_CACHE_SHIFT -
  382. bmap->b_inode->i_blkbits);
  383. for (pbh = page_buffers(bh->b_page); pbh != bh;
  384. pbh = pbh->b_this_page, key++);
  385. return key;
  386. }
  387. __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *bmap, __u64 key)
  388. {
  389. __s64 diff;
  390. diff = key - bmap->b_last_allocated_key;
  391. if ((nilfs_bmap_keydiff_abs(diff) < NILFS_INODE_BMAP_SIZE) &&
  392. (bmap->b_last_allocated_ptr != NILFS_BMAP_INVALID_PTR) &&
  393. (bmap->b_last_allocated_ptr + diff > 0))
  394. return bmap->b_last_allocated_ptr + diff;
  395. else
  396. return NILFS_BMAP_INVALID_PTR;
  397. }
  398. static struct inode *nilfs_bmap_get_dat(const struct nilfs_bmap *bmap)
  399. {
  400. return nilfs_dat_inode(NILFS_I_NILFS(bmap->b_inode));
  401. }
  402. #define NILFS_BMAP_GROUP_DIV 8
  403. __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *bmap)
  404. {
  405. struct inode *dat = nilfs_bmap_get_dat(bmap);
  406. unsigned long entries_per_group = nilfs_palloc_entries_per_group(dat);
  407. unsigned long group = bmap->b_inode->i_ino / entries_per_group;
  408. return group * entries_per_group +
  409. (bmap->b_inode->i_ino % NILFS_BMAP_GROUP_DIV) *
  410. (entries_per_group / NILFS_BMAP_GROUP_DIV);
  411. }
  412. static int nilfs_bmap_prepare_alloc_v(struct nilfs_bmap *bmap,
  413. union nilfs_bmap_ptr_req *req)
  414. {
  415. return nilfs_dat_prepare_alloc(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  416. }
  417. static void nilfs_bmap_commit_alloc_v(struct nilfs_bmap *bmap,
  418. union nilfs_bmap_ptr_req *req)
  419. {
  420. nilfs_dat_commit_alloc(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  421. }
  422. static void nilfs_bmap_abort_alloc_v(struct nilfs_bmap *bmap,
  423. union nilfs_bmap_ptr_req *req)
  424. {
  425. nilfs_dat_abort_alloc(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  426. }
  427. int nilfs_bmap_start_v(struct nilfs_bmap *bmap, union nilfs_bmap_ptr_req *req,
  428. sector_t blocknr)
  429. {
  430. struct inode *dat = nilfs_bmap_get_dat(bmap);
  431. int ret;
  432. ret = nilfs_dat_prepare_start(dat, &req->bpr_req);
  433. if (likely(!ret))
  434. nilfs_dat_commit_start(dat, &req->bpr_req, blocknr);
  435. return ret;
  436. }
  437. static int nilfs_bmap_prepare_end_v(struct nilfs_bmap *bmap,
  438. union nilfs_bmap_ptr_req *req)
  439. {
  440. return nilfs_dat_prepare_end(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  441. }
  442. static void nilfs_bmap_commit_end_v(struct nilfs_bmap *bmap,
  443. union nilfs_bmap_ptr_req *req)
  444. {
  445. nilfs_dat_commit_end(nilfs_bmap_get_dat(bmap), &req->bpr_req, 0);
  446. }
  447. static void nilfs_bmap_commit_end_vmdt(struct nilfs_bmap *bmap,
  448. union nilfs_bmap_ptr_req *req)
  449. {
  450. nilfs_dat_commit_end(nilfs_bmap_get_dat(bmap), &req->bpr_req, 1);
  451. }
  452. static void nilfs_bmap_abort_end_v(struct nilfs_bmap *bmap,
  453. union nilfs_bmap_ptr_req *req)
  454. {
  455. nilfs_dat_abort_end(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  456. }
  457. int nilfs_bmap_move_v(const struct nilfs_bmap *bmap, __u64 vblocknr,
  458. sector_t blocknr)
  459. {
  460. return nilfs_dat_move(nilfs_bmap_get_dat(bmap), vblocknr, blocknr);
  461. }
  462. int nilfs_bmap_mark_dirty(const struct nilfs_bmap *bmap, __u64 vblocknr)
  463. {
  464. return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(bmap), vblocknr);
  465. }
  466. int nilfs_bmap_prepare_update(struct nilfs_bmap *bmap,
  467. union nilfs_bmap_ptr_req *oldreq,
  468. union nilfs_bmap_ptr_req *newreq)
  469. {
  470. int ret;
  471. ret = bmap->b_pops->bpop_prepare_end_ptr(bmap, oldreq);
  472. if (ret < 0)
  473. return ret;
  474. ret = bmap->b_pops->bpop_prepare_alloc_ptr(bmap, newreq);
  475. if (ret < 0)
  476. bmap->b_pops->bpop_abort_end_ptr(bmap, oldreq);
  477. return ret;
  478. }
  479. void nilfs_bmap_commit_update(struct nilfs_bmap *bmap,
  480. union nilfs_bmap_ptr_req *oldreq,
  481. union nilfs_bmap_ptr_req *newreq)
  482. {
  483. bmap->b_pops->bpop_commit_end_ptr(bmap, oldreq);
  484. bmap->b_pops->bpop_commit_alloc_ptr(bmap, newreq);
  485. }
  486. void nilfs_bmap_abort_update(struct nilfs_bmap *bmap,
  487. union nilfs_bmap_ptr_req *oldreq,
  488. union nilfs_bmap_ptr_req *newreq)
  489. {
  490. bmap->b_pops->bpop_abort_end_ptr(bmap, oldreq);
  491. bmap->b_pops->bpop_abort_alloc_ptr(bmap, newreq);
  492. }
  493. static int nilfs_bmap_translate_v(const struct nilfs_bmap *bmap, __u64 ptr,
  494. __u64 *ptrp)
  495. {
  496. sector_t blocknr;
  497. int ret;
  498. ret = nilfs_dat_translate(nilfs_bmap_get_dat(bmap), ptr, &blocknr);
  499. if (ret < 0)
  500. return ret;
  501. if (ptrp != NULL)
  502. *ptrp = blocknr;
  503. return 0;
  504. }
  505. static int nilfs_bmap_prepare_alloc_p(struct nilfs_bmap *bmap,
  506. union nilfs_bmap_ptr_req *req)
  507. {
  508. /* ignore target ptr */
  509. req->bpr_ptr = bmap->b_last_allocated_ptr++;
  510. return 0;
  511. }
  512. static void nilfs_bmap_commit_alloc_p(struct nilfs_bmap *bmap,
  513. union nilfs_bmap_ptr_req *req)
  514. {
  515. /* do nothing */
  516. }
  517. static void nilfs_bmap_abort_alloc_p(struct nilfs_bmap *bmap,
  518. union nilfs_bmap_ptr_req *req)
  519. {
  520. bmap->b_last_allocated_ptr--;
  521. }
  522. static const struct nilfs_bmap_ptr_operations nilfs_bmap_ptr_ops_v = {
  523. .bpop_prepare_alloc_ptr = nilfs_bmap_prepare_alloc_v,
  524. .bpop_commit_alloc_ptr = nilfs_bmap_commit_alloc_v,
  525. .bpop_abort_alloc_ptr = nilfs_bmap_abort_alloc_v,
  526. .bpop_prepare_end_ptr = nilfs_bmap_prepare_end_v,
  527. .bpop_commit_end_ptr = nilfs_bmap_commit_end_v,
  528. .bpop_abort_end_ptr = nilfs_bmap_abort_end_v,
  529. .bpop_translate = nilfs_bmap_translate_v,
  530. };
  531. static const struct nilfs_bmap_ptr_operations nilfs_bmap_ptr_ops_vmdt = {
  532. .bpop_prepare_alloc_ptr = nilfs_bmap_prepare_alloc_v,
  533. .bpop_commit_alloc_ptr = nilfs_bmap_commit_alloc_v,
  534. .bpop_abort_alloc_ptr = nilfs_bmap_abort_alloc_v,
  535. .bpop_prepare_end_ptr = nilfs_bmap_prepare_end_v,
  536. .bpop_commit_end_ptr = nilfs_bmap_commit_end_vmdt,
  537. .bpop_abort_end_ptr = nilfs_bmap_abort_end_v,
  538. .bpop_translate = nilfs_bmap_translate_v,
  539. };
  540. static const struct nilfs_bmap_ptr_operations nilfs_bmap_ptr_ops_p = {
  541. .bpop_prepare_alloc_ptr = nilfs_bmap_prepare_alloc_p,
  542. .bpop_commit_alloc_ptr = nilfs_bmap_commit_alloc_p,
  543. .bpop_abort_alloc_ptr = nilfs_bmap_abort_alloc_p,
  544. .bpop_prepare_end_ptr = NULL,
  545. .bpop_commit_end_ptr = NULL,
  546. .bpop_abort_end_ptr = NULL,
  547. .bpop_translate = NULL,
  548. };
  549. static const struct nilfs_bmap_ptr_operations nilfs_bmap_ptr_ops_gc = {
  550. .bpop_prepare_alloc_ptr = NULL,
  551. .bpop_commit_alloc_ptr = NULL,
  552. .bpop_abort_alloc_ptr = NULL,
  553. .bpop_prepare_end_ptr = NULL,
  554. .bpop_commit_end_ptr = NULL,
  555. .bpop_abort_end_ptr = NULL,
  556. .bpop_translate = NULL,
  557. };
  558. static struct lock_class_key nilfs_bmap_dat_lock_key;
  559. /**
  560. * nilfs_bmap_read - read a bmap from an inode
  561. * @bmap: bmap
  562. * @raw_inode: on-disk inode
  563. *
  564. * Description: nilfs_bmap_read() initializes the bmap @bmap.
  565. *
  566. * Return Value: On success, 0 is returned. On error, the following negative
  567. * error code is returned.
  568. *
  569. * %-ENOMEM - Insufficient amount of memory available.
  570. */
  571. int nilfs_bmap_read(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
  572. {
  573. if (raw_inode == NULL)
  574. memset(bmap->b_u.u_data, 0, NILFS_BMAP_SIZE);
  575. else
  576. memcpy(bmap->b_u.u_data, raw_inode->i_bmap, NILFS_BMAP_SIZE);
  577. init_rwsem(&bmap->b_sem);
  578. bmap->b_state = 0;
  579. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  580. switch (bmap->b_inode->i_ino) {
  581. case NILFS_DAT_INO:
  582. bmap->b_pops = &nilfs_bmap_ptr_ops_p;
  583. bmap->b_last_allocated_key = 0; /* XXX: use macro */
  584. bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
  585. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
  586. break;
  587. case NILFS_CPFILE_INO:
  588. case NILFS_SUFILE_INO:
  589. bmap->b_pops = &nilfs_bmap_ptr_ops_vmdt;
  590. bmap->b_last_allocated_key = 0; /* XXX: use macro */
  591. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  592. break;
  593. default:
  594. bmap->b_pops = &nilfs_bmap_ptr_ops_v;
  595. bmap->b_last_allocated_key = 0; /* XXX: use macro */
  596. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  597. break;
  598. }
  599. return (bmap->b_u.u_flags & NILFS_BMAP_LARGE) ?
  600. nilfs_btree_init(bmap) : nilfs_direct_init(bmap);
  601. }
  602. /**
  603. * nilfs_bmap_write - write back a bmap to an inode
  604. * @bmap: bmap
  605. * @raw_inode: on-disk inode
  606. *
  607. * Description: nilfs_bmap_write() stores @bmap in @raw_inode.
  608. */
  609. void nilfs_bmap_write(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
  610. {
  611. down_write(&bmap->b_sem);
  612. memcpy(raw_inode->i_bmap, bmap->b_u.u_data,
  613. NILFS_INODE_BMAP_SIZE * sizeof(__le64));
  614. if (bmap->b_inode->i_ino == NILFS_DAT_INO)
  615. bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
  616. up_write(&bmap->b_sem);
  617. }
  618. void nilfs_bmap_init_gc(struct nilfs_bmap *bmap)
  619. {
  620. memset(&bmap->b_u, 0, NILFS_BMAP_SIZE);
  621. init_rwsem(&bmap->b_sem);
  622. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  623. bmap->b_pops = &nilfs_bmap_ptr_ops_gc;
  624. bmap->b_last_allocated_key = 0;
  625. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  626. bmap->b_state = 0;
  627. nilfs_btree_init_gc(bmap);
  628. }
  629. void nilfs_bmap_init_gcdat(struct nilfs_bmap *gcbmap, struct nilfs_bmap *bmap)
  630. {
  631. memcpy(gcbmap, bmap, sizeof(union nilfs_bmap_union));
  632. init_rwsem(&gcbmap->b_sem);
  633. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
  634. gcbmap->b_inode = &NILFS_BMAP_I(gcbmap)->vfs_inode;
  635. }
  636. void nilfs_bmap_commit_gcdat(struct nilfs_bmap *gcbmap, struct nilfs_bmap *bmap)
  637. {
  638. memcpy(bmap, gcbmap, sizeof(union nilfs_bmap_union));
  639. init_rwsem(&bmap->b_sem);
  640. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
  641. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  642. }