xfs_attr_leaf.c 84 KB

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