xfs_dir2_leaf.c 51 KB

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