xattr.c 140 KB

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