inode.c 231 KB

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