xattr.c 142 KB

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