xfs_attr_leaf.c 79 KB

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