xfs_attr_leaf.c 84 KB

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