xfs_dir2_leaf.c 50 KB

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