btree.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409
  1. /*
  2. * btree.c - NILFS B-tree.
  3. *
  4. * Copyright (C) 2005-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/slab.h>
  23. #include <linux/string.h>
  24. #include <linux/errno.h>
  25. #include <linux/pagevec.h>
  26. #include "nilfs.h"
  27. #include "page.h"
  28. #include "btnode.h"
  29. #include "btree.h"
  30. #include "alloc.h"
  31. #include "dat.h"
  32. static void __nilfs_btree_init(struct nilfs_bmap *bmap);
  33. static struct nilfs_btree_path *nilfs_btree_alloc_path(void)
  34. {
  35. struct nilfs_btree_path *path;
  36. int level = NILFS_BTREE_LEVEL_DATA;
  37. path = kmem_cache_alloc(nilfs_btree_path_cache, GFP_NOFS);
  38. if (path == NULL)
  39. goto out;
  40. for (; level < NILFS_BTREE_LEVEL_MAX; level++) {
  41. path[level].bp_bh = NULL;
  42. path[level].bp_sib_bh = NULL;
  43. path[level].bp_index = 0;
  44. path[level].bp_oldreq.bpr_ptr = NILFS_BMAP_INVALID_PTR;
  45. path[level].bp_newreq.bpr_ptr = NILFS_BMAP_INVALID_PTR;
  46. path[level].bp_op = NULL;
  47. }
  48. out:
  49. return path;
  50. }
  51. static void nilfs_btree_free_path(struct nilfs_btree_path *path)
  52. {
  53. int level = NILFS_BTREE_LEVEL_DATA;
  54. for (; level < NILFS_BTREE_LEVEL_MAX; level++)
  55. brelse(path[level].bp_bh);
  56. kmem_cache_free(nilfs_btree_path_cache, path);
  57. }
  58. /*
  59. * B-tree node operations
  60. */
  61. static int nilfs_btree_get_new_block(const struct nilfs_bmap *btree,
  62. __u64 ptr, struct buffer_head **bhp)
  63. {
  64. struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
  65. struct buffer_head *bh;
  66. bh = nilfs_btnode_create_block(btnc, ptr);
  67. if (!bh)
  68. return -ENOMEM;
  69. set_buffer_nilfs_volatile(bh);
  70. *bhp = bh;
  71. return 0;
  72. }
  73. static int nilfs_btree_node_get_flags(const struct nilfs_btree_node *node)
  74. {
  75. return node->bn_flags;
  76. }
  77. static void
  78. nilfs_btree_node_set_flags(struct nilfs_btree_node *node, int flags)
  79. {
  80. node->bn_flags = flags;
  81. }
  82. static int nilfs_btree_node_root(const struct nilfs_btree_node *node)
  83. {
  84. return nilfs_btree_node_get_flags(node) & NILFS_BTREE_NODE_ROOT;
  85. }
  86. static int nilfs_btree_node_get_level(const struct nilfs_btree_node *node)
  87. {
  88. return node->bn_level;
  89. }
  90. static void
  91. nilfs_btree_node_set_level(struct nilfs_btree_node *node, int level)
  92. {
  93. node->bn_level = level;
  94. }
  95. static int nilfs_btree_node_get_nchildren(const struct nilfs_btree_node *node)
  96. {
  97. return le16_to_cpu(node->bn_nchildren);
  98. }
  99. static void
  100. nilfs_btree_node_set_nchildren(struct nilfs_btree_node *node, int nchildren)
  101. {
  102. node->bn_nchildren = cpu_to_le16(nchildren);
  103. }
  104. static int nilfs_btree_node_size(const struct nilfs_bmap *btree)
  105. {
  106. return 1 << btree->b_inode->i_blkbits;
  107. }
  108. static int nilfs_btree_nchildren_per_block(const struct nilfs_bmap *btree)
  109. {
  110. return btree->b_nchildren_per_block;
  111. }
  112. static __le64 *
  113. nilfs_btree_node_dkeys(const struct nilfs_btree_node *node)
  114. {
  115. return (__le64 *)((char *)(node + 1) +
  116. (nilfs_btree_node_root(node) ?
  117. 0 : NILFS_BTREE_NODE_EXTRA_PAD_SIZE));
  118. }
  119. static __le64 *
  120. nilfs_btree_node_dptrs(const struct nilfs_btree_node *node, int ncmax)
  121. {
  122. return (__le64 *)(nilfs_btree_node_dkeys(node) + ncmax);
  123. }
  124. static __u64
  125. nilfs_btree_node_get_key(const struct nilfs_btree_node *node, int index)
  126. {
  127. return le64_to_cpu(*(nilfs_btree_node_dkeys(node) + index));
  128. }
  129. static void
  130. nilfs_btree_node_set_key(struct nilfs_btree_node *node, int index, __u64 key)
  131. {
  132. *(nilfs_btree_node_dkeys(node) + index) = cpu_to_le64(key);
  133. }
  134. static __u64
  135. nilfs_btree_node_get_ptr(const struct nilfs_btree_node *node, int index,
  136. int ncmax)
  137. {
  138. return le64_to_cpu(*(nilfs_btree_node_dptrs(node, ncmax) + index));
  139. }
  140. static void
  141. nilfs_btree_node_set_ptr(struct nilfs_btree_node *node, int index, __u64 ptr,
  142. int ncmax)
  143. {
  144. *(nilfs_btree_node_dptrs(node, ncmax) + index) = cpu_to_le64(ptr);
  145. }
  146. static void nilfs_btree_node_init(struct nilfs_btree_node *node, int flags,
  147. int level, int nchildren, int ncmax,
  148. const __u64 *keys, const __u64 *ptrs)
  149. {
  150. __le64 *dkeys;
  151. __le64 *dptrs;
  152. int i;
  153. nilfs_btree_node_set_flags(node, flags);
  154. nilfs_btree_node_set_level(node, level);
  155. nilfs_btree_node_set_nchildren(node, nchildren);
  156. dkeys = nilfs_btree_node_dkeys(node);
  157. dptrs = nilfs_btree_node_dptrs(node, ncmax);
  158. for (i = 0; i < nchildren; i++) {
  159. dkeys[i] = cpu_to_le64(keys[i]);
  160. dptrs[i] = cpu_to_le64(ptrs[i]);
  161. }
  162. }
  163. /* Assume the buffer heads corresponding to left and right are locked. */
  164. static void nilfs_btree_node_move_left(struct nilfs_btree_node *left,
  165. struct nilfs_btree_node *right,
  166. int n, int lncmax, int rncmax)
  167. {
  168. __le64 *ldkeys, *rdkeys;
  169. __le64 *ldptrs, *rdptrs;
  170. int lnchildren, rnchildren;
  171. ldkeys = nilfs_btree_node_dkeys(left);
  172. ldptrs = nilfs_btree_node_dptrs(left, lncmax);
  173. lnchildren = nilfs_btree_node_get_nchildren(left);
  174. rdkeys = nilfs_btree_node_dkeys(right);
  175. rdptrs = nilfs_btree_node_dptrs(right, rncmax);
  176. rnchildren = nilfs_btree_node_get_nchildren(right);
  177. memcpy(ldkeys + lnchildren, rdkeys, n * sizeof(*rdkeys));
  178. memcpy(ldptrs + lnchildren, rdptrs, n * sizeof(*rdptrs));
  179. memmove(rdkeys, rdkeys + n, (rnchildren - n) * sizeof(*rdkeys));
  180. memmove(rdptrs, rdptrs + n, (rnchildren - n) * sizeof(*rdptrs));
  181. lnchildren += n;
  182. rnchildren -= n;
  183. nilfs_btree_node_set_nchildren(left, lnchildren);
  184. nilfs_btree_node_set_nchildren(right, rnchildren);
  185. }
  186. /* Assume that the buffer heads corresponding to left and right are locked. */
  187. static void nilfs_btree_node_move_right(struct nilfs_btree_node *left,
  188. struct nilfs_btree_node *right,
  189. int n, int lncmax, int rncmax)
  190. {
  191. __le64 *ldkeys, *rdkeys;
  192. __le64 *ldptrs, *rdptrs;
  193. int lnchildren, rnchildren;
  194. ldkeys = nilfs_btree_node_dkeys(left);
  195. ldptrs = nilfs_btree_node_dptrs(left, lncmax);
  196. lnchildren = nilfs_btree_node_get_nchildren(left);
  197. rdkeys = nilfs_btree_node_dkeys(right);
  198. rdptrs = nilfs_btree_node_dptrs(right, rncmax);
  199. rnchildren = nilfs_btree_node_get_nchildren(right);
  200. memmove(rdkeys + n, rdkeys, rnchildren * sizeof(*rdkeys));
  201. memmove(rdptrs + n, rdptrs, rnchildren * sizeof(*rdptrs));
  202. memcpy(rdkeys, ldkeys + lnchildren - n, n * sizeof(*rdkeys));
  203. memcpy(rdptrs, ldptrs + lnchildren - n, n * sizeof(*rdptrs));
  204. lnchildren -= n;
  205. rnchildren += n;
  206. nilfs_btree_node_set_nchildren(left, lnchildren);
  207. nilfs_btree_node_set_nchildren(right, rnchildren);
  208. }
  209. /* Assume that the buffer head corresponding to node is locked. */
  210. static void nilfs_btree_node_insert(struct nilfs_btree_node *node, int index,
  211. __u64 key, __u64 ptr, int ncmax)
  212. {
  213. __le64 *dkeys;
  214. __le64 *dptrs;
  215. int nchildren;
  216. dkeys = nilfs_btree_node_dkeys(node);
  217. dptrs = nilfs_btree_node_dptrs(node, ncmax);
  218. nchildren = nilfs_btree_node_get_nchildren(node);
  219. if (index < nchildren) {
  220. memmove(dkeys + index + 1, dkeys + index,
  221. (nchildren - index) * sizeof(*dkeys));
  222. memmove(dptrs + index + 1, dptrs + index,
  223. (nchildren - index) * sizeof(*dptrs));
  224. }
  225. dkeys[index] = cpu_to_le64(key);
  226. dptrs[index] = cpu_to_le64(ptr);
  227. nchildren++;
  228. nilfs_btree_node_set_nchildren(node, nchildren);
  229. }
  230. /* Assume that the buffer head corresponding to node is locked. */
  231. static void nilfs_btree_node_delete(struct nilfs_btree_node *node, int index,
  232. __u64 *keyp, __u64 *ptrp, int ncmax)
  233. {
  234. __u64 key;
  235. __u64 ptr;
  236. __le64 *dkeys;
  237. __le64 *dptrs;
  238. int nchildren;
  239. dkeys = nilfs_btree_node_dkeys(node);
  240. dptrs = nilfs_btree_node_dptrs(node, ncmax);
  241. key = le64_to_cpu(dkeys[index]);
  242. ptr = le64_to_cpu(dptrs[index]);
  243. nchildren = nilfs_btree_node_get_nchildren(node);
  244. if (keyp != NULL)
  245. *keyp = key;
  246. if (ptrp != NULL)
  247. *ptrp = ptr;
  248. if (index < nchildren - 1) {
  249. memmove(dkeys + index, dkeys + index + 1,
  250. (nchildren - index - 1) * sizeof(*dkeys));
  251. memmove(dptrs + index, dptrs + index + 1,
  252. (nchildren - index - 1) * sizeof(*dptrs));
  253. }
  254. nchildren--;
  255. nilfs_btree_node_set_nchildren(node, nchildren);
  256. }
  257. static int nilfs_btree_node_lookup(const struct nilfs_btree_node *node,
  258. __u64 key, int *indexp)
  259. {
  260. __u64 nkey;
  261. int index, low, high, s;
  262. /* binary search */
  263. low = 0;
  264. high = nilfs_btree_node_get_nchildren(node) - 1;
  265. index = 0;
  266. s = 0;
  267. while (low <= high) {
  268. index = (low + high) / 2;
  269. nkey = nilfs_btree_node_get_key(node, index);
  270. if (nkey == key) {
  271. s = 0;
  272. goto out;
  273. } else if (nkey < key) {
  274. low = index + 1;
  275. s = -1;
  276. } else {
  277. high = index - 1;
  278. s = 1;
  279. }
  280. }
  281. /* adjust index */
  282. if (nilfs_btree_node_get_level(node) > NILFS_BTREE_LEVEL_NODE_MIN) {
  283. if (s > 0 && index > 0)
  284. index--;
  285. } else if (s < 0)
  286. index++;
  287. out:
  288. *indexp = index;
  289. return s == 0;
  290. }
  291. /**
  292. * nilfs_btree_node_broken - verify consistency of btree node
  293. * @node: btree node block to be examined
  294. * @size: node size (in bytes)
  295. * @blocknr: block number
  296. *
  297. * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
  298. */
  299. static int nilfs_btree_node_broken(const struct nilfs_btree_node *node,
  300. size_t size, sector_t blocknr)
  301. {
  302. int level, flags, nchildren;
  303. int ret = 0;
  304. level = nilfs_btree_node_get_level(node);
  305. flags = nilfs_btree_node_get_flags(node);
  306. nchildren = nilfs_btree_node_get_nchildren(node);
  307. if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN ||
  308. level >= NILFS_BTREE_LEVEL_MAX ||
  309. (flags & NILFS_BTREE_NODE_ROOT) ||
  310. nchildren < 0 ||
  311. nchildren > NILFS_BTREE_NODE_NCHILDREN_MAX(size))) {
  312. printk(KERN_CRIT "NILFS: bad btree node (blocknr=%llu): "
  313. "level = %d, flags = 0x%x, nchildren = %d\n",
  314. (unsigned long long)blocknr, level, flags, nchildren);
  315. ret = 1;
  316. }
  317. return ret;
  318. }
  319. /**
  320. * nilfs_btree_root_broken - verify consistency of btree root node
  321. * @node: btree root node to be examined
  322. * @ino: inode number
  323. *
  324. * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned.
  325. */
  326. static int nilfs_btree_root_broken(const struct nilfs_btree_node *node,
  327. unsigned long ino)
  328. {
  329. int level, flags, nchildren;
  330. int ret = 0;
  331. level = nilfs_btree_node_get_level(node);
  332. flags = nilfs_btree_node_get_flags(node);
  333. nchildren = nilfs_btree_node_get_nchildren(node);
  334. if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN ||
  335. level >= NILFS_BTREE_LEVEL_MAX ||
  336. nchildren < 0 ||
  337. nchildren > NILFS_BTREE_ROOT_NCHILDREN_MAX)) {
  338. pr_crit("NILFS: bad btree root (inode number=%lu): level = %d, flags = 0x%x, nchildren = %d\n",
  339. ino, level, flags, nchildren);
  340. ret = 1;
  341. }
  342. return ret;
  343. }
  344. int nilfs_btree_broken_node_block(struct buffer_head *bh)
  345. {
  346. int ret;
  347. if (buffer_nilfs_checked(bh))
  348. return 0;
  349. ret = nilfs_btree_node_broken((struct nilfs_btree_node *)bh->b_data,
  350. bh->b_size, bh->b_blocknr);
  351. if (likely(!ret))
  352. set_buffer_nilfs_checked(bh);
  353. return ret;
  354. }
  355. static struct nilfs_btree_node *
  356. nilfs_btree_get_root(const struct nilfs_bmap *btree)
  357. {
  358. return (struct nilfs_btree_node *)btree->b_u.u_data;
  359. }
  360. static struct nilfs_btree_node *
  361. nilfs_btree_get_nonroot_node(const struct nilfs_btree_path *path, int level)
  362. {
  363. return (struct nilfs_btree_node *)path[level].bp_bh->b_data;
  364. }
  365. static struct nilfs_btree_node *
  366. nilfs_btree_get_sib_node(const struct nilfs_btree_path *path, int level)
  367. {
  368. return (struct nilfs_btree_node *)path[level].bp_sib_bh->b_data;
  369. }
  370. static int nilfs_btree_height(const struct nilfs_bmap *btree)
  371. {
  372. return nilfs_btree_node_get_level(nilfs_btree_get_root(btree)) + 1;
  373. }
  374. static struct nilfs_btree_node *
  375. nilfs_btree_get_node(const struct nilfs_bmap *btree,
  376. const struct nilfs_btree_path *path,
  377. int level, int *ncmaxp)
  378. {
  379. struct nilfs_btree_node *node;
  380. if (level == nilfs_btree_height(btree) - 1) {
  381. node = nilfs_btree_get_root(btree);
  382. *ncmaxp = NILFS_BTREE_ROOT_NCHILDREN_MAX;
  383. } else {
  384. node = nilfs_btree_get_nonroot_node(path, level);
  385. *ncmaxp = nilfs_btree_nchildren_per_block(btree);
  386. }
  387. return node;
  388. }
  389. static int
  390. nilfs_btree_bad_node(struct nilfs_btree_node *node, int level)
  391. {
  392. if (unlikely(nilfs_btree_node_get_level(node) != level)) {
  393. dump_stack();
  394. printk(KERN_CRIT "NILFS: btree level mismatch: %d != %d\n",
  395. nilfs_btree_node_get_level(node), level);
  396. return 1;
  397. }
  398. return 0;
  399. }
  400. struct nilfs_btree_readahead_info {
  401. struct nilfs_btree_node *node; /* parent node */
  402. int max_ra_blocks; /* max nof blocks to read ahead */
  403. int index; /* current index on the parent node */
  404. int ncmax; /* nof children in the parent node */
  405. };
  406. static int __nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr,
  407. struct buffer_head **bhp,
  408. const struct nilfs_btree_readahead_info *ra)
  409. {
  410. struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache;
  411. struct buffer_head *bh, *ra_bh;
  412. sector_t submit_ptr = 0;
  413. int ret;
  414. ret = nilfs_btnode_submit_block(btnc, ptr, 0, READ, &bh, &submit_ptr);
  415. if (ret) {
  416. if (ret != -EEXIST)
  417. return ret;
  418. goto out_check;
  419. }
  420. if (ra) {
  421. int i, n;
  422. __u64 ptr2;
  423. /* read ahead sibling nodes */
  424. for (n = ra->max_ra_blocks, i = ra->index + 1;
  425. n > 0 && i < ra->ncmax; n--, i++) {
  426. ptr2 = nilfs_btree_node_get_ptr(ra->node, i, ra->ncmax);
  427. ret = nilfs_btnode_submit_block(btnc, ptr2, 0, READA,
  428. &ra_bh, &submit_ptr);
  429. if (likely(!ret || ret == -EEXIST))
  430. brelse(ra_bh);
  431. else if (ret != -EBUSY)
  432. break;
  433. if (!buffer_locked(bh))
  434. goto out_no_wait;
  435. }
  436. }
  437. wait_on_buffer(bh);
  438. out_no_wait:
  439. if (!buffer_uptodate(bh)) {
  440. brelse(bh);
  441. return -EIO;
  442. }
  443. out_check:
  444. if (nilfs_btree_broken_node_block(bh)) {
  445. clear_buffer_uptodate(bh);
  446. brelse(bh);
  447. return -EINVAL;
  448. }
  449. *bhp = bh;
  450. return 0;
  451. }
  452. static int nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr,
  453. struct buffer_head **bhp)
  454. {
  455. return __nilfs_btree_get_block(btree, ptr, bhp, NULL);
  456. }
  457. static int nilfs_btree_do_lookup(const struct nilfs_bmap *btree,
  458. struct nilfs_btree_path *path,
  459. __u64 key, __u64 *ptrp, int minlevel,
  460. int readahead)
  461. {
  462. struct nilfs_btree_node *node;
  463. struct nilfs_btree_readahead_info p, *ra;
  464. __u64 ptr;
  465. int level, index, found, ncmax, ret;
  466. node = nilfs_btree_get_root(btree);
  467. level = nilfs_btree_node_get_level(node);
  468. if (level < minlevel || nilfs_btree_node_get_nchildren(node) <= 0)
  469. return -ENOENT;
  470. found = nilfs_btree_node_lookup(node, key, &index);
  471. ptr = nilfs_btree_node_get_ptr(node, index,
  472. NILFS_BTREE_ROOT_NCHILDREN_MAX);
  473. path[level].bp_bh = NULL;
  474. path[level].bp_index = index;
  475. ncmax = nilfs_btree_nchildren_per_block(btree);
  476. while (--level >= minlevel) {
  477. ra = NULL;
  478. if (level == NILFS_BTREE_LEVEL_NODE_MIN && readahead) {
  479. p.node = nilfs_btree_get_node(btree, path, level + 1,
  480. &p.ncmax);
  481. p.index = index;
  482. p.max_ra_blocks = 7;
  483. ra = &p;
  484. }
  485. ret = __nilfs_btree_get_block(btree, ptr, &path[level].bp_bh,
  486. ra);
  487. if (ret < 0)
  488. return ret;
  489. node = nilfs_btree_get_nonroot_node(path, level);
  490. if (nilfs_btree_bad_node(node, level))
  491. return -EINVAL;
  492. if (!found)
  493. found = nilfs_btree_node_lookup(node, key, &index);
  494. else
  495. index = 0;
  496. if (index < ncmax) {
  497. ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
  498. } else {
  499. WARN_ON(found || level != NILFS_BTREE_LEVEL_NODE_MIN);
  500. /* insert */
  501. ptr = NILFS_BMAP_INVALID_PTR;
  502. }
  503. path[level].bp_index = index;
  504. }
  505. if (!found)
  506. return -ENOENT;
  507. if (ptrp != NULL)
  508. *ptrp = ptr;
  509. return 0;
  510. }
  511. static int nilfs_btree_do_lookup_last(const struct nilfs_bmap *btree,
  512. struct nilfs_btree_path *path,
  513. __u64 *keyp, __u64 *ptrp)
  514. {
  515. struct nilfs_btree_node *node;
  516. __u64 ptr;
  517. int index, level, ncmax, ret;
  518. node = nilfs_btree_get_root(btree);
  519. index = nilfs_btree_node_get_nchildren(node) - 1;
  520. if (index < 0)
  521. return -ENOENT;
  522. level = nilfs_btree_node_get_level(node);
  523. ptr = nilfs_btree_node_get_ptr(node, index,
  524. NILFS_BTREE_ROOT_NCHILDREN_MAX);
  525. path[level].bp_bh = NULL;
  526. path[level].bp_index = index;
  527. ncmax = nilfs_btree_nchildren_per_block(btree);
  528. for (level--; level > 0; level--) {
  529. ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh);
  530. if (ret < 0)
  531. return ret;
  532. node = nilfs_btree_get_nonroot_node(path, level);
  533. if (nilfs_btree_bad_node(node, level))
  534. return -EINVAL;
  535. index = nilfs_btree_node_get_nchildren(node) - 1;
  536. ptr = nilfs_btree_node_get_ptr(node, index, ncmax);
  537. path[level].bp_index = index;
  538. }
  539. if (keyp != NULL)
  540. *keyp = nilfs_btree_node_get_key(node, index);
  541. if (ptrp != NULL)
  542. *ptrp = ptr;
  543. return 0;
  544. }
  545. /**
  546. * nilfs_btree_get_next_key - get next valid key from btree path array
  547. * @btree: bmap struct of btree
  548. * @path: array of nilfs_btree_path struct
  549. * @minlevel: start level
  550. * @nextkey: place to store the next valid key
  551. *
  552. * Return Value: If a next key was found, 0 is returned. Otherwise,
  553. * -ENOENT is returned.
  554. */
  555. static int nilfs_btree_get_next_key(const struct nilfs_bmap *btree,
  556. const struct nilfs_btree_path *path,
  557. int minlevel, __u64 *nextkey)
  558. {
  559. struct nilfs_btree_node *node;
  560. int maxlevel = nilfs_btree_height(btree) - 1;
  561. int index, next_adj, level;
  562. /* Next index is already set to bp_index for leaf nodes. */
  563. next_adj = 0;
  564. for (level = minlevel; level <= maxlevel; level++) {
  565. if (level == maxlevel)
  566. node = nilfs_btree_get_root(btree);
  567. else
  568. node = nilfs_btree_get_nonroot_node(path, level);
  569. index = path[level].bp_index + next_adj;
  570. if (index < nilfs_btree_node_get_nchildren(node)) {
  571. /* Next key is in this node */
  572. *nextkey = nilfs_btree_node_get_key(node, index);
  573. return 0;
  574. }
  575. /* For non-leaf nodes, next index is stored at bp_index + 1. */
  576. next_adj = 1;
  577. }
  578. return -ENOENT;
  579. }
  580. static int nilfs_btree_lookup(const struct nilfs_bmap *btree,
  581. __u64 key, int level, __u64 *ptrp)
  582. {
  583. struct nilfs_btree_path *path;
  584. int ret;
  585. path = nilfs_btree_alloc_path();
  586. if (path == NULL)
  587. return -ENOMEM;
  588. ret = nilfs_btree_do_lookup(btree, path, key, ptrp, level, 0);
  589. nilfs_btree_free_path(path);
  590. return ret;
  591. }
  592. static int nilfs_btree_lookup_contig(const struct nilfs_bmap *btree,
  593. __u64 key, __u64 *ptrp, unsigned maxblocks)
  594. {
  595. struct nilfs_btree_path *path;
  596. struct nilfs_btree_node *node;
  597. struct inode *dat = NULL;
  598. __u64 ptr, ptr2;
  599. sector_t blocknr;
  600. int level = NILFS_BTREE_LEVEL_NODE_MIN;
  601. int ret, cnt, index, maxlevel, ncmax;
  602. struct nilfs_btree_readahead_info p;
  603. path = nilfs_btree_alloc_path();
  604. if (path == NULL)
  605. return -ENOMEM;
  606. ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level, 1);
  607. if (ret < 0)
  608. goto out;
  609. if (NILFS_BMAP_USE_VBN(btree)) {
  610. dat = nilfs_bmap_get_dat(btree);
  611. ret = nilfs_dat_translate(dat, ptr, &blocknr);
  612. if (ret < 0)
  613. goto out;
  614. ptr = blocknr;
  615. }
  616. cnt = 1;
  617. if (cnt == maxblocks)
  618. goto end;
  619. maxlevel = nilfs_btree_height(btree) - 1;
  620. node = nilfs_btree_get_node(btree, path, level, &ncmax);
  621. index = path[level].bp_index + 1;
  622. for (;;) {
  623. while (index < nilfs_btree_node_get_nchildren(node)) {
  624. if (nilfs_btree_node_get_key(node, index) !=
  625. key + cnt)
  626. goto end;
  627. ptr2 = nilfs_btree_node_get_ptr(node, index, ncmax);
  628. if (dat) {
  629. ret = nilfs_dat_translate(dat, ptr2, &blocknr);
  630. if (ret < 0)
  631. goto out;
  632. ptr2 = blocknr;
  633. }
  634. if (ptr2 != ptr + cnt || ++cnt == maxblocks)
  635. goto end;
  636. index++;
  637. continue;
  638. }
  639. if (level == maxlevel)
  640. break;
  641. /* look-up right sibling node */
  642. p.node = nilfs_btree_get_node(btree, path, level + 1, &p.ncmax);
  643. p.index = path[level + 1].bp_index + 1;
  644. p.max_ra_blocks = 7;
  645. if (p.index >= nilfs_btree_node_get_nchildren(p.node) ||
  646. nilfs_btree_node_get_key(p.node, p.index) != key + cnt)
  647. break;
  648. ptr2 = nilfs_btree_node_get_ptr(p.node, p.index, p.ncmax);
  649. path[level + 1].bp_index = p.index;
  650. brelse(path[level].bp_bh);
  651. path[level].bp_bh = NULL;
  652. ret = __nilfs_btree_get_block(btree, ptr2, &path[level].bp_bh,
  653. &p);
  654. if (ret < 0)
  655. goto out;
  656. node = nilfs_btree_get_nonroot_node(path, level);
  657. ncmax = nilfs_btree_nchildren_per_block(btree);
  658. index = 0;
  659. path[level].bp_index = index;
  660. }
  661. end:
  662. *ptrp = ptr;
  663. ret = cnt;
  664. out:
  665. nilfs_btree_free_path(path);
  666. return ret;
  667. }
  668. static void nilfs_btree_promote_key(struct nilfs_bmap *btree,
  669. struct nilfs_btree_path *path,
  670. int level, __u64 key)
  671. {
  672. if (level < nilfs_btree_height(btree) - 1) {
  673. do {
  674. nilfs_btree_node_set_key(
  675. nilfs_btree_get_nonroot_node(path, level),
  676. path[level].bp_index, key);
  677. if (!buffer_dirty(path[level].bp_bh))
  678. mark_buffer_dirty(path[level].bp_bh);
  679. } while ((path[level].bp_index == 0) &&
  680. (++level < nilfs_btree_height(btree) - 1));
  681. }
  682. /* root */
  683. if (level == nilfs_btree_height(btree) - 1) {
  684. nilfs_btree_node_set_key(nilfs_btree_get_root(btree),
  685. path[level].bp_index, key);
  686. }
  687. }
  688. static void nilfs_btree_do_insert(struct nilfs_bmap *btree,
  689. struct nilfs_btree_path *path,
  690. int level, __u64 *keyp, __u64 *ptrp)
  691. {
  692. struct nilfs_btree_node *node;
  693. int ncblk;
  694. if (level < nilfs_btree_height(btree) - 1) {
  695. node = nilfs_btree_get_nonroot_node(path, level);
  696. ncblk = nilfs_btree_nchildren_per_block(btree);
  697. nilfs_btree_node_insert(node, path[level].bp_index,
  698. *keyp, *ptrp, ncblk);
  699. if (!buffer_dirty(path[level].bp_bh))
  700. mark_buffer_dirty(path[level].bp_bh);
  701. if (path[level].bp_index == 0)
  702. nilfs_btree_promote_key(btree, path, level + 1,
  703. nilfs_btree_node_get_key(node,
  704. 0));
  705. } else {
  706. node = nilfs_btree_get_root(btree);
  707. nilfs_btree_node_insert(node, path[level].bp_index,
  708. *keyp, *ptrp,
  709. NILFS_BTREE_ROOT_NCHILDREN_MAX);
  710. }
  711. }
  712. static void nilfs_btree_carry_left(struct nilfs_bmap *btree,
  713. struct nilfs_btree_path *path,
  714. int level, __u64 *keyp, __u64 *ptrp)
  715. {
  716. struct nilfs_btree_node *node, *left;
  717. int nchildren, lnchildren, n, move, ncblk;
  718. node = nilfs_btree_get_nonroot_node(path, level);
  719. left = nilfs_btree_get_sib_node(path, level);
  720. nchildren = nilfs_btree_node_get_nchildren(node);
  721. lnchildren = nilfs_btree_node_get_nchildren(left);
  722. ncblk = nilfs_btree_nchildren_per_block(btree);
  723. move = 0;
  724. n = (nchildren + lnchildren + 1) / 2 - lnchildren;
  725. if (n > path[level].bp_index) {
  726. /* move insert point */
  727. n--;
  728. move = 1;
  729. }
  730. nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
  731. if (!buffer_dirty(path[level].bp_bh))
  732. mark_buffer_dirty(path[level].bp_bh);
  733. if (!buffer_dirty(path[level].bp_sib_bh))
  734. mark_buffer_dirty(path[level].bp_sib_bh);
  735. nilfs_btree_promote_key(btree, path, level + 1,
  736. nilfs_btree_node_get_key(node, 0));
  737. if (move) {
  738. brelse(path[level].bp_bh);
  739. path[level].bp_bh = path[level].bp_sib_bh;
  740. path[level].bp_sib_bh = NULL;
  741. path[level].bp_index += lnchildren;
  742. path[level + 1].bp_index--;
  743. } else {
  744. brelse(path[level].bp_sib_bh);
  745. path[level].bp_sib_bh = NULL;
  746. path[level].bp_index -= n;
  747. }
  748. nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
  749. }
  750. static void nilfs_btree_carry_right(struct nilfs_bmap *btree,
  751. struct nilfs_btree_path *path,
  752. int level, __u64 *keyp, __u64 *ptrp)
  753. {
  754. struct nilfs_btree_node *node, *right;
  755. int nchildren, rnchildren, n, move, ncblk;
  756. node = nilfs_btree_get_nonroot_node(path, level);
  757. right = nilfs_btree_get_sib_node(path, level);
  758. nchildren = nilfs_btree_node_get_nchildren(node);
  759. rnchildren = nilfs_btree_node_get_nchildren(right);
  760. ncblk = nilfs_btree_nchildren_per_block(btree);
  761. move = 0;
  762. n = (nchildren + rnchildren + 1) / 2 - rnchildren;
  763. if (n > nchildren - path[level].bp_index) {
  764. /* move insert point */
  765. n--;
  766. move = 1;
  767. }
  768. nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
  769. if (!buffer_dirty(path[level].bp_bh))
  770. mark_buffer_dirty(path[level].bp_bh);
  771. if (!buffer_dirty(path[level].bp_sib_bh))
  772. mark_buffer_dirty(path[level].bp_sib_bh);
  773. path[level + 1].bp_index++;
  774. nilfs_btree_promote_key(btree, path, level + 1,
  775. nilfs_btree_node_get_key(right, 0));
  776. path[level + 1].bp_index--;
  777. if (move) {
  778. brelse(path[level].bp_bh);
  779. path[level].bp_bh = path[level].bp_sib_bh;
  780. path[level].bp_sib_bh = NULL;
  781. path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
  782. path[level + 1].bp_index++;
  783. } else {
  784. brelse(path[level].bp_sib_bh);
  785. path[level].bp_sib_bh = NULL;
  786. }
  787. nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
  788. }
  789. static void nilfs_btree_split(struct nilfs_bmap *btree,
  790. struct nilfs_btree_path *path,
  791. int level, __u64 *keyp, __u64 *ptrp)
  792. {
  793. struct nilfs_btree_node *node, *right;
  794. int nchildren, n, move, ncblk;
  795. node = nilfs_btree_get_nonroot_node(path, level);
  796. right = nilfs_btree_get_sib_node(path, level);
  797. nchildren = nilfs_btree_node_get_nchildren(node);
  798. ncblk = nilfs_btree_nchildren_per_block(btree);
  799. move = 0;
  800. n = (nchildren + 1) / 2;
  801. if (n > nchildren - path[level].bp_index) {
  802. n--;
  803. move = 1;
  804. }
  805. nilfs_btree_node_move_right(node, right, n, ncblk, ncblk);
  806. if (!buffer_dirty(path[level].bp_bh))
  807. mark_buffer_dirty(path[level].bp_bh);
  808. if (!buffer_dirty(path[level].bp_sib_bh))
  809. mark_buffer_dirty(path[level].bp_sib_bh);
  810. if (move) {
  811. path[level].bp_index -= nilfs_btree_node_get_nchildren(node);
  812. nilfs_btree_node_insert(right, path[level].bp_index,
  813. *keyp, *ptrp, ncblk);
  814. *keyp = nilfs_btree_node_get_key(right, 0);
  815. *ptrp = path[level].bp_newreq.bpr_ptr;
  816. brelse(path[level].bp_bh);
  817. path[level].bp_bh = path[level].bp_sib_bh;
  818. path[level].bp_sib_bh = NULL;
  819. } else {
  820. nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
  821. *keyp = nilfs_btree_node_get_key(right, 0);
  822. *ptrp = path[level].bp_newreq.bpr_ptr;
  823. brelse(path[level].bp_sib_bh);
  824. path[level].bp_sib_bh = NULL;
  825. }
  826. path[level + 1].bp_index++;
  827. }
  828. static void nilfs_btree_grow(struct nilfs_bmap *btree,
  829. struct nilfs_btree_path *path,
  830. int level, __u64 *keyp, __u64 *ptrp)
  831. {
  832. struct nilfs_btree_node *root, *child;
  833. int n, ncblk;
  834. root = nilfs_btree_get_root(btree);
  835. child = nilfs_btree_get_sib_node(path, level);
  836. ncblk = nilfs_btree_nchildren_per_block(btree);
  837. n = nilfs_btree_node_get_nchildren(root);
  838. nilfs_btree_node_move_right(root, child, n,
  839. NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
  840. nilfs_btree_node_set_level(root, level + 1);
  841. if (!buffer_dirty(path[level].bp_sib_bh))
  842. mark_buffer_dirty(path[level].bp_sib_bh);
  843. path[level].bp_bh = path[level].bp_sib_bh;
  844. path[level].bp_sib_bh = NULL;
  845. nilfs_btree_do_insert(btree, path, level, keyp, ptrp);
  846. *keyp = nilfs_btree_node_get_key(child, 0);
  847. *ptrp = path[level].bp_newreq.bpr_ptr;
  848. }
  849. static __u64 nilfs_btree_find_near(const struct nilfs_bmap *btree,
  850. const struct nilfs_btree_path *path)
  851. {
  852. struct nilfs_btree_node *node;
  853. int level, ncmax;
  854. if (path == NULL)
  855. return NILFS_BMAP_INVALID_PTR;
  856. /* left sibling */
  857. level = NILFS_BTREE_LEVEL_NODE_MIN;
  858. if (path[level].bp_index > 0) {
  859. node = nilfs_btree_get_node(btree, path, level, &ncmax);
  860. return nilfs_btree_node_get_ptr(node,
  861. path[level].bp_index - 1,
  862. ncmax);
  863. }
  864. /* parent */
  865. level = NILFS_BTREE_LEVEL_NODE_MIN + 1;
  866. if (level <= nilfs_btree_height(btree) - 1) {
  867. node = nilfs_btree_get_node(btree, path, level, &ncmax);
  868. return nilfs_btree_node_get_ptr(node, path[level].bp_index,
  869. ncmax);
  870. }
  871. return NILFS_BMAP_INVALID_PTR;
  872. }
  873. static __u64 nilfs_btree_find_target_v(const struct nilfs_bmap *btree,
  874. const struct nilfs_btree_path *path,
  875. __u64 key)
  876. {
  877. __u64 ptr;
  878. ptr = nilfs_bmap_find_target_seq(btree, key);
  879. if (ptr != NILFS_BMAP_INVALID_PTR)
  880. /* sequential access */
  881. return ptr;
  882. else {
  883. ptr = nilfs_btree_find_near(btree, path);
  884. if (ptr != NILFS_BMAP_INVALID_PTR)
  885. /* near */
  886. return ptr;
  887. }
  888. /* block group */
  889. return nilfs_bmap_find_target_in_group(btree);
  890. }
  891. static int nilfs_btree_prepare_insert(struct nilfs_bmap *btree,
  892. struct nilfs_btree_path *path,
  893. int *levelp, __u64 key, __u64 ptr,
  894. struct nilfs_bmap_stats *stats)
  895. {
  896. struct buffer_head *bh;
  897. struct nilfs_btree_node *node, *parent, *sib;
  898. __u64 sibptr;
  899. int pindex, level, ncmax, ncblk, ret;
  900. struct inode *dat = NULL;
  901. stats->bs_nblocks = 0;
  902. level = NILFS_BTREE_LEVEL_DATA;
  903. /* allocate a new ptr for data block */
  904. if (NILFS_BMAP_USE_VBN(btree)) {
  905. path[level].bp_newreq.bpr_ptr =
  906. nilfs_btree_find_target_v(btree, path, key);
  907. dat = nilfs_bmap_get_dat(btree);
  908. }
  909. ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
  910. if (ret < 0)
  911. goto err_out_data;
  912. ncblk = nilfs_btree_nchildren_per_block(btree);
  913. for (level = NILFS_BTREE_LEVEL_NODE_MIN;
  914. level < nilfs_btree_height(btree) - 1;
  915. level++) {
  916. node = nilfs_btree_get_nonroot_node(path, level);
  917. if (nilfs_btree_node_get_nchildren(node) < ncblk) {
  918. path[level].bp_op = nilfs_btree_do_insert;
  919. stats->bs_nblocks++;
  920. goto out;
  921. }
  922. parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
  923. pindex = path[level + 1].bp_index;
  924. /* left sibling */
  925. if (pindex > 0) {
  926. sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
  927. ncmax);
  928. ret = nilfs_btree_get_block(btree, sibptr, &bh);
  929. if (ret < 0)
  930. goto err_out_child_node;
  931. sib = (struct nilfs_btree_node *)bh->b_data;
  932. if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
  933. path[level].bp_sib_bh = bh;
  934. path[level].bp_op = nilfs_btree_carry_left;
  935. stats->bs_nblocks++;
  936. goto out;
  937. } else {
  938. brelse(bh);
  939. }
  940. }
  941. /* right sibling */
  942. if (pindex < nilfs_btree_node_get_nchildren(parent) - 1) {
  943. sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
  944. ncmax);
  945. ret = nilfs_btree_get_block(btree, sibptr, &bh);
  946. if (ret < 0)
  947. goto err_out_child_node;
  948. sib = (struct nilfs_btree_node *)bh->b_data;
  949. if (nilfs_btree_node_get_nchildren(sib) < ncblk) {
  950. path[level].bp_sib_bh = bh;
  951. path[level].bp_op = nilfs_btree_carry_right;
  952. stats->bs_nblocks++;
  953. goto out;
  954. } else {
  955. brelse(bh);
  956. }
  957. }
  958. /* split */
  959. path[level].bp_newreq.bpr_ptr =
  960. path[level - 1].bp_newreq.bpr_ptr + 1;
  961. ret = nilfs_bmap_prepare_alloc_ptr(btree,
  962. &path[level].bp_newreq, dat);
  963. if (ret < 0)
  964. goto err_out_child_node;
  965. ret = nilfs_btree_get_new_block(btree,
  966. path[level].bp_newreq.bpr_ptr,
  967. &bh);
  968. if (ret < 0)
  969. goto err_out_curr_node;
  970. stats->bs_nblocks++;
  971. sib = (struct nilfs_btree_node *)bh->b_data;
  972. nilfs_btree_node_init(sib, 0, level, 0, ncblk, NULL, NULL);
  973. path[level].bp_sib_bh = bh;
  974. path[level].bp_op = nilfs_btree_split;
  975. }
  976. /* root */
  977. node = nilfs_btree_get_root(btree);
  978. if (nilfs_btree_node_get_nchildren(node) <
  979. NILFS_BTREE_ROOT_NCHILDREN_MAX) {
  980. path[level].bp_op = nilfs_btree_do_insert;
  981. stats->bs_nblocks++;
  982. goto out;
  983. }
  984. /* grow */
  985. path[level].bp_newreq.bpr_ptr = path[level - 1].bp_newreq.bpr_ptr + 1;
  986. ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat);
  987. if (ret < 0)
  988. goto err_out_child_node;
  989. ret = nilfs_btree_get_new_block(btree, path[level].bp_newreq.bpr_ptr,
  990. &bh);
  991. if (ret < 0)
  992. goto err_out_curr_node;
  993. nilfs_btree_node_init((struct nilfs_btree_node *)bh->b_data,
  994. 0, level, 0, ncblk, NULL, NULL);
  995. path[level].bp_sib_bh = bh;
  996. path[level].bp_op = nilfs_btree_grow;
  997. level++;
  998. path[level].bp_op = nilfs_btree_do_insert;
  999. /* a newly-created node block and a data block are added */
  1000. stats->bs_nblocks += 2;
  1001. /* success */
  1002. out:
  1003. *levelp = level;
  1004. return ret;
  1005. /* error */
  1006. err_out_curr_node:
  1007. nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
  1008. err_out_child_node:
  1009. for (level--; level > NILFS_BTREE_LEVEL_DATA; level--) {
  1010. nilfs_btnode_delete(path[level].bp_sib_bh);
  1011. nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
  1012. }
  1013. nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat);
  1014. err_out_data:
  1015. *levelp = level;
  1016. stats->bs_nblocks = 0;
  1017. return ret;
  1018. }
  1019. static void nilfs_btree_commit_insert(struct nilfs_bmap *btree,
  1020. struct nilfs_btree_path *path,
  1021. int maxlevel, __u64 key, __u64 ptr)
  1022. {
  1023. struct inode *dat = NULL;
  1024. int level;
  1025. set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
  1026. ptr = path[NILFS_BTREE_LEVEL_DATA].bp_newreq.bpr_ptr;
  1027. if (NILFS_BMAP_USE_VBN(btree)) {
  1028. nilfs_bmap_set_target_v(btree, key, ptr);
  1029. dat = nilfs_bmap_get_dat(btree);
  1030. }
  1031. for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
  1032. nilfs_bmap_commit_alloc_ptr(btree,
  1033. &path[level - 1].bp_newreq, dat);
  1034. path[level].bp_op(btree, path, level, &key, &ptr);
  1035. }
  1036. if (!nilfs_bmap_dirty(btree))
  1037. nilfs_bmap_set_dirty(btree);
  1038. }
  1039. static int nilfs_btree_insert(struct nilfs_bmap *btree, __u64 key, __u64 ptr)
  1040. {
  1041. struct nilfs_btree_path *path;
  1042. struct nilfs_bmap_stats stats;
  1043. int level, ret;
  1044. path = nilfs_btree_alloc_path();
  1045. if (path == NULL)
  1046. return -ENOMEM;
  1047. ret = nilfs_btree_do_lookup(btree, path, key, NULL,
  1048. NILFS_BTREE_LEVEL_NODE_MIN, 0);
  1049. if (ret != -ENOENT) {
  1050. if (ret == 0)
  1051. ret = -EEXIST;
  1052. goto out;
  1053. }
  1054. ret = nilfs_btree_prepare_insert(btree, path, &level, key, ptr, &stats);
  1055. if (ret < 0)
  1056. goto out;
  1057. nilfs_btree_commit_insert(btree, path, level, key, ptr);
  1058. nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks);
  1059. out:
  1060. nilfs_btree_free_path(path);
  1061. return ret;
  1062. }
  1063. static void nilfs_btree_do_delete(struct nilfs_bmap *btree,
  1064. struct nilfs_btree_path *path,
  1065. int level, __u64 *keyp, __u64 *ptrp)
  1066. {
  1067. struct nilfs_btree_node *node;
  1068. int ncblk;
  1069. if (level < nilfs_btree_height(btree) - 1) {
  1070. node = nilfs_btree_get_nonroot_node(path, level);
  1071. ncblk = nilfs_btree_nchildren_per_block(btree);
  1072. nilfs_btree_node_delete(node, path[level].bp_index,
  1073. keyp, ptrp, ncblk);
  1074. if (!buffer_dirty(path[level].bp_bh))
  1075. mark_buffer_dirty(path[level].bp_bh);
  1076. if (path[level].bp_index == 0)
  1077. nilfs_btree_promote_key(btree, path, level + 1,
  1078. nilfs_btree_node_get_key(node, 0));
  1079. } else {
  1080. node = nilfs_btree_get_root(btree);
  1081. nilfs_btree_node_delete(node, path[level].bp_index,
  1082. keyp, ptrp,
  1083. NILFS_BTREE_ROOT_NCHILDREN_MAX);
  1084. }
  1085. }
  1086. static void nilfs_btree_borrow_left(struct nilfs_bmap *btree,
  1087. struct nilfs_btree_path *path,
  1088. int level, __u64 *keyp, __u64 *ptrp)
  1089. {
  1090. struct nilfs_btree_node *node, *left;
  1091. int nchildren, lnchildren, n, ncblk;
  1092. nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
  1093. node = nilfs_btree_get_nonroot_node(path, level);
  1094. left = nilfs_btree_get_sib_node(path, level);
  1095. nchildren = nilfs_btree_node_get_nchildren(node);
  1096. lnchildren = nilfs_btree_node_get_nchildren(left);
  1097. ncblk = nilfs_btree_nchildren_per_block(btree);
  1098. n = (nchildren + lnchildren) / 2 - nchildren;
  1099. nilfs_btree_node_move_right(left, node, n, ncblk, ncblk);
  1100. if (!buffer_dirty(path[level].bp_bh))
  1101. mark_buffer_dirty(path[level].bp_bh);
  1102. if (!buffer_dirty(path[level].bp_sib_bh))
  1103. mark_buffer_dirty(path[level].bp_sib_bh);
  1104. nilfs_btree_promote_key(btree, path, level + 1,
  1105. nilfs_btree_node_get_key(node, 0));
  1106. brelse(path[level].bp_sib_bh);
  1107. path[level].bp_sib_bh = NULL;
  1108. path[level].bp_index += n;
  1109. }
  1110. static void nilfs_btree_borrow_right(struct nilfs_bmap *btree,
  1111. struct nilfs_btree_path *path,
  1112. int level, __u64 *keyp, __u64 *ptrp)
  1113. {
  1114. struct nilfs_btree_node *node, *right;
  1115. int nchildren, rnchildren, n, ncblk;
  1116. nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
  1117. node = nilfs_btree_get_nonroot_node(path, level);
  1118. right = nilfs_btree_get_sib_node(path, level);
  1119. nchildren = nilfs_btree_node_get_nchildren(node);
  1120. rnchildren = nilfs_btree_node_get_nchildren(right);
  1121. ncblk = nilfs_btree_nchildren_per_block(btree);
  1122. n = (nchildren + rnchildren) / 2 - nchildren;
  1123. nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
  1124. if (!buffer_dirty(path[level].bp_bh))
  1125. mark_buffer_dirty(path[level].bp_bh);
  1126. if (!buffer_dirty(path[level].bp_sib_bh))
  1127. mark_buffer_dirty(path[level].bp_sib_bh);
  1128. path[level + 1].bp_index++;
  1129. nilfs_btree_promote_key(btree, path, level + 1,
  1130. nilfs_btree_node_get_key(right, 0));
  1131. path[level + 1].bp_index--;
  1132. brelse(path[level].bp_sib_bh);
  1133. path[level].bp_sib_bh = NULL;
  1134. }
  1135. static void nilfs_btree_concat_left(struct nilfs_bmap *btree,
  1136. struct nilfs_btree_path *path,
  1137. int level, __u64 *keyp, __u64 *ptrp)
  1138. {
  1139. struct nilfs_btree_node *node, *left;
  1140. int n, ncblk;
  1141. nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
  1142. node = nilfs_btree_get_nonroot_node(path, level);
  1143. left = nilfs_btree_get_sib_node(path, level);
  1144. ncblk = nilfs_btree_nchildren_per_block(btree);
  1145. n = nilfs_btree_node_get_nchildren(node);
  1146. nilfs_btree_node_move_left(left, node, n, ncblk, ncblk);
  1147. if (!buffer_dirty(path[level].bp_sib_bh))
  1148. mark_buffer_dirty(path[level].bp_sib_bh);
  1149. nilfs_btnode_delete(path[level].bp_bh);
  1150. path[level].bp_bh = path[level].bp_sib_bh;
  1151. path[level].bp_sib_bh = NULL;
  1152. path[level].bp_index += nilfs_btree_node_get_nchildren(left);
  1153. }
  1154. static void nilfs_btree_concat_right(struct nilfs_bmap *btree,
  1155. struct nilfs_btree_path *path,
  1156. int level, __u64 *keyp, __u64 *ptrp)
  1157. {
  1158. struct nilfs_btree_node *node, *right;
  1159. int n, ncblk;
  1160. nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
  1161. node = nilfs_btree_get_nonroot_node(path, level);
  1162. right = nilfs_btree_get_sib_node(path, level);
  1163. ncblk = nilfs_btree_nchildren_per_block(btree);
  1164. n = nilfs_btree_node_get_nchildren(right);
  1165. nilfs_btree_node_move_left(node, right, n, ncblk, ncblk);
  1166. if (!buffer_dirty(path[level].bp_bh))
  1167. mark_buffer_dirty(path[level].bp_bh);
  1168. nilfs_btnode_delete(path[level].bp_sib_bh);
  1169. path[level].bp_sib_bh = NULL;
  1170. path[level + 1].bp_index++;
  1171. }
  1172. static void nilfs_btree_shrink(struct nilfs_bmap *btree,
  1173. struct nilfs_btree_path *path,
  1174. int level, __u64 *keyp, __u64 *ptrp)
  1175. {
  1176. struct nilfs_btree_node *root, *child;
  1177. int n, ncblk;
  1178. nilfs_btree_do_delete(btree, path, level, keyp, ptrp);
  1179. root = nilfs_btree_get_root(btree);
  1180. child = nilfs_btree_get_nonroot_node(path, level);
  1181. ncblk = nilfs_btree_nchildren_per_block(btree);
  1182. nilfs_btree_node_delete(root, 0, NULL, NULL,
  1183. NILFS_BTREE_ROOT_NCHILDREN_MAX);
  1184. nilfs_btree_node_set_level(root, level);
  1185. n = nilfs_btree_node_get_nchildren(child);
  1186. nilfs_btree_node_move_left(root, child, n,
  1187. NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk);
  1188. nilfs_btnode_delete(path[level].bp_bh);
  1189. path[level].bp_bh = NULL;
  1190. }
  1191. static void nilfs_btree_nop(struct nilfs_bmap *btree,
  1192. struct nilfs_btree_path *path,
  1193. int level, __u64 *keyp, __u64 *ptrp)
  1194. {
  1195. }
  1196. static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree,
  1197. struct nilfs_btree_path *path,
  1198. int *levelp,
  1199. struct nilfs_bmap_stats *stats,
  1200. struct inode *dat)
  1201. {
  1202. struct buffer_head *bh;
  1203. struct nilfs_btree_node *node, *parent, *sib;
  1204. __u64 sibptr;
  1205. int pindex, dindex, level, ncmin, ncmax, ncblk, ret;
  1206. ret = 0;
  1207. stats->bs_nblocks = 0;
  1208. ncmin = NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree));
  1209. ncblk = nilfs_btree_nchildren_per_block(btree);
  1210. for (level = NILFS_BTREE_LEVEL_NODE_MIN, dindex = path[level].bp_index;
  1211. level < nilfs_btree_height(btree) - 1;
  1212. level++) {
  1213. node = nilfs_btree_get_nonroot_node(path, level);
  1214. path[level].bp_oldreq.bpr_ptr =
  1215. nilfs_btree_node_get_ptr(node, dindex, ncblk);
  1216. ret = nilfs_bmap_prepare_end_ptr(btree,
  1217. &path[level].bp_oldreq, dat);
  1218. if (ret < 0)
  1219. goto err_out_child_node;
  1220. if (nilfs_btree_node_get_nchildren(node) > ncmin) {
  1221. path[level].bp_op = nilfs_btree_do_delete;
  1222. stats->bs_nblocks++;
  1223. goto out;
  1224. }
  1225. parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
  1226. pindex = path[level + 1].bp_index;
  1227. dindex = pindex;
  1228. if (pindex > 0) {
  1229. /* left sibling */
  1230. sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1,
  1231. ncmax);
  1232. ret = nilfs_btree_get_block(btree, sibptr, &bh);
  1233. if (ret < 0)
  1234. goto err_out_curr_node;
  1235. sib = (struct nilfs_btree_node *)bh->b_data;
  1236. if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
  1237. path[level].bp_sib_bh = bh;
  1238. path[level].bp_op = nilfs_btree_borrow_left;
  1239. stats->bs_nblocks++;
  1240. goto out;
  1241. } else {
  1242. path[level].bp_sib_bh = bh;
  1243. path[level].bp_op = nilfs_btree_concat_left;
  1244. stats->bs_nblocks++;
  1245. /* continue; */
  1246. }
  1247. } else if (pindex <
  1248. nilfs_btree_node_get_nchildren(parent) - 1) {
  1249. /* right sibling */
  1250. sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1,
  1251. ncmax);
  1252. ret = nilfs_btree_get_block(btree, sibptr, &bh);
  1253. if (ret < 0)
  1254. goto err_out_curr_node;
  1255. sib = (struct nilfs_btree_node *)bh->b_data;
  1256. if (nilfs_btree_node_get_nchildren(sib) > ncmin) {
  1257. path[level].bp_sib_bh = bh;
  1258. path[level].bp_op = nilfs_btree_borrow_right;
  1259. stats->bs_nblocks++;
  1260. goto out;
  1261. } else {
  1262. path[level].bp_sib_bh = bh;
  1263. path[level].bp_op = nilfs_btree_concat_right;
  1264. stats->bs_nblocks++;
  1265. /*
  1266. * When merging right sibling node
  1267. * into the current node, pointer to
  1268. * the right sibling node must be
  1269. * terminated instead. The adjustment
  1270. * below is required for that.
  1271. */
  1272. dindex = pindex + 1;
  1273. /* continue; */
  1274. }
  1275. } else {
  1276. /* no siblings */
  1277. /* the only child of the root node */
  1278. WARN_ON(level != nilfs_btree_height(btree) - 2);
  1279. if (nilfs_btree_node_get_nchildren(node) - 1 <=
  1280. NILFS_BTREE_ROOT_NCHILDREN_MAX) {
  1281. path[level].bp_op = nilfs_btree_shrink;
  1282. stats->bs_nblocks += 2;
  1283. level++;
  1284. path[level].bp_op = nilfs_btree_nop;
  1285. goto shrink_root_child;
  1286. } else {
  1287. path[level].bp_op = nilfs_btree_do_delete;
  1288. stats->bs_nblocks++;
  1289. goto out;
  1290. }
  1291. }
  1292. }
  1293. /* child of the root node is deleted */
  1294. path[level].bp_op = nilfs_btree_do_delete;
  1295. stats->bs_nblocks++;
  1296. shrink_root_child:
  1297. node = nilfs_btree_get_root(btree);
  1298. path[level].bp_oldreq.bpr_ptr =
  1299. nilfs_btree_node_get_ptr(node, dindex,
  1300. NILFS_BTREE_ROOT_NCHILDREN_MAX);
  1301. ret = nilfs_bmap_prepare_end_ptr(btree, &path[level].bp_oldreq, dat);
  1302. if (ret < 0)
  1303. goto err_out_child_node;
  1304. /* success */
  1305. out:
  1306. *levelp = level;
  1307. return ret;
  1308. /* error */
  1309. err_out_curr_node:
  1310. nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
  1311. err_out_child_node:
  1312. for (level--; level >= NILFS_BTREE_LEVEL_NODE_MIN; level--) {
  1313. brelse(path[level].bp_sib_bh);
  1314. nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat);
  1315. }
  1316. *levelp = level;
  1317. stats->bs_nblocks = 0;
  1318. return ret;
  1319. }
  1320. static void nilfs_btree_commit_delete(struct nilfs_bmap *btree,
  1321. struct nilfs_btree_path *path,
  1322. int maxlevel, struct inode *dat)
  1323. {
  1324. int level;
  1325. for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) {
  1326. nilfs_bmap_commit_end_ptr(btree, &path[level].bp_oldreq, dat);
  1327. path[level].bp_op(btree, path, level, NULL, NULL);
  1328. }
  1329. if (!nilfs_bmap_dirty(btree))
  1330. nilfs_bmap_set_dirty(btree);
  1331. }
  1332. static int nilfs_btree_delete(struct nilfs_bmap *btree, __u64 key)
  1333. {
  1334. struct nilfs_btree_path *path;
  1335. struct nilfs_bmap_stats stats;
  1336. struct inode *dat;
  1337. int level, ret;
  1338. path = nilfs_btree_alloc_path();
  1339. if (path == NULL)
  1340. return -ENOMEM;
  1341. ret = nilfs_btree_do_lookup(btree, path, key, NULL,
  1342. NILFS_BTREE_LEVEL_NODE_MIN, 0);
  1343. if (ret < 0)
  1344. goto out;
  1345. dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
  1346. ret = nilfs_btree_prepare_delete(btree, path, &level, &stats, dat);
  1347. if (ret < 0)
  1348. goto out;
  1349. nilfs_btree_commit_delete(btree, path, level, dat);
  1350. nilfs_inode_sub_blocks(btree->b_inode, stats.bs_nblocks);
  1351. out:
  1352. nilfs_btree_free_path(path);
  1353. return ret;
  1354. }
  1355. static int nilfs_btree_seek_key(const struct nilfs_bmap *btree, __u64 start,
  1356. __u64 *keyp)
  1357. {
  1358. struct nilfs_btree_path *path;
  1359. const int minlevel = NILFS_BTREE_LEVEL_NODE_MIN;
  1360. int ret;
  1361. path = nilfs_btree_alloc_path();
  1362. if (!path)
  1363. return -ENOMEM;
  1364. ret = nilfs_btree_do_lookup(btree, path, start, NULL, minlevel, 0);
  1365. if (!ret)
  1366. *keyp = start;
  1367. else if (ret == -ENOENT)
  1368. ret = nilfs_btree_get_next_key(btree, path, minlevel, keyp);
  1369. nilfs_btree_free_path(path);
  1370. return ret;
  1371. }
  1372. static int nilfs_btree_last_key(const struct nilfs_bmap *btree, __u64 *keyp)
  1373. {
  1374. struct nilfs_btree_path *path;
  1375. int ret;
  1376. path = nilfs_btree_alloc_path();
  1377. if (path == NULL)
  1378. return -ENOMEM;
  1379. ret = nilfs_btree_do_lookup_last(btree, path, keyp, NULL);
  1380. nilfs_btree_free_path(path);
  1381. return ret;
  1382. }
  1383. static int nilfs_btree_check_delete(struct nilfs_bmap *btree, __u64 key)
  1384. {
  1385. struct buffer_head *bh;
  1386. struct nilfs_btree_node *root, *node;
  1387. __u64 maxkey, nextmaxkey;
  1388. __u64 ptr;
  1389. int nchildren, ret;
  1390. root = nilfs_btree_get_root(btree);
  1391. switch (nilfs_btree_height(btree)) {
  1392. case 2:
  1393. bh = NULL;
  1394. node = root;
  1395. break;
  1396. case 3:
  1397. nchildren = nilfs_btree_node_get_nchildren(root);
  1398. if (nchildren > 1)
  1399. return 0;
  1400. ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
  1401. NILFS_BTREE_ROOT_NCHILDREN_MAX);
  1402. ret = nilfs_btree_get_block(btree, ptr, &bh);
  1403. if (ret < 0)
  1404. return ret;
  1405. node = (struct nilfs_btree_node *)bh->b_data;
  1406. break;
  1407. default:
  1408. return 0;
  1409. }
  1410. nchildren = nilfs_btree_node_get_nchildren(node);
  1411. maxkey = nilfs_btree_node_get_key(node, nchildren - 1);
  1412. nextmaxkey = (nchildren > 1) ?
  1413. nilfs_btree_node_get_key(node, nchildren - 2) : 0;
  1414. if (bh != NULL)
  1415. brelse(bh);
  1416. return (maxkey == key) && (nextmaxkey < NILFS_BMAP_LARGE_LOW);
  1417. }
  1418. static int nilfs_btree_gather_data(struct nilfs_bmap *btree,
  1419. __u64 *keys, __u64 *ptrs, int nitems)
  1420. {
  1421. struct buffer_head *bh;
  1422. struct nilfs_btree_node *node, *root;
  1423. __le64 *dkeys;
  1424. __le64 *dptrs;
  1425. __u64 ptr;
  1426. int nchildren, ncmax, i, ret;
  1427. root = nilfs_btree_get_root(btree);
  1428. switch (nilfs_btree_height(btree)) {
  1429. case 2:
  1430. bh = NULL;
  1431. node = root;
  1432. ncmax = NILFS_BTREE_ROOT_NCHILDREN_MAX;
  1433. break;
  1434. case 3:
  1435. nchildren = nilfs_btree_node_get_nchildren(root);
  1436. WARN_ON(nchildren > 1);
  1437. ptr = nilfs_btree_node_get_ptr(root, nchildren - 1,
  1438. NILFS_BTREE_ROOT_NCHILDREN_MAX);
  1439. ret = nilfs_btree_get_block(btree, ptr, &bh);
  1440. if (ret < 0)
  1441. return ret;
  1442. node = (struct nilfs_btree_node *)bh->b_data;
  1443. ncmax = nilfs_btree_nchildren_per_block(btree);
  1444. break;
  1445. default:
  1446. node = NULL;
  1447. return -EINVAL;
  1448. }
  1449. nchildren = nilfs_btree_node_get_nchildren(node);
  1450. if (nchildren < nitems)
  1451. nitems = nchildren;
  1452. dkeys = nilfs_btree_node_dkeys(node);
  1453. dptrs = nilfs_btree_node_dptrs(node, ncmax);
  1454. for (i = 0; i < nitems; i++) {
  1455. keys[i] = le64_to_cpu(dkeys[i]);
  1456. ptrs[i] = le64_to_cpu(dptrs[i]);
  1457. }
  1458. if (bh != NULL)
  1459. brelse(bh);
  1460. return nitems;
  1461. }
  1462. static int
  1463. nilfs_btree_prepare_convert_and_insert(struct nilfs_bmap *btree, __u64 key,
  1464. union nilfs_bmap_ptr_req *dreq,
  1465. union nilfs_bmap_ptr_req *nreq,
  1466. struct buffer_head **bhp,
  1467. struct nilfs_bmap_stats *stats)
  1468. {
  1469. struct buffer_head *bh;
  1470. struct inode *dat = NULL;
  1471. int ret;
  1472. stats->bs_nblocks = 0;
  1473. /* for data */
  1474. /* cannot find near ptr */
  1475. if (NILFS_BMAP_USE_VBN(btree)) {
  1476. dreq->bpr_ptr = nilfs_btree_find_target_v(btree, NULL, key);
  1477. dat = nilfs_bmap_get_dat(btree);
  1478. }
  1479. ret = nilfs_bmap_prepare_alloc_ptr(btree, dreq, dat);
  1480. if (ret < 0)
  1481. return ret;
  1482. *bhp = NULL;
  1483. stats->bs_nblocks++;
  1484. if (nreq != NULL) {
  1485. nreq->bpr_ptr = dreq->bpr_ptr + 1;
  1486. ret = nilfs_bmap_prepare_alloc_ptr(btree, nreq, dat);
  1487. if (ret < 0)
  1488. goto err_out_dreq;
  1489. ret = nilfs_btree_get_new_block(btree, nreq->bpr_ptr, &bh);
  1490. if (ret < 0)
  1491. goto err_out_nreq;
  1492. *bhp = bh;
  1493. stats->bs_nblocks++;
  1494. }
  1495. /* success */
  1496. return 0;
  1497. /* error */
  1498. err_out_nreq:
  1499. nilfs_bmap_abort_alloc_ptr(btree, nreq, dat);
  1500. err_out_dreq:
  1501. nilfs_bmap_abort_alloc_ptr(btree, dreq, dat);
  1502. stats->bs_nblocks = 0;
  1503. return ret;
  1504. }
  1505. static void
  1506. nilfs_btree_commit_convert_and_insert(struct nilfs_bmap *btree,
  1507. __u64 key, __u64 ptr,
  1508. const __u64 *keys, const __u64 *ptrs,
  1509. int n,
  1510. union nilfs_bmap_ptr_req *dreq,
  1511. union nilfs_bmap_ptr_req *nreq,
  1512. struct buffer_head *bh)
  1513. {
  1514. struct nilfs_btree_node *node;
  1515. struct inode *dat;
  1516. __u64 tmpptr;
  1517. int ncblk;
  1518. /* free resources */
  1519. if (btree->b_ops->bop_clear != NULL)
  1520. btree->b_ops->bop_clear(btree);
  1521. /* ptr must be a pointer to a buffer head. */
  1522. set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr));
  1523. /* convert and insert */
  1524. dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL;
  1525. __nilfs_btree_init(btree);
  1526. if (nreq != NULL) {
  1527. nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
  1528. nilfs_bmap_commit_alloc_ptr(btree, nreq, dat);
  1529. /* create child node at level 1 */
  1530. node = (struct nilfs_btree_node *)bh->b_data;
  1531. ncblk = nilfs_btree_nchildren_per_block(btree);
  1532. nilfs_btree_node_init(node, 0, 1, n, ncblk, keys, ptrs);
  1533. nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr, ncblk);
  1534. if (!buffer_dirty(bh))
  1535. mark_buffer_dirty(bh);
  1536. if (!nilfs_bmap_dirty(btree))
  1537. nilfs_bmap_set_dirty(btree);
  1538. brelse(bh);
  1539. /* create root node at level 2 */
  1540. node = nilfs_btree_get_root(btree);
  1541. tmpptr = nreq->bpr_ptr;
  1542. nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 2, 1,
  1543. NILFS_BTREE_ROOT_NCHILDREN_MAX,
  1544. &keys[0], &tmpptr);
  1545. } else {
  1546. nilfs_bmap_commit_alloc_ptr(btree, dreq, dat);
  1547. /* create root node at level 1 */
  1548. node = nilfs_btree_get_root(btree);
  1549. nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 1, n,
  1550. NILFS_BTREE_ROOT_NCHILDREN_MAX,
  1551. keys, ptrs);
  1552. nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr,
  1553. NILFS_BTREE_ROOT_NCHILDREN_MAX);
  1554. if (!nilfs_bmap_dirty(btree))
  1555. nilfs_bmap_set_dirty(btree);
  1556. }
  1557. if (NILFS_BMAP_USE_VBN(btree))
  1558. nilfs_bmap_set_target_v(btree, key, dreq->bpr_ptr);
  1559. }
  1560. /**
  1561. * nilfs_btree_convert_and_insert -
  1562. * @bmap:
  1563. * @key:
  1564. * @ptr:
  1565. * @keys:
  1566. * @ptrs:
  1567. * @n:
  1568. */
  1569. int nilfs_btree_convert_and_insert(struct nilfs_bmap *btree,
  1570. __u64 key, __u64 ptr,
  1571. const __u64 *keys, const __u64 *ptrs, int n)
  1572. {
  1573. struct buffer_head *bh = NULL;
  1574. union nilfs_bmap_ptr_req dreq, nreq, *di, *ni;
  1575. struct nilfs_bmap_stats stats;
  1576. int ret;
  1577. if (n + 1 <= NILFS_BTREE_ROOT_NCHILDREN_MAX) {
  1578. di = &dreq;
  1579. ni = NULL;
  1580. } else if ((n + 1) <= NILFS_BTREE_NODE_NCHILDREN_MAX(
  1581. 1 << btree->b_inode->i_blkbits)) {
  1582. di = &dreq;
  1583. ni = &nreq;
  1584. } else {
  1585. di = NULL;
  1586. ni = NULL;
  1587. BUG();
  1588. }
  1589. ret = nilfs_btree_prepare_convert_and_insert(btree, key, di, ni, &bh,
  1590. &stats);
  1591. if (ret < 0)
  1592. return ret;
  1593. nilfs_btree_commit_convert_and_insert(btree, key, ptr, keys, ptrs, n,
  1594. di, ni, bh);
  1595. nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks);
  1596. return 0;
  1597. }
  1598. static int nilfs_btree_propagate_p(struct nilfs_bmap *btree,
  1599. struct nilfs_btree_path *path,
  1600. int level,
  1601. struct buffer_head *bh)
  1602. {
  1603. while ((++level < nilfs_btree_height(btree) - 1) &&
  1604. !buffer_dirty(path[level].bp_bh))
  1605. mark_buffer_dirty(path[level].bp_bh);
  1606. return 0;
  1607. }
  1608. static int nilfs_btree_prepare_update_v(struct nilfs_bmap *btree,
  1609. struct nilfs_btree_path *path,
  1610. int level, struct inode *dat)
  1611. {
  1612. struct nilfs_btree_node *parent;
  1613. int ncmax, ret;
  1614. parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
  1615. path[level].bp_oldreq.bpr_ptr =
  1616. nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
  1617. ncmax);
  1618. path[level].bp_newreq.bpr_ptr = path[level].bp_oldreq.bpr_ptr + 1;
  1619. ret = nilfs_dat_prepare_update(dat, &path[level].bp_oldreq.bpr_req,
  1620. &path[level].bp_newreq.bpr_req);
  1621. if (ret < 0)
  1622. return ret;
  1623. if (buffer_nilfs_node(path[level].bp_bh)) {
  1624. path[level].bp_ctxt.oldkey = path[level].bp_oldreq.bpr_ptr;
  1625. path[level].bp_ctxt.newkey = path[level].bp_newreq.bpr_ptr;
  1626. path[level].bp_ctxt.bh = path[level].bp_bh;
  1627. ret = nilfs_btnode_prepare_change_key(
  1628. &NILFS_BMAP_I(btree)->i_btnode_cache,
  1629. &path[level].bp_ctxt);
  1630. if (ret < 0) {
  1631. nilfs_dat_abort_update(dat,
  1632. &path[level].bp_oldreq.bpr_req,
  1633. &path[level].bp_newreq.bpr_req);
  1634. return ret;
  1635. }
  1636. }
  1637. return 0;
  1638. }
  1639. static void nilfs_btree_commit_update_v(struct nilfs_bmap *btree,
  1640. struct nilfs_btree_path *path,
  1641. int level, struct inode *dat)
  1642. {
  1643. struct nilfs_btree_node *parent;
  1644. int ncmax;
  1645. nilfs_dat_commit_update(dat, &path[level].bp_oldreq.bpr_req,
  1646. &path[level].bp_newreq.bpr_req,
  1647. btree->b_ptr_type == NILFS_BMAP_PTR_VS);
  1648. if (buffer_nilfs_node(path[level].bp_bh)) {
  1649. nilfs_btnode_commit_change_key(
  1650. &NILFS_BMAP_I(btree)->i_btnode_cache,
  1651. &path[level].bp_ctxt);
  1652. path[level].bp_bh = path[level].bp_ctxt.bh;
  1653. }
  1654. set_buffer_nilfs_volatile(path[level].bp_bh);
  1655. parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
  1656. nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index,
  1657. path[level].bp_newreq.bpr_ptr, ncmax);
  1658. }
  1659. static void nilfs_btree_abort_update_v(struct nilfs_bmap *btree,
  1660. struct nilfs_btree_path *path,
  1661. int level, struct inode *dat)
  1662. {
  1663. nilfs_dat_abort_update(dat, &path[level].bp_oldreq.bpr_req,
  1664. &path[level].bp_newreq.bpr_req);
  1665. if (buffer_nilfs_node(path[level].bp_bh))
  1666. nilfs_btnode_abort_change_key(
  1667. &NILFS_BMAP_I(btree)->i_btnode_cache,
  1668. &path[level].bp_ctxt);
  1669. }
  1670. static int nilfs_btree_prepare_propagate_v(struct nilfs_bmap *btree,
  1671. struct nilfs_btree_path *path,
  1672. int minlevel, int *maxlevelp,
  1673. struct inode *dat)
  1674. {
  1675. int level, ret;
  1676. level = minlevel;
  1677. if (!buffer_nilfs_volatile(path[level].bp_bh)) {
  1678. ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
  1679. if (ret < 0)
  1680. return ret;
  1681. }
  1682. while ((++level < nilfs_btree_height(btree) - 1) &&
  1683. !buffer_dirty(path[level].bp_bh)) {
  1684. WARN_ON(buffer_nilfs_volatile(path[level].bp_bh));
  1685. ret = nilfs_btree_prepare_update_v(btree, path, level, dat);
  1686. if (ret < 0)
  1687. goto out;
  1688. }
  1689. /* success */
  1690. *maxlevelp = level - 1;
  1691. return 0;
  1692. /* error */
  1693. out:
  1694. while (--level > minlevel)
  1695. nilfs_btree_abort_update_v(btree, path, level, dat);
  1696. if (!buffer_nilfs_volatile(path[level].bp_bh))
  1697. nilfs_btree_abort_update_v(btree, path, level, dat);
  1698. return ret;
  1699. }
  1700. static void nilfs_btree_commit_propagate_v(struct nilfs_bmap *btree,
  1701. struct nilfs_btree_path *path,
  1702. int minlevel, int maxlevel,
  1703. struct buffer_head *bh,
  1704. struct inode *dat)
  1705. {
  1706. int level;
  1707. if (!buffer_nilfs_volatile(path[minlevel].bp_bh))
  1708. nilfs_btree_commit_update_v(btree, path, minlevel, dat);
  1709. for (level = minlevel + 1; level <= maxlevel; level++)
  1710. nilfs_btree_commit_update_v(btree, path, level, dat);
  1711. }
  1712. static int nilfs_btree_propagate_v(struct nilfs_bmap *btree,
  1713. struct nilfs_btree_path *path,
  1714. int level, struct buffer_head *bh)
  1715. {
  1716. int maxlevel = 0, ret;
  1717. struct nilfs_btree_node *parent;
  1718. struct inode *dat = nilfs_bmap_get_dat(btree);
  1719. __u64 ptr;
  1720. int ncmax;
  1721. get_bh(bh);
  1722. path[level].bp_bh = bh;
  1723. ret = nilfs_btree_prepare_propagate_v(btree, path, level, &maxlevel,
  1724. dat);
  1725. if (ret < 0)
  1726. goto out;
  1727. if (buffer_nilfs_volatile(path[level].bp_bh)) {
  1728. parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
  1729. ptr = nilfs_btree_node_get_ptr(parent,
  1730. path[level + 1].bp_index,
  1731. ncmax);
  1732. ret = nilfs_dat_mark_dirty(dat, ptr);
  1733. if (ret < 0)
  1734. goto out;
  1735. }
  1736. nilfs_btree_commit_propagate_v(btree, path, level, maxlevel, bh, dat);
  1737. out:
  1738. brelse(path[level].bp_bh);
  1739. path[level].bp_bh = NULL;
  1740. return ret;
  1741. }
  1742. static int nilfs_btree_propagate(struct nilfs_bmap *btree,
  1743. struct buffer_head *bh)
  1744. {
  1745. struct nilfs_btree_path *path;
  1746. struct nilfs_btree_node *node;
  1747. __u64 key;
  1748. int level, ret;
  1749. WARN_ON(!buffer_dirty(bh));
  1750. path = nilfs_btree_alloc_path();
  1751. if (path == NULL)
  1752. return -ENOMEM;
  1753. if (buffer_nilfs_node(bh)) {
  1754. node = (struct nilfs_btree_node *)bh->b_data;
  1755. key = nilfs_btree_node_get_key(node, 0);
  1756. level = nilfs_btree_node_get_level(node);
  1757. } else {
  1758. key = nilfs_bmap_data_get_key(btree, bh);
  1759. level = NILFS_BTREE_LEVEL_DATA;
  1760. }
  1761. ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0);
  1762. if (ret < 0) {
  1763. if (unlikely(ret == -ENOENT))
  1764. printk(KERN_CRIT "%s: key = %llu, level == %d\n",
  1765. __func__, (unsigned long long)key, level);
  1766. goto out;
  1767. }
  1768. ret = NILFS_BMAP_USE_VBN(btree) ?
  1769. nilfs_btree_propagate_v(btree, path, level, bh) :
  1770. nilfs_btree_propagate_p(btree, path, level, bh);
  1771. out:
  1772. nilfs_btree_free_path(path);
  1773. return ret;
  1774. }
  1775. static int nilfs_btree_propagate_gc(struct nilfs_bmap *btree,
  1776. struct buffer_head *bh)
  1777. {
  1778. return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree), bh->b_blocknr);
  1779. }
  1780. static void nilfs_btree_add_dirty_buffer(struct nilfs_bmap *btree,
  1781. struct list_head *lists,
  1782. struct buffer_head *bh)
  1783. {
  1784. struct list_head *head;
  1785. struct buffer_head *cbh;
  1786. struct nilfs_btree_node *node, *cnode;
  1787. __u64 key, ckey;
  1788. int level;
  1789. get_bh(bh);
  1790. node = (struct nilfs_btree_node *)bh->b_data;
  1791. key = nilfs_btree_node_get_key(node, 0);
  1792. level = nilfs_btree_node_get_level(node);
  1793. if (level < NILFS_BTREE_LEVEL_NODE_MIN ||
  1794. level >= NILFS_BTREE_LEVEL_MAX) {
  1795. dump_stack();
  1796. printk(KERN_WARNING
  1797. "%s: invalid btree level: %d (key=%llu, ino=%lu, "
  1798. "blocknr=%llu)\n",
  1799. __func__, level, (unsigned long long)key,
  1800. NILFS_BMAP_I(btree)->vfs_inode.i_ino,
  1801. (unsigned long long)bh->b_blocknr);
  1802. return;
  1803. }
  1804. list_for_each(head, &lists[level]) {
  1805. cbh = list_entry(head, struct buffer_head, b_assoc_buffers);
  1806. cnode = (struct nilfs_btree_node *)cbh->b_data;
  1807. ckey = nilfs_btree_node_get_key(cnode, 0);
  1808. if (key < ckey)
  1809. break;
  1810. }
  1811. list_add_tail(&bh->b_assoc_buffers, head);
  1812. }
  1813. static void nilfs_btree_lookup_dirty_buffers(struct nilfs_bmap *btree,
  1814. struct list_head *listp)
  1815. {
  1816. struct address_space *btcache = &NILFS_BMAP_I(btree)->i_btnode_cache;
  1817. struct list_head lists[NILFS_BTREE_LEVEL_MAX];
  1818. struct pagevec pvec;
  1819. struct buffer_head *bh, *head;
  1820. pgoff_t index = 0;
  1821. int level, i;
  1822. for (level = NILFS_BTREE_LEVEL_NODE_MIN;
  1823. level < NILFS_BTREE_LEVEL_MAX;
  1824. level++)
  1825. INIT_LIST_HEAD(&lists[level]);
  1826. pagevec_init(&pvec, 0);
  1827. while (pagevec_lookup_tag(&pvec, btcache, &index, PAGECACHE_TAG_DIRTY,
  1828. PAGEVEC_SIZE)) {
  1829. for (i = 0; i < pagevec_count(&pvec); i++) {
  1830. bh = head = page_buffers(pvec.pages[i]);
  1831. do {
  1832. if (buffer_dirty(bh))
  1833. nilfs_btree_add_dirty_buffer(btree,
  1834. lists, bh);
  1835. } while ((bh = bh->b_this_page) != head);
  1836. }
  1837. pagevec_release(&pvec);
  1838. cond_resched();
  1839. }
  1840. for (level = NILFS_BTREE_LEVEL_NODE_MIN;
  1841. level < NILFS_BTREE_LEVEL_MAX;
  1842. level++)
  1843. list_splice_tail(&lists[level], listp);
  1844. }
  1845. static int nilfs_btree_assign_p(struct nilfs_bmap *btree,
  1846. struct nilfs_btree_path *path,
  1847. int level,
  1848. struct buffer_head **bh,
  1849. sector_t blocknr,
  1850. union nilfs_binfo *binfo)
  1851. {
  1852. struct nilfs_btree_node *parent;
  1853. __u64 key;
  1854. __u64 ptr;
  1855. int ncmax, ret;
  1856. parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
  1857. ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
  1858. ncmax);
  1859. if (buffer_nilfs_node(*bh)) {
  1860. path[level].bp_ctxt.oldkey = ptr;
  1861. path[level].bp_ctxt.newkey = blocknr;
  1862. path[level].bp_ctxt.bh = *bh;
  1863. ret = nilfs_btnode_prepare_change_key(
  1864. &NILFS_BMAP_I(btree)->i_btnode_cache,
  1865. &path[level].bp_ctxt);
  1866. if (ret < 0)
  1867. return ret;
  1868. nilfs_btnode_commit_change_key(
  1869. &NILFS_BMAP_I(btree)->i_btnode_cache,
  1870. &path[level].bp_ctxt);
  1871. *bh = path[level].bp_ctxt.bh;
  1872. }
  1873. nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index, blocknr,
  1874. ncmax);
  1875. key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
  1876. /* on-disk format */
  1877. binfo->bi_dat.bi_blkoff = cpu_to_le64(key);
  1878. binfo->bi_dat.bi_level = level;
  1879. return 0;
  1880. }
  1881. static int nilfs_btree_assign_v(struct nilfs_bmap *btree,
  1882. struct nilfs_btree_path *path,
  1883. int level,
  1884. struct buffer_head **bh,
  1885. sector_t blocknr,
  1886. union nilfs_binfo *binfo)
  1887. {
  1888. struct nilfs_btree_node *parent;
  1889. struct inode *dat = nilfs_bmap_get_dat(btree);
  1890. __u64 key;
  1891. __u64 ptr;
  1892. union nilfs_bmap_ptr_req req;
  1893. int ncmax, ret;
  1894. parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax);
  1895. ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index,
  1896. ncmax);
  1897. req.bpr_ptr = ptr;
  1898. ret = nilfs_dat_prepare_start(dat, &req.bpr_req);
  1899. if (ret < 0)
  1900. return ret;
  1901. nilfs_dat_commit_start(dat, &req.bpr_req, blocknr);
  1902. key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index);
  1903. /* on-disk format */
  1904. binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr);
  1905. binfo->bi_v.bi_blkoff = cpu_to_le64(key);
  1906. return 0;
  1907. }
  1908. static int nilfs_btree_assign(struct nilfs_bmap *btree,
  1909. struct buffer_head **bh,
  1910. sector_t blocknr,
  1911. union nilfs_binfo *binfo)
  1912. {
  1913. struct nilfs_btree_path *path;
  1914. struct nilfs_btree_node *node;
  1915. __u64 key;
  1916. int level, ret;
  1917. path = nilfs_btree_alloc_path();
  1918. if (path == NULL)
  1919. return -ENOMEM;
  1920. if (buffer_nilfs_node(*bh)) {
  1921. node = (struct nilfs_btree_node *)(*bh)->b_data;
  1922. key = nilfs_btree_node_get_key(node, 0);
  1923. level = nilfs_btree_node_get_level(node);
  1924. } else {
  1925. key = nilfs_bmap_data_get_key(btree, *bh);
  1926. level = NILFS_BTREE_LEVEL_DATA;
  1927. }
  1928. ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0);
  1929. if (ret < 0) {
  1930. WARN_ON(ret == -ENOENT);
  1931. goto out;
  1932. }
  1933. ret = NILFS_BMAP_USE_VBN(btree) ?
  1934. nilfs_btree_assign_v(btree, path, level, bh, blocknr, binfo) :
  1935. nilfs_btree_assign_p(btree, path, level, bh, blocknr, binfo);
  1936. out:
  1937. nilfs_btree_free_path(path);
  1938. return ret;
  1939. }
  1940. static int nilfs_btree_assign_gc(struct nilfs_bmap *btree,
  1941. struct buffer_head **bh,
  1942. sector_t blocknr,
  1943. union nilfs_binfo *binfo)
  1944. {
  1945. struct nilfs_btree_node *node;
  1946. __u64 key;
  1947. int ret;
  1948. ret = nilfs_dat_move(nilfs_bmap_get_dat(btree), (*bh)->b_blocknr,
  1949. blocknr);
  1950. if (ret < 0)
  1951. return ret;
  1952. if (buffer_nilfs_node(*bh)) {
  1953. node = (struct nilfs_btree_node *)(*bh)->b_data;
  1954. key = nilfs_btree_node_get_key(node, 0);
  1955. } else
  1956. key = nilfs_bmap_data_get_key(btree, *bh);
  1957. /* on-disk format */
  1958. binfo->bi_v.bi_vblocknr = cpu_to_le64((*bh)->b_blocknr);
  1959. binfo->bi_v.bi_blkoff = cpu_to_le64(key);
  1960. return 0;
  1961. }
  1962. static int nilfs_btree_mark(struct nilfs_bmap *btree, __u64 key, int level)
  1963. {
  1964. struct buffer_head *bh;
  1965. struct nilfs_btree_path *path;
  1966. __u64 ptr;
  1967. int ret;
  1968. path = nilfs_btree_alloc_path();
  1969. if (path == NULL)
  1970. return -ENOMEM;
  1971. ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level + 1, 0);
  1972. if (ret < 0) {
  1973. WARN_ON(ret == -ENOENT);
  1974. goto out;
  1975. }
  1976. ret = nilfs_btree_get_block(btree, ptr, &bh);
  1977. if (ret < 0) {
  1978. WARN_ON(ret == -ENOENT);
  1979. goto out;
  1980. }
  1981. if (!buffer_dirty(bh))
  1982. mark_buffer_dirty(bh);
  1983. brelse(bh);
  1984. if (!nilfs_bmap_dirty(btree))
  1985. nilfs_bmap_set_dirty(btree);
  1986. out:
  1987. nilfs_btree_free_path(path);
  1988. return ret;
  1989. }
  1990. static const struct nilfs_bmap_operations nilfs_btree_ops = {
  1991. .bop_lookup = nilfs_btree_lookup,
  1992. .bop_lookup_contig = nilfs_btree_lookup_contig,
  1993. .bop_insert = nilfs_btree_insert,
  1994. .bop_delete = nilfs_btree_delete,
  1995. .bop_clear = NULL,
  1996. .bop_propagate = nilfs_btree_propagate,
  1997. .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
  1998. .bop_assign = nilfs_btree_assign,
  1999. .bop_mark = nilfs_btree_mark,
  2000. .bop_seek_key = nilfs_btree_seek_key,
  2001. .bop_last_key = nilfs_btree_last_key,
  2002. .bop_check_insert = NULL,
  2003. .bop_check_delete = nilfs_btree_check_delete,
  2004. .bop_gather_data = nilfs_btree_gather_data,
  2005. };
  2006. static const struct nilfs_bmap_operations nilfs_btree_ops_gc = {
  2007. .bop_lookup = NULL,
  2008. .bop_lookup_contig = NULL,
  2009. .bop_insert = NULL,
  2010. .bop_delete = NULL,
  2011. .bop_clear = NULL,
  2012. .bop_propagate = nilfs_btree_propagate_gc,
  2013. .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers,
  2014. .bop_assign = nilfs_btree_assign_gc,
  2015. .bop_mark = NULL,
  2016. .bop_seek_key = NULL,
  2017. .bop_last_key = NULL,
  2018. .bop_check_insert = NULL,
  2019. .bop_check_delete = NULL,
  2020. .bop_gather_data = NULL,
  2021. };
  2022. static void __nilfs_btree_init(struct nilfs_bmap *bmap)
  2023. {
  2024. bmap->b_ops = &nilfs_btree_ops;
  2025. bmap->b_nchildren_per_block =
  2026. NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap));
  2027. }
  2028. int nilfs_btree_init(struct nilfs_bmap *bmap)
  2029. {
  2030. int ret = 0;
  2031. __nilfs_btree_init(bmap);
  2032. if (nilfs_btree_root_broken(nilfs_btree_get_root(bmap),
  2033. bmap->b_inode->i_ino))
  2034. ret = -EIO;
  2035. return ret;
  2036. }
  2037. void nilfs_btree_init_gc(struct nilfs_bmap *bmap)
  2038. {
  2039. bmap->b_ops = &nilfs_btree_ops_gc;
  2040. bmap->b_nchildren_per_block =
  2041. NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap));
  2042. }