xfs_dir2_leaf.c 54 KB

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