xfs_attr_leaf.c 89 KB

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