inode.c 197 KB

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