xfs_attr_leaf.c 82 KB

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