xfs_attr_leaf.c 84 KB

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