xfs_attr_leaf.c 88 KB

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