xfs_dir2_node.c 57 KB

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