xfs_attr_leaf.c 89 KB

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