xfs_dir2_leaf.c 53 KB

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