xfs_dir2_leaf.c 53 KB

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