inode.c 193 KB

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