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