xfs_attr_leaf.c 85 KB

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