xfs_dir2_node.c 56 KB

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