xfs_attr_leaf.c 87 KB

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