inode.c 207 KB

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