xfs_dir_leaf.c 65 KB

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