inode.c 194 KB

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