xfs_attr_leaf.c 84 KB

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