xfs_attr_leaf.c 76 KB

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