xfs_dir2_leaf.c 55 KB

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