xfs_dir2_node.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * Copyright (c) 2013 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_da_format.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_inode.h"
  30. #include "xfs_bmap.h"
  31. #include "xfs_dir2.h"
  32. #include "xfs_dir2_priv.h"
  33. #include "xfs_error.h"
  34. #include "xfs_trace.h"
  35. #include "xfs_trans.h"
  36. #include "xfs_buf_item.h"
  37. #include "xfs_cksum.h"
  38. /*
  39. * Function declarations.
  40. */
  41. static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
  42. int index);
  43. static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
  44. xfs_da_state_blk_t *blk1,
  45. xfs_da_state_blk_t *blk2);
  46. static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
  47. int index, xfs_da_state_blk_t *dblk,
  48. int *rval);
  49. static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
  50. xfs_da_state_blk_t *fblk);
  51. /*
  52. * Check internal consistency of a leafn block.
  53. */
  54. #ifdef DEBUG
  55. #define xfs_dir3_leaf_check(dp, bp) \
  56. do { \
  57. if (!xfs_dir3_leafn_check((dp), (bp))) \
  58. ASSERT(0); \
  59. } while (0);
  60. static bool
  61. xfs_dir3_leafn_check(
  62. struct xfs_inode *dp,
  63. struct xfs_buf *bp)
  64. {
  65. struct xfs_dir2_leaf *leaf = bp->b_addr;
  66. struct xfs_dir3_icleaf_hdr leafhdr;
  67. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  68. if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
  69. struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
  70. if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
  71. return false;
  72. } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
  73. return false;
  74. return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
  75. }
  76. #else
  77. #define xfs_dir3_leaf_check(dp, bp)
  78. #endif
  79. static bool
  80. xfs_dir3_free_verify(
  81. struct xfs_buf *bp)
  82. {
  83. struct xfs_mount *mp = bp->b_target->bt_mount;
  84. struct xfs_dir2_free_hdr *hdr = bp->b_addr;
  85. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  86. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  87. if (hdr3->magic != cpu_to_be32(XFS_DIR3_FREE_MAGIC))
  88. return false;
  89. if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
  90. return false;
  91. if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
  92. return false;
  93. } else {
  94. if (hdr->magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC))
  95. return false;
  96. }
  97. /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
  98. return true;
  99. }
  100. static void
  101. xfs_dir3_free_read_verify(
  102. struct xfs_buf *bp)
  103. {
  104. struct xfs_mount *mp = bp->b_target->bt_mount;
  105. if ((xfs_sb_version_hascrc(&mp->m_sb) &&
  106. !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
  107. XFS_DIR3_FREE_CRC_OFF)) ||
  108. !xfs_dir3_free_verify(bp)) {
  109. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  110. xfs_buf_ioerror(bp, EFSCORRUPTED);
  111. }
  112. }
  113. static void
  114. xfs_dir3_free_write_verify(
  115. struct xfs_buf *bp)
  116. {
  117. struct xfs_mount *mp = bp->b_target->bt_mount;
  118. struct xfs_buf_log_item *bip = bp->b_fspriv;
  119. struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
  120. if (!xfs_dir3_free_verify(bp)) {
  121. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  122. xfs_buf_ioerror(bp, EFSCORRUPTED);
  123. return;
  124. }
  125. if (!xfs_sb_version_hascrc(&mp->m_sb))
  126. return;
  127. if (bip)
  128. hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
  129. xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_FREE_CRC_OFF);
  130. }
  131. const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
  132. .verify_read = xfs_dir3_free_read_verify,
  133. .verify_write = xfs_dir3_free_write_verify,
  134. };
  135. static int
  136. __xfs_dir3_free_read(
  137. struct xfs_trans *tp,
  138. struct xfs_inode *dp,
  139. xfs_dablk_t fbno,
  140. xfs_daddr_t mappedbno,
  141. struct xfs_buf **bpp)
  142. {
  143. int err;
  144. err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
  145. XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
  146. /* try read returns without an error or *bpp if it lands in a hole */
  147. if (!err && tp && *bpp)
  148. xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
  149. return err;
  150. }
  151. int
  152. xfs_dir2_free_read(
  153. struct xfs_trans *tp,
  154. struct xfs_inode *dp,
  155. xfs_dablk_t fbno,
  156. struct xfs_buf **bpp)
  157. {
  158. return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
  159. }
  160. static int
  161. xfs_dir2_free_try_read(
  162. struct xfs_trans *tp,
  163. struct xfs_inode *dp,
  164. xfs_dablk_t fbno,
  165. struct xfs_buf **bpp)
  166. {
  167. return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
  168. }
  169. static int
  170. xfs_dir3_free_get_buf(
  171. struct xfs_trans *tp,
  172. struct xfs_inode *dp,
  173. xfs_dir2_db_t fbno,
  174. struct xfs_buf **bpp)
  175. {
  176. struct xfs_mount *mp = dp->i_mount;
  177. struct xfs_buf *bp;
  178. int error;
  179. struct xfs_dir3_icfree_hdr hdr;
  180. error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fbno),
  181. -1, &bp, XFS_DATA_FORK);
  182. if (error)
  183. return error;
  184. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
  185. bp->b_ops = &xfs_dir3_free_buf_ops;
  186. /*
  187. * Initialize the new block to be empty, and remember
  188. * its first slot as our empty slot.
  189. */
  190. memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
  191. memset(&hdr, 0, sizeof(hdr));
  192. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  193. struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
  194. hdr.magic = XFS_DIR3_FREE_MAGIC;
  195. hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
  196. hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
  197. uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_uuid);
  198. } else
  199. hdr.magic = XFS_DIR2_FREE_MAGIC;
  200. dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
  201. *bpp = bp;
  202. return 0;
  203. }
  204. /*
  205. * Log entries from a freespace block.
  206. */
  207. STATIC void
  208. xfs_dir2_free_log_bests(
  209. struct xfs_trans *tp,
  210. struct xfs_inode *dp,
  211. struct xfs_buf *bp,
  212. int first, /* first entry to log */
  213. int last) /* last entry to log */
  214. {
  215. xfs_dir2_free_t *free; /* freespace structure */
  216. __be16 *bests;
  217. free = bp->b_addr;
  218. bests = dp->d_ops->free_bests_p(free);
  219. ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
  220. free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
  221. xfs_trans_log_buf(tp, bp,
  222. (uint)((char *)&bests[first] - (char *)free),
  223. (uint)((char *)&bests[last] - (char *)free +
  224. sizeof(bests[0]) - 1));
  225. }
  226. /*
  227. * Log header from a freespace block.
  228. */
  229. static void
  230. xfs_dir2_free_log_header(
  231. struct xfs_trans *tp,
  232. struct xfs_inode *dp,
  233. struct xfs_buf *bp)
  234. {
  235. #ifdef DEBUG
  236. xfs_dir2_free_t *free; /* freespace structure */
  237. free = bp->b_addr;
  238. ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
  239. free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
  240. #endif
  241. xfs_trans_log_buf(tp, bp, 0, dp->d_ops->free_hdr_size() - 1);
  242. }
  243. /*
  244. * Convert a leaf-format directory to a node-format directory.
  245. * We need to change the magic number of the leaf block, and copy
  246. * the freespace table out of the leaf block into its own block.
  247. */
  248. int /* error */
  249. xfs_dir2_leaf_to_node(
  250. xfs_da_args_t *args, /* operation arguments */
  251. struct xfs_buf *lbp) /* leaf buffer */
  252. {
  253. xfs_inode_t *dp; /* incore directory inode */
  254. int error; /* error return value */
  255. struct xfs_buf *fbp; /* freespace buffer */
  256. xfs_dir2_db_t fdb; /* freespace block number */
  257. xfs_dir2_free_t *free; /* freespace structure */
  258. __be16 *from; /* pointer to freespace entry */
  259. int i; /* leaf freespace index */
  260. xfs_dir2_leaf_t *leaf; /* leaf structure */
  261. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  262. xfs_mount_t *mp; /* filesystem mount point */
  263. int n; /* count of live freespc ents */
  264. xfs_dir2_data_off_t off; /* freespace entry value */
  265. __be16 *to; /* pointer to freespace entry */
  266. xfs_trans_t *tp; /* transaction pointer */
  267. struct xfs_dir3_icfree_hdr freehdr;
  268. trace_xfs_dir2_leaf_to_node(args);
  269. dp = args->dp;
  270. mp = dp->i_mount;
  271. tp = args->trans;
  272. /*
  273. * Add a freespace block to the directory.
  274. */
  275. if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
  276. return error;
  277. }
  278. ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
  279. /*
  280. * Get the buffer for the new freespace block.
  281. */
  282. error = xfs_dir3_free_get_buf(tp, dp, fdb, &fbp);
  283. if (error)
  284. return error;
  285. free = fbp->b_addr;
  286. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  287. leaf = lbp->b_addr;
  288. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  289. ASSERT(be32_to_cpu(ltp->bestcount) <=
  290. (uint)dp->i_d.di_size / mp->m_dirblksize);
  291. /*
  292. * Copy freespace entries from the leaf block to the new block.
  293. * Count active entries.
  294. */
  295. from = xfs_dir2_leaf_bests_p(ltp);
  296. to = dp->d_ops->free_bests_p(free);
  297. for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
  298. if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
  299. n++;
  300. *to = cpu_to_be16(off);
  301. }
  302. /*
  303. * Now initialize the freespace block header.
  304. */
  305. freehdr.nused = n;
  306. freehdr.nvalid = be32_to_cpu(ltp->bestcount);
  307. dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
  308. xfs_dir2_free_log_bests(tp, dp, fbp, 0, freehdr.nvalid - 1);
  309. xfs_dir2_free_log_header(tp, dp, fbp);
  310. /*
  311. * Converting the leaf to a leafnode is just a matter of changing the
  312. * magic number and the ops. Do the change directly to the buffer as
  313. * it's less work (and less code) than decoding the header to host
  314. * format and back again.
  315. */
  316. if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
  317. leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
  318. else
  319. leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
  320. lbp->b_ops = &xfs_dir3_leafn_buf_ops;
  321. xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
  322. xfs_dir3_leaf_log_header(tp, dp, lbp);
  323. xfs_dir3_leaf_check(dp, lbp);
  324. return 0;
  325. }
  326. /*
  327. * Add a leaf entry to a leaf block in a node-form directory.
  328. * The other work necessary is done from the caller.
  329. */
  330. static int /* error */
  331. xfs_dir2_leafn_add(
  332. struct xfs_buf *bp, /* leaf buffer */
  333. xfs_da_args_t *args, /* operation arguments */
  334. int index) /* insertion pt for new entry */
  335. {
  336. int compact; /* compacting stale leaves */
  337. xfs_inode_t *dp; /* incore directory inode */
  338. int highstale; /* next stale entry */
  339. xfs_dir2_leaf_t *leaf; /* leaf structure */
  340. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  341. int lfloghigh; /* high leaf entry logging */
  342. int lfloglow; /* low leaf entry logging */
  343. int lowstale; /* previous stale entry */
  344. xfs_mount_t *mp; /* filesystem mount point */
  345. xfs_trans_t *tp; /* transaction pointer */
  346. struct xfs_dir3_icleaf_hdr leafhdr;
  347. struct xfs_dir2_leaf_entry *ents;
  348. trace_xfs_dir2_leafn_add(args, index);
  349. dp = args->dp;
  350. mp = dp->i_mount;
  351. tp = args->trans;
  352. leaf = bp->b_addr;
  353. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  354. ents = dp->d_ops->leaf_ents_p(leaf);
  355. /*
  356. * Quick check just to make sure we are not going to index
  357. * into other peoples memory
  358. */
  359. if (index < 0)
  360. return XFS_ERROR(EFSCORRUPTED);
  361. /*
  362. * If there are already the maximum number of leaf entries in
  363. * the block, if there are no stale entries it won't fit.
  364. * Caller will do a split. If there are stale entries we'll do
  365. * a compact.
  366. */
  367. if (leafhdr.count == dp->d_ops->leaf_max_ents(mp)) {
  368. if (!leafhdr.stale)
  369. return XFS_ERROR(ENOSPC);
  370. compact = leafhdr.stale > 1;
  371. } else
  372. compact = 0;
  373. ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
  374. ASSERT(index == leafhdr.count ||
  375. be32_to_cpu(ents[index].hashval) >= args->hashval);
  376. if (args->op_flags & XFS_DA_OP_JUSTCHECK)
  377. return 0;
  378. /*
  379. * Compact out all but one stale leaf entry. Leaves behind
  380. * the entry closest to index.
  381. */
  382. if (compact)
  383. xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
  384. &highstale, &lfloglow, &lfloghigh);
  385. else if (leafhdr.stale) {
  386. /*
  387. * Set impossible logging indices for this case.
  388. */
  389. lfloglow = leafhdr.count;
  390. lfloghigh = -1;
  391. }
  392. /*
  393. * Insert the new entry, log everything.
  394. */
  395. lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
  396. highstale, &lfloglow, &lfloghigh);
  397. lep->hashval = cpu_to_be32(args->hashval);
  398. lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
  399. args->blkno, args->index));
  400. dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
  401. xfs_dir3_leaf_log_header(tp, dp, bp);
  402. xfs_dir3_leaf_log_ents(tp, dp, bp, lfloglow, lfloghigh);
  403. xfs_dir3_leaf_check(dp, bp);
  404. return 0;
  405. }
  406. #ifdef DEBUG
  407. static void
  408. xfs_dir2_free_hdr_check(
  409. struct xfs_inode *dp,
  410. struct xfs_buf *bp,
  411. xfs_dir2_db_t db)
  412. {
  413. struct xfs_dir3_icfree_hdr hdr;
  414. dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
  415. ASSERT((hdr.firstdb % dp->d_ops->free_max_bests(dp->i_mount)) == 0);
  416. ASSERT(hdr.firstdb <= db);
  417. ASSERT(db < hdr.firstdb + hdr.nvalid);
  418. }
  419. #else
  420. #define xfs_dir2_free_hdr_check(dp, bp, db)
  421. #endif /* DEBUG */
  422. /*
  423. * Return the last hash value in the leaf.
  424. * Stale entries are ok.
  425. */
  426. xfs_dahash_t /* hash value */
  427. xfs_dir2_leafn_lasthash(
  428. struct xfs_inode *dp,
  429. struct xfs_buf *bp, /* leaf buffer */
  430. int *count) /* count of entries in leaf */
  431. {
  432. struct xfs_dir2_leaf *leaf = bp->b_addr;
  433. struct xfs_dir2_leaf_entry *ents;
  434. struct xfs_dir3_icleaf_hdr leafhdr;
  435. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  436. ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
  437. leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
  438. if (count)
  439. *count = leafhdr.count;
  440. if (!leafhdr.count)
  441. return 0;
  442. ents = dp->d_ops->leaf_ents_p(leaf);
  443. return be32_to_cpu(ents[leafhdr.count - 1].hashval);
  444. }
  445. /*
  446. * Look up a leaf entry for space to add a name in a node-format leaf block.
  447. * The extrablk in state is a freespace block.
  448. */
  449. STATIC int
  450. xfs_dir2_leafn_lookup_for_addname(
  451. struct xfs_buf *bp, /* leaf buffer */
  452. xfs_da_args_t *args, /* operation arguments */
  453. int *indexp, /* out: leaf entry index */
  454. xfs_da_state_t *state) /* state to fill in */
  455. {
  456. struct xfs_buf *curbp = NULL; /* current data/free buffer */
  457. xfs_dir2_db_t curdb = -1; /* current data block number */
  458. xfs_dir2_db_t curfdb = -1; /* current free block number */
  459. xfs_inode_t *dp; /* incore directory inode */
  460. int error; /* error return value */
  461. int fi; /* free entry index */
  462. xfs_dir2_free_t *free = NULL; /* free block structure */
  463. int index; /* leaf entry index */
  464. xfs_dir2_leaf_t *leaf; /* leaf structure */
  465. int length; /* length of new data entry */
  466. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  467. xfs_mount_t *mp; /* filesystem mount point */
  468. xfs_dir2_db_t newdb; /* new data block number */
  469. xfs_dir2_db_t newfdb; /* new free block number */
  470. xfs_trans_t *tp; /* transaction pointer */
  471. struct xfs_dir2_leaf_entry *ents;
  472. struct xfs_dir3_icleaf_hdr leafhdr;
  473. dp = args->dp;
  474. tp = args->trans;
  475. mp = dp->i_mount;
  476. leaf = bp->b_addr;
  477. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  478. ents = dp->d_ops->leaf_ents_p(leaf);
  479. xfs_dir3_leaf_check(dp, bp);
  480. ASSERT(leafhdr.count > 0);
  481. /*
  482. * Look up the hash value in the leaf entries.
  483. */
  484. index = xfs_dir2_leaf_search_hash(args, bp);
  485. /*
  486. * Do we have a buffer coming in?
  487. */
  488. if (state->extravalid) {
  489. /* If so, it's a free block buffer, get the block number. */
  490. curbp = state->extrablk.bp;
  491. curfdb = state->extrablk.blkno;
  492. free = curbp->b_addr;
  493. ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
  494. free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
  495. }
  496. length = dp->d_ops->data_entsize(args->namelen);
  497. /*
  498. * Loop over leaf entries with the right hash value.
  499. */
  500. for (lep = &ents[index];
  501. index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
  502. lep++, index++) {
  503. /*
  504. * Skip stale leaf entries.
  505. */
  506. if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
  507. continue;
  508. /*
  509. * Pull the data block number from the entry.
  510. */
  511. newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
  512. /*
  513. * For addname, we're looking for a place to put the new entry.
  514. * We want to use a data block with an entry of equal
  515. * hash value to ours if there is one with room.
  516. *
  517. * If this block isn't the data block we already have
  518. * in hand, take a look at it.
  519. */
  520. if (newdb != curdb) {
  521. __be16 *bests;
  522. curdb = newdb;
  523. /*
  524. * Convert the data block to the free block
  525. * holding its freespace information.
  526. */
  527. newfdb = dp->d_ops->db_to_fdb(mp, newdb);
  528. /*
  529. * If it's not the one we have in hand, read it in.
  530. */
  531. if (newfdb != curfdb) {
  532. /*
  533. * If we had one before, drop it.
  534. */
  535. if (curbp)
  536. xfs_trans_brelse(tp, curbp);
  537. error = xfs_dir2_free_read(tp, dp,
  538. xfs_dir2_db_to_da(mp, newfdb),
  539. &curbp);
  540. if (error)
  541. return error;
  542. free = curbp->b_addr;
  543. xfs_dir2_free_hdr_check(dp, curbp, curdb);
  544. }
  545. /*
  546. * Get the index for our entry.
  547. */
  548. fi = dp->d_ops->db_to_fdindex(mp, curdb);
  549. /*
  550. * If it has room, return it.
  551. */
  552. bests = dp->d_ops->free_bests_p(free);
  553. if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
  554. XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
  555. XFS_ERRLEVEL_LOW, mp);
  556. if (curfdb != newfdb)
  557. xfs_trans_brelse(tp, curbp);
  558. return XFS_ERROR(EFSCORRUPTED);
  559. }
  560. curfdb = newfdb;
  561. if (be16_to_cpu(bests[fi]) >= length)
  562. goto out;
  563. }
  564. }
  565. /* Didn't find any space */
  566. fi = -1;
  567. out:
  568. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  569. if (curbp) {
  570. /* Giving back a free block. */
  571. state->extravalid = 1;
  572. state->extrablk.bp = curbp;
  573. state->extrablk.index = fi;
  574. state->extrablk.blkno = curfdb;
  575. /*
  576. * Important: this magic number is not in the buffer - it's for
  577. * buffer type information and therefore only the free/data type
  578. * matters here, not whether CRCs are enabled or not.
  579. */
  580. state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
  581. } else {
  582. state->extravalid = 0;
  583. }
  584. /*
  585. * Return the index, that will be the insertion point.
  586. */
  587. *indexp = index;
  588. return XFS_ERROR(ENOENT);
  589. }
  590. /*
  591. * Look up a leaf entry in a node-format leaf block.
  592. * The extrablk in state a data block.
  593. */
  594. STATIC int
  595. xfs_dir2_leafn_lookup_for_entry(
  596. struct xfs_buf *bp, /* leaf buffer */
  597. xfs_da_args_t *args, /* operation arguments */
  598. int *indexp, /* out: leaf entry index */
  599. xfs_da_state_t *state) /* state to fill in */
  600. {
  601. struct xfs_buf *curbp = NULL; /* current data/free buffer */
  602. xfs_dir2_db_t curdb = -1; /* current data block number */
  603. xfs_dir2_data_entry_t *dep; /* data block entry */
  604. xfs_inode_t *dp; /* incore directory inode */
  605. int error; /* error return value */
  606. int index; /* leaf entry index */
  607. xfs_dir2_leaf_t *leaf; /* leaf structure */
  608. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  609. xfs_mount_t *mp; /* filesystem mount point */
  610. xfs_dir2_db_t newdb; /* new data block number */
  611. xfs_trans_t *tp; /* transaction pointer */
  612. enum xfs_dacmp cmp; /* comparison result */
  613. struct xfs_dir2_leaf_entry *ents;
  614. struct xfs_dir3_icleaf_hdr leafhdr;
  615. dp = args->dp;
  616. tp = args->trans;
  617. mp = dp->i_mount;
  618. leaf = bp->b_addr;
  619. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  620. ents = dp->d_ops->leaf_ents_p(leaf);
  621. xfs_dir3_leaf_check(dp, bp);
  622. ASSERT(leafhdr.count > 0);
  623. /*
  624. * Look up the hash value in the leaf entries.
  625. */
  626. index = xfs_dir2_leaf_search_hash(args, bp);
  627. /*
  628. * Do we have a buffer coming in?
  629. */
  630. if (state->extravalid) {
  631. curbp = state->extrablk.bp;
  632. curdb = state->extrablk.blkno;
  633. }
  634. /*
  635. * Loop over leaf entries with the right hash value.
  636. */
  637. for (lep = &ents[index];
  638. index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
  639. lep++, index++) {
  640. /*
  641. * Skip stale leaf entries.
  642. */
  643. if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
  644. continue;
  645. /*
  646. * Pull the data block number from the entry.
  647. */
  648. newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
  649. /*
  650. * Not adding a new entry, so we really want to find
  651. * the name given to us.
  652. *
  653. * If it's a different data block, go get it.
  654. */
  655. if (newdb != curdb) {
  656. /*
  657. * If we had a block before that we aren't saving
  658. * for a CI name, drop it
  659. */
  660. if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
  661. curdb != state->extrablk.blkno))
  662. xfs_trans_brelse(tp, curbp);
  663. /*
  664. * If needing the block that is saved with a CI match,
  665. * use it otherwise read in the new data block.
  666. */
  667. if (args->cmpresult != XFS_CMP_DIFFERENT &&
  668. newdb == state->extrablk.blkno) {
  669. ASSERT(state->extravalid);
  670. curbp = state->extrablk.bp;
  671. } else {
  672. error = xfs_dir3_data_read(tp, dp,
  673. xfs_dir2_db_to_da(mp, newdb),
  674. -1, &curbp);
  675. if (error)
  676. return error;
  677. }
  678. xfs_dir3_data_check(dp, curbp);
  679. curdb = newdb;
  680. }
  681. /*
  682. * Point to the data entry.
  683. */
  684. dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
  685. xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
  686. /*
  687. * Compare the entry and if it's an exact match, return
  688. * EEXIST immediately. If it's the first case-insensitive
  689. * match, store the block & inode number and continue looking.
  690. */
  691. cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
  692. if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
  693. /* If there is a CI match block, drop it */
  694. if (args->cmpresult != XFS_CMP_DIFFERENT &&
  695. curdb != state->extrablk.blkno)
  696. xfs_trans_brelse(tp, state->extrablk.bp);
  697. args->cmpresult = cmp;
  698. args->inumber = be64_to_cpu(dep->inumber);
  699. args->filetype = dp->d_ops->data_get_ftype(dep);
  700. *indexp = index;
  701. state->extravalid = 1;
  702. state->extrablk.bp = curbp;
  703. state->extrablk.blkno = curdb;
  704. state->extrablk.index = (int)((char *)dep -
  705. (char *)curbp->b_addr);
  706. state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
  707. curbp->b_ops = &xfs_dir3_data_buf_ops;
  708. xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
  709. if (cmp == XFS_CMP_EXACT)
  710. return XFS_ERROR(EEXIST);
  711. }
  712. }
  713. ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
  714. if (curbp) {
  715. if (args->cmpresult == XFS_CMP_DIFFERENT) {
  716. /* Giving back last used data block. */
  717. state->extravalid = 1;
  718. state->extrablk.bp = curbp;
  719. state->extrablk.index = -1;
  720. state->extrablk.blkno = curdb;
  721. state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
  722. curbp->b_ops = &xfs_dir3_data_buf_ops;
  723. xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
  724. } else {
  725. /* If the curbp is not the CI match block, drop it */
  726. if (state->extrablk.bp != curbp)
  727. xfs_trans_brelse(tp, curbp);
  728. }
  729. } else {
  730. state->extravalid = 0;
  731. }
  732. *indexp = index;
  733. return XFS_ERROR(ENOENT);
  734. }
  735. /*
  736. * Look up a leaf entry in a node-format leaf block.
  737. * If this is an addname then the extrablk in state is a freespace block,
  738. * otherwise it's a data block.
  739. */
  740. int
  741. xfs_dir2_leafn_lookup_int(
  742. struct xfs_buf *bp, /* leaf buffer */
  743. xfs_da_args_t *args, /* operation arguments */
  744. int *indexp, /* out: leaf entry index */
  745. xfs_da_state_t *state) /* state to fill in */
  746. {
  747. if (args->op_flags & XFS_DA_OP_ADDNAME)
  748. return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
  749. state);
  750. return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
  751. }
  752. /*
  753. * Move count leaf entries from source to destination leaf.
  754. * Log entries and headers. Stale entries are preserved.
  755. */
  756. static void
  757. xfs_dir3_leafn_moveents(
  758. xfs_da_args_t *args, /* operation arguments */
  759. struct xfs_buf *bp_s, /* source */
  760. struct xfs_dir3_icleaf_hdr *shdr,
  761. struct xfs_dir2_leaf_entry *sents,
  762. int start_s,/* source leaf index */
  763. struct xfs_buf *bp_d, /* destination */
  764. struct xfs_dir3_icleaf_hdr *dhdr,
  765. struct xfs_dir2_leaf_entry *dents,
  766. int start_d,/* destination leaf index */
  767. int count) /* count of leaves to copy */
  768. {
  769. struct xfs_trans *tp = args->trans;
  770. int stale; /* count stale leaves copied */
  771. trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
  772. /*
  773. * Silently return if nothing to do.
  774. */
  775. if (count == 0)
  776. return;
  777. /*
  778. * If the destination index is not the end of the current
  779. * destination leaf entries, open up a hole in the destination
  780. * to hold the new entries.
  781. */
  782. if (start_d < dhdr->count) {
  783. memmove(&dents[start_d + count], &dents[start_d],
  784. (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
  785. xfs_dir3_leaf_log_ents(tp, args->dp, bp_d, start_d + count,
  786. count + dhdr->count - 1);
  787. }
  788. /*
  789. * If the source has stale leaves, count the ones in the copy range
  790. * so we can update the header correctly.
  791. */
  792. if (shdr->stale) {
  793. int i; /* temp leaf index */
  794. for (i = start_s, stale = 0; i < start_s + count; i++) {
  795. if (sents[i].address ==
  796. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  797. stale++;
  798. }
  799. } else
  800. stale = 0;
  801. /*
  802. * Copy the leaf entries from source to destination.
  803. */
  804. memcpy(&dents[start_d], &sents[start_s],
  805. count * sizeof(xfs_dir2_leaf_entry_t));
  806. xfs_dir3_leaf_log_ents(tp, args->dp, bp_d,
  807. start_d, start_d + count - 1);
  808. /*
  809. * If there are source entries after the ones we copied,
  810. * delete the ones we copied by sliding the next ones down.
  811. */
  812. if (start_s + count < shdr->count) {
  813. memmove(&sents[start_s], &sents[start_s + count],
  814. count * sizeof(xfs_dir2_leaf_entry_t));
  815. xfs_dir3_leaf_log_ents(tp, args->dp, bp_s,
  816. start_s, start_s + count - 1);
  817. }
  818. /*
  819. * Update the headers and log them.
  820. */
  821. shdr->count -= count;
  822. shdr->stale -= stale;
  823. dhdr->count += count;
  824. dhdr->stale += stale;
  825. }
  826. /*
  827. * Determine the sort order of two leaf blocks.
  828. * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
  829. */
  830. int /* sort order */
  831. xfs_dir2_leafn_order(
  832. struct xfs_inode *dp,
  833. struct xfs_buf *leaf1_bp, /* leaf1 buffer */
  834. struct xfs_buf *leaf2_bp) /* leaf2 buffer */
  835. {
  836. struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
  837. struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
  838. struct xfs_dir2_leaf_entry *ents1;
  839. struct xfs_dir2_leaf_entry *ents2;
  840. struct xfs_dir3_icleaf_hdr hdr1;
  841. struct xfs_dir3_icleaf_hdr hdr2;
  842. dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
  843. dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
  844. ents1 = dp->d_ops->leaf_ents_p(leaf1);
  845. ents2 = dp->d_ops->leaf_ents_p(leaf2);
  846. if (hdr1.count > 0 && hdr2.count > 0 &&
  847. (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
  848. be32_to_cpu(ents2[hdr2.count - 1].hashval) <
  849. be32_to_cpu(ents1[hdr1.count - 1].hashval)))
  850. return 1;
  851. return 0;
  852. }
  853. /*
  854. * Rebalance leaf entries between two leaf blocks.
  855. * This is actually only called when the second block is new,
  856. * though the code deals with the general case.
  857. * A new entry will be inserted in one of the blocks, and that
  858. * entry is taken into account when balancing.
  859. */
  860. static void
  861. xfs_dir2_leafn_rebalance(
  862. xfs_da_state_t *state, /* btree cursor */
  863. xfs_da_state_blk_t *blk1, /* first btree block */
  864. xfs_da_state_blk_t *blk2) /* second btree block */
  865. {
  866. xfs_da_args_t *args; /* operation arguments */
  867. int count; /* count (& direction) leaves */
  868. int isleft; /* new goes in left leaf */
  869. xfs_dir2_leaf_t *leaf1; /* first leaf structure */
  870. xfs_dir2_leaf_t *leaf2; /* second leaf structure */
  871. int mid; /* midpoint leaf index */
  872. #if defined(DEBUG) || defined(XFS_WARN)
  873. int oldstale; /* old count of stale leaves */
  874. #endif
  875. int oldsum; /* old total leaf count */
  876. int swap; /* swapped leaf blocks */
  877. struct xfs_dir2_leaf_entry *ents1;
  878. struct xfs_dir2_leaf_entry *ents2;
  879. struct xfs_dir3_icleaf_hdr hdr1;
  880. struct xfs_dir3_icleaf_hdr hdr2;
  881. struct xfs_inode *dp = state->args->dp;
  882. args = state->args;
  883. /*
  884. * If the block order is wrong, swap the arguments.
  885. */
  886. if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
  887. xfs_da_state_blk_t *tmp; /* temp for block swap */
  888. tmp = blk1;
  889. blk1 = blk2;
  890. blk2 = tmp;
  891. }
  892. leaf1 = blk1->bp->b_addr;
  893. leaf2 = blk2->bp->b_addr;
  894. dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
  895. dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
  896. ents1 = dp->d_ops->leaf_ents_p(leaf1);
  897. ents2 = dp->d_ops->leaf_ents_p(leaf2);
  898. oldsum = hdr1.count + hdr2.count;
  899. #if defined(DEBUG) || defined(XFS_WARN)
  900. oldstale = hdr1.stale + hdr2.stale;
  901. #endif
  902. mid = oldsum >> 1;
  903. /*
  904. * If the old leaf count was odd then the new one will be even,
  905. * so we need to divide the new count evenly.
  906. */
  907. if (oldsum & 1) {
  908. xfs_dahash_t midhash; /* middle entry hash value */
  909. if (mid >= hdr1.count)
  910. midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
  911. else
  912. midhash = be32_to_cpu(ents1[mid].hashval);
  913. isleft = args->hashval <= midhash;
  914. }
  915. /*
  916. * If the old count is even then the new count is odd, so there's
  917. * no preferred side for the new entry.
  918. * Pick the left one.
  919. */
  920. else
  921. isleft = 1;
  922. /*
  923. * Calculate moved entry count. Positive means left-to-right,
  924. * negative means right-to-left. Then move the entries.
  925. */
  926. count = hdr1.count - mid + (isleft == 0);
  927. if (count > 0)
  928. xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
  929. hdr1.count - count, blk2->bp,
  930. &hdr2, ents2, 0, count);
  931. else if (count < 0)
  932. xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
  933. blk1->bp, &hdr1, ents1,
  934. hdr1.count, count);
  935. ASSERT(hdr1.count + hdr2.count == oldsum);
  936. ASSERT(hdr1.stale + hdr2.stale == oldstale);
  937. /* log the changes made when moving the entries */
  938. dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
  939. dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
  940. xfs_dir3_leaf_log_header(args->trans, dp, blk1->bp);
  941. xfs_dir3_leaf_log_header(args->trans, dp, blk2->bp);
  942. xfs_dir3_leaf_check(dp, blk1->bp);
  943. xfs_dir3_leaf_check(dp, blk2->bp);
  944. /*
  945. * Mark whether we're inserting into the old or new leaf.
  946. */
  947. if (hdr1.count < hdr2.count)
  948. state->inleaf = swap;
  949. else if (hdr1.count > hdr2.count)
  950. state->inleaf = !swap;
  951. else
  952. state->inleaf = swap ^ (blk1->index <= hdr1.count);
  953. /*
  954. * Adjust the expected index for insertion.
  955. */
  956. if (!state->inleaf)
  957. blk2->index = blk1->index - hdr1.count;
  958. /*
  959. * Finally sanity check just to make sure we are not returning a
  960. * negative index
  961. */
  962. if (blk2->index < 0) {
  963. state->inleaf = 1;
  964. blk2->index = 0;
  965. xfs_alert(dp->i_mount,
  966. "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
  967. __func__, blk1->index);
  968. }
  969. }
  970. static int
  971. xfs_dir3_data_block_free(
  972. xfs_da_args_t *args,
  973. struct xfs_dir2_data_hdr *hdr,
  974. struct xfs_dir2_free *free,
  975. xfs_dir2_db_t fdb,
  976. int findex,
  977. struct xfs_buf *fbp,
  978. int longest)
  979. {
  980. struct xfs_trans *tp = args->trans;
  981. int logfree = 0;
  982. __be16 *bests;
  983. struct xfs_dir3_icfree_hdr freehdr;
  984. struct xfs_inode *dp = args->dp;
  985. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  986. bests = dp->d_ops->free_bests_p(free);
  987. if (hdr) {
  988. /*
  989. * Data block is not empty, just set the free entry to the new
  990. * value.
  991. */
  992. bests[findex] = cpu_to_be16(longest);
  993. xfs_dir2_free_log_bests(tp, dp, fbp, findex, findex);
  994. return 0;
  995. }
  996. /* One less used entry in the free table. */
  997. freehdr.nused--;
  998. /*
  999. * If this was the last entry in the table, we can trim the table size
  1000. * back. There might be other entries at the end referring to
  1001. * non-existent data blocks, get those too.
  1002. */
  1003. if (findex == freehdr.nvalid - 1) {
  1004. int i; /* free entry index */
  1005. for (i = findex - 1; i >= 0; i--) {
  1006. if (bests[i] != cpu_to_be16(NULLDATAOFF))
  1007. break;
  1008. }
  1009. freehdr.nvalid = i + 1;
  1010. logfree = 0;
  1011. } else {
  1012. /* Not the last entry, just punch it out. */
  1013. bests[findex] = cpu_to_be16(NULLDATAOFF);
  1014. logfree = 1;
  1015. }
  1016. dp->d_ops->free_hdr_to_disk(free, &freehdr);
  1017. xfs_dir2_free_log_header(tp, dp, fbp);
  1018. /*
  1019. * If there are no useful entries left in the block, get rid of the
  1020. * block if we can.
  1021. */
  1022. if (!freehdr.nused) {
  1023. int error;
  1024. error = xfs_dir2_shrink_inode(args, fdb, fbp);
  1025. if (error == 0) {
  1026. fbp = NULL;
  1027. logfree = 0;
  1028. } else if (error != ENOSPC || args->total != 0)
  1029. return error;
  1030. /*
  1031. * It's possible to get ENOSPC if there is no
  1032. * space reservation. In this case some one
  1033. * else will eventually get rid of this block.
  1034. */
  1035. }
  1036. /* Log the free entry that changed, unless we got rid of it. */
  1037. if (logfree)
  1038. xfs_dir2_free_log_bests(tp, dp, fbp, findex, findex);
  1039. return 0;
  1040. }
  1041. /*
  1042. * Remove an entry from a node directory.
  1043. * This removes the leaf entry and the data entry,
  1044. * and updates the free block if necessary.
  1045. */
  1046. static int /* error */
  1047. xfs_dir2_leafn_remove(
  1048. xfs_da_args_t *args, /* operation arguments */
  1049. struct xfs_buf *bp, /* leaf buffer */
  1050. int index, /* leaf entry index */
  1051. xfs_da_state_blk_t *dblk, /* data block */
  1052. int *rval) /* resulting block needs join */
  1053. {
  1054. xfs_dir2_data_hdr_t *hdr; /* data block header */
  1055. xfs_dir2_db_t db; /* data block number */
  1056. struct xfs_buf *dbp; /* data block buffer */
  1057. xfs_dir2_data_entry_t *dep; /* data block entry */
  1058. xfs_inode_t *dp; /* incore directory inode */
  1059. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1060. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1061. int longest; /* longest data free entry */
  1062. int off; /* data block entry offset */
  1063. xfs_mount_t *mp; /* filesystem mount point */
  1064. int needlog; /* need to log data header */
  1065. int needscan; /* need to rescan data frees */
  1066. xfs_trans_t *tp; /* transaction pointer */
  1067. struct xfs_dir2_data_free *bf; /* bestfree table */
  1068. struct xfs_dir3_icleaf_hdr leafhdr;
  1069. struct xfs_dir2_leaf_entry *ents;
  1070. trace_xfs_dir2_leafn_remove(args, index);
  1071. dp = args->dp;
  1072. tp = args->trans;
  1073. mp = dp->i_mount;
  1074. leaf = bp->b_addr;
  1075. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  1076. ents = dp->d_ops->leaf_ents_p(leaf);
  1077. /*
  1078. * Point to the entry we're removing.
  1079. */
  1080. lep = &ents[index];
  1081. /*
  1082. * Extract the data block and offset from the entry.
  1083. */
  1084. db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
  1085. ASSERT(dblk->blkno == db);
  1086. off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
  1087. ASSERT(dblk->index == off);
  1088. /*
  1089. * Kill the leaf entry by marking it stale.
  1090. * Log the leaf block changes.
  1091. */
  1092. leafhdr.stale++;
  1093. dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
  1094. xfs_dir3_leaf_log_header(tp, dp, bp);
  1095. lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
  1096. xfs_dir3_leaf_log_ents(tp, dp, bp, index, index);
  1097. /*
  1098. * Make the data entry free. Keep track of the longest freespace
  1099. * in the data block in case it changes.
  1100. */
  1101. dbp = dblk->bp;
  1102. hdr = dbp->b_addr;
  1103. dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
  1104. bf = dp->d_ops->data_bestfree_p(hdr);
  1105. longest = be16_to_cpu(bf[0].length);
  1106. needlog = needscan = 0;
  1107. xfs_dir2_data_make_free(tp, dp, dbp, off,
  1108. dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
  1109. /*
  1110. * Rescan the data block freespaces for bestfree.
  1111. * Log the data block header if needed.
  1112. */
  1113. if (needscan)
  1114. xfs_dir2_data_freescan(dp, hdr, &needlog);
  1115. if (needlog)
  1116. xfs_dir2_data_log_header(tp, dp, dbp);
  1117. xfs_dir3_data_check(dp, dbp);
  1118. /*
  1119. * If the longest data block freespace changes, need to update
  1120. * the corresponding freeblock entry.
  1121. */
  1122. if (longest < be16_to_cpu(bf[0].length)) {
  1123. int error; /* error return value */
  1124. struct xfs_buf *fbp; /* freeblock buffer */
  1125. xfs_dir2_db_t fdb; /* freeblock block number */
  1126. int findex; /* index in freeblock entries */
  1127. xfs_dir2_free_t *free; /* freeblock structure */
  1128. /*
  1129. * Convert the data block number to a free block,
  1130. * read in the free block.
  1131. */
  1132. fdb = dp->d_ops->db_to_fdb(mp, db);
  1133. error = xfs_dir2_free_read(tp, dp, xfs_dir2_db_to_da(mp, fdb),
  1134. &fbp);
  1135. if (error)
  1136. return error;
  1137. free = fbp->b_addr;
  1138. #ifdef DEBUG
  1139. {
  1140. struct xfs_dir3_icfree_hdr freehdr;
  1141. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1142. ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(mp) *
  1143. (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
  1144. }
  1145. #endif
  1146. /*
  1147. * Calculate which entry we need to fix.
  1148. */
  1149. findex = dp->d_ops->db_to_fdindex(mp, db);
  1150. longest = be16_to_cpu(bf[0].length);
  1151. /*
  1152. * If the data block is now empty we can get rid of it
  1153. * (usually).
  1154. */
  1155. if (longest == mp->m_dirblksize -
  1156. dp->d_ops->data_entry_offset()) {
  1157. /*
  1158. * Try to punch out the data block.
  1159. */
  1160. error = xfs_dir2_shrink_inode(args, db, dbp);
  1161. if (error == 0) {
  1162. dblk->bp = NULL;
  1163. hdr = NULL;
  1164. }
  1165. /*
  1166. * We can get ENOSPC if there's no space reservation.
  1167. * In this case just drop the buffer and some one else
  1168. * will eventually get rid of the empty block.
  1169. */
  1170. else if (!(error == ENOSPC && args->total == 0))
  1171. return error;
  1172. }
  1173. /*
  1174. * If we got rid of the data block, we can eliminate that entry
  1175. * in the free block.
  1176. */
  1177. error = xfs_dir3_data_block_free(args, hdr, free,
  1178. fdb, findex, fbp, longest);
  1179. if (error)
  1180. return error;
  1181. }
  1182. xfs_dir3_leaf_check(dp, bp);
  1183. /*
  1184. * Return indication of whether this leaf block is empty enough
  1185. * to justify trying to join it with a neighbor.
  1186. */
  1187. *rval = (dp->d_ops->leaf_hdr_size() +
  1188. (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
  1189. mp->m_dir_magicpct;
  1190. return 0;
  1191. }
  1192. /*
  1193. * Split the leaf entries in the old block into old and new blocks.
  1194. */
  1195. int /* error */
  1196. xfs_dir2_leafn_split(
  1197. xfs_da_state_t *state, /* btree cursor */
  1198. xfs_da_state_blk_t *oldblk, /* original block */
  1199. xfs_da_state_blk_t *newblk) /* newly created block */
  1200. {
  1201. xfs_da_args_t *args; /* operation arguments */
  1202. xfs_dablk_t blkno; /* new leaf block number */
  1203. int error; /* error return value */
  1204. xfs_mount_t *mp; /* filesystem mount point */
  1205. struct xfs_inode *dp;
  1206. /*
  1207. * Allocate space for a new leaf node.
  1208. */
  1209. args = state->args;
  1210. dp = args->dp;
  1211. mp = dp->i_mount;
  1212. ASSERT(args != NULL);
  1213. ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
  1214. error = xfs_da_grow_inode(args, &blkno);
  1215. if (error) {
  1216. return error;
  1217. }
  1218. /*
  1219. * Initialize the new leaf block.
  1220. */
  1221. error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(mp, blkno),
  1222. &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
  1223. if (error)
  1224. return error;
  1225. newblk->blkno = blkno;
  1226. newblk->magic = XFS_DIR2_LEAFN_MAGIC;
  1227. /*
  1228. * Rebalance the entries across the two leaves, link the new
  1229. * block into the leaves.
  1230. */
  1231. xfs_dir2_leafn_rebalance(state, oldblk, newblk);
  1232. error = xfs_da3_blk_link(state, oldblk, newblk);
  1233. if (error) {
  1234. return error;
  1235. }
  1236. /*
  1237. * Insert the new entry in the correct block.
  1238. */
  1239. if (state->inleaf)
  1240. error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
  1241. else
  1242. error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
  1243. /*
  1244. * Update last hashval in each block since we added the name.
  1245. */
  1246. oldblk->hashval = xfs_dir2_leafn_lasthash(dp, oldblk->bp, NULL);
  1247. newblk->hashval = xfs_dir2_leafn_lasthash(dp, newblk->bp, NULL);
  1248. xfs_dir3_leaf_check(dp, oldblk->bp);
  1249. xfs_dir3_leaf_check(dp, newblk->bp);
  1250. return error;
  1251. }
  1252. /*
  1253. * Check a leaf block and its neighbors to see if the block should be
  1254. * collapsed into one or the other neighbor. Always keep the block
  1255. * with the smaller block number.
  1256. * If the current block is over 50% full, don't try to join it, return 0.
  1257. * If the block is empty, fill in the state structure and return 2.
  1258. * If it can be collapsed, fill in the state structure and return 1.
  1259. * If nothing can be done, return 0.
  1260. */
  1261. int /* error */
  1262. xfs_dir2_leafn_toosmall(
  1263. xfs_da_state_t *state, /* btree cursor */
  1264. int *action) /* resulting action to take */
  1265. {
  1266. xfs_da_state_blk_t *blk; /* leaf block */
  1267. xfs_dablk_t blkno; /* leaf block number */
  1268. struct xfs_buf *bp; /* leaf buffer */
  1269. int bytes; /* bytes in use */
  1270. int count; /* leaf live entry count */
  1271. int error; /* error return value */
  1272. int forward; /* sibling block direction */
  1273. int i; /* sibling counter */
  1274. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1275. int rval; /* result from path_shift */
  1276. struct xfs_dir3_icleaf_hdr leafhdr;
  1277. struct xfs_dir2_leaf_entry *ents;
  1278. struct xfs_inode *dp = state->args->dp;
  1279. /*
  1280. * Check for the degenerate case of the block being over 50% full.
  1281. * If so, it's not worth even looking to see if we might be able
  1282. * to coalesce with a sibling.
  1283. */
  1284. blk = &state->path.blk[state->path.active - 1];
  1285. leaf = blk->bp->b_addr;
  1286. dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
  1287. ents = dp->d_ops->leaf_ents_p(leaf);
  1288. xfs_dir3_leaf_check(dp, blk->bp);
  1289. count = leafhdr.count - leafhdr.stale;
  1290. bytes = dp->d_ops->leaf_hdr_size() + count * sizeof(ents[0]);
  1291. if (bytes > (state->blocksize >> 1)) {
  1292. /*
  1293. * Blk over 50%, don't try to join.
  1294. */
  1295. *action = 0;
  1296. return 0;
  1297. }
  1298. /*
  1299. * Check for the degenerate case of the block being empty.
  1300. * If the block is empty, we'll simply delete it, no need to
  1301. * coalesce it with a sibling block. We choose (arbitrarily)
  1302. * to merge with the forward block unless it is NULL.
  1303. */
  1304. if (count == 0) {
  1305. /*
  1306. * Make altpath point to the block we want to keep and
  1307. * path point to the block we want to drop (this one).
  1308. */
  1309. forward = (leafhdr.forw != 0);
  1310. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1311. error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
  1312. &rval);
  1313. if (error)
  1314. return error;
  1315. *action = rval ? 2 : 0;
  1316. return 0;
  1317. }
  1318. /*
  1319. * Examine each sibling block to see if we can coalesce with
  1320. * at least 25% free space to spare. We need to figure out
  1321. * whether to merge with the forward or the backward block.
  1322. * We prefer coalescing with the lower numbered sibling so as
  1323. * to shrink a directory over time.
  1324. */
  1325. forward = leafhdr.forw < leafhdr.back;
  1326. for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
  1327. struct xfs_dir3_icleaf_hdr hdr2;
  1328. blkno = forward ? leafhdr.forw : leafhdr.back;
  1329. if (blkno == 0)
  1330. continue;
  1331. /*
  1332. * Read the sibling leaf block.
  1333. */
  1334. error = xfs_dir3_leafn_read(state->args->trans, dp,
  1335. blkno, -1, &bp);
  1336. if (error)
  1337. return error;
  1338. /*
  1339. * Count bytes in the two blocks combined.
  1340. */
  1341. count = leafhdr.count - leafhdr.stale;
  1342. bytes = state->blocksize - (state->blocksize >> 2);
  1343. leaf = bp->b_addr;
  1344. dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
  1345. ents = dp->d_ops->leaf_ents_p(leaf);
  1346. count += hdr2.count - hdr2.stale;
  1347. bytes -= count * sizeof(ents[0]);
  1348. /*
  1349. * Fits with at least 25% to spare.
  1350. */
  1351. if (bytes >= 0)
  1352. break;
  1353. xfs_trans_brelse(state->args->trans, bp);
  1354. }
  1355. /*
  1356. * Didn't like either block, give up.
  1357. */
  1358. if (i >= 2) {
  1359. *action = 0;
  1360. return 0;
  1361. }
  1362. /*
  1363. * Make altpath point to the block we want to keep (the lower
  1364. * numbered block) and path point to the block we want to drop.
  1365. */
  1366. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1367. if (blkno < blk->blkno)
  1368. error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
  1369. &rval);
  1370. else
  1371. error = xfs_da3_path_shift(state, &state->path, forward, 0,
  1372. &rval);
  1373. if (error) {
  1374. return error;
  1375. }
  1376. *action = rval ? 0 : 1;
  1377. return 0;
  1378. }
  1379. /*
  1380. * Move all the leaf entries from drop_blk to save_blk.
  1381. * This is done as part of a join operation.
  1382. */
  1383. void
  1384. xfs_dir2_leafn_unbalance(
  1385. xfs_da_state_t *state, /* cursor */
  1386. xfs_da_state_blk_t *drop_blk, /* dead block */
  1387. xfs_da_state_blk_t *save_blk) /* surviving block */
  1388. {
  1389. xfs_da_args_t *args; /* operation arguments */
  1390. xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
  1391. xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
  1392. struct xfs_dir3_icleaf_hdr savehdr;
  1393. struct xfs_dir3_icleaf_hdr drophdr;
  1394. struct xfs_dir2_leaf_entry *sents;
  1395. struct xfs_dir2_leaf_entry *dents;
  1396. struct xfs_inode *dp = state->args->dp;
  1397. args = state->args;
  1398. ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1399. ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1400. drop_leaf = drop_blk->bp->b_addr;
  1401. save_leaf = save_blk->bp->b_addr;
  1402. dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
  1403. dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
  1404. sents = dp->d_ops->leaf_ents_p(save_leaf);
  1405. dents = dp->d_ops->leaf_ents_p(drop_leaf);
  1406. /*
  1407. * If there are any stale leaf entries, take this opportunity
  1408. * to purge them.
  1409. */
  1410. if (drophdr.stale)
  1411. xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
  1412. if (savehdr.stale)
  1413. xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
  1414. /*
  1415. * Move the entries from drop to the appropriate end of save.
  1416. */
  1417. drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
  1418. if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
  1419. xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
  1420. save_blk->bp, &savehdr, sents, 0,
  1421. drophdr.count);
  1422. else
  1423. xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
  1424. save_blk->bp, &savehdr, sents,
  1425. savehdr.count, drophdr.count);
  1426. save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
  1427. /* log the changes made when moving the entries */
  1428. dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
  1429. dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
  1430. xfs_dir3_leaf_log_header(args->trans, dp, save_blk->bp);
  1431. xfs_dir3_leaf_log_header(args->trans, dp, drop_blk->bp);
  1432. xfs_dir3_leaf_check(dp, save_blk->bp);
  1433. xfs_dir3_leaf_check(dp, drop_blk->bp);
  1434. }
  1435. /*
  1436. * Top-level node form directory addname routine.
  1437. */
  1438. int /* error */
  1439. xfs_dir2_node_addname(
  1440. xfs_da_args_t *args) /* operation arguments */
  1441. {
  1442. xfs_da_state_blk_t *blk; /* leaf block for insert */
  1443. int error; /* error return value */
  1444. int rval; /* sub-return value */
  1445. xfs_da_state_t *state; /* btree cursor */
  1446. trace_xfs_dir2_node_addname(args);
  1447. /*
  1448. * Allocate and initialize the state (btree cursor).
  1449. */
  1450. state = xfs_da_state_alloc();
  1451. state->args = args;
  1452. state->mp = args->dp->i_mount;
  1453. state->blocksize = state->mp->m_dirblksize;
  1454. state->node_ents = state->mp->m_dir_node_ents;
  1455. /*
  1456. * Look up the name. We're not supposed to find it, but
  1457. * this gives us the insertion point.
  1458. */
  1459. error = xfs_da3_node_lookup_int(state, &rval);
  1460. if (error)
  1461. rval = error;
  1462. if (rval != ENOENT) {
  1463. goto done;
  1464. }
  1465. /*
  1466. * Add the data entry to a data block.
  1467. * Extravalid is set to a freeblock found by lookup.
  1468. */
  1469. rval = xfs_dir2_node_addname_int(args,
  1470. state->extravalid ? &state->extrablk : NULL);
  1471. if (rval) {
  1472. goto done;
  1473. }
  1474. blk = &state->path.blk[state->path.active - 1];
  1475. ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1476. /*
  1477. * Add the new leaf entry.
  1478. */
  1479. rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
  1480. if (rval == 0) {
  1481. /*
  1482. * It worked, fix the hash values up the btree.
  1483. */
  1484. if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
  1485. xfs_da3_fixhashpath(state, &state->path);
  1486. } else {
  1487. /*
  1488. * It didn't work, we need to split the leaf block.
  1489. */
  1490. if (args->total == 0) {
  1491. ASSERT(rval == ENOSPC);
  1492. goto done;
  1493. }
  1494. /*
  1495. * Split the leaf block and insert the new entry.
  1496. */
  1497. rval = xfs_da3_split(state);
  1498. }
  1499. done:
  1500. xfs_da_state_free(state);
  1501. return rval;
  1502. }
  1503. /*
  1504. * Add the data entry for a node-format directory name addition.
  1505. * The leaf entry is added in xfs_dir2_leafn_add.
  1506. * We may enter with a freespace block that the lookup found.
  1507. */
  1508. static int /* error */
  1509. xfs_dir2_node_addname_int(
  1510. xfs_da_args_t *args, /* operation arguments */
  1511. xfs_da_state_blk_t *fblk) /* optional freespace block */
  1512. {
  1513. xfs_dir2_data_hdr_t *hdr; /* data block header */
  1514. xfs_dir2_db_t dbno; /* data block number */
  1515. struct xfs_buf *dbp; /* data block buffer */
  1516. xfs_dir2_data_entry_t *dep; /* data entry pointer */
  1517. xfs_inode_t *dp; /* incore directory inode */
  1518. xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
  1519. int error; /* error return value */
  1520. xfs_dir2_db_t fbno; /* freespace block number */
  1521. struct xfs_buf *fbp; /* freespace buffer */
  1522. int findex; /* freespace entry index */
  1523. xfs_dir2_free_t *free=NULL; /* freespace block structure */
  1524. xfs_dir2_db_t ifbno; /* initial freespace block no */
  1525. xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
  1526. int length; /* length of the new entry */
  1527. int logfree; /* need to log free entry */
  1528. xfs_mount_t *mp; /* filesystem mount point */
  1529. int needlog; /* need to log data header */
  1530. int needscan; /* need to rescan data frees */
  1531. __be16 *tagp; /* data entry tag pointer */
  1532. xfs_trans_t *tp; /* transaction pointer */
  1533. __be16 *bests;
  1534. struct xfs_dir3_icfree_hdr freehdr;
  1535. struct xfs_dir2_data_free *bf;
  1536. dp = args->dp;
  1537. mp = dp->i_mount;
  1538. tp = args->trans;
  1539. length = dp->d_ops->data_entsize(args->namelen);
  1540. /*
  1541. * If we came in with a freespace block that means that lookup
  1542. * found an entry with our hash value. This is the freespace
  1543. * block for that data entry.
  1544. */
  1545. if (fblk) {
  1546. fbp = fblk->bp;
  1547. /*
  1548. * Remember initial freespace block number.
  1549. */
  1550. ifbno = fblk->blkno;
  1551. free = fbp->b_addr;
  1552. findex = fblk->index;
  1553. bests = dp->d_ops->free_bests_p(free);
  1554. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1555. /*
  1556. * This means the free entry showed that the data block had
  1557. * space for our entry, so we remembered it.
  1558. * Use that data block.
  1559. */
  1560. if (findex >= 0) {
  1561. ASSERT(findex < freehdr.nvalid);
  1562. ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
  1563. ASSERT(be16_to_cpu(bests[findex]) >= length);
  1564. dbno = freehdr.firstdb + findex;
  1565. } else {
  1566. /*
  1567. * The data block looked at didn't have enough room.
  1568. * We'll start at the beginning of the freespace entries.
  1569. */
  1570. dbno = -1;
  1571. findex = 0;
  1572. }
  1573. } else {
  1574. /*
  1575. * Didn't come in with a freespace block, so no data block.
  1576. */
  1577. ifbno = dbno = -1;
  1578. fbp = NULL;
  1579. findex = 0;
  1580. }
  1581. /*
  1582. * If we don't have a data block yet, we're going to scan the
  1583. * freespace blocks looking for one. Figure out what the
  1584. * highest freespace block number is.
  1585. */
  1586. if (dbno == -1) {
  1587. xfs_fileoff_t fo; /* freespace block number */
  1588. if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
  1589. return error;
  1590. lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
  1591. fbno = ifbno;
  1592. }
  1593. /*
  1594. * While we haven't identified a data block, search the freeblock
  1595. * data for a good data block. If we find a null freeblock entry,
  1596. * indicating a hole in the data blocks, remember that.
  1597. */
  1598. while (dbno == -1) {
  1599. /*
  1600. * If we don't have a freeblock in hand, get the next one.
  1601. */
  1602. if (fbp == NULL) {
  1603. /*
  1604. * Happens the first time through unless lookup gave
  1605. * us a freespace block to start with.
  1606. */
  1607. if (++fbno == 0)
  1608. fbno = XFS_DIR2_FREE_FIRSTDB(mp);
  1609. /*
  1610. * If it's ifbno we already looked at it.
  1611. */
  1612. if (fbno == ifbno)
  1613. fbno++;
  1614. /*
  1615. * If it's off the end we're done.
  1616. */
  1617. if (fbno >= lastfbno)
  1618. break;
  1619. /*
  1620. * Read the block. There can be holes in the
  1621. * freespace blocks, so this might not succeed.
  1622. * This should be really rare, so there's no reason
  1623. * to avoid it.
  1624. */
  1625. error = xfs_dir2_free_try_read(tp, dp,
  1626. xfs_dir2_db_to_da(mp, fbno),
  1627. &fbp);
  1628. if (error)
  1629. return error;
  1630. if (!fbp)
  1631. continue;
  1632. free = fbp->b_addr;
  1633. findex = 0;
  1634. }
  1635. /*
  1636. * Look at the current free entry. Is it good enough?
  1637. *
  1638. * The bests initialisation should be where the bufer is read in
  1639. * the above branch. But gcc is too stupid to realise that bests
  1640. * and the freehdr are actually initialised if they are placed
  1641. * there, so we have to do it here to avoid warnings. Blech.
  1642. */
  1643. bests = dp->d_ops->free_bests_p(free);
  1644. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1645. if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
  1646. be16_to_cpu(bests[findex]) >= length)
  1647. dbno = freehdr.firstdb + findex;
  1648. else {
  1649. /*
  1650. * Are we done with the freeblock?
  1651. */
  1652. if (++findex == freehdr.nvalid) {
  1653. /*
  1654. * Drop the block.
  1655. */
  1656. xfs_trans_brelse(tp, fbp);
  1657. fbp = NULL;
  1658. if (fblk && fblk->bp)
  1659. fblk->bp = NULL;
  1660. }
  1661. }
  1662. }
  1663. /*
  1664. * If we don't have a data block, we need to allocate one and make
  1665. * the freespace entries refer to it.
  1666. */
  1667. if (unlikely(dbno == -1)) {
  1668. /*
  1669. * Not allowed to allocate, return failure.
  1670. */
  1671. if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
  1672. return XFS_ERROR(ENOSPC);
  1673. /*
  1674. * Allocate and initialize the new data block.
  1675. */
  1676. if (unlikely((error = xfs_dir2_grow_inode(args,
  1677. XFS_DIR2_DATA_SPACE,
  1678. &dbno)) ||
  1679. (error = xfs_dir3_data_init(args, dbno, &dbp))))
  1680. return error;
  1681. /*
  1682. * If (somehow) we have a freespace block, get rid of it.
  1683. */
  1684. if (fbp)
  1685. xfs_trans_brelse(tp, fbp);
  1686. if (fblk && fblk->bp)
  1687. fblk->bp = NULL;
  1688. /*
  1689. * Get the freespace block corresponding to the data block
  1690. * that was just allocated.
  1691. */
  1692. fbno = dp->d_ops->db_to_fdb(mp, dbno);
  1693. error = xfs_dir2_free_try_read(tp, dp,
  1694. xfs_dir2_db_to_da(mp, fbno),
  1695. &fbp);
  1696. if (error)
  1697. return error;
  1698. /*
  1699. * If there wasn't a freespace block, the read will
  1700. * return a NULL fbp. Allocate and initialize a new one.
  1701. */
  1702. if (!fbp) {
  1703. error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
  1704. &fbno);
  1705. if (error)
  1706. return error;
  1707. if (unlikely(dp->d_ops->db_to_fdb(mp, dbno) != fbno)) {
  1708. xfs_alert(mp,
  1709. "%s: dir ino %llu needed freesp block %lld for\n"
  1710. " data block %lld, got %lld ifbno %llu lastfbno %d",
  1711. __func__, (unsigned long long)dp->i_ino,
  1712. (long long)dp->d_ops->db_to_fdb(mp, dbno),
  1713. (long long)dbno, (long long)fbno,
  1714. (unsigned long long)ifbno, lastfbno);
  1715. if (fblk) {
  1716. xfs_alert(mp,
  1717. " fblk 0x%p blkno %llu index %d magic 0x%x",
  1718. fblk,
  1719. (unsigned long long)fblk->blkno,
  1720. fblk->index,
  1721. fblk->magic);
  1722. } else {
  1723. xfs_alert(mp, " ... fblk is NULL");
  1724. }
  1725. XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
  1726. XFS_ERRLEVEL_LOW, mp);
  1727. return XFS_ERROR(EFSCORRUPTED);
  1728. }
  1729. /*
  1730. * Get a buffer for the new block.
  1731. */
  1732. error = xfs_dir3_free_get_buf(tp, dp, fbno, &fbp);
  1733. if (error)
  1734. return error;
  1735. free = fbp->b_addr;
  1736. bests = dp->d_ops->free_bests_p(free);
  1737. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1738. /*
  1739. * Remember the first slot as our empty slot.
  1740. */
  1741. freehdr.firstdb = (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
  1742. dp->d_ops->free_max_bests(mp);
  1743. } else {
  1744. free = fbp->b_addr;
  1745. bests = dp->d_ops->free_bests_p(free);
  1746. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  1747. }
  1748. /*
  1749. * Set the freespace block index from the data block number.
  1750. */
  1751. findex = dp->d_ops->db_to_fdindex(mp, dbno);
  1752. /*
  1753. * If it's after the end of the current entries in the
  1754. * freespace block, extend that table.
  1755. */
  1756. if (findex >= freehdr.nvalid) {
  1757. ASSERT(findex < dp->d_ops->free_max_bests(mp));
  1758. freehdr.nvalid = findex + 1;
  1759. /*
  1760. * Tag new entry so nused will go up.
  1761. */
  1762. bests[findex] = cpu_to_be16(NULLDATAOFF);
  1763. }
  1764. /*
  1765. * If this entry was for an empty data block
  1766. * (this should always be true) then update the header.
  1767. */
  1768. if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
  1769. freehdr.nused++;
  1770. dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
  1771. xfs_dir2_free_log_header(tp, dp, fbp);
  1772. }
  1773. /*
  1774. * Update the real value in the table.
  1775. * We haven't allocated the data entry yet so this will
  1776. * change again.
  1777. */
  1778. hdr = dbp->b_addr;
  1779. bf = dp->d_ops->data_bestfree_p(hdr);
  1780. bests[findex] = bf[0].length;
  1781. logfree = 1;
  1782. }
  1783. /*
  1784. * We had a data block so we don't have to make a new one.
  1785. */
  1786. else {
  1787. /*
  1788. * If just checking, we succeeded.
  1789. */
  1790. if (args->op_flags & XFS_DA_OP_JUSTCHECK)
  1791. return 0;
  1792. /*
  1793. * Read the data block in.
  1794. */
  1795. error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(mp, dbno),
  1796. -1, &dbp);
  1797. if (error)
  1798. return error;
  1799. hdr = dbp->b_addr;
  1800. bf = dp->d_ops->data_bestfree_p(hdr);
  1801. logfree = 0;
  1802. }
  1803. ASSERT(be16_to_cpu(bf[0].length) >= length);
  1804. /*
  1805. * Point to the existing unused space.
  1806. */
  1807. dup = (xfs_dir2_data_unused_t *)
  1808. ((char *)hdr + be16_to_cpu(bf[0].offset));
  1809. needscan = needlog = 0;
  1810. /*
  1811. * Mark the first part of the unused space, inuse for us.
  1812. */
  1813. xfs_dir2_data_use_free(tp, dp, dbp, dup,
  1814. (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
  1815. &needlog, &needscan);
  1816. /*
  1817. * Fill in the new entry and log it.
  1818. */
  1819. dep = (xfs_dir2_data_entry_t *)dup;
  1820. dep->inumber = cpu_to_be64(args->inumber);
  1821. dep->namelen = args->namelen;
  1822. memcpy(dep->name, args->name, dep->namelen);
  1823. dp->d_ops->data_put_ftype(dep, args->filetype);
  1824. tagp = dp->d_ops->data_entry_tag_p(dep);
  1825. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  1826. xfs_dir2_data_log_entry(tp, dp, dbp, dep);
  1827. /*
  1828. * Rescan the block for bestfree if needed.
  1829. */
  1830. if (needscan)
  1831. xfs_dir2_data_freescan(dp, hdr, &needlog);
  1832. /*
  1833. * Log the data block header if needed.
  1834. */
  1835. if (needlog)
  1836. xfs_dir2_data_log_header(tp, dp, dbp);
  1837. /*
  1838. * If the freespace entry is now wrong, update it.
  1839. */
  1840. bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
  1841. if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
  1842. bests[findex] = bf[0].length;
  1843. logfree = 1;
  1844. }
  1845. /*
  1846. * Log the freespace entry if needed.
  1847. */
  1848. if (logfree)
  1849. xfs_dir2_free_log_bests(tp, dp, fbp, findex, findex);
  1850. /*
  1851. * Return the data block and offset in args, then drop the data block.
  1852. */
  1853. args->blkno = (xfs_dablk_t)dbno;
  1854. args->index = be16_to_cpu(*tagp);
  1855. return 0;
  1856. }
  1857. /*
  1858. * Lookup an entry in a node-format directory.
  1859. * All the real work happens in xfs_da3_node_lookup_int.
  1860. * The only real output is the inode number of the entry.
  1861. */
  1862. int /* error */
  1863. xfs_dir2_node_lookup(
  1864. xfs_da_args_t *args) /* operation arguments */
  1865. {
  1866. int error; /* error return value */
  1867. int i; /* btree level */
  1868. int rval; /* operation return value */
  1869. xfs_da_state_t *state; /* btree cursor */
  1870. trace_xfs_dir2_node_lookup(args);
  1871. /*
  1872. * Allocate and initialize the btree cursor.
  1873. */
  1874. state = xfs_da_state_alloc();
  1875. state->args = args;
  1876. state->mp = args->dp->i_mount;
  1877. state->blocksize = state->mp->m_dirblksize;
  1878. state->node_ents = state->mp->m_dir_node_ents;
  1879. /*
  1880. * Fill in the path to the entry in the cursor.
  1881. */
  1882. error = xfs_da3_node_lookup_int(state, &rval);
  1883. if (error)
  1884. rval = error;
  1885. else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
  1886. /* If a CI match, dup the actual name and return EEXIST */
  1887. xfs_dir2_data_entry_t *dep;
  1888. dep = (xfs_dir2_data_entry_t *)
  1889. ((char *)state->extrablk.bp->b_addr +
  1890. state->extrablk.index);
  1891. rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
  1892. }
  1893. /*
  1894. * Release the btree blocks and leaf block.
  1895. */
  1896. for (i = 0; i < state->path.active; i++) {
  1897. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  1898. state->path.blk[i].bp = NULL;
  1899. }
  1900. /*
  1901. * Release the data block if we have it.
  1902. */
  1903. if (state->extravalid && state->extrablk.bp) {
  1904. xfs_trans_brelse(args->trans, state->extrablk.bp);
  1905. state->extrablk.bp = NULL;
  1906. }
  1907. xfs_da_state_free(state);
  1908. return rval;
  1909. }
  1910. /*
  1911. * Remove an entry from a node-format directory.
  1912. */
  1913. int /* error */
  1914. xfs_dir2_node_removename(
  1915. xfs_da_args_t *args) /* operation arguments */
  1916. {
  1917. xfs_da_state_blk_t *blk; /* leaf block */
  1918. int error; /* error return value */
  1919. int rval; /* operation return value */
  1920. xfs_da_state_t *state; /* btree cursor */
  1921. trace_xfs_dir2_node_removename(args);
  1922. /*
  1923. * Allocate and initialize the btree cursor.
  1924. */
  1925. state = xfs_da_state_alloc();
  1926. state->args = args;
  1927. state->mp = args->dp->i_mount;
  1928. state->blocksize = state->mp->m_dirblksize;
  1929. state->node_ents = state->mp->m_dir_node_ents;
  1930. /*
  1931. * Look up the entry we're deleting, set up the cursor.
  1932. */
  1933. error = xfs_da3_node_lookup_int(state, &rval);
  1934. if (error)
  1935. rval = error;
  1936. /*
  1937. * Didn't find it, upper layer screwed up.
  1938. */
  1939. if (rval != EEXIST) {
  1940. xfs_da_state_free(state);
  1941. return rval;
  1942. }
  1943. blk = &state->path.blk[state->path.active - 1];
  1944. ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
  1945. ASSERT(state->extravalid);
  1946. /*
  1947. * Remove the leaf and data entries.
  1948. * Extrablk refers to the data block.
  1949. */
  1950. error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
  1951. &state->extrablk, &rval);
  1952. if (error)
  1953. return error;
  1954. /*
  1955. * Fix the hash values up the btree.
  1956. */
  1957. xfs_da3_fixhashpath(state, &state->path);
  1958. /*
  1959. * If we need to join leaf blocks, do it.
  1960. */
  1961. if (rval && state->path.active > 1)
  1962. error = xfs_da3_join(state);
  1963. /*
  1964. * If no errors so far, try conversion to leaf format.
  1965. */
  1966. if (!error)
  1967. error = xfs_dir2_node_to_leaf(state);
  1968. xfs_da_state_free(state);
  1969. return error;
  1970. }
  1971. /*
  1972. * Replace an entry's inode number in a node-format directory.
  1973. */
  1974. int /* error */
  1975. xfs_dir2_node_replace(
  1976. xfs_da_args_t *args) /* operation arguments */
  1977. {
  1978. xfs_da_state_blk_t *blk; /* leaf block */
  1979. xfs_dir2_data_hdr_t *hdr; /* data block header */
  1980. xfs_dir2_data_entry_t *dep; /* data entry changed */
  1981. int error; /* error return value */
  1982. int i; /* btree level */
  1983. xfs_ino_t inum; /* new inode number */
  1984. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1985. xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
  1986. int rval; /* internal return value */
  1987. xfs_da_state_t *state; /* btree cursor */
  1988. trace_xfs_dir2_node_replace(args);
  1989. /*
  1990. * Allocate and initialize the btree cursor.
  1991. */
  1992. state = xfs_da_state_alloc();
  1993. state->args = args;
  1994. state->mp = args->dp->i_mount;
  1995. state->blocksize = state->mp->m_dirblksize;
  1996. state->node_ents = state->mp->m_dir_node_ents;
  1997. inum = args->inumber;
  1998. /*
  1999. * Lookup the entry to change in the btree.
  2000. */
  2001. error = xfs_da3_node_lookup_int(state, &rval);
  2002. if (error) {
  2003. rval = error;
  2004. }
  2005. /*
  2006. * It should be found, since the vnodeops layer has looked it up
  2007. * and locked it. But paranoia is good.
  2008. */
  2009. if (rval == EEXIST) {
  2010. struct xfs_dir2_leaf_entry *ents;
  2011. /*
  2012. * Find the leaf entry.
  2013. */
  2014. blk = &state->path.blk[state->path.active - 1];
  2015. ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
  2016. leaf = blk->bp->b_addr;
  2017. ents = args->dp->d_ops->leaf_ents_p(leaf);
  2018. lep = &ents[blk->index];
  2019. ASSERT(state->extravalid);
  2020. /*
  2021. * Point to the data entry.
  2022. */
  2023. hdr = state->extrablk.bp->b_addr;
  2024. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
  2025. hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
  2026. dep = (xfs_dir2_data_entry_t *)
  2027. ((char *)hdr +
  2028. xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
  2029. ASSERT(inum != be64_to_cpu(dep->inumber));
  2030. /*
  2031. * Fill in the new inode number and log the entry.
  2032. */
  2033. dep->inumber = cpu_to_be64(inum);
  2034. args->dp->d_ops->data_put_ftype(dep, args->filetype);
  2035. xfs_dir2_data_log_entry(args->trans, args->dp,
  2036. state->extrablk.bp, dep);
  2037. rval = 0;
  2038. }
  2039. /*
  2040. * Didn't find it, and we're holding a data block. Drop it.
  2041. */
  2042. else if (state->extravalid) {
  2043. xfs_trans_brelse(args->trans, state->extrablk.bp);
  2044. state->extrablk.bp = NULL;
  2045. }
  2046. /*
  2047. * Release all the buffers in the cursor.
  2048. */
  2049. for (i = 0; i < state->path.active; i++) {
  2050. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  2051. state->path.blk[i].bp = NULL;
  2052. }
  2053. xfs_da_state_free(state);
  2054. return rval;
  2055. }
  2056. /*
  2057. * Trim off a trailing empty freespace block.
  2058. * Return (in rvalp) 1 if we did it, 0 if not.
  2059. */
  2060. int /* error */
  2061. xfs_dir2_node_trim_free(
  2062. xfs_da_args_t *args, /* operation arguments */
  2063. xfs_fileoff_t fo, /* free block number */
  2064. int *rvalp) /* out: did something */
  2065. {
  2066. struct xfs_buf *bp; /* freespace buffer */
  2067. xfs_inode_t *dp; /* incore directory inode */
  2068. int error; /* error return code */
  2069. xfs_dir2_free_t *free; /* freespace structure */
  2070. xfs_mount_t *mp; /* filesystem mount point */
  2071. xfs_trans_t *tp; /* transaction pointer */
  2072. struct xfs_dir3_icfree_hdr freehdr;
  2073. dp = args->dp;
  2074. mp = dp->i_mount;
  2075. tp = args->trans;
  2076. /*
  2077. * Read the freespace block.
  2078. */
  2079. error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
  2080. if (error)
  2081. return error;
  2082. /*
  2083. * There can be holes in freespace. If fo is a hole, there's
  2084. * nothing to do.
  2085. */
  2086. if (!bp)
  2087. return 0;
  2088. free = bp->b_addr;
  2089. dp->d_ops->free_hdr_from_disk(&freehdr, free);
  2090. /*
  2091. * If there are used entries, there's nothing to do.
  2092. */
  2093. if (freehdr.nused > 0) {
  2094. xfs_trans_brelse(tp, bp);
  2095. *rvalp = 0;
  2096. return 0;
  2097. }
  2098. /*
  2099. * Blow the block away.
  2100. */
  2101. if ((error =
  2102. xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
  2103. bp))) {
  2104. /*
  2105. * Can't fail with ENOSPC since that only happens with no
  2106. * space reservation, when breaking up an extent into two
  2107. * pieces. This is the last block of an extent.
  2108. */
  2109. ASSERT(error != ENOSPC);
  2110. xfs_trans_brelse(tp, bp);
  2111. return error;
  2112. }
  2113. /*
  2114. * Return that we succeeded.
  2115. */
  2116. *rvalp = 1;
  2117. return 0;
  2118. }