xfs_attr_leaf.c 92 KB

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