xfs_dir_leaf.c 65 KB

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