direct.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * direct.c - NILFS direct block pointer.
  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/errno.h>
  23. #include "nilfs.h"
  24. #include "page.h"
  25. #include "direct.h"
  26. #include "alloc.h"
  27. #include "dat.h"
  28. static inline __le64 *nilfs_direct_dptrs(const struct nilfs_bmap *direct)
  29. {
  30. return (__le64 *)
  31. ((struct nilfs_direct_node *)direct->b_u.u_data + 1);
  32. }
  33. static inline __u64
  34. nilfs_direct_get_ptr(const struct nilfs_bmap *direct, __u64 key)
  35. {
  36. return le64_to_cpu(*(nilfs_direct_dptrs(direct) + key));
  37. }
  38. static inline void nilfs_direct_set_ptr(struct nilfs_bmap *direct,
  39. __u64 key, __u64 ptr)
  40. {
  41. *(nilfs_direct_dptrs(direct) + key) = cpu_to_le64(ptr);
  42. }
  43. static int nilfs_direct_lookup(const struct nilfs_bmap *direct,
  44. __u64 key, int level, __u64 *ptrp)
  45. {
  46. __u64 ptr;
  47. if (key > NILFS_DIRECT_KEY_MAX || level != 1)
  48. return -ENOENT;
  49. ptr = nilfs_direct_get_ptr(direct, key);
  50. if (ptr == NILFS_BMAP_INVALID_PTR)
  51. return -ENOENT;
  52. *ptrp = ptr;
  53. return 0;
  54. }
  55. static int nilfs_direct_lookup_contig(const struct nilfs_bmap *direct,
  56. __u64 key, __u64 *ptrp,
  57. unsigned maxblocks)
  58. {
  59. struct inode *dat = NULL;
  60. __u64 ptr, ptr2;
  61. sector_t blocknr;
  62. int ret, cnt;
  63. if (key > NILFS_DIRECT_KEY_MAX)
  64. return -ENOENT;
  65. ptr = nilfs_direct_get_ptr(direct, key);
  66. if (ptr == NILFS_BMAP_INVALID_PTR)
  67. return -ENOENT;
  68. if (NILFS_BMAP_USE_VBN(direct)) {
  69. dat = nilfs_bmap_get_dat(direct);
  70. ret = nilfs_dat_translate(dat, ptr, &blocknr);
  71. if (ret < 0)
  72. return ret;
  73. ptr = blocknr;
  74. }
  75. maxblocks = min_t(unsigned, maxblocks, NILFS_DIRECT_KEY_MAX - key + 1);
  76. for (cnt = 1; cnt < maxblocks &&
  77. (ptr2 = nilfs_direct_get_ptr(direct, key + cnt)) !=
  78. NILFS_BMAP_INVALID_PTR;
  79. cnt++) {
  80. if (dat) {
  81. ret = nilfs_dat_translate(dat, ptr2, &blocknr);
  82. if (ret < 0)
  83. return ret;
  84. ptr2 = blocknr;
  85. }
  86. if (ptr2 != ptr + cnt)
  87. break;
  88. }
  89. *ptrp = ptr;
  90. return cnt;
  91. }
  92. static __u64
  93. nilfs_direct_find_target_v(const struct nilfs_bmap *direct, __u64 key)
  94. {
  95. __u64 ptr;
  96. ptr = nilfs_bmap_find_target_seq(direct, key);
  97. if (ptr != NILFS_BMAP_INVALID_PTR)
  98. /* sequential access */
  99. return ptr;
  100. else
  101. /* block group */
  102. return nilfs_bmap_find_target_in_group(direct);
  103. }
  104. static int nilfs_direct_insert(struct nilfs_bmap *bmap, __u64 key, __u64 ptr)
  105. {
  106. union nilfs_bmap_ptr_req req;
  107. struct inode *dat = NULL;
  108. struct buffer_head *bh;
  109. int ret;
  110. if (key > NILFS_DIRECT_KEY_MAX)
  111. return -ENOENT;
  112. if (nilfs_direct_get_ptr(bmap, key) != NILFS_BMAP_INVALID_PTR)
  113. return -EEXIST;
  114. if (NILFS_BMAP_USE_VBN(bmap)) {
  115. req.bpr_ptr = nilfs_direct_find_target_v(bmap, key);
  116. dat = nilfs_bmap_get_dat(bmap);
  117. }
  118. ret = nilfs_bmap_prepare_alloc_ptr(bmap, &req, dat);
  119. if (!ret) {
  120. /* ptr must be a pointer to a buffer head. */
  121. bh = (struct buffer_head *)((unsigned long)ptr);
  122. set_buffer_nilfs_volatile(bh);
  123. nilfs_bmap_commit_alloc_ptr(bmap, &req, dat);
  124. nilfs_direct_set_ptr(bmap, key, req.bpr_ptr);
  125. if (!nilfs_bmap_dirty(bmap))
  126. nilfs_bmap_set_dirty(bmap);
  127. if (NILFS_BMAP_USE_VBN(bmap))
  128. nilfs_bmap_set_target_v(bmap, key, req.bpr_ptr);
  129. nilfs_inode_add_blocks(bmap->b_inode, 1);
  130. }
  131. return ret;
  132. }
  133. static int nilfs_direct_delete(struct nilfs_bmap *bmap, __u64 key)
  134. {
  135. union nilfs_bmap_ptr_req req;
  136. struct inode *dat;
  137. int ret;
  138. if (key > NILFS_DIRECT_KEY_MAX ||
  139. nilfs_direct_get_ptr(bmap, key) == NILFS_BMAP_INVALID_PTR)
  140. return -ENOENT;
  141. dat = NILFS_BMAP_USE_VBN(bmap) ? nilfs_bmap_get_dat(bmap) : NULL;
  142. req.bpr_ptr = nilfs_direct_get_ptr(bmap, key);
  143. ret = nilfs_bmap_prepare_end_ptr(bmap, &req, dat);
  144. if (!ret) {
  145. nilfs_bmap_commit_end_ptr(bmap, &req, dat);
  146. nilfs_direct_set_ptr(bmap, key, NILFS_BMAP_INVALID_PTR);
  147. nilfs_inode_sub_blocks(bmap->b_inode, 1);
  148. }
  149. return ret;
  150. }
  151. static int nilfs_direct_seek_key(const struct nilfs_bmap *direct, __u64 start,
  152. __u64 *keyp)
  153. {
  154. __u64 key;
  155. for (key = start; key <= NILFS_DIRECT_KEY_MAX; key++) {
  156. if (nilfs_direct_get_ptr(direct, key) !=
  157. NILFS_BMAP_INVALID_PTR) {
  158. *keyp = key;
  159. return 0;
  160. }
  161. }
  162. return -ENOENT;
  163. }
  164. static int nilfs_direct_last_key(const struct nilfs_bmap *direct, __u64 *keyp)
  165. {
  166. __u64 key, lastkey;
  167. lastkey = NILFS_DIRECT_KEY_MAX + 1;
  168. for (key = NILFS_DIRECT_KEY_MIN; key <= NILFS_DIRECT_KEY_MAX; key++)
  169. if (nilfs_direct_get_ptr(direct, key) !=
  170. NILFS_BMAP_INVALID_PTR)
  171. lastkey = key;
  172. if (lastkey == NILFS_DIRECT_KEY_MAX + 1)
  173. return -ENOENT;
  174. *keyp = lastkey;
  175. return 0;
  176. }
  177. static int nilfs_direct_check_insert(const struct nilfs_bmap *bmap, __u64 key)
  178. {
  179. return key > NILFS_DIRECT_KEY_MAX;
  180. }
  181. static int nilfs_direct_gather_data(struct nilfs_bmap *direct,
  182. __u64 *keys, __u64 *ptrs, int nitems)
  183. {
  184. __u64 key;
  185. __u64 ptr;
  186. int n;
  187. if (nitems > NILFS_DIRECT_NBLOCKS)
  188. nitems = NILFS_DIRECT_NBLOCKS;
  189. n = 0;
  190. for (key = 0; key < nitems; key++) {
  191. ptr = nilfs_direct_get_ptr(direct, key);
  192. if (ptr != NILFS_BMAP_INVALID_PTR) {
  193. keys[n] = key;
  194. ptrs[n] = ptr;
  195. n++;
  196. }
  197. }
  198. return n;
  199. }
  200. int nilfs_direct_delete_and_convert(struct nilfs_bmap *bmap,
  201. __u64 key, __u64 *keys, __u64 *ptrs, int n)
  202. {
  203. __le64 *dptrs;
  204. int ret, i, j;
  205. /* no need to allocate any resource for conversion */
  206. /* delete */
  207. ret = bmap->b_ops->bop_delete(bmap, key);
  208. if (ret < 0)
  209. return ret;
  210. /* free resources */
  211. if (bmap->b_ops->bop_clear != NULL)
  212. bmap->b_ops->bop_clear(bmap);
  213. /* convert */
  214. dptrs = nilfs_direct_dptrs(bmap);
  215. for (i = 0, j = 0; i < NILFS_DIRECT_NBLOCKS; i++) {
  216. if ((j < n) && (i == keys[j])) {
  217. dptrs[i] = (i != key) ?
  218. cpu_to_le64(ptrs[j]) :
  219. NILFS_BMAP_INVALID_PTR;
  220. j++;
  221. } else
  222. dptrs[i] = NILFS_BMAP_INVALID_PTR;
  223. }
  224. nilfs_direct_init(bmap);
  225. return 0;
  226. }
  227. static int nilfs_direct_propagate(struct nilfs_bmap *bmap,
  228. struct buffer_head *bh)
  229. {
  230. struct nilfs_palloc_req oldreq, newreq;
  231. struct inode *dat;
  232. __u64 key;
  233. __u64 ptr;
  234. int ret;
  235. if (!NILFS_BMAP_USE_VBN(bmap))
  236. return 0;
  237. dat = nilfs_bmap_get_dat(bmap);
  238. key = nilfs_bmap_data_get_key(bmap, bh);
  239. ptr = nilfs_direct_get_ptr(bmap, key);
  240. if (!buffer_nilfs_volatile(bh)) {
  241. oldreq.pr_entry_nr = ptr;
  242. newreq.pr_entry_nr = ptr;
  243. ret = nilfs_dat_prepare_update(dat, &oldreq, &newreq);
  244. if (ret < 0)
  245. return ret;
  246. nilfs_dat_commit_update(dat, &oldreq, &newreq,
  247. bmap->b_ptr_type == NILFS_BMAP_PTR_VS);
  248. set_buffer_nilfs_volatile(bh);
  249. nilfs_direct_set_ptr(bmap, key, newreq.pr_entry_nr);
  250. } else
  251. ret = nilfs_dat_mark_dirty(dat, ptr);
  252. return ret;
  253. }
  254. static int nilfs_direct_assign_v(struct nilfs_bmap *direct,
  255. __u64 key, __u64 ptr,
  256. struct buffer_head **bh,
  257. sector_t blocknr,
  258. union nilfs_binfo *binfo)
  259. {
  260. struct inode *dat = nilfs_bmap_get_dat(direct);
  261. union nilfs_bmap_ptr_req req;
  262. int ret;
  263. req.bpr_ptr = ptr;
  264. ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
  265. if (!ret) {
  266. nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
  267. binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
  268. binfo->bi_v.bi_blkoff = cpu_to_le64(key);
  269. }
  270. return ret;
  271. }
  272. static int nilfs_direct_assign_p(struct nilfs_bmap *direct,
  273. __u64 key, __u64 ptr,
  274. struct buffer_head **bh,
  275. sector_t blocknr,
  276. union nilfs_binfo *binfo)
  277. {
  278. nilfs_direct_set_ptr(direct, key, blocknr);
  279. binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
  280. binfo->bi_dat.bi_level = 0;
  281. return 0;
  282. }
  283. static int nilfs_direct_assign(struct nilfs_bmap *bmap,
  284. struct buffer_head **bh,
  285. sector_t blocknr,
  286. union nilfs_binfo *binfo)
  287. {
  288. __u64 key;
  289. __u64 ptr;
  290. key = nilfs_bmap_data_get_key(bmap, *bh);
  291. if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
  292. printk(KERN_CRIT "%s: invalid key: %llu\n", __func__,
  293. (unsigned long long)key);
  294. return -EINVAL;
  295. }
  296. ptr = nilfs_direct_get_ptr(bmap, key);
  297. if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
  298. printk(KERN_CRIT "%s: invalid pointer: %llu\n", __func__,
  299. (unsigned long long)ptr);
  300. return -EINVAL;
  301. }
  302. return NILFS_BMAP_USE_VBN(bmap) ?
  303. nilfs_direct_assign_v(bmap, key, ptr, bh, blocknr, binfo) :
  304. nilfs_direct_assign_p(bmap, key, ptr, bh, blocknr, binfo);
  305. }
  306. static const struct nilfs_bmap_operations nilfs_direct_ops = {
  307. .bop_lookup = nilfs_direct_lookup,
  308. .bop_lookup_contig = nilfs_direct_lookup_contig,
  309. .bop_insert = nilfs_direct_insert,
  310. .bop_delete = nilfs_direct_delete,
  311. .bop_clear = NULL,
  312. .bop_propagate = nilfs_direct_propagate,
  313. .bop_lookup_dirty_buffers = NULL,
  314. .bop_assign = nilfs_direct_assign,
  315. .bop_mark = NULL,
  316. .bop_seek_key = nilfs_direct_seek_key,
  317. .bop_last_key = nilfs_direct_last_key,
  318. .bop_check_insert = nilfs_direct_check_insert,
  319. .bop_check_delete = NULL,
  320. .bop_gather_data = nilfs_direct_gather_data,
  321. };
  322. int nilfs_direct_init(struct nilfs_bmap *bmap)
  323. {
  324. bmap->b_ops = &nilfs_direct_ops;
  325. return 0;
  326. }