xfs_dir_leaf.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212
  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. INT_SET(node->btree[0].hashval, ARCH_CONVERT, INT_GET(leaf->entries[ INT_GET(leaf->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT));
  685. xfs_da_buf_done(bp2);
  686. INT_SET(node->btree[0].before, ARCH_CONVERT, blkno);
  687. INT_SET(node->hdr.count, ARCH_CONVERT, 1);
  688. xfs_da_log_buf(args->trans, bp1,
  689. XFS_DA_LOGRANGE(node, &node->btree[0], sizeof(node->btree[0])));
  690. xfs_da_buf_done(bp1);
  691. return retval;
  692. }
  693. /*========================================================================
  694. * Routines used for growing the Btree.
  695. *========================================================================*/
  696. /*
  697. * Create the initial contents of a leaf directory
  698. * or a leaf in a node directory.
  699. */
  700. STATIC int
  701. xfs_dir_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
  702. {
  703. xfs_dir_leafblock_t *leaf;
  704. xfs_dir_leaf_hdr_t *hdr;
  705. xfs_inode_t *dp;
  706. xfs_dabuf_t *bp;
  707. int retval;
  708. dp = args->dp;
  709. ASSERT(dp != NULL);
  710. retval = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp, XFS_DATA_FORK);
  711. if (retval)
  712. return retval;
  713. ASSERT(bp != NULL);
  714. leaf = bp->data;
  715. memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
  716. hdr = &leaf->hdr;
  717. hdr->info.magic = cpu_to_be16(XFS_DIR_LEAF_MAGIC);
  718. INT_SET(hdr->firstused, ARCH_CONVERT, XFS_LBSIZE(dp->i_mount));
  719. if (!hdr->firstused)
  720. INT_SET(hdr->firstused, ARCH_CONVERT, XFS_LBSIZE(dp->i_mount) - 1);
  721. INT_SET(hdr->freemap[0].base, ARCH_CONVERT, sizeof(xfs_dir_leaf_hdr_t));
  722. INT_SET(hdr->freemap[0].size, ARCH_CONVERT, INT_GET(hdr->firstused, ARCH_CONVERT) - INT_GET(hdr->freemap[0].base, ARCH_CONVERT));
  723. xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
  724. *bpp = bp;
  725. return 0;
  726. }
  727. /*
  728. * Split the leaf node, rebalance, then add the new entry.
  729. */
  730. int
  731. xfs_dir_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
  732. xfs_da_state_blk_t *newblk)
  733. {
  734. xfs_dablk_t blkno;
  735. xfs_da_args_t *args;
  736. int error;
  737. /*
  738. * Allocate space for a new leaf node.
  739. */
  740. args = state->args;
  741. ASSERT(args != NULL);
  742. ASSERT(oldblk->magic == XFS_DIR_LEAF_MAGIC);
  743. error = xfs_da_grow_inode(args, &blkno);
  744. if (error)
  745. return error;
  746. error = xfs_dir_leaf_create(args, blkno, &newblk->bp);
  747. if (error)
  748. return error;
  749. newblk->blkno = blkno;
  750. newblk->magic = XFS_DIR_LEAF_MAGIC;
  751. /*
  752. * Rebalance the entries across the two leaves.
  753. */
  754. xfs_dir_leaf_rebalance(state, oldblk, newblk);
  755. error = xfs_da_blk_link(state, oldblk, newblk);
  756. if (error)
  757. return error;
  758. /*
  759. * Insert the new entry in the correct block.
  760. */
  761. if (state->inleaf) {
  762. error = xfs_dir_leaf_add(oldblk->bp, args, oldblk->index);
  763. } else {
  764. error = xfs_dir_leaf_add(newblk->bp, args, newblk->index);
  765. }
  766. /*
  767. * Update last hashval in each block since we added the name.
  768. */
  769. oldblk->hashval = xfs_dir_leaf_lasthash(oldblk->bp, NULL);
  770. newblk->hashval = xfs_dir_leaf_lasthash(newblk->bp, NULL);
  771. return error;
  772. }
  773. /*
  774. * Add a name to the leaf directory structure.
  775. *
  776. * Must take into account fragmented leaves and leaves where spacemap has
  777. * lost some freespace information (ie: holes).
  778. */
  779. int
  780. xfs_dir_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index)
  781. {
  782. xfs_dir_leafblock_t *leaf;
  783. xfs_dir_leaf_hdr_t *hdr;
  784. xfs_dir_leaf_map_t *map;
  785. int tablesize, entsize, sum, i, tmp, error;
  786. leaf = bp->data;
  787. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  788. ASSERT((index >= 0) && (index <= INT_GET(leaf->hdr.count, ARCH_CONVERT)));
  789. hdr = &leaf->hdr;
  790. entsize = XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen);
  791. /*
  792. * Search through freemap for first-fit on new name length.
  793. * (may need to figure in size of entry struct too)
  794. */
  795. tablesize = (INT_GET(hdr->count, ARCH_CONVERT) + 1) * (uint)sizeof(xfs_dir_leaf_entry_t)
  796. + (uint)sizeof(xfs_dir_leaf_hdr_t);
  797. map = &hdr->freemap[XFS_DIR_LEAF_MAPSIZE-1];
  798. for (sum = 0, i = XFS_DIR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
  799. if (tablesize > INT_GET(hdr->firstused, ARCH_CONVERT)) {
  800. sum += INT_GET(map->size, ARCH_CONVERT);
  801. continue;
  802. }
  803. if (!map->size)
  804. continue; /* no space in this map */
  805. tmp = entsize;
  806. if (INT_GET(map->base, ARCH_CONVERT) < INT_GET(hdr->firstused, ARCH_CONVERT))
  807. tmp += (uint)sizeof(xfs_dir_leaf_entry_t);
  808. if (INT_GET(map->size, ARCH_CONVERT) >= tmp) {
  809. if (!args->justcheck)
  810. xfs_dir_leaf_add_work(bp, args, index, i);
  811. return 0;
  812. }
  813. sum += INT_GET(map->size, ARCH_CONVERT);
  814. }
  815. /*
  816. * If there are no holes in the address space of the block,
  817. * and we don't have enough freespace, then compaction will do us
  818. * no good and we should just give up.
  819. */
  820. if (!hdr->holes && (sum < entsize))
  821. return XFS_ERROR(ENOSPC);
  822. /*
  823. * Compact the entries to coalesce free space.
  824. * Pass the justcheck flag so the checking pass can return
  825. * an error, without changing anything, if it won't fit.
  826. */
  827. error = xfs_dir_leaf_compact(args->trans, bp,
  828. args->total == 0 ?
  829. entsize +
  830. (uint)sizeof(xfs_dir_leaf_entry_t) : 0,
  831. args->justcheck);
  832. if (error)
  833. return error;
  834. /*
  835. * After compaction, the block is guaranteed to have only one
  836. * free region, in freemap[0]. If it is not big enough, give up.
  837. */
  838. if (INT_GET(hdr->freemap[0].size, ARCH_CONVERT) <
  839. (entsize + (uint)sizeof(xfs_dir_leaf_entry_t)))
  840. return XFS_ERROR(ENOSPC);
  841. if (!args->justcheck)
  842. xfs_dir_leaf_add_work(bp, args, index, 0);
  843. return 0;
  844. }
  845. /*
  846. * Add a name to a leaf directory structure.
  847. */
  848. STATIC void
  849. xfs_dir_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int index,
  850. int mapindex)
  851. {
  852. xfs_dir_leafblock_t *leaf;
  853. xfs_dir_leaf_hdr_t *hdr;
  854. xfs_dir_leaf_entry_t *entry;
  855. xfs_dir_leaf_name_t *namest;
  856. xfs_dir_leaf_map_t *map;
  857. /* REFERENCED */
  858. xfs_mount_t *mp;
  859. int tmp, i;
  860. leaf = bp->data;
  861. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  862. hdr = &leaf->hdr;
  863. ASSERT((mapindex >= 0) && (mapindex < XFS_DIR_LEAF_MAPSIZE));
  864. ASSERT((index >= 0) && (index <= INT_GET(hdr->count, ARCH_CONVERT)));
  865. /*
  866. * Force open some space in the entry array and fill it in.
  867. */
  868. entry = &leaf->entries[index];
  869. if (index < INT_GET(hdr->count, ARCH_CONVERT)) {
  870. tmp = INT_GET(hdr->count, ARCH_CONVERT) - index;
  871. tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
  872. memmove(entry + 1, entry, tmp);
  873. xfs_da_log_buf(args->trans, bp,
  874. XFS_DA_LOGRANGE(leaf, entry, tmp + (uint)sizeof(*entry)));
  875. }
  876. INT_MOD(hdr->count, ARCH_CONVERT, +1);
  877. /*
  878. * Allocate space for the new string (at the end of the run).
  879. */
  880. map = &hdr->freemap[mapindex];
  881. mp = args->trans->t_mountp;
  882. ASSERT(INT_GET(map->base, ARCH_CONVERT) < XFS_LBSIZE(mp));
  883. ASSERT(INT_GET(map->size, ARCH_CONVERT) >= XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen));
  884. ASSERT(INT_GET(map->size, ARCH_CONVERT) < XFS_LBSIZE(mp));
  885. INT_MOD(map->size, ARCH_CONVERT, -(XFS_DIR_LEAF_ENTSIZE_BYNAME(args->namelen)));
  886. INT_SET(entry->nameidx, ARCH_CONVERT, INT_GET(map->base, ARCH_CONVERT) + INT_GET(map->size, ARCH_CONVERT));
  887. INT_SET(entry->hashval, ARCH_CONVERT, args->hashval);
  888. entry->namelen = args->namelen;
  889. xfs_da_log_buf(args->trans, bp,
  890. XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
  891. /*
  892. * Copy the string and inode number into the new space.
  893. */
  894. namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
  895. XFS_DIR_SF_PUT_DIRINO(&args->inumber, &namest->inumber);
  896. memcpy(namest->name, args->name, args->namelen);
  897. xfs_da_log_buf(args->trans, bp,
  898. XFS_DA_LOGRANGE(leaf, namest, XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry)));
  899. /*
  900. * Update the control info for this leaf node
  901. */
  902. if (INT_GET(entry->nameidx, ARCH_CONVERT) < INT_GET(hdr->firstused, ARCH_CONVERT))
  903. INT_COPY(hdr->firstused, entry->nameidx, ARCH_CONVERT);
  904. ASSERT(INT_GET(hdr->firstused, ARCH_CONVERT) >= ((INT_GET(hdr->count, ARCH_CONVERT)*sizeof(*entry))+sizeof(*hdr)));
  905. tmp = (INT_GET(hdr->count, ARCH_CONVERT)-1) * (uint)sizeof(xfs_dir_leaf_entry_t)
  906. + (uint)sizeof(xfs_dir_leaf_hdr_t);
  907. map = &hdr->freemap[0];
  908. for (i = 0; i < XFS_DIR_LEAF_MAPSIZE; map++, i++) {
  909. if (INT_GET(map->base, ARCH_CONVERT) == tmp) {
  910. INT_MOD(map->base, ARCH_CONVERT, (uint)sizeof(xfs_dir_leaf_entry_t));
  911. INT_MOD(map->size, ARCH_CONVERT, -((uint)sizeof(xfs_dir_leaf_entry_t)));
  912. }
  913. }
  914. INT_MOD(hdr->namebytes, ARCH_CONVERT, args->namelen);
  915. xfs_da_log_buf(args->trans, bp,
  916. XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
  917. }
  918. /*
  919. * Garbage collect a leaf directory block by copying it to a new buffer.
  920. */
  921. STATIC int
  922. xfs_dir_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp, int musthave,
  923. int justcheck)
  924. {
  925. xfs_dir_leafblock_t *leaf_s, *leaf_d;
  926. xfs_dir_leaf_hdr_t *hdr_s, *hdr_d;
  927. xfs_mount_t *mp;
  928. char *tmpbuffer;
  929. char *tmpbuffer2=NULL;
  930. int rval;
  931. int lbsize;
  932. mp = trans->t_mountp;
  933. lbsize = XFS_LBSIZE(mp);
  934. tmpbuffer = kmem_alloc(lbsize, KM_SLEEP);
  935. ASSERT(tmpbuffer != NULL);
  936. memcpy(tmpbuffer, bp->data, lbsize);
  937. /*
  938. * Make a second copy in case xfs_dir_leaf_moveents()
  939. * below destroys the original.
  940. */
  941. if (musthave || justcheck) {
  942. tmpbuffer2 = kmem_alloc(lbsize, KM_SLEEP);
  943. memcpy(tmpbuffer2, bp->data, lbsize);
  944. }
  945. memset(bp->data, 0, lbsize);
  946. /*
  947. * Copy basic information
  948. */
  949. leaf_s = (xfs_dir_leafblock_t *)tmpbuffer;
  950. leaf_d = bp->data;
  951. hdr_s = &leaf_s->hdr;
  952. hdr_d = &leaf_d->hdr;
  953. hdr_d->info = hdr_s->info; /* struct copy */
  954. INT_SET(hdr_d->firstused, ARCH_CONVERT, lbsize);
  955. if (!hdr_d->firstused)
  956. INT_SET(hdr_d->firstused, ARCH_CONVERT, lbsize - 1);
  957. hdr_d->namebytes = 0;
  958. hdr_d->count = 0;
  959. hdr_d->holes = 0;
  960. INT_SET(hdr_d->freemap[0].base, ARCH_CONVERT, sizeof(xfs_dir_leaf_hdr_t));
  961. INT_SET(hdr_d->freemap[0].size, ARCH_CONVERT, INT_GET(hdr_d->firstused, ARCH_CONVERT) - INT_GET(hdr_d->freemap[0].base, ARCH_CONVERT));
  962. /*
  963. * Copy all entry's in the same (sorted) order,
  964. * but allocate filenames packed and in sequence.
  965. * This changes the source (leaf_s) as well.
  966. */
  967. xfs_dir_leaf_moveents(leaf_s, 0, leaf_d, 0, (int)INT_GET(hdr_s->count, ARCH_CONVERT), mp);
  968. if (musthave && INT_GET(hdr_d->freemap[0].size, ARCH_CONVERT) < musthave)
  969. rval = XFS_ERROR(ENOSPC);
  970. else
  971. rval = 0;
  972. if (justcheck || rval == ENOSPC) {
  973. ASSERT(tmpbuffer2);
  974. memcpy(bp->data, tmpbuffer2, lbsize);
  975. } else {
  976. xfs_da_log_buf(trans, bp, 0, lbsize - 1);
  977. }
  978. kmem_free(tmpbuffer, lbsize);
  979. if (musthave || justcheck)
  980. kmem_free(tmpbuffer2, lbsize);
  981. return rval;
  982. }
  983. /*
  984. * Redistribute the directory entries between two leaf nodes,
  985. * taking into account the size of the new entry.
  986. *
  987. * NOTE: if new block is empty, then it will get the upper half of old block.
  988. */
  989. STATIC void
  990. xfs_dir_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
  991. xfs_da_state_blk_t *blk2)
  992. {
  993. xfs_da_state_blk_t *tmp_blk;
  994. xfs_dir_leafblock_t *leaf1, *leaf2;
  995. xfs_dir_leaf_hdr_t *hdr1, *hdr2;
  996. int count, totallen, max, space, swap;
  997. /*
  998. * Set up environment.
  999. */
  1000. ASSERT(blk1->magic == XFS_DIR_LEAF_MAGIC);
  1001. ASSERT(blk2->magic == XFS_DIR_LEAF_MAGIC);
  1002. leaf1 = blk1->bp->data;
  1003. leaf2 = blk2->bp->data;
  1004. ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1005. ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1006. /*
  1007. * Check ordering of blocks, reverse if it makes things simpler.
  1008. */
  1009. swap = 0;
  1010. if (xfs_dir_leaf_order(blk1->bp, blk2->bp)) {
  1011. tmp_blk = blk1;
  1012. blk1 = blk2;
  1013. blk2 = tmp_blk;
  1014. leaf1 = blk1->bp->data;
  1015. leaf2 = blk2->bp->data;
  1016. swap = 1;
  1017. }
  1018. hdr1 = &leaf1->hdr;
  1019. hdr2 = &leaf2->hdr;
  1020. /*
  1021. * Examine entries until we reduce the absolute difference in
  1022. * byte usage between the two blocks to a minimum. Then get
  1023. * the direction to copy and the number of elements to move.
  1024. */
  1025. state->inleaf = xfs_dir_leaf_figure_balance(state, blk1, blk2,
  1026. &count, &totallen);
  1027. if (swap)
  1028. state->inleaf = !state->inleaf;
  1029. /*
  1030. * Move any entries required from leaf to leaf:
  1031. */
  1032. if (count < INT_GET(hdr1->count, ARCH_CONVERT)) {
  1033. /*
  1034. * Figure the total bytes to be added to the destination leaf.
  1035. */
  1036. count = INT_GET(hdr1->count, ARCH_CONVERT) - count; /* number entries being moved */
  1037. space = INT_GET(hdr1->namebytes, ARCH_CONVERT) - totallen;
  1038. space += count * ((uint)sizeof(xfs_dir_leaf_name_t)-1);
  1039. space += count * (uint)sizeof(xfs_dir_leaf_entry_t);
  1040. /*
  1041. * leaf2 is the destination, compact it if it looks tight.
  1042. */
  1043. max = INT_GET(hdr2->firstused, ARCH_CONVERT) - (uint)sizeof(xfs_dir_leaf_hdr_t);
  1044. max -= INT_GET(hdr2->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t);
  1045. if (space > max) {
  1046. xfs_dir_leaf_compact(state->args->trans, blk2->bp,
  1047. 0, 0);
  1048. }
  1049. /*
  1050. * Move high entries from leaf1 to low end of leaf2.
  1051. */
  1052. xfs_dir_leaf_moveents(leaf1, INT_GET(hdr1->count, ARCH_CONVERT) - count,
  1053. leaf2, 0, count, state->mp);
  1054. xfs_da_log_buf(state->args->trans, blk1->bp, 0,
  1055. state->blocksize-1);
  1056. xfs_da_log_buf(state->args->trans, blk2->bp, 0,
  1057. state->blocksize-1);
  1058. } else if (count > INT_GET(hdr1->count, ARCH_CONVERT)) {
  1059. /*
  1060. * Figure the total bytes to be added to the destination leaf.
  1061. */
  1062. count -= INT_GET(hdr1->count, ARCH_CONVERT); /* number entries being moved */
  1063. space = totallen - INT_GET(hdr1->namebytes, ARCH_CONVERT);
  1064. space += count * ((uint)sizeof(xfs_dir_leaf_name_t)-1);
  1065. space += count * (uint)sizeof(xfs_dir_leaf_entry_t);
  1066. /*
  1067. * leaf1 is the destination, compact it if it looks tight.
  1068. */
  1069. max = INT_GET(hdr1->firstused, ARCH_CONVERT) - (uint)sizeof(xfs_dir_leaf_hdr_t);
  1070. max -= INT_GET(hdr1->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t);
  1071. if (space > max) {
  1072. xfs_dir_leaf_compact(state->args->trans, blk1->bp,
  1073. 0, 0);
  1074. }
  1075. /*
  1076. * Move low entries from leaf2 to high end of leaf1.
  1077. */
  1078. xfs_dir_leaf_moveents(leaf2, 0, leaf1, (int)INT_GET(hdr1->count, ARCH_CONVERT),
  1079. count, state->mp);
  1080. xfs_da_log_buf(state->args->trans, blk1->bp, 0,
  1081. state->blocksize-1);
  1082. xfs_da_log_buf(state->args->trans, blk2->bp, 0,
  1083. state->blocksize-1);
  1084. }
  1085. /*
  1086. * Copy out last hashval in each block for B-tree code.
  1087. */
  1088. blk1->hashval = INT_GET(leaf1->entries[ INT_GET(leaf1->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
  1089. blk2->hashval = INT_GET(leaf2->entries[ INT_GET(leaf2->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
  1090. /*
  1091. * Adjust the expected index for insertion.
  1092. * GROT: this doesn't work unless blk2 was originally empty.
  1093. */
  1094. if (!state->inleaf) {
  1095. blk2->index = blk1->index - INT_GET(leaf1->hdr.count, ARCH_CONVERT);
  1096. }
  1097. }
  1098. /*
  1099. * Examine entries until we reduce the absolute difference in
  1100. * byte usage between the two blocks to a minimum.
  1101. * GROT: Is this really necessary? With other than a 512 byte blocksize,
  1102. * GROT: there will always be enough room in either block for a new entry.
  1103. * GROT: Do a double-split for this case?
  1104. */
  1105. STATIC int
  1106. xfs_dir_leaf_figure_balance(xfs_da_state_t *state,
  1107. xfs_da_state_blk_t *blk1,
  1108. xfs_da_state_blk_t *blk2,
  1109. int *countarg, int *namebytesarg)
  1110. {
  1111. xfs_dir_leafblock_t *leaf1, *leaf2;
  1112. xfs_dir_leaf_hdr_t *hdr1, *hdr2;
  1113. xfs_dir_leaf_entry_t *entry;
  1114. int count, max, totallen, half;
  1115. int lastdelta, foundit, tmp;
  1116. /*
  1117. * Set up environment.
  1118. */
  1119. leaf1 = blk1->bp->data;
  1120. leaf2 = blk2->bp->data;
  1121. hdr1 = &leaf1->hdr;
  1122. hdr2 = &leaf2->hdr;
  1123. foundit = 0;
  1124. totallen = 0;
  1125. /*
  1126. * Examine entries until we reduce the absolute difference in
  1127. * byte usage between the two blocks to a minimum.
  1128. */
  1129. max = INT_GET(hdr1->count, ARCH_CONVERT) + INT_GET(hdr2->count, ARCH_CONVERT);
  1130. half = (max+1) * (uint)(sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1);
  1131. half += INT_GET(hdr1->namebytes, ARCH_CONVERT) + INT_GET(hdr2->namebytes, ARCH_CONVERT) + state->args->namelen;
  1132. half /= 2;
  1133. lastdelta = state->blocksize;
  1134. entry = &leaf1->entries[0];
  1135. for (count = 0; count < max; entry++, count++) {
  1136. #define XFS_DIR_ABS(A) (((A) < 0) ? -(A) : (A))
  1137. /*
  1138. * The new entry is in the first block, account for it.
  1139. */
  1140. if (count == blk1->index) {
  1141. tmp = totallen + (uint)sizeof(*entry)
  1142. + XFS_DIR_LEAF_ENTSIZE_BYNAME(state->args->namelen);
  1143. if (XFS_DIR_ABS(half - tmp) > lastdelta)
  1144. break;
  1145. lastdelta = XFS_DIR_ABS(half - tmp);
  1146. totallen = tmp;
  1147. foundit = 1;
  1148. }
  1149. /*
  1150. * Wrap around into the second block if necessary.
  1151. */
  1152. if (count == INT_GET(hdr1->count, ARCH_CONVERT)) {
  1153. leaf1 = leaf2;
  1154. entry = &leaf1->entries[0];
  1155. }
  1156. /*
  1157. * Figure out if next leaf entry would be too much.
  1158. */
  1159. tmp = totallen + (uint)sizeof(*entry)
  1160. + XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry);
  1161. if (XFS_DIR_ABS(half - tmp) > lastdelta)
  1162. break;
  1163. lastdelta = XFS_DIR_ABS(half - tmp);
  1164. totallen = tmp;
  1165. #undef XFS_DIR_ABS
  1166. }
  1167. /*
  1168. * Calculate the number of namebytes that will end up in lower block.
  1169. * If new entry not in lower block, fix up the count.
  1170. */
  1171. totallen -=
  1172. count * (uint)(sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1);
  1173. if (foundit) {
  1174. totallen -= (sizeof(*entry)+sizeof(xfs_dir_leaf_entry_t)-1) +
  1175. state->args->namelen;
  1176. }
  1177. *countarg = count;
  1178. *namebytesarg = totallen;
  1179. return foundit;
  1180. }
  1181. /*========================================================================
  1182. * Routines used for shrinking the Btree.
  1183. *========================================================================*/
  1184. /*
  1185. * Check a leaf block and its neighbors to see if the block should be
  1186. * collapsed into one or the other neighbor. Always keep the block
  1187. * with the smaller block number.
  1188. * If the current block is over 50% full, don't try to join it, return 0.
  1189. * If the block is empty, fill in the state structure and return 2.
  1190. * If it can be collapsed, fill in the state structure and return 1.
  1191. * If nothing can be done, return 0.
  1192. */
  1193. int
  1194. xfs_dir_leaf_toosmall(xfs_da_state_t *state, int *action)
  1195. {
  1196. xfs_dir_leafblock_t *leaf;
  1197. xfs_da_state_blk_t *blk;
  1198. xfs_da_blkinfo_t *info;
  1199. int count, bytes, forward, error, retval, i;
  1200. xfs_dablk_t blkno;
  1201. xfs_dabuf_t *bp;
  1202. /*
  1203. * Check for the degenerate case of the block being over 50% full.
  1204. * If so, it's not worth even looking to see if we might be able
  1205. * to coalesce with a sibling.
  1206. */
  1207. blk = &state->path.blk[ state->path.active-1 ];
  1208. info = blk->bp->data;
  1209. ASSERT(be16_to_cpu(info->magic) == XFS_DIR_LEAF_MAGIC);
  1210. leaf = (xfs_dir_leafblock_t *)info;
  1211. count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
  1212. bytes = (uint)sizeof(xfs_dir_leaf_hdr_t) +
  1213. count * (uint)sizeof(xfs_dir_leaf_entry_t) +
  1214. count * ((uint)sizeof(xfs_dir_leaf_name_t)-1) +
  1215. INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
  1216. if (bytes > (state->blocksize >> 1)) {
  1217. *action = 0; /* blk over 50%, don't try to join */
  1218. return 0;
  1219. }
  1220. /*
  1221. * Check for the degenerate case of the block being empty.
  1222. * If the block is empty, we'll simply delete it, no need to
  1223. * coalesce it with a sibling block. We choose (aribtrarily)
  1224. * to merge with the forward block unless it is NULL.
  1225. */
  1226. if (count == 0) {
  1227. /*
  1228. * Make altpath point to the block we want to keep and
  1229. * path point to the block we want to drop (this one).
  1230. */
  1231. forward = (info->forw != 0);
  1232. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1233. error = xfs_da_path_shift(state, &state->altpath, forward,
  1234. 0, &retval);
  1235. if (error)
  1236. return error;
  1237. if (retval) {
  1238. *action = 0;
  1239. } else {
  1240. *action = 2;
  1241. }
  1242. return 0;
  1243. }
  1244. /*
  1245. * Examine each sibling block to see if we can coalesce with
  1246. * at least 25% free space to spare. We need to figure out
  1247. * whether to merge with the forward or the backward block.
  1248. * We prefer coalescing with the lower numbered sibling so as
  1249. * to shrink a directory over time.
  1250. */
  1251. forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back)); /* start with smaller blk num */
  1252. for (i = 0; i < 2; forward = !forward, i++) {
  1253. if (forward)
  1254. blkno = be32_to_cpu(info->forw);
  1255. else
  1256. blkno = be32_to_cpu(info->back);
  1257. if (blkno == 0)
  1258. continue;
  1259. error = xfs_da_read_buf(state->args->trans, state->args->dp,
  1260. blkno, -1, &bp,
  1261. XFS_DATA_FORK);
  1262. if (error)
  1263. return error;
  1264. ASSERT(bp != NULL);
  1265. leaf = (xfs_dir_leafblock_t *)info;
  1266. count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
  1267. bytes = state->blocksize - (state->blocksize>>2);
  1268. bytes -= INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
  1269. leaf = bp->data;
  1270. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1271. count += INT_GET(leaf->hdr.count, ARCH_CONVERT);
  1272. bytes -= INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
  1273. bytes -= count * ((uint)sizeof(xfs_dir_leaf_name_t) - 1);
  1274. bytes -= count * (uint)sizeof(xfs_dir_leaf_entry_t);
  1275. bytes -= (uint)sizeof(xfs_dir_leaf_hdr_t);
  1276. if (bytes >= 0)
  1277. break; /* fits with at least 25% to spare */
  1278. xfs_da_brelse(state->args->trans, bp);
  1279. }
  1280. if (i >= 2) {
  1281. *action = 0;
  1282. return 0;
  1283. }
  1284. xfs_da_buf_done(bp);
  1285. /*
  1286. * Make altpath point to the block we want to keep (the lower
  1287. * numbered block) and path point to the block we want to drop.
  1288. */
  1289. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1290. if (blkno < blk->blkno) {
  1291. error = xfs_da_path_shift(state, &state->altpath, forward,
  1292. 0, &retval);
  1293. } else {
  1294. error = xfs_da_path_shift(state, &state->path, forward,
  1295. 0, &retval);
  1296. }
  1297. if (error)
  1298. return error;
  1299. if (retval) {
  1300. *action = 0;
  1301. } else {
  1302. *action = 1;
  1303. }
  1304. return 0;
  1305. }
  1306. /*
  1307. * Remove a name from the leaf directory structure.
  1308. *
  1309. * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
  1310. * If two leaves are 37% full, when combined they will leave 25% free.
  1311. */
  1312. int
  1313. xfs_dir_leaf_remove(xfs_trans_t *trans, xfs_dabuf_t *bp, int index)
  1314. {
  1315. xfs_dir_leafblock_t *leaf;
  1316. xfs_dir_leaf_hdr_t *hdr;
  1317. xfs_dir_leaf_map_t *map;
  1318. xfs_dir_leaf_entry_t *entry;
  1319. xfs_dir_leaf_name_t *namest;
  1320. int before, after, smallest, entsize;
  1321. int tablesize, tmp, i;
  1322. xfs_mount_t *mp;
  1323. leaf = bp->data;
  1324. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1325. hdr = &leaf->hdr;
  1326. mp = trans->t_mountp;
  1327. ASSERT((INT_GET(hdr->count, ARCH_CONVERT) > 0) && (INT_GET(hdr->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8)));
  1328. ASSERT((index >= 0) && (index < INT_GET(hdr->count, ARCH_CONVERT)));
  1329. ASSERT(INT_GET(hdr->firstused, ARCH_CONVERT) >= ((INT_GET(hdr->count, ARCH_CONVERT)*sizeof(*entry))+sizeof(*hdr)));
  1330. entry = &leaf->entries[index];
  1331. ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) >= INT_GET(hdr->firstused, ARCH_CONVERT));
  1332. ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) < XFS_LBSIZE(mp));
  1333. /*
  1334. * Scan through free region table:
  1335. * check for adjacency of free'd entry with an existing one,
  1336. * find smallest free region in case we need to replace it,
  1337. * adjust any map that borders the entry table,
  1338. */
  1339. tablesize = INT_GET(hdr->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t)
  1340. + (uint)sizeof(xfs_dir_leaf_hdr_t);
  1341. map = &hdr->freemap[0];
  1342. tmp = INT_GET(map->size, ARCH_CONVERT);
  1343. before = after = -1;
  1344. smallest = XFS_DIR_LEAF_MAPSIZE - 1;
  1345. entsize = XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry);
  1346. for (i = 0; i < XFS_DIR_LEAF_MAPSIZE; map++, i++) {
  1347. ASSERT(INT_GET(map->base, ARCH_CONVERT) < XFS_LBSIZE(mp));
  1348. ASSERT(INT_GET(map->size, ARCH_CONVERT) < XFS_LBSIZE(mp));
  1349. if (INT_GET(map->base, ARCH_CONVERT) == tablesize) {
  1350. INT_MOD(map->base, ARCH_CONVERT, -((uint)sizeof(xfs_dir_leaf_entry_t)));
  1351. INT_MOD(map->size, ARCH_CONVERT, (uint)sizeof(xfs_dir_leaf_entry_t));
  1352. }
  1353. if ((INT_GET(map->base, ARCH_CONVERT) + INT_GET(map->size, ARCH_CONVERT)) == INT_GET(entry->nameidx, ARCH_CONVERT)) {
  1354. before = i;
  1355. } else if (INT_GET(map->base, ARCH_CONVERT) == (INT_GET(entry->nameidx, ARCH_CONVERT) + entsize)) {
  1356. after = i;
  1357. } else if (INT_GET(map->size, ARCH_CONVERT) < tmp) {
  1358. tmp = INT_GET(map->size, ARCH_CONVERT);
  1359. smallest = i;
  1360. }
  1361. }
  1362. /*
  1363. * Coalesce adjacent freemap regions,
  1364. * or replace the smallest region.
  1365. */
  1366. if ((before >= 0) || (after >= 0)) {
  1367. if ((before >= 0) && (after >= 0)) {
  1368. map = &hdr->freemap[before];
  1369. INT_MOD(map->size, ARCH_CONVERT, entsize);
  1370. INT_MOD(map->size, ARCH_CONVERT, INT_GET(hdr->freemap[after].size, ARCH_CONVERT));
  1371. hdr->freemap[after].base = 0;
  1372. hdr->freemap[after].size = 0;
  1373. } else if (before >= 0) {
  1374. map = &hdr->freemap[before];
  1375. INT_MOD(map->size, ARCH_CONVERT, entsize);
  1376. } else {
  1377. map = &hdr->freemap[after];
  1378. INT_COPY(map->base, entry->nameidx, ARCH_CONVERT);
  1379. INT_MOD(map->size, ARCH_CONVERT, entsize);
  1380. }
  1381. } else {
  1382. /*
  1383. * Replace smallest region (if it is smaller than free'd entry)
  1384. */
  1385. map = &hdr->freemap[smallest];
  1386. if (INT_GET(map->size, ARCH_CONVERT) < entsize) {
  1387. INT_COPY(map->base, entry->nameidx, ARCH_CONVERT);
  1388. INT_SET(map->size, ARCH_CONVERT, entsize);
  1389. }
  1390. }
  1391. /*
  1392. * Did we remove the first entry?
  1393. */
  1394. if (INT_GET(entry->nameidx, ARCH_CONVERT) == INT_GET(hdr->firstused, ARCH_CONVERT))
  1395. smallest = 1;
  1396. else
  1397. smallest = 0;
  1398. /*
  1399. * Compress the remaining entries and zero out the removed stuff.
  1400. */
  1401. namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
  1402. memset((char *)namest, 0, entsize);
  1403. xfs_da_log_buf(trans, bp, XFS_DA_LOGRANGE(leaf, namest, entsize));
  1404. INT_MOD(hdr->namebytes, ARCH_CONVERT, -(entry->namelen));
  1405. tmp = (INT_GET(hdr->count, ARCH_CONVERT) - index) * (uint)sizeof(xfs_dir_leaf_entry_t);
  1406. memmove(entry, entry + 1, tmp);
  1407. INT_MOD(hdr->count, ARCH_CONVERT, -1);
  1408. xfs_da_log_buf(trans, bp,
  1409. XFS_DA_LOGRANGE(leaf, entry, tmp + (uint)sizeof(*entry)));
  1410. entry = &leaf->entries[INT_GET(hdr->count, ARCH_CONVERT)];
  1411. memset((char *)entry, 0, sizeof(xfs_dir_leaf_entry_t));
  1412. /*
  1413. * If we removed the first entry, re-find the first used byte
  1414. * in the name area. Note that if the entry was the "firstused",
  1415. * then we don't have a "hole" in our block resulting from
  1416. * removing the name.
  1417. */
  1418. if (smallest) {
  1419. tmp = XFS_LBSIZE(mp);
  1420. entry = &leaf->entries[0];
  1421. for (i = INT_GET(hdr->count, ARCH_CONVERT)-1; i >= 0; entry++, i--) {
  1422. ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) >= INT_GET(hdr->firstused, ARCH_CONVERT));
  1423. ASSERT(INT_GET(entry->nameidx, ARCH_CONVERT) < XFS_LBSIZE(mp));
  1424. if (INT_GET(entry->nameidx, ARCH_CONVERT) < tmp)
  1425. tmp = INT_GET(entry->nameidx, ARCH_CONVERT);
  1426. }
  1427. INT_SET(hdr->firstused, ARCH_CONVERT, tmp);
  1428. if (!hdr->firstused)
  1429. INT_SET(hdr->firstused, ARCH_CONVERT, tmp - 1);
  1430. } else {
  1431. hdr->holes = 1; /* mark as needing compaction */
  1432. }
  1433. xfs_da_log_buf(trans, bp, XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
  1434. /*
  1435. * Check if leaf is less than 50% full, caller may want to
  1436. * "join" the leaf with a sibling if so.
  1437. */
  1438. tmp = (uint)sizeof(xfs_dir_leaf_hdr_t);
  1439. tmp += INT_GET(leaf->hdr.count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t);
  1440. tmp += INT_GET(leaf->hdr.count, ARCH_CONVERT) * ((uint)sizeof(xfs_dir_leaf_name_t) - 1);
  1441. tmp += INT_GET(leaf->hdr.namebytes, ARCH_CONVERT);
  1442. if (tmp < mp->m_dir_magicpct)
  1443. return 1; /* leaf is < 37% full */
  1444. return 0;
  1445. }
  1446. /*
  1447. * Move all the directory entries from drop_leaf into save_leaf.
  1448. */
  1449. void
  1450. xfs_dir_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
  1451. xfs_da_state_blk_t *save_blk)
  1452. {
  1453. xfs_dir_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
  1454. xfs_dir_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
  1455. xfs_mount_t *mp;
  1456. char *tmpbuffer;
  1457. /*
  1458. * Set up environment.
  1459. */
  1460. mp = state->mp;
  1461. ASSERT(drop_blk->magic == XFS_DIR_LEAF_MAGIC);
  1462. ASSERT(save_blk->magic == XFS_DIR_LEAF_MAGIC);
  1463. drop_leaf = drop_blk->bp->data;
  1464. save_leaf = save_blk->bp->data;
  1465. ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1466. ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1467. drop_hdr = &drop_leaf->hdr;
  1468. save_hdr = &save_leaf->hdr;
  1469. /*
  1470. * Save last hashval from dying block for later Btree fixup.
  1471. */
  1472. drop_blk->hashval = INT_GET(drop_leaf->entries[ drop_leaf->hdr.count-1 ].hashval, ARCH_CONVERT);
  1473. /*
  1474. * Check if we need a temp buffer, or can we do it in place.
  1475. * Note that we don't check "leaf" for holes because we will
  1476. * always be dropping it, toosmall() decided that for us already.
  1477. */
  1478. if (save_hdr->holes == 0) {
  1479. /*
  1480. * dest leaf has no holes, so we add there. May need
  1481. * to make some room in the entry array.
  1482. */
  1483. if (xfs_dir_leaf_order(save_blk->bp, drop_blk->bp)) {
  1484. xfs_dir_leaf_moveents(drop_leaf, 0, save_leaf, 0,
  1485. (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
  1486. } else {
  1487. xfs_dir_leaf_moveents(drop_leaf, 0,
  1488. save_leaf, INT_GET(save_hdr->count, ARCH_CONVERT),
  1489. (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
  1490. }
  1491. } else {
  1492. /*
  1493. * Destination has holes, so we make a temporary copy
  1494. * of the leaf and add them both to that.
  1495. */
  1496. tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
  1497. ASSERT(tmpbuffer != NULL);
  1498. memset(tmpbuffer, 0, state->blocksize);
  1499. tmp_leaf = (xfs_dir_leafblock_t *)tmpbuffer;
  1500. tmp_hdr = &tmp_leaf->hdr;
  1501. tmp_hdr->info = save_hdr->info; /* struct copy */
  1502. tmp_hdr->count = 0;
  1503. INT_SET(tmp_hdr->firstused, ARCH_CONVERT, state->blocksize);
  1504. if (!tmp_hdr->firstused)
  1505. INT_SET(tmp_hdr->firstused, ARCH_CONVERT, state->blocksize - 1);
  1506. tmp_hdr->namebytes = 0;
  1507. if (xfs_dir_leaf_order(save_blk->bp, drop_blk->bp)) {
  1508. xfs_dir_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
  1509. (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
  1510. xfs_dir_leaf_moveents(save_leaf, 0,
  1511. tmp_leaf, INT_GET(tmp_leaf->hdr.count, ARCH_CONVERT),
  1512. (int)INT_GET(save_hdr->count, ARCH_CONVERT), mp);
  1513. } else {
  1514. xfs_dir_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
  1515. (int)INT_GET(save_hdr->count, ARCH_CONVERT), mp);
  1516. xfs_dir_leaf_moveents(drop_leaf, 0,
  1517. tmp_leaf, INT_GET(tmp_leaf->hdr.count, ARCH_CONVERT),
  1518. (int)INT_GET(drop_hdr->count, ARCH_CONVERT), mp);
  1519. }
  1520. memcpy(save_leaf, tmp_leaf, state->blocksize);
  1521. kmem_free(tmpbuffer, state->blocksize);
  1522. }
  1523. xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
  1524. state->blocksize - 1);
  1525. /*
  1526. * Copy out last hashval in each block for B-tree code.
  1527. */
  1528. save_blk->hashval = INT_GET(save_leaf->entries[ INT_GET(save_leaf->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
  1529. }
  1530. /*========================================================================
  1531. * Routines used for finding things in the Btree.
  1532. *========================================================================*/
  1533. /*
  1534. * Look up a name in a leaf directory structure.
  1535. * This is the internal routine, it uses the caller's buffer.
  1536. *
  1537. * Note that duplicate keys are allowed, but only check within the
  1538. * current leaf node. The Btree code must check in adjacent leaf nodes.
  1539. *
  1540. * Return in *index the index into the entry[] array of either the found
  1541. * entry, or where the entry should have been (insert before that entry).
  1542. *
  1543. * Don't change the args->inumber unless we find the filename.
  1544. */
  1545. int
  1546. xfs_dir_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args, int *index)
  1547. {
  1548. xfs_dir_leafblock_t *leaf;
  1549. xfs_dir_leaf_entry_t *entry;
  1550. xfs_dir_leaf_name_t *namest;
  1551. int probe, span;
  1552. xfs_dahash_t hashval;
  1553. leaf = bp->data;
  1554. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1555. ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT) < (XFS_LBSIZE(args->dp->i_mount)/8));
  1556. /*
  1557. * Binary search. (note: small blocks will skip this loop)
  1558. */
  1559. hashval = args->hashval;
  1560. probe = span = INT_GET(leaf->hdr.count, ARCH_CONVERT) / 2;
  1561. for (entry = &leaf->entries[probe]; span > 4;
  1562. entry = &leaf->entries[probe]) {
  1563. span /= 2;
  1564. if (INT_GET(entry->hashval, ARCH_CONVERT) < hashval)
  1565. probe += span;
  1566. else if (INT_GET(entry->hashval, ARCH_CONVERT) > hashval)
  1567. probe -= span;
  1568. else
  1569. break;
  1570. }
  1571. ASSERT((probe >= 0) && \
  1572. ((!leaf->hdr.count) || (probe < INT_GET(leaf->hdr.count, ARCH_CONVERT))));
  1573. ASSERT((span <= 4) || (INT_GET(entry->hashval, ARCH_CONVERT) == hashval));
  1574. /*
  1575. * Since we may have duplicate hashval's, find the first matching
  1576. * hashval in the leaf.
  1577. */
  1578. while ((probe > 0) && (INT_GET(entry->hashval, ARCH_CONVERT) >= hashval)) {
  1579. entry--;
  1580. probe--;
  1581. }
  1582. while ((probe < INT_GET(leaf->hdr.count, ARCH_CONVERT)) && (INT_GET(entry->hashval, ARCH_CONVERT) < hashval)) {
  1583. entry++;
  1584. probe++;
  1585. }
  1586. if ((probe == INT_GET(leaf->hdr.count, ARCH_CONVERT)) || (INT_GET(entry->hashval, ARCH_CONVERT) != hashval)) {
  1587. *index = probe;
  1588. ASSERT(args->oknoent);
  1589. return XFS_ERROR(ENOENT);
  1590. }
  1591. /*
  1592. * Duplicate keys may be present, so search all of them for a match.
  1593. */
  1594. while ((probe < INT_GET(leaf->hdr.count, ARCH_CONVERT)) && (INT_GET(entry->hashval, ARCH_CONVERT) == hashval)) {
  1595. namest = XFS_DIR_LEAF_NAMESTRUCT(leaf, INT_GET(entry->nameidx, ARCH_CONVERT));
  1596. if (entry->namelen == args->namelen &&
  1597. namest->name[0] == args->name[0] &&
  1598. memcmp(args->name, namest->name, args->namelen) == 0) {
  1599. XFS_DIR_SF_GET_DIRINO(&namest->inumber, &args->inumber);
  1600. *index = probe;
  1601. return XFS_ERROR(EEXIST);
  1602. }
  1603. entry++;
  1604. probe++;
  1605. }
  1606. *index = probe;
  1607. ASSERT(probe == INT_GET(leaf->hdr.count, ARCH_CONVERT) || args->oknoent);
  1608. return XFS_ERROR(ENOENT);
  1609. }
  1610. /*========================================================================
  1611. * Utility routines.
  1612. *========================================================================*/
  1613. /*
  1614. * Move the indicated entries from one leaf to another.
  1615. * NOTE: this routine modifies both source and destination leaves.
  1616. */
  1617. /* ARGSUSED */
  1618. STATIC void
  1619. xfs_dir_leaf_moveents(xfs_dir_leafblock_t *leaf_s, int start_s,
  1620. xfs_dir_leafblock_t *leaf_d, int start_d,
  1621. int count, xfs_mount_t *mp)
  1622. {
  1623. xfs_dir_leaf_hdr_t *hdr_s, *hdr_d;
  1624. xfs_dir_leaf_entry_t *entry_s, *entry_d;
  1625. int tmp, i;
  1626. /*
  1627. * Check for nothing to do.
  1628. */
  1629. if (count == 0)
  1630. return;
  1631. /*
  1632. * Set up environment.
  1633. */
  1634. ASSERT(be16_to_cpu(leaf_s->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1635. ASSERT(be16_to_cpu(leaf_d->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1636. hdr_s = &leaf_s->hdr;
  1637. hdr_d = &leaf_d->hdr;
  1638. ASSERT((INT_GET(hdr_s->count, ARCH_CONVERT) > 0) && (INT_GET(hdr_s->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8)));
  1639. ASSERT(INT_GET(hdr_s->firstused, ARCH_CONVERT) >=
  1640. ((INT_GET(hdr_s->count, ARCH_CONVERT)*sizeof(*entry_s))+sizeof(*hdr_s)));
  1641. ASSERT(INT_GET(hdr_d->count, ARCH_CONVERT) < (XFS_LBSIZE(mp)/8));
  1642. ASSERT(INT_GET(hdr_d->firstused, ARCH_CONVERT) >=
  1643. ((INT_GET(hdr_d->count, ARCH_CONVERT)*sizeof(*entry_d))+sizeof(*hdr_d)));
  1644. ASSERT(start_s < INT_GET(hdr_s->count, ARCH_CONVERT));
  1645. ASSERT(start_d <= INT_GET(hdr_d->count, ARCH_CONVERT));
  1646. ASSERT(count <= INT_GET(hdr_s->count, ARCH_CONVERT));
  1647. /*
  1648. * Move the entries in the destination leaf up to make a hole?
  1649. */
  1650. if (start_d < INT_GET(hdr_d->count, ARCH_CONVERT)) {
  1651. tmp = INT_GET(hdr_d->count, ARCH_CONVERT) - start_d;
  1652. tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
  1653. entry_s = &leaf_d->entries[start_d];
  1654. entry_d = &leaf_d->entries[start_d + count];
  1655. memcpy(entry_d, entry_s, tmp);
  1656. }
  1657. /*
  1658. * Copy all entry's in the same (sorted) order,
  1659. * but allocate filenames packed and in sequence.
  1660. */
  1661. entry_s = &leaf_s->entries[start_s];
  1662. entry_d = &leaf_d->entries[start_d];
  1663. for (i = 0; i < count; entry_s++, entry_d++, i++) {
  1664. ASSERT(INT_GET(entry_s->nameidx, ARCH_CONVERT) >= INT_GET(hdr_s->firstused, ARCH_CONVERT));
  1665. tmp = XFS_DIR_LEAF_ENTSIZE_BYENTRY(entry_s);
  1666. INT_MOD(hdr_d->firstused, ARCH_CONVERT, -(tmp));
  1667. entry_d->hashval = entry_s->hashval; /* INT_: direct copy */
  1668. INT_COPY(entry_d->nameidx, hdr_d->firstused, ARCH_CONVERT);
  1669. entry_d->namelen = entry_s->namelen;
  1670. ASSERT(INT_GET(entry_d->nameidx, ARCH_CONVERT) + tmp <= XFS_LBSIZE(mp));
  1671. memcpy(XFS_DIR_LEAF_NAMESTRUCT(leaf_d, INT_GET(entry_d->nameidx, ARCH_CONVERT)),
  1672. XFS_DIR_LEAF_NAMESTRUCT(leaf_s, INT_GET(entry_s->nameidx, ARCH_CONVERT)), tmp);
  1673. ASSERT(INT_GET(entry_s->nameidx, ARCH_CONVERT) + tmp <= XFS_LBSIZE(mp));
  1674. memset((char *)XFS_DIR_LEAF_NAMESTRUCT(leaf_s, INT_GET(entry_s->nameidx, ARCH_CONVERT)),
  1675. 0, tmp);
  1676. INT_MOD(hdr_s->namebytes, ARCH_CONVERT, -(entry_d->namelen));
  1677. INT_MOD(hdr_d->namebytes, ARCH_CONVERT, entry_d->namelen);
  1678. INT_MOD(hdr_s->count, ARCH_CONVERT, -1);
  1679. INT_MOD(hdr_d->count, ARCH_CONVERT, +1);
  1680. tmp = INT_GET(hdr_d->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t)
  1681. + (uint)sizeof(xfs_dir_leaf_hdr_t);
  1682. ASSERT(INT_GET(hdr_d->firstused, ARCH_CONVERT) >= tmp);
  1683. }
  1684. /*
  1685. * Zero out the entries we just copied.
  1686. */
  1687. if (start_s == INT_GET(hdr_s->count, ARCH_CONVERT)) {
  1688. tmp = count * (uint)sizeof(xfs_dir_leaf_entry_t);
  1689. entry_s = &leaf_s->entries[start_s];
  1690. ASSERT((char *)entry_s + tmp <= (char *)leaf_s + XFS_LBSIZE(mp));
  1691. memset((char *)entry_s, 0, tmp);
  1692. } else {
  1693. /*
  1694. * Move the remaining entries down to fill the hole,
  1695. * then zero the entries at the top.
  1696. */
  1697. tmp = INT_GET(hdr_s->count, ARCH_CONVERT) - count;
  1698. tmp *= (uint)sizeof(xfs_dir_leaf_entry_t);
  1699. entry_s = &leaf_s->entries[start_s + count];
  1700. entry_d = &leaf_s->entries[start_s];
  1701. memcpy(entry_d, entry_s, tmp);
  1702. tmp = count * (uint)sizeof(xfs_dir_leaf_entry_t);
  1703. entry_s = &leaf_s->entries[INT_GET(hdr_s->count, ARCH_CONVERT)];
  1704. ASSERT((char *)entry_s + tmp <= (char *)leaf_s + XFS_LBSIZE(mp));
  1705. memset((char *)entry_s, 0, tmp);
  1706. }
  1707. /*
  1708. * Fill in the freemap information
  1709. */
  1710. INT_SET(hdr_d->freemap[0].base, ARCH_CONVERT, (uint)sizeof(xfs_dir_leaf_hdr_t));
  1711. INT_MOD(hdr_d->freemap[0].base, ARCH_CONVERT, INT_GET(hdr_d->count, ARCH_CONVERT) * (uint)sizeof(xfs_dir_leaf_entry_t));
  1712. INT_SET(hdr_d->freemap[0].size, ARCH_CONVERT, INT_GET(hdr_d->firstused, ARCH_CONVERT) - INT_GET(hdr_d->freemap[0].base, ARCH_CONVERT));
  1713. INT_SET(hdr_d->freemap[1].base, ARCH_CONVERT, (hdr_d->freemap[2].base = 0));
  1714. INT_SET(hdr_d->freemap[1].size, ARCH_CONVERT, (hdr_d->freemap[2].size = 0));
  1715. hdr_s->holes = 1; /* leaf may not be compact */
  1716. }
  1717. /*
  1718. * Compare two leaf blocks "order".
  1719. */
  1720. int
  1721. xfs_dir_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
  1722. {
  1723. xfs_dir_leafblock_t *leaf1, *leaf2;
  1724. leaf1 = leaf1_bp->data;
  1725. leaf2 = leaf2_bp->data;
  1726. ASSERT((be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR_LEAF_MAGIC) &&
  1727. (be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR_LEAF_MAGIC));
  1728. if ((INT_GET(leaf1->hdr.count, ARCH_CONVERT) > 0) && (INT_GET(leaf2->hdr.count, ARCH_CONVERT) > 0) &&
  1729. ((INT_GET(leaf2->entries[ 0 ].hashval, ARCH_CONVERT) <
  1730. INT_GET(leaf1->entries[ 0 ].hashval, ARCH_CONVERT)) ||
  1731. (INT_GET(leaf2->entries[ INT_GET(leaf2->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT) <
  1732. INT_GET(leaf1->entries[ INT_GET(leaf1->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT)))) {
  1733. return 1;
  1734. }
  1735. return 0;
  1736. }
  1737. /*
  1738. * Pick up the last hashvalue from a leaf block.
  1739. */
  1740. xfs_dahash_t
  1741. xfs_dir_leaf_lasthash(xfs_dabuf_t *bp, int *count)
  1742. {
  1743. xfs_dir_leafblock_t *leaf;
  1744. leaf = bp->data;
  1745. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR_LEAF_MAGIC);
  1746. if (count)
  1747. *count = INT_GET(leaf->hdr.count, ARCH_CONVERT);
  1748. if (!leaf->hdr.count)
  1749. return(0);
  1750. return(INT_GET(leaf->entries[ INT_GET(leaf->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT));
  1751. }
  1752. /*
  1753. * Copy out directory entries for getdents(), for leaf directories.
  1754. */
  1755. int
  1756. xfs_dir_leaf_getdents_int(
  1757. xfs_dabuf_t *bp,
  1758. xfs_inode_t *dp,
  1759. xfs_dablk_t bno,
  1760. uio_t *uio,
  1761. int *eobp,
  1762. xfs_dirent_t *dbp,
  1763. xfs_dir_put_t put,
  1764. xfs_daddr_t nextda)
  1765. {
  1766. xfs_dir_leafblock_t *leaf;
  1767. xfs_dir_leaf_entry_t *entry;
  1768. xfs_dir_leaf_name_t *namest;
  1769. int entno, want_entno, i, nextentno;
  1770. xfs_mount_t *mp;
  1771. xfs_dahash_t cookhash;
  1772. xfs_dahash_t nexthash = 0;
  1773. #if (BITS_PER_LONG == 32)
  1774. xfs_dahash_t lasthash = XFS_DA_MAXHASH;
  1775. #endif
  1776. xfs_dir_put_args_t p;
  1777. mp = dp->i_mount;
  1778. leaf = bp->data;
  1779. if (be16_to_cpu(leaf->hdr.info.magic) != XFS_DIR_LEAF_MAGIC) {
  1780. *eobp = 1;
  1781. return XFS_ERROR(ENOENT); /* XXX wrong code */
  1782. }
  1783. want_entno = XFS_DA_COOKIE_ENTRY(mp, uio->uio_offset);
  1784. cookhash = XFS_DA_COOKIE_HASH(mp, uio->uio_offset);
  1785. xfs_dir_trace_g_dul("leaf: start", dp, uio, leaf);
  1786. /*
  1787. * Re-find our place.
  1788. */
  1789. for (i = entno = 0, entry = &leaf->entries[0];
  1790. i < INT_GET(leaf->hdr.count, ARCH_CONVERT);
  1791. entry++, i++) {
  1792. namest = XFS_DIR_LEAF_NAMESTRUCT(leaf,
  1793. INT_GET(entry->nameidx, ARCH_CONVERT));
  1794. if (unlikely(
  1795. ((char *)namest < (char *)leaf) ||
  1796. ((char *)namest >= (char *)leaf + XFS_LBSIZE(mp)))) {
  1797. XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(1)",
  1798. XFS_ERRLEVEL_LOW, mp, leaf);
  1799. xfs_dir_trace_g_du("leaf: corrupted", dp, uio);
  1800. return XFS_ERROR(EFSCORRUPTED);
  1801. }
  1802. if (INT_GET(entry->hashval, ARCH_CONVERT) >= cookhash) {
  1803. if ( entno < want_entno
  1804. && INT_GET(entry->hashval, ARCH_CONVERT)
  1805. == cookhash) {
  1806. /*
  1807. * Trying to get to a particular offset in a
  1808. * run of equal-hashval entries.
  1809. */
  1810. entno++;
  1811. } else if ( want_entno > 0
  1812. && entno == want_entno
  1813. && INT_GET(entry->hashval, ARCH_CONVERT)
  1814. == cookhash) {
  1815. break;
  1816. } else {
  1817. entno = 0;
  1818. break;
  1819. }
  1820. }
  1821. }
  1822. if (i == INT_GET(leaf->hdr.count, ARCH_CONVERT)) {
  1823. xfs_dir_trace_g_du("leaf: hash not found", dp, uio);
  1824. if (!leaf->hdr.info.forw)
  1825. uio->uio_offset =
  1826. XFS_DA_MAKE_COOKIE(mp, 0, 0, XFS_DA_MAXHASH);
  1827. /*
  1828. * Don't set uio_offset if there's another block:
  1829. * the node code will be setting uio_offset anyway.
  1830. */
  1831. *eobp = 0;
  1832. return 0;
  1833. }
  1834. xfs_dir_trace_g_due("leaf: hash found", dp, uio, entry);
  1835. p.dbp = dbp;
  1836. p.put = put;
  1837. p.uio = uio;
  1838. /*
  1839. * We're synchronized, start copying entries out to the user.
  1840. */
  1841. for (; entno >= 0 && i < INT_GET(leaf->hdr.count, ARCH_CONVERT);
  1842. entry++, i++, (entno = nextentno)) {
  1843. int lastresid=0, retval;
  1844. xfs_dircook_t lastoffset;
  1845. xfs_dahash_t thishash;
  1846. /*
  1847. * Check for a damaged directory leaf block and pick up
  1848. * the inode number from this entry.
  1849. */
  1850. namest = XFS_DIR_LEAF_NAMESTRUCT(leaf,
  1851. INT_GET(entry->nameidx, ARCH_CONVERT));
  1852. if (unlikely(
  1853. ((char *)namest < (char *)leaf) ||
  1854. ((char *)namest >= (char *)leaf + XFS_LBSIZE(mp)))) {
  1855. XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(2)",
  1856. XFS_ERRLEVEL_LOW, mp, leaf);
  1857. xfs_dir_trace_g_du("leaf: corrupted", dp, uio);
  1858. return XFS_ERROR(EFSCORRUPTED);
  1859. }
  1860. xfs_dir_trace_g_duc("leaf: middle cookie ",
  1861. dp, uio, p.cook.o);
  1862. if (i < (INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1)) {
  1863. nexthash = INT_GET(entry[1].hashval, ARCH_CONVERT);
  1864. if (nexthash == INT_GET(entry->hashval, ARCH_CONVERT))
  1865. nextentno = entno + 1;
  1866. else
  1867. nextentno = 0;
  1868. XFS_PUT_COOKIE(p.cook, mp, bno, nextentno, nexthash);
  1869. xfs_dir_trace_g_duc("leaf: middle cookie ",
  1870. dp, uio, p.cook.o);
  1871. } else if ((thishash = be32_to_cpu(leaf->hdr.info.forw))) {
  1872. xfs_dabuf_t *bp2;
  1873. xfs_dir_leafblock_t *leaf2;
  1874. ASSERT(nextda != -1);
  1875. retval = xfs_da_read_buf(dp->i_transp, dp, thishash,
  1876. nextda, &bp2, XFS_DATA_FORK);
  1877. if (retval)
  1878. return retval;
  1879. ASSERT(bp2 != NULL);
  1880. leaf2 = bp2->data;
  1881. if (unlikely(
  1882. (be16_to_cpu(leaf2->hdr.info.magic)
  1883. != XFS_DIR_LEAF_MAGIC)
  1884. || (be32_to_cpu(leaf2->hdr.info.back)
  1885. != bno))) { /* GROT */
  1886. XFS_CORRUPTION_ERROR("xfs_dir_leaf_getdents_int(3)",
  1887. XFS_ERRLEVEL_LOW, mp,
  1888. leaf2);
  1889. xfs_da_brelse(dp->i_transp, bp2);
  1890. return XFS_ERROR(EFSCORRUPTED);
  1891. }
  1892. nexthash = INT_GET(leaf2->entries[0].hashval,
  1893. ARCH_CONVERT);
  1894. nextentno = -1;
  1895. XFS_PUT_COOKIE(p.cook, mp, thishash, 0, nexthash);
  1896. xfs_da_brelse(dp->i_transp, bp2);
  1897. xfs_dir_trace_g_duc("leaf: next blk cookie",
  1898. dp, uio, p.cook.o);
  1899. } else {
  1900. nextentno = -1;
  1901. XFS_PUT_COOKIE(p.cook, mp, 0, 0, XFS_DA_MAXHASH);
  1902. }
  1903. /*
  1904. * Save off the cookie so we can fall back should the
  1905. * 'put' into the outgoing buffer fails. To handle a run
  1906. * of equal-hashvals, the off_t structure on 64bit
  1907. * builds has entno built into the cookie to ID the
  1908. * entry. On 32bit builds, we only have space for the
  1909. * hashval so we can't ID specific entries within a group
  1910. * of same hashval entries. For this, lastoffset is set
  1911. * to the first in the run of equal hashvals so we don't
  1912. * include any entries unless we can include all entries
  1913. * that share the same hashval. Hopefully the buffer
  1914. * provided is big enough to handle it (see pv763517).
  1915. */
  1916. #if (BITS_PER_LONG == 32)
  1917. if ((thishash = INT_GET(entry->hashval, ARCH_CONVERT))
  1918. != lasthash) {
  1919. XFS_PUT_COOKIE(lastoffset, mp, bno, entno, thishash);
  1920. lastresid = uio->uio_resid;
  1921. lasthash = thishash;
  1922. } else {
  1923. xfs_dir_trace_g_duc("leaf: DUP COOKIES, skipped",
  1924. dp, uio, p.cook.o);
  1925. }
  1926. #else
  1927. thishash = INT_GET(entry->hashval, ARCH_CONVERT);
  1928. XFS_PUT_COOKIE(lastoffset, mp, bno, entno, thishash);
  1929. lastresid = uio->uio_resid;
  1930. #endif /* BITS_PER_LONG == 32 */
  1931. /*
  1932. * Put the current entry into the outgoing buffer. If we fail
  1933. * then restore the UIO to the first entry in the current
  1934. * run of equal-hashval entries (probably one 1 entry long).
  1935. */
  1936. p.ino = XFS_GET_DIR_INO8(namest->inumber);
  1937. #if XFS_BIG_INUMS
  1938. p.ino += mp->m_inoadd;
  1939. #endif
  1940. p.name = (char *)namest->name;
  1941. p.namelen = entry->namelen;
  1942. retval = p.put(&p);
  1943. if (!p.done) {
  1944. uio->uio_offset = lastoffset.o;
  1945. uio->uio_resid = lastresid;
  1946. *eobp = 1;
  1947. xfs_dir_trace_g_du("leaf: E-O-B", dp, uio);
  1948. return retval;
  1949. }
  1950. }
  1951. uio->uio_offset = p.cook.o;
  1952. *eobp = 0;
  1953. xfs_dir_trace_g_du("leaf: E-O-F", dp, uio);
  1954. return 0;
  1955. }
  1956. /*
  1957. * Format a dirent64 structure and copy it out the the user's buffer.
  1958. */
  1959. int
  1960. xfs_dir_put_dirent64_direct(xfs_dir_put_args_t *pa)
  1961. {
  1962. iovec_t *iovp;
  1963. int reclen, namelen;
  1964. xfs_dirent_t *idbp;
  1965. uio_t *uio;
  1966. namelen = pa->namelen;
  1967. reclen = DIRENTSIZE(namelen);
  1968. uio = pa->uio;
  1969. if (reclen > uio->uio_resid) {
  1970. pa->done = 0;
  1971. return 0;
  1972. }
  1973. iovp = uio->uio_iov;
  1974. idbp = (xfs_dirent_t *)iovp->iov_base;
  1975. iovp->iov_base = (char *)idbp + reclen;
  1976. iovp->iov_len -= reclen;
  1977. uio->uio_resid -= reclen;
  1978. idbp->d_reclen = reclen;
  1979. idbp->d_ino = pa->ino;
  1980. idbp->d_off = pa->cook.o;
  1981. idbp->d_name[namelen] = '\0';
  1982. pa->done = 1;
  1983. memcpy(idbp->d_name, pa->name, namelen);
  1984. return 0;
  1985. }
  1986. /*
  1987. * Format a dirent64 structure and copy it out the the user's buffer.
  1988. */
  1989. int
  1990. xfs_dir_put_dirent64_uio(xfs_dir_put_args_t *pa)
  1991. {
  1992. int retval, reclen, namelen;
  1993. xfs_dirent_t *idbp;
  1994. uio_t *uio;
  1995. namelen = pa->namelen;
  1996. reclen = DIRENTSIZE(namelen);
  1997. uio = pa->uio;
  1998. if (reclen > uio->uio_resid) {
  1999. pa->done = 0;
  2000. return 0;
  2001. }
  2002. idbp = pa->dbp;
  2003. idbp->d_reclen = reclen;
  2004. idbp->d_ino = pa->ino;
  2005. idbp->d_off = pa->cook.o;
  2006. idbp->d_name[namelen] = '\0';
  2007. memcpy(idbp->d_name, pa->name, namelen);
  2008. retval = uio_read((caddr_t)idbp, reclen, uio);
  2009. pa->done = (retval == 0);
  2010. return retval;
  2011. }