xfs_dir2_node.c 59 KB

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