xfs_attr_leaf.c 84 KB

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