btree.c 63 KB

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