xfs_dir_leaf.c 65 KB

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