xfs_attr_leaf.c 92 KB

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