xfs_attr_leaf.c 91 KB

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