inode.c 155 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798
  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 "compat.h"
  39. #include "ctree.h"
  40. #include "disk-io.h"
  41. #include "transaction.h"
  42. #include "btrfs_inode.h"
  43. #include "ioctl.h"
  44. #include "print-tree.h"
  45. #include "volumes.h"
  46. #include "ordered-data.h"
  47. #include "xattr.h"
  48. #include "tree-log.h"
  49. #include "compression.h"
  50. #include "locking.h"
  51. struct btrfs_iget_args {
  52. u64 ino;
  53. struct btrfs_root *root;
  54. };
  55. static struct inode_operations btrfs_dir_inode_operations;
  56. static struct inode_operations btrfs_symlink_inode_operations;
  57. static struct inode_operations btrfs_dir_ro_inode_operations;
  58. static struct inode_operations btrfs_special_inode_operations;
  59. static struct inode_operations btrfs_file_inode_operations;
  60. static struct address_space_operations btrfs_aops;
  61. static struct address_space_operations btrfs_symlink_aops;
  62. static struct file_operations btrfs_dir_file_operations;
  63. static struct extent_io_ops btrfs_extent_io_ops;
  64. static struct kmem_cache *btrfs_inode_cachep;
  65. struct kmem_cache *btrfs_trans_handle_cachep;
  66. struct kmem_cache *btrfs_transaction_cachep;
  67. struct kmem_cache *btrfs_path_cachep;
  68. #define S_SHIFT 12
  69. static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
  70. [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
  71. [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
  72. [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
  73. [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
  74. [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
  75. [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
  76. [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
  77. };
  78. static void btrfs_truncate(struct inode *inode);
  79. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
  80. static noinline int cow_file_range(struct inode *inode,
  81. struct page *locked_page,
  82. u64 start, u64 end, int *page_started,
  83. unsigned long *nr_written, int unlock);
  84. static int btrfs_init_inode_security(struct inode *inode, struct inode *dir)
  85. {
  86. int err;
  87. err = btrfs_init_acl(inode, dir);
  88. if (!err)
  89. err = btrfs_xattr_security_init(inode, dir);
  90. return err;
  91. }
  92. /*
  93. * this does all the hard work for inserting an inline extent into
  94. * the btree. The caller should have done a btrfs_drop_extents so that
  95. * no overlapping inline items exist in the btree
  96. */
  97. static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
  98. struct btrfs_root *root, struct inode *inode,
  99. u64 start, size_t size, size_t compressed_size,
  100. struct page **compressed_pages)
  101. {
  102. struct btrfs_key key;
  103. struct btrfs_path *path;
  104. struct extent_buffer *leaf;
  105. struct page *page = NULL;
  106. char *kaddr;
  107. unsigned long ptr;
  108. struct btrfs_file_extent_item *ei;
  109. int err = 0;
  110. int ret;
  111. size_t cur_size = size;
  112. size_t datasize;
  113. unsigned long offset;
  114. int use_compress = 0;
  115. if (compressed_size && compressed_pages) {
  116. use_compress = 1;
  117. cur_size = compressed_size;
  118. }
  119. path = btrfs_alloc_path();
  120. if (!path)
  121. return -ENOMEM;
  122. path->leave_spinning = 1;
  123. btrfs_set_trans_block_group(trans, inode);
  124. key.objectid = inode->i_ino;
  125. key.offset = start;
  126. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  127. datasize = btrfs_file_extent_calc_inline_size(cur_size);
  128. inode_add_bytes(inode, size);
  129. ret = btrfs_insert_empty_item(trans, root, path, &key,
  130. datasize);
  131. BUG_ON(ret);
  132. if (ret) {
  133. err = ret;
  134. goto fail;
  135. }
  136. leaf = path->nodes[0];
  137. ei = btrfs_item_ptr(leaf, path->slots[0],
  138. struct btrfs_file_extent_item);
  139. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  140. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  141. btrfs_set_file_extent_encryption(leaf, ei, 0);
  142. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  143. btrfs_set_file_extent_ram_bytes(leaf, ei, size);
  144. ptr = btrfs_file_extent_inline_start(ei);
  145. if (use_compress) {
  146. struct page *cpage;
  147. int i = 0;
  148. while (compressed_size > 0) {
  149. cpage = compressed_pages[i];
  150. cur_size = min_t(unsigned long, compressed_size,
  151. PAGE_CACHE_SIZE);
  152. kaddr = kmap_atomic(cpage, KM_USER0);
  153. write_extent_buffer(leaf, kaddr, ptr, cur_size);
  154. kunmap_atomic(kaddr, KM_USER0);
  155. i++;
  156. ptr += cur_size;
  157. compressed_size -= cur_size;
  158. }
  159. btrfs_set_file_extent_compression(leaf, ei,
  160. BTRFS_COMPRESS_ZLIB);
  161. } else {
  162. page = find_get_page(inode->i_mapping,
  163. start >> PAGE_CACHE_SHIFT);
  164. btrfs_set_file_extent_compression(leaf, ei, 0);
  165. kaddr = kmap_atomic(page, KM_USER0);
  166. offset = start & (PAGE_CACHE_SIZE - 1);
  167. write_extent_buffer(leaf, kaddr + offset, ptr, size);
  168. kunmap_atomic(kaddr, KM_USER0);
  169. page_cache_release(page);
  170. }
  171. btrfs_mark_buffer_dirty(leaf);
  172. btrfs_free_path(path);
  173. BTRFS_I(inode)->disk_i_size = inode->i_size;
  174. btrfs_update_inode(trans, root, inode);
  175. return 0;
  176. fail:
  177. btrfs_free_path(path);
  178. return err;
  179. }
  180. /*
  181. * conditionally insert an inline extent into the file. This
  182. * does the checks required to make sure the data is small enough
  183. * to fit as an inline extent.
  184. */
  185. static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
  186. struct btrfs_root *root,
  187. struct inode *inode, u64 start, u64 end,
  188. size_t compressed_size,
  189. struct page **compressed_pages)
  190. {
  191. u64 isize = i_size_read(inode);
  192. u64 actual_end = min(end + 1, isize);
  193. u64 inline_len = actual_end - start;
  194. u64 aligned_end = (end + root->sectorsize - 1) &
  195. ~((u64)root->sectorsize - 1);
  196. u64 hint_byte;
  197. u64 data_len = inline_len;
  198. int ret;
  199. if (compressed_size)
  200. data_len = compressed_size;
  201. if (start > 0 ||
  202. actual_end >= PAGE_CACHE_SIZE ||
  203. data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
  204. (!compressed_size &&
  205. (actual_end & (root->sectorsize - 1)) == 0) ||
  206. end + 1 < isize ||
  207. data_len > root->fs_info->max_inline) {
  208. return 1;
  209. }
  210. ret = btrfs_drop_extents(trans, root, inode, start,
  211. aligned_end, aligned_end, start,
  212. &hint_byte, 1);
  213. BUG_ON(ret);
  214. if (isize > actual_end)
  215. inline_len = min_t(u64, isize, actual_end);
  216. ret = insert_inline_extent(trans, root, inode, start,
  217. inline_len, compressed_size,
  218. compressed_pages);
  219. BUG_ON(ret);
  220. btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
  221. return 0;
  222. }
  223. struct async_extent {
  224. u64 start;
  225. u64 ram_size;
  226. u64 compressed_size;
  227. struct page **pages;
  228. unsigned long nr_pages;
  229. struct list_head list;
  230. };
  231. struct async_cow {
  232. struct inode *inode;
  233. struct btrfs_root *root;
  234. struct page *locked_page;
  235. u64 start;
  236. u64 end;
  237. struct list_head extents;
  238. struct btrfs_work work;
  239. };
  240. static noinline int add_async_extent(struct async_cow *cow,
  241. u64 start, u64 ram_size,
  242. u64 compressed_size,
  243. struct page **pages,
  244. unsigned long nr_pages)
  245. {
  246. struct async_extent *async_extent;
  247. async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
  248. async_extent->start = start;
  249. async_extent->ram_size = ram_size;
  250. async_extent->compressed_size = compressed_size;
  251. async_extent->pages = pages;
  252. async_extent->nr_pages = nr_pages;
  253. list_add_tail(&async_extent->list, &cow->extents);
  254. return 0;
  255. }
  256. /*
  257. * we create compressed extents in two phases. The first
  258. * phase compresses a range of pages that have already been
  259. * locked (both pages and state bits are locked).
  260. *
  261. * This is done inside an ordered work queue, and the compression
  262. * is spread across many cpus. The actual IO submission is step
  263. * two, and the ordered work queue takes care of making sure that
  264. * happens in the same order things were put onto the queue by
  265. * writepages and friends.
  266. *
  267. * If this code finds it can't get good compression, it puts an
  268. * entry onto the work queue to write the uncompressed bytes. This
  269. * makes sure that both compressed inodes and uncompressed inodes
  270. * are written in the same order that pdflush sent them down.
  271. */
  272. static noinline int compress_file_range(struct inode *inode,
  273. struct page *locked_page,
  274. u64 start, u64 end,
  275. struct async_cow *async_cow,
  276. int *num_added)
  277. {
  278. struct btrfs_root *root = BTRFS_I(inode)->root;
  279. struct btrfs_trans_handle *trans;
  280. u64 num_bytes;
  281. u64 orig_start;
  282. u64 disk_num_bytes;
  283. u64 blocksize = root->sectorsize;
  284. u64 actual_end;
  285. u64 isize = i_size_read(inode);
  286. int ret = 0;
  287. struct page **pages = NULL;
  288. unsigned long nr_pages;
  289. unsigned long nr_pages_ret = 0;
  290. unsigned long total_compressed = 0;
  291. unsigned long total_in = 0;
  292. unsigned long max_compressed = 128 * 1024;
  293. unsigned long max_uncompressed = 128 * 1024;
  294. int i;
  295. int will_compress;
  296. orig_start = start;
  297. actual_end = min_t(u64, isize, end + 1);
  298. again:
  299. will_compress = 0;
  300. nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
  301. nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
  302. /*
  303. * we don't want to send crud past the end of i_size through
  304. * compression, that's just a waste of CPU time. So, if the
  305. * end of the file is before the start of our current
  306. * requested range of bytes, we bail out to the uncompressed
  307. * cleanup code that can deal with all of this.
  308. *
  309. * It isn't really the fastest way to fix things, but this is a
  310. * very uncommon corner.
  311. */
  312. if (actual_end <= start)
  313. goto cleanup_and_bail_uncompressed;
  314. total_compressed = actual_end - start;
  315. /* we want to make sure that amount of ram required to uncompress
  316. * an extent is reasonable, so we limit the total size in ram
  317. * of a compressed extent to 128k. This is a crucial number
  318. * because it also controls how easily we can spread reads across
  319. * cpus for decompression.
  320. *
  321. * We also want to make sure the amount of IO required to do
  322. * a random read is reasonably small, so we limit the size of
  323. * a compressed extent to 128k.
  324. */
  325. total_compressed = min(total_compressed, max_uncompressed);
  326. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  327. num_bytes = max(blocksize, num_bytes);
  328. disk_num_bytes = num_bytes;
  329. total_in = 0;
  330. ret = 0;
  331. /*
  332. * we do compression for mount -o compress and when the
  333. * inode has not been flagged as nocompress. This flag can
  334. * change at any time if we discover bad compression ratios.
  335. */
  336. if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
  337. btrfs_test_opt(root, COMPRESS)) {
  338. WARN_ON(pages);
  339. pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
  340. ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
  341. total_compressed, pages,
  342. nr_pages, &nr_pages_ret,
  343. &total_in,
  344. &total_compressed,
  345. max_compressed);
  346. if (!ret) {
  347. unsigned long offset = total_compressed &
  348. (PAGE_CACHE_SIZE - 1);
  349. struct page *page = pages[nr_pages_ret - 1];
  350. char *kaddr;
  351. /* zero the tail end of the last page, we might be
  352. * sending it down to disk
  353. */
  354. if (offset) {
  355. kaddr = kmap_atomic(page, KM_USER0);
  356. memset(kaddr + offset, 0,
  357. PAGE_CACHE_SIZE - offset);
  358. kunmap_atomic(kaddr, KM_USER0);
  359. }
  360. will_compress = 1;
  361. }
  362. }
  363. if (start == 0) {
  364. trans = btrfs_join_transaction(root, 1);
  365. BUG_ON(!trans);
  366. btrfs_set_trans_block_group(trans, inode);
  367. /* lets try to make an inline extent */
  368. if (ret || total_in < (actual_end - start)) {
  369. /* we didn't compress the entire range, try
  370. * to make an uncompressed inline extent.
  371. */
  372. ret = cow_file_range_inline(trans, root, inode,
  373. start, end, 0, NULL);
  374. } else {
  375. /* try making a compressed inline extent */
  376. ret = cow_file_range_inline(trans, root, inode,
  377. start, end,
  378. total_compressed, pages);
  379. }
  380. btrfs_end_transaction(trans, root);
  381. if (ret == 0) {
  382. /*
  383. * inline extent creation worked, we don't need
  384. * to create any more async work items. Unlock
  385. * and free up our temp pages.
  386. */
  387. extent_clear_unlock_delalloc(inode,
  388. &BTRFS_I(inode)->io_tree,
  389. start, end, NULL, 1, 0,
  390. 0, 1, 1, 1, 0);
  391. ret = 0;
  392. goto free_pages_out;
  393. }
  394. }
  395. if (will_compress) {
  396. /*
  397. * we aren't doing an inline extent round the compressed size
  398. * up to a block size boundary so the allocator does sane
  399. * things
  400. */
  401. total_compressed = (total_compressed + blocksize - 1) &
  402. ~(blocksize - 1);
  403. /*
  404. * one last check to make sure the compression is really a
  405. * win, compare the page count read with the blocks on disk
  406. */
  407. total_in = (total_in + PAGE_CACHE_SIZE - 1) &
  408. ~(PAGE_CACHE_SIZE - 1);
  409. if (total_compressed >= total_in) {
  410. will_compress = 0;
  411. } else {
  412. disk_num_bytes = total_compressed;
  413. num_bytes = total_in;
  414. }
  415. }
  416. if (!will_compress && pages) {
  417. /*
  418. * the compression code ran but failed to make things smaller,
  419. * free any pages it allocated and our page pointer array
  420. */
  421. for (i = 0; i < nr_pages_ret; i++) {
  422. WARN_ON(pages[i]->mapping);
  423. page_cache_release(pages[i]);
  424. }
  425. kfree(pages);
  426. pages = NULL;
  427. total_compressed = 0;
  428. nr_pages_ret = 0;
  429. /* flag the file so we don't compress in the future */
  430. BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
  431. }
  432. if (will_compress) {
  433. *num_added += 1;
  434. /* the async work queues will take care of doing actual
  435. * allocation on disk for these compressed pages,
  436. * and will submit them to the elevator.
  437. */
  438. add_async_extent(async_cow, start, num_bytes,
  439. total_compressed, pages, nr_pages_ret);
  440. if (start + num_bytes < end && start + num_bytes < actual_end) {
  441. start += num_bytes;
  442. pages = NULL;
  443. cond_resched();
  444. goto again;
  445. }
  446. } else {
  447. cleanup_and_bail_uncompressed:
  448. /*
  449. * No compression, but we still need to write the pages in
  450. * the file we've been given so far. redirty the locked
  451. * page if it corresponds to our extent and set things up
  452. * for the async work queue to run cow_file_range to do
  453. * the normal delalloc dance
  454. */
  455. if (page_offset(locked_page) >= start &&
  456. page_offset(locked_page) <= end) {
  457. __set_page_dirty_nobuffers(locked_page);
  458. /* unlocked later on in the async handlers */
  459. }
  460. add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
  461. *num_added += 1;
  462. }
  463. out:
  464. return 0;
  465. free_pages_out:
  466. for (i = 0; i < nr_pages_ret; i++) {
  467. WARN_ON(pages[i]->mapping);
  468. page_cache_release(pages[i]);
  469. }
  470. kfree(pages);
  471. goto out;
  472. }
  473. /*
  474. * phase two of compressed writeback. This is the ordered portion
  475. * of the code, which only gets called in the order the work was
  476. * queued. We walk all the async extents created by compress_file_range
  477. * and send them down to the disk.
  478. */
  479. static noinline int submit_compressed_extents(struct inode *inode,
  480. struct async_cow *async_cow)
  481. {
  482. struct async_extent *async_extent;
  483. u64 alloc_hint = 0;
  484. struct btrfs_trans_handle *trans;
  485. struct btrfs_key ins;
  486. struct extent_map *em;
  487. struct btrfs_root *root = BTRFS_I(inode)->root;
  488. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  489. struct extent_io_tree *io_tree;
  490. int ret;
  491. if (list_empty(&async_cow->extents))
  492. return 0;
  493. trans = btrfs_join_transaction(root, 1);
  494. while (!list_empty(&async_cow->extents)) {
  495. async_extent = list_entry(async_cow->extents.next,
  496. struct async_extent, list);
  497. list_del(&async_extent->list);
  498. io_tree = &BTRFS_I(inode)->io_tree;
  499. /* did the compression code fall back to uncompressed IO? */
  500. if (!async_extent->pages) {
  501. int page_started = 0;
  502. unsigned long nr_written = 0;
  503. lock_extent(io_tree, async_extent->start,
  504. async_extent->start +
  505. async_extent->ram_size - 1, GFP_NOFS);
  506. /* allocate blocks */
  507. cow_file_range(inode, async_cow->locked_page,
  508. async_extent->start,
  509. async_extent->start +
  510. async_extent->ram_size - 1,
  511. &page_started, &nr_written, 0);
  512. /*
  513. * if page_started, cow_file_range inserted an
  514. * inline extent and took care of all the unlocking
  515. * and IO for us. Otherwise, we need to submit
  516. * all those pages down to the drive.
  517. */
  518. if (!page_started)
  519. extent_write_locked_range(io_tree,
  520. inode, async_extent->start,
  521. async_extent->start +
  522. async_extent->ram_size - 1,
  523. btrfs_get_extent,
  524. WB_SYNC_ALL);
  525. kfree(async_extent);
  526. cond_resched();
  527. continue;
  528. }
  529. lock_extent(io_tree, async_extent->start,
  530. async_extent->start + async_extent->ram_size - 1,
  531. GFP_NOFS);
  532. /*
  533. * here we're doing allocation and writeback of the
  534. * compressed pages
  535. */
  536. btrfs_drop_extent_cache(inode, async_extent->start,
  537. async_extent->start +
  538. async_extent->ram_size - 1, 0);
  539. ret = btrfs_reserve_extent(trans, root,
  540. async_extent->compressed_size,
  541. async_extent->compressed_size,
  542. 0, alloc_hint,
  543. (u64)-1, &ins, 1);
  544. BUG_ON(ret);
  545. em = alloc_extent_map(GFP_NOFS);
  546. em->start = async_extent->start;
  547. em->len = async_extent->ram_size;
  548. em->orig_start = em->start;
  549. em->block_start = ins.objectid;
  550. em->block_len = ins.offset;
  551. em->bdev = root->fs_info->fs_devices->latest_bdev;
  552. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  553. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  554. while (1) {
  555. write_lock(&em_tree->lock);
  556. ret = add_extent_mapping(em_tree, em);
  557. write_unlock(&em_tree->lock);
  558. if (ret != -EEXIST) {
  559. free_extent_map(em);
  560. break;
  561. }
  562. btrfs_drop_extent_cache(inode, async_extent->start,
  563. async_extent->start +
  564. async_extent->ram_size - 1, 0);
  565. }
  566. ret = btrfs_add_ordered_extent(inode, async_extent->start,
  567. ins.objectid,
  568. async_extent->ram_size,
  569. ins.offset,
  570. BTRFS_ORDERED_COMPRESSED);
  571. BUG_ON(ret);
  572. btrfs_end_transaction(trans, root);
  573. /*
  574. * clear dirty, set writeback and unlock the pages.
  575. */
  576. extent_clear_unlock_delalloc(inode,
  577. &BTRFS_I(inode)->io_tree,
  578. async_extent->start,
  579. async_extent->start +
  580. async_extent->ram_size - 1,
  581. NULL, 1, 1, 0, 1, 1, 0, 0);
  582. ret = btrfs_submit_compressed_write(inode,
  583. async_extent->start,
  584. async_extent->ram_size,
  585. ins.objectid,
  586. ins.offset, async_extent->pages,
  587. async_extent->nr_pages);
  588. BUG_ON(ret);
  589. trans = btrfs_join_transaction(root, 1);
  590. alloc_hint = ins.objectid + ins.offset;
  591. kfree(async_extent);
  592. cond_resched();
  593. }
  594. btrfs_end_transaction(trans, root);
  595. return 0;
  596. }
  597. /*
  598. * when extent_io.c finds a delayed allocation range in the file,
  599. * the call backs end up in this code. The basic idea is to
  600. * allocate extents on disk for the range, and create ordered data structs
  601. * in ram to track those extents.
  602. *
  603. * locked_page is the page that writepage had locked already. We use
  604. * it to make sure we don't do extra locks or unlocks.
  605. *
  606. * *page_started is set to one if we unlock locked_page and do everything
  607. * required to start IO on it. It may be clean and already done with
  608. * IO when we return.
  609. */
  610. static noinline int cow_file_range(struct inode *inode,
  611. struct page *locked_page,
  612. u64 start, u64 end, int *page_started,
  613. unsigned long *nr_written,
  614. int unlock)
  615. {
  616. struct btrfs_root *root = BTRFS_I(inode)->root;
  617. struct btrfs_trans_handle *trans;
  618. u64 alloc_hint = 0;
  619. u64 num_bytes;
  620. unsigned long ram_size;
  621. u64 disk_num_bytes;
  622. u64 cur_alloc_size;
  623. u64 blocksize = root->sectorsize;
  624. u64 actual_end;
  625. u64 isize = i_size_read(inode);
  626. struct btrfs_key ins;
  627. struct extent_map *em;
  628. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  629. int ret = 0;
  630. trans = btrfs_join_transaction(root, 1);
  631. BUG_ON(!trans);
  632. btrfs_set_trans_block_group(trans, inode);
  633. actual_end = min_t(u64, isize, end + 1);
  634. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  635. num_bytes = max(blocksize, num_bytes);
  636. disk_num_bytes = num_bytes;
  637. ret = 0;
  638. if (start == 0) {
  639. /* lets try to make an inline extent */
  640. ret = cow_file_range_inline(trans, root, inode,
  641. start, end, 0, NULL);
  642. if (ret == 0) {
  643. extent_clear_unlock_delalloc(inode,
  644. &BTRFS_I(inode)->io_tree,
  645. start, end, NULL, 1, 1,
  646. 1, 1, 1, 1, 0);
  647. *nr_written = *nr_written +
  648. (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
  649. *page_started = 1;
  650. ret = 0;
  651. goto out;
  652. }
  653. }
  654. BUG_ON(disk_num_bytes >
  655. btrfs_super_total_bytes(&root->fs_info->super_copy));
  656. read_lock(&BTRFS_I(inode)->extent_tree.lock);
  657. em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
  658. start, num_bytes);
  659. if (em) {
  660. alloc_hint = em->block_start;
  661. free_extent_map(em);
  662. }
  663. read_unlock(&BTRFS_I(inode)->extent_tree.lock);
  664. btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
  665. while (disk_num_bytes > 0) {
  666. cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
  667. ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
  668. root->sectorsize, 0, alloc_hint,
  669. (u64)-1, &ins, 1);
  670. BUG_ON(ret);
  671. em = alloc_extent_map(GFP_NOFS);
  672. em->start = start;
  673. em->orig_start = em->start;
  674. ram_size = ins.offset;
  675. em->len = ins.offset;
  676. em->block_start = ins.objectid;
  677. em->block_len = ins.offset;
  678. em->bdev = root->fs_info->fs_devices->latest_bdev;
  679. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  680. while (1) {
  681. write_lock(&em_tree->lock);
  682. ret = add_extent_mapping(em_tree, em);
  683. write_unlock(&em_tree->lock);
  684. if (ret != -EEXIST) {
  685. free_extent_map(em);
  686. break;
  687. }
  688. btrfs_drop_extent_cache(inode, start,
  689. start + ram_size - 1, 0);
  690. }
  691. cur_alloc_size = ins.offset;
  692. ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
  693. ram_size, cur_alloc_size, 0);
  694. BUG_ON(ret);
  695. if (root->root_key.objectid ==
  696. BTRFS_DATA_RELOC_TREE_OBJECTID) {
  697. ret = btrfs_reloc_clone_csums(inode, start,
  698. cur_alloc_size);
  699. BUG_ON(ret);
  700. }
  701. if (disk_num_bytes < cur_alloc_size)
  702. break;
  703. /* we're not doing compressed IO, don't unlock the first
  704. * page (which the caller expects to stay locked), don't
  705. * clear any dirty bits and don't set any writeback bits
  706. *
  707. * Do set the Private2 bit so we know this page was properly
  708. * setup for writepage
  709. */
  710. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  711. start, start + ram_size - 1,
  712. locked_page, unlock, 1,
  713. 1, 0, 0, 0, 1);
  714. disk_num_bytes -= cur_alloc_size;
  715. num_bytes -= cur_alloc_size;
  716. alloc_hint = ins.objectid + ins.offset;
  717. start += cur_alloc_size;
  718. }
  719. out:
  720. ret = 0;
  721. btrfs_end_transaction(trans, root);
  722. return ret;
  723. }
  724. /*
  725. * work queue call back to started compression on a file and pages
  726. */
  727. static noinline void async_cow_start(struct btrfs_work *work)
  728. {
  729. struct async_cow *async_cow;
  730. int num_added = 0;
  731. async_cow = container_of(work, struct async_cow, work);
  732. compress_file_range(async_cow->inode, async_cow->locked_page,
  733. async_cow->start, async_cow->end, async_cow,
  734. &num_added);
  735. if (num_added == 0)
  736. async_cow->inode = NULL;
  737. }
  738. /*
  739. * work queue call back to submit previously compressed pages
  740. */
  741. static noinline void async_cow_submit(struct btrfs_work *work)
  742. {
  743. struct async_cow *async_cow;
  744. struct btrfs_root *root;
  745. unsigned long nr_pages;
  746. async_cow = container_of(work, struct async_cow, work);
  747. root = async_cow->root;
  748. nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
  749. PAGE_CACHE_SHIFT;
  750. atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
  751. if (atomic_read(&root->fs_info->async_delalloc_pages) <
  752. 5 * 1042 * 1024 &&
  753. waitqueue_active(&root->fs_info->async_submit_wait))
  754. wake_up(&root->fs_info->async_submit_wait);
  755. if (async_cow->inode)
  756. submit_compressed_extents(async_cow->inode, async_cow);
  757. }
  758. static noinline void async_cow_free(struct btrfs_work *work)
  759. {
  760. struct async_cow *async_cow;
  761. async_cow = container_of(work, struct async_cow, work);
  762. kfree(async_cow);
  763. }
  764. static int cow_file_range_async(struct inode *inode, struct page *locked_page,
  765. u64 start, u64 end, int *page_started,
  766. unsigned long *nr_written)
  767. {
  768. struct async_cow *async_cow;
  769. struct btrfs_root *root = BTRFS_I(inode)->root;
  770. unsigned long nr_pages;
  771. u64 cur_end;
  772. int limit = 10 * 1024 * 1042;
  773. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
  774. EXTENT_DELALLOC, 1, 0, NULL, GFP_NOFS);
  775. while (start < end) {
  776. async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
  777. async_cow->inode = inode;
  778. async_cow->root = root;
  779. async_cow->locked_page = locked_page;
  780. async_cow->start = start;
  781. if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
  782. cur_end = end;
  783. else
  784. cur_end = min(end, start + 512 * 1024 - 1);
  785. async_cow->end = cur_end;
  786. INIT_LIST_HEAD(&async_cow->extents);
  787. async_cow->work.func = async_cow_start;
  788. async_cow->work.ordered_func = async_cow_submit;
  789. async_cow->work.ordered_free = async_cow_free;
  790. async_cow->work.flags = 0;
  791. nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
  792. PAGE_CACHE_SHIFT;
  793. atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
  794. btrfs_queue_worker(&root->fs_info->delalloc_workers,
  795. &async_cow->work);
  796. if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
  797. wait_event(root->fs_info->async_submit_wait,
  798. (atomic_read(&root->fs_info->async_delalloc_pages) <
  799. limit));
  800. }
  801. while (atomic_read(&root->fs_info->async_submit_draining) &&
  802. atomic_read(&root->fs_info->async_delalloc_pages)) {
  803. wait_event(root->fs_info->async_submit_wait,
  804. (atomic_read(&root->fs_info->async_delalloc_pages) ==
  805. 0));
  806. }
  807. *nr_written += nr_pages;
  808. start = cur_end + 1;
  809. }
  810. *page_started = 1;
  811. return 0;
  812. }
  813. static noinline int csum_exist_in_range(struct btrfs_root *root,
  814. u64 bytenr, u64 num_bytes)
  815. {
  816. int ret;
  817. struct btrfs_ordered_sum *sums;
  818. LIST_HEAD(list);
  819. ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
  820. bytenr + num_bytes - 1, &list);
  821. if (ret == 0 && list_empty(&list))
  822. return 0;
  823. while (!list_empty(&list)) {
  824. sums = list_entry(list.next, struct btrfs_ordered_sum, list);
  825. list_del(&sums->list);
  826. kfree(sums);
  827. }
  828. return 1;
  829. }
  830. /*
  831. * when nowcow writeback call back. This checks for snapshots or COW copies
  832. * of the extents that exist in the file, and COWs the file as required.
  833. *
  834. * If no cow copies or snapshots exist, we write directly to the existing
  835. * blocks on disk
  836. */
  837. static noinline int run_delalloc_nocow(struct inode *inode,
  838. struct page *locked_page,
  839. u64 start, u64 end, int *page_started, int force,
  840. unsigned long *nr_written)
  841. {
  842. struct btrfs_root *root = BTRFS_I(inode)->root;
  843. struct btrfs_trans_handle *trans;
  844. struct extent_buffer *leaf;
  845. struct btrfs_path *path;
  846. struct btrfs_file_extent_item *fi;
  847. struct btrfs_key found_key;
  848. u64 cow_start;
  849. u64 cur_offset;
  850. u64 extent_end;
  851. u64 extent_offset;
  852. u64 disk_bytenr;
  853. u64 num_bytes;
  854. int extent_type;
  855. int ret;
  856. int type;
  857. int nocow;
  858. int check_prev = 1;
  859. path = btrfs_alloc_path();
  860. BUG_ON(!path);
  861. trans = btrfs_join_transaction(root, 1);
  862. BUG_ON(!trans);
  863. cow_start = (u64)-1;
  864. cur_offset = start;
  865. while (1) {
  866. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  867. cur_offset, 0);
  868. BUG_ON(ret < 0);
  869. if (ret > 0 && path->slots[0] > 0 && check_prev) {
  870. leaf = path->nodes[0];
  871. btrfs_item_key_to_cpu(leaf, &found_key,
  872. path->slots[0] - 1);
  873. if (found_key.objectid == inode->i_ino &&
  874. found_key.type == BTRFS_EXTENT_DATA_KEY)
  875. path->slots[0]--;
  876. }
  877. check_prev = 0;
  878. next_slot:
  879. leaf = path->nodes[0];
  880. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  881. ret = btrfs_next_leaf(root, path);
  882. if (ret < 0)
  883. BUG_ON(1);
  884. if (ret > 0)
  885. break;
  886. leaf = path->nodes[0];
  887. }
  888. nocow = 0;
  889. disk_bytenr = 0;
  890. num_bytes = 0;
  891. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  892. if (found_key.objectid > inode->i_ino ||
  893. found_key.type > BTRFS_EXTENT_DATA_KEY ||
  894. found_key.offset > end)
  895. break;
  896. if (found_key.offset > cur_offset) {
  897. extent_end = found_key.offset;
  898. goto out_check;
  899. }
  900. fi = btrfs_item_ptr(leaf, path->slots[0],
  901. struct btrfs_file_extent_item);
  902. extent_type = btrfs_file_extent_type(leaf, fi);
  903. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  904. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  905. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  906. extent_offset = btrfs_file_extent_offset(leaf, fi);
  907. extent_end = found_key.offset +
  908. btrfs_file_extent_num_bytes(leaf, fi);
  909. if (extent_end <= start) {
  910. path->slots[0]++;
  911. goto next_slot;
  912. }
  913. if (disk_bytenr == 0)
  914. goto out_check;
  915. if (btrfs_file_extent_compression(leaf, fi) ||
  916. btrfs_file_extent_encryption(leaf, fi) ||
  917. btrfs_file_extent_other_encoding(leaf, fi))
  918. goto out_check;
  919. if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
  920. goto out_check;
  921. if (btrfs_extent_readonly(root, disk_bytenr))
  922. goto out_check;
  923. if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
  924. found_key.offset -
  925. extent_offset, disk_bytenr))
  926. goto out_check;
  927. disk_bytenr += extent_offset;
  928. disk_bytenr += cur_offset - found_key.offset;
  929. num_bytes = min(end + 1, extent_end) - cur_offset;
  930. /*
  931. * force cow if csum exists in the range.
  932. * this ensure that csum for a given extent are
  933. * either valid or do not exist.
  934. */
  935. if (csum_exist_in_range(root, disk_bytenr, num_bytes))
  936. goto out_check;
  937. nocow = 1;
  938. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  939. extent_end = found_key.offset +
  940. btrfs_file_extent_inline_len(leaf, fi);
  941. extent_end = ALIGN(extent_end, root->sectorsize);
  942. } else {
  943. BUG_ON(1);
  944. }
  945. out_check:
  946. if (extent_end <= start) {
  947. path->slots[0]++;
  948. goto next_slot;
  949. }
  950. if (!nocow) {
  951. if (cow_start == (u64)-1)
  952. cow_start = cur_offset;
  953. cur_offset = extent_end;
  954. if (cur_offset > end)
  955. break;
  956. path->slots[0]++;
  957. goto next_slot;
  958. }
  959. btrfs_release_path(root, path);
  960. if (cow_start != (u64)-1) {
  961. ret = cow_file_range(inode, locked_page, cow_start,
  962. found_key.offset - 1, page_started,
  963. nr_written, 1);
  964. BUG_ON(ret);
  965. cow_start = (u64)-1;
  966. }
  967. if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  968. struct extent_map *em;
  969. struct extent_map_tree *em_tree;
  970. em_tree = &BTRFS_I(inode)->extent_tree;
  971. em = alloc_extent_map(GFP_NOFS);
  972. em->start = cur_offset;
  973. em->orig_start = em->start;
  974. em->len = num_bytes;
  975. em->block_len = num_bytes;
  976. em->block_start = disk_bytenr;
  977. em->bdev = root->fs_info->fs_devices->latest_bdev;
  978. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  979. while (1) {
  980. write_lock(&em_tree->lock);
  981. ret = add_extent_mapping(em_tree, em);
  982. write_unlock(&em_tree->lock);
  983. if (ret != -EEXIST) {
  984. free_extent_map(em);
  985. break;
  986. }
  987. btrfs_drop_extent_cache(inode, em->start,
  988. em->start + em->len - 1, 0);
  989. }
  990. type = BTRFS_ORDERED_PREALLOC;
  991. } else {
  992. type = BTRFS_ORDERED_NOCOW;
  993. }
  994. ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
  995. num_bytes, num_bytes, type);
  996. BUG_ON(ret);
  997. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  998. cur_offset, cur_offset + num_bytes - 1,
  999. locked_page, 1, 1, 1, 0, 0, 0, 1);
  1000. cur_offset = extent_end;
  1001. if (cur_offset > end)
  1002. break;
  1003. }
  1004. btrfs_release_path(root, path);
  1005. if (cur_offset <= end && cow_start == (u64)-1)
  1006. cow_start = cur_offset;
  1007. if (cow_start != (u64)-1) {
  1008. ret = cow_file_range(inode, locked_page, cow_start, end,
  1009. page_started, nr_written, 1);
  1010. BUG_ON(ret);
  1011. }
  1012. ret = btrfs_end_transaction(trans, root);
  1013. BUG_ON(ret);
  1014. btrfs_free_path(path);
  1015. return 0;
  1016. }
  1017. /*
  1018. * extent_io.c call back to do delayed allocation processing
  1019. */
  1020. static int run_delalloc_range(struct inode *inode, struct page *locked_page,
  1021. u64 start, u64 end, int *page_started,
  1022. unsigned long *nr_written)
  1023. {
  1024. int ret;
  1025. struct btrfs_root *root = BTRFS_I(inode)->root;
  1026. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
  1027. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1028. page_started, 1, nr_written);
  1029. else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
  1030. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1031. page_started, 0, nr_written);
  1032. else if (!btrfs_test_opt(root, COMPRESS))
  1033. ret = cow_file_range(inode, locked_page, start, end,
  1034. page_started, nr_written, 1);
  1035. else
  1036. ret = cow_file_range_async(inode, locked_page, start, end,
  1037. page_started, nr_written);
  1038. return ret;
  1039. }
  1040. static int btrfs_split_extent_hook(struct inode *inode,
  1041. struct extent_state *orig, u64 split)
  1042. {
  1043. struct btrfs_root *root = BTRFS_I(inode)->root;
  1044. u64 size;
  1045. if (!(orig->state & EXTENT_DELALLOC))
  1046. return 0;
  1047. size = orig->end - orig->start + 1;
  1048. if (size > root->fs_info->max_extent) {
  1049. u64 num_extents;
  1050. u64 new_size;
  1051. new_size = orig->end - split + 1;
  1052. num_extents = div64_u64(size + root->fs_info->max_extent - 1,
  1053. root->fs_info->max_extent);
  1054. /*
  1055. * if we break a large extent up then leave delalloc_extents be,
  1056. * since we've already accounted for the large extent.
  1057. */
  1058. if (div64_u64(new_size + root->fs_info->max_extent - 1,
  1059. root->fs_info->max_extent) < num_extents)
  1060. return 0;
  1061. }
  1062. BTRFS_I(inode)->delalloc_extents++;
  1063. return 0;
  1064. }
  1065. /*
  1066. * extent_io.c merge_extent_hook, used to track merged delayed allocation
  1067. * extents so we can keep track of new extents that are just merged onto old
  1068. * extents, such as when we are doing sequential writes, so we can properly
  1069. * account for the metadata space we'll need.
  1070. */
  1071. static int btrfs_merge_extent_hook(struct inode *inode,
  1072. struct extent_state *new,
  1073. struct extent_state *other)
  1074. {
  1075. struct btrfs_root *root = BTRFS_I(inode)->root;
  1076. u64 new_size, old_size;
  1077. u64 num_extents;
  1078. /* not delalloc, ignore it */
  1079. if (!(other->state & EXTENT_DELALLOC))
  1080. return 0;
  1081. old_size = other->end - other->start + 1;
  1082. if (new->start < other->start)
  1083. new_size = other->end - new->start + 1;
  1084. else
  1085. new_size = new->end - other->start + 1;
  1086. /* we're not bigger than the max, unreserve the space and go */
  1087. if (new_size <= root->fs_info->max_extent) {
  1088. BTRFS_I(inode)->delalloc_extents--;
  1089. return 0;
  1090. }
  1091. /*
  1092. * If we grew by another max_extent, just return, we want to keep that
  1093. * reserved amount.
  1094. */
  1095. num_extents = div64_u64(old_size + root->fs_info->max_extent - 1,
  1096. root->fs_info->max_extent);
  1097. if (div64_u64(new_size + root->fs_info->max_extent - 1,
  1098. root->fs_info->max_extent) > num_extents)
  1099. return 0;
  1100. BTRFS_I(inode)->delalloc_extents--;
  1101. return 0;
  1102. }
  1103. /*
  1104. * extent_io.c set_bit_hook, used to track delayed allocation
  1105. * bytes in this file, and to maintain the list of inodes that
  1106. * have pending delalloc work to be done.
  1107. */
  1108. static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
  1109. unsigned long old, unsigned long bits)
  1110. {
  1111. /*
  1112. * set_bit and clear bit hooks normally require _irqsave/restore
  1113. * but in this case, we are only testeing for the DELALLOC
  1114. * bit, which is only set or cleared with irqs on
  1115. */
  1116. if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
  1117. struct btrfs_root *root = BTRFS_I(inode)->root;
  1118. BTRFS_I(inode)->delalloc_extents++;
  1119. btrfs_delalloc_reserve_space(root, inode, end - start + 1);
  1120. spin_lock(&root->fs_info->delalloc_lock);
  1121. BTRFS_I(inode)->delalloc_bytes += end - start + 1;
  1122. root->fs_info->delalloc_bytes += end - start + 1;
  1123. if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1124. list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
  1125. &root->fs_info->delalloc_inodes);
  1126. }
  1127. spin_unlock(&root->fs_info->delalloc_lock);
  1128. }
  1129. return 0;
  1130. }
  1131. /*
  1132. * extent_io.c clear_bit_hook, see set_bit_hook for why
  1133. */
  1134. static int btrfs_clear_bit_hook(struct inode *inode,
  1135. struct extent_state *state, unsigned long bits)
  1136. {
  1137. /*
  1138. * set_bit and clear bit hooks normally require _irqsave/restore
  1139. * but in this case, we are only testeing for the DELALLOC
  1140. * bit, which is only set or cleared with irqs on
  1141. */
  1142. if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
  1143. struct btrfs_root *root = BTRFS_I(inode)->root;
  1144. BTRFS_I(inode)->delalloc_extents--;
  1145. btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
  1146. spin_lock(&root->fs_info->delalloc_lock);
  1147. if (state->end - state->start + 1 >
  1148. root->fs_info->delalloc_bytes) {
  1149. printk(KERN_INFO "btrfs warning: delalloc account "
  1150. "%llu %llu\n",
  1151. (unsigned long long)
  1152. state->end - state->start + 1,
  1153. (unsigned long long)
  1154. root->fs_info->delalloc_bytes);
  1155. btrfs_delalloc_free_space(root, inode, (u64)-1);
  1156. root->fs_info->delalloc_bytes = 0;
  1157. BTRFS_I(inode)->delalloc_bytes = 0;
  1158. } else {
  1159. btrfs_delalloc_free_space(root, inode,
  1160. state->end -
  1161. state->start + 1);
  1162. root->fs_info->delalloc_bytes -= state->end -
  1163. state->start + 1;
  1164. BTRFS_I(inode)->delalloc_bytes -= state->end -
  1165. state->start + 1;
  1166. }
  1167. if (BTRFS_I(inode)->delalloc_bytes == 0 &&
  1168. !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1169. list_del_init(&BTRFS_I(inode)->delalloc_inodes);
  1170. }
  1171. spin_unlock(&root->fs_info->delalloc_lock);
  1172. }
  1173. return 0;
  1174. }
  1175. /*
  1176. * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
  1177. * we don't create bios that span stripes or chunks
  1178. */
  1179. int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
  1180. size_t size, struct bio *bio,
  1181. unsigned long bio_flags)
  1182. {
  1183. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  1184. struct btrfs_mapping_tree *map_tree;
  1185. u64 logical = (u64)bio->bi_sector << 9;
  1186. u64 length = 0;
  1187. u64 map_length;
  1188. int ret;
  1189. if (bio_flags & EXTENT_BIO_COMPRESSED)
  1190. return 0;
  1191. length = bio->bi_size;
  1192. map_tree = &root->fs_info->mapping_tree;
  1193. map_length = length;
  1194. ret = btrfs_map_block(map_tree, READ, logical,
  1195. &map_length, NULL, 0);
  1196. if (map_length < length + size)
  1197. return 1;
  1198. return 0;
  1199. }
  1200. /*
  1201. * in order to insert checksums into the metadata in large chunks,
  1202. * we wait until bio submission time. All the pages in the bio are
  1203. * checksummed and sums are attached onto the ordered extent record.
  1204. *
  1205. * At IO completion time the cums attached on the ordered extent record
  1206. * are inserted into the btree
  1207. */
  1208. static int __btrfs_submit_bio_start(struct inode *inode, int rw,
  1209. struct bio *bio, int mirror_num,
  1210. unsigned long bio_flags)
  1211. {
  1212. struct btrfs_root *root = BTRFS_I(inode)->root;
  1213. int ret = 0;
  1214. ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
  1215. BUG_ON(ret);
  1216. return 0;
  1217. }
  1218. /*
  1219. * in order to insert checksums into the metadata in large chunks,
  1220. * we wait until bio submission time. All the pages in the bio are
  1221. * checksummed and sums are attached onto the ordered extent record.
  1222. *
  1223. * At IO completion time the cums attached on the ordered extent record
  1224. * are inserted into the btree
  1225. */
  1226. static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
  1227. int mirror_num, unsigned long bio_flags)
  1228. {
  1229. struct btrfs_root *root = BTRFS_I(inode)->root;
  1230. return btrfs_map_bio(root, rw, bio, mirror_num, 1);
  1231. }
  1232. /*
  1233. * extent_io.c submission hook. This does the right thing for csum calculation
  1234. * on write, or reading the csums from the tree before a read
  1235. */
  1236. static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  1237. int mirror_num, unsigned long bio_flags)
  1238. {
  1239. struct btrfs_root *root = BTRFS_I(inode)->root;
  1240. int ret = 0;
  1241. int skip_sum;
  1242. skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
  1243. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  1244. BUG_ON(ret);
  1245. if (!(rw & (1 << BIO_RW))) {
  1246. if (bio_flags & EXTENT_BIO_COMPRESSED) {
  1247. return btrfs_submit_compressed_read(inode, bio,
  1248. mirror_num, bio_flags);
  1249. } else if (!skip_sum)
  1250. btrfs_lookup_bio_sums(root, inode, bio, NULL);
  1251. goto mapit;
  1252. } else if (!skip_sum) {
  1253. /* csum items have already been cloned */
  1254. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
  1255. goto mapit;
  1256. /* we're doing a write, do the async checksumming */
  1257. return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  1258. inode, rw, bio, mirror_num,
  1259. bio_flags, __btrfs_submit_bio_start,
  1260. __btrfs_submit_bio_done);
  1261. }
  1262. mapit:
  1263. return btrfs_map_bio(root, rw, bio, mirror_num, 0);
  1264. }
  1265. /*
  1266. * given a list of ordered sums record them in the inode. This happens
  1267. * at IO completion time based on sums calculated at bio submission time.
  1268. */
  1269. static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
  1270. struct inode *inode, u64 file_offset,
  1271. struct list_head *list)
  1272. {
  1273. struct btrfs_ordered_sum *sum;
  1274. btrfs_set_trans_block_group(trans, inode);
  1275. list_for_each_entry(sum, list, list) {
  1276. btrfs_csum_file_blocks(trans,
  1277. BTRFS_I(inode)->root->fs_info->csum_root, sum);
  1278. }
  1279. return 0;
  1280. }
  1281. int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
  1282. {
  1283. if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
  1284. WARN_ON(1);
  1285. return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
  1286. GFP_NOFS);
  1287. }
  1288. /* see btrfs_writepage_start_hook for details on why this is required */
  1289. struct btrfs_writepage_fixup {
  1290. struct page *page;
  1291. struct btrfs_work work;
  1292. };
  1293. static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
  1294. {
  1295. struct btrfs_writepage_fixup *fixup;
  1296. struct btrfs_ordered_extent *ordered;
  1297. struct page *page;
  1298. struct inode *inode;
  1299. u64 page_start;
  1300. u64 page_end;
  1301. fixup = container_of(work, struct btrfs_writepage_fixup, work);
  1302. page = fixup->page;
  1303. again:
  1304. lock_page(page);
  1305. if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
  1306. ClearPageChecked(page);
  1307. goto out_page;
  1308. }
  1309. inode = page->mapping->host;
  1310. page_start = page_offset(page);
  1311. page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
  1312. lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
  1313. /* already ordered? We're done */
  1314. if (PagePrivate2(page))
  1315. goto out;
  1316. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  1317. if (ordered) {
  1318. unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
  1319. page_end, GFP_NOFS);
  1320. unlock_page(page);
  1321. btrfs_start_ordered_extent(inode, ordered, 1);
  1322. goto again;
  1323. }
  1324. btrfs_set_extent_delalloc(inode, page_start, page_end);
  1325. ClearPageChecked(page);
  1326. out:
  1327. unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
  1328. out_page:
  1329. unlock_page(page);
  1330. page_cache_release(page);
  1331. }
  1332. /*
  1333. * There are a few paths in the higher layers of the kernel that directly
  1334. * set the page dirty bit without asking the filesystem if it is a
  1335. * good idea. This causes problems because we want to make sure COW
  1336. * properly happens and the data=ordered rules are followed.
  1337. *
  1338. * In our case any range that doesn't have the ORDERED bit set
  1339. * hasn't been properly setup for IO. We kick off an async process
  1340. * to fix it up. The async helper will wait for ordered extents, set
  1341. * the delalloc bit and make it safe to write the page.
  1342. */
  1343. static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
  1344. {
  1345. struct inode *inode = page->mapping->host;
  1346. struct btrfs_writepage_fixup *fixup;
  1347. struct btrfs_root *root = BTRFS_I(inode)->root;
  1348. /* this page is properly in the ordered list */
  1349. if (TestClearPagePrivate2(page))
  1350. return 0;
  1351. if (PageChecked(page))
  1352. return -EAGAIN;
  1353. fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
  1354. if (!fixup)
  1355. return -EAGAIN;
  1356. SetPageChecked(page);
  1357. page_cache_get(page);
  1358. fixup->work.func = btrfs_writepage_fixup_worker;
  1359. fixup->page = page;
  1360. btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
  1361. return -EAGAIN;
  1362. }
  1363. static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
  1364. struct inode *inode, u64 file_pos,
  1365. u64 disk_bytenr, u64 disk_num_bytes,
  1366. u64 num_bytes, u64 ram_bytes,
  1367. u64 locked_end,
  1368. u8 compression, u8 encryption,
  1369. u16 other_encoding, int extent_type)
  1370. {
  1371. struct btrfs_root *root = BTRFS_I(inode)->root;
  1372. struct btrfs_file_extent_item *fi;
  1373. struct btrfs_path *path;
  1374. struct extent_buffer *leaf;
  1375. struct btrfs_key ins;
  1376. u64 hint;
  1377. int ret;
  1378. path = btrfs_alloc_path();
  1379. BUG_ON(!path);
  1380. path->leave_spinning = 1;
  1381. /*
  1382. * we may be replacing one extent in the tree with another.
  1383. * The new extent is pinned in the extent map, and we don't want
  1384. * to drop it from the cache until it is completely in the btree.
  1385. *
  1386. * So, tell btrfs_drop_extents to leave this extent in the cache.
  1387. * the caller is expected to unpin it and allow it to be merged
  1388. * with the others.
  1389. */
  1390. ret = btrfs_drop_extents(trans, root, inode, file_pos,
  1391. file_pos + num_bytes, locked_end,
  1392. file_pos, &hint, 0);
  1393. BUG_ON(ret);
  1394. ins.objectid = inode->i_ino;
  1395. ins.offset = file_pos;
  1396. ins.type = BTRFS_EXTENT_DATA_KEY;
  1397. ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
  1398. BUG_ON(ret);
  1399. leaf = path->nodes[0];
  1400. fi = btrfs_item_ptr(leaf, path->slots[0],
  1401. struct btrfs_file_extent_item);
  1402. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1403. btrfs_set_file_extent_type(leaf, fi, extent_type);
  1404. btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
  1405. btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
  1406. btrfs_set_file_extent_offset(leaf, fi, 0);
  1407. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1408. btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
  1409. btrfs_set_file_extent_compression(leaf, fi, compression);
  1410. btrfs_set_file_extent_encryption(leaf, fi, encryption);
  1411. btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
  1412. btrfs_unlock_up_safe(path, 1);
  1413. btrfs_set_lock_blocking(leaf);
  1414. btrfs_mark_buffer_dirty(leaf);
  1415. inode_add_bytes(inode, num_bytes);
  1416. ins.objectid = disk_bytenr;
  1417. ins.offset = disk_num_bytes;
  1418. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1419. ret = btrfs_alloc_reserved_file_extent(trans, root,
  1420. root->root_key.objectid,
  1421. inode->i_ino, file_pos, &ins);
  1422. BUG_ON(ret);
  1423. btrfs_free_path(path);
  1424. return 0;
  1425. }
  1426. /*
  1427. * helper function for btrfs_finish_ordered_io, this
  1428. * just reads in some of the csum leaves to prime them into ram
  1429. * before we start the transaction. It limits the amount of btree
  1430. * reads required while inside the transaction.
  1431. */
  1432. static noinline void reada_csum(struct btrfs_root *root,
  1433. struct btrfs_path *path,
  1434. struct btrfs_ordered_extent *ordered_extent)
  1435. {
  1436. struct btrfs_ordered_sum *sum;
  1437. u64 bytenr;
  1438. sum = list_entry(ordered_extent->list.next, struct btrfs_ordered_sum,
  1439. list);
  1440. bytenr = sum->sums[0].bytenr;
  1441. /*
  1442. * we don't care about the results, the point of this search is
  1443. * just to get the btree leaves into ram
  1444. */
  1445. btrfs_lookup_csum(NULL, root->fs_info->csum_root, path, bytenr, 0);
  1446. }
  1447. /* as ordered data IO finishes, this gets called so we can finish
  1448. * an ordered extent if the range of bytes in the file it covers are
  1449. * fully written.
  1450. */
  1451. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
  1452. {
  1453. struct btrfs_root *root = BTRFS_I(inode)->root;
  1454. struct btrfs_trans_handle *trans;
  1455. struct btrfs_ordered_extent *ordered_extent = NULL;
  1456. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1457. struct btrfs_path *path;
  1458. int compressed = 0;
  1459. int ret;
  1460. ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
  1461. if (!ret)
  1462. return 0;
  1463. /*
  1464. * before we join the transaction, try to do some of our IO.
  1465. * This will limit the amount of IO that we have to do with
  1466. * the transaction running. We're unlikely to need to do any
  1467. * IO if the file extents are new, the disk_i_size checks
  1468. * covers the most common case.
  1469. */
  1470. if (start < BTRFS_I(inode)->disk_i_size) {
  1471. path = btrfs_alloc_path();
  1472. if (path) {
  1473. ret = btrfs_lookup_file_extent(NULL, root, path,
  1474. inode->i_ino,
  1475. start, 0);
  1476. ordered_extent = btrfs_lookup_ordered_extent(inode,
  1477. start);
  1478. if (!list_empty(&ordered_extent->list)) {
  1479. btrfs_release_path(root, path);
  1480. reada_csum(root, path, ordered_extent);
  1481. }
  1482. btrfs_free_path(path);
  1483. }
  1484. }
  1485. trans = btrfs_join_transaction(root, 1);
  1486. if (!ordered_extent)
  1487. ordered_extent = btrfs_lookup_ordered_extent(inode, start);
  1488. BUG_ON(!ordered_extent);
  1489. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
  1490. goto nocow;
  1491. lock_extent(io_tree, ordered_extent->file_offset,
  1492. ordered_extent->file_offset + ordered_extent->len - 1,
  1493. GFP_NOFS);
  1494. if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
  1495. compressed = 1;
  1496. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
  1497. BUG_ON(compressed);
  1498. ret = btrfs_mark_extent_written(trans, root, inode,
  1499. ordered_extent->file_offset,
  1500. ordered_extent->file_offset +
  1501. ordered_extent->len);
  1502. BUG_ON(ret);
  1503. } else {
  1504. ret = insert_reserved_file_extent(trans, inode,
  1505. ordered_extent->file_offset,
  1506. ordered_extent->start,
  1507. ordered_extent->disk_len,
  1508. ordered_extent->len,
  1509. ordered_extent->len,
  1510. ordered_extent->file_offset +
  1511. ordered_extent->len,
  1512. compressed, 0, 0,
  1513. BTRFS_FILE_EXTENT_REG);
  1514. unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
  1515. ordered_extent->file_offset,
  1516. ordered_extent->len);
  1517. BUG_ON(ret);
  1518. }
  1519. unlock_extent(io_tree, ordered_extent->file_offset,
  1520. ordered_extent->file_offset + ordered_extent->len - 1,
  1521. GFP_NOFS);
  1522. nocow:
  1523. add_pending_csums(trans, inode, ordered_extent->file_offset,
  1524. &ordered_extent->list);
  1525. mutex_lock(&BTRFS_I(inode)->extent_mutex);
  1526. btrfs_ordered_update_i_size(inode, ordered_extent);
  1527. btrfs_update_inode(trans, root, inode);
  1528. btrfs_remove_ordered_extent(inode, ordered_extent);
  1529. mutex_unlock(&BTRFS_I(inode)->extent_mutex);
  1530. /* once for us */
  1531. btrfs_put_ordered_extent(ordered_extent);
  1532. /* once for the tree */
  1533. btrfs_put_ordered_extent(ordered_extent);
  1534. btrfs_end_transaction(trans, root);
  1535. return 0;
  1536. }
  1537. static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
  1538. struct extent_state *state, int uptodate)
  1539. {
  1540. ClearPagePrivate2(page);
  1541. return btrfs_finish_ordered_io(page->mapping->host, start, end);
  1542. }
  1543. /*
  1544. * When IO fails, either with EIO or csum verification fails, we
  1545. * try other mirrors that might have a good copy of the data. This
  1546. * io_failure_record is used to record state as we go through all the
  1547. * mirrors. If another mirror has good data, the page is set up to date
  1548. * and things continue. If a good mirror can't be found, the original
  1549. * bio end_io callback is called to indicate things have failed.
  1550. */
  1551. struct io_failure_record {
  1552. struct page *page;
  1553. u64 start;
  1554. u64 len;
  1555. u64 logical;
  1556. unsigned long bio_flags;
  1557. int last_mirror;
  1558. };
  1559. static int btrfs_io_failed_hook(struct bio *failed_bio,
  1560. struct page *page, u64 start, u64 end,
  1561. struct extent_state *state)
  1562. {
  1563. struct io_failure_record *failrec = NULL;
  1564. u64 private;
  1565. struct extent_map *em;
  1566. struct inode *inode = page->mapping->host;
  1567. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1568. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1569. struct bio *bio;
  1570. int num_copies;
  1571. int ret;
  1572. int rw;
  1573. u64 logical;
  1574. ret = get_state_private(failure_tree, start, &private);
  1575. if (ret) {
  1576. failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
  1577. if (!failrec)
  1578. return -ENOMEM;
  1579. failrec->start = start;
  1580. failrec->len = end - start + 1;
  1581. failrec->last_mirror = 0;
  1582. failrec->bio_flags = 0;
  1583. read_lock(&em_tree->lock);
  1584. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1585. if (em->start > start || em->start + em->len < start) {
  1586. free_extent_map(em);
  1587. em = NULL;
  1588. }
  1589. read_unlock(&em_tree->lock);
  1590. if (!em || IS_ERR(em)) {
  1591. kfree(failrec);
  1592. return -EIO;
  1593. }
  1594. logical = start - em->start;
  1595. logical = em->block_start + logical;
  1596. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1597. logical = em->block_start;
  1598. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1599. }
  1600. failrec->logical = logical;
  1601. free_extent_map(em);
  1602. set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
  1603. EXTENT_DIRTY, GFP_NOFS);
  1604. set_state_private(failure_tree, start,
  1605. (u64)(unsigned long)failrec);
  1606. } else {
  1607. failrec = (struct io_failure_record *)(unsigned long)private;
  1608. }
  1609. num_copies = btrfs_num_copies(
  1610. &BTRFS_I(inode)->root->fs_info->mapping_tree,
  1611. failrec->logical, failrec->len);
  1612. failrec->last_mirror++;
  1613. if (!state) {
  1614. spin_lock(&BTRFS_I(inode)->io_tree.lock);
  1615. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  1616. failrec->start,
  1617. EXTENT_LOCKED);
  1618. if (state && state->start != failrec->start)
  1619. state = NULL;
  1620. spin_unlock(&BTRFS_I(inode)->io_tree.lock);
  1621. }
  1622. if (!state || failrec->last_mirror > num_copies) {
  1623. set_state_private(failure_tree, failrec->start, 0);
  1624. clear_extent_bits(failure_tree, failrec->start,
  1625. failrec->start + failrec->len - 1,
  1626. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1627. kfree(failrec);
  1628. return -EIO;
  1629. }
  1630. bio = bio_alloc(GFP_NOFS, 1);
  1631. bio->bi_private = state;
  1632. bio->bi_end_io = failed_bio->bi_end_io;
  1633. bio->bi_sector = failrec->logical >> 9;
  1634. bio->bi_bdev = failed_bio->bi_bdev;
  1635. bio->bi_size = 0;
  1636. bio_add_page(bio, page, failrec->len, start - page_offset(page));
  1637. if (failed_bio->bi_rw & (1 << BIO_RW))
  1638. rw = WRITE;
  1639. else
  1640. rw = READ;
  1641. BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
  1642. failrec->last_mirror,
  1643. failrec->bio_flags);
  1644. return 0;
  1645. }
  1646. /*
  1647. * each time an IO finishes, we do a fast check in the IO failure tree
  1648. * to see if we need to process or clean up an io_failure_record
  1649. */
  1650. static int btrfs_clean_io_failures(struct inode *inode, u64 start)
  1651. {
  1652. u64 private;
  1653. u64 private_failure;
  1654. struct io_failure_record *failure;
  1655. int ret;
  1656. private = 0;
  1657. if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  1658. (u64)-1, 1, EXTENT_DIRTY)) {
  1659. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
  1660. start, &private_failure);
  1661. if (ret == 0) {
  1662. failure = (struct io_failure_record *)(unsigned long)
  1663. private_failure;
  1664. set_state_private(&BTRFS_I(inode)->io_failure_tree,
  1665. failure->start, 0);
  1666. clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
  1667. failure->start,
  1668. failure->start + failure->len - 1,
  1669. EXTENT_DIRTY | EXTENT_LOCKED,
  1670. GFP_NOFS);
  1671. kfree(failure);
  1672. }
  1673. }
  1674. return 0;
  1675. }
  1676. /*
  1677. * when reads are done, we need to check csums to verify the data is correct
  1678. * if there's a match, we allow the bio to finish. If not, we go through
  1679. * the io_failure_record routines to find good copies
  1680. */
  1681. static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  1682. struct extent_state *state)
  1683. {
  1684. size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
  1685. struct inode *inode = page->mapping->host;
  1686. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1687. char *kaddr;
  1688. u64 private = ~(u32)0;
  1689. int ret;
  1690. struct btrfs_root *root = BTRFS_I(inode)->root;
  1691. u32 csum = ~(u32)0;
  1692. if (PageChecked(page)) {
  1693. ClearPageChecked(page);
  1694. goto good;
  1695. }
  1696. if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
  1697. return 0;
  1698. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
  1699. test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
  1700. clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
  1701. GFP_NOFS);
  1702. return 0;
  1703. }
  1704. if (state && state->start == start) {
  1705. private = state->private;
  1706. ret = 0;
  1707. } else {
  1708. ret = get_state_private(io_tree, start, &private);
  1709. }
  1710. kaddr = kmap_atomic(page, KM_USER0);
  1711. if (ret)
  1712. goto zeroit;
  1713. csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
  1714. btrfs_csum_final(csum, (char *)&csum);
  1715. if (csum != private)
  1716. goto zeroit;
  1717. kunmap_atomic(kaddr, KM_USER0);
  1718. good:
  1719. /* if the io failure tree for this inode is non-empty,
  1720. * check to see if we've recovered from a failed IO
  1721. */
  1722. btrfs_clean_io_failures(inode, start);
  1723. return 0;
  1724. zeroit:
  1725. if (printk_ratelimit()) {
  1726. printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
  1727. "private %llu\n", page->mapping->host->i_ino,
  1728. (unsigned long long)start, csum,
  1729. (unsigned long long)private);
  1730. }
  1731. memset(kaddr + offset, 1, end - start + 1);
  1732. flush_dcache_page(page);
  1733. kunmap_atomic(kaddr, KM_USER0);
  1734. if (private == 0)
  1735. return 0;
  1736. return -EIO;
  1737. }
  1738. /*
  1739. * This creates an orphan entry for the given inode in case something goes
  1740. * wrong in the middle of an unlink/truncate.
  1741. */
  1742. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  1743. {
  1744. struct btrfs_root *root = BTRFS_I(inode)->root;
  1745. int ret = 0;
  1746. spin_lock(&root->list_lock);
  1747. /* already on the orphan list, we're good */
  1748. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  1749. spin_unlock(&root->list_lock);
  1750. return 0;
  1751. }
  1752. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1753. spin_unlock(&root->list_lock);
  1754. /*
  1755. * insert an orphan item to track this unlinked/truncated file
  1756. */
  1757. ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
  1758. return ret;
  1759. }
  1760. /*
  1761. * We have done the truncate/delete so we can go ahead and remove the orphan
  1762. * item for this particular inode.
  1763. */
  1764. int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
  1765. {
  1766. struct btrfs_root *root = BTRFS_I(inode)->root;
  1767. int ret = 0;
  1768. spin_lock(&root->list_lock);
  1769. if (list_empty(&BTRFS_I(inode)->i_orphan)) {
  1770. spin_unlock(&root->list_lock);
  1771. return 0;
  1772. }
  1773. list_del_init(&BTRFS_I(inode)->i_orphan);
  1774. if (!trans) {
  1775. spin_unlock(&root->list_lock);
  1776. return 0;
  1777. }
  1778. spin_unlock(&root->list_lock);
  1779. ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
  1780. return ret;
  1781. }
  1782. /*
  1783. * this cleans up any orphans that may be left on the list from the last use
  1784. * of this root.
  1785. */
  1786. void btrfs_orphan_cleanup(struct btrfs_root *root)
  1787. {
  1788. struct btrfs_path *path;
  1789. struct extent_buffer *leaf;
  1790. struct btrfs_item *item;
  1791. struct btrfs_key key, found_key;
  1792. struct btrfs_trans_handle *trans;
  1793. struct inode *inode;
  1794. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  1795. path = btrfs_alloc_path();
  1796. if (!path)
  1797. return;
  1798. path->reada = -1;
  1799. key.objectid = BTRFS_ORPHAN_OBJECTID;
  1800. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  1801. key.offset = (u64)-1;
  1802. while (1) {
  1803. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1804. if (ret < 0) {
  1805. printk(KERN_ERR "Error searching slot for orphan: %d"
  1806. "\n", ret);
  1807. break;
  1808. }
  1809. /*
  1810. * if ret == 0 means we found what we were searching for, which
  1811. * is weird, but possible, so only screw with path if we didnt
  1812. * find the key and see if we have stuff that matches
  1813. */
  1814. if (ret > 0) {
  1815. if (path->slots[0] == 0)
  1816. break;
  1817. path->slots[0]--;
  1818. }
  1819. /* pull out the item */
  1820. leaf = path->nodes[0];
  1821. item = btrfs_item_nr(leaf, path->slots[0]);
  1822. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1823. /* make sure the item matches what we want */
  1824. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  1825. break;
  1826. if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
  1827. break;
  1828. /* release the path since we're done with it */
  1829. btrfs_release_path(root, path);
  1830. /*
  1831. * this is where we are basically btrfs_lookup, without the
  1832. * crossing root thing. we store the inode number in the
  1833. * offset of the orphan item.
  1834. */
  1835. found_key.objectid = found_key.offset;
  1836. found_key.type = BTRFS_INODE_ITEM_KEY;
  1837. found_key.offset = 0;
  1838. inode = btrfs_iget(root->fs_info->sb, &found_key, root);
  1839. if (IS_ERR(inode))
  1840. break;
  1841. /*
  1842. * add this inode to the orphan list so btrfs_orphan_del does
  1843. * the proper thing when we hit it
  1844. */
  1845. spin_lock(&root->list_lock);
  1846. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1847. spin_unlock(&root->list_lock);
  1848. /*
  1849. * if this is a bad inode, means we actually succeeded in
  1850. * removing the inode, but not the orphan record, which means
  1851. * we need to manually delete the orphan since iput will just
  1852. * do a destroy_inode
  1853. */
  1854. if (is_bad_inode(inode)) {
  1855. trans = btrfs_start_transaction(root, 1);
  1856. btrfs_orphan_del(trans, inode);
  1857. btrfs_end_transaction(trans, root);
  1858. iput(inode);
  1859. continue;
  1860. }
  1861. /* if we have links, this was a truncate, lets do that */
  1862. if (inode->i_nlink) {
  1863. nr_truncate++;
  1864. btrfs_truncate(inode);
  1865. } else {
  1866. nr_unlink++;
  1867. }
  1868. /* this will do delete_inode and everything for us */
  1869. iput(inode);
  1870. }
  1871. if (nr_unlink)
  1872. printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
  1873. if (nr_truncate)
  1874. printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
  1875. btrfs_free_path(path);
  1876. }
  1877. /*
  1878. * very simple check to peek ahead in the leaf looking for xattrs. If we
  1879. * don't find any xattrs, we know there can't be any acls.
  1880. *
  1881. * slot is the slot the inode is in, objectid is the objectid of the inode
  1882. */
  1883. static noinline int acls_after_inode_item(struct extent_buffer *leaf,
  1884. int slot, u64 objectid)
  1885. {
  1886. u32 nritems = btrfs_header_nritems(leaf);
  1887. struct btrfs_key found_key;
  1888. int scanned = 0;
  1889. slot++;
  1890. while (slot < nritems) {
  1891. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1892. /* we found a different objectid, there must not be acls */
  1893. if (found_key.objectid != objectid)
  1894. return 0;
  1895. /* we found an xattr, assume we've got an acl */
  1896. if (found_key.type == BTRFS_XATTR_ITEM_KEY)
  1897. return 1;
  1898. /*
  1899. * we found a key greater than an xattr key, there can't
  1900. * be any acls later on
  1901. */
  1902. if (found_key.type > BTRFS_XATTR_ITEM_KEY)
  1903. return 0;
  1904. slot++;
  1905. scanned++;
  1906. /*
  1907. * it goes inode, inode backrefs, xattrs, extents,
  1908. * so if there are a ton of hard links to an inode there can
  1909. * be a lot of backrefs. Don't waste time searching too hard,
  1910. * this is just an optimization
  1911. */
  1912. if (scanned >= 8)
  1913. break;
  1914. }
  1915. /* we hit the end of the leaf before we found an xattr or
  1916. * something larger than an xattr. We have to assume the inode
  1917. * has acls
  1918. */
  1919. return 1;
  1920. }
  1921. /*
  1922. * read an inode from the btree into the in-memory inode
  1923. */
  1924. static void btrfs_read_locked_inode(struct inode *inode)
  1925. {
  1926. struct btrfs_path *path;
  1927. struct extent_buffer *leaf;
  1928. struct btrfs_inode_item *inode_item;
  1929. struct btrfs_timespec *tspec;
  1930. struct btrfs_root *root = BTRFS_I(inode)->root;
  1931. struct btrfs_key location;
  1932. int maybe_acls;
  1933. u64 alloc_group_block;
  1934. u32 rdev;
  1935. int ret;
  1936. path = btrfs_alloc_path();
  1937. BUG_ON(!path);
  1938. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  1939. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  1940. if (ret)
  1941. goto make_bad;
  1942. leaf = path->nodes[0];
  1943. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  1944. struct btrfs_inode_item);
  1945. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  1946. inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
  1947. inode->i_uid = btrfs_inode_uid(leaf, inode_item);
  1948. inode->i_gid = btrfs_inode_gid(leaf, inode_item);
  1949. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  1950. tspec = btrfs_inode_atime(inode_item);
  1951. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1952. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1953. tspec = btrfs_inode_mtime(inode_item);
  1954. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1955. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1956. tspec = btrfs_inode_ctime(inode_item);
  1957. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1958. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1959. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  1960. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  1961. BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
  1962. inode->i_generation = BTRFS_I(inode)->generation;
  1963. inode->i_rdev = 0;
  1964. rdev = btrfs_inode_rdev(leaf, inode_item);
  1965. BTRFS_I(inode)->index_cnt = (u64)-1;
  1966. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  1967. alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
  1968. /*
  1969. * try to precache a NULL acl entry for files that don't have
  1970. * any xattrs or acls
  1971. */
  1972. maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
  1973. if (!maybe_acls)
  1974. cache_no_acl(inode);
  1975. BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
  1976. alloc_group_block, 0);
  1977. btrfs_free_path(path);
  1978. inode_item = NULL;
  1979. switch (inode->i_mode & S_IFMT) {
  1980. case S_IFREG:
  1981. inode->i_mapping->a_ops = &btrfs_aops;
  1982. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  1983. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  1984. inode->i_fop = &btrfs_file_operations;
  1985. inode->i_op = &btrfs_file_inode_operations;
  1986. break;
  1987. case S_IFDIR:
  1988. inode->i_fop = &btrfs_dir_file_operations;
  1989. if (root == root->fs_info->tree_root)
  1990. inode->i_op = &btrfs_dir_ro_inode_operations;
  1991. else
  1992. inode->i_op = &btrfs_dir_inode_operations;
  1993. break;
  1994. case S_IFLNK:
  1995. inode->i_op = &btrfs_symlink_inode_operations;
  1996. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  1997. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  1998. break;
  1999. default:
  2000. inode->i_op = &btrfs_special_inode_operations;
  2001. init_special_inode(inode, inode->i_mode, rdev);
  2002. break;
  2003. }
  2004. btrfs_update_iflags(inode);
  2005. return;
  2006. make_bad:
  2007. btrfs_free_path(path);
  2008. make_bad_inode(inode);
  2009. }
  2010. /*
  2011. * given a leaf and an inode, copy the inode fields into the leaf
  2012. */
  2013. static void fill_inode_item(struct btrfs_trans_handle *trans,
  2014. struct extent_buffer *leaf,
  2015. struct btrfs_inode_item *item,
  2016. struct inode *inode)
  2017. {
  2018. btrfs_set_inode_uid(leaf, item, inode->i_uid);
  2019. btrfs_set_inode_gid(leaf, item, inode->i_gid);
  2020. btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
  2021. btrfs_set_inode_mode(leaf, item, inode->i_mode);
  2022. btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
  2023. btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
  2024. inode->i_atime.tv_sec);
  2025. btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
  2026. inode->i_atime.tv_nsec);
  2027. btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
  2028. inode->i_mtime.tv_sec);
  2029. btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
  2030. inode->i_mtime.tv_nsec);
  2031. btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
  2032. inode->i_ctime.tv_sec);
  2033. btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
  2034. inode->i_ctime.tv_nsec);
  2035. btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
  2036. btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
  2037. btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
  2038. btrfs_set_inode_transid(leaf, item, trans->transid);
  2039. btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
  2040. btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
  2041. btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
  2042. }
  2043. /*
  2044. * copy everything in the in-memory inode into the btree.
  2045. */
  2046. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  2047. struct btrfs_root *root, struct inode *inode)
  2048. {
  2049. struct btrfs_inode_item *inode_item;
  2050. struct btrfs_path *path;
  2051. struct extent_buffer *leaf;
  2052. int ret;
  2053. path = btrfs_alloc_path();
  2054. BUG_ON(!path);
  2055. path->leave_spinning = 1;
  2056. ret = btrfs_lookup_inode(trans, root, path,
  2057. &BTRFS_I(inode)->location, 1);
  2058. if (ret) {
  2059. if (ret > 0)
  2060. ret = -ENOENT;
  2061. goto failed;
  2062. }
  2063. btrfs_unlock_up_safe(path, 1);
  2064. leaf = path->nodes[0];
  2065. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  2066. struct btrfs_inode_item);
  2067. fill_inode_item(trans, leaf, inode_item, inode);
  2068. btrfs_mark_buffer_dirty(leaf);
  2069. btrfs_set_inode_last_trans(trans, inode);
  2070. ret = 0;
  2071. failed:
  2072. btrfs_free_path(path);
  2073. return ret;
  2074. }
  2075. /*
  2076. * unlink helper that gets used here in inode.c and in the tree logging
  2077. * recovery code. It remove a link in a directory with a given name, and
  2078. * also drops the back refs in the inode to the directory
  2079. */
  2080. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  2081. struct btrfs_root *root,
  2082. struct inode *dir, struct inode *inode,
  2083. const char *name, int name_len)
  2084. {
  2085. struct btrfs_path *path;
  2086. int ret = 0;
  2087. struct extent_buffer *leaf;
  2088. struct btrfs_dir_item *di;
  2089. struct btrfs_key key;
  2090. u64 index;
  2091. path = btrfs_alloc_path();
  2092. if (!path) {
  2093. ret = -ENOMEM;
  2094. goto err;
  2095. }
  2096. path->leave_spinning = 1;
  2097. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2098. name, name_len, -1);
  2099. if (IS_ERR(di)) {
  2100. ret = PTR_ERR(di);
  2101. goto err;
  2102. }
  2103. if (!di) {
  2104. ret = -ENOENT;
  2105. goto err;
  2106. }
  2107. leaf = path->nodes[0];
  2108. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2109. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2110. if (ret)
  2111. goto err;
  2112. btrfs_release_path(root, path);
  2113. ret = btrfs_del_inode_ref(trans, root, name, name_len,
  2114. inode->i_ino,
  2115. dir->i_ino, &index);
  2116. if (ret) {
  2117. printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
  2118. "inode %lu parent %lu\n", name_len, name,
  2119. inode->i_ino, dir->i_ino);
  2120. goto err;
  2121. }
  2122. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  2123. index, name, name_len, -1);
  2124. if (IS_ERR(di)) {
  2125. ret = PTR_ERR(di);
  2126. goto err;
  2127. }
  2128. if (!di) {
  2129. ret = -ENOENT;
  2130. goto err;
  2131. }
  2132. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2133. btrfs_release_path(root, path);
  2134. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  2135. inode, dir->i_ino);
  2136. BUG_ON(ret != 0 && ret != -ENOENT);
  2137. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  2138. dir, index);
  2139. BUG_ON(ret);
  2140. err:
  2141. btrfs_free_path(path);
  2142. if (ret)
  2143. goto out;
  2144. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2145. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2146. btrfs_update_inode(trans, root, dir);
  2147. btrfs_drop_nlink(inode);
  2148. ret = btrfs_update_inode(trans, root, inode);
  2149. out:
  2150. return ret;
  2151. }
  2152. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  2153. {
  2154. struct btrfs_root *root;
  2155. struct btrfs_trans_handle *trans;
  2156. struct inode *inode = dentry->d_inode;
  2157. int ret;
  2158. unsigned long nr = 0;
  2159. root = BTRFS_I(dir)->root;
  2160. trans = btrfs_start_transaction(root, 1);
  2161. btrfs_set_trans_block_group(trans, dir);
  2162. btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
  2163. ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2164. dentry->d_name.name, dentry->d_name.len);
  2165. if (inode->i_nlink == 0)
  2166. ret = btrfs_orphan_add(trans, inode);
  2167. nr = trans->blocks_used;
  2168. btrfs_end_transaction_throttle(trans, root);
  2169. btrfs_btree_balance_dirty(root, nr);
  2170. return ret;
  2171. }
  2172. int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
  2173. struct btrfs_root *root,
  2174. struct inode *dir, u64 objectid,
  2175. const char *name, int name_len)
  2176. {
  2177. struct btrfs_path *path;
  2178. struct extent_buffer *leaf;
  2179. struct btrfs_dir_item *di;
  2180. struct btrfs_key key;
  2181. u64 index;
  2182. int ret;
  2183. path = btrfs_alloc_path();
  2184. if (!path)
  2185. return -ENOMEM;
  2186. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  2187. name, name_len, -1);
  2188. BUG_ON(!di || IS_ERR(di));
  2189. leaf = path->nodes[0];
  2190. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2191. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  2192. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2193. BUG_ON(ret);
  2194. btrfs_release_path(root, path);
  2195. ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
  2196. objectid, root->root_key.objectid,
  2197. dir->i_ino, &index, name, name_len);
  2198. if (ret < 0) {
  2199. BUG_ON(ret != -ENOENT);
  2200. di = btrfs_search_dir_index_item(root, path, dir->i_ino,
  2201. name, name_len);
  2202. BUG_ON(!di || IS_ERR(di));
  2203. leaf = path->nodes[0];
  2204. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  2205. btrfs_release_path(root, path);
  2206. index = key.offset;
  2207. }
  2208. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  2209. index, name, name_len, -1);
  2210. BUG_ON(!di || IS_ERR(di));
  2211. leaf = path->nodes[0];
  2212. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  2213. WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
  2214. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  2215. BUG_ON(ret);
  2216. btrfs_release_path(root, path);
  2217. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  2218. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  2219. ret = btrfs_update_inode(trans, root, dir);
  2220. BUG_ON(ret);
  2221. dir->i_sb->s_dirt = 1;
  2222. btrfs_free_path(path);
  2223. return 0;
  2224. }
  2225. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  2226. {
  2227. struct inode *inode = dentry->d_inode;
  2228. int err = 0;
  2229. int ret;
  2230. struct btrfs_root *root = BTRFS_I(dir)->root;
  2231. struct btrfs_trans_handle *trans;
  2232. unsigned long nr = 0;
  2233. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
  2234. inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  2235. return -ENOTEMPTY;
  2236. trans = btrfs_start_transaction(root, 1);
  2237. btrfs_set_trans_block_group(trans, dir);
  2238. if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  2239. err = btrfs_unlink_subvol(trans, root, dir,
  2240. BTRFS_I(inode)->location.objectid,
  2241. dentry->d_name.name,
  2242. dentry->d_name.len);
  2243. goto out;
  2244. }
  2245. err = btrfs_orphan_add(trans, inode);
  2246. if (err)
  2247. goto out;
  2248. /* now the directory is empty */
  2249. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2250. dentry->d_name.name, dentry->d_name.len);
  2251. if (!err)
  2252. btrfs_i_size_write(inode, 0);
  2253. out:
  2254. nr = trans->blocks_used;
  2255. ret = btrfs_end_transaction_throttle(trans, root);
  2256. btrfs_btree_balance_dirty(root, nr);
  2257. if (ret && !err)
  2258. err = ret;
  2259. return err;
  2260. }
  2261. #if 0
  2262. /*
  2263. * when truncating bytes in a file, it is possible to avoid reading
  2264. * the leaves that contain only checksum items. This can be the
  2265. * majority of the IO required to delete a large file, but it must
  2266. * be done carefully.
  2267. *
  2268. * The keys in the level just above the leaves are checked to make sure
  2269. * the lowest key in a given leaf is a csum key, and starts at an offset
  2270. * after the new size.
  2271. *
  2272. * Then the key for the next leaf is checked to make sure it also has
  2273. * a checksum item for the same file. If it does, we know our target leaf
  2274. * contains only checksum items, and it can be safely freed without reading
  2275. * it.
  2276. *
  2277. * This is just an optimization targeted at large files. It may do
  2278. * nothing. It will return 0 unless things went badly.
  2279. */
  2280. static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
  2281. struct btrfs_root *root,
  2282. struct btrfs_path *path,
  2283. struct inode *inode, u64 new_size)
  2284. {
  2285. struct btrfs_key key;
  2286. int ret;
  2287. int nritems;
  2288. struct btrfs_key found_key;
  2289. struct btrfs_key other_key;
  2290. struct btrfs_leaf_ref *ref;
  2291. u64 leaf_gen;
  2292. u64 leaf_start;
  2293. path->lowest_level = 1;
  2294. key.objectid = inode->i_ino;
  2295. key.type = BTRFS_CSUM_ITEM_KEY;
  2296. key.offset = new_size;
  2297. again:
  2298. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2299. if (ret < 0)
  2300. goto out;
  2301. if (path->nodes[1] == NULL) {
  2302. ret = 0;
  2303. goto out;
  2304. }
  2305. ret = 0;
  2306. btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
  2307. nritems = btrfs_header_nritems(path->nodes[1]);
  2308. if (!nritems)
  2309. goto out;
  2310. if (path->slots[1] >= nritems)
  2311. goto next_node;
  2312. /* did we find a key greater than anything we want to delete? */
  2313. if (found_key.objectid > inode->i_ino ||
  2314. (found_key.objectid == inode->i_ino && found_key.type > key.type))
  2315. goto out;
  2316. /* we check the next key in the node to make sure the leave contains
  2317. * only checksum items. This comparison doesn't work if our
  2318. * leaf is the last one in the node
  2319. */
  2320. if (path->slots[1] + 1 >= nritems) {
  2321. next_node:
  2322. /* search forward from the last key in the node, this
  2323. * will bring us into the next node in the tree
  2324. */
  2325. btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
  2326. /* unlikely, but we inc below, so check to be safe */
  2327. if (found_key.offset == (u64)-1)
  2328. goto out;
  2329. /* search_forward needs a path with locks held, do the
  2330. * search again for the original key. It is possible
  2331. * this will race with a balance and return a path that
  2332. * we could modify, but this drop is just an optimization
  2333. * and is allowed to miss some leaves.
  2334. */
  2335. btrfs_release_path(root, path);
  2336. found_key.offset++;
  2337. /* setup a max key for search_forward */
  2338. other_key.offset = (u64)-1;
  2339. other_key.type = key.type;
  2340. other_key.objectid = key.objectid;
  2341. path->keep_locks = 1;
  2342. ret = btrfs_search_forward(root, &found_key, &other_key,
  2343. path, 0, 0);
  2344. path->keep_locks = 0;
  2345. if (ret || found_key.objectid != key.objectid ||
  2346. found_key.type != key.type) {
  2347. ret = 0;
  2348. goto out;
  2349. }
  2350. key.offset = found_key.offset;
  2351. btrfs_release_path(root, path);
  2352. cond_resched();
  2353. goto again;
  2354. }
  2355. /* we know there's one more slot after us in the tree,
  2356. * read that key so we can verify it is also a checksum item
  2357. */
  2358. btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
  2359. if (found_key.objectid < inode->i_ino)
  2360. goto next_key;
  2361. if (found_key.type != key.type || found_key.offset < new_size)
  2362. goto next_key;
  2363. /*
  2364. * if the key for the next leaf isn't a csum key from this objectid,
  2365. * we can't be sure there aren't good items inside this leaf.
  2366. * Bail out
  2367. */
  2368. if (other_key.objectid != inode->i_ino || other_key.type != key.type)
  2369. goto out;
  2370. leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
  2371. leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
  2372. /*
  2373. * it is safe to delete this leaf, it contains only
  2374. * csum items from this inode at an offset >= new_size
  2375. */
  2376. ret = btrfs_del_leaf(trans, root, path, leaf_start);
  2377. BUG_ON(ret);
  2378. if (root->ref_cows && leaf_gen < trans->transid) {
  2379. ref = btrfs_alloc_leaf_ref(root, 0);
  2380. if (ref) {
  2381. ref->root_gen = root->root_key.offset;
  2382. ref->bytenr = leaf_start;
  2383. ref->owner = 0;
  2384. ref->generation = leaf_gen;
  2385. ref->nritems = 0;
  2386. btrfs_sort_leaf_ref(ref);
  2387. ret = btrfs_add_leaf_ref(root, ref, 0);
  2388. WARN_ON(ret);
  2389. btrfs_free_leaf_ref(root, ref);
  2390. } else {
  2391. WARN_ON(1);
  2392. }
  2393. }
  2394. next_key:
  2395. btrfs_release_path(root, path);
  2396. if (other_key.objectid == inode->i_ino &&
  2397. other_key.type == key.type && other_key.offset > key.offset) {
  2398. key.offset = other_key.offset;
  2399. cond_resched();
  2400. goto again;
  2401. }
  2402. ret = 0;
  2403. out:
  2404. /* fixup any changes we've made to the path */
  2405. path->lowest_level = 0;
  2406. path->keep_locks = 0;
  2407. btrfs_release_path(root, path);
  2408. return ret;
  2409. }
  2410. #endif
  2411. /*
  2412. * this can truncate away extent items, csum items and directory items.
  2413. * It starts at a high offset and removes keys until it can't find
  2414. * any higher than new_size
  2415. *
  2416. * csum items that cross the new i_size are truncated to the new size
  2417. * as well.
  2418. *
  2419. * min_type is the minimum key type to truncate down to. If set to 0, this
  2420. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  2421. */
  2422. noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  2423. struct btrfs_root *root,
  2424. struct inode *inode,
  2425. u64 new_size, u32 min_type)
  2426. {
  2427. int ret;
  2428. struct btrfs_path *path;
  2429. struct btrfs_key key;
  2430. struct btrfs_key found_key;
  2431. u32 found_type = (u8)-1;
  2432. struct extent_buffer *leaf;
  2433. struct btrfs_file_extent_item *fi;
  2434. u64 extent_start = 0;
  2435. u64 extent_num_bytes = 0;
  2436. u64 extent_offset = 0;
  2437. u64 item_end = 0;
  2438. int found_extent;
  2439. int del_item;
  2440. int pending_del_nr = 0;
  2441. int pending_del_slot = 0;
  2442. int extent_type = -1;
  2443. int encoding;
  2444. u64 mask = root->sectorsize - 1;
  2445. if (root->ref_cows)
  2446. btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
  2447. path = btrfs_alloc_path();
  2448. BUG_ON(!path);
  2449. path->reada = -1;
  2450. /* FIXME, add redo link to tree so we don't leak on crash */
  2451. key.objectid = inode->i_ino;
  2452. key.offset = (u64)-1;
  2453. key.type = (u8)-1;
  2454. search_again:
  2455. path->leave_spinning = 1;
  2456. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2457. if (ret < 0)
  2458. goto error;
  2459. if (ret > 0) {
  2460. /* there are no items in the tree for us to truncate, we're
  2461. * done
  2462. */
  2463. if (path->slots[0] == 0) {
  2464. ret = 0;
  2465. goto error;
  2466. }
  2467. path->slots[0]--;
  2468. }
  2469. while (1) {
  2470. fi = NULL;
  2471. leaf = path->nodes[0];
  2472. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2473. found_type = btrfs_key_type(&found_key);
  2474. encoding = 0;
  2475. if (found_key.objectid != inode->i_ino)
  2476. break;
  2477. if (found_type < min_type)
  2478. break;
  2479. item_end = found_key.offset;
  2480. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  2481. fi = btrfs_item_ptr(leaf, path->slots[0],
  2482. struct btrfs_file_extent_item);
  2483. extent_type = btrfs_file_extent_type(leaf, fi);
  2484. encoding = btrfs_file_extent_compression(leaf, fi);
  2485. encoding |= btrfs_file_extent_encryption(leaf, fi);
  2486. encoding |= btrfs_file_extent_other_encoding(leaf, fi);
  2487. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2488. item_end +=
  2489. btrfs_file_extent_num_bytes(leaf, fi);
  2490. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2491. item_end += btrfs_file_extent_inline_len(leaf,
  2492. fi);
  2493. }
  2494. item_end--;
  2495. }
  2496. if (item_end < new_size) {
  2497. if (found_type == BTRFS_DIR_ITEM_KEY)
  2498. found_type = BTRFS_INODE_ITEM_KEY;
  2499. else if (found_type == BTRFS_EXTENT_ITEM_KEY)
  2500. found_type = BTRFS_EXTENT_DATA_KEY;
  2501. else if (found_type == BTRFS_EXTENT_DATA_KEY)
  2502. found_type = BTRFS_XATTR_ITEM_KEY;
  2503. else if (found_type == BTRFS_XATTR_ITEM_KEY)
  2504. found_type = BTRFS_INODE_REF_KEY;
  2505. else if (found_type)
  2506. found_type--;
  2507. else
  2508. break;
  2509. btrfs_set_key_type(&key, found_type);
  2510. goto next;
  2511. }
  2512. if (found_key.offset >= new_size)
  2513. del_item = 1;
  2514. else
  2515. del_item = 0;
  2516. found_extent = 0;
  2517. /* FIXME, shrink the extent if the ref count is only 1 */
  2518. if (found_type != BTRFS_EXTENT_DATA_KEY)
  2519. goto delete;
  2520. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2521. u64 num_dec;
  2522. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  2523. if (!del_item && !encoding) {
  2524. u64 orig_num_bytes =
  2525. btrfs_file_extent_num_bytes(leaf, fi);
  2526. extent_num_bytes = new_size -
  2527. found_key.offset + root->sectorsize - 1;
  2528. extent_num_bytes = extent_num_bytes &
  2529. ~((u64)root->sectorsize - 1);
  2530. btrfs_set_file_extent_num_bytes(leaf, fi,
  2531. extent_num_bytes);
  2532. num_dec = (orig_num_bytes -
  2533. extent_num_bytes);
  2534. if (root->ref_cows && extent_start != 0)
  2535. inode_sub_bytes(inode, num_dec);
  2536. btrfs_mark_buffer_dirty(leaf);
  2537. } else {
  2538. extent_num_bytes =
  2539. btrfs_file_extent_disk_num_bytes(leaf,
  2540. fi);
  2541. extent_offset = found_key.offset -
  2542. btrfs_file_extent_offset(leaf, fi);
  2543. /* FIXME blocksize != 4096 */
  2544. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  2545. if (extent_start != 0) {
  2546. found_extent = 1;
  2547. if (root->ref_cows)
  2548. inode_sub_bytes(inode, num_dec);
  2549. }
  2550. }
  2551. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2552. /*
  2553. * we can't truncate inline items that have had
  2554. * special encodings
  2555. */
  2556. if (!del_item &&
  2557. btrfs_file_extent_compression(leaf, fi) == 0 &&
  2558. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  2559. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  2560. u32 size = new_size - found_key.offset;
  2561. if (root->ref_cows) {
  2562. inode_sub_bytes(inode, item_end + 1 -
  2563. new_size);
  2564. }
  2565. size =
  2566. btrfs_file_extent_calc_inline_size(size);
  2567. ret = btrfs_truncate_item(trans, root, path,
  2568. size, 1);
  2569. BUG_ON(ret);
  2570. } else if (root->ref_cows) {
  2571. inode_sub_bytes(inode, item_end + 1 -
  2572. found_key.offset);
  2573. }
  2574. }
  2575. delete:
  2576. if (del_item) {
  2577. if (!pending_del_nr) {
  2578. /* no pending yet, add ourselves */
  2579. pending_del_slot = path->slots[0];
  2580. pending_del_nr = 1;
  2581. } else if (pending_del_nr &&
  2582. path->slots[0] + 1 == pending_del_slot) {
  2583. /* hop on the pending chunk */
  2584. pending_del_nr++;
  2585. pending_del_slot = path->slots[0];
  2586. } else {
  2587. BUG();
  2588. }
  2589. } else {
  2590. break;
  2591. }
  2592. if (found_extent && root->ref_cows) {
  2593. btrfs_set_path_blocking(path);
  2594. ret = btrfs_free_extent(trans, root, extent_start,
  2595. extent_num_bytes, 0,
  2596. btrfs_header_owner(leaf),
  2597. inode->i_ino, extent_offset);
  2598. BUG_ON(ret);
  2599. }
  2600. next:
  2601. if (path->slots[0] == 0) {
  2602. if (pending_del_nr)
  2603. goto del_pending;
  2604. btrfs_release_path(root, path);
  2605. if (found_type == BTRFS_INODE_ITEM_KEY)
  2606. break;
  2607. goto search_again;
  2608. }
  2609. path->slots[0]--;
  2610. if (pending_del_nr &&
  2611. path->slots[0] + 1 != pending_del_slot) {
  2612. struct btrfs_key debug;
  2613. del_pending:
  2614. btrfs_item_key_to_cpu(path->nodes[0], &debug,
  2615. pending_del_slot);
  2616. ret = btrfs_del_items(trans, root, path,
  2617. pending_del_slot,
  2618. pending_del_nr);
  2619. BUG_ON(ret);
  2620. pending_del_nr = 0;
  2621. btrfs_release_path(root, path);
  2622. if (found_type == BTRFS_INODE_ITEM_KEY)
  2623. break;
  2624. goto search_again;
  2625. }
  2626. }
  2627. ret = 0;
  2628. error:
  2629. if (pending_del_nr) {
  2630. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  2631. pending_del_nr);
  2632. }
  2633. btrfs_free_path(path);
  2634. return ret;
  2635. }
  2636. /*
  2637. * taken from block_truncate_page, but does cow as it zeros out
  2638. * any bytes left in the last page in the file.
  2639. */
  2640. static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
  2641. {
  2642. struct inode *inode = mapping->host;
  2643. struct btrfs_root *root = BTRFS_I(inode)->root;
  2644. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2645. struct btrfs_ordered_extent *ordered;
  2646. char *kaddr;
  2647. u32 blocksize = root->sectorsize;
  2648. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  2649. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  2650. struct page *page;
  2651. int ret = 0;
  2652. u64 page_start;
  2653. u64 page_end;
  2654. if ((offset & (blocksize - 1)) == 0)
  2655. goto out;
  2656. ret = -ENOMEM;
  2657. again:
  2658. page = grab_cache_page(mapping, index);
  2659. if (!page)
  2660. goto out;
  2661. page_start = page_offset(page);
  2662. page_end = page_start + PAGE_CACHE_SIZE - 1;
  2663. if (!PageUptodate(page)) {
  2664. ret = btrfs_readpage(NULL, page);
  2665. lock_page(page);
  2666. if (page->mapping != mapping) {
  2667. unlock_page(page);
  2668. page_cache_release(page);
  2669. goto again;
  2670. }
  2671. if (!PageUptodate(page)) {
  2672. ret = -EIO;
  2673. goto out_unlock;
  2674. }
  2675. }
  2676. wait_on_page_writeback(page);
  2677. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2678. set_page_extent_mapped(page);
  2679. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  2680. if (ordered) {
  2681. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2682. unlock_page(page);
  2683. page_cache_release(page);
  2684. btrfs_start_ordered_extent(inode, ordered, 1);
  2685. btrfs_put_ordered_extent(ordered);
  2686. goto again;
  2687. }
  2688. ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
  2689. if (ret) {
  2690. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2691. goto out_unlock;
  2692. }
  2693. ret = 0;
  2694. if (offset != PAGE_CACHE_SIZE) {
  2695. kaddr = kmap(page);
  2696. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  2697. flush_dcache_page(page);
  2698. kunmap(page);
  2699. }
  2700. ClearPageChecked(page);
  2701. set_page_dirty(page);
  2702. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2703. out_unlock:
  2704. unlock_page(page);
  2705. page_cache_release(page);
  2706. out:
  2707. return ret;
  2708. }
  2709. int btrfs_cont_expand(struct inode *inode, loff_t size)
  2710. {
  2711. struct btrfs_trans_handle *trans;
  2712. struct btrfs_root *root = BTRFS_I(inode)->root;
  2713. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2714. struct extent_map *em;
  2715. u64 mask = root->sectorsize - 1;
  2716. u64 hole_start = (inode->i_size + mask) & ~mask;
  2717. u64 block_end = (size + mask) & ~mask;
  2718. u64 last_byte;
  2719. u64 cur_offset;
  2720. u64 hole_size;
  2721. int err = 0;
  2722. if (size <= hole_start)
  2723. return 0;
  2724. btrfs_truncate_page(inode->i_mapping, inode->i_size);
  2725. while (1) {
  2726. struct btrfs_ordered_extent *ordered;
  2727. btrfs_wait_ordered_range(inode, hole_start,
  2728. block_end - hole_start);
  2729. lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2730. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  2731. if (!ordered)
  2732. break;
  2733. unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2734. btrfs_put_ordered_extent(ordered);
  2735. }
  2736. trans = btrfs_start_transaction(root, 1);
  2737. btrfs_set_trans_block_group(trans, inode);
  2738. cur_offset = hole_start;
  2739. while (1) {
  2740. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  2741. block_end - cur_offset, 0);
  2742. BUG_ON(IS_ERR(em) || !em);
  2743. last_byte = min(extent_map_end(em), block_end);
  2744. last_byte = (last_byte + mask) & ~mask;
  2745. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  2746. u64 hint_byte = 0;
  2747. hole_size = last_byte - cur_offset;
  2748. err = btrfs_drop_extents(trans, root, inode,
  2749. cur_offset,
  2750. cur_offset + hole_size,
  2751. block_end,
  2752. cur_offset, &hint_byte, 1);
  2753. if (err)
  2754. break;
  2755. err = btrfs_reserve_metadata_space(root, 1);
  2756. if (err)
  2757. break;
  2758. err = btrfs_insert_file_extent(trans, root,
  2759. inode->i_ino, cur_offset, 0,
  2760. 0, hole_size, 0, hole_size,
  2761. 0, 0, 0);
  2762. btrfs_drop_extent_cache(inode, hole_start,
  2763. last_byte - 1, 0);
  2764. btrfs_unreserve_metadata_space(root, 1);
  2765. }
  2766. free_extent_map(em);
  2767. cur_offset = last_byte;
  2768. if (err || cur_offset >= block_end)
  2769. break;
  2770. }
  2771. btrfs_end_transaction(trans, root);
  2772. unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2773. return err;
  2774. }
  2775. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  2776. {
  2777. struct inode *inode = dentry->d_inode;
  2778. int err;
  2779. err = inode_change_ok(inode, attr);
  2780. if (err)
  2781. return err;
  2782. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  2783. if (attr->ia_size > inode->i_size) {
  2784. err = btrfs_cont_expand(inode, attr->ia_size);
  2785. if (err)
  2786. return err;
  2787. } else if (inode->i_size > 0 &&
  2788. attr->ia_size == 0) {
  2789. /* we're truncating a file that used to have good
  2790. * data down to zero. Make sure it gets into
  2791. * the ordered flush list so that any new writes
  2792. * get down to disk quickly.
  2793. */
  2794. BTRFS_I(inode)->ordered_data_close = 1;
  2795. }
  2796. }
  2797. err = inode_setattr(inode, attr);
  2798. if (!err && ((attr->ia_valid & ATTR_MODE)))
  2799. err = btrfs_acl_chmod(inode);
  2800. return err;
  2801. }
  2802. void btrfs_delete_inode(struct inode *inode)
  2803. {
  2804. struct btrfs_trans_handle *trans;
  2805. struct btrfs_root *root = BTRFS_I(inode)->root;
  2806. unsigned long nr;
  2807. int ret;
  2808. truncate_inode_pages(&inode->i_data, 0);
  2809. if (is_bad_inode(inode)) {
  2810. btrfs_orphan_del(NULL, inode);
  2811. goto no_delete;
  2812. }
  2813. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  2814. if (inode->i_nlink > 0) {
  2815. BUG_ON(btrfs_root_refs(&root->root_item) != 0);
  2816. goto no_delete;
  2817. }
  2818. btrfs_i_size_write(inode, 0);
  2819. trans = btrfs_join_transaction(root, 1);
  2820. btrfs_set_trans_block_group(trans, inode);
  2821. ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
  2822. if (ret) {
  2823. btrfs_orphan_del(NULL, inode);
  2824. goto no_delete_lock;
  2825. }
  2826. btrfs_orphan_del(trans, inode);
  2827. nr = trans->blocks_used;
  2828. clear_inode(inode);
  2829. btrfs_end_transaction(trans, root);
  2830. btrfs_btree_balance_dirty(root, nr);
  2831. return;
  2832. no_delete_lock:
  2833. nr = trans->blocks_used;
  2834. btrfs_end_transaction(trans, root);
  2835. btrfs_btree_balance_dirty(root, nr);
  2836. no_delete:
  2837. clear_inode(inode);
  2838. }
  2839. /*
  2840. * this returns the key found in the dir entry in the location pointer.
  2841. * If no dir entries were found, location->objectid is 0.
  2842. */
  2843. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  2844. struct btrfs_key *location)
  2845. {
  2846. const char *name = dentry->d_name.name;
  2847. int namelen = dentry->d_name.len;
  2848. struct btrfs_dir_item *di;
  2849. struct btrfs_path *path;
  2850. struct btrfs_root *root = BTRFS_I(dir)->root;
  2851. int ret = 0;
  2852. path = btrfs_alloc_path();
  2853. BUG_ON(!path);
  2854. di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  2855. namelen, 0);
  2856. if (IS_ERR(di))
  2857. ret = PTR_ERR(di);
  2858. if (!di || IS_ERR(di))
  2859. goto out_err;
  2860. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  2861. out:
  2862. btrfs_free_path(path);
  2863. return ret;
  2864. out_err:
  2865. location->objectid = 0;
  2866. goto out;
  2867. }
  2868. /*
  2869. * when we hit a tree root in a directory, the btrfs part of the inode
  2870. * needs to be changed to reflect the root directory of the tree root. This
  2871. * is kind of like crossing a mount point.
  2872. */
  2873. static int fixup_tree_root_location(struct btrfs_root *root,
  2874. struct inode *dir,
  2875. struct dentry *dentry,
  2876. struct btrfs_key *location,
  2877. struct btrfs_root **sub_root)
  2878. {
  2879. struct btrfs_path *path;
  2880. struct btrfs_root *new_root;
  2881. struct btrfs_root_ref *ref;
  2882. struct extent_buffer *leaf;
  2883. int ret;
  2884. int err = 0;
  2885. path = btrfs_alloc_path();
  2886. if (!path) {
  2887. err = -ENOMEM;
  2888. goto out;
  2889. }
  2890. err = -ENOENT;
  2891. ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
  2892. BTRFS_I(dir)->root->root_key.objectid,
  2893. location->objectid);
  2894. if (ret) {
  2895. if (ret < 0)
  2896. err = ret;
  2897. goto out;
  2898. }
  2899. leaf = path->nodes[0];
  2900. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  2901. if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
  2902. btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
  2903. goto out;
  2904. ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
  2905. (unsigned long)(ref + 1),
  2906. dentry->d_name.len);
  2907. if (ret)
  2908. goto out;
  2909. btrfs_release_path(root->fs_info->tree_root, path);
  2910. new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
  2911. if (IS_ERR(new_root)) {
  2912. err = PTR_ERR(new_root);
  2913. goto out;
  2914. }
  2915. if (btrfs_root_refs(&new_root->root_item) == 0) {
  2916. err = -ENOENT;
  2917. goto out;
  2918. }
  2919. *sub_root = new_root;
  2920. location->objectid = btrfs_root_dirid(&new_root->root_item);
  2921. location->type = BTRFS_INODE_ITEM_KEY;
  2922. location->offset = 0;
  2923. err = 0;
  2924. out:
  2925. btrfs_free_path(path);
  2926. return err;
  2927. }
  2928. static void inode_tree_add(struct inode *inode)
  2929. {
  2930. struct btrfs_root *root = BTRFS_I(inode)->root;
  2931. struct btrfs_inode *entry;
  2932. struct rb_node **p;
  2933. struct rb_node *parent;
  2934. again:
  2935. p = &root->inode_tree.rb_node;
  2936. parent = NULL;
  2937. if (hlist_unhashed(&inode->i_hash))
  2938. return;
  2939. spin_lock(&root->inode_lock);
  2940. while (*p) {
  2941. parent = *p;
  2942. entry = rb_entry(parent, struct btrfs_inode, rb_node);
  2943. if (inode->i_ino < entry->vfs_inode.i_ino)
  2944. p = &parent->rb_left;
  2945. else if (inode->i_ino > entry->vfs_inode.i_ino)
  2946. p = &parent->rb_right;
  2947. else {
  2948. WARN_ON(!(entry->vfs_inode.i_state &
  2949. (I_WILL_FREE | I_FREEING | I_CLEAR)));
  2950. rb_erase(parent, &root->inode_tree);
  2951. RB_CLEAR_NODE(parent);
  2952. spin_unlock(&root->inode_lock);
  2953. goto again;
  2954. }
  2955. }
  2956. rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
  2957. rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  2958. spin_unlock(&root->inode_lock);
  2959. }
  2960. static void inode_tree_del(struct inode *inode)
  2961. {
  2962. struct btrfs_root *root = BTRFS_I(inode)->root;
  2963. int empty = 0;
  2964. spin_lock(&root->inode_lock);
  2965. if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
  2966. rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
  2967. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  2968. empty = RB_EMPTY_ROOT(&root->inode_tree);
  2969. }
  2970. spin_unlock(&root->inode_lock);
  2971. if (empty && btrfs_root_refs(&root->root_item) == 0) {
  2972. synchronize_srcu(&root->fs_info->subvol_srcu);
  2973. spin_lock(&root->inode_lock);
  2974. empty = RB_EMPTY_ROOT(&root->inode_tree);
  2975. spin_unlock(&root->inode_lock);
  2976. if (empty)
  2977. btrfs_add_dead_root(root);
  2978. }
  2979. }
  2980. int btrfs_invalidate_inodes(struct btrfs_root *root)
  2981. {
  2982. struct rb_node *node;
  2983. struct rb_node *prev;
  2984. struct btrfs_inode *entry;
  2985. struct inode *inode;
  2986. u64 objectid = 0;
  2987. WARN_ON(btrfs_root_refs(&root->root_item) != 0);
  2988. spin_lock(&root->inode_lock);
  2989. again:
  2990. node = root->inode_tree.rb_node;
  2991. prev = NULL;
  2992. while (node) {
  2993. prev = node;
  2994. entry = rb_entry(node, struct btrfs_inode, rb_node);
  2995. if (objectid < entry->vfs_inode.i_ino)
  2996. node = node->rb_left;
  2997. else if (objectid > entry->vfs_inode.i_ino)
  2998. node = node->rb_right;
  2999. else
  3000. break;
  3001. }
  3002. if (!node) {
  3003. while (prev) {
  3004. entry = rb_entry(prev, struct btrfs_inode, rb_node);
  3005. if (objectid <= entry->vfs_inode.i_ino) {
  3006. node = prev;
  3007. break;
  3008. }
  3009. prev = rb_next(prev);
  3010. }
  3011. }
  3012. while (node) {
  3013. entry = rb_entry(node, struct btrfs_inode, rb_node);
  3014. objectid = entry->vfs_inode.i_ino + 1;
  3015. inode = igrab(&entry->vfs_inode);
  3016. if (inode) {
  3017. spin_unlock(&root->inode_lock);
  3018. if (atomic_read(&inode->i_count) > 1)
  3019. d_prune_aliases(inode);
  3020. /*
  3021. * btrfs_drop_inode will remove it from
  3022. * the inode cache when its usage count
  3023. * hits zero.
  3024. */
  3025. iput(inode);
  3026. cond_resched();
  3027. spin_lock(&root->inode_lock);
  3028. goto again;
  3029. }
  3030. if (cond_resched_lock(&root->inode_lock))
  3031. goto again;
  3032. node = rb_next(node);
  3033. }
  3034. spin_unlock(&root->inode_lock);
  3035. return 0;
  3036. }
  3037. static noinline void init_btrfs_i(struct inode *inode)
  3038. {
  3039. struct btrfs_inode *bi = BTRFS_I(inode);
  3040. bi->generation = 0;
  3041. bi->sequence = 0;
  3042. bi->last_trans = 0;
  3043. bi->logged_trans = 0;
  3044. bi->delalloc_bytes = 0;
  3045. bi->reserved_bytes = 0;
  3046. bi->disk_i_size = 0;
  3047. bi->flags = 0;
  3048. bi->index_cnt = (u64)-1;
  3049. bi->last_unlink_trans = 0;
  3050. bi->ordered_data_close = 0;
  3051. extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
  3052. extent_io_tree_init(&BTRFS_I(inode)->io_tree,
  3053. inode->i_mapping, GFP_NOFS);
  3054. extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
  3055. inode->i_mapping, GFP_NOFS);
  3056. INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
  3057. INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
  3058. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  3059. btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
  3060. mutex_init(&BTRFS_I(inode)->extent_mutex);
  3061. mutex_init(&BTRFS_I(inode)->log_mutex);
  3062. }
  3063. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  3064. {
  3065. struct btrfs_iget_args *args = p;
  3066. inode->i_ino = args->ino;
  3067. init_btrfs_i(inode);
  3068. BTRFS_I(inode)->root = args->root;
  3069. btrfs_set_inode_space_info(args->root, inode);
  3070. return 0;
  3071. }
  3072. static int btrfs_find_actor(struct inode *inode, void *opaque)
  3073. {
  3074. struct btrfs_iget_args *args = opaque;
  3075. return args->ino == inode->i_ino &&
  3076. args->root == BTRFS_I(inode)->root;
  3077. }
  3078. static struct inode *btrfs_iget_locked(struct super_block *s,
  3079. u64 objectid,
  3080. struct btrfs_root *root)
  3081. {
  3082. struct inode *inode;
  3083. struct btrfs_iget_args args;
  3084. args.ino = objectid;
  3085. args.root = root;
  3086. inode = iget5_locked(s, objectid, btrfs_find_actor,
  3087. btrfs_init_locked_inode,
  3088. (void *)&args);
  3089. return inode;
  3090. }
  3091. /* Get an inode object given its location and corresponding root.
  3092. * Returns in *is_new if the inode was read from disk
  3093. */
  3094. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  3095. struct btrfs_root *root)
  3096. {
  3097. struct inode *inode;
  3098. inode = btrfs_iget_locked(s, location->objectid, root);
  3099. if (!inode)
  3100. return ERR_PTR(-ENOMEM);
  3101. if (inode->i_state & I_NEW) {
  3102. BTRFS_I(inode)->root = root;
  3103. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  3104. btrfs_read_locked_inode(inode);
  3105. inode_tree_add(inode);
  3106. unlock_new_inode(inode);
  3107. }
  3108. return inode;
  3109. }
  3110. static struct inode *new_simple_dir(struct super_block *s,
  3111. struct btrfs_key *key,
  3112. struct btrfs_root *root)
  3113. {
  3114. struct inode *inode = new_inode(s);
  3115. if (!inode)
  3116. return ERR_PTR(-ENOMEM);
  3117. init_btrfs_i(inode);
  3118. BTRFS_I(inode)->root = root;
  3119. memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
  3120. BTRFS_I(inode)->dummy_inode = 1;
  3121. inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
  3122. inode->i_op = &simple_dir_inode_operations;
  3123. inode->i_fop = &simple_dir_operations;
  3124. inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
  3125. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3126. return inode;
  3127. }
  3128. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  3129. {
  3130. struct inode *inode;
  3131. struct btrfs_root *root = BTRFS_I(dir)->root;
  3132. struct btrfs_root *sub_root = root;
  3133. struct btrfs_key location;
  3134. int index;
  3135. int ret;
  3136. dentry->d_op = &btrfs_dentry_operations;
  3137. if (dentry->d_name.len > BTRFS_NAME_LEN)
  3138. return ERR_PTR(-ENAMETOOLONG);
  3139. ret = btrfs_inode_by_name(dir, dentry, &location);
  3140. if (ret < 0)
  3141. return ERR_PTR(ret);
  3142. if (location.objectid == 0)
  3143. return NULL;
  3144. if (location.type == BTRFS_INODE_ITEM_KEY) {
  3145. inode = btrfs_iget(dir->i_sb, &location, root);
  3146. return inode;
  3147. }
  3148. BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
  3149. index = srcu_read_lock(&root->fs_info->subvol_srcu);
  3150. ret = fixup_tree_root_location(root, dir, dentry,
  3151. &location, &sub_root);
  3152. if (ret < 0) {
  3153. if (ret != -ENOENT)
  3154. inode = ERR_PTR(ret);
  3155. else
  3156. inode = new_simple_dir(dir->i_sb, &location, sub_root);
  3157. } else {
  3158. inode = btrfs_iget(dir->i_sb, &location, sub_root);
  3159. }
  3160. srcu_read_unlock(&root->fs_info->subvol_srcu, index);
  3161. return inode;
  3162. }
  3163. static int btrfs_dentry_delete(struct dentry *dentry)
  3164. {
  3165. struct btrfs_root *root;
  3166. if (!dentry->d_inode)
  3167. return 0;
  3168. root = BTRFS_I(dentry->d_inode)->root;
  3169. if (btrfs_root_refs(&root->root_item) == 0)
  3170. return 1;
  3171. return 0;
  3172. }
  3173. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  3174. struct nameidata *nd)
  3175. {
  3176. struct inode *inode;
  3177. inode = btrfs_lookup_dentry(dir, dentry);
  3178. if (IS_ERR(inode))
  3179. return ERR_CAST(inode);
  3180. return d_splice_alias(inode, dentry);
  3181. }
  3182. static unsigned char btrfs_filetype_table[] = {
  3183. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  3184. };
  3185. static int btrfs_real_readdir(struct file *filp, void *dirent,
  3186. filldir_t filldir)
  3187. {
  3188. struct inode *inode = filp->f_dentry->d_inode;
  3189. struct btrfs_root *root = BTRFS_I(inode)->root;
  3190. struct btrfs_item *item;
  3191. struct btrfs_dir_item *di;
  3192. struct btrfs_key key;
  3193. struct btrfs_key found_key;
  3194. struct btrfs_path *path;
  3195. int ret;
  3196. u32 nritems;
  3197. struct extent_buffer *leaf;
  3198. int slot;
  3199. int advance;
  3200. unsigned char d_type;
  3201. int over = 0;
  3202. u32 di_cur;
  3203. u32 di_total;
  3204. u32 di_len;
  3205. int key_type = BTRFS_DIR_INDEX_KEY;
  3206. char tmp_name[32];
  3207. char *name_ptr;
  3208. int name_len;
  3209. /* FIXME, use a real flag for deciding about the key type */
  3210. if (root->fs_info->tree_root == root)
  3211. key_type = BTRFS_DIR_ITEM_KEY;
  3212. /* special case for "." */
  3213. if (filp->f_pos == 0) {
  3214. over = filldir(dirent, ".", 1,
  3215. 1, inode->i_ino,
  3216. DT_DIR);
  3217. if (over)
  3218. return 0;
  3219. filp->f_pos = 1;
  3220. }
  3221. /* special case for .., just use the back ref */
  3222. if (filp->f_pos == 1) {
  3223. u64 pino = parent_ino(filp->f_path.dentry);
  3224. over = filldir(dirent, "..", 2,
  3225. 2, pino, DT_DIR);
  3226. if (over)
  3227. return 0;
  3228. filp->f_pos = 2;
  3229. }
  3230. path = btrfs_alloc_path();
  3231. path->reada = 2;
  3232. btrfs_set_key_type(&key, key_type);
  3233. key.offset = filp->f_pos;
  3234. key.objectid = inode->i_ino;
  3235. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3236. if (ret < 0)
  3237. goto err;
  3238. advance = 0;
  3239. while (1) {
  3240. leaf = path->nodes[0];
  3241. nritems = btrfs_header_nritems(leaf);
  3242. slot = path->slots[0];
  3243. if (advance || slot >= nritems) {
  3244. if (slot >= nritems - 1) {
  3245. ret = btrfs_next_leaf(root, path);
  3246. if (ret)
  3247. break;
  3248. leaf = path->nodes[0];
  3249. nritems = btrfs_header_nritems(leaf);
  3250. slot = path->slots[0];
  3251. } else {
  3252. slot++;
  3253. path->slots[0]++;
  3254. }
  3255. }
  3256. advance = 1;
  3257. item = btrfs_item_nr(leaf, slot);
  3258. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3259. if (found_key.objectid != key.objectid)
  3260. break;
  3261. if (btrfs_key_type(&found_key) != key_type)
  3262. break;
  3263. if (found_key.offset < filp->f_pos)
  3264. continue;
  3265. filp->f_pos = found_key.offset;
  3266. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  3267. di_cur = 0;
  3268. di_total = btrfs_item_size(leaf, item);
  3269. while (di_cur < di_total) {
  3270. struct btrfs_key location;
  3271. name_len = btrfs_dir_name_len(leaf, di);
  3272. if (name_len <= sizeof(tmp_name)) {
  3273. name_ptr = tmp_name;
  3274. } else {
  3275. name_ptr = kmalloc(name_len, GFP_NOFS);
  3276. if (!name_ptr) {
  3277. ret = -ENOMEM;
  3278. goto err;
  3279. }
  3280. }
  3281. read_extent_buffer(leaf, name_ptr,
  3282. (unsigned long)(di + 1), name_len);
  3283. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  3284. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  3285. /* is this a reference to our own snapshot? If so
  3286. * skip it
  3287. */
  3288. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  3289. location.objectid == root->root_key.objectid) {
  3290. over = 0;
  3291. goto skip;
  3292. }
  3293. over = filldir(dirent, name_ptr, name_len,
  3294. found_key.offset, location.objectid,
  3295. d_type);
  3296. skip:
  3297. if (name_ptr != tmp_name)
  3298. kfree(name_ptr);
  3299. if (over)
  3300. goto nopos;
  3301. di_len = btrfs_dir_name_len(leaf, di) +
  3302. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  3303. di_cur += di_len;
  3304. di = (struct btrfs_dir_item *)((char *)di + di_len);
  3305. }
  3306. }
  3307. /* Reached end of directory/root. Bump pos past the last item. */
  3308. if (key_type == BTRFS_DIR_INDEX_KEY)
  3309. filp->f_pos = INT_LIMIT(off_t);
  3310. else
  3311. filp->f_pos++;
  3312. nopos:
  3313. ret = 0;
  3314. err:
  3315. btrfs_free_path(path);
  3316. return ret;
  3317. }
  3318. int btrfs_write_inode(struct inode *inode, int wait)
  3319. {
  3320. struct btrfs_root *root = BTRFS_I(inode)->root;
  3321. struct btrfs_trans_handle *trans;
  3322. int ret = 0;
  3323. if (root->fs_info->btree_inode == inode)
  3324. return 0;
  3325. if (wait) {
  3326. trans = btrfs_join_transaction(root, 1);
  3327. btrfs_set_trans_block_group(trans, inode);
  3328. ret = btrfs_commit_transaction(trans, root);
  3329. }
  3330. return ret;
  3331. }
  3332. /*
  3333. * This is somewhat expensive, updating the tree every time the
  3334. * inode changes. But, it is most likely to find the inode in cache.
  3335. * FIXME, needs more benchmarking...there are no reasons other than performance
  3336. * to keep or drop this code.
  3337. */
  3338. void btrfs_dirty_inode(struct inode *inode)
  3339. {
  3340. struct btrfs_root *root = BTRFS_I(inode)->root;
  3341. struct btrfs_trans_handle *trans;
  3342. trans = btrfs_join_transaction(root, 1);
  3343. btrfs_set_trans_block_group(trans, inode);
  3344. btrfs_update_inode(trans, root, inode);
  3345. btrfs_end_transaction(trans, root);
  3346. }
  3347. /*
  3348. * find the highest existing sequence number in a directory
  3349. * and then set the in-memory index_cnt variable to reflect
  3350. * free sequence numbers
  3351. */
  3352. static int btrfs_set_inode_index_count(struct inode *inode)
  3353. {
  3354. struct btrfs_root *root = BTRFS_I(inode)->root;
  3355. struct btrfs_key key, found_key;
  3356. struct btrfs_path *path;
  3357. struct extent_buffer *leaf;
  3358. int ret;
  3359. key.objectid = inode->i_ino;
  3360. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  3361. key.offset = (u64)-1;
  3362. path = btrfs_alloc_path();
  3363. if (!path)
  3364. return -ENOMEM;
  3365. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3366. if (ret < 0)
  3367. goto out;
  3368. /* FIXME: we should be able to handle this */
  3369. if (ret == 0)
  3370. goto out;
  3371. ret = 0;
  3372. /*
  3373. * MAGIC NUMBER EXPLANATION:
  3374. * since we search a directory based on f_pos we have to start at 2
  3375. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  3376. * else has to start at 2
  3377. */
  3378. if (path->slots[0] == 0) {
  3379. BTRFS_I(inode)->index_cnt = 2;
  3380. goto out;
  3381. }
  3382. path->slots[0]--;
  3383. leaf = path->nodes[0];
  3384. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3385. if (found_key.objectid != inode->i_ino ||
  3386. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  3387. BTRFS_I(inode)->index_cnt = 2;
  3388. goto out;
  3389. }
  3390. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  3391. out:
  3392. btrfs_free_path(path);
  3393. return ret;
  3394. }
  3395. /*
  3396. * helper to find a free sequence number in a given directory. This current
  3397. * code is very simple, later versions will do smarter things in the btree
  3398. */
  3399. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  3400. {
  3401. int ret = 0;
  3402. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  3403. ret = btrfs_set_inode_index_count(dir);
  3404. if (ret)
  3405. return ret;
  3406. }
  3407. *index = BTRFS_I(dir)->index_cnt;
  3408. BTRFS_I(dir)->index_cnt++;
  3409. return ret;
  3410. }
  3411. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  3412. struct btrfs_root *root,
  3413. struct inode *dir,
  3414. const char *name, int name_len,
  3415. u64 ref_objectid, u64 objectid,
  3416. u64 alloc_hint, int mode, u64 *index)
  3417. {
  3418. struct inode *inode;
  3419. struct btrfs_inode_item *inode_item;
  3420. struct btrfs_key *location;
  3421. struct btrfs_path *path;
  3422. struct btrfs_inode_ref *ref;
  3423. struct btrfs_key key[2];
  3424. u32 sizes[2];
  3425. unsigned long ptr;
  3426. int ret;
  3427. int owner;
  3428. path = btrfs_alloc_path();
  3429. BUG_ON(!path);
  3430. inode = new_inode(root->fs_info->sb);
  3431. if (!inode)
  3432. return ERR_PTR(-ENOMEM);
  3433. if (dir) {
  3434. ret = btrfs_set_inode_index(dir, index);
  3435. if (ret) {
  3436. iput(inode);
  3437. return ERR_PTR(ret);
  3438. }
  3439. }
  3440. /*
  3441. * index_cnt is ignored for everything but a dir,
  3442. * btrfs_get_inode_index_count has an explanation for the magic
  3443. * number
  3444. */
  3445. init_btrfs_i(inode);
  3446. BTRFS_I(inode)->index_cnt = 2;
  3447. BTRFS_I(inode)->root = root;
  3448. BTRFS_I(inode)->generation = trans->transid;
  3449. btrfs_set_inode_space_info(root, inode);
  3450. if (mode & S_IFDIR)
  3451. owner = 0;
  3452. else
  3453. owner = 1;
  3454. BTRFS_I(inode)->block_group =
  3455. btrfs_find_block_group(root, 0, alloc_hint, owner);
  3456. key[0].objectid = objectid;
  3457. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  3458. key[0].offset = 0;
  3459. key[1].objectid = objectid;
  3460. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  3461. key[1].offset = ref_objectid;
  3462. sizes[0] = sizeof(struct btrfs_inode_item);
  3463. sizes[1] = name_len + sizeof(*ref);
  3464. path->leave_spinning = 1;
  3465. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  3466. if (ret != 0)
  3467. goto fail;
  3468. inode->i_uid = current_fsuid();
  3469. if (dir && (dir->i_mode & S_ISGID)) {
  3470. inode->i_gid = dir->i_gid;
  3471. if (S_ISDIR(mode))
  3472. mode |= S_ISGID;
  3473. } else
  3474. inode->i_gid = current_fsgid();
  3475. inode->i_mode = mode;
  3476. inode->i_ino = objectid;
  3477. inode_set_bytes(inode, 0);
  3478. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3479. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3480. struct btrfs_inode_item);
  3481. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  3482. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  3483. struct btrfs_inode_ref);
  3484. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  3485. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  3486. ptr = (unsigned long)(ref + 1);
  3487. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  3488. btrfs_mark_buffer_dirty(path->nodes[0]);
  3489. btrfs_free_path(path);
  3490. location = &BTRFS_I(inode)->location;
  3491. location->objectid = objectid;
  3492. location->offset = 0;
  3493. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  3494. btrfs_inherit_iflags(inode, dir);
  3495. if ((mode & S_IFREG)) {
  3496. if (btrfs_test_opt(root, NODATASUM))
  3497. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
  3498. if (btrfs_test_opt(root, NODATACOW))
  3499. BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
  3500. }
  3501. insert_inode_hash(inode);
  3502. inode_tree_add(inode);
  3503. return inode;
  3504. fail:
  3505. if (dir)
  3506. BTRFS_I(dir)->index_cnt--;
  3507. btrfs_free_path(path);
  3508. iput(inode);
  3509. return ERR_PTR(ret);
  3510. }
  3511. static inline u8 btrfs_inode_type(struct inode *inode)
  3512. {
  3513. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  3514. }
  3515. /*
  3516. * utility function to add 'inode' into 'parent_inode' with
  3517. * a give name and a given sequence number.
  3518. * if 'add_backref' is true, also insert a backref from the
  3519. * inode to the parent directory.
  3520. */
  3521. int btrfs_add_link(struct btrfs_trans_handle *trans,
  3522. struct inode *parent_inode, struct inode *inode,
  3523. const char *name, int name_len, int add_backref, u64 index)
  3524. {
  3525. int ret = 0;
  3526. struct btrfs_key key;
  3527. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  3528. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  3529. memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
  3530. } else {
  3531. key.objectid = inode->i_ino;
  3532. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  3533. key.offset = 0;
  3534. }
  3535. if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  3536. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  3537. key.objectid, root->root_key.objectid,
  3538. parent_inode->i_ino,
  3539. index, name, name_len);
  3540. } else if (add_backref) {
  3541. ret = btrfs_insert_inode_ref(trans, root,
  3542. name, name_len, inode->i_ino,
  3543. parent_inode->i_ino, index);
  3544. }
  3545. if (ret == 0) {
  3546. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  3547. parent_inode->i_ino, &key,
  3548. btrfs_inode_type(inode), index);
  3549. BUG_ON(ret);
  3550. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  3551. name_len * 2);
  3552. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  3553. ret = btrfs_update_inode(trans, root, parent_inode);
  3554. }
  3555. return ret;
  3556. }
  3557. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  3558. struct dentry *dentry, struct inode *inode,
  3559. int backref, u64 index)
  3560. {
  3561. int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  3562. inode, dentry->d_name.name,
  3563. dentry->d_name.len, backref, index);
  3564. if (!err) {
  3565. d_instantiate(dentry, inode);
  3566. return 0;
  3567. }
  3568. if (err > 0)
  3569. err = -EEXIST;
  3570. return err;
  3571. }
  3572. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  3573. int mode, dev_t rdev)
  3574. {
  3575. struct btrfs_trans_handle *trans;
  3576. struct btrfs_root *root = BTRFS_I(dir)->root;
  3577. struct inode *inode = NULL;
  3578. int err;
  3579. int drop_inode = 0;
  3580. u64 objectid;
  3581. unsigned long nr = 0;
  3582. u64 index = 0;
  3583. if (!new_valid_dev(rdev))
  3584. return -EINVAL;
  3585. /*
  3586. * 2 for inode item and ref
  3587. * 2 for dir items
  3588. * 1 for xattr if selinux is on
  3589. */
  3590. err = btrfs_reserve_metadata_space(root, 5);
  3591. if (err)
  3592. return err;
  3593. trans = btrfs_start_transaction(root, 1);
  3594. if (!trans)
  3595. goto fail;
  3596. btrfs_set_trans_block_group(trans, dir);
  3597. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3598. if (err) {
  3599. err = -ENOSPC;
  3600. goto out_unlock;
  3601. }
  3602. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3603. dentry->d_name.len,
  3604. dentry->d_parent->d_inode->i_ino, objectid,
  3605. BTRFS_I(dir)->block_group, mode, &index);
  3606. err = PTR_ERR(inode);
  3607. if (IS_ERR(inode))
  3608. goto out_unlock;
  3609. err = btrfs_init_inode_security(inode, dir);
  3610. if (err) {
  3611. drop_inode = 1;
  3612. goto out_unlock;
  3613. }
  3614. btrfs_set_trans_block_group(trans, inode);
  3615. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  3616. if (err)
  3617. drop_inode = 1;
  3618. else {
  3619. inode->i_op = &btrfs_special_inode_operations;
  3620. init_special_inode(inode, inode->i_mode, rdev);
  3621. btrfs_update_inode(trans, root, inode);
  3622. }
  3623. btrfs_update_inode_block_group(trans, inode);
  3624. btrfs_update_inode_block_group(trans, dir);
  3625. out_unlock:
  3626. nr = trans->blocks_used;
  3627. btrfs_end_transaction_throttle(trans, root);
  3628. fail:
  3629. btrfs_unreserve_metadata_space(root, 5);
  3630. if (drop_inode) {
  3631. inode_dec_link_count(inode);
  3632. iput(inode);
  3633. }
  3634. btrfs_btree_balance_dirty(root, nr);
  3635. return err;
  3636. }
  3637. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  3638. int mode, struct nameidata *nd)
  3639. {
  3640. struct btrfs_trans_handle *trans;
  3641. struct btrfs_root *root = BTRFS_I(dir)->root;
  3642. struct inode *inode = NULL;
  3643. int err;
  3644. int drop_inode = 0;
  3645. unsigned long nr = 0;
  3646. u64 objectid;
  3647. u64 index = 0;
  3648. /*
  3649. * 2 for inode item and ref
  3650. * 2 for dir items
  3651. * 1 for xattr if selinux is on
  3652. */
  3653. err = btrfs_reserve_metadata_space(root, 5);
  3654. if (err)
  3655. return err;
  3656. trans = btrfs_start_transaction(root, 1);
  3657. if (!trans)
  3658. goto fail;
  3659. btrfs_set_trans_block_group(trans, dir);
  3660. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3661. if (err) {
  3662. err = -ENOSPC;
  3663. goto out_unlock;
  3664. }
  3665. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3666. dentry->d_name.len,
  3667. dentry->d_parent->d_inode->i_ino,
  3668. objectid, BTRFS_I(dir)->block_group, mode,
  3669. &index);
  3670. err = PTR_ERR(inode);
  3671. if (IS_ERR(inode))
  3672. goto out_unlock;
  3673. err = btrfs_init_inode_security(inode, dir);
  3674. if (err) {
  3675. drop_inode = 1;
  3676. goto out_unlock;
  3677. }
  3678. btrfs_set_trans_block_group(trans, inode);
  3679. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  3680. if (err)
  3681. drop_inode = 1;
  3682. else {
  3683. inode->i_mapping->a_ops = &btrfs_aops;
  3684. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  3685. inode->i_fop = &btrfs_file_operations;
  3686. inode->i_op = &btrfs_file_inode_operations;
  3687. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  3688. }
  3689. btrfs_update_inode_block_group(trans, inode);
  3690. btrfs_update_inode_block_group(trans, dir);
  3691. out_unlock:
  3692. nr = trans->blocks_used;
  3693. btrfs_end_transaction_throttle(trans, root);
  3694. fail:
  3695. btrfs_unreserve_metadata_space(root, 5);
  3696. if (drop_inode) {
  3697. inode_dec_link_count(inode);
  3698. iput(inode);
  3699. }
  3700. btrfs_btree_balance_dirty(root, nr);
  3701. return err;
  3702. }
  3703. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  3704. struct dentry *dentry)
  3705. {
  3706. struct btrfs_trans_handle *trans;
  3707. struct btrfs_root *root = BTRFS_I(dir)->root;
  3708. struct inode *inode = old_dentry->d_inode;
  3709. u64 index;
  3710. unsigned long nr = 0;
  3711. int err;
  3712. int drop_inode = 0;
  3713. if (inode->i_nlink == 0)
  3714. return -ENOENT;
  3715. /*
  3716. * 1 item for inode ref
  3717. * 2 items for dir items
  3718. */
  3719. err = btrfs_reserve_metadata_space(root, 3);
  3720. if (err)
  3721. return err;
  3722. btrfs_inc_nlink(inode);
  3723. err = btrfs_set_inode_index(dir, &index);
  3724. if (err)
  3725. goto fail;
  3726. trans = btrfs_start_transaction(root, 1);
  3727. btrfs_set_trans_block_group(trans, dir);
  3728. atomic_inc(&inode->i_count);
  3729. err = btrfs_add_nondir(trans, dentry, inode, 1, index);
  3730. if (err) {
  3731. drop_inode = 1;
  3732. } else {
  3733. btrfs_update_inode_block_group(trans, dir);
  3734. err = btrfs_update_inode(trans, root, inode);
  3735. BUG_ON(err);
  3736. btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
  3737. }
  3738. nr = trans->blocks_used;
  3739. btrfs_end_transaction_throttle(trans, root);
  3740. fail:
  3741. btrfs_unreserve_metadata_space(root, 3);
  3742. if (drop_inode) {
  3743. inode_dec_link_count(inode);
  3744. iput(inode);
  3745. }
  3746. btrfs_btree_balance_dirty(root, nr);
  3747. return err;
  3748. }
  3749. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  3750. {
  3751. struct inode *inode = NULL;
  3752. struct btrfs_trans_handle *trans;
  3753. struct btrfs_root *root = BTRFS_I(dir)->root;
  3754. int err = 0;
  3755. int drop_on_err = 0;
  3756. u64 objectid = 0;
  3757. u64 index = 0;
  3758. unsigned long nr = 1;
  3759. /*
  3760. * 2 items for inode and ref
  3761. * 2 items for dir items
  3762. * 1 for xattr if selinux is on
  3763. */
  3764. err = btrfs_reserve_metadata_space(root, 5);
  3765. if (err)
  3766. return err;
  3767. trans = btrfs_start_transaction(root, 1);
  3768. if (!trans) {
  3769. err = -ENOMEM;
  3770. goto out_unlock;
  3771. }
  3772. btrfs_set_trans_block_group(trans, dir);
  3773. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3774. if (err) {
  3775. err = -ENOSPC;
  3776. goto out_unlock;
  3777. }
  3778. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3779. dentry->d_name.len,
  3780. dentry->d_parent->d_inode->i_ino, objectid,
  3781. BTRFS_I(dir)->block_group, S_IFDIR | mode,
  3782. &index);
  3783. if (IS_ERR(inode)) {
  3784. err = PTR_ERR(inode);
  3785. goto out_fail;
  3786. }
  3787. drop_on_err = 1;
  3788. err = btrfs_init_inode_security(inode, dir);
  3789. if (err)
  3790. goto out_fail;
  3791. inode->i_op = &btrfs_dir_inode_operations;
  3792. inode->i_fop = &btrfs_dir_file_operations;
  3793. btrfs_set_trans_block_group(trans, inode);
  3794. btrfs_i_size_write(inode, 0);
  3795. err = btrfs_update_inode(trans, root, inode);
  3796. if (err)
  3797. goto out_fail;
  3798. err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  3799. inode, dentry->d_name.name,
  3800. dentry->d_name.len, 0, index);
  3801. if (err)
  3802. goto out_fail;
  3803. d_instantiate(dentry, inode);
  3804. drop_on_err = 0;
  3805. btrfs_update_inode_block_group(trans, inode);
  3806. btrfs_update_inode_block_group(trans, dir);
  3807. out_fail:
  3808. nr = trans->blocks_used;
  3809. btrfs_end_transaction_throttle(trans, root);
  3810. out_unlock:
  3811. btrfs_unreserve_metadata_space(root, 5);
  3812. if (drop_on_err)
  3813. iput(inode);
  3814. btrfs_btree_balance_dirty(root, nr);
  3815. return err;
  3816. }
  3817. /* helper for btfs_get_extent. Given an existing extent in the tree,
  3818. * and an extent that you want to insert, deal with overlap and insert
  3819. * the new extent into the tree.
  3820. */
  3821. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  3822. struct extent_map *existing,
  3823. struct extent_map *em,
  3824. u64 map_start, u64 map_len)
  3825. {
  3826. u64 start_diff;
  3827. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  3828. start_diff = map_start - em->start;
  3829. em->start = map_start;
  3830. em->len = map_len;
  3831. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  3832. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  3833. em->block_start += start_diff;
  3834. em->block_len -= start_diff;
  3835. }
  3836. return add_extent_mapping(em_tree, em);
  3837. }
  3838. static noinline int uncompress_inline(struct btrfs_path *path,
  3839. struct inode *inode, struct page *page,
  3840. size_t pg_offset, u64 extent_offset,
  3841. struct btrfs_file_extent_item *item)
  3842. {
  3843. int ret;
  3844. struct extent_buffer *leaf = path->nodes[0];
  3845. char *tmp;
  3846. size_t max_size;
  3847. unsigned long inline_size;
  3848. unsigned long ptr;
  3849. WARN_ON(pg_offset != 0);
  3850. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  3851. inline_size = btrfs_file_extent_inline_item_len(leaf,
  3852. btrfs_item_nr(leaf, path->slots[0]));
  3853. tmp = kmalloc(inline_size, GFP_NOFS);
  3854. ptr = btrfs_file_extent_inline_start(item);
  3855. read_extent_buffer(leaf, tmp, ptr, inline_size);
  3856. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  3857. ret = btrfs_zlib_decompress(tmp, page, extent_offset,
  3858. inline_size, max_size);
  3859. if (ret) {
  3860. char *kaddr = kmap_atomic(page, KM_USER0);
  3861. unsigned long copy_size = min_t(u64,
  3862. PAGE_CACHE_SIZE - pg_offset,
  3863. max_size - extent_offset);
  3864. memset(kaddr + pg_offset, 0, copy_size);
  3865. kunmap_atomic(kaddr, KM_USER0);
  3866. }
  3867. kfree(tmp);
  3868. return 0;
  3869. }
  3870. /*
  3871. * a bit scary, this does extent mapping from logical file offset to the disk.
  3872. * the ugly parts come from merging extents from the disk with the in-ram
  3873. * representation. This gets more complex because of the data=ordered code,
  3874. * where the in-ram extents might be locked pending data=ordered completion.
  3875. *
  3876. * This also copies inline extents directly into the page.
  3877. */
  3878. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  3879. size_t pg_offset, u64 start, u64 len,
  3880. int create)
  3881. {
  3882. int ret;
  3883. int err = 0;
  3884. u64 bytenr;
  3885. u64 extent_start = 0;
  3886. u64 extent_end = 0;
  3887. u64 objectid = inode->i_ino;
  3888. u32 found_type;
  3889. struct btrfs_path *path = NULL;
  3890. struct btrfs_root *root = BTRFS_I(inode)->root;
  3891. struct btrfs_file_extent_item *item;
  3892. struct extent_buffer *leaf;
  3893. struct btrfs_key found_key;
  3894. struct extent_map *em = NULL;
  3895. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  3896. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3897. struct btrfs_trans_handle *trans = NULL;
  3898. int compressed;
  3899. again:
  3900. read_lock(&em_tree->lock);
  3901. em = lookup_extent_mapping(em_tree, start, len);
  3902. if (em)
  3903. em->bdev = root->fs_info->fs_devices->latest_bdev;
  3904. read_unlock(&em_tree->lock);
  3905. if (em) {
  3906. if (em->start > start || em->start + em->len <= start)
  3907. free_extent_map(em);
  3908. else if (em->block_start == EXTENT_MAP_INLINE && page)
  3909. free_extent_map(em);
  3910. else
  3911. goto out;
  3912. }
  3913. em = alloc_extent_map(GFP_NOFS);
  3914. if (!em) {
  3915. err = -ENOMEM;
  3916. goto out;
  3917. }
  3918. em->bdev = root->fs_info->fs_devices->latest_bdev;
  3919. em->start = EXTENT_MAP_HOLE;
  3920. em->orig_start = EXTENT_MAP_HOLE;
  3921. em->len = (u64)-1;
  3922. em->block_len = (u64)-1;
  3923. if (!path) {
  3924. path = btrfs_alloc_path();
  3925. BUG_ON(!path);
  3926. }
  3927. ret = btrfs_lookup_file_extent(trans, root, path,
  3928. objectid, start, trans != NULL);
  3929. if (ret < 0) {
  3930. err = ret;
  3931. goto out;
  3932. }
  3933. if (ret != 0) {
  3934. if (path->slots[0] == 0)
  3935. goto not_found;
  3936. path->slots[0]--;
  3937. }
  3938. leaf = path->nodes[0];
  3939. item = btrfs_item_ptr(leaf, path->slots[0],
  3940. struct btrfs_file_extent_item);
  3941. /* are we inside the extent that was found? */
  3942. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3943. found_type = btrfs_key_type(&found_key);
  3944. if (found_key.objectid != objectid ||
  3945. found_type != BTRFS_EXTENT_DATA_KEY) {
  3946. goto not_found;
  3947. }
  3948. found_type = btrfs_file_extent_type(leaf, item);
  3949. extent_start = found_key.offset;
  3950. compressed = btrfs_file_extent_compression(leaf, item);
  3951. if (found_type == BTRFS_FILE_EXTENT_REG ||
  3952. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  3953. extent_end = extent_start +
  3954. btrfs_file_extent_num_bytes(leaf, item);
  3955. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  3956. size_t size;
  3957. size = btrfs_file_extent_inline_len(leaf, item);
  3958. extent_end = (extent_start + size + root->sectorsize - 1) &
  3959. ~((u64)root->sectorsize - 1);
  3960. }
  3961. if (start >= extent_end) {
  3962. path->slots[0]++;
  3963. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  3964. ret = btrfs_next_leaf(root, path);
  3965. if (ret < 0) {
  3966. err = ret;
  3967. goto out;
  3968. }
  3969. if (ret > 0)
  3970. goto not_found;
  3971. leaf = path->nodes[0];
  3972. }
  3973. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3974. if (found_key.objectid != objectid ||
  3975. found_key.type != BTRFS_EXTENT_DATA_KEY)
  3976. goto not_found;
  3977. if (start + len <= found_key.offset)
  3978. goto not_found;
  3979. em->start = start;
  3980. em->len = found_key.offset - start;
  3981. goto not_found_em;
  3982. }
  3983. if (found_type == BTRFS_FILE_EXTENT_REG ||
  3984. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  3985. em->start = extent_start;
  3986. em->len = extent_end - extent_start;
  3987. em->orig_start = extent_start -
  3988. btrfs_file_extent_offset(leaf, item);
  3989. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  3990. if (bytenr == 0) {
  3991. em->block_start = EXTENT_MAP_HOLE;
  3992. goto insert;
  3993. }
  3994. if (compressed) {
  3995. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  3996. em->block_start = bytenr;
  3997. em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
  3998. item);
  3999. } else {
  4000. bytenr += btrfs_file_extent_offset(leaf, item);
  4001. em->block_start = bytenr;
  4002. em->block_len = em->len;
  4003. if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
  4004. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  4005. }
  4006. goto insert;
  4007. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  4008. unsigned long ptr;
  4009. char *map;
  4010. size_t size;
  4011. size_t extent_offset;
  4012. size_t copy_size;
  4013. em->block_start = EXTENT_MAP_INLINE;
  4014. if (!page || create) {
  4015. em->start = extent_start;
  4016. em->len = extent_end - extent_start;
  4017. goto out;
  4018. }
  4019. size = btrfs_file_extent_inline_len(leaf, item);
  4020. extent_offset = page_offset(page) + pg_offset - extent_start;
  4021. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  4022. size - extent_offset);
  4023. em->start = extent_start + extent_offset;
  4024. em->len = (copy_size + root->sectorsize - 1) &
  4025. ~((u64)root->sectorsize - 1);
  4026. em->orig_start = EXTENT_MAP_INLINE;
  4027. if (compressed)
  4028. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  4029. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  4030. if (create == 0 && !PageUptodate(page)) {
  4031. if (btrfs_file_extent_compression(leaf, item) ==
  4032. BTRFS_COMPRESS_ZLIB) {
  4033. ret = uncompress_inline(path, inode, page,
  4034. pg_offset,
  4035. extent_offset, item);
  4036. BUG_ON(ret);
  4037. } else {
  4038. map = kmap(page);
  4039. read_extent_buffer(leaf, map + pg_offset, ptr,
  4040. copy_size);
  4041. if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
  4042. memset(map + pg_offset + copy_size, 0,
  4043. PAGE_CACHE_SIZE - pg_offset -
  4044. copy_size);
  4045. }
  4046. kunmap(page);
  4047. }
  4048. flush_dcache_page(page);
  4049. } else if (create && PageUptodate(page)) {
  4050. if (!trans) {
  4051. kunmap(page);
  4052. free_extent_map(em);
  4053. em = NULL;
  4054. btrfs_release_path(root, path);
  4055. trans = btrfs_join_transaction(root, 1);
  4056. goto again;
  4057. }
  4058. map = kmap(page);
  4059. write_extent_buffer(leaf, map + pg_offset, ptr,
  4060. copy_size);
  4061. kunmap(page);
  4062. btrfs_mark_buffer_dirty(leaf);
  4063. }
  4064. set_extent_uptodate(io_tree, em->start,
  4065. extent_map_end(em) - 1, GFP_NOFS);
  4066. goto insert;
  4067. } else {
  4068. printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
  4069. WARN_ON(1);
  4070. }
  4071. not_found:
  4072. em->start = start;
  4073. em->len = len;
  4074. not_found_em:
  4075. em->block_start = EXTENT_MAP_HOLE;
  4076. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  4077. insert:
  4078. btrfs_release_path(root, path);
  4079. if (em->start > start || extent_map_end(em) <= start) {
  4080. printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
  4081. "[%llu %llu]\n", (unsigned long long)em->start,
  4082. (unsigned long long)em->len,
  4083. (unsigned long long)start,
  4084. (unsigned long long)len);
  4085. err = -EIO;
  4086. goto out;
  4087. }
  4088. err = 0;
  4089. write_lock(&em_tree->lock);
  4090. ret = add_extent_mapping(em_tree, em);
  4091. /* it is possible that someone inserted the extent into the tree
  4092. * while we had the lock dropped. It is also possible that
  4093. * an overlapping map exists in the tree
  4094. */
  4095. if (ret == -EEXIST) {
  4096. struct extent_map *existing;
  4097. ret = 0;
  4098. existing = lookup_extent_mapping(em_tree, start, len);
  4099. if (existing && (existing->start > start ||
  4100. existing->start + existing->len <= start)) {
  4101. free_extent_map(existing);
  4102. existing = NULL;
  4103. }
  4104. if (!existing) {
  4105. existing = lookup_extent_mapping(em_tree, em->start,
  4106. em->len);
  4107. if (existing) {
  4108. err = merge_extent_mapping(em_tree, existing,
  4109. em, start,
  4110. root->sectorsize);
  4111. free_extent_map(existing);
  4112. if (err) {
  4113. free_extent_map(em);
  4114. em = NULL;
  4115. }
  4116. } else {
  4117. err = -EIO;
  4118. free_extent_map(em);
  4119. em = NULL;
  4120. }
  4121. } else {
  4122. free_extent_map(em);
  4123. em = existing;
  4124. err = 0;
  4125. }
  4126. }
  4127. write_unlock(&em_tree->lock);
  4128. out:
  4129. if (path)
  4130. btrfs_free_path(path);
  4131. if (trans) {
  4132. ret = btrfs_end_transaction(trans, root);
  4133. if (!err)
  4134. err = ret;
  4135. }
  4136. if (err) {
  4137. free_extent_map(em);
  4138. return ERR_PTR(err);
  4139. }
  4140. return em;
  4141. }
  4142. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  4143. const struct iovec *iov, loff_t offset,
  4144. unsigned long nr_segs)
  4145. {
  4146. return -EINVAL;
  4147. }
  4148. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  4149. __u64 start, __u64 len)
  4150. {
  4151. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
  4152. }
  4153. int btrfs_readpage(struct file *file, struct page *page)
  4154. {
  4155. struct extent_io_tree *tree;
  4156. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4157. return extent_read_full_page(tree, page, btrfs_get_extent);
  4158. }
  4159. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  4160. {
  4161. struct extent_io_tree *tree;
  4162. if (current->flags & PF_MEMALLOC) {
  4163. redirty_page_for_writepage(wbc, page);
  4164. unlock_page(page);
  4165. return 0;
  4166. }
  4167. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4168. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  4169. }
  4170. int btrfs_writepages(struct address_space *mapping,
  4171. struct writeback_control *wbc)
  4172. {
  4173. struct extent_io_tree *tree;
  4174. tree = &BTRFS_I(mapping->host)->io_tree;
  4175. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  4176. }
  4177. static int
  4178. btrfs_readpages(struct file *file, struct address_space *mapping,
  4179. struct list_head *pages, unsigned nr_pages)
  4180. {
  4181. struct extent_io_tree *tree;
  4182. tree = &BTRFS_I(mapping->host)->io_tree;
  4183. return extent_readpages(tree, mapping, pages, nr_pages,
  4184. btrfs_get_extent);
  4185. }
  4186. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  4187. {
  4188. struct extent_io_tree *tree;
  4189. struct extent_map_tree *map;
  4190. int ret;
  4191. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4192. map = &BTRFS_I(page->mapping->host)->extent_tree;
  4193. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  4194. if (ret == 1) {
  4195. ClearPagePrivate(page);
  4196. set_page_private(page, 0);
  4197. page_cache_release(page);
  4198. }
  4199. return ret;
  4200. }
  4201. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  4202. {
  4203. if (PageWriteback(page) || PageDirty(page))
  4204. return 0;
  4205. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  4206. }
  4207. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  4208. {
  4209. struct extent_io_tree *tree;
  4210. struct btrfs_ordered_extent *ordered;
  4211. u64 page_start = page_offset(page);
  4212. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  4213. /*
  4214. * we have the page locked, so new writeback can't start,
  4215. * and the dirty bit won't be cleared while we are here.
  4216. *
  4217. * Wait for IO on this page so that we can safely clear
  4218. * the PagePrivate2 bit and do ordered accounting
  4219. */
  4220. wait_on_page_writeback(page);
  4221. tree = &BTRFS_I(page->mapping->host)->io_tree;
  4222. if (offset) {
  4223. btrfs_releasepage(page, GFP_NOFS);
  4224. return;
  4225. }
  4226. lock_extent(tree, page_start, page_end, GFP_NOFS);
  4227. ordered = btrfs_lookup_ordered_extent(page->mapping->host,
  4228. page_offset(page));
  4229. if (ordered) {
  4230. /*
  4231. * IO on this page will never be started, so we need
  4232. * to account for any ordered extents now
  4233. */
  4234. clear_extent_bit(tree, page_start, page_end,
  4235. EXTENT_DIRTY | EXTENT_DELALLOC |
  4236. EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
  4237. /*
  4238. * whoever cleared the private bit is responsible
  4239. * for the finish_ordered_io
  4240. */
  4241. if (TestClearPagePrivate2(page)) {
  4242. btrfs_finish_ordered_io(page->mapping->host,
  4243. page_start, page_end);
  4244. }
  4245. btrfs_put_ordered_extent(ordered);
  4246. lock_extent(tree, page_start, page_end, GFP_NOFS);
  4247. }
  4248. clear_extent_bit(tree, page_start, page_end,
  4249. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
  4250. 1, 1, NULL, GFP_NOFS);
  4251. __btrfs_releasepage(page, GFP_NOFS);
  4252. ClearPageChecked(page);
  4253. if (PagePrivate(page)) {
  4254. ClearPagePrivate(page);
  4255. set_page_private(page, 0);
  4256. page_cache_release(page);
  4257. }
  4258. }
  4259. /*
  4260. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  4261. * called from a page fault handler when a page is first dirtied. Hence we must
  4262. * be careful to check for EOF conditions here. We set the page up correctly
  4263. * for a written page which means we get ENOSPC checking when writing into
  4264. * holes and correct delalloc and unwritten extent mapping on filesystems that
  4265. * support these features.
  4266. *
  4267. * We are not allowed to take the i_mutex here so we have to play games to
  4268. * protect against truncate races as the page could now be beyond EOF. Because
  4269. * vmtruncate() writes the inode size before removing pages, once we have the
  4270. * page lock we can determine safely if the page is beyond EOF. If it is not
  4271. * beyond EOF, then the page is guaranteed safe against truncation until we
  4272. * unlock the page.
  4273. */
  4274. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  4275. {
  4276. struct page *page = vmf->page;
  4277. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  4278. struct btrfs_root *root = BTRFS_I(inode)->root;
  4279. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  4280. struct btrfs_ordered_extent *ordered;
  4281. char *kaddr;
  4282. unsigned long zero_start;
  4283. loff_t size;
  4284. int ret;
  4285. u64 page_start;
  4286. u64 page_end;
  4287. ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
  4288. if (ret) {
  4289. if (ret == -ENOMEM)
  4290. ret = VM_FAULT_OOM;
  4291. else /* -ENOSPC, -EIO, etc */
  4292. ret = VM_FAULT_SIGBUS;
  4293. goto out;
  4294. }
  4295. ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
  4296. if (ret) {
  4297. btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
  4298. ret = VM_FAULT_SIGBUS;
  4299. goto out;
  4300. }
  4301. ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
  4302. again:
  4303. lock_page(page);
  4304. size = i_size_read(inode);
  4305. page_start = page_offset(page);
  4306. page_end = page_start + PAGE_CACHE_SIZE - 1;
  4307. if ((page->mapping != inode->i_mapping) ||
  4308. (page_start >= size)) {
  4309. btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
  4310. /* page got truncated out from underneath us */
  4311. goto out_unlock;
  4312. }
  4313. wait_on_page_writeback(page);
  4314. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  4315. set_page_extent_mapped(page);
  4316. /*
  4317. * we can't set the delalloc bits if there are pending ordered
  4318. * extents. Drop our locks and wait for them to finish
  4319. */
  4320. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  4321. if (ordered) {
  4322. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  4323. unlock_page(page);
  4324. btrfs_start_ordered_extent(inode, ordered, 1);
  4325. btrfs_put_ordered_extent(ordered);
  4326. goto again;
  4327. }
  4328. ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
  4329. if (ret) {
  4330. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  4331. ret = VM_FAULT_SIGBUS;
  4332. goto out_unlock;
  4333. }
  4334. ret = 0;
  4335. /* page is wholly or partially inside EOF */
  4336. if (page_start + PAGE_CACHE_SIZE > size)
  4337. zero_start = size & ~PAGE_CACHE_MASK;
  4338. else
  4339. zero_start = PAGE_CACHE_SIZE;
  4340. if (zero_start != PAGE_CACHE_SIZE) {
  4341. kaddr = kmap(page);
  4342. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  4343. flush_dcache_page(page);
  4344. kunmap(page);
  4345. }
  4346. ClearPageChecked(page);
  4347. set_page_dirty(page);
  4348. SetPageUptodate(page);
  4349. BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
  4350. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  4351. out_unlock:
  4352. btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
  4353. if (!ret)
  4354. return VM_FAULT_LOCKED;
  4355. unlock_page(page);
  4356. out:
  4357. return ret;
  4358. }
  4359. static void btrfs_truncate(struct inode *inode)
  4360. {
  4361. struct btrfs_root *root = BTRFS_I(inode)->root;
  4362. int ret;
  4363. struct btrfs_trans_handle *trans;
  4364. unsigned long nr;
  4365. u64 mask = root->sectorsize - 1;
  4366. if (!S_ISREG(inode->i_mode))
  4367. return;
  4368. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  4369. return;
  4370. btrfs_truncate_page(inode->i_mapping, inode->i_size);
  4371. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  4372. trans = btrfs_start_transaction(root, 1);
  4373. /*
  4374. * setattr is responsible for setting the ordered_data_close flag,
  4375. * but that is only tested during the last file release. That
  4376. * could happen well after the next commit, leaving a great big
  4377. * window where new writes may get lost if someone chooses to write
  4378. * to this file after truncating to zero
  4379. *
  4380. * The inode doesn't have any dirty data here, and so if we commit
  4381. * this is a noop. If someone immediately starts writing to the inode
  4382. * it is very likely we'll catch some of their writes in this
  4383. * transaction, and the commit will find this file on the ordered
  4384. * data list with good things to send down.
  4385. *
  4386. * This is a best effort solution, there is still a window where
  4387. * using truncate to replace the contents of the file will
  4388. * end up with a zero length file after a crash.
  4389. */
  4390. if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
  4391. btrfs_add_ordered_operation(trans, root, inode);
  4392. btrfs_set_trans_block_group(trans, inode);
  4393. btrfs_i_size_write(inode, inode->i_size);
  4394. ret = btrfs_orphan_add(trans, inode);
  4395. if (ret)
  4396. goto out;
  4397. /* FIXME, add redo link to tree so we don't leak on crash */
  4398. ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
  4399. BTRFS_EXTENT_DATA_KEY);
  4400. btrfs_update_inode(trans, root, inode);
  4401. ret = btrfs_orphan_del(trans, inode);
  4402. BUG_ON(ret);
  4403. out:
  4404. nr = trans->blocks_used;
  4405. ret = btrfs_end_transaction_throttle(trans, root);
  4406. BUG_ON(ret);
  4407. btrfs_btree_balance_dirty(root, nr);
  4408. }
  4409. /*
  4410. * create a new subvolume directory/inode (helper for the ioctl).
  4411. */
  4412. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  4413. struct btrfs_root *new_root,
  4414. u64 new_dirid, u64 alloc_hint)
  4415. {
  4416. struct inode *inode;
  4417. int err;
  4418. u64 index = 0;
  4419. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
  4420. new_dirid, alloc_hint, S_IFDIR | 0700, &index);
  4421. if (IS_ERR(inode))
  4422. return PTR_ERR(inode);
  4423. inode->i_op = &btrfs_dir_inode_operations;
  4424. inode->i_fop = &btrfs_dir_file_operations;
  4425. inode->i_nlink = 1;
  4426. btrfs_i_size_write(inode, 0);
  4427. err = btrfs_update_inode(trans, new_root, inode);
  4428. BUG_ON(err);
  4429. iput(inode);
  4430. return 0;
  4431. }
  4432. /* helper function for file defrag and space balancing. This
  4433. * forces readahead on a given range of bytes in an inode
  4434. */
  4435. unsigned long btrfs_force_ra(struct address_space *mapping,
  4436. struct file_ra_state *ra, struct file *file,
  4437. pgoff_t offset, pgoff_t last_index)
  4438. {
  4439. pgoff_t req_size = last_index - offset + 1;
  4440. page_cache_sync_readahead(mapping, ra, file, offset, req_size);
  4441. return offset + req_size;
  4442. }
  4443. struct inode *btrfs_alloc_inode(struct super_block *sb)
  4444. {
  4445. struct btrfs_inode *ei;
  4446. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  4447. if (!ei)
  4448. return NULL;
  4449. ei->last_trans = 0;
  4450. ei->logged_trans = 0;
  4451. ei->delalloc_extents = 0;
  4452. ei->delalloc_reserved_extents = 0;
  4453. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  4454. INIT_LIST_HEAD(&ei->i_orphan);
  4455. INIT_LIST_HEAD(&ei->ordered_operations);
  4456. return &ei->vfs_inode;
  4457. }
  4458. void btrfs_destroy_inode(struct inode *inode)
  4459. {
  4460. struct btrfs_ordered_extent *ordered;
  4461. struct btrfs_root *root = BTRFS_I(inode)->root;
  4462. WARN_ON(!list_empty(&inode->i_dentry));
  4463. WARN_ON(inode->i_data.nrpages);
  4464. /*
  4465. * Make sure we're properly removed from the ordered operation
  4466. * lists.
  4467. */
  4468. smp_mb();
  4469. if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
  4470. spin_lock(&root->fs_info->ordered_extent_lock);
  4471. list_del_init(&BTRFS_I(inode)->ordered_operations);
  4472. spin_unlock(&root->fs_info->ordered_extent_lock);
  4473. }
  4474. spin_lock(&root->list_lock);
  4475. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  4476. printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
  4477. " list\n", inode->i_ino);
  4478. dump_stack();
  4479. }
  4480. spin_unlock(&root->list_lock);
  4481. while (1) {
  4482. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  4483. if (!ordered)
  4484. break;
  4485. else {
  4486. printk(KERN_ERR "btrfs found ordered "
  4487. "extent %llu %llu on inode cleanup\n",
  4488. (unsigned long long)ordered->file_offset,
  4489. (unsigned long long)ordered->len);
  4490. btrfs_remove_ordered_extent(inode, ordered);
  4491. btrfs_put_ordered_extent(ordered);
  4492. btrfs_put_ordered_extent(ordered);
  4493. }
  4494. }
  4495. inode_tree_del(inode);
  4496. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  4497. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  4498. }
  4499. void btrfs_drop_inode(struct inode *inode)
  4500. {
  4501. struct btrfs_root *root = BTRFS_I(inode)->root;
  4502. if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
  4503. generic_delete_inode(inode);
  4504. else
  4505. generic_drop_inode(inode);
  4506. }
  4507. static void init_once(void *foo)
  4508. {
  4509. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  4510. inode_init_once(&ei->vfs_inode);
  4511. }
  4512. void btrfs_destroy_cachep(void)
  4513. {
  4514. if (btrfs_inode_cachep)
  4515. kmem_cache_destroy(btrfs_inode_cachep);
  4516. if (btrfs_trans_handle_cachep)
  4517. kmem_cache_destroy(btrfs_trans_handle_cachep);
  4518. if (btrfs_transaction_cachep)
  4519. kmem_cache_destroy(btrfs_transaction_cachep);
  4520. if (btrfs_path_cachep)
  4521. kmem_cache_destroy(btrfs_path_cachep);
  4522. }
  4523. int btrfs_init_cachep(void)
  4524. {
  4525. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  4526. sizeof(struct btrfs_inode), 0,
  4527. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
  4528. if (!btrfs_inode_cachep)
  4529. goto fail;
  4530. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  4531. sizeof(struct btrfs_trans_handle), 0,
  4532. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4533. if (!btrfs_trans_handle_cachep)
  4534. goto fail;
  4535. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  4536. sizeof(struct btrfs_transaction), 0,
  4537. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4538. if (!btrfs_transaction_cachep)
  4539. goto fail;
  4540. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  4541. sizeof(struct btrfs_path), 0,
  4542. SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
  4543. if (!btrfs_path_cachep)
  4544. goto fail;
  4545. return 0;
  4546. fail:
  4547. btrfs_destroy_cachep();
  4548. return -ENOMEM;
  4549. }
  4550. static int btrfs_getattr(struct vfsmount *mnt,
  4551. struct dentry *dentry, struct kstat *stat)
  4552. {
  4553. struct inode *inode = dentry->d_inode;
  4554. generic_fillattr(inode, stat);
  4555. stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
  4556. stat->blksize = PAGE_CACHE_SIZE;
  4557. stat->blocks = (inode_get_bytes(inode) +
  4558. BTRFS_I(inode)->delalloc_bytes) >> 9;
  4559. return 0;
  4560. }
  4561. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  4562. struct inode *new_dir, struct dentry *new_dentry)
  4563. {
  4564. struct btrfs_trans_handle *trans;
  4565. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  4566. struct btrfs_root *dest = BTRFS_I(new_dir)->root;
  4567. struct inode *new_inode = new_dentry->d_inode;
  4568. struct inode *old_inode = old_dentry->d_inode;
  4569. struct timespec ctime = CURRENT_TIME;
  4570. u64 index = 0;
  4571. u64 root_objectid;
  4572. int ret;
  4573. if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
  4574. return -EPERM;
  4575. /* we only allow rename subvolume link between subvolumes */
  4576. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
  4577. return -EXDEV;
  4578. if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
  4579. (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
  4580. return -ENOTEMPTY;
  4581. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  4582. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
  4583. return -ENOTEMPTY;
  4584. /*
  4585. * 2 items for dir items
  4586. * 1 item for orphan entry
  4587. * 1 item for ref
  4588. */
  4589. ret = btrfs_reserve_metadata_space(root, 4);
  4590. if (ret)
  4591. return ret;
  4592. /*
  4593. * we're using rename to replace one file with another.
  4594. * and the replacement file is large. Start IO on it now so
  4595. * we don't add too much work to the end of the transaction
  4596. */
  4597. if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
  4598. old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
  4599. filemap_flush(old_inode->i_mapping);
  4600. /* close the racy window with snapshot create/destroy ioctl */
  4601. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  4602. down_read(&root->fs_info->subvol_sem);
  4603. trans = btrfs_start_transaction(root, 1);
  4604. btrfs_set_trans_block_group(trans, new_dir);
  4605. if (dest != root)
  4606. btrfs_record_root_in_trans(trans, dest);
  4607. ret = btrfs_set_inode_index(new_dir, &index);
  4608. if (ret)
  4609. goto out_fail;
  4610. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4611. /* force full log commit if subvolume involved. */
  4612. root->fs_info->last_trans_log_full_commit = trans->transid;
  4613. } else {
  4614. ret = btrfs_insert_inode_ref(trans, dest,
  4615. new_dentry->d_name.name,
  4616. new_dentry->d_name.len,
  4617. old_inode->i_ino,
  4618. new_dir->i_ino, index);
  4619. if (ret)
  4620. goto out_fail;
  4621. /*
  4622. * this is an ugly little race, but the rename is required
  4623. * to make sure that if we crash, the inode is either at the
  4624. * old name or the new one. pinning the log transaction lets
  4625. * us make sure we don't allow a log commit to come in after
  4626. * we unlink the name but before we add the new name back in.
  4627. */
  4628. btrfs_pin_log_trans(root);
  4629. }
  4630. /*
  4631. * make sure the inode gets flushed if it is replacing
  4632. * something.
  4633. */
  4634. if (new_inode && new_inode->i_size &&
  4635. old_inode && S_ISREG(old_inode->i_mode)) {
  4636. btrfs_add_ordered_operation(trans, root, old_inode);
  4637. }
  4638. old_dir->i_ctime = old_dir->i_mtime = ctime;
  4639. new_dir->i_ctime = new_dir->i_mtime = ctime;
  4640. old_inode->i_ctime = ctime;
  4641. if (old_dentry->d_parent != new_dentry->d_parent)
  4642. btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
  4643. if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
  4644. root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
  4645. ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
  4646. old_dentry->d_name.name,
  4647. old_dentry->d_name.len);
  4648. } else {
  4649. btrfs_inc_nlink(old_dentry->d_inode);
  4650. ret = btrfs_unlink_inode(trans, root, old_dir,
  4651. old_dentry->d_inode,
  4652. old_dentry->d_name.name,
  4653. old_dentry->d_name.len);
  4654. }
  4655. BUG_ON(ret);
  4656. if (new_inode) {
  4657. new_inode->i_ctime = CURRENT_TIME;
  4658. if (unlikely(new_inode->i_ino ==
  4659. BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
  4660. root_objectid = BTRFS_I(new_inode)->location.objectid;
  4661. ret = btrfs_unlink_subvol(trans, dest, new_dir,
  4662. root_objectid,
  4663. new_dentry->d_name.name,
  4664. new_dentry->d_name.len);
  4665. BUG_ON(new_inode->i_nlink == 0);
  4666. } else {
  4667. ret = btrfs_unlink_inode(trans, dest, new_dir,
  4668. new_dentry->d_inode,
  4669. new_dentry->d_name.name,
  4670. new_dentry->d_name.len);
  4671. }
  4672. BUG_ON(ret);
  4673. if (new_inode->i_nlink == 0) {
  4674. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  4675. BUG_ON(ret);
  4676. }
  4677. }
  4678. ret = btrfs_add_link(trans, new_dir, old_inode,
  4679. new_dentry->d_name.name,
  4680. new_dentry->d_name.len, 0, index);
  4681. BUG_ON(ret);
  4682. if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  4683. btrfs_log_new_name(trans, old_inode, old_dir,
  4684. new_dentry->d_parent);
  4685. btrfs_end_log_trans(root);
  4686. }
  4687. out_fail:
  4688. btrfs_end_transaction_throttle(trans, root);
  4689. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  4690. up_read(&root->fs_info->subvol_sem);
  4691. btrfs_unreserve_metadata_space(root, 4);
  4692. return ret;
  4693. }
  4694. /*
  4695. * some fairly slow code that needs optimization. This walks the list
  4696. * of all the inodes with pending delalloc and forces them to disk.
  4697. */
  4698. int btrfs_start_delalloc_inodes(struct btrfs_root *root)
  4699. {
  4700. struct list_head *head = &root->fs_info->delalloc_inodes;
  4701. struct btrfs_inode *binode;
  4702. struct inode *inode;
  4703. if (root->fs_info->sb->s_flags & MS_RDONLY)
  4704. return -EROFS;
  4705. spin_lock(&root->fs_info->delalloc_lock);
  4706. while (!list_empty(head)) {
  4707. binode = list_entry(head->next, struct btrfs_inode,
  4708. delalloc_inodes);
  4709. inode = igrab(&binode->vfs_inode);
  4710. if (!inode)
  4711. list_del_init(&binode->delalloc_inodes);
  4712. spin_unlock(&root->fs_info->delalloc_lock);
  4713. if (inode) {
  4714. filemap_flush(inode->i_mapping);
  4715. iput(inode);
  4716. }
  4717. cond_resched();
  4718. spin_lock(&root->fs_info->delalloc_lock);
  4719. }
  4720. spin_unlock(&root->fs_info->delalloc_lock);
  4721. /* the filemap_flush will queue IO into the worker threads, but
  4722. * we have to make sure the IO is actually started and that
  4723. * ordered extents get created before we return
  4724. */
  4725. atomic_inc(&root->fs_info->async_submit_draining);
  4726. while (atomic_read(&root->fs_info->nr_async_submits) ||
  4727. atomic_read(&root->fs_info->async_delalloc_pages)) {
  4728. wait_event(root->fs_info->async_submit_wait,
  4729. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  4730. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  4731. }
  4732. atomic_dec(&root->fs_info->async_submit_draining);
  4733. return 0;
  4734. }
  4735. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  4736. const char *symname)
  4737. {
  4738. struct btrfs_trans_handle *trans;
  4739. struct btrfs_root *root = BTRFS_I(dir)->root;
  4740. struct btrfs_path *path;
  4741. struct btrfs_key key;
  4742. struct inode *inode = NULL;
  4743. int err;
  4744. int drop_inode = 0;
  4745. u64 objectid;
  4746. u64 index = 0 ;
  4747. int name_len;
  4748. int datasize;
  4749. unsigned long ptr;
  4750. struct btrfs_file_extent_item *ei;
  4751. struct extent_buffer *leaf;
  4752. unsigned long nr = 0;
  4753. name_len = strlen(symname) + 1;
  4754. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  4755. return -ENAMETOOLONG;
  4756. /*
  4757. * 2 items for inode item and ref
  4758. * 2 items for dir items
  4759. * 1 item for xattr if selinux is on
  4760. */
  4761. err = btrfs_reserve_metadata_space(root, 5);
  4762. if (err)
  4763. return err;
  4764. trans = btrfs_start_transaction(root, 1);
  4765. if (!trans)
  4766. goto out_fail;
  4767. btrfs_set_trans_block_group(trans, dir);
  4768. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  4769. if (err) {
  4770. err = -ENOSPC;
  4771. goto out_unlock;
  4772. }
  4773. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4774. dentry->d_name.len,
  4775. dentry->d_parent->d_inode->i_ino, objectid,
  4776. BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
  4777. &index);
  4778. err = PTR_ERR(inode);
  4779. if (IS_ERR(inode))
  4780. goto out_unlock;
  4781. err = btrfs_init_inode_security(inode, dir);
  4782. if (err) {
  4783. drop_inode = 1;
  4784. goto out_unlock;
  4785. }
  4786. btrfs_set_trans_block_group(trans, inode);
  4787. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  4788. if (err)
  4789. drop_inode = 1;
  4790. else {
  4791. inode->i_mapping->a_ops = &btrfs_aops;
  4792. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4793. inode->i_fop = &btrfs_file_operations;
  4794. inode->i_op = &btrfs_file_inode_operations;
  4795. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  4796. }
  4797. btrfs_update_inode_block_group(trans, inode);
  4798. btrfs_update_inode_block_group(trans, dir);
  4799. if (drop_inode)
  4800. goto out_unlock;
  4801. path = btrfs_alloc_path();
  4802. BUG_ON(!path);
  4803. key.objectid = inode->i_ino;
  4804. key.offset = 0;
  4805. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  4806. datasize = btrfs_file_extent_calc_inline_size(name_len);
  4807. err = btrfs_insert_empty_item(trans, root, path, &key,
  4808. datasize);
  4809. if (err) {
  4810. drop_inode = 1;
  4811. goto out_unlock;
  4812. }
  4813. leaf = path->nodes[0];
  4814. ei = btrfs_item_ptr(leaf, path->slots[0],
  4815. struct btrfs_file_extent_item);
  4816. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  4817. btrfs_set_file_extent_type(leaf, ei,
  4818. BTRFS_FILE_EXTENT_INLINE);
  4819. btrfs_set_file_extent_encryption(leaf, ei, 0);
  4820. btrfs_set_file_extent_compression(leaf, ei, 0);
  4821. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  4822. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  4823. ptr = btrfs_file_extent_inline_start(ei);
  4824. write_extent_buffer(leaf, symname, ptr, name_len);
  4825. btrfs_mark_buffer_dirty(leaf);
  4826. btrfs_free_path(path);
  4827. inode->i_op = &btrfs_symlink_inode_operations;
  4828. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  4829. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4830. inode_set_bytes(inode, name_len);
  4831. btrfs_i_size_write(inode, name_len - 1);
  4832. err = btrfs_update_inode(trans, root, inode);
  4833. if (err)
  4834. drop_inode = 1;
  4835. out_unlock:
  4836. nr = trans->blocks_used;
  4837. btrfs_end_transaction_throttle(trans, root);
  4838. out_fail:
  4839. btrfs_unreserve_metadata_space(root, 5);
  4840. if (drop_inode) {
  4841. inode_dec_link_count(inode);
  4842. iput(inode);
  4843. }
  4844. btrfs_btree_balance_dirty(root, nr);
  4845. return err;
  4846. }
  4847. static int prealloc_file_range(struct btrfs_trans_handle *trans,
  4848. struct inode *inode, u64 start, u64 end,
  4849. u64 locked_end, u64 alloc_hint, int mode)
  4850. {
  4851. struct btrfs_root *root = BTRFS_I(inode)->root;
  4852. struct btrfs_key ins;
  4853. u64 alloc_size;
  4854. u64 cur_offset = start;
  4855. u64 num_bytes = end - start;
  4856. int ret = 0;
  4857. while (num_bytes > 0) {
  4858. alloc_size = min(num_bytes, root->fs_info->max_extent);
  4859. ret = btrfs_reserve_metadata_space(root, 1);
  4860. if (ret)
  4861. goto out;
  4862. ret = btrfs_reserve_extent(trans, root, alloc_size,
  4863. root->sectorsize, 0, alloc_hint,
  4864. (u64)-1, &ins, 1);
  4865. if (ret) {
  4866. WARN_ON(1);
  4867. goto out;
  4868. }
  4869. ret = insert_reserved_file_extent(trans, inode,
  4870. cur_offset, ins.objectid,
  4871. ins.offset, ins.offset,
  4872. ins.offset, locked_end,
  4873. 0, 0, 0,
  4874. BTRFS_FILE_EXTENT_PREALLOC);
  4875. BUG_ON(ret);
  4876. btrfs_drop_extent_cache(inode, cur_offset,
  4877. cur_offset + ins.offset -1, 0);
  4878. num_bytes -= ins.offset;
  4879. cur_offset += ins.offset;
  4880. alloc_hint = ins.objectid + ins.offset;
  4881. btrfs_unreserve_metadata_space(root, 1);
  4882. }
  4883. out:
  4884. if (cur_offset > start) {
  4885. inode->i_ctime = CURRENT_TIME;
  4886. BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
  4887. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  4888. cur_offset > i_size_read(inode))
  4889. btrfs_i_size_write(inode, cur_offset);
  4890. ret = btrfs_update_inode(trans, root, inode);
  4891. BUG_ON(ret);
  4892. }
  4893. return ret;
  4894. }
  4895. static long btrfs_fallocate(struct inode *inode, int mode,
  4896. loff_t offset, loff_t len)
  4897. {
  4898. u64 cur_offset;
  4899. u64 last_byte;
  4900. u64 alloc_start;
  4901. u64 alloc_end;
  4902. u64 alloc_hint = 0;
  4903. u64 locked_end;
  4904. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  4905. struct extent_map *em;
  4906. struct btrfs_trans_handle *trans;
  4907. struct btrfs_root *root;
  4908. int ret;
  4909. alloc_start = offset & ~mask;
  4910. alloc_end = (offset + len + mask) & ~mask;
  4911. /*
  4912. * wait for ordered IO before we have any locks. We'll loop again
  4913. * below with the locks held.
  4914. */
  4915. btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
  4916. mutex_lock(&inode->i_mutex);
  4917. if (alloc_start > inode->i_size) {
  4918. ret = btrfs_cont_expand(inode, alloc_start);
  4919. if (ret)
  4920. goto out;
  4921. }
  4922. root = BTRFS_I(inode)->root;
  4923. ret = btrfs_check_data_free_space(root, inode,
  4924. alloc_end - alloc_start);
  4925. if (ret)
  4926. goto out;
  4927. locked_end = alloc_end - 1;
  4928. while (1) {
  4929. struct btrfs_ordered_extent *ordered;
  4930. trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
  4931. if (!trans) {
  4932. ret = -EIO;
  4933. goto out_free;
  4934. }
  4935. /* the extent lock is ordered inside the running
  4936. * transaction
  4937. */
  4938. lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  4939. GFP_NOFS);
  4940. ordered = btrfs_lookup_first_ordered_extent(inode,
  4941. alloc_end - 1);
  4942. if (ordered &&
  4943. ordered->file_offset + ordered->len > alloc_start &&
  4944. ordered->file_offset < alloc_end) {
  4945. btrfs_put_ordered_extent(ordered);
  4946. unlock_extent(&BTRFS_I(inode)->io_tree,
  4947. alloc_start, locked_end, GFP_NOFS);
  4948. btrfs_end_transaction(trans, BTRFS_I(inode)->root);
  4949. /*
  4950. * we can't wait on the range with the transaction
  4951. * running or with the extent lock held
  4952. */
  4953. btrfs_wait_ordered_range(inode, alloc_start,
  4954. alloc_end - alloc_start);
  4955. } else {
  4956. if (ordered)
  4957. btrfs_put_ordered_extent(ordered);
  4958. break;
  4959. }
  4960. }
  4961. cur_offset = alloc_start;
  4962. while (1) {
  4963. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  4964. alloc_end - cur_offset, 0);
  4965. BUG_ON(IS_ERR(em) || !em);
  4966. last_byte = min(extent_map_end(em), alloc_end);
  4967. last_byte = (last_byte + mask) & ~mask;
  4968. if (em->block_start == EXTENT_MAP_HOLE) {
  4969. ret = prealloc_file_range(trans, inode, cur_offset,
  4970. last_byte, locked_end + 1,
  4971. alloc_hint, mode);
  4972. if (ret < 0) {
  4973. free_extent_map(em);
  4974. break;
  4975. }
  4976. }
  4977. if (em->block_start <= EXTENT_MAP_LAST_BYTE)
  4978. alloc_hint = em->block_start;
  4979. free_extent_map(em);
  4980. cur_offset = last_byte;
  4981. if (cur_offset >= alloc_end) {
  4982. ret = 0;
  4983. break;
  4984. }
  4985. }
  4986. unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
  4987. GFP_NOFS);
  4988. btrfs_end_transaction(trans, BTRFS_I(inode)->root);
  4989. out_free:
  4990. btrfs_free_reserved_data_space(root, inode, alloc_end - alloc_start);
  4991. out:
  4992. mutex_unlock(&inode->i_mutex);
  4993. return ret;
  4994. }
  4995. static int btrfs_set_page_dirty(struct page *page)
  4996. {
  4997. return __set_page_dirty_nobuffers(page);
  4998. }
  4999. static int btrfs_permission(struct inode *inode, int mask)
  5000. {
  5001. if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
  5002. return -EACCES;
  5003. return generic_permission(inode, mask, btrfs_check_acl);
  5004. }
  5005. static struct inode_operations btrfs_dir_inode_operations = {
  5006. .getattr = btrfs_getattr,
  5007. .lookup = btrfs_lookup,
  5008. .create = btrfs_create,
  5009. .unlink = btrfs_unlink,
  5010. .link = btrfs_link,
  5011. .mkdir = btrfs_mkdir,
  5012. .rmdir = btrfs_rmdir,
  5013. .rename = btrfs_rename,
  5014. .symlink = btrfs_symlink,
  5015. .setattr = btrfs_setattr,
  5016. .mknod = btrfs_mknod,
  5017. .setxattr = btrfs_setxattr,
  5018. .getxattr = btrfs_getxattr,
  5019. .listxattr = btrfs_listxattr,
  5020. .removexattr = btrfs_removexattr,
  5021. .permission = btrfs_permission,
  5022. };
  5023. static struct inode_operations btrfs_dir_ro_inode_operations = {
  5024. .lookup = btrfs_lookup,
  5025. .permission = btrfs_permission,
  5026. };
  5027. static struct file_operations btrfs_dir_file_operations = {
  5028. .llseek = generic_file_llseek,
  5029. .read = generic_read_dir,
  5030. .readdir = btrfs_real_readdir,
  5031. .unlocked_ioctl = btrfs_ioctl,
  5032. #ifdef CONFIG_COMPAT
  5033. .compat_ioctl = btrfs_ioctl,
  5034. #endif
  5035. .release = btrfs_release_file,
  5036. .fsync = btrfs_sync_file,
  5037. };
  5038. static struct extent_io_ops btrfs_extent_io_ops = {
  5039. .fill_delalloc = run_delalloc_range,
  5040. .submit_bio_hook = btrfs_submit_bio_hook,
  5041. .merge_bio_hook = btrfs_merge_bio_hook,
  5042. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  5043. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  5044. .writepage_start_hook = btrfs_writepage_start_hook,
  5045. .readpage_io_failed_hook = btrfs_io_failed_hook,
  5046. .set_bit_hook = btrfs_set_bit_hook,
  5047. .clear_bit_hook = btrfs_clear_bit_hook,
  5048. .merge_extent_hook = btrfs_merge_extent_hook,
  5049. .split_extent_hook = btrfs_split_extent_hook,
  5050. };
  5051. /*
  5052. * btrfs doesn't support the bmap operation because swapfiles
  5053. * use bmap to make a mapping of extents in the file. They assume
  5054. * these extents won't change over the life of the file and they
  5055. * use the bmap result to do IO directly to the drive.
  5056. *
  5057. * the btrfs bmap call would return logical addresses that aren't
  5058. * suitable for IO and they also will change frequently as COW
  5059. * operations happen. So, swapfile + btrfs == corruption.
  5060. *
  5061. * For now we're avoiding this by dropping bmap.
  5062. */
  5063. static struct address_space_operations btrfs_aops = {
  5064. .readpage = btrfs_readpage,
  5065. .writepage = btrfs_writepage,
  5066. .writepages = btrfs_writepages,
  5067. .readpages = btrfs_readpages,
  5068. .sync_page = block_sync_page,
  5069. .direct_IO = btrfs_direct_IO,
  5070. .invalidatepage = btrfs_invalidatepage,
  5071. .releasepage = btrfs_releasepage,
  5072. .set_page_dirty = btrfs_set_page_dirty,
  5073. };
  5074. static struct address_space_operations btrfs_symlink_aops = {
  5075. .readpage = btrfs_readpage,
  5076. .writepage = btrfs_writepage,
  5077. .invalidatepage = btrfs_invalidatepage,
  5078. .releasepage = btrfs_releasepage,
  5079. };
  5080. static struct inode_operations btrfs_file_inode_operations = {
  5081. .truncate = btrfs_truncate,
  5082. .getattr = btrfs_getattr,
  5083. .setattr = btrfs_setattr,
  5084. .setxattr = btrfs_setxattr,
  5085. .getxattr = btrfs_getxattr,
  5086. .listxattr = btrfs_listxattr,
  5087. .removexattr = btrfs_removexattr,
  5088. .permission = btrfs_permission,
  5089. .fallocate = btrfs_fallocate,
  5090. .fiemap = btrfs_fiemap,
  5091. };
  5092. static struct inode_operations btrfs_special_inode_operations = {
  5093. .getattr = btrfs_getattr,
  5094. .setattr = btrfs_setattr,
  5095. .permission = btrfs_permission,
  5096. .setxattr = btrfs_setxattr,
  5097. .getxattr = btrfs_getxattr,
  5098. .listxattr = btrfs_listxattr,
  5099. .removexattr = btrfs_removexattr,
  5100. };
  5101. static struct inode_operations btrfs_symlink_inode_operations = {
  5102. .readlink = generic_readlink,
  5103. .follow_link = page_follow_link_light,
  5104. .put_link = page_put_link,
  5105. .permission = btrfs_permission,
  5106. .setxattr = btrfs_setxattr,
  5107. .getxattr = btrfs_getxattr,
  5108. .listxattr = btrfs_listxattr,
  5109. .removexattr = btrfs_removexattr,
  5110. };
  5111. struct dentry_operations btrfs_dentry_operations = {
  5112. .d_delete = btrfs_dentry_delete,
  5113. };