xfs_dir2_leaf.c 53 KB

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