xfs_attr_leaf.c 85 KB

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