xfs_attr_leaf.c 81 KB

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