xfs_dir2_leaf.c 55 KB

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