xfs_attr_leaf.c 76 KB

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