xfs_attr_leaf.c 86 KB

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