inode.c 215 KB

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