xfs_attr_leaf.c 87 KB

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