xfs_attr_leaf.c 85 KB

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