xfs_attr_leaf.c 79 KB

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