xfs_attr_leaf.c 92 KB

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