inode.c 195 KB

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