inode.c 206 KB

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