xfs_attr_leaf.c 88 KB

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