xfs_attr_leaf.c 88 KB

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