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