xfs_attr_leaf.c 88 KB

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