inode.c 215 KB

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