xfs_dir2_leaf.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  1. /*
  2. * Copyright (c) 2000-2003,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_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_dinode.h"
  31. #include "xfs_inode.h"
  32. #include "xfs_bmap.h"
  33. #include "xfs_dir2_format.h"
  34. #include "xfs_dir2_priv.h"
  35. #include "xfs_error.h"
  36. #include "xfs_trace.h"
  37. /*
  38. * Local function declarations.
  39. */
  40. #ifdef DEBUG
  41. static void xfs_dir2_leaf_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
  42. #else
  43. #define xfs_dir2_leaf_check(dp, bp)
  44. #endif
  45. static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **lbpp,
  46. int *indexp, xfs_dabuf_t **dbpp);
  47. static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_dabuf *bp,
  48. int first, int last);
  49. static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_dabuf *bp);
  50. /*
  51. * Convert a block form directory to a leaf form directory.
  52. */
  53. int /* error */
  54. xfs_dir2_block_to_leaf(
  55. xfs_da_args_t *args, /* operation arguments */
  56. xfs_dabuf_t *dbp) /* input block's buffer */
  57. {
  58. __be16 *bestsp; /* leaf's bestsp entries */
  59. xfs_dablk_t blkno; /* leaf block's bno */
  60. xfs_dir2_data_hdr_t *hdr; /* block header */
  61. xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
  62. xfs_dir2_block_tail_t *btp; /* block's tail */
  63. xfs_inode_t *dp; /* incore directory inode */
  64. int error; /* error return code */
  65. xfs_dabuf_t *lbp; /* leaf block's buffer */
  66. xfs_dir2_db_t ldb; /* leaf block's bno */
  67. xfs_dir2_leaf_t *leaf; /* leaf structure */
  68. xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
  69. xfs_mount_t *mp; /* filesystem mount point */
  70. int needlog; /* need to log block header */
  71. int needscan; /* need to rescan bestfree */
  72. xfs_trans_t *tp; /* transaction pointer */
  73. trace_xfs_dir2_block_to_leaf(args);
  74. dp = args->dp;
  75. mp = dp->i_mount;
  76. tp = args->trans;
  77. /*
  78. * Add the leaf block to the inode.
  79. * This interface will only put blocks in the leaf/node range.
  80. * Since that's empty now, we'll get the root (block 0 in range).
  81. */
  82. if ((error = xfs_da_grow_inode(args, &blkno))) {
  83. return error;
  84. }
  85. ldb = xfs_dir2_da_to_db(mp, blkno);
  86. ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
  87. /*
  88. * Initialize the leaf block, get a buffer for it.
  89. */
  90. if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) {
  91. return error;
  92. }
  93. ASSERT(lbp != NULL);
  94. leaf = lbp->data;
  95. hdr = dbp->data;
  96. xfs_dir2_data_check(dp, dbp);
  97. btp = xfs_dir2_block_tail_p(mp, hdr);
  98. blp = xfs_dir2_block_leaf_p(btp);
  99. /*
  100. * Set the counts in the leaf header.
  101. */
  102. leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count));
  103. leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale));
  104. /*
  105. * Could compact these but I think we always do the conversion
  106. * after squeezing out stale entries.
  107. */
  108. memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
  109. xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1);
  110. needscan = 0;
  111. needlog = 1;
  112. /*
  113. * Make the space formerly occupied by the leaf entries and block
  114. * tail be free.
  115. */
  116. xfs_dir2_data_make_free(tp, dbp,
  117. (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
  118. (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
  119. (char *)blp),
  120. &needlog, &needscan);
  121. /*
  122. * Fix up the block header, make it a data block.
  123. */
  124. hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
  125. if (needscan)
  126. xfs_dir2_data_freescan(mp, hdr, &needlog);
  127. /*
  128. * Set up leaf tail and bests table.
  129. */
  130. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  131. ltp->bestcount = cpu_to_be32(1);
  132. bestsp = xfs_dir2_leaf_bests_p(ltp);
  133. bestsp[0] = hdr->bestfree[0].length;
  134. /*
  135. * Log the data header and leaf bests table.
  136. */
  137. if (needlog)
  138. xfs_dir2_data_log_header(tp, dbp);
  139. xfs_dir2_leaf_check(dp, lbp);
  140. xfs_dir2_data_check(dp, dbp);
  141. xfs_dir2_leaf_log_bests(tp, lbp, 0, 0);
  142. xfs_da_buf_done(lbp);
  143. return 0;
  144. }
  145. STATIC void
  146. xfs_dir2_leaf_find_stale(
  147. struct xfs_dir2_leaf *leaf,
  148. int index,
  149. int *lowstale,
  150. int *highstale)
  151. {
  152. /*
  153. * Find the first stale entry before our index, if any.
  154. */
  155. for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
  156. if (leaf->ents[*lowstale].address ==
  157. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  158. break;
  159. }
  160. /*
  161. * Find the first stale entry at or after our index, if any.
  162. * Stop if the result would require moving more entries than using
  163. * lowstale.
  164. */
  165. for (*highstale = index;
  166. *highstale < be16_to_cpu(leaf->hdr.count);
  167. ++*highstale) {
  168. if (leaf->ents[*highstale].address ==
  169. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  170. break;
  171. if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
  172. break;
  173. }
  174. }
  175. struct xfs_dir2_leaf_entry *
  176. xfs_dir2_leaf_find_entry(
  177. xfs_dir2_leaf_t *leaf, /* leaf structure */
  178. int index, /* leaf table position */
  179. int compact, /* need to compact leaves */
  180. int lowstale, /* index of prev stale leaf */
  181. int highstale, /* index of next stale leaf */
  182. int *lfloglow, /* low leaf logging index */
  183. int *lfloghigh) /* high leaf logging index */
  184. {
  185. if (!leaf->hdr.stale) {
  186. xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
  187. /*
  188. * Now we need to make room to insert the leaf entry.
  189. *
  190. * If there are no stale entries, just insert a hole at index.
  191. */
  192. lep = &leaf->ents[index];
  193. if (index < be16_to_cpu(leaf->hdr.count))
  194. memmove(lep + 1, lep,
  195. (be16_to_cpu(leaf->hdr.count) - index) *
  196. sizeof(*lep));
  197. /*
  198. * Record low and high logging indices for the leaf.
  199. */
  200. *lfloglow = index;
  201. *lfloghigh = be16_to_cpu(leaf->hdr.count);
  202. be16_add_cpu(&leaf->hdr.count, 1);
  203. return lep;
  204. }
  205. /*
  206. * There are stale entries.
  207. *
  208. * We will use one of them for the new entry. It's probably not at
  209. * the right location, so we'll have to shift some up or down first.
  210. *
  211. * If we didn't compact before, we need to find the nearest stale
  212. * entries before and after our insertion point.
  213. */
  214. if (compact == 0)
  215. xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
  216. /*
  217. * If the low one is better, use it.
  218. */
  219. if (lowstale >= 0 &&
  220. (highstale == be16_to_cpu(leaf->hdr.count) ||
  221. index - lowstale - 1 < highstale - index)) {
  222. ASSERT(index - lowstale - 1 >= 0);
  223. ASSERT(leaf->ents[lowstale].address ==
  224. cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
  225. /*
  226. * Copy entries up to cover the stale entry and make room
  227. * for the new entry.
  228. */
  229. if (index - lowstale - 1 > 0) {
  230. memmove(&leaf->ents[lowstale],
  231. &leaf->ents[lowstale + 1],
  232. (index - lowstale - 1) *
  233. sizeof(xfs_dir2_leaf_entry_t));
  234. }
  235. *lfloglow = MIN(lowstale, *lfloglow);
  236. *lfloghigh = MAX(index - 1, *lfloghigh);
  237. be16_add_cpu(&leaf->hdr.stale, -1);
  238. return &leaf->ents[index - 1];
  239. }
  240. /*
  241. * The high one is better, so use that one.
  242. */
  243. ASSERT(highstale - index >= 0);
  244. ASSERT(leaf->ents[highstale].address ==
  245. cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
  246. /*
  247. * Copy entries down to cover the stale entry and make room for the
  248. * new entry.
  249. */
  250. if (highstale - index > 0) {
  251. memmove(&leaf->ents[index + 1],
  252. &leaf->ents[index],
  253. (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
  254. }
  255. *lfloglow = MIN(index, *lfloglow);
  256. *lfloghigh = MAX(highstale, *lfloghigh);
  257. be16_add_cpu(&leaf->hdr.stale, -1);
  258. return &leaf->ents[index];
  259. }
  260. /*
  261. * Add an entry to a leaf form directory.
  262. */
  263. int /* error */
  264. xfs_dir2_leaf_addname(
  265. xfs_da_args_t *args) /* operation arguments */
  266. {
  267. __be16 *bestsp; /* freespace table in leaf */
  268. int compact; /* need to compact leaves */
  269. xfs_dir2_data_hdr_t *hdr; /* data block header */
  270. xfs_dabuf_t *dbp; /* data block buffer */
  271. xfs_dir2_data_entry_t *dep; /* data block entry */
  272. xfs_inode_t *dp; /* incore directory inode */
  273. xfs_dir2_data_unused_t *dup; /* data unused entry */
  274. int error; /* error return value */
  275. int grown; /* allocated new data block */
  276. int highstale; /* index of next stale leaf */
  277. int i; /* temporary, index */
  278. int index; /* leaf table position */
  279. xfs_dabuf_t *lbp; /* leaf's buffer */
  280. xfs_dir2_leaf_t *leaf; /* leaf structure */
  281. int length; /* length of new entry */
  282. xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
  283. int lfloglow; /* low leaf logging index */
  284. int lfloghigh; /* high leaf logging index */
  285. int lowstale; /* index of prev stale leaf */
  286. xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
  287. xfs_mount_t *mp; /* filesystem mount point */
  288. int needbytes; /* leaf block bytes needed */
  289. int needlog; /* need to log data header */
  290. int needscan; /* need to rescan data free */
  291. __be16 *tagp; /* end of data entry */
  292. xfs_trans_t *tp; /* transaction pointer */
  293. xfs_dir2_db_t use_block; /* data block number */
  294. trace_xfs_dir2_leaf_addname(args);
  295. dp = args->dp;
  296. tp = args->trans;
  297. mp = dp->i_mount;
  298. /*
  299. * Read the leaf block.
  300. */
  301. error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
  302. XFS_DATA_FORK);
  303. if (error) {
  304. return error;
  305. }
  306. ASSERT(lbp != NULL);
  307. /*
  308. * Look up the entry by hash value and name.
  309. * We know it's not there, our caller has already done a lookup.
  310. * So the index is of the entry to insert in front of.
  311. * But if there are dup hash values the index is of the first of those.
  312. */
  313. index = xfs_dir2_leaf_search_hash(args, lbp);
  314. leaf = lbp->data;
  315. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  316. bestsp = xfs_dir2_leaf_bests_p(ltp);
  317. length = xfs_dir2_data_entsize(args->namelen);
  318. /*
  319. * See if there are any entries with the same hash value
  320. * and space in their block for the new entry.
  321. * This is good because it puts multiple same-hash value entries
  322. * in a data block, improving the lookup of those entries.
  323. */
  324. for (use_block = -1, lep = &leaf->ents[index];
  325. index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
  326. index++, lep++) {
  327. if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
  328. continue;
  329. i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
  330. ASSERT(i < be32_to_cpu(ltp->bestcount));
  331. ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
  332. if (be16_to_cpu(bestsp[i]) >= length) {
  333. use_block = i;
  334. break;
  335. }
  336. }
  337. /*
  338. * Didn't find a block yet, linear search all the data blocks.
  339. */
  340. if (use_block == -1) {
  341. for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
  342. /*
  343. * Remember a block we see that's missing.
  344. */
  345. if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
  346. use_block == -1)
  347. use_block = i;
  348. else if (be16_to_cpu(bestsp[i]) >= length) {
  349. use_block = i;
  350. break;
  351. }
  352. }
  353. }
  354. /*
  355. * How many bytes do we need in the leaf block?
  356. */
  357. needbytes = 0;
  358. if (!leaf->hdr.stale)
  359. needbytes += sizeof(xfs_dir2_leaf_entry_t);
  360. if (use_block == -1)
  361. needbytes += sizeof(xfs_dir2_data_off_t);
  362. /*
  363. * Now kill use_block if it refers to a missing block, so we
  364. * can use it as an indication of allocation needed.
  365. */
  366. if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
  367. use_block = -1;
  368. /*
  369. * If we don't have enough free bytes but we can make enough
  370. * by compacting out stale entries, we'll do that.
  371. */
  372. if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <
  373. needbytes && be16_to_cpu(leaf->hdr.stale) > 1) {
  374. compact = 1;
  375. }
  376. /*
  377. * Otherwise if we don't have enough free bytes we need to
  378. * convert to node form.
  379. */
  380. else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(
  381. leaf->hdr.count)] < needbytes) {
  382. /*
  383. * Just checking or no space reservation, give up.
  384. */
  385. if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
  386. args->total == 0) {
  387. xfs_da_brelse(tp, lbp);
  388. return XFS_ERROR(ENOSPC);
  389. }
  390. /*
  391. * Convert to node form.
  392. */
  393. error = xfs_dir2_leaf_to_node(args, lbp);
  394. xfs_da_buf_done(lbp);
  395. if (error)
  396. return error;
  397. /*
  398. * Then add the new entry.
  399. */
  400. return xfs_dir2_node_addname(args);
  401. }
  402. /*
  403. * Otherwise it will fit without compaction.
  404. */
  405. else
  406. compact = 0;
  407. /*
  408. * If just checking, then it will fit unless we needed to allocate
  409. * a new data block.
  410. */
  411. if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
  412. xfs_da_brelse(tp, lbp);
  413. return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
  414. }
  415. /*
  416. * If no allocations are allowed, return now before we've
  417. * changed anything.
  418. */
  419. if (args->total == 0 && use_block == -1) {
  420. xfs_da_brelse(tp, lbp);
  421. return XFS_ERROR(ENOSPC);
  422. }
  423. /*
  424. * Need to compact the leaf entries, removing stale ones.
  425. * Leave one stale entry behind - the one closest to our
  426. * insertion index - and we'll shift that one to our insertion
  427. * point later.
  428. */
  429. if (compact) {
  430. xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale,
  431. &lfloglow, &lfloghigh);
  432. }
  433. /*
  434. * There are stale entries, so we'll need log-low and log-high
  435. * impossibly bad values later.
  436. */
  437. else if (be16_to_cpu(leaf->hdr.stale)) {
  438. lfloglow = be16_to_cpu(leaf->hdr.count);
  439. lfloghigh = -1;
  440. }
  441. /*
  442. * If there was no data block space found, we need to allocate
  443. * a new one.
  444. */
  445. if (use_block == -1) {
  446. /*
  447. * Add the new data block.
  448. */
  449. if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
  450. &use_block))) {
  451. xfs_da_brelse(tp, lbp);
  452. return error;
  453. }
  454. /*
  455. * Initialize the block.
  456. */
  457. if ((error = xfs_dir2_data_init(args, use_block, &dbp))) {
  458. xfs_da_brelse(tp, lbp);
  459. return error;
  460. }
  461. /*
  462. * If we're adding a new data block on the end we need to
  463. * extend the bests table. Copy it up one entry.
  464. */
  465. if (use_block >= be32_to_cpu(ltp->bestcount)) {
  466. bestsp--;
  467. memmove(&bestsp[0], &bestsp[1],
  468. be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
  469. be32_add_cpu(&ltp->bestcount, 1);
  470. xfs_dir2_leaf_log_tail(tp, lbp);
  471. xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
  472. }
  473. /*
  474. * If we're filling in a previously empty block just log it.
  475. */
  476. else
  477. xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
  478. hdr = dbp->data;
  479. bestsp[use_block] = hdr->bestfree[0].length;
  480. grown = 1;
  481. }
  482. /*
  483. * Already had space in some data block.
  484. * Just read that one in.
  485. */
  486. else {
  487. if ((error =
  488. xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, use_block),
  489. -1, &dbp, XFS_DATA_FORK))) {
  490. xfs_da_brelse(tp, lbp);
  491. return error;
  492. }
  493. hdr = dbp->data;
  494. grown = 0;
  495. }
  496. xfs_dir2_data_check(dp, dbp);
  497. /*
  498. * Point to the biggest freespace in our data block.
  499. */
  500. dup = (xfs_dir2_data_unused_t *)
  501. ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
  502. ASSERT(be16_to_cpu(dup->length) >= length);
  503. needscan = needlog = 0;
  504. /*
  505. * Mark the initial part of our freespace in use for the new entry.
  506. */
  507. xfs_dir2_data_use_free(tp, dbp, dup,
  508. (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
  509. &needlog, &needscan);
  510. /*
  511. * Initialize our new entry (at last).
  512. */
  513. dep = (xfs_dir2_data_entry_t *)dup;
  514. dep->inumber = cpu_to_be64(args->inumber);
  515. dep->namelen = args->namelen;
  516. memcpy(dep->name, args->name, dep->namelen);
  517. tagp = xfs_dir2_data_entry_tag_p(dep);
  518. *tagp = cpu_to_be16((char *)dep - (char *)hdr);
  519. /*
  520. * Need to scan fix up the bestfree table.
  521. */
  522. if (needscan)
  523. xfs_dir2_data_freescan(mp, hdr, &needlog);
  524. /*
  525. * Need to log the data block's header.
  526. */
  527. if (needlog)
  528. xfs_dir2_data_log_header(tp, dbp);
  529. xfs_dir2_data_log_entry(tp, dbp, dep);
  530. /*
  531. * If the bests table needs to be changed, do it.
  532. * Log the change unless we've already done that.
  533. */
  534. if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(hdr->bestfree[0].length)) {
  535. bestsp[use_block] = hdr->bestfree[0].length;
  536. if (!grown)
  537. xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block);
  538. }
  539. lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
  540. highstale, &lfloglow, &lfloghigh);
  541. /*
  542. * Fill in the new leaf entry.
  543. */
  544. lep->hashval = cpu_to_be32(args->hashval);
  545. lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
  546. be16_to_cpu(*tagp)));
  547. /*
  548. * Log the leaf fields and give up the buffers.
  549. */
  550. xfs_dir2_leaf_log_header(tp, lbp);
  551. xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
  552. xfs_dir2_leaf_check(dp, lbp);
  553. xfs_da_buf_done(lbp);
  554. xfs_dir2_data_check(dp, dbp);
  555. xfs_da_buf_done(dbp);
  556. return 0;
  557. }
  558. #ifdef DEBUG
  559. /*
  560. * Check the internal consistency of a leaf1 block.
  561. * Pop an assert if something is wrong.
  562. */
  563. STATIC void
  564. xfs_dir2_leaf_check(
  565. xfs_inode_t *dp, /* incore directory inode */
  566. xfs_dabuf_t *bp) /* leaf's buffer */
  567. {
  568. int i; /* leaf index */
  569. xfs_dir2_leaf_t *leaf; /* leaf structure */
  570. xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
  571. xfs_mount_t *mp; /* filesystem mount point */
  572. int stale; /* count of stale leaves */
  573. leaf = bp->data;
  574. mp = dp->i_mount;
  575. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
  576. /*
  577. * This value is not restrictive enough.
  578. * Should factor in the size of the bests table as well.
  579. * We can deduce a value for that from di_size.
  580. */
  581. ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
  582. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  583. /*
  584. * Leaves and bests don't overlap.
  585. */
  586. ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <=
  587. (char *)xfs_dir2_leaf_bests_p(ltp));
  588. /*
  589. * Check hash value order, count stale entries.
  590. */
  591. for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
  592. if (i + 1 < be16_to_cpu(leaf->hdr.count))
  593. ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
  594. be32_to_cpu(leaf->ents[i + 1].hashval));
  595. if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  596. stale++;
  597. }
  598. ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
  599. }
  600. #endif /* DEBUG */
  601. /*
  602. * Compact out any stale entries in the leaf.
  603. * Log the header and changed leaf entries, if any.
  604. */
  605. void
  606. xfs_dir2_leaf_compact(
  607. xfs_da_args_t *args, /* operation arguments */
  608. xfs_dabuf_t *bp) /* leaf buffer */
  609. {
  610. int from; /* source leaf index */
  611. xfs_dir2_leaf_t *leaf; /* leaf structure */
  612. int loglow; /* first leaf entry to log */
  613. int to; /* target leaf index */
  614. leaf = bp->data;
  615. if (!leaf->hdr.stale) {
  616. return;
  617. }
  618. /*
  619. * Compress out the stale entries in place.
  620. */
  621. for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) {
  622. if (leaf->ents[from].address ==
  623. cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
  624. continue;
  625. /*
  626. * Only actually copy the entries that are different.
  627. */
  628. if (from > to) {
  629. if (loglow == -1)
  630. loglow = to;
  631. leaf->ents[to] = leaf->ents[from];
  632. }
  633. to++;
  634. }
  635. /*
  636. * Update and log the header, log the leaf entries.
  637. */
  638. ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to);
  639. be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale)));
  640. leaf->hdr.stale = 0;
  641. xfs_dir2_leaf_log_header(args->trans, bp);
  642. if (loglow != -1)
  643. xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1);
  644. }
  645. /*
  646. * Compact the leaf entries, removing stale ones.
  647. * Leave one stale entry behind - the one closest to our
  648. * insertion index - and the caller will shift that one to our insertion
  649. * point later.
  650. * Return new insertion index, where the remaining stale entry is,
  651. * and leaf logging indices.
  652. */
  653. void
  654. xfs_dir2_leaf_compact_x1(
  655. xfs_dabuf_t *bp, /* leaf buffer */
  656. int *indexp, /* insertion index */
  657. int *lowstalep, /* out: stale entry before us */
  658. int *highstalep, /* out: stale entry after us */
  659. int *lowlogp, /* out: low log index */
  660. int *highlogp) /* out: high log index */
  661. {
  662. int from; /* source copy index */
  663. int highstale; /* stale entry at/after index */
  664. int index; /* insertion index */
  665. int keepstale; /* source index of kept stale */
  666. xfs_dir2_leaf_t *leaf; /* leaf structure */
  667. int lowstale; /* stale entry before index */
  668. int newindex=0; /* new insertion index */
  669. int to; /* destination copy index */
  670. leaf = bp->data;
  671. ASSERT(be16_to_cpu(leaf->hdr.stale) > 1);
  672. index = *indexp;
  673. xfs_dir2_leaf_find_stale(leaf, index, &lowstale, &highstale);
  674. /*
  675. * Pick the better of lowstale and highstale.
  676. */
  677. if (lowstale >= 0 &&
  678. (highstale == be16_to_cpu(leaf->hdr.count) ||
  679. index - lowstale <= highstale - index))
  680. keepstale = lowstale;
  681. else
  682. keepstale = highstale;
  683. /*
  684. * Copy the entries in place, removing all the stale entries
  685. * except keepstale.
  686. */
  687. for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) {
  688. /*
  689. * Notice the new value of index.
  690. */
  691. if (index == from)
  692. newindex = to;
  693. if (from != keepstale &&
  694. leaf->ents[from].address ==
  695. cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
  696. if (from == to)
  697. *lowlogp = to;
  698. continue;
  699. }
  700. /*
  701. * Record the new keepstale value for the insertion.
  702. */
  703. if (from == keepstale)
  704. lowstale = highstale = to;
  705. /*
  706. * Copy only the entries that have moved.
  707. */
  708. if (from > to)
  709. leaf->ents[to] = leaf->ents[from];
  710. to++;
  711. }
  712. ASSERT(from > to);
  713. /*
  714. * If the insertion point was past the last entry,
  715. * set the new insertion point accordingly.
  716. */
  717. if (index == from)
  718. newindex = to;
  719. *indexp = newindex;
  720. /*
  721. * Adjust the leaf header values.
  722. */
  723. be16_add_cpu(&leaf->hdr.count, -(from - to));
  724. leaf->hdr.stale = cpu_to_be16(1);
  725. /*
  726. * Remember the low/high stale value only in the "right"
  727. * direction.
  728. */
  729. if (lowstale >= newindex)
  730. lowstale = -1;
  731. else
  732. highstale = be16_to_cpu(leaf->hdr.count);
  733. *highlogp = be16_to_cpu(leaf->hdr.count) - 1;
  734. *lowstalep = lowstale;
  735. *highstalep = highstale;
  736. }
  737. /*
  738. * Getdents (readdir) for leaf and node directories.
  739. * This reads the data blocks only, so is the same for both forms.
  740. */
  741. int /* error */
  742. xfs_dir2_leaf_getdents(
  743. xfs_inode_t *dp, /* incore directory inode */
  744. void *dirent,
  745. size_t bufsize,
  746. xfs_off_t *offset,
  747. filldir_t filldir)
  748. {
  749. xfs_dabuf_t *bp; /* data block buffer */
  750. int byteoff; /* offset in current block */
  751. xfs_dir2_db_t curdb; /* db for current block */
  752. xfs_dir2_off_t curoff; /* current overall offset */
  753. xfs_dir2_data_hdr_t *hdr; /* data block header */
  754. xfs_dir2_data_entry_t *dep; /* data entry */
  755. xfs_dir2_data_unused_t *dup; /* unused entry */
  756. int error = 0; /* error return value */
  757. int i; /* temporary loop index */
  758. int j; /* temporary loop index */
  759. int length; /* temporary length value */
  760. xfs_bmbt_irec_t *map; /* map vector for blocks */
  761. xfs_extlen_t map_blocks; /* number of fsbs in map */
  762. xfs_dablk_t map_off; /* last mapped file offset */
  763. int map_size; /* total entries in *map */
  764. int map_valid; /* valid entries in *map */
  765. xfs_mount_t *mp; /* filesystem mount point */
  766. xfs_dir2_off_t newoff; /* new curoff after new blk */
  767. int nmap; /* mappings to ask xfs_bmapi */
  768. char *ptr = NULL; /* pointer to current data */
  769. int ra_current; /* number of read-ahead blks */
  770. int ra_index; /* *map index for read-ahead */
  771. int ra_offset; /* map entry offset for ra */
  772. int ra_want; /* readahead count wanted */
  773. /*
  774. * If the offset is at or past the largest allowed value,
  775. * give up right away.
  776. */
  777. if (*offset >= XFS_DIR2_MAX_DATAPTR)
  778. return 0;
  779. mp = dp->i_mount;
  780. /*
  781. * Set up to bmap a number of blocks based on the caller's
  782. * buffer size, the directory block size, and the filesystem
  783. * block size.
  784. */
  785. map_size = howmany(bufsize + mp->m_dirblksize, mp->m_sb.sb_blocksize);
  786. map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP);
  787. map_valid = ra_index = ra_offset = ra_current = map_blocks = 0;
  788. bp = NULL;
  789. /*
  790. * Inside the loop we keep the main offset value as a byte offset
  791. * in the directory file.
  792. */
  793. curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
  794. /*
  795. * Force this conversion through db so we truncate the offset
  796. * down to get the start of the data block.
  797. */
  798. map_off = xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, curoff));
  799. /*
  800. * Loop over directory entries until we reach the end offset.
  801. * Get more blocks and readahead as necessary.
  802. */
  803. while (curoff < XFS_DIR2_LEAF_OFFSET) {
  804. /*
  805. * If we have no buffer, or we're off the end of the
  806. * current buffer, need to get another one.
  807. */
  808. if (!bp || ptr >= (char *)bp->data + mp->m_dirblksize) {
  809. /*
  810. * If we have a buffer, we need to release it and
  811. * take it out of the mapping.
  812. */
  813. if (bp) {
  814. xfs_da_brelse(NULL, bp);
  815. bp = NULL;
  816. map_blocks -= mp->m_dirblkfsbs;
  817. /*
  818. * Loop to get rid of the extents for the
  819. * directory block.
  820. */
  821. for (i = mp->m_dirblkfsbs; i > 0; ) {
  822. j = MIN((int)map->br_blockcount, i);
  823. map->br_blockcount -= j;
  824. map->br_startblock += j;
  825. map->br_startoff += j;
  826. /*
  827. * If mapping is done, pitch it from
  828. * the table.
  829. */
  830. if (!map->br_blockcount && --map_valid)
  831. memmove(&map[0], &map[1],
  832. sizeof(map[0]) *
  833. map_valid);
  834. i -= j;
  835. }
  836. }
  837. /*
  838. * Recalculate the readahead blocks wanted.
  839. */
  840. ra_want = howmany(bufsize + mp->m_dirblksize,
  841. mp->m_sb.sb_blocksize) - 1;
  842. ASSERT(ra_want >= 0);
  843. /*
  844. * If we don't have as many as we want, and we haven't
  845. * run out of data blocks, get some more mappings.
  846. */
  847. if (1 + ra_want > map_blocks &&
  848. map_off <
  849. xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
  850. /*
  851. * Get more bmaps, fill in after the ones
  852. * we already have in the table.
  853. */
  854. nmap = map_size - map_valid;
  855. error = xfs_bmapi_read(dp, map_off,
  856. xfs_dir2_byte_to_da(mp,
  857. XFS_DIR2_LEAF_OFFSET) - map_off,
  858. &map[map_valid], &nmap, 0);
  859. /*
  860. * Don't know if we should ignore this or
  861. * try to return an error.
  862. * The trouble with returning errors
  863. * is that readdir will just stop without
  864. * actually passing the error through.
  865. */
  866. if (error)
  867. break; /* XXX */
  868. /*
  869. * If we got all the mappings we asked for,
  870. * set the final map offset based on the
  871. * last bmap value received.
  872. * Otherwise, we've reached the end.
  873. */
  874. if (nmap == map_size - map_valid)
  875. map_off =
  876. map[map_valid + nmap - 1].br_startoff +
  877. map[map_valid + nmap - 1].br_blockcount;
  878. else
  879. map_off =
  880. xfs_dir2_byte_to_da(mp,
  881. XFS_DIR2_LEAF_OFFSET);
  882. /*
  883. * Look for holes in the mapping, and
  884. * eliminate them. Count up the valid blocks.
  885. */
  886. for (i = map_valid; i < map_valid + nmap; ) {
  887. if (map[i].br_startblock ==
  888. HOLESTARTBLOCK) {
  889. nmap--;
  890. length = map_valid + nmap - i;
  891. if (length)
  892. memmove(&map[i],
  893. &map[i + 1],
  894. sizeof(map[i]) *
  895. length);
  896. } else {
  897. map_blocks +=
  898. map[i].br_blockcount;
  899. i++;
  900. }
  901. }
  902. map_valid += nmap;
  903. }
  904. /*
  905. * No valid mappings, so no more data blocks.
  906. */
  907. if (!map_valid) {
  908. curoff = xfs_dir2_da_to_byte(mp, map_off);
  909. break;
  910. }
  911. /*
  912. * Read the directory block starting at the first
  913. * mapping.
  914. */
  915. curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
  916. error = xfs_da_read_buf(NULL, dp, map->br_startoff,
  917. map->br_blockcount >= mp->m_dirblkfsbs ?
  918. XFS_FSB_TO_DADDR(mp, map->br_startblock) :
  919. -1,
  920. &bp, XFS_DATA_FORK);
  921. /*
  922. * Should just skip over the data block instead
  923. * of giving up.
  924. */
  925. if (error)
  926. break; /* XXX */
  927. /*
  928. * Adjust the current amount of read-ahead: we just
  929. * read a block that was previously ra.
  930. */
  931. if (ra_current)
  932. ra_current -= mp->m_dirblkfsbs;
  933. /*
  934. * Do we need more readahead?
  935. */
  936. for (ra_index = ra_offset = i = 0;
  937. ra_want > ra_current && i < map_blocks;
  938. i += mp->m_dirblkfsbs) {
  939. ASSERT(ra_index < map_valid);
  940. /*
  941. * Read-ahead a contiguous directory block.
  942. */
  943. if (i > ra_current &&
  944. map[ra_index].br_blockcount >=
  945. mp->m_dirblkfsbs) {
  946. xfs_buf_readahead(mp->m_ddev_targp,
  947. XFS_FSB_TO_DADDR(mp,
  948. map[ra_index].br_startblock +
  949. ra_offset),
  950. (int)BTOBB(mp->m_dirblksize));
  951. ra_current = i;
  952. }
  953. /*
  954. * Read-ahead a non-contiguous directory block.
  955. * This doesn't use our mapping, but this
  956. * is a very rare case.
  957. */
  958. else if (i > ra_current) {
  959. (void)xfs_da_reada_buf(NULL, dp,
  960. map[ra_index].br_startoff +
  961. ra_offset, XFS_DATA_FORK);
  962. ra_current = i;
  963. }
  964. /*
  965. * Advance offset through the mapping table.
  966. */
  967. for (j = 0; j < mp->m_dirblkfsbs; j++) {
  968. /*
  969. * The rest of this extent but not
  970. * more than a dir block.
  971. */
  972. length = MIN(mp->m_dirblkfsbs,
  973. (int)(map[ra_index].br_blockcount -
  974. ra_offset));
  975. j += length;
  976. ra_offset += length;
  977. /*
  978. * Advance to the next mapping if
  979. * this one is used up.
  980. */
  981. if (ra_offset ==
  982. map[ra_index].br_blockcount) {
  983. ra_offset = 0;
  984. ra_index++;
  985. }
  986. }
  987. }
  988. /*
  989. * Having done a read, we need to set a new offset.
  990. */
  991. newoff = xfs_dir2_db_off_to_byte(mp, curdb, 0);
  992. /*
  993. * Start of the current block.
  994. */
  995. if (curoff < newoff)
  996. curoff = newoff;
  997. /*
  998. * Make sure we're in the right block.
  999. */
  1000. else if (curoff > newoff)
  1001. ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
  1002. curdb);
  1003. hdr = bp->data;
  1004. xfs_dir2_data_check(dp, bp);
  1005. /*
  1006. * Find our position in the block.
  1007. */
  1008. ptr = (char *)(hdr + 1);
  1009. byteoff = xfs_dir2_byte_to_off(mp, curoff);
  1010. /*
  1011. * Skip past the header.
  1012. */
  1013. if (byteoff == 0)
  1014. curoff += (uint)sizeof(*hdr);
  1015. /*
  1016. * Skip past entries until we reach our offset.
  1017. */
  1018. else {
  1019. while ((char *)ptr - (char *)hdr < byteoff) {
  1020. dup = (xfs_dir2_data_unused_t *)ptr;
  1021. if (be16_to_cpu(dup->freetag)
  1022. == XFS_DIR2_DATA_FREE_TAG) {
  1023. length = be16_to_cpu(dup->length);
  1024. ptr += length;
  1025. continue;
  1026. }
  1027. dep = (xfs_dir2_data_entry_t *)ptr;
  1028. length =
  1029. xfs_dir2_data_entsize(dep->namelen);
  1030. ptr += length;
  1031. }
  1032. /*
  1033. * Now set our real offset.
  1034. */
  1035. curoff =
  1036. xfs_dir2_db_off_to_byte(mp,
  1037. xfs_dir2_byte_to_db(mp, curoff),
  1038. (char *)ptr - (char *)hdr);
  1039. if (ptr >= (char *)hdr + mp->m_dirblksize) {
  1040. continue;
  1041. }
  1042. }
  1043. }
  1044. /*
  1045. * We have a pointer to an entry.
  1046. * Is it a live one?
  1047. */
  1048. dup = (xfs_dir2_data_unused_t *)ptr;
  1049. /*
  1050. * No, it's unused, skip over it.
  1051. */
  1052. if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
  1053. length = be16_to_cpu(dup->length);
  1054. ptr += length;
  1055. curoff += length;
  1056. continue;
  1057. }
  1058. dep = (xfs_dir2_data_entry_t *)ptr;
  1059. length = xfs_dir2_data_entsize(dep->namelen);
  1060. if (filldir(dirent, (char *)dep->name, dep->namelen,
  1061. xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
  1062. be64_to_cpu(dep->inumber), DT_UNKNOWN))
  1063. break;
  1064. /*
  1065. * Advance to next entry in the block.
  1066. */
  1067. ptr += length;
  1068. curoff += length;
  1069. /* bufsize may have just been a guess; don't go negative */
  1070. bufsize = bufsize > length ? bufsize - length : 0;
  1071. }
  1072. /*
  1073. * All done. Set output offset value to current offset.
  1074. */
  1075. if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
  1076. *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
  1077. else
  1078. *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
  1079. kmem_free(map);
  1080. if (bp)
  1081. xfs_da_brelse(NULL, bp);
  1082. return error;
  1083. }
  1084. /*
  1085. * Initialize a new leaf block, leaf1 or leafn magic accepted.
  1086. */
  1087. int
  1088. xfs_dir2_leaf_init(
  1089. xfs_da_args_t *args, /* operation arguments */
  1090. xfs_dir2_db_t bno, /* directory block number */
  1091. xfs_dabuf_t **bpp, /* out: leaf buffer */
  1092. int magic) /* magic number for block */
  1093. {
  1094. xfs_dabuf_t *bp; /* leaf buffer */
  1095. xfs_inode_t *dp; /* incore directory inode */
  1096. int error; /* error return code */
  1097. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1098. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  1099. xfs_mount_t *mp; /* filesystem mount point */
  1100. xfs_trans_t *tp; /* transaction pointer */
  1101. dp = args->dp;
  1102. ASSERT(dp != NULL);
  1103. tp = args->trans;
  1104. mp = dp->i_mount;
  1105. ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
  1106. bno < XFS_DIR2_FREE_FIRSTDB(mp));
  1107. /*
  1108. * Get the buffer for the block.
  1109. */
  1110. error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
  1111. XFS_DATA_FORK);
  1112. if (error) {
  1113. return error;
  1114. }
  1115. ASSERT(bp != NULL);
  1116. leaf = bp->data;
  1117. /*
  1118. * Initialize the header.
  1119. */
  1120. leaf->hdr.info.magic = cpu_to_be16(magic);
  1121. leaf->hdr.info.forw = 0;
  1122. leaf->hdr.info.back = 0;
  1123. leaf->hdr.count = 0;
  1124. leaf->hdr.stale = 0;
  1125. xfs_dir2_leaf_log_header(tp, bp);
  1126. /*
  1127. * If it's a leaf-format directory initialize the tail.
  1128. * In this case our caller has the real bests table to copy into
  1129. * the block.
  1130. */
  1131. if (magic == XFS_DIR2_LEAF1_MAGIC) {
  1132. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  1133. ltp->bestcount = 0;
  1134. xfs_dir2_leaf_log_tail(tp, bp);
  1135. }
  1136. *bpp = bp;
  1137. return 0;
  1138. }
  1139. /*
  1140. * Log the bests entries indicated from a leaf1 block.
  1141. */
  1142. static void
  1143. xfs_dir2_leaf_log_bests(
  1144. xfs_trans_t *tp, /* transaction pointer */
  1145. xfs_dabuf_t *bp, /* leaf buffer */
  1146. int first, /* first entry to log */
  1147. int last) /* last entry to log */
  1148. {
  1149. __be16 *firstb; /* pointer to first entry */
  1150. __be16 *lastb; /* pointer to last entry */
  1151. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1152. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  1153. leaf = bp->data;
  1154. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
  1155. ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
  1156. firstb = xfs_dir2_leaf_bests_p(ltp) + first;
  1157. lastb = xfs_dir2_leaf_bests_p(ltp) + last;
  1158. xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
  1159. (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
  1160. }
  1161. /*
  1162. * Log the leaf entries indicated from a leaf1 or leafn block.
  1163. */
  1164. void
  1165. xfs_dir2_leaf_log_ents(
  1166. xfs_trans_t *tp, /* transaction pointer */
  1167. xfs_dabuf_t *bp, /* leaf buffer */
  1168. int first, /* first entry to log */
  1169. int last) /* last entry to log */
  1170. {
  1171. xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
  1172. xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
  1173. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1174. leaf = bp->data;
  1175. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
  1176. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
  1177. firstlep = &leaf->ents[first];
  1178. lastlep = &leaf->ents[last];
  1179. xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
  1180. (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
  1181. }
  1182. /*
  1183. * Log the header of the leaf1 or leafn block.
  1184. */
  1185. void
  1186. xfs_dir2_leaf_log_header(
  1187. xfs_trans_t *tp, /* transaction pointer */
  1188. xfs_dabuf_t *bp) /* leaf buffer */
  1189. {
  1190. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1191. leaf = bp->data;
  1192. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
  1193. leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
  1194. xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
  1195. (uint)(sizeof(leaf->hdr) - 1));
  1196. }
  1197. /*
  1198. * Log the tail of the leaf1 block.
  1199. */
  1200. STATIC void
  1201. xfs_dir2_leaf_log_tail(
  1202. xfs_trans_t *tp, /* transaction pointer */
  1203. xfs_dabuf_t *bp) /* leaf buffer */
  1204. {
  1205. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1206. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  1207. xfs_mount_t *mp; /* filesystem mount point */
  1208. mp = tp->t_mountp;
  1209. leaf = bp->data;
  1210. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC));
  1211. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  1212. xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
  1213. (uint)(mp->m_dirblksize - 1));
  1214. }
  1215. /*
  1216. * Look up the entry referred to by args in the leaf format directory.
  1217. * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
  1218. * is also used by the node-format code.
  1219. */
  1220. int
  1221. xfs_dir2_leaf_lookup(
  1222. xfs_da_args_t *args) /* operation arguments */
  1223. {
  1224. xfs_dabuf_t *dbp; /* data block buffer */
  1225. xfs_dir2_data_entry_t *dep; /* data block entry */
  1226. xfs_inode_t *dp; /* incore directory inode */
  1227. int error; /* error return code */
  1228. int index; /* found entry index */
  1229. xfs_dabuf_t *lbp; /* leaf buffer */
  1230. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1231. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1232. xfs_trans_t *tp; /* transaction pointer */
  1233. trace_xfs_dir2_leaf_lookup(args);
  1234. /*
  1235. * Look up name in the leaf block, returning both buffers and index.
  1236. */
  1237. if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
  1238. return error;
  1239. }
  1240. tp = args->trans;
  1241. dp = args->dp;
  1242. xfs_dir2_leaf_check(dp, lbp);
  1243. leaf = lbp->data;
  1244. /*
  1245. * Get to the leaf entry and contained data entry address.
  1246. */
  1247. lep = &leaf->ents[index];
  1248. /*
  1249. * Point to the data entry.
  1250. */
  1251. dep = (xfs_dir2_data_entry_t *)
  1252. ((char *)dbp->data +
  1253. xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
  1254. /*
  1255. * Return the found inode number & CI name if appropriate
  1256. */
  1257. args->inumber = be64_to_cpu(dep->inumber);
  1258. error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
  1259. xfs_da_brelse(tp, dbp);
  1260. xfs_da_brelse(tp, lbp);
  1261. return XFS_ERROR(error);
  1262. }
  1263. /*
  1264. * Look up name/hash in the leaf block.
  1265. * Fill in indexp with the found index, and dbpp with the data buffer.
  1266. * If not found dbpp will be NULL, and ENOENT comes back.
  1267. * lbpp will always be filled in with the leaf buffer unless there's an error.
  1268. */
  1269. static int /* error */
  1270. xfs_dir2_leaf_lookup_int(
  1271. xfs_da_args_t *args, /* operation arguments */
  1272. xfs_dabuf_t **lbpp, /* out: leaf buffer */
  1273. int *indexp, /* out: index in leaf block */
  1274. xfs_dabuf_t **dbpp) /* out: data buffer */
  1275. {
  1276. xfs_dir2_db_t curdb = -1; /* current data block number */
  1277. xfs_dabuf_t *dbp = NULL; /* data buffer */
  1278. xfs_dir2_data_entry_t *dep; /* data entry */
  1279. xfs_inode_t *dp; /* incore directory inode */
  1280. int error; /* error return code */
  1281. int index; /* index in leaf block */
  1282. xfs_dabuf_t *lbp; /* leaf buffer */
  1283. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1284. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1285. xfs_mount_t *mp; /* filesystem mount point */
  1286. xfs_dir2_db_t newdb; /* new data block number */
  1287. xfs_trans_t *tp; /* transaction pointer */
  1288. xfs_dir2_db_t cidb = -1; /* case match data block no. */
  1289. enum xfs_dacmp cmp; /* name compare result */
  1290. dp = args->dp;
  1291. tp = args->trans;
  1292. mp = dp->i_mount;
  1293. /*
  1294. * Read the leaf block into the buffer.
  1295. */
  1296. error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp,
  1297. XFS_DATA_FORK);
  1298. if (error)
  1299. return error;
  1300. *lbpp = lbp;
  1301. leaf = lbp->data;
  1302. xfs_dir2_leaf_check(dp, lbp);
  1303. /*
  1304. * Look for the first leaf entry with our hash value.
  1305. */
  1306. index = xfs_dir2_leaf_search_hash(args, lbp);
  1307. /*
  1308. * Loop over all the entries with the right hash value
  1309. * looking to match the name.
  1310. */
  1311. for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
  1312. be32_to_cpu(lep->hashval) == args->hashval;
  1313. lep++, index++) {
  1314. /*
  1315. * Skip over stale leaf entries.
  1316. */
  1317. if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
  1318. continue;
  1319. /*
  1320. * Get the new data block number.
  1321. */
  1322. newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
  1323. /*
  1324. * If it's not the same as the old data block number,
  1325. * need to pitch the old one and read the new one.
  1326. */
  1327. if (newdb != curdb) {
  1328. if (dbp)
  1329. xfs_da_brelse(tp, dbp);
  1330. error = xfs_da_read_buf(tp, dp,
  1331. xfs_dir2_db_to_da(mp, newdb),
  1332. -1, &dbp, XFS_DATA_FORK);
  1333. if (error) {
  1334. xfs_da_brelse(tp, lbp);
  1335. return error;
  1336. }
  1337. xfs_dir2_data_check(dp, dbp);
  1338. curdb = newdb;
  1339. }
  1340. /*
  1341. * Point to the data entry.
  1342. */
  1343. dep = (xfs_dir2_data_entry_t *)((char *)dbp->data +
  1344. xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
  1345. /*
  1346. * Compare name and if it's an exact match, return the index
  1347. * and buffer. If it's the first case-insensitive match, store
  1348. * the index and buffer and continue looking for an exact match.
  1349. */
  1350. cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
  1351. if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
  1352. args->cmpresult = cmp;
  1353. *indexp = index;
  1354. /* case exact match: return the current buffer. */
  1355. if (cmp == XFS_CMP_EXACT) {
  1356. *dbpp = dbp;
  1357. return 0;
  1358. }
  1359. cidb = curdb;
  1360. }
  1361. }
  1362. ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
  1363. /*
  1364. * Here, we can only be doing a lookup (not a rename or remove).
  1365. * If a case-insensitive match was found earlier, re-read the
  1366. * appropriate data block if required and return it.
  1367. */
  1368. if (args->cmpresult == XFS_CMP_CASE) {
  1369. ASSERT(cidb != -1);
  1370. if (cidb != curdb) {
  1371. xfs_da_brelse(tp, dbp);
  1372. error = xfs_da_read_buf(tp, dp,
  1373. xfs_dir2_db_to_da(mp, cidb),
  1374. -1, &dbp, XFS_DATA_FORK);
  1375. if (error) {
  1376. xfs_da_brelse(tp, lbp);
  1377. return error;
  1378. }
  1379. }
  1380. *dbpp = dbp;
  1381. return 0;
  1382. }
  1383. /*
  1384. * No match found, return ENOENT.
  1385. */
  1386. ASSERT(cidb == -1);
  1387. if (dbp)
  1388. xfs_da_brelse(tp, dbp);
  1389. xfs_da_brelse(tp, lbp);
  1390. return XFS_ERROR(ENOENT);
  1391. }
  1392. /*
  1393. * Remove an entry from a leaf format directory.
  1394. */
  1395. int /* error */
  1396. xfs_dir2_leaf_removename(
  1397. xfs_da_args_t *args) /* operation arguments */
  1398. {
  1399. __be16 *bestsp; /* leaf block best freespace */
  1400. xfs_dir2_data_hdr_t *hdr; /* data block header */
  1401. xfs_dir2_db_t db; /* data block number */
  1402. xfs_dabuf_t *dbp; /* data block buffer */
  1403. xfs_dir2_data_entry_t *dep; /* data entry structure */
  1404. xfs_inode_t *dp; /* incore directory inode */
  1405. int error; /* error return code */
  1406. xfs_dir2_db_t i; /* temporary data block # */
  1407. int index; /* index into leaf entries */
  1408. xfs_dabuf_t *lbp; /* leaf buffer */
  1409. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1410. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1411. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  1412. xfs_mount_t *mp; /* filesystem mount point */
  1413. int needlog; /* need to log data header */
  1414. int needscan; /* need to rescan data frees */
  1415. xfs_dir2_data_off_t oldbest; /* old value of best free */
  1416. xfs_trans_t *tp; /* transaction pointer */
  1417. trace_xfs_dir2_leaf_removename(args);
  1418. /*
  1419. * Lookup the leaf entry, get the leaf and data blocks read in.
  1420. */
  1421. if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
  1422. return error;
  1423. }
  1424. dp = args->dp;
  1425. tp = args->trans;
  1426. mp = dp->i_mount;
  1427. leaf = lbp->data;
  1428. hdr = dbp->data;
  1429. xfs_dir2_data_check(dp, dbp);
  1430. /*
  1431. * Point to the leaf entry, use that to point to the data entry.
  1432. */
  1433. lep = &leaf->ents[index];
  1434. db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
  1435. dep = (xfs_dir2_data_entry_t *)
  1436. ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
  1437. needscan = needlog = 0;
  1438. oldbest = be16_to_cpu(hdr->bestfree[0].length);
  1439. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  1440. bestsp = xfs_dir2_leaf_bests_p(ltp);
  1441. ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
  1442. /*
  1443. * Mark the former data entry unused.
  1444. */
  1445. xfs_dir2_data_make_free(tp, dbp,
  1446. (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
  1447. xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
  1448. /*
  1449. * We just mark the leaf entry stale by putting a null in it.
  1450. */
  1451. be16_add_cpu(&leaf->hdr.stale, 1);
  1452. xfs_dir2_leaf_log_header(tp, lbp);
  1453. lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
  1454. xfs_dir2_leaf_log_ents(tp, lbp, index, index);
  1455. /*
  1456. * Scan the freespace in the data block again if necessary,
  1457. * log the data block header if necessary.
  1458. */
  1459. if (needscan)
  1460. xfs_dir2_data_freescan(mp, hdr, &needlog);
  1461. if (needlog)
  1462. xfs_dir2_data_log_header(tp, dbp);
  1463. /*
  1464. * If the longest freespace in the data block has changed,
  1465. * put the new value in the bests table and log that.
  1466. */
  1467. if (be16_to_cpu(hdr->bestfree[0].length) != oldbest) {
  1468. bestsp[db] = hdr->bestfree[0].length;
  1469. xfs_dir2_leaf_log_bests(tp, lbp, db, db);
  1470. }
  1471. xfs_dir2_data_check(dp, dbp);
  1472. /*
  1473. * If the data block is now empty then get rid of the data block.
  1474. */
  1475. if (be16_to_cpu(hdr->bestfree[0].length) ==
  1476. mp->m_dirblksize - (uint)sizeof(*hdr)) {
  1477. ASSERT(db != mp->m_dirdatablk);
  1478. if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
  1479. /*
  1480. * Nope, can't get rid of it because it caused
  1481. * allocation of a bmap btree block to do so.
  1482. * Just go on, returning success, leaving the
  1483. * empty block in place.
  1484. */
  1485. if (error == ENOSPC && args->total == 0) {
  1486. xfs_da_buf_done(dbp);
  1487. error = 0;
  1488. }
  1489. xfs_dir2_leaf_check(dp, lbp);
  1490. xfs_da_buf_done(lbp);
  1491. return error;
  1492. }
  1493. dbp = NULL;
  1494. /*
  1495. * If this is the last data block then compact the
  1496. * bests table by getting rid of entries.
  1497. */
  1498. if (db == be32_to_cpu(ltp->bestcount) - 1) {
  1499. /*
  1500. * Look for the last active entry (i).
  1501. */
  1502. for (i = db - 1; i > 0; i--) {
  1503. if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
  1504. break;
  1505. }
  1506. /*
  1507. * Copy the table down so inactive entries at the
  1508. * end are removed.
  1509. */
  1510. memmove(&bestsp[db - i], bestsp,
  1511. (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
  1512. be32_add_cpu(&ltp->bestcount, -(db - i));
  1513. xfs_dir2_leaf_log_tail(tp, lbp);
  1514. xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
  1515. } else
  1516. bestsp[db] = cpu_to_be16(NULLDATAOFF);
  1517. }
  1518. /*
  1519. * If the data block was not the first one, drop it.
  1520. */
  1521. else if (db != mp->m_dirdatablk && dbp != NULL) {
  1522. xfs_da_buf_done(dbp);
  1523. dbp = NULL;
  1524. }
  1525. xfs_dir2_leaf_check(dp, lbp);
  1526. /*
  1527. * See if we can convert to block form.
  1528. */
  1529. return xfs_dir2_leaf_to_block(args, lbp, dbp);
  1530. }
  1531. /*
  1532. * Replace the inode number in a leaf format directory entry.
  1533. */
  1534. int /* error */
  1535. xfs_dir2_leaf_replace(
  1536. xfs_da_args_t *args) /* operation arguments */
  1537. {
  1538. xfs_dabuf_t *dbp; /* data block buffer */
  1539. xfs_dir2_data_entry_t *dep; /* data block entry */
  1540. xfs_inode_t *dp; /* incore directory inode */
  1541. int error; /* error return code */
  1542. int index; /* index of leaf entry */
  1543. xfs_dabuf_t *lbp; /* leaf buffer */
  1544. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1545. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1546. xfs_trans_t *tp; /* transaction pointer */
  1547. trace_xfs_dir2_leaf_replace(args);
  1548. /*
  1549. * Look up the entry.
  1550. */
  1551. if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
  1552. return error;
  1553. }
  1554. dp = args->dp;
  1555. leaf = lbp->data;
  1556. /*
  1557. * Point to the leaf entry, get data address from it.
  1558. */
  1559. lep = &leaf->ents[index];
  1560. /*
  1561. * Point to the data entry.
  1562. */
  1563. dep = (xfs_dir2_data_entry_t *)
  1564. ((char *)dbp->data +
  1565. xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
  1566. ASSERT(args->inumber != be64_to_cpu(dep->inumber));
  1567. /*
  1568. * Put the new inode number in, log it.
  1569. */
  1570. dep->inumber = cpu_to_be64(args->inumber);
  1571. tp = args->trans;
  1572. xfs_dir2_data_log_entry(tp, dbp, dep);
  1573. xfs_da_buf_done(dbp);
  1574. xfs_dir2_leaf_check(dp, lbp);
  1575. xfs_da_brelse(tp, lbp);
  1576. return 0;
  1577. }
  1578. /*
  1579. * Return index in the leaf block (lbp) which is either the first
  1580. * one with this hash value, or if there are none, the insert point
  1581. * for that hash value.
  1582. */
  1583. int /* index value */
  1584. xfs_dir2_leaf_search_hash(
  1585. xfs_da_args_t *args, /* operation arguments */
  1586. xfs_dabuf_t *lbp) /* leaf buffer */
  1587. {
  1588. xfs_dahash_t hash=0; /* hash from this entry */
  1589. xfs_dahash_t hashwant; /* hash value looking for */
  1590. int high; /* high leaf index */
  1591. int low; /* low leaf index */
  1592. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1593. xfs_dir2_leaf_entry_t *lep; /* leaf entry */
  1594. int mid=0; /* current leaf index */
  1595. leaf = lbp->data;
  1596. #ifndef __KERNEL__
  1597. if (!leaf->hdr.count)
  1598. return 0;
  1599. #endif
  1600. /*
  1601. * Note, the table cannot be empty, so we have to go through the loop.
  1602. * Binary search the leaf entries looking for our hash value.
  1603. */
  1604. for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1,
  1605. hashwant = args->hashval;
  1606. low <= high; ) {
  1607. mid = (low + high) >> 1;
  1608. if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
  1609. break;
  1610. if (hash < hashwant)
  1611. low = mid + 1;
  1612. else
  1613. high = mid - 1;
  1614. }
  1615. /*
  1616. * Found one, back up through all the equal hash values.
  1617. */
  1618. if (hash == hashwant) {
  1619. while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
  1620. mid--;
  1621. }
  1622. }
  1623. /*
  1624. * Need to point to an entry higher than ours.
  1625. */
  1626. else if (hash < hashwant)
  1627. mid++;
  1628. return mid;
  1629. }
  1630. /*
  1631. * Trim off a trailing data block. We know it's empty since the leaf
  1632. * freespace table says so.
  1633. */
  1634. int /* error */
  1635. xfs_dir2_leaf_trim_data(
  1636. xfs_da_args_t *args, /* operation arguments */
  1637. xfs_dabuf_t *lbp, /* leaf buffer */
  1638. xfs_dir2_db_t db) /* data block number */
  1639. {
  1640. __be16 *bestsp; /* leaf bests table */
  1641. xfs_dabuf_t *dbp; /* data block buffer */
  1642. xfs_inode_t *dp; /* incore directory inode */
  1643. int error; /* error return value */
  1644. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1645. xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
  1646. xfs_mount_t *mp; /* filesystem mount point */
  1647. xfs_trans_t *tp; /* transaction pointer */
  1648. dp = args->dp;
  1649. mp = dp->i_mount;
  1650. tp = args->trans;
  1651. /*
  1652. * Read the offending data block. We need its buffer.
  1653. */
  1654. if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp,
  1655. XFS_DATA_FORK))) {
  1656. return error;
  1657. }
  1658. leaf = lbp->data;
  1659. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  1660. #ifdef DEBUG
  1661. {
  1662. struct xfs_dir2_data_hdr *hdr = dbp->data;
  1663. ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
  1664. ASSERT(be16_to_cpu(hdr->bestfree[0].length) ==
  1665. mp->m_dirblksize - (uint)sizeof(*hdr));
  1666. ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
  1667. }
  1668. #endif
  1669. /*
  1670. * Get rid of the data block.
  1671. */
  1672. if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
  1673. ASSERT(error != ENOSPC);
  1674. xfs_da_brelse(tp, dbp);
  1675. return error;
  1676. }
  1677. /*
  1678. * Eliminate the last bests entry from the table.
  1679. */
  1680. bestsp = xfs_dir2_leaf_bests_p(ltp);
  1681. be32_add_cpu(&ltp->bestcount, -1);
  1682. memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
  1683. xfs_dir2_leaf_log_tail(tp, lbp);
  1684. xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
  1685. return 0;
  1686. }
  1687. static inline size_t
  1688. xfs_dir2_leaf_size(
  1689. struct xfs_dir2_leaf_hdr *hdr,
  1690. int counts)
  1691. {
  1692. int entries;
  1693. entries = be16_to_cpu(hdr->count) - be16_to_cpu(hdr->stale);
  1694. return sizeof(xfs_dir2_leaf_hdr_t) +
  1695. entries * sizeof(xfs_dir2_leaf_entry_t) +
  1696. counts * sizeof(xfs_dir2_data_off_t) +
  1697. sizeof(xfs_dir2_leaf_tail_t);
  1698. }
  1699. /*
  1700. * Convert node form directory to leaf form directory.
  1701. * The root of the node form dir needs to already be a LEAFN block.
  1702. * Just return if we can't do anything.
  1703. */
  1704. int /* error */
  1705. xfs_dir2_node_to_leaf(
  1706. xfs_da_state_t *state) /* directory operation state */
  1707. {
  1708. xfs_da_args_t *args; /* operation arguments */
  1709. xfs_inode_t *dp; /* incore directory inode */
  1710. int error; /* error return code */
  1711. xfs_dabuf_t *fbp; /* buffer for freespace block */
  1712. xfs_fileoff_t fo; /* freespace file offset */
  1713. xfs_dir2_free_t *free; /* freespace structure */
  1714. xfs_dabuf_t *lbp; /* buffer for leaf block */
  1715. xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
  1716. xfs_dir2_leaf_t *leaf; /* leaf structure */
  1717. xfs_mount_t *mp; /* filesystem mount point */
  1718. int rval; /* successful free trim? */
  1719. xfs_trans_t *tp; /* transaction pointer */
  1720. /*
  1721. * There's more than a leaf level in the btree, so there must
  1722. * be multiple leafn blocks. Give up.
  1723. */
  1724. if (state->path.active > 1)
  1725. return 0;
  1726. args = state->args;
  1727. trace_xfs_dir2_node_to_leaf(args);
  1728. mp = state->mp;
  1729. dp = args->dp;
  1730. tp = args->trans;
  1731. /*
  1732. * Get the last offset in the file.
  1733. */
  1734. if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
  1735. return error;
  1736. }
  1737. fo -= mp->m_dirblkfsbs;
  1738. /*
  1739. * If there are freespace blocks other than the first one,
  1740. * take this opportunity to remove trailing empty freespace blocks
  1741. * that may have been left behind during no-space-reservation
  1742. * operations.
  1743. */
  1744. while (fo > mp->m_dirfreeblk) {
  1745. if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
  1746. return error;
  1747. }
  1748. if (rval)
  1749. fo -= mp->m_dirblkfsbs;
  1750. else
  1751. return 0;
  1752. }
  1753. /*
  1754. * Now find the block just before the freespace block.
  1755. */
  1756. if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
  1757. return error;
  1758. }
  1759. /*
  1760. * If it's not the single leaf block, give up.
  1761. */
  1762. if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
  1763. return 0;
  1764. lbp = state->path.blk[0].bp;
  1765. leaf = lbp->data;
  1766. ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
  1767. /*
  1768. * Read the freespace block.
  1769. */
  1770. if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp,
  1771. XFS_DATA_FORK))) {
  1772. return error;
  1773. }
  1774. free = fbp->data;
  1775. ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
  1776. ASSERT(!free->hdr.firstdb);
  1777. /*
  1778. * Now see if the leafn and free data will fit in a leaf1.
  1779. * If not, release the buffer and give up.
  1780. */
  1781. if (xfs_dir2_leaf_size(&leaf->hdr, be32_to_cpu(free->hdr.nvalid)) >
  1782. mp->m_dirblksize) {
  1783. xfs_da_brelse(tp, fbp);
  1784. return 0;
  1785. }
  1786. /*
  1787. * If the leaf has any stale entries in it, compress them out.
  1788. * The compact routine will log the header.
  1789. */
  1790. if (be16_to_cpu(leaf->hdr.stale))
  1791. xfs_dir2_leaf_compact(args, lbp);
  1792. else
  1793. xfs_dir2_leaf_log_header(tp, lbp);
  1794. leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC);
  1795. /*
  1796. * Set up the leaf tail from the freespace block.
  1797. */
  1798. ltp = xfs_dir2_leaf_tail_p(mp, leaf);
  1799. ltp->bestcount = free->hdr.nvalid;
  1800. /*
  1801. * Set up the leaf bests table.
  1802. */
  1803. memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests,
  1804. be32_to_cpu(ltp->bestcount) * sizeof(xfs_dir2_data_off_t));
  1805. xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
  1806. xfs_dir2_leaf_log_tail(tp, lbp);
  1807. xfs_dir2_leaf_check(dp, lbp);
  1808. /*
  1809. * Get rid of the freespace block.
  1810. */
  1811. error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
  1812. if (error) {
  1813. /*
  1814. * This can't fail here because it can only happen when
  1815. * punching out the middle of an extent, and this is an
  1816. * isolated block.
  1817. */
  1818. ASSERT(error != ENOSPC);
  1819. return error;
  1820. }
  1821. fbp = NULL;
  1822. /*
  1823. * Now see if we can convert the single-leaf directory
  1824. * down to a block form directory.
  1825. * This routine always kills the dabuf for the leaf, so
  1826. * eliminate it from the path.
  1827. */
  1828. error = xfs_dir2_leaf_to_block(args, lbp, NULL);
  1829. state->path.blk[0].bp = NULL;
  1830. return error;
  1831. }