xfs_dir_leaf.c 65 KB

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