xfs_dir2_leaf.c 53 KB

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