xfs_dir_leaf.c 62 KB

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