xfs_attr_leaf.c 84 KB

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