xfs_attr_leaf.c 76 KB

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