inode.c 210 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/bio.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/file.h>
  22. #include <linux/fs.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/highmem.h>
  25. #include <linux/time.h>
  26. #include <linux/init.h>
  27. #include <linux/string.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/mpage.h>
  30. #include <linux/swap.h>
  31. #include <linux/writeback.h>
  32. #include <linux/statfs.h>
  33. #include <linux/compat.h>
  34. #include <linux/bit_spinlock.h>
  35. #include <linux/xattr.h>
  36. #include <linux/posix_acl.h>
  37. #include <linux/falloc.h>
  38. #include <linux/slab.h>
  39. #include <linux/ratelimit.h>
  40. #include <linux/mount.h>
  41. #include "compat.h"
  42. #include "ctree.h"
  43. #include "disk-io.h"
  44. #include "transaction.h"
  45. #include "btrfs_inode.h"
  46. #include "ioctl.h"
  47. #include "print-tree.h"
  48. #include "ordered-data.h"
  49. #include "xattr.h"
  50. #include "tree-log.h"
  51. #include "volumes.h"
  52. #include "compression.h"
  53. #include "locking.h"
  54. #include "free-space-cache.h"
  55. #include "inode-map.h"
  56. struct btrfs_iget_args {
  57. u64 ino;
  58. struct btrfs_root *root;
  59. };
  60. static const struct inode_operations btrfs_dir_inode_operations;
  61. static const struct inode_operations btrfs_symlink_inode_operations;
  62. static const struct inode_operations btrfs_dir_ro_inode_operations;
  63. static const struct inode_operations btrfs_special_inode_operations;
  64. static const struct inode_operations btrfs_file_inode_operations;
  65. static const struct address_space_operations btrfs_aops;
  66. static const struct address_space_operations btrfs_symlink_aops;
  67. static const struct file_operations btrfs_dir_file_operations;
  68. static struct extent_io_ops btrfs_extent_io_ops;
  69. static struct kmem_cache *btrfs_inode_cachep;
  70. static struct kmem_cache *btrfs_delalloc_work_cachep;
  71. struct kmem_cache *btrfs_trans_handle_cachep;
  72. struct kmem_cache *btrfs_transaction_cachep;
  73. struct kmem_cache *btrfs_path_cachep;
  74. struct kmem_cache *btrfs_free_space_cachep;
  75. #define S_SHIFT 12
  76. static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
  77. [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
  78. [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
  79. [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
  80. [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
  81. [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
  82. [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
  83. [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
  84. };
  85. static int btrfs_setsize(struct inode *inode, loff_t newsize);
  86. static int btrfs_truncate(struct inode *inode);
  87. static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
  88. static noinline int cow_file_range(struct inode *inode,
  89. struct page *locked_page,
  90. u64 start, u64 end, int *page_started,
  91. unsigned long *nr_written, int unlock);
  92. static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
  93. struct inode *inode, struct inode *dir,
  94. const struct qstr *qstr)
  95. {
  96. int err;
  97. err = btrfs_init_acl(trans, inode, dir);
  98. if (!err)
  99. err = btrfs_xattr_security_init(trans, inode, dir, qstr);
  100. return err;
  101. }
  102. /*
  103. * this does all the hard work for inserting an inline extent into
  104. * the btree. The caller should have done a btrfs_drop_extents so that
  105. * no overlapping inline items exist in the btree
  106. */
  107. static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
  108. struct btrfs_root *root, struct inode *inode,
  109. u64 start, size_t size, size_t compressed_size,
  110. int compress_type,
  111. struct page **compressed_pages)
  112. {
  113. struct btrfs_key key;
  114. struct btrfs_path *path;
  115. struct extent_buffer *leaf;
  116. struct page *page = NULL;
  117. char *kaddr;
  118. unsigned long ptr;
  119. struct btrfs_file_extent_item *ei;
  120. int err = 0;
  121. int ret;
  122. size_t cur_size = size;
  123. size_t datasize;
  124. unsigned long offset;
  125. if (compressed_size && compressed_pages)
  126. cur_size = compressed_size;
  127. path = btrfs_alloc_path();
  128. if (!path)
  129. return -ENOMEM;
  130. path->leave_spinning = 1;
  131. key.objectid = btrfs_ino(inode);
  132. key.offset = start;
  133. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  134. datasize = btrfs_file_extent_calc_inline_size(cur_size);
  135. inode_add_bytes(inode, size);
  136. ret = btrfs_insert_empty_item(trans, root, path, &key,
  137. datasize);
  138. if (ret) {
  139. err = ret;
  140. goto fail;
  141. }
  142. leaf = path->nodes[0];
  143. ei = btrfs_item_ptr(leaf, path->slots[0],
  144. struct btrfs_file_extent_item);
  145. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  146. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  147. btrfs_set_file_extent_encryption(leaf, ei, 0);
  148. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  149. btrfs_set_file_extent_ram_bytes(leaf, ei, size);
  150. ptr = btrfs_file_extent_inline_start(ei);
  151. if (compress_type != BTRFS_COMPRESS_NONE) {
  152. struct page *cpage;
  153. int i = 0;
  154. while (compressed_size > 0) {
  155. cpage = compressed_pages[i];
  156. cur_size = min_t(unsigned long, compressed_size,
  157. PAGE_CACHE_SIZE);
  158. kaddr = kmap_atomic(cpage);
  159. write_extent_buffer(leaf, kaddr, ptr, cur_size);
  160. kunmap_atomic(kaddr);
  161. i++;
  162. ptr += cur_size;
  163. compressed_size -= cur_size;
  164. }
  165. btrfs_set_file_extent_compression(leaf, ei,
  166. compress_type);
  167. } else {
  168. page = find_get_page(inode->i_mapping,
  169. start >> PAGE_CACHE_SHIFT);
  170. btrfs_set_file_extent_compression(leaf, ei, 0);
  171. kaddr = kmap_atomic(page);
  172. offset = start & (PAGE_CACHE_SIZE - 1);
  173. write_extent_buffer(leaf, kaddr + offset, ptr, size);
  174. kunmap_atomic(kaddr);
  175. page_cache_release(page);
  176. }
  177. btrfs_mark_buffer_dirty(leaf);
  178. btrfs_free_path(path);
  179. /*
  180. * we're an inline extent, so nobody can
  181. * extend the file past i_size without locking
  182. * a page we already have locked.
  183. *
  184. * We must do any isize and inode updates
  185. * before we unlock the pages. Otherwise we
  186. * could end up racing with unlink.
  187. */
  188. BTRFS_I(inode)->disk_i_size = inode->i_size;
  189. ret = btrfs_update_inode(trans, root, inode);
  190. return ret;
  191. fail:
  192. btrfs_free_path(path);
  193. return err;
  194. }
  195. /*
  196. * conditionally insert an inline extent into the file. This
  197. * does the checks required to make sure the data is small enough
  198. * to fit as an inline extent.
  199. */
  200. static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
  201. struct btrfs_root *root,
  202. struct inode *inode, u64 start, u64 end,
  203. size_t compressed_size, int compress_type,
  204. struct page **compressed_pages)
  205. {
  206. u64 isize = i_size_read(inode);
  207. u64 actual_end = min(end + 1, isize);
  208. u64 inline_len = actual_end - start;
  209. u64 aligned_end = (end + root->sectorsize - 1) &
  210. ~((u64)root->sectorsize - 1);
  211. u64 data_len = inline_len;
  212. int ret;
  213. if (compressed_size)
  214. data_len = compressed_size;
  215. if (start > 0 ||
  216. actual_end >= PAGE_CACHE_SIZE ||
  217. data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
  218. (!compressed_size &&
  219. (actual_end & (root->sectorsize - 1)) == 0) ||
  220. end + 1 < isize ||
  221. data_len > root->fs_info->max_inline) {
  222. return 1;
  223. }
  224. ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
  225. if (ret)
  226. return ret;
  227. if (isize > actual_end)
  228. inline_len = min_t(u64, isize, actual_end);
  229. ret = insert_inline_extent(trans, root, inode, start,
  230. inline_len, compressed_size,
  231. compress_type, compressed_pages);
  232. if (ret && ret != -ENOSPC) {
  233. btrfs_abort_transaction(trans, root, ret);
  234. return ret;
  235. } else if (ret == -ENOSPC) {
  236. return 1;
  237. }
  238. btrfs_delalloc_release_metadata(inode, end + 1 - start);
  239. btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
  240. return 0;
  241. }
  242. struct async_extent {
  243. u64 start;
  244. u64 ram_size;
  245. u64 compressed_size;
  246. struct page **pages;
  247. unsigned long nr_pages;
  248. int compress_type;
  249. struct list_head list;
  250. };
  251. struct async_cow {
  252. struct inode *inode;
  253. struct btrfs_root *root;
  254. struct page *locked_page;
  255. u64 start;
  256. u64 end;
  257. struct list_head extents;
  258. struct btrfs_work work;
  259. };
  260. static noinline int add_async_extent(struct async_cow *cow,
  261. u64 start, u64 ram_size,
  262. u64 compressed_size,
  263. struct page **pages,
  264. unsigned long nr_pages,
  265. int compress_type)
  266. {
  267. struct async_extent *async_extent;
  268. async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
  269. BUG_ON(!async_extent); /* -ENOMEM */
  270. async_extent->start = start;
  271. async_extent->ram_size = ram_size;
  272. async_extent->compressed_size = compressed_size;
  273. async_extent->pages = pages;
  274. async_extent->nr_pages = nr_pages;
  275. async_extent->compress_type = compress_type;
  276. list_add_tail(&async_extent->list, &cow->extents);
  277. return 0;
  278. }
  279. /*
  280. * we create compressed extents in two phases. The first
  281. * phase compresses a range of pages that have already been
  282. * locked (both pages and state bits are locked).
  283. *
  284. * This is done inside an ordered work queue, and the compression
  285. * is spread across many cpus. The actual IO submission is step
  286. * two, and the ordered work queue takes care of making sure that
  287. * happens in the same order things were put onto the queue by
  288. * writepages and friends.
  289. *
  290. * If this code finds it can't get good compression, it puts an
  291. * entry onto the work queue to write the uncompressed bytes. This
  292. * makes sure that both compressed inodes and uncompressed inodes
  293. * are written in the same order that the flusher thread sent them
  294. * down.
  295. */
  296. static noinline int compress_file_range(struct inode *inode,
  297. struct page *locked_page,
  298. u64 start, u64 end,
  299. struct async_cow *async_cow,
  300. int *num_added)
  301. {
  302. struct btrfs_root *root = BTRFS_I(inode)->root;
  303. struct btrfs_trans_handle *trans;
  304. u64 num_bytes;
  305. u64 blocksize = root->sectorsize;
  306. u64 actual_end;
  307. u64 isize = i_size_read(inode);
  308. int ret = 0;
  309. struct page **pages = NULL;
  310. unsigned long nr_pages;
  311. unsigned long nr_pages_ret = 0;
  312. unsigned long total_compressed = 0;
  313. unsigned long total_in = 0;
  314. unsigned long max_compressed = 128 * 1024;
  315. unsigned long max_uncompressed = 128 * 1024;
  316. int i;
  317. int will_compress;
  318. int compress_type = root->fs_info->compress_type;
  319. /* if this is a small write inside eof, kick off a defrag */
  320. if ((end - start + 1) < 16 * 1024 &&
  321. (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
  322. btrfs_add_inode_defrag(NULL, inode);
  323. actual_end = min_t(u64, isize, end + 1);
  324. again:
  325. will_compress = 0;
  326. nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
  327. nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
  328. /*
  329. * we don't want to send crud past the end of i_size through
  330. * compression, that's just a waste of CPU time. So, if the
  331. * end of the file is before the start of our current
  332. * requested range of bytes, we bail out to the uncompressed
  333. * cleanup code that can deal with all of this.
  334. *
  335. * It isn't really the fastest way to fix things, but this is a
  336. * very uncommon corner.
  337. */
  338. if (actual_end <= start)
  339. goto cleanup_and_bail_uncompressed;
  340. total_compressed = actual_end - start;
  341. /* we want to make sure that amount of ram required to uncompress
  342. * an extent is reasonable, so we limit the total size in ram
  343. * of a compressed extent to 128k. This is a crucial number
  344. * because it also controls how easily we can spread reads across
  345. * cpus for decompression.
  346. *
  347. * We also want to make sure the amount of IO required to do
  348. * a random read is reasonably small, so we limit the size of
  349. * a compressed extent to 128k.
  350. */
  351. total_compressed = min(total_compressed, max_uncompressed);
  352. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  353. num_bytes = max(blocksize, num_bytes);
  354. total_in = 0;
  355. ret = 0;
  356. /*
  357. * we do compression for mount -o compress and when the
  358. * inode has not been flagged as nocompress. This flag can
  359. * change at any time if we discover bad compression ratios.
  360. */
  361. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
  362. (btrfs_test_opt(root, COMPRESS) ||
  363. (BTRFS_I(inode)->force_compress) ||
  364. (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
  365. WARN_ON(pages);
  366. pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
  367. if (!pages) {
  368. /* just bail out to the uncompressed code */
  369. goto cont;
  370. }
  371. if (BTRFS_I(inode)->force_compress)
  372. compress_type = BTRFS_I(inode)->force_compress;
  373. ret = btrfs_compress_pages(compress_type,
  374. inode->i_mapping, start,
  375. total_compressed, pages,
  376. nr_pages, &nr_pages_ret,
  377. &total_in,
  378. &total_compressed,
  379. max_compressed);
  380. if (!ret) {
  381. unsigned long offset = total_compressed &
  382. (PAGE_CACHE_SIZE - 1);
  383. struct page *page = pages[nr_pages_ret - 1];
  384. char *kaddr;
  385. /* zero the tail end of the last page, we might be
  386. * sending it down to disk
  387. */
  388. if (offset) {
  389. kaddr = kmap_atomic(page);
  390. memset(kaddr + offset, 0,
  391. PAGE_CACHE_SIZE - offset);
  392. kunmap_atomic(kaddr);
  393. }
  394. will_compress = 1;
  395. }
  396. }
  397. cont:
  398. if (start == 0) {
  399. trans = btrfs_join_transaction(root);
  400. if (IS_ERR(trans)) {
  401. ret = PTR_ERR(trans);
  402. trans = NULL;
  403. goto cleanup_and_out;
  404. }
  405. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  406. /* lets try to make an inline extent */
  407. if (ret || total_in < (actual_end - start)) {
  408. /* we didn't compress the entire range, try
  409. * to make an uncompressed inline extent.
  410. */
  411. ret = cow_file_range_inline(trans, root, inode,
  412. start, end, 0, 0, NULL);
  413. } else {
  414. /* try making a compressed inline extent */
  415. ret = cow_file_range_inline(trans, root, inode,
  416. start, end,
  417. total_compressed,
  418. compress_type, pages);
  419. }
  420. if (ret <= 0) {
  421. /*
  422. * inline extent creation worked or returned error,
  423. * we don't need to create any more async work items.
  424. * Unlock and free up our temp pages.
  425. */
  426. extent_clear_unlock_delalloc(inode,
  427. &BTRFS_I(inode)->io_tree,
  428. start, end, NULL,
  429. EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
  430. EXTENT_CLEAR_DELALLOC |
  431. EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
  432. btrfs_end_transaction(trans, root);
  433. goto free_pages_out;
  434. }
  435. btrfs_end_transaction(trans, root);
  436. }
  437. if (will_compress) {
  438. /*
  439. * we aren't doing an inline extent round the compressed size
  440. * up to a block size boundary so the allocator does sane
  441. * things
  442. */
  443. total_compressed = (total_compressed + blocksize - 1) &
  444. ~(blocksize - 1);
  445. /*
  446. * one last check to make sure the compression is really a
  447. * win, compare the page count read with the blocks on disk
  448. */
  449. total_in = (total_in + PAGE_CACHE_SIZE - 1) &
  450. ~(PAGE_CACHE_SIZE - 1);
  451. if (total_compressed >= total_in) {
  452. will_compress = 0;
  453. } else {
  454. num_bytes = total_in;
  455. }
  456. }
  457. if (!will_compress && pages) {
  458. /*
  459. * the compression code ran but failed to make things smaller,
  460. * free any pages it allocated and our page pointer array
  461. */
  462. for (i = 0; i < nr_pages_ret; i++) {
  463. WARN_ON(pages[i]->mapping);
  464. page_cache_release(pages[i]);
  465. }
  466. kfree(pages);
  467. pages = NULL;
  468. total_compressed = 0;
  469. nr_pages_ret = 0;
  470. /* flag the file so we don't compress in the future */
  471. if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
  472. !(BTRFS_I(inode)->force_compress)) {
  473. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  474. }
  475. }
  476. if (will_compress) {
  477. *num_added += 1;
  478. /* the async work queues will take care of doing actual
  479. * allocation on disk for these compressed pages,
  480. * and will submit them to the elevator.
  481. */
  482. add_async_extent(async_cow, start, num_bytes,
  483. total_compressed, pages, nr_pages_ret,
  484. compress_type);
  485. if (start + num_bytes < end) {
  486. start += num_bytes;
  487. pages = NULL;
  488. cond_resched();
  489. goto again;
  490. }
  491. } else {
  492. cleanup_and_bail_uncompressed:
  493. /*
  494. * No compression, but we still need to write the pages in
  495. * the file we've been given so far. redirty the locked
  496. * page if it corresponds to our extent and set things up
  497. * for the async work queue to run cow_file_range to do
  498. * the normal delalloc dance
  499. */
  500. if (page_offset(locked_page) >= start &&
  501. page_offset(locked_page) <= end) {
  502. __set_page_dirty_nobuffers(locked_page);
  503. /* unlocked later on in the async handlers */
  504. }
  505. add_async_extent(async_cow, start, end - start + 1,
  506. 0, NULL, 0, BTRFS_COMPRESS_NONE);
  507. *num_added += 1;
  508. }
  509. out:
  510. return ret;
  511. free_pages_out:
  512. for (i = 0; i < nr_pages_ret; i++) {
  513. WARN_ON(pages[i]->mapping);
  514. page_cache_release(pages[i]);
  515. }
  516. kfree(pages);
  517. goto out;
  518. cleanup_and_out:
  519. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  520. start, end, NULL,
  521. EXTENT_CLEAR_UNLOCK_PAGE |
  522. EXTENT_CLEAR_DIRTY |
  523. EXTENT_CLEAR_DELALLOC |
  524. EXTENT_SET_WRITEBACK |
  525. EXTENT_END_WRITEBACK);
  526. if (!trans || IS_ERR(trans))
  527. btrfs_error(root->fs_info, ret, "Failed to join transaction");
  528. else
  529. btrfs_abort_transaction(trans, root, ret);
  530. goto free_pages_out;
  531. }
  532. /*
  533. * phase two of compressed writeback. This is the ordered portion
  534. * of the code, which only gets called in the order the work was
  535. * queued. We walk all the async extents created by compress_file_range
  536. * and send them down to the disk.
  537. */
  538. static noinline int submit_compressed_extents(struct inode *inode,
  539. struct async_cow *async_cow)
  540. {
  541. struct async_extent *async_extent;
  542. u64 alloc_hint = 0;
  543. struct btrfs_trans_handle *trans;
  544. struct btrfs_key ins;
  545. struct extent_map *em;
  546. struct btrfs_root *root = BTRFS_I(inode)->root;
  547. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  548. struct extent_io_tree *io_tree;
  549. int ret = 0;
  550. if (list_empty(&async_cow->extents))
  551. return 0;
  552. while (!list_empty(&async_cow->extents)) {
  553. async_extent = list_entry(async_cow->extents.next,
  554. struct async_extent, list);
  555. list_del(&async_extent->list);
  556. io_tree = &BTRFS_I(inode)->io_tree;
  557. retry:
  558. /* did the compression code fall back to uncompressed IO? */
  559. if (!async_extent->pages) {
  560. int page_started = 0;
  561. unsigned long nr_written = 0;
  562. lock_extent(io_tree, async_extent->start,
  563. async_extent->start +
  564. async_extent->ram_size - 1);
  565. /* allocate blocks */
  566. ret = cow_file_range(inode, async_cow->locked_page,
  567. async_extent->start,
  568. async_extent->start +
  569. async_extent->ram_size - 1,
  570. &page_started, &nr_written, 0);
  571. /* JDM XXX */
  572. /*
  573. * if page_started, cow_file_range inserted an
  574. * inline extent and took care of all the unlocking
  575. * and IO for us. Otherwise, we need to submit
  576. * all those pages down to the drive.
  577. */
  578. if (!page_started && !ret)
  579. extent_write_locked_range(io_tree,
  580. inode, async_extent->start,
  581. async_extent->start +
  582. async_extent->ram_size - 1,
  583. btrfs_get_extent,
  584. WB_SYNC_ALL);
  585. kfree(async_extent);
  586. cond_resched();
  587. continue;
  588. }
  589. lock_extent(io_tree, async_extent->start,
  590. async_extent->start + async_extent->ram_size - 1);
  591. trans = btrfs_join_transaction(root);
  592. if (IS_ERR(trans)) {
  593. ret = PTR_ERR(trans);
  594. } else {
  595. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  596. ret = btrfs_reserve_extent(trans, root,
  597. async_extent->compressed_size,
  598. async_extent->compressed_size,
  599. 0, alloc_hint, &ins, 1);
  600. if (ret && ret != -ENOSPC)
  601. btrfs_abort_transaction(trans, root, ret);
  602. btrfs_end_transaction(trans, root);
  603. }
  604. if (ret) {
  605. int i;
  606. for (i = 0; i < async_extent->nr_pages; i++) {
  607. WARN_ON(async_extent->pages[i]->mapping);
  608. page_cache_release(async_extent->pages[i]);
  609. }
  610. kfree(async_extent->pages);
  611. async_extent->nr_pages = 0;
  612. async_extent->pages = NULL;
  613. unlock_extent(io_tree, async_extent->start,
  614. async_extent->start +
  615. async_extent->ram_size - 1);
  616. if (ret == -ENOSPC)
  617. goto retry;
  618. goto out_free; /* JDM: Requeue? */
  619. }
  620. /*
  621. * here we're doing allocation and writeback of the
  622. * compressed pages
  623. */
  624. btrfs_drop_extent_cache(inode, async_extent->start,
  625. async_extent->start +
  626. async_extent->ram_size - 1, 0);
  627. em = alloc_extent_map();
  628. BUG_ON(!em); /* -ENOMEM */
  629. em->start = async_extent->start;
  630. em->len = async_extent->ram_size;
  631. em->orig_start = em->start;
  632. em->block_start = ins.objectid;
  633. em->block_len = ins.offset;
  634. em->bdev = root->fs_info->fs_devices->latest_bdev;
  635. em->compress_type = async_extent->compress_type;
  636. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  637. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  638. while (1) {
  639. write_lock(&em_tree->lock);
  640. ret = add_extent_mapping(em_tree, em);
  641. write_unlock(&em_tree->lock);
  642. if (ret != -EEXIST) {
  643. free_extent_map(em);
  644. break;
  645. }
  646. btrfs_drop_extent_cache(inode, async_extent->start,
  647. async_extent->start +
  648. async_extent->ram_size - 1, 0);
  649. }
  650. ret = btrfs_add_ordered_extent_compress(inode,
  651. async_extent->start,
  652. ins.objectid,
  653. async_extent->ram_size,
  654. ins.offset,
  655. BTRFS_ORDERED_COMPRESSED,
  656. async_extent->compress_type);
  657. BUG_ON(ret); /* -ENOMEM */
  658. /*
  659. * clear dirty, set writeback and unlock the pages.
  660. */
  661. extent_clear_unlock_delalloc(inode,
  662. &BTRFS_I(inode)->io_tree,
  663. async_extent->start,
  664. async_extent->start +
  665. async_extent->ram_size - 1,
  666. NULL, EXTENT_CLEAR_UNLOCK_PAGE |
  667. EXTENT_CLEAR_UNLOCK |
  668. EXTENT_CLEAR_DELALLOC |
  669. EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
  670. ret = btrfs_submit_compressed_write(inode,
  671. async_extent->start,
  672. async_extent->ram_size,
  673. ins.objectid,
  674. ins.offset, async_extent->pages,
  675. async_extent->nr_pages);
  676. BUG_ON(ret); /* -ENOMEM */
  677. alloc_hint = ins.objectid + ins.offset;
  678. kfree(async_extent);
  679. cond_resched();
  680. }
  681. ret = 0;
  682. out:
  683. return ret;
  684. out_free:
  685. kfree(async_extent);
  686. goto out;
  687. }
  688. static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
  689. u64 num_bytes)
  690. {
  691. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  692. struct extent_map *em;
  693. u64 alloc_hint = 0;
  694. read_lock(&em_tree->lock);
  695. em = search_extent_mapping(em_tree, start, num_bytes);
  696. if (em) {
  697. /*
  698. * if block start isn't an actual block number then find the
  699. * first block in this inode and use that as a hint. If that
  700. * block is also bogus then just don't worry about it.
  701. */
  702. if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
  703. free_extent_map(em);
  704. em = search_extent_mapping(em_tree, 0, 0);
  705. if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
  706. alloc_hint = em->block_start;
  707. if (em)
  708. free_extent_map(em);
  709. } else {
  710. alloc_hint = em->block_start;
  711. free_extent_map(em);
  712. }
  713. }
  714. read_unlock(&em_tree->lock);
  715. return alloc_hint;
  716. }
  717. /*
  718. * when extent_io.c finds a delayed allocation range in the file,
  719. * the call backs end up in this code. The basic idea is to
  720. * allocate extents on disk for the range, and create ordered data structs
  721. * in ram to track those extents.
  722. *
  723. * locked_page is the page that writepage had locked already. We use
  724. * it to make sure we don't do extra locks or unlocks.
  725. *
  726. * *page_started is set to one if we unlock locked_page and do everything
  727. * required to start IO on it. It may be clean and already done with
  728. * IO when we return.
  729. */
  730. static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
  731. struct inode *inode,
  732. struct btrfs_root *root,
  733. struct page *locked_page,
  734. u64 start, u64 end, int *page_started,
  735. unsigned long *nr_written,
  736. int unlock)
  737. {
  738. u64 alloc_hint = 0;
  739. u64 num_bytes;
  740. unsigned long ram_size;
  741. u64 disk_num_bytes;
  742. u64 cur_alloc_size;
  743. u64 blocksize = root->sectorsize;
  744. struct btrfs_key ins;
  745. struct extent_map *em;
  746. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  747. int ret = 0;
  748. BUG_ON(btrfs_is_free_space_inode(inode));
  749. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  750. num_bytes = max(blocksize, num_bytes);
  751. disk_num_bytes = num_bytes;
  752. /* if this is a small write inside eof, kick off defrag */
  753. if (num_bytes < 64 * 1024 &&
  754. (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
  755. btrfs_add_inode_defrag(trans, inode);
  756. if (start == 0) {
  757. /* lets try to make an inline extent */
  758. ret = cow_file_range_inline(trans, root, inode,
  759. start, end, 0, 0, NULL);
  760. if (ret == 0) {
  761. extent_clear_unlock_delalloc(inode,
  762. &BTRFS_I(inode)->io_tree,
  763. start, end, NULL,
  764. EXTENT_CLEAR_UNLOCK_PAGE |
  765. EXTENT_CLEAR_UNLOCK |
  766. EXTENT_CLEAR_DELALLOC |
  767. EXTENT_CLEAR_DIRTY |
  768. EXTENT_SET_WRITEBACK |
  769. EXTENT_END_WRITEBACK);
  770. *nr_written = *nr_written +
  771. (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
  772. *page_started = 1;
  773. goto out;
  774. } else if (ret < 0) {
  775. btrfs_abort_transaction(trans, root, ret);
  776. goto out_unlock;
  777. }
  778. }
  779. BUG_ON(disk_num_bytes >
  780. btrfs_super_total_bytes(root->fs_info->super_copy));
  781. alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
  782. btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
  783. while (disk_num_bytes > 0) {
  784. unsigned long op;
  785. cur_alloc_size = disk_num_bytes;
  786. ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
  787. root->sectorsize, 0, alloc_hint,
  788. &ins, 1);
  789. if (ret < 0) {
  790. btrfs_abort_transaction(trans, root, ret);
  791. goto out_unlock;
  792. }
  793. em = alloc_extent_map();
  794. BUG_ON(!em); /* -ENOMEM */
  795. em->start = start;
  796. em->orig_start = em->start;
  797. ram_size = ins.offset;
  798. em->len = ins.offset;
  799. em->block_start = ins.objectid;
  800. em->block_len = ins.offset;
  801. em->bdev = root->fs_info->fs_devices->latest_bdev;
  802. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  803. while (1) {
  804. write_lock(&em_tree->lock);
  805. ret = add_extent_mapping(em_tree, em);
  806. write_unlock(&em_tree->lock);
  807. if (ret != -EEXIST) {
  808. free_extent_map(em);
  809. break;
  810. }
  811. btrfs_drop_extent_cache(inode, start,
  812. start + ram_size - 1, 0);
  813. }
  814. cur_alloc_size = ins.offset;
  815. ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
  816. ram_size, cur_alloc_size, 0);
  817. BUG_ON(ret); /* -ENOMEM */
  818. if (root->root_key.objectid ==
  819. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  820. ret = btrfs_reloc_clone_csums(inode, start,
  821. cur_alloc_size);
  822. if (ret) {
  823. btrfs_abort_transaction(trans, root, ret);
  824. goto out_unlock;
  825. }
  826. }
  827. if (disk_num_bytes < cur_alloc_size)
  828. break;
  829. /* we're not doing compressed IO, don't unlock the first
  830. * page (which the caller expects to stay locked), don't
  831. * clear any dirty bits and don't set any writeback bits
  832. *
  833. * Do set the Private2 bit so we know this page was properly
  834. * setup for writepage
  835. */
  836. op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
  837. op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
  838. EXTENT_SET_PRIVATE2;
  839. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  840. start, start + ram_size - 1,
  841. locked_page, op);
  842. disk_num_bytes -= cur_alloc_size;
  843. num_bytes -= cur_alloc_size;
  844. alloc_hint = ins.objectid + ins.offset;
  845. start += cur_alloc_size;
  846. }
  847. out:
  848. return ret;
  849. out_unlock:
  850. extent_clear_unlock_delalloc(inode,
  851. &BTRFS_I(inode)->io_tree,
  852. start, end, locked_page,
  853. EXTENT_CLEAR_UNLOCK_PAGE |
  854. EXTENT_CLEAR_UNLOCK |
  855. EXTENT_CLEAR_DELALLOC |
  856. EXTENT_CLEAR_DIRTY |
  857. EXTENT_SET_WRITEBACK |
  858. EXTENT_END_WRITEBACK);
  859. goto out;
  860. }
  861. static noinline int cow_file_range(struct inode *inode,
  862. struct page *locked_page,
  863. u64 start, u64 end, int *page_started,
  864. unsigned long *nr_written,
  865. int unlock)
  866. {
  867. struct btrfs_trans_handle *trans;
  868. struct btrfs_root *root = BTRFS_I(inode)->root;
  869. int ret;
  870. trans = btrfs_join_transaction(root);
  871. if (IS_ERR(trans)) {
  872. extent_clear_unlock_delalloc(inode,
  873. &BTRFS_I(inode)->io_tree,
  874. start, end, locked_page,
  875. EXTENT_CLEAR_UNLOCK_PAGE |
  876. EXTENT_CLEAR_UNLOCK |
  877. EXTENT_CLEAR_DELALLOC |
  878. EXTENT_CLEAR_DIRTY |
  879. EXTENT_SET_WRITEBACK |
  880. EXTENT_END_WRITEBACK);
  881. return PTR_ERR(trans);
  882. }
  883. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  884. ret = __cow_file_range(trans, inode, root, locked_page, start, end,
  885. page_started, nr_written, unlock);
  886. btrfs_end_transaction(trans, root);
  887. return ret;
  888. }
  889. /*
  890. * work queue call back to started compression on a file and pages
  891. */
  892. static noinline void async_cow_start(struct btrfs_work *work)
  893. {
  894. struct async_cow *async_cow;
  895. int num_added = 0;
  896. async_cow = container_of(work, struct async_cow, work);
  897. compress_file_range(async_cow->inode, async_cow->locked_page,
  898. async_cow->start, async_cow->end, async_cow,
  899. &num_added);
  900. if (num_added == 0) {
  901. btrfs_add_delayed_iput(async_cow->inode);
  902. async_cow->inode = NULL;
  903. }
  904. }
  905. /*
  906. * work queue call back to submit previously compressed pages
  907. */
  908. static noinline void async_cow_submit(struct btrfs_work *work)
  909. {
  910. struct async_cow *async_cow;
  911. struct btrfs_root *root;
  912. unsigned long nr_pages;
  913. async_cow = container_of(work, struct async_cow, work);
  914. root = async_cow->root;
  915. nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
  916. PAGE_CACHE_SHIFT;
  917. if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
  918. 5 * 1024 * 1024 &&
  919. waitqueue_active(&root->fs_info->async_submit_wait))
  920. wake_up(&root->fs_info->async_submit_wait);
  921. if (async_cow->inode)
  922. submit_compressed_extents(async_cow->inode, async_cow);
  923. }
  924. static noinline void async_cow_free(struct btrfs_work *work)
  925. {
  926. struct async_cow *async_cow;
  927. async_cow = container_of(work, struct async_cow, work);
  928. if (async_cow->inode)
  929. btrfs_add_delayed_iput(async_cow->inode);
  930. kfree(async_cow);
  931. }
  932. static int cow_file_range_async(struct inode *inode, struct page *locked_page,
  933. u64 start, u64 end, int *page_started,
  934. unsigned long *nr_written)
  935. {
  936. struct async_cow *async_cow;
  937. struct btrfs_root *root = BTRFS_I(inode)->root;
  938. unsigned long nr_pages;
  939. u64 cur_end;
  940. int limit = 10 * 1024 * 1024;
  941. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
  942. 1, 0, NULL, GFP_NOFS);
  943. while (start < end) {
  944. async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
  945. BUG_ON(!async_cow); /* -ENOMEM */
  946. async_cow->inode = igrab(inode);
  947. async_cow->root = root;
  948. async_cow->locked_page = locked_page;
  949. async_cow->start = start;
  950. if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
  951. cur_end = end;
  952. else
  953. cur_end = min(end, start + 512 * 1024 - 1);
  954. async_cow->end = cur_end;
  955. INIT_LIST_HEAD(&async_cow->extents);
  956. async_cow->work.func = async_cow_start;
  957. async_cow->work.ordered_func = async_cow_submit;
  958. async_cow->work.ordered_free = async_cow_free;
  959. async_cow->work.flags = 0;
  960. nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
  961. PAGE_CACHE_SHIFT;
  962. atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
  963. btrfs_queue_worker(&root->fs_info->delalloc_workers,
  964. &async_cow->work);
  965. if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
  966. wait_event(root->fs_info->async_submit_wait,
  967. (atomic_read(&root->fs_info->async_delalloc_pages) <
  968. limit));
  969. }
  970. while (atomic_read(&root->fs_info->async_submit_draining) &&
  971. atomic_read(&root->fs_info->async_delalloc_pages)) {
  972. wait_event(root->fs_info->async_submit_wait,
  973. (atomic_read(&root->fs_info->async_delalloc_pages) ==
  974. 0));
  975. }
  976. *nr_written += nr_pages;
  977. start = cur_end + 1;
  978. }
  979. *page_started = 1;
  980. return 0;
  981. }
  982. static noinline int csum_exist_in_range(struct btrfs_root *root,
  983. u64 bytenr, u64 num_bytes)
  984. {
  985. int ret;
  986. struct btrfs_ordered_sum *sums;
  987. LIST_HEAD(list);
  988. ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
  989. bytenr + num_bytes - 1, &list, 0);
  990. if (ret == 0 && list_empty(&list))
  991. return 0;
  992. while (!list_empty(&list)) {
  993. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  994. list_del(&sums->list);
  995. kfree(sums);
  996. }
  997. return 1;
  998. }
  999. /*
  1000. * when nowcow writeback call back. This checks for snapshots or COW copies
  1001. * of the extents that exist in the file, and COWs the file as required.
  1002. *
  1003. * If no cow copies or snapshots exist, we write directly to the existing
  1004. * blocks on disk
  1005. */
  1006. static noinline int run_delalloc_nocow(struct inode *inode,
  1007. struct page *locked_page,
  1008. u64 start, u64 end, int *page_started, int force,
  1009. unsigned long *nr_written)
  1010. {
  1011. struct btrfs_root *root = BTRFS_I(inode)->root;
  1012. struct btrfs_trans_handle *trans;
  1013. struct extent_buffer *leaf;
  1014. struct btrfs_path *path;
  1015. struct btrfs_file_extent_item *fi;
  1016. struct btrfs_key found_key;
  1017. u64 cow_start;
  1018. u64 cur_offset;
  1019. u64 extent_end;
  1020. u64 extent_offset;
  1021. u64 disk_bytenr;
  1022. u64 num_bytes;
  1023. int extent_type;
  1024. int ret, err;
  1025. int type;
  1026. int nocow;
  1027. int check_prev = 1;
  1028. bool nolock;
  1029. u64 ino = btrfs_ino(inode);
  1030. path = btrfs_alloc_path();
  1031. if (!path) {
  1032. extent_clear_unlock_delalloc(inode,
  1033. &BTRFS_I(inode)->io_tree,
  1034. start, end, locked_page,
  1035. EXTENT_CLEAR_UNLOCK_PAGE |
  1036. EXTENT_CLEAR_UNLOCK |
  1037. EXTENT_CLEAR_DELALLOC |
  1038. EXTENT_CLEAR_DIRTY |
  1039. EXTENT_SET_WRITEBACK |
  1040. EXTENT_END_WRITEBACK);
  1041. return -ENOMEM;
  1042. }
  1043. nolock = btrfs_is_free_space_inode(inode);
  1044. if (nolock)
  1045. trans = btrfs_join_transaction_nolock(root);
  1046. else
  1047. trans = btrfs_join_transaction(root);
  1048. if (IS_ERR(trans)) {
  1049. extent_clear_unlock_delalloc(inode,
  1050. &BTRFS_I(inode)->io_tree,
  1051. start, end, locked_page,
  1052. EXTENT_CLEAR_UNLOCK_PAGE |
  1053. EXTENT_CLEAR_UNLOCK |
  1054. EXTENT_CLEAR_DELALLOC |
  1055. EXTENT_CLEAR_DIRTY |
  1056. EXTENT_SET_WRITEBACK |
  1057. EXTENT_END_WRITEBACK);
  1058. btrfs_free_path(path);
  1059. return PTR_ERR(trans);
  1060. }
  1061. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  1062. cow_start = (u64)-1;
  1063. cur_offset = start;
  1064. while (1) {
  1065. ret = btrfs_lookup_file_extent(trans, root, path, ino,
  1066. cur_offset, 0);
  1067. if (ret < 0) {
  1068. btrfs_abort_transaction(trans, root, ret);
  1069. goto error;
  1070. }
  1071. if (ret > 0 && path->slots[0] > 0 && check_prev) {
  1072. leaf = path->nodes[0];
  1073. btrfs_item_key_to_cpu(leaf, &found_key,
  1074. path->slots[0] - 1);
  1075. if (found_key.objectid == ino &&
  1076. found_key.type == BTRFS_EXTENT_DATA_KEY)
  1077. path->slots[0]--;
  1078. }
  1079. check_prev = 0;
  1080. next_slot:
  1081. leaf = path->nodes[0];
  1082. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1083. ret = btrfs_next_leaf(root, path);
  1084. if (ret < 0) {
  1085. btrfs_abort_transaction(trans, root, ret);
  1086. goto error;
  1087. }
  1088. if (ret > 0)
  1089. break;
  1090. leaf = path->nodes[0];
  1091. }
  1092. nocow = 0;
  1093. disk_bytenr = 0;
  1094. num_bytes = 0;
  1095. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1096. if (found_key.objectid > ino ||
  1097. found_key.type > BTRFS_EXTENT_DATA_KEY ||
  1098. found_key.offset > end)
  1099. break;
  1100. if (found_key.offset > cur_offset) {
  1101. extent_end = found_key.offset;
  1102. extent_type = 0;
  1103. goto out_check;
  1104. }
  1105. fi = btrfs_item_ptr(leaf, path->slots[0],
  1106. struct btrfs_file_extent_item);
  1107. extent_type = btrfs_file_extent_type(leaf, fi);
  1108. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  1109. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  1110. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  1111. extent_offset = btrfs_file_extent_offset(leaf, fi);
  1112. extent_end = found_key.offset +
  1113. btrfs_file_extent_num_bytes(leaf, fi);
  1114. if (extent_end <= start) {
  1115. path->slots[0]++;
  1116. goto next_slot;
  1117. }
  1118. if (disk_bytenr == 0)
  1119. goto out_check;
  1120. if (btrfs_file_extent_compression(leaf, fi) ||
  1121. btrfs_file_extent_encryption(leaf, fi) ||
  1122. btrfs_file_extent_other_encoding(leaf, fi))
  1123. goto out_check;
  1124. if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
  1125. goto out_check;
  1126. if (btrfs_extent_readonly(root, disk_bytenr))
  1127. goto out_check;
  1128. if (btrfs_cross_ref_exist(trans, root, ino,
  1129. found_key.offset -
  1130. extent_offset, disk_bytenr))
  1131. goto out_check;
  1132. disk_bytenr += extent_offset;
  1133. disk_bytenr += cur_offset - found_key.offset;
  1134. num_bytes = min(end + 1, extent_end) - cur_offset;
  1135. /*
  1136. * force cow if csum exists in the range.
  1137. * this ensure that csum for a given extent are
  1138. * either valid or do not exist.
  1139. */
  1140. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  1141. goto out_check;
  1142. nocow = 1;
  1143. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  1144. extent_end = found_key.offset +
  1145. btrfs_file_extent_inline_len(leaf, fi);
  1146. extent_end = ALIGN(extent_end, root->sectorsize);
  1147. } else {
  1148. BUG_ON(1);
  1149. }
  1150. out_check:
  1151. if (extent_end <= start) {
  1152. path->slots[0]++;
  1153. goto next_slot;
  1154. }
  1155. if (!nocow) {
  1156. if (cow_start == (u64)-1)
  1157. cow_start = cur_offset;
  1158. cur_offset = extent_end;
  1159. if (cur_offset > end)
  1160. break;
  1161. path->slots[0]++;
  1162. goto next_slot;
  1163. }
  1164. btrfs_release_path(path);
  1165. if (cow_start != (u64)-1) {
  1166. ret = __cow_file_range(trans, inode, root, locked_page,
  1167. cow_start, found_key.offset - 1,
  1168. page_started, nr_written, 1);
  1169. if (ret) {
  1170. btrfs_abort_transaction(trans, root, ret);
  1171. goto error;
  1172. }
  1173. cow_start = (u64)-1;
  1174. }
  1175. if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  1176. struct extent_map *em;
  1177. struct extent_map_tree *em_tree;
  1178. em_tree = &BTRFS_I(inode)->extent_tree;
  1179. em = alloc_extent_map();
  1180. BUG_ON(!em); /* -ENOMEM */
  1181. em->start = cur_offset;
  1182. em->orig_start = em->start;
  1183. em->len = num_bytes;
  1184. em->block_len = num_bytes;
  1185. em->block_start = disk_bytenr;
  1186. em->bdev = root->fs_info->fs_devices->latest_bdev;
  1187. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  1188. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  1189. while (1) {
  1190. write_lock(&em_tree->lock);
  1191. ret = add_extent_mapping(em_tree, em);
  1192. write_unlock(&em_tree->lock);
  1193. if (ret != -EEXIST) {
  1194. free_extent_map(em);
  1195. break;
  1196. }
  1197. btrfs_drop_extent_cache(inode, em->start,
  1198. em->start + em->len - 1, 0);
  1199. }
  1200. type = BTRFS_ORDERED_PREALLOC;
  1201. } else {
  1202. type = BTRFS_ORDERED_NOCOW;
  1203. }
  1204. ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
  1205. num_bytes, num_bytes, type);
  1206. BUG_ON(ret); /* -ENOMEM */
  1207. if (root->root_key.objectid ==
  1208. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  1209. ret = btrfs_reloc_clone_csums(inode, cur_offset,
  1210. num_bytes);
  1211. if (ret) {
  1212. btrfs_abort_transaction(trans, root, ret);
  1213. goto error;
  1214. }
  1215. }
  1216. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  1217. cur_offset, cur_offset + num_bytes - 1,
  1218. locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
  1219. EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
  1220. EXTENT_SET_PRIVATE2);
  1221. cur_offset = extent_end;
  1222. if (cur_offset > end)
  1223. break;
  1224. }
  1225. btrfs_release_path(path);
  1226. if (cur_offset <= end && cow_start == (u64)-1) {
  1227. cow_start = cur_offset;
  1228. cur_offset = end;
  1229. }
  1230. if (cow_start != (u64)-1) {
  1231. ret = __cow_file_range(trans, inode, root, locked_page,
  1232. cow_start, end,
  1233. page_started, nr_written, 1);
  1234. if (ret) {
  1235. btrfs_abort_transaction(trans, root, ret);
  1236. goto error;
  1237. }
  1238. }
  1239. error:
  1240. err = btrfs_end_transaction(trans, root);
  1241. if (!ret)
  1242. ret = err;
  1243. if (ret && cur_offset < end)
  1244. extent_clear_unlock_delalloc(inode,
  1245. &BTRFS_I(inode)->io_tree,
  1246. cur_offset, end, locked_page,
  1247. EXTENT_CLEAR_UNLOCK_PAGE |
  1248. EXTENT_CLEAR_UNLOCK |
  1249. EXTENT_CLEAR_DELALLOC |
  1250. EXTENT_CLEAR_DIRTY |
  1251. EXTENT_SET_WRITEBACK |
  1252. EXTENT_END_WRITEBACK);
  1253. btrfs_free_path(path);
  1254. return ret;
  1255. }
  1256. /*
  1257. * extent_io.c call back to do delayed allocation processing
  1258. */
  1259. static int run_delalloc_range(struct inode *inode, struct page *locked_page,
  1260. u64 start, u64 end, int *page_started,
  1261. unsigned long *nr_written)
  1262. {
  1263. int ret;
  1264. struct btrfs_root *root = BTRFS_I(inode)->root;
  1265. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
  1266. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1267. page_started, 1, nr_written);
  1268. } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
  1269. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1270. page_started, 0, nr_written);
  1271. } else if (!btrfs_test_opt(root, COMPRESS) &&
  1272. !(BTRFS_I(inode)->force_compress) &&
  1273. !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
  1274. ret = cow_file_range(inode, locked_page, start, end,
  1275. page_started, nr_written, 1);
  1276. } else {
  1277. set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
  1278. &BTRFS_I(inode)->runtime_flags);
  1279. ret = cow_file_range_async(inode, locked_page, start, end,
  1280. page_started, nr_written);
  1281. }
  1282. return ret;
  1283. }
  1284. static void btrfs_split_extent_hook(struct inode *inode,
  1285. struct extent_state *orig, u64 split)
  1286. {
  1287. /* not delalloc, ignore it */
  1288. if (!(orig->state & EXTENT_DELALLOC))
  1289. return;
  1290. spin_lock(&BTRFS_I(inode)->lock);
  1291. BTRFS_I(inode)->outstanding_extents++;
  1292. spin_unlock(&BTRFS_I(inode)->lock);
  1293. }
  1294. /*
  1295. * extent_io.c merge_extent_hook, used to track merged delayed allocation
  1296. * extents so we can keep track of new extents that are just merged onto old
  1297. * extents, such as when we are doing sequential writes, so we can properly
  1298. * account for the metadata space we'll need.
  1299. */
  1300. static void btrfs_merge_extent_hook(struct inode *inode,
  1301. struct extent_state *new,
  1302. struct extent_state *other)
  1303. {
  1304. /* not delalloc, ignore it */
  1305. if (!(other->state & EXTENT_DELALLOC))
  1306. return;
  1307. spin_lock(&BTRFS_I(inode)->lock);
  1308. BTRFS_I(inode)->outstanding_extents--;
  1309. spin_unlock(&BTRFS_I(inode)->lock);
  1310. }
  1311. /*
  1312. * extent_io.c set_bit_hook, used to track delayed allocation
  1313. * bytes in this file, and to maintain the list of inodes that
  1314. * have pending delalloc work to be done.
  1315. */
  1316. static void btrfs_set_bit_hook(struct inode *inode,
  1317. struct extent_state *state, int *bits)
  1318. {
  1319. /*
  1320. * set_bit and clear bit hooks normally require _irqsave/restore
  1321. * but in this case, we are only testing for the DELALLOC
  1322. * bit, which is only set or cleared with irqs on
  1323. */
  1324. if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
  1325. struct btrfs_root *root = BTRFS_I(inode)->root;
  1326. u64 len = state->end + 1 - state->start;
  1327. bool do_list = !btrfs_is_free_space_inode(inode);
  1328. if (*bits & EXTENT_FIRST_DELALLOC) {
  1329. *bits &= ~EXTENT_FIRST_DELALLOC;
  1330. } else {
  1331. spin_lock(&BTRFS_I(inode)->lock);
  1332. BTRFS_I(inode)->outstanding_extents++;
  1333. spin_unlock(&BTRFS_I(inode)->lock);
  1334. }
  1335. spin_lock(&root->fs_info->delalloc_lock);
  1336. BTRFS_I(inode)->delalloc_bytes += len;
  1337. root->fs_info->delalloc_bytes += len;
  1338. if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1339. list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
  1340. &root->fs_info->delalloc_inodes);
  1341. }
  1342. spin_unlock(&root->fs_info->delalloc_lock);
  1343. }
  1344. }
  1345. /*
  1346. * extent_io.c clear_bit_hook, see set_bit_hook for why
  1347. */
  1348. static void btrfs_clear_bit_hook(struct inode *inode,
  1349. struct extent_state *state, int *bits)
  1350. {
  1351. /*
  1352. * set_bit and clear bit hooks normally require _irqsave/restore
  1353. * but in this case, we are only testing for the DELALLOC
  1354. * bit, which is only set or cleared with irqs on
  1355. */
  1356. if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
  1357. struct btrfs_root *root = BTRFS_I(inode)->root;
  1358. u64 len = state->end + 1 - state->start;
  1359. bool do_list = !btrfs_is_free_space_inode(inode);
  1360. if (*bits & EXTENT_FIRST_DELALLOC) {
  1361. *bits &= ~EXTENT_FIRST_DELALLOC;
  1362. } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
  1363. spin_lock(&BTRFS_I(inode)->lock);
  1364. BTRFS_I(inode)->outstanding_extents--;
  1365. spin_unlock(&BTRFS_I(inode)->lock);
  1366. }
  1367. if (*bits & EXTENT_DO_ACCOUNTING)
  1368. btrfs_delalloc_release_metadata(inode, len);
  1369. if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
  1370. && do_list)
  1371. btrfs_free_reserved_data_space(inode, len);
  1372. spin_lock(&root->fs_info->delalloc_lock);
  1373. root->fs_info->delalloc_bytes -= len;
  1374. BTRFS_I(inode)->delalloc_bytes -= len;
  1375. if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
  1376. !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1377. list_del_init(&BTRFS_I(inode)->delalloc_inodes);
  1378. }
  1379. spin_unlock(&root->fs_info->delalloc_lock);
  1380. }
  1381. }
  1382. /*
  1383. * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
  1384. * we don't create bios that span stripes or chunks
  1385. */
  1386. int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
  1387. size_t size, struct bio *bio,
  1388. unsigned long bio_flags)
  1389. {
  1390. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  1391. u64 logical = (u64)bio->bi_sector << 9;
  1392. u64 length = 0;
  1393. u64 map_length;
  1394. int ret;
  1395. if (bio_flags & EXTENT_BIO_COMPRESSED)
  1396. return 0;
  1397. length = bio->bi_size;
  1398. map_length = length;
  1399. ret = btrfs_map_block(root->fs_info, READ, logical,
  1400. &map_length, NULL, 0);
  1401. /* Will always return 0 with map_multi == NULL */
  1402. BUG_ON(ret < 0);
  1403. if (map_length < length + size)
  1404. return 1;
  1405. return 0;
  1406. }
  1407. /*
  1408. * in order to insert checksums into the metadata in large chunks,
  1409. * we wait until bio submission time. All the pages in the bio are
  1410. * checksummed and sums are attached onto the ordered extent record.
  1411. *
  1412. * At IO completion time the cums attached on the ordered extent record
  1413. * are inserted into the btree
  1414. */
  1415. static int __btrfs_submit_bio_start(struct inode *inode, int rw,
  1416. struct bio *bio, int mirror_num,
  1417. unsigned long bio_flags,
  1418. u64 bio_offset)
  1419. {
  1420. struct btrfs_root *root = BTRFS_I(inode)->root;
  1421. int ret = 0;
  1422. ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
  1423. BUG_ON(ret); /* -ENOMEM */
  1424. return 0;
  1425. }
  1426. /*
  1427. * in order to insert checksums into the metadata in large chunks,
  1428. * we wait until bio submission time. All the pages in the bio are
  1429. * checksummed and sums are attached onto the ordered extent record.
  1430. *
  1431. * At IO completion time the cums attached on the ordered extent record
  1432. * are inserted into the btree
  1433. */
  1434. static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
  1435. int mirror_num, unsigned long bio_flags,
  1436. u64 bio_offset)
  1437. {
  1438. struct btrfs_root *root = BTRFS_I(inode)->root;
  1439. int ret;
  1440. ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
  1441. if (ret)
  1442. bio_endio(bio, ret);
  1443. return ret;
  1444. }
  1445. /*
  1446. * extent_io.c submission hook. This does the right thing for csum calculation
  1447. * on write, or reading the csums from the tree before a read
  1448. */
  1449. static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  1450. int mirror_num, unsigned long bio_flags,
  1451. u64 bio_offset)
  1452. {
  1453. struct btrfs_root *root = BTRFS_I(inode)->root;
  1454. int ret = 0;
  1455. int skip_sum;
  1456. int metadata = 0;
  1457. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  1458. if (btrfs_is_free_space_inode(inode))
  1459. metadata = 2;
  1460. if (!(rw & REQ_WRITE)) {
  1461. ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
  1462. if (ret)
  1463. goto out;
  1464. if (bio_flags & EXTENT_BIO_COMPRESSED) {
  1465. ret = btrfs_submit_compressed_read(inode, bio,
  1466. mirror_num,
  1467. bio_flags);
  1468. goto out;
  1469. } else if (!skip_sum) {
  1470. ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
  1471. if (ret)
  1472. goto out;
  1473. }
  1474. goto mapit;
  1475. } else if (!skip_sum) {
  1476. /* csum items have already been cloned */
  1477. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  1478. goto mapit;
  1479. /* we're doing a write, do the async checksumming */
  1480. ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  1481. inode, rw, bio, mirror_num,
  1482. bio_flags, bio_offset,
  1483. __btrfs_submit_bio_start,
  1484. __btrfs_submit_bio_done);
  1485. goto out;
  1486. }
  1487. mapit:
  1488. ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
  1489. out:
  1490. if (ret < 0)
  1491. bio_endio(bio, ret);
  1492. return ret;
  1493. }
  1494. /*
  1495. * given a list of ordered sums record them in the inode. This happens
  1496. * at IO completion time based on sums calculated at bio submission time.
  1497. */
  1498. static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
  1499. struct inode *inode, u64 file_offset,
  1500. struct list_head *list)
  1501. {
  1502. struct btrfs_ordered_sum *sum;
  1503. list_for_each_entry(sum, list, list) {
  1504. btrfs_csum_file_blocks(trans,
  1505. BTRFS_I(inode)->root->fs_info->csum_root, sum);
  1506. }
  1507. return 0;
  1508. }
  1509. int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
  1510. struct extent_state **cached_state)
  1511. {
  1512. WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
  1513. return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
  1514. cached_state, GFP_NOFS);
  1515. }
  1516. /* see btrfs_writepage_start_hook for details on why this is required */
  1517. struct btrfs_writepage_fixup {
  1518. struct page *page;
  1519. struct btrfs_work work;
  1520. };
  1521. static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
  1522. {
  1523. struct btrfs_writepage_fixup *fixup;
  1524. struct btrfs_ordered_extent *ordered;
  1525. struct extent_state *cached_state = NULL;
  1526. struct page *page;
  1527. struct inode *inode;
  1528. u64 page_start;
  1529. u64 page_end;
  1530. int ret;
  1531. fixup = container_of(work, struct btrfs_writepage_fixup, work);
  1532. page = fixup->page;
  1533. again:
  1534. lock_page(page);
  1535. if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
  1536. ClearPageChecked(page);
  1537. goto out_page;
  1538. }
  1539. inode = page->mapping->host;
  1540. page_start = page_offset(page);
  1541. page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
  1542. lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
  1543. &cached_state);
  1544. /* already ordered? We're done */
  1545. if (PagePrivate2(page))
  1546. goto out;
  1547. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  1548. if (ordered) {
  1549. unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
  1550. page_end, &cached_state, GFP_NOFS);
  1551. unlock_page(page);
  1552. btrfs_start_ordered_extent(inode, ordered, 1);
  1553. btrfs_put_ordered_extent(ordered);
  1554. goto again;
  1555. }
  1556. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  1557. if (ret) {
  1558. mapping_set_error(page->mapping, ret);
  1559. end_extent_writepage(page, ret, page_start, page_end);
  1560. ClearPageChecked(page);
  1561. goto out;
  1562. }
  1563. btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
  1564. ClearPageChecked(page);
  1565. set_page_dirty(page);
  1566. out:
  1567. unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
  1568. &cached_state, GFP_NOFS);
  1569. out_page:
  1570. unlock_page(page);
  1571. page_cache_release(page);
  1572. kfree(fixup);
  1573. }
  1574. /*
  1575. * There are a few paths in the higher layers of the kernel that directly
  1576. * set the page dirty bit without asking the filesystem if it is a
  1577. * good idea. This causes problems because we want to make sure COW
  1578. * properly happens and the data=ordered rules are followed.
  1579. *
  1580. * In our case any range that doesn't have the ORDERED bit set
  1581. * hasn't been properly setup for IO. We kick off an async process
  1582. * to fix it up. The async helper will wait for ordered extents, set
  1583. * the delalloc bit and make it safe to write the page.
  1584. */
  1585. static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
  1586. {
  1587. struct inode *inode = page->mapping->host;
  1588. struct btrfs_writepage_fixup *fixup;
  1589. struct btrfs_root *root = BTRFS_I(inode)->root;
  1590. /* this page is properly in the ordered list */
  1591. if (TestClearPagePrivate2(page))
  1592. return 0;
  1593. if (PageChecked(page))
  1594. return -EAGAIN;
  1595. fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
  1596. if (!fixup)
  1597. return -EAGAIN;
  1598. SetPageChecked(page);
  1599. page_cache_get(page);
  1600. fixup->work.func = btrfs_writepage_fixup_worker;
  1601. fixup->page = page;
  1602. btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
  1603. return -EBUSY;
  1604. }
  1605. static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
  1606. struct inode *inode, u64 file_pos,
  1607. u64 disk_bytenr, u64 disk_num_bytes,
  1608. u64 num_bytes, u64 ram_bytes,
  1609. u8 compression, u8 encryption,
  1610. u16 other_encoding, int extent_type)
  1611. {
  1612. struct btrfs_root *root = BTRFS_I(inode)->root;
  1613. struct btrfs_file_extent_item *fi;
  1614. struct btrfs_path *path;
  1615. struct extent_buffer *leaf;
  1616. struct btrfs_key ins;
  1617. int ret;
  1618. path = btrfs_alloc_path();
  1619. if (!path)
  1620. return -ENOMEM;
  1621. path->leave_spinning = 1;
  1622. /*
  1623. * we may be replacing one extent in the tree with another.
  1624. * The new extent is pinned in the extent map, and we don't want
  1625. * to drop it from the cache until it is completely in the btree.
  1626. *
  1627. * So, tell btrfs_drop_extents to leave this extent in the cache.
  1628. * the caller is expected to unpin it and allow it to be merged
  1629. * with the others.
  1630. */
  1631. ret = btrfs_drop_extents(trans, root, inode, file_pos,
  1632. file_pos + num_bytes, 0);
  1633. if (ret)
  1634. goto out;
  1635. ins.objectid = btrfs_ino(inode);
  1636. ins.offset = file_pos;
  1637. ins.type = BTRFS_EXTENT_DATA_KEY;
  1638. ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
  1639. if (ret)
  1640. goto out;
  1641. leaf = path->nodes[0];
  1642. fi = btrfs_item_ptr(leaf, path->slots[0],
  1643. struct btrfs_file_extent_item);
  1644. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1645. btrfs_set_file_extent_type(leaf, fi, extent_type);
  1646. btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
  1647. btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
  1648. btrfs_set_file_extent_offset(leaf, fi, 0);
  1649. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1650. btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
  1651. btrfs_set_file_extent_compression(leaf, fi, compression);
  1652. btrfs_set_file_extent_encryption(leaf, fi, encryption);
  1653. btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
  1654. btrfs_mark_buffer_dirty(leaf);
  1655. btrfs_release_path(path);
  1656. inode_add_bytes(inode, num_bytes);
  1657. ins.objectid = disk_bytenr;
  1658. ins.offset = disk_num_bytes;
  1659. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1660. ret = btrfs_alloc_reserved_file_extent(trans, root,
  1661. root->root_key.objectid,
  1662. btrfs_ino(inode), file_pos, &ins);
  1663. out:
  1664. btrfs_free_path(path);
  1665. return ret;
  1666. }
  1667. /*
  1668. * helper function for btrfs_finish_ordered_io, this
  1669. * just reads in some of the csum leaves to prime them into ram
  1670. * before we start the transaction. It limits the amount of btree
  1671. * reads required while inside the transaction.
  1672. */
  1673. /* as ordered data IO finishes, this gets called so we can finish
  1674. * an ordered extent if the range of bytes in the file it covers are
  1675. * fully written.
  1676. */
  1677. static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
  1678. {
  1679. struct inode *inode = ordered_extent->inode;
  1680. struct btrfs_root *root = BTRFS_I(inode)->root;
  1681. struct btrfs_trans_handle *trans = NULL;
  1682. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1683. struct extent_state *cached_state = NULL;
  1684. int compress_type = 0;
  1685. int ret;
  1686. bool nolock;
  1687. nolock = btrfs_is_free_space_inode(inode);
  1688. if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
  1689. ret = -EIO;
  1690. goto out;
  1691. }
  1692. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
  1693. BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
  1694. ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  1695. if (!ret) {
  1696. if (nolock)
  1697. trans = btrfs_join_transaction_nolock(root);
  1698. else
  1699. trans = btrfs_join_transaction(root);
  1700. if (IS_ERR(trans)) {
  1701. ret = PTR_ERR(trans);
  1702. trans = NULL;
  1703. goto out;
  1704. }
  1705. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  1706. ret = btrfs_update_inode_fallback(trans, root, inode);
  1707. if (ret) /* -ENOMEM or corruption */
  1708. btrfs_abort_transaction(trans, root, ret);
  1709. }
  1710. goto out;
  1711. }
  1712. lock_extent_bits(io_tree, ordered_extent->file_offset,
  1713. ordered_extent->file_offset + ordered_extent->len - 1,
  1714. 0, &cached_state);
  1715. if (nolock)
  1716. trans = btrfs_join_transaction_nolock(root);
  1717. else
  1718. trans = btrfs_join_transaction(root);
  1719. if (IS_ERR(trans)) {
  1720. ret = PTR_ERR(trans);
  1721. trans = NULL;
  1722. goto out_unlock;
  1723. }
  1724. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  1725. if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
  1726. compress_type = ordered_extent->compress_type;
  1727. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
  1728. BUG_ON(compress_type);
  1729. ret = btrfs_mark_extent_written(trans, inode,
  1730. ordered_extent->file_offset,
  1731. ordered_extent->file_offset +
  1732. ordered_extent->len);
  1733. } else {
  1734. BUG_ON(root == root->fs_info->tree_root);
  1735. ret = insert_reserved_file_extent(trans, inode,
  1736. ordered_extent->file_offset,
  1737. ordered_extent->start,
  1738. ordered_extent->disk_len,
  1739. ordered_extent->len,
  1740. ordered_extent->len,
  1741. compress_type, 0, 0,
  1742. BTRFS_FILE_EXTENT_REG);
  1743. }
  1744. unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
  1745. ordered_extent->file_offset, ordered_extent->len,
  1746. trans->transid);
  1747. if (ret < 0) {
  1748. btrfs_abort_transaction(trans, root, ret);
  1749. goto out_unlock;
  1750. }
  1751. add_pending_csums(trans, inode, ordered_extent->file_offset,
  1752. &ordered_extent->list);
  1753. ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
  1754. if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
  1755. ret = btrfs_update_inode_fallback(trans, root, inode);
  1756. if (ret) { /* -ENOMEM or corruption */
  1757. btrfs_abort_transaction(trans, root, ret);
  1758. goto out_unlock;
  1759. }
  1760. } else {
  1761. btrfs_set_inode_last_trans(trans, inode);
  1762. }
  1763. ret = 0;
  1764. out_unlock:
  1765. unlock_extent_cached(io_tree, ordered_extent->file_offset,
  1766. ordered_extent->file_offset +
  1767. ordered_extent->len - 1, &cached_state, GFP_NOFS);
  1768. out:
  1769. if (root != root->fs_info->tree_root)
  1770. btrfs_delalloc_release_metadata(inode, ordered_extent->len);
  1771. if (trans)
  1772. btrfs_end_transaction(trans, root);
  1773. if (ret)
  1774. clear_extent_uptodate(io_tree, ordered_extent->file_offset,
  1775. ordered_extent->file_offset +
  1776. ordered_extent->len - 1, NULL, GFP_NOFS);
  1777. /*
  1778. * This needs to be done to make sure anybody waiting knows we are done
  1779. * updating everything for this ordered extent.
  1780. */
  1781. btrfs_remove_ordered_extent(inode, ordered_extent);
  1782. /* once for us */
  1783. btrfs_put_ordered_extent(ordered_extent);
  1784. /* once for the tree */
  1785. btrfs_put_ordered_extent(ordered_extent);
  1786. return ret;
  1787. }
  1788. static void finish_ordered_fn(struct btrfs_work *work)
  1789. {
  1790. struct btrfs_ordered_extent *ordered_extent;
  1791. ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
  1792. btrfs_finish_ordered_io(ordered_extent);
  1793. }
  1794. static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
  1795. struct extent_state *state, int uptodate)
  1796. {
  1797. struct inode *inode = page->mapping->host;
  1798. struct btrfs_root *root = BTRFS_I(inode)->root;
  1799. struct btrfs_ordered_extent *ordered_extent = NULL;
  1800. struct btrfs_workers *workers;
  1801. trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
  1802. ClearPagePrivate2(page);
  1803. if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
  1804. end - start + 1, uptodate))
  1805. return 0;
  1806. ordered_extent->work.func = finish_ordered_fn;
  1807. ordered_extent->work.flags = 0;
  1808. if (btrfs_is_free_space_inode(inode))
  1809. workers = &root->fs_info->endio_freespace_worker;
  1810. else
  1811. workers = &root->fs_info->endio_write_workers;
  1812. btrfs_queue_worker(workers, &ordered_extent->work);
  1813. return 0;
  1814. }
  1815. /*
  1816. * when reads are done, we need to check csums to verify the data is correct
  1817. * if there's a match, we allow the bio to finish. If not, the code in
  1818. * extent_io.c will try to find good copies for us.
  1819. */
  1820. static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  1821. struct extent_state *state, int mirror)
  1822. {
  1823. size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
  1824. struct inode *inode = page->mapping->host;
  1825. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1826. char *kaddr;
  1827. u64 private = ~(u32)0;
  1828. int ret;
  1829. struct btrfs_root *root = BTRFS_I(inode)->root;
  1830. u32 csum = ~(u32)0;
  1831. if (PageChecked(page)) {
  1832. ClearPageChecked(page);
  1833. goto good;
  1834. }
  1835. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  1836. goto good;
  1837. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
  1838. test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
  1839. clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
  1840. GFP_NOFS);
  1841. return 0;
  1842. }
  1843. if (state && state->start == start) {
  1844. private = state->private;
  1845. ret = 0;
  1846. } else {
  1847. ret = get_state_private(io_tree, start, &private);
  1848. }
  1849. kaddr = kmap_atomic(page);
  1850. if (ret)
  1851. goto zeroit;
  1852. csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
  1853. btrfs_csum_final(csum, (char *)&csum);
  1854. if (csum != private)
  1855. goto zeroit;
  1856. kunmap_atomic(kaddr);
  1857. good:
  1858. return 0;
  1859. zeroit:
  1860. printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
  1861. "private %llu\n",
  1862. (unsigned long long)btrfs_ino(page->mapping->host),
  1863. (unsigned long long)start, csum,
  1864. (unsigned long long)private);
  1865. memset(kaddr + offset, 1, end - start + 1);
  1866. flush_dcache_page(page);
  1867. kunmap_atomic(kaddr);
  1868. if (private == 0)
  1869. return 0;
  1870. return -EIO;
  1871. }
  1872. struct delayed_iput {
  1873. struct list_head list;
  1874. struct inode *inode;
  1875. };
  1876. /* JDM: If this is fs-wide, why can't we add a pointer to
  1877. * btrfs_inode instead and avoid the allocation? */
  1878. void btrfs_add_delayed_iput(struct inode *inode)
  1879. {
  1880. struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
  1881. struct delayed_iput *delayed;
  1882. if (atomic_add_unless(&inode->i_count, -1, 1))
  1883. return;
  1884. delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
  1885. delayed->inode = inode;
  1886. spin_lock(&fs_info->delayed_iput_lock);
  1887. list_add_tail(&delayed->list, &fs_info->delayed_iputs);
  1888. spin_unlock(&fs_info->delayed_iput_lock);
  1889. }
  1890. void btrfs_run_delayed_iputs(struct btrfs_root *root)
  1891. {
  1892. LIST_HEAD(list);
  1893. struct btrfs_fs_info *fs_info = root->fs_info;
  1894. struct delayed_iput *delayed;
  1895. int empty;
  1896. spin_lock(&fs_info->delayed_iput_lock);
  1897. empty = list_empty(&fs_info->delayed_iputs);
  1898. spin_unlock(&fs_info->delayed_iput_lock);
  1899. if (empty)
  1900. return;
  1901. spin_lock(&fs_info->delayed_iput_lock);
  1902. list_splice_init(&fs_info->delayed_iputs, &list);
  1903. spin_unlock(&fs_info->delayed_iput_lock);
  1904. while (!list_empty(&list)) {
  1905. delayed = list_entry(list.next, struct delayed_iput, list);
  1906. list_del(&delayed->list);
  1907. iput(delayed->inode);
  1908. kfree(delayed);
  1909. }
  1910. }
  1911. enum btrfs_orphan_cleanup_state {
  1912. ORPHAN_CLEANUP_STARTED = 1,
  1913. ORPHAN_CLEANUP_DONE = 2,
  1914. };
  1915. /*
  1916. * This is called in transaction commit time. If there are no orphan
  1917. * files in the subvolume, it removes orphan item and frees block_rsv
  1918. * structure.
  1919. */
  1920. void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
  1921. struct btrfs_root *root)
  1922. {
  1923. struct btrfs_block_rsv *block_rsv;
  1924. int ret;
  1925. if (atomic_read(&root->orphan_inodes) ||
  1926. root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
  1927. return;
  1928. spin_lock(&root->orphan_lock);
  1929. if (atomic_read(&root->orphan_inodes)) {
  1930. spin_unlock(&root->orphan_lock);
  1931. return;
  1932. }
  1933. if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
  1934. spin_unlock(&root->orphan_lock);
  1935. return;
  1936. }
  1937. block_rsv = root->orphan_block_rsv;
  1938. root->orphan_block_rsv = NULL;
  1939. spin_unlock(&root->orphan_lock);
  1940. if (root->orphan_item_inserted &&
  1941. btrfs_root_refs(&root->root_item) > 0) {
  1942. ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
  1943. root->root_key.objectid);
  1944. BUG_ON(ret);
  1945. root->orphan_item_inserted = 0;
  1946. }
  1947. if (block_rsv) {
  1948. WARN_ON(block_rsv->size > 0);
  1949. btrfs_free_block_rsv(root, block_rsv);
  1950. }
  1951. }
  1952. /*
  1953. * This creates an orphan entry for the given inode in case something goes
  1954. * wrong in the middle of an unlink/truncate.
  1955. *
  1956. * NOTE: caller of this function should reserve 5 units of metadata for
  1957. * this function.
  1958. */
  1959. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  1960. {
  1961. struct btrfs_root *root = BTRFS_I(inode)->root;
  1962. struct btrfs_block_rsv *block_rsv = NULL;
  1963. int reserve = 0;
  1964. int insert = 0;
  1965. int ret;
  1966. if (!root->orphan_block_rsv) {
  1967. block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
  1968. if (!block_rsv)
  1969. return -ENOMEM;
  1970. }
  1971. spin_lock(&root->orphan_lock);
  1972. if (!root->orphan_block_rsv) {
  1973. root->orphan_block_rsv = block_rsv;
  1974. } else if (block_rsv) {
  1975. btrfs_free_block_rsv(root, block_rsv);
  1976. block_rsv = NULL;
  1977. }
  1978. if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  1979. &BTRFS_I(inode)->runtime_flags)) {
  1980. #if 0
  1981. /*
  1982. * For proper ENOSPC handling, we should do orphan
  1983. * cleanup when mounting. But this introduces backward
  1984. * compatibility issue.
  1985. */
  1986. if (!xchg(&root->orphan_item_inserted, 1))
  1987. insert = 2;
  1988. else
  1989. insert = 1;
  1990. #endif
  1991. insert = 1;
  1992. atomic_inc(&root->orphan_inodes);
  1993. }
  1994. if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
  1995. &BTRFS_I(inode)->runtime_flags))
  1996. reserve = 1;
  1997. spin_unlock(&root->orphan_lock);
  1998. /* grab metadata reservation from transaction handle */
  1999. if (reserve) {
  2000. ret = btrfs_orphan_reserve_metadata(trans, inode);
  2001. BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
  2002. }
  2003. /* insert an orphan item to track this unlinked/truncated file */
  2004. if (insert >= 1) {
  2005. ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
  2006. if (ret && ret != -EEXIST) {
  2007. clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2008. &BTRFS_I(inode)->runtime_flags);
  2009. btrfs_abort_transaction(trans, root, ret);
  2010. return ret;
  2011. }
  2012. ret = 0;
  2013. }
  2014. /* insert an orphan item to track subvolume contains orphan files */
  2015. if (insert >= 2) {
  2016. ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
  2017. root->root_key.objectid);
  2018. if (ret && ret != -EEXIST) {
  2019. btrfs_abort_transaction(trans, root, ret);
  2020. return ret;
  2021. }
  2022. }
  2023. return 0;
  2024. }
  2025. /*
  2026. * We have done the truncate/delete so we can go ahead and remove the orphan
  2027. * item for this particular inode.
  2028. */
  2029. int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
  2030. {
  2031. struct btrfs_root *root = BTRFS_I(inode)->root;
  2032. int delete_item = 0;
  2033. int release_rsv = 0;
  2034. int ret = 0;
  2035. spin_lock(&root->orphan_lock);
  2036. if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2037. &BTRFS_I(inode)->runtime_flags))
  2038. delete_item = 1;
  2039. if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
  2040. &BTRFS_I(inode)->runtime_flags))
  2041. release_rsv = 1;
  2042. spin_unlock(&root->orphan_lock);
  2043. if (trans && delete_item) {
  2044. ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
  2045. BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
  2046. }
  2047. if (release_rsv) {
  2048. btrfs_orphan_release_metadata(inode);
  2049. atomic_dec(&root->orphan_inodes);
  2050. }
  2051. return 0;
  2052. }
  2053. /*
  2054. * this cleans up any orphans that may be left on the list from the last use
  2055. * of this root.
  2056. */
  2057. int btrfs_orphan_cleanup(struct btrfs_root *root)
  2058. {
  2059. struct btrfs_path *path;
  2060. struct extent_buffer *leaf;
  2061. struct btrfs_key key, found_key;
  2062. struct btrfs_trans_handle *trans;
  2063. struct inode *inode;
  2064. u64 last_objectid = 0;
  2065. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  2066. if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
  2067. return 0;
  2068. path = btrfs_alloc_path();
  2069. if (!path) {
  2070. ret = -ENOMEM;
  2071. goto out;
  2072. }
  2073. path->reada = -1;
  2074. key.objectid = BTRFS_ORPHAN_OBJECTID;
  2075. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  2076. key.offset = (u64)-1;
  2077. while (1) {
  2078. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2079. if (ret < 0)
  2080. goto out;
  2081. /*
  2082. * if ret == 0 means we found what we were searching for, which
  2083. * is weird, but possible, so only screw with path if we didn't
  2084. * find the key and see if we have stuff that matches
  2085. */
  2086. if (ret > 0) {
  2087. ret = 0;
  2088. if (path->slots[0] == 0)
  2089. break;
  2090. path->slots[0]--;
  2091. }
  2092. /* pull out the item */
  2093. leaf = path->nodes[0];
  2094. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2095. /* make sure the item matches what we want */
  2096. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  2097. break;
  2098. if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
  2099. break;
  2100. /* release the path since we're done with it */
  2101. btrfs_release_path(path);
  2102. /*
  2103. * this is where we are basically btrfs_lookup, without the
  2104. * crossing root thing. we store the inode number in the
  2105. * offset of the orphan item.
  2106. */
  2107. if (found_key.offset == last_objectid) {
  2108. printk(KERN_ERR "btrfs: Error removing orphan entry, "
  2109. "stopping orphan cleanup\n");
  2110. ret = -EINVAL;
  2111. goto out;
  2112. }
  2113. last_objectid = found_key.offset;
  2114. found_key.objectid = found_key.offset;
  2115. found_key.type = BTRFS_INODE_ITEM_KEY;
  2116. found_key.offset = 0;
  2117. inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
  2118. ret = PTR_RET(inode);
  2119. if (ret && ret != -ESTALE)
  2120. goto out;
  2121. if (ret == -ESTALE && root == root->fs_info->tree_root) {
  2122. struct btrfs_root *dead_root;
  2123. struct btrfs_fs_info *fs_info = root->fs_info;
  2124. int is_dead_root = 0;
  2125. /*
  2126. * this is an orphan in the tree root. Currently these
  2127. * could come from 2 sources:
  2128. * a) a snapshot deletion in progress
  2129. * b) a free space cache inode
  2130. * We need to distinguish those two, as the snapshot
  2131. * orphan must not get deleted.
  2132. * find_dead_roots already ran before us, so if this
  2133. * is a snapshot deletion, we should find the root
  2134. * in the dead_roots list
  2135. */
  2136. spin_lock(&fs_info->trans_lock);
  2137. list_for_each_entry(dead_root, &fs_info->dead_roots,
  2138. root_list) {
  2139. if (dead_root->root_key.objectid ==
  2140. found_key.objectid) {
  2141. is_dead_root = 1;
  2142. break;
  2143. }
  2144. }
  2145. spin_unlock(&fs_info->trans_lock);
  2146. if (is_dead_root) {
  2147. /* prevent this orphan from being found again */
  2148. key.offset = found_key.objectid - 1;
  2149. continue;
  2150. }
  2151. }
  2152. /*
  2153. * Inode is already gone but the orphan item is still there,
  2154. * kill the orphan item.
  2155. */
  2156. if (ret == -ESTALE) {
  2157. trans = btrfs_start_transaction(root, 1);
  2158. if (IS_ERR(trans)) {
  2159. ret = PTR_ERR(trans);
  2160. goto out;
  2161. }
  2162. printk(KERN_ERR "auto deleting %Lu\n",
  2163. found_key.objectid);
  2164. ret = btrfs_del_orphan_item(trans, root,
  2165. found_key.objectid);
  2166. BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
  2167. btrfs_end_transaction(trans, root);
  2168. continue;
  2169. }
  2170. /*
  2171. * add this inode to the orphan list so btrfs_orphan_del does
  2172. * the proper thing when we hit it
  2173. */
  2174. set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  2175. &BTRFS_I(inode)->runtime_flags);
  2176. /* if we have links, this was a truncate, lets do that */
  2177. if (inode->i_nlink) {
  2178. if (!S_ISREG(inode->i_mode)) {
  2179. WARN_ON(1);
  2180. iput(inode);
  2181. continue;
  2182. }
  2183. nr_truncate++;
  2184. ret = btrfs_truncate(inode);
  2185. } else {
  2186. nr_unlink++;
  2187. }
  2188. /* this will do delete_inode and everything for us */
  2189. iput(inode);
  2190. if (ret)
  2191. goto out;
  2192. }
  2193. /* release the path since we're done with it */
  2194. btrfs_release_path(path);
  2195. root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
  2196. if (root->orphan_block_rsv)
  2197. btrfs_block_rsv_release(root, root->orphan_block_rsv,
  2198. (u64)-1);
  2199. if (root->orphan_block_rsv || root->orphan_item_inserted) {
  2200. trans = btrfs_join_transaction(root);
  2201. if (!IS_ERR(trans))
  2202. btrfs_end_transaction(trans, root);
  2203. }
  2204. if (nr_unlink)
  2205. printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
  2206. if (nr_truncate)
  2207. printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
  2208. out:
  2209. if (ret)
  2210. printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
  2211. btrfs_free_path(path);
  2212. return ret;
  2213. }
  2214. /*
  2215. * very simple check to peek ahead in the leaf looking for xattrs. If we
  2216. * don't find any xattrs, we know there can't be any acls.
  2217. *
  2218. * slot is the slot the inode is in, objectid is the objectid of the inode
  2219. */
  2220. static noinline int acls_after_inode_item(struct extent_buffer *leaf,
  2221. int slot, u64 objectid)
  2222. {
  2223. u32 nritems = btrfs_header_nritems(leaf);
  2224. struct btrfs_key found_key;
  2225. int scanned = 0;
  2226. slot++;
  2227. while (slot < nritems) {
  2228. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2229. /* we found a different objectid, there must not be acls */
  2230. if (found_key.objectid != objectid)
  2231. return 0;
  2232. /* we found an xattr, assume we've got an acl */
  2233. if (found_key.type == BTRFS_XATTR_ITEM_KEY)
  2234. return 1;
  2235. /*
  2236. * we found a key greater than an xattr key, there can't
  2237. * be any acls later on
  2238. */
  2239. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  2240. return 0;
  2241. slot++;
  2242. scanned++;
  2243. /*
  2244. * it goes inode, inode backrefs, xattrs, extents,
  2245. * so if there are a ton of hard links to an inode there can
  2246. * be a lot of backrefs. Don't waste time searching too hard,
  2247. * this is just an optimization
  2248. */
  2249. if (scanned >= 8)
  2250. break;
  2251. }
  2252. /* we hit the end of the leaf before we found an xattr or
  2253. * something larger than an xattr. We have to assume the inode
  2254. * has acls
  2255. */
  2256. return 1;
  2257. }
  2258. /*
  2259. * read an inode from the btree into the in-memory inode
  2260. */
  2261. static void btrfs_read_locked_inode(struct inode *inode)
  2262. {
  2263. struct btrfs_path *path;
  2264. struct extent_buffer *leaf;
  2265. struct btrfs_inode_item *inode_item;
  2266. struct btrfs_timespec *tspec;
  2267. struct btrfs_root *root = BTRFS_I(inode)->root;
  2268. struct btrfs_key location;
  2269. int maybe_acls;
  2270. u32 rdev;
  2271. int ret;
  2272. bool filled = false;
  2273. ret = btrfs_fill_inode(inode, &rdev);
  2274. if (!ret)
  2275. filled = true;
  2276. path = btrfs_alloc_path();
  2277. if (!path)
  2278. goto make_bad;
  2279. path->leave_spinning = 1;
  2280. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  2281. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  2282. if (ret)
  2283. goto make_bad;
  2284. leaf = path->nodes[0];
  2285. if (filled)
  2286. goto cache_acl;
  2287. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  2288. struct btrfs_inode_item);
  2289. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  2290. set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
  2291. i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
  2292. i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
  2293. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  2294. tspec = btrfs_inode_atime(inode_item);
  2295. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2296. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2297. tspec = btrfs_inode_mtime(inode_item);
  2298. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2299. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2300. tspec = btrfs_inode_ctime(inode_item);
  2301. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  2302. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  2303. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  2304. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  2305. BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
  2306. /*
  2307. * If we were modified in the current generation and evicted from memory
  2308. * and then re-read we need to do a full sync since we don't have any
  2309. * idea about which extents were modified before we were evicted from
  2310. * cache.
  2311. */
  2312. if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
  2313. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  2314. &BTRFS_I(inode)->runtime_flags);
  2315. inode->i_version = btrfs_inode_sequence(leaf, inode_item);
  2316. inode->i_generation = BTRFS_I(inode)->generation;
  2317. inode->i_rdev = 0;
  2318. rdev = btrfs_inode_rdev(leaf, inode_item);
  2319. BTRFS_I(inode)->index_cnt = (u64)-1;
  2320. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  2321. cache_acl:
  2322. /*
  2323. * try to precache a NULL acl entry for files that don't have
  2324. * any xattrs or acls
  2325. */
  2326. maybe_acls = acls_after_inode_item(leaf, path->slots[0],
  2327. btrfs_ino(inode));
  2328. if (!maybe_acls)
  2329. cache_no_acl(inode);
  2330. btrfs_free_path(path);
  2331. switch (inode->i_mode & S_IFMT) {
  2332. case S_IFREG:
  2333. inode->i_mapping->a_ops = &btrfs_aops;
  2334. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2335. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  2336. inode->i_fop = &btrfs_file_operations;
  2337. inode->i_op = &btrfs_file_inode_operations;
  2338. break;
  2339. case S_IFDIR:
  2340. inode->i_fop = &btrfs_dir_file_operations;
  2341. if (root == root->fs_info->tree_root)
  2342. inode->i_op = &btrfs_dir_ro_inode_operations;
  2343. else
  2344. inode->i_op = &btrfs_dir_inode_operations;
  2345. break;
  2346. case S_IFLNK:
  2347. inode->i_op = &btrfs_symlink_inode_operations;
  2348. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  2349. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2350. break;
  2351. default:
  2352. inode->i_op = &btrfs_special_inode_operations;
  2353. init_special_inode(inode, inode->i_mode, rdev);
  2354. break;
  2355. }
  2356. btrfs_update_iflags(inode);
  2357. return;
  2358. make_bad:
  2359. btrfs_free_path(path);
  2360. make_bad_inode(inode);
  2361. }
  2362. /*
  2363. * given a leaf and an inode, copy the inode fields into the leaf
  2364. */
  2365. static void fill_inode_item(struct btrfs_trans_handle *trans,
  2366. struct extent_buffer *leaf,
  2367. struct btrfs_inode_item *item,
  2368. struct inode *inode)
  2369. {
  2370. btrfs_set_inode_uid(leaf, item, i_uid_read(inode));
  2371. btrfs_set_inode_gid(leaf, item, i_gid_read(inode));
  2372. btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
  2373. btrfs_set_inode_mode(leaf, item, inode->i_mode);
  2374. btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
  2375. btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
  2376. inode->i_atime.tv_sec);
  2377. btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
  2378. inode->i_atime.tv_nsec);
  2379. btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
  2380. inode->i_mtime.tv_sec);
  2381. btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
  2382. inode->i_mtime.tv_nsec);
  2383. btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
  2384. inode->i_ctime.tv_sec);
  2385. btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
  2386. inode->i_ctime.tv_nsec);
  2387. btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
  2388. btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
  2389. btrfs_set_inode_sequence(leaf, item, inode->i_version);
  2390. btrfs_set_inode_transid(leaf, item, trans->transid);
  2391. btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
  2392. btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
  2393. btrfs_set_inode_block_group(leaf, item, 0);
  2394. }
  2395. /*
  2396. * copy everything in the in-memory inode into the btree.
  2397. */
  2398. static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
  2399. struct btrfs_root *root, struct inode *inode)
  2400. {
  2401. struct btrfs_inode_item *inode_item;
  2402. struct btrfs_path *path;
  2403. struct extent_buffer *leaf;
  2404. int ret;
  2405. path = btrfs_alloc_path();
  2406. if (!path)
  2407. return -ENOMEM;
  2408. path->leave_spinning = 1;
  2409. ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
  2410. 1);
  2411. if (ret) {
  2412. if (ret > 0)
  2413. ret = -ENOENT;
  2414. goto failed;
  2415. }
  2416. btrfs_unlock_up_safe(path, 1);
  2417. leaf = path->nodes[0];
  2418. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  2419. struct btrfs_inode_item);
  2420. fill_inode_item(trans, leaf, inode_item, inode);
  2421. btrfs_mark_buffer_dirty(leaf);
  2422. btrfs_set_inode_last_trans(trans, inode);
  2423. ret = 0;
  2424. failed:
  2425. btrfs_free_path(path);
  2426. return ret;
  2427. }
  2428. /*
  2429. * copy everything in the in-memory inode into the btree.
  2430. */
  2431. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  2432. struct btrfs_root *root, struct inode *inode)
  2433. {
  2434. int ret;
  2435. /*
  2436. * If the inode is a free space inode, we can deadlock during commit
  2437. * if we put it into the delayed code.
  2438. *
  2439. * The data relocation inode should also be directly updated
  2440. * without delay
  2441. */
  2442. if (!btrfs_is_free_space_inode(inode)
  2443. && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
  2444. btrfs_update_root_times(trans, root);
  2445. ret = btrfs_delayed_update_inode(trans, root, inode);
  2446. if (!ret)
  2447. btrfs_set_inode_last_trans(trans, inode);
  2448. return ret;
  2449. }
  2450. return btrfs_update_inode_item(trans, root, inode);
  2451. }
  2452. noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
  2453. struct btrfs_root *root,
  2454. struct inode *inode)
  2455. {
  2456. int ret;
  2457. ret = btrfs_update_inode(trans, root, inode);
  2458. if (ret == -ENOSPC)
  2459. return btrfs_update_inode_item(trans, root, inode);
  2460. return ret;
  2461. }
  2462. /*
  2463. * unlink helper that gets used here in inode.c and in the tree logging
  2464. * recovery code. It remove a link in a directory with a given name, and
  2465. * also drops the back refs in the inode to the directory
  2466. */
  2467. static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  2468. struct btrfs_root *root,
  2469. struct inode *dir, struct inode *inode,
  2470. const char *name, int name_len)
  2471. {
  2472. struct btrfs_path *path;
  2473. int ret = 0;
  2474. struct extent_buffer *leaf;
  2475. struct btrfs_dir_item *di;
  2476. struct btrfs_key key;
  2477. u64 index;
  2478. u64 ino = btrfs_ino(inode);
  2479. u64 dir_ino = btrfs_ino(dir);
  2480. path = btrfs_alloc_path();
  2481. if (!path) {
  2482. ret = -ENOMEM;
  2483. goto out;
  2484. }
  2485. path->leave_spinning = 1;
  2486. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  2487. name, name_len, -1);
  2488. if (IS_ERR(di)) {
  2489. ret = PTR_ERR(di);
  2490. goto err;
  2491. }
  2492. if (!di) {
  2493. ret = -ENOENT;
  2494. goto err;
  2495. }
  2496. leaf = path->nodes[0];
  2497. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2498. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2499. if (ret)
  2500. goto err;
  2501. btrfs_release_path(path);
  2502. ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
  2503. dir_ino, &index);
  2504. if (ret) {
  2505. printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
  2506. "inode %llu parent %llu\n", name_len, name,
  2507. (unsigned long long)ino, (unsigned long long)dir_ino);
  2508. btrfs_abort_transaction(trans, root, ret);
  2509. goto err;
  2510. }
  2511. ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
  2512. if (ret) {
  2513. btrfs_abort_transaction(trans, root, ret);
  2514. goto err;
  2515. }
  2516. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  2517. inode, dir_ino);
  2518. if (ret != 0 && ret != -ENOENT) {
  2519. btrfs_abort_transaction(trans, root, ret);
  2520. goto err;
  2521. }
  2522. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  2523. dir, index);
  2524. if (ret == -ENOENT)
  2525. ret = 0;
  2526. err:
  2527. btrfs_free_path(path);
  2528. if (ret)
  2529. goto out;
  2530. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2531. inode_inc_iversion(inode);
  2532. inode_inc_iversion(dir);
  2533. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2534. ret = btrfs_update_inode(trans, root, dir);
  2535. out:
  2536. return ret;
  2537. }
  2538. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  2539. struct btrfs_root *root,
  2540. struct inode *dir, struct inode *inode,
  2541. const char *name, int name_len)
  2542. {
  2543. int ret;
  2544. ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
  2545. if (!ret) {
  2546. btrfs_drop_nlink(inode);
  2547. ret = btrfs_update_inode(trans, root, inode);
  2548. }
  2549. return ret;
  2550. }
  2551. /* helper to check if there is any shared block in the path */
  2552. static int check_path_shared(struct btrfs_root *root,
  2553. struct btrfs_path *path)
  2554. {
  2555. struct extent_buffer *eb;
  2556. int level;
  2557. u64 refs = 1;
  2558. for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
  2559. int ret;
  2560. if (!path->nodes[level])
  2561. break;
  2562. eb = path->nodes[level];
  2563. if (!btrfs_block_can_be_shared(root, eb))
  2564. continue;
  2565. ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
  2566. &refs, NULL);
  2567. if (refs > 1)
  2568. return 1;
  2569. }
  2570. return 0;
  2571. }
  2572. /*
  2573. * helper to start transaction for unlink and rmdir.
  2574. *
  2575. * unlink and rmdir are special in btrfs, they do not always free space.
  2576. * so in enospc case, we should make sure they will free space before
  2577. * allowing them to use the global metadata reservation.
  2578. */
  2579. static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
  2580. struct dentry *dentry)
  2581. {
  2582. struct btrfs_trans_handle *trans;
  2583. struct btrfs_root *root = BTRFS_I(dir)->root;
  2584. struct btrfs_path *path;
  2585. struct btrfs_dir_item *di;
  2586. struct inode *inode = dentry->d_inode;
  2587. u64 index;
  2588. int check_link = 1;
  2589. int err = -ENOSPC;
  2590. int ret;
  2591. u64 ino = btrfs_ino(inode);
  2592. u64 dir_ino = btrfs_ino(dir);
  2593. /*
  2594. * 1 for the possible orphan item
  2595. * 1 for the dir item
  2596. * 1 for the dir index
  2597. * 1 for the inode ref
  2598. * 1 for the inode ref in the tree log
  2599. * 2 for the dir entries in the log
  2600. * 1 for the inode
  2601. */
  2602. trans = btrfs_start_transaction(root, 8);
  2603. if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
  2604. return trans;
  2605. if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  2606. return ERR_PTR(-ENOSPC);
  2607. /* check if there is someone else holds reference */
  2608. if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
  2609. return ERR_PTR(-ENOSPC);
  2610. if (atomic_read(&inode->i_count) > 2)
  2611. return ERR_PTR(-ENOSPC);
  2612. if (xchg(&root->fs_info->enospc_unlink, 1))
  2613. return ERR_PTR(-ENOSPC);
  2614. path = btrfs_alloc_path();
  2615. if (!path) {
  2616. root->fs_info->enospc_unlink = 0;
  2617. return ERR_PTR(-ENOMEM);
  2618. }
  2619. /* 1 for the orphan item */
  2620. trans = btrfs_start_transaction(root, 1);
  2621. if (IS_ERR(trans)) {
  2622. btrfs_free_path(path);
  2623. root->fs_info->enospc_unlink = 0;
  2624. return trans;
  2625. }
  2626. path->skip_locking = 1;
  2627. path->search_commit_root = 1;
  2628. ret = btrfs_lookup_inode(trans, root, path,
  2629. &BTRFS_I(dir)->location, 0);
  2630. if (ret < 0) {
  2631. err = ret;
  2632. goto out;
  2633. }
  2634. if (ret == 0) {
  2635. if (check_path_shared(root, path))
  2636. goto out;
  2637. } else {
  2638. check_link = 0;
  2639. }
  2640. btrfs_release_path(path);
  2641. ret = btrfs_lookup_inode(trans, root, path,
  2642. &BTRFS_I(inode)->location, 0);
  2643. if (ret < 0) {
  2644. err = ret;
  2645. goto out;
  2646. }
  2647. if (ret == 0) {
  2648. if (check_path_shared(root, path))
  2649. goto out;
  2650. } else {
  2651. check_link = 0;
  2652. }
  2653. btrfs_release_path(path);
  2654. if (ret == 0 && S_ISREG(inode->i_mode)) {
  2655. ret = btrfs_lookup_file_extent(trans, root, path,
  2656. ino, (u64)-1, 0);
  2657. if (ret < 0) {
  2658. err = ret;
  2659. goto out;
  2660. }
  2661. BUG_ON(ret == 0); /* Corruption */
  2662. if (check_path_shared(root, path))
  2663. goto out;
  2664. btrfs_release_path(path);
  2665. }
  2666. if (!check_link) {
  2667. err = 0;
  2668. goto out;
  2669. }
  2670. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  2671. dentry->d_name.name, dentry->d_name.len, 0);
  2672. if (IS_ERR(di)) {
  2673. err = PTR_ERR(di);
  2674. goto out;
  2675. }
  2676. if (di) {
  2677. if (check_path_shared(root, path))
  2678. goto out;
  2679. } else {
  2680. err = 0;
  2681. goto out;
  2682. }
  2683. btrfs_release_path(path);
  2684. ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
  2685. dentry->d_name.len, ino, dir_ino, 0,
  2686. &index);
  2687. if (ret) {
  2688. err = ret;
  2689. goto out;
  2690. }
  2691. if (check_path_shared(root, path))
  2692. goto out;
  2693. btrfs_release_path(path);
  2694. /*
  2695. * This is a commit root search, if we can lookup inode item and other
  2696. * relative items in the commit root, it means the transaction of
  2697. * dir/file creation has been committed, and the dir index item that we
  2698. * delay to insert has also been inserted into the commit root. So
  2699. * we needn't worry about the delayed insertion of the dir index item
  2700. * here.
  2701. */
  2702. di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
  2703. dentry->d_name.name, dentry->d_name.len, 0);
  2704. if (IS_ERR(di)) {
  2705. err = PTR_ERR(di);
  2706. goto out;
  2707. }
  2708. BUG_ON(ret == -ENOENT);
  2709. if (check_path_shared(root, path))
  2710. goto out;
  2711. err = 0;
  2712. out:
  2713. btrfs_free_path(path);
  2714. /* Migrate the orphan reservation over */
  2715. if (!err)
  2716. err = btrfs_block_rsv_migrate(trans->block_rsv,
  2717. &root->fs_info->global_block_rsv,
  2718. trans->bytes_reserved);
  2719. if (err) {
  2720. btrfs_end_transaction(trans, root);
  2721. root->fs_info->enospc_unlink = 0;
  2722. return ERR_PTR(err);
  2723. }
  2724. trans->block_rsv = &root->fs_info->global_block_rsv;
  2725. return trans;
  2726. }
  2727. static void __unlink_end_trans(struct btrfs_trans_handle *trans,
  2728. struct btrfs_root *root)
  2729. {
  2730. if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
  2731. btrfs_block_rsv_release(root, trans->block_rsv,
  2732. trans->bytes_reserved);
  2733. trans->block_rsv = &root->fs_info->trans_block_rsv;
  2734. BUG_ON(!root->fs_info->enospc_unlink);
  2735. root->fs_info->enospc_unlink = 0;
  2736. }
  2737. btrfs_end_transaction(trans, root);
  2738. }
  2739. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  2740. {
  2741. struct btrfs_root *root = BTRFS_I(dir)->root;
  2742. struct btrfs_trans_handle *trans;
  2743. struct inode *inode = dentry->d_inode;
  2744. int ret;
  2745. trans = __unlink_start_trans(dir, dentry);
  2746. if (IS_ERR(trans))
  2747. return PTR_ERR(trans);
  2748. btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
  2749. ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2750. dentry->d_name.name, dentry->d_name.len);
  2751. if (ret)
  2752. goto out;
  2753. if (inode->i_nlink == 0) {
  2754. ret = btrfs_orphan_add(trans, inode);
  2755. if (ret)
  2756. goto out;
  2757. }
  2758. out:
  2759. __unlink_end_trans(trans, root);
  2760. btrfs_btree_balance_dirty(root);
  2761. return ret;
  2762. }
  2763. int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
  2764. struct btrfs_root *root,
  2765. struct inode *dir, u64 objectid,
  2766. const char *name, int name_len)
  2767. {
  2768. struct btrfs_path *path;
  2769. struct extent_buffer *leaf;
  2770. struct btrfs_dir_item *di;
  2771. struct btrfs_key key;
  2772. u64 index;
  2773. int ret;
  2774. u64 dir_ino = btrfs_ino(dir);
  2775. path = btrfs_alloc_path();
  2776. if (!path)
  2777. return -ENOMEM;
  2778. di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
  2779. name, name_len, -1);
  2780. if (IS_ERR_OR_NULL(di)) {
  2781. if (!di)
  2782. ret = -ENOENT;
  2783. else
  2784. ret = PTR_ERR(di);
  2785. goto out;
  2786. }
  2787. leaf = path->nodes[0];
  2788. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2789. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  2790. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2791. if (ret) {
  2792. btrfs_abort_transaction(trans, root, ret);
  2793. goto out;
  2794. }
  2795. btrfs_release_path(path);
  2796. ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  2797. objectid, root->root_key.objectid,
  2798. dir_ino, &index, name, name_len);
  2799. if (ret < 0) {
  2800. if (ret != -ENOENT) {
  2801. btrfs_abort_transaction(trans, root, ret);
  2802. goto out;
  2803. }
  2804. di = btrfs_search_dir_index_item(root, path, dir_ino,
  2805. name, name_len);
  2806. if (IS_ERR_OR_NULL(di)) {
  2807. if (!di)
  2808. ret = -ENOENT;
  2809. else
  2810. ret = PTR_ERR(di);
  2811. btrfs_abort_transaction(trans, root, ret);
  2812. goto out;
  2813. }
  2814. leaf = path->nodes[0];
  2815. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2816. btrfs_release_path(path);
  2817. index = key.offset;
  2818. }
  2819. btrfs_release_path(path);
  2820. ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
  2821. if (ret) {
  2822. btrfs_abort_transaction(trans, root, ret);
  2823. goto out;
  2824. }
  2825. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2826. inode_inc_iversion(dir);
  2827. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2828. ret = btrfs_update_inode_fallback(trans, root, dir);
  2829. if (ret)
  2830. btrfs_abort_transaction(trans, root, ret);
  2831. out:
  2832. btrfs_free_path(path);
  2833. return ret;
  2834. }
  2835. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  2836. {
  2837. struct inode *inode = dentry->d_inode;
  2838. int err = 0;
  2839. struct btrfs_root *root = BTRFS_I(dir)->root;
  2840. struct btrfs_trans_handle *trans;
  2841. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  2842. return -ENOTEMPTY;
  2843. if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
  2844. return -EPERM;
  2845. trans = __unlink_start_trans(dir, dentry);
  2846. if (IS_ERR(trans))
  2847. return PTR_ERR(trans);
  2848. if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  2849. err = btrfs_unlink_subvol(trans, root, dir,
  2850. BTRFS_I(inode)->location.objectid,
  2851. dentry->d_name.name,
  2852. dentry->d_name.len);
  2853. goto out;
  2854. }
  2855. err = btrfs_orphan_add(trans, inode);
  2856. if (err)
  2857. goto out;
  2858. /* now the directory is empty */
  2859. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2860. dentry->d_name.name, dentry->d_name.len);
  2861. if (!err)
  2862. btrfs_i_size_write(inode, 0);
  2863. out:
  2864. __unlink_end_trans(trans, root);
  2865. btrfs_btree_balance_dirty(root);
  2866. return err;
  2867. }
  2868. /*
  2869. * this can truncate away extent items, csum items and directory items.
  2870. * It starts at a high offset and removes keys until it can't find
  2871. * any higher than new_size
  2872. *
  2873. * csum items that cross the new i_size are truncated to the new size
  2874. * as well.
  2875. *
  2876. * min_type is the minimum key type to truncate down to. If set to 0, this
  2877. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  2878. */
  2879. int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  2880. struct btrfs_root *root,
  2881. struct inode *inode,
  2882. u64 new_size, u32 min_type)
  2883. {
  2884. struct btrfs_path *path;
  2885. struct extent_buffer *leaf;
  2886. struct btrfs_file_extent_item *fi;
  2887. struct btrfs_key key;
  2888. struct btrfs_key found_key;
  2889. u64 extent_start = 0;
  2890. u64 extent_num_bytes = 0;
  2891. u64 extent_offset = 0;
  2892. u64 item_end = 0;
  2893. u64 mask = root->sectorsize - 1;
  2894. u32 found_type = (u8)-1;
  2895. int found_extent;
  2896. int del_item;
  2897. int pending_del_nr = 0;
  2898. int pending_del_slot = 0;
  2899. int extent_type = -1;
  2900. int ret;
  2901. int err = 0;
  2902. u64 ino = btrfs_ino(inode);
  2903. BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
  2904. path = btrfs_alloc_path();
  2905. if (!path)
  2906. return -ENOMEM;
  2907. path->reada = -1;
  2908. /*
  2909. * We want to drop from the next block forward in case this new size is
  2910. * not block aligned since we will be keeping the last block of the
  2911. * extent just the way it is.
  2912. */
  2913. if (root->ref_cows || root == root->fs_info->tree_root)
  2914. btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
  2915. /*
  2916. * This function is also used to drop the items in the log tree before
  2917. * we relog the inode, so if root != BTRFS_I(inode)->root, it means
  2918. * it is used to drop the loged items. So we shouldn't kill the delayed
  2919. * items.
  2920. */
  2921. if (min_type == 0 && root == BTRFS_I(inode)->root)
  2922. btrfs_kill_delayed_inode_items(inode);
  2923. key.objectid = ino;
  2924. key.offset = (u64)-1;
  2925. key.type = (u8)-1;
  2926. search_again:
  2927. path->leave_spinning = 1;
  2928. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2929. if (ret < 0) {
  2930. err = ret;
  2931. goto out;
  2932. }
  2933. if (ret > 0) {
  2934. /* there are no items in the tree for us to truncate, we're
  2935. * done
  2936. */
  2937. if (path->slots[0] == 0)
  2938. goto out;
  2939. path->slots[0]--;
  2940. }
  2941. while (1) {
  2942. fi = NULL;
  2943. leaf = path->nodes[0];
  2944. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2945. found_type = btrfs_key_type(&found_key);
  2946. if (found_key.objectid != ino)
  2947. break;
  2948. if (found_type < min_type)
  2949. break;
  2950. item_end = found_key.offset;
  2951. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  2952. fi = btrfs_item_ptr(leaf, path->slots[0],
  2953. struct btrfs_file_extent_item);
  2954. extent_type = btrfs_file_extent_type(leaf, fi);
  2955. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2956. item_end +=
  2957. btrfs_file_extent_num_bytes(leaf, fi);
  2958. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2959. item_end += btrfs_file_extent_inline_len(leaf,
  2960. fi);
  2961. }
  2962. item_end--;
  2963. }
  2964. if (found_type > min_type) {
  2965. del_item = 1;
  2966. } else {
  2967. if (item_end < new_size)
  2968. break;
  2969. if (found_key.offset >= new_size)
  2970. del_item = 1;
  2971. else
  2972. del_item = 0;
  2973. }
  2974. found_extent = 0;
  2975. /* FIXME, shrink the extent if the ref count is only 1 */
  2976. if (found_type != BTRFS_EXTENT_DATA_KEY)
  2977. goto delete;
  2978. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2979. u64 num_dec;
  2980. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  2981. if (!del_item) {
  2982. u64 orig_num_bytes =
  2983. btrfs_file_extent_num_bytes(leaf, fi);
  2984. extent_num_bytes = new_size -
  2985. found_key.offset + root->sectorsize - 1;
  2986. extent_num_bytes = extent_num_bytes &
  2987. ~((u64)root->sectorsize - 1);
  2988. btrfs_set_file_extent_num_bytes(leaf, fi,
  2989. extent_num_bytes);
  2990. num_dec = (orig_num_bytes -
  2991. extent_num_bytes);
  2992. if (root->ref_cows && extent_start != 0)
  2993. inode_sub_bytes(inode, num_dec);
  2994. btrfs_mark_buffer_dirty(leaf);
  2995. } else {
  2996. extent_num_bytes =
  2997. btrfs_file_extent_disk_num_bytes(leaf,
  2998. fi);
  2999. extent_offset = found_key.offset -
  3000. btrfs_file_extent_offset(leaf, fi);
  3001. /* FIXME blocksize != 4096 */
  3002. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  3003. if (extent_start != 0) {
  3004. found_extent = 1;
  3005. if (root->ref_cows)
  3006. inode_sub_bytes(inode, num_dec);
  3007. }
  3008. }
  3009. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  3010. /*
  3011. * we can't truncate inline items that have had
  3012. * special encodings
  3013. */
  3014. if (!del_item &&
  3015. btrfs_file_extent_compression(leaf, fi) == 0 &&
  3016. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  3017. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  3018. u32 size = new_size - found_key.offset;
  3019. if (root->ref_cows) {
  3020. inode_sub_bytes(inode, item_end + 1 -
  3021. new_size);
  3022. }
  3023. size =
  3024. btrfs_file_extent_calc_inline_size(size);
  3025. btrfs_truncate_item(trans, root, path,
  3026. size, 1);
  3027. } else if (root->ref_cows) {
  3028. inode_sub_bytes(inode, item_end + 1 -
  3029. found_key.offset);
  3030. }
  3031. }
  3032. delete:
  3033. if (del_item) {
  3034. if (!pending_del_nr) {
  3035. /* no pending yet, add ourselves */
  3036. pending_del_slot = path->slots[0];
  3037. pending_del_nr = 1;
  3038. } else if (pending_del_nr &&
  3039. path->slots[0] + 1 == pending_del_slot) {
  3040. /* hop on the pending chunk */
  3041. pending_del_nr++;
  3042. pending_del_slot = path->slots[0];
  3043. } else {
  3044. BUG();
  3045. }
  3046. } else {
  3047. break;
  3048. }
  3049. if (found_extent && (root->ref_cows ||
  3050. root == root->fs_info->tree_root)) {
  3051. btrfs_set_path_blocking(path);
  3052. ret = btrfs_free_extent(trans, root, extent_start,
  3053. extent_num_bytes, 0,
  3054. btrfs_header_owner(leaf),
  3055. ino, extent_offset, 0);
  3056. BUG_ON(ret);
  3057. }
  3058. if (found_type == BTRFS_INODE_ITEM_KEY)
  3059. break;
  3060. if (path->slots[0] == 0 ||
  3061. path->slots[0] != pending_del_slot) {
  3062. if (pending_del_nr) {
  3063. ret = btrfs_del_items(trans, root, path,
  3064. pending_del_slot,
  3065. pending_del_nr);
  3066. if (ret) {
  3067. btrfs_abort_transaction(trans,
  3068. root, ret);
  3069. goto error;
  3070. }
  3071. pending_del_nr = 0;
  3072. }
  3073. btrfs_release_path(path);
  3074. goto search_again;
  3075. } else {
  3076. path->slots[0]--;
  3077. }
  3078. }
  3079. out:
  3080. if (pending_del_nr) {
  3081. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  3082. pending_del_nr);
  3083. if (ret)
  3084. btrfs_abort_transaction(trans, root, ret);
  3085. }
  3086. error:
  3087. btrfs_free_path(path);
  3088. return err;
  3089. }
  3090. /*
  3091. * btrfs_truncate_page - read, zero a chunk and write a page
  3092. * @inode - inode that we're zeroing
  3093. * @from - the offset to start zeroing
  3094. * @len - the length to zero, 0 to zero the entire range respective to the
  3095. * offset
  3096. * @front - zero up to the offset instead of from the offset on
  3097. *
  3098. * This will find the page for the "from" offset and cow the page and zero the
  3099. * part we want to zero. This is used with truncate and hole punching.
  3100. */
  3101. int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
  3102. int front)
  3103. {
  3104. struct address_space *mapping = inode->i_mapping;
  3105. struct btrfs_root *root = BTRFS_I(inode)->root;
  3106. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3107. struct btrfs_ordered_extent *ordered;
  3108. struct extent_state *cached_state = NULL;
  3109. char *kaddr;
  3110. u32 blocksize = root->sectorsize;
  3111. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  3112. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  3113. struct page *page;
  3114. gfp_t mask = btrfs_alloc_write_mask(mapping);
  3115. int ret = 0;
  3116. u64 page_start;
  3117. u64 page_end;
  3118. if ((offset & (blocksize - 1)) == 0 &&
  3119. (!len || ((len & (blocksize - 1)) == 0)))
  3120. goto out;
  3121. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  3122. if (ret)
  3123. goto out;
  3124. again:
  3125. page = find_or_create_page(mapping, index, mask);
  3126. if (!page) {
  3127. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3128. ret = -ENOMEM;
  3129. goto out;
  3130. }
  3131. page_start = page_offset(page);
  3132. page_end = page_start + PAGE_CACHE_SIZE - 1;
  3133. if (!PageUptodate(page)) {
  3134. ret = btrfs_readpage(NULL, page);
  3135. lock_page(page);
  3136. if (page->mapping != mapping) {
  3137. unlock_page(page);
  3138. page_cache_release(page);
  3139. goto again;
  3140. }
  3141. if (!PageUptodate(page)) {
  3142. ret = -EIO;
  3143. goto out_unlock;
  3144. }
  3145. }
  3146. wait_on_page_writeback(page);
  3147. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
  3148. set_page_extent_mapped(page);
  3149. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  3150. if (ordered) {
  3151. unlock_extent_cached(io_tree, page_start, page_end,
  3152. &cached_state, GFP_NOFS);
  3153. unlock_page(page);
  3154. page_cache_release(page);
  3155. btrfs_start_ordered_extent(inode, ordered, 1);
  3156. btrfs_put_ordered_extent(ordered);
  3157. goto again;
  3158. }
  3159. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  3160. EXTENT_DIRTY | EXTENT_DELALLOC |
  3161. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  3162. 0, 0, &cached_state, GFP_NOFS);
  3163. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  3164. &cached_state);
  3165. if (ret) {
  3166. unlock_extent_cached(io_tree, page_start, page_end,
  3167. &cached_state, GFP_NOFS);
  3168. goto out_unlock;
  3169. }
  3170. if (offset != PAGE_CACHE_SIZE) {
  3171. if (!len)
  3172. len = PAGE_CACHE_SIZE - offset;
  3173. kaddr = kmap(page);
  3174. if (front)
  3175. memset(kaddr, 0, offset);
  3176. else
  3177. memset(kaddr + offset, 0, len);
  3178. flush_dcache_page(page);
  3179. kunmap(page);
  3180. }
  3181. ClearPageChecked(page);
  3182. set_page_dirty(page);
  3183. unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
  3184. GFP_NOFS);
  3185. out_unlock:
  3186. if (ret)
  3187. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  3188. unlock_page(page);
  3189. page_cache_release(page);
  3190. out:
  3191. return ret;
  3192. }
  3193. /*
  3194. * This function puts in dummy file extents for the area we're creating a hole
  3195. * for. So if we are truncating this file to a larger size we need to insert
  3196. * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
  3197. * the range between oldsize and size
  3198. */
  3199. int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
  3200. {
  3201. struct btrfs_trans_handle *trans;
  3202. struct btrfs_root *root = BTRFS_I(inode)->root;
  3203. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3204. struct extent_map *em = NULL;
  3205. struct extent_state *cached_state = NULL;
  3206. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  3207. u64 mask = root->sectorsize - 1;
  3208. u64 hole_start = (oldsize + mask) & ~mask;
  3209. u64 block_end = (size + mask) & ~mask;
  3210. u64 last_byte;
  3211. u64 cur_offset;
  3212. u64 hole_size;
  3213. int err = 0;
  3214. if (size <= hole_start)
  3215. return 0;
  3216. while (1) {
  3217. struct btrfs_ordered_extent *ordered;
  3218. btrfs_wait_ordered_range(inode, hole_start,
  3219. block_end - hole_start);
  3220. lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
  3221. &cached_state);
  3222. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  3223. if (!ordered)
  3224. break;
  3225. unlock_extent_cached(io_tree, hole_start, block_end - 1,
  3226. &cached_state, GFP_NOFS);
  3227. btrfs_put_ordered_extent(ordered);
  3228. }
  3229. cur_offset = hole_start;
  3230. while (1) {
  3231. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  3232. block_end - cur_offset, 0);
  3233. if (IS_ERR(em)) {
  3234. err = PTR_ERR(em);
  3235. break;
  3236. }
  3237. last_byte = min(extent_map_end(em), block_end);
  3238. last_byte = (last_byte + mask) & ~mask;
  3239. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
  3240. struct extent_map *hole_em;
  3241. hole_size = last_byte - cur_offset;
  3242. trans = btrfs_start_transaction(root, 3);
  3243. if (IS_ERR(trans)) {
  3244. err = PTR_ERR(trans);
  3245. break;
  3246. }
  3247. err = btrfs_drop_extents(trans, root, inode,
  3248. cur_offset,
  3249. cur_offset + hole_size, 1);
  3250. if (err) {
  3251. btrfs_abort_transaction(trans, root, err);
  3252. btrfs_end_transaction(trans, root);
  3253. break;
  3254. }
  3255. err = btrfs_insert_file_extent(trans, root,
  3256. btrfs_ino(inode), cur_offset, 0,
  3257. 0, hole_size, 0, hole_size,
  3258. 0, 0, 0);
  3259. if (err) {
  3260. btrfs_abort_transaction(trans, root, err);
  3261. btrfs_end_transaction(trans, root);
  3262. break;
  3263. }
  3264. btrfs_drop_extent_cache(inode, cur_offset,
  3265. cur_offset + hole_size - 1, 0);
  3266. hole_em = alloc_extent_map();
  3267. if (!hole_em) {
  3268. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  3269. &BTRFS_I(inode)->runtime_flags);
  3270. goto next;
  3271. }
  3272. hole_em->start = cur_offset;
  3273. hole_em->len = hole_size;
  3274. hole_em->orig_start = cur_offset;
  3275. hole_em->block_start = EXTENT_MAP_HOLE;
  3276. hole_em->block_len = 0;
  3277. hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
  3278. hole_em->compress_type = BTRFS_COMPRESS_NONE;
  3279. hole_em->generation = trans->transid;
  3280. while (1) {
  3281. write_lock(&em_tree->lock);
  3282. err = add_extent_mapping(em_tree, hole_em);
  3283. if (!err)
  3284. list_move(&hole_em->list,
  3285. &em_tree->modified_extents);
  3286. write_unlock(&em_tree->lock);
  3287. if (err != -EEXIST)
  3288. break;
  3289. btrfs_drop_extent_cache(inode, cur_offset,
  3290. cur_offset +
  3291. hole_size - 1, 0);
  3292. }
  3293. free_extent_map(hole_em);
  3294. next:
  3295. btrfs_update_inode(trans, root, inode);
  3296. btrfs_end_transaction(trans, root);
  3297. }
  3298. free_extent_map(em);
  3299. em = NULL;
  3300. cur_offset = last_byte;
  3301. if (cur_offset >= block_end)
  3302. break;
  3303. }
  3304. free_extent_map(em);
  3305. unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
  3306. GFP_NOFS);
  3307. return err;
  3308. }
  3309. static int btrfs_setsize(struct inode *inode, loff_t newsize)
  3310. {
  3311. struct btrfs_root *root = BTRFS_I(inode)->root;
  3312. struct btrfs_trans_handle *trans;
  3313. loff_t oldsize = i_size_read(inode);
  3314. int ret;
  3315. if (newsize == oldsize)
  3316. return 0;
  3317. if (newsize > oldsize) {
  3318. truncate_pagecache(inode, oldsize, newsize);
  3319. ret = btrfs_cont_expand(inode, oldsize, newsize);
  3320. if (ret)
  3321. return ret;
  3322. trans = btrfs_start_transaction(root, 1);
  3323. if (IS_ERR(trans))
  3324. return PTR_ERR(trans);
  3325. i_size_write(inode, newsize);
  3326. btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
  3327. ret = btrfs_update_inode(trans, root, inode);
  3328. btrfs_end_transaction(trans, root);
  3329. } else {
  3330. /*
  3331. * We're truncating a file that used to have good data down to
  3332. * zero. Make sure it gets into the ordered flush list so that
  3333. * any new writes get down to disk quickly.
  3334. */
  3335. if (newsize == 0)
  3336. set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  3337. &BTRFS_I(inode)->runtime_flags);
  3338. /* we don't support swapfiles, so vmtruncate shouldn't fail */
  3339. truncate_setsize(inode, newsize);
  3340. ret = btrfs_truncate(inode);
  3341. }
  3342. return ret;
  3343. }
  3344. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  3345. {
  3346. struct inode *inode = dentry->d_inode;
  3347. struct btrfs_root *root = BTRFS_I(inode)->root;
  3348. int err;
  3349. if (btrfs_root_readonly(root))
  3350. return -EROFS;
  3351. err = inode_change_ok(inode, attr);
  3352. if (err)
  3353. return err;
  3354. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  3355. err = btrfs_setsize(inode, attr->ia_size);
  3356. if (err)
  3357. return err;
  3358. }
  3359. if (attr->ia_valid) {
  3360. setattr_copy(inode, attr);
  3361. inode_inc_iversion(inode);
  3362. err = btrfs_dirty_inode(inode);
  3363. if (!err && attr->ia_valid & ATTR_MODE)
  3364. err = btrfs_acl_chmod(inode);
  3365. }
  3366. return err;
  3367. }
  3368. void btrfs_evict_inode(struct inode *inode)
  3369. {
  3370. struct btrfs_trans_handle *trans;
  3371. struct btrfs_root *root = BTRFS_I(inode)->root;
  3372. struct btrfs_block_rsv *rsv, *global_rsv;
  3373. u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
  3374. int ret;
  3375. trace_btrfs_inode_evict(inode);
  3376. truncate_inode_pages(&inode->i_data, 0);
  3377. if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
  3378. btrfs_is_free_space_inode(inode)))
  3379. goto no_delete;
  3380. if (is_bad_inode(inode)) {
  3381. btrfs_orphan_del(NULL, inode);
  3382. goto no_delete;
  3383. }
  3384. /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
  3385. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  3386. if (root->fs_info->log_root_recovering) {
  3387. BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  3388. &BTRFS_I(inode)->runtime_flags));
  3389. goto no_delete;
  3390. }
  3391. if (inode->i_nlink > 0) {
  3392. BUG_ON(btrfs_root_refs(&root->root_item) != 0);
  3393. goto no_delete;
  3394. }
  3395. rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
  3396. if (!rsv) {
  3397. btrfs_orphan_del(NULL, inode);
  3398. goto no_delete;
  3399. }
  3400. rsv->size = min_size;
  3401. rsv->failfast = 1;
  3402. global_rsv = &root->fs_info->global_block_rsv;
  3403. btrfs_i_size_write(inode, 0);
  3404. /*
  3405. * This is a bit simpler than btrfs_truncate since we've already
  3406. * reserved our space for our orphan item in the unlink, so we just
  3407. * need to reserve some slack space in case we add bytes and update
  3408. * inode item when doing the truncate.
  3409. */
  3410. while (1) {
  3411. ret = btrfs_block_rsv_refill(root, rsv, min_size,
  3412. BTRFS_RESERVE_FLUSH_LIMIT);
  3413. /*
  3414. * Try and steal from the global reserve since we will
  3415. * likely not use this space anyway, we want to try as
  3416. * hard as possible to get this to work.
  3417. */
  3418. if (ret)
  3419. ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
  3420. if (ret) {
  3421. printk(KERN_WARNING "Could not get space for a "
  3422. "delete, will truncate on mount %d\n", ret);
  3423. btrfs_orphan_del(NULL, inode);
  3424. btrfs_free_block_rsv(root, rsv);
  3425. goto no_delete;
  3426. }
  3427. trans = btrfs_start_transaction_lflush(root, 1);
  3428. if (IS_ERR(trans)) {
  3429. btrfs_orphan_del(NULL, inode);
  3430. btrfs_free_block_rsv(root, rsv);
  3431. goto no_delete;
  3432. }
  3433. trans->block_rsv = rsv;
  3434. ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
  3435. if (ret != -ENOSPC)
  3436. break;
  3437. trans->block_rsv = &root->fs_info->trans_block_rsv;
  3438. ret = btrfs_update_inode(trans, root, inode);
  3439. BUG_ON(ret);
  3440. btrfs_end_transaction(trans, root);
  3441. trans = NULL;
  3442. btrfs_btree_balance_dirty(root);
  3443. }
  3444. btrfs_free_block_rsv(root, rsv);
  3445. if (ret == 0) {
  3446. trans->block_rsv = root->orphan_block_rsv;
  3447. ret = btrfs_orphan_del(trans, inode);
  3448. BUG_ON(ret);
  3449. }
  3450. trans->block_rsv = &root->fs_info->trans_block_rsv;
  3451. if (!(root == root->fs_info->tree_root ||
  3452. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
  3453. btrfs_return_ino(root, btrfs_ino(inode));
  3454. btrfs_end_transaction(trans, root);
  3455. btrfs_btree_balance_dirty(root);
  3456. no_delete:
  3457. clear_inode(inode);
  3458. return;
  3459. }
  3460. /*
  3461. * this returns the key found in the dir entry in the location pointer.
  3462. * If no dir entries were found, location->objectid is 0.
  3463. */
  3464. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  3465. struct btrfs_key *location)
  3466. {
  3467. const char *name = dentry->d_name.name;
  3468. int namelen = dentry->d_name.len;
  3469. struct btrfs_dir_item *di;
  3470. struct btrfs_path *path;
  3471. struct btrfs_root *root = BTRFS_I(dir)->root;
  3472. int ret = 0;
  3473. path = btrfs_alloc_path();
  3474. if (!path)
  3475. return -ENOMEM;
  3476. di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
  3477. namelen, 0);
  3478. if (IS_ERR(di))
  3479. ret = PTR_ERR(di);
  3480. if (IS_ERR_OR_NULL(di))
  3481. goto out_err;
  3482. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  3483. out:
  3484. btrfs_free_path(path);
  3485. return ret;
  3486. out_err:
  3487. location->objectid = 0;
  3488. goto out;
  3489. }
  3490. /*
  3491. * when we hit a tree root in a directory, the btrfs part of the inode
  3492. * needs to be changed to reflect the root directory of the tree root. This
  3493. * is kind of like crossing a mount point.
  3494. */
  3495. static int fixup_tree_root_location(struct btrfs_root *root,
  3496. struct inode *dir,
  3497. struct dentry *dentry,
  3498. struct btrfs_key *location,
  3499. struct btrfs_root **sub_root)
  3500. {
  3501. struct btrfs_path *path;
  3502. struct btrfs_root *new_root;
  3503. struct btrfs_root_ref *ref;
  3504. struct extent_buffer *leaf;
  3505. int ret;
  3506. int err = 0;
  3507. path = btrfs_alloc_path();
  3508. if (!path) {
  3509. err = -ENOMEM;
  3510. goto out;
  3511. }
  3512. err = -ENOENT;
  3513. ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
  3514. BTRFS_I(dir)->root->root_key.objectid,
  3515. location->objectid);
  3516. if (ret) {
  3517. if (ret < 0)
  3518. err = ret;
  3519. goto out;
  3520. }
  3521. leaf = path->nodes[0];
  3522. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  3523. if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
  3524. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  3525. goto out;
  3526. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  3527. (unsigned long)(ref + 1),
  3528. dentry->d_name.len);
  3529. if (ret)
  3530. goto out;
  3531. btrfs_release_path(path);
  3532. new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
  3533. if (IS_ERR(new_root)) {
  3534. err = PTR_ERR(new_root);
  3535. goto out;
  3536. }
  3537. if (btrfs_root_refs(&new_root->root_item) == 0) {
  3538. err = -ENOENT;
  3539. goto out;
  3540. }
  3541. *sub_root = new_root;
  3542. location->objectid = btrfs_root_dirid(&new_root->root_item);
  3543. location->type = BTRFS_INODE_ITEM_KEY;
  3544. location->offset = 0;
  3545. err = 0;
  3546. out:
  3547. btrfs_free_path(path);
  3548. return err;
  3549. }
  3550. static void inode_tree_add(struct inode *inode)
  3551. {
  3552. struct btrfs_root *root = BTRFS_I(inode)->root;
  3553. struct btrfs_inode *entry;
  3554. struct rb_node **p;
  3555. struct rb_node *parent;
  3556. u64 ino = btrfs_ino(inode);
  3557. again:
  3558. p = &root->inode_tree.rb_node;
  3559. parent = NULL;
  3560. if (inode_unhashed(inode))
  3561. return;
  3562. spin_lock(&root->inode_lock);
  3563. while (*p) {
  3564. parent = *p;
  3565. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  3566. if (ino < btrfs_ino(&entry->vfs_inode))
  3567. p = &parent->rb_left;
  3568. else if (ino > btrfs_ino(&entry->vfs_inode))
  3569. p = &parent->rb_right;
  3570. else {
  3571. WARN_ON(!(entry->vfs_inode.i_state &
  3572. (I_WILL_FREE | I_FREEING)));
  3573. rb_erase(parent, &root->inode_tree);
  3574. RB_CLEAR_NODE(parent);
  3575. spin_unlock(&root->inode_lock);
  3576. goto again;
  3577. }
  3578. }
  3579. rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
  3580. rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3581. spin_unlock(&root->inode_lock);
  3582. }
  3583. static void inode_tree_del(struct inode *inode)
  3584. {
  3585. struct btrfs_root *root = BTRFS_I(inode)->root;
  3586. int empty = 0;
  3587. spin_lock(&root->inode_lock);
  3588. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  3589. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  3590. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  3591. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3592. }
  3593. spin_unlock(&root->inode_lock);
  3594. /*
  3595. * Free space cache has inodes in the tree root, but the tree root has a
  3596. * root_refs of 0, so this could end up dropping the tree root as a
  3597. * snapshot, so we need the extra !root->fs_info->tree_root check to
  3598. * make sure we don't drop it.
  3599. */
  3600. if (empty && btrfs_root_refs(&root->root_item) == 0 &&
  3601. root != root->fs_info->tree_root) {
  3602. synchronize_srcu(&root->fs_info->subvol_srcu);
  3603. spin_lock(&root->inode_lock);
  3604. empty = RB_EMPTY_ROOT(&root->inode_tree);
  3605. spin_unlock(&root->inode_lock);
  3606. if (empty)
  3607. btrfs_add_dead_root(root);
  3608. }
  3609. }
  3610. void btrfs_invalidate_inodes(struct btrfs_root *root)
  3611. {
  3612. struct rb_node *node;
  3613. struct rb_node *prev;
  3614. struct btrfs_inode *entry;
  3615. struct inode *inode;
  3616. u64 objectid = 0;
  3617. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  3618. spin_lock(&root->inode_lock);
  3619. again:
  3620. node = root->inode_tree.rb_node;
  3621. prev = NULL;
  3622. while (node) {
  3623. prev = node;
  3624. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3625. if (objectid < btrfs_ino(&entry->vfs_inode))
  3626. node = node->rb_left;
  3627. else if (objectid > btrfs_ino(&entry->vfs_inode))
  3628. node = node->rb_right;
  3629. else
  3630. break;
  3631. }
  3632. if (!node) {
  3633. while (prev) {
  3634. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  3635. if (objectid <= btrfs_ino(&entry->vfs_inode)) {
  3636. node = prev;
  3637. break;
  3638. }
  3639. prev = rb_next(prev);
  3640. }
  3641. }
  3642. while (node) {
  3643. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3644. objectid = btrfs_ino(&entry->vfs_inode) + 1;
  3645. inode = igrab(&entry->vfs_inode);
  3646. if (inode) {
  3647. spin_unlock(&root->inode_lock);
  3648. if (atomic_read(&inode->i_count) > 1)
  3649. d_prune_aliases(inode);
  3650. /*
  3651. * btrfs_drop_inode will have it removed from
  3652. * the inode cache when its usage count
  3653. * hits zero.
  3654. */
  3655. iput(inode);
  3656. cond_resched();
  3657. spin_lock(&root->inode_lock);
  3658. goto again;
  3659. }
  3660. if (cond_resched_lock(&root->inode_lock))
  3661. goto again;
  3662. node = rb_next(node);
  3663. }
  3664. spin_unlock(&root->inode_lock);
  3665. }
  3666. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  3667. {
  3668. struct btrfs_iget_args *args = p;
  3669. inode->i_ino = args->ino;
  3670. BTRFS_I(inode)->root = args->root;
  3671. return 0;
  3672. }
  3673. static int btrfs_find_actor(struct inode *inode, void *opaque)
  3674. {
  3675. struct btrfs_iget_args *args = opaque;
  3676. return args->ino == btrfs_ino(inode) &&
  3677. args->root == BTRFS_I(inode)->root;
  3678. }
  3679. static struct inode *btrfs_iget_locked(struct super_block *s,
  3680. u64 objectid,
  3681. struct btrfs_root *root)
  3682. {
  3683. struct inode *inode;
  3684. struct btrfs_iget_args args;
  3685. args.ino = objectid;
  3686. args.root = root;
  3687. inode = iget5_locked(s, objectid, btrfs_find_actor,
  3688. btrfs_init_locked_inode,
  3689. (void *)&args);
  3690. return inode;
  3691. }
  3692. /* Get an inode object given its location and corresponding root.
  3693. * Returns in *is_new if the inode was read from disk
  3694. */
  3695. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  3696. struct btrfs_root *root, int *new)
  3697. {
  3698. struct inode *inode;
  3699. inode = btrfs_iget_locked(s, location->objectid, root);
  3700. if (!inode)
  3701. return ERR_PTR(-ENOMEM);
  3702. if (inode->i_state & I_NEW) {
  3703. BTRFS_I(inode)->root = root;
  3704. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  3705. btrfs_read_locked_inode(inode);
  3706. if (!is_bad_inode(inode)) {
  3707. inode_tree_add(inode);
  3708. unlock_new_inode(inode);
  3709. if (new)
  3710. *new = 1;
  3711. } else {
  3712. unlock_new_inode(inode);
  3713. iput(inode);
  3714. inode = ERR_PTR(-ESTALE);
  3715. }
  3716. }
  3717. return inode;
  3718. }
  3719. static struct inode *new_simple_dir(struct super_block *s,
  3720. struct btrfs_key *key,
  3721. struct btrfs_root *root)
  3722. {
  3723. struct inode *inode = new_inode(s);
  3724. if (!inode)
  3725. return ERR_PTR(-ENOMEM);
  3726. BTRFS_I(inode)->root = root;
  3727. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  3728. set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
  3729. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  3730. inode->i_op = &btrfs_dir_ro_inode_operations;
  3731. inode->i_fop = &simple_dir_operations;
  3732. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  3733. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3734. return inode;
  3735. }
  3736. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  3737. {
  3738. struct inode *inode;
  3739. struct btrfs_root *root = BTRFS_I(dir)->root;
  3740. struct btrfs_root *sub_root = root;
  3741. struct btrfs_key location;
  3742. int index;
  3743. int ret = 0;
  3744. if (dentry->d_name.len > BTRFS_NAME_LEN)
  3745. return ERR_PTR(-ENAMETOOLONG);
  3746. if (unlikely(d_need_lookup(dentry))) {
  3747. memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
  3748. kfree(dentry->d_fsdata);
  3749. dentry->d_fsdata = NULL;
  3750. /* This thing is hashed, drop it for now */
  3751. d_drop(dentry);
  3752. } else {
  3753. ret = btrfs_inode_by_name(dir, dentry, &location);
  3754. }
  3755. if (ret < 0)
  3756. return ERR_PTR(ret);
  3757. if (location.objectid == 0)
  3758. return NULL;
  3759. if (location.type == BTRFS_INODE_ITEM_KEY) {
  3760. inode = btrfs_iget(dir->i_sb, &location, root, NULL);
  3761. return inode;
  3762. }
  3763. BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
  3764. index = srcu_read_lock(&root->fs_info->subvol_srcu);
  3765. ret = fixup_tree_root_location(root, dir, dentry,
  3766. &location, &sub_root);
  3767. if (ret < 0) {
  3768. if (ret != -ENOENT)
  3769. inode = ERR_PTR(ret);
  3770. else
  3771. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  3772. } else {
  3773. inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
  3774. }
  3775. srcu_read_unlock(&root->fs_info->subvol_srcu, index);
  3776. if (!IS_ERR(inode) && root != sub_root) {
  3777. down_read(&root->fs_info->cleanup_work_sem);
  3778. if (!(inode->i_sb->s_flags & MS_RDONLY))
  3779. ret = btrfs_orphan_cleanup(sub_root);
  3780. up_read(&root->fs_info->cleanup_work_sem);
  3781. if (ret)
  3782. inode = ERR_PTR(ret);
  3783. }
  3784. return inode;
  3785. }
  3786. static int btrfs_dentry_delete(const struct dentry *dentry)
  3787. {
  3788. struct btrfs_root *root;
  3789. struct inode *inode = dentry->d_inode;
  3790. if (!inode && !IS_ROOT(dentry))
  3791. inode = dentry->d_parent->d_inode;
  3792. if (inode) {
  3793. root = BTRFS_I(inode)->root;
  3794. if (btrfs_root_refs(&root->root_item) == 0)
  3795. return 1;
  3796. if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  3797. return 1;
  3798. }
  3799. return 0;
  3800. }
  3801. static void btrfs_dentry_release(struct dentry *dentry)
  3802. {
  3803. if (dentry->d_fsdata)
  3804. kfree(dentry->d_fsdata);
  3805. }
  3806. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  3807. unsigned int flags)
  3808. {
  3809. struct dentry *ret;
  3810. ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
  3811. if (unlikely(d_need_lookup(dentry))) {
  3812. spin_lock(&dentry->d_lock);
  3813. dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
  3814. spin_unlock(&dentry->d_lock);
  3815. }
  3816. return ret;
  3817. }
  3818. unsigned char btrfs_filetype_table[] = {
  3819. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  3820. };
  3821. static int btrfs_real_readdir(struct file *filp, void *dirent,
  3822. filldir_t filldir)
  3823. {
  3824. struct inode *inode = filp->f_dentry->d_inode;
  3825. struct btrfs_root *root = BTRFS_I(inode)->root;
  3826. struct btrfs_item *item;
  3827. struct btrfs_dir_item *di;
  3828. struct btrfs_key key;
  3829. struct btrfs_key found_key;
  3830. struct btrfs_path *path;
  3831. struct list_head ins_list;
  3832. struct list_head del_list;
  3833. int ret;
  3834. struct extent_buffer *leaf;
  3835. int slot;
  3836. unsigned char d_type;
  3837. int over = 0;
  3838. u32 di_cur;
  3839. u32 di_total;
  3840. u32 di_len;
  3841. int key_type = BTRFS_DIR_INDEX_KEY;
  3842. char tmp_name[32];
  3843. char *name_ptr;
  3844. int name_len;
  3845. int is_curr = 0; /* filp->f_pos points to the current index? */
  3846. /* FIXME, use a real flag for deciding about the key type */
  3847. if (root->fs_info->tree_root == root)
  3848. key_type = BTRFS_DIR_ITEM_KEY;
  3849. /* special case for "." */
  3850. if (filp->f_pos == 0) {
  3851. over = filldir(dirent, ".", 1,
  3852. filp->f_pos, btrfs_ino(inode), DT_DIR);
  3853. if (over)
  3854. return 0;
  3855. filp->f_pos = 1;
  3856. }
  3857. /* special case for .., just use the back ref */
  3858. if (filp->f_pos == 1) {
  3859. u64 pino = parent_ino(filp->f_path.dentry);
  3860. over = filldir(dirent, "..", 2,
  3861. filp->f_pos, pino, DT_DIR);
  3862. if (over)
  3863. return 0;
  3864. filp->f_pos = 2;
  3865. }
  3866. path = btrfs_alloc_path();
  3867. if (!path)
  3868. return -ENOMEM;
  3869. path->reada = 1;
  3870. if (key_type == BTRFS_DIR_INDEX_KEY) {
  3871. INIT_LIST_HEAD(&ins_list);
  3872. INIT_LIST_HEAD(&del_list);
  3873. btrfs_get_delayed_items(inode, &ins_list, &del_list);
  3874. }
  3875. btrfs_set_key_type(&key, key_type);
  3876. key.offset = filp->f_pos;
  3877. key.objectid = btrfs_ino(inode);
  3878. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3879. if (ret < 0)
  3880. goto err;
  3881. while (1) {
  3882. leaf = path->nodes[0];
  3883. slot = path->slots[0];
  3884. if (slot >= btrfs_header_nritems(leaf)) {
  3885. ret = btrfs_next_leaf(root, path);
  3886. if (ret < 0)
  3887. goto err;
  3888. else if (ret > 0)
  3889. break;
  3890. continue;
  3891. }
  3892. item = btrfs_item_nr(leaf, slot);
  3893. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3894. if (found_key.objectid != key.objectid)
  3895. break;
  3896. if (btrfs_key_type(&found_key) != key_type)
  3897. break;
  3898. if (found_key.offset < filp->f_pos)
  3899. goto next;
  3900. if (key_type == BTRFS_DIR_INDEX_KEY &&
  3901. btrfs_should_delete_dir_index(&del_list,
  3902. found_key.offset))
  3903. goto next;
  3904. filp->f_pos = found_key.offset;
  3905. is_curr = 1;
  3906. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  3907. di_cur = 0;
  3908. di_total = btrfs_item_size(leaf, item);
  3909. while (di_cur < di_total) {
  3910. struct btrfs_key location;
  3911. if (verify_dir_item(root, leaf, di))
  3912. break;
  3913. name_len = btrfs_dir_name_len(leaf, di);
  3914. if (name_len <= sizeof(tmp_name)) {
  3915. name_ptr = tmp_name;
  3916. } else {
  3917. name_ptr = kmalloc(name_len, GFP_NOFS);
  3918. if (!name_ptr) {
  3919. ret = -ENOMEM;
  3920. goto err;
  3921. }
  3922. }
  3923. read_extent_buffer(leaf, name_ptr,
  3924. (unsigned long)(di + 1), name_len);
  3925. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  3926. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  3927. /* is this a reference to our own snapshot? If so
  3928. * skip it.
  3929. *
  3930. * In contrast to old kernels, we insert the snapshot's
  3931. * dir item and dir index after it has been created, so
  3932. * we won't find a reference to our own snapshot. We
  3933. * still keep the following code for backward
  3934. * compatibility.
  3935. */
  3936. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  3937. location.objectid == root->root_key.objectid) {
  3938. over = 0;
  3939. goto skip;
  3940. }
  3941. over = filldir(dirent, name_ptr, name_len,
  3942. found_key.offset, location.objectid,
  3943. d_type);
  3944. skip:
  3945. if (name_ptr != tmp_name)
  3946. kfree(name_ptr);
  3947. if (over)
  3948. goto nopos;
  3949. di_len = btrfs_dir_name_len(leaf, di) +
  3950. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  3951. di_cur += di_len;
  3952. di = (struct btrfs_dir_item *)((char *)di + di_len);
  3953. }
  3954. next:
  3955. path->slots[0]++;
  3956. }
  3957. if (key_type == BTRFS_DIR_INDEX_KEY) {
  3958. if (is_curr)
  3959. filp->f_pos++;
  3960. ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
  3961. &ins_list);
  3962. if (ret)
  3963. goto nopos;
  3964. }
  3965. /* Reached end of directory/root. Bump pos past the last item. */
  3966. if (key_type == BTRFS_DIR_INDEX_KEY)
  3967. /*
  3968. * 32-bit glibc will use getdents64, but then strtol -
  3969. * so the last number we can serve is this.
  3970. */
  3971. filp->f_pos = 0x7fffffff;
  3972. else
  3973. filp->f_pos++;
  3974. nopos:
  3975. ret = 0;
  3976. err:
  3977. if (key_type == BTRFS_DIR_INDEX_KEY)
  3978. btrfs_put_delayed_items(&ins_list, &del_list);
  3979. btrfs_free_path(path);
  3980. return ret;
  3981. }
  3982. int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  3983. {
  3984. struct btrfs_root *root = BTRFS_I(inode)->root;
  3985. struct btrfs_trans_handle *trans;
  3986. int ret = 0;
  3987. bool nolock = false;
  3988. if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
  3989. return 0;
  3990. if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
  3991. nolock = true;
  3992. if (wbc->sync_mode == WB_SYNC_ALL) {
  3993. if (nolock)
  3994. trans = btrfs_join_transaction_nolock(root);
  3995. else
  3996. trans = btrfs_join_transaction(root);
  3997. if (IS_ERR(trans))
  3998. return PTR_ERR(trans);
  3999. ret = btrfs_commit_transaction(trans, root);
  4000. }
  4001. return ret;
  4002. }
  4003. /*
  4004. * This is somewhat expensive, updating the tree every time the
  4005. * inode changes. But, it is most likely to find the inode in cache.
  4006. * FIXME, needs more benchmarking...there are no reasons other than performance
  4007. * to keep or drop this code.
  4008. */
  4009. int btrfs_dirty_inode(struct inode *inode)
  4010. {
  4011. struct btrfs_root *root = BTRFS_I(inode)->root;
  4012. struct btrfs_trans_handle *trans;
  4013. int ret;
  4014. if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
  4015. return 0;
  4016. trans = btrfs_join_transaction(root);
  4017. if (IS_ERR(trans))
  4018. return PTR_ERR(trans);
  4019. ret = btrfs_update_inode(trans, root, inode);
  4020. if (ret && ret == -ENOSPC) {
  4021. /* whoops, lets try again with the full transaction */
  4022. btrfs_end_transaction(trans, root);
  4023. trans = btrfs_start_transaction(root, 1);
  4024. if (IS_ERR(trans))
  4025. return PTR_ERR(trans);
  4026. ret = btrfs_update_inode(trans, root, inode);
  4027. }
  4028. btrfs_end_transaction(trans, root);
  4029. if (BTRFS_I(inode)->delayed_node)
  4030. btrfs_balance_delayed_items(root);
  4031. return ret;
  4032. }
  4033. /*
  4034. * This is a copy of file_update_time. We need this so we can return error on
  4035. * ENOSPC for updating the inode in the case of file write and mmap writes.
  4036. */
  4037. static int btrfs_update_time(struct inode *inode, struct timespec *now,
  4038. int flags)
  4039. {
  4040. struct btrfs_root *root = BTRFS_I(inode)->root;
  4041. if (btrfs_root_readonly(root))
  4042. return -EROFS;
  4043. if (flags & S_VERSION)
  4044. inode_inc_iversion(inode);
  4045. if (flags & S_CTIME)
  4046. inode->i_ctime = *now;
  4047. if (flags & S_MTIME)
  4048. inode->i_mtime = *now;
  4049. if (flags & S_ATIME)
  4050. inode->i_atime = *now;
  4051. return btrfs_dirty_inode(inode);
  4052. }
  4053. /*
  4054. * find the highest existing sequence number in a directory
  4055. * and then set the in-memory index_cnt variable to reflect
  4056. * free sequence numbers
  4057. */
  4058. static int btrfs_set_inode_index_count(struct inode *inode)
  4059. {
  4060. struct btrfs_root *root = BTRFS_I(inode)->root;
  4061. struct btrfs_key key, found_key;
  4062. struct btrfs_path *path;
  4063. struct extent_buffer *leaf;
  4064. int ret;
  4065. key.objectid = btrfs_ino(inode);
  4066. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  4067. key.offset = (u64)-1;
  4068. path = btrfs_alloc_path();
  4069. if (!path)
  4070. return -ENOMEM;
  4071. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4072. if (ret < 0)
  4073. goto out;
  4074. /* FIXME: we should be able to handle this */
  4075. if (ret == 0)
  4076. goto out;
  4077. ret = 0;
  4078. /*
  4079. * MAGIC NUMBER EXPLANATION:
  4080. * since we search a directory based on f_pos we have to start at 2
  4081. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  4082. * else has to start at 2
  4083. */
  4084. if (path->slots[0] == 0) {
  4085. BTRFS_I(inode)->index_cnt = 2;
  4086. goto out;
  4087. }
  4088. path->slots[0]--;
  4089. leaf = path->nodes[0];
  4090. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4091. if (found_key.objectid != btrfs_ino(inode) ||
  4092. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  4093. BTRFS_I(inode)->index_cnt = 2;
  4094. goto out;
  4095. }
  4096. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  4097. out:
  4098. btrfs_free_path(path);
  4099. return ret;
  4100. }
  4101. /*
  4102. * helper to find a free sequence number in a given directory. This current
  4103. * code is very simple, later versions will do smarter things in the btree
  4104. */
  4105. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  4106. {
  4107. int ret = 0;
  4108. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  4109. ret = btrfs_inode_delayed_dir_index_count(dir);
  4110. if (ret) {
  4111. ret = btrfs_set_inode_index_count(dir);
  4112. if (ret)
  4113. return ret;
  4114. }
  4115. }
  4116. *index = BTRFS_I(dir)->index_cnt;
  4117. BTRFS_I(dir)->index_cnt++;
  4118. return ret;
  4119. }
  4120. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  4121. struct btrfs_root *root,
  4122. struct inode *dir,
  4123. const char *name, int name_len,
  4124. u64 ref_objectid, u64 objectid,
  4125. umode_t mode, u64 *index)
  4126. {
  4127. struct inode *inode;
  4128. struct btrfs_inode_item *inode_item;
  4129. struct btrfs_key *location;
  4130. struct btrfs_path *path;
  4131. struct btrfs_inode_ref *ref;
  4132. struct btrfs_key key[2];
  4133. u32 sizes[2];
  4134. unsigned long ptr;
  4135. int ret;
  4136. int owner;
  4137. path = btrfs_alloc_path();
  4138. if (!path)
  4139. return ERR_PTR(-ENOMEM);
  4140. inode = new_inode(root->fs_info->sb);
  4141. if (!inode) {
  4142. btrfs_free_path(path);
  4143. return ERR_PTR(-ENOMEM);
  4144. }
  4145. /*
  4146. * we have to initialize this early, so we can reclaim the inode
  4147. * number if we fail afterwards in this function.
  4148. */
  4149. inode->i_ino = objectid;
  4150. if (dir) {
  4151. trace_btrfs_inode_request(dir);
  4152. ret = btrfs_set_inode_index(dir, index);
  4153. if (ret) {
  4154. btrfs_free_path(path);
  4155. iput(inode);
  4156. return ERR_PTR(ret);
  4157. }
  4158. }
  4159. /*
  4160. * index_cnt is ignored for everything but a dir,
  4161. * btrfs_get_inode_index_count has an explanation for the magic
  4162. * number
  4163. */
  4164. BTRFS_I(inode)->index_cnt = 2;
  4165. BTRFS_I(inode)->root = root;
  4166. BTRFS_I(inode)->generation = trans->transid;
  4167. inode->i_generation = BTRFS_I(inode)->generation;
  4168. /*
  4169. * We could have gotten an inode number from somebody who was fsynced
  4170. * and then removed in this same transaction, so let's just set full
  4171. * sync since it will be a full sync anyway and this will blow away the
  4172. * old info in the log.
  4173. */
  4174. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  4175. if (S_ISDIR(mode))
  4176. owner = 0;
  4177. else
  4178. owner = 1;
  4179. key[0].objectid = objectid;
  4180. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  4181. key[0].offset = 0;
  4182. /*
  4183. * Start new inodes with an inode_ref. This is slightly more
  4184. * efficient for small numbers of hard links since they will
  4185. * be packed into one item. Extended refs will kick in if we
  4186. * add more hard links than can fit in the ref item.
  4187. */
  4188. key[1].objectid = objectid;
  4189. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  4190. key[1].offset = ref_objectid;
  4191. sizes[0] = sizeof(struct btrfs_inode_item);
  4192. sizes[1] = name_len + sizeof(*ref);
  4193. path->leave_spinning = 1;
  4194. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  4195. if (ret != 0)
  4196. goto fail;
  4197. inode_init_owner(inode, dir, mode);
  4198. inode_set_bytes(inode, 0);
  4199. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  4200. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  4201. struct btrfs_inode_item);
  4202. memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
  4203. sizeof(*inode_item));
  4204. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  4205. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  4206. struct btrfs_inode_ref);
  4207. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  4208. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  4209. ptr = (unsigned long)(ref + 1);
  4210. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  4211. btrfs_mark_buffer_dirty(path->nodes[0]);
  4212. btrfs_free_path(path);
  4213. location = &BTRFS_I(inode)->location;
  4214. location->objectid = objectid;
  4215. location->offset = 0;
  4216. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  4217. btrfs_inherit_iflags(inode, dir);
  4218. if (S_ISREG(mode)) {
  4219. if (btrfs_test_opt(root, NODATASUM))
  4220. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  4221. if (btrfs_test_opt(root, NODATACOW) ||
  4222. (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
  4223. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  4224. }
  4225. insert_inode_hash(inode);
  4226. inode_tree_add(inode);
  4227. trace_btrfs_inode_new(inode);
  4228. btrfs_set_inode_last_trans(trans, inode);
  4229. btrfs_update_root_times(trans, root);
  4230. return inode;
  4231. fail:
  4232. if (dir)
  4233. BTRFS_I(dir)->index_cnt--;
  4234. btrfs_free_path(path);
  4235. iput(inode);
  4236. return ERR_PTR(ret);
  4237. }
  4238. static inline u8 btrfs_inode_type(struct inode *inode)
  4239. {
  4240. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  4241. }
  4242. /*
  4243. * utility function to add 'inode' into 'parent_inode' with
  4244. * a give name and a given sequence number.
  4245. * if 'add_backref' is true, also insert a backref from the
  4246. * inode to the parent directory.
  4247. */
  4248. int btrfs_add_link(struct btrfs_trans_handle *trans,
  4249. struct inode *parent_inode, struct inode *inode,
  4250. const char *name, int name_len, int add_backref, u64 index)
  4251. {
  4252. int ret = 0;
  4253. struct btrfs_key key;
  4254. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  4255. u64 ino = btrfs_ino(inode);
  4256. u64 parent_ino = btrfs_ino(parent_inode);
  4257. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4258. memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
  4259. } else {
  4260. key.objectid = ino;
  4261. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  4262. key.offset = 0;
  4263. }
  4264. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4265. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  4266. key.objectid, root->root_key.objectid,
  4267. parent_ino, index, name, name_len);
  4268. } else if (add_backref) {
  4269. ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
  4270. parent_ino, index);
  4271. }
  4272. /* Nothing to clean up yet */
  4273. if (ret)
  4274. return ret;
  4275. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  4276. parent_inode, &key,
  4277. btrfs_inode_type(inode), index);
  4278. if (ret == -EEXIST)
  4279. goto fail_dir_item;
  4280. else if (ret) {
  4281. btrfs_abort_transaction(trans, root, ret);
  4282. return ret;
  4283. }
  4284. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  4285. name_len * 2);
  4286. inode_inc_iversion(parent_inode);
  4287. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  4288. ret = btrfs_update_inode(trans, root, parent_inode);
  4289. if (ret)
  4290. btrfs_abort_transaction(trans, root, ret);
  4291. return ret;
  4292. fail_dir_item:
  4293. if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4294. u64 local_index;
  4295. int err;
  4296. err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  4297. key.objectid, root->root_key.objectid,
  4298. parent_ino, &local_index, name, name_len);
  4299. } else if (add_backref) {
  4300. u64 local_index;
  4301. int err;
  4302. err = btrfs_del_inode_ref(trans, root, name, name_len,
  4303. ino, parent_ino, &local_index);
  4304. }
  4305. return ret;
  4306. }
  4307. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  4308. struct inode *dir, struct dentry *dentry,
  4309. struct inode *inode, int backref, u64 index)
  4310. {
  4311. int err = btrfs_add_link(trans, dir, inode,
  4312. dentry->d_name.name, dentry->d_name.len,
  4313. backref, index);
  4314. if (err > 0)
  4315. err = -EEXIST;
  4316. return err;
  4317. }
  4318. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  4319. umode_t mode, dev_t rdev)
  4320. {
  4321. struct btrfs_trans_handle *trans;
  4322. struct btrfs_root *root = BTRFS_I(dir)->root;
  4323. struct inode *inode = NULL;
  4324. int err;
  4325. int drop_inode = 0;
  4326. u64 objectid;
  4327. u64 index = 0;
  4328. if (!new_valid_dev(rdev))
  4329. return -EINVAL;
  4330. /*
  4331. * 2 for inode item and ref
  4332. * 2 for dir items
  4333. * 1 for xattr if selinux is on
  4334. */
  4335. trans = btrfs_start_transaction(root, 5);
  4336. if (IS_ERR(trans))
  4337. return PTR_ERR(trans);
  4338. err = btrfs_find_free_ino(root, &objectid);
  4339. if (err)
  4340. goto out_unlock;
  4341. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4342. dentry->d_name.len, btrfs_ino(dir), objectid,
  4343. mode, &index);
  4344. if (IS_ERR(inode)) {
  4345. err = PTR_ERR(inode);
  4346. goto out_unlock;
  4347. }
  4348. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  4349. if (err) {
  4350. drop_inode = 1;
  4351. goto out_unlock;
  4352. }
  4353. err = btrfs_update_inode(trans, root, inode);
  4354. if (err) {
  4355. drop_inode = 1;
  4356. goto out_unlock;
  4357. }
  4358. /*
  4359. * If the active LSM wants to access the inode during
  4360. * d_instantiate it needs these. Smack checks to see
  4361. * if the filesystem supports xattrs by looking at the
  4362. * ops vector.
  4363. */
  4364. inode->i_op = &btrfs_special_inode_operations;
  4365. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  4366. if (err)
  4367. drop_inode = 1;
  4368. else {
  4369. init_special_inode(inode, inode->i_mode, rdev);
  4370. btrfs_update_inode(trans, root, inode);
  4371. d_instantiate(dentry, inode);
  4372. }
  4373. out_unlock:
  4374. btrfs_end_transaction(trans, root);
  4375. btrfs_btree_balance_dirty(root);
  4376. if (drop_inode) {
  4377. inode_dec_link_count(inode);
  4378. iput(inode);
  4379. }
  4380. return err;
  4381. }
  4382. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  4383. umode_t mode, bool excl)
  4384. {
  4385. struct btrfs_trans_handle *trans;
  4386. struct btrfs_root *root = BTRFS_I(dir)->root;
  4387. struct inode *inode = NULL;
  4388. int drop_inode_on_err = 0;
  4389. int err;
  4390. u64 objectid;
  4391. u64 index = 0;
  4392. /*
  4393. * 2 for inode item and ref
  4394. * 2 for dir items
  4395. * 1 for xattr if selinux is on
  4396. */
  4397. trans = btrfs_start_transaction(root, 5);
  4398. if (IS_ERR(trans))
  4399. return PTR_ERR(trans);
  4400. err = btrfs_find_free_ino(root, &objectid);
  4401. if (err)
  4402. goto out_unlock;
  4403. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4404. dentry->d_name.len, btrfs_ino(dir), objectid,
  4405. mode, &index);
  4406. if (IS_ERR(inode)) {
  4407. err = PTR_ERR(inode);
  4408. goto out_unlock;
  4409. }
  4410. drop_inode_on_err = 1;
  4411. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  4412. if (err)
  4413. goto out_unlock;
  4414. /*
  4415. * If the active LSM wants to access the inode during
  4416. * d_instantiate it needs these. Smack checks to see
  4417. * if the filesystem supports xattrs by looking at the
  4418. * ops vector.
  4419. */
  4420. inode->i_fop = &btrfs_file_operations;
  4421. inode->i_op = &btrfs_file_inode_operations;
  4422. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  4423. if (err)
  4424. goto out_unlock;
  4425. inode->i_mapping->a_ops = &btrfs_aops;
  4426. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4427. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  4428. d_instantiate(dentry, inode);
  4429. out_unlock:
  4430. btrfs_end_transaction(trans, root);
  4431. if (err && drop_inode_on_err) {
  4432. inode_dec_link_count(inode);
  4433. iput(inode);
  4434. }
  4435. btrfs_btree_balance_dirty(root);
  4436. return err;
  4437. }
  4438. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  4439. struct dentry *dentry)
  4440. {
  4441. struct btrfs_trans_handle *trans;
  4442. struct btrfs_root *root = BTRFS_I(dir)->root;
  4443. struct inode *inode = old_dentry->d_inode;
  4444. u64 index;
  4445. int err;
  4446. int drop_inode = 0;
  4447. /* do not allow sys_link's with other subvols of the same device */
  4448. if (root->objectid != BTRFS_I(inode)->root->objectid)
  4449. return -EXDEV;
  4450. if (inode->i_nlink >= BTRFS_LINK_MAX)
  4451. return -EMLINK;
  4452. err = btrfs_set_inode_index(dir, &index);
  4453. if (err)
  4454. goto fail;
  4455. /*
  4456. * 2 items for inode and inode ref
  4457. * 2 items for dir items
  4458. * 1 item for parent inode
  4459. */
  4460. trans = btrfs_start_transaction(root, 5);
  4461. if (IS_ERR(trans)) {
  4462. err = PTR_ERR(trans);
  4463. goto fail;
  4464. }
  4465. btrfs_inc_nlink(inode);
  4466. inode_inc_iversion(inode);
  4467. inode->i_ctime = CURRENT_TIME;
  4468. ihold(inode);
  4469. err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
  4470. if (err) {
  4471. drop_inode = 1;
  4472. } else {
  4473. struct dentry *parent = dentry->d_parent;
  4474. err = btrfs_update_inode(trans, root, inode);
  4475. if (err)
  4476. goto fail;
  4477. d_instantiate(dentry, inode);
  4478. btrfs_log_new_name(trans, inode, NULL, parent);
  4479. }
  4480. btrfs_end_transaction(trans, root);
  4481. fail:
  4482. if (drop_inode) {
  4483. inode_dec_link_count(inode);
  4484. iput(inode);
  4485. }
  4486. btrfs_btree_balance_dirty(root);
  4487. return err;
  4488. }
  4489. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  4490. {
  4491. struct inode *inode = NULL;
  4492. struct btrfs_trans_handle *trans;
  4493. struct btrfs_root *root = BTRFS_I(dir)->root;
  4494. int err = 0;
  4495. int drop_on_err = 0;
  4496. u64 objectid = 0;
  4497. u64 index = 0;
  4498. /*
  4499. * 2 items for inode and ref
  4500. * 2 items for dir items
  4501. * 1 for xattr if selinux is on
  4502. */
  4503. trans = btrfs_start_transaction(root, 5);
  4504. if (IS_ERR(trans))
  4505. return PTR_ERR(trans);
  4506. err = btrfs_find_free_ino(root, &objectid);
  4507. if (err)
  4508. goto out_fail;
  4509. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4510. dentry->d_name.len, btrfs_ino(dir), objectid,
  4511. S_IFDIR | mode, &index);
  4512. if (IS_ERR(inode)) {
  4513. err = PTR_ERR(inode);
  4514. goto out_fail;
  4515. }
  4516. drop_on_err = 1;
  4517. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  4518. if (err)
  4519. goto out_fail;
  4520. inode->i_op = &btrfs_dir_inode_operations;
  4521. inode->i_fop = &btrfs_dir_file_operations;
  4522. btrfs_i_size_write(inode, 0);
  4523. err = btrfs_update_inode(trans, root, inode);
  4524. if (err)
  4525. goto out_fail;
  4526. err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
  4527. dentry->d_name.len, 0, index);
  4528. if (err)
  4529. goto out_fail;
  4530. d_instantiate(dentry, inode);
  4531. drop_on_err = 0;
  4532. out_fail:
  4533. btrfs_end_transaction(trans, root);
  4534. if (drop_on_err)
  4535. iput(inode);
  4536. btrfs_btree_balance_dirty(root);
  4537. return err;
  4538. }
  4539. /* helper for btfs_get_extent. Given an existing extent in the tree,
  4540. * and an extent that you want to insert, deal with overlap and insert
  4541. * the new extent into the tree.
  4542. */
  4543. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  4544. struct extent_map *existing,
  4545. struct extent_map *em,
  4546. u64 map_start, u64 map_len)
  4547. {
  4548. u64 start_diff;
  4549. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  4550. start_diff = map_start - em->start;
  4551. em->start = map_start;
  4552. em->len = map_len;
  4553. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  4554. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  4555. em->block_start += start_diff;
  4556. em->block_len -= start_diff;
  4557. }
  4558. return add_extent_mapping(em_tree, em);
  4559. }
  4560. static noinline int uncompress_inline(struct btrfs_path *path,
  4561. struct inode *inode, struct page *page,
  4562. size_t pg_offset, u64 extent_offset,
  4563. struct btrfs_file_extent_item *item)
  4564. {
  4565. int ret;
  4566. struct extent_buffer *leaf = path->nodes[0];
  4567. char *tmp;
  4568. size_t max_size;
  4569. unsigned long inline_size;
  4570. unsigned long ptr;
  4571. int compress_type;
  4572. WARN_ON(pg_offset != 0);
  4573. compress_type = btrfs_file_extent_compression(leaf, item);
  4574. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  4575. inline_size = btrfs_file_extent_inline_item_len(leaf,
  4576. btrfs_item_nr(leaf, path->slots[0]));
  4577. tmp = kmalloc(inline_size, GFP_NOFS);
  4578. if (!tmp)
  4579. return -ENOMEM;
  4580. ptr = btrfs_file_extent_inline_start(item);
  4581. read_extent_buffer(leaf, tmp, ptr, inline_size);
  4582. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  4583. ret = btrfs_decompress(compress_type, tmp, page,
  4584. extent_offset, inline_size, max_size);
  4585. if (ret) {
  4586. char *kaddr = kmap_atomic(page);
  4587. unsigned long copy_size = min_t(u64,
  4588. PAGE_CACHE_SIZE - pg_offset,
  4589. max_size - extent_offset);
  4590. memset(kaddr + pg_offset, 0, copy_size);
  4591. kunmap_atomic(kaddr);
  4592. }
  4593. kfree(tmp);
  4594. return 0;
  4595. }
  4596. /*
  4597. * a bit scary, this does extent mapping from logical file offset to the disk.
  4598. * the ugly parts come from merging extents from the disk with the in-ram
  4599. * representation. This gets more complex because of the data=ordered code,
  4600. * where the in-ram extents might be locked pending data=ordered completion.
  4601. *
  4602. * This also copies inline extents directly into the page.
  4603. */
  4604. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  4605. size_t pg_offset, u64 start, u64 len,
  4606. int create)
  4607. {
  4608. int ret;
  4609. int err = 0;
  4610. u64 bytenr;
  4611. u64 extent_start = 0;
  4612. u64 extent_end = 0;
  4613. u64 objectid = btrfs_ino(inode);
  4614. u32 found_type;
  4615. struct btrfs_path *path = NULL;
  4616. struct btrfs_root *root = BTRFS_I(inode)->root;
  4617. struct btrfs_file_extent_item *item;
  4618. struct extent_buffer *leaf;
  4619. struct btrfs_key found_key;
  4620. struct extent_map *em = NULL;
  4621. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  4622. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4623. struct btrfs_trans_handle *trans = NULL;
  4624. int compress_type;
  4625. again:
  4626. read_lock(&em_tree->lock);
  4627. em = lookup_extent_mapping(em_tree, start, len);
  4628. if (em)
  4629. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4630. read_unlock(&em_tree->lock);
  4631. if (em) {
  4632. if (em->start > start || em->start + em->len <= start)
  4633. free_extent_map(em);
  4634. else if (em->block_start == EXTENT_MAP_INLINE && page)
  4635. free_extent_map(em);
  4636. else
  4637. goto out;
  4638. }
  4639. em = alloc_extent_map();
  4640. if (!em) {
  4641. err = -ENOMEM;
  4642. goto out;
  4643. }
  4644. em->bdev = root->fs_info->fs_devices->latest_bdev;
  4645. em->start = EXTENT_MAP_HOLE;
  4646. em->orig_start = EXTENT_MAP_HOLE;
  4647. em->len = (u64)-1;
  4648. em->block_len = (u64)-1;
  4649. if (!path) {
  4650. path = btrfs_alloc_path();
  4651. if (!path) {
  4652. err = -ENOMEM;
  4653. goto out;
  4654. }
  4655. /*
  4656. * Chances are we'll be called again, so go ahead and do
  4657. * readahead
  4658. */
  4659. path->reada = 1;
  4660. }
  4661. ret = btrfs_lookup_file_extent(trans, root, path,
  4662. objectid, start, trans != NULL);
  4663. if (ret < 0) {
  4664. err = ret;
  4665. goto out;
  4666. }
  4667. if (ret != 0) {
  4668. if (path->slots[0] == 0)
  4669. goto not_found;
  4670. path->slots[0]--;
  4671. }
  4672. leaf = path->nodes[0];
  4673. item = btrfs_item_ptr(leaf, path->slots[0],
  4674. struct btrfs_file_extent_item);
  4675. /* are we inside the extent that was found? */
  4676. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4677. found_type = btrfs_key_type(&found_key);
  4678. if (found_key.objectid != objectid ||
  4679. found_type != BTRFS_EXTENT_DATA_KEY) {
  4680. goto not_found;
  4681. }
  4682. found_type = btrfs_file_extent_type(leaf, item);
  4683. extent_start = found_key.offset;
  4684. compress_type = btrfs_file_extent_compression(leaf, item);
  4685. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4686. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4687. extent_end = extent_start +
  4688. btrfs_file_extent_num_bytes(leaf, item);
  4689. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4690. size_t size;
  4691. size = btrfs_file_extent_inline_len(leaf, item);
  4692. extent_end = (extent_start + size + root->sectorsize - 1) &
  4693. ~((u64)root->sectorsize - 1);
  4694. }
  4695. if (start >= extent_end) {
  4696. path->slots[0]++;
  4697. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  4698. ret = btrfs_next_leaf(root, path);
  4699. if (ret < 0) {
  4700. err = ret;
  4701. goto out;
  4702. }
  4703. if (ret > 0)
  4704. goto not_found;
  4705. leaf = path->nodes[0];
  4706. }
  4707. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4708. if (found_key.objectid != objectid ||
  4709. found_key.type != BTRFS_EXTENT_DATA_KEY)
  4710. goto not_found;
  4711. if (start + len <= found_key.offset)
  4712. goto not_found;
  4713. em->start = start;
  4714. em->len = found_key.offset - start;
  4715. goto not_found_em;
  4716. }
  4717. if (found_type == BTRFS_FILE_EXTENT_REG ||
  4718. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  4719. em->start = extent_start;
  4720. em->len = extent_end - extent_start;
  4721. em->orig_start = extent_start -
  4722. btrfs_file_extent_offset(leaf, item);
  4723. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  4724. if (bytenr == 0) {
  4725. em->block_start = EXTENT_MAP_HOLE;
  4726. goto insert;
  4727. }
  4728. if (compress_type != BTRFS_COMPRESS_NONE) {
  4729. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4730. em->compress_type = compress_type;
  4731. em->block_start = bytenr;
  4732. em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
  4733. item);
  4734. } else {
  4735. bytenr += btrfs_file_extent_offset(leaf, item);
  4736. em->block_start = bytenr;
  4737. em->block_len = em->len;
  4738. if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
  4739. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  4740. }
  4741. goto insert;
  4742. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4743. unsigned long ptr;
  4744. char *map;
  4745. size_t size;
  4746. size_t extent_offset;
  4747. size_t copy_size;
  4748. em->block_start = EXTENT_MAP_INLINE;
  4749. if (!page || create) {
  4750. em->start = extent_start;
  4751. em->len = extent_end - extent_start;
  4752. goto out;
  4753. }
  4754. size = btrfs_file_extent_inline_len(leaf, item);
  4755. extent_offset = page_offset(page) + pg_offset - extent_start;
  4756. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  4757. size - extent_offset);
  4758. em->start = extent_start + extent_offset;
  4759. em->len = (copy_size + root->sectorsize - 1) &
  4760. ~((u64)root->sectorsize - 1);
  4761. em->orig_start = EXTENT_MAP_INLINE;
  4762. if (compress_type) {
  4763. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4764. em->compress_type = compress_type;
  4765. }
  4766. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  4767. if (create == 0 && !PageUptodate(page)) {
  4768. if (btrfs_file_extent_compression(leaf, item) !=
  4769. BTRFS_COMPRESS_NONE) {
  4770. ret = uncompress_inline(path, inode, page,
  4771. pg_offset,
  4772. extent_offset, item);
  4773. BUG_ON(ret); /* -ENOMEM */
  4774. } else {
  4775. map = kmap(page);
  4776. read_extent_buffer(leaf, map + pg_offset, ptr,
  4777. copy_size);
  4778. if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
  4779. memset(map + pg_offset + copy_size, 0,
  4780. PAGE_CACHE_SIZE - pg_offset -
  4781. copy_size);
  4782. }
  4783. kunmap(page);
  4784. }
  4785. flush_dcache_page(page);
  4786. } else if (create && PageUptodate(page)) {
  4787. BUG();
  4788. if (!trans) {
  4789. kunmap(page);
  4790. free_extent_map(em);
  4791. em = NULL;
  4792. btrfs_release_path(path);
  4793. trans = btrfs_join_transaction(root);
  4794. if (IS_ERR(trans))
  4795. return ERR_CAST(trans);
  4796. goto again;
  4797. }
  4798. map = kmap(page);
  4799. write_extent_buffer(leaf, map + pg_offset, ptr,
  4800. copy_size);
  4801. kunmap(page);
  4802. btrfs_mark_buffer_dirty(leaf);
  4803. }
  4804. set_extent_uptodate(io_tree, em->start,
  4805. extent_map_end(em) - 1, NULL, GFP_NOFS);
  4806. goto insert;
  4807. } else {
  4808. WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
  4809. }
  4810. not_found:
  4811. em->start = start;
  4812. em->len = len;
  4813. not_found_em:
  4814. em->block_start = EXTENT_MAP_HOLE;
  4815. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  4816. insert:
  4817. btrfs_release_path(path);
  4818. if (em->start > start || extent_map_end(em) <= start) {
  4819. printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
  4820. "[%llu %llu]\n", (unsigned long long)em->start,
  4821. (unsigned long long)em->len,
  4822. (unsigned long long)start,
  4823. (unsigned long long)len);
  4824. err = -EIO;
  4825. goto out;
  4826. }
  4827. err = 0;
  4828. write_lock(&em_tree->lock);
  4829. ret = add_extent_mapping(em_tree, em);
  4830. /* it is possible that someone inserted the extent into the tree
  4831. * while we had the lock dropped. It is also possible that
  4832. * an overlapping map exists in the tree
  4833. */
  4834. if (ret == -EEXIST) {
  4835. struct extent_map *existing;
  4836. ret = 0;
  4837. existing = lookup_extent_mapping(em_tree, start, len);
  4838. if (existing && (existing->start > start ||
  4839. existing->start + existing->len <= start)) {
  4840. free_extent_map(existing);
  4841. existing = NULL;
  4842. }
  4843. if (!existing) {
  4844. existing = lookup_extent_mapping(em_tree, em->start,
  4845. em->len);
  4846. if (existing) {
  4847. err = merge_extent_mapping(em_tree, existing,
  4848. em, start,
  4849. root->sectorsize);
  4850. free_extent_map(existing);
  4851. if (err) {
  4852. free_extent_map(em);
  4853. em = NULL;
  4854. }
  4855. } else {
  4856. err = -EIO;
  4857. free_extent_map(em);
  4858. em = NULL;
  4859. }
  4860. } else {
  4861. free_extent_map(em);
  4862. em = existing;
  4863. err = 0;
  4864. }
  4865. }
  4866. write_unlock(&em_tree->lock);
  4867. out:
  4868. if (em)
  4869. trace_btrfs_get_extent(root, em);
  4870. if (path)
  4871. btrfs_free_path(path);
  4872. if (trans) {
  4873. ret = btrfs_end_transaction(trans, root);
  4874. if (!err)
  4875. err = ret;
  4876. }
  4877. if (err) {
  4878. free_extent_map(em);
  4879. return ERR_PTR(err);
  4880. }
  4881. BUG_ON(!em); /* Error is always set */
  4882. return em;
  4883. }
  4884. struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
  4885. size_t pg_offset, u64 start, u64 len,
  4886. int create)
  4887. {
  4888. struct extent_map *em;
  4889. struct extent_map *hole_em = NULL;
  4890. u64 range_start = start;
  4891. u64 end;
  4892. u64 found;
  4893. u64 found_end;
  4894. int err = 0;
  4895. em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
  4896. if (IS_ERR(em))
  4897. return em;
  4898. if (em) {
  4899. /*
  4900. * if our em maps to a hole, there might
  4901. * actually be delalloc bytes behind it
  4902. */
  4903. if (em->block_start != EXTENT_MAP_HOLE)
  4904. return em;
  4905. else
  4906. hole_em = em;
  4907. }
  4908. /* check to see if we've wrapped (len == -1 or similar) */
  4909. end = start + len;
  4910. if (end < start)
  4911. end = (u64)-1;
  4912. else
  4913. end -= 1;
  4914. em = NULL;
  4915. /* ok, we didn't find anything, lets look for delalloc */
  4916. found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
  4917. end, len, EXTENT_DELALLOC, 1);
  4918. found_end = range_start + found;
  4919. if (found_end < range_start)
  4920. found_end = (u64)-1;
  4921. /*
  4922. * we didn't find anything useful, return
  4923. * the original results from get_extent()
  4924. */
  4925. if (range_start > end || found_end <= start) {
  4926. em = hole_em;
  4927. hole_em = NULL;
  4928. goto out;
  4929. }
  4930. /* adjust the range_start to make sure it doesn't
  4931. * go backwards from the start they passed in
  4932. */
  4933. range_start = max(start,range_start);
  4934. found = found_end - range_start;
  4935. if (found > 0) {
  4936. u64 hole_start = start;
  4937. u64 hole_len = len;
  4938. em = alloc_extent_map();
  4939. if (!em) {
  4940. err = -ENOMEM;
  4941. goto out;
  4942. }
  4943. /*
  4944. * when btrfs_get_extent can't find anything it
  4945. * returns one huge hole
  4946. *
  4947. * make sure what it found really fits our range, and
  4948. * adjust to make sure it is based on the start from
  4949. * the caller
  4950. */
  4951. if (hole_em) {
  4952. u64 calc_end = extent_map_end(hole_em);
  4953. if (calc_end <= start || (hole_em->start > end)) {
  4954. free_extent_map(hole_em);
  4955. hole_em = NULL;
  4956. } else {
  4957. hole_start = max(hole_em->start, start);
  4958. hole_len = calc_end - hole_start;
  4959. }
  4960. }
  4961. em->bdev = NULL;
  4962. if (hole_em && range_start > hole_start) {
  4963. /* our hole starts before our delalloc, so we
  4964. * have to return just the parts of the hole
  4965. * that go until the delalloc starts
  4966. */
  4967. em->len = min(hole_len,
  4968. range_start - hole_start);
  4969. em->start = hole_start;
  4970. em->orig_start = hole_start;
  4971. /*
  4972. * don't adjust block start at all,
  4973. * it is fixed at EXTENT_MAP_HOLE
  4974. */
  4975. em->block_start = hole_em->block_start;
  4976. em->block_len = hole_len;
  4977. } else {
  4978. em->start = range_start;
  4979. em->len = found;
  4980. em->orig_start = range_start;
  4981. em->block_start = EXTENT_MAP_DELALLOC;
  4982. em->block_len = found;
  4983. }
  4984. } else if (hole_em) {
  4985. return hole_em;
  4986. }
  4987. out:
  4988. free_extent_map(hole_em);
  4989. if (err) {
  4990. free_extent_map(em);
  4991. return ERR_PTR(err);
  4992. }
  4993. return em;
  4994. }
  4995. static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
  4996. struct extent_map *em,
  4997. u64 start, u64 len)
  4998. {
  4999. struct btrfs_root *root = BTRFS_I(inode)->root;
  5000. struct btrfs_trans_handle *trans;
  5001. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  5002. struct btrfs_key ins;
  5003. u64 alloc_hint;
  5004. int ret;
  5005. bool insert = false;
  5006. /*
  5007. * Ok if the extent map we looked up is a hole and is for the exact
  5008. * range we want, there is no reason to allocate a new one, however if
  5009. * it is not right then we need to free this one and drop the cache for
  5010. * our range.
  5011. */
  5012. if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
  5013. em->len != len) {
  5014. free_extent_map(em);
  5015. em = NULL;
  5016. insert = true;
  5017. btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
  5018. }
  5019. trans = btrfs_join_transaction(root);
  5020. if (IS_ERR(trans))
  5021. return ERR_CAST(trans);
  5022. trans->block_rsv = &root->fs_info->delalloc_block_rsv;
  5023. alloc_hint = get_extent_allocation_hint(inode, start, len);
  5024. ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
  5025. alloc_hint, &ins, 1);
  5026. if (ret) {
  5027. em = ERR_PTR(ret);
  5028. goto out;
  5029. }
  5030. if (!em) {
  5031. em = alloc_extent_map();
  5032. if (!em) {
  5033. em = ERR_PTR(-ENOMEM);
  5034. goto out;
  5035. }
  5036. }
  5037. em->start = start;
  5038. em->orig_start = em->start;
  5039. em->len = ins.offset;
  5040. em->block_start = ins.objectid;
  5041. em->block_len = ins.offset;
  5042. em->bdev = root->fs_info->fs_devices->latest_bdev;
  5043. /*
  5044. * We need to do this because if we're using the original em we searched
  5045. * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
  5046. */
  5047. em->flags = 0;
  5048. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  5049. while (insert) {
  5050. write_lock(&em_tree->lock);
  5051. ret = add_extent_mapping(em_tree, em);
  5052. write_unlock(&em_tree->lock);
  5053. if (ret != -EEXIST)
  5054. break;
  5055. btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
  5056. }
  5057. ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
  5058. ins.offset, ins.offset, 0);
  5059. if (ret) {
  5060. btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
  5061. em = ERR_PTR(ret);
  5062. }
  5063. out:
  5064. btrfs_end_transaction(trans, root);
  5065. return em;
  5066. }
  5067. /*
  5068. * returns 1 when the nocow is safe, < 1 on error, 0 if the
  5069. * block must be cow'd
  5070. */
  5071. static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
  5072. struct inode *inode, u64 offset, u64 len)
  5073. {
  5074. struct btrfs_path *path;
  5075. int ret;
  5076. struct extent_buffer *leaf;
  5077. struct btrfs_root *root = BTRFS_I(inode)->root;
  5078. struct btrfs_file_extent_item *fi;
  5079. struct btrfs_key key;
  5080. u64 disk_bytenr;
  5081. u64 backref_offset;
  5082. u64 extent_end;
  5083. u64 num_bytes;
  5084. int slot;
  5085. int found_type;
  5086. path = btrfs_alloc_path();
  5087. if (!path)
  5088. return -ENOMEM;
  5089. ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
  5090. offset, 0);
  5091. if (ret < 0)
  5092. goto out;
  5093. slot = path->slots[0];
  5094. if (ret == 1) {
  5095. if (slot == 0) {
  5096. /* can't find the item, must cow */
  5097. ret = 0;
  5098. goto out;
  5099. }
  5100. slot--;
  5101. }
  5102. ret = 0;
  5103. leaf = path->nodes[0];
  5104. btrfs_item_key_to_cpu(leaf, &key, slot);
  5105. if (key.objectid != btrfs_ino(inode) ||
  5106. key.type != BTRFS_EXTENT_DATA_KEY) {
  5107. /* not our file or wrong item type, must cow */
  5108. goto out;
  5109. }
  5110. if (key.offset > offset) {
  5111. /* Wrong offset, must cow */
  5112. goto out;
  5113. }
  5114. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  5115. found_type = btrfs_file_extent_type(leaf, fi);
  5116. if (found_type != BTRFS_FILE_EXTENT_REG &&
  5117. found_type != BTRFS_FILE_EXTENT_PREALLOC) {
  5118. /* not a regular extent, must cow */
  5119. goto out;
  5120. }
  5121. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  5122. backref_offset = btrfs_file_extent_offset(leaf, fi);
  5123. extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
  5124. if (extent_end < offset + len) {
  5125. /* extent doesn't include our full range, must cow */
  5126. goto out;
  5127. }
  5128. if (btrfs_extent_readonly(root, disk_bytenr))
  5129. goto out;
  5130. /*
  5131. * look for other files referencing this extent, if we
  5132. * find any we must cow
  5133. */
  5134. if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
  5135. key.offset - backref_offset, disk_bytenr))
  5136. goto out;
  5137. /*
  5138. * adjust disk_bytenr and num_bytes to cover just the bytes
  5139. * in this extent we are about to write. If there
  5140. * are any csums in that range we have to cow in order
  5141. * to keep the csums correct
  5142. */
  5143. disk_bytenr += backref_offset;
  5144. disk_bytenr += offset - key.offset;
  5145. num_bytes = min(offset + len, extent_end) - offset;
  5146. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  5147. goto out;
  5148. /*
  5149. * all of the above have passed, it is safe to overwrite this extent
  5150. * without cow
  5151. */
  5152. ret = 1;
  5153. out:
  5154. btrfs_free_path(path);
  5155. return ret;
  5156. }
  5157. static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
  5158. struct extent_state **cached_state, int writing)
  5159. {
  5160. struct btrfs_ordered_extent *ordered;
  5161. int ret = 0;
  5162. while (1) {
  5163. lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5164. 0, cached_state);
  5165. /*
  5166. * We're concerned with the entire range that we're going to be
  5167. * doing DIO to, so we need to make sure theres no ordered
  5168. * extents in this range.
  5169. */
  5170. ordered = btrfs_lookup_ordered_range(inode, lockstart,
  5171. lockend - lockstart + 1);
  5172. /*
  5173. * We need to make sure there are no buffered pages in this
  5174. * range either, we could have raced between the invalidate in
  5175. * generic_file_direct_write and locking the extent. The
  5176. * invalidate needs to happen so that reads after a write do not
  5177. * get stale data.
  5178. */
  5179. if (!ordered && (!writing ||
  5180. !test_range_bit(&BTRFS_I(inode)->io_tree,
  5181. lockstart, lockend, EXTENT_UPTODATE, 0,
  5182. *cached_state)))
  5183. break;
  5184. unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5185. cached_state, GFP_NOFS);
  5186. if (ordered) {
  5187. btrfs_start_ordered_extent(inode, ordered, 1);
  5188. btrfs_put_ordered_extent(ordered);
  5189. } else {
  5190. /* Screw you mmap */
  5191. ret = filemap_write_and_wait_range(inode->i_mapping,
  5192. lockstart,
  5193. lockend);
  5194. if (ret)
  5195. break;
  5196. /*
  5197. * If we found a page that couldn't be invalidated just
  5198. * fall back to buffered.
  5199. */
  5200. ret = invalidate_inode_pages2_range(inode->i_mapping,
  5201. lockstart >> PAGE_CACHE_SHIFT,
  5202. lockend >> PAGE_CACHE_SHIFT);
  5203. if (ret)
  5204. break;
  5205. }
  5206. cond_resched();
  5207. }
  5208. return ret;
  5209. }
  5210. static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
  5211. u64 len, u64 orig_start,
  5212. u64 block_start, u64 block_len,
  5213. int type)
  5214. {
  5215. struct extent_map_tree *em_tree;
  5216. struct extent_map *em;
  5217. struct btrfs_root *root = BTRFS_I(inode)->root;
  5218. int ret;
  5219. em_tree = &BTRFS_I(inode)->extent_tree;
  5220. em = alloc_extent_map();
  5221. if (!em)
  5222. return ERR_PTR(-ENOMEM);
  5223. em->start = start;
  5224. em->orig_start = orig_start;
  5225. em->len = len;
  5226. em->block_len = block_len;
  5227. em->block_start = block_start;
  5228. em->bdev = root->fs_info->fs_devices->latest_bdev;
  5229. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  5230. if (type == BTRFS_ORDERED_PREALLOC)
  5231. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  5232. do {
  5233. btrfs_drop_extent_cache(inode, em->start,
  5234. em->start + em->len - 1, 0);
  5235. write_lock(&em_tree->lock);
  5236. ret = add_extent_mapping(em_tree, em);
  5237. write_unlock(&em_tree->lock);
  5238. } while (ret == -EEXIST);
  5239. if (ret) {
  5240. free_extent_map(em);
  5241. return ERR_PTR(ret);
  5242. }
  5243. return em;
  5244. }
  5245. static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
  5246. struct buffer_head *bh_result, int create)
  5247. {
  5248. struct extent_map *em;
  5249. struct btrfs_root *root = BTRFS_I(inode)->root;
  5250. struct extent_state *cached_state = NULL;
  5251. u64 start = iblock << inode->i_blkbits;
  5252. u64 lockstart, lockend;
  5253. u64 len = bh_result->b_size;
  5254. struct btrfs_trans_handle *trans;
  5255. int unlock_bits = EXTENT_LOCKED;
  5256. int ret;
  5257. if (create) {
  5258. ret = btrfs_delalloc_reserve_space(inode, len);
  5259. if (ret)
  5260. return ret;
  5261. unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
  5262. } else {
  5263. len = min_t(u64, len, root->sectorsize);
  5264. }
  5265. lockstart = start;
  5266. lockend = start + len - 1;
  5267. /*
  5268. * If this errors out it's because we couldn't invalidate pagecache for
  5269. * this range and we need to fallback to buffered.
  5270. */
  5271. if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
  5272. return -ENOTBLK;
  5273. if (create) {
  5274. ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  5275. lockend, EXTENT_DELALLOC, NULL,
  5276. &cached_state, GFP_NOFS);
  5277. if (ret)
  5278. goto unlock_err;
  5279. }
  5280. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  5281. if (IS_ERR(em)) {
  5282. ret = PTR_ERR(em);
  5283. goto unlock_err;
  5284. }
  5285. /*
  5286. * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
  5287. * io. INLINE is special, and we could probably kludge it in here, but
  5288. * it's still buffered so for safety lets just fall back to the generic
  5289. * buffered path.
  5290. *
  5291. * For COMPRESSED we _have_ to read the entire extent in so we can
  5292. * decompress it, so there will be buffering required no matter what we
  5293. * do, so go ahead and fallback to buffered.
  5294. *
  5295. * We return -ENOTBLK because thats what makes DIO go ahead and go back
  5296. * to buffered IO. Don't blame me, this is the price we pay for using
  5297. * the generic code.
  5298. */
  5299. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
  5300. em->block_start == EXTENT_MAP_INLINE) {
  5301. free_extent_map(em);
  5302. ret = -ENOTBLK;
  5303. goto unlock_err;
  5304. }
  5305. /* Just a good old fashioned hole, return */
  5306. if (!create && (em->block_start == EXTENT_MAP_HOLE ||
  5307. test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
  5308. free_extent_map(em);
  5309. ret = 0;
  5310. goto unlock_err;
  5311. }
  5312. /*
  5313. * We don't allocate a new extent in the following cases
  5314. *
  5315. * 1) The inode is marked as NODATACOW. In this case we'll just use the
  5316. * existing extent.
  5317. * 2) The extent is marked as PREALLOC. We're good to go here and can
  5318. * just use the extent.
  5319. *
  5320. */
  5321. if (!create) {
  5322. len = min(len, em->len - (start - em->start));
  5323. lockstart = start + len;
  5324. goto unlock;
  5325. }
  5326. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
  5327. ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
  5328. em->block_start != EXTENT_MAP_HOLE)) {
  5329. int type;
  5330. int ret;
  5331. u64 block_start;
  5332. if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  5333. type = BTRFS_ORDERED_PREALLOC;
  5334. else
  5335. type = BTRFS_ORDERED_NOCOW;
  5336. len = min(len, em->len - (start - em->start));
  5337. block_start = em->block_start + (start - em->start);
  5338. /*
  5339. * we're not going to log anything, but we do need
  5340. * to make sure the current transaction stays open
  5341. * while we look for nocow cross refs
  5342. */
  5343. trans = btrfs_join_transaction(root);
  5344. if (IS_ERR(trans))
  5345. goto must_cow;
  5346. if (can_nocow_odirect(trans, inode, start, len) == 1) {
  5347. u64 orig_start = em->start;
  5348. if (type == BTRFS_ORDERED_PREALLOC) {
  5349. free_extent_map(em);
  5350. em = create_pinned_em(inode, start, len,
  5351. orig_start,
  5352. block_start, len, type);
  5353. if (IS_ERR(em)) {
  5354. btrfs_end_transaction(trans, root);
  5355. goto unlock_err;
  5356. }
  5357. }
  5358. ret = btrfs_add_ordered_extent_dio(inode, start,
  5359. block_start, len, len, type);
  5360. btrfs_end_transaction(trans, root);
  5361. if (ret) {
  5362. free_extent_map(em);
  5363. goto unlock_err;
  5364. }
  5365. goto unlock;
  5366. }
  5367. btrfs_end_transaction(trans, root);
  5368. }
  5369. must_cow:
  5370. /*
  5371. * this will cow the extent, reset the len in case we changed
  5372. * it above
  5373. */
  5374. len = bh_result->b_size;
  5375. em = btrfs_new_extent_direct(inode, em, start, len);
  5376. if (IS_ERR(em)) {
  5377. ret = PTR_ERR(em);
  5378. goto unlock_err;
  5379. }
  5380. len = min(len, em->len - (start - em->start));
  5381. unlock:
  5382. bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
  5383. inode->i_blkbits;
  5384. bh_result->b_size = len;
  5385. bh_result->b_bdev = em->bdev;
  5386. set_buffer_mapped(bh_result);
  5387. if (create) {
  5388. if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
  5389. set_buffer_new(bh_result);
  5390. /*
  5391. * Need to update the i_size under the extent lock so buffered
  5392. * readers will get the updated i_size when we unlock.
  5393. */
  5394. if (start + len > i_size_read(inode))
  5395. i_size_write(inode, start + len);
  5396. }
  5397. /*
  5398. * In the case of write we need to clear and unlock the entire range,
  5399. * in the case of read we need to unlock only the end area that we
  5400. * aren't using if there is any left over space.
  5401. */
  5402. if (lockstart < lockend) {
  5403. if (create && len < lockend - lockstart) {
  5404. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  5405. lockstart + len - 1,
  5406. unlock_bits | EXTENT_DEFRAG, 1, 0,
  5407. &cached_state, GFP_NOFS);
  5408. /*
  5409. * Beside unlock, we also need to cleanup reserved space
  5410. * for the left range by attaching EXTENT_DO_ACCOUNTING.
  5411. */
  5412. clear_extent_bit(&BTRFS_I(inode)->io_tree,
  5413. lockstart + len, lockend,
  5414. unlock_bits | EXTENT_DO_ACCOUNTING |
  5415. EXTENT_DEFRAG, 1, 0, NULL, GFP_NOFS);
  5416. } else {
  5417. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
  5418. lockend, unlock_bits, 1, 0,
  5419. &cached_state, GFP_NOFS);
  5420. }
  5421. } else {
  5422. free_extent_state(cached_state);
  5423. }
  5424. free_extent_map(em);
  5425. return 0;
  5426. unlock_err:
  5427. if (create)
  5428. unlock_bits |= EXTENT_DO_ACCOUNTING;
  5429. clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
  5430. unlock_bits, 1, 0, &cached_state, GFP_NOFS);
  5431. return ret;
  5432. }
  5433. struct btrfs_dio_private {
  5434. struct inode *inode;
  5435. u64 logical_offset;
  5436. u64 disk_bytenr;
  5437. u64 bytes;
  5438. void *private;
  5439. /* number of bios pending for this dio */
  5440. atomic_t pending_bios;
  5441. /* IO errors */
  5442. int errors;
  5443. struct bio *orig_bio;
  5444. };
  5445. static void btrfs_endio_direct_read(struct bio *bio, int err)
  5446. {
  5447. struct btrfs_dio_private *dip = bio->bi_private;
  5448. struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
  5449. struct bio_vec *bvec = bio->bi_io_vec;
  5450. struct inode *inode = dip->inode;
  5451. struct btrfs_root *root = BTRFS_I(inode)->root;
  5452. u64 start;
  5453. start = dip->logical_offset;
  5454. do {
  5455. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
  5456. struct page *page = bvec->bv_page;
  5457. char *kaddr;
  5458. u32 csum = ~(u32)0;
  5459. u64 private = ~(u32)0;
  5460. unsigned long flags;
  5461. if (get_state_private(&BTRFS_I(inode)->io_tree,
  5462. start, &private))
  5463. goto failed;
  5464. local_irq_save(flags);
  5465. kaddr = kmap_atomic(page);
  5466. csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
  5467. csum, bvec->bv_len);
  5468. btrfs_csum_final(csum, (char *)&csum);
  5469. kunmap_atomic(kaddr);
  5470. local_irq_restore(flags);
  5471. flush_dcache_page(bvec->bv_page);
  5472. if (csum != private) {
  5473. failed:
  5474. printk(KERN_ERR "btrfs csum failed ino %llu off"
  5475. " %llu csum %u private %u\n",
  5476. (unsigned long long)btrfs_ino(inode),
  5477. (unsigned long long)start,
  5478. csum, (unsigned)private);
  5479. err = -EIO;
  5480. }
  5481. }
  5482. start += bvec->bv_len;
  5483. bvec++;
  5484. } while (bvec <= bvec_end);
  5485. unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
  5486. dip->logical_offset + dip->bytes - 1);
  5487. bio->bi_private = dip->private;
  5488. kfree(dip);
  5489. /* If we had a csum failure make sure to clear the uptodate flag */
  5490. if (err)
  5491. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  5492. dio_end_io(bio, err);
  5493. }
  5494. static void btrfs_endio_direct_write(struct bio *bio, int err)
  5495. {
  5496. struct btrfs_dio_private *dip = bio->bi_private;
  5497. struct inode *inode = dip->inode;
  5498. struct btrfs_root *root = BTRFS_I(inode)->root;
  5499. struct btrfs_ordered_extent *ordered = NULL;
  5500. u64 ordered_offset = dip->logical_offset;
  5501. u64 ordered_bytes = dip->bytes;
  5502. int ret;
  5503. if (err)
  5504. goto out_done;
  5505. again:
  5506. ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
  5507. &ordered_offset,
  5508. ordered_bytes, !err);
  5509. if (!ret)
  5510. goto out_test;
  5511. ordered->work.func = finish_ordered_fn;
  5512. ordered->work.flags = 0;
  5513. btrfs_queue_worker(&root->fs_info->endio_write_workers,
  5514. &ordered->work);
  5515. out_test:
  5516. /*
  5517. * our bio might span multiple ordered extents. If we haven't
  5518. * completed the accounting for the whole dio, go back and try again
  5519. */
  5520. if (ordered_offset < dip->logical_offset + dip->bytes) {
  5521. ordered_bytes = dip->logical_offset + dip->bytes -
  5522. ordered_offset;
  5523. ordered = NULL;
  5524. goto again;
  5525. }
  5526. out_done:
  5527. bio->bi_private = dip->private;
  5528. kfree(dip);
  5529. /* If we had an error make sure to clear the uptodate flag */
  5530. if (err)
  5531. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  5532. dio_end_io(bio, err);
  5533. }
  5534. static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
  5535. struct bio *bio, int mirror_num,
  5536. unsigned long bio_flags, u64 offset)
  5537. {
  5538. int ret;
  5539. struct btrfs_root *root = BTRFS_I(inode)->root;
  5540. ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
  5541. BUG_ON(ret); /* -ENOMEM */
  5542. return 0;
  5543. }
  5544. static void btrfs_end_dio_bio(struct bio *bio, int err)
  5545. {
  5546. struct btrfs_dio_private *dip = bio->bi_private;
  5547. if (err) {
  5548. printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
  5549. "sector %#Lx len %u err no %d\n",
  5550. (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
  5551. (unsigned long long)bio->bi_sector, bio->bi_size, err);
  5552. dip->errors = 1;
  5553. /*
  5554. * before atomic variable goto zero, we must make sure
  5555. * dip->errors is perceived to be set.
  5556. */
  5557. smp_mb__before_atomic_dec();
  5558. }
  5559. /* if there are more bios still pending for this dio, just exit */
  5560. if (!atomic_dec_and_test(&dip->pending_bios))
  5561. goto out;
  5562. if (dip->errors)
  5563. bio_io_error(dip->orig_bio);
  5564. else {
  5565. set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
  5566. bio_endio(dip->orig_bio, 0);
  5567. }
  5568. out:
  5569. bio_put(bio);
  5570. }
  5571. static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
  5572. u64 first_sector, gfp_t gfp_flags)
  5573. {
  5574. int nr_vecs = bio_get_nr_vecs(bdev);
  5575. return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
  5576. }
  5577. static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
  5578. int rw, u64 file_offset, int skip_sum,
  5579. int async_submit)
  5580. {
  5581. int write = rw & REQ_WRITE;
  5582. struct btrfs_root *root = BTRFS_I(inode)->root;
  5583. int ret;
  5584. bio_get(bio);
  5585. if (!write) {
  5586. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  5587. if (ret)
  5588. goto err;
  5589. }
  5590. if (skip_sum)
  5591. goto map;
  5592. if (write && async_submit) {
  5593. ret = btrfs_wq_submit_bio(root->fs_info,
  5594. inode, rw, bio, 0, 0,
  5595. file_offset,
  5596. __btrfs_submit_bio_start_direct_io,
  5597. __btrfs_submit_bio_done);
  5598. goto err;
  5599. } else if (write) {
  5600. /*
  5601. * If we aren't doing async submit, calculate the csum of the
  5602. * bio now.
  5603. */
  5604. ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
  5605. if (ret)
  5606. goto err;
  5607. } else if (!skip_sum) {
  5608. ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
  5609. if (ret)
  5610. goto err;
  5611. }
  5612. map:
  5613. ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
  5614. err:
  5615. bio_put(bio);
  5616. return ret;
  5617. }
  5618. static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
  5619. int skip_sum)
  5620. {
  5621. struct inode *inode = dip->inode;
  5622. struct btrfs_root *root = BTRFS_I(inode)->root;
  5623. struct bio *bio;
  5624. struct bio *orig_bio = dip->orig_bio;
  5625. struct bio_vec *bvec = orig_bio->bi_io_vec;
  5626. u64 start_sector = orig_bio->bi_sector;
  5627. u64 file_offset = dip->logical_offset;
  5628. u64 submit_len = 0;
  5629. u64 map_length;
  5630. int nr_pages = 0;
  5631. int ret = 0;
  5632. int async_submit = 0;
  5633. map_length = orig_bio->bi_size;
  5634. ret = btrfs_map_block(root->fs_info, READ, start_sector << 9,
  5635. &map_length, NULL, 0);
  5636. if (ret) {
  5637. bio_put(orig_bio);
  5638. return -EIO;
  5639. }
  5640. if (map_length >= orig_bio->bi_size) {
  5641. bio = orig_bio;
  5642. goto submit;
  5643. }
  5644. async_submit = 1;
  5645. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
  5646. if (!bio)
  5647. return -ENOMEM;
  5648. bio->bi_private = dip;
  5649. bio->bi_end_io = btrfs_end_dio_bio;
  5650. atomic_inc(&dip->pending_bios);
  5651. while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
  5652. if (unlikely(map_length < submit_len + bvec->bv_len ||
  5653. bio_add_page(bio, bvec->bv_page, bvec->bv_len,
  5654. bvec->bv_offset) < bvec->bv_len)) {
  5655. /*
  5656. * inc the count before we submit the bio so
  5657. * we know the end IO handler won't happen before
  5658. * we inc the count. Otherwise, the dip might get freed
  5659. * before we're done setting it up
  5660. */
  5661. atomic_inc(&dip->pending_bios);
  5662. ret = __btrfs_submit_dio_bio(bio, inode, rw,
  5663. file_offset, skip_sum,
  5664. async_submit);
  5665. if (ret) {
  5666. bio_put(bio);
  5667. atomic_dec(&dip->pending_bios);
  5668. goto out_err;
  5669. }
  5670. start_sector += submit_len >> 9;
  5671. file_offset += submit_len;
  5672. submit_len = 0;
  5673. nr_pages = 0;
  5674. bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
  5675. start_sector, GFP_NOFS);
  5676. if (!bio)
  5677. goto out_err;
  5678. bio->bi_private = dip;
  5679. bio->bi_end_io = btrfs_end_dio_bio;
  5680. map_length = orig_bio->bi_size;
  5681. ret = btrfs_map_block(root->fs_info, READ,
  5682. start_sector << 9,
  5683. &map_length, NULL, 0);
  5684. if (ret) {
  5685. bio_put(bio);
  5686. goto out_err;
  5687. }
  5688. } else {
  5689. submit_len += bvec->bv_len;
  5690. nr_pages ++;
  5691. bvec++;
  5692. }
  5693. }
  5694. submit:
  5695. ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
  5696. async_submit);
  5697. if (!ret)
  5698. return 0;
  5699. bio_put(bio);
  5700. out_err:
  5701. dip->errors = 1;
  5702. /*
  5703. * before atomic variable goto zero, we must
  5704. * make sure dip->errors is perceived to be set.
  5705. */
  5706. smp_mb__before_atomic_dec();
  5707. if (atomic_dec_and_test(&dip->pending_bios))
  5708. bio_io_error(dip->orig_bio);
  5709. /* bio_end_io() will handle error, so we needn't return it */
  5710. return 0;
  5711. }
  5712. static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
  5713. loff_t file_offset)
  5714. {
  5715. struct btrfs_root *root = BTRFS_I(inode)->root;
  5716. struct btrfs_dio_private *dip;
  5717. struct bio_vec *bvec = bio->bi_io_vec;
  5718. int skip_sum;
  5719. int write = rw & REQ_WRITE;
  5720. int ret = 0;
  5721. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  5722. dip = kmalloc(sizeof(*dip), GFP_NOFS);
  5723. if (!dip) {
  5724. ret = -ENOMEM;
  5725. goto free_ordered;
  5726. }
  5727. dip->private = bio->bi_private;
  5728. dip->inode = inode;
  5729. dip->logical_offset = file_offset;
  5730. dip->bytes = 0;
  5731. do {
  5732. dip->bytes += bvec->bv_len;
  5733. bvec++;
  5734. } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
  5735. dip->disk_bytenr = (u64)bio->bi_sector << 9;
  5736. bio->bi_private = dip;
  5737. dip->errors = 0;
  5738. dip->orig_bio = bio;
  5739. atomic_set(&dip->pending_bios, 0);
  5740. if (write)
  5741. bio->bi_end_io = btrfs_endio_direct_write;
  5742. else
  5743. bio->bi_end_io = btrfs_endio_direct_read;
  5744. ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
  5745. if (!ret)
  5746. return;
  5747. free_ordered:
  5748. /*
  5749. * If this is a write, we need to clean up the reserved space and kill
  5750. * the ordered extent.
  5751. */
  5752. if (write) {
  5753. struct btrfs_ordered_extent *ordered;
  5754. ordered = btrfs_lookup_ordered_extent(inode, file_offset);
  5755. if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
  5756. !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
  5757. btrfs_free_reserved_extent(root, ordered->start,
  5758. ordered->disk_len);
  5759. btrfs_put_ordered_extent(ordered);
  5760. btrfs_put_ordered_extent(ordered);
  5761. }
  5762. bio_endio(bio, ret);
  5763. }
  5764. static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
  5765. const struct iovec *iov, loff_t offset,
  5766. unsigned long nr_segs)
  5767. {
  5768. int seg;
  5769. int i;
  5770. size_t size;
  5771. unsigned long addr;
  5772. unsigned blocksize_mask = root->sectorsize - 1;
  5773. ssize_t retval = -EINVAL;
  5774. loff_t end = offset;
  5775. if (offset & blocksize_mask)
  5776. goto out;
  5777. /* Check the memory alignment. Blocks cannot straddle pages */
  5778. for (seg = 0; seg < nr_segs; seg++) {
  5779. addr = (unsigned long)iov[seg].iov_base;
  5780. size = iov[seg].iov_len;
  5781. end += size;
  5782. if ((addr & blocksize_mask) || (size & blocksize_mask))
  5783. goto out;
  5784. /* If this is a write we don't need to check anymore */
  5785. if (rw & WRITE)
  5786. continue;
  5787. /*
  5788. * Check to make sure we don't have duplicate iov_base's in this
  5789. * iovec, if so return EINVAL, otherwise we'll get csum errors
  5790. * when reading back.
  5791. */
  5792. for (i = seg + 1; i < nr_segs; i++) {
  5793. if (iov[seg].iov_base == iov[i].iov_base)
  5794. goto out;
  5795. }
  5796. }
  5797. retval = 0;
  5798. out:
  5799. return retval;
  5800. }
  5801. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  5802. const struct iovec *iov, loff_t offset,
  5803. unsigned long nr_segs)
  5804. {
  5805. struct file *file = iocb->ki_filp;
  5806. struct inode *inode = file->f_mapping->host;
  5807. if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
  5808. offset, nr_segs))
  5809. return 0;
  5810. return __blockdev_direct_IO(rw, iocb, inode,
  5811. BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
  5812. iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
  5813. btrfs_submit_direct, 0);
  5814. }
  5815. #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
  5816. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  5817. __u64 start, __u64 len)
  5818. {
  5819. int ret;
  5820. ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
  5821. if (ret)
  5822. return ret;
  5823. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
  5824. }
  5825. int btrfs_readpage(struct file *file, struct page *page)
  5826. {
  5827. struct extent_io_tree *tree;
  5828. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5829. return extent_read_full_page(tree, page, btrfs_get_extent, 0);
  5830. }
  5831. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  5832. {
  5833. struct extent_io_tree *tree;
  5834. if (current->flags & PF_MEMALLOC) {
  5835. redirty_page_for_writepage(wbc, page);
  5836. unlock_page(page);
  5837. return 0;
  5838. }
  5839. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5840. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  5841. }
  5842. int btrfs_writepages(struct address_space *mapping,
  5843. struct writeback_control *wbc)
  5844. {
  5845. struct extent_io_tree *tree;
  5846. tree = &BTRFS_I(mapping->host)->io_tree;
  5847. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  5848. }
  5849. static int
  5850. btrfs_readpages(struct file *file, struct address_space *mapping,
  5851. struct list_head *pages, unsigned nr_pages)
  5852. {
  5853. struct extent_io_tree *tree;
  5854. tree = &BTRFS_I(mapping->host)->io_tree;
  5855. return extent_readpages(tree, mapping, pages, nr_pages,
  5856. btrfs_get_extent);
  5857. }
  5858. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  5859. {
  5860. struct extent_io_tree *tree;
  5861. struct extent_map_tree *map;
  5862. int ret;
  5863. tree = &BTRFS_I(page->mapping->host)->io_tree;
  5864. map = &BTRFS_I(page->mapping->host)->extent_tree;
  5865. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  5866. if (ret == 1) {
  5867. ClearPagePrivate(page);
  5868. set_page_private(page, 0);
  5869. page_cache_release(page);
  5870. }
  5871. return ret;
  5872. }
  5873. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  5874. {
  5875. if (PageWriteback(page) || PageDirty(page))
  5876. return 0;
  5877. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  5878. }
  5879. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  5880. {
  5881. struct inode *inode = page->mapping->host;
  5882. struct extent_io_tree *tree;
  5883. struct btrfs_ordered_extent *ordered;
  5884. struct extent_state *cached_state = NULL;
  5885. u64 page_start = page_offset(page);
  5886. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  5887. /*
  5888. * we have the page locked, so new writeback can't start,
  5889. * and the dirty bit won't be cleared while we are here.
  5890. *
  5891. * Wait for IO on this page so that we can safely clear
  5892. * the PagePrivate2 bit and do ordered accounting
  5893. */
  5894. wait_on_page_writeback(page);
  5895. tree = &BTRFS_I(inode)->io_tree;
  5896. if (offset) {
  5897. btrfs_releasepage(page, GFP_NOFS);
  5898. return;
  5899. }
  5900. lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
  5901. ordered = btrfs_lookup_ordered_extent(inode,
  5902. page_offset(page));
  5903. if (ordered) {
  5904. /*
  5905. * IO on this page will never be started, so we need
  5906. * to account for any ordered extents now
  5907. */
  5908. clear_extent_bit(tree, page_start, page_end,
  5909. EXTENT_DIRTY | EXTENT_DELALLOC |
  5910. EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
  5911. EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
  5912. /*
  5913. * whoever cleared the private bit is responsible
  5914. * for the finish_ordered_io
  5915. */
  5916. if (TestClearPagePrivate2(page) &&
  5917. btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
  5918. PAGE_CACHE_SIZE, 1)) {
  5919. btrfs_finish_ordered_io(ordered);
  5920. }
  5921. btrfs_put_ordered_extent(ordered);
  5922. cached_state = NULL;
  5923. lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
  5924. }
  5925. clear_extent_bit(tree, page_start, page_end,
  5926. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  5927. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
  5928. &cached_state, GFP_NOFS);
  5929. __btrfs_releasepage(page, GFP_NOFS);
  5930. ClearPageChecked(page);
  5931. if (PagePrivate(page)) {
  5932. ClearPagePrivate(page);
  5933. set_page_private(page, 0);
  5934. page_cache_release(page);
  5935. }
  5936. }
  5937. /*
  5938. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  5939. * called from a page fault handler when a page is first dirtied. Hence we must
  5940. * be careful to check for EOF conditions here. We set the page up correctly
  5941. * for a written page which means we get ENOSPC checking when writing into
  5942. * holes and correct delalloc and unwritten extent mapping on filesystems that
  5943. * support these features.
  5944. *
  5945. * We are not allowed to take the i_mutex here so we have to play games to
  5946. * protect against truncate races as the page could now be beyond EOF. Because
  5947. * vmtruncate() writes the inode size before removing pages, once we have the
  5948. * page lock we can determine safely if the page is beyond EOF. If it is not
  5949. * beyond EOF, then the page is guaranteed safe against truncation until we
  5950. * unlock the page.
  5951. */
  5952. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  5953. {
  5954. struct page *page = vmf->page;
  5955. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  5956. struct btrfs_root *root = BTRFS_I(inode)->root;
  5957. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  5958. struct btrfs_ordered_extent *ordered;
  5959. struct extent_state *cached_state = NULL;
  5960. char *kaddr;
  5961. unsigned long zero_start;
  5962. loff_t size;
  5963. int ret;
  5964. int reserved = 0;
  5965. u64 page_start;
  5966. u64 page_end;
  5967. sb_start_pagefault(inode->i_sb);
  5968. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  5969. if (!ret) {
  5970. ret = file_update_time(vma->vm_file);
  5971. reserved = 1;
  5972. }
  5973. if (ret) {
  5974. if (ret == -ENOMEM)
  5975. ret = VM_FAULT_OOM;
  5976. else /* -ENOSPC, -EIO, etc */
  5977. ret = VM_FAULT_SIGBUS;
  5978. if (reserved)
  5979. goto out;
  5980. goto out_noreserve;
  5981. }
  5982. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  5983. again:
  5984. lock_page(page);
  5985. size = i_size_read(inode);
  5986. page_start = page_offset(page);
  5987. page_end = page_start + PAGE_CACHE_SIZE - 1;
  5988. if ((page->mapping != inode->i_mapping) ||
  5989. (page_start >= size)) {
  5990. /* page got truncated out from underneath us */
  5991. goto out_unlock;
  5992. }
  5993. wait_on_page_writeback(page);
  5994. lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
  5995. set_page_extent_mapped(page);
  5996. /*
  5997. * we can't set the delalloc bits if there are pending ordered
  5998. * extents. Drop our locks and wait for them to finish
  5999. */
  6000. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  6001. if (ordered) {
  6002. unlock_extent_cached(io_tree, page_start, page_end,
  6003. &cached_state, GFP_NOFS);
  6004. unlock_page(page);
  6005. btrfs_start_ordered_extent(inode, ordered, 1);
  6006. btrfs_put_ordered_extent(ordered);
  6007. goto again;
  6008. }
  6009. /*
  6010. * XXX - page_mkwrite gets called every time the page is dirtied, even
  6011. * if it was already dirty, so for space accounting reasons we need to
  6012. * clear any delalloc bits for the range we are fixing to save. There
  6013. * is probably a better way to do this, but for now keep consistent with
  6014. * prepare_pages in the normal write path.
  6015. */
  6016. clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  6017. EXTENT_DIRTY | EXTENT_DELALLOC |
  6018. EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
  6019. 0, 0, &cached_state, GFP_NOFS);
  6020. ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
  6021. &cached_state);
  6022. if (ret) {
  6023. unlock_extent_cached(io_tree, page_start, page_end,
  6024. &cached_state, GFP_NOFS);
  6025. ret = VM_FAULT_SIGBUS;
  6026. goto out_unlock;
  6027. }
  6028. ret = 0;
  6029. /* page is wholly or partially inside EOF */
  6030. if (page_start + PAGE_CACHE_SIZE > size)
  6031. zero_start = size & ~PAGE_CACHE_MASK;
  6032. else
  6033. zero_start = PAGE_CACHE_SIZE;
  6034. if (zero_start != PAGE_CACHE_SIZE) {
  6035. kaddr = kmap(page);
  6036. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  6037. flush_dcache_page(page);
  6038. kunmap(page);
  6039. }
  6040. ClearPageChecked(page);
  6041. set_page_dirty(page);
  6042. SetPageUptodate(page);
  6043. BTRFS_I(inode)->last_trans = root->fs_info->generation;
  6044. BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
  6045. BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
  6046. unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
  6047. out_unlock:
  6048. if (!ret) {
  6049. sb_end_pagefault(inode->i_sb);
  6050. return VM_FAULT_LOCKED;
  6051. }
  6052. unlock_page(page);
  6053. out:
  6054. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  6055. out_noreserve:
  6056. sb_end_pagefault(inode->i_sb);
  6057. return ret;
  6058. }
  6059. static int btrfs_truncate(struct inode *inode)
  6060. {
  6061. struct btrfs_root *root = BTRFS_I(inode)->root;
  6062. struct btrfs_block_rsv *rsv;
  6063. int ret;
  6064. int err = 0;
  6065. struct btrfs_trans_handle *trans;
  6066. u64 mask = root->sectorsize - 1;
  6067. u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
  6068. ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
  6069. if (ret)
  6070. return ret;
  6071. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  6072. btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
  6073. /*
  6074. * Yes ladies and gentelment, this is indeed ugly. The fact is we have
  6075. * 3 things going on here
  6076. *
  6077. * 1) We need to reserve space for our orphan item and the space to
  6078. * delete our orphan item. Lord knows we don't want to have a dangling
  6079. * orphan item because we didn't reserve space to remove it.
  6080. *
  6081. * 2) We need to reserve space to update our inode.
  6082. *
  6083. * 3) We need to have something to cache all the space that is going to
  6084. * be free'd up by the truncate operation, but also have some slack
  6085. * space reserved in case it uses space during the truncate (thank you
  6086. * very much snapshotting).
  6087. *
  6088. * And we need these to all be seperate. The fact is we can use alot of
  6089. * space doing the truncate, and we have no earthly idea how much space
  6090. * we will use, so we need the truncate reservation to be seperate so it
  6091. * doesn't end up using space reserved for updating the inode or
  6092. * removing the orphan item. We also need to be able to stop the
  6093. * transaction and start a new one, which means we need to be able to
  6094. * update the inode several times, and we have no idea of knowing how
  6095. * many times that will be, so we can't just reserve 1 item for the
  6096. * entirety of the opration, so that has to be done seperately as well.
  6097. * Then there is the orphan item, which does indeed need to be held on
  6098. * to for the whole operation, and we need nobody to touch this reserved
  6099. * space except the orphan code.
  6100. *
  6101. * So that leaves us with
  6102. *
  6103. * 1) root->orphan_block_rsv - for the orphan deletion.
  6104. * 2) rsv - for the truncate reservation, which we will steal from the
  6105. * transaction reservation.
  6106. * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
  6107. * updating the inode.
  6108. */
  6109. rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
  6110. if (!rsv)
  6111. return -ENOMEM;
  6112. rsv->size = min_size;
  6113. rsv->failfast = 1;
  6114. /*
  6115. * 1 for the truncate slack space
  6116. * 1 for the orphan item we're going to add
  6117. * 1 for the orphan item deletion
  6118. * 1 for updating the inode.
  6119. */
  6120. trans = btrfs_start_transaction(root, 4);
  6121. if (IS_ERR(trans)) {
  6122. err = PTR_ERR(trans);
  6123. goto out;
  6124. }
  6125. /* Migrate the slack space for the truncate to our reserve */
  6126. ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
  6127. min_size);
  6128. BUG_ON(ret);
  6129. ret = btrfs_orphan_add(trans, inode);
  6130. if (ret) {
  6131. btrfs_end_transaction(trans, root);
  6132. goto out;
  6133. }
  6134. /*
  6135. * setattr is responsible for setting the ordered_data_close flag,
  6136. * but that is only tested during the last file release. That
  6137. * could happen well after the next commit, leaving a great big
  6138. * window where new writes may get lost if someone chooses to write
  6139. * to this file after truncating to zero
  6140. *
  6141. * The inode doesn't have any dirty data here, and so if we commit
  6142. * this is a noop. If someone immediately starts writing to the inode
  6143. * it is very likely we'll catch some of their writes in this
  6144. * transaction, and the commit will find this file on the ordered
  6145. * data list with good things to send down.
  6146. *
  6147. * This is a best effort solution, there is still a window where
  6148. * using truncate to replace the contents of the file will
  6149. * end up with a zero length file after a crash.
  6150. */
  6151. if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
  6152. &BTRFS_I(inode)->runtime_flags))
  6153. btrfs_add_ordered_operation(trans, root, inode);
  6154. /*
  6155. * So if we truncate and then write and fsync we normally would just
  6156. * write the extents that changed, which is a problem if we need to
  6157. * first truncate that entire inode. So set this flag so we write out
  6158. * all of the extents in the inode to the sync log so we're completely
  6159. * safe.
  6160. */
  6161. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
  6162. trans->block_rsv = rsv;
  6163. while (1) {
  6164. ret = btrfs_truncate_inode_items(trans, root, inode,
  6165. inode->i_size,
  6166. BTRFS_EXTENT_DATA_KEY);
  6167. if (ret != -ENOSPC) {
  6168. err = ret;
  6169. break;
  6170. }
  6171. trans->block_rsv = &root->fs_info->trans_block_rsv;
  6172. ret = btrfs_update_inode(trans, root, inode);
  6173. if (ret) {
  6174. err = ret;
  6175. break;
  6176. }
  6177. btrfs_end_transaction(trans, root);
  6178. btrfs_btree_balance_dirty(root);
  6179. trans = btrfs_start_transaction(root, 2);
  6180. if (IS_ERR(trans)) {
  6181. ret = err = PTR_ERR(trans);
  6182. trans = NULL;
  6183. break;
  6184. }
  6185. ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
  6186. rsv, min_size);
  6187. BUG_ON(ret); /* shouldn't happen */
  6188. trans->block_rsv = rsv;
  6189. }
  6190. if (ret == 0 && inode->i_nlink > 0) {
  6191. trans->block_rsv = root->orphan_block_rsv;
  6192. ret = btrfs_orphan_del(trans, inode);
  6193. if (ret)
  6194. err = ret;
  6195. } else if (ret && inode->i_nlink > 0) {
  6196. /*
  6197. * Failed to do the truncate, remove us from the in memory
  6198. * orphan list.
  6199. */
  6200. ret = btrfs_orphan_del(NULL, inode);
  6201. }
  6202. if (trans) {
  6203. trans->block_rsv = &root->fs_info->trans_block_rsv;
  6204. ret = btrfs_update_inode(trans, root, inode);
  6205. if (ret && !err)
  6206. err = ret;
  6207. ret = btrfs_end_transaction(trans, root);
  6208. btrfs_btree_balance_dirty(root);
  6209. }
  6210. out:
  6211. btrfs_free_block_rsv(root, rsv);
  6212. if (ret && !err)
  6213. err = ret;
  6214. return err;
  6215. }
  6216. /*
  6217. * create a new subvolume directory/inode (helper for the ioctl).
  6218. */
  6219. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  6220. struct btrfs_root *new_root, u64 new_dirid)
  6221. {
  6222. struct inode *inode;
  6223. int err;
  6224. u64 index = 0;
  6225. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
  6226. new_dirid, new_dirid,
  6227. S_IFDIR | (~current_umask() & S_IRWXUGO),
  6228. &index);
  6229. if (IS_ERR(inode))
  6230. return PTR_ERR(inode);
  6231. inode->i_op = &btrfs_dir_inode_operations;
  6232. inode->i_fop = &btrfs_dir_file_operations;
  6233. set_nlink(inode, 1);
  6234. btrfs_i_size_write(inode, 0);
  6235. err = btrfs_update_inode(trans, new_root, inode);
  6236. iput(inode);
  6237. return err;
  6238. }
  6239. struct inode *btrfs_alloc_inode(struct super_block *sb)
  6240. {
  6241. struct btrfs_inode *ei;
  6242. struct inode *inode;
  6243. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  6244. if (!ei)
  6245. return NULL;
  6246. ei->root = NULL;
  6247. ei->generation = 0;
  6248. ei->last_trans = 0;
  6249. ei->last_sub_trans = 0;
  6250. ei->logged_trans = 0;
  6251. ei->delalloc_bytes = 0;
  6252. ei->disk_i_size = 0;
  6253. ei->flags = 0;
  6254. ei->csum_bytes = 0;
  6255. ei->index_cnt = (u64)-1;
  6256. ei->last_unlink_trans = 0;
  6257. ei->last_log_commit = 0;
  6258. spin_lock_init(&ei->lock);
  6259. ei->outstanding_extents = 0;
  6260. ei->reserved_extents = 0;
  6261. ei->runtime_flags = 0;
  6262. ei->force_compress = BTRFS_COMPRESS_NONE;
  6263. ei->delayed_node = NULL;
  6264. inode = &ei->vfs_inode;
  6265. extent_map_tree_init(&ei->extent_tree);
  6266. extent_io_tree_init(&ei->io_tree, &inode->i_data);
  6267. extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
  6268. ei->io_tree.track_uptodate = 1;
  6269. ei->io_failure_tree.track_uptodate = 1;
  6270. mutex_init(&ei->log_mutex);
  6271. mutex_init(&ei->delalloc_mutex);
  6272. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  6273. INIT_LIST_HEAD(&ei->delalloc_inodes);
  6274. INIT_LIST_HEAD(&ei->ordered_operations);
  6275. RB_CLEAR_NODE(&ei->rb_node);
  6276. return inode;
  6277. }
  6278. static void btrfs_i_callback(struct rcu_head *head)
  6279. {
  6280. struct inode *inode = container_of(head, struct inode, i_rcu);
  6281. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  6282. }
  6283. void btrfs_destroy_inode(struct inode *inode)
  6284. {
  6285. struct btrfs_ordered_extent *ordered;
  6286. struct btrfs_root *root = BTRFS_I(inode)->root;
  6287. WARN_ON(!hlist_empty(&inode->i_dentry));
  6288. WARN_ON(inode->i_data.nrpages);
  6289. WARN_ON(BTRFS_I(inode)->outstanding_extents);
  6290. WARN_ON(BTRFS_I(inode)->reserved_extents);
  6291. WARN_ON(BTRFS_I(inode)->delalloc_bytes);
  6292. WARN_ON(BTRFS_I(inode)->csum_bytes);
  6293. /*
  6294. * This can happen where we create an inode, but somebody else also
  6295. * created the same inode and we need to destroy the one we already
  6296. * created.
  6297. */
  6298. if (!root)
  6299. goto free;
  6300. /*
  6301. * Make sure we're properly removed from the ordered operation
  6302. * lists.
  6303. */
  6304. smp_mb();
  6305. if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
  6306. spin_lock(&root->fs_info->ordered_extent_lock);
  6307. list_del_init(&BTRFS_I(inode)->ordered_operations);
  6308. spin_unlock(&root->fs_info->ordered_extent_lock);
  6309. }
  6310. if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
  6311. &BTRFS_I(inode)->runtime_flags)) {
  6312. printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
  6313. (unsigned long long)btrfs_ino(inode));
  6314. atomic_dec(&root->orphan_inodes);
  6315. }
  6316. while (1) {
  6317. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  6318. if (!ordered)
  6319. break;
  6320. else {
  6321. printk(KERN_ERR "btrfs found ordered "
  6322. "extent %llu %llu on inode cleanup\n",
  6323. (unsigned long long)ordered->file_offset,
  6324. (unsigned long long)ordered->len);
  6325. btrfs_remove_ordered_extent(inode, ordered);
  6326. btrfs_put_ordered_extent(ordered);
  6327. btrfs_put_ordered_extent(ordered);
  6328. }
  6329. }
  6330. inode_tree_del(inode);
  6331. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  6332. free:
  6333. btrfs_remove_delayed_node(inode);
  6334. call_rcu(&inode->i_rcu, btrfs_i_callback);
  6335. }
  6336. int btrfs_drop_inode(struct inode *inode)
  6337. {
  6338. struct btrfs_root *root = BTRFS_I(inode)->root;
  6339. if (btrfs_root_refs(&root->root_item) == 0 &&
  6340. !btrfs_is_free_space_inode(inode))
  6341. return 1;
  6342. else
  6343. return generic_drop_inode(inode);
  6344. }
  6345. static void init_once(void *foo)
  6346. {
  6347. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  6348. inode_init_once(&ei->vfs_inode);
  6349. }
  6350. void btrfs_destroy_cachep(void)
  6351. {
  6352. /*
  6353. * Make sure all delayed rcu free inodes are flushed before we
  6354. * destroy cache.
  6355. */
  6356. rcu_barrier();
  6357. if (btrfs_inode_cachep)
  6358. kmem_cache_destroy(btrfs_inode_cachep);
  6359. if (btrfs_trans_handle_cachep)
  6360. kmem_cache_destroy(btrfs_trans_handle_cachep);
  6361. if (btrfs_transaction_cachep)
  6362. kmem_cache_destroy(btrfs_transaction_cachep);
  6363. if (btrfs_path_cachep)
  6364. kmem_cache_destroy(btrfs_path_cachep);
  6365. if (btrfs_free_space_cachep)
  6366. kmem_cache_destroy(btrfs_free_space_cachep);
  6367. if (btrfs_delalloc_work_cachep)
  6368. kmem_cache_destroy(btrfs_delalloc_work_cachep);
  6369. }
  6370. int btrfs_init_cachep(void)
  6371. {
  6372. btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
  6373. sizeof(struct btrfs_inode), 0,
  6374. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
  6375. if (!btrfs_inode_cachep)
  6376. goto fail;
  6377. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
  6378. sizeof(struct btrfs_trans_handle), 0,
  6379. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  6380. if (!btrfs_trans_handle_cachep)
  6381. goto fail;
  6382. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
  6383. sizeof(struct btrfs_transaction), 0,
  6384. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  6385. if (!btrfs_transaction_cachep)
  6386. goto fail;
  6387. btrfs_path_cachep = kmem_cache_create("btrfs_path",
  6388. sizeof(struct btrfs_path), 0,
  6389. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  6390. if (!btrfs_path_cachep)
  6391. goto fail;
  6392. btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
  6393. sizeof(struct btrfs_free_space), 0,
  6394. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  6395. if (!btrfs_free_space_cachep)
  6396. goto fail;
  6397. btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
  6398. sizeof(struct btrfs_delalloc_work), 0,
  6399. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
  6400. NULL);
  6401. if (!btrfs_delalloc_work_cachep)
  6402. goto fail;
  6403. return 0;
  6404. fail:
  6405. btrfs_destroy_cachep();
  6406. return -ENOMEM;
  6407. }
  6408. static int btrfs_getattr(struct vfsmount *mnt,
  6409. struct dentry *dentry, struct kstat *stat)
  6410. {
  6411. struct inode *inode = dentry->d_inode;
  6412. u32 blocksize = inode->i_sb->s_blocksize;
  6413. generic_fillattr(inode, stat);
  6414. stat->dev = BTRFS_I(inode)->root->anon_dev;
  6415. stat->blksize = PAGE_CACHE_SIZE;
  6416. stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
  6417. ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
  6418. return 0;
  6419. }
  6420. /*
  6421. * If a file is moved, it will inherit the cow and compression flags of the new
  6422. * directory.
  6423. */
  6424. static void fixup_inode_flags(struct inode *dir, struct inode *inode)
  6425. {
  6426. struct btrfs_inode *b_dir = BTRFS_I(dir);
  6427. struct btrfs_inode *b_inode = BTRFS_I(inode);
  6428. if (b_dir->flags & BTRFS_INODE_NODATACOW)
  6429. b_inode->flags |= BTRFS_INODE_NODATACOW;
  6430. else
  6431. b_inode->flags &= ~BTRFS_INODE_NODATACOW;
  6432. if (b_dir->flags & BTRFS_INODE_COMPRESS) {
  6433. b_inode->flags |= BTRFS_INODE_COMPRESS;
  6434. b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
  6435. } else {
  6436. b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
  6437. BTRFS_INODE_NOCOMPRESS);
  6438. }
  6439. }
  6440. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  6441. struct inode *new_dir, struct dentry *new_dentry)
  6442. {
  6443. struct btrfs_trans_handle *trans;
  6444. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  6445. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  6446. struct inode *new_inode = new_dentry->d_inode;
  6447. struct inode *old_inode = old_dentry->d_inode;
  6448. struct timespec ctime = CURRENT_TIME;
  6449. u64 index = 0;
  6450. u64 root_objectid;
  6451. int ret;
  6452. u64 old_ino = btrfs_ino(old_inode);
  6453. if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  6454. return -EPERM;
  6455. /* we only allow rename subvolume link between subvolumes */
  6456. if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  6457. return -EXDEV;
  6458. if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
  6459. (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
  6460. return -ENOTEMPTY;
  6461. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  6462. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  6463. return -ENOTEMPTY;
  6464. /*
  6465. * we're using rename to replace one file with another.
  6466. * and the replacement file is large. Start IO on it now so
  6467. * we don't add too much work to the end of the transaction
  6468. */
  6469. if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
  6470. old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  6471. filemap_flush(old_inode->i_mapping);
  6472. /* close the racy window with snapshot create/destroy ioctl */
  6473. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  6474. down_read(&root->fs_info->subvol_sem);
  6475. /*
  6476. * We want to reserve the absolute worst case amount of items. So if
  6477. * both inodes are subvols and we need to unlink them then that would
  6478. * require 4 item modifications, but if they are both normal inodes it
  6479. * would require 5 item modifications, so we'll assume their normal
  6480. * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
  6481. * should cover the worst case number of items we'll modify.
  6482. */
  6483. trans = btrfs_start_transaction(root, 20);
  6484. if (IS_ERR(trans)) {
  6485. ret = PTR_ERR(trans);
  6486. goto out_notrans;
  6487. }
  6488. if (dest != root)
  6489. btrfs_record_root_in_trans(trans, dest);
  6490. ret = btrfs_set_inode_index(new_dir, &index);
  6491. if (ret)
  6492. goto out_fail;
  6493. if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  6494. /* force full log commit if subvolume involved. */
  6495. root->fs_info->last_trans_log_full_commit = trans->transid;
  6496. } else {
  6497. ret = btrfs_insert_inode_ref(trans, dest,
  6498. new_dentry->d_name.name,
  6499. new_dentry->d_name.len,
  6500. old_ino,
  6501. btrfs_ino(new_dir), index);
  6502. if (ret)
  6503. goto out_fail;
  6504. /*
  6505. * this is an ugly little race, but the rename is required
  6506. * to make sure that if we crash, the inode is either at the
  6507. * old name or the new one. pinning the log transaction lets
  6508. * us make sure we don't allow a log commit to come in after
  6509. * we unlink the name but before we add the new name back in.
  6510. */
  6511. btrfs_pin_log_trans(root);
  6512. }
  6513. /*
  6514. * make sure the inode gets flushed if it is replacing
  6515. * something.
  6516. */
  6517. if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
  6518. btrfs_add_ordered_operation(trans, root, old_inode);
  6519. inode_inc_iversion(old_dir);
  6520. inode_inc_iversion(new_dir);
  6521. inode_inc_iversion(old_inode);
  6522. old_dir->i_ctime = old_dir->i_mtime = ctime;
  6523. new_dir->i_ctime = new_dir->i_mtime = ctime;
  6524. old_inode->i_ctime = ctime;
  6525. if (old_dentry->d_parent != new_dentry->d_parent)
  6526. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  6527. if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  6528. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  6529. ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
  6530. old_dentry->d_name.name,
  6531. old_dentry->d_name.len);
  6532. } else {
  6533. ret = __btrfs_unlink_inode(trans, root, old_dir,
  6534. old_dentry->d_inode,
  6535. old_dentry->d_name.name,
  6536. old_dentry->d_name.len);
  6537. if (!ret)
  6538. ret = btrfs_update_inode(trans, root, old_inode);
  6539. }
  6540. if (ret) {
  6541. btrfs_abort_transaction(trans, root, ret);
  6542. goto out_fail;
  6543. }
  6544. if (new_inode) {
  6545. inode_inc_iversion(new_inode);
  6546. new_inode->i_ctime = CURRENT_TIME;
  6547. if (unlikely(btrfs_ino(new_inode) ==
  6548. BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  6549. root_objectid = BTRFS_I(new_inode)->location.objectid;
  6550. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  6551. root_objectid,
  6552. new_dentry->d_name.name,
  6553. new_dentry->d_name.len);
  6554. BUG_ON(new_inode->i_nlink == 0);
  6555. } else {
  6556. ret = btrfs_unlink_inode(trans, dest, new_dir,
  6557. new_dentry->d_inode,
  6558. new_dentry->d_name.name,
  6559. new_dentry->d_name.len);
  6560. }
  6561. if (!ret && new_inode->i_nlink == 0) {
  6562. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  6563. BUG_ON(ret);
  6564. }
  6565. if (ret) {
  6566. btrfs_abort_transaction(trans, root, ret);
  6567. goto out_fail;
  6568. }
  6569. }
  6570. fixup_inode_flags(new_dir, old_inode);
  6571. ret = btrfs_add_link(trans, new_dir, old_inode,
  6572. new_dentry->d_name.name,
  6573. new_dentry->d_name.len, 0, index);
  6574. if (ret) {
  6575. btrfs_abort_transaction(trans, root, ret);
  6576. goto out_fail;
  6577. }
  6578. if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
  6579. struct dentry *parent = new_dentry->d_parent;
  6580. btrfs_log_new_name(trans, old_inode, old_dir, parent);
  6581. btrfs_end_log_trans(root);
  6582. }
  6583. out_fail:
  6584. btrfs_end_transaction(trans, root);
  6585. out_notrans:
  6586. if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
  6587. up_read(&root->fs_info->subvol_sem);
  6588. return ret;
  6589. }
  6590. static void btrfs_run_delalloc_work(struct btrfs_work *work)
  6591. {
  6592. struct btrfs_delalloc_work *delalloc_work;
  6593. delalloc_work = container_of(work, struct btrfs_delalloc_work,
  6594. work);
  6595. if (delalloc_work->wait)
  6596. btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
  6597. else
  6598. filemap_flush(delalloc_work->inode->i_mapping);
  6599. if (delalloc_work->delay_iput)
  6600. btrfs_add_delayed_iput(delalloc_work->inode);
  6601. else
  6602. iput(delalloc_work->inode);
  6603. complete(&delalloc_work->completion);
  6604. }
  6605. struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
  6606. int wait, int delay_iput)
  6607. {
  6608. struct btrfs_delalloc_work *work;
  6609. work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
  6610. if (!work)
  6611. return NULL;
  6612. init_completion(&work->completion);
  6613. INIT_LIST_HEAD(&work->list);
  6614. work->inode = inode;
  6615. work->wait = wait;
  6616. work->delay_iput = delay_iput;
  6617. work->work.func = btrfs_run_delalloc_work;
  6618. return work;
  6619. }
  6620. void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
  6621. {
  6622. wait_for_completion(&work->completion);
  6623. kmem_cache_free(btrfs_delalloc_work_cachep, work);
  6624. }
  6625. /*
  6626. * some fairly slow code that needs optimization. This walks the list
  6627. * of all the inodes with pending delalloc and forces them to disk.
  6628. */
  6629. int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
  6630. {
  6631. struct list_head *head = &root->fs_info->delalloc_inodes;
  6632. struct btrfs_inode *binode;
  6633. struct inode *inode;
  6634. struct btrfs_delalloc_work *work, *next;
  6635. struct list_head works;
  6636. int ret = 0;
  6637. if (root->fs_info->sb->s_flags & MS_RDONLY)
  6638. return -EROFS;
  6639. INIT_LIST_HEAD(&works);
  6640. spin_lock(&root->fs_info->delalloc_lock);
  6641. while (!list_empty(head)) {
  6642. binode = list_entry(head->next, struct btrfs_inode,
  6643. delalloc_inodes);
  6644. inode = igrab(&binode->vfs_inode);
  6645. if (!inode)
  6646. list_del_init(&binode->delalloc_inodes);
  6647. spin_unlock(&root->fs_info->delalloc_lock);
  6648. if (inode) {
  6649. work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
  6650. if (!work) {
  6651. ret = -ENOMEM;
  6652. goto out;
  6653. }
  6654. list_add_tail(&work->list, &works);
  6655. btrfs_queue_worker(&root->fs_info->flush_workers,
  6656. &work->work);
  6657. }
  6658. cond_resched();
  6659. spin_lock(&root->fs_info->delalloc_lock);
  6660. }
  6661. spin_unlock(&root->fs_info->delalloc_lock);
  6662. /* the filemap_flush will queue IO into the worker threads, but
  6663. * we have to make sure the IO is actually started and that
  6664. * ordered extents get created before we return
  6665. */
  6666. atomic_inc(&root->fs_info->async_submit_draining);
  6667. while (atomic_read(&root->fs_info->nr_async_submits) ||
  6668. atomic_read(&root->fs_info->async_delalloc_pages)) {
  6669. wait_event(root->fs_info->async_submit_wait,
  6670. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  6671. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  6672. }
  6673. atomic_dec(&root->fs_info->async_submit_draining);
  6674. out:
  6675. list_for_each_entry_safe(work, next, &works, list) {
  6676. list_del_init(&work->list);
  6677. btrfs_wait_and_free_delalloc_work(work);
  6678. }
  6679. return ret;
  6680. }
  6681. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  6682. const char *symname)
  6683. {
  6684. struct btrfs_trans_handle *trans;
  6685. struct btrfs_root *root = BTRFS_I(dir)->root;
  6686. struct btrfs_path *path;
  6687. struct btrfs_key key;
  6688. struct inode *inode = NULL;
  6689. int err;
  6690. int drop_inode = 0;
  6691. u64 objectid;
  6692. u64 index = 0 ;
  6693. int name_len;
  6694. int datasize;
  6695. unsigned long ptr;
  6696. struct btrfs_file_extent_item *ei;
  6697. struct extent_buffer *leaf;
  6698. name_len = strlen(symname) + 1;
  6699. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  6700. return -ENAMETOOLONG;
  6701. /*
  6702. * 2 items for inode item and ref
  6703. * 2 items for dir items
  6704. * 1 item for xattr if selinux is on
  6705. */
  6706. trans = btrfs_start_transaction(root, 5);
  6707. if (IS_ERR(trans))
  6708. return PTR_ERR(trans);
  6709. err = btrfs_find_free_ino(root, &objectid);
  6710. if (err)
  6711. goto out_unlock;
  6712. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  6713. dentry->d_name.len, btrfs_ino(dir), objectid,
  6714. S_IFLNK|S_IRWXUGO, &index);
  6715. if (IS_ERR(inode)) {
  6716. err = PTR_ERR(inode);
  6717. goto out_unlock;
  6718. }
  6719. err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
  6720. if (err) {
  6721. drop_inode = 1;
  6722. goto out_unlock;
  6723. }
  6724. /*
  6725. * If the active LSM wants to access the inode during
  6726. * d_instantiate it needs these. Smack checks to see
  6727. * if the filesystem supports xattrs by looking at the
  6728. * ops vector.
  6729. */
  6730. inode->i_fop = &btrfs_file_operations;
  6731. inode->i_op = &btrfs_file_inode_operations;
  6732. err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
  6733. if (err)
  6734. drop_inode = 1;
  6735. else {
  6736. inode->i_mapping->a_ops = &btrfs_aops;
  6737. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  6738. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  6739. }
  6740. if (drop_inode)
  6741. goto out_unlock;
  6742. path = btrfs_alloc_path();
  6743. if (!path) {
  6744. err = -ENOMEM;
  6745. drop_inode = 1;
  6746. goto out_unlock;
  6747. }
  6748. key.objectid = btrfs_ino(inode);
  6749. key.offset = 0;
  6750. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  6751. datasize = btrfs_file_extent_calc_inline_size(name_len);
  6752. err = btrfs_insert_empty_item(trans, root, path, &key,
  6753. datasize);
  6754. if (err) {
  6755. drop_inode = 1;
  6756. btrfs_free_path(path);
  6757. goto out_unlock;
  6758. }
  6759. leaf = path->nodes[0];
  6760. ei = btrfs_item_ptr(leaf, path->slots[0],
  6761. struct btrfs_file_extent_item);
  6762. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  6763. btrfs_set_file_extent_type(leaf, ei,
  6764. BTRFS_FILE_EXTENT_INLINE);
  6765. btrfs_set_file_extent_encryption(leaf, ei, 0);
  6766. btrfs_set_file_extent_compression(leaf, ei, 0);
  6767. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  6768. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  6769. ptr = btrfs_file_extent_inline_start(ei);
  6770. write_extent_buffer(leaf, symname, ptr, name_len);
  6771. btrfs_mark_buffer_dirty(leaf);
  6772. btrfs_free_path(path);
  6773. inode->i_op = &btrfs_symlink_inode_operations;
  6774. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  6775. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  6776. inode_set_bytes(inode, name_len);
  6777. btrfs_i_size_write(inode, name_len - 1);
  6778. err = btrfs_update_inode(trans, root, inode);
  6779. if (err)
  6780. drop_inode = 1;
  6781. out_unlock:
  6782. if (!err)
  6783. d_instantiate(dentry, inode);
  6784. btrfs_end_transaction(trans, root);
  6785. if (drop_inode) {
  6786. inode_dec_link_count(inode);
  6787. iput(inode);
  6788. }
  6789. btrfs_btree_balance_dirty(root);
  6790. return err;
  6791. }
  6792. static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
  6793. u64 start, u64 num_bytes, u64 min_size,
  6794. loff_t actual_len, u64 *alloc_hint,
  6795. struct btrfs_trans_handle *trans)
  6796. {
  6797. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  6798. struct extent_map *em;
  6799. struct btrfs_root *root = BTRFS_I(inode)->root;
  6800. struct btrfs_key ins;
  6801. u64 cur_offset = start;
  6802. u64 i_size;
  6803. int ret = 0;
  6804. bool own_trans = true;
  6805. if (trans)
  6806. own_trans = false;
  6807. while (num_bytes > 0) {
  6808. if (own_trans) {
  6809. trans = btrfs_start_transaction(root, 3);
  6810. if (IS_ERR(trans)) {
  6811. ret = PTR_ERR(trans);
  6812. break;
  6813. }
  6814. }
  6815. ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
  6816. 0, *alloc_hint, &ins, 1);
  6817. if (ret) {
  6818. if (own_trans)
  6819. btrfs_end_transaction(trans, root);
  6820. break;
  6821. }
  6822. ret = insert_reserved_file_extent(trans, inode,
  6823. cur_offset, ins.objectid,
  6824. ins.offset, ins.offset,
  6825. ins.offset, 0, 0, 0,
  6826. BTRFS_FILE_EXTENT_PREALLOC);
  6827. if (ret) {
  6828. btrfs_abort_transaction(trans, root, ret);
  6829. if (own_trans)
  6830. btrfs_end_transaction(trans, root);
  6831. break;
  6832. }
  6833. btrfs_drop_extent_cache(inode, cur_offset,
  6834. cur_offset + ins.offset -1, 0);
  6835. em = alloc_extent_map();
  6836. if (!em) {
  6837. set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
  6838. &BTRFS_I(inode)->runtime_flags);
  6839. goto next;
  6840. }
  6841. em->start = cur_offset;
  6842. em->orig_start = cur_offset;
  6843. em->len = ins.offset;
  6844. em->block_start = ins.objectid;
  6845. em->block_len = ins.offset;
  6846. em->bdev = root->fs_info->fs_devices->latest_bdev;
  6847. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  6848. em->generation = trans->transid;
  6849. while (1) {
  6850. write_lock(&em_tree->lock);
  6851. ret = add_extent_mapping(em_tree, em);
  6852. if (!ret)
  6853. list_move(&em->list,
  6854. &em_tree->modified_extents);
  6855. write_unlock(&em_tree->lock);
  6856. if (ret != -EEXIST)
  6857. break;
  6858. btrfs_drop_extent_cache(inode, cur_offset,
  6859. cur_offset + ins.offset - 1,
  6860. 0);
  6861. }
  6862. free_extent_map(em);
  6863. next:
  6864. num_bytes -= ins.offset;
  6865. cur_offset += ins.offset;
  6866. *alloc_hint = ins.objectid + ins.offset;
  6867. inode_inc_iversion(inode);
  6868. inode->i_ctime = CURRENT_TIME;
  6869. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  6870. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  6871. (actual_len > inode->i_size) &&
  6872. (cur_offset > inode->i_size)) {
  6873. if (cur_offset > actual_len)
  6874. i_size = actual_len;
  6875. else
  6876. i_size = cur_offset;
  6877. i_size_write(inode, i_size);
  6878. btrfs_ordered_update_i_size(inode, i_size, NULL);
  6879. }
  6880. ret = btrfs_update_inode(trans, root, inode);
  6881. if (ret) {
  6882. btrfs_abort_transaction(trans, root, ret);
  6883. if (own_trans)
  6884. btrfs_end_transaction(trans, root);
  6885. break;
  6886. }
  6887. if (own_trans)
  6888. btrfs_end_transaction(trans, root);
  6889. }
  6890. return ret;
  6891. }
  6892. int btrfs_prealloc_file_range(struct inode *inode, int mode,
  6893. u64 start, u64 num_bytes, u64 min_size,
  6894. loff_t actual_len, u64 *alloc_hint)
  6895. {
  6896. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  6897. min_size, actual_len, alloc_hint,
  6898. NULL);
  6899. }
  6900. int btrfs_prealloc_file_range_trans(struct inode *inode,
  6901. struct btrfs_trans_handle *trans, int mode,
  6902. u64 start, u64 num_bytes, u64 min_size,
  6903. loff_t actual_len, u64 *alloc_hint)
  6904. {
  6905. return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
  6906. min_size, actual_len, alloc_hint, trans);
  6907. }
  6908. static int btrfs_set_page_dirty(struct page *page)
  6909. {
  6910. return __set_page_dirty_nobuffers(page);
  6911. }
  6912. static int btrfs_permission(struct inode *inode, int mask)
  6913. {
  6914. struct btrfs_root *root = BTRFS_I(inode)->root;
  6915. umode_t mode = inode->i_mode;
  6916. if (mask & MAY_WRITE &&
  6917. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
  6918. if (btrfs_root_readonly(root))
  6919. return -EROFS;
  6920. if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
  6921. return -EACCES;
  6922. }
  6923. return generic_permission(inode, mask);
  6924. }
  6925. static const struct inode_operations btrfs_dir_inode_operations = {
  6926. .getattr = btrfs_getattr,
  6927. .lookup = btrfs_lookup,
  6928. .create = btrfs_create,
  6929. .unlink = btrfs_unlink,
  6930. .link = btrfs_link,
  6931. .mkdir = btrfs_mkdir,
  6932. .rmdir = btrfs_rmdir,
  6933. .rename = btrfs_rename,
  6934. .symlink = btrfs_symlink,
  6935. .setattr = btrfs_setattr,
  6936. .mknod = btrfs_mknod,
  6937. .setxattr = btrfs_setxattr,
  6938. .getxattr = btrfs_getxattr,
  6939. .listxattr = btrfs_listxattr,
  6940. .removexattr = btrfs_removexattr,
  6941. .permission = btrfs_permission,
  6942. .get_acl = btrfs_get_acl,
  6943. };
  6944. static const struct inode_operations btrfs_dir_ro_inode_operations = {
  6945. .lookup = btrfs_lookup,
  6946. .permission = btrfs_permission,
  6947. .get_acl = btrfs_get_acl,
  6948. };
  6949. static const struct file_operations btrfs_dir_file_operations = {
  6950. .llseek = generic_file_llseek,
  6951. .read = generic_read_dir,
  6952. .readdir = btrfs_real_readdir,
  6953. .unlocked_ioctl = btrfs_ioctl,
  6954. #ifdef CONFIG_COMPAT
  6955. .compat_ioctl = btrfs_ioctl,
  6956. #endif
  6957. .release = btrfs_release_file,
  6958. .fsync = btrfs_sync_file,
  6959. };
  6960. static struct extent_io_ops btrfs_extent_io_ops = {
  6961. .fill_delalloc = run_delalloc_range,
  6962. .submit_bio_hook = btrfs_submit_bio_hook,
  6963. .merge_bio_hook = btrfs_merge_bio_hook,
  6964. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  6965. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  6966. .writepage_start_hook = btrfs_writepage_start_hook,
  6967. .set_bit_hook = btrfs_set_bit_hook,
  6968. .clear_bit_hook = btrfs_clear_bit_hook,
  6969. .merge_extent_hook = btrfs_merge_extent_hook,
  6970. .split_extent_hook = btrfs_split_extent_hook,
  6971. };
  6972. /*
  6973. * btrfs doesn't support the bmap operation because swapfiles
  6974. * use bmap to make a mapping of extents in the file. They assume
  6975. * these extents won't change over the life of the file and they
  6976. * use the bmap result to do IO directly to the drive.
  6977. *
  6978. * the btrfs bmap call would return logical addresses that aren't
  6979. * suitable for IO and they also will change frequently as COW
  6980. * operations happen. So, swapfile + btrfs == corruption.
  6981. *
  6982. * For now we're avoiding this by dropping bmap.
  6983. */
  6984. static const struct address_space_operations btrfs_aops = {
  6985. .readpage = btrfs_readpage,
  6986. .writepage = btrfs_writepage,
  6987. .writepages = btrfs_writepages,
  6988. .readpages = btrfs_readpages,
  6989. .direct_IO = btrfs_direct_IO,
  6990. .invalidatepage = btrfs_invalidatepage,
  6991. .releasepage = btrfs_releasepage,
  6992. .set_page_dirty = btrfs_set_page_dirty,
  6993. .error_remove_page = generic_error_remove_page,
  6994. };
  6995. static const struct address_space_operations btrfs_symlink_aops = {
  6996. .readpage = btrfs_readpage,
  6997. .writepage = btrfs_writepage,
  6998. .invalidatepage = btrfs_invalidatepage,
  6999. .releasepage = btrfs_releasepage,
  7000. };
  7001. static const struct inode_operations btrfs_file_inode_operations = {
  7002. .getattr = btrfs_getattr,
  7003. .setattr = btrfs_setattr,
  7004. .setxattr = btrfs_setxattr,
  7005. .getxattr = btrfs_getxattr,
  7006. .listxattr = btrfs_listxattr,
  7007. .removexattr = btrfs_removexattr,
  7008. .permission = btrfs_permission,
  7009. .fiemap = btrfs_fiemap,
  7010. .get_acl = btrfs_get_acl,
  7011. .update_time = btrfs_update_time,
  7012. };
  7013. static const struct inode_operations btrfs_special_inode_operations = {
  7014. .getattr = btrfs_getattr,
  7015. .setattr = btrfs_setattr,
  7016. .permission = btrfs_permission,
  7017. .setxattr = btrfs_setxattr,
  7018. .getxattr = btrfs_getxattr,
  7019. .listxattr = btrfs_listxattr,
  7020. .removexattr = btrfs_removexattr,
  7021. .get_acl = btrfs_get_acl,
  7022. .update_time = btrfs_update_time,
  7023. };
  7024. static const struct inode_operations btrfs_symlink_inode_operations = {
  7025. .readlink = generic_readlink,
  7026. .follow_link = page_follow_link_light,
  7027. .put_link = page_put_link,
  7028. .getattr = btrfs_getattr,
  7029. .setattr = btrfs_setattr,
  7030. .permission = btrfs_permission,
  7031. .setxattr = btrfs_setxattr,
  7032. .getxattr = btrfs_getxattr,
  7033. .listxattr = btrfs_listxattr,
  7034. .removexattr = btrfs_removexattr,
  7035. .get_acl = btrfs_get_acl,
  7036. .update_time = btrfs_update_time,
  7037. };
  7038. const struct dentry_operations btrfs_dentry_operations = {
  7039. .d_delete = btrfs_dentry_delete,
  7040. .d_release = btrfs_dentry_release,
  7041. };