xfs_attr_leaf.c 92 KB

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