xfs_attr_leaf.c 76 KB

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