xattr.c 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * xattr.c
  5. *
  6. * Copyright (C) 2008 Oracle. All rights reserved.
  7. *
  8. * CREDITS:
  9. * Lots of code in this file is taken from ext3.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2 of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public
  22. * License along with this program; if not, write to the
  23. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. * Boston, MA 021110-1307, USA.
  25. */
  26. #include <linux/capability.h>
  27. #include <linux/fs.h>
  28. #include <linux/types.h>
  29. #include <linux/slab.h>
  30. #include <linux/highmem.h>
  31. #include <linux/pagemap.h>
  32. #include <linux/uio.h>
  33. #include <linux/sched.h>
  34. #include <linux/splice.h>
  35. #include <linux/mount.h>
  36. #include <linux/writeback.h>
  37. #include <linux/falloc.h>
  38. #include <linux/sort.h>
  39. #include <linux/init.h>
  40. #include <linux/module.h>
  41. #include <linux/string.h>
  42. #define MLOG_MASK_PREFIX ML_XATTR
  43. #include <cluster/masklog.h>
  44. #include "ocfs2.h"
  45. #include "alloc.h"
  46. #include "dlmglue.h"
  47. #include "file.h"
  48. #include "symlink.h"
  49. #include "sysfile.h"
  50. #include "inode.h"
  51. #include "journal.h"
  52. #include "ocfs2_fs.h"
  53. #include "suballoc.h"
  54. #include "uptodate.h"
  55. #include "buffer_head_io.h"
  56. #include "super.h"
  57. #include "xattr.h"
  58. struct ocfs2_xattr_def_value_root {
  59. struct ocfs2_xattr_value_root xv;
  60. struct ocfs2_extent_rec er;
  61. };
  62. struct ocfs2_xattr_bucket {
  63. struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
  64. struct ocfs2_xattr_header *xh;
  65. };
  66. #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
  67. #define OCFS2_XATTR_INLINE_SIZE 80
  68. static struct ocfs2_xattr_def_value_root def_xv = {
  69. .xv.xr_list.l_count = cpu_to_le16(1),
  70. };
  71. struct xattr_handler *ocfs2_xattr_handlers[] = {
  72. &ocfs2_xattr_user_handler,
  73. &ocfs2_xattr_trusted_handler,
  74. NULL
  75. };
  76. static struct xattr_handler *ocfs2_xattr_handler_map[] = {
  77. [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
  78. [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
  79. };
  80. struct ocfs2_xattr_info {
  81. int name_index;
  82. const char *name;
  83. const void *value;
  84. size_t value_len;
  85. };
  86. struct ocfs2_xattr_search {
  87. struct buffer_head *inode_bh;
  88. /*
  89. * xattr_bh point to the block buffer head which has extended attribute
  90. * when extended attribute in inode, xattr_bh is equal to inode_bh.
  91. */
  92. struct buffer_head *xattr_bh;
  93. struct ocfs2_xattr_header *header;
  94. struct ocfs2_xattr_bucket bucket;
  95. void *base;
  96. void *end;
  97. struct ocfs2_xattr_entry *here;
  98. int not_found;
  99. };
  100. static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
  101. struct ocfs2_xattr_header *xh,
  102. int index,
  103. int *block_off,
  104. int *new_offset);
  105. static int ocfs2_xattr_index_block_find(struct inode *inode,
  106. struct buffer_head *root_bh,
  107. int name_index,
  108. const char *name,
  109. struct ocfs2_xattr_search *xs);
  110. static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
  111. struct ocfs2_xattr_tree_root *xt,
  112. char *buffer,
  113. size_t buffer_size);
  114. static int ocfs2_xattr_create_index_block(struct inode *inode,
  115. struct ocfs2_xattr_search *xs);
  116. static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
  117. struct ocfs2_xattr_info *xi,
  118. struct ocfs2_xattr_search *xs);
  119. static int ocfs2_delete_xattr_index_block(struct inode *inode,
  120. struct buffer_head *xb_bh);
  121. static inline const char *ocfs2_xattr_prefix(int name_index)
  122. {
  123. struct xattr_handler *handler = NULL;
  124. if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
  125. handler = ocfs2_xattr_handler_map[name_index];
  126. return handler ? handler->prefix : NULL;
  127. }
  128. static u32 ocfs2_xattr_name_hash(struct inode *inode,
  129. const char *name,
  130. int name_len)
  131. {
  132. /* Get hash value of uuid from super block */
  133. u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
  134. int i;
  135. /* hash extended attribute name */
  136. for (i = 0; i < name_len; i++) {
  137. hash = (hash << OCFS2_HASH_SHIFT) ^
  138. (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
  139. *name++;
  140. }
  141. return hash;
  142. }
  143. /*
  144. * ocfs2_xattr_hash_entry()
  145. *
  146. * Compute the hash of an extended attribute.
  147. */
  148. static void ocfs2_xattr_hash_entry(struct inode *inode,
  149. struct ocfs2_xattr_header *header,
  150. struct ocfs2_xattr_entry *entry)
  151. {
  152. u32 hash = 0;
  153. char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
  154. hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
  155. entry->xe_name_hash = cpu_to_le32(hash);
  156. return;
  157. }
  158. static int ocfs2_xattr_extend_allocation(struct inode *inode,
  159. u32 clusters_to_add,
  160. struct buffer_head *xattr_bh,
  161. struct ocfs2_xattr_value_root *xv)
  162. {
  163. int status = 0;
  164. int restart_func = 0;
  165. int credits = 0;
  166. handle_t *handle = NULL;
  167. struct ocfs2_alloc_context *data_ac = NULL;
  168. struct ocfs2_alloc_context *meta_ac = NULL;
  169. enum ocfs2_alloc_restarted why;
  170. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  171. u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
  172. struct ocfs2_extent_tree et;
  173. mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
  174. ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
  175. restart_all:
  176. status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
  177. &data_ac, &meta_ac);
  178. if (status) {
  179. mlog_errno(status);
  180. goto leave;
  181. }
  182. credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
  183. clusters_to_add);
  184. handle = ocfs2_start_trans(osb, credits);
  185. if (IS_ERR(handle)) {
  186. status = PTR_ERR(handle);
  187. handle = NULL;
  188. mlog_errno(status);
  189. goto leave;
  190. }
  191. restarted_transaction:
  192. status = ocfs2_journal_access(handle, inode, xattr_bh,
  193. OCFS2_JOURNAL_ACCESS_WRITE);
  194. if (status < 0) {
  195. mlog_errno(status);
  196. goto leave;
  197. }
  198. prev_clusters = le32_to_cpu(xv->xr_clusters);
  199. status = ocfs2_add_clusters_in_btree(osb,
  200. inode,
  201. &logical_start,
  202. clusters_to_add,
  203. 0,
  204. &et,
  205. handle,
  206. data_ac,
  207. meta_ac,
  208. &why);
  209. if ((status < 0) && (status != -EAGAIN)) {
  210. if (status != -ENOSPC)
  211. mlog_errno(status);
  212. goto leave;
  213. }
  214. status = ocfs2_journal_dirty(handle, xattr_bh);
  215. if (status < 0) {
  216. mlog_errno(status);
  217. goto leave;
  218. }
  219. clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
  220. if (why != RESTART_NONE && clusters_to_add) {
  221. if (why == RESTART_META) {
  222. mlog(0, "restarting function.\n");
  223. restart_func = 1;
  224. } else {
  225. BUG_ON(why != RESTART_TRANS);
  226. mlog(0, "restarting transaction.\n");
  227. /* TODO: This can be more intelligent. */
  228. credits = ocfs2_calc_extend_credits(osb->sb,
  229. et.et_root_el,
  230. clusters_to_add);
  231. status = ocfs2_extend_trans(handle, credits);
  232. if (status < 0) {
  233. /* handle still has to be committed at
  234. * this point. */
  235. status = -ENOMEM;
  236. mlog_errno(status);
  237. goto leave;
  238. }
  239. goto restarted_transaction;
  240. }
  241. }
  242. leave:
  243. if (handle) {
  244. ocfs2_commit_trans(osb, handle);
  245. handle = NULL;
  246. }
  247. if (data_ac) {
  248. ocfs2_free_alloc_context(data_ac);
  249. data_ac = NULL;
  250. }
  251. if (meta_ac) {
  252. ocfs2_free_alloc_context(meta_ac);
  253. meta_ac = NULL;
  254. }
  255. if ((!status) && restart_func) {
  256. restart_func = 0;
  257. goto restart_all;
  258. }
  259. return status;
  260. }
  261. static int __ocfs2_remove_xattr_range(struct inode *inode,
  262. struct buffer_head *root_bh,
  263. struct ocfs2_xattr_value_root *xv,
  264. u32 cpos, u32 phys_cpos, u32 len,
  265. struct ocfs2_cached_dealloc_ctxt *dealloc)
  266. {
  267. int ret;
  268. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  269. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  270. struct inode *tl_inode = osb->osb_tl_inode;
  271. handle_t *handle;
  272. struct ocfs2_alloc_context *meta_ac = NULL;
  273. struct ocfs2_extent_tree et;
  274. ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
  275. ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
  276. if (ret) {
  277. mlog_errno(ret);
  278. return ret;
  279. }
  280. mutex_lock(&tl_inode->i_mutex);
  281. if (ocfs2_truncate_log_needs_flush(osb)) {
  282. ret = __ocfs2_flush_truncate_log(osb);
  283. if (ret < 0) {
  284. mlog_errno(ret);
  285. goto out;
  286. }
  287. }
  288. handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
  289. if (IS_ERR(handle)) {
  290. ret = PTR_ERR(handle);
  291. mlog_errno(ret);
  292. goto out;
  293. }
  294. ret = ocfs2_journal_access(handle, inode, root_bh,
  295. OCFS2_JOURNAL_ACCESS_WRITE);
  296. if (ret) {
  297. mlog_errno(ret);
  298. goto out_commit;
  299. }
  300. ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
  301. dealloc);
  302. if (ret) {
  303. mlog_errno(ret);
  304. goto out_commit;
  305. }
  306. le32_add_cpu(&xv->xr_clusters, -len);
  307. ret = ocfs2_journal_dirty(handle, root_bh);
  308. if (ret) {
  309. mlog_errno(ret);
  310. goto out_commit;
  311. }
  312. ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
  313. if (ret)
  314. mlog_errno(ret);
  315. out_commit:
  316. ocfs2_commit_trans(osb, handle);
  317. out:
  318. mutex_unlock(&tl_inode->i_mutex);
  319. if (meta_ac)
  320. ocfs2_free_alloc_context(meta_ac);
  321. return ret;
  322. }
  323. static int ocfs2_xattr_shrink_size(struct inode *inode,
  324. u32 old_clusters,
  325. u32 new_clusters,
  326. struct buffer_head *root_bh,
  327. struct ocfs2_xattr_value_root *xv)
  328. {
  329. int ret = 0;
  330. u32 trunc_len, cpos, phys_cpos, alloc_size;
  331. u64 block;
  332. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  333. struct ocfs2_cached_dealloc_ctxt dealloc;
  334. ocfs2_init_dealloc_ctxt(&dealloc);
  335. if (old_clusters <= new_clusters)
  336. return 0;
  337. cpos = new_clusters;
  338. trunc_len = old_clusters - new_clusters;
  339. while (trunc_len) {
  340. ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
  341. &alloc_size, &xv->xr_list);
  342. if (ret) {
  343. mlog_errno(ret);
  344. goto out;
  345. }
  346. if (alloc_size > trunc_len)
  347. alloc_size = trunc_len;
  348. ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
  349. phys_cpos, alloc_size,
  350. &dealloc);
  351. if (ret) {
  352. mlog_errno(ret);
  353. goto out;
  354. }
  355. block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  356. ocfs2_remove_xattr_clusters_from_cache(inode, block,
  357. alloc_size);
  358. cpos += alloc_size;
  359. trunc_len -= alloc_size;
  360. }
  361. out:
  362. ocfs2_schedule_truncate_log_flush(osb, 1);
  363. ocfs2_run_deallocs(osb, &dealloc);
  364. return ret;
  365. }
  366. static int ocfs2_xattr_value_truncate(struct inode *inode,
  367. struct buffer_head *root_bh,
  368. struct ocfs2_xattr_value_root *xv,
  369. int len)
  370. {
  371. int ret;
  372. u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
  373. u32 old_clusters = le32_to_cpu(xv->xr_clusters);
  374. if (new_clusters == old_clusters)
  375. return 0;
  376. if (new_clusters > old_clusters)
  377. ret = ocfs2_xattr_extend_allocation(inode,
  378. new_clusters - old_clusters,
  379. root_bh, xv);
  380. else
  381. ret = ocfs2_xattr_shrink_size(inode,
  382. old_clusters, new_clusters,
  383. root_bh, xv);
  384. return ret;
  385. }
  386. static int ocfs2_xattr_list_entry(char *buffer, size_t size,
  387. size_t *result, const char *prefix,
  388. const char *name, int name_len)
  389. {
  390. char *p = buffer + *result;
  391. int prefix_len = strlen(prefix);
  392. int total_len = prefix_len + name_len + 1;
  393. *result += total_len;
  394. /* we are just looking for how big our buffer needs to be */
  395. if (!size)
  396. return 0;
  397. if (*result > size)
  398. return -ERANGE;
  399. memcpy(p, prefix, prefix_len);
  400. memcpy(p + prefix_len, name, name_len);
  401. p[prefix_len + name_len] = '\0';
  402. return 0;
  403. }
  404. static int ocfs2_xattr_list_entries(struct inode *inode,
  405. struct ocfs2_xattr_header *header,
  406. char *buffer, size_t buffer_size)
  407. {
  408. size_t result = 0;
  409. int i, type, ret;
  410. const char *prefix, *name;
  411. for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
  412. struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
  413. type = ocfs2_xattr_get_type(entry);
  414. prefix = ocfs2_xattr_prefix(type);
  415. if (prefix) {
  416. name = (const char *)header +
  417. le16_to_cpu(entry->xe_name_offset);
  418. ret = ocfs2_xattr_list_entry(buffer, buffer_size,
  419. &result, prefix, name,
  420. entry->xe_name_len);
  421. if (ret)
  422. return ret;
  423. }
  424. }
  425. return result;
  426. }
  427. static int ocfs2_xattr_ibody_list(struct inode *inode,
  428. struct ocfs2_dinode *di,
  429. char *buffer,
  430. size_t buffer_size)
  431. {
  432. struct ocfs2_xattr_header *header = NULL;
  433. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  434. int ret = 0;
  435. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
  436. return ret;
  437. header = (struct ocfs2_xattr_header *)
  438. ((void *)di + inode->i_sb->s_blocksize -
  439. le16_to_cpu(di->i_xattr_inline_size));
  440. ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
  441. return ret;
  442. }
  443. static int ocfs2_xattr_block_list(struct inode *inode,
  444. struct ocfs2_dinode *di,
  445. char *buffer,
  446. size_t buffer_size)
  447. {
  448. struct buffer_head *blk_bh = NULL;
  449. struct ocfs2_xattr_block *xb;
  450. int ret = 0;
  451. if (!di->i_xattr_loc)
  452. return ret;
  453. ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
  454. if (ret < 0) {
  455. mlog_errno(ret);
  456. return ret;
  457. }
  458. /*Verify the signature of xattr block*/
  459. if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
  460. strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
  461. ret = -EFAULT;
  462. goto cleanup;
  463. }
  464. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  465. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  466. struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
  467. ret = ocfs2_xattr_list_entries(inode, header,
  468. buffer, buffer_size);
  469. } else {
  470. struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
  471. ret = ocfs2_xattr_tree_list_index_block(inode, xt,
  472. buffer, buffer_size);
  473. }
  474. cleanup:
  475. brelse(blk_bh);
  476. return ret;
  477. }
  478. ssize_t ocfs2_listxattr(struct dentry *dentry,
  479. char *buffer,
  480. size_t size)
  481. {
  482. int ret = 0, i_ret = 0, b_ret = 0;
  483. struct buffer_head *di_bh = NULL;
  484. struct ocfs2_dinode *di = NULL;
  485. struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
  486. if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
  487. return -EOPNOTSUPP;
  488. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  489. return ret;
  490. ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
  491. if (ret < 0) {
  492. mlog_errno(ret);
  493. return ret;
  494. }
  495. di = (struct ocfs2_dinode *)di_bh->b_data;
  496. down_read(&oi->ip_xattr_sem);
  497. i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
  498. if (i_ret < 0)
  499. b_ret = 0;
  500. else {
  501. if (buffer) {
  502. buffer += i_ret;
  503. size -= i_ret;
  504. }
  505. b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
  506. buffer, size);
  507. if (b_ret < 0)
  508. i_ret = 0;
  509. }
  510. up_read(&oi->ip_xattr_sem);
  511. ocfs2_inode_unlock(dentry->d_inode, 0);
  512. brelse(di_bh);
  513. return i_ret + b_ret;
  514. }
  515. static int ocfs2_xattr_find_entry(int name_index,
  516. const char *name,
  517. struct ocfs2_xattr_search *xs)
  518. {
  519. struct ocfs2_xattr_entry *entry;
  520. size_t name_len;
  521. int i, cmp = 1;
  522. if (name == NULL)
  523. return -EINVAL;
  524. name_len = strlen(name);
  525. entry = xs->here;
  526. for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
  527. cmp = name_index - ocfs2_xattr_get_type(entry);
  528. if (!cmp)
  529. cmp = name_len - entry->xe_name_len;
  530. if (!cmp)
  531. cmp = memcmp(name, (xs->base +
  532. le16_to_cpu(entry->xe_name_offset)),
  533. name_len);
  534. if (cmp == 0)
  535. break;
  536. entry += 1;
  537. }
  538. xs->here = entry;
  539. return cmp ? -ENODATA : 0;
  540. }
  541. static int ocfs2_xattr_get_value_outside(struct inode *inode,
  542. struct ocfs2_xattr_value_root *xv,
  543. void *buffer,
  544. size_t len)
  545. {
  546. u32 cpos, p_cluster, num_clusters, bpc, clusters;
  547. u64 blkno;
  548. int i, ret = 0;
  549. size_t cplen, blocksize;
  550. struct buffer_head *bh = NULL;
  551. struct ocfs2_extent_list *el;
  552. el = &xv->xr_list;
  553. clusters = le32_to_cpu(xv->xr_clusters);
  554. bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  555. blocksize = inode->i_sb->s_blocksize;
  556. cpos = 0;
  557. while (cpos < clusters) {
  558. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  559. &num_clusters, el);
  560. if (ret) {
  561. mlog_errno(ret);
  562. goto out;
  563. }
  564. blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  565. /* Copy ocfs2_xattr_value */
  566. for (i = 0; i < num_clusters * bpc; i++, blkno++) {
  567. ret = ocfs2_read_block(inode, blkno, &bh);
  568. if (ret) {
  569. mlog_errno(ret);
  570. goto out;
  571. }
  572. cplen = len >= blocksize ? blocksize : len;
  573. memcpy(buffer, bh->b_data, cplen);
  574. len -= cplen;
  575. buffer += cplen;
  576. brelse(bh);
  577. bh = NULL;
  578. if (len == 0)
  579. break;
  580. }
  581. cpos += num_clusters;
  582. }
  583. out:
  584. return ret;
  585. }
  586. static int ocfs2_xattr_ibody_get(struct inode *inode,
  587. int name_index,
  588. const char *name,
  589. void *buffer,
  590. size_t buffer_size,
  591. struct ocfs2_xattr_search *xs)
  592. {
  593. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  594. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  595. struct ocfs2_xattr_value_root *xv;
  596. size_t size;
  597. int ret = 0;
  598. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
  599. return -ENODATA;
  600. xs->end = (void *)di + inode->i_sb->s_blocksize;
  601. xs->header = (struct ocfs2_xattr_header *)
  602. (xs->end - le16_to_cpu(di->i_xattr_inline_size));
  603. xs->base = (void *)xs->header;
  604. xs->here = xs->header->xh_entries;
  605. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  606. if (ret)
  607. return ret;
  608. size = le64_to_cpu(xs->here->xe_value_size);
  609. if (buffer) {
  610. if (size > buffer_size)
  611. return -ERANGE;
  612. if (ocfs2_xattr_is_local(xs->here)) {
  613. memcpy(buffer, (void *)xs->base +
  614. le16_to_cpu(xs->here->xe_name_offset) +
  615. OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
  616. } else {
  617. xv = (struct ocfs2_xattr_value_root *)
  618. (xs->base + le16_to_cpu(
  619. xs->here->xe_name_offset) +
  620. OCFS2_XATTR_SIZE(xs->here->xe_name_len));
  621. ret = ocfs2_xattr_get_value_outside(inode, xv,
  622. buffer, size);
  623. if (ret < 0) {
  624. mlog_errno(ret);
  625. return ret;
  626. }
  627. }
  628. }
  629. return size;
  630. }
  631. static int ocfs2_xattr_block_get(struct inode *inode,
  632. int name_index,
  633. const char *name,
  634. void *buffer,
  635. size_t buffer_size,
  636. struct ocfs2_xattr_search *xs)
  637. {
  638. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  639. struct buffer_head *blk_bh = NULL;
  640. struct ocfs2_xattr_block *xb;
  641. struct ocfs2_xattr_value_root *xv;
  642. size_t size;
  643. int ret = -ENODATA, name_offset, name_len, block_off, i;
  644. if (!di->i_xattr_loc)
  645. return ret;
  646. memset(&xs->bucket, 0, sizeof(xs->bucket));
  647. ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
  648. if (ret < 0) {
  649. mlog_errno(ret);
  650. return ret;
  651. }
  652. /*Verify the signature of xattr block*/
  653. if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
  654. strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
  655. ret = -EFAULT;
  656. goto cleanup;
  657. }
  658. xs->xattr_bh = blk_bh;
  659. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  660. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  661. xs->header = &xb->xb_attrs.xb_header;
  662. xs->base = (void *)xs->header;
  663. xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
  664. xs->here = xs->header->xh_entries;
  665. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  666. } else
  667. ret = ocfs2_xattr_index_block_find(inode, blk_bh,
  668. name_index,
  669. name, xs);
  670. if (ret)
  671. goto cleanup;
  672. size = le64_to_cpu(xs->here->xe_value_size);
  673. if (buffer) {
  674. ret = -ERANGE;
  675. if (size > buffer_size)
  676. goto cleanup;
  677. name_offset = le16_to_cpu(xs->here->xe_name_offset);
  678. name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
  679. i = xs->here - xs->header->xh_entries;
  680. if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
  681. ret = ocfs2_xattr_bucket_get_name_value(inode,
  682. xs->bucket.xh,
  683. i,
  684. &block_off,
  685. &name_offset);
  686. xs->base = xs->bucket.bhs[block_off]->b_data;
  687. }
  688. if (ocfs2_xattr_is_local(xs->here)) {
  689. memcpy(buffer, (void *)xs->base +
  690. name_offset + name_len, size);
  691. } else {
  692. xv = (struct ocfs2_xattr_value_root *)
  693. (xs->base + name_offset + name_len);
  694. ret = ocfs2_xattr_get_value_outside(inode, xv,
  695. buffer, size);
  696. if (ret < 0) {
  697. mlog_errno(ret);
  698. goto cleanup;
  699. }
  700. }
  701. }
  702. ret = size;
  703. cleanup:
  704. for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
  705. brelse(xs->bucket.bhs[i]);
  706. memset(&xs->bucket, 0, sizeof(xs->bucket));
  707. brelse(blk_bh);
  708. return ret;
  709. }
  710. /* ocfs2_xattr_get()
  711. *
  712. * Copy an extended attribute into the buffer provided.
  713. * Buffer is NULL to compute the size of buffer required.
  714. */
  715. int ocfs2_xattr_get(struct inode *inode,
  716. int name_index,
  717. const char *name,
  718. void *buffer,
  719. size_t buffer_size)
  720. {
  721. int ret;
  722. struct ocfs2_dinode *di = NULL;
  723. struct buffer_head *di_bh = NULL;
  724. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  725. struct ocfs2_xattr_search xis = {
  726. .not_found = -ENODATA,
  727. };
  728. struct ocfs2_xattr_search xbs = {
  729. .not_found = -ENODATA,
  730. };
  731. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  732. return -EOPNOTSUPP;
  733. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  734. ret = -ENODATA;
  735. ret = ocfs2_inode_lock(inode, &di_bh, 0);
  736. if (ret < 0) {
  737. mlog_errno(ret);
  738. return ret;
  739. }
  740. xis.inode_bh = xbs.inode_bh = di_bh;
  741. di = (struct ocfs2_dinode *)di_bh->b_data;
  742. down_read(&oi->ip_xattr_sem);
  743. ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
  744. buffer_size, &xis);
  745. if (ret == -ENODATA)
  746. ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
  747. buffer_size, &xbs);
  748. up_read(&oi->ip_xattr_sem);
  749. ocfs2_inode_unlock(inode, 0);
  750. brelse(di_bh);
  751. return ret;
  752. }
  753. static int __ocfs2_xattr_set_value_outside(struct inode *inode,
  754. struct ocfs2_xattr_value_root *xv,
  755. const void *value,
  756. int value_len)
  757. {
  758. int ret = 0, i, cp_len, credits;
  759. u16 blocksize = inode->i_sb->s_blocksize;
  760. u32 p_cluster, num_clusters;
  761. u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  762. u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
  763. u64 blkno;
  764. struct buffer_head *bh = NULL;
  765. handle_t *handle;
  766. BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
  767. credits = clusters * bpc;
  768. handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
  769. if (IS_ERR(handle)) {
  770. ret = PTR_ERR(handle);
  771. mlog_errno(ret);
  772. goto out;
  773. }
  774. while (cpos < clusters) {
  775. ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
  776. &num_clusters, &xv->xr_list);
  777. if (ret) {
  778. mlog_errno(ret);
  779. goto out_commit;
  780. }
  781. blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
  782. for (i = 0; i < num_clusters * bpc; i++, blkno++) {
  783. ret = ocfs2_read_block(inode, blkno, &bh);
  784. if (ret) {
  785. mlog_errno(ret);
  786. goto out_commit;
  787. }
  788. ret = ocfs2_journal_access(handle,
  789. inode,
  790. bh,
  791. OCFS2_JOURNAL_ACCESS_WRITE);
  792. if (ret < 0) {
  793. mlog_errno(ret);
  794. goto out_commit;
  795. }
  796. cp_len = value_len > blocksize ? blocksize : value_len;
  797. memcpy(bh->b_data, value, cp_len);
  798. value_len -= cp_len;
  799. value += cp_len;
  800. if (cp_len < blocksize)
  801. memset(bh->b_data + cp_len, 0,
  802. blocksize - cp_len);
  803. ret = ocfs2_journal_dirty(handle, bh);
  804. if (ret < 0) {
  805. mlog_errno(ret);
  806. goto out_commit;
  807. }
  808. brelse(bh);
  809. bh = NULL;
  810. /*
  811. * XXX: do we need to empty all the following
  812. * blocks in this cluster?
  813. */
  814. if (!value_len)
  815. break;
  816. }
  817. cpos += num_clusters;
  818. }
  819. out_commit:
  820. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  821. out:
  822. brelse(bh);
  823. return ret;
  824. }
  825. static int ocfs2_xattr_cleanup(struct inode *inode,
  826. struct ocfs2_xattr_info *xi,
  827. struct ocfs2_xattr_search *xs,
  828. size_t offs)
  829. {
  830. handle_t *handle = NULL;
  831. int ret = 0;
  832. size_t name_len = strlen(xi->name);
  833. void *val = xs->base + offs;
  834. size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  835. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  836. OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
  837. if (IS_ERR(handle)) {
  838. ret = PTR_ERR(handle);
  839. mlog_errno(ret);
  840. goto out;
  841. }
  842. ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
  843. OCFS2_JOURNAL_ACCESS_WRITE);
  844. if (ret) {
  845. mlog_errno(ret);
  846. goto out_commit;
  847. }
  848. /* Decrease xattr count */
  849. le16_add_cpu(&xs->header->xh_count, -1);
  850. /* Remove the xattr entry and tree root which has already be set*/
  851. memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
  852. memset(val, 0, size);
  853. ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
  854. if (ret < 0)
  855. mlog_errno(ret);
  856. out_commit:
  857. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  858. out:
  859. return ret;
  860. }
  861. static int ocfs2_xattr_update_entry(struct inode *inode,
  862. struct ocfs2_xattr_info *xi,
  863. struct ocfs2_xattr_search *xs,
  864. size_t offs)
  865. {
  866. handle_t *handle = NULL;
  867. int ret = 0;
  868. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  869. OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
  870. if (IS_ERR(handle)) {
  871. ret = PTR_ERR(handle);
  872. mlog_errno(ret);
  873. goto out;
  874. }
  875. ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
  876. OCFS2_JOURNAL_ACCESS_WRITE);
  877. if (ret) {
  878. mlog_errno(ret);
  879. goto out_commit;
  880. }
  881. xs->here->xe_name_offset = cpu_to_le16(offs);
  882. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  883. if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
  884. ocfs2_xattr_set_local(xs->here, 1);
  885. else
  886. ocfs2_xattr_set_local(xs->here, 0);
  887. ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
  888. ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
  889. if (ret < 0)
  890. mlog_errno(ret);
  891. out_commit:
  892. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  893. out:
  894. return ret;
  895. }
  896. /*
  897. * ocfs2_xattr_set_value_outside()
  898. *
  899. * Set large size value in B tree.
  900. */
  901. static int ocfs2_xattr_set_value_outside(struct inode *inode,
  902. struct ocfs2_xattr_info *xi,
  903. struct ocfs2_xattr_search *xs,
  904. size_t offs)
  905. {
  906. size_t name_len = strlen(xi->name);
  907. void *val = xs->base + offs;
  908. struct ocfs2_xattr_value_root *xv = NULL;
  909. size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  910. int ret = 0;
  911. memset(val, 0, size);
  912. memcpy(val, xi->name, name_len);
  913. xv = (struct ocfs2_xattr_value_root *)
  914. (val + OCFS2_XATTR_SIZE(name_len));
  915. xv->xr_clusters = 0;
  916. xv->xr_last_eb_blk = 0;
  917. xv->xr_list.l_tree_depth = 0;
  918. xv->xr_list.l_count = cpu_to_le16(1);
  919. xv->xr_list.l_next_free_rec = 0;
  920. ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
  921. xi->value_len);
  922. if (ret < 0) {
  923. mlog_errno(ret);
  924. return ret;
  925. }
  926. ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
  927. xi->value_len);
  928. if (ret < 0) {
  929. mlog_errno(ret);
  930. return ret;
  931. }
  932. ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
  933. if (ret < 0)
  934. mlog_errno(ret);
  935. return ret;
  936. }
  937. /*
  938. * ocfs2_xattr_set_entry_local()
  939. *
  940. * Set, replace or remove extended attribute in local.
  941. */
  942. static void ocfs2_xattr_set_entry_local(struct inode *inode,
  943. struct ocfs2_xattr_info *xi,
  944. struct ocfs2_xattr_search *xs,
  945. struct ocfs2_xattr_entry *last,
  946. size_t min_offs)
  947. {
  948. size_t name_len = strlen(xi->name);
  949. int i;
  950. if (xi->value && xs->not_found) {
  951. /* Insert the new xattr entry. */
  952. le16_add_cpu(&xs->header->xh_count, 1);
  953. ocfs2_xattr_set_type(last, xi->name_index);
  954. ocfs2_xattr_set_local(last, 1);
  955. last->xe_name_len = name_len;
  956. } else {
  957. void *first_val;
  958. void *val;
  959. size_t offs, size;
  960. first_val = xs->base + min_offs;
  961. offs = le16_to_cpu(xs->here->xe_name_offset);
  962. val = xs->base + offs;
  963. if (le64_to_cpu(xs->here->xe_value_size) >
  964. OCFS2_XATTR_INLINE_SIZE)
  965. size = OCFS2_XATTR_SIZE(name_len) +
  966. OCFS2_XATTR_ROOT_SIZE;
  967. else
  968. size = OCFS2_XATTR_SIZE(name_len) +
  969. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  970. if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
  971. OCFS2_XATTR_SIZE(xi->value_len)) {
  972. /* The old and the new value have the
  973. same size. Just replace the value. */
  974. ocfs2_xattr_set_local(xs->here, 1);
  975. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  976. /* Clear value bytes. */
  977. memset(val + OCFS2_XATTR_SIZE(name_len),
  978. 0,
  979. OCFS2_XATTR_SIZE(xi->value_len));
  980. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  981. xi->value,
  982. xi->value_len);
  983. return;
  984. }
  985. /* Remove the old name+value. */
  986. memmove(first_val + size, first_val, val - first_val);
  987. memset(first_val, 0, size);
  988. xs->here->xe_name_hash = 0;
  989. xs->here->xe_name_offset = 0;
  990. ocfs2_xattr_set_local(xs->here, 1);
  991. xs->here->xe_value_size = 0;
  992. min_offs += size;
  993. /* Adjust all value offsets. */
  994. last = xs->header->xh_entries;
  995. for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
  996. size_t o = le16_to_cpu(last->xe_name_offset);
  997. if (o < offs)
  998. last->xe_name_offset = cpu_to_le16(o + size);
  999. last += 1;
  1000. }
  1001. if (!xi->value) {
  1002. /* Remove the old entry. */
  1003. last -= 1;
  1004. memmove(xs->here, xs->here + 1,
  1005. (void *)last - (void *)xs->here);
  1006. memset(last, 0, sizeof(struct ocfs2_xattr_entry));
  1007. le16_add_cpu(&xs->header->xh_count, -1);
  1008. }
  1009. }
  1010. if (xi->value) {
  1011. /* Insert the new name+value. */
  1012. size_t size = OCFS2_XATTR_SIZE(name_len) +
  1013. OCFS2_XATTR_SIZE(xi->value_len);
  1014. void *val = xs->base + min_offs - size;
  1015. xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
  1016. memset(val, 0, size);
  1017. memcpy(val, xi->name, name_len);
  1018. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  1019. xi->value,
  1020. xi->value_len);
  1021. xs->here->xe_value_size = cpu_to_le64(xi->value_len);
  1022. ocfs2_xattr_set_local(xs->here, 1);
  1023. ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
  1024. }
  1025. return;
  1026. }
  1027. /*
  1028. * ocfs2_xattr_set_entry()
  1029. *
  1030. * Set extended attribute entry into inode or block.
  1031. *
  1032. * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
  1033. * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
  1034. * then set value in B tree with set_value_outside().
  1035. */
  1036. static int ocfs2_xattr_set_entry(struct inode *inode,
  1037. struct ocfs2_xattr_info *xi,
  1038. struct ocfs2_xattr_search *xs,
  1039. int flag)
  1040. {
  1041. struct ocfs2_xattr_entry *last;
  1042. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1043. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1044. size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
  1045. size_t size_l = 0;
  1046. handle_t *handle = NULL;
  1047. int free, i, ret;
  1048. struct ocfs2_xattr_info xi_l = {
  1049. .name_index = xi->name_index,
  1050. .name = xi->name,
  1051. .value = xi->value,
  1052. .value_len = xi->value_len,
  1053. };
  1054. /* Compute min_offs, last and free space. */
  1055. last = xs->header->xh_entries;
  1056. for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
  1057. size_t offs = le16_to_cpu(last->xe_name_offset);
  1058. if (offs < min_offs)
  1059. min_offs = offs;
  1060. last += 1;
  1061. }
  1062. free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
  1063. if (free < 0)
  1064. return -EFAULT;
  1065. if (!xs->not_found) {
  1066. size_t size = 0;
  1067. if (ocfs2_xattr_is_local(xs->here))
  1068. size = OCFS2_XATTR_SIZE(name_len) +
  1069. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  1070. else
  1071. size = OCFS2_XATTR_SIZE(name_len) +
  1072. OCFS2_XATTR_ROOT_SIZE;
  1073. free += (size + sizeof(struct ocfs2_xattr_entry));
  1074. }
  1075. /* Check free space in inode or block */
  1076. if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1077. if (free < sizeof(struct ocfs2_xattr_entry) +
  1078. OCFS2_XATTR_SIZE(name_len) +
  1079. OCFS2_XATTR_ROOT_SIZE) {
  1080. ret = -ENOSPC;
  1081. goto out;
  1082. }
  1083. size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
  1084. xi_l.value = (void *)&def_xv;
  1085. xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
  1086. } else if (xi->value) {
  1087. if (free < sizeof(struct ocfs2_xattr_entry) +
  1088. OCFS2_XATTR_SIZE(name_len) +
  1089. OCFS2_XATTR_SIZE(xi->value_len)) {
  1090. ret = -ENOSPC;
  1091. goto out;
  1092. }
  1093. }
  1094. if (!xs->not_found) {
  1095. /* For existing extended attribute */
  1096. size_t size = OCFS2_XATTR_SIZE(name_len) +
  1097. OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
  1098. size_t offs = le16_to_cpu(xs->here->xe_name_offset);
  1099. void *val = xs->base + offs;
  1100. if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
  1101. /* Replace existing local xattr with tree root */
  1102. ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
  1103. offs);
  1104. if (ret < 0)
  1105. mlog_errno(ret);
  1106. goto out;
  1107. } else if (!ocfs2_xattr_is_local(xs->here)) {
  1108. /* For existing xattr which has value outside */
  1109. struct ocfs2_xattr_value_root *xv = NULL;
  1110. xv = (struct ocfs2_xattr_value_root *)(val +
  1111. OCFS2_XATTR_SIZE(name_len));
  1112. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1113. /*
  1114. * If new value need set outside also,
  1115. * first truncate old value to new value,
  1116. * then set new value with set_value_outside().
  1117. */
  1118. ret = ocfs2_xattr_value_truncate(inode,
  1119. xs->xattr_bh,
  1120. xv,
  1121. xi->value_len);
  1122. if (ret < 0) {
  1123. mlog_errno(ret);
  1124. goto out;
  1125. }
  1126. ret = __ocfs2_xattr_set_value_outside(inode,
  1127. xv,
  1128. xi->value,
  1129. xi->value_len);
  1130. if (ret < 0) {
  1131. mlog_errno(ret);
  1132. goto out;
  1133. }
  1134. ret = ocfs2_xattr_update_entry(inode,
  1135. xi,
  1136. xs,
  1137. offs);
  1138. if (ret < 0)
  1139. mlog_errno(ret);
  1140. goto out;
  1141. } else {
  1142. /*
  1143. * If new value need set in local,
  1144. * just trucate old value to zero.
  1145. */
  1146. ret = ocfs2_xattr_value_truncate(inode,
  1147. xs->xattr_bh,
  1148. xv,
  1149. 0);
  1150. if (ret < 0)
  1151. mlog_errno(ret);
  1152. }
  1153. }
  1154. }
  1155. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  1156. OCFS2_INODE_UPDATE_CREDITS);
  1157. if (IS_ERR(handle)) {
  1158. ret = PTR_ERR(handle);
  1159. mlog_errno(ret);
  1160. goto out;
  1161. }
  1162. ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
  1163. OCFS2_JOURNAL_ACCESS_WRITE);
  1164. if (ret) {
  1165. mlog_errno(ret);
  1166. goto out_commit;
  1167. }
  1168. if (!(flag & OCFS2_INLINE_XATTR_FL)) {
  1169. /* set extended attribute in external block. */
  1170. ret = ocfs2_extend_trans(handle,
  1171. OCFS2_INODE_UPDATE_CREDITS +
  1172. OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
  1173. if (ret) {
  1174. mlog_errno(ret);
  1175. goto out_commit;
  1176. }
  1177. ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
  1178. OCFS2_JOURNAL_ACCESS_WRITE);
  1179. if (ret) {
  1180. mlog_errno(ret);
  1181. goto out_commit;
  1182. }
  1183. }
  1184. /*
  1185. * Set value in local, include set tree root in local.
  1186. * This is the first step for value size >INLINE_SIZE.
  1187. */
  1188. ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
  1189. if (!(flag & OCFS2_INLINE_XATTR_FL)) {
  1190. ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
  1191. if (ret < 0) {
  1192. mlog_errno(ret);
  1193. goto out_commit;
  1194. }
  1195. }
  1196. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
  1197. (flag & OCFS2_INLINE_XATTR_FL)) {
  1198. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1199. unsigned int xattrsize = osb->s_xattr_inline_size;
  1200. /*
  1201. * Adjust extent record count or inline data size
  1202. * to reserve space for extended attribute.
  1203. */
  1204. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1205. struct ocfs2_inline_data *idata = &di->id2.i_data;
  1206. le16_add_cpu(&idata->id_count, -xattrsize);
  1207. } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
  1208. struct ocfs2_extent_list *el = &di->id2.i_list;
  1209. le16_add_cpu(&el->l_count, -(xattrsize /
  1210. sizeof(struct ocfs2_extent_rec)));
  1211. }
  1212. di->i_xattr_inline_size = cpu_to_le16(xattrsize);
  1213. }
  1214. /* Update xattr flag */
  1215. spin_lock(&oi->ip_lock);
  1216. oi->ip_dyn_features |= flag;
  1217. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  1218. spin_unlock(&oi->ip_lock);
  1219. /* Update inode ctime */
  1220. inode->i_ctime = CURRENT_TIME;
  1221. di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  1222. di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  1223. ret = ocfs2_journal_dirty(handle, xs->inode_bh);
  1224. if (ret < 0)
  1225. mlog_errno(ret);
  1226. out_commit:
  1227. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  1228. if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
  1229. /*
  1230. * Set value outside in B tree.
  1231. * This is the second step for value size > INLINE_SIZE.
  1232. */
  1233. size_t offs = le16_to_cpu(xs->here->xe_name_offset);
  1234. ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
  1235. if (ret < 0) {
  1236. int ret2;
  1237. mlog_errno(ret);
  1238. /*
  1239. * If set value outside failed, we have to clean
  1240. * the junk tree root we have already set in local.
  1241. */
  1242. ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
  1243. if (ret2 < 0)
  1244. mlog_errno(ret2);
  1245. }
  1246. }
  1247. out:
  1248. return ret;
  1249. }
  1250. static int ocfs2_remove_value_outside(struct inode*inode,
  1251. struct buffer_head *bh,
  1252. struct ocfs2_xattr_header *header)
  1253. {
  1254. int ret = 0, i;
  1255. for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
  1256. struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
  1257. if (!ocfs2_xattr_is_local(entry)) {
  1258. struct ocfs2_xattr_value_root *xv;
  1259. void *val;
  1260. val = (void *)header +
  1261. le16_to_cpu(entry->xe_name_offset);
  1262. xv = (struct ocfs2_xattr_value_root *)
  1263. (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
  1264. ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
  1265. if (ret < 0) {
  1266. mlog_errno(ret);
  1267. return ret;
  1268. }
  1269. }
  1270. }
  1271. return ret;
  1272. }
  1273. static int ocfs2_xattr_ibody_remove(struct inode *inode,
  1274. struct buffer_head *di_bh)
  1275. {
  1276. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1277. struct ocfs2_xattr_header *header;
  1278. int ret;
  1279. header = (struct ocfs2_xattr_header *)
  1280. ((void *)di + inode->i_sb->s_blocksize -
  1281. le16_to_cpu(di->i_xattr_inline_size));
  1282. ret = ocfs2_remove_value_outside(inode, di_bh, header);
  1283. return ret;
  1284. }
  1285. static int ocfs2_xattr_block_remove(struct inode *inode,
  1286. struct buffer_head *blk_bh)
  1287. {
  1288. struct ocfs2_xattr_block *xb;
  1289. int ret = 0;
  1290. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1291. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1292. struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
  1293. ret = ocfs2_remove_value_outside(inode, blk_bh, header);
  1294. } else
  1295. ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
  1296. return ret;
  1297. }
  1298. static int ocfs2_xattr_free_block(struct inode *inode,
  1299. u64 block)
  1300. {
  1301. struct inode *xb_alloc_inode;
  1302. struct buffer_head *xb_alloc_bh = NULL;
  1303. struct buffer_head *blk_bh = NULL;
  1304. struct ocfs2_xattr_block *xb;
  1305. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1306. handle_t *handle;
  1307. int ret = 0;
  1308. u64 blk, bg_blkno;
  1309. u16 bit;
  1310. ret = ocfs2_read_block(inode, block, &blk_bh);
  1311. if (ret < 0) {
  1312. mlog_errno(ret);
  1313. goto out;
  1314. }
  1315. /*Verify the signature of xattr block*/
  1316. if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
  1317. strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
  1318. ret = -EFAULT;
  1319. goto out;
  1320. }
  1321. ret = ocfs2_xattr_block_remove(inode, blk_bh);
  1322. if (ret < 0) {
  1323. mlog_errno(ret);
  1324. goto out;
  1325. }
  1326. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1327. blk = le64_to_cpu(xb->xb_blkno);
  1328. bit = le16_to_cpu(xb->xb_suballoc_bit);
  1329. bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  1330. xb_alloc_inode = ocfs2_get_system_file_inode(osb,
  1331. EXTENT_ALLOC_SYSTEM_INODE,
  1332. le16_to_cpu(xb->xb_suballoc_slot));
  1333. if (!xb_alloc_inode) {
  1334. ret = -ENOMEM;
  1335. mlog_errno(ret);
  1336. goto out;
  1337. }
  1338. mutex_lock(&xb_alloc_inode->i_mutex);
  1339. ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
  1340. if (ret < 0) {
  1341. mlog_errno(ret);
  1342. goto out_mutex;
  1343. }
  1344. handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
  1345. if (IS_ERR(handle)) {
  1346. ret = PTR_ERR(handle);
  1347. mlog_errno(ret);
  1348. goto out_unlock;
  1349. }
  1350. ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
  1351. bit, bg_blkno, 1);
  1352. if (ret < 0)
  1353. mlog_errno(ret);
  1354. ocfs2_commit_trans(osb, handle);
  1355. out_unlock:
  1356. ocfs2_inode_unlock(xb_alloc_inode, 1);
  1357. brelse(xb_alloc_bh);
  1358. out_mutex:
  1359. mutex_unlock(&xb_alloc_inode->i_mutex);
  1360. iput(xb_alloc_inode);
  1361. out:
  1362. brelse(blk_bh);
  1363. return ret;
  1364. }
  1365. /*
  1366. * ocfs2_xattr_remove()
  1367. *
  1368. * Free extended attribute resources associated with this inode.
  1369. */
  1370. int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
  1371. {
  1372. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1373. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1374. handle_t *handle;
  1375. int ret;
  1376. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  1377. return 0;
  1378. if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
  1379. return 0;
  1380. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  1381. ret = ocfs2_xattr_ibody_remove(inode, di_bh);
  1382. if (ret < 0) {
  1383. mlog_errno(ret);
  1384. goto out;
  1385. }
  1386. }
  1387. if (di->i_xattr_loc) {
  1388. ret = ocfs2_xattr_free_block(inode,
  1389. le64_to_cpu(di->i_xattr_loc));
  1390. if (ret < 0) {
  1391. mlog_errno(ret);
  1392. goto out;
  1393. }
  1394. }
  1395. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
  1396. OCFS2_INODE_UPDATE_CREDITS);
  1397. if (IS_ERR(handle)) {
  1398. ret = PTR_ERR(handle);
  1399. mlog_errno(ret);
  1400. goto out;
  1401. }
  1402. ret = ocfs2_journal_access(handle, inode, di_bh,
  1403. OCFS2_JOURNAL_ACCESS_WRITE);
  1404. if (ret) {
  1405. mlog_errno(ret);
  1406. goto out_commit;
  1407. }
  1408. di->i_xattr_loc = 0;
  1409. spin_lock(&oi->ip_lock);
  1410. oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
  1411. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  1412. spin_unlock(&oi->ip_lock);
  1413. ret = ocfs2_journal_dirty(handle, di_bh);
  1414. if (ret < 0)
  1415. mlog_errno(ret);
  1416. out_commit:
  1417. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  1418. out:
  1419. return ret;
  1420. }
  1421. static int ocfs2_xattr_has_space_inline(struct inode *inode,
  1422. struct ocfs2_dinode *di)
  1423. {
  1424. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1425. unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
  1426. int free;
  1427. if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
  1428. return 0;
  1429. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1430. struct ocfs2_inline_data *idata = &di->id2.i_data;
  1431. free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
  1432. } else if (ocfs2_inode_is_fast_symlink(inode)) {
  1433. free = ocfs2_fast_symlink_chars(inode->i_sb) -
  1434. le64_to_cpu(di->i_size);
  1435. } else {
  1436. struct ocfs2_extent_list *el = &di->id2.i_list;
  1437. free = (le16_to_cpu(el->l_count) -
  1438. le16_to_cpu(el->l_next_free_rec)) *
  1439. sizeof(struct ocfs2_extent_rec);
  1440. }
  1441. if (free >= xattrsize)
  1442. return 1;
  1443. return 0;
  1444. }
  1445. /*
  1446. * ocfs2_xattr_ibody_find()
  1447. *
  1448. * Find extended attribute in inode block and
  1449. * fill search info into struct ocfs2_xattr_search.
  1450. */
  1451. static int ocfs2_xattr_ibody_find(struct inode *inode,
  1452. int name_index,
  1453. const char *name,
  1454. struct ocfs2_xattr_search *xs)
  1455. {
  1456. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1457. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1458. int ret;
  1459. int has_space = 0;
  1460. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
  1461. return 0;
  1462. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
  1463. down_read(&oi->ip_alloc_sem);
  1464. has_space = ocfs2_xattr_has_space_inline(inode, di);
  1465. up_read(&oi->ip_alloc_sem);
  1466. if (!has_space)
  1467. return 0;
  1468. }
  1469. xs->xattr_bh = xs->inode_bh;
  1470. xs->end = (void *)di + inode->i_sb->s_blocksize;
  1471. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
  1472. xs->header = (struct ocfs2_xattr_header *)
  1473. (xs->end - le16_to_cpu(di->i_xattr_inline_size));
  1474. else
  1475. xs->header = (struct ocfs2_xattr_header *)
  1476. (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
  1477. xs->base = (void *)xs->header;
  1478. xs->here = xs->header->xh_entries;
  1479. /* Find the named attribute. */
  1480. if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
  1481. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  1482. if (ret && ret != -ENODATA)
  1483. return ret;
  1484. xs->not_found = ret;
  1485. }
  1486. return 0;
  1487. }
  1488. /*
  1489. * ocfs2_xattr_ibody_set()
  1490. *
  1491. * Set, replace or remove an extended attribute into inode block.
  1492. *
  1493. */
  1494. static int ocfs2_xattr_ibody_set(struct inode *inode,
  1495. struct ocfs2_xattr_info *xi,
  1496. struct ocfs2_xattr_search *xs)
  1497. {
  1498. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  1499. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1500. int ret;
  1501. if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
  1502. return -ENOSPC;
  1503. down_write(&oi->ip_alloc_sem);
  1504. if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
  1505. if (!ocfs2_xattr_has_space_inline(inode, di)) {
  1506. ret = -ENOSPC;
  1507. goto out;
  1508. }
  1509. }
  1510. ret = ocfs2_xattr_set_entry(inode, xi, xs,
  1511. (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
  1512. out:
  1513. up_write(&oi->ip_alloc_sem);
  1514. return ret;
  1515. }
  1516. /*
  1517. * ocfs2_xattr_block_find()
  1518. *
  1519. * Find extended attribute in external block and
  1520. * fill search info into struct ocfs2_xattr_search.
  1521. */
  1522. static int ocfs2_xattr_block_find(struct inode *inode,
  1523. int name_index,
  1524. const char *name,
  1525. struct ocfs2_xattr_search *xs)
  1526. {
  1527. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1528. struct buffer_head *blk_bh = NULL;
  1529. struct ocfs2_xattr_block *xb;
  1530. int ret = 0;
  1531. if (!di->i_xattr_loc)
  1532. return ret;
  1533. ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
  1534. if (ret < 0) {
  1535. mlog_errno(ret);
  1536. return ret;
  1537. }
  1538. /*Verify the signature of xattr block*/
  1539. if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
  1540. strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
  1541. ret = -EFAULT;
  1542. goto cleanup;
  1543. }
  1544. xs->xattr_bh = blk_bh;
  1545. xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
  1546. if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1547. xs->header = &xb->xb_attrs.xb_header;
  1548. xs->base = (void *)xs->header;
  1549. xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
  1550. xs->here = xs->header->xh_entries;
  1551. ret = ocfs2_xattr_find_entry(name_index, name, xs);
  1552. } else
  1553. ret = ocfs2_xattr_index_block_find(inode, blk_bh,
  1554. name_index,
  1555. name, xs);
  1556. if (ret && ret != -ENODATA) {
  1557. xs->xattr_bh = NULL;
  1558. goto cleanup;
  1559. }
  1560. xs->not_found = ret;
  1561. return 0;
  1562. cleanup:
  1563. brelse(blk_bh);
  1564. return ret;
  1565. }
  1566. /*
  1567. * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
  1568. * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
  1569. * re-initialized.
  1570. */
  1571. static int ocfs2_restore_xattr_block(struct inode *inode,
  1572. struct ocfs2_xattr_search *xs)
  1573. {
  1574. int ret;
  1575. handle_t *handle;
  1576. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1577. struct ocfs2_xattr_block *xb =
  1578. (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
  1579. struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
  1580. u16 xb_flags = le16_to_cpu(xb->xb_flags);
  1581. BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
  1582. le16_to_cpu(el->l_next_free_rec) != 0);
  1583. handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
  1584. if (IS_ERR(handle)) {
  1585. ret = PTR_ERR(handle);
  1586. handle = NULL;
  1587. goto out;
  1588. }
  1589. ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
  1590. OCFS2_JOURNAL_ACCESS_WRITE);
  1591. if (ret < 0) {
  1592. mlog_errno(ret);
  1593. goto out_commit;
  1594. }
  1595. memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
  1596. offsetof(struct ocfs2_xattr_block, xb_attrs));
  1597. xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
  1598. ocfs2_journal_dirty(handle, xs->xattr_bh);
  1599. out_commit:
  1600. ocfs2_commit_trans(osb, handle);
  1601. out:
  1602. return ret;
  1603. }
  1604. /*
  1605. * ocfs2_xattr_block_set()
  1606. *
  1607. * Set, replace or remove an extended attribute into external block.
  1608. *
  1609. */
  1610. static int ocfs2_xattr_block_set(struct inode *inode,
  1611. struct ocfs2_xattr_info *xi,
  1612. struct ocfs2_xattr_search *xs)
  1613. {
  1614. struct buffer_head *new_bh = NULL;
  1615. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1616. struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
  1617. struct ocfs2_alloc_context *meta_ac = NULL;
  1618. handle_t *handle = NULL;
  1619. struct ocfs2_xattr_block *xblk = NULL;
  1620. u16 suballoc_bit_start;
  1621. u32 num_got;
  1622. u64 first_blkno;
  1623. int ret;
  1624. if (!xs->xattr_bh) {
  1625. /*
  1626. * Alloc one external block for extended attribute
  1627. * outside of inode.
  1628. */
  1629. ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
  1630. if (ret < 0) {
  1631. mlog_errno(ret);
  1632. goto out;
  1633. }
  1634. handle = ocfs2_start_trans(osb,
  1635. OCFS2_XATTR_BLOCK_CREATE_CREDITS);
  1636. if (IS_ERR(handle)) {
  1637. ret = PTR_ERR(handle);
  1638. mlog_errno(ret);
  1639. goto out;
  1640. }
  1641. ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
  1642. OCFS2_JOURNAL_ACCESS_CREATE);
  1643. if (ret < 0) {
  1644. mlog_errno(ret);
  1645. goto out_commit;
  1646. }
  1647. ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
  1648. &suballoc_bit_start, &num_got,
  1649. &first_blkno);
  1650. if (ret < 0) {
  1651. mlog_errno(ret);
  1652. goto out_commit;
  1653. }
  1654. new_bh = sb_getblk(inode->i_sb, first_blkno);
  1655. ocfs2_set_new_buffer_uptodate(inode, new_bh);
  1656. ret = ocfs2_journal_access(handle, inode, new_bh,
  1657. OCFS2_JOURNAL_ACCESS_CREATE);
  1658. if (ret < 0) {
  1659. mlog_errno(ret);
  1660. goto out_commit;
  1661. }
  1662. /* Initialize ocfs2_xattr_block */
  1663. xs->xattr_bh = new_bh;
  1664. xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
  1665. memset(xblk, 0, inode->i_sb->s_blocksize);
  1666. strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
  1667. xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
  1668. xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
  1669. xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
  1670. xblk->xb_blkno = cpu_to_le64(first_blkno);
  1671. xs->header = &xblk->xb_attrs.xb_header;
  1672. xs->base = (void *)xs->header;
  1673. xs->end = (void *)xblk + inode->i_sb->s_blocksize;
  1674. xs->here = xs->header->xh_entries;
  1675. ret = ocfs2_journal_dirty(handle, new_bh);
  1676. if (ret < 0) {
  1677. mlog_errno(ret);
  1678. goto out_commit;
  1679. }
  1680. di->i_xattr_loc = cpu_to_le64(first_blkno);
  1681. ret = ocfs2_journal_dirty(handle, xs->inode_bh);
  1682. if (ret < 0)
  1683. mlog_errno(ret);
  1684. out_commit:
  1685. ocfs2_commit_trans(osb, handle);
  1686. out:
  1687. if (meta_ac)
  1688. ocfs2_free_alloc_context(meta_ac);
  1689. if (ret < 0)
  1690. return ret;
  1691. } else
  1692. xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
  1693. if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
  1694. /* Set extended attribute into external block */
  1695. ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
  1696. if (!ret || ret != -ENOSPC)
  1697. goto end;
  1698. ret = ocfs2_xattr_create_index_block(inode, xs);
  1699. if (ret)
  1700. goto end;
  1701. }
  1702. ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
  1703. if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0)
  1704. ret = ocfs2_restore_xattr_block(inode, xs);
  1705. end:
  1706. return ret;
  1707. }
  1708. /*
  1709. * ocfs2_xattr_set()
  1710. *
  1711. * Set, replace or remove an extended attribute for this inode.
  1712. * value is NULL to remove an existing extended attribute, else either
  1713. * create or replace an extended attribute.
  1714. */
  1715. int ocfs2_xattr_set(struct inode *inode,
  1716. int name_index,
  1717. const char *name,
  1718. const void *value,
  1719. size_t value_len,
  1720. int flags)
  1721. {
  1722. struct buffer_head *di_bh = NULL;
  1723. struct ocfs2_dinode *di;
  1724. int ret;
  1725. u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  1726. struct ocfs2_xattr_info xi = {
  1727. .name_index = name_index,
  1728. .name = name,
  1729. .value = value,
  1730. .value_len = value_len,
  1731. };
  1732. struct ocfs2_xattr_search xis = {
  1733. .not_found = -ENODATA,
  1734. };
  1735. struct ocfs2_xattr_search xbs = {
  1736. .not_found = -ENODATA,
  1737. };
  1738. if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
  1739. return -EOPNOTSUPP;
  1740. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1741. if (ret < 0) {
  1742. mlog_errno(ret);
  1743. return ret;
  1744. }
  1745. xis.inode_bh = xbs.inode_bh = di_bh;
  1746. di = (struct ocfs2_dinode *)di_bh->b_data;
  1747. down_write(&OCFS2_I(inode)->ip_xattr_sem);
  1748. /*
  1749. * Scan inode and external block to find the same name
  1750. * extended attribute and collect search infomation.
  1751. */
  1752. ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
  1753. if (ret)
  1754. goto cleanup;
  1755. if (xis.not_found) {
  1756. ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
  1757. if (ret)
  1758. goto cleanup;
  1759. }
  1760. if (xis.not_found && xbs.not_found) {
  1761. ret = -ENODATA;
  1762. if (flags & XATTR_REPLACE)
  1763. goto cleanup;
  1764. ret = 0;
  1765. if (!value)
  1766. goto cleanup;
  1767. } else {
  1768. ret = -EEXIST;
  1769. if (flags & XATTR_CREATE)
  1770. goto cleanup;
  1771. }
  1772. if (!value) {
  1773. /* Remove existing extended attribute */
  1774. if (!xis.not_found)
  1775. ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
  1776. else if (!xbs.not_found)
  1777. ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
  1778. } else {
  1779. /* We always try to set extended attribute into inode first*/
  1780. ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
  1781. if (!ret && !xbs.not_found) {
  1782. /*
  1783. * If succeed and that extended attribute existing in
  1784. * external block, then we will remove it.
  1785. */
  1786. xi.value = NULL;
  1787. xi.value_len = 0;
  1788. ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
  1789. } else if (ret == -ENOSPC) {
  1790. if (di->i_xattr_loc && !xbs.xattr_bh) {
  1791. ret = ocfs2_xattr_block_find(inode, name_index,
  1792. name, &xbs);
  1793. if (ret)
  1794. goto cleanup;
  1795. }
  1796. /*
  1797. * If no space in inode, we will set extended attribute
  1798. * into external block.
  1799. */
  1800. ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
  1801. if (ret)
  1802. goto cleanup;
  1803. if (!xis.not_found) {
  1804. /*
  1805. * If succeed and that extended attribute
  1806. * existing in inode, we will remove it.
  1807. */
  1808. xi.value = NULL;
  1809. xi.value_len = 0;
  1810. ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
  1811. }
  1812. }
  1813. }
  1814. cleanup:
  1815. up_write(&OCFS2_I(inode)->ip_xattr_sem);
  1816. ocfs2_inode_unlock(inode, 1);
  1817. brelse(di_bh);
  1818. brelse(xbs.xattr_bh);
  1819. for (i = 0; i < blk_per_bucket; i++)
  1820. brelse(xbs.bucket.bhs[i]);
  1821. return ret;
  1822. }
  1823. /*
  1824. * Find the xattr extent rec which may contains name_hash.
  1825. * e_cpos will be the first name hash of the xattr rec.
  1826. * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
  1827. */
  1828. static int ocfs2_xattr_get_rec(struct inode *inode,
  1829. u32 name_hash,
  1830. u64 *p_blkno,
  1831. u32 *e_cpos,
  1832. u32 *num_clusters,
  1833. struct ocfs2_extent_list *el)
  1834. {
  1835. int ret = 0, i;
  1836. struct buffer_head *eb_bh = NULL;
  1837. struct ocfs2_extent_block *eb;
  1838. struct ocfs2_extent_rec *rec = NULL;
  1839. u64 e_blkno = 0;
  1840. if (el->l_tree_depth) {
  1841. ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
  1842. if (ret) {
  1843. mlog_errno(ret);
  1844. goto out;
  1845. }
  1846. eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  1847. el = &eb->h_list;
  1848. if (el->l_tree_depth) {
  1849. ocfs2_error(inode->i_sb,
  1850. "Inode %lu has non zero tree depth in "
  1851. "xattr tree block %llu\n", inode->i_ino,
  1852. (unsigned long long)eb_bh->b_blocknr);
  1853. ret = -EROFS;
  1854. goto out;
  1855. }
  1856. }
  1857. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  1858. rec = &el->l_recs[i];
  1859. if (le32_to_cpu(rec->e_cpos) <= name_hash) {
  1860. e_blkno = le64_to_cpu(rec->e_blkno);
  1861. break;
  1862. }
  1863. }
  1864. if (!e_blkno) {
  1865. ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
  1866. "record (%u, %u, 0) in xattr", inode->i_ino,
  1867. le32_to_cpu(rec->e_cpos),
  1868. ocfs2_rec_clusters(el, rec));
  1869. ret = -EROFS;
  1870. goto out;
  1871. }
  1872. *p_blkno = le64_to_cpu(rec->e_blkno);
  1873. *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
  1874. if (e_cpos)
  1875. *e_cpos = le32_to_cpu(rec->e_cpos);
  1876. out:
  1877. brelse(eb_bh);
  1878. return ret;
  1879. }
  1880. typedef int (xattr_bucket_func)(struct inode *inode,
  1881. struct ocfs2_xattr_bucket *bucket,
  1882. void *para);
  1883. static int ocfs2_find_xe_in_bucket(struct inode *inode,
  1884. struct buffer_head *header_bh,
  1885. int name_index,
  1886. const char *name,
  1887. u32 name_hash,
  1888. u16 *xe_index,
  1889. int *found)
  1890. {
  1891. int i, ret = 0, cmp = 1, block_off, new_offset;
  1892. struct ocfs2_xattr_header *xh =
  1893. (struct ocfs2_xattr_header *)header_bh->b_data;
  1894. size_t name_len = strlen(name);
  1895. struct ocfs2_xattr_entry *xe = NULL;
  1896. struct buffer_head *name_bh = NULL;
  1897. char *xe_name;
  1898. /*
  1899. * We don't use binary search in the bucket because there
  1900. * may be multiple entries with the same name hash.
  1901. */
  1902. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  1903. xe = &xh->xh_entries[i];
  1904. if (name_hash > le32_to_cpu(xe->xe_name_hash))
  1905. continue;
  1906. else if (name_hash < le32_to_cpu(xe->xe_name_hash))
  1907. break;
  1908. cmp = name_index - ocfs2_xattr_get_type(xe);
  1909. if (!cmp)
  1910. cmp = name_len - xe->xe_name_len;
  1911. if (cmp)
  1912. continue;
  1913. ret = ocfs2_xattr_bucket_get_name_value(inode,
  1914. xh,
  1915. i,
  1916. &block_off,
  1917. &new_offset);
  1918. if (ret) {
  1919. mlog_errno(ret);
  1920. break;
  1921. }
  1922. ret = ocfs2_read_block(inode, header_bh->b_blocknr + block_off,
  1923. &name_bh);
  1924. if (ret) {
  1925. mlog_errno(ret);
  1926. break;
  1927. }
  1928. xe_name = name_bh->b_data + new_offset;
  1929. cmp = memcmp(name, xe_name, name_len);
  1930. brelse(name_bh);
  1931. name_bh = NULL;
  1932. if (cmp == 0) {
  1933. *xe_index = i;
  1934. *found = 1;
  1935. ret = 0;
  1936. break;
  1937. }
  1938. }
  1939. return ret;
  1940. }
  1941. /*
  1942. * Find the specified xattr entry in a series of buckets.
  1943. * This series start from p_blkno and last for num_clusters.
  1944. * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
  1945. * the num of the valid buckets.
  1946. *
  1947. * Return the buffer_head this xattr should reside in. And if the xattr's
  1948. * hash is in the gap of 2 buckets, return the lower bucket.
  1949. */
  1950. static int ocfs2_xattr_bucket_find(struct inode *inode,
  1951. int name_index,
  1952. const char *name,
  1953. u32 name_hash,
  1954. u64 p_blkno,
  1955. u32 first_hash,
  1956. u32 num_clusters,
  1957. struct ocfs2_xattr_search *xs)
  1958. {
  1959. int ret, found = 0;
  1960. struct buffer_head *bh = NULL;
  1961. struct buffer_head *lower_bh = NULL;
  1962. struct ocfs2_xattr_header *xh = NULL;
  1963. struct ocfs2_xattr_entry *xe = NULL;
  1964. u16 index = 0;
  1965. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  1966. int low_bucket = 0, bucket, high_bucket;
  1967. u32 last_hash;
  1968. u64 blkno;
  1969. ret = ocfs2_read_block(inode, p_blkno, &bh);
  1970. if (ret) {
  1971. mlog_errno(ret);
  1972. goto out;
  1973. }
  1974. xh = (struct ocfs2_xattr_header *)bh->b_data;
  1975. high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
  1976. while (low_bucket <= high_bucket) {
  1977. brelse(bh);
  1978. bh = NULL;
  1979. bucket = (low_bucket + high_bucket) / 2;
  1980. blkno = p_blkno + bucket * blk_per_bucket;
  1981. ret = ocfs2_read_block(inode, blkno, &bh);
  1982. if (ret) {
  1983. mlog_errno(ret);
  1984. goto out;
  1985. }
  1986. xh = (struct ocfs2_xattr_header *)bh->b_data;
  1987. xe = &xh->xh_entries[0];
  1988. if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
  1989. high_bucket = bucket - 1;
  1990. continue;
  1991. }
  1992. /*
  1993. * Check whether the hash of the last entry in our
  1994. * bucket is larger than the search one. for an empty
  1995. * bucket, the last one is also the first one.
  1996. */
  1997. if (xh->xh_count)
  1998. xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
  1999. last_hash = le32_to_cpu(xe->xe_name_hash);
  2000. /* record lower_bh which may be the insert place. */
  2001. brelse(lower_bh);
  2002. lower_bh = bh;
  2003. bh = NULL;
  2004. if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
  2005. low_bucket = bucket + 1;
  2006. continue;
  2007. }
  2008. /* the searched xattr should reside in this bucket if exists. */
  2009. ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
  2010. name_index, name, name_hash,
  2011. &index, &found);
  2012. if (ret) {
  2013. mlog_errno(ret);
  2014. goto out;
  2015. }
  2016. break;
  2017. }
  2018. /*
  2019. * Record the bucket we have found.
  2020. * When the xattr's hash value is in the gap of 2 buckets, we will
  2021. * always set it to the previous bucket.
  2022. */
  2023. if (!lower_bh) {
  2024. /*
  2025. * We can't find any bucket whose first name_hash is less
  2026. * than the find name_hash.
  2027. */
  2028. BUG_ON(bh->b_blocknr != p_blkno);
  2029. lower_bh = bh;
  2030. bh = NULL;
  2031. }
  2032. xs->bucket.bhs[0] = lower_bh;
  2033. xs->bucket.xh = (struct ocfs2_xattr_header *)
  2034. xs->bucket.bhs[0]->b_data;
  2035. lower_bh = NULL;
  2036. xs->header = xs->bucket.xh;
  2037. xs->base = xs->bucket.bhs[0]->b_data;
  2038. xs->end = xs->base + inode->i_sb->s_blocksize;
  2039. if (found) {
  2040. /*
  2041. * If we have found the xattr enty, read all the blocks in
  2042. * this bucket.
  2043. */
  2044. ret = ocfs2_read_blocks(inode, xs->bucket.bhs[0]->b_blocknr + 1,
  2045. blk_per_bucket - 1, &xs->bucket.bhs[1],
  2046. OCFS2_BH_CACHED);
  2047. if (ret) {
  2048. mlog_errno(ret);
  2049. goto out;
  2050. }
  2051. xs->here = &xs->header->xh_entries[index];
  2052. mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
  2053. (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
  2054. } else
  2055. ret = -ENODATA;
  2056. out:
  2057. brelse(bh);
  2058. brelse(lower_bh);
  2059. return ret;
  2060. }
  2061. static int ocfs2_xattr_index_block_find(struct inode *inode,
  2062. struct buffer_head *root_bh,
  2063. int name_index,
  2064. const char *name,
  2065. struct ocfs2_xattr_search *xs)
  2066. {
  2067. int ret;
  2068. struct ocfs2_xattr_block *xb =
  2069. (struct ocfs2_xattr_block *)root_bh->b_data;
  2070. struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
  2071. struct ocfs2_extent_list *el = &xb_root->xt_list;
  2072. u64 p_blkno = 0;
  2073. u32 first_hash, num_clusters = 0;
  2074. u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
  2075. if (le16_to_cpu(el->l_next_free_rec) == 0)
  2076. return -ENODATA;
  2077. mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
  2078. name, name_hash, name_index);
  2079. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
  2080. &num_clusters, el);
  2081. if (ret) {
  2082. mlog_errno(ret);
  2083. goto out;
  2084. }
  2085. BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
  2086. mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
  2087. "in the rec is %u\n", num_clusters, p_blkno, first_hash);
  2088. ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
  2089. p_blkno, first_hash, num_clusters, xs);
  2090. out:
  2091. return ret;
  2092. }
  2093. static int ocfs2_iterate_xattr_buckets(struct inode *inode,
  2094. u64 blkno,
  2095. u32 clusters,
  2096. xattr_bucket_func *func,
  2097. void *para)
  2098. {
  2099. int i, j, ret = 0;
  2100. int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2101. u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
  2102. u32 num_buckets = clusters * bpc;
  2103. struct ocfs2_xattr_bucket bucket;
  2104. memset(&bucket, 0, sizeof(bucket));
  2105. mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
  2106. clusters, blkno);
  2107. for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
  2108. ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket,
  2109. bucket.bhs, OCFS2_BH_CACHED);
  2110. if (ret) {
  2111. mlog_errno(ret);
  2112. goto out;
  2113. }
  2114. bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
  2115. /*
  2116. * The real bucket num in this series of blocks is stored
  2117. * in the 1st bucket.
  2118. */
  2119. if (i == 0)
  2120. num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
  2121. mlog(0, "iterating xattr bucket %llu, first hash %u\n", blkno,
  2122. le32_to_cpu(bucket.xh->xh_entries[0].xe_name_hash));
  2123. if (func) {
  2124. ret = func(inode, &bucket, para);
  2125. if (ret) {
  2126. mlog_errno(ret);
  2127. break;
  2128. }
  2129. }
  2130. for (j = 0; j < blk_per_bucket; j++)
  2131. brelse(bucket.bhs[j]);
  2132. memset(&bucket, 0, sizeof(bucket));
  2133. }
  2134. out:
  2135. for (j = 0; j < blk_per_bucket; j++)
  2136. brelse(bucket.bhs[j]);
  2137. return ret;
  2138. }
  2139. struct ocfs2_xattr_tree_list {
  2140. char *buffer;
  2141. size_t buffer_size;
  2142. size_t result;
  2143. };
  2144. static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
  2145. struct ocfs2_xattr_header *xh,
  2146. int index,
  2147. int *block_off,
  2148. int *new_offset)
  2149. {
  2150. u16 name_offset;
  2151. if (index < 0 || index >= le16_to_cpu(xh->xh_count))
  2152. return -EINVAL;
  2153. name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
  2154. *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
  2155. *new_offset = name_offset % inode->i_sb->s_blocksize;
  2156. return 0;
  2157. }
  2158. static int ocfs2_list_xattr_bucket(struct inode *inode,
  2159. struct ocfs2_xattr_bucket *bucket,
  2160. void *para)
  2161. {
  2162. int ret = 0, type;
  2163. struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
  2164. int i, block_off, new_offset;
  2165. const char *prefix, *name;
  2166. for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
  2167. struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
  2168. type = ocfs2_xattr_get_type(entry);
  2169. prefix = ocfs2_xattr_prefix(type);
  2170. if (prefix) {
  2171. ret = ocfs2_xattr_bucket_get_name_value(inode,
  2172. bucket->xh,
  2173. i,
  2174. &block_off,
  2175. &new_offset);
  2176. if (ret)
  2177. break;
  2178. name = (const char *)bucket->bhs[block_off]->b_data +
  2179. new_offset;
  2180. ret = ocfs2_xattr_list_entry(xl->buffer,
  2181. xl->buffer_size,
  2182. &xl->result,
  2183. prefix, name,
  2184. entry->xe_name_len);
  2185. if (ret)
  2186. break;
  2187. }
  2188. }
  2189. return ret;
  2190. }
  2191. static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
  2192. struct ocfs2_xattr_tree_root *xt,
  2193. char *buffer,
  2194. size_t buffer_size)
  2195. {
  2196. struct ocfs2_extent_list *el = &xt->xt_list;
  2197. int ret = 0;
  2198. u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
  2199. u64 p_blkno = 0;
  2200. struct ocfs2_xattr_tree_list xl = {
  2201. .buffer = buffer,
  2202. .buffer_size = buffer_size,
  2203. .result = 0,
  2204. };
  2205. if (le16_to_cpu(el->l_next_free_rec) == 0)
  2206. return 0;
  2207. while (name_hash > 0) {
  2208. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
  2209. &e_cpos, &num_clusters, el);
  2210. if (ret) {
  2211. mlog_errno(ret);
  2212. goto out;
  2213. }
  2214. ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
  2215. ocfs2_list_xattr_bucket,
  2216. &xl);
  2217. if (ret) {
  2218. mlog_errno(ret);
  2219. goto out;
  2220. }
  2221. if (e_cpos == 0)
  2222. break;
  2223. name_hash = e_cpos - 1;
  2224. }
  2225. ret = xl.result;
  2226. out:
  2227. return ret;
  2228. }
  2229. static int cmp_xe(const void *a, const void *b)
  2230. {
  2231. const struct ocfs2_xattr_entry *l = a, *r = b;
  2232. u32 l_hash = le32_to_cpu(l->xe_name_hash);
  2233. u32 r_hash = le32_to_cpu(r->xe_name_hash);
  2234. if (l_hash > r_hash)
  2235. return 1;
  2236. if (l_hash < r_hash)
  2237. return -1;
  2238. return 0;
  2239. }
  2240. static void swap_xe(void *a, void *b, int size)
  2241. {
  2242. struct ocfs2_xattr_entry *l = a, *r = b, tmp;
  2243. tmp = *l;
  2244. memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
  2245. memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
  2246. }
  2247. /*
  2248. * When the ocfs2_xattr_block is filled up, new bucket will be created
  2249. * and all the xattr entries will be moved to the new bucket.
  2250. * Note: we need to sort the entries since they are not saved in order
  2251. * in the ocfs2_xattr_block.
  2252. */
  2253. static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
  2254. struct buffer_head *xb_bh,
  2255. struct buffer_head *xh_bh,
  2256. struct buffer_head *data_bh)
  2257. {
  2258. int i, blocksize = inode->i_sb->s_blocksize;
  2259. u16 offset, size, off_change;
  2260. struct ocfs2_xattr_entry *xe;
  2261. struct ocfs2_xattr_block *xb =
  2262. (struct ocfs2_xattr_block *)xb_bh->b_data;
  2263. struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
  2264. struct ocfs2_xattr_header *xh =
  2265. (struct ocfs2_xattr_header *)xh_bh->b_data;
  2266. u16 count = le16_to_cpu(xb_xh->xh_count);
  2267. char *target = xh_bh->b_data, *src = xb_bh->b_data;
  2268. mlog(0, "cp xattr from block %llu to bucket %llu\n",
  2269. (unsigned long long)xb_bh->b_blocknr,
  2270. (unsigned long long)xh_bh->b_blocknr);
  2271. memset(xh_bh->b_data, 0, blocksize);
  2272. if (data_bh)
  2273. memset(data_bh->b_data, 0, blocksize);
  2274. /*
  2275. * Since the xe_name_offset is based on ocfs2_xattr_header,
  2276. * there is a offset change corresponding to the change of
  2277. * ocfs2_xattr_header's position.
  2278. */
  2279. off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
  2280. xe = &xb_xh->xh_entries[count - 1];
  2281. offset = le16_to_cpu(xe->xe_name_offset) + off_change;
  2282. size = blocksize - offset;
  2283. /* copy all the names and values. */
  2284. if (data_bh)
  2285. target = data_bh->b_data;
  2286. memcpy(target + offset, src + offset, size);
  2287. /* Init new header now. */
  2288. xh->xh_count = xb_xh->xh_count;
  2289. xh->xh_num_buckets = cpu_to_le16(1);
  2290. xh->xh_name_value_len = cpu_to_le16(size);
  2291. xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
  2292. /* copy all the entries. */
  2293. target = xh_bh->b_data;
  2294. offset = offsetof(struct ocfs2_xattr_header, xh_entries);
  2295. size = count * sizeof(struct ocfs2_xattr_entry);
  2296. memcpy(target + offset, (char *)xb_xh + offset, size);
  2297. /* Change the xe offset for all the xe because of the move. */
  2298. off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
  2299. offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
  2300. for (i = 0; i < count; i++)
  2301. le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
  2302. mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
  2303. offset, size, off_change);
  2304. sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
  2305. cmp_xe, swap_xe);
  2306. }
  2307. /*
  2308. * After we move xattr from block to index btree, we have to
  2309. * update ocfs2_xattr_search to the new xe and base.
  2310. *
  2311. * When the entry is in xattr block, xattr_bh indicates the storage place.
  2312. * While if the entry is in index b-tree, "bucket" indicates the
  2313. * real place of the xattr.
  2314. */
  2315. static int ocfs2_xattr_update_xattr_search(struct inode *inode,
  2316. struct ocfs2_xattr_search *xs,
  2317. struct buffer_head *old_bh,
  2318. struct buffer_head *new_bh)
  2319. {
  2320. int ret = 0;
  2321. char *buf = old_bh->b_data;
  2322. struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
  2323. struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
  2324. int i, blocksize = inode->i_sb->s_blocksize;
  2325. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2326. xs->bucket.bhs[0] = new_bh;
  2327. get_bh(new_bh);
  2328. xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
  2329. xs->header = xs->bucket.xh;
  2330. xs->base = new_bh->b_data;
  2331. xs->end = xs->base + inode->i_sb->s_blocksize;
  2332. if (!xs->not_found) {
  2333. if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
  2334. ret = ocfs2_read_blocks(inode,
  2335. xs->bucket.bhs[0]->b_blocknr + 1,
  2336. blk_per_bucket - 1, &xs->bucket.bhs[1],
  2337. OCFS2_BH_CACHED);
  2338. if (ret) {
  2339. mlog_errno(ret);
  2340. return ret;
  2341. }
  2342. i = xs->here - old_xh->xh_entries;
  2343. xs->here = &xs->header->xh_entries[i];
  2344. }
  2345. }
  2346. return ret;
  2347. }
  2348. static int ocfs2_xattr_create_index_block(struct inode *inode,
  2349. struct ocfs2_xattr_search *xs)
  2350. {
  2351. int ret, credits = OCFS2_SUBALLOC_ALLOC;
  2352. u32 bit_off, len;
  2353. u64 blkno;
  2354. handle_t *handle;
  2355. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2356. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  2357. struct ocfs2_alloc_context *data_ac;
  2358. struct buffer_head *xh_bh = NULL, *data_bh = NULL;
  2359. struct buffer_head *xb_bh = xs->xattr_bh;
  2360. struct ocfs2_xattr_block *xb =
  2361. (struct ocfs2_xattr_block *)xb_bh->b_data;
  2362. struct ocfs2_xattr_tree_root *xr;
  2363. u16 xb_flags = le16_to_cpu(xb->xb_flags);
  2364. u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2365. mlog(0, "create xattr index block for %llu\n",
  2366. (unsigned long long)xb_bh->b_blocknr);
  2367. BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
  2368. ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
  2369. if (ret) {
  2370. mlog_errno(ret);
  2371. goto out;
  2372. }
  2373. /*
  2374. * XXX:
  2375. * We can use this lock for now, and maybe move to a dedicated mutex
  2376. * if performance becomes a problem later.
  2377. */
  2378. down_write(&oi->ip_alloc_sem);
  2379. /*
  2380. * 3 more credits, one for xattr block update, one for the 1st block
  2381. * of the new xattr bucket and one for the value/data.
  2382. */
  2383. credits += 3;
  2384. handle = ocfs2_start_trans(osb, credits);
  2385. if (IS_ERR(handle)) {
  2386. ret = PTR_ERR(handle);
  2387. mlog_errno(ret);
  2388. goto out_sem;
  2389. }
  2390. ret = ocfs2_journal_access(handle, inode, xb_bh,
  2391. OCFS2_JOURNAL_ACCESS_WRITE);
  2392. if (ret) {
  2393. mlog_errno(ret);
  2394. goto out_commit;
  2395. }
  2396. ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
  2397. if (ret) {
  2398. mlog_errno(ret);
  2399. goto out_commit;
  2400. }
  2401. /*
  2402. * The bucket may spread in many blocks, and
  2403. * we will only touch the 1st block and the last block
  2404. * in the whole bucket(one for entry and one for data).
  2405. */
  2406. blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
  2407. mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
  2408. xh_bh = sb_getblk(inode->i_sb, blkno);
  2409. if (!xh_bh) {
  2410. ret = -EIO;
  2411. mlog_errno(ret);
  2412. goto out_commit;
  2413. }
  2414. ocfs2_set_new_buffer_uptodate(inode, xh_bh);
  2415. ret = ocfs2_journal_access(handle, inode, xh_bh,
  2416. OCFS2_JOURNAL_ACCESS_CREATE);
  2417. if (ret) {
  2418. mlog_errno(ret);
  2419. goto out_commit;
  2420. }
  2421. if (bpb > 1) {
  2422. data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
  2423. if (!data_bh) {
  2424. ret = -EIO;
  2425. mlog_errno(ret);
  2426. goto out_commit;
  2427. }
  2428. ocfs2_set_new_buffer_uptodate(inode, data_bh);
  2429. ret = ocfs2_journal_access(handle, inode, data_bh,
  2430. OCFS2_JOURNAL_ACCESS_CREATE);
  2431. if (ret) {
  2432. mlog_errno(ret);
  2433. goto out_commit;
  2434. }
  2435. }
  2436. ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
  2437. ocfs2_journal_dirty(handle, xh_bh);
  2438. if (data_bh)
  2439. ocfs2_journal_dirty(handle, data_bh);
  2440. ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
  2441. /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
  2442. memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
  2443. offsetof(struct ocfs2_xattr_block, xb_attrs));
  2444. xr = &xb->xb_attrs.xb_root;
  2445. xr->xt_clusters = cpu_to_le32(1);
  2446. xr->xt_last_eb_blk = 0;
  2447. xr->xt_list.l_tree_depth = 0;
  2448. xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
  2449. xr->xt_list.l_next_free_rec = cpu_to_le16(1);
  2450. xr->xt_list.l_recs[0].e_cpos = 0;
  2451. xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
  2452. xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
  2453. xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
  2454. ret = ocfs2_journal_dirty(handle, xb_bh);
  2455. if (ret) {
  2456. mlog_errno(ret);
  2457. goto out_commit;
  2458. }
  2459. out_commit:
  2460. ocfs2_commit_trans(osb, handle);
  2461. out_sem:
  2462. up_write(&oi->ip_alloc_sem);
  2463. out:
  2464. if (data_ac)
  2465. ocfs2_free_alloc_context(data_ac);
  2466. brelse(xh_bh);
  2467. brelse(data_bh);
  2468. return ret;
  2469. }
  2470. static int cmp_xe_offset(const void *a, const void *b)
  2471. {
  2472. const struct ocfs2_xattr_entry *l = a, *r = b;
  2473. u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
  2474. u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
  2475. if (l_name_offset < r_name_offset)
  2476. return 1;
  2477. if (l_name_offset > r_name_offset)
  2478. return -1;
  2479. return 0;
  2480. }
  2481. /*
  2482. * defrag a xattr bucket if we find that the bucket has some
  2483. * holes beteen name/value pairs.
  2484. * We will move all the name/value pairs to the end of the bucket
  2485. * so that we can spare some space for insertion.
  2486. */
  2487. static int ocfs2_defrag_xattr_bucket(struct inode *inode,
  2488. struct ocfs2_xattr_bucket *bucket)
  2489. {
  2490. int ret, i;
  2491. size_t end, offset, len, value_len;
  2492. struct ocfs2_xattr_header *xh;
  2493. char *entries, *buf, *bucket_buf = NULL;
  2494. u64 blkno = bucket->bhs[0]->b_blocknr;
  2495. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2496. u16 xh_free_start;
  2497. size_t blocksize = inode->i_sb->s_blocksize;
  2498. handle_t *handle;
  2499. struct buffer_head **bhs;
  2500. struct ocfs2_xattr_entry *xe;
  2501. bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
  2502. GFP_NOFS);
  2503. if (!bhs)
  2504. return -ENOMEM;
  2505. ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket, bhs,
  2506. OCFS2_BH_CACHED);
  2507. if (ret)
  2508. goto out;
  2509. /*
  2510. * In order to make the operation more efficient and generic,
  2511. * we copy all the blocks into a contiguous memory and do the
  2512. * defragment there, so if anything is error, we will not touch
  2513. * the real block.
  2514. */
  2515. bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
  2516. if (!bucket_buf) {
  2517. ret = -EIO;
  2518. goto out;
  2519. }
  2520. buf = bucket_buf;
  2521. for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
  2522. memcpy(buf, bhs[i]->b_data, blocksize);
  2523. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
  2524. if (IS_ERR(handle)) {
  2525. ret = PTR_ERR(handle);
  2526. handle = NULL;
  2527. mlog_errno(ret);
  2528. goto out;
  2529. }
  2530. for (i = 0; i < blk_per_bucket; i++) {
  2531. ret = ocfs2_journal_access(handle, inode, bhs[i],
  2532. OCFS2_JOURNAL_ACCESS_WRITE);
  2533. if (ret < 0) {
  2534. mlog_errno(ret);
  2535. goto commit;
  2536. }
  2537. }
  2538. xh = (struct ocfs2_xattr_header *)bucket_buf;
  2539. entries = (char *)xh->xh_entries;
  2540. xh_free_start = le16_to_cpu(xh->xh_free_start);
  2541. mlog(0, "adjust xattr bucket in %llu, count = %u, "
  2542. "xh_free_start = %u, xh_name_value_len = %u.\n",
  2543. blkno, le16_to_cpu(xh->xh_count), xh_free_start,
  2544. le16_to_cpu(xh->xh_name_value_len));
  2545. /*
  2546. * sort all the entries by their offset.
  2547. * the largest will be the first, so that we can
  2548. * move them to the end one by one.
  2549. */
  2550. sort(entries, le16_to_cpu(xh->xh_count),
  2551. sizeof(struct ocfs2_xattr_entry),
  2552. cmp_xe_offset, swap_xe);
  2553. /* Move all name/values to the end of the bucket. */
  2554. xe = xh->xh_entries;
  2555. end = OCFS2_XATTR_BUCKET_SIZE;
  2556. for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
  2557. offset = le16_to_cpu(xe->xe_name_offset);
  2558. if (ocfs2_xattr_is_local(xe))
  2559. value_len = OCFS2_XATTR_SIZE(
  2560. le64_to_cpu(xe->xe_value_size));
  2561. else
  2562. value_len = OCFS2_XATTR_ROOT_SIZE;
  2563. len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
  2564. /*
  2565. * We must make sure that the name/value pair
  2566. * exist in the same block. So adjust end to
  2567. * the previous block end if needed.
  2568. */
  2569. if (((end - len) / blocksize !=
  2570. (end - 1) / blocksize))
  2571. end = end - end % blocksize;
  2572. if (end > offset + len) {
  2573. memmove(bucket_buf + end - len,
  2574. bucket_buf + offset, len);
  2575. xe->xe_name_offset = cpu_to_le16(end - len);
  2576. }
  2577. mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
  2578. "bucket %llu\n", (unsigned long long)blkno);
  2579. end -= len;
  2580. }
  2581. mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
  2582. "bucket %llu\n", (unsigned long long)blkno);
  2583. if (xh_free_start == end)
  2584. goto commit;
  2585. memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
  2586. xh->xh_free_start = cpu_to_le16(end);
  2587. /* sort the entries by their name_hash. */
  2588. sort(entries, le16_to_cpu(xh->xh_count),
  2589. sizeof(struct ocfs2_xattr_entry),
  2590. cmp_xe, swap_xe);
  2591. buf = bucket_buf;
  2592. for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
  2593. memcpy(bhs[i]->b_data, buf, blocksize);
  2594. ocfs2_journal_dirty(handle, bhs[i]);
  2595. }
  2596. commit:
  2597. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  2598. out:
  2599. if (bhs) {
  2600. for (i = 0; i < blk_per_bucket; i++)
  2601. brelse(bhs[i]);
  2602. }
  2603. kfree(bhs);
  2604. kfree(bucket_buf);
  2605. return ret;
  2606. }
  2607. /*
  2608. * Move half nums of the xattr bucket in the previous cluster to this new
  2609. * cluster. We only touch the last cluster of the previous extend record.
  2610. *
  2611. * first_bh is the first buffer_head of a series of bucket in the same
  2612. * extent rec and header_bh is the header of one bucket in this cluster.
  2613. * They will be updated if we move the data header_bh contains to the new
  2614. * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
  2615. */
  2616. static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
  2617. handle_t *handle,
  2618. struct buffer_head **first_bh,
  2619. struct buffer_head **header_bh,
  2620. u64 new_blkno,
  2621. u64 prev_blkno,
  2622. u32 num_clusters,
  2623. u32 *first_hash)
  2624. {
  2625. int i, ret, credits;
  2626. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2627. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  2628. int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
  2629. int blocksize = inode->i_sb->s_blocksize;
  2630. struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
  2631. struct ocfs2_xattr_header *new_xh;
  2632. struct ocfs2_xattr_header *xh =
  2633. (struct ocfs2_xattr_header *)((*first_bh)->b_data);
  2634. BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
  2635. BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
  2636. prev_bh = *first_bh;
  2637. get_bh(prev_bh);
  2638. xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
  2639. prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
  2640. mlog(0, "move half of xattrs in cluster %llu to %llu\n",
  2641. prev_blkno, new_blkno);
  2642. /*
  2643. * We need to update the 1st half of the new cluster and
  2644. * 1 more for the update of the 1st bucket of the previous
  2645. * extent record.
  2646. */
  2647. credits = bpc / 2 + 1;
  2648. ret = ocfs2_extend_trans(handle, credits);
  2649. if (ret) {
  2650. mlog_errno(ret);
  2651. goto out;
  2652. }
  2653. ret = ocfs2_journal_access(handle, inode, prev_bh,
  2654. OCFS2_JOURNAL_ACCESS_WRITE);
  2655. if (ret) {
  2656. mlog_errno(ret);
  2657. goto out;
  2658. }
  2659. for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
  2660. old_bh = new_bh = NULL;
  2661. new_bh = sb_getblk(inode->i_sb, new_blkno);
  2662. if (!new_bh) {
  2663. ret = -EIO;
  2664. mlog_errno(ret);
  2665. goto out;
  2666. }
  2667. ocfs2_set_new_buffer_uptodate(inode, new_bh);
  2668. ret = ocfs2_journal_access(handle, inode, new_bh,
  2669. OCFS2_JOURNAL_ACCESS_CREATE);
  2670. if (ret < 0) {
  2671. mlog_errno(ret);
  2672. brelse(new_bh);
  2673. goto out;
  2674. }
  2675. ret = ocfs2_read_block(inode, prev_blkno, &old_bh);
  2676. if (ret < 0) {
  2677. mlog_errno(ret);
  2678. brelse(new_bh);
  2679. goto out;
  2680. }
  2681. memcpy(new_bh->b_data, old_bh->b_data, blocksize);
  2682. if (i == 0) {
  2683. new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
  2684. new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
  2685. if (first_hash)
  2686. *first_hash = le32_to_cpu(
  2687. new_xh->xh_entries[0].xe_name_hash);
  2688. new_first_bh = new_bh;
  2689. get_bh(new_first_bh);
  2690. }
  2691. ocfs2_journal_dirty(handle, new_bh);
  2692. if (*header_bh == old_bh) {
  2693. brelse(*header_bh);
  2694. *header_bh = new_bh;
  2695. get_bh(*header_bh);
  2696. brelse(*first_bh);
  2697. *first_bh = new_first_bh;
  2698. get_bh(*first_bh);
  2699. }
  2700. brelse(new_bh);
  2701. brelse(old_bh);
  2702. }
  2703. le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
  2704. ocfs2_journal_dirty(handle, prev_bh);
  2705. out:
  2706. brelse(prev_bh);
  2707. brelse(new_first_bh);
  2708. return ret;
  2709. }
  2710. static int ocfs2_read_xattr_bucket(struct inode *inode,
  2711. u64 blkno,
  2712. struct buffer_head **bhs,
  2713. int new)
  2714. {
  2715. int ret = 0;
  2716. u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2717. if (!new)
  2718. return ocfs2_read_blocks(inode, blkno,
  2719. blk_per_bucket, bhs,
  2720. OCFS2_BH_CACHED);
  2721. for (i = 0; i < blk_per_bucket; i++) {
  2722. bhs[i] = sb_getblk(inode->i_sb, blkno + i);
  2723. if (bhs[i] == NULL) {
  2724. ret = -EIO;
  2725. mlog_errno(ret);
  2726. break;
  2727. }
  2728. ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
  2729. }
  2730. return ret;
  2731. }
  2732. /*
  2733. * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
  2734. * first_hash will record the 1st hash of the new bucket.
  2735. */
  2736. static int ocfs2_half_xattr_bucket(struct inode *inode,
  2737. handle_t *handle,
  2738. u64 blk,
  2739. u64 new_blk,
  2740. u32 *first_hash,
  2741. int new_bucket_head)
  2742. {
  2743. int ret, i;
  2744. u16 count, start, len, name_value_len, xe_len, name_offset;
  2745. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2746. struct buffer_head **s_bhs, **t_bhs = NULL;
  2747. struct ocfs2_xattr_header *xh;
  2748. struct ocfs2_xattr_entry *xe;
  2749. int blocksize = inode->i_sb->s_blocksize;
  2750. mlog(0, "move half of xattrs from bucket %llu to %llu\n",
  2751. blk, new_blk);
  2752. s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
  2753. if (!s_bhs)
  2754. return -ENOMEM;
  2755. ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
  2756. if (ret) {
  2757. mlog_errno(ret);
  2758. goto out;
  2759. }
  2760. ret = ocfs2_journal_access(handle, inode, s_bhs[0],
  2761. OCFS2_JOURNAL_ACCESS_WRITE);
  2762. if (ret) {
  2763. mlog_errno(ret);
  2764. goto out;
  2765. }
  2766. t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
  2767. if (!t_bhs) {
  2768. ret = -ENOMEM;
  2769. goto out;
  2770. }
  2771. ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
  2772. if (ret) {
  2773. mlog_errno(ret);
  2774. goto out;
  2775. }
  2776. for (i = 0; i < blk_per_bucket; i++) {
  2777. ret = ocfs2_journal_access(handle, inode, t_bhs[i],
  2778. OCFS2_JOURNAL_ACCESS_CREATE);
  2779. if (ret) {
  2780. mlog_errno(ret);
  2781. goto out;
  2782. }
  2783. }
  2784. /* copy the whole bucket to the new first. */
  2785. for (i = 0; i < blk_per_bucket; i++)
  2786. memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
  2787. /* update the new bucket. */
  2788. xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
  2789. count = le16_to_cpu(xh->xh_count);
  2790. start = count / 2;
  2791. /*
  2792. * Calculate the total name/value len and xh_free_start for
  2793. * the old bucket first.
  2794. */
  2795. name_offset = OCFS2_XATTR_BUCKET_SIZE;
  2796. name_value_len = 0;
  2797. for (i = 0; i < start; i++) {
  2798. xe = &xh->xh_entries[i];
  2799. xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  2800. if (ocfs2_xattr_is_local(xe))
  2801. xe_len +=
  2802. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  2803. else
  2804. xe_len += OCFS2_XATTR_ROOT_SIZE;
  2805. name_value_len += xe_len;
  2806. if (le16_to_cpu(xe->xe_name_offset) < name_offset)
  2807. name_offset = le16_to_cpu(xe->xe_name_offset);
  2808. }
  2809. /*
  2810. * Now begin the modification to the new bucket.
  2811. *
  2812. * In the new bucket, We just move the xattr entry to the beginning
  2813. * and don't touch the name/value. So there will be some holes in the
  2814. * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
  2815. * called.
  2816. */
  2817. xe = &xh->xh_entries[start];
  2818. len = sizeof(struct ocfs2_xattr_entry) * (count - start);
  2819. mlog(0, "mv xattr entry len %d from %d to %d\n", len,
  2820. (int)((char *)xe - (char *)xh),
  2821. (int)((char *)xh->xh_entries - (char *)xh));
  2822. memmove((char *)xh->xh_entries, (char *)xe, len);
  2823. xe = &xh->xh_entries[count - start];
  2824. len = sizeof(struct ocfs2_xattr_entry) * start;
  2825. memset((char *)xe, 0, len);
  2826. le16_add_cpu(&xh->xh_count, -start);
  2827. le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
  2828. /* Calculate xh_free_start for the new bucket. */
  2829. xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
  2830. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  2831. xe = &xh->xh_entries[i];
  2832. xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
  2833. if (ocfs2_xattr_is_local(xe))
  2834. xe_len +=
  2835. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  2836. else
  2837. xe_len += OCFS2_XATTR_ROOT_SIZE;
  2838. if (le16_to_cpu(xe->xe_name_offset) <
  2839. le16_to_cpu(xh->xh_free_start))
  2840. xh->xh_free_start = xe->xe_name_offset;
  2841. }
  2842. /* set xh->xh_num_buckets for the new xh. */
  2843. if (new_bucket_head)
  2844. xh->xh_num_buckets = cpu_to_le16(1);
  2845. else
  2846. xh->xh_num_buckets = 0;
  2847. for (i = 0; i < blk_per_bucket; i++) {
  2848. ocfs2_journal_dirty(handle, t_bhs[i]);
  2849. if (ret)
  2850. mlog_errno(ret);
  2851. }
  2852. /* store the first_hash of the new bucket. */
  2853. if (first_hash)
  2854. *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
  2855. /*
  2856. * Now only update the 1st block of the old bucket.
  2857. * Please note that the entry has been sorted already above.
  2858. */
  2859. xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
  2860. memset(&xh->xh_entries[start], 0,
  2861. sizeof(struct ocfs2_xattr_entry) * (count - start));
  2862. xh->xh_count = cpu_to_le16(start);
  2863. xh->xh_free_start = cpu_to_le16(name_offset);
  2864. xh->xh_name_value_len = cpu_to_le16(name_value_len);
  2865. ocfs2_journal_dirty(handle, s_bhs[0]);
  2866. if (ret)
  2867. mlog_errno(ret);
  2868. out:
  2869. if (s_bhs) {
  2870. for (i = 0; i < blk_per_bucket; i++)
  2871. brelse(s_bhs[i]);
  2872. }
  2873. kfree(s_bhs);
  2874. if (t_bhs) {
  2875. for (i = 0; i < blk_per_bucket; i++)
  2876. brelse(t_bhs[i]);
  2877. }
  2878. kfree(t_bhs);
  2879. return ret;
  2880. }
  2881. /*
  2882. * Copy xattr from one bucket to another bucket.
  2883. *
  2884. * The caller must make sure that the journal transaction
  2885. * has enough space for journaling.
  2886. */
  2887. static int ocfs2_cp_xattr_bucket(struct inode *inode,
  2888. handle_t *handle,
  2889. u64 s_blkno,
  2890. u64 t_blkno,
  2891. int t_is_new)
  2892. {
  2893. int ret, i;
  2894. int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2895. int blocksize = inode->i_sb->s_blocksize;
  2896. struct buffer_head **s_bhs, **t_bhs = NULL;
  2897. BUG_ON(s_blkno == t_blkno);
  2898. mlog(0, "cp bucket %llu to %llu, target is %d\n",
  2899. s_blkno, t_blkno, t_is_new);
  2900. s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
  2901. GFP_NOFS);
  2902. if (!s_bhs)
  2903. return -ENOMEM;
  2904. ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
  2905. if (ret)
  2906. goto out;
  2907. t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
  2908. GFP_NOFS);
  2909. if (!t_bhs) {
  2910. ret = -ENOMEM;
  2911. goto out;
  2912. }
  2913. ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
  2914. if (ret)
  2915. goto out;
  2916. for (i = 0; i < blk_per_bucket; i++) {
  2917. ret = ocfs2_journal_access(handle, inode, t_bhs[i],
  2918. OCFS2_JOURNAL_ACCESS_WRITE);
  2919. if (ret)
  2920. goto out;
  2921. }
  2922. for (i = 0; i < blk_per_bucket; i++) {
  2923. memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
  2924. ocfs2_journal_dirty(handle, t_bhs[i]);
  2925. }
  2926. out:
  2927. if (s_bhs) {
  2928. for (i = 0; i < blk_per_bucket; i++)
  2929. brelse(s_bhs[i]);
  2930. }
  2931. kfree(s_bhs);
  2932. if (t_bhs) {
  2933. for (i = 0; i < blk_per_bucket; i++)
  2934. brelse(t_bhs[i]);
  2935. }
  2936. kfree(t_bhs);
  2937. return ret;
  2938. }
  2939. /*
  2940. * Copy one xattr cluster from src_blk to to_blk.
  2941. * The to_blk will become the first bucket header of the cluster, so its
  2942. * xh_num_buckets will be initialized as the bucket num in the cluster.
  2943. */
  2944. static int ocfs2_cp_xattr_cluster(struct inode *inode,
  2945. handle_t *handle,
  2946. struct buffer_head *first_bh,
  2947. u64 src_blk,
  2948. u64 to_blk,
  2949. u32 *first_hash)
  2950. {
  2951. int i, ret, credits;
  2952. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  2953. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  2954. int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
  2955. struct buffer_head *bh = NULL;
  2956. struct ocfs2_xattr_header *xh;
  2957. u64 to_blk_start = to_blk;
  2958. mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
  2959. /*
  2960. * We need to update the new cluster and 1 more for the update of
  2961. * the 1st bucket of the previous extent rec.
  2962. */
  2963. credits = bpc + 1;
  2964. ret = ocfs2_extend_trans(handle, credits);
  2965. if (ret) {
  2966. mlog_errno(ret);
  2967. goto out;
  2968. }
  2969. ret = ocfs2_journal_access(handle, inode, first_bh,
  2970. OCFS2_JOURNAL_ACCESS_WRITE);
  2971. if (ret) {
  2972. mlog_errno(ret);
  2973. goto out;
  2974. }
  2975. for (i = 0; i < num_buckets; i++) {
  2976. ret = ocfs2_cp_xattr_bucket(inode, handle,
  2977. src_blk, to_blk, 1);
  2978. if (ret) {
  2979. mlog_errno(ret);
  2980. goto out;
  2981. }
  2982. src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2983. to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  2984. }
  2985. /* update the old bucket header. */
  2986. xh = (struct ocfs2_xattr_header *)first_bh->b_data;
  2987. le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
  2988. ocfs2_journal_dirty(handle, first_bh);
  2989. /* update the new bucket header. */
  2990. ret = ocfs2_read_block(inode, to_blk_start, &bh);
  2991. if (ret < 0) {
  2992. mlog_errno(ret);
  2993. goto out;
  2994. }
  2995. ret = ocfs2_journal_access(handle, inode, bh,
  2996. OCFS2_JOURNAL_ACCESS_WRITE);
  2997. if (ret) {
  2998. mlog_errno(ret);
  2999. goto out;
  3000. }
  3001. xh = (struct ocfs2_xattr_header *)bh->b_data;
  3002. xh->xh_num_buckets = cpu_to_le16(num_buckets);
  3003. ocfs2_journal_dirty(handle, bh);
  3004. if (first_hash)
  3005. *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
  3006. out:
  3007. brelse(bh);
  3008. return ret;
  3009. }
  3010. /*
  3011. * Move half of the xattrs in this cluster to the new cluster.
  3012. * This function should only be called when bucket size == cluster size.
  3013. * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
  3014. */
  3015. static int ocfs2_half_xattr_cluster(struct inode *inode,
  3016. handle_t *handle,
  3017. u64 prev_blk,
  3018. u64 new_blk,
  3019. u32 *first_hash)
  3020. {
  3021. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3022. int ret, credits = 2 * blk_per_bucket;
  3023. BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
  3024. ret = ocfs2_extend_trans(handle, credits);
  3025. if (ret) {
  3026. mlog_errno(ret);
  3027. return ret;
  3028. }
  3029. /* Move half of the xattr in start_blk to the next bucket. */
  3030. return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
  3031. new_blk, first_hash, 1);
  3032. }
  3033. /*
  3034. * Move some xattrs from the old cluster to the new one since they are not
  3035. * contiguous in ocfs2 xattr tree.
  3036. *
  3037. * new_blk starts a new separate cluster, and we will move some xattrs from
  3038. * prev_blk to it. v_start will be set as the first name hash value in this
  3039. * new cluster so that it can be used as e_cpos during tree insertion and
  3040. * don't collide with our original b-tree operations. first_bh and header_bh
  3041. * will also be updated since they will be used in ocfs2_extend_xattr_bucket
  3042. * to extend the insert bucket.
  3043. *
  3044. * The problem is how much xattr should we move to the new one and when should
  3045. * we update first_bh and header_bh?
  3046. * 1. If cluster size > bucket size, that means the previous cluster has more
  3047. * than 1 bucket, so just move half nums of bucket into the new cluster and
  3048. * update the first_bh and header_bh if the insert bucket has been moved
  3049. * to the new cluster.
  3050. * 2. If cluster_size == bucket_size:
  3051. * a) If the previous extent rec has more than one cluster and the insert
  3052. * place isn't in the last cluster, copy the entire last cluster to the
  3053. * new one. This time, we don't need to upate the first_bh and header_bh
  3054. * since they will not be moved into the new cluster.
  3055. * b) Otherwise, move the bottom half of the xattrs in the last cluster into
  3056. * the new one. And we set the extend flag to zero if the insert place is
  3057. * moved into the new allocated cluster since no extend is needed.
  3058. */
  3059. static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
  3060. handle_t *handle,
  3061. struct buffer_head **first_bh,
  3062. struct buffer_head **header_bh,
  3063. u64 new_blk,
  3064. u64 prev_blk,
  3065. u32 prev_clusters,
  3066. u32 *v_start,
  3067. int *extend)
  3068. {
  3069. int ret = 0;
  3070. int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  3071. mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
  3072. prev_blk, prev_clusters, new_blk);
  3073. if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
  3074. ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
  3075. handle,
  3076. first_bh,
  3077. header_bh,
  3078. new_blk,
  3079. prev_blk,
  3080. prev_clusters,
  3081. v_start);
  3082. else {
  3083. u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
  3084. if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
  3085. ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
  3086. last_blk, new_blk,
  3087. v_start);
  3088. else {
  3089. ret = ocfs2_half_xattr_cluster(inode, handle,
  3090. last_blk, new_blk,
  3091. v_start);
  3092. if ((*header_bh)->b_blocknr == last_blk && extend)
  3093. *extend = 0;
  3094. }
  3095. }
  3096. return ret;
  3097. }
  3098. /*
  3099. * Add a new cluster for xattr storage.
  3100. *
  3101. * If the new cluster is contiguous with the previous one, it will be
  3102. * appended to the same extent record, and num_clusters will be updated.
  3103. * If not, we will insert a new extent for it and move some xattrs in
  3104. * the last cluster into the new allocated one.
  3105. * We also need to limit the maximum size of a btree leaf, otherwise we'll
  3106. * lose the benefits of hashing because we'll have to search large leaves.
  3107. * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
  3108. * if it's bigger).
  3109. *
  3110. * first_bh is the first block of the previous extent rec and header_bh
  3111. * indicates the bucket we will insert the new xattrs. They will be updated
  3112. * when the header_bh is moved into the new cluster.
  3113. */
  3114. static int ocfs2_add_new_xattr_cluster(struct inode *inode,
  3115. struct buffer_head *root_bh,
  3116. struct buffer_head **first_bh,
  3117. struct buffer_head **header_bh,
  3118. u32 *num_clusters,
  3119. u32 prev_cpos,
  3120. u64 prev_blkno,
  3121. int *extend)
  3122. {
  3123. int ret, credits;
  3124. u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
  3125. u32 prev_clusters = *num_clusters;
  3126. u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
  3127. u64 block;
  3128. handle_t *handle = NULL;
  3129. struct ocfs2_alloc_context *data_ac = NULL;
  3130. struct ocfs2_alloc_context *meta_ac = NULL;
  3131. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3132. struct ocfs2_extent_tree et;
  3133. mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
  3134. "previous xattr blkno = %llu\n",
  3135. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  3136. prev_cpos, prev_blkno);
  3137. ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
  3138. ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
  3139. &data_ac, &meta_ac);
  3140. if (ret) {
  3141. mlog_errno(ret);
  3142. goto leave;
  3143. }
  3144. credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
  3145. clusters_to_add);
  3146. handle = ocfs2_start_trans(osb, credits);
  3147. if (IS_ERR(handle)) {
  3148. ret = PTR_ERR(handle);
  3149. handle = NULL;
  3150. mlog_errno(ret);
  3151. goto leave;
  3152. }
  3153. ret = ocfs2_journal_access(handle, inode, root_bh,
  3154. OCFS2_JOURNAL_ACCESS_WRITE);
  3155. if (ret < 0) {
  3156. mlog_errno(ret);
  3157. goto leave;
  3158. }
  3159. ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
  3160. clusters_to_add, &bit_off, &num_bits);
  3161. if (ret < 0) {
  3162. if (ret != -ENOSPC)
  3163. mlog_errno(ret);
  3164. goto leave;
  3165. }
  3166. BUG_ON(num_bits > clusters_to_add);
  3167. block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  3168. mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
  3169. num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
  3170. if (prev_blkno + prev_clusters * bpc == block &&
  3171. (prev_clusters + num_bits) << osb->s_clustersize_bits <=
  3172. OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
  3173. /*
  3174. * If this cluster is contiguous with the old one and
  3175. * adding this new cluster, we don't surpass the limit of
  3176. * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
  3177. * initialized and used like other buckets in the previous
  3178. * cluster.
  3179. * So add it as a contiguous one. The caller will handle
  3180. * its init process.
  3181. */
  3182. v_start = prev_cpos + prev_clusters;
  3183. *num_clusters = prev_clusters + num_bits;
  3184. mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
  3185. num_bits);
  3186. } else {
  3187. ret = ocfs2_adjust_xattr_cross_cluster(inode,
  3188. handle,
  3189. first_bh,
  3190. header_bh,
  3191. block,
  3192. prev_blkno,
  3193. prev_clusters,
  3194. &v_start,
  3195. extend);
  3196. if (ret) {
  3197. mlog_errno(ret);
  3198. goto leave;
  3199. }
  3200. }
  3201. if (handle->h_buffer_credits < credits) {
  3202. /*
  3203. * The journal has been restarted before, and don't
  3204. * have enough space for the insertion, so extend it
  3205. * here.
  3206. */
  3207. ret = ocfs2_extend_trans(handle, credits);
  3208. if (ret) {
  3209. mlog_errno(ret);
  3210. goto leave;
  3211. }
  3212. }
  3213. mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
  3214. num_bits, block, v_start);
  3215. ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
  3216. num_bits, 0, meta_ac);
  3217. if (ret < 0) {
  3218. mlog_errno(ret);
  3219. goto leave;
  3220. }
  3221. ret = ocfs2_journal_dirty(handle, root_bh);
  3222. if (ret < 0) {
  3223. mlog_errno(ret);
  3224. goto leave;
  3225. }
  3226. leave:
  3227. if (handle)
  3228. ocfs2_commit_trans(osb, handle);
  3229. if (data_ac)
  3230. ocfs2_free_alloc_context(data_ac);
  3231. if (meta_ac)
  3232. ocfs2_free_alloc_context(meta_ac);
  3233. return ret;
  3234. }
  3235. /*
  3236. * Extend a new xattr bucket and move xattrs to the end one by one until
  3237. * We meet with start_bh. Only move half of the xattrs to the bucket after it.
  3238. */
  3239. static int ocfs2_extend_xattr_bucket(struct inode *inode,
  3240. struct buffer_head *first_bh,
  3241. struct buffer_head *start_bh,
  3242. u32 num_clusters)
  3243. {
  3244. int ret, credits;
  3245. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3246. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3247. u64 start_blk = start_bh->b_blocknr, end_blk;
  3248. u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
  3249. handle_t *handle;
  3250. struct ocfs2_xattr_header *first_xh =
  3251. (struct ocfs2_xattr_header *)first_bh->b_data;
  3252. u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
  3253. mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
  3254. "from %llu, len = %u\n", start_blk,
  3255. (unsigned long long)first_bh->b_blocknr, num_clusters);
  3256. BUG_ON(bucket >= num_buckets);
  3257. end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
  3258. /*
  3259. * We will touch all the buckets after the start_bh(include it).
  3260. * Add one more bucket and modify the first_bh.
  3261. */
  3262. credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
  3263. handle = ocfs2_start_trans(osb, credits);
  3264. if (IS_ERR(handle)) {
  3265. ret = PTR_ERR(handle);
  3266. handle = NULL;
  3267. mlog_errno(ret);
  3268. goto out;
  3269. }
  3270. ret = ocfs2_journal_access(handle, inode, first_bh,
  3271. OCFS2_JOURNAL_ACCESS_WRITE);
  3272. if (ret) {
  3273. mlog_errno(ret);
  3274. goto commit;
  3275. }
  3276. while (end_blk != start_blk) {
  3277. ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
  3278. end_blk + blk_per_bucket, 0);
  3279. if (ret)
  3280. goto commit;
  3281. end_blk -= blk_per_bucket;
  3282. }
  3283. /* Move half of the xattr in start_blk to the next bucket. */
  3284. ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
  3285. start_blk + blk_per_bucket, NULL, 0);
  3286. le16_add_cpu(&first_xh->xh_num_buckets, 1);
  3287. ocfs2_journal_dirty(handle, first_bh);
  3288. commit:
  3289. ocfs2_commit_trans(osb, handle);
  3290. out:
  3291. return ret;
  3292. }
  3293. /*
  3294. * Add new xattr bucket in an extent record and adjust the buckets accordingly.
  3295. * xb_bh is the ocfs2_xattr_block.
  3296. * We will move all the buckets starting from header_bh to the next place. As
  3297. * for this one, half num of its xattrs will be moved to the next one.
  3298. *
  3299. * We will allocate a new cluster if current cluster is full and adjust
  3300. * header_bh and first_bh if the insert place is moved to the new cluster.
  3301. */
  3302. static int ocfs2_add_new_xattr_bucket(struct inode *inode,
  3303. struct buffer_head *xb_bh,
  3304. struct buffer_head *header_bh)
  3305. {
  3306. struct ocfs2_xattr_header *first_xh = NULL;
  3307. struct buffer_head *first_bh = NULL;
  3308. struct ocfs2_xattr_block *xb =
  3309. (struct ocfs2_xattr_block *)xb_bh->b_data;
  3310. struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
  3311. struct ocfs2_extent_list *el = &xb_root->xt_list;
  3312. struct ocfs2_xattr_header *xh =
  3313. (struct ocfs2_xattr_header *)header_bh->b_data;
  3314. u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
  3315. struct super_block *sb = inode->i_sb;
  3316. struct ocfs2_super *osb = OCFS2_SB(sb);
  3317. int ret, num_buckets, extend = 1;
  3318. u64 p_blkno;
  3319. u32 e_cpos, num_clusters;
  3320. mlog(0, "Add new xattr bucket starting form %llu\n",
  3321. (unsigned long long)header_bh->b_blocknr);
  3322. /*
  3323. * Add refrence for header_bh here because it may be
  3324. * changed in ocfs2_add_new_xattr_cluster and we need
  3325. * to free it in the end.
  3326. */
  3327. get_bh(header_bh);
  3328. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
  3329. &num_clusters, el);
  3330. if (ret) {
  3331. mlog_errno(ret);
  3332. goto out;
  3333. }
  3334. ret = ocfs2_read_block(inode, p_blkno, &first_bh);
  3335. if (ret) {
  3336. mlog_errno(ret);
  3337. goto out;
  3338. }
  3339. num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
  3340. first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
  3341. if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
  3342. ret = ocfs2_add_new_xattr_cluster(inode,
  3343. xb_bh,
  3344. &first_bh,
  3345. &header_bh,
  3346. &num_clusters,
  3347. e_cpos,
  3348. p_blkno,
  3349. &extend);
  3350. if (ret) {
  3351. mlog_errno(ret);
  3352. goto out;
  3353. }
  3354. }
  3355. if (extend)
  3356. ret = ocfs2_extend_xattr_bucket(inode,
  3357. first_bh,
  3358. header_bh,
  3359. num_clusters);
  3360. if (ret)
  3361. mlog_errno(ret);
  3362. out:
  3363. brelse(first_bh);
  3364. brelse(header_bh);
  3365. return ret;
  3366. }
  3367. static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
  3368. struct ocfs2_xattr_bucket *bucket,
  3369. int offs)
  3370. {
  3371. int block_off = offs >> inode->i_sb->s_blocksize_bits;
  3372. offs = offs % inode->i_sb->s_blocksize;
  3373. return bucket->bhs[block_off]->b_data + offs;
  3374. }
  3375. /*
  3376. * Handle the normal xattr set, including replace, delete and new.
  3377. *
  3378. * Note: "local" indicates the real data's locality. So we can't
  3379. * just its bucket locality by its length.
  3380. */
  3381. static void ocfs2_xattr_set_entry_normal(struct inode *inode,
  3382. struct ocfs2_xattr_info *xi,
  3383. struct ocfs2_xattr_search *xs,
  3384. u32 name_hash,
  3385. int local)
  3386. {
  3387. struct ocfs2_xattr_entry *last, *xe;
  3388. int name_len = strlen(xi->name);
  3389. struct ocfs2_xattr_header *xh = xs->header;
  3390. u16 count = le16_to_cpu(xh->xh_count), start;
  3391. size_t blocksize = inode->i_sb->s_blocksize;
  3392. char *val;
  3393. size_t offs, size, new_size;
  3394. last = &xh->xh_entries[count];
  3395. if (!xs->not_found) {
  3396. xe = xs->here;
  3397. offs = le16_to_cpu(xe->xe_name_offset);
  3398. if (ocfs2_xattr_is_local(xe))
  3399. size = OCFS2_XATTR_SIZE(name_len) +
  3400. OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  3401. else
  3402. size = OCFS2_XATTR_SIZE(name_len) +
  3403. OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
  3404. /*
  3405. * If the new value will be stored outside, xi->value has been
  3406. * initalized as an empty ocfs2_xattr_value_root, and the same
  3407. * goes with xi->value_len, so we can set new_size safely here.
  3408. * See ocfs2_xattr_set_in_bucket.
  3409. */
  3410. new_size = OCFS2_XATTR_SIZE(name_len) +
  3411. OCFS2_XATTR_SIZE(xi->value_len);
  3412. le16_add_cpu(&xh->xh_name_value_len, -size);
  3413. if (xi->value) {
  3414. if (new_size > size)
  3415. goto set_new_name_value;
  3416. /* Now replace the old value with new one. */
  3417. if (local)
  3418. xe->xe_value_size = cpu_to_le64(xi->value_len);
  3419. else
  3420. xe->xe_value_size = 0;
  3421. val = ocfs2_xattr_bucket_get_val(inode,
  3422. &xs->bucket, offs);
  3423. memset(val + OCFS2_XATTR_SIZE(name_len), 0,
  3424. size - OCFS2_XATTR_SIZE(name_len));
  3425. if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
  3426. memcpy(val + OCFS2_XATTR_SIZE(name_len),
  3427. xi->value, xi->value_len);
  3428. le16_add_cpu(&xh->xh_name_value_len, new_size);
  3429. ocfs2_xattr_set_local(xe, local);
  3430. return;
  3431. } else {
  3432. /*
  3433. * Remove the old entry if there is more than one.
  3434. * We don't remove the last entry so that we can
  3435. * use it to indicate the hash value of the empty
  3436. * bucket.
  3437. */
  3438. last -= 1;
  3439. le16_add_cpu(&xh->xh_count, -1);
  3440. if (xh->xh_count) {
  3441. memmove(xe, xe + 1,
  3442. (void *)last - (void *)xe);
  3443. memset(last, 0,
  3444. sizeof(struct ocfs2_xattr_entry));
  3445. } else
  3446. xh->xh_free_start =
  3447. cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
  3448. return;
  3449. }
  3450. } else {
  3451. /* find a new entry for insert. */
  3452. int low = 0, high = count - 1, tmp;
  3453. struct ocfs2_xattr_entry *tmp_xe;
  3454. while (low <= high && count) {
  3455. tmp = (low + high) / 2;
  3456. tmp_xe = &xh->xh_entries[tmp];
  3457. if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
  3458. low = tmp + 1;
  3459. else if (name_hash <
  3460. le32_to_cpu(tmp_xe->xe_name_hash))
  3461. high = tmp - 1;
  3462. else {
  3463. low = tmp;
  3464. break;
  3465. }
  3466. }
  3467. xe = &xh->xh_entries[low];
  3468. if (low != count)
  3469. memmove(xe + 1, xe, (void *)last - (void *)xe);
  3470. le16_add_cpu(&xh->xh_count, 1);
  3471. memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
  3472. xe->xe_name_hash = cpu_to_le32(name_hash);
  3473. xe->xe_name_len = name_len;
  3474. ocfs2_xattr_set_type(xe, xi->name_index);
  3475. }
  3476. set_new_name_value:
  3477. /* Insert the new name+value. */
  3478. size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
  3479. /*
  3480. * We must make sure that the name/value pair
  3481. * exists in the same block.
  3482. */
  3483. offs = le16_to_cpu(xh->xh_free_start);
  3484. start = offs - size;
  3485. if (start >> inode->i_sb->s_blocksize_bits !=
  3486. (offs - 1) >> inode->i_sb->s_blocksize_bits) {
  3487. offs = offs - offs % blocksize;
  3488. xh->xh_free_start = cpu_to_le16(offs);
  3489. }
  3490. val = ocfs2_xattr_bucket_get_val(inode,
  3491. &xs->bucket, offs - size);
  3492. xe->xe_name_offset = cpu_to_le16(offs - size);
  3493. memset(val, 0, size);
  3494. memcpy(val, xi->name, name_len);
  3495. memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
  3496. xe->xe_value_size = cpu_to_le64(xi->value_len);
  3497. ocfs2_xattr_set_local(xe, local);
  3498. xs->here = xe;
  3499. le16_add_cpu(&xh->xh_free_start, -size);
  3500. le16_add_cpu(&xh->xh_name_value_len, size);
  3501. return;
  3502. }
  3503. static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
  3504. handle_t *handle,
  3505. struct ocfs2_xattr_search *xs,
  3506. struct buffer_head **bhs,
  3507. u16 bh_num)
  3508. {
  3509. int ret = 0, off, block_off;
  3510. struct ocfs2_xattr_entry *xe = xs->here;
  3511. /*
  3512. * First calculate all the blocks we should journal_access
  3513. * and journal_dirty. The first block should always be touched.
  3514. */
  3515. ret = ocfs2_journal_dirty(handle, bhs[0]);
  3516. if (ret)
  3517. mlog_errno(ret);
  3518. /* calc the data. */
  3519. off = le16_to_cpu(xe->xe_name_offset);
  3520. block_off = off >> inode->i_sb->s_blocksize_bits;
  3521. ret = ocfs2_journal_dirty(handle, bhs[block_off]);
  3522. if (ret)
  3523. mlog_errno(ret);
  3524. return ret;
  3525. }
  3526. /*
  3527. * Set the xattr entry in the specified bucket.
  3528. * The bucket is indicated by xs->bucket and it should have the enough
  3529. * space for the xattr insertion.
  3530. */
  3531. static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
  3532. struct ocfs2_xattr_info *xi,
  3533. struct ocfs2_xattr_search *xs,
  3534. u32 name_hash,
  3535. int local)
  3536. {
  3537. int i, ret;
  3538. handle_t *handle = NULL;
  3539. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3540. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3541. mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
  3542. (unsigned long)xi->value_len, xi->name_index,
  3543. (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
  3544. if (!xs->bucket.bhs[1]) {
  3545. ret = ocfs2_read_blocks(inode,
  3546. xs->bucket.bhs[0]->b_blocknr + 1,
  3547. blk_per_bucket - 1, &xs->bucket.bhs[1],
  3548. OCFS2_BH_CACHED);
  3549. if (ret) {
  3550. mlog_errno(ret);
  3551. goto out;
  3552. }
  3553. }
  3554. handle = ocfs2_start_trans(osb, blk_per_bucket);
  3555. if (IS_ERR(handle)) {
  3556. ret = PTR_ERR(handle);
  3557. handle = NULL;
  3558. mlog_errno(ret);
  3559. goto out;
  3560. }
  3561. for (i = 0; i < blk_per_bucket; i++) {
  3562. ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
  3563. OCFS2_JOURNAL_ACCESS_WRITE);
  3564. if (ret < 0) {
  3565. mlog_errno(ret);
  3566. goto out;
  3567. }
  3568. }
  3569. ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
  3570. /*Only dirty the blocks we have touched in set xattr. */
  3571. ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
  3572. xs->bucket.bhs, blk_per_bucket);
  3573. if (ret)
  3574. mlog_errno(ret);
  3575. out:
  3576. ocfs2_commit_trans(osb, handle);
  3577. return ret;
  3578. }
  3579. static int ocfs2_xattr_value_update_size(struct inode *inode,
  3580. struct buffer_head *xe_bh,
  3581. struct ocfs2_xattr_entry *xe,
  3582. u64 new_size)
  3583. {
  3584. int ret;
  3585. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3586. handle_t *handle = NULL;
  3587. handle = ocfs2_start_trans(osb, 1);
  3588. if (handle == NULL) {
  3589. ret = -ENOMEM;
  3590. mlog_errno(ret);
  3591. goto out;
  3592. }
  3593. ret = ocfs2_journal_access(handle, inode, xe_bh,
  3594. OCFS2_JOURNAL_ACCESS_WRITE);
  3595. if (ret < 0) {
  3596. mlog_errno(ret);
  3597. goto out_commit;
  3598. }
  3599. xe->xe_value_size = cpu_to_le64(new_size);
  3600. ret = ocfs2_journal_dirty(handle, xe_bh);
  3601. if (ret < 0)
  3602. mlog_errno(ret);
  3603. out_commit:
  3604. ocfs2_commit_trans(osb, handle);
  3605. out:
  3606. return ret;
  3607. }
  3608. /*
  3609. * Truncate the specified xe_off entry in xattr bucket.
  3610. * bucket is indicated by header_bh and len is the new length.
  3611. * Both the ocfs2_xattr_value_root and the entry will be updated here.
  3612. *
  3613. * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
  3614. */
  3615. static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
  3616. struct buffer_head *header_bh,
  3617. int xe_off,
  3618. int len)
  3619. {
  3620. int ret, offset;
  3621. u64 value_blk;
  3622. struct buffer_head *value_bh = NULL;
  3623. struct ocfs2_xattr_value_root *xv;
  3624. struct ocfs2_xattr_entry *xe;
  3625. struct ocfs2_xattr_header *xh =
  3626. (struct ocfs2_xattr_header *)header_bh->b_data;
  3627. size_t blocksize = inode->i_sb->s_blocksize;
  3628. xe = &xh->xh_entries[xe_off];
  3629. BUG_ON(!xe || ocfs2_xattr_is_local(xe));
  3630. offset = le16_to_cpu(xe->xe_name_offset) +
  3631. OCFS2_XATTR_SIZE(xe->xe_name_len);
  3632. value_blk = offset / blocksize;
  3633. /* We don't allow ocfs2_xattr_value to be stored in different block. */
  3634. BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
  3635. value_blk += header_bh->b_blocknr;
  3636. ret = ocfs2_read_block(inode, value_blk, &value_bh);
  3637. if (ret) {
  3638. mlog_errno(ret);
  3639. goto out;
  3640. }
  3641. xv = (struct ocfs2_xattr_value_root *)
  3642. (value_bh->b_data + offset % blocksize);
  3643. mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
  3644. xe_off, (unsigned long long)header_bh->b_blocknr, len);
  3645. ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
  3646. if (ret) {
  3647. mlog_errno(ret);
  3648. goto out;
  3649. }
  3650. ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
  3651. if (ret) {
  3652. mlog_errno(ret);
  3653. goto out;
  3654. }
  3655. out:
  3656. brelse(value_bh);
  3657. return ret;
  3658. }
  3659. static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
  3660. struct ocfs2_xattr_search *xs,
  3661. int len)
  3662. {
  3663. int ret, offset;
  3664. struct ocfs2_xattr_entry *xe = xs->here;
  3665. struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
  3666. BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
  3667. offset = xe - xh->xh_entries;
  3668. ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
  3669. offset, len);
  3670. if (ret)
  3671. mlog_errno(ret);
  3672. return ret;
  3673. }
  3674. static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
  3675. struct ocfs2_xattr_search *xs,
  3676. char *val,
  3677. int value_len)
  3678. {
  3679. int offset;
  3680. struct ocfs2_xattr_value_root *xv;
  3681. struct ocfs2_xattr_entry *xe = xs->here;
  3682. BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
  3683. offset = le16_to_cpu(xe->xe_name_offset) +
  3684. OCFS2_XATTR_SIZE(xe->xe_name_len);
  3685. xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
  3686. return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
  3687. }
  3688. static int ocfs2_rm_xattr_cluster(struct inode *inode,
  3689. struct buffer_head *root_bh,
  3690. u64 blkno,
  3691. u32 cpos,
  3692. u32 len)
  3693. {
  3694. int ret;
  3695. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  3696. struct inode *tl_inode = osb->osb_tl_inode;
  3697. handle_t *handle;
  3698. struct ocfs2_xattr_block *xb =
  3699. (struct ocfs2_xattr_block *)root_bh->b_data;
  3700. struct ocfs2_alloc_context *meta_ac = NULL;
  3701. struct ocfs2_cached_dealloc_ctxt dealloc;
  3702. struct ocfs2_extent_tree et;
  3703. ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
  3704. ocfs2_init_dealloc_ctxt(&dealloc);
  3705. mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
  3706. cpos, len, (unsigned long long)blkno);
  3707. ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
  3708. ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
  3709. if (ret) {
  3710. mlog_errno(ret);
  3711. return ret;
  3712. }
  3713. mutex_lock(&tl_inode->i_mutex);
  3714. if (ocfs2_truncate_log_needs_flush(osb)) {
  3715. ret = __ocfs2_flush_truncate_log(osb);
  3716. if (ret < 0) {
  3717. mlog_errno(ret);
  3718. goto out;
  3719. }
  3720. }
  3721. handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
  3722. if (handle == NULL) {
  3723. ret = -ENOMEM;
  3724. mlog_errno(ret);
  3725. goto out;
  3726. }
  3727. ret = ocfs2_journal_access(handle, inode, root_bh,
  3728. OCFS2_JOURNAL_ACCESS_WRITE);
  3729. if (ret) {
  3730. mlog_errno(ret);
  3731. goto out_commit;
  3732. }
  3733. ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
  3734. &dealloc);
  3735. if (ret) {
  3736. mlog_errno(ret);
  3737. goto out_commit;
  3738. }
  3739. le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
  3740. ret = ocfs2_journal_dirty(handle, root_bh);
  3741. if (ret) {
  3742. mlog_errno(ret);
  3743. goto out_commit;
  3744. }
  3745. ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
  3746. if (ret)
  3747. mlog_errno(ret);
  3748. out_commit:
  3749. ocfs2_commit_trans(osb, handle);
  3750. out:
  3751. ocfs2_schedule_truncate_log_flush(osb, 1);
  3752. mutex_unlock(&tl_inode->i_mutex);
  3753. if (meta_ac)
  3754. ocfs2_free_alloc_context(meta_ac);
  3755. ocfs2_run_deallocs(osb, &dealloc);
  3756. return ret;
  3757. }
  3758. static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
  3759. struct ocfs2_xattr_search *xs)
  3760. {
  3761. handle_t *handle = NULL;
  3762. struct ocfs2_xattr_header *xh = xs->bucket.xh;
  3763. struct ocfs2_xattr_entry *last = &xh->xh_entries[
  3764. le16_to_cpu(xh->xh_count) - 1];
  3765. int ret = 0;
  3766. handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
  3767. if (IS_ERR(handle)) {
  3768. ret = PTR_ERR(handle);
  3769. mlog_errno(ret);
  3770. return;
  3771. }
  3772. ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
  3773. OCFS2_JOURNAL_ACCESS_WRITE);
  3774. if (ret) {
  3775. mlog_errno(ret);
  3776. goto out_commit;
  3777. }
  3778. /* Remove the old entry. */
  3779. memmove(xs->here, xs->here + 1,
  3780. (void *)last - (void *)xs->here);
  3781. memset(last, 0, sizeof(struct ocfs2_xattr_entry));
  3782. le16_add_cpu(&xh->xh_count, -1);
  3783. ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
  3784. if (ret < 0)
  3785. mlog_errno(ret);
  3786. out_commit:
  3787. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  3788. }
  3789. /*
  3790. * Set the xattr name/value in the bucket specified in xs.
  3791. *
  3792. * As the new value in xi may be stored in the bucket or in an outside cluster,
  3793. * we divide the whole process into 3 steps:
  3794. * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
  3795. * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
  3796. * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
  3797. * 4. If the clusters for the new outside value can't be allocated, we need
  3798. * to free the xattr we allocated in set.
  3799. */
  3800. static int ocfs2_xattr_set_in_bucket(struct inode *inode,
  3801. struct ocfs2_xattr_info *xi,
  3802. struct ocfs2_xattr_search *xs)
  3803. {
  3804. int ret, local = 1;
  3805. size_t value_len;
  3806. char *val = (char *)xi->value;
  3807. struct ocfs2_xattr_entry *xe = xs->here;
  3808. u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
  3809. strlen(xi->name));
  3810. if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
  3811. /*
  3812. * We need to truncate the xattr storage first.
  3813. *
  3814. * If both the old and new value are stored to
  3815. * outside block, we only need to truncate
  3816. * the storage and then set the value outside.
  3817. *
  3818. * If the new value should be stored within block,
  3819. * we should free all the outside block first and
  3820. * the modification to the xattr block will be done
  3821. * by following steps.
  3822. */
  3823. if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  3824. value_len = xi->value_len;
  3825. else
  3826. value_len = 0;
  3827. ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
  3828. value_len);
  3829. if (ret)
  3830. goto out;
  3831. if (value_len)
  3832. goto set_value_outside;
  3833. }
  3834. value_len = xi->value_len;
  3835. /* So we have to handle the inside block change now. */
  3836. if (value_len > OCFS2_XATTR_INLINE_SIZE) {
  3837. /*
  3838. * If the new value will be stored outside of block,
  3839. * initalize a new empty value root and insert it first.
  3840. */
  3841. local = 0;
  3842. xi->value = &def_xv;
  3843. xi->value_len = OCFS2_XATTR_ROOT_SIZE;
  3844. }
  3845. ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local);
  3846. if (ret) {
  3847. mlog_errno(ret);
  3848. goto out;
  3849. }
  3850. if (value_len <= OCFS2_XATTR_INLINE_SIZE)
  3851. goto out;
  3852. /* allocate the space now for the outside block storage. */
  3853. ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
  3854. value_len);
  3855. if (ret) {
  3856. mlog_errno(ret);
  3857. if (xs->not_found) {
  3858. /*
  3859. * We can't allocate enough clusters for outside
  3860. * storage and we have allocated xattr already,
  3861. * so need to remove it.
  3862. */
  3863. ocfs2_xattr_bucket_remove_xs(inode, xs);
  3864. }
  3865. goto out;
  3866. }
  3867. set_value_outside:
  3868. ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
  3869. out:
  3870. return ret;
  3871. }
  3872. /* check whether the xattr bucket is filled up with the same hash value. */
  3873. static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
  3874. struct ocfs2_xattr_bucket *bucket)
  3875. {
  3876. struct ocfs2_xattr_header *xh = bucket->xh;
  3877. if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
  3878. xh->xh_entries[0].xe_name_hash) {
  3879. mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
  3880. "hash = %u\n",
  3881. (unsigned long long)bucket->bhs[0]->b_blocknr,
  3882. le32_to_cpu(xh->xh_entries[0].xe_name_hash));
  3883. return -ENOSPC;
  3884. }
  3885. return 0;
  3886. }
  3887. static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
  3888. struct ocfs2_xattr_info *xi,
  3889. struct ocfs2_xattr_search *xs)
  3890. {
  3891. struct ocfs2_xattr_header *xh;
  3892. struct ocfs2_xattr_entry *xe;
  3893. u16 count, header_size, xh_free_start;
  3894. int i, free, max_free, need, old;
  3895. size_t value_size = 0, name_len = strlen(xi->name);
  3896. size_t blocksize = inode->i_sb->s_blocksize;
  3897. int ret, allocation = 0;
  3898. u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
  3899. mlog_entry("Set xattr %s in xattr index block\n", xi->name);
  3900. try_again:
  3901. xh = xs->header;
  3902. count = le16_to_cpu(xh->xh_count);
  3903. xh_free_start = le16_to_cpu(xh->xh_free_start);
  3904. header_size = sizeof(struct ocfs2_xattr_header) +
  3905. count * sizeof(struct ocfs2_xattr_entry);
  3906. max_free = OCFS2_XATTR_BUCKET_SIZE -
  3907. le16_to_cpu(xh->xh_name_value_len) - header_size;
  3908. mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
  3909. "of %u which exceed block size\n",
  3910. (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
  3911. header_size);
  3912. if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
  3913. value_size = OCFS2_XATTR_ROOT_SIZE;
  3914. else if (xi->value)
  3915. value_size = OCFS2_XATTR_SIZE(xi->value_len);
  3916. if (xs->not_found)
  3917. need = sizeof(struct ocfs2_xattr_entry) +
  3918. OCFS2_XATTR_SIZE(name_len) + value_size;
  3919. else {
  3920. need = value_size + OCFS2_XATTR_SIZE(name_len);
  3921. /*
  3922. * We only replace the old value if the new length is smaller
  3923. * than the old one. Otherwise we will allocate new space in the
  3924. * bucket to store it.
  3925. */
  3926. xe = xs->here;
  3927. if (ocfs2_xattr_is_local(xe))
  3928. old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
  3929. else
  3930. old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
  3931. if (old >= value_size)
  3932. need = 0;
  3933. }
  3934. free = xh_free_start - header_size;
  3935. /*
  3936. * We need to make sure the new name/value pair
  3937. * can exist in the same block.
  3938. */
  3939. if (xh_free_start % blocksize < need)
  3940. free -= xh_free_start % blocksize;
  3941. mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
  3942. "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
  3943. " %u\n", xs->not_found,
  3944. (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
  3945. free, need, max_free, le16_to_cpu(xh->xh_free_start),
  3946. le16_to_cpu(xh->xh_name_value_len));
  3947. if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
  3948. if (need <= max_free &&
  3949. count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
  3950. /*
  3951. * We can create the space by defragment. Since only the
  3952. * name/value will be moved, the xe shouldn't be changed
  3953. * in xs.
  3954. */
  3955. ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
  3956. if (ret) {
  3957. mlog_errno(ret);
  3958. goto out;
  3959. }
  3960. xh_free_start = le16_to_cpu(xh->xh_free_start);
  3961. free = xh_free_start - header_size;
  3962. if (xh_free_start % blocksize < need)
  3963. free -= xh_free_start % blocksize;
  3964. if (free >= need)
  3965. goto xattr_set;
  3966. mlog(0, "Can't get enough space for xattr insert by "
  3967. "defragment. Need %u bytes, but we have %d, so "
  3968. "allocate new bucket for it.\n", need, free);
  3969. }
  3970. /*
  3971. * We have to add new buckets or clusters and one
  3972. * allocation should leave us enough space for insert.
  3973. */
  3974. BUG_ON(allocation);
  3975. /*
  3976. * We do not allow for overlapping ranges between buckets. And
  3977. * the maximum number of collisions we will allow for then is
  3978. * one bucket's worth, so check it here whether we need to
  3979. * add a new bucket for the insert.
  3980. */
  3981. ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
  3982. if (ret) {
  3983. mlog_errno(ret);
  3984. goto out;
  3985. }
  3986. ret = ocfs2_add_new_xattr_bucket(inode,
  3987. xs->xattr_bh,
  3988. xs->bucket.bhs[0]);
  3989. if (ret) {
  3990. mlog_errno(ret);
  3991. goto out;
  3992. }
  3993. for (i = 0; i < blk_per_bucket; i++)
  3994. brelse(xs->bucket.bhs[i]);
  3995. memset(&xs->bucket, 0, sizeof(xs->bucket));
  3996. ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
  3997. xi->name_index,
  3998. xi->name, xs);
  3999. if (ret && ret != -ENODATA)
  4000. goto out;
  4001. xs->not_found = ret;
  4002. allocation = 1;
  4003. goto try_again;
  4004. }
  4005. xattr_set:
  4006. ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
  4007. out:
  4008. mlog_exit(ret);
  4009. return ret;
  4010. }
  4011. static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
  4012. struct ocfs2_xattr_bucket *bucket,
  4013. void *para)
  4014. {
  4015. int ret = 0;
  4016. struct ocfs2_xattr_header *xh = bucket->xh;
  4017. u16 i;
  4018. struct ocfs2_xattr_entry *xe;
  4019. for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
  4020. xe = &xh->xh_entries[i];
  4021. if (ocfs2_xattr_is_local(xe))
  4022. continue;
  4023. ret = ocfs2_xattr_bucket_value_truncate(inode,
  4024. bucket->bhs[0],
  4025. i, 0);
  4026. if (ret) {
  4027. mlog_errno(ret);
  4028. break;
  4029. }
  4030. }
  4031. return ret;
  4032. }
  4033. static int ocfs2_delete_xattr_index_block(struct inode *inode,
  4034. struct buffer_head *xb_bh)
  4035. {
  4036. struct ocfs2_xattr_block *xb =
  4037. (struct ocfs2_xattr_block *)xb_bh->b_data;
  4038. struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
  4039. int ret = 0;
  4040. u32 name_hash = UINT_MAX, e_cpos, num_clusters;
  4041. u64 p_blkno;
  4042. if (le16_to_cpu(el->l_next_free_rec) == 0)
  4043. return 0;
  4044. while (name_hash > 0) {
  4045. ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
  4046. &e_cpos, &num_clusters, el);
  4047. if (ret) {
  4048. mlog_errno(ret);
  4049. goto out;
  4050. }
  4051. ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
  4052. ocfs2_delete_xattr_in_bucket,
  4053. NULL);
  4054. if (ret) {
  4055. mlog_errno(ret);
  4056. goto out;
  4057. }
  4058. ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
  4059. p_blkno, e_cpos, num_clusters);
  4060. if (ret) {
  4061. mlog_errno(ret);
  4062. break;
  4063. }
  4064. if (e_cpos == 0)
  4065. break;
  4066. name_hash = e_cpos - 1;
  4067. }
  4068. out:
  4069. return ret;
  4070. }
  4071. /*
  4072. * 'trusted' attributes support
  4073. */
  4074. #define XATTR_TRUSTED_PREFIX "trusted."
  4075. static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
  4076. size_t list_size, const char *name,
  4077. size_t name_len)
  4078. {
  4079. const size_t prefix_len = sizeof(XATTR_TRUSTED_PREFIX) - 1;
  4080. const size_t total_len = prefix_len + name_len + 1;
  4081. if (list && total_len <= list_size) {
  4082. memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
  4083. memcpy(list + prefix_len, name, name_len);
  4084. list[prefix_len + name_len] = '\0';
  4085. }
  4086. return total_len;
  4087. }
  4088. static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
  4089. void *buffer, size_t size)
  4090. {
  4091. if (strcmp(name, "") == 0)
  4092. return -EINVAL;
  4093. return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
  4094. buffer, size);
  4095. }
  4096. static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
  4097. const void *value, size_t size, int flags)
  4098. {
  4099. if (strcmp(name, "") == 0)
  4100. return -EINVAL;
  4101. return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
  4102. size, flags);
  4103. }
  4104. struct xattr_handler ocfs2_xattr_trusted_handler = {
  4105. .prefix = XATTR_TRUSTED_PREFIX,
  4106. .list = ocfs2_xattr_trusted_list,
  4107. .get = ocfs2_xattr_trusted_get,
  4108. .set = ocfs2_xattr_trusted_set,
  4109. };
  4110. /*
  4111. * 'user' attributes support
  4112. */
  4113. #define XATTR_USER_PREFIX "user."
  4114. static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
  4115. size_t list_size, const char *name,
  4116. size_t name_len)
  4117. {
  4118. const size_t prefix_len = sizeof(XATTR_USER_PREFIX) - 1;
  4119. const size_t total_len = prefix_len + name_len + 1;
  4120. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4121. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  4122. return 0;
  4123. if (list && total_len <= list_size) {
  4124. memcpy(list, XATTR_USER_PREFIX, prefix_len);
  4125. memcpy(list + prefix_len, name, name_len);
  4126. list[prefix_len + name_len] = '\0';
  4127. }
  4128. return total_len;
  4129. }
  4130. static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
  4131. void *buffer, size_t size)
  4132. {
  4133. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4134. if (strcmp(name, "") == 0)
  4135. return -EINVAL;
  4136. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  4137. return -EOPNOTSUPP;
  4138. return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
  4139. buffer, size);
  4140. }
  4141. static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
  4142. const void *value, size_t size, int flags)
  4143. {
  4144. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  4145. if (strcmp(name, "") == 0)
  4146. return -EINVAL;
  4147. if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
  4148. return -EOPNOTSUPP;
  4149. return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
  4150. size, flags);
  4151. }
  4152. struct xattr_handler ocfs2_xattr_user_handler = {
  4153. .prefix = XATTR_USER_PREFIX,
  4154. .list = ocfs2_xattr_user_list,
  4155. .get = ocfs2_xattr_user_get,
  4156. .set = ocfs2_xattr_user_set,
  4157. };