bmap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. __u64 nilfs_bmap_data_get_key(const struct nilfs_bmap *bmap,
  379. const struct buffer_head *bh)
  380. {
  381. struct buffer_head *pbh;
  382. __u64 key;
  383. key = page_index(bh->b_page) << (PAGE_CACHE_SHIFT -
  384. bmap->b_inode->i_blkbits);
  385. for (pbh = page_buffers(bh->b_page); pbh != bh;
  386. pbh = pbh->b_this_page, key++);
  387. return key;
  388. }
  389. __u64 nilfs_bmap_find_target_seq(const struct nilfs_bmap *bmap, __u64 key)
  390. {
  391. __s64 diff;
  392. diff = key - bmap->b_last_allocated_key;
  393. if ((nilfs_bmap_keydiff_abs(diff) < NILFS_INODE_BMAP_SIZE) &&
  394. (bmap->b_last_allocated_ptr != NILFS_BMAP_INVALID_PTR) &&
  395. (bmap->b_last_allocated_ptr + diff > 0))
  396. return bmap->b_last_allocated_ptr + diff;
  397. else
  398. return NILFS_BMAP_INVALID_PTR;
  399. }
  400. static struct inode *nilfs_bmap_get_dat(const struct nilfs_bmap *bmap)
  401. {
  402. return nilfs_dat_inode(NILFS_I_NILFS(bmap->b_inode));
  403. }
  404. #define NILFS_BMAP_GROUP_DIV 8
  405. __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *bmap)
  406. {
  407. struct inode *dat = nilfs_bmap_get_dat(bmap);
  408. unsigned long entries_per_group = nilfs_palloc_entries_per_group(dat);
  409. unsigned long group = bmap->b_inode->i_ino / entries_per_group;
  410. return group * entries_per_group +
  411. (bmap->b_inode->i_ino % NILFS_BMAP_GROUP_DIV) *
  412. (entries_per_group / NILFS_BMAP_GROUP_DIV);
  413. }
  414. static int nilfs_bmap_prepare_alloc_v(struct nilfs_bmap *bmap,
  415. union nilfs_bmap_ptr_req *req)
  416. {
  417. return nilfs_dat_prepare_alloc(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  418. }
  419. static void nilfs_bmap_commit_alloc_v(struct nilfs_bmap *bmap,
  420. union nilfs_bmap_ptr_req *req)
  421. {
  422. nilfs_dat_commit_alloc(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  423. }
  424. static void nilfs_bmap_abort_alloc_v(struct nilfs_bmap *bmap,
  425. union nilfs_bmap_ptr_req *req)
  426. {
  427. nilfs_dat_abort_alloc(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  428. }
  429. int nilfs_bmap_start_v(struct nilfs_bmap *bmap, union nilfs_bmap_ptr_req *req,
  430. sector_t blocknr)
  431. {
  432. struct inode *dat = nilfs_bmap_get_dat(bmap);
  433. int ret;
  434. ret = nilfs_dat_prepare_start(dat, &req->bpr_req);
  435. if (likely(!ret))
  436. nilfs_dat_commit_start(dat, &req->bpr_req, blocknr);
  437. return ret;
  438. }
  439. static int nilfs_bmap_prepare_end_v(struct nilfs_bmap *bmap,
  440. union nilfs_bmap_ptr_req *req)
  441. {
  442. return nilfs_dat_prepare_end(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  443. }
  444. static void nilfs_bmap_commit_end_v(struct nilfs_bmap *bmap,
  445. union nilfs_bmap_ptr_req *req)
  446. {
  447. nilfs_dat_commit_end(nilfs_bmap_get_dat(bmap), &req->bpr_req, 0);
  448. }
  449. static void nilfs_bmap_commit_end_vmdt(struct nilfs_bmap *bmap,
  450. union nilfs_bmap_ptr_req *req)
  451. {
  452. nilfs_dat_commit_end(nilfs_bmap_get_dat(bmap), &req->bpr_req, 1);
  453. }
  454. static void nilfs_bmap_abort_end_v(struct nilfs_bmap *bmap,
  455. union nilfs_bmap_ptr_req *req)
  456. {
  457. nilfs_dat_abort_end(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  458. }
  459. int nilfs_bmap_move_v(const struct nilfs_bmap *bmap, __u64 vblocknr,
  460. sector_t blocknr)
  461. {
  462. return nilfs_dat_move(nilfs_bmap_get_dat(bmap), vblocknr, blocknr);
  463. }
  464. int nilfs_bmap_mark_dirty(const struct nilfs_bmap *bmap, __u64 vblocknr)
  465. {
  466. return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(bmap), vblocknr);
  467. }
  468. int nilfs_bmap_prepare_update(struct nilfs_bmap *bmap,
  469. union nilfs_bmap_ptr_req *oldreq,
  470. union nilfs_bmap_ptr_req *newreq)
  471. {
  472. int ret;
  473. ret = bmap->b_pops->bpop_prepare_end_ptr(bmap, oldreq);
  474. if (ret < 0)
  475. return ret;
  476. ret = bmap->b_pops->bpop_prepare_alloc_ptr(bmap, newreq);
  477. if (ret < 0)
  478. bmap->b_pops->bpop_abort_end_ptr(bmap, oldreq);
  479. return ret;
  480. }
  481. void nilfs_bmap_commit_update(struct nilfs_bmap *bmap,
  482. union nilfs_bmap_ptr_req *oldreq,
  483. union nilfs_bmap_ptr_req *newreq)
  484. {
  485. bmap->b_pops->bpop_commit_end_ptr(bmap, oldreq);
  486. bmap->b_pops->bpop_commit_alloc_ptr(bmap, newreq);
  487. }
  488. void nilfs_bmap_abort_update(struct nilfs_bmap *bmap,
  489. union nilfs_bmap_ptr_req *oldreq,
  490. union nilfs_bmap_ptr_req *newreq)
  491. {
  492. bmap->b_pops->bpop_abort_end_ptr(bmap, oldreq);
  493. bmap->b_pops->bpop_abort_alloc_ptr(bmap, newreq);
  494. }
  495. static int nilfs_bmap_translate_v(const struct nilfs_bmap *bmap, __u64 ptr,
  496. __u64 *ptrp)
  497. {
  498. sector_t blocknr;
  499. int ret;
  500. ret = nilfs_dat_translate(nilfs_bmap_get_dat(bmap), ptr, &blocknr);
  501. if (ret < 0)
  502. return ret;
  503. if (ptrp != NULL)
  504. *ptrp = blocknr;
  505. return 0;
  506. }
  507. static int nilfs_bmap_prepare_alloc_p(struct nilfs_bmap *bmap,
  508. union nilfs_bmap_ptr_req *req)
  509. {
  510. /* ignore target ptr */
  511. req->bpr_ptr = bmap->b_last_allocated_ptr++;
  512. return 0;
  513. }
  514. static void nilfs_bmap_commit_alloc_p(struct nilfs_bmap *bmap,
  515. union nilfs_bmap_ptr_req *req)
  516. {
  517. /* do nothing */
  518. }
  519. static void nilfs_bmap_abort_alloc_p(struct nilfs_bmap *bmap,
  520. union nilfs_bmap_ptr_req *req)
  521. {
  522. bmap->b_last_allocated_ptr--;
  523. }
  524. static const struct nilfs_bmap_ptr_operations nilfs_bmap_ptr_ops_v = {
  525. .bpop_prepare_alloc_ptr = nilfs_bmap_prepare_alloc_v,
  526. .bpop_commit_alloc_ptr = nilfs_bmap_commit_alloc_v,
  527. .bpop_abort_alloc_ptr = nilfs_bmap_abort_alloc_v,
  528. .bpop_prepare_end_ptr = nilfs_bmap_prepare_end_v,
  529. .bpop_commit_end_ptr = nilfs_bmap_commit_end_v,
  530. .bpop_abort_end_ptr = nilfs_bmap_abort_end_v,
  531. .bpop_translate = nilfs_bmap_translate_v,
  532. };
  533. static const struct nilfs_bmap_ptr_operations nilfs_bmap_ptr_ops_vmdt = {
  534. .bpop_prepare_alloc_ptr = nilfs_bmap_prepare_alloc_v,
  535. .bpop_commit_alloc_ptr = nilfs_bmap_commit_alloc_v,
  536. .bpop_abort_alloc_ptr = nilfs_bmap_abort_alloc_v,
  537. .bpop_prepare_end_ptr = nilfs_bmap_prepare_end_v,
  538. .bpop_commit_end_ptr = nilfs_bmap_commit_end_vmdt,
  539. .bpop_abort_end_ptr = nilfs_bmap_abort_end_v,
  540. .bpop_translate = nilfs_bmap_translate_v,
  541. };
  542. static const struct nilfs_bmap_ptr_operations nilfs_bmap_ptr_ops_p = {
  543. .bpop_prepare_alloc_ptr = nilfs_bmap_prepare_alloc_p,
  544. .bpop_commit_alloc_ptr = nilfs_bmap_commit_alloc_p,
  545. .bpop_abort_alloc_ptr = nilfs_bmap_abort_alloc_p,
  546. .bpop_prepare_end_ptr = NULL,
  547. .bpop_commit_end_ptr = NULL,
  548. .bpop_abort_end_ptr = NULL,
  549. .bpop_translate = NULL,
  550. };
  551. static const struct nilfs_bmap_ptr_operations nilfs_bmap_ptr_ops_gc = {
  552. .bpop_prepare_alloc_ptr = NULL,
  553. .bpop_commit_alloc_ptr = NULL,
  554. .bpop_abort_alloc_ptr = NULL,
  555. .bpop_prepare_end_ptr = NULL,
  556. .bpop_commit_end_ptr = NULL,
  557. .bpop_abort_end_ptr = NULL,
  558. .bpop_translate = NULL,
  559. };
  560. static struct lock_class_key nilfs_bmap_dat_lock_key;
  561. /**
  562. * nilfs_bmap_read - read a bmap from an inode
  563. * @bmap: bmap
  564. * @raw_inode: on-disk inode
  565. *
  566. * Description: nilfs_bmap_read() initializes the bmap @bmap.
  567. *
  568. * Return Value: On success, 0 is returned. On error, the following negative
  569. * error code is returned.
  570. *
  571. * %-ENOMEM - Insufficient amount of memory available.
  572. */
  573. int nilfs_bmap_read(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
  574. {
  575. if (raw_inode == NULL)
  576. memset(bmap->b_u.u_data, 0, NILFS_BMAP_SIZE);
  577. else
  578. memcpy(bmap->b_u.u_data, raw_inode->i_bmap, NILFS_BMAP_SIZE);
  579. init_rwsem(&bmap->b_sem);
  580. bmap->b_state = 0;
  581. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  582. switch (bmap->b_inode->i_ino) {
  583. case NILFS_DAT_INO:
  584. bmap->b_pops = &nilfs_bmap_ptr_ops_p;
  585. bmap->b_last_allocated_key = 0; /* XXX: use macro */
  586. bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
  587. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
  588. break;
  589. case NILFS_CPFILE_INO:
  590. case NILFS_SUFILE_INO:
  591. bmap->b_pops = &nilfs_bmap_ptr_ops_vmdt;
  592. bmap->b_last_allocated_key = 0; /* XXX: use macro */
  593. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  594. break;
  595. default:
  596. bmap->b_pops = &nilfs_bmap_ptr_ops_v;
  597. bmap->b_last_allocated_key = 0; /* XXX: use macro */
  598. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  599. break;
  600. }
  601. return (bmap->b_u.u_flags & NILFS_BMAP_LARGE) ?
  602. nilfs_btree_init(bmap,
  603. NILFS_BMAP_LARGE_LOW,
  604. NILFS_BMAP_LARGE_HIGH) :
  605. nilfs_direct_init(bmap,
  606. NILFS_BMAP_SMALL_LOW,
  607. NILFS_BMAP_SMALL_HIGH);
  608. }
  609. /**
  610. * nilfs_bmap_write - write back a bmap to an inode
  611. * @bmap: bmap
  612. * @raw_inode: on-disk inode
  613. *
  614. * Description: nilfs_bmap_write() stores @bmap in @raw_inode.
  615. */
  616. void nilfs_bmap_write(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
  617. {
  618. down_write(&bmap->b_sem);
  619. memcpy(raw_inode->i_bmap, bmap->b_u.u_data,
  620. NILFS_INODE_BMAP_SIZE * sizeof(__le64));
  621. if (bmap->b_inode->i_ino == NILFS_DAT_INO)
  622. bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
  623. up_write(&bmap->b_sem);
  624. }
  625. void nilfs_bmap_init_gc(struct nilfs_bmap *bmap)
  626. {
  627. memset(&bmap->b_u, 0, NILFS_BMAP_SIZE);
  628. init_rwsem(&bmap->b_sem);
  629. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  630. bmap->b_pops = &nilfs_bmap_ptr_ops_gc;
  631. bmap->b_last_allocated_key = 0;
  632. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  633. bmap->b_state = 0;
  634. nilfs_btree_init_gc(bmap);
  635. }
  636. void nilfs_bmap_init_gcdat(struct nilfs_bmap *gcbmap, struct nilfs_bmap *bmap)
  637. {
  638. memcpy(gcbmap, bmap, sizeof(union nilfs_bmap_union));
  639. init_rwsem(&gcbmap->b_sem);
  640. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
  641. gcbmap->b_inode = &NILFS_BMAP_I(gcbmap)->vfs_inode;
  642. }
  643. void nilfs_bmap_commit_gcdat(struct nilfs_bmap *gcbmap, struct nilfs_bmap *bmap)
  644. {
  645. memcpy(bmap, gcbmap, sizeof(union nilfs_bmap_union));
  646. init_rwsem(&bmap->b_sem);
  647. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
  648. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  649. }