xfs_dir2_leaf.c 50 KB

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