xfs_dir2_leaf.c 51 KB

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