inode.c 198 KB

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