xfs_dir2_leaf.c 55 KB

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