xfs_dir2_leaf.c 54 KB

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