xfs_dir_leaf.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  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_log.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_dir.h"
  26. #include "xfs_dir2.h"
  27. #include "xfs_dmapi.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_da_btree.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_dir_sf.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. #include "xfs_inode.h"
  38. #include "xfs_inode_item.h"
  39. #include "xfs_alloc.h"
  40. #include "xfs_btree.h"
  41. #include "xfs_bmap.h"
  42. #include "xfs_dir_leaf.h"
  43. #include "xfs_error.h"
  44. /*
  45. * xfs_dir_leaf.c
  46. *
  47. * Routines to implement leaf blocks of directories as Btrees of hashed names.
  48. */
  49. /*========================================================================
  50. * Function prototypes for the kernel.
  51. *========================================================================*/
  52. /*
  53. * Routines used for growing the Btree.
  54. */
  55. STATIC void xfs_dir_leaf_add_work(xfs_dabuf_t *leaf_buffer, xfs_da_args_t *args,
  56. int insertion_index,
  57. int freemap_index);
  58. STATIC int xfs_dir_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *leaf_buffer,
  59. int musthave, int justcheck);
  60. STATIC void xfs_dir_leaf_rebalance(xfs_da_state_t *state,
  61. xfs_da_state_blk_t *blk1,
  62. xfs_da_state_blk_t *blk2);
  63. STATIC int xfs_dir_leaf_figure_balance(xfs_da_state_t *state,
  64. xfs_da_state_blk_t *leaf_blk_1,
  65. xfs_da_state_blk_t *leaf_blk_2,
  66. int *number_entries_in_blk1,
  67. int *number_namebytes_in_blk1);
  68. STATIC int xfs_dir_leaf_create(struct xfs_da_args *args,
  69. xfs_dablk_t which_block,
  70. struct xfs_dabuf **bpp);
  71. /*
  72. * Utility routines.
  73. */
  74. STATIC void xfs_dir_leaf_moveents(xfs_dir_leafblock_t *src_leaf,
  75. int src_start,
  76. xfs_dir_leafblock_t *dst_leaf,
  77. int dst_start, int move_count,
  78. xfs_mount_t *mp);
  79. /*========================================================================
  80. * External routines when dirsize < XFS_IFORK_DSIZE(dp).
  81. *========================================================================*/
  82. /*
  83. * Validate a given inode number.
  84. */
  85. int
  86. xfs_dir_ino_validate(xfs_mount_t *mp, xfs_ino_t ino)
  87. {
  88. xfs_agblock_t agblkno;
  89. xfs_agino_t agino;
  90. xfs_agnumber_t agno;
  91. int ino_ok;
  92. int ioff;
  93. agno = XFS_INO_TO_AGNO(mp, ino);
  94. agblkno = XFS_INO_TO_AGBNO(mp, ino);
  95. ioff = XFS_INO_TO_OFFSET(mp, ino);
  96. agino = XFS_OFFBNO_TO_AGINO(mp, agblkno, ioff);
  97. ino_ok =
  98. agno < mp->m_sb.sb_agcount &&
  99. agblkno < mp->m_sb.sb_agblocks &&
  100. agblkno != 0 &&
  101. ioff < (1 << mp->m_sb.sb_inopblog) &&
  102. XFS_AGINO_TO_INO(mp, agno, agino) == ino;
  103. if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE,
  104. XFS_RANDOM_DIR_INO_VALIDATE))) {
  105. xfs_fs_cmn_err(CE_WARN, mp, "Invalid inode number 0x%Lx",
  106. (unsigned long long) ino);
  107. XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
  108. return XFS_ERROR(EFSCORRUPTED);
  109. }
  110. return 0;
  111. }
  112. /*
  113. * Create the initial contents of a shortform directory.
  114. */
  115. int
  116. xfs_dir_shortform_create(xfs_da_args_t *args, xfs_ino_t parent)
  117. {
  118. xfs_dir_sf_hdr_t *hdr;
  119. xfs_inode_t *dp;
  120. dp = args->dp;
  121. ASSERT(dp != NULL);
  122. ASSERT(dp->i_d.di_size == 0);
  123. if (dp->i_d.di_format == XFS_DINODE_FMT_EXTENTS) {
  124. dp->i_df.if_flags &= ~XFS_IFEXTENTS; /* just in case */
  125. dp->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  126. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
  127. dp->i_df.if_flags |= XFS_IFINLINE;
  128. }
  129. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  130. ASSERT(dp->i_df.if_bytes == 0);
  131. xfs_idata_realloc(dp, sizeof(*hdr), XFS_DATA_FORK);
  132. hdr = (xfs_dir_sf_hdr_t *)dp->i_df.if_u1.if_data;
  133. XFS_DIR_SF_PUT_DIRINO(&parent, &hdr->parent);
  134. hdr->count = 0;
  135. dp->i_d.di_size = sizeof(*hdr);
  136. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  137. return 0;
  138. }
  139. /*
  140. * Add a name to the shortform directory structure.
  141. * Overflow from the inode has already been checked for.
  142. */
  143. int
  144. xfs_dir_shortform_addname(xfs_da_args_t *args)
  145. {
  146. xfs_dir_shortform_t *sf;
  147. xfs_dir_sf_entry_t *sfe;
  148. int i, offset, size;
  149. xfs_inode_t *dp;
  150. dp = args->dp;
  151. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  152. /*
  153. * Catch the case where the conversion from shortform to leaf
  154. * failed part way through.
  155. */
  156. if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
  157. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  158. return XFS_ERROR(EIO);
  159. }
  160. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  161. ASSERT(dp->i_df.if_u1.if_data != NULL);
  162. sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
  163. sfe = &sf->list[0];
  164. for (i = sf->hdr.count-1; i >= 0; i--) {
  165. if (sfe->namelen == args->namelen &&
  166. args->name[0] == sfe->name[0] &&
  167. memcmp(args->name, sfe->name, args->namelen) == 0)
  168. return XFS_ERROR(EEXIST);
  169. sfe = XFS_DIR_SF_NEXTENTRY(sfe);
  170. }
  171. offset = (int)((char *)sfe - (char *)sf);
  172. size = XFS_DIR_SF_ENTSIZE_BYNAME(args->namelen);
  173. xfs_idata_realloc(dp, size, XFS_DATA_FORK);
  174. sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
  175. sfe = (xfs_dir_sf_entry_t *)((char *)sf + offset);
  176. XFS_DIR_SF_PUT_DIRINO(&args->inumber, &sfe->inumber);
  177. sfe->namelen = args->namelen;
  178. memcpy(sfe->name, args->name, sfe->namelen);
  179. sf->hdr.count++;
  180. dp->i_d.di_size += size;
  181. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  182. return 0;
  183. }
  184. /*
  185. * Remove a name from the shortform directory structure.
  186. */
  187. int
  188. xfs_dir_shortform_removename(xfs_da_args_t *args)
  189. {
  190. xfs_dir_shortform_t *sf;
  191. xfs_dir_sf_entry_t *sfe;
  192. int base, size = 0, i;
  193. xfs_inode_t *dp;
  194. dp = args->dp;
  195. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  196. /*
  197. * Catch the case where the conversion from shortform to leaf
  198. * failed part way through.
  199. */
  200. if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
  201. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  202. return XFS_ERROR(EIO);
  203. }
  204. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  205. ASSERT(dp->i_df.if_u1.if_data != NULL);
  206. base = sizeof(xfs_dir_sf_hdr_t);
  207. sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
  208. sfe = &sf->list[0];
  209. for (i = sf->hdr.count-1; i >= 0; i--) {
  210. size = XFS_DIR_SF_ENTSIZE_BYENTRY(sfe);
  211. if (sfe->namelen == args->namelen &&
  212. sfe->name[0] == args->name[0] &&
  213. memcmp(sfe->name, args->name, args->namelen) == 0)
  214. break;
  215. base += size;
  216. sfe = XFS_DIR_SF_NEXTENTRY(sfe);
  217. }
  218. if (i < 0) {
  219. ASSERT(args->oknoent);
  220. return XFS_ERROR(ENOENT);
  221. }
  222. if ((base + size) != dp->i_d.di_size) {
  223. memmove(&((char *)sf)[base], &((char *)sf)[base+size],
  224. dp->i_d.di_size - (base+size));
  225. }
  226. sf->hdr.count--;
  227. xfs_idata_realloc(dp, -size, XFS_DATA_FORK);
  228. dp->i_d.di_size -= size;
  229. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_DDATA);
  230. return 0;
  231. }
  232. /*
  233. * Look up a name in a shortform directory structure.
  234. */
  235. int
  236. xfs_dir_shortform_lookup(xfs_da_args_t *args)
  237. {
  238. xfs_dir_shortform_t *sf;
  239. xfs_dir_sf_entry_t *sfe;
  240. int i;
  241. xfs_inode_t *dp;
  242. dp = args->dp;
  243. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  244. /*
  245. * Catch the case where the conversion from shortform to leaf
  246. * failed part way through.
  247. */
  248. if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
  249. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  250. return XFS_ERROR(EIO);
  251. }
  252. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  253. ASSERT(dp->i_df.if_u1.if_data != NULL);
  254. sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
  255. if (args->namelen == 2 &&
  256. args->name[0] == '.' && args->name[1] == '.') {
  257. XFS_DIR_SF_GET_DIRINO(&sf->hdr.parent, &args->inumber);
  258. return(XFS_ERROR(EEXIST));
  259. }
  260. if (args->namelen == 1 && args->name[0] == '.') {
  261. args->inumber = dp->i_ino;
  262. return(XFS_ERROR(EEXIST));
  263. }
  264. sfe = &sf->list[0];
  265. for (i = sf->hdr.count-1; i >= 0; i--) {
  266. if (sfe->namelen == args->namelen &&
  267. sfe->name[0] == args->name[0] &&
  268. memcmp(args->name, sfe->name, args->namelen) == 0) {
  269. XFS_DIR_SF_GET_DIRINO(&sfe->inumber, &args->inumber);
  270. return(XFS_ERROR(EEXIST));
  271. }
  272. sfe = XFS_DIR_SF_NEXTENTRY(sfe);
  273. }
  274. ASSERT(args->oknoent);
  275. return(XFS_ERROR(ENOENT));
  276. }
  277. /*
  278. * Convert from using the shortform to the leaf.
  279. */
  280. int
  281. xfs_dir_shortform_to_leaf(xfs_da_args_t *iargs)
  282. {
  283. xfs_inode_t *dp;
  284. xfs_dir_shortform_t *sf;
  285. xfs_dir_sf_entry_t *sfe;
  286. xfs_da_args_t args;
  287. xfs_ino_t inumber;
  288. char *tmpbuffer;
  289. int retval, i, size;
  290. xfs_dablk_t blkno;
  291. xfs_dabuf_t *bp;
  292. dp = iargs->dp;
  293. /*
  294. * Catch the case where the conversion from shortform to leaf
  295. * failed part way through.
  296. */
  297. if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
  298. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  299. return XFS_ERROR(EIO);
  300. }
  301. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  302. ASSERT(dp->i_df.if_u1.if_data != NULL);
  303. size = dp->i_df.if_bytes;
  304. tmpbuffer = kmem_alloc(size, KM_SLEEP);
  305. ASSERT(tmpbuffer != NULL);
  306. memcpy(tmpbuffer, dp->i_df.if_u1.if_data, size);
  307. sf = (xfs_dir_shortform_t *)tmpbuffer;
  308. XFS_DIR_SF_GET_DIRINO(&sf->hdr.parent, &inumber);
  309. xfs_idata_realloc(dp, -size, XFS_DATA_FORK);
  310. dp->i_d.di_size = 0;
  311. xfs_trans_log_inode(iargs->trans, dp, XFS_ILOG_CORE);
  312. retval = xfs_da_grow_inode(iargs, &blkno);
  313. if (retval)
  314. goto out;
  315. ASSERT(blkno == 0);
  316. retval = xfs_dir_leaf_create(iargs, blkno, &bp);
  317. if (retval)
  318. goto out;
  319. xfs_da_buf_done(bp);
  320. args.name = ".";
  321. args.namelen = 1;
  322. args.hashval = xfs_dir_hash_dot;
  323. args.inumber = dp->i_ino;
  324. args.dp = dp;
  325. args.firstblock = iargs->firstblock;
  326. args.flist = iargs->flist;
  327. args.total = iargs->total;
  328. args.whichfork = XFS_DATA_FORK;
  329. args.trans = iargs->trans;
  330. args.justcheck = 0;
  331. args.addname = args.oknoent = 1;
  332. retval = xfs_dir_leaf_addname(&args);
  333. if (retval)
  334. goto out;
  335. args.name = "..";
  336. args.namelen = 2;
  337. args.hashval = xfs_dir_hash_dotdot;
  338. args.inumber = inumber;
  339. retval = xfs_dir_leaf_addname(&args);
  340. if (retval)
  341. goto out;
  342. sfe = &sf->list[0];
  343. for (i = 0; i < sf->hdr.count; i++) {
  344. args.name = (char *)(sfe->name);
  345. args.namelen = sfe->namelen;
  346. args.hashval = xfs_da_hashname((char *)(sfe->name),
  347. sfe->namelen);
  348. XFS_DIR_SF_GET_DIRINO(&sfe->inumber, &args.inumber);
  349. retval = xfs_dir_leaf_addname(&args);
  350. if (retval)
  351. goto out;
  352. sfe = XFS_DIR_SF_NEXTENTRY(sfe);
  353. }
  354. retval = 0;
  355. out:
  356. kmem_free(tmpbuffer, size);
  357. return retval;
  358. }
  359. STATIC int
  360. xfs_dir_shortform_compare(const void *a, const void *b)
  361. {
  362. xfs_dir_sf_sort_t *sa, *sb;
  363. sa = (xfs_dir_sf_sort_t *)a;
  364. sb = (xfs_dir_sf_sort_t *)b;
  365. if (sa->hash < sb->hash)
  366. return -1;
  367. else if (sa->hash > sb->hash)
  368. return 1;
  369. else
  370. return sa->entno - sb->entno;
  371. }
  372. /*
  373. * Copy out directory entries for getdents(), for shortform directories.
  374. */
  375. /*ARGSUSED*/
  376. int
  377. xfs_dir_shortform_getdents(xfs_inode_t *dp, uio_t *uio, int *eofp,
  378. xfs_dirent_t *dbp, xfs_dir_put_t put)
  379. {
  380. xfs_dir_shortform_t *sf;
  381. xfs_dir_sf_entry_t *sfe;
  382. int retval, i, sbsize, nsbuf, lastresid=0, want_entno;
  383. xfs_mount_t *mp;
  384. xfs_dahash_t cookhash, hash;
  385. xfs_dir_put_args_t p;
  386. xfs_dir_sf_sort_t *sbuf, *sbp;
  387. mp = dp->i_mount;
  388. sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
  389. cookhash = XFS_DA_COOKIE_HASH(mp, uio->uio_offset);
  390. want_entno = XFS_DA_COOKIE_ENTRY(mp, uio->uio_offset);
  391. nsbuf = sf->hdr.count + 2;
  392. sbsize = (nsbuf + 1) * sizeof(*sbuf);
  393. sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP);
  394. xfs_dir_trace_g_du("sf: start", dp, uio);
  395. /*
  396. * Collect all the entries into the buffer.
  397. * Entry 0 is .
  398. */
  399. sbp->entno = 0;
  400. sbp->seqno = 0;
  401. sbp->hash = xfs_dir_hash_dot;
  402. sbp->ino = dp->i_ino;
  403. sbp->name = ".";
  404. sbp->namelen = 1;
  405. sbp++;
  406. /*
  407. * Entry 1 is ..
  408. */
  409. sbp->entno = 1;
  410. sbp->seqno = 0;
  411. sbp->hash = xfs_dir_hash_dotdot;
  412. sbp->ino = XFS_GET_DIR_INO8(sf->hdr.parent);
  413. sbp->name = "..";
  414. sbp->namelen = 2;
  415. sbp++;
  416. /*
  417. * Scan the directory data for the rest of the entries.
  418. */
  419. for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
  420. if (unlikely(
  421. ((char *)sfe < (char *)sf) ||
  422. ((char *)sfe >= ((char *)sf + dp->i_df.if_bytes)))) {
  423. xfs_dir_trace_g_du("sf: corrupted", dp, uio);
  424. XFS_CORRUPTION_ERROR("xfs_dir_shortform_getdents",
  425. XFS_ERRLEVEL_LOW, mp, sfe);
  426. kmem_free(sbuf, sbsize);
  427. return XFS_ERROR(EFSCORRUPTED);
  428. }
  429. sbp->entno = i + 2;
  430. sbp->seqno = 0;
  431. sbp->hash = xfs_da_hashname((char *)sfe->name, sfe->namelen);
  432. sbp->ino = XFS_GET_DIR_INO8(sfe->inumber);
  433. sbp->name = (char *)sfe->name;
  434. sbp->namelen = sfe->namelen;
  435. sfe = XFS_DIR_SF_NEXTENTRY(sfe);
  436. sbp++;
  437. }
  438. /*
  439. * Sort the entries on hash then entno.
  440. */
  441. xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_dir_shortform_compare);
  442. /*
  443. * Stuff in last entry.
  444. */
  445. sbp->entno = nsbuf;
  446. sbp->hash = XFS_DA_MAXHASH;
  447. sbp->seqno = 0;
  448. /*
  449. * Figure out the sequence numbers in case there's a hash duplicate.
  450. */
  451. for (hash = sbuf->hash, sbp = sbuf + 1;
  452. sbp < &sbuf[nsbuf + 1]; sbp++) {
  453. if (sbp->hash == hash)
  454. sbp->seqno = sbp[-1].seqno + 1;
  455. else
  456. hash = sbp->hash;
  457. }
  458. /*
  459. * Set up put routine.
  460. */
  461. p.dbp = dbp;
  462. p.put = put;
  463. p.uio = uio;
  464. /*
  465. * Find our place.
  466. */
  467. for (sbp = sbuf; sbp < &sbuf[nsbuf + 1]; sbp++) {
  468. if (sbp->hash > cookhash ||
  469. (sbp->hash == cookhash && sbp->seqno >= want_entno))
  470. break;
  471. }
  472. /*
  473. * Did we fail to find anything? We stop at the last entry,
  474. * the one we put maxhash into.
  475. */
  476. if (sbp == &sbuf[nsbuf]) {
  477. kmem_free(sbuf, sbsize);
  478. xfs_dir_trace_g_du("sf: hash beyond end", dp, uio);
  479. uio->uio_offset = XFS_DA_MAKE_COOKIE(mp, 0, 0, XFS_DA_MAXHASH);
  480. *eofp = 1;
  481. return 0;
  482. }
  483. /*
  484. * Loop putting entries into the user buffer.
  485. */
  486. while (sbp < &sbuf[nsbuf]) {
  487. /*
  488. * Save the first resid in a run of equal-hashval entries
  489. * so that we can back them out if they don't all fit.
  490. */
  491. if (sbp->seqno == 0 || sbp == sbuf)
  492. lastresid = uio->uio_resid;
  493. XFS_PUT_COOKIE(p.cook, mp, 0, sbp[1].seqno, sbp[1].hash);
  494. p.ino = sbp->ino;
  495. #if XFS_BIG_INUMS
  496. p.ino += mp->m_inoadd;
  497. #endif
  498. p.name = sbp->name;
  499. p.namelen = sbp->namelen;
  500. retval = p.put(&p);
  501. if (!p.done) {
  502. uio->uio_offset =
  503. XFS_DA_MAKE_COOKIE(mp, 0, 0, sbp->hash);
  504. kmem_free(sbuf, sbsize);
  505. uio->uio_resid = lastresid;
  506. xfs_dir_trace_g_du("sf: E-O-B", dp, uio);
  507. return retval;
  508. }
  509. sbp++;
  510. }
  511. kmem_free(sbuf, sbsize);
  512. uio->uio_offset = p.cook.o;
  513. *eofp = 1;
  514. xfs_dir_trace_g_du("sf: E-O-F", dp, uio);
  515. return 0;
  516. }
  517. /*
  518. * Look up a name in a shortform directory structure, replace the inode number.
  519. */
  520. int
  521. xfs_dir_shortform_replace(xfs_da_args_t *args)
  522. {
  523. xfs_dir_shortform_t *sf;
  524. xfs_dir_sf_entry_t *sfe;
  525. xfs_inode_t *dp;
  526. int i;
  527. dp = args->dp;
  528. ASSERT(dp->i_df.if_flags & XFS_IFINLINE);
  529. /*
  530. * Catch the case where the conversion from shortform to leaf
  531. * failed part way through.
  532. */
  533. if (dp->i_d.di_size < sizeof(xfs_dir_sf_hdr_t)) {
  534. ASSERT(XFS_FORCED_SHUTDOWN(dp->i_mount));
  535. return XFS_ERROR(EIO);
  536. }
  537. ASSERT(dp->i_df.if_bytes == dp->i_d.di_size);
  538. ASSERT(dp->i_df.if_u1.if_data != NULL);
  539. sf = (xfs_dir_shortform_t *)dp->i_df.if_u1.if_data;
  540. if (args->namelen == 2 &&
  541. args->name[0] == '.' && args->name[1] == '.') {
  542. /* XXX - replace assert? */
  543. XFS_DIR_SF_PUT_DIRINO(&args->inumber, &sf->hdr.parent);
  544. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
  545. return 0;
  546. }
  547. ASSERT(args->namelen != 1 || args->name[0] != '.');
  548. sfe = &sf->list[0];
  549. for (i = sf->hdr.count-1; i >= 0; i--) {
  550. if (sfe->namelen == args->namelen &&
  551. sfe->name[0] == args->name[0] &&
  552. memcmp(args->name, sfe->name, args->namelen) == 0) {
  553. ASSERT(memcmp((char *)&args->inumber,
  554. (char *)&sfe->inumber, sizeof(xfs_ino_t)));
  555. XFS_DIR_SF_PUT_DIRINO(&args->inumber, &sfe->inumber);
  556. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_DDATA);
  557. return 0;
  558. }
  559. sfe = XFS_DIR_SF_NEXTENTRY(sfe);
  560. }
  561. ASSERT(args->oknoent);
  562. return XFS_ERROR(ENOENT);
  563. }
  564. /*
  565. * Convert a leaf directory to shortform structure
  566. */
  567. int
  568. xfs_dir_leaf_to_shortform(xfs_da_args_t *iargs)
  569. {
  570. xfs_dir_leafblock_t *leaf;
  571. xfs_dir_leaf_hdr_t *hdr;
  572. xfs_dir_leaf_entry_t *entry;
  573. xfs_dir_leaf_name_t *namest;
  574. xfs_da_args_t args;
  575. xfs_inode_t *dp;
  576. xfs_ino_t parent = 0;
  577. char *tmpbuffer;
  578. int retval, i;
  579. xfs_dabuf_t *bp;
  580. dp = iargs->dp;
  581. tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
  582. ASSERT(tmpbuffer != NULL);
  583. retval = xfs_da_read_buf(iargs->trans, iargs->dp, 0, -1, &bp,
  584. XFS_DATA_FORK);
  585. if (retval)
  586. goto out;
  587. ASSERT(bp != NULL);
  588. memcpy(tmpbuffer, bp->data, XFS_LBSIZE(dp->i_mount));
  589. leaf = (xfs_dir_leafblock_t *)tmpbuffer;
  590. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  591. memset(bp->data, 0, XFS_LBSIZE(dp->i_mount));
  592. /*
  593. * Find and special case the parent inode number
  594. */
  595. hdr = &leaf->hdr;
  596. entry = &leaf->entries[0];
  597. for (i = be16_to_cpu(hdr->count)-1; i >= 0; entry++, i--) {
  598. namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, be16_to_cpu(entry->nameidx));
  599. if ((entry->namelen == 2) &&
  600. (namest->name[0] == '.') &&
  601. (namest->name[1] == '.')) {
  602. XFS_DIR_SF_GET_DIRINO(&namest->inumber, &parent);
  603. entry->nameidx = 0;
  604. } else if ((entry->namelen == 1) && (namest->name[0] == '.')) {
  605. entry->nameidx = 0;
  606. }
  607. }
  608. retval = xfs_da_shrink_inode(iargs, 0, bp);
  609. if (retval)
  610. goto out;
  611. retval = xfs_dir_shortform_create(iargs, parent);
  612. if (retval)
  613. goto out;
  614. /*
  615. * Copy the rest of the filenames
  616. */
  617. entry = &leaf->entries[0];
  618. args.dp = dp;
  619. args.firstblock = iargs->firstblock;
  620. args.flist = iargs->flist;
  621. args.total = iargs->total;
  622. args.whichfork = XFS_DATA_FORK;
  623. args.trans = iargs->trans;
  624. args.justcheck = 0;
  625. args.addname = args.oknoent = 1;
  626. for (i = 0; i < be16_to_cpu(hdr->count); entry++, i++) {
  627. if (!entry->nameidx)
  628. continue;
  629. namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, be16_to_cpu(entry->nameidx));
  630. args.name = (char *)(namest->name);
  631. args.namelen = entry->namelen;
  632. args.hashval = be32_to_cpu(entry->hashval);
  633. XFS_DIR_SF_GET_DIRINO(&namest->inumber, &args.inumber);
  634. xfs_dir_shortform_addname(&args);
  635. }
  636. out:
  637. kmem_free(tmpbuffer, XFS_LBSIZE(dp->i_mount));
  638. return retval;
  639. }
  640. /*
  641. * Convert from using a single leaf to a root node and a leaf.
  642. */
  643. int
  644. xfs_dir_leaf_to_node(xfs_da_args_t *args)
  645. {
  646. xfs_dir_leafblock_t *leaf;
  647. xfs_da_intnode_t *node;
  648. xfs_inode_t *dp;
  649. xfs_dabuf_t *bp1, *bp2;
  650. xfs_dablk_t blkno;
  651. int retval;
  652. dp = args->dp;
  653. retval = xfs_da_grow_inode(args, &blkno);
  654. ASSERT(blkno == 1);
  655. if (retval)
  656. return retval;
  657. retval = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
  658. XFS_DATA_FORK);
  659. if (retval)
  660. return retval;
  661. ASSERT(bp1 != NULL);
  662. retval = xfs_da_get_buf(args->trans, args->dp, 1, -1, &bp2,
  663. XFS_DATA_FORK);
  664. if (retval) {
  665. xfs_da_buf_done(bp1);
  666. return retval;
  667. }
  668. ASSERT(bp2 != NULL);
  669. memcpy(bp2->data, bp1->data, XFS_LBSIZE(dp->i_mount));
  670. xfs_da_buf_done(bp1);
  671. xfs_da_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
  672. /*
  673. * Set up the new root node.
  674. */
  675. retval = xfs_da_node_create(args, 0, 1, &bp1, XFS_DATA_FORK);
  676. if (retval) {
  677. xfs_da_buf_done(bp2);
  678. return retval;
  679. }
  680. node = bp1->data;
  681. leaf = bp2->data;
  682. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  683. node->btree[0].hashval = leaf->entries[be16_to_cpu(leaf->hdr.count)-1].hashval;
  684. xfs_da_buf_done(bp2);
  685. node->btree[0].before = cpu_to_be32(blkno);
  686. node->hdr.count = cpu_to_be16(1);
  687. xfs_da_log_buf(args->trans, bp1,
  688. XFS_DA_LOGRANGE(node, &node->btree[0], sizeof(node->btree[0])));
  689. xfs_da_buf_done(bp1);
  690. return retval;
  691. }
  692. /*========================================================================
  693. * Routines used for growing the Btree.
  694. *========================================================================*/
  695. /*
  696. * Create the initial contents of a leaf directory
  697. * or a leaf in a node directory.
  698. */
  699. STATIC int
  700. xfs_dir_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
  701. {
  702. xfs_dir_leafblock_t *leaf;
  703. xfs_dir_leaf_hdr_t *hdr;
  704. xfs_inode_t *dp;
  705. xfs_dabuf_t *bp;
  706. int retval;
  707. dp = args->dp;
  708. ASSERT(dp != NULL);
  709. retval = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp, XFS_DATA_FORK);
  710. if (retval)
  711. return retval;
  712. ASSERT(bp != NULL);
  713. leaf = bp->data;
  714. memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
  715. hdr = &leaf->hdr;
  716. hdr->info.magic = cpu_to_be16(XFS_DIR_LEAF_MAGIC);
  717. hdr->firstused = cpu_to_be16(XFS_LBSIZE(dp->i_mount));
  718. if (!hdr->firstused)
  719. hdr->firstused = cpu_to_be16(XFS_LBSIZE(dp->i_mount) - 1);
  720. hdr->freemap[0].base = cpu_to_be16(sizeof(xfs_dir_leaf_hdr_t));
  721. hdr->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr->firstused) -
  722. be16_to_cpu(hdr->freemap[0].base));
  723. xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
  724. *bpp = bp;
  725. return 0;
  726. }
  727. /*
  728. * Split the leaf node, rebalance, then add the new entry.
  729. */
  730. int
  731. xfs_dir_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
  732. xfs_da_state_blk_t *newblk)
  733. {
  734. xfs_dablk_t blkno;
  735. xfs_da_args_t *args;
  736. int error;
  737. /*
  738. * Allocate space for a new leaf node.
  739. */
  740. args = state->args;
  741. ASSERT(args != NULL);
  742. ASSERT(oldblk->magic == XFS_DIR_LEAF_MAGIC);
  743. error = xfs_da_grow_inode(args, &blkno);
  744. if (error)
  745. return error;
  746. error = xfs_dir_leaf_create(args, blkno, &newblk->bp);
  747. if (error)
  748. return error;
  749. newblk->blkno = blkno;
  750. newblk->magic = XFS_DIR_LEAF_MAGIC;
  751. /*
  752. * Rebalance the entries across the two leaves.
  753. */
  754. xfs_dir_leaf_rebalance(state, oldblk, newblk);
  755. error = xfs_da_blk_link(state, oldblk, newblk);
  756. if (error)
  757. return error;
  758. /*
  759. * Insert the new entry in the correct block.
  760. */
  761. if (state->inleaf) {
  762. error = xfs_dir_leaf_add(oldblk->bp, args, oldblk->index);
  763. } else {
  764. error = xfs_dir_leaf_add(newblk->bp, args, newblk->index);
  765. }
  766. /*
  767. * Update last hashval in each block since we added the name.
  768. */
  769. oldblk->hashval = xfs_dir_leaf_lasthash(oldblk->bp, NULL);
  770. newblk->hashval = xfs_dir_leaf_lasthash(newblk->bp, NULL);
  771. return error;
  772. }
  773. /*
  774. * Add a name to the leaf directory structure.
  775. *
  776. * Must take into account fragmented leaves and leaves where spacemap has
  777. * lost some freespace information (ie: holes).
  778. */
  779. int
  780. xfs_dir_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index)
  781. {
  782. xfs_dir_leafblock_t *leaf;
  783. xfs_dir_leaf_hdr_t *hdr;
  784. xfs_dir_leaf_map_t *map;
  785. int tablesize, entsize, sum, i, tmp, error;
  786. leaf = bp->data;
  787. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  788. ASSERT((index >= 0) && (index <= be16_to_cpu(leaf->hdr.count)));
  789. hdr = &leaf->hdr;
  790. entsize = XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen);
  791. /*
  792. * Search through freemap for first-fit on new name length.
  793. * (may need to figure in size of entry struct too)
  794. */
  795. tablesize = (be16_to_cpu(hdr->count) + 1) *
  796. sizeof(xfs_dir_leaf_entry_t) + sizeof(xfs_dir_leaf_hdr_t);
  797. map = &hdr->freemap[XFS_DIR_LEAF_MAPSIZE-1];
  798. for (sum = 0, i = XFS_DIR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
  799. if (tablesize > be16_to_cpu(hdr->firstused)) {
  800. sum += be16_to_cpu(map->size);
  801. continue;
  802. }
  803. if (!map->size)
  804. continue; /* no space in this map */
  805. tmp = entsize;
  806. if (be16_to_cpu(map->base) < be16_to_cpu(hdr->firstused))
  807. tmp += (uint)sizeof(xfs_dir_leaf_entry_t);
  808. if (be16_to_cpu(map->size) >= tmp) {
  809. if (!args->justcheck)
  810. xfs_dir_leaf_add_work(bp, args, index, i);
  811. return 0;
  812. }
  813. sum += be16_to_cpu(map->size);
  814. }
  815. /*
  816. * If there are no holes in the address space of the block,
  817. * and we don't have enough freespace, then compaction will do us
  818. * no good and we should just give up.
  819. */
  820. if (!hdr->holes && (sum < entsize))
  821. return XFS_ERROR(ENOSPC);
  822. /*
  823. * Compact the entries to coalesce free space.
  824. * Pass the justcheck flag so the checking pass can return
  825. * an error, without changing anything, if it won't fit.
  826. */
  827. error = xfs_dir_leaf_compact(args->trans, bp,
  828. args->total == 0 ?
  829. entsize +
  830. (uint)sizeof(xfs_dir_leaf_entry_t) : 0,
  831. args->justcheck);
  832. if (error)
  833. return error;
  834. /*
  835. * After compaction, the block is guaranteed to have only one
  836. * free region, in freemap[0]. If it is not big enough, give up.
  837. */
  838. if (be16_to_cpu(hdr->freemap[0].size) <
  839. (entsize + (uint)sizeof(xfs_dir_leaf_entry_t)))
  840. return XFS_ERROR(ENOSPC);
  841. if (!args->justcheck)
  842. xfs_dir_leaf_add_work(bp, args, index, 0);
  843. return 0;
  844. }
  845. /*
  846. * Add a name to a leaf directory structure.
  847. */
  848. STATIC void
  849. xfs_dir_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int index,
  850. int mapindex)
  851. {
  852. xfs_dir_leafblock_t *leaf;
  853. xfs_dir_leaf_hdr_t *hdr;
  854. xfs_dir_leaf_entry_t *entry;
  855. xfs_dir_leaf_name_t *namest;
  856. xfs_dir_leaf_map_t *map;
  857. /* REFERENCED */
  858. xfs_mount_t *mp;
  859. int tmp, i;
  860. leaf = bp->data;
  861. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  862. hdr = &leaf->hdr;
  863. ASSERT((mapindex >= 0) && (mapindex < XFS_DIR_LEAF_MAPSIZE));
  864. ASSERT((index >= 0) && (index <= be16_to_cpu(hdr->count)));
  865. /*
  866. * Force open some space in the entry array and fill it in.
  867. */
  868. entry = &leaf->entries[index];
  869. if (index < be16_to_cpu(hdr->count)) {
  870. tmp = be16_to_cpu(hdr->count) - index;
  871. tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
  872. memmove(entry + 1, entry, tmp);
  873. xfs_da_log_buf(args->trans, bp,
  874. XFS_DA_LOGRANGE(leaf, entry, tmp + (uint)sizeof(*entry)));
  875. }
  876. be16_add(&hdr->count, 1);
  877. /*
  878. * Allocate space for the new string (at the end of the run).
  879. */
  880. map = &hdr->freemap[mapindex];
  881. mp = args->trans->t_mountp;
  882. ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
  883. ASSERT(be16_to_cpu(map->size) >= XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen));
  884. ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
  885. be16_add(&map->size, -(XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen)));
  886. entry->nameidx = cpu_to_be16(be16_to_cpu(map->base) +
  887. be16_to_cpu(map->size));
  888. entry->hashval = cpu_to_be32(args->hashval);
  889. entry->namelen = args->namelen;
  890. xfs_da_log_buf(args->trans, bp,
  891. XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
  892. /*
  893. * Copy the string and inode number into the new space.
  894. */
  895. namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, be16_to_cpu(entry->nameidx));
  896. XFS_DIR_SF_PUT_DIRINO(&args->inumber, &namest->inumber);
  897. memcpy(namest->name, args->name, args->namelen);
  898. xfs_da_log_buf(args->trans, bp,
  899. XFS_DA_LOGRANGE(leaf, namest, XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry)));
  900. /*
  901. * Update the control info for this leaf node
  902. */
  903. if (be16_to_cpu(entry->nameidx) < be16_to_cpu(hdr->firstused))
  904. hdr->firstused = entry->nameidx;
  905. ASSERT(be16_to_cpu(hdr->firstused) >=
  906. ((be16_to_cpu(hdr->count)*sizeof(*entry))+sizeof(*hdr)));
  907. tmp = (be16_to_cpu(hdr->count)-1) * (uint)sizeof(xfs_dir_leaf_entry_t)
  908. + (uint)sizeof(xfs_dir_leaf_hdr_t);
  909. map = &hdr->freemap[0];
  910. for (i = 0; i < XFS_DIR_LEAF_MAPSIZE; map++, i++) {
  911. if (be16_to_cpu(map->base) == tmp) {
  912. int entry_size = sizeof(xfs_dir_leaf_entry_t);
  913. be16_add(&map->base, entry_size);
  914. be16_add(&map->size, -entry_size);
  915. }
  916. }
  917. be16_add(&hdr->namebytes, args->namelen);
  918. xfs_da_log_buf(args->trans, bp,
  919. XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
  920. }
  921. /*
  922. * Garbage collect a leaf directory block by copying it to a new buffer.
  923. */
  924. STATIC int
  925. xfs_dir_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp, int musthave,
  926. int justcheck)
  927. {
  928. xfs_dir_leafblock_t *leaf_s, *leaf_d;
  929. xfs_dir_leaf_hdr_t *hdr_s, *hdr_d;
  930. xfs_mount_t *mp;
  931. char *tmpbuffer;
  932. char *tmpbuffer2=NULL;
  933. int rval;
  934. int lbsize;
  935. mp = trans->t_mountp;
  936. lbsize = XFS_LBSIZE(mp);
  937. tmpbuffer = kmem_alloc(lbsize, KM_SLEEP);
  938. ASSERT(tmpbuffer != NULL);
  939. memcpy(tmpbuffer, bp->data, lbsize);
  940. /*
  941. * Make a second copy in case xfs_dir_leaf_moveents()
  942. * below destroys the original.
  943. */
  944. if (musthave || justcheck) {
  945. tmpbuffer2 = kmem_alloc(lbsize, KM_SLEEP);
  946. memcpy(tmpbuffer2, bp->data, lbsize);
  947. }
  948. memset(bp->data, 0, lbsize);
  949. /*
  950. * Copy basic information
  951. */
  952. leaf_s = (xfs_dir_leafblock_t *)tmpbuffer;
  953. leaf_d = bp->data;
  954. hdr_s = &leaf_s->hdr;
  955. hdr_d = &leaf_d->hdr;
  956. hdr_d->info = hdr_s->info; /* struct copy */
  957. hdr_d->firstused = cpu_to_be16(lbsize);
  958. if (!hdr_d->firstused)
  959. hdr_d->firstused = cpu_to_be16(lbsize - 1);
  960. hdr_d->namebytes = 0;
  961. hdr_d->count = 0;
  962. hdr_d->holes = 0;
  963. hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_dir_leaf_hdr_t));
  964. hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused) -
  965. be16_to_cpu(hdr_d->freemap[0].base));
  966. /*
  967. * Copy all entry's in the same (sorted) order,
  968. * but allocate filenames packed and in sequence.
  969. * This changes the source (leaf_s) as well.
  970. */
  971. xfs_dir_leaf_moveents(leaf_s, 0, leaf_d, 0, be16_to_cpu(hdr_s->count), mp);
  972. if (musthave && be16_to_cpu(hdr_d->freemap[0].size) < musthave)
  973. rval = XFS_ERROR(ENOSPC);
  974. else
  975. rval = 0;
  976. if (justcheck || rval == ENOSPC) {
  977. ASSERT(tmpbuffer2);
  978. memcpy(bp->data, tmpbuffer2, lbsize);
  979. } else {
  980. xfs_da_log_buf(trans, bp, 0, lbsize - 1);
  981. }
  982. kmem_free(tmpbuffer, lbsize);
  983. if (musthave || justcheck)
  984. kmem_free(tmpbuffer2, lbsize);
  985. return rval;
  986. }
  987. /*
  988. * Redistribute the directory entries between two leaf nodes,
  989. * taking into account the size of the new entry.
  990. *
  991. * NOTE: if new block is empty, then it will get the upper half of old block.
  992. */
  993. STATIC void
  994. xfs_dir_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
  995. xfs_da_state_blk_t *blk2)
  996. {
  997. xfs_da_state_blk_t *tmp_blk;
  998. xfs_dir_leafblock_t *leaf1, *leaf2;
  999. xfs_dir_leaf_hdr_t *hdr1, *hdr2;
  1000. int count, totallen, max, space, swap;
  1001. /*
  1002. * Set up environment.
  1003. */
  1004. ASSERT(blk1->magic == XFS_DIR_LEAF_MAGIC);
  1005. ASSERT(blk2->magic == XFS_DIR_LEAF_MAGIC);
  1006. leaf1 = blk1->bp->data;
  1007. leaf2 = blk2->bp->data;
  1008. ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1009. ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1010. /*
  1011. * Check ordering of blocks, reverse if it makes things simpler.
  1012. */
  1013. swap = 0;
  1014. if (xfs_dir_leaf_order(blk1->bp, blk2->bp)) {
  1015. tmp_blk = blk1;
  1016. blk1 = blk2;
  1017. blk2 = tmp_blk;
  1018. leaf1 = blk1->bp->data;
  1019. leaf2 = blk2->bp->data;
  1020. swap = 1;
  1021. }
  1022. hdr1 = &leaf1->hdr;
  1023. hdr2 = &leaf2->hdr;
  1024. /*
  1025. * Examine entries until we reduce the absolute difference in
  1026. * byte usage between the two blocks to a minimum. Then get
  1027. * the direction to copy and the number of elements to move.
  1028. */
  1029. state->inleaf = xfs_dir_leaf_figure_balance(state, blk1, blk2,
  1030. &count, &totallen);
  1031. if (swap)
  1032. state->inleaf = !state->inleaf;
  1033. /*
  1034. * Move any entries required from leaf to leaf:
  1035. */
  1036. if (count < be16_to_cpu(hdr1->count)) {
  1037. /*
  1038. * Figure the total bytes to be added to the destination leaf.
  1039. */
  1040. count = be16_to_cpu(hdr1->count) - count; /* number entries being moved */
  1041. space = be16_to_cpu(hdr1->namebytes) - totallen;
  1042. space += count * ((uint)sizeof(xfs_dir_leaf_name_t)-1);
  1043. space += count * (uint)sizeof(xfs_dir_leaf_entry_t);
  1044. /*
  1045. * leaf2 is the destination, compact it if it looks tight.
  1046. */
  1047. max = be16_to_cpu(hdr2->firstused) - (uint)sizeof(xfs_dir_leaf_hdr_t);
  1048. max -= be16_to_cpu(hdr2->count) * (uint)sizeof(xfs_dir_leaf_entry_t);
  1049. if (space > max) {
  1050. xfs_dir_leaf_compact(state->args->trans, blk2->bp,
  1051. 0, 0);
  1052. }
  1053. /*
  1054. * Move high entries from leaf1 to low end of leaf2.
  1055. */
  1056. xfs_dir_leaf_moveents(leaf1, be16_to_cpu(hdr1->count) - count,
  1057. leaf2, 0, count, state->mp);
  1058. xfs_da_log_buf(state->args->trans, blk1->bp, 0,
  1059. state->blocksize-1);
  1060. xfs_da_log_buf(state->args->trans, blk2->bp, 0,
  1061. state->blocksize-1);
  1062. } else if (count > be16_to_cpu(hdr1->count)) {
  1063. /*
  1064. * Figure the total bytes to be added to the destination leaf.
  1065. */
  1066. count -= be16_to_cpu(hdr1->count); /* number entries being moved */
  1067. space = totallen - be16_to_cpu(hdr1->namebytes);
  1068. space += count * ((uint)sizeof(xfs_dir_leaf_name_t)-1);
  1069. space += count * (uint)sizeof(xfs_dir_leaf_entry_t);
  1070. /*
  1071. * leaf1 is the destination, compact it if it looks tight.
  1072. */
  1073. max = be16_to_cpu(hdr1->firstused) - (uint)sizeof(xfs_dir_leaf_hdr_t);
  1074. max -= be16_to_cpu(hdr1->count) * (uint)sizeof(xfs_dir_leaf_entry_t);
  1075. if (space > max) {
  1076. xfs_dir_leaf_compact(state->args->trans, blk1->bp,
  1077. 0, 0);
  1078. }
  1079. /*
  1080. * Move low entries from leaf2 to high end of leaf1.
  1081. */
  1082. xfs_dir_leaf_moveents(leaf2, 0, leaf1, be16_to_cpu(hdr1->count),
  1083. count, state->mp);
  1084. xfs_da_log_buf(state->args->trans, blk1->bp, 0,
  1085. state->blocksize-1);
  1086. xfs_da_log_buf(state->args->trans, blk2->bp, 0,
  1087. state->blocksize-1);
  1088. }
  1089. /*
  1090. * Copy out last hashval in each block for B-tree code.
  1091. */
  1092. blk1->hashval = be32_to_cpu(leaf1->entries[
  1093. be16_to_cpu(leaf1->hdr.count)-1].hashval);
  1094. blk2->hashval = be32_to_cpu(leaf2->entries[
  1095. be16_to_cpu(leaf2->hdr.count)-1].hashval);
  1096. /*
  1097. * Adjust the expected index for insertion.
  1098. * GROT: this doesn't work unless blk2 was originally empty.
  1099. */
  1100. if (!state->inleaf) {
  1101. blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
  1102. }
  1103. }
  1104. /*
  1105. * Examine entries until we reduce the absolute difference in
  1106. * byte usage between the two blocks to a minimum.
  1107. * GROT: Is this really necessary? With other than a 512 byte blocksize,
  1108. * GROT: there will always be enough room in either block for a new entry.
  1109. * GROT: Do a double-split for this case?
  1110. */
  1111. STATIC int
  1112. xfs_dir_leaf_figure_balance(xfs_da_state_t *state,
  1113. xfs_da_state_blk_t *blk1,
  1114. xfs_da_state_blk_t *blk2,
  1115. int *countarg, int *namebytesarg)
  1116. {
  1117. xfs_dir_leafblock_t *leaf1, *leaf2;
  1118. xfs_dir_leaf_hdr_t *hdr1, *hdr2;
  1119. xfs_dir_leaf_entry_t *entry;
  1120. int count, max, totallen, half;
  1121. int lastdelta, foundit, tmp;
  1122. /*
  1123. * Set up environment.
  1124. */
  1125. leaf1 = blk1->bp->data;
  1126. leaf2 = blk2->bp->data;
  1127. hdr1 = &leaf1->hdr;
  1128. hdr2 = &leaf2->hdr;
  1129. foundit = 0;
  1130. totallen = 0;
  1131. /*
  1132. * Examine entries until we reduce the absolute difference in
  1133. * byte usage between the two blocks to a minimum.
  1134. */
  1135. max = be16_to_cpu(hdr1->count) + be16_to_cpu(hdr2->count);
  1136. half = (max+1) * (uint)(sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1);
  1137. half += be16_to_cpu(hdr1->namebytes) + be16_to_cpu(hdr2->namebytes) +
  1138. state->args->namelen;
  1139. half /= 2;
  1140. lastdelta = state->blocksize;
  1141. entry = &leaf1->entries[0];
  1142. for (count = 0; count < max; entry++, count++) {
  1143. #define XFS_DIR_ABS(A) (((A) < 0) ? -(A) : (A))
  1144. /*
  1145. * The new entry is in the first block, account for it.
  1146. */
  1147. if (count == blk1->index) {
  1148. tmp = totallen + (uint)sizeof(*entry)
  1149. + XFS_DIR_LEAF_ENTSIZE_BYNAME(state->args->namelen);
  1150. if (XFS_DIR_ABS(half - tmp) > lastdelta)
  1151. break;
  1152. lastdelta = XFS_DIR_ABS(half - tmp);
  1153. totallen = tmp;
  1154. foundit = 1;
  1155. }
  1156. /*
  1157. * Wrap around into the second block if necessary.
  1158. */
  1159. if (count == be16_to_cpu(hdr1->count)) {
  1160. leaf1 = leaf2;
  1161. entry = &leaf1->entries[0];
  1162. }
  1163. /*
  1164. * Figure out if next leaf entry would be too much.
  1165. */
  1166. tmp = totallen + (uint)sizeof(*entry)
  1167. + XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry);
  1168. if (XFS_DIR_ABS(half - tmp) > lastdelta)
  1169. break;
  1170. lastdelta = XFS_DIR_ABS(half - tmp);
  1171. totallen = tmp;
  1172. #undef XFS_DIR_ABS
  1173. }
  1174. /*
  1175. * Calculate the number of namebytes that will end up in lower block.
  1176. * If new entry not in lower block, fix up the count.
  1177. */
  1178. totallen -=
  1179. count * (uint)(sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1);
  1180. if (foundit) {
  1181. totallen -= (sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1) +
  1182. state->args->namelen;
  1183. }
  1184. *countarg = count;
  1185. *namebytesarg = totallen;
  1186. return foundit;
  1187. }
  1188. /*========================================================================
  1189. * Routines used for shrinking the Btree.
  1190. *========================================================================*/
  1191. /*
  1192. * Check a leaf block and its neighbors to see if the block should be
  1193. * collapsed into one or the other neighbor. Always keep the block
  1194. * with the smaller block number.
  1195. * If the current block is over 50% full, don't try to join it, return 0.
  1196. * If the block is empty, fill in the state structure and return 2.
  1197. * If it can be collapsed, fill in the state structure and return 1.
  1198. * If nothing can be done, return 0.
  1199. */
  1200. int
  1201. xfs_dir_leaf_toosmall(xfs_da_state_t *state, int *action)
  1202. {
  1203. xfs_dir_leafblock_t *leaf;
  1204. xfs_da_state_blk_t *blk;
  1205. xfs_da_blkinfo_t *info;
  1206. int count, bytes, forward, error, retval, i;
  1207. xfs_dablk_t blkno;
  1208. xfs_dabuf_t *bp;
  1209. /*
  1210. * Check for the degenerate case of the block being over 50% full.
  1211. * If so, it's not worth even looking to see if we might be able
  1212. * to coalesce with a sibling.
  1213. */
  1214. blk = &state->path.blk[ state->path.active-1 ];
  1215. info = blk->bp->data;
  1216. ASSERT(be16_to_cpu(info->magic) == XFS_DIR_LEAF_MAGIC);
  1217. leaf = (xfs_dir_leafblock_t *)info;
  1218. count = be16_to_cpu(leaf->hdr.count);
  1219. bytes = (uint)sizeof(xfs_dir_leaf_hdr_t) +
  1220. count * (uint)sizeof(xfs_dir_leaf_entry_t) +
  1221. count * ((uint)sizeof(xfs_dir_leaf_name_t)-1) +
  1222. be16_to_cpu(leaf->hdr.namebytes);
  1223. if (bytes > (state->blocksize >> 1)) {
  1224. *action = 0; /* blk over 50%, don't try to join */
  1225. return 0;
  1226. }
  1227. /*
  1228. * Check for the degenerate case of the block being empty.
  1229. * If the block is empty, we'll simply delete it, no need to
  1230. * coalesce it with a sibling block. We choose (arbitrarily)
  1231. * to merge with the forward block unless it is NULL.
  1232. */
  1233. if (count == 0) {
  1234. /*
  1235. * Make altpath point to the block we want to keep and
  1236. * path point to the block we want to drop (this one).
  1237. */
  1238. forward = (info->forw != 0);
  1239. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1240. error = xfs_da_path_shift(state, &state->altpath, forward,
  1241. 0, &retval);
  1242. if (error)
  1243. return error;
  1244. if (retval) {
  1245. *action = 0;
  1246. } else {
  1247. *action = 2;
  1248. }
  1249. return 0;
  1250. }
  1251. /*
  1252. * Examine each sibling block to see if we can coalesce with
  1253. * at least 25% free space to spare. We need to figure out
  1254. * whether to merge with the forward or the backward block.
  1255. * We prefer coalescing with the lower numbered sibling so as
  1256. * to shrink a directory over time.
  1257. */
  1258. forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back)); /* start with smaller blk num */
  1259. for (i = 0; i < 2; forward = !forward, i++) {
  1260. if (forward)
  1261. blkno = be32_to_cpu(info->forw);
  1262. else
  1263. blkno = be32_to_cpu(info->back);
  1264. if (blkno == 0)
  1265. continue;
  1266. error = xfs_da_read_buf(state->args->trans, state->args->dp,
  1267. blkno, -1, &bp,
  1268. XFS_DATA_FORK);
  1269. if (error)
  1270. return error;
  1271. ASSERT(bp != NULL);
  1272. leaf = (xfs_dir_leafblock_t *)info;
  1273. count = be16_to_cpu(leaf->hdr.count);
  1274. bytes = state->blocksize - (state->blocksize>>2);
  1275. bytes -= be16_to_cpu(leaf->hdr.namebytes);
  1276. leaf = bp->data;
  1277. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1278. count += be16_to_cpu(leaf->hdr.count);
  1279. bytes -= be16_to_cpu(leaf->hdr.namebytes);
  1280. bytes -= count * ((uint)sizeof(xfs_dir_leaf_name_t) - 1);
  1281. bytes -= count * (uint)sizeof(xfs_dir_leaf_entry_t);
  1282. bytes -= (uint)sizeof(xfs_dir_leaf_hdr_t);
  1283. if (bytes >= 0)
  1284. break; /* fits with at least 25% to spare */
  1285. xfs_da_brelse(state->args->trans, bp);
  1286. }
  1287. if (i >= 2) {
  1288. *action = 0;
  1289. return 0;
  1290. }
  1291. xfs_da_buf_done(bp);
  1292. /*
  1293. * Make altpath point to the block we want to keep (the lower
  1294. * numbered block) and path point to the block we want to drop.
  1295. */
  1296. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1297. if (blkno < blk->blkno) {
  1298. error = xfs_da_path_shift(state, &state->altpath, forward,
  1299. 0, &retval);
  1300. } else {
  1301. error = xfs_da_path_shift(state, &state->path, forward,
  1302. 0, &retval);
  1303. }
  1304. if (error)
  1305. return error;
  1306. if (retval) {
  1307. *action = 0;
  1308. } else {
  1309. *action = 1;
  1310. }
  1311. return 0;
  1312. }
  1313. /*
  1314. * Remove a name from the leaf directory structure.
  1315. *
  1316. * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
  1317. * If two leaves are 37% full, when combined they will leave 25% free.
  1318. */
  1319. int
  1320. xfs_dir_leaf_remove(xfs_trans_t *trans, xfs_dabuf_t *bp, int index)
  1321. {
  1322. xfs_dir_leafblock_t *leaf;
  1323. xfs_dir_leaf_hdr_t *hdr;
  1324. xfs_dir_leaf_map_t *map;
  1325. xfs_dir_leaf_entry_t *entry;
  1326. xfs_dir_leaf_name_t *namest;
  1327. int before, after, smallest, entsize;
  1328. int tablesize, tmp, i;
  1329. xfs_mount_t *mp;
  1330. leaf = bp->data;
  1331. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1332. hdr = &leaf->hdr;
  1333. mp = trans->t_mountp;
  1334. ASSERT(hdr->count && (be16_to_cpu(hdr->count) < (XFS_LBSIZE(mp)/8)));
  1335. ASSERT((index >= 0) && (index < be16_to_cpu(hdr->count)));
  1336. ASSERT(be16_to_cpu(hdr->firstused) >=
  1337. ((be16_to_cpu(hdr->count)*sizeof(*entry))+sizeof(*hdr)));
  1338. entry = &leaf->entries[index];
  1339. ASSERT(be16_to_cpu(entry->nameidx) >= be16_to_cpu(hdr->firstused));
  1340. ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
  1341. /*
  1342. * Scan through free region table:
  1343. * check for adjacency of free'd entry with an existing one,
  1344. * find smallest free region in case we need to replace it,
  1345. * adjust any map that borders the entry table,
  1346. */
  1347. tablesize = be16_to_cpu(hdr->count) * (uint)sizeof(xfs_dir_leaf_entry_t)
  1348. + (uint)sizeof(xfs_dir_leaf_hdr_t);
  1349. map = &hdr->freemap[0];
  1350. tmp = be16_to_cpu(map->size);
  1351. before = after = -1;
  1352. smallest = XFS_DIR_LEAF_MAPSIZE - 1;
  1353. entsize = XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry);
  1354. for (i = 0; i < XFS_DIR_LEAF_MAPSIZE; map++, i++) {
  1355. ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
  1356. ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
  1357. if (be16_to_cpu(map->base) == tablesize) {
  1358. int entry_size = sizeof(xfs_dir_leaf_entry_t);
  1359. be16_add(&map->base, -entry_size);
  1360. be16_add(&map->size, entry_size);
  1361. }
  1362. if ((be16_to_cpu(map->base) + be16_to_cpu(map->size)) ==
  1363. be16_to_cpu(entry->nameidx)) {
  1364. before = i;
  1365. } else if (be16_to_cpu(map->base) ==
  1366. (be16_to_cpu(entry->nameidx) + entsize)) {
  1367. after = i;
  1368. } else if (be16_to_cpu(map->size) < tmp) {
  1369. tmp = be16_to_cpu(map->size);
  1370. smallest = i;
  1371. }
  1372. }
  1373. /*
  1374. * Coalesce adjacent freemap regions,
  1375. * or replace the smallest region.
  1376. */
  1377. if ((before >= 0) || (after >= 0)) {
  1378. if ((before >= 0) && (after >= 0)) {
  1379. map = &hdr->freemap[before];
  1380. be16_add(&map->size, entsize);
  1381. be16_add(&map->size, be16_to_cpu(hdr->freemap[after].size));
  1382. hdr->freemap[after].base = 0;
  1383. hdr->freemap[after].size = 0;
  1384. } else if (before >= 0) {
  1385. map = &hdr->freemap[before];
  1386. be16_add(&map->size, entsize);
  1387. } else {
  1388. map = &hdr->freemap[after];
  1389. map->base = entry->nameidx;
  1390. be16_add(&map->size, entsize);
  1391. }
  1392. } else {
  1393. /*
  1394. * Replace smallest region (if it is smaller than free'd entry)
  1395. */
  1396. map = &hdr->freemap[smallest];
  1397. if (be16_to_cpu(map->size) < entsize) {
  1398. map->base = entry->nameidx;
  1399. map->size = cpu_to_be16(entsize);
  1400. }
  1401. }
  1402. /*
  1403. * Did we remove the first entry?
  1404. */
  1405. if (be16_to_cpu(entry->nameidx) == be16_to_cpu(hdr->firstused))
  1406. smallest = 1;
  1407. else
  1408. smallest = 0;
  1409. /*
  1410. * Compress the remaining entries and zero out the removed stuff.
  1411. */
  1412. namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, be16_to_cpu(entry->nameidx));
  1413. memset((char *)namest, 0, entsize);
  1414. xfs_da_log_buf(trans, bp, XFS_DA_LOGRANGE(leaf, namest, entsize));
  1415. be16_add(&hdr->namebytes, -(entry->namelen));
  1416. tmp = (be16_to_cpu(hdr->count) - index) * (uint)sizeof(xfs_dir_leaf_entry_t);
  1417. memmove(entry, entry + 1, tmp);
  1418. be16_add(&hdr->count, -1);
  1419. xfs_da_log_buf(trans, bp,
  1420. XFS_DA_LOGRANGE(leaf, entry, tmp + (uint)sizeof(*entry)));
  1421. entry = &leaf->entries[be16_to_cpu(hdr->count)];
  1422. memset((char *)entry, 0, sizeof(xfs_dir_leaf_entry_t));
  1423. /*
  1424. * If we removed the first entry, re-find the first used byte
  1425. * in the name area. Note that if the entry was the "firstused",
  1426. * then we don't have a "hole" in our block resulting from
  1427. * removing the name.
  1428. */
  1429. if (smallest) {
  1430. tmp = XFS_LBSIZE(mp);
  1431. entry = &leaf->entries[0];
  1432. for (i = be16_to_cpu(hdr->count)-1; i >= 0; entry++, i--) {
  1433. ASSERT(be16_to_cpu(entry->nameidx) >=
  1434. be16_to_cpu(hdr->firstused));
  1435. ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
  1436. if (be16_to_cpu(entry->nameidx) < tmp)
  1437. tmp = be16_to_cpu(entry->nameidx);
  1438. }
  1439. hdr->firstused = cpu_to_be16(tmp);
  1440. if (!hdr->firstused)
  1441. hdr->firstused = cpu_to_be16(tmp - 1);
  1442. } else {
  1443. hdr->holes = 1; /* mark as needing compaction */
  1444. }
  1445. xfs_da_log_buf(trans, bp, XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
  1446. /*
  1447. * Check if leaf is less than 50% full, caller may want to
  1448. * "join" the leaf with a sibling if so.
  1449. */
  1450. tmp = (uint)sizeof(xfs_dir_leaf_hdr_t);
  1451. tmp += be16_to_cpu(leaf->hdr.count) * (uint)sizeof(xfs_dir_leaf_entry_t);
  1452. tmp += be16_to_cpu(leaf->hdr.count) * ((uint)sizeof(xfs_dir_leaf_name_t) - 1);
  1453. tmp += be16_to_cpu(leaf->hdr.namebytes);
  1454. if (tmp < mp->m_dir_magicpct)
  1455. return 1; /* leaf is < 37% full */
  1456. return 0;
  1457. }
  1458. /*
  1459. * Move all the directory entries from drop_leaf into save_leaf.
  1460. */
  1461. void
  1462. xfs_dir_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
  1463. xfs_da_state_blk_t *save_blk)
  1464. {
  1465. xfs_dir_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
  1466. xfs_dir_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
  1467. xfs_mount_t *mp;
  1468. char *tmpbuffer;
  1469. /*
  1470. * Set up environment.
  1471. */
  1472. mp = state->mp;
  1473. ASSERT(drop_blk->magic == XFS_DIR_LEAF_MAGIC);
  1474. ASSERT(save_blk->magic == XFS_DIR_LEAF_MAGIC);
  1475. drop_leaf = drop_blk->bp->data;
  1476. save_leaf = save_blk->bp->data;
  1477. ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1478. ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1479. drop_hdr = &drop_leaf->hdr;
  1480. save_hdr = &save_leaf->hdr;
  1481. /*
  1482. * Save last hashval from dying block for later Btree fixup.
  1483. */
  1484. drop_blk->hashval = be32_to_cpu(drop_leaf->entries[
  1485. be16_to_cpu(drop_leaf->hdr.count)-1].hashval);
  1486. /*
  1487. * Check if we need a temp buffer, or can we do it in place.
  1488. * Note that we don't check "leaf" for holes because we will
  1489. * always be dropping it, toosmall() decided that for us already.
  1490. */
  1491. if (save_hdr->holes == 0) {
  1492. /*
  1493. * dest leaf has no holes, so we add there. May need
  1494. * to make some room in the entry array.
  1495. */
  1496. if (xfs_dir_leaf_order(save_blk->bp, drop_blk->bp)) {
  1497. xfs_dir_leaf_moveents(drop_leaf, 0, save_leaf, 0,
  1498. be16_to_cpu(drop_hdr->count), mp);
  1499. } else {
  1500. xfs_dir_leaf_moveents(drop_leaf, 0,
  1501. save_leaf, be16_to_cpu(save_hdr->count),
  1502. be16_to_cpu(drop_hdr->count), mp);
  1503. }
  1504. } else {
  1505. /*
  1506. * Destination has holes, so we make a temporary copy
  1507. * of the leaf and add them both to that.
  1508. */
  1509. tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
  1510. ASSERT(tmpbuffer != NULL);
  1511. memset(tmpbuffer, 0, state->blocksize);
  1512. tmp_leaf = (xfs_dir_leafblock_t *)tmpbuffer;
  1513. tmp_hdr = &tmp_leaf->hdr;
  1514. tmp_hdr->info = save_hdr->info; /* struct copy */
  1515. tmp_hdr->count = 0;
  1516. tmp_hdr->firstused = cpu_to_be16(state->blocksize);
  1517. if (!tmp_hdr->firstused)
  1518. tmp_hdr->firstused = cpu_to_be16(state->blocksize - 1);
  1519. tmp_hdr->namebytes = 0;
  1520. if (xfs_dir_leaf_order(save_blk->bp, drop_blk->bp)) {
  1521. xfs_dir_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
  1522. be16_to_cpu(drop_hdr->count), mp);
  1523. xfs_dir_leaf_moveents(save_leaf, 0,
  1524. tmp_leaf, be16_to_cpu(tmp_leaf->hdr.count),
  1525. be16_to_cpu(save_hdr->count), mp);
  1526. } else {
  1527. xfs_dir_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
  1528. be16_to_cpu(save_hdr->count), mp);
  1529. xfs_dir_leaf_moveents(drop_leaf, 0,
  1530. tmp_leaf, be16_to_cpu(tmp_leaf->hdr.count),
  1531. be16_to_cpu(drop_hdr->count), mp);
  1532. }
  1533. memcpy(save_leaf, tmp_leaf, state->blocksize);
  1534. kmem_free(tmpbuffer, state->blocksize);
  1535. }
  1536. xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
  1537. state->blocksize - 1);
  1538. /*
  1539. * Copy out last hashval in each block for B-tree code.
  1540. */
  1541. save_blk->hashval = be32_to_cpu(save_leaf->entries[
  1542. be16_to_cpu(save_leaf->hdr.count)-1].hashval);
  1543. }
  1544. /*========================================================================
  1545. * Routines used for finding things in the Btree.
  1546. *========================================================================*/
  1547. /*
  1548. * Look up a name in a leaf directory structure.
  1549. * This is the internal routine, it uses the caller's buffer.
  1550. *
  1551. * Note that duplicate keys are allowed, but only check within the
  1552. * current leaf node. The Btree code must check in adjacent leaf nodes.
  1553. *
  1554. * Return in *index the index into the entry[] array of either the found
  1555. * entry, or where the entry should have been (insert before that entry).
  1556. *
  1557. * Don't change the args->inumber unless we find the filename.
  1558. */
  1559. int
  1560. xfs_dir_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args, int *index)
  1561. {
  1562. xfs_dir_leafblock_t *leaf;
  1563. xfs_dir_leaf_entry_t *entry;
  1564. xfs_dir_leaf_name_t *namest;
  1565. int probe, span;
  1566. xfs_dahash_t hashval;
  1567. leaf = bp->data;
  1568. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1569. ASSERT(be16_to_cpu(leaf->hdr.count) < (XFS_LBSIZE(args->dp->i_mount)/8));
  1570. /*
  1571. * Binary search. (note: small blocks will skip this loop)
  1572. */
  1573. hashval = args->hashval;
  1574. probe = span = be16_to_cpu(leaf->hdr.count) / 2;
  1575. for (entry = &leaf->entries[probe]; span > 4;
  1576. entry = &leaf->entries[probe]) {
  1577. span /= 2;
  1578. if (be32_to_cpu(entry->hashval) < hashval)
  1579. probe += span;
  1580. else if (be32_to_cpu(entry->hashval) > hashval)
  1581. probe -= span;
  1582. else
  1583. break;
  1584. }
  1585. ASSERT((probe >= 0) && \
  1586. ((!leaf->hdr.count) || (probe < be16_to_cpu(leaf->hdr.count))));
  1587. ASSERT((span <= 4) || (be32_to_cpu(entry->hashval) == hashval));
  1588. /*
  1589. * Since we may have duplicate hashval's, find the first matching
  1590. * hashval in the leaf.
  1591. */
  1592. while ((probe > 0) && (be32_to_cpu(entry->hashval) >= hashval)) {
  1593. entry--;
  1594. probe--;
  1595. }
  1596. while ((probe < be16_to_cpu(leaf->hdr.count)) &&
  1597. (be32_to_cpu(entry->hashval) < hashval)) {
  1598. entry++;
  1599. probe++;
  1600. }
  1601. if ((probe == be16_to_cpu(leaf->hdr.count)) ||
  1602. (be32_to_cpu(entry->hashval) != hashval)) {
  1603. *index = probe;
  1604. ASSERT(args->oknoent);
  1605. return XFS_ERROR(ENOENT);
  1606. }
  1607. /*
  1608. * Duplicate keys may be present, so search all of them for a match.
  1609. */
  1610. while ((probe < be16_to_cpu(leaf->hdr.count)) &&
  1611. (be32_to_cpu(entry->hashval) == hashval)) {
  1612. namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, be16_to_cpu(entry->nameidx));
  1613. if (entry->namelen == args->namelen &&
  1614. namest->name[0] == args->name[0] &&
  1615. memcmp(args->name, namest->name, args->namelen) == 0) {
  1616. XFS_DIR_SF_GET_DIRINO(&namest->inumber, &args->inumber);
  1617. *index = probe;
  1618. return XFS_ERROR(EEXIST);
  1619. }
  1620. entry++;
  1621. probe++;
  1622. }
  1623. *index = probe;
  1624. ASSERT(probe == be16_to_cpu(leaf->hdr.count) || args->oknoent);
  1625. return XFS_ERROR(ENOENT);
  1626. }
  1627. /*========================================================================
  1628. * Utility routines.
  1629. *========================================================================*/
  1630. /*
  1631. * Move the indicated entries from one leaf to another.
  1632. * NOTE: this routine modifies both source and destination leaves.
  1633. */
  1634. /* ARGSUSED */
  1635. STATIC void
  1636. xfs_dir_leaf_moveents(xfs_dir_leafblock_t *leaf_s, int start_s,
  1637. xfs_dir_leafblock_t *leaf_d, int start_d,
  1638. int count, xfs_mount_t *mp)
  1639. {
  1640. xfs_dir_leaf_hdr_t *hdr_s, *hdr_d;
  1641. xfs_dir_leaf_entry_t *entry_s, *entry_d;
  1642. int tmp, i;
  1643. /*
  1644. * Check for nothing to do.
  1645. */
  1646. if (count == 0)
  1647. return;
  1648. /*
  1649. * Set up environment.
  1650. */
  1651. ASSERT(be16_to_cpu(leaf_s->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1652. ASSERT(be16_to_cpu(leaf_d->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1653. hdr_s = &leaf_s->hdr;
  1654. hdr_d = &leaf_d->hdr;
  1655. ASSERT(hdr_s->count && (be16_to_cpu(hdr_s->count) < (XFS_LBSIZE(mp)/8)));
  1656. ASSERT(be16_to_cpu(hdr_s->firstused) >=
  1657. ((be16_to_cpu(hdr_s->count)*sizeof(*entry_s))+sizeof(*hdr_s)));
  1658. ASSERT(be16_to_cpu(hdr_d->count) < (XFS_LBSIZE(mp)/8));
  1659. ASSERT(be16_to_cpu(hdr_d->firstused) >=
  1660. ((be16_to_cpu(hdr_d->count)*sizeof(*entry_d))+sizeof(*hdr_d)));
  1661. ASSERT(start_s < be16_to_cpu(hdr_s->count));
  1662. ASSERT(start_d <= be16_to_cpu(hdr_d->count));
  1663. ASSERT(count <= be16_to_cpu(hdr_s->count));
  1664. /*
  1665. * Move the entries in the destination leaf up to make a hole?
  1666. */
  1667. if (start_d < be16_to_cpu(hdr_d->count)) {
  1668. tmp = be16_to_cpu(hdr_d->count) - start_d;
  1669. tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
  1670. entry_s = &leaf_d->entries[start_d];
  1671. entry_d = &leaf_d->entries[start_d + count];
  1672. memcpy(entry_d, entry_s, tmp);
  1673. }
  1674. /*
  1675. * Copy all entry's in the same (sorted) order,
  1676. * but allocate filenames packed and in sequence.
  1677. */
  1678. entry_s = &leaf_s->entries[start_s];
  1679. entry_d = &leaf_d->entries[start_d];
  1680. for (i = 0; i < count; entry_s++, entry_d++, i++) {
  1681. ASSERT(be16_to_cpu(entry_s->nameidx) >=
  1682. be16_to_cpu(hdr_s->firstused));
  1683. tmp = XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry_s);
  1684. be16_add(&hdr_d->firstused, -(tmp));
  1685. entry_d->hashval = entry_s->hashval;
  1686. entry_d->nameidx = hdr_d->firstused;
  1687. entry_d->namelen = entry_s->namelen;
  1688. ASSERT(be16_to_cpu(entry_d->nameidx) + tmp <= XFS_LBSIZE(mp));
  1689. memcpy(XFS_DIR_LEAF_NAMESTRUCT(leaf_d, be16_to_cpu(entry_d->nameidx)),
  1690. XFS_DIR_LEAF_NAMESTRUCT(leaf_s, be16_to_cpu(entry_s->nameidx)), tmp);
  1691. ASSERT(be16_to_cpu(entry_s->nameidx) + tmp <= XFS_LBSIZE(mp));
  1692. memset((char *)XFS_DIR_LEAF_NAMESTRUCT(leaf_s,
  1693. be16_to_cpu(entry_s->nameidx)), 0, tmp);
  1694. be16_add(&hdr_s->namebytes, -(entry_d->namelen));
  1695. be16_add(&hdr_d->namebytes, entry_d->namelen);
  1696. be16_add(&hdr_s->count, -1);
  1697. be16_add(&hdr_d->count, +1);
  1698. tmp = be16_to_cpu(hdr_d->count) * (uint)sizeof(xfs_dir_leaf_entry_t)
  1699. + (uint)sizeof(xfs_dir_leaf_hdr_t);
  1700. ASSERT(be16_to_cpu(hdr_d->firstused) >= tmp);
  1701. }
  1702. /*
  1703. * Zero out the entries we just copied.
  1704. */
  1705. if (start_s == be16_to_cpu(hdr_s->count)) {
  1706. tmp = count * (uint)sizeof(xfs_dir_leaf_entry_t);
  1707. entry_s = &leaf_s->entries[start_s];
  1708. ASSERT((char *)entry_s + tmp <= (char *)leaf_s + XFS_LBSIZE(mp));
  1709. memset((char *)entry_s, 0, tmp);
  1710. } else {
  1711. /*
  1712. * Move the remaining entries down to fill the hole,
  1713. * then zero the entries at the top.
  1714. */
  1715. tmp = be16_to_cpu(hdr_s->count) - count;
  1716. tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
  1717. entry_s = &leaf_s->entries[start_s + count];
  1718. entry_d = &leaf_s->entries[start_s];
  1719. memcpy(entry_d, entry_s, tmp);
  1720. tmp = count * (uint)sizeof(xfs_dir_leaf_entry_t);
  1721. entry_s = &leaf_s->entries[be16_to_cpu(hdr_s->count)];
  1722. ASSERT((char *)entry_s + tmp <= (char *)leaf_s + XFS_LBSIZE(mp));
  1723. memset((char *)entry_s, 0, tmp);
  1724. }
  1725. /*
  1726. * Fill in the freemap information
  1727. */
  1728. hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_dir_leaf_hdr_t) +
  1729. be16_to_cpu(hdr_d->count) * sizeof(xfs_dir_leaf_entry_t));
  1730. hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused) -
  1731. be16_to_cpu(hdr_d->freemap[0].base));
  1732. hdr_d->freemap[1].base = 0;
  1733. hdr_d->freemap[1].size = 0;
  1734. hdr_d->freemap[2].base = 0;
  1735. hdr_d->freemap[2].size = 0;
  1736. hdr_s->holes = 1; /* leaf may not be compact */
  1737. }
  1738. /*
  1739. * Compare two leaf blocks "order".
  1740. */
  1741. int
  1742. xfs_dir_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
  1743. {
  1744. xfs_dir_leafblock_t *leaf1, *leaf2;
  1745. leaf1 = leaf1_bp->data;
  1746. leaf2 = leaf2_bp->data;
  1747. ASSERT((be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR_LEAF_MAGIC) &&
  1748. (be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR_LEAF_MAGIC));
  1749. if (leaf1->hdr.count && leaf2->hdr.count &&
  1750. ((be32_to_cpu(leaf2->entries[0].hashval) <
  1751. be32_to_cpu(leaf1->entries[0 ].hashval)) ||
  1752. (be32_to_cpu(leaf2->entries[
  1753. be16_to_cpu(leaf2->hdr.count)-1].hashval) <
  1754. be32_to_cpu(leaf1->entries[
  1755. be16_to_cpu(leaf1->hdr.count)-1].hashval)))) {
  1756. return 1;
  1757. }
  1758. return 0;
  1759. }
  1760. /*
  1761. * Pick up the last hashvalue from a leaf block.
  1762. */
  1763. xfs_dahash_t
  1764. xfs_dir_leaf_lasthash(xfs_dabuf_t *bp, int *count)
  1765. {
  1766. xfs_dir_leafblock_t *leaf;
  1767. leaf = bp->data;
  1768. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1769. if (count)
  1770. *count = be16_to_cpu(leaf->hdr.count);
  1771. if (!leaf->hdr.count)
  1772. return(0);
  1773. return be32_to_cpu(leaf->entries[be16_to_cpu(leaf->hdr.count)-1].hashval);
  1774. }
  1775. /*
  1776. * Copy out directory entries for getdents(), for leaf directories.
  1777. */
  1778. int
  1779. xfs_dir_leaf_getdents_int(
  1780. xfs_dabuf_t *bp,
  1781. xfs_inode_t *dp,
  1782. xfs_dablk_t bno,
  1783. uio_t *uio,
  1784. int *eobp,
  1785. xfs_dirent_t *dbp,
  1786. xfs_dir_put_t put,
  1787. xfs_daddr_t nextda)
  1788. {
  1789. xfs_dir_leafblock_t *leaf;
  1790. xfs_dir_leaf_entry_t *entry;
  1791. xfs_dir_leaf_name_t *namest;
  1792. int entno, want_entno, i, nextentno;
  1793. xfs_mount_t *mp;
  1794. xfs_dahash_t cookhash;
  1795. xfs_dahash_t nexthash = 0;
  1796. #if (BITS_PER_LONG == 32)
  1797. xfs_dahash_t lasthash = XFS_DA_MAXHASH;
  1798. #endif
  1799. xfs_dir_put_args_t p;
  1800. mp = dp->i_mount;
  1801. leaf = bp->data;
  1802. if (be16_to_cpu(leaf->hdr.info.magic) != XFS_DIR_LEAF_MAGIC) {
  1803. *eobp = 1;
  1804. return XFS_ERROR(ENOENT); /* XXX wrong code */
  1805. }
  1806. want_entno = XFS_DA_COOKIE_ENTRY(mp, uio->uio_offset);
  1807. cookhash = XFS_DA_COOKIE_HASH(mp, uio->uio_offset);
  1808. xfs_dir_trace_g_dul("leaf: start", dp, uio, leaf);
  1809. /*
  1810. * Re-find our place.
  1811. */
  1812. for (i = entno = 0, entry = &leaf->entries[0];
  1813. i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
  1814. namest = XFS_DIR_LEAF_NAMESTRUCT(leaf,
  1815. be16_to_cpu(entry->nameidx));
  1816. if (unlikely(
  1817. ((char *)namest < (char *)leaf) ||
  1818. ((char *)namest >= (char *)leaf + XFS_LBSIZE(mp)))) {
  1819. XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(1)",
  1820. XFS_ERRLEVEL_LOW, mp, leaf);
  1821. xfs_dir_trace_g_du("leaf: corrupted", dp, uio);
  1822. return XFS_ERROR(EFSCORRUPTED);
  1823. }
  1824. if (be32_to_cpu(entry->hashval) >= cookhash) {
  1825. if (entno < want_entno &&
  1826. be32_to_cpu(entry->hashval) == cookhash) {
  1827. /*
  1828. * Trying to get to a particular offset in a
  1829. * run of equal-hashval entries.
  1830. */
  1831. entno++;
  1832. } else if (want_entno > 0 && entno == want_entno &&
  1833. be32_to_cpu(entry->hashval) == cookhash) {
  1834. break;
  1835. } else {
  1836. entno = 0;
  1837. break;
  1838. }
  1839. }
  1840. }
  1841. if (i == be16_to_cpu(leaf->hdr.count)) {
  1842. xfs_dir_trace_g_du("leaf: hash not found", dp, uio);
  1843. if (!leaf->hdr.info.forw)
  1844. uio->uio_offset =
  1845. XFS_DA_MAKE_COOKIE(mp, 0, 0, XFS_DA_MAXHASH);
  1846. /*
  1847. * Don't set uio_offset if there's another block:
  1848. * the node code will be setting uio_offset anyway.
  1849. */
  1850. *eobp = 0;
  1851. return 0;
  1852. }
  1853. xfs_dir_trace_g_due("leaf: hash found", dp, uio, entry);
  1854. p.dbp = dbp;
  1855. p.put = put;
  1856. p.uio = uio;
  1857. /*
  1858. * We're synchronized, start copying entries out to the user.
  1859. */
  1860. for (; entno >= 0 && i < be16_to_cpu(leaf->hdr.count);
  1861. entry++, i++, (entno = nextentno)) {
  1862. int lastresid=0, retval;
  1863. xfs_dircook_t lastoffset;
  1864. xfs_dahash_t thishash;
  1865. /*
  1866. * Check for a damaged directory leaf block and pick up
  1867. * the inode number from this entry.
  1868. */
  1869. namest = XFS_DIR_LEAF_NAMESTRUCT(leaf,
  1870. be16_to_cpu(entry->nameidx));
  1871. if (unlikely(
  1872. ((char *)namest < (char *)leaf) ||
  1873. ((char *)namest >= (char *)leaf + XFS_LBSIZE(mp)))) {
  1874. XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(2)",
  1875. XFS_ERRLEVEL_LOW, mp, leaf);
  1876. xfs_dir_trace_g_du("leaf: corrupted", dp, uio);
  1877. return XFS_ERROR(EFSCORRUPTED);
  1878. }
  1879. xfs_dir_trace_g_duc("leaf: middle cookie ",
  1880. dp, uio, p.cook.o);
  1881. if (i < (be16_to_cpu(leaf->hdr.count) - 1)) {
  1882. nexthash = be32_to_cpu(entry[1].hashval);
  1883. if (nexthash == be32_to_cpu(entry->hashval))
  1884. nextentno = entno + 1;
  1885. else
  1886. nextentno = 0;
  1887. XFS_PUT_COOKIE(p.cook, mp, bno, nextentno, nexthash);
  1888. xfs_dir_trace_g_duc("leaf: middle cookie ",
  1889. dp, uio, p.cook.o);
  1890. } else if ((thishash = be32_to_cpu(leaf->hdr.info.forw))) {
  1891. xfs_dabuf_t *bp2;
  1892. xfs_dir_leafblock_t *leaf2;
  1893. ASSERT(nextda != -1);
  1894. retval = xfs_da_read_buf(dp->i_transp, dp, thishash,
  1895. nextda, &bp2, XFS_DATA_FORK);
  1896. if (retval)
  1897. return retval;
  1898. ASSERT(bp2 != NULL);
  1899. leaf2 = bp2->data;
  1900. if (unlikely(
  1901. (be16_to_cpu(leaf2->hdr.info.magic)
  1902. != XFS_DIR_LEAF_MAGIC)
  1903. || (be32_to_cpu(leaf2->hdr.info.back)
  1904. != bno))) { /* GROT */
  1905. XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(3)",
  1906. XFS_ERRLEVEL_LOW, mp,
  1907. leaf2);
  1908. xfs_da_brelse(dp->i_transp, bp2);
  1909. return XFS_ERROR(EFSCORRUPTED);
  1910. }
  1911. nexthash = be32_to_cpu(leaf2->entries[0].hashval);
  1912. nextentno = -1;
  1913. XFS_PUT_COOKIE(p.cook, mp, thishash, 0, nexthash);
  1914. xfs_da_brelse(dp->i_transp, bp2);
  1915. xfs_dir_trace_g_duc("leaf: next blk cookie",
  1916. dp, uio, p.cook.o);
  1917. } else {
  1918. nextentno = -1;
  1919. XFS_PUT_COOKIE(p.cook, mp, 0, 0, XFS_DA_MAXHASH);
  1920. }
  1921. /*
  1922. * Save off the cookie so we can fall back should the
  1923. * 'put' into the outgoing buffer fails. To handle a run
  1924. * of equal-hashvals, the off_t structure on 64bit
  1925. * builds has entno built into the cookie to ID the
  1926. * entry. On 32bit builds, we only have space for the
  1927. * hashval so we can't ID specific entries within a group
  1928. * of same hashval entries. For this, lastoffset is set
  1929. * to the first in the run of equal hashvals so we don't
  1930. * include any entries unless we can include all entries
  1931. * that share the same hashval. Hopefully the buffer
  1932. * provided is big enough to handle it (see pv763517).
  1933. */
  1934. thishash = be32_to_cpu(entry->hashval);
  1935. #if (BITS_PER_LONG == 32)
  1936. if (thishash != lasthash) {
  1937. XFS_PUT_COOKIE(lastoffset, mp, bno, entno, thishash);
  1938. lastresid = uio->uio_resid;
  1939. lasthash = thishash;
  1940. } else {
  1941. xfs_dir_trace_g_duc("leaf: DUP COOKIES, skipped",
  1942. dp, uio, p.cook.o);
  1943. }
  1944. #else
  1945. XFS_PUT_COOKIE(lastoffset, mp, bno, entno, thishash);
  1946. lastresid = uio->uio_resid;
  1947. #endif /* BITS_PER_LONG == 32 */
  1948. /*
  1949. * Put the current entry into the outgoing buffer. If we fail
  1950. * then restore the UIO to the first entry in the current
  1951. * run of equal-hashval entries (probably one 1 entry long).
  1952. */
  1953. p.ino = XFS_GET_DIR_INO8(namest->inumber);
  1954. #if XFS_BIG_INUMS
  1955. p.ino += mp->m_inoadd;
  1956. #endif
  1957. p.name = (char *)namest->name;
  1958. p.namelen = entry->namelen;
  1959. retval = p.put(&p);
  1960. if (!p.done) {
  1961. uio->uio_offset = lastoffset.o;
  1962. uio->uio_resid = lastresid;
  1963. *eobp = 1;
  1964. xfs_dir_trace_g_du("leaf: E-O-B", dp, uio);
  1965. return retval;
  1966. }
  1967. }
  1968. uio->uio_offset = p.cook.o;
  1969. *eobp = 0;
  1970. xfs_dir_trace_g_du("leaf: E-O-F", dp, uio);
  1971. return 0;
  1972. }
  1973. /*
  1974. * Format a dirent64 structure and copy it out the the user's buffer.
  1975. */
  1976. int
  1977. xfs_dir_put_dirent64_direct(xfs_dir_put_args_t *pa)
  1978. {
  1979. iovec_t *iovp;
  1980. int reclen, namelen;
  1981. xfs_dirent_t *idbp;
  1982. uio_t *uio;
  1983. namelen = pa->namelen;
  1984. reclen = DIRENTSIZE(namelen);
  1985. uio = pa->uio;
  1986. if (reclen > uio->uio_resid) {
  1987. pa->done = 0;
  1988. return 0;
  1989. }
  1990. iovp = uio->uio_iov;
  1991. idbp = (xfs_dirent_t *)iovp->iov_base;
  1992. iovp->iov_base = (char *)idbp + reclen;
  1993. iovp->iov_len -= reclen;
  1994. uio->uio_resid -= reclen;
  1995. idbp->d_reclen = reclen;
  1996. idbp->d_ino = pa->ino;
  1997. idbp->d_off = pa->cook.o;
  1998. idbp->d_name[namelen] = '\0';
  1999. pa->done = 1;
  2000. memcpy(idbp->d_name, pa->name, namelen);
  2001. return 0;
  2002. }
  2003. /*
  2004. * Format a dirent64 structure and copy it out the the user's buffer.
  2005. */
  2006. int
  2007. xfs_dir_put_dirent64_uio(xfs_dir_put_args_t *pa)
  2008. {
  2009. int retval, reclen, namelen;
  2010. xfs_dirent_t *idbp;
  2011. uio_t *uio;
  2012. namelen = pa->namelen;
  2013. reclen = DIRENTSIZE(namelen);
  2014. uio = pa->uio;
  2015. if (reclen > uio->uio_resid) {
  2016. pa->done = 0;
  2017. return 0;
  2018. }
  2019. idbp = pa->dbp;
  2020. idbp->d_reclen = reclen;
  2021. idbp->d_ino = pa->ino;
  2022. idbp->d_off = pa->cook.o;
  2023. idbp->d_name[namelen] = '\0';
  2024. memcpy(idbp->d_name, pa->name, namelen);
  2025. retval = uio_read((caddr_t)idbp, reclen, uio);
  2026. pa->done = (retval == 0);
  2027. return retval;
  2028. }