xfs_dir2_leaf.c 53 KB

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