xfs_dir2_node.c 58 KB

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