xfs_dir2_leaf.c 61 KB

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