xfs_attr_leaf.c 76 KB

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