xfs_dir2_leaf.c 55 KB

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