xfs_attr_leaf.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970
  1. /*
  2. * Copyright (c) 2000-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_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_da_btree.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_ialloc_btree.h"
  34. #include "xfs_alloc.h"
  35. #include "xfs_btree.h"
  36. #include "xfs_dir2_sf.h"
  37. #include "xfs_attr_sf.h"
  38. #include "xfs_dinode.h"
  39. #include "xfs_inode.h"
  40. #include "xfs_inode_item.h"
  41. #include "xfs_bmap.h"
  42. #include "xfs_attr.h"
  43. #include "xfs_attr_leaf.h"
  44. #include "xfs_error.h"
  45. /*
  46. * xfs_attr_leaf.c
  47. *
  48. * Routines to implement leaf blocks of attributes as Btrees of hashed names.
  49. */
  50. /*========================================================================
  51. * Function prototypes for the kernel.
  52. *========================================================================*/
  53. /*
  54. * Routines used for growing the Btree.
  55. */
  56. STATIC int xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t which_block,
  57. xfs_dabuf_t **bpp);
  58. STATIC int xfs_attr_leaf_add_work(xfs_dabuf_t *leaf_buffer, xfs_da_args_t *args,
  59. int freemap_index);
  60. STATIC void xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *leaf_buffer);
  61. STATIC void xfs_attr_leaf_rebalance(xfs_da_state_t *state,
  62. xfs_da_state_blk_t *blk1,
  63. xfs_da_state_blk_t *blk2);
  64. STATIC int xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
  65. xfs_da_state_blk_t *leaf_blk_1,
  66. xfs_da_state_blk_t *leaf_blk_2,
  67. int *number_entries_in_blk1,
  68. int *number_usedbytes_in_blk1);
  69. /*
  70. * Routines used for shrinking the Btree.
  71. */
  72. STATIC int xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
  73. xfs_dabuf_t *bp, int level);
  74. STATIC int xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp,
  75. xfs_dabuf_t *bp);
  76. STATIC int xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
  77. xfs_dablk_t blkno, int blkcnt);
  78. /*
  79. * Utility routines.
  80. */
  81. STATIC void xfs_attr_leaf_moveents(xfs_attr_leafblock_t *src_leaf,
  82. int src_start,
  83. xfs_attr_leafblock_t *dst_leaf,
  84. int dst_start, int move_count,
  85. xfs_mount_t *mp);
  86. STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
  87. /*========================================================================
  88. * Namespace helper routines
  89. *========================================================================*/
  90. /*
  91. * If namespace bits don't match return 0.
  92. * If all match then return 1.
  93. */
  94. STATIC_INLINE int
  95. xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
  96. {
  97. return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
  98. }
  99. /*========================================================================
  100. * External routines when attribute fork size < XFS_LITINO(mp).
  101. *========================================================================*/
  102. /*
  103. * Query whether the requested number of additional bytes of extended
  104. * attribute space will be able to fit inline.
  105. * Returns zero if not, else the di_forkoff fork offset to be used in the
  106. * literal area for attribute data once the new bytes have been added.
  107. *
  108. * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
  109. * special case for dev/uuid inodes, they have fixed size data forks.
  110. */
  111. int
  112. xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
  113. {
  114. int offset;
  115. int minforkoff; /* lower limit on valid forkoff locations */
  116. int maxforkoff; /* upper limit on valid forkoff locations */
  117. int dsize;
  118. xfs_mount_t *mp = dp->i_mount;
  119. offset = (XFS_LITINO(mp) - bytes) >> 3; /* rounded down */
  120. switch (dp->i_d.di_format) {
  121. case XFS_DINODE_FMT_DEV:
  122. minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
  123. return (offset >= minforkoff) ? minforkoff : 0;
  124. case XFS_DINODE_FMT_UUID:
  125. minforkoff = roundup(sizeof(uuid_t), 8) >> 3;
  126. return (offset >= minforkoff) ? minforkoff : 0;
  127. }
  128. if (!(mp->m_flags & XFS_MOUNT_ATTR2)) {
  129. if (bytes <= XFS_IFORK_ASIZE(dp))
  130. return dp->i_d.di_forkoff;
  131. return 0;
  132. }
  133. dsize = dp->i_df.if_bytes;
  134. switch (dp->i_d.di_format) {
  135. case XFS_DINODE_FMT_EXTENTS:
  136. /*
  137. * If there is no attr fork and the data fork is extents,
  138. * determine if creating the default attr fork will result
  139. * in the extents form migrating to btree. If so, the
  140. * minimum offset only needs to be the space required for
  141. * the btree root.
  142. */
  143. if (!dp->i_d.di_forkoff && dp->i_df.if_bytes >
  144. xfs_default_attroffset(dp))
  145. dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
  146. break;
  147. case XFS_DINODE_FMT_BTREE:
  148. /*
  149. * If have data btree then keep forkoff if we have one,
  150. * otherwise we are adding a new attr, so then we set
  151. * minforkoff to where the btree root can finish so we have
  152. * plenty of room for attrs
  153. */
  154. if (dp->i_d.di_forkoff) {
  155. if (offset < dp->i_d.di_forkoff)
  156. return 0;
  157. else
  158. return dp->i_d.di_forkoff;
  159. } else
  160. dsize = XFS_BMAP_BROOT_SPACE(dp->i_df.if_broot);
  161. break;
  162. }
  163. /*
  164. * A data fork btree root must have space for at least
  165. * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
  166. */
  167. minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
  168. minforkoff = roundup(minforkoff, 8) >> 3;
  169. /* attr fork btree root can have at least this many key/ptr pairs */
  170. maxforkoff = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
  171. maxforkoff = maxforkoff >> 3; /* rounded down */
  172. if (offset >= minforkoff && offset < maxforkoff)
  173. return offset;
  174. if (offset >= maxforkoff)
  175. return maxforkoff;
  176. return 0;
  177. }
  178. /*
  179. * Switch on the ATTR2 superblock bit (implies also FEATURES2)
  180. */
  181. STATIC void
  182. xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
  183. {
  184. if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
  185. !(xfs_sb_version_hasattr2(&mp->m_sb))) {
  186. spin_lock(&mp->m_sb_lock);
  187. if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
  188. xfs_sb_version_addattr2(&mp->m_sb);
  189. spin_unlock(&mp->m_sb_lock);
  190. xfs_mod_sb(tp, XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
  191. } else
  192. spin_unlock(&mp->m_sb_lock);
  193. }
  194. }
  195. /*
  196. * Create the initial contents of a shortform attribute list.
  197. */
  198. void
  199. xfs_attr_shortform_create(xfs_da_args_t *args)
  200. {
  201. xfs_attr_sf_hdr_t *hdr;
  202. xfs_inode_t *dp;
  203. xfs_ifork_t *ifp;
  204. dp = args->dp;
  205. ASSERT(dp != NULL);
  206. ifp = dp->i_afp;
  207. ASSERT(ifp != NULL);
  208. ASSERT(ifp->if_bytes == 0);
  209. if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
  210. ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */
  211. dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
  212. ifp->if_flags |= XFS_IFINLINE;
  213. } else {
  214. ASSERT(ifp->if_flags & XFS_IFINLINE);
  215. }
  216. xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
  217. hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
  218. hdr->count = 0;
  219. hdr->totsize = cpu_to_be16(sizeof(*hdr));
  220. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
  221. }
  222. /*
  223. * Add a name/value pair to the shortform attribute list.
  224. * Overflow from the inode has already been checked for.
  225. */
  226. void
  227. xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
  228. {
  229. xfs_attr_shortform_t *sf;
  230. xfs_attr_sf_entry_t *sfe;
  231. int i, offset, size;
  232. xfs_mount_t *mp;
  233. xfs_inode_t *dp;
  234. xfs_ifork_t *ifp;
  235. dp = args->dp;
  236. mp = dp->i_mount;
  237. dp->i_d.di_forkoff = forkoff;
  238. dp->i_df.if_ext_max =
  239. XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
  240. dp->i_afp->if_ext_max =
  241. XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
  242. ifp = dp->i_afp;
  243. ASSERT(ifp->if_flags & XFS_IFINLINE);
  244. sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
  245. sfe = &sf->list[0];
  246. for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
  247. #ifdef DEBUG
  248. if (sfe->namelen != args->namelen)
  249. continue;
  250. if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
  251. continue;
  252. if (!xfs_attr_namesp_match(args->flags, sfe->flags))
  253. continue;
  254. ASSERT(0);
  255. #endif
  256. }
  257. offset = (char *)sfe - (char *)sf;
  258. size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
  259. xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
  260. sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
  261. sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
  262. sfe->namelen = args->namelen;
  263. sfe->valuelen = args->valuelen;
  264. sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
  265. memcpy(sfe->nameval, args->name, args->namelen);
  266. memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
  267. sf->hdr.count++;
  268. be16_add_cpu(&sf->hdr.totsize, size);
  269. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
  270. xfs_sbversion_add_attr2(mp, args->trans);
  271. }
  272. /*
  273. * After the last attribute is removed revert to original inode format,
  274. * making all literal area available to the data fork once more.
  275. */
  276. STATIC void
  277. xfs_attr_fork_reset(
  278. struct xfs_inode *ip,
  279. struct xfs_trans *tp)
  280. {
  281. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  282. ip->i_d.di_forkoff = 0;
  283. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  284. ASSERT(ip->i_d.di_anextents == 0);
  285. ASSERT(ip->i_afp == NULL);
  286. ip->i_df.if_ext_max = XFS_IFORK_DSIZE(ip) / sizeof(xfs_bmbt_rec_t);
  287. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  288. }
  289. /*
  290. * Remove an attribute from the shortform attribute list structure.
  291. */
  292. int
  293. xfs_attr_shortform_remove(xfs_da_args_t *args)
  294. {
  295. xfs_attr_shortform_t *sf;
  296. xfs_attr_sf_entry_t *sfe;
  297. int base, size=0, end, totsize, i;
  298. xfs_mount_t *mp;
  299. xfs_inode_t *dp;
  300. dp = args->dp;
  301. mp = dp->i_mount;
  302. base = sizeof(xfs_attr_sf_hdr_t);
  303. sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
  304. sfe = &sf->list[0];
  305. end = sf->hdr.count;
  306. for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
  307. base += size, i++) {
  308. size = XFS_ATTR_SF_ENTSIZE(sfe);
  309. if (sfe->namelen != args->namelen)
  310. continue;
  311. if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
  312. continue;
  313. if (!xfs_attr_namesp_match(args->flags, sfe->flags))
  314. continue;
  315. break;
  316. }
  317. if (i == end)
  318. return(XFS_ERROR(ENOATTR));
  319. /*
  320. * Fix up the attribute fork data, covering the hole
  321. */
  322. end = base + size;
  323. totsize = be16_to_cpu(sf->hdr.totsize);
  324. if (end != totsize)
  325. memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
  326. sf->hdr.count--;
  327. be16_add_cpu(&sf->hdr.totsize, -size);
  328. /*
  329. * Fix up the start offset of the attribute fork
  330. */
  331. totsize -= size;
  332. if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
  333. (mp->m_flags & XFS_MOUNT_ATTR2) &&
  334. (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  335. !(args->op_flags & XFS_DA_OP_ADDNAME)) {
  336. xfs_attr_fork_reset(dp, args->trans);
  337. } else {
  338. xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
  339. dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
  340. ASSERT(dp->i_d.di_forkoff);
  341. ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
  342. (args->op_flags & XFS_DA_OP_ADDNAME) ||
  343. !(mp->m_flags & XFS_MOUNT_ATTR2) ||
  344. dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
  345. dp->i_afp->if_ext_max =
  346. XFS_IFORK_ASIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
  347. dp->i_df.if_ext_max =
  348. XFS_IFORK_DSIZE(dp) / (uint)sizeof(xfs_bmbt_rec_t);
  349. xfs_trans_log_inode(args->trans, dp,
  350. XFS_ILOG_CORE | XFS_ILOG_ADATA);
  351. }
  352. xfs_sbversion_add_attr2(mp, args->trans);
  353. return(0);
  354. }
  355. /*
  356. * Look up a name in a shortform attribute list structure.
  357. */
  358. /*ARGSUSED*/
  359. int
  360. xfs_attr_shortform_lookup(xfs_da_args_t *args)
  361. {
  362. xfs_attr_shortform_t *sf;
  363. xfs_attr_sf_entry_t *sfe;
  364. int i;
  365. xfs_ifork_t *ifp;
  366. ifp = args->dp->i_afp;
  367. ASSERT(ifp->if_flags & XFS_IFINLINE);
  368. sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
  369. sfe = &sf->list[0];
  370. for (i = 0; i < sf->hdr.count;
  371. sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
  372. if (sfe->namelen != args->namelen)
  373. continue;
  374. if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
  375. continue;
  376. if (!xfs_attr_namesp_match(args->flags, sfe->flags))
  377. continue;
  378. return(XFS_ERROR(EEXIST));
  379. }
  380. return(XFS_ERROR(ENOATTR));
  381. }
  382. /*
  383. * Look up a name in a shortform attribute list structure.
  384. */
  385. /*ARGSUSED*/
  386. int
  387. xfs_attr_shortform_getvalue(xfs_da_args_t *args)
  388. {
  389. xfs_attr_shortform_t *sf;
  390. xfs_attr_sf_entry_t *sfe;
  391. int i;
  392. ASSERT(args->dp->i_d.di_aformat == XFS_IFINLINE);
  393. sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
  394. sfe = &sf->list[0];
  395. for (i = 0; i < sf->hdr.count;
  396. sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
  397. if (sfe->namelen != args->namelen)
  398. continue;
  399. if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
  400. continue;
  401. if (!xfs_attr_namesp_match(args->flags, sfe->flags))
  402. continue;
  403. if (args->flags & ATTR_KERNOVAL) {
  404. args->valuelen = sfe->valuelen;
  405. return(XFS_ERROR(EEXIST));
  406. }
  407. if (args->valuelen < sfe->valuelen) {
  408. args->valuelen = sfe->valuelen;
  409. return(XFS_ERROR(ERANGE));
  410. }
  411. args->valuelen = sfe->valuelen;
  412. memcpy(args->value, &sfe->nameval[args->namelen],
  413. args->valuelen);
  414. return(XFS_ERROR(EEXIST));
  415. }
  416. return(XFS_ERROR(ENOATTR));
  417. }
  418. /*
  419. * Convert from using the shortform to the leaf.
  420. */
  421. int
  422. xfs_attr_shortform_to_leaf(xfs_da_args_t *args)
  423. {
  424. xfs_inode_t *dp;
  425. xfs_attr_shortform_t *sf;
  426. xfs_attr_sf_entry_t *sfe;
  427. xfs_da_args_t nargs;
  428. char *tmpbuffer;
  429. int error, i, size;
  430. xfs_dablk_t blkno;
  431. xfs_dabuf_t *bp;
  432. xfs_ifork_t *ifp;
  433. dp = args->dp;
  434. ifp = dp->i_afp;
  435. sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
  436. size = be16_to_cpu(sf->hdr.totsize);
  437. tmpbuffer = kmem_alloc(size, KM_SLEEP);
  438. ASSERT(tmpbuffer != NULL);
  439. memcpy(tmpbuffer, ifp->if_u1.if_data, size);
  440. sf = (xfs_attr_shortform_t *)tmpbuffer;
  441. xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
  442. bp = NULL;
  443. error = xfs_da_grow_inode(args, &blkno);
  444. if (error) {
  445. /*
  446. * If we hit an IO error middle of the transaction inside
  447. * grow_inode(), we may have inconsistent data. Bail out.
  448. */
  449. if (error == EIO)
  450. goto out;
  451. xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
  452. memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
  453. goto out;
  454. }
  455. ASSERT(blkno == 0);
  456. error = xfs_attr_leaf_create(args, blkno, &bp);
  457. if (error) {
  458. error = xfs_da_shrink_inode(args, 0, bp);
  459. bp = NULL;
  460. if (error)
  461. goto out;
  462. xfs_idata_realloc(dp, size, XFS_ATTR_FORK); /* try to put */
  463. memcpy(ifp->if_u1.if_data, tmpbuffer, size); /* it back */
  464. goto out;
  465. }
  466. memset((char *)&nargs, 0, sizeof(nargs));
  467. nargs.dp = dp;
  468. nargs.firstblock = args->firstblock;
  469. nargs.flist = args->flist;
  470. nargs.total = args->total;
  471. nargs.whichfork = XFS_ATTR_FORK;
  472. nargs.trans = args->trans;
  473. nargs.op_flags = XFS_DA_OP_OKNOENT;
  474. sfe = &sf->list[0];
  475. for (i = 0; i < sf->hdr.count; i++) {
  476. nargs.name = (char *)sfe->nameval;
  477. nargs.namelen = sfe->namelen;
  478. nargs.value = (char *)&sfe->nameval[nargs.namelen];
  479. nargs.valuelen = sfe->valuelen;
  480. nargs.hashval = xfs_da_hashname((char *)sfe->nameval,
  481. sfe->namelen);
  482. nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
  483. error = xfs_attr_leaf_lookup_int(bp, &nargs); /* set a->index */
  484. ASSERT(error == ENOATTR);
  485. error = xfs_attr_leaf_add(bp, &nargs);
  486. ASSERT(error != ENOSPC);
  487. if (error)
  488. goto out;
  489. sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
  490. }
  491. error = 0;
  492. out:
  493. if(bp)
  494. xfs_da_buf_done(bp);
  495. kmem_free(tmpbuffer);
  496. return(error);
  497. }
  498. STATIC int
  499. xfs_attr_shortform_compare(const void *a, const void *b)
  500. {
  501. xfs_attr_sf_sort_t *sa, *sb;
  502. sa = (xfs_attr_sf_sort_t *)a;
  503. sb = (xfs_attr_sf_sort_t *)b;
  504. if (sa->hash < sb->hash) {
  505. return(-1);
  506. } else if (sa->hash > sb->hash) {
  507. return(1);
  508. } else {
  509. return(sa->entno - sb->entno);
  510. }
  511. }
  512. #define XFS_ISRESET_CURSOR(cursor) \
  513. (!((cursor)->initted) && !((cursor)->hashval) && \
  514. !((cursor)->blkno) && !((cursor)->offset))
  515. /*
  516. * Copy out entries of shortform attribute lists for attr_list().
  517. * Shortform attribute lists are not stored in hashval sorted order.
  518. * If the output buffer is not large enough to hold them all, then we
  519. * we have to calculate each entries' hashvalue and sort them before
  520. * we can begin returning them to the user.
  521. */
  522. /*ARGSUSED*/
  523. int
  524. xfs_attr_shortform_list(xfs_attr_list_context_t *context)
  525. {
  526. attrlist_cursor_kern_t *cursor;
  527. xfs_attr_sf_sort_t *sbuf, *sbp;
  528. xfs_attr_shortform_t *sf;
  529. xfs_attr_sf_entry_t *sfe;
  530. xfs_inode_t *dp;
  531. int sbsize, nsbuf, count, i;
  532. int error;
  533. ASSERT(context != NULL);
  534. dp = context->dp;
  535. ASSERT(dp != NULL);
  536. ASSERT(dp->i_afp != NULL);
  537. sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
  538. ASSERT(sf != NULL);
  539. if (!sf->hdr.count)
  540. return(0);
  541. cursor = context->cursor;
  542. ASSERT(cursor != NULL);
  543. xfs_attr_trace_l_c("sf start", context);
  544. /*
  545. * If the buffer is large enough and the cursor is at the start,
  546. * do not bother with sorting since we will return everything in
  547. * one buffer and another call using the cursor won't need to be
  548. * made.
  549. * Note the generous fudge factor of 16 overhead bytes per entry.
  550. * If bufsize is zero then put_listent must be a search function
  551. * and can just scan through what we have.
  552. */
  553. if (context->bufsize == 0 ||
  554. (XFS_ISRESET_CURSOR(cursor) &&
  555. (dp->i_afp->if_bytes + sf->hdr.count * 16) < context->bufsize)) {
  556. for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
  557. error = context->put_listent(context,
  558. sfe->flags,
  559. (char *)sfe->nameval,
  560. (int)sfe->namelen,
  561. (int)sfe->valuelen,
  562. (char*)&sfe->nameval[sfe->namelen]);
  563. /*
  564. * Either search callback finished early or
  565. * didn't fit it all in the buffer after all.
  566. */
  567. if (context->seen_enough)
  568. break;
  569. if (error)
  570. return error;
  571. sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
  572. }
  573. xfs_attr_trace_l_c("sf big-gulp", context);
  574. return(0);
  575. }
  576. /* do no more for a search callback */
  577. if (context->bufsize == 0)
  578. return 0;
  579. /*
  580. * It didn't all fit, so we have to sort everything on hashval.
  581. */
  582. sbsize = sf->hdr.count * sizeof(*sbuf);
  583. sbp = sbuf = kmem_alloc(sbsize, KM_SLEEP);
  584. /*
  585. * Scan the attribute list for the rest of the entries, storing
  586. * the relevant info from only those that match into a buffer.
  587. */
  588. nsbuf = 0;
  589. for (i = 0, sfe = &sf->list[0]; i < sf->hdr.count; i++) {
  590. if (unlikely(
  591. ((char *)sfe < (char *)sf) ||
  592. ((char *)sfe >= ((char *)sf + dp->i_afp->if_bytes)))) {
  593. XFS_CORRUPTION_ERROR("xfs_attr_shortform_list",
  594. XFS_ERRLEVEL_LOW,
  595. context->dp->i_mount, sfe);
  596. xfs_attr_trace_l_c("sf corrupted", context);
  597. kmem_free(sbuf);
  598. return XFS_ERROR(EFSCORRUPTED);
  599. }
  600. sbp->entno = i;
  601. sbp->hash = xfs_da_hashname((char *)sfe->nameval, sfe->namelen);
  602. sbp->name = (char *)sfe->nameval;
  603. sbp->namelen = sfe->namelen;
  604. /* These are bytes, and both on-disk, don't endian-flip */
  605. sbp->valuelen = sfe->valuelen;
  606. sbp->flags = sfe->flags;
  607. sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
  608. sbp++;
  609. nsbuf++;
  610. }
  611. /*
  612. * Sort the entries on hash then entno.
  613. */
  614. xfs_sort(sbuf, nsbuf, sizeof(*sbuf), xfs_attr_shortform_compare);
  615. /*
  616. * Re-find our place IN THE SORTED LIST.
  617. */
  618. count = 0;
  619. cursor->initted = 1;
  620. cursor->blkno = 0;
  621. for (sbp = sbuf, i = 0; i < nsbuf; i++, sbp++) {
  622. if (sbp->hash == cursor->hashval) {
  623. if (cursor->offset == count) {
  624. break;
  625. }
  626. count++;
  627. } else if (sbp->hash > cursor->hashval) {
  628. break;
  629. }
  630. }
  631. if (i == nsbuf) {
  632. kmem_free(sbuf);
  633. xfs_attr_trace_l_c("blk end", context);
  634. return(0);
  635. }
  636. /*
  637. * Loop putting entries into the user buffer.
  638. */
  639. for ( ; i < nsbuf; i++, sbp++) {
  640. if (cursor->hashval != sbp->hash) {
  641. cursor->hashval = sbp->hash;
  642. cursor->offset = 0;
  643. }
  644. error = context->put_listent(context,
  645. sbp->flags,
  646. sbp->name,
  647. sbp->namelen,
  648. sbp->valuelen,
  649. &sbp->name[sbp->namelen]);
  650. if (error)
  651. return error;
  652. if (context->seen_enough)
  653. break;
  654. cursor->offset++;
  655. }
  656. kmem_free(sbuf);
  657. xfs_attr_trace_l_c("sf E-O-F", context);
  658. return(0);
  659. }
  660. /*
  661. * Check a leaf attribute block to see if all the entries would fit into
  662. * a shortform attribute list.
  663. */
  664. int
  665. xfs_attr_shortform_allfit(xfs_dabuf_t *bp, xfs_inode_t *dp)
  666. {
  667. xfs_attr_leafblock_t *leaf;
  668. xfs_attr_leaf_entry_t *entry;
  669. xfs_attr_leaf_name_local_t *name_loc;
  670. int bytes, i;
  671. leaf = bp->data;
  672. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  673. entry = &leaf->entries[0];
  674. bytes = sizeof(struct xfs_attr_sf_hdr);
  675. for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
  676. if (entry->flags & XFS_ATTR_INCOMPLETE)
  677. continue; /* don't copy partial entries */
  678. if (!(entry->flags & XFS_ATTR_LOCAL))
  679. return(0);
  680. name_loc = xfs_attr_leaf_name_local(leaf, i);
  681. if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
  682. return(0);
  683. if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
  684. return(0);
  685. bytes += sizeof(struct xfs_attr_sf_entry)-1
  686. + name_loc->namelen
  687. + be16_to_cpu(name_loc->valuelen);
  688. }
  689. if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
  690. (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  691. (bytes == sizeof(struct xfs_attr_sf_hdr)))
  692. return(-1);
  693. return(xfs_attr_shortform_bytesfit(dp, bytes));
  694. }
  695. /*
  696. * Convert a leaf attribute list to shortform attribute list
  697. */
  698. int
  699. xfs_attr_leaf_to_shortform(xfs_dabuf_t *bp, xfs_da_args_t *args, int forkoff)
  700. {
  701. xfs_attr_leafblock_t *leaf;
  702. xfs_attr_leaf_entry_t *entry;
  703. xfs_attr_leaf_name_local_t *name_loc;
  704. xfs_da_args_t nargs;
  705. xfs_inode_t *dp;
  706. char *tmpbuffer;
  707. int error, i;
  708. dp = args->dp;
  709. tmpbuffer = kmem_alloc(XFS_LBSIZE(dp->i_mount), KM_SLEEP);
  710. ASSERT(tmpbuffer != NULL);
  711. ASSERT(bp != NULL);
  712. memcpy(tmpbuffer, bp->data, XFS_LBSIZE(dp->i_mount));
  713. leaf = (xfs_attr_leafblock_t *)tmpbuffer;
  714. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  715. memset(bp->data, 0, XFS_LBSIZE(dp->i_mount));
  716. /*
  717. * Clean out the prior contents of the attribute list.
  718. */
  719. error = xfs_da_shrink_inode(args, 0, bp);
  720. if (error)
  721. goto out;
  722. if (forkoff == -1) {
  723. ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
  724. ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
  725. xfs_attr_fork_reset(dp, args->trans);
  726. goto out;
  727. }
  728. xfs_attr_shortform_create(args);
  729. /*
  730. * Copy the attributes
  731. */
  732. memset((char *)&nargs, 0, sizeof(nargs));
  733. nargs.dp = dp;
  734. nargs.firstblock = args->firstblock;
  735. nargs.flist = args->flist;
  736. nargs.total = args->total;
  737. nargs.whichfork = XFS_ATTR_FORK;
  738. nargs.trans = args->trans;
  739. nargs.op_flags = XFS_DA_OP_OKNOENT;
  740. entry = &leaf->entries[0];
  741. for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
  742. if (entry->flags & XFS_ATTR_INCOMPLETE)
  743. continue; /* don't copy partial entries */
  744. if (!entry->nameidx)
  745. continue;
  746. ASSERT(entry->flags & XFS_ATTR_LOCAL);
  747. name_loc = xfs_attr_leaf_name_local(leaf, i);
  748. nargs.name = (char *)name_loc->nameval;
  749. nargs.namelen = name_loc->namelen;
  750. nargs.value = (char *)&name_loc->nameval[nargs.namelen];
  751. nargs.valuelen = be16_to_cpu(name_loc->valuelen);
  752. nargs.hashval = be32_to_cpu(entry->hashval);
  753. nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
  754. xfs_attr_shortform_add(&nargs, forkoff);
  755. }
  756. error = 0;
  757. out:
  758. kmem_free(tmpbuffer);
  759. return(error);
  760. }
  761. /*
  762. * Convert from using a single leaf to a root node and a leaf.
  763. */
  764. int
  765. xfs_attr_leaf_to_node(xfs_da_args_t *args)
  766. {
  767. xfs_attr_leafblock_t *leaf;
  768. xfs_da_intnode_t *node;
  769. xfs_inode_t *dp;
  770. xfs_dabuf_t *bp1, *bp2;
  771. xfs_dablk_t blkno;
  772. int error;
  773. dp = args->dp;
  774. bp1 = bp2 = NULL;
  775. error = xfs_da_grow_inode(args, &blkno);
  776. if (error)
  777. goto out;
  778. error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp1,
  779. XFS_ATTR_FORK);
  780. if (error)
  781. goto out;
  782. ASSERT(bp1 != NULL);
  783. bp2 = NULL;
  784. error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp2,
  785. XFS_ATTR_FORK);
  786. if (error)
  787. goto out;
  788. ASSERT(bp2 != NULL);
  789. memcpy(bp2->data, bp1->data, XFS_LBSIZE(dp->i_mount));
  790. xfs_da_buf_done(bp1);
  791. bp1 = NULL;
  792. xfs_da_log_buf(args->trans, bp2, 0, XFS_LBSIZE(dp->i_mount) - 1);
  793. /*
  794. * Set up the new root node.
  795. */
  796. error = xfs_da_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
  797. if (error)
  798. goto out;
  799. node = bp1->data;
  800. leaf = bp2->data;
  801. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  802. /* both on-disk, don't endian-flip twice */
  803. node->btree[0].hashval =
  804. leaf->entries[be16_to_cpu(leaf->hdr.count)-1 ].hashval;
  805. node->btree[0].before = cpu_to_be32(blkno);
  806. node->hdr.count = cpu_to_be16(1);
  807. xfs_da_log_buf(args->trans, bp1, 0, XFS_LBSIZE(dp->i_mount) - 1);
  808. error = 0;
  809. out:
  810. if (bp1)
  811. xfs_da_buf_done(bp1);
  812. if (bp2)
  813. xfs_da_buf_done(bp2);
  814. return(error);
  815. }
  816. /*========================================================================
  817. * Routines used for growing the Btree.
  818. *========================================================================*/
  819. /*
  820. * Create the initial contents of a leaf attribute list
  821. * or a leaf in a node attribute list.
  822. */
  823. STATIC int
  824. xfs_attr_leaf_create(xfs_da_args_t *args, xfs_dablk_t blkno, xfs_dabuf_t **bpp)
  825. {
  826. xfs_attr_leafblock_t *leaf;
  827. xfs_attr_leaf_hdr_t *hdr;
  828. xfs_inode_t *dp;
  829. xfs_dabuf_t *bp;
  830. int error;
  831. dp = args->dp;
  832. ASSERT(dp != NULL);
  833. error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
  834. XFS_ATTR_FORK);
  835. if (error)
  836. return(error);
  837. ASSERT(bp != NULL);
  838. leaf = bp->data;
  839. memset((char *)leaf, 0, XFS_LBSIZE(dp->i_mount));
  840. hdr = &leaf->hdr;
  841. hdr->info.magic = cpu_to_be16(XFS_ATTR_LEAF_MAGIC);
  842. hdr->firstused = cpu_to_be16(XFS_LBSIZE(dp->i_mount));
  843. if (!hdr->firstused) {
  844. hdr->firstused = cpu_to_be16(
  845. XFS_LBSIZE(dp->i_mount) - XFS_ATTR_LEAF_NAME_ALIGN);
  846. }
  847. hdr->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
  848. hdr->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr->firstused) -
  849. sizeof(xfs_attr_leaf_hdr_t));
  850. xfs_da_log_buf(args->trans, bp, 0, XFS_LBSIZE(dp->i_mount) - 1);
  851. *bpp = bp;
  852. return(0);
  853. }
  854. /*
  855. * Split the leaf node, rebalance, then add the new entry.
  856. */
  857. int
  858. xfs_attr_leaf_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
  859. xfs_da_state_blk_t *newblk)
  860. {
  861. xfs_dablk_t blkno;
  862. int error;
  863. /*
  864. * Allocate space for a new leaf node.
  865. */
  866. ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
  867. error = xfs_da_grow_inode(state->args, &blkno);
  868. if (error)
  869. return(error);
  870. error = xfs_attr_leaf_create(state->args, blkno, &newblk->bp);
  871. if (error)
  872. return(error);
  873. newblk->blkno = blkno;
  874. newblk->magic = XFS_ATTR_LEAF_MAGIC;
  875. /*
  876. * Rebalance the entries across the two leaves.
  877. * NOTE: rebalance() currently depends on the 2nd block being empty.
  878. */
  879. xfs_attr_leaf_rebalance(state, oldblk, newblk);
  880. error = xfs_da_blk_link(state, oldblk, newblk);
  881. if (error)
  882. return(error);
  883. /*
  884. * Save info on "old" attribute for "atomic rename" ops, leaf_add()
  885. * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
  886. * "new" attrs info. Will need the "old" info to remove it later.
  887. *
  888. * Insert the "new" entry in the correct block.
  889. */
  890. if (state->inleaf)
  891. error = xfs_attr_leaf_add(oldblk->bp, state->args);
  892. else
  893. error = xfs_attr_leaf_add(newblk->bp, state->args);
  894. /*
  895. * Update last hashval in each block since we added the name.
  896. */
  897. oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
  898. newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
  899. return(error);
  900. }
  901. /*
  902. * Add a name to the leaf attribute list structure.
  903. */
  904. int
  905. xfs_attr_leaf_add(xfs_dabuf_t *bp, xfs_da_args_t *args)
  906. {
  907. xfs_attr_leafblock_t *leaf;
  908. xfs_attr_leaf_hdr_t *hdr;
  909. xfs_attr_leaf_map_t *map;
  910. int tablesize, entsize, sum, tmp, i;
  911. leaf = bp->data;
  912. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  913. ASSERT((args->index >= 0)
  914. && (args->index <= be16_to_cpu(leaf->hdr.count)));
  915. hdr = &leaf->hdr;
  916. entsize = xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
  917. args->trans->t_mountp->m_sb.sb_blocksize, NULL);
  918. /*
  919. * Search through freemap for first-fit on new name length.
  920. * (may need to figure in size of entry struct too)
  921. */
  922. tablesize = (be16_to_cpu(hdr->count) + 1)
  923. * sizeof(xfs_attr_leaf_entry_t)
  924. + sizeof(xfs_attr_leaf_hdr_t);
  925. map = &hdr->freemap[XFS_ATTR_LEAF_MAPSIZE-1];
  926. for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE-1; i >= 0; map--, i--) {
  927. if (tablesize > be16_to_cpu(hdr->firstused)) {
  928. sum += be16_to_cpu(map->size);
  929. continue;
  930. }
  931. if (!map->size)
  932. continue; /* no space in this map */
  933. tmp = entsize;
  934. if (be16_to_cpu(map->base) < be16_to_cpu(hdr->firstused))
  935. tmp += sizeof(xfs_attr_leaf_entry_t);
  936. if (be16_to_cpu(map->size) >= tmp) {
  937. tmp = xfs_attr_leaf_add_work(bp, args, i);
  938. return(tmp);
  939. }
  940. sum += be16_to_cpu(map->size);
  941. }
  942. /*
  943. * If there are no holes in the address space of the block,
  944. * and we don't have enough freespace, then compaction will do us
  945. * no good and we should just give up.
  946. */
  947. if (!hdr->holes && (sum < entsize))
  948. return(XFS_ERROR(ENOSPC));
  949. /*
  950. * Compact the entries to coalesce free space.
  951. * This may change the hdr->count via dropping INCOMPLETE entries.
  952. */
  953. xfs_attr_leaf_compact(args->trans, bp);
  954. /*
  955. * After compaction, the block is guaranteed to have only one
  956. * free region, in freemap[0]. If it is not big enough, give up.
  957. */
  958. if (be16_to_cpu(hdr->freemap[0].size)
  959. < (entsize + sizeof(xfs_attr_leaf_entry_t)))
  960. return(XFS_ERROR(ENOSPC));
  961. return(xfs_attr_leaf_add_work(bp, args, 0));
  962. }
  963. /*
  964. * Add a name to a leaf attribute list structure.
  965. */
  966. STATIC int
  967. xfs_attr_leaf_add_work(xfs_dabuf_t *bp, xfs_da_args_t *args, int mapindex)
  968. {
  969. xfs_attr_leafblock_t *leaf;
  970. xfs_attr_leaf_hdr_t *hdr;
  971. xfs_attr_leaf_entry_t *entry;
  972. xfs_attr_leaf_name_local_t *name_loc;
  973. xfs_attr_leaf_name_remote_t *name_rmt;
  974. xfs_attr_leaf_map_t *map;
  975. xfs_mount_t *mp;
  976. int tmp, i;
  977. leaf = bp->data;
  978. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  979. hdr = &leaf->hdr;
  980. ASSERT((mapindex >= 0) && (mapindex < XFS_ATTR_LEAF_MAPSIZE));
  981. ASSERT((args->index >= 0) && (args->index <= be16_to_cpu(hdr->count)));
  982. /*
  983. * Force open some space in the entry array and fill it in.
  984. */
  985. entry = &leaf->entries[args->index];
  986. if (args->index < be16_to_cpu(hdr->count)) {
  987. tmp = be16_to_cpu(hdr->count) - args->index;
  988. tmp *= sizeof(xfs_attr_leaf_entry_t);
  989. memmove((char *)(entry+1), (char *)entry, tmp);
  990. xfs_da_log_buf(args->trans, bp,
  991. XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
  992. }
  993. be16_add_cpu(&hdr->count, 1);
  994. /*
  995. * Allocate space for the new string (at the end of the run).
  996. */
  997. map = &hdr->freemap[mapindex];
  998. mp = args->trans->t_mountp;
  999. ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
  1000. ASSERT((be16_to_cpu(map->base) & 0x3) == 0);
  1001. ASSERT(be16_to_cpu(map->size) >=
  1002. xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
  1003. mp->m_sb.sb_blocksize, NULL));
  1004. ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
  1005. ASSERT((be16_to_cpu(map->size) & 0x3) == 0);
  1006. be16_add_cpu(&map->size,
  1007. -xfs_attr_leaf_newentsize(args->namelen, args->valuelen,
  1008. mp->m_sb.sb_blocksize, &tmp));
  1009. entry->nameidx = cpu_to_be16(be16_to_cpu(map->base) +
  1010. be16_to_cpu(map->size));
  1011. entry->hashval = cpu_to_be32(args->hashval);
  1012. entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
  1013. entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
  1014. if (args->op_flags & XFS_DA_OP_RENAME) {
  1015. entry->flags |= XFS_ATTR_INCOMPLETE;
  1016. if ((args->blkno2 == args->blkno) &&
  1017. (args->index2 <= args->index)) {
  1018. args->index2++;
  1019. }
  1020. }
  1021. xfs_da_log_buf(args->trans, bp,
  1022. XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
  1023. ASSERT((args->index == 0) ||
  1024. (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
  1025. ASSERT((args->index == be16_to_cpu(hdr->count)-1) ||
  1026. (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
  1027. /*
  1028. * Copy the attribute name and value into the new space.
  1029. *
  1030. * For "remote" attribute values, simply note that we need to
  1031. * allocate space for the "remote" value. We can't actually
  1032. * allocate the extents in this transaction, and we can't decide
  1033. * which blocks they should be as we might allocate more blocks
  1034. * as part of this transaction (a split operation for example).
  1035. */
  1036. if (entry->flags & XFS_ATTR_LOCAL) {
  1037. name_loc = xfs_attr_leaf_name_local(leaf, args->index);
  1038. name_loc->namelen = args->namelen;
  1039. name_loc->valuelen = cpu_to_be16(args->valuelen);
  1040. memcpy((char *)name_loc->nameval, args->name, args->namelen);
  1041. memcpy((char *)&name_loc->nameval[args->namelen], args->value,
  1042. be16_to_cpu(name_loc->valuelen));
  1043. } else {
  1044. name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
  1045. name_rmt->namelen = args->namelen;
  1046. memcpy((char *)name_rmt->name, args->name, args->namelen);
  1047. entry->flags |= XFS_ATTR_INCOMPLETE;
  1048. /* just in case */
  1049. name_rmt->valuelen = 0;
  1050. name_rmt->valueblk = 0;
  1051. args->rmtblkno = 1;
  1052. args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen);
  1053. }
  1054. xfs_da_log_buf(args->trans, bp,
  1055. XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
  1056. xfs_attr_leaf_entsize(leaf, args->index)));
  1057. /*
  1058. * Update the control info for this leaf node
  1059. */
  1060. if (be16_to_cpu(entry->nameidx) < be16_to_cpu(hdr->firstused)) {
  1061. /* both on-disk, don't endian-flip twice */
  1062. hdr->firstused = entry->nameidx;
  1063. }
  1064. ASSERT(be16_to_cpu(hdr->firstused) >=
  1065. ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
  1066. tmp = (be16_to_cpu(hdr->count)-1) * sizeof(xfs_attr_leaf_entry_t)
  1067. + sizeof(xfs_attr_leaf_hdr_t);
  1068. map = &hdr->freemap[0];
  1069. for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
  1070. if (be16_to_cpu(map->base) == tmp) {
  1071. be16_add_cpu(&map->base, sizeof(xfs_attr_leaf_entry_t));
  1072. be16_add_cpu(&map->size,
  1073. -((int)sizeof(xfs_attr_leaf_entry_t)));
  1074. }
  1075. }
  1076. be16_add_cpu(&hdr->usedbytes, xfs_attr_leaf_entsize(leaf, args->index));
  1077. xfs_da_log_buf(args->trans, bp,
  1078. XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
  1079. return(0);
  1080. }
  1081. /*
  1082. * Garbage collect a leaf attribute list block by copying it to a new buffer.
  1083. */
  1084. STATIC void
  1085. xfs_attr_leaf_compact(xfs_trans_t *trans, xfs_dabuf_t *bp)
  1086. {
  1087. xfs_attr_leafblock_t *leaf_s, *leaf_d;
  1088. xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
  1089. xfs_mount_t *mp;
  1090. char *tmpbuffer;
  1091. mp = trans->t_mountp;
  1092. tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
  1093. ASSERT(tmpbuffer != NULL);
  1094. memcpy(tmpbuffer, bp->data, XFS_LBSIZE(mp));
  1095. memset(bp->data, 0, XFS_LBSIZE(mp));
  1096. /*
  1097. * Copy basic information
  1098. */
  1099. leaf_s = (xfs_attr_leafblock_t *)tmpbuffer;
  1100. leaf_d = bp->data;
  1101. hdr_s = &leaf_s->hdr;
  1102. hdr_d = &leaf_d->hdr;
  1103. hdr_d->info = hdr_s->info; /* struct copy */
  1104. hdr_d->firstused = cpu_to_be16(XFS_LBSIZE(mp));
  1105. /* handle truncation gracefully */
  1106. if (!hdr_d->firstused) {
  1107. hdr_d->firstused = cpu_to_be16(
  1108. XFS_LBSIZE(mp) - XFS_ATTR_LEAF_NAME_ALIGN);
  1109. }
  1110. hdr_d->usedbytes = 0;
  1111. hdr_d->count = 0;
  1112. hdr_d->holes = 0;
  1113. hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
  1114. hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused) -
  1115. sizeof(xfs_attr_leaf_hdr_t));
  1116. /*
  1117. * Copy all entry's in the same (sorted) order,
  1118. * but allocate name/value pairs packed and in sequence.
  1119. */
  1120. xfs_attr_leaf_moveents(leaf_s, 0, leaf_d, 0,
  1121. be16_to_cpu(hdr_s->count), mp);
  1122. xfs_da_log_buf(trans, bp, 0, XFS_LBSIZE(mp) - 1);
  1123. kmem_free(tmpbuffer);
  1124. }
  1125. /*
  1126. * Redistribute the attribute list entries between two leaf nodes,
  1127. * taking into account the size of the new entry.
  1128. *
  1129. * NOTE: if new block is empty, then it will get the upper half of the
  1130. * old block. At present, all (one) callers pass in an empty second block.
  1131. *
  1132. * This code adjusts the args->index/blkno and args->index2/blkno2 fields
  1133. * to match what it is doing in splitting the attribute leaf block. Those
  1134. * values are used in "atomic rename" operations on attributes. Note that
  1135. * the "new" and "old" values can end up in different blocks.
  1136. */
  1137. STATIC void
  1138. xfs_attr_leaf_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
  1139. xfs_da_state_blk_t *blk2)
  1140. {
  1141. xfs_da_args_t *args;
  1142. xfs_da_state_blk_t *tmp_blk;
  1143. xfs_attr_leafblock_t *leaf1, *leaf2;
  1144. xfs_attr_leaf_hdr_t *hdr1, *hdr2;
  1145. int count, totallen, max, space, swap;
  1146. /*
  1147. * Set up environment.
  1148. */
  1149. ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
  1150. ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
  1151. leaf1 = blk1->bp->data;
  1152. leaf2 = blk2->bp->data;
  1153. ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  1154. ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  1155. args = state->args;
  1156. /*
  1157. * Check ordering of blocks, reverse if it makes things simpler.
  1158. *
  1159. * NOTE: Given that all (current) callers pass in an empty
  1160. * second block, this code should never set "swap".
  1161. */
  1162. swap = 0;
  1163. if (xfs_attr_leaf_order(blk1->bp, blk2->bp)) {
  1164. tmp_blk = blk1;
  1165. blk1 = blk2;
  1166. blk2 = tmp_blk;
  1167. leaf1 = blk1->bp->data;
  1168. leaf2 = blk2->bp->data;
  1169. swap = 1;
  1170. }
  1171. hdr1 = &leaf1->hdr;
  1172. hdr2 = &leaf2->hdr;
  1173. /*
  1174. * Examine entries until we reduce the absolute difference in
  1175. * byte usage between the two blocks to a minimum. Then get
  1176. * the direction to copy and the number of elements to move.
  1177. *
  1178. * "inleaf" is true if the new entry should be inserted into blk1.
  1179. * If "swap" is also true, then reverse the sense of "inleaf".
  1180. */
  1181. state->inleaf = xfs_attr_leaf_figure_balance(state, blk1, blk2,
  1182. &count, &totallen);
  1183. if (swap)
  1184. state->inleaf = !state->inleaf;
  1185. /*
  1186. * Move any entries required from leaf to leaf:
  1187. */
  1188. if (count < be16_to_cpu(hdr1->count)) {
  1189. /*
  1190. * Figure the total bytes to be added to the destination leaf.
  1191. */
  1192. /* number entries being moved */
  1193. count = be16_to_cpu(hdr1->count) - count;
  1194. space = be16_to_cpu(hdr1->usedbytes) - totallen;
  1195. space += count * sizeof(xfs_attr_leaf_entry_t);
  1196. /*
  1197. * leaf2 is the destination, compact it if it looks tight.
  1198. */
  1199. max = be16_to_cpu(hdr2->firstused)
  1200. - sizeof(xfs_attr_leaf_hdr_t);
  1201. max -= be16_to_cpu(hdr2->count) * sizeof(xfs_attr_leaf_entry_t);
  1202. if (space > max) {
  1203. xfs_attr_leaf_compact(args->trans, blk2->bp);
  1204. }
  1205. /*
  1206. * Move high entries from leaf1 to low end of leaf2.
  1207. */
  1208. xfs_attr_leaf_moveents(leaf1, be16_to_cpu(hdr1->count) - count,
  1209. leaf2, 0, count, state->mp);
  1210. xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
  1211. xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
  1212. } else if (count > be16_to_cpu(hdr1->count)) {
  1213. /*
  1214. * I assert that since all callers pass in an empty
  1215. * second buffer, this code should never execute.
  1216. */
  1217. /*
  1218. * Figure the total bytes to be added to the destination leaf.
  1219. */
  1220. /* number entries being moved */
  1221. count -= be16_to_cpu(hdr1->count);
  1222. space = totallen - be16_to_cpu(hdr1->usedbytes);
  1223. space += count * sizeof(xfs_attr_leaf_entry_t);
  1224. /*
  1225. * leaf1 is the destination, compact it if it looks tight.
  1226. */
  1227. max = be16_to_cpu(hdr1->firstused)
  1228. - sizeof(xfs_attr_leaf_hdr_t);
  1229. max -= be16_to_cpu(hdr1->count) * sizeof(xfs_attr_leaf_entry_t);
  1230. if (space > max) {
  1231. xfs_attr_leaf_compact(args->trans, blk1->bp);
  1232. }
  1233. /*
  1234. * Move low entries from leaf2 to high end of leaf1.
  1235. */
  1236. xfs_attr_leaf_moveents(leaf2, 0, leaf1,
  1237. be16_to_cpu(hdr1->count), count, state->mp);
  1238. xfs_da_log_buf(args->trans, blk1->bp, 0, state->blocksize-1);
  1239. xfs_da_log_buf(args->trans, blk2->bp, 0, state->blocksize-1);
  1240. }
  1241. /*
  1242. * Copy out last hashval in each block for B-tree code.
  1243. */
  1244. blk1->hashval = be32_to_cpu(
  1245. leaf1->entries[be16_to_cpu(leaf1->hdr.count)-1].hashval);
  1246. blk2->hashval = be32_to_cpu(
  1247. leaf2->entries[be16_to_cpu(leaf2->hdr.count)-1].hashval);
  1248. /*
  1249. * Adjust the expected index for insertion.
  1250. * NOTE: this code depends on the (current) situation that the
  1251. * second block was originally empty.
  1252. *
  1253. * If the insertion point moved to the 2nd block, we must adjust
  1254. * the index. We must also track the entry just following the
  1255. * new entry for use in an "atomic rename" operation, that entry
  1256. * is always the "old" entry and the "new" entry is what we are
  1257. * inserting. The index/blkno fields refer to the "old" entry,
  1258. * while the index2/blkno2 fields refer to the "new" entry.
  1259. */
  1260. if (blk1->index > be16_to_cpu(leaf1->hdr.count)) {
  1261. ASSERT(state->inleaf == 0);
  1262. blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
  1263. args->index = args->index2 = blk2->index;
  1264. args->blkno = args->blkno2 = blk2->blkno;
  1265. } else if (blk1->index == be16_to_cpu(leaf1->hdr.count)) {
  1266. if (state->inleaf) {
  1267. args->index = blk1->index;
  1268. args->blkno = blk1->blkno;
  1269. args->index2 = 0;
  1270. args->blkno2 = blk2->blkno;
  1271. } else {
  1272. blk2->index = blk1->index
  1273. - be16_to_cpu(leaf1->hdr.count);
  1274. args->index = args->index2 = blk2->index;
  1275. args->blkno = args->blkno2 = blk2->blkno;
  1276. }
  1277. } else {
  1278. ASSERT(state->inleaf == 1);
  1279. args->index = args->index2 = blk1->index;
  1280. args->blkno = args->blkno2 = blk1->blkno;
  1281. }
  1282. }
  1283. /*
  1284. * Examine entries until we reduce the absolute difference in
  1285. * byte usage between the two blocks to a minimum.
  1286. * GROT: Is this really necessary? With other than a 512 byte blocksize,
  1287. * GROT: there will always be enough room in either block for a new entry.
  1288. * GROT: Do a double-split for this case?
  1289. */
  1290. STATIC int
  1291. xfs_attr_leaf_figure_balance(xfs_da_state_t *state,
  1292. xfs_da_state_blk_t *blk1,
  1293. xfs_da_state_blk_t *blk2,
  1294. int *countarg, int *usedbytesarg)
  1295. {
  1296. xfs_attr_leafblock_t *leaf1, *leaf2;
  1297. xfs_attr_leaf_hdr_t *hdr1, *hdr2;
  1298. xfs_attr_leaf_entry_t *entry;
  1299. int count, max, index, totallen, half;
  1300. int lastdelta, foundit, tmp;
  1301. /*
  1302. * Set up environment.
  1303. */
  1304. leaf1 = blk1->bp->data;
  1305. leaf2 = blk2->bp->data;
  1306. hdr1 = &leaf1->hdr;
  1307. hdr2 = &leaf2->hdr;
  1308. foundit = 0;
  1309. totallen = 0;
  1310. /*
  1311. * Examine entries until we reduce the absolute difference in
  1312. * byte usage between the two blocks to a minimum.
  1313. */
  1314. max = be16_to_cpu(hdr1->count) + be16_to_cpu(hdr2->count);
  1315. half = (max+1) * sizeof(*entry);
  1316. half += be16_to_cpu(hdr1->usedbytes) +
  1317. be16_to_cpu(hdr2->usedbytes) +
  1318. xfs_attr_leaf_newentsize(
  1319. state->args->namelen,
  1320. state->args->valuelen,
  1321. state->blocksize, NULL);
  1322. half /= 2;
  1323. lastdelta = state->blocksize;
  1324. entry = &leaf1->entries[0];
  1325. for (count = index = 0; count < max; entry++, index++, count++) {
  1326. #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
  1327. /*
  1328. * The new entry is in the first block, account for it.
  1329. */
  1330. if (count == blk1->index) {
  1331. tmp = totallen + sizeof(*entry) +
  1332. xfs_attr_leaf_newentsize(
  1333. state->args->namelen,
  1334. state->args->valuelen,
  1335. state->blocksize, NULL);
  1336. if (XFS_ATTR_ABS(half - tmp) > lastdelta)
  1337. break;
  1338. lastdelta = XFS_ATTR_ABS(half - tmp);
  1339. totallen = tmp;
  1340. foundit = 1;
  1341. }
  1342. /*
  1343. * Wrap around into the second block if necessary.
  1344. */
  1345. if (count == be16_to_cpu(hdr1->count)) {
  1346. leaf1 = leaf2;
  1347. entry = &leaf1->entries[0];
  1348. index = 0;
  1349. }
  1350. /*
  1351. * Figure out if next leaf entry would be too much.
  1352. */
  1353. tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
  1354. index);
  1355. if (XFS_ATTR_ABS(half - tmp) > lastdelta)
  1356. break;
  1357. lastdelta = XFS_ATTR_ABS(half - tmp);
  1358. totallen = tmp;
  1359. #undef XFS_ATTR_ABS
  1360. }
  1361. /*
  1362. * Calculate the number of usedbytes that will end up in lower block.
  1363. * If new entry not in lower block, fix up the count.
  1364. */
  1365. totallen -= count * sizeof(*entry);
  1366. if (foundit) {
  1367. totallen -= sizeof(*entry) +
  1368. xfs_attr_leaf_newentsize(
  1369. state->args->namelen,
  1370. state->args->valuelen,
  1371. state->blocksize, NULL);
  1372. }
  1373. *countarg = count;
  1374. *usedbytesarg = totallen;
  1375. return(foundit);
  1376. }
  1377. /*========================================================================
  1378. * Routines used for shrinking the Btree.
  1379. *========================================================================*/
  1380. /*
  1381. * Check a leaf block and its neighbors to see if the block should be
  1382. * collapsed into one or the other neighbor. Always keep the block
  1383. * with the smaller block number.
  1384. * If the current block is over 50% full, don't try to join it, return 0.
  1385. * If the block is empty, fill in the state structure and return 2.
  1386. * If it can be collapsed, fill in the state structure and return 1.
  1387. * If nothing can be done, return 0.
  1388. *
  1389. * GROT: allow for INCOMPLETE entries in calculation.
  1390. */
  1391. int
  1392. xfs_attr_leaf_toosmall(xfs_da_state_t *state, int *action)
  1393. {
  1394. xfs_attr_leafblock_t *leaf;
  1395. xfs_da_state_blk_t *blk;
  1396. xfs_da_blkinfo_t *info;
  1397. int count, bytes, forward, error, retval, i;
  1398. xfs_dablk_t blkno;
  1399. xfs_dabuf_t *bp;
  1400. /*
  1401. * Check for the degenerate case of the block being over 50% full.
  1402. * If so, it's not worth even looking to see if we might be able
  1403. * to coalesce with a sibling.
  1404. */
  1405. blk = &state->path.blk[ state->path.active-1 ];
  1406. info = blk->bp->data;
  1407. ASSERT(be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC);
  1408. leaf = (xfs_attr_leafblock_t *)info;
  1409. count = be16_to_cpu(leaf->hdr.count);
  1410. bytes = sizeof(xfs_attr_leaf_hdr_t) +
  1411. count * sizeof(xfs_attr_leaf_entry_t) +
  1412. be16_to_cpu(leaf->hdr.usedbytes);
  1413. if (bytes > (state->blocksize >> 1)) {
  1414. *action = 0; /* blk over 50%, don't try to join */
  1415. return(0);
  1416. }
  1417. /*
  1418. * Check for the degenerate case of the block being empty.
  1419. * If the block is empty, we'll simply delete it, no need to
  1420. * coalesce it with a sibling block. We choose (arbitrarily)
  1421. * to merge with the forward block unless it is NULL.
  1422. */
  1423. if (count == 0) {
  1424. /*
  1425. * Make altpath point to the block we want to keep and
  1426. * path point to the block we want to drop (this one).
  1427. */
  1428. forward = (info->forw != 0);
  1429. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1430. error = xfs_da_path_shift(state, &state->altpath, forward,
  1431. 0, &retval);
  1432. if (error)
  1433. return(error);
  1434. if (retval) {
  1435. *action = 0;
  1436. } else {
  1437. *action = 2;
  1438. }
  1439. return(0);
  1440. }
  1441. /*
  1442. * Examine each sibling block to see if we can coalesce with
  1443. * at least 25% free space to spare. We need to figure out
  1444. * whether to merge with the forward or the backward block.
  1445. * We prefer coalescing with the lower numbered sibling so as
  1446. * to shrink an attribute list over time.
  1447. */
  1448. /* start with smaller blk num */
  1449. forward = (be32_to_cpu(info->forw) < be32_to_cpu(info->back));
  1450. for (i = 0; i < 2; forward = !forward, i++) {
  1451. if (forward)
  1452. blkno = be32_to_cpu(info->forw);
  1453. else
  1454. blkno = be32_to_cpu(info->back);
  1455. if (blkno == 0)
  1456. continue;
  1457. error = xfs_da_read_buf(state->args->trans, state->args->dp,
  1458. blkno, -1, &bp, XFS_ATTR_FORK);
  1459. if (error)
  1460. return(error);
  1461. ASSERT(bp != NULL);
  1462. leaf = (xfs_attr_leafblock_t *)info;
  1463. count = be16_to_cpu(leaf->hdr.count);
  1464. bytes = state->blocksize - (state->blocksize>>2);
  1465. bytes -= be16_to_cpu(leaf->hdr.usedbytes);
  1466. leaf = bp->data;
  1467. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  1468. count += be16_to_cpu(leaf->hdr.count);
  1469. bytes -= be16_to_cpu(leaf->hdr.usedbytes);
  1470. bytes -= count * sizeof(xfs_attr_leaf_entry_t);
  1471. bytes -= sizeof(xfs_attr_leaf_hdr_t);
  1472. xfs_da_brelse(state->args->trans, bp);
  1473. if (bytes >= 0)
  1474. break; /* fits with at least 25% to spare */
  1475. }
  1476. if (i >= 2) {
  1477. *action = 0;
  1478. return(0);
  1479. }
  1480. /*
  1481. * Make altpath point to the block we want to keep (the lower
  1482. * numbered block) and path point to the block we want to drop.
  1483. */
  1484. memcpy(&state->altpath, &state->path, sizeof(state->path));
  1485. if (blkno < blk->blkno) {
  1486. error = xfs_da_path_shift(state, &state->altpath, forward,
  1487. 0, &retval);
  1488. } else {
  1489. error = xfs_da_path_shift(state, &state->path, forward,
  1490. 0, &retval);
  1491. }
  1492. if (error)
  1493. return(error);
  1494. if (retval) {
  1495. *action = 0;
  1496. } else {
  1497. *action = 1;
  1498. }
  1499. return(0);
  1500. }
  1501. /*
  1502. * Remove a name from the leaf attribute list structure.
  1503. *
  1504. * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
  1505. * If two leaves are 37% full, when combined they will leave 25% free.
  1506. */
  1507. int
  1508. xfs_attr_leaf_remove(xfs_dabuf_t *bp, xfs_da_args_t *args)
  1509. {
  1510. xfs_attr_leafblock_t *leaf;
  1511. xfs_attr_leaf_hdr_t *hdr;
  1512. xfs_attr_leaf_map_t *map;
  1513. xfs_attr_leaf_entry_t *entry;
  1514. int before, after, smallest, entsize;
  1515. int tablesize, tmp, i;
  1516. xfs_mount_t *mp;
  1517. leaf = bp->data;
  1518. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  1519. hdr = &leaf->hdr;
  1520. mp = args->trans->t_mountp;
  1521. ASSERT((be16_to_cpu(hdr->count) > 0)
  1522. && (be16_to_cpu(hdr->count) < (XFS_LBSIZE(mp)/8)));
  1523. ASSERT((args->index >= 0)
  1524. && (args->index < be16_to_cpu(hdr->count)));
  1525. ASSERT(be16_to_cpu(hdr->firstused) >=
  1526. ((be16_to_cpu(hdr->count) * sizeof(*entry)) + sizeof(*hdr)));
  1527. entry = &leaf->entries[args->index];
  1528. ASSERT(be16_to_cpu(entry->nameidx) >= be16_to_cpu(hdr->firstused));
  1529. ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
  1530. /*
  1531. * Scan through free region table:
  1532. * check for adjacency of free'd entry with an existing one,
  1533. * find smallest free region in case we need to replace it,
  1534. * adjust any map that borders the entry table,
  1535. */
  1536. tablesize = be16_to_cpu(hdr->count) * sizeof(xfs_attr_leaf_entry_t)
  1537. + sizeof(xfs_attr_leaf_hdr_t);
  1538. map = &hdr->freemap[0];
  1539. tmp = be16_to_cpu(map->size);
  1540. before = after = -1;
  1541. smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
  1542. entsize = xfs_attr_leaf_entsize(leaf, args->index);
  1543. for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; map++, i++) {
  1544. ASSERT(be16_to_cpu(map->base) < XFS_LBSIZE(mp));
  1545. ASSERT(be16_to_cpu(map->size) < XFS_LBSIZE(mp));
  1546. if (be16_to_cpu(map->base) == tablesize) {
  1547. be16_add_cpu(&map->base,
  1548. -((int)sizeof(xfs_attr_leaf_entry_t)));
  1549. be16_add_cpu(&map->size, sizeof(xfs_attr_leaf_entry_t));
  1550. }
  1551. if ((be16_to_cpu(map->base) + be16_to_cpu(map->size))
  1552. == be16_to_cpu(entry->nameidx)) {
  1553. before = i;
  1554. } else if (be16_to_cpu(map->base)
  1555. == (be16_to_cpu(entry->nameidx) + entsize)) {
  1556. after = i;
  1557. } else if (be16_to_cpu(map->size) < tmp) {
  1558. tmp = be16_to_cpu(map->size);
  1559. smallest = i;
  1560. }
  1561. }
  1562. /*
  1563. * Coalesce adjacent freemap regions,
  1564. * or replace the smallest region.
  1565. */
  1566. if ((before >= 0) || (after >= 0)) {
  1567. if ((before >= 0) && (after >= 0)) {
  1568. map = &hdr->freemap[before];
  1569. be16_add_cpu(&map->size, entsize);
  1570. be16_add_cpu(&map->size,
  1571. be16_to_cpu(hdr->freemap[after].size));
  1572. hdr->freemap[after].base = 0;
  1573. hdr->freemap[after].size = 0;
  1574. } else if (before >= 0) {
  1575. map = &hdr->freemap[before];
  1576. be16_add_cpu(&map->size, entsize);
  1577. } else {
  1578. map = &hdr->freemap[after];
  1579. /* both on-disk, don't endian flip twice */
  1580. map->base = entry->nameidx;
  1581. be16_add_cpu(&map->size, entsize);
  1582. }
  1583. } else {
  1584. /*
  1585. * Replace smallest region (if it is smaller than free'd entry)
  1586. */
  1587. map = &hdr->freemap[smallest];
  1588. if (be16_to_cpu(map->size) < entsize) {
  1589. map->base = cpu_to_be16(be16_to_cpu(entry->nameidx));
  1590. map->size = cpu_to_be16(entsize);
  1591. }
  1592. }
  1593. /*
  1594. * Did we remove the first entry?
  1595. */
  1596. if (be16_to_cpu(entry->nameidx) == be16_to_cpu(hdr->firstused))
  1597. smallest = 1;
  1598. else
  1599. smallest = 0;
  1600. /*
  1601. * Compress the remaining entries and zero out the removed stuff.
  1602. */
  1603. memset(xfs_attr_leaf_name(leaf, args->index), 0, entsize);
  1604. be16_add_cpu(&hdr->usedbytes, -entsize);
  1605. xfs_da_log_buf(args->trans, bp,
  1606. XFS_DA_LOGRANGE(leaf, xfs_attr_leaf_name(leaf, args->index),
  1607. entsize));
  1608. tmp = (be16_to_cpu(hdr->count) - args->index)
  1609. * sizeof(xfs_attr_leaf_entry_t);
  1610. memmove((char *)entry, (char *)(entry+1), tmp);
  1611. be16_add_cpu(&hdr->count, -1);
  1612. xfs_da_log_buf(args->trans, bp,
  1613. XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
  1614. entry = &leaf->entries[be16_to_cpu(hdr->count)];
  1615. memset((char *)entry, 0, sizeof(xfs_attr_leaf_entry_t));
  1616. /*
  1617. * If we removed the first entry, re-find the first used byte
  1618. * in the name area. Note that if the entry was the "firstused",
  1619. * then we don't have a "hole" in our block resulting from
  1620. * removing the name.
  1621. */
  1622. if (smallest) {
  1623. tmp = XFS_LBSIZE(mp);
  1624. entry = &leaf->entries[0];
  1625. for (i = be16_to_cpu(hdr->count)-1; i >= 0; entry++, i--) {
  1626. ASSERT(be16_to_cpu(entry->nameidx) >=
  1627. be16_to_cpu(hdr->firstused));
  1628. ASSERT(be16_to_cpu(entry->nameidx) < XFS_LBSIZE(mp));
  1629. if (be16_to_cpu(entry->nameidx) < tmp)
  1630. tmp = be16_to_cpu(entry->nameidx);
  1631. }
  1632. hdr->firstused = cpu_to_be16(tmp);
  1633. if (!hdr->firstused) {
  1634. hdr->firstused = cpu_to_be16(
  1635. tmp - XFS_ATTR_LEAF_NAME_ALIGN);
  1636. }
  1637. } else {
  1638. hdr->holes = 1; /* mark as needing compaction */
  1639. }
  1640. xfs_da_log_buf(args->trans, bp,
  1641. XFS_DA_LOGRANGE(leaf, hdr, sizeof(*hdr)));
  1642. /*
  1643. * Check if leaf is less than 50% full, caller may want to
  1644. * "join" the leaf with a sibling if so.
  1645. */
  1646. tmp = sizeof(xfs_attr_leaf_hdr_t);
  1647. tmp += be16_to_cpu(leaf->hdr.count) * sizeof(xfs_attr_leaf_entry_t);
  1648. tmp += be16_to_cpu(leaf->hdr.usedbytes);
  1649. return(tmp < mp->m_attr_magicpct); /* leaf is < 37% full */
  1650. }
  1651. /*
  1652. * Move all the attribute list entries from drop_leaf into save_leaf.
  1653. */
  1654. void
  1655. xfs_attr_leaf_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
  1656. xfs_da_state_blk_t *save_blk)
  1657. {
  1658. xfs_attr_leafblock_t *drop_leaf, *save_leaf, *tmp_leaf;
  1659. xfs_attr_leaf_hdr_t *drop_hdr, *save_hdr, *tmp_hdr;
  1660. xfs_mount_t *mp;
  1661. char *tmpbuffer;
  1662. /*
  1663. * Set up environment.
  1664. */
  1665. mp = state->mp;
  1666. ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC);
  1667. ASSERT(save_blk->magic == XFS_ATTR_LEAF_MAGIC);
  1668. drop_leaf = drop_blk->bp->data;
  1669. save_leaf = save_blk->bp->data;
  1670. ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  1671. ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  1672. drop_hdr = &drop_leaf->hdr;
  1673. save_hdr = &save_leaf->hdr;
  1674. /*
  1675. * Save last hashval from dying block for later Btree fixup.
  1676. */
  1677. drop_blk->hashval = be32_to_cpu(
  1678. drop_leaf->entries[be16_to_cpu(drop_leaf->hdr.count)-1].hashval);
  1679. /*
  1680. * Check if we need a temp buffer, or can we do it in place.
  1681. * Note that we don't check "leaf" for holes because we will
  1682. * always be dropping it, toosmall() decided that for us already.
  1683. */
  1684. if (save_hdr->holes == 0) {
  1685. /*
  1686. * dest leaf has no holes, so we add there. May need
  1687. * to make some room in the entry array.
  1688. */
  1689. if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
  1690. xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf, 0,
  1691. be16_to_cpu(drop_hdr->count), mp);
  1692. } else {
  1693. xfs_attr_leaf_moveents(drop_leaf, 0, save_leaf,
  1694. be16_to_cpu(save_hdr->count),
  1695. be16_to_cpu(drop_hdr->count), mp);
  1696. }
  1697. } else {
  1698. /*
  1699. * Destination has holes, so we make a temporary copy
  1700. * of the leaf and add them both to that.
  1701. */
  1702. tmpbuffer = kmem_alloc(state->blocksize, KM_SLEEP);
  1703. ASSERT(tmpbuffer != NULL);
  1704. memset(tmpbuffer, 0, state->blocksize);
  1705. tmp_leaf = (xfs_attr_leafblock_t *)tmpbuffer;
  1706. tmp_hdr = &tmp_leaf->hdr;
  1707. tmp_hdr->info = save_hdr->info; /* struct copy */
  1708. tmp_hdr->count = 0;
  1709. tmp_hdr->firstused = cpu_to_be16(state->blocksize);
  1710. if (!tmp_hdr->firstused) {
  1711. tmp_hdr->firstused = cpu_to_be16(
  1712. state->blocksize - XFS_ATTR_LEAF_NAME_ALIGN);
  1713. }
  1714. tmp_hdr->usedbytes = 0;
  1715. if (xfs_attr_leaf_order(save_blk->bp, drop_blk->bp)) {
  1716. xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf, 0,
  1717. be16_to_cpu(drop_hdr->count), mp);
  1718. xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf,
  1719. be16_to_cpu(tmp_leaf->hdr.count),
  1720. be16_to_cpu(save_hdr->count), mp);
  1721. } else {
  1722. xfs_attr_leaf_moveents(save_leaf, 0, tmp_leaf, 0,
  1723. be16_to_cpu(save_hdr->count), mp);
  1724. xfs_attr_leaf_moveents(drop_leaf, 0, tmp_leaf,
  1725. be16_to_cpu(tmp_leaf->hdr.count),
  1726. be16_to_cpu(drop_hdr->count), mp);
  1727. }
  1728. memcpy((char *)save_leaf, (char *)tmp_leaf, state->blocksize);
  1729. kmem_free(tmpbuffer);
  1730. }
  1731. xfs_da_log_buf(state->args->trans, save_blk->bp, 0,
  1732. state->blocksize - 1);
  1733. /*
  1734. * Copy out last hashval in each block for B-tree code.
  1735. */
  1736. save_blk->hashval = be32_to_cpu(
  1737. save_leaf->entries[be16_to_cpu(save_leaf->hdr.count)-1].hashval);
  1738. }
  1739. /*========================================================================
  1740. * Routines used for finding things in the Btree.
  1741. *========================================================================*/
  1742. /*
  1743. * Look up a name in a leaf attribute list structure.
  1744. * This is the internal routine, it uses the caller's buffer.
  1745. *
  1746. * Note that duplicate keys are allowed, but only check within the
  1747. * current leaf node. The Btree code must check in adjacent leaf nodes.
  1748. *
  1749. * Return in args->index the index into the entry[] array of either
  1750. * the found entry, or where the entry should have been (insert before
  1751. * that entry).
  1752. *
  1753. * Don't change the args->value unless we find the attribute.
  1754. */
  1755. int
  1756. xfs_attr_leaf_lookup_int(xfs_dabuf_t *bp, xfs_da_args_t *args)
  1757. {
  1758. xfs_attr_leafblock_t *leaf;
  1759. xfs_attr_leaf_entry_t *entry;
  1760. xfs_attr_leaf_name_local_t *name_loc;
  1761. xfs_attr_leaf_name_remote_t *name_rmt;
  1762. int probe, span;
  1763. xfs_dahash_t hashval;
  1764. leaf = bp->data;
  1765. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  1766. ASSERT(be16_to_cpu(leaf->hdr.count)
  1767. < (XFS_LBSIZE(args->dp->i_mount)/8));
  1768. /*
  1769. * Binary search. (note: small blocks will skip this loop)
  1770. */
  1771. hashval = args->hashval;
  1772. probe = span = be16_to_cpu(leaf->hdr.count) / 2;
  1773. for (entry = &leaf->entries[probe]; span > 4;
  1774. entry = &leaf->entries[probe]) {
  1775. span /= 2;
  1776. if (be32_to_cpu(entry->hashval) < hashval)
  1777. probe += span;
  1778. else if (be32_to_cpu(entry->hashval) > hashval)
  1779. probe -= span;
  1780. else
  1781. break;
  1782. }
  1783. ASSERT((probe >= 0) &&
  1784. (!leaf->hdr.count
  1785. || (probe < be16_to_cpu(leaf->hdr.count))));
  1786. ASSERT((span <= 4) || (be32_to_cpu(entry->hashval) == hashval));
  1787. /*
  1788. * Since we may have duplicate hashval's, find the first matching
  1789. * hashval in the leaf.
  1790. */
  1791. while ((probe > 0) && (be32_to_cpu(entry->hashval) >= hashval)) {
  1792. entry--;
  1793. probe--;
  1794. }
  1795. while ((probe < be16_to_cpu(leaf->hdr.count)) &&
  1796. (be32_to_cpu(entry->hashval) < hashval)) {
  1797. entry++;
  1798. probe++;
  1799. }
  1800. if ((probe == be16_to_cpu(leaf->hdr.count)) ||
  1801. (be32_to_cpu(entry->hashval) != hashval)) {
  1802. args->index = probe;
  1803. return(XFS_ERROR(ENOATTR));
  1804. }
  1805. /*
  1806. * Duplicate keys may be present, so search all of them for a match.
  1807. */
  1808. for ( ; (probe < be16_to_cpu(leaf->hdr.count)) &&
  1809. (be32_to_cpu(entry->hashval) == hashval);
  1810. entry++, probe++) {
  1811. /*
  1812. * GROT: Add code to remove incomplete entries.
  1813. */
  1814. /*
  1815. * If we are looking for INCOMPLETE entries, show only those.
  1816. * If we are looking for complete entries, show only those.
  1817. */
  1818. if ((args->flags & XFS_ATTR_INCOMPLETE) !=
  1819. (entry->flags & XFS_ATTR_INCOMPLETE)) {
  1820. continue;
  1821. }
  1822. if (entry->flags & XFS_ATTR_LOCAL) {
  1823. name_loc = xfs_attr_leaf_name_local(leaf, probe);
  1824. if (name_loc->namelen != args->namelen)
  1825. continue;
  1826. if (memcmp(args->name, (char *)name_loc->nameval, args->namelen) != 0)
  1827. continue;
  1828. if (!xfs_attr_namesp_match(args->flags, entry->flags))
  1829. continue;
  1830. args->index = probe;
  1831. return(XFS_ERROR(EEXIST));
  1832. } else {
  1833. name_rmt = xfs_attr_leaf_name_remote(leaf, probe);
  1834. if (name_rmt->namelen != args->namelen)
  1835. continue;
  1836. if (memcmp(args->name, (char *)name_rmt->name,
  1837. args->namelen) != 0)
  1838. continue;
  1839. if (!xfs_attr_namesp_match(args->flags, entry->flags))
  1840. continue;
  1841. args->index = probe;
  1842. args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
  1843. args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount,
  1844. be32_to_cpu(name_rmt->valuelen));
  1845. return(XFS_ERROR(EEXIST));
  1846. }
  1847. }
  1848. args->index = probe;
  1849. return(XFS_ERROR(ENOATTR));
  1850. }
  1851. /*
  1852. * Get the value associated with an attribute name from a leaf attribute
  1853. * list structure.
  1854. */
  1855. int
  1856. xfs_attr_leaf_getvalue(xfs_dabuf_t *bp, xfs_da_args_t *args)
  1857. {
  1858. int valuelen;
  1859. xfs_attr_leafblock_t *leaf;
  1860. xfs_attr_leaf_entry_t *entry;
  1861. xfs_attr_leaf_name_local_t *name_loc;
  1862. xfs_attr_leaf_name_remote_t *name_rmt;
  1863. leaf = bp->data;
  1864. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  1865. ASSERT(be16_to_cpu(leaf->hdr.count)
  1866. < (XFS_LBSIZE(args->dp->i_mount)/8));
  1867. ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
  1868. entry = &leaf->entries[args->index];
  1869. if (entry->flags & XFS_ATTR_LOCAL) {
  1870. name_loc = xfs_attr_leaf_name_local(leaf, args->index);
  1871. ASSERT(name_loc->namelen == args->namelen);
  1872. ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
  1873. valuelen = be16_to_cpu(name_loc->valuelen);
  1874. if (args->flags & ATTR_KERNOVAL) {
  1875. args->valuelen = valuelen;
  1876. return(0);
  1877. }
  1878. if (args->valuelen < valuelen) {
  1879. args->valuelen = valuelen;
  1880. return(XFS_ERROR(ERANGE));
  1881. }
  1882. args->valuelen = valuelen;
  1883. memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
  1884. } else {
  1885. name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
  1886. ASSERT(name_rmt->namelen == args->namelen);
  1887. ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
  1888. valuelen = be32_to_cpu(name_rmt->valuelen);
  1889. args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
  1890. args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen);
  1891. if (args->flags & ATTR_KERNOVAL) {
  1892. args->valuelen = valuelen;
  1893. return(0);
  1894. }
  1895. if (args->valuelen < valuelen) {
  1896. args->valuelen = valuelen;
  1897. return(XFS_ERROR(ERANGE));
  1898. }
  1899. args->valuelen = valuelen;
  1900. }
  1901. return(0);
  1902. }
  1903. /*========================================================================
  1904. * Utility routines.
  1905. *========================================================================*/
  1906. /*
  1907. * Move the indicated entries from one leaf to another.
  1908. * NOTE: this routine modifies both source and destination leaves.
  1909. */
  1910. /*ARGSUSED*/
  1911. STATIC void
  1912. xfs_attr_leaf_moveents(xfs_attr_leafblock_t *leaf_s, int start_s,
  1913. xfs_attr_leafblock_t *leaf_d, int start_d,
  1914. int count, xfs_mount_t *mp)
  1915. {
  1916. xfs_attr_leaf_hdr_t *hdr_s, *hdr_d;
  1917. xfs_attr_leaf_entry_t *entry_s, *entry_d;
  1918. int desti, tmp, i;
  1919. /*
  1920. * Check for nothing to do.
  1921. */
  1922. if (count == 0)
  1923. return;
  1924. /*
  1925. * Set up environment.
  1926. */
  1927. ASSERT(be16_to_cpu(leaf_s->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  1928. ASSERT(be16_to_cpu(leaf_d->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  1929. hdr_s = &leaf_s->hdr;
  1930. hdr_d = &leaf_d->hdr;
  1931. ASSERT((be16_to_cpu(hdr_s->count) > 0) &&
  1932. (be16_to_cpu(hdr_s->count) < (XFS_LBSIZE(mp)/8)));
  1933. ASSERT(be16_to_cpu(hdr_s->firstused) >=
  1934. ((be16_to_cpu(hdr_s->count)
  1935. * sizeof(*entry_s))+sizeof(*hdr_s)));
  1936. ASSERT(be16_to_cpu(hdr_d->count) < (XFS_LBSIZE(mp)/8));
  1937. ASSERT(be16_to_cpu(hdr_d->firstused) >=
  1938. ((be16_to_cpu(hdr_d->count)
  1939. * sizeof(*entry_d))+sizeof(*hdr_d)));
  1940. ASSERT(start_s < be16_to_cpu(hdr_s->count));
  1941. ASSERT(start_d <= be16_to_cpu(hdr_d->count));
  1942. ASSERT(count <= be16_to_cpu(hdr_s->count));
  1943. /*
  1944. * Move the entries in the destination leaf up to make a hole?
  1945. */
  1946. if (start_d < be16_to_cpu(hdr_d->count)) {
  1947. tmp = be16_to_cpu(hdr_d->count) - start_d;
  1948. tmp *= sizeof(xfs_attr_leaf_entry_t);
  1949. entry_s = &leaf_d->entries[start_d];
  1950. entry_d = &leaf_d->entries[start_d + count];
  1951. memmove((char *)entry_d, (char *)entry_s, tmp);
  1952. }
  1953. /*
  1954. * Copy all entry's in the same (sorted) order,
  1955. * but allocate attribute info packed and in sequence.
  1956. */
  1957. entry_s = &leaf_s->entries[start_s];
  1958. entry_d = &leaf_d->entries[start_d];
  1959. desti = start_d;
  1960. for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
  1961. ASSERT(be16_to_cpu(entry_s->nameidx)
  1962. >= be16_to_cpu(hdr_s->firstused));
  1963. tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
  1964. #ifdef GROT
  1965. /*
  1966. * Code to drop INCOMPLETE entries. Difficult to use as we
  1967. * may also need to change the insertion index. Code turned
  1968. * off for 6.2, should be revisited later.
  1969. */
  1970. if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
  1971. memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
  1972. be16_add_cpu(&hdr_s->usedbytes, -tmp);
  1973. be16_add_cpu(&hdr_s->count, -1);
  1974. entry_d--; /* to compensate for ++ in loop hdr */
  1975. desti--;
  1976. if ((start_s + i) < offset)
  1977. result++; /* insertion index adjustment */
  1978. } else {
  1979. #endif /* GROT */
  1980. be16_add_cpu(&hdr_d->firstused, -tmp);
  1981. /* both on-disk, don't endian flip twice */
  1982. entry_d->hashval = entry_s->hashval;
  1983. /* both on-disk, don't endian flip twice */
  1984. entry_d->nameidx = hdr_d->firstused;
  1985. entry_d->flags = entry_s->flags;
  1986. ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
  1987. <= XFS_LBSIZE(mp));
  1988. memmove(xfs_attr_leaf_name(leaf_d, desti),
  1989. xfs_attr_leaf_name(leaf_s, start_s + i), tmp);
  1990. ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
  1991. <= XFS_LBSIZE(mp));
  1992. memset(xfs_attr_leaf_name(leaf_s, start_s + i), 0, tmp);
  1993. be16_add_cpu(&hdr_s->usedbytes, -tmp);
  1994. be16_add_cpu(&hdr_d->usedbytes, tmp);
  1995. be16_add_cpu(&hdr_s->count, -1);
  1996. be16_add_cpu(&hdr_d->count, 1);
  1997. tmp = be16_to_cpu(hdr_d->count)
  1998. * sizeof(xfs_attr_leaf_entry_t)
  1999. + sizeof(xfs_attr_leaf_hdr_t);
  2000. ASSERT(be16_to_cpu(hdr_d->firstused) >= tmp);
  2001. #ifdef GROT
  2002. }
  2003. #endif /* GROT */
  2004. }
  2005. /*
  2006. * Zero out the entries we just copied.
  2007. */
  2008. if (start_s == be16_to_cpu(hdr_s->count)) {
  2009. tmp = count * sizeof(xfs_attr_leaf_entry_t);
  2010. entry_s = &leaf_s->entries[start_s];
  2011. ASSERT(((char *)entry_s + tmp) <=
  2012. ((char *)leaf_s + XFS_LBSIZE(mp)));
  2013. memset((char *)entry_s, 0, tmp);
  2014. } else {
  2015. /*
  2016. * Move the remaining entries down to fill the hole,
  2017. * then zero the entries at the top.
  2018. */
  2019. tmp = be16_to_cpu(hdr_s->count) - count;
  2020. tmp *= sizeof(xfs_attr_leaf_entry_t);
  2021. entry_s = &leaf_s->entries[start_s + count];
  2022. entry_d = &leaf_s->entries[start_s];
  2023. memmove((char *)entry_d, (char *)entry_s, tmp);
  2024. tmp = count * sizeof(xfs_attr_leaf_entry_t);
  2025. entry_s = &leaf_s->entries[be16_to_cpu(hdr_s->count)];
  2026. ASSERT(((char *)entry_s + tmp) <=
  2027. ((char *)leaf_s + XFS_LBSIZE(mp)));
  2028. memset((char *)entry_s, 0, tmp);
  2029. }
  2030. /*
  2031. * Fill in the freemap information
  2032. */
  2033. hdr_d->freemap[0].base = cpu_to_be16(sizeof(xfs_attr_leaf_hdr_t));
  2034. be16_add_cpu(&hdr_d->freemap[0].base, be16_to_cpu(hdr_d->count) *
  2035. sizeof(xfs_attr_leaf_entry_t));
  2036. hdr_d->freemap[0].size = cpu_to_be16(be16_to_cpu(hdr_d->firstused)
  2037. - be16_to_cpu(hdr_d->freemap[0].base));
  2038. hdr_d->freemap[1].base = 0;
  2039. hdr_d->freemap[2].base = 0;
  2040. hdr_d->freemap[1].size = 0;
  2041. hdr_d->freemap[2].size = 0;
  2042. hdr_s->holes = 1; /* leaf may not be compact */
  2043. }
  2044. /*
  2045. * Compare two leaf blocks "order".
  2046. * Return 0 unless leaf2 should go before leaf1.
  2047. */
  2048. int
  2049. xfs_attr_leaf_order(xfs_dabuf_t *leaf1_bp, xfs_dabuf_t *leaf2_bp)
  2050. {
  2051. xfs_attr_leafblock_t *leaf1, *leaf2;
  2052. leaf1 = leaf1_bp->data;
  2053. leaf2 = leaf2_bp->data;
  2054. ASSERT((be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC) &&
  2055. (be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC));
  2056. if ((be16_to_cpu(leaf1->hdr.count) > 0) &&
  2057. (be16_to_cpu(leaf2->hdr.count) > 0) &&
  2058. ((be32_to_cpu(leaf2->entries[0].hashval) <
  2059. be32_to_cpu(leaf1->entries[0].hashval)) ||
  2060. (be32_to_cpu(leaf2->entries[
  2061. be16_to_cpu(leaf2->hdr.count)-1].hashval) <
  2062. be32_to_cpu(leaf1->entries[
  2063. be16_to_cpu(leaf1->hdr.count)-1].hashval)))) {
  2064. return(1);
  2065. }
  2066. return(0);
  2067. }
  2068. /*
  2069. * Pick up the last hashvalue from a leaf block.
  2070. */
  2071. xfs_dahash_t
  2072. xfs_attr_leaf_lasthash(xfs_dabuf_t *bp, int *count)
  2073. {
  2074. xfs_attr_leafblock_t *leaf;
  2075. leaf = bp->data;
  2076. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  2077. if (count)
  2078. *count = be16_to_cpu(leaf->hdr.count);
  2079. if (!leaf->hdr.count)
  2080. return(0);
  2081. return be32_to_cpu(leaf->entries[be16_to_cpu(leaf->hdr.count)-1].hashval);
  2082. }
  2083. /*
  2084. * Calculate the number of bytes used to store the indicated attribute
  2085. * (whether local or remote only calculate bytes in this block).
  2086. */
  2087. STATIC int
  2088. xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
  2089. {
  2090. xfs_attr_leaf_name_local_t *name_loc;
  2091. xfs_attr_leaf_name_remote_t *name_rmt;
  2092. int size;
  2093. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  2094. if (leaf->entries[index].flags & XFS_ATTR_LOCAL) {
  2095. name_loc = xfs_attr_leaf_name_local(leaf, index);
  2096. size = xfs_attr_leaf_entsize_local(name_loc->namelen,
  2097. be16_to_cpu(name_loc->valuelen));
  2098. } else {
  2099. name_rmt = xfs_attr_leaf_name_remote(leaf, index);
  2100. size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
  2101. }
  2102. return(size);
  2103. }
  2104. /*
  2105. * Calculate the number of bytes that would be required to store the new
  2106. * attribute (whether local or remote only calculate bytes in this block).
  2107. * This routine decides as a side effect whether the attribute will be
  2108. * a "local" or a "remote" attribute.
  2109. */
  2110. int
  2111. xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize, int *local)
  2112. {
  2113. int size;
  2114. size = xfs_attr_leaf_entsize_local(namelen, valuelen);
  2115. if (size < xfs_attr_leaf_entsize_local_max(blocksize)) {
  2116. if (local) {
  2117. *local = 1;
  2118. }
  2119. } else {
  2120. size = xfs_attr_leaf_entsize_remote(namelen);
  2121. if (local) {
  2122. *local = 0;
  2123. }
  2124. }
  2125. return(size);
  2126. }
  2127. /*
  2128. * Copy out attribute list entries for attr_list(), for leaf attribute lists.
  2129. */
  2130. int
  2131. xfs_attr_leaf_list_int(xfs_dabuf_t *bp, xfs_attr_list_context_t *context)
  2132. {
  2133. attrlist_cursor_kern_t *cursor;
  2134. xfs_attr_leafblock_t *leaf;
  2135. xfs_attr_leaf_entry_t *entry;
  2136. int retval, i;
  2137. ASSERT(bp != NULL);
  2138. leaf = bp->data;
  2139. cursor = context->cursor;
  2140. cursor->initted = 1;
  2141. xfs_attr_trace_l_cl("blk start", context, leaf);
  2142. /*
  2143. * Re-find our place in the leaf block if this is a new syscall.
  2144. */
  2145. if (context->resynch) {
  2146. entry = &leaf->entries[0];
  2147. for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
  2148. if (be32_to_cpu(entry->hashval) == cursor->hashval) {
  2149. if (cursor->offset == context->dupcnt) {
  2150. context->dupcnt = 0;
  2151. break;
  2152. }
  2153. context->dupcnt++;
  2154. } else if (be32_to_cpu(entry->hashval) >
  2155. cursor->hashval) {
  2156. context->dupcnt = 0;
  2157. break;
  2158. }
  2159. }
  2160. if (i == be16_to_cpu(leaf->hdr.count)) {
  2161. xfs_attr_trace_l_c("not found", context);
  2162. return(0);
  2163. }
  2164. } else {
  2165. entry = &leaf->entries[0];
  2166. i = 0;
  2167. }
  2168. context->resynch = 0;
  2169. /*
  2170. * We have found our place, start copying out the new attributes.
  2171. */
  2172. retval = 0;
  2173. for ( ; (i < be16_to_cpu(leaf->hdr.count)); entry++, i++) {
  2174. if (be32_to_cpu(entry->hashval) != cursor->hashval) {
  2175. cursor->hashval = be32_to_cpu(entry->hashval);
  2176. cursor->offset = 0;
  2177. }
  2178. if (entry->flags & XFS_ATTR_INCOMPLETE)
  2179. continue; /* skip incomplete entries */
  2180. if (entry->flags & XFS_ATTR_LOCAL) {
  2181. xfs_attr_leaf_name_local_t *name_loc =
  2182. xfs_attr_leaf_name_local(leaf, i);
  2183. retval = context->put_listent(context,
  2184. entry->flags,
  2185. (char *)name_loc->nameval,
  2186. (int)name_loc->namelen,
  2187. be16_to_cpu(name_loc->valuelen),
  2188. (char *)&name_loc->nameval[name_loc->namelen]);
  2189. if (retval)
  2190. return retval;
  2191. } else {
  2192. xfs_attr_leaf_name_remote_t *name_rmt =
  2193. xfs_attr_leaf_name_remote(leaf, i);
  2194. int valuelen = be32_to_cpu(name_rmt->valuelen);
  2195. if (context->put_value) {
  2196. xfs_da_args_t args;
  2197. memset((char *)&args, 0, sizeof(args));
  2198. args.dp = context->dp;
  2199. args.whichfork = XFS_ATTR_FORK;
  2200. args.valuelen = valuelen;
  2201. args.value = kmem_alloc(valuelen, KM_SLEEP);
  2202. args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
  2203. args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen);
  2204. retval = xfs_attr_rmtval_get(&args);
  2205. if (retval)
  2206. return retval;
  2207. retval = context->put_listent(context,
  2208. entry->flags,
  2209. (char *)name_rmt->name,
  2210. (int)name_rmt->namelen,
  2211. valuelen,
  2212. (char*)args.value);
  2213. kmem_free(args.value);
  2214. } else {
  2215. retval = context->put_listent(context,
  2216. entry->flags,
  2217. (char *)name_rmt->name,
  2218. (int)name_rmt->namelen,
  2219. valuelen,
  2220. NULL);
  2221. }
  2222. if (retval)
  2223. return retval;
  2224. }
  2225. if (context->seen_enough)
  2226. break;
  2227. cursor->offset++;
  2228. }
  2229. xfs_attr_trace_l_cl("blk end", context, leaf);
  2230. return(retval);
  2231. }
  2232. /*========================================================================
  2233. * Manage the INCOMPLETE flag in a leaf entry
  2234. *========================================================================*/
  2235. /*
  2236. * Clear the INCOMPLETE flag on an entry in a leaf block.
  2237. */
  2238. int
  2239. xfs_attr_leaf_clearflag(xfs_da_args_t *args)
  2240. {
  2241. xfs_attr_leafblock_t *leaf;
  2242. xfs_attr_leaf_entry_t *entry;
  2243. xfs_attr_leaf_name_remote_t *name_rmt;
  2244. xfs_dabuf_t *bp;
  2245. int error;
  2246. #ifdef DEBUG
  2247. xfs_attr_leaf_name_local_t *name_loc;
  2248. int namelen;
  2249. char *name;
  2250. #endif /* DEBUG */
  2251. /*
  2252. * Set up the operation.
  2253. */
  2254. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
  2255. XFS_ATTR_FORK);
  2256. if (error) {
  2257. return(error);
  2258. }
  2259. ASSERT(bp != NULL);
  2260. leaf = bp->data;
  2261. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  2262. ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
  2263. ASSERT(args->index >= 0);
  2264. entry = &leaf->entries[ args->index ];
  2265. ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
  2266. #ifdef DEBUG
  2267. if (entry->flags & XFS_ATTR_LOCAL) {
  2268. name_loc = xfs_attr_leaf_name_local(leaf, args->index);
  2269. namelen = name_loc->namelen;
  2270. name = (char *)name_loc->nameval;
  2271. } else {
  2272. name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
  2273. namelen = name_rmt->namelen;
  2274. name = (char *)name_rmt->name;
  2275. }
  2276. ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
  2277. ASSERT(namelen == args->namelen);
  2278. ASSERT(memcmp(name, args->name, namelen) == 0);
  2279. #endif /* DEBUG */
  2280. entry->flags &= ~XFS_ATTR_INCOMPLETE;
  2281. xfs_da_log_buf(args->trans, bp,
  2282. XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
  2283. if (args->rmtblkno) {
  2284. ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
  2285. name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
  2286. name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
  2287. name_rmt->valuelen = cpu_to_be32(args->valuelen);
  2288. xfs_da_log_buf(args->trans, bp,
  2289. XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
  2290. }
  2291. xfs_da_buf_done(bp);
  2292. /*
  2293. * Commit the flag value change and start the next trans in series.
  2294. */
  2295. return xfs_trans_roll(&args->trans, args->dp);
  2296. }
  2297. /*
  2298. * Set the INCOMPLETE flag on an entry in a leaf block.
  2299. */
  2300. int
  2301. xfs_attr_leaf_setflag(xfs_da_args_t *args)
  2302. {
  2303. xfs_attr_leafblock_t *leaf;
  2304. xfs_attr_leaf_entry_t *entry;
  2305. xfs_attr_leaf_name_remote_t *name_rmt;
  2306. xfs_dabuf_t *bp;
  2307. int error;
  2308. /*
  2309. * Set up the operation.
  2310. */
  2311. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
  2312. XFS_ATTR_FORK);
  2313. if (error) {
  2314. return(error);
  2315. }
  2316. ASSERT(bp != NULL);
  2317. leaf = bp->data;
  2318. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  2319. ASSERT(args->index < be16_to_cpu(leaf->hdr.count));
  2320. ASSERT(args->index >= 0);
  2321. entry = &leaf->entries[ args->index ];
  2322. ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
  2323. entry->flags |= XFS_ATTR_INCOMPLETE;
  2324. xfs_da_log_buf(args->trans, bp,
  2325. XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
  2326. if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
  2327. name_rmt = xfs_attr_leaf_name_remote(leaf, args->index);
  2328. name_rmt->valueblk = 0;
  2329. name_rmt->valuelen = 0;
  2330. xfs_da_log_buf(args->trans, bp,
  2331. XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
  2332. }
  2333. xfs_da_buf_done(bp);
  2334. /*
  2335. * Commit the flag value change and start the next trans in series.
  2336. */
  2337. return xfs_trans_roll(&args->trans, args->dp);
  2338. }
  2339. /*
  2340. * In a single transaction, clear the INCOMPLETE flag on the leaf entry
  2341. * given by args->blkno/index and set the INCOMPLETE flag on the leaf
  2342. * entry given by args->blkno2/index2.
  2343. *
  2344. * Note that they could be in different blocks, or in the same block.
  2345. */
  2346. int
  2347. xfs_attr_leaf_flipflags(xfs_da_args_t *args)
  2348. {
  2349. xfs_attr_leafblock_t *leaf1, *leaf2;
  2350. xfs_attr_leaf_entry_t *entry1, *entry2;
  2351. xfs_attr_leaf_name_remote_t *name_rmt;
  2352. xfs_dabuf_t *bp1, *bp2;
  2353. int error;
  2354. #ifdef DEBUG
  2355. xfs_attr_leaf_name_local_t *name_loc;
  2356. int namelen1, namelen2;
  2357. char *name1, *name2;
  2358. #endif /* DEBUG */
  2359. /*
  2360. * Read the block containing the "old" attr
  2361. */
  2362. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp1,
  2363. XFS_ATTR_FORK);
  2364. if (error) {
  2365. return(error);
  2366. }
  2367. ASSERT(bp1 != NULL);
  2368. /*
  2369. * Read the block containing the "new" attr, if it is different
  2370. */
  2371. if (args->blkno2 != args->blkno) {
  2372. error = xfs_da_read_buf(args->trans, args->dp, args->blkno2,
  2373. -1, &bp2, XFS_ATTR_FORK);
  2374. if (error) {
  2375. return(error);
  2376. }
  2377. ASSERT(bp2 != NULL);
  2378. } else {
  2379. bp2 = bp1;
  2380. }
  2381. leaf1 = bp1->data;
  2382. ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  2383. ASSERT(args->index < be16_to_cpu(leaf1->hdr.count));
  2384. ASSERT(args->index >= 0);
  2385. entry1 = &leaf1->entries[ args->index ];
  2386. leaf2 = bp2->data;
  2387. ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  2388. ASSERT(args->index2 < be16_to_cpu(leaf2->hdr.count));
  2389. ASSERT(args->index2 >= 0);
  2390. entry2 = &leaf2->entries[ args->index2 ];
  2391. #ifdef DEBUG
  2392. if (entry1->flags & XFS_ATTR_LOCAL) {
  2393. name_loc = xfs_attr_leaf_name_local(leaf1, args->index);
  2394. namelen1 = name_loc->namelen;
  2395. name1 = (char *)name_loc->nameval;
  2396. } else {
  2397. name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
  2398. namelen1 = name_rmt->namelen;
  2399. name1 = (char *)name_rmt->name;
  2400. }
  2401. if (entry2->flags & XFS_ATTR_LOCAL) {
  2402. name_loc = xfs_attr_leaf_name_local(leaf2, args->index2);
  2403. namelen2 = name_loc->namelen;
  2404. name2 = (char *)name_loc->nameval;
  2405. } else {
  2406. name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
  2407. namelen2 = name_rmt->namelen;
  2408. name2 = (char *)name_rmt->name;
  2409. }
  2410. ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
  2411. ASSERT(namelen1 == namelen2);
  2412. ASSERT(memcmp(name1, name2, namelen1) == 0);
  2413. #endif /* DEBUG */
  2414. ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
  2415. ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
  2416. entry1->flags &= ~XFS_ATTR_INCOMPLETE;
  2417. xfs_da_log_buf(args->trans, bp1,
  2418. XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
  2419. if (args->rmtblkno) {
  2420. ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
  2421. name_rmt = xfs_attr_leaf_name_remote(leaf1, args->index);
  2422. name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
  2423. name_rmt->valuelen = cpu_to_be32(args->valuelen);
  2424. xfs_da_log_buf(args->trans, bp1,
  2425. XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
  2426. }
  2427. entry2->flags |= XFS_ATTR_INCOMPLETE;
  2428. xfs_da_log_buf(args->trans, bp2,
  2429. XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
  2430. if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
  2431. name_rmt = xfs_attr_leaf_name_remote(leaf2, args->index2);
  2432. name_rmt->valueblk = 0;
  2433. name_rmt->valuelen = 0;
  2434. xfs_da_log_buf(args->trans, bp2,
  2435. XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
  2436. }
  2437. xfs_da_buf_done(bp1);
  2438. if (bp1 != bp2)
  2439. xfs_da_buf_done(bp2);
  2440. /*
  2441. * Commit the flag value change and start the next trans in series.
  2442. */
  2443. error = xfs_trans_roll(&args->trans, args->dp);
  2444. return(error);
  2445. }
  2446. /*========================================================================
  2447. * Indiscriminately delete the entire attribute fork
  2448. *========================================================================*/
  2449. /*
  2450. * Recurse (gasp!) through the attribute nodes until we find leaves.
  2451. * We're doing a depth-first traversal in order to invalidate everything.
  2452. */
  2453. int
  2454. xfs_attr_root_inactive(xfs_trans_t **trans, xfs_inode_t *dp)
  2455. {
  2456. xfs_da_blkinfo_t *info;
  2457. xfs_daddr_t blkno;
  2458. xfs_dabuf_t *bp;
  2459. int error;
  2460. /*
  2461. * Read block 0 to see what we have to work with.
  2462. * We only get here if we have extents, since we remove
  2463. * the extents in reverse order the extent containing
  2464. * block 0 must still be there.
  2465. */
  2466. error = xfs_da_read_buf(*trans, dp, 0, -1, &bp, XFS_ATTR_FORK);
  2467. if (error)
  2468. return(error);
  2469. blkno = xfs_da_blkno(bp);
  2470. /*
  2471. * Invalidate the tree, even if the "tree" is only a single leaf block.
  2472. * This is a depth-first traversal!
  2473. */
  2474. info = bp->data;
  2475. if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
  2476. error = xfs_attr_node_inactive(trans, dp, bp, 1);
  2477. } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
  2478. error = xfs_attr_leaf_inactive(trans, dp, bp);
  2479. } else {
  2480. error = XFS_ERROR(EIO);
  2481. xfs_da_brelse(*trans, bp);
  2482. }
  2483. if (error)
  2484. return(error);
  2485. /*
  2486. * Invalidate the incore copy of the root block.
  2487. */
  2488. error = xfs_da_get_buf(*trans, dp, 0, blkno, &bp, XFS_ATTR_FORK);
  2489. if (error)
  2490. return(error);
  2491. xfs_da_binval(*trans, bp); /* remove from cache */
  2492. /*
  2493. * Commit the invalidate and start the next transaction.
  2494. */
  2495. error = xfs_trans_roll(trans, dp);
  2496. return (error);
  2497. }
  2498. /*
  2499. * Recurse (gasp!) through the attribute nodes until we find leaves.
  2500. * We're doing a depth-first traversal in order to invalidate everything.
  2501. */
  2502. STATIC int
  2503. xfs_attr_node_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp,
  2504. int level)
  2505. {
  2506. xfs_da_blkinfo_t *info;
  2507. xfs_da_intnode_t *node;
  2508. xfs_dablk_t child_fsb;
  2509. xfs_daddr_t parent_blkno, child_blkno;
  2510. int error, count, i;
  2511. xfs_dabuf_t *child_bp;
  2512. /*
  2513. * Since this code is recursive (gasp!) we must protect ourselves.
  2514. */
  2515. if (level > XFS_DA_NODE_MAXDEPTH) {
  2516. xfs_da_brelse(*trans, bp); /* no locks for later trans */
  2517. return(XFS_ERROR(EIO));
  2518. }
  2519. node = bp->data;
  2520. ASSERT(be16_to_cpu(node->hdr.info.magic) == XFS_DA_NODE_MAGIC);
  2521. parent_blkno = xfs_da_blkno(bp); /* save for re-read later */
  2522. count = be16_to_cpu(node->hdr.count);
  2523. if (!count) {
  2524. xfs_da_brelse(*trans, bp);
  2525. return(0);
  2526. }
  2527. child_fsb = be32_to_cpu(node->btree[0].before);
  2528. xfs_da_brelse(*trans, bp); /* no locks for later trans */
  2529. /*
  2530. * If this is the node level just above the leaves, simply loop
  2531. * over the leaves removing all of them. If this is higher up
  2532. * in the tree, recurse downward.
  2533. */
  2534. for (i = 0; i < count; i++) {
  2535. /*
  2536. * Read the subsidiary block to see what we have to work with.
  2537. * Don't do this in a transaction. This is a depth-first
  2538. * traversal of the tree so we may deal with many blocks
  2539. * before we come back to this one.
  2540. */
  2541. error = xfs_da_read_buf(*trans, dp, child_fsb, -2, &child_bp,
  2542. XFS_ATTR_FORK);
  2543. if (error)
  2544. return(error);
  2545. if (child_bp) {
  2546. /* save for re-read later */
  2547. child_blkno = xfs_da_blkno(child_bp);
  2548. /*
  2549. * Invalidate the subtree, however we have to.
  2550. */
  2551. info = child_bp->data;
  2552. if (be16_to_cpu(info->magic) == XFS_DA_NODE_MAGIC) {
  2553. error = xfs_attr_node_inactive(trans, dp,
  2554. child_bp, level+1);
  2555. } else if (be16_to_cpu(info->magic) == XFS_ATTR_LEAF_MAGIC) {
  2556. error = xfs_attr_leaf_inactive(trans, dp,
  2557. child_bp);
  2558. } else {
  2559. error = XFS_ERROR(EIO);
  2560. xfs_da_brelse(*trans, child_bp);
  2561. }
  2562. if (error)
  2563. return(error);
  2564. /*
  2565. * Remove the subsidiary block from the cache
  2566. * and from the log.
  2567. */
  2568. error = xfs_da_get_buf(*trans, dp, 0, child_blkno,
  2569. &child_bp, XFS_ATTR_FORK);
  2570. if (error)
  2571. return(error);
  2572. xfs_da_binval(*trans, child_bp);
  2573. }
  2574. /*
  2575. * If we're not done, re-read the parent to get the next
  2576. * child block number.
  2577. */
  2578. if ((i+1) < count) {
  2579. error = xfs_da_read_buf(*trans, dp, 0, parent_blkno,
  2580. &bp, XFS_ATTR_FORK);
  2581. if (error)
  2582. return(error);
  2583. child_fsb = be32_to_cpu(node->btree[i+1].before);
  2584. xfs_da_brelse(*trans, bp);
  2585. }
  2586. /*
  2587. * Atomically commit the whole invalidate stuff.
  2588. */
  2589. error = xfs_trans_roll(trans, dp);
  2590. if (error)
  2591. return (error);
  2592. }
  2593. return(0);
  2594. }
  2595. /*
  2596. * Invalidate all of the "remote" value regions pointed to by a particular
  2597. * leaf block.
  2598. * Note that we must release the lock on the buffer so that we are not
  2599. * caught holding something that the logging code wants to flush to disk.
  2600. */
  2601. STATIC int
  2602. xfs_attr_leaf_inactive(xfs_trans_t **trans, xfs_inode_t *dp, xfs_dabuf_t *bp)
  2603. {
  2604. xfs_attr_leafblock_t *leaf;
  2605. xfs_attr_leaf_entry_t *entry;
  2606. xfs_attr_leaf_name_remote_t *name_rmt;
  2607. xfs_attr_inactive_list_t *list, *lp;
  2608. int error, count, size, tmp, i;
  2609. leaf = bp->data;
  2610. ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_ATTR_LEAF_MAGIC);
  2611. /*
  2612. * Count the number of "remote" value extents.
  2613. */
  2614. count = 0;
  2615. entry = &leaf->entries[0];
  2616. for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
  2617. if (be16_to_cpu(entry->nameidx) &&
  2618. ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
  2619. name_rmt = xfs_attr_leaf_name_remote(leaf, i);
  2620. if (name_rmt->valueblk)
  2621. count++;
  2622. }
  2623. }
  2624. /*
  2625. * If there are no "remote" values, we're done.
  2626. */
  2627. if (count == 0) {
  2628. xfs_da_brelse(*trans, bp);
  2629. return(0);
  2630. }
  2631. /*
  2632. * Allocate storage for a list of all the "remote" value extents.
  2633. */
  2634. size = count * sizeof(xfs_attr_inactive_list_t);
  2635. list = (xfs_attr_inactive_list_t *)kmem_alloc(size, KM_SLEEP);
  2636. /*
  2637. * Identify each of the "remote" value extents.
  2638. */
  2639. lp = list;
  2640. entry = &leaf->entries[0];
  2641. for (i = 0; i < be16_to_cpu(leaf->hdr.count); entry++, i++) {
  2642. if (be16_to_cpu(entry->nameidx) &&
  2643. ((entry->flags & XFS_ATTR_LOCAL) == 0)) {
  2644. name_rmt = xfs_attr_leaf_name_remote(leaf, i);
  2645. if (name_rmt->valueblk) {
  2646. lp->valueblk = be32_to_cpu(name_rmt->valueblk);
  2647. lp->valuelen = XFS_B_TO_FSB(dp->i_mount,
  2648. be32_to_cpu(name_rmt->valuelen));
  2649. lp++;
  2650. }
  2651. }
  2652. }
  2653. xfs_da_brelse(*trans, bp); /* unlock for trans. in freextent() */
  2654. /*
  2655. * Invalidate each of the "remote" value extents.
  2656. */
  2657. error = 0;
  2658. for (lp = list, i = 0; i < count; i++, lp++) {
  2659. tmp = xfs_attr_leaf_freextent(trans, dp,
  2660. lp->valueblk, lp->valuelen);
  2661. if (error == 0)
  2662. error = tmp; /* save only the 1st errno */
  2663. }
  2664. kmem_free((xfs_caddr_t)list);
  2665. return(error);
  2666. }
  2667. /*
  2668. * Look at all the extents for this logical region,
  2669. * invalidate any buffers that are incore/in transactions.
  2670. */
  2671. STATIC int
  2672. xfs_attr_leaf_freextent(xfs_trans_t **trans, xfs_inode_t *dp,
  2673. xfs_dablk_t blkno, int blkcnt)
  2674. {
  2675. xfs_bmbt_irec_t map;
  2676. xfs_dablk_t tblkno;
  2677. int tblkcnt, dblkcnt, nmap, error;
  2678. xfs_daddr_t dblkno;
  2679. xfs_buf_t *bp;
  2680. /*
  2681. * Roll through the "value", invalidating the attribute value's
  2682. * blocks.
  2683. */
  2684. tblkno = blkno;
  2685. tblkcnt = blkcnt;
  2686. while (tblkcnt > 0) {
  2687. /*
  2688. * Try to remember where we decided to put the value.
  2689. */
  2690. nmap = 1;
  2691. error = xfs_bmapi(*trans, dp, (xfs_fileoff_t)tblkno, tblkcnt,
  2692. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  2693. NULL, 0, &map, &nmap, NULL, NULL);
  2694. if (error) {
  2695. return(error);
  2696. }
  2697. ASSERT(nmap == 1);
  2698. ASSERT(map.br_startblock != DELAYSTARTBLOCK);
  2699. /*
  2700. * If it's a hole, these are already unmapped
  2701. * so there's nothing to invalidate.
  2702. */
  2703. if (map.br_startblock != HOLESTARTBLOCK) {
  2704. dblkno = XFS_FSB_TO_DADDR(dp->i_mount,
  2705. map.br_startblock);
  2706. dblkcnt = XFS_FSB_TO_BB(dp->i_mount,
  2707. map.br_blockcount);
  2708. bp = xfs_trans_get_buf(*trans,
  2709. dp->i_mount->m_ddev_targp,
  2710. dblkno, dblkcnt, XFS_BUF_LOCK);
  2711. xfs_trans_binval(*trans, bp);
  2712. /*
  2713. * Roll to next transaction.
  2714. */
  2715. error = xfs_trans_roll(trans, dp);
  2716. if (error)
  2717. return (error);
  2718. }
  2719. tblkno += map.br_blockcount;
  2720. tblkcnt -= map.br_blockcount;
  2721. }
  2722. return(0);
  2723. }