xfs_attr_leaf.c 76 KB

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