bmap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 "btree.h"
  28. #include "direct.h"
  29. #include "btnode.h"
  30. #include "mdt.h"
  31. #include "dat.h"
  32. #include "alloc.h"
  33. struct inode *nilfs_bmap_get_dat(const struct nilfs_bmap *bmap)
  34. {
  35. return NILFS_I_NILFS(bmap->b_inode)->ns_dat;
  36. }
  37. static int nilfs_bmap_convert_error(struct nilfs_bmap *bmap,
  38. const char *fname, int err)
  39. {
  40. struct inode *inode = bmap->b_inode;
  41. if (err == -EINVAL) {
  42. nilfs_error(inode->i_sb, fname,
  43. "broken bmap (inode number=%lu)\n", inode->i_ino);
  44. err = -EIO;
  45. }
  46. return err;
  47. }
  48. /**
  49. * nilfs_bmap_lookup_at_level - find a data block or node block
  50. * @bmap: bmap
  51. * @key: key
  52. * @level: level
  53. * @ptrp: place to store the value associated to @key
  54. *
  55. * Description: nilfs_bmap_lookup_at_level() finds a record whose key
  56. * matches @key in the block at @level of the bmap.
  57. *
  58. * Return Value: On success, 0 is returned and the record associated with @key
  59. * is stored in the place pointed by @ptrp. On error, one of the following
  60. * negative error codes is returned.
  61. *
  62. * %-EIO - I/O error.
  63. *
  64. * %-ENOMEM - Insufficient amount of memory available.
  65. *
  66. * %-ENOENT - A record associated with @key does not exist.
  67. */
  68. int nilfs_bmap_lookup_at_level(struct nilfs_bmap *bmap, __u64 key, int level,
  69. __u64 *ptrp)
  70. {
  71. sector_t blocknr;
  72. int ret;
  73. down_read(&bmap->b_sem);
  74. ret = bmap->b_ops->bop_lookup(bmap, key, level, ptrp);
  75. if (ret < 0) {
  76. ret = nilfs_bmap_convert_error(bmap, __func__, ret);
  77. goto out;
  78. }
  79. if (NILFS_BMAP_USE_VBN(bmap)) {
  80. ret = nilfs_dat_translate(nilfs_bmap_get_dat(bmap), *ptrp,
  81. &blocknr);
  82. if (!ret)
  83. *ptrp = blocknr;
  84. }
  85. out:
  86. up_read(&bmap->b_sem);
  87. return ret;
  88. }
  89. int nilfs_bmap_lookup_contig(struct nilfs_bmap *bmap, __u64 key, __u64 *ptrp,
  90. unsigned maxblocks)
  91. {
  92. int ret;
  93. down_read(&bmap->b_sem);
  94. ret = bmap->b_ops->bop_lookup_contig(bmap, key, ptrp, maxblocks);
  95. up_read(&bmap->b_sem);
  96. return nilfs_bmap_convert_error(bmap, __func__, ret);
  97. }
  98. static int nilfs_bmap_do_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
  99. {
  100. __u64 keys[NILFS_BMAP_SMALL_HIGH + 1];
  101. __u64 ptrs[NILFS_BMAP_SMALL_HIGH + 1];
  102. int ret, n;
  103. if (bmap->b_ops->bop_check_insert != NULL) {
  104. ret = bmap->b_ops->bop_check_insert(bmap, key);
  105. if (ret > 0) {
  106. n = bmap->b_ops->bop_gather_data(
  107. bmap, keys, ptrs, NILFS_BMAP_SMALL_HIGH + 1);
  108. if (n < 0)
  109. return n;
  110. ret = nilfs_btree_convert_and_insert(
  111. bmap, key, ptr, keys, ptrs, n);
  112. if (ret == 0)
  113. bmap->b_u.u_flags |= NILFS_BMAP_LARGE;
  114. return ret;
  115. } else if (ret < 0)
  116. return ret;
  117. }
  118. return bmap->b_ops->bop_insert(bmap, key, ptr);
  119. }
  120. /**
  121. * nilfs_bmap_insert - insert a new key-record pair into a bmap
  122. * @bmap: bmap
  123. * @key: key
  124. * @rec: record
  125. *
  126. * Description: nilfs_bmap_insert() inserts the new key-record pair specified
  127. * by @key and @rec into @bmap.
  128. *
  129. * Return Value: On success, 0 is returned. On error, one of the following
  130. * negative error codes is returned.
  131. *
  132. * %-EIO - I/O error.
  133. *
  134. * %-ENOMEM - Insufficient amount of memory available.
  135. *
  136. * %-EEXIST - A record associated with @key already exist.
  137. */
  138. int nilfs_bmap_insert(struct nilfs_bmap *bmap,
  139. unsigned long key,
  140. unsigned long rec)
  141. {
  142. int ret;
  143. down_write(&bmap->b_sem);
  144. ret = nilfs_bmap_do_insert(bmap, key, rec);
  145. up_write(&bmap->b_sem);
  146. return nilfs_bmap_convert_error(bmap, __func__, ret);
  147. }
  148. static int nilfs_bmap_do_delete(struct nilfs_bmap *bmap, __u64 key)
  149. {
  150. __u64 keys[NILFS_BMAP_LARGE_LOW + 1];
  151. __u64 ptrs[NILFS_BMAP_LARGE_LOW + 1];
  152. int ret, n;
  153. if (bmap->b_ops->bop_check_delete != NULL) {
  154. ret = bmap->b_ops->bop_check_delete(bmap, key);
  155. if (ret > 0) {
  156. n = bmap->b_ops->bop_gather_data(
  157. bmap, keys, ptrs, NILFS_BMAP_LARGE_LOW + 1);
  158. if (n < 0)
  159. return n;
  160. ret = nilfs_direct_delete_and_convert(
  161. bmap, key, keys, ptrs, n);
  162. if (ret == 0)
  163. bmap->b_u.u_flags &= ~NILFS_BMAP_LARGE;
  164. return ret;
  165. } else if (ret < 0)
  166. return ret;
  167. }
  168. return bmap->b_ops->bop_delete(bmap, key);
  169. }
  170. int nilfs_bmap_last_key(struct nilfs_bmap *bmap, unsigned long *key)
  171. {
  172. __u64 lastkey;
  173. int ret;
  174. down_read(&bmap->b_sem);
  175. ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
  176. up_read(&bmap->b_sem);
  177. if (ret < 0)
  178. ret = nilfs_bmap_convert_error(bmap, __func__, ret);
  179. else
  180. *key = lastkey;
  181. return ret;
  182. }
  183. /**
  184. * nilfs_bmap_delete - delete a key-record pair from a bmap
  185. * @bmap: bmap
  186. * @key: key
  187. *
  188. * Description: nilfs_bmap_delete() deletes the key-record pair specified by
  189. * @key from @bmap.
  190. *
  191. * Return Value: On success, 0 is returned. On error, one of the following
  192. * negative error codes is returned.
  193. *
  194. * %-EIO - I/O error.
  195. *
  196. * %-ENOMEM - Insufficient amount of memory available.
  197. *
  198. * %-ENOENT - A record associated with @key does not exist.
  199. */
  200. int nilfs_bmap_delete(struct nilfs_bmap *bmap, unsigned long key)
  201. {
  202. int ret;
  203. down_write(&bmap->b_sem);
  204. ret = nilfs_bmap_do_delete(bmap, key);
  205. up_write(&bmap->b_sem);
  206. return nilfs_bmap_convert_error(bmap, __func__, ret);
  207. }
  208. static int nilfs_bmap_do_truncate(struct nilfs_bmap *bmap, unsigned long key)
  209. {
  210. __u64 lastkey;
  211. int ret;
  212. ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
  213. if (ret < 0) {
  214. if (ret == -ENOENT)
  215. ret = 0;
  216. return ret;
  217. }
  218. while (key <= lastkey) {
  219. ret = nilfs_bmap_do_delete(bmap, lastkey);
  220. if (ret < 0)
  221. return ret;
  222. ret = bmap->b_ops->bop_last_key(bmap, &lastkey);
  223. if (ret < 0) {
  224. if (ret == -ENOENT)
  225. ret = 0;
  226. return ret;
  227. }
  228. }
  229. return 0;
  230. }
  231. /**
  232. * nilfs_bmap_truncate - truncate a bmap to a specified key
  233. * @bmap: bmap
  234. * @key: key
  235. *
  236. * Description: nilfs_bmap_truncate() removes key-record pairs whose keys are
  237. * greater than or equal to @key from @bmap.
  238. *
  239. * Return Value: On success, 0 is returned. On error, one of the following
  240. * negative error codes is returned.
  241. *
  242. * %-EIO - I/O error.
  243. *
  244. * %-ENOMEM - Insufficient amount of memory available.
  245. */
  246. int nilfs_bmap_truncate(struct nilfs_bmap *bmap, unsigned long key)
  247. {
  248. int ret;
  249. down_write(&bmap->b_sem);
  250. ret = nilfs_bmap_do_truncate(bmap, key);
  251. up_write(&bmap->b_sem);
  252. return nilfs_bmap_convert_error(bmap, __func__, ret);
  253. }
  254. /**
  255. * nilfs_bmap_clear - free resources a bmap holds
  256. * @bmap: bmap
  257. *
  258. * Description: nilfs_bmap_clear() frees resources associated with @bmap.
  259. */
  260. void nilfs_bmap_clear(struct nilfs_bmap *bmap)
  261. {
  262. down_write(&bmap->b_sem);
  263. if (bmap->b_ops->bop_clear != NULL)
  264. bmap->b_ops->bop_clear(bmap);
  265. up_write(&bmap->b_sem);
  266. }
  267. /**
  268. * nilfs_bmap_propagate - propagate dirty state
  269. * @bmap: bmap
  270. * @bh: buffer head
  271. *
  272. * Description: nilfs_bmap_propagate() marks the buffers that directly or
  273. * indirectly refer to the block specified by @bh dirty.
  274. *
  275. * Return Value: On success, 0 is returned. On error, one of the following
  276. * negative error codes is returned.
  277. *
  278. * %-EIO - I/O error.
  279. *
  280. * %-ENOMEM - Insufficient amount of memory available.
  281. */
  282. int nilfs_bmap_propagate(struct nilfs_bmap *bmap, struct buffer_head *bh)
  283. {
  284. int ret;
  285. down_write(&bmap->b_sem);
  286. ret = bmap->b_ops->bop_propagate(bmap, bh);
  287. up_write(&bmap->b_sem);
  288. return nilfs_bmap_convert_error(bmap, __func__, ret);
  289. }
  290. /**
  291. * nilfs_bmap_lookup_dirty_buffers -
  292. * @bmap: bmap
  293. * @listp: pointer to buffer head list
  294. */
  295. void nilfs_bmap_lookup_dirty_buffers(struct nilfs_bmap *bmap,
  296. struct list_head *listp)
  297. {
  298. if (bmap->b_ops->bop_lookup_dirty_buffers != NULL)
  299. bmap->b_ops->bop_lookup_dirty_buffers(bmap, listp);
  300. }
  301. /**
  302. * nilfs_bmap_assign - assign a new block number to a block
  303. * @bmap: bmap
  304. * @bhp: pointer to buffer head
  305. * @blocknr: block number
  306. * @binfo: block information
  307. *
  308. * Description: nilfs_bmap_assign() assigns the block number @blocknr to the
  309. * buffer specified by @bh.
  310. *
  311. * Return Value: On success, 0 is returned and the buffer head of a newly
  312. * create buffer and the block information associated with the buffer are
  313. * stored in the place pointed by @bh and @binfo, respectively. On error, one
  314. * of the following negative error codes is returned.
  315. *
  316. * %-EIO - I/O error.
  317. *
  318. * %-ENOMEM - Insufficient amount of memory available.
  319. */
  320. int nilfs_bmap_assign(struct nilfs_bmap *bmap,
  321. struct buffer_head **bh,
  322. unsigned long blocknr,
  323. union nilfs_binfo *binfo)
  324. {
  325. int ret;
  326. down_write(&bmap->b_sem);
  327. ret = bmap->b_ops->bop_assign(bmap, bh, blocknr, binfo);
  328. up_write(&bmap->b_sem);
  329. return nilfs_bmap_convert_error(bmap, __func__, ret);
  330. }
  331. /**
  332. * nilfs_bmap_mark - mark block dirty
  333. * @bmap: bmap
  334. * @key: key
  335. * @level: level
  336. *
  337. * Description: nilfs_bmap_mark() marks the block specified by @key and @level
  338. * as dirty.
  339. *
  340. * Return Value: On success, 0 is returned. On error, one of the following
  341. * negative error codes is returned.
  342. *
  343. * %-EIO - I/O error.
  344. *
  345. * %-ENOMEM - Insufficient amount of memory available.
  346. */
  347. int nilfs_bmap_mark(struct nilfs_bmap *bmap, __u64 key, int level)
  348. {
  349. int ret;
  350. if (bmap->b_ops->bop_mark == NULL)
  351. return 0;
  352. down_write(&bmap->b_sem);
  353. ret = bmap->b_ops->bop_mark(bmap, key, level);
  354. up_write(&bmap->b_sem);
  355. return nilfs_bmap_convert_error(bmap, __func__, ret);
  356. }
  357. /**
  358. * nilfs_bmap_test_and_clear_dirty - test and clear a bmap dirty state
  359. * @bmap: bmap
  360. *
  361. * Description: nilfs_test_and_clear() is the atomic operation to test and
  362. * clear the dirty state of @bmap.
  363. *
  364. * Return Value: 1 is returned if @bmap is dirty, or 0 if clear.
  365. */
  366. int nilfs_bmap_test_and_clear_dirty(struct nilfs_bmap *bmap)
  367. {
  368. int ret;
  369. down_write(&bmap->b_sem);
  370. ret = nilfs_bmap_dirty(bmap);
  371. nilfs_bmap_clear_dirty(bmap);
  372. up_write(&bmap->b_sem);
  373. return ret;
  374. }
  375. /*
  376. * Internal use only
  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; pbh = pbh->b_this_page)
  386. 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. #define NILFS_BMAP_GROUP_DIV 8
  401. __u64 nilfs_bmap_find_target_in_group(const struct nilfs_bmap *bmap)
  402. {
  403. struct inode *dat = nilfs_bmap_get_dat(bmap);
  404. unsigned long entries_per_group = nilfs_palloc_entries_per_group(dat);
  405. unsigned long group = bmap->b_inode->i_ino / entries_per_group;
  406. return group * entries_per_group +
  407. (bmap->b_inode->i_ino % NILFS_BMAP_GROUP_DIV) *
  408. (entries_per_group / NILFS_BMAP_GROUP_DIV);
  409. }
  410. static struct lock_class_key nilfs_bmap_dat_lock_key;
  411. static struct lock_class_key nilfs_bmap_mdt_lock_key;
  412. /**
  413. * nilfs_bmap_read - read a bmap from an inode
  414. * @bmap: bmap
  415. * @raw_inode: on-disk inode
  416. *
  417. * Description: nilfs_bmap_read() initializes the bmap @bmap.
  418. *
  419. * Return Value: On success, 0 is returned. On error, the following negative
  420. * error code is returned.
  421. *
  422. * %-ENOMEM - Insufficient amount of memory available.
  423. */
  424. int nilfs_bmap_read(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
  425. {
  426. if (raw_inode == NULL)
  427. memset(bmap->b_u.u_data, 0, NILFS_BMAP_SIZE);
  428. else
  429. memcpy(bmap->b_u.u_data, raw_inode->i_bmap, NILFS_BMAP_SIZE);
  430. init_rwsem(&bmap->b_sem);
  431. bmap->b_state = 0;
  432. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  433. switch (bmap->b_inode->i_ino) {
  434. case NILFS_DAT_INO:
  435. bmap->b_ptr_type = NILFS_BMAP_PTR_P;
  436. bmap->b_last_allocated_key = 0;
  437. bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
  438. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_dat_lock_key);
  439. break;
  440. case NILFS_CPFILE_INO:
  441. case NILFS_SUFILE_INO:
  442. bmap->b_ptr_type = NILFS_BMAP_PTR_VS;
  443. bmap->b_last_allocated_key = 0;
  444. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  445. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_mdt_lock_key);
  446. break;
  447. case NILFS_IFILE_INO:
  448. lockdep_set_class(&bmap->b_sem, &nilfs_bmap_mdt_lock_key);
  449. /* Fall through */
  450. default:
  451. bmap->b_ptr_type = NILFS_BMAP_PTR_VM;
  452. bmap->b_last_allocated_key = 0;
  453. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  454. break;
  455. }
  456. return (bmap->b_u.u_flags & NILFS_BMAP_LARGE) ?
  457. nilfs_btree_init(bmap) : nilfs_direct_init(bmap);
  458. }
  459. /**
  460. * nilfs_bmap_write - write back a bmap to an inode
  461. * @bmap: bmap
  462. * @raw_inode: on-disk inode
  463. *
  464. * Description: nilfs_bmap_write() stores @bmap in @raw_inode.
  465. */
  466. void nilfs_bmap_write(struct nilfs_bmap *bmap, struct nilfs_inode *raw_inode)
  467. {
  468. down_write(&bmap->b_sem);
  469. memcpy(raw_inode->i_bmap, bmap->b_u.u_data,
  470. NILFS_INODE_BMAP_SIZE * sizeof(__le64));
  471. if (bmap->b_inode->i_ino == NILFS_DAT_INO)
  472. bmap->b_last_allocated_ptr = NILFS_BMAP_NEW_PTR_INIT;
  473. up_write(&bmap->b_sem);
  474. }
  475. void nilfs_bmap_init_gc(struct nilfs_bmap *bmap)
  476. {
  477. memset(&bmap->b_u, 0, NILFS_BMAP_SIZE);
  478. init_rwsem(&bmap->b_sem);
  479. bmap->b_inode = &NILFS_BMAP_I(bmap)->vfs_inode;
  480. bmap->b_ptr_type = NILFS_BMAP_PTR_U;
  481. bmap->b_last_allocated_key = 0;
  482. bmap->b_last_allocated_ptr = NILFS_BMAP_INVALID_PTR;
  483. bmap->b_state = 0;
  484. nilfs_btree_init_gc(bmap);
  485. }
  486. void nilfs_bmap_save(const struct nilfs_bmap *bmap,
  487. struct nilfs_bmap_store *store)
  488. {
  489. memcpy(store->data, bmap->b_u.u_data, sizeof(store->data));
  490. store->last_allocated_key = bmap->b_last_allocated_key;
  491. store->last_allocated_ptr = bmap->b_last_allocated_ptr;
  492. store->state = bmap->b_state;
  493. }
  494. void nilfs_bmap_restore(struct nilfs_bmap *bmap,
  495. const struct nilfs_bmap_store *store)
  496. {
  497. memcpy(bmap->b_u.u_data, store->data, sizeof(store->data));
  498. bmap->b_last_allocated_key = store->last_allocated_key;
  499. bmap->b_last_allocated_ptr = store->last_allocated_ptr;
  500. bmap->b_state = store->state;
  501. }