xfs_dir2_leaf.c 52 KB

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