xfs_attr_leaf.c 87 KB

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