xfs_attr_leaf.c 89 KB

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