xfs_dir2_leaf.c 53 KB

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