bmap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. int nilfs_bmap_start_v(struct nilfs_bmap *bmap, union nilfs_bmap_ptr_req *req,
  457. sector_t blocknr)
  458. {
  459. struct inode *dat = nilfs_bmap_get_dat(bmap);
  460. int ret;
  461. ret = nilfs_dat_prepare_start(dat, &req->bpr_req);
  462. if (likely(!ret))
  463. nilfs_dat_commit_start(dat, &req->bpr_req, blocknr);
  464. return ret;
  465. }
  466. static int nilfs_bmap_prepare_end_v(struct nilfs_bmap *bmap,
  467. union nilfs_bmap_ptr_req *req)
  468. {
  469. return nilfs_dat_prepare_end(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  470. }
  471. static void nilfs_bmap_commit_end_v(struct nilfs_bmap *bmap,
  472. union nilfs_bmap_ptr_req *req)
  473. {
  474. nilfs_dat_commit_end(nilfs_bmap_get_dat(bmap), &req->bpr_req, 0);
  475. }
  476. static void nilfs_bmap_commit_end_vmdt(struct nilfs_bmap *bmap,
  477. union nilfs_bmap_ptr_req *req)
  478. {
  479. nilfs_dat_commit_end(nilfs_bmap_get_dat(bmap), &req->bpr_req, 1);
  480. }
  481. static void nilfs_bmap_abort_end_v(struct nilfs_bmap *bmap,
  482. union nilfs_bmap_ptr_req *req)
  483. {
  484. nilfs_dat_abort_end(nilfs_bmap_get_dat(bmap), &req->bpr_req);
  485. }
  486. int nilfs_bmap_move_v(const struct nilfs_bmap *bmap, __u64 vblocknr,
  487. sector_t blocknr)
  488. {
  489. return nilfs_dat_move(nilfs_bmap_get_dat(bmap), vblocknr, blocknr);
  490. }
  491. int nilfs_bmap_mark_dirty(const struct nilfs_bmap *bmap, __u64 vblocknr)
  492. {
  493. return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(bmap), vblocknr);
  494. }
  495. int nilfs_bmap_prepare_update(struct nilfs_bmap *bmap,
  496. union nilfs_bmap_ptr_req *oldreq,
  497. union nilfs_bmap_ptr_req *newreq)
  498. {
  499. int ret;
  500. ret = bmap->b_pops->bpop_prepare_end_ptr(bmap, oldreq);
  501. if (ret < 0)
  502. return ret;
  503. ret = bmap->b_pops->bpop_prepare_alloc_ptr(bmap, newreq);
  504. if (ret < 0)
  505. bmap->b_pops->bpop_abort_end_ptr(bmap, oldreq);
  506. return ret;
  507. }
  508. void nilfs_bmap_commit_update(struct nilfs_bmap *bmap,
  509. union nilfs_bmap_ptr_req *oldreq,
  510. union nilfs_bmap_ptr_req *newreq)
  511. {
  512. bmap->b_pops->bpop_commit_end_ptr(bmap, oldreq);
  513. bmap->b_pops->bpop_commit_alloc_ptr(bmap, newreq);
  514. }
  515. void nilfs_bmap_abort_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_abort_end_ptr(bmap, oldreq);
  520. bmap->b_pops->bpop_abort_alloc_ptr(bmap, newreq);
  521. }
  522. static int nilfs_bmap_translate_v(const struct nilfs_bmap *bmap, __u64 ptr,
  523. __u64 *ptrp)
  524. {
  525. sector_t blocknr;
  526. int ret;
  527. ret = nilfs_dat_translate(nilfs_bmap_get_dat(bmap), ptr, &blocknr);
  528. if (ret < 0)
  529. return ret;
  530. if (ptrp != NULL)
  531. *ptrp = blocknr;
  532. return 0;
  533. }
  534. static int nilfs_bmap_prepare_alloc_p(struct nilfs_bmap *bmap,
  535. union nilfs_bmap_ptr_req *req)
  536. {
  537. /* ignore target ptr */
  538. req->bpr_ptr = bmap->b_last_allocated_ptr++;
  539. return 0;
  540. }
  541. static void nilfs_bmap_commit_alloc_p(struct nilfs_bmap *bmap,
  542. union nilfs_bmap_ptr_req *req)
  543. {
  544. /* do nothing */
  545. }
  546. static void nilfs_bmap_abort_alloc_p(struct nilfs_bmap *bmap,
  547. union nilfs_bmap_ptr_req *req)
  548. {
  549. bmap->b_last_allocated_ptr--;
  550. }
  551. static const struct nilfs_bmap_ptr_operations nilfs_bmap_ptr_ops_v = {
  552. .bpop_prepare_alloc_ptr = nilfs_bmap_prepare_alloc_v,
  553. .bpop_commit_alloc_ptr = nilfs_bmap_commit_alloc_v,
  554. .bpop_abort_alloc_ptr = nilfs_bmap_abort_alloc_v,
  555. .bpop_prepare_end_ptr = nilfs_bmap_prepare_end_v,
  556. .bpop_commit_end_ptr = nilfs_bmap_commit_end_v,
  557. .bpop_abort_end_ptr = nilfs_bmap_abort_end_v,
  558. .bpop_translate = nilfs_bmap_translate_v,
  559. };
  560. static const struct nilfs_bmap_ptr_operations nilfs_bmap_ptr_ops_vmdt = {
  561. .bpop_prepare_alloc_ptr = nilfs_bmap_prepare_alloc_v,
  562. .bpop_commit_alloc_ptr = nilfs_bmap_commit_alloc_v,
  563. .bpop_abort_alloc_ptr = nilfs_bmap_abort_alloc_v,
  564. .bpop_prepare_end_ptr = nilfs_bmap_prepare_end_v,
  565. .bpop_commit_end_ptr = nilfs_bmap_commit_end_vmdt,
  566. .bpop_abort_end_ptr = nilfs_bmap_abort_end_v,
  567. .bpop_translate = nilfs_bmap_translate_v,
  568. };
  569. static const struct nilfs_bmap_ptr_operations nilfs_bmap_ptr_ops_p = {
  570. .bpop_prepare_alloc_ptr = nilfs_bmap_prepare_alloc_p,
  571. .bpop_commit_alloc_ptr = nilfs_bmap_commit_alloc_p,
  572. .bpop_abort_alloc_ptr = nilfs_bmap_abort_alloc_p,
  573. .bpop_prepare_end_ptr = NULL,
  574. .bpop_commit_end_ptr = NULL,
  575. .bpop_abort_end_ptr = NULL,
  576. .bpop_translate = NULL,
  577. };
  578. static const struct nilfs_bmap_ptr_operations nilfs_bmap_ptr_ops_gc = {
  579. .bpop_prepare_alloc_ptr = NULL,
  580. .bpop_commit_alloc_ptr = NULL,
  581. .bpop_abort_alloc_ptr = NULL,
  582. .bpop_prepare_end_ptr = NULL,
  583. .bpop_commit_end_ptr = NULL,
  584. .bpop_abort_end_ptr = NULL,
  585. .bpop_translate = NULL,
  586. };
  587. static struct lock_class_key nilfs_bmap_dat_lock_key;
  588. /**
  589. * nilfs_bmap_read - read a bmap from an inode
  590. * @bmap: bmap
  591. * @raw_inode: on-disk inode
  592. *
  593. * Description: nilfs_bmap_read() initializes the bmap @bmap.
  594. *
  595. * Return Value: On success, 0 is returned. On error, the following negative
  596. * error code is returned.
  597. *
  598. * %-ENOMEM - Insufficient amount of memory available.
  599. */
  600. int nilfs_bmap_read(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
  601. {
  602. if (raw_inode == NULL)
  603. memset(bmap->b_u.u_data, 0, NILFS_BMAP_SIZE);
  604. else
  605. memcpy(bmap->b_u.u_data, raw_inode->i_bmap, NILFS_BMAP_SIZE);
  606. init_rwsem(&bmap->b_sem);
  607. bmap->b_state = 0;
  608. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  609. switch (bmap->b_inode->i_ino) {
  610. case NILFS_DAT_INO:
  611. bmap->b_pops = &nilfs_bmap_ptr_ops_p;
  612. bmap->b_last_allocated_key = 0; /* XXX: use macro */
  613. bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
  614. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
  615. break;
  616. case NILFS_CPFILE_INO:
  617. case NILFS_SUFILE_INO:
  618. bmap->b_pops = &nilfs_bmap_ptr_ops_vmdt;
  619. bmap->b_last_allocated_key = 0; /* XXX: use macro */
  620. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  621. break;
  622. default:
  623. bmap->b_pops = &nilfs_bmap_ptr_ops_v;
  624. bmap->b_last_allocated_key = 0; /* XXX: use macro */
  625. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  626. break;
  627. }
  628. return (bmap->b_u.u_flags & NILFS_BMAP_LARGE) ?
  629. nilfs_btree_init(bmap,
  630. NILFS_BMAP_LARGE_LOW,
  631. NILFS_BMAP_LARGE_HIGH) :
  632. nilfs_direct_init(bmap,
  633. NILFS_BMAP_SMALL_LOW,
  634. NILFS_BMAP_SMALL_HIGH);
  635. }
  636. /**
  637. * nilfs_bmap_write - write back a bmap to an inode
  638. * @bmap: bmap
  639. * @raw_inode: on-disk inode
  640. *
  641. * Description: nilfs_bmap_write() stores @bmap in @raw_inode.
  642. */
  643. void nilfs_bmap_write(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
  644. {
  645. down_write(&bmap->b_sem);
  646. memcpy(raw_inode->i_bmap, bmap->b_u.u_data,
  647. NILFS_INODE_BMAP_SIZE * sizeof(__le64));
  648. if (bmap->b_inode->i_ino == NILFS_DAT_INO)
  649. bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
  650. up_write(&bmap->b_sem);
  651. }
  652. void nilfs_bmap_init_gc(struct nilfs_bmap *bmap)
  653. {
  654. memset(&bmap->b_u, 0, NILFS_BMAP_SIZE);
  655. init_rwsem(&bmap->b_sem);
  656. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  657. bmap->b_pops = &nilfs_bmap_ptr_ops_gc;
  658. bmap->b_last_allocated_key = 0;
  659. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  660. bmap->b_state = 0;
  661. nilfs_btree_init_gc(bmap);
  662. }
  663. void nilfs_bmap_init_gcdat(struct nilfs_bmap *gcbmap, struct nilfs_bmap *bmap)
  664. {
  665. memcpy(gcbmap, bmap, sizeof(union nilfs_bmap_union));
  666. init_rwsem(&gcbmap->b_sem);
  667. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
  668. gcbmap->b_inode = &NILFS_BMAP_I(gcbmap)->vfs_inode;
  669. }
  670. void nilfs_bmap_commit_gcdat(struct nilfs_bmap *gcbmap, struct nilfs_bmap *bmap)
  671. {
  672. memcpy(bmap, gcbmap, sizeof(union nilfs_bmap_union));
  673. init_rwsem(&bmap->b_sem);
  674. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
  675. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  676. }