xfs_dir2_leaf.c 55 KB

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