xfs_attr_leaf.c 84 KB

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