xfs_dir2_leaf.c 50 KB

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