xfs_attr_leaf.c 85 KB

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