inode.c 197 KB

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