xfs_attr_leaf.c 84 KB

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