inode.c 199 KB

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