xfs_dir_leaf.c 64 KB

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